blob: a6a4226a922fc80ddab9ba589cbb664ffa36de70 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#include <net/ip.h>
75#include <net/icmp.h>
76#include <net/route.h>
77#include <net/ipv6.h>
78#include <net/inet_common.h>
79
80#include <linux/socket.h> /* for sa_family_t */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040081#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <net/sock.h>
83#include <net/sctp/sctp.h>
84#include <net/sctp/sm.h>
85
86/* WARNING: Please do not remove the SCTP_STATIC attribute to
87 * any of the functions below as they are used to export functions
88 * used by a project regression testsuite.
89 */
90
91/* Forward declarations for internal helper functions. */
92static int sctp_writeable(struct sock *sk);
93static void sctp_wfree(struct sk_buff *skb);
94static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
95 size_t msg_len);
96static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
97static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
98static int sctp_wait_for_accept(struct sock *sk, long timeo);
99static void sctp_wait_for_close(struct sock *sk, long timeo);
100static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
101 union sctp_addr *addr, int len);
102static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
103static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
104static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
105static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
106static int sctp_send_asconf(struct sctp_association *asoc,
107 struct sctp_chunk *chunk);
108static int sctp_do_bind(struct sock *, union sctp_addr *, int);
109static int sctp_autobind(struct sock *sk);
110static void sctp_sock_migrate(struct sock *, struct sock *,
111 struct sctp_association *, sctp_socket_type_t);
112static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
113
Neil Horman4d93df02007-08-15 16:07:44 -0700114extern struct kmem_cache *sctp_bucket_cachep;
Eric Dumazet8d987e52010-11-09 23:24:26 +0000115extern long sysctl_sctp_mem[3];
Neil Horman4d93df02007-08-15 16:07:44 -0700116extern int sysctl_sctp_rmem[3];
117extern int sysctl_sctp_wmem[3];
118
Adrian Bunkb6fa1a42007-09-12 15:18:00 +0200119static int sctp_memory_pressure;
Eric Dumazet8d987e52010-11-09 23:24:26 +0000120static atomic_long_t sctp_memory_allocated;
Eric Dumazet17483762008-11-25 21:16:35 -0800121struct percpu_counter sctp_sockets_allocated;
Neil Horman4d93df02007-08-15 16:07:44 -0700122
Pavel Emelyanov5c52ba12008-07-16 20:28:10 -0700123static void sctp_enter_memory_pressure(struct sock *sk)
Neil Horman4d93df02007-08-15 16:07:44 -0700124{
125 sctp_memory_pressure = 1;
126}
127
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/* Get the sndbuf space available at the time on the association. */
130static inline int sctp_wspace(struct sctp_association *asoc)
131{
Neil Horman4d93df02007-08-15 16:07:44 -0700132 int amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Neil Horman4d93df02007-08-15 16:07:44 -0700134 if (asoc->ep->sndbuf_policy)
135 amt = asoc->sndbuf_used;
136 else
Eric Dumazet31e6d362009-06-17 19:05:41 -0700137 amt = sk_wmem_alloc_get(asoc->base.sk);
Neil Horman4d93df02007-08-15 16:07:44 -0700138
139 if (amt >= asoc->base.sk->sk_sndbuf) {
140 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
141 amt = 0;
142 else {
143 amt = sk_stream_wspace(asoc->base.sk);
144 if (amt < 0)
145 amt = 0;
146 }
Neil Horman4eb701d2005-04-28 12:02:04 -0700147 } else {
Neil Horman4d93df02007-08-15 16:07:44 -0700148 amt = asoc->base.sk->sk_sndbuf - amt;
Neil Horman4eb701d2005-04-28 12:02:04 -0700149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return amt;
151}
152
153/* Increment the used sndbuf space count of the corresponding association by
154 * the size of the outgoing data chunk.
155 * Also, set the skb destructor for sndbuf accounting later.
156 *
157 * Since it is always 1-1 between chunk and skb, and also a new skb is always
158 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
159 * destructor in the data chunk skb for the purpose of the sndbuf space
160 * tracking.
161 */
162static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
163{
164 struct sctp_association *asoc = chunk->asoc;
165 struct sock *sk = asoc->base.sk;
166
167 /* The sndbuf space is tracked per association. */
168 sctp_association_hold(asoc);
169
Neil Horman4eb701d2005-04-28 12:02:04 -0700170 skb_set_owner_w(chunk->skb, sk);
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 chunk->skb->destructor = sctp_wfree;
173 /* Save the chunk pointer in skb for sctp_wfree to use later. */
174 *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
175
Neil Horman4eb701d2005-04-28 12:02:04 -0700176 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
177 sizeof(struct sk_buff) +
178 sizeof(struct sctp_chunk);
179
Neil Horman4eb701d2005-04-28 12:02:04 -0700180 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800181 sk->sk_wmem_queued += chunk->skb->truesize;
182 sk_mem_charge(sk, chunk->skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185/* Verify that this is a valid address. */
186static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
187 int len)
188{
189 struct sctp_af *af;
190
191 /* Verify basic sockaddr. */
192 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
193 if (!af)
194 return -EINVAL;
195
196 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700197 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return -EINVAL;
199
200 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
201 return -EINVAL;
202
203 return 0;
204}
205
206/* Look up the association by its id. If this is not a UDP-style
207 * socket, the ID field is always ignored.
208 */
209struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
210{
211 struct sctp_association *asoc = NULL;
212
213 /* If this is not a UDP-style socket, assoc id should be ignored. */
214 if (!sctp_style(sk, UDP)) {
215 /* Return NULL if the socket state is not ESTABLISHED. It
216 * could be a TCP-style listening socket or a socket which
217 * hasn't yet called connect() to establish an association.
218 */
219 if (!sctp_sstate(sk, ESTABLISHED))
220 return NULL;
221
222 /* Get the first and the only association from the list. */
223 if (!list_empty(&sctp_sk(sk)->ep->asocs))
224 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
225 struct sctp_association, asocs);
226 return asoc;
227 }
228
229 /* Otherwise this is a UDP-style socket. */
230 if (!id || (id == (sctp_assoc_t)-1))
231 return NULL;
232
233 spin_lock_bh(&sctp_assocs_id_lock);
234 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
235 spin_unlock_bh(&sctp_assocs_id_lock);
236
237 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
238 return NULL;
239
240 return asoc;
241}
242
243/* Look up the transport from an address and an assoc id. If both address and
244 * id are specified, the associations matching the address and the id should be
245 * the same.
246 */
247static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
248 struct sockaddr_storage *addr,
249 sctp_assoc_t id)
250{
251 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
252 struct sctp_transport *transport;
253 union sctp_addr *laddr = (union sctp_addr *)addr;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800256 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 if (!addr_asoc)
260 return NULL;
261
262 id_asoc = sctp_id2assoc(sk, id);
263 if (id_asoc && (id_asoc != addr_asoc))
264 return NULL;
265
266 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
267 (union sctp_addr *)addr);
268
269 return transport;
270}
271
272/* API 3.1.2 bind() - UDP Style Syntax
273 * The syntax of bind() is,
274 *
275 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
276 *
277 * sd - the socket descriptor returned by socket().
278 * addr - the address structure (struct sockaddr_in or struct
279 * sockaddr_in6 [RFC 2553]),
280 * addr_len - the size of the address structure.
281 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700282SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 int retval = 0;
285
286 sctp_lock_sock(sk);
287
Frank Filz3f7a87d2005-06-20 13:14:57 -0700288 SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
289 sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 /* Disallow binding twice. */
292 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700293 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 addr_len);
295 else
296 retval = -EINVAL;
297
298 sctp_release_sock(sk);
299
300 return retval;
301}
302
303static long sctp_get_port_local(struct sock *, union sctp_addr *);
304
305/* Verify this is a valid sockaddr. */
306static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
307 union sctp_addr *addr, int len)
308{
309 struct sctp_af *af;
310
311 /* Check minimum size. */
312 if (len < sizeof (struct sockaddr))
313 return NULL;
314
Vlad Yasevich7dab83d2008-07-18 23:05:40 -0700315 /* V4 mapped address are really of AF_INET family */
316 if (addr->sa.sa_family == AF_INET6 &&
317 ipv6_addr_v4mapped(&addr->v6.sin6_addr)) {
318 if (!opt->pf->af_supported(AF_INET, opt))
319 return NULL;
320 } else {
321 /* Does this PF support this AF? */
322 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
323 return NULL;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /* If we get this far, af is valid. */
327 af = sctp_get_af_specific(addr->sa.sa_family);
328
329 if (len < af->sockaddr_len)
330 return NULL;
331
332 return af;
333}
334
335/* Bind a local address either to an endpoint or to an association. */
336SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
337{
338 struct sctp_sock *sp = sctp_sk(sk);
339 struct sctp_endpoint *ep = sp->ep;
340 struct sctp_bind_addr *bp = &ep->base.bind_addr;
341 struct sctp_af *af;
342 unsigned short snum;
343 int ret = 0;
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /* Common sockaddr verification. */
346 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700347 if (!af) {
348 SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
349 sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700351 }
352
353 snum = ntohs(addr->v4.sin_port);
354
355 SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
356 ", port: %d, new port: %d, len: %d)\n",
357 sk,
358 addr,
359 bp->port, snum,
360 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 /* PF specific bind() address verification. */
363 if (!sp->pf->bind_verify(sp, addr))
364 return -EADDRNOTAVAIL;
365
Vlad Yasevich8b358052007-05-15 17:14:58 -0400366 /* We must either be unbound, or bind to the same port.
367 * It's OK to allow 0 ports if we are already bound.
368 * We'll just inhert an already bound port in this case
369 */
370 if (bp->port) {
371 if (!snum)
372 snum = bp->port;
373 else if (snum != bp->port) {
374 SCTP_DEBUG_PRINTK("sctp_do_bind:"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 " New port %d does not match existing port "
376 "%d.\n", snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400377 return -EINVAL;
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
381 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
382 return -EACCES;
383
Vlad Yasevich4e540642008-07-18 23:06:32 -0700384 /* See if the address matches any of the addresses we may have
385 * already bound before checking against other endpoints.
386 */
387 if (sctp_bind_addr_match(bp, addr, sp))
388 return -EINVAL;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* Make sure we are allowed to bind here.
391 * The function sctp_get_port_local() does duplicate address
392 * detection.
393 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900394 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if ((ret = sctp_get_port_local(sk, addr))) {
Vlad Yasevich4e540642008-07-18 23:06:32 -0700396 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
399 /* Refresh ephemeral port. */
400 if (!bp->port)
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000401 bp->port = inet_sk(sk)->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Vlad Yasevich559cf712007-09-16 16:03:28 -0700403 /* Add the address to the bind address list.
404 * Use GFP_ATOMIC since BHs will be disabled.
405 */
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800406 ret = sctp_add_bind_addr(bp, addr, SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* Copy back into socket for getsockname() use. */
409 if (!ret) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000410 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 af->to_sk_saddr(addr, sk);
412 }
413
414 return ret;
415}
416
417 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
418 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900419 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900421 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900423 * subsequent ASCONF. Note this restriction binds each side, so at any
424 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * from each endpoint).
426 */
427static int sctp_send_asconf(struct sctp_association *asoc,
428 struct sctp_chunk *chunk)
429{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000430 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 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);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000443 retval = sctp_primitive_ASCONF(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (retval)
445 sctp_chunk_free(chunk);
446 else
447 asoc->addip_last_asconf = chunk;
448
449out:
450 return retval;
451}
452
453/* Add a list of addresses as bind addresses to local endpoint or
454 * association.
455 *
456 * Basically run through each address specified in the addrs/addrcnt
457 * array/length pair, determine if it is IPv6 or IPv4 and call
458 * sctp_do_bind() on it.
459 *
460 * If any of them fails, then the operation will be reversed and the
461 * ones that were added will be removed.
462 *
463 * Only sctp_setsockopt_bindx() is supposed to call this function.
464 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200465static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 int cnt;
468 int retval = 0;
469 void *addr_buf;
470 struct sockaddr *sa_addr;
471 struct sctp_af *af;
472
473 SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
474 sk, addrs, addrcnt);
475
476 addr_buf = addrs;
477 for (cnt = 0; cnt < addrcnt; cnt++) {
478 /* The list may contain either IPv4 or IPv6 address;
479 * determine the address length for walking thru the list.
480 */
Joe Perchesea110732011-06-13 16:21:26 +0000481 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 af = sctp_get_af_specific(sa_addr->sa_family);
483 if (!af) {
484 retval = -EINVAL;
485 goto err_bindx_add;
486 }
487
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900488 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 af->sockaddr_len);
490
491 addr_buf += af->sockaddr_len;
492
493err_bindx_add:
494 if (retval < 0) {
495 /* Failed. Cleanup the ones that have been added */
496 if (cnt > 0)
497 sctp_bindx_rem(sk, addrs, cnt);
498 return retval;
499 }
500 }
501
502 return retval;
503}
504
505/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
506 * associations that are part of the endpoint indicating that a list of local
507 * addresses are added to the endpoint.
508 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900509 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 * association, we do not send the chunk for that association. But it will not
511 * affect other associations.
512 *
513 * Only sctp_setsockopt_bindx() is supposed to call this function.
514 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900515static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct sockaddr *addrs,
517 int addrcnt)
518{
519 struct sctp_sock *sp;
520 struct sctp_endpoint *ep;
521 struct sctp_association *asoc;
522 struct sctp_bind_addr *bp;
523 struct sctp_chunk *chunk;
524 struct sctp_sockaddr_entry *laddr;
525 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700526 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 void *addr_buf;
528 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 struct list_head *p;
530 int i;
531 int retval = 0;
532
533 if (!sctp_addip_enable)
534 return retval;
535
536 sp = sctp_sk(sk);
537 ep = sp->ep;
538
539 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800540 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700542 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 if (!asoc->peer.asconf_capable)
545 continue;
546
547 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
548 continue;
549
550 if (!sctp_state(asoc, ESTABLISHED))
551 continue;
552
553 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900554 * in the bind address list of the association. If so,
555 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * other associations.
557 */
558 addr_buf = addrs;
559 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000560 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 af = sctp_get_af_specific(addr->v4.sin_family);
562 if (!af) {
563 retval = -EINVAL;
564 goto out;
565 }
566
567 if (sctp_assoc_lookup_laddr(asoc, addr))
568 break;
569
570 addr_buf += af->sockaddr_len;
571 }
572 if (i < addrcnt)
573 continue;
574
Vlad Yasevich559cf712007-09-16 16:03:28 -0700575 /* Use the first valid address in bind addr list of
576 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 bp = &asoc->base.bind_addr;
579 p = bp->address_list.next;
580 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800581 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 addrcnt, SCTP_PARAM_ADD_IP);
583 if (!chunk) {
584 retval = -ENOMEM;
585 goto out;
586 }
587
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700588 /* Add the new addresses to the bind address list with
589 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700591 addr_buf = addrs;
592 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000593 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700594 af = sctp_get_af_specific(addr->v4.sin_family);
595 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800596 retval = sctp_add_bind_addr(bp, &saveaddr,
597 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700598 addr_buf += af->sockaddr_len;
599 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900600 if (asoc->src_out_of_asoc_ok) {
601 struct sctp_transport *trans;
602
603 list_for_each_entry(trans,
604 &asoc->peer.transport_addr_list, transports) {
605 /* Clear the source and route cache */
606 dst_release(trans->dst);
607 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
608 2*asoc->pathmtu, 4380));
609 trans->ssthresh = asoc->peer.i.a_rwnd;
610 trans->rto = asoc->rto_initial;
611 trans->rtt = trans->srtt = trans->rttvar = 0;
612 sctp_transport_route(trans, NULL,
613 sctp_sk(asoc->base.sk));
614 }
615 }
616 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
619out:
620 return retval;
621}
622
623/* Remove a list of addresses from bind addresses list. Do not remove the
624 * last address.
625 *
626 * Basically run through each address specified in the addrs/addrcnt
627 * array/length pair, determine if it is IPv6 or IPv4 and call
628 * sctp_del_bind() on it.
629 *
630 * If any of them fails, then the operation will be reversed and the
631 * ones that were removed will be added back.
632 *
633 * At least one address has to be left; if only one address is
634 * available, the operation will return -EBUSY.
635 *
636 * Only sctp_setsockopt_bindx() is supposed to call this function.
637 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200638static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct sctp_sock *sp = sctp_sk(sk);
641 struct sctp_endpoint *ep = sp->ep;
642 int cnt;
643 struct sctp_bind_addr *bp = &ep->base.bind_addr;
644 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800646 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct sctp_af *af;
648
649 SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
650 sk, addrs, addrcnt);
651
652 addr_buf = addrs;
653 for (cnt = 0; cnt < addrcnt; cnt++) {
654 /* If the bind address list is empty or if there is only one
655 * bind address, there is nothing more to be removed (we need
656 * at least one address here).
657 */
658 if (list_empty(&bp->address_list) ||
659 (sctp_list_single_entry(&bp->address_list))) {
660 retval = -EBUSY;
661 goto err_bindx_rem;
662 }
663
Joe Perchesea110732011-06-13 16:21:26 +0000664 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800665 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (!af) {
667 retval = -EINVAL;
668 goto err_bindx_rem;
669 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700670
671 if (!af->addr_valid(sa_addr, sp, NULL)) {
672 retval = -EADDRNOTAVAIL;
673 goto err_bindx_rem;
674 }
675
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000676 if (sa_addr->v4.sin_port &&
677 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 retval = -EINVAL;
679 goto err_bindx_rem;
680 }
681
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000682 if (!sa_addr->v4.sin_port)
683 sa_addr->v4.sin_port = htons(bp->port);
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* FIXME - There is probably a need to check if sk->sk_saddr and
686 * sk->sk_rcv_addr are currently set to one of the addresses to
687 * be removed. This is something which needs to be looked into
688 * when we are fixing the outstanding issues with multi-homing
689 * socket routing and failover schemes. Refer to comments in
690 * sctp_do_bind(). -daisy
691 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400692 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 addr_buf += af->sockaddr_len;
695err_bindx_rem:
696 if (retval < 0) {
697 /* Failed. Add the ones that has been removed back */
698 if (cnt > 0)
699 sctp_bindx_add(sk, addrs, cnt);
700 return retval;
701 }
702 }
703
704 return retval;
705}
706
707/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
708 * the associations that are part of the endpoint indicating that a list of
709 * local addresses are removed from the endpoint.
710 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900711 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 * association, we do not send the chunk for that association. But it will not
713 * affect other associations.
714 *
715 * Only sctp_setsockopt_bindx() is supposed to call this function.
716 */
717static int sctp_send_asconf_del_ip(struct sock *sk,
718 struct sockaddr *addrs,
719 int addrcnt)
720{
721 struct sctp_sock *sp;
722 struct sctp_endpoint *ep;
723 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700724 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct sctp_bind_addr *bp;
726 struct sctp_chunk *chunk;
727 union sctp_addr *laddr;
728 void *addr_buf;
729 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700730 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 int i;
732 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900733 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Michio Honda8a07eb02011-04-26 20:19:36 +0900735 chunk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (!sctp_addip_enable)
737 return retval;
738
739 sp = sctp_sk(sk);
740 ep = sp->ep;
741
742 SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800743 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700745 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 if (!asoc->peer.asconf_capable)
748 continue;
749
750 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
751 continue;
752
753 if (!sctp_state(asoc, ESTABLISHED))
754 continue;
755
756 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900757 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 * If so, do not send the asconf chunk to its peer, but
759 * continue with other associations.
760 */
761 addr_buf = addrs;
762 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000763 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 af = sctp_get_af_specific(laddr->v4.sin_family);
765 if (!af) {
766 retval = -EINVAL;
767 goto out;
768 }
769
770 if (!sctp_assoc_lookup_laddr(asoc, laddr))
771 break;
772
773 addr_buf += af->sockaddr_len;
774 }
775 if (i < addrcnt)
776 continue;
777
778 /* Find one address in the association's bind address list
779 * that is not in the packed array of addresses. This is to
780 * make sure that we do not delete all the addresses in the
781 * association.
782 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 bp = &asoc->base.bind_addr;
784 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
785 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900786 if ((laddr == NULL) && (addrcnt == 1)) {
787 if (asoc->asconf_addr_del_pending)
788 continue;
789 asoc->asconf_addr_del_pending =
790 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900791 if (asoc->asconf_addr_del_pending == NULL) {
792 retval = -ENOMEM;
793 goto out;
794 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900795 asoc->asconf_addr_del_pending->sa.sa_family =
796 addrs->sa_family;
797 asoc->asconf_addr_del_pending->v4.sin_port =
798 htons(bp->port);
799 if (addrs->sa_family == AF_INET) {
800 struct sockaddr_in *sin;
801
802 sin = (struct sockaddr_in *)addrs;
803 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
804 } else if (addrs->sa_family == AF_INET6) {
805 struct sockaddr_in6 *sin6;
806
807 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000808 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900809 }
810 SCTP_DEBUG_PRINTK_IPADDR("send_asconf_del_ip: keep the last address asoc: %p ",
811 " at %p\n", asoc, asoc->asconf_addr_del_pending,
812 asoc->asconf_addr_del_pending);
813 asoc->src_out_of_asoc_ok = 1;
814 stored = 1;
815 goto skip_mkasconf;
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Vlad Yasevich559cf712007-09-16 16:03:28 -0700818 /* We do not need RCU protection throughout this loop
819 * because this is done under a socket lock from the
820 * setsockopt call.
821 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
823 SCTP_PARAM_DEL_IP);
824 if (!chunk) {
825 retval = -ENOMEM;
826 goto out;
827 }
828
Michio Honda8a07eb02011-04-26 20:19:36 +0900829skip_mkasconf:
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700830 /* Reset use_as_src flag for the addresses in the bind address
831 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700833 addr_buf = addrs;
834 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000835 laddr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700836 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700837 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a12006-11-20 17:05:23 -0800838 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800839 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700840 }
841 addr_buf += af->sockaddr_len;
842 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700843
844 /* Update the route and saddr entries for all the transports
845 * as some of the addresses in the bind address list are
846 * about to be deleted and cannot be used as source addresses.
847 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700848 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
849 transports) {
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700850 dst_release(transport->dst);
851 sctp_transport_route(transport, NULL,
852 sctp_sk(asoc->base.sk));
853 }
854
Michio Honda8a07eb02011-04-26 20:19:36 +0900855 if (stored)
856 /* We don't need to transmit ASCONF */
857 continue;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700858 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860out:
861 return retval;
862}
863
Michio Honda9f7d6532011-04-26 19:32:51 +0900864/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
865int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
866{
867 struct sock *sk = sctp_opt2sk(sp);
868 union sctp_addr *addr;
869 struct sctp_af *af;
870
871 /* It is safe to write port space in caller. */
872 addr = &addrw->a;
873 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
874 af = sctp_get_af_specific(addr->sa.sa_family);
875 if (!af)
876 return -EINVAL;
877 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
878 return -EINVAL;
879
880 if (addrw->state == SCTP_ADDR_NEW)
881 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
882 else
883 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
887 *
888 * API 8.1
889 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
890 * int flags);
891 *
892 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
893 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
894 * or IPv6 addresses.
895 *
896 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
897 * Section 3.1.2 for this usage.
898 *
899 * addrs is a pointer to an array of one or more socket addresses. Each
900 * address is contained in its appropriate structure (i.e. struct
901 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700902 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 * representation is termed a "packed array" of addresses). The caller
904 * specifies the number of addresses in the array with addrcnt.
905 *
906 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
907 * -1, and sets errno to the appropriate error code.
908 *
909 * For SCTP, the port given in each socket address must be the same, or
910 * sctp_bindx() will fail, setting errno to EINVAL.
911 *
912 * The flags parameter is formed from the bitwise OR of zero or more of
913 * the following currently defined flags:
914 *
915 * SCTP_BINDX_ADD_ADDR
916 *
917 * SCTP_BINDX_REM_ADDR
918 *
919 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
920 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
921 * addresses from the association. The two flags are mutually exclusive;
922 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
923 * not remove all addresses from an association; sctp_bindx() will
924 * reject such an attempt with EINVAL.
925 *
926 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
927 * additional addresses with an endpoint after calling bind(). Or use
928 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
929 * socket is associated with so that no new association accepted will be
930 * associated with those addresses. If the endpoint supports dynamic
931 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
932 * endpoint to send the appropriate message to the peer to change the
933 * peers address lists.
934 *
935 * Adding and removing addresses from a connected association is
936 * optional functionality. Implementations that do not support this
937 * functionality should return EOPNOTSUPP.
938 *
939 * Basically do nothing but copying the addresses from user to kernel
940 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700941 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
942 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 *
944 * We don't use copy_from_user() for optimization: we first do the
945 * sanity checks (buffer size -fast- and access check-healthy
946 * pointer); if all of those succeed, then we can alloc the memory
947 * (expensive operation) needed to copy the data to kernel. Then we do
948 * the copying without checking the user space area
949 * (__copy_from_user()).
950 *
951 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
952 * it.
953 *
954 * sk The sk of the socket
955 * addrs The pointer to the addresses in user land
956 * addrssize Size of the addrs buffer
957 * op Operation to perform (add or remove, see the flags of
958 * sctp_bindx)
959 *
960 * Returns 0 if ok, <0 errno code on error.
961 */
962SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
963 struct sockaddr __user *addrs,
964 int addrs_size, int op)
965{
966 struct sockaddr *kaddrs;
967 int err;
968 int addrcnt = 0;
969 int walk_size = 0;
970 struct sockaddr *sa_addr;
971 void *addr_buf;
972 struct sctp_af *af;
973
974 SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
975 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
976
977 if (unlikely(addrs_size <= 0))
978 return -EINVAL;
979
980 /* Check the user passed a healthy pointer. */
981 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
982 return -EFAULT;
983
984 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800985 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (unlikely(!kaddrs))
987 return -ENOMEM;
988
989 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
990 kfree(kaddrs);
991 return -EFAULT;
992 }
993
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900994 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 addr_buf = kaddrs;
996 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +0000997 if (walk_size + sizeof(sa_family_t) > addrs_size) {
998 kfree(kaddrs);
999 return -EINVAL;
1000 }
1001
Joe Perchesea110732011-06-13 16:21:26 +00001002 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 af = sctp_get_af_specific(sa_addr->sa_family);
1004
1005 /* If the address family is not supported or if this address
1006 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1009 kfree(kaddrs);
1010 return -EINVAL;
1011 }
1012 addrcnt++;
1013 addr_buf += af->sockaddr_len;
1014 walk_size += af->sockaddr_len;
1015 }
1016
1017 /* Do the work. */
1018 switch (op) {
1019 case SCTP_BINDX_ADD_ADDR:
1020 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1021 if (err)
1022 goto out;
1023 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1024 break;
1025
1026 case SCTP_BINDX_REM_ADDR:
1027 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1028 if (err)
1029 goto out;
1030 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1031 break;
1032
1033 default:
1034 err = -EINVAL;
1035 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038out:
1039 kfree(kaddrs);
1040
1041 return err;
1042}
1043
Frank Filz3f7a87d2005-06-20 13:14:57 -07001044/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1045 *
1046 * Common routine for handling connect() and sctp_connectx().
1047 * Connect will come in with just a single address.
1048 */
1049static int __sctp_connect(struct sock* sk,
1050 struct sockaddr *kaddrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001051 int addrs_size,
1052 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001053{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001054 struct net *net = sock_net(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001055 struct sctp_sock *sp;
1056 struct sctp_endpoint *ep;
1057 struct sctp_association *asoc = NULL;
1058 struct sctp_association *asoc2;
1059 struct sctp_transport *transport;
1060 union sctp_addr to;
1061 struct sctp_af *af;
1062 sctp_scope_t scope;
1063 long timeo;
1064 int err = 0;
1065 int addrcnt = 0;
1066 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001067 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001068 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001069 unsigned short port;
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001070 unsigned int f_flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001071
1072 sp = sctp_sk(sk);
1073 ep = sp->ep;
1074
1075 /* connect() cannot be done on a socket that is already in ESTABLISHED
1076 * state - UDP-style peeled off socket or a TCP-style socket that
1077 * is already connected.
1078 * It cannot be done even on a TCP-style listening socket.
1079 */
1080 if (sctp_sstate(sk, ESTABLISHED) ||
1081 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1082 err = -EISCONN;
1083 goto out_free;
1084 }
1085
1086 /* Walk through the addrs buffer and count the number of addresses. */
1087 addr_buf = kaddrs;
1088 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001089 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1090 err = -EINVAL;
1091 goto out_free;
1092 }
1093
Joe Perchesea110732011-06-13 16:21:26 +00001094 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001095 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001096
1097 /* If the address family is not supported or if this address
1098 * causes the address buffer to overflow return EINVAL.
1099 */
1100 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1101 err = -EINVAL;
1102 goto out_free;
1103 }
1104
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001105 port = ntohs(sa_addr->v4.sin_port);
1106
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001107 /* Save current address so we can work with it */
1108 memcpy(&to, sa_addr, af->sockaddr_len);
1109
1110 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001111 if (err)
1112 goto out_free;
1113
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001114 /* Make sure the destination port is correctly set
1115 * in all addresses.
1116 */
1117 if (asoc && asoc->peer.port && asoc->peer.port != port)
1118 goto out_free;
1119
Frank Filz3f7a87d2005-06-20 13:14:57 -07001120
1121 /* Check if there already is a matching association on the
1122 * endpoint (other than the one created here).
1123 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001124 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001125 if (asoc2 && asoc2 != asoc) {
1126 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1127 err = -EISCONN;
1128 else
1129 err = -EALREADY;
1130 goto out_free;
1131 }
1132
1133 /* If we could not find a matching association on the endpoint,
1134 * make sure that there is no peeled-off association matching
1135 * the peer address even on another socket.
1136 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001137 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001138 err = -EADDRNOTAVAIL;
1139 goto out_free;
1140 }
1141
1142 if (!asoc) {
1143 /* If a bind() or sctp_bindx() is not called prior to
1144 * an sctp_connectx() call, the system picks an
1145 * ephemeral port and will choose an address set
1146 * equivalent to binding with a wildcard address.
1147 */
1148 if (!ep->base.bind_addr.port) {
1149 if (sctp_autobind(sk)) {
1150 err = -EAGAIN;
1151 goto out_free;
1152 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001153 } else {
1154 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001155 * If an unprivileged user inherits a 1-many
1156 * style socket with open associations on a
1157 * privileged port, it MAY be permitted to
1158 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001159 * be permitted to open new associations.
1160 */
1161 if (ep->base.bind_addr.port < PROT_SOCK &&
1162 !capable(CAP_NET_BIND_SERVICE)) {
1163 err = -EACCES;
1164 goto out_free;
1165 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001166 }
1167
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001168 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001169 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1170 if (!asoc) {
1171 err = -ENOMEM;
1172 goto out_free;
1173 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001174
1175 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1176 GFP_KERNEL);
1177 if (err < 0) {
1178 goto out_free;
1179 }
1180
Frank Filz3f7a87d2005-06-20 13:14:57 -07001181 }
1182
1183 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001184 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001185 SCTP_UNKNOWN);
1186 if (!transport) {
1187 err = -ENOMEM;
1188 goto out_free;
1189 }
1190
1191 addrcnt++;
1192 addr_buf += af->sockaddr_len;
1193 walk_size += af->sockaddr_len;
1194 }
1195
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001196 /* In case the user of sctp_connectx() wants an association
1197 * id back, assign one now.
1198 */
1199 if (assoc_id) {
1200 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1201 if (err < 0)
1202 goto out_free;
1203 }
1204
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001205 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001206 if (err < 0) {
1207 goto out_free;
1208 }
1209
1210 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001211 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001212 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1213 af->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001214 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001215
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001216 /* in-kernel sockets don't generally have a file allocated to them
1217 * if all they do is call sock_create_kern().
1218 */
1219 if (sk->sk_socket->file)
1220 f_flags = sk->sk_socket->file->f_flags;
1221
1222 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1223
Frank Filz3f7a87d2005-06-20 13:14:57 -07001224 err = sctp_wait_for_connect(asoc, &timeo);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001225 if ((err == 0 || err == -EINPROGRESS) && assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001226 *assoc_id = asoc->assoc_id;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001227
1228 /* Don't free association on exit. */
1229 asoc = NULL;
1230
1231out_free:
1232
1233 SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001234 " kaddrs: %p err: %d\n",
1235 asoc, kaddrs, err);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001236 if (asoc) {
1237 /* sctp_primitive_ASSOCIATE may have added this association
1238 * To the hash table, try to unhash it, just in case, its a noop
1239 * if it wasn't hashed so we're safe
1240 */
1241 sctp_unhash_established(asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001242 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001243 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001244 return err;
1245}
1246
1247/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1248 *
1249 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001250 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1251 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001252 *
1253 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1254 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1255 * or IPv6 addresses.
1256 *
1257 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1258 * Section 3.1.2 for this usage.
1259 *
1260 * addrs is a pointer to an array of one or more socket addresses. Each
1261 * address is contained in its appropriate structure (i.e. struct
1262 * sockaddr_in or struct sockaddr_in6) the family of the address type
1263 * must be used to distengish the address length (note that this
1264 * representation is termed a "packed array" of addresses). The caller
1265 * specifies the number of addresses in the array with addrcnt.
1266 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001267 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1268 * the association id of the new association. On failure, sctp_connectx()
1269 * returns -1, and sets errno to the appropriate error code. The assoc_id
1270 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001271 *
1272 * For SCTP, the port given in each socket address must be the same, or
1273 * sctp_connectx() will fail, setting errno to EINVAL.
1274 *
1275 * An application can use sctp_connectx to initiate an association with
1276 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1277 * allows a caller to specify multiple addresses at which a peer can be
1278 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001279 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001280 * specifies that the stack will try to make use of all the addresses in
1281 * the list when needed.
1282 *
1283 * Note that the list of addresses passed in is only used for setting up
1284 * the association. It does not necessarily equal the set of addresses
1285 * the peer uses for the resulting association. If the caller wants to
1286 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1287 * retrieve them after the association has been set up.
1288 *
1289 * Basically do nothing but copying the addresses from user to kernel
1290 * land and invoking either sctp_connectx(). This is used for tunneling
1291 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1292 *
1293 * We don't use copy_from_user() for optimization: we first do the
1294 * sanity checks (buffer size -fast- and access check-healthy
1295 * pointer); if all of those succeed, then we can alloc the memory
1296 * (expensive operation) needed to copy the data to kernel. Then we do
1297 * the copying without checking the user space area
1298 * (__copy_from_user()).
1299 *
1300 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1301 * it.
1302 *
1303 * sk The sk of the socket
1304 * addrs The pointer to the addresses in user land
1305 * addrssize Size of the addrs buffer
1306 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001307 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001308 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001309SCTP_STATIC int __sctp_setsockopt_connectx(struct sock* sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001310 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001311 int addrs_size,
1312 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001313{
1314 int err = 0;
1315 struct sockaddr *kaddrs;
1316
1317 SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001318 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001319
1320 if (unlikely(addrs_size <= 0))
1321 return -EINVAL;
1322
1323 /* Check the user passed a healthy pointer. */
1324 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1325 return -EFAULT;
1326
1327 /* Alloc space for the address array in kernel memory. */
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001328 kaddrs = kmalloc(addrs_size, GFP_KERNEL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001329 if (unlikely(!kaddrs))
1330 return -ENOMEM;
1331
1332 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1333 err = -EFAULT;
1334 } else {
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001335 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001336 }
1337
1338 kfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001339
Frank Filz3f7a87d2005-06-20 13:14:57 -07001340 return err;
1341}
1342
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001343/*
1344 * This is an older interface. It's kept for backward compatibility
1345 * to the option that doesn't provide association id.
1346 */
1347SCTP_STATIC int sctp_setsockopt_connectx_old(struct sock* sk,
1348 struct sockaddr __user *addrs,
1349 int addrs_size)
1350{
1351 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1352}
1353
1354/*
1355 * New interface for the API. The since the API is done with a socket
1356 * option, to make it simple we feed back the association id is as a return
1357 * indication to the call. Error is always negative and association id is
1358 * always positive.
1359 */
1360SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
1361 struct sockaddr __user *addrs,
1362 int addrs_size)
1363{
1364 sctp_assoc_t assoc_id = 0;
1365 int err = 0;
1366
1367 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1368
1369 if (err)
1370 return err;
1371 else
1372 return assoc_id;
1373}
1374
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001375/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001376 * New (hopefully final) interface for the API.
1377 * We use the sctp_getaddrs_old structure so that use-space library
1378 * can avoid any unnecessary allocations. The only defferent part
1379 * is that we store the actual length of the address buffer into the
1380 * addrs_num structure member. That way we can re-use the existing
1381 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001382 */
1383SCTP_STATIC int sctp_getsockopt_connectx3(struct sock* sk, int len,
1384 char __user *optval,
1385 int __user *optlen)
1386{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001387 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001388 sctp_assoc_t assoc_id = 0;
1389 int err = 0;
1390
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001391 if (len < sizeof(param))
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001392 return -EINVAL;
1393
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001394 if (copy_from_user(&param, optval, sizeof(param)))
1395 return -EFAULT;
1396
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001397 err = __sctp_setsockopt_connectx(sk,
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001398 (struct sockaddr __user *)param.addrs,
1399 param.addr_num, &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001400
1401 if (err == 0 || err == -EINPROGRESS) {
1402 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1403 return -EFAULT;
1404 if (put_user(sizeof(assoc_id), optlen))
1405 return -EFAULT;
1406 }
1407
1408 return err;
1409}
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411/* API 3.1.4 close() - UDP Style Syntax
1412 * Applications use close() to perform graceful shutdown (as described in
1413 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1414 * by a UDP-style socket.
1415 *
1416 * The syntax is
1417 *
1418 * ret = close(int sd);
1419 *
1420 * sd - the socket descriptor of the associations to be closed.
1421 *
1422 * To gracefully shutdown a specific association represented by the
1423 * UDP-style socket, an application should use the sendmsg() call,
1424 * passing no user data, but including the appropriate flag in the
1425 * ancillary data (see Section xxxx).
1426 *
1427 * If sd in the close() call is a branched-off socket representing only
1428 * one association, the shutdown is performed on that association only.
1429 *
1430 * 4.1.6 close() - TCP Style Syntax
1431 *
1432 * Applications use close() to gracefully close down an association.
1433 *
1434 * The syntax is:
1435 *
1436 * int close(int sd);
1437 *
1438 * sd - the socket descriptor of the association to be closed.
1439 *
1440 * After an application calls close() on a socket descriptor, no further
1441 * socket operations will succeed on that descriptor.
1442 *
1443 * API 7.1.4 SO_LINGER
1444 *
1445 * An application using the TCP-style socket can use this option to
1446 * perform the SCTP ABORT primitive. The linger option structure is:
1447 *
1448 * struct linger {
1449 * int l_onoff; // option on/off
1450 * int l_linger; // linger time
1451 * };
1452 *
1453 * To enable the option, set l_onoff to 1. If the l_linger value is set
1454 * to 0, calling close() is the same as the ABORT primitive. If the
1455 * value is set to a negative value, the setsockopt() call will return
1456 * an error. If the value is set to a positive value linger_time, the
1457 * close() can be blocked for at most linger_time ms. If the graceful
1458 * shutdown phase does not finish during this period, close() will
1459 * return but the graceful shutdown phase continues in the system.
1460 */
1461SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
1462{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001463 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 struct sctp_endpoint *ep;
1465 struct sctp_association *asoc;
1466 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001467 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
1470
1471 sctp_lock_sock(sk);
1472 sk->sk_shutdown = SHUTDOWN_MASK;
Vlad Yasevichbec96402009-07-30 18:08:28 -04001473 sk->sk_state = SCTP_SS_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 ep = sctp_sk(sk)->ep;
1476
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001477 /* Clean up any skbs sitting on the receive queue. */
1478 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1479 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1480
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001481 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 list_for_each_safe(pos, temp, &ep->asocs) {
1483 asoc = list_entry(pos, struct sctp_association, asocs);
1484
1485 if (sctp_style(sk, TCP)) {
1486 /* A closed association can still be in the list if
1487 * it belongs to a TCP-style listening socket that is
1488 * not yet accepted. If so, free it. If not, send an
1489 * ABORT or SHUTDOWN based on the linger options.
1490 */
1491 if (sctp_state(asoc, CLOSED)) {
1492 sctp_unhash_established(asoc);
1493 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001494 continue;
1495 }
1496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001498 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1499 !skb_queue_empty(&asoc->ulpq.reasm) ||
1500 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001501 struct sctp_chunk *chunk;
1502
1503 chunk = sctp_make_abort_user(asoc, NULL, 0);
1504 if (chunk)
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001505 sctp_primitive_ABORT(net, asoc, chunk);
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001506 } else
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001507 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /* On a TCP-style socket, block for at most linger_time if set. */
1511 if (sctp_style(sk, TCP) && timeout)
1512 sctp_wait_for_close(sk, timeout);
1513
1514 /* This will run the backlog queue. */
1515 sctp_release_sock(sk);
1516
1517 /* Supposedly, no process has access to the socket, but
1518 * the net layers still may.
1519 */
1520 sctp_local_bh_disable();
1521 sctp_bh_lock_sock(sk);
1522
1523 /* Hold the sock, since sk_common_release() will put sock_put()
1524 * and we have just a little more cleanup.
1525 */
1526 sock_hold(sk);
1527 sk_common_release(sk);
1528
1529 sctp_bh_unlock_sock(sk);
1530 sctp_local_bh_enable();
1531
1532 sock_put(sk);
1533
1534 SCTP_DBG_OBJCNT_DEC(sock);
1535}
1536
1537/* Handle EPIPE error. */
1538static int sctp_error(struct sock *sk, int flags, int err)
1539{
1540 if (err == -EPIPE)
1541 err = sock_error(sk) ? : -EPIPE;
1542 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1543 send_sig(SIGPIPE, current, 0);
1544 return err;
1545}
1546
1547/* API 3.1.3 sendmsg() - UDP Style Syntax
1548 *
1549 * An application uses sendmsg() and recvmsg() calls to transmit data to
1550 * and receive data from its peer.
1551 *
1552 * ssize_t sendmsg(int socket, const struct msghdr *message,
1553 * int flags);
1554 *
1555 * socket - the socket descriptor of the endpoint.
1556 * message - pointer to the msghdr structure which contains a single
1557 * user message and possibly some ancillary data.
1558 *
1559 * See Section 5 for complete description of the data
1560 * structures.
1561 *
1562 * flags - flags sent or received with the user message, see Section
1563 * 5 for complete description of the flags.
1564 *
1565 * Note: This function could use a rewrite especially when explicit
1566 * connect support comes in.
1567 */
1568/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1569
1570SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1571
1572SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
1573 struct msghdr *msg, size_t msg_len)
1574{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001575 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 struct sctp_sock *sp;
1577 struct sctp_endpoint *ep;
1578 struct sctp_association *new_asoc=NULL, *asoc=NULL;
1579 struct sctp_transport *transport, *chunk_tp;
1580 struct sctp_chunk *chunk;
Al Virodce116a2006-11-20 17:25:15 -08001581 union sctp_addr to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 struct sockaddr *msg_name = NULL;
Joe Perches517aa0b2011-05-12 11:27:20 +00001583 struct sctp_sndrcvinfo default_sinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 struct sctp_sndrcvinfo *sinfo;
1585 struct sctp_initmsg *sinit;
1586 sctp_assoc_t associd = 0;
1587 sctp_cmsgs_t cmsgs = { NULL };
1588 int err;
1589 sctp_scope_t scope;
1590 long timeo;
1591 __u16 sinfo_flags = 0;
1592 struct sctp_datamsg *datamsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 int msg_flags = msg->msg_flags;
1594
1595 SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
1596 sk, msg, msg_len);
1597
1598 err = 0;
1599 sp = sctp_sk(sk);
1600 ep = sp->ep;
1601
Frank Filz3f7a87d2005-06-20 13:14:57 -07001602 SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 /* We cannot send a message over a TCP-style listening socket. */
1605 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1606 err = -EPIPE;
1607 goto out_nounlock;
1608 }
1609
1610 /* Parse out the SCTP CMSGs. */
1611 err = sctp_msghdr_parse(msg, &cmsgs);
1612
1613 if (err) {
1614 SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
1615 goto out_nounlock;
1616 }
1617
1618 /* Fetch the destination address for this packet. This
1619 * address only selects the association--it is not necessarily
1620 * the address we will send to.
1621 * For a peeled-off socket, msg_name is ignored.
1622 */
1623 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1624 int msg_namelen = msg->msg_namelen;
1625
1626 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1627 msg_namelen);
1628 if (err)
1629 return err;
1630
1631 if (msg_namelen > sizeof(to))
1632 msg_namelen = sizeof(to);
1633 memcpy(&to, msg->msg_name, msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 msg_name = msg->msg_name;
1635 }
1636
1637 sinfo = cmsgs.info;
1638 sinit = cmsgs.init;
1639
1640 /* Did the user specify SNDRCVINFO? */
1641 if (sinfo) {
1642 sinfo_flags = sinfo->sinfo_flags;
1643 associd = sinfo->sinfo_assoc_id;
1644 }
1645
1646 SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
1647 msg_len, sinfo_flags);
1648
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001649 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1650 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 err = -EINVAL;
1652 goto out_nounlock;
1653 }
1654
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001655 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1656 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1657 * If SCTP_ABORT is set, the message length could be non zero with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 * the msg_iov set to the user abort reason.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001659 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001660 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1661 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 err = -EINVAL;
1663 goto out_nounlock;
1664 }
1665
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001666 /* If SCTP_ADDR_OVER is set, there must be an address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 * specified in msg_name.
1668 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001669 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 err = -EINVAL;
1671 goto out_nounlock;
1672 }
1673
1674 transport = NULL;
1675
1676 SCTP_DEBUG_PRINTK("About to look up association.\n");
1677
1678 sctp_lock_sock(sk);
1679
1680 /* If a msg_name has been specified, assume this is to be used. */
1681 if (msg_name) {
1682 /* Look for a matching association on the endpoint. */
Al Virodce116a2006-11-20 17:25:15 -08001683 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (!asoc) {
1685 /* If we could not find a matching association on the
1686 * endpoint, make sure that it is not a TCP-style
1687 * socket that already has an association or there is
1688 * no peeled-off association on another socket.
1689 */
1690 if ((sctp_style(sk, TCP) &&
1691 sctp_sstate(sk, ESTABLISHED)) ||
Al Virodce116a2006-11-20 17:25:15 -08001692 sctp_endpoint_is_peeled_off(ep, &to)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 err = -EADDRNOTAVAIL;
1694 goto out_unlock;
1695 }
1696 }
1697 } else {
1698 asoc = sctp_id2assoc(sk, associd);
1699 if (!asoc) {
1700 err = -EPIPE;
1701 goto out_unlock;
1702 }
1703 }
1704
1705 if (asoc) {
1706 SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
1707
1708 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1709 * socket that has an association in CLOSED state. This can
1710 * happen when an accepted socket has an association that is
1711 * already CLOSED.
1712 */
1713 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1714 err = -EPIPE;
1715 goto out_unlock;
1716 }
1717
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001718 if (sinfo_flags & SCTP_EOF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
1720 asoc);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001721 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 err = 0;
1723 goto out_unlock;
1724 }
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001725 if (sinfo_flags & SCTP_ABORT) {
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001726
1727 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1728 if (!chunk) {
1729 err = -ENOMEM;
1730 goto out_unlock;
1731 }
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001734 sctp_primitive_ABORT(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 err = 0;
1736 goto out_unlock;
1737 }
1738 }
1739
1740 /* Do we need to create the association? */
1741 if (!asoc) {
1742 SCTP_DEBUG_PRINTK("There is no association yet.\n");
1743
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001744 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 err = -EINVAL;
1746 goto out_unlock;
1747 }
1748
1749 /* Check for invalid stream against the stream counts,
1750 * either the default or the user specified stream counts.
1751 */
1752 if (sinfo) {
1753 if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
1754 /* Check against the defaults. */
1755 if (sinfo->sinfo_stream >=
1756 sp->initmsg.sinit_num_ostreams) {
1757 err = -EINVAL;
1758 goto out_unlock;
1759 }
1760 } else {
1761 /* Check against the requested. */
1762 if (sinfo->sinfo_stream >=
1763 sinit->sinit_num_ostreams) {
1764 err = -EINVAL;
1765 goto out_unlock;
1766 }
1767 }
1768 }
1769
1770 /*
1771 * API 3.1.2 bind() - UDP Style Syntax
1772 * If a bind() or sctp_bindx() is not called prior to a
1773 * sendmsg() call that initiates a new association, the
1774 * system picks an ephemeral port and will choose an address
1775 * set equivalent to binding with a wildcard address.
1776 */
1777 if (!ep->base.bind_addr.port) {
1778 if (sctp_autobind(sk)) {
1779 err = -EAGAIN;
1780 goto out_unlock;
1781 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001782 } else {
1783 /*
1784 * If an unprivileged user inherits a one-to-many
1785 * style socket with open associations on a privileged
1786 * port, it MAY be permitted to accept new associations,
1787 * but it SHOULD NOT be permitted to open new
1788 * associations.
1789 */
1790 if (ep->base.bind_addr.port < PROT_SOCK &&
1791 !capable(CAP_NET_BIND_SERVICE)) {
1792 err = -EACCES;
1793 goto out_unlock;
1794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796
1797 scope = sctp_scope(&to);
1798 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1799 if (!new_asoc) {
1800 err = -ENOMEM;
1801 goto out_unlock;
1802 }
1803 asoc = new_asoc;
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001804 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1805 if (err < 0) {
1806 err = -ENOMEM;
1807 goto out_free;
1808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 /* If the SCTP_INIT ancillary data is specified, set all
1811 * the association init values accordingly.
1812 */
1813 if (sinit) {
1814 if (sinit->sinit_num_ostreams) {
1815 asoc->c.sinit_num_ostreams =
1816 sinit->sinit_num_ostreams;
1817 }
1818 if (sinit->sinit_max_instreams) {
1819 asoc->c.sinit_max_instreams =
1820 sinit->sinit_max_instreams;
1821 }
1822 if (sinit->sinit_max_attempts) {
1823 asoc->max_init_attempts
1824 = sinit->sinit_max_attempts;
1825 }
1826 if (sinit->sinit_max_init_timeo) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001827 asoc->max_init_timeo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1829 }
1830 }
1831
1832 /* Prime the peer's transport structures. */
Al Virodce116a2006-11-20 17:25:15 -08001833 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (!transport) {
1835 err = -ENOMEM;
1836 goto out_free;
1837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839
1840 /* ASSERT: we have a valid association at this point. */
1841 SCTP_DEBUG_PRINTK("We have a valid association.\n");
1842
1843 if (!sinfo) {
1844 /* If the user didn't specify SNDRCVINFO, make up one with
1845 * some defaults.
1846 */
Joe Perches517aa0b2011-05-12 11:27:20 +00001847 memset(&default_sinfo, 0, sizeof(default_sinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 default_sinfo.sinfo_stream = asoc->default_stream;
1849 default_sinfo.sinfo_flags = asoc->default_flags;
1850 default_sinfo.sinfo_ppid = asoc->default_ppid;
1851 default_sinfo.sinfo_context = asoc->default_context;
1852 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1853 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
1854 sinfo = &default_sinfo;
1855 }
1856
1857 /* API 7.1.7, the sndbuf size per association bounds the
1858 * maximum size of data that can be sent in a single send call.
1859 */
1860 if (msg_len > sk->sk_sndbuf) {
1861 err = -EMSGSIZE;
1862 goto out_free;
1863 }
1864
Vlad Yasevich8a479492007-06-07 14:21:05 -04001865 if (asoc->pmtu_pending)
David S. Miller02f3d4c2012-07-16 03:57:14 -07001866 sctp_assoc_pending_pmtu(sk, asoc);
Vlad Yasevich8a479492007-06-07 14:21:05 -04001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /* If fragmentation is disabled and the message length exceeds the
1869 * association fragmentation point, return EMSGSIZE. The I-D
1870 * does not specify what this error is, but this looks like
1871 * a great fit.
1872 */
1873 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1874 err = -EMSGSIZE;
1875 goto out_free;
1876 }
1877
Joe Perchesafd76142011-05-12 09:19:10 +00001878 /* Check for invalid stream. */
1879 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1880 err = -EINVAL;
1881 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883
1884 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1885 if (!sctp_wspace(asoc)) {
1886 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1887 if (err)
1888 goto out_free;
1889 }
1890
1891 /* If an address is passed with the sendto/sendmsg call, it is used
1892 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001893 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 */
1895 if ((sctp_style(sk, TCP) && msg_name) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001896 (sinfo_flags & SCTP_ADDR_OVER)) {
Al Virodce116a2006-11-20 17:25:15 -08001897 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (!chunk_tp) {
1899 err = -EINVAL;
1900 goto out_free;
1901 }
1902 } else
1903 chunk_tp = NULL;
1904
1905 /* Auto-connect, if we aren't connected already. */
1906 if (sctp_state(asoc, CLOSED)) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001907 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (err < 0)
1909 goto out_free;
1910 SCTP_DEBUG_PRINTK("We associated primitively.\n");
1911 }
1912
1913 /* Break the message into multiple chunks of maximum size. */
1914 datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
1915 if (!datamsg) {
1916 err = -ENOMEM;
1917 goto out_free;
1918 }
1919
1920 /* Now send the (possibly) fragmented message. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001921 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
Florian Westphal80445cf2008-03-23 22:47:08 -07001922 sctp_chunk_hold(chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 /* Do accounting for the write space. */
1925 sctp_set_owner_w(chunk);
1926
1927 chunk->transport = chunk_tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
1929
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04001930 /* Send it to the lower layers. Note: all chunks
1931 * must either fail or succeed. The lower layer
1932 * works that way today. Keep it that way or this
1933 * breaks.
1934 */
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001935 err = sctp_primitive_SEND(net, asoc, datamsg);
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04001936 /* Did the lower layer accept the chunk? */
1937 if (err)
1938 sctp_datamsg_free(datamsg);
1939 else
1940 sctp_datamsg_put(datamsg);
1941
1942 SCTP_DEBUG_PRINTK("We sent primitively.\n");
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (err)
1945 goto out_free;
1946 else
1947 err = msg_len;
1948
1949 /* If we are already past ASSOCIATE, the lower
1950 * layers are responsible for association cleanup.
1951 */
1952 goto out_unlock;
1953
1954out_free:
Neil Horman2eebc1e2012-07-16 09:13:51 +00001955 if (new_asoc) {
1956 sctp_unhash_established(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959out_unlock:
1960 sctp_release_sock(sk);
1961
1962out_nounlock:
1963 return sctp_error(sk, msg_flags, err);
1964
1965#if 0
1966do_sock_err:
1967 if (msg_len)
1968 err = msg_len;
1969 else
1970 err = sock_error(sk);
1971 goto out;
1972
1973do_interrupted:
1974 if (msg_len)
1975 err = msg_len;
1976 goto out;
1977#endif /* 0 */
1978}
1979
1980/* This is an extended version of skb_pull() that removes the data from the
1981 * start of a skb even when data is spread across the list of skb's in the
1982 * frag_list. len specifies the total amount of data that needs to be removed.
1983 * when 'len' bytes could be removed from the skb, it returns 0.
1984 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1985 * could not be removed.
1986 */
1987static int sctp_skb_pull(struct sk_buff *skb, int len)
1988{
1989 struct sk_buff *list;
1990 int skb_len = skb_headlen(skb);
1991 int rlen;
1992
1993 if (len <= skb_len) {
1994 __skb_pull(skb, len);
1995 return 0;
1996 }
1997 len -= skb_len;
1998 __skb_pull(skb, skb_len);
1999
David S. Miller1b003be2009-06-09 00:22:35 -07002000 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 rlen = sctp_skb_pull(list, len);
2002 skb->len -= (len-rlen);
2003 skb->data_len -= (len-rlen);
2004
2005 if (!rlen)
2006 return 0;
2007
2008 len = rlen;
2009 }
2010
2011 return len;
2012}
2013
2014/* API 3.1.3 recvmsg() - UDP Style Syntax
2015 *
2016 * ssize_t recvmsg(int socket, struct msghdr *message,
2017 * int flags);
2018 *
2019 * socket - the socket descriptor of the endpoint.
2020 * message - pointer to the msghdr structure which contains a single
2021 * user message and possibly some ancillary data.
2022 *
2023 * See Section 5 for complete description of the data
2024 * structures.
2025 *
2026 * flags - flags sent or received with the user message, see Section
2027 * 5 for complete description of the flags.
2028 */
2029static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
2030
2031SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
2032 struct msghdr *msg, size_t len, int noblock,
2033 int flags, int *addr_len)
2034{
2035 struct sctp_ulpevent *event = NULL;
2036 struct sctp_sock *sp = sctp_sk(sk);
2037 struct sk_buff *skb;
2038 int copied;
2039 int err = 0;
2040 int skb_len;
2041
2042 SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
2043 "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
2044 "len", len, "knoblauch", noblock,
2045 "flags", flags, "addr_len", addr_len);
2046
2047 sctp_lock_sock(sk);
2048
2049 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
2050 err = -ENOTCONN;
2051 goto out;
2052 }
2053
2054 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2055 if (!skb)
2056 goto out;
2057
2058 /* Get the total length of the skb including any skb's in the
2059 * frag_list.
2060 */
2061 skb_len = skb->len;
2062
2063 copied = skb_len;
2064 if (copied > len)
2065 copied = len;
2066
2067 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2068
2069 event = sctp_skb2event(skb);
2070
2071 if (err)
2072 goto out_free;
2073
Neil Horman3b885782009-10-12 13:26:31 -07002074 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (sctp_ulpevent_is_notification(event)) {
2076 msg->msg_flags |= MSG_NOTIFICATION;
2077 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2078 } else {
2079 sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
2080 }
2081
2082 /* Check if we allow SCTP_SNDRCVINFO. */
2083 if (sp->subscribe.sctp_data_io_event)
2084 sctp_ulpevent_read_sndrcvinfo(event, msg);
2085#if 0
2086 /* FIXME: we should be calling IP/IPv6 layers. */
2087 if (sk->sk_protinfo.af_inet.cmsg_flags)
2088 ip_cmsg_recv(msg, skb);
2089#endif
2090
2091 err = copied;
2092
2093 /* If skb's length exceeds the user's buffer, update the skb and
2094 * push it back to the receive_queue so that the next call to
2095 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2096 */
2097 if (skb_len > copied) {
2098 msg->msg_flags &= ~MSG_EOR;
2099 if (flags & MSG_PEEK)
2100 goto out_free;
2101 sctp_skb_pull(skb, copied);
2102 skb_queue_head(&sk->sk_receive_queue, skb);
2103
2104 /* When only partial message is copied to the user, increase
2105 * rwnd by that amount. If all the data in the skb is read,
2106 * rwnd is updated when the event is freed.
2107 */
Vlad Yasevich0eca8fe2008-01-11 10:12:56 -05002108 if (!sctp_ulpevent_is_notification(event))
2109 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 goto out;
2111 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2112 (event->msg_flags & MSG_EOR))
2113 msg->msg_flags |= MSG_EOR;
2114 else
2115 msg->msg_flags &= ~MSG_EOR;
2116
2117out_free:
2118 if (flags & MSG_PEEK) {
2119 /* Release the skb reference acquired after peeking the skb in
2120 * sctp_skb_recv_datagram().
2121 */
2122 kfree_skb(skb);
2123 } else {
2124 /* Free the event which includes releasing the reference to
2125 * the owner of the skb, freeing the skb and updating the
2126 * rwnd.
2127 */
2128 sctp_ulpevent_free(event);
2129 }
2130out:
2131 sctp_release_sock(sk);
2132 return err;
2133}
2134
2135/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2136 *
2137 * This option is a on/off flag. If enabled no SCTP message
2138 * fragmentation will be performed. Instead if a message being sent
2139 * exceeds the current PMTU size, the message will NOT be sent and
2140 * instead a error will be indicated to the user.
2141 */
2142static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002143 char __user *optval,
2144 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
2146 int val;
2147
2148 if (optlen < sizeof(int))
2149 return -EINVAL;
2150
2151 if (get_user(val, (int __user *)optval))
2152 return -EFAULT;
2153
2154 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2155
2156 return 0;
2157}
2158
2159static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002160 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
Wei Yongjun94912302011-07-02 09:28:04 +00002162 struct sctp_association *asoc;
2163 struct sctp_ulpevent *event;
2164
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002165 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return -EINVAL;
2167 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2168 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002169
2170 /*
2171 * At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2172 * if there is no data to be sent or retransmit, the stack will
2173 * immediately send up this notification.
2174 */
2175 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2176 &sctp_sk(sk)->subscribe)) {
2177 asoc = sctp_id2assoc(sk, 0);
2178
2179 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2180 event = sctp_ulpevent_make_sender_dry_event(asoc,
2181 GFP_ATOMIC);
2182 if (!event)
2183 return -ENOMEM;
2184
2185 sctp_ulpq_tail_event(&asoc->ulpq, event);
2186 }
2187 }
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return 0;
2190}
2191
2192/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2193 *
2194 * This socket option is applicable to the UDP-style socket only. When
2195 * set it will cause associations that are idle for more than the
2196 * specified number of seconds to automatically close. An association
2197 * being idle is defined an association that has NOT sent or received
2198 * user data. The special value of '0' indicates that no automatic
2199 * close of any associations should be performed. The option expects an
2200 * integer defining the number of seconds of idle time before an
2201 * association is closed.
2202 */
2203static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002204 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
2206 struct sctp_sock *sp = sctp_sk(sk);
2207
2208 /* Applicable to UDP-style socket only */
2209 if (sctp_style(sk, TCP))
2210 return -EOPNOTSUPP;
2211 if (optlen != sizeof(int))
2212 return -EINVAL;
2213 if (copy_from_user(&sp->autoclose, optval, optlen))
2214 return -EFAULT;
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 return 0;
2217}
2218
2219/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2220 *
2221 * Applications can enable or disable heartbeats for any peer address of
2222 * an association, modify an address's heartbeat interval, force a
2223 * heartbeat to be sent immediately, and adjust the address's maximum
2224 * number of retransmissions sent before an address is considered
2225 * unreachable. The following structure is used to access and modify an
2226 * address's parameters:
2227 *
2228 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002229 * sctp_assoc_t spp_assoc_id;
2230 * struct sockaddr_storage spp_address;
2231 * uint32_t spp_hbinterval;
2232 * uint16_t spp_pathmaxrxt;
2233 * uint32_t spp_pathmtu;
2234 * uint32_t spp_sackdelay;
2235 * uint32_t spp_flags;
2236 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002238 * spp_assoc_id - (one-to-many style socket) This is filled in the
2239 * application, and identifies the association for
2240 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 * spp_address - This specifies which address is of interest.
2242 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002243 * in milliseconds. If a value of zero
2244 * is present in this field then no changes are to
2245 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 * spp_pathmaxrxt - This contains the maximum number of
2247 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002248 * considered unreachable. If a value of zero
2249 * is present in this field then no changes are to
2250 * be made to this parameter.
2251 * spp_pathmtu - When Path MTU discovery is disabled the value
2252 * specified here will be the "fixed" path mtu.
2253 * Note that if the spp_address field is empty
2254 * then all associations on this address will
2255 * have this fixed path mtu set upon them.
2256 *
2257 * spp_sackdelay - When delayed sack is enabled, this value specifies
2258 * the number of milliseconds that sacks will be delayed
2259 * for. This value will apply to all addresses of an
2260 * association if the spp_address field is empty. Note
2261 * also, that if delayed sack is enabled and this
2262 * value is set to 0, no change is made to the last
2263 * recorded delayed sack timer value.
2264 *
2265 * spp_flags - These flags are used to control various features
2266 * on an association. The flag field may contain
2267 * zero or more of the following options.
2268 *
2269 * SPP_HB_ENABLE - Enable heartbeats on the
2270 * specified address. Note that if the address
2271 * field is empty all addresses for the association
2272 * have heartbeats enabled upon them.
2273 *
2274 * SPP_HB_DISABLE - Disable heartbeats on the
2275 * speicifed address. Note that if the address
2276 * field is empty all addresses for the association
2277 * will have their heartbeats disabled. Note also
2278 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2279 * mutually exclusive, only one of these two should
2280 * be specified. Enabling both fields will have
2281 * undetermined results.
2282 *
2283 * SPP_HB_DEMAND - Request a user initiated heartbeat
2284 * to be made immediately.
2285 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002286 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2287 * heartbeat delayis to be set to the value of 0
2288 * milliseconds.
2289 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002290 * SPP_PMTUD_ENABLE - This field will enable PMTU
2291 * discovery upon the specified address. Note that
2292 * if the address feild is empty then all addresses
2293 * on the association are effected.
2294 *
2295 * SPP_PMTUD_DISABLE - This field will disable PMTU
2296 * discovery upon the specified address. Note that
2297 * if the address feild is empty then all addresses
2298 * on the association are effected. Not also that
2299 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2300 * exclusive. Enabling both will have undetermined
2301 * results.
2302 *
2303 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2304 * on delayed sack. The time specified in spp_sackdelay
2305 * is used to specify the sack delay for this address. Note
2306 * that if spp_address is empty then all addresses will
2307 * enable delayed sack and take on the sack delay
2308 * value specified in spp_sackdelay.
2309 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2310 * off delayed sack. If the spp_address field is blank then
2311 * delayed sack is disabled for the entire association. Note
2312 * also that this field is mutually exclusive to
2313 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2314 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 */
Adrian Bunk16164362006-09-18 00:40:38 -07002316static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2317 struct sctp_transport *trans,
2318 struct sctp_association *asoc,
2319 struct sctp_sock *sp,
2320 int hb_change,
2321 int pmtud_change,
2322 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 int error;
2325
Frank Filz52ccb8e2005-12-22 11:36:46 -08002326 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002327 struct net *net = sock_net(trans->asoc->base.sk);
2328
2329 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (error)
2331 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 }
2333
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002334 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2335 * this field is ignored. Note also that a value of zero indicates
2336 * the current setting should be left unchanged.
2337 */
2338 if (params->spp_flags & SPP_HB_ENABLE) {
2339
2340 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2341 * set. This lets us use 0 value when this flag
2342 * is set.
2343 */
2344 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2345 params->spp_hbinterval = 0;
2346
2347 if (params->spp_hbinterval ||
2348 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2349 if (trans) {
2350 trans->hbinterval =
2351 msecs_to_jiffies(params->spp_hbinterval);
2352 } else if (asoc) {
2353 asoc->hbinterval =
2354 msecs_to_jiffies(params->spp_hbinterval);
2355 } else {
2356 sp->hbinterval = params->spp_hbinterval;
2357 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002358 }
2359 }
2360
2361 if (hb_change) {
2362 if (trans) {
2363 trans->param_flags =
2364 (trans->param_flags & ~SPP_HB) | hb_change;
2365 } else if (asoc) {
2366 asoc->param_flags =
2367 (asoc->param_flags & ~SPP_HB) | hb_change;
2368 } else {
2369 sp->param_flags =
2370 (sp->param_flags & ~SPP_HB) | hb_change;
2371 }
2372 }
2373
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002374 /* When Path MTU discovery is disabled the value specified here will
2375 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2376 * include the flag SPP_PMTUD_DISABLE for this field to have any
2377 * effect).
2378 */
2379 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002380 if (trans) {
2381 trans->pathmtu = params->spp_pathmtu;
David S. Miller02f3d4c2012-07-16 03:57:14 -07002382 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002383 } else if (asoc) {
2384 asoc->pathmtu = params->spp_pathmtu;
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04002385 sctp_frag_point(asoc, params->spp_pathmtu);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002386 } else {
2387 sp->pathmtu = params->spp_pathmtu;
2388 }
2389 }
2390
2391 if (pmtud_change) {
2392 if (trans) {
2393 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2394 (params->spp_flags & SPP_PMTUD_ENABLE);
2395 trans->param_flags =
2396 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2397 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002398 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
David S. Miller02f3d4c2012-07-16 03:57:14 -07002399 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002400 }
2401 } else if (asoc) {
2402 asoc->param_flags =
2403 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2404 } else {
2405 sp->param_flags =
2406 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2407 }
2408 }
2409
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002410 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2411 * value of this field is ignored. Note also that a value of zero
2412 * indicates the current setting should be left unchanged.
2413 */
2414 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002415 if (trans) {
2416 trans->sackdelay =
2417 msecs_to_jiffies(params->spp_sackdelay);
2418 } else if (asoc) {
2419 asoc->sackdelay =
2420 msecs_to_jiffies(params->spp_sackdelay);
2421 } else {
2422 sp->sackdelay = params->spp_sackdelay;
2423 }
2424 }
2425
2426 if (sackdelay_change) {
2427 if (trans) {
2428 trans->param_flags =
2429 (trans->param_flags & ~SPP_SACKDELAY) |
2430 sackdelay_change;
2431 } else if (asoc) {
2432 asoc->param_flags =
2433 (asoc->param_flags & ~SPP_SACKDELAY) |
2434 sackdelay_change;
2435 } else {
2436 sp->param_flags =
2437 (sp->param_flags & ~SPP_SACKDELAY) |
2438 sackdelay_change;
2439 }
2440 }
2441
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002442 /* Note that a value of zero indicates the current setting should be
2443 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002444 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002445 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002446 if (trans) {
2447 trans->pathmaxrxt = params->spp_pathmaxrxt;
2448 } else if (asoc) {
2449 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2450 } else {
2451 sp->pathmaxrxt = params->spp_pathmaxrxt;
2452 }
2453 }
2454
2455 return 0;
2456}
2457
2458static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002459 char __user *optval,
2460 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002461{
2462 struct sctp_paddrparams params;
2463 struct sctp_transport *trans = NULL;
2464 struct sctp_association *asoc = NULL;
2465 struct sctp_sock *sp = sctp_sk(sk);
2466 int error;
2467 int hb_change, pmtud_change, sackdelay_change;
2468
2469 if (optlen != sizeof(struct sctp_paddrparams))
2470 return - EINVAL;
2471
2472 if (copy_from_user(&params, optval, optlen))
2473 return -EFAULT;
2474
2475 /* Validate flags and value parameters. */
2476 hb_change = params.spp_flags & SPP_HB;
2477 pmtud_change = params.spp_flags & SPP_PMTUD;
2478 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2479
2480 if (hb_change == SPP_HB ||
2481 pmtud_change == SPP_PMTUD ||
2482 sackdelay_change == SPP_SACKDELAY ||
2483 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002484 (params.spp_pathmtu &&
2485 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002486 return -EINVAL;
2487
2488 /* If an address other than INADDR_ANY is specified, and
2489 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 */
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04002491 if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002492 trans = sctp_addr_id2transport(sk, &params.spp_address,
2493 params.spp_assoc_id);
2494 if (!trans)
2495 return -EINVAL;
2496 }
2497
2498 /* Get association, if assoc_id != 0 and the socket is a one
2499 * to many style socket, and an association was not found, then
2500 * the id was invalid.
2501 */
2502 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2503 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2504 return -EINVAL;
2505
2506 /* Heartbeat demand can only be sent on a transport or
2507 * association, but not a socket.
2508 */
2509 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2510 return -EINVAL;
2511
2512 /* Process parameters. */
2513 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2514 hb_change, pmtud_change,
2515 sackdelay_change);
2516
2517 if (error)
2518 return error;
2519
2520 /* If changes are for association, also apply parameters to each
2521 * transport.
2522 */
2523 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002524 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2525 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002526 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2527 hb_change, pmtud_change,
2528 sackdelay_change);
2529 }
2530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532 return 0;
2533}
2534
Wei Yongjund364d922008-05-09 15:13:26 -07002535/*
2536 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002537 *
Wei Yongjund364d922008-05-09 15:13:26 -07002538 * This option will effect the way delayed acks are performed. This
2539 * option allows you to get or set the delayed ack time, in
2540 * milliseconds. It also allows changing the delayed ack frequency.
2541 * Changing the frequency to 1 disables the delayed sack algorithm. If
2542 * the assoc_id is 0, then this sets or gets the endpoints default
2543 * values. If the assoc_id field is non-zero, then the set or get
2544 * effects the specified association for the one to many model (the
2545 * assoc_id field is ignored by the one to one model). Note that if
2546 * sack_delay or sack_freq are 0 when setting this option, then the
2547 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002548 *
Wei Yongjund364d922008-05-09 15:13:26 -07002549 * struct sctp_sack_info {
2550 * sctp_assoc_t sack_assoc_id;
2551 * uint32_t sack_delay;
2552 * uint32_t sack_freq;
2553 * };
Frank Filz77086102005-12-22 11:37:30 -08002554 *
Wei Yongjund364d922008-05-09 15:13:26 -07002555 * sack_assoc_id - This parameter, indicates which association the user
2556 * is performing an action upon. Note that if this field's value is
2557 * zero then the endpoints default value is changed (effecting future
2558 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002559 *
Wei Yongjund364d922008-05-09 15:13:26 -07002560 * sack_delay - This parameter contains the number of milliseconds that
2561 * the user is requesting the delayed ACK timer be set to. Note that
2562 * this value is defined in the standard to be between 200 and 500
2563 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002564 *
Wei Yongjund364d922008-05-09 15:13:26 -07002565 * sack_freq - This parameter contains the number of packets that must
2566 * be received before a sack is sent without waiting for the delay
2567 * timer to expire. The default value for this is 2, setting this
2568 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002569 */
2570
Wei Yongjund364d922008-05-09 15:13:26 -07002571static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002572 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002573{
Wei Yongjund364d922008-05-09 15:13:26 -07002574 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002575 struct sctp_transport *trans = NULL;
2576 struct sctp_association *asoc = NULL;
2577 struct sctp_sock *sp = sctp_sk(sk);
2578
Wei Yongjund364d922008-05-09 15:13:26 -07002579 if (optlen == sizeof(struct sctp_sack_info)) {
2580 if (copy_from_user(&params, optval, optlen))
2581 return -EFAULT;
2582
2583 if (params.sack_delay == 0 && params.sack_freq == 0)
2584 return 0;
2585 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Joe Perches145ce502010-08-24 13:21:08 +00002586 pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
2587 pr_warn("Use struct sctp_sack_info instead\n");
Wei Yongjund364d922008-05-09 15:13:26 -07002588 if (copy_from_user(&params, optval, optlen))
2589 return -EFAULT;
2590
2591 if (params.sack_delay == 0)
2592 params.sack_freq = 1;
2593 else
2594 params.sack_freq = 0;
2595 } else
Frank Filz77086102005-12-22 11:37:30 -08002596 return - EINVAL;
2597
Frank Filz77086102005-12-22 11:37:30 -08002598 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002599 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002600 return -EINVAL;
2601
Wei Yongjund364d922008-05-09 15:13:26 -07002602 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002603 * to many style socket, and an association was not found, then
2604 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002605 */
Wei Yongjund364d922008-05-09 15:13:26 -07002606 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2607 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002608 return -EINVAL;
2609
Wei Yongjund364d922008-05-09 15:13:26 -07002610 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002611 if (asoc) {
2612 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002613 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002614 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002615 (asoc->param_flags & ~SPP_SACKDELAY) |
2616 SPP_SACKDELAY_ENABLE;
2617 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002618 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002619 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002620 (sp->param_flags & ~SPP_SACKDELAY) |
2621 SPP_SACKDELAY_ENABLE;
2622 }
Wei Yongjund364d922008-05-09 15:13:26 -07002623 }
2624
2625 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002626 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002627 asoc->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002628 (asoc->param_flags & ~SPP_SACKDELAY) |
2629 SPP_SACKDELAY_DISABLE;
2630 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002631 sp->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002632 (sp->param_flags & ~SPP_SACKDELAY) |
2633 SPP_SACKDELAY_DISABLE;
2634 }
Wei Yongjund364d922008-05-09 15:13:26 -07002635 } else if (params.sack_freq > 1) {
2636 if (asoc) {
2637 asoc->sackfreq = params.sack_freq;
2638 asoc->param_flags =
2639 (asoc->param_flags & ~SPP_SACKDELAY) |
2640 SPP_SACKDELAY_ENABLE;
2641 } else {
2642 sp->sackfreq = params.sack_freq;
2643 sp->param_flags =
2644 (sp->param_flags & ~SPP_SACKDELAY) |
2645 SPP_SACKDELAY_ENABLE;
2646 }
Frank Filz77086102005-12-22 11:37:30 -08002647 }
2648
2649 /* If change is for association, also apply to each transport. */
2650 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002651 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2652 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002653 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002654 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002655 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002656 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002657 (trans->param_flags & ~SPP_SACKDELAY) |
2658 SPP_SACKDELAY_ENABLE;
Wei Yongjund364d922008-05-09 15:13:26 -07002659 }
Vlad Yasevich7bfe8bd2008-06-09 15:45:05 -07002660 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002661 trans->param_flags =
Frank Filz77086102005-12-22 11:37:30 -08002662 (trans->param_flags & ~SPP_SACKDELAY) |
2663 SPP_SACKDELAY_DISABLE;
Wei Yongjund364d922008-05-09 15:13:26 -07002664 } else if (params.sack_freq > 1) {
2665 trans->sackfreq = params.sack_freq;
2666 trans->param_flags =
2667 (trans->param_flags & ~SPP_SACKDELAY) |
2668 SPP_SACKDELAY_ENABLE;
Frank Filz77086102005-12-22 11:37:30 -08002669 }
2670 }
2671 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002672
Frank Filz77086102005-12-22 11:37:30 -08002673 return 0;
2674}
2675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2677 *
2678 * Applications can specify protocol parameters for the default association
2679 * initialization. The option name argument to setsockopt() and getsockopt()
2680 * is SCTP_INITMSG.
2681 *
2682 * Setting initialization parameters is effective only on an unconnected
2683 * socket (for UDP-style sockets only future associations are effected
2684 * by the change). With TCP-style sockets, this option is inherited by
2685 * sockets derived from a listener socket.
2686 */
David S. Millerb7058842009-09-30 16:12:20 -07002687static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
2689 struct sctp_initmsg sinit;
2690 struct sctp_sock *sp = sctp_sk(sk);
2691
2692 if (optlen != sizeof(struct sctp_initmsg))
2693 return -EINVAL;
2694 if (copy_from_user(&sinit, optval, optlen))
2695 return -EFAULT;
2696
2697 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002698 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002700 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002702 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002704 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706 return 0;
2707}
2708
2709/*
2710 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2711 *
2712 * Applications that wish to use the sendto() system call may wish to
2713 * specify a default set of parameters that would normally be supplied
2714 * through the inclusion of ancillary data. This socket option allows
2715 * such an application to set the default sctp_sndrcvinfo structure.
2716 * The application that wishes to use this socket option simply passes
2717 * in to this call the sctp_sndrcvinfo structure defined in Section
2718 * 5.2.2) The input parameters accepted by this call include
2719 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2720 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2721 * to this call if the caller is using the UDP model.
2722 */
2723static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002724 char __user *optval,
2725 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
2727 struct sctp_sndrcvinfo info;
2728 struct sctp_association *asoc;
2729 struct sctp_sock *sp = sctp_sk(sk);
2730
2731 if (optlen != sizeof(struct sctp_sndrcvinfo))
2732 return -EINVAL;
2733 if (copy_from_user(&info, optval, optlen))
2734 return -EFAULT;
2735
2736 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2737 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2738 return -EINVAL;
2739
2740 if (asoc) {
2741 asoc->default_stream = info.sinfo_stream;
2742 asoc->default_flags = info.sinfo_flags;
2743 asoc->default_ppid = info.sinfo_ppid;
2744 asoc->default_context = info.sinfo_context;
2745 asoc->default_timetolive = info.sinfo_timetolive;
2746 } else {
2747 sp->default_stream = info.sinfo_stream;
2748 sp->default_flags = info.sinfo_flags;
2749 sp->default_ppid = info.sinfo_ppid;
2750 sp->default_context = info.sinfo_context;
2751 sp->default_timetolive = info.sinfo_timetolive;
2752 }
2753
2754 return 0;
2755}
2756
2757/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2758 *
2759 * Requests that the local SCTP stack use the enclosed peer address as
2760 * the association primary. The enclosed address must be one of the
2761 * association peer's addresses.
2762 */
2763static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002764 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
2766 struct sctp_prim prim;
2767 struct sctp_transport *trans;
2768
2769 if (optlen != sizeof(struct sctp_prim))
2770 return -EINVAL;
2771
2772 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2773 return -EFAULT;
2774
2775 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2776 if (!trans)
2777 return -EINVAL;
2778
2779 sctp_assoc_set_primary(trans->asoc, trans);
2780
2781 return 0;
2782}
2783
2784/*
2785 * 7.1.5 SCTP_NODELAY
2786 *
2787 * Turn on/off any Nagle-like algorithm. This means that packets are
2788 * generally sent as soon as possible and no unnecessary delays are
2789 * introduced, at the cost of more packets in the network. Expects an
2790 * integer boolean flag.
2791 */
2792static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002793 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
2795 int val;
2796
2797 if (optlen < sizeof(int))
2798 return -EINVAL;
2799 if (get_user(val, (int __user *)optval))
2800 return -EFAULT;
2801
2802 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2803 return 0;
2804}
2805
2806/*
2807 *
2808 * 7.1.1 SCTP_RTOINFO
2809 *
2810 * The protocol parameters used to initialize and bound retransmission
2811 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2812 * and modify these parameters.
2813 * All parameters are time values, in milliseconds. A value of 0, when
2814 * modifying the parameters, indicates that the current value should not
2815 * be changed.
2816 *
2817 */
David S. Millerb7058842009-09-30 16:12:20 -07002818static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2819{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 struct sctp_rtoinfo rtoinfo;
2821 struct sctp_association *asoc;
2822
2823 if (optlen != sizeof (struct sctp_rtoinfo))
2824 return -EINVAL;
2825
2826 if (copy_from_user(&rtoinfo, optval, optlen))
2827 return -EFAULT;
2828
2829 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2830
2831 /* Set the values to the specific association */
2832 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2833 return -EINVAL;
2834
2835 if (asoc) {
2836 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002837 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 msecs_to_jiffies(rtoinfo.srto_initial);
2839 if (rtoinfo.srto_max != 0)
2840 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
2841 if (rtoinfo.srto_min != 0)
2842 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2843 } else {
2844 /* If there is no association or the association-id = 0
2845 * set the values to the endpoint.
2846 */
2847 struct sctp_sock *sp = sctp_sk(sk);
2848
2849 if (rtoinfo.srto_initial != 0)
2850 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2851 if (rtoinfo.srto_max != 0)
2852 sp->rtoinfo.srto_max = rtoinfo.srto_max;
2853 if (rtoinfo.srto_min != 0)
2854 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2855 }
2856
2857 return 0;
2858}
2859
2860/*
2861 *
2862 * 7.1.2 SCTP_ASSOCINFO
2863 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02002864 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 * of the association.
2866 * Returns an error if the new association retransmission value is
2867 * greater than the sum of the retransmission value of the peer.
2868 * See [SCTP] for more information.
2869 *
2870 */
David S. Millerb7058842009-09-30 16:12:20 -07002871static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
2873
2874 struct sctp_assocparams assocparams;
2875 struct sctp_association *asoc;
2876
2877 if (optlen != sizeof(struct sctp_assocparams))
2878 return -EINVAL;
2879 if (copy_from_user(&assocparams, optval, optlen))
2880 return -EFAULT;
2881
2882 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2883
2884 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2885 return -EINVAL;
2886
2887 /* Set the values to the specific association */
2888 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002889 if (assocparams.sasoc_asocmaxrxt != 0) {
2890 __u32 path_sum = 0;
2891 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002892 struct sctp_transport *peer_addr;
2893
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002894 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
2895 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002896 path_sum += peer_addr->pathmaxrxt;
2897 paths++;
2898 }
2899
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002900 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002901 * one path/transport. We do this because path
2902 * retransmissions are only counted when we have more
2903 * then one path.
2904 */
2905 if (paths > 1 &&
2906 assocparams.sasoc_asocmaxrxt > path_sum)
2907 return -EINVAL;
2908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002910 }
2911
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 if (assocparams.sasoc_cookie_life != 0) {
2913 asoc->cookie_life.tv_sec =
2914 assocparams.sasoc_cookie_life / 1000;
2915 asoc->cookie_life.tv_usec =
2916 (assocparams.sasoc_cookie_life % 1000)
2917 * 1000;
2918 }
2919 } else {
2920 /* Set the values to the endpoint */
2921 struct sctp_sock *sp = sctp_sk(sk);
2922
2923 if (assocparams.sasoc_asocmaxrxt != 0)
2924 sp->assocparams.sasoc_asocmaxrxt =
2925 assocparams.sasoc_asocmaxrxt;
2926 if (assocparams.sasoc_cookie_life != 0)
2927 sp->assocparams.sasoc_cookie_life =
2928 assocparams.sasoc_cookie_life;
2929 }
2930 return 0;
2931}
2932
2933/*
2934 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
2935 *
2936 * This socket option is a boolean flag which turns on or off mapped V4
2937 * addresses. If this option is turned on and the socket is type
2938 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
2939 * If this option is turned off, then no mapping will be done of V4
2940 * addresses and a user will receive both PF_INET6 and PF_INET type
2941 * addresses on the socket.
2942 */
David S. Millerb7058842009-09-30 16:12:20 -07002943static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944{
2945 int val;
2946 struct sctp_sock *sp = sctp_sk(sk);
2947
2948 if (optlen < sizeof(int))
2949 return -EINVAL;
2950 if (get_user(val, (int __user *)optval))
2951 return -EFAULT;
2952 if (val)
2953 sp->v4mapped = 1;
2954 else
2955 sp->v4mapped = 0;
2956
2957 return 0;
2958}
2959
2960/*
Wei Yongjune89c2092008-12-25 16:54:58 -08002961 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
2962 * This option will get or set the maximum size to put in any outgoing
2963 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 * fragmented by SCTP into the specified size. Note that the underlying
2965 * SCTP implementation may fragment into smaller sized chunks when the
2966 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08002967 * the user. The default value for this option is '0' which indicates
2968 * the user is NOT limiting fragmentation and only the PMTU will effect
2969 * SCTP's choice of DATA chunk size. Note also that values set larger
2970 * than the maximum size of an IP datagram will effectively let SCTP
2971 * control fragmentation (i.e. the same as setting this option to 0).
2972 *
2973 * The following structure is used to access and modify this parameter:
2974 *
2975 * struct sctp_assoc_value {
2976 * sctp_assoc_t assoc_id;
2977 * uint32_t assoc_value;
2978 * };
2979 *
2980 * assoc_id: This parameter is ignored for one-to-one style sockets.
2981 * For one-to-many style sockets this parameter indicates which
2982 * association the user is performing an action upon. Note that if
2983 * this field's value is zero then the endpoints default value is
2984 * changed (effecting future associations only).
2985 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 */
David S. Millerb7058842009-09-30 16:12:20 -07002987static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
Wei Yongjune89c2092008-12-25 16:54:58 -08002989 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 struct sctp_sock *sp = sctp_sk(sk);
2992 int val;
2993
Wei Yongjune89c2092008-12-25 16:54:58 -08002994 if (optlen == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00002995 pr_warn("Use of int in maxseg socket option deprecated\n");
2996 pr_warn("Use struct sctp_assoc_value instead\n");
Wei Yongjune89c2092008-12-25 16:54:58 -08002997 if (copy_from_user(&val, optval, optlen))
2998 return -EFAULT;
2999 params.assoc_id = 0;
3000 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3001 if (copy_from_user(&params, optval, optlen))
3002 return -EFAULT;
3003 val = params.assoc_value;
3004 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003006
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07003007 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Wei Yongjune89c2092008-12-25 16:54:58 -08003010 asoc = sctp_id2assoc(sk, params.assoc_id);
3011 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3012 return -EINVAL;
3013
3014 if (asoc) {
3015 if (val == 0) {
3016 val = asoc->pathmtu;
3017 val -= sp->pf->af->net_header_len;
3018 val -= sizeof(struct sctphdr) +
3019 sizeof(struct sctp_data_chunk);
3020 }
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003021 asoc->user_frag = val;
3022 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
Wei Yongjune89c2092008-12-25 16:54:58 -08003023 } else {
3024 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 }
3026
3027 return 0;
3028}
3029
3030
3031/*
3032 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3033 *
3034 * Requests that the peer mark the enclosed address as the association
3035 * primary. The enclosed address must be one of the association's
3036 * locally bound addresses. The following structure is used to make a
3037 * set primary request:
3038 */
3039static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003040 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
3042 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 struct sctp_association *asoc = NULL;
3044 struct sctp_setpeerprim prim;
3045 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003046 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 int err;
3048
3049 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051 if (!sctp_addip_enable)
3052 return -EPERM;
3053
3054 if (optlen != sizeof(struct sctp_setpeerprim))
3055 return -EINVAL;
3056
3057 if (copy_from_user(&prim, optval, optlen))
3058 return -EFAULT;
3059
3060 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003061 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 return -EINVAL;
3063
3064 if (!asoc->peer.asconf_capable)
3065 return -EPERM;
3066
3067 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3068 return -EPERM;
3069
3070 if (!sctp_state(asoc, ESTABLISHED))
3071 return -ENOTCONN;
3072
Wei Yongjun40a01032010-12-07 17:11:09 +00003073 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3074 if (!af)
3075 return -EINVAL;
3076
3077 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3078 return -EADDRNOTAVAIL;
3079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3081 return -EADDRNOTAVAIL;
3082
3083 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3084 chunk = sctp_make_asconf_set_prim(asoc,
3085 (union sctp_addr *)&prim.sspp_addr);
3086 if (!chunk)
3087 return -ENOMEM;
3088
3089 err = sctp_send_asconf(asoc, chunk);
3090
3091 SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
3092
3093 return err;
3094}
3095
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003096static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003097 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003099 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003101 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003103 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return -EFAULT;
3105
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003106 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
3108 return 0;
3109}
3110
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003111/*
3112 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3113 *
3114 * The context field in the sctp_sndrcvinfo structure is normally only
3115 * used when a failed message is retrieved holding the value that was
3116 * sent down on the actual send call. This option allows the setting of
3117 * a default context on an association basis that will be received on
3118 * reading messages from the peer. This is especially helpful in the
3119 * one-2-many model for an application to keep some reference to an
3120 * internal state machine that is processing messages on the
3121 * association. Note that the setting of this value only effects
3122 * received messages from the peer and does not effect the value that is
3123 * saved with outbound messages.
3124 */
3125static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003126 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003127{
3128 struct sctp_assoc_value params;
3129 struct sctp_sock *sp;
3130 struct sctp_association *asoc;
3131
3132 if (optlen != sizeof(struct sctp_assoc_value))
3133 return -EINVAL;
3134 if (copy_from_user(&params, optval, optlen))
3135 return -EFAULT;
3136
3137 sp = sctp_sk(sk);
3138
3139 if (params.assoc_id != 0) {
3140 asoc = sctp_id2assoc(sk, params.assoc_id);
3141 if (!asoc)
3142 return -EINVAL;
3143 asoc->default_rcv_context = params.assoc_value;
3144 } else {
3145 sp->default_rcv_context = params.assoc_value;
3146 }
3147
3148 return 0;
3149}
3150
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003151/*
3152 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3153 *
3154 * This options will at a minimum specify if the implementation is doing
3155 * fragmented interleave. Fragmented interleave, for a one to many
3156 * socket, is when subsequent calls to receive a message may return
3157 * parts of messages from different associations. Some implementations
3158 * may allow you to turn this value on or off. If so, when turned off,
3159 * no fragment interleave will occur (which will cause a head of line
3160 * blocking amongst multiple associations sharing the same one to many
3161 * socket). When this option is turned on, then each receive call may
3162 * come from a different association (thus the user must receive data
3163 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3164 * association each receive belongs to.
3165 *
3166 * This option takes a boolean value. A non-zero value indicates that
3167 * fragmented interleave is on. A value of zero indicates that
3168 * fragmented interleave is off.
3169 *
3170 * Note that it is important that an implementation that allows this
3171 * option to be turned on, have it off by default. Otherwise an unaware
3172 * application using the one to many model may become confused and act
3173 * incorrectly.
3174 */
3175static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3176 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003177 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003178{
3179 int val;
3180
3181 if (optlen != sizeof(int))
3182 return -EINVAL;
3183 if (get_user(val, (int __user *)optval))
3184 return -EFAULT;
3185
3186 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
3187
3188 return 0;
3189}
3190
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003191/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003192 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003193 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003194 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003195 * This option will set or get the SCTP partial delivery point. This
3196 * point is the size of a message where the partial delivery API will be
3197 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003198 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003199 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003200 * point. Note also that the call will fail if the user attempts to set
3201 * this value larger than the socket receive buffer size.
3202 *
3203 * Note that any single message having a length smaller than or equal to
3204 * the SCTP partial delivery point will be delivered in one single read
3205 * call as long as the user provided buffer is large enough to hold the
3206 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003207 */
3208static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3209 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003210 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003211{
3212 u32 val;
3213
3214 if (optlen != sizeof(u32))
3215 return -EINVAL;
3216 if (get_user(val, (int __user *)optval))
3217 return -EFAULT;
3218
Wei Yongjun8510b932008-12-25 16:59:03 -08003219 /* Note: We double the receive buffer from what the user sets
3220 * it to be, also initial rwnd is based on rcvbuf/2.
3221 */
3222 if (val > (sk->sk_rcvbuf >> 1))
3223 return -EINVAL;
3224
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003225 sctp_sk(sk)->pd_point = val;
3226
3227 return 0; /* is this the right error code? */
3228}
3229
Vlad Yasevich70331572007-03-23 11:34:36 -07003230/*
3231 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3232 *
3233 * This option will allow a user to change the maximum burst of packets
3234 * that can be emitted by this association. Note that the default value
3235 * is 4, and some implementations may restrict this setting so that it
3236 * can only be lowered.
3237 *
3238 * NOTE: This text doesn't seem right. Do this on a socket basis with
3239 * future associations inheriting the socket value.
3240 */
3241static int sctp_setsockopt_maxburst(struct sock *sk,
3242 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003243 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003244{
Neil Horman219b99a2008-03-05 13:44:46 -08003245 struct sctp_assoc_value params;
3246 struct sctp_sock *sp;
3247 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003248 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003249 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003250
Neil Horman219b99a2008-03-05 13:44:46 -08003251 if (optlen == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00003252 pr_warn("Use of int in max_burst socket option deprecated\n");
3253 pr_warn("Use struct sctp_assoc_value instead\n");
Neil Horman219b99a2008-03-05 13:44:46 -08003254 if (copy_from_user(&val, optval, optlen))
3255 return -EFAULT;
3256 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3257 if (copy_from_user(&params, optval, optlen))
3258 return -EFAULT;
3259 val = params.assoc_value;
3260 assoc_id = params.assoc_id;
3261 } else
3262 return -EINVAL;
3263
3264 sp = sctp_sk(sk);
3265
3266 if (assoc_id != 0) {
3267 asoc = sctp_id2assoc(sk, assoc_id);
3268 if (!asoc)
3269 return -EINVAL;
3270 asoc->max_burst = val;
3271 } else
3272 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003273
3274 return 0;
3275}
3276
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003277/*
3278 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3279 *
3280 * This set option adds a chunk type that the user is requesting to be
3281 * received only in an authenticated way. Changes to the list of chunks
3282 * will only effect future associations on the socket.
3283 */
3284static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003285 char __user *optval,
3286 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003287{
3288 struct sctp_authchunk val;
3289
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003290 if (!sctp_auth_enable)
3291 return -EACCES;
3292
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003293 if (optlen != sizeof(struct sctp_authchunk))
3294 return -EINVAL;
3295 if (copy_from_user(&val, optval, optlen))
3296 return -EFAULT;
3297
3298 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003299 case SCTP_CID_INIT:
3300 case SCTP_CID_INIT_ACK:
3301 case SCTP_CID_SHUTDOWN_COMPLETE:
3302 case SCTP_CID_AUTH:
3303 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003304 }
3305
3306 /* add this chunk id to the endpoint */
3307 return sctp_auth_ep_add_chunkid(sctp_sk(sk)->ep, val.sauth_chunk);
3308}
3309
3310/*
3311 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3312 *
3313 * This option gets or sets the list of HMAC algorithms that the local
3314 * endpoint requires the peer to use.
3315 */
3316static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003317 char __user *optval,
3318 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003319{
3320 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003321 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003322 int err;
3323
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003324 if (!sctp_auth_enable)
3325 return -EACCES;
3326
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003327 if (optlen < sizeof(struct sctp_hmacalgo))
3328 return -EINVAL;
3329
Shan Wei934253a2011-04-18 19:13:18 +00003330 hmacs= memdup_user(optval, optlen);
3331 if (IS_ERR(hmacs))
3332 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003333
Vlad Yasevichd9724052008-08-27 16:09:49 -07003334 idents = hmacs->shmac_num_idents;
3335 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3336 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003337 err = -EINVAL;
3338 goto out;
3339 }
3340
3341 err = sctp_auth_ep_set_hmacs(sctp_sk(sk)->ep, hmacs);
3342out:
3343 kfree(hmacs);
3344 return err;
3345}
3346
3347/*
3348 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3349 *
3350 * This option will set a shared secret key which is used to build an
3351 * association shared key.
3352 */
3353static int sctp_setsockopt_auth_key(struct sock *sk,
3354 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003355 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003356{
3357 struct sctp_authkey *authkey;
3358 struct sctp_association *asoc;
3359 int ret;
3360
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003361 if (!sctp_auth_enable)
3362 return -EACCES;
3363
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003364 if (optlen <= sizeof(struct sctp_authkey))
3365 return -EINVAL;
3366
Shan Wei934253a2011-04-18 19:13:18 +00003367 authkey= memdup_user(optval, optlen);
3368 if (IS_ERR(authkey))
3369 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003370
Vlad Yasevich328fc472008-08-27 16:08:54 -07003371 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003372 ret = -EINVAL;
3373 goto out;
3374 }
3375
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003376 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3377 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3378 ret = -EINVAL;
3379 goto out;
3380 }
3381
3382 ret = sctp_auth_set_key(sctp_sk(sk)->ep, asoc, authkey);
3383out:
3384 kfree(authkey);
3385 return ret;
3386}
3387
3388/*
3389 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3390 *
3391 * This option will get or set the active shared key to be used to build
3392 * the association shared key.
3393 */
3394static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003395 char __user *optval,
3396 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003397{
3398 struct sctp_authkeyid val;
3399 struct sctp_association *asoc;
3400
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003401 if (!sctp_auth_enable)
3402 return -EACCES;
3403
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003404 if (optlen != sizeof(struct sctp_authkeyid))
3405 return -EINVAL;
3406 if (copy_from_user(&val, optval, optlen))
3407 return -EFAULT;
3408
3409 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3410 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3411 return -EINVAL;
3412
3413 return sctp_auth_set_active_key(sctp_sk(sk)->ep, asoc,
3414 val.scact_keynumber);
3415}
3416
3417/*
3418 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3419 *
3420 * This set option will delete a shared secret key from use.
3421 */
3422static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003423 char __user *optval,
3424 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003425{
3426 struct sctp_authkeyid val;
3427 struct sctp_association *asoc;
3428
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003429 if (!sctp_auth_enable)
3430 return -EACCES;
3431
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003432 if (optlen != sizeof(struct sctp_authkeyid))
3433 return -EINVAL;
3434 if (copy_from_user(&val, optval, optlen))
3435 return -EFAULT;
3436
3437 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3438 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3439 return -EINVAL;
3440
3441 return sctp_auth_del_key_id(sctp_sk(sk)->ep, asoc,
3442 val.scact_keynumber);
3443
3444}
3445
Michio Honda7dc04d72011-04-26 20:16:31 +09003446/*
3447 * 8.1.23 SCTP_AUTO_ASCONF
3448 *
3449 * This option will enable or disable the use of the automatic generation of
3450 * ASCONF chunks to add and delete addresses to an existing association. Note
3451 * that this option has two caveats namely: a) it only affects sockets that
3452 * are bound to all addresses available to the SCTP stack, and b) the system
3453 * administrator may have an overriding control that turns the ASCONF feature
3454 * off no matter what setting the socket option may have.
3455 * This option expects an integer boolean flag, where a non-zero value turns on
3456 * the option, and a zero value turns off the option.
3457 * Note. In this implementation, socket operation overrides default parameter
3458 * being set by sysctl as well as FreeBSD implementation
3459 */
3460static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3461 unsigned int optlen)
3462{
3463 int val;
3464 struct sctp_sock *sp = sctp_sk(sk);
3465
3466 if (optlen < sizeof(int))
3467 return -EINVAL;
3468 if (get_user(val, (int __user *)optval))
3469 return -EFAULT;
3470 if (!sctp_is_ep_boundall(sk) && val)
3471 return -EINVAL;
3472 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3473 return 0;
3474
3475 if (val == 0 && sp->do_auto_asconf) {
3476 list_del(&sp->auto_asconf_list);
3477 sp->do_auto_asconf = 0;
3478 } else if (val && !sp->do_auto_asconf) {
3479 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00003480 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09003481 sp->do_auto_asconf = 1;
3482 }
3483 return 0;
3484}
3485
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003486
Neil Horman5aa93bc2012-07-21 07:56:07 +00003487/*
3488 * SCTP_PEER_ADDR_THLDS
3489 *
3490 * This option allows us to alter the partially failed threshold for one or all
3491 * transports in an association. See Section 6.1 of:
3492 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3493 */
3494static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3495 char __user *optval,
3496 unsigned int optlen)
3497{
3498 struct sctp_paddrthlds val;
3499 struct sctp_transport *trans;
3500 struct sctp_association *asoc;
3501
3502 if (optlen < sizeof(struct sctp_paddrthlds))
3503 return -EINVAL;
3504 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3505 sizeof(struct sctp_paddrthlds)))
3506 return -EFAULT;
3507
3508
3509 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3510 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3511 if (!asoc)
3512 return -ENOENT;
3513 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3514 transports) {
3515 if (val.spt_pathmaxrxt)
3516 trans->pathmaxrxt = val.spt_pathmaxrxt;
3517 trans->pf_retrans = val.spt_pathpfthld;
3518 }
3519
3520 if (val.spt_pathmaxrxt)
3521 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3522 asoc->pf_retrans = val.spt_pathpfthld;
3523 } else {
3524 trans = sctp_addr_id2transport(sk, &val.spt_address,
3525 val.spt_assoc_id);
3526 if (!trans)
3527 return -ENOENT;
3528
3529 if (val.spt_pathmaxrxt)
3530 trans->pathmaxrxt = val.spt_pathmaxrxt;
3531 trans->pf_retrans = val.spt_pathpfthld;
3532 }
3533
3534 return 0;
3535}
3536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537/* API 6.2 setsockopt(), getsockopt()
3538 *
3539 * Applications use setsockopt() and getsockopt() to set or retrieve
3540 * socket options. Socket options are used to change the default
3541 * behavior of sockets calls. They are described in Section 7.
3542 *
3543 * The syntax is:
3544 *
3545 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3546 * int __user *optlen);
3547 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
3548 * int optlen);
3549 *
3550 * sd - the socket descript.
3551 * level - set to IPPROTO_SCTP for all SCTP options.
3552 * optname - the option name.
3553 * optval - the buffer to store the value of the option.
3554 * optlen - the size of the buffer.
3555 */
3556SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07003557 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
3559 int retval = 0;
3560
3561 SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
3562 sk, optname);
3563
3564 /* I can hardly begin to describe how wrong this is. This is
3565 * so broken as to be worse than useless. The API draft
3566 * REALLY is NOT helpful here... I am not convinced that the
3567 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
3568 * are at all well-founded.
3569 */
3570 if (level != SOL_SCTP) {
3571 struct sctp_af *af = sctp_sk(sk)->pf->af;
3572 retval = af->setsockopt(sk, level, optname, optval, optlen);
3573 goto out_nounlock;
3574 }
3575
3576 sctp_lock_sock(sk);
3577
3578 switch (optname) {
3579 case SCTP_SOCKOPT_BINDX_ADD:
3580 /* 'optlen' is the size of the addresses buffer. */
3581 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3582 optlen, SCTP_BINDX_ADD_ADDR);
3583 break;
3584
3585 case SCTP_SOCKOPT_BINDX_REM:
3586 /* 'optlen' is the size of the addresses buffer. */
3587 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3588 optlen, SCTP_BINDX_REM_ADDR);
3589 break;
3590
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003591 case SCTP_SOCKOPT_CONNECTX_OLD:
3592 /* 'optlen' is the size of the addresses buffer. */
3593 retval = sctp_setsockopt_connectx_old(sk,
3594 (struct sockaddr __user *)optval,
3595 optlen);
3596 break;
3597
Frank Filz3f7a87d2005-06-20 13:14:57 -07003598 case SCTP_SOCKOPT_CONNECTX:
3599 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003600 retval = sctp_setsockopt_connectx(sk,
3601 (struct sockaddr __user *)optval,
3602 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003603 break;
3604
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 case SCTP_DISABLE_FRAGMENTS:
3606 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3607 break;
3608
3609 case SCTP_EVENTS:
3610 retval = sctp_setsockopt_events(sk, optval, optlen);
3611 break;
3612
3613 case SCTP_AUTOCLOSE:
3614 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3615 break;
3616
3617 case SCTP_PEER_ADDR_PARAMS:
3618 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3619 break;
3620
Shan Wei4580ccc2011-01-18 22:39:00 +00003621 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07003622 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08003623 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003624 case SCTP_PARTIAL_DELIVERY_POINT:
3625 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3626 break;
Frank Filz77086102005-12-22 11:37:30 -08003627
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 case SCTP_INITMSG:
3629 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3630 break;
3631 case SCTP_DEFAULT_SEND_PARAM:
3632 retval = sctp_setsockopt_default_send_param(sk, optval,
3633 optlen);
3634 break;
3635 case SCTP_PRIMARY_ADDR:
3636 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3637 break;
3638 case SCTP_SET_PEER_PRIMARY_ADDR:
3639 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3640 break;
3641 case SCTP_NODELAY:
3642 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3643 break;
3644 case SCTP_RTOINFO:
3645 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3646 break;
3647 case SCTP_ASSOCINFO:
3648 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3649 break;
3650 case SCTP_I_WANT_MAPPED_V4_ADDR:
3651 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3652 break;
3653 case SCTP_MAXSEG:
3654 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3655 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003656 case SCTP_ADAPTATION_LAYER:
3657 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003659 case SCTP_CONTEXT:
3660 retval = sctp_setsockopt_context(sk, optval, optlen);
3661 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003662 case SCTP_FRAGMENT_INTERLEAVE:
3663 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3664 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07003665 case SCTP_MAX_BURST:
3666 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3667 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003668 case SCTP_AUTH_CHUNK:
3669 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
3670 break;
3671 case SCTP_HMAC_IDENT:
3672 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
3673 break;
3674 case SCTP_AUTH_KEY:
3675 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
3676 break;
3677 case SCTP_AUTH_ACTIVE_KEY:
3678 retval = sctp_setsockopt_active_key(sk, optval, optlen);
3679 break;
3680 case SCTP_AUTH_DELETE_KEY:
3681 retval = sctp_setsockopt_del_key(sk, optval, optlen);
3682 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09003683 case SCTP_AUTO_ASCONF:
3684 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
3685 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00003686 case SCTP_PEER_ADDR_THLDS:
3687 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
3688 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 default:
3690 retval = -ENOPROTOOPT;
3691 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
3694 sctp_release_sock(sk);
3695
3696out_nounlock:
3697 return retval;
3698}
3699
3700/* API 3.1.6 connect() - UDP Style Syntax
3701 *
3702 * An application may use the connect() call in the UDP model to initiate an
3703 * association without sending data.
3704 *
3705 * The syntax is:
3706 *
3707 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3708 *
3709 * sd: the socket descriptor to have a new association added to.
3710 *
3711 * nam: the address structure (either struct sockaddr_in or struct
3712 * sockaddr_in6 defined in RFC2553 [7]).
3713 *
3714 * len: the size of the address.
3715 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07003716SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 int addr_len)
3718{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07003720 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
3722 sctp_lock_sock(sk);
3723
Frank Filz3f7a87d2005-06-20 13:14:57 -07003724 SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08003725 __func__, sk, addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
Frank Filz3f7a87d2005-06-20 13:14:57 -07003727 /* Validate addr_len before calling common connect/connectx routine. */
3728 af = sctp_get_af_specific(addr->sa_family);
3729 if (!af || addr_len < af->sockaddr_len) {
3730 err = -EINVAL;
3731 } else {
3732 /* Pass correct addr len to common routine (so it knows there
3733 * is only one address being passed.
3734 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003735 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 }
3737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 sctp_release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 return err;
3740}
3741
3742/* FIXME: Write comments. */
3743SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
3744{
3745 return -EOPNOTSUPP; /* STUB */
3746}
3747
3748/* 4.1.4 accept() - TCP Style Syntax
3749 *
3750 * Applications use accept() call to remove an established SCTP
3751 * association from the accept queue of the endpoint. A new socket
3752 * descriptor will be returned from accept() to represent the newly
3753 * formed association.
3754 */
3755SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
3756{
3757 struct sctp_sock *sp;
3758 struct sctp_endpoint *ep;
3759 struct sock *newsk = NULL;
3760 struct sctp_association *asoc;
3761 long timeo;
3762 int error = 0;
3763
3764 sctp_lock_sock(sk);
3765
3766 sp = sctp_sk(sk);
3767 ep = sp->ep;
3768
3769 if (!sctp_style(sk, TCP)) {
3770 error = -EOPNOTSUPP;
3771 goto out;
3772 }
3773
3774 if (!sctp_sstate(sk, LISTENING)) {
3775 error = -EINVAL;
3776 goto out;
3777 }
3778
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07003779 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
3781 error = sctp_wait_for_accept(sk, timeo);
3782 if (error)
3783 goto out;
3784
3785 /* We treat the list of associations on the endpoint as the accept
3786 * queue and pick the first association on the list.
3787 */
3788 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
3789
3790 newsk = sp->pf->create_accept_sk(sk, asoc);
3791 if (!newsk) {
3792 error = -ENOMEM;
3793 goto out;
3794 }
3795
3796 /* Populate the fields of the newsk from the oldsk and migrate the
3797 * asoc to the newsk.
3798 */
3799 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
3800
3801out:
3802 sctp_release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003803 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 return newsk;
3805}
3806
3807/* The SCTP ioctl handler. */
3808SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
3809{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00003810 int rc = -ENOTCONN;
3811
3812 sctp_lock_sock(sk);
3813
3814 /*
3815 * SEQPACKET-style sockets in LISTENING state are valid, for
3816 * SCTP, so only discard TCP-style sockets in LISTENING state.
3817 */
3818 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
3819 goto out;
3820
3821 switch (cmd) {
3822 case SIOCINQ: {
3823 struct sk_buff *skb;
3824 unsigned int amount = 0;
3825
3826 skb = skb_peek(&sk->sk_receive_queue);
3827 if (skb != NULL) {
3828 /*
3829 * We will only return the amount of this packet since
3830 * that is all that will be read.
3831 */
3832 amount = skb->len;
3833 }
3834 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00003835 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07003836 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00003837 default:
3838 rc = -ENOIOCTLCMD;
3839 break;
3840 }
3841out:
3842 sctp_release_sock(sk);
3843 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844}
3845
3846/* This is the function which gets called during socket creation to
3847 * initialized the SCTP-specific portion of the sock.
3848 * The sock structure should already be zero-filled memory.
3849 */
3850SCTP_STATIC int sctp_init_sock(struct sock *sk)
3851{
3852 struct sctp_endpoint *ep;
3853 struct sctp_sock *sp;
3854
3855 SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
3856
3857 sp = sctp_sk(sk);
3858
3859 /* Initialize the SCTP per socket area. */
3860 switch (sk->sk_type) {
3861 case SOCK_SEQPACKET:
3862 sp->type = SCTP_SOCKET_UDP;
3863 break;
3864 case SOCK_STREAM:
3865 sp->type = SCTP_SOCKET_TCP;
3866 break;
3867 default:
3868 return -ESOCKTNOSUPPORT;
3869 }
3870
3871 /* Initialize default send parameters. These parameters can be
3872 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
3873 */
3874 sp->default_stream = 0;
3875 sp->default_ppid = 0;
3876 sp->default_flags = 0;
3877 sp->default_context = 0;
3878 sp->default_timetolive = 0;
3879
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003880 sp->default_rcv_context = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003881 sp->max_burst = sctp_max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003882
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 /* Initialize default setup parameters. These parameters
3884 * can be modified with the SCTP_INITMSG socket option or
3885 * overridden by the SCTP_INIT CMSG.
3886 */
3887 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
3888 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
3889 sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003890 sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
3892 /* Initialize default RTO related parameters. These parameters can
3893 * be modified for with the SCTP_RTOINFO socket option.
3894 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003895 sp->rtoinfo.srto_initial = sctp_rto_initial;
3896 sp->rtoinfo.srto_max = sctp_rto_max;
3897 sp->rtoinfo.srto_min = sctp_rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
3899 /* Initialize default association related parameters. These parameters
3900 * can be modified with the SCTP_ASSOCINFO socket option.
3901 */
3902 sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
3903 sp->assocparams.sasoc_number_peer_destinations = 0;
3904 sp->assocparams.sasoc_peer_rwnd = 0;
3905 sp->assocparams.sasoc_local_rwnd = 0;
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003906 sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
3908 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003909 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 */
3911 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
3912
3913 /* Default Peer Address Parameters. These defaults can
3914 * be modified via SCTP_PEER_ADDR_PARAMS
3915 */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003916 sp->hbinterval = sctp_hb_interval;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003917 sp->pathmaxrxt = sctp_max_retrans_path;
3918 sp->pathmtu = 0; // allow default discovery
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07003919 sp->sackdelay = sctp_sack_timeout;
Vlad Yasevich7bfe8bd2008-06-09 15:45:05 -07003920 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08003921 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003922 SPP_PMTUD_ENABLE |
3923 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924
3925 /* If enabled no SCTP message fragmentation will be performed.
3926 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
3927 */
3928 sp->disable_fragments = 0;
3929
Sridhar Samudrala208edef2006-09-29 17:08:01 -07003930 /* Enable Nagle algorithm by default. */
3931 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
3933 /* Enable by default. */
3934 sp->v4mapped = 1;
3935
3936 /* Auto-close idle associations after the configured
3937 * number of seconds. A value of 0 disables this
3938 * feature. Configure through the SCTP_AUTOCLOSE socket option,
3939 * for UDP-style sockets only.
3940 */
3941 sp->autoclose = 0;
3942
3943 /* User specified fragmentation limit. */
3944 sp->user_frag = 0;
3945
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003946 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
3948 sp->pf = sctp_get_pf_specific(sk->sk_family);
3949
3950 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003951 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003953 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
3955 /* Create a per socket endpoint structure. Even if we
3956 * change the data structure relationships, this may still
3957 * be useful for storing pre-connect address information.
3958 */
3959 ep = sctp_endpoint_new(sk, GFP_KERNEL);
3960 if (!ep)
3961 return -ENOMEM;
3962
3963 sp->ep = ep;
3964 sp->hmac = NULL;
3965
3966 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08003967
3968 local_bh_disable();
Vlad Yasevich81419d82010-04-28 08:47:20 +00003969 percpu_counter_inc(&sctp_sockets_allocated);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08003970 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
Michio Honda9f7d6532011-04-26 19:32:51 +09003971 if (sctp_default_auto_asconf) {
3972 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00003973 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09003974 sp->do_auto_asconf = 1;
3975 } else
3976 sp->do_auto_asconf = 0;
David S. Miller6f756a82008-11-23 17:34:03 -08003977 local_bh_enable();
3978
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 return 0;
3980}
3981
3982/* Cleanup any SCTP per socket resources. */
Brian Haley7d06b2e2008-06-14 17:04:49 -07003983SCTP_STATIC void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
Michio Honda9f7d6532011-04-26 19:32:51 +09003985 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
3987 SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
3988
3989 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09003990 sp = sctp_sk(sk);
3991 if (sp->do_auto_asconf) {
3992 sp->do_auto_asconf = 0;
3993 list_del(&sp->auto_asconf_list);
3994 }
3995 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08003996 local_bh_disable();
Vlad Yasevich81419d82010-04-28 08:47:20 +00003997 percpu_counter_dec(&sctp_sockets_allocated);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08003998 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08003999 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000}
4001
4002/* API 4.1.7 shutdown() - TCP Style Syntax
4003 * int shutdown(int socket, int how);
4004 *
4005 * sd - the socket descriptor of the association to be closed.
4006 * how - Specifies the type of shutdown. The values are
4007 * as follows:
4008 * SHUT_RD
4009 * Disables further receive operations. No SCTP
4010 * protocol action is taken.
4011 * SHUT_WR
4012 * Disables further send operations, and initiates
4013 * the SCTP shutdown sequence.
4014 * SHUT_RDWR
4015 * Disables further send and receive operations
4016 * and initiates the SCTP shutdown sequence.
4017 */
4018SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
4019{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004020 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 struct sctp_endpoint *ep;
4022 struct sctp_association *asoc;
4023
4024 if (!sctp_style(sk, TCP))
4025 return;
4026
4027 if (how & SEND_SHUTDOWN) {
4028 ep = sctp_sk(sk)->ep;
4029 if (!list_empty(&ep->asocs)) {
4030 asoc = list_entry(ep->asocs.next,
4031 struct sctp_association, asocs);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004032 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 }
4034 }
4035}
4036
4037/* 7.2.1 Association Status (SCTP_STATUS)
4038
4039 * Applications can retrieve current status information about an
4040 * association, including association state, peer receiver window size,
4041 * number of unacked data chunks, and number of data chunks pending
4042 * receipt. This information is read-only.
4043 */
4044static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4045 char __user *optval,
4046 int __user *optlen)
4047{
4048 struct sctp_status status;
4049 struct sctp_association *asoc = NULL;
4050 struct sctp_transport *transport;
4051 sctp_assoc_t associd;
4052 int retval = 0;
4053
Neil Horman408f22e2007-06-16 14:03:45 -04004054 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 retval = -EINVAL;
4056 goto out;
4057 }
4058
Neil Horman408f22e2007-06-16 14:03:45 -04004059 len = sizeof(status);
4060 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 retval = -EFAULT;
4062 goto out;
4063 }
4064
4065 associd = status.sstat_assoc_id;
4066 asoc = sctp_id2assoc(sk, associd);
4067 if (!asoc) {
4068 retval = -EINVAL;
4069 goto out;
4070 }
4071
4072 transport = asoc->peer.primary_path;
4073
4074 status.sstat_assoc_id = sctp_assoc2id(asoc);
4075 status.sstat_state = asoc->state;
4076 status.sstat_rwnd = asoc->peer.rwnd;
4077 status.sstat_unackdata = asoc->unack_data;
4078
4079 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4080 status.sstat_instrms = asoc->c.sinit_max_instreams;
4081 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
4082 status.sstat_fragmentation_point = asoc->frag_point;
4083 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08004084 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4085 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 /* Map ipv4 address into v4-mapped-on-v6 address. */
4087 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4088 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004089 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4091 status.sstat_primary.spinfo_srtt = transport->srtt;
4092 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004093 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
Frank Filz3f7a87d2005-06-20 13:14:57 -07004095 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4096 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4097
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 if (put_user(len, optlen)) {
4099 retval = -EFAULT;
4100 goto out;
4101 }
4102
4103 SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
4104 len, status.sstat_state, status.sstat_rwnd,
4105 status.sstat_assoc_id);
4106
4107 if (copy_to_user(optval, &status, len)) {
4108 retval = -EFAULT;
4109 goto out;
4110 }
4111
4112out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004113 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114}
4115
4116
4117/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4118 *
4119 * Applications can retrieve information about a specific peer address
4120 * of an association, including its reachability state, congestion
4121 * window, and retransmission timer values. This information is
4122 * read-only.
4123 */
4124static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4125 char __user *optval,
4126 int __user *optlen)
4127{
4128 struct sctp_paddrinfo pinfo;
4129 struct sctp_transport *transport;
4130 int retval = 0;
4131
Neil Horman408f22e2007-06-16 14:03:45 -04004132 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 retval = -EINVAL;
4134 goto out;
4135 }
4136
Neil Horman408f22e2007-06-16 14:03:45 -04004137 len = sizeof(pinfo);
4138 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 retval = -EFAULT;
4140 goto out;
4141 }
4142
4143 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4144 pinfo.spinfo_assoc_id);
4145 if (!transport)
4146 return -EINVAL;
4147
4148 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004149 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 pinfo.spinfo_cwnd = transport->cwnd;
4151 pinfo.spinfo_srtt = transport->srtt;
4152 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004153 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Frank Filz3f7a87d2005-06-20 13:14:57 -07004155 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4156 pinfo.spinfo_state = SCTP_ACTIVE;
4157
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 if (put_user(len, optlen)) {
4159 retval = -EFAULT;
4160 goto out;
4161 }
4162
4163 if (copy_to_user(optval, &pinfo, len)) {
4164 retval = -EFAULT;
4165 goto out;
4166 }
4167
4168out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004169 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170}
4171
4172/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4173 *
4174 * This option is a on/off flag. If enabled no SCTP message
4175 * fragmentation will be performed. Instead if a message being sent
4176 * exceeds the current PMTU size, the message will NOT be sent and
4177 * instead a error will be indicated to the user.
4178 */
4179static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4180 char __user *optval, int __user *optlen)
4181{
4182 int val;
4183
4184 if (len < sizeof(int))
4185 return -EINVAL;
4186
4187 len = sizeof(int);
4188 val = (sctp_sk(sk)->disable_fragments == 1);
4189 if (put_user(len, optlen))
4190 return -EFAULT;
4191 if (copy_to_user(optval, &val, len))
4192 return -EFAULT;
4193 return 0;
4194}
4195
4196/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4197 *
4198 * This socket option is used to specify various notifications and
4199 * ancillary data the user wishes to receive.
4200 */
4201static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
4202 int __user *optlen)
4203{
Thomas Grafacdd5982012-04-03 22:17:53 +00004204 if (len <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00004206 if (len > sizeof(struct sctp_event_subscribe))
4207 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04004208 if (put_user(len, optlen))
4209 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4211 return -EFAULT;
4212 return 0;
4213}
4214
4215/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
4216 *
4217 * This socket option is applicable to the UDP-style socket only. When
4218 * set it will cause associations that are idle for more than the
4219 * specified number of seconds to automatically close. An association
4220 * being idle is defined an association that has NOT sent or received
4221 * user data. The special value of '0' indicates that no automatic
4222 * close of any associations should be performed. The option expects an
4223 * integer defining the number of seconds of idle time before an
4224 * association is closed.
4225 */
4226static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
4227{
4228 /* Applicable to UDP-style socket only */
4229 if (sctp_style(sk, TCP))
4230 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04004231 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004233 len = sizeof(int);
4234 if (put_user(len, optlen))
4235 return -EFAULT;
4236 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 return -EFAULT;
4238 return 0;
4239}
4240
4241/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00004242int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
Benjamin Poirier0343c552012-03-08 05:55:58 +00004244 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 struct socket *sock;
Vlad Yasevichd570ee42007-05-15 16:32:39 -04004246 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 int err = 0;
4248
Benjamin Poirier0343c552012-03-08 05:55:58 +00004249 if (!asoc)
4250 return -EINVAL;
4251
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 /* An association cannot be branched off from an already peeled-off
4253 * socket, nor is this supported for tcp style sockets.
4254 */
4255 if (!sctp_style(sk, UDP))
4256 return -EINVAL;
4257
4258 /* Create a new socket. */
4259 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
4260 if (err < 0)
4261 return err;
4262
Vlad Yasevich914e1c82009-02-13 08:33:44 +00004263 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08004264
4265 /* Make peeled-off sockets more like 1-1 accepted sockets.
4266 * Set the daddr and initialize id to something more random
4267 */
Vlad Yasevichd570ee42007-05-15 16:32:39 -04004268 af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
4269 af->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00004270
4271 /* Populate the fields of the newsk from the oldsk and migrate the
4272 * asoc to the newsk.
4273 */
4274 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08004275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 *sockp = sock;
4277
4278 return err;
4279}
Benjamin Poirier0343c552012-03-08 05:55:58 +00004280EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
4282static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
4283{
4284 sctp_peeloff_arg_t peeloff;
4285 struct socket *newsock;
4286 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
Neil Horman408f22e2007-06-16 14:03:45 -04004288 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004290 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 if (copy_from_user(&peeloff, optval, len))
4292 return -EFAULT;
4293
Benjamin Poirier0343c552012-03-08 05:55:58 +00004294 retval = sctp_do_peeloff(sk, peeloff.associd, &newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 if (retval < 0)
4296 goto out;
4297
4298 /* Map the socket to an unused fd that can be returned to the user. */
Ulrich Dreppera677a032008-07-23 21:29:17 -07004299 retval = sock_map_fd(newsock, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 if (retval < 0) {
4301 sock_release(newsock);
4302 goto out;
4303 }
4304
Benjamin Poirier0343c552012-03-08 05:55:58 +00004305 SCTP_DEBUG_PRINTK("%s: sk: %p newsk: %p sd: %d\n",
4306 __func__, sk, newsock->sk, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
4308 /* Return the fd mapped to the new socket. */
4309 peeloff.sd = retval;
Neil Horman408f22e2007-06-16 14:03:45 -04004310 if (put_user(len, optlen))
4311 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 if (copy_to_user(optval, &peeloff, len))
4313 retval = -EFAULT;
4314
4315out:
4316 return retval;
4317}
4318
4319/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
4320 *
4321 * Applications can enable or disable heartbeats for any peer address of
4322 * an association, modify an address's heartbeat interval, force a
4323 * heartbeat to be sent immediately, and adjust the address's maximum
4324 * number of retransmissions sent before an address is considered
4325 * unreachable. The following structure is used to access and modify an
4326 * address's parameters:
4327 *
4328 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08004329 * sctp_assoc_t spp_assoc_id;
4330 * struct sockaddr_storage spp_address;
4331 * uint32_t spp_hbinterval;
4332 * uint16_t spp_pathmaxrxt;
4333 * uint32_t spp_pathmtu;
4334 * uint32_t spp_sackdelay;
4335 * uint32_t spp_flags;
4336 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08004338 * spp_assoc_id - (one-to-many style socket) This is filled in the
4339 * application, and identifies the association for
4340 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 * spp_address - This specifies which address is of interest.
4342 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08004343 * in milliseconds. If a value of zero
4344 * is present in this field then no changes are to
4345 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 * spp_pathmaxrxt - This contains the maximum number of
4347 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08004348 * considered unreachable. If a value of zero
4349 * is present in this field then no changes are to
4350 * be made to this parameter.
4351 * spp_pathmtu - When Path MTU discovery is disabled the value
4352 * specified here will be the "fixed" path mtu.
4353 * Note that if the spp_address field is empty
4354 * then all associations on this address will
4355 * have this fixed path mtu set upon them.
4356 *
4357 * spp_sackdelay - When delayed sack is enabled, this value specifies
4358 * the number of milliseconds that sacks will be delayed
4359 * for. This value will apply to all addresses of an
4360 * association if the spp_address field is empty. Note
4361 * also, that if delayed sack is enabled and this
4362 * value is set to 0, no change is made to the last
4363 * recorded delayed sack timer value.
4364 *
4365 * spp_flags - These flags are used to control various features
4366 * on an association. The flag field may contain
4367 * zero or more of the following options.
4368 *
4369 * SPP_HB_ENABLE - Enable heartbeats on the
4370 * specified address. Note that if the address
4371 * field is empty all addresses for the association
4372 * have heartbeats enabled upon them.
4373 *
4374 * SPP_HB_DISABLE - Disable heartbeats on the
4375 * speicifed address. Note that if the address
4376 * field is empty all addresses for the association
4377 * will have their heartbeats disabled. Note also
4378 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
4379 * mutually exclusive, only one of these two should
4380 * be specified. Enabling both fields will have
4381 * undetermined results.
4382 *
4383 * SPP_HB_DEMAND - Request a user initiated heartbeat
4384 * to be made immediately.
4385 *
4386 * SPP_PMTUD_ENABLE - This field will enable PMTU
4387 * discovery upon the specified address. Note that
4388 * if the address feild is empty then all addresses
4389 * on the association are effected.
4390 *
4391 * SPP_PMTUD_DISABLE - This field will disable PMTU
4392 * discovery upon the specified address. Note that
4393 * if the address feild is empty then all addresses
4394 * on the association are effected. Not also that
4395 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
4396 * exclusive. Enabling both will have undetermined
4397 * results.
4398 *
4399 * SPP_SACKDELAY_ENABLE - Setting this flag turns
4400 * on delayed sack. The time specified in spp_sackdelay
4401 * is used to specify the sack delay for this address. Note
4402 * that if spp_address is empty then all addresses will
4403 * enable delayed sack and take on the sack delay
4404 * value specified in spp_sackdelay.
4405 * SPP_SACKDELAY_DISABLE - Setting this flag turns
4406 * off delayed sack. If the spp_address field is blank then
4407 * delayed sack is disabled for the entire association. Note
4408 * also that this field is mutually exclusive to
4409 * SPP_SACKDELAY_ENABLE, setting both will have undefined
4410 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 */
4412static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08004413 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414{
Frank Filz52ccb8e2005-12-22 11:36:46 -08004415 struct sctp_paddrparams params;
4416 struct sctp_transport *trans = NULL;
4417 struct sctp_association *asoc = NULL;
4418 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Neil Horman408f22e2007-06-16 14:03:45 -04004420 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004422 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 if (copy_from_user(&params, optval, len))
4424 return -EFAULT;
4425
Frank Filz52ccb8e2005-12-22 11:36:46 -08004426 /* If an address other than INADDR_ANY is specified, and
4427 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 */
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04004429 if (!sctp_is_any(sk, ( union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08004430 trans = sctp_addr_id2transport(sk, &params.spp_address,
4431 params.spp_assoc_id);
4432 if (!trans) {
4433 SCTP_DEBUG_PRINTK("Failed no transport\n");
4434 return -EINVAL;
4435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 }
4437
Frank Filz52ccb8e2005-12-22 11:36:46 -08004438 /* Get association, if assoc_id != 0 and the socket is a one
4439 * to many style socket, and an association was not found, then
4440 * the id was invalid.
4441 */
4442 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
4443 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
4444 SCTP_DEBUG_PRINTK("Failed no association\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Frank Filz52ccb8e2005-12-22 11:36:46 -08004448 if (trans) {
4449 /* Fetch transport values. */
4450 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
4451 params.spp_pathmtu = trans->pathmtu;
4452 params.spp_pathmaxrxt = trans->pathmaxrxt;
4453 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Frank Filz52ccb8e2005-12-22 11:36:46 -08004455 /*draft-11 doesn't say what to return in spp_flags*/
4456 params.spp_flags = trans->param_flags;
4457 } else if (asoc) {
4458 /* Fetch association values. */
4459 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
4460 params.spp_pathmtu = asoc->pathmtu;
4461 params.spp_pathmaxrxt = asoc->pathmaxrxt;
4462 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
Frank Filz52ccb8e2005-12-22 11:36:46 -08004464 /*draft-11 doesn't say what to return in spp_flags*/
4465 params.spp_flags = asoc->param_flags;
4466 } else {
4467 /* Fetch socket values. */
4468 params.spp_hbinterval = sp->hbinterval;
4469 params.spp_pathmtu = sp->pathmtu;
4470 params.spp_sackdelay = sp->sackdelay;
4471 params.spp_pathmaxrxt = sp->pathmaxrxt;
4472
4473 /*draft-11 doesn't say what to return in spp_flags*/
4474 params.spp_flags = sp->param_flags;
4475 }
4476
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 if (copy_to_user(optval, &params, len))
4478 return -EFAULT;
4479
4480 if (put_user(len, optlen))
4481 return -EFAULT;
4482
4483 return 0;
4484}
4485
Wei Yongjund364d922008-05-09 15:13:26 -07004486/*
4487 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08004488 *
Wei Yongjund364d922008-05-09 15:13:26 -07004489 * This option will effect the way delayed acks are performed. This
4490 * option allows you to get or set the delayed ack time, in
4491 * milliseconds. It also allows changing the delayed ack frequency.
4492 * Changing the frequency to 1 disables the delayed sack algorithm. If
4493 * the assoc_id is 0, then this sets or gets the endpoints default
4494 * values. If the assoc_id field is non-zero, then the set or get
4495 * effects the specified association for the one to many model (the
4496 * assoc_id field is ignored by the one to one model). Note that if
4497 * sack_delay or sack_freq are 0 when setting this option, then the
4498 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08004499 *
Wei Yongjund364d922008-05-09 15:13:26 -07004500 * struct sctp_sack_info {
4501 * sctp_assoc_t sack_assoc_id;
4502 * uint32_t sack_delay;
4503 * uint32_t sack_freq;
4504 * };
Frank Filz77086102005-12-22 11:37:30 -08004505 *
Wei Yongjund364d922008-05-09 15:13:26 -07004506 * sack_assoc_id - This parameter, indicates which association the user
4507 * is performing an action upon. Note that if this field's value is
4508 * zero then the endpoints default value is changed (effecting future
4509 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08004510 *
Wei Yongjund364d922008-05-09 15:13:26 -07004511 * sack_delay - This parameter contains the number of milliseconds that
4512 * the user is requesting the delayed ACK timer be set to. Note that
4513 * this value is defined in the standard to be between 200 and 500
4514 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08004515 *
Wei Yongjund364d922008-05-09 15:13:26 -07004516 * sack_freq - This parameter contains the number of packets that must
4517 * be received before a sack is sent without waiting for the delay
4518 * timer to expire. The default value for this is 2, setting this
4519 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08004520 */
Wei Yongjund364d922008-05-09 15:13:26 -07004521static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08004522 char __user *optval,
4523 int __user *optlen)
4524{
Wei Yongjund364d922008-05-09 15:13:26 -07004525 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08004526 struct sctp_association *asoc = NULL;
4527 struct sctp_sock *sp = sctp_sk(sk);
4528
Wei Yongjund364d922008-05-09 15:13:26 -07004529 if (len >= sizeof(struct sctp_sack_info)) {
4530 len = sizeof(struct sctp_sack_info);
4531
4532 if (copy_from_user(&params, optval, len))
4533 return -EFAULT;
4534 } else if (len == sizeof(struct sctp_assoc_value)) {
Joe Perches145ce502010-08-24 13:21:08 +00004535 pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
4536 pr_warn("Use struct sctp_sack_info instead\n");
Wei Yongjund364d922008-05-09 15:13:26 -07004537 if (copy_from_user(&params, optval, len))
4538 return -EFAULT;
4539 } else
Frank Filz77086102005-12-22 11:37:30 -08004540 return - EINVAL;
4541
Wei Yongjund364d922008-05-09 15:13:26 -07004542 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08004543 * to many style socket, and an association was not found, then
4544 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004545 */
Wei Yongjund364d922008-05-09 15:13:26 -07004546 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
4547 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08004548 return -EINVAL;
4549
4550 if (asoc) {
4551 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07004552 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
4553 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08004554 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07004555 params.sack_freq = asoc->sackfreq;
4556
4557 } else {
4558 params.sack_delay = 0;
4559 params.sack_freq = 1;
4560 }
Frank Filz77086102005-12-22 11:37:30 -08004561 } else {
4562 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07004563 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
4564 params.sack_delay = sp->sackdelay;
4565 params.sack_freq = sp->sackfreq;
4566 } else {
4567 params.sack_delay = 0;
4568 params.sack_freq = 1;
4569 }
Frank Filz77086102005-12-22 11:37:30 -08004570 }
4571
4572 if (copy_to_user(optval, &params, len))
4573 return -EFAULT;
4574
4575 if (put_user(len, optlen))
4576 return -EFAULT;
4577
4578 return 0;
4579}
4580
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
4582 *
4583 * Applications can specify protocol parameters for the default association
4584 * initialization. The option name argument to setsockopt() and getsockopt()
4585 * is SCTP_INITMSG.
4586 *
4587 * Setting initialization parameters is effective only on an unconnected
4588 * socket (for UDP-style sockets only future associations are effected
4589 * by the change). With TCP-style sockets, this option is inherited by
4590 * sockets derived from a listener socket.
4591 */
4592static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
4593{
Neil Horman408f22e2007-06-16 14:03:45 -04004594 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004596 len = sizeof(struct sctp_initmsg);
4597 if (put_user(len, optlen))
4598 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
4600 return -EFAULT;
4601 return 0;
4602}
4603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004605static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
4606 char __user *optval, int __user *optlen)
4607{
4608 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004609 int cnt = 0;
4610 struct sctp_getaddrs getaddrs;
4611 struct sctp_transport *from;
4612 void __user *to;
4613 union sctp_addr temp;
4614 struct sctp_sock *sp = sctp_sk(sk);
4615 int addrlen;
4616 size_t space_left;
4617 int bytes_copied;
4618
4619 if (len < sizeof(struct sctp_getaddrs))
4620 return -EINVAL;
4621
4622 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4623 return -EFAULT;
4624
4625 /* For UDP-style sockets, id specifies the association to query. */
4626 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4627 if (!asoc)
4628 return -EINVAL;
4629
4630 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004631 space_left = len - offsetof(struct sctp_getaddrs,addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004632
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07004633 list_for_each_entry(from, &asoc->peer.transport_addr_list,
4634 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08004635 memcpy(&temp, &from->ipaddr, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004636 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
Vlad Yasevicha5f4cea2010-04-30 21:42:42 -04004637 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004638 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004639 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004640 if (copy_to_user(to, &temp, addrlen))
4641 return -EFAULT;
4642 to += addrlen;
4643 cnt++;
4644 space_left -= addrlen;
4645 }
4646
4647 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
4648 return -EFAULT;
4649 bytes_copied = ((char __user *)to) - optval;
4650 if (put_user(bytes_copied, optlen))
4651 return -EFAULT;
4652
4653 return 0;
4654}
4655
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004656static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
4657 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004658{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004659 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004660 union sctp_addr temp;
4661 int cnt = 0;
4662 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00004663 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004664
Vlad Yasevich29303542007-09-16 16:02:12 -07004665 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00004666 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07004667 if (!addr->valid)
4668 continue;
4669
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004670 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08004671 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004672 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07004673 if ((PF_INET6 == sk->sk_family) &&
4674 inet_v6_ipv6only(sk) &&
4675 (AF_INET == addr->a.sa.sa_family))
4676 continue;
Al Viro6244be42006-11-20 17:21:44 -08004677 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05004678 if (!temp.v4.sin_port)
4679 temp.v4.sin_port = htons(port);
4680
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004681 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
4682 &temp);
4683 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevich29303542007-09-16 16:02:12 -07004684 if (space_left < addrlen) {
4685 cnt = -ENOMEM;
4686 break;
4687 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004688 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08004689
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004690 to += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004691 cnt ++;
4692 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04004693 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004694 }
Vlad Yasevich29303542007-09-16 16:02:12 -07004695 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004696
4697 return cnt;
4698}
4699
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004701static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
4702 char __user *optval, int __user *optlen)
4703{
4704 struct sctp_bind_addr *bp;
4705 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004706 int cnt = 0;
4707 struct sctp_getaddrs getaddrs;
4708 struct sctp_sockaddr_entry *addr;
4709 void __user *to;
4710 union sctp_addr temp;
4711 struct sctp_sock *sp = sctp_sk(sk);
4712 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004713 int err = 0;
4714 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004715 int bytes_copied = 0;
4716 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004717 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004718
Neil Horman408f22e2007-06-16 14:03:45 -04004719 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004720 return -EINVAL;
4721
4722 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
4723 return -EFAULT;
4724
4725 /*
4726 * For UDP-style sockets, id specifies the association to query.
4727 * If the id field is set to the value '0' then the locally bound
4728 * addresses are returned without regard to any particular
4729 * association.
4730 */
4731 if (0 == getaddrs.assoc_id) {
4732 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004733 } else {
4734 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
4735 if (!asoc)
4736 return -EINVAL;
4737 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004738 }
4739
4740 to = optval + offsetof(struct sctp_getaddrs,addrs);
Neil Horman186e2342007-06-18 19:59:16 -04004741 space_left = len - offsetof(struct sctp_getaddrs,addrs);
4742
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004743 addrs = kmalloc(space_left, GFP_KERNEL);
4744 if (!addrs)
4745 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004746
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004747 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
4748 * addresses from the global local address list.
4749 */
4750 if (sctp_list_single_entry(&bp->address_list)) {
4751 addr = list_entry(bp->address_list.next,
4752 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04004753 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004754 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
4755 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004756 if (cnt < 0) {
4757 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07004758 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004759 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004760 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004761 }
4762 }
4763
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004764 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07004765 /* Protection on the bound address list is not needed since
4766 * in the socket option context we hold a socket lock and
4767 * thus the bound address list can't change.
4768 */
4769 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08004770 memcpy(&temp, &addr->a, sizeof(temp));
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004771 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
4772 addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004773 if (space_left < addrlen) {
4774 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07004775 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004776 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07004777 memcpy(buf, &temp, addrlen);
4778 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004779 bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004780 cnt ++;
4781 space_left -= addrlen;
4782 }
4783
4784copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004785 if (copy_to_user(to, addrs, bytes_copied)) {
4786 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004787 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004788 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004789 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
4790 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004791 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004792 }
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004793 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04004794 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02004795out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07004796 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07004797 return err;
4798}
4799
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
4801 *
4802 * Requests that the local SCTP stack use the enclosed peer address as
4803 * the association primary. The enclosed address must be one of the
4804 * association peer's addresses.
4805 */
4806static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
4807 char __user *optval, int __user *optlen)
4808{
4809 struct sctp_prim prim;
4810 struct sctp_association *asoc;
4811 struct sctp_sock *sp = sctp_sk(sk);
4812
Neil Horman408f22e2007-06-16 14:03:45 -04004813 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 return -EINVAL;
4815
Neil Horman408f22e2007-06-16 14:03:45 -04004816 len = sizeof(struct sctp_prim);
4817
4818 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 return -EFAULT;
4820
4821 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
4822 if (!asoc)
4823 return -EINVAL;
4824
4825 if (!asoc->peer.primary_path)
4826 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004827
Al Viro8cec6b82006-11-20 17:23:01 -08004828 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
4829 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
4831 sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
4832 (union sctp_addr *)&prim.ssp_addr);
4833
Neil Horman408f22e2007-06-16 14:03:45 -04004834 if (put_user(len, optlen))
4835 return -EFAULT;
4836 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 return -EFAULT;
4838
4839 return 0;
4840}
4841
4842/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004843 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004845 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 * Indication parameter for all future INIT and INIT-ACK exchanges.
4847 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004848static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 char __user *optval, int __user *optlen)
4850{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004851 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
Neil Horman408f22e2007-06-16 14:03:45 -04004853 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 return -EINVAL;
4855
Neil Horman408f22e2007-06-16 14:03:45 -04004856 len = sizeof(struct sctp_setadaptation);
4857
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004858 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04004859
4860 if (put_user(len, optlen))
4861 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004862 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07004864
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 return 0;
4866}
4867
4868/*
4869 *
4870 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
4871 *
4872 * Applications that wish to use the sendto() system call may wish to
4873 * specify a default set of parameters that would normally be supplied
4874 * through the inclusion of ancillary data. This socket option allows
4875 * such an application to set the default sctp_sndrcvinfo structure.
4876
4877
4878 * The application that wishes to use this socket option simply passes
4879 * in to this call the sctp_sndrcvinfo structure defined in Section
4880 * 5.2.2) The input parameters accepted by this call include
4881 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
4882 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
4883 * to this call if the caller is using the UDP model.
4884 *
4885 * For getsockopt, it get the default sctp_sndrcvinfo structure.
4886 */
4887static int sctp_getsockopt_default_send_param(struct sock *sk,
4888 int len, char __user *optval,
4889 int __user *optlen)
4890{
4891 struct sctp_sndrcvinfo info;
4892 struct sctp_association *asoc;
4893 struct sctp_sock *sp = sctp_sk(sk);
4894
Neil Horman408f22e2007-06-16 14:03:45 -04004895 if (len < sizeof(struct sctp_sndrcvinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004897
4898 len = sizeof(struct sctp_sndrcvinfo);
4899
4900 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 return -EFAULT;
4902
4903 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
4904 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
4905 return -EINVAL;
4906
4907 if (asoc) {
4908 info.sinfo_stream = asoc->default_stream;
4909 info.sinfo_flags = asoc->default_flags;
4910 info.sinfo_ppid = asoc->default_ppid;
4911 info.sinfo_context = asoc->default_context;
4912 info.sinfo_timetolive = asoc->default_timetolive;
4913 } else {
4914 info.sinfo_stream = sp->default_stream;
4915 info.sinfo_flags = sp->default_flags;
4916 info.sinfo_ppid = sp->default_ppid;
4917 info.sinfo_context = sp->default_context;
4918 info.sinfo_timetolive = sp->default_timetolive;
4919 }
4920
Neil Horman408f22e2007-06-16 14:03:45 -04004921 if (put_user(len, optlen))
4922 return -EFAULT;
4923 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 return -EFAULT;
4925
4926 return 0;
4927}
4928
4929/*
4930 *
4931 * 7.1.5 SCTP_NODELAY
4932 *
4933 * Turn on/off any Nagle-like algorithm. This means that packets are
4934 * generally sent as soon as possible and no unnecessary delays are
4935 * introduced, at the cost of more packets in the network. Expects an
4936 * integer boolean flag.
4937 */
4938
4939static int sctp_getsockopt_nodelay(struct sock *sk, int len,
4940 char __user *optval, int __user *optlen)
4941{
4942 int val;
4943
4944 if (len < sizeof(int))
4945 return -EINVAL;
4946
4947 len = sizeof(int);
4948 val = (sctp_sk(sk)->nodelay == 1);
4949 if (put_user(len, optlen))
4950 return -EFAULT;
4951 if (copy_to_user(optval, &val, len))
4952 return -EFAULT;
4953 return 0;
4954}
4955
4956/*
4957 *
4958 * 7.1.1 SCTP_RTOINFO
4959 *
4960 * The protocol parameters used to initialize and bound retransmission
4961 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
4962 * and modify these parameters.
4963 * All parameters are time values, in milliseconds. A value of 0, when
4964 * modifying the parameters, indicates that the current value should not
4965 * be changed.
4966 *
4967 */
4968static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
4969 char __user *optval,
4970 int __user *optlen) {
4971 struct sctp_rtoinfo rtoinfo;
4972 struct sctp_association *asoc;
4973
Neil Horman408f22e2007-06-16 14:03:45 -04004974 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 return -EINVAL;
4976
Neil Horman408f22e2007-06-16 14:03:45 -04004977 len = sizeof(struct sctp_rtoinfo);
4978
4979 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 return -EFAULT;
4981
4982 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
4983
4984 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
4985 return -EINVAL;
4986
4987 /* Values corresponding to the specific association. */
4988 if (asoc) {
4989 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
4990 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
4991 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
4992 } else {
4993 /* Values corresponding to the endpoint. */
4994 struct sctp_sock *sp = sctp_sk(sk);
4995
4996 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
4997 rtoinfo.srto_max = sp->rtoinfo.srto_max;
4998 rtoinfo.srto_min = sp->rtoinfo.srto_min;
4999 }
5000
5001 if (put_user(len, optlen))
5002 return -EFAULT;
5003
5004 if (copy_to_user(optval, &rtoinfo, len))
5005 return -EFAULT;
5006
5007 return 0;
5008}
5009
5010/*
5011 *
5012 * 7.1.2 SCTP_ASSOCINFO
5013 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02005014 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 * of the association.
5016 * Returns an error if the new association retransmission value is
5017 * greater than the sum of the retransmission value of the peer.
5018 * See [SCTP] for more information.
5019 *
5020 */
5021static int sctp_getsockopt_associnfo(struct sock *sk, int len,
5022 char __user *optval,
5023 int __user *optlen)
5024{
5025
5026 struct sctp_assocparams assocparams;
5027 struct sctp_association *asoc;
5028 struct list_head *pos;
5029 int cnt = 0;
5030
Neil Horman408f22e2007-06-16 14:03:45 -04005031 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 return -EINVAL;
5033
Neil Horman408f22e2007-06-16 14:03:45 -04005034 len = sizeof(struct sctp_assocparams);
5035
5036 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 return -EFAULT;
5038
5039 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
5040
5041 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
5042 return -EINVAL;
5043
5044 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07005045 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
5047 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
5048 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
5049 assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
5050 * 1000) +
5051 (asoc->cookie_life.tv_usec
5052 / 1000);
5053
5054 list_for_each(pos, &asoc->peer.transport_addr_list) {
5055 cnt ++;
5056 }
5057
5058 assocparams.sasoc_number_peer_destinations = cnt;
5059 } else {
5060 /* Values corresponding to the endpoint */
5061 struct sctp_sock *sp = sctp_sk(sk);
5062
5063 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
5064 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
5065 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
5066 assocparams.sasoc_cookie_life =
5067 sp->assocparams.sasoc_cookie_life;
5068 assocparams.sasoc_number_peer_destinations =
5069 sp->assocparams.
5070 sasoc_number_peer_destinations;
5071 }
5072
5073 if (put_user(len, optlen))
5074 return -EFAULT;
5075
5076 if (copy_to_user(optval, &assocparams, len))
5077 return -EFAULT;
5078
5079 return 0;
5080}
5081
5082/*
5083 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
5084 *
5085 * This socket option is a boolean flag which turns on or off mapped V4
5086 * addresses. If this option is turned on and the socket is type
5087 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
5088 * If this option is turned off, then no mapping will be done of V4
5089 * addresses and a user will receive both PF_INET6 and PF_INET type
5090 * addresses on the socket.
5091 */
5092static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
5093 char __user *optval, int __user *optlen)
5094{
5095 int val;
5096 struct sctp_sock *sp = sctp_sk(sk);
5097
5098 if (len < sizeof(int))
5099 return -EINVAL;
5100
5101 len = sizeof(int);
5102 val = sp->v4mapped;
5103 if (put_user(len, optlen))
5104 return -EFAULT;
5105 if (copy_to_user(optval, &val, len))
5106 return -EFAULT;
5107
5108 return 0;
5109}
5110
5111/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005112 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
5113 * (chapter and verse is quoted at sctp_setsockopt_context())
5114 */
5115static int sctp_getsockopt_context(struct sock *sk, int len,
5116 char __user *optval, int __user *optlen)
5117{
5118 struct sctp_assoc_value params;
5119 struct sctp_sock *sp;
5120 struct sctp_association *asoc;
5121
Neil Horman408f22e2007-06-16 14:03:45 -04005122 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005123 return -EINVAL;
5124
Neil Horman408f22e2007-06-16 14:03:45 -04005125 len = sizeof(struct sctp_assoc_value);
5126
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005127 if (copy_from_user(&params, optval, len))
5128 return -EFAULT;
5129
5130 sp = sctp_sk(sk);
5131
5132 if (params.assoc_id != 0) {
5133 asoc = sctp_id2assoc(sk, params.assoc_id);
5134 if (!asoc)
5135 return -EINVAL;
5136 params.assoc_value = asoc->default_rcv_context;
5137 } else {
5138 params.assoc_value = sp->default_rcv_context;
5139 }
5140
5141 if (put_user(len, optlen))
5142 return -EFAULT;
5143 if (copy_to_user(optval, &params, len))
5144 return -EFAULT;
5145
5146 return 0;
5147}
5148
5149/*
Wei Yongjune89c2092008-12-25 16:54:58 -08005150 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
5151 * This option will get or set the maximum size to put in any outgoing
5152 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 * fragmented by SCTP into the specified size. Note that the underlying
5154 * SCTP implementation may fragment into smaller sized chunks when the
5155 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08005156 * the user. The default value for this option is '0' which indicates
5157 * the user is NOT limiting fragmentation and only the PMTU will effect
5158 * SCTP's choice of DATA chunk size. Note also that values set larger
5159 * than the maximum size of an IP datagram will effectively let SCTP
5160 * control fragmentation (i.e. the same as setting this option to 0).
5161 *
5162 * The following structure is used to access and modify this parameter:
5163 *
5164 * struct sctp_assoc_value {
5165 * sctp_assoc_t assoc_id;
5166 * uint32_t assoc_value;
5167 * };
5168 *
5169 * assoc_id: This parameter is ignored for one-to-one style sockets.
5170 * For one-to-many style sockets this parameter indicates which
5171 * association the user is performing an action upon. Note that if
5172 * this field's value is zero then the endpoints default value is
5173 * changed (effecting future associations only).
5174 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 */
5176static int sctp_getsockopt_maxseg(struct sock *sk, int len,
5177 char __user *optval, int __user *optlen)
5178{
Wei Yongjune89c2092008-12-25 16:54:58 -08005179 struct sctp_assoc_value params;
5180 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181
Wei Yongjune89c2092008-12-25 16:54:58 -08005182 if (len == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00005183 pr_warn("Use of int in maxseg socket option deprecated\n");
5184 pr_warn("Use struct sctp_assoc_value instead\n");
Wei Yongjune89c2092008-12-25 16:54:58 -08005185 params.assoc_id = 0;
5186 } else if (len >= sizeof(struct sctp_assoc_value)) {
5187 len = sizeof(struct sctp_assoc_value);
5188 if (copy_from_user(&params, optval, sizeof(params)))
5189 return -EFAULT;
5190 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 return -EINVAL;
5192
Wei Yongjune89c2092008-12-25 16:54:58 -08005193 asoc = sctp_id2assoc(sk, params.assoc_id);
5194 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
5195 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196
Wei Yongjune89c2092008-12-25 16:54:58 -08005197 if (asoc)
5198 params.assoc_value = asoc->frag_point;
5199 else
5200 params.assoc_value = sctp_sk(sk)->user_frag;
5201
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 if (put_user(len, optlen))
5203 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08005204 if (len == sizeof(int)) {
5205 if (copy_to_user(optval, &params.assoc_value, len))
5206 return -EFAULT;
5207 } else {
5208 if (copy_to_user(optval, &params, len))
5209 return -EFAULT;
5210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211
5212 return 0;
5213}
5214
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005215/*
5216 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
5217 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
5218 */
5219static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
5220 char __user *optval, int __user *optlen)
5221{
5222 int val;
5223
5224 if (len < sizeof(int))
5225 return -EINVAL;
5226
5227 len = sizeof(int);
5228
5229 val = sctp_sk(sk)->frag_interleave;
5230 if (put_user(len, optlen))
5231 return -EFAULT;
5232 if (copy_to_user(optval, &val, len))
5233 return -EFAULT;
5234
5235 return 0;
5236}
5237
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005238/*
5239 * 7.1.25. Set or Get the sctp partial delivery point
5240 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
5241 */
5242static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
5243 char __user *optval,
5244 int __user *optlen)
5245{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09005246 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005247
5248 if (len < sizeof(u32))
5249 return -EINVAL;
5250
5251 len = sizeof(u32);
5252
5253 val = sctp_sk(sk)->pd_point;
5254 if (put_user(len, optlen))
5255 return -EFAULT;
5256 if (copy_to_user(optval, &val, len))
5257 return -EFAULT;
5258
Wei Yongjun7d743b72010-12-14 16:10:41 +00005259 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005260}
5261
Vlad Yasevich70331572007-03-23 11:34:36 -07005262/*
5263 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
5264 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
5265 */
5266static int sctp_getsockopt_maxburst(struct sock *sk, int len,
5267 char __user *optval,
5268 int __user *optlen)
5269{
Neil Horman219b99a2008-03-05 13:44:46 -08005270 struct sctp_assoc_value params;
5271 struct sctp_sock *sp;
5272 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07005273
Neil Horman219b99a2008-03-05 13:44:46 -08005274 if (len == sizeof(int)) {
Joe Perches145ce502010-08-24 13:21:08 +00005275 pr_warn("Use of int in max_burst socket option deprecated\n");
5276 pr_warn("Use struct sctp_assoc_value instead\n");
Neil Horman219b99a2008-03-05 13:44:46 -08005277 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00005278 } else if (len >= sizeof(struct sctp_assoc_value)) {
5279 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08005280 if (copy_from_user(&params, optval, len))
5281 return -EFAULT;
5282 } else
5283 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07005284
Neil Horman219b99a2008-03-05 13:44:46 -08005285 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07005286
Neil Horman219b99a2008-03-05 13:44:46 -08005287 if (params.assoc_id != 0) {
5288 asoc = sctp_id2assoc(sk, params.assoc_id);
5289 if (!asoc)
5290 return -EINVAL;
5291 params.assoc_value = asoc->max_burst;
5292 } else
5293 params.assoc_value = sp->max_burst;
5294
5295 if (len == sizeof(int)) {
5296 if (copy_to_user(optval, &params.assoc_value, len))
5297 return -EFAULT;
5298 } else {
5299 if (copy_to_user(optval, &params, len))
5300 return -EFAULT;
5301 }
5302
5303 return 0;
5304
Vlad Yasevich70331572007-03-23 11:34:36 -07005305}
5306
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005307static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
5308 char __user *optval, int __user *optlen)
5309{
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005310 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005311 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005312 __u16 data_len = 0;
5313 u32 num_idents;
5314
5315 if (!sctp_auth_enable)
5316 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005317
5318 hmacs = sctp_sk(sk)->ep->auth_hmacs_list;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005319 data_len = ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005320
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005321 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005322 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005323
5324 len = sizeof(struct sctp_hmacalgo) + data_len;
5325 num_idents = data_len / sizeof(u16);
5326
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005327 if (put_user(len, optlen))
5328 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005329 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005330 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005331 if (copy_to_user(p->shmac_idents, hmacs->hmac_ids, data_len))
5332 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005333 return 0;
5334}
5335
5336static int sctp_getsockopt_active_key(struct sock *sk, int len,
5337 char __user *optval, int __user *optlen)
5338{
5339 struct sctp_authkeyid val;
5340 struct sctp_association *asoc;
5341
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005342 if (!sctp_auth_enable)
5343 return -EACCES;
5344
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005345 if (len < sizeof(struct sctp_authkeyid))
5346 return -EINVAL;
5347 if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid)))
5348 return -EFAULT;
5349
5350 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
5351 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
5352 return -EINVAL;
5353
5354 if (asoc)
5355 val.scact_keynumber = asoc->active_key_id;
5356 else
5357 val.scact_keynumber = sctp_sk(sk)->ep->active_key_id;
5358
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005359 len = sizeof(struct sctp_authkeyid);
5360 if (put_user(len, optlen))
5361 return -EFAULT;
5362 if (copy_to_user(optval, &val, len))
5363 return -EFAULT;
5364
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005365 return 0;
5366}
5367
5368static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
5369 char __user *optval, int __user *optlen)
5370{
Al Viro411223c2007-10-14 19:21:20 +01005371 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005372 struct sctp_authchunks val;
5373 struct sctp_association *asoc;
5374 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005375 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005376 char __user *to;
5377
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005378 if (!sctp_auth_enable)
5379 return -EACCES;
5380
5381 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005382 return -EINVAL;
5383
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005384 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005385 return -EFAULT;
5386
Al Viro411223c2007-10-14 19:21:20 +01005387 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005388 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5389 if (!asoc)
5390 return -EINVAL;
5391
5392 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005393 if (!ch)
5394 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005395
5396 /* See if the user provided enough room for all the data */
Vlad Yasevichb40db682008-02-27 14:40:37 -05005397 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
5398 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005399 return -EINVAL;
5400
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005401 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005402 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005403num:
5404 len = sizeof(struct sctp_authchunks) + num_chunks;
5405 if (put_user(len, optlen)) return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05005406 if (put_user(num_chunks, &p->gauth_number_of_chunks))
5407 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005408 return 0;
5409}
5410
5411static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
5412 char __user *optval, int __user *optlen)
5413{
Al Viro411223c2007-10-14 19:21:20 +01005414 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005415 struct sctp_authchunks val;
5416 struct sctp_association *asoc;
5417 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005418 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005419 char __user *to;
5420
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005421 if (!sctp_auth_enable)
5422 return -EACCES;
5423
5424 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005425 return -EINVAL;
5426
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005427 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005428 return -EFAULT;
5429
Al Viro411223c2007-10-14 19:21:20 +01005430 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005431 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5432 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
5433 return -EINVAL;
5434
5435 if (asoc)
5436 ch = (struct sctp_chunks_param*)asoc->c.auth_chunks;
5437 else
5438 ch = sctp_sk(sk)->ep->auth_chunk_list;
5439
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005440 if (!ch)
5441 goto num;
5442
Vlad Yasevichb40db682008-02-27 14:40:37 -05005443 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005444 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005445 return -EINVAL;
5446
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005447 if (copy_to_user(to, ch->chunks, num_chunks))
5448 return -EFAULT;
5449num:
5450 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005451 if (put_user(len, optlen))
5452 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05005453 if (put_user(num_chunks, &p->gauth_number_of_chunks))
5454 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005455
5456 return 0;
5457}
5458
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08005459/*
5460 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
5461 * This option gets the current number of associations that are attached
5462 * to a one-to-many style socket. The option value is an uint32_t.
5463 */
5464static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
5465 char __user *optval, int __user *optlen)
5466{
5467 struct sctp_sock *sp = sctp_sk(sk);
5468 struct sctp_association *asoc;
5469 u32 val = 0;
5470
5471 if (sctp_style(sk, TCP))
5472 return -EOPNOTSUPP;
5473
5474 if (len < sizeof(u32))
5475 return -EINVAL;
5476
5477 len = sizeof(u32);
5478
5479 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5480 val++;
5481 }
5482
5483 if (put_user(len, optlen))
5484 return -EFAULT;
5485 if (copy_to_user(optval, &val, len))
5486 return -EFAULT;
5487
5488 return 0;
5489}
5490
Wei Yongjun209ba422011-04-17 17:27:08 +00005491/*
Michio Honda7dc04d72011-04-26 20:16:31 +09005492 * 8.1.23 SCTP_AUTO_ASCONF
5493 * See the corresponding setsockopt entry as description
5494 */
5495static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
5496 char __user *optval, int __user *optlen)
5497{
5498 int val = 0;
5499
5500 if (len < sizeof(int))
5501 return -EINVAL;
5502
5503 len = sizeof(int);
5504 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
5505 val = 1;
5506 if (put_user(len, optlen))
5507 return -EFAULT;
5508 if (copy_to_user(optval, &val, len))
5509 return -EFAULT;
5510 return 0;
5511}
5512
5513/*
Wei Yongjun209ba422011-04-17 17:27:08 +00005514 * 8.2.6. Get the Current Identifiers of Associations
5515 * (SCTP_GET_ASSOC_ID_LIST)
5516 *
5517 * This option gets the current list of SCTP association identifiers of
5518 * the SCTP associations handled by a one-to-many style socket.
5519 */
5520static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
5521 char __user *optval, int __user *optlen)
5522{
5523 struct sctp_sock *sp = sctp_sk(sk);
5524 struct sctp_association *asoc;
5525 struct sctp_assoc_ids *ids;
5526 u32 num = 0;
5527
5528 if (sctp_style(sk, TCP))
5529 return -EOPNOTSUPP;
5530
5531 if (len < sizeof(struct sctp_assoc_ids))
5532 return -EINVAL;
5533
5534 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5535 num++;
5536 }
5537
5538 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
5539 return -EINVAL;
5540
5541 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
5542
5543 ids = kmalloc(len, GFP_KERNEL);
5544 if (unlikely(!ids))
5545 return -ENOMEM;
5546
5547 ids->gaids_number_of_ids = num;
5548 num = 0;
5549 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
5550 ids->gaids_assoc_id[num++] = asoc->assoc_id;
5551 }
5552
5553 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
5554 kfree(ids);
5555 return -EFAULT;
5556 }
5557
5558 kfree(ids);
5559 return 0;
5560}
5561
Neil Horman5aa93bc2012-07-21 07:56:07 +00005562/*
5563 * SCTP_PEER_ADDR_THLDS
5564 *
5565 * This option allows us to fetch the partially failed threshold for one or all
5566 * transports in an association. See Section 6.1 of:
5567 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
5568 */
5569static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
5570 char __user *optval,
5571 int len,
5572 int __user *optlen)
5573{
5574 struct sctp_paddrthlds val;
5575 struct sctp_transport *trans;
5576 struct sctp_association *asoc;
5577
5578 if (len < sizeof(struct sctp_paddrthlds))
5579 return -EINVAL;
5580 len = sizeof(struct sctp_paddrthlds);
5581 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
5582 return -EFAULT;
5583
5584 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
5585 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
5586 if (!asoc)
5587 return -ENOENT;
5588
5589 val.spt_pathpfthld = asoc->pf_retrans;
5590 val.spt_pathmaxrxt = asoc->pathmaxrxt;
5591 } else {
5592 trans = sctp_addr_id2transport(sk, &val.spt_address,
5593 val.spt_assoc_id);
5594 if (!trans)
5595 return -ENOENT;
5596
5597 val.spt_pathmaxrxt = trans->pathmaxrxt;
5598 val.spt_pathpfthld = trans->pf_retrans;
5599 }
5600
5601 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
5602 return -EFAULT;
5603
5604 return 0;
5605}
5606
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
5608 char __user *optval, int __user *optlen)
5609{
5610 int retval = 0;
5611 int len;
5612
Frank Filz3f7a87d2005-06-20 13:14:57 -07005613 SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
5614 sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
5616 /* I can hardly begin to describe how wrong this is. This is
5617 * so broken as to be worse than useless. The API draft
5618 * REALLY is NOT helpful here... I am not convinced that the
5619 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
5620 * are at all well-founded.
5621 */
5622 if (level != SOL_SCTP) {
5623 struct sctp_af *af = sctp_sk(sk)->pf->af;
5624
5625 retval = af->getsockopt(sk, level, optname, optval, optlen);
5626 return retval;
5627 }
5628
5629 if (get_user(len, optlen))
5630 return -EFAULT;
5631
5632 sctp_lock_sock(sk);
5633
5634 switch (optname) {
5635 case SCTP_STATUS:
5636 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
5637 break;
5638 case SCTP_DISABLE_FRAGMENTS:
5639 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
5640 optlen);
5641 break;
5642 case SCTP_EVENTS:
5643 retval = sctp_getsockopt_events(sk, len, optval, optlen);
5644 break;
5645 case SCTP_AUTOCLOSE:
5646 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
5647 break;
5648 case SCTP_SOCKOPT_PEELOFF:
5649 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
5650 break;
5651 case SCTP_PEER_ADDR_PARAMS:
5652 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
5653 optlen);
5654 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00005655 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07005656 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08005657 optlen);
5658 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 case SCTP_INITMSG:
5660 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
5661 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 case SCTP_GET_PEER_ADDRS:
5663 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
5664 optlen);
5665 break;
5666 case SCTP_GET_LOCAL_ADDRS:
5667 retval = sctp_getsockopt_local_addrs(sk, len, optval,
5668 optlen);
5669 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04005670 case SCTP_SOCKOPT_CONNECTX3:
5671 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
5672 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 case SCTP_DEFAULT_SEND_PARAM:
5674 retval = sctp_getsockopt_default_send_param(sk, len,
5675 optval, optlen);
5676 break;
5677 case SCTP_PRIMARY_ADDR:
5678 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
5679 break;
5680 case SCTP_NODELAY:
5681 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
5682 break;
5683 case SCTP_RTOINFO:
5684 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
5685 break;
5686 case SCTP_ASSOCINFO:
5687 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
5688 break;
5689 case SCTP_I_WANT_MAPPED_V4_ADDR:
5690 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
5691 break;
5692 case SCTP_MAXSEG:
5693 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
5694 break;
5695 case SCTP_GET_PEER_ADDR_INFO:
5696 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
5697 optlen);
5698 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005699 case SCTP_ADAPTATION_LAYER:
5700 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 optlen);
5702 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005703 case SCTP_CONTEXT:
5704 retval = sctp_getsockopt_context(sk, len, optval, optlen);
5705 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005706 case SCTP_FRAGMENT_INTERLEAVE:
5707 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
5708 optlen);
5709 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005710 case SCTP_PARTIAL_DELIVERY_POINT:
5711 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
5712 optlen);
5713 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07005714 case SCTP_MAX_BURST:
5715 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
5716 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005717 case SCTP_AUTH_KEY:
5718 case SCTP_AUTH_CHUNK:
5719 case SCTP_AUTH_DELETE_KEY:
5720 retval = -EOPNOTSUPP;
5721 break;
5722 case SCTP_HMAC_IDENT:
5723 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
5724 break;
5725 case SCTP_AUTH_ACTIVE_KEY:
5726 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
5727 break;
5728 case SCTP_PEER_AUTH_CHUNKS:
5729 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
5730 optlen);
5731 break;
5732 case SCTP_LOCAL_AUTH_CHUNKS:
5733 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
5734 optlen);
5735 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08005736 case SCTP_GET_ASSOC_NUMBER:
5737 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
5738 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00005739 case SCTP_GET_ASSOC_ID_LIST:
5740 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
5741 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09005742 case SCTP_AUTO_ASCONF:
5743 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
5744 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00005745 case SCTP_PEER_ADDR_THLDS:
5746 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
5747 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 default:
5749 retval = -ENOPROTOOPT;
5750 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
5753 sctp_release_sock(sk);
5754 return retval;
5755}
5756
5757static void sctp_hash(struct sock *sk)
5758{
5759 /* STUB */
5760}
5761
5762static void sctp_unhash(struct sock *sk)
5763{
5764 /* STUB */
5765}
5766
5767/* Check if port is acceptable. Possibly find first available port.
5768 *
5769 * The port hash table (contained in the 'global' SCTP protocol storage
5770 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
5771 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
5772 * list (the list number is the port number hashed out, so as you
5773 * would expect from a hash function, all the ports in a given list have
5774 * such a number that hashes out to the same list number; you were
5775 * expecting that, right?); so each list has a set of ports, with a
5776 * link to the socket (struct sock) that uses it, the port number and
5777 * a fastreuse flag (FIXME: NPI ipg).
5778 */
5779static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00005780 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
5782static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
5783{
5784 struct sctp_bind_hashbucket *head; /* hash list */
5785 struct sctp_bind_bucket *pp; /* hash list port iterator */
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005786 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 unsigned short snum;
5788 int ret;
5789
Al Viro04afd8b2006-11-20 17:02:01 -08005790 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791
5792 SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
5793 sctp_local_bh_disable();
5794
5795 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07005796 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07005797 int low, high, remaining, index;
5798 unsigned int rover;
5799
5800 inet_get_local_port_range(&low, &high);
5801 remaining = (high - low) + 1;
5802 rover = net_random() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 do {
5805 rover++;
5806 if ((rover < low) || (rover > high))
5807 rover = low;
Amerigo Wange3826f12010-05-05 00:27:06 +00005808 if (inet_is_reserved_local_port(rover))
5809 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00005810 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 head = &sctp_port_hashtable[index];
5812 sctp_spin_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005813 sctp_for_each_hentry(pp, node, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00005814 if ((pp->port == rover) &&
5815 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 goto next;
5817 break;
5818 next:
5819 sctp_spin_unlock(&head->lock);
5820 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821
5822 /* Exhausted local port range during search? */
5823 ret = 1;
5824 if (remaining <= 0)
5825 goto fail;
5826
5827 /* OK, here is the one we will use. HEAD (the port
5828 * hash table list entry) is non-NULL and we hold it's
5829 * mutex.
5830 */
5831 snum = rover;
5832 } else {
5833 /* We are given an specific port number; we verify
5834 * that it is not being used. If it is used, we will
5835 * exahust the search in the hash list corresponding
5836 * to the port number (snum) - we detect that with the
5837 * port iterator, pp being NULL.
5838 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00005839 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 sctp_spin_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05005841 sctp_for_each_hentry(pp, node, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00005842 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 goto pp_found;
5844 }
5845 }
5846 pp = NULL;
5847 goto pp_not_found;
5848pp_found:
5849 if (!hlist_empty(&pp->owner)) {
5850 /* We had a port hash table hit - there is an
5851 * available port (pp != NULL) and it is being
5852 * used by other socket (pp->owner not empty); that other
5853 * socket is going to be sk2.
5854 */
5855 int reuse = sk->sk_reuse;
5856 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
5858 SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005859 if (pp->fastreuse && sk->sk_reuse &&
5860 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 goto success;
5862
5863 /* Run through the list of sockets bound to the port
5864 * (pp->port) [via the pointers bind_next and
5865 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
5866 * we get the endpoint they describe and run through
5867 * the endpoint's list of IP (v4 or v6) addresses,
5868 * comparing each of the addresses with the address of
5869 * the socket sk. If we find a match, then that means
5870 * that this port/socket (sk) combination are already
5871 * in an endpoint.
5872 */
5873 sk_for_each_bound(sk2, node, &pp->owner) {
5874 struct sctp_endpoint *ep2;
5875 ep2 = sctp_sk(sk2)->ep;
5876
Vlad Yasevich4e540642008-07-18 23:06:32 -07005877 if (sk == sk2 ||
5878 (reuse && sk2->sk_reuse &&
5879 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 continue;
5881
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005882 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
5883 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 ret = (long)sk2;
5885 goto fail_unlock;
5886 }
5887 }
5888 SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
5889 }
5890pp_not_found:
5891 /* If there was a hash table miss, create a new port. */
5892 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00005893 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 goto fail_unlock;
5895
5896 /* In either case (hit or miss), make sure fastreuse is 1 only
5897 * if sk->sk_reuse is too (that is, if the caller requested
5898 * SO_REUSEADDR on this socket -sk-).
5899 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07005900 if (hlist_empty(&pp->owner)) {
5901 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
5902 pp->fastreuse = 1;
5903 else
5904 pp->fastreuse = 0;
5905 } else if (pp->fastreuse &&
5906 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 pp->fastreuse = 0;
5908
5909 /* We are set, so fill up all the data in the hash table
5910 * entry, tie the socket list information with the rest of the
5911 * sockets FIXME: Blurry, NPI (ipg).
5912 */
5913success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +00005915 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 sk_add_bind_node(sk, &pp->owner);
5917 sctp_sk(sk)->bind_hash = pp;
5918 }
5919 ret = 0;
5920
5921fail_unlock:
5922 sctp_spin_unlock(&head->lock);
5923
5924fail:
5925 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 return ret;
5927}
5928
5929/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
5930 * port is requested.
5931 */
5932static int sctp_get_port(struct sock *sk, unsigned short snum)
5933{
5934 long ret;
5935 union sctp_addr addr;
5936 struct sctp_af *af = sctp_sk(sk)->pf->af;
5937
5938 /* Set up a dummy address struct from the sk. */
5939 af->from_sk(&addr, sk);
5940 addr.v4.sin_port = htons(snum);
5941
5942 /* Note: sk->sk_num gets filled in if ephemeral port request. */
5943 ret = sctp_get_port_local(sk, &addr);
5944
Eric Dumazeta02cec22010-09-22 20:43:57 +00005945 return ret ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946}
5947
5948/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 * Move a socket to LISTENING state.
5950 */
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005951SCTP_STATIC int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005953 struct sctp_sock *sp = sctp_sk(sk);
5954 struct sctp_endpoint *ep = sp->ep;
Herbert Xu1b489e12006-08-20 15:07:14 +10005955 struct crypto_hash *tfm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956
5957 /* Allocate HMAC for generating cookie. */
Vlad Yasevich23b29ed2008-07-18 23:06:07 -07005958 if (!sctp_sk(sk)->hmac && sctp_hmac_alg) {
Herbert Xu1b489e12006-08-20 15:07:14 +10005959 tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07005960 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00005961 net_info_ratelimited("failed to load transform for %s: %ld\n",
5962 sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005963 return -ENOSYS;
5964 }
5965 sctp_sk(sk)->hmac = tfm;
5966 }
5967
5968 /*
5969 * If a bind() or sctp_bindx() is not called prior to a listen()
5970 * call that allows new associations to be accepted, the system
5971 * picks an ephemeral port and will choose an address set equivalent
5972 * to binding with a wildcard address.
5973 *
5974 * This is not currently spelled out in the SCTP sockets
5975 * extensions draft, but follows the practice as seen in TCP
5976 * sockets.
5977 *
5978 */
5979 sk->sk_state = SCTP_SS_LISTENING;
5980 if (!ep->base.bind_addr.port) {
5981 if (sctp_autobind(sk))
5982 return -EAGAIN;
5983 } else {
Eric Dumazetc720c7e2009-10-15 06:30:45 +00005984 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005985 sk->sk_state = SCTP_SS_CLOSED;
5986 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 }
5988 }
5989
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00005990 sk->sk_max_ack_backlog = backlog;
5991 sctp_hash_endpoint(ep);
5992 return 0;
5993}
5994
5995/*
5996 * 4.1.3 / 5.1.3 listen()
5997 *
5998 * By default, new associations are not accepted for UDP style sockets.
5999 * An application uses listen() to mark a socket as being able to
6000 * accept new associations.
6001 *
6002 * On TCP style sockets, applications use listen() to ready the SCTP
6003 * endpoint for accepting inbound associations.
6004 *
6005 * On both types of endpoints a backlog of '0' disables listening.
6006 *
6007 * Move a socket to LISTENING state.
6008 */
6009int sctp_inet_listen(struct socket *sock, int backlog)
6010{
6011 struct sock *sk = sock->sk;
6012 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6013 int err = -EINVAL;
6014
6015 if (unlikely(backlog < 0))
6016 return err;
6017
6018 sctp_lock_sock(sk);
6019
6020 /* Peeled-off sockets are not allowed to listen(). */
6021 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
6022 goto out;
6023
6024 if (sock->state != SS_UNCONNECTED)
6025 goto out;
6026
6027 /* If backlog is zero, disable listening. */
6028 if (!backlog) {
6029 if (sctp_sstate(sk, CLOSED))
6030 goto out;
6031
6032 err = 0;
6033 sctp_unhash_endpoint(ep);
6034 sk->sk_state = SCTP_SS_CLOSED;
6035 if (sk->sk_reuse)
6036 sctp_sk(sk)->bind_hash->fastreuse = 1;
6037 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07006038 }
6039
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006040 /* If we are already listening, just update the backlog */
6041 if (sctp_sstate(sk, LISTENING))
6042 sk->sk_max_ack_backlog = backlog;
6043 else {
6044 err = sctp_listen_start(sk, backlog);
6045 if (err)
6046 goto out;
6047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006049 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050out:
6051 sctp_release_sock(sk);
6052 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053}
6054
6055/*
6056 * This function is done by modeling the current datagram_poll() and the
6057 * tcp_poll(). Note that, based on these implementations, we don't
6058 * lock the socket in this function, even though it seems that,
6059 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08006060 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 * in this place. We assume that we don't need locks either until proven
6062 * otherwise.
6063 *
6064 * Another thing to note is that we include the Async I/O support
6065 * here, again, by modeling the current TCP/UDP code. We don't have
6066 * a good way to test with it yet.
6067 */
6068unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
6069{
6070 struct sock *sk = sock->sk;
6071 struct sctp_sock *sp = sctp_sk(sk);
6072 unsigned int mask;
6073
Eric Dumazetaa395142010-04-20 13:03:51 +00006074 poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
6076 /* A TCP-style listening socket becomes readable when the accept queue
6077 * is not empty.
6078 */
6079 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
6080 return (!list_empty(&sp->ep->asocs)) ?
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006081 (POLLIN | POLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082
6083 mask = 0;
6084
6085 /* Is there any exceptional events? */
6086 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
6087 mask |= POLLERR;
Davide Libenzif348d702006-03-25 03:07:39 -08006088 if (sk->sk_shutdown & RCV_SHUTDOWN)
Eric Dumazetdb409802010-09-06 11:13:50 +00006089 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 if (sk->sk_shutdown == SHUTDOWN_MASK)
6091 mask |= POLLHUP;
6092
6093 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00006094 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 mask |= POLLIN | POLLRDNORM;
6096
6097 /* The association is either gone or not ready. */
6098 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
6099 return mask;
6100
6101 /* Is it writable? */
6102 if (sctp_writeable(sk)) {
6103 mask |= POLLOUT | POLLWRNORM;
6104 } else {
6105 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
6106 /*
6107 * Since the socket is not locked, the buffer
6108 * might be made available after the writeable check and
6109 * before the bit is set. This could cause a lost I/O
6110 * signal. tcp_poll() has a race breaker for this race
6111 * condition. Based on their implementation, we put
6112 * in the following code to cover it as well.
6113 */
6114 if (sctp_writeable(sk))
6115 mask |= POLLOUT | POLLWRNORM;
6116 }
6117 return mask;
6118}
6119
6120/********************************************************************
6121 * 2nd Level Abstractions
6122 ********************************************************************/
6123
6124static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006125 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126{
6127 struct sctp_bind_bucket *pp;
6128
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08006129 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07006131 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 pp->port = snum;
6133 pp->fastreuse = 0;
6134 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006135 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05006136 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 }
6138 return pp;
6139}
6140
6141/* Caller must hold hashbucket lock for this tb with local BH disabled */
6142static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
6143{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07006144 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05006145 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 kmem_cache_free(sctp_bucket_cachep, pp);
6147 SCTP_DBG_OBJCNT_DEC(bind_bucket);
6148 }
6149}
6150
6151/* Release this socket's reference to a local port. */
6152static inline void __sctp_put_port(struct sock *sk)
6153{
6154 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006155 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
6156 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 struct sctp_bind_bucket *pp;
6158
6159 sctp_spin_lock(&head->lock);
6160 pp = sctp_sk(sk)->bind_hash;
6161 __sk_del_bind_node(sk);
6162 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006163 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 sctp_bucket_destroy(pp);
6165 sctp_spin_unlock(&head->lock);
6166}
6167
6168void sctp_put_port(struct sock *sk)
6169{
6170 sctp_local_bh_disable();
6171 __sctp_put_port(sk);
6172 sctp_local_bh_enable();
6173}
6174
6175/*
6176 * The system picks an ephemeral port and choose an address set equivalent
6177 * to binding with a wildcard address.
6178 * One of those addresses will be the primary address for the association.
6179 * This automatically enables the multihoming capability of SCTP.
6180 */
6181static int sctp_autobind(struct sock *sk)
6182{
6183 union sctp_addr autoaddr;
6184 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08006185 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186
6187 /* Initialize a local sockaddr structure to INADDR_ANY. */
6188 af = sctp_sk(sk)->pf->af;
6189
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006190 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 af->inaddr_any(&autoaddr, port);
6192
6193 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
6194}
6195
6196/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
6197 *
6198 * From RFC 2292
6199 * 4.2 The cmsghdr Structure *
6200 *
6201 * When ancillary data is sent or received, any number of ancillary data
6202 * objects can be specified by the msg_control and msg_controllen members of
6203 * the msghdr structure, because each object is preceded by
6204 * a cmsghdr structure defining the object's length (the cmsg_len member).
6205 * Historically Berkeley-derived implementations have passed only one object
6206 * at a time, but this API allows multiple objects to be
6207 * passed in a single call to sendmsg() or recvmsg(). The following example
6208 * shows two ancillary data objects in a control buffer.
6209 *
6210 * |<--------------------------- msg_controllen -------------------------->|
6211 * | |
6212 *
6213 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
6214 *
6215 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
6216 * | | |
6217 *
6218 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
6219 *
6220 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
6221 * | | | | |
6222 *
6223 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
6224 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
6225 *
6226 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
6227 *
6228 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
6229 * ^
6230 * |
6231 *
6232 * msg_control
6233 * points here
6234 */
6235SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
6236 sctp_cmsgs_t *cmsgs)
6237{
6238 struct cmsghdr *cmsg;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07006239 struct msghdr *my_msg = (struct msghdr *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240
6241 for (cmsg = CMSG_FIRSTHDR(msg);
6242 cmsg != NULL;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07006243 cmsg = CMSG_NXTHDR(my_msg, cmsg)) {
6244 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 return -EINVAL;
6246
6247 /* Should we parse this header or ignore? */
6248 if (cmsg->cmsg_level != IPPROTO_SCTP)
6249 continue;
6250
6251 /* Strictly check lengths following example in SCM code. */
6252 switch (cmsg->cmsg_type) {
6253 case SCTP_INIT:
6254 /* SCTP Socket API Extension
6255 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
6256 *
6257 * This cmsghdr structure provides information for
6258 * initializing new SCTP associations with sendmsg().
6259 * The SCTP_INITMSG socket option uses this same data
6260 * structure. This structure is not used for
6261 * recvmsg().
6262 *
6263 * cmsg_level cmsg_type cmsg_data[]
6264 * ------------ ------------ ----------------------
6265 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
6266 */
6267 if (cmsg->cmsg_len !=
6268 CMSG_LEN(sizeof(struct sctp_initmsg)))
6269 return -EINVAL;
6270 cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
6271 break;
6272
6273 case SCTP_SNDRCV:
6274 /* SCTP Socket API Extension
6275 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
6276 *
6277 * This cmsghdr structure specifies SCTP options for
6278 * sendmsg() and describes SCTP header information
6279 * about a received message through recvmsg().
6280 *
6281 * cmsg_level cmsg_type cmsg_data[]
6282 * ------------ ------------ ----------------------
6283 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
6284 */
6285 if (cmsg->cmsg_len !=
6286 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
6287 return -EINVAL;
6288
6289 cmsgs->info =
6290 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
6291
6292 /* Minimally, validate the sinfo_flags. */
6293 if (cmsgs->info->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07006294 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
6295 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 return -EINVAL;
6297 break;
6298
6299 default:
6300 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07006301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 }
6303 return 0;
6304}
6305
6306/*
6307 * Wait for a packet..
6308 * Note: This function is the same function as in core/datagram.c
6309 * with a few modifications to make lksctp work.
6310 */
6311static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
6312{
6313 int error;
6314 DEFINE_WAIT(wait);
6315
Eric Dumazetaa395142010-04-20 13:03:51 +00006316 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317
6318 /* Socket errors? */
6319 error = sock_error(sk);
6320 if (error)
6321 goto out;
6322
6323 if (!skb_queue_empty(&sk->sk_receive_queue))
6324 goto ready;
6325
6326 /* Socket shut down? */
6327 if (sk->sk_shutdown & RCV_SHUTDOWN)
6328 goto out;
6329
6330 /* Sequenced packets can come disconnected. If so we report the
6331 * problem.
6332 */
6333 error = -ENOTCONN;
6334
6335 /* Is there a good reason to think that we may receive some data? */
6336 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
6337 goto out;
6338
6339 /* Handle signals. */
6340 if (signal_pending(current))
6341 goto interrupted;
6342
6343 /* Let another process have a go. Since we are going to sleep
6344 * anyway. Note: This may cause odd behaviors if the message
6345 * does not fit in the user's buffer, but this seems to be the
6346 * only way to honor MSG_DONTWAIT realistically.
6347 */
6348 sctp_release_sock(sk);
6349 *timeo_p = schedule_timeout(*timeo_p);
6350 sctp_lock_sock(sk);
6351
6352ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00006353 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 return 0;
6355
6356interrupted:
6357 error = sock_intr_errno(*timeo_p);
6358
6359out:
Eric Dumazetaa395142010-04-20 13:03:51 +00006360 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 *err = error;
6362 return error;
6363}
6364
6365/* Receive a datagram.
6366 * Note: This is pretty much the same routine as in core/datagram.c
6367 * with a few changes to make lksctp work.
6368 */
6369static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
6370 int noblock, int *err)
6371{
6372 int error;
6373 struct sk_buff *skb;
6374 long timeo;
6375
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 timeo = sock_rcvtimeo(sk, noblock);
6377
6378 SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
6379 timeo, MAX_SCHEDULE_TIMEOUT);
6380
6381 do {
6382 /* Again only user level code calls this function,
6383 * so nothing interrupt level
6384 * will suddenly eat the receive_queue.
6385 *
6386 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00006387 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 */
6389 if (flags & MSG_PEEK) {
Herbert Xu1e061ab2005-06-18 22:56:42 -07006390 spin_lock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 skb = skb_peek(&sk->sk_receive_queue);
6392 if (skb)
6393 atomic_inc(&skb->users);
Herbert Xu1e061ab2005-06-18 22:56:42 -07006394 spin_unlock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 } else {
6396 skb = skb_dequeue(&sk->sk_receive_queue);
6397 }
6398
6399 if (skb)
6400 return skb;
6401
Neil Horman6736dc32005-12-02 20:30:06 -08006402 /* Caller is allowed not to check sk->sk_err before calling. */
6403 error = sock_error(sk);
6404 if (error)
6405 goto no_packet;
6406
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 if (sk->sk_shutdown & RCV_SHUTDOWN)
6408 break;
6409
6410 /* User doesn't want to wait. */
6411 error = -EAGAIN;
6412 if (!timeo)
6413 goto no_packet;
6414 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
6415
6416 return NULL;
6417
6418no_packet:
6419 *err = error;
6420 return NULL;
6421}
6422
6423/* If sndbuf has changed, wake up per association sndbuf waiters. */
6424static void __sctp_write_space(struct sctp_association *asoc)
6425{
6426 struct sock *sk = asoc->base.sk;
6427 struct socket *sock = sk->sk_socket;
6428
6429 if ((sctp_wspace(asoc) > 0) && sock) {
6430 if (waitqueue_active(&asoc->wait))
6431 wake_up_interruptible(&asoc->wait);
6432
6433 if (sctp_writeable(sk)) {
Eric Dumazeteaefd112011-02-18 03:26:36 +00006434 wait_queue_head_t *wq = sk_sleep(sk);
6435
6436 if (wq && waitqueue_active(wq))
6437 wake_up_interruptible(wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438
6439 /* Note that we try to include the Async I/O support
6440 * here by modeling from the current TCP/UDP code.
6441 * We have not tested with it yet.
6442 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00006443 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08006444 sock_wake_async(sock,
6445 SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 }
6447 }
6448}
6449
6450/* Do accounting for the sndbuf space.
6451 * Decrement the used sndbuf space of the corresponding association by the
6452 * data size which was just transmitted(freed).
6453 */
6454static void sctp_wfree(struct sk_buff *skb)
6455{
6456 struct sctp_association *asoc;
6457 struct sctp_chunk *chunk;
6458 struct sock *sk;
6459
6460 /* Get the saved chunk pointer. */
6461 chunk = *((struct sctp_chunk **)(skb->cb));
6462 asoc = chunk->asoc;
6463 sk = asoc->base.sk;
Neil Horman4eb701d2005-04-28 12:02:04 -07006464 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
6465 sizeof(struct sk_buff) +
6466 sizeof(struct sctp_chunk);
6467
Neil Horman4eb701d2005-04-28 12:02:04 -07006468 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
6469
Neil Horman4d93df02007-08-15 16:07:44 -07006470 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006471 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
Neil Horman4d93df02007-08-15 16:07:44 -07006472 */
6473 sk->sk_wmem_queued -= skb->truesize;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006474 sk_mem_uncharge(sk, skb->truesize);
Neil Horman4d93df02007-08-15 16:07:44 -07006475
Neil Horman4eb701d2005-04-28 12:02:04 -07006476 sock_wfree(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 __sctp_write_space(asoc);
6478
6479 sctp_association_put(asoc);
6480}
6481
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07006482/* Do accounting for the receive space on the socket.
6483 * Accounting for the association is done in ulpevent.c
6484 * We set this as a destructor for the cloned data skbs so that
6485 * accounting is done at the correct time.
6486 */
6487void sctp_sock_rfree(struct sk_buff *skb)
6488{
6489 struct sock *sk = skb->sk;
6490 struct sctp_ulpevent *event = sctp_skb2event(skb);
6491
6492 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07006493
6494 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006495 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07006496 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006497 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07006498}
6499
6500
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501/* Helper function to wait for space in the sndbuf. */
6502static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
6503 size_t msg_len)
6504{
6505 struct sock *sk = asoc->base.sk;
6506 int err = 0;
6507 long current_timeo = *timeo_p;
6508 DEFINE_WAIT(wait);
6509
6510 SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006511 asoc, (long)(*timeo_p), msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512
6513 /* Increment the association's refcnt. */
6514 sctp_association_hold(asoc);
6515
6516 /* Wait on the association specific sndbuf space. */
6517 for (;;) {
6518 prepare_to_wait_exclusive(&asoc->wait, &wait,
6519 TASK_INTERRUPTIBLE);
6520 if (!*timeo_p)
6521 goto do_nonblock;
6522 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6523 asoc->base.dead)
6524 goto do_error;
6525 if (signal_pending(current))
6526 goto do_interrupted;
6527 if (msg_len <= sctp_wspace(asoc))
6528 break;
6529
6530 /* Let another process have a go. Since we are going
6531 * to sleep anyway.
6532 */
6533 sctp_release_sock(sk);
6534 current_timeo = schedule_timeout(current_timeo);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006535 BUG_ON(sk != asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 sctp_lock_sock(sk);
6537
6538 *timeo_p = current_timeo;
6539 }
6540
6541out:
6542 finish_wait(&asoc->wait, &wait);
6543
6544 /* Release the association's refcnt. */
6545 sctp_association_put(asoc);
6546
6547 return err;
6548
6549do_error:
6550 err = -EPIPE;
6551 goto out;
6552
6553do_interrupted:
6554 err = sock_intr_errno(*timeo_p);
6555 goto out;
6556
6557do_nonblock:
6558 err = -EAGAIN;
6559 goto out;
6560}
6561
Wei Yongjun561b1732010-04-28 08:47:18 +00006562void sctp_data_ready(struct sock *sk, int len)
6563{
David S. Miller7ef52732010-05-02 21:43:40 -07006564 struct socket_wq *wq;
6565
6566 rcu_read_lock();
6567 wq = rcu_dereference(sk->sk_wq);
6568 if (wq_has_sleeper(wq))
6569 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
Wei Yongjun561b1732010-04-28 08:47:18 +00006570 POLLRDNORM | POLLRDBAND);
6571 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07006572 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00006573}
6574
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575/* If socket sndbuf has changed, wake up all per association waiters. */
6576void sctp_write_space(struct sock *sk)
6577{
6578 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579
6580 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07006581 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 __sctp_write_space(asoc);
6583 }
6584}
6585
6586/* Is there any sndbuf space available on the socket?
6587 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08006588 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 * associations on the same socket. For a UDP-style socket with
6590 * multiple associations, it is possible for it to be "unwriteable"
6591 * prematurely. I assume that this is acceptable because
6592 * a premature "unwriteable" is better than an accidental "writeable" which
6593 * would cause an unwanted block under certain circumstances. For the 1-1
6594 * UDP-style sockets or TCP-style sockets, this code should work.
6595 * - Daisy
6596 */
6597static int sctp_writeable(struct sock *sk)
6598{
6599 int amt = 0;
6600
Eric Dumazet31e6d362009-06-17 19:05:41 -07006601 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 if (amt < 0)
6603 amt = 0;
6604 return amt;
6605}
6606
6607/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
6608 * returns immediately with EINPROGRESS.
6609 */
6610static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
6611{
6612 struct sock *sk = asoc->base.sk;
6613 int err = 0;
6614 long current_timeo = *timeo_p;
6615 DEFINE_WAIT(wait);
6616
Harvey Harrison0dc47872008-03-05 20:47:47 -08006617 SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __func__, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 (long)(*timeo_p));
6619
6620 /* Increment the association's refcnt. */
6621 sctp_association_hold(asoc);
6622
6623 for (;;) {
6624 prepare_to_wait_exclusive(&asoc->wait, &wait,
6625 TASK_INTERRUPTIBLE);
6626 if (!*timeo_p)
6627 goto do_nonblock;
6628 if (sk->sk_shutdown & RCV_SHUTDOWN)
6629 break;
6630 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
6631 asoc->base.dead)
6632 goto do_error;
6633 if (signal_pending(current))
6634 goto do_interrupted;
6635
6636 if (sctp_state(asoc, ESTABLISHED))
6637 break;
6638
6639 /* Let another process have a go. Since we are going
6640 * to sleep anyway.
6641 */
6642 sctp_release_sock(sk);
6643 current_timeo = schedule_timeout(current_timeo);
6644 sctp_lock_sock(sk);
6645
6646 *timeo_p = current_timeo;
6647 }
6648
6649out:
6650 finish_wait(&asoc->wait, &wait);
6651
6652 /* Release the association's refcnt. */
6653 sctp_association_put(asoc);
6654
6655 return err;
6656
6657do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08006658 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 err = -ETIMEDOUT;
6660 else
6661 err = -ECONNREFUSED;
6662 goto out;
6663
6664do_interrupted:
6665 err = sock_intr_errno(*timeo_p);
6666 goto out;
6667
6668do_nonblock:
6669 err = -EINPROGRESS;
6670 goto out;
6671}
6672
6673static int sctp_wait_for_accept(struct sock *sk, long timeo)
6674{
6675 struct sctp_endpoint *ep;
6676 int err = 0;
6677 DEFINE_WAIT(wait);
6678
6679 ep = sctp_sk(sk)->ep;
6680
6681
6682 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00006683 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 TASK_INTERRUPTIBLE);
6685
6686 if (list_empty(&ep->asocs)) {
6687 sctp_release_sock(sk);
6688 timeo = schedule_timeout(timeo);
6689 sctp_lock_sock(sk);
6690 }
6691
6692 err = -EINVAL;
6693 if (!sctp_sstate(sk, LISTENING))
6694 break;
6695
6696 err = 0;
6697 if (!list_empty(&ep->asocs))
6698 break;
6699
6700 err = sock_intr_errno(timeo);
6701 if (signal_pending(current))
6702 break;
6703
6704 err = -EAGAIN;
6705 if (!timeo)
6706 break;
6707 }
6708
Eric Dumazetaa395142010-04-20 13:03:51 +00006709 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710
6711 return err;
6712}
6713
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02006714static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715{
6716 DEFINE_WAIT(wait);
6717
6718 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00006719 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 if (list_empty(&sctp_sk(sk)->ep->asocs))
6721 break;
6722 sctp_release_sock(sk);
6723 timeout = schedule_timeout(timeout);
6724 sctp_lock_sock(sk);
6725 } while (!signal_pending(current) && timeout);
6726
Eric Dumazetaa395142010-04-20 13:03:51 +00006727 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728}
6729
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006730static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
6731{
6732 struct sk_buff *frag;
6733
6734 if (!skb->data_len)
6735 goto done;
6736
6737 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07006738 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006739 sctp_skb_set_owner_r_frag(frag, sk);
6740
6741done:
6742 sctp_skb_set_owner_r(skb, sk);
6743}
6744
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006745void sctp_copy_sock(struct sock *newsk, struct sock *sk,
6746 struct sctp_association *asoc)
6747{
6748 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08006749 struct inet_sock *newinet;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006750
6751 newsk->sk_type = sk->sk_type;
6752 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
6753 newsk->sk_flags = sk->sk_flags;
6754 newsk->sk_no_check = sk->sk_no_check;
6755 newsk->sk_reuse = sk->sk_reuse;
6756
6757 newsk->sk_shutdown = sk->sk_shutdown;
6758 newsk->sk_destruct = inet_sock_destruct;
6759 newsk->sk_family = sk->sk_family;
6760 newsk->sk_protocol = IPPROTO_SCTP;
6761 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
6762 newsk->sk_sndbuf = sk->sk_sndbuf;
6763 newsk->sk_rcvbuf = sk->sk_rcvbuf;
6764 newsk->sk_lingertime = sk->sk_lingertime;
6765 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
6766 newsk->sk_sndtimeo = sk->sk_sndtimeo;
6767
6768 newinet = inet_sk(newsk);
6769
6770 /* Initialize sk's sport, dport, rcv_saddr and daddr for
6771 * getsockname() and getpeername()
6772 */
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006773 newinet->inet_sport = inet->inet_sport;
6774 newinet->inet_saddr = inet->inet_saddr;
6775 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
6776 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006777 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006778 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00006779
6780 newinet->uc_ttl = inet->uc_ttl;
6781 newinet->mc_loop = 1;
6782 newinet->mc_ttl = 1;
6783 newinet->mc_index = 0;
6784 newinet->mc_list = NULL;
6785}
6786
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787/* Populate the fields of the newsk from the oldsk and migrate the assoc
6788 * and its messages to the newsk.
6789 */
6790static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
6791 struct sctp_association *assoc,
6792 sctp_socket_type_t type)
6793{
6794 struct sctp_sock *oldsp = sctp_sk(oldsk);
6795 struct sctp_sock *newsp = sctp_sk(newsk);
6796 struct sctp_bind_bucket *pp; /* hash list port iterator */
6797 struct sctp_endpoint *newep = newsp->ep;
6798 struct sk_buff *skb, *tmp;
6799 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08006800 struct sctp_bind_hashbucket *head;
Michio Honda9f7d6532011-04-26 19:32:51 +09006801 struct list_head tmplist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802
6803 /* Migrate socket buffer sizes and all the socket level options to the
6804 * new socket.
6805 */
6806 newsk->sk_sndbuf = oldsk->sk_sndbuf;
6807 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
6808 /* Brute force copy old sctp opt. */
Michio Honda9f7d6532011-04-26 19:32:51 +09006809 if (oldsp->do_auto_asconf) {
6810 memcpy(&tmplist, &newsp->auto_asconf_list, sizeof(tmplist));
6811 inet_sk_copy_descendant(newsk, oldsk);
6812 memcpy(&newsp->auto_asconf_list, &tmplist, sizeof(tmplist));
6813 } else
6814 inet_sk_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815
6816 /* Restore the ep value that was overwritten with the above structure
6817 * copy.
6818 */
6819 newsp->ep = newep;
6820 newsp->hmac = NULL;
6821
6822 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006823 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
6824 inet_sk(oldsk)->inet_num)];
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08006825 sctp_local_bh_disable();
6826 sctp_spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827 pp = sctp_sk(oldsk)->bind_hash;
6828 sk_add_bind_node(newsk, &pp->owner);
6829 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006830 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08006831 sctp_spin_unlock(&head->lock);
6832 sctp_local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006834 /* Copy the bind_addr list from the original endpoint to the new
6835 * endpoint so that we can handle restarts properly
6836 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08006837 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
6838 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07006839
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 /* Move any messages in the old socket's receive queue that are for the
6841 * peeled off association to the new socket's receive queue.
6842 */
6843 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
6844 event = sctp_skb2event(skb);
6845 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07006846 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006848 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 }
6850 }
6851
6852 /* Clean up any messages pending delivery due to partial
6853 * delivery. Three cases:
6854 * 1) No partial deliver; no work.
6855 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
6856 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
6857 */
6858 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006859 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006861 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 struct sk_buff_head *queue;
6863
6864 /* Decide which queue to move pd_lobby skbs to. */
6865 if (assoc->ulpq.pd_mode) {
6866 queue = &newsp->pd_lobby;
6867 } else
6868 queue = &newsk->sk_receive_queue;
6869
6870 /* Walk through the pd_lobby, looking for skbs that
6871 * need moved to the new socket.
6872 */
6873 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
6874 event = sctp_skb2event(skb);
6875 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07006876 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006878 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 }
6880 }
6881
6882 /* Clear up any skbs waiting for the partial
6883 * delivery to finish.
6884 */
6885 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006886 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887
6888 }
6889
Wei Yongjun1bc4ee42009-07-05 19:45:48 +00006890 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006891 sctp_skb_set_owner_r_frag(skb, newsk);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006892
Wei Yongjun1bc4ee42009-07-05 19:45:48 +00006893 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006894 sctp_skb_set_owner_r_frag(skb, newsk);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07006895
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 /* Set the type of socket to indicate that it is peeled off from the
6897 * original UDP-style socket or created with the accept() call on a
6898 * TCP-style socket..
6899 */
6900 newsp->type = type;
6901
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006902 /* Mark the new socket "in-use" by the user so that any packets
6903 * that may arrive on the association after we've moved it are
6904 * queued to the backlog. This prevents a potential race between
6905 * backlog processing on the old socket and new-packet processing
6906 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07006907 *
6908 * The caller has just allocated newsk so we can guarantee that other
6909 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006910 */
Zach Brown5131a182007-06-22 15:14:46 -07006911 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 sctp_assoc_migrate(assoc, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913
6914 /* If the association on the newsk is already closed before accept()
6915 * is called, set RCV_SHUTDOWN flag.
6916 */
6917 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
6918 newsk->sk_shutdown |= RCV_SHUTDOWN;
6919
6920 newsk->sk_state = SCTP_SS_ESTABLISHED;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07006921 sctp_release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922}
6923
Neil Horman4d93df02007-08-15 16:07:44 -07006924
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925/* This proto struct describes the ULP interface for SCTP. */
6926struct proto sctp_prot = {
6927 .name = "SCTP",
6928 .owner = THIS_MODULE,
6929 .close = sctp_close,
6930 .connect = sctp_connect,
6931 .disconnect = sctp_disconnect,
6932 .accept = sctp_accept,
6933 .ioctl = sctp_ioctl,
6934 .init = sctp_init_sock,
6935 .destroy = sctp_destroy_sock,
6936 .shutdown = sctp_shutdown,
6937 .setsockopt = sctp_setsockopt,
6938 .getsockopt = sctp_getsockopt,
6939 .sendmsg = sctp_sendmsg,
6940 .recvmsg = sctp_recvmsg,
6941 .bind = sctp_bind,
6942 .backlog_rcv = sctp_backlog_rcv,
6943 .hash = sctp_hash,
6944 .unhash = sctp_unhash,
6945 .get_port = sctp_get_port,
6946 .obj_size = sizeof(struct sctp_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07006947 .sysctl_mem = sysctl_sctp_mem,
6948 .sysctl_rmem = sysctl_sctp_rmem,
6949 .sysctl_wmem = sysctl_sctp_wmem,
6950 .memory_pressure = &sctp_memory_pressure,
6951 .enter_memory_pressure = sctp_enter_memory_pressure,
6952 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08006953 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954};
6955
Eric Dumazetdfd56b82011-12-10 09:48:31 +00006956#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08006957
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958struct proto sctpv6_prot = {
6959 .name = "SCTPv6",
6960 .owner = THIS_MODULE,
6961 .close = sctp_close,
6962 .connect = sctp_connect,
6963 .disconnect = sctp_disconnect,
6964 .accept = sctp_accept,
6965 .ioctl = sctp_ioctl,
6966 .init = sctp_init_sock,
6967 .destroy = sctp_destroy_sock,
6968 .shutdown = sctp_shutdown,
6969 .setsockopt = sctp_setsockopt,
6970 .getsockopt = sctp_getsockopt,
6971 .sendmsg = sctp_sendmsg,
6972 .recvmsg = sctp_recvmsg,
6973 .bind = sctp_bind,
6974 .backlog_rcv = sctp_backlog_rcv,
6975 .hash = sctp_hash,
6976 .unhash = sctp_unhash,
6977 .get_port = sctp_get_port,
6978 .obj_size = sizeof(struct sctp6_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07006979 .sysctl_mem = sysctl_sctp_mem,
6980 .sysctl_rmem = sysctl_sctp_rmem,
6981 .sysctl_wmem = sysctl_sctp_wmem,
6982 .memory_pressure = &sctp_memory_pressure,
6983 .enter_memory_pressure = sctp_enter_memory_pressure,
6984 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08006985 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00006987#endif /* IS_ENABLED(CONFIG_IPV6) */