blob: aa80cda3658116b94081f4700c28b8cb03fd2de6 [file] [log] [blame]
Thomas Gleixner47505b82019-05-23 11:14:41 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Vlad Yasevich60c778b2008-01-11 09:57:09 -05002/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * (C) Copyright IBM Corp. 2001, 2004
4 * Copyright (c) 1999-2000 Cisco, Inc.
5 * Copyright (c) 1999-2001 Motorola, Inc.
6 * Copyright (c) 2001-2003 Intel Corp.
7 * Copyright (c) 2001-2002 Nokia, Inc.
8 * Copyright (c) 2001 La Monte H.P. Yarroll
9 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050010 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * These functions interface with the sockets layer to implement the
13 * SCTP Extensions for the Sockets API.
14 *
15 * Note that the descriptions from the specification are USER level
16 * functions--this file is the functions which populate the struct proto
17 * for SCTP which is the BOTTOM of the sockets interface.
18 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Please send any bug reports or fixes you make to the
20 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020021 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * Written or modified by:
24 * La Monte H.P. Yarroll <piggy@acm.org>
25 * Narasimha Budihal <narsi@refcode.org>
26 * Karl Knutson <karl@athena.chicago.il.us>
27 * Jon Grimm <jgrimm@us.ibm.com>
28 * Xingang Guo <xingang.guo@intel.com>
29 * Daisy Chang <daisyc@us.ibm.com>
30 * Sridhar Samudrala <samudrala@us.ibm.com>
31 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
32 * Ardelle Fan <ardelle.fan@intel.com>
33 * Ryan Layer <rmlayer@us.ibm.com>
34 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
35 * Kevin Gao <kevin.gao@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
37
Joe Perches145ce502010-08-24 13:21:08 +000038#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39
Herbert Xu5821c762016-01-24 21:20:12 +080040#include <crypto/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/types.h>
42#include <linux/kernel.h>
43#include <linux/wait.h>
44#include <linux/time.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010045#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/ip.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080047#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/fcntl.h>
49#include <linux/poll.h>
50#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Al Viro56b31d12012-08-18 00:25:51 -040052#include <linux/file.h>
Daniel Borkmannffd59392014-02-17 12:11:11 +010053#include <linux/compat.h>
NeilBrown0eb71a92018-06-18 12:52:50 +100054#include <linux/rhashtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#include <net/ip.h>
57#include <net/icmp.h>
58#include <net/route.h>
59#include <net/ipv6.h>
60#include <net/inet_common.h>
Neil Horman8465a5f2014-04-17 15:26:51 -040061#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#include <linux/socket.h> /* for sa_family_t */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040064#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <net/sock.h>
66#include <net/sctp/sctp.h>
67#include <net/sctp/sm.h>
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -030068#include <net/sctp/stream_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* Forward declarations for internal helper functions. */
Xin Longcd305c72018-10-17 03:07:51 +080071static bool sctp_writeable(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static void sctp_wfree(struct sk_buff *skb);
Xin Longcea0cc82017-11-15 16:57:26 +080073static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +080074 size_t msg_len);
wangweidong26ac8e52013-12-23 12:16:51 +080075static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
77static int sctp_wait_for_accept(struct sock *sk, long timeo);
78static void sctp_wait_for_close(struct sock *sk, long timeo);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +020079static void sctp_destruct_sock(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
81 union sctp_addr *addr, int len);
82static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
83static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
84static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
85static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
86static int sctp_send_asconf(struct sctp_association *asoc,
87 struct sctp_chunk *chunk);
88static int sctp_do_bind(struct sock *, union sctp_addr *, int);
89static int sctp_autobind(struct sock *sk);
Xin Long89664c622019-03-03 17:54:53 +080090static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
91 struct sctp_association *assoc,
92 enum sctp_socket_type type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Eric Dumazet06044752017-06-07 13:29:12 -070094static unsigned long sctp_memory_pressure;
Eric Dumazet8d987e52010-11-09 23:24:26 +000095static atomic_long_t sctp_memory_allocated;
Eric Dumazet17483762008-11-25 21:16:35 -080096struct percpu_counter sctp_sockets_allocated;
Neil Horman4d93df02007-08-15 16:07:44 -070097
Pavel Emelyanov5c52ba12008-07-16 20:28:10 -070098static void sctp_enter_memory_pressure(struct sock *sk)
Neil Horman4d93df02007-08-15 16:07:44 -070099{
100 sctp_memory_pressure = 1;
101}
102
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/* Get the sndbuf space available at the time on the association. */
105static inline int sctp_wspace(struct sctp_association *asoc)
106{
Xin Longcd305c72018-10-17 03:07:51 +0800107 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Xin Longcd305c72018-10-17 03:07:51 +0800109 return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used
110 : sk_stream_wspace(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113/* Increment the used sndbuf space count of the corresponding association by
114 * the size of the outgoing data chunk.
115 * Also, set the skb destructor for sndbuf accounting later.
116 *
117 * Since it is always 1-1 between chunk and skb, and also a new skb is always
118 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
119 * destructor in the data chunk skb for the purpose of the sndbuf space
120 * tracking.
121 */
122static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
123{
124 struct sctp_association *asoc = chunk->asoc;
125 struct sock *sk = asoc->base.sk;
126
127 /* The sndbuf space is tracked per association. */
128 sctp_association_hold(asoc);
129
Xin Long1b1e0bc2018-03-14 19:05:30 +0800130 if (chunk->shkey)
131 sctp_auth_shkey_hold(chunk->shkey);
132
Neil Horman4eb701d2005-04-28 12:02:04 -0700133 skb_set_owner_w(chunk->skb, sk);
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 chunk->skb->destructor = sctp_wfree;
136 /* Save the chunk pointer in skb for sctp_wfree to use later. */
Daniel Borkmannf869c912014-11-20 01:54:48 +0100137 skb_shinfo(chunk->skb)->destructor_arg = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Reshetova, Elena14afee42017-06-30 13:08:00 +0300139 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Xin Long605c0ac2018-10-17 03:07:50 +0800140 asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk);
141 sk->sk_wmem_queued += chunk->skb->truesize + sizeof(struct sctp_chunk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800142 sk_mem_charge(sk, chunk->skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Xin Longd04adf12017-10-28 02:13:29 +0800145static void sctp_clear_owner_w(struct sctp_chunk *chunk)
146{
147 skb_orphan(chunk->skb);
148}
149
150static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
151 void (*cb)(struct sctp_chunk *))
152
153{
154 struct sctp_outq *q = &asoc->outqueue;
155 struct sctp_transport *t;
156 struct sctp_chunk *chunk;
157
158 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
159 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
160 cb(chunk);
161
Xin Longa8dd3972017-11-26 20:56:07 +0800162 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800163 cb(chunk);
164
Xin Longa8dd3972017-11-26 20:56:07 +0800165 list_for_each_entry(chunk, &q->sacked, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800166 cb(chunk);
167
Xin Longa8dd3972017-11-26 20:56:07 +0800168 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800169 cb(chunk);
170
171 list_for_each_entry(chunk, &q->out_chunk_list, list)
172 cb(chunk);
173}
174
Xin Long13228232017-12-08 21:04:09 +0800175static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
176 void (*cb)(struct sk_buff *, struct sock *))
177
178{
179 struct sk_buff *skb, *tmp;
180
181 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
182 cb(skb, sk);
183
184 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
185 cb(skb, sk);
186
187 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
188 cb(skb, sk);
189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/* Verify that this is a valid address. */
192static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
193 int len)
194{
195 struct sctp_af *af;
196
197 /* Verify basic sockaddr. */
198 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
199 if (!af)
200 return -EINVAL;
201
202 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700203 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -EINVAL;
205
206 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
207 return -EINVAL;
208
209 return 0;
210}
211
212/* Look up the association by its id. If this is not a UDP-style
213 * socket, the ID field is always ignored.
214 */
215struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
216{
217 struct sctp_association *asoc = NULL;
218
219 /* If this is not a UDP-style socket, assoc id should be ignored. */
220 if (!sctp_style(sk, UDP)) {
221 /* Return NULL if the socket state is not ESTABLISHED. It
222 * could be a TCP-style listening socket or a socket which
223 * hasn't yet called connect() to establish an association.
224 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -0300225 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return NULL;
227
228 /* Get the first and the only association from the list. */
229 if (!list_empty(&sctp_sk(sk)->ep->asocs))
230 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
231 struct sctp_association, asocs);
232 return asoc;
233 }
234
235 /* Otherwise this is a UDP-style socket. */
Xin Long80df2702019-01-28 15:08:23 +0800236 if (id <= SCTP_ALL_ASSOC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return NULL;
238
239 spin_lock_bh(&sctp_assocs_id_lock);
240 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
Marcelo Ricardo Leitnerb336dec2018-10-16 15:18:17 -0300241 if (asoc && (asoc->base.sk != sk || asoc->base.dead))
242 asoc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 spin_unlock_bh(&sctp_assocs_id_lock);
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return asoc;
246}
247
248/* Look up the transport from an address and an assoc id. If both address and
249 * id are specified, the associations matching the address and the id should be
250 * the same.
251 */
252static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
253 struct sockaddr_storage *addr,
254 sctp_assoc_t id)
255{
256 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
Xin Long6f29a132017-01-24 14:01:53 +0800257 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 union sctp_addr *laddr = (union sctp_addr *)addr;
Xin Long6f29a132017-01-24 14:01:53 +0800259 struct sctp_transport *transport;
260
Xin Long912964e2017-02-07 20:56:08 +0800261 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
Xin Long6f29a132017-01-24 14:01:53 +0800262 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800265 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (!addr_asoc)
269 return NULL;
270
271 id_asoc = sctp_id2assoc(sk, id);
272 if (id_asoc && (id_asoc != addr_asoc))
273 return NULL;
274
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600275 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 (union sctp_addr *)addr);
277
278 return transport;
279}
280
281/* API 3.1.2 bind() - UDP Style Syntax
282 * The syntax of bind() is,
283 *
284 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
285 *
286 * sd - the socket descriptor returned by socket().
287 * addr - the address structure (struct sockaddr_in or struct
288 * sockaddr_in6 [RFC 2553]),
289 * addr_len - the size of the address structure.
290 */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200291static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 int retval = 0;
294
wangweidong048ed4b2014-01-21 15:44:11 +0800295 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Daniel Borkmannbb333812013-06-28 19:49:40 +0200297 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
298 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* Disallow binding twice. */
301 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700302 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 addr_len);
304 else
305 retval = -EINVAL;
306
wangweidong048ed4b2014-01-21 15:44:11 +0800307 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 return retval;
310}
311
312static long sctp_get_port_local(struct sock *, union sctp_addr *);
313
314/* Verify this is a valid sockaddr. */
315static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
316 union sctp_addr *addr, int len)
317{
318 struct sctp_af *af;
319
320 /* Check minimum size. */
321 if (len < sizeof (struct sockaddr))
322 return NULL;
323
Xin Longc5006b82018-01-15 17:02:00 +0800324 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
325 return NULL;
326
Eric Dumazet81e98372018-04-08 07:52:08 -0700327 if (addr->sa.sa_family == AF_INET6) {
328 if (len < SIN6_LEN_RFC2133)
329 return NULL;
330 /* V4 mapped address are really of AF_INET family */
331 if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
332 !opt->pf->af_supported(AF_INET, opt))
333 return NULL;
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /* If we get this far, af is valid. */
337 af = sctp_get_af_specific(addr->sa.sa_family);
338
339 if (len < af->sockaddr_len)
340 return NULL;
341
342 return af;
343}
344
345/* Bind a local address either to an endpoint or to an association. */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200346static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Eric W. Biederman35946982012-11-16 03:03:12 +0000348 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct sctp_sock *sp = sctp_sk(sk);
350 struct sctp_endpoint *ep = sp->ep;
351 struct sctp_bind_addr *bp = &ep->base.bind_addr;
352 struct sctp_af *af;
353 unsigned short snum;
354 int ret = 0;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 /* Common sockaddr verification. */
357 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700358 if (!af) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200359 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
360 __func__, sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700362 }
363
364 snum = ntohs(addr->v4.sin_port);
365
Daniel Borkmannbb333812013-06-28 19:49:40 +0200366 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
367 __func__, sk, &addr->sa, bp->port, snum, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /* PF specific bind() address verification. */
370 if (!sp->pf->bind_verify(sp, addr))
371 return -EADDRNOTAVAIL;
372
Vlad Yasevich8b358052007-05-15 17:14:58 -0400373 /* We must either be unbound, or bind to the same port.
374 * It's OK to allow 0 ports if we are already bound.
375 * We'll just inhert an already bound port in this case
376 */
377 if (bp->port) {
378 if (!snum)
379 snum = bp->port;
380 else if (snum != bp->port) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200381 pr_debug("%s: new port %d doesn't match existing port "
382 "%d\n", __func__, snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400383 return -EINVAL;
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386
Krister Johansen4548b682017-01-20 17:49:11 -0800387 if (snum && snum < inet_prot_sock(net) &&
Eric W. Biederman35946982012-11-16 03:03:12 +0000388 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return -EACCES;
390
Vlad Yasevich4e540642008-07-18 23:06:32 -0700391 /* See if the address matches any of the addresses we may have
392 * already bound before checking against other endpoints.
393 */
394 if (sctp_bind_addr_match(bp, addr, sp))
395 return -EINVAL;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* Make sure we are allowed to bind here.
398 * The function sctp_get_port_local() does duplicate address
399 * detection.
400 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900401 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if ((ret = sctp_get_port_local(sk, addr))) {
Vlad Yasevich4e540642008-07-18 23:06:32 -0700403 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405
406 /* Refresh ephemeral port. */
407 if (!bp->port)
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000408 bp->port = inet_sk(sk)->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Vlad Yasevich559cf712007-09-16 16:03:28 -0700410 /* Add the address to the bind address list.
411 * Use GFP_ATOMIC since BHs will be disabled.
412 */
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300413 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
414 SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* Copy back into socket for getsockname() use. */
417 if (!ret) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000418 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600419 sp->pf->to_sk_saddr(addr, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 return ret;
423}
424
425 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
426 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900427 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900429 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900431 * subsequent ASCONF. Note this restriction binds each side, so at any
432 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * from each endpoint).
434 */
435static int sctp_send_asconf(struct sctp_association *asoc,
436 struct sctp_chunk *chunk)
437{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000438 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 int retval = 0;
440
441 /* If there is an outstanding ASCONF chunk, queue it for later
442 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900443 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700445 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900446 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
449 /* Hold the chunk until an ASCONF_ACK is received. */
450 sctp_chunk_hold(chunk);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000451 retval = sctp_primitive_ASCONF(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (retval)
453 sctp_chunk_free(chunk);
454 else
455 asoc->addip_last_asconf = chunk;
456
457out:
458 return retval;
459}
460
461/* Add a list of addresses as bind addresses to local endpoint or
462 * association.
463 *
464 * Basically run through each address specified in the addrs/addrcnt
465 * array/length pair, determine if it is IPv6 or IPv4 and call
466 * sctp_do_bind() on it.
467 *
468 * If any of them fails, then the operation will be reversed and the
469 * ones that were added will be removed.
470 *
471 * Only sctp_setsockopt_bindx() is supposed to call this function.
472 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200473static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 int cnt;
476 int retval = 0;
477 void *addr_buf;
478 struct sockaddr *sa_addr;
479 struct sctp_af *af;
480
Daniel Borkmannbb333812013-06-28 19:49:40 +0200481 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
482 addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 addr_buf = addrs;
485 for (cnt = 0; cnt < addrcnt; cnt++) {
486 /* The list may contain either IPv4 or IPv6 address;
487 * determine the address length for walking thru the list.
488 */
Joe Perchesea110732011-06-13 16:21:26 +0000489 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 af = sctp_get_af_specific(sa_addr->sa_family);
491 if (!af) {
492 retval = -EINVAL;
493 goto err_bindx_add;
494 }
495
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900496 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 af->sockaddr_len);
498
499 addr_buf += af->sockaddr_len;
500
501err_bindx_add:
502 if (retval < 0) {
503 /* Failed. Cleanup the ones that have been added */
504 if (cnt > 0)
505 sctp_bindx_rem(sk, addrs, cnt);
506 return retval;
507 }
508 }
509
510 return retval;
511}
512
513/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
514 * associations that are part of the endpoint indicating that a list of local
515 * addresses are added to the endpoint.
516 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900517 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * association, we do not send the chunk for that association. But it will not
519 * affect other associations.
520 *
521 * Only sctp_setsockopt_bindx() is supposed to call this function.
522 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900523static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct sockaddr *addrs,
525 int addrcnt)
526{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000527 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct sctp_sock *sp;
529 struct sctp_endpoint *ep;
530 struct sctp_association *asoc;
531 struct sctp_bind_addr *bp;
532 struct sctp_chunk *chunk;
533 struct sctp_sockaddr_entry *laddr;
534 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700535 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 void *addr_buf;
537 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct list_head *p;
539 int i;
540 int retval = 0;
541
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000542 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return retval;
544
545 sp = sctp_sk(sk);
546 ep = sp->ep;
547
Daniel Borkmannbb333812013-06-28 19:49:40 +0200548 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
549 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Robert P. J. Day9dbc15f02008-04-12 18:54:24 -0700551 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (!asoc->peer.asconf_capable)
553 continue;
554
555 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
556 continue;
557
558 if (!sctp_state(asoc, ESTABLISHED))
559 continue;
560
561 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900562 * in the bind address list of the association. If so,
563 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 * other associations.
565 */
566 addr_buf = addrs;
567 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000568 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 af = sctp_get_af_specific(addr->v4.sin_family);
570 if (!af) {
571 retval = -EINVAL;
572 goto out;
573 }
574
575 if (sctp_assoc_lookup_laddr(asoc, addr))
576 break;
577
578 addr_buf += af->sockaddr_len;
579 }
580 if (i < addrcnt)
581 continue;
582
Vlad Yasevich559cf712007-09-16 16:03:28 -0700583 /* Use the first valid address in bind addr list of
584 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 bp = &asoc->base.bind_addr;
587 p = bp->address_list.next;
588 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800589 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 addrcnt, SCTP_PARAM_ADD_IP);
591 if (!chunk) {
592 retval = -ENOMEM;
593 goto out;
594 }
595
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700596 /* Add the new addresses to the bind address list with
597 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700599 addr_buf = addrs;
600 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000601 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700602 af = sctp_get_af_specific(addr->v4.sin_family);
603 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800604 retval = sctp_add_bind_addr(bp, &saveaddr,
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300605 sizeof(saveaddr),
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800606 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700607 addr_buf += af->sockaddr_len;
608 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900609 if (asoc->src_out_of_asoc_ok) {
610 struct sctp_transport *trans;
611
612 list_for_each_entry(trans,
613 &asoc->peer.transport_addr_list, transports) {
Michio Honda8a07eb02011-04-26 20:19:36 +0900614 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
615 2*asoc->pathmtu, 4380));
616 trans->ssthresh = asoc->peer.i.a_rwnd;
617 trans->rto = asoc->rto_initial;
Michele Baldessari196d6752012-12-01 04:49:42 +0000618 sctp_max_rto(asoc, trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900619 trans->rtt = trans->srtt = trans->rttvar = 0;
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300620 /* Clear the source and route cache */
Michio Honda8a07eb02011-04-26 20:19:36 +0900621 sctp_transport_route(trans, NULL,
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300622 sctp_sk(asoc->base.sk));
Michio Honda8a07eb02011-04-26 20:19:36 +0900623 }
624 }
625 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
628out:
629 return retval;
630}
631
632/* Remove a list of addresses from bind addresses list. Do not remove the
633 * last address.
634 *
635 * Basically run through each address specified in the addrs/addrcnt
636 * array/length pair, determine if it is IPv6 or IPv4 and call
637 * sctp_del_bind() on it.
638 *
639 * If any of them fails, then the operation will be reversed and the
640 * ones that were removed will be added back.
641 *
642 * At least one address has to be left; if only one address is
643 * available, the operation will return -EBUSY.
644 *
645 * Only sctp_setsockopt_bindx() is supposed to call this function.
646 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200647static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct sctp_sock *sp = sctp_sk(sk);
650 struct sctp_endpoint *ep = sp->ep;
651 int cnt;
652 struct sctp_bind_addr *bp = &ep->base.bind_addr;
653 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800655 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 struct sctp_af *af;
657
Daniel Borkmannbb333812013-06-28 19:49:40 +0200658 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
659 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 addr_buf = addrs;
662 for (cnt = 0; cnt < addrcnt; cnt++) {
663 /* If the bind address list is empty or if there is only one
664 * bind address, there is nothing more to be removed (we need
665 * at least one address here).
666 */
667 if (list_empty(&bp->address_list) ||
668 (sctp_list_single_entry(&bp->address_list))) {
669 retval = -EBUSY;
670 goto err_bindx_rem;
671 }
672
Joe Perchesea110732011-06-13 16:21:26 +0000673 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800674 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (!af) {
676 retval = -EINVAL;
677 goto err_bindx_rem;
678 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700679
680 if (!af->addr_valid(sa_addr, sp, NULL)) {
681 retval = -EADDRNOTAVAIL;
682 goto err_bindx_rem;
683 }
684
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000685 if (sa_addr->v4.sin_port &&
686 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 retval = -EINVAL;
688 goto err_bindx_rem;
689 }
690
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000691 if (!sa_addr->v4.sin_port)
692 sa_addr->v4.sin_port = htons(bp->port);
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* FIXME - There is probably a need to check if sk->sk_saddr and
695 * sk->sk_rcv_addr are currently set to one of the addresses to
696 * be removed. This is something which needs to be looked into
697 * when we are fixing the outstanding issues with multi-homing
698 * socket routing and failover schemes. Refer to comments in
699 * sctp_do_bind(). -daisy
700 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400701 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 addr_buf += af->sockaddr_len;
704err_bindx_rem:
705 if (retval < 0) {
706 /* Failed. Add the ones that has been removed back */
707 if (cnt > 0)
708 sctp_bindx_add(sk, addrs, cnt);
709 return retval;
710 }
711 }
712
713 return retval;
714}
715
716/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
717 * the associations that are part of the endpoint indicating that a list of
718 * local addresses are removed from the endpoint.
719 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900720 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * association, we do not send the chunk for that association. But it will not
722 * affect other associations.
723 *
724 * Only sctp_setsockopt_bindx() is supposed to call this function.
725 */
726static int sctp_send_asconf_del_ip(struct sock *sk,
727 struct sockaddr *addrs,
728 int addrcnt)
729{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000730 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct sctp_sock *sp;
732 struct sctp_endpoint *ep;
733 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700734 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct sctp_bind_addr *bp;
736 struct sctp_chunk *chunk;
737 union sctp_addr *laddr;
738 void *addr_buf;
739 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700740 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 int i;
742 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900743 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Michio Honda8a07eb02011-04-26 20:19:36 +0900745 chunk = NULL;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000746 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return retval;
748
749 sp = sctp_sk(sk);
750 ep = sp->ep;
751
Daniel Borkmannbb333812013-06-28 19:49:40 +0200752 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
753 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Robert P. J. Day9dbc15f02008-04-12 18:54:24 -0700755 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (!asoc->peer.asconf_capable)
758 continue;
759
760 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
761 continue;
762
763 if (!sctp_state(asoc, ESTABLISHED))
764 continue;
765
766 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900767 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 * If so, do not send the asconf chunk to its peer, but
769 * continue with other associations.
770 */
771 addr_buf = addrs;
772 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000773 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 af = sctp_get_af_specific(laddr->v4.sin_family);
775 if (!af) {
776 retval = -EINVAL;
777 goto out;
778 }
779
780 if (!sctp_assoc_lookup_laddr(asoc, laddr))
781 break;
782
783 addr_buf += af->sockaddr_len;
784 }
785 if (i < addrcnt)
786 continue;
787
788 /* Find one address in the association's bind address list
789 * that is not in the packed array of addresses. This is to
790 * make sure that we do not delete all the addresses in the
791 * association.
792 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 bp = &asoc->base.bind_addr;
794 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
795 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900796 if ((laddr == NULL) && (addrcnt == 1)) {
797 if (asoc->asconf_addr_del_pending)
798 continue;
799 asoc->asconf_addr_del_pending =
800 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900801 if (asoc->asconf_addr_del_pending == NULL) {
802 retval = -ENOMEM;
803 goto out;
804 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900805 asoc->asconf_addr_del_pending->sa.sa_family =
806 addrs->sa_family;
807 asoc->asconf_addr_del_pending->v4.sin_port =
808 htons(bp->port);
809 if (addrs->sa_family == AF_INET) {
810 struct sockaddr_in *sin;
811
812 sin = (struct sockaddr_in *)addrs;
813 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
814 } else if (addrs->sa_family == AF_INET6) {
815 struct sockaddr_in6 *sin6;
816
817 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000818 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900819 }
Daniel Borkmannbb333812013-06-28 19:49:40 +0200820
821 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
822 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
823 asoc->asconf_addr_del_pending);
824
Michio Honda8a07eb02011-04-26 20:19:36 +0900825 asoc->src_out_of_asoc_ok = 1;
826 stored = 1;
827 goto skip_mkasconf;
828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Daniel Borkmann88362ad2013-09-07 20:51:21 +0200830 if (laddr == NULL)
831 return -EINVAL;
832
Vlad Yasevich559cf712007-09-16 16:03:28 -0700833 /* We do not need RCU protection throughout this loop
834 * because this is done under a socket lock from the
835 * setsockopt call.
836 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
838 SCTP_PARAM_DEL_IP);
839 if (!chunk) {
840 retval = -ENOMEM;
841 goto out;
842 }
843
Michio Honda8a07eb02011-04-26 20:19:36 +0900844skip_mkasconf:
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700845 /* Reset use_as_src flag for the addresses in the bind address
846 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700848 addr_buf = addrs;
849 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000850 laddr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700851 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700852 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a132006-11-20 17:05:23 -0800853 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800854 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700855 }
856 addr_buf += af->sockaddr_len;
857 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700858
859 /* Update the route and saddr entries for all the transports
860 * as some of the addresses in the bind address list are
861 * about to be deleted and cannot be used as source addresses.
862 */
Robert P. J. Day9dbc15f02008-04-12 18:54:24 -0700863 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
864 transports) {
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700865 sctp_transport_route(transport, NULL,
866 sctp_sk(asoc->base.sk));
867 }
868
Michio Honda8a07eb02011-04-26 20:19:36 +0900869 if (stored)
870 /* We don't need to transmit ASCONF */
871 continue;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700872 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874out:
875 return retval;
876}
877
Michio Honda9f7d6532011-04-26 19:32:51 +0900878/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
879int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
880{
881 struct sock *sk = sctp_opt2sk(sp);
882 union sctp_addr *addr;
883 struct sctp_af *af;
884
885 /* It is safe to write port space in caller. */
886 addr = &addrw->a;
887 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
888 af = sctp_get_af_specific(addr->sa.sa_family);
889 if (!af)
890 return -EINVAL;
891 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
892 return -EINVAL;
893
894 if (addrw->state == SCTP_ADDR_NEW)
895 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
896 else
897 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
898}
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
901 *
902 * API 8.1
903 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
904 * int flags);
905 *
906 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
907 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
908 * or IPv6 addresses.
909 *
910 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
911 * Section 3.1.2 for this usage.
912 *
913 * addrs is a pointer to an array of one or more socket addresses. Each
914 * address is contained in its appropriate structure (i.e. struct
915 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700916 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 * representation is termed a "packed array" of addresses). The caller
918 * specifies the number of addresses in the array with addrcnt.
919 *
920 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
921 * -1, and sets errno to the appropriate error code.
922 *
923 * For SCTP, the port given in each socket address must be the same, or
924 * sctp_bindx() will fail, setting errno to EINVAL.
925 *
926 * The flags parameter is formed from the bitwise OR of zero or more of
927 * the following currently defined flags:
928 *
929 * SCTP_BINDX_ADD_ADDR
930 *
931 * SCTP_BINDX_REM_ADDR
932 *
933 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
934 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
935 * addresses from the association. The two flags are mutually exclusive;
936 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
937 * not remove all addresses from an association; sctp_bindx() will
938 * reject such an attempt with EINVAL.
939 *
940 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
941 * additional addresses with an endpoint after calling bind(). Or use
942 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
943 * socket is associated with so that no new association accepted will be
944 * associated with those addresses. If the endpoint supports dynamic
945 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
946 * endpoint to send the appropriate message to the peer to change the
947 * peers address lists.
948 *
949 * Adding and removing addresses from a connected association is
950 * optional functionality. Implementations that do not support this
951 * functionality should return EOPNOTSUPP.
952 *
953 * Basically do nothing but copying the addresses from user to kernel
954 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700955 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
956 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
959 * it.
960 *
961 * sk The sk of the socket
962 * addrs The pointer to the addresses in user land
963 * addrssize Size of the addrs buffer
964 * op Operation to perform (add or remove, see the flags of
965 * sctp_bindx)
966 *
967 * Returns 0 if ok, <0 errno code on error.
968 */
wangweidong26ac8e52013-12-23 12:16:51 +0800969static int sctp_setsockopt_bindx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +0200970 struct sockaddr __user *addrs,
971 int addrs_size, int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 struct sockaddr *kaddrs;
974 int err;
975 int addrcnt = 0;
976 int walk_size = 0;
977 struct sockaddr *sa_addr;
978 void *addr_buf;
979 struct sctp_af *af;
980
Daniel Borkmannbb333812013-06-28 19:49:40 +0200981 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
982 __func__, sk, addrs, addrs_size, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 if (unlikely(addrs_size <= 0))
985 return -EINVAL;
986
Xin Longef82bcf2019-03-20 14:49:38 +0800987 kaddrs = memdup_user(addrs, addrs_size);
Al Viroc981f252018-01-07 13:19:09 -0500988 if (unlikely(IS_ERR(kaddrs)))
989 return PTR_ERR(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900991 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 addr_buf = kaddrs;
993 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +0000994 if (walk_size + sizeof(sa_family_t) > addrs_size) {
Xin Longef82bcf2019-03-20 14:49:38 +0800995 kfree(kaddrs);
Dan Rosenbergd7e0d192010-10-01 11:16:58 +0000996 return -EINVAL;
997 }
998
Joe Perchesea110732011-06-13 16:21:26 +0000999 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 af = sctp_get_af_specific(sa_addr->sa_family);
1001
1002 /* If the address family is not supported or if this address
1003 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
Xin Longef82bcf2019-03-20 14:49:38 +08001006 kfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return -EINVAL;
1008 }
1009 addrcnt++;
1010 addr_buf += af->sockaddr_len;
1011 walk_size += af->sockaddr_len;
1012 }
1013
1014 /* Do the work. */
1015 switch (op) {
1016 case SCTP_BINDX_ADD_ADDR:
Richard Haines2277c7c2018-02-13 20:56:24 +00001017 /* Allow security module to validate bindx addresses. */
1018 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1019 (struct sockaddr *)kaddrs,
1020 addrs_size);
1021 if (err)
1022 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1024 if (err)
1025 goto out;
1026 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1027 break;
1028
1029 case SCTP_BINDX_REM_ADDR:
1030 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1031 if (err)
1032 goto out;
1033 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1034 break;
1035
1036 default:
1037 err = -EINVAL;
1038 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041out:
Xin Longef82bcf2019-03-20 14:49:38 +08001042 kfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 return err;
1045}
1046
Frank Filz3f7a87d2005-06-20 13:14:57 -07001047/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1048 *
1049 * Common routine for handling connect() and sctp_connectx().
1050 * Connect will come in with just a single address.
1051 */
wangweidong26ac8e52013-12-23 12:16:51 +08001052static int __sctp_connect(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001053 struct sockaddr *kaddrs,
Xin Long644fbde2018-05-20 16:39:10 +08001054 int addrs_size, int flags,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001055 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001056{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001057 struct net *net = sock_net(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001058 struct sctp_sock *sp;
1059 struct sctp_endpoint *ep;
1060 struct sctp_association *asoc = NULL;
1061 struct sctp_association *asoc2;
1062 struct sctp_transport *transport;
1063 union sctp_addr to;
Xin Long1c662012017-08-05 19:59:54 +08001064 enum sctp_scope scope;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001065 long timeo;
1066 int err = 0;
1067 int addrcnt = 0;
1068 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001069 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001070 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001071 unsigned short port;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001072
1073 sp = sctp_sk(sk);
1074 ep = sp->ep;
1075
1076 /* connect() cannot be done on a socket that is already in ESTABLISHED
1077 * state - UDP-style peeled off socket or a TCP-style socket that
1078 * is already connected.
1079 * It cannot be done even on a TCP-style listening socket.
1080 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03001081 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
Frank Filz3f7a87d2005-06-20 13:14:57 -07001082 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1083 err = -EISCONN;
1084 goto out_free;
1085 }
1086
1087 /* Walk through the addrs buffer and count the number of addresses. */
1088 addr_buf = kaddrs;
1089 while (walk_size < addrs_size) {
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001090 struct sctp_af *af;
1091
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001092 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1093 err = -EINVAL;
1094 goto out_free;
1095 }
1096
Joe Perchesea110732011-06-13 16:21:26 +00001097 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001098 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001099
1100 /* If the address family is not supported or if this address
1101 * causes the address buffer to overflow return EINVAL.
1102 */
1103 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1104 err = -EINVAL;
1105 goto out_free;
1106 }
1107
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001108 port = ntohs(sa_addr->v4.sin_port);
1109
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001110 /* Save current address so we can work with it */
1111 memcpy(&to, sa_addr, af->sockaddr_len);
1112
1113 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001114 if (err)
1115 goto out_free;
1116
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001117 /* Make sure the destination port is correctly set
1118 * in all addresses.
1119 */
Wei Yongjun524fba62013-04-03 03:02:28 +00001120 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1121 err = -EINVAL;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001122 goto out_free;
Wei Yongjun524fba62013-04-03 03:02:28 +00001123 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001124
1125 /* Check if there already is a matching association on the
1126 * endpoint (other than the one created here).
1127 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001128 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001129 if (asoc2 && asoc2 != asoc) {
1130 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1131 err = -EISCONN;
1132 else
1133 err = -EALREADY;
1134 goto out_free;
1135 }
1136
1137 /* If we could not find a matching association on the endpoint,
1138 * make sure that there is no peeled-off association matching
1139 * the peer address even on another socket.
1140 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001141 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001142 err = -EADDRNOTAVAIL;
1143 goto out_free;
1144 }
1145
1146 if (!asoc) {
1147 /* If a bind() or sctp_bindx() is not called prior to
1148 * an sctp_connectx() call, the system picks an
1149 * ephemeral port and will choose an address set
1150 * equivalent to binding with a wildcard address.
1151 */
1152 if (!ep->base.bind_addr.port) {
1153 if (sctp_autobind(sk)) {
1154 err = -EAGAIN;
1155 goto out_free;
1156 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001157 } else {
1158 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001159 * If an unprivileged user inherits a 1-many
1160 * style socket with open associations on a
1161 * privileged port, it MAY be permitted to
1162 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001163 * be permitted to open new associations.
1164 */
Krister Johansen4548b682017-01-20 17:49:11 -08001165 if (ep->base.bind_addr.port <
1166 inet_prot_sock(net) &&
1167 !ns_capable(net->user_ns,
1168 CAP_NET_BIND_SERVICE)) {
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001169 err = -EACCES;
1170 goto out_free;
1171 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001172 }
1173
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001174 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001175 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1176 if (!asoc) {
1177 err = -ENOMEM;
1178 goto out_free;
1179 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001180
1181 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1182 GFP_KERNEL);
1183 if (err < 0) {
1184 goto out_free;
1185 }
1186
Frank Filz3f7a87d2005-06-20 13:14:57 -07001187 }
1188
1189 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001190 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001191 SCTP_UNKNOWN);
1192 if (!transport) {
1193 err = -ENOMEM;
1194 goto out_free;
1195 }
1196
1197 addrcnt++;
1198 addr_buf += af->sockaddr_len;
1199 walk_size += af->sockaddr_len;
1200 }
1201
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001202 /* In case the user of sctp_connectx() wants an association
1203 * id back, assign one now.
1204 */
1205 if (assoc_id) {
1206 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1207 if (err < 0)
1208 goto out_free;
1209 }
1210
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001211 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001212 if (err < 0) {
1213 goto out_free;
1214 }
1215
1216 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001217 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001218 sp->pf->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001219 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001220
Xin Long644fbde2018-05-20 16:39:10 +08001221 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001222
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001223 if (assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001224 *assoc_id = asoc->assoc_id;
Richard Haines2277c7c2018-02-13 20:56:24 +00001225
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001226 err = sctp_wait_for_connect(asoc, &timeo);
1227 /* Note: the asoc may be freed after the return of
1228 * sctp_wait_for_connect.
1229 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001230
1231 /* Don't free association on exit. */
1232 asoc = NULL;
1233
1234out_free:
Daniel Borkmannbb333812013-06-28 19:49:40 +02001235 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1236 __func__, asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001237
Neil Horman2eebc1e2012-07-16 09:13:51 +00001238 if (asoc) {
1239 /* sctp_primitive_ASSOCIATE may have added this association
1240 * To the hash table, try to unhash it, just in case, its a noop
1241 * if it wasn't hashed so we're safe
1242 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001243 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001244 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001245 return err;
1246}
1247
1248/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1249 *
1250 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001251 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1252 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001253 *
1254 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1255 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1256 * or IPv6 addresses.
1257 *
1258 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1259 * Section 3.1.2 for this usage.
1260 *
1261 * addrs is a pointer to an array of one or more socket addresses. Each
1262 * address is contained in its appropriate structure (i.e. struct
1263 * sockaddr_in or struct sockaddr_in6) the family of the address type
1264 * must be used to distengish the address length (note that this
1265 * representation is termed a "packed array" of addresses). The caller
1266 * specifies the number of addresses in the array with addrcnt.
1267 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001268 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1269 * the association id of the new association. On failure, sctp_connectx()
1270 * returns -1, and sets errno to the appropriate error code. The assoc_id
1271 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001272 *
1273 * For SCTP, the port given in each socket address must be the same, or
1274 * sctp_connectx() will fail, setting errno to EINVAL.
1275 *
1276 * An application can use sctp_connectx to initiate an association with
1277 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1278 * allows a caller to specify multiple addresses at which a peer can be
1279 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001280 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001281 * specifies that the stack will try to make use of all the addresses in
1282 * the list when needed.
1283 *
1284 * Note that the list of addresses passed in is only used for setting up
1285 * the association. It does not necessarily equal the set of addresses
1286 * the peer uses for the resulting association. If the caller wants to
1287 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1288 * retrieve them after the association has been set up.
1289 *
1290 * Basically do nothing but copying the addresses from user to kernel
1291 * land and invoking either sctp_connectx(). This is used for tunneling
1292 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1293 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07001294 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1295 * it.
1296 *
1297 * sk The sk of the socket
1298 * addrs The pointer to the addresses in user land
1299 * addrssize Size of the addrs buffer
1300 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001301 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001302 */
wangweidong26ac8e52013-12-23 12:16:51 +08001303static int __sctp_setsockopt_connectx(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001304 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001305 int addrs_size,
1306 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001307{
Frank Filz3f7a87d2005-06-20 13:14:57 -07001308 struct sockaddr *kaddrs;
Xin Long644fbde2018-05-20 16:39:10 +08001309 int err = 0, flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001310
Daniel Borkmannbb333812013-06-28 19:49:40 +02001311 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1312 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001313
1314 if (unlikely(addrs_size <= 0))
1315 return -EINVAL;
1316
Xin Longef82bcf2019-03-20 14:49:38 +08001317 kaddrs = memdup_user(addrs, addrs_size);
Al Viroc981f252018-01-07 13:19:09 -05001318 if (unlikely(IS_ERR(kaddrs)))
1319 return PTR_ERR(kaddrs);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001320
Richard Haines2277c7c2018-02-13 20:56:24 +00001321 /* Allow security module to validate connectx addresses. */
1322 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1323 (struct sockaddr *)kaddrs,
1324 addrs_size);
1325 if (err)
1326 goto out_free;
1327
Xin Long644fbde2018-05-20 16:39:10 +08001328 /* in-kernel sockets don't generally have a file allocated to them
1329 * if all they do is call sock_create_kern().
1330 */
1331 if (sk->sk_socket->file)
1332 flags = sk->sk_socket->file->f_flags;
1333
1334 err = __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
Richard Haines2277c7c2018-02-13 20:56:24 +00001335
1336out_free:
Xin Longef82bcf2019-03-20 14:49:38 +08001337 kfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001338
Frank Filz3f7a87d2005-06-20 13:14:57 -07001339 return err;
1340}
1341
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001342/*
1343 * This is an older interface. It's kept for backward compatibility
1344 * to the option that doesn't provide association id.
1345 */
wangweidong26ac8e52013-12-23 12:16:51 +08001346static int sctp_setsockopt_connectx_old(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001347 struct sockaddr __user *addrs,
1348 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001349{
1350 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1351}
1352
1353/*
1354 * New interface for the API. The since the API is done with a socket
1355 * option, to make it simple we feed back the association id is as a return
1356 * indication to the call. Error is always negative and association id is
1357 * always positive.
1358 */
wangweidong26ac8e52013-12-23 12:16:51 +08001359static int sctp_setsockopt_connectx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001360 struct sockaddr __user *addrs,
1361 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001362{
1363 sctp_assoc_t assoc_id = 0;
1364 int err = 0;
1365
1366 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1367
1368 if (err)
1369 return err;
1370 else
1371 return assoc_id;
1372}
1373
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001374/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001375 * New (hopefully final) interface for the API.
1376 * We use the sctp_getaddrs_old structure so that use-space library
Daniel Borkmannffd59392014-02-17 12:11:11 +01001377 * can avoid any unnecessary allocations. The only different part
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001378 * is that we store the actual length of the address buffer into the
Daniel Borkmannffd59392014-02-17 12:11:11 +01001379 * addrs_num structure member. That way we can re-use the existing
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001380 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001381 */
Daniel Borkmannffd59392014-02-17 12:11:11 +01001382#ifdef CONFIG_COMPAT
1383struct compat_sctp_getaddrs_old {
1384 sctp_assoc_t assoc_id;
1385 s32 addr_num;
1386 compat_uptr_t addrs; /* struct sockaddr * */
1387};
1388#endif
1389
wangweidong26ac8e52013-12-23 12:16:51 +08001390static int sctp_getsockopt_connectx3(struct sock *sk, int len,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001391 char __user *optval,
1392 int __user *optlen)
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001393{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001394 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001395 sctp_assoc_t assoc_id = 0;
1396 int err = 0;
1397
Daniel Borkmannffd59392014-02-17 12:11:11 +01001398#ifdef CONFIG_COMPAT
Andy Lutomirski96c0e0a2016-03-22 14:25:07 -07001399 if (in_compat_syscall()) {
Daniel Borkmannffd59392014-02-17 12:11:11 +01001400 struct compat_sctp_getaddrs_old param32;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001401
Daniel Borkmannffd59392014-02-17 12:11:11 +01001402 if (len < sizeof(param32))
1403 return -EINVAL;
1404 if (copy_from_user(&param32, optval, sizeof(param32)))
1405 return -EFAULT;
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001406
Daniel Borkmannffd59392014-02-17 12:11:11 +01001407 param.assoc_id = param32.assoc_id;
1408 param.addr_num = param32.addr_num;
1409 param.addrs = compat_ptr(param32.addrs);
1410 } else
1411#endif
1412 {
1413 if (len < sizeof(param))
1414 return -EINVAL;
1415 if (copy_from_user(&param, optval, sizeof(param)))
1416 return -EFAULT;
1417 }
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001418
Daniel Borkmannffd59392014-02-17 12:11:11 +01001419 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1420 param.addrs, param.addr_num,
1421 &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001422 if (err == 0 || err == -EINPROGRESS) {
1423 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1424 return -EFAULT;
1425 if (put_user(sizeof(assoc_id), optlen))
1426 return -EFAULT;
1427 }
1428
1429 return err;
1430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432/* API 3.1.4 close() - UDP Style Syntax
1433 * Applications use close() to perform graceful shutdown (as described in
1434 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1435 * by a UDP-style socket.
1436 *
1437 * The syntax is
1438 *
1439 * ret = close(int sd);
1440 *
1441 * sd - the socket descriptor of the associations to be closed.
1442 *
1443 * To gracefully shutdown a specific association represented by the
1444 * UDP-style socket, an application should use the sendmsg() call,
1445 * passing no user data, but including the appropriate flag in the
1446 * ancillary data (see Section xxxx).
1447 *
1448 * If sd in the close() call is a branched-off socket representing only
1449 * one association, the shutdown is performed on that association only.
1450 *
1451 * 4.1.6 close() - TCP Style Syntax
1452 *
1453 * Applications use close() to gracefully close down an association.
1454 *
1455 * The syntax is:
1456 *
1457 * int close(int sd);
1458 *
1459 * sd - the socket descriptor of the association to be closed.
1460 *
1461 * After an application calls close() on a socket descriptor, no further
1462 * socket operations will succeed on that descriptor.
1463 *
1464 * API 7.1.4 SO_LINGER
1465 *
1466 * An application using the TCP-style socket can use this option to
1467 * perform the SCTP ABORT primitive. The linger option structure is:
1468 *
1469 * struct linger {
1470 * int l_onoff; // option on/off
1471 * int l_linger; // linger time
1472 * };
1473 *
1474 * To enable the option, set l_onoff to 1. If the l_linger value is set
1475 * to 0, calling close() is the same as the ABORT primitive. If the
1476 * value is set to a negative value, the setsockopt() call will return
1477 * an error. If the value is set to a positive value linger_time, the
1478 * close() can be blocked for at most linger_time ms. If the graceful
1479 * shutdown phase does not finish during this period, close() will
1480 * return but the graceful shutdown phase continues in the system.
1481 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02001482static void sctp_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001484 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 struct sctp_endpoint *ep;
1486 struct sctp_association *asoc;
1487 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001488 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Daniel Borkmannbb333812013-06-28 19:49:40 +02001490 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Xin Long6dfe4b92017-06-10 14:56:56 +08001492 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 sk->sk_shutdown = SHUTDOWN_MASK;
Yafang Shaocbabf462017-12-20 11:12:54 +08001494 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 ep = sctp_sk(sk)->ep;
1497
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001498 /* Clean up any skbs sitting on the receive queue. */
1499 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1500 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1501
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001502 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 list_for_each_safe(pos, temp, &ep->asocs) {
1504 asoc = list_entry(pos, struct sctp_association, asocs);
1505
1506 if (sctp_style(sk, TCP)) {
1507 /* A closed association can still be in the list if
1508 * it belongs to a TCP-style listening socket that is
1509 * not yet accepted. If so, free it. If not, send an
1510 * ABORT or SHUTDOWN based on the linger options.
1511 */
1512 if (sctp_state(asoc, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001514 continue;
1515 }
1516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001518 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1519 !skb_queue_empty(&asoc->ulpq.reasm) ||
Xin Long13228232017-12-08 21:04:09 +08001520 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001521 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001522 struct sctp_chunk *chunk;
1523
1524 chunk = sctp_make_abort_user(asoc, NULL, 0);
Xin Long068d8bd2015-12-29 17:49:25 +08001525 sctp_primitive_ABORT(net, asoc, chunk);
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001526 } else
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001527 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /* On a TCP-style socket, block for at most linger_time if set. */
1531 if (sctp_style(sk, TCP) && timeout)
1532 sctp_wait_for_close(sk, timeout);
1533
1534 /* This will run the backlog queue. */
wangweidong048ed4b2014-01-21 15:44:11 +08001535 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 /* Supposedly, no process has access to the socket, but
1538 * the net layers still may.
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001539 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1540 * held and that should be grabbed before socket lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001542 spin_lock_bh(&net->sctp.addr_wq_lock);
Xin Long6dfe4b92017-06-10 14:56:56 +08001543 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 /* Hold the sock, since sk_common_release() will put sock_put()
1546 * and we have just a little more cleanup.
1547 */
1548 sock_hold(sk);
1549 sk_common_release(sk);
1550
wangweidong5bc1d1b2014-01-21 15:44:12 +08001551 bh_unlock_sock(sk);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001552 spin_unlock_bh(&net->sctp.addr_wq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 sock_put(sk);
1555
1556 SCTP_DBG_OBJCNT_DEC(sock);
1557}
1558
1559/* Handle EPIPE error. */
1560static int sctp_error(struct sock *sk, int flags, int err)
1561{
1562 if (err == -EPIPE)
1563 err = sock_error(sk) ? : -EPIPE;
1564 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1565 send_sig(SIGPIPE, current, 0);
1566 return err;
1567}
1568
1569/* API 3.1.3 sendmsg() - UDP Style Syntax
1570 *
1571 * An application uses sendmsg() and recvmsg() calls to transmit data to
1572 * and receive data from its peer.
1573 *
1574 * ssize_t sendmsg(int socket, const struct msghdr *message,
1575 * int flags);
1576 *
1577 * socket - the socket descriptor of the endpoint.
1578 * message - pointer to the msghdr structure which contains a single
1579 * user message and possibly some ancillary data.
1580 *
1581 * See Section 5 for complete description of the data
1582 * structures.
1583 *
1584 * flags - flags sent or received with the user message, see Section
1585 * 5 for complete description of the flags.
1586 *
1587 * Note: This function could use a rewrite especially when explicit
1588 * connect support comes in.
1589 */
1590/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1591
Xin Longa05437a2017-08-11 10:23:48 +08001592static int sctp_msghdr_parse(const struct msghdr *msg,
1593 struct sctp_cmsgs *cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Xin Long204f8172018-03-01 23:05:14 +08001595static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1596 struct sctp_sndrcvinfo *srinfo,
1597 const struct msghdr *msg, size_t msg_len)
1598{
1599 __u16 sflags;
1600 int err;
1601
1602 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1603 return -EPIPE;
1604
1605 if (msg_len > sk->sk_sndbuf)
1606 return -EMSGSIZE;
1607
1608 memset(cmsgs, 0, sizeof(*cmsgs));
1609 err = sctp_msghdr_parse(msg, cmsgs);
1610 if (err) {
1611 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1612 return err;
1613 }
1614
1615 memset(srinfo, 0, sizeof(*srinfo));
1616 if (cmsgs->srinfo) {
1617 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1618 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1619 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1620 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1621 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1622 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1623 }
1624
1625 if (cmsgs->sinfo) {
1626 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1627 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1628 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1629 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1630 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1631 }
1632
Xin Longed63afb2018-03-05 20:44:18 +08001633 if (cmsgs->prinfo) {
1634 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1635 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1636 cmsgs->prinfo->pr_policy);
1637 }
1638
Xin Long204f8172018-03-01 23:05:14 +08001639 sflags = srinfo->sinfo_flags;
1640 if (!sflags && msg_len)
1641 return 0;
1642
1643 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1644 return -EINVAL;
1645
1646 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1647 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1648 return -EINVAL;
1649
1650 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1651 return -EINVAL;
1652
1653 return 0;
1654}
1655
Xin Long2bfd80f2018-03-01 23:05:11 +08001656static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1657 struct sctp_cmsgs *cmsgs,
1658 union sctp_addr *daddr,
1659 struct sctp_transport **tp)
1660{
1661 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1662 struct net *net = sock_net(sk);
1663 struct sctp_association *asoc;
1664 enum sctp_scope scope;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001665 struct cmsghdr *cmsg;
Xin Long4be41392018-07-02 18:21:14 +08001666 __be32 flowinfo = 0;
Linus Torvalds9eda2d22018-04-06 15:39:26 -07001667 struct sctp_af *af;
Wei Yongjund98985d2018-03-13 03:03:30 +00001668 int err;
Xin Long2bfd80f2018-03-01 23:05:11 +08001669
1670 *tp = NULL;
1671
1672 if (sflags & (SCTP_EOF | SCTP_ABORT))
1673 return -EINVAL;
1674
1675 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1676 sctp_sstate(sk, CLOSING)))
1677 return -EADDRNOTAVAIL;
1678
1679 if (sctp_endpoint_is_peeled_off(ep, daddr))
1680 return -EADDRNOTAVAIL;
1681
1682 if (!ep->base.bind_addr.port) {
1683 if (sctp_autobind(sk))
1684 return -EAGAIN;
1685 } else {
1686 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1687 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1688 return -EACCES;
1689 }
1690
1691 scope = sctp_scope(daddr);
1692
Linus Torvalds9eda2d22018-04-06 15:39:26 -07001693 /* Label connection socket for first association 1-to-many
1694 * style for client sequence socket()->sendmsg(). This
1695 * needs to be done before sctp_assoc_add_peer() as that will
1696 * set up the initial packet that needs to account for any
1697 * security ip options (CIPSO/CALIPSO) added to the packet.
1698 */
1699 af = sctp_get_af_specific(daddr->sa.sa_family);
1700 if (!af)
1701 return -EINVAL;
1702 err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1703 (struct sockaddr *)daddr,
1704 af->sockaddr_len);
1705 if (err < 0)
1706 return err;
1707
Xin Long2bfd80f2018-03-01 23:05:11 +08001708 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1709 if (!asoc)
1710 return -ENOMEM;
1711
1712 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1713 err = -ENOMEM;
1714 goto free;
1715 }
1716
1717 if (cmsgs->init) {
1718 struct sctp_initmsg *init = cmsgs->init;
1719
1720 if (init->sinit_num_ostreams) {
1721 __u16 outcnt = init->sinit_num_ostreams;
1722
1723 asoc->c.sinit_num_ostreams = outcnt;
1724 /* outcnt has been changed, need to re-init stream */
1725 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1726 GFP_KERNEL);
1727 if (err)
1728 goto free;
1729 }
1730
1731 if (init->sinit_max_instreams)
1732 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1733
1734 if (init->sinit_max_attempts)
1735 asoc->max_init_attempts = init->sinit_max_attempts;
1736
1737 if (init->sinit_max_init_timeo)
1738 asoc->max_init_timeo =
1739 msecs_to_jiffies(init->sinit_max_init_timeo);
1740 }
1741
1742 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1743 if (!*tp) {
1744 err = -ENOMEM;
1745 goto free;
1746 }
1747
Xin Long2c0dbaa2018-03-05 20:44:19 +08001748 if (!cmsgs->addrs_msg)
1749 return 0;
1750
Xin Long4be41392018-07-02 18:21:14 +08001751 if (daddr->sa.sa_family == AF_INET6)
1752 flowinfo = daddr->v6.sin6_flowinfo;
1753
Xin Long2c0dbaa2018-03-05 20:44:19 +08001754 /* sendv addr list parse */
1755 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1756 struct sctp_transport *transport;
1757 struct sctp_association *old;
1758 union sctp_addr _daddr;
1759 int dlen;
1760
1761 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1762 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1763 cmsg->cmsg_type != SCTP_DSTADDRV6))
1764 continue;
1765
1766 daddr = &_daddr;
1767 memset(daddr, 0, sizeof(*daddr));
1768 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1769 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
Wei Yongjund98985d2018-03-13 03:03:30 +00001770 if (dlen < sizeof(struct in_addr)) {
1771 err = -EINVAL;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001772 goto free;
Wei Yongjund98985d2018-03-13 03:03:30 +00001773 }
Xin Long2c0dbaa2018-03-05 20:44:19 +08001774
1775 dlen = sizeof(struct in_addr);
1776 daddr->v4.sin_family = AF_INET;
1777 daddr->v4.sin_port = htons(asoc->peer.port);
1778 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1779 } else {
Wei Yongjund98985d2018-03-13 03:03:30 +00001780 if (dlen < sizeof(struct in6_addr)) {
1781 err = -EINVAL;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001782 goto free;
Wei Yongjund98985d2018-03-13 03:03:30 +00001783 }
Xin Long2c0dbaa2018-03-05 20:44:19 +08001784
1785 dlen = sizeof(struct in6_addr);
Xin Long4be41392018-07-02 18:21:14 +08001786 daddr->v6.sin6_flowinfo = flowinfo;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001787 daddr->v6.sin6_family = AF_INET6;
1788 daddr->v6.sin6_port = htons(asoc->peer.port);
1789 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1790 }
1791 err = sctp_verify_addr(sk, daddr, sizeof(*daddr));
1792 if (err)
1793 goto free;
1794
1795 old = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1796 if (old && old != asoc) {
1797 if (old->state >= SCTP_STATE_ESTABLISHED)
1798 err = -EISCONN;
1799 else
1800 err = -EALREADY;
1801 goto free;
1802 }
1803
1804 if (sctp_endpoint_is_peeled_off(ep, daddr)) {
1805 err = -EADDRNOTAVAIL;
1806 goto free;
1807 }
1808
1809 transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL,
1810 SCTP_UNKNOWN);
1811 if (!transport) {
1812 err = -ENOMEM;
1813 goto free;
1814 }
1815 }
1816
Xin Long2bfd80f2018-03-01 23:05:11 +08001817 return 0;
1818
1819free:
1820 sctp_association_free(asoc);
1821 return err;
1822}
1823
Xin Longc2666de2018-03-01 23:05:12 +08001824static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1825 __u16 sflags, struct msghdr *msg,
1826 size_t msg_len)
1827{
1828 struct sock *sk = asoc->base.sk;
1829 struct net *net = sock_net(sk);
1830
1831 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1832 return -EPIPE;
1833
Xin Long49102802018-03-05 20:44:20 +08001834 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1835 !sctp_state(asoc, ESTABLISHED))
1836 return 0;
1837
Xin Longc2666de2018-03-01 23:05:12 +08001838 if (sflags & SCTP_EOF) {
1839 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1840 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1841
1842 return 0;
1843 }
1844
1845 if (sflags & SCTP_ABORT) {
1846 struct sctp_chunk *chunk;
1847
1848 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1849 if (!chunk)
1850 return -ENOMEM;
1851
1852 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1853 sctp_primitive_ABORT(net, asoc, chunk);
Xin Long901efe12019-03-03 16:50:26 +08001854 iov_iter_revert(&msg->msg_iter, msg_len);
Xin Longc2666de2018-03-01 23:05:12 +08001855
1856 return 0;
1857 }
1858
1859 return 1;
1860}
1861
Xin Longf84af332018-03-01 23:05:10 +08001862static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1863 struct msghdr *msg, size_t msg_len,
1864 struct sctp_transport *transport,
1865 struct sctp_sndrcvinfo *sinfo)
1866{
1867 struct sock *sk = asoc->base.sk;
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001868 struct sctp_sock *sp = sctp_sk(sk);
Xin Longf84af332018-03-01 23:05:10 +08001869 struct net *net = sock_net(sk);
1870 struct sctp_datamsg *datamsg;
1871 bool wait_connect = false;
1872 struct sctp_chunk *chunk;
1873 long timeo;
1874 int err;
1875
1876 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1877 err = -EINVAL;
1878 goto err;
1879 }
1880
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001881 if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
Xin Longf84af332018-03-01 23:05:10 +08001882 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1883 if (err)
1884 goto err;
1885 }
1886
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001887 if (sp->disable_fragments && msg_len > asoc->frag_point) {
Xin Longf84af332018-03-01 23:05:10 +08001888 err = -EMSGSIZE;
1889 goto err;
1890 }
1891
Marcelo Ricardo Leitner25216802018-04-26 16:58:56 -03001892 if (asoc->pmtu_pending) {
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001893 if (sp->param_flags & SPP_PMTUD_ENABLE)
1894 sctp_assoc_sync_pmtu(asoc);
Marcelo Ricardo Leitner25216802018-04-26 16:58:56 -03001895 asoc->pmtu_pending = 0;
1896 }
Neil Horman0aee4c22018-03-12 14:15:25 -04001897
Xin Longcd305c72018-10-17 03:07:51 +08001898 if (sctp_wspace(asoc) < (int)msg_len)
Neil Horman0aee4c22018-03-12 14:15:25 -04001899 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1900
Xin Long10339902019-04-15 17:15:06 +08001901 if (sk_under_memory_pressure(sk))
1902 sk_mem_reclaim(sk);
1903
1904 if (sctp_wspace(asoc) <= 0 || !sk_wmem_schedule(sk, msg_len)) {
Neil Horman0aee4c22018-03-12 14:15:25 -04001905 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1906 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1907 if (err)
1908 goto err;
1909 }
1910
Xin Longf84af332018-03-01 23:05:10 +08001911 if (sctp_state(asoc, CLOSED)) {
1912 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1913 if (err)
1914 goto err;
1915
Xin Longe55f4b82019-07-09 00:57:07 +08001916 if (asoc->ep->intl_enable) {
Xin Longf84af332018-03-01 23:05:10 +08001917 timeo = sock_sndtimeo(sk, 0);
1918 err = sctp_wait_for_connect(asoc, &timeo);
Xin Longc8638502018-10-17 03:06:12 +08001919 if (err) {
1920 err = -ESRCH;
Xin Longf84af332018-03-01 23:05:10 +08001921 goto err;
Xin Longc8638502018-10-17 03:06:12 +08001922 }
Xin Longf84af332018-03-01 23:05:10 +08001923 } else {
1924 wait_connect = true;
1925 }
1926
1927 pr_debug("%s: we associated primitively\n", __func__);
1928 }
1929
Xin Longf84af332018-03-01 23:05:10 +08001930 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1931 if (IS_ERR(datamsg)) {
1932 err = PTR_ERR(datamsg);
1933 goto err;
1934 }
1935
1936 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1937
1938 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1939 sctp_chunk_hold(chunk);
1940 sctp_set_owner_w(chunk);
1941 chunk->transport = transport;
1942 }
1943
1944 err = sctp_primitive_SEND(net, asoc, datamsg);
1945 if (err) {
1946 sctp_datamsg_free(datamsg);
1947 goto err;
1948 }
1949
1950 pr_debug("%s: we sent primitively\n", __func__);
1951
1952 sctp_datamsg_put(datamsg);
1953
1954 if (unlikely(wait_connect)) {
1955 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1956 sctp_wait_for_connect(asoc, &timeo);
1957 }
1958
1959 err = msg_len;
1960
1961err:
1962 return err;
1963}
1964
Xin Longbecef9b2018-03-01 23:05:13 +08001965static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1966 const struct msghdr *msg,
1967 struct sctp_cmsgs *cmsgs)
1968{
1969 union sctp_addr *daddr = NULL;
1970 int err;
1971
1972 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1973 int len = msg->msg_namelen;
1974
1975 if (len > sizeof(*daddr))
1976 len = sizeof(*daddr);
1977
1978 daddr = (union sctp_addr *)msg->msg_name;
1979
1980 err = sctp_verify_addr(sk, daddr, len);
1981 if (err)
1982 return ERR_PTR(err);
1983 }
1984
1985 return daddr;
1986}
1987
Xin Longd42cb062018-03-01 23:05:15 +08001988static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
1989 struct sctp_sndrcvinfo *sinfo,
1990 struct sctp_cmsgs *cmsgs)
1991{
1992 if (!cmsgs->srinfo && !cmsgs->sinfo) {
1993 sinfo->sinfo_stream = asoc->default_stream;
1994 sinfo->sinfo_ppid = asoc->default_ppid;
1995 sinfo->sinfo_context = asoc->default_context;
1996 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
Xin Longed63afb2018-03-05 20:44:18 +08001997
1998 if (!cmsgs->prinfo)
1999 sinfo->sinfo_flags = asoc->default_flags;
Xin Longd42cb062018-03-01 23:05:15 +08002000 }
2001
Xin Longed63afb2018-03-05 20:44:18 +08002002 if (!cmsgs->srinfo && !cmsgs->prinfo)
Xin Longd42cb062018-03-01 23:05:15 +08002003 sinfo->sinfo_timetolive = asoc->default_timetolive;
Xin Long3ff547c2018-03-14 19:05:31 +08002004
2005 if (cmsgs->authinfo) {
2006 /* Reuse sinfo_tsn to indicate that authinfo was set and
2007 * sinfo_ssn to save the keyid on tx path.
2008 */
2009 sinfo->sinfo_tsn = 1;
2010 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
2011 }
Xin Longd42cb062018-03-01 23:05:15 +08002012}
2013
Ying Xue1b784142015-03-02 15:37:48 +08002014static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
Xin Long204f8172018-03-01 23:05:14 +08002016 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Xin Long8e87c6e2018-03-01 23:05:16 +08002017 struct sctp_transport *transport = NULL;
Xin Long204f8172018-03-01 23:05:14 +08002018 struct sctp_sndrcvinfo _sinfo, *sinfo;
Greg Kroah-Hartmanba59fb02019-02-01 15:15:22 +01002019 struct sctp_association *asoc, *tmp;
Xin Long007b7e12018-03-01 23:05:17 +08002020 struct sctp_cmsgs cmsgs;
Xin Longbecef9b2018-03-01 23:05:13 +08002021 union sctp_addr *daddr;
Xin Long007b7e12018-03-01 23:05:17 +08002022 bool new = false;
2023 __u16 sflags;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02002024 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Xin Long204f8172018-03-01 23:05:14 +08002026 /* Parse and get snd_info */
2027 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
2028 if (err)
Xin Long007b7e12018-03-01 23:05:17 +08002029 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Xin Long204f8172018-03-01 23:05:14 +08002031 sinfo = &_sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08002032 sflags = sinfo->sinfo_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Xin Longbecef9b2018-03-01 23:05:13 +08002034 /* Get daddr from msg */
2035 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
2036 if (IS_ERR(daddr)) {
2037 err = PTR_ERR(daddr);
Xin Long007b7e12018-03-01 23:05:17 +08002038 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
2040
wangweidong048ed4b2014-01-21 15:44:11 +08002041 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Xin Long49102802018-03-05 20:44:20 +08002043 /* SCTP_SENDALL process */
2044 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
Greg Kroah-Hartmanba59fb02019-02-01 15:15:22 +01002045 list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
Xin Long49102802018-03-05 20:44:20 +08002046 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2047 msg_len);
2048 if (err == 0)
2049 continue;
2050 if (err < 0)
2051 goto out_unlock;
2052
2053 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2054
2055 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
2056 NULL, sinfo);
2057 if (err < 0)
2058 goto out_unlock;
2059
2060 iov_iter_revert(&msg->msg_iter, err);
2061 }
2062
2063 goto out_unlock;
2064 }
2065
Xin Long0a3920d2018-03-01 23:05:18 +08002066 /* Get and check or create asoc */
Xin Longbecef9b2018-03-01 23:05:13 +08002067 if (daddr) {
Xin Longbecef9b2018-03-01 23:05:13 +08002068 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
Xin Long0a3920d2018-03-01 23:05:18 +08002069 if (asoc) {
2070 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2071 msg_len);
2072 if (err <= 0)
2073 goto out_unlock;
2074 } else {
2075 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2076 &transport);
2077 if (err)
2078 goto out_unlock;
2079
2080 asoc = transport->asoc;
2081 new = true;
2082 }
2083
2084 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2085 transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 } else {
Xin Long007b7e12018-03-01 23:05:17 +08002087 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (!asoc) {
2089 err = -EPIPE;
2090 goto out_unlock;
2091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Xin Long007b7e12018-03-01 23:05:17 +08002093 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
Xin Longc2666de2018-03-01 23:05:12 +08002094 if (err <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097
Xin Longd42cb062018-03-01 23:05:15 +08002098 /* Update snd_info with the asoc */
2099 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Xin Longf84af332018-03-01 23:05:10 +08002101 /* Send msg to the asoc */
Xin Long8e87c6e2018-03-01 23:05:16 +08002102 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
Xin Long007b7e12018-03-01 23:05:17 +08002103 if (err < 0 && err != -ESRCH && new)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 sctp_association_free(asoc);
Xin Long8e87c6e2018-03-01 23:05:16 +08002105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106out_unlock:
wangweidong048ed4b2014-01-21 15:44:11 +08002107 release_sock(sk);
Xin Long007b7e12018-03-01 23:05:17 +08002108out:
Xin Longf84af332018-03-01 23:05:10 +08002109 return sctp_error(sk, msg->msg_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
2112/* This is an extended version of skb_pull() that removes the data from the
2113 * start of a skb even when data is spread across the list of skb's in the
2114 * frag_list. len specifies the total amount of data that needs to be removed.
2115 * when 'len' bytes could be removed from the skb, it returns 0.
2116 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2117 * could not be removed.
2118 */
2119static int sctp_skb_pull(struct sk_buff *skb, int len)
2120{
2121 struct sk_buff *list;
2122 int skb_len = skb_headlen(skb);
2123 int rlen;
2124
2125 if (len <= skb_len) {
2126 __skb_pull(skb, len);
2127 return 0;
2128 }
2129 len -= skb_len;
2130 __skb_pull(skb, skb_len);
2131
David S. Miller1b003be2009-06-09 00:22:35 -07002132 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 rlen = sctp_skb_pull(list, len);
2134 skb->len -= (len-rlen);
2135 skb->data_len -= (len-rlen);
2136
2137 if (!rlen)
2138 return 0;
2139
2140 len = rlen;
2141 }
2142
2143 return len;
2144}
2145
2146/* API 3.1.3 recvmsg() - UDP Style Syntax
2147 *
2148 * ssize_t recvmsg(int socket, struct msghdr *message,
2149 * int flags);
2150 *
2151 * socket - the socket descriptor of the endpoint.
2152 * message - pointer to the msghdr structure which contains a single
2153 * user message and possibly some ancillary data.
2154 *
2155 * See Section 5 for complete description of the data
2156 * structures.
2157 *
2158 * flags - flags sent or received with the user message, see Section
2159 * 5 for complete description of the flags.
2160 */
Ying Xue1b784142015-03-02 15:37:48 +08002161static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2162 int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 struct sctp_ulpevent *event = NULL;
2165 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002166 struct sk_buff *skb, *head_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 int copied;
2168 int err = 0;
2169 int skb_len;
2170
Daniel Borkmannbb333812013-06-28 19:49:40 +02002171 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2172 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2173 addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
wangweidong048ed4b2014-01-21 15:44:11 +08002175 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03002177 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
Xin Longe0878692016-07-30 14:14:41 +08002178 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 err = -ENOTCONN;
2180 goto out;
2181 }
2182
2183 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2184 if (!skb)
2185 goto out;
2186
2187 /* Get the total length of the skb including any skb's in the
2188 * frag_list.
2189 */
2190 skb_len = skb->len;
2191
2192 copied = skb_len;
2193 if (copied > len)
2194 copied = len;
2195
David S. Miller51f3d022014-11-05 16:46:40 -05002196 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 event = sctp_skb2event(skb);
2199
2200 if (err)
2201 goto out_free;
2202
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002203 if (event->chunk && event->chunk->head_skb)
2204 head_skb = event->chunk->head_skb;
2205 else
2206 head_skb = skb;
2207 sock_recv_ts_and_drops(msg, sk, head_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (sctp_ulpevent_is_notification(event)) {
2209 msg->msg_flags |= MSG_NOTIFICATION;
2210 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2211 } else {
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002212 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 }
2214
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02002215 /* Check if we allow SCTP_NXTINFO. */
2216 if (sp->recvnxtinfo)
2217 sctp_ulpevent_read_nxtinfo(event, msg, sk);
Geir Ola Vaagland0d3a421d2014-07-12 20:30:37 +02002218 /* Check if we allow SCTP_RCVINFO. */
2219 if (sp->recvrcvinfo)
2220 sctp_ulpevent_read_rcvinfo(event, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 /* Check if we allow SCTP_SNDRCVINFO. */
Xin Long2cc0eeb2018-11-18 16:08:51 +08002222 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_DATA_IO_EVENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 sctp_ulpevent_read_sndrcvinfo(event, msg);
Geir Ola Vaagland0d3a421d2014-07-12 20:30:37 +02002224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 err = copied;
2226
2227 /* If skb's length exceeds the user's buffer, update the skb and
2228 * push it back to the receive_queue so that the next call to
2229 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2230 */
2231 if (skb_len > copied) {
2232 msg->msg_flags &= ~MSG_EOR;
2233 if (flags & MSG_PEEK)
2234 goto out_free;
2235 sctp_skb_pull(skb, copied);
2236 skb_queue_head(&sk->sk_receive_queue, skb);
2237
Daniel Borkmann362d5202014-04-14 21:45:17 +02002238 /* When only partial message is copied to the user, increase
2239 * rwnd by that amount. If all the data in the skb is read,
2240 * rwnd is updated when the event is freed.
2241 */
2242 if (!sctp_ulpevent_is_notification(event))
2243 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 goto out;
2245 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2246 (event->msg_flags & MSG_EOR))
2247 msg->msg_flags |= MSG_EOR;
2248 else
2249 msg->msg_flags &= ~MSG_EOR;
2250
2251out_free:
2252 if (flags & MSG_PEEK) {
2253 /* Release the skb reference acquired after peeking the skb in
2254 * sctp_skb_recv_datagram().
2255 */
2256 kfree_skb(skb);
2257 } else {
2258 /* Free the event which includes releasing the reference to
2259 * the owner of the skb, freeing the skb and updating the
2260 * rwnd.
2261 */
2262 sctp_ulpevent_free(event);
2263 }
2264out:
wangweidong048ed4b2014-01-21 15:44:11 +08002265 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return err;
2267}
2268
2269/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2270 *
2271 * This option is a on/off flag. If enabled no SCTP message
2272 * fragmentation will be performed. Instead if a message being sent
2273 * exceeds the current PMTU size, the message will NOT be sent and
2274 * instead a error will be indicated to the user.
2275 */
2276static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002277 char __user *optval,
2278 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
2280 int val;
2281
2282 if (optlen < sizeof(int))
2283 return -EINVAL;
2284
2285 if (get_user(val, (int __user *)optval))
2286 return -EFAULT;
2287
2288 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2289
2290 return 0;
2291}
2292
2293static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002294 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
Xin Long2cc0eeb2018-11-18 16:08:51 +08002296 struct sctp_event_subscribe subscribe;
2297 __u8 *sn_type = (__u8 *)&subscribe;
2298 struct sctp_sock *sp = sctp_sk(sk);
Xin Longa1e3a052018-11-18 16:08:52 +08002299 struct sctp_association *asoc;
Xin Long2cc0eeb2018-11-18 16:08:51 +08002300 int i;
Wei Yongjun94912302011-07-02 09:28:04 +00002301
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002302 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return -EINVAL;
Xin Long2cc0eeb2018-11-18 16:08:51 +08002304
2305 if (copy_from_user(&subscribe, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002307
Xin Long2cc0eeb2018-11-18 16:08:51 +08002308 for (i = 0; i < optlen; i++)
2309 sctp_ulpevent_type_set(&sp->subscribe, SCTP_SN_TYPE_BASE + i,
2310 sn_type[i]);
2311
Xin Longa1e3a052018-11-18 16:08:52 +08002312 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2313 asoc->subscribe = sctp_sk(sk)->subscribe;
2314
Daniel Borkmannbbbea412014-07-12 20:30:40 +02002315 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
Wei Yongjun94912302011-07-02 09:28:04 +00002316 * if there is no data to be sent or retransmit, the stack will
2317 * immediately send up this notification.
2318 */
Xin Long2cc0eeb2018-11-18 16:08:51 +08002319 if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_SENDER_DRY_EVENT)) {
Xin Long2cc0eeb2018-11-18 16:08:51 +08002320 struct sctp_ulpevent *event;
Wei Yongjun94912302011-07-02 09:28:04 +00002321
Xin Longa1e3a052018-11-18 16:08:52 +08002322 asoc = sctp_id2assoc(sk, 0);
Wei Yongjun94912302011-07-02 09:28:04 +00002323 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2324 event = sctp_ulpevent_make_sender_dry_event(asoc,
Marcelo Ricardo Leitner2e83acb2018-01-08 19:02:27 -02002325 GFP_USER | __GFP_NOWARN);
Wei Yongjun94912302011-07-02 09:28:04 +00002326 if (!event)
2327 return -ENOMEM;
2328
Xin Long9162e0e2017-12-08 21:04:05 +08002329 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
Wei Yongjun94912302011-07-02 09:28:04 +00002330 }
2331 }
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 return 0;
2334}
2335
2336/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2337 *
2338 * This socket option is applicable to the UDP-style socket only. When
2339 * set it will cause associations that are idle for more than the
2340 * specified number of seconds to automatically close. An association
2341 * being idle is defined an association that has NOT sent or received
2342 * user data. The special value of '0' indicates that no automatic
2343 * close of any associations should be performed. The option expects an
2344 * integer defining the number of seconds of idle time before an
2345 * association is closed.
2346 */
2347static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002348 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
2350 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman9f70f462013-12-10 06:48:15 -05002351 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 /* Applicable to UDP-style socket only */
2354 if (sctp_style(sk, TCP))
2355 return -EOPNOTSUPP;
2356 if (optlen != sizeof(int))
2357 return -EINVAL;
2358 if (copy_from_user(&sp->autoclose, optval, optlen))
2359 return -EFAULT;
2360
Neil Horman9f70f462013-12-10 06:48:15 -05002361 if (sp->autoclose > net->sctp.max_autoclose)
2362 sp->autoclose = net->sctp.max_autoclose;
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 return 0;
2365}
2366
2367/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2368 *
2369 * Applications can enable or disable heartbeats for any peer address of
2370 * an association, modify an address's heartbeat interval, force a
2371 * heartbeat to be sent immediately, and adjust the address's maximum
2372 * number of retransmissions sent before an address is considered
2373 * unreachable. The following structure is used to access and modify an
2374 * address's parameters:
2375 *
2376 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002377 * sctp_assoc_t spp_assoc_id;
2378 * struct sockaddr_storage spp_address;
2379 * uint32_t spp_hbinterval;
2380 * uint16_t spp_pathmaxrxt;
2381 * uint32_t spp_pathmtu;
2382 * uint32_t spp_sackdelay;
2383 * uint32_t spp_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08002384 * uint32_t spp_ipv6_flowlabel;
2385 * uint8_t spp_dscp;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002386 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002388 * spp_assoc_id - (one-to-many style socket) This is filled in the
2389 * application, and identifies the association for
2390 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 * spp_address - This specifies which address is of interest.
2392 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002393 * in milliseconds. If a value of zero
2394 * is present in this field then no changes are to
2395 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 * spp_pathmaxrxt - This contains the maximum number of
2397 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002398 * considered unreachable. If a value of zero
2399 * is present in this field then no changes are to
2400 * be made to this parameter.
2401 * spp_pathmtu - When Path MTU discovery is disabled the value
2402 * specified here will be the "fixed" path mtu.
2403 * Note that if the spp_address field is empty
2404 * then all associations on this address will
2405 * have this fixed path mtu set upon them.
2406 *
2407 * spp_sackdelay - When delayed sack is enabled, this value specifies
2408 * the number of milliseconds that sacks will be delayed
2409 * for. This value will apply to all addresses of an
2410 * association if the spp_address field is empty. Note
2411 * also, that if delayed sack is enabled and this
2412 * value is set to 0, no change is made to the last
2413 * recorded delayed sack timer value.
2414 *
2415 * spp_flags - These flags are used to control various features
2416 * on an association. The flag field may contain
2417 * zero or more of the following options.
2418 *
2419 * SPP_HB_ENABLE - Enable heartbeats on the
2420 * specified address. Note that if the address
2421 * field is empty all addresses for the association
2422 * have heartbeats enabled upon them.
2423 *
2424 * SPP_HB_DISABLE - Disable heartbeats on the
2425 * speicifed address. Note that if the address
2426 * field is empty all addresses for the association
2427 * will have their heartbeats disabled. Note also
2428 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2429 * mutually exclusive, only one of these two should
2430 * be specified. Enabling both fields will have
2431 * undetermined results.
2432 *
2433 * SPP_HB_DEMAND - Request a user initiated heartbeat
2434 * to be made immediately.
2435 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002436 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2437 * heartbeat delayis to be set to the value of 0
2438 * milliseconds.
2439 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002440 * SPP_PMTUD_ENABLE - This field will enable PMTU
2441 * discovery upon the specified address. Note that
2442 * if the address feild is empty then all addresses
2443 * on the association are effected.
2444 *
2445 * SPP_PMTUD_DISABLE - This field will disable PMTU
2446 * discovery upon the specified address. Note that
2447 * if the address feild is empty then all addresses
2448 * on the association are effected. Not also that
2449 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2450 * exclusive. Enabling both will have undetermined
2451 * results.
2452 *
2453 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2454 * on delayed sack. The time specified in spp_sackdelay
2455 * is used to specify the sack delay for this address. Note
2456 * that if spp_address is empty then all addresses will
2457 * enable delayed sack and take on the sack delay
2458 * value specified in spp_sackdelay.
2459 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2460 * off delayed sack. If the spp_address field is blank then
2461 * delayed sack is disabled for the entire association. Note
2462 * also that this field is mutually exclusive to
2463 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2464 * results.
Xin Long0b0dce72018-07-02 18:21:13 +08002465 *
2466 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
2467 * setting of the IPV6 flow label value. The value is
2468 * contained in the spp_ipv6_flowlabel field.
2469 * Upon retrieval, this flag will be set to indicate that
2470 * the spp_ipv6_flowlabel field has a valid value returned.
2471 * If a specific destination address is set (in the
2472 * spp_address field), then the value returned is that of
2473 * the address. If just an association is specified (and
2474 * no address), then the association's default flow label
2475 * is returned. If neither an association nor a destination
2476 * is specified, then the socket's default flow label is
2477 * returned. For non-IPv6 sockets, this flag will be left
2478 * cleared.
2479 *
2480 * SPP_DSCP: Setting this flag enables the setting of the
2481 * Differentiated Services Code Point (DSCP) value
2482 * associated with either the association or a specific
2483 * address. The value is obtained in the spp_dscp field.
2484 * Upon retrieval, this flag will be set to indicate that
2485 * the spp_dscp field has a valid value returned. If a
2486 * specific destination address is set when called (in the
2487 * spp_address field), then that specific destination
2488 * address's DSCP value is returned. If just an association
2489 * is specified, then the association's default DSCP is
2490 * returned. If neither an association nor a destination is
2491 * specified, then the socket's default DSCP is returned.
2492 *
2493 * spp_ipv6_flowlabel
2494 * - This field is used in conjunction with the
2495 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
2496 * The 20 least significant bits are used for the flow
2497 * label. This setting has precedence over any IPv6-layer
2498 * setting.
2499 *
2500 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
2501 * and contains the DSCP. The 6 most significant bits are
2502 * used for the DSCP. This setting has precedence over any
2503 * IPv4- or IPv6- layer setting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 */
Adrian Bunk16164362006-09-18 00:40:38 -07002505static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2506 struct sctp_transport *trans,
2507 struct sctp_association *asoc,
2508 struct sctp_sock *sp,
2509 int hb_change,
2510 int pmtud_change,
2511 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 int error;
2514
Frank Filz52ccb8e2005-12-22 11:36:46 -08002515 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002516 struct net *net = sock_net(trans->asoc->base.sk);
2517
2518 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if (error)
2520 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 }
2522
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002523 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2524 * this field is ignored. Note also that a value of zero indicates
2525 * the current setting should be left unchanged.
2526 */
2527 if (params->spp_flags & SPP_HB_ENABLE) {
2528
2529 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2530 * set. This lets us use 0 value when this flag
2531 * is set.
2532 */
2533 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2534 params->spp_hbinterval = 0;
2535
2536 if (params->spp_hbinterval ||
2537 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2538 if (trans) {
2539 trans->hbinterval =
2540 msecs_to_jiffies(params->spp_hbinterval);
2541 } else if (asoc) {
2542 asoc->hbinterval =
2543 msecs_to_jiffies(params->spp_hbinterval);
2544 } else {
2545 sp->hbinterval = params->spp_hbinterval;
2546 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002547 }
2548 }
2549
2550 if (hb_change) {
2551 if (trans) {
2552 trans->param_flags =
2553 (trans->param_flags & ~SPP_HB) | hb_change;
2554 } else if (asoc) {
2555 asoc->param_flags =
2556 (asoc->param_flags & ~SPP_HB) | hb_change;
2557 } else {
2558 sp->param_flags =
2559 (sp->param_flags & ~SPP_HB) | hb_change;
2560 }
2561 }
2562
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002563 /* When Path MTU discovery is disabled the value specified here will
2564 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2565 * include the flag SPP_PMTUD_DISABLE for this field to have any
2566 * effect).
2567 */
2568 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002569 if (trans) {
2570 trans->pathmtu = params->spp_pathmtu;
Xin Long3ebfdf02017-04-04 13:39:55 +08002571 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002572 } else if (asoc) {
Marcelo Ricardo Leitnerc4b28932018-04-26 16:58:53 -03002573 sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002574 } else {
2575 sp->pathmtu = params->spp_pathmtu;
2576 }
2577 }
2578
2579 if (pmtud_change) {
2580 if (trans) {
2581 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2582 (params->spp_flags & SPP_PMTUD_ENABLE);
2583 trans->param_flags =
2584 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2585 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002586 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
Xin Long3ebfdf02017-04-04 13:39:55 +08002587 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002588 }
2589 } else if (asoc) {
2590 asoc->param_flags =
2591 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2592 } else {
2593 sp->param_flags =
2594 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2595 }
2596 }
2597
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002598 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2599 * value of this field is ignored. Note also that a value of zero
2600 * indicates the current setting should be left unchanged.
2601 */
2602 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002603 if (trans) {
2604 trans->sackdelay =
2605 msecs_to_jiffies(params->spp_sackdelay);
2606 } else if (asoc) {
2607 asoc->sackdelay =
2608 msecs_to_jiffies(params->spp_sackdelay);
2609 } else {
2610 sp->sackdelay = params->spp_sackdelay;
2611 }
2612 }
2613
2614 if (sackdelay_change) {
2615 if (trans) {
2616 trans->param_flags =
2617 (trans->param_flags & ~SPP_SACKDELAY) |
2618 sackdelay_change;
2619 } else if (asoc) {
2620 asoc->param_flags =
2621 (asoc->param_flags & ~SPP_SACKDELAY) |
2622 sackdelay_change;
2623 } else {
2624 sp->param_flags =
2625 (sp->param_flags & ~SPP_SACKDELAY) |
2626 sackdelay_change;
2627 }
2628 }
2629
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002630 /* Note that a value of zero indicates the current setting should be
2631 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002632 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002633 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002634 if (trans) {
2635 trans->pathmaxrxt = params->spp_pathmaxrxt;
2636 } else if (asoc) {
2637 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2638 } else {
2639 sp->pathmaxrxt = params->spp_pathmaxrxt;
2640 }
2641 }
2642
Xin Long0b0dce72018-07-02 18:21:13 +08002643 if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
Xin Long741880e2018-09-03 15:47:11 +08002644 if (trans) {
2645 if (trans->ipaddr.sa.sa_family == AF_INET6) {
2646 trans->flowlabel = params->spp_ipv6_flowlabel &
2647 SCTP_FLOWLABEL_VAL_MASK;
2648 trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2649 }
Xin Long0b0dce72018-07-02 18:21:13 +08002650 } else if (asoc) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002651 struct sctp_transport *t;
2652
2653 list_for_each_entry(t, &asoc->peer.transport_addr_list,
Xin Long0b0dce72018-07-02 18:21:13 +08002654 transports) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002655 if (t->ipaddr.sa.sa_family != AF_INET6)
Xin Long0b0dce72018-07-02 18:21:13 +08002656 continue;
Xin Longaf8a2b82018-09-03 15:47:10 +08002657 t->flowlabel = params->spp_ipv6_flowlabel &
2658 SCTP_FLOWLABEL_VAL_MASK;
2659 t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
Xin Long0b0dce72018-07-02 18:21:13 +08002660 }
2661 asoc->flowlabel = params->spp_ipv6_flowlabel &
2662 SCTP_FLOWLABEL_VAL_MASK;
2663 asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2664 } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
2665 sp->flowlabel = params->spp_ipv6_flowlabel &
2666 SCTP_FLOWLABEL_VAL_MASK;
2667 sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2668 }
2669 }
2670
2671 if (params->spp_flags & SPP_DSCP) {
2672 if (trans) {
2673 trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2674 trans->dscp |= SCTP_DSCP_SET_MASK;
2675 } else if (asoc) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002676 struct sctp_transport *t;
2677
2678 list_for_each_entry(t, &asoc->peer.transport_addr_list,
Xin Long0b0dce72018-07-02 18:21:13 +08002679 transports) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002680 t->dscp = params->spp_dscp &
2681 SCTP_DSCP_VAL_MASK;
2682 t->dscp |= SCTP_DSCP_SET_MASK;
Xin Long0b0dce72018-07-02 18:21:13 +08002683 }
2684 asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2685 asoc->dscp |= SCTP_DSCP_SET_MASK;
2686 } else {
2687 sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2688 sp->dscp |= SCTP_DSCP_SET_MASK;
2689 }
2690 }
2691
Frank Filz52ccb8e2005-12-22 11:36:46 -08002692 return 0;
2693}
2694
2695static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002696 char __user *optval,
2697 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002698{
2699 struct sctp_paddrparams params;
2700 struct sctp_transport *trans = NULL;
2701 struct sctp_association *asoc = NULL;
2702 struct sctp_sock *sp = sctp_sk(sk);
2703 int error;
2704 int hb_change, pmtud_change, sackdelay_change;
2705
Xin Long0b0dce72018-07-02 18:21:13 +08002706 if (optlen == sizeof(params)) {
2707 if (copy_from_user(&params, optval, optlen))
2708 return -EFAULT;
2709 } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
2710 spp_ipv6_flowlabel), 4)) {
2711 if (copy_from_user(&params, optval, optlen))
2712 return -EFAULT;
2713 if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
2714 return -EINVAL;
2715 } else {
wangweidongcb3f8372013-12-23 12:16:50 +08002716 return -EINVAL;
Xin Long0b0dce72018-07-02 18:21:13 +08002717 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002718
2719 /* Validate flags and value parameters. */
2720 hb_change = params.spp_flags & SPP_HB;
2721 pmtud_change = params.spp_flags & SPP_PMTUD;
2722 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2723
2724 if (hb_change == SPP_HB ||
2725 pmtud_change == SPP_PMTUD ||
2726 sackdelay_change == SPP_SACKDELAY ||
2727 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002728 (params.spp_pathmtu &&
2729 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002730 return -EINVAL;
2731
2732 /* If an address other than INADDR_ANY is specified, and
2733 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 */
wangweidongcb3f8372013-12-23 12:16:50 +08002735 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002736 trans = sctp_addr_id2transport(sk, &params.spp_address,
2737 params.spp_assoc_id);
2738 if (!trans)
2739 return -EINVAL;
2740 }
2741
Xin Longb99e5e02019-01-28 15:08:24 +08002742 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
2743 * socket is a one to many style socket, and an association
2744 * was not found, then the id was invalid.
Frank Filz52ccb8e2005-12-22 11:36:46 -08002745 */
2746 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
Xin Longb99e5e02019-01-28 15:08:24 +08002747 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
2748 sctp_style(sk, UDP))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002749 return -EINVAL;
2750
2751 /* Heartbeat demand can only be sent on a transport or
2752 * association, but not a socket.
2753 */
2754 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2755 return -EINVAL;
2756
2757 /* Process parameters. */
2758 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2759 hb_change, pmtud_change,
2760 sackdelay_change);
2761
2762 if (error)
2763 return error;
2764
2765 /* If changes are for association, also apply parameters to each
2766 * transport.
2767 */
2768 if (!trans && asoc) {
Robert P. J. Day9dbc15f02008-04-12 18:54:24 -07002769 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2770 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002771 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2772 hb_change, pmtud_change,
2773 sackdelay_change);
2774 }
2775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 return 0;
2778}
2779
wangweidong0ea5e4d2014-01-15 17:24:01 +08002780static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2781{
2782 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2783}
2784
2785static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2786{
2787 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2788}
2789
Xin Long9c5829e2019-01-28 15:08:34 +08002790static void sctp_apply_asoc_delayed_ack(struct sctp_sack_info *params,
2791 struct sctp_association *asoc)
2792{
2793 struct sctp_transport *trans;
2794
2795 if (params->sack_delay) {
2796 asoc->sackdelay = msecs_to_jiffies(params->sack_delay);
2797 asoc->param_flags =
2798 sctp_spp_sackdelay_enable(asoc->param_flags);
2799 }
2800 if (params->sack_freq == 1) {
2801 asoc->param_flags =
2802 sctp_spp_sackdelay_disable(asoc->param_flags);
2803 } else if (params->sack_freq > 1) {
2804 asoc->sackfreq = params->sack_freq;
2805 asoc->param_flags =
2806 sctp_spp_sackdelay_enable(asoc->param_flags);
2807 }
2808
2809 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2810 transports) {
2811 if (params->sack_delay) {
2812 trans->sackdelay = msecs_to_jiffies(params->sack_delay);
2813 trans->param_flags =
2814 sctp_spp_sackdelay_enable(trans->param_flags);
2815 }
2816 if (params->sack_freq == 1) {
2817 trans->param_flags =
2818 sctp_spp_sackdelay_disable(trans->param_flags);
2819 } else if (params->sack_freq > 1) {
2820 trans->sackfreq = params->sack_freq;
2821 trans->param_flags =
2822 sctp_spp_sackdelay_enable(trans->param_flags);
2823 }
2824 }
2825}
2826
Wei Yongjund364d922008-05-09 15:13:26 -07002827/*
2828 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002829 *
Wei Yongjund364d922008-05-09 15:13:26 -07002830 * This option will effect the way delayed acks are performed. This
2831 * option allows you to get or set the delayed ack time, in
2832 * milliseconds. It also allows changing the delayed ack frequency.
2833 * Changing the frequency to 1 disables the delayed sack algorithm. If
2834 * the assoc_id is 0, then this sets or gets the endpoints default
2835 * values. If the assoc_id field is non-zero, then the set or get
2836 * effects the specified association for the one to many model (the
2837 * assoc_id field is ignored by the one to one model). Note that if
2838 * sack_delay or sack_freq are 0 when setting this option, then the
2839 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002840 *
Wei Yongjund364d922008-05-09 15:13:26 -07002841 * struct sctp_sack_info {
2842 * sctp_assoc_t sack_assoc_id;
2843 * uint32_t sack_delay;
2844 * uint32_t sack_freq;
2845 * };
Frank Filz77086102005-12-22 11:37:30 -08002846 *
Wei Yongjund364d922008-05-09 15:13:26 -07002847 * sack_assoc_id - This parameter, indicates which association the user
2848 * is performing an action upon. Note that if this field's value is
2849 * zero then the endpoints default value is changed (effecting future
2850 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002851 *
Wei Yongjund364d922008-05-09 15:13:26 -07002852 * sack_delay - This parameter contains the number of milliseconds that
2853 * the user is requesting the delayed ACK timer be set to. Note that
2854 * this value is defined in the standard to be between 200 and 500
2855 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002856 *
Wei Yongjund364d922008-05-09 15:13:26 -07002857 * sack_freq - This parameter contains the number of packets that must
2858 * be received before a sack is sent without waiting for the delay
2859 * timer to expire. The default value for this is 2, setting this
2860 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002861 */
2862
Wei Yongjund364d922008-05-09 15:13:26 -07002863static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002864 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002865{
Xin Long9c5829e2019-01-28 15:08:34 +08002866 struct sctp_sock *sp = sctp_sk(sk);
2867 struct sctp_association *asoc;
2868 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002869
Wei Yongjund364d922008-05-09 15:13:26 -07002870 if (optlen == sizeof(struct sctp_sack_info)) {
2871 if (copy_from_user(&params, optval, optlen))
2872 return -EFAULT;
2873
2874 if (params.sack_delay == 0 && params.sack_freq == 0)
2875 return 0;
2876 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05002877 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05002878 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05002879 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05002880 "Use struct sctp_sack_info instead\n",
2881 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07002882 if (copy_from_user(&params, optval, optlen))
2883 return -EFAULT;
2884
2885 if (params.sack_delay == 0)
2886 params.sack_freq = 1;
2887 else
2888 params.sack_freq = 0;
2889 } else
wangweidongcb3f8372013-12-23 12:16:50 +08002890 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08002891
Frank Filz77086102005-12-22 11:37:30 -08002892 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002893 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002894 return -EINVAL;
2895
Xin Long9c5829e2019-01-28 15:08:34 +08002896 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
2897 * socket is a one to many style socket, and an association
2898 * was not found, then the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002899 */
Wei Yongjund364d922008-05-09 15:13:26 -07002900 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
Xin Long9c5829e2019-01-28 15:08:34 +08002901 if (!asoc && params.sack_assoc_id > SCTP_ALL_ASSOC &&
2902 sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002903 return -EINVAL;
2904
Xin Long9c5829e2019-01-28 15:08:34 +08002905 if (asoc) {
2906 sctp_apply_asoc_delayed_ack(&params, asoc);
2907
2908 return 0;
2909 }
2910
Xin Long8e2614f2019-03-18 20:06:00 +08002911 if (sctp_style(sk, TCP))
2912 params.sack_assoc_id = SCTP_FUTURE_ASSOC;
2913
Xin Long9c5829e2019-01-28 15:08:34 +08002914 if (params.sack_assoc_id == SCTP_FUTURE_ASSOC ||
2915 params.sack_assoc_id == SCTP_ALL_ASSOC) {
2916 if (params.sack_delay) {
Wei Yongjund364d922008-05-09 15:13:26 -07002917 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002918 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002919 sctp_spp_sackdelay_enable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002920 }
Xin Long9c5829e2019-01-28 15:08:34 +08002921 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002922 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002923 sctp_spp_sackdelay_disable(sp->param_flags);
Xin Long9c5829e2019-01-28 15:08:34 +08002924 } else if (params.sack_freq > 1) {
Wei Yongjund364d922008-05-09 15:13:26 -07002925 sp->sackfreq = params.sack_freq;
2926 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002927 sctp_spp_sackdelay_enable(sp->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002928 }
Frank Filz77086102005-12-22 11:37:30 -08002929 }
2930
Xin Long9c5829e2019-01-28 15:08:34 +08002931 if (params.sack_assoc_id == SCTP_CURRENT_ASSOC ||
2932 params.sack_assoc_id == SCTP_ALL_ASSOC)
2933 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2934 sctp_apply_asoc_delayed_ack(&params, asoc);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002935
Frank Filz77086102005-12-22 11:37:30 -08002936 return 0;
2937}
2938
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2940 *
2941 * Applications can specify protocol parameters for the default association
2942 * initialization. The option name argument to setsockopt() and getsockopt()
2943 * is SCTP_INITMSG.
2944 *
2945 * Setting initialization parameters is effective only on an unconnected
2946 * socket (for UDP-style sockets only future associations are effected
2947 * by the change). With TCP-style sockets, this option is inherited by
2948 * sockets derived from a listener socket.
2949 */
David S. Millerb7058842009-09-30 16:12:20 -07002950static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951{
2952 struct sctp_initmsg sinit;
2953 struct sctp_sock *sp = sctp_sk(sk);
2954
2955 if (optlen != sizeof(struct sctp_initmsg))
2956 return -EINVAL;
2957 if (copy_from_user(&sinit, optval, optlen))
2958 return -EFAULT;
2959
2960 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002961 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002963 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002965 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002967 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
2969 return 0;
2970}
2971
2972/*
2973 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2974 *
2975 * Applications that wish to use the sendto() system call may wish to
2976 * specify a default set of parameters that would normally be supplied
2977 * through the inclusion of ancillary data. This socket option allows
2978 * such an application to set the default sctp_sndrcvinfo structure.
2979 * The application that wishes to use this socket option simply passes
2980 * in to this call the sctp_sndrcvinfo structure defined in Section
2981 * 5.2.2) The input parameters accepted by this call include
2982 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2983 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2984 * to this call if the caller is using the UDP model.
2985 */
2986static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002987 char __user *optval,
2988 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002991 struct sctp_association *asoc;
2992 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002994 if (optlen != sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 return -EINVAL;
2996 if (copy_from_user(&info, optval, optlen))
2997 return -EFAULT;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002998 if (info.sinfo_flags &
2999 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3000 SCTP_ABORT | SCTP_EOF))
3001 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
3003 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
Xin Long707e45b2019-01-28 15:08:35 +08003004 if (!asoc && info.sinfo_assoc_id > SCTP_ALL_ASSOC &&
3005 sctp_style(sk, UDP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 return -EINVAL;
Xin Long707e45b2019-01-28 15:08:35 +08003007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (asoc) {
3009 asoc->default_stream = info.sinfo_stream;
3010 asoc->default_flags = info.sinfo_flags;
3011 asoc->default_ppid = info.sinfo_ppid;
3012 asoc->default_context = info.sinfo_context;
3013 asoc->default_timetolive = info.sinfo_timetolive;
Xin Long707e45b2019-01-28 15:08:35 +08003014
3015 return 0;
3016 }
3017
Marcelo Ricardo Leitner1354e722019-03-18 20:05:59 +08003018 if (sctp_style(sk, TCP))
3019 info.sinfo_assoc_id = SCTP_FUTURE_ASSOC;
3020
Xin Long707e45b2019-01-28 15:08:35 +08003021 if (info.sinfo_assoc_id == SCTP_FUTURE_ASSOC ||
3022 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 sp->default_stream = info.sinfo_stream;
3024 sp->default_flags = info.sinfo_flags;
3025 sp->default_ppid = info.sinfo_ppid;
3026 sp->default_context = info.sinfo_context;
3027 sp->default_timetolive = info.sinfo_timetolive;
3028 }
3029
Xin Long707e45b2019-01-28 15:08:35 +08003030 if (info.sinfo_assoc_id == SCTP_CURRENT_ASSOC ||
3031 info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
3032 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
3033 asoc->default_stream = info.sinfo_stream;
3034 asoc->default_flags = info.sinfo_flags;
3035 asoc->default_ppid = info.sinfo_ppid;
3036 asoc->default_context = info.sinfo_context;
3037 asoc->default_timetolive = info.sinfo_timetolive;
3038 }
3039 }
3040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return 0;
3042}
3043
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003044/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
3045 * (SCTP_DEFAULT_SNDINFO)
3046 */
3047static int sctp_setsockopt_default_sndinfo(struct sock *sk,
3048 char __user *optval,
3049 unsigned int optlen)
3050{
3051 struct sctp_sock *sp = sctp_sk(sk);
3052 struct sctp_association *asoc;
3053 struct sctp_sndinfo info;
3054
3055 if (optlen != sizeof(info))
3056 return -EINVAL;
3057 if (copy_from_user(&info, optval, optlen))
3058 return -EFAULT;
3059 if (info.snd_flags &
3060 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3061 SCTP_ABORT | SCTP_EOF))
3062 return -EINVAL;
3063
3064 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
Xin Long92fc3bd2019-01-28 15:08:36 +08003065 if (!asoc && info.snd_assoc_id > SCTP_ALL_ASSOC &&
3066 sctp_style(sk, UDP))
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003067 return -EINVAL;
Xin Long92fc3bd2019-01-28 15:08:36 +08003068
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003069 if (asoc) {
3070 asoc->default_stream = info.snd_sid;
3071 asoc->default_flags = info.snd_flags;
3072 asoc->default_ppid = info.snd_ppid;
3073 asoc->default_context = info.snd_context;
Xin Long92fc3bd2019-01-28 15:08:36 +08003074
3075 return 0;
3076 }
3077
Xin Longa842e652019-03-18 20:06:01 +08003078 if (sctp_style(sk, TCP))
3079 info.snd_assoc_id = SCTP_FUTURE_ASSOC;
3080
Xin Long92fc3bd2019-01-28 15:08:36 +08003081 if (info.snd_assoc_id == SCTP_FUTURE_ASSOC ||
3082 info.snd_assoc_id == SCTP_ALL_ASSOC) {
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003083 sp->default_stream = info.snd_sid;
3084 sp->default_flags = info.snd_flags;
3085 sp->default_ppid = info.snd_ppid;
3086 sp->default_context = info.snd_context;
3087 }
3088
Xin Long92fc3bd2019-01-28 15:08:36 +08003089 if (info.snd_assoc_id == SCTP_CURRENT_ASSOC ||
3090 info.snd_assoc_id == SCTP_ALL_ASSOC) {
3091 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
3092 asoc->default_stream = info.snd_sid;
3093 asoc->default_flags = info.snd_flags;
3094 asoc->default_ppid = info.snd_ppid;
3095 asoc->default_context = info.snd_context;
3096 }
3097 }
3098
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003099 return 0;
3100}
3101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3103 *
3104 * Requests that the local SCTP stack use the enclosed peer address as
3105 * the association primary. The enclosed address must be one of the
3106 * association peer's addresses.
3107 */
3108static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003109 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
3111 struct sctp_prim prim;
3112 struct sctp_transport *trans;
Richard Haines2277c7c2018-02-13 20:56:24 +00003113 struct sctp_af *af;
3114 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
3116 if (optlen != sizeof(struct sctp_prim))
3117 return -EINVAL;
3118
3119 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3120 return -EFAULT;
3121
Richard Haines2277c7c2018-02-13 20:56:24 +00003122 /* Allow security module to validate address but need address len. */
3123 af = sctp_get_af_specific(prim.ssp_addr.ss_family);
3124 if (!af)
3125 return -EINVAL;
3126
3127 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
3128 (struct sockaddr *)&prim.ssp_addr,
3129 af->sockaddr_len);
3130 if (err)
3131 return err;
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
3134 if (!trans)
3135 return -EINVAL;
3136
3137 sctp_assoc_set_primary(trans->asoc, trans);
3138
3139 return 0;
3140}
3141
3142/*
3143 * 7.1.5 SCTP_NODELAY
3144 *
3145 * Turn on/off any Nagle-like algorithm. This means that packets are
3146 * generally sent as soon as possible and no unnecessary delays are
3147 * introduced, at the cost of more packets in the network. Expects an
3148 * integer boolean flag.
3149 */
3150static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003151 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
3153 int val;
3154
3155 if (optlen < sizeof(int))
3156 return -EINVAL;
3157 if (get_user(val, (int __user *)optval))
3158 return -EFAULT;
3159
3160 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3161 return 0;
3162}
3163
3164/*
3165 *
3166 * 7.1.1 SCTP_RTOINFO
3167 *
3168 * The protocol parameters used to initialize and bound retransmission
3169 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3170 * and modify these parameters.
3171 * All parameters are time values, in milliseconds. A value of 0, when
3172 * modifying the parameters, indicates that the current value should not
3173 * be changed.
3174 *
3175 */
David S. Millerb7058842009-09-30 16:12:20 -07003176static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3177{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 struct sctp_rtoinfo rtoinfo;
3179 struct sctp_association *asoc;
wangweidong85f935d2013-12-11 09:50:38 +08003180 unsigned long rto_min, rto_max;
3181 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
3183 if (optlen != sizeof (struct sctp_rtoinfo))
3184 return -EINVAL;
3185
3186 if (copy_from_user(&rtoinfo, optval, optlen))
3187 return -EFAULT;
3188
3189 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3190
3191 /* Set the values to the specific association */
Xin Long7adb5ed2019-01-28 15:08:25 +08003192 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
3193 sctp_style(sk, UDP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 return -EINVAL;
3195
wangweidong85f935d2013-12-11 09:50:38 +08003196 rto_max = rtoinfo.srto_max;
3197 rto_min = rtoinfo.srto_min;
3198
3199 if (rto_max)
3200 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3201 else
3202 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3203
3204 if (rto_min)
3205 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3206 else
3207 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3208
3209 if (rto_min > rto_max)
3210 return -EINVAL;
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 if (asoc) {
3213 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003214 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 msecs_to_jiffies(rtoinfo.srto_initial);
wangweidong85f935d2013-12-11 09:50:38 +08003216 asoc->rto_max = rto_max;
3217 asoc->rto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 } else {
3219 /* If there is no association or the association-id = 0
3220 * set the values to the endpoint.
3221 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 if (rtoinfo.srto_initial != 0)
3223 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
wangweidong85f935d2013-12-11 09:50:38 +08003224 sp->rtoinfo.srto_max = rto_max;
3225 sp->rtoinfo.srto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 }
3227
3228 return 0;
3229}
3230
3231/*
3232 *
3233 * 7.1.2 SCTP_ASSOCINFO
3234 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02003235 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 * of the association.
3237 * Returns an error if the new association retransmission value is
3238 * greater than the sum of the retransmission value of the peer.
3239 * See [SCTP] for more information.
3240 *
3241 */
David S. Millerb7058842009-09-30 16:12:20 -07003242static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
3244
3245 struct sctp_assocparams assocparams;
3246 struct sctp_association *asoc;
3247
3248 if (optlen != sizeof(struct sctp_assocparams))
3249 return -EINVAL;
3250 if (copy_from_user(&assocparams, optval, optlen))
3251 return -EFAULT;
3252
3253 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3254
Xin Long88893942019-01-28 15:08:26 +08003255 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
3256 sctp_style(sk, UDP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return -EINVAL;
3258
3259 /* Set the values to the specific association */
3260 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003261 if (assocparams.sasoc_asocmaxrxt != 0) {
3262 __u32 path_sum = 0;
3263 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003264 struct sctp_transport *peer_addr;
3265
Robert P. J. Day9dbc15f02008-04-12 18:54:24 -07003266 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3267 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003268 path_sum += peer_addr->pathmaxrxt;
3269 paths++;
3270 }
3271
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003272 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003273 * one path/transport. We do this because path
3274 * retransmissions are only counted when we have more
3275 * then one path.
3276 */
3277 if (paths > 1 &&
3278 assocparams.sasoc_asocmaxrxt > path_sum)
3279 return -EINVAL;
3280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003282 }
3283
Daniel Borkmann52db8822013-06-25 18:17:27 +02003284 if (assocparams.sasoc_cookie_life != 0)
3285 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 } else {
3287 /* Set the values to the endpoint */
3288 struct sctp_sock *sp = sctp_sk(sk);
3289
3290 if (assocparams.sasoc_asocmaxrxt != 0)
3291 sp->assocparams.sasoc_asocmaxrxt =
3292 assocparams.sasoc_asocmaxrxt;
3293 if (assocparams.sasoc_cookie_life != 0)
3294 sp->assocparams.sasoc_cookie_life =
3295 assocparams.sasoc_cookie_life;
3296 }
3297 return 0;
3298}
3299
3300/*
3301 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3302 *
3303 * This socket option is a boolean flag which turns on or off mapped V4
3304 * addresses. If this option is turned on and the socket is type
3305 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3306 * If this option is turned off, then no mapping will be done of V4
3307 * addresses and a user will receive both PF_INET6 and PF_INET type
3308 * addresses on the socket.
3309 */
David S. Millerb7058842009-09-30 16:12:20 -07003310static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
3312 int val;
3313 struct sctp_sock *sp = sctp_sk(sk);
3314
3315 if (optlen < sizeof(int))
3316 return -EINVAL;
3317 if (get_user(val, (int __user *)optval))
3318 return -EFAULT;
3319 if (val)
3320 sp->v4mapped = 1;
3321 else
3322 sp->v4mapped = 0;
3323
3324 return 0;
3325}
3326
3327/*
Wei Yongjune89c2092008-12-25 16:54:58 -08003328 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3329 * This option will get or set the maximum size to put in any outgoing
3330 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 * fragmented by SCTP into the specified size. Note that the underlying
3332 * SCTP implementation may fragment into smaller sized chunks when the
3333 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08003334 * the user. The default value for this option is '0' which indicates
3335 * the user is NOT limiting fragmentation and only the PMTU will effect
3336 * SCTP's choice of DATA chunk size. Note also that values set larger
3337 * than the maximum size of an IP datagram will effectively let SCTP
3338 * control fragmentation (i.e. the same as setting this option to 0).
3339 *
3340 * The following structure is used to access and modify this parameter:
3341 *
3342 * struct sctp_assoc_value {
3343 * sctp_assoc_t assoc_id;
3344 * uint32_t assoc_value;
3345 * };
3346 *
3347 * assoc_id: This parameter is ignored for one-to-one style sockets.
3348 * For one-to-many style sockets this parameter indicates which
3349 * association the user is performing an action upon. Note that if
3350 * this field's value is zero then the endpoints default value is
3351 * changed (effecting future associations only).
3352 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 */
David S. Millerb7058842009-09-30 16:12:20 -07003354static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
Xin Longecca8f82017-11-17 14:11:11 +08003356 struct sctp_sock *sp = sctp_sk(sk);
Wei Yongjune89c2092008-12-25 16:54:58 -08003357 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 int val;
3360
Wei Yongjune89c2092008-12-25 16:54:58 -08003361 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003362 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003363 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003364 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003365 "Use struct sctp_assoc_value instead\n",
3366 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08003367 if (copy_from_user(&val, optval, optlen))
3368 return -EFAULT;
Xin Long6fd769b2019-01-28 15:08:27 +08003369 params.assoc_id = SCTP_FUTURE_ASSOC;
Wei Yongjune89c2092008-12-25 16:54:58 -08003370 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3371 if (copy_from_user(&params, optval, optlen))
3372 return -EFAULT;
3373 val = params.assoc_value;
Xin Longecca8f82017-11-17 14:11:11 +08003374 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 return -EINVAL;
Xin Longecca8f82017-11-17 14:11:11 +08003376 }
Wei Yongjune89c2092008-12-25 16:54:58 -08003377
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003378 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Long6fd769b2019-01-28 15:08:27 +08003379 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
3380 sctp_style(sk, UDP))
3381 return -EINVAL;
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003382
Xin Longecca8f82017-11-17 14:11:11 +08003383 if (val) {
3384 int min_len, max_len;
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003385 __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
3386 sizeof(struct sctp_data_chunk);
Xin Longecca8f82017-11-17 14:11:11 +08003387
Jakub Audykowiczafd0a802018-12-04 20:27:41 +01003388 min_len = sctp_min_frag_point(sp, datasize);
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003389 max_len = SCTP_MAX_CHUNK_LEN - datasize;
Xin Longecca8f82017-11-17 14:11:11 +08003390
3391 if (val < min_len || val > max_len)
3392 return -EINVAL;
3393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
Wei Yongjune89c2092008-12-25 16:54:58 -08003395 if (asoc) {
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003396 asoc->user_frag = val;
Marcelo Ricardo Leitner2f5e3c92018-04-26 16:58:55 -03003397 sctp_assoc_update_frag_point(asoc);
Wei Yongjune89c2092008-12-25 16:54:58 -08003398 } else {
3399 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 }
3401
3402 return 0;
3403}
3404
3405
3406/*
3407 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3408 *
3409 * Requests that the peer mark the enclosed address as the association
3410 * primary. The enclosed address must be one of the association's
3411 * locally bound addresses. The following structure is used to make a
3412 * set primary request:
3413 */
3414static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003415 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003417 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 struct sctp_association *asoc = NULL;
3420 struct sctp_setpeerprim prim;
3421 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003422 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 int err;
3424
3425 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003427 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return -EPERM;
3429
3430 if (optlen != sizeof(struct sctp_setpeerprim))
3431 return -EINVAL;
3432
3433 if (copy_from_user(&prim, optval, optlen))
3434 return -EFAULT;
3435
3436 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003437 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 return -EINVAL;
3439
3440 if (!asoc->peer.asconf_capable)
3441 return -EPERM;
3442
3443 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3444 return -EPERM;
3445
3446 if (!sctp_state(asoc, ESTABLISHED))
3447 return -ENOTCONN;
3448
Wei Yongjun40a01032010-12-07 17:11:09 +00003449 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3450 if (!af)
3451 return -EINVAL;
3452
3453 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3454 return -EADDRNOTAVAIL;
3455
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3457 return -EADDRNOTAVAIL;
3458
Richard Haines2277c7c2018-02-13 20:56:24 +00003459 /* Allow security module to validate address. */
3460 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3461 (struct sockaddr *)&prim.sspp_addr,
3462 af->sockaddr_len);
3463 if (err)
3464 return err;
3465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3467 chunk = sctp_make_asconf_set_prim(asoc,
3468 (union sctp_addr *)&prim.sspp_addr);
3469 if (!chunk)
3470 return -ENOMEM;
3471
3472 err = sctp_send_asconf(asoc, chunk);
3473
Daniel Borkmannbb333812013-06-28 19:49:40 +02003474 pr_debug("%s: we set peer primary addr primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
3476 return err;
3477}
3478
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003479static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003480 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003482 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003484 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003486 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 return -EFAULT;
3488
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003489 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
3491 return 0;
3492}
3493
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003494/*
3495 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3496 *
3497 * The context field in the sctp_sndrcvinfo structure is normally only
3498 * used when a failed message is retrieved holding the value that was
3499 * sent down on the actual send call. This option allows the setting of
3500 * a default context on an association basis that will be received on
3501 * reading messages from the peer. This is especially helpful in the
3502 * one-2-many model for an application to keep some reference to an
3503 * internal state machine that is processing messages on the
3504 * association. Note that the setting of this value only effects
3505 * received messages from the peer and does not effect the value that is
3506 * saved with outbound messages.
3507 */
3508static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003509 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003510{
Xin Long49b037a2019-01-28 15:08:37 +08003511 struct sctp_sock *sp = sctp_sk(sk);
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003512 struct sctp_assoc_value params;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003513 struct sctp_association *asoc;
3514
3515 if (optlen != sizeof(struct sctp_assoc_value))
3516 return -EINVAL;
3517 if (copy_from_user(&params, optval, optlen))
3518 return -EFAULT;
3519
Xin Long49b037a2019-01-28 15:08:37 +08003520 asoc = sctp_id2assoc(sk, params.assoc_id);
3521 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3522 sctp_style(sk, UDP))
3523 return -EINVAL;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003524
Xin Long49b037a2019-01-28 15:08:37 +08003525 if (asoc) {
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003526 asoc->default_rcv_context = params.assoc_value;
Xin Long49b037a2019-01-28 15:08:37 +08003527
3528 return 0;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003529 }
3530
Xin Longcface2c2019-03-18 20:06:02 +08003531 if (sctp_style(sk, TCP))
3532 params.assoc_id = SCTP_FUTURE_ASSOC;
3533
Xin Long49b037a2019-01-28 15:08:37 +08003534 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3535 params.assoc_id == SCTP_ALL_ASSOC)
3536 sp->default_rcv_context = params.assoc_value;
3537
3538 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3539 params.assoc_id == SCTP_ALL_ASSOC)
3540 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3541 asoc->default_rcv_context = params.assoc_value;
3542
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003543 return 0;
3544}
3545
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003546/*
3547 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3548 *
3549 * This options will at a minimum specify if the implementation is doing
3550 * fragmented interleave. Fragmented interleave, for a one to many
3551 * socket, is when subsequent calls to receive a message may return
3552 * parts of messages from different associations. Some implementations
3553 * may allow you to turn this value on or off. If so, when turned off,
3554 * no fragment interleave will occur (which will cause a head of line
3555 * blocking amongst multiple associations sharing the same one to many
3556 * socket). When this option is turned on, then each receive call may
3557 * come from a different association (thus the user must receive data
3558 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3559 * association each receive belongs to.
3560 *
3561 * This option takes a boolean value. A non-zero value indicates that
3562 * fragmented interleave is on. A value of zero indicates that
3563 * fragmented interleave is off.
3564 *
3565 * Note that it is important that an implementation that allows this
3566 * option to be turned on, have it off by default. Otherwise an unaware
3567 * application using the one to many model may become confused and act
3568 * incorrectly.
3569 */
3570static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3571 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003572 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003573{
3574 int val;
3575
3576 if (optlen != sizeof(int))
3577 return -EINVAL;
3578 if (get_user(val, (int __user *)optval))
3579 return -EFAULT;
3580
Xin Long772a5862017-12-08 21:03:58 +08003581 sctp_sk(sk)->frag_interleave = !!val;
3582
3583 if (!sctp_sk(sk)->frag_interleave)
Xin Longe55f4b82019-07-09 00:57:07 +08003584 sctp_sk(sk)->ep->intl_enable = 0;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003585
3586 return 0;
3587}
3588
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003589/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003590 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003591 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003592 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003593 * This option will set or get the SCTP partial delivery point. This
3594 * point is the size of a message where the partial delivery API will be
3595 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003596 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003597 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003598 * point. Note also that the call will fail if the user attempts to set
3599 * this value larger than the socket receive buffer size.
3600 *
3601 * Note that any single message having a length smaller than or equal to
3602 * the SCTP partial delivery point will be delivered in one single read
3603 * call as long as the user provided buffer is large enough to hold the
3604 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003605 */
3606static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3607 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003608 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003609{
3610 u32 val;
3611
3612 if (optlen != sizeof(u32))
3613 return -EINVAL;
3614 if (get_user(val, (int __user *)optval))
3615 return -EFAULT;
3616
Wei Yongjun8510b932008-12-25 16:59:03 -08003617 /* Note: We double the receive buffer from what the user sets
3618 * it to be, also initial rwnd is based on rcvbuf/2.
3619 */
3620 if (val > (sk->sk_rcvbuf >> 1))
3621 return -EINVAL;
3622
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003623 sctp_sk(sk)->pd_point = val;
3624
3625 return 0; /* is this the right error code? */
3626}
3627
Vlad Yasevich70331572007-03-23 11:34:36 -07003628/*
3629 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3630 *
3631 * This option will allow a user to change the maximum burst of packets
3632 * that can be emitted by this association. Note that the default value
3633 * is 4, and some implementations may restrict this setting so that it
3634 * can only be lowered.
3635 *
3636 * NOTE: This text doesn't seem right. Do this on a socket basis with
3637 * future associations inheriting the socket value.
3638 */
3639static int sctp_setsockopt_maxburst(struct sock *sk,
3640 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003641 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003642{
Xin Longe0651a02019-01-28 15:08:38 +08003643 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman219b99a2008-03-05 13:44:46 -08003644 struct sctp_assoc_value params;
Neil Horman219b99a2008-03-05 13:44:46 -08003645 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003646
Neil Horman219b99a2008-03-05 13:44:46 -08003647 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003648 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003649 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003650 "Use of int in max_burst socket option deprecated.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003651 "Use struct sctp_assoc_value instead\n",
3652 current->comm, task_pid_nr(current));
Xin Longe0651a02019-01-28 15:08:38 +08003653 if (copy_from_user(&params.assoc_value, optval, optlen))
Neil Horman219b99a2008-03-05 13:44:46 -08003654 return -EFAULT;
Xin Longe0651a02019-01-28 15:08:38 +08003655 params.assoc_id = SCTP_FUTURE_ASSOC;
Neil Horman219b99a2008-03-05 13:44:46 -08003656 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3657 if (copy_from_user(&params, optval, optlen))
3658 return -EFAULT;
Neil Horman219b99a2008-03-05 13:44:46 -08003659 } else
3660 return -EINVAL;
3661
Xin Longe0651a02019-01-28 15:08:38 +08003662 asoc = sctp_id2assoc(sk, params.assoc_id);
3663 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3664 sctp_style(sk, UDP))
3665 return -EINVAL;
Neil Horman219b99a2008-03-05 13:44:46 -08003666
Xin Longe0651a02019-01-28 15:08:38 +08003667 if (asoc) {
3668 asoc->max_burst = params.assoc_value;
3669
3670 return 0;
3671 }
3672
Xin Long746bc212019-03-18 20:06:03 +08003673 if (sctp_style(sk, TCP))
3674 params.assoc_id = SCTP_FUTURE_ASSOC;
3675
Xin Longe0651a02019-01-28 15:08:38 +08003676 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3677 params.assoc_id == SCTP_ALL_ASSOC)
3678 sp->max_burst = params.assoc_value;
3679
3680 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3681 params.assoc_id == SCTP_ALL_ASSOC)
3682 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3683 asoc->max_burst = params.assoc_value;
Vlad Yasevich70331572007-03-23 11:34:36 -07003684
3685 return 0;
3686}
3687
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003688/*
3689 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3690 *
3691 * This set option adds a chunk type that the user is requesting to be
3692 * received only in an authenticated way. Changes to the list of chunks
3693 * will only effect future associations on the socket.
3694 */
3695static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003696 char __user *optval,
3697 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003698{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003699 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003700 struct sctp_authchunk val;
3701
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003702 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003703 return -EACCES;
3704
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003705 if (optlen != sizeof(struct sctp_authchunk))
3706 return -EINVAL;
3707 if (copy_from_user(&val, optval, optlen))
3708 return -EFAULT;
3709
3710 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003711 case SCTP_CID_INIT:
3712 case SCTP_CID_INIT_ACK:
3713 case SCTP_CID_SHUTDOWN_COMPLETE:
3714 case SCTP_CID_AUTH:
3715 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003716 }
3717
3718 /* add this chunk id to the endpoint */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003719 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003720}
3721
3722/*
3723 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3724 *
3725 * This option gets or sets the list of HMAC algorithms that the local
3726 * endpoint requires the peer to use.
3727 */
3728static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003729 char __user *optval,
3730 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003731{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003732 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003733 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003734 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003735 int err;
3736
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003737 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003738 return -EACCES;
3739
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003740 if (optlen < sizeof(struct sctp_hmacalgo))
3741 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003742 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3743 SCTP_AUTH_NUM_HMACS * sizeof(u16));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003744
wangweidongcb3f8372013-12-23 12:16:50 +08003745 hmacs = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003746 if (IS_ERR(hmacs))
3747 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003748
Vlad Yasevichd9724052008-08-27 16:09:49 -07003749 idents = hmacs->shmac_num_idents;
3750 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3751 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003752 err = -EINVAL;
3753 goto out;
3754 }
3755
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003756 err = sctp_auth_ep_set_hmacs(ep, hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003757out:
3758 kfree(hmacs);
3759 return err;
3760}
3761
3762/*
3763 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3764 *
3765 * This option will set a shared secret key which is used to build an
3766 * association shared key.
3767 */
3768static int sctp_setsockopt_auth_key(struct sock *sk,
3769 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003770 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003771{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003772 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003773 struct sctp_authkey *authkey;
3774 struct sctp_association *asoc;
Xin Long7fb3be12019-01-28 15:08:39 +08003775 int ret = -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003776
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003777 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003778 return -EACCES;
3779
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003780 if (optlen <= sizeof(struct sctp_authkey))
3781 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003782 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3783 * this.
3784 */
Xin Long7fb3be12019-01-28 15:08:39 +08003785 optlen = min_t(unsigned int, optlen, USHRT_MAX + sizeof(*authkey));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003786
wangweidongcb3f8372013-12-23 12:16:50 +08003787 authkey = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003788 if (IS_ERR(authkey))
3789 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003790
Xin Long7fb3be12019-01-28 15:08:39 +08003791 if (authkey->sca_keylength > optlen - sizeof(*authkey))
Vlad Yasevich30c22352008-08-25 15:16:19 -07003792 goto out;
Vlad Yasevich30c22352008-08-25 15:16:19 -07003793
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003794 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
Xin Long7fb3be12019-01-28 15:08:39 +08003795 if (!asoc && authkey->sca_assoc_id > SCTP_ALL_ASSOC &&
3796 sctp_style(sk, UDP))
3797 goto out;
3798
3799 if (asoc) {
3800 ret = sctp_auth_set_key(ep, asoc, authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003801 goto out;
3802 }
3803
Xin Long0685d6b2019-03-18 20:06:04 +08003804 if (sctp_style(sk, TCP))
3805 authkey->sca_assoc_id = SCTP_FUTURE_ASSOC;
3806
Xin Long7fb3be12019-01-28 15:08:39 +08003807 if (authkey->sca_assoc_id == SCTP_FUTURE_ASSOC ||
3808 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3809 ret = sctp_auth_set_key(ep, asoc, authkey);
3810 if (ret)
3811 goto out;
3812 }
3813
3814 ret = 0;
3815
3816 if (authkey->sca_assoc_id == SCTP_CURRENT_ASSOC ||
3817 authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3818 list_for_each_entry(asoc, &ep->asocs, asocs) {
3819 int res = sctp_auth_set_key(ep, asoc, authkey);
3820
3821 if (res && !ret)
3822 ret = res;
3823 }
3824 }
3825
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003826out:
Daniel Borkmann6ba542a2013-02-08 03:04:34 +00003827 kzfree(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003828 return ret;
3829}
3830
3831/*
3832 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3833 *
3834 * This option will get or set the active shared key to be used to build
3835 * the association shared key.
3836 */
3837static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003838 char __user *optval,
3839 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003840{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003841 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003842 struct sctp_association *asoc;
Xin Longbf9fb6a2019-01-28 15:08:40 +08003843 struct sctp_authkeyid val;
3844 int ret = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003845
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003846 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003847 return -EACCES;
3848
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003849 if (optlen != sizeof(struct sctp_authkeyid))
3850 return -EINVAL;
3851 if (copy_from_user(&val, optval, optlen))
3852 return -EFAULT;
3853
3854 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
Xin Longbf9fb6a2019-01-28 15:08:40 +08003855 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3856 sctp_style(sk, UDP))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003857 return -EINVAL;
3858
Xin Longbf9fb6a2019-01-28 15:08:40 +08003859 if (asoc)
3860 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3861
Xin Long06b39e82019-03-18 20:06:05 +08003862 if (sctp_style(sk, TCP))
3863 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3864
Xin Longbf9fb6a2019-01-28 15:08:40 +08003865 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 Long220675e2019-03-18 20:06:06 +08003916 if (sctp_style(sk, TCP))
3917 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3918
Xin Long3adcc302019-01-28 15:08:41 +08003919 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3920 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3921 ret = sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3922 if (ret)
3923 return ret;
3924 }
3925
3926 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3927 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3928 list_for_each_entry(asoc, &ep->asocs, asocs) {
3929 int res = sctp_auth_del_key_id(ep, asoc,
3930 val.scact_keynumber);
3931
3932 if (res && !ret)
3933 ret = res;
3934 }
3935 }
3936
3937 return ret;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003938}
3939
Michio Honda7dc04d72011-04-26 20:16:31 +09003940/*
Xin Long601590e2018-03-14 19:05:32 +08003941 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
3942 *
3943 * This set option will deactivate a shared secret key.
3944 */
3945static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
3946 unsigned int optlen)
3947{
3948 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Xin Long601590e2018-03-14 19:05:32 +08003949 struct sctp_association *asoc;
Xin Long2af66ff2019-01-28 15:08:42 +08003950 struct sctp_authkeyid val;
3951 int ret = 0;
Xin Long601590e2018-03-14 19:05:32 +08003952
3953 if (!ep->auth_enable)
3954 return -EACCES;
3955
3956 if (optlen != sizeof(struct sctp_authkeyid))
3957 return -EINVAL;
3958 if (copy_from_user(&val, optval, optlen))
3959 return -EFAULT;
3960
3961 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
Xin Long2af66ff2019-01-28 15:08:42 +08003962 if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3963 sctp_style(sk, UDP))
Xin Long601590e2018-03-14 19:05:32 +08003964 return -EINVAL;
3965
Xin Long2af66ff2019-01-28 15:08:42 +08003966 if (asoc)
3967 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3968
Xin Long200f3a3b2019-03-18 20:06:07 +08003969 if (sctp_style(sk, TCP))
3970 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3971
Xin Long2af66ff2019-01-28 15:08:42 +08003972 if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3973 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3974 ret = sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3975 if (ret)
3976 return ret;
3977 }
3978
3979 if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3980 val.scact_assoc_id == SCTP_ALL_ASSOC) {
3981 list_for_each_entry(asoc, &ep->asocs, asocs) {
3982 int res = sctp_auth_deact_key_id(ep, asoc,
3983 val.scact_keynumber);
3984
3985 if (res && !ret)
3986 ret = res;
3987 }
3988 }
3989
3990 return ret;
Xin Long601590e2018-03-14 19:05:32 +08003991}
3992
3993/*
Michio Honda7dc04d72011-04-26 20:16:31 +09003994 * 8.1.23 SCTP_AUTO_ASCONF
3995 *
3996 * This option will enable or disable the use of the automatic generation of
3997 * ASCONF chunks to add and delete addresses to an existing association. Note
3998 * that this option has two caveats namely: a) it only affects sockets that
3999 * are bound to all addresses available to the SCTP stack, and b) the system
4000 * administrator may have an overriding control that turns the ASCONF feature
4001 * off no matter what setting the socket option may have.
4002 * This option expects an integer boolean flag, where a non-zero value turns on
4003 * the option, and a zero value turns off the option.
4004 * Note. In this implementation, socket operation overrides default parameter
4005 * being set by sysctl as well as FreeBSD implementation
4006 */
4007static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
4008 unsigned int optlen)
4009{
4010 int val;
4011 struct sctp_sock *sp = sctp_sk(sk);
4012
4013 if (optlen < sizeof(int))
4014 return -EINVAL;
4015 if (get_user(val, (int __user *)optval))
4016 return -EFAULT;
4017 if (!sctp_is_ep_boundall(sk) && val)
4018 return -EINVAL;
4019 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
4020 return 0;
4021
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004022 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09004023 if (val == 0 && sp->do_auto_asconf) {
4024 list_del(&sp->auto_asconf_list);
4025 sp->do_auto_asconf = 0;
4026 } else if (val && !sp->do_auto_asconf) {
4027 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00004028 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09004029 sp->do_auto_asconf = 1;
4030 }
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004031 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09004032 return 0;
4033}
4034
Neil Horman5aa93bc2012-07-21 07:56:07 +00004035/*
4036 * SCTP_PEER_ADDR_THLDS
4037 *
4038 * This option allows us to alter the partially failed threshold for one or all
4039 * transports in an association. See Section 6.1 of:
4040 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
4041 */
4042static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
4043 char __user *optval,
4044 unsigned int optlen)
4045{
4046 struct sctp_paddrthlds val;
4047 struct sctp_transport *trans;
4048 struct sctp_association *asoc;
4049
4050 if (optlen < sizeof(struct sctp_paddrthlds))
4051 return -EINVAL;
4052 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
4053 sizeof(struct sctp_paddrthlds)))
4054 return -EFAULT;
4055
Xin Long8add5432019-01-28 15:08:29 +08004056 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
4057 trans = sctp_addr_id2transport(sk, &val.spt_address,
4058 val.spt_assoc_id);
4059 if (!trans)
Neil Horman5aa93bc2012-07-21 07:56:07 +00004060 return -ENOENT;
Xin Long8add5432019-01-28 15:08:29 +08004061
4062 if (val.spt_pathmaxrxt)
4063 trans->pathmaxrxt = val.spt_pathmaxrxt;
4064 trans->pf_retrans = val.spt_pathpfthld;
4065
4066 return 0;
4067 }
4068
4069 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
4070 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
4071 sctp_style(sk, UDP))
4072 return -EINVAL;
4073
4074 if (asoc) {
Neil Horman5aa93bc2012-07-21 07:56:07 +00004075 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
4076 transports) {
4077 if (val.spt_pathmaxrxt)
4078 trans->pathmaxrxt = val.spt_pathmaxrxt;
4079 trans->pf_retrans = val.spt_pathpfthld;
4080 }
4081
4082 if (val.spt_pathmaxrxt)
4083 asoc->pathmaxrxt = val.spt_pathmaxrxt;
4084 asoc->pf_retrans = val.spt_pathpfthld;
4085 } else {
Xin Long8add5432019-01-28 15:08:29 +08004086 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman5aa93bc2012-07-21 07:56:07 +00004087
4088 if (val.spt_pathmaxrxt)
Xin Long8add5432019-01-28 15:08:29 +08004089 sp->pathmaxrxt = val.spt_pathmaxrxt;
4090 sp->pf_retrans = val.spt_pathpfthld;
Neil Horman5aa93bc2012-07-21 07:56:07 +00004091 }
4092
4093 return 0;
4094}
4095
Geir Ola Vaagland0d3a421d2014-07-12 20:30:37 +02004096static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
4097 char __user *optval,
4098 unsigned int optlen)
4099{
4100 int val;
4101
4102 if (optlen < sizeof(int))
4103 return -EINVAL;
4104 if (get_user(val, (int __user *) optval))
4105 return -EFAULT;
4106
4107 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
4108
4109 return 0;
4110}
4111
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004112static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
4113 char __user *optval,
4114 unsigned int optlen)
4115{
4116 int val;
4117
4118 if (optlen < sizeof(int))
4119 return -EINVAL;
4120 if (get_user(val, (int __user *) optval))
4121 return -EFAULT;
4122
4123 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
4124
4125 return 0;
4126}
4127
Xin Long28aa4c22016-07-09 19:47:40 +08004128static int sctp_setsockopt_pr_supported(struct sock *sk,
4129 char __user *optval,
4130 unsigned int optlen)
4131{
4132 struct sctp_assoc_value params;
Xin Longfb195602019-01-28 15:08:30 +08004133 struct sctp_association *asoc;
Xin Long28aa4c22016-07-09 19:47:40 +08004134
4135 if (optlen != sizeof(params))
Xin Longcc3ccf22018-11-18 15:21:53 +08004136 return -EINVAL;
Xin Long28aa4c22016-07-09 19:47:40 +08004137
Xin Longcc3ccf22018-11-18 15:21:53 +08004138 if (copy_from_user(&params, optval, optlen))
4139 return -EFAULT;
Xin Long28aa4c22016-07-09 19:47:40 +08004140
Xin Longfb195602019-01-28 15:08:30 +08004141 asoc = sctp_id2assoc(sk, params.assoc_id);
4142 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4143 sctp_style(sk, UDP))
4144 return -EINVAL;
4145
Xin Longcc3ccf22018-11-18 15:21:53 +08004146 sctp_sk(sk)->ep->prsctp_enable = !!params.assoc_value;
Xin Long28aa4c22016-07-09 19:47:40 +08004147
Xin Longcc3ccf22018-11-18 15:21:53 +08004148 return 0;
Xin Long28aa4c22016-07-09 19:47:40 +08004149}
4150
Xin Longf959fb42016-07-09 19:47:41 +08004151static int sctp_setsockopt_default_prinfo(struct sock *sk,
4152 char __user *optval,
4153 unsigned int optlen)
4154{
Xin Long3a583052019-01-28 15:08:43 +08004155 struct sctp_sock *sp = sctp_sk(sk);
Xin Longf959fb42016-07-09 19:47:41 +08004156 struct sctp_default_prinfo info;
4157 struct sctp_association *asoc;
4158 int retval = -EINVAL;
4159
4160 if (optlen != sizeof(info))
4161 goto out;
4162
4163 if (copy_from_user(&info, optval, sizeof(info))) {
4164 retval = -EFAULT;
4165 goto out;
4166 }
4167
4168 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
4169 goto out;
4170
4171 if (info.pr_policy == SCTP_PR_SCTP_NONE)
4172 info.pr_value = 0;
4173
4174 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
Xin Long3a583052019-01-28 15:08:43 +08004175 if (!asoc && info.pr_assoc_id > SCTP_ALL_ASSOC &&
4176 sctp_style(sk, UDP))
4177 goto out;
4178
4179 retval = 0;
4180
Xin Longf959fb42016-07-09 19:47:41 +08004181 if (asoc) {
4182 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4183 asoc->default_timetolive = info.pr_value;
Xin Longf959fb42016-07-09 19:47:41 +08004184 goto out;
4185 }
4186
Xin Longcbb45c62019-03-18 20:06:08 +08004187 if (sctp_style(sk, TCP))
4188 info.pr_assoc_id = SCTP_FUTURE_ASSOC;
4189
Xin Long3a583052019-01-28 15:08:43 +08004190 if (info.pr_assoc_id == SCTP_FUTURE_ASSOC ||
4191 info.pr_assoc_id == SCTP_ALL_ASSOC) {
4192 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
4193 sp->default_timetolive = info.pr_value;
4194 }
4195
4196 if (info.pr_assoc_id == SCTP_CURRENT_ASSOC ||
4197 info.pr_assoc_id == SCTP_ALL_ASSOC) {
4198 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4199 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4200 asoc->default_timetolive = info.pr_value;
4201 }
4202 }
Xin Longf959fb42016-07-09 19:47:41 +08004203
4204out:
4205 return retval;
4206}
4207
Xin Longc0d8bab2017-03-10 12:11:12 +08004208static int sctp_setsockopt_reconfig_supported(struct sock *sk,
4209 char __user *optval,
4210 unsigned int optlen)
4211{
4212 struct sctp_assoc_value params;
4213 struct sctp_association *asoc;
4214 int retval = -EINVAL;
4215
4216 if (optlen != sizeof(params))
4217 goto out;
4218
4219 if (copy_from_user(&params, optval, optlen)) {
4220 retval = -EFAULT;
4221 goto out;
4222 }
4223
4224 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Longacce7f32019-01-28 15:08:31 +08004225 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4226 sctp_style(sk, UDP))
Xin Longc0d8bab2017-03-10 12:11:12 +08004227 goto out;
Xin Longacce7f32019-01-28 15:08:31 +08004228
Xin Longa96701f2019-07-09 00:57:04 +08004229 sctp_sk(sk)->ep->reconf_enable = !!params.assoc_value;
Xin Longc0d8bab2017-03-10 12:11:12 +08004230
4231 retval = 0;
4232
4233out:
4234 return retval;
4235}
4236
Xin Long9fb657a2017-01-18 00:44:46 +08004237static int sctp_setsockopt_enable_strreset(struct sock *sk,
4238 char __user *optval,
4239 unsigned int optlen)
4240{
Xin Long99a62132019-01-28 15:08:44 +08004241 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Xin Long9fb657a2017-01-18 00:44:46 +08004242 struct sctp_assoc_value params;
4243 struct sctp_association *asoc;
4244 int retval = -EINVAL;
4245
4246 if (optlen != sizeof(params))
4247 goto out;
4248
4249 if (copy_from_user(&params, optval, optlen)) {
4250 retval = -EFAULT;
4251 goto out;
4252 }
4253
4254 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
4255 goto out;
4256
4257 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Long99a62132019-01-28 15:08:44 +08004258 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4259 sctp_style(sk, UDP))
4260 goto out;
4261
4262 retval = 0;
4263
Xin Long9fb657a2017-01-18 00:44:46 +08004264 if (asoc) {
4265 asoc->strreset_enable = params.assoc_value;
Xin Long9fb657a2017-01-18 00:44:46 +08004266 goto out;
4267 }
4268
Xin Long9430ff92019-03-18 20:06:09 +08004269 if (sctp_style(sk, TCP))
4270 params.assoc_id = SCTP_FUTURE_ASSOC;
4271
Xin Long99a62132019-01-28 15:08:44 +08004272 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4273 params.assoc_id == SCTP_ALL_ASSOC)
4274 ep->strreset_enable = params.assoc_value;
4275
4276 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4277 params.assoc_id == SCTP_ALL_ASSOC)
4278 list_for_each_entry(asoc, &ep->asocs, asocs)
4279 asoc->strreset_enable = params.assoc_value;
Xin Long9fb657a2017-01-18 00:44:46 +08004280
4281out:
4282 return retval;
4283}
4284
Xin Long7f9d68a2017-01-18 00:44:47 +08004285static int sctp_setsockopt_reset_streams(struct sock *sk,
4286 char __user *optval,
4287 unsigned int optlen)
4288{
4289 struct sctp_reset_streams *params;
4290 struct sctp_association *asoc;
4291 int retval = -EINVAL;
4292
Xin Long2342b8d2017-12-10 15:40:51 +08004293 if (optlen < sizeof(*params))
Xin Long7f9d68a2017-01-18 00:44:47 +08004294 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02004295 /* srs_number_streams is u16, so optlen can't be bigger than this. */
4296 optlen = min_t(unsigned int, optlen, USHRT_MAX +
4297 sizeof(__u16) * sizeof(*params));
Xin Long7f9d68a2017-01-18 00:44:47 +08004298
4299 params = memdup_user(optval, optlen);
4300 if (IS_ERR(params))
4301 return PTR_ERR(params);
4302
Xin Long2342b8d2017-12-10 15:40:51 +08004303 if (params->srs_number_streams * sizeof(__u16) >
4304 optlen - sizeof(*params))
4305 goto out;
4306
Xin Long7f9d68a2017-01-18 00:44:47 +08004307 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4308 if (!asoc)
4309 goto out;
4310
4311 retval = sctp_send_reset_streams(asoc, params);
4312
4313out:
4314 kfree(params);
4315 return retval;
4316}
4317
Xin Longa92ce1a2017-02-09 01:18:18 +08004318static int sctp_setsockopt_reset_assoc(struct sock *sk,
4319 char __user *optval,
4320 unsigned int optlen)
4321{
4322 struct sctp_association *asoc;
4323 sctp_assoc_t associd;
4324 int retval = -EINVAL;
4325
4326 if (optlen != sizeof(associd))
4327 goto out;
4328
4329 if (copy_from_user(&associd, optval, optlen)) {
4330 retval = -EFAULT;
4331 goto out;
4332 }
4333
4334 asoc = sctp_id2assoc(sk, associd);
4335 if (!asoc)
4336 goto out;
4337
4338 retval = sctp_send_reset_assoc(asoc);
4339
4340out:
4341 return retval;
4342}
4343
Xin Long242bd2d2017-02-09 01:18:20 +08004344static int sctp_setsockopt_add_streams(struct sock *sk,
4345 char __user *optval,
4346 unsigned int optlen)
4347{
4348 struct sctp_association *asoc;
4349 struct sctp_add_streams params;
4350 int retval = -EINVAL;
4351
4352 if (optlen != sizeof(params))
4353 goto out;
4354
4355 if (copy_from_user(&params, optval, optlen)) {
4356 retval = -EFAULT;
4357 goto out;
4358 }
4359
4360 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
4361 if (!asoc)
4362 goto out;
4363
4364 retval = sctp_send_add_streams(asoc, &params);
4365
4366out:
4367 return retval;
4368}
4369
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004370static int sctp_setsockopt_scheduler(struct sock *sk,
4371 char __user *optval,
4372 unsigned int optlen)
4373{
Xin Long7efba102019-01-28 15:08:46 +08004374 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004375 struct sctp_association *asoc;
4376 struct sctp_assoc_value params;
Xin Long7efba102019-01-28 15:08:46 +08004377 int retval = 0;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004378
4379 if (optlen < sizeof(params))
Xin Long7efba102019-01-28 15:08:46 +08004380 return -EINVAL;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004381
4382 optlen = sizeof(params);
Xin Long7efba102019-01-28 15:08:46 +08004383 if (copy_from_user(&params, optval, optlen))
4384 return -EFAULT;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004385
4386 if (params.assoc_value > SCTP_SS_MAX)
Xin Long7efba102019-01-28 15:08:46 +08004387 return -EINVAL;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004388
4389 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Long7efba102019-01-28 15:08:46 +08004390 if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4391 sctp_style(sk, UDP))
4392 return -EINVAL;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004393
Xin Long7efba102019-01-28 15:08:46 +08004394 if (asoc)
4395 return sctp_sched_set_sched(asoc, params.assoc_value);
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004396
Xin Longb59c19d2019-03-18 20:06:11 +08004397 if (sctp_style(sk, TCP))
4398 params.assoc_id = SCTP_FUTURE_ASSOC;
4399
Xin Long7efba102019-01-28 15:08:46 +08004400 if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4401 params.assoc_id == SCTP_ALL_ASSOC)
4402 sp->default_ss = params.assoc_value;
4403
4404 if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4405 params.assoc_id == SCTP_ALL_ASSOC) {
4406 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4407 int ret = sctp_sched_set_sched(asoc,
4408 params.assoc_value);
4409
4410 if (ret && !retval)
4411 retval = ret;
4412 }
4413 }
4414
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004415 return retval;
4416}
4417
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004418static int sctp_setsockopt_scheduler_value(struct sock *sk,
4419 char __user *optval,
4420 unsigned int optlen)
4421{
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004422 struct sctp_stream_value params;
Xin Longe7f28242019-01-28 15:08:33 +08004423 struct sctp_association *asoc;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004424 int retval = -EINVAL;
4425
4426 if (optlen < sizeof(params))
4427 goto out;
4428
4429 optlen = sizeof(params);
4430 if (copy_from_user(&params, optval, optlen)) {
4431 retval = -EFAULT;
4432 goto out;
4433 }
4434
4435 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Longe7f28242019-01-28 15:08:33 +08004436 if (!asoc && params.assoc_id != SCTP_CURRENT_ASSOC &&
4437 sctp_style(sk, UDP))
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004438 goto out;
4439
Xin Longe7f28242019-01-28 15:08:33 +08004440 if (asoc) {
4441 retval = sctp_sched_set_value(asoc, params.stream_id,
4442 params.stream_value, GFP_KERNEL);
4443 goto out;
4444 }
4445
4446 retval = 0;
4447
4448 list_for_each_entry(asoc, &sctp_sk(sk)->ep->asocs, asocs) {
4449 int ret = sctp_sched_set_value(asoc, params.stream_id,
4450 params.stream_value, GFP_KERNEL);
4451 if (ret && !retval) /* try to return the 1st error. */
4452 retval = ret;
4453 }
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004454
4455out:
4456 return retval;
4457}
4458
Xin Long772a5862017-12-08 21:03:58 +08004459static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4460 char __user *optval,
4461 unsigned int optlen)
4462{
4463 struct sctp_sock *sp = sctp_sk(sk);
Xin Long772a5862017-12-08 21:03:58 +08004464 struct sctp_assoc_value params;
Xin Long2e7709d2019-01-28 15:08:32 +08004465 struct sctp_association *asoc;
Xin Long772a5862017-12-08 21:03:58 +08004466 int retval = -EINVAL;
4467
4468 if (optlen < sizeof(params))
4469 goto out;
4470
4471 optlen = sizeof(params);
4472 if (copy_from_user(&params, optval, optlen)) {
4473 retval = -EFAULT;
4474 goto out;
4475 }
4476
Xin Long2e7709d2019-01-28 15:08:32 +08004477 asoc = sctp_id2assoc(sk, params.assoc_id);
4478 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4479 sctp_style(sk, UDP))
Xin Long772a5862017-12-08 21:03:58 +08004480 goto out;
4481
Xin Long2e7709d2019-01-28 15:08:32 +08004482 if (!sock_net(sk)->sctp.intl_enable || !sp->frag_interleave) {
Xin Long772a5862017-12-08 21:03:58 +08004483 retval = -EPERM;
4484 goto out;
4485 }
4486
Xin Longe55f4b82019-07-09 00:57:07 +08004487 sp->ep->intl_enable = !!params.assoc_value;
Xin Long772a5862017-12-08 21:03:58 +08004488
4489 retval = 0;
4490
4491out:
4492 return retval;
4493}
4494
Xin Longb0e9a2f2018-06-28 15:31:00 +08004495static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval,
4496 unsigned int optlen)
4497{
4498 int val;
4499
4500 if (!sctp_style(sk, TCP))
4501 return -EOPNOTSUPP;
4502
4503 if (sctp_sk(sk)->ep->base.bind_addr.port)
4504 return -EFAULT;
4505
4506 if (optlen < sizeof(int))
4507 return -EINVAL;
4508
4509 if (get_user(val, (int __user *)optval))
4510 return -EFAULT;
4511
4512 sctp_sk(sk)->reuse = !!val;
4513
4514 return 0;
4515}
4516
Xin Longd251f052019-01-28 15:08:45 +08004517static int sctp_assoc_ulpevent_type_set(struct sctp_event *param,
4518 struct sctp_association *asoc)
Xin Long480ba9c2018-11-18 16:08:54 +08004519{
Xin Long480ba9c2018-11-18 16:08:54 +08004520 struct sctp_ulpevent *event;
Xin Long480ba9c2018-11-18 16:08:54 +08004521
Xin Longd251f052019-01-28 15:08:45 +08004522 sctp_ulpevent_type_set(&asoc->subscribe, param->se_type, param->se_on);
Xin Long480ba9c2018-11-18 16:08:54 +08004523
Xin Longd251f052019-01-28 15:08:45 +08004524 if (param->se_type == SCTP_SENDER_DRY_EVENT && param->se_on) {
Xin Long480ba9c2018-11-18 16:08:54 +08004525 if (sctp_outq_is_empty(&asoc->outqueue)) {
4526 event = sctp_ulpevent_make_sender_dry_event(asoc,
4527 GFP_USER | __GFP_NOWARN);
Xin Longd251f052019-01-28 15:08:45 +08004528 if (!event)
4529 return -ENOMEM;
Xin Long480ba9c2018-11-18 16:08:54 +08004530
4531 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
4532 }
4533 }
4534
Xin Longd251f052019-01-28 15:08:45 +08004535 return 0;
4536}
4537
4538static int sctp_setsockopt_event(struct sock *sk, char __user *optval,
4539 unsigned int optlen)
4540{
4541 struct sctp_sock *sp = sctp_sk(sk);
4542 struct sctp_association *asoc;
4543 struct sctp_event param;
4544 int retval = 0;
4545
4546 if (optlen < sizeof(param))
4547 return -EINVAL;
4548
4549 optlen = sizeof(param);
4550 if (copy_from_user(&param, optval, optlen))
4551 return -EFAULT;
4552
4553 if (param.se_type < SCTP_SN_TYPE_BASE ||
4554 param.se_type > SCTP_SN_TYPE_MAX)
4555 return -EINVAL;
4556
4557 asoc = sctp_id2assoc(sk, param.se_assoc_id);
4558 if (!asoc && param.se_assoc_id > SCTP_ALL_ASSOC &&
4559 sctp_style(sk, UDP))
4560 return -EINVAL;
4561
4562 if (asoc)
4563 return sctp_assoc_ulpevent_type_set(&param, asoc);
4564
Xin Long99518612019-03-18 20:06:10 +08004565 if (sctp_style(sk, TCP))
4566 param.se_assoc_id = SCTP_FUTURE_ASSOC;
4567
Xin Longd251f052019-01-28 15:08:45 +08004568 if (param.se_assoc_id == SCTP_FUTURE_ASSOC ||
4569 param.se_assoc_id == SCTP_ALL_ASSOC)
4570 sctp_ulpevent_type_set(&sp->subscribe,
4571 param.se_type, param.se_on);
4572
4573 if (param.se_assoc_id == SCTP_CURRENT_ASSOC ||
4574 param.se_assoc_id == SCTP_ALL_ASSOC) {
4575 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4576 int ret = sctp_assoc_ulpevent_type_set(&param, asoc);
4577
4578 if (ret && !retval)
4579 retval = ret;
4580 }
4581 }
4582
Xin Long480ba9c2018-11-18 16:08:54 +08004583 return retval;
4584}
4585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586/* API 6.2 setsockopt(), getsockopt()
4587 *
4588 * Applications use setsockopt() and getsockopt() to set or retrieve
4589 * socket options. Socket options are used to change the default
4590 * behavior of sockets calls. They are described in Section 7.
4591 *
4592 * The syntax is:
4593 *
4594 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4595 * int __user *optlen);
4596 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4597 * int optlen);
4598 *
4599 * sd - the socket descript.
4600 * level - set to IPPROTO_SCTP for all SCTP options.
4601 * optname - the option name.
4602 * optval - the buffer to store the value of the option.
4603 * optlen - the size of the buffer.
4604 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004605static int sctp_setsockopt(struct sock *sk, int level, int optname,
4606 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607{
4608 int retval = 0;
4609
Daniel Borkmannbb333812013-06-28 19:49:40 +02004610 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
4612 /* I can hardly begin to describe how wrong this is. This is
4613 * so broken as to be worse than useless. The API draft
4614 * REALLY is NOT helpful here... I am not convinced that the
4615 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4616 * are at all well-founded.
4617 */
4618 if (level != SOL_SCTP) {
4619 struct sctp_af *af = sctp_sk(sk)->pf->af;
4620 retval = af->setsockopt(sk, level, optname, optval, optlen);
4621 goto out_nounlock;
4622 }
4623
wangweidong048ed4b2014-01-21 15:44:11 +08004624 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
4626 switch (optname) {
4627 case SCTP_SOCKOPT_BINDX_ADD:
4628 /* 'optlen' is the size of the addresses buffer. */
4629 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4630 optlen, SCTP_BINDX_ADD_ADDR);
4631 break;
4632
4633 case SCTP_SOCKOPT_BINDX_REM:
4634 /* 'optlen' is the size of the addresses buffer. */
4635 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4636 optlen, SCTP_BINDX_REM_ADDR);
4637 break;
4638
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004639 case SCTP_SOCKOPT_CONNECTX_OLD:
4640 /* 'optlen' is the size of the addresses buffer. */
4641 retval = sctp_setsockopt_connectx_old(sk,
4642 (struct sockaddr __user *)optval,
4643 optlen);
4644 break;
4645
Frank Filz3f7a87d2005-06-20 13:14:57 -07004646 case SCTP_SOCKOPT_CONNECTX:
4647 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004648 retval = sctp_setsockopt_connectx(sk,
4649 (struct sockaddr __user *)optval,
4650 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004651 break;
4652
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 case SCTP_DISABLE_FRAGMENTS:
4654 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4655 break;
4656
4657 case SCTP_EVENTS:
4658 retval = sctp_setsockopt_events(sk, optval, optlen);
4659 break;
4660
4661 case SCTP_AUTOCLOSE:
4662 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4663 break;
4664
4665 case SCTP_PEER_ADDR_PARAMS:
4666 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4667 break;
4668
Shan Wei4580ccc2011-01-18 22:39:00 +00004669 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07004670 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08004671 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004672 case SCTP_PARTIAL_DELIVERY_POINT:
4673 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4674 break;
Frank Filz77086102005-12-22 11:37:30 -08004675
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 case SCTP_INITMSG:
4677 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4678 break;
4679 case SCTP_DEFAULT_SEND_PARAM:
4680 retval = sctp_setsockopt_default_send_param(sk, optval,
4681 optlen);
4682 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02004683 case SCTP_DEFAULT_SNDINFO:
4684 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4685 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 case SCTP_PRIMARY_ADDR:
4687 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4688 break;
4689 case SCTP_SET_PEER_PRIMARY_ADDR:
4690 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4691 break;
4692 case SCTP_NODELAY:
4693 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4694 break;
4695 case SCTP_RTOINFO:
4696 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4697 break;
4698 case SCTP_ASSOCINFO:
4699 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4700 break;
4701 case SCTP_I_WANT_MAPPED_V4_ADDR:
4702 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4703 break;
4704 case SCTP_MAXSEG:
4705 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4706 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004707 case SCTP_ADAPTATION_LAYER:
4708 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004710 case SCTP_CONTEXT:
4711 retval = sctp_setsockopt_context(sk, optval, optlen);
4712 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004713 case SCTP_FRAGMENT_INTERLEAVE:
4714 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4715 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07004716 case SCTP_MAX_BURST:
4717 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4718 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004719 case SCTP_AUTH_CHUNK:
4720 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4721 break;
4722 case SCTP_HMAC_IDENT:
4723 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4724 break;
4725 case SCTP_AUTH_KEY:
4726 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4727 break;
4728 case SCTP_AUTH_ACTIVE_KEY:
4729 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4730 break;
4731 case SCTP_AUTH_DELETE_KEY:
4732 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4733 break;
Xin Long601590e2018-03-14 19:05:32 +08004734 case SCTP_AUTH_DEACTIVATE_KEY:
4735 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
4736 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09004737 case SCTP_AUTO_ASCONF:
4738 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4739 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00004740 case SCTP_PEER_ADDR_THLDS:
4741 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4742 break;
Geir Ola Vaagland0d3a421d2014-07-12 20:30:37 +02004743 case SCTP_RECVRCVINFO:
4744 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4745 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004746 case SCTP_RECVNXTINFO:
4747 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4748 break;
Xin Long28aa4c22016-07-09 19:47:40 +08004749 case SCTP_PR_SUPPORTED:
4750 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4751 break;
Xin Longf959fb42016-07-09 19:47:41 +08004752 case SCTP_DEFAULT_PRINFO:
4753 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4754 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08004755 case SCTP_RECONFIG_SUPPORTED:
4756 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4757 break;
Xin Long9fb657a2017-01-18 00:44:46 +08004758 case SCTP_ENABLE_STREAM_RESET:
4759 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4760 break;
Xin Long7f9d68a2017-01-18 00:44:47 +08004761 case SCTP_RESET_STREAMS:
4762 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4763 break;
Xin Longa92ce1a2017-02-09 01:18:18 +08004764 case SCTP_RESET_ASSOC:
4765 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4766 break;
Xin Long242bd2d2017-02-09 01:18:20 +08004767 case SCTP_ADD_STREAMS:
4768 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4769 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004770 case SCTP_STREAM_SCHEDULER:
4771 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4772 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004773 case SCTP_STREAM_SCHEDULER_VALUE:
4774 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4775 break;
Xin Long772a5862017-12-08 21:03:58 +08004776 case SCTP_INTERLEAVING_SUPPORTED:
4777 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4778 optlen);
4779 break;
Xin Longb0e9a2f2018-06-28 15:31:00 +08004780 case SCTP_REUSE_PORT:
4781 retval = sctp_setsockopt_reuse_port(sk, optval, optlen);
4782 break;
Xin Long480ba9c2018-11-18 16:08:54 +08004783 case SCTP_EVENT:
4784 retval = sctp_setsockopt_event(sk, optval, optlen);
4785 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 default:
4787 retval = -ENOPROTOOPT;
4788 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
wangweidong048ed4b2014-01-21 15:44:11 +08004791 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
4793out_nounlock:
4794 return retval;
4795}
4796
4797/* API 3.1.6 connect() - UDP Style Syntax
4798 *
4799 * An application may use the connect() call in the UDP model to initiate an
4800 * association without sending data.
4801 *
4802 * The syntax is:
4803 *
4804 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4805 *
4806 * sd: the socket descriptor to have a new association added to.
4807 *
4808 * nam: the address structure (either struct sockaddr_in or struct
4809 * sockaddr_in6 defined in RFC2553 [7]).
4810 *
4811 * len: the size of the address.
4812 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004813static int sctp_connect(struct sock *sk, struct sockaddr *addr,
Xin Long644fbde2018-05-20 16:39:10 +08004814 int addr_len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815{
Frank Filz3f7a87d2005-06-20 13:14:57 -07004816 struct sctp_af *af;
Xin Long9b6c0882019-06-26 16:31:39 +08004817 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
wangweidong048ed4b2014-01-21 15:44:11 +08004819 lock_sock(sk);
Daniel Borkmannbb333812013-06-28 19:49:40 +02004820 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4821 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822
Frank Filz3f7a87d2005-06-20 13:14:57 -07004823 /* Validate addr_len before calling common connect/connectx routine. */
Xin Long9b6c0882019-06-26 16:31:39 +08004824 af = sctp_get_af_specific(addr->sa_family);
4825 if (af && addr_len >= af->sockaddr_len)
Xin Long644fbde2018-05-20 16:39:10 +08004826 err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
wangweidong048ed4b2014-01-21 15:44:11 +08004828 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 return err;
4830}
4831
Xin Long644fbde2018-05-20 16:39:10 +08004832int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
4833 int addr_len, int flags)
4834{
4835 if (addr_len < sizeof(uaddr->sa_family))
4836 return -EINVAL;
4837
4838 if (uaddr->sa_family == AF_UNSPEC)
4839 return -EOPNOTSUPP;
4840
4841 return sctp_connect(sock->sk, uaddr, addr_len, flags);
4842}
4843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844/* FIXME: Write comments. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004845static int sctp_disconnect(struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846{
4847 return -EOPNOTSUPP; /* STUB */
4848}
4849
4850/* 4.1.4 accept() - TCP Style Syntax
4851 *
4852 * Applications use accept() call to remove an established SCTP
4853 * association from the accept queue of the endpoint. A new socket
4854 * descriptor will be returned from accept() to represent the newly
4855 * formed association.
4856 */
David Howellscdfbabf2017-03-09 08:09:05 +00004857static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858{
4859 struct sctp_sock *sp;
4860 struct sctp_endpoint *ep;
4861 struct sock *newsk = NULL;
4862 struct sctp_association *asoc;
4863 long timeo;
4864 int error = 0;
4865
wangweidong048ed4b2014-01-21 15:44:11 +08004866 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
4868 sp = sctp_sk(sk);
4869 ep = sp->ep;
4870
4871 if (!sctp_style(sk, TCP)) {
4872 error = -EOPNOTSUPP;
4873 goto out;
4874 }
4875
4876 if (!sctp_sstate(sk, LISTENING)) {
4877 error = -EINVAL;
4878 goto out;
4879 }
4880
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07004881 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
4883 error = sctp_wait_for_accept(sk, timeo);
4884 if (error)
4885 goto out;
4886
4887 /* We treat the list of associations on the endpoint as the accept
4888 * queue and pick the first association on the list.
4889 */
4890 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4891
David Howellscdfbabf2017-03-09 08:09:05 +00004892 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 if (!newsk) {
4894 error = -ENOMEM;
4895 goto out;
4896 }
4897
4898 /* Populate the fields of the newsk from the oldsk and migrate the
4899 * asoc to the newsk.
4900 */
Xin Long89664c622019-03-03 17:54:53 +08004901 error = sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4902 if (error) {
4903 sk_common_release(newsk);
4904 newsk = NULL;
4905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
4907out:
wangweidong048ed4b2014-01-21 15:44:11 +08004908 release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004909 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 return newsk;
4911}
4912
4913/* The SCTP ioctl handler. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004914static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004916 int rc = -ENOTCONN;
4917
wangweidong048ed4b2014-01-21 15:44:11 +08004918 lock_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004919
4920 /*
4921 * SEQPACKET-style sockets in LISTENING state are valid, for
4922 * SCTP, so only discard TCP-style sockets in LISTENING state.
4923 */
4924 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4925 goto out;
4926
4927 switch (cmd) {
4928 case SIOCINQ: {
4929 struct sk_buff *skb;
4930 unsigned int amount = 0;
4931
4932 skb = skb_peek(&sk->sk_receive_queue);
4933 if (skb != NULL) {
4934 /*
4935 * We will only return the amount of this packet since
4936 * that is all that will be read.
4937 */
4938 amount = skb->len;
4939 }
4940 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004941 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07004942 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004943 default:
4944 rc = -ENOIOCTLCMD;
4945 break;
4946 }
4947out:
wangweidong048ed4b2014-01-21 15:44:11 +08004948 release_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004949 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950}
4951
4952/* This is the function which gets called during socket creation to
4953 * initialized the SCTP-specific portion of the sock.
4954 * The sock structure should already be zero-filled memory.
4955 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004956static int sctp_init_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004958 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 struct sctp_sock *sp;
4960
Daniel Borkmannbb333812013-06-28 19:49:40 +02004961 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
4963 sp = sctp_sk(sk);
4964
4965 /* Initialize the SCTP per socket area. */
4966 switch (sk->sk_type) {
4967 case SOCK_SEQPACKET:
4968 sp->type = SCTP_SOCKET_UDP;
4969 break;
4970 case SOCK_STREAM:
4971 sp->type = SCTP_SOCKET_TCP;
4972 break;
4973 default:
4974 return -ESOCKTNOSUPPORT;
4975 }
4976
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004977 sk->sk_gso_type = SKB_GSO_SCTP;
4978
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 /* Initialize default send parameters. These parameters can be
4980 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4981 */
4982 sp->default_stream = 0;
4983 sp->default_ppid = 0;
4984 sp->default_flags = 0;
4985 sp->default_context = 0;
4986 sp->default_timetolive = 0;
4987
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004988 sp->default_rcv_context = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004989 sp->max_burst = net->sctp.max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004990
Neil Horman3c681982012-10-24 09:20:03 +00004991 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4992
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 /* Initialize default setup parameters. These parameters
4994 * can be modified with the SCTP_INITMSG socket option or
4995 * overridden by the SCTP_INIT CMSG.
4996 */
4997 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4998 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004999 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
5000 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
5002 /* Initialize default RTO related parameters. These parameters can
5003 * be modified for with the SCTP_RTOINFO socket option.
5004 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005005 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
5006 sp->rtoinfo.srto_max = net->sctp.rto_max;
5007 sp->rtoinfo.srto_min = net->sctp.rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
5009 /* Initialize default association related parameters. These parameters
5010 * can be modified with the SCTP_ASSOCINFO socket option.
5011 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005012 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 sp->assocparams.sasoc_number_peer_destinations = 0;
5014 sp->assocparams.sasoc_peer_rwnd = 0;
5015 sp->assocparams.sasoc_local_rwnd = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005016 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
5018 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005019 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 */
Xin Long2cc0eeb2018-11-18 16:08:51 +08005021 sp->subscribe = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
5023 /* Default Peer Address Parameters. These defaults can
5024 * be modified via SCTP_PEER_ADDR_PARAMS
5025 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005026 sp->hbinterval = net->sctp.hb_interval;
5027 sp->pathmaxrxt = net->sctp.max_retrans_path;
Xin Long8add5432019-01-28 15:08:29 +08005028 sp->pf_retrans = net->sctp.pf_retrans;
wangweidong4e2d52b2013-12-23 12:16:54 +08005029 sp->pathmtu = 0; /* allow default discovery */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005030 sp->sackdelay = net->sctp.sack_timeout;
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07005031 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08005032 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005033 SPP_PMTUD_ENABLE |
5034 SPP_SACKDELAY_ENABLE;
Xin Long7efba102019-01-28 15:08:46 +08005035 sp->default_ss = SCTP_SS_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036
5037 /* If enabled no SCTP message fragmentation will be performed.
5038 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
5039 */
5040 sp->disable_fragments = 0;
5041
Sridhar Samudrala208edef2006-09-29 17:08:01 -07005042 /* Enable Nagle algorithm by default. */
5043 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
Geir Ola Vaagland0d3a421d2014-07-12 20:30:37 +02005045 sp->recvrcvinfo = 0;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02005046 sp->recvnxtinfo = 0;
Geir Ola Vaagland0d3a421d2014-07-12 20:30:37 +02005047
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 /* Enable by default. */
5049 sp->v4mapped = 1;
5050
5051 /* Auto-close idle associations after the configured
5052 * number of seconds. A value of 0 disables this
5053 * feature. Configure through the SCTP_AUTOCLOSE socket option,
5054 * for UDP-style sockets only.
5055 */
5056 sp->autoclose = 0;
5057
5058 /* User specified fragmentation limit. */
5059 sp->user_frag = 0;
5060
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005061 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
5063 sp->pf = sctp_get_pf_specific(sk->sk_family);
5064
5065 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005066 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005068 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
5070 /* Create a per socket endpoint structure. Even if we
5071 * change the data structure relationships, this may still
5072 * be useful for storing pre-connect address information.
5073 */
Daniel Borkmannc164b832013-06-14 18:24:06 +02005074 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
5075 if (!sp->ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 return -ENOMEM;
5077
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 sp->hmac = NULL;
5079
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02005080 sk->sk_destruct = sctp_destruct_sock;
5081
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08005083
5084 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08005085 sk_sockets_allocated_inc(sk);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005086 sock_prot_inuse_add(net, sk->sk_prot, 1);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03005087
5088 /* Nothing can fail after this block, otherwise
5089 * sctp_destroy_sock() will be called without addr_wq_lock held
5090 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005091 if (net->sctp.default_auto_asconf) {
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03005092 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda9f7d6532011-04-26 19:32:51 +09005093 list_add_tail(&sp->auto_asconf_list,
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00005094 &net->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09005095 sp->do_auto_asconf = 1;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03005096 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
5097 } else {
Michio Honda9f7d6532011-04-26 19:32:51 +09005098 sp->do_auto_asconf = 0;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03005099 }
5100
David S. Miller6f756a82008-11-23 17:34:03 -08005101 local_bh_enable();
5102
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 return 0;
5104}
5105
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03005106/* Cleanup any SCTP per socket resources. Must be called with
5107 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
5108 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02005109static void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110{
Michio Honda9f7d6532011-04-26 19:32:51 +09005111 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
Daniel Borkmannbb333812013-06-28 19:49:40 +02005113 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
5115 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09005116 sp = sctp_sk(sk);
Daniel Borkmann1abd1652013-06-06 15:53:47 +02005117 /* This could happen during socket init, thus we bail out
5118 * early, since the rest of the below is not setup either.
5119 */
5120 if (sp->ep == NULL)
5121 return;
5122
Michio Honda9f7d6532011-04-26 19:32:51 +09005123 if (sp->do_auto_asconf) {
5124 sp->do_auto_asconf = 0;
5125 list_del(&sp->auto_asconf_list);
5126 }
5127 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08005128 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08005129 sk_sockets_allocated_dec(sk);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08005130 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08005131 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132}
5133
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02005134/* Triggered when there are no references on the socket anymore */
5135static void sctp_destruct_sock(struct sock *sk)
5136{
5137 struct sctp_sock *sp = sctp_sk(sk);
5138
5139 /* Free up the HMAC transform. */
Herbert Xu5821c762016-01-24 21:20:12 +08005140 crypto_free_shash(sp->hmac);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02005141
5142 inet_sock_destruct(sk);
5143}
5144
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145/* API 4.1.7 shutdown() - TCP Style Syntax
5146 * int shutdown(int socket, int how);
5147 *
5148 * sd - the socket descriptor of the association to be closed.
5149 * how - Specifies the type of shutdown. The values are
5150 * as follows:
5151 * SHUT_RD
5152 * Disables further receive operations. No SCTP
5153 * protocol action is taken.
5154 * SHUT_WR
5155 * Disables further send operations, and initiates
5156 * the SCTP shutdown sequence.
5157 * SHUT_RDWR
5158 * Disables further send and receive operations
5159 * and initiates the SCTP shutdown sequence.
5160 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02005161static void sctp_shutdown(struct sock *sk, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00005163 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 struct sctp_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
5166 if (!sctp_style(sk, TCP))
5167 return;
5168
Xin Long5bf35dd2016-11-13 21:44:37 +08005169 ep = sctp_sk(sk)->ep;
5170 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
5171 struct sctp_association *asoc;
5172
Yafang Shaocbabf462017-12-20 11:12:54 +08005173 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Xin Long5bf35dd2016-11-13 21:44:37 +08005174 asoc = list_entry(ep->asocs.next,
5175 struct sctp_association, asocs);
5176 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 }
5178}
5179
Xin Long52c52a62016-04-14 15:35:30 +08005180int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
5181 struct sctp_info *info)
5182{
5183 struct sctp_transport *prim;
5184 struct list_head *pos;
5185 int mask;
5186
5187 memset(info, 0, sizeof(*info));
5188 if (!asoc) {
5189 struct sctp_sock *sp = sctp_sk(sk);
5190
5191 info->sctpi_s_autoclose = sp->autoclose;
5192 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
5193 info->sctpi_s_pd_point = sp->pd_point;
5194 info->sctpi_s_nodelay = sp->nodelay;
5195 info->sctpi_s_disable_fragments = sp->disable_fragments;
5196 info->sctpi_s_v4mapped = sp->v4mapped;
5197 info->sctpi_s_frag_interleave = sp->frag_interleave;
Xin Long40eb90e92016-05-29 17:42:13 +08005198 info->sctpi_s_type = sp->type;
Xin Long52c52a62016-04-14 15:35:30 +08005199
5200 return 0;
5201 }
5202
5203 info->sctpi_tag = asoc->c.my_vtag;
5204 info->sctpi_state = asoc->state;
5205 info->sctpi_rwnd = asoc->a_rwnd;
5206 info->sctpi_unackdata = asoc->unack_data;
5207 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08005208 info->sctpi_instrms = asoc->stream.incnt;
5209 info->sctpi_outstrms = asoc->stream.outcnt;
Xin Long52c52a62016-04-14 15:35:30 +08005210 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
5211 info->sctpi_inqueue++;
5212 list_for_each(pos, &asoc->outqueue.out_chunk_list)
5213 info->sctpi_outqueue++;
5214 info->sctpi_overall_error = asoc->overall_error_count;
5215 info->sctpi_max_burst = asoc->max_burst;
5216 info->sctpi_maxseg = asoc->frag_point;
5217 info->sctpi_peer_rwnd = asoc->peer.rwnd;
5218 info->sctpi_peer_tag = asoc->c.peer_vtag;
5219
5220 mask = asoc->peer.ecn_capable << 1;
5221 mask = (mask | asoc->peer.ipv4_address) << 1;
5222 mask = (mask | asoc->peer.ipv6_address) << 1;
5223 mask = (mask | asoc->peer.hostname_address) << 1;
5224 mask = (mask | asoc->peer.asconf_capable) << 1;
5225 mask = (mask | asoc->peer.prsctp_capable) << 1;
5226 mask = (mask | asoc->peer.auth_capable);
5227 info->sctpi_peer_capable = mask;
5228 mask = asoc->peer.sack_needed << 1;
5229 mask = (mask | asoc->peer.sack_generation) << 1;
5230 mask = (mask | asoc->peer.zero_window_announced);
5231 info->sctpi_peer_sack = mask;
5232
5233 info->sctpi_isacks = asoc->stats.isacks;
5234 info->sctpi_osacks = asoc->stats.osacks;
5235 info->sctpi_opackets = asoc->stats.opackets;
5236 info->sctpi_ipackets = asoc->stats.ipackets;
5237 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
5238 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
5239 info->sctpi_idupchunks = asoc->stats.idupchunks;
5240 info->sctpi_gapcnt = asoc->stats.gapcnt;
5241 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
5242 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
5243 info->sctpi_oodchunks = asoc->stats.oodchunks;
5244 info->sctpi_iodchunks = asoc->stats.iodchunks;
5245 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
5246 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
5247
5248 prim = asoc->peer.primary_path;
Stefano Brivioee6c88b2017-08-23 13:27:13 +02005249 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
Xin Long52c52a62016-04-14 15:35:30 +08005250 info->sctpi_p_state = prim->state;
5251 info->sctpi_p_cwnd = prim->cwnd;
5252 info->sctpi_p_srtt = prim->srtt;
5253 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
5254 info->sctpi_p_hbinterval = prim->hbinterval;
5255 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
5256 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
5257 info->sctpi_p_ssthresh = prim->ssthresh;
5258 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
5259 info->sctpi_p_flight_size = prim->flight_size;
5260 info->sctpi_p_error = prim->error_count;
5261
5262 return 0;
5263}
5264EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
5265
Xin Long626d16f2016-04-14 15:35:31 +08005266/* use callback to avoid exporting the core structure */
Tom Herbert97a6ec42017-12-04 10:31:41 -08005267void sctp_transport_walk_start(struct rhashtable_iter *iter)
Xin Long626d16f2016-04-14 15:35:31 +08005268{
Xin Long7fda7022016-11-15 23:23:11 +08005269 rhltable_walk_enter(&sctp_transport_hashtable, iter);
Xin Long626d16f2016-04-14 15:35:31 +08005270
Tom Herbert97a6ec42017-12-04 10:31:41 -08005271 rhashtable_walk_start(iter);
Xin Long626d16f2016-04-14 15:35:31 +08005272}
5273
5274void sctp_transport_walk_stop(struct rhashtable_iter *iter)
5275{
5276 rhashtable_walk_stop(iter);
5277 rhashtable_walk_exit(iter);
5278}
5279
5280struct sctp_transport *sctp_transport_get_next(struct net *net,
5281 struct rhashtable_iter *iter)
5282{
5283 struct sctp_transport *t;
5284
5285 t = rhashtable_walk_next(iter);
5286 for (; t; t = rhashtable_walk_next(iter)) {
5287 if (IS_ERR(t)) {
5288 if (PTR_ERR(t) == -EAGAIN)
5289 continue;
5290 break;
5291 }
5292
Xin Longbab1be72018-08-27 18:38:31 +08005293 if (!sctp_transport_hold(t))
5294 continue;
5295
Xin Long626d16f2016-04-14 15:35:31 +08005296 if (net_eq(sock_net(t->asoc->base.sk), net) &&
5297 t->asoc->peer.primary_path == t)
5298 break;
Xin Longbab1be72018-08-27 18:38:31 +08005299
5300 sctp_transport_put(t);
Xin Long626d16f2016-04-14 15:35:31 +08005301 }
5302
5303 return t;
5304}
5305
5306struct sctp_transport *sctp_transport_get_idx(struct net *net,
5307 struct rhashtable_iter *iter,
5308 int pos)
5309{
Xin Longbab1be72018-08-27 18:38:31 +08005310 struct sctp_transport *t;
Xin Long626d16f2016-04-14 15:35:31 +08005311
Xin Longbab1be72018-08-27 18:38:31 +08005312 if (!pos)
5313 return SEQ_START_TOKEN;
Xin Long626d16f2016-04-14 15:35:31 +08005314
Xin Longbab1be72018-08-27 18:38:31 +08005315 while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
5316 if (!--pos)
5317 break;
5318 sctp_transport_put(t);
5319 }
5320
5321 return t;
Xin Long626d16f2016-04-14 15:35:31 +08005322}
5323
5324int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
5325 void *p) {
5326 int err = 0;
5327 int hash = 0;
5328 struct sctp_ep_common *epb;
5329 struct sctp_hashbucket *head;
5330
5331 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
5332 hash++, head++) {
Xin Long581409d2017-06-10 14:48:14 +08005333 read_lock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08005334 sctp_for_each_hentry(epb, &head->chain) {
5335 err = cb(sctp_ep(epb), p);
5336 if (err)
5337 break;
5338 }
Xin Long581409d2017-06-10 14:48:14 +08005339 read_unlock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08005340 }
5341
5342 return err;
5343}
5344EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
5345
5346int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
5347 struct net *net,
5348 const union sctp_addr *laddr,
5349 const union sctp_addr *paddr, void *p)
5350{
5351 struct sctp_transport *transport;
Xin Long08abb792016-12-15 23:05:52 +08005352 int err;
Xin Long626d16f2016-04-14 15:35:31 +08005353
5354 rcu_read_lock();
5355 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
Xin Long626d16f2016-04-14 15:35:31 +08005356 rcu_read_unlock();
Xin Long08abb792016-12-15 23:05:52 +08005357 if (!transport)
5358 return -ENOENT;
5359
Xin Long1cceda782016-09-29 02:55:44 +08005360 err = cb(transport, p);
Xin Longcd26da42016-10-31 20:32:31 +08005361 sctp_transport_put(transport);
Xin Long1cceda782016-09-29 02:55:44 +08005362
Xin Long626d16f2016-04-14 15:35:31 +08005363 return err;
5364}
5365EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
5366
5367int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
Xin Longd25adbe2017-09-15 11:02:21 +08005368 int (*cb_done)(struct sctp_transport *, void *),
5369 struct net *net, int *pos, void *p) {
Xin Long626d16f2016-04-14 15:35:31 +08005370 struct rhashtable_iter hti;
Xin Longd25adbe2017-09-15 11:02:21 +08005371 struct sctp_transport *tsp;
5372 int ret;
Xin Long626d16f2016-04-14 15:35:31 +08005373
Xin Longd25adbe2017-09-15 11:02:21 +08005374again:
Xin Longf53d77e2018-01-23 18:22:25 +08005375 ret = 0;
Tom Herbert97a6ec42017-12-04 10:31:41 -08005376 sctp_transport_walk_start(&hti);
Xin Long626d16f2016-04-14 15:35:31 +08005377
Xin Longd25adbe2017-09-15 11:02:21 +08005378 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5379 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
Xin Longd25adbe2017-09-15 11:02:21 +08005380 ret = cb(tsp, p);
5381 if (ret)
Xin Long626d16f2016-04-14 15:35:31 +08005382 break;
Xin Longd25adbe2017-09-15 11:02:21 +08005383 (*pos)++;
5384 sctp_transport_put(tsp);
Xin Long626d16f2016-04-14 15:35:31 +08005385 }
Xin Long626d16f2016-04-14 15:35:31 +08005386 sctp_transport_walk_stop(&hti);
Xin Long53fa1032016-04-14 15:35:35 +08005387
Xin Longd25adbe2017-09-15 11:02:21 +08005388 if (ret) {
5389 if (cb_done && !cb_done(tsp, p)) {
5390 (*pos)++;
5391 sctp_transport_put(tsp);
5392 goto again;
5393 }
5394 sctp_transport_put(tsp);
5395 }
5396
5397 return ret;
Xin Long626d16f2016-04-14 15:35:31 +08005398}
5399EXPORT_SYMBOL_GPL(sctp_for_each_transport);
5400
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401/* 7.2.1 Association Status (SCTP_STATUS)
5402
5403 * Applications can retrieve current status information about an
5404 * association, including association state, peer receiver window size,
5405 * number of unacked data chunks, and number of data chunks pending
5406 * receipt. This information is read-only.
5407 */
5408static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5409 char __user *optval,
5410 int __user *optlen)
5411{
5412 struct sctp_status status;
5413 struct sctp_association *asoc = NULL;
5414 struct sctp_transport *transport;
5415 sctp_assoc_t associd;
5416 int retval = 0;
5417
Neil Horman408f22e2007-06-16 14:03:45 -04005418 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 retval = -EINVAL;
5420 goto out;
5421 }
5422
Neil Horman408f22e2007-06-16 14:03:45 -04005423 len = sizeof(status);
5424 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 retval = -EFAULT;
5426 goto out;
5427 }
5428
5429 associd = status.sstat_assoc_id;
5430 asoc = sctp_id2assoc(sk, associd);
5431 if (!asoc) {
5432 retval = -EINVAL;
5433 goto out;
5434 }
5435
5436 transport = asoc->peer.primary_path;
5437
5438 status.sstat_assoc_id = sctp_assoc2id(asoc);
Daniel Borkmann38ab1fa2014-08-28 15:28:26 +02005439 status.sstat_state = sctp_assoc_to_state(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 status.sstat_rwnd = asoc->peer.rwnd;
5441 status.sstat_unackdata = asoc->unack_data;
5442
5443 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08005444 status.sstat_instrms = asoc->stream.incnt;
5445 status.sstat_outstrms = asoc->stream.outcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 status.sstat_fragmentation_point = asoc->frag_point;
5447 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08005448 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5449 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 /* Map ipv4 address into v4-mapped-on-v6 address. */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005451 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005453 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 status.sstat_primary.spinfo_cwnd = transport->cwnd;
5455 status.sstat_primary.spinfo_srtt = transport->srtt;
5456 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005457 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Frank Filz3f7a87d2005-06-20 13:14:57 -07005459 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5460 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5461
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 if (put_user(len, optlen)) {
5463 retval = -EFAULT;
5464 goto out;
5465 }
5466
Daniel Borkmannbb333812013-06-28 19:49:40 +02005467 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5468 __func__, len, status.sstat_state, status.sstat_rwnd,
5469 status.sstat_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
5471 if (copy_to_user(optval, &status, len)) {
5472 retval = -EFAULT;
5473 goto out;
5474 }
5475
5476out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00005477 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478}
5479
5480
5481/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5482 *
5483 * Applications can retrieve information about a specific peer address
5484 * of an association, including its reachability state, congestion
5485 * window, and retransmission timer values. This information is
5486 * read-only.
5487 */
5488static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5489 char __user *optval,
5490 int __user *optlen)
5491{
5492 struct sctp_paddrinfo pinfo;
5493 struct sctp_transport *transport;
5494 int retval = 0;
5495
Neil Horman408f22e2007-06-16 14:03:45 -04005496 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 retval = -EINVAL;
5498 goto out;
5499 }
5500
Neil Horman408f22e2007-06-16 14:03:45 -04005501 len = sizeof(pinfo);
5502 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 retval = -EFAULT;
5504 goto out;
5505 }
5506
5507 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5508 pinfo.spinfo_assoc_id);
5509 if (!transport)
5510 return -EINVAL;
5511
5512 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005513 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 pinfo.spinfo_cwnd = transport->cwnd;
5515 pinfo.spinfo_srtt = transport->srtt;
5516 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005517 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518
Frank Filz3f7a87d2005-06-20 13:14:57 -07005519 if (pinfo.spinfo_state == SCTP_UNKNOWN)
5520 pinfo.spinfo_state = SCTP_ACTIVE;
5521
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 if (put_user(len, optlen)) {
5523 retval = -EFAULT;
5524 goto out;
5525 }
5526
5527 if (copy_to_user(optval, &pinfo, len)) {
5528 retval = -EFAULT;
5529 goto out;
5530 }
5531
5532out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00005533 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534}
5535
5536/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5537 *
5538 * This option is a on/off flag. If enabled no SCTP message
5539 * fragmentation will be performed. Instead if a message being sent
5540 * exceeds the current PMTU size, the message will NOT be sent and
5541 * instead a error will be indicated to the user.
5542 */
5543static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5544 char __user *optval, int __user *optlen)
5545{
5546 int val;
5547
5548 if (len < sizeof(int))
5549 return -EINVAL;
5550
5551 len = sizeof(int);
5552 val = (sctp_sk(sk)->disable_fragments == 1);
5553 if (put_user(len, optlen))
5554 return -EFAULT;
5555 if (copy_to_user(optval, &val, len))
5556 return -EFAULT;
5557 return 0;
5558}
5559
5560/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5561 *
5562 * This socket option is used to specify various notifications and
5563 * ancillary data the user wishes to receive.
5564 */
5565static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5566 int __user *optlen)
5567{
Xin Long2cc0eeb2018-11-18 16:08:51 +08005568 struct sctp_event_subscribe subscribe;
5569 __u8 *sn_type = (__u8 *)&subscribe;
5570 int i;
5571
Jiri Slabya4b8e712016-10-21 14:13:24 +02005572 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00005574 if (len > sizeof(struct sctp_event_subscribe))
5575 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04005576 if (put_user(len, optlen))
5577 return -EFAULT;
Xin Long2cc0eeb2018-11-18 16:08:51 +08005578
5579 for (i = 0; i < len; i++)
5580 sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe,
5581 SCTP_SN_TYPE_BASE + i);
5582
5583 if (copy_to_user(optval, &subscribe, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 return -EFAULT;
Xin Long2cc0eeb2018-11-18 16:08:51 +08005585
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 return 0;
5587}
5588
5589/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5590 *
5591 * This socket option is applicable to the UDP-style socket only. When
5592 * set it will cause associations that are idle for more than the
5593 * specified number of seconds to automatically close. An association
5594 * being idle is defined an association that has NOT sent or received
5595 * user data. The special value of '0' indicates that no automatic
5596 * close of any associations should be performed. The option expects an
5597 * integer defining the number of seconds of idle time before an
5598 * association is closed.
5599 */
5600static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5601{
5602 /* Applicable to UDP-style socket only */
5603 if (sctp_style(sk, TCP))
5604 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04005605 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005607 len = sizeof(int);
5608 if (put_user(len, optlen))
5609 return -EFAULT;
David Windsorb2ce04c2017-06-10 22:50:43 -04005610 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 return -EFAULT;
5612 return 0;
5613}
5614
5615/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00005616int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617{
Benjamin Poirier0343c552012-03-08 05:55:58 +00005618 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005619 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 struct socket *sock;
5621 int err = 0;
5622
Xin Longdf80cd92017-10-17 23:26:10 +08005623 /* Do not peel off from one netns to another one. */
5624 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5625 return -EINVAL;
5626
Benjamin Poirier0343c552012-03-08 05:55:58 +00005627 if (!asoc)
5628 return -EINVAL;
5629
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 /* An association cannot be branched off from an already peeled-off
5631 * socket, nor is this supported for tcp style sockets.
5632 */
5633 if (!sctp_style(sk, UDP))
5634 return -EINVAL;
5635
5636 /* Create a new socket. */
5637 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5638 if (err < 0)
5639 return err;
5640
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005641 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005642
5643 /* Make peeled-off sockets more like 1-1 accepted sockets.
Richard Hainesb7e10c22018-02-24 16:18:51 +00005644 * Set the daddr and initialize id to something more random and also
5645 * copy over any ip options.
Vlad Yasevich4f444302006-10-30 18:54:32 -08005646 */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005647 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
Richard Hainesb7e10c22018-02-24 16:18:51 +00005648 sp->pf->copy_ip_options(sk, sock->sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005649
5650 /* Populate the fields of the newsk from the oldsk and migrate the
5651 * asoc to the newsk.
5652 */
Xin Long89664c622019-03-03 17:54:53 +08005653 err = sctp_sock_migrate(sk, sock->sk, asoc,
5654 SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
5655 if (err) {
5656 sock_release(sock);
5657 sock = NULL;
5658 }
Vlad Yasevich4f444302006-10-30 18:54:32 -08005659
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 *sockp = sock;
5661
5662 return err;
5663}
Benjamin Poirier0343c552012-03-08 05:55:58 +00005664EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005666static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5667 struct file **newfile, unsigned flags)
5668{
5669 struct socket *newsock;
5670 int retval;
5671
5672 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5673 if (retval < 0)
5674 goto out;
5675
5676 /* Map the socket to an unused fd that can be returned to the user. */
5677 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5678 if (retval < 0) {
5679 sock_release(newsock);
5680 goto out;
5681 }
5682
5683 *newfile = sock_alloc_file(newsock, 0, NULL);
5684 if (IS_ERR(*newfile)) {
5685 put_unused_fd(retval);
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005686 retval = PTR_ERR(*newfile);
5687 *newfile = NULL;
5688 return retval;
5689 }
5690
5691 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5692 retval);
5693
5694 peeloff->sd = retval;
5695
5696 if (flags & SOCK_NONBLOCK)
5697 (*newfile)->f_flags |= O_NONBLOCK;
5698out:
5699 return retval;
5700}
5701
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5703{
5704 sctp_peeloff_arg_t peeloff;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005705 struct file *newfile = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707
Neil Horman408f22e2007-06-16 14:03:45 -04005708 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005710 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 if (copy_from_user(&peeloff, optval, len))
5712 return -EFAULT;
5713
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005714 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 if (retval < 0)
5716 goto out;
5717
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04005719 if (put_user(len, optlen)) {
5720 fput(newfile);
5721 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04005722 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04005723 }
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005724
5725 if (copy_to_user(optval, &peeloff, len)) {
5726 fput(newfile);
5727 put_unused_fd(retval);
5728 return -EFAULT;
5729 }
5730 fd_install(retval, newfile);
5731out:
5732 return retval;
5733}
5734
5735static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5736 char __user *optval, int __user *optlen)
5737{
5738 sctp_peeloff_flags_arg_t peeloff;
5739 struct file *newfile = NULL;
5740 int retval = 0;
5741
5742 if (len < sizeof(sctp_peeloff_flags_arg_t))
5743 return -EINVAL;
5744 len = sizeof(sctp_peeloff_flags_arg_t);
5745 if (copy_from_user(&peeloff, optval, len))
5746 return -EFAULT;
5747
5748 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5749 &newfile, peeloff.flags);
5750 if (retval < 0)
5751 goto out;
5752
5753 /* Return the fd mapped to the new socket. */
5754 if (put_user(len, optlen)) {
5755 fput(newfile);
5756 put_unused_fd(retval);
5757 return -EFAULT;
5758 }
5759
Al Viro56b31d12012-08-18 00:25:51 -04005760 if (copy_to_user(optval, &peeloff, len)) {
5761 fput(newfile);
5762 put_unused_fd(retval);
5763 return -EFAULT;
5764 }
5765 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766out:
5767 return retval;
5768}
5769
5770/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5771 *
5772 * Applications can enable or disable heartbeats for any peer address of
5773 * an association, modify an address's heartbeat interval, force a
5774 * heartbeat to be sent immediately, and adjust the address's maximum
5775 * number of retransmissions sent before an address is considered
5776 * unreachable. The following structure is used to access and modify an
5777 * address's parameters:
5778 *
5779 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005780 * sctp_assoc_t spp_assoc_id;
5781 * struct sockaddr_storage spp_address;
5782 * uint32_t spp_hbinterval;
5783 * uint16_t spp_pathmaxrxt;
5784 * uint32_t spp_pathmtu;
5785 * uint32_t spp_sackdelay;
5786 * uint32_t spp_flags;
5787 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08005789 * spp_assoc_id - (one-to-many style socket) This is filled in the
5790 * application, and identifies the association for
5791 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 * spp_address - This specifies which address is of interest.
5793 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005794 * in milliseconds. If a value of zero
5795 * is present in this field then no changes are to
5796 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 * spp_pathmaxrxt - This contains the maximum number of
5798 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08005799 * considered unreachable. If a value of zero
5800 * is present in this field then no changes are to
5801 * be made to this parameter.
5802 * spp_pathmtu - When Path MTU discovery is disabled the value
5803 * specified here will be the "fixed" path mtu.
5804 * Note that if the spp_address field is empty
5805 * then all associations on this address will
5806 * have this fixed path mtu set upon them.
5807 *
5808 * spp_sackdelay - When delayed sack is enabled, this value specifies
5809 * the number of milliseconds that sacks will be delayed
5810 * for. This value will apply to all addresses of an
5811 * association if the spp_address field is empty. Note
5812 * also, that if delayed sack is enabled and this
5813 * value is set to 0, no change is made to the last
5814 * recorded delayed sack timer value.
5815 *
5816 * spp_flags - These flags are used to control various features
5817 * on an association. The flag field may contain
5818 * zero or more of the following options.
5819 *
5820 * SPP_HB_ENABLE - Enable heartbeats on the
5821 * specified address. Note that if the address
5822 * field is empty all addresses for the association
5823 * have heartbeats enabled upon them.
5824 *
5825 * SPP_HB_DISABLE - Disable heartbeats on the
5826 * speicifed address. Note that if the address
5827 * field is empty all addresses for the association
5828 * will have their heartbeats disabled. Note also
5829 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5830 * mutually exclusive, only one of these two should
5831 * be specified. Enabling both fields will have
5832 * undetermined results.
5833 *
5834 * SPP_HB_DEMAND - Request a user initiated heartbeat
5835 * to be made immediately.
5836 *
5837 * SPP_PMTUD_ENABLE - This field will enable PMTU
5838 * discovery upon the specified address. Note that
5839 * if the address feild is empty then all addresses
5840 * on the association are effected.
5841 *
5842 * SPP_PMTUD_DISABLE - This field will disable PMTU
5843 * discovery upon the specified address. Note that
5844 * if the address feild is empty then all addresses
5845 * on the association are effected. Not also that
5846 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5847 * exclusive. Enabling both will have undetermined
5848 * results.
5849 *
5850 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5851 * on delayed sack. The time specified in spp_sackdelay
5852 * is used to specify the sack delay for this address. Note
5853 * that if spp_address is empty then all addresses will
5854 * enable delayed sack and take on the sack delay
5855 * value specified in spp_sackdelay.
5856 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5857 * off delayed sack. If the spp_address field is blank then
5858 * delayed sack is disabled for the entire association. Note
5859 * also that this field is mutually exclusive to
5860 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5861 * results.
Xin Long0b0dce72018-07-02 18:21:13 +08005862 *
5863 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
5864 * setting of the IPV6 flow label value. The value is
5865 * contained in the spp_ipv6_flowlabel field.
5866 * Upon retrieval, this flag will be set to indicate that
5867 * the spp_ipv6_flowlabel field has a valid value returned.
5868 * If a specific destination address is set (in the
5869 * spp_address field), then the value returned is that of
5870 * the address. If just an association is specified (and
5871 * no address), then the association's default flow label
5872 * is returned. If neither an association nor a destination
5873 * is specified, then the socket's default flow label is
5874 * returned. For non-IPv6 sockets, this flag will be left
5875 * cleared.
5876 *
5877 * SPP_DSCP: Setting this flag enables the setting of the
5878 * Differentiated Services Code Point (DSCP) value
5879 * associated with either the association or a specific
5880 * address. The value is obtained in the spp_dscp field.
5881 * Upon retrieval, this flag will be set to indicate that
5882 * the spp_dscp field has a valid value returned. If a
5883 * specific destination address is set when called (in the
5884 * spp_address field), then that specific destination
5885 * address's DSCP value is returned. If just an association
5886 * is specified, then the association's default DSCP is
5887 * returned. If neither an association nor a destination is
5888 * specified, then the socket's default DSCP is returned.
5889 *
5890 * spp_ipv6_flowlabel
5891 * - This field is used in conjunction with the
5892 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
5893 * The 20 least significant bits are used for the flow
5894 * label. This setting has precedence over any IPv6-layer
5895 * setting.
5896 *
5897 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
5898 * and contains the DSCP. The 6 most significant bits are
5899 * used for the DSCP. This setting has precedence over any
5900 * IPv4- or IPv6- layer setting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 */
5902static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005903 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904{
Frank Filz52ccb8e2005-12-22 11:36:46 -08005905 struct sctp_paddrparams params;
5906 struct sctp_transport *trans = NULL;
5907 struct sctp_association *asoc = NULL;
5908 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909
Xin Long0b0dce72018-07-02 18:21:13 +08005910 if (len >= sizeof(params))
5911 len = sizeof(params);
5912 else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
5913 spp_ipv6_flowlabel), 4))
5914 len = ALIGN(offsetof(struct sctp_paddrparams,
5915 spp_ipv6_flowlabel), 4);
5916 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 return -EINVAL;
Xin Long0b0dce72018-07-02 18:21:13 +08005918
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 if (copy_from_user(&params, optval, len))
5920 return -EFAULT;
5921
Frank Filz52ccb8e2005-12-22 11:36:46 -08005922 /* If an address other than INADDR_ANY is specified, and
5923 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 */
wangweidongcb3f8372013-12-23 12:16:50 +08005925 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005926 trans = sctp_addr_id2transport(sk, &params.spp_address,
5927 params.spp_assoc_id);
5928 if (!trans) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005929 pr_debug("%s: failed no transport\n", __func__);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005930 return -EINVAL;
5931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 }
5933
Xin Longb99e5e02019-01-28 15:08:24 +08005934 /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
5935 * socket is a one to many style socket, and an association
5936 * was not found, then the id was invalid.
Frank Filz52ccb8e2005-12-22 11:36:46 -08005937 */
5938 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
Xin Longb99e5e02019-01-28 15:08:24 +08005939 if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
5940 sctp_style(sk, UDP)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005941 pr_debug("%s: failed no association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08005943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944
Frank Filz52ccb8e2005-12-22 11:36:46 -08005945 if (trans) {
5946 /* Fetch transport values. */
5947 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5948 params.spp_pathmtu = trans->pathmtu;
5949 params.spp_pathmaxrxt = trans->pathmaxrxt;
5950 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
Frank Filz52ccb8e2005-12-22 11:36:46 -08005952 /*draft-11 doesn't say what to return in spp_flags*/
5953 params.spp_flags = trans->param_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08005954 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5955 params.spp_ipv6_flowlabel = trans->flowlabel &
5956 SCTP_FLOWLABEL_VAL_MASK;
5957 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5958 }
5959 if (trans->dscp & SCTP_DSCP_SET_MASK) {
5960 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
5961 params.spp_flags |= SPP_DSCP;
5962 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08005963 } else if (asoc) {
5964 /* Fetch association values. */
5965 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5966 params.spp_pathmtu = asoc->pathmtu;
5967 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5968 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969
Frank Filz52ccb8e2005-12-22 11:36:46 -08005970 /*draft-11 doesn't say what to return in spp_flags*/
5971 params.spp_flags = asoc->param_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08005972 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5973 params.spp_ipv6_flowlabel = asoc->flowlabel &
5974 SCTP_FLOWLABEL_VAL_MASK;
5975 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5976 }
5977 if (asoc->dscp & SCTP_DSCP_SET_MASK) {
5978 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
5979 params.spp_flags |= SPP_DSCP;
5980 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08005981 } else {
5982 /* Fetch socket values. */
5983 params.spp_hbinterval = sp->hbinterval;
5984 params.spp_pathmtu = sp->pathmtu;
5985 params.spp_sackdelay = sp->sackdelay;
5986 params.spp_pathmaxrxt = sp->pathmaxrxt;
5987
5988 /*draft-11 doesn't say what to return in spp_flags*/
5989 params.spp_flags = sp->param_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08005990 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5991 params.spp_ipv6_flowlabel = sp->flowlabel &
5992 SCTP_FLOWLABEL_VAL_MASK;
5993 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5994 }
5995 if (sp->dscp & SCTP_DSCP_SET_MASK) {
5996 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
5997 params.spp_flags |= SPP_DSCP;
5998 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08005999 }
6000
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 if (copy_to_user(optval, &params, len))
6002 return -EFAULT;
6003
6004 if (put_user(len, optlen))
6005 return -EFAULT;
6006
6007 return 0;
6008}
6009
Wei Yongjund364d922008-05-09 15:13:26 -07006010/*
6011 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08006012 *
Wei Yongjund364d922008-05-09 15:13:26 -07006013 * This option will effect the way delayed acks are performed. This
6014 * option allows you to get or set the delayed ack time, in
6015 * milliseconds. It also allows changing the delayed ack frequency.
6016 * Changing the frequency to 1 disables the delayed sack algorithm. If
6017 * the assoc_id is 0, then this sets or gets the endpoints default
6018 * values. If the assoc_id field is non-zero, then the set or get
6019 * effects the specified association for the one to many model (the
6020 * assoc_id field is ignored by the one to one model). Note that if
6021 * sack_delay or sack_freq are 0 when setting this option, then the
6022 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08006023 *
Wei Yongjund364d922008-05-09 15:13:26 -07006024 * struct sctp_sack_info {
6025 * sctp_assoc_t sack_assoc_id;
6026 * uint32_t sack_delay;
6027 * uint32_t sack_freq;
6028 * };
Frank Filz77086102005-12-22 11:37:30 -08006029 *
Wei Yongjund364d922008-05-09 15:13:26 -07006030 * sack_assoc_id - This parameter, indicates which association the user
6031 * is performing an action upon. Note that if this field's value is
6032 * zero then the endpoints default value is changed (effecting future
6033 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08006034 *
Wei Yongjund364d922008-05-09 15:13:26 -07006035 * sack_delay - This parameter contains the number of milliseconds that
6036 * the user is requesting the delayed ACK timer be set to. Note that
6037 * this value is defined in the standard to be between 200 and 500
6038 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08006039 *
Wei Yongjund364d922008-05-09 15:13:26 -07006040 * sack_freq - This parameter contains the number of packets that must
6041 * be received before a sack is sent without waiting for the delay
6042 * timer to expire. The default value for this is 2, setting this
6043 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08006044 */
Wei Yongjund364d922008-05-09 15:13:26 -07006045static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08006046 char __user *optval,
6047 int __user *optlen)
6048{
Wei Yongjund364d922008-05-09 15:13:26 -07006049 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08006050 struct sctp_association *asoc = NULL;
6051 struct sctp_sock *sp = sctp_sk(sk);
6052
Wei Yongjund364d922008-05-09 15:13:26 -07006053 if (len >= sizeof(struct sctp_sack_info)) {
6054 len = sizeof(struct sctp_sack_info);
6055
6056 if (copy_from_user(&params, optval, len))
6057 return -EFAULT;
6058 } else if (len == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05006059 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006060 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006061 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006062 "Use struct sctp_sack_info instead\n",
6063 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07006064 if (copy_from_user(&params, optval, len))
6065 return -EFAULT;
6066 } else
wangweidongcb3f8372013-12-23 12:16:50 +08006067 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08006068
Xin Long9c5829e2019-01-28 15:08:34 +08006069 /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
6070 * socket is a one to many style socket, and an association
6071 * was not found, then the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006072 */
Wei Yongjund364d922008-05-09 15:13:26 -07006073 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
Xin Long9c5829e2019-01-28 15:08:34 +08006074 if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC &&
6075 sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08006076 return -EINVAL;
6077
6078 if (asoc) {
6079 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07006080 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
Xin Long9c5829e2019-01-28 15:08:34 +08006081 params.sack_delay = jiffies_to_msecs(asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07006082 params.sack_freq = asoc->sackfreq;
6083
6084 } else {
6085 params.sack_delay = 0;
6086 params.sack_freq = 1;
6087 }
Frank Filz77086102005-12-22 11:37:30 -08006088 } else {
6089 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07006090 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
6091 params.sack_delay = sp->sackdelay;
6092 params.sack_freq = sp->sackfreq;
6093 } else {
6094 params.sack_delay = 0;
6095 params.sack_freq = 1;
6096 }
Frank Filz77086102005-12-22 11:37:30 -08006097 }
6098
6099 if (copy_to_user(optval, &params, len))
6100 return -EFAULT;
6101
6102 if (put_user(len, optlen))
6103 return -EFAULT;
6104
6105 return 0;
6106}
6107
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
6109 *
6110 * Applications can specify protocol parameters for the default association
6111 * initialization. The option name argument to setsockopt() and getsockopt()
6112 * is SCTP_INITMSG.
6113 *
6114 * Setting initialization parameters is effective only on an unconnected
6115 * socket (for UDP-style sockets only future associations are effected
6116 * by the change). With TCP-style sockets, this option is inherited by
6117 * sockets derived from a listener socket.
6118 */
6119static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
6120{
Neil Horman408f22e2007-06-16 14:03:45 -04006121 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04006123 len = sizeof(struct sctp_initmsg);
6124 if (put_user(len, optlen))
6125 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
6127 return -EFAULT;
6128 return 0;
6129}
6130
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006132static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
6133 char __user *optval, int __user *optlen)
6134{
6135 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006136 int cnt = 0;
6137 struct sctp_getaddrs getaddrs;
6138 struct sctp_transport *from;
6139 void __user *to;
6140 union sctp_addr temp;
6141 struct sctp_sock *sp = sctp_sk(sk);
6142 int addrlen;
6143 size_t space_left;
6144 int bytes_copied;
6145
6146 if (len < sizeof(struct sctp_getaddrs))
6147 return -EINVAL;
6148
6149 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6150 return -EFAULT;
6151
6152 /* For UDP-style sockets, id specifies the association to query. */
6153 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6154 if (!asoc)
6155 return -EINVAL;
6156
wangweidongcb3f8372013-12-23 12:16:50 +08006157 to = optval + offsetof(struct sctp_getaddrs, addrs);
6158 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006159
Robert P. J. Day9dbc15f02008-04-12 18:54:24 -07006160 list_for_each_entry(from, &asoc->peer.transport_addr_list,
6161 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08006162 memcpy(&temp, &from->ipaddr, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06006163 addrlen = sctp_get_pf_specific(sk->sk_family)
6164 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006165 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006166 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006167 if (copy_to_user(to, &temp, addrlen))
6168 return -EFAULT;
6169 to += addrlen;
6170 cnt++;
6171 space_left -= addrlen;
6172 }
6173
6174 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
6175 return -EFAULT;
6176 bytes_copied = ((char __user *)to) - optval;
6177 if (put_user(bytes_copied, optlen))
6178 return -EFAULT;
6179
6180 return 0;
6181}
6182
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006183static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
6184 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006185{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006186 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006187 union sctp_addr temp;
6188 int cnt = 0;
6189 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00006190 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006191
Vlad Yasevich29303542007-09-16 16:02:12 -07006192 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00006193 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07006194 if (!addr->valid)
6195 continue;
6196
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006197 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08006198 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006199 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07006200 if ((PF_INET6 == sk->sk_family) &&
6201 inet_v6_ipv6only(sk) &&
6202 (AF_INET == addr->a.sa.sa_family))
6203 continue;
Al Viro6244be42006-11-20 17:21:44 -08006204 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05006205 if (!temp.v4.sin_port)
6206 temp.v4.sin_port = htons(port);
6207
Jason Gunthorpe299ee122014-07-30 12:40:53 -06006208 addrlen = sctp_get_pf_specific(sk->sk_family)
6209 ->addr_to_user(sctp_sk(sk), &temp);
6210
Vlad Yasevich29303542007-09-16 16:02:12 -07006211 if (space_left < addrlen) {
6212 cnt = -ENOMEM;
6213 break;
6214 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006215 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08006216
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006217 to += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08006218 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006219 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04006220 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006221 }
Vlad Yasevich29303542007-09-16 16:02:12 -07006222 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006223
6224 return cnt;
6225}
6226
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006228static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
6229 char __user *optval, int __user *optlen)
6230{
6231 struct sctp_bind_addr *bp;
6232 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006233 int cnt = 0;
6234 struct sctp_getaddrs getaddrs;
6235 struct sctp_sockaddr_entry *addr;
6236 void __user *to;
6237 union sctp_addr temp;
6238 struct sctp_sock *sp = sctp_sk(sk);
6239 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006240 int err = 0;
6241 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006242 int bytes_copied = 0;
6243 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07006244 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006245
Neil Horman408f22e2007-06-16 14:03:45 -04006246 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006247 return -EINVAL;
6248
6249 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6250 return -EFAULT;
6251
6252 /*
6253 * For UDP-style sockets, id specifies the association to query.
6254 * If the id field is set to the value '0' then the locally bound
6255 * addresses are returned without regard to any particular
6256 * association.
6257 */
6258 if (0 == getaddrs.assoc_id) {
6259 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006260 } else {
6261 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6262 if (!asoc)
6263 return -EINVAL;
6264 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006265 }
6266
wangweidongcb3f8372013-12-23 12:16:50 +08006267 to = optval + offsetof(struct sctp_getaddrs, addrs);
6268 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Neil Horman186e2342007-06-18 19:59:16 -04006269
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02006270 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006271 if (!addrs)
6272 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006273
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006274 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
6275 * addresses from the global local address list.
6276 */
6277 if (sctp_list_single_entry(&bp->address_list)) {
6278 addr = list_entry(bp->address_list.next,
6279 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04006280 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006281 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
6282 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006283 if (cnt < 0) {
6284 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07006285 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006286 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006287 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006288 }
6289 }
6290
Vlad Yasevich70b57b82007-05-09 13:51:31 -07006291 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07006292 /* Protection on the bound address list is not needed since
6293 * in the socket option context we hold a socket lock and
6294 * thus the bound address list can't change.
6295 */
6296 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08006297 memcpy(&temp, &addr->a, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06006298 addrlen = sctp_get_pf_specific(sk->sk_family)
6299 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006300 if (space_left < addrlen) {
6301 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07006302 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006303 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07006304 memcpy(buf, &temp, addrlen);
6305 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006306 bytes_copied += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08006307 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006308 space_left -= addrlen;
6309 }
6310
6311copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006312 if (copy_to_user(to, addrs, bytes_copied)) {
6313 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02006314 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006315 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04006316 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
6317 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02006318 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04006319 }
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006320 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
6321 * but we can't change it anymore.
6322 */
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006323 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04006324 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02006325out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006326 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006327 return err;
6328}
6329
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
6331 *
6332 * Requests that the local SCTP stack use the enclosed peer address as
6333 * the association primary. The enclosed address must be one of the
6334 * association peer's addresses.
6335 */
6336static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
6337 char __user *optval, int __user *optlen)
6338{
6339 struct sctp_prim prim;
6340 struct sctp_association *asoc;
6341 struct sctp_sock *sp = sctp_sk(sk);
6342
Neil Horman408f22e2007-06-16 14:03:45 -04006343 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 return -EINVAL;
6345
Neil Horman408f22e2007-06-16 14:03:45 -04006346 len = sizeof(struct sctp_prim);
6347
6348 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 return -EFAULT;
6350
6351 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
6352 if (!asoc)
6353 return -EINVAL;
6354
6355 if (!asoc->peer.primary_path)
6356 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006357
Al Viro8cec6b82006-11-20 17:23:01 -08006358 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
6359 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360
Jason Gunthorpe299ee122014-07-30 12:40:53 -06006361 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 (union sctp_addr *)&prim.ssp_addr);
6363
Neil Horman408f22e2007-06-16 14:03:45 -04006364 if (put_user(len, optlen))
6365 return -EFAULT;
6366 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 return -EFAULT;
6368
6369 return 0;
6370}
6371
6372/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006373 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006375 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 * Indication parameter for all future INIT and INIT-ACK exchanges.
6377 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006378static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 char __user *optval, int __user *optlen)
6380{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006381 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
Neil Horman408f22e2007-06-16 14:03:45 -04006383 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 return -EINVAL;
6385
Neil Horman408f22e2007-06-16 14:03:45 -04006386 len = sizeof(struct sctp_setadaptation);
6387
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006388 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04006389
6390 if (put_user(len, optlen))
6391 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006392 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07006394
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 return 0;
6396}
6397
6398/*
6399 *
6400 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
6401 *
6402 * Applications that wish to use the sendto() system call may wish to
6403 * specify a default set of parameters that would normally be supplied
6404 * through the inclusion of ancillary data. This socket option allows
6405 * such an application to set the default sctp_sndrcvinfo structure.
6406
6407
6408 * The application that wishes to use this socket option simply passes
6409 * in to this call the sctp_sndrcvinfo structure defined in Section
6410 * 5.2.2) The input parameters accepted by this call include
6411 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
6412 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
6413 * to this call if the caller is using the UDP model.
6414 *
6415 * For getsockopt, it get the default sctp_sndrcvinfo structure.
6416 */
6417static int sctp_getsockopt_default_send_param(struct sock *sk,
6418 int len, char __user *optval,
6419 int __user *optlen)
6420{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006422 struct sctp_association *asoc;
6423 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006425 if (len < sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04006427
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006428 len = sizeof(info);
Neil Horman408f22e2007-06-16 14:03:45 -04006429
6430 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431 return -EFAULT;
6432
6433 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
Xin Long707e45b2019-01-28 15:08:35 +08006434 if (!asoc && info.sinfo_assoc_id != SCTP_FUTURE_ASSOC &&
6435 sctp_style(sk, UDP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 return -EINVAL;
Xin Long707e45b2019-01-28 15:08:35 +08006437
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 if (asoc) {
6439 info.sinfo_stream = asoc->default_stream;
6440 info.sinfo_flags = asoc->default_flags;
6441 info.sinfo_ppid = asoc->default_ppid;
6442 info.sinfo_context = asoc->default_context;
6443 info.sinfo_timetolive = asoc->default_timetolive;
6444 } else {
6445 info.sinfo_stream = sp->default_stream;
6446 info.sinfo_flags = sp->default_flags;
6447 info.sinfo_ppid = sp->default_ppid;
6448 info.sinfo_context = sp->default_context;
6449 info.sinfo_timetolive = sp->default_timetolive;
6450 }
6451
Neil Horman408f22e2007-06-16 14:03:45 -04006452 if (put_user(len, optlen))
6453 return -EFAULT;
6454 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 return -EFAULT;
6456
6457 return 0;
6458}
6459
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006460/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
6461 * (SCTP_DEFAULT_SNDINFO)
6462 */
6463static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
6464 char __user *optval,
6465 int __user *optlen)
6466{
6467 struct sctp_sock *sp = sctp_sk(sk);
6468 struct sctp_association *asoc;
6469 struct sctp_sndinfo info;
6470
6471 if (len < sizeof(info))
6472 return -EINVAL;
6473
6474 len = sizeof(info);
6475
6476 if (copy_from_user(&info, optval, len))
6477 return -EFAULT;
6478
6479 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
Xin Long92fc3bd2019-01-28 15:08:36 +08006480 if (!asoc && info.snd_assoc_id != SCTP_FUTURE_ASSOC &&
6481 sctp_style(sk, UDP))
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006482 return -EINVAL;
Xin Long92fc3bd2019-01-28 15:08:36 +08006483
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006484 if (asoc) {
6485 info.snd_sid = asoc->default_stream;
6486 info.snd_flags = asoc->default_flags;
6487 info.snd_ppid = asoc->default_ppid;
6488 info.snd_context = asoc->default_context;
6489 } else {
6490 info.snd_sid = sp->default_stream;
6491 info.snd_flags = sp->default_flags;
6492 info.snd_ppid = sp->default_ppid;
6493 info.snd_context = sp->default_context;
6494 }
6495
6496 if (put_user(len, optlen))
6497 return -EFAULT;
6498 if (copy_to_user(optval, &info, len))
6499 return -EFAULT;
6500
6501 return 0;
6502}
6503
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504/*
6505 *
6506 * 7.1.5 SCTP_NODELAY
6507 *
6508 * Turn on/off any Nagle-like algorithm. This means that packets are
6509 * generally sent as soon as possible and no unnecessary delays are
6510 * introduced, at the cost of more packets in the network. Expects an
6511 * integer boolean flag.
6512 */
6513
6514static int sctp_getsockopt_nodelay(struct sock *sk, int len,
6515 char __user *optval, int __user *optlen)
6516{
6517 int val;
6518
6519 if (len < sizeof(int))
6520 return -EINVAL;
6521
6522 len = sizeof(int);
6523 val = (sctp_sk(sk)->nodelay == 1);
6524 if (put_user(len, optlen))
6525 return -EFAULT;
6526 if (copy_to_user(optval, &val, len))
6527 return -EFAULT;
6528 return 0;
6529}
6530
6531/*
6532 *
6533 * 7.1.1 SCTP_RTOINFO
6534 *
6535 * The protocol parameters used to initialize and bound retransmission
6536 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
6537 * and modify these parameters.
6538 * All parameters are time values, in milliseconds. A value of 0, when
6539 * modifying the parameters, indicates that the current value should not
6540 * be changed.
6541 *
6542 */
6543static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6544 char __user *optval,
6545 int __user *optlen) {
6546 struct sctp_rtoinfo rtoinfo;
6547 struct sctp_association *asoc;
6548
Neil Horman408f22e2007-06-16 14:03:45 -04006549 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 return -EINVAL;
6551
Neil Horman408f22e2007-06-16 14:03:45 -04006552 len = sizeof(struct sctp_rtoinfo);
6553
6554 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 return -EFAULT;
6556
6557 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6558
Xin Long7adb5ed2019-01-28 15:08:25 +08006559 if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
6560 sctp_style(sk, UDP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 return -EINVAL;
6562
6563 /* Values corresponding to the specific association. */
6564 if (asoc) {
6565 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6566 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6567 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6568 } else {
6569 /* Values corresponding to the endpoint. */
6570 struct sctp_sock *sp = sctp_sk(sk);
6571
6572 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6573 rtoinfo.srto_max = sp->rtoinfo.srto_max;
6574 rtoinfo.srto_min = sp->rtoinfo.srto_min;
6575 }
6576
6577 if (put_user(len, optlen))
6578 return -EFAULT;
6579
6580 if (copy_to_user(optval, &rtoinfo, len))
6581 return -EFAULT;
6582
6583 return 0;
6584}
6585
6586/*
6587 *
6588 * 7.1.2 SCTP_ASSOCINFO
6589 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02006590 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 * of the association.
6592 * Returns an error if the new association retransmission value is
6593 * greater than the sum of the retransmission value of the peer.
6594 * See [SCTP] for more information.
6595 *
6596 */
6597static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6598 char __user *optval,
6599 int __user *optlen)
6600{
6601
6602 struct sctp_assocparams assocparams;
6603 struct sctp_association *asoc;
6604 struct list_head *pos;
6605 int cnt = 0;
6606
Neil Horman408f22e2007-06-16 14:03:45 -04006607 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 return -EINVAL;
6609
Neil Horman408f22e2007-06-16 14:03:45 -04006610 len = sizeof(struct sctp_assocparams);
6611
6612 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 return -EFAULT;
6614
6615 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6616
Xin Long88893942019-01-28 15:08:26 +08006617 if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
6618 sctp_style(sk, UDP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 return -EINVAL;
6620
6621 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07006622 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6624 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6625 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
Daniel Borkmann52db8822013-06-25 18:17:27 +02006626 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627
6628 list_for_each(pos, &asoc->peer.transport_addr_list) {
wangweidongcb3f8372013-12-23 12:16:50 +08006629 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 }
6631
6632 assocparams.sasoc_number_peer_destinations = cnt;
6633 } else {
6634 /* Values corresponding to the endpoint */
6635 struct sctp_sock *sp = sctp_sk(sk);
6636
6637 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6638 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6639 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6640 assocparams.sasoc_cookie_life =
6641 sp->assocparams.sasoc_cookie_life;
6642 assocparams.sasoc_number_peer_destinations =
6643 sp->assocparams.
6644 sasoc_number_peer_destinations;
6645 }
6646
6647 if (put_user(len, optlen))
6648 return -EFAULT;
6649
6650 if (copy_to_user(optval, &assocparams, len))
6651 return -EFAULT;
6652
6653 return 0;
6654}
6655
6656/*
6657 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6658 *
6659 * This socket option is a boolean flag which turns on or off mapped V4
6660 * addresses. If this option is turned on and the socket is type
6661 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6662 * If this option is turned off, then no mapping will be done of V4
6663 * addresses and a user will receive both PF_INET6 and PF_INET type
6664 * addresses on the socket.
6665 */
6666static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6667 char __user *optval, int __user *optlen)
6668{
6669 int val;
6670 struct sctp_sock *sp = sctp_sk(sk);
6671
6672 if (len < sizeof(int))
6673 return -EINVAL;
6674
6675 len = sizeof(int);
6676 val = sp->v4mapped;
6677 if (put_user(len, optlen))
6678 return -EFAULT;
6679 if (copy_to_user(optval, &val, len))
6680 return -EFAULT;
6681
6682 return 0;
6683}
6684
6685/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006686 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6687 * (chapter and verse is quoted at sctp_setsockopt_context())
6688 */
6689static int sctp_getsockopt_context(struct sock *sk, int len,
6690 char __user *optval, int __user *optlen)
6691{
6692 struct sctp_assoc_value params;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006693 struct sctp_association *asoc;
6694
Neil Horman408f22e2007-06-16 14:03:45 -04006695 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006696 return -EINVAL;
6697
Neil Horman408f22e2007-06-16 14:03:45 -04006698 len = sizeof(struct sctp_assoc_value);
6699
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006700 if (copy_from_user(&params, optval, len))
6701 return -EFAULT;
6702
Xin Long49b037a2019-01-28 15:08:37 +08006703 asoc = sctp_id2assoc(sk, params.assoc_id);
6704 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6705 sctp_style(sk, UDP))
6706 return -EINVAL;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006707
Xin Long49b037a2019-01-28 15:08:37 +08006708 params.assoc_value = asoc ? asoc->default_rcv_context
6709 : sctp_sk(sk)->default_rcv_context;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006710
6711 if (put_user(len, optlen))
6712 return -EFAULT;
6713 if (copy_to_user(optval, &params, len))
6714 return -EFAULT;
6715
6716 return 0;
6717}
6718
6719/*
Wei Yongjune89c2092008-12-25 16:54:58 -08006720 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6721 * This option will get or set the maximum size to put in any outgoing
6722 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 * fragmented by SCTP into the specified size. Note that the underlying
6724 * SCTP implementation may fragment into smaller sized chunks when the
6725 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08006726 * the user. The default value for this option is '0' which indicates
6727 * the user is NOT limiting fragmentation and only the PMTU will effect
6728 * SCTP's choice of DATA chunk size. Note also that values set larger
6729 * than the maximum size of an IP datagram will effectively let SCTP
6730 * control fragmentation (i.e. the same as setting this option to 0).
6731 *
6732 * The following structure is used to access and modify this parameter:
6733 *
6734 * struct sctp_assoc_value {
6735 * sctp_assoc_t assoc_id;
6736 * uint32_t assoc_value;
6737 * };
6738 *
6739 * assoc_id: This parameter is ignored for one-to-one style sockets.
6740 * For one-to-many style sockets this parameter indicates which
6741 * association the user is performing an action upon. Note that if
6742 * this field's value is zero then the endpoints default value is
6743 * changed (effecting future associations only).
6744 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745 */
6746static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6747 char __user *optval, int __user *optlen)
6748{
Wei Yongjune89c2092008-12-25 16:54:58 -08006749 struct sctp_assoc_value params;
6750 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751
Wei Yongjune89c2092008-12-25 16:54:58 -08006752 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006753 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006754 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006755 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006756 "Use struct sctp_assoc_value instead\n",
6757 current->comm, task_pid_nr(current));
Xin Long6fd769b2019-01-28 15:08:27 +08006758 params.assoc_id = SCTP_FUTURE_ASSOC;
Wei Yongjune89c2092008-12-25 16:54:58 -08006759 } else if (len >= sizeof(struct sctp_assoc_value)) {
6760 len = sizeof(struct sctp_assoc_value);
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006761 if (copy_from_user(&params, optval, len))
Wei Yongjune89c2092008-12-25 16:54:58 -08006762 return -EFAULT;
6763 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 return -EINVAL;
6765
Wei Yongjune89c2092008-12-25 16:54:58 -08006766 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Long6fd769b2019-01-28 15:08:27 +08006767 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6768 sctp_style(sk, UDP))
Wei Yongjune89c2092008-12-25 16:54:58 -08006769 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770
Wei Yongjune89c2092008-12-25 16:54:58 -08006771 if (asoc)
6772 params.assoc_value = asoc->frag_point;
6773 else
6774 params.assoc_value = sctp_sk(sk)->user_frag;
6775
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 if (put_user(len, optlen))
6777 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08006778 if (len == sizeof(int)) {
6779 if (copy_to_user(optval, &params.assoc_value, len))
6780 return -EFAULT;
6781 } else {
6782 if (copy_to_user(optval, &params, len))
6783 return -EFAULT;
6784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785
6786 return 0;
6787}
6788
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006789/*
6790 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6791 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6792 */
6793static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6794 char __user *optval, int __user *optlen)
6795{
6796 int val;
6797
6798 if (len < sizeof(int))
6799 return -EINVAL;
6800
6801 len = sizeof(int);
6802
6803 val = sctp_sk(sk)->frag_interleave;
6804 if (put_user(len, optlen))
6805 return -EFAULT;
6806 if (copy_to_user(optval, &val, len))
6807 return -EFAULT;
6808
6809 return 0;
6810}
6811
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006812/*
6813 * 7.1.25. Set or Get the sctp partial delivery point
6814 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6815 */
6816static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6817 char __user *optval,
6818 int __user *optlen)
6819{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09006820 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006821
6822 if (len < sizeof(u32))
6823 return -EINVAL;
6824
6825 len = sizeof(u32);
6826
6827 val = sctp_sk(sk)->pd_point;
6828 if (put_user(len, optlen))
6829 return -EFAULT;
6830 if (copy_to_user(optval, &val, len))
6831 return -EFAULT;
6832
Wei Yongjun7d743b72010-12-14 16:10:41 +00006833 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006834}
6835
Vlad Yasevich70331572007-03-23 11:34:36 -07006836/*
6837 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6838 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6839 */
6840static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6841 char __user *optval,
6842 int __user *optlen)
6843{
Neil Horman219b99a2008-03-05 13:44:46 -08006844 struct sctp_assoc_value params;
Neil Horman219b99a2008-03-05 13:44:46 -08006845 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07006846
Neil Horman219b99a2008-03-05 13:44:46 -08006847 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006848 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006849 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006850 "Use of int in max_burst socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006851 "Use struct sctp_assoc_value instead\n",
6852 current->comm, task_pid_nr(current));
Xin Longe0651a02019-01-28 15:08:38 +08006853 params.assoc_id = SCTP_FUTURE_ASSOC;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00006854 } else if (len >= sizeof(struct sctp_assoc_value)) {
6855 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08006856 if (copy_from_user(&params, optval, len))
6857 return -EFAULT;
6858 } else
6859 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07006860
Xin Longe0651a02019-01-28 15:08:38 +08006861 asoc = sctp_id2assoc(sk, params.assoc_id);
6862 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6863 sctp_style(sk, UDP))
6864 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07006865
Xin Longe0651a02019-01-28 15:08:38 +08006866 params.assoc_value = asoc ? asoc->max_burst : sctp_sk(sk)->max_burst;
Neil Horman219b99a2008-03-05 13:44:46 -08006867
6868 if (len == sizeof(int)) {
6869 if (copy_to_user(optval, &params.assoc_value, len))
6870 return -EFAULT;
6871 } else {
6872 if (copy_to_user(optval, &params, len))
6873 return -EFAULT;
6874 }
6875
6876 return 0;
6877
Vlad Yasevich70331572007-03-23 11:34:36 -07006878}
6879
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006880static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6881 char __user *optval, int __user *optlen)
6882{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006883 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006884 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006885 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006886 __u16 data_len = 0;
6887 u32 num_idents;
Xin Long7a84bd42016-02-03 23:33:30 +08006888 int i;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006889
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006890 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006891 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006892
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006893 hmacs = ep->auth_hmacs_list;
Xin Long3c918702017-06-30 11:52:16 +08006894 data_len = ntohs(hmacs->param_hdr.length) -
6895 sizeof(struct sctp_paramhdr);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006896
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006897 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006898 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006899
6900 len = sizeof(struct sctp_hmacalgo) + data_len;
6901 num_idents = data_len / sizeof(u16);
6902
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006903 if (put_user(len, optlen))
6904 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006905 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006906 return -EFAULT;
Xin Long7a84bd42016-02-03 23:33:30 +08006907 for (i = 0; i < num_idents; i++) {
6908 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6909
6910 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6911 return -EFAULT;
6912 }
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006913 return 0;
6914}
6915
6916static int sctp_getsockopt_active_key(struct sock *sk, int len,
6917 char __user *optval, int __user *optlen)
6918{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006919 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006920 struct sctp_authkeyid val;
6921 struct sctp_association *asoc;
6922
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006923 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006924 return -EACCES;
6925
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006926 if (len < sizeof(struct sctp_authkeyid))
6927 return -EINVAL;
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006928
6929 len = sizeof(struct sctp_authkeyid);
6930 if (copy_from_user(&val, optval, len))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006931 return -EFAULT;
6932
6933 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6934 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6935 return -EINVAL;
6936
6937 if (asoc)
6938 val.scact_keynumber = asoc->active_key_id;
6939 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006940 val.scact_keynumber = ep->active_key_id;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006941
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006942 if (put_user(len, optlen))
6943 return -EFAULT;
6944 if (copy_to_user(optval, &val, len))
6945 return -EFAULT;
6946
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006947 return 0;
6948}
6949
6950static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6951 char __user *optval, int __user *optlen)
6952{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006953 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006954 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006955 struct sctp_authchunks val;
6956 struct sctp_association *asoc;
6957 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006958 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006959 char __user *to;
6960
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006961 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006962 return -EACCES;
6963
6964 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006965 return -EINVAL;
6966
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006967 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006968 return -EFAULT;
6969
Al Viro411223c2007-10-14 19:21:20 +01006970 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006971 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6972 if (!asoc)
6973 return -EINVAL;
6974
6975 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006976 if (!ch)
6977 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006978
6979 /* See if the user provided enough room for all the data */
Xin Long3c918702017-06-30 11:52:16 +08006980 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevichb40db682008-02-27 14:40:37 -05006981 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006982 return -EINVAL;
6983
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006984 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006985 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006986num:
6987 len = sizeof(struct sctp_authchunks) + num_chunks;
wangweidong8d726512013-12-23 12:16:53 +08006988 if (put_user(len, optlen))
6989 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006990 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6991 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006992 return 0;
6993}
6994
6995static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6996 char __user *optval, int __user *optlen)
6997{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006998 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006999 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007000 struct sctp_authchunks val;
7001 struct sctp_association *asoc;
7002 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07007003 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007004 char __user *to;
7005
Vlad Yasevichb14878c2014-04-17 17:26:50 +02007006 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07007007 return -EACCES;
7008
7009 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007010 return -EINVAL;
7011
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02007012 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007013 return -EFAULT;
7014
Al Viro411223c2007-10-14 19:21:20 +01007015 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007016 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
Xin Long48c07212019-01-28 15:08:28 +08007017 if (!asoc && val.gauth_assoc_id != SCTP_FUTURE_ASSOC &&
7018 sctp_style(sk, UDP))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007019 return -EINVAL;
7020
Xin Long48c07212019-01-28 15:08:28 +08007021 ch = asoc ? (struct sctp_chunks_param *)asoc->c.auth_chunks
7022 : ep->auth_chunk_list;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07007023 if (!ch)
7024 goto num;
7025
Xin Long3c918702017-06-30 11:52:16 +08007026 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07007027 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007028 return -EINVAL;
7029
Vlad Yasevich5e739d12008-08-21 03:34:25 -07007030 if (copy_to_user(to, ch->chunks, num_chunks))
7031 return -EFAULT;
7032num:
7033 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007034 if (put_user(len, optlen))
7035 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05007036 if (put_user(num_chunks, &p->gauth_number_of_chunks))
7037 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007038
7039 return 0;
7040}
7041
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08007042/*
7043 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
7044 * This option gets the current number of associations that are attached
7045 * to a one-to-many style socket. The option value is an uint32_t.
7046 */
7047static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
7048 char __user *optval, int __user *optlen)
7049{
7050 struct sctp_sock *sp = sctp_sk(sk);
7051 struct sctp_association *asoc;
7052 u32 val = 0;
7053
7054 if (sctp_style(sk, TCP))
7055 return -EOPNOTSUPP;
7056
7057 if (len < sizeof(u32))
7058 return -EINVAL;
7059
7060 len = sizeof(u32);
7061
7062 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7063 val++;
7064 }
7065
7066 if (put_user(len, optlen))
7067 return -EFAULT;
7068 if (copy_to_user(optval, &val, len))
7069 return -EFAULT;
7070
7071 return 0;
7072}
7073
Wei Yongjun209ba422011-04-17 17:27:08 +00007074/*
Michio Honda7dc04d72011-04-26 20:16:31 +09007075 * 8.1.23 SCTP_AUTO_ASCONF
7076 * See the corresponding setsockopt entry as description
7077 */
7078static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
7079 char __user *optval, int __user *optlen)
7080{
7081 int val = 0;
7082
7083 if (len < sizeof(int))
7084 return -EINVAL;
7085
7086 len = sizeof(int);
7087 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
7088 val = 1;
7089 if (put_user(len, optlen))
7090 return -EFAULT;
7091 if (copy_to_user(optval, &val, len))
7092 return -EFAULT;
7093 return 0;
7094}
7095
7096/*
Wei Yongjun209ba422011-04-17 17:27:08 +00007097 * 8.2.6. Get the Current Identifiers of Associations
7098 * (SCTP_GET_ASSOC_ID_LIST)
7099 *
7100 * This option gets the current list of SCTP association identifiers of
7101 * the SCTP associations handled by a one-to-many style socket.
7102 */
7103static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
7104 char __user *optval, int __user *optlen)
7105{
7106 struct sctp_sock *sp = sctp_sk(sk);
7107 struct sctp_association *asoc;
7108 struct sctp_assoc_ids *ids;
7109 u32 num = 0;
7110
7111 if (sctp_style(sk, TCP))
7112 return -EOPNOTSUPP;
7113
7114 if (len < sizeof(struct sctp_assoc_ids))
7115 return -EINVAL;
7116
7117 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7118 num++;
7119 }
7120
7121 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
7122 return -EINVAL;
7123
7124 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
7125
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02007126 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
Wei Yongjun209ba422011-04-17 17:27:08 +00007127 if (unlikely(!ids))
7128 return -ENOMEM;
7129
7130 ids->gaids_number_of_ids = num;
7131 num = 0;
7132 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7133 ids->gaids_assoc_id[num++] = asoc->assoc_id;
7134 }
7135
7136 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
7137 kfree(ids);
7138 return -EFAULT;
7139 }
7140
7141 kfree(ids);
7142 return 0;
7143}
7144
Neil Horman5aa93bc2012-07-21 07:56:07 +00007145/*
7146 * SCTP_PEER_ADDR_THLDS
7147 *
7148 * This option allows us to fetch the partially failed threshold for one or all
7149 * transports in an association. See Section 6.1 of:
7150 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
7151 */
7152static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
7153 char __user *optval,
7154 int len,
7155 int __user *optlen)
7156{
7157 struct sctp_paddrthlds val;
7158 struct sctp_transport *trans;
7159 struct sctp_association *asoc;
7160
7161 if (len < sizeof(struct sctp_paddrthlds))
7162 return -EINVAL;
7163 len = sizeof(struct sctp_paddrthlds);
7164 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
7165 return -EFAULT;
7166
Xin Long8add5432019-01-28 15:08:29 +08007167 if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
Neil Horman5aa93bc2012-07-21 07:56:07 +00007168 trans = sctp_addr_id2transport(sk, &val.spt_address,
7169 val.spt_assoc_id);
7170 if (!trans)
7171 return -ENOENT;
7172
7173 val.spt_pathmaxrxt = trans->pathmaxrxt;
7174 val.spt_pathpfthld = trans->pf_retrans;
Xin Long8add5432019-01-28 15:08:29 +08007175
7176 return 0;
7177 }
7178
7179 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
7180 if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
7181 sctp_style(sk, UDP))
7182 return -EINVAL;
7183
7184 if (asoc) {
7185 val.spt_pathpfthld = asoc->pf_retrans;
7186 val.spt_pathmaxrxt = asoc->pathmaxrxt;
7187 } else {
7188 struct sctp_sock *sp = sctp_sk(sk);
7189
7190 val.spt_pathpfthld = sp->pf_retrans;
7191 val.spt_pathmaxrxt = sp->pathmaxrxt;
Neil Horman5aa93bc2012-07-21 07:56:07 +00007192 }
7193
7194 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
7195 return -EFAULT;
7196
7197 return 0;
7198}
7199
Michele Baldessari196d6752012-12-01 04:49:42 +00007200/*
7201 * SCTP_GET_ASSOC_STATS
7202 *
7203 * This option retrieves local per endpoint statistics. It is modeled
7204 * after OpenSolaris' implementation
7205 */
7206static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
7207 char __user *optval,
7208 int __user *optlen)
7209{
7210 struct sctp_assoc_stats sas;
7211 struct sctp_association *asoc = NULL;
7212
7213 /* User must provide at least the assoc id */
7214 if (len < sizeof(sctp_assoc_t))
7215 return -EINVAL;
7216
Guenter Roeck726bc6b2013-02-27 10:57:31 +00007217 /* Allow the struct to grow and fill in as much as possible */
7218 len = min_t(size_t, len, sizeof(sas));
7219
Michele Baldessari196d6752012-12-01 04:49:42 +00007220 if (copy_from_user(&sas, optval, len))
7221 return -EFAULT;
7222
7223 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
7224 if (!asoc)
7225 return -EINVAL;
7226
7227 sas.sas_rtxchunks = asoc->stats.rtxchunks;
7228 sas.sas_gapcnt = asoc->stats.gapcnt;
7229 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
7230 sas.sas_osacks = asoc->stats.osacks;
7231 sas.sas_isacks = asoc->stats.isacks;
7232 sas.sas_octrlchunks = asoc->stats.octrlchunks;
7233 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
7234 sas.sas_oodchunks = asoc->stats.oodchunks;
7235 sas.sas_iodchunks = asoc->stats.iodchunks;
7236 sas.sas_ouodchunks = asoc->stats.ouodchunks;
7237 sas.sas_iuodchunks = asoc->stats.iuodchunks;
7238 sas.sas_idupchunks = asoc->stats.idupchunks;
7239 sas.sas_opackets = asoc->stats.opackets;
7240 sas.sas_ipackets = asoc->stats.ipackets;
7241
7242 /* New high max rto observed, will return 0 if not a single
7243 * RTO update took place. obs_rto_ipaddr will be bogus
7244 * in such a case
7245 */
7246 sas.sas_maxrto = asoc->stats.max_obs_rto;
7247 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
7248 sizeof(struct sockaddr_storage));
7249
7250 /* Mark beginning of a new observation period */
7251 asoc->stats.max_obs_rto = asoc->rto_min;
7252
Michele Baldessari196d6752012-12-01 04:49:42 +00007253 if (put_user(len, optlen))
7254 return -EFAULT;
7255
Daniel Borkmannbb333812013-06-28 19:49:40 +02007256 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
Michele Baldessari196d6752012-12-01 04:49:42 +00007257
7258 if (copy_to_user(optval, &sas, len))
7259 return -EFAULT;
7260
7261 return 0;
7262}
7263
Geir Ola Vaagland0d3a421d2014-07-12 20:30:37 +02007264static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
7265 char __user *optval,
7266 int __user *optlen)
7267{
7268 int val = 0;
7269
7270 if (len < sizeof(int))
7271 return -EINVAL;
7272
7273 len = sizeof(int);
7274 if (sctp_sk(sk)->recvrcvinfo)
7275 val = 1;
7276 if (put_user(len, optlen))
7277 return -EFAULT;
7278 if (copy_to_user(optval, &val, len))
7279 return -EFAULT;
7280
7281 return 0;
7282}
7283
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007284static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
7285 char __user *optval,
7286 int __user *optlen)
7287{
7288 int val = 0;
7289
7290 if (len < sizeof(int))
7291 return -EINVAL;
7292
7293 len = sizeof(int);
7294 if (sctp_sk(sk)->recvnxtinfo)
7295 val = 1;
7296 if (put_user(len, optlen))
7297 return -EFAULT;
7298 if (copy_to_user(optval, &val, len))
7299 return -EFAULT;
7300
7301 return 0;
7302}
7303
Xin Long28aa4c22016-07-09 19:47:40 +08007304static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
7305 char __user *optval,
7306 int __user *optlen)
7307{
7308 struct sctp_assoc_value params;
7309 struct sctp_association *asoc;
7310 int retval = -EFAULT;
7311
7312 if (len < sizeof(params)) {
7313 retval = -EINVAL;
7314 goto out;
7315 }
7316
7317 len = sizeof(params);
7318 if (copy_from_user(&params, optval, len))
7319 goto out;
7320
7321 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Longfb195602019-01-28 15:08:30 +08007322 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7323 sctp_style(sk, UDP)) {
Xin Long28aa4c22016-07-09 19:47:40 +08007324 retval = -EINVAL;
7325 goto out;
7326 }
7327
Xin Long1c134752019-07-09 00:57:05 +08007328 params.assoc_value = asoc ? asoc->peer.prsctp_capable
Xin Longfb195602019-01-28 15:08:30 +08007329 : sctp_sk(sk)->ep->prsctp_enable;
7330
Xin Long28aa4c22016-07-09 19:47:40 +08007331 if (put_user(len, optlen))
7332 goto out;
7333
7334 if (copy_to_user(optval, &params, len))
7335 goto out;
7336
7337 retval = 0;
7338
7339out:
7340 return retval;
7341}
7342
Xin Longf959fb42016-07-09 19:47:41 +08007343static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
7344 char __user *optval,
7345 int __user *optlen)
7346{
7347 struct sctp_default_prinfo info;
7348 struct sctp_association *asoc;
7349 int retval = -EFAULT;
7350
7351 if (len < sizeof(info)) {
7352 retval = -EINVAL;
7353 goto out;
7354 }
7355
7356 len = sizeof(info);
7357 if (copy_from_user(&info, optval, len))
7358 goto out;
7359
7360 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
Xin Long3a583052019-01-28 15:08:43 +08007361 if (!asoc && info.pr_assoc_id != SCTP_FUTURE_ASSOC &&
7362 sctp_style(sk, UDP)) {
7363 retval = -EINVAL;
7364 goto out;
7365 }
7366
Xin Longf959fb42016-07-09 19:47:41 +08007367 if (asoc) {
7368 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
7369 info.pr_value = asoc->default_timetolive;
Xin Long3a583052019-01-28 15:08:43 +08007370 } else {
Xin Longf959fb42016-07-09 19:47:41 +08007371 struct sctp_sock *sp = sctp_sk(sk);
7372
7373 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
7374 info.pr_value = sp->default_timetolive;
Xin Longf959fb42016-07-09 19:47:41 +08007375 }
7376
7377 if (put_user(len, optlen))
7378 goto out;
7379
7380 if (copy_to_user(optval, &info, len))
7381 goto out;
7382
7383 retval = 0;
7384
7385out:
7386 return retval;
7387}
7388
Xin Long826d2532016-07-09 19:47:42 +08007389static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
7390 char __user *optval,
7391 int __user *optlen)
7392{
7393 struct sctp_prstatus params;
7394 struct sctp_association *asoc;
7395 int policy;
7396 int retval = -EINVAL;
7397
7398 if (len < sizeof(params))
7399 goto out;
7400
7401 len = sizeof(params);
7402 if (copy_from_user(&params, optval, len)) {
7403 retval = -EFAULT;
7404 goto out;
7405 }
7406
7407 policy = params.sprstat_policy;
Xin Long71335832018-10-29 23:13:11 +08007408 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7409 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
Xin Long826d2532016-07-09 19:47:42 +08007410 goto out;
7411
7412 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7413 if (!asoc)
7414 goto out;
7415
Xin Long71335832018-10-29 23:13:11 +08007416 if (policy == SCTP_PR_SCTP_ALL) {
Xin Long826d2532016-07-09 19:47:42 +08007417 params.sprstat_abandoned_unsent = 0;
7418 params.sprstat_abandoned_sent = 0;
7419 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7420 params.sprstat_abandoned_unsent +=
7421 asoc->abandoned_unsent[policy];
7422 params.sprstat_abandoned_sent +=
7423 asoc->abandoned_sent[policy];
7424 }
7425 } else {
7426 params.sprstat_abandoned_unsent =
7427 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7428 params.sprstat_abandoned_sent =
7429 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
7430 }
7431
7432 if (put_user(len, optlen)) {
7433 retval = -EFAULT;
7434 goto out;
7435 }
7436
7437 if (copy_to_user(optval, &params, len)) {
7438 retval = -EFAULT;
7439 goto out;
7440 }
7441
7442 retval = 0;
7443
7444out:
7445 return retval;
7446}
7447
Xin Longd229d482017-04-01 17:07:46 +08007448static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
7449 char __user *optval,
7450 int __user *optlen)
7451{
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007452 struct sctp_stream_out_ext *streamoute;
Xin Longd229d482017-04-01 17:07:46 +08007453 struct sctp_association *asoc;
7454 struct sctp_prstatus params;
7455 int retval = -EINVAL;
7456 int policy;
7457
7458 if (len < sizeof(params))
7459 goto out;
7460
7461 len = sizeof(params);
7462 if (copy_from_user(&params, optval, len)) {
7463 retval = -EFAULT;
7464 goto out;
7465 }
7466
7467 policy = params.sprstat_policy;
Xin Long71335832018-10-29 23:13:11 +08007468 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7469 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
Xin Longd229d482017-04-01 17:07:46 +08007470 goto out;
7471
7472 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
Xin Longcee360a2017-05-31 16:36:31 +08007473 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
Xin Longd229d482017-04-01 17:07:46 +08007474 goto out;
7475
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03007476 streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007477 if (!streamoute) {
7478 /* Not allocated yet, means all stats are 0 */
7479 params.sprstat_abandoned_unsent = 0;
7480 params.sprstat_abandoned_sent = 0;
7481 retval = 0;
7482 goto out;
7483 }
7484
Xin Long0ac10772018-10-16 15:52:02 +08007485 if (policy == SCTP_PR_SCTP_ALL) {
Xin Longd229d482017-04-01 17:07:46 +08007486 params.sprstat_abandoned_unsent = 0;
7487 params.sprstat_abandoned_sent = 0;
7488 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7489 params.sprstat_abandoned_unsent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007490 streamoute->abandoned_unsent[policy];
Xin Longd229d482017-04-01 17:07:46 +08007491 params.sprstat_abandoned_sent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007492 streamoute->abandoned_sent[policy];
Xin Longd229d482017-04-01 17:07:46 +08007493 }
7494 } else {
7495 params.sprstat_abandoned_unsent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007496 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08007497 params.sprstat_abandoned_sent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007498 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08007499 }
7500
7501 if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
7502 retval = -EFAULT;
7503 goto out;
7504 }
7505
7506 retval = 0;
7507
7508out:
7509 return retval;
7510}
7511
Xin Longc0d8bab2017-03-10 12:11:12 +08007512static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
7513 char __user *optval,
7514 int __user *optlen)
7515{
7516 struct sctp_assoc_value params;
7517 struct sctp_association *asoc;
7518 int retval = -EFAULT;
7519
7520 if (len < sizeof(params)) {
7521 retval = -EINVAL;
7522 goto out;
7523 }
7524
7525 len = sizeof(params);
7526 if (copy_from_user(&params, optval, len))
7527 goto out;
7528
7529 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Longacce7f32019-01-28 15:08:31 +08007530 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7531 sctp_style(sk, UDP)) {
Xin Longc0d8bab2017-03-10 12:11:12 +08007532 retval = -EINVAL;
7533 goto out;
7534 }
7535
Xin Longa96701f2019-07-09 00:57:04 +08007536 params.assoc_value = asoc ? asoc->peer.reconf_capable
Xin Longacce7f32019-01-28 15:08:31 +08007537 : sctp_sk(sk)->ep->reconf_enable;
7538
Xin Longc0d8bab2017-03-10 12:11:12 +08007539 if (put_user(len, optlen))
7540 goto out;
7541
7542 if (copy_to_user(optval, &params, len))
7543 goto out;
7544
7545 retval = 0;
7546
7547out:
7548 return retval;
7549}
7550
Xin Long9fb657a2017-01-18 00:44:46 +08007551static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7552 char __user *optval,
7553 int __user *optlen)
7554{
7555 struct sctp_assoc_value params;
7556 struct sctp_association *asoc;
7557 int retval = -EFAULT;
7558
7559 if (len < sizeof(params)) {
7560 retval = -EINVAL;
7561 goto out;
7562 }
7563
7564 len = sizeof(params);
7565 if (copy_from_user(&params, optval, len))
7566 goto out;
7567
7568 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Long99a62132019-01-28 15:08:44 +08007569 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7570 sctp_style(sk, UDP)) {
Xin Long9fb657a2017-01-18 00:44:46 +08007571 retval = -EINVAL;
7572 goto out;
7573 }
7574
Xin Long99a62132019-01-28 15:08:44 +08007575 params.assoc_value = asoc ? asoc->strreset_enable
7576 : sctp_sk(sk)->ep->strreset_enable;
7577
Xin Long9fb657a2017-01-18 00:44:46 +08007578 if (put_user(len, optlen))
7579 goto out;
7580
7581 if (copy_to_user(optval, &params, len))
7582 goto out;
7583
7584 retval = 0;
7585
7586out:
7587 return retval;
7588}
7589
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007590static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7591 char __user *optval,
7592 int __user *optlen)
7593{
7594 struct sctp_assoc_value params;
7595 struct sctp_association *asoc;
7596 int retval = -EFAULT;
7597
7598 if (len < sizeof(params)) {
7599 retval = -EINVAL;
7600 goto out;
7601 }
7602
7603 len = sizeof(params);
7604 if (copy_from_user(&params, optval, len))
7605 goto out;
7606
7607 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Long7efba102019-01-28 15:08:46 +08007608 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7609 sctp_style(sk, UDP)) {
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007610 retval = -EINVAL;
7611 goto out;
7612 }
7613
Xin Long7efba102019-01-28 15:08:46 +08007614 params.assoc_value = asoc ? sctp_sched_get_sched(asoc)
7615 : sctp_sk(sk)->default_ss;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007616
7617 if (put_user(len, optlen))
7618 goto out;
7619
7620 if (copy_to_user(optval, &params, len))
7621 goto out;
7622
7623 retval = 0;
7624
7625out:
7626 return retval;
7627}
7628
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007629static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7630 char __user *optval,
7631 int __user *optlen)
7632{
7633 struct sctp_stream_value params;
7634 struct sctp_association *asoc;
7635 int retval = -EFAULT;
7636
7637 if (len < sizeof(params)) {
7638 retval = -EINVAL;
7639 goto out;
7640 }
7641
7642 len = sizeof(params);
7643 if (copy_from_user(&params, optval, len))
7644 goto out;
7645
7646 asoc = sctp_id2assoc(sk, params.assoc_id);
7647 if (!asoc) {
7648 retval = -EINVAL;
7649 goto out;
7650 }
7651
7652 retval = sctp_sched_get_value(asoc, params.stream_id,
7653 &params.stream_value);
7654 if (retval)
7655 goto out;
7656
7657 if (put_user(len, optlen)) {
7658 retval = -EFAULT;
7659 goto out;
7660 }
7661
7662 if (copy_to_user(optval, &params, len)) {
7663 retval = -EFAULT;
7664 goto out;
7665 }
7666
7667out:
7668 return retval;
7669}
7670
Xin Long772a5862017-12-08 21:03:58 +08007671static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7672 char __user *optval,
7673 int __user *optlen)
7674{
7675 struct sctp_assoc_value params;
7676 struct sctp_association *asoc;
7677 int retval = -EFAULT;
7678
7679 if (len < sizeof(params)) {
7680 retval = -EINVAL;
7681 goto out;
7682 }
7683
7684 len = sizeof(params);
7685 if (copy_from_user(&params, optval, len))
7686 goto out;
7687
7688 asoc = sctp_id2assoc(sk, params.assoc_id);
Xin Long2e7709d2019-01-28 15:08:32 +08007689 if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7690 sctp_style(sk, UDP)) {
Xin Long772a5862017-12-08 21:03:58 +08007691 retval = -EINVAL;
7692 goto out;
7693 }
7694
Xin Longda1f6d42019-07-09 00:57:06 +08007695 params.assoc_value = asoc ? asoc->peer.intl_capable
Xin Longe55f4b82019-07-09 00:57:07 +08007696 : sctp_sk(sk)->ep->intl_enable;
Xin Long2e7709d2019-01-28 15:08:32 +08007697
Xin Long772a5862017-12-08 21:03:58 +08007698 if (put_user(len, optlen))
7699 goto out;
7700
7701 if (copy_to_user(optval, &params, len))
7702 goto out;
7703
7704 retval = 0;
7705
7706out:
7707 return retval;
7708}
7709
Xin Longb0e9a2f2018-06-28 15:31:00 +08007710static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
7711 char __user *optval,
7712 int __user *optlen)
7713{
7714 int val;
7715
7716 if (len < sizeof(int))
7717 return -EINVAL;
7718
7719 len = sizeof(int);
7720 val = sctp_sk(sk)->reuse;
7721 if (put_user(len, optlen))
7722 return -EFAULT;
7723
7724 if (copy_to_user(optval, &val, len))
7725 return -EFAULT;
7726
7727 return 0;
7728}
7729
Xin Long480ba9c2018-11-18 16:08:54 +08007730static int sctp_getsockopt_event(struct sock *sk, int len, char __user *optval,
7731 int __user *optlen)
7732{
7733 struct sctp_association *asoc;
7734 struct sctp_event param;
7735 __u16 subscribe;
7736
7737 if (len < sizeof(param))
7738 return -EINVAL;
7739
7740 len = sizeof(param);
7741 if (copy_from_user(&param, optval, len))
7742 return -EFAULT;
7743
7744 if (param.se_type < SCTP_SN_TYPE_BASE ||
7745 param.se_type > SCTP_SN_TYPE_MAX)
7746 return -EINVAL;
7747
7748 asoc = sctp_id2assoc(sk, param.se_assoc_id);
Xin Longd251f052019-01-28 15:08:45 +08007749 if (!asoc && param.se_assoc_id != SCTP_FUTURE_ASSOC &&
7750 sctp_style(sk, UDP))
7751 return -EINVAL;
7752
Xin Long480ba9c2018-11-18 16:08:54 +08007753 subscribe = asoc ? asoc->subscribe : sctp_sk(sk)->subscribe;
7754 param.se_on = sctp_ulpevent_type_enabled(subscribe, param.se_type);
7755
7756 if (put_user(len, optlen))
7757 return -EFAULT;
7758
7759 if (copy_to_user(optval, &param, len))
7760 return -EFAULT;
7761
7762 return 0;
7763}
7764
Daniel Borkmanndda91922013-06-17 11:40:05 +02007765static int sctp_getsockopt(struct sock *sk, int level, int optname,
7766 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767{
7768 int retval = 0;
7769 int len;
7770
Daniel Borkmannbb333812013-06-28 19:49:40 +02007771 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772
7773 /* I can hardly begin to describe how wrong this is. This is
7774 * so broken as to be worse than useless. The API draft
7775 * REALLY is NOT helpful here... I am not convinced that the
7776 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7777 * are at all well-founded.
7778 */
7779 if (level != SOL_SCTP) {
7780 struct sctp_af *af = sctp_sk(sk)->pf->af;
7781
7782 retval = af->getsockopt(sk, level, optname, optval, optlen);
7783 return retval;
7784 }
7785
7786 if (get_user(len, optlen))
7787 return -EFAULT;
7788
Jiri Slabya4b8e712016-10-21 14:13:24 +02007789 if (len < 0)
7790 return -EINVAL;
7791
wangweidong048ed4b2014-01-21 15:44:11 +08007792 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793
7794 switch (optname) {
7795 case SCTP_STATUS:
7796 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7797 break;
7798 case SCTP_DISABLE_FRAGMENTS:
7799 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7800 optlen);
7801 break;
7802 case SCTP_EVENTS:
7803 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7804 break;
7805 case SCTP_AUTOCLOSE:
7806 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7807 break;
7808 case SCTP_SOCKOPT_PEELOFF:
7809 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7810 break;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04007811 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7812 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7813 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007814 case SCTP_PEER_ADDR_PARAMS:
7815 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7816 optlen);
7817 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00007818 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07007819 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08007820 optlen);
7821 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822 case SCTP_INITMSG:
7823 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7824 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825 case SCTP_GET_PEER_ADDRS:
7826 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7827 optlen);
7828 break;
7829 case SCTP_GET_LOCAL_ADDRS:
7830 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7831 optlen);
7832 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04007833 case SCTP_SOCKOPT_CONNECTX3:
7834 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7835 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836 case SCTP_DEFAULT_SEND_PARAM:
7837 retval = sctp_getsockopt_default_send_param(sk, len,
7838 optval, optlen);
7839 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02007840 case SCTP_DEFAULT_SNDINFO:
7841 retval = sctp_getsockopt_default_sndinfo(sk, len,
7842 optval, optlen);
7843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844 case SCTP_PRIMARY_ADDR:
7845 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7846 break;
7847 case SCTP_NODELAY:
7848 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7849 break;
7850 case SCTP_RTOINFO:
7851 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7852 break;
7853 case SCTP_ASSOCINFO:
7854 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7855 break;
7856 case SCTP_I_WANT_MAPPED_V4_ADDR:
7857 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7858 break;
7859 case SCTP_MAXSEG:
7860 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7861 break;
7862 case SCTP_GET_PEER_ADDR_INFO:
7863 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7864 optlen);
7865 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08007866 case SCTP_ADAPTATION_LAYER:
7867 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868 optlen);
7869 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08007870 case SCTP_CONTEXT:
7871 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7872 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007873 case SCTP_FRAGMENT_INTERLEAVE:
7874 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7875 optlen);
7876 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07007877 case SCTP_PARTIAL_DELIVERY_POINT:
7878 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7879 optlen);
7880 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07007881 case SCTP_MAX_BURST:
7882 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7883 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007884 case SCTP_AUTH_KEY:
7885 case SCTP_AUTH_CHUNK:
7886 case SCTP_AUTH_DELETE_KEY:
Xin Long601590e2018-03-14 19:05:32 +08007887 case SCTP_AUTH_DEACTIVATE_KEY:
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007888 retval = -EOPNOTSUPP;
7889 break;
7890 case SCTP_HMAC_IDENT:
7891 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7892 break;
7893 case SCTP_AUTH_ACTIVE_KEY:
7894 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7895 break;
7896 case SCTP_PEER_AUTH_CHUNKS:
7897 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7898 optlen);
7899 break;
7900 case SCTP_LOCAL_AUTH_CHUNKS:
7901 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7902 optlen);
7903 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08007904 case SCTP_GET_ASSOC_NUMBER:
7905 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7906 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00007907 case SCTP_GET_ASSOC_ID_LIST:
7908 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7909 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09007910 case SCTP_AUTO_ASCONF:
7911 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7912 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00007913 case SCTP_PEER_ADDR_THLDS:
7914 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7915 break;
Michele Baldessari196d6752012-12-01 04:49:42 +00007916 case SCTP_GET_ASSOC_STATS:
7917 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7918 break;
Geir Ola Vaagland0d3a421d2014-07-12 20:30:37 +02007919 case SCTP_RECVRCVINFO:
7920 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7921 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007922 case SCTP_RECVNXTINFO:
7923 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7924 break;
Xin Long28aa4c22016-07-09 19:47:40 +08007925 case SCTP_PR_SUPPORTED:
7926 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7927 break;
Xin Longf959fb42016-07-09 19:47:41 +08007928 case SCTP_DEFAULT_PRINFO:
7929 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7930 optlen);
7931 break;
Xin Long826d2532016-07-09 19:47:42 +08007932 case SCTP_PR_ASSOC_STATUS:
7933 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7934 optlen);
7935 break;
Xin Longd229d482017-04-01 17:07:46 +08007936 case SCTP_PR_STREAM_STATUS:
7937 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7938 optlen);
7939 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08007940 case SCTP_RECONFIG_SUPPORTED:
7941 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7942 optlen);
7943 break;
Xin Long9fb657a2017-01-18 00:44:46 +08007944 case SCTP_ENABLE_STREAM_RESET:
7945 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7946 optlen);
7947 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007948 case SCTP_STREAM_SCHEDULER:
7949 retval = sctp_getsockopt_scheduler(sk, len, optval,
7950 optlen);
7951 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007952 case SCTP_STREAM_SCHEDULER_VALUE:
7953 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7954 optlen);
7955 break;
Xin Long772a5862017-12-08 21:03:58 +08007956 case SCTP_INTERLEAVING_SUPPORTED:
7957 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7958 optlen);
7959 break;
Xin Longb0e9a2f2018-06-28 15:31:00 +08007960 case SCTP_REUSE_PORT:
7961 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
7962 break;
Xin Long480ba9c2018-11-18 16:08:54 +08007963 case SCTP_EVENT:
7964 retval = sctp_getsockopt_event(sk, len, optval, optlen);
7965 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966 default:
7967 retval = -ENOPROTOOPT;
7968 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007970
wangweidong048ed4b2014-01-21 15:44:11 +08007971 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972 return retval;
7973}
7974
Craig Gallek086c6532016-02-10 11:50:35 -05007975static int sctp_hash(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976{
7977 /* STUB */
Craig Gallek086c6532016-02-10 11:50:35 -05007978 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979}
7980
7981static void sctp_unhash(struct sock *sk)
7982{
7983 /* STUB */
7984}
7985
7986/* Check if port is acceptable. Possibly find first available port.
7987 *
7988 * The port hash table (contained in the 'global' SCTP protocol storage
7989 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7990 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7991 * list (the list number is the port number hashed out, so as you
7992 * would expect from a hash function, all the ports in a given list have
7993 * such a number that hashes out to the same list number; you were
7994 * expecting that, right?); so each list has a set of ports, with a
7995 * link to the socket (struct sock) that uses it, the port number and
7996 * a fastreuse flag (FIXME: NPI ipg).
7997 */
7998static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007999 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008000
8001static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
8002{
Xin Long6ba84572018-11-12 18:27:17 +08008003 struct sctp_sock *sp = sctp_sk(sk);
8004 bool reuse = (sk->sk_reuse || sp->reuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005 struct sctp_bind_hashbucket *head; /* hash list */
Xin Long6ba84572018-11-12 18:27:17 +08008006 kuid_t uid = sock_i_uid(sk);
Sasha Levinb67bfe02013-02-27 17:06:00 -08008007 struct sctp_bind_bucket *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008008 unsigned short snum;
8009 int ret;
8010
Al Viro04afd8b2006-11-20 17:02:01 -08008011 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012
Daniel Borkmannbb333812013-06-28 19:49:40 +02008013 pr_debug("%s: begins, snum:%d\n", __func__, snum);
8014
wangweidong79b91132014-01-21 15:44:07 +08008015 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008016
8017 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07008018 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07008019 int low, high, remaining, index;
8020 unsigned int rover;
WANG Cong122ff242014-05-12 16:04:53 -07008021 struct net *net = sock_net(sk);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07008022
WANG Cong122ff242014-05-12 16:04:53 -07008023 inet_get_local_port_range(net, &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07008024 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05008025 rover = prandom_u32() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008026
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027 do {
8028 rover++;
8029 if ((rover < low) || (rover > high))
8030 rover = low;
WANG Cong122ff242014-05-12 16:04:53 -07008031 if (inet_is_local_reserved_port(net, rover))
Amerigo Wange3826f12010-05-05 00:27:06 +00008032 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008033 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034 head = &sctp_port_hashtable[index];
wangweidong3c8e43b2014-01-21 15:44:08 +08008035 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08008036 sctp_for_each_hentry(pp, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008037 if ((pp->port == rover) &&
8038 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008039 goto next;
8040 break;
8041 next:
wangweidong3c8e43b2014-01-21 15:44:08 +08008042 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008043 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044
8045 /* Exhausted local port range during search? */
8046 ret = 1;
8047 if (remaining <= 0)
8048 goto fail;
8049
8050 /* OK, here is the one we will use. HEAD (the port
8051 * hash table list entry) is non-NULL and we hold it's
8052 * mutex.
8053 */
8054 snum = rover;
8055 } else {
8056 /* We are given an specific port number; we verify
8057 * that it is not being used. If it is used, we will
8058 * exahust the search in the hash list corresponding
8059 * to the port number (snum) - we detect that with the
8060 * port iterator, pp being NULL.
8061 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008062 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
wangweidong3c8e43b2014-01-21 15:44:08 +08008063 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08008064 sctp_for_each_hentry(pp, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008065 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008066 goto pp_found;
8067 }
8068 }
8069 pp = NULL;
8070 goto pp_not_found;
8071pp_found:
8072 if (!hlist_empty(&pp->owner)) {
8073 /* We had a port hash table hit - there is an
8074 * available port (pp != NULL) and it is being
8075 * used by other socket (pp->owner not empty); that other
8076 * socket is going to be sk2.
8077 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008079
Daniel Borkmannbb333812013-06-28 19:49:40 +02008080 pr_debug("%s: found a possible match\n", __func__);
8081
Xin Long6ba84572018-11-12 18:27:17 +08008082 if ((pp->fastreuse && reuse &&
8083 sk->sk_state != SCTP_SS_LISTENING) ||
8084 (pp->fastreuseport && sk->sk_reuseport &&
8085 uid_eq(pp->fastuid, uid)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008086 goto success;
8087
8088 /* Run through the list of sockets bound to the port
8089 * (pp->port) [via the pointers bind_next and
8090 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
8091 * we get the endpoint they describe and run through
8092 * the endpoint's list of IP (v4 or v6) addresses,
8093 * comparing each of the addresses with the address of
8094 * the socket sk. If we find a match, then that means
8095 * that this port/socket (sk) combination are already
8096 * in an endpoint.
8097 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08008098 sk_for_each_bound(sk2, &pp->owner) {
Xin Long6ba84572018-11-12 18:27:17 +08008099 struct sctp_sock *sp2 = sctp_sk(sk2);
8100 struct sctp_endpoint *ep2 = sp2->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008101
Vlad Yasevich4e540642008-07-18 23:06:32 -07008102 if (sk == sk2 ||
Xin Long6ba84572018-11-12 18:27:17 +08008103 (reuse && (sk2->sk_reuse || sp2->reuse) &&
8104 sk2->sk_state != SCTP_SS_LISTENING) ||
8105 (sk->sk_reuseport && sk2->sk_reuseport &&
8106 uid_eq(uid, sock_i_uid(sk2))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008107 continue;
8108
Xin Long6ba84572018-11-12 18:27:17 +08008109 if (sctp_bind_addr_conflict(&ep2->base.bind_addr,
8110 addr, sp2, sp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008111 ret = (long)sk2;
8112 goto fail_unlock;
8113 }
8114 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02008115
8116 pr_debug("%s: found a match\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117 }
8118pp_not_found:
8119 /* If there was a hash table miss, create a new port. */
8120 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008121 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008122 goto fail_unlock;
8123
8124 /* In either case (hit or miss), make sure fastreuse is 1 only
8125 * if sk->sk_reuse is too (that is, if the caller requested
8126 * SO_REUSEADDR on this socket -sk-).
8127 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07008128 if (hlist_empty(&pp->owner)) {
Xin Longb0e9a2f2018-06-28 15:31:00 +08008129 if (reuse && sk->sk_state != SCTP_SS_LISTENING)
Vlad Yasevichce5325c2007-05-04 13:34:49 -07008130 pp->fastreuse = 1;
8131 else
8132 pp->fastreuse = 0;
Xin Long6ba84572018-11-12 18:27:17 +08008133
8134 if (sk->sk_reuseport) {
8135 pp->fastreuseport = 1;
8136 pp->fastuid = uid;
8137 } else {
8138 pp->fastreuseport = 0;
8139 }
8140 } else {
8141 if (pp->fastreuse &&
8142 (!reuse || sk->sk_state == SCTP_SS_LISTENING))
8143 pp->fastreuse = 0;
8144
8145 if (pp->fastreuseport &&
8146 (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid)))
8147 pp->fastreuseport = 0;
8148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149
8150 /* We are set, so fill up all the data in the hash table
8151 * entry, tie the socket list information with the rest of the
8152 * sockets FIXME: Blurry, NPI (ipg).
8153 */
8154success:
Xin Long6ba84572018-11-12 18:27:17 +08008155 if (!sp->bind_hash) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008156 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008157 sk_add_bind_node(sk, &pp->owner);
Xin Long6ba84572018-11-12 18:27:17 +08008158 sp->bind_hash = pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008159 }
8160 ret = 0;
8161
8162fail_unlock:
wangweidong3c8e43b2014-01-21 15:44:08 +08008163 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008164
8165fail:
wangweidong79b91132014-01-21 15:44:07 +08008166 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008167 return ret;
8168}
8169
8170/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
8171 * port is requested.
8172 */
8173static int sctp_get_port(struct sock *sk, unsigned short snum)
8174{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175 union sctp_addr addr;
8176 struct sctp_af *af = sctp_sk(sk)->pf->af;
8177
8178 /* Set up a dummy address struct from the sk. */
8179 af->from_sk(&addr, sk);
8180 addr.v4.sin_port = htons(snum);
8181
8182 /* Note: sk->sk_num gets filled in if ephemeral port request. */
Daniel Borkmann62208f12013-06-25 18:17:30 +02008183 return !!sctp_get_port_local(sk, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008184}
8185
8186/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07008187 * Move a socket to LISTENING state.
8188 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02008189static int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008190{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008191 struct sctp_sock *sp = sctp_sk(sk);
8192 struct sctp_endpoint *ep = sp->ep;
Herbert Xu5821c762016-01-24 21:20:12 +08008193 struct crypto_shash *tfm = NULL;
Neil Horman3c681982012-10-24 09:20:03 +00008194 char alg[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195
8196 /* Allocate HMAC for generating cookie. */
Neil Horman3c681982012-10-24 09:20:03 +00008197 if (!sp->hmac && sp->sctp_hmac_alg) {
8198 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
Herbert Xu5821c762016-01-24 21:20:12 +08008199 tfm = crypto_alloc_shash(alg, 0, 0);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07008200 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00008201 net_info_ratelimited("failed to load transform for %s: %ld\n",
Neil Horman3c681982012-10-24 09:20:03 +00008202 sp->sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008203 return -ENOSYS;
8204 }
8205 sctp_sk(sk)->hmac = tfm;
8206 }
8207
8208 /*
8209 * If a bind() or sctp_bindx() is not called prior to a listen()
8210 * call that allows new associations to be accepted, the system
8211 * picks an ephemeral port and will choose an address set equivalent
8212 * to binding with a wildcard address.
8213 *
8214 * This is not currently spelled out in the SCTP sockets
8215 * extensions draft, but follows the practice as seen in TCP
8216 * sockets.
8217 *
8218 */
Yafang Shaocbabf462017-12-20 11:12:54 +08008219 inet_sk_set_state(sk, SCTP_SS_LISTENING);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008220 if (!ep->base.bind_addr.port) {
8221 if (sctp_autobind(sk))
8222 return -EAGAIN;
8223 } else {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008224 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08008225 inet_sk_set_state(sk, SCTP_SS_CLOSED);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008226 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227 }
8228 }
8229
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008230 sk->sk_max_ack_backlog = backlog;
Xin Long76c6d982018-11-12 18:27:16 +08008231 return sctp_hash_endpoint(ep);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008232}
8233
8234/*
8235 * 4.1.3 / 5.1.3 listen()
8236 *
8237 * By default, new associations are not accepted for UDP style sockets.
8238 * An application uses listen() to mark a socket as being able to
8239 * accept new associations.
8240 *
8241 * On TCP style sockets, applications use listen() to ready the SCTP
8242 * endpoint for accepting inbound associations.
8243 *
8244 * On both types of endpoints a backlog of '0' disables listening.
8245 *
8246 * Move a socket to LISTENING state.
8247 */
8248int sctp_inet_listen(struct socket *sock, int backlog)
8249{
8250 struct sock *sk = sock->sk;
8251 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
8252 int err = -EINVAL;
8253
8254 if (unlikely(backlog < 0))
8255 return err;
8256
wangweidong048ed4b2014-01-21 15:44:11 +08008257 lock_sock(sk);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008258
8259 /* Peeled-off sockets are not allowed to listen(). */
8260 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
8261 goto out;
8262
8263 if (sock->state != SS_UNCONNECTED)
8264 goto out;
8265
Xin Long34b27892017-04-06 13:10:52 +08008266 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
8267 goto out;
8268
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008269 /* If backlog is zero, disable listening. */
8270 if (!backlog) {
8271 if (sctp_sstate(sk, CLOSED))
8272 goto out;
8273
8274 err = 0;
8275 sctp_unhash_endpoint(ep);
8276 sk->sk_state = SCTP_SS_CLOSED;
Xin Longb0e9a2f2018-06-28 15:31:00 +08008277 if (sk->sk_reuse || sctp_sk(sk)->reuse)
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008278 sctp_sk(sk)->bind_hash->fastreuse = 1;
8279 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07008280 }
8281
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008282 /* If we are already listening, just update the backlog */
8283 if (sctp_sstate(sk, LISTENING))
8284 sk->sk_max_ack_backlog = backlog;
8285 else {
8286 err = sctp_listen_start(sk, backlog);
8287 if (err)
8288 goto out;
8289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008290
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00008291 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008292out:
wangweidong048ed4b2014-01-21 15:44:11 +08008293 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008295}
8296
8297/*
8298 * This function is done by modeling the current datagram_poll() and the
8299 * tcp_poll(). Note that, based on these implementations, we don't
8300 * lock the socket in this function, even though it seems that,
8301 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08008302 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07008303 * in this place. We assume that we don't need locks either until proven
8304 * otherwise.
8305 *
8306 * Another thing to note is that we include the Async I/O support
8307 * here, again, by modeling the current TCP/UDP code. We don't have
8308 * a good way to test with it yet.
8309 */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07008310__poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008311{
8312 struct sock *sk = sock->sk;
8313 struct sctp_sock *sp = sctp_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -04008314 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07008316 poll_wait(file, sk_sleep(sk), wait);
8317
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03008318 sock_rps_record_flow(sk);
8319
Linus Torvalds1da177e2005-04-16 15:20:36 -07008320 /* A TCP-style listening socket becomes readable when the accept queue
8321 * is not empty.
8322 */
8323 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
8324 return (!list_empty(&sp->ep->asocs)) ?
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008325 (EPOLLIN | EPOLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008326
8327 mask = 0;
8328
8329 /* Is there any exceptional events? */
8330 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008331 mask |= EPOLLERR |
8332 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
Davide Libenzif348d702006-03-25 03:07:39 -08008333 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008334 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008335 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008336 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008337
8338 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00008339 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008340 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341
8342 /* The association is either gone or not ready. */
8343 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
8344 return mask;
8345
8346 /* Is it writable? */
8347 if (sctp_writeable(sk)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008348 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008349 } else {
Eric Dumazet9cd3e072015-11-29 20:03:10 -08008350 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008351 /*
8352 * Since the socket is not locked, the buffer
8353 * might be made available after the writeable check and
8354 * before the bit is set. This could cause a lost I/O
8355 * signal. tcp_poll() has a race breaker for this race
8356 * condition. Based on their implementation, we put
8357 * in the following code to cover it as well.
8358 */
8359 if (sctp_writeable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008360 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008361 }
8362 return mask;
8363}
8364
8365/********************************************************************
8366 * 2nd Level Abstractions
8367 ********************************************************************/
8368
8369static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008370 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008371{
8372 struct sctp_bind_bucket *pp;
8373
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08008374 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008375 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07008376 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008377 pp->port = snum;
8378 pp->fastreuse = 0;
8379 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008380 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05008381 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382 }
8383 return pp;
8384}
8385
8386/* Caller must hold hashbucket lock for this tb with local BH disabled */
8387static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
8388{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07008389 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05008390 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008391 kmem_cache_free(sctp_bucket_cachep, pp);
8392 SCTP_DBG_OBJCNT_DEC(bind_bucket);
8393 }
8394}
8395
8396/* Release this socket's reference to a local port. */
8397static inline void __sctp_put_port(struct sock *sk)
8398{
8399 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008400 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
8401 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402 struct sctp_bind_bucket *pp;
8403
wangweidong3c8e43b2014-01-21 15:44:08 +08008404 spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405 pp = sctp_sk(sk)->bind_hash;
8406 __sk_del_bind_node(sk);
8407 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008408 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008409 sctp_bucket_destroy(pp);
wangweidong3c8e43b2014-01-21 15:44:08 +08008410 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008411}
8412
8413void sctp_put_port(struct sock *sk)
8414{
wangweidong79b91132014-01-21 15:44:07 +08008415 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008416 __sctp_put_port(sk);
wangweidong79b91132014-01-21 15:44:07 +08008417 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008418}
8419
8420/*
8421 * The system picks an ephemeral port and choose an address set equivalent
8422 * to binding with a wildcard address.
8423 * One of those addresses will be the primary address for the association.
8424 * This automatically enables the multihoming capability of SCTP.
8425 */
8426static int sctp_autobind(struct sock *sk)
8427{
8428 union sctp_addr autoaddr;
8429 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08008430 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008431
8432 /* Initialize a local sockaddr structure to INADDR_ANY. */
8433 af = sctp_sk(sk)->pf->af;
8434
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008435 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008436 af->inaddr_any(&autoaddr, port);
8437
8438 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
8439}
8440
8441/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
8442 *
8443 * From RFC 2292
8444 * 4.2 The cmsghdr Structure *
8445 *
8446 * When ancillary data is sent or received, any number of ancillary data
8447 * objects can be specified by the msg_control and msg_controllen members of
8448 * the msghdr structure, because each object is preceded by
8449 * a cmsghdr structure defining the object's length (the cmsg_len member).
8450 * Historically Berkeley-derived implementations have passed only one object
8451 * at a time, but this API allows multiple objects to be
8452 * passed in a single call to sendmsg() or recvmsg(). The following example
8453 * shows two ancillary data objects in a control buffer.
8454 *
8455 * |<--------------------------- msg_controllen -------------------------->|
8456 * | |
8457 *
8458 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
8459 *
8460 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
8461 * | | |
8462 *
8463 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
8464 *
8465 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
8466 * | | | | |
8467 *
8468 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8469 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
8470 *
8471 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
8472 *
8473 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8474 * ^
8475 * |
8476 *
8477 * msg_control
8478 * points here
8479 */
Xin Longa05437a2017-08-11 10:23:48 +08008480static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481{
Vlad Yasevichab38fb02008-04-12 18:40:06 -07008482 struct msghdr *my_msg = (struct msghdr *)msg;
Xin Longa05437a2017-08-11 10:23:48 +08008483 struct cmsghdr *cmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008484
Gu Zhengf95b4142014-12-11 11:22:04 +08008485 for_each_cmsghdr(cmsg, my_msg) {
Vlad Yasevichab38fb02008-04-12 18:40:06 -07008486 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008487 return -EINVAL;
8488
8489 /* Should we parse this header or ignore? */
8490 if (cmsg->cmsg_level != IPPROTO_SCTP)
8491 continue;
8492
8493 /* Strictly check lengths following example in SCM code. */
8494 switch (cmsg->cmsg_type) {
8495 case SCTP_INIT:
8496 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008497 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008498 *
8499 * This cmsghdr structure provides information for
8500 * initializing new SCTP associations with sendmsg().
8501 * The SCTP_INITMSG socket option uses this same data
8502 * structure. This structure is not used for
8503 * recvmsg().
8504 *
8505 * cmsg_level cmsg_type cmsg_data[]
8506 * ------------ ------------ ----------------------
8507 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
8508 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008509 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510 return -EINVAL;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008511
8512 cmsgs->init = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513 break;
8514
8515 case SCTP_SNDRCV:
8516 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008517 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008518 *
8519 * This cmsghdr structure specifies SCTP options for
8520 * sendmsg() and describes SCTP header information
8521 * about a received message through recvmsg().
8522 *
8523 * cmsg_level cmsg_type cmsg_data[]
8524 * ------------ ------------ ----------------------
8525 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
8526 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008527 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008528 return -EINVAL;
8529
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008530 cmsgs->srinfo = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008532 if (cmsgs->srinfo->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07008533 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Long49102802018-03-05 20:44:20 +08008534 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8535 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008536 return -EINVAL;
8537 break;
8538
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008539 case SCTP_SNDINFO:
8540 /* SCTP Socket API Extension
8541 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
8542 *
8543 * This cmsghdr structure specifies SCTP options for
8544 * sendmsg(). This structure and SCTP_RCVINFO replaces
8545 * SCTP_SNDRCV which has been deprecated.
8546 *
8547 * cmsg_level cmsg_type cmsg_data[]
8548 * ------------ ------------ ---------------------
8549 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
8550 */
8551 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
8552 return -EINVAL;
8553
8554 cmsgs->sinfo = CMSG_DATA(cmsg);
8555
8556 if (cmsgs->sinfo->snd_flags &
8557 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Long49102802018-03-05 20:44:20 +08008558 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8559 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008560 return -EINVAL;
8561 break;
Xin Longed63afb2018-03-05 20:44:18 +08008562 case SCTP_PRINFO:
8563 /* SCTP Socket API Extension
8564 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
8565 *
8566 * This cmsghdr structure specifies SCTP options for sendmsg().
8567 *
8568 * cmsg_level cmsg_type cmsg_data[]
8569 * ------------ ------------ ---------------------
8570 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
8571 */
8572 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
8573 return -EINVAL;
8574
8575 cmsgs->prinfo = CMSG_DATA(cmsg);
8576 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
8577 return -EINVAL;
8578
8579 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
8580 cmsgs->prinfo->pr_value = 0;
8581 break;
Xin Long3ff547c2018-03-14 19:05:31 +08008582 case SCTP_AUTHINFO:
8583 /* SCTP Socket API Extension
8584 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
8585 *
8586 * This cmsghdr structure specifies SCTP options for sendmsg().
8587 *
8588 * cmsg_level cmsg_type cmsg_data[]
8589 * ------------ ------------ ---------------------
8590 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
8591 */
8592 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
8593 return -EINVAL;
8594
8595 cmsgs->authinfo = CMSG_DATA(cmsg);
8596 break;
Xin Long2c0dbaa2018-03-05 20:44:19 +08008597 case SCTP_DSTADDRV4:
8598 case SCTP_DSTADDRV6:
8599 /* SCTP Socket API Extension
8600 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
8601 *
8602 * This cmsghdr structure specifies SCTP options for sendmsg().
8603 *
8604 * cmsg_level cmsg_type cmsg_data[]
8605 * ------------ ------------ ---------------------
8606 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
8607 * ------------ ------------ ---------------------
8608 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
8609 */
8610 cmsgs->addrs_msg = my_msg;
8611 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612 default:
8613 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07008614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615 }
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008616
Linus Torvalds1da177e2005-04-16 15:20:36 -07008617 return 0;
8618}
8619
8620/*
8621 * Wait for a packet..
8622 * Note: This function is the same function as in core/datagram.c
8623 * with a few modifications to make lksctp work.
8624 */
wangweidong26ac8e52013-12-23 12:16:51 +08008625static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008626{
8627 int error;
8628 DEFINE_WAIT(wait);
8629
Eric Dumazetaa395142010-04-20 13:03:51 +00008630 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008631
8632 /* Socket errors? */
8633 error = sock_error(sk);
8634 if (error)
8635 goto out;
8636
8637 if (!skb_queue_empty(&sk->sk_receive_queue))
8638 goto ready;
8639
8640 /* Socket shut down? */
8641 if (sk->sk_shutdown & RCV_SHUTDOWN)
8642 goto out;
8643
8644 /* Sequenced packets can come disconnected. If so we report the
8645 * problem.
8646 */
8647 error = -ENOTCONN;
8648
8649 /* Is there a good reason to think that we may receive some data? */
8650 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8651 goto out;
8652
8653 /* Handle signals. */
8654 if (signal_pending(current))
8655 goto interrupted;
8656
8657 /* Let another process have a go. Since we are going to sleep
8658 * anyway. Note: This may cause odd behaviors if the message
8659 * does not fit in the user's buffer, but this seems to be the
8660 * only way to honor MSG_DONTWAIT realistically.
8661 */
wangweidong048ed4b2014-01-21 15:44:11 +08008662 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008663 *timeo_p = schedule_timeout(*timeo_p);
wangweidong048ed4b2014-01-21 15:44:11 +08008664 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665
8666ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00008667 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008668 return 0;
8669
8670interrupted:
8671 error = sock_intr_errno(*timeo_p);
8672
8673out:
Eric Dumazetaa395142010-04-20 13:03:51 +00008674 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008675 *err = error;
8676 return error;
8677}
8678
8679/* Receive a datagram.
8680 * Note: This is pretty much the same routine as in core/datagram.c
8681 * with a few changes to make lksctp work.
8682 */
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02008683struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8684 int noblock, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008685{
8686 int error;
8687 struct sk_buff *skb;
8688 long timeo;
8689
Linus Torvalds1da177e2005-04-16 15:20:36 -07008690 timeo = sock_rcvtimeo(sk, noblock);
8691
Daniel Borkmannbb333812013-06-28 19:49:40 +02008692 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8693 MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008694
8695 do {
8696 /* Again only user level code calls this function,
8697 * so nothing interrupt level
8698 * will suddenly eat the receive_queue.
8699 *
8700 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00008701 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702 */
8703 if (flags & MSG_PEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008704 skb = skb_peek(&sk->sk_receive_queue);
8705 if (skb)
Reshetova, Elena63354792017-06-30 13:07:58 +03008706 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008707 } else {
Marcelo Ricardo Leitner311b2172016-04-13 19:12:29 -03008708 skb = __skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008709 }
8710
8711 if (skb)
8712 return skb;
8713
Neil Horman6736dc32005-12-02 20:30:06 -08008714 /* Caller is allowed not to check sk->sk_err before calling. */
8715 error = sock_error(sk);
8716 if (error)
8717 goto no_packet;
8718
Linus Torvalds1da177e2005-04-16 15:20:36 -07008719 if (sk->sk_shutdown & RCV_SHUTDOWN)
8720 break;
8721
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07008722 if (sk_can_busy_loop(sk)) {
8723 sk_busy_loop(sk, noblock);
8724
8725 if (!skb_queue_empty(&sk->sk_receive_queue))
8726 continue;
8727 }
Neil Horman8465a5f2014-04-17 15:26:51 -04008728
Linus Torvalds1da177e2005-04-16 15:20:36 -07008729 /* User doesn't want to wait. */
8730 error = -EAGAIN;
8731 if (!timeo)
8732 goto no_packet;
8733 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8734
8735 return NULL;
8736
8737no_packet:
8738 *err = error;
8739 return NULL;
8740}
8741
8742/* If sndbuf has changed, wake up per association sndbuf waiters. */
8743static void __sctp_write_space(struct sctp_association *asoc)
8744{
8745 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008746
Eric Dumazetceb5d582015-11-29 20:03:11 -08008747 if (sctp_wspace(asoc) <= 0)
8748 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008749
Eric Dumazetceb5d582015-11-29 20:03:11 -08008750 if (waitqueue_active(&asoc->wait))
8751 wake_up_interruptible(&asoc->wait);
Eric Dumazeteaefd1102011-02-18 03:26:36 +00008752
Eric Dumazetceb5d582015-11-29 20:03:11 -08008753 if (sctp_writeable(sk)) {
8754 struct socket_wq *wq;
8755
8756 rcu_read_lock();
8757 wq = rcu_dereference(sk->sk_wq);
8758 if (wq) {
8759 if (waitqueue_active(&wq->wait))
8760 wake_up_interruptible(&wq->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008761
8762 /* Note that we try to include the Async I/O support
8763 * here by modeling from the current TCP/UDP code.
8764 * We have not tested with it yet.
8765 */
Eric Dumazeteaefd1102011-02-18 03:26:36 +00008766 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Eric Dumazetceb5d582015-11-29 20:03:11 -08008767 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008768 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08008769 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008770 }
8771}
8772
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008773static void sctp_wake_up_waiters(struct sock *sk,
8774 struct sctp_association *asoc)
8775{
8776 struct sctp_association *tmp = asoc;
8777
8778 /* We do accounting for the sndbuf space per association,
8779 * so we only need to wake our own association.
8780 */
8781 if (asoc->ep->sndbuf_policy)
8782 return __sctp_write_space(asoc);
8783
Daniel Borkmann1e1cdf82014-04-09 16:10:20 +02008784 /* If association goes down and is just flushing its
8785 * outq, then just normally notify others.
8786 */
8787 if (asoc->base.dead)
8788 return sctp_write_space(sk);
8789
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008790 /* Accounting for the sndbuf space is per socket, so we
8791 * need to wake up others, try to be fair and in case of
8792 * other associations, let them have a go first instead
8793 * of just doing a sctp_write_space() call.
8794 *
8795 * Note that we reach sctp_wake_up_waiters() only when
8796 * associations free up queued chunks, thus we are under
8797 * lock and the list of associations on a socket is
8798 * guaranteed not to change.
8799 */
8800 for (tmp = list_next_entry(tmp, asocs); 1;
8801 tmp = list_next_entry(tmp, asocs)) {
8802 /* Manually skip the head element. */
8803 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8804 continue;
8805 /* Wake up association. */
8806 __sctp_write_space(tmp);
8807 /* We've reached the end. */
8808 if (tmp == asoc)
8809 break;
8810 }
8811}
8812
Linus Torvalds1da177e2005-04-16 15:20:36 -07008813/* Do accounting for the sndbuf space.
8814 * Decrement the used sndbuf space of the corresponding association by the
8815 * data size which was just transmitted(freed).
8816 */
8817static void sctp_wfree(struct sk_buff *skb)
8818{
Daniel Borkmannf869c912014-11-20 01:54:48 +01008819 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8820 struct sctp_association *asoc = chunk->asoc;
8821 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008822
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008823 sk_mem_uncharge(sk, skb->truesize);
Xin Long605c0ac2018-10-17 03:07:50 +08008824 sk->sk_wmem_queued -= skb->truesize + sizeof(struct sctp_chunk);
8825 asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk);
8826 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk),
8827 &sk->sk_wmem_alloc));
Neil Horman4d93df02007-08-15 16:07:44 -07008828
Xin Longec2e5062018-03-14 19:05:33 +08008829 if (chunk->shkey) {
8830 struct sctp_shared_key *shkey = chunk->shkey;
8831
8832 /* refcnt == 2 and !list_empty mean after this release, it's
8833 * not being used anywhere, and it's time to notify userland
8834 * that this shkey can be freed if it's been deactivated.
8835 */
8836 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
8837 refcount_read(&shkey->refcnt) == 2) {
8838 struct sctp_ulpevent *ev;
8839
8840 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
8841 SCTP_AUTH_FREE_KEY,
8842 GFP_KERNEL);
8843 if (ev)
8844 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
8845 }
Xin Long1b1e0bc2018-03-14 19:05:30 +08008846 sctp_auth_shkey_release(chunk->shkey);
Xin Longec2e5062018-03-14 19:05:33 +08008847 }
Xin Long1b1e0bc2018-03-14 19:05:30 +08008848
Neil Horman4eb701d2005-04-28 12:02:04 -07008849 sock_wfree(skb);
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008850 sctp_wake_up_waiters(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008851
8852 sctp_association_put(asoc);
8853}
8854
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008855/* Do accounting for the receive space on the socket.
8856 * Accounting for the association is done in ulpevent.c
8857 * We set this as a destructor for the cloned data skbs so that
8858 * accounting is done at the correct time.
8859 */
8860void sctp_sock_rfree(struct sk_buff *skb)
8861{
8862 struct sock *sk = skb->sk;
8863 struct sctp_ulpevent *event = sctp_skb2event(skb);
8864
8865 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07008866
8867 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008868 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07008869 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008870 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008871}
8872
8873
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874/* Helper function to wait for space in the sndbuf. */
8875static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +08008876 size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008877{
8878 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008879 long current_timeo = *timeo_p;
8880 DEFINE_WAIT(wait);
Xin Longa0ff6602018-01-15 17:01:36 +08008881 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008882
Daniel Borkmannbb333812013-06-28 19:49:40 +02008883 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8884 *timeo_p, msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008885
8886 /* Increment the association's refcnt. */
8887 sctp_association_hold(asoc);
8888
8889 /* Wait on the association specific sndbuf space. */
8890 for (;;) {
8891 prepare_to_wait_exclusive(&asoc->wait, &wait,
8892 TASK_INTERRUPTIBLE);
Xin Longca3af4d2017-11-15 16:55:54 +08008893 if (asoc->base.dead)
8894 goto do_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008895 if (!*timeo_p)
8896 goto do_nonblock;
Xin Longca3af4d2017-11-15 16:55:54 +08008897 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008898 goto do_error;
8899 if (signal_pending(current))
8900 goto do_interrupted;
Xin Long10339902019-04-15 17:15:06 +08008901 if (sk_under_memory_pressure(sk))
8902 sk_mem_reclaim(sk);
8903 if ((int)msg_len <= sctp_wspace(asoc) &&
8904 sk_wmem_schedule(sk, msg_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008905 break;
8906
8907 /* Let another process have a go. Since we are going
8908 * to sleep anyway.
8909 */
wangweidong048ed4b2014-01-21 15:44:11 +08008910 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008911 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008912 lock_sock(sk);
Xin Longa0ff6602018-01-15 17:01:36 +08008913 if (sk != asoc->base.sk)
8914 goto do_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008915
8916 *timeo_p = current_timeo;
8917 }
8918
8919out:
8920 finish_wait(&asoc->wait, &wait);
8921
8922 /* Release the association's refcnt. */
8923 sctp_association_put(asoc);
8924
8925 return err;
8926
Xin Longca3af4d2017-11-15 16:55:54 +08008927do_dead:
8928 err = -ESRCH;
8929 goto out;
8930
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931do_error:
8932 err = -EPIPE;
8933 goto out;
8934
8935do_interrupted:
8936 err = sock_intr_errno(*timeo_p);
8937 goto out;
8938
8939do_nonblock:
8940 err = -EAGAIN;
8941 goto out;
8942}
8943
David S. Miller676d2362014-04-11 16:15:36 -04008944void sctp_data_ready(struct sock *sk)
Wei Yongjun561b1732010-04-28 08:47:18 +00008945{
David S. Miller7ef52732010-05-02 21:43:40 -07008946 struct socket_wq *wq;
8947
8948 rcu_read_lock();
8949 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08008950 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008951 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8952 EPOLLRDNORM | EPOLLRDBAND);
Wei Yongjun561b1732010-04-28 08:47:18 +00008953 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07008954 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00008955}
8956
Linus Torvalds1da177e2005-04-16 15:20:36 -07008957/* If socket sndbuf has changed, wake up all per association waiters. */
8958void sctp_write_space(struct sock *sk)
8959{
8960 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008961
8962 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f02008-04-12 18:54:24 -07008963 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008964 __sctp_write_space(asoc);
8965 }
8966}
8967
8968/* Is there any sndbuf space available on the socket?
8969 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08008970 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971 * associations on the same socket. For a UDP-style socket with
8972 * multiple associations, it is possible for it to be "unwriteable"
8973 * prematurely. I assume that this is acceptable because
8974 * a premature "unwriteable" is better than an accidental "writeable" which
8975 * would cause an unwanted block under certain circumstances. For the 1-1
8976 * UDP-style sockets or TCP-style sockets, this code should work.
8977 * - Daisy
8978 */
Xin Longcd305c72018-10-17 03:07:51 +08008979static bool sctp_writeable(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008980{
Xin Longcd305c72018-10-17 03:07:51 +08008981 return sk->sk_sndbuf > sk->sk_wmem_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008982}
8983
8984/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8985 * returns immediately with EINPROGRESS.
8986 */
8987static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8988{
8989 struct sock *sk = asoc->base.sk;
8990 int err = 0;
8991 long current_timeo = *timeo_p;
8992 DEFINE_WAIT(wait);
8993
Daniel Borkmannbb333812013-06-28 19:49:40 +02008994 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008995
8996 /* Increment the association's refcnt. */
8997 sctp_association_hold(asoc);
8998
8999 for (;;) {
9000 prepare_to_wait_exclusive(&asoc->wait, &wait,
9001 TASK_INTERRUPTIBLE);
9002 if (!*timeo_p)
9003 goto do_nonblock;
9004 if (sk->sk_shutdown & RCV_SHUTDOWN)
9005 break;
9006 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
9007 asoc->base.dead)
9008 goto do_error;
9009 if (signal_pending(current))
9010 goto do_interrupted;
9011
9012 if (sctp_state(asoc, ESTABLISHED))
9013 break;
9014
9015 /* Let another process have a go. Since we are going
9016 * to sleep anyway.
9017 */
wangweidong048ed4b2014-01-21 15:44:11 +08009018 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009019 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08009020 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009021
9022 *timeo_p = current_timeo;
9023 }
9024
9025out:
9026 finish_wait(&asoc->wait, &wait);
9027
9028 /* Release the association's refcnt. */
9029 sctp_association_put(asoc);
9030
9031 return err;
9032
9033do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08009034 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009035 err = -ETIMEDOUT;
9036 else
9037 err = -ECONNREFUSED;
9038 goto out;
9039
9040do_interrupted:
9041 err = sock_intr_errno(*timeo_p);
9042 goto out;
9043
9044do_nonblock:
9045 err = -EINPROGRESS;
9046 goto out;
9047}
9048
9049static int sctp_wait_for_accept(struct sock *sk, long timeo)
9050{
9051 struct sctp_endpoint *ep;
9052 int err = 0;
9053 DEFINE_WAIT(wait);
9054
9055 ep = sctp_sk(sk)->ep;
9056
9057
9058 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00009059 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009060 TASK_INTERRUPTIBLE);
9061
9062 if (list_empty(&ep->asocs)) {
wangweidong048ed4b2014-01-21 15:44:11 +08009063 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009064 timeo = schedule_timeout(timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08009065 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009066 }
9067
9068 err = -EINVAL;
9069 if (!sctp_sstate(sk, LISTENING))
9070 break;
9071
9072 err = 0;
9073 if (!list_empty(&ep->asocs))
9074 break;
9075
9076 err = sock_intr_errno(timeo);
9077 if (signal_pending(current))
9078 break;
9079
9080 err = -EAGAIN;
9081 if (!timeo)
9082 break;
9083 }
9084
Eric Dumazetaa395142010-04-20 13:03:51 +00009085 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009086
9087 return err;
9088}
9089
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02009090static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009091{
9092 DEFINE_WAIT(wait);
9093
9094 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00009095 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009096 if (list_empty(&sctp_sk(sk)->ep->asocs))
9097 break;
wangweidong048ed4b2014-01-21 15:44:11 +08009098 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009099 timeout = schedule_timeout(timeout);
wangweidong048ed4b2014-01-21 15:44:11 +08009100 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009101 } while (!signal_pending(current) && timeout);
9102
Eric Dumazetaa395142010-04-20 13:03:51 +00009103 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104}
9105
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07009106static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
9107{
9108 struct sk_buff *frag;
9109
9110 if (!skb->data_len)
9111 goto done;
9112
9113 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07009114 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07009115 sctp_skb_set_owner_r_frag(frag, sk);
9116
9117done:
9118 sctp_skb_set_owner_r(skb, sk);
9119}
9120
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009121void sctp_copy_sock(struct sock *newsk, struct sock *sk,
9122 struct sctp_association *asoc)
9123{
9124 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08009125 struct inet_sock *newinet;
Richard Haines2277c7c2018-02-13 20:56:24 +00009126 struct sctp_sock *sp = sctp_sk(sk);
9127 struct sctp_endpoint *ep = sp->ep;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009128
9129 newsk->sk_type = sk->sk_type;
9130 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
9131 newsk->sk_flags = sk->sk_flags;
Marcelo Ricardo Leitner50a5ffb2015-12-04 15:14:05 -02009132 newsk->sk_tsflags = sk->sk_tsflags;
Tom Herbert28448b82014-05-23 08:47:19 -07009133 newsk->sk_no_check_tx = sk->sk_no_check_tx;
9134 newsk->sk_no_check_rx = sk->sk_no_check_rx;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009135 newsk->sk_reuse = sk->sk_reuse;
Xin Longb0e9a2f2018-06-28 15:31:00 +08009136 sctp_sk(newsk)->reuse = sp->reuse;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009137
9138 newsk->sk_shutdown = sk->sk_shutdown;
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02009139 newsk->sk_destruct = sctp_destruct_sock;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009140 newsk->sk_family = sk->sk_family;
9141 newsk->sk_protocol = IPPROTO_SCTP;
9142 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
9143 newsk->sk_sndbuf = sk->sk_sndbuf;
9144 newsk->sk_rcvbuf = sk->sk_rcvbuf;
9145 newsk->sk_lingertime = sk->sk_lingertime;
9146 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
9147 newsk->sk_sndtimeo = sk->sk_sndtimeo;
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03009148 newsk->sk_rxhash = sk->sk_rxhash;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009149
9150 newinet = inet_sk(newsk);
9151
9152 /* Initialize sk's sport, dport, rcv_saddr and daddr for
9153 * getsockname() and getpeername()
9154 */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00009155 newinet->inet_sport = inet->inet_sport;
9156 newinet->inet_saddr = inet->inet_saddr;
9157 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
9158 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009159 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00009160 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009161
9162 newinet->uc_ttl = inet->uc_ttl;
9163 newinet->mc_loop = 1;
9164 newinet->mc_ttl = 1;
9165 newinet->mc_index = 0;
9166 newinet->mc_list = NULL;
Marcelo Ricardo Leitner01ce63c2015-12-04 15:14:04 -02009167
9168 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
9169 net_enable_timestamp();
Marcelo Ricardo Leitner3538a5c2015-12-23 16:44:09 -02009170
Richard Haines2277c7c2018-02-13 20:56:24 +00009171 /* Set newsk security attributes from orginal sk and connection
9172 * security attribute from ep.
9173 */
9174 security_sctp_sk_clone(ep, sk, newsk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00009175}
9176
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03009177static inline void sctp_copy_descendant(struct sock *sk_to,
9178 const struct sock *sk_from)
9179{
9180 int ancestor_size = sizeof(struct inet_sock) +
9181 sizeof(struct sctp_sock) -
Xin Long636d25d2019-03-18 19:58:29 +08009182 offsetof(struct sctp_sock, pd_lobby);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03009183
9184 if (sk_from->sk_family == PF_INET6)
9185 ancestor_size += sizeof(struct ipv6_pinfo);
9186
9187 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
9188}
9189
Linus Torvalds1da177e2005-04-16 15:20:36 -07009190/* Populate the fields of the newsk from the oldsk and migrate the assoc
9191 * and its messages to the newsk.
9192 */
Xin Long89664c622019-03-03 17:54:53 +08009193static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
9194 struct sctp_association *assoc,
9195 enum sctp_socket_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009196{
9197 struct sctp_sock *oldsp = sctp_sk(oldsk);
9198 struct sctp_sock *newsp = sctp_sk(newsk);
9199 struct sctp_bind_bucket *pp; /* hash list port iterator */
9200 struct sctp_endpoint *newep = newsp->ep;
9201 struct sk_buff *skb, *tmp;
9202 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08009203 struct sctp_bind_hashbucket *head;
Xin Long89664c622019-03-03 17:54:53 +08009204 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009205
9206 /* Migrate socket buffer sizes and all the socket level options to the
9207 * new socket.
9208 */
9209 newsk->sk_sndbuf = oldsk->sk_sndbuf;
9210 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
9211 /* Brute force copy old sctp opt. */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03009212 sctp_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009213
9214 /* Restore the ep value that was overwritten with the above structure
9215 * copy.
9216 */
9217 newsp->ep = newep;
9218 newsp->hmac = NULL;
9219
9220 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00009221 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
9222 inet_sk(oldsk)->inet_num)];
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01009223 spin_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009224 pp = sctp_sk(oldsk)->bind_hash;
9225 sk_add_bind_node(newsk, &pp->owner);
9226 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00009227 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01009228 spin_unlock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009229
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07009230 /* Copy the bind_addr list from the original endpoint to the new
9231 * endpoint so that we can handle restarts properly
9232 */
Xin Long89664c622019-03-03 17:54:53 +08009233 err = sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
9234 &oldsp->ep->base.bind_addr, GFP_KERNEL);
9235 if (err)
9236 return err;
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07009237
Xin Longc6f33e02019-03-03 17:54:55 +08009238 /* New ep's auth_hmacs should be set if old ep's is set, in case
9239 * that net->sctp.auth_enable has been changed to 0 by users and
9240 * new ep's auth_hmacs couldn't be set in sctp_endpoint_init().
9241 */
9242 if (oldsp->ep->auth_hmacs) {
9243 err = sctp_auth_init_hmacs(newsp->ep, GFP_KERNEL);
9244 if (err)
9245 return err;
9246 }
9247
Linus Torvalds1da177e2005-04-16 15:20:36 -07009248 /* Move any messages in the old socket's receive queue that are for the
9249 * peeled off association to the new socket's receive queue.
9250 */
9251 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
9252 event = sctp_skb2event(skb);
9253 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07009254 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009255 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07009256 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009257 }
9258 }
9259
9260 /* Clean up any messages pending delivery due to partial
9261 * delivery. Three cases:
9262 * 1) No partial deliver; no work.
9263 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
9264 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
9265 */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07009266 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009267
Vlad Yasevichb6e13312007-04-20 12:23:15 -07009268 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009269 struct sk_buff_head *queue;
9270
9271 /* Decide which queue to move pd_lobby skbs to. */
9272 if (assoc->ulpq.pd_mode) {
9273 queue = &newsp->pd_lobby;
9274 } else
9275 queue = &newsk->sk_receive_queue;
9276
9277 /* Walk through the pd_lobby, looking for skbs that
9278 * need moved to the new socket.
9279 */
9280 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
9281 event = sctp_skb2event(skb);
9282 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07009283 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009284 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07009285 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009286 }
9287 }
9288
9289 /* Clear up any skbs waiting for the partial
9290 * delivery to finish.
9291 */
9292 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07009293 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009294
9295 }
9296
Xin Long13228232017-12-08 21:04:09 +08009297 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07009298
Linus Torvalds1da177e2005-04-16 15:20:36 -07009299 /* Set the type of socket to indicate that it is peeled off from the
9300 * original UDP-style socket or created with the accept() call on a
9301 * TCP-style socket..
9302 */
9303 newsp->type = type;
9304
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07009305 /* Mark the new socket "in-use" by the user so that any packets
9306 * that may arrive on the association after we've moved it are
9307 * queued to the backlog. This prevents a potential race between
9308 * backlog processing on the old socket and new-packet processing
9309 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07009310 *
9311 * The caller has just allocated newsk so we can guarantee that other
9312 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07009313 */
Zach Brown5131a182007-06-22 15:14:46 -07009314 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Xin Longd04adf12017-10-28 02:13:29 +08009315 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009316 sctp_assoc_migrate(assoc, newsk);
Xin Longd04adf12017-10-28 02:13:29 +08009317 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009318
9319 /* If the association on the newsk is already closed before accept()
9320 * is called, set RCV_SHUTDOWN flag.
9321 */
Xin Longd46e4162016-06-09 22:48:18 +08009322 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08009323 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324 newsk->sk_shutdown |= RCV_SHUTDOWN;
Xin Longd46e4162016-06-09 22:48:18 +08009325 } else {
Yafang Shaocbabf462017-12-20 11:12:54 +08009326 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
Xin Longd46e4162016-06-09 22:48:18 +08009327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009328
wangweidong048ed4b2014-01-21 15:44:11 +08009329 release_sock(newsk);
Xin Long89664c622019-03-03 17:54:53 +08009330
9331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009332}
9333
Neil Horman4d93df02007-08-15 16:07:44 -07009334
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335/* This proto struct describes the ULP interface for SCTP. */
9336struct proto sctp_prot = {
9337 .name = "SCTP",
9338 .owner = THIS_MODULE,
9339 .close = sctp_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009340 .disconnect = sctp_disconnect,
9341 .accept = sctp_accept,
9342 .ioctl = sctp_ioctl,
9343 .init = sctp_init_sock,
9344 .destroy = sctp_destroy_sock,
9345 .shutdown = sctp_shutdown,
9346 .setsockopt = sctp_setsockopt,
9347 .getsockopt = sctp_getsockopt,
9348 .sendmsg = sctp_sendmsg,
9349 .recvmsg = sctp_recvmsg,
9350 .bind = sctp_bind,
9351 .backlog_rcv = sctp_backlog_rcv,
9352 .hash = sctp_hash,
9353 .unhash = sctp_unhash,
9354 .get_port = sctp_get_port,
9355 .obj_size = sizeof(struct sctp_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07009356 .useroffset = offsetof(struct sctp_sock, subscribe),
9357 .usersize = offsetof(struct sctp_sock, initmsg) -
9358 offsetof(struct sctp_sock, subscribe) +
9359 sizeof_field(struct sctp_sock, initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07009360 .sysctl_mem = sysctl_sctp_mem,
9361 .sysctl_rmem = sysctl_sctp_rmem,
9362 .sysctl_wmem = sysctl_sctp_wmem,
9363 .memory_pressure = &sctp_memory_pressure,
9364 .enter_memory_pressure = sctp_enter_memory_pressure,
9365 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08009366 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009367};
9368
Eric Dumazetdfd56b82011-12-10 09:48:31 +00009369#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08009370
Eric Dumazet602dd622015-12-01 07:20:07 -08009371#include <net/transp_v6.h>
9372static void sctp_v6_destroy_sock(struct sock *sk)
9373{
9374 sctp_destroy_sock(sk);
9375 inet6_destroy_sock(sk);
9376}
9377
Linus Torvalds1da177e2005-04-16 15:20:36 -07009378struct proto sctpv6_prot = {
9379 .name = "SCTPv6",
9380 .owner = THIS_MODULE,
9381 .close = sctp_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009382 .disconnect = sctp_disconnect,
9383 .accept = sctp_accept,
9384 .ioctl = sctp_ioctl,
9385 .init = sctp_init_sock,
Eric Dumazet602dd622015-12-01 07:20:07 -08009386 .destroy = sctp_v6_destroy_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387 .shutdown = sctp_shutdown,
9388 .setsockopt = sctp_setsockopt,
9389 .getsockopt = sctp_getsockopt,
9390 .sendmsg = sctp_sendmsg,
9391 .recvmsg = sctp_recvmsg,
9392 .bind = sctp_bind,
9393 .backlog_rcv = sctp_backlog_rcv,
9394 .hash = sctp_hash,
9395 .unhash = sctp_unhash,
9396 .get_port = sctp_get_port,
9397 .obj_size = sizeof(struct sctp6_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07009398 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
9399 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
9400 offsetof(struct sctp6_sock, sctp.subscribe) +
9401 sizeof_field(struct sctp6_sock, sctp.initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07009402 .sysctl_mem = sysctl_sctp_mem,
9403 .sysctl_rmem = sysctl_sctp_rmem,
9404 .sysctl_wmem = sysctl_sctp_wmem,
9405 .memory_pressure = &sctp_memory_pressure,
9406 .enter_memory_pressure = sctp_enter_memory_pressure,
9407 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08009408 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009409};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00009410#endif /* IS_ENABLED(CONFIG_IPV6) */