blob: 2a2e094560dedafa043b7374941e82559e47a739 [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
8 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05009 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
13 *
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
22 * any later version.
23 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050024 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080031 * along with GNU CC; see the file COPYING. If not, see
32 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
34 * Please send any bug reports or fixes you make to the
35 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020036 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * Written or modified by:
39 * La Monte H.P. Yarroll <piggy@acm.org>
40 * Narasimha Budihal <narsi@refcode.org>
41 * Karl Knutson <karl@athena.chicago.il.us>
42 * Jon Grimm <jgrimm@us.ibm.com>
43 * Xingang Guo <xingang.guo@intel.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Sridhar Samudrala <samudrala@us.ibm.com>
46 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
47 * Ardelle Fan <ardelle.fan@intel.com>
48 * Ryan Layer <rmlayer@us.ibm.com>
49 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
50 * Kevin Gao <kevin.gao@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
52
Joe Perches145ce502010-08-24 13:21:08 +000053#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
Herbert Xu5821c762016-01-24 21:20:12 +080055#include <crypto/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/types.h>
57#include <linux/kernel.h>
58#include <linux/wait.h>
59#include <linux/time.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010060#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/ip.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080062#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/fcntl.h>
64#include <linux/poll.h>
65#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090066#include <linux/slab.h>
Al Viro56b31d12012-08-18 00:25:51 -040067#include <linux/file.h>
Daniel Borkmannffd59392014-02-17 12:11:11 +010068#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#include <net/ip.h>
71#include <net/icmp.h>
72#include <net/route.h>
73#include <net/ipv6.h>
74#include <net/inet_common.h>
Neil Horman8465a5f2014-04-17 15:26:51 -040075#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#include <linux/socket.h> /* for sa_family_t */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040078#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <net/sock.h>
80#include <net/sctp/sctp.h>
81#include <net/sctp/sm.h>
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -030082#include <net/sctp/stream_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/* Forward declarations for internal helper functions. */
85static int sctp_writeable(struct sock *sk);
86static void sctp_wfree(struct sk_buff *skb);
Xin Longcea0cc82017-11-15 16:57:26 +080087static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +080088 size_t msg_len);
wangweidong26ac8e52013-12-23 12:16:51 +080089static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
91static int sctp_wait_for_accept(struct sock *sk, long timeo);
92static void sctp_wait_for_close(struct sock *sk, long timeo);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +020093static void sctp_destruct_sock(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
95 union sctp_addr *addr, int len);
96static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
97static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
98static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
99static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
100static int sctp_send_asconf(struct sctp_association *asoc,
101 struct sctp_chunk *chunk);
102static int sctp_do_bind(struct sock *, union sctp_addr *, int);
103static int sctp_autobind(struct sock *sk);
Xin Longb7ef2612017-08-11 10:23:50 +0800104static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
105 struct sctp_association *assoc,
106 enum sctp_socket_type type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Eric Dumazet06044752017-06-07 13:29:12 -0700108static unsigned long sctp_memory_pressure;
Eric Dumazet8d987e52010-11-09 23:24:26 +0000109static atomic_long_t sctp_memory_allocated;
Eric Dumazet17483762008-11-25 21:16:35 -0800110struct percpu_counter sctp_sockets_allocated;
Neil Horman4d93df02007-08-15 16:07:44 -0700111
Pavel Emelyanov5c52ba12008-07-16 20:28:10 -0700112static void sctp_enter_memory_pressure(struct sock *sk)
Neil Horman4d93df02007-08-15 16:07:44 -0700113{
114 sctp_memory_pressure = 1;
115}
116
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/* Get the sndbuf space available at the time on the association. */
119static inline int sctp_wspace(struct sctp_association *asoc)
120{
Neil Horman4d93df02007-08-15 16:07:44 -0700121 int amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Neil Horman4d93df02007-08-15 16:07:44 -0700123 if (asoc->ep->sndbuf_policy)
124 amt = asoc->sndbuf_used;
125 else
Eric Dumazet31e6d362009-06-17 19:05:41 -0700126 amt = sk_wmem_alloc_get(asoc->base.sk);
Neil Horman4d93df02007-08-15 16:07:44 -0700127
128 if (amt >= asoc->base.sk->sk_sndbuf) {
129 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
130 amt = 0;
131 else {
132 amt = sk_stream_wspace(asoc->base.sk);
133 if (amt < 0)
134 amt = 0;
135 }
Neil Horman4eb701d2005-04-28 12:02:04 -0700136 } else {
Neil Horman4d93df02007-08-15 16:07:44 -0700137 amt = asoc->base.sk->sk_sndbuf - amt;
Neil Horman4eb701d2005-04-28 12:02:04 -0700138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return amt;
140}
141
142/* Increment the used sndbuf space count of the corresponding association by
143 * the size of the outgoing data chunk.
144 * Also, set the skb destructor for sndbuf accounting later.
145 *
146 * Since it is always 1-1 between chunk and skb, and also a new skb is always
147 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
148 * destructor in the data chunk skb for the purpose of the sndbuf space
149 * tracking.
150 */
151static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
152{
153 struct sctp_association *asoc = chunk->asoc;
154 struct sock *sk = asoc->base.sk;
155
156 /* The sndbuf space is tracked per association. */
157 sctp_association_hold(asoc);
158
Xin Long1b1e0bc2018-03-14 19:05:30 +0800159 if (chunk->shkey)
160 sctp_auth_shkey_hold(chunk->shkey);
161
Neil Horman4eb701d2005-04-28 12:02:04 -0700162 skb_set_owner_w(chunk->skb, sk);
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 chunk->skb->destructor = sctp_wfree;
165 /* Save the chunk pointer in skb for sctp_wfree to use later. */
Daniel Borkmannf869c912014-11-20 01:54:48 +0100166 skb_shinfo(chunk->skb)->destructor_arg = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Neil Horman4eb701d2005-04-28 12:02:04 -0700168 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
169 sizeof(struct sk_buff) +
170 sizeof(struct sctp_chunk);
171
Reshetova, Elena14afee42017-06-30 13:08:00 +0300172 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800173 sk->sk_wmem_queued += chunk->skb->truesize;
174 sk_mem_charge(sk, chunk->skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Xin Longd04adf12017-10-28 02:13:29 +0800177static void sctp_clear_owner_w(struct sctp_chunk *chunk)
178{
179 skb_orphan(chunk->skb);
180}
181
182static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
183 void (*cb)(struct sctp_chunk *))
184
185{
186 struct sctp_outq *q = &asoc->outqueue;
187 struct sctp_transport *t;
188 struct sctp_chunk *chunk;
189
190 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
191 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
192 cb(chunk);
193
Xin Longa8dd3972017-11-26 20:56:07 +0800194 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800195 cb(chunk);
196
Xin Longa8dd3972017-11-26 20:56:07 +0800197 list_for_each_entry(chunk, &q->sacked, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800198 cb(chunk);
199
Xin Longa8dd3972017-11-26 20:56:07 +0800200 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800201 cb(chunk);
202
203 list_for_each_entry(chunk, &q->out_chunk_list, list)
204 cb(chunk);
205}
206
Xin Long13228232017-12-08 21:04:09 +0800207static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
208 void (*cb)(struct sk_buff *, struct sock *))
209
210{
211 struct sk_buff *skb, *tmp;
212
213 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
214 cb(skb, sk);
215
216 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
217 cb(skb, sk);
218
219 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
220 cb(skb, sk);
221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/* Verify that this is a valid address. */
224static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
225 int len)
226{
227 struct sctp_af *af;
228
229 /* Verify basic sockaddr. */
230 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
231 if (!af)
232 return -EINVAL;
233
234 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700235 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return -EINVAL;
237
238 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
239 return -EINVAL;
240
241 return 0;
242}
243
244/* Look up the association by its id. If this is not a UDP-style
245 * socket, the ID field is always ignored.
246 */
247struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
248{
249 struct sctp_association *asoc = NULL;
250
251 /* If this is not a UDP-style socket, assoc id should be ignored. */
252 if (!sctp_style(sk, UDP)) {
253 /* Return NULL if the socket state is not ESTABLISHED. It
254 * could be a TCP-style listening socket or a socket which
255 * hasn't yet called connect() to establish an association.
256 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -0300257 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return NULL;
259
260 /* Get the first and the only association from the list. */
261 if (!list_empty(&sctp_sk(sk)->ep->asocs))
262 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
263 struct sctp_association, asocs);
264 return asoc;
265 }
266
267 /* Otherwise this is a UDP-style socket. */
268 if (!id || (id == (sctp_assoc_t)-1))
269 return NULL;
270
271 spin_lock_bh(&sctp_assocs_id_lock);
272 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
273 spin_unlock_bh(&sctp_assocs_id_lock);
274
275 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
276 return NULL;
277
278 return asoc;
279}
280
281/* Look up the transport from an address and an assoc id. If both address and
282 * id are specified, the associations matching the address and the id should be
283 * the same.
284 */
285static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
286 struct sockaddr_storage *addr,
287 sctp_assoc_t id)
288{
289 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
Xin Long6f29a132017-01-24 14:01:53 +0800290 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 union sctp_addr *laddr = (union sctp_addr *)addr;
Xin Long6f29a132017-01-24 14:01:53 +0800292 struct sctp_transport *transport;
293
Xin Long912964e2017-02-07 20:56:08 +0800294 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
Xin Long6f29a132017-01-24 14:01:53 +0800295 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800298 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (!addr_asoc)
302 return NULL;
303
304 id_asoc = sctp_id2assoc(sk, id);
305 if (id_asoc && (id_asoc != addr_asoc))
306 return NULL;
307
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600308 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 (union sctp_addr *)addr);
310
311 return transport;
312}
313
314/* API 3.1.2 bind() - UDP Style Syntax
315 * The syntax of bind() is,
316 *
317 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
318 *
319 * sd - the socket descriptor returned by socket().
320 * addr - the address structure (struct sockaddr_in or struct
321 * sockaddr_in6 [RFC 2553]),
322 * addr_len - the size of the address structure.
323 */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200324static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 int retval = 0;
327
wangweidong048ed4b2014-01-21 15:44:11 +0800328 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Daniel Borkmannbb333812013-06-28 19:49:40 +0200330 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
331 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* Disallow binding twice. */
334 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700335 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 addr_len);
337 else
338 retval = -EINVAL;
339
wangweidong048ed4b2014-01-21 15:44:11 +0800340 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 return retval;
343}
344
345static long sctp_get_port_local(struct sock *, union sctp_addr *);
346
347/* Verify this is a valid sockaddr. */
348static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
349 union sctp_addr *addr, int len)
350{
351 struct sctp_af *af;
352
353 /* Check minimum size. */
354 if (len < sizeof (struct sockaddr))
355 return NULL;
356
Xin Longc5006b82018-01-15 17:02:00 +0800357 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
358 return NULL;
359
Vlad Yasevich7dab83d2008-07-18 23:05:40 -0700360 /* V4 mapped address are really of AF_INET family */
361 if (addr->sa.sa_family == AF_INET6 &&
Xin Longc5006b82018-01-15 17:02:00 +0800362 ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
363 !opt->pf->af_supported(AF_INET, opt))
364 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 /* If we get this far, af is valid. */
367 af = sctp_get_af_specific(addr->sa.sa_family);
368
369 if (len < af->sockaddr_len)
370 return NULL;
371
372 return af;
373}
374
375/* Bind a local address either to an endpoint or to an association. */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200376static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Eric W. Biederman35946982012-11-16 03:03:12 +0000378 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct sctp_sock *sp = sctp_sk(sk);
380 struct sctp_endpoint *ep = sp->ep;
381 struct sctp_bind_addr *bp = &ep->base.bind_addr;
382 struct sctp_af *af;
383 unsigned short snum;
384 int ret = 0;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* Common sockaddr verification. */
387 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700388 if (!af) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200389 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
390 __func__, sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700392 }
393
394 snum = ntohs(addr->v4.sin_port);
395
Daniel Borkmannbb333812013-06-28 19:49:40 +0200396 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
397 __func__, sk, &addr->sa, bp->port, snum, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /* PF specific bind() address verification. */
400 if (!sp->pf->bind_verify(sp, addr))
401 return -EADDRNOTAVAIL;
402
Vlad Yasevich8b358052007-05-15 17:14:58 -0400403 /* We must either be unbound, or bind to the same port.
404 * It's OK to allow 0 ports if we are already bound.
405 * We'll just inhert an already bound port in this case
406 */
407 if (bp->port) {
408 if (!snum)
409 snum = bp->port;
410 else if (snum != bp->port) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200411 pr_debug("%s: new port %d doesn't match existing port "
412 "%d\n", __func__, snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400413 return -EINVAL;
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
Krister Johansen4548b682017-01-20 17:49:11 -0800417 if (snum && snum < inet_prot_sock(net) &&
Eric W. Biederman35946982012-11-16 03:03:12 +0000418 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return -EACCES;
420
Vlad Yasevich4e540642008-07-18 23:06:32 -0700421 /* See if the address matches any of the addresses we may have
422 * already bound before checking against other endpoints.
423 */
424 if (sctp_bind_addr_match(bp, addr, sp))
425 return -EINVAL;
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* Make sure we are allowed to bind here.
428 * The function sctp_get_port_local() does duplicate address
429 * detection.
430 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900431 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if ((ret = sctp_get_port_local(sk, addr))) {
Vlad Yasevich4e540642008-07-18 23:06:32 -0700433 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
436 /* Refresh ephemeral port. */
437 if (!bp->port)
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000438 bp->port = inet_sk(sk)->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Vlad Yasevich559cf712007-09-16 16:03:28 -0700440 /* Add the address to the bind address list.
441 * Use GFP_ATOMIC since BHs will be disabled.
442 */
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300443 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
444 SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /* Copy back into socket for getsockname() use. */
447 if (!ret) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000448 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600449 sp->pf->to_sk_saddr(addr, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
452 return ret;
453}
454
455 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
456 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900457 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900459 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900461 * subsequent ASCONF. Note this restriction binds each side, so at any
462 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * from each endpoint).
464 */
465static int sctp_send_asconf(struct sctp_association *asoc,
466 struct sctp_chunk *chunk)
467{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000468 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 int retval = 0;
470
471 /* If there is an outstanding ASCONF chunk, queue it for later
472 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900473 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700475 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900476 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478
479 /* Hold the chunk until an ASCONF_ACK is received. */
480 sctp_chunk_hold(chunk);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000481 retval = sctp_primitive_ASCONF(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (retval)
483 sctp_chunk_free(chunk);
484 else
485 asoc->addip_last_asconf = chunk;
486
487out:
488 return retval;
489}
490
491/* Add a list of addresses as bind addresses to local endpoint or
492 * association.
493 *
494 * Basically run through each address specified in the addrs/addrcnt
495 * array/length pair, determine if it is IPv6 or IPv4 and call
496 * sctp_do_bind() on it.
497 *
498 * If any of them fails, then the operation will be reversed and the
499 * ones that were added will be removed.
500 *
501 * Only sctp_setsockopt_bindx() is supposed to call this function.
502 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200503static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 int cnt;
506 int retval = 0;
507 void *addr_buf;
508 struct sockaddr *sa_addr;
509 struct sctp_af *af;
510
Daniel Borkmannbb333812013-06-28 19:49:40 +0200511 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
512 addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 addr_buf = addrs;
515 for (cnt = 0; cnt < addrcnt; cnt++) {
516 /* The list may contain either IPv4 or IPv6 address;
517 * determine the address length for walking thru the list.
518 */
Joe Perchesea110732011-06-13 16:21:26 +0000519 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 af = sctp_get_af_specific(sa_addr->sa_family);
521 if (!af) {
522 retval = -EINVAL;
523 goto err_bindx_add;
524 }
525
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900526 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 af->sockaddr_len);
528
529 addr_buf += af->sockaddr_len;
530
531err_bindx_add:
532 if (retval < 0) {
533 /* Failed. Cleanup the ones that have been added */
534 if (cnt > 0)
535 sctp_bindx_rem(sk, addrs, cnt);
536 return retval;
537 }
538 }
539
540 return retval;
541}
542
543/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
544 * associations that are part of the endpoint indicating that a list of local
545 * addresses are added to the endpoint.
546 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900547 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 * association, we do not send the chunk for that association. But it will not
549 * affect other associations.
550 *
551 * Only sctp_setsockopt_bindx() is supposed to call this function.
552 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900553static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 struct sockaddr *addrs,
555 int addrcnt)
556{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000557 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct sctp_sock *sp;
559 struct sctp_endpoint *ep;
560 struct sctp_association *asoc;
561 struct sctp_bind_addr *bp;
562 struct sctp_chunk *chunk;
563 struct sctp_sockaddr_entry *laddr;
564 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700565 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 void *addr_buf;
567 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 struct list_head *p;
569 int i;
570 int retval = 0;
571
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000572 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return retval;
574
575 sp = sctp_sk(sk);
576 ep = sp->ep;
577
Daniel Borkmannbb333812013-06-28 19:49:40 +0200578 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
579 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700581 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (!asoc->peer.asconf_capable)
583 continue;
584
585 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
586 continue;
587
588 if (!sctp_state(asoc, ESTABLISHED))
589 continue;
590
591 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900592 * in the bind address list of the association. If so,
593 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * other associations.
595 */
596 addr_buf = addrs;
597 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000598 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 af = sctp_get_af_specific(addr->v4.sin_family);
600 if (!af) {
601 retval = -EINVAL;
602 goto out;
603 }
604
605 if (sctp_assoc_lookup_laddr(asoc, addr))
606 break;
607
608 addr_buf += af->sockaddr_len;
609 }
610 if (i < addrcnt)
611 continue;
612
Vlad Yasevich559cf712007-09-16 16:03:28 -0700613 /* Use the first valid address in bind addr list of
614 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 bp = &asoc->base.bind_addr;
617 p = bp->address_list.next;
618 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800619 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 addrcnt, SCTP_PARAM_ADD_IP);
621 if (!chunk) {
622 retval = -ENOMEM;
623 goto out;
624 }
625
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700626 /* Add the new addresses to the bind address list with
627 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700629 addr_buf = addrs;
630 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000631 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700632 af = sctp_get_af_specific(addr->v4.sin_family);
633 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800634 retval = sctp_add_bind_addr(bp, &saveaddr,
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300635 sizeof(saveaddr),
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800636 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700637 addr_buf += af->sockaddr_len;
638 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900639 if (asoc->src_out_of_asoc_ok) {
640 struct sctp_transport *trans;
641
642 list_for_each_entry(trans,
643 &asoc->peer.transport_addr_list, transports) {
644 /* Clear the source and route cache */
Julian Anastasovc86a7732017-02-06 23:14:13 +0200645 sctp_transport_dst_release(trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900646 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
647 2*asoc->pathmtu, 4380));
648 trans->ssthresh = asoc->peer.i.a_rwnd;
649 trans->rto = asoc->rto_initial;
Michele Baldessari196d6752012-12-01 04:49:42 +0000650 sctp_max_rto(asoc, trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900651 trans->rtt = trans->srtt = trans->rttvar = 0;
652 sctp_transport_route(trans, NULL,
653 sctp_sk(asoc->base.sk));
654 }
655 }
656 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
659out:
660 return retval;
661}
662
663/* Remove a list of addresses from bind addresses list. Do not remove the
664 * last address.
665 *
666 * Basically run through each address specified in the addrs/addrcnt
667 * array/length pair, determine if it is IPv6 or IPv4 and call
668 * sctp_del_bind() on it.
669 *
670 * If any of them fails, then the operation will be reversed and the
671 * ones that were removed will be added back.
672 *
673 * At least one address has to be left; if only one address is
674 * available, the operation will return -EBUSY.
675 *
676 * Only sctp_setsockopt_bindx() is supposed to call this function.
677 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200678static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct sctp_sock *sp = sctp_sk(sk);
681 struct sctp_endpoint *ep = sp->ep;
682 int cnt;
683 struct sctp_bind_addr *bp = &ep->base.bind_addr;
684 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800686 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct sctp_af *af;
688
Daniel Borkmannbb333812013-06-28 19:49:40 +0200689 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
690 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 addr_buf = addrs;
693 for (cnt = 0; cnt < addrcnt; cnt++) {
694 /* If the bind address list is empty or if there is only one
695 * bind address, there is nothing more to be removed (we need
696 * at least one address here).
697 */
698 if (list_empty(&bp->address_list) ||
699 (sctp_list_single_entry(&bp->address_list))) {
700 retval = -EBUSY;
701 goto err_bindx_rem;
702 }
703
Joe Perchesea110732011-06-13 16:21:26 +0000704 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800705 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (!af) {
707 retval = -EINVAL;
708 goto err_bindx_rem;
709 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700710
711 if (!af->addr_valid(sa_addr, sp, NULL)) {
712 retval = -EADDRNOTAVAIL;
713 goto err_bindx_rem;
714 }
715
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000716 if (sa_addr->v4.sin_port &&
717 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 retval = -EINVAL;
719 goto err_bindx_rem;
720 }
721
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000722 if (!sa_addr->v4.sin_port)
723 sa_addr->v4.sin_port = htons(bp->port);
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /* FIXME - There is probably a need to check if sk->sk_saddr and
726 * sk->sk_rcv_addr are currently set to one of the addresses to
727 * be removed. This is something which needs to be looked into
728 * when we are fixing the outstanding issues with multi-homing
729 * socket routing and failover schemes. Refer to comments in
730 * sctp_do_bind(). -daisy
731 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400732 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 addr_buf += af->sockaddr_len;
735err_bindx_rem:
736 if (retval < 0) {
737 /* Failed. Add the ones that has been removed back */
738 if (cnt > 0)
739 sctp_bindx_add(sk, addrs, cnt);
740 return retval;
741 }
742 }
743
744 return retval;
745}
746
747/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
748 * the associations that are part of the endpoint indicating that a list of
749 * local addresses are removed from the endpoint.
750 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900751 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * association, we do not send the chunk for that association. But it will not
753 * affect other associations.
754 *
755 * Only sctp_setsockopt_bindx() is supposed to call this function.
756 */
757static int sctp_send_asconf_del_ip(struct sock *sk,
758 struct sockaddr *addrs,
759 int addrcnt)
760{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000761 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct sctp_sock *sp;
763 struct sctp_endpoint *ep;
764 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700765 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct sctp_bind_addr *bp;
767 struct sctp_chunk *chunk;
768 union sctp_addr *laddr;
769 void *addr_buf;
770 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700771 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 int i;
773 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900774 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Michio Honda8a07eb02011-04-26 20:19:36 +0900776 chunk = NULL;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000777 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return retval;
779
780 sp = sctp_sk(sk);
781 ep = sp->ep;
782
Daniel Borkmannbb333812013-06-28 19:49:40 +0200783 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
784 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700786 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 if (!asoc->peer.asconf_capable)
789 continue;
790
791 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
792 continue;
793
794 if (!sctp_state(asoc, ESTABLISHED))
795 continue;
796
797 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900798 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 * If so, do not send the asconf chunk to its peer, but
800 * continue with other associations.
801 */
802 addr_buf = addrs;
803 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000804 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 af = sctp_get_af_specific(laddr->v4.sin_family);
806 if (!af) {
807 retval = -EINVAL;
808 goto out;
809 }
810
811 if (!sctp_assoc_lookup_laddr(asoc, laddr))
812 break;
813
814 addr_buf += af->sockaddr_len;
815 }
816 if (i < addrcnt)
817 continue;
818
819 /* Find one address in the association's bind address list
820 * that is not in the packed array of addresses. This is to
821 * make sure that we do not delete all the addresses in the
822 * association.
823 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 bp = &asoc->base.bind_addr;
825 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
826 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900827 if ((laddr == NULL) && (addrcnt == 1)) {
828 if (asoc->asconf_addr_del_pending)
829 continue;
830 asoc->asconf_addr_del_pending =
831 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900832 if (asoc->asconf_addr_del_pending == NULL) {
833 retval = -ENOMEM;
834 goto out;
835 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900836 asoc->asconf_addr_del_pending->sa.sa_family =
837 addrs->sa_family;
838 asoc->asconf_addr_del_pending->v4.sin_port =
839 htons(bp->port);
840 if (addrs->sa_family == AF_INET) {
841 struct sockaddr_in *sin;
842
843 sin = (struct sockaddr_in *)addrs;
844 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
845 } else if (addrs->sa_family == AF_INET6) {
846 struct sockaddr_in6 *sin6;
847
848 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000849 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900850 }
Daniel Borkmannbb333812013-06-28 19:49:40 +0200851
852 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
853 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
854 asoc->asconf_addr_del_pending);
855
Michio Honda8a07eb02011-04-26 20:19:36 +0900856 asoc->src_out_of_asoc_ok = 1;
857 stored = 1;
858 goto skip_mkasconf;
859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Daniel Borkmann88362ad2013-09-07 20:51:21 +0200861 if (laddr == NULL)
862 return -EINVAL;
863
Vlad Yasevich559cf712007-09-16 16:03:28 -0700864 /* We do not need RCU protection throughout this loop
865 * because this is done under a socket lock from the
866 * setsockopt call.
867 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
869 SCTP_PARAM_DEL_IP);
870 if (!chunk) {
871 retval = -ENOMEM;
872 goto out;
873 }
874
Michio Honda8a07eb02011-04-26 20:19:36 +0900875skip_mkasconf:
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700876 /* Reset use_as_src flag for the addresses in the bind address
877 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700879 addr_buf = addrs;
880 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000881 laddr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700882 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700883 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a12006-11-20 17:05:23 -0800884 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800885 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700886 }
887 addr_buf += af->sockaddr_len;
888 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700889
890 /* Update the route and saddr entries for all the transports
891 * as some of the addresses in the bind address list are
892 * about to be deleted and cannot be used as source addresses.
893 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700894 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
895 transports) {
Julian Anastasovc86a7732017-02-06 23:14:13 +0200896 sctp_transport_dst_release(transport);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700897 sctp_transport_route(transport, NULL,
898 sctp_sk(asoc->base.sk));
899 }
900
Michio Honda8a07eb02011-04-26 20:19:36 +0900901 if (stored)
902 /* We don't need to transmit ASCONF */
903 continue;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700904 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906out:
907 return retval;
908}
909
Michio Honda9f7d6532011-04-26 19:32:51 +0900910/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
911int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
912{
913 struct sock *sk = sctp_opt2sk(sp);
914 union sctp_addr *addr;
915 struct sctp_af *af;
916
917 /* It is safe to write port space in caller. */
918 addr = &addrw->a;
919 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
920 af = sctp_get_af_specific(addr->sa.sa_family);
921 if (!af)
922 return -EINVAL;
923 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
924 return -EINVAL;
925
926 if (addrw->state == SCTP_ADDR_NEW)
927 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
928 else
929 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
933 *
934 * API 8.1
935 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
936 * int flags);
937 *
938 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
939 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
940 * or IPv6 addresses.
941 *
942 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
943 * Section 3.1.2 for this usage.
944 *
945 * addrs is a pointer to an array of one or more socket addresses. Each
946 * address is contained in its appropriate structure (i.e. struct
947 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700948 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * representation is termed a "packed array" of addresses). The caller
950 * specifies the number of addresses in the array with addrcnt.
951 *
952 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
953 * -1, and sets errno to the appropriate error code.
954 *
955 * For SCTP, the port given in each socket address must be the same, or
956 * sctp_bindx() will fail, setting errno to EINVAL.
957 *
958 * The flags parameter is formed from the bitwise OR of zero or more of
959 * the following currently defined flags:
960 *
961 * SCTP_BINDX_ADD_ADDR
962 *
963 * SCTP_BINDX_REM_ADDR
964 *
965 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
966 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
967 * addresses from the association. The two flags are mutually exclusive;
968 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
969 * not remove all addresses from an association; sctp_bindx() will
970 * reject such an attempt with EINVAL.
971 *
972 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
973 * additional addresses with an endpoint after calling bind(). Or use
974 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
975 * socket is associated with so that no new association accepted will be
976 * associated with those addresses. If the endpoint supports dynamic
977 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
978 * endpoint to send the appropriate message to the peer to change the
979 * peers address lists.
980 *
981 * Adding and removing addresses from a connected association is
982 * optional functionality. Implementations that do not support this
983 * functionality should return EOPNOTSUPP.
984 *
985 * Basically do nothing but copying the addresses from user to kernel
986 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700987 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
988 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
991 * it.
992 *
993 * sk The sk of the socket
994 * addrs The pointer to the addresses in user land
995 * addrssize Size of the addrs buffer
996 * op Operation to perform (add or remove, see the flags of
997 * sctp_bindx)
998 *
999 * Returns 0 if ok, <0 errno code on error.
1000 */
wangweidong26ac8e52013-12-23 12:16:51 +08001001static int sctp_setsockopt_bindx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001002 struct sockaddr __user *addrs,
1003 int addrs_size, int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 struct sockaddr *kaddrs;
1006 int err;
1007 int addrcnt = 0;
1008 int walk_size = 0;
1009 struct sockaddr *sa_addr;
1010 void *addr_buf;
1011 struct sctp_af *af;
1012
Daniel Borkmannbb333812013-06-28 19:49:40 +02001013 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1014 __func__, sk, addrs, addrs_size, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 if (unlikely(addrs_size <= 0))
1017 return -EINVAL;
1018
Al Viroc981f252018-01-07 13:19:09 -05001019 kaddrs = vmemdup_user(addrs, addrs_size);
1020 if (unlikely(IS_ERR(kaddrs)))
1021 return PTR_ERR(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001023 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 addr_buf = kaddrs;
1025 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001026 if (walk_size + sizeof(sa_family_t) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001027 kvfree(kaddrs);
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001028 return -EINVAL;
1029 }
1030
Joe Perchesea110732011-06-13 16:21:26 +00001031 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 af = sctp_get_af_specific(sa_addr->sa_family);
1033
1034 /* If the address family is not supported or if this address
1035 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001036 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001038 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return -EINVAL;
1040 }
1041 addrcnt++;
1042 addr_buf += af->sockaddr_len;
1043 walk_size += af->sockaddr_len;
1044 }
1045
1046 /* Do the work. */
1047 switch (op) {
1048 case SCTP_BINDX_ADD_ADDR:
Richard Haines2277c7c2018-02-13 20:56:24 +00001049 /* Allow security module to validate bindx addresses. */
1050 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1051 (struct sockaddr *)kaddrs,
1052 addrs_size);
1053 if (err)
1054 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1056 if (err)
1057 goto out;
1058 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1059 break;
1060
1061 case SCTP_BINDX_REM_ADDR:
1062 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1063 if (err)
1064 goto out;
1065 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1066 break;
1067
1068 default:
1069 err = -EINVAL;
1070 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073out:
Al Viroc981f252018-01-07 13:19:09 -05001074 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 return err;
1077}
1078
Frank Filz3f7a87d2005-06-20 13:14:57 -07001079/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1080 *
1081 * Common routine for handling connect() and sctp_connectx().
1082 * Connect will come in with just a single address.
1083 */
wangweidong26ac8e52013-12-23 12:16:51 +08001084static int __sctp_connect(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001085 struct sockaddr *kaddrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001086 int addrs_size,
1087 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001088{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001089 struct net *net = sock_net(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001090 struct sctp_sock *sp;
1091 struct sctp_endpoint *ep;
1092 struct sctp_association *asoc = NULL;
1093 struct sctp_association *asoc2;
1094 struct sctp_transport *transport;
1095 union sctp_addr to;
Xin Long1c662012017-08-05 19:59:54 +08001096 enum sctp_scope scope;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001097 long timeo;
1098 int err = 0;
1099 int addrcnt = 0;
1100 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001101 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001102 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001103 unsigned short port;
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001104 unsigned int f_flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001105
1106 sp = sctp_sk(sk);
1107 ep = sp->ep;
1108
1109 /* connect() cannot be done on a socket that is already in ESTABLISHED
1110 * state - UDP-style peeled off socket or a TCP-style socket that
1111 * is already connected.
1112 * It cannot be done even on a TCP-style listening socket.
1113 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03001114 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
Frank Filz3f7a87d2005-06-20 13:14:57 -07001115 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1116 err = -EISCONN;
1117 goto out_free;
1118 }
1119
1120 /* Walk through the addrs buffer and count the number of addresses. */
1121 addr_buf = kaddrs;
1122 while (walk_size < addrs_size) {
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001123 struct sctp_af *af;
1124
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001125 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1126 err = -EINVAL;
1127 goto out_free;
1128 }
1129
Joe Perchesea110732011-06-13 16:21:26 +00001130 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001131 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001132
1133 /* If the address family is not supported or if this address
1134 * causes the address buffer to overflow return EINVAL.
1135 */
1136 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1137 err = -EINVAL;
1138 goto out_free;
1139 }
1140
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001141 port = ntohs(sa_addr->v4.sin_port);
1142
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001143 /* Save current address so we can work with it */
1144 memcpy(&to, sa_addr, af->sockaddr_len);
1145
1146 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001147 if (err)
1148 goto out_free;
1149
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001150 /* Make sure the destination port is correctly set
1151 * in all addresses.
1152 */
Wei Yongjun524fba62013-04-03 03:02:28 +00001153 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1154 err = -EINVAL;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001155 goto out_free;
Wei Yongjun524fba62013-04-03 03:02:28 +00001156 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001157
1158 /* Check if there already is a matching association on the
1159 * endpoint (other than the one created here).
1160 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001161 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001162 if (asoc2 && asoc2 != asoc) {
1163 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1164 err = -EISCONN;
1165 else
1166 err = -EALREADY;
1167 goto out_free;
1168 }
1169
1170 /* If we could not find a matching association on the endpoint,
1171 * make sure that there is no peeled-off association matching
1172 * the peer address even on another socket.
1173 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001174 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001175 err = -EADDRNOTAVAIL;
1176 goto out_free;
1177 }
1178
1179 if (!asoc) {
1180 /* If a bind() or sctp_bindx() is not called prior to
1181 * an sctp_connectx() call, the system picks an
1182 * ephemeral port and will choose an address set
1183 * equivalent to binding with a wildcard address.
1184 */
1185 if (!ep->base.bind_addr.port) {
1186 if (sctp_autobind(sk)) {
1187 err = -EAGAIN;
1188 goto out_free;
1189 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001190 } else {
1191 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001192 * If an unprivileged user inherits a 1-many
1193 * style socket with open associations on a
1194 * privileged port, it MAY be permitted to
1195 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001196 * be permitted to open new associations.
1197 */
Krister Johansen4548b682017-01-20 17:49:11 -08001198 if (ep->base.bind_addr.port <
1199 inet_prot_sock(net) &&
1200 !ns_capable(net->user_ns,
1201 CAP_NET_BIND_SERVICE)) {
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001202 err = -EACCES;
1203 goto out_free;
1204 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001205 }
1206
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001207 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001208 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1209 if (!asoc) {
1210 err = -ENOMEM;
1211 goto out_free;
1212 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001213
1214 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1215 GFP_KERNEL);
1216 if (err < 0) {
1217 goto out_free;
1218 }
1219
Frank Filz3f7a87d2005-06-20 13:14:57 -07001220 }
1221
1222 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001223 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001224 SCTP_UNKNOWN);
1225 if (!transport) {
1226 err = -ENOMEM;
1227 goto out_free;
1228 }
1229
1230 addrcnt++;
1231 addr_buf += af->sockaddr_len;
1232 walk_size += af->sockaddr_len;
1233 }
1234
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001235 /* In case the user of sctp_connectx() wants an association
1236 * id back, assign one now.
1237 */
1238 if (assoc_id) {
1239 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1240 if (err < 0)
1241 goto out_free;
1242 }
1243
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001244 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001245 if (err < 0) {
1246 goto out_free;
1247 }
1248
1249 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001250 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001251 sp->pf->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001252 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001253
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001254 /* in-kernel sockets don't generally have a file allocated to them
1255 * if all they do is call sock_create_kern().
1256 */
1257 if (sk->sk_socket->file)
1258 f_flags = sk->sk_socket->file->f_flags;
1259
1260 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1261
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001262 if (assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001263 *assoc_id = asoc->assoc_id;
Richard Haines2277c7c2018-02-13 20:56:24 +00001264
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001265 err = sctp_wait_for_connect(asoc, &timeo);
1266 /* Note: the asoc may be freed after the return of
1267 * sctp_wait_for_connect.
1268 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001269
1270 /* Don't free association on exit. */
1271 asoc = NULL;
1272
1273out_free:
Daniel Borkmannbb333812013-06-28 19:49:40 +02001274 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1275 __func__, asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001276
Neil Horman2eebc1e2012-07-16 09:13:51 +00001277 if (asoc) {
1278 /* sctp_primitive_ASSOCIATE may have added this association
1279 * To the hash table, try to unhash it, just in case, its a noop
1280 * if it wasn't hashed so we're safe
1281 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001282 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001283 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001284 return err;
1285}
1286
1287/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1288 *
1289 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001290 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1291 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001292 *
1293 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1294 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1295 * or IPv6 addresses.
1296 *
1297 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1298 * Section 3.1.2 for this usage.
1299 *
1300 * addrs is a pointer to an array of one or more socket addresses. Each
1301 * address is contained in its appropriate structure (i.e. struct
1302 * sockaddr_in or struct sockaddr_in6) the family of the address type
1303 * must be used to distengish the address length (note that this
1304 * representation is termed a "packed array" of addresses). The caller
1305 * specifies the number of addresses in the array with addrcnt.
1306 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001307 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1308 * the association id of the new association. On failure, sctp_connectx()
1309 * returns -1, and sets errno to the appropriate error code. The assoc_id
1310 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001311 *
1312 * For SCTP, the port given in each socket address must be the same, or
1313 * sctp_connectx() will fail, setting errno to EINVAL.
1314 *
1315 * An application can use sctp_connectx to initiate an association with
1316 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1317 * allows a caller to specify multiple addresses at which a peer can be
1318 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001319 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001320 * specifies that the stack will try to make use of all the addresses in
1321 * the list when needed.
1322 *
1323 * Note that the list of addresses passed in is only used for setting up
1324 * the association. It does not necessarily equal the set of addresses
1325 * the peer uses for the resulting association. If the caller wants to
1326 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1327 * retrieve them after the association has been set up.
1328 *
1329 * Basically do nothing but copying the addresses from user to kernel
1330 * land and invoking either sctp_connectx(). This is used for tunneling
1331 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1332 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07001333 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1334 * it.
1335 *
1336 * sk The sk of the socket
1337 * addrs The pointer to the addresses in user land
1338 * addrssize Size of the addrs buffer
1339 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001340 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001341 */
wangweidong26ac8e52013-12-23 12:16:51 +08001342static int __sctp_setsockopt_connectx(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001343 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001344 int addrs_size,
1345 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001346{
Frank Filz3f7a87d2005-06-20 13:14:57 -07001347 struct sockaddr *kaddrs;
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02001348 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001349
Daniel Borkmannbb333812013-06-28 19:49:40 +02001350 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1351 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001352
1353 if (unlikely(addrs_size <= 0))
1354 return -EINVAL;
1355
Al Viroc981f252018-01-07 13:19:09 -05001356 kaddrs = vmemdup_user(addrs, addrs_size);
1357 if (unlikely(IS_ERR(kaddrs)))
1358 return PTR_ERR(kaddrs);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001359
Richard Haines2277c7c2018-02-13 20:56:24 +00001360 /* Allow security module to validate connectx addresses. */
1361 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1362 (struct sockaddr *)kaddrs,
1363 addrs_size);
1364 if (err)
1365 goto out_free;
1366
Al Viroc981f252018-01-07 13:19:09 -05001367 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
Richard Haines2277c7c2018-02-13 20:56:24 +00001368
1369out_free:
Al Viroc981f252018-01-07 13:19:09 -05001370 kvfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001371
Frank Filz3f7a87d2005-06-20 13:14:57 -07001372 return err;
1373}
1374
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001375/*
1376 * This is an older interface. It's kept for backward compatibility
1377 * to the option that doesn't provide association id.
1378 */
wangweidong26ac8e52013-12-23 12:16:51 +08001379static int sctp_setsockopt_connectx_old(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001380 struct sockaddr __user *addrs,
1381 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001382{
1383 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1384}
1385
1386/*
1387 * New interface for the API. The since the API is done with a socket
1388 * option, to make it simple we feed back the association id is as a return
1389 * indication to the call. Error is always negative and association id is
1390 * always positive.
1391 */
wangweidong26ac8e52013-12-23 12:16:51 +08001392static int sctp_setsockopt_connectx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001393 struct sockaddr __user *addrs,
1394 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001395{
1396 sctp_assoc_t assoc_id = 0;
1397 int err = 0;
1398
1399 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1400
1401 if (err)
1402 return err;
1403 else
1404 return assoc_id;
1405}
1406
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001407/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001408 * New (hopefully final) interface for the API.
1409 * We use the sctp_getaddrs_old structure so that use-space library
Daniel Borkmannffd59392014-02-17 12:11:11 +01001410 * can avoid any unnecessary allocations. The only different part
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001411 * is that we store the actual length of the address buffer into the
Daniel Borkmannffd59392014-02-17 12:11:11 +01001412 * addrs_num structure member. That way we can re-use the existing
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001413 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001414 */
Daniel Borkmannffd59392014-02-17 12:11:11 +01001415#ifdef CONFIG_COMPAT
1416struct compat_sctp_getaddrs_old {
1417 sctp_assoc_t assoc_id;
1418 s32 addr_num;
1419 compat_uptr_t addrs; /* struct sockaddr * */
1420};
1421#endif
1422
wangweidong26ac8e52013-12-23 12:16:51 +08001423static int sctp_getsockopt_connectx3(struct sock *sk, int len,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001424 char __user *optval,
1425 int __user *optlen)
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001426{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001427 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001428 sctp_assoc_t assoc_id = 0;
1429 int err = 0;
1430
Daniel Borkmannffd59392014-02-17 12:11:11 +01001431#ifdef CONFIG_COMPAT
Andy Lutomirski96c0e0a2016-03-22 14:25:07 -07001432 if (in_compat_syscall()) {
Daniel Borkmannffd59392014-02-17 12:11:11 +01001433 struct compat_sctp_getaddrs_old param32;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001434
Daniel Borkmannffd59392014-02-17 12:11:11 +01001435 if (len < sizeof(param32))
1436 return -EINVAL;
1437 if (copy_from_user(&param32, optval, sizeof(param32)))
1438 return -EFAULT;
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001439
Daniel Borkmannffd59392014-02-17 12:11:11 +01001440 param.assoc_id = param32.assoc_id;
1441 param.addr_num = param32.addr_num;
1442 param.addrs = compat_ptr(param32.addrs);
1443 } else
1444#endif
1445 {
1446 if (len < sizeof(param))
1447 return -EINVAL;
1448 if (copy_from_user(&param, optval, sizeof(param)))
1449 return -EFAULT;
1450 }
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001451
Daniel Borkmannffd59392014-02-17 12:11:11 +01001452 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1453 param.addrs, param.addr_num,
1454 &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001455 if (err == 0 || err == -EINPROGRESS) {
1456 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1457 return -EFAULT;
1458 if (put_user(sizeof(assoc_id), optlen))
1459 return -EFAULT;
1460 }
1461
1462 return err;
1463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465/* API 3.1.4 close() - UDP Style Syntax
1466 * Applications use close() to perform graceful shutdown (as described in
1467 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1468 * by a UDP-style socket.
1469 *
1470 * The syntax is
1471 *
1472 * ret = close(int sd);
1473 *
1474 * sd - the socket descriptor of the associations to be closed.
1475 *
1476 * To gracefully shutdown a specific association represented by the
1477 * UDP-style socket, an application should use the sendmsg() call,
1478 * passing no user data, but including the appropriate flag in the
1479 * ancillary data (see Section xxxx).
1480 *
1481 * If sd in the close() call is a branched-off socket representing only
1482 * one association, the shutdown is performed on that association only.
1483 *
1484 * 4.1.6 close() - TCP Style Syntax
1485 *
1486 * Applications use close() to gracefully close down an association.
1487 *
1488 * The syntax is:
1489 *
1490 * int close(int sd);
1491 *
1492 * sd - the socket descriptor of the association to be closed.
1493 *
1494 * After an application calls close() on a socket descriptor, no further
1495 * socket operations will succeed on that descriptor.
1496 *
1497 * API 7.1.4 SO_LINGER
1498 *
1499 * An application using the TCP-style socket can use this option to
1500 * perform the SCTP ABORT primitive. The linger option structure is:
1501 *
1502 * struct linger {
1503 * int l_onoff; // option on/off
1504 * int l_linger; // linger time
1505 * };
1506 *
1507 * To enable the option, set l_onoff to 1. If the l_linger value is set
1508 * to 0, calling close() is the same as the ABORT primitive. If the
1509 * value is set to a negative value, the setsockopt() call will return
1510 * an error. If the value is set to a positive value linger_time, the
1511 * close() can be blocked for at most linger_time ms. If the graceful
1512 * shutdown phase does not finish during this period, close() will
1513 * return but the graceful shutdown phase continues in the system.
1514 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02001515static void sctp_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001517 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 struct sctp_endpoint *ep;
1519 struct sctp_association *asoc;
1520 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001521 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Daniel Borkmannbb333812013-06-28 19:49:40 +02001523 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Xin Long6dfe4b92017-06-10 14:56:56 +08001525 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 sk->sk_shutdown = SHUTDOWN_MASK;
Yafang Shaocbabf462017-12-20 11:12:54 +08001527 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 ep = sctp_sk(sk)->ep;
1530
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001531 /* Clean up any skbs sitting on the receive queue. */
1532 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1533 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1534
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001535 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 list_for_each_safe(pos, temp, &ep->asocs) {
1537 asoc = list_entry(pos, struct sctp_association, asocs);
1538
1539 if (sctp_style(sk, TCP)) {
1540 /* A closed association can still be in the list if
1541 * it belongs to a TCP-style listening socket that is
1542 * not yet accepted. If so, free it. If not, send an
1543 * ABORT or SHUTDOWN based on the linger options.
1544 */
1545 if (sctp_state(asoc, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001547 continue;
1548 }
1549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001551 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1552 !skb_queue_empty(&asoc->ulpq.reasm) ||
Xin Long13228232017-12-08 21:04:09 +08001553 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001554 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001555 struct sctp_chunk *chunk;
1556
1557 chunk = sctp_make_abort_user(asoc, NULL, 0);
Xin Long068d8bd2015-12-29 17:49:25 +08001558 sctp_primitive_ABORT(net, asoc, chunk);
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001559 } else
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001560 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 /* On a TCP-style socket, block for at most linger_time if set. */
1564 if (sctp_style(sk, TCP) && timeout)
1565 sctp_wait_for_close(sk, timeout);
1566
1567 /* This will run the backlog queue. */
wangweidong048ed4b2014-01-21 15:44:11 +08001568 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 /* Supposedly, no process has access to the socket, but
1571 * the net layers still may.
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001572 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1573 * held and that should be grabbed before socket lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001575 spin_lock_bh(&net->sctp.addr_wq_lock);
Xin Long6dfe4b92017-06-10 14:56:56 +08001576 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 /* Hold the sock, since sk_common_release() will put sock_put()
1579 * and we have just a little more cleanup.
1580 */
1581 sock_hold(sk);
1582 sk_common_release(sk);
1583
wangweidong5bc1d1b2014-01-21 15:44:12 +08001584 bh_unlock_sock(sk);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001585 spin_unlock_bh(&net->sctp.addr_wq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 sock_put(sk);
1588
1589 SCTP_DBG_OBJCNT_DEC(sock);
1590}
1591
1592/* Handle EPIPE error. */
1593static int sctp_error(struct sock *sk, int flags, int err)
1594{
1595 if (err == -EPIPE)
1596 err = sock_error(sk) ? : -EPIPE;
1597 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1598 send_sig(SIGPIPE, current, 0);
1599 return err;
1600}
1601
1602/* API 3.1.3 sendmsg() - UDP Style Syntax
1603 *
1604 * An application uses sendmsg() and recvmsg() calls to transmit data to
1605 * and receive data from its peer.
1606 *
1607 * ssize_t sendmsg(int socket, const struct msghdr *message,
1608 * int flags);
1609 *
1610 * socket - the socket descriptor of the endpoint.
1611 * message - pointer to the msghdr structure which contains a single
1612 * user message and possibly some ancillary data.
1613 *
1614 * See Section 5 for complete description of the data
1615 * structures.
1616 *
1617 * flags - flags sent or received with the user message, see Section
1618 * 5 for complete description of the flags.
1619 *
1620 * Note: This function could use a rewrite especially when explicit
1621 * connect support comes in.
1622 */
1623/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1624
Xin Longa05437a2017-08-11 10:23:48 +08001625static int sctp_msghdr_parse(const struct msghdr *msg,
1626 struct sctp_cmsgs *cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Xin Long204f8172018-03-01 23:05:14 +08001628static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1629 struct sctp_sndrcvinfo *srinfo,
1630 const struct msghdr *msg, size_t msg_len)
1631{
1632 __u16 sflags;
1633 int err;
1634
1635 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1636 return -EPIPE;
1637
1638 if (msg_len > sk->sk_sndbuf)
1639 return -EMSGSIZE;
1640
1641 memset(cmsgs, 0, sizeof(*cmsgs));
1642 err = sctp_msghdr_parse(msg, cmsgs);
1643 if (err) {
1644 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1645 return err;
1646 }
1647
1648 memset(srinfo, 0, sizeof(*srinfo));
1649 if (cmsgs->srinfo) {
1650 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1651 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1652 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1653 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1654 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1655 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1656 }
1657
1658 if (cmsgs->sinfo) {
1659 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1660 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1661 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1662 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1663 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1664 }
1665
Xin Longed63afb2018-03-05 20:44:18 +08001666 if (cmsgs->prinfo) {
1667 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1668 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1669 cmsgs->prinfo->pr_policy);
1670 }
1671
Xin Long204f8172018-03-01 23:05:14 +08001672 sflags = srinfo->sinfo_flags;
1673 if (!sflags && msg_len)
1674 return 0;
1675
1676 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1677 return -EINVAL;
1678
1679 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1680 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1681 return -EINVAL;
1682
1683 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1684 return -EINVAL;
1685
1686 return 0;
1687}
1688
Xin Long2bfd80f2018-03-01 23:05:11 +08001689static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1690 struct sctp_cmsgs *cmsgs,
1691 union sctp_addr *daddr,
1692 struct sctp_transport **tp)
1693{
1694 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1695 struct net *net = sock_net(sk);
1696 struct sctp_association *asoc;
1697 enum sctp_scope scope;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001698 struct cmsghdr *cmsg;
Linus Torvalds9eda2d22018-04-06 15:39:26 -07001699 struct sctp_af *af;
Wei Yongjund98985d2018-03-13 03:03:30 +00001700 int err;
Xin Long2bfd80f2018-03-01 23:05:11 +08001701
1702 *tp = NULL;
1703
1704 if (sflags & (SCTP_EOF | SCTP_ABORT))
1705 return -EINVAL;
1706
1707 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1708 sctp_sstate(sk, CLOSING)))
1709 return -EADDRNOTAVAIL;
1710
1711 if (sctp_endpoint_is_peeled_off(ep, daddr))
1712 return -EADDRNOTAVAIL;
1713
1714 if (!ep->base.bind_addr.port) {
1715 if (sctp_autobind(sk))
1716 return -EAGAIN;
1717 } else {
1718 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1719 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1720 return -EACCES;
1721 }
1722
1723 scope = sctp_scope(daddr);
1724
Linus Torvalds9eda2d22018-04-06 15:39:26 -07001725 /* Label connection socket for first association 1-to-many
1726 * style for client sequence socket()->sendmsg(). This
1727 * needs to be done before sctp_assoc_add_peer() as that will
1728 * set up the initial packet that needs to account for any
1729 * security ip options (CIPSO/CALIPSO) added to the packet.
1730 */
1731 af = sctp_get_af_specific(daddr->sa.sa_family);
1732 if (!af)
1733 return -EINVAL;
1734 err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1735 (struct sockaddr *)daddr,
1736 af->sockaddr_len);
1737 if (err < 0)
1738 return err;
1739
Xin Long2bfd80f2018-03-01 23:05:11 +08001740 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1741 if (!asoc)
1742 return -ENOMEM;
1743
1744 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1745 err = -ENOMEM;
1746 goto free;
1747 }
1748
1749 if (cmsgs->init) {
1750 struct sctp_initmsg *init = cmsgs->init;
1751
1752 if (init->sinit_num_ostreams) {
1753 __u16 outcnt = init->sinit_num_ostreams;
1754
1755 asoc->c.sinit_num_ostreams = outcnt;
1756 /* outcnt has been changed, need to re-init stream */
1757 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1758 GFP_KERNEL);
1759 if (err)
1760 goto free;
1761 }
1762
1763 if (init->sinit_max_instreams)
1764 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1765
1766 if (init->sinit_max_attempts)
1767 asoc->max_init_attempts = init->sinit_max_attempts;
1768
1769 if (init->sinit_max_init_timeo)
1770 asoc->max_init_timeo =
1771 msecs_to_jiffies(init->sinit_max_init_timeo);
1772 }
1773
1774 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1775 if (!*tp) {
1776 err = -ENOMEM;
1777 goto free;
1778 }
1779
Xin Long2c0dbaa2018-03-05 20:44:19 +08001780 if (!cmsgs->addrs_msg)
1781 return 0;
1782
1783 /* sendv addr list parse */
1784 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1785 struct sctp_transport *transport;
1786 struct sctp_association *old;
1787 union sctp_addr _daddr;
1788 int dlen;
1789
1790 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1791 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1792 cmsg->cmsg_type != SCTP_DSTADDRV6))
1793 continue;
1794
1795 daddr = &_daddr;
1796 memset(daddr, 0, sizeof(*daddr));
1797 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1798 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
Wei Yongjund98985d2018-03-13 03:03:30 +00001799 if (dlen < sizeof(struct in_addr)) {
1800 err = -EINVAL;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001801 goto free;
Wei Yongjund98985d2018-03-13 03:03:30 +00001802 }
Xin Long2c0dbaa2018-03-05 20:44:19 +08001803
1804 dlen = sizeof(struct in_addr);
1805 daddr->v4.sin_family = AF_INET;
1806 daddr->v4.sin_port = htons(asoc->peer.port);
1807 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1808 } else {
Wei Yongjund98985d2018-03-13 03:03:30 +00001809 if (dlen < sizeof(struct in6_addr)) {
1810 err = -EINVAL;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001811 goto free;
Wei Yongjund98985d2018-03-13 03:03:30 +00001812 }
Xin Long2c0dbaa2018-03-05 20:44:19 +08001813
1814 dlen = sizeof(struct in6_addr);
1815 daddr->v6.sin6_family = AF_INET6;
1816 daddr->v6.sin6_port = htons(asoc->peer.port);
1817 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1818 }
1819 err = sctp_verify_addr(sk, daddr, sizeof(*daddr));
1820 if (err)
1821 goto free;
1822
1823 old = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1824 if (old && old != asoc) {
1825 if (old->state >= SCTP_STATE_ESTABLISHED)
1826 err = -EISCONN;
1827 else
1828 err = -EALREADY;
1829 goto free;
1830 }
1831
1832 if (sctp_endpoint_is_peeled_off(ep, daddr)) {
1833 err = -EADDRNOTAVAIL;
1834 goto free;
1835 }
1836
1837 transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL,
1838 SCTP_UNKNOWN);
1839 if (!transport) {
1840 err = -ENOMEM;
1841 goto free;
1842 }
1843 }
1844
Xin Long2bfd80f2018-03-01 23:05:11 +08001845 return 0;
1846
1847free:
1848 sctp_association_free(asoc);
1849 return err;
1850}
1851
Xin Longc2666de2018-03-01 23:05:12 +08001852static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1853 __u16 sflags, struct msghdr *msg,
1854 size_t msg_len)
1855{
1856 struct sock *sk = asoc->base.sk;
1857 struct net *net = sock_net(sk);
1858
1859 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1860 return -EPIPE;
1861
Xin Long49102802018-03-05 20:44:20 +08001862 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1863 !sctp_state(asoc, ESTABLISHED))
1864 return 0;
1865
Xin Longc2666de2018-03-01 23:05:12 +08001866 if (sflags & SCTP_EOF) {
1867 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1868 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1869
1870 return 0;
1871 }
1872
1873 if (sflags & SCTP_ABORT) {
1874 struct sctp_chunk *chunk;
1875
1876 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1877 if (!chunk)
1878 return -ENOMEM;
1879
1880 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1881 sctp_primitive_ABORT(net, asoc, chunk);
1882
1883 return 0;
1884 }
1885
1886 return 1;
1887}
1888
Xin Longf84af332018-03-01 23:05:10 +08001889static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1890 struct msghdr *msg, size_t msg_len,
1891 struct sctp_transport *transport,
1892 struct sctp_sndrcvinfo *sinfo)
1893{
1894 struct sock *sk = asoc->base.sk;
1895 struct net *net = sock_net(sk);
1896 struct sctp_datamsg *datamsg;
1897 bool wait_connect = false;
1898 struct sctp_chunk *chunk;
1899 long timeo;
1900 int err;
1901
1902 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1903 err = -EINVAL;
1904 goto err;
1905 }
1906
1907 if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) {
1908 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1909 if (err)
1910 goto err;
1911 }
1912
1913 if (sctp_sk(sk)->disable_fragments && msg_len > asoc->frag_point) {
1914 err = -EMSGSIZE;
1915 goto err;
1916 }
1917
Neil Horman0aee4c22018-03-12 14:15:25 -04001918 if (asoc->pmtu_pending)
1919 sctp_assoc_pending_pmtu(asoc);
1920
1921 if (sctp_wspace(asoc) < msg_len)
1922 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1923
1924 if (!sctp_wspace(asoc)) {
1925 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1926 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1927 if (err)
1928 goto err;
1929 }
1930
Xin Longf84af332018-03-01 23:05:10 +08001931 if (sctp_state(asoc, CLOSED)) {
1932 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1933 if (err)
1934 goto err;
1935
1936 if (sctp_sk(sk)->strm_interleave) {
1937 timeo = sock_sndtimeo(sk, 0);
1938 err = sctp_wait_for_connect(asoc, &timeo);
1939 if (err)
1940 goto err;
1941 } else {
1942 wait_connect = true;
1943 }
1944
1945 pr_debug("%s: we associated primitively\n", __func__);
1946 }
1947
Xin Longf84af332018-03-01 23:05:10 +08001948 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1949 if (IS_ERR(datamsg)) {
1950 err = PTR_ERR(datamsg);
1951 goto err;
1952 }
1953
1954 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1955
1956 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1957 sctp_chunk_hold(chunk);
1958 sctp_set_owner_w(chunk);
1959 chunk->transport = transport;
1960 }
1961
1962 err = sctp_primitive_SEND(net, asoc, datamsg);
1963 if (err) {
1964 sctp_datamsg_free(datamsg);
1965 goto err;
1966 }
1967
1968 pr_debug("%s: we sent primitively\n", __func__);
1969
1970 sctp_datamsg_put(datamsg);
1971
1972 if (unlikely(wait_connect)) {
1973 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1974 sctp_wait_for_connect(asoc, &timeo);
1975 }
1976
1977 err = msg_len;
1978
1979err:
1980 return err;
1981}
1982
Xin Longbecef9b2018-03-01 23:05:13 +08001983static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1984 const struct msghdr *msg,
1985 struct sctp_cmsgs *cmsgs)
1986{
1987 union sctp_addr *daddr = NULL;
1988 int err;
1989
1990 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1991 int len = msg->msg_namelen;
1992
1993 if (len > sizeof(*daddr))
1994 len = sizeof(*daddr);
1995
1996 daddr = (union sctp_addr *)msg->msg_name;
1997
1998 err = sctp_verify_addr(sk, daddr, len);
1999 if (err)
2000 return ERR_PTR(err);
2001 }
2002
2003 return daddr;
2004}
2005
Xin Longd42cb062018-03-01 23:05:15 +08002006static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
2007 struct sctp_sndrcvinfo *sinfo,
2008 struct sctp_cmsgs *cmsgs)
2009{
2010 if (!cmsgs->srinfo && !cmsgs->sinfo) {
2011 sinfo->sinfo_stream = asoc->default_stream;
2012 sinfo->sinfo_ppid = asoc->default_ppid;
2013 sinfo->sinfo_context = asoc->default_context;
2014 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
Xin Longed63afb2018-03-05 20:44:18 +08002015
2016 if (!cmsgs->prinfo)
2017 sinfo->sinfo_flags = asoc->default_flags;
Xin Longd42cb062018-03-01 23:05:15 +08002018 }
2019
Xin Longed63afb2018-03-05 20:44:18 +08002020 if (!cmsgs->srinfo && !cmsgs->prinfo)
Xin Longd42cb062018-03-01 23:05:15 +08002021 sinfo->sinfo_timetolive = asoc->default_timetolive;
Xin Long3ff547c2018-03-14 19:05:31 +08002022
2023 if (cmsgs->authinfo) {
2024 /* Reuse sinfo_tsn to indicate that authinfo was set and
2025 * sinfo_ssn to save the keyid on tx path.
2026 */
2027 sinfo->sinfo_tsn = 1;
2028 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
2029 }
Xin Longd42cb062018-03-01 23:05:15 +08002030}
2031
Ying Xue1b784142015-03-02 15:37:48 +08002032static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Xin Long204f8172018-03-01 23:05:14 +08002034 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Xin Long8e87c6e2018-03-01 23:05:16 +08002035 struct sctp_transport *transport = NULL;
Xin Long204f8172018-03-01 23:05:14 +08002036 struct sctp_sndrcvinfo _sinfo, *sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08002037 struct sctp_association *asoc;
2038 struct sctp_cmsgs cmsgs;
Xin Longbecef9b2018-03-01 23:05:13 +08002039 union sctp_addr *daddr;
Xin Long007b7e12018-03-01 23:05:17 +08002040 bool new = false;
2041 __u16 sflags;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02002042 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Xin Long204f8172018-03-01 23:05:14 +08002044 /* Parse and get snd_info */
2045 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
2046 if (err)
Xin Long007b7e12018-03-01 23:05:17 +08002047 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Xin Long204f8172018-03-01 23:05:14 +08002049 sinfo = &_sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08002050 sflags = sinfo->sinfo_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Xin Longbecef9b2018-03-01 23:05:13 +08002052 /* Get daddr from msg */
2053 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
2054 if (IS_ERR(daddr)) {
2055 err = PTR_ERR(daddr);
Xin Long007b7e12018-03-01 23:05:17 +08002056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
2058
wangweidong048ed4b2014-01-21 15:44:11 +08002059 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Xin Long49102802018-03-05 20:44:20 +08002061 /* SCTP_SENDALL process */
2062 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
2063 list_for_each_entry(asoc, &ep->asocs, asocs) {
2064 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2065 msg_len);
2066 if (err == 0)
2067 continue;
2068 if (err < 0)
2069 goto out_unlock;
2070
2071 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2072
2073 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
2074 NULL, sinfo);
2075 if (err < 0)
2076 goto out_unlock;
2077
2078 iov_iter_revert(&msg->msg_iter, err);
2079 }
2080
2081 goto out_unlock;
2082 }
2083
Xin Long0a3920d2018-03-01 23:05:18 +08002084 /* Get and check or create asoc */
Xin Longbecef9b2018-03-01 23:05:13 +08002085 if (daddr) {
Xin Longbecef9b2018-03-01 23:05:13 +08002086 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
Xin Long0a3920d2018-03-01 23:05:18 +08002087 if (asoc) {
2088 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2089 msg_len);
2090 if (err <= 0)
2091 goto out_unlock;
2092 } else {
2093 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2094 &transport);
2095 if (err)
2096 goto out_unlock;
2097
2098 asoc = transport->asoc;
2099 new = true;
2100 }
2101
2102 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2103 transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 } else {
Xin Long007b7e12018-03-01 23:05:17 +08002105 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (!asoc) {
2107 err = -EPIPE;
2108 goto out_unlock;
2109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Xin Long007b7e12018-03-01 23:05:17 +08002111 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
Xin Longc2666de2018-03-01 23:05:12 +08002112 if (err <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
2115
Xin Longd42cb062018-03-01 23:05:15 +08002116 /* Update snd_info with the asoc */
2117 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Xin Longf84af332018-03-01 23:05:10 +08002119 /* Send msg to the asoc */
Xin Long8e87c6e2018-03-01 23:05:16 +08002120 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
Xin Long007b7e12018-03-01 23:05:17 +08002121 if (err < 0 && err != -ESRCH && new)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 sctp_association_free(asoc);
Xin Long8e87c6e2018-03-01 23:05:16 +08002123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124out_unlock:
wangweidong048ed4b2014-01-21 15:44:11 +08002125 release_sock(sk);
Xin Long007b7e12018-03-01 23:05:17 +08002126out:
Xin Longf84af332018-03-01 23:05:10 +08002127 return sctp_error(sk, msg->msg_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
2130/* This is an extended version of skb_pull() that removes the data from the
2131 * start of a skb even when data is spread across the list of skb's in the
2132 * frag_list. len specifies the total amount of data that needs to be removed.
2133 * when 'len' bytes could be removed from the skb, it returns 0.
2134 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2135 * could not be removed.
2136 */
2137static int sctp_skb_pull(struct sk_buff *skb, int len)
2138{
2139 struct sk_buff *list;
2140 int skb_len = skb_headlen(skb);
2141 int rlen;
2142
2143 if (len <= skb_len) {
2144 __skb_pull(skb, len);
2145 return 0;
2146 }
2147 len -= skb_len;
2148 __skb_pull(skb, skb_len);
2149
David S. Miller1b003be2009-06-09 00:22:35 -07002150 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 rlen = sctp_skb_pull(list, len);
2152 skb->len -= (len-rlen);
2153 skb->data_len -= (len-rlen);
2154
2155 if (!rlen)
2156 return 0;
2157
2158 len = rlen;
2159 }
2160
2161 return len;
2162}
2163
2164/* API 3.1.3 recvmsg() - UDP Style Syntax
2165 *
2166 * ssize_t recvmsg(int socket, struct msghdr *message,
2167 * int flags);
2168 *
2169 * socket - the socket descriptor of the endpoint.
2170 * message - pointer to the msghdr structure which contains a single
2171 * user message and possibly some ancillary data.
2172 *
2173 * See Section 5 for complete description of the data
2174 * structures.
2175 *
2176 * flags - flags sent or received with the user message, see Section
2177 * 5 for complete description of the flags.
2178 */
Ying Xue1b784142015-03-02 15:37:48 +08002179static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2180 int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 struct sctp_ulpevent *event = NULL;
2183 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002184 struct sk_buff *skb, *head_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 int copied;
2186 int err = 0;
2187 int skb_len;
2188
Daniel Borkmannbb333812013-06-28 19:49:40 +02002189 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2190 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2191 addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
wangweidong048ed4b2014-01-21 15:44:11 +08002193 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03002195 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
Xin Longe0878692016-07-30 14:14:41 +08002196 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 err = -ENOTCONN;
2198 goto out;
2199 }
2200
2201 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2202 if (!skb)
2203 goto out;
2204
2205 /* Get the total length of the skb including any skb's in the
2206 * frag_list.
2207 */
2208 skb_len = skb->len;
2209
2210 copied = skb_len;
2211 if (copied > len)
2212 copied = len;
2213
David S. Miller51f3d022014-11-05 16:46:40 -05002214 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216 event = sctp_skb2event(skb);
2217
2218 if (err)
2219 goto out_free;
2220
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002221 if (event->chunk && event->chunk->head_skb)
2222 head_skb = event->chunk->head_skb;
2223 else
2224 head_skb = skb;
2225 sock_recv_ts_and_drops(msg, sk, head_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (sctp_ulpevent_is_notification(event)) {
2227 msg->msg_flags |= MSG_NOTIFICATION;
2228 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2229 } else {
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002230 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
2232
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02002233 /* Check if we allow SCTP_NXTINFO. */
2234 if (sp->recvnxtinfo)
2235 sctp_ulpevent_read_nxtinfo(event, msg, sk);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002236 /* Check if we allow SCTP_RCVINFO. */
2237 if (sp->recvrcvinfo)
2238 sctp_ulpevent_read_rcvinfo(event, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 /* Check if we allow SCTP_SNDRCVINFO. */
2240 if (sp->subscribe.sctp_data_io_event)
2241 sctp_ulpevent_read_sndrcvinfo(event, msg);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 err = copied;
2244
2245 /* If skb's length exceeds the user's buffer, update the skb and
2246 * push it back to the receive_queue so that the next call to
2247 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2248 */
2249 if (skb_len > copied) {
2250 msg->msg_flags &= ~MSG_EOR;
2251 if (flags & MSG_PEEK)
2252 goto out_free;
2253 sctp_skb_pull(skb, copied);
2254 skb_queue_head(&sk->sk_receive_queue, skb);
2255
Daniel Borkmann362d5202014-04-14 21:45:17 +02002256 /* When only partial message is copied to the user, increase
2257 * rwnd by that amount. If all the data in the skb is read,
2258 * rwnd is updated when the event is freed.
2259 */
2260 if (!sctp_ulpevent_is_notification(event))
2261 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 goto out;
2263 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2264 (event->msg_flags & MSG_EOR))
2265 msg->msg_flags |= MSG_EOR;
2266 else
2267 msg->msg_flags &= ~MSG_EOR;
2268
2269out_free:
2270 if (flags & MSG_PEEK) {
2271 /* Release the skb reference acquired after peeking the skb in
2272 * sctp_skb_recv_datagram().
2273 */
2274 kfree_skb(skb);
2275 } else {
2276 /* Free the event which includes releasing the reference to
2277 * the owner of the skb, freeing the skb and updating the
2278 * rwnd.
2279 */
2280 sctp_ulpevent_free(event);
2281 }
2282out:
wangweidong048ed4b2014-01-21 15:44:11 +08002283 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return err;
2285}
2286
2287/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2288 *
2289 * This option is a on/off flag. If enabled no SCTP message
2290 * fragmentation will be performed. Instead if a message being sent
2291 * exceeds the current PMTU size, the message will NOT be sent and
2292 * instead a error will be indicated to the user.
2293 */
2294static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002295 char __user *optval,
2296 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
2298 int val;
2299
2300 if (optlen < sizeof(int))
2301 return -EINVAL;
2302
2303 if (get_user(val, (int __user *)optval))
2304 return -EFAULT;
2305
2306 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2307
2308 return 0;
2309}
2310
2311static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002312 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Wei Yongjun94912302011-07-02 09:28:04 +00002314 struct sctp_association *asoc;
2315 struct sctp_ulpevent *event;
2316
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002317 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return -EINVAL;
2319 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2320 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002321
Daniel Borkmannbbbea412014-07-12 20:30:40 +02002322 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
Wei Yongjun94912302011-07-02 09:28:04 +00002323 * if there is no data to be sent or retransmit, the stack will
2324 * immediately send up this notification.
2325 */
2326 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2327 &sctp_sk(sk)->subscribe)) {
2328 asoc = sctp_id2assoc(sk, 0);
2329
2330 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2331 event = sctp_ulpevent_make_sender_dry_event(asoc,
Marcelo Ricardo Leitner2e83acb2018-01-08 19:02:27 -02002332 GFP_USER | __GFP_NOWARN);
Wei Yongjun94912302011-07-02 09:28:04 +00002333 if (!event)
2334 return -ENOMEM;
2335
Xin Long9162e0e2017-12-08 21:04:05 +08002336 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
Wei Yongjun94912302011-07-02 09:28:04 +00002337 }
2338 }
2339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return 0;
2341}
2342
2343/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2344 *
2345 * This socket option is applicable to the UDP-style socket only. When
2346 * set it will cause associations that are idle for more than the
2347 * specified number of seconds to automatically close. An association
2348 * being idle is defined an association that has NOT sent or received
2349 * user data. The special value of '0' indicates that no automatic
2350 * close of any associations should be performed. The option expects an
2351 * integer defining the number of seconds of idle time before an
2352 * association is closed.
2353 */
2354static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002355 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
2357 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman9f70f462013-12-10 06:48:15 -05002358 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 /* Applicable to UDP-style socket only */
2361 if (sctp_style(sk, TCP))
2362 return -EOPNOTSUPP;
2363 if (optlen != sizeof(int))
2364 return -EINVAL;
2365 if (copy_from_user(&sp->autoclose, optval, optlen))
2366 return -EFAULT;
2367
Neil Horman9f70f462013-12-10 06:48:15 -05002368 if (sp->autoclose > net->sctp.max_autoclose)
2369 sp->autoclose = net->sctp.max_autoclose;
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return 0;
2372}
2373
2374/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2375 *
2376 * Applications can enable or disable heartbeats for any peer address of
2377 * an association, modify an address's heartbeat interval, force a
2378 * heartbeat to be sent immediately, and adjust the address's maximum
2379 * number of retransmissions sent before an address is considered
2380 * unreachable. The following structure is used to access and modify an
2381 * address's parameters:
2382 *
2383 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002384 * sctp_assoc_t spp_assoc_id;
2385 * struct sockaddr_storage spp_address;
2386 * uint32_t spp_hbinterval;
2387 * uint16_t spp_pathmaxrxt;
2388 * uint32_t spp_pathmtu;
2389 * uint32_t spp_sackdelay;
2390 * uint32_t spp_flags;
2391 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002393 * spp_assoc_id - (one-to-many style socket) This is filled in the
2394 * application, and identifies the association for
2395 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 * spp_address - This specifies which address is of interest.
2397 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002398 * in milliseconds. If a value of zero
2399 * is present in this field then no changes are to
2400 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 * spp_pathmaxrxt - This contains the maximum number of
2402 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002403 * considered unreachable. If a value of zero
2404 * is present in this field then no changes are to
2405 * be made to this parameter.
2406 * spp_pathmtu - When Path MTU discovery is disabled the value
2407 * specified here will be the "fixed" path mtu.
2408 * Note that if the spp_address field is empty
2409 * then all associations on this address will
2410 * have this fixed path mtu set upon them.
2411 *
2412 * spp_sackdelay - When delayed sack is enabled, this value specifies
2413 * the number of milliseconds that sacks will be delayed
2414 * for. This value will apply to all addresses of an
2415 * association if the spp_address field is empty. Note
2416 * also, that if delayed sack is enabled and this
2417 * value is set to 0, no change is made to the last
2418 * recorded delayed sack timer value.
2419 *
2420 * spp_flags - These flags are used to control various features
2421 * on an association. The flag field may contain
2422 * zero or more of the following options.
2423 *
2424 * SPP_HB_ENABLE - Enable heartbeats on the
2425 * specified address. Note that if the address
2426 * field is empty all addresses for the association
2427 * have heartbeats enabled upon them.
2428 *
2429 * SPP_HB_DISABLE - Disable heartbeats on the
2430 * speicifed address. Note that if the address
2431 * field is empty all addresses for the association
2432 * will have their heartbeats disabled. Note also
2433 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2434 * mutually exclusive, only one of these two should
2435 * be specified. Enabling both fields will have
2436 * undetermined results.
2437 *
2438 * SPP_HB_DEMAND - Request a user initiated heartbeat
2439 * to be made immediately.
2440 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002441 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2442 * heartbeat delayis to be set to the value of 0
2443 * milliseconds.
2444 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002445 * SPP_PMTUD_ENABLE - This field will enable 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.
2449 *
2450 * SPP_PMTUD_DISABLE - This field will disable PMTU
2451 * discovery upon the specified address. Note that
2452 * if the address feild is empty then all addresses
2453 * on the association are effected. Not also that
2454 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2455 * exclusive. Enabling both will have undetermined
2456 * results.
2457 *
2458 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2459 * on delayed sack. The time specified in spp_sackdelay
2460 * is used to specify the sack delay for this address. Note
2461 * that if spp_address is empty then all addresses will
2462 * enable delayed sack and take on the sack delay
2463 * value specified in spp_sackdelay.
2464 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2465 * off delayed sack. If the spp_address field is blank then
2466 * delayed sack is disabled for the entire association. Note
2467 * also that this field is mutually exclusive to
2468 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2469 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 */
Adrian Bunk16164362006-09-18 00:40:38 -07002471static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2472 struct sctp_transport *trans,
2473 struct sctp_association *asoc,
2474 struct sctp_sock *sp,
2475 int hb_change,
2476 int pmtud_change,
2477 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 int error;
2480
Frank Filz52ccb8e2005-12-22 11:36:46 -08002481 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002482 struct net *net = sock_net(trans->asoc->base.sk);
2483
2484 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 if (error)
2486 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
2488
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002489 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2490 * this field is ignored. Note also that a value of zero indicates
2491 * the current setting should be left unchanged.
2492 */
2493 if (params->spp_flags & SPP_HB_ENABLE) {
2494
2495 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2496 * set. This lets us use 0 value when this flag
2497 * is set.
2498 */
2499 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2500 params->spp_hbinterval = 0;
2501
2502 if (params->spp_hbinterval ||
2503 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2504 if (trans) {
2505 trans->hbinterval =
2506 msecs_to_jiffies(params->spp_hbinterval);
2507 } else if (asoc) {
2508 asoc->hbinterval =
2509 msecs_to_jiffies(params->spp_hbinterval);
2510 } else {
2511 sp->hbinterval = params->spp_hbinterval;
2512 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002513 }
2514 }
2515
2516 if (hb_change) {
2517 if (trans) {
2518 trans->param_flags =
2519 (trans->param_flags & ~SPP_HB) | hb_change;
2520 } else if (asoc) {
2521 asoc->param_flags =
2522 (asoc->param_flags & ~SPP_HB) | hb_change;
2523 } else {
2524 sp->param_flags =
2525 (sp->param_flags & ~SPP_HB) | hb_change;
2526 }
2527 }
2528
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002529 /* When Path MTU discovery is disabled the value specified here will
2530 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2531 * include the flag SPP_PMTUD_DISABLE for this field to have any
2532 * effect).
2533 */
2534 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002535 if (trans) {
2536 trans->pathmtu = params->spp_pathmtu;
Xin Long3ebfdf02017-04-04 13:39:55 +08002537 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002538 } else if (asoc) {
2539 asoc->pathmtu = params->spp_pathmtu;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002540 } else {
2541 sp->pathmtu = params->spp_pathmtu;
2542 }
2543 }
2544
2545 if (pmtud_change) {
2546 if (trans) {
2547 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2548 (params->spp_flags & SPP_PMTUD_ENABLE);
2549 trans->param_flags =
2550 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2551 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002552 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
Xin Long3ebfdf02017-04-04 13:39:55 +08002553 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002554 }
2555 } else if (asoc) {
2556 asoc->param_flags =
2557 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2558 } else {
2559 sp->param_flags =
2560 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2561 }
2562 }
2563
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002564 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2565 * value of this field is ignored. Note also that a value of zero
2566 * indicates the current setting should be left unchanged.
2567 */
2568 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002569 if (trans) {
2570 trans->sackdelay =
2571 msecs_to_jiffies(params->spp_sackdelay);
2572 } else if (asoc) {
2573 asoc->sackdelay =
2574 msecs_to_jiffies(params->spp_sackdelay);
2575 } else {
2576 sp->sackdelay = params->spp_sackdelay;
2577 }
2578 }
2579
2580 if (sackdelay_change) {
2581 if (trans) {
2582 trans->param_flags =
2583 (trans->param_flags & ~SPP_SACKDELAY) |
2584 sackdelay_change;
2585 } else if (asoc) {
2586 asoc->param_flags =
2587 (asoc->param_flags & ~SPP_SACKDELAY) |
2588 sackdelay_change;
2589 } else {
2590 sp->param_flags =
2591 (sp->param_flags & ~SPP_SACKDELAY) |
2592 sackdelay_change;
2593 }
2594 }
2595
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002596 /* Note that a value of zero indicates the current setting should be
2597 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002598 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002599 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002600 if (trans) {
2601 trans->pathmaxrxt = params->spp_pathmaxrxt;
2602 } else if (asoc) {
2603 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2604 } else {
2605 sp->pathmaxrxt = params->spp_pathmaxrxt;
2606 }
2607 }
2608
2609 return 0;
2610}
2611
2612static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002613 char __user *optval,
2614 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002615{
2616 struct sctp_paddrparams params;
2617 struct sctp_transport *trans = NULL;
2618 struct sctp_association *asoc = NULL;
2619 struct sctp_sock *sp = sctp_sk(sk);
2620 int error;
2621 int hb_change, pmtud_change, sackdelay_change;
2622
2623 if (optlen != sizeof(struct sctp_paddrparams))
wangweidongcb3f8372013-12-23 12:16:50 +08002624 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002625
2626 if (copy_from_user(&params, optval, optlen))
2627 return -EFAULT;
2628
2629 /* Validate flags and value parameters. */
2630 hb_change = params.spp_flags & SPP_HB;
2631 pmtud_change = params.spp_flags & SPP_PMTUD;
2632 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2633
2634 if (hb_change == SPP_HB ||
2635 pmtud_change == SPP_PMTUD ||
2636 sackdelay_change == SPP_SACKDELAY ||
2637 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002638 (params.spp_pathmtu &&
2639 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002640 return -EINVAL;
2641
2642 /* If an address other than INADDR_ANY is specified, and
2643 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 */
wangweidongcb3f8372013-12-23 12:16:50 +08002645 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002646 trans = sctp_addr_id2transport(sk, &params.spp_address,
2647 params.spp_assoc_id);
2648 if (!trans)
2649 return -EINVAL;
2650 }
2651
2652 /* Get association, if assoc_id != 0 and the socket is a one
2653 * to many style socket, and an association was not found, then
2654 * the id was invalid.
2655 */
2656 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2657 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2658 return -EINVAL;
2659
2660 /* Heartbeat demand can only be sent on a transport or
2661 * association, but not a socket.
2662 */
2663 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2664 return -EINVAL;
2665
2666 /* Process parameters. */
2667 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2668 hb_change, pmtud_change,
2669 sackdelay_change);
2670
2671 if (error)
2672 return error;
2673
2674 /* If changes are for association, also apply parameters to each
2675 * transport.
2676 */
2677 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002678 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2679 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002680 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2681 hb_change, pmtud_change,
2682 sackdelay_change);
2683 }
2684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
2686 return 0;
2687}
2688
wangweidong0ea5e4d2014-01-15 17:24:01 +08002689static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2690{
2691 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2692}
2693
2694static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2695{
2696 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2697}
2698
Wei Yongjund364d922008-05-09 15:13:26 -07002699/*
2700 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002701 *
Wei Yongjund364d922008-05-09 15:13:26 -07002702 * This option will effect the way delayed acks are performed. This
2703 * option allows you to get or set the delayed ack time, in
2704 * milliseconds. It also allows changing the delayed ack frequency.
2705 * Changing the frequency to 1 disables the delayed sack algorithm. If
2706 * the assoc_id is 0, then this sets or gets the endpoints default
2707 * values. If the assoc_id field is non-zero, then the set or get
2708 * effects the specified association for the one to many model (the
2709 * assoc_id field is ignored by the one to one model). Note that if
2710 * sack_delay or sack_freq are 0 when setting this option, then the
2711 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002712 *
Wei Yongjund364d922008-05-09 15:13:26 -07002713 * struct sctp_sack_info {
2714 * sctp_assoc_t sack_assoc_id;
2715 * uint32_t sack_delay;
2716 * uint32_t sack_freq;
2717 * };
Frank Filz77086102005-12-22 11:37:30 -08002718 *
Wei Yongjund364d922008-05-09 15:13:26 -07002719 * sack_assoc_id - This parameter, indicates which association the user
2720 * is performing an action upon. Note that if this field's value is
2721 * zero then the endpoints default value is changed (effecting future
2722 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002723 *
Wei Yongjund364d922008-05-09 15:13:26 -07002724 * sack_delay - This parameter contains the number of milliseconds that
2725 * the user is requesting the delayed ACK timer be set to. Note that
2726 * this value is defined in the standard to be between 200 and 500
2727 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002728 *
Wei Yongjund364d922008-05-09 15:13:26 -07002729 * sack_freq - This parameter contains the number of packets that must
2730 * be received before a sack is sent without waiting for the delay
2731 * timer to expire. The default value for this is 2, setting this
2732 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002733 */
2734
Wei Yongjund364d922008-05-09 15:13:26 -07002735static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002736 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002737{
Wei Yongjund364d922008-05-09 15:13:26 -07002738 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002739 struct sctp_transport *trans = NULL;
2740 struct sctp_association *asoc = NULL;
2741 struct sctp_sock *sp = sctp_sk(sk);
2742
Wei Yongjund364d922008-05-09 15:13:26 -07002743 if (optlen == sizeof(struct sctp_sack_info)) {
2744 if (copy_from_user(&params, optval, optlen))
2745 return -EFAULT;
2746
2747 if (params.sack_delay == 0 && params.sack_freq == 0)
2748 return 0;
2749 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05002750 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05002751 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05002752 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05002753 "Use struct sctp_sack_info instead\n",
2754 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07002755 if (copy_from_user(&params, optval, optlen))
2756 return -EFAULT;
2757
2758 if (params.sack_delay == 0)
2759 params.sack_freq = 1;
2760 else
2761 params.sack_freq = 0;
2762 } else
wangweidongcb3f8372013-12-23 12:16:50 +08002763 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08002764
Frank Filz77086102005-12-22 11:37:30 -08002765 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002766 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002767 return -EINVAL;
2768
Wei Yongjund364d922008-05-09 15:13:26 -07002769 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002770 * to many style socket, and an association was not found, then
2771 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002772 */
Wei Yongjund364d922008-05-09 15:13:26 -07002773 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2774 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002775 return -EINVAL;
2776
Wei Yongjund364d922008-05-09 15:13:26 -07002777 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002778 if (asoc) {
2779 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002780 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002781 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002782 sctp_spp_sackdelay_enable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002783 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002784 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002785 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002786 sctp_spp_sackdelay_enable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002787 }
Wei Yongjund364d922008-05-09 15:13:26 -07002788 }
2789
2790 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002791 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002792 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002793 sctp_spp_sackdelay_disable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002794 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002795 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002796 sctp_spp_sackdelay_disable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002797 }
Wei Yongjund364d922008-05-09 15:13:26 -07002798 } else if (params.sack_freq > 1) {
2799 if (asoc) {
2800 asoc->sackfreq = params.sack_freq;
2801 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002802 sctp_spp_sackdelay_enable(asoc->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002803 } else {
2804 sp->sackfreq = params.sack_freq;
2805 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002806 sctp_spp_sackdelay_enable(sp->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002807 }
Frank Filz77086102005-12-22 11:37:30 -08002808 }
2809
2810 /* If change is for association, also apply to each transport. */
2811 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002812 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2813 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002814 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002815 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002816 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002817 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002818 sctp_spp_sackdelay_enable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002819 }
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07002820 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002821 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002822 sctp_spp_sackdelay_disable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002823 } else if (params.sack_freq > 1) {
2824 trans->sackfreq = params.sack_freq;
2825 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002826 sctp_spp_sackdelay_enable(trans->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002827 }
2828 }
2829 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002830
Frank Filz77086102005-12-22 11:37:30 -08002831 return 0;
2832}
2833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2835 *
2836 * Applications can specify protocol parameters for the default association
2837 * initialization. The option name argument to setsockopt() and getsockopt()
2838 * is SCTP_INITMSG.
2839 *
2840 * Setting initialization parameters is effective only on an unconnected
2841 * socket (for UDP-style sockets only future associations are effected
2842 * by the change). With TCP-style sockets, this option is inherited by
2843 * sockets derived from a listener socket.
2844 */
David S. Millerb7058842009-09-30 16:12:20 -07002845static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
2847 struct sctp_initmsg sinit;
2848 struct sctp_sock *sp = sctp_sk(sk);
2849
2850 if (optlen != sizeof(struct sctp_initmsg))
2851 return -EINVAL;
2852 if (copy_from_user(&sinit, optval, optlen))
2853 return -EFAULT;
2854
2855 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002856 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002858 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002860 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002862 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
2864 return 0;
2865}
2866
2867/*
2868 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2869 *
2870 * Applications that wish to use the sendto() system call may wish to
2871 * specify a default set of parameters that would normally be supplied
2872 * through the inclusion of ancillary data. This socket option allows
2873 * such an application to set the default sctp_sndrcvinfo structure.
2874 * The application that wishes to use this socket option simply passes
2875 * in to this call the sctp_sndrcvinfo structure defined in Section
2876 * 5.2.2) The input parameters accepted by this call include
2877 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2878 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2879 * to this call if the caller is using the UDP model.
2880 */
2881static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002882 char __user *optval,
2883 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002886 struct sctp_association *asoc;
2887 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002889 if (optlen != sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return -EINVAL;
2891 if (copy_from_user(&info, optval, optlen))
2892 return -EFAULT;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002893 if (info.sinfo_flags &
2894 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2895 SCTP_ABORT | SCTP_EOF))
2896 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
2898 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2899 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2900 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 if (asoc) {
2902 asoc->default_stream = info.sinfo_stream;
2903 asoc->default_flags = info.sinfo_flags;
2904 asoc->default_ppid = info.sinfo_ppid;
2905 asoc->default_context = info.sinfo_context;
2906 asoc->default_timetolive = info.sinfo_timetolive;
2907 } else {
2908 sp->default_stream = info.sinfo_stream;
2909 sp->default_flags = info.sinfo_flags;
2910 sp->default_ppid = info.sinfo_ppid;
2911 sp->default_context = info.sinfo_context;
2912 sp->default_timetolive = info.sinfo_timetolive;
2913 }
2914
2915 return 0;
2916}
2917
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002918/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2919 * (SCTP_DEFAULT_SNDINFO)
2920 */
2921static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2922 char __user *optval,
2923 unsigned int optlen)
2924{
2925 struct sctp_sock *sp = sctp_sk(sk);
2926 struct sctp_association *asoc;
2927 struct sctp_sndinfo info;
2928
2929 if (optlen != sizeof(info))
2930 return -EINVAL;
2931 if (copy_from_user(&info, optval, optlen))
2932 return -EFAULT;
2933 if (info.snd_flags &
2934 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2935 SCTP_ABORT | SCTP_EOF))
2936 return -EINVAL;
2937
2938 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
2939 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
2940 return -EINVAL;
2941 if (asoc) {
2942 asoc->default_stream = info.snd_sid;
2943 asoc->default_flags = info.snd_flags;
2944 asoc->default_ppid = info.snd_ppid;
2945 asoc->default_context = info.snd_context;
2946 } else {
2947 sp->default_stream = info.snd_sid;
2948 sp->default_flags = info.snd_flags;
2949 sp->default_ppid = info.snd_ppid;
2950 sp->default_context = info.snd_context;
2951 }
2952
2953 return 0;
2954}
2955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2957 *
2958 * Requests that the local SCTP stack use the enclosed peer address as
2959 * the association primary. The enclosed address must be one of the
2960 * association peer's addresses.
2961 */
2962static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002963 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
2965 struct sctp_prim prim;
2966 struct sctp_transport *trans;
Richard Haines2277c7c2018-02-13 20:56:24 +00002967 struct sctp_af *af;
2968 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
2970 if (optlen != sizeof(struct sctp_prim))
2971 return -EINVAL;
2972
2973 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2974 return -EFAULT;
2975
Richard Haines2277c7c2018-02-13 20:56:24 +00002976 /* Allow security module to validate address but need address len. */
2977 af = sctp_get_af_specific(prim.ssp_addr.ss_family);
2978 if (!af)
2979 return -EINVAL;
2980
2981 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
2982 (struct sockaddr *)&prim.ssp_addr,
2983 af->sockaddr_len);
2984 if (err)
2985 return err;
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2988 if (!trans)
2989 return -EINVAL;
2990
2991 sctp_assoc_set_primary(trans->asoc, trans);
2992
2993 return 0;
2994}
2995
2996/*
2997 * 7.1.5 SCTP_NODELAY
2998 *
2999 * Turn on/off any Nagle-like algorithm. This means that packets are
3000 * generally sent as soon as possible and no unnecessary delays are
3001 * introduced, at the cost of more packets in the network. Expects an
3002 * integer boolean flag.
3003 */
3004static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003005 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
3007 int val;
3008
3009 if (optlen < sizeof(int))
3010 return -EINVAL;
3011 if (get_user(val, (int __user *)optval))
3012 return -EFAULT;
3013
3014 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3015 return 0;
3016}
3017
3018/*
3019 *
3020 * 7.1.1 SCTP_RTOINFO
3021 *
3022 * The protocol parameters used to initialize and bound retransmission
3023 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3024 * and modify these parameters.
3025 * All parameters are time values, in milliseconds. A value of 0, when
3026 * modifying the parameters, indicates that the current value should not
3027 * be changed.
3028 *
3029 */
David S. Millerb7058842009-09-30 16:12:20 -07003030static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 struct sctp_rtoinfo rtoinfo;
3033 struct sctp_association *asoc;
wangweidong85f935d2013-12-11 09:50:38 +08003034 unsigned long rto_min, rto_max;
3035 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
3037 if (optlen != sizeof (struct sctp_rtoinfo))
3038 return -EINVAL;
3039
3040 if (copy_from_user(&rtoinfo, optval, optlen))
3041 return -EFAULT;
3042
3043 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3044
3045 /* Set the values to the specific association */
3046 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3047 return -EINVAL;
3048
wangweidong85f935d2013-12-11 09:50:38 +08003049 rto_max = rtoinfo.srto_max;
3050 rto_min = rtoinfo.srto_min;
3051
3052 if (rto_max)
3053 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3054 else
3055 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3056
3057 if (rto_min)
3058 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3059 else
3060 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3061
3062 if (rto_min > rto_max)
3063 return -EINVAL;
3064
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (asoc) {
3066 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003067 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 msecs_to_jiffies(rtoinfo.srto_initial);
wangweidong85f935d2013-12-11 09:50:38 +08003069 asoc->rto_max = rto_max;
3070 asoc->rto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 } else {
3072 /* If there is no association or the association-id = 0
3073 * set the values to the endpoint.
3074 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 if (rtoinfo.srto_initial != 0)
3076 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
wangweidong85f935d2013-12-11 09:50:38 +08003077 sp->rtoinfo.srto_max = rto_max;
3078 sp->rtoinfo.srto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 }
3080
3081 return 0;
3082}
3083
3084/*
3085 *
3086 * 7.1.2 SCTP_ASSOCINFO
3087 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02003088 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 * of the association.
3090 * Returns an error if the new association retransmission value is
3091 * greater than the sum of the retransmission value of the peer.
3092 * See [SCTP] for more information.
3093 *
3094 */
David S. Millerb7058842009-09-30 16:12:20 -07003095static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
3097
3098 struct sctp_assocparams assocparams;
3099 struct sctp_association *asoc;
3100
3101 if (optlen != sizeof(struct sctp_assocparams))
3102 return -EINVAL;
3103 if (copy_from_user(&assocparams, optval, optlen))
3104 return -EFAULT;
3105
3106 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3107
3108 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3109 return -EINVAL;
3110
3111 /* Set the values to the specific association */
3112 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003113 if (assocparams.sasoc_asocmaxrxt != 0) {
3114 __u32 path_sum = 0;
3115 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003116 struct sctp_transport *peer_addr;
3117
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07003118 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3119 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003120 path_sum += peer_addr->pathmaxrxt;
3121 paths++;
3122 }
3123
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003124 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003125 * one path/transport. We do this because path
3126 * retransmissions are only counted when we have more
3127 * then one path.
3128 */
3129 if (paths > 1 &&
3130 assocparams.sasoc_asocmaxrxt > path_sum)
3131 return -EINVAL;
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003134 }
3135
Daniel Borkmann52db8822013-06-25 18:17:27 +02003136 if (assocparams.sasoc_cookie_life != 0)
3137 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 } else {
3139 /* Set the values to the endpoint */
3140 struct sctp_sock *sp = sctp_sk(sk);
3141
3142 if (assocparams.sasoc_asocmaxrxt != 0)
3143 sp->assocparams.sasoc_asocmaxrxt =
3144 assocparams.sasoc_asocmaxrxt;
3145 if (assocparams.sasoc_cookie_life != 0)
3146 sp->assocparams.sasoc_cookie_life =
3147 assocparams.sasoc_cookie_life;
3148 }
3149 return 0;
3150}
3151
3152/*
3153 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3154 *
3155 * This socket option is a boolean flag which turns on or off mapped V4
3156 * addresses. If this option is turned on and the socket is type
3157 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3158 * If this option is turned off, then no mapping will be done of V4
3159 * addresses and a user will receive both PF_INET6 and PF_INET type
3160 * addresses on the socket.
3161 */
David S. Millerb7058842009-09-30 16:12:20 -07003162static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163{
3164 int val;
3165 struct sctp_sock *sp = sctp_sk(sk);
3166
3167 if (optlen < sizeof(int))
3168 return -EINVAL;
3169 if (get_user(val, (int __user *)optval))
3170 return -EFAULT;
3171 if (val)
3172 sp->v4mapped = 1;
3173 else
3174 sp->v4mapped = 0;
3175
3176 return 0;
3177}
3178
3179/*
Wei Yongjune89c2092008-12-25 16:54:58 -08003180 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3181 * This option will get or set the maximum size to put in any outgoing
3182 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 * fragmented by SCTP into the specified size. Note that the underlying
3184 * SCTP implementation may fragment into smaller sized chunks when the
3185 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08003186 * the user. The default value for this option is '0' which indicates
3187 * the user is NOT limiting fragmentation and only the PMTU will effect
3188 * SCTP's choice of DATA chunk size. Note also that values set larger
3189 * than the maximum size of an IP datagram will effectively let SCTP
3190 * control fragmentation (i.e. the same as setting this option to 0).
3191 *
3192 * The following structure is used to access and modify this parameter:
3193 *
3194 * struct sctp_assoc_value {
3195 * sctp_assoc_t assoc_id;
3196 * uint32_t assoc_value;
3197 * };
3198 *
3199 * assoc_id: This parameter is ignored for one-to-one style sockets.
3200 * For one-to-many style sockets this parameter indicates which
3201 * association the user is performing an action upon. Note that if
3202 * this field's value is zero then the endpoints default value is
3203 * changed (effecting future associations only).
3204 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 */
David S. Millerb7058842009-09-30 16:12:20 -07003206static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
Xin Longecca8f82017-11-17 14:11:11 +08003208 struct sctp_sock *sp = sctp_sk(sk);
Richard Hainesb7e10c22018-02-24 16:18:51 +00003209 struct sctp_af *af = sp->pf->af;
Wei Yongjune89c2092008-12-25 16:54:58 -08003210 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 int val;
3213
Wei Yongjune89c2092008-12-25 16:54:58 -08003214 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003215 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003216 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003217 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003218 "Use struct sctp_assoc_value instead\n",
3219 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08003220 if (copy_from_user(&val, optval, optlen))
3221 return -EFAULT;
3222 params.assoc_id = 0;
3223 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3224 if (copy_from_user(&params, optval, optlen))
3225 return -EFAULT;
3226 val = params.assoc_value;
Xin Longecca8f82017-11-17 14:11:11 +08003227 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return -EINVAL;
Xin Longecca8f82017-11-17 14:11:11 +08003229 }
Wei Yongjune89c2092008-12-25 16:54:58 -08003230
Xin Longecca8f82017-11-17 14:11:11 +08003231 if (val) {
3232 int min_len, max_len;
3233
Richard Hainesb7e10c22018-02-24 16:18:51 +00003234 min_len = SCTP_DEFAULT_MINSEGMENT - af->net_header_len;
3235 min_len -= af->ip_options_len(sk);
Xin Longecca8f82017-11-17 14:11:11 +08003236 min_len -= sizeof(struct sctphdr) +
3237 sizeof(struct sctp_data_chunk);
3238
3239 max_len = SCTP_MAX_CHUNK_LEN - sizeof(struct sctp_data_chunk);
3240
3241 if (val < min_len || val > max_len)
3242 return -EINVAL;
3243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Wei Yongjune89c2092008-12-25 16:54:58 -08003245 asoc = sctp_id2assoc(sk, params.assoc_id);
Wei Yongjune89c2092008-12-25 16:54:58 -08003246 if (asoc) {
3247 if (val == 0) {
Richard Hainesb7e10c22018-02-24 16:18:51 +00003248 val = asoc->pathmtu - af->net_header_len;
3249 val -= af->ip_options_len(sk);
Wei Yongjune89c2092008-12-25 16:54:58 -08003250 val -= sizeof(struct sctphdr) +
Xin Long668c9be2017-12-08 21:04:02 +08003251 sctp_datachk_len(&asoc->stream);
Wei Yongjune89c2092008-12-25 16:54:58 -08003252 }
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003253 asoc->user_frag = val;
3254 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
Wei Yongjune89c2092008-12-25 16:54:58 -08003255 } else {
Xin Longecca8f82017-11-17 14:11:11 +08003256 if (params.assoc_id && sctp_style(sk, UDP))
3257 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003258 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 }
3260
3261 return 0;
3262}
3263
3264
3265/*
3266 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3267 *
3268 * Requests that the peer mark the enclosed address as the association
3269 * primary. The enclosed address must be one of the association's
3270 * locally bound addresses. The following structure is used to make a
3271 * set primary request:
3272 */
3273static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003274 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003276 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 struct sctp_association *asoc = NULL;
3279 struct sctp_setpeerprim prim;
3280 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003281 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 int err;
3283
3284 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003286 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 return -EPERM;
3288
3289 if (optlen != sizeof(struct sctp_setpeerprim))
3290 return -EINVAL;
3291
3292 if (copy_from_user(&prim, optval, optlen))
3293 return -EFAULT;
3294
3295 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003296 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 return -EINVAL;
3298
3299 if (!asoc->peer.asconf_capable)
3300 return -EPERM;
3301
3302 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3303 return -EPERM;
3304
3305 if (!sctp_state(asoc, ESTABLISHED))
3306 return -ENOTCONN;
3307
Wei Yongjun40a01032010-12-07 17:11:09 +00003308 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3309 if (!af)
3310 return -EINVAL;
3311
3312 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3313 return -EADDRNOTAVAIL;
3314
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3316 return -EADDRNOTAVAIL;
3317
Richard Haines2277c7c2018-02-13 20:56:24 +00003318 /* Allow security module to validate address. */
3319 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3320 (struct sockaddr *)&prim.sspp_addr,
3321 af->sockaddr_len);
3322 if (err)
3323 return err;
3324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3326 chunk = sctp_make_asconf_set_prim(asoc,
3327 (union sctp_addr *)&prim.sspp_addr);
3328 if (!chunk)
3329 return -ENOMEM;
3330
3331 err = sctp_send_asconf(asoc, chunk);
3332
Daniel Borkmannbb333812013-06-28 19:49:40 +02003333 pr_debug("%s: we set peer primary addr primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
3335 return err;
3336}
3337
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003338static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003339 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003341 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003343 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003345 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 return -EFAULT;
3347
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003348 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 return 0;
3351}
3352
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003353/*
3354 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3355 *
3356 * The context field in the sctp_sndrcvinfo structure is normally only
3357 * used when a failed message is retrieved holding the value that was
3358 * sent down on the actual send call. This option allows the setting of
3359 * a default context on an association basis that will be received on
3360 * reading messages from the peer. This is especially helpful in the
3361 * one-2-many model for an application to keep some reference to an
3362 * internal state machine that is processing messages on the
3363 * association. Note that the setting of this value only effects
3364 * received messages from the peer and does not effect the value that is
3365 * saved with outbound messages.
3366 */
3367static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003368 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003369{
3370 struct sctp_assoc_value params;
3371 struct sctp_sock *sp;
3372 struct sctp_association *asoc;
3373
3374 if (optlen != sizeof(struct sctp_assoc_value))
3375 return -EINVAL;
3376 if (copy_from_user(&params, optval, optlen))
3377 return -EFAULT;
3378
3379 sp = sctp_sk(sk);
3380
3381 if (params.assoc_id != 0) {
3382 asoc = sctp_id2assoc(sk, params.assoc_id);
3383 if (!asoc)
3384 return -EINVAL;
3385 asoc->default_rcv_context = params.assoc_value;
3386 } else {
3387 sp->default_rcv_context = params.assoc_value;
3388 }
3389
3390 return 0;
3391}
3392
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003393/*
3394 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3395 *
3396 * This options will at a minimum specify if the implementation is doing
3397 * fragmented interleave. Fragmented interleave, for a one to many
3398 * socket, is when subsequent calls to receive a message may return
3399 * parts of messages from different associations. Some implementations
3400 * may allow you to turn this value on or off. If so, when turned off,
3401 * no fragment interleave will occur (which will cause a head of line
3402 * blocking amongst multiple associations sharing the same one to many
3403 * socket). When this option is turned on, then each receive call may
3404 * come from a different association (thus the user must receive data
3405 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3406 * association each receive belongs to.
3407 *
3408 * This option takes a boolean value. A non-zero value indicates that
3409 * fragmented interleave is on. A value of zero indicates that
3410 * fragmented interleave is off.
3411 *
3412 * Note that it is important that an implementation that allows this
3413 * option to be turned on, have it off by default. Otherwise an unaware
3414 * application using the one to many model may become confused and act
3415 * incorrectly.
3416 */
3417static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3418 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003419 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003420{
3421 int val;
3422
3423 if (optlen != sizeof(int))
3424 return -EINVAL;
3425 if (get_user(val, (int __user *)optval))
3426 return -EFAULT;
3427
Xin Long772a5862017-12-08 21:03:58 +08003428 sctp_sk(sk)->frag_interleave = !!val;
3429
3430 if (!sctp_sk(sk)->frag_interleave)
3431 sctp_sk(sk)->strm_interleave = 0;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003432
3433 return 0;
3434}
3435
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003436/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003437 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003438 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003439 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003440 * This option will set or get the SCTP partial delivery point. This
3441 * point is the size of a message where the partial delivery API will be
3442 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003443 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003444 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003445 * point. Note also that the call will fail if the user attempts to set
3446 * this value larger than the socket receive buffer size.
3447 *
3448 * Note that any single message having a length smaller than or equal to
3449 * the SCTP partial delivery point will be delivered in one single read
3450 * call as long as the user provided buffer is large enough to hold the
3451 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003452 */
3453static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3454 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003455 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003456{
3457 u32 val;
3458
3459 if (optlen != sizeof(u32))
3460 return -EINVAL;
3461 if (get_user(val, (int __user *)optval))
3462 return -EFAULT;
3463
Wei Yongjun8510b932008-12-25 16:59:03 -08003464 /* Note: We double the receive buffer from what the user sets
3465 * it to be, also initial rwnd is based on rcvbuf/2.
3466 */
3467 if (val > (sk->sk_rcvbuf >> 1))
3468 return -EINVAL;
3469
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003470 sctp_sk(sk)->pd_point = val;
3471
3472 return 0; /* is this the right error code? */
3473}
3474
Vlad Yasevich70331572007-03-23 11:34:36 -07003475/*
3476 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3477 *
3478 * This option will allow a user to change the maximum burst of packets
3479 * that can be emitted by this association. Note that the default value
3480 * is 4, and some implementations may restrict this setting so that it
3481 * can only be lowered.
3482 *
3483 * NOTE: This text doesn't seem right. Do this on a socket basis with
3484 * future associations inheriting the socket value.
3485 */
3486static int sctp_setsockopt_maxburst(struct sock *sk,
3487 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003488 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003489{
Neil Horman219b99a2008-03-05 13:44:46 -08003490 struct sctp_assoc_value params;
3491 struct sctp_sock *sp;
3492 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003493 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003494 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003495
Neil Horman219b99a2008-03-05 13:44:46 -08003496 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003497 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003498 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003499 "Use of int in max_burst socket option deprecated.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003500 "Use struct sctp_assoc_value instead\n",
3501 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08003502 if (copy_from_user(&val, optval, optlen))
3503 return -EFAULT;
3504 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3505 if (copy_from_user(&params, optval, optlen))
3506 return -EFAULT;
3507 val = params.assoc_value;
3508 assoc_id = params.assoc_id;
3509 } else
3510 return -EINVAL;
3511
3512 sp = sctp_sk(sk);
3513
3514 if (assoc_id != 0) {
3515 asoc = sctp_id2assoc(sk, assoc_id);
3516 if (!asoc)
3517 return -EINVAL;
3518 asoc->max_burst = val;
3519 } else
3520 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003521
3522 return 0;
3523}
3524
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003525/*
3526 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3527 *
3528 * This set option adds a chunk type that the user is requesting to be
3529 * received only in an authenticated way. Changes to the list of chunks
3530 * will only effect future associations on the socket.
3531 */
3532static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003533 char __user *optval,
3534 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003535{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003536 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003537 struct sctp_authchunk val;
3538
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003539 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003540 return -EACCES;
3541
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003542 if (optlen != sizeof(struct sctp_authchunk))
3543 return -EINVAL;
3544 if (copy_from_user(&val, optval, optlen))
3545 return -EFAULT;
3546
3547 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003548 case SCTP_CID_INIT:
3549 case SCTP_CID_INIT_ACK:
3550 case SCTP_CID_SHUTDOWN_COMPLETE:
3551 case SCTP_CID_AUTH:
3552 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003553 }
3554
3555 /* add this chunk id to the endpoint */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003556 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003557}
3558
3559/*
3560 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3561 *
3562 * This option gets or sets the list of HMAC algorithms that the local
3563 * endpoint requires the peer to use.
3564 */
3565static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003566 char __user *optval,
3567 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003568{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003569 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003570 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003571 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003572 int err;
3573
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003574 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003575 return -EACCES;
3576
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003577 if (optlen < sizeof(struct sctp_hmacalgo))
3578 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003579 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3580 SCTP_AUTH_NUM_HMACS * sizeof(u16));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003581
wangweidongcb3f8372013-12-23 12:16:50 +08003582 hmacs = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003583 if (IS_ERR(hmacs))
3584 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003585
Vlad Yasevichd9724052008-08-27 16:09:49 -07003586 idents = hmacs->shmac_num_idents;
3587 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3588 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003589 err = -EINVAL;
3590 goto out;
3591 }
3592
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003593 err = sctp_auth_ep_set_hmacs(ep, hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003594out:
3595 kfree(hmacs);
3596 return err;
3597}
3598
3599/*
3600 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3601 *
3602 * This option will set a shared secret key which is used to build an
3603 * association shared key.
3604 */
3605static int sctp_setsockopt_auth_key(struct sock *sk,
3606 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003607 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003608{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003609 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003610 struct sctp_authkey *authkey;
3611 struct sctp_association *asoc;
3612 int ret;
3613
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003614 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003615 return -EACCES;
3616
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003617 if (optlen <= sizeof(struct sctp_authkey))
3618 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003619 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3620 * this.
3621 */
3622 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3623 sizeof(struct sctp_authkey));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003624
wangweidongcb3f8372013-12-23 12:16:50 +08003625 authkey = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003626 if (IS_ERR(authkey))
3627 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003628
Vlad Yasevich328fc472008-08-27 16:08:54 -07003629 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003630 ret = -EINVAL;
3631 goto out;
3632 }
3633
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003634 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3635 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3636 ret = -EINVAL;
3637 goto out;
3638 }
3639
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003640 ret = sctp_auth_set_key(ep, asoc, authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003641out:
Daniel Borkmann6ba542a2013-02-08 03:04:34 +00003642 kzfree(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003643 return ret;
3644}
3645
3646/*
3647 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3648 *
3649 * This option will get or set the active shared key to be used to build
3650 * the association shared key.
3651 */
3652static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003653 char __user *optval,
3654 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003655{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003656 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003657 struct sctp_authkeyid val;
3658 struct sctp_association *asoc;
3659
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003660 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003661 return -EACCES;
3662
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003663 if (optlen != sizeof(struct sctp_authkeyid))
3664 return -EINVAL;
3665 if (copy_from_user(&val, optval, optlen))
3666 return -EFAULT;
3667
3668 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3669 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3670 return -EINVAL;
3671
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003672 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003673}
3674
3675/*
3676 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3677 *
3678 * This set option will delete a shared secret key from use.
3679 */
3680static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003681 char __user *optval,
3682 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003683{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003684 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003685 struct sctp_authkeyid val;
3686 struct sctp_association *asoc;
3687
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003688 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003689 return -EACCES;
3690
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003691 if (optlen != sizeof(struct sctp_authkeyid))
3692 return -EINVAL;
3693 if (copy_from_user(&val, optval, optlen))
3694 return -EFAULT;
3695
3696 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3697 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3698 return -EINVAL;
3699
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003700 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003701
3702}
3703
Michio Honda7dc04d72011-04-26 20:16:31 +09003704/*
Xin Long601590e2018-03-14 19:05:32 +08003705 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
3706 *
3707 * This set option will deactivate a shared secret key.
3708 */
3709static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
3710 unsigned int optlen)
3711{
3712 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3713 struct sctp_authkeyid val;
3714 struct sctp_association *asoc;
3715
3716 if (!ep->auth_enable)
3717 return -EACCES;
3718
3719 if (optlen != sizeof(struct sctp_authkeyid))
3720 return -EINVAL;
3721 if (copy_from_user(&val, optval, optlen))
3722 return -EFAULT;
3723
3724 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3725 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3726 return -EINVAL;
3727
3728 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3729}
3730
3731/*
Michio Honda7dc04d72011-04-26 20:16:31 +09003732 * 8.1.23 SCTP_AUTO_ASCONF
3733 *
3734 * This option will enable or disable the use of the automatic generation of
3735 * ASCONF chunks to add and delete addresses to an existing association. Note
3736 * that this option has two caveats namely: a) it only affects sockets that
3737 * are bound to all addresses available to the SCTP stack, and b) the system
3738 * administrator may have an overriding control that turns the ASCONF feature
3739 * off no matter what setting the socket option may have.
3740 * This option expects an integer boolean flag, where a non-zero value turns on
3741 * the option, and a zero value turns off the option.
3742 * Note. In this implementation, socket operation overrides default parameter
3743 * being set by sysctl as well as FreeBSD implementation
3744 */
3745static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3746 unsigned int optlen)
3747{
3748 int val;
3749 struct sctp_sock *sp = sctp_sk(sk);
3750
3751 if (optlen < sizeof(int))
3752 return -EINVAL;
3753 if (get_user(val, (int __user *)optval))
3754 return -EFAULT;
3755 if (!sctp_is_ep_boundall(sk) && val)
3756 return -EINVAL;
3757 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3758 return 0;
3759
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003760 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003761 if (val == 0 && sp->do_auto_asconf) {
3762 list_del(&sp->auto_asconf_list);
3763 sp->do_auto_asconf = 0;
3764 } else if (val && !sp->do_auto_asconf) {
3765 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00003766 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09003767 sp->do_auto_asconf = 1;
3768 }
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003769 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003770 return 0;
3771}
3772
Neil Horman5aa93bc2012-07-21 07:56:07 +00003773/*
3774 * SCTP_PEER_ADDR_THLDS
3775 *
3776 * This option allows us to alter the partially failed threshold for one or all
3777 * transports in an association. See Section 6.1 of:
3778 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3779 */
3780static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3781 char __user *optval,
3782 unsigned int optlen)
3783{
3784 struct sctp_paddrthlds val;
3785 struct sctp_transport *trans;
3786 struct sctp_association *asoc;
3787
3788 if (optlen < sizeof(struct sctp_paddrthlds))
3789 return -EINVAL;
3790 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3791 sizeof(struct sctp_paddrthlds)))
3792 return -EFAULT;
3793
3794
3795 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3796 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3797 if (!asoc)
3798 return -ENOENT;
3799 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3800 transports) {
3801 if (val.spt_pathmaxrxt)
3802 trans->pathmaxrxt = val.spt_pathmaxrxt;
3803 trans->pf_retrans = val.spt_pathpfthld;
3804 }
3805
3806 if (val.spt_pathmaxrxt)
3807 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3808 asoc->pf_retrans = val.spt_pathpfthld;
3809 } else {
3810 trans = sctp_addr_id2transport(sk, &val.spt_address,
3811 val.spt_assoc_id);
3812 if (!trans)
3813 return -ENOENT;
3814
3815 if (val.spt_pathmaxrxt)
3816 trans->pathmaxrxt = val.spt_pathmaxrxt;
3817 trans->pf_retrans = val.spt_pathpfthld;
3818 }
3819
3820 return 0;
3821}
3822
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02003823static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3824 char __user *optval,
3825 unsigned int optlen)
3826{
3827 int val;
3828
3829 if (optlen < sizeof(int))
3830 return -EINVAL;
3831 if (get_user(val, (int __user *) optval))
3832 return -EFAULT;
3833
3834 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3835
3836 return 0;
3837}
3838
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02003839static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3840 char __user *optval,
3841 unsigned int optlen)
3842{
3843 int val;
3844
3845 if (optlen < sizeof(int))
3846 return -EINVAL;
3847 if (get_user(val, (int __user *) optval))
3848 return -EFAULT;
3849
3850 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3851
3852 return 0;
3853}
3854
Xin Long28aa4c22016-07-09 19:47:40 +08003855static int sctp_setsockopt_pr_supported(struct sock *sk,
3856 char __user *optval,
3857 unsigned int optlen)
3858{
3859 struct sctp_assoc_value params;
3860 struct sctp_association *asoc;
3861 int retval = -EINVAL;
3862
3863 if (optlen != sizeof(params))
3864 goto out;
3865
3866 if (copy_from_user(&params, optval, optlen)) {
3867 retval = -EFAULT;
3868 goto out;
3869 }
3870
3871 asoc = sctp_id2assoc(sk, params.assoc_id);
3872 if (asoc) {
3873 asoc->prsctp_enable = !!params.assoc_value;
3874 } else if (!params.assoc_id) {
3875 struct sctp_sock *sp = sctp_sk(sk);
3876
3877 sp->ep->prsctp_enable = !!params.assoc_value;
3878 } else {
3879 goto out;
3880 }
3881
3882 retval = 0;
3883
3884out:
3885 return retval;
3886}
3887
Xin Longf959fb42016-07-09 19:47:41 +08003888static int sctp_setsockopt_default_prinfo(struct sock *sk,
3889 char __user *optval,
3890 unsigned int optlen)
3891{
3892 struct sctp_default_prinfo info;
3893 struct sctp_association *asoc;
3894 int retval = -EINVAL;
3895
3896 if (optlen != sizeof(info))
3897 goto out;
3898
3899 if (copy_from_user(&info, optval, sizeof(info))) {
3900 retval = -EFAULT;
3901 goto out;
3902 }
3903
3904 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
3905 goto out;
3906
3907 if (info.pr_policy == SCTP_PR_SCTP_NONE)
3908 info.pr_value = 0;
3909
3910 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
3911 if (asoc) {
3912 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
3913 asoc->default_timetolive = info.pr_value;
3914 } else if (!info.pr_assoc_id) {
3915 struct sctp_sock *sp = sctp_sk(sk);
3916
3917 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
3918 sp->default_timetolive = info.pr_value;
3919 } else {
3920 goto out;
3921 }
3922
3923 retval = 0;
3924
3925out:
3926 return retval;
3927}
3928
Xin Longc0d8bab2017-03-10 12:11:12 +08003929static int sctp_setsockopt_reconfig_supported(struct sock *sk,
3930 char __user *optval,
3931 unsigned int optlen)
3932{
3933 struct sctp_assoc_value params;
3934 struct sctp_association *asoc;
3935 int retval = -EINVAL;
3936
3937 if (optlen != sizeof(params))
3938 goto out;
3939
3940 if (copy_from_user(&params, optval, optlen)) {
3941 retval = -EFAULT;
3942 goto out;
3943 }
3944
3945 asoc = sctp_id2assoc(sk, params.assoc_id);
3946 if (asoc) {
3947 asoc->reconf_enable = !!params.assoc_value;
3948 } else if (!params.assoc_id) {
3949 struct sctp_sock *sp = sctp_sk(sk);
3950
3951 sp->ep->reconf_enable = !!params.assoc_value;
3952 } else {
3953 goto out;
3954 }
3955
3956 retval = 0;
3957
3958out:
3959 return retval;
3960}
3961
Xin Long9fb657a2017-01-18 00:44:46 +08003962static int sctp_setsockopt_enable_strreset(struct sock *sk,
3963 char __user *optval,
3964 unsigned int optlen)
3965{
3966 struct sctp_assoc_value params;
3967 struct sctp_association *asoc;
3968 int retval = -EINVAL;
3969
3970 if (optlen != sizeof(params))
3971 goto out;
3972
3973 if (copy_from_user(&params, optval, optlen)) {
3974 retval = -EFAULT;
3975 goto out;
3976 }
3977
3978 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
3979 goto out;
3980
3981 asoc = sctp_id2assoc(sk, params.assoc_id);
3982 if (asoc) {
3983 asoc->strreset_enable = params.assoc_value;
3984 } else if (!params.assoc_id) {
3985 struct sctp_sock *sp = sctp_sk(sk);
3986
3987 sp->ep->strreset_enable = params.assoc_value;
3988 } else {
3989 goto out;
3990 }
3991
3992 retval = 0;
3993
3994out:
3995 return retval;
3996}
3997
Xin Long7f9d68a2017-01-18 00:44:47 +08003998static int sctp_setsockopt_reset_streams(struct sock *sk,
3999 char __user *optval,
4000 unsigned int optlen)
4001{
4002 struct sctp_reset_streams *params;
4003 struct sctp_association *asoc;
4004 int retval = -EINVAL;
4005
Xin Long2342b8d2017-12-10 15:40:51 +08004006 if (optlen < sizeof(*params))
Xin Long7f9d68a2017-01-18 00:44:47 +08004007 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02004008 /* srs_number_streams is u16, so optlen can't be bigger than this. */
4009 optlen = min_t(unsigned int, optlen, USHRT_MAX +
4010 sizeof(__u16) * sizeof(*params));
Xin Long7f9d68a2017-01-18 00:44:47 +08004011
4012 params = memdup_user(optval, optlen);
4013 if (IS_ERR(params))
4014 return PTR_ERR(params);
4015
Xin Long2342b8d2017-12-10 15:40:51 +08004016 if (params->srs_number_streams * sizeof(__u16) >
4017 optlen - sizeof(*params))
4018 goto out;
4019
Xin Long7f9d68a2017-01-18 00:44:47 +08004020 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4021 if (!asoc)
4022 goto out;
4023
4024 retval = sctp_send_reset_streams(asoc, params);
4025
4026out:
4027 kfree(params);
4028 return retval;
4029}
4030
Xin Longa92ce1a2017-02-09 01:18:18 +08004031static int sctp_setsockopt_reset_assoc(struct sock *sk,
4032 char __user *optval,
4033 unsigned int optlen)
4034{
4035 struct sctp_association *asoc;
4036 sctp_assoc_t associd;
4037 int retval = -EINVAL;
4038
4039 if (optlen != sizeof(associd))
4040 goto out;
4041
4042 if (copy_from_user(&associd, optval, optlen)) {
4043 retval = -EFAULT;
4044 goto out;
4045 }
4046
4047 asoc = sctp_id2assoc(sk, associd);
4048 if (!asoc)
4049 goto out;
4050
4051 retval = sctp_send_reset_assoc(asoc);
4052
4053out:
4054 return retval;
4055}
4056
Xin Long242bd2d2017-02-09 01:18:20 +08004057static int sctp_setsockopt_add_streams(struct sock *sk,
4058 char __user *optval,
4059 unsigned int optlen)
4060{
4061 struct sctp_association *asoc;
4062 struct sctp_add_streams params;
4063 int retval = -EINVAL;
4064
4065 if (optlen != sizeof(params))
4066 goto out;
4067
4068 if (copy_from_user(&params, optval, optlen)) {
4069 retval = -EFAULT;
4070 goto out;
4071 }
4072
4073 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
4074 if (!asoc)
4075 goto out;
4076
4077 retval = sctp_send_add_streams(asoc, &params);
4078
4079out:
4080 return retval;
4081}
4082
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004083static int sctp_setsockopt_scheduler(struct sock *sk,
4084 char __user *optval,
4085 unsigned int optlen)
4086{
4087 struct sctp_association *asoc;
4088 struct sctp_assoc_value params;
4089 int retval = -EINVAL;
4090
4091 if (optlen < sizeof(params))
4092 goto out;
4093
4094 optlen = sizeof(params);
4095 if (copy_from_user(&params, optval, optlen)) {
4096 retval = -EFAULT;
4097 goto out;
4098 }
4099
4100 if (params.assoc_value > SCTP_SS_MAX)
4101 goto out;
4102
4103 asoc = sctp_id2assoc(sk, params.assoc_id);
4104 if (!asoc)
4105 goto out;
4106
4107 retval = sctp_sched_set_sched(asoc, params.assoc_value);
4108
4109out:
4110 return retval;
4111}
4112
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004113static int sctp_setsockopt_scheduler_value(struct sock *sk,
4114 char __user *optval,
4115 unsigned int optlen)
4116{
4117 struct sctp_association *asoc;
4118 struct sctp_stream_value params;
4119 int retval = -EINVAL;
4120
4121 if (optlen < sizeof(params))
4122 goto out;
4123
4124 optlen = sizeof(params);
4125 if (copy_from_user(&params, optval, optlen)) {
4126 retval = -EFAULT;
4127 goto out;
4128 }
4129
4130 asoc = sctp_id2assoc(sk, params.assoc_id);
4131 if (!asoc)
4132 goto out;
4133
4134 retval = sctp_sched_set_value(asoc, params.stream_id,
4135 params.stream_value, GFP_KERNEL);
4136
4137out:
4138 return retval;
4139}
4140
Xin Long772a5862017-12-08 21:03:58 +08004141static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4142 char __user *optval,
4143 unsigned int optlen)
4144{
4145 struct sctp_sock *sp = sctp_sk(sk);
4146 struct net *net = sock_net(sk);
4147 struct sctp_assoc_value params;
4148 int retval = -EINVAL;
4149
4150 if (optlen < sizeof(params))
4151 goto out;
4152
4153 optlen = sizeof(params);
4154 if (copy_from_user(&params, optval, optlen)) {
4155 retval = -EFAULT;
4156 goto out;
4157 }
4158
4159 if (params.assoc_id)
4160 goto out;
4161
4162 if (!net->sctp.intl_enable || !sp->frag_interleave) {
4163 retval = -EPERM;
4164 goto out;
4165 }
4166
4167 sp->strm_interleave = !!params.assoc_value;
4168
4169 retval = 0;
4170
4171out:
4172 return retval;
4173}
4174
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175/* API 6.2 setsockopt(), getsockopt()
4176 *
4177 * Applications use setsockopt() and getsockopt() to set or retrieve
4178 * socket options. Socket options are used to change the default
4179 * behavior of sockets calls. They are described in Section 7.
4180 *
4181 * The syntax is:
4182 *
4183 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4184 * int __user *optlen);
4185 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4186 * int optlen);
4187 *
4188 * sd - the socket descript.
4189 * level - set to IPPROTO_SCTP for all SCTP options.
4190 * optname - the option name.
4191 * optval - the buffer to store the value of the option.
4192 * optlen - the size of the buffer.
4193 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004194static int sctp_setsockopt(struct sock *sk, int level, int optname,
4195 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196{
4197 int retval = 0;
4198
Daniel Borkmannbb333812013-06-28 19:49:40 +02004199 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
4201 /* I can hardly begin to describe how wrong this is. This is
4202 * so broken as to be worse than useless. The API draft
4203 * REALLY is NOT helpful here... I am not convinced that the
4204 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4205 * are at all well-founded.
4206 */
4207 if (level != SOL_SCTP) {
4208 struct sctp_af *af = sctp_sk(sk)->pf->af;
4209 retval = af->setsockopt(sk, level, optname, optval, optlen);
4210 goto out_nounlock;
4211 }
4212
wangweidong048ed4b2014-01-21 15:44:11 +08004213 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
4215 switch (optname) {
4216 case SCTP_SOCKOPT_BINDX_ADD:
4217 /* 'optlen' is the size of the addresses buffer. */
4218 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4219 optlen, SCTP_BINDX_ADD_ADDR);
4220 break;
4221
4222 case SCTP_SOCKOPT_BINDX_REM:
4223 /* 'optlen' is the size of the addresses buffer. */
4224 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4225 optlen, SCTP_BINDX_REM_ADDR);
4226 break;
4227
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004228 case SCTP_SOCKOPT_CONNECTX_OLD:
4229 /* 'optlen' is the size of the addresses buffer. */
4230 retval = sctp_setsockopt_connectx_old(sk,
4231 (struct sockaddr __user *)optval,
4232 optlen);
4233 break;
4234
Frank Filz3f7a87d2005-06-20 13:14:57 -07004235 case SCTP_SOCKOPT_CONNECTX:
4236 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004237 retval = sctp_setsockopt_connectx(sk,
4238 (struct sockaddr __user *)optval,
4239 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004240 break;
4241
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 case SCTP_DISABLE_FRAGMENTS:
4243 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4244 break;
4245
4246 case SCTP_EVENTS:
4247 retval = sctp_setsockopt_events(sk, optval, optlen);
4248 break;
4249
4250 case SCTP_AUTOCLOSE:
4251 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4252 break;
4253
4254 case SCTP_PEER_ADDR_PARAMS:
4255 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4256 break;
4257
Shan Wei4580ccc2011-01-18 22:39:00 +00004258 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07004259 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08004260 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004261 case SCTP_PARTIAL_DELIVERY_POINT:
4262 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4263 break;
Frank Filz77086102005-12-22 11:37:30 -08004264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 case SCTP_INITMSG:
4266 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4267 break;
4268 case SCTP_DEFAULT_SEND_PARAM:
4269 retval = sctp_setsockopt_default_send_param(sk, optval,
4270 optlen);
4271 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02004272 case SCTP_DEFAULT_SNDINFO:
4273 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 case SCTP_PRIMARY_ADDR:
4276 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4277 break;
4278 case SCTP_SET_PEER_PRIMARY_ADDR:
4279 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4280 break;
4281 case SCTP_NODELAY:
4282 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4283 break;
4284 case SCTP_RTOINFO:
4285 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4286 break;
4287 case SCTP_ASSOCINFO:
4288 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4289 break;
4290 case SCTP_I_WANT_MAPPED_V4_ADDR:
4291 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4292 break;
4293 case SCTP_MAXSEG:
4294 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4295 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004296 case SCTP_ADAPTATION_LAYER:
4297 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004299 case SCTP_CONTEXT:
4300 retval = sctp_setsockopt_context(sk, optval, optlen);
4301 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004302 case SCTP_FRAGMENT_INTERLEAVE:
4303 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4304 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07004305 case SCTP_MAX_BURST:
4306 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4307 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004308 case SCTP_AUTH_CHUNK:
4309 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4310 break;
4311 case SCTP_HMAC_IDENT:
4312 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4313 break;
4314 case SCTP_AUTH_KEY:
4315 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4316 break;
4317 case SCTP_AUTH_ACTIVE_KEY:
4318 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4319 break;
4320 case SCTP_AUTH_DELETE_KEY:
4321 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4322 break;
Xin Long601590e2018-03-14 19:05:32 +08004323 case SCTP_AUTH_DEACTIVATE_KEY:
4324 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
4325 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09004326 case SCTP_AUTO_ASCONF:
4327 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4328 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00004329 case SCTP_PEER_ADDR_THLDS:
4330 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4331 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004332 case SCTP_RECVRCVINFO:
4333 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4334 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004335 case SCTP_RECVNXTINFO:
4336 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4337 break;
Xin Long28aa4c22016-07-09 19:47:40 +08004338 case SCTP_PR_SUPPORTED:
4339 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4340 break;
Xin Longf959fb42016-07-09 19:47:41 +08004341 case SCTP_DEFAULT_PRINFO:
4342 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4343 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08004344 case SCTP_RECONFIG_SUPPORTED:
4345 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4346 break;
Xin Long9fb657a2017-01-18 00:44:46 +08004347 case SCTP_ENABLE_STREAM_RESET:
4348 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4349 break;
Xin Long7f9d68a2017-01-18 00:44:47 +08004350 case SCTP_RESET_STREAMS:
4351 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4352 break;
Xin Longa92ce1a2017-02-09 01:18:18 +08004353 case SCTP_RESET_ASSOC:
4354 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4355 break;
Xin Long242bd2d2017-02-09 01:18:20 +08004356 case SCTP_ADD_STREAMS:
4357 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4358 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004359 case SCTP_STREAM_SCHEDULER:
4360 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4361 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004362 case SCTP_STREAM_SCHEDULER_VALUE:
4363 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4364 break;
Xin Long772a5862017-12-08 21:03:58 +08004365 case SCTP_INTERLEAVING_SUPPORTED:
4366 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4367 optlen);
4368 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 default:
4370 retval = -ENOPROTOOPT;
4371 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
wangweidong048ed4b2014-01-21 15:44:11 +08004374 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
4376out_nounlock:
4377 return retval;
4378}
4379
4380/* API 3.1.6 connect() - UDP Style Syntax
4381 *
4382 * An application may use the connect() call in the UDP model to initiate an
4383 * association without sending data.
4384 *
4385 * The syntax is:
4386 *
4387 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4388 *
4389 * sd: the socket descriptor to have a new association added to.
4390 *
4391 * nam: the address structure (either struct sockaddr_in or struct
4392 * sockaddr_in6 defined in RFC2553 [7]).
4393 *
4394 * len: the size of the address.
4395 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004396static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4397 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07004400 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
wangweidong048ed4b2014-01-21 15:44:11 +08004402 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
Daniel Borkmannbb333812013-06-28 19:49:40 +02004404 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4405 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406
Frank Filz3f7a87d2005-06-20 13:14:57 -07004407 /* Validate addr_len before calling common connect/connectx routine. */
4408 af = sctp_get_af_specific(addr->sa_family);
4409 if (!af || addr_len < af->sockaddr_len) {
4410 err = -EINVAL;
4411 } else {
4412 /* Pass correct addr len to common routine (so it knows there
4413 * is only one address being passed.
4414 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004415 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 }
4417
wangweidong048ed4b2014-01-21 15:44:11 +08004418 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 return err;
4420}
4421
4422/* FIXME: Write comments. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004423static int sctp_disconnect(struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424{
4425 return -EOPNOTSUPP; /* STUB */
4426}
4427
4428/* 4.1.4 accept() - TCP Style Syntax
4429 *
4430 * Applications use accept() call to remove an established SCTP
4431 * association from the accept queue of the endpoint. A new socket
4432 * descriptor will be returned from accept() to represent the newly
4433 * formed association.
4434 */
David Howellscdfbabf2017-03-09 08:09:05 +00004435static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436{
4437 struct sctp_sock *sp;
4438 struct sctp_endpoint *ep;
4439 struct sock *newsk = NULL;
4440 struct sctp_association *asoc;
4441 long timeo;
4442 int error = 0;
4443
wangweidong048ed4b2014-01-21 15:44:11 +08004444 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
4446 sp = sctp_sk(sk);
4447 ep = sp->ep;
4448
4449 if (!sctp_style(sk, TCP)) {
4450 error = -EOPNOTSUPP;
4451 goto out;
4452 }
4453
4454 if (!sctp_sstate(sk, LISTENING)) {
4455 error = -EINVAL;
4456 goto out;
4457 }
4458
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07004459 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
4461 error = sctp_wait_for_accept(sk, timeo);
4462 if (error)
4463 goto out;
4464
4465 /* We treat the list of associations on the endpoint as the accept
4466 * queue and pick the first association on the list.
4467 */
4468 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4469
David Howellscdfbabf2017-03-09 08:09:05 +00004470 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 if (!newsk) {
4472 error = -ENOMEM;
4473 goto out;
4474 }
4475
4476 /* Populate the fields of the newsk from the oldsk and migrate the
4477 * asoc to the newsk.
4478 */
4479 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4480
4481out:
wangweidong048ed4b2014-01-21 15:44:11 +08004482 release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004483 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 return newsk;
4485}
4486
4487/* The SCTP ioctl handler. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004488static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004490 int rc = -ENOTCONN;
4491
wangweidong048ed4b2014-01-21 15:44:11 +08004492 lock_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004493
4494 /*
4495 * SEQPACKET-style sockets in LISTENING state are valid, for
4496 * SCTP, so only discard TCP-style sockets in LISTENING state.
4497 */
4498 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4499 goto out;
4500
4501 switch (cmd) {
4502 case SIOCINQ: {
4503 struct sk_buff *skb;
4504 unsigned int amount = 0;
4505
4506 skb = skb_peek(&sk->sk_receive_queue);
4507 if (skb != NULL) {
4508 /*
4509 * We will only return the amount of this packet since
4510 * that is all that will be read.
4511 */
4512 amount = skb->len;
4513 }
4514 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004515 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07004516 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004517 default:
4518 rc = -ENOIOCTLCMD;
4519 break;
4520 }
4521out:
wangweidong048ed4b2014-01-21 15:44:11 +08004522 release_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004523 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524}
4525
4526/* This is the function which gets called during socket creation to
4527 * initialized the SCTP-specific portion of the sock.
4528 * The sock structure should already be zero-filled memory.
4529 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004530static int sctp_init_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004532 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 struct sctp_sock *sp;
4534
Daniel Borkmannbb333812013-06-28 19:49:40 +02004535 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536
4537 sp = sctp_sk(sk);
4538
4539 /* Initialize the SCTP per socket area. */
4540 switch (sk->sk_type) {
4541 case SOCK_SEQPACKET:
4542 sp->type = SCTP_SOCKET_UDP;
4543 break;
4544 case SOCK_STREAM:
4545 sp->type = SCTP_SOCKET_TCP;
4546 break;
4547 default:
4548 return -ESOCKTNOSUPPORT;
4549 }
4550
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004551 sk->sk_gso_type = SKB_GSO_SCTP;
4552
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 /* Initialize default send parameters. These parameters can be
4554 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4555 */
4556 sp->default_stream = 0;
4557 sp->default_ppid = 0;
4558 sp->default_flags = 0;
4559 sp->default_context = 0;
4560 sp->default_timetolive = 0;
4561
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004562 sp->default_rcv_context = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004563 sp->max_burst = net->sctp.max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004564
Neil Horman3c681982012-10-24 09:20:03 +00004565 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4566
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 /* Initialize default setup parameters. These parameters
4568 * can be modified with the SCTP_INITMSG socket option or
4569 * overridden by the SCTP_INIT CMSG.
4570 */
4571 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4572 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004573 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4574 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575
4576 /* Initialize default RTO related parameters. These parameters can
4577 * be modified for with the SCTP_RTOINFO socket option.
4578 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004579 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4580 sp->rtoinfo.srto_max = net->sctp.rto_max;
4581 sp->rtoinfo.srto_min = net->sctp.rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
4583 /* Initialize default association related parameters. These parameters
4584 * can be modified with the SCTP_ASSOCINFO socket option.
4585 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004586 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 sp->assocparams.sasoc_number_peer_destinations = 0;
4588 sp->assocparams.sasoc_peer_rwnd = 0;
4589 sp->assocparams.sasoc_local_rwnd = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004590 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
4592 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004593 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 */
4595 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4596
4597 /* Default Peer Address Parameters. These defaults can
4598 * be modified via SCTP_PEER_ADDR_PARAMS
4599 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004600 sp->hbinterval = net->sctp.hb_interval;
4601 sp->pathmaxrxt = net->sctp.max_retrans_path;
wangweidong4e2d52b2013-12-23 12:16:54 +08004602 sp->pathmtu = 0; /* allow default discovery */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004603 sp->sackdelay = net->sctp.sack_timeout;
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07004604 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004605 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004606 SPP_PMTUD_ENABLE |
4607 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
4609 /* If enabled no SCTP message fragmentation will be performed.
4610 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4611 */
4612 sp->disable_fragments = 0;
4613
Sridhar Samudrala208edef2006-09-29 17:08:01 -07004614 /* Enable Nagle algorithm by default. */
4615 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004617 sp->recvrcvinfo = 0;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004618 sp->recvnxtinfo = 0;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004619
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 /* Enable by default. */
4621 sp->v4mapped = 1;
4622
4623 /* Auto-close idle associations after the configured
4624 * number of seconds. A value of 0 disables this
4625 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4626 * for UDP-style sockets only.
4627 */
4628 sp->autoclose = 0;
4629
4630 /* User specified fragmentation limit. */
4631 sp->user_frag = 0;
4632
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004633 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
4635 sp->pf = sctp_get_pf_specific(sk->sk_family);
4636
4637 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004638 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004640 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
4642 /* Create a per socket endpoint structure. Even if we
4643 * change the data structure relationships, this may still
4644 * be useful for storing pre-connect address information.
4645 */
Daniel Borkmannc164b832013-06-14 18:24:06 +02004646 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4647 if (!sp->ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 return -ENOMEM;
4649
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 sp->hmac = NULL;
4651
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004652 sk->sk_destruct = sctp_destruct_sock;
4653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08004655
4656 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004657 sk_sockets_allocated_inc(sk);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004658 sock_prot_inuse_add(net, sk->sk_prot, 1);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004659
4660 /* Nothing can fail after this block, otherwise
4661 * sctp_destroy_sock() will be called without addr_wq_lock held
4662 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004663 if (net->sctp.default_auto_asconf) {
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004664 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda9f7d6532011-04-26 19:32:51 +09004665 list_add_tail(&sp->auto_asconf_list,
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004666 &net->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09004667 sp->do_auto_asconf = 1;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004668 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4669 } else {
Michio Honda9f7d6532011-04-26 19:32:51 +09004670 sp->do_auto_asconf = 0;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004671 }
4672
David S. Miller6f756a82008-11-23 17:34:03 -08004673 local_bh_enable();
4674
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 return 0;
4676}
4677
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004678/* Cleanup any SCTP per socket resources. Must be called with
4679 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4680 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004681static void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682{
Michio Honda9f7d6532011-04-26 19:32:51 +09004683 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
Daniel Borkmannbb333812013-06-28 19:49:40 +02004685 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
4687 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09004688 sp = sctp_sk(sk);
Daniel Borkmann1abd1652013-06-06 15:53:47 +02004689 /* This could happen during socket init, thus we bail out
4690 * early, since the rest of the below is not setup either.
4691 */
4692 if (sp->ep == NULL)
4693 return;
4694
Michio Honda9f7d6532011-04-26 19:32:51 +09004695 if (sp->do_auto_asconf) {
4696 sp->do_auto_asconf = 0;
4697 list_del(&sp->auto_asconf_list);
4698 }
4699 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004700 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004701 sk_sockets_allocated_dec(sk);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08004702 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004703 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704}
4705
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004706/* Triggered when there are no references on the socket anymore */
4707static void sctp_destruct_sock(struct sock *sk)
4708{
4709 struct sctp_sock *sp = sctp_sk(sk);
4710
4711 /* Free up the HMAC transform. */
Herbert Xu5821c762016-01-24 21:20:12 +08004712 crypto_free_shash(sp->hmac);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004713
4714 inet_sock_destruct(sk);
4715}
4716
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717/* API 4.1.7 shutdown() - TCP Style Syntax
4718 * int shutdown(int socket, int how);
4719 *
4720 * sd - the socket descriptor of the association to be closed.
4721 * how - Specifies the type of shutdown. The values are
4722 * as follows:
4723 * SHUT_RD
4724 * Disables further receive operations. No SCTP
4725 * protocol action is taken.
4726 * SHUT_WR
4727 * Disables further send operations, and initiates
4728 * the SCTP shutdown sequence.
4729 * SHUT_RDWR
4730 * Disables further send and receive operations
4731 * and initiates the SCTP shutdown sequence.
4732 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004733static void sctp_shutdown(struct sock *sk, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004735 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 struct sctp_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
4738 if (!sctp_style(sk, TCP))
4739 return;
4740
Xin Long5bf35dd2016-11-13 21:44:37 +08004741 ep = sctp_sk(sk)->ep;
4742 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4743 struct sctp_association *asoc;
4744
Yafang Shaocbabf462017-12-20 11:12:54 +08004745 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Xin Long5bf35dd2016-11-13 21:44:37 +08004746 asoc = list_entry(ep->asocs.next,
4747 struct sctp_association, asocs);
4748 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 }
4750}
4751
Xin Long52c52a62016-04-14 15:35:30 +08004752int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4753 struct sctp_info *info)
4754{
4755 struct sctp_transport *prim;
4756 struct list_head *pos;
4757 int mask;
4758
4759 memset(info, 0, sizeof(*info));
4760 if (!asoc) {
4761 struct sctp_sock *sp = sctp_sk(sk);
4762
4763 info->sctpi_s_autoclose = sp->autoclose;
4764 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4765 info->sctpi_s_pd_point = sp->pd_point;
4766 info->sctpi_s_nodelay = sp->nodelay;
4767 info->sctpi_s_disable_fragments = sp->disable_fragments;
4768 info->sctpi_s_v4mapped = sp->v4mapped;
4769 info->sctpi_s_frag_interleave = sp->frag_interleave;
Xin Long40eb90e92016-05-29 17:42:13 +08004770 info->sctpi_s_type = sp->type;
Xin Long52c52a62016-04-14 15:35:30 +08004771
4772 return 0;
4773 }
4774
4775 info->sctpi_tag = asoc->c.my_vtag;
4776 info->sctpi_state = asoc->state;
4777 info->sctpi_rwnd = asoc->a_rwnd;
4778 info->sctpi_unackdata = asoc->unack_data;
4779 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004780 info->sctpi_instrms = asoc->stream.incnt;
4781 info->sctpi_outstrms = asoc->stream.outcnt;
Xin Long52c52a62016-04-14 15:35:30 +08004782 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4783 info->sctpi_inqueue++;
4784 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4785 info->sctpi_outqueue++;
4786 info->sctpi_overall_error = asoc->overall_error_count;
4787 info->sctpi_max_burst = asoc->max_burst;
4788 info->sctpi_maxseg = asoc->frag_point;
4789 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4790 info->sctpi_peer_tag = asoc->c.peer_vtag;
4791
4792 mask = asoc->peer.ecn_capable << 1;
4793 mask = (mask | asoc->peer.ipv4_address) << 1;
4794 mask = (mask | asoc->peer.ipv6_address) << 1;
4795 mask = (mask | asoc->peer.hostname_address) << 1;
4796 mask = (mask | asoc->peer.asconf_capable) << 1;
4797 mask = (mask | asoc->peer.prsctp_capable) << 1;
4798 mask = (mask | asoc->peer.auth_capable);
4799 info->sctpi_peer_capable = mask;
4800 mask = asoc->peer.sack_needed << 1;
4801 mask = (mask | asoc->peer.sack_generation) << 1;
4802 mask = (mask | asoc->peer.zero_window_announced);
4803 info->sctpi_peer_sack = mask;
4804
4805 info->sctpi_isacks = asoc->stats.isacks;
4806 info->sctpi_osacks = asoc->stats.osacks;
4807 info->sctpi_opackets = asoc->stats.opackets;
4808 info->sctpi_ipackets = asoc->stats.ipackets;
4809 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4810 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4811 info->sctpi_idupchunks = asoc->stats.idupchunks;
4812 info->sctpi_gapcnt = asoc->stats.gapcnt;
4813 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4814 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4815 info->sctpi_oodchunks = asoc->stats.oodchunks;
4816 info->sctpi_iodchunks = asoc->stats.iodchunks;
4817 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4818 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4819
4820 prim = asoc->peer.primary_path;
Stefano Brivioee6c88b2017-08-23 13:27:13 +02004821 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
Xin Long52c52a62016-04-14 15:35:30 +08004822 info->sctpi_p_state = prim->state;
4823 info->sctpi_p_cwnd = prim->cwnd;
4824 info->sctpi_p_srtt = prim->srtt;
4825 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4826 info->sctpi_p_hbinterval = prim->hbinterval;
4827 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4828 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4829 info->sctpi_p_ssthresh = prim->ssthresh;
4830 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4831 info->sctpi_p_flight_size = prim->flight_size;
4832 info->sctpi_p_error = prim->error_count;
4833
4834 return 0;
4835}
4836EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4837
Xin Long626d16f2016-04-14 15:35:31 +08004838/* use callback to avoid exporting the core structure */
Tom Herbert97a6ec42017-12-04 10:31:41 -08004839void sctp_transport_walk_start(struct rhashtable_iter *iter)
Xin Long626d16f2016-04-14 15:35:31 +08004840{
Xin Long7fda702f2016-11-15 23:23:11 +08004841 rhltable_walk_enter(&sctp_transport_hashtable, iter);
Xin Long626d16f2016-04-14 15:35:31 +08004842
Tom Herbert97a6ec42017-12-04 10:31:41 -08004843 rhashtable_walk_start(iter);
Xin Long626d16f2016-04-14 15:35:31 +08004844}
4845
4846void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4847{
4848 rhashtable_walk_stop(iter);
4849 rhashtable_walk_exit(iter);
4850}
4851
4852struct sctp_transport *sctp_transport_get_next(struct net *net,
4853 struct rhashtable_iter *iter)
4854{
4855 struct sctp_transport *t;
4856
4857 t = rhashtable_walk_next(iter);
4858 for (; t; t = rhashtable_walk_next(iter)) {
4859 if (IS_ERR(t)) {
4860 if (PTR_ERR(t) == -EAGAIN)
4861 continue;
4862 break;
4863 }
4864
4865 if (net_eq(sock_net(t->asoc->base.sk), net) &&
4866 t->asoc->peer.primary_path == t)
4867 break;
4868 }
4869
4870 return t;
4871}
4872
4873struct sctp_transport *sctp_transport_get_idx(struct net *net,
4874 struct rhashtable_iter *iter,
4875 int pos)
4876{
4877 void *obj = SEQ_START_TOKEN;
4878
4879 while (pos && (obj = sctp_transport_get_next(net, iter)) &&
4880 !IS_ERR(obj))
4881 pos--;
4882
4883 return obj;
4884}
4885
4886int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
4887 void *p) {
4888 int err = 0;
4889 int hash = 0;
4890 struct sctp_ep_common *epb;
4891 struct sctp_hashbucket *head;
4892
4893 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
4894 hash++, head++) {
Xin Long581409d2017-06-10 14:48:14 +08004895 read_lock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004896 sctp_for_each_hentry(epb, &head->chain) {
4897 err = cb(sctp_ep(epb), p);
4898 if (err)
4899 break;
4900 }
Xin Long581409d2017-06-10 14:48:14 +08004901 read_unlock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004902 }
4903
4904 return err;
4905}
4906EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
4907
4908int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
4909 struct net *net,
4910 const union sctp_addr *laddr,
4911 const union sctp_addr *paddr, void *p)
4912{
4913 struct sctp_transport *transport;
Xin Long08abb792016-12-15 23:05:52 +08004914 int err;
Xin Long626d16f2016-04-14 15:35:31 +08004915
4916 rcu_read_lock();
4917 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
Xin Long626d16f2016-04-14 15:35:31 +08004918 rcu_read_unlock();
Xin Long08abb792016-12-15 23:05:52 +08004919 if (!transport)
4920 return -ENOENT;
4921
Xin Long1cceda782016-09-29 02:55:44 +08004922 err = cb(transport, p);
Xin Longcd26da42016-10-31 20:32:31 +08004923 sctp_transport_put(transport);
Xin Long1cceda782016-09-29 02:55:44 +08004924
Xin Long626d16f2016-04-14 15:35:31 +08004925 return err;
4926}
4927EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
4928
4929int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
Xin Longd25adbe2017-09-15 11:02:21 +08004930 int (*cb_done)(struct sctp_transport *, void *),
4931 struct net *net, int *pos, void *p) {
Xin Long626d16f2016-04-14 15:35:31 +08004932 struct rhashtable_iter hti;
Xin Longd25adbe2017-09-15 11:02:21 +08004933 struct sctp_transport *tsp;
4934 int ret;
Xin Long626d16f2016-04-14 15:35:31 +08004935
Xin Longd25adbe2017-09-15 11:02:21 +08004936again:
Xin Longf53d77e2018-01-23 18:22:25 +08004937 ret = 0;
Tom Herbert97a6ec42017-12-04 10:31:41 -08004938 sctp_transport_walk_start(&hti);
Xin Long626d16f2016-04-14 15:35:31 +08004939
Xin Longd25adbe2017-09-15 11:02:21 +08004940 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
4941 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
4942 if (!sctp_transport_hold(tsp))
Xin Long626d16f2016-04-14 15:35:31 +08004943 continue;
Xin Longd25adbe2017-09-15 11:02:21 +08004944 ret = cb(tsp, p);
4945 if (ret)
Xin Long626d16f2016-04-14 15:35:31 +08004946 break;
Xin Longd25adbe2017-09-15 11:02:21 +08004947 (*pos)++;
4948 sctp_transport_put(tsp);
Xin Long626d16f2016-04-14 15:35:31 +08004949 }
Xin Long626d16f2016-04-14 15:35:31 +08004950 sctp_transport_walk_stop(&hti);
Xin Long53fa1032016-04-14 15:35:35 +08004951
Xin Longd25adbe2017-09-15 11:02:21 +08004952 if (ret) {
4953 if (cb_done && !cb_done(tsp, p)) {
4954 (*pos)++;
4955 sctp_transport_put(tsp);
4956 goto again;
4957 }
4958 sctp_transport_put(tsp);
4959 }
4960
4961 return ret;
Xin Long626d16f2016-04-14 15:35:31 +08004962}
4963EXPORT_SYMBOL_GPL(sctp_for_each_transport);
4964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965/* 7.2.1 Association Status (SCTP_STATUS)
4966
4967 * Applications can retrieve current status information about an
4968 * association, including association state, peer receiver window size,
4969 * number of unacked data chunks, and number of data chunks pending
4970 * receipt. This information is read-only.
4971 */
4972static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4973 char __user *optval,
4974 int __user *optlen)
4975{
4976 struct sctp_status status;
4977 struct sctp_association *asoc = NULL;
4978 struct sctp_transport *transport;
4979 sctp_assoc_t associd;
4980 int retval = 0;
4981
Neil Horman408f22e2007-06-16 14:03:45 -04004982 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 retval = -EINVAL;
4984 goto out;
4985 }
4986
Neil Horman408f22e2007-06-16 14:03:45 -04004987 len = sizeof(status);
4988 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 retval = -EFAULT;
4990 goto out;
4991 }
4992
4993 associd = status.sstat_assoc_id;
4994 asoc = sctp_id2assoc(sk, associd);
4995 if (!asoc) {
4996 retval = -EINVAL;
4997 goto out;
4998 }
4999
5000 transport = asoc->peer.primary_path;
5001
5002 status.sstat_assoc_id = sctp_assoc2id(asoc);
Daniel Borkmann38ab1fa2014-08-28 15:28:26 +02005003 status.sstat_state = sctp_assoc_to_state(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 status.sstat_rwnd = asoc->peer.rwnd;
5005 status.sstat_unackdata = asoc->unack_data;
5006
5007 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08005008 status.sstat_instrms = asoc->stream.incnt;
5009 status.sstat_outstrms = asoc->stream.outcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 status.sstat_fragmentation_point = asoc->frag_point;
5011 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08005012 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5013 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 /* Map ipv4 address into v4-mapped-on-v6 address. */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005015 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005017 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 status.sstat_primary.spinfo_cwnd = transport->cwnd;
5019 status.sstat_primary.spinfo_srtt = transport->srtt;
5020 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005021 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
Frank Filz3f7a87d2005-06-20 13:14:57 -07005023 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5024 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5025
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 if (put_user(len, optlen)) {
5027 retval = -EFAULT;
5028 goto out;
5029 }
5030
Daniel Borkmannbb333812013-06-28 19:49:40 +02005031 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5032 __func__, len, status.sstat_state, status.sstat_rwnd,
5033 status.sstat_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
5035 if (copy_to_user(optval, &status, len)) {
5036 retval = -EFAULT;
5037 goto out;
5038 }
5039
5040out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00005041 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042}
5043
5044
5045/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5046 *
5047 * Applications can retrieve information about a specific peer address
5048 * of an association, including its reachability state, congestion
5049 * window, and retransmission timer values. This information is
5050 * read-only.
5051 */
5052static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5053 char __user *optval,
5054 int __user *optlen)
5055{
5056 struct sctp_paddrinfo pinfo;
5057 struct sctp_transport *transport;
5058 int retval = 0;
5059
Neil Horman408f22e2007-06-16 14:03:45 -04005060 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 retval = -EINVAL;
5062 goto out;
5063 }
5064
Neil Horman408f22e2007-06-16 14:03:45 -04005065 len = sizeof(pinfo);
5066 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 retval = -EFAULT;
5068 goto out;
5069 }
5070
5071 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5072 pinfo.spinfo_assoc_id);
5073 if (!transport)
5074 return -EINVAL;
5075
5076 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005077 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 pinfo.spinfo_cwnd = transport->cwnd;
5079 pinfo.spinfo_srtt = transport->srtt;
5080 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005081 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082
Frank Filz3f7a87d2005-06-20 13:14:57 -07005083 if (pinfo.spinfo_state == SCTP_UNKNOWN)
5084 pinfo.spinfo_state = SCTP_ACTIVE;
5085
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 if (put_user(len, optlen)) {
5087 retval = -EFAULT;
5088 goto out;
5089 }
5090
5091 if (copy_to_user(optval, &pinfo, len)) {
5092 retval = -EFAULT;
5093 goto out;
5094 }
5095
5096out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00005097 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098}
5099
5100/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5101 *
5102 * This option is a on/off flag. If enabled no SCTP message
5103 * fragmentation will be performed. Instead if a message being sent
5104 * exceeds the current PMTU size, the message will NOT be sent and
5105 * instead a error will be indicated to the user.
5106 */
5107static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5108 char __user *optval, int __user *optlen)
5109{
5110 int val;
5111
5112 if (len < sizeof(int))
5113 return -EINVAL;
5114
5115 len = sizeof(int);
5116 val = (sctp_sk(sk)->disable_fragments == 1);
5117 if (put_user(len, optlen))
5118 return -EFAULT;
5119 if (copy_to_user(optval, &val, len))
5120 return -EFAULT;
5121 return 0;
5122}
5123
5124/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5125 *
5126 * This socket option is used to specify various notifications and
5127 * ancillary data the user wishes to receive.
5128 */
5129static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5130 int __user *optlen)
5131{
Jiri Slabya4b8e712016-10-21 14:13:24 +02005132 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00005134 if (len > sizeof(struct sctp_event_subscribe))
5135 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04005136 if (put_user(len, optlen))
5137 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
5139 return -EFAULT;
5140 return 0;
5141}
5142
5143/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5144 *
5145 * This socket option is applicable to the UDP-style socket only. When
5146 * set it will cause associations that are idle for more than the
5147 * specified number of seconds to automatically close. An association
5148 * being idle is defined an association that has NOT sent or received
5149 * user data. The special value of '0' indicates that no automatic
5150 * close of any associations should be performed. The option expects an
5151 * integer defining the number of seconds of idle time before an
5152 * association is closed.
5153 */
5154static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5155{
5156 /* Applicable to UDP-style socket only */
5157 if (sctp_style(sk, TCP))
5158 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04005159 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005161 len = sizeof(int);
5162 if (put_user(len, optlen))
5163 return -EFAULT;
David Windsorb2ce04c2017-06-10 22:50:43 -04005164 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 return -EFAULT;
5166 return 0;
5167}
5168
5169/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00005170int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171{
Benjamin Poirier0343c552012-03-08 05:55:58 +00005172 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005173 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 struct socket *sock;
5175 int err = 0;
5176
Xin Longdf80cd92017-10-17 23:26:10 +08005177 /* Do not peel off from one netns to another one. */
5178 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5179 return -EINVAL;
5180
Benjamin Poirier0343c552012-03-08 05:55:58 +00005181 if (!asoc)
5182 return -EINVAL;
5183
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 /* An association cannot be branched off from an already peeled-off
5185 * socket, nor is this supported for tcp style sockets.
5186 */
5187 if (!sctp_style(sk, UDP))
5188 return -EINVAL;
5189
5190 /* Create a new socket. */
5191 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5192 if (err < 0)
5193 return err;
5194
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005195 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005196
5197 /* Make peeled-off sockets more like 1-1 accepted sockets.
Richard Hainesb7e10c22018-02-24 16:18:51 +00005198 * Set the daddr and initialize id to something more random and also
5199 * copy over any ip options.
Vlad Yasevich4f444302006-10-30 18:54:32 -08005200 */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005201 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
Richard Hainesb7e10c22018-02-24 16:18:51 +00005202 sp->pf->copy_ip_options(sk, sock->sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005203
5204 /* Populate the fields of the newsk from the oldsk and migrate the
5205 * asoc to the newsk.
5206 */
5207 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005208
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 *sockp = sock;
5210
5211 return err;
5212}
Benjamin Poirier0343c552012-03-08 05:55:58 +00005213EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005215static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5216 struct file **newfile, unsigned flags)
5217{
5218 struct socket *newsock;
5219 int retval;
5220
5221 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5222 if (retval < 0)
5223 goto out;
5224
5225 /* Map the socket to an unused fd that can be returned to the user. */
5226 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5227 if (retval < 0) {
5228 sock_release(newsock);
5229 goto out;
5230 }
5231
5232 *newfile = sock_alloc_file(newsock, 0, NULL);
5233 if (IS_ERR(*newfile)) {
5234 put_unused_fd(retval);
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005235 retval = PTR_ERR(*newfile);
5236 *newfile = NULL;
5237 return retval;
5238 }
5239
5240 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5241 retval);
5242
5243 peeloff->sd = retval;
5244
5245 if (flags & SOCK_NONBLOCK)
5246 (*newfile)->f_flags |= O_NONBLOCK;
5247out:
5248 return retval;
5249}
5250
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5252{
5253 sctp_peeloff_arg_t peeloff;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005254 struct file *newfile = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
Neil Horman408f22e2007-06-16 14:03:45 -04005257 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005259 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 if (copy_from_user(&peeloff, optval, len))
5261 return -EFAULT;
5262
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005263 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 if (retval < 0)
5265 goto out;
5266
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04005268 if (put_user(len, optlen)) {
5269 fput(newfile);
5270 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04005271 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04005272 }
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005273
5274 if (copy_to_user(optval, &peeloff, len)) {
5275 fput(newfile);
5276 put_unused_fd(retval);
5277 return -EFAULT;
5278 }
5279 fd_install(retval, newfile);
5280out:
5281 return retval;
5282}
5283
5284static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5285 char __user *optval, int __user *optlen)
5286{
5287 sctp_peeloff_flags_arg_t peeloff;
5288 struct file *newfile = NULL;
5289 int retval = 0;
5290
5291 if (len < sizeof(sctp_peeloff_flags_arg_t))
5292 return -EINVAL;
5293 len = sizeof(sctp_peeloff_flags_arg_t);
5294 if (copy_from_user(&peeloff, optval, len))
5295 return -EFAULT;
5296
5297 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5298 &newfile, peeloff.flags);
5299 if (retval < 0)
5300 goto out;
5301
5302 /* Return the fd mapped to the new socket. */
5303 if (put_user(len, optlen)) {
5304 fput(newfile);
5305 put_unused_fd(retval);
5306 return -EFAULT;
5307 }
5308
Al Viro56b31d12012-08-18 00:25:51 -04005309 if (copy_to_user(optval, &peeloff, len)) {
5310 fput(newfile);
5311 put_unused_fd(retval);
5312 return -EFAULT;
5313 }
5314 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315out:
5316 return retval;
5317}
5318
5319/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5320 *
5321 * Applications can enable or disable heartbeats for any peer address of
5322 * an association, modify an address's heartbeat interval, force a
5323 * heartbeat to be sent immediately, and adjust the address's maximum
5324 * number of retransmissions sent before an address is considered
5325 * unreachable. The following structure is used to access and modify an
5326 * address's parameters:
5327 *
5328 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005329 * sctp_assoc_t spp_assoc_id;
5330 * struct sockaddr_storage spp_address;
5331 * uint32_t spp_hbinterval;
5332 * uint16_t spp_pathmaxrxt;
5333 * uint32_t spp_pathmtu;
5334 * uint32_t spp_sackdelay;
5335 * uint32_t spp_flags;
5336 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08005338 * spp_assoc_id - (one-to-many style socket) This is filled in the
5339 * application, and identifies the association for
5340 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 * spp_address - This specifies which address is of interest.
5342 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005343 * in milliseconds. If a value of zero
5344 * is present in this field then no changes are to
5345 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 * spp_pathmaxrxt - This contains the maximum number of
5347 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08005348 * considered unreachable. If a value of zero
5349 * is present in this field then no changes are to
5350 * be made to this parameter.
5351 * spp_pathmtu - When Path MTU discovery is disabled the value
5352 * specified here will be the "fixed" path mtu.
5353 * Note that if the spp_address field is empty
5354 * then all associations on this address will
5355 * have this fixed path mtu set upon them.
5356 *
5357 * spp_sackdelay - When delayed sack is enabled, this value specifies
5358 * the number of milliseconds that sacks will be delayed
5359 * for. This value will apply to all addresses of an
5360 * association if the spp_address field is empty. Note
5361 * also, that if delayed sack is enabled and this
5362 * value is set to 0, no change is made to the last
5363 * recorded delayed sack timer value.
5364 *
5365 * spp_flags - These flags are used to control various features
5366 * on an association. The flag field may contain
5367 * zero or more of the following options.
5368 *
5369 * SPP_HB_ENABLE - Enable heartbeats on the
5370 * specified address. Note that if the address
5371 * field is empty all addresses for the association
5372 * have heartbeats enabled upon them.
5373 *
5374 * SPP_HB_DISABLE - Disable heartbeats on the
5375 * speicifed address. Note that if the address
5376 * field is empty all addresses for the association
5377 * will have their heartbeats disabled. Note also
5378 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5379 * mutually exclusive, only one of these two should
5380 * be specified. Enabling both fields will have
5381 * undetermined results.
5382 *
5383 * SPP_HB_DEMAND - Request a user initiated heartbeat
5384 * to be made immediately.
5385 *
5386 * SPP_PMTUD_ENABLE - This field will enable PMTU
5387 * discovery upon the specified address. Note that
5388 * if the address feild is empty then all addresses
5389 * on the association are effected.
5390 *
5391 * SPP_PMTUD_DISABLE - This field will disable PMTU
5392 * discovery upon the specified address. Note that
5393 * if the address feild is empty then all addresses
5394 * on the association are effected. Not also that
5395 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5396 * exclusive. Enabling both will have undetermined
5397 * results.
5398 *
5399 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5400 * on delayed sack. The time specified in spp_sackdelay
5401 * is used to specify the sack delay for this address. Note
5402 * that if spp_address is empty then all addresses will
5403 * enable delayed sack and take on the sack delay
5404 * value specified in spp_sackdelay.
5405 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5406 * off delayed sack. If the spp_address field is blank then
5407 * delayed sack is disabled for the entire association. Note
5408 * also that this field is mutually exclusive to
5409 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5410 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 */
5412static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005413 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414{
Frank Filz52ccb8e2005-12-22 11:36:46 -08005415 struct sctp_paddrparams params;
5416 struct sctp_transport *trans = NULL;
5417 struct sctp_association *asoc = NULL;
5418 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419
Neil Horman408f22e2007-06-16 14:03:45 -04005420 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005422 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 if (copy_from_user(&params, optval, len))
5424 return -EFAULT;
5425
Frank Filz52ccb8e2005-12-22 11:36:46 -08005426 /* If an address other than INADDR_ANY is specified, and
5427 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 */
wangweidongcb3f8372013-12-23 12:16:50 +08005429 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005430 trans = sctp_addr_id2transport(sk, &params.spp_address,
5431 params.spp_assoc_id);
5432 if (!trans) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005433 pr_debug("%s: failed no transport\n", __func__);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005434 return -EINVAL;
5435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 }
5437
Frank Filz52ccb8e2005-12-22 11:36:46 -08005438 /* Get association, if assoc_id != 0 and the socket is a one
5439 * to many style socket, and an association was not found, then
5440 * the id was invalid.
5441 */
5442 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5443 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005444 pr_debug("%s: failed no association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08005446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
Frank Filz52ccb8e2005-12-22 11:36:46 -08005448 if (trans) {
5449 /* Fetch transport values. */
5450 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5451 params.spp_pathmtu = trans->pathmtu;
5452 params.spp_pathmaxrxt = trans->pathmaxrxt;
5453 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
Frank Filz52ccb8e2005-12-22 11:36:46 -08005455 /*draft-11 doesn't say what to return in spp_flags*/
5456 params.spp_flags = trans->param_flags;
5457 } else if (asoc) {
5458 /* Fetch association values. */
5459 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5460 params.spp_pathmtu = asoc->pathmtu;
5461 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5462 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463
Frank Filz52ccb8e2005-12-22 11:36:46 -08005464 /*draft-11 doesn't say what to return in spp_flags*/
5465 params.spp_flags = asoc->param_flags;
5466 } else {
5467 /* Fetch socket values. */
5468 params.spp_hbinterval = sp->hbinterval;
5469 params.spp_pathmtu = sp->pathmtu;
5470 params.spp_sackdelay = sp->sackdelay;
5471 params.spp_pathmaxrxt = sp->pathmaxrxt;
5472
5473 /*draft-11 doesn't say what to return in spp_flags*/
5474 params.spp_flags = sp->param_flags;
5475 }
5476
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 if (copy_to_user(optval, &params, len))
5478 return -EFAULT;
5479
5480 if (put_user(len, optlen))
5481 return -EFAULT;
5482
5483 return 0;
5484}
5485
Wei Yongjund364d922008-05-09 15:13:26 -07005486/*
5487 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08005488 *
Wei Yongjund364d922008-05-09 15:13:26 -07005489 * This option will effect the way delayed acks are performed. This
5490 * option allows you to get or set the delayed ack time, in
5491 * milliseconds. It also allows changing the delayed ack frequency.
5492 * Changing the frequency to 1 disables the delayed sack algorithm. If
5493 * the assoc_id is 0, then this sets or gets the endpoints default
5494 * values. If the assoc_id field is non-zero, then the set or get
5495 * effects the specified association for the one to many model (the
5496 * assoc_id field is ignored by the one to one model). Note that if
5497 * sack_delay or sack_freq are 0 when setting this option, then the
5498 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08005499 *
Wei Yongjund364d922008-05-09 15:13:26 -07005500 * struct sctp_sack_info {
5501 * sctp_assoc_t sack_assoc_id;
5502 * uint32_t sack_delay;
5503 * uint32_t sack_freq;
5504 * };
Frank Filz77086102005-12-22 11:37:30 -08005505 *
Wei Yongjund364d922008-05-09 15:13:26 -07005506 * sack_assoc_id - This parameter, indicates which association the user
5507 * is performing an action upon. Note that if this field's value is
5508 * zero then the endpoints default value is changed (effecting future
5509 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08005510 *
Wei Yongjund364d922008-05-09 15:13:26 -07005511 * sack_delay - This parameter contains the number of milliseconds that
5512 * the user is requesting the delayed ACK timer be set to. Note that
5513 * this value is defined in the standard to be between 200 and 500
5514 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08005515 *
Wei Yongjund364d922008-05-09 15:13:26 -07005516 * sack_freq - This parameter contains the number of packets that must
5517 * be received before a sack is sent without waiting for the delay
5518 * timer to expire. The default value for this is 2, setting this
5519 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08005520 */
Wei Yongjund364d922008-05-09 15:13:26 -07005521static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08005522 char __user *optval,
5523 int __user *optlen)
5524{
Wei Yongjund364d922008-05-09 15:13:26 -07005525 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08005526 struct sctp_association *asoc = NULL;
5527 struct sctp_sock *sp = sctp_sk(sk);
5528
Wei Yongjund364d922008-05-09 15:13:26 -07005529 if (len >= sizeof(struct sctp_sack_info)) {
5530 len = sizeof(struct sctp_sack_info);
5531
5532 if (copy_from_user(&params, optval, len))
5533 return -EFAULT;
5534 } else if (len == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05005535 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005536 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005537 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005538 "Use struct sctp_sack_info instead\n",
5539 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07005540 if (copy_from_user(&params, optval, len))
5541 return -EFAULT;
5542 } else
wangweidongcb3f8372013-12-23 12:16:50 +08005543 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08005544
Wei Yongjund364d922008-05-09 15:13:26 -07005545 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08005546 * to many style socket, and an association was not found, then
5547 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005548 */
Wei Yongjund364d922008-05-09 15:13:26 -07005549 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5550 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08005551 return -EINVAL;
5552
5553 if (asoc) {
5554 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005555 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5556 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08005557 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07005558 params.sack_freq = asoc->sackfreq;
5559
5560 } else {
5561 params.sack_delay = 0;
5562 params.sack_freq = 1;
5563 }
Frank Filz77086102005-12-22 11:37:30 -08005564 } else {
5565 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005566 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5567 params.sack_delay = sp->sackdelay;
5568 params.sack_freq = sp->sackfreq;
5569 } else {
5570 params.sack_delay = 0;
5571 params.sack_freq = 1;
5572 }
Frank Filz77086102005-12-22 11:37:30 -08005573 }
5574
5575 if (copy_to_user(optval, &params, len))
5576 return -EFAULT;
5577
5578 if (put_user(len, optlen))
5579 return -EFAULT;
5580
5581 return 0;
5582}
5583
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5585 *
5586 * Applications can specify protocol parameters for the default association
5587 * initialization. The option name argument to setsockopt() and getsockopt()
5588 * is SCTP_INITMSG.
5589 *
5590 * Setting initialization parameters is effective only on an unconnected
5591 * socket (for UDP-style sockets only future associations are effected
5592 * by the change). With TCP-style sockets, this option is inherited by
5593 * sockets derived from a listener socket.
5594 */
5595static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5596{
Neil Horman408f22e2007-06-16 14:03:45 -04005597 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005599 len = sizeof(struct sctp_initmsg);
5600 if (put_user(len, optlen))
5601 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5603 return -EFAULT;
5604 return 0;
5605}
5606
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005608static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5609 char __user *optval, int __user *optlen)
5610{
5611 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005612 int cnt = 0;
5613 struct sctp_getaddrs getaddrs;
5614 struct sctp_transport *from;
5615 void __user *to;
5616 union sctp_addr temp;
5617 struct sctp_sock *sp = sctp_sk(sk);
5618 int addrlen;
5619 size_t space_left;
5620 int bytes_copied;
5621
5622 if (len < sizeof(struct sctp_getaddrs))
5623 return -EINVAL;
5624
5625 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5626 return -EFAULT;
5627
5628 /* For UDP-style sockets, id specifies the association to query. */
5629 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5630 if (!asoc)
5631 return -EINVAL;
5632
wangweidongcb3f8372013-12-23 12:16:50 +08005633 to = optval + offsetof(struct sctp_getaddrs, addrs);
5634 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005635
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07005636 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5637 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08005638 memcpy(&temp, &from->ipaddr, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005639 addrlen = sctp_get_pf_specific(sk->sk_family)
5640 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005641 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005642 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005643 if (copy_to_user(to, &temp, addrlen))
5644 return -EFAULT;
5645 to += addrlen;
5646 cnt++;
5647 space_left -= addrlen;
5648 }
5649
5650 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5651 return -EFAULT;
5652 bytes_copied = ((char __user *)to) - optval;
5653 if (put_user(bytes_copied, optlen))
5654 return -EFAULT;
5655
5656 return 0;
5657}
5658
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005659static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5660 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005661{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005662 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005663 union sctp_addr temp;
5664 int cnt = 0;
5665 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005666 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005667
Vlad Yasevich29303542007-09-16 16:02:12 -07005668 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005669 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07005670 if (!addr->valid)
5671 continue;
5672
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005673 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08005674 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005675 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005676 if ((PF_INET6 == sk->sk_family) &&
5677 inet_v6_ipv6only(sk) &&
5678 (AF_INET == addr->a.sa.sa_family))
5679 continue;
Al Viro6244be42006-11-20 17:21:44 -08005680 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05005681 if (!temp.v4.sin_port)
5682 temp.v4.sin_port = htons(port);
5683
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005684 addrlen = sctp_get_pf_specific(sk->sk_family)
5685 ->addr_to_user(sctp_sk(sk), &temp);
5686
Vlad Yasevich29303542007-09-16 16:02:12 -07005687 if (space_left < addrlen) {
5688 cnt = -ENOMEM;
5689 break;
5690 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005691 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08005692
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005693 to += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005694 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005695 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04005696 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005697 }
Vlad Yasevich29303542007-09-16 16:02:12 -07005698 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005699
5700 return cnt;
5701}
5702
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005704static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5705 char __user *optval, int __user *optlen)
5706{
5707 struct sctp_bind_addr *bp;
5708 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005709 int cnt = 0;
5710 struct sctp_getaddrs getaddrs;
5711 struct sctp_sockaddr_entry *addr;
5712 void __user *to;
5713 union sctp_addr temp;
5714 struct sctp_sock *sp = sctp_sk(sk);
5715 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005716 int err = 0;
5717 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005718 int bytes_copied = 0;
5719 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005720 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005721
Neil Horman408f22e2007-06-16 14:03:45 -04005722 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005723 return -EINVAL;
5724
5725 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5726 return -EFAULT;
5727
5728 /*
5729 * For UDP-style sockets, id specifies the association to query.
5730 * If the id field is set to the value '0' then the locally bound
5731 * addresses are returned without regard to any particular
5732 * association.
5733 */
5734 if (0 == getaddrs.assoc_id) {
5735 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005736 } else {
5737 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5738 if (!asoc)
5739 return -EINVAL;
5740 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005741 }
5742
wangweidongcb3f8372013-12-23 12:16:50 +08005743 to = optval + offsetof(struct sctp_getaddrs, addrs);
5744 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Neil Horman186e2342007-06-18 19:59:16 -04005745
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02005746 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005747 if (!addrs)
5748 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005749
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005750 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5751 * addresses from the global local address list.
5752 */
5753 if (sctp_list_single_entry(&bp->address_list)) {
5754 addr = list_entry(bp->address_list.next,
5755 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04005756 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005757 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5758 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005759 if (cnt < 0) {
5760 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005761 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005762 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005763 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005764 }
5765 }
5766
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005767 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005768 /* Protection on the bound address list is not needed since
5769 * in the socket option context we hold a socket lock and
5770 * thus the bound address list can't change.
5771 */
5772 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08005773 memcpy(&temp, &addr->a, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005774 addrlen = sctp_get_pf_specific(sk->sk_family)
5775 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005776 if (space_left < addrlen) {
5777 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07005778 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005779 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005780 memcpy(buf, &temp, addrlen);
5781 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005782 bytes_copied += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005783 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005784 space_left -= addrlen;
5785 }
5786
5787copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005788 if (copy_to_user(to, addrs, bytes_copied)) {
5789 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005790 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005791 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005792 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
5793 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005794 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005795 }
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02005796 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
5797 * but we can't change it anymore.
5798 */
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005799 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005800 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005801out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005802 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005803 return err;
5804}
5805
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
5807 *
5808 * Requests that the local SCTP stack use the enclosed peer address as
5809 * the association primary. The enclosed address must be one of the
5810 * association peer's addresses.
5811 */
5812static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
5813 char __user *optval, int __user *optlen)
5814{
5815 struct sctp_prim prim;
5816 struct sctp_association *asoc;
5817 struct sctp_sock *sp = sctp_sk(sk);
5818
Neil Horman408f22e2007-06-16 14:03:45 -04005819 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 return -EINVAL;
5821
Neil Horman408f22e2007-06-16 14:03:45 -04005822 len = sizeof(struct sctp_prim);
5823
5824 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 return -EFAULT;
5826
5827 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
5828 if (!asoc)
5829 return -EINVAL;
5830
5831 if (!asoc->peer.primary_path)
5832 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005833
Al Viro8cec6b82006-11-20 17:23:01 -08005834 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
5835 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005837 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 (union sctp_addr *)&prim.ssp_addr);
5839
Neil Horman408f22e2007-06-16 14:03:45 -04005840 if (put_user(len, optlen))
5841 return -EFAULT;
5842 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 return -EFAULT;
5844
5845 return 0;
5846}
5847
5848/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005849 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005851 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 * Indication parameter for all future INIT and INIT-ACK exchanges.
5853 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005854static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 char __user *optval, int __user *optlen)
5856{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005857 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858
Neil Horman408f22e2007-06-16 14:03:45 -04005859 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 return -EINVAL;
5861
Neil Horman408f22e2007-06-16 14:03:45 -04005862 len = sizeof(struct sctp_setadaptation);
5863
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005864 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04005865
5866 if (put_user(len, optlen))
5867 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005868 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07005870
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 return 0;
5872}
5873
5874/*
5875 *
5876 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
5877 *
5878 * Applications that wish to use the sendto() system call may wish to
5879 * specify a default set of parameters that would normally be supplied
5880 * through the inclusion of ancillary data. This socket option allows
5881 * such an application to set the default sctp_sndrcvinfo structure.
5882
5883
5884 * The application that wishes to use this socket option simply passes
5885 * in to this call the sctp_sndrcvinfo structure defined in Section
5886 * 5.2.2) The input parameters accepted by this call include
5887 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
5888 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
5889 * to this call if the caller is using the UDP model.
5890 *
5891 * For getsockopt, it get the default sctp_sndrcvinfo structure.
5892 */
5893static int sctp_getsockopt_default_send_param(struct sock *sk,
5894 int len, char __user *optval,
5895 int __user *optlen)
5896{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005898 struct sctp_association *asoc;
5899 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005901 if (len < sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005903
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005904 len = sizeof(info);
Neil Horman408f22e2007-06-16 14:03:45 -04005905
5906 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 return -EFAULT;
5908
5909 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
5910 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
5911 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 if (asoc) {
5913 info.sinfo_stream = asoc->default_stream;
5914 info.sinfo_flags = asoc->default_flags;
5915 info.sinfo_ppid = asoc->default_ppid;
5916 info.sinfo_context = asoc->default_context;
5917 info.sinfo_timetolive = asoc->default_timetolive;
5918 } else {
5919 info.sinfo_stream = sp->default_stream;
5920 info.sinfo_flags = sp->default_flags;
5921 info.sinfo_ppid = sp->default_ppid;
5922 info.sinfo_context = sp->default_context;
5923 info.sinfo_timetolive = sp->default_timetolive;
5924 }
5925
Neil Horman408f22e2007-06-16 14:03:45 -04005926 if (put_user(len, optlen))
5927 return -EFAULT;
5928 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 return -EFAULT;
5930
5931 return 0;
5932}
5933
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005934/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
5935 * (SCTP_DEFAULT_SNDINFO)
5936 */
5937static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
5938 char __user *optval,
5939 int __user *optlen)
5940{
5941 struct sctp_sock *sp = sctp_sk(sk);
5942 struct sctp_association *asoc;
5943 struct sctp_sndinfo info;
5944
5945 if (len < sizeof(info))
5946 return -EINVAL;
5947
5948 len = sizeof(info);
5949
5950 if (copy_from_user(&info, optval, len))
5951 return -EFAULT;
5952
5953 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
5954 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
5955 return -EINVAL;
5956 if (asoc) {
5957 info.snd_sid = asoc->default_stream;
5958 info.snd_flags = asoc->default_flags;
5959 info.snd_ppid = asoc->default_ppid;
5960 info.snd_context = asoc->default_context;
5961 } else {
5962 info.snd_sid = sp->default_stream;
5963 info.snd_flags = sp->default_flags;
5964 info.snd_ppid = sp->default_ppid;
5965 info.snd_context = sp->default_context;
5966 }
5967
5968 if (put_user(len, optlen))
5969 return -EFAULT;
5970 if (copy_to_user(optval, &info, len))
5971 return -EFAULT;
5972
5973 return 0;
5974}
5975
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976/*
5977 *
5978 * 7.1.5 SCTP_NODELAY
5979 *
5980 * Turn on/off any Nagle-like algorithm. This means that packets are
5981 * generally sent as soon as possible and no unnecessary delays are
5982 * introduced, at the cost of more packets in the network. Expects an
5983 * integer boolean flag.
5984 */
5985
5986static int sctp_getsockopt_nodelay(struct sock *sk, int len,
5987 char __user *optval, int __user *optlen)
5988{
5989 int val;
5990
5991 if (len < sizeof(int))
5992 return -EINVAL;
5993
5994 len = sizeof(int);
5995 val = (sctp_sk(sk)->nodelay == 1);
5996 if (put_user(len, optlen))
5997 return -EFAULT;
5998 if (copy_to_user(optval, &val, len))
5999 return -EFAULT;
6000 return 0;
6001}
6002
6003/*
6004 *
6005 * 7.1.1 SCTP_RTOINFO
6006 *
6007 * The protocol parameters used to initialize and bound retransmission
6008 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
6009 * and modify these parameters.
6010 * All parameters are time values, in milliseconds. A value of 0, when
6011 * modifying the parameters, indicates that the current value should not
6012 * be changed.
6013 *
6014 */
6015static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6016 char __user *optval,
6017 int __user *optlen) {
6018 struct sctp_rtoinfo rtoinfo;
6019 struct sctp_association *asoc;
6020
Neil Horman408f22e2007-06-16 14:03:45 -04006021 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 return -EINVAL;
6023
Neil Horman408f22e2007-06-16 14:03:45 -04006024 len = sizeof(struct sctp_rtoinfo);
6025
6026 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 return -EFAULT;
6028
6029 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6030
6031 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
6032 return -EINVAL;
6033
6034 /* Values corresponding to the specific association. */
6035 if (asoc) {
6036 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6037 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6038 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6039 } else {
6040 /* Values corresponding to the endpoint. */
6041 struct sctp_sock *sp = sctp_sk(sk);
6042
6043 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6044 rtoinfo.srto_max = sp->rtoinfo.srto_max;
6045 rtoinfo.srto_min = sp->rtoinfo.srto_min;
6046 }
6047
6048 if (put_user(len, optlen))
6049 return -EFAULT;
6050
6051 if (copy_to_user(optval, &rtoinfo, len))
6052 return -EFAULT;
6053
6054 return 0;
6055}
6056
6057/*
6058 *
6059 * 7.1.2 SCTP_ASSOCINFO
6060 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02006061 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 * of the association.
6063 * Returns an error if the new association retransmission value is
6064 * greater than the sum of the retransmission value of the peer.
6065 * See [SCTP] for more information.
6066 *
6067 */
6068static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6069 char __user *optval,
6070 int __user *optlen)
6071{
6072
6073 struct sctp_assocparams assocparams;
6074 struct sctp_association *asoc;
6075 struct list_head *pos;
6076 int cnt = 0;
6077
Neil Horman408f22e2007-06-16 14:03:45 -04006078 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 return -EINVAL;
6080
Neil Horman408f22e2007-06-16 14:03:45 -04006081 len = sizeof(struct sctp_assocparams);
6082
6083 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 return -EFAULT;
6085
6086 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6087
6088 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
6089 return -EINVAL;
6090
6091 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07006092 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6094 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6095 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
Daniel Borkmann52db8822013-06-25 18:17:27 +02006096 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097
6098 list_for_each(pos, &asoc->peer.transport_addr_list) {
wangweidongcb3f8372013-12-23 12:16:50 +08006099 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 }
6101
6102 assocparams.sasoc_number_peer_destinations = cnt;
6103 } else {
6104 /* Values corresponding to the endpoint */
6105 struct sctp_sock *sp = sctp_sk(sk);
6106
6107 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6108 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6109 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6110 assocparams.sasoc_cookie_life =
6111 sp->assocparams.sasoc_cookie_life;
6112 assocparams.sasoc_number_peer_destinations =
6113 sp->assocparams.
6114 sasoc_number_peer_destinations;
6115 }
6116
6117 if (put_user(len, optlen))
6118 return -EFAULT;
6119
6120 if (copy_to_user(optval, &assocparams, len))
6121 return -EFAULT;
6122
6123 return 0;
6124}
6125
6126/*
6127 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6128 *
6129 * This socket option is a boolean flag which turns on or off mapped V4
6130 * addresses. If this option is turned on and the socket is type
6131 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6132 * If this option is turned off, then no mapping will be done of V4
6133 * addresses and a user will receive both PF_INET6 and PF_INET type
6134 * addresses on the socket.
6135 */
6136static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6137 char __user *optval, int __user *optlen)
6138{
6139 int val;
6140 struct sctp_sock *sp = sctp_sk(sk);
6141
6142 if (len < sizeof(int))
6143 return -EINVAL;
6144
6145 len = sizeof(int);
6146 val = sp->v4mapped;
6147 if (put_user(len, optlen))
6148 return -EFAULT;
6149 if (copy_to_user(optval, &val, len))
6150 return -EFAULT;
6151
6152 return 0;
6153}
6154
6155/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006156 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6157 * (chapter and verse is quoted at sctp_setsockopt_context())
6158 */
6159static int sctp_getsockopt_context(struct sock *sk, int len,
6160 char __user *optval, int __user *optlen)
6161{
6162 struct sctp_assoc_value params;
6163 struct sctp_sock *sp;
6164 struct sctp_association *asoc;
6165
Neil Horman408f22e2007-06-16 14:03:45 -04006166 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006167 return -EINVAL;
6168
Neil Horman408f22e2007-06-16 14:03:45 -04006169 len = sizeof(struct sctp_assoc_value);
6170
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006171 if (copy_from_user(&params, optval, len))
6172 return -EFAULT;
6173
6174 sp = sctp_sk(sk);
6175
6176 if (params.assoc_id != 0) {
6177 asoc = sctp_id2assoc(sk, params.assoc_id);
6178 if (!asoc)
6179 return -EINVAL;
6180 params.assoc_value = asoc->default_rcv_context;
6181 } else {
6182 params.assoc_value = sp->default_rcv_context;
6183 }
6184
6185 if (put_user(len, optlen))
6186 return -EFAULT;
6187 if (copy_to_user(optval, &params, len))
6188 return -EFAULT;
6189
6190 return 0;
6191}
6192
6193/*
Wei Yongjune89c2092008-12-25 16:54:58 -08006194 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6195 * This option will get or set the maximum size to put in any outgoing
6196 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 * fragmented by SCTP into the specified size. Note that the underlying
6198 * SCTP implementation may fragment into smaller sized chunks when the
6199 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08006200 * the user. The default value for this option is '0' which indicates
6201 * the user is NOT limiting fragmentation and only the PMTU will effect
6202 * SCTP's choice of DATA chunk size. Note also that values set larger
6203 * than the maximum size of an IP datagram will effectively let SCTP
6204 * control fragmentation (i.e. the same as setting this option to 0).
6205 *
6206 * The following structure is used to access and modify this parameter:
6207 *
6208 * struct sctp_assoc_value {
6209 * sctp_assoc_t assoc_id;
6210 * uint32_t assoc_value;
6211 * };
6212 *
6213 * assoc_id: This parameter is ignored for one-to-one style sockets.
6214 * For one-to-many style sockets this parameter indicates which
6215 * association the user is performing an action upon. Note that if
6216 * this field's value is zero then the endpoints default value is
6217 * changed (effecting future associations only).
6218 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 */
6220static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6221 char __user *optval, int __user *optlen)
6222{
Wei Yongjune89c2092008-12-25 16:54:58 -08006223 struct sctp_assoc_value params;
6224 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225
Wei Yongjune89c2092008-12-25 16:54:58 -08006226 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006227 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006228 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006229 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006230 "Use struct sctp_assoc_value instead\n",
6231 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08006232 params.assoc_id = 0;
6233 } else if (len >= sizeof(struct sctp_assoc_value)) {
6234 len = sizeof(struct sctp_assoc_value);
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006235 if (copy_from_user(&params, optval, len))
Wei Yongjune89c2092008-12-25 16:54:58 -08006236 return -EFAULT;
6237 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 return -EINVAL;
6239
Wei Yongjune89c2092008-12-25 16:54:58 -08006240 asoc = sctp_id2assoc(sk, params.assoc_id);
6241 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
6242 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
Wei Yongjune89c2092008-12-25 16:54:58 -08006244 if (asoc)
6245 params.assoc_value = asoc->frag_point;
6246 else
6247 params.assoc_value = sctp_sk(sk)->user_frag;
6248
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 if (put_user(len, optlen))
6250 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08006251 if (len == sizeof(int)) {
6252 if (copy_to_user(optval, &params.assoc_value, len))
6253 return -EFAULT;
6254 } else {
6255 if (copy_to_user(optval, &params, len))
6256 return -EFAULT;
6257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258
6259 return 0;
6260}
6261
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006262/*
6263 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6264 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6265 */
6266static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6267 char __user *optval, int __user *optlen)
6268{
6269 int val;
6270
6271 if (len < sizeof(int))
6272 return -EINVAL;
6273
6274 len = sizeof(int);
6275
6276 val = sctp_sk(sk)->frag_interleave;
6277 if (put_user(len, optlen))
6278 return -EFAULT;
6279 if (copy_to_user(optval, &val, len))
6280 return -EFAULT;
6281
6282 return 0;
6283}
6284
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006285/*
6286 * 7.1.25. Set or Get the sctp partial delivery point
6287 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6288 */
6289static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6290 char __user *optval,
6291 int __user *optlen)
6292{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09006293 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006294
6295 if (len < sizeof(u32))
6296 return -EINVAL;
6297
6298 len = sizeof(u32);
6299
6300 val = sctp_sk(sk)->pd_point;
6301 if (put_user(len, optlen))
6302 return -EFAULT;
6303 if (copy_to_user(optval, &val, len))
6304 return -EFAULT;
6305
Wei Yongjun7d743b72010-12-14 16:10:41 +00006306 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006307}
6308
Vlad Yasevich70331572007-03-23 11:34:36 -07006309/*
6310 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6311 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6312 */
6313static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6314 char __user *optval,
6315 int __user *optlen)
6316{
Neil Horman219b99a2008-03-05 13:44:46 -08006317 struct sctp_assoc_value params;
6318 struct sctp_sock *sp;
6319 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07006320
Neil Horman219b99a2008-03-05 13:44:46 -08006321 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006322 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006323 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006324 "Use of int in max_burst socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006325 "Use struct sctp_assoc_value instead\n",
6326 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08006327 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00006328 } else if (len >= sizeof(struct sctp_assoc_value)) {
6329 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08006330 if (copy_from_user(&params, optval, len))
6331 return -EFAULT;
6332 } else
6333 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07006334
Neil Horman219b99a2008-03-05 13:44:46 -08006335 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07006336
Neil Horman219b99a2008-03-05 13:44:46 -08006337 if (params.assoc_id != 0) {
6338 asoc = sctp_id2assoc(sk, params.assoc_id);
6339 if (!asoc)
6340 return -EINVAL;
6341 params.assoc_value = asoc->max_burst;
6342 } else
6343 params.assoc_value = sp->max_burst;
6344
6345 if (len == sizeof(int)) {
6346 if (copy_to_user(optval, &params.assoc_value, len))
6347 return -EFAULT;
6348 } else {
6349 if (copy_to_user(optval, &params, len))
6350 return -EFAULT;
6351 }
6352
6353 return 0;
6354
Vlad Yasevich70331572007-03-23 11:34:36 -07006355}
6356
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006357static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6358 char __user *optval, int __user *optlen)
6359{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006360 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006361 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006362 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006363 __u16 data_len = 0;
6364 u32 num_idents;
Xin Long7a84bd42016-02-03 23:33:30 +08006365 int i;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006366
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006367 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006368 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006369
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006370 hmacs = ep->auth_hmacs_list;
Xin Long3c918702017-06-30 11:52:16 +08006371 data_len = ntohs(hmacs->param_hdr.length) -
6372 sizeof(struct sctp_paramhdr);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006373
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006374 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006375 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006376
6377 len = sizeof(struct sctp_hmacalgo) + data_len;
6378 num_idents = data_len / sizeof(u16);
6379
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006380 if (put_user(len, optlen))
6381 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006382 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006383 return -EFAULT;
Xin Long7a84bd42016-02-03 23:33:30 +08006384 for (i = 0; i < num_idents; i++) {
6385 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6386
6387 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6388 return -EFAULT;
6389 }
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006390 return 0;
6391}
6392
6393static int sctp_getsockopt_active_key(struct sock *sk, int len,
6394 char __user *optval, int __user *optlen)
6395{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006396 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006397 struct sctp_authkeyid val;
6398 struct sctp_association *asoc;
6399
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006400 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006401 return -EACCES;
6402
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006403 if (len < sizeof(struct sctp_authkeyid))
6404 return -EINVAL;
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006405
6406 len = sizeof(struct sctp_authkeyid);
6407 if (copy_from_user(&val, optval, len))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006408 return -EFAULT;
6409
6410 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6411 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6412 return -EINVAL;
6413
6414 if (asoc)
6415 val.scact_keynumber = asoc->active_key_id;
6416 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006417 val.scact_keynumber = ep->active_key_id;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006418
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006419 if (put_user(len, optlen))
6420 return -EFAULT;
6421 if (copy_to_user(optval, &val, len))
6422 return -EFAULT;
6423
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006424 return 0;
6425}
6426
6427static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6428 char __user *optval, int __user *optlen)
6429{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006430 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006431 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006432 struct sctp_authchunks val;
6433 struct sctp_association *asoc;
6434 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006435 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006436 char __user *to;
6437
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006438 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006439 return -EACCES;
6440
6441 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006442 return -EINVAL;
6443
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006444 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006445 return -EFAULT;
6446
Al Viro411223c2007-10-14 19:21:20 +01006447 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006448 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6449 if (!asoc)
6450 return -EINVAL;
6451
6452 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006453 if (!ch)
6454 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006455
6456 /* See if the user provided enough room for all the data */
Xin Long3c918702017-06-30 11:52:16 +08006457 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevichb40db682008-02-27 14:40:37 -05006458 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006459 return -EINVAL;
6460
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006461 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006462 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006463num:
6464 len = sizeof(struct sctp_authchunks) + num_chunks;
wangweidong8d726512013-12-23 12:16:53 +08006465 if (put_user(len, optlen))
6466 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006467 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6468 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006469 return 0;
6470}
6471
6472static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6473 char __user *optval, int __user *optlen)
6474{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006475 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006476 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006477 struct sctp_authchunks val;
6478 struct sctp_association *asoc;
6479 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006480 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006481 char __user *to;
6482
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006483 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006484 return -EACCES;
6485
6486 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006487 return -EINVAL;
6488
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006489 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006490 return -EFAULT;
6491
Al Viro411223c2007-10-14 19:21:20 +01006492 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006493 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6494 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6495 return -EINVAL;
6496
6497 if (asoc)
wangweidong26ac8e52013-12-23 12:16:51 +08006498 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006499 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006500 ch = ep->auth_chunk_list;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006501
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006502 if (!ch)
6503 goto num;
6504
Xin Long3c918702017-06-30 11:52:16 +08006505 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006506 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006507 return -EINVAL;
6508
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006509 if (copy_to_user(to, ch->chunks, num_chunks))
6510 return -EFAULT;
6511num:
6512 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006513 if (put_user(len, optlen))
6514 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006515 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6516 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006517
6518 return 0;
6519}
6520
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08006521/*
6522 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6523 * This option gets the current number of associations that are attached
6524 * to a one-to-many style socket. The option value is an uint32_t.
6525 */
6526static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6527 char __user *optval, int __user *optlen)
6528{
6529 struct sctp_sock *sp = sctp_sk(sk);
6530 struct sctp_association *asoc;
6531 u32 val = 0;
6532
6533 if (sctp_style(sk, TCP))
6534 return -EOPNOTSUPP;
6535
6536 if (len < sizeof(u32))
6537 return -EINVAL;
6538
6539 len = sizeof(u32);
6540
6541 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6542 val++;
6543 }
6544
6545 if (put_user(len, optlen))
6546 return -EFAULT;
6547 if (copy_to_user(optval, &val, len))
6548 return -EFAULT;
6549
6550 return 0;
6551}
6552
Wei Yongjun209ba422011-04-17 17:27:08 +00006553/*
Michio Honda7dc04d72011-04-26 20:16:31 +09006554 * 8.1.23 SCTP_AUTO_ASCONF
6555 * See the corresponding setsockopt entry as description
6556 */
6557static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6558 char __user *optval, int __user *optlen)
6559{
6560 int val = 0;
6561
6562 if (len < sizeof(int))
6563 return -EINVAL;
6564
6565 len = sizeof(int);
6566 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6567 val = 1;
6568 if (put_user(len, optlen))
6569 return -EFAULT;
6570 if (copy_to_user(optval, &val, len))
6571 return -EFAULT;
6572 return 0;
6573}
6574
6575/*
Wei Yongjun209ba422011-04-17 17:27:08 +00006576 * 8.2.6. Get the Current Identifiers of Associations
6577 * (SCTP_GET_ASSOC_ID_LIST)
6578 *
6579 * This option gets the current list of SCTP association identifiers of
6580 * the SCTP associations handled by a one-to-many style socket.
6581 */
6582static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6583 char __user *optval, int __user *optlen)
6584{
6585 struct sctp_sock *sp = sctp_sk(sk);
6586 struct sctp_association *asoc;
6587 struct sctp_assoc_ids *ids;
6588 u32 num = 0;
6589
6590 if (sctp_style(sk, TCP))
6591 return -EOPNOTSUPP;
6592
6593 if (len < sizeof(struct sctp_assoc_ids))
6594 return -EINVAL;
6595
6596 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6597 num++;
6598 }
6599
6600 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6601 return -EINVAL;
6602
6603 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6604
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02006605 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
Wei Yongjun209ba422011-04-17 17:27:08 +00006606 if (unlikely(!ids))
6607 return -ENOMEM;
6608
6609 ids->gaids_number_of_ids = num;
6610 num = 0;
6611 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6612 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6613 }
6614
6615 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6616 kfree(ids);
6617 return -EFAULT;
6618 }
6619
6620 kfree(ids);
6621 return 0;
6622}
6623
Neil Horman5aa93bc2012-07-21 07:56:07 +00006624/*
6625 * SCTP_PEER_ADDR_THLDS
6626 *
6627 * This option allows us to fetch the partially failed threshold for one or all
6628 * transports in an association. See Section 6.1 of:
6629 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6630 */
6631static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6632 char __user *optval,
6633 int len,
6634 int __user *optlen)
6635{
6636 struct sctp_paddrthlds val;
6637 struct sctp_transport *trans;
6638 struct sctp_association *asoc;
6639
6640 if (len < sizeof(struct sctp_paddrthlds))
6641 return -EINVAL;
6642 len = sizeof(struct sctp_paddrthlds);
6643 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6644 return -EFAULT;
6645
6646 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6647 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6648 if (!asoc)
6649 return -ENOENT;
6650
6651 val.spt_pathpfthld = asoc->pf_retrans;
6652 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6653 } else {
6654 trans = sctp_addr_id2transport(sk, &val.spt_address,
6655 val.spt_assoc_id);
6656 if (!trans)
6657 return -ENOENT;
6658
6659 val.spt_pathmaxrxt = trans->pathmaxrxt;
6660 val.spt_pathpfthld = trans->pf_retrans;
6661 }
6662
6663 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6664 return -EFAULT;
6665
6666 return 0;
6667}
6668
Michele Baldessari196d6752012-12-01 04:49:42 +00006669/*
6670 * SCTP_GET_ASSOC_STATS
6671 *
6672 * This option retrieves local per endpoint statistics. It is modeled
6673 * after OpenSolaris' implementation
6674 */
6675static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6676 char __user *optval,
6677 int __user *optlen)
6678{
6679 struct sctp_assoc_stats sas;
6680 struct sctp_association *asoc = NULL;
6681
6682 /* User must provide at least the assoc id */
6683 if (len < sizeof(sctp_assoc_t))
6684 return -EINVAL;
6685
Guenter Roeck726bc6b2013-02-27 10:57:31 +00006686 /* Allow the struct to grow and fill in as much as possible */
6687 len = min_t(size_t, len, sizeof(sas));
6688
Michele Baldessari196d6752012-12-01 04:49:42 +00006689 if (copy_from_user(&sas, optval, len))
6690 return -EFAULT;
6691
6692 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6693 if (!asoc)
6694 return -EINVAL;
6695
6696 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6697 sas.sas_gapcnt = asoc->stats.gapcnt;
6698 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6699 sas.sas_osacks = asoc->stats.osacks;
6700 sas.sas_isacks = asoc->stats.isacks;
6701 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6702 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6703 sas.sas_oodchunks = asoc->stats.oodchunks;
6704 sas.sas_iodchunks = asoc->stats.iodchunks;
6705 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6706 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6707 sas.sas_idupchunks = asoc->stats.idupchunks;
6708 sas.sas_opackets = asoc->stats.opackets;
6709 sas.sas_ipackets = asoc->stats.ipackets;
6710
6711 /* New high max rto observed, will return 0 if not a single
6712 * RTO update took place. obs_rto_ipaddr will be bogus
6713 * in such a case
6714 */
6715 sas.sas_maxrto = asoc->stats.max_obs_rto;
6716 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6717 sizeof(struct sockaddr_storage));
6718
6719 /* Mark beginning of a new observation period */
6720 asoc->stats.max_obs_rto = asoc->rto_min;
6721
Michele Baldessari196d6752012-12-01 04:49:42 +00006722 if (put_user(len, optlen))
6723 return -EFAULT;
6724
Daniel Borkmannbb333812013-06-28 19:49:40 +02006725 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
Michele Baldessari196d6752012-12-01 04:49:42 +00006726
6727 if (copy_to_user(optval, &sas, len))
6728 return -EFAULT;
6729
6730 return 0;
6731}
6732
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02006733static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6734 char __user *optval,
6735 int __user *optlen)
6736{
6737 int val = 0;
6738
6739 if (len < sizeof(int))
6740 return -EINVAL;
6741
6742 len = sizeof(int);
6743 if (sctp_sk(sk)->recvrcvinfo)
6744 val = 1;
6745 if (put_user(len, optlen))
6746 return -EFAULT;
6747 if (copy_to_user(optval, &val, len))
6748 return -EFAULT;
6749
6750 return 0;
6751}
6752
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02006753static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6754 char __user *optval,
6755 int __user *optlen)
6756{
6757 int val = 0;
6758
6759 if (len < sizeof(int))
6760 return -EINVAL;
6761
6762 len = sizeof(int);
6763 if (sctp_sk(sk)->recvnxtinfo)
6764 val = 1;
6765 if (put_user(len, optlen))
6766 return -EFAULT;
6767 if (copy_to_user(optval, &val, len))
6768 return -EFAULT;
6769
6770 return 0;
6771}
6772
Xin Long28aa4c22016-07-09 19:47:40 +08006773static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6774 char __user *optval,
6775 int __user *optlen)
6776{
6777 struct sctp_assoc_value params;
6778 struct sctp_association *asoc;
6779 int retval = -EFAULT;
6780
6781 if (len < sizeof(params)) {
6782 retval = -EINVAL;
6783 goto out;
6784 }
6785
6786 len = sizeof(params);
6787 if (copy_from_user(&params, optval, len))
6788 goto out;
6789
6790 asoc = sctp_id2assoc(sk, params.assoc_id);
6791 if (asoc) {
6792 params.assoc_value = asoc->prsctp_enable;
6793 } else if (!params.assoc_id) {
6794 struct sctp_sock *sp = sctp_sk(sk);
6795
6796 params.assoc_value = sp->ep->prsctp_enable;
6797 } else {
6798 retval = -EINVAL;
6799 goto out;
6800 }
6801
6802 if (put_user(len, optlen))
6803 goto out;
6804
6805 if (copy_to_user(optval, &params, len))
6806 goto out;
6807
6808 retval = 0;
6809
6810out:
6811 return retval;
6812}
6813
Xin Longf959fb42016-07-09 19:47:41 +08006814static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
6815 char __user *optval,
6816 int __user *optlen)
6817{
6818 struct sctp_default_prinfo info;
6819 struct sctp_association *asoc;
6820 int retval = -EFAULT;
6821
6822 if (len < sizeof(info)) {
6823 retval = -EINVAL;
6824 goto out;
6825 }
6826
6827 len = sizeof(info);
6828 if (copy_from_user(&info, optval, len))
6829 goto out;
6830
6831 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
6832 if (asoc) {
6833 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
6834 info.pr_value = asoc->default_timetolive;
6835 } else if (!info.pr_assoc_id) {
6836 struct sctp_sock *sp = sctp_sk(sk);
6837
6838 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
6839 info.pr_value = sp->default_timetolive;
6840 } else {
6841 retval = -EINVAL;
6842 goto out;
6843 }
6844
6845 if (put_user(len, optlen))
6846 goto out;
6847
6848 if (copy_to_user(optval, &info, len))
6849 goto out;
6850
6851 retval = 0;
6852
6853out:
6854 return retval;
6855}
6856
Xin Long826d2532016-07-09 19:47:42 +08006857static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
6858 char __user *optval,
6859 int __user *optlen)
6860{
6861 struct sctp_prstatus params;
6862 struct sctp_association *asoc;
6863 int policy;
6864 int retval = -EINVAL;
6865
6866 if (len < sizeof(params))
6867 goto out;
6868
6869 len = sizeof(params);
6870 if (copy_from_user(&params, optval, len)) {
6871 retval = -EFAULT;
6872 goto out;
6873 }
6874
6875 policy = params.sprstat_policy;
6876 if (policy & ~SCTP_PR_SCTP_MASK)
6877 goto out;
6878
6879 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
6880 if (!asoc)
6881 goto out;
6882
6883 if (policy == SCTP_PR_SCTP_NONE) {
6884 params.sprstat_abandoned_unsent = 0;
6885 params.sprstat_abandoned_sent = 0;
6886 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6887 params.sprstat_abandoned_unsent +=
6888 asoc->abandoned_unsent[policy];
6889 params.sprstat_abandoned_sent +=
6890 asoc->abandoned_sent[policy];
6891 }
6892 } else {
6893 params.sprstat_abandoned_unsent =
6894 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
6895 params.sprstat_abandoned_sent =
6896 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
6897 }
6898
6899 if (put_user(len, optlen)) {
6900 retval = -EFAULT;
6901 goto out;
6902 }
6903
6904 if (copy_to_user(optval, &params, len)) {
6905 retval = -EFAULT;
6906 goto out;
6907 }
6908
6909 retval = 0;
6910
6911out:
6912 return retval;
6913}
6914
Xin Longd229d482017-04-01 17:07:46 +08006915static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
6916 char __user *optval,
6917 int __user *optlen)
6918{
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006919 struct sctp_stream_out_ext *streamoute;
Xin Longd229d482017-04-01 17:07:46 +08006920 struct sctp_association *asoc;
6921 struct sctp_prstatus params;
6922 int retval = -EINVAL;
6923 int policy;
6924
6925 if (len < sizeof(params))
6926 goto out;
6927
6928 len = sizeof(params);
6929 if (copy_from_user(&params, optval, len)) {
6930 retval = -EFAULT;
6931 goto out;
6932 }
6933
6934 policy = params.sprstat_policy;
6935 if (policy & ~SCTP_PR_SCTP_MASK)
6936 goto out;
6937
6938 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
Xin Longcee360a2017-05-31 16:36:31 +08006939 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
Xin Longd229d482017-04-01 17:07:46 +08006940 goto out;
6941
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006942 streamoute = asoc->stream.out[params.sprstat_sid].ext;
6943 if (!streamoute) {
6944 /* Not allocated yet, means all stats are 0 */
6945 params.sprstat_abandoned_unsent = 0;
6946 params.sprstat_abandoned_sent = 0;
6947 retval = 0;
6948 goto out;
6949 }
6950
Xin Longd229d482017-04-01 17:07:46 +08006951 if (policy == SCTP_PR_SCTP_NONE) {
6952 params.sprstat_abandoned_unsent = 0;
6953 params.sprstat_abandoned_sent = 0;
6954 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6955 params.sprstat_abandoned_unsent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006956 streamoute->abandoned_unsent[policy];
Xin Longd229d482017-04-01 17:07:46 +08006957 params.sprstat_abandoned_sent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006958 streamoute->abandoned_sent[policy];
Xin Longd229d482017-04-01 17:07:46 +08006959 }
6960 } else {
6961 params.sprstat_abandoned_unsent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006962 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08006963 params.sprstat_abandoned_sent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006964 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08006965 }
6966
6967 if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
6968 retval = -EFAULT;
6969 goto out;
6970 }
6971
6972 retval = 0;
6973
6974out:
6975 return retval;
6976}
6977
Xin Longc0d8bab2017-03-10 12:11:12 +08006978static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
6979 char __user *optval,
6980 int __user *optlen)
6981{
6982 struct sctp_assoc_value params;
6983 struct sctp_association *asoc;
6984 int retval = -EFAULT;
6985
6986 if (len < sizeof(params)) {
6987 retval = -EINVAL;
6988 goto out;
6989 }
6990
6991 len = sizeof(params);
6992 if (copy_from_user(&params, optval, len))
6993 goto out;
6994
6995 asoc = sctp_id2assoc(sk, params.assoc_id);
6996 if (asoc) {
6997 params.assoc_value = asoc->reconf_enable;
6998 } else if (!params.assoc_id) {
6999 struct sctp_sock *sp = sctp_sk(sk);
7000
7001 params.assoc_value = sp->ep->reconf_enable;
7002 } else {
7003 retval = -EINVAL;
7004 goto out;
7005 }
7006
7007 if (put_user(len, optlen))
7008 goto out;
7009
7010 if (copy_to_user(optval, &params, len))
7011 goto out;
7012
7013 retval = 0;
7014
7015out:
7016 return retval;
7017}
7018
Xin Long9fb657a2017-01-18 00:44:46 +08007019static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7020 char __user *optval,
7021 int __user *optlen)
7022{
7023 struct sctp_assoc_value params;
7024 struct sctp_association *asoc;
7025 int retval = -EFAULT;
7026
7027 if (len < sizeof(params)) {
7028 retval = -EINVAL;
7029 goto out;
7030 }
7031
7032 len = sizeof(params);
7033 if (copy_from_user(&params, optval, len))
7034 goto out;
7035
7036 asoc = sctp_id2assoc(sk, params.assoc_id);
7037 if (asoc) {
7038 params.assoc_value = asoc->strreset_enable;
7039 } else if (!params.assoc_id) {
7040 struct sctp_sock *sp = sctp_sk(sk);
7041
7042 params.assoc_value = sp->ep->strreset_enable;
7043 } else {
7044 retval = -EINVAL;
7045 goto out;
7046 }
7047
7048 if (put_user(len, optlen))
7049 goto out;
7050
7051 if (copy_to_user(optval, &params, len))
7052 goto out;
7053
7054 retval = 0;
7055
7056out:
7057 return retval;
7058}
7059
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007060static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7061 char __user *optval,
7062 int __user *optlen)
7063{
7064 struct sctp_assoc_value params;
7065 struct sctp_association *asoc;
7066 int retval = -EFAULT;
7067
7068 if (len < sizeof(params)) {
7069 retval = -EINVAL;
7070 goto out;
7071 }
7072
7073 len = sizeof(params);
7074 if (copy_from_user(&params, optval, len))
7075 goto out;
7076
7077 asoc = sctp_id2assoc(sk, params.assoc_id);
7078 if (!asoc) {
7079 retval = -EINVAL;
7080 goto out;
7081 }
7082
7083 params.assoc_value = sctp_sched_get_sched(asoc);
7084
7085 if (put_user(len, optlen))
7086 goto out;
7087
7088 if (copy_to_user(optval, &params, len))
7089 goto out;
7090
7091 retval = 0;
7092
7093out:
7094 return retval;
7095}
7096
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007097static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7098 char __user *optval,
7099 int __user *optlen)
7100{
7101 struct sctp_stream_value params;
7102 struct sctp_association *asoc;
7103 int retval = -EFAULT;
7104
7105 if (len < sizeof(params)) {
7106 retval = -EINVAL;
7107 goto out;
7108 }
7109
7110 len = sizeof(params);
7111 if (copy_from_user(&params, optval, len))
7112 goto out;
7113
7114 asoc = sctp_id2assoc(sk, params.assoc_id);
7115 if (!asoc) {
7116 retval = -EINVAL;
7117 goto out;
7118 }
7119
7120 retval = sctp_sched_get_value(asoc, params.stream_id,
7121 &params.stream_value);
7122 if (retval)
7123 goto out;
7124
7125 if (put_user(len, optlen)) {
7126 retval = -EFAULT;
7127 goto out;
7128 }
7129
7130 if (copy_to_user(optval, &params, len)) {
7131 retval = -EFAULT;
7132 goto out;
7133 }
7134
7135out:
7136 return retval;
7137}
7138
Xin Long772a5862017-12-08 21:03:58 +08007139static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7140 char __user *optval,
7141 int __user *optlen)
7142{
7143 struct sctp_assoc_value params;
7144 struct sctp_association *asoc;
7145 int retval = -EFAULT;
7146
7147 if (len < sizeof(params)) {
7148 retval = -EINVAL;
7149 goto out;
7150 }
7151
7152 len = sizeof(params);
7153 if (copy_from_user(&params, optval, len))
7154 goto out;
7155
7156 asoc = sctp_id2assoc(sk, params.assoc_id);
7157 if (asoc) {
7158 params.assoc_value = asoc->intl_enable;
7159 } else if (!params.assoc_id) {
7160 struct sctp_sock *sp = sctp_sk(sk);
7161
7162 params.assoc_value = sp->strm_interleave;
7163 } else {
7164 retval = -EINVAL;
7165 goto out;
7166 }
7167
7168 if (put_user(len, optlen))
7169 goto out;
7170
7171 if (copy_to_user(optval, &params, len))
7172 goto out;
7173
7174 retval = 0;
7175
7176out:
7177 return retval;
7178}
7179
Daniel Borkmanndda91922013-06-17 11:40:05 +02007180static int sctp_getsockopt(struct sock *sk, int level, int optname,
7181 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182{
7183 int retval = 0;
7184 int len;
7185
Daniel Borkmannbb333812013-06-28 19:49:40 +02007186 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187
7188 /* I can hardly begin to describe how wrong this is. This is
7189 * so broken as to be worse than useless. The API draft
7190 * REALLY is NOT helpful here... I am not convinced that the
7191 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7192 * are at all well-founded.
7193 */
7194 if (level != SOL_SCTP) {
7195 struct sctp_af *af = sctp_sk(sk)->pf->af;
7196
7197 retval = af->getsockopt(sk, level, optname, optval, optlen);
7198 return retval;
7199 }
7200
7201 if (get_user(len, optlen))
7202 return -EFAULT;
7203
Jiri Slabya4b8e712016-10-21 14:13:24 +02007204 if (len < 0)
7205 return -EINVAL;
7206
wangweidong048ed4b2014-01-21 15:44:11 +08007207 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208
7209 switch (optname) {
7210 case SCTP_STATUS:
7211 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7212 break;
7213 case SCTP_DISABLE_FRAGMENTS:
7214 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7215 optlen);
7216 break;
7217 case SCTP_EVENTS:
7218 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7219 break;
7220 case SCTP_AUTOCLOSE:
7221 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7222 break;
7223 case SCTP_SOCKOPT_PEELOFF:
7224 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7225 break;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04007226 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7227 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7228 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 case SCTP_PEER_ADDR_PARAMS:
7230 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7231 optlen);
7232 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00007233 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07007234 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08007235 optlen);
7236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 case SCTP_INITMSG:
7238 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7239 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240 case SCTP_GET_PEER_ADDRS:
7241 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7242 optlen);
7243 break;
7244 case SCTP_GET_LOCAL_ADDRS:
7245 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7246 optlen);
7247 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04007248 case SCTP_SOCKOPT_CONNECTX3:
7249 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7250 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251 case SCTP_DEFAULT_SEND_PARAM:
7252 retval = sctp_getsockopt_default_send_param(sk, len,
7253 optval, optlen);
7254 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02007255 case SCTP_DEFAULT_SNDINFO:
7256 retval = sctp_getsockopt_default_sndinfo(sk, len,
7257 optval, optlen);
7258 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259 case SCTP_PRIMARY_ADDR:
7260 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7261 break;
7262 case SCTP_NODELAY:
7263 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7264 break;
7265 case SCTP_RTOINFO:
7266 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7267 break;
7268 case SCTP_ASSOCINFO:
7269 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7270 break;
7271 case SCTP_I_WANT_MAPPED_V4_ADDR:
7272 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7273 break;
7274 case SCTP_MAXSEG:
7275 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7276 break;
7277 case SCTP_GET_PEER_ADDR_INFO:
7278 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7279 optlen);
7280 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08007281 case SCTP_ADAPTATION_LAYER:
7282 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 optlen);
7284 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08007285 case SCTP_CONTEXT:
7286 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7287 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007288 case SCTP_FRAGMENT_INTERLEAVE:
7289 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7290 optlen);
7291 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07007292 case SCTP_PARTIAL_DELIVERY_POINT:
7293 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7294 optlen);
7295 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07007296 case SCTP_MAX_BURST:
7297 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7298 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007299 case SCTP_AUTH_KEY:
7300 case SCTP_AUTH_CHUNK:
7301 case SCTP_AUTH_DELETE_KEY:
Xin Long601590e2018-03-14 19:05:32 +08007302 case SCTP_AUTH_DEACTIVATE_KEY:
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007303 retval = -EOPNOTSUPP;
7304 break;
7305 case SCTP_HMAC_IDENT:
7306 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7307 break;
7308 case SCTP_AUTH_ACTIVE_KEY:
7309 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7310 break;
7311 case SCTP_PEER_AUTH_CHUNKS:
7312 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7313 optlen);
7314 break;
7315 case SCTP_LOCAL_AUTH_CHUNKS:
7316 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7317 optlen);
7318 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08007319 case SCTP_GET_ASSOC_NUMBER:
7320 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7321 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00007322 case SCTP_GET_ASSOC_ID_LIST:
7323 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7324 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09007325 case SCTP_AUTO_ASCONF:
7326 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7327 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00007328 case SCTP_PEER_ADDR_THLDS:
7329 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7330 break;
Michele Baldessari196d6752012-12-01 04:49:42 +00007331 case SCTP_GET_ASSOC_STATS:
7332 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7333 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02007334 case SCTP_RECVRCVINFO:
7335 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7336 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007337 case SCTP_RECVNXTINFO:
7338 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7339 break;
Xin Long28aa4c22016-07-09 19:47:40 +08007340 case SCTP_PR_SUPPORTED:
7341 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7342 break;
Xin Longf959fb42016-07-09 19:47:41 +08007343 case SCTP_DEFAULT_PRINFO:
7344 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7345 optlen);
7346 break;
Xin Long826d2532016-07-09 19:47:42 +08007347 case SCTP_PR_ASSOC_STATUS:
7348 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7349 optlen);
7350 break;
Xin Longd229d482017-04-01 17:07:46 +08007351 case SCTP_PR_STREAM_STATUS:
7352 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7353 optlen);
7354 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08007355 case SCTP_RECONFIG_SUPPORTED:
7356 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7357 optlen);
7358 break;
Xin Long9fb657a2017-01-18 00:44:46 +08007359 case SCTP_ENABLE_STREAM_RESET:
7360 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7361 optlen);
7362 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007363 case SCTP_STREAM_SCHEDULER:
7364 retval = sctp_getsockopt_scheduler(sk, len, optval,
7365 optlen);
7366 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007367 case SCTP_STREAM_SCHEDULER_VALUE:
7368 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7369 optlen);
7370 break;
Xin Long772a5862017-12-08 21:03:58 +08007371 case SCTP_INTERLEAVING_SUPPORTED:
7372 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7373 optlen);
7374 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 default:
7376 retval = -ENOPROTOOPT;
7377 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379
wangweidong048ed4b2014-01-21 15:44:11 +08007380 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 return retval;
7382}
7383
Craig Gallek086c6532016-02-10 11:50:35 -05007384static int sctp_hash(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385{
7386 /* STUB */
Craig Gallek086c6532016-02-10 11:50:35 -05007387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388}
7389
7390static void sctp_unhash(struct sock *sk)
7391{
7392 /* STUB */
7393}
7394
7395/* Check if port is acceptable. Possibly find first available port.
7396 *
7397 * The port hash table (contained in the 'global' SCTP protocol storage
7398 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7399 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7400 * list (the list number is the port number hashed out, so as you
7401 * would expect from a hash function, all the ports in a given list have
7402 * such a number that hashes out to the same list number; you were
7403 * expecting that, right?); so each list has a set of ports, with a
7404 * link to the socket (struct sock) that uses it, the port number and
7405 * a fastreuse flag (FIXME: NPI ipg).
7406 */
7407static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007408 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409
7410static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
7411{
7412 struct sctp_bind_hashbucket *head; /* hash list */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007413 struct sctp_bind_bucket *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414 unsigned short snum;
7415 int ret;
7416
Al Viro04afd8b2006-11-20 17:02:01 -08007417 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418
Daniel Borkmannbb333812013-06-28 19:49:40 +02007419 pr_debug("%s: begins, snum:%d\n", __func__, snum);
7420
wangweidong79b91132014-01-21 15:44:07 +08007421 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422
7423 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07007424 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007425 int low, high, remaining, index;
7426 unsigned int rover;
WANG Cong122ff242014-05-12 16:04:53 -07007427 struct net *net = sock_net(sk);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007428
WANG Cong122ff242014-05-12 16:04:53 -07007429 inet_get_local_port_range(net, &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007430 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05007431 rover = prandom_u32() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 do {
7434 rover++;
7435 if ((rover < low) || (rover > high))
7436 rover = low;
WANG Cong122ff242014-05-12 16:04:53 -07007437 if (inet_is_local_reserved_port(net, rover))
Amerigo Wange3826f12010-05-05 00:27:06 +00007438 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007439 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440 head = &sctp_port_hashtable[index];
wangweidong3c8e43b2014-01-21 15:44:08 +08007441 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007442 sctp_for_each_hentry(pp, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007443 if ((pp->port == rover) &&
7444 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445 goto next;
7446 break;
7447 next:
wangweidong3c8e43b2014-01-21 15:44:08 +08007448 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450
7451 /* Exhausted local port range during search? */
7452 ret = 1;
7453 if (remaining <= 0)
7454 goto fail;
7455
7456 /* OK, here is the one we will use. HEAD (the port
7457 * hash table list entry) is non-NULL and we hold it's
7458 * mutex.
7459 */
7460 snum = rover;
7461 } else {
7462 /* We are given an specific port number; we verify
7463 * that it is not being used. If it is used, we will
7464 * exahust the search in the hash list corresponding
7465 * to the port number (snum) - we detect that with the
7466 * port iterator, pp being NULL.
7467 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007468 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
wangweidong3c8e43b2014-01-21 15:44:08 +08007469 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007470 sctp_for_each_hentry(pp, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007471 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 goto pp_found;
7473 }
7474 }
7475 pp = NULL;
7476 goto pp_not_found;
7477pp_found:
7478 if (!hlist_empty(&pp->owner)) {
7479 /* We had a port hash table hit - there is an
7480 * available port (pp != NULL) and it is being
7481 * used by other socket (pp->owner not empty); that other
7482 * socket is going to be sk2.
7483 */
7484 int reuse = sk->sk_reuse;
7485 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007486
Daniel Borkmannbb333812013-06-28 19:49:40 +02007487 pr_debug("%s: found a possible match\n", __func__);
7488
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007489 if (pp->fastreuse && sk->sk_reuse &&
7490 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491 goto success;
7492
7493 /* Run through the list of sockets bound to the port
7494 * (pp->port) [via the pointers bind_next and
7495 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
7496 * we get the endpoint they describe and run through
7497 * the endpoint's list of IP (v4 or v6) addresses,
7498 * comparing each of the addresses with the address of
7499 * the socket sk. If we find a match, then that means
7500 * that this port/socket (sk) combination are already
7501 * in an endpoint.
7502 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007503 sk_for_each_bound(sk2, &pp->owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 struct sctp_endpoint *ep2;
7505 ep2 = sctp_sk(sk2)->ep;
7506
Vlad Yasevich4e540642008-07-18 23:06:32 -07007507 if (sk == sk2 ||
7508 (reuse && sk2->sk_reuse &&
7509 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510 continue;
7511
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07007512 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
7513 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514 ret = (long)sk2;
7515 goto fail_unlock;
7516 }
7517 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02007518
7519 pr_debug("%s: found a match\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520 }
7521pp_not_found:
7522 /* If there was a hash table miss, create a new port. */
7523 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007524 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 goto fail_unlock;
7526
7527 /* In either case (hit or miss), make sure fastreuse is 1 only
7528 * if sk->sk_reuse is too (that is, if the caller requested
7529 * SO_REUSEADDR on this socket -sk-).
7530 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007531 if (hlist_empty(&pp->owner)) {
7532 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
7533 pp->fastreuse = 1;
7534 else
7535 pp->fastreuse = 0;
7536 } else if (pp->fastreuse &&
7537 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 pp->fastreuse = 0;
7539
7540 /* We are set, so fill up all the data in the hash table
7541 * entry, tie the socket list information with the rest of the
7542 * sockets FIXME: Blurry, NPI (ipg).
7543 */
7544success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007546 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 sk_add_bind_node(sk, &pp->owner);
7548 sctp_sk(sk)->bind_hash = pp;
7549 }
7550 ret = 0;
7551
7552fail_unlock:
wangweidong3c8e43b2014-01-21 15:44:08 +08007553 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554
7555fail:
wangweidong79b91132014-01-21 15:44:07 +08007556 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557 return ret;
7558}
7559
7560/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
7561 * port is requested.
7562 */
7563static int sctp_get_port(struct sock *sk, unsigned short snum)
7564{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565 union sctp_addr addr;
7566 struct sctp_af *af = sctp_sk(sk)->pf->af;
7567
7568 /* Set up a dummy address struct from the sk. */
7569 af->from_sk(&addr, sk);
7570 addr.v4.sin_port = htons(snum);
7571
7572 /* Note: sk->sk_num gets filled in if ephemeral port request. */
Daniel Borkmann62208f12013-06-25 18:17:30 +02007573 return !!sctp_get_port_local(sk, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574}
7575
7576/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577 * Move a socket to LISTENING state.
7578 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02007579static int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007581 struct sctp_sock *sp = sctp_sk(sk);
7582 struct sctp_endpoint *ep = sp->ep;
Herbert Xu5821c762016-01-24 21:20:12 +08007583 struct crypto_shash *tfm = NULL;
Neil Horman3c681982012-10-24 09:20:03 +00007584 char alg[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585
7586 /* Allocate HMAC for generating cookie. */
Neil Horman3c681982012-10-24 09:20:03 +00007587 if (!sp->hmac && sp->sctp_hmac_alg) {
7588 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
Herbert Xu5821c762016-01-24 21:20:12 +08007589 tfm = crypto_alloc_shash(alg, 0, 0);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07007590 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00007591 net_info_ratelimited("failed to load transform for %s: %ld\n",
Neil Horman3c681982012-10-24 09:20:03 +00007592 sp->sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007593 return -ENOSYS;
7594 }
7595 sctp_sk(sk)->hmac = tfm;
7596 }
7597
7598 /*
7599 * If a bind() or sctp_bindx() is not called prior to a listen()
7600 * call that allows new associations to be accepted, the system
7601 * picks an ephemeral port and will choose an address set equivalent
7602 * to binding with a wildcard address.
7603 *
7604 * This is not currently spelled out in the SCTP sockets
7605 * extensions draft, but follows the practice as seen in TCP
7606 * sockets.
7607 *
7608 */
Yafang Shaocbabf462017-12-20 11:12:54 +08007609 inet_sk_set_state(sk, SCTP_SS_LISTENING);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007610 if (!ep->base.bind_addr.port) {
7611 if (sctp_autobind(sk))
7612 return -EAGAIN;
7613 } else {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007614 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08007615 inet_sk_set_state(sk, SCTP_SS_CLOSED);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007616 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617 }
7618 }
7619
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007620 sk->sk_max_ack_backlog = backlog;
7621 sctp_hash_endpoint(ep);
7622 return 0;
7623}
7624
7625/*
7626 * 4.1.3 / 5.1.3 listen()
7627 *
7628 * By default, new associations are not accepted for UDP style sockets.
7629 * An application uses listen() to mark a socket as being able to
7630 * accept new associations.
7631 *
7632 * On TCP style sockets, applications use listen() to ready the SCTP
7633 * endpoint for accepting inbound associations.
7634 *
7635 * On both types of endpoints a backlog of '0' disables listening.
7636 *
7637 * Move a socket to LISTENING state.
7638 */
7639int sctp_inet_listen(struct socket *sock, int backlog)
7640{
7641 struct sock *sk = sock->sk;
7642 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7643 int err = -EINVAL;
7644
7645 if (unlikely(backlog < 0))
7646 return err;
7647
wangweidong048ed4b2014-01-21 15:44:11 +08007648 lock_sock(sk);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007649
7650 /* Peeled-off sockets are not allowed to listen(). */
7651 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
7652 goto out;
7653
7654 if (sock->state != SS_UNCONNECTED)
7655 goto out;
7656
Xin Long34b27892017-04-06 13:10:52 +08007657 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
7658 goto out;
7659
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007660 /* If backlog is zero, disable listening. */
7661 if (!backlog) {
7662 if (sctp_sstate(sk, CLOSED))
7663 goto out;
7664
7665 err = 0;
7666 sctp_unhash_endpoint(ep);
7667 sk->sk_state = SCTP_SS_CLOSED;
7668 if (sk->sk_reuse)
7669 sctp_sk(sk)->bind_hash->fastreuse = 1;
7670 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007671 }
7672
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007673 /* If we are already listening, just update the backlog */
7674 if (sctp_sstate(sk, LISTENING))
7675 sk->sk_max_ack_backlog = backlog;
7676 else {
7677 err = sctp_listen_start(sk, backlog);
7678 if (err)
7679 goto out;
7680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007682 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683out:
wangweidong048ed4b2014-01-21 15:44:11 +08007684 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686}
7687
7688/*
7689 * This function is done by modeling the current datagram_poll() and the
7690 * tcp_poll(). Note that, based on these implementations, we don't
7691 * lock the socket in this function, even though it seems that,
7692 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08007693 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694 * in this place. We assume that we don't need locks either until proven
7695 * otherwise.
7696 *
7697 * Another thing to note is that we include the Async I/O support
7698 * here, again, by modeling the current TCP/UDP code. We don't have
7699 * a good way to test with it yet.
7700 */
Al Viroade994f2017-07-03 00:01:49 -04007701__poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007702{
7703 struct sock *sk = sock->sk;
7704 struct sctp_sock *sp = sctp_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -04007705 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706
Eric Dumazetaa395142010-04-20 13:03:51 +00007707 poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03007709 sock_rps_record_flow(sk);
7710
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711 /* A TCP-style listening socket becomes readable when the accept queue
7712 * is not empty.
7713 */
7714 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
7715 return (!list_empty(&sp->ep->asocs)) ?
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007716 (EPOLLIN | EPOLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717
7718 mask = 0;
7719
7720 /* Is there any exceptional events? */
7721 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007722 mask |= EPOLLERR |
7723 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
Davide Libenzif348d702006-03-25 03:07:39 -08007724 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007725 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007726 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007727 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007728
7729 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00007730 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007731 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007732
7733 /* The association is either gone or not ready. */
7734 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
7735 return mask;
7736
7737 /* Is it writable? */
7738 if (sctp_writeable(sk)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007739 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740 } else {
Eric Dumazet9cd3e072015-11-29 20:03:10 -08007741 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742 /*
7743 * Since the socket is not locked, the buffer
7744 * might be made available after the writeable check and
7745 * before the bit is set. This could cause a lost I/O
7746 * signal. tcp_poll() has a race breaker for this race
7747 * condition. Based on their implementation, we put
7748 * in the following code to cover it as well.
7749 */
7750 if (sctp_writeable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007751 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007752 }
7753 return mask;
7754}
7755
7756/********************************************************************
7757 * 2nd Level Abstractions
7758 ********************************************************************/
7759
7760static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007761 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762{
7763 struct sctp_bind_bucket *pp;
7764
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08007765 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07007767 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768 pp->port = snum;
7769 pp->fastreuse = 0;
7770 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007771 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007772 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773 }
7774 return pp;
7775}
7776
7777/* Caller must hold hashbucket lock for this tb with local BH disabled */
7778static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
7779{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07007780 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007781 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782 kmem_cache_free(sctp_bucket_cachep, pp);
7783 SCTP_DBG_OBJCNT_DEC(bind_bucket);
7784 }
7785}
7786
7787/* Release this socket's reference to a local port. */
7788static inline void __sctp_put_port(struct sock *sk)
7789{
7790 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007791 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
7792 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793 struct sctp_bind_bucket *pp;
7794
wangweidong3c8e43b2014-01-21 15:44:08 +08007795 spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796 pp = sctp_sk(sk)->bind_hash;
7797 __sk_del_bind_node(sk);
7798 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007799 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800 sctp_bucket_destroy(pp);
wangweidong3c8e43b2014-01-21 15:44:08 +08007801 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007802}
7803
7804void sctp_put_port(struct sock *sk)
7805{
wangweidong79b91132014-01-21 15:44:07 +08007806 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807 __sctp_put_port(sk);
wangweidong79b91132014-01-21 15:44:07 +08007808 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007809}
7810
7811/*
7812 * The system picks an ephemeral port and choose an address set equivalent
7813 * to binding with a wildcard address.
7814 * One of those addresses will be the primary address for the association.
7815 * This automatically enables the multihoming capability of SCTP.
7816 */
7817static int sctp_autobind(struct sock *sk)
7818{
7819 union sctp_addr autoaddr;
7820 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08007821 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822
7823 /* Initialize a local sockaddr structure to INADDR_ANY. */
7824 af = sctp_sk(sk)->pf->af;
7825
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007826 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827 af->inaddr_any(&autoaddr, port);
7828
7829 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
7830}
7831
7832/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
7833 *
7834 * From RFC 2292
7835 * 4.2 The cmsghdr Structure *
7836 *
7837 * When ancillary data is sent or received, any number of ancillary data
7838 * objects can be specified by the msg_control and msg_controllen members of
7839 * the msghdr structure, because each object is preceded by
7840 * a cmsghdr structure defining the object's length (the cmsg_len member).
7841 * Historically Berkeley-derived implementations have passed only one object
7842 * at a time, but this API allows multiple objects to be
7843 * passed in a single call to sendmsg() or recvmsg(). The following example
7844 * shows two ancillary data objects in a control buffer.
7845 *
7846 * |<--------------------------- msg_controllen -------------------------->|
7847 * | |
7848 *
7849 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
7850 *
7851 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
7852 * | | |
7853 *
7854 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
7855 *
7856 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
7857 * | | | | |
7858 *
7859 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7860 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
7861 *
7862 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
7863 *
7864 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7865 * ^
7866 * |
7867 *
7868 * msg_control
7869 * points here
7870 */
Xin Longa05437a2017-08-11 10:23:48 +08007871static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872{
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007873 struct msghdr *my_msg = (struct msghdr *)msg;
Xin Longa05437a2017-08-11 10:23:48 +08007874 struct cmsghdr *cmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875
Gu Zhengf95b4142014-12-11 11:22:04 +08007876 for_each_cmsghdr(cmsg, my_msg) {
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007877 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 return -EINVAL;
7879
7880 /* Should we parse this header or ignore? */
7881 if (cmsg->cmsg_level != IPPROTO_SCTP)
7882 continue;
7883
7884 /* Strictly check lengths following example in SCM code. */
7885 switch (cmsg->cmsg_type) {
7886 case SCTP_INIT:
7887 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007888 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 *
7890 * This cmsghdr structure provides information for
7891 * initializing new SCTP associations with sendmsg().
7892 * The SCTP_INITMSG socket option uses this same data
7893 * structure. This structure is not used for
7894 * recvmsg().
7895 *
7896 * cmsg_level cmsg_type cmsg_data[]
7897 * ------------ ------------ ----------------------
7898 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
7899 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007900 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 return -EINVAL;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007902
7903 cmsgs->init = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007904 break;
7905
7906 case SCTP_SNDRCV:
7907 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007908 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 *
7910 * This cmsghdr structure specifies SCTP options for
7911 * sendmsg() and describes SCTP header information
7912 * about a received message through recvmsg().
7913 *
7914 * cmsg_level cmsg_type cmsg_data[]
7915 * ------------ ------------ ----------------------
7916 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
7917 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007918 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 return -EINVAL;
7920
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007921 cmsgs->srinfo = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007923 if (cmsgs->srinfo->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007924 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Long49102802018-03-05 20:44:20 +08007925 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
7926 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927 return -EINVAL;
7928 break;
7929
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007930 case SCTP_SNDINFO:
7931 /* SCTP Socket API Extension
7932 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
7933 *
7934 * This cmsghdr structure specifies SCTP options for
7935 * sendmsg(). This structure and SCTP_RCVINFO replaces
7936 * SCTP_SNDRCV which has been deprecated.
7937 *
7938 * cmsg_level cmsg_type cmsg_data[]
7939 * ------------ ------------ ---------------------
7940 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
7941 */
7942 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
7943 return -EINVAL;
7944
7945 cmsgs->sinfo = CMSG_DATA(cmsg);
7946
7947 if (cmsgs->sinfo->snd_flags &
7948 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Long49102802018-03-05 20:44:20 +08007949 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
7950 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007951 return -EINVAL;
7952 break;
Xin Longed63afb2018-03-05 20:44:18 +08007953 case SCTP_PRINFO:
7954 /* SCTP Socket API Extension
7955 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
7956 *
7957 * This cmsghdr structure specifies SCTP options for sendmsg().
7958 *
7959 * cmsg_level cmsg_type cmsg_data[]
7960 * ------------ ------------ ---------------------
7961 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
7962 */
7963 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
7964 return -EINVAL;
7965
7966 cmsgs->prinfo = CMSG_DATA(cmsg);
7967 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
7968 return -EINVAL;
7969
7970 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
7971 cmsgs->prinfo->pr_value = 0;
7972 break;
Xin Long3ff547c2018-03-14 19:05:31 +08007973 case SCTP_AUTHINFO:
7974 /* SCTP Socket API Extension
7975 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
7976 *
7977 * This cmsghdr structure specifies SCTP options for sendmsg().
7978 *
7979 * cmsg_level cmsg_type cmsg_data[]
7980 * ------------ ------------ ---------------------
7981 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
7982 */
7983 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
7984 return -EINVAL;
7985
7986 cmsgs->authinfo = CMSG_DATA(cmsg);
7987 break;
Xin Long2c0dbaa2018-03-05 20:44:19 +08007988 case SCTP_DSTADDRV4:
7989 case SCTP_DSTADDRV6:
7990 /* SCTP Socket API Extension
7991 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
7992 *
7993 * This cmsghdr structure specifies SCTP options for sendmsg().
7994 *
7995 * cmsg_level cmsg_type cmsg_data[]
7996 * ------------ ------------ ---------------------
7997 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
7998 * ------------ ------------ ---------------------
7999 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
8000 */
8001 cmsgs->addrs_msg = my_msg;
8002 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003 default:
8004 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07008005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006 }
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008007
Linus Torvalds1da177e2005-04-16 15:20:36 -07008008 return 0;
8009}
8010
8011/*
8012 * Wait for a packet..
8013 * Note: This function is the same function as in core/datagram.c
8014 * with a few modifications to make lksctp work.
8015 */
wangweidong26ac8e52013-12-23 12:16:51 +08008016static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008017{
8018 int error;
8019 DEFINE_WAIT(wait);
8020
Eric Dumazetaa395142010-04-20 13:03:51 +00008021 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008022
8023 /* Socket errors? */
8024 error = sock_error(sk);
8025 if (error)
8026 goto out;
8027
8028 if (!skb_queue_empty(&sk->sk_receive_queue))
8029 goto ready;
8030
8031 /* Socket shut down? */
8032 if (sk->sk_shutdown & RCV_SHUTDOWN)
8033 goto out;
8034
8035 /* Sequenced packets can come disconnected. If so we report the
8036 * problem.
8037 */
8038 error = -ENOTCONN;
8039
8040 /* Is there a good reason to think that we may receive some data? */
8041 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8042 goto out;
8043
8044 /* Handle signals. */
8045 if (signal_pending(current))
8046 goto interrupted;
8047
8048 /* Let another process have a go. Since we are going to sleep
8049 * anyway. Note: This may cause odd behaviors if the message
8050 * does not fit in the user's buffer, but this seems to be the
8051 * only way to honor MSG_DONTWAIT realistically.
8052 */
wangweidong048ed4b2014-01-21 15:44:11 +08008053 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008054 *timeo_p = schedule_timeout(*timeo_p);
wangweidong048ed4b2014-01-21 15:44:11 +08008055 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056
8057ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00008058 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059 return 0;
8060
8061interrupted:
8062 error = sock_intr_errno(*timeo_p);
8063
8064out:
Eric Dumazetaa395142010-04-20 13:03:51 +00008065 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008066 *err = error;
8067 return error;
8068}
8069
8070/* Receive a datagram.
8071 * Note: This is pretty much the same routine as in core/datagram.c
8072 * with a few changes to make lksctp work.
8073 */
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02008074struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8075 int noblock, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008076{
8077 int error;
8078 struct sk_buff *skb;
8079 long timeo;
8080
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081 timeo = sock_rcvtimeo(sk, noblock);
8082
Daniel Borkmannbb333812013-06-28 19:49:40 +02008083 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8084 MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008085
8086 do {
8087 /* Again only user level code calls this function,
8088 * so nothing interrupt level
8089 * will suddenly eat the receive_queue.
8090 *
8091 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00008092 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008093 */
8094 if (flags & MSG_PEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008095 skb = skb_peek(&sk->sk_receive_queue);
8096 if (skb)
Reshetova, Elena63354792017-06-30 13:07:58 +03008097 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008098 } else {
Marcelo Ricardo Leitner311b2172016-04-13 19:12:29 -03008099 skb = __skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008100 }
8101
8102 if (skb)
8103 return skb;
8104
Neil Horman6736dc32005-12-02 20:30:06 -08008105 /* Caller is allowed not to check sk->sk_err before calling. */
8106 error = sock_error(sk);
8107 if (error)
8108 goto no_packet;
8109
Linus Torvalds1da177e2005-04-16 15:20:36 -07008110 if (sk->sk_shutdown & RCV_SHUTDOWN)
8111 break;
8112
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07008113 if (sk_can_busy_loop(sk)) {
8114 sk_busy_loop(sk, noblock);
8115
8116 if (!skb_queue_empty(&sk->sk_receive_queue))
8117 continue;
8118 }
Neil Horman8465a5f2014-04-17 15:26:51 -04008119
Linus Torvalds1da177e2005-04-16 15:20:36 -07008120 /* User doesn't want to wait. */
8121 error = -EAGAIN;
8122 if (!timeo)
8123 goto no_packet;
8124 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8125
8126 return NULL;
8127
8128no_packet:
8129 *err = error;
8130 return NULL;
8131}
8132
8133/* If sndbuf has changed, wake up per association sndbuf waiters. */
8134static void __sctp_write_space(struct sctp_association *asoc)
8135{
8136 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008137
Eric Dumazetceb5d582015-11-29 20:03:11 -08008138 if (sctp_wspace(asoc) <= 0)
8139 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008140
Eric Dumazetceb5d582015-11-29 20:03:11 -08008141 if (waitqueue_active(&asoc->wait))
8142 wake_up_interruptible(&asoc->wait);
Eric Dumazeteaefd112011-02-18 03:26:36 +00008143
Eric Dumazetceb5d582015-11-29 20:03:11 -08008144 if (sctp_writeable(sk)) {
8145 struct socket_wq *wq;
8146
8147 rcu_read_lock();
8148 wq = rcu_dereference(sk->sk_wq);
8149 if (wq) {
8150 if (waitqueue_active(&wq->wait))
8151 wake_up_interruptible(&wq->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152
8153 /* Note that we try to include the Async I/O support
8154 * here by modeling from the current TCP/UDP code.
8155 * We have not tested with it yet.
8156 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00008157 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Eric Dumazetceb5d582015-11-29 20:03:11 -08008158 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008159 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08008160 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 }
8162}
8163
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008164static void sctp_wake_up_waiters(struct sock *sk,
8165 struct sctp_association *asoc)
8166{
8167 struct sctp_association *tmp = asoc;
8168
8169 /* We do accounting for the sndbuf space per association,
8170 * so we only need to wake our own association.
8171 */
8172 if (asoc->ep->sndbuf_policy)
8173 return __sctp_write_space(asoc);
8174
Daniel Borkmann1e1cdf82014-04-09 16:10:20 +02008175 /* If association goes down and is just flushing its
8176 * outq, then just normally notify others.
8177 */
8178 if (asoc->base.dead)
8179 return sctp_write_space(sk);
8180
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008181 /* Accounting for the sndbuf space is per socket, so we
8182 * need to wake up others, try to be fair and in case of
8183 * other associations, let them have a go first instead
8184 * of just doing a sctp_write_space() call.
8185 *
8186 * Note that we reach sctp_wake_up_waiters() only when
8187 * associations free up queued chunks, thus we are under
8188 * lock and the list of associations on a socket is
8189 * guaranteed not to change.
8190 */
8191 for (tmp = list_next_entry(tmp, asocs); 1;
8192 tmp = list_next_entry(tmp, asocs)) {
8193 /* Manually skip the head element. */
8194 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8195 continue;
8196 /* Wake up association. */
8197 __sctp_write_space(tmp);
8198 /* We've reached the end. */
8199 if (tmp == asoc)
8200 break;
8201 }
8202}
8203
Linus Torvalds1da177e2005-04-16 15:20:36 -07008204/* Do accounting for the sndbuf space.
8205 * Decrement the used sndbuf space of the corresponding association by the
8206 * data size which was just transmitted(freed).
8207 */
8208static void sctp_wfree(struct sk_buff *skb)
8209{
Daniel Borkmannf869c912014-11-20 01:54:48 +01008210 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8211 struct sctp_association *asoc = chunk->asoc;
8212 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008213
Neil Horman4eb701d2005-04-28 12:02:04 -07008214 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
8215 sizeof(struct sk_buff) +
8216 sizeof(struct sctp_chunk);
8217
Reshetova, Elena14afee42017-06-30 13:08:00 +03008218 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
Neil Horman4eb701d2005-04-28 12:02:04 -07008219
Neil Horman4d93df02007-08-15 16:07:44 -07008220 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008221 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
Neil Horman4d93df02007-08-15 16:07:44 -07008222 */
8223 sk->sk_wmem_queued -= skb->truesize;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008224 sk_mem_uncharge(sk, skb->truesize);
Neil Horman4d93df02007-08-15 16:07:44 -07008225
Xin Longec2e5062018-03-14 19:05:33 +08008226 if (chunk->shkey) {
8227 struct sctp_shared_key *shkey = chunk->shkey;
8228
8229 /* refcnt == 2 and !list_empty mean after this release, it's
8230 * not being used anywhere, and it's time to notify userland
8231 * that this shkey can be freed if it's been deactivated.
8232 */
8233 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
8234 refcount_read(&shkey->refcnt) == 2) {
8235 struct sctp_ulpevent *ev;
8236
8237 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
8238 SCTP_AUTH_FREE_KEY,
8239 GFP_KERNEL);
8240 if (ev)
8241 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
8242 }
Xin Long1b1e0bc2018-03-14 19:05:30 +08008243 sctp_auth_shkey_release(chunk->shkey);
Xin Longec2e5062018-03-14 19:05:33 +08008244 }
Xin Long1b1e0bc2018-03-14 19:05:30 +08008245
Neil Horman4eb701d2005-04-28 12:02:04 -07008246 sock_wfree(skb);
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008247 sctp_wake_up_waiters(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248
8249 sctp_association_put(asoc);
8250}
8251
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008252/* Do accounting for the receive space on the socket.
8253 * Accounting for the association is done in ulpevent.c
8254 * We set this as a destructor for the cloned data skbs so that
8255 * accounting is done at the correct time.
8256 */
8257void sctp_sock_rfree(struct sk_buff *skb)
8258{
8259 struct sock *sk = skb->sk;
8260 struct sctp_ulpevent *event = sctp_skb2event(skb);
8261
8262 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07008263
8264 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008265 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07008266 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008267 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008268}
8269
8270
Linus Torvalds1da177e2005-04-16 15:20:36 -07008271/* Helper function to wait for space in the sndbuf. */
8272static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +08008273 size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008274{
8275 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008276 long current_timeo = *timeo_p;
8277 DEFINE_WAIT(wait);
Xin Longa0ff6602018-01-15 17:01:36 +08008278 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008279
Daniel Borkmannbb333812013-06-28 19:49:40 +02008280 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8281 *timeo_p, msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008282
8283 /* Increment the association's refcnt. */
8284 sctp_association_hold(asoc);
8285
8286 /* Wait on the association specific sndbuf space. */
8287 for (;;) {
8288 prepare_to_wait_exclusive(&asoc->wait, &wait,
8289 TASK_INTERRUPTIBLE);
Xin Longca3af4d2017-11-15 16:55:54 +08008290 if (asoc->base.dead)
8291 goto do_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008292 if (!*timeo_p)
8293 goto do_nonblock;
Xin Longca3af4d2017-11-15 16:55:54 +08008294 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008295 goto do_error;
8296 if (signal_pending(current))
8297 goto do_interrupted;
8298 if (msg_len <= sctp_wspace(asoc))
8299 break;
8300
8301 /* Let another process have a go. Since we are going
8302 * to sleep anyway.
8303 */
wangweidong048ed4b2014-01-21 15:44:11 +08008304 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008306 lock_sock(sk);
Xin Longa0ff6602018-01-15 17:01:36 +08008307 if (sk != asoc->base.sk)
8308 goto do_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008309
8310 *timeo_p = current_timeo;
8311 }
8312
8313out:
8314 finish_wait(&asoc->wait, &wait);
8315
8316 /* Release the association's refcnt. */
8317 sctp_association_put(asoc);
8318
8319 return err;
8320
Xin Longca3af4d2017-11-15 16:55:54 +08008321do_dead:
8322 err = -ESRCH;
8323 goto out;
8324
Linus Torvalds1da177e2005-04-16 15:20:36 -07008325do_error:
8326 err = -EPIPE;
8327 goto out;
8328
8329do_interrupted:
8330 err = sock_intr_errno(*timeo_p);
8331 goto out;
8332
8333do_nonblock:
8334 err = -EAGAIN;
8335 goto out;
8336}
8337
David S. Miller676d2362014-04-11 16:15:36 -04008338void sctp_data_ready(struct sock *sk)
Wei Yongjun561b1732010-04-28 08:47:18 +00008339{
David S. Miller7ef52732010-05-02 21:43:40 -07008340 struct socket_wq *wq;
8341
8342 rcu_read_lock();
8343 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08008344 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008345 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8346 EPOLLRDNORM | EPOLLRDBAND);
Wei Yongjun561b1732010-04-28 08:47:18 +00008347 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07008348 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00008349}
8350
Linus Torvalds1da177e2005-04-16 15:20:36 -07008351/* If socket sndbuf has changed, wake up all per association waiters. */
8352void sctp_write_space(struct sock *sk)
8353{
8354 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355
8356 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07008357 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008358 __sctp_write_space(asoc);
8359 }
8360}
8361
8362/* Is there any sndbuf space available on the socket?
8363 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08008364 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365 * associations on the same socket. For a UDP-style socket with
8366 * multiple associations, it is possible for it to be "unwriteable"
8367 * prematurely. I assume that this is acceptable because
8368 * a premature "unwriteable" is better than an accidental "writeable" which
8369 * would cause an unwanted block under certain circumstances. For the 1-1
8370 * UDP-style sockets or TCP-style sockets, this code should work.
8371 * - Daisy
8372 */
8373static int sctp_writeable(struct sock *sk)
8374{
8375 int amt = 0;
8376
Eric Dumazet31e6d362009-06-17 19:05:41 -07008377 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378 if (amt < 0)
8379 amt = 0;
8380 return amt;
8381}
8382
8383/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8384 * returns immediately with EINPROGRESS.
8385 */
8386static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8387{
8388 struct sock *sk = asoc->base.sk;
8389 int err = 0;
8390 long current_timeo = *timeo_p;
8391 DEFINE_WAIT(wait);
8392
Daniel Borkmannbb333812013-06-28 19:49:40 +02008393 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394
8395 /* Increment the association's refcnt. */
8396 sctp_association_hold(asoc);
8397
8398 for (;;) {
8399 prepare_to_wait_exclusive(&asoc->wait, &wait,
8400 TASK_INTERRUPTIBLE);
8401 if (!*timeo_p)
8402 goto do_nonblock;
8403 if (sk->sk_shutdown & RCV_SHUTDOWN)
8404 break;
8405 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
8406 asoc->base.dead)
8407 goto do_error;
8408 if (signal_pending(current))
8409 goto do_interrupted;
8410
8411 if (sctp_state(asoc, ESTABLISHED))
8412 break;
8413
8414 /* Let another process have a go. Since we are going
8415 * to sleep anyway.
8416 */
wangweidong048ed4b2014-01-21 15:44:11 +08008417 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008418 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008419 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008420
8421 *timeo_p = current_timeo;
8422 }
8423
8424out:
8425 finish_wait(&asoc->wait, &wait);
8426
8427 /* Release the association's refcnt. */
8428 sctp_association_put(asoc);
8429
8430 return err;
8431
8432do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08008433 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008434 err = -ETIMEDOUT;
8435 else
8436 err = -ECONNREFUSED;
8437 goto out;
8438
8439do_interrupted:
8440 err = sock_intr_errno(*timeo_p);
8441 goto out;
8442
8443do_nonblock:
8444 err = -EINPROGRESS;
8445 goto out;
8446}
8447
8448static int sctp_wait_for_accept(struct sock *sk, long timeo)
8449{
8450 struct sctp_endpoint *ep;
8451 int err = 0;
8452 DEFINE_WAIT(wait);
8453
8454 ep = sctp_sk(sk)->ep;
8455
8456
8457 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00008458 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459 TASK_INTERRUPTIBLE);
8460
8461 if (list_empty(&ep->asocs)) {
wangweidong048ed4b2014-01-21 15:44:11 +08008462 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463 timeo = schedule_timeout(timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008464 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008465 }
8466
8467 err = -EINVAL;
8468 if (!sctp_sstate(sk, LISTENING))
8469 break;
8470
8471 err = 0;
8472 if (!list_empty(&ep->asocs))
8473 break;
8474
8475 err = sock_intr_errno(timeo);
8476 if (signal_pending(current))
8477 break;
8478
8479 err = -EAGAIN;
8480 if (!timeo)
8481 break;
8482 }
8483
Eric Dumazetaa395142010-04-20 13:03:51 +00008484 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485
8486 return err;
8487}
8488
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02008489static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490{
8491 DEFINE_WAIT(wait);
8492
8493 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00008494 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008495 if (list_empty(&sctp_sk(sk)->ep->asocs))
8496 break;
wangweidong048ed4b2014-01-21 15:44:11 +08008497 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008498 timeout = schedule_timeout(timeout);
wangweidong048ed4b2014-01-21 15:44:11 +08008499 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008500 } while (!signal_pending(current) && timeout);
8501
Eric Dumazetaa395142010-04-20 13:03:51 +00008502 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503}
8504
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008505static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
8506{
8507 struct sk_buff *frag;
8508
8509 if (!skb->data_len)
8510 goto done;
8511
8512 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07008513 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008514 sctp_skb_set_owner_r_frag(frag, sk);
8515
8516done:
8517 sctp_skb_set_owner_r(skb, sk);
8518}
8519
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008520void sctp_copy_sock(struct sock *newsk, struct sock *sk,
8521 struct sctp_association *asoc)
8522{
8523 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08008524 struct inet_sock *newinet;
Richard Haines2277c7c2018-02-13 20:56:24 +00008525 struct sctp_sock *sp = sctp_sk(sk);
8526 struct sctp_endpoint *ep = sp->ep;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008527
8528 newsk->sk_type = sk->sk_type;
8529 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
8530 newsk->sk_flags = sk->sk_flags;
Marcelo Ricardo Leitner50a5ffb2015-12-04 15:14:05 -02008531 newsk->sk_tsflags = sk->sk_tsflags;
Tom Herbert28448b82014-05-23 08:47:19 -07008532 newsk->sk_no_check_tx = sk->sk_no_check_tx;
8533 newsk->sk_no_check_rx = sk->sk_no_check_rx;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008534 newsk->sk_reuse = sk->sk_reuse;
8535
8536 newsk->sk_shutdown = sk->sk_shutdown;
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02008537 newsk->sk_destruct = sctp_destruct_sock;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008538 newsk->sk_family = sk->sk_family;
8539 newsk->sk_protocol = IPPROTO_SCTP;
8540 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
8541 newsk->sk_sndbuf = sk->sk_sndbuf;
8542 newsk->sk_rcvbuf = sk->sk_rcvbuf;
8543 newsk->sk_lingertime = sk->sk_lingertime;
8544 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
8545 newsk->sk_sndtimeo = sk->sk_sndtimeo;
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03008546 newsk->sk_rxhash = sk->sk_rxhash;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008547
8548 newinet = inet_sk(newsk);
8549
8550 /* Initialize sk's sport, dport, rcv_saddr and daddr for
8551 * getsockname() and getpeername()
8552 */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008553 newinet->inet_sport = inet->inet_sport;
8554 newinet->inet_saddr = inet->inet_saddr;
8555 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
8556 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008557 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008558 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008559
8560 newinet->uc_ttl = inet->uc_ttl;
8561 newinet->mc_loop = 1;
8562 newinet->mc_ttl = 1;
8563 newinet->mc_index = 0;
8564 newinet->mc_list = NULL;
Marcelo Ricardo Leitner01ce63c2015-12-04 15:14:04 -02008565
8566 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
8567 net_enable_timestamp();
Marcelo Ricardo Leitner3538a5c2015-12-23 16:44:09 -02008568
Richard Haines2277c7c2018-02-13 20:56:24 +00008569 /* Set newsk security attributes from orginal sk and connection
8570 * security attribute from ep.
8571 */
8572 security_sctp_sk_clone(ep, sk, newsk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008573}
8574
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008575static inline void sctp_copy_descendant(struct sock *sk_to,
8576 const struct sock *sk_from)
8577{
8578 int ancestor_size = sizeof(struct inet_sock) +
8579 sizeof(struct sctp_sock) -
8580 offsetof(struct sctp_sock, auto_asconf_list);
8581
8582 if (sk_from->sk_family == PF_INET6)
8583 ancestor_size += sizeof(struct ipv6_pinfo);
8584
8585 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
8586}
8587
Linus Torvalds1da177e2005-04-16 15:20:36 -07008588/* Populate the fields of the newsk from the oldsk and migrate the assoc
8589 * and its messages to the newsk.
8590 */
8591static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
8592 struct sctp_association *assoc,
Xin Longb7ef2612017-08-11 10:23:50 +08008593 enum sctp_socket_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008594{
8595 struct sctp_sock *oldsp = sctp_sk(oldsk);
8596 struct sctp_sock *newsp = sctp_sk(newsk);
8597 struct sctp_bind_bucket *pp; /* hash list port iterator */
8598 struct sctp_endpoint *newep = newsp->ep;
8599 struct sk_buff *skb, *tmp;
8600 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08008601 struct sctp_bind_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008602
8603 /* Migrate socket buffer sizes and all the socket level options to the
8604 * new socket.
8605 */
8606 newsk->sk_sndbuf = oldsk->sk_sndbuf;
8607 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
8608 /* Brute force copy old sctp opt. */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008609 sctp_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008610
8611 /* Restore the ep value that was overwritten with the above structure
8612 * copy.
8613 */
8614 newsp->ep = newep;
8615 newsp->hmac = NULL;
8616
8617 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008618 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
8619 inet_sk(oldsk)->inet_num)];
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008620 spin_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621 pp = sctp_sk(oldsk)->bind_hash;
8622 sk_add_bind_node(newsk, &pp->owner);
8623 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008624 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008625 spin_unlock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008626
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008627 /* Copy the bind_addr list from the original endpoint to the new
8628 * endpoint so that we can handle restarts properly
8629 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08008630 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
8631 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008632
Linus Torvalds1da177e2005-04-16 15:20:36 -07008633 /* Move any messages in the old socket's receive queue that are for the
8634 * peeled off association to the new socket's receive queue.
8635 */
8636 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
8637 event = sctp_skb2event(skb);
8638 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008639 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008641 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008642 }
8643 }
8644
8645 /* Clean up any messages pending delivery due to partial
8646 * delivery. Three cases:
8647 * 1) No partial deliver; no work.
8648 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
8649 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
8650 */
8651 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008652 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008653
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008654 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008655 struct sk_buff_head *queue;
8656
8657 /* Decide which queue to move pd_lobby skbs to. */
8658 if (assoc->ulpq.pd_mode) {
8659 queue = &newsp->pd_lobby;
8660 } else
8661 queue = &newsk->sk_receive_queue;
8662
8663 /* Walk through the pd_lobby, looking for skbs that
8664 * need moved to the new socket.
8665 */
8666 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
8667 event = sctp_skb2event(skb);
8668 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008669 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008670 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008671 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008672 }
8673 }
8674
8675 /* Clear up any skbs waiting for the partial
8676 * delivery to finish.
8677 */
8678 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008679 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008680
8681 }
8682
Xin Long13228232017-12-08 21:04:09 +08008683 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008684
Linus Torvalds1da177e2005-04-16 15:20:36 -07008685 /* Set the type of socket to indicate that it is peeled off from the
8686 * original UDP-style socket or created with the accept() call on a
8687 * TCP-style socket..
8688 */
8689 newsp->type = type;
8690
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008691 /* Mark the new socket "in-use" by the user so that any packets
8692 * that may arrive on the association after we've moved it are
8693 * queued to the backlog. This prevents a potential race between
8694 * backlog processing on the old socket and new-packet processing
8695 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07008696 *
8697 * The caller has just allocated newsk so we can guarantee that other
8698 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008699 */
Zach Brown5131a182007-06-22 15:14:46 -07008700 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Xin Longd04adf12017-10-28 02:13:29 +08008701 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702 sctp_assoc_migrate(assoc, newsk);
Xin Longd04adf12017-10-28 02:13:29 +08008703 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008704
8705 /* If the association on the newsk is already closed before accept()
8706 * is called, set RCV_SHUTDOWN flag.
8707 */
Xin Longd46e4162016-06-09 22:48:18 +08008708 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08008709 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710 newsk->sk_shutdown |= RCV_SHUTDOWN;
Xin Longd46e4162016-06-09 22:48:18 +08008711 } else {
Yafang Shaocbabf462017-12-20 11:12:54 +08008712 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
Xin Longd46e4162016-06-09 22:48:18 +08008713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008714
wangweidong048ed4b2014-01-21 15:44:11 +08008715 release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008716}
8717
Neil Horman4d93df02007-08-15 16:07:44 -07008718
Linus Torvalds1da177e2005-04-16 15:20:36 -07008719/* This proto struct describes the ULP interface for SCTP. */
8720struct proto sctp_prot = {
8721 .name = "SCTP",
8722 .owner = THIS_MODULE,
8723 .close = sctp_close,
8724 .connect = sctp_connect,
8725 .disconnect = sctp_disconnect,
8726 .accept = sctp_accept,
8727 .ioctl = sctp_ioctl,
8728 .init = sctp_init_sock,
8729 .destroy = sctp_destroy_sock,
8730 .shutdown = sctp_shutdown,
8731 .setsockopt = sctp_setsockopt,
8732 .getsockopt = sctp_getsockopt,
8733 .sendmsg = sctp_sendmsg,
8734 .recvmsg = sctp_recvmsg,
8735 .bind = sctp_bind,
8736 .backlog_rcv = sctp_backlog_rcv,
8737 .hash = sctp_hash,
8738 .unhash = sctp_unhash,
8739 .get_port = sctp_get_port,
8740 .obj_size = sizeof(struct sctp_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07008741 .useroffset = offsetof(struct sctp_sock, subscribe),
8742 .usersize = offsetof(struct sctp_sock, initmsg) -
8743 offsetof(struct sctp_sock, subscribe) +
8744 sizeof_field(struct sctp_sock, initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07008745 .sysctl_mem = sysctl_sctp_mem,
8746 .sysctl_rmem = sysctl_sctp_rmem,
8747 .sysctl_wmem = sysctl_sctp_wmem,
8748 .memory_pressure = &sctp_memory_pressure,
8749 .enter_memory_pressure = sctp_enter_memory_pressure,
8750 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008751 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752};
8753
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008754#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08008755
Eric Dumazet602dd622015-12-01 07:20:07 -08008756#include <net/transp_v6.h>
8757static void sctp_v6_destroy_sock(struct sock *sk)
8758{
8759 sctp_destroy_sock(sk);
8760 inet6_destroy_sock(sk);
8761}
8762
Linus Torvalds1da177e2005-04-16 15:20:36 -07008763struct proto sctpv6_prot = {
8764 .name = "SCTPv6",
8765 .owner = THIS_MODULE,
8766 .close = sctp_close,
8767 .connect = sctp_connect,
8768 .disconnect = sctp_disconnect,
8769 .accept = sctp_accept,
8770 .ioctl = sctp_ioctl,
8771 .init = sctp_init_sock,
Eric Dumazet602dd622015-12-01 07:20:07 -08008772 .destroy = sctp_v6_destroy_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773 .shutdown = sctp_shutdown,
8774 .setsockopt = sctp_setsockopt,
8775 .getsockopt = sctp_getsockopt,
8776 .sendmsg = sctp_sendmsg,
8777 .recvmsg = sctp_recvmsg,
8778 .bind = sctp_bind,
8779 .backlog_rcv = sctp_backlog_rcv,
8780 .hash = sctp_hash,
8781 .unhash = sctp_unhash,
8782 .get_port = sctp_get_port,
8783 .obj_size = sizeof(struct sctp6_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07008784 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
8785 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
8786 offsetof(struct sctp6_sock, sctp.subscribe) +
8787 sizeof_field(struct sctp6_sock, sctp.initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07008788 .sysctl_mem = sysctl_sctp_mem,
8789 .sysctl_rmem = sysctl_sctp_rmem,
8790 .sysctl_wmem = sysctl_sctp_wmem,
8791 .memory_pressure = &sctp_memory_pressure,
8792 .enter_memory_pressure = sctp_enter_memory_pressure,
8793 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008794 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008795};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008796#endif /* IS_ENABLED(CONFIG_IPV6) */