blob: 78f38056fca6dc0cb32ad050940ace584735e07f [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
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080031 * along with GNU CC; see the file COPYING. If not, see
32 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
34 * Please send any bug reports or fixes you make to the
35 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020036 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * Written or modified by:
39 * La Monte H.P. Yarroll <piggy@acm.org>
40 * Narasimha Budihal <narsi@refcode.org>
41 * Karl Knutson <karl@athena.chicago.il.us>
42 * Jon Grimm <jgrimm@us.ibm.com>
43 * Xingang Guo <xingang.guo@intel.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Sridhar Samudrala <samudrala@us.ibm.com>
46 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
47 * Ardelle Fan <ardelle.fan@intel.com>
48 * Ryan Layer <rmlayer@us.ibm.com>
49 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
50 * Kevin Gao <kevin.gao@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
52
Joe Perches145ce502010-08-24 13:21:08 +000053#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
Herbert Xu5821c762016-01-24 21:20:12 +080055#include <crypto/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/types.h>
57#include <linux/kernel.h>
58#include <linux/wait.h>
59#include <linux/time.h>
60#include <linux/ip.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080061#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/fcntl.h>
63#include <linux/poll.h>
64#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090065#include <linux/slab.h>
Al Viro56b31d12012-08-18 00:25:51 -040066#include <linux/file.h>
Daniel Borkmannffd59392014-02-17 12:11:11 +010067#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#include <net/ip.h>
70#include <net/icmp.h>
71#include <net/route.h>
72#include <net/ipv6.h>
73#include <net/inet_common.h>
Neil Horman8465a5f2014-04-17 15:26:51 -040074#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76#include <linux/socket.h> /* for sa_family_t */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040077#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <net/sock.h>
79#include <net/sctp/sctp.h>
80#include <net/sctp/sm.h>
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* Forward declarations for internal helper functions. */
83static int sctp_writeable(struct sock *sk);
84static void sctp_wfree(struct sk_buff *skb);
Xin Long9904da52017-11-15 16:57:26 +080085static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Long2f056e72018-01-15 17:01:36 +080086 size_t msg_len);
wangweidong26ac8e52013-12-23 12:16:51 +080087static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
89static int sctp_wait_for_accept(struct sock *sk, long timeo);
90static void sctp_wait_for_close(struct sock *sk, long timeo);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +020091static void sctp_destruct_sock(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
93 union sctp_addr *addr, int len);
94static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
95static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
96static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
97static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
98static int sctp_send_asconf(struct sctp_association *asoc,
99 struct sctp_chunk *chunk);
100static int sctp_do_bind(struct sock *, union sctp_addr *, int);
101static int sctp_autobind(struct sock *sk);
102static void sctp_sock_migrate(struct sock *, struct sock *,
103 struct sctp_association *, sctp_socket_type_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Adrian Bunkb6fa1a42007-09-12 15:18:00 +0200105static int sctp_memory_pressure;
Eric Dumazet8d987e52010-11-09 23:24:26 +0000106static atomic_long_t sctp_memory_allocated;
Eric Dumazet17483762008-11-25 21:16:35 -0800107struct percpu_counter sctp_sockets_allocated;
Neil Horman4d93df02007-08-15 16:07:44 -0700108
Pavel Emelyanov5c52ba12008-07-16 20:28:10 -0700109static void sctp_enter_memory_pressure(struct sock *sk)
Neil Horman4d93df02007-08-15 16:07:44 -0700110{
111 sctp_memory_pressure = 1;
112}
113
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* Get the sndbuf space available at the time on the association. */
116static inline int sctp_wspace(struct sctp_association *asoc)
117{
Neil Horman4d93df02007-08-15 16:07:44 -0700118 int amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Neil Horman4d93df02007-08-15 16:07:44 -0700120 if (asoc->ep->sndbuf_policy)
121 amt = asoc->sndbuf_used;
122 else
Eric Dumazet31e6d362009-06-17 19:05:41 -0700123 amt = sk_wmem_alloc_get(asoc->base.sk);
Neil Horman4d93df02007-08-15 16:07:44 -0700124
125 if (amt >= asoc->base.sk->sk_sndbuf) {
126 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
127 amt = 0;
128 else {
129 amt = sk_stream_wspace(asoc->base.sk);
130 if (amt < 0)
131 amt = 0;
132 }
Neil Horman4eb701d2005-04-28 12:02:04 -0700133 } else {
Neil Horman4d93df02007-08-15 16:07:44 -0700134 amt = asoc->base.sk->sk_sndbuf - amt;
Neil Horman4eb701d2005-04-28 12:02:04 -0700135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return amt;
137}
138
139/* Increment the used sndbuf space count of the corresponding association by
140 * the size of the outgoing data chunk.
141 * Also, set the skb destructor for sndbuf accounting later.
142 *
143 * Since it is always 1-1 between chunk and skb, and also a new skb is always
144 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
145 * destructor in the data chunk skb for the purpose of the sndbuf space
146 * tracking.
147 */
148static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
149{
150 struct sctp_association *asoc = chunk->asoc;
151 struct sock *sk = asoc->base.sk;
152
153 /* The sndbuf space is tracked per association. */
154 sctp_association_hold(asoc);
155
Neil Horman4eb701d2005-04-28 12:02:04 -0700156 skb_set_owner_w(chunk->skb, sk);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 chunk->skb->destructor = sctp_wfree;
159 /* Save the chunk pointer in skb for sctp_wfree to use later. */
Daniel Borkmannf869c912014-11-20 01:54:48 +0100160 skb_shinfo(chunk->skb)->destructor_arg = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Neil Horman4eb701d2005-04-28 12:02:04 -0700162 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
163 sizeof(struct sk_buff) +
164 sizeof(struct sctp_chunk);
165
Neil Horman4eb701d2005-04-28 12:02:04 -0700166 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800167 sk->sk_wmem_queued += chunk->skb->truesize;
168 sk_mem_charge(sk, chunk->skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Xin Longb89fc6a2017-10-28 02:13:29 +0800171static void sctp_clear_owner_w(struct sctp_chunk *chunk)
172{
173 skb_orphan(chunk->skb);
174}
175
176static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
177 void (*cb)(struct sctp_chunk *))
178
179{
180 struct sctp_outq *q = &asoc->outqueue;
181 struct sctp_transport *t;
182 struct sctp_chunk *chunk;
183
184 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
185 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
186 cb(chunk);
187
188 list_for_each_entry(chunk, &q->retransmit, list)
189 cb(chunk);
190
191 list_for_each_entry(chunk, &q->sacked, list)
192 cb(chunk);
193
194 list_for_each_entry(chunk, &q->abandoned, list)
195 cb(chunk);
196
197 list_for_each_entry(chunk, &q->out_chunk_list, list)
198 cb(chunk);
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/* Verify that this is a valid address. */
202static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
203 int len)
204{
205 struct sctp_af *af;
206
207 /* Verify basic sockaddr. */
208 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
209 if (!af)
210 return -EINVAL;
211
212 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700213 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return -EINVAL;
215
216 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
217 return -EINVAL;
218
219 return 0;
220}
221
222/* Look up the association by its id. If this is not a UDP-style
223 * socket, the ID field is always ignored.
224 */
225struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
226{
227 struct sctp_association *asoc = NULL;
228
229 /* If this is not a UDP-style socket, assoc id should be ignored. */
230 if (!sctp_style(sk, UDP)) {
231 /* Return NULL if the socket state is not ESTABLISHED. It
232 * could be a TCP-style listening socket or a socket which
233 * hasn't yet called connect() to establish an association.
234 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -0300235 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return NULL;
237
238 /* Get the first and the only association from the list. */
239 if (!list_empty(&sctp_sk(sk)->ep->asocs))
240 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
241 struct sctp_association, asocs);
242 return asoc;
243 }
244
245 /* Otherwise this is a UDP-style socket. */
246 if (!id || (id == (sctp_assoc_t)-1))
247 return NULL;
248
249 spin_lock_bh(&sctp_assocs_id_lock);
250 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
251 spin_unlock_bh(&sctp_assocs_id_lock);
252
253 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
254 return NULL;
255
256 return asoc;
257}
258
259/* Look up the transport from an address and an assoc id. If both address and
260 * id are specified, the associations matching the address and the id should be
261 * the same.
262 */
263static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
264 struct sockaddr_storage *addr,
265 sctp_assoc_t id)
266{
267 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
Xin Longfb72eca2017-01-24 14:01:53 +0800268 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 union sctp_addr *laddr = (union sctp_addr *)addr;
Xin Longfb72eca2017-01-24 14:01:53 +0800270 struct sctp_transport *transport;
271
Xin Long168bd512017-02-07 20:56:08 +0800272 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
Xin Longfb72eca2017-01-24 14:01:53 +0800273 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800276 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (!addr_asoc)
280 return NULL;
281
282 id_asoc = sctp_id2assoc(sk, id);
283 if (id_asoc && (id_asoc != addr_asoc))
284 return NULL;
285
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600286 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 (union sctp_addr *)addr);
288
289 return transport;
290}
291
292/* API 3.1.2 bind() - UDP Style Syntax
293 * The syntax of bind() is,
294 *
295 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
296 *
297 * sd - the socket descriptor returned by socket().
298 * addr - the address structure (struct sockaddr_in or struct
299 * sockaddr_in6 [RFC 2553]),
300 * addr_len - the size of the address structure.
301 */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200302static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 int retval = 0;
305
wangweidong048ed4b2014-01-21 15:44:11 +0800306 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Daniel Borkmannbb333812013-06-28 19:49:40 +0200308 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
309 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* Disallow binding twice. */
312 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700313 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 addr_len);
315 else
316 retval = -EINVAL;
317
wangweidong048ed4b2014-01-21 15:44:11 +0800318 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 return retval;
321}
322
323static long sctp_get_port_local(struct sock *, union sctp_addr *);
324
325/* Verify this is a valid sockaddr. */
326static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
327 union sctp_addr *addr, int len)
328{
329 struct sctp_af *af;
330
331 /* Check minimum size. */
332 if (len < sizeof (struct sockaddr))
333 return NULL;
334
Xin Long8e3534ea2018-01-15 17:02:00 +0800335 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
336 return NULL;
337
Eric Dumazet16002a42018-04-08 07:52:08 -0700338 if (addr->sa.sa_family == AF_INET6) {
339 if (len < SIN6_LEN_RFC2133)
340 return NULL;
341 /* V4 mapped address are really of AF_INET family */
342 if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
343 !opt->pf->af_supported(AF_INET, opt))
344 return NULL;
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /* If we get this far, af is valid. */
348 af = sctp_get_af_specific(addr->sa.sa_family);
349
350 if (len < af->sockaddr_len)
351 return NULL;
352
353 return af;
354}
355
356/* Bind a local address either to an endpoint or to an association. */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200357static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Eric W. Biederman35946982012-11-16 03:03:12 +0000359 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 struct sctp_sock *sp = sctp_sk(sk);
361 struct sctp_endpoint *ep = sp->ep;
362 struct sctp_bind_addr *bp = &ep->base.bind_addr;
363 struct sctp_af *af;
364 unsigned short snum;
365 int ret = 0;
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /* Common sockaddr verification. */
368 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700369 if (!af) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200370 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
371 __func__, sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700373 }
374
375 snum = ntohs(addr->v4.sin_port);
376
Daniel Borkmannbb333812013-06-28 19:49:40 +0200377 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
378 __func__, sk, &addr->sa, bp->port, snum, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /* PF specific bind() address verification. */
381 if (!sp->pf->bind_verify(sp, addr))
382 return -EADDRNOTAVAIL;
383
Vlad Yasevich8b358052007-05-15 17:14:58 -0400384 /* We must either be unbound, or bind to the same port.
385 * It's OK to allow 0 ports if we are already bound.
386 * We'll just inhert an already bound port in this case
387 */
388 if (bp->port) {
389 if (!snum)
390 snum = bp->port;
391 else if (snum != bp->port) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200392 pr_debug("%s: new port %d doesn't match existing port "
393 "%d\n", __func__, snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400394 return -EINVAL;
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
Eric W. Biederman35946982012-11-16 03:03:12 +0000398 if (snum && snum < PROT_SOCK &&
399 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return -EACCES;
401
Vlad Yasevich4e540642008-07-18 23:06:32 -0700402 /* See if the address matches any of the addresses we may have
403 * already bound before checking against other endpoints.
404 */
405 if (sctp_bind_addr_match(bp, addr, sp))
406 return -EINVAL;
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* Make sure we are allowed to bind here.
409 * The function sctp_get_port_local() does duplicate address
410 * detection.
411 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900412 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if ((ret = sctp_get_port_local(sk, addr))) {
Vlad Yasevich4e540642008-07-18 23:06:32 -0700414 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
417 /* Refresh ephemeral port. */
418 if (!bp->port)
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000419 bp->port = inet_sk(sk)->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Vlad Yasevich559cf712007-09-16 16:03:28 -0700421 /* Add the address to the bind address list.
422 * Use GFP_ATOMIC since BHs will be disabled.
423 */
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300424 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
425 SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /* Copy back into socket for getsockname() use. */
428 if (!ret) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000429 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600430 sp->pf->to_sk_saddr(addr, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
433 return ret;
434}
435
436 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
437 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900438 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900440 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900442 * subsequent ASCONF. Note this restriction binds each side, so at any
443 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * from each endpoint).
445 */
446static int sctp_send_asconf(struct sctp_association *asoc,
447 struct sctp_chunk *chunk)
448{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000449 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 int retval = 0;
451
452 /* If there is an outstanding ASCONF chunk, queue it for later
453 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900454 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700456 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900457 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
460 /* Hold the chunk until an ASCONF_ACK is received. */
461 sctp_chunk_hold(chunk);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000462 retval = sctp_primitive_ASCONF(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (retval)
464 sctp_chunk_free(chunk);
465 else
466 asoc->addip_last_asconf = chunk;
467
468out:
469 return retval;
470}
471
472/* Add a list of addresses as bind addresses to local endpoint or
473 * association.
474 *
475 * Basically run through each address specified in the addrs/addrcnt
476 * array/length pair, determine if it is IPv6 or IPv4 and call
477 * sctp_do_bind() on it.
478 *
479 * If any of them fails, then the operation will be reversed and the
480 * ones that were added will be removed.
481 *
482 * Only sctp_setsockopt_bindx() is supposed to call this function.
483 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200484static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 int cnt;
487 int retval = 0;
488 void *addr_buf;
489 struct sockaddr *sa_addr;
490 struct sctp_af *af;
491
Daniel Borkmannbb333812013-06-28 19:49:40 +0200492 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
493 addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 addr_buf = addrs;
496 for (cnt = 0; cnt < addrcnt; cnt++) {
497 /* The list may contain either IPv4 or IPv6 address;
498 * determine the address length for walking thru the list.
499 */
Joe Perchesea110732011-06-13 16:21:26 +0000500 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 af = sctp_get_af_specific(sa_addr->sa_family);
502 if (!af) {
503 retval = -EINVAL;
504 goto err_bindx_add;
505 }
506
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900507 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 af->sockaddr_len);
509
510 addr_buf += af->sockaddr_len;
511
512err_bindx_add:
513 if (retval < 0) {
514 /* Failed. Cleanup the ones that have been added */
515 if (cnt > 0)
516 sctp_bindx_rem(sk, addrs, cnt);
517 return retval;
518 }
519 }
520
521 return retval;
522}
523
524/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
525 * associations that are part of the endpoint indicating that a list of local
526 * addresses are added to the endpoint.
527 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900528 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * association, we do not send the chunk for that association. But it will not
530 * affect other associations.
531 *
532 * Only sctp_setsockopt_bindx() is supposed to call this function.
533 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900534static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 struct sockaddr *addrs,
536 int addrcnt)
537{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000538 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct sctp_sock *sp;
540 struct sctp_endpoint *ep;
541 struct sctp_association *asoc;
542 struct sctp_bind_addr *bp;
543 struct sctp_chunk *chunk;
544 struct sctp_sockaddr_entry *laddr;
545 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700546 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 void *addr_buf;
548 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct list_head *p;
550 int i;
551 int retval = 0;
552
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000553 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return retval;
555
556 sp = sctp_sk(sk);
557 ep = sp->ep;
558
Daniel Borkmannbb333812013-06-28 19:49:40 +0200559 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
560 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700562 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (!asoc->peer.asconf_capable)
564 continue;
565
566 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
567 continue;
568
569 if (!sctp_state(asoc, ESTABLISHED))
570 continue;
571
572 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900573 * in the bind address list of the association. If so,
574 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 * other associations.
576 */
577 addr_buf = addrs;
578 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000579 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 af = sctp_get_af_specific(addr->v4.sin_family);
581 if (!af) {
582 retval = -EINVAL;
583 goto out;
584 }
585
586 if (sctp_assoc_lookup_laddr(asoc, addr))
587 break;
588
589 addr_buf += af->sockaddr_len;
590 }
591 if (i < addrcnt)
592 continue;
593
Vlad Yasevich559cf712007-09-16 16:03:28 -0700594 /* Use the first valid address in bind addr list of
595 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 bp = &asoc->base.bind_addr;
598 p = bp->address_list.next;
599 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800600 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 addrcnt, SCTP_PARAM_ADD_IP);
602 if (!chunk) {
603 retval = -ENOMEM;
604 goto out;
605 }
606
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700607 /* Add the new addresses to the bind address list with
608 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700610 addr_buf = addrs;
611 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000612 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700613 af = sctp_get_af_specific(addr->v4.sin_family);
614 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800615 retval = sctp_add_bind_addr(bp, &saveaddr,
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300616 sizeof(saveaddr),
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800617 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700618 addr_buf += af->sockaddr_len;
619 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900620 if (asoc->src_out_of_asoc_ok) {
621 struct sctp_transport *trans;
622
623 list_for_each_entry(trans,
624 &asoc->peer.transport_addr_list, transports) {
625 /* Clear the source and route cache */
626 dst_release(trans->dst);
627 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
628 2*asoc->pathmtu, 4380));
629 trans->ssthresh = asoc->peer.i.a_rwnd;
630 trans->rto = asoc->rto_initial;
Michele Baldessari196d6752012-12-01 04:49:42 +0000631 sctp_max_rto(asoc, trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900632 trans->rtt = trans->srtt = trans->rttvar = 0;
633 sctp_transport_route(trans, NULL,
634 sctp_sk(asoc->base.sk));
635 }
636 }
637 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
640out:
641 return retval;
642}
643
644/* Remove a list of addresses from bind addresses list. Do not remove the
645 * last address.
646 *
647 * Basically run through each address specified in the addrs/addrcnt
648 * array/length pair, determine if it is IPv6 or IPv4 and call
649 * sctp_del_bind() on it.
650 *
651 * If any of them fails, then the operation will be reversed and the
652 * ones that were removed will be added back.
653 *
654 * At least one address has to be left; if only one address is
655 * available, the operation will return -EBUSY.
656 *
657 * Only sctp_setsockopt_bindx() is supposed to call this function.
658 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200659static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct sctp_sock *sp = sctp_sk(sk);
662 struct sctp_endpoint *ep = sp->ep;
663 int cnt;
664 struct sctp_bind_addr *bp = &ep->base.bind_addr;
665 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800667 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct sctp_af *af;
669
Daniel Borkmannbb333812013-06-28 19:49:40 +0200670 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
671 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 addr_buf = addrs;
674 for (cnt = 0; cnt < addrcnt; cnt++) {
675 /* If the bind address list is empty or if there is only one
676 * bind address, there is nothing more to be removed (we need
677 * at least one address here).
678 */
679 if (list_empty(&bp->address_list) ||
680 (sctp_list_single_entry(&bp->address_list))) {
681 retval = -EBUSY;
682 goto err_bindx_rem;
683 }
684
Joe Perchesea110732011-06-13 16:21:26 +0000685 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800686 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (!af) {
688 retval = -EINVAL;
689 goto err_bindx_rem;
690 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700691
692 if (!af->addr_valid(sa_addr, sp, NULL)) {
693 retval = -EADDRNOTAVAIL;
694 goto err_bindx_rem;
695 }
696
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000697 if (sa_addr->v4.sin_port &&
698 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 retval = -EINVAL;
700 goto err_bindx_rem;
701 }
702
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000703 if (!sa_addr->v4.sin_port)
704 sa_addr->v4.sin_port = htons(bp->port);
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /* FIXME - There is probably a need to check if sk->sk_saddr and
707 * sk->sk_rcv_addr are currently set to one of the addresses to
708 * be removed. This is something which needs to be looked into
709 * when we are fixing the outstanding issues with multi-homing
710 * socket routing and failover schemes. Refer to comments in
711 * sctp_do_bind(). -daisy
712 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400713 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 addr_buf += af->sockaddr_len;
716err_bindx_rem:
717 if (retval < 0) {
718 /* Failed. Add the ones that has been removed back */
719 if (cnt > 0)
720 sctp_bindx_add(sk, addrs, cnt);
721 return retval;
722 }
723 }
724
725 return retval;
726}
727
728/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
729 * the associations that are part of the endpoint indicating that a list of
730 * local addresses are removed from the endpoint.
731 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900732 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * association, we do not send the chunk for that association. But it will not
734 * affect other associations.
735 *
736 * Only sctp_setsockopt_bindx() is supposed to call this function.
737 */
738static int sctp_send_asconf_del_ip(struct sock *sk,
739 struct sockaddr *addrs,
740 int addrcnt)
741{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000742 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct sctp_sock *sp;
744 struct sctp_endpoint *ep;
745 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700746 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 struct sctp_bind_addr *bp;
748 struct sctp_chunk *chunk;
749 union sctp_addr *laddr;
750 void *addr_buf;
751 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700752 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 int i;
754 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900755 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Michio Honda8a07eb02011-04-26 20:19:36 +0900757 chunk = NULL;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000758 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return retval;
760
761 sp = sctp_sk(sk);
762 ep = sp->ep;
763
Daniel Borkmannbb333812013-06-28 19:49:40 +0200764 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
765 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700767 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 if (!asoc->peer.asconf_capable)
770 continue;
771
772 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
773 continue;
774
775 if (!sctp_state(asoc, ESTABLISHED))
776 continue;
777
778 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900779 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * If so, do not send the asconf chunk to its peer, but
781 * continue with other associations.
782 */
783 addr_buf = addrs;
784 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000785 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 af = sctp_get_af_specific(laddr->v4.sin_family);
787 if (!af) {
788 retval = -EINVAL;
789 goto out;
790 }
791
792 if (!sctp_assoc_lookup_laddr(asoc, laddr))
793 break;
794
795 addr_buf += af->sockaddr_len;
796 }
797 if (i < addrcnt)
798 continue;
799
800 /* Find one address in the association's bind address list
801 * that is not in the packed array of addresses. This is to
802 * make sure that we do not delete all the addresses in the
803 * association.
804 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 bp = &asoc->base.bind_addr;
806 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
807 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900808 if ((laddr == NULL) && (addrcnt == 1)) {
809 if (asoc->asconf_addr_del_pending)
810 continue;
811 asoc->asconf_addr_del_pending =
812 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900813 if (asoc->asconf_addr_del_pending == NULL) {
814 retval = -ENOMEM;
815 goto out;
816 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900817 asoc->asconf_addr_del_pending->sa.sa_family =
818 addrs->sa_family;
819 asoc->asconf_addr_del_pending->v4.sin_port =
820 htons(bp->port);
821 if (addrs->sa_family == AF_INET) {
822 struct sockaddr_in *sin;
823
824 sin = (struct sockaddr_in *)addrs;
825 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
826 } else if (addrs->sa_family == AF_INET6) {
827 struct sockaddr_in6 *sin6;
828
829 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000830 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900831 }
Daniel Borkmannbb333812013-06-28 19:49:40 +0200832
833 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
834 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
835 asoc->asconf_addr_del_pending);
836
Michio Honda8a07eb02011-04-26 20:19:36 +0900837 asoc->src_out_of_asoc_ok = 1;
838 stored = 1;
839 goto skip_mkasconf;
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Daniel Borkmann88362ad2013-09-07 20:51:21 +0200842 if (laddr == NULL)
843 return -EINVAL;
844
Vlad Yasevich559cf712007-09-16 16:03:28 -0700845 /* We do not need RCU protection throughout this loop
846 * because this is done under a socket lock from the
847 * setsockopt call.
848 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
850 SCTP_PARAM_DEL_IP);
851 if (!chunk) {
852 retval = -ENOMEM;
853 goto out;
854 }
855
Michio Honda8a07eb02011-04-26 20:19:36 +0900856skip_mkasconf:
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700857 /* Reset use_as_src flag for the addresses in the bind address
858 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700860 addr_buf = addrs;
861 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000862 laddr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700863 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700864 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a12006-11-20 17:05:23 -0800865 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800866 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700867 }
868 addr_buf += af->sockaddr_len;
869 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700870
871 /* Update the route and saddr entries for all the transports
872 * as some of the addresses in the bind address list are
873 * about to be deleted and cannot be used as source addresses.
874 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700875 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
876 transports) {
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700877 dst_release(transport->dst);
878 sctp_transport_route(transport, NULL,
879 sctp_sk(asoc->base.sk));
880 }
881
Michio Honda8a07eb02011-04-26 20:19:36 +0900882 if (stored)
883 /* We don't need to transmit ASCONF */
884 continue;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700885 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887out:
888 return retval;
889}
890
Michio Honda9f7d6532011-04-26 19:32:51 +0900891/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
892int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
893{
894 struct sock *sk = sctp_opt2sk(sp);
895 union sctp_addr *addr;
896 struct sctp_af *af;
897
898 /* It is safe to write port space in caller. */
899 addr = &addrw->a;
900 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
901 af = sctp_get_af_specific(addr->sa.sa_family);
902 if (!af)
903 return -EINVAL;
904 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
905 return -EINVAL;
906
907 if (addrw->state == SCTP_ADDR_NEW)
908 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
909 else
910 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
911}
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
914 *
915 * API 8.1
916 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
917 * int flags);
918 *
919 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
920 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
921 * or IPv6 addresses.
922 *
923 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
924 * Section 3.1.2 for this usage.
925 *
926 * addrs is a pointer to an array of one or more socket addresses. Each
927 * address is contained in its appropriate structure (i.e. struct
928 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700929 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 * representation is termed a "packed array" of addresses). The caller
931 * specifies the number of addresses in the array with addrcnt.
932 *
933 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
934 * -1, and sets errno to the appropriate error code.
935 *
936 * For SCTP, the port given in each socket address must be the same, or
937 * sctp_bindx() will fail, setting errno to EINVAL.
938 *
939 * The flags parameter is formed from the bitwise OR of zero or more of
940 * the following currently defined flags:
941 *
942 * SCTP_BINDX_ADD_ADDR
943 *
944 * SCTP_BINDX_REM_ADDR
945 *
946 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
947 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
948 * addresses from the association. The two flags are mutually exclusive;
949 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
950 * not remove all addresses from an association; sctp_bindx() will
951 * reject such an attempt with EINVAL.
952 *
953 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
954 * additional addresses with an endpoint after calling bind(). Or use
955 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
956 * socket is associated with so that no new association accepted will be
957 * associated with those addresses. If the endpoint supports dynamic
958 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
959 * endpoint to send the appropriate message to the peer to change the
960 * peers address lists.
961 *
962 * Adding and removing addresses from a connected association is
963 * optional functionality. Implementations that do not support this
964 * functionality should return EOPNOTSUPP.
965 *
966 * Basically do nothing but copying the addresses from user to kernel
967 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700968 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
969 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 *
971 * We don't use copy_from_user() for optimization: we first do the
972 * sanity checks (buffer size -fast- and access check-healthy
973 * pointer); if all of those succeed, then we can alloc the memory
974 * (expensive operation) needed to copy the data to kernel. Then we do
975 * the copying without checking the user space area
976 * (__copy_from_user()).
977 *
978 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
979 * it.
980 *
981 * sk The sk of the socket
982 * addrs The pointer to the addresses in user land
983 * addrssize Size of the addrs buffer
984 * op Operation to perform (add or remove, see the flags of
985 * sctp_bindx)
986 *
987 * Returns 0 if ok, <0 errno code on error.
988 */
wangweidong26ac8e52013-12-23 12:16:51 +0800989static int sctp_setsockopt_bindx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +0200990 struct sockaddr __user *addrs,
991 int addrs_size, int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 struct sockaddr *kaddrs;
994 int err;
995 int addrcnt = 0;
996 int walk_size = 0;
997 struct sockaddr *sa_addr;
998 void *addr_buf;
999 struct sctp_af *af;
1000
Daniel Borkmannbb333812013-06-28 19:49:40 +02001001 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1002 __func__, sk, addrs, addrs_size, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 if (unlikely(addrs_size <= 0))
1005 return -EINVAL;
1006
1007 /* Check the user passed a healthy pointer. */
1008 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1009 return -EFAULT;
1010
1011 /* Alloc space for the address array in kernel memory. */
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02001012 kaddrs = kmalloc(addrs_size, GFP_USER | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (unlikely(!kaddrs))
1014 return -ENOMEM;
1015
1016 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1017 kfree(kaddrs);
1018 return -EFAULT;
1019 }
1020
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001021 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 addr_buf = kaddrs;
1023 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001024 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1025 kfree(kaddrs);
1026 return -EINVAL;
1027 }
1028
Joe Perchesea110732011-06-13 16:21:26 +00001029 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 af = sctp_get_af_specific(sa_addr->sa_family);
1031
1032 /* If the address family is not supported or if this address
1033 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1036 kfree(kaddrs);
1037 return -EINVAL;
1038 }
1039 addrcnt++;
1040 addr_buf += af->sockaddr_len;
1041 walk_size += af->sockaddr_len;
1042 }
1043
1044 /* Do the work. */
1045 switch (op) {
1046 case SCTP_BINDX_ADD_ADDR:
1047 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1048 if (err)
1049 goto out;
1050 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1051 break;
1052
1053 case SCTP_BINDX_REM_ADDR:
1054 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1055 if (err)
1056 goto out;
1057 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1058 break;
1059
1060 default:
1061 err = -EINVAL;
1062 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065out:
1066 kfree(kaddrs);
1067
1068 return err;
1069}
1070
Frank Filz3f7a87d2005-06-20 13:14:57 -07001071/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1072 *
1073 * Common routine for handling connect() and sctp_connectx().
1074 * Connect will come in with just a single address.
1075 */
wangweidong26ac8e52013-12-23 12:16:51 +08001076static int __sctp_connect(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001077 struct sockaddr *kaddrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001078 int addrs_size,
1079 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001080{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001081 struct net *net = sock_net(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001082 struct sctp_sock *sp;
1083 struct sctp_endpoint *ep;
1084 struct sctp_association *asoc = NULL;
1085 struct sctp_association *asoc2;
1086 struct sctp_transport *transport;
1087 union sctp_addr to;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001088 sctp_scope_t scope;
1089 long timeo;
1090 int err = 0;
1091 int addrcnt = 0;
1092 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001093 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001094 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001095 unsigned short port;
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001096 unsigned int f_flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001097
1098 sp = sctp_sk(sk);
1099 ep = sp->ep;
1100
1101 /* connect() cannot be done on a socket that is already in ESTABLISHED
1102 * state - UDP-style peeled off socket or a TCP-style socket that
1103 * is already connected.
1104 * It cannot be done even on a TCP-style listening socket.
1105 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03001106 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
Frank Filz3f7a87d2005-06-20 13:14:57 -07001107 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1108 err = -EISCONN;
1109 goto out_free;
1110 }
1111
1112 /* Walk through the addrs buffer and count the number of addresses. */
1113 addr_buf = kaddrs;
1114 while (walk_size < addrs_size) {
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001115 struct sctp_af *af;
1116
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001117 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1118 err = -EINVAL;
1119 goto out_free;
1120 }
1121
Joe Perchesea110732011-06-13 16:21:26 +00001122 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001123 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001124
1125 /* If the address family is not supported or if this address
1126 * causes the address buffer to overflow return EINVAL.
1127 */
1128 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1129 err = -EINVAL;
1130 goto out_free;
1131 }
1132
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001133 port = ntohs(sa_addr->v4.sin_port);
1134
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001135 /* Save current address so we can work with it */
1136 memcpy(&to, sa_addr, af->sockaddr_len);
1137
1138 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001139 if (err)
1140 goto out_free;
1141
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001142 /* Make sure the destination port is correctly set
1143 * in all addresses.
1144 */
Wei Yongjun524fba62013-04-03 03:02:28 +00001145 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1146 err = -EINVAL;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001147 goto out_free;
Wei Yongjun524fba62013-04-03 03:02:28 +00001148 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001149
1150 /* Check if there already is a matching association on the
1151 * endpoint (other than the one created here).
1152 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001153 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001154 if (asoc2 && asoc2 != asoc) {
1155 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1156 err = -EISCONN;
1157 else
1158 err = -EALREADY;
1159 goto out_free;
1160 }
1161
1162 /* If we could not find a matching association on the endpoint,
1163 * make sure that there is no peeled-off association matching
1164 * the peer address even on another socket.
1165 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001166 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001167 err = -EADDRNOTAVAIL;
1168 goto out_free;
1169 }
1170
1171 if (!asoc) {
1172 /* If a bind() or sctp_bindx() is not called prior to
1173 * an sctp_connectx() call, the system picks an
1174 * ephemeral port and will choose an address set
1175 * equivalent to binding with a wildcard address.
1176 */
1177 if (!ep->base.bind_addr.port) {
1178 if (sctp_autobind(sk)) {
1179 err = -EAGAIN;
1180 goto out_free;
1181 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001182 } else {
1183 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001184 * If an unprivileged user inherits a 1-many
1185 * style socket with open associations on a
1186 * privileged port, it MAY be permitted to
1187 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001188 * be permitted to open new associations.
1189 */
1190 if (ep->base.bind_addr.port < PROT_SOCK &&
Eric W. Biederman35946982012-11-16 03:03:12 +00001191 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) {
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001192 err = -EACCES;
1193 goto out_free;
1194 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001195 }
1196
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001197 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001198 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1199 if (!asoc) {
1200 err = -ENOMEM;
1201 goto out_free;
1202 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001203
1204 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1205 GFP_KERNEL);
1206 if (err < 0) {
1207 goto out_free;
1208 }
1209
Frank Filz3f7a87d2005-06-20 13:14:57 -07001210 }
1211
1212 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001213 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001214 SCTP_UNKNOWN);
1215 if (!transport) {
1216 err = -ENOMEM;
1217 goto out_free;
1218 }
1219
1220 addrcnt++;
1221 addr_buf += af->sockaddr_len;
1222 walk_size += af->sockaddr_len;
1223 }
1224
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001225 /* In case the user of sctp_connectx() wants an association
1226 * id back, assign one now.
1227 */
1228 if (assoc_id) {
1229 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1230 if (err < 0)
1231 goto out_free;
1232 }
1233
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001234 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001235 if (err < 0) {
1236 goto out_free;
1237 }
1238
1239 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001240 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001241 sp->pf->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001242 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001243
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001244 /* in-kernel sockets don't generally have a file allocated to them
1245 * if all they do is call sock_create_kern().
1246 */
1247 if (sk->sk_socket->file)
1248 f_flags = sk->sk_socket->file->f_flags;
1249
1250 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1251
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001252 if (assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001253 *assoc_id = asoc->assoc_id;
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001254 err = sctp_wait_for_connect(asoc, &timeo);
1255 /* Note: the asoc may be freed after the return of
1256 * sctp_wait_for_connect.
1257 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001258
1259 /* Don't free association on exit. */
1260 asoc = NULL;
1261
1262out_free:
Daniel Borkmannbb333812013-06-28 19:49:40 +02001263 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1264 __func__, asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001265
Neil Horman2eebc1e2012-07-16 09:13:51 +00001266 if (asoc) {
1267 /* sctp_primitive_ASSOCIATE may have added this association
1268 * To the hash table, try to unhash it, just in case, its a noop
1269 * if it wasn't hashed so we're safe
1270 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001271 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001272 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001273 return err;
1274}
1275
1276/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1277 *
1278 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001279 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1280 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001281 *
1282 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1283 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1284 * or IPv6 addresses.
1285 *
1286 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1287 * Section 3.1.2 for this usage.
1288 *
1289 * addrs is a pointer to an array of one or more socket addresses. Each
1290 * address is contained in its appropriate structure (i.e. struct
1291 * sockaddr_in or struct sockaddr_in6) the family of the address type
1292 * must be used to distengish the address length (note that this
1293 * representation is termed a "packed array" of addresses). The caller
1294 * specifies the number of addresses in the array with addrcnt.
1295 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001296 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1297 * the association id of the new association. On failure, sctp_connectx()
1298 * returns -1, and sets errno to the appropriate error code. The assoc_id
1299 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001300 *
1301 * For SCTP, the port given in each socket address must be the same, or
1302 * sctp_connectx() will fail, setting errno to EINVAL.
1303 *
1304 * An application can use sctp_connectx to initiate an association with
1305 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1306 * allows a caller to specify multiple addresses at which a peer can be
1307 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001308 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001309 * specifies that the stack will try to make use of all the addresses in
1310 * the list when needed.
1311 *
1312 * Note that the list of addresses passed in is only used for setting up
1313 * the association. It does not necessarily equal the set of addresses
1314 * the peer uses for the resulting association. If the caller wants to
1315 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1316 * retrieve them after the association has been set up.
1317 *
1318 * Basically do nothing but copying the addresses from user to kernel
1319 * land and invoking either sctp_connectx(). This is used for tunneling
1320 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1321 *
1322 * We don't use copy_from_user() for optimization: we first do the
1323 * sanity checks (buffer size -fast- and access check-healthy
1324 * pointer); if all of those succeed, then we can alloc the memory
1325 * (expensive operation) needed to copy the data to kernel. Then we do
1326 * the copying without checking the user space area
1327 * (__copy_from_user()).
1328 *
1329 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1330 * it.
1331 *
1332 * sk The sk of the socket
1333 * addrs The pointer to the addresses in user land
1334 * addrssize Size of the addrs buffer
1335 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001336 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001337 */
wangweidong26ac8e52013-12-23 12:16:51 +08001338static int __sctp_setsockopt_connectx(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001339 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001340 int addrs_size,
1341 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001342{
Frank Filz3f7a87d2005-06-20 13:14:57 -07001343 struct sockaddr *kaddrs;
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02001344 gfp_t gfp = GFP_KERNEL;
1345 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001346
Daniel Borkmannbb333812013-06-28 19:49:40 +02001347 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1348 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001349
1350 if (unlikely(addrs_size <= 0))
1351 return -EINVAL;
1352
1353 /* Check the user passed a healthy pointer. */
1354 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1355 return -EFAULT;
1356
1357 /* Alloc space for the address array in kernel memory. */
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02001358 if (sk->sk_socket->file)
1359 gfp = GFP_USER | __GFP_NOWARN;
1360 kaddrs = kmalloc(addrs_size, gfp);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001361 if (unlikely(!kaddrs))
1362 return -ENOMEM;
1363
1364 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1365 err = -EFAULT;
1366 } else {
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001367 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001368 }
1369
1370 kfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001371
Frank Filz3f7a87d2005-06-20 13:14:57 -07001372 return err;
1373}
1374
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001375/*
1376 * This is an older interface. It's kept for backward compatibility
1377 * to the option that doesn't provide association id.
1378 */
wangweidong26ac8e52013-12-23 12:16:51 +08001379static int sctp_setsockopt_connectx_old(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001380 struct sockaddr __user *addrs,
1381 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001382{
1383 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1384}
1385
1386/*
1387 * New interface for the API. The since the API is done with a socket
1388 * option, to make it simple we feed back the association id is as a return
1389 * indication to the call. Error is always negative and association id is
1390 * always positive.
1391 */
wangweidong26ac8e52013-12-23 12:16:51 +08001392static int sctp_setsockopt_connectx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001393 struct sockaddr __user *addrs,
1394 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001395{
1396 sctp_assoc_t assoc_id = 0;
1397 int err = 0;
1398
1399 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1400
1401 if (err)
1402 return err;
1403 else
1404 return assoc_id;
1405}
1406
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001407/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001408 * New (hopefully final) interface for the API.
1409 * We use the sctp_getaddrs_old structure so that use-space library
Daniel Borkmannffd59392014-02-17 12:11:11 +01001410 * can avoid any unnecessary allocations. The only different part
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001411 * is that we store the actual length of the address buffer into the
Daniel Borkmannffd59392014-02-17 12:11:11 +01001412 * addrs_num structure member. That way we can re-use the existing
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001413 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001414 */
Daniel Borkmannffd59392014-02-17 12:11:11 +01001415#ifdef CONFIG_COMPAT
1416struct compat_sctp_getaddrs_old {
1417 sctp_assoc_t assoc_id;
1418 s32 addr_num;
1419 compat_uptr_t addrs; /* struct sockaddr * */
1420};
1421#endif
1422
wangweidong26ac8e52013-12-23 12:16:51 +08001423static int sctp_getsockopt_connectx3(struct sock *sk, int len,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001424 char __user *optval,
1425 int __user *optlen)
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001426{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001427 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001428 sctp_assoc_t assoc_id = 0;
1429 int err = 0;
1430
Daniel Borkmannffd59392014-02-17 12:11:11 +01001431#ifdef CONFIG_COMPAT
Andy Lutomirski96c0e0a2016-03-22 14:25:07 -07001432 if (in_compat_syscall()) {
Daniel Borkmannffd59392014-02-17 12:11:11 +01001433 struct compat_sctp_getaddrs_old param32;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001434
Daniel Borkmannffd59392014-02-17 12:11:11 +01001435 if (len < sizeof(param32))
1436 return -EINVAL;
1437 if (copy_from_user(&param32, optval, sizeof(param32)))
1438 return -EFAULT;
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001439
Daniel Borkmannffd59392014-02-17 12:11:11 +01001440 param.assoc_id = param32.assoc_id;
1441 param.addr_num = param32.addr_num;
1442 param.addrs = compat_ptr(param32.addrs);
1443 } else
1444#endif
1445 {
1446 if (len < sizeof(param))
1447 return -EINVAL;
1448 if (copy_from_user(&param, optval, sizeof(param)))
1449 return -EFAULT;
1450 }
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001451
Daniel Borkmannffd59392014-02-17 12:11:11 +01001452 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1453 param.addrs, param.addr_num,
1454 &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001455 if (err == 0 || err == -EINPROGRESS) {
1456 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1457 return -EFAULT;
1458 if (put_user(sizeof(assoc_id), optlen))
1459 return -EFAULT;
1460 }
1461
1462 return err;
1463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465/* API 3.1.4 close() - UDP Style Syntax
1466 * Applications use close() to perform graceful shutdown (as described in
1467 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1468 * by a UDP-style socket.
1469 *
1470 * The syntax is
1471 *
1472 * ret = close(int sd);
1473 *
1474 * sd - the socket descriptor of the associations to be closed.
1475 *
1476 * To gracefully shutdown a specific association represented by the
1477 * UDP-style socket, an application should use the sendmsg() call,
1478 * passing no user data, but including the appropriate flag in the
1479 * ancillary data (see Section xxxx).
1480 *
1481 * If sd in the close() call is a branched-off socket representing only
1482 * one association, the shutdown is performed on that association only.
1483 *
1484 * 4.1.6 close() - TCP Style Syntax
1485 *
1486 * Applications use close() to gracefully close down an association.
1487 *
1488 * The syntax is:
1489 *
1490 * int close(int sd);
1491 *
1492 * sd - the socket descriptor of the association to be closed.
1493 *
1494 * After an application calls close() on a socket descriptor, no further
1495 * socket operations will succeed on that descriptor.
1496 *
1497 * API 7.1.4 SO_LINGER
1498 *
1499 * An application using the TCP-style socket can use this option to
1500 * perform the SCTP ABORT primitive. The linger option structure is:
1501 *
1502 * struct linger {
1503 * int l_onoff; // option on/off
1504 * int l_linger; // linger time
1505 * };
1506 *
1507 * To enable the option, set l_onoff to 1. If the l_linger value is set
1508 * to 0, calling close() is the same as the ABORT primitive. If the
1509 * value is set to a negative value, the setsockopt() call will return
1510 * an error. If the value is set to a positive value linger_time, the
1511 * close() can be blocked for at most linger_time ms. If the graceful
1512 * shutdown phase does not finish during this period, close() will
1513 * return but the graceful shutdown phase continues in the system.
1514 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02001515static void sctp_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001517 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 struct sctp_endpoint *ep;
1519 struct sctp_association *asoc;
1520 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001521 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Daniel Borkmannbb333812013-06-28 19:49:40 +02001523 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Xin Long2c7608e2017-06-10 14:56:56 +08001525 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 sk->sk_shutdown = SHUTDOWN_MASK;
Vlad Yasevichbec96402009-07-30 18:08:28 -04001527 sk->sk_state = SCTP_SS_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 ep = sctp_sk(sk)->ep;
1530
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001531 /* Clean up any skbs sitting on the receive queue. */
1532 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1533 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1534
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001535 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 list_for_each_safe(pos, temp, &ep->asocs) {
1537 asoc = list_entry(pos, struct sctp_association, asocs);
1538
1539 if (sctp_style(sk, TCP)) {
1540 /* A closed association can still be in the list if
1541 * it belongs to a TCP-style listening socket that is
1542 * not yet accepted. If so, free it. If not, send an
1543 * ABORT or SHUTDOWN based on the linger options.
1544 */
1545 if (sctp_state(asoc, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001547 continue;
1548 }
1549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001551 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1552 !skb_queue_empty(&asoc->ulpq.reasm) ||
1553 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001554 struct sctp_chunk *chunk;
1555
1556 chunk = sctp_make_abort_user(asoc, NULL, 0);
Xin Long068d8bd2015-12-29 17:49:25 +08001557 sctp_primitive_ABORT(net, asoc, chunk);
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001558 } else
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001559 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 /* On a TCP-style socket, block for at most linger_time if set. */
1563 if (sctp_style(sk, TCP) && timeout)
1564 sctp_wait_for_close(sk, timeout);
1565
1566 /* This will run the backlog queue. */
wangweidong048ed4b2014-01-21 15:44:11 +08001567 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 /* Supposedly, no process has access to the socket, but
1570 * the net layers still may.
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001571 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1572 * held and that should be grabbed before socket lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001574 spin_lock_bh(&net->sctp.addr_wq_lock);
Xin Long2c7608e2017-06-10 14:56:56 +08001575 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 /* Hold the sock, since sk_common_release() will put sock_put()
1578 * and we have just a little more cleanup.
1579 */
1580 sock_hold(sk);
1581 sk_common_release(sk);
1582
wangweidong5bc1d1b2014-01-21 15:44:12 +08001583 bh_unlock_sock(sk);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001584 spin_unlock_bh(&net->sctp.addr_wq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 sock_put(sk);
1587
1588 SCTP_DBG_OBJCNT_DEC(sock);
1589}
1590
1591/* Handle EPIPE error. */
1592static int sctp_error(struct sock *sk, int flags, int err)
1593{
1594 if (err == -EPIPE)
1595 err = sock_error(sk) ? : -EPIPE;
1596 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1597 send_sig(SIGPIPE, current, 0);
1598 return err;
1599}
1600
1601/* API 3.1.3 sendmsg() - UDP Style Syntax
1602 *
1603 * An application uses sendmsg() and recvmsg() calls to transmit data to
1604 * and receive data from its peer.
1605 *
1606 * ssize_t sendmsg(int socket, const struct msghdr *message,
1607 * int flags);
1608 *
1609 * socket - the socket descriptor of the endpoint.
1610 * message - pointer to the msghdr structure which contains a single
1611 * user message and possibly some ancillary data.
1612 *
1613 * See Section 5 for complete description of the data
1614 * structures.
1615 *
1616 * flags - flags sent or received with the user message, see Section
1617 * 5 for complete description of the flags.
1618 *
1619 * Note: This function could use a rewrite especially when explicit
1620 * connect support comes in.
1621 */
1622/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1623
Daniel Borkmanndda91922013-06-17 11:40:05 +02001624static int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Ying Xue1b784142015-03-02 15:37:48 +08001626static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001628 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 struct sctp_sock *sp;
1630 struct sctp_endpoint *ep;
wangweidongcb3f8372013-12-23 12:16:50 +08001631 struct sctp_association *new_asoc = NULL, *asoc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 struct sctp_transport *transport, *chunk_tp;
1633 struct sctp_chunk *chunk;
Al Virodce116a2006-11-20 17:25:15 -08001634 union sctp_addr to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 struct sockaddr *msg_name = NULL;
Joe Perches517aa0b2011-05-12 11:27:20 +00001636 struct sctp_sndrcvinfo default_sinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 struct sctp_sndrcvinfo *sinfo;
1638 struct sctp_initmsg *sinit;
1639 sctp_assoc_t associd = 0;
1640 sctp_cmsgs_t cmsgs = { NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 sctp_scope_t scope;
Daniel Borkmann2061dcd2015-01-15 16:34:35 +01001642 bool fill_sinfo_ttl = false, wait_connect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 struct sctp_datamsg *datamsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 int msg_flags = msg->msg_flags;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001645 __u16 sinfo_flags = 0;
1646 long timeo;
1647 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 err = 0;
1650 sp = sctp_sk(sk);
1651 ep = sp->ep;
1652
Daniel Borkmannbb333812013-06-28 19:49:40 +02001653 pr_debug("%s: sk:%p, msg:%p, msg_len:%zu ep:%p\n", __func__, sk,
1654 msg, msg_len, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 /* We cannot send a message over a TCP-style listening socket. */
1657 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1658 err = -EPIPE;
1659 goto out_nounlock;
1660 }
1661
1662 /* Parse out the SCTP CMSGs. */
1663 err = sctp_msghdr_parse(msg, &cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (err) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001665 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 goto out_nounlock;
1667 }
1668
1669 /* Fetch the destination address for this packet. This
1670 * address only selects the association--it is not necessarily
1671 * the address we will send to.
1672 * For a peeled-off socket, msg_name is ignored.
1673 */
1674 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1675 int msg_namelen = msg->msg_namelen;
1676
1677 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1678 msg_namelen);
1679 if (err)
1680 return err;
1681
1682 if (msg_namelen > sizeof(to))
1683 msg_namelen = sizeof(to);
1684 memcpy(&to, msg->msg_name, msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 msg_name = msg->msg_name;
1686 }
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 sinit = cmsgs.init;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001689 if (cmsgs.sinfo != NULL) {
1690 memset(&default_sinfo, 0, sizeof(default_sinfo));
1691 default_sinfo.sinfo_stream = cmsgs.sinfo->snd_sid;
1692 default_sinfo.sinfo_flags = cmsgs.sinfo->snd_flags;
1693 default_sinfo.sinfo_ppid = cmsgs.sinfo->snd_ppid;
1694 default_sinfo.sinfo_context = cmsgs.sinfo->snd_context;
1695 default_sinfo.sinfo_assoc_id = cmsgs.sinfo->snd_assoc_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001697 sinfo = &default_sinfo;
1698 fill_sinfo_ttl = true;
1699 } else {
1700 sinfo = cmsgs.srinfo;
1701 }
1702 /* Did the user specify SNDINFO/SNDRCVINFO? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (sinfo) {
1704 sinfo_flags = sinfo->sinfo_flags;
1705 associd = sinfo->sinfo_assoc_id;
1706 }
1707
Daniel Borkmannbb333812013-06-28 19:49:40 +02001708 pr_debug("%s: msg_len:%zu, sinfo_flags:0x%x\n", __func__,
1709 msg_len, sinfo_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001711 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1712 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 err = -EINVAL;
1714 goto out_nounlock;
1715 }
1716
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001717 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1718 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1719 * If SCTP_ABORT is set, the message length could be non zero with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 * the msg_iov set to the user abort reason.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001721 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001722 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1723 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 err = -EINVAL;
1725 goto out_nounlock;
1726 }
1727
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001728 /* If SCTP_ADDR_OVER is set, there must be an address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 * specified in msg_name.
1730 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001731 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 err = -EINVAL;
1733 goto out_nounlock;
1734 }
1735
1736 transport = NULL;
1737
Daniel Borkmannbb333812013-06-28 19:49:40 +02001738 pr_debug("%s: about to look up association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
wangweidong048ed4b2014-01-21 15:44:11 +08001740 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 /* If a msg_name has been specified, assume this is to be used. */
1743 if (msg_name) {
1744 /* Look for a matching association on the endpoint. */
Al Virodce116a2006-11-20 17:25:15 -08001745 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03001746
1747 /* If we could not find a matching association on the
1748 * endpoint, make sure that it is not a TCP-style
1749 * socket that already has an association or there is
1750 * no peeled-off association on another socket.
1751 */
1752 if (!asoc &&
1753 ((sctp_style(sk, TCP) &&
1754 (sctp_sstate(sk, ESTABLISHED) ||
1755 sctp_sstate(sk, CLOSING))) ||
1756 sctp_endpoint_is_peeled_off(ep, &to))) {
1757 err = -EADDRNOTAVAIL;
1758 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
1760 } else {
1761 asoc = sctp_id2assoc(sk, associd);
1762 if (!asoc) {
1763 err = -EPIPE;
1764 goto out_unlock;
1765 }
1766 }
1767
1768 if (asoc) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001769 pr_debug("%s: just looked up association:%p\n", __func__, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1772 * socket that has an association in CLOSED state. This can
1773 * happen when an accepted socket has an association that is
1774 * already CLOSED.
1775 */
1776 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1777 err = -EPIPE;
1778 goto out_unlock;
1779 }
1780
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001781 if (sinfo_flags & SCTP_EOF) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001782 pr_debug("%s: shutting down association:%p\n",
1783 __func__, asoc);
1784
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001785 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 err = 0;
1787 goto out_unlock;
1788 }
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001789 if (sinfo_flags & SCTP_ABORT) {
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001790
1791 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1792 if (!chunk) {
1793 err = -ENOMEM;
1794 goto out_unlock;
1795 }
1796
Daniel Borkmannbb333812013-06-28 19:49:40 +02001797 pr_debug("%s: aborting association:%p\n",
1798 __func__, asoc);
1799
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001800 sctp_primitive_ABORT(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 err = 0;
1802 goto out_unlock;
1803 }
1804 }
1805
1806 /* Do we need to create the association? */
1807 if (!asoc) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001808 pr_debug("%s: there is no association yet\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001810 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 err = -EINVAL;
1812 goto out_unlock;
1813 }
1814
1815 /* Check for invalid stream against the stream counts,
1816 * either the default or the user specified stream counts.
1817 */
1818 if (sinfo) {
Dan Carpenter0e864b22014-01-13 16:46:08 +03001819 if (!sinit || !sinit->sinit_num_ostreams) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 /* Check against the defaults. */
1821 if (sinfo->sinfo_stream >=
1822 sp->initmsg.sinit_num_ostreams) {
1823 err = -EINVAL;
1824 goto out_unlock;
1825 }
1826 } else {
1827 /* Check against the requested. */
1828 if (sinfo->sinfo_stream >=
1829 sinit->sinit_num_ostreams) {
1830 err = -EINVAL;
1831 goto out_unlock;
1832 }
1833 }
1834 }
1835
1836 /*
1837 * API 3.1.2 bind() - UDP Style Syntax
1838 * If a bind() or sctp_bindx() is not called prior to a
1839 * sendmsg() call that initiates a new association, the
1840 * system picks an ephemeral port and will choose an address
1841 * set equivalent to binding with a wildcard address.
1842 */
1843 if (!ep->base.bind_addr.port) {
1844 if (sctp_autobind(sk)) {
1845 err = -EAGAIN;
1846 goto out_unlock;
1847 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001848 } else {
1849 /*
1850 * If an unprivileged user inherits a one-to-many
1851 * style socket with open associations on a privileged
1852 * port, it MAY be permitted to accept new associations,
1853 * but it SHOULD NOT be permitted to open new
1854 * associations.
1855 */
1856 if (ep->base.bind_addr.port < PROT_SOCK &&
Eric W. Biederman35946982012-11-16 03:03:12 +00001857 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) {
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001858 err = -EACCES;
1859 goto out_unlock;
1860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
1862
1863 scope = sctp_scope(&to);
1864 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1865 if (!new_asoc) {
1866 err = -ENOMEM;
1867 goto out_unlock;
1868 }
1869 asoc = new_asoc;
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001870 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1871 if (err < 0) {
1872 err = -ENOMEM;
1873 goto out_free;
1874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 /* If the SCTP_INIT ancillary data is specified, set all
1877 * the association init values accordingly.
1878 */
1879 if (sinit) {
1880 if (sinit->sinit_num_ostreams) {
1881 asoc->c.sinit_num_ostreams =
1882 sinit->sinit_num_ostreams;
1883 }
1884 if (sinit->sinit_max_instreams) {
1885 asoc->c.sinit_max_instreams =
1886 sinit->sinit_max_instreams;
1887 }
1888 if (sinit->sinit_max_attempts) {
1889 asoc->max_init_attempts
1890 = sinit->sinit_max_attempts;
1891 }
1892 if (sinit->sinit_max_init_timeo) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001893 asoc->max_init_timeo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1895 }
1896 }
1897
1898 /* Prime the peer's transport structures. */
Al Virodce116a2006-11-20 17:25:15 -08001899 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (!transport) {
1901 err = -ENOMEM;
1902 goto out_free;
1903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905
1906 /* ASSERT: we have a valid association at this point. */
Daniel Borkmannbb333812013-06-28 19:49:40 +02001907 pr_debug("%s: we have a valid association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 if (!sinfo) {
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001910 /* If the user didn't specify SNDINFO/SNDRCVINFO, make up
1911 * one with some defaults.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 */
Joe Perches517aa0b2011-05-12 11:27:20 +00001913 memset(&default_sinfo, 0, sizeof(default_sinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 default_sinfo.sinfo_stream = asoc->default_stream;
1915 default_sinfo.sinfo_flags = asoc->default_flags;
1916 default_sinfo.sinfo_ppid = asoc->default_ppid;
1917 default_sinfo.sinfo_context = asoc->default_context;
1918 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1919 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 sinfo = &default_sinfo;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001922 } else if (fill_sinfo_ttl) {
1923 /* In case SNDINFO was specified, we still need to fill
1924 * it with a default ttl from the assoc here.
1925 */
1926 sinfo->sinfo_timetolive = asoc->default_timetolive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928
1929 /* API 7.1.7, the sndbuf size per association bounds the
1930 * maximum size of data that can be sent in a single send call.
1931 */
1932 if (msg_len > sk->sk_sndbuf) {
1933 err = -EMSGSIZE;
1934 goto out_free;
1935 }
1936
Vlad Yasevich8a479492007-06-07 14:21:05 -04001937 if (asoc->pmtu_pending)
David S. Miller02f3d4c2012-07-16 03:57:14 -07001938 sctp_assoc_pending_pmtu(sk, asoc);
Vlad Yasevich8a479492007-06-07 14:21:05 -04001939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 /* If fragmentation is disabled and the message length exceeds the
1941 * association fragmentation point, return EMSGSIZE. The I-D
1942 * does not specify what this error is, but this looks like
1943 * a great fit.
1944 */
1945 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1946 err = -EMSGSIZE;
1947 goto out_free;
1948 }
1949
Joe Perchesafd76142011-05-12 09:19:10 +00001950 /* Check for invalid stream. */
1951 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1952 err = -EINVAL;
1953 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955
Xin Long8dbdf1f2016-07-09 19:47:45 +08001956 if (sctp_wspace(asoc) < msg_len)
1957 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1960 if (!sctp_wspace(asoc)) {
Xin Long9904da52017-11-15 16:57:26 +08001961 /* sk can be changed by peel off when waiting for buf. */
Xin Long2f056e72018-01-15 17:01:36 +08001962 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
Xin Long1158ecd52017-11-15 16:55:54 +08001963 if (err) {
1964 if (err == -ESRCH) {
1965 /* asoc is already dead. */
1966 new_asoc = NULL;
1967 err = -EPIPE;
1968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 goto out_free;
Xin Long1158ecd52017-11-15 16:55:54 +08001970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972
1973 /* If an address is passed with the sendto/sendmsg call, it is used
1974 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001975 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 */
1977 if ((sctp_style(sk, TCP) && msg_name) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001978 (sinfo_flags & SCTP_ADDR_OVER)) {
Al Virodce116a2006-11-20 17:25:15 -08001979 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (!chunk_tp) {
1981 err = -EINVAL;
1982 goto out_free;
1983 }
1984 } else
1985 chunk_tp = NULL;
1986
1987 /* Auto-connect, if we aren't connected already. */
1988 if (sctp_state(asoc, CLOSED)) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001989 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (err < 0)
1991 goto out_free;
Daniel Borkmannbb333812013-06-28 19:49:40 +02001992
Daniel Borkmann2061dcd2015-01-15 16:34:35 +01001993 wait_connect = true;
Daniel Borkmannbb333812013-06-28 19:49:40 +02001994 pr_debug("%s: we associated primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996
1997 /* Break the message into multiple chunks of maximum size. */
Al Viroc0371da2014-11-24 10:42:55 -05001998 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
Tommi Rantala6e51fe72012-11-22 03:23:16 +00001999 if (IS_ERR(datamsg)) {
2000 err = PTR_ERR(datamsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 goto out_free;
2002 }
2003
2004 /* Now send the (possibly) fragmented message. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002005 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
Xin Longb61c6542016-09-14 02:04:20 +08002006 sctp_chunk_hold(chunk);
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 /* Do accounting for the write space. */
2009 sctp_set_owner_w(chunk);
2010
2011 chunk->transport = chunk_tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04002014 /* Send it to the lower layers. Note: all chunks
2015 * must either fail or succeed. The lower layer
2016 * works that way today. Keep it that way or this
2017 * breaks.
2018 */
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002019 err = sctp_primitive_SEND(net, asoc, datamsg);
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04002020 /* Did the lower layer accept the chunk? */
Xin Longb61c6542016-09-14 02:04:20 +08002021 if (err) {
2022 sctp_datamsg_free(datamsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 goto out_free;
Xin Longb61c6542016-09-14 02:04:20 +08002024 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02002025
2026 pr_debug("%s: we sent primitively\n", __func__);
2027
Xin Longb61c6542016-09-14 02:04:20 +08002028 sctp_datamsg_put(datamsg);
Daniel Borkmannbb333812013-06-28 19:49:40 +02002029 err = msg_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Daniel Borkmann2061dcd2015-01-15 16:34:35 +01002031 if (unlikely(wait_connect)) {
2032 timeo = sock_sndtimeo(sk, msg_flags & MSG_DONTWAIT);
2033 sctp_wait_for_connect(asoc, &timeo);
2034 }
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /* If we are already past ASSOCIATE, the lower
2037 * layers are responsible for association cleanup.
2038 */
2039 goto out_unlock;
2040
2041out_free:
Xin Longb5eff712015-12-30 23:50:49 +08002042 if (new_asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 sctp_association_free(asoc);
2044out_unlock:
wangweidong048ed4b2014-01-21 15:44:11 +08002045 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047out_nounlock:
2048 return sctp_error(sk, msg_flags, err);
2049
2050#if 0
2051do_sock_err:
2052 if (msg_len)
2053 err = msg_len;
2054 else
2055 err = sock_error(sk);
2056 goto out;
2057
2058do_interrupted:
2059 if (msg_len)
2060 err = msg_len;
2061 goto out;
2062#endif /* 0 */
2063}
2064
2065/* This is an extended version of skb_pull() that removes the data from the
2066 * start of a skb even when data is spread across the list of skb's in the
2067 * frag_list. len specifies the total amount of data that needs to be removed.
2068 * when 'len' bytes could be removed from the skb, it returns 0.
2069 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2070 * could not be removed.
2071 */
2072static int sctp_skb_pull(struct sk_buff *skb, int len)
2073{
2074 struct sk_buff *list;
2075 int skb_len = skb_headlen(skb);
2076 int rlen;
2077
2078 if (len <= skb_len) {
2079 __skb_pull(skb, len);
2080 return 0;
2081 }
2082 len -= skb_len;
2083 __skb_pull(skb, skb_len);
2084
David S. Miller1b003be2009-06-09 00:22:35 -07002085 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 rlen = sctp_skb_pull(list, len);
2087 skb->len -= (len-rlen);
2088 skb->data_len -= (len-rlen);
2089
2090 if (!rlen)
2091 return 0;
2092
2093 len = rlen;
2094 }
2095
2096 return len;
2097}
2098
2099/* API 3.1.3 recvmsg() - UDP Style Syntax
2100 *
2101 * ssize_t recvmsg(int socket, struct msghdr *message,
2102 * int flags);
2103 *
2104 * socket - the socket descriptor of the endpoint.
2105 * message - pointer to the msghdr structure which contains a single
2106 * user message and possibly some ancillary data.
2107 *
2108 * See Section 5 for complete description of the data
2109 * structures.
2110 *
2111 * flags - flags sent or received with the user message, see Section
2112 * 5 for complete description of the flags.
2113 */
Ying Xue1b784142015-03-02 15:37:48 +08002114static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2115 int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
2117 struct sctp_ulpevent *event = NULL;
2118 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002119 struct sk_buff *skb, *head_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 int copied;
2121 int err = 0;
2122 int skb_len;
2123
Daniel Borkmannbb333812013-06-28 19:49:40 +02002124 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2125 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2126 addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
wangweidong048ed4b2014-01-21 15:44:11 +08002128 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03002130 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
Xin Longe0878692016-07-30 14:14:41 +08002131 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 err = -ENOTCONN;
2133 goto out;
2134 }
2135
2136 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2137 if (!skb)
2138 goto out;
2139
2140 /* Get the total length of the skb including any skb's in the
2141 * frag_list.
2142 */
2143 skb_len = skb->len;
2144
2145 copied = skb_len;
2146 if (copied > len)
2147 copied = len;
2148
David S. Miller51f3d022014-11-05 16:46:40 -05002149 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 event = sctp_skb2event(skb);
2152
2153 if (err)
2154 goto out_free;
2155
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002156 if (event->chunk && event->chunk->head_skb)
2157 head_skb = event->chunk->head_skb;
2158 else
2159 head_skb = skb;
2160 sock_recv_ts_and_drops(msg, sk, head_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if (sctp_ulpevent_is_notification(event)) {
2162 msg->msg_flags |= MSG_NOTIFICATION;
2163 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2164 } else {
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002165 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
2167
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02002168 /* Check if we allow SCTP_NXTINFO. */
2169 if (sp->recvnxtinfo)
2170 sctp_ulpevent_read_nxtinfo(event, msg, sk);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002171 /* Check if we allow SCTP_RCVINFO. */
2172 if (sp->recvrcvinfo)
2173 sctp_ulpevent_read_rcvinfo(event, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 /* Check if we allow SCTP_SNDRCVINFO. */
2175 if (sp->subscribe.sctp_data_io_event)
2176 sctp_ulpevent_read_sndrcvinfo(event, msg);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 err = copied;
2179
2180 /* If skb's length exceeds the user's buffer, update the skb and
2181 * push it back to the receive_queue so that the next call to
2182 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2183 */
2184 if (skb_len > copied) {
2185 msg->msg_flags &= ~MSG_EOR;
2186 if (flags & MSG_PEEK)
2187 goto out_free;
2188 sctp_skb_pull(skb, copied);
2189 skb_queue_head(&sk->sk_receive_queue, skb);
2190
Daniel Borkmann362d5202014-04-14 21:45:17 +02002191 /* When only partial message is copied to the user, increase
2192 * rwnd by that amount. If all the data in the skb is read,
2193 * rwnd is updated when the event is freed.
2194 */
2195 if (!sctp_ulpevent_is_notification(event))
2196 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 goto out;
2198 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2199 (event->msg_flags & MSG_EOR))
2200 msg->msg_flags |= MSG_EOR;
2201 else
2202 msg->msg_flags &= ~MSG_EOR;
2203
2204out_free:
2205 if (flags & MSG_PEEK) {
2206 /* Release the skb reference acquired after peeking the skb in
2207 * sctp_skb_recv_datagram().
2208 */
2209 kfree_skb(skb);
2210 } else {
2211 /* Free the event which includes releasing the reference to
2212 * the owner of the skb, freeing the skb and updating the
2213 * rwnd.
2214 */
2215 sctp_ulpevent_free(event);
2216 }
2217out:
wangweidong048ed4b2014-01-21 15:44:11 +08002218 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return err;
2220}
2221
2222/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2223 *
2224 * This option is a on/off flag. If enabled no SCTP message
2225 * fragmentation will be performed. Instead if a message being sent
2226 * exceeds the current PMTU size, the message will NOT be sent and
2227 * instead a error will be indicated to the user.
2228 */
2229static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002230 char __user *optval,
2231 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
2233 int val;
2234
2235 if (optlen < sizeof(int))
2236 return -EINVAL;
2237
2238 if (get_user(val, (int __user *)optval))
2239 return -EFAULT;
2240
2241 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2242
2243 return 0;
2244}
2245
2246static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002247 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Wei Yongjun94912302011-07-02 09:28:04 +00002249 struct sctp_association *asoc;
2250 struct sctp_ulpevent *event;
2251
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002252 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return -EINVAL;
2254 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2255 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002256
Daniel Borkmannbbbea412014-07-12 20:30:40 +02002257 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
Wei Yongjun94912302011-07-02 09:28:04 +00002258 * if there is no data to be sent or retransmit, the stack will
2259 * immediately send up this notification.
2260 */
2261 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2262 &sctp_sk(sk)->subscribe)) {
2263 asoc = sctp_id2assoc(sk, 0);
2264
2265 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2266 event = sctp_ulpevent_make_sender_dry_event(asoc,
2267 GFP_ATOMIC);
2268 if (!event)
2269 return -ENOMEM;
2270
2271 sctp_ulpq_tail_event(&asoc->ulpq, event);
2272 }
2273 }
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return 0;
2276}
2277
2278/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2279 *
2280 * This socket option is applicable to the UDP-style socket only. When
2281 * set it will cause associations that are idle for more than the
2282 * specified number of seconds to automatically close. An association
2283 * being idle is defined an association that has NOT sent or received
2284 * user data. The special value of '0' indicates that no automatic
2285 * close of any associations should be performed. The option expects an
2286 * integer defining the number of seconds of idle time before an
2287 * association is closed.
2288 */
2289static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002290 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
2292 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman9f70f462013-12-10 06:48:15 -05002293 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 /* Applicable to UDP-style socket only */
2296 if (sctp_style(sk, TCP))
2297 return -EOPNOTSUPP;
2298 if (optlen != sizeof(int))
2299 return -EINVAL;
2300 if (copy_from_user(&sp->autoclose, optval, optlen))
2301 return -EFAULT;
2302
Neil Horman9f70f462013-12-10 06:48:15 -05002303 if (sp->autoclose > net->sctp.max_autoclose)
2304 sp->autoclose = net->sctp.max_autoclose;
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return 0;
2307}
2308
2309/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2310 *
2311 * Applications can enable or disable heartbeats for any peer address of
2312 * an association, modify an address's heartbeat interval, force a
2313 * heartbeat to be sent immediately, and adjust the address's maximum
2314 * number of retransmissions sent before an address is considered
2315 * unreachable. The following structure is used to access and modify an
2316 * address's parameters:
2317 *
2318 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002319 * sctp_assoc_t spp_assoc_id;
2320 * struct sockaddr_storage spp_address;
2321 * uint32_t spp_hbinterval;
2322 * uint16_t spp_pathmaxrxt;
2323 * uint32_t spp_pathmtu;
2324 * uint32_t spp_sackdelay;
2325 * uint32_t spp_flags;
2326 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002328 * spp_assoc_id - (one-to-many style socket) This is filled in the
2329 * application, and identifies the association for
2330 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 * spp_address - This specifies which address is of interest.
2332 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002333 * in milliseconds. If a value of zero
2334 * is present in this field then no changes are to
2335 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 * spp_pathmaxrxt - This contains the maximum number of
2337 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002338 * considered unreachable. If a value of zero
2339 * is present in this field then no changes are to
2340 * be made to this parameter.
2341 * spp_pathmtu - When Path MTU discovery is disabled the value
2342 * specified here will be the "fixed" path mtu.
2343 * Note that if the spp_address field is empty
2344 * then all associations on this address will
2345 * have this fixed path mtu set upon them.
2346 *
2347 * spp_sackdelay - When delayed sack is enabled, this value specifies
2348 * the number of milliseconds that sacks will be delayed
2349 * for. This value will apply to all addresses of an
2350 * association if the spp_address field is empty. Note
2351 * also, that if delayed sack is enabled and this
2352 * value is set to 0, no change is made to the last
2353 * recorded delayed sack timer value.
2354 *
2355 * spp_flags - These flags are used to control various features
2356 * on an association. The flag field may contain
2357 * zero or more of the following options.
2358 *
2359 * SPP_HB_ENABLE - Enable heartbeats on the
2360 * specified address. Note that if the address
2361 * field is empty all addresses for the association
2362 * have heartbeats enabled upon them.
2363 *
2364 * SPP_HB_DISABLE - Disable heartbeats on the
2365 * speicifed address. Note that if the address
2366 * field is empty all addresses for the association
2367 * will have their heartbeats disabled. Note also
2368 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2369 * mutually exclusive, only one of these two should
2370 * be specified. Enabling both fields will have
2371 * undetermined results.
2372 *
2373 * SPP_HB_DEMAND - Request a user initiated heartbeat
2374 * to be made immediately.
2375 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002376 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2377 * heartbeat delayis to be set to the value of 0
2378 * milliseconds.
2379 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002380 * SPP_PMTUD_ENABLE - This field will enable PMTU
2381 * discovery upon the specified address. Note that
2382 * if the address feild is empty then all addresses
2383 * on the association are effected.
2384 *
2385 * SPP_PMTUD_DISABLE - This field will disable PMTU
2386 * discovery upon the specified address. Note that
2387 * if the address feild is empty then all addresses
2388 * on the association are effected. Not also that
2389 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2390 * exclusive. Enabling both will have undetermined
2391 * results.
2392 *
2393 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2394 * on delayed sack. The time specified in spp_sackdelay
2395 * is used to specify the sack delay for this address. Note
2396 * that if spp_address is empty then all addresses will
2397 * enable delayed sack and take on the sack delay
2398 * value specified in spp_sackdelay.
2399 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2400 * off delayed sack. If the spp_address field is blank then
2401 * delayed sack is disabled for the entire association. Note
2402 * also that this field is mutually exclusive to
2403 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2404 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 */
Adrian Bunk16164362006-09-18 00:40:38 -07002406static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2407 struct sctp_transport *trans,
2408 struct sctp_association *asoc,
2409 struct sctp_sock *sp,
2410 int hb_change,
2411 int pmtud_change,
2412 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 int error;
2415
Frank Filz52ccb8e2005-12-22 11:36:46 -08002416 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002417 struct net *net = sock_net(trans->asoc->base.sk);
2418
2419 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if (error)
2421 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 }
2423
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002424 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2425 * this field is ignored. Note also that a value of zero indicates
2426 * the current setting should be left unchanged.
2427 */
2428 if (params->spp_flags & SPP_HB_ENABLE) {
2429
2430 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2431 * set. This lets us use 0 value when this flag
2432 * is set.
2433 */
2434 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2435 params->spp_hbinterval = 0;
2436
2437 if (params->spp_hbinterval ||
2438 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2439 if (trans) {
2440 trans->hbinterval =
2441 msecs_to_jiffies(params->spp_hbinterval);
2442 } else if (asoc) {
2443 asoc->hbinterval =
2444 msecs_to_jiffies(params->spp_hbinterval);
2445 } else {
2446 sp->hbinterval = params->spp_hbinterval;
2447 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002448 }
2449 }
2450
2451 if (hb_change) {
2452 if (trans) {
2453 trans->param_flags =
2454 (trans->param_flags & ~SPP_HB) | hb_change;
2455 } else if (asoc) {
2456 asoc->param_flags =
2457 (asoc->param_flags & ~SPP_HB) | hb_change;
2458 } else {
2459 sp->param_flags =
2460 (sp->param_flags & ~SPP_HB) | hb_change;
2461 }
2462 }
2463
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002464 /* When Path MTU discovery is disabled the value specified here will
2465 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2466 * include the flag SPP_PMTUD_DISABLE for this field to have any
2467 * effect).
2468 */
2469 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002470 if (trans) {
2471 trans->pathmtu = params->spp_pathmtu;
David S. Miller02f3d4c2012-07-16 03:57:14 -07002472 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002473 } else if (asoc) {
2474 asoc->pathmtu = params->spp_pathmtu;
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04002475 sctp_frag_point(asoc, params->spp_pathmtu);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002476 } else {
2477 sp->pathmtu = params->spp_pathmtu;
2478 }
2479 }
2480
2481 if (pmtud_change) {
2482 if (trans) {
2483 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2484 (params->spp_flags & SPP_PMTUD_ENABLE);
2485 trans->param_flags =
2486 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2487 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002488 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
David S. Miller02f3d4c2012-07-16 03:57:14 -07002489 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002490 }
2491 } else if (asoc) {
2492 asoc->param_flags =
2493 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2494 } else {
2495 sp->param_flags =
2496 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2497 }
2498 }
2499
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002500 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2501 * value of this field is ignored. Note also that a value of zero
2502 * indicates the current setting should be left unchanged.
2503 */
2504 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002505 if (trans) {
2506 trans->sackdelay =
2507 msecs_to_jiffies(params->spp_sackdelay);
2508 } else if (asoc) {
2509 asoc->sackdelay =
2510 msecs_to_jiffies(params->spp_sackdelay);
2511 } else {
2512 sp->sackdelay = params->spp_sackdelay;
2513 }
2514 }
2515
2516 if (sackdelay_change) {
2517 if (trans) {
2518 trans->param_flags =
2519 (trans->param_flags & ~SPP_SACKDELAY) |
2520 sackdelay_change;
2521 } else if (asoc) {
2522 asoc->param_flags =
2523 (asoc->param_flags & ~SPP_SACKDELAY) |
2524 sackdelay_change;
2525 } else {
2526 sp->param_flags =
2527 (sp->param_flags & ~SPP_SACKDELAY) |
2528 sackdelay_change;
2529 }
2530 }
2531
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002532 /* Note that a value of zero indicates the current setting should be
2533 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002534 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002535 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002536 if (trans) {
2537 trans->pathmaxrxt = params->spp_pathmaxrxt;
2538 } else if (asoc) {
2539 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2540 } else {
2541 sp->pathmaxrxt = params->spp_pathmaxrxt;
2542 }
2543 }
2544
2545 return 0;
2546}
2547
2548static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002549 char __user *optval,
2550 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002551{
2552 struct sctp_paddrparams params;
2553 struct sctp_transport *trans = NULL;
2554 struct sctp_association *asoc = NULL;
2555 struct sctp_sock *sp = sctp_sk(sk);
2556 int error;
2557 int hb_change, pmtud_change, sackdelay_change;
2558
2559 if (optlen != sizeof(struct sctp_paddrparams))
wangweidongcb3f8372013-12-23 12:16:50 +08002560 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002561
2562 if (copy_from_user(&params, optval, optlen))
2563 return -EFAULT;
2564
2565 /* Validate flags and value parameters. */
2566 hb_change = params.spp_flags & SPP_HB;
2567 pmtud_change = params.spp_flags & SPP_PMTUD;
2568 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2569
2570 if (hb_change == SPP_HB ||
2571 pmtud_change == SPP_PMTUD ||
2572 sackdelay_change == SPP_SACKDELAY ||
2573 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002574 (params.spp_pathmtu &&
2575 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002576 return -EINVAL;
2577
2578 /* If an address other than INADDR_ANY is specified, and
2579 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 */
wangweidongcb3f8372013-12-23 12:16:50 +08002581 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002582 trans = sctp_addr_id2transport(sk, &params.spp_address,
2583 params.spp_assoc_id);
2584 if (!trans)
2585 return -EINVAL;
2586 }
2587
2588 /* Get association, if assoc_id != 0 and the socket is a one
2589 * to many style socket, and an association was not found, then
2590 * the id was invalid.
2591 */
2592 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2593 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2594 return -EINVAL;
2595
2596 /* Heartbeat demand can only be sent on a transport or
2597 * association, but not a socket.
2598 */
2599 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2600 return -EINVAL;
2601
2602 /* Process parameters. */
2603 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2604 hb_change, pmtud_change,
2605 sackdelay_change);
2606
2607 if (error)
2608 return error;
2609
2610 /* If changes are for association, also apply parameters to each
2611 * transport.
2612 */
2613 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002614 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2615 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002616 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2617 hb_change, pmtud_change,
2618 sackdelay_change);
2619 }
2620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622 return 0;
2623}
2624
wangweidong0ea5e4d2014-01-15 17:24:01 +08002625static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2626{
2627 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2628}
2629
2630static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2631{
2632 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2633}
2634
Wei Yongjund364d922008-05-09 15:13:26 -07002635/*
2636 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002637 *
Wei Yongjund364d922008-05-09 15:13:26 -07002638 * This option will effect the way delayed acks are performed. This
2639 * option allows you to get or set the delayed ack time, in
2640 * milliseconds. It also allows changing the delayed ack frequency.
2641 * Changing the frequency to 1 disables the delayed sack algorithm. If
2642 * the assoc_id is 0, then this sets or gets the endpoints default
2643 * values. If the assoc_id field is non-zero, then the set or get
2644 * effects the specified association for the one to many model (the
2645 * assoc_id field is ignored by the one to one model). Note that if
2646 * sack_delay or sack_freq are 0 when setting this option, then the
2647 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002648 *
Wei Yongjund364d922008-05-09 15:13:26 -07002649 * struct sctp_sack_info {
2650 * sctp_assoc_t sack_assoc_id;
2651 * uint32_t sack_delay;
2652 * uint32_t sack_freq;
2653 * };
Frank Filz77086102005-12-22 11:37:30 -08002654 *
Wei Yongjund364d922008-05-09 15:13:26 -07002655 * sack_assoc_id - This parameter, indicates which association the user
2656 * is performing an action upon. Note that if this field's value is
2657 * zero then the endpoints default value is changed (effecting future
2658 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002659 *
Wei Yongjund364d922008-05-09 15:13:26 -07002660 * sack_delay - This parameter contains the number of milliseconds that
2661 * the user is requesting the delayed ACK timer be set to. Note that
2662 * this value is defined in the standard to be between 200 and 500
2663 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002664 *
Wei Yongjund364d922008-05-09 15:13:26 -07002665 * sack_freq - This parameter contains the number of packets that must
2666 * be received before a sack is sent without waiting for the delay
2667 * timer to expire. The default value for this is 2, setting this
2668 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002669 */
2670
Wei Yongjund364d922008-05-09 15:13:26 -07002671static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002672 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002673{
Wei Yongjund364d922008-05-09 15:13:26 -07002674 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002675 struct sctp_transport *trans = NULL;
2676 struct sctp_association *asoc = NULL;
2677 struct sctp_sock *sp = sctp_sk(sk);
2678
Wei Yongjund364d922008-05-09 15:13:26 -07002679 if (optlen == sizeof(struct sctp_sack_info)) {
2680 if (copy_from_user(&params, optval, optlen))
2681 return -EFAULT;
2682
2683 if (params.sack_delay == 0 && params.sack_freq == 0)
2684 return 0;
2685 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05002686 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05002687 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05002688 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05002689 "Use struct sctp_sack_info instead\n",
2690 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07002691 if (copy_from_user(&params, optval, optlen))
2692 return -EFAULT;
2693
2694 if (params.sack_delay == 0)
2695 params.sack_freq = 1;
2696 else
2697 params.sack_freq = 0;
2698 } else
wangweidongcb3f8372013-12-23 12:16:50 +08002699 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08002700
Frank Filz77086102005-12-22 11:37:30 -08002701 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002702 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002703 return -EINVAL;
2704
Wei Yongjund364d922008-05-09 15:13:26 -07002705 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002706 * to many style socket, and an association was not found, then
2707 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002708 */
Wei Yongjund364d922008-05-09 15:13:26 -07002709 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2710 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002711 return -EINVAL;
2712
Wei Yongjund364d922008-05-09 15:13:26 -07002713 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002714 if (asoc) {
2715 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002716 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002717 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002718 sctp_spp_sackdelay_enable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002719 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002720 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002721 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002722 sctp_spp_sackdelay_enable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002723 }
Wei Yongjund364d922008-05-09 15:13:26 -07002724 }
2725
2726 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002727 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002728 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002729 sctp_spp_sackdelay_disable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002730 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002731 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002732 sctp_spp_sackdelay_disable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002733 }
Wei Yongjund364d922008-05-09 15:13:26 -07002734 } else if (params.sack_freq > 1) {
2735 if (asoc) {
2736 asoc->sackfreq = params.sack_freq;
2737 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002738 sctp_spp_sackdelay_enable(asoc->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002739 } else {
2740 sp->sackfreq = params.sack_freq;
2741 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002742 sctp_spp_sackdelay_enable(sp->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002743 }
Frank Filz77086102005-12-22 11:37:30 -08002744 }
2745
2746 /* If change is for association, also apply to each transport. */
2747 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002748 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2749 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002750 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002751 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002752 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002753 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002754 sctp_spp_sackdelay_enable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002755 }
Vlad Yasevich7bfe8bd2008-06-09 15:45:05 -07002756 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002757 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002758 sctp_spp_sackdelay_disable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002759 } else if (params.sack_freq > 1) {
2760 trans->sackfreq = params.sack_freq;
2761 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002762 sctp_spp_sackdelay_enable(trans->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002763 }
2764 }
2765 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002766
Frank Filz77086102005-12-22 11:37:30 -08002767 return 0;
2768}
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2771 *
2772 * Applications can specify protocol parameters for the default association
2773 * initialization. The option name argument to setsockopt() and getsockopt()
2774 * is SCTP_INITMSG.
2775 *
2776 * Setting initialization parameters is effective only on an unconnected
2777 * socket (for UDP-style sockets only future associations are effected
2778 * by the change). With TCP-style sockets, this option is inherited by
2779 * sockets derived from a listener socket.
2780 */
David S. Millerb7058842009-09-30 16:12:20 -07002781static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782{
2783 struct sctp_initmsg sinit;
2784 struct sctp_sock *sp = sctp_sk(sk);
2785
2786 if (optlen != sizeof(struct sctp_initmsg))
2787 return -EINVAL;
2788 if (copy_from_user(&sinit, optval, optlen))
2789 return -EFAULT;
2790
2791 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002792 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002794 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002796 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002798 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 return 0;
2801}
2802
2803/*
2804 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2805 *
2806 * Applications that wish to use the sendto() system call may wish to
2807 * specify a default set of parameters that would normally be supplied
2808 * through the inclusion of ancillary data. This socket option allows
2809 * such an application to set the default sctp_sndrcvinfo structure.
2810 * The application that wishes to use this socket option simply passes
2811 * in to this call the sctp_sndrcvinfo structure defined in Section
2812 * 5.2.2) The input parameters accepted by this call include
2813 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2814 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2815 * to this call if the caller is using the UDP model.
2816 */
2817static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002818 char __user *optval,
2819 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002822 struct sctp_association *asoc;
2823 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002825 if (optlen != sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return -EINVAL;
2827 if (copy_from_user(&info, optval, optlen))
2828 return -EFAULT;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002829 if (info.sinfo_flags &
2830 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2831 SCTP_ABORT | SCTP_EOF))
2832 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
2834 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2835 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2836 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 if (asoc) {
2838 asoc->default_stream = info.sinfo_stream;
2839 asoc->default_flags = info.sinfo_flags;
2840 asoc->default_ppid = info.sinfo_ppid;
2841 asoc->default_context = info.sinfo_context;
2842 asoc->default_timetolive = info.sinfo_timetolive;
2843 } else {
2844 sp->default_stream = info.sinfo_stream;
2845 sp->default_flags = info.sinfo_flags;
2846 sp->default_ppid = info.sinfo_ppid;
2847 sp->default_context = info.sinfo_context;
2848 sp->default_timetolive = info.sinfo_timetolive;
2849 }
2850
2851 return 0;
2852}
2853
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002854/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2855 * (SCTP_DEFAULT_SNDINFO)
2856 */
2857static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2858 char __user *optval,
2859 unsigned int optlen)
2860{
2861 struct sctp_sock *sp = sctp_sk(sk);
2862 struct sctp_association *asoc;
2863 struct sctp_sndinfo info;
2864
2865 if (optlen != sizeof(info))
2866 return -EINVAL;
2867 if (copy_from_user(&info, optval, optlen))
2868 return -EFAULT;
2869 if (info.snd_flags &
2870 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2871 SCTP_ABORT | SCTP_EOF))
2872 return -EINVAL;
2873
2874 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
2875 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
2876 return -EINVAL;
2877 if (asoc) {
2878 asoc->default_stream = info.snd_sid;
2879 asoc->default_flags = info.snd_flags;
2880 asoc->default_ppid = info.snd_ppid;
2881 asoc->default_context = info.snd_context;
2882 } else {
2883 sp->default_stream = info.snd_sid;
2884 sp->default_flags = info.snd_flags;
2885 sp->default_ppid = info.snd_ppid;
2886 sp->default_context = info.snd_context;
2887 }
2888
2889 return 0;
2890}
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2893 *
2894 * Requests that the local SCTP stack use the enclosed peer address as
2895 * the association primary. The enclosed address must be one of the
2896 * association peer's addresses.
2897 */
2898static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002899 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
2901 struct sctp_prim prim;
2902 struct sctp_transport *trans;
2903
2904 if (optlen != sizeof(struct sctp_prim))
2905 return -EINVAL;
2906
2907 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2908 return -EFAULT;
2909
2910 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2911 if (!trans)
2912 return -EINVAL;
2913
2914 sctp_assoc_set_primary(trans->asoc, trans);
2915
2916 return 0;
2917}
2918
2919/*
2920 * 7.1.5 SCTP_NODELAY
2921 *
2922 * Turn on/off any Nagle-like algorithm. This means that packets are
2923 * generally sent as soon as possible and no unnecessary delays are
2924 * introduced, at the cost of more packets in the network. Expects an
2925 * integer boolean flag.
2926 */
2927static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002928 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
2930 int val;
2931
2932 if (optlen < sizeof(int))
2933 return -EINVAL;
2934 if (get_user(val, (int __user *)optval))
2935 return -EFAULT;
2936
2937 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2938 return 0;
2939}
2940
2941/*
2942 *
2943 * 7.1.1 SCTP_RTOINFO
2944 *
2945 * The protocol parameters used to initialize and bound retransmission
2946 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2947 * and modify these parameters.
2948 * All parameters are time values, in milliseconds. A value of 0, when
2949 * modifying the parameters, indicates that the current value should not
2950 * be changed.
2951 *
2952 */
David S. Millerb7058842009-09-30 16:12:20 -07002953static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2954{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 struct sctp_rtoinfo rtoinfo;
2956 struct sctp_association *asoc;
wangweidong85f935d2013-12-11 09:50:38 +08002957 unsigned long rto_min, rto_max;
2958 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
2960 if (optlen != sizeof (struct sctp_rtoinfo))
2961 return -EINVAL;
2962
2963 if (copy_from_user(&rtoinfo, optval, optlen))
2964 return -EFAULT;
2965
2966 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2967
2968 /* Set the values to the specific association */
2969 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2970 return -EINVAL;
2971
wangweidong85f935d2013-12-11 09:50:38 +08002972 rto_max = rtoinfo.srto_max;
2973 rto_min = rtoinfo.srto_min;
2974
2975 if (rto_max)
2976 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
2977 else
2978 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
2979
2980 if (rto_min)
2981 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
2982 else
2983 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
2984
2985 if (rto_min > rto_max)
2986 return -EINVAL;
2987
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (asoc) {
2989 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002990 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 msecs_to_jiffies(rtoinfo.srto_initial);
wangweidong85f935d2013-12-11 09:50:38 +08002992 asoc->rto_max = rto_max;
2993 asoc->rto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 } else {
2995 /* If there is no association or the association-id = 0
2996 * set the values to the endpoint.
2997 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 if (rtoinfo.srto_initial != 0)
2999 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
wangweidong85f935d2013-12-11 09:50:38 +08003000 sp->rtoinfo.srto_max = rto_max;
3001 sp->rtoinfo.srto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 }
3003
3004 return 0;
3005}
3006
3007/*
3008 *
3009 * 7.1.2 SCTP_ASSOCINFO
3010 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02003011 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 * of the association.
3013 * Returns an error if the new association retransmission value is
3014 * greater than the sum of the retransmission value of the peer.
3015 * See [SCTP] for more information.
3016 *
3017 */
David S. Millerb7058842009-09-30 16:12:20 -07003018static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
3020
3021 struct sctp_assocparams assocparams;
3022 struct sctp_association *asoc;
3023
3024 if (optlen != sizeof(struct sctp_assocparams))
3025 return -EINVAL;
3026 if (copy_from_user(&assocparams, optval, optlen))
3027 return -EFAULT;
3028
3029 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3030
3031 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3032 return -EINVAL;
3033
3034 /* Set the values to the specific association */
3035 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003036 if (assocparams.sasoc_asocmaxrxt != 0) {
3037 __u32 path_sum = 0;
3038 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003039 struct sctp_transport *peer_addr;
3040
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07003041 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3042 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003043 path_sum += peer_addr->pathmaxrxt;
3044 paths++;
3045 }
3046
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003047 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003048 * one path/transport. We do this because path
3049 * retransmissions are only counted when we have more
3050 * then one path.
3051 */
3052 if (paths > 1 &&
3053 assocparams.sasoc_asocmaxrxt > path_sum)
3054 return -EINVAL;
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003057 }
3058
Daniel Borkmann52db8822013-06-25 18:17:27 +02003059 if (assocparams.sasoc_cookie_life != 0)
3060 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 } else {
3062 /* Set the values to the endpoint */
3063 struct sctp_sock *sp = sctp_sk(sk);
3064
3065 if (assocparams.sasoc_asocmaxrxt != 0)
3066 sp->assocparams.sasoc_asocmaxrxt =
3067 assocparams.sasoc_asocmaxrxt;
3068 if (assocparams.sasoc_cookie_life != 0)
3069 sp->assocparams.sasoc_cookie_life =
3070 assocparams.sasoc_cookie_life;
3071 }
3072 return 0;
3073}
3074
3075/*
3076 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3077 *
3078 * This socket option is a boolean flag which turns on or off mapped V4
3079 * addresses. If this option is turned on and the socket is type
3080 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3081 * If this option is turned off, then no mapping will be done of V4
3082 * addresses and a user will receive both PF_INET6 and PF_INET type
3083 * addresses on the socket.
3084 */
David S. Millerb7058842009-09-30 16:12:20 -07003085static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086{
3087 int val;
3088 struct sctp_sock *sp = sctp_sk(sk);
3089
3090 if (optlen < sizeof(int))
3091 return -EINVAL;
3092 if (get_user(val, (int __user *)optval))
3093 return -EFAULT;
3094 if (val)
3095 sp->v4mapped = 1;
3096 else
3097 sp->v4mapped = 0;
3098
3099 return 0;
3100}
3101
3102/*
Wei Yongjune89c2092008-12-25 16:54:58 -08003103 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3104 * This option will get or set the maximum size to put in any outgoing
3105 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 * fragmented by SCTP into the specified size. Note that the underlying
3107 * SCTP implementation may fragment into smaller sized chunks when the
3108 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08003109 * the user. The default value for this option is '0' which indicates
3110 * the user is NOT limiting fragmentation and only the PMTU will effect
3111 * SCTP's choice of DATA chunk size. Note also that values set larger
3112 * than the maximum size of an IP datagram will effectively let SCTP
3113 * control fragmentation (i.e. the same as setting this option to 0).
3114 *
3115 * The following structure is used to access and modify this parameter:
3116 *
3117 * struct sctp_assoc_value {
3118 * sctp_assoc_t assoc_id;
3119 * uint32_t assoc_value;
3120 * };
3121 *
3122 * assoc_id: This parameter is ignored for one-to-one style sockets.
3123 * For one-to-many style sockets this parameter indicates which
3124 * association the user is performing an action upon. Note that if
3125 * this field's value is zero then the endpoints default value is
3126 * changed (effecting future associations only).
3127 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 */
David S. Millerb7058842009-09-30 16:12:20 -07003129static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
Xin Long2e671222017-11-17 14:11:11 +08003131 struct sctp_sock *sp = sctp_sk(sk);
Wei Yongjune89c2092008-12-25 16:54:58 -08003132 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 int val;
3135
Wei Yongjune89c2092008-12-25 16:54:58 -08003136 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003137 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003138 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003139 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003140 "Use struct sctp_assoc_value instead\n",
3141 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08003142 if (copy_from_user(&val, optval, optlen))
3143 return -EFAULT;
3144 params.assoc_id = 0;
3145 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3146 if (copy_from_user(&params, optval, optlen))
3147 return -EFAULT;
3148 val = params.assoc_value;
Xin Long2e671222017-11-17 14:11:11 +08003149 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 return -EINVAL;
Xin Long2e671222017-11-17 14:11:11 +08003151 }
Wei Yongjune89c2092008-12-25 16:54:58 -08003152
Xin Long2e671222017-11-17 14:11:11 +08003153 if (val) {
3154 int min_len, max_len;
3155
3156 min_len = SCTP_DEFAULT_MINSEGMENT - sp->pf->af->net_header_len;
3157 min_len -= sizeof(struct sctphdr) +
3158 sizeof(struct sctp_data_chunk);
3159
3160 max_len = SCTP_MAX_CHUNK_LEN - sizeof(struct sctp_data_chunk);
3161
3162 if (val < min_len || val > max_len)
3163 return -EINVAL;
3164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Wei Yongjune89c2092008-12-25 16:54:58 -08003166 asoc = sctp_id2assoc(sk, params.assoc_id);
Wei Yongjune89c2092008-12-25 16:54:58 -08003167 if (asoc) {
3168 if (val == 0) {
Xin Long2e671222017-11-17 14:11:11 +08003169 val = asoc->pathmtu - sp->pf->af->net_header_len;
Wei Yongjune89c2092008-12-25 16:54:58 -08003170 val -= sizeof(struct sctphdr) +
Xin Long2e671222017-11-17 14:11:11 +08003171 sizeof(struct sctp_data_chunk);
Wei Yongjune89c2092008-12-25 16:54:58 -08003172 }
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003173 asoc->user_frag = val;
3174 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
Wei Yongjune89c2092008-12-25 16:54:58 -08003175 } else {
Xin Long2e671222017-11-17 14:11:11 +08003176 if (params.assoc_id && sctp_style(sk, UDP))
3177 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003178 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 }
3180
3181 return 0;
3182}
3183
3184
3185/*
3186 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3187 *
3188 * Requests that the peer mark the enclosed address as the association
3189 * primary. The enclosed address must be one of the association's
3190 * locally bound addresses. The following structure is used to make a
3191 * set primary request:
3192 */
3193static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003194 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003196 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 struct sctp_association *asoc = NULL;
3199 struct sctp_setpeerprim prim;
3200 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003201 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 int err;
3203
3204 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003206 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 return -EPERM;
3208
3209 if (optlen != sizeof(struct sctp_setpeerprim))
3210 return -EINVAL;
3211
3212 if (copy_from_user(&prim, optval, optlen))
3213 return -EFAULT;
3214
3215 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003216 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 return -EINVAL;
3218
3219 if (!asoc->peer.asconf_capable)
3220 return -EPERM;
3221
3222 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3223 return -EPERM;
3224
3225 if (!sctp_state(asoc, ESTABLISHED))
3226 return -ENOTCONN;
3227
Wei Yongjun40a01032010-12-07 17:11:09 +00003228 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3229 if (!af)
3230 return -EINVAL;
3231
3232 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3233 return -EADDRNOTAVAIL;
3234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3236 return -EADDRNOTAVAIL;
3237
3238 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3239 chunk = sctp_make_asconf_set_prim(asoc,
3240 (union sctp_addr *)&prim.sspp_addr);
3241 if (!chunk)
3242 return -ENOMEM;
3243
3244 err = sctp_send_asconf(asoc, chunk);
3245
Daniel Borkmannbb333812013-06-28 19:49:40 +02003246 pr_debug("%s: we set peer primary addr primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
3248 return err;
3249}
3250
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003251static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003252 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003254 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003256 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003258 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 return -EFAULT;
3260
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003261 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
3263 return 0;
3264}
3265
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003266/*
3267 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3268 *
3269 * The context field in the sctp_sndrcvinfo structure is normally only
3270 * used when a failed message is retrieved holding the value that was
3271 * sent down on the actual send call. This option allows the setting of
3272 * a default context on an association basis that will be received on
3273 * reading messages from the peer. This is especially helpful in the
3274 * one-2-many model for an application to keep some reference to an
3275 * internal state machine that is processing messages on the
3276 * association. Note that the setting of this value only effects
3277 * received messages from the peer and does not effect the value that is
3278 * saved with outbound messages.
3279 */
3280static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003281 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003282{
3283 struct sctp_assoc_value params;
3284 struct sctp_sock *sp;
3285 struct sctp_association *asoc;
3286
3287 if (optlen != sizeof(struct sctp_assoc_value))
3288 return -EINVAL;
3289 if (copy_from_user(&params, optval, optlen))
3290 return -EFAULT;
3291
3292 sp = sctp_sk(sk);
3293
3294 if (params.assoc_id != 0) {
3295 asoc = sctp_id2assoc(sk, params.assoc_id);
3296 if (!asoc)
3297 return -EINVAL;
3298 asoc->default_rcv_context = params.assoc_value;
3299 } else {
3300 sp->default_rcv_context = params.assoc_value;
3301 }
3302
3303 return 0;
3304}
3305
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003306/*
3307 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3308 *
3309 * This options will at a minimum specify if the implementation is doing
3310 * fragmented interleave. Fragmented interleave, for a one to many
3311 * socket, is when subsequent calls to receive a message may return
3312 * parts of messages from different associations. Some implementations
3313 * may allow you to turn this value on or off. If so, when turned off,
3314 * no fragment interleave will occur (which will cause a head of line
3315 * blocking amongst multiple associations sharing the same one to many
3316 * socket). When this option is turned on, then each receive call may
3317 * come from a different association (thus the user must receive data
3318 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3319 * association each receive belongs to.
3320 *
3321 * This option takes a boolean value. A non-zero value indicates that
3322 * fragmented interleave is on. A value of zero indicates that
3323 * fragmented interleave is off.
3324 *
3325 * Note that it is important that an implementation that allows this
3326 * option to be turned on, have it off by default. Otherwise an unaware
3327 * application using the one to many model may become confused and act
3328 * incorrectly.
3329 */
3330static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3331 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003332 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003333{
3334 int val;
3335
3336 if (optlen != sizeof(int))
3337 return -EINVAL;
3338 if (get_user(val, (int __user *)optval))
3339 return -EFAULT;
3340
3341 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
3342
3343 return 0;
3344}
3345
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003346/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003347 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003348 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003349 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003350 * This option will set or get the SCTP partial delivery point. This
3351 * point is the size of a message where the partial delivery API will be
3352 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003353 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003354 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003355 * point. Note also that the call will fail if the user attempts to set
3356 * this value larger than the socket receive buffer size.
3357 *
3358 * Note that any single message having a length smaller than or equal to
3359 * the SCTP partial delivery point will be delivered in one single read
3360 * call as long as the user provided buffer is large enough to hold the
3361 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003362 */
3363static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3364 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003365 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003366{
3367 u32 val;
3368
3369 if (optlen != sizeof(u32))
3370 return -EINVAL;
3371 if (get_user(val, (int __user *)optval))
3372 return -EFAULT;
3373
Wei Yongjun8510b932008-12-25 16:59:03 -08003374 /* Note: We double the receive buffer from what the user sets
3375 * it to be, also initial rwnd is based on rcvbuf/2.
3376 */
3377 if (val > (sk->sk_rcvbuf >> 1))
3378 return -EINVAL;
3379
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003380 sctp_sk(sk)->pd_point = val;
3381
3382 return 0; /* is this the right error code? */
3383}
3384
Vlad Yasevich70331572007-03-23 11:34:36 -07003385/*
3386 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3387 *
3388 * This option will allow a user to change the maximum burst of packets
3389 * that can be emitted by this association. Note that the default value
3390 * is 4, and some implementations may restrict this setting so that it
3391 * can only be lowered.
3392 *
3393 * NOTE: This text doesn't seem right. Do this on a socket basis with
3394 * future associations inheriting the socket value.
3395 */
3396static int sctp_setsockopt_maxburst(struct sock *sk,
3397 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003398 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003399{
Neil Horman219b99a2008-03-05 13:44:46 -08003400 struct sctp_assoc_value params;
3401 struct sctp_sock *sp;
3402 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003403 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003404 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003405
Neil Horman219b99a2008-03-05 13:44:46 -08003406 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003407 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003408 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003409 "Use of int in max_burst socket option deprecated.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003410 "Use struct sctp_assoc_value instead\n",
3411 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08003412 if (copy_from_user(&val, optval, optlen))
3413 return -EFAULT;
3414 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3415 if (copy_from_user(&params, optval, optlen))
3416 return -EFAULT;
3417 val = params.assoc_value;
3418 assoc_id = params.assoc_id;
3419 } else
3420 return -EINVAL;
3421
3422 sp = sctp_sk(sk);
3423
3424 if (assoc_id != 0) {
3425 asoc = sctp_id2assoc(sk, assoc_id);
3426 if (!asoc)
3427 return -EINVAL;
3428 asoc->max_burst = val;
3429 } else
3430 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003431
3432 return 0;
3433}
3434
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003435/*
3436 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3437 *
3438 * This set option adds a chunk type that the user is requesting to be
3439 * received only in an authenticated way. Changes to the list of chunks
3440 * will only effect future associations on the socket.
3441 */
3442static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003443 char __user *optval,
3444 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003445{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003446 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003447 struct sctp_authchunk val;
3448
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003449 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003450 return -EACCES;
3451
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003452 if (optlen != sizeof(struct sctp_authchunk))
3453 return -EINVAL;
3454 if (copy_from_user(&val, optval, optlen))
3455 return -EFAULT;
3456
3457 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003458 case SCTP_CID_INIT:
3459 case SCTP_CID_INIT_ACK:
3460 case SCTP_CID_SHUTDOWN_COMPLETE:
3461 case SCTP_CID_AUTH:
3462 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003463 }
3464
3465 /* add this chunk id to the endpoint */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003466 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003467}
3468
3469/*
3470 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3471 *
3472 * This option gets or sets the list of HMAC algorithms that the local
3473 * endpoint requires the peer to use.
3474 */
3475static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003476 char __user *optval,
3477 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003478{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003479 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003480 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003481 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003482 int err;
3483
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003484 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003485 return -EACCES;
3486
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003487 if (optlen < sizeof(struct sctp_hmacalgo))
3488 return -EINVAL;
3489
wangweidongcb3f8372013-12-23 12:16:50 +08003490 hmacs = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003491 if (IS_ERR(hmacs))
3492 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003493
Vlad Yasevichd9724052008-08-27 16:09:49 -07003494 idents = hmacs->shmac_num_idents;
3495 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3496 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003497 err = -EINVAL;
3498 goto out;
3499 }
3500
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003501 err = sctp_auth_ep_set_hmacs(ep, hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003502out:
3503 kfree(hmacs);
3504 return err;
3505}
3506
3507/*
3508 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3509 *
3510 * This option will set a shared secret key which is used to build an
3511 * association shared key.
3512 */
3513static int sctp_setsockopt_auth_key(struct sock *sk,
3514 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003515 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003516{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003517 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003518 struct sctp_authkey *authkey;
3519 struct sctp_association *asoc;
3520 int ret;
3521
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003522 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003523 return -EACCES;
3524
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003525 if (optlen <= sizeof(struct sctp_authkey))
3526 return -EINVAL;
3527
wangweidongcb3f8372013-12-23 12:16:50 +08003528 authkey = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003529 if (IS_ERR(authkey))
3530 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003531
Vlad Yasevich328fc472008-08-27 16:08:54 -07003532 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003533 ret = -EINVAL;
3534 goto out;
3535 }
3536
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003537 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3538 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3539 ret = -EINVAL;
3540 goto out;
3541 }
3542
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003543 ret = sctp_auth_set_key(ep, asoc, authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003544out:
Daniel Borkmann6ba542a2013-02-08 03:04:34 +00003545 kzfree(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003546 return ret;
3547}
3548
3549/*
3550 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3551 *
3552 * This option will get or set the active shared key to be used to build
3553 * the association shared key.
3554 */
3555static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003556 char __user *optval,
3557 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003558{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003559 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003560 struct sctp_authkeyid val;
3561 struct sctp_association *asoc;
3562
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003563 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003564 return -EACCES;
3565
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003566 if (optlen != sizeof(struct sctp_authkeyid))
3567 return -EINVAL;
3568 if (copy_from_user(&val, optval, optlen))
3569 return -EFAULT;
3570
3571 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3572 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3573 return -EINVAL;
3574
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003575 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003576}
3577
3578/*
3579 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3580 *
3581 * This set option will delete a shared secret key from use.
3582 */
3583static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003584 char __user *optval,
3585 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003586{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003587 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003588 struct sctp_authkeyid val;
3589 struct sctp_association *asoc;
3590
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003591 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003592 return -EACCES;
3593
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003594 if (optlen != sizeof(struct sctp_authkeyid))
3595 return -EINVAL;
3596 if (copy_from_user(&val, optval, optlen))
3597 return -EFAULT;
3598
3599 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3600 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3601 return -EINVAL;
3602
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003603 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003604
3605}
3606
Michio Honda7dc04d72011-04-26 20:16:31 +09003607/*
3608 * 8.1.23 SCTP_AUTO_ASCONF
3609 *
3610 * This option will enable or disable the use of the automatic generation of
3611 * ASCONF chunks to add and delete addresses to an existing association. Note
3612 * that this option has two caveats namely: a) it only affects sockets that
3613 * are bound to all addresses available to the SCTP stack, and b) the system
3614 * administrator may have an overriding control that turns the ASCONF feature
3615 * off no matter what setting the socket option may have.
3616 * This option expects an integer boolean flag, where a non-zero value turns on
3617 * the option, and a zero value turns off the option.
3618 * Note. In this implementation, socket operation overrides default parameter
3619 * being set by sysctl as well as FreeBSD implementation
3620 */
3621static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3622 unsigned int optlen)
3623{
3624 int val;
3625 struct sctp_sock *sp = sctp_sk(sk);
3626
3627 if (optlen < sizeof(int))
3628 return -EINVAL;
3629 if (get_user(val, (int __user *)optval))
3630 return -EFAULT;
3631 if (!sctp_is_ep_boundall(sk) && val)
3632 return -EINVAL;
3633 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3634 return 0;
3635
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003636 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003637 if (val == 0 && sp->do_auto_asconf) {
3638 list_del(&sp->auto_asconf_list);
3639 sp->do_auto_asconf = 0;
3640 } else if (val && !sp->do_auto_asconf) {
3641 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00003642 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09003643 sp->do_auto_asconf = 1;
3644 }
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003645 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003646 return 0;
3647}
3648
Neil Horman5aa93bc2012-07-21 07:56:07 +00003649/*
3650 * SCTP_PEER_ADDR_THLDS
3651 *
3652 * This option allows us to alter the partially failed threshold for one or all
3653 * transports in an association. See Section 6.1 of:
3654 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3655 */
3656static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3657 char __user *optval,
3658 unsigned int optlen)
3659{
3660 struct sctp_paddrthlds val;
3661 struct sctp_transport *trans;
3662 struct sctp_association *asoc;
3663
3664 if (optlen < sizeof(struct sctp_paddrthlds))
3665 return -EINVAL;
3666 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3667 sizeof(struct sctp_paddrthlds)))
3668 return -EFAULT;
3669
3670
3671 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3672 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3673 if (!asoc)
3674 return -ENOENT;
3675 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3676 transports) {
3677 if (val.spt_pathmaxrxt)
3678 trans->pathmaxrxt = val.spt_pathmaxrxt;
3679 trans->pf_retrans = val.spt_pathpfthld;
3680 }
3681
3682 if (val.spt_pathmaxrxt)
3683 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3684 asoc->pf_retrans = val.spt_pathpfthld;
3685 } else {
3686 trans = sctp_addr_id2transport(sk, &val.spt_address,
3687 val.spt_assoc_id);
3688 if (!trans)
3689 return -ENOENT;
3690
3691 if (val.spt_pathmaxrxt)
3692 trans->pathmaxrxt = val.spt_pathmaxrxt;
3693 trans->pf_retrans = val.spt_pathpfthld;
3694 }
3695
3696 return 0;
3697}
3698
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02003699static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3700 char __user *optval,
3701 unsigned int optlen)
3702{
3703 int val;
3704
3705 if (optlen < sizeof(int))
3706 return -EINVAL;
3707 if (get_user(val, (int __user *) optval))
3708 return -EFAULT;
3709
3710 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3711
3712 return 0;
3713}
3714
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02003715static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3716 char __user *optval,
3717 unsigned int optlen)
3718{
3719 int val;
3720
3721 if (optlen < sizeof(int))
3722 return -EINVAL;
3723 if (get_user(val, (int __user *) optval))
3724 return -EFAULT;
3725
3726 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3727
3728 return 0;
3729}
3730
Xin Long28aa4c22016-07-09 19:47:40 +08003731static int sctp_setsockopt_pr_supported(struct sock *sk,
3732 char __user *optval,
3733 unsigned int optlen)
3734{
3735 struct sctp_assoc_value params;
3736 struct sctp_association *asoc;
3737 int retval = -EINVAL;
3738
3739 if (optlen != sizeof(params))
3740 goto out;
3741
3742 if (copy_from_user(&params, optval, optlen)) {
3743 retval = -EFAULT;
3744 goto out;
3745 }
3746
3747 asoc = sctp_id2assoc(sk, params.assoc_id);
3748 if (asoc) {
3749 asoc->prsctp_enable = !!params.assoc_value;
3750 } else if (!params.assoc_id) {
3751 struct sctp_sock *sp = sctp_sk(sk);
3752
3753 sp->ep->prsctp_enable = !!params.assoc_value;
3754 } else {
3755 goto out;
3756 }
3757
3758 retval = 0;
3759
3760out:
3761 return retval;
3762}
3763
Xin Longf959fb42016-07-09 19:47:41 +08003764static int sctp_setsockopt_default_prinfo(struct sock *sk,
3765 char __user *optval,
3766 unsigned int optlen)
3767{
3768 struct sctp_default_prinfo info;
3769 struct sctp_association *asoc;
3770 int retval = -EINVAL;
3771
3772 if (optlen != sizeof(info))
3773 goto out;
3774
3775 if (copy_from_user(&info, optval, sizeof(info))) {
3776 retval = -EFAULT;
3777 goto out;
3778 }
3779
3780 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
3781 goto out;
3782
3783 if (info.pr_policy == SCTP_PR_SCTP_NONE)
3784 info.pr_value = 0;
3785
3786 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
3787 if (asoc) {
3788 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
3789 asoc->default_timetolive = info.pr_value;
3790 } else if (!info.pr_assoc_id) {
3791 struct sctp_sock *sp = sctp_sk(sk);
3792
3793 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
3794 sp->default_timetolive = info.pr_value;
3795 } else {
3796 goto out;
3797 }
3798
3799 retval = 0;
3800
3801out:
3802 return retval;
3803}
3804
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805/* API 6.2 setsockopt(), getsockopt()
3806 *
3807 * Applications use setsockopt() and getsockopt() to set or retrieve
3808 * socket options. Socket options are used to change the default
3809 * behavior of sockets calls. They are described in Section 7.
3810 *
3811 * The syntax is:
3812 *
3813 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3814 * int __user *optlen);
3815 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
3816 * int optlen);
3817 *
3818 * sd - the socket descript.
3819 * level - set to IPPROTO_SCTP for all SCTP options.
3820 * optname - the option name.
3821 * optval - the buffer to store the value of the option.
3822 * optlen - the size of the buffer.
3823 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02003824static int sctp_setsockopt(struct sock *sk, int level, int optname,
3825 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826{
3827 int retval = 0;
3828
Daniel Borkmannbb333812013-06-28 19:49:40 +02003829 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
3831 /* I can hardly begin to describe how wrong this is. This is
3832 * so broken as to be worse than useless. The API draft
3833 * REALLY is NOT helpful here... I am not convinced that the
3834 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
3835 * are at all well-founded.
3836 */
3837 if (level != SOL_SCTP) {
3838 struct sctp_af *af = sctp_sk(sk)->pf->af;
3839 retval = af->setsockopt(sk, level, optname, optval, optlen);
3840 goto out_nounlock;
3841 }
3842
wangweidong048ed4b2014-01-21 15:44:11 +08003843 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
3845 switch (optname) {
3846 case SCTP_SOCKOPT_BINDX_ADD:
3847 /* 'optlen' is the size of the addresses buffer. */
3848 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3849 optlen, SCTP_BINDX_ADD_ADDR);
3850 break;
3851
3852 case SCTP_SOCKOPT_BINDX_REM:
3853 /* 'optlen' is the size of the addresses buffer. */
3854 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3855 optlen, SCTP_BINDX_REM_ADDR);
3856 break;
3857
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003858 case SCTP_SOCKOPT_CONNECTX_OLD:
3859 /* 'optlen' is the size of the addresses buffer. */
3860 retval = sctp_setsockopt_connectx_old(sk,
3861 (struct sockaddr __user *)optval,
3862 optlen);
3863 break;
3864
Frank Filz3f7a87d2005-06-20 13:14:57 -07003865 case SCTP_SOCKOPT_CONNECTX:
3866 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003867 retval = sctp_setsockopt_connectx(sk,
3868 (struct sockaddr __user *)optval,
3869 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003870 break;
3871
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 case SCTP_DISABLE_FRAGMENTS:
3873 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3874 break;
3875
3876 case SCTP_EVENTS:
3877 retval = sctp_setsockopt_events(sk, optval, optlen);
3878 break;
3879
3880 case SCTP_AUTOCLOSE:
3881 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3882 break;
3883
3884 case SCTP_PEER_ADDR_PARAMS:
3885 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3886 break;
3887
Shan Wei4580ccc2011-01-18 22:39:00 +00003888 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07003889 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08003890 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003891 case SCTP_PARTIAL_DELIVERY_POINT:
3892 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3893 break;
Frank Filz77086102005-12-22 11:37:30 -08003894
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 case SCTP_INITMSG:
3896 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3897 break;
3898 case SCTP_DEFAULT_SEND_PARAM:
3899 retval = sctp_setsockopt_default_send_param(sk, optval,
3900 optlen);
3901 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003902 case SCTP_DEFAULT_SNDINFO:
3903 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
3904 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 case SCTP_PRIMARY_ADDR:
3906 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3907 break;
3908 case SCTP_SET_PEER_PRIMARY_ADDR:
3909 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3910 break;
3911 case SCTP_NODELAY:
3912 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3913 break;
3914 case SCTP_RTOINFO:
3915 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3916 break;
3917 case SCTP_ASSOCINFO:
3918 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3919 break;
3920 case SCTP_I_WANT_MAPPED_V4_ADDR:
3921 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3922 break;
3923 case SCTP_MAXSEG:
3924 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3925 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003926 case SCTP_ADAPTATION_LAYER:
3927 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003929 case SCTP_CONTEXT:
3930 retval = sctp_setsockopt_context(sk, optval, optlen);
3931 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003932 case SCTP_FRAGMENT_INTERLEAVE:
3933 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3934 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07003935 case SCTP_MAX_BURST:
3936 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3937 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003938 case SCTP_AUTH_CHUNK:
3939 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
3940 break;
3941 case SCTP_HMAC_IDENT:
3942 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
3943 break;
3944 case SCTP_AUTH_KEY:
3945 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
3946 break;
3947 case SCTP_AUTH_ACTIVE_KEY:
3948 retval = sctp_setsockopt_active_key(sk, optval, optlen);
3949 break;
3950 case SCTP_AUTH_DELETE_KEY:
3951 retval = sctp_setsockopt_del_key(sk, optval, optlen);
3952 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09003953 case SCTP_AUTO_ASCONF:
3954 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
3955 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00003956 case SCTP_PEER_ADDR_THLDS:
3957 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
3958 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02003959 case SCTP_RECVRCVINFO:
3960 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
3961 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02003962 case SCTP_RECVNXTINFO:
3963 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
3964 break;
Xin Long28aa4c22016-07-09 19:47:40 +08003965 case SCTP_PR_SUPPORTED:
3966 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
3967 break;
Xin Longf959fb42016-07-09 19:47:41 +08003968 case SCTP_DEFAULT_PRINFO:
3969 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
3970 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 default:
3972 retval = -ENOPROTOOPT;
3973 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975
wangweidong048ed4b2014-01-21 15:44:11 +08003976 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
3978out_nounlock:
3979 return retval;
3980}
3981
3982/* API 3.1.6 connect() - UDP Style Syntax
3983 *
3984 * An application may use the connect() call in the UDP model to initiate an
3985 * association without sending data.
3986 *
3987 * The syntax is:
3988 *
3989 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3990 *
3991 * sd: the socket descriptor to have a new association added to.
3992 *
3993 * nam: the address structure (either struct sockaddr_in or struct
3994 * sockaddr_in6 defined in RFC2553 [7]).
3995 *
3996 * len: the size of the address.
3997 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02003998static int sctp_connect(struct sock *sk, struct sockaddr *addr,
3999 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07004002 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
wangweidong048ed4b2014-01-21 15:44:11 +08004004 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Daniel Borkmannbb333812013-06-28 19:49:40 +02004006 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4007 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Frank Filz3f7a87d2005-06-20 13:14:57 -07004009 /* Validate addr_len before calling common connect/connectx routine. */
4010 af = sctp_get_af_specific(addr->sa_family);
4011 if (!af || addr_len < af->sockaddr_len) {
4012 err = -EINVAL;
4013 } else {
4014 /* Pass correct addr len to common routine (so it knows there
4015 * is only one address being passed.
4016 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004017 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 }
4019
wangweidong048ed4b2014-01-21 15:44:11 +08004020 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 return err;
4022}
4023
4024/* FIXME: Write comments. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004025static int sctp_disconnect(struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
4027 return -EOPNOTSUPP; /* STUB */
4028}
4029
4030/* 4.1.4 accept() - TCP Style Syntax
4031 *
4032 * Applications use accept() call to remove an established SCTP
4033 * association from the accept queue of the endpoint. A new socket
4034 * descriptor will be returned from accept() to represent the newly
4035 * formed association.
4036 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004037static struct sock *sctp_accept(struct sock *sk, int flags, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038{
4039 struct sctp_sock *sp;
4040 struct sctp_endpoint *ep;
4041 struct sock *newsk = NULL;
4042 struct sctp_association *asoc;
4043 long timeo;
4044 int error = 0;
4045
wangweidong048ed4b2014-01-21 15:44:11 +08004046 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
4048 sp = sctp_sk(sk);
4049 ep = sp->ep;
4050
4051 if (!sctp_style(sk, TCP)) {
4052 error = -EOPNOTSUPP;
4053 goto out;
4054 }
4055
4056 if (!sctp_sstate(sk, LISTENING)) {
4057 error = -EINVAL;
4058 goto out;
4059 }
4060
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07004061 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
4063 error = sctp_wait_for_accept(sk, timeo);
4064 if (error)
4065 goto out;
4066
4067 /* We treat the list of associations on the endpoint as the accept
4068 * queue and pick the first association on the list.
4069 */
4070 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4071
4072 newsk = sp->pf->create_accept_sk(sk, asoc);
4073 if (!newsk) {
4074 error = -ENOMEM;
4075 goto out;
4076 }
4077
4078 /* Populate the fields of the newsk from the oldsk and migrate the
4079 * asoc to the newsk.
4080 */
4081 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4082
4083out:
wangweidong048ed4b2014-01-21 15:44:11 +08004084 release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004085 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 return newsk;
4087}
4088
4089/* The SCTP ioctl handler. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004090static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004092 int rc = -ENOTCONN;
4093
wangweidong048ed4b2014-01-21 15:44:11 +08004094 lock_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004095
4096 /*
4097 * SEQPACKET-style sockets in LISTENING state are valid, for
4098 * SCTP, so only discard TCP-style sockets in LISTENING state.
4099 */
4100 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4101 goto out;
4102
4103 switch (cmd) {
4104 case SIOCINQ: {
4105 struct sk_buff *skb;
4106 unsigned int amount = 0;
4107
4108 skb = skb_peek(&sk->sk_receive_queue);
4109 if (skb != NULL) {
4110 /*
4111 * We will only return the amount of this packet since
4112 * that is all that will be read.
4113 */
4114 amount = skb->len;
4115 }
4116 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004117 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07004118 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004119 default:
4120 rc = -ENOIOCTLCMD;
4121 break;
4122 }
4123out:
wangweidong048ed4b2014-01-21 15:44:11 +08004124 release_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004125 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126}
4127
4128/* This is the function which gets called during socket creation to
4129 * initialized the SCTP-specific portion of the sock.
4130 * The sock structure should already be zero-filled memory.
4131 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004132static int sctp_init_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004134 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 struct sctp_sock *sp;
4136
Daniel Borkmannbb333812013-06-28 19:49:40 +02004137 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
4139 sp = sctp_sk(sk);
4140
4141 /* Initialize the SCTP per socket area. */
4142 switch (sk->sk_type) {
4143 case SOCK_SEQPACKET:
4144 sp->type = SCTP_SOCKET_UDP;
4145 break;
4146 case SOCK_STREAM:
4147 sp->type = SCTP_SOCKET_TCP;
4148 break;
4149 default:
4150 return -ESOCKTNOSUPPORT;
4151 }
4152
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004153 sk->sk_gso_type = SKB_GSO_SCTP;
4154
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 /* Initialize default send parameters. These parameters can be
4156 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4157 */
4158 sp->default_stream = 0;
4159 sp->default_ppid = 0;
4160 sp->default_flags = 0;
4161 sp->default_context = 0;
4162 sp->default_timetolive = 0;
4163
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004164 sp->default_rcv_context = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004165 sp->max_burst = net->sctp.max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004166
Neil Horman3c681982012-10-24 09:20:03 +00004167 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4168
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 /* Initialize default setup parameters. These parameters
4170 * can be modified with the SCTP_INITMSG socket option or
4171 * overridden by the SCTP_INIT CMSG.
4172 */
4173 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4174 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004175 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4176 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
4178 /* Initialize default RTO related parameters. These parameters can
4179 * be modified for with the SCTP_RTOINFO socket option.
4180 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004181 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4182 sp->rtoinfo.srto_max = net->sctp.rto_max;
4183 sp->rtoinfo.srto_min = net->sctp.rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
4185 /* Initialize default association related parameters. These parameters
4186 * can be modified with the SCTP_ASSOCINFO socket option.
4187 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004188 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 sp->assocparams.sasoc_number_peer_destinations = 0;
4190 sp->assocparams.sasoc_peer_rwnd = 0;
4191 sp->assocparams.sasoc_local_rwnd = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004192 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
4194 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004195 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 */
4197 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4198
4199 /* Default Peer Address Parameters. These defaults can
4200 * be modified via SCTP_PEER_ADDR_PARAMS
4201 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004202 sp->hbinterval = net->sctp.hb_interval;
4203 sp->pathmaxrxt = net->sctp.max_retrans_path;
wangweidong4e2d52b2013-12-23 12:16:54 +08004204 sp->pathmtu = 0; /* allow default discovery */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004205 sp->sackdelay = net->sctp.sack_timeout;
Vlad Yasevich7bfe8bd2008-06-09 15:45:05 -07004206 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004207 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004208 SPP_PMTUD_ENABLE |
4209 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
4211 /* If enabled no SCTP message fragmentation will be performed.
4212 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4213 */
4214 sp->disable_fragments = 0;
4215
Sridhar Samudrala208edef2006-09-29 17:08:01 -07004216 /* Enable Nagle algorithm by default. */
4217 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004219 sp->recvrcvinfo = 0;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004220 sp->recvnxtinfo = 0;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004221
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 /* Enable by default. */
4223 sp->v4mapped = 1;
4224
4225 /* Auto-close idle associations after the configured
4226 * number of seconds. A value of 0 disables this
4227 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4228 * for UDP-style sockets only.
4229 */
4230 sp->autoclose = 0;
4231
4232 /* User specified fragmentation limit. */
4233 sp->user_frag = 0;
4234
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004235 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
4237 sp->pf = sctp_get_pf_specific(sk->sk_family);
4238
4239 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004240 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004242 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
4244 /* Create a per socket endpoint structure. Even if we
4245 * change the data structure relationships, this may still
4246 * be useful for storing pre-connect address information.
4247 */
Daniel Borkmannc164b832013-06-14 18:24:06 +02004248 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4249 if (!sp->ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 return -ENOMEM;
4251
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 sp->hmac = NULL;
4253
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004254 sk->sk_destruct = sctp_destruct_sock;
4255
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08004257
4258 local_bh_disable();
Tonghao Zhangae67e542017-12-22 10:15:20 -08004259 sk_sockets_allocated_inc(sk);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004260 sock_prot_inuse_add(net, sk->sk_prot, 1);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004261
4262 /* Nothing can fail after this block, otherwise
4263 * sctp_destroy_sock() will be called without addr_wq_lock held
4264 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004265 if (net->sctp.default_auto_asconf) {
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004266 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda9f7d6532011-04-26 19:32:51 +09004267 list_add_tail(&sp->auto_asconf_list,
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004268 &net->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09004269 sp->do_auto_asconf = 1;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004270 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4271 } else {
Michio Honda9f7d6532011-04-26 19:32:51 +09004272 sp->do_auto_asconf = 0;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004273 }
4274
David S. Miller6f756a82008-11-23 17:34:03 -08004275 local_bh_enable();
4276
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 return 0;
4278}
4279
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004280/* Cleanup any SCTP per socket resources. Must be called with
4281 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4282 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004283static void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
Michio Honda9f7d6532011-04-26 19:32:51 +09004285 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Daniel Borkmannbb333812013-06-28 19:49:40 +02004287 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
4289 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09004290 sp = sctp_sk(sk);
Daniel Borkmann1abd1652013-06-06 15:53:47 +02004291 /* This could happen during socket init, thus we bail out
4292 * early, since the rest of the below is not setup either.
4293 */
4294 if (sp->ep == NULL)
4295 return;
4296
Michio Honda9f7d6532011-04-26 19:32:51 +09004297 if (sp->do_auto_asconf) {
4298 sp->do_auto_asconf = 0;
4299 list_del(&sp->auto_asconf_list);
4300 }
4301 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004302 local_bh_disable();
Tonghao Zhangae67e542017-12-22 10:15:20 -08004303 sk_sockets_allocated_dec(sk);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08004304 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004305 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306}
4307
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004308/* Triggered when there are no references on the socket anymore */
4309static void sctp_destruct_sock(struct sock *sk)
4310{
4311 struct sctp_sock *sp = sctp_sk(sk);
4312
4313 /* Free up the HMAC transform. */
Herbert Xu5821c762016-01-24 21:20:12 +08004314 crypto_free_shash(sp->hmac);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004315
4316 inet_sock_destruct(sk);
4317}
4318
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319/* API 4.1.7 shutdown() - TCP Style Syntax
4320 * int shutdown(int socket, int how);
4321 *
4322 * sd - the socket descriptor of the association to be closed.
4323 * how - Specifies the type of shutdown. The values are
4324 * as follows:
4325 * SHUT_RD
4326 * Disables further receive operations. No SCTP
4327 * protocol action is taken.
4328 * SHUT_WR
4329 * Disables further send operations, and initiates
4330 * the SCTP shutdown sequence.
4331 * SHUT_RDWR
4332 * Disables further send and receive operations
4333 * and initiates the SCTP shutdown sequence.
4334 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004335static void sctp_shutdown(struct sock *sk, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004337 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 struct sctp_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
4340 if (!sctp_style(sk, TCP))
4341 return;
4342
Xin Long5bf35dd2016-11-13 21:44:37 +08004343 ep = sctp_sk(sk)->ep;
4344 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4345 struct sctp_association *asoc;
4346
Xin Longd46e4162016-06-09 22:48:18 +08004347 sk->sk_state = SCTP_SS_CLOSING;
Xin Long5bf35dd2016-11-13 21:44:37 +08004348 asoc = list_entry(ep->asocs.next,
4349 struct sctp_association, asocs);
4350 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 }
4352}
4353
Xin Long52c52a62016-04-14 15:35:30 +08004354int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4355 struct sctp_info *info)
4356{
4357 struct sctp_transport *prim;
4358 struct list_head *pos;
4359 int mask;
4360
4361 memset(info, 0, sizeof(*info));
4362 if (!asoc) {
4363 struct sctp_sock *sp = sctp_sk(sk);
4364
4365 info->sctpi_s_autoclose = sp->autoclose;
4366 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4367 info->sctpi_s_pd_point = sp->pd_point;
4368 info->sctpi_s_nodelay = sp->nodelay;
4369 info->sctpi_s_disable_fragments = sp->disable_fragments;
4370 info->sctpi_s_v4mapped = sp->v4mapped;
4371 info->sctpi_s_frag_interleave = sp->frag_interleave;
Xin Long40eb90e2016-05-29 17:42:13 +08004372 info->sctpi_s_type = sp->type;
Xin Long52c52a62016-04-14 15:35:30 +08004373
4374 return 0;
4375 }
4376
4377 info->sctpi_tag = asoc->c.my_vtag;
4378 info->sctpi_state = asoc->state;
4379 info->sctpi_rwnd = asoc->a_rwnd;
4380 info->sctpi_unackdata = asoc->unack_data;
4381 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4382 info->sctpi_instrms = asoc->c.sinit_max_instreams;
4383 info->sctpi_outstrms = asoc->c.sinit_num_ostreams;
4384 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4385 info->sctpi_inqueue++;
4386 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4387 info->sctpi_outqueue++;
4388 info->sctpi_overall_error = asoc->overall_error_count;
4389 info->sctpi_max_burst = asoc->max_burst;
4390 info->sctpi_maxseg = asoc->frag_point;
4391 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4392 info->sctpi_peer_tag = asoc->c.peer_vtag;
4393
4394 mask = asoc->peer.ecn_capable << 1;
4395 mask = (mask | asoc->peer.ipv4_address) << 1;
4396 mask = (mask | asoc->peer.ipv6_address) << 1;
4397 mask = (mask | asoc->peer.hostname_address) << 1;
4398 mask = (mask | asoc->peer.asconf_capable) << 1;
4399 mask = (mask | asoc->peer.prsctp_capable) << 1;
4400 mask = (mask | asoc->peer.auth_capable);
4401 info->sctpi_peer_capable = mask;
4402 mask = asoc->peer.sack_needed << 1;
4403 mask = (mask | asoc->peer.sack_generation) << 1;
4404 mask = (mask | asoc->peer.zero_window_announced);
4405 info->sctpi_peer_sack = mask;
4406
4407 info->sctpi_isacks = asoc->stats.isacks;
4408 info->sctpi_osacks = asoc->stats.osacks;
4409 info->sctpi_opackets = asoc->stats.opackets;
4410 info->sctpi_ipackets = asoc->stats.ipackets;
4411 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4412 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4413 info->sctpi_idupchunks = asoc->stats.idupchunks;
4414 info->sctpi_gapcnt = asoc->stats.gapcnt;
4415 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4416 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4417 info->sctpi_oodchunks = asoc->stats.oodchunks;
4418 info->sctpi_iodchunks = asoc->stats.iodchunks;
4419 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4420 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4421
4422 prim = asoc->peer.primary_path;
Stefano Brivio08d56d82017-08-23 13:27:13 +02004423 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
Xin Long52c52a62016-04-14 15:35:30 +08004424 info->sctpi_p_state = prim->state;
4425 info->sctpi_p_cwnd = prim->cwnd;
4426 info->sctpi_p_srtt = prim->srtt;
4427 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4428 info->sctpi_p_hbinterval = prim->hbinterval;
4429 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4430 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4431 info->sctpi_p_ssthresh = prim->ssthresh;
4432 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4433 info->sctpi_p_flight_size = prim->flight_size;
4434 info->sctpi_p_error = prim->error_count;
4435
4436 return 0;
4437}
4438EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4439
Xin Long626d16f2016-04-14 15:35:31 +08004440/* use callback to avoid exporting the core structure */
4441int sctp_transport_walk_start(struct rhashtable_iter *iter)
4442{
4443 int err;
4444
4445 err = rhashtable_walk_init(&sctp_transport_hashtable, iter,
4446 GFP_KERNEL);
4447 if (err)
4448 return err;
4449
4450 err = rhashtable_walk_start(iter);
Xin Long53fa1032016-04-14 15:35:35 +08004451 if (err && err != -EAGAIN) {
Vegard Nossum5fc382d2016-07-23 09:42:35 +02004452 rhashtable_walk_stop(iter);
Xin Long53fa1032016-04-14 15:35:35 +08004453 rhashtable_walk_exit(iter);
4454 return err;
4455 }
Xin Long626d16f2016-04-14 15:35:31 +08004456
Xin Long53fa1032016-04-14 15:35:35 +08004457 return 0;
Xin Long626d16f2016-04-14 15:35:31 +08004458}
4459
4460void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4461{
4462 rhashtable_walk_stop(iter);
4463 rhashtable_walk_exit(iter);
4464}
4465
4466struct sctp_transport *sctp_transport_get_next(struct net *net,
4467 struct rhashtable_iter *iter)
4468{
4469 struct sctp_transport *t;
4470
4471 t = rhashtable_walk_next(iter);
4472 for (; t; t = rhashtable_walk_next(iter)) {
4473 if (IS_ERR(t)) {
4474 if (PTR_ERR(t) == -EAGAIN)
4475 continue;
4476 break;
4477 }
4478
4479 if (net_eq(sock_net(t->asoc->base.sk), net) &&
4480 t->asoc->peer.primary_path == t)
4481 break;
4482 }
4483
4484 return t;
4485}
4486
4487struct sctp_transport *sctp_transport_get_idx(struct net *net,
4488 struct rhashtable_iter *iter,
4489 int pos)
4490{
4491 void *obj = SEQ_START_TOKEN;
4492
4493 while (pos && (obj = sctp_transport_get_next(net, iter)) &&
4494 !IS_ERR(obj))
4495 pos--;
4496
4497 return obj;
4498}
4499
4500int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
4501 void *p) {
4502 int err = 0;
4503 int hash = 0;
4504 struct sctp_ep_common *epb;
4505 struct sctp_hashbucket *head;
4506
4507 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
4508 hash++, head++) {
Xin Long8cda4262017-06-10 14:48:14 +08004509 read_lock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004510 sctp_for_each_hentry(epb, &head->chain) {
4511 err = cb(sctp_ep(epb), p);
4512 if (err)
4513 break;
4514 }
Xin Long8cda4262017-06-10 14:48:14 +08004515 read_unlock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004516 }
4517
4518 return err;
4519}
4520EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
4521
4522int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
4523 struct net *net,
4524 const union sctp_addr *laddr,
4525 const union sctp_addr *paddr, void *p)
4526{
4527 struct sctp_transport *transport;
Xin Long1cceda782016-09-29 02:55:44 +08004528 int err = -ENOENT;
Xin Long626d16f2016-04-14 15:35:31 +08004529
4530 rcu_read_lock();
4531 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
Xin Longed3cc322016-12-15 23:05:52 +08004532 if (!transport || !sctp_transport_hold(transport)) {
4533 rcu_read_unlock();
Xin Long626d16f2016-04-14 15:35:31 +08004534 goto out;
Xin Longed3cc322016-12-15 23:05:52 +08004535 }
Xin Long626d16f2016-04-14 15:35:31 +08004536 rcu_read_unlock();
Xin Long1cceda782016-09-29 02:55:44 +08004537 err = cb(transport, p);
Xin Longcd26da42016-10-31 20:32:31 +08004538 sctp_transport_put(transport);
Xin Long1cceda782016-09-29 02:55:44 +08004539
4540out:
Xin Long626d16f2016-04-14 15:35:31 +08004541 return err;
4542}
4543EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
4544
4545int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
4546 struct net *net, int pos, void *p) {
4547 struct rhashtable_iter hti;
Xin Long626d16f2016-04-14 15:35:31 +08004548 void *obj;
Xin Long53fa1032016-04-14 15:35:35 +08004549 int err;
Xin Long626d16f2016-04-14 15:35:31 +08004550
Xin Long53fa1032016-04-14 15:35:35 +08004551 err = sctp_transport_walk_start(&hti);
4552 if (err)
4553 return err;
Xin Long626d16f2016-04-14 15:35:31 +08004554
Xin Long4c246862017-06-15 17:49:08 +08004555 obj = sctp_transport_get_idx(net, &hti, pos + 1);
4556 for (; !IS_ERR_OR_NULL(obj); obj = sctp_transport_get_next(net, &hti)) {
Xin Long626d16f2016-04-14 15:35:31 +08004557 struct sctp_transport *transport = obj;
4558
4559 if (!sctp_transport_hold(transport))
4560 continue;
4561 err = cb(transport, p);
4562 sctp_transport_put(transport);
4563 if (err)
4564 break;
4565 }
Xin Long626d16f2016-04-14 15:35:31 +08004566 sctp_transport_walk_stop(&hti);
Xin Long53fa1032016-04-14 15:35:35 +08004567
Xin Long626d16f2016-04-14 15:35:31 +08004568 return err;
4569}
4570EXPORT_SYMBOL_GPL(sctp_for_each_transport);
4571
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572/* 7.2.1 Association Status (SCTP_STATUS)
4573
4574 * Applications can retrieve current status information about an
4575 * association, including association state, peer receiver window size,
4576 * number of unacked data chunks, and number of data chunks pending
4577 * receipt. This information is read-only.
4578 */
4579static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4580 char __user *optval,
4581 int __user *optlen)
4582{
4583 struct sctp_status status;
4584 struct sctp_association *asoc = NULL;
4585 struct sctp_transport *transport;
4586 sctp_assoc_t associd;
4587 int retval = 0;
4588
Neil Horman408f22e2007-06-16 14:03:45 -04004589 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 retval = -EINVAL;
4591 goto out;
4592 }
4593
Neil Horman408f22e2007-06-16 14:03:45 -04004594 len = sizeof(status);
4595 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 retval = -EFAULT;
4597 goto out;
4598 }
4599
4600 associd = status.sstat_assoc_id;
4601 asoc = sctp_id2assoc(sk, associd);
4602 if (!asoc) {
4603 retval = -EINVAL;
4604 goto out;
4605 }
4606
4607 transport = asoc->peer.primary_path;
4608
4609 status.sstat_assoc_id = sctp_assoc2id(asoc);
Daniel Borkmann38ab1fa2014-08-28 15:28:26 +02004610 status.sstat_state = sctp_assoc_to_state(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 status.sstat_rwnd = asoc->peer.rwnd;
4612 status.sstat_unackdata = asoc->unack_data;
4613
4614 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4615 status.sstat_instrms = asoc->c.sinit_max_instreams;
4616 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
4617 status.sstat_fragmentation_point = asoc->frag_point;
4618 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08004619 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4620 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 /* Map ipv4 address into v4-mapped-on-v6 address. */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06004622 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004624 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4626 status.sstat_primary.spinfo_srtt = transport->srtt;
4627 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004628 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
Frank Filz3f7a87d2005-06-20 13:14:57 -07004630 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4631 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4632
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 if (put_user(len, optlen)) {
4634 retval = -EFAULT;
4635 goto out;
4636 }
4637
Daniel Borkmannbb333812013-06-28 19:49:40 +02004638 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
4639 __func__, len, status.sstat_state, status.sstat_rwnd,
4640 status.sstat_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
4642 if (copy_to_user(optval, &status, len)) {
4643 retval = -EFAULT;
4644 goto out;
4645 }
4646
4647out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004648 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649}
4650
4651
4652/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4653 *
4654 * Applications can retrieve information about a specific peer address
4655 * of an association, including its reachability state, congestion
4656 * window, and retransmission timer values. This information is
4657 * read-only.
4658 */
4659static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4660 char __user *optval,
4661 int __user *optlen)
4662{
4663 struct sctp_paddrinfo pinfo;
4664 struct sctp_transport *transport;
4665 int retval = 0;
4666
Neil Horman408f22e2007-06-16 14:03:45 -04004667 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 retval = -EINVAL;
4669 goto out;
4670 }
4671
Neil Horman408f22e2007-06-16 14:03:45 -04004672 len = sizeof(pinfo);
4673 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 retval = -EFAULT;
4675 goto out;
4676 }
4677
4678 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4679 pinfo.spinfo_assoc_id);
4680 if (!transport)
4681 return -EINVAL;
4682
4683 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004684 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 pinfo.spinfo_cwnd = transport->cwnd;
4686 pinfo.spinfo_srtt = transport->srtt;
4687 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004688 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
Frank Filz3f7a87d2005-06-20 13:14:57 -07004690 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4691 pinfo.spinfo_state = SCTP_ACTIVE;
4692
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 if (put_user(len, optlen)) {
4694 retval = -EFAULT;
4695 goto out;
4696 }
4697
4698 if (copy_to_user(optval, &pinfo, len)) {
4699 retval = -EFAULT;
4700 goto out;
4701 }
4702
4703out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004704 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705}
4706
4707/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4708 *
4709 * This option is a on/off flag. If enabled no SCTP message
4710 * fragmentation will be performed. Instead if a message being sent
4711 * exceeds the current PMTU size, the message will NOT be sent and
4712 * instead a error will be indicated to the user.
4713 */
4714static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4715 char __user *optval, int __user *optlen)
4716{
4717 int val;
4718
4719 if (len < sizeof(int))
4720 return -EINVAL;
4721
4722 len = sizeof(int);
4723 val = (sctp_sk(sk)->disable_fragments == 1);
4724 if (put_user(len, optlen))
4725 return -EFAULT;
4726 if (copy_to_user(optval, &val, len))
4727 return -EFAULT;
4728 return 0;
4729}
4730
4731/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4732 *
4733 * This socket option is used to specify various notifications and
4734 * ancillary data the user wishes to receive.
4735 */
4736static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
4737 int __user *optlen)
4738{
Jiri Slabya4b8e712016-10-21 14:13:24 +02004739 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00004741 if (len > sizeof(struct sctp_event_subscribe))
4742 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04004743 if (put_user(len, optlen))
4744 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4746 return -EFAULT;
4747 return 0;
4748}
4749
4750/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
4751 *
4752 * This socket option is applicable to the UDP-style socket only. When
4753 * set it will cause associations that are idle for more than the
4754 * specified number of seconds to automatically close. An association
4755 * being idle is defined an association that has NOT sent or received
4756 * user data. The special value of '0' indicates that no automatic
4757 * close of any associations should be performed. The option expects an
4758 * integer defining the number of seconds of idle time before an
4759 * association is closed.
4760 */
4761static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
4762{
4763 /* Applicable to UDP-style socket only */
4764 if (sctp_style(sk, TCP))
4765 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04004766 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004768 len = sizeof(int);
4769 if (put_user(len, optlen))
4770 return -EFAULT;
Marcelo Ricardo Leitnerb923c862018-01-08 19:02:29 -02004771 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 return -EFAULT;
4773 return 0;
4774}
4775
4776/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00004777int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778{
Benjamin Poirier0343c552012-03-08 05:55:58 +00004779 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06004780 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 struct socket *sock;
4782 int err = 0;
4783
Xin Long362d2ce2017-10-17 23:26:10 +08004784 /* Do not peel off from one netns to another one. */
4785 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
4786 return -EINVAL;
4787
Benjamin Poirier0343c552012-03-08 05:55:58 +00004788 if (!asoc)
4789 return -EINVAL;
4790
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 /* An association cannot be branched off from an already peeled-off
4792 * socket, nor is this supported for tcp style sockets.
4793 */
4794 if (!sctp_style(sk, UDP))
4795 return -EINVAL;
4796
4797 /* Create a new socket. */
4798 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
4799 if (err < 0)
4800 return err;
4801
Vlad Yasevich914e1c82009-02-13 08:33:44 +00004802 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08004803
4804 /* Make peeled-off sockets more like 1-1 accepted sockets.
4805 * Set the daddr and initialize id to something more random
4806 */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06004807 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00004808
4809 /* Populate the fields of the newsk from the oldsk and migrate the
4810 * asoc to the newsk.
4811 */
4812 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08004813
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 *sockp = sock;
4815
4816 return err;
4817}
Benjamin Poirier0343c552012-03-08 05:55:58 +00004818EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
4820static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
4821{
4822 sctp_peeloff_arg_t peeloff;
4823 struct socket *newsock;
Al Viro56b31d12012-08-18 00:25:51 -04004824 struct file *newfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
Neil Horman408f22e2007-06-16 14:03:45 -04004827 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004829 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 if (copy_from_user(&peeloff, optval, len))
4831 return -EFAULT;
4832
Benjamin Poirier0343c552012-03-08 05:55:58 +00004833 retval = sctp_do_peeloff(sk, peeloff.associd, &newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 if (retval < 0)
4835 goto out;
4836
4837 /* Map the socket to an unused fd that can be returned to the user. */
Yann Droneaud8a59bd32013-07-02 18:39:36 +02004838 retval = get_unused_fd_flags(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 if (retval < 0) {
4840 sock_release(newsock);
4841 goto out;
4842 }
4843
Linus Torvaldsaab174f2012-10-02 20:25:04 -07004844 newfile = sock_alloc_file(newsock, 0, NULL);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05304845 if (IS_ERR(newfile)) {
Al Viro56b31d12012-08-18 00:25:51 -04004846 put_unused_fd(retval);
4847 sock_release(newsock);
4848 return PTR_ERR(newfile);
4849 }
4850
Daniel Borkmannbb333812013-06-28 19:49:40 +02004851 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
4852 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
4854 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04004855 if (put_user(len, optlen)) {
4856 fput(newfile);
4857 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04004858 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04004859 }
4860 peeloff.sd = retval;
4861 if (copy_to_user(optval, &peeloff, len)) {
4862 fput(newfile);
4863 put_unused_fd(retval);
4864 return -EFAULT;
4865 }
4866 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867out:
4868 return retval;
4869}
4870
4871/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
4872 *
4873 * Applications can enable or disable heartbeats for any peer address of
4874 * an association, modify an address's heartbeat interval, force a
4875 * heartbeat to be sent immediately, and adjust the address's maximum
4876 * number of retransmissions sent before an address is considered
4877 * unreachable. The following structure is used to access and modify an
4878 * address's parameters:
4879 *
4880 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08004881 * sctp_assoc_t spp_assoc_id;
4882 * struct sockaddr_storage spp_address;
4883 * uint32_t spp_hbinterval;
4884 * uint16_t spp_pathmaxrxt;
4885 * uint32_t spp_pathmtu;
4886 * uint32_t spp_sackdelay;
4887 * uint32_t spp_flags;
4888 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08004890 * spp_assoc_id - (one-to-many style socket) This is filled in the
4891 * application, and identifies the association for
4892 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 * spp_address - This specifies which address is of interest.
4894 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08004895 * in milliseconds. If a value of zero
4896 * is present in this field then no changes are to
4897 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 * spp_pathmaxrxt - This contains the maximum number of
4899 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08004900 * considered unreachable. If a value of zero
4901 * is present in this field then no changes are to
4902 * be made to this parameter.
4903 * spp_pathmtu - When Path MTU discovery is disabled the value
4904 * specified here will be the "fixed" path mtu.
4905 * Note that if the spp_address field is empty
4906 * then all associations on this address will
4907 * have this fixed path mtu set upon them.
4908 *
4909 * spp_sackdelay - When delayed sack is enabled, this value specifies
4910 * the number of milliseconds that sacks will be delayed
4911 * for. This value will apply to all addresses of an
4912 * association if the spp_address field is empty. Note
4913 * also, that if delayed sack is enabled and this
4914 * value is set to 0, no change is made to the last
4915 * recorded delayed sack timer value.
4916 *
4917 * spp_flags - These flags are used to control various features
4918 * on an association. The flag field may contain
4919 * zero or more of the following options.
4920 *
4921 * SPP_HB_ENABLE - Enable heartbeats on the
4922 * specified address. Note that if the address
4923 * field is empty all addresses for the association
4924 * have heartbeats enabled upon them.
4925 *
4926 * SPP_HB_DISABLE - Disable heartbeats on the
4927 * speicifed address. Note that if the address
4928 * field is empty all addresses for the association
4929 * will have their heartbeats disabled. Note also
4930 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
4931 * mutually exclusive, only one of these two should
4932 * be specified. Enabling both fields will have
4933 * undetermined results.
4934 *
4935 * SPP_HB_DEMAND - Request a user initiated heartbeat
4936 * to be made immediately.
4937 *
4938 * SPP_PMTUD_ENABLE - This field will enable PMTU
4939 * discovery upon the specified address. Note that
4940 * if the address feild is empty then all addresses
4941 * on the association are effected.
4942 *
4943 * SPP_PMTUD_DISABLE - This field will disable PMTU
4944 * discovery upon the specified address. Note that
4945 * if the address feild is empty then all addresses
4946 * on the association are effected. Not also that
4947 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
4948 * exclusive. Enabling both will have undetermined
4949 * results.
4950 *
4951 * SPP_SACKDELAY_ENABLE - Setting this flag turns
4952 * on delayed sack. The time specified in spp_sackdelay
4953 * is used to specify the sack delay for this address. Note
4954 * that if spp_address is empty then all addresses will
4955 * enable delayed sack and take on the sack delay
4956 * value specified in spp_sackdelay.
4957 * SPP_SACKDELAY_DISABLE - Setting this flag turns
4958 * off delayed sack. If the spp_address field is blank then
4959 * delayed sack is disabled for the entire association. Note
4960 * also that this field is mutually exclusive to
4961 * SPP_SACKDELAY_ENABLE, setting both will have undefined
4962 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 */
4964static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08004965 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966{
Frank Filz52ccb8e2005-12-22 11:36:46 -08004967 struct sctp_paddrparams params;
4968 struct sctp_transport *trans = NULL;
4969 struct sctp_association *asoc = NULL;
4970 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
Neil Horman408f22e2007-06-16 14:03:45 -04004972 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004974 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 if (copy_from_user(&params, optval, len))
4976 return -EFAULT;
4977
Frank Filz52ccb8e2005-12-22 11:36:46 -08004978 /* If an address other than INADDR_ANY is specified, and
4979 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 */
wangweidongcb3f8372013-12-23 12:16:50 +08004981 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08004982 trans = sctp_addr_id2transport(sk, &params.spp_address,
4983 params.spp_assoc_id);
4984 if (!trans) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02004985 pr_debug("%s: failed no transport\n", __func__);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004986 return -EINVAL;
4987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 }
4989
Frank Filz52ccb8e2005-12-22 11:36:46 -08004990 /* Get association, if assoc_id != 0 and the socket is a one
4991 * to many style socket, and an association was not found, then
4992 * the id was invalid.
4993 */
4994 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
4995 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02004996 pr_debug("%s: failed no association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999
Frank Filz52ccb8e2005-12-22 11:36:46 -08005000 if (trans) {
5001 /* Fetch transport values. */
5002 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5003 params.spp_pathmtu = trans->pathmtu;
5004 params.spp_pathmaxrxt = trans->pathmaxrxt;
5005 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
Frank Filz52ccb8e2005-12-22 11:36:46 -08005007 /*draft-11 doesn't say what to return in spp_flags*/
5008 params.spp_flags = trans->param_flags;
5009 } else if (asoc) {
5010 /* Fetch association values. */
5011 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5012 params.spp_pathmtu = asoc->pathmtu;
5013 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5014 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
Frank Filz52ccb8e2005-12-22 11:36:46 -08005016 /*draft-11 doesn't say what to return in spp_flags*/
5017 params.spp_flags = asoc->param_flags;
5018 } else {
5019 /* Fetch socket values. */
5020 params.spp_hbinterval = sp->hbinterval;
5021 params.spp_pathmtu = sp->pathmtu;
5022 params.spp_sackdelay = sp->sackdelay;
5023 params.spp_pathmaxrxt = sp->pathmaxrxt;
5024
5025 /*draft-11 doesn't say what to return in spp_flags*/
5026 params.spp_flags = sp->param_flags;
5027 }
5028
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 if (copy_to_user(optval, &params, len))
5030 return -EFAULT;
5031
5032 if (put_user(len, optlen))
5033 return -EFAULT;
5034
5035 return 0;
5036}
5037
Wei Yongjund364d922008-05-09 15:13:26 -07005038/*
5039 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08005040 *
Wei Yongjund364d922008-05-09 15:13:26 -07005041 * This option will effect the way delayed acks are performed. This
5042 * option allows you to get or set the delayed ack time, in
5043 * milliseconds. It also allows changing the delayed ack frequency.
5044 * Changing the frequency to 1 disables the delayed sack algorithm. If
5045 * the assoc_id is 0, then this sets or gets the endpoints default
5046 * values. If the assoc_id field is non-zero, then the set or get
5047 * effects the specified association for the one to many model (the
5048 * assoc_id field is ignored by the one to one model). Note that if
5049 * sack_delay or sack_freq are 0 when setting this option, then the
5050 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08005051 *
Wei Yongjund364d922008-05-09 15:13:26 -07005052 * struct sctp_sack_info {
5053 * sctp_assoc_t sack_assoc_id;
5054 * uint32_t sack_delay;
5055 * uint32_t sack_freq;
5056 * };
Frank Filz77086102005-12-22 11:37:30 -08005057 *
Wei Yongjund364d922008-05-09 15:13:26 -07005058 * sack_assoc_id - This parameter, indicates which association the user
5059 * is performing an action upon. Note that if this field's value is
5060 * zero then the endpoints default value is changed (effecting future
5061 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08005062 *
Wei Yongjund364d922008-05-09 15:13:26 -07005063 * sack_delay - This parameter contains the number of milliseconds that
5064 * the user is requesting the delayed ACK timer be set to. Note that
5065 * this value is defined in the standard to be between 200 and 500
5066 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08005067 *
Wei Yongjund364d922008-05-09 15:13:26 -07005068 * sack_freq - This parameter contains the number of packets that must
5069 * be received before a sack is sent without waiting for the delay
5070 * timer to expire. The default value for this is 2, setting this
5071 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08005072 */
Wei Yongjund364d922008-05-09 15:13:26 -07005073static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08005074 char __user *optval,
5075 int __user *optlen)
5076{
Wei Yongjund364d922008-05-09 15:13:26 -07005077 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08005078 struct sctp_association *asoc = NULL;
5079 struct sctp_sock *sp = sctp_sk(sk);
5080
Wei Yongjund364d922008-05-09 15:13:26 -07005081 if (len >= sizeof(struct sctp_sack_info)) {
5082 len = sizeof(struct sctp_sack_info);
5083
5084 if (copy_from_user(&params, optval, len))
5085 return -EFAULT;
5086 } else if (len == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05005087 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005088 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005089 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005090 "Use struct sctp_sack_info instead\n",
5091 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07005092 if (copy_from_user(&params, optval, len))
5093 return -EFAULT;
5094 } else
wangweidongcb3f8372013-12-23 12:16:50 +08005095 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08005096
Wei Yongjund364d922008-05-09 15:13:26 -07005097 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08005098 * to many style socket, and an association was not found, then
5099 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005100 */
Wei Yongjund364d922008-05-09 15:13:26 -07005101 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5102 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08005103 return -EINVAL;
5104
5105 if (asoc) {
5106 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005107 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5108 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08005109 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07005110 params.sack_freq = asoc->sackfreq;
5111
5112 } else {
5113 params.sack_delay = 0;
5114 params.sack_freq = 1;
5115 }
Frank Filz77086102005-12-22 11:37:30 -08005116 } else {
5117 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005118 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5119 params.sack_delay = sp->sackdelay;
5120 params.sack_freq = sp->sackfreq;
5121 } else {
5122 params.sack_delay = 0;
5123 params.sack_freq = 1;
5124 }
Frank Filz77086102005-12-22 11:37:30 -08005125 }
5126
5127 if (copy_to_user(optval, &params, len))
5128 return -EFAULT;
5129
5130 if (put_user(len, optlen))
5131 return -EFAULT;
5132
5133 return 0;
5134}
5135
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5137 *
5138 * Applications can specify protocol parameters for the default association
5139 * initialization. The option name argument to setsockopt() and getsockopt()
5140 * is SCTP_INITMSG.
5141 *
5142 * Setting initialization parameters is effective only on an unconnected
5143 * socket (for UDP-style sockets only future associations are effected
5144 * by the change). With TCP-style sockets, this option is inherited by
5145 * sockets derived from a listener socket.
5146 */
5147static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5148{
Neil Horman408f22e2007-06-16 14:03:45 -04005149 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005151 len = sizeof(struct sctp_initmsg);
5152 if (put_user(len, optlen))
5153 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5155 return -EFAULT;
5156 return 0;
5157}
5158
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005160static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5161 char __user *optval, int __user *optlen)
5162{
5163 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005164 int cnt = 0;
5165 struct sctp_getaddrs getaddrs;
5166 struct sctp_transport *from;
5167 void __user *to;
5168 union sctp_addr temp;
5169 struct sctp_sock *sp = sctp_sk(sk);
5170 int addrlen;
5171 size_t space_left;
5172 int bytes_copied;
5173
5174 if (len < sizeof(struct sctp_getaddrs))
5175 return -EINVAL;
5176
5177 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5178 return -EFAULT;
5179
5180 /* For UDP-style sockets, id specifies the association to query. */
5181 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5182 if (!asoc)
5183 return -EINVAL;
5184
wangweidongcb3f8372013-12-23 12:16:50 +08005185 to = optval + offsetof(struct sctp_getaddrs, addrs);
5186 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005187
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07005188 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5189 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08005190 memcpy(&temp, &from->ipaddr, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005191 addrlen = sctp_get_pf_specific(sk->sk_family)
5192 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005193 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005194 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005195 if (copy_to_user(to, &temp, addrlen))
5196 return -EFAULT;
5197 to += addrlen;
5198 cnt++;
5199 space_left -= addrlen;
5200 }
5201
5202 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5203 return -EFAULT;
5204 bytes_copied = ((char __user *)to) - optval;
5205 if (put_user(bytes_copied, optlen))
5206 return -EFAULT;
5207
5208 return 0;
5209}
5210
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005211static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5212 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005213{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005214 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005215 union sctp_addr temp;
5216 int cnt = 0;
5217 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005218 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005219
Vlad Yasevich29303542007-09-16 16:02:12 -07005220 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005221 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07005222 if (!addr->valid)
5223 continue;
5224
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005225 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08005226 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005227 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005228 if ((PF_INET6 == sk->sk_family) &&
5229 inet_v6_ipv6only(sk) &&
5230 (AF_INET == addr->a.sa.sa_family))
5231 continue;
Al Viro6244be42006-11-20 17:21:44 -08005232 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05005233 if (!temp.v4.sin_port)
5234 temp.v4.sin_port = htons(port);
5235
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005236 addrlen = sctp_get_pf_specific(sk->sk_family)
5237 ->addr_to_user(sctp_sk(sk), &temp);
5238
Vlad Yasevich29303542007-09-16 16:02:12 -07005239 if (space_left < addrlen) {
5240 cnt = -ENOMEM;
5241 break;
5242 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005243 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08005244
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005245 to += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005246 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005247 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04005248 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005249 }
Vlad Yasevich29303542007-09-16 16:02:12 -07005250 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005251
5252 return cnt;
5253}
5254
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005256static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5257 char __user *optval, int __user *optlen)
5258{
5259 struct sctp_bind_addr *bp;
5260 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005261 int cnt = 0;
5262 struct sctp_getaddrs getaddrs;
5263 struct sctp_sockaddr_entry *addr;
5264 void __user *to;
5265 union sctp_addr temp;
5266 struct sctp_sock *sp = sctp_sk(sk);
5267 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005268 int err = 0;
5269 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005270 int bytes_copied = 0;
5271 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005272 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005273
Neil Horman408f22e2007-06-16 14:03:45 -04005274 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005275 return -EINVAL;
5276
5277 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5278 return -EFAULT;
5279
5280 /*
5281 * For UDP-style sockets, id specifies the association to query.
5282 * If the id field is set to the value '0' then the locally bound
5283 * addresses are returned without regard to any particular
5284 * association.
5285 */
5286 if (0 == getaddrs.assoc_id) {
5287 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005288 } else {
5289 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5290 if (!asoc)
5291 return -EINVAL;
5292 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005293 }
5294
wangweidongcb3f8372013-12-23 12:16:50 +08005295 to = optval + offsetof(struct sctp_getaddrs, addrs);
5296 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Neil Horman186e2342007-06-18 19:59:16 -04005297
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02005298 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005299 if (!addrs)
5300 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005301
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005302 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5303 * addresses from the global local address list.
5304 */
5305 if (sctp_list_single_entry(&bp->address_list)) {
5306 addr = list_entry(bp->address_list.next,
5307 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04005308 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005309 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5310 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005311 if (cnt < 0) {
5312 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005313 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005314 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005315 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005316 }
5317 }
5318
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005319 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005320 /* Protection on the bound address list is not needed since
5321 * in the socket option context we hold a socket lock and
5322 * thus the bound address list can't change.
5323 */
5324 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08005325 memcpy(&temp, &addr->a, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005326 addrlen = sctp_get_pf_specific(sk->sk_family)
5327 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005328 if (space_left < addrlen) {
5329 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07005330 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005331 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005332 memcpy(buf, &temp, addrlen);
5333 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005334 bytes_copied += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005335 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005336 space_left -= addrlen;
5337 }
5338
5339copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005340 if (copy_to_user(to, addrs, bytes_copied)) {
5341 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005342 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005343 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005344 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
5345 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005346 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005347 }
Marcelo Ricardo Leitnerb923c862018-01-08 19:02:29 -02005348 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
5349 * but we can't change it anymore.
5350 */
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005351 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005352 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005353out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005354 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005355 return err;
5356}
5357
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
5359 *
5360 * Requests that the local SCTP stack use the enclosed peer address as
5361 * the association primary. The enclosed address must be one of the
5362 * association peer's addresses.
5363 */
5364static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
5365 char __user *optval, int __user *optlen)
5366{
5367 struct sctp_prim prim;
5368 struct sctp_association *asoc;
5369 struct sctp_sock *sp = sctp_sk(sk);
5370
Neil Horman408f22e2007-06-16 14:03:45 -04005371 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 return -EINVAL;
5373
Neil Horman408f22e2007-06-16 14:03:45 -04005374 len = sizeof(struct sctp_prim);
5375
5376 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 return -EFAULT;
5378
5379 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
5380 if (!asoc)
5381 return -EINVAL;
5382
5383 if (!asoc->peer.primary_path)
5384 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005385
Al Viro8cec6b82006-11-20 17:23:01 -08005386 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
5387 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005389 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 (union sctp_addr *)&prim.ssp_addr);
5391
Neil Horman408f22e2007-06-16 14:03:45 -04005392 if (put_user(len, optlen))
5393 return -EFAULT;
5394 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 return -EFAULT;
5396
5397 return 0;
5398}
5399
5400/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005401 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005403 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 * Indication parameter for all future INIT and INIT-ACK exchanges.
5405 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005406static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 char __user *optval, int __user *optlen)
5408{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005409 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
Neil Horman408f22e2007-06-16 14:03:45 -04005411 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 return -EINVAL;
5413
Neil Horman408f22e2007-06-16 14:03:45 -04005414 len = sizeof(struct sctp_setadaptation);
5415
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005416 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04005417
5418 if (put_user(len, optlen))
5419 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005420 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07005422
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 return 0;
5424}
5425
5426/*
5427 *
5428 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
5429 *
5430 * Applications that wish to use the sendto() system call may wish to
5431 * specify a default set of parameters that would normally be supplied
5432 * through the inclusion of ancillary data. This socket option allows
5433 * such an application to set the default sctp_sndrcvinfo structure.
5434
5435
5436 * The application that wishes to use this socket option simply passes
5437 * in to this call the sctp_sndrcvinfo structure defined in Section
5438 * 5.2.2) The input parameters accepted by this call include
5439 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
5440 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
5441 * to this call if the caller is using the UDP model.
5442 *
5443 * For getsockopt, it get the default sctp_sndrcvinfo structure.
5444 */
5445static int sctp_getsockopt_default_send_param(struct sock *sk,
5446 int len, char __user *optval,
5447 int __user *optlen)
5448{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005450 struct sctp_association *asoc;
5451 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005453 if (len < sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005455
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005456 len = sizeof(info);
Neil Horman408f22e2007-06-16 14:03:45 -04005457
5458 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 return -EFAULT;
5460
5461 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
5462 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
5463 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 if (asoc) {
5465 info.sinfo_stream = asoc->default_stream;
5466 info.sinfo_flags = asoc->default_flags;
5467 info.sinfo_ppid = asoc->default_ppid;
5468 info.sinfo_context = asoc->default_context;
5469 info.sinfo_timetolive = asoc->default_timetolive;
5470 } else {
5471 info.sinfo_stream = sp->default_stream;
5472 info.sinfo_flags = sp->default_flags;
5473 info.sinfo_ppid = sp->default_ppid;
5474 info.sinfo_context = sp->default_context;
5475 info.sinfo_timetolive = sp->default_timetolive;
5476 }
5477
Neil Horman408f22e2007-06-16 14:03:45 -04005478 if (put_user(len, optlen))
5479 return -EFAULT;
5480 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 return -EFAULT;
5482
5483 return 0;
5484}
5485
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005486/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
5487 * (SCTP_DEFAULT_SNDINFO)
5488 */
5489static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
5490 char __user *optval,
5491 int __user *optlen)
5492{
5493 struct sctp_sock *sp = sctp_sk(sk);
5494 struct sctp_association *asoc;
5495 struct sctp_sndinfo info;
5496
5497 if (len < sizeof(info))
5498 return -EINVAL;
5499
5500 len = sizeof(info);
5501
5502 if (copy_from_user(&info, optval, len))
5503 return -EFAULT;
5504
5505 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
5506 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
5507 return -EINVAL;
5508 if (asoc) {
5509 info.snd_sid = asoc->default_stream;
5510 info.snd_flags = asoc->default_flags;
5511 info.snd_ppid = asoc->default_ppid;
5512 info.snd_context = asoc->default_context;
5513 } else {
5514 info.snd_sid = sp->default_stream;
5515 info.snd_flags = sp->default_flags;
5516 info.snd_ppid = sp->default_ppid;
5517 info.snd_context = sp->default_context;
5518 }
5519
5520 if (put_user(len, optlen))
5521 return -EFAULT;
5522 if (copy_to_user(optval, &info, len))
5523 return -EFAULT;
5524
5525 return 0;
5526}
5527
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528/*
5529 *
5530 * 7.1.5 SCTP_NODELAY
5531 *
5532 * Turn on/off any Nagle-like algorithm. This means that packets are
5533 * generally sent as soon as possible and no unnecessary delays are
5534 * introduced, at the cost of more packets in the network. Expects an
5535 * integer boolean flag.
5536 */
5537
5538static int sctp_getsockopt_nodelay(struct sock *sk, int len,
5539 char __user *optval, int __user *optlen)
5540{
5541 int val;
5542
5543 if (len < sizeof(int))
5544 return -EINVAL;
5545
5546 len = sizeof(int);
5547 val = (sctp_sk(sk)->nodelay == 1);
5548 if (put_user(len, optlen))
5549 return -EFAULT;
5550 if (copy_to_user(optval, &val, len))
5551 return -EFAULT;
5552 return 0;
5553}
5554
5555/*
5556 *
5557 * 7.1.1 SCTP_RTOINFO
5558 *
5559 * The protocol parameters used to initialize and bound retransmission
5560 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
5561 * and modify these parameters.
5562 * All parameters are time values, in milliseconds. A value of 0, when
5563 * modifying the parameters, indicates that the current value should not
5564 * be changed.
5565 *
5566 */
5567static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
5568 char __user *optval,
5569 int __user *optlen) {
5570 struct sctp_rtoinfo rtoinfo;
5571 struct sctp_association *asoc;
5572
Neil Horman408f22e2007-06-16 14:03:45 -04005573 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 return -EINVAL;
5575
Neil Horman408f22e2007-06-16 14:03:45 -04005576 len = sizeof(struct sctp_rtoinfo);
5577
5578 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 return -EFAULT;
5580
5581 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
5582
5583 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
5584 return -EINVAL;
5585
5586 /* Values corresponding to the specific association. */
5587 if (asoc) {
5588 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
5589 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
5590 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
5591 } else {
5592 /* Values corresponding to the endpoint. */
5593 struct sctp_sock *sp = sctp_sk(sk);
5594
5595 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
5596 rtoinfo.srto_max = sp->rtoinfo.srto_max;
5597 rtoinfo.srto_min = sp->rtoinfo.srto_min;
5598 }
5599
5600 if (put_user(len, optlen))
5601 return -EFAULT;
5602
5603 if (copy_to_user(optval, &rtoinfo, len))
5604 return -EFAULT;
5605
5606 return 0;
5607}
5608
5609/*
5610 *
5611 * 7.1.2 SCTP_ASSOCINFO
5612 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02005613 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 * of the association.
5615 * Returns an error if the new association retransmission value is
5616 * greater than the sum of the retransmission value of the peer.
5617 * See [SCTP] for more information.
5618 *
5619 */
5620static int sctp_getsockopt_associnfo(struct sock *sk, int len,
5621 char __user *optval,
5622 int __user *optlen)
5623{
5624
5625 struct sctp_assocparams assocparams;
5626 struct sctp_association *asoc;
5627 struct list_head *pos;
5628 int cnt = 0;
5629
Neil Horman408f22e2007-06-16 14:03:45 -04005630 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 return -EINVAL;
5632
Neil Horman408f22e2007-06-16 14:03:45 -04005633 len = sizeof(struct sctp_assocparams);
5634
5635 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 return -EFAULT;
5637
5638 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
5639
5640 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
5641 return -EINVAL;
5642
5643 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07005644 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
5646 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
5647 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
Daniel Borkmann52db8822013-06-25 18:17:27 +02005648 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
5650 list_for_each(pos, &asoc->peer.transport_addr_list) {
wangweidongcb3f8372013-12-23 12:16:50 +08005651 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 }
5653
5654 assocparams.sasoc_number_peer_destinations = cnt;
5655 } else {
5656 /* Values corresponding to the endpoint */
5657 struct sctp_sock *sp = sctp_sk(sk);
5658
5659 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
5660 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
5661 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
5662 assocparams.sasoc_cookie_life =
5663 sp->assocparams.sasoc_cookie_life;
5664 assocparams.sasoc_number_peer_destinations =
5665 sp->assocparams.
5666 sasoc_number_peer_destinations;
5667 }
5668
5669 if (put_user(len, optlen))
5670 return -EFAULT;
5671
5672 if (copy_to_user(optval, &assocparams, len))
5673 return -EFAULT;
5674
5675 return 0;
5676}
5677
5678/*
5679 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
5680 *
5681 * This socket option is a boolean flag which turns on or off mapped V4
5682 * addresses. If this option is turned on and the socket is type
5683 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
5684 * If this option is turned off, then no mapping will be done of V4
5685 * addresses and a user will receive both PF_INET6 and PF_INET type
5686 * addresses on the socket.
5687 */
5688static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
5689 char __user *optval, int __user *optlen)
5690{
5691 int val;
5692 struct sctp_sock *sp = sctp_sk(sk);
5693
5694 if (len < sizeof(int))
5695 return -EINVAL;
5696
5697 len = sizeof(int);
5698 val = sp->v4mapped;
5699 if (put_user(len, optlen))
5700 return -EFAULT;
5701 if (copy_to_user(optval, &val, len))
5702 return -EFAULT;
5703
5704 return 0;
5705}
5706
5707/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005708 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
5709 * (chapter and verse is quoted at sctp_setsockopt_context())
5710 */
5711static int sctp_getsockopt_context(struct sock *sk, int len,
5712 char __user *optval, int __user *optlen)
5713{
5714 struct sctp_assoc_value params;
5715 struct sctp_sock *sp;
5716 struct sctp_association *asoc;
5717
Neil Horman408f22e2007-06-16 14:03:45 -04005718 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005719 return -EINVAL;
5720
Neil Horman408f22e2007-06-16 14:03:45 -04005721 len = sizeof(struct sctp_assoc_value);
5722
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005723 if (copy_from_user(&params, optval, len))
5724 return -EFAULT;
5725
5726 sp = sctp_sk(sk);
5727
5728 if (params.assoc_id != 0) {
5729 asoc = sctp_id2assoc(sk, params.assoc_id);
5730 if (!asoc)
5731 return -EINVAL;
5732 params.assoc_value = asoc->default_rcv_context;
5733 } else {
5734 params.assoc_value = sp->default_rcv_context;
5735 }
5736
5737 if (put_user(len, optlen))
5738 return -EFAULT;
5739 if (copy_to_user(optval, &params, len))
5740 return -EFAULT;
5741
5742 return 0;
5743}
5744
5745/*
Wei Yongjune89c2092008-12-25 16:54:58 -08005746 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
5747 * This option will get or set the maximum size to put in any outgoing
5748 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 * fragmented by SCTP into the specified size. Note that the underlying
5750 * SCTP implementation may fragment into smaller sized chunks when the
5751 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08005752 * the user. The default value for this option is '0' which indicates
5753 * the user is NOT limiting fragmentation and only the PMTU will effect
5754 * SCTP's choice of DATA chunk size. Note also that values set larger
5755 * than the maximum size of an IP datagram will effectively let SCTP
5756 * control fragmentation (i.e. the same as setting this option to 0).
5757 *
5758 * The following structure is used to access and modify this parameter:
5759 *
5760 * struct sctp_assoc_value {
5761 * sctp_assoc_t assoc_id;
5762 * uint32_t assoc_value;
5763 * };
5764 *
5765 * assoc_id: This parameter is ignored for one-to-one style sockets.
5766 * For one-to-many style sockets this parameter indicates which
5767 * association the user is performing an action upon. Note that if
5768 * this field's value is zero then the endpoints default value is
5769 * changed (effecting future associations only).
5770 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 */
5772static int sctp_getsockopt_maxseg(struct sock *sk, int len,
5773 char __user *optval, int __user *optlen)
5774{
Wei Yongjune89c2092008-12-25 16:54:58 -08005775 struct sctp_assoc_value params;
5776 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777
Wei Yongjune89c2092008-12-25 16:54:58 -08005778 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05005779 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005780 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005781 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005782 "Use struct sctp_assoc_value instead\n",
5783 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08005784 params.assoc_id = 0;
5785 } else if (len >= sizeof(struct sctp_assoc_value)) {
5786 len = sizeof(struct sctp_assoc_value);
Marcelo Ricardo Leitnerb923c862018-01-08 19:02:29 -02005787 if (copy_from_user(&params, optval, len))
Wei Yongjune89c2092008-12-25 16:54:58 -08005788 return -EFAULT;
5789 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 return -EINVAL;
5791
Wei Yongjune89c2092008-12-25 16:54:58 -08005792 asoc = sctp_id2assoc(sk, params.assoc_id);
5793 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
5794 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795
Wei Yongjune89c2092008-12-25 16:54:58 -08005796 if (asoc)
5797 params.assoc_value = asoc->frag_point;
5798 else
5799 params.assoc_value = sctp_sk(sk)->user_frag;
5800
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 if (put_user(len, optlen))
5802 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08005803 if (len == sizeof(int)) {
5804 if (copy_to_user(optval, &params.assoc_value, len))
5805 return -EFAULT;
5806 } else {
5807 if (copy_to_user(optval, &params, len))
5808 return -EFAULT;
5809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810
5811 return 0;
5812}
5813
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005814/*
5815 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
5816 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
5817 */
5818static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
5819 char __user *optval, int __user *optlen)
5820{
5821 int val;
5822
5823 if (len < sizeof(int))
5824 return -EINVAL;
5825
5826 len = sizeof(int);
5827
5828 val = sctp_sk(sk)->frag_interleave;
5829 if (put_user(len, optlen))
5830 return -EFAULT;
5831 if (copy_to_user(optval, &val, len))
5832 return -EFAULT;
5833
5834 return 0;
5835}
5836
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005837/*
5838 * 7.1.25. Set or Get the sctp partial delivery point
5839 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
5840 */
5841static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
5842 char __user *optval,
5843 int __user *optlen)
5844{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09005845 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005846
5847 if (len < sizeof(u32))
5848 return -EINVAL;
5849
5850 len = sizeof(u32);
5851
5852 val = sctp_sk(sk)->pd_point;
5853 if (put_user(len, optlen))
5854 return -EFAULT;
5855 if (copy_to_user(optval, &val, len))
5856 return -EFAULT;
5857
Wei Yongjun7d743b72010-12-14 16:10:41 +00005858 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005859}
5860
Vlad Yasevich70331572007-03-23 11:34:36 -07005861/*
5862 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
5863 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
5864 */
5865static int sctp_getsockopt_maxburst(struct sock *sk, int len,
5866 char __user *optval,
5867 int __user *optlen)
5868{
Neil Horman219b99a2008-03-05 13:44:46 -08005869 struct sctp_assoc_value params;
5870 struct sctp_sock *sp;
5871 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07005872
Neil Horman219b99a2008-03-05 13:44:46 -08005873 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05005874 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005875 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005876 "Use of int in max_burst socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005877 "Use struct sctp_assoc_value instead\n",
5878 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08005879 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00005880 } else if (len >= sizeof(struct sctp_assoc_value)) {
5881 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08005882 if (copy_from_user(&params, optval, len))
5883 return -EFAULT;
5884 } else
5885 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07005886
Neil Horman219b99a2008-03-05 13:44:46 -08005887 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07005888
Neil Horman219b99a2008-03-05 13:44:46 -08005889 if (params.assoc_id != 0) {
5890 asoc = sctp_id2assoc(sk, params.assoc_id);
5891 if (!asoc)
5892 return -EINVAL;
5893 params.assoc_value = asoc->max_burst;
5894 } else
5895 params.assoc_value = sp->max_burst;
5896
5897 if (len == sizeof(int)) {
5898 if (copy_to_user(optval, &params.assoc_value, len))
5899 return -EFAULT;
5900 } else {
5901 if (copy_to_user(optval, &params, len))
5902 return -EFAULT;
5903 }
5904
5905 return 0;
5906
Vlad Yasevich70331572007-03-23 11:34:36 -07005907}
5908
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005909static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
5910 char __user *optval, int __user *optlen)
5911{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005912 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005913 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005914 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005915 __u16 data_len = 0;
5916 u32 num_idents;
Xin Long7a84bd42016-02-03 23:33:30 +08005917 int i;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005918
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005919 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005920 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005921
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005922 hmacs = ep->auth_hmacs_list;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005923 data_len = ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005924
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005925 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005926 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005927
5928 len = sizeof(struct sctp_hmacalgo) + data_len;
5929 num_idents = data_len / sizeof(u16);
5930
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005931 if (put_user(len, optlen))
5932 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005933 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005934 return -EFAULT;
Xin Long7a84bd42016-02-03 23:33:30 +08005935 for (i = 0; i < num_idents; i++) {
5936 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
5937
5938 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
5939 return -EFAULT;
5940 }
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005941 return 0;
5942}
5943
5944static int sctp_getsockopt_active_key(struct sock *sk, int len,
5945 char __user *optval, int __user *optlen)
5946{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005947 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005948 struct sctp_authkeyid val;
5949 struct sctp_association *asoc;
5950
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005951 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005952 return -EACCES;
5953
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005954 if (len < sizeof(struct sctp_authkeyid))
5955 return -EINVAL;
Marcelo Ricardo Leitnerb923c862018-01-08 19:02:29 -02005956
5957 len = sizeof(struct sctp_authkeyid);
5958 if (copy_from_user(&val, optval, len))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005959 return -EFAULT;
5960
5961 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
5962 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
5963 return -EINVAL;
5964
5965 if (asoc)
5966 val.scact_keynumber = asoc->active_key_id;
5967 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005968 val.scact_keynumber = ep->active_key_id;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005969
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005970 if (put_user(len, optlen))
5971 return -EFAULT;
5972 if (copy_to_user(optval, &val, len))
5973 return -EFAULT;
5974
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005975 return 0;
5976}
5977
5978static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
5979 char __user *optval, int __user *optlen)
5980{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005981 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01005982 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005983 struct sctp_authchunks val;
5984 struct sctp_association *asoc;
5985 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005986 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005987 char __user *to;
5988
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005989 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005990 return -EACCES;
5991
5992 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005993 return -EINVAL;
5994
Marcelo Ricardo Leitnerb923c862018-01-08 19:02:29 -02005995 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005996 return -EFAULT;
5997
Al Viro411223c2007-10-14 19:21:20 +01005998 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005999 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6000 if (!asoc)
6001 return -EINVAL;
6002
6003 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006004 if (!ch)
6005 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006006
6007 /* See if the user provided enough room for all the data */
Vlad Yasevichb40db682008-02-27 14:40:37 -05006008 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
6009 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006010 return -EINVAL;
6011
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006012 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006013 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006014num:
6015 len = sizeof(struct sctp_authchunks) + num_chunks;
wangweidong8d726512013-12-23 12:16:53 +08006016 if (put_user(len, optlen))
6017 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006018 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6019 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006020 return 0;
6021}
6022
6023static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6024 char __user *optval, int __user *optlen)
6025{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006026 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006027 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006028 struct sctp_authchunks val;
6029 struct sctp_association *asoc;
6030 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006031 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006032 char __user *to;
6033
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006034 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006035 return -EACCES;
6036
6037 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006038 return -EINVAL;
6039
Marcelo Ricardo Leitnerb923c862018-01-08 19:02:29 -02006040 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006041 return -EFAULT;
6042
Al Viro411223c2007-10-14 19:21:20 +01006043 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006044 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6045 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6046 return -EINVAL;
6047
6048 if (asoc)
wangweidong26ac8e52013-12-23 12:16:51 +08006049 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006050 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006051 ch = ep->auth_chunk_list;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006052
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006053 if (!ch)
6054 goto num;
6055
Vlad Yasevichb40db682008-02-27 14:40:37 -05006056 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006057 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006058 return -EINVAL;
6059
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006060 if (copy_to_user(to, ch->chunks, num_chunks))
6061 return -EFAULT;
6062num:
6063 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006064 if (put_user(len, optlen))
6065 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006066 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6067 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006068
6069 return 0;
6070}
6071
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08006072/*
6073 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6074 * This option gets the current number of associations that are attached
6075 * to a one-to-many style socket. The option value is an uint32_t.
6076 */
6077static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6078 char __user *optval, int __user *optlen)
6079{
6080 struct sctp_sock *sp = sctp_sk(sk);
6081 struct sctp_association *asoc;
6082 u32 val = 0;
6083
6084 if (sctp_style(sk, TCP))
6085 return -EOPNOTSUPP;
6086
6087 if (len < sizeof(u32))
6088 return -EINVAL;
6089
6090 len = sizeof(u32);
6091
6092 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6093 val++;
6094 }
6095
6096 if (put_user(len, optlen))
6097 return -EFAULT;
6098 if (copy_to_user(optval, &val, len))
6099 return -EFAULT;
6100
6101 return 0;
6102}
6103
Wei Yongjun209ba422011-04-17 17:27:08 +00006104/*
Michio Honda7dc04d72011-04-26 20:16:31 +09006105 * 8.1.23 SCTP_AUTO_ASCONF
6106 * See the corresponding setsockopt entry as description
6107 */
6108static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6109 char __user *optval, int __user *optlen)
6110{
6111 int val = 0;
6112
6113 if (len < sizeof(int))
6114 return -EINVAL;
6115
6116 len = sizeof(int);
6117 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6118 val = 1;
6119 if (put_user(len, optlen))
6120 return -EFAULT;
6121 if (copy_to_user(optval, &val, len))
6122 return -EFAULT;
6123 return 0;
6124}
6125
6126/*
Wei Yongjun209ba422011-04-17 17:27:08 +00006127 * 8.2.6. Get the Current Identifiers of Associations
6128 * (SCTP_GET_ASSOC_ID_LIST)
6129 *
6130 * This option gets the current list of SCTP association identifiers of
6131 * the SCTP associations handled by a one-to-many style socket.
6132 */
6133static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6134 char __user *optval, int __user *optlen)
6135{
6136 struct sctp_sock *sp = sctp_sk(sk);
6137 struct sctp_association *asoc;
6138 struct sctp_assoc_ids *ids;
6139 u32 num = 0;
6140
6141 if (sctp_style(sk, TCP))
6142 return -EOPNOTSUPP;
6143
6144 if (len < sizeof(struct sctp_assoc_ids))
6145 return -EINVAL;
6146
6147 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6148 num++;
6149 }
6150
6151 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6152 return -EINVAL;
6153
6154 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6155
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02006156 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
Wei Yongjun209ba422011-04-17 17:27:08 +00006157 if (unlikely(!ids))
6158 return -ENOMEM;
6159
6160 ids->gaids_number_of_ids = num;
6161 num = 0;
6162 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6163 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6164 }
6165
6166 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6167 kfree(ids);
6168 return -EFAULT;
6169 }
6170
6171 kfree(ids);
6172 return 0;
6173}
6174
Neil Horman5aa93bc2012-07-21 07:56:07 +00006175/*
6176 * SCTP_PEER_ADDR_THLDS
6177 *
6178 * This option allows us to fetch the partially failed threshold for one or all
6179 * transports in an association. See Section 6.1 of:
6180 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6181 */
6182static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6183 char __user *optval,
6184 int len,
6185 int __user *optlen)
6186{
6187 struct sctp_paddrthlds val;
6188 struct sctp_transport *trans;
6189 struct sctp_association *asoc;
6190
6191 if (len < sizeof(struct sctp_paddrthlds))
6192 return -EINVAL;
6193 len = sizeof(struct sctp_paddrthlds);
6194 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6195 return -EFAULT;
6196
6197 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6198 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6199 if (!asoc)
6200 return -ENOENT;
6201
6202 val.spt_pathpfthld = asoc->pf_retrans;
6203 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6204 } else {
6205 trans = sctp_addr_id2transport(sk, &val.spt_address,
6206 val.spt_assoc_id);
6207 if (!trans)
6208 return -ENOENT;
6209
6210 val.spt_pathmaxrxt = trans->pathmaxrxt;
6211 val.spt_pathpfthld = trans->pf_retrans;
6212 }
6213
6214 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6215 return -EFAULT;
6216
6217 return 0;
6218}
6219
Michele Baldessari196d6752012-12-01 04:49:42 +00006220/*
6221 * SCTP_GET_ASSOC_STATS
6222 *
6223 * This option retrieves local per endpoint statistics. It is modeled
6224 * after OpenSolaris' implementation
6225 */
6226static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6227 char __user *optval,
6228 int __user *optlen)
6229{
6230 struct sctp_assoc_stats sas;
6231 struct sctp_association *asoc = NULL;
6232
6233 /* User must provide at least the assoc id */
6234 if (len < sizeof(sctp_assoc_t))
6235 return -EINVAL;
6236
Guenter Roeck726bc6b2013-02-27 10:57:31 +00006237 /* Allow the struct to grow and fill in as much as possible */
6238 len = min_t(size_t, len, sizeof(sas));
6239
Michele Baldessari196d6752012-12-01 04:49:42 +00006240 if (copy_from_user(&sas, optval, len))
6241 return -EFAULT;
6242
6243 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6244 if (!asoc)
6245 return -EINVAL;
6246
6247 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6248 sas.sas_gapcnt = asoc->stats.gapcnt;
6249 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6250 sas.sas_osacks = asoc->stats.osacks;
6251 sas.sas_isacks = asoc->stats.isacks;
6252 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6253 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6254 sas.sas_oodchunks = asoc->stats.oodchunks;
6255 sas.sas_iodchunks = asoc->stats.iodchunks;
6256 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6257 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6258 sas.sas_idupchunks = asoc->stats.idupchunks;
6259 sas.sas_opackets = asoc->stats.opackets;
6260 sas.sas_ipackets = asoc->stats.ipackets;
6261
6262 /* New high max rto observed, will return 0 if not a single
6263 * RTO update took place. obs_rto_ipaddr will be bogus
6264 * in such a case
6265 */
6266 sas.sas_maxrto = asoc->stats.max_obs_rto;
6267 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6268 sizeof(struct sockaddr_storage));
6269
6270 /* Mark beginning of a new observation period */
6271 asoc->stats.max_obs_rto = asoc->rto_min;
6272
Michele Baldessari196d6752012-12-01 04:49:42 +00006273 if (put_user(len, optlen))
6274 return -EFAULT;
6275
Daniel Borkmannbb333812013-06-28 19:49:40 +02006276 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
Michele Baldessari196d6752012-12-01 04:49:42 +00006277
6278 if (copy_to_user(optval, &sas, len))
6279 return -EFAULT;
6280
6281 return 0;
6282}
6283
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02006284static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6285 char __user *optval,
6286 int __user *optlen)
6287{
6288 int val = 0;
6289
6290 if (len < sizeof(int))
6291 return -EINVAL;
6292
6293 len = sizeof(int);
6294 if (sctp_sk(sk)->recvrcvinfo)
6295 val = 1;
6296 if (put_user(len, optlen))
6297 return -EFAULT;
6298 if (copy_to_user(optval, &val, len))
6299 return -EFAULT;
6300
6301 return 0;
6302}
6303
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02006304static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6305 char __user *optval,
6306 int __user *optlen)
6307{
6308 int val = 0;
6309
6310 if (len < sizeof(int))
6311 return -EINVAL;
6312
6313 len = sizeof(int);
6314 if (sctp_sk(sk)->recvnxtinfo)
6315 val = 1;
6316 if (put_user(len, optlen))
6317 return -EFAULT;
6318 if (copy_to_user(optval, &val, len))
6319 return -EFAULT;
6320
6321 return 0;
6322}
6323
Xin Long28aa4c22016-07-09 19:47:40 +08006324static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6325 char __user *optval,
6326 int __user *optlen)
6327{
6328 struct sctp_assoc_value params;
6329 struct sctp_association *asoc;
6330 int retval = -EFAULT;
6331
6332 if (len < sizeof(params)) {
6333 retval = -EINVAL;
6334 goto out;
6335 }
6336
6337 len = sizeof(params);
6338 if (copy_from_user(&params, optval, len))
6339 goto out;
6340
6341 asoc = sctp_id2assoc(sk, params.assoc_id);
6342 if (asoc) {
6343 params.assoc_value = asoc->prsctp_enable;
6344 } else if (!params.assoc_id) {
6345 struct sctp_sock *sp = sctp_sk(sk);
6346
6347 params.assoc_value = sp->ep->prsctp_enable;
6348 } else {
6349 retval = -EINVAL;
6350 goto out;
6351 }
6352
6353 if (put_user(len, optlen))
6354 goto out;
6355
6356 if (copy_to_user(optval, &params, len))
6357 goto out;
6358
6359 retval = 0;
6360
6361out:
6362 return retval;
6363}
6364
Xin Longf959fb42016-07-09 19:47:41 +08006365static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
6366 char __user *optval,
6367 int __user *optlen)
6368{
6369 struct sctp_default_prinfo info;
6370 struct sctp_association *asoc;
6371 int retval = -EFAULT;
6372
6373 if (len < sizeof(info)) {
6374 retval = -EINVAL;
6375 goto out;
6376 }
6377
6378 len = sizeof(info);
6379 if (copy_from_user(&info, optval, len))
6380 goto out;
6381
6382 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
6383 if (asoc) {
6384 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
6385 info.pr_value = asoc->default_timetolive;
6386 } else if (!info.pr_assoc_id) {
6387 struct sctp_sock *sp = sctp_sk(sk);
6388
6389 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
6390 info.pr_value = sp->default_timetolive;
6391 } else {
6392 retval = -EINVAL;
6393 goto out;
6394 }
6395
6396 if (put_user(len, optlen))
6397 goto out;
6398
6399 if (copy_to_user(optval, &info, len))
6400 goto out;
6401
6402 retval = 0;
6403
6404out:
6405 return retval;
6406}
6407
Xin Long826d2532016-07-09 19:47:42 +08006408static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
6409 char __user *optval,
6410 int __user *optlen)
6411{
6412 struct sctp_prstatus params;
6413 struct sctp_association *asoc;
6414 int policy;
6415 int retval = -EINVAL;
6416
6417 if (len < sizeof(params))
6418 goto out;
6419
6420 len = sizeof(params);
6421 if (copy_from_user(&params, optval, len)) {
6422 retval = -EFAULT;
6423 goto out;
6424 }
6425
6426 policy = params.sprstat_policy;
6427 if (policy & ~SCTP_PR_SCTP_MASK)
6428 goto out;
6429
6430 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
6431 if (!asoc)
6432 goto out;
6433
6434 if (policy == SCTP_PR_SCTP_NONE) {
6435 params.sprstat_abandoned_unsent = 0;
6436 params.sprstat_abandoned_sent = 0;
6437 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6438 params.sprstat_abandoned_unsent +=
6439 asoc->abandoned_unsent[policy];
6440 params.sprstat_abandoned_sent +=
6441 asoc->abandoned_sent[policy];
6442 }
6443 } else {
6444 params.sprstat_abandoned_unsent =
6445 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
6446 params.sprstat_abandoned_sent =
6447 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
6448 }
6449
6450 if (put_user(len, optlen)) {
6451 retval = -EFAULT;
6452 goto out;
6453 }
6454
6455 if (copy_to_user(optval, &params, len)) {
6456 retval = -EFAULT;
6457 goto out;
6458 }
6459
6460 retval = 0;
6461
6462out:
6463 return retval;
6464}
6465
Daniel Borkmanndda91922013-06-17 11:40:05 +02006466static int sctp_getsockopt(struct sock *sk, int level, int optname,
6467 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468{
6469 int retval = 0;
6470 int len;
6471
Daniel Borkmannbb333812013-06-28 19:49:40 +02006472 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473
6474 /* I can hardly begin to describe how wrong this is. This is
6475 * so broken as to be worse than useless. The API draft
6476 * REALLY is NOT helpful here... I am not convinced that the
6477 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
6478 * are at all well-founded.
6479 */
6480 if (level != SOL_SCTP) {
6481 struct sctp_af *af = sctp_sk(sk)->pf->af;
6482
6483 retval = af->getsockopt(sk, level, optname, optval, optlen);
6484 return retval;
6485 }
6486
6487 if (get_user(len, optlen))
6488 return -EFAULT;
6489
Jiri Slabya4b8e712016-10-21 14:13:24 +02006490 if (len < 0)
6491 return -EINVAL;
6492
wangweidong048ed4b2014-01-21 15:44:11 +08006493 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494
6495 switch (optname) {
6496 case SCTP_STATUS:
6497 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
6498 break;
6499 case SCTP_DISABLE_FRAGMENTS:
6500 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
6501 optlen);
6502 break;
6503 case SCTP_EVENTS:
6504 retval = sctp_getsockopt_events(sk, len, optval, optlen);
6505 break;
6506 case SCTP_AUTOCLOSE:
6507 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
6508 break;
6509 case SCTP_SOCKOPT_PEELOFF:
6510 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
6511 break;
6512 case SCTP_PEER_ADDR_PARAMS:
6513 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
6514 optlen);
6515 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00006516 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07006517 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08006518 optlen);
6519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 case SCTP_INITMSG:
6521 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
6522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 case SCTP_GET_PEER_ADDRS:
6524 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
6525 optlen);
6526 break;
6527 case SCTP_GET_LOCAL_ADDRS:
6528 retval = sctp_getsockopt_local_addrs(sk, len, optval,
6529 optlen);
6530 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04006531 case SCTP_SOCKOPT_CONNECTX3:
6532 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
6533 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534 case SCTP_DEFAULT_SEND_PARAM:
6535 retval = sctp_getsockopt_default_send_param(sk, len,
6536 optval, optlen);
6537 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006538 case SCTP_DEFAULT_SNDINFO:
6539 retval = sctp_getsockopt_default_sndinfo(sk, len,
6540 optval, optlen);
6541 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542 case SCTP_PRIMARY_ADDR:
6543 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
6544 break;
6545 case SCTP_NODELAY:
6546 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
6547 break;
6548 case SCTP_RTOINFO:
6549 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
6550 break;
6551 case SCTP_ASSOCINFO:
6552 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
6553 break;
6554 case SCTP_I_WANT_MAPPED_V4_ADDR:
6555 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
6556 break;
6557 case SCTP_MAXSEG:
6558 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
6559 break;
6560 case SCTP_GET_PEER_ADDR_INFO:
6561 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
6562 optlen);
6563 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006564 case SCTP_ADAPTATION_LAYER:
6565 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 optlen);
6567 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006568 case SCTP_CONTEXT:
6569 retval = sctp_getsockopt_context(sk, len, optval, optlen);
6570 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006571 case SCTP_FRAGMENT_INTERLEAVE:
6572 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
6573 optlen);
6574 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006575 case SCTP_PARTIAL_DELIVERY_POINT:
6576 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
6577 optlen);
6578 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07006579 case SCTP_MAX_BURST:
6580 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
6581 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006582 case SCTP_AUTH_KEY:
6583 case SCTP_AUTH_CHUNK:
6584 case SCTP_AUTH_DELETE_KEY:
6585 retval = -EOPNOTSUPP;
6586 break;
6587 case SCTP_HMAC_IDENT:
6588 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
6589 break;
6590 case SCTP_AUTH_ACTIVE_KEY:
6591 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
6592 break;
6593 case SCTP_PEER_AUTH_CHUNKS:
6594 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
6595 optlen);
6596 break;
6597 case SCTP_LOCAL_AUTH_CHUNKS:
6598 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
6599 optlen);
6600 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08006601 case SCTP_GET_ASSOC_NUMBER:
6602 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
6603 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00006604 case SCTP_GET_ASSOC_ID_LIST:
6605 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
6606 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09006607 case SCTP_AUTO_ASCONF:
6608 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
6609 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00006610 case SCTP_PEER_ADDR_THLDS:
6611 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
6612 break;
Michele Baldessari196d6752012-12-01 04:49:42 +00006613 case SCTP_GET_ASSOC_STATS:
6614 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
6615 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02006616 case SCTP_RECVRCVINFO:
6617 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
6618 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02006619 case SCTP_RECVNXTINFO:
6620 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
6621 break;
Xin Long28aa4c22016-07-09 19:47:40 +08006622 case SCTP_PR_SUPPORTED:
6623 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
6624 break;
Xin Longf959fb42016-07-09 19:47:41 +08006625 case SCTP_DEFAULT_PRINFO:
6626 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
6627 optlen);
6628 break;
Xin Long826d2532016-07-09 19:47:42 +08006629 case SCTP_PR_ASSOC_STATUS:
6630 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
6631 optlen);
6632 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633 default:
6634 retval = -ENOPROTOOPT;
6635 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07006636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637
wangweidong048ed4b2014-01-21 15:44:11 +08006638 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639 return retval;
6640}
6641
Craig Gallek086c6532016-02-10 11:50:35 -05006642static int sctp_hash(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643{
6644 /* STUB */
Craig Gallek086c6532016-02-10 11:50:35 -05006645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646}
6647
6648static void sctp_unhash(struct sock *sk)
6649{
6650 /* STUB */
6651}
6652
6653/* Check if port is acceptable. Possibly find first available port.
6654 *
6655 * The port hash table (contained in the 'global' SCTP protocol storage
6656 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
6657 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
6658 * list (the list number is the port number hashed out, so as you
6659 * would expect from a hash function, all the ports in a given list have
6660 * such a number that hashes out to the same list number; you were
6661 * expecting that, right?); so each list has a set of ports, with a
6662 * link to the socket (struct sock) that uses it, the port number and
6663 * a fastreuse flag (FIXME: NPI ipg).
6664 */
6665static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006666 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667
6668static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
6669{
6670 struct sctp_bind_hashbucket *head; /* hash list */
Sasha Levinb67bfe02013-02-27 17:06:00 -08006671 struct sctp_bind_bucket *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 unsigned short snum;
6673 int ret;
6674
Al Viro04afd8b2006-11-20 17:02:01 -08006675 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676
Daniel Borkmannbb333812013-06-28 19:49:40 +02006677 pr_debug("%s: begins, snum:%d\n", __func__, snum);
6678
wangweidong79b91132014-01-21 15:44:07 +08006679 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680
6681 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07006682 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07006683 int low, high, remaining, index;
6684 unsigned int rover;
WANG Cong122ff242014-05-12 16:04:53 -07006685 struct net *net = sock_net(sk);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07006686
WANG Cong122ff242014-05-12 16:04:53 -07006687 inet_get_local_port_range(net, &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07006688 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05006689 rover = prandom_u32() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 do {
6692 rover++;
6693 if ((rover < low) || (rover > high))
6694 rover = low;
WANG Cong122ff242014-05-12 16:04:53 -07006695 if (inet_is_local_reserved_port(net, rover))
Amerigo Wange3826f12010-05-05 00:27:06 +00006696 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006697 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 head = &sctp_port_hashtable[index];
wangweidong3c8e43b2014-01-21 15:44:08 +08006699 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08006700 sctp_for_each_hentry(pp, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006701 if ((pp->port == rover) &&
6702 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 goto next;
6704 break;
6705 next:
wangweidong3c8e43b2014-01-21 15:44:08 +08006706 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708
6709 /* Exhausted local port range during search? */
6710 ret = 1;
6711 if (remaining <= 0)
6712 goto fail;
6713
6714 /* OK, here is the one we will use. HEAD (the port
6715 * hash table list entry) is non-NULL and we hold it's
6716 * mutex.
6717 */
6718 snum = rover;
6719 } else {
6720 /* We are given an specific port number; we verify
6721 * that it is not being used. If it is used, we will
6722 * exahust the search in the hash list corresponding
6723 * to the port number (snum) - we detect that with the
6724 * port iterator, pp being NULL.
6725 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006726 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
wangweidong3c8e43b2014-01-21 15:44:08 +08006727 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08006728 sctp_for_each_hentry(pp, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006729 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730 goto pp_found;
6731 }
6732 }
6733 pp = NULL;
6734 goto pp_not_found;
6735pp_found:
6736 if (!hlist_empty(&pp->owner)) {
6737 /* We had a port hash table hit - there is an
6738 * available port (pp != NULL) and it is being
6739 * used by other socket (pp->owner not empty); that other
6740 * socket is going to be sk2.
6741 */
6742 int reuse = sk->sk_reuse;
6743 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744
Daniel Borkmannbb333812013-06-28 19:49:40 +02006745 pr_debug("%s: found a possible match\n", __func__);
6746
Vlad Yasevichce5325c2007-05-04 13:34:49 -07006747 if (pp->fastreuse && sk->sk_reuse &&
6748 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749 goto success;
6750
6751 /* Run through the list of sockets bound to the port
6752 * (pp->port) [via the pointers bind_next and
6753 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
6754 * we get the endpoint they describe and run through
6755 * the endpoint's list of IP (v4 or v6) addresses,
6756 * comparing each of the addresses with the address of
6757 * the socket sk. If we find a match, then that means
6758 * that this port/socket (sk) combination are already
6759 * in an endpoint.
6760 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08006761 sk_for_each_bound(sk2, &pp->owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 struct sctp_endpoint *ep2;
6763 ep2 = sctp_sk(sk2)->ep;
6764
Vlad Yasevich4e540642008-07-18 23:06:32 -07006765 if (sk == sk2 ||
6766 (reuse && sk2->sk_reuse &&
6767 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 continue;
6769
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07006770 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
6771 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 ret = (long)sk2;
6773 goto fail_unlock;
6774 }
6775 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02006776
6777 pr_debug("%s: found a match\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 }
6779pp_not_found:
6780 /* If there was a hash table miss, create a new port. */
6781 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006782 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 goto fail_unlock;
6784
6785 /* In either case (hit or miss), make sure fastreuse is 1 only
6786 * if sk->sk_reuse is too (that is, if the caller requested
6787 * SO_REUSEADDR on this socket -sk-).
6788 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07006789 if (hlist_empty(&pp->owner)) {
6790 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
6791 pp->fastreuse = 1;
6792 else
6793 pp->fastreuse = 0;
6794 } else if (pp->fastreuse &&
6795 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 pp->fastreuse = 0;
6797
6798 /* We are set, so fill up all the data in the hash table
6799 * entry, tie the socket list information with the rest of the
6800 * sockets FIXME: Blurry, NPI (ipg).
6801 */
6802success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006804 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 sk_add_bind_node(sk, &pp->owner);
6806 sctp_sk(sk)->bind_hash = pp;
6807 }
6808 ret = 0;
6809
6810fail_unlock:
wangweidong3c8e43b2014-01-21 15:44:08 +08006811 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812
6813fail:
wangweidong79b91132014-01-21 15:44:07 +08006814 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 return ret;
6816}
6817
6818/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
6819 * port is requested.
6820 */
6821static int sctp_get_port(struct sock *sk, unsigned short snum)
6822{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 union sctp_addr addr;
6824 struct sctp_af *af = sctp_sk(sk)->pf->af;
6825
6826 /* Set up a dummy address struct from the sk. */
6827 af->from_sk(&addr, sk);
6828 addr.v4.sin_port = htons(snum);
6829
6830 /* Note: sk->sk_num gets filled in if ephemeral port request. */
Daniel Borkmann62208f12013-06-25 18:17:30 +02006831 return !!sctp_get_port_local(sk, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832}
6833
6834/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 * Move a socket to LISTENING state.
6836 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02006837static int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006839 struct sctp_sock *sp = sctp_sk(sk);
6840 struct sctp_endpoint *ep = sp->ep;
Herbert Xu5821c762016-01-24 21:20:12 +08006841 struct crypto_shash *tfm = NULL;
Neil Horman3c681982012-10-24 09:20:03 +00006842 char alg[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843
6844 /* Allocate HMAC for generating cookie. */
Neil Horman3c681982012-10-24 09:20:03 +00006845 if (!sp->hmac && sp->sctp_hmac_alg) {
6846 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
Herbert Xu5821c762016-01-24 21:20:12 +08006847 tfm = crypto_alloc_shash(alg, 0, 0);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07006848 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00006849 net_info_ratelimited("failed to load transform for %s: %ld\n",
Neil Horman3c681982012-10-24 09:20:03 +00006850 sp->sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006851 return -ENOSYS;
6852 }
6853 sctp_sk(sk)->hmac = tfm;
6854 }
6855
6856 /*
6857 * If a bind() or sctp_bindx() is not called prior to a listen()
6858 * call that allows new associations to be accepted, the system
6859 * picks an ephemeral port and will choose an address set equivalent
6860 * to binding with a wildcard address.
6861 *
6862 * This is not currently spelled out in the SCTP sockets
6863 * extensions draft, but follows the practice as seen in TCP
6864 * sockets.
6865 *
6866 */
6867 sk->sk_state = SCTP_SS_LISTENING;
6868 if (!ep->base.bind_addr.port) {
6869 if (sctp_autobind(sk))
6870 return -EAGAIN;
6871 } else {
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006872 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006873 sk->sk_state = SCTP_SS_CLOSED;
6874 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 }
6876 }
6877
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006878 sk->sk_max_ack_backlog = backlog;
6879 sctp_hash_endpoint(ep);
6880 return 0;
6881}
6882
6883/*
6884 * 4.1.3 / 5.1.3 listen()
6885 *
6886 * By default, new associations are not accepted for UDP style sockets.
6887 * An application uses listen() to mark a socket as being able to
6888 * accept new associations.
6889 *
6890 * On TCP style sockets, applications use listen() to ready the SCTP
6891 * endpoint for accepting inbound associations.
6892 *
6893 * On both types of endpoints a backlog of '0' disables listening.
6894 *
6895 * Move a socket to LISTENING state.
6896 */
6897int sctp_inet_listen(struct socket *sock, int backlog)
6898{
6899 struct sock *sk = sock->sk;
6900 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6901 int err = -EINVAL;
6902
6903 if (unlikely(backlog < 0))
6904 return err;
6905
wangweidong048ed4b2014-01-21 15:44:11 +08006906 lock_sock(sk);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006907
6908 /* Peeled-off sockets are not allowed to listen(). */
6909 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
6910 goto out;
6911
6912 if (sock->state != SS_UNCONNECTED)
6913 goto out;
6914
Xin Long02f04302017-04-06 13:10:52 +08006915 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
6916 goto out;
6917
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006918 /* If backlog is zero, disable listening. */
6919 if (!backlog) {
6920 if (sctp_sstate(sk, CLOSED))
6921 goto out;
6922
6923 err = 0;
6924 sctp_unhash_endpoint(ep);
6925 sk->sk_state = SCTP_SS_CLOSED;
6926 if (sk->sk_reuse)
6927 sctp_sk(sk)->bind_hash->fastreuse = 1;
6928 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07006929 }
6930
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006931 /* If we are already listening, just update the backlog */
6932 if (sctp_sstate(sk, LISTENING))
6933 sk->sk_max_ack_backlog = backlog;
6934 else {
6935 err = sctp_listen_start(sk, backlog);
6936 if (err)
6937 goto out;
6938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006940 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941out:
wangweidong048ed4b2014-01-21 15:44:11 +08006942 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944}
6945
6946/*
6947 * This function is done by modeling the current datagram_poll() and the
6948 * tcp_poll(). Note that, based on these implementations, we don't
6949 * lock the socket in this function, even though it seems that,
6950 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08006951 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952 * in this place. We assume that we don't need locks either until proven
6953 * otherwise.
6954 *
6955 * Another thing to note is that we include the Async I/O support
6956 * here, again, by modeling the current TCP/UDP code. We don't have
6957 * a good way to test with it yet.
6958 */
6959unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
6960{
6961 struct sock *sk = sock->sk;
6962 struct sctp_sock *sp = sctp_sk(sk);
6963 unsigned int mask;
6964
Eric Dumazetaa395142010-04-20 13:03:51 +00006965 poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03006967 sock_rps_record_flow(sk);
6968
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969 /* A TCP-style listening socket becomes readable when the accept queue
6970 * is not empty.
6971 */
6972 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
6973 return (!list_empty(&sp->ep->asocs)) ?
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006974 (POLLIN | POLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975
6976 mask = 0;
6977
6978 /* Is there any exceptional events? */
6979 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +00006980 mask |= POLLERR |
Daniel Borkmanna0fb05d2013-09-07 16:44:59 +02006981 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
Davide Libenzif348d702006-03-25 03:07:39 -08006982 if (sk->sk_shutdown & RCV_SHUTDOWN)
Eric Dumazetdb409802010-09-06 11:13:50 +00006983 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984 if (sk->sk_shutdown == SHUTDOWN_MASK)
6985 mask |= POLLHUP;
6986
6987 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00006988 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989 mask |= POLLIN | POLLRDNORM;
6990
6991 /* The association is either gone or not ready. */
6992 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
6993 return mask;
6994
6995 /* Is it writable? */
6996 if (sctp_writeable(sk)) {
6997 mask |= POLLOUT | POLLWRNORM;
6998 } else {
Eric Dumazet9cd3e072015-11-29 20:03:10 -08006999 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 /*
7001 * Since the socket is not locked, the buffer
7002 * might be made available after the writeable check and
7003 * before the bit is set. This could cause a lost I/O
7004 * signal. tcp_poll() has a race breaker for this race
7005 * condition. Based on their implementation, we put
7006 * in the following code to cover it as well.
7007 */
7008 if (sctp_writeable(sk))
7009 mask |= POLLOUT | POLLWRNORM;
7010 }
7011 return mask;
7012}
7013
7014/********************************************************************
7015 * 2nd Level Abstractions
7016 ********************************************************************/
7017
7018static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007019 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020{
7021 struct sctp_bind_bucket *pp;
7022
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08007023 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07007025 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 pp->port = snum;
7027 pp->fastreuse = 0;
7028 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007029 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007030 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031 }
7032 return pp;
7033}
7034
7035/* Caller must hold hashbucket lock for this tb with local BH disabled */
7036static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
7037{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07007038 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007039 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040 kmem_cache_free(sctp_bucket_cachep, pp);
7041 SCTP_DBG_OBJCNT_DEC(bind_bucket);
7042 }
7043}
7044
7045/* Release this socket's reference to a local port. */
7046static inline void __sctp_put_port(struct sock *sk)
7047{
7048 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007049 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
7050 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 struct sctp_bind_bucket *pp;
7052
wangweidong3c8e43b2014-01-21 15:44:08 +08007053 spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 pp = sctp_sk(sk)->bind_hash;
7055 __sk_del_bind_node(sk);
7056 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00007057 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 sctp_bucket_destroy(pp);
wangweidong3c8e43b2014-01-21 15:44:08 +08007059 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060}
7061
7062void sctp_put_port(struct sock *sk)
7063{
wangweidong79b91132014-01-21 15:44:07 +08007064 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 __sctp_put_port(sk);
wangweidong79b91132014-01-21 15:44:07 +08007066 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067}
7068
7069/*
7070 * The system picks an ephemeral port and choose an address set equivalent
7071 * to binding with a wildcard address.
7072 * One of those addresses will be the primary address for the association.
7073 * This automatically enables the multihoming capability of SCTP.
7074 */
7075static int sctp_autobind(struct sock *sk)
7076{
7077 union sctp_addr autoaddr;
7078 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08007079 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080
7081 /* Initialize a local sockaddr structure to INADDR_ANY. */
7082 af = sctp_sk(sk)->pf->af;
7083
Eric Dumazetc720c7e2009-10-15 06:30:45 +00007084 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 af->inaddr_any(&autoaddr, port);
7086
7087 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
7088}
7089
7090/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
7091 *
7092 * From RFC 2292
7093 * 4.2 The cmsghdr Structure *
7094 *
7095 * When ancillary data is sent or received, any number of ancillary data
7096 * objects can be specified by the msg_control and msg_controllen members of
7097 * the msghdr structure, because each object is preceded by
7098 * a cmsghdr structure defining the object's length (the cmsg_len member).
7099 * Historically Berkeley-derived implementations have passed only one object
7100 * at a time, but this API allows multiple objects to be
7101 * passed in a single call to sendmsg() or recvmsg(). The following example
7102 * shows two ancillary data objects in a control buffer.
7103 *
7104 * |<--------------------------- msg_controllen -------------------------->|
7105 * | |
7106 *
7107 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
7108 *
7109 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
7110 * | | |
7111 *
7112 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
7113 *
7114 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
7115 * | | | | |
7116 *
7117 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7118 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
7119 *
7120 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
7121 *
7122 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7123 * ^
7124 * |
7125 *
7126 * msg_control
7127 * points here
7128 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02007129static int sctp_msghdr_parse(const struct msghdr *msg, sctp_cmsgs_t *cmsgs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130{
7131 struct cmsghdr *cmsg;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007132 struct msghdr *my_msg = (struct msghdr *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133
Gu Zhengf95b4142014-12-11 11:22:04 +08007134 for_each_cmsghdr(cmsg, my_msg) {
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007135 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 return -EINVAL;
7137
7138 /* Should we parse this header or ignore? */
7139 if (cmsg->cmsg_level != IPPROTO_SCTP)
7140 continue;
7141
7142 /* Strictly check lengths following example in SCM code. */
7143 switch (cmsg->cmsg_type) {
7144 case SCTP_INIT:
7145 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007146 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 *
7148 * This cmsghdr structure provides information for
7149 * initializing new SCTP associations with sendmsg().
7150 * The SCTP_INITMSG socket option uses this same data
7151 * structure. This structure is not used for
7152 * recvmsg().
7153 *
7154 * cmsg_level cmsg_type cmsg_data[]
7155 * ------------ ------------ ----------------------
7156 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
7157 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007158 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 return -EINVAL;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007160
7161 cmsgs->init = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 break;
7163
7164 case SCTP_SNDRCV:
7165 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007166 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167 *
7168 * This cmsghdr structure specifies SCTP options for
7169 * sendmsg() and describes SCTP header information
7170 * about a received message through recvmsg().
7171 *
7172 * cmsg_level cmsg_type cmsg_data[]
7173 * ------------ ------------ ----------------------
7174 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
7175 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007176 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177 return -EINVAL;
7178
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007179 cmsgs->srinfo = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007181 if (cmsgs->srinfo->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007182 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Longa6c2f792016-07-09 19:47:43 +08007183 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007184 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 return -EINVAL;
7186 break;
7187
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007188 case SCTP_SNDINFO:
7189 /* SCTP Socket API Extension
7190 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
7191 *
7192 * This cmsghdr structure specifies SCTP options for
7193 * sendmsg(). This structure and SCTP_RCVINFO replaces
7194 * SCTP_SNDRCV which has been deprecated.
7195 *
7196 * cmsg_level cmsg_type cmsg_data[]
7197 * ------------ ------------ ---------------------
7198 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
7199 */
7200 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
7201 return -EINVAL;
7202
7203 cmsgs->sinfo = CMSG_DATA(cmsg);
7204
7205 if (cmsgs->sinfo->snd_flags &
7206 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Longa6c2f792016-07-09 19:47:43 +08007207 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007208 SCTP_ABORT | SCTP_EOF))
7209 return -EINVAL;
7210 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211 default:
7212 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 }
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007215
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216 return 0;
7217}
7218
7219/*
7220 * Wait for a packet..
7221 * Note: This function is the same function as in core/datagram.c
7222 * with a few modifications to make lksctp work.
7223 */
wangweidong26ac8e52013-12-23 12:16:51 +08007224static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225{
7226 int error;
7227 DEFINE_WAIT(wait);
7228
Eric Dumazetaa395142010-04-20 13:03:51 +00007229 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230
7231 /* Socket errors? */
7232 error = sock_error(sk);
7233 if (error)
7234 goto out;
7235
7236 if (!skb_queue_empty(&sk->sk_receive_queue))
7237 goto ready;
7238
7239 /* Socket shut down? */
7240 if (sk->sk_shutdown & RCV_SHUTDOWN)
7241 goto out;
7242
7243 /* Sequenced packets can come disconnected. If so we report the
7244 * problem.
7245 */
7246 error = -ENOTCONN;
7247
7248 /* Is there a good reason to think that we may receive some data? */
7249 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
7250 goto out;
7251
7252 /* Handle signals. */
7253 if (signal_pending(current))
7254 goto interrupted;
7255
7256 /* Let another process have a go. Since we are going to sleep
7257 * anyway. Note: This may cause odd behaviors if the message
7258 * does not fit in the user's buffer, but this seems to be the
7259 * only way to honor MSG_DONTWAIT realistically.
7260 */
wangweidong048ed4b2014-01-21 15:44:11 +08007261 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262 *timeo_p = schedule_timeout(*timeo_p);
wangweidong048ed4b2014-01-21 15:44:11 +08007263 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264
7265ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00007266 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267 return 0;
7268
7269interrupted:
7270 error = sock_intr_errno(*timeo_p);
7271
7272out:
Eric Dumazetaa395142010-04-20 13:03:51 +00007273 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274 *err = error;
7275 return error;
7276}
7277
7278/* Receive a datagram.
7279 * Note: This is pretty much the same routine as in core/datagram.c
7280 * with a few changes to make lksctp work.
7281 */
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007282struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
7283 int noblock, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284{
7285 int error;
7286 struct sk_buff *skb;
7287 long timeo;
7288
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289 timeo = sock_rcvtimeo(sk, noblock);
7290
Daniel Borkmannbb333812013-06-28 19:49:40 +02007291 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
7292 MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293
7294 do {
7295 /* Again only user level code calls this function,
7296 * so nothing interrupt level
7297 * will suddenly eat the receive_queue.
7298 *
7299 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00007300 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301 */
7302 if (flags & MSG_PEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303 skb = skb_peek(&sk->sk_receive_queue);
7304 if (skb)
7305 atomic_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306 } else {
Marcelo Ricardo Leitner311b2172016-04-13 19:12:29 -03007307 skb = __skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 }
7309
7310 if (skb)
7311 return skb;
7312
Neil Horman6736dc32005-12-02 20:30:06 -08007313 /* Caller is allowed not to check sk->sk_err before calling. */
7314 error = sock_error(sk);
7315 if (error)
7316 goto no_packet;
7317
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 if (sk->sk_shutdown & RCV_SHUTDOWN)
7319 break;
7320
Neil Horman8465a5f2014-04-17 15:26:51 -04007321 if (sk_can_busy_loop(sk) &&
7322 sk_busy_loop(sk, noblock))
7323 continue;
7324
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325 /* User doesn't want to wait. */
7326 error = -EAGAIN;
7327 if (!timeo)
7328 goto no_packet;
7329 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
7330
7331 return NULL;
7332
7333no_packet:
7334 *err = error;
7335 return NULL;
7336}
7337
7338/* If sndbuf has changed, wake up per association sndbuf waiters. */
7339static void __sctp_write_space(struct sctp_association *asoc)
7340{
7341 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342
Eric Dumazetceb5d582015-11-29 20:03:11 -08007343 if (sctp_wspace(asoc) <= 0)
7344 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345
Eric Dumazetceb5d582015-11-29 20:03:11 -08007346 if (waitqueue_active(&asoc->wait))
7347 wake_up_interruptible(&asoc->wait);
Eric Dumazeteaefd112011-02-18 03:26:36 +00007348
Eric Dumazetceb5d582015-11-29 20:03:11 -08007349 if (sctp_writeable(sk)) {
7350 struct socket_wq *wq;
7351
7352 rcu_read_lock();
7353 wq = rcu_dereference(sk->sk_wq);
7354 if (wq) {
7355 if (waitqueue_active(&wq->wait))
7356 wake_up_interruptible(&wq->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357
7358 /* Note that we try to include the Async I/O support
7359 * here by modeling from the current TCP/UDP code.
7360 * We have not tested with it yet.
7361 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00007362 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Eric Dumazetceb5d582015-11-29 20:03:11 -08007363 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08007365 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366 }
7367}
7368
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007369static void sctp_wake_up_waiters(struct sock *sk,
7370 struct sctp_association *asoc)
7371{
7372 struct sctp_association *tmp = asoc;
7373
7374 /* We do accounting for the sndbuf space per association,
7375 * so we only need to wake our own association.
7376 */
7377 if (asoc->ep->sndbuf_policy)
7378 return __sctp_write_space(asoc);
7379
Daniel Borkmann1e1cdf82014-04-09 16:10:20 +02007380 /* If association goes down and is just flushing its
7381 * outq, then just normally notify others.
7382 */
7383 if (asoc->base.dead)
7384 return sctp_write_space(sk);
7385
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007386 /* Accounting for the sndbuf space is per socket, so we
7387 * need to wake up others, try to be fair and in case of
7388 * other associations, let them have a go first instead
7389 * of just doing a sctp_write_space() call.
7390 *
7391 * Note that we reach sctp_wake_up_waiters() only when
7392 * associations free up queued chunks, thus we are under
7393 * lock and the list of associations on a socket is
7394 * guaranteed not to change.
7395 */
7396 for (tmp = list_next_entry(tmp, asocs); 1;
7397 tmp = list_next_entry(tmp, asocs)) {
7398 /* Manually skip the head element. */
7399 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
7400 continue;
7401 /* Wake up association. */
7402 __sctp_write_space(tmp);
7403 /* We've reached the end. */
7404 if (tmp == asoc)
7405 break;
7406 }
7407}
7408
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409/* Do accounting for the sndbuf space.
7410 * Decrement the used sndbuf space of the corresponding association by the
7411 * data size which was just transmitted(freed).
7412 */
7413static void sctp_wfree(struct sk_buff *skb)
7414{
Daniel Borkmannf869c912014-11-20 01:54:48 +01007415 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
7416 struct sctp_association *asoc = chunk->asoc;
7417 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418
Neil Horman4eb701d2005-04-28 12:02:04 -07007419 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
7420 sizeof(struct sk_buff) +
7421 sizeof(struct sctp_chunk);
7422
Neil Horman4eb701d2005-04-28 12:02:04 -07007423 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
7424
Neil Horman4d93df02007-08-15 16:07:44 -07007425 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007426 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
Neil Horman4d93df02007-08-15 16:07:44 -07007427 */
7428 sk->sk_wmem_queued -= skb->truesize;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007429 sk_mem_uncharge(sk, skb->truesize);
Neil Horman4d93df02007-08-15 16:07:44 -07007430
Neil Horman4eb701d2005-04-28 12:02:04 -07007431 sock_wfree(skb);
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007432 sctp_wake_up_waiters(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433
7434 sctp_association_put(asoc);
7435}
7436
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07007437/* Do accounting for the receive space on the socket.
7438 * Accounting for the association is done in ulpevent.c
7439 * We set this as a destructor for the cloned data skbs so that
7440 * accounting is done at the correct time.
7441 */
7442void sctp_sock_rfree(struct sk_buff *skb)
7443{
7444 struct sock *sk = skb->sk;
7445 struct sctp_ulpevent *event = sctp_skb2event(skb);
7446
7447 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07007448
7449 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007450 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07007451 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007452 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07007453}
7454
7455
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456/* Helper function to wait for space in the sndbuf. */
7457static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Long2f056e72018-01-15 17:01:36 +08007458 size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459{
7460 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 long current_timeo = *timeo_p;
7462 DEFINE_WAIT(wait);
Xin Long2f056e72018-01-15 17:01:36 +08007463 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464
Daniel Borkmannbb333812013-06-28 19:49:40 +02007465 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
7466 *timeo_p, msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467
7468 /* Increment the association's refcnt. */
7469 sctp_association_hold(asoc);
7470
7471 /* Wait on the association specific sndbuf space. */
7472 for (;;) {
7473 prepare_to_wait_exclusive(&asoc->wait, &wait,
7474 TASK_INTERRUPTIBLE);
Xin Long1158ecd52017-11-15 16:55:54 +08007475 if (asoc->base.dead)
7476 goto do_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477 if (!*timeo_p)
7478 goto do_nonblock;
Xin Long1158ecd52017-11-15 16:55:54 +08007479 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 goto do_error;
7481 if (signal_pending(current))
7482 goto do_interrupted;
7483 if (msg_len <= sctp_wspace(asoc))
7484 break;
7485
7486 /* Let another process have a go. Since we are going
7487 * to sleep anyway.
7488 */
wangweidong048ed4b2014-01-21 15:44:11 +08007489 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08007491 lock_sock(sk);
Xin Long2f056e72018-01-15 17:01:36 +08007492 if (sk != asoc->base.sk)
7493 goto do_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494
7495 *timeo_p = current_timeo;
7496 }
7497
7498out:
7499 finish_wait(&asoc->wait, &wait);
7500
7501 /* Release the association's refcnt. */
7502 sctp_association_put(asoc);
7503
7504 return err;
7505
Xin Long1158ecd52017-11-15 16:55:54 +08007506do_dead:
7507 err = -ESRCH;
7508 goto out;
7509
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510do_error:
7511 err = -EPIPE;
7512 goto out;
7513
7514do_interrupted:
7515 err = sock_intr_errno(*timeo_p);
7516 goto out;
7517
7518do_nonblock:
7519 err = -EAGAIN;
7520 goto out;
7521}
7522
David S. Miller676d2362014-04-11 16:15:36 -04007523void sctp_data_ready(struct sock *sk)
Wei Yongjun561b1732010-04-28 08:47:18 +00007524{
David S. Miller7ef52732010-05-02 21:43:40 -07007525 struct socket_wq *wq;
7526
7527 rcu_read_lock();
7528 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08007529 if (skwq_has_sleeper(wq))
David S. Miller7ef52732010-05-02 21:43:40 -07007530 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
Wei Yongjun561b1732010-04-28 08:47:18 +00007531 POLLRDNORM | POLLRDBAND);
7532 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07007533 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00007534}
7535
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536/* If socket sndbuf has changed, wake up all per association waiters. */
7537void sctp_write_space(struct sock *sk)
7538{
7539 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007540
7541 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07007542 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007543 __sctp_write_space(asoc);
7544 }
7545}
7546
7547/* Is there any sndbuf space available on the socket?
7548 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08007549 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 * associations on the same socket. For a UDP-style socket with
7551 * multiple associations, it is possible for it to be "unwriteable"
7552 * prematurely. I assume that this is acceptable because
7553 * a premature "unwriteable" is better than an accidental "writeable" which
7554 * would cause an unwanted block under certain circumstances. For the 1-1
7555 * UDP-style sockets or TCP-style sockets, this code should work.
7556 * - Daisy
7557 */
7558static int sctp_writeable(struct sock *sk)
7559{
7560 int amt = 0;
7561
Eric Dumazet31e6d362009-06-17 19:05:41 -07007562 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563 if (amt < 0)
7564 amt = 0;
7565 return amt;
7566}
7567
7568/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
7569 * returns immediately with EINPROGRESS.
7570 */
7571static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
7572{
7573 struct sock *sk = asoc->base.sk;
7574 int err = 0;
7575 long current_timeo = *timeo_p;
7576 DEFINE_WAIT(wait);
7577
Daniel Borkmannbb333812013-06-28 19:49:40 +02007578 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579
7580 /* Increment the association's refcnt. */
7581 sctp_association_hold(asoc);
7582
7583 for (;;) {
7584 prepare_to_wait_exclusive(&asoc->wait, &wait,
7585 TASK_INTERRUPTIBLE);
7586 if (!*timeo_p)
7587 goto do_nonblock;
7588 if (sk->sk_shutdown & RCV_SHUTDOWN)
7589 break;
7590 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
7591 asoc->base.dead)
7592 goto do_error;
7593 if (signal_pending(current))
7594 goto do_interrupted;
7595
7596 if (sctp_state(asoc, ESTABLISHED))
7597 break;
7598
7599 /* Let another process have a go. Since we are going
7600 * to sleep anyway.
7601 */
wangweidong048ed4b2014-01-21 15:44:11 +08007602 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08007604 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605
7606 *timeo_p = current_timeo;
7607 }
7608
7609out:
7610 finish_wait(&asoc->wait, &wait);
7611
7612 /* Release the association's refcnt. */
7613 sctp_association_put(asoc);
7614
7615 return err;
7616
7617do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08007618 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 err = -ETIMEDOUT;
7620 else
7621 err = -ECONNREFUSED;
7622 goto out;
7623
7624do_interrupted:
7625 err = sock_intr_errno(*timeo_p);
7626 goto out;
7627
7628do_nonblock:
7629 err = -EINPROGRESS;
7630 goto out;
7631}
7632
7633static int sctp_wait_for_accept(struct sock *sk, long timeo)
7634{
7635 struct sctp_endpoint *ep;
7636 int err = 0;
7637 DEFINE_WAIT(wait);
7638
7639 ep = sctp_sk(sk)->ep;
7640
7641
7642 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00007643 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644 TASK_INTERRUPTIBLE);
7645
7646 if (list_empty(&ep->asocs)) {
wangweidong048ed4b2014-01-21 15:44:11 +08007647 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648 timeo = schedule_timeout(timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08007649 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650 }
7651
7652 err = -EINVAL;
7653 if (!sctp_sstate(sk, LISTENING))
7654 break;
7655
7656 err = 0;
7657 if (!list_empty(&ep->asocs))
7658 break;
7659
7660 err = sock_intr_errno(timeo);
7661 if (signal_pending(current))
7662 break;
7663
7664 err = -EAGAIN;
7665 if (!timeo)
7666 break;
7667 }
7668
Eric Dumazetaa395142010-04-20 13:03:51 +00007669 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670
7671 return err;
7672}
7673
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02007674static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675{
7676 DEFINE_WAIT(wait);
7677
7678 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00007679 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680 if (list_empty(&sctp_sk(sk)->ep->asocs))
7681 break;
wangweidong048ed4b2014-01-21 15:44:11 +08007682 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683 timeout = schedule_timeout(timeout);
wangweidong048ed4b2014-01-21 15:44:11 +08007684 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685 } while (!signal_pending(current) && timeout);
7686
Eric Dumazetaa395142010-04-20 13:03:51 +00007687 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688}
7689
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007690static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
7691{
7692 struct sk_buff *frag;
7693
7694 if (!skb->data_len)
7695 goto done;
7696
7697 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07007698 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007699 sctp_skb_set_owner_r_frag(frag, sk);
7700
7701done:
7702 sctp_skb_set_owner_r(skb, sk);
7703}
7704
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007705void sctp_copy_sock(struct sock *newsk, struct sock *sk,
7706 struct sctp_association *asoc)
7707{
7708 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08007709 struct inet_sock *newinet;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007710
7711 newsk->sk_type = sk->sk_type;
7712 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
7713 newsk->sk_flags = sk->sk_flags;
Marcelo Ricardo Leitner50a5ffb2015-12-04 15:14:05 -02007714 newsk->sk_tsflags = sk->sk_tsflags;
Tom Herbert28448b82014-05-23 08:47:19 -07007715 newsk->sk_no_check_tx = sk->sk_no_check_tx;
7716 newsk->sk_no_check_rx = sk->sk_no_check_rx;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007717 newsk->sk_reuse = sk->sk_reuse;
7718
7719 newsk->sk_shutdown = sk->sk_shutdown;
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02007720 newsk->sk_destruct = sctp_destruct_sock;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007721 newsk->sk_family = sk->sk_family;
7722 newsk->sk_protocol = IPPROTO_SCTP;
7723 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
7724 newsk->sk_sndbuf = sk->sk_sndbuf;
7725 newsk->sk_rcvbuf = sk->sk_rcvbuf;
7726 newsk->sk_lingertime = sk->sk_lingertime;
7727 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
7728 newsk->sk_sndtimeo = sk->sk_sndtimeo;
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03007729 newsk->sk_rxhash = sk->sk_rxhash;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007730
7731 newinet = inet_sk(newsk);
7732
7733 /* Initialize sk's sport, dport, rcv_saddr and daddr for
7734 * getsockname() and getpeername()
7735 */
Eric Dumazetc720c7e2009-10-15 06:30:45 +00007736 newinet->inet_sport = inet->inet_sport;
7737 newinet->inet_saddr = inet->inet_saddr;
7738 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
7739 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007740 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00007741 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007742
7743 newinet->uc_ttl = inet->uc_ttl;
7744 newinet->mc_loop = 1;
7745 newinet->mc_ttl = 1;
7746 newinet->mc_index = 0;
7747 newinet->mc_list = NULL;
Marcelo Ricardo Leitner01ce63c2015-12-04 15:14:04 -02007748
7749 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
7750 net_enable_timestamp();
Marcelo Ricardo Leitner3538a5c2015-12-23 16:44:09 -02007751
7752 security_sk_clone(sk, newsk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007753}
7754
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03007755static inline void sctp_copy_descendant(struct sock *sk_to,
7756 const struct sock *sk_from)
7757{
7758 int ancestor_size = sizeof(struct inet_sock) +
7759 sizeof(struct sctp_sock) -
7760 offsetof(struct sctp_sock, auto_asconf_list);
7761
7762 if (sk_from->sk_family == PF_INET6)
7763 ancestor_size += sizeof(struct ipv6_pinfo);
7764
7765 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
7766}
7767
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768/* Populate the fields of the newsk from the oldsk and migrate the assoc
7769 * and its messages to the newsk.
7770 */
7771static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
7772 struct sctp_association *assoc,
7773 sctp_socket_type_t type)
7774{
7775 struct sctp_sock *oldsp = sctp_sk(oldsk);
7776 struct sctp_sock *newsp = sctp_sk(newsk);
7777 struct sctp_bind_bucket *pp; /* hash list port iterator */
7778 struct sctp_endpoint *newep = newsp->ep;
7779 struct sk_buff *skb, *tmp;
7780 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08007781 struct sctp_bind_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782
7783 /* Migrate socket buffer sizes and all the socket level options to the
7784 * new socket.
7785 */
7786 newsk->sk_sndbuf = oldsk->sk_sndbuf;
7787 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
7788 /* Brute force copy old sctp opt. */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03007789 sctp_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790
7791 /* Restore the ep value that was overwritten with the above structure
7792 * copy.
7793 */
7794 newsp->ep = newep;
7795 newsp->hmac = NULL;
7796
7797 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007798 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
7799 inet_sk(oldsk)->inet_num)];
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01007800 spin_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801 pp = sctp_sk(oldsk)->bind_hash;
7802 sk_add_bind_node(newsk, &pp->owner);
7803 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00007804 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01007805 spin_unlock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007806
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07007807 /* Copy the bind_addr list from the original endpoint to the new
7808 * endpoint so that we can handle restarts properly
7809 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08007810 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
7811 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07007812
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813 /* Move any messages in the old socket's receive queue that are for the
7814 * peeled off association to the new socket's receive queue.
7815 */
7816 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
7817 event = sctp_skb2event(skb);
7818 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07007819 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007821 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822 }
7823 }
7824
7825 /* Clean up any messages pending delivery due to partial
7826 * delivery. Three cases:
7827 * 1) No partial deliver; no work.
7828 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
7829 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
7830 */
7831 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007832 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007834 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835 struct sk_buff_head *queue;
7836
7837 /* Decide which queue to move pd_lobby skbs to. */
7838 if (assoc->ulpq.pd_mode) {
7839 queue = &newsp->pd_lobby;
7840 } else
7841 queue = &newsk->sk_receive_queue;
7842
7843 /* Walk through the pd_lobby, looking for skbs that
7844 * need moved to the new socket.
7845 */
7846 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
7847 event = sctp_skb2event(skb);
7848 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07007849 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007851 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852 }
7853 }
7854
7855 /* Clear up any skbs waiting for the partial
7856 * delivery to finish.
7857 */
7858 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007859 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860
7861 }
7862
Wei Yongjun1bc4ee42009-07-05 19:45:48 +00007863 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007864 sctp_skb_set_owner_r_frag(skb, newsk);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007865
Wei Yongjun1bc4ee42009-07-05 19:45:48 +00007866 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007867 sctp_skb_set_owner_r_frag(skb, newsk);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007868
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869 /* Set the type of socket to indicate that it is peeled off from the
7870 * original UDP-style socket or created with the accept() call on a
7871 * TCP-style socket..
7872 */
7873 newsp->type = type;
7874
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07007875 /* Mark the new socket "in-use" by the user so that any packets
7876 * that may arrive on the association after we've moved it are
7877 * queued to the backlog. This prevents a potential race between
7878 * backlog processing on the old socket and new-packet processing
7879 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07007880 *
7881 * The caller has just allocated newsk so we can guarantee that other
7882 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07007883 */
Zach Brown5131a182007-06-22 15:14:46 -07007884 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Xin Longb89fc6a2017-10-28 02:13:29 +08007885 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886 sctp_assoc_migrate(assoc, newsk);
Xin Longb89fc6a2017-10-28 02:13:29 +08007887 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888
7889 /* If the association on the newsk is already closed before accept()
7890 * is called, set RCV_SHUTDOWN flag.
7891 */
Xin Longd46e4162016-06-09 22:48:18 +08007892 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
Xin Long141ddef2016-06-16 01:15:06 +08007893 newsk->sk_state = SCTP_SS_CLOSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894 newsk->sk_shutdown |= RCV_SHUTDOWN;
Xin Longd46e4162016-06-09 22:48:18 +08007895 } else {
7896 newsk->sk_state = SCTP_SS_ESTABLISHED;
7897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898
wangweidong048ed4b2014-01-21 15:44:11 +08007899 release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007900}
7901
Neil Horman4d93df02007-08-15 16:07:44 -07007902
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903/* This proto struct describes the ULP interface for SCTP. */
7904struct proto sctp_prot = {
7905 .name = "SCTP",
7906 .owner = THIS_MODULE,
7907 .close = sctp_close,
7908 .connect = sctp_connect,
7909 .disconnect = sctp_disconnect,
7910 .accept = sctp_accept,
7911 .ioctl = sctp_ioctl,
7912 .init = sctp_init_sock,
7913 .destroy = sctp_destroy_sock,
7914 .shutdown = sctp_shutdown,
7915 .setsockopt = sctp_setsockopt,
7916 .getsockopt = sctp_getsockopt,
7917 .sendmsg = sctp_sendmsg,
7918 .recvmsg = sctp_recvmsg,
7919 .bind = sctp_bind,
7920 .backlog_rcv = sctp_backlog_rcv,
7921 .hash = sctp_hash,
7922 .unhash = sctp_unhash,
7923 .get_port = sctp_get_port,
7924 .obj_size = sizeof(struct sctp_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07007925 .sysctl_mem = sysctl_sctp_mem,
7926 .sysctl_rmem = sysctl_sctp_rmem,
7927 .sysctl_wmem = sysctl_sctp_wmem,
7928 .memory_pressure = &sctp_memory_pressure,
7929 .enter_memory_pressure = sctp_enter_memory_pressure,
7930 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08007931 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932};
7933
Eric Dumazetdfd56b82011-12-10 09:48:31 +00007934#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08007935
Eric Dumazet602dd622015-12-01 07:20:07 -08007936#include <net/transp_v6.h>
7937static void sctp_v6_destroy_sock(struct sock *sk)
7938{
7939 sctp_destroy_sock(sk);
7940 inet6_destroy_sock(sk);
7941}
7942
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943struct proto sctpv6_prot = {
7944 .name = "SCTPv6",
7945 .owner = THIS_MODULE,
7946 .close = sctp_close,
7947 .connect = sctp_connect,
7948 .disconnect = sctp_disconnect,
7949 .accept = sctp_accept,
7950 .ioctl = sctp_ioctl,
7951 .init = sctp_init_sock,
Eric Dumazet602dd622015-12-01 07:20:07 -08007952 .destroy = sctp_v6_destroy_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953 .shutdown = sctp_shutdown,
7954 .setsockopt = sctp_setsockopt,
7955 .getsockopt = sctp_getsockopt,
7956 .sendmsg = sctp_sendmsg,
7957 .recvmsg = sctp_recvmsg,
7958 .bind = sctp_bind,
7959 .backlog_rcv = sctp_backlog_rcv,
7960 .hash = sctp_hash,
7961 .unhash = sctp_unhash,
7962 .get_port = sctp_get_port,
7963 .obj_size = sizeof(struct sctp6_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07007964 .sysctl_mem = sysctl_sctp_mem,
7965 .sysctl_rmem = sysctl_sctp_rmem,
7966 .sysctl_wmem = sysctl_sctp_wmem,
7967 .memory_pressure = &sctp_memory_pressure,
7968 .enter_memory_pressure = sctp_enter_memory_pressure,
7969 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08007970 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007971};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00007972#endif /* IS_ENABLED(CONFIG_IPV6) */