blob: 3bac03931b67e22e250c9df66bc9e042b2c6e5bc [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>
Ingo Molnar3f07c012017-02-08 18:51:30 +010060#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/ip.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080062#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/fcntl.h>
64#include <linux/poll.h>
65#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090066#include <linux/slab.h>
Al Viro56b31d12012-08-18 00:25:51 -040067#include <linux/file.h>
Daniel Borkmannffd59392014-02-17 12:11:11 +010068#include <linux/compat.h>
NeilBrown0eb71a92018-06-18 12:52:50 +100069#include <linux/rhashtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71#include <net/ip.h>
72#include <net/icmp.h>
73#include <net/route.h>
74#include <net/ipv6.h>
75#include <net/inet_common.h>
Neil Horman8465a5f2014-04-17 15:26:51 -040076#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#include <linux/socket.h> /* for sa_family_t */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040079#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <net/sock.h>
81#include <net/sctp/sctp.h>
82#include <net/sctp/sm.h>
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -030083#include <net/sctp/stream_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* Forward declarations for internal helper functions. */
Xin Longcd305c72018-10-17 03:07:51 +080086static bool sctp_writeable(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static void sctp_wfree(struct sk_buff *skb);
Xin Longcea0cc82017-11-15 16:57:26 +080088static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +080089 size_t msg_len);
wangweidong26ac8e52013-12-23 12:16:51 +080090static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
92static int sctp_wait_for_accept(struct sock *sk, long timeo);
93static void sctp_wait_for_close(struct sock *sk, long timeo);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +020094static void sctp_destruct_sock(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
96 union sctp_addr *addr, int len);
97static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
98static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
99static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
100static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
101static int sctp_send_asconf(struct sctp_association *asoc,
102 struct sctp_chunk *chunk);
103static int sctp_do_bind(struct sock *, union sctp_addr *, int);
104static int sctp_autobind(struct sock *sk);
Xin Long89664c622019-03-03 17:54:53 +0800105static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
106 struct sctp_association *assoc,
107 enum sctp_socket_type type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Eric Dumazet06044752017-06-07 13:29:12 -0700109static unsigned long sctp_memory_pressure;
Eric Dumazet8d987e52010-11-09 23:24:26 +0000110static atomic_long_t sctp_memory_allocated;
Eric Dumazet17483762008-11-25 21:16:35 -0800111struct percpu_counter sctp_sockets_allocated;
Neil Horman4d93df02007-08-15 16:07:44 -0700112
Pavel Emelyanov5c52ba12008-07-16 20:28:10 -0700113static void sctp_enter_memory_pressure(struct sock *sk)
Neil Horman4d93df02007-08-15 16:07:44 -0700114{
115 sctp_memory_pressure = 1;
116}
117
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* Get the sndbuf space available at the time on the association. */
120static inline int sctp_wspace(struct sctp_association *asoc)
121{
Xin Longcd305c72018-10-17 03:07:51 +0800122 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Xin Longcd305c72018-10-17 03:07:51 +0800124 return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used
125 : sk_stream_wspace(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128/* Increment the used sndbuf space count of the corresponding association by
129 * the size of the outgoing data chunk.
130 * Also, set the skb destructor for sndbuf accounting later.
131 *
132 * Since it is always 1-1 between chunk and skb, and also a new skb is always
133 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
134 * destructor in the data chunk skb for the purpose of the sndbuf space
135 * tracking.
136 */
137static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
138{
139 struct sctp_association *asoc = chunk->asoc;
140 struct sock *sk = asoc->base.sk;
141
142 /* The sndbuf space is tracked per association. */
143 sctp_association_hold(asoc);
144
Xin Long1b1e0bc2018-03-14 19:05:30 +0800145 if (chunk->shkey)
146 sctp_auth_shkey_hold(chunk->shkey);
147
Neil Horman4eb701d2005-04-28 12:02:04 -0700148 skb_set_owner_w(chunk->skb, sk);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 chunk->skb->destructor = sctp_wfree;
151 /* Save the chunk pointer in skb for sctp_wfree to use later. */
Daniel Borkmannf869c912014-11-20 01:54:48 +0100152 skb_shinfo(chunk->skb)->destructor_arg = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Reshetova, Elena14afee42017-06-30 13:08:00 +0300154 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Xin Long605c0ac2018-10-17 03:07:50 +0800155 asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk);
156 sk->sk_wmem_queued += chunk->skb->truesize + sizeof(struct sctp_chunk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800157 sk_mem_charge(sk, chunk->skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Xin Longd04adf12017-10-28 02:13:29 +0800160static void sctp_clear_owner_w(struct sctp_chunk *chunk)
161{
162 skb_orphan(chunk->skb);
163}
164
165static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
166 void (*cb)(struct sctp_chunk *))
167
168{
169 struct sctp_outq *q = &asoc->outqueue;
170 struct sctp_transport *t;
171 struct sctp_chunk *chunk;
172
173 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
174 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
175 cb(chunk);
176
Xin Longa8dd3972017-11-26 20:56:07 +0800177 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800178 cb(chunk);
179
Xin Longa8dd3972017-11-26 20:56:07 +0800180 list_for_each_entry(chunk, &q->sacked, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800181 cb(chunk);
182
Xin Longa8dd3972017-11-26 20:56:07 +0800183 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800184 cb(chunk);
185
186 list_for_each_entry(chunk, &q->out_chunk_list, list)
187 cb(chunk);
188}
189
Xin Long13228232017-12-08 21:04:09 +0800190static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
191 void (*cb)(struct sk_buff *, struct sock *))
192
193{
194 struct sk_buff *skb, *tmp;
195
196 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
197 cb(skb, sk);
198
199 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
200 cb(skb, sk);
201
202 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
203 cb(skb, sk);
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/* Verify that this is a valid address. */
207static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
208 int len)
209{
210 struct sctp_af *af;
211
212 /* Verify basic sockaddr. */
213 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
214 if (!af)
215 return -EINVAL;
216
217 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700218 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return -EINVAL;
220
221 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
222 return -EINVAL;
223
224 return 0;
225}
226
227/* Look up the association by its id. If this is not a UDP-style
228 * socket, the ID field is always ignored.
229 */
230struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
231{
232 struct sctp_association *asoc = NULL;
233
234 /* If this is not a UDP-style socket, assoc id should be ignored. */
235 if (!sctp_style(sk, UDP)) {
236 /* Return NULL if the socket state is not ESTABLISHED. It
237 * could be a TCP-style listening socket or a socket which
238 * hasn't yet called connect() to establish an association.
239 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -0300240 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return NULL;
242
243 /* Get the first and the only association from the list. */
244 if (!list_empty(&sctp_sk(sk)->ep->asocs))
245 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
246 struct sctp_association, asocs);
247 return asoc;
248 }
249
250 /* Otherwise this is a UDP-style socket. */
Xin Long80df2702019-01-28 15:08:23 +0800251 if (id <= SCTP_ALL_ASSOC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return NULL;
253
254 spin_lock_bh(&sctp_assocs_id_lock);
255 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
Marcelo Ricardo Leitnerb336dec2018-10-16 15:18:17 -0300256 if (asoc && (asoc->base.sk != sk || asoc->base.dead))
257 asoc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 spin_unlock_bh(&sctp_assocs_id_lock);
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return asoc;
261}
262
263/* Look up the transport from an address and an assoc id. If both address and
264 * id are specified, the associations matching the address and the id should be
265 * the same.
266 */
267static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
268 struct sockaddr_storage *addr,
269 sctp_assoc_t id)
270{
271 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
Xin Long6f29a132017-01-24 14:01:53 +0800272 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 union sctp_addr *laddr = (union sctp_addr *)addr;
Xin Long6f29a132017-01-24 14:01:53 +0800274 struct sctp_transport *transport;
275
Xin Long912964e2017-02-07 20:56:08 +0800276 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
Xin Long6f29a132017-01-24 14:01:53 +0800277 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800280 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (!addr_asoc)
284 return NULL;
285
286 id_asoc = sctp_id2assoc(sk, id);
287 if (id_asoc && (id_asoc != addr_asoc))
288 return NULL;
289
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600290 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 (union sctp_addr *)addr);
292
293 return transport;
294}
295
296/* API 3.1.2 bind() - UDP Style Syntax
297 * The syntax of bind() is,
298 *
299 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
300 *
301 * sd - the socket descriptor returned by socket().
302 * addr - the address structure (struct sockaddr_in or struct
303 * sockaddr_in6 [RFC 2553]),
304 * addr_len - the size of the address structure.
305 */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200306static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 int retval = 0;
309
wangweidong048ed4b2014-01-21 15:44:11 +0800310 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Daniel Borkmannbb333812013-06-28 19:49:40 +0200312 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
313 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 /* Disallow binding twice. */
316 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700317 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 addr_len);
319 else
320 retval = -EINVAL;
321
wangweidong048ed4b2014-01-21 15:44:11 +0800322 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 return retval;
325}
326
327static long sctp_get_port_local(struct sock *, union sctp_addr *);
328
329/* Verify this is a valid sockaddr. */
330static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
331 union sctp_addr *addr, int len)
332{
333 struct sctp_af *af;
334
335 /* Check minimum size. */
336 if (len < sizeof (struct sockaddr))
337 return NULL;
338
Xin Longc5006b82018-01-15 17:02:00 +0800339 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
340 return NULL;
341
Eric Dumazet81e98372018-04-08 07:52:08 -0700342 if (addr->sa.sa_family == AF_INET6) {
343 if (len < SIN6_LEN_RFC2133)
344 return NULL;
345 /* V4 mapped address are really of AF_INET family */
346 if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
347 !opt->pf->af_supported(AF_INET, opt))
348 return NULL;
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* If we get this far, af is valid. */
352 af = sctp_get_af_specific(addr->sa.sa_family);
353
354 if (len < af->sockaddr_len)
355 return NULL;
356
357 return af;
358}
359
360/* Bind a local address either to an endpoint or to an association. */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200361static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Eric W. Biederman35946982012-11-16 03:03:12 +0000363 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct sctp_sock *sp = sctp_sk(sk);
365 struct sctp_endpoint *ep = sp->ep;
366 struct sctp_bind_addr *bp = &ep->base.bind_addr;
367 struct sctp_af *af;
368 unsigned short snum;
369 int ret = 0;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* Common sockaddr verification. */
372 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700373 if (!af) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200374 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
375 __func__, sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700377 }
378
379 snum = ntohs(addr->v4.sin_port);
380
Daniel Borkmannbb333812013-06-28 19:49:40 +0200381 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
382 __func__, sk, &addr->sa, bp->port, snum, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 /* PF specific bind() address verification. */
385 if (!sp->pf->bind_verify(sp, addr))
386 return -EADDRNOTAVAIL;
387
Vlad Yasevich8b358052007-05-15 17:14:58 -0400388 /* We must either be unbound, or bind to the same port.
389 * It's OK to allow 0 ports if we are already bound.
390 * We'll just inhert an already bound port in this case
391 */
392 if (bp->port) {
393 if (!snum)
394 snum = bp->port;
395 else if (snum != bp->port) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200396 pr_debug("%s: new port %d doesn't match existing port "
397 "%d\n", __func__, snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400398 return -EINVAL;
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
Krister Johansen4548b682017-01-20 17:49:11 -0800402 if (snum && snum < inet_prot_sock(net) &&
Eric W. Biederman35946982012-11-16 03:03:12 +0000403 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return -EACCES;
405
Vlad Yasevich4e540642008-07-18 23:06:32 -0700406 /* See if the address matches any of the addresses we may have
407 * already bound before checking against other endpoints.
408 */
409 if (sctp_bind_addr_match(bp, addr, sp))
410 return -EINVAL;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* Make sure we are allowed to bind here.
413 * The function sctp_get_port_local() does duplicate address
414 * detection.
415 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900416 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if ((ret = sctp_get_port_local(sk, addr))) {
Vlad Yasevich4e540642008-07-18 23:06:32 -0700418 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
421 /* Refresh ephemeral port. */
422 if (!bp->port)
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000423 bp->port = inet_sk(sk)->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Vlad Yasevich559cf712007-09-16 16:03:28 -0700425 /* Add the address to the bind address list.
426 * Use GFP_ATOMIC since BHs will be disabled.
427 */
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300428 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
429 SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* Copy back into socket for getsockname() use. */
432 if (!ret) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000433 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600434 sp->pf->to_sk_saddr(addr, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
437 return ret;
438}
439
440 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
441 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900442 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900444 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900446 * subsequent ASCONF. Note this restriction binds each side, so at any
447 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * from each endpoint).
449 */
450static int sctp_send_asconf(struct sctp_association *asoc,
451 struct sctp_chunk *chunk)
452{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000453 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int retval = 0;
455
456 /* If there is an outstanding ASCONF chunk, queue it for later
457 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700460 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900461 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
464 /* Hold the chunk until an ASCONF_ACK is received. */
465 sctp_chunk_hold(chunk);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000466 retval = sctp_primitive_ASCONF(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (retval)
468 sctp_chunk_free(chunk);
469 else
470 asoc->addip_last_asconf = chunk;
471
472out:
473 return retval;
474}
475
476/* Add a list of addresses as bind addresses to local endpoint or
477 * association.
478 *
479 * Basically run through each address specified in the addrs/addrcnt
480 * array/length pair, determine if it is IPv6 or IPv4 and call
481 * sctp_do_bind() on it.
482 *
483 * If any of them fails, then the operation will be reversed and the
484 * ones that were added will be removed.
485 *
486 * Only sctp_setsockopt_bindx() is supposed to call this function.
487 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200488static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 int cnt;
491 int retval = 0;
492 void *addr_buf;
493 struct sockaddr *sa_addr;
494 struct sctp_af *af;
495
Daniel Borkmannbb333812013-06-28 19:49:40 +0200496 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
497 addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 addr_buf = addrs;
500 for (cnt = 0; cnt < addrcnt; cnt++) {
501 /* The list may contain either IPv4 or IPv6 address;
502 * determine the address length for walking thru the list.
503 */
Joe Perchesea110732011-06-13 16:21:26 +0000504 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 af = sctp_get_af_specific(sa_addr->sa_family);
506 if (!af) {
507 retval = -EINVAL;
508 goto err_bindx_add;
509 }
510
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900511 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 af->sockaddr_len);
513
514 addr_buf += af->sockaddr_len;
515
516err_bindx_add:
517 if (retval < 0) {
518 /* Failed. Cleanup the ones that have been added */
519 if (cnt > 0)
520 sctp_bindx_rem(sk, addrs, cnt);
521 return retval;
522 }
523 }
524
525 return retval;
526}
527
528/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
529 * associations that are part of the endpoint indicating that a list of local
530 * addresses are added to the endpoint.
531 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900532 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 * association, we do not send the chunk for that association. But it will not
534 * affect other associations.
535 *
536 * Only sctp_setsockopt_bindx() is supposed to call this function.
537 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900538static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct sockaddr *addrs,
540 int addrcnt)
541{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000542 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 struct sctp_sock *sp;
544 struct sctp_endpoint *ep;
545 struct sctp_association *asoc;
546 struct sctp_bind_addr *bp;
547 struct sctp_chunk *chunk;
548 struct sctp_sockaddr_entry *laddr;
549 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700550 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 void *addr_buf;
552 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct list_head *p;
554 int i;
555 int retval = 0;
556
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000557 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return retval;
559
560 sp = sctp_sk(sk);
561 ep = sp->ep;
562
Daniel Borkmannbb333812013-06-28 19:49:40 +0200563 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
564 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700566 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (!asoc->peer.asconf_capable)
568 continue;
569
570 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
571 continue;
572
573 if (!sctp_state(asoc, ESTABLISHED))
574 continue;
575
576 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900577 * in the bind address list of the association. If so,
578 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * other associations.
580 */
581 addr_buf = addrs;
582 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000583 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 af = sctp_get_af_specific(addr->v4.sin_family);
585 if (!af) {
586 retval = -EINVAL;
587 goto out;
588 }
589
590 if (sctp_assoc_lookup_laddr(asoc, addr))
591 break;
592
593 addr_buf += af->sockaddr_len;
594 }
595 if (i < addrcnt)
596 continue;
597
Vlad Yasevich559cf712007-09-16 16:03:28 -0700598 /* Use the first valid address in bind addr list of
599 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 bp = &asoc->base.bind_addr;
602 p = bp->address_list.next;
603 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800604 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 addrcnt, SCTP_PARAM_ADD_IP);
606 if (!chunk) {
607 retval = -ENOMEM;
608 goto out;
609 }
610
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700611 /* Add the new addresses to the bind address list with
612 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700614 addr_buf = addrs;
615 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000616 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700617 af = sctp_get_af_specific(addr->v4.sin_family);
618 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800619 retval = sctp_add_bind_addr(bp, &saveaddr,
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300620 sizeof(saveaddr),
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800621 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700622 addr_buf += af->sockaddr_len;
623 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900624 if (asoc->src_out_of_asoc_ok) {
625 struct sctp_transport *trans;
626
627 list_for_each_entry(trans,
628 &asoc->peer.transport_addr_list, transports) {
Michio Honda8a07eb02011-04-26 20:19:36 +0900629 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
630 2*asoc->pathmtu, 4380));
631 trans->ssthresh = asoc->peer.i.a_rwnd;
632 trans->rto = asoc->rto_initial;
Michele Baldessari196d6752012-12-01 04:49:42 +0000633 sctp_max_rto(asoc, trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900634 trans->rtt = trans->srtt = trans->rttvar = 0;
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300635 /* Clear the source and route cache */
Michio Honda8a07eb02011-04-26 20:19:36 +0900636 sctp_transport_route(trans, NULL,
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300637 sctp_sk(asoc->base.sk));
Michio Honda8a07eb02011-04-26 20:19:36 +0900638 }
639 }
640 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
643out:
644 return retval;
645}
646
647/* Remove a list of addresses from bind addresses list. Do not remove the
648 * last address.
649 *
650 * Basically run through each address specified in the addrs/addrcnt
651 * array/length pair, determine if it is IPv6 or IPv4 and call
652 * sctp_del_bind() on it.
653 *
654 * If any of them fails, then the operation will be reversed and the
655 * ones that were removed will be added back.
656 *
657 * At least one address has to be left; if only one address is
658 * available, the operation will return -EBUSY.
659 *
660 * Only sctp_setsockopt_bindx() is supposed to call this function.
661 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200662static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 struct sctp_sock *sp = sctp_sk(sk);
665 struct sctp_endpoint *ep = sp->ep;
666 int cnt;
667 struct sctp_bind_addr *bp = &ep->base.bind_addr;
668 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800670 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct sctp_af *af;
672
Daniel Borkmannbb333812013-06-28 19:49:40 +0200673 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
674 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 addr_buf = addrs;
677 for (cnt = 0; cnt < addrcnt; cnt++) {
678 /* If the bind address list is empty or if there is only one
679 * bind address, there is nothing more to be removed (we need
680 * at least one address here).
681 */
682 if (list_empty(&bp->address_list) ||
683 (sctp_list_single_entry(&bp->address_list))) {
684 retval = -EBUSY;
685 goto err_bindx_rem;
686 }
687
Joe Perchesea110732011-06-13 16:21:26 +0000688 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800689 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (!af) {
691 retval = -EINVAL;
692 goto err_bindx_rem;
693 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700694
695 if (!af->addr_valid(sa_addr, sp, NULL)) {
696 retval = -EADDRNOTAVAIL;
697 goto err_bindx_rem;
698 }
699
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000700 if (sa_addr->v4.sin_port &&
701 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 retval = -EINVAL;
703 goto err_bindx_rem;
704 }
705
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000706 if (!sa_addr->v4.sin_port)
707 sa_addr->v4.sin_port = htons(bp->port);
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /* FIXME - There is probably a need to check if sk->sk_saddr and
710 * sk->sk_rcv_addr are currently set to one of the addresses to
711 * be removed. This is something which needs to be looked into
712 * when we are fixing the outstanding issues with multi-homing
713 * socket routing and failover schemes. Refer to comments in
714 * sctp_do_bind(). -daisy
715 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400716 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 addr_buf += af->sockaddr_len;
719err_bindx_rem:
720 if (retval < 0) {
721 /* Failed. Add the ones that has been removed back */
722 if (cnt > 0)
723 sctp_bindx_add(sk, addrs, cnt);
724 return retval;
725 }
726 }
727
728 return retval;
729}
730
731/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
732 * the associations that are part of the endpoint indicating that a list of
733 * local addresses are removed from the endpoint.
734 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900735 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 * association, we do not send the chunk for that association. But it will not
737 * affect other associations.
738 *
739 * Only sctp_setsockopt_bindx() is supposed to call this function.
740 */
741static int sctp_send_asconf_del_ip(struct sock *sk,
742 struct sockaddr *addrs,
743 int addrcnt)
744{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000745 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 struct sctp_sock *sp;
747 struct sctp_endpoint *ep;
748 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700749 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 struct sctp_bind_addr *bp;
751 struct sctp_chunk *chunk;
752 union sctp_addr *laddr;
753 void *addr_buf;
754 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700755 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 int i;
757 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900758 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Michio Honda8a07eb02011-04-26 20:19:36 +0900760 chunk = NULL;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000761 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return retval;
763
764 sp = sctp_sk(sk);
765 ep = sp->ep;
766
Daniel Borkmannbb333812013-06-28 19:49:40 +0200767 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
768 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700770 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 if (!asoc->peer.asconf_capable)
773 continue;
774
775 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
776 continue;
777
778 if (!sctp_state(asoc, ESTABLISHED))
779 continue;
780
781 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900782 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 * If so, do not send the asconf chunk to its peer, but
784 * continue with other associations.
785 */
786 addr_buf = addrs;
787 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000788 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 af = sctp_get_af_specific(laddr->v4.sin_family);
790 if (!af) {
791 retval = -EINVAL;
792 goto out;
793 }
794
795 if (!sctp_assoc_lookup_laddr(asoc, laddr))
796 break;
797
798 addr_buf += af->sockaddr_len;
799 }
800 if (i < addrcnt)
801 continue;
802
803 /* Find one address in the association's bind address list
804 * that is not in the packed array of addresses. This is to
805 * make sure that we do not delete all the addresses in the
806 * association.
807 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 bp = &asoc->base.bind_addr;
809 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
810 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900811 if ((laddr == NULL) && (addrcnt == 1)) {
812 if (asoc->asconf_addr_del_pending)
813 continue;
814 asoc->asconf_addr_del_pending =
815 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900816 if (asoc->asconf_addr_del_pending == NULL) {
817 retval = -ENOMEM;
818 goto out;
819 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900820 asoc->asconf_addr_del_pending->sa.sa_family =
821 addrs->sa_family;
822 asoc->asconf_addr_del_pending->v4.sin_port =
823 htons(bp->port);
824 if (addrs->sa_family == AF_INET) {
825 struct sockaddr_in *sin;
826
827 sin = (struct sockaddr_in *)addrs;
828 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
829 } else if (addrs->sa_family == AF_INET6) {
830 struct sockaddr_in6 *sin6;
831
832 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000833 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900834 }
Daniel Borkmannbb333812013-06-28 19:49:40 +0200835
836 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
837 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
838 asoc->asconf_addr_del_pending);
839
Michio Honda8a07eb02011-04-26 20:19:36 +0900840 asoc->src_out_of_asoc_ok = 1;
841 stored = 1;
842 goto skip_mkasconf;
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Daniel Borkmann88362ad2013-09-07 20:51:21 +0200845 if (laddr == NULL)
846 return -EINVAL;
847
Vlad Yasevich559cf712007-09-16 16:03:28 -0700848 /* We do not need RCU protection throughout this loop
849 * because this is done under a socket lock from the
850 * setsockopt call.
851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
853 SCTP_PARAM_DEL_IP);
854 if (!chunk) {
855 retval = -ENOMEM;
856 goto out;
857 }
858
Michio Honda8a07eb02011-04-26 20:19:36 +0900859skip_mkasconf:
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700860 /* Reset use_as_src flag for the addresses in the bind address
861 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700863 addr_buf = addrs;
864 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000865 laddr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700866 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700867 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a132006-11-20 17:05:23 -0800868 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800869 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700870 }
871 addr_buf += af->sockaddr_len;
872 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700873
874 /* Update the route and saddr entries for all the transports
875 * as some of the addresses in the bind address list are
876 * about to be deleted and cannot be used as source addresses.
877 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700878 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
879 transports) {
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700880 sctp_transport_route(transport, NULL,
881 sctp_sk(asoc->base.sk));
882 }
883
Michio Honda8a07eb02011-04-26 20:19:36 +0900884 if (stored)
885 /* We don't need to transmit ASCONF */
886 continue;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700887 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889out:
890 return retval;
891}
892
Michio Honda9f7d6532011-04-26 19:32:51 +0900893/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
894int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
895{
896 struct sock *sk = sctp_opt2sk(sp);
897 union sctp_addr *addr;
898 struct sctp_af *af;
899
900 /* It is safe to write port space in caller. */
901 addr = &addrw->a;
902 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
903 af = sctp_get_af_specific(addr->sa.sa_family);
904 if (!af)
905 return -EINVAL;
906 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
907 return -EINVAL;
908
909 if (addrw->state == SCTP_ADDR_NEW)
910 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
911 else
912 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
913}
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
916 *
917 * API 8.1
918 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
919 * int flags);
920 *
921 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
922 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
923 * or IPv6 addresses.
924 *
925 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
926 * Section 3.1.2 for this usage.
927 *
928 * addrs is a pointer to an array of one or more socket addresses. Each
929 * address is contained in its appropriate structure (i.e. struct
930 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700931 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 * representation is termed a "packed array" of addresses). The caller
933 * specifies the number of addresses in the array with addrcnt.
934 *
935 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
936 * -1, and sets errno to the appropriate error code.
937 *
938 * For SCTP, the port given in each socket address must be the same, or
939 * sctp_bindx() will fail, setting errno to EINVAL.
940 *
941 * The flags parameter is formed from the bitwise OR of zero or more of
942 * the following currently defined flags:
943 *
944 * SCTP_BINDX_ADD_ADDR
945 *
946 * SCTP_BINDX_REM_ADDR
947 *
948 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
949 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
950 * addresses from the association. The two flags are mutually exclusive;
951 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
952 * not remove all addresses from an association; sctp_bindx() will
953 * reject such an attempt with EINVAL.
954 *
955 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
956 * additional addresses with an endpoint after calling bind(). Or use
957 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
958 * socket is associated with so that no new association accepted will be
959 * associated with those addresses. If the endpoint supports dynamic
960 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
961 * endpoint to send the appropriate message to the peer to change the
962 * peers address lists.
963 *
964 * Adding and removing addresses from a connected association is
965 * optional functionality. Implementations that do not support this
966 * functionality should return EOPNOTSUPP.
967 *
968 * Basically do nothing but copying the addresses from user to kernel
969 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700970 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
971 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
974 * it.
975 *
976 * sk The sk of the socket
977 * addrs The pointer to the addresses in user land
978 * addrssize Size of the addrs buffer
979 * op Operation to perform (add or remove, see the flags of
980 * sctp_bindx)
981 *
982 * Returns 0 if ok, <0 errno code on error.
983 */
wangweidong26ac8e52013-12-23 12:16:51 +0800984static int sctp_setsockopt_bindx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +0200985 struct sockaddr __user *addrs,
986 int addrs_size, int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 struct sockaddr *kaddrs;
989 int err;
990 int addrcnt = 0;
991 int walk_size = 0;
992 struct sockaddr *sa_addr;
993 void *addr_buf;
994 struct sctp_af *af;
995
Daniel Borkmannbb333812013-06-28 19:49:40 +0200996 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
997 __func__, sk, addrs, addrs_size, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 if (unlikely(addrs_size <= 0))
1000 return -EINVAL;
1001
Al Viroc981f252018-01-07 13:19:09 -05001002 kaddrs = vmemdup_user(addrs, addrs_size);
1003 if (unlikely(IS_ERR(kaddrs)))
1004 return PTR_ERR(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001006 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 addr_buf = kaddrs;
1008 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001009 if (walk_size + sizeof(sa_family_t) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001010 kvfree(kaddrs);
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001011 return -EINVAL;
1012 }
1013
Joe Perchesea110732011-06-13 16:21:26 +00001014 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 af = sctp_get_af_specific(sa_addr->sa_family);
1016
1017 /* If the address family is not supported or if this address
1018 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001021 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return -EINVAL;
1023 }
1024 addrcnt++;
1025 addr_buf += af->sockaddr_len;
1026 walk_size += af->sockaddr_len;
1027 }
1028
1029 /* Do the work. */
1030 switch (op) {
1031 case SCTP_BINDX_ADD_ADDR:
Richard Haines2277c7c2018-02-13 20:56:24 +00001032 /* Allow security module to validate bindx addresses. */
1033 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1034 (struct sockaddr *)kaddrs,
1035 addrs_size);
1036 if (err)
1037 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1039 if (err)
1040 goto out;
1041 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1042 break;
1043
1044 case SCTP_BINDX_REM_ADDR:
1045 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1046 if (err)
1047 goto out;
1048 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1049 break;
1050
1051 default:
1052 err = -EINVAL;
1053 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056out:
Al Viroc981f252018-01-07 13:19:09 -05001057 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 return err;
1060}
1061
Frank Filz3f7a87d2005-06-20 13:14:57 -07001062/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1063 *
1064 * Common routine for handling connect() and sctp_connectx().
1065 * Connect will come in with just a single address.
1066 */
wangweidong26ac8e52013-12-23 12:16:51 +08001067static int __sctp_connect(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001068 struct sockaddr *kaddrs,
Xin Long644fbde2018-05-20 16:39:10 +08001069 int addrs_size, int flags,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001070 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001071{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001072 struct net *net = sock_net(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001073 struct sctp_sock *sp;
1074 struct sctp_endpoint *ep;
1075 struct sctp_association *asoc = NULL;
1076 struct sctp_association *asoc2;
1077 struct sctp_transport *transport;
1078 union sctp_addr to;
Xin Long1c662012017-08-05 19:59:54 +08001079 enum sctp_scope scope;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001080 long timeo;
1081 int err = 0;
1082 int addrcnt = 0;
1083 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001084 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001085 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001086 unsigned short port;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001087
1088 sp = sctp_sk(sk);
1089 ep = sp->ep;
1090
1091 /* connect() cannot be done on a socket that is already in ESTABLISHED
1092 * state - UDP-style peeled off socket or a TCP-style socket that
1093 * is already connected.
1094 * It cannot be done even on a TCP-style listening socket.
1095 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03001096 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
Frank Filz3f7a87d2005-06-20 13:14:57 -07001097 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1098 err = -EISCONN;
1099 goto out_free;
1100 }
1101
1102 /* Walk through the addrs buffer and count the number of addresses. */
1103 addr_buf = kaddrs;
1104 while (walk_size < addrs_size) {
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001105 struct sctp_af *af;
1106
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001107 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1108 err = -EINVAL;
1109 goto out_free;
1110 }
1111
Joe Perchesea110732011-06-13 16:21:26 +00001112 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001113 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001114
1115 /* If the address family is not supported or if this address
1116 * causes the address buffer to overflow return EINVAL.
1117 */
1118 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1119 err = -EINVAL;
1120 goto out_free;
1121 }
1122
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001123 port = ntohs(sa_addr->v4.sin_port);
1124
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001125 /* Save current address so we can work with it */
1126 memcpy(&to, sa_addr, af->sockaddr_len);
1127
1128 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001129 if (err)
1130 goto out_free;
1131
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001132 /* Make sure the destination port is correctly set
1133 * in all addresses.
1134 */
Wei Yongjun524fba62013-04-03 03:02:28 +00001135 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1136 err = -EINVAL;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001137 goto out_free;
Wei Yongjun524fba62013-04-03 03:02:28 +00001138 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001139
1140 /* Check if there already is a matching association on the
1141 * endpoint (other than the one created here).
1142 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001143 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001144 if (asoc2 && asoc2 != asoc) {
1145 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1146 err = -EISCONN;
1147 else
1148 err = -EALREADY;
1149 goto out_free;
1150 }
1151
1152 /* If we could not find a matching association on the endpoint,
1153 * make sure that there is no peeled-off association matching
1154 * the peer address even on another socket.
1155 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001156 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001157 err = -EADDRNOTAVAIL;
1158 goto out_free;
1159 }
1160
1161 if (!asoc) {
1162 /* If a bind() or sctp_bindx() is not called prior to
1163 * an sctp_connectx() call, the system picks an
1164 * ephemeral port and will choose an address set
1165 * equivalent to binding with a wildcard address.
1166 */
1167 if (!ep->base.bind_addr.port) {
1168 if (sctp_autobind(sk)) {
1169 err = -EAGAIN;
1170 goto out_free;
1171 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001172 } else {
1173 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001174 * If an unprivileged user inherits a 1-many
1175 * style socket with open associations on a
1176 * privileged port, it MAY be permitted to
1177 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001178 * be permitted to open new associations.
1179 */
Krister Johansen4548b682017-01-20 17:49:11 -08001180 if (ep->base.bind_addr.port <
1181 inet_prot_sock(net) &&
1182 !ns_capable(net->user_ns,
1183 CAP_NET_BIND_SERVICE)) {
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001184 err = -EACCES;
1185 goto out_free;
1186 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001187 }
1188
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001189 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001190 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1191 if (!asoc) {
1192 err = -ENOMEM;
1193 goto out_free;
1194 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001195
1196 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1197 GFP_KERNEL);
1198 if (err < 0) {
1199 goto out_free;
1200 }
1201
Frank Filz3f7a87d2005-06-20 13:14:57 -07001202 }
1203
1204 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001205 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001206 SCTP_UNKNOWN);
1207 if (!transport) {
1208 err = -ENOMEM;
1209 goto out_free;
1210 }
1211
1212 addrcnt++;
1213 addr_buf += af->sockaddr_len;
1214 walk_size += af->sockaddr_len;
1215 }
1216
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001217 /* In case the user of sctp_connectx() wants an association
1218 * id back, assign one now.
1219 */
1220 if (assoc_id) {
1221 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1222 if (err < 0)
1223 goto out_free;
1224 }
1225
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001226 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001227 if (err < 0) {
1228 goto out_free;
1229 }
1230
1231 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001232 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001233 sp->pf->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001234 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001235
Xin Long644fbde2018-05-20 16:39:10 +08001236 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001237
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001238 if (assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001239 *assoc_id = asoc->assoc_id;
Richard Haines2277c7c2018-02-13 20:56:24 +00001240
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001241 err = sctp_wait_for_connect(asoc, &timeo);
1242 /* Note: the asoc may be freed after the return of
1243 * sctp_wait_for_connect.
1244 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001245
1246 /* Don't free association on exit. */
1247 asoc = NULL;
1248
1249out_free:
Daniel Borkmannbb333812013-06-28 19:49:40 +02001250 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1251 __func__, asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001252
Neil Horman2eebc1e2012-07-16 09:13:51 +00001253 if (asoc) {
1254 /* sctp_primitive_ASSOCIATE may have added this association
1255 * To the hash table, try to unhash it, just in case, its a noop
1256 * if it wasn't hashed so we're safe
1257 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001258 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001259 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001260 return err;
1261}
1262
1263/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1264 *
1265 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001266 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1267 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001268 *
1269 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1270 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1271 * or IPv6 addresses.
1272 *
1273 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1274 * Section 3.1.2 for this usage.
1275 *
1276 * addrs is a pointer to an array of one or more socket addresses. Each
1277 * address is contained in its appropriate structure (i.e. struct
1278 * sockaddr_in or struct sockaddr_in6) the family of the address type
1279 * must be used to distengish the address length (note that this
1280 * representation is termed a "packed array" of addresses). The caller
1281 * specifies the number of addresses in the array with addrcnt.
1282 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001283 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1284 * the association id of the new association. On failure, sctp_connectx()
1285 * returns -1, and sets errno to the appropriate error code. The assoc_id
1286 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001287 *
1288 * For SCTP, the port given in each socket address must be the same, or
1289 * sctp_connectx() will fail, setting errno to EINVAL.
1290 *
1291 * An application can use sctp_connectx to initiate an association with
1292 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1293 * allows a caller to specify multiple addresses at which a peer can be
1294 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001295 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001296 * specifies that the stack will try to make use of all the addresses in
1297 * the list when needed.
1298 *
1299 * Note that the list of addresses passed in is only used for setting up
1300 * the association. It does not necessarily equal the set of addresses
1301 * the peer uses for the resulting association. If the caller wants to
1302 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1303 * retrieve them after the association has been set up.
1304 *
1305 * Basically do nothing but copying the addresses from user to kernel
1306 * land and invoking either sctp_connectx(). This is used for tunneling
1307 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1308 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07001309 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1310 * it.
1311 *
1312 * sk The sk of the socket
1313 * addrs The pointer to the addresses in user land
1314 * addrssize Size of the addrs buffer
1315 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001316 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001317 */
wangweidong26ac8e52013-12-23 12:16:51 +08001318static int __sctp_setsockopt_connectx(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001319 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001320 int addrs_size,
1321 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001322{
Frank Filz3f7a87d2005-06-20 13:14:57 -07001323 struct sockaddr *kaddrs;
Xin Long644fbde2018-05-20 16:39:10 +08001324 int err = 0, flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001325
Daniel Borkmannbb333812013-06-28 19:49:40 +02001326 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1327 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001328
1329 if (unlikely(addrs_size <= 0))
1330 return -EINVAL;
1331
Al Viroc981f252018-01-07 13:19:09 -05001332 kaddrs = vmemdup_user(addrs, addrs_size);
1333 if (unlikely(IS_ERR(kaddrs)))
1334 return PTR_ERR(kaddrs);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001335
Richard Haines2277c7c2018-02-13 20:56:24 +00001336 /* Allow security module to validate connectx addresses. */
1337 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1338 (struct sockaddr *)kaddrs,
1339 addrs_size);
1340 if (err)
1341 goto out_free;
1342
Xin Long644fbde2018-05-20 16:39:10 +08001343 /* in-kernel sockets don't generally have a file allocated to them
1344 * if all they do is call sock_create_kern().
1345 */
1346 if (sk->sk_socket->file)
1347 flags = sk->sk_socket->file->f_flags;
1348
1349 err = __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
Richard Haines2277c7c2018-02-13 20:56:24 +00001350
1351out_free:
Al Viroc981f252018-01-07 13:19:09 -05001352 kvfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001353
Frank Filz3f7a87d2005-06-20 13:14:57 -07001354 return err;
1355}
1356
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001357/*
1358 * This is an older interface. It's kept for backward compatibility
1359 * to the option that doesn't provide association id.
1360 */
wangweidong26ac8e52013-12-23 12:16:51 +08001361static int sctp_setsockopt_connectx_old(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001362 struct sockaddr __user *addrs,
1363 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001364{
1365 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1366}
1367
1368/*
1369 * New interface for the API. The since the API is done with a socket
1370 * option, to make it simple we feed back the association id is as a return
1371 * indication to the call. Error is always negative and association id is
1372 * always positive.
1373 */
wangweidong26ac8e52013-12-23 12:16:51 +08001374static int sctp_setsockopt_connectx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001375 struct sockaddr __user *addrs,
1376 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001377{
1378 sctp_assoc_t assoc_id = 0;
1379 int err = 0;
1380
1381 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1382
1383 if (err)
1384 return err;
1385 else
1386 return assoc_id;
1387}
1388
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001389/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001390 * New (hopefully final) interface for the API.
1391 * We use the sctp_getaddrs_old structure so that use-space library
Daniel Borkmannffd59392014-02-17 12:11:11 +01001392 * can avoid any unnecessary allocations. The only different part
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001393 * is that we store the actual length of the address buffer into the
Daniel Borkmannffd59392014-02-17 12:11:11 +01001394 * addrs_num structure member. That way we can re-use the existing
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001395 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001396 */
Daniel Borkmannffd59392014-02-17 12:11:11 +01001397#ifdef CONFIG_COMPAT
1398struct compat_sctp_getaddrs_old {
1399 sctp_assoc_t assoc_id;
1400 s32 addr_num;
1401 compat_uptr_t addrs; /* struct sockaddr * */
1402};
1403#endif
1404
wangweidong26ac8e52013-12-23 12:16:51 +08001405static int sctp_getsockopt_connectx3(struct sock *sk, int len,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001406 char __user *optval,
1407 int __user *optlen)
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001408{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001409 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001410 sctp_assoc_t assoc_id = 0;
1411 int err = 0;
1412
Daniel Borkmannffd59392014-02-17 12:11:11 +01001413#ifdef CONFIG_COMPAT
Andy Lutomirski96c0e0a2016-03-22 14:25:07 -07001414 if (in_compat_syscall()) {
Daniel Borkmannffd59392014-02-17 12:11:11 +01001415 struct compat_sctp_getaddrs_old param32;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001416
Daniel Borkmannffd59392014-02-17 12:11:11 +01001417 if (len < sizeof(param32))
1418 return -EINVAL;
1419 if (copy_from_user(&param32, optval, sizeof(param32)))
1420 return -EFAULT;
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001421
Daniel Borkmannffd59392014-02-17 12:11:11 +01001422 param.assoc_id = param32.assoc_id;
1423 param.addr_num = param32.addr_num;
1424 param.addrs = compat_ptr(param32.addrs);
1425 } else
1426#endif
1427 {
1428 if (len < sizeof(param))
1429 return -EINVAL;
1430 if (copy_from_user(&param, optval, sizeof(param)))
1431 return -EFAULT;
1432 }
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001433
Daniel Borkmannffd59392014-02-17 12:11:11 +01001434 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1435 param.addrs, param.addr_num,
1436 &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001437 if (err == 0 || err == -EINPROGRESS) {
1438 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1439 return -EFAULT;
1440 if (put_user(sizeof(assoc_id), optlen))
1441 return -EFAULT;
1442 }
1443
1444 return err;
1445}
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447/* API 3.1.4 close() - UDP Style Syntax
1448 * Applications use close() to perform graceful shutdown (as described in
1449 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1450 * by a UDP-style socket.
1451 *
1452 * The syntax is
1453 *
1454 * ret = close(int sd);
1455 *
1456 * sd - the socket descriptor of the associations to be closed.
1457 *
1458 * To gracefully shutdown a specific association represented by the
1459 * UDP-style socket, an application should use the sendmsg() call,
1460 * passing no user data, but including the appropriate flag in the
1461 * ancillary data (see Section xxxx).
1462 *
1463 * If sd in the close() call is a branched-off socket representing only
1464 * one association, the shutdown is performed on that association only.
1465 *
1466 * 4.1.6 close() - TCP Style Syntax
1467 *
1468 * Applications use close() to gracefully close down an association.
1469 *
1470 * The syntax is:
1471 *
1472 * int close(int sd);
1473 *
1474 * sd - the socket descriptor of the association to be closed.
1475 *
1476 * After an application calls close() on a socket descriptor, no further
1477 * socket operations will succeed on that descriptor.
1478 *
1479 * API 7.1.4 SO_LINGER
1480 *
1481 * An application using the TCP-style socket can use this option to
1482 * perform the SCTP ABORT primitive. The linger option structure is:
1483 *
1484 * struct linger {
1485 * int l_onoff; // option on/off
1486 * int l_linger; // linger time
1487 * };
1488 *
1489 * To enable the option, set l_onoff to 1. If the l_linger value is set
1490 * to 0, calling close() is the same as the ABORT primitive. If the
1491 * value is set to a negative value, the setsockopt() call will return
1492 * an error. If the value is set to a positive value linger_time, the
1493 * close() can be blocked for at most linger_time ms. If the graceful
1494 * shutdown phase does not finish during this period, close() will
1495 * return but the graceful shutdown phase continues in the system.
1496 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02001497static void sctp_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001499 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 struct sctp_endpoint *ep;
1501 struct sctp_association *asoc;
1502 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001503 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Daniel Borkmannbb333812013-06-28 19:49:40 +02001505 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Xin Long6dfe4b92017-06-10 14:56:56 +08001507 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 sk->sk_shutdown = SHUTDOWN_MASK;
Yafang Shaocbabf462017-12-20 11:12:54 +08001509 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 ep = sctp_sk(sk)->ep;
1512
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001513 /* Clean up any skbs sitting on the receive queue. */
1514 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1515 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1516
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001517 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 list_for_each_safe(pos, temp, &ep->asocs) {
1519 asoc = list_entry(pos, struct sctp_association, asocs);
1520
1521 if (sctp_style(sk, TCP)) {
1522 /* A closed association can still be in the list if
1523 * it belongs to a TCP-style listening socket that is
1524 * not yet accepted. If so, free it. If not, send an
1525 * ABORT or SHUTDOWN based on the linger options.
1526 */
1527 if (sctp_state(asoc, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001529 continue;
1530 }
1531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001533 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1534 !skb_queue_empty(&asoc->ulpq.reasm) ||
Xin Long13228232017-12-08 21:04:09 +08001535 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001536 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001537 struct sctp_chunk *chunk;
1538
1539 chunk = sctp_make_abort_user(asoc, NULL, 0);
Xin Long068d8bd2015-12-29 17:49:25 +08001540 sctp_primitive_ABORT(net, asoc, chunk);
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001541 } else
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001542 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 /* On a TCP-style socket, block for at most linger_time if set. */
1546 if (sctp_style(sk, TCP) && timeout)
1547 sctp_wait_for_close(sk, timeout);
1548
1549 /* This will run the backlog queue. */
wangweidong048ed4b2014-01-21 15:44:11 +08001550 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 /* Supposedly, no process has access to the socket, but
1553 * the net layers still may.
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001554 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1555 * held and that should be grabbed before socket lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001557 spin_lock_bh(&net->sctp.addr_wq_lock);
Xin Long6dfe4b92017-06-10 14:56:56 +08001558 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 /* Hold the sock, since sk_common_release() will put sock_put()
1561 * and we have just a little more cleanup.
1562 */
1563 sock_hold(sk);
1564 sk_common_release(sk);
1565
wangweidong5bc1d1b2014-01-21 15:44:12 +08001566 bh_unlock_sock(sk);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001567 spin_unlock_bh(&net->sctp.addr_wq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 sock_put(sk);
1570
1571 SCTP_DBG_OBJCNT_DEC(sock);
1572}
1573
1574/* Handle EPIPE error. */
1575static int sctp_error(struct sock *sk, int flags, int err)
1576{
1577 if (err == -EPIPE)
1578 err = sock_error(sk) ? : -EPIPE;
1579 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1580 send_sig(SIGPIPE, current, 0);
1581 return err;
1582}
1583
1584/* API 3.1.3 sendmsg() - UDP Style Syntax
1585 *
1586 * An application uses sendmsg() and recvmsg() calls to transmit data to
1587 * and receive data from its peer.
1588 *
1589 * ssize_t sendmsg(int socket, const struct msghdr *message,
1590 * int flags);
1591 *
1592 * socket - the socket descriptor of the endpoint.
1593 * message - pointer to the msghdr structure which contains a single
1594 * user message and possibly some ancillary data.
1595 *
1596 * See Section 5 for complete description of the data
1597 * structures.
1598 *
1599 * flags - flags sent or received with the user message, see Section
1600 * 5 for complete description of the flags.
1601 *
1602 * Note: This function could use a rewrite especially when explicit
1603 * connect support comes in.
1604 */
1605/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1606
Xin Longa05437a2017-08-11 10:23:48 +08001607static int sctp_msghdr_parse(const struct msghdr *msg,
1608 struct sctp_cmsgs *cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Xin Long204f8172018-03-01 23:05:14 +08001610static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1611 struct sctp_sndrcvinfo *srinfo,
1612 const struct msghdr *msg, size_t msg_len)
1613{
1614 __u16 sflags;
1615 int err;
1616
1617 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1618 return -EPIPE;
1619
1620 if (msg_len > sk->sk_sndbuf)
1621 return -EMSGSIZE;
1622
1623 memset(cmsgs, 0, sizeof(*cmsgs));
1624 err = sctp_msghdr_parse(msg, cmsgs);
1625 if (err) {
1626 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1627 return err;
1628 }
1629
1630 memset(srinfo, 0, sizeof(*srinfo));
1631 if (cmsgs->srinfo) {
1632 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1633 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1634 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1635 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1636 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1637 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1638 }
1639
1640 if (cmsgs->sinfo) {
1641 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1642 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1643 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1644 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1645 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1646 }
1647
Xin Longed63afb2018-03-05 20:44:18 +08001648 if (cmsgs->prinfo) {
1649 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1650 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1651 cmsgs->prinfo->pr_policy);
1652 }
1653
Xin Long204f8172018-03-01 23:05:14 +08001654 sflags = srinfo->sinfo_flags;
1655 if (!sflags && msg_len)
1656 return 0;
1657
1658 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1659 return -EINVAL;
1660
1661 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1662 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1663 return -EINVAL;
1664
1665 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1666 return -EINVAL;
1667
1668 return 0;
1669}
1670
Xin Long2bfd80f2018-03-01 23:05:11 +08001671static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1672 struct sctp_cmsgs *cmsgs,
1673 union sctp_addr *daddr,
1674 struct sctp_transport **tp)
1675{
1676 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1677 struct net *net = sock_net(sk);
1678 struct sctp_association *asoc;
1679 enum sctp_scope scope;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001680 struct cmsghdr *cmsg;
Xin Long4be41392018-07-02 18:21:14 +08001681 __be32 flowinfo = 0;
Linus Torvalds9eda2d22018-04-06 15:39:26 -07001682 struct sctp_af *af;
Wei Yongjund98985d2018-03-13 03:03:30 +00001683 int err;
Xin Long2bfd80f2018-03-01 23:05:11 +08001684
1685 *tp = NULL;
1686
1687 if (sflags & (SCTP_EOF | SCTP_ABORT))
1688 return -EINVAL;
1689
1690 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1691 sctp_sstate(sk, CLOSING)))
1692 return -EADDRNOTAVAIL;
1693
1694 if (sctp_endpoint_is_peeled_off(ep, daddr))
1695 return -EADDRNOTAVAIL;
1696
1697 if (!ep->base.bind_addr.port) {
1698 if (sctp_autobind(sk))
1699 return -EAGAIN;
1700 } else {
1701 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1702 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1703 return -EACCES;
1704 }
1705
1706 scope = sctp_scope(daddr);
1707
Linus Torvalds9eda2d22018-04-06 15:39:26 -07001708 /* Label connection socket for first association 1-to-many
1709 * style for client sequence socket()->sendmsg(). This
1710 * needs to be done before sctp_assoc_add_peer() as that will
1711 * set up the initial packet that needs to account for any
1712 * security ip options (CIPSO/CALIPSO) added to the packet.
1713 */
1714 af = sctp_get_af_specific(daddr->sa.sa_family);
1715 if (!af)
1716 return -EINVAL;
1717 err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1718 (struct sockaddr *)daddr,
1719 af->sockaddr_len);
1720 if (err < 0)
1721 return err;
1722
Xin Long2bfd80f2018-03-01 23:05:11 +08001723 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1724 if (!asoc)
1725 return -ENOMEM;
1726
1727 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1728 err = -ENOMEM;
1729 goto free;
1730 }
1731
1732 if (cmsgs->init) {
1733 struct sctp_initmsg *init = cmsgs->init;
1734
1735 if (init->sinit_num_ostreams) {
1736 __u16 outcnt = init->sinit_num_ostreams;
1737
1738 asoc->c.sinit_num_ostreams = outcnt;
1739 /* outcnt has been changed, need to re-init stream */
1740 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1741 GFP_KERNEL);
1742 if (err)
1743 goto free;
1744 }
1745
1746 if (init->sinit_max_instreams)
1747 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1748
1749 if (init->sinit_max_attempts)
1750 asoc->max_init_attempts = init->sinit_max_attempts;
1751
1752 if (init->sinit_max_init_timeo)
1753 asoc->max_init_timeo =
1754 msecs_to_jiffies(init->sinit_max_init_timeo);
1755 }
1756
1757 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1758 if (!*tp) {
1759 err = -ENOMEM;
1760 goto free;
1761 }
1762
Xin Long2c0dbaa2018-03-05 20:44:19 +08001763 if (!cmsgs->addrs_msg)
1764 return 0;
1765
Xin Long4be41392018-07-02 18:21:14 +08001766 if (daddr->sa.sa_family == AF_INET6)
1767 flowinfo = daddr->v6.sin6_flowinfo;
1768
Xin Long2c0dbaa2018-03-05 20:44:19 +08001769 /* sendv addr list parse */
1770 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1771 struct sctp_transport *transport;
1772 struct sctp_association *old;
1773 union sctp_addr _daddr;
1774 int dlen;
1775
1776 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1777 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1778 cmsg->cmsg_type != SCTP_DSTADDRV6))
1779 continue;
1780
1781 daddr = &_daddr;
1782 memset(daddr, 0, sizeof(*daddr));
1783 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1784 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
Wei Yongjund98985d2018-03-13 03:03:30 +00001785 if (dlen < sizeof(struct in_addr)) {
1786 err = -EINVAL;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001787 goto free;
Wei Yongjund98985d2018-03-13 03:03:30 +00001788 }
Xin Long2c0dbaa2018-03-05 20:44:19 +08001789
1790 dlen = sizeof(struct in_addr);
1791 daddr->v4.sin_family = AF_INET;
1792 daddr->v4.sin_port = htons(asoc->peer.port);
1793 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1794 } else {
Wei Yongjund98985d2018-03-13 03:03:30 +00001795 if (dlen < sizeof(struct in6_addr)) {
1796 err = -EINVAL;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001797 goto free;
Wei Yongjund98985d2018-03-13 03:03:30 +00001798 }
Xin Long2c0dbaa2018-03-05 20:44:19 +08001799
1800 dlen = sizeof(struct in6_addr);
Xin Long4be41392018-07-02 18:21:14 +08001801 daddr->v6.sin6_flowinfo = flowinfo;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001802 daddr->v6.sin6_family = AF_INET6;
1803 daddr->v6.sin6_port = htons(asoc->peer.port);
1804 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1805 }
1806 err = sctp_verify_addr(sk, daddr, sizeof(*daddr));
1807 if (err)
1808 goto free;
1809
1810 old = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1811 if (old && old != asoc) {
1812 if (old->state >= SCTP_STATE_ESTABLISHED)
1813 err = -EISCONN;
1814 else
1815 err = -EALREADY;
1816 goto free;
1817 }
1818
1819 if (sctp_endpoint_is_peeled_off(ep, daddr)) {
1820 err = -EADDRNOTAVAIL;
1821 goto free;
1822 }
1823
1824 transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL,
1825 SCTP_UNKNOWN);
1826 if (!transport) {
1827 err = -ENOMEM;
1828 goto free;
1829 }
1830 }
1831
Xin Long2bfd80f2018-03-01 23:05:11 +08001832 return 0;
1833
1834free:
1835 sctp_association_free(asoc);
1836 return err;
1837}
1838
Xin Longc2666de2018-03-01 23:05:12 +08001839static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1840 __u16 sflags, struct msghdr *msg,
1841 size_t msg_len)
1842{
1843 struct sock *sk = asoc->base.sk;
1844 struct net *net = sock_net(sk);
1845
1846 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1847 return -EPIPE;
1848
Xin Long49102802018-03-05 20:44:20 +08001849 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1850 !sctp_state(asoc, ESTABLISHED))
1851 return 0;
1852
Xin Longc2666de2018-03-01 23:05:12 +08001853 if (sflags & SCTP_EOF) {
1854 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1855 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1856
1857 return 0;
1858 }
1859
1860 if (sflags & SCTP_ABORT) {
1861 struct sctp_chunk *chunk;
1862
1863 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1864 if (!chunk)
1865 return -ENOMEM;
1866
1867 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1868 sctp_primitive_ABORT(net, asoc, chunk);
Xin Long901efe12019-03-03 16:50:26 +08001869 iov_iter_revert(&msg->msg_iter, msg_len);
Xin Longc2666de2018-03-01 23:05:12 +08001870
1871 return 0;
1872 }
1873
1874 return 1;
1875}
1876
Xin Longf84af332018-03-01 23:05:10 +08001877static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1878 struct msghdr *msg, size_t msg_len,
1879 struct sctp_transport *transport,
1880 struct sctp_sndrcvinfo *sinfo)
1881{
1882 struct sock *sk = asoc->base.sk;
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001883 struct sctp_sock *sp = sctp_sk(sk);
Xin Longf84af332018-03-01 23:05:10 +08001884 struct net *net = sock_net(sk);
1885 struct sctp_datamsg *datamsg;
1886 bool wait_connect = false;
1887 struct sctp_chunk *chunk;
1888 long timeo;
1889 int err;
1890
1891 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1892 err = -EINVAL;
1893 goto err;
1894 }
1895
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001896 if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
Xin Longf84af332018-03-01 23:05:10 +08001897 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1898 if (err)
1899 goto err;
1900 }
1901
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001902 if (sp->disable_fragments && msg_len > asoc->frag_point) {
Xin Longf84af332018-03-01 23:05:10 +08001903 err = -EMSGSIZE;
1904 goto err;
1905 }
1906
Marcelo Ricardo Leitner25216802018-04-26 16:58:56 -03001907 if (asoc->pmtu_pending) {
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001908 if (sp->param_flags & SPP_PMTUD_ENABLE)
1909 sctp_assoc_sync_pmtu(asoc);
Marcelo Ricardo Leitner25216802018-04-26 16:58:56 -03001910 asoc->pmtu_pending = 0;
1911 }
Neil Horman0aee4c22018-03-12 14:15:25 -04001912
Xin Longcd305c72018-10-17 03:07:51 +08001913 if (sctp_wspace(asoc) < (int)msg_len)
Neil Horman0aee4c22018-03-12 14:15:25 -04001914 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1915
Xin Longcd305c72018-10-17 03:07:51 +08001916 if (sctp_wspace(asoc) <= 0) {
Neil Horman0aee4c22018-03-12 14:15:25 -04001917 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1918 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1919 if (err)
1920 goto err;
1921 }
1922
Xin Longf84af332018-03-01 23:05:10 +08001923 if (sctp_state(asoc, CLOSED)) {
1924 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1925 if (err)
1926 goto err;
1927
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001928 if (sp->strm_interleave) {
Xin Longf84af332018-03-01 23:05:10 +08001929 timeo = sock_sndtimeo(sk, 0);
1930 err = sctp_wait_for_connect(asoc, &timeo);
Xin Longc8638502018-10-17 03:06:12 +08001931 if (err) {
1932 err = -ESRCH;
Xin Longf84af332018-03-01 23:05:10 +08001933 goto err;
Xin Longc8638502018-10-17 03:06:12 +08001934 }
Xin Longf84af332018-03-01 23:05:10 +08001935 } else {
1936 wait_connect = true;
1937 }
1938
1939 pr_debug("%s: we associated primitively\n", __func__);
1940 }
1941
Xin Longf84af332018-03-01 23:05:10 +08001942 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1943 if (IS_ERR(datamsg)) {
1944 err = PTR_ERR(datamsg);
1945 goto err;
1946 }
1947
1948 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1949
1950 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1951 sctp_chunk_hold(chunk);
1952 sctp_set_owner_w(chunk);
1953 chunk->transport = transport;
1954 }
1955
1956 err = sctp_primitive_SEND(net, asoc, datamsg);
1957 if (err) {
1958 sctp_datamsg_free(datamsg);
1959 goto err;
1960 }
1961
1962 pr_debug("%s: we sent primitively\n", __func__);
1963
1964 sctp_datamsg_put(datamsg);
1965
1966 if (unlikely(wait_connect)) {
1967 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1968 sctp_wait_for_connect(asoc, &timeo);
1969 }
1970
1971 err = msg_len;
1972
1973err:
1974 return err;
1975}
1976
Xin Longbecef9b2018-03-01 23:05:13 +08001977static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1978 const struct msghdr *msg,
1979 struct sctp_cmsgs *cmsgs)
1980{
1981 union sctp_addr *daddr = NULL;
1982 int err;
1983
1984 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1985 int len = msg->msg_namelen;
1986
1987 if (len > sizeof(*daddr))
1988 len = sizeof(*daddr);
1989
1990 daddr = (union sctp_addr *)msg->msg_name;
1991
1992 err = sctp_verify_addr(sk, daddr, len);
1993 if (err)
1994 return ERR_PTR(err);
1995 }
1996
1997 return daddr;
1998}
1999
Xin Longd42cb062018-03-01 23:05:15 +08002000static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
2001 struct sctp_sndrcvinfo *sinfo,
2002 struct sctp_cmsgs *cmsgs)
2003{
2004 if (!cmsgs->srinfo && !cmsgs->sinfo) {
2005 sinfo->sinfo_stream = asoc->default_stream;
2006 sinfo->sinfo_ppid = asoc->default_ppid;
2007 sinfo->sinfo_context = asoc->default_context;
2008 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
Xin Longed63afb2018-03-05 20:44:18 +08002009
2010 if (!cmsgs->prinfo)
2011 sinfo->sinfo_flags = asoc->default_flags;
Xin Longd42cb062018-03-01 23:05:15 +08002012 }
2013
Xin Longed63afb2018-03-05 20:44:18 +08002014 if (!cmsgs->srinfo && !cmsgs->prinfo)
Xin Longd42cb062018-03-01 23:05:15 +08002015 sinfo->sinfo_timetolive = asoc->default_timetolive;
Xin Long3ff547c2018-03-14 19:05:31 +08002016
2017 if (cmsgs->authinfo) {
2018 /* Reuse sinfo_tsn to indicate that authinfo was set and
2019 * sinfo_ssn to save the keyid on tx path.
2020 */
2021 sinfo->sinfo_tsn = 1;
2022 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
2023 }
Xin Longd42cb062018-03-01 23:05:15 +08002024}
2025
Ying Xue1b784142015-03-02 15:37:48 +08002026static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Xin Long204f8172018-03-01 23:05:14 +08002028 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Xin Long8e87c6e2018-03-01 23:05:16 +08002029 struct sctp_transport *transport = NULL;
Xin Long204f8172018-03-01 23:05:14 +08002030 struct sctp_sndrcvinfo _sinfo, *sinfo;
Greg Kroah-Hartmanba59fb02019-02-01 15:15:22 +01002031 struct sctp_association *asoc, *tmp;
Xin Long007b7e12018-03-01 23:05:17 +08002032 struct sctp_cmsgs cmsgs;
Xin Longbecef9b2018-03-01 23:05:13 +08002033 union sctp_addr *daddr;
Xin Long007b7e12018-03-01 23:05:17 +08002034 bool new = false;
2035 __u16 sflags;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02002036 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Xin Long204f8172018-03-01 23:05:14 +08002038 /* Parse and get snd_info */
2039 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
2040 if (err)
Xin Long007b7e12018-03-01 23:05:17 +08002041 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Xin Long204f8172018-03-01 23:05:14 +08002043 sinfo = &_sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08002044 sflags = sinfo->sinfo_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Xin Longbecef9b2018-03-01 23:05:13 +08002046 /* Get daddr from msg */
2047 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
2048 if (IS_ERR(daddr)) {
2049 err = PTR_ERR(daddr);
Xin Long007b7e12018-03-01 23:05:17 +08002050 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052
wangweidong048ed4b2014-01-21 15:44:11 +08002053 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Xin Long49102802018-03-05 20:44:20 +08002055 /* SCTP_SENDALL process */
2056 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
Greg Kroah-Hartmanba59fb02019-02-01 15:15:22 +01002057 list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
Xin Long49102802018-03-05 20:44:20 +08002058 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2059 msg_len);
2060 if (err == 0)
2061 continue;
2062 if (err < 0)
2063 goto out_unlock;
2064
2065 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2066
2067 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
2068 NULL, sinfo);
2069 if (err < 0)
2070 goto out_unlock;
2071
2072 iov_iter_revert(&msg->msg_iter, err);
2073 }
2074
2075 goto out_unlock;
2076 }
2077
Xin Long0a3920d2018-03-01 23:05:18 +08002078 /* Get and check or create asoc */
Xin Longbecef9b2018-03-01 23:05:13 +08002079 if (daddr) {
Xin Longbecef9b2018-03-01 23:05:13 +08002080 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
Xin Long0a3920d2018-03-01 23:05:18 +08002081 if (asoc) {
2082 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2083 msg_len);
2084 if (err <= 0)
2085 goto out_unlock;
2086 } else {
2087 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2088 &transport);
2089 if (err)
2090 goto out_unlock;
2091
2092 asoc = transport->asoc;
2093 new = true;
2094 }
2095
2096 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2097 transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 } else {
Xin Long007b7e12018-03-01 23:05:17 +08002099 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (!asoc) {
2101 err = -EPIPE;
2102 goto out_unlock;
2103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Xin Long007b7e12018-03-01 23:05:17 +08002105 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
Xin Longc2666de2018-03-01 23:05:12 +08002106 if (err <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
2109
Xin Longd42cb062018-03-01 23:05:15 +08002110 /* Update snd_info with the asoc */
2111 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Xin Longf84af332018-03-01 23:05:10 +08002113 /* Send msg to the asoc */
Xin Long8e87c6e2018-03-01 23:05:16 +08002114 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
Xin Long007b7e12018-03-01 23:05:17 +08002115 if (err < 0 && err != -ESRCH && new)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 sctp_association_free(asoc);
Xin Long8e87c6e2018-03-01 23:05:16 +08002117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118out_unlock:
wangweidong048ed4b2014-01-21 15:44:11 +08002119 release_sock(sk);
Xin Long007b7e12018-03-01 23:05:17 +08002120out:
Xin Longf84af332018-03-01 23:05:10 +08002121 return sctp_error(sk, msg->msg_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
2124/* This is an extended version of skb_pull() that removes the data from the
2125 * start of a skb even when data is spread across the list of skb's in the
2126 * frag_list. len specifies the total amount of data that needs to be removed.
2127 * when 'len' bytes could be removed from the skb, it returns 0.
2128 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2129 * could not be removed.
2130 */
2131static int sctp_skb_pull(struct sk_buff *skb, int len)
2132{
2133 struct sk_buff *list;
2134 int skb_len = skb_headlen(skb);
2135 int rlen;
2136
2137 if (len <= skb_len) {
2138 __skb_pull(skb, len);
2139 return 0;
2140 }
2141 len -= skb_len;
2142 __skb_pull(skb, skb_len);
2143
David S. Miller1b003be2009-06-09 00:22:35 -07002144 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 rlen = sctp_skb_pull(list, len);
2146 skb->len -= (len-rlen);
2147 skb->data_len -= (len-rlen);
2148
2149 if (!rlen)
2150 return 0;
2151
2152 len = rlen;
2153 }
2154
2155 return len;
2156}
2157
2158/* API 3.1.3 recvmsg() - UDP Style Syntax
2159 *
2160 * ssize_t recvmsg(int socket, struct msghdr *message,
2161 * int flags);
2162 *
2163 * socket - the socket descriptor of the endpoint.
2164 * message - pointer to the msghdr structure which contains a single
2165 * user message and possibly some ancillary data.
2166 *
2167 * See Section 5 for complete description of the data
2168 * structures.
2169 *
2170 * flags - flags sent or received with the user message, see Section
2171 * 5 for complete description of the flags.
2172 */
Ying Xue1b784142015-03-02 15:37:48 +08002173static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2174 int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
2176 struct sctp_ulpevent *event = NULL;
2177 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002178 struct sk_buff *skb, *head_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 int copied;
2180 int err = 0;
2181 int skb_len;
2182
Daniel Borkmannbb333812013-06-28 19:49:40 +02002183 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2184 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2185 addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
wangweidong048ed4b2014-01-21 15:44:11 +08002187 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03002189 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
Xin Longe0878692016-07-30 14:14:41 +08002190 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 err = -ENOTCONN;
2192 goto out;
2193 }
2194
2195 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2196 if (!skb)
2197 goto out;
2198
2199 /* Get the total length of the skb including any skb's in the
2200 * frag_list.
2201 */
2202 skb_len = skb->len;
2203
2204 copied = skb_len;
2205 if (copied > len)
2206 copied = len;
2207
David S. Miller51f3d022014-11-05 16:46:40 -05002208 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 event = sctp_skb2event(skb);
2211
2212 if (err)
2213 goto out_free;
2214
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002215 if (event->chunk && event->chunk->head_skb)
2216 head_skb = event->chunk->head_skb;
2217 else
2218 head_skb = skb;
2219 sock_recv_ts_and_drops(msg, sk, head_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (sctp_ulpevent_is_notification(event)) {
2221 msg->msg_flags |= MSG_NOTIFICATION;
2222 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2223 } else {
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002224 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 }
2226
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02002227 /* Check if we allow SCTP_NXTINFO. */
2228 if (sp->recvnxtinfo)
2229 sctp_ulpevent_read_nxtinfo(event, msg, sk);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002230 /* Check if we allow SCTP_RCVINFO. */
2231 if (sp->recvrcvinfo)
2232 sctp_ulpevent_read_rcvinfo(event, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 /* Check if we allow SCTP_SNDRCVINFO. */
Xin Long2cc0eeb2018-11-18 16:08:51 +08002234 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_DATA_IO_EVENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 sctp_ulpevent_read_sndrcvinfo(event, msg);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 err = copied;
2238
2239 /* If skb's length exceeds the user's buffer, update the skb and
2240 * push it back to the receive_queue so that the next call to
2241 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2242 */
2243 if (skb_len > copied) {
2244 msg->msg_flags &= ~MSG_EOR;
2245 if (flags & MSG_PEEK)
2246 goto out_free;
2247 sctp_skb_pull(skb, copied);
2248 skb_queue_head(&sk->sk_receive_queue, skb);
2249
Daniel Borkmann362d5202014-04-14 21:45:17 +02002250 /* When only partial message is copied to the user, increase
2251 * rwnd by that amount. If all the data in the skb is read,
2252 * rwnd is updated when the event is freed.
2253 */
2254 if (!sctp_ulpevent_is_notification(event))
2255 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 goto out;
2257 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2258 (event->msg_flags & MSG_EOR))
2259 msg->msg_flags |= MSG_EOR;
2260 else
2261 msg->msg_flags &= ~MSG_EOR;
2262
2263out_free:
2264 if (flags & MSG_PEEK) {
2265 /* Release the skb reference acquired after peeking the skb in
2266 * sctp_skb_recv_datagram().
2267 */
2268 kfree_skb(skb);
2269 } else {
2270 /* Free the event which includes releasing the reference to
2271 * the owner of the skb, freeing the skb and updating the
2272 * rwnd.
2273 */
2274 sctp_ulpevent_free(event);
2275 }
2276out:
wangweidong048ed4b2014-01-21 15:44:11 +08002277 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return err;
2279}
2280
2281/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2282 *
2283 * This option is a on/off flag. If enabled no SCTP message
2284 * fragmentation will be performed. Instead if a message being sent
2285 * exceeds the current PMTU size, the message will NOT be sent and
2286 * instead a error will be indicated to the user.
2287 */
2288static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002289 char __user *optval,
2290 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
2292 int val;
2293
2294 if (optlen < sizeof(int))
2295 return -EINVAL;
2296
2297 if (get_user(val, (int __user *)optval))
2298 return -EFAULT;
2299
2300 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2301
2302 return 0;
2303}
2304
2305static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002306 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
Xin Long2cc0eeb2018-11-18 16:08:51 +08002308 struct sctp_event_subscribe subscribe;
2309 __u8 *sn_type = (__u8 *)&subscribe;
2310 struct sctp_sock *sp = sctp_sk(sk);
Xin Longa1e3a052018-11-18 16:08:52 +08002311 struct sctp_association *asoc;
Xin Long2cc0eeb2018-11-18 16:08:51 +08002312 int i;
Wei Yongjun94912302011-07-02 09:28:04 +00002313
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002314 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return -EINVAL;
Xin Long2cc0eeb2018-11-18 16:08:51 +08002316
2317 if (copy_from_user(&subscribe, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002319
Xin Long2cc0eeb2018-11-18 16:08:51 +08002320 for (i = 0; i < optlen; i++)
2321 sctp_ulpevent_type_set(&sp->subscribe, SCTP_SN_TYPE_BASE + i,
2322 sn_type[i]);
2323
Xin Longa1e3a052018-11-18 16:08:52 +08002324 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2325 asoc->subscribe = sctp_sk(sk)->subscribe;
2326
Daniel Borkmannbbbea412014-07-12 20:30:40 +02002327 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
Wei Yongjun94912302011-07-02 09:28:04 +00002328 * if there is no data to be sent or retransmit, the stack will
2329 * immediately send up this notification.
2330 */
Xin Long2cc0eeb2018-11-18 16:08:51 +08002331 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_SENDER_DRY_EVENT)) {
Xin Long2cc0eeb2018-11-18 16:08:51 +08002332 struct sctp_ulpevent *event;
Wei Yongjun94912302011-07-02 09:28:04 +00002333
Xin Longa1e3a052018-11-18 16:08:52 +08002334 asoc = sctp_id2assoc(sk, 0);
Wei Yongjun94912302011-07-02 09:28:04 +00002335 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2336 event = sctp_ulpevent_make_sender_dry_event(asoc,
Marcelo Ricardo Leitner2e83acb2018-01-08 19:02:27 -02002337 GFP_USER | __GFP_NOWARN);
Wei Yongjun94912302011-07-02 09:28:04 +00002338 if (!event)
2339 return -ENOMEM;
2340
Xin Long9162e0e2017-12-08 21:04:05 +08002341 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
Wei Yongjun94912302011-07-02 09:28:04 +00002342 }
2343 }
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return 0;
2346}
2347
2348/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2349 *
2350 * This socket option is applicable to the UDP-style socket only. When
2351 * set it will cause associations that are idle for more than the
2352 * specified number of seconds to automatically close. An association
2353 * being idle is defined an association that has NOT sent or received
2354 * user data. The special value of '0' indicates that no automatic
2355 * close of any associations should be performed. The option expects an
2356 * integer defining the number of seconds of idle time before an
2357 * association is closed.
2358 */
2359static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002360 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
2362 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman9f70f462013-12-10 06:48:15 -05002363 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 /* Applicable to UDP-style socket only */
2366 if (sctp_style(sk, TCP))
2367 return -EOPNOTSUPP;
2368 if (optlen != sizeof(int))
2369 return -EINVAL;
2370 if (copy_from_user(&sp->autoclose, optval, optlen))
2371 return -EFAULT;
2372
Neil Horman9f70f462013-12-10 06:48:15 -05002373 if (sp->autoclose > net->sctp.max_autoclose)
2374 sp->autoclose = net->sctp.max_autoclose;
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return 0;
2377}
2378
2379/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2380 *
2381 * Applications can enable or disable heartbeats for any peer address of
2382 * an association, modify an address's heartbeat interval, force a
2383 * heartbeat to be sent immediately, and adjust the address's maximum
2384 * number of retransmissions sent before an address is considered
2385 * unreachable. The following structure is used to access and modify an
2386 * address's parameters:
2387 *
2388 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002389 * sctp_assoc_t spp_assoc_id;
2390 * struct sockaddr_storage spp_address;
2391 * uint32_t spp_hbinterval;
2392 * uint16_t spp_pathmaxrxt;
2393 * uint32_t spp_pathmtu;
2394 * uint32_t spp_sackdelay;
2395 * uint32_t spp_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08002396 * uint32_t spp_ipv6_flowlabel;
2397 * uint8_t spp_dscp;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002398 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002400 * spp_assoc_id - (one-to-many style socket) This is filled in the
2401 * application, and identifies the association for
2402 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 * spp_address - This specifies which address is of interest.
2404 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002405 * in milliseconds. If a value of zero
2406 * is present in this field then no changes are to
2407 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 * spp_pathmaxrxt - This contains the maximum number of
2409 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002410 * considered unreachable. If a value of zero
2411 * is present in this field then no changes are to
2412 * be made to this parameter.
2413 * spp_pathmtu - When Path MTU discovery is disabled the value
2414 * specified here will be the "fixed" path mtu.
2415 * Note that if the spp_address field is empty
2416 * then all associations on this address will
2417 * have this fixed path mtu set upon them.
2418 *
2419 * spp_sackdelay - When delayed sack is enabled, this value specifies
2420 * the number of milliseconds that sacks will be delayed
2421 * for. This value will apply to all addresses of an
2422 * association if the spp_address field is empty. Note
2423 * also, that if delayed sack is enabled and this
2424 * value is set to 0, no change is made to the last
2425 * recorded delayed sack timer value.
2426 *
2427 * spp_flags - These flags are used to control various features
2428 * on an association. The flag field may contain
2429 * zero or more of the following options.
2430 *
2431 * SPP_HB_ENABLE - Enable heartbeats on the
2432 * specified address. Note that if the address
2433 * field is empty all addresses for the association
2434 * have heartbeats enabled upon them.
2435 *
2436 * SPP_HB_DISABLE - Disable heartbeats on the
2437 * speicifed address. Note that if the address
2438 * field is empty all addresses for the association
2439 * will have their heartbeats disabled. Note also
2440 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2441 * mutually exclusive, only one of these two should
2442 * be specified. Enabling both fields will have
2443 * undetermined results.
2444 *
2445 * SPP_HB_DEMAND - Request a user initiated heartbeat
2446 * to be made immediately.
2447 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002448 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2449 * heartbeat delayis to be set to the value of 0
2450 * milliseconds.
2451 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002452 * SPP_PMTUD_ENABLE - This field will enable PMTU
2453 * discovery upon the specified address. Note that
2454 * if the address feild is empty then all addresses
2455 * on the association are effected.
2456 *
2457 * SPP_PMTUD_DISABLE - This field will disable PMTU
2458 * discovery upon the specified address. Note that
2459 * if the address feild is empty then all addresses
2460 * on the association are effected. Not also that
2461 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2462 * exclusive. Enabling both will have undetermined
2463 * results.
2464 *
2465 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2466 * on delayed sack. The time specified in spp_sackdelay
2467 * is used to specify the sack delay for this address. Note
2468 * that if spp_address is empty then all addresses will
2469 * enable delayed sack and take on the sack delay
2470 * value specified in spp_sackdelay.
2471 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2472 * off delayed sack. If the spp_address field is blank then
2473 * delayed sack is disabled for the entire association. Note
2474 * also that this field is mutually exclusive to
2475 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2476 * results.
Xin Long0b0dce72018-07-02 18:21:13 +08002477 *
2478 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
2479 * setting of the IPV6 flow label value. The value is
2480 * contained in the spp_ipv6_flowlabel field.
2481 * Upon retrieval, this flag will be set to indicate that
2482 * the spp_ipv6_flowlabel field has a valid value returned.
2483 * If a specific destination address is set (in the
2484 * spp_address field), then the value returned is that of
2485 * the address. If just an association is specified (and
2486 * no address), then the association's default flow label
2487 * is returned. If neither an association nor a destination
2488 * is specified, then the socket's default flow label is
2489 * returned. For non-IPv6 sockets, this flag will be left
2490 * cleared.
2491 *
2492 * SPP_DSCP: Setting this flag enables the setting of the
2493 * Differentiated Services Code Point (DSCP) value
2494 * associated with either the association or a specific
2495 * address. The value is obtained in the spp_dscp field.
2496 * Upon retrieval, this flag will be set to indicate that
2497 * the spp_dscp field has a valid value returned. If a
2498 * specific destination address is set when called (in the
2499 * spp_address field), then that specific destination
2500 * address's DSCP value is returned. If just an association
2501 * is specified, then the association's default DSCP is
2502 * returned. If neither an association nor a destination is
2503 * specified, then the socket's default DSCP is returned.
2504 *
2505 * spp_ipv6_flowlabel
2506 * - This field is used in conjunction with the
2507 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
2508 * The 20 least significant bits are used for the flow
2509 * label. This setting has precedence over any IPv6-layer
2510 * setting.
2511 *
2512 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
2513 * and contains the DSCP. The 6 most significant bits are
2514 * used for the DSCP. This setting has precedence over any
2515 * IPv4- or IPv6- layer setting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 */
Adrian Bunk16164362006-09-18 00:40:38 -07002517static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2518 struct sctp_transport *trans,
2519 struct sctp_association *asoc,
2520 struct sctp_sock *sp,
2521 int hb_change,
2522 int pmtud_change,
2523 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 int error;
2526
Frank Filz52ccb8e2005-12-22 11:36:46 -08002527 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002528 struct net *net = sock_net(trans->asoc->base.sk);
2529
2530 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (error)
2532 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
2534
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002535 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2536 * this field is ignored. Note also that a value of zero indicates
2537 * the current setting should be left unchanged.
2538 */
2539 if (params->spp_flags & SPP_HB_ENABLE) {
2540
2541 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2542 * set. This lets us use 0 value when this flag
2543 * is set.
2544 */
2545 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2546 params->spp_hbinterval = 0;
2547
2548 if (params->spp_hbinterval ||
2549 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2550 if (trans) {
2551 trans->hbinterval =
2552 msecs_to_jiffies(params->spp_hbinterval);
2553 } else if (asoc) {
2554 asoc->hbinterval =
2555 msecs_to_jiffies(params->spp_hbinterval);
2556 } else {
2557 sp->hbinterval = params->spp_hbinterval;
2558 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002559 }
2560 }
2561
2562 if (hb_change) {
2563 if (trans) {
2564 trans->param_flags =
2565 (trans->param_flags & ~SPP_HB) | hb_change;
2566 } else if (asoc) {
2567 asoc->param_flags =
2568 (asoc->param_flags & ~SPP_HB) | hb_change;
2569 } else {
2570 sp->param_flags =
2571 (sp->param_flags & ~SPP_HB) | hb_change;
2572 }
2573 }
2574
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002575 /* When Path MTU discovery is disabled the value specified here will
2576 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2577 * include the flag SPP_PMTUD_DISABLE for this field to have any
2578 * effect).
2579 */
2580 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002581 if (trans) {
2582 trans->pathmtu = params->spp_pathmtu;
Xin Long3ebfdf02017-04-04 13:39:55 +08002583 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002584 } else if (asoc) {
Marcelo Ricardo Leitnerc4b28932018-04-26 16:58:53 -03002585 sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002586 } else {
2587 sp->pathmtu = params->spp_pathmtu;
2588 }
2589 }
2590
2591 if (pmtud_change) {
2592 if (trans) {
2593 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2594 (params->spp_flags & SPP_PMTUD_ENABLE);
2595 trans->param_flags =
2596 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2597 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002598 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
Xin Long3ebfdf02017-04-04 13:39:55 +08002599 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002600 }
2601 } else if (asoc) {
2602 asoc->param_flags =
2603 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2604 } else {
2605 sp->param_flags =
2606 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2607 }
2608 }
2609
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002610 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2611 * value of this field is ignored. Note also that a value of zero
2612 * indicates the current setting should be left unchanged.
2613 */
2614 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002615 if (trans) {
2616 trans->sackdelay =
2617 msecs_to_jiffies(params->spp_sackdelay);
2618 } else if (asoc) {
2619 asoc->sackdelay =
2620 msecs_to_jiffies(params->spp_sackdelay);
2621 } else {
2622 sp->sackdelay = params->spp_sackdelay;
2623 }
2624 }
2625
2626 if (sackdelay_change) {
2627 if (trans) {
2628 trans->param_flags =
2629 (trans->param_flags & ~SPP_SACKDELAY) |
2630 sackdelay_change;
2631 } else if (asoc) {
2632 asoc->param_flags =
2633 (asoc->param_flags & ~SPP_SACKDELAY) |
2634 sackdelay_change;
2635 } else {
2636 sp->param_flags =
2637 (sp->param_flags & ~SPP_SACKDELAY) |
2638 sackdelay_change;
2639 }
2640 }
2641
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002642 /* Note that a value of zero indicates the current setting should be
2643 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002644 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002645 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002646 if (trans) {
2647 trans->pathmaxrxt = params->spp_pathmaxrxt;
2648 } else if (asoc) {
2649 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2650 } else {
2651 sp->pathmaxrxt = params->spp_pathmaxrxt;
2652 }
2653 }
2654
Xin Long0b0dce72018-07-02 18:21:13 +08002655 if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
Xin Long741880e2018-09-03 15:47:11 +08002656 if (trans) {
2657 if (trans->ipaddr.sa.sa_family == AF_INET6) {
2658 trans->flowlabel = params->spp_ipv6_flowlabel &
2659 SCTP_FLOWLABEL_VAL_MASK;
2660 trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2661 }
Xin Long0b0dce72018-07-02 18:21:13 +08002662 } else if (asoc) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002663 struct sctp_transport *t;
2664
2665 list_for_each_entry(t, &asoc->peer.transport_addr_list,
Xin Long0b0dce72018-07-02 18:21:13 +08002666 transports) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002667 if (t->ipaddr.sa.sa_family != AF_INET6)
Xin Long0b0dce72018-07-02 18:21:13 +08002668 continue;
Xin Longaf8a2b82018-09-03 15:47:10 +08002669 t->flowlabel = params->spp_ipv6_flowlabel &
2670 SCTP_FLOWLABEL_VAL_MASK;
2671 t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
Xin Long0b0dce72018-07-02 18:21:13 +08002672 }
2673 asoc->flowlabel = params->spp_ipv6_flowlabel &
2674 SCTP_FLOWLABEL_VAL_MASK;
2675 asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2676 } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
2677 sp->flowlabel = params->spp_ipv6_flowlabel &
2678 SCTP_FLOWLABEL_VAL_MASK;
2679 sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2680 }
2681 }
2682
2683 if (params->spp_flags & SPP_DSCP) {
2684 if (trans) {
2685 trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2686 trans->dscp |= SCTP_DSCP_SET_MASK;
2687 } else if (asoc) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002688 struct sctp_transport *t;
2689
2690 list_for_each_entry(t, &asoc->peer.transport_addr_list,
Xin Long0b0dce72018-07-02 18:21:13 +08002691 transports) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002692 t->dscp = params->spp_dscp &
2693 SCTP_DSCP_VAL_MASK;
2694 t->dscp |= SCTP_DSCP_SET_MASK;
Xin Long0b0dce72018-07-02 18:21:13 +08002695 }
2696 asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2697 asoc->dscp |= SCTP_DSCP_SET_MASK;
2698 } else {
2699 sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2700 sp->dscp |= SCTP_DSCP_SET_MASK;
2701 }
2702 }
2703
Frank Filz52ccb8e2005-12-22 11:36:46 -08002704 return 0;
2705}
2706
2707static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002708 char __user *optval,
2709 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002710{
2711 struct sctp_paddrparams params;
2712 struct sctp_transport *trans = NULL;
2713 struct sctp_association *asoc = NULL;
2714 struct sctp_sock *sp = sctp_sk(sk);
2715 int error;
2716 int hb_change, pmtud_change, sackdelay_change;
2717
Xin Long0b0dce72018-07-02 18:21:13 +08002718 if (optlen == sizeof(params)) {
2719 if (copy_from_user(&params, optval, optlen))
2720 return -EFAULT;
2721 } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
2722 spp_ipv6_flowlabel), 4)) {
2723 if (copy_from_user(&params, optval, optlen))
2724 return -EFAULT;
2725 if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
2726 return -EINVAL;
2727 } else {
wangweidongcb3f8372013-12-23 12:16:50 +08002728 return -EINVAL;
Xin Long0b0dce72018-07-02 18:21:13 +08002729 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002730
2731 /* Validate flags and value parameters. */
2732 hb_change = params.spp_flags & SPP_HB;
2733 pmtud_change = params.spp_flags & SPP_PMTUD;
2734 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2735
2736 if (hb_change == SPP_HB ||
2737 pmtud_change == SPP_PMTUD ||
2738 sackdelay_change == SPP_SACKDELAY ||
2739 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002740 (params.spp_pathmtu &&
2741 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002742 return -EINVAL;
2743
2744 /* If an address other than INADDR_ANY is specified, and
2745 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 */
wangweidongcb3f8372013-12-23 12:16:50 +08002747 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002748 trans = sctp_addr_id2transport(sk, &params.spp_address,
2749 params.spp_assoc_id);
2750 if (!trans)
2751 return -EINVAL;
2752 }
2753
Xin Longb99e5e02019-01-28 15:08:24 +08002754 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
2755 * socket is a one to many style socket, and an association
2756 * was not found, then the id was invalid.
Frank Filz52ccb8e2005-12-22 11:36:46 -08002757 */
2758 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
Xin Longb99e5e02019-01-28 15:08:24 +08002759 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
2760 sctp_style(sk, UDP))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002761 return -EINVAL;
2762
2763 /* Heartbeat demand can only be sent on a transport or
2764 * association, but not a socket.
2765 */
2766 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2767 return -EINVAL;
2768
2769 /* Process parameters. */
2770 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2771 hb_change, pmtud_change,
2772 sackdelay_change);
2773
2774 if (error)
2775 return error;
2776
2777 /* If changes are for association, also apply parameters to each
2778 * transport.
2779 */
2780 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002781 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2782 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002783 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2784 hb_change, pmtud_change,
2785 sackdelay_change);
2786 }
2787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 return 0;
2790}
2791
wangweidong0ea5e4d2014-01-15 17:24:01 +08002792static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2793{
2794 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2795}
2796
2797static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2798{
2799 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2800}
2801
Xin Long9c5829e2019-01-28 15:08:34 +08002802static void sctp_apply_asoc_delayed_ack(struct sctp_sack_info *params,
2803 struct sctp_association *asoc)
2804{
2805 struct sctp_transport *trans;
2806
2807 if (params->sack_delay) {
2808 asoc->sackdelay = msecs_to_jiffies(params->sack_delay);
2809 asoc->param_flags =
2810 sctp_spp_sackdelay_enable(asoc->param_flags);
2811 }
2812 if (params->sack_freq == 1) {
2813 asoc->param_flags =
2814 sctp_spp_sackdelay_disable(asoc->param_flags);
2815 } else if (params->sack_freq > 1) {
2816 asoc->sackfreq = params->sack_freq;
2817 asoc->param_flags =
2818 sctp_spp_sackdelay_enable(asoc->param_flags);
2819 }
2820
2821 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2822 transports) {
2823 if (params->sack_delay) {
2824 trans->sackdelay = msecs_to_jiffies(params->sack_delay);
2825 trans->param_flags =
2826 sctp_spp_sackdelay_enable(trans->param_flags);
2827 }
2828 if (params->sack_freq == 1) {
2829 trans->param_flags =
2830 sctp_spp_sackdelay_disable(trans->param_flags);
2831 } else if (params->sack_freq > 1) {
2832 trans->sackfreq = params->sack_freq;
2833 trans->param_flags =
2834 sctp_spp_sackdelay_enable(trans->param_flags);
2835 }
2836 }
2837}
2838
Wei Yongjund364d922008-05-09 15:13:26 -07002839/*
2840 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002841 *
Wei Yongjund364d922008-05-09 15:13:26 -07002842 * This option will effect the way delayed acks are performed. This
2843 * option allows you to get or set the delayed ack time, in
2844 * milliseconds. It also allows changing the delayed ack frequency.
2845 * Changing the frequency to 1 disables the delayed sack algorithm. If
2846 * the assoc_id is 0, then this sets or gets the endpoints default
2847 * values. If the assoc_id field is non-zero, then the set or get
2848 * effects the specified association for the one to many model (the
2849 * assoc_id field is ignored by the one to one model). Note that if
2850 * sack_delay or sack_freq are 0 when setting this option, then the
2851 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002852 *
Wei Yongjund364d922008-05-09 15:13:26 -07002853 * struct sctp_sack_info {
2854 * sctp_assoc_t sack_assoc_id;
2855 * uint32_t sack_delay;
2856 * uint32_t sack_freq;
2857 * };
Frank Filz77086102005-12-22 11:37:30 -08002858 *
Wei Yongjund364d922008-05-09 15:13:26 -07002859 * sack_assoc_id - This parameter, indicates which association the user
2860 * is performing an action upon. Note that if this field's value is
2861 * zero then the endpoints default value is changed (effecting future
2862 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002863 *
Wei Yongjund364d922008-05-09 15:13:26 -07002864 * sack_delay - This parameter contains the number of milliseconds that
2865 * the user is requesting the delayed ACK timer be set to. Note that
2866 * this value is defined in the standard to be between 200 and 500
2867 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002868 *
Wei Yongjund364d922008-05-09 15:13:26 -07002869 * sack_freq - This parameter contains the number of packets that must
2870 * be received before a sack is sent without waiting for the delay
2871 * timer to expire. The default value for this is 2, setting this
2872 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002873 */
2874
Wei Yongjund364d922008-05-09 15:13:26 -07002875static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002876 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002877{
Xin Long9c5829e2019-01-28 15:08:34 +08002878 struct sctp_sock *sp = sctp_sk(sk);
2879 struct sctp_association *asoc;
2880 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002881
Wei Yongjund364d922008-05-09 15:13:26 -07002882 if (optlen == sizeof(struct sctp_sack_info)) {
2883 if (copy_from_user(&params, optval, optlen))
2884 return -EFAULT;
2885
2886 if (params.sack_delay == 0 && params.sack_freq == 0)
2887 return 0;
2888 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05002889 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05002890 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05002891 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05002892 "Use struct sctp_sack_info instead\n",
2893 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07002894 if (copy_from_user(&params, optval, optlen))
2895 return -EFAULT;
2896
2897 if (params.sack_delay == 0)
2898 params.sack_freq = 1;
2899 else
2900 params.sack_freq = 0;
2901 } else
wangweidongcb3f8372013-12-23 12:16:50 +08002902 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08002903
Frank Filz77086102005-12-22 11:37:30 -08002904 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002905 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002906 return -EINVAL;
2907
Xin Long9c5829e2019-01-28 15:08:34 +08002908 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
2909 * socket is a one to many style socket, and an association
2910 * was not found, then the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002911 */
Wei Yongjund364d922008-05-09 15:13:26 -07002912 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
Xin Long9c5829e2019-01-28 15:08:34 +08002913 if (!asoc && params.sack_assoc_id > SCTP_ALL_ASSOC &&
2914 sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002915 return -EINVAL;
2916
Xin Long9c5829e2019-01-28 15:08:34 +08002917 if (asoc) {
2918 sctp_apply_asoc_delayed_ack(&params, asoc);
2919
2920 return 0;
2921 }
2922
Xin Long8e2614f2019-03-18 20:06:00 +08002923 if (sctp_style(sk, TCP))
2924 params.sack_assoc_id = SCTP_FUTURE_ASSOC;
2925
Xin Long9c5829e2019-01-28 15:08:34 +08002926 if (params.sack_assoc_id == SCTP_FUTURE_ASSOC ||
2927 params.sack_assoc_id == SCTP_ALL_ASSOC) {
2928 if (params.sack_delay) {
Wei Yongjund364d922008-05-09 15:13:26 -07002929 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002930 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002931 sctp_spp_sackdelay_enable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002932 }
Xin Long9c5829e2019-01-28 15:08:34 +08002933 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002934 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002935 sctp_spp_sackdelay_disable(sp->param_flags);
Xin Long9c5829e2019-01-28 15:08:34 +08002936 } else if (params.sack_freq > 1) {
Wei Yongjund364d922008-05-09 15:13:26 -07002937 sp->sackfreq = params.sack_freq;
2938 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002939 sctp_spp_sackdelay_enable(sp->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002940 }
Frank Filz77086102005-12-22 11:37:30 -08002941 }
2942
Xin Long9c5829e2019-01-28 15:08:34 +08002943 if (params.sack_assoc_id == SCTP_CURRENT_ASSOC ||
2944 params.sack_assoc_id == SCTP_ALL_ASSOC)
2945 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2946 sctp_apply_asoc_delayed_ack(&params, asoc);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002947
Frank Filz77086102005-12-22 11:37:30 -08002948 return 0;
2949}
2950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2952 *
2953 * Applications can specify protocol parameters for the default association
2954 * initialization. The option name argument to setsockopt() and getsockopt()
2955 * is SCTP_INITMSG.
2956 *
2957 * Setting initialization parameters is effective only on an unconnected
2958 * socket (for UDP-style sockets only future associations are effected
2959 * by the change). With TCP-style sockets, this option is inherited by
2960 * sockets derived from a listener socket.
2961 */
David S. Millerb7058842009-09-30 16:12:20 -07002962static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
2964 struct sctp_initmsg sinit;
2965 struct sctp_sock *sp = sctp_sk(sk);
2966
2967 if (optlen != sizeof(struct sctp_initmsg))
2968 return -EINVAL;
2969 if (copy_from_user(&sinit, optval, optlen))
2970 return -EFAULT;
2971
2972 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002973 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002975 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002977 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002979 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 return 0;
2982}
2983
2984/*
2985 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2986 *
2987 * Applications that wish to use the sendto() system call may wish to
2988 * specify a default set of parameters that would normally be supplied
2989 * through the inclusion of ancillary data. This socket option allows
2990 * such an application to set the default sctp_sndrcvinfo structure.
2991 * The application that wishes to use this socket option simply passes
2992 * in to this call the sctp_sndrcvinfo structure defined in Section
2993 * 5.2.2) The input parameters accepted by this call include
2994 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2995 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2996 * to this call if the caller is using the UDP model.
2997 */
2998static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002999 char __user *optval,
3000 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003003 struct sctp_association *asoc;
3004 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003006 if (optlen != sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 return -EINVAL;
3008 if (copy_from_user(&info, optval, optlen))
3009 return -EFAULT;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003010 if (info.sinfo_flags &
3011 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3012 SCTP_ABORT | SCTP_EOF))
3013 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
Xin Long707e45b2019-01-28 15:08:35 +08003016 if (!asoc && info.sinfo_assoc_id > SCTP_ALL_ASSOC &&
3017 sctp_style(sk, UDP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 return -EINVAL;
Xin Long707e45b2019-01-28 15:08:35 +08003019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 if (asoc) {
3021 asoc->default_stream = info.sinfo_stream;
3022 asoc->default_flags = info.sinfo_flags;
3023 asoc->default_ppid = info.sinfo_ppid;
3024 asoc->default_context = info.sinfo_context;
3025 asoc->default_timetolive = info.sinfo_timetolive;
Xin Long707e45b2019-01-28 15:08:35 +08003026
3027 return 0;
3028 }
3029
Marcelo Ricardo Leitner1354e722019-03-18 20:05:59 +08003030 if (sctp_style(sk, TCP))
3031 info.sinfo_assoc_id = SCTP_FUTURE_ASSOC;
3032
Xin Long707e45b2019-01-28 15:08:35 +08003033 if (info.sinfo_assoc_id == SCTP_FUTURE_ASSOC ||
3034 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 sp->default_stream = info.sinfo_stream;
3036 sp->default_flags = info.sinfo_flags;
3037 sp->default_ppid = info.sinfo_ppid;
3038 sp->default_context = info.sinfo_context;
3039 sp->default_timetolive = info.sinfo_timetolive;
3040 }
3041
Xin Long707e45b2019-01-28 15:08:35 +08003042 if (info.sinfo_assoc_id == SCTP_CURRENT_ASSOC ||
3043 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
3044 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
3045 asoc->default_stream = info.sinfo_stream;
3046 asoc->default_flags = info.sinfo_flags;
3047 asoc->default_ppid = info.sinfo_ppid;
3048 asoc->default_context = info.sinfo_context;
3049 asoc->default_timetolive = info.sinfo_timetolive;
3050 }
3051 }
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 return 0;
3054}
3055
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003056/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
3057 * (SCTP_DEFAULT_SNDINFO)
3058 */
3059static int sctp_setsockopt_default_sndinfo(struct sock *sk,
3060 char __user *optval,
3061 unsigned int optlen)
3062{
3063 struct sctp_sock *sp = sctp_sk(sk);
3064 struct sctp_association *asoc;
3065 struct sctp_sndinfo info;
3066
3067 if (optlen != sizeof(info))
3068 return -EINVAL;
3069 if (copy_from_user(&info, optval, optlen))
3070 return -EFAULT;
3071 if (info.snd_flags &
3072 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3073 SCTP_ABORT | SCTP_EOF))
3074 return -EINVAL;
3075
3076 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
Xin Long92fc3bd2019-01-28 15:08:36 +08003077 if (!asoc && info.snd_assoc_id > SCTP_ALL_ASSOC &&
3078 sctp_style(sk, UDP))
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003079 return -EINVAL;
Xin Long92fc3bd2019-01-28 15:08:36 +08003080
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003081 if (asoc) {
3082 asoc->default_stream = info.snd_sid;
3083 asoc->default_flags = info.snd_flags;
3084 asoc->default_ppid = info.snd_ppid;
3085 asoc->default_context = info.snd_context;
Xin Long92fc3bd2019-01-28 15:08:36 +08003086
3087 return 0;
3088 }
3089
Xin Longa842e652019-03-18 20:06:01 +08003090 if (sctp_style(sk, TCP))
3091 info.snd_assoc_id = SCTP_FUTURE_ASSOC;
3092
Xin Long92fc3bd2019-01-28 15:08:36 +08003093 if (info.snd_assoc_id == SCTP_FUTURE_ASSOC ||
3094 info.snd_assoc_id == SCTP_ALL_ASSOC) {
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003095 sp->default_stream = info.snd_sid;
3096 sp->default_flags = info.snd_flags;
3097 sp->default_ppid = info.snd_ppid;
3098 sp->default_context = info.snd_context;
3099 }
3100
Xin Long92fc3bd2019-01-28 15:08:36 +08003101 if (info.snd_assoc_id == SCTP_CURRENT_ASSOC ||
3102 info.snd_assoc_id == SCTP_ALL_ASSOC) {
3103 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
3104 asoc->default_stream = info.snd_sid;
3105 asoc->default_flags = info.snd_flags;
3106 asoc->default_ppid = info.snd_ppid;
3107 asoc->default_context = info.snd_context;
3108 }
3109 }
3110
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003111 return 0;
3112}
3113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3115 *
3116 * Requests that the local SCTP stack use the enclosed peer address as
3117 * the association primary. The enclosed address must be one of the
3118 * association peer's addresses.
3119 */
3120static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003121 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
3123 struct sctp_prim prim;
3124 struct sctp_transport *trans;
Richard Haines2277c7c2018-02-13 20:56:24 +00003125 struct sctp_af *af;
3126 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
3128 if (optlen != sizeof(struct sctp_prim))
3129 return -EINVAL;
3130
3131 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3132 return -EFAULT;
3133
Richard Haines2277c7c2018-02-13 20:56:24 +00003134 /* Allow security module to validate address but need address len. */
3135 af = sctp_get_af_specific(prim.ssp_addr.ss_family);
3136 if (!af)
3137 return -EINVAL;
3138
3139 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
3140 (struct sockaddr *)&prim.ssp_addr,
3141 af->sockaddr_len);
3142 if (err)
3143 return err;
3144
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
3146 if (!trans)
3147 return -EINVAL;
3148
3149 sctp_assoc_set_primary(trans->asoc, trans);
3150
3151 return 0;
3152}
3153
3154/*
3155 * 7.1.5 SCTP_NODELAY
3156 *
3157 * Turn on/off any Nagle-like algorithm. This means that packets are
3158 * generally sent as soon as possible and no unnecessary delays are
3159 * introduced, at the cost of more packets in the network. Expects an
3160 * integer boolean flag.
3161 */
3162static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003163 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
3165 int val;
3166
3167 if (optlen < sizeof(int))
3168 return -EINVAL;
3169 if (get_user(val, (int __user *)optval))
3170 return -EFAULT;
3171
3172 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3173 return 0;
3174}
3175
3176/*
3177 *
3178 * 7.1.1 SCTP_RTOINFO
3179 *
3180 * The protocol parameters used to initialize and bound retransmission
3181 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3182 * and modify these parameters.
3183 * All parameters are time values, in milliseconds. A value of 0, when
3184 * modifying the parameters, indicates that the current value should not
3185 * be changed.
3186 *
3187 */
David S. Millerb7058842009-09-30 16:12:20 -07003188static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3189{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 struct sctp_rtoinfo rtoinfo;
3191 struct sctp_association *asoc;
wangweidong85f935d2013-12-11 09:50:38 +08003192 unsigned long rto_min, rto_max;
3193 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
3195 if (optlen != sizeof (struct sctp_rtoinfo))
3196 return -EINVAL;
3197
3198 if (copy_from_user(&rtoinfo, optval, optlen))
3199 return -EFAULT;
3200
3201 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3202
3203 /* Set the values to the specific association */
Xin Long7adb5ed2019-01-28 15:08:25 +08003204 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
3205 sctp_style(sk, UDP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 return -EINVAL;
3207
wangweidong85f935d2013-12-11 09:50:38 +08003208 rto_max = rtoinfo.srto_max;
3209 rto_min = rtoinfo.srto_min;
3210
3211 if (rto_max)
3212 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3213 else
3214 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3215
3216 if (rto_min)
3217 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3218 else
3219 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3220
3221 if (rto_min > rto_max)
3222 return -EINVAL;
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 if (asoc) {
3225 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003226 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 msecs_to_jiffies(rtoinfo.srto_initial);
wangweidong85f935d2013-12-11 09:50:38 +08003228 asoc->rto_max = rto_max;
3229 asoc->rto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 } else {
3231 /* If there is no association or the association-id = 0
3232 * set the values to the endpoint.
3233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 if (rtoinfo.srto_initial != 0)
3235 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
wangweidong85f935d2013-12-11 09:50:38 +08003236 sp->rtoinfo.srto_max = rto_max;
3237 sp->rtoinfo.srto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 }
3239
3240 return 0;
3241}
3242
3243/*
3244 *
3245 * 7.1.2 SCTP_ASSOCINFO
3246 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02003247 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 * of the association.
3249 * Returns an error if the new association retransmission value is
3250 * greater than the sum of the retransmission value of the peer.
3251 * See [SCTP] for more information.
3252 *
3253 */
David S. Millerb7058842009-09-30 16:12:20 -07003254static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
3256
3257 struct sctp_assocparams assocparams;
3258 struct sctp_association *asoc;
3259
3260 if (optlen != sizeof(struct sctp_assocparams))
3261 return -EINVAL;
3262 if (copy_from_user(&assocparams, optval, optlen))
3263 return -EFAULT;
3264
3265 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3266
Xin Long88893942019-01-28 15:08:26 +08003267 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
3268 sctp_style(sk, UDP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 return -EINVAL;
3270
3271 /* Set the values to the specific association */
3272 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003273 if (assocparams.sasoc_asocmaxrxt != 0) {
3274 __u32 path_sum = 0;
3275 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003276 struct sctp_transport *peer_addr;
3277
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07003278 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3279 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003280 path_sum += peer_addr->pathmaxrxt;
3281 paths++;
3282 }
3283
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003284 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003285 * one path/transport. We do this because path
3286 * retransmissions are only counted when we have more
3287 * then one path.
3288 */
3289 if (paths > 1 &&
3290 assocparams.sasoc_asocmaxrxt > path_sum)
3291 return -EINVAL;
3292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003294 }
3295
Daniel Borkmann52db8822013-06-25 18:17:27 +02003296 if (assocparams.sasoc_cookie_life != 0)
3297 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 } else {
3299 /* Set the values to the endpoint */
3300 struct sctp_sock *sp = sctp_sk(sk);
3301
3302 if (assocparams.sasoc_asocmaxrxt != 0)
3303 sp->assocparams.sasoc_asocmaxrxt =
3304 assocparams.sasoc_asocmaxrxt;
3305 if (assocparams.sasoc_cookie_life != 0)
3306 sp->assocparams.sasoc_cookie_life =
3307 assocparams.sasoc_cookie_life;
3308 }
3309 return 0;
3310}
3311
3312/*
3313 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3314 *
3315 * This socket option is a boolean flag which turns on or off mapped V4
3316 * addresses. If this option is turned on and the socket is type
3317 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3318 * If this option is turned off, then no mapping will be done of V4
3319 * addresses and a user will receive both PF_INET6 and PF_INET type
3320 * addresses on the socket.
3321 */
David S. Millerb7058842009-09-30 16:12:20 -07003322static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323{
3324 int val;
3325 struct sctp_sock *sp = sctp_sk(sk);
3326
3327 if (optlen < sizeof(int))
3328 return -EINVAL;
3329 if (get_user(val, (int __user *)optval))
3330 return -EFAULT;
3331 if (val)
3332 sp->v4mapped = 1;
3333 else
3334 sp->v4mapped = 0;
3335
3336 return 0;
3337}
3338
3339/*
Wei Yongjune89c2092008-12-25 16:54:58 -08003340 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3341 * This option will get or set the maximum size to put in any outgoing
3342 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 * fragmented by SCTP into the specified size. Note that the underlying
3344 * SCTP implementation may fragment into smaller sized chunks when the
3345 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08003346 * the user. The default value for this option is '0' which indicates
3347 * the user is NOT limiting fragmentation and only the PMTU will effect
3348 * SCTP's choice of DATA chunk size. Note also that values set larger
3349 * than the maximum size of an IP datagram will effectively let SCTP
3350 * control fragmentation (i.e. the same as setting this option to 0).
3351 *
3352 * The following structure is used to access and modify this parameter:
3353 *
3354 * struct sctp_assoc_value {
3355 * sctp_assoc_t assoc_id;
3356 * uint32_t assoc_value;
3357 * };
3358 *
3359 * assoc_id: This parameter is ignored for one-to-one style sockets.
3360 * For one-to-many style sockets this parameter indicates which
3361 * association the user is performing an action upon. Note that if
3362 * this field's value is zero then the endpoints default value is
3363 * changed (effecting future associations only).
3364 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 */
David S. Millerb7058842009-09-30 16:12:20 -07003366static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367{
Xin Longecca8f82017-11-17 14:11:11 +08003368 struct sctp_sock *sp = sctp_sk(sk);
Wei Yongjune89c2092008-12-25 16:54:58 -08003369 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 int val;
3372
Wei Yongjune89c2092008-12-25 16:54:58 -08003373 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003374 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003375 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003376 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003377 "Use struct sctp_assoc_value instead\n",
3378 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08003379 if (copy_from_user(&val, optval, optlen))
3380 return -EFAULT;
Xin Long6fd769b2019-01-28 15:08:27 +08003381 params.assoc_id = SCTP_FUTURE_ASSOC;
Wei Yongjune89c2092008-12-25 16:54:58 -08003382 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3383 if (copy_from_user(&params, optval, optlen))
3384 return -EFAULT;
3385 val = params.assoc_value;
Xin Longecca8f82017-11-17 14:11:11 +08003386 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 return -EINVAL;
Xin Longecca8f82017-11-17 14:11:11 +08003388 }
Wei Yongjune89c2092008-12-25 16:54:58 -08003389
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003390 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Long6fd769b2019-01-28 15:08:27 +08003391 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
3392 sctp_style(sk, UDP))
3393 return -EINVAL;
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003394
Xin Longecca8f82017-11-17 14:11:11 +08003395 if (val) {
3396 int min_len, max_len;
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003397 __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
3398 sizeof(struct sctp_data_chunk);
Xin Longecca8f82017-11-17 14:11:11 +08003399
Jakub Audykowiczafd0a802018-12-04 20:27:41 +01003400 min_len = sctp_min_frag_point(sp, datasize);
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003401 max_len = SCTP_MAX_CHUNK_LEN - datasize;
Xin Longecca8f82017-11-17 14:11:11 +08003402
3403 if (val < min_len || val > max_len)
3404 return -EINVAL;
3405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Wei Yongjune89c2092008-12-25 16:54:58 -08003407 if (asoc) {
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003408 asoc->user_frag = val;
Marcelo Ricardo Leitner2f5e3c92018-04-26 16:58:55 -03003409 sctp_assoc_update_frag_point(asoc);
Wei Yongjune89c2092008-12-25 16:54:58 -08003410 } else {
3411 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 }
3413
3414 return 0;
3415}
3416
3417
3418/*
3419 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3420 *
3421 * Requests that the peer mark the enclosed address as the association
3422 * primary. The enclosed address must be one of the association's
3423 * locally bound addresses. The following structure is used to make a
3424 * set primary request:
3425 */
3426static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003427 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003429 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 struct sctp_association *asoc = NULL;
3432 struct sctp_setpeerprim prim;
3433 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003434 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 int err;
3436
3437 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003439 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 return -EPERM;
3441
3442 if (optlen != sizeof(struct sctp_setpeerprim))
3443 return -EINVAL;
3444
3445 if (copy_from_user(&prim, optval, optlen))
3446 return -EFAULT;
3447
3448 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003449 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 return -EINVAL;
3451
3452 if (!asoc->peer.asconf_capable)
3453 return -EPERM;
3454
3455 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3456 return -EPERM;
3457
3458 if (!sctp_state(asoc, ESTABLISHED))
3459 return -ENOTCONN;
3460
Wei Yongjun40a01032010-12-07 17:11:09 +00003461 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3462 if (!af)
3463 return -EINVAL;
3464
3465 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3466 return -EADDRNOTAVAIL;
3467
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3469 return -EADDRNOTAVAIL;
3470
Richard Haines2277c7c2018-02-13 20:56:24 +00003471 /* Allow security module to validate address. */
3472 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3473 (struct sockaddr *)&prim.sspp_addr,
3474 af->sockaddr_len);
3475 if (err)
3476 return err;
3477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3479 chunk = sctp_make_asconf_set_prim(asoc,
3480 (union sctp_addr *)&prim.sspp_addr);
3481 if (!chunk)
3482 return -ENOMEM;
3483
3484 err = sctp_send_asconf(asoc, chunk);
3485
Daniel Borkmannbb333812013-06-28 19:49:40 +02003486 pr_debug("%s: we set peer primary addr primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
3488 return err;
3489}
3490
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003491static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003492 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003494 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003496 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003498 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 return -EFAULT;
3500
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003501 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
3503 return 0;
3504}
3505
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003506/*
3507 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3508 *
3509 * The context field in the sctp_sndrcvinfo structure is normally only
3510 * used when a failed message is retrieved holding the value that was
3511 * sent down on the actual send call. This option allows the setting of
3512 * a default context on an association basis that will be received on
3513 * reading messages from the peer. This is especially helpful in the
3514 * one-2-many model for an application to keep some reference to an
3515 * internal state machine that is processing messages on the
3516 * association. Note that the setting of this value only effects
3517 * received messages from the peer and does not effect the value that is
3518 * saved with outbound messages.
3519 */
3520static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003521 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003522{
Xin Long49b037a2019-01-28 15:08:37 +08003523 struct sctp_sock *sp = sctp_sk(sk);
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003524 struct sctp_assoc_value params;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003525 struct sctp_association *asoc;
3526
3527 if (optlen != sizeof(struct sctp_assoc_value))
3528 return -EINVAL;
3529 if (copy_from_user(&params, optval, optlen))
3530 return -EFAULT;
3531
Xin Long49b037a2019-01-28 15:08:37 +08003532 asoc = sctp_id2assoc(sk, params.assoc_id);
3533 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3534 sctp_style(sk, UDP))
3535 return -EINVAL;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003536
Xin Long49b037a2019-01-28 15:08:37 +08003537 if (asoc) {
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003538 asoc->default_rcv_context = params.assoc_value;
Xin Long49b037a2019-01-28 15:08:37 +08003539
3540 return 0;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003541 }
3542
Xin Long49b037a2019-01-28 15:08:37 +08003543 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3544 params.assoc_id == SCTP_ALL_ASSOC)
3545 sp->default_rcv_context = params.assoc_value;
3546
3547 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3548 params.assoc_id == SCTP_ALL_ASSOC)
3549 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3550 asoc->default_rcv_context = params.assoc_value;
3551
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003552 return 0;
3553}
3554
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003555/*
3556 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3557 *
3558 * This options will at a minimum specify if the implementation is doing
3559 * fragmented interleave. Fragmented interleave, for a one to many
3560 * socket, is when subsequent calls to receive a message may return
3561 * parts of messages from different associations. Some implementations
3562 * may allow you to turn this value on or off. If so, when turned off,
3563 * no fragment interleave will occur (which will cause a head of line
3564 * blocking amongst multiple associations sharing the same one to many
3565 * socket). When this option is turned on, then each receive call may
3566 * come from a different association (thus the user must receive data
3567 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3568 * association each receive belongs to.
3569 *
3570 * This option takes a boolean value. A non-zero value indicates that
3571 * fragmented interleave is on. A value of zero indicates that
3572 * fragmented interleave is off.
3573 *
3574 * Note that it is important that an implementation that allows this
3575 * option to be turned on, have it off by default. Otherwise an unaware
3576 * application using the one to many model may become confused and act
3577 * incorrectly.
3578 */
3579static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3580 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003581 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003582{
3583 int val;
3584
3585 if (optlen != sizeof(int))
3586 return -EINVAL;
3587 if (get_user(val, (int __user *)optval))
3588 return -EFAULT;
3589
Xin Long772a5862017-12-08 21:03:58 +08003590 sctp_sk(sk)->frag_interleave = !!val;
3591
3592 if (!sctp_sk(sk)->frag_interleave)
3593 sctp_sk(sk)->strm_interleave = 0;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003594
3595 return 0;
3596}
3597
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003598/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003599 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003600 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003601 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003602 * This option will set or get the SCTP partial delivery point. This
3603 * point is the size of a message where the partial delivery API will be
3604 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003605 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003606 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003607 * point. Note also that the call will fail if the user attempts to set
3608 * this value larger than the socket receive buffer size.
3609 *
3610 * Note that any single message having a length smaller than or equal to
3611 * the SCTP partial delivery point will be delivered in one single read
3612 * call as long as the user provided buffer is large enough to hold the
3613 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003614 */
3615static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3616 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003617 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003618{
3619 u32 val;
3620
3621 if (optlen != sizeof(u32))
3622 return -EINVAL;
3623 if (get_user(val, (int __user *)optval))
3624 return -EFAULT;
3625
Wei Yongjun8510b932008-12-25 16:59:03 -08003626 /* Note: We double the receive buffer from what the user sets
3627 * it to be, also initial rwnd is based on rcvbuf/2.
3628 */
3629 if (val > (sk->sk_rcvbuf >> 1))
3630 return -EINVAL;
3631
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003632 sctp_sk(sk)->pd_point = val;
3633
3634 return 0; /* is this the right error code? */
3635}
3636
Vlad Yasevich70331572007-03-23 11:34:36 -07003637/*
3638 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3639 *
3640 * This option will allow a user to change the maximum burst of packets
3641 * that can be emitted by this association. Note that the default value
3642 * is 4, and some implementations may restrict this setting so that it
3643 * can only be lowered.
3644 *
3645 * NOTE: This text doesn't seem right. Do this on a socket basis with
3646 * future associations inheriting the socket value.
3647 */
3648static int sctp_setsockopt_maxburst(struct sock *sk,
3649 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003650 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003651{
Xin Longe0651a02019-01-28 15:08:38 +08003652 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman219b99a2008-03-05 13:44:46 -08003653 struct sctp_assoc_value params;
Neil Horman219b99a2008-03-05 13:44:46 -08003654 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003655
Neil Horman219b99a2008-03-05 13:44:46 -08003656 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003657 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003658 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003659 "Use of int in max_burst socket option deprecated.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003660 "Use struct sctp_assoc_value instead\n",
3661 current->comm, task_pid_nr(current));
Xin Longe0651a02019-01-28 15:08:38 +08003662 if (copy_from_user(&params.assoc_value, optval, optlen))
Neil Horman219b99a2008-03-05 13:44:46 -08003663 return -EFAULT;
Xin Longe0651a02019-01-28 15:08:38 +08003664 params.assoc_id = SCTP_FUTURE_ASSOC;
Neil Horman219b99a2008-03-05 13:44:46 -08003665 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3666 if (copy_from_user(&params, optval, optlen))
3667 return -EFAULT;
Neil Horman219b99a2008-03-05 13:44:46 -08003668 } else
3669 return -EINVAL;
3670
Xin Longe0651a02019-01-28 15:08:38 +08003671 asoc = sctp_id2assoc(sk, params.assoc_id);
3672 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3673 sctp_style(sk, UDP))
3674 return -EINVAL;
Neil Horman219b99a2008-03-05 13:44:46 -08003675
Xin Longe0651a02019-01-28 15:08:38 +08003676 if (asoc) {
3677 asoc->max_burst = params.assoc_value;
3678
3679 return 0;
3680 }
3681
3682 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3683 params.assoc_id == SCTP_ALL_ASSOC)
3684 sp->max_burst = params.assoc_value;
3685
3686 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3687 params.assoc_id == SCTP_ALL_ASSOC)
3688 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3689 asoc->max_burst = params.assoc_value;
Vlad Yasevich70331572007-03-23 11:34:36 -07003690
3691 return 0;
3692}
3693
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003694/*
3695 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3696 *
3697 * This set option adds a chunk type that the user is requesting to be
3698 * received only in an authenticated way. Changes to the list of chunks
3699 * will only effect future associations on the socket.
3700 */
3701static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003702 char __user *optval,
3703 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003704{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003705 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003706 struct sctp_authchunk val;
3707
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003708 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003709 return -EACCES;
3710
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003711 if (optlen != sizeof(struct sctp_authchunk))
3712 return -EINVAL;
3713 if (copy_from_user(&val, optval, optlen))
3714 return -EFAULT;
3715
3716 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003717 case SCTP_CID_INIT:
3718 case SCTP_CID_INIT_ACK:
3719 case SCTP_CID_SHUTDOWN_COMPLETE:
3720 case SCTP_CID_AUTH:
3721 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003722 }
3723
3724 /* add this chunk id to the endpoint */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003725 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003726}
3727
3728/*
3729 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3730 *
3731 * This option gets or sets the list of HMAC algorithms that the local
3732 * endpoint requires the peer to use.
3733 */
3734static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003735 char __user *optval,
3736 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003737{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003738 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003739 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003740 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003741 int err;
3742
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003743 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003744 return -EACCES;
3745
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003746 if (optlen < sizeof(struct sctp_hmacalgo))
3747 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003748 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3749 SCTP_AUTH_NUM_HMACS * sizeof(u16));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003750
wangweidongcb3f8372013-12-23 12:16:50 +08003751 hmacs = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003752 if (IS_ERR(hmacs))
3753 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003754
Vlad Yasevichd9724052008-08-27 16:09:49 -07003755 idents = hmacs->shmac_num_idents;
3756 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3757 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003758 err = -EINVAL;
3759 goto out;
3760 }
3761
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003762 err = sctp_auth_ep_set_hmacs(ep, hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003763out:
3764 kfree(hmacs);
3765 return err;
3766}
3767
3768/*
3769 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3770 *
3771 * This option will set a shared secret key which is used to build an
3772 * association shared key.
3773 */
3774static int sctp_setsockopt_auth_key(struct sock *sk,
3775 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003776 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003777{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003778 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003779 struct sctp_authkey *authkey;
3780 struct sctp_association *asoc;
Xin Long7fb3be12019-01-28 15:08:39 +08003781 int ret = -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003782
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003783 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003784 return -EACCES;
3785
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003786 if (optlen <= sizeof(struct sctp_authkey))
3787 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003788 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3789 * this.
3790 */
Xin Long7fb3be12019-01-28 15:08:39 +08003791 optlen = min_t(unsigned int, optlen, USHRT_MAX + sizeof(*authkey));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003792
wangweidongcb3f8372013-12-23 12:16:50 +08003793 authkey = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003794 if (IS_ERR(authkey))
3795 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003796
Xin Long7fb3be12019-01-28 15:08:39 +08003797 if (authkey->sca_keylength > optlen - sizeof(*authkey))
Vlad Yasevich30c22352008-08-25 15:16:19 -07003798 goto out;
Vlad Yasevich30c22352008-08-25 15:16:19 -07003799
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003800 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
Xin Long7fb3be12019-01-28 15:08:39 +08003801 if (!asoc && authkey->sca_assoc_id > SCTP_ALL_ASSOC &&
3802 sctp_style(sk, UDP))
3803 goto out;
3804
3805 if (asoc) {
3806 ret = sctp_auth_set_key(ep, asoc, authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003807 goto out;
3808 }
3809
Xin Long7fb3be12019-01-28 15:08:39 +08003810 if (authkey->sca_assoc_id == SCTP_FUTURE_ASSOC ||
3811 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3812 ret = sctp_auth_set_key(ep, asoc, authkey);
3813 if (ret)
3814 goto out;
3815 }
3816
3817 ret = 0;
3818
3819 if (authkey->sca_assoc_id == SCTP_CURRENT_ASSOC ||
3820 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3821 list_for_each_entry(asoc, &ep->asocs, asocs) {
3822 int res = sctp_auth_set_key(ep, asoc, authkey);
3823
3824 if (res && !ret)
3825 ret = res;
3826 }
3827 }
3828
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003829out:
Daniel Borkmann6ba542a2013-02-08 03:04:34 +00003830 kzfree(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003831 return ret;
3832}
3833
3834/*
3835 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3836 *
3837 * This option will get or set the active shared key to be used to build
3838 * the association shared key.
3839 */
3840static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003841 char __user *optval,
3842 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003843{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003844 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003845 struct sctp_association *asoc;
Xin Longbf9fb6a2019-01-28 15:08:40 +08003846 struct sctp_authkeyid val;
3847 int ret = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003848
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003849 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003850 return -EACCES;
3851
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003852 if (optlen != sizeof(struct sctp_authkeyid))
3853 return -EINVAL;
3854 if (copy_from_user(&val, optval, optlen))
3855 return -EFAULT;
3856
3857 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
Xin Longbf9fb6a2019-01-28 15:08:40 +08003858 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3859 sctp_style(sk, UDP))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003860 return -EINVAL;
3861
Xin Longbf9fb6a2019-01-28 15:08:40 +08003862 if (asoc)
3863 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3864
3865 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3866 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3867 ret = sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3868 if (ret)
3869 return ret;
3870 }
3871
3872 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3873 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3874 list_for_each_entry(asoc, &ep->asocs, asocs) {
3875 int res = sctp_auth_set_active_key(ep, asoc,
3876 val.scact_keynumber);
3877
3878 if (res && !ret)
3879 ret = res;
3880 }
3881 }
3882
3883 return ret;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003884}
3885
3886/*
3887 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3888 *
3889 * This set option will delete a shared secret key from use.
3890 */
3891static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003892 char __user *optval,
3893 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003894{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003895 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003896 struct sctp_association *asoc;
Xin Long3adcc302019-01-28 15:08:41 +08003897 struct sctp_authkeyid val;
3898 int ret = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003899
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003900 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003901 return -EACCES;
3902
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003903 if (optlen != sizeof(struct sctp_authkeyid))
3904 return -EINVAL;
3905 if (copy_from_user(&val, optval, optlen))
3906 return -EFAULT;
3907
3908 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
Xin Long3adcc302019-01-28 15:08:41 +08003909 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3910 sctp_style(sk, UDP))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003911 return -EINVAL;
3912
Xin Long3adcc302019-01-28 15:08:41 +08003913 if (asoc)
3914 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003915
Xin Long3adcc302019-01-28 15:08:41 +08003916 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3917 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3918 ret = sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3919 if (ret)
3920 return ret;
3921 }
3922
3923 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3924 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3925 list_for_each_entry(asoc, &ep->asocs, asocs) {
3926 int res = sctp_auth_del_key_id(ep, asoc,
3927 val.scact_keynumber);
3928
3929 if (res && !ret)
3930 ret = res;
3931 }
3932 }
3933
3934 return ret;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003935}
3936
Michio Honda7dc04d72011-04-26 20:16:31 +09003937/*
Xin Long601590e2018-03-14 19:05:32 +08003938 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
3939 *
3940 * This set option will deactivate a shared secret key.
3941 */
3942static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
3943 unsigned int optlen)
3944{
3945 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Xin Long601590e2018-03-14 19:05:32 +08003946 struct sctp_association *asoc;
Xin Long2af66ff2019-01-28 15:08:42 +08003947 struct sctp_authkeyid val;
3948 int ret = 0;
Xin Long601590e2018-03-14 19:05:32 +08003949
3950 if (!ep->auth_enable)
3951 return -EACCES;
3952
3953 if (optlen != sizeof(struct sctp_authkeyid))
3954 return -EINVAL;
3955 if (copy_from_user(&val, optval, optlen))
3956 return -EFAULT;
3957
3958 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
Xin Long2af66ff2019-01-28 15:08:42 +08003959 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3960 sctp_style(sk, UDP))
Xin Long601590e2018-03-14 19:05:32 +08003961 return -EINVAL;
3962
Xin Long2af66ff2019-01-28 15:08:42 +08003963 if (asoc)
3964 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3965
3966 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3967 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3968 ret = sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3969 if (ret)
3970 return ret;
3971 }
3972
3973 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3974 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3975 list_for_each_entry(asoc, &ep->asocs, asocs) {
3976 int res = sctp_auth_deact_key_id(ep, asoc,
3977 val.scact_keynumber);
3978
3979 if (res && !ret)
3980 ret = res;
3981 }
3982 }
3983
3984 return ret;
Xin Long601590e2018-03-14 19:05:32 +08003985}
3986
3987/*
Michio Honda7dc04d72011-04-26 20:16:31 +09003988 * 8.1.23 SCTP_AUTO_ASCONF
3989 *
3990 * This option will enable or disable the use of the automatic generation of
3991 * ASCONF chunks to add and delete addresses to an existing association. Note
3992 * that this option has two caveats namely: a) it only affects sockets that
3993 * are bound to all addresses available to the SCTP stack, and b) the system
3994 * administrator may have an overriding control that turns the ASCONF feature
3995 * off no matter what setting the socket option may have.
3996 * This option expects an integer boolean flag, where a non-zero value turns on
3997 * the option, and a zero value turns off the option.
3998 * Note. In this implementation, socket operation overrides default parameter
3999 * being set by sysctl as well as FreeBSD implementation
4000 */
4001static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
4002 unsigned int optlen)
4003{
4004 int val;
4005 struct sctp_sock *sp = sctp_sk(sk);
4006
4007 if (optlen < sizeof(int))
4008 return -EINVAL;
4009 if (get_user(val, (int __user *)optval))
4010 return -EFAULT;
4011 if (!sctp_is_ep_boundall(sk) && val)
4012 return -EINVAL;
4013 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
4014 return 0;
4015
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004016 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09004017 if (val == 0 && sp->do_auto_asconf) {
4018 list_del(&sp->auto_asconf_list);
4019 sp->do_auto_asconf = 0;
4020 } else if (val && !sp->do_auto_asconf) {
4021 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00004022 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09004023 sp->do_auto_asconf = 1;
4024 }
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004025 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09004026 return 0;
4027}
4028
Neil Horman5aa93bc2012-07-21 07:56:07 +00004029/*
4030 * SCTP_PEER_ADDR_THLDS
4031 *
4032 * This option allows us to alter the partially failed threshold for one or all
4033 * transports in an association. See Section 6.1 of:
4034 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
4035 */
4036static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
4037 char __user *optval,
4038 unsigned int optlen)
4039{
4040 struct sctp_paddrthlds val;
4041 struct sctp_transport *trans;
4042 struct sctp_association *asoc;
4043
4044 if (optlen < sizeof(struct sctp_paddrthlds))
4045 return -EINVAL;
4046 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
4047 sizeof(struct sctp_paddrthlds)))
4048 return -EFAULT;
4049
Xin Long8add5432019-01-28 15:08:29 +08004050 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
4051 trans = sctp_addr_id2transport(sk, &val.spt_address,
4052 val.spt_assoc_id);
4053 if (!trans)
Neil Horman5aa93bc2012-07-21 07:56:07 +00004054 return -ENOENT;
Xin Long8add5432019-01-28 15:08:29 +08004055
4056 if (val.spt_pathmaxrxt)
4057 trans->pathmaxrxt = val.spt_pathmaxrxt;
4058 trans->pf_retrans = val.spt_pathpfthld;
4059
4060 return 0;
4061 }
4062
4063 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
4064 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
4065 sctp_style(sk, UDP))
4066 return -EINVAL;
4067
4068 if (asoc) {
Neil Horman5aa93bc2012-07-21 07:56:07 +00004069 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
4070 transports) {
4071 if (val.spt_pathmaxrxt)
4072 trans->pathmaxrxt = val.spt_pathmaxrxt;
4073 trans->pf_retrans = val.spt_pathpfthld;
4074 }
4075
4076 if (val.spt_pathmaxrxt)
4077 asoc->pathmaxrxt = val.spt_pathmaxrxt;
4078 asoc->pf_retrans = val.spt_pathpfthld;
4079 } else {
Xin Long8add5432019-01-28 15:08:29 +08004080 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman5aa93bc2012-07-21 07:56:07 +00004081
4082 if (val.spt_pathmaxrxt)
Xin Long8add5432019-01-28 15:08:29 +08004083 sp->pathmaxrxt = val.spt_pathmaxrxt;
4084 sp->pf_retrans = val.spt_pathpfthld;
Neil Horman5aa93bc2012-07-21 07:56:07 +00004085 }
4086
4087 return 0;
4088}
4089
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004090static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
4091 char __user *optval,
4092 unsigned int optlen)
4093{
4094 int val;
4095
4096 if (optlen < sizeof(int))
4097 return -EINVAL;
4098 if (get_user(val, (int __user *) optval))
4099 return -EFAULT;
4100
4101 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
4102
4103 return 0;
4104}
4105
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004106static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
4107 char __user *optval,
4108 unsigned int optlen)
4109{
4110 int val;
4111
4112 if (optlen < sizeof(int))
4113 return -EINVAL;
4114 if (get_user(val, (int __user *) optval))
4115 return -EFAULT;
4116
4117 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
4118
4119 return 0;
4120}
4121
Xin Long28aa4c22016-07-09 19:47:40 +08004122static int sctp_setsockopt_pr_supported(struct sock *sk,
4123 char __user *optval,
4124 unsigned int optlen)
4125{
4126 struct sctp_assoc_value params;
Xin Longfb195602019-01-28 15:08:30 +08004127 struct sctp_association *asoc;
Xin Long28aa4c22016-07-09 19:47:40 +08004128
4129 if (optlen != sizeof(params))
Xin Longcc3ccf22018-11-18 15:21:53 +08004130 return -EINVAL;
Xin Long28aa4c22016-07-09 19:47:40 +08004131
Xin Longcc3ccf22018-11-18 15:21:53 +08004132 if (copy_from_user(&params, optval, optlen))
4133 return -EFAULT;
Xin Long28aa4c22016-07-09 19:47:40 +08004134
Xin Longfb195602019-01-28 15:08:30 +08004135 asoc = sctp_id2assoc(sk, params.assoc_id);
4136 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4137 sctp_style(sk, UDP))
4138 return -EINVAL;
4139
Xin Longcc3ccf22018-11-18 15:21:53 +08004140 sctp_sk(sk)->ep->prsctp_enable = !!params.assoc_value;
Xin Long28aa4c22016-07-09 19:47:40 +08004141
Xin Longcc3ccf22018-11-18 15:21:53 +08004142 return 0;
Xin Long28aa4c22016-07-09 19:47:40 +08004143}
4144
Xin Longf959fb42016-07-09 19:47:41 +08004145static int sctp_setsockopt_default_prinfo(struct sock *sk,
4146 char __user *optval,
4147 unsigned int optlen)
4148{
Xin Long3a583052019-01-28 15:08:43 +08004149 struct sctp_sock *sp = sctp_sk(sk);
Xin Longf959fb42016-07-09 19:47:41 +08004150 struct sctp_default_prinfo info;
4151 struct sctp_association *asoc;
4152 int retval = -EINVAL;
4153
4154 if (optlen != sizeof(info))
4155 goto out;
4156
4157 if (copy_from_user(&info, optval, sizeof(info))) {
4158 retval = -EFAULT;
4159 goto out;
4160 }
4161
4162 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
4163 goto out;
4164
4165 if (info.pr_policy == SCTP_PR_SCTP_NONE)
4166 info.pr_value = 0;
4167
4168 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
Xin Long3a583052019-01-28 15:08:43 +08004169 if (!asoc && info.pr_assoc_id > SCTP_ALL_ASSOC &&
4170 sctp_style(sk, UDP))
4171 goto out;
4172
4173 retval = 0;
4174
Xin Longf959fb42016-07-09 19:47:41 +08004175 if (asoc) {
4176 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4177 asoc->default_timetolive = info.pr_value;
Xin Longf959fb42016-07-09 19:47:41 +08004178 goto out;
4179 }
4180
Xin Long3a583052019-01-28 15:08:43 +08004181 if (info.pr_assoc_id == SCTP_FUTURE_ASSOC ||
4182 info.pr_assoc_id == SCTP_ALL_ASSOC) {
4183 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
4184 sp->default_timetolive = info.pr_value;
4185 }
4186
4187 if (info.pr_assoc_id == SCTP_CURRENT_ASSOC ||
4188 info.pr_assoc_id == SCTP_ALL_ASSOC) {
4189 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4190 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4191 asoc->default_timetolive = info.pr_value;
4192 }
4193 }
Xin Longf959fb42016-07-09 19:47:41 +08004194
4195out:
4196 return retval;
4197}
4198
Xin Longc0d8bab2017-03-10 12:11:12 +08004199static int sctp_setsockopt_reconfig_supported(struct sock *sk,
4200 char __user *optval,
4201 unsigned int optlen)
4202{
4203 struct sctp_assoc_value params;
4204 struct sctp_association *asoc;
4205 int retval = -EINVAL;
4206
4207 if (optlen != sizeof(params))
4208 goto out;
4209
4210 if (copy_from_user(&params, optval, optlen)) {
4211 retval = -EFAULT;
4212 goto out;
4213 }
4214
4215 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Longacce7f32019-01-28 15:08:31 +08004216 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4217 sctp_style(sk, UDP))
Xin Longc0d8bab2017-03-10 12:11:12 +08004218 goto out;
Xin Longacce7f32019-01-28 15:08:31 +08004219
4220 if (asoc)
4221 asoc->reconf_enable = !!params.assoc_value;
4222 else
4223 sctp_sk(sk)->ep->reconf_enable = !!params.assoc_value;
Xin Longc0d8bab2017-03-10 12:11:12 +08004224
4225 retval = 0;
4226
4227out:
4228 return retval;
4229}
4230
Xin Long9fb657a2017-01-18 00:44:46 +08004231static int sctp_setsockopt_enable_strreset(struct sock *sk,
4232 char __user *optval,
4233 unsigned int optlen)
4234{
Xin Long99a62132019-01-28 15:08:44 +08004235 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Xin Long9fb657a2017-01-18 00:44:46 +08004236 struct sctp_assoc_value params;
4237 struct sctp_association *asoc;
4238 int retval = -EINVAL;
4239
4240 if (optlen != sizeof(params))
4241 goto out;
4242
4243 if (copy_from_user(&params, optval, optlen)) {
4244 retval = -EFAULT;
4245 goto out;
4246 }
4247
4248 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
4249 goto out;
4250
4251 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Long99a62132019-01-28 15:08:44 +08004252 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4253 sctp_style(sk, UDP))
4254 goto out;
4255
4256 retval = 0;
4257
Xin Long9fb657a2017-01-18 00:44:46 +08004258 if (asoc) {
4259 asoc->strreset_enable = params.assoc_value;
Xin Long9fb657a2017-01-18 00:44:46 +08004260 goto out;
4261 }
4262
Xin Long99a62132019-01-28 15:08:44 +08004263 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4264 params.assoc_id == SCTP_ALL_ASSOC)
4265 ep->strreset_enable = params.assoc_value;
4266
4267 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4268 params.assoc_id == SCTP_ALL_ASSOC)
4269 list_for_each_entry(asoc, &ep->asocs, asocs)
4270 asoc->strreset_enable = params.assoc_value;
Xin Long9fb657a2017-01-18 00:44:46 +08004271
4272out:
4273 return retval;
4274}
4275
Xin Long7f9d68a2017-01-18 00:44:47 +08004276static int sctp_setsockopt_reset_streams(struct sock *sk,
4277 char __user *optval,
4278 unsigned int optlen)
4279{
4280 struct sctp_reset_streams *params;
4281 struct sctp_association *asoc;
4282 int retval = -EINVAL;
4283
Xin Long2342b8d2017-12-10 15:40:51 +08004284 if (optlen < sizeof(*params))
Xin Long7f9d68a2017-01-18 00:44:47 +08004285 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02004286 /* srs_number_streams is u16, so optlen can't be bigger than this. */
4287 optlen = min_t(unsigned int, optlen, USHRT_MAX +
4288 sizeof(__u16) * sizeof(*params));
Xin Long7f9d68a2017-01-18 00:44:47 +08004289
4290 params = memdup_user(optval, optlen);
4291 if (IS_ERR(params))
4292 return PTR_ERR(params);
4293
Xin Long2342b8d2017-12-10 15:40:51 +08004294 if (params->srs_number_streams * sizeof(__u16) >
4295 optlen - sizeof(*params))
4296 goto out;
4297
Xin Long7f9d68a2017-01-18 00:44:47 +08004298 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4299 if (!asoc)
4300 goto out;
4301
4302 retval = sctp_send_reset_streams(asoc, params);
4303
4304out:
4305 kfree(params);
4306 return retval;
4307}
4308
Xin Longa92ce1a2017-02-09 01:18:18 +08004309static int sctp_setsockopt_reset_assoc(struct sock *sk,
4310 char __user *optval,
4311 unsigned int optlen)
4312{
4313 struct sctp_association *asoc;
4314 sctp_assoc_t associd;
4315 int retval = -EINVAL;
4316
4317 if (optlen != sizeof(associd))
4318 goto out;
4319
4320 if (copy_from_user(&associd, optval, optlen)) {
4321 retval = -EFAULT;
4322 goto out;
4323 }
4324
4325 asoc = sctp_id2assoc(sk, associd);
4326 if (!asoc)
4327 goto out;
4328
4329 retval = sctp_send_reset_assoc(asoc);
4330
4331out:
4332 return retval;
4333}
4334
Xin Long242bd2d2017-02-09 01:18:20 +08004335static int sctp_setsockopt_add_streams(struct sock *sk,
4336 char __user *optval,
4337 unsigned int optlen)
4338{
4339 struct sctp_association *asoc;
4340 struct sctp_add_streams params;
4341 int retval = -EINVAL;
4342
4343 if (optlen != sizeof(params))
4344 goto out;
4345
4346 if (copy_from_user(&params, optval, optlen)) {
4347 retval = -EFAULT;
4348 goto out;
4349 }
4350
4351 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
4352 if (!asoc)
4353 goto out;
4354
4355 retval = sctp_send_add_streams(asoc, &params);
4356
4357out:
4358 return retval;
4359}
4360
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004361static int sctp_setsockopt_scheduler(struct sock *sk,
4362 char __user *optval,
4363 unsigned int optlen)
4364{
Xin Long7efba102019-01-28 15:08:46 +08004365 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004366 struct sctp_association *asoc;
4367 struct sctp_assoc_value params;
Xin Long7efba102019-01-28 15:08:46 +08004368 int retval = 0;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004369
4370 if (optlen < sizeof(params))
Xin Long7efba102019-01-28 15:08:46 +08004371 return -EINVAL;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004372
4373 optlen = sizeof(params);
Xin Long7efba102019-01-28 15:08:46 +08004374 if (copy_from_user(&params, optval, optlen))
4375 return -EFAULT;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004376
4377 if (params.assoc_value > SCTP_SS_MAX)
Xin Long7efba102019-01-28 15:08:46 +08004378 return -EINVAL;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004379
4380 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Long7efba102019-01-28 15:08:46 +08004381 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4382 sctp_style(sk, UDP))
4383 return -EINVAL;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004384
Xin Long7efba102019-01-28 15:08:46 +08004385 if (asoc)
4386 return sctp_sched_set_sched(asoc, params.assoc_value);
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004387
Xin Long7efba102019-01-28 15:08:46 +08004388 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4389 params.assoc_id == SCTP_ALL_ASSOC)
4390 sp->default_ss = params.assoc_value;
4391
4392 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4393 params.assoc_id == SCTP_ALL_ASSOC) {
4394 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4395 int ret = sctp_sched_set_sched(asoc,
4396 params.assoc_value);
4397
4398 if (ret && !retval)
4399 retval = ret;
4400 }
4401 }
4402
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004403 return retval;
4404}
4405
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004406static int sctp_setsockopt_scheduler_value(struct sock *sk,
4407 char __user *optval,
4408 unsigned int optlen)
4409{
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004410 struct sctp_stream_value params;
Xin Longe7f28242019-01-28 15:08:33 +08004411 struct sctp_association *asoc;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004412 int retval = -EINVAL;
4413
4414 if (optlen < sizeof(params))
4415 goto out;
4416
4417 optlen = sizeof(params);
4418 if (copy_from_user(&params, optval, optlen)) {
4419 retval = -EFAULT;
4420 goto out;
4421 }
4422
4423 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Longe7f28242019-01-28 15:08:33 +08004424 if (!asoc && params.assoc_id != SCTP_CURRENT_ASSOC &&
4425 sctp_style(sk, UDP))
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004426 goto out;
4427
Xin Longe7f28242019-01-28 15:08:33 +08004428 if (asoc) {
4429 retval = sctp_sched_set_value(asoc, params.stream_id,
4430 params.stream_value, GFP_KERNEL);
4431 goto out;
4432 }
4433
4434 retval = 0;
4435
4436 list_for_each_entry(asoc, &sctp_sk(sk)->ep->asocs, asocs) {
4437 int ret = sctp_sched_set_value(asoc, params.stream_id,
4438 params.stream_value, GFP_KERNEL);
4439 if (ret && !retval) /* try to return the 1st error. */
4440 retval = ret;
4441 }
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004442
4443out:
4444 return retval;
4445}
4446
Xin Long772a5862017-12-08 21:03:58 +08004447static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4448 char __user *optval,
4449 unsigned int optlen)
4450{
4451 struct sctp_sock *sp = sctp_sk(sk);
Xin Long772a5862017-12-08 21:03:58 +08004452 struct sctp_assoc_value params;
Xin Long2e7709d2019-01-28 15:08:32 +08004453 struct sctp_association *asoc;
Xin Long772a5862017-12-08 21:03:58 +08004454 int retval = -EINVAL;
4455
4456 if (optlen < sizeof(params))
4457 goto out;
4458
4459 optlen = sizeof(params);
4460 if (copy_from_user(&params, optval, optlen)) {
4461 retval = -EFAULT;
4462 goto out;
4463 }
4464
Xin Long2e7709d2019-01-28 15:08:32 +08004465 asoc = sctp_id2assoc(sk, params.assoc_id);
4466 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4467 sctp_style(sk, UDP))
Xin Long772a5862017-12-08 21:03:58 +08004468 goto out;
4469
Xin Long2e7709d2019-01-28 15:08:32 +08004470 if (!sock_net(sk)->sctp.intl_enable || !sp->frag_interleave) {
Xin Long772a5862017-12-08 21:03:58 +08004471 retval = -EPERM;
4472 goto out;
4473 }
4474
4475 sp->strm_interleave = !!params.assoc_value;
4476
4477 retval = 0;
4478
4479out:
4480 return retval;
4481}
4482
Xin Longb0e9a2f2018-06-28 15:31:00 +08004483static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval,
4484 unsigned int optlen)
4485{
4486 int val;
4487
4488 if (!sctp_style(sk, TCP))
4489 return -EOPNOTSUPP;
4490
4491 if (sctp_sk(sk)->ep->base.bind_addr.port)
4492 return -EFAULT;
4493
4494 if (optlen < sizeof(int))
4495 return -EINVAL;
4496
4497 if (get_user(val, (int __user *)optval))
4498 return -EFAULT;
4499
4500 sctp_sk(sk)->reuse = !!val;
4501
4502 return 0;
4503}
4504
Xin Longd251f052019-01-28 15:08:45 +08004505static int sctp_assoc_ulpevent_type_set(struct sctp_event *param,
4506 struct sctp_association *asoc)
Xin Long480ba9c2018-11-18 16:08:54 +08004507{
Xin Long480ba9c2018-11-18 16:08:54 +08004508 struct sctp_ulpevent *event;
Xin Long480ba9c2018-11-18 16:08:54 +08004509
Xin Longd251f052019-01-28 15:08:45 +08004510 sctp_ulpevent_type_set(&asoc->subscribe, param->se_type, param->se_on);
Xin Long480ba9c2018-11-18 16:08:54 +08004511
Xin Longd251f052019-01-28 15:08:45 +08004512 if (param->se_type == SCTP_SENDER_DRY_EVENT && param->se_on) {
Xin Long480ba9c2018-11-18 16:08:54 +08004513 if (sctp_outq_is_empty(&asoc->outqueue)) {
4514 event = sctp_ulpevent_make_sender_dry_event(asoc,
4515 GFP_USER | __GFP_NOWARN);
Xin Longd251f052019-01-28 15:08:45 +08004516 if (!event)
4517 return -ENOMEM;
Xin Long480ba9c2018-11-18 16:08:54 +08004518
4519 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
4520 }
4521 }
4522
Xin Longd251f052019-01-28 15:08:45 +08004523 return 0;
4524}
4525
4526static int sctp_setsockopt_event(struct sock *sk, char __user *optval,
4527 unsigned int optlen)
4528{
4529 struct sctp_sock *sp = sctp_sk(sk);
4530 struct sctp_association *asoc;
4531 struct sctp_event param;
4532 int retval = 0;
4533
4534 if (optlen < sizeof(param))
4535 return -EINVAL;
4536
4537 optlen = sizeof(param);
4538 if (copy_from_user(&param, optval, optlen))
4539 return -EFAULT;
4540
4541 if (param.se_type < SCTP_SN_TYPE_BASE ||
4542 param.se_type > SCTP_SN_TYPE_MAX)
4543 return -EINVAL;
4544
4545 asoc = sctp_id2assoc(sk, param.se_assoc_id);
4546 if (!asoc && param.se_assoc_id > SCTP_ALL_ASSOC &&
4547 sctp_style(sk, UDP))
4548 return -EINVAL;
4549
4550 if (asoc)
4551 return sctp_assoc_ulpevent_type_set(&param, asoc);
4552
4553 if (param.se_assoc_id == SCTP_FUTURE_ASSOC ||
4554 param.se_assoc_id == SCTP_ALL_ASSOC)
4555 sctp_ulpevent_type_set(&sp->subscribe,
4556 param.se_type, param.se_on);
4557
4558 if (param.se_assoc_id == SCTP_CURRENT_ASSOC ||
4559 param.se_assoc_id == SCTP_ALL_ASSOC) {
4560 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4561 int ret = sctp_assoc_ulpevent_type_set(&param, asoc);
4562
4563 if (ret && !retval)
4564 retval = ret;
4565 }
4566 }
4567
Xin Long480ba9c2018-11-18 16:08:54 +08004568 return retval;
4569}
4570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571/* API 6.2 setsockopt(), getsockopt()
4572 *
4573 * Applications use setsockopt() and getsockopt() to set or retrieve
4574 * socket options. Socket options are used to change the default
4575 * behavior of sockets calls. They are described in Section 7.
4576 *
4577 * The syntax is:
4578 *
4579 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4580 * int __user *optlen);
4581 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4582 * int optlen);
4583 *
4584 * sd - the socket descript.
4585 * level - set to IPPROTO_SCTP for all SCTP options.
4586 * optname - the option name.
4587 * optval - the buffer to store the value of the option.
4588 * optlen - the size of the buffer.
4589 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004590static int sctp_setsockopt(struct sock *sk, int level, int optname,
4591 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592{
4593 int retval = 0;
4594
Daniel Borkmannbb333812013-06-28 19:49:40 +02004595 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
4597 /* I can hardly begin to describe how wrong this is. This is
4598 * so broken as to be worse than useless. The API draft
4599 * REALLY is NOT helpful here... I am not convinced that the
4600 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4601 * are at all well-founded.
4602 */
4603 if (level != SOL_SCTP) {
4604 struct sctp_af *af = sctp_sk(sk)->pf->af;
4605 retval = af->setsockopt(sk, level, optname, optval, optlen);
4606 goto out_nounlock;
4607 }
4608
wangweidong048ed4b2014-01-21 15:44:11 +08004609 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
4611 switch (optname) {
4612 case SCTP_SOCKOPT_BINDX_ADD:
4613 /* 'optlen' is the size of the addresses buffer. */
4614 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4615 optlen, SCTP_BINDX_ADD_ADDR);
4616 break;
4617
4618 case SCTP_SOCKOPT_BINDX_REM:
4619 /* 'optlen' is the size of the addresses buffer. */
4620 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4621 optlen, SCTP_BINDX_REM_ADDR);
4622 break;
4623
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004624 case SCTP_SOCKOPT_CONNECTX_OLD:
4625 /* 'optlen' is the size of the addresses buffer. */
4626 retval = sctp_setsockopt_connectx_old(sk,
4627 (struct sockaddr __user *)optval,
4628 optlen);
4629 break;
4630
Frank Filz3f7a87d2005-06-20 13:14:57 -07004631 case SCTP_SOCKOPT_CONNECTX:
4632 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004633 retval = sctp_setsockopt_connectx(sk,
4634 (struct sockaddr __user *)optval,
4635 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004636 break;
4637
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 case SCTP_DISABLE_FRAGMENTS:
4639 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4640 break;
4641
4642 case SCTP_EVENTS:
4643 retval = sctp_setsockopt_events(sk, optval, optlen);
4644 break;
4645
4646 case SCTP_AUTOCLOSE:
4647 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4648 break;
4649
4650 case SCTP_PEER_ADDR_PARAMS:
4651 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4652 break;
4653
Shan Wei4580ccc2011-01-18 22:39:00 +00004654 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07004655 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08004656 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004657 case SCTP_PARTIAL_DELIVERY_POINT:
4658 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4659 break;
Frank Filz77086102005-12-22 11:37:30 -08004660
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 case SCTP_INITMSG:
4662 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4663 break;
4664 case SCTP_DEFAULT_SEND_PARAM:
4665 retval = sctp_setsockopt_default_send_param(sk, optval,
4666 optlen);
4667 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02004668 case SCTP_DEFAULT_SNDINFO:
4669 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4670 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 case SCTP_PRIMARY_ADDR:
4672 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4673 break;
4674 case SCTP_SET_PEER_PRIMARY_ADDR:
4675 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4676 break;
4677 case SCTP_NODELAY:
4678 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4679 break;
4680 case SCTP_RTOINFO:
4681 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4682 break;
4683 case SCTP_ASSOCINFO:
4684 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4685 break;
4686 case SCTP_I_WANT_MAPPED_V4_ADDR:
4687 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4688 break;
4689 case SCTP_MAXSEG:
4690 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4691 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004692 case SCTP_ADAPTATION_LAYER:
4693 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004695 case SCTP_CONTEXT:
4696 retval = sctp_setsockopt_context(sk, optval, optlen);
4697 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004698 case SCTP_FRAGMENT_INTERLEAVE:
4699 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4700 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07004701 case SCTP_MAX_BURST:
4702 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4703 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004704 case SCTP_AUTH_CHUNK:
4705 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4706 break;
4707 case SCTP_HMAC_IDENT:
4708 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4709 break;
4710 case SCTP_AUTH_KEY:
4711 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4712 break;
4713 case SCTP_AUTH_ACTIVE_KEY:
4714 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4715 break;
4716 case SCTP_AUTH_DELETE_KEY:
4717 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4718 break;
Xin Long601590e2018-03-14 19:05:32 +08004719 case SCTP_AUTH_DEACTIVATE_KEY:
4720 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
4721 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09004722 case SCTP_AUTO_ASCONF:
4723 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4724 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00004725 case SCTP_PEER_ADDR_THLDS:
4726 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4727 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004728 case SCTP_RECVRCVINFO:
4729 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4730 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004731 case SCTP_RECVNXTINFO:
4732 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4733 break;
Xin Long28aa4c22016-07-09 19:47:40 +08004734 case SCTP_PR_SUPPORTED:
4735 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4736 break;
Xin Longf959fb42016-07-09 19:47:41 +08004737 case SCTP_DEFAULT_PRINFO:
4738 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4739 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08004740 case SCTP_RECONFIG_SUPPORTED:
4741 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4742 break;
Xin Long9fb657a2017-01-18 00:44:46 +08004743 case SCTP_ENABLE_STREAM_RESET:
4744 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4745 break;
Xin Long7f9d68a2017-01-18 00:44:47 +08004746 case SCTP_RESET_STREAMS:
4747 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4748 break;
Xin Longa92ce1a2017-02-09 01:18:18 +08004749 case SCTP_RESET_ASSOC:
4750 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4751 break;
Xin Long242bd2d2017-02-09 01:18:20 +08004752 case SCTP_ADD_STREAMS:
4753 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4754 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004755 case SCTP_STREAM_SCHEDULER:
4756 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4757 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004758 case SCTP_STREAM_SCHEDULER_VALUE:
4759 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4760 break;
Xin Long772a5862017-12-08 21:03:58 +08004761 case SCTP_INTERLEAVING_SUPPORTED:
4762 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4763 optlen);
4764 break;
Xin Longb0e9a2f2018-06-28 15:31:00 +08004765 case SCTP_REUSE_PORT:
4766 retval = sctp_setsockopt_reuse_port(sk, optval, optlen);
4767 break;
Xin Long480ba9c2018-11-18 16:08:54 +08004768 case SCTP_EVENT:
4769 retval = sctp_setsockopt_event(sk, optval, optlen);
4770 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 default:
4772 retval = -ENOPROTOOPT;
4773 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
wangweidong048ed4b2014-01-21 15:44:11 +08004776 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
4778out_nounlock:
4779 return retval;
4780}
4781
4782/* API 3.1.6 connect() - UDP Style Syntax
4783 *
4784 * An application may use the connect() call in the UDP model to initiate an
4785 * association without sending data.
4786 *
4787 * The syntax is:
4788 *
4789 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4790 *
4791 * sd: the socket descriptor to have a new association added to.
4792 *
4793 * nam: the address structure (either struct sockaddr_in or struct
4794 * sockaddr_in6 defined in RFC2553 [7]).
4795 *
4796 * len: the size of the address.
4797 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004798static int sctp_connect(struct sock *sk, struct sockaddr *addr,
Xin Long644fbde2018-05-20 16:39:10 +08004799 int addr_len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{
Xin Long644fbde2018-05-20 16:39:10 +08004801 struct inet_sock *inet = inet_sk(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004802 struct sctp_af *af;
Xin Long644fbde2018-05-20 16:39:10 +08004803 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
wangweidong048ed4b2014-01-21 15:44:11 +08004805 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
Daniel Borkmannbb333812013-06-28 19:49:40 +02004807 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4808 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Xin Long644fbde2018-05-20 16:39:10 +08004810 /* We may need to bind the socket. */
4811 if (!inet->inet_num) {
4812 if (sk->sk_prot->get_port(sk, 0)) {
4813 release_sock(sk);
4814 return -EAGAIN;
4815 }
4816 inet->inet_sport = htons(inet->inet_num);
4817 }
4818
Frank Filz3f7a87d2005-06-20 13:14:57 -07004819 /* Validate addr_len before calling common connect/connectx routine. */
4820 af = sctp_get_af_specific(addr->sa_family);
4821 if (!af || addr_len < af->sockaddr_len) {
4822 err = -EINVAL;
4823 } else {
4824 /* Pass correct addr len to common routine (so it knows there
4825 * is only one address being passed.
4826 */
Xin Long644fbde2018-05-20 16:39:10 +08004827 err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 }
4829
wangweidong048ed4b2014-01-21 15:44:11 +08004830 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 return err;
4832}
4833
Xin Long644fbde2018-05-20 16:39:10 +08004834int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
4835 int addr_len, int flags)
4836{
4837 if (addr_len < sizeof(uaddr->sa_family))
4838 return -EINVAL;
4839
4840 if (uaddr->sa_family == AF_UNSPEC)
4841 return -EOPNOTSUPP;
4842
4843 return sctp_connect(sock->sk, uaddr, addr_len, flags);
4844}
4845
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846/* FIXME: Write comments. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004847static int sctp_disconnect(struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848{
4849 return -EOPNOTSUPP; /* STUB */
4850}
4851
4852/* 4.1.4 accept() - TCP Style Syntax
4853 *
4854 * Applications use accept() call to remove an established SCTP
4855 * association from the accept queue of the endpoint. A new socket
4856 * descriptor will be returned from accept() to represent the newly
4857 * formed association.
4858 */
David Howellscdfbabf2017-03-09 08:09:05 +00004859static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860{
4861 struct sctp_sock *sp;
4862 struct sctp_endpoint *ep;
4863 struct sock *newsk = NULL;
4864 struct sctp_association *asoc;
4865 long timeo;
4866 int error = 0;
4867
wangweidong048ed4b2014-01-21 15:44:11 +08004868 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
4870 sp = sctp_sk(sk);
4871 ep = sp->ep;
4872
4873 if (!sctp_style(sk, TCP)) {
4874 error = -EOPNOTSUPP;
4875 goto out;
4876 }
4877
4878 if (!sctp_sstate(sk, LISTENING)) {
4879 error = -EINVAL;
4880 goto out;
4881 }
4882
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07004883 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
4885 error = sctp_wait_for_accept(sk, timeo);
4886 if (error)
4887 goto out;
4888
4889 /* We treat the list of associations on the endpoint as the accept
4890 * queue and pick the first association on the list.
4891 */
4892 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4893
David Howellscdfbabf2017-03-09 08:09:05 +00004894 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 if (!newsk) {
4896 error = -ENOMEM;
4897 goto out;
4898 }
4899
4900 /* Populate the fields of the newsk from the oldsk and migrate the
4901 * asoc to the newsk.
4902 */
Xin Long89664c622019-03-03 17:54:53 +08004903 error = sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4904 if (error) {
4905 sk_common_release(newsk);
4906 newsk = NULL;
4907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
4909out:
wangweidong048ed4b2014-01-21 15:44:11 +08004910 release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004911 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 return newsk;
4913}
4914
4915/* The SCTP ioctl handler. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004916static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004918 int rc = -ENOTCONN;
4919
wangweidong048ed4b2014-01-21 15:44:11 +08004920 lock_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004921
4922 /*
4923 * SEQPACKET-style sockets in LISTENING state are valid, for
4924 * SCTP, so only discard TCP-style sockets in LISTENING state.
4925 */
4926 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4927 goto out;
4928
4929 switch (cmd) {
4930 case SIOCINQ: {
4931 struct sk_buff *skb;
4932 unsigned int amount = 0;
4933
4934 skb = skb_peek(&sk->sk_receive_queue);
4935 if (skb != NULL) {
4936 /*
4937 * We will only return the amount of this packet since
4938 * that is all that will be read.
4939 */
4940 amount = skb->len;
4941 }
4942 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004943 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07004944 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004945 default:
4946 rc = -ENOIOCTLCMD;
4947 break;
4948 }
4949out:
wangweidong048ed4b2014-01-21 15:44:11 +08004950 release_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004951 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952}
4953
4954/* This is the function which gets called during socket creation to
4955 * initialized the SCTP-specific portion of the sock.
4956 * The sock structure should already be zero-filled memory.
4957 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004958static int sctp_init_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004960 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 struct sctp_sock *sp;
4962
Daniel Borkmannbb333812013-06-28 19:49:40 +02004963 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
4965 sp = sctp_sk(sk);
4966
4967 /* Initialize the SCTP per socket area. */
4968 switch (sk->sk_type) {
4969 case SOCK_SEQPACKET:
4970 sp->type = SCTP_SOCKET_UDP;
4971 break;
4972 case SOCK_STREAM:
4973 sp->type = SCTP_SOCKET_TCP;
4974 break;
4975 default:
4976 return -ESOCKTNOSUPPORT;
4977 }
4978
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004979 sk->sk_gso_type = SKB_GSO_SCTP;
4980
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 /* Initialize default send parameters. These parameters can be
4982 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4983 */
4984 sp->default_stream = 0;
4985 sp->default_ppid = 0;
4986 sp->default_flags = 0;
4987 sp->default_context = 0;
4988 sp->default_timetolive = 0;
4989
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004990 sp->default_rcv_context = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004991 sp->max_burst = net->sctp.max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004992
Neil Horman3c681982012-10-24 09:20:03 +00004993 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4994
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 /* Initialize default setup parameters. These parameters
4996 * can be modified with the SCTP_INITMSG socket option or
4997 * overridden by the SCTP_INIT CMSG.
4998 */
4999 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
5000 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005001 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
5002 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003
5004 /* Initialize default RTO related parameters. These parameters can
5005 * be modified for with the SCTP_RTOINFO socket option.
5006 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005007 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
5008 sp->rtoinfo.srto_max = net->sctp.rto_max;
5009 sp->rtoinfo.srto_min = net->sctp.rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
5011 /* Initialize default association related parameters. These parameters
5012 * can be modified with the SCTP_ASSOCINFO socket option.
5013 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005014 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 sp->assocparams.sasoc_number_peer_destinations = 0;
5016 sp->assocparams.sasoc_peer_rwnd = 0;
5017 sp->assocparams.sasoc_local_rwnd = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005018 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
5020 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005021 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 */
Xin Long2cc0eeb2018-11-18 16:08:51 +08005023 sp->subscribe = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
5025 /* Default Peer Address Parameters. These defaults can
5026 * be modified via SCTP_PEER_ADDR_PARAMS
5027 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005028 sp->hbinterval = net->sctp.hb_interval;
5029 sp->pathmaxrxt = net->sctp.max_retrans_path;
Xin Long8add5432019-01-28 15:08:29 +08005030 sp->pf_retrans = net->sctp.pf_retrans;
wangweidong4e2d52b2013-12-23 12:16:54 +08005031 sp->pathmtu = 0; /* allow default discovery */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005032 sp->sackdelay = net->sctp.sack_timeout;
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07005033 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08005034 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005035 SPP_PMTUD_ENABLE |
5036 SPP_SACKDELAY_ENABLE;
Xin Long7efba102019-01-28 15:08:46 +08005037 sp->default_ss = SCTP_SS_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038
5039 /* If enabled no SCTP message fragmentation will be performed.
5040 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
5041 */
5042 sp->disable_fragments = 0;
5043
Sridhar Samudrala208edef2006-09-29 17:08:01 -07005044 /* Enable Nagle algorithm by default. */
5045 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02005047 sp->recvrcvinfo = 0;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02005048 sp->recvnxtinfo = 0;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02005049
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 /* Enable by default. */
5051 sp->v4mapped = 1;
5052
5053 /* Auto-close idle associations after the configured
5054 * number of seconds. A value of 0 disables this
5055 * feature. Configure through the SCTP_AUTOCLOSE socket option,
5056 * for UDP-style sockets only.
5057 */
5058 sp->autoclose = 0;
5059
5060 /* User specified fragmentation limit. */
5061 sp->user_frag = 0;
5062
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005063 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064
5065 sp->pf = sctp_get_pf_specific(sk->sk_family);
5066
5067 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005068 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005070 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071
5072 /* Create a per socket endpoint structure. Even if we
5073 * change the data structure relationships, this may still
5074 * be useful for storing pre-connect address information.
5075 */
Daniel Borkmannc164b832013-06-14 18:24:06 +02005076 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
5077 if (!sp->ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 return -ENOMEM;
5079
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 sp->hmac = NULL;
5081
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02005082 sk->sk_destruct = sctp_destruct_sock;
5083
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08005085
5086 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08005087 sk_sockets_allocated_inc(sk);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005088 sock_prot_inuse_add(net, sk->sk_prot, 1);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03005089
5090 /* Nothing can fail after this block, otherwise
5091 * sctp_destroy_sock() will be called without addr_wq_lock held
5092 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005093 if (net->sctp.default_auto_asconf) {
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03005094 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda9f7d6532011-04-26 19:32:51 +09005095 list_add_tail(&sp->auto_asconf_list,
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005096 &net->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09005097 sp->do_auto_asconf = 1;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03005098 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
5099 } else {
Michio Honda9f7d6532011-04-26 19:32:51 +09005100 sp->do_auto_asconf = 0;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03005101 }
5102
David S. Miller6f756a82008-11-23 17:34:03 -08005103 local_bh_enable();
5104
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 return 0;
5106}
5107
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03005108/* Cleanup any SCTP per socket resources. Must be called with
5109 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
5110 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02005111static void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112{
Michio Honda9f7d6532011-04-26 19:32:51 +09005113 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
Daniel Borkmannbb333812013-06-28 19:49:40 +02005115 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116
5117 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09005118 sp = sctp_sk(sk);
Daniel Borkmann1abd1652013-06-06 15:53:47 +02005119 /* This could happen during socket init, thus we bail out
5120 * early, since the rest of the below is not setup either.
5121 */
5122 if (sp->ep == NULL)
5123 return;
5124
Michio Honda9f7d6532011-04-26 19:32:51 +09005125 if (sp->do_auto_asconf) {
5126 sp->do_auto_asconf = 0;
5127 list_del(&sp->auto_asconf_list);
5128 }
5129 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08005130 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08005131 sk_sockets_allocated_dec(sk);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08005132 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08005133 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134}
5135
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02005136/* Triggered when there are no references on the socket anymore */
5137static void sctp_destruct_sock(struct sock *sk)
5138{
5139 struct sctp_sock *sp = sctp_sk(sk);
5140
5141 /* Free up the HMAC transform. */
Herbert Xu5821c762016-01-24 21:20:12 +08005142 crypto_free_shash(sp->hmac);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02005143
5144 inet_sock_destruct(sk);
5145}
5146
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147/* API 4.1.7 shutdown() - TCP Style Syntax
5148 * int shutdown(int socket, int how);
5149 *
5150 * sd - the socket descriptor of the association to be closed.
5151 * how - Specifies the type of shutdown. The values are
5152 * as follows:
5153 * SHUT_RD
5154 * Disables further receive operations. No SCTP
5155 * protocol action is taken.
5156 * SHUT_WR
5157 * Disables further send operations, and initiates
5158 * the SCTP shutdown sequence.
5159 * SHUT_RDWR
5160 * Disables further send and receive operations
5161 * and initiates the SCTP shutdown sequence.
5162 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02005163static void sctp_shutdown(struct sock *sk, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00005165 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 struct sctp_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167
5168 if (!sctp_style(sk, TCP))
5169 return;
5170
Xin Long5bf35dd2016-11-13 21:44:37 +08005171 ep = sctp_sk(sk)->ep;
5172 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
5173 struct sctp_association *asoc;
5174
Yafang Shaocbabf462017-12-20 11:12:54 +08005175 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Xin Long5bf35dd2016-11-13 21:44:37 +08005176 asoc = list_entry(ep->asocs.next,
5177 struct sctp_association, asocs);
5178 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 }
5180}
5181
Xin Long52c52a62016-04-14 15:35:30 +08005182int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
5183 struct sctp_info *info)
5184{
5185 struct sctp_transport *prim;
5186 struct list_head *pos;
5187 int mask;
5188
5189 memset(info, 0, sizeof(*info));
5190 if (!asoc) {
5191 struct sctp_sock *sp = sctp_sk(sk);
5192
5193 info->sctpi_s_autoclose = sp->autoclose;
5194 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
5195 info->sctpi_s_pd_point = sp->pd_point;
5196 info->sctpi_s_nodelay = sp->nodelay;
5197 info->sctpi_s_disable_fragments = sp->disable_fragments;
5198 info->sctpi_s_v4mapped = sp->v4mapped;
5199 info->sctpi_s_frag_interleave = sp->frag_interleave;
Xin Long40eb90e92016-05-29 17:42:13 +08005200 info->sctpi_s_type = sp->type;
Xin Long52c52a62016-04-14 15:35:30 +08005201
5202 return 0;
5203 }
5204
5205 info->sctpi_tag = asoc->c.my_vtag;
5206 info->sctpi_state = asoc->state;
5207 info->sctpi_rwnd = asoc->a_rwnd;
5208 info->sctpi_unackdata = asoc->unack_data;
5209 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08005210 info->sctpi_instrms = asoc->stream.incnt;
5211 info->sctpi_outstrms = asoc->stream.outcnt;
Xin Long52c52a62016-04-14 15:35:30 +08005212 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
5213 info->sctpi_inqueue++;
5214 list_for_each(pos, &asoc->outqueue.out_chunk_list)
5215 info->sctpi_outqueue++;
5216 info->sctpi_overall_error = asoc->overall_error_count;
5217 info->sctpi_max_burst = asoc->max_burst;
5218 info->sctpi_maxseg = asoc->frag_point;
5219 info->sctpi_peer_rwnd = asoc->peer.rwnd;
5220 info->sctpi_peer_tag = asoc->c.peer_vtag;
5221
5222 mask = asoc->peer.ecn_capable << 1;
5223 mask = (mask | asoc->peer.ipv4_address) << 1;
5224 mask = (mask | asoc->peer.ipv6_address) << 1;
5225 mask = (mask | asoc->peer.hostname_address) << 1;
5226 mask = (mask | asoc->peer.asconf_capable) << 1;
5227 mask = (mask | asoc->peer.prsctp_capable) << 1;
5228 mask = (mask | asoc->peer.auth_capable);
5229 info->sctpi_peer_capable = mask;
5230 mask = asoc->peer.sack_needed << 1;
5231 mask = (mask | asoc->peer.sack_generation) << 1;
5232 mask = (mask | asoc->peer.zero_window_announced);
5233 info->sctpi_peer_sack = mask;
5234
5235 info->sctpi_isacks = asoc->stats.isacks;
5236 info->sctpi_osacks = asoc->stats.osacks;
5237 info->sctpi_opackets = asoc->stats.opackets;
5238 info->sctpi_ipackets = asoc->stats.ipackets;
5239 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
5240 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
5241 info->sctpi_idupchunks = asoc->stats.idupchunks;
5242 info->sctpi_gapcnt = asoc->stats.gapcnt;
5243 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
5244 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
5245 info->sctpi_oodchunks = asoc->stats.oodchunks;
5246 info->sctpi_iodchunks = asoc->stats.iodchunks;
5247 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
5248 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
5249
5250 prim = asoc->peer.primary_path;
Stefano Brivioee6c88b2017-08-23 13:27:13 +02005251 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
Xin Long52c52a62016-04-14 15:35:30 +08005252 info->sctpi_p_state = prim->state;
5253 info->sctpi_p_cwnd = prim->cwnd;
5254 info->sctpi_p_srtt = prim->srtt;
5255 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
5256 info->sctpi_p_hbinterval = prim->hbinterval;
5257 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
5258 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
5259 info->sctpi_p_ssthresh = prim->ssthresh;
5260 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
5261 info->sctpi_p_flight_size = prim->flight_size;
5262 info->sctpi_p_error = prim->error_count;
5263
5264 return 0;
5265}
5266EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
5267
Xin Long626d16f2016-04-14 15:35:31 +08005268/* use callback to avoid exporting the core structure */
Tom Herbert97a6ec42017-12-04 10:31:41 -08005269void sctp_transport_walk_start(struct rhashtable_iter *iter)
Xin Long626d16f2016-04-14 15:35:31 +08005270{
Xin Long7fda7022016-11-15 23:23:11 +08005271 rhltable_walk_enter(&sctp_transport_hashtable, iter);
Xin Long626d16f2016-04-14 15:35:31 +08005272
Tom Herbert97a6ec42017-12-04 10:31:41 -08005273 rhashtable_walk_start(iter);
Xin Long626d16f2016-04-14 15:35:31 +08005274}
5275
5276void sctp_transport_walk_stop(struct rhashtable_iter *iter)
5277{
5278 rhashtable_walk_stop(iter);
5279 rhashtable_walk_exit(iter);
5280}
5281
5282struct sctp_transport *sctp_transport_get_next(struct net *net,
5283 struct rhashtable_iter *iter)
5284{
5285 struct sctp_transport *t;
5286
5287 t = rhashtable_walk_next(iter);
5288 for (; t; t = rhashtable_walk_next(iter)) {
5289 if (IS_ERR(t)) {
5290 if (PTR_ERR(t) == -EAGAIN)
5291 continue;
5292 break;
5293 }
5294
Xin Longbab1be72018-08-27 18:38:31 +08005295 if (!sctp_transport_hold(t))
5296 continue;
5297
Xin Long626d16f2016-04-14 15:35:31 +08005298 if (net_eq(sock_net(t->asoc->base.sk), net) &&
5299 t->asoc->peer.primary_path == t)
5300 break;
Xin Longbab1be72018-08-27 18:38:31 +08005301
5302 sctp_transport_put(t);
Xin Long626d16f2016-04-14 15:35:31 +08005303 }
5304
5305 return t;
5306}
5307
5308struct sctp_transport *sctp_transport_get_idx(struct net *net,
5309 struct rhashtable_iter *iter,
5310 int pos)
5311{
Xin Longbab1be72018-08-27 18:38:31 +08005312 struct sctp_transport *t;
Xin Long626d16f2016-04-14 15:35:31 +08005313
Xin Longbab1be72018-08-27 18:38:31 +08005314 if (!pos)
5315 return SEQ_START_TOKEN;
Xin Long626d16f2016-04-14 15:35:31 +08005316
Xin Longbab1be72018-08-27 18:38:31 +08005317 while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
5318 if (!--pos)
5319 break;
5320 sctp_transport_put(t);
5321 }
5322
5323 return t;
Xin Long626d16f2016-04-14 15:35:31 +08005324}
5325
5326int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
5327 void *p) {
5328 int err = 0;
5329 int hash = 0;
5330 struct sctp_ep_common *epb;
5331 struct sctp_hashbucket *head;
5332
5333 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
5334 hash++, head++) {
Xin Long581409d2017-06-10 14:48:14 +08005335 read_lock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08005336 sctp_for_each_hentry(epb, &head->chain) {
5337 err = cb(sctp_ep(epb), p);
5338 if (err)
5339 break;
5340 }
Xin Long581409d2017-06-10 14:48:14 +08005341 read_unlock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08005342 }
5343
5344 return err;
5345}
5346EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
5347
5348int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
5349 struct net *net,
5350 const union sctp_addr *laddr,
5351 const union sctp_addr *paddr, void *p)
5352{
5353 struct sctp_transport *transport;
Xin Long08abb792016-12-15 23:05:52 +08005354 int err;
Xin Long626d16f2016-04-14 15:35:31 +08005355
5356 rcu_read_lock();
5357 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
Xin Long626d16f2016-04-14 15:35:31 +08005358 rcu_read_unlock();
Xin Long08abb792016-12-15 23:05:52 +08005359 if (!transport)
5360 return -ENOENT;
5361
Xin Long1cceda782016-09-29 02:55:44 +08005362 err = cb(transport, p);
Xin Longcd26da42016-10-31 20:32:31 +08005363 sctp_transport_put(transport);
Xin Long1cceda782016-09-29 02:55:44 +08005364
Xin Long626d16f2016-04-14 15:35:31 +08005365 return err;
5366}
5367EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
5368
5369int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
Xin Longd25adbe2017-09-15 11:02:21 +08005370 int (*cb_done)(struct sctp_transport *, void *),
5371 struct net *net, int *pos, void *p) {
Xin Long626d16f2016-04-14 15:35:31 +08005372 struct rhashtable_iter hti;
Xin Longd25adbe2017-09-15 11:02:21 +08005373 struct sctp_transport *tsp;
5374 int ret;
Xin Long626d16f2016-04-14 15:35:31 +08005375
Xin Longd25adbe2017-09-15 11:02:21 +08005376again:
Xin Longf53d77e2018-01-23 18:22:25 +08005377 ret = 0;
Tom Herbert97a6ec42017-12-04 10:31:41 -08005378 sctp_transport_walk_start(&hti);
Xin Long626d16f2016-04-14 15:35:31 +08005379
Xin Longd25adbe2017-09-15 11:02:21 +08005380 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5381 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
Xin Longd25adbe2017-09-15 11:02:21 +08005382 ret = cb(tsp, p);
5383 if (ret)
Xin Long626d16f2016-04-14 15:35:31 +08005384 break;
Xin Longd25adbe2017-09-15 11:02:21 +08005385 (*pos)++;
5386 sctp_transport_put(tsp);
Xin Long626d16f2016-04-14 15:35:31 +08005387 }
Xin Long626d16f2016-04-14 15:35:31 +08005388 sctp_transport_walk_stop(&hti);
Xin Long53fa1032016-04-14 15:35:35 +08005389
Xin Longd25adbe2017-09-15 11:02:21 +08005390 if (ret) {
5391 if (cb_done && !cb_done(tsp, p)) {
5392 (*pos)++;
5393 sctp_transport_put(tsp);
5394 goto again;
5395 }
5396 sctp_transport_put(tsp);
5397 }
5398
5399 return ret;
Xin Long626d16f2016-04-14 15:35:31 +08005400}
5401EXPORT_SYMBOL_GPL(sctp_for_each_transport);
5402
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403/* 7.2.1 Association Status (SCTP_STATUS)
5404
5405 * Applications can retrieve current status information about an
5406 * association, including association state, peer receiver window size,
5407 * number of unacked data chunks, and number of data chunks pending
5408 * receipt. This information is read-only.
5409 */
5410static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5411 char __user *optval,
5412 int __user *optlen)
5413{
5414 struct sctp_status status;
5415 struct sctp_association *asoc = NULL;
5416 struct sctp_transport *transport;
5417 sctp_assoc_t associd;
5418 int retval = 0;
5419
Neil Horman408f22e2007-06-16 14:03:45 -04005420 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 retval = -EINVAL;
5422 goto out;
5423 }
5424
Neil Horman408f22e2007-06-16 14:03:45 -04005425 len = sizeof(status);
5426 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 retval = -EFAULT;
5428 goto out;
5429 }
5430
5431 associd = status.sstat_assoc_id;
5432 asoc = sctp_id2assoc(sk, associd);
5433 if (!asoc) {
5434 retval = -EINVAL;
5435 goto out;
5436 }
5437
5438 transport = asoc->peer.primary_path;
5439
5440 status.sstat_assoc_id = sctp_assoc2id(asoc);
Daniel Borkmann38ab1fa2014-08-28 15:28:26 +02005441 status.sstat_state = sctp_assoc_to_state(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 status.sstat_rwnd = asoc->peer.rwnd;
5443 status.sstat_unackdata = asoc->unack_data;
5444
5445 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08005446 status.sstat_instrms = asoc->stream.incnt;
5447 status.sstat_outstrms = asoc->stream.outcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 status.sstat_fragmentation_point = asoc->frag_point;
5449 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08005450 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5451 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 /* Map ipv4 address into v4-mapped-on-v6 address. */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005453 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005455 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 status.sstat_primary.spinfo_cwnd = transport->cwnd;
5457 status.sstat_primary.spinfo_srtt = transport->srtt;
5458 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005459 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460
Frank Filz3f7a87d2005-06-20 13:14:57 -07005461 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5462 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5463
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 if (put_user(len, optlen)) {
5465 retval = -EFAULT;
5466 goto out;
5467 }
5468
Daniel Borkmannbb333812013-06-28 19:49:40 +02005469 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5470 __func__, len, status.sstat_state, status.sstat_rwnd,
5471 status.sstat_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472
5473 if (copy_to_user(optval, &status, len)) {
5474 retval = -EFAULT;
5475 goto out;
5476 }
5477
5478out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00005479 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480}
5481
5482
5483/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5484 *
5485 * Applications can retrieve information about a specific peer address
5486 * of an association, including its reachability state, congestion
5487 * window, and retransmission timer values. This information is
5488 * read-only.
5489 */
5490static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5491 char __user *optval,
5492 int __user *optlen)
5493{
5494 struct sctp_paddrinfo pinfo;
5495 struct sctp_transport *transport;
5496 int retval = 0;
5497
Neil Horman408f22e2007-06-16 14:03:45 -04005498 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 retval = -EINVAL;
5500 goto out;
5501 }
5502
Neil Horman408f22e2007-06-16 14:03:45 -04005503 len = sizeof(pinfo);
5504 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 retval = -EFAULT;
5506 goto out;
5507 }
5508
5509 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5510 pinfo.spinfo_assoc_id);
5511 if (!transport)
5512 return -EINVAL;
5513
5514 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005515 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 pinfo.spinfo_cwnd = transport->cwnd;
5517 pinfo.spinfo_srtt = transport->srtt;
5518 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005519 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
Frank Filz3f7a87d2005-06-20 13:14:57 -07005521 if (pinfo.spinfo_state == SCTP_UNKNOWN)
5522 pinfo.spinfo_state = SCTP_ACTIVE;
5523
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 if (put_user(len, optlen)) {
5525 retval = -EFAULT;
5526 goto out;
5527 }
5528
5529 if (copy_to_user(optval, &pinfo, len)) {
5530 retval = -EFAULT;
5531 goto out;
5532 }
5533
5534out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00005535 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536}
5537
5538/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5539 *
5540 * This option is a on/off flag. If enabled no SCTP message
5541 * fragmentation will be performed. Instead if a message being sent
5542 * exceeds the current PMTU size, the message will NOT be sent and
5543 * instead a error will be indicated to the user.
5544 */
5545static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5546 char __user *optval, int __user *optlen)
5547{
5548 int val;
5549
5550 if (len < sizeof(int))
5551 return -EINVAL;
5552
5553 len = sizeof(int);
5554 val = (sctp_sk(sk)->disable_fragments == 1);
5555 if (put_user(len, optlen))
5556 return -EFAULT;
5557 if (copy_to_user(optval, &val, len))
5558 return -EFAULT;
5559 return 0;
5560}
5561
5562/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5563 *
5564 * This socket option is used to specify various notifications and
5565 * ancillary data the user wishes to receive.
5566 */
5567static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5568 int __user *optlen)
5569{
Xin Long2cc0eeb2018-11-18 16:08:51 +08005570 struct sctp_event_subscribe subscribe;
5571 __u8 *sn_type = (__u8 *)&subscribe;
5572 int i;
5573
Jiri Slabya4b8e712016-10-21 14:13:24 +02005574 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00005576 if (len > sizeof(struct sctp_event_subscribe))
5577 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04005578 if (put_user(len, optlen))
5579 return -EFAULT;
Xin Long2cc0eeb2018-11-18 16:08:51 +08005580
5581 for (i = 0; i < len; i++)
5582 sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe,
5583 SCTP_SN_TYPE_BASE + i);
5584
5585 if (copy_to_user(optval, &subscribe, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 return -EFAULT;
Xin Long2cc0eeb2018-11-18 16:08:51 +08005587
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 return 0;
5589}
5590
5591/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5592 *
5593 * This socket option is applicable to the UDP-style socket only. When
5594 * set it will cause associations that are idle for more than the
5595 * specified number of seconds to automatically close. An association
5596 * being idle is defined an association that has NOT sent or received
5597 * user data. The special value of '0' indicates that no automatic
5598 * close of any associations should be performed. The option expects an
5599 * integer defining the number of seconds of idle time before an
5600 * association is closed.
5601 */
5602static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5603{
5604 /* Applicable to UDP-style socket only */
5605 if (sctp_style(sk, TCP))
5606 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04005607 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005609 len = sizeof(int);
5610 if (put_user(len, optlen))
5611 return -EFAULT;
David Windsorb2ce04c2017-06-10 22:50:43 -04005612 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 return -EFAULT;
5614 return 0;
5615}
5616
5617/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00005618int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619{
Benjamin Poirier0343c552012-03-08 05:55:58 +00005620 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005621 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 struct socket *sock;
5623 int err = 0;
5624
Xin Longdf80cd92017-10-17 23:26:10 +08005625 /* Do not peel off from one netns to another one. */
5626 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5627 return -EINVAL;
5628
Benjamin Poirier0343c552012-03-08 05:55:58 +00005629 if (!asoc)
5630 return -EINVAL;
5631
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 /* An association cannot be branched off from an already peeled-off
5633 * socket, nor is this supported for tcp style sockets.
5634 */
5635 if (!sctp_style(sk, UDP))
5636 return -EINVAL;
5637
5638 /* Create a new socket. */
5639 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5640 if (err < 0)
5641 return err;
5642
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005643 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005644
5645 /* Make peeled-off sockets more like 1-1 accepted sockets.
Richard Hainesb7e10c22018-02-24 16:18:51 +00005646 * Set the daddr and initialize id to something more random and also
5647 * copy over any ip options.
Vlad Yasevich4f444302006-10-30 18:54:32 -08005648 */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005649 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
Richard Hainesb7e10c22018-02-24 16:18:51 +00005650 sp->pf->copy_ip_options(sk, sock->sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005651
5652 /* Populate the fields of the newsk from the oldsk and migrate the
5653 * asoc to the newsk.
5654 */
Xin Long89664c622019-03-03 17:54:53 +08005655 err = sctp_sock_migrate(sk, sock->sk, asoc,
5656 SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
5657 if (err) {
5658 sock_release(sock);
5659 sock = NULL;
5660 }
Vlad Yasevich4f444302006-10-30 18:54:32 -08005661
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 *sockp = sock;
5663
5664 return err;
5665}
Benjamin Poirier0343c552012-03-08 05:55:58 +00005666EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005668static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5669 struct file **newfile, unsigned flags)
5670{
5671 struct socket *newsock;
5672 int retval;
5673
5674 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5675 if (retval < 0)
5676 goto out;
5677
5678 /* Map the socket to an unused fd that can be returned to the user. */
5679 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5680 if (retval < 0) {
5681 sock_release(newsock);
5682 goto out;
5683 }
5684
5685 *newfile = sock_alloc_file(newsock, 0, NULL);
5686 if (IS_ERR(*newfile)) {
5687 put_unused_fd(retval);
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005688 retval = PTR_ERR(*newfile);
5689 *newfile = NULL;
5690 return retval;
5691 }
5692
5693 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5694 retval);
5695
5696 peeloff->sd = retval;
5697
5698 if (flags & SOCK_NONBLOCK)
5699 (*newfile)->f_flags |= O_NONBLOCK;
5700out:
5701 return retval;
5702}
5703
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5705{
5706 sctp_peeloff_arg_t peeloff;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005707 struct file *newfile = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
Neil Horman408f22e2007-06-16 14:03:45 -04005710 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005712 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 if (copy_from_user(&peeloff, optval, len))
5714 return -EFAULT;
5715
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005716 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 if (retval < 0)
5718 goto out;
5719
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04005721 if (put_user(len, optlen)) {
5722 fput(newfile);
5723 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04005724 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04005725 }
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005726
5727 if (copy_to_user(optval, &peeloff, len)) {
5728 fput(newfile);
5729 put_unused_fd(retval);
5730 return -EFAULT;
5731 }
5732 fd_install(retval, newfile);
5733out:
5734 return retval;
5735}
5736
5737static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5738 char __user *optval, int __user *optlen)
5739{
5740 sctp_peeloff_flags_arg_t peeloff;
5741 struct file *newfile = NULL;
5742 int retval = 0;
5743
5744 if (len < sizeof(sctp_peeloff_flags_arg_t))
5745 return -EINVAL;
5746 len = sizeof(sctp_peeloff_flags_arg_t);
5747 if (copy_from_user(&peeloff, optval, len))
5748 return -EFAULT;
5749
5750 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5751 &newfile, peeloff.flags);
5752 if (retval < 0)
5753 goto out;
5754
5755 /* Return the fd mapped to the new socket. */
5756 if (put_user(len, optlen)) {
5757 fput(newfile);
5758 put_unused_fd(retval);
5759 return -EFAULT;
5760 }
5761
Al Viro56b31d12012-08-18 00:25:51 -04005762 if (copy_to_user(optval, &peeloff, len)) {
5763 fput(newfile);
5764 put_unused_fd(retval);
5765 return -EFAULT;
5766 }
5767 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768out:
5769 return retval;
5770}
5771
5772/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5773 *
5774 * Applications can enable or disable heartbeats for any peer address of
5775 * an association, modify an address's heartbeat interval, force a
5776 * heartbeat to be sent immediately, and adjust the address's maximum
5777 * number of retransmissions sent before an address is considered
5778 * unreachable. The following structure is used to access and modify an
5779 * address's parameters:
5780 *
5781 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005782 * sctp_assoc_t spp_assoc_id;
5783 * struct sockaddr_storage spp_address;
5784 * uint32_t spp_hbinterval;
5785 * uint16_t spp_pathmaxrxt;
5786 * uint32_t spp_pathmtu;
5787 * uint32_t spp_sackdelay;
5788 * uint32_t spp_flags;
5789 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08005791 * spp_assoc_id - (one-to-many style socket) This is filled in the
5792 * application, and identifies the association for
5793 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 * spp_address - This specifies which address is of interest.
5795 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005796 * in milliseconds. If a value of zero
5797 * is present in this field then no changes are to
5798 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 * spp_pathmaxrxt - This contains the maximum number of
5800 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08005801 * considered unreachable. If a value of zero
5802 * is present in this field then no changes are to
5803 * be made to this parameter.
5804 * spp_pathmtu - When Path MTU discovery is disabled the value
5805 * specified here will be the "fixed" path mtu.
5806 * Note that if the spp_address field is empty
5807 * then all associations on this address will
5808 * have this fixed path mtu set upon them.
5809 *
5810 * spp_sackdelay - When delayed sack is enabled, this value specifies
5811 * the number of milliseconds that sacks will be delayed
5812 * for. This value will apply to all addresses of an
5813 * association if the spp_address field is empty. Note
5814 * also, that if delayed sack is enabled and this
5815 * value is set to 0, no change is made to the last
5816 * recorded delayed sack timer value.
5817 *
5818 * spp_flags - These flags are used to control various features
5819 * on an association. The flag field may contain
5820 * zero or more of the following options.
5821 *
5822 * SPP_HB_ENABLE - Enable heartbeats on the
5823 * specified address. Note that if the address
5824 * field is empty all addresses for the association
5825 * have heartbeats enabled upon them.
5826 *
5827 * SPP_HB_DISABLE - Disable heartbeats on the
5828 * speicifed address. Note that if the address
5829 * field is empty all addresses for the association
5830 * will have their heartbeats disabled. Note also
5831 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5832 * mutually exclusive, only one of these two should
5833 * be specified. Enabling both fields will have
5834 * undetermined results.
5835 *
5836 * SPP_HB_DEMAND - Request a user initiated heartbeat
5837 * to be made immediately.
5838 *
5839 * SPP_PMTUD_ENABLE - This field will enable PMTU
5840 * discovery upon the specified address. Note that
5841 * if the address feild is empty then all addresses
5842 * on the association are effected.
5843 *
5844 * SPP_PMTUD_DISABLE - This field will disable PMTU
5845 * discovery upon the specified address. Note that
5846 * if the address feild is empty then all addresses
5847 * on the association are effected. Not also that
5848 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5849 * exclusive. Enabling both will have undetermined
5850 * results.
5851 *
5852 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5853 * on delayed sack. The time specified in spp_sackdelay
5854 * is used to specify the sack delay for this address. Note
5855 * that if spp_address is empty then all addresses will
5856 * enable delayed sack and take on the sack delay
5857 * value specified in spp_sackdelay.
5858 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5859 * off delayed sack. If the spp_address field is blank then
5860 * delayed sack is disabled for the entire association. Note
5861 * also that this field is mutually exclusive to
5862 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5863 * results.
Xin Long0b0dce72018-07-02 18:21:13 +08005864 *
5865 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
5866 * setting of the IPV6 flow label value. The value is
5867 * contained in the spp_ipv6_flowlabel field.
5868 * Upon retrieval, this flag will be set to indicate that
5869 * the spp_ipv6_flowlabel field has a valid value returned.
5870 * If a specific destination address is set (in the
5871 * spp_address field), then the value returned is that of
5872 * the address. If just an association is specified (and
5873 * no address), then the association's default flow label
5874 * is returned. If neither an association nor a destination
5875 * is specified, then the socket's default flow label is
5876 * returned. For non-IPv6 sockets, this flag will be left
5877 * cleared.
5878 *
5879 * SPP_DSCP: Setting this flag enables the setting of the
5880 * Differentiated Services Code Point (DSCP) value
5881 * associated with either the association or a specific
5882 * address. The value is obtained in the spp_dscp field.
5883 * Upon retrieval, this flag will be set to indicate that
5884 * the spp_dscp field has a valid value returned. If a
5885 * specific destination address is set when called (in the
5886 * spp_address field), then that specific destination
5887 * address's DSCP value is returned. If just an association
5888 * is specified, then the association's default DSCP is
5889 * returned. If neither an association nor a destination is
5890 * specified, then the socket's default DSCP is returned.
5891 *
5892 * spp_ipv6_flowlabel
5893 * - This field is used in conjunction with the
5894 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
5895 * The 20 least significant bits are used for the flow
5896 * label. This setting has precedence over any IPv6-layer
5897 * setting.
5898 *
5899 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
5900 * and contains the DSCP. The 6 most significant bits are
5901 * used for the DSCP. This setting has precedence over any
5902 * IPv4- or IPv6- layer setting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 */
5904static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005905 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906{
Frank Filz52ccb8e2005-12-22 11:36:46 -08005907 struct sctp_paddrparams params;
5908 struct sctp_transport *trans = NULL;
5909 struct sctp_association *asoc = NULL;
5910 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911
Xin Long0b0dce72018-07-02 18:21:13 +08005912 if (len >= sizeof(params))
5913 len = sizeof(params);
5914 else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
5915 spp_ipv6_flowlabel), 4))
5916 len = ALIGN(offsetof(struct sctp_paddrparams,
5917 spp_ipv6_flowlabel), 4);
5918 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 return -EINVAL;
Xin Long0b0dce72018-07-02 18:21:13 +08005920
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 if (copy_from_user(&params, optval, len))
5922 return -EFAULT;
5923
Frank Filz52ccb8e2005-12-22 11:36:46 -08005924 /* If an address other than INADDR_ANY is specified, and
5925 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 */
wangweidongcb3f8372013-12-23 12:16:50 +08005927 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005928 trans = sctp_addr_id2transport(sk, &params.spp_address,
5929 params.spp_assoc_id);
5930 if (!trans) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005931 pr_debug("%s: failed no transport\n", __func__);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005932 return -EINVAL;
5933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 }
5935
Xin Longb99e5e02019-01-28 15:08:24 +08005936 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
5937 * socket is a one to many style socket, and an association
5938 * was not found, then the id was invalid.
Frank Filz52ccb8e2005-12-22 11:36:46 -08005939 */
5940 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
Xin Longb99e5e02019-01-28 15:08:24 +08005941 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
5942 sctp_style(sk, UDP)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005943 pr_debug("%s: failed no association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08005945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946
Frank Filz52ccb8e2005-12-22 11:36:46 -08005947 if (trans) {
5948 /* Fetch transport values. */
5949 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5950 params.spp_pathmtu = trans->pathmtu;
5951 params.spp_pathmaxrxt = trans->pathmaxrxt;
5952 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
Frank Filz52ccb8e2005-12-22 11:36:46 -08005954 /*draft-11 doesn't say what to return in spp_flags*/
5955 params.spp_flags = trans->param_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08005956 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5957 params.spp_ipv6_flowlabel = trans->flowlabel &
5958 SCTP_FLOWLABEL_VAL_MASK;
5959 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5960 }
5961 if (trans->dscp & SCTP_DSCP_SET_MASK) {
5962 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
5963 params.spp_flags |= SPP_DSCP;
5964 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08005965 } else if (asoc) {
5966 /* Fetch association values. */
5967 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5968 params.spp_pathmtu = asoc->pathmtu;
5969 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5970 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971
Frank Filz52ccb8e2005-12-22 11:36:46 -08005972 /*draft-11 doesn't say what to return in spp_flags*/
5973 params.spp_flags = asoc->param_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08005974 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5975 params.spp_ipv6_flowlabel = asoc->flowlabel &
5976 SCTP_FLOWLABEL_VAL_MASK;
5977 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5978 }
5979 if (asoc->dscp & SCTP_DSCP_SET_MASK) {
5980 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
5981 params.spp_flags |= SPP_DSCP;
5982 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08005983 } else {
5984 /* Fetch socket values. */
5985 params.spp_hbinterval = sp->hbinterval;
5986 params.spp_pathmtu = sp->pathmtu;
5987 params.spp_sackdelay = sp->sackdelay;
5988 params.spp_pathmaxrxt = sp->pathmaxrxt;
5989
5990 /*draft-11 doesn't say what to return in spp_flags*/
5991 params.spp_flags = sp->param_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08005992 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5993 params.spp_ipv6_flowlabel = sp->flowlabel &
5994 SCTP_FLOWLABEL_VAL_MASK;
5995 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5996 }
5997 if (sp->dscp & SCTP_DSCP_SET_MASK) {
5998 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
5999 params.spp_flags |= SPP_DSCP;
6000 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08006001 }
6002
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 if (copy_to_user(optval, &params, len))
6004 return -EFAULT;
6005
6006 if (put_user(len, optlen))
6007 return -EFAULT;
6008
6009 return 0;
6010}
6011
Wei Yongjund364d922008-05-09 15:13:26 -07006012/*
6013 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08006014 *
Wei Yongjund364d922008-05-09 15:13:26 -07006015 * This option will effect the way delayed acks are performed. This
6016 * option allows you to get or set the delayed ack time, in
6017 * milliseconds. It also allows changing the delayed ack frequency.
6018 * Changing the frequency to 1 disables the delayed sack algorithm. If
6019 * the assoc_id is 0, then this sets or gets the endpoints default
6020 * values. If the assoc_id field is non-zero, then the set or get
6021 * effects the specified association for the one to many model (the
6022 * assoc_id field is ignored by the one to one model). Note that if
6023 * sack_delay or sack_freq are 0 when setting this option, then the
6024 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08006025 *
Wei Yongjund364d922008-05-09 15:13:26 -07006026 * struct sctp_sack_info {
6027 * sctp_assoc_t sack_assoc_id;
6028 * uint32_t sack_delay;
6029 * uint32_t sack_freq;
6030 * };
Frank Filz77086102005-12-22 11:37:30 -08006031 *
Wei Yongjund364d922008-05-09 15:13:26 -07006032 * sack_assoc_id - This parameter, indicates which association the user
6033 * is performing an action upon. Note that if this field's value is
6034 * zero then the endpoints default value is changed (effecting future
6035 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08006036 *
Wei Yongjund364d922008-05-09 15:13:26 -07006037 * sack_delay - This parameter contains the number of milliseconds that
6038 * the user is requesting the delayed ACK timer be set to. Note that
6039 * this value is defined in the standard to be between 200 and 500
6040 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08006041 *
Wei Yongjund364d922008-05-09 15:13:26 -07006042 * sack_freq - This parameter contains the number of packets that must
6043 * be received before a sack is sent without waiting for the delay
6044 * timer to expire. The default value for this is 2, setting this
6045 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08006046 */
Wei Yongjund364d922008-05-09 15:13:26 -07006047static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08006048 char __user *optval,
6049 int __user *optlen)
6050{
Wei Yongjund364d922008-05-09 15:13:26 -07006051 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08006052 struct sctp_association *asoc = NULL;
6053 struct sctp_sock *sp = sctp_sk(sk);
6054
Wei Yongjund364d922008-05-09 15:13:26 -07006055 if (len >= sizeof(struct sctp_sack_info)) {
6056 len = sizeof(struct sctp_sack_info);
6057
6058 if (copy_from_user(&params, optval, len))
6059 return -EFAULT;
6060 } else if (len == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05006061 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006062 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006063 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006064 "Use struct sctp_sack_info instead\n",
6065 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07006066 if (copy_from_user(&params, optval, len))
6067 return -EFAULT;
6068 } else
wangweidongcb3f8372013-12-23 12:16:50 +08006069 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08006070
Xin Long9c5829e2019-01-28 15:08:34 +08006071 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
6072 * socket is a one to many style socket, and an association
6073 * was not found, then the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006074 */
Wei Yongjund364d922008-05-09 15:13:26 -07006075 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
Xin Long9c5829e2019-01-28 15:08:34 +08006076 if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC &&
6077 sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08006078 return -EINVAL;
6079
6080 if (asoc) {
6081 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07006082 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
Xin Long9c5829e2019-01-28 15:08:34 +08006083 params.sack_delay = jiffies_to_msecs(asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07006084 params.sack_freq = asoc->sackfreq;
6085
6086 } else {
6087 params.sack_delay = 0;
6088 params.sack_freq = 1;
6089 }
Frank Filz77086102005-12-22 11:37:30 -08006090 } else {
6091 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07006092 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
6093 params.sack_delay = sp->sackdelay;
6094 params.sack_freq = sp->sackfreq;
6095 } else {
6096 params.sack_delay = 0;
6097 params.sack_freq = 1;
6098 }
Frank Filz77086102005-12-22 11:37:30 -08006099 }
6100
6101 if (copy_to_user(optval, &params, len))
6102 return -EFAULT;
6103
6104 if (put_user(len, optlen))
6105 return -EFAULT;
6106
6107 return 0;
6108}
6109
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
6111 *
6112 * Applications can specify protocol parameters for the default association
6113 * initialization. The option name argument to setsockopt() and getsockopt()
6114 * is SCTP_INITMSG.
6115 *
6116 * Setting initialization parameters is effective only on an unconnected
6117 * socket (for UDP-style sockets only future associations are effected
6118 * by the change). With TCP-style sockets, this option is inherited by
6119 * sockets derived from a listener socket.
6120 */
6121static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
6122{
Neil Horman408f22e2007-06-16 14:03:45 -04006123 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04006125 len = sizeof(struct sctp_initmsg);
6126 if (put_user(len, optlen))
6127 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
6129 return -EFAULT;
6130 return 0;
6131}
6132
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006134static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
6135 char __user *optval, int __user *optlen)
6136{
6137 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006138 int cnt = 0;
6139 struct sctp_getaddrs getaddrs;
6140 struct sctp_transport *from;
6141 void __user *to;
6142 union sctp_addr temp;
6143 struct sctp_sock *sp = sctp_sk(sk);
6144 int addrlen;
6145 size_t space_left;
6146 int bytes_copied;
6147
6148 if (len < sizeof(struct sctp_getaddrs))
6149 return -EINVAL;
6150
6151 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6152 return -EFAULT;
6153
6154 /* For UDP-style sockets, id specifies the association to query. */
6155 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6156 if (!asoc)
6157 return -EINVAL;
6158
wangweidongcb3f8372013-12-23 12:16:50 +08006159 to = optval + offsetof(struct sctp_getaddrs, addrs);
6160 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006161
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07006162 list_for_each_entry(from, &asoc->peer.transport_addr_list,
6163 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08006164 memcpy(&temp, &from->ipaddr, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06006165 addrlen = sctp_get_pf_specific(sk->sk_family)
6166 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006167 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006168 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006169 if (copy_to_user(to, &temp, addrlen))
6170 return -EFAULT;
6171 to += addrlen;
6172 cnt++;
6173 space_left -= addrlen;
6174 }
6175
6176 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
6177 return -EFAULT;
6178 bytes_copied = ((char __user *)to) - optval;
6179 if (put_user(bytes_copied, optlen))
6180 return -EFAULT;
6181
6182 return 0;
6183}
6184
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006185static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
6186 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006187{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006188 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006189 union sctp_addr temp;
6190 int cnt = 0;
6191 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00006192 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006193
Vlad Yasevich29303542007-09-16 16:02:12 -07006194 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00006195 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07006196 if (!addr->valid)
6197 continue;
6198
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006199 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08006200 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006201 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07006202 if ((PF_INET6 == sk->sk_family) &&
6203 inet_v6_ipv6only(sk) &&
6204 (AF_INET == addr->a.sa.sa_family))
6205 continue;
Al Viro6244be42006-11-20 17:21:44 -08006206 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05006207 if (!temp.v4.sin_port)
6208 temp.v4.sin_port = htons(port);
6209
Jason Gunthorpe299ee122014-07-30 12:40:53 -06006210 addrlen = sctp_get_pf_specific(sk->sk_family)
6211 ->addr_to_user(sctp_sk(sk), &temp);
6212
Vlad Yasevich29303542007-09-16 16:02:12 -07006213 if (space_left < addrlen) {
6214 cnt = -ENOMEM;
6215 break;
6216 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006217 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08006218
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006219 to += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08006220 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006221 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04006222 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006223 }
Vlad Yasevich29303542007-09-16 16:02:12 -07006224 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006225
6226 return cnt;
6227}
6228
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006230static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
6231 char __user *optval, int __user *optlen)
6232{
6233 struct sctp_bind_addr *bp;
6234 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006235 int cnt = 0;
6236 struct sctp_getaddrs getaddrs;
6237 struct sctp_sockaddr_entry *addr;
6238 void __user *to;
6239 union sctp_addr temp;
6240 struct sctp_sock *sp = sctp_sk(sk);
6241 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006242 int err = 0;
6243 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006244 int bytes_copied = 0;
6245 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07006246 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006247
Neil Horman408f22e2007-06-16 14:03:45 -04006248 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006249 return -EINVAL;
6250
6251 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6252 return -EFAULT;
6253
6254 /*
6255 * For UDP-style sockets, id specifies the association to query.
6256 * If the id field is set to the value '0' then the locally bound
6257 * addresses are returned without regard to any particular
6258 * association.
6259 */
6260 if (0 == getaddrs.assoc_id) {
6261 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006262 } else {
6263 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6264 if (!asoc)
6265 return -EINVAL;
6266 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006267 }
6268
wangweidongcb3f8372013-12-23 12:16:50 +08006269 to = optval + offsetof(struct sctp_getaddrs, addrs);
6270 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Neil Horman186e2342007-06-18 19:59:16 -04006271
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02006272 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006273 if (!addrs)
6274 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006275
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006276 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
6277 * addresses from the global local address list.
6278 */
6279 if (sctp_list_single_entry(&bp->address_list)) {
6280 addr = list_entry(bp->address_list.next,
6281 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04006282 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006283 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
6284 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006285 if (cnt < 0) {
6286 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07006287 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006288 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006289 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006290 }
6291 }
6292
Vlad Yasevich70b57b82007-05-09 13:51:31 -07006293 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07006294 /* Protection on the bound address list is not needed since
6295 * in the socket option context we hold a socket lock and
6296 * thus the bound address list can't change.
6297 */
6298 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08006299 memcpy(&temp, &addr->a, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06006300 addrlen = sctp_get_pf_specific(sk->sk_family)
6301 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006302 if (space_left < addrlen) {
6303 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07006304 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006305 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07006306 memcpy(buf, &temp, addrlen);
6307 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006308 bytes_copied += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08006309 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006310 space_left -= addrlen;
6311 }
6312
6313copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006314 if (copy_to_user(to, addrs, bytes_copied)) {
6315 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02006316 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006317 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04006318 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
6319 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02006320 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04006321 }
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006322 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
6323 * but we can't change it anymore.
6324 */
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006325 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04006326 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02006327out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006328 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006329 return err;
6330}
6331
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
6333 *
6334 * Requests that the local SCTP stack use the enclosed peer address as
6335 * the association primary. The enclosed address must be one of the
6336 * association peer's addresses.
6337 */
6338static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
6339 char __user *optval, int __user *optlen)
6340{
6341 struct sctp_prim prim;
6342 struct sctp_association *asoc;
6343 struct sctp_sock *sp = sctp_sk(sk);
6344
Neil Horman408f22e2007-06-16 14:03:45 -04006345 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 return -EINVAL;
6347
Neil Horman408f22e2007-06-16 14:03:45 -04006348 len = sizeof(struct sctp_prim);
6349
6350 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 return -EFAULT;
6352
6353 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
6354 if (!asoc)
6355 return -EINVAL;
6356
6357 if (!asoc->peer.primary_path)
6358 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006359
Al Viro8cec6b82006-11-20 17:23:01 -08006360 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
6361 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362
Jason Gunthorpe299ee122014-07-30 12:40:53 -06006363 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 (union sctp_addr *)&prim.ssp_addr);
6365
Neil Horman408f22e2007-06-16 14:03:45 -04006366 if (put_user(len, optlen))
6367 return -EFAULT;
6368 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 return -EFAULT;
6370
6371 return 0;
6372}
6373
6374/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006375 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006377 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 * Indication parameter for all future INIT and INIT-ACK exchanges.
6379 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006380static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 char __user *optval, int __user *optlen)
6382{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006383 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384
Neil Horman408f22e2007-06-16 14:03:45 -04006385 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 return -EINVAL;
6387
Neil Horman408f22e2007-06-16 14:03:45 -04006388 len = sizeof(struct sctp_setadaptation);
6389
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006390 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04006391
6392 if (put_user(len, optlen))
6393 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006394 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07006396
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 return 0;
6398}
6399
6400/*
6401 *
6402 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
6403 *
6404 * Applications that wish to use the sendto() system call may wish to
6405 * specify a default set of parameters that would normally be supplied
6406 * through the inclusion of ancillary data. This socket option allows
6407 * such an application to set the default sctp_sndrcvinfo structure.
6408
6409
6410 * The application that wishes to use this socket option simply passes
6411 * in to this call the sctp_sndrcvinfo structure defined in Section
6412 * 5.2.2) The input parameters accepted by this call include
6413 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
6414 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
6415 * to this call if the caller is using the UDP model.
6416 *
6417 * For getsockopt, it get the default sctp_sndrcvinfo structure.
6418 */
6419static int sctp_getsockopt_default_send_param(struct sock *sk,
6420 int len, char __user *optval,
6421 int __user *optlen)
6422{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006424 struct sctp_association *asoc;
6425 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006427 if (len < sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04006429
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006430 len = sizeof(info);
Neil Horman408f22e2007-06-16 14:03:45 -04006431
6432 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 return -EFAULT;
6434
6435 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
Xin Long707e45b2019-01-28 15:08:35 +08006436 if (!asoc && info.sinfo_assoc_id != SCTP_FUTURE_ASSOC &&
6437 sctp_style(sk, UDP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 return -EINVAL;
Xin Long707e45b2019-01-28 15:08:35 +08006439
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 if (asoc) {
6441 info.sinfo_stream = asoc->default_stream;
6442 info.sinfo_flags = asoc->default_flags;
6443 info.sinfo_ppid = asoc->default_ppid;
6444 info.sinfo_context = asoc->default_context;
6445 info.sinfo_timetolive = asoc->default_timetolive;
6446 } else {
6447 info.sinfo_stream = sp->default_stream;
6448 info.sinfo_flags = sp->default_flags;
6449 info.sinfo_ppid = sp->default_ppid;
6450 info.sinfo_context = sp->default_context;
6451 info.sinfo_timetolive = sp->default_timetolive;
6452 }
6453
Neil Horman408f22e2007-06-16 14:03:45 -04006454 if (put_user(len, optlen))
6455 return -EFAULT;
6456 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 return -EFAULT;
6458
6459 return 0;
6460}
6461
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006462/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
6463 * (SCTP_DEFAULT_SNDINFO)
6464 */
6465static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
6466 char __user *optval,
6467 int __user *optlen)
6468{
6469 struct sctp_sock *sp = sctp_sk(sk);
6470 struct sctp_association *asoc;
6471 struct sctp_sndinfo info;
6472
6473 if (len < sizeof(info))
6474 return -EINVAL;
6475
6476 len = sizeof(info);
6477
6478 if (copy_from_user(&info, optval, len))
6479 return -EFAULT;
6480
6481 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
Xin Long92fc3bd2019-01-28 15:08:36 +08006482 if (!asoc && info.snd_assoc_id != SCTP_FUTURE_ASSOC &&
6483 sctp_style(sk, UDP))
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006484 return -EINVAL;
Xin Long92fc3bd2019-01-28 15:08:36 +08006485
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006486 if (asoc) {
6487 info.snd_sid = asoc->default_stream;
6488 info.snd_flags = asoc->default_flags;
6489 info.snd_ppid = asoc->default_ppid;
6490 info.snd_context = asoc->default_context;
6491 } else {
6492 info.snd_sid = sp->default_stream;
6493 info.snd_flags = sp->default_flags;
6494 info.snd_ppid = sp->default_ppid;
6495 info.snd_context = sp->default_context;
6496 }
6497
6498 if (put_user(len, optlen))
6499 return -EFAULT;
6500 if (copy_to_user(optval, &info, len))
6501 return -EFAULT;
6502
6503 return 0;
6504}
6505
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506/*
6507 *
6508 * 7.1.5 SCTP_NODELAY
6509 *
6510 * Turn on/off any Nagle-like algorithm. This means that packets are
6511 * generally sent as soon as possible and no unnecessary delays are
6512 * introduced, at the cost of more packets in the network. Expects an
6513 * integer boolean flag.
6514 */
6515
6516static int sctp_getsockopt_nodelay(struct sock *sk, int len,
6517 char __user *optval, int __user *optlen)
6518{
6519 int val;
6520
6521 if (len < sizeof(int))
6522 return -EINVAL;
6523
6524 len = sizeof(int);
6525 val = (sctp_sk(sk)->nodelay == 1);
6526 if (put_user(len, optlen))
6527 return -EFAULT;
6528 if (copy_to_user(optval, &val, len))
6529 return -EFAULT;
6530 return 0;
6531}
6532
6533/*
6534 *
6535 * 7.1.1 SCTP_RTOINFO
6536 *
6537 * The protocol parameters used to initialize and bound retransmission
6538 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
6539 * and modify these parameters.
6540 * All parameters are time values, in milliseconds. A value of 0, when
6541 * modifying the parameters, indicates that the current value should not
6542 * be changed.
6543 *
6544 */
6545static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6546 char __user *optval,
6547 int __user *optlen) {
6548 struct sctp_rtoinfo rtoinfo;
6549 struct sctp_association *asoc;
6550
Neil Horman408f22e2007-06-16 14:03:45 -04006551 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 return -EINVAL;
6553
Neil Horman408f22e2007-06-16 14:03:45 -04006554 len = sizeof(struct sctp_rtoinfo);
6555
6556 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 return -EFAULT;
6558
6559 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6560
Xin Long7adb5ed2019-01-28 15:08:25 +08006561 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
6562 sctp_style(sk, UDP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 return -EINVAL;
6564
6565 /* Values corresponding to the specific association. */
6566 if (asoc) {
6567 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6568 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6569 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6570 } else {
6571 /* Values corresponding to the endpoint. */
6572 struct sctp_sock *sp = sctp_sk(sk);
6573
6574 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6575 rtoinfo.srto_max = sp->rtoinfo.srto_max;
6576 rtoinfo.srto_min = sp->rtoinfo.srto_min;
6577 }
6578
6579 if (put_user(len, optlen))
6580 return -EFAULT;
6581
6582 if (copy_to_user(optval, &rtoinfo, len))
6583 return -EFAULT;
6584
6585 return 0;
6586}
6587
6588/*
6589 *
6590 * 7.1.2 SCTP_ASSOCINFO
6591 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02006592 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593 * of the association.
6594 * Returns an error if the new association retransmission value is
6595 * greater than the sum of the retransmission value of the peer.
6596 * See [SCTP] for more information.
6597 *
6598 */
6599static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6600 char __user *optval,
6601 int __user *optlen)
6602{
6603
6604 struct sctp_assocparams assocparams;
6605 struct sctp_association *asoc;
6606 struct list_head *pos;
6607 int cnt = 0;
6608
Neil Horman408f22e2007-06-16 14:03:45 -04006609 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 return -EINVAL;
6611
Neil Horman408f22e2007-06-16 14:03:45 -04006612 len = sizeof(struct sctp_assocparams);
6613
6614 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 return -EFAULT;
6616
6617 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6618
Xin Long88893942019-01-28 15:08:26 +08006619 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
6620 sctp_style(sk, UDP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621 return -EINVAL;
6622
6623 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07006624 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6626 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6627 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
Daniel Borkmann52db8822013-06-25 18:17:27 +02006628 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629
6630 list_for_each(pos, &asoc->peer.transport_addr_list) {
wangweidongcb3f8372013-12-23 12:16:50 +08006631 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 }
6633
6634 assocparams.sasoc_number_peer_destinations = cnt;
6635 } else {
6636 /* Values corresponding to the endpoint */
6637 struct sctp_sock *sp = sctp_sk(sk);
6638
6639 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6640 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6641 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6642 assocparams.sasoc_cookie_life =
6643 sp->assocparams.sasoc_cookie_life;
6644 assocparams.sasoc_number_peer_destinations =
6645 sp->assocparams.
6646 sasoc_number_peer_destinations;
6647 }
6648
6649 if (put_user(len, optlen))
6650 return -EFAULT;
6651
6652 if (copy_to_user(optval, &assocparams, len))
6653 return -EFAULT;
6654
6655 return 0;
6656}
6657
6658/*
6659 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6660 *
6661 * This socket option is a boolean flag which turns on or off mapped V4
6662 * addresses. If this option is turned on and the socket is type
6663 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6664 * If this option is turned off, then no mapping will be done of V4
6665 * addresses and a user will receive both PF_INET6 and PF_INET type
6666 * addresses on the socket.
6667 */
6668static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6669 char __user *optval, int __user *optlen)
6670{
6671 int val;
6672 struct sctp_sock *sp = sctp_sk(sk);
6673
6674 if (len < sizeof(int))
6675 return -EINVAL;
6676
6677 len = sizeof(int);
6678 val = sp->v4mapped;
6679 if (put_user(len, optlen))
6680 return -EFAULT;
6681 if (copy_to_user(optval, &val, len))
6682 return -EFAULT;
6683
6684 return 0;
6685}
6686
6687/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006688 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6689 * (chapter and verse is quoted at sctp_setsockopt_context())
6690 */
6691static int sctp_getsockopt_context(struct sock *sk, int len,
6692 char __user *optval, int __user *optlen)
6693{
6694 struct sctp_assoc_value params;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006695 struct sctp_association *asoc;
6696
Neil Horman408f22e2007-06-16 14:03:45 -04006697 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006698 return -EINVAL;
6699
Neil Horman408f22e2007-06-16 14:03:45 -04006700 len = sizeof(struct sctp_assoc_value);
6701
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006702 if (copy_from_user(&params, optval, len))
6703 return -EFAULT;
6704
Xin Long49b037a2019-01-28 15:08:37 +08006705 asoc = sctp_id2assoc(sk, params.assoc_id);
6706 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6707 sctp_style(sk, UDP))
6708 return -EINVAL;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006709
Xin Long49b037a2019-01-28 15:08:37 +08006710 params.assoc_value = asoc ? asoc->default_rcv_context
6711 : sctp_sk(sk)->default_rcv_context;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006712
6713 if (put_user(len, optlen))
6714 return -EFAULT;
6715 if (copy_to_user(optval, &params, len))
6716 return -EFAULT;
6717
6718 return 0;
6719}
6720
6721/*
Wei Yongjune89c2092008-12-25 16:54:58 -08006722 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6723 * This option will get or set the maximum size to put in any outgoing
6724 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 * fragmented by SCTP into the specified size. Note that the underlying
6726 * SCTP implementation may fragment into smaller sized chunks when the
6727 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08006728 * the user. The default value for this option is '0' which indicates
6729 * the user is NOT limiting fragmentation and only the PMTU will effect
6730 * SCTP's choice of DATA chunk size. Note also that values set larger
6731 * than the maximum size of an IP datagram will effectively let SCTP
6732 * control fragmentation (i.e. the same as setting this option to 0).
6733 *
6734 * The following structure is used to access and modify this parameter:
6735 *
6736 * struct sctp_assoc_value {
6737 * sctp_assoc_t assoc_id;
6738 * uint32_t assoc_value;
6739 * };
6740 *
6741 * assoc_id: This parameter is ignored for one-to-one style sockets.
6742 * For one-to-many style sockets this parameter indicates which
6743 * association the user is performing an action upon. Note that if
6744 * this field's value is zero then the endpoints default value is
6745 * changed (effecting future associations only).
6746 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 */
6748static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6749 char __user *optval, int __user *optlen)
6750{
Wei Yongjune89c2092008-12-25 16:54:58 -08006751 struct sctp_assoc_value params;
6752 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753
Wei Yongjune89c2092008-12-25 16:54:58 -08006754 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006755 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006756 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006757 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006758 "Use struct sctp_assoc_value instead\n",
6759 current->comm, task_pid_nr(current));
Xin Long6fd769b2019-01-28 15:08:27 +08006760 params.assoc_id = SCTP_FUTURE_ASSOC;
Wei Yongjune89c2092008-12-25 16:54:58 -08006761 } else if (len >= sizeof(struct sctp_assoc_value)) {
6762 len = sizeof(struct sctp_assoc_value);
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006763 if (copy_from_user(&params, optval, len))
Wei Yongjune89c2092008-12-25 16:54:58 -08006764 return -EFAULT;
6765 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 return -EINVAL;
6767
Wei Yongjune89c2092008-12-25 16:54:58 -08006768 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Long6fd769b2019-01-28 15:08:27 +08006769 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6770 sctp_style(sk, UDP))
Wei Yongjune89c2092008-12-25 16:54:58 -08006771 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772
Wei Yongjune89c2092008-12-25 16:54:58 -08006773 if (asoc)
6774 params.assoc_value = asoc->frag_point;
6775 else
6776 params.assoc_value = sctp_sk(sk)->user_frag;
6777
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 if (put_user(len, optlen))
6779 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08006780 if (len == sizeof(int)) {
6781 if (copy_to_user(optval, &params.assoc_value, len))
6782 return -EFAULT;
6783 } else {
6784 if (copy_to_user(optval, &params, len))
6785 return -EFAULT;
6786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787
6788 return 0;
6789}
6790
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006791/*
6792 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6793 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6794 */
6795static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6796 char __user *optval, int __user *optlen)
6797{
6798 int val;
6799
6800 if (len < sizeof(int))
6801 return -EINVAL;
6802
6803 len = sizeof(int);
6804
6805 val = sctp_sk(sk)->frag_interleave;
6806 if (put_user(len, optlen))
6807 return -EFAULT;
6808 if (copy_to_user(optval, &val, len))
6809 return -EFAULT;
6810
6811 return 0;
6812}
6813
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006814/*
6815 * 7.1.25. Set or Get the sctp partial delivery point
6816 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6817 */
6818static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6819 char __user *optval,
6820 int __user *optlen)
6821{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09006822 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006823
6824 if (len < sizeof(u32))
6825 return -EINVAL;
6826
6827 len = sizeof(u32);
6828
6829 val = sctp_sk(sk)->pd_point;
6830 if (put_user(len, optlen))
6831 return -EFAULT;
6832 if (copy_to_user(optval, &val, len))
6833 return -EFAULT;
6834
Wei Yongjun7d743b72010-12-14 16:10:41 +00006835 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006836}
6837
Vlad Yasevich70331572007-03-23 11:34:36 -07006838/*
6839 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6840 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6841 */
6842static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6843 char __user *optval,
6844 int __user *optlen)
6845{
Neil Horman219b99a2008-03-05 13:44:46 -08006846 struct sctp_assoc_value params;
Neil Horman219b99a2008-03-05 13:44:46 -08006847 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07006848
Neil Horman219b99a2008-03-05 13:44:46 -08006849 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006850 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006851 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006852 "Use of int in max_burst socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006853 "Use struct sctp_assoc_value instead\n",
6854 current->comm, task_pid_nr(current));
Xin Longe0651a02019-01-28 15:08:38 +08006855 params.assoc_id = SCTP_FUTURE_ASSOC;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00006856 } else if (len >= sizeof(struct sctp_assoc_value)) {
6857 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08006858 if (copy_from_user(&params, optval, len))
6859 return -EFAULT;
6860 } else
6861 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07006862
Xin Longe0651a02019-01-28 15:08:38 +08006863 asoc = sctp_id2assoc(sk, params.assoc_id);
6864 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6865 sctp_style(sk, UDP))
6866 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07006867
Xin Longe0651a02019-01-28 15:08:38 +08006868 params.assoc_value = asoc ? asoc->max_burst : sctp_sk(sk)->max_burst;
Neil Horman219b99a2008-03-05 13:44:46 -08006869
6870 if (len == sizeof(int)) {
6871 if (copy_to_user(optval, &params.assoc_value, len))
6872 return -EFAULT;
6873 } else {
6874 if (copy_to_user(optval, &params, len))
6875 return -EFAULT;
6876 }
6877
6878 return 0;
6879
Vlad Yasevich70331572007-03-23 11:34:36 -07006880}
6881
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006882static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6883 char __user *optval, int __user *optlen)
6884{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006885 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006886 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006887 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006888 __u16 data_len = 0;
6889 u32 num_idents;
Xin Long7a84bd42016-02-03 23:33:30 +08006890 int i;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006891
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006892 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006893 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006894
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006895 hmacs = ep->auth_hmacs_list;
Xin Long3c918702017-06-30 11:52:16 +08006896 data_len = ntohs(hmacs->param_hdr.length) -
6897 sizeof(struct sctp_paramhdr);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006898
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006899 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006900 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006901
6902 len = sizeof(struct sctp_hmacalgo) + data_len;
6903 num_idents = data_len / sizeof(u16);
6904
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006905 if (put_user(len, optlen))
6906 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006907 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006908 return -EFAULT;
Xin Long7a84bd42016-02-03 23:33:30 +08006909 for (i = 0; i < num_idents; i++) {
6910 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6911
6912 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6913 return -EFAULT;
6914 }
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006915 return 0;
6916}
6917
6918static int sctp_getsockopt_active_key(struct sock *sk, int len,
6919 char __user *optval, int __user *optlen)
6920{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006921 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006922 struct sctp_authkeyid val;
6923 struct sctp_association *asoc;
6924
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006925 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006926 return -EACCES;
6927
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006928 if (len < sizeof(struct sctp_authkeyid))
6929 return -EINVAL;
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006930
6931 len = sizeof(struct sctp_authkeyid);
6932 if (copy_from_user(&val, optval, len))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006933 return -EFAULT;
6934
6935 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6936 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6937 return -EINVAL;
6938
6939 if (asoc)
6940 val.scact_keynumber = asoc->active_key_id;
6941 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006942 val.scact_keynumber = ep->active_key_id;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006943
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006944 if (put_user(len, optlen))
6945 return -EFAULT;
6946 if (copy_to_user(optval, &val, len))
6947 return -EFAULT;
6948
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006949 return 0;
6950}
6951
6952static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6953 char __user *optval, int __user *optlen)
6954{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006955 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006956 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006957 struct sctp_authchunks val;
6958 struct sctp_association *asoc;
6959 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006960 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006961 char __user *to;
6962
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006963 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006964 return -EACCES;
6965
6966 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006967 return -EINVAL;
6968
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006969 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006970 return -EFAULT;
6971
Al Viro411223c2007-10-14 19:21:20 +01006972 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006973 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6974 if (!asoc)
6975 return -EINVAL;
6976
6977 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006978 if (!ch)
6979 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006980
6981 /* See if the user provided enough room for all the data */
Xin Long3c918702017-06-30 11:52:16 +08006982 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevichb40db682008-02-27 14:40:37 -05006983 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006984 return -EINVAL;
6985
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006986 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006987 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006988num:
6989 len = sizeof(struct sctp_authchunks) + num_chunks;
wangweidong8d726512013-12-23 12:16:53 +08006990 if (put_user(len, optlen))
6991 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006992 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6993 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006994 return 0;
6995}
6996
6997static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6998 char __user *optval, int __user *optlen)
6999{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02007000 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01007001 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007002 struct sctp_authchunks val;
7003 struct sctp_association *asoc;
7004 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07007005 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007006 char __user *to;
7007
Vlad Yasevichb14878c2014-04-17 17:26:50 +02007008 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07007009 return -EACCES;
7010
7011 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007012 return -EINVAL;
7013
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02007014 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007015 return -EFAULT;
7016
Al Viro411223c2007-10-14 19:21:20 +01007017 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007018 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
Xin Long48c07212019-01-28 15:08:28 +08007019 if (!asoc && val.gauth_assoc_id != SCTP_FUTURE_ASSOC &&
7020 sctp_style(sk, UDP))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007021 return -EINVAL;
7022
Xin Long48c07212019-01-28 15:08:28 +08007023 ch = asoc ? (struct sctp_chunks_param *)asoc->c.auth_chunks
7024 : ep->auth_chunk_list;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07007025 if (!ch)
7026 goto num;
7027
Xin Long3c918702017-06-30 11:52:16 +08007028 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07007029 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007030 return -EINVAL;
7031
Vlad Yasevich5e739d12008-08-21 03:34:25 -07007032 if (copy_to_user(to, ch->chunks, num_chunks))
7033 return -EFAULT;
7034num:
7035 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007036 if (put_user(len, optlen))
7037 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05007038 if (put_user(num_chunks, &p->gauth_number_of_chunks))
7039 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007040
7041 return 0;
7042}
7043
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08007044/*
7045 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
7046 * This option gets the current number of associations that are attached
7047 * to a one-to-many style socket. The option value is an uint32_t.
7048 */
7049static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
7050 char __user *optval, int __user *optlen)
7051{
7052 struct sctp_sock *sp = sctp_sk(sk);
7053 struct sctp_association *asoc;
7054 u32 val = 0;
7055
7056 if (sctp_style(sk, TCP))
7057 return -EOPNOTSUPP;
7058
7059 if (len < sizeof(u32))
7060 return -EINVAL;
7061
7062 len = sizeof(u32);
7063
7064 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7065 val++;
7066 }
7067
7068 if (put_user(len, optlen))
7069 return -EFAULT;
7070 if (copy_to_user(optval, &val, len))
7071 return -EFAULT;
7072
7073 return 0;
7074}
7075
Wei Yongjun209ba422011-04-17 17:27:08 +00007076/*
Michio Honda7dc04d72011-04-26 20:16:31 +09007077 * 8.1.23 SCTP_AUTO_ASCONF
7078 * See the corresponding setsockopt entry as description
7079 */
7080static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
7081 char __user *optval, int __user *optlen)
7082{
7083 int val = 0;
7084
7085 if (len < sizeof(int))
7086 return -EINVAL;
7087
7088 len = sizeof(int);
7089 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
7090 val = 1;
7091 if (put_user(len, optlen))
7092 return -EFAULT;
7093 if (copy_to_user(optval, &val, len))
7094 return -EFAULT;
7095 return 0;
7096}
7097
7098/*
Wei Yongjun209ba422011-04-17 17:27:08 +00007099 * 8.2.6. Get the Current Identifiers of Associations
7100 * (SCTP_GET_ASSOC_ID_LIST)
7101 *
7102 * This option gets the current list of SCTP association identifiers of
7103 * the SCTP associations handled by a one-to-many style socket.
7104 */
7105static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
7106 char __user *optval, int __user *optlen)
7107{
7108 struct sctp_sock *sp = sctp_sk(sk);
7109 struct sctp_association *asoc;
7110 struct sctp_assoc_ids *ids;
7111 u32 num = 0;
7112
7113 if (sctp_style(sk, TCP))
7114 return -EOPNOTSUPP;
7115
7116 if (len < sizeof(struct sctp_assoc_ids))
7117 return -EINVAL;
7118
7119 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7120 num++;
7121 }
7122
7123 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
7124 return -EINVAL;
7125
7126 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
7127
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02007128 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
Wei Yongjun209ba422011-04-17 17:27:08 +00007129 if (unlikely(!ids))
7130 return -ENOMEM;
7131
7132 ids->gaids_number_of_ids = num;
7133 num = 0;
7134 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7135 ids->gaids_assoc_id[num++] = asoc->assoc_id;
7136 }
7137
7138 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
7139 kfree(ids);
7140 return -EFAULT;
7141 }
7142
7143 kfree(ids);
7144 return 0;
7145}
7146
Neil Horman5aa93bc2012-07-21 07:56:07 +00007147/*
7148 * SCTP_PEER_ADDR_THLDS
7149 *
7150 * This option allows us to fetch the partially failed threshold for one or all
7151 * transports in an association. See Section 6.1 of:
7152 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
7153 */
7154static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
7155 char __user *optval,
7156 int len,
7157 int __user *optlen)
7158{
7159 struct sctp_paddrthlds val;
7160 struct sctp_transport *trans;
7161 struct sctp_association *asoc;
7162
7163 if (len < sizeof(struct sctp_paddrthlds))
7164 return -EINVAL;
7165 len = sizeof(struct sctp_paddrthlds);
7166 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
7167 return -EFAULT;
7168
Xin Long8add5432019-01-28 15:08:29 +08007169 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
Neil Horman5aa93bc2012-07-21 07:56:07 +00007170 trans = sctp_addr_id2transport(sk, &val.spt_address,
7171 val.spt_assoc_id);
7172 if (!trans)
7173 return -ENOENT;
7174
7175 val.spt_pathmaxrxt = trans->pathmaxrxt;
7176 val.spt_pathpfthld = trans->pf_retrans;
Xin Long8add5432019-01-28 15:08:29 +08007177
7178 return 0;
7179 }
7180
7181 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
7182 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
7183 sctp_style(sk, UDP))
7184 return -EINVAL;
7185
7186 if (asoc) {
7187 val.spt_pathpfthld = asoc->pf_retrans;
7188 val.spt_pathmaxrxt = asoc->pathmaxrxt;
7189 } else {
7190 struct sctp_sock *sp = sctp_sk(sk);
7191
7192 val.spt_pathpfthld = sp->pf_retrans;
7193 val.spt_pathmaxrxt = sp->pathmaxrxt;
Neil Horman5aa93bc2012-07-21 07:56:07 +00007194 }
7195
7196 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
7197 return -EFAULT;
7198
7199 return 0;
7200}
7201
Michele Baldessari196d6752012-12-01 04:49:42 +00007202/*
7203 * SCTP_GET_ASSOC_STATS
7204 *
7205 * This option retrieves local per endpoint statistics. It is modeled
7206 * after OpenSolaris' implementation
7207 */
7208static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
7209 char __user *optval,
7210 int __user *optlen)
7211{
7212 struct sctp_assoc_stats sas;
7213 struct sctp_association *asoc = NULL;
7214
7215 /* User must provide at least the assoc id */
7216 if (len < sizeof(sctp_assoc_t))
7217 return -EINVAL;
7218
Guenter Roeck726bc6b2013-02-27 10:57:31 +00007219 /* Allow the struct to grow and fill in as much as possible */
7220 len = min_t(size_t, len, sizeof(sas));
7221
Michele Baldessari196d6752012-12-01 04:49:42 +00007222 if (copy_from_user(&sas, optval, len))
7223 return -EFAULT;
7224
7225 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
7226 if (!asoc)
7227 return -EINVAL;
7228
7229 sas.sas_rtxchunks = asoc->stats.rtxchunks;
7230 sas.sas_gapcnt = asoc->stats.gapcnt;
7231 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
7232 sas.sas_osacks = asoc->stats.osacks;
7233 sas.sas_isacks = asoc->stats.isacks;
7234 sas.sas_octrlchunks = asoc->stats.octrlchunks;
7235 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
7236 sas.sas_oodchunks = asoc->stats.oodchunks;
7237 sas.sas_iodchunks = asoc->stats.iodchunks;
7238 sas.sas_ouodchunks = asoc->stats.ouodchunks;
7239 sas.sas_iuodchunks = asoc->stats.iuodchunks;
7240 sas.sas_idupchunks = asoc->stats.idupchunks;
7241 sas.sas_opackets = asoc->stats.opackets;
7242 sas.sas_ipackets = asoc->stats.ipackets;
7243
7244 /* New high max rto observed, will return 0 if not a single
7245 * RTO update took place. obs_rto_ipaddr will be bogus
7246 * in such a case
7247 */
7248 sas.sas_maxrto = asoc->stats.max_obs_rto;
7249 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
7250 sizeof(struct sockaddr_storage));
7251
7252 /* Mark beginning of a new observation period */
7253 asoc->stats.max_obs_rto = asoc->rto_min;
7254
Michele Baldessari196d6752012-12-01 04:49:42 +00007255 if (put_user(len, optlen))
7256 return -EFAULT;
7257
Daniel Borkmannbb333812013-06-28 19:49:40 +02007258 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
Michele Baldessari196d6752012-12-01 04:49:42 +00007259
7260 if (copy_to_user(optval, &sas, len))
7261 return -EFAULT;
7262
7263 return 0;
7264}
7265
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02007266static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
7267 char __user *optval,
7268 int __user *optlen)
7269{
7270 int val = 0;
7271
7272 if (len < sizeof(int))
7273 return -EINVAL;
7274
7275 len = sizeof(int);
7276 if (sctp_sk(sk)->recvrcvinfo)
7277 val = 1;
7278 if (put_user(len, optlen))
7279 return -EFAULT;
7280 if (copy_to_user(optval, &val, len))
7281 return -EFAULT;
7282
7283 return 0;
7284}
7285
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007286static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
7287 char __user *optval,
7288 int __user *optlen)
7289{
7290 int val = 0;
7291
7292 if (len < sizeof(int))
7293 return -EINVAL;
7294
7295 len = sizeof(int);
7296 if (sctp_sk(sk)->recvnxtinfo)
7297 val = 1;
7298 if (put_user(len, optlen))
7299 return -EFAULT;
7300 if (copy_to_user(optval, &val, len))
7301 return -EFAULT;
7302
7303 return 0;
7304}
7305
Xin Long28aa4c22016-07-09 19:47:40 +08007306static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
7307 char __user *optval,
7308 int __user *optlen)
7309{
7310 struct sctp_assoc_value params;
7311 struct sctp_association *asoc;
7312 int retval = -EFAULT;
7313
7314 if (len < sizeof(params)) {
7315 retval = -EINVAL;
7316 goto out;
7317 }
7318
7319 len = sizeof(params);
7320 if (copy_from_user(&params, optval, len))
7321 goto out;
7322
7323 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Longfb195602019-01-28 15:08:30 +08007324 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7325 sctp_style(sk, UDP)) {
Xin Long28aa4c22016-07-09 19:47:40 +08007326 retval = -EINVAL;
7327 goto out;
7328 }
7329
Xin Longfb195602019-01-28 15:08:30 +08007330 params.assoc_value = asoc ? asoc->prsctp_enable
7331 : sctp_sk(sk)->ep->prsctp_enable;
7332
Xin Long28aa4c22016-07-09 19:47:40 +08007333 if (put_user(len, optlen))
7334 goto out;
7335
7336 if (copy_to_user(optval, &params, len))
7337 goto out;
7338
7339 retval = 0;
7340
7341out:
7342 return retval;
7343}
7344
Xin Longf959fb42016-07-09 19:47:41 +08007345static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
7346 char __user *optval,
7347 int __user *optlen)
7348{
7349 struct sctp_default_prinfo info;
7350 struct sctp_association *asoc;
7351 int retval = -EFAULT;
7352
7353 if (len < sizeof(info)) {
7354 retval = -EINVAL;
7355 goto out;
7356 }
7357
7358 len = sizeof(info);
7359 if (copy_from_user(&info, optval, len))
7360 goto out;
7361
7362 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
Xin Long3a583052019-01-28 15:08:43 +08007363 if (!asoc && info.pr_assoc_id != SCTP_FUTURE_ASSOC &&
7364 sctp_style(sk, UDP)) {
7365 retval = -EINVAL;
7366 goto out;
7367 }
7368
Xin Longf959fb42016-07-09 19:47:41 +08007369 if (asoc) {
7370 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
7371 info.pr_value = asoc->default_timetolive;
Xin Long3a583052019-01-28 15:08:43 +08007372 } else {
Xin Longf959fb42016-07-09 19:47:41 +08007373 struct sctp_sock *sp = sctp_sk(sk);
7374
7375 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
7376 info.pr_value = sp->default_timetolive;
Xin Longf959fb42016-07-09 19:47:41 +08007377 }
7378
7379 if (put_user(len, optlen))
7380 goto out;
7381
7382 if (copy_to_user(optval, &info, len))
7383 goto out;
7384
7385 retval = 0;
7386
7387out:
7388 return retval;
7389}
7390
Xin Long826d2532016-07-09 19:47:42 +08007391static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
7392 char __user *optval,
7393 int __user *optlen)
7394{
7395 struct sctp_prstatus params;
7396 struct sctp_association *asoc;
7397 int policy;
7398 int retval = -EINVAL;
7399
7400 if (len < sizeof(params))
7401 goto out;
7402
7403 len = sizeof(params);
7404 if (copy_from_user(&params, optval, len)) {
7405 retval = -EFAULT;
7406 goto out;
7407 }
7408
7409 policy = params.sprstat_policy;
Xin Long71335832018-10-29 23:13:11 +08007410 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7411 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
Xin Long826d2532016-07-09 19:47:42 +08007412 goto out;
7413
7414 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7415 if (!asoc)
7416 goto out;
7417
Xin Long71335832018-10-29 23:13:11 +08007418 if (policy == SCTP_PR_SCTP_ALL) {
Xin Long826d2532016-07-09 19:47:42 +08007419 params.sprstat_abandoned_unsent = 0;
7420 params.sprstat_abandoned_sent = 0;
7421 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7422 params.sprstat_abandoned_unsent +=
7423 asoc->abandoned_unsent[policy];
7424 params.sprstat_abandoned_sent +=
7425 asoc->abandoned_sent[policy];
7426 }
7427 } else {
7428 params.sprstat_abandoned_unsent =
7429 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7430 params.sprstat_abandoned_sent =
7431 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
7432 }
7433
7434 if (put_user(len, optlen)) {
7435 retval = -EFAULT;
7436 goto out;
7437 }
7438
7439 if (copy_to_user(optval, &params, len)) {
7440 retval = -EFAULT;
7441 goto out;
7442 }
7443
7444 retval = 0;
7445
7446out:
7447 return retval;
7448}
7449
Xin Longd229d482017-04-01 17:07:46 +08007450static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
7451 char __user *optval,
7452 int __user *optlen)
7453{
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007454 struct sctp_stream_out_ext *streamoute;
Xin Longd229d482017-04-01 17:07:46 +08007455 struct sctp_association *asoc;
7456 struct sctp_prstatus params;
7457 int retval = -EINVAL;
7458 int policy;
7459
7460 if (len < sizeof(params))
7461 goto out;
7462
7463 len = sizeof(params);
7464 if (copy_from_user(&params, optval, len)) {
7465 retval = -EFAULT;
7466 goto out;
7467 }
7468
7469 policy = params.sprstat_policy;
Xin Long71335832018-10-29 23:13:11 +08007470 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7471 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
Xin Longd229d482017-04-01 17:07:46 +08007472 goto out;
7473
7474 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
Xin Longcee360a2017-05-31 16:36:31 +08007475 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
Xin Longd229d482017-04-01 17:07:46 +08007476 goto out;
7477
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03007478 streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007479 if (!streamoute) {
7480 /* Not allocated yet, means all stats are 0 */
7481 params.sprstat_abandoned_unsent = 0;
7482 params.sprstat_abandoned_sent = 0;
7483 retval = 0;
7484 goto out;
7485 }
7486
Xin Long0ac10772018-10-16 15:52:02 +08007487 if (policy == SCTP_PR_SCTP_ALL) {
Xin Longd229d482017-04-01 17:07:46 +08007488 params.sprstat_abandoned_unsent = 0;
7489 params.sprstat_abandoned_sent = 0;
7490 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7491 params.sprstat_abandoned_unsent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007492 streamoute->abandoned_unsent[policy];
Xin Longd229d482017-04-01 17:07:46 +08007493 params.sprstat_abandoned_sent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007494 streamoute->abandoned_sent[policy];
Xin Longd229d482017-04-01 17:07:46 +08007495 }
7496 } else {
7497 params.sprstat_abandoned_unsent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007498 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08007499 params.sprstat_abandoned_sent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007500 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08007501 }
7502
7503 if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
7504 retval = -EFAULT;
7505 goto out;
7506 }
7507
7508 retval = 0;
7509
7510out:
7511 return retval;
7512}
7513
Xin Longc0d8bab2017-03-10 12:11:12 +08007514static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
7515 char __user *optval,
7516 int __user *optlen)
7517{
7518 struct sctp_assoc_value params;
7519 struct sctp_association *asoc;
7520 int retval = -EFAULT;
7521
7522 if (len < sizeof(params)) {
7523 retval = -EINVAL;
7524 goto out;
7525 }
7526
7527 len = sizeof(params);
7528 if (copy_from_user(&params, optval, len))
7529 goto out;
7530
7531 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Longacce7f32019-01-28 15:08:31 +08007532 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7533 sctp_style(sk, UDP)) {
Xin Longc0d8bab2017-03-10 12:11:12 +08007534 retval = -EINVAL;
7535 goto out;
7536 }
7537
Xin Longacce7f32019-01-28 15:08:31 +08007538 params.assoc_value = asoc ? asoc->reconf_enable
7539 : sctp_sk(sk)->ep->reconf_enable;
7540
Xin Longc0d8bab2017-03-10 12:11:12 +08007541 if (put_user(len, optlen))
7542 goto out;
7543
7544 if (copy_to_user(optval, &params, len))
7545 goto out;
7546
7547 retval = 0;
7548
7549out:
7550 return retval;
7551}
7552
Xin Long9fb657a2017-01-18 00:44:46 +08007553static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7554 char __user *optval,
7555 int __user *optlen)
7556{
7557 struct sctp_assoc_value params;
7558 struct sctp_association *asoc;
7559 int retval = -EFAULT;
7560
7561 if (len < sizeof(params)) {
7562 retval = -EINVAL;
7563 goto out;
7564 }
7565
7566 len = sizeof(params);
7567 if (copy_from_user(&params, optval, len))
7568 goto out;
7569
7570 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Long99a62132019-01-28 15:08:44 +08007571 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7572 sctp_style(sk, UDP)) {
Xin Long9fb657a2017-01-18 00:44:46 +08007573 retval = -EINVAL;
7574 goto out;
7575 }
7576
Xin Long99a62132019-01-28 15:08:44 +08007577 params.assoc_value = asoc ? asoc->strreset_enable
7578 : sctp_sk(sk)->ep->strreset_enable;
7579
Xin Long9fb657a2017-01-18 00:44:46 +08007580 if (put_user(len, optlen))
7581 goto out;
7582
7583 if (copy_to_user(optval, &params, len))
7584 goto out;
7585
7586 retval = 0;
7587
7588out:
7589 return retval;
7590}
7591
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007592static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7593 char __user *optval,
7594 int __user *optlen)
7595{
7596 struct sctp_assoc_value params;
7597 struct sctp_association *asoc;
7598 int retval = -EFAULT;
7599
7600 if (len < sizeof(params)) {
7601 retval = -EINVAL;
7602 goto out;
7603 }
7604
7605 len = sizeof(params);
7606 if (copy_from_user(&params, optval, len))
7607 goto out;
7608
7609 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Long7efba102019-01-28 15:08:46 +08007610 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7611 sctp_style(sk, UDP)) {
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007612 retval = -EINVAL;
7613 goto out;
7614 }
7615
Xin Long7efba102019-01-28 15:08:46 +08007616 params.assoc_value = asoc ? sctp_sched_get_sched(asoc)
7617 : sctp_sk(sk)->default_ss;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007618
7619 if (put_user(len, optlen))
7620 goto out;
7621
7622 if (copy_to_user(optval, &params, len))
7623 goto out;
7624
7625 retval = 0;
7626
7627out:
7628 return retval;
7629}
7630
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007631static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7632 char __user *optval,
7633 int __user *optlen)
7634{
7635 struct sctp_stream_value params;
7636 struct sctp_association *asoc;
7637 int retval = -EFAULT;
7638
7639 if (len < sizeof(params)) {
7640 retval = -EINVAL;
7641 goto out;
7642 }
7643
7644 len = sizeof(params);
7645 if (copy_from_user(&params, optval, len))
7646 goto out;
7647
7648 asoc = sctp_id2assoc(sk, params.assoc_id);
7649 if (!asoc) {
7650 retval = -EINVAL;
7651 goto out;
7652 }
7653
7654 retval = sctp_sched_get_value(asoc, params.stream_id,
7655 &params.stream_value);
7656 if (retval)
7657 goto out;
7658
7659 if (put_user(len, optlen)) {
7660 retval = -EFAULT;
7661 goto out;
7662 }
7663
7664 if (copy_to_user(optval, &params, len)) {
7665 retval = -EFAULT;
7666 goto out;
7667 }
7668
7669out:
7670 return retval;
7671}
7672
Xin Long772a5862017-12-08 21:03:58 +08007673static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7674 char __user *optval,
7675 int __user *optlen)
7676{
7677 struct sctp_assoc_value params;
7678 struct sctp_association *asoc;
7679 int retval = -EFAULT;
7680
7681 if (len < sizeof(params)) {
7682 retval = -EINVAL;
7683 goto out;
7684 }
7685
7686 len = sizeof(params);
7687 if (copy_from_user(&params, optval, len))
7688 goto out;
7689
7690 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Long2e7709d2019-01-28 15:08:32 +08007691 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7692 sctp_style(sk, UDP)) {
Xin Long772a5862017-12-08 21:03:58 +08007693 retval = -EINVAL;
7694 goto out;
7695 }
7696
Xin Long2e7709d2019-01-28 15:08:32 +08007697 params.assoc_value = asoc ? asoc->intl_enable
7698 : sctp_sk(sk)->strm_interleave;
7699
Xin Long772a5862017-12-08 21:03:58 +08007700 if (put_user(len, optlen))
7701 goto out;
7702
7703 if (copy_to_user(optval, &params, len))
7704 goto out;
7705
7706 retval = 0;
7707
7708out:
7709 return retval;
7710}
7711
Xin Longb0e9a2f2018-06-28 15:31:00 +08007712static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
7713 char __user *optval,
7714 int __user *optlen)
7715{
7716 int val;
7717
7718 if (len < sizeof(int))
7719 return -EINVAL;
7720
7721 len = sizeof(int);
7722 val = sctp_sk(sk)->reuse;
7723 if (put_user(len, optlen))
7724 return -EFAULT;
7725
7726 if (copy_to_user(optval, &val, len))
7727 return -EFAULT;
7728
7729 return 0;
7730}
7731
Xin Long480ba9c2018-11-18 16:08:54 +08007732static int sctp_getsockopt_event(struct sock *sk, int len, char __user *optval,
7733 int __user *optlen)
7734{
7735 struct sctp_association *asoc;
7736 struct sctp_event param;
7737 __u16 subscribe;
7738
7739 if (len < sizeof(param))
7740 return -EINVAL;
7741
7742 len = sizeof(param);
7743 if (copy_from_user(&param, optval, len))
7744 return -EFAULT;
7745
7746 if (param.se_type < SCTP_SN_TYPE_BASE ||
7747 param.se_type > SCTP_SN_TYPE_MAX)
7748 return -EINVAL;
7749
7750 asoc = sctp_id2assoc(sk, param.se_assoc_id);
Xin Longd251f052019-01-28 15:08:45 +08007751 if (!asoc && param.se_assoc_id != SCTP_FUTURE_ASSOC &&
7752 sctp_style(sk, UDP))
7753 return -EINVAL;
7754
Xin Long480ba9c2018-11-18 16:08:54 +08007755 subscribe = asoc ? asoc->subscribe : sctp_sk(sk)->subscribe;
7756 param.se_on = sctp_ulpevent_type_enabled(subscribe, param.se_type);
7757
7758 if (put_user(len, optlen))
7759 return -EFAULT;
7760
7761 if (copy_to_user(optval, &param, len))
7762 return -EFAULT;
7763
7764 return 0;
7765}
7766
Daniel Borkmanndda91922013-06-17 11:40:05 +02007767static int sctp_getsockopt(struct sock *sk, int level, int optname,
7768 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769{
7770 int retval = 0;
7771 int len;
7772
Daniel Borkmannbb333812013-06-28 19:49:40 +02007773 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774
7775 /* I can hardly begin to describe how wrong this is. This is
7776 * so broken as to be worse than useless. The API draft
7777 * REALLY is NOT helpful here... I am not convinced that the
7778 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7779 * are at all well-founded.
7780 */
7781 if (level != SOL_SCTP) {
7782 struct sctp_af *af = sctp_sk(sk)->pf->af;
7783
7784 retval = af->getsockopt(sk, level, optname, optval, optlen);
7785 return retval;
7786 }
7787
7788 if (get_user(len, optlen))
7789 return -EFAULT;
7790
Jiri Slabya4b8e712016-10-21 14:13:24 +02007791 if (len < 0)
7792 return -EINVAL;
7793
wangweidong048ed4b2014-01-21 15:44:11 +08007794 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795
7796 switch (optname) {
7797 case SCTP_STATUS:
7798 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7799 break;
7800 case SCTP_DISABLE_FRAGMENTS:
7801 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7802 optlen);
7803 break;
7804 case SCTP_EVENTS:
7805 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7806 break;
7807 case SCTP_AUTOCLOSE:
7808 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7809 break;
7810 case SCTP_SOCKOPT_PEELOFF:
7811 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7812 break;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04007813 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7814 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7815 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 case SCTP_PEER_ADDR_PARAMS:
7817 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7818 optlen);
7819 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00007820 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07007821 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08007822 optlen);
7823 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824 case SCTP_INITMSG:
7825 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7826 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827 case SCTP_GET_PEER_ADDRS:
7828 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7829 optlen);
7830 break;
7831 case SCTP_GET_LOCAL_ADDRS:
7832 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7833 optlen);
7834 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04007835 case SCTP_SOCKOPT_CONNECTX3:
7836 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7837 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838 case SCTP_DEFAULT_SEND_PARAM:
7839 retval = sctp_getsockopt_default_send_param(sk, len,
7840 optval, optlen);
7841 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02007842 case SCTP_DEFAULT_SNDINFO:
7843 retval = sctp_getsockopt_default_sndinfo(sk, len,
7844 optval, optlen);
7845 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007846 case SCTP_PRIMARY_ADDR:
7847 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7848 break;
7849 case SCTP_NODELAY:
7850 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7851 break;
7852 case SCTP_RTOINFO:
7853 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7854 break;
7855 case SCTP_ASSOCINFO:
7856 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7857 break;
7858 case SCTP_I_WANT_MAPPED_V4_ADDR:
7859 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7860 break;
7861 case SCTP_MAXSEG:
7862 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7863 break;
7864 case SCTP_GET_PEER_ADDR_INFO:
7865 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7866 optlen);
7867 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08007868 case SCTP_ADAPTATION_LAYER:
7869 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870 optlen);
7871 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08007872 case SCTP_CONTEXT:
7873 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7874 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007875 case SCTP_FRAGMENT_INTERLEAVE:
7876 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7877 optlen);
7878 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07007879 case SCTP_PARTIAL_DELIVERY_POINT:
7880 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7881 optlen);
7882 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07007883 case SCTP_MAX_BURST:
7884 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7885 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007886 case SCTP_AUTH_KEY:
7887 case SCTP_AUTH_CHUNK:
7888 case SCTP_AUTH_DELETE_KEY:
Xin Long601590e2018-03-14 19:05:32 +08007889 case SCTP_AUTH_DEACTIVATE_KEY:
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007890 retval = -EOPNOTSUPP;
7891 break;
7892 case SCTP_HMAC_IDENT:
7893 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7894 break;
7895 case SCTP_AUTH_ACTIVE_KEY:
7896 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7897 break;
7898 case SCTP_PEER_AUTH_CHUNKS:
7899 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7900 optlen);
7901 break;
7902 case SCTP_LOCAL_AUTH_CHUNKS:
7903 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7904 optlen);
7905 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08007906 case SCTP_GET_ASSOC_NUMBER:
7907 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7908 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00007909 case SCTP_GET_ASSOC_ID_LIST:
7910 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7911 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09007912 case SCTP_AUTO_ASCONF:
7913 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7914 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00007915 case SCTP_PEER_ADDR_THLDS:
7916 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7917 break;
Michele Baldessari196d6752012-12-01 04:49:42 +00007918 case SCTP_GET_ASSOC_STATS:
7919 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7920 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02007921 case SCTP_RECVRCVINFO:
7922 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7923 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007924 case SCTP_RECVNXTINFO:
7925 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7926 break;
Xin Long28aa4c22016-07-09 19:47:40 +08007927 case SCTP_PR_SUPPORTED:
7928 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7929 break;
Xin Longf959fb42016-07-09 19:47:41 +08007930 case SCTP_DEFAULT_PRINFO:
7931 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7932 optlen);
7933 break;
Xin Long826d2532016-07-09 19:47:42 +08007934 case SCTP_PR_ASSOC_STATUS:
7935 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7936 optlen);
7937 break;
Xin Longd229d482017-04-01 17:07:46 +08007938 case SCTP_PR_STREAM_STATUS:
7939 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7940 optlen);
7941 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08007942 case SCTP_RECONFIG_SUPPORTED:
7943 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7944 optlen);
7945 break;
Xin Long9fb657a2017-01-18 00:44:46 +08007946 case SCTP_ENABLE_STREAM_RESET:
7947 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7948 optlen);
7949 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007950 case SCTP_STREAM_SCHEDULER:
7951 retval = sctp_getsockopt_scheduler(sk, len, optval,
7952 optlen);
7953 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007954 case SCTP_STREAM_SCHEDULER_VALUE:
7955 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7956 optlen);
7957 break;
Xin Long772a5862017-12-08 21:03:58 +08007958 case SCTP_INTERLEAVING_SUPPORTED:
7959 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7960 optlen);
7961 break;
Xin Longb0e9a2f2018-06-28 15:31:00 +08007962 case SCTP_REUSE_PORT:
7963 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
7964 break;
Xin Long480ba9c2018-11-18 16:08:54 +08007965 case SCTP_EVENT:
7966 retval = sctp_getsockopt_event(sk, len, optval, optlen);
7967 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007968 default:
7969 retval = -ENOPROTOOPT;
7970 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972
wangweidong048ed4b2014-01-21 15:44:11 +08007973 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007974 return retval;
7975}
7976
Craig Gallek086c6532016-02-10 11:50:35 -05007977static int sctp_hash(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007978{
7979 /* STUB */
Craig Gallek086c6532016-02-10 11:50:35 -05007980 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007981}
7982
7983static void sctp_unhash(struct sock *sk)
7984{
7985 /* STUB */
7986}
7987
7988/* Check if port is acceptable. Possibly find first available port.
7989 *
7990 * The port hash table (contained in the 'global' SCTP protocol storage
7991 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7992 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7993 * list (the list number is the port number hashed out, so as you
7994 * would expect from a hash function, all the ports in a given list have
7995 * such a number that hashes out to the same list number; you were
7996 * expecting that, right?); so each list has a set of ports, with a
7997 * link to the socket (struct sock) that uses it, the port number and
7998 * a fastreuse flag (FIXME: NPI ipg).
7999 */
8000static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008001 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008002
8003static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
8004{
Xin Long6ba84572018-11-12 18:27:17 +08008005 struct sctp_sock *sp = sctp_sk(sk);
8006 bool reuse = (sk->sk_reuse || sp->reuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007 struct sctp_bind_hashbucket *head; /* hash list */
Xin Long6ba84572018-11-12 18:27:17 +08008008 kuid_t uid = sock_i_uid(sk);
Sasha Levinb67bfe02013-02-27 17:06:00 -08008009 struct sctp_bind_bucket *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008010 unsigned short snum;
8011 int ret;
8012
Al Viro04afd8b2006-11-20 17:02:01 -08008013 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008014
Daniel Borkmannbb333812013-06-28 19:49:40 +02008015 pr_debug("%s: begins, snum:%d\n", __func__, snum);
8016
wangweidong79b91132014-01-21 15:44:07 +08008017 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008018
8019 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07008020 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07008021 int low, high, remaining, index;
8022 unsigned int rover;
WANG Cong122ff242014-05-12 16:04:53 -07008023 struct net *net = sock_net(sk);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07008024
WANG Cong122ff242014-05-12 16:04:53 -07008025 inet_get_local_port_range(net, &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07008026 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05008027 rover = prandom_u32() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029 do {
8030 rover++;
8031 if ((rover < low) || (rover > high))
8032 rover = low;
WANG Cong122ff242014-05-12 16:04:53 -07008033 if (inet_is_local_reserved_port(net, rover))
Amerigo Wange3826f12010-05-05 00:27:06 +00008034 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008035 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008036 head = &sctp_port_hashtable[index];
wangweidong3c8e43b2014-01-21 15:44:08 +08008037 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08008038 sctp_for_each_hentry(pp, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008039 if ((pp->port == rover) &&
8040 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008041 goto next;
8042 break;
8043 next:
wangweidong3c8e43b2014-01-21 15:44:08 +08008044 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008046
8047 /* Exhausted local port range during search? */
8048 ret = 1;
8049 if (remaining <= 0)
8050 goto fail;
8051
8052 /* OK, here is the one we will use. HEAD (the port
8053 * hash table list entry) is non-NULL and we hold it's
8054 * mutex.
8055 */
8056 snum = rover;
8057 } else {
8058 /* We are given an specific port number; we verify
8059 * that it is not being used. If it is used, we will
8060 * exahust the search in the hash list corresponding
8061 * to the port number (snum) - we detect that with the
8062 * port iterator, pp being NULL.
8063 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008064 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
wangweidong3c8e43b2014-01-21 15:44:08 +08008065 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08008066 sctp_for_each_hentry(pp, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008067 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068 goto pp_found;
8069 }
8070 }
8071 pp = NULL;
8072 goto pp_not_found;
8073pp_found:
8074 if (!hlist_empty(&pp->owner)) {
8075 /* We had a port hash table hit - there is an
8076 * available port (pp != NULL) and it is being
8077 * used by other socket (pp->owner not empty); that other
8078 * socket is going to be sk2.
8079 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008080 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081
Daniel Borkmannbb333812013-06-28 19:49:40 +02008082 pr_debug("%s: found a possible match\n", __func__);
8083
Xin Long6ba84572018-11-12 18:27:17 +08008084 if ((pp->fastreuse && reuse &&
8085 sk->sk_state != SCTP_SS_LISTENING) ||
8086 (pp->fastreuseport && sk->sk_reuseport &&
8087 uid_eq(pp->fastuid, uid)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088 goto success;
8089
8090 /* Run through the list of sockets bound to the port
8091 * (pp->port) [via the pointers bind_next and
8092 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
8093 * we get the endpoint they describe and run through
8094 * the endpoint's list of IP (v4 or v6) addresses,
8095 * comparing each of the addresses with the address of
8096 * the socket sk. If we find a match, then that means
8097 * that this port/socket (sk) combination are already
8098 * in an endpoint.
8099 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08008100 sk_for_each_bound(sk2, &pp->owner) {
Xin Long6ba84572018-11-12 18:27:17 +08008101 struct sctp_sock *sp2 = sctp_sk(sk2);
8102 struct sctp_endpoint *ep2 = sp2->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008103
Vlad Yasevich4e540642008-07-18 23:06:32 -07008104 if (sk == sk2 ||
Xin Long6ba84572018-11-12 18:27:17 +08008105 (reuse && (sk2->sk_reuse || sp2->reuse) &&
8106 sk2->sk_state != SCTP_SS_LISTENING) ||
8107 (sk->sk_reuseport && sk2->sk_reuseport &&
8108 uid_eq(uid, sock_i_uid(sk2))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109 continue;
8110
Xin Long6ba84572018-11-12 18:27:17 +08008111 if (sctp_bind_addr_conflict(&ep2->base.bind_addr,
8112 addr, sp2, sp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008113 ret = (long)sk2;
8114 goto fail_unlock;
8115 }
8116 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02008117
8118 pr_debug("%s: found a match\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008119 }
8120pp_not_found:
8121 /* If there was a hash table miss, create a new port. */
8122 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008123 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124 goto fail_unlock;
8125
8126 /* In either case (hit or miss), make sure fastreuse is 1 only
8127 * if sk->sk_reuse is too (that is, if the caller requested
8128 * SO_REUSEADDR on this socket -sk-).
8129 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07008130 if (hlist_empty(&pp->owner)) {
Xin Longb0e9a2f2018-06-28 15:31:00 +08008131 if (reuse && sk->sk_state != SCTP_SS_LISTENING)
Vlad Yasevichce5325c2007-05-04 13:34:49 -07008132 pp->fastreuse = 1;
8133 else
8134 pp->fastreuse = 0;
Xin Long6ba84572018-11-12 18:27:17 +08008135
8136 if (sk->sk_reuseport) {
8137 pp->fastreuseport = 1;
8138 pp->fastuid = uid;
8139 } else {
8140 pp->fastreuseport = 0;
8141 }
8142 } else {
8143 if (pp->fastreuse &&
8144 (!reuse || sk->sk_state == SCTP_SS_LISTENING))
8145 pp->fastreuse = 0;
8146
8147 if (pp->fastreuseport &&
8148 (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid)))
8149 pp->fastreuseport = 0;
8150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151
8152 /* We are set, so fill up all the data in the hash table
8153 * entry, tie the socket list information with the rest of the
8154 * sockets FIXME: Blurry, NPI (ipg).
8155 */
8156success:
Xin Long6ba84572018-11-12 18:27:17 +08008157 if (!sp->bind_hash) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008158 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008159 sk_add_bind_node(sk, &pp->owner);
Xin Long6ba84572018-11-12 18:27:17 +08008160 sp->bind_hash = pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 }
8162 ret = 0;
8163
8164fail_unlock:
wangweidong3c8e43b2014-01-21 15:44:08 +08008165 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166
8167fail:
wangweidong79b91132014-01-21 15:44:07 +08008168 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169 return ret;
8170}
8171
8172/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
8173 * port is requested.
8174 */
8175static int sctp_get_port(struct sock *sk, unsigned short snum)
8176{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008177 union sctp_addr addr;
8178 struct sctp_af *af = sctp_sk(sk)->pf->af;
8179
8180 /* Set up a dummy address struct from the sk. */
8181 af->from_sk(&addr, sk);
8182 addr.v4.sin_port = htons(snum);
8183
8184 /* Note: sk->sk_num gets filled in if ephemeral port request. */
Daniel Borkmann62208f12013-06-25 18:17:30 +02008185 return !!sctp_get_port_local(sk, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008186}
8187
8188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189 * Move a socket to LISTENING state.
8190 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02008191static int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008193 struct sctp_sock *sp = sctp_sk(sk);
8194 struct sctp_endpoint *ep = sp->ep;
Herbert Xu5821c762016-01-24 21:20:12 +08008195 struct crypto_shash *tfm = NULL;
Neil Horman3c681982012-10-24 09:20:03 +00008196 char alg[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197
8198 /* Allocate HMAC for generating cookie. */
Neil Horman3c681982012-10-24 09:20:03 +00008199 if (!sp->hmac && sp->sctp_hmac_alg) {
8200 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
Herbert Xu5821c762016-01-24 21:20:12 +08008201 tfm = crypto_alloc_shash(alg, 0, 0);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07008202 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00008203 net_info_ratelimited("failed to load transform for %s: %ld\n",
Neil Horman3c681982012-10-24 09:20:03 +00008204 sp->sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008205 return -ENOSYS;
8206 }
8207 sctp_sk(sk)->hmac = tfm;
8208 }
8209
8210 /*
8211 * If a bind() or sctp_bindx() is not called prior to a listen()
8212 * call that allows new associations to be accepted, the system
8213 * picks an ephemeral port and will choose an address set equivalent
8214 * to binding with a wildcard address.
8215 *
8216 * This is not currently spelled out in the SCTP sockets
8217 * extensions draft, but follows the practice as seen in TCP
8218 * sockets.
8219 *
8220 */
Yafang Shaocbabf462017-12-20 11:12:54 +08008221 inet_sk_set_state(sk, SCTP_SS_LISTENING);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008222 if (!ep->base.bind_addr.port) {
8223 if (sctp_autobind(sk))
8224 return -EAGAIN;
8225 } else {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008226 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08008227 inet_sk_set_state(sk, SCTP_SS_CLOSED);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008228 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229 }
8230 }
8231
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008232 sk->sk_max_ack_backlog = backlog;
Xin Long76c6d982018-11-12 18:27:16 +08008233 return sctp_hash_endpoint(ep);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008234}
8235
8236/*
8237 * 4.1.3 / 5.1.3 listen()
8238 *
8239 * By default, new associations are not accepted for UDP style sockets.
8240 * An application uses listen() to mark a socket as being able to
8241 * accept new associations.
8242 *
8243 * On TCP style sockets, applications use listen() to ready the SCTP
8244 * endpoint for accepting inbound associations.
8245 *
8246 * On both types of endpoints a backlog of '0' disables listening.
8247 *
8248 * Move a socket to LISTENING state.
8249 */
8250int sctp_inet_listen(struct socket *sock, int backlog)
8251{
8252 struct sock *sk = sock->sk;
8253 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
8254 int err = -EINVAL;
8255
8256 if (unlikely(backlog < 0))
8257 return err;
8258
wangweidong048ed4b2014-01-21 15:44:11 +08008259 lock_sock(sk);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008260
8261 /* Peeled-off sockets are not allowed to listen(). */
8262 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
8263 goto out;
8264
8265 if (sock->state != SS_UNCONNECTED)
8266 goto out;
8267
Xin Long34b27892017-04-06 13:10:52 +08008268 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
8269 goto out;
8270
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008271 /* If backlog is zero, disable listening. */
8272 if (!backlog) {
8273 if (sctp_sstate(sk, CLOSED))
8274 goto out;
8275
8276 err = 0;
8277 sctp_unhash_endpoint(ep);
8278 sk->sk_state = SCTP_SS_CLOSED;
Xin Longb0e9a2f2018-06-28 15:31:00 +08008279 if (sk->sk_reuse || sctp_sk(sk)->reuse)
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008280 sctp_sk(sk)->bind_hash->fastreuse = 1;
8281 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07008282 }
8283
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008284 /* If we are already listening, just update the backlog */
8285 if (sctp_sstate(sk, LISTENING))
8286 sk->sk_max_ack_backlog = backlog;
8287 else {
8288 err = sctp_listen_start(sk, backlog);
8289 if (err)
8290 goto out;
8291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008292
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008293 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294out:
wangweidong048ed4b2014-01-21 15:44:11 +08008295 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008296 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008297}
8298
8299/*
8300 * This function is done by modeling the current datagram_poll() and the
8301 * tcp_poll(). Note that, based on these implementations, we don't
8302 * lock the socket in this function, even though it seems that,
8303 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08008304 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305 * in this place. We assume that we don't need locks either until proven
8306 * otherwise.
8307 *
8308 * Another thing to note is that we include the Async I/O support
8309 * here, again, by modeling the current TCP/UDP code. We don't have
8310 * a good way to test with it yet.
8311 */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07008312__poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313{
8314 struct sock *sk = sock->sk;
8315 struct sctp_sock *sp = sctp_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -04008316 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07008318 poll_wait(file, sk_sleep(sk), wait);
8319
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03008320 sock_rps_record_flow(sk);
8321
Linus Torvalds1da177e2005-04-16 15:20:36 -07008322 /* A TCP-style listening socket becomes readable when the accept queue
8323 * is not empty.
8324 */
8325 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
8326 return (!list_empty(&sp->ep->asocs)) ?
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008327 (EPOLLIN | EPOLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328
8329 mask = 0;
8330
8331 /* Is there any exceptional events? */
8332 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008333 mask |= EPOLLERR |
8334 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
Davide Libenzif348d702006-03-25 03:07:39 -08008335 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008336 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008337 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008338 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339
8340 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00008341 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008342 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343
8344 /* The association is either gone or not ready. */
8345 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
8346 return mask;
8347
8348 /* Is it writable? */
8349 if (sctp_writeable(sk)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008350 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008351 } else {
Eric Dumazet9cd3e072015-11-29 20:03:10 -08008352 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008353 /*
8354 * Since the socket is not locked, the buffer
8355 * might be made available after the writeable check and
8356 * before the bit is set. This could cause a lost I/O
8357 * signal. tcp_poll() has a race breaker for this race
8358 * condition. Based on their implementation, we put
8359 * in the following code to cover it as well.
8360 */
8361 if (sctp_writeable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008362 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008363 }
8364 return mask;
8365}
8366
8367/********************************************************************
8368 * 2nd Level Abstractions
8369 ********************************************************************/
8370
8371static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008372 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008373{
8374 struct sctp_bind_bucket *pp;
8375
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08008376 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008377 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07008378 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379 pp->port = snum;
8380 pp->fastreuse = 0;
8381 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008382 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05008383 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384 }
8385 return pp;
8386}
8387
8388/* Caller must hold hashbucket lock for this tb with local BH disabled */
8389static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
8390{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07008391 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05008392 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008393 kmem_cache_free(sctp_bucket_cachep, pp);
8394 SCTP_DBG_OBJCNT_DEC(bind_bucket);
8395 }
8396}
8397
8398/* Release this socket's reference to a local port. */
8399static inline void __sctp_put_port(struct sock *sk)
8400{
8401 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008402 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
8403 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404 struct sctp_bind_bucket *pp;
8405
wangweidong3c8e43b2014-01-21 15:44:08 +08008406 spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407 pp = sctp_sk(sk)->bind_hash;
8408 __sk_del_bind_node(sk);
8409 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008410 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008411 sctp_bucket_destroy(pp);
wangweidong3c8e43b2014-01-21 15:44:08 +08008412 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413}
8414
8415void sctp_put_port(struct sock *sk)
8416{
wangweidong79b91132014-01-21 15:44:07 +08008417 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008418 __sctp_put_port(sk);
wangweidong79b91132014-01-21 15:44:07 +08008419 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008420}
8421
8422/*
8423 * The system picks an ephemeral port and choose an address set equivalent
8424 * to binding with a wildcard address.
8425 * One of those addresses will be the primary address for the association.
8426 * This automatically enables the multihoming capability of SCTP.
8427 */
8428static int sctp_autobind(struct sock *sk)
8429{
8430 union sctp_addr autoaddr;
8431 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08008432 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433
8434 /* Initialize a local sockaddr structure to INADDR_ANY. */
8435 af = sctp_sk(sk)->pf->af;
8436
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008437 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008438 af->inaddr_any(&autoaddr, port);
8439
8440 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
8441}
8442
8443/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
8444 *
8445 * From RFC 2292
8446 * 4.2 The cmsghdr Structure *
8447 *
8448 * When ancillary data is sent or received, any number of ancillary data
8449 * objects can be specified by the msg_control and msg_controllen members of
8450 * the msghdr structure, because each object is preceded by
8451 * a cmsghdr structure defining the object's length (the cmsg_len member).
8452 * Historically Berkeley-derived implementations have passed only one object
8453 * at a time, but this API allows multiple objects to be
8454 * passed in a single call to sendmsg() or recvmsg(). The following example
8455 * shows two ancillary data objects in a control buffer.
8456 *
8457 * |<--------------------------- msg_controllen -------------------------->|
8458 * | |
8459 *
8460 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
8461 *
8462 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
8463 * | | |
8464 *
8465 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
8466 *
8467 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
8468 * | | | | |
8469 *
8470 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8471 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
8472 *
8473 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
8474 *
8475 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8476 * ^
8477 * |
8478 *
8479 * msg_control
8480 * points here
8481 */
Xin Longa05437a2017-08-11 10:23:48 +08008482static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008483{
Vlad Yasevichab38fb02008-04-12 18:40:06 -07008484 struct msghdr *my_msg = (struct msghdr *)msg;
Xin Longa05437a2017-08-11 10:23:48 +08008485 struct cmsghdr *cmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008486
Gu Zhengf95b4142014-12-11 11:22:04 +08008487 for_each_cmsghdr(cmsg, my_msg) {
Vlad Yasevichab38fb02008-04-12 18:40:06 -07008488 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008489 return -EINVAL;
8490
8491 /* Should we parse this header or ignore? */
8492 if (cmsg->cmsg_level != IPPROTO_SCTP)
8493 continue;
8494
8495 /* Strictly check lengths following example in SCM code. */
8496 switch (cmsg->cmsg_type) {
8497 case SCTP_INIT:
8498 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008499 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008500 *
8501 * This cmsghdr structure provides information for
8502 * initializing new SCTP associations with sendmsg().
8503 * The SCTP_INITMSG socket option uses this same data
8504 * structure. This structure is not used for
8505 * recvmsg().
8506 *
8507 * cmsg_level cmsg_type cmsg_data[]
8508 * ------------ ------------ ----------------------
8509 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
8510 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008511 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512 return -EINVAL;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008513
8514 cmsgs->init = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515 break;
8516
8517 case SCTP_SNDRCV:
8518 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008519 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520 *
8521 * This cmsghdr structure specifies SCTP options for
8522 * sendmsg() and describes SCTP header information
8523 * about a received message through recvmsg().
8524 *
8525 * cmsg_level cmsg_type cmsg_data[]
8526 * ------------ ------------ ----------------------
8527 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
8528 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008529 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530 return -EINVAL;
8531
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008532 cmsgs->srinfo = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008534 if (cmsgs->srinfo->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07008535 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Long49102802018-03-05 20:44:20 +08008536 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8537 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538 return -EINVAL;
8539 break;
8540
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008541 case SCTP_SNDINFO:
8542 /* SCTP Socket API Extension
8543 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
8544 *
8545 * This cmsghdr structure specifies SCTP options for
8546 * sendmsg(). This structure and SCTP_RCVINFO replaces
8547 * SCTP_SNDRCV which has been deprecated.
8548 *
8549 * cmsg_level cmsg_type cmsg_data[]
8550 * ------------ ------------ ---------------------
8551 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
8552 */
8553 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
8554 return -EINVAL;
8555
8556 cmsgs->sinfo = CMSG_DATA(cmsg);
8557
8558 if (cmsgs->sinfo->snd_flags &
8559 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Long49102802018-03-05 20:44:20 +08008560 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8561 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008562 return -EINVAL;
8563 break;
Xin Longed63afb2018-03-05 20:44:18 +08008564 case SCTP_PRINFO:
8565 /* SCTP Socket API Extension
8566 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
8567 *
8568 * This cmsghdr structure specifies SCTP options for sendmsg().
8569 *
8570 * cmsg_level cmsg_type cmsg_data[]
8571 * ------------ ------------ ---------------------
8572 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
8573 */
8574 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
8575 return -EINVAL;
8576
8577 cmsgs->prinfo = CMSG_DATA(cmsg);
8578 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
8579 return -EINVAL;
8580
8581 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
8582 cmsgs->prinfo->pr_value = 0;
8583 break;
Xin Long3ff547c2018-03-14 19:05:31 +08008584 case SCTP_AUTHINFO:
8585 /* SCTP Socket API Extension
8586 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
8587 *
8588 * This cmsghdr structure specifies SCTP options for sendmsg().
8589 *
8590 * cmsg_level cmsg_type cmsg_data[]
8591 * ------------ ------------ ---------------------
8592 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
8593 */
8594 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
8595 return -EINVAL;
8596
8597 cmsgs->authinfo = CMSG_DATA(cmsg);
8598 break;
Xin Long2c0dbaa2018-03-05 20:44:19 +08008599 case SCTP_DSTADDRV4:
8600 case SCTP_DSTADDRV6:
8601 /* SCTP Socket API Extension
8602 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
8603 *
8604 * This cmsghdr structure specifies SCTP options for sendmsg().
8605 *
8606 * cmsg_level cmsg_type cmsg_data[]
8607 * ------------ ------------ ---------------------
8608 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
8609 * ------------ ------------ ---------------------
8610 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
8611 */
8612 cmsgs->addrs_msg = my_msg;
8613 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008614 default:
8615 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07008616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008617 }
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008618
Linus Torvalds1da177e2005-04-16 15:20:36 -07008619 return 0;
8620}
8621
8622/*
8623 * Wait for a packet..
8624 * Note: This function is the same function as in core/datagram.c
8625 * with a few modifications to make lksctp work.
8626 */
wangweidong26ac8e52013-12-23 12:16:51 +08008627static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008628{
8629 int error;
8630 DEFINE_WAIT(wait);
8631
Eric Dumazetaa395142010-04-20 13:03:51 +00008632 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008633
8634 /* Socket errors? */
8635 error = sock_error(sk);
8636 if (error)
8637 goto out;
8638
8639 if (!skb_queue_empty(&sk->sk_receive_queue))
8640 goto ready;
8641
8642 /* Socket shut down? */
8643 if (sk->sk_shutdown & RCV_SHUTDOWN)
8644 goto out;
8645
8646 /* Sequenced packets can come disconnected. If so we report the
8647 * problem.
8648 */
8649 error = -ENOTCONN;
8650
8651 /* Is there a good reason to think that we may receive some data? */
8652 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8653 goto out;
8654
8655 /* Handle signals. */
8656 if (signal_pending(current))
8657 goto interrupted;
8658
8659 /* Let another process have a go. Since we are going to sleep
8660 * anyway. Note: This may cause odd behaviors if the message
8661 * does not fit in the user's buffer, but this seems to be the
8662 * only way to honor MSG_DONTWAIT realistically.
8663 */
wangweidong048ed4b2014-01-21 15:44:11 +08008664 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665 *timeo_p = schedule_timeout(*timeo_p);
wangweidong048ed4b2014-01-21 15:44:11 +08008666 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008667
8668ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00008669 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008670 return 0;
8671
8672interrupted:
8673 error = sock_intr_errno(*timeo_p);
8674
8675out:
Eric Dumazetaa395142010-04-20 13:03:51 +00008676 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677 *err = error;
8678 return error;
8679}
8680
8681/* Receive a datagram.
8682 * Note: This is pretty much the same routine as in core/datagram.c
8683 * with a few changes to make lksctp work.
8684 */
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02008685struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8686 int noblock, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008687{
8688 int error;
8689 struct sk_buff *skb;
8690 long timeo;
8691
Linus Torvalds1da177e2005-04-16 15:20:36 -07008692 timeo = sock_rcvtimeo(sk, noblock);
8693
Daniel Borkmannbb333812013-06-28 19:49:40 +02008694 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8695 MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008696
8697 do {
8698 /* Again only user level code calls this function,
8699 * so nothing interrupt level
8700 * will suddenly eat the receive_queue.
8701 *
8702 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00008703 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008704 */
8705 if (flags & MSG_PEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008706 skb = skb_peek(&sk->sk_receive_queue);
8707 if (skb)
Reshetova, Elena63354792017-06-30 13:07:58 +03008708 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008709 } else {
Marcelo Ricardo Leitner311b2172016-04-13 19:12:29 -03008710 skb = __skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008711 }
8712
8713 if (skb)
8714 return skb;
8715
Neil Horman6736dc32005-12-02 20:30:06 -08008716 /* Caller is allowed not to check sk->sk_err before calling. */
8717 error = sock_error(sk);
8718 if (error)
8719 goto no_packet;
8720
Linus Torvalds1da177e2005-04-16 15:20:36 -07008721 if (sk->sk_shutdown & RCV_SHUTDOWN)
8722 break;
8723
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07008724 if (sk_can_busy_loop(sk)) {
8725 sk_busy_loop(sk, noblock);
8726
8727 if (!skb_queue_empty(&sk->sk_receive_queue))
8728 continue;
8729 }
Neil Horman8465a5f2014-04-17 15:26:51 -04008730
Linus Torvalds1da177e2005-04-16 15:20:36 -07008731 /* User doesn't want to wait. */
8732 error = -EAGAIN;
8733 if (!timeo)
8734 goto no_packet;
8735 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8736
8737 return NULL;
8738
8739no_packet:
8740 *err = error;
8741 return NULL;
8742}
8743
8744/* If sndbuf has changed, wake up per association sndbuf waiters. */
8745static void __sctp_write_space(struct sctp_association *asoc)
8746{
8747 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008748
Eric Dumazetceb5d582015-11-29 20:03:11 -08008749 if (sctp_wspace(asoc) <= 0)
8750 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008751
Eric Dumazetceb5d582015-11-29 20:03:11 -08008752 if (waitqueue_active(&asoc->wait))
8753 wake_up_interruptible(&asoc->wait);
Eric Dumazeteaefd112011-02-18 03:26:36 +00008754
Eric Dumazetceb5d582015-11-29 20:03:11 -08008755 if (sctp_writeable(sk)) {
8756 struct socket_wq *wq;
8757
8758 rcu_read_lock();
8759 wq = rcu_dereference(sk->sk_wq);
8760 if (wq) {
8761 if (waitqueue_active(&wq->wait))
8762 wake_up_interruptible(&wq->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008763
8764 /* Note that we try to include the Async I/O support
8765 * here by modeling from the current TCP/UDP code.
8766 * We have not tested with it yet.
8767 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00008768 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Eric Dumazetceb5d582015-11-29 20:03:11 -08008769 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008770 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08008771 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008772 }
8773}
8774
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008775static void sctp_wake_up_waiters(struct sock *sk,
8776 struct sctp_association *asoc)
8777{
8778 struct sctp_association *tmp = asoc;
8779
8780 /* We do accounting for the sndbuf space per association,
8781 * so we only need to wake our own association.
8782 */
8783 if (asoc->ep->sndbuf_policy)
8784 return __sctp_write_space(asoc);
8785
Daniel Borkmann1e1cdf82014-04-09 16:10:20 +02008786 /* If association goes down and is just flushing its
8787 * outq, then just normally notify others.
8788 */
8789 if (asoc->base.dead)
8790 return sctp_write_space(sk);
8791
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008792 /* Accounting for the sndbuf space is per socket, so we
8793 * need to wake up others, try to be fair and in case of
8794 * other associations, let them have a go first instead
8795 * of just doing a sctp_write_space() call.
8796 *
8797 * Note that we reach sctp_wake_up_waiters() only when
8798 * associations free up queued chunks, thus we are under
8799 * lock and the list of associations on a socket is
8800 * guaranteed not to change.
8801 */
8802 for (tmp = list_next_entry(tmp, asocs); 1;
8803 tmp = list_next_entry(tmp, asocs)) {
8804 /* Manually skip the head element. */
8805 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8806 continue;
8807 /* Wake up association. */
8808 __sctp_write_space(tmp);
8809 /* We've reached the end. */
8810 if (tmp == asoc)
8811 break;
8812 }
8813}
8814
Linus Torvalds1da177e2005-04-16 15:20:36 -07008815/* Do accounting for the sndbuf space.
8816 * Decrement the used sndbuf space of the corresponding association by the
8817 * data size which was just transmitted(freed).
8818 */
8819static void sctp_wfree(struct sk_buff *skb)
8820{
Daniel Borkmannf869c912014-11-20 01:54:48 +01008821 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8822 struct sctp_association *asoc = chunk->asoc;
8823 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008825 sk_mem_uncharge(sk, skb->truesize);
Xin Long605c0ac2018-10-17 03:07:50 +08008826 sk->sk_wmem_queued -= skb->truesize + sizeof(struct sctp_chunk);
8827 asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk);
8828 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk),
8829 &sk->sk_wmem_alloc));
Neil Horman4d93df02007-08-15 16:07:44 -07008830
Xin Longec2e5062018-03-14 19:05:33 +08008831 if (chunk->shkey) {
8832 struct sctp_shared_key *shkey = chunk->shkey;
8833
8834 /* refcnt == 2 and !list_empty mean after this release, it's
8835 * not being used anywhere, and it's time to notify userland
8836 * that this shkey can be freed if it's been deactivated.
8837 */
8838 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
8839 refcount_read(&shkey->refcnt) == 2) {
8840 struct sctp_ulpevent *ev;
8841
8842 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
8843 SCTP_AUTH_FREE_KEY,
8844 GFP_KERNEL);
8845 if (ev)
8846 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
8847 }
Xin Long1b1e0bc2018-03-14 19:05:30 +08008848 sctp_auth_shkey_release(chunk->shkey);
Xin Longec2e5062018-03-14 19:05:33 +08008849 }
Xin Long1b1e0bc2018-03-14 19:05:30 +08008850
Neil Horman4eb701d2005-04-28 12:02:04 -07008851 sock_wfree(skb);
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008852 sctp_wake_up_waiters(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008853
8854 sctp_association_put(asoc);
8855}
8856
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008857/* Do accounting for the receive space on the socket.
8858 * Accounting for the association is done in ulpevent.c
8859 * We set this as a destructor for the cloned data skbs so that
8860 * accounting is done at the correct time.
8861 */
8862void sctp_sock_rfree(struct sk_buff *skb)
8863{
8864 struct sock *sk = skb->sk;
8865 struct sctp_ulpevent *event = sctp_skb2event(skb);
8866
8867 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07008868
8869 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008870 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07008871 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008872 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008873}
8874
8875
Linus Torvalds1da177e2005-04-16 15:20:36 -07008876/* Helper function to wait for space in the sndbuf. */
8877static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +08008878 size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008879{
8880 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008881 long current_timeo = *timeo_p;
8882 DEFINE_WAIT(wait);
Xin Longa0ff6602018-01-15 17:01:36 +08008883 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008884
Daniel Borkmannbb333812013-06-28 19:49:40 +02008885 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8886 *timeo_p, msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008887
8888 /* Increment the association's refcnt. */
8889 sctp_association_hold(asoc);
8890
8891 /* Wait on the association specific sndbuf space. */
8892 for (;;) {
8893 prepare_to_wait_exclusive(&asoc->wait, &wait,
8894 TASK_INTERRUPTIBLE);
Xin Longca3af4d2017-11-15 16:55:54 +08008895 if (asoc->base.dead)
8896 goto do_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008897 if (!*timeo_p)
8898 goto do_nonblock;
Xin Longca3af4d2017-11-15 16:55:54 +08008899 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900 goto do_error;
8901 if (signal_pending(current))
8902 goto do_interrupted;
Xin Longcd305c72018-10-17 03:07:51 +08008903 if ((int)msg_len <= sctp_wspace(asoc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008904 break;
8905
8906 /* Let another process have a go. Since we are going
8907 * to sleep anyway.
8908 */
wangweidong048ed4b2014-01-21 15:44:11 +08008909 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008910 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008911 lock_sock(sk);
Xin Longa0ff6602018-01-15 17:01:36 +08008912 if (sk != asoc->base.sk)
8913 goto do_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008914
8915 *timeo_p = current_timeo;
8916 }
8917
8918out:
8919 finish_wait(&asoc->wait, &wait);
8920
8921 /* Release the association's refcnt. */
8922 sctp_association_put(asoc);
8923
8924 return err;
8925
Xin Longca3af4d2017-11-15 16:55:54 +08008926do_dead:
8927 err = -ESRCH;
8928 goto out;
8929
Linus Torvalds1da177e2005-04-16 15:20:36 -07008930do_error:
8931 err = -EPIPE;
8932 goto out;
8933
8934do_interrupted:
8935 err = sock_intr_errno(*timeo_p);
8936 goto out;
8937
8938do_nonblock:
8939 err = -EAGAIN;
8940 goto out;
8941}
8942
David S. Miller676d2362014-04-11 16:15:36 -04008943void sctp_data_ready(struct sock *sk)
Wei Yongjun561b1732010-04-28 08:47:18 +00008944{
David S. Miller7ef52732010-05-02 21:43:40 -07008945 struct socket_wq *wq;
8946
8947 rcu_read_lock();
8948 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08008949 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008950 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8951 EPOLLRDNORM | EPOLLRDBAND);
Wei Yongjun561b1732010-04-28 08:47:18 +00008952 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07008953 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00008954}
8955
Linus Torvalds1da177e2005-04-16 15:20:36 -07008956/* If socket sndbuf has changed, wake up all per association waiters. */
8957void sctp_write_space(struct sock *sk)
8958{
8959 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008960
8961 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07008962 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008963 __sctp_write_space(asoc);
8964 }
8965}
8966
8967/* Is there any sndbuf space available on the socket?
8968 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08008969 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07008970 * associations on the same socket. For a UDP-style socket with
8971 * multiple associations, it is possible for it to be "unwriteable"
8972 * prematurely. I assume that this is acceptable because
8973 * a premature "unwriteable" is better than an accidental "writeable" which
8974 * would cause an unwanted block under certain circumstances. For the 1-1
8975 * UDP-style sockets or TCP-style sockets, this code should work.
8976 * - Daisy
8977 */
Xin Longcd305c72018-10-17 03:07:51 +08008978static bool sctp_writeable(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008979{
Xin Longcd305c72018-10-17 03:07:51 +08008980 return sk->sk_sndbuf > sk->sk_wmem_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981}
8982
8983/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8984 * returns immediately with EINPROGRESS.
8985 */
8986static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8987{
8988 struct sock *sk = asoc->base.sk;
8989 int err = 0;
8990 long current_timeo = *timeo_p;
8991 DEFINE_WAIT(wait);
8992
Daniel Borkmannbb333812013-06-28 19:49:40 +02008993 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008994
8995 /* Increment the association's refcnt. */
8996 sctp_association_hold(asoc);
8997
8998 for (;;) {
8999 prepare_to_wait_exclusive(&asoc->wait, &wait,
9000 TASK_INTERRUPTIBLE);
9001 if (!*timeo_p)
9002 goto do_nonblock;
9003 if (sk->sk_shutdown & RCV_SHUTDOWN)
9004 break;
9005 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
9006 asoc->base.dead)
9007 goto do_error;
9008 if (signal_pending(current))
9009 goto do_interrupted;
9010
9011 if (sctp_state(asoc, ESTABLISHED))
9012 break;
9013
9014 /* Let another process have a go. Since we are going
9015 * to sleep anyway.
9016 */
wangweidong048ed4b2014-01-21 15:44:11 +08009017 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009018 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08009019 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009020
9021 *timeo_p = current_timeo;
9022 }
9023
9024out:
9025 finish_wait(&asoc->wait, &wait);
9026
9027 /* Release the association's refcnt. */
9028 sctp_association_put(asoc);
9029
9030 return err;
9031
9032do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08009033 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009034 err = -ETIMEDOUT;
9035 else
9036 err = -ECONNREFUSED;
9037 goto out;
9038
9039do_interrupted:
9040 err = sock_intr_errno(*timeo_p);
9041 goto out;
9042
9043do_nonblock:
9044 err = -EINPROGRESS;
9045 goto out;
9046}
9047
9048static int sctp_wait_for_accept(struct sock *sk, long timeo)
9049{
9050 struct sctp_endpoint *ep;
9051 int err = 0;
9052 DEFINE_WAIT(wait);
9053
9054 ep = sctp_sk(sk)->ep;
9055
9056
9057 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00009058 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009059 TASK_INTERRUPTIBLE);
9060
9061 if (list_empty(&ep->asocs)) {
wangweidong048ed4b2014-01-21 15:44:11 +08009062 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009063 timeo = schedule_timeout(timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08009064 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009065 }
9066
9067 err = -EINVAL;
9068 if (!sctp_sstate(sk, LISTENING))
9069 break;
9070
9071 err = 0;
9072 if (!list_empty(&ep->asocs))
9073 break;
9074
9075 err = sock_intr_errno(timeo);
9076 if (signal_pending(current))
9077 break;
9078
9079 err = -EAGAIN;
9080 if (!timeo)
9081 break;
9082 }
9083
Eric Dumazetaa395142010-04-20 13:03:51 +00009084 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009085
9086 return err;
9087}
9088
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02009089static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009090{
9091 DEFINE_WAIT(wait);
9092
9093 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00009094 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009095 if (list_empty(&sctp_sk(sk)->ep->asocs))
9096 break;
wangweidong048ed4b2014-01-21 15:44:11 +08009097 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009098 timeout = schedule_timeout(timeout);
wangweidong048ed4b2014-01-21 15:44:11 +08009099 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009100 } while (!signal_pending(current) && timeout);
9101
Eric Dumazetaa395142010-04-20 13:03:51 +00009102 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009103}
9104
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07009105static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
9106{
9107 struct sk_buff *frag;
9108
9109 if (!skb->data_len)
9110 goto done;
9111
9112 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07009113 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07009114 sctp_skb_set_owner_r_frag(frag, sk);
9115
9116done:
9117 sctp_skb_set_owner_r(skb, sk);
9118}
9119
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009120void sctp_copy_sock(struct sock *newsk, struct sock *sk,
9121 struct sctp_association *asoc)
9122{
9123 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08009124 struct inet_sock *newinet;
Richard Haines2277c7c2018-02-13 20:56:24 +00009125 struct sctp_sock *sp = sctp_sk(sk);
9126 struct sctp_endpoint *ep = sp->ep;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009127
9128 newsk->sk_type = sk->sk_type;
9129 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
9130 newsk->sk_flags = sk->sk_flags;
Marcelo Ricardo Leitner50a5ffb2015-12-04 15:14:05 -02009131 newsk->sk_tsflags = sk->sk_tsflags;
Tom Herbert28448b82014-05-23 08:47:19 -07009132 newsk->sk_no_check_tx = sk->sk_no_check_tx;
9133 newsk->sk_no_check_rx = sk->sk_no_check_rx;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009134 newsk->sk_reuse = sk->sk_reuse;
Xin Longb0e9a2f2018-06-28 15:31:00 +08009135 sctp_sk(newsk)->reuse = sp->reuse;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009136
9137 newsk->sk_shutdown = sk->sk_shutdown;
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02009138 newsk->sk_destruct = sctp_destruct_sock;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009139 newsk->sk_family = sk->sk_family;
9140 newsk->sk_protocol = IPPROTO_SCTP;
9141 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
9142 newsk->sk_sndbuf = sk->sk_sndbuf;
9143 newsk->sk_rcvbuf = sk->sk_rcvbuf;
9144 newsk->sk_lingertime = sk->sk_lingertime;
9145 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
9146 newsk->sk_sndtimeo = sk->sk_sndtimeo;
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03009147 newsk->sk_rxhash = sk->sk_rxhash;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009148
9149 newinet = inet_sk(newsk);
9150
9151 /* Initialize sk's sport, dport, rcv_saddr and daddr for
9152 * getsockname() and getpeername()
9153 */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00009154 newinet->inet_sport = inet->inet_sport;
9155 newinet->inet_saddr = inet->inet_saddr;
9156 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
9157 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009158 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00009159 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009160
9161 newinet->uc_ttl = inet->uc_ttl;
9162 newinet->mc_loop = 1;
9163 newinet->mc_ttl = 1;
9164 newinet->mc_index = 0;
9165 newinet->mc_list = NULL;
Marcelo Ricardo Leitner01ce63c2015-12-04 15:14:04 -02009166
9167 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
9168 net_enable_timestamp();
Marcelo Ricardo Leitner3538a5c2015-12-23 16:44:09 -02009169
Richard Haines2277c7c2018-02-13 20:56:24 +00009170 /* Set newsk security attributes from orginal sk and connection
9171 * security attribute from ep.
9172 */
9173 security_sctp_sk_clone(ep, sk, newsk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009174}
9175
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03009176static inline void sctp_copy_descendant(struct sock *sk_to,
9177 const struct sock *sk_from)
9178{
9179 int ancestor_size = sizeof(struct inet_sock) +
9180 sizeof(struct sctp_sock) -
Xin Long636d25d2019-03-18 19:58:29 +08009181 offsetof(struct sctp_sock, pd_lobby);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03009182
9183 if (sk_from->sk_family == PF_INET6)
9184 ancestor_size += sizeof(struct ipv6_pinfo);
9185
9186 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
9187}
9188
Linus Torvalds1da177e2005-04-16 15:20:36 -07009189/* Populate the fields of the newsk from the oldsk and migrate the assoc
9190 * and its messages to the newsk.
9191 */
Xin Long89664c622019-03-03 17:54:53 +08009192static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
9193 struct sctp_association *assoc,
9194 enum sctp_socket_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009195{
9196 struct sctp_sock *oldsp = sctp_sk(oldsk);
9197 struct sctp_sock *newsp = sctp_sk(newsk);
9198 struct sctp_bind_bucket *pp; /* hash list port iterator */
9199 struct sctp_endpoint *newep = newsp->ep;
9200 struct sk_buff *skb, *tmp;
9201 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08009202 struct sctp_bind_hashbucket *head;
Xin Long89664c622019-03-03 17:54:53 +08009203 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009204
9205 /* Migrate socket buffer sizes and all the socket level options to the
9206 * new socket.
9207 */
9208 newsk->sk_sndbuf = oldsk->sk_sndbuf;
9209 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
9210 /* Brute force copy old sctp opt. */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03009211 sctp_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009212
9213 /* Restore the ep value that was overwritten with the above structure
9214 * copy.
9215 */
9216 newsp->ep = newep;
9217 newsp->hmac = NULL;
9218
9219 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00009220 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
9221 inet_sk(oldsk)->inet_num)];
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01009222 spin_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009223 pp = sctp_sk(oldsk)->bind_hash;
9224 sk_add_bind_node(newsk, &pp->owner);
9225 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00009226 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01009227 spin_unlock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009228
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07009229 /* Copy the bind_addr list from the original endpoint to the new
9230 * endpoint so that we can handle restarts properly
9231 */
Xin Long89664c622019-03-03 17:54:53 +08009232 err = sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
9233 &oldsp->ep->base.bind_addr, GFP_KERNEL);
9234 if (err)
9235 return err;
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07009236
Xin Longc6f33e02019-03-03 17:54:55 +08009237 /* New ep's auth_hmacs should be set if old ep's is set, in case
9238 * that net->sctp.auth_enable has been changed to 0 by users and
9239 * new ep's auth_hmacs couldn't be set in sctp_endpoint_init().
9240 */
9241 if (oldsp->ep->auth_hmacs) {
9242 err = sctp_auth_init_hmacs(newsp->ep, GFP_KERNEL);
9243 if (err)
9244 return err;
9245 }
9246
Linus Torvalds1da177e2005-04-16 15:20:36 -07009247 /* Move any messages in the old socket's receive queue that are for the
9248 * peeled off association to the new socket's receive queue.
9249 */
9250 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
9251 event = sctp_skb2event(skb);
9252 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07009253 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009254 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07009255 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009256 }
9257 }
9258
9259 /* Clean up any messages pending delivery due to partial
9260 * delivery. Three cases:
9261 * 1) No partial deliver; no work.
9262 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
9263 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
9264 */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07009265 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009266
Vlad Yasevichb6e13312007-04-20 12:23:15 -07009267 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009268 struct sk_buff_head *queue;
9269
9270 /* Decide which queue to move pd_lobby skbs to. */
9271 if (assoc->ulpq.pd_mode) {
9272 queue = &newsp->pd_lobby;
9273 } else
9274 queue = &newsk->sk_receive_queue;
9275
9276 /* Walk through the pd_lobby, looking for skbs that
9277 * need moved to the new socket.
9278 */
9279 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
9280 event = sctp_skb2event(skb);
9281 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07009282 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009283 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07009284 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009285 }
9286 }
9287
9288 /* Clear up any skbs waiting for the partial
9289 * delivery to finish.
9290 */
9291 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07009292 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009293
9294 }
9295
Xin Long13228232017-12-08 21:04:09 +08009296 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07009297
Linus Torvalds1da177e2005-04-16 15:20:36 -07009298 /* Set the type of socket to indicate that it is peeled off from the
9299 * original UDP-style socket or created with the accept() call on a
9300 * TCP-style socket..
9301 */
9302 newsp->type = type;
9303
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07009304 /* Mark the new socket "in-use" by the user so that any packets
9305 * that may arrive on the association after we've moved it are
9306 * queued to the backlog. This prevents a potential race between
9307 * backlog processing on the old socket and new-packet processing
9308 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07009309 *
9310 * The caller has just allocated newsk so we can guarantee that other
9311 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07009312 */
Zach Brown5131a182007-06-22 15:14:46 -07009313 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Xin Longd04adf12017-10-28 02:13:29 +08009314 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315 sctp_assoc_migrate(assoc, newsk);
Xin Longd04adf12017-10-28 02:13:29 +08009316 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317
9318 /* If the association on the newsk is already closed before accept()
9319 * is called, set RCV_SHUTDOWN flag.
9320 */
Xin Longd46e4162016-06-09 22:48:18 +08009321 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08009322 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323 newsk->sk_shutdown |= RCV_SHUTDOWN;
Xin Longd46e4162016-06-09 22:48:18 +08009324 } else {
Yafang Shaocbabf462017-12-20 11:12:54 +08009325 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
Xin Longd46e4162016-06-09 22:48:18 +08009326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009327
wangweidong048ed4b2014-01-21 15:44:11 +08009328 release_sock(newsk);
Xin Long89664c622019-03-03 17:54:53 +08009329
9330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009331}
9332
Neil Horman4d93df02007-08-15 16:07:44 -07009333
Linus Torvalds1da177e2005-04-16 15:20:36 -07009334/* This proto struct describes the ULP interface for SCTP. */
9335struct proto sctp_prot = {
9336 .name = "SCTP",
9337 .owner = THIS_MODULE,
9338 .close = sctp_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009339 .disconnect = sctp_disconnect,
9340 .accept = sctp_accept,
9341 .ioctl = sctp_ioctl,
9342 .init = sctp_init_sock,
9343 .destroy = sctp_destroy_sock,
9344 .shutdown = sctp_shutdown,
9345 .setsockopt = sctp_setsockopt,
9346 .getsockopt = sctp_getsockopt,
9347 .sendmsg = sctp_sendmsg,
9348 .recvmsg = sctp_recvmsg,
9349 .bind = sctp_bind,
9350 .backlog_rcv = sctp_backlog_rcv,
9351 .hash = sctp_hash,
9352 .unhash = sctp_unhash,
9353 .get_port = sctp_get_port,
9354 .obj_size = sizeof(struct sctp_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07009355 .useroffset = offsetof(struct sctp_sock, subscribe),
9356 .usersize = offsetof(struct sctp_sock, initmsg) -
9357 offsetof(struct sctp_sock, subscribe) +
9358 sizeof_field(struct sctp_sock, initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07009359 .sysctl_mem = sysctl_sctp_mem,
9360 .sysctl_rmem = sysctl_sctp_rmem,
9361 .sysctl_wmem = sysctl_sctp_wmem,
9362 .memory_pressure = &sctp_memory_pressure,
9363 .enter_memory_pressure = sctp_enter_memory_pressure,
9364 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08009365 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009366};
9367
Eric Dumazetdfd56b82011-12-10 09:48:31 +00009368#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08009369
Eric Dumazet602dd622015-12-01 07:20:07 -08009370#include <net/transp_v6.h>
9371static void sctp_v6_destroy_sock(struct sock *sk)
9372{
9373 sctp_destroy_sock(sk);
9374 inet6_destroy_sock(sk);
9375}
9376
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377struct proto sctpv6_prot = {
9378 .name = "SCTPv6",
9379 .owner = THIS_MODULE,
9380 .close = sctp_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009381 .disconnect = sctp_disconnect,
9382 .accept = sctp_accept,
9383 .ioctl = sctp_ioctl,
9384 .init = sctp_init_sock,
Eric Dumazet602dd622015-12-01 07:20:07 -08009385 .destroy = sctp_v6_destroy_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009386 .shutdown = sctp_shutdown,
9387 .setsockopt = sctp_setsockopt,
9388 .getsockopt = sctp_getsockopt,
9389 .sendmsg = sctp_sendmsg,
9390 .recvmsg = sctp_recvmsg,
9391 .bind = sctp_bind,
9392 .backlog_rcv = sctp_backlog_rcv,
9393 .hash = sctp_hash,
9394 .unhash = sctp_unhash,
9395 .get_port = sctp_get_port,
9396 .obj_size = sizeof(struct sctp6_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07009397 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
9398 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
9399 offsetof(struct sctp6_sock, sctp.subscribe) +
9400 sizeof_field(struct sctp6_sock, sctp.initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07009401 .sysctl_mem = sysctl_sctp_mem,
9402 .sysctl_rmem = sysctl_sctp_rmem,
9403 .sysctl_wmem = sysctl_sctp_wmem,
9404 .memory_pressure = &sctp_memory_pressure,
9405 .enter_memory_pressure = sctp_enter_memory_pressure,
9406 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08009407 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009408};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00009409#endif /* IS_ENABLED(CONFIG_IPV6) */