blob: a0ccfa4b82205340b1c0aa6f86893e6e497b8475 [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
8 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05009 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
13 *
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
22 * any later version.
23 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050024 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080031 * along with GNU CC; see the file COPYING. If not, see
32 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
34 * Please send any bug reports or fixes you make to the
35 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020036 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * Written or modified by:
39 * La Monte H.P. Yarroll <piggy@acm.org>
40 * Narasimha Budihal <narsi@refcode.org>
41 * Karl Knutson <karl@athena.chicago.il.us>
42 * Jon Grimm <jgrimm@us.ibm.com>
43 * Xingang Guo <xingang.guo@intel.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Sridhar Samudrala <samudrala@us.ibm.com>
46 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
47 * Ardelle Fan <ardelle.fan@intel.com>
48 * Ryan Layer <rmlayer@us.ibm.com>
49 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
50 * Kevin Gao <kevin.gao@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
52
Joe Perches145ce502010-08-24 13:21:08 +000053#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
Herbert Xu5821c762016-01-24 21:20:12 +080055#include <crypto/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/types.h>
57#include <linux/kernel.h>
58#include <linux/wait.h>
59#include <linux/time.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010060#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/ip.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080062#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/fcntl.h>
64#include <linux/poll.h>
65#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090066#include <linux/slab.h>
Al Viro56b31d12012-08-18 00:25:51 -040067#include <linux/file.h>
Daniel Borkmannffd59392014-02-17 12:11:11 +010068#include <linux/compat.h>
NeilBrown0eb71a92018-06-18 12:52:50 +100069#include <linux/rhashtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71#include <net/ip.h>
72#include <net/icmp.h>
73#include <net/route.h>
74#include <net/ipv6.h>
75#include <net/inet_common.h>
Neil Horman8465a5f2014-04-17 15:26:51 -040076#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#include <linux/socket.h> /* for sa_family_t */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040079#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <net/sock.h>
81#include <net/sctp/sctp.h>
82#include <net/sctp/sm.h>
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -030083#include <net/sctp/stream_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* Forward declarations for internal helper functions. */
86static int sctp_writeable(struct sock *sk);
87static void sctp_wfree(struct sk_buff *skb);
Xin Longcea0cc82017-11-15 16:57:26 +080088static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +080089 size_t msg_len);
wangweidong26ac8e52013-12-23 12:16:51 +080090static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
92static int sctp_wait_for_accept(struct sock *sk, long timeo);
93static void sctp_wait_for_close(struct sock *sk, long timeo);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +020094static void sctp_destruct_sock(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
96 union sctp_addr *addr, int len);
97static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
98static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
99static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
100static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
101static int sctp_send_asconf(struct sctp_association *asoc,
102 struct sctp_chunk *chunk);
103static int sctp_do_bind(struct sock *, union sctp_addr *, int);
104static int sctp_autobind(struct sock *sk);
Xin Longb7ef2612017-08-11 10:23:50 +0800105static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
106 struct sctp_association *assoc,
107 enum sctp_socket_type type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Eric Dumazet06044752017-06-07 13:29:12 -0700109static unsigned long sctp_memory_pressure;
Eric Dumazet8d987e52010-11-09 23:24:26 +0000110static atomic_long_t sctp_memory_allocated;
Eric Dumazet17483762008-11-25 21:16:35 -0800111struct percpu_counter sctp_sockets_allocated;
Neil Horman4d93df02007-08-15 16:07:44 -0700112
Pavel Emelyanov5c52ba12008-07-16 20:28:10 -0700113static void sctp_enter_memory_pressure(struct sock *sk)
Neil Horman4d93df02007-08-15 16:07:44 -0700114{
115 sctp_memory_pressure = 1;
116}
117
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* Get the sndbuf space available at the time on the association. */
120static inline int sctp_wspace(struct sctp_association *asoc)
121{
Neil Horman4d93df02007-08-15 16:07:44 -0700122 int amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Neil Horman4d93df02007-08-15 16:07:44 -0700124 if (asoc->ep->sndbuf_policy)
125 amt = asoc->sndbuf_used;
126 else
Eric Dumazet31e6d362009-06-17 19:05:41 -0700127 amt = sk_wmem_alloc_get(asoc->base.sk);
Neil Horman4d93df02007-08-15 16:07:44 -0700128
129 if (amt >= asoc->base.sk->sk_sndbuf) {
130 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
131 amt = 0;
132 else {
133 amt = sk_stream_wspace(asoc->base.sk);
134 if (amt < 0)
135 amt = 0;
136 }
Neil Horman4eb701d2005-04-28 12:02:04 -0700137 } else {
Neil Horman4d93df02007-08-15 16:07:44 -0700138 amt = asoc->base.sk->sk_sndbuf - amt;
Neil Horman4eb701d2005-04-28 12:02:04 -0700139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return amt;
141}
142
143/* Increment the used sndbuf space count of the corresponding association by
144 * the size of the outgoing data chunk.
145 * Also, set the skb destructor for sndbuf accounting later.
146 *
147 * Since it is always 1-1 between chunk and skb, and also a new skb is always
148 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
149 * destructor in the data chunk skb for the purpose of the sndbuf space
150 * tracking.
151 */
152static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
153{
154 struct sctp_association *asoc = chunk->asoc;
155 struct sock *sk = asoc->base.sk;
156
157 /* The sndbuf space is tracked per association. */
158 sctp_association_hold(asoc);
159
Xin Long1b1e0bc2018-03-14 19:05:30 +0800160 if (chunk->shkey)
161 sctp_auth_shkey_hold(chunk->shkey);
162
Neil Horman4eb701d2005-04-28 12:02:04 -0700163 skb_set_owner_w(chunk->skb, sk);
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 chunk->skb->destructor = sctp_wfree;
166 /* Save the chunk pointer in skb for sctp_wfree to use later. */
Daniel Borkmannf869c912014-11-20 01:54:48 +0100167 skb_shinfo(chunk->skb)->destructor_arg = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Neil Horman4eb701d2005-04-28 12:02:04 -0700169 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
170 sizeof(struct sk_buff) +
171 sizeof(struct sctp_chunk);
172
Reshetova, Elena14afee42017-06-30 13:08:00 +0300173 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800174 sk->sk_wmem_queued += chunk->skb->truesize;
175 sk_mem_charge(sk, chunk->skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Xin Longd04adf12017-10-28 02:13:29 +0800178static void sctp_clear_owner_w(struct sctp_chunk *chunk)
179{
180 skb_orphan(chunk->skb);
181}
182
183static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
184 void (*cb)(struct sctp_chunk *))
185
186{
187 struct sctp_outq *q = &asoc->outqueue;
188 struct sctp_transport *t;
189 struct sctp_chunk *chunk;
190
191 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
192 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
193 cb(chunk);
194
Xin Longa8dd3972017-11-26 20:56:07 +0800195 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800196 cb(chunk);
197
Xin Longa8dd3972017-11-26 20:56:07 +0800198 list_for_each_entry(chunk, &q->sacked, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800199 cb(chunk);
200
Xin Longa8dd3972017-11-26 20:56:07 +0800201 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800202 cb(chunk);
203
204 list_for_each_entry(chunk, &q->out_chunk_list, list)
205 cb(chunk);
206}
207
Xin Long13228232017-12-08 21:04:09 +0800208static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
209 void (*cb)(struct sk_buff *, struct sock *))
210
211{
212 struct sk_buff *skb, *tmp;
213
214 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
215 cb(skb, sk);
216
217 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
218 cb(skb, sk);
219
220 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
221 cb(skb, sk);
222}
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/* Verify that this is a valid address. */
225static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
226 int len)
227{
228 struct sctp_af *af;
229
230 /* Verify basic sockaddr. */
231 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
232 if (!af)
233 return -EINVAL;
234
235 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700236 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return -EINVAL;
238
239 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
240 return -EINVAL;
241
242 return 0;
243}
244
245/* Look up the association by its id. If this is not a UDP-style
246 * socket, the ID field is always ignored.
247 */
248struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
249{
250 struct sctp_association *asoc = NULL;
251
252 /* If this is not a UDP-style socket, assoc id should be ignored. */
253 if (!sctp_style(sk, UDP)) {
254 /* Return NULL if the socket state is not ESTABLISHED. It
255 * could be a TCP-style listening socket or a socket which
256 * hasn't yet called connect() to establish an association.
257 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -0300258 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return NULL;
260
261 /* Get the first and the only association from the list. */
262 if (!list_empty(&sctp_sk(sk)->ep->asocs))
263 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
264 struct sctp_association, asocs);
265 return asoc;
266 }
267
268 /* Otherwise this is a UDP-style socket. */
269 if (!id || (id == (sctp_assoc_t)-1))
270 return NULL;
271
272 spin_lock_bh(&sctp_assocs_id_lock);
273 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
274 spin_unlock_bh(&sctp_assocs_id_lock);
275
276 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
277 return NULL;
278
279 return asoc;
280}
281
282/* Look up the transport from an address and an assoc id. If both address and
283 * id are specified, the associations matching the address and the id should be
284 * the same.
285 */
286static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
287 struct sockaddr_storage *addr,
288 sctp_assoc_t id)
289{
290 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
Xin Long6f29a132017-01-24 14:01:53 +0800291 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 union sctp_addr *laddr = (union sctp_addr *)addr;
Xin Long6f29a132017-01-24 14:01:53 +0800293 struct sctp_transport *transport;
294
Xin Long912964e2017-02-07 20:56:08 +0800295 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
Xin Long6f29a132017-01-24 14:01:53 +0800296 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800299 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (!addr_asoc)
303 return NULL;
304
305 id_asoc = sctp_id2assoc(sk, id);
306 if (id_asoc && (id_asoc != addr_asoc))
307 return NULL;
308
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600309 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 (union sctp_addr *)addr);
311
312 return transport;
313}
314
315/* API 3.1.2 bind() - UDP Style Syntax
316 * The syntax of bind() is,
317 *
318 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
319 *
320 * sd - the socket descriptor returned by socket().
321 * addr - the address structure (struct sockaddr_in or struct
322 * sockaddr_in6 [RFC 2553]),
323 * addr_len - the size of the address structure.
324 */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200325static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 int retval = 0;
328
wangweidong048ed4b2014-01-21 15:44:11 +0800329 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Daniel Borkmannbb333812013-06-28 19:49:40 +0200331 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
332 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* Disallow binding twice. */
335 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700336 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 addr_len);
338 else
339 retval = -EINVAL;
340
wangweidong048ed4b2014-01-21 15:44:11 +0800341 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 return retval;
344}
345
346static long sctp_get_port_local(struct sock *, union sctp_addr *);
347
348/* Verify this is a valid sockaddr. */
349static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
350 union sctp_addr *addr, int len)
351{
352 struct sctp_af *af;
353
354 /* Check minimum size. */
355 if (len < sizeof (struct sockaddr))
356 return NULL;
357
Xin Longc5006b82018-01-15 17:02:00 +0800358 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
359 return NULL;
360
Eric Dumazet81e98372018-04-08 07:52:08 -0700361 if (addr->sa.sa_family == AF_INET6) {
362 if (len < SIN6_LEN_RFC2133)
363 return NULL;
364 /* V4 mapped address are really of AF_INET family */
365 if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
366 !opt->pf->af_supported(AF_INET, opt))
367 return NULL;
368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /* If we get this far, af is valid. */
371 af = sctp_get_af_specific(addr->sa.sa_family);
372
373 if (len < af->sockaddr_len)
374 return NULL;
375
376 return af;
377}
378
379/* Bind a local address either to an endpoint or to an association. */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200380static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Eric W. Biederman35946982012-11-16 03:03:12 +0000382 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct sctp_sock *sp = sctp_sk(sk);
384 struct sctp_endpoint *ep = sp->ep;
385 struct sctp_bind_addr *bp = &ep->base.bind_addr;
386 struct sctp_af *af;
387 unsigned short snum;
388 int ret = 0;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* Common sockaddr verification. */
391 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700392 if (!af) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200393 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
394 __func__, sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700396 }
397
398 snum = ntohs(addr->v4.sin_port);
399
Daniel Borkmannbb333812013-06-28 19:49:40 +0200400 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
401 __func__, sk, &addr->sa, bp->port, snum, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /* PF specific bind() address verification. */
404 if (!sp->pf->bind_verify(sp, addr))
405 return -EADDRNOTAVAIL;
406
Vlad Yasevich8b358052007-05-15 17:14:58 -0400407 /* We must either be unbound, or bind to the same port.
408 * It's OK to allow 0 ports if we are already bound.
409 * We'll just inhert an already bound port in this case
410 */
411 if (bp->port) {
412 if (!snum)
413 snum = bp->port;
414 else if (snum != bp->port) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200415 pr_debug("%s: new port %d doesn't match existing port "
416 "%d\n", __func__, snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400417 return -EINVAL;
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
Krister Johansen4548b682017-01-20 17:49:11 -0800421 if (snum && snum < inet_prot_sock(net) &&
Eric W. Biederman35946982012-11-16 03:03:12 +0000422 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return -EACCES;
424
Vlad Yasevich4e540642008-07-18 23:06:32 -0700425 /* See if the address matches any of the addresses we may have
426 * already bound before checking against other endpoints.
427 */
428 if (sctp_bind_addr_match(bp, addr, sp))
429 return -EINVAL;
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* Make sure we are allowed to bind here.
432 * The function sctp_get_port_local() does duplicate address
433 * detection.
434 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900435 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if ((ret = sctp_get_port_local(sk, addr))) {
Vlad Yasevich4e540642008-07-18 23:06:32 -0700437 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
440 /* Refresh ephemeral port. */
441 if (!bp->port)
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000442 bp->port = inet_sk(sk)->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Vlad Yasevich559cf712007-09-16 16:03:28 -0700444 /* Add the address to the bind address list.
445 * Use GFP_ATOMIC since BHs will be disabled.
446 */
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300447 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
448 SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 /* Copy back into socket for getsockname() use. */
451 if (!ret) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000452 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600453 sp->pf->to_sk_saddr(addr, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 return ret;
457}
458
459 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
460 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900461 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900463 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900465 * subsequent ASCONF. Note this restriction binds each side, so at any
466 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * from each endpoint).
468 */
469static int sctp_send_asconf(struct sctp_association *asoc,
470 struct sctp_chunk *chunk)
471{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000472 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 int retval = 0;
474
475 /* If there is an outstanding ASCONF chunk, queue it for later
476 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900477 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700479 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900480 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482
483 /* Hold the chunk until an ASCONF_ACK is received. */
484 sctp_chunk_hold(chunk);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000485 retval = sctp_primitive_ASCONF(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (retval)
487 sctp_chunk_free(chunk);
488 else
489 asoc->addip_last_asconf = chunk;
490
491out:
492 return retval;
493}
494
495/* Add a list of addresses as bind addresses to local endpoint or
496 * association.
497 *
498 * Basically run through each address specified in the addrs/addrcnt
499 * array/length pair, determine if it is IPv6 or IPv4 and call
500 * sctp_do_bind() on it.
501 *
502 * If any of them fails, then the operation will be reversed and the
503 * ones that were added will be removed.
504 *
505 * Only sctp_setsockopt_bindx() is supposed to call this function.
506 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200507static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 int cnt;
510 int retval = 0;
511 void *addr_buf;
512 struct sockaddr *sa_addr;
513 struct sctp_af *af;
514
Daniel Borkmannbb333812013-06-28 19:49:40 +0200515 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
516 addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 addr_buf = addrs;
519 for (cnt = 0; cnt < addrcnt; cnt++) {
520 /* The list may contain either IPv4 or IPv6 address;
521 * determine the address length for walking thru the list.
522 */
Joe Perchesea110732011-06-13 16:21:26 +0000523 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 af = sctp_get_af_specific(sa_addr->sa_family);
525 if (!af) {
526 retval = -EINVAL;
527 goto err_bindx_add;
528 }
529
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900530 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 af->sockaddr_len);
532
533 addr_buf += af->sockaddr_len;
534
535err_bindx_add:
536 if (retval < 0) {
537 /* Failed. Cleanup the ones that have been added */
538 if (cnt > 0)
539 sctp_bindx_rem(sk, addrs, cnt);
540 return retval;
541 }
542 }
543
544 return retval;
545}
546
547/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
548 * associations that are part of the endpoint indicating that a list of local
549 * addresses are added to the endpoint.
550 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900551 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * association, we do not send the chunk for that association. But it will not
553 * affect other associations.
554 *
555 * Only sctp_setsockopt_bindx() is supposed to call this function.
556 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900557static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct sockaddr *addrs,
559 int addrcnt)
560{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000561 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct sctp_sock *sp;
563 struct sctp_endpoint *ep;
564 struct sctp_association *asoc;
565 struct sctp_bind_addr *bp;
566 struct sctp_chunk *chunk;
567 struct sctp_sockaddr_entry *laddr;
568 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700569 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 void *addr_buf;
571 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct list_head *p;
573 int i;
574 int retval = 0;
575
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000576 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return retval;
578
579 sp = sctp_sk(sk);
580 ep = sp->ep;
581
Daniel Borkmannbb333812013-06-28 19:49:40 +0200582 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
583 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700585 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (!asoc->peer.asconf_capable)
587 continue;
588
589 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
590 continue;
591
592 if (!sctp_state(asoc, ESTABLISHED))
593 continue;
594
595 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900596 * in the bind address list of the association. If so,
597 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 * other associations.
599 */
600 addr_buf = addrs;
601 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000602 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 af = sctp_get_af_specific(addr->v4.sin_family);
604 if (!af) {
605 retval = -EINVAL;
606 goto out;
607 }
608
609 if (sctp_assoc_lookup_laddr(asoc, addr))
610 break;
611
612 addr_buf += af->sockaddr_len;
613 }
614 if (i < addrcnt)
615 continue;
616
Vlad Yasevich559cf712007-09-16 16:03:28 -0700617 /* Use the first valid address in bind addr list of
618 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 bp = &asoc->base.bind_addr;
621 p = bp->address_list.next;
622 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800623 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 addrcnt, SCTP_PARAM_ADD_IP);
625 if (!chunk) {
626 retval = -ENOMEM;
627 goto out;
628 }
629
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700630 /* Add the new addresses to the bind address list with
631 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700633 addr_buf = addrs;
634 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000635 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700636 af = sctp_get_af_specific(addr->v4.sin_family);
637 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800638 retval = sctp_add_bind_addr(bp, &saveaddr,
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300639 sizeof(saveaddr),
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800640 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700641 addr_buf += af->sockaddr_len;
642 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900643 if (asoc->src_out_of_asoc_ok) {
644 struct sctp_transport *trans;
645
646 list_for_each_entry(trans,
647 &asoc->peer.transport_addr_list, transports) {
Michio Honda8a07eb02011-04-26 20:19:36 +0900648 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
649 2*asoc->pathmtu, 4380));
650 trans->ssthresh = asoc->peer.i.a_rwnd;
651 trans->rto = asoc->rto_initial;
Michele Baldessari196d6752012-12-01 04:49:42 +0000652 sctp_max_rto(asoc, trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900653 trans->rtt = trans->srtt = trans->rttvar = 0;
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300654 /* Clear the source and route cache */
Michio Honda8a07eb02011-04-26 20:19:36 +0900655 sctp_transport_route(trans, NULL,
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300656 sctp_sk(asoc->base.sk));
Michio Honda8a07eb02011-04-26 20:19:36 +0900657 }
658 }
659 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
662out:
663 return retval;
664}
665
666/* Remove a list of addresses from bind addresses list. Do not remove the
667 * last address.
668 *
669 * Basically run through each address specified in the addrs/addrcnt
670 * array/length pair, determine if it is IPv6 or IPv4 and call
671 * sctp_del_bind() on it.
672 *
673 * If any of them fails, then the operation will be reversed and the
674 * ones that were removed will be added back.
675 *
676 * At least one address has to be left; if only one address is
677 * available, the operation will return -EBUSY.
678 *
679 * Only sctp_setsockopt_bindx() is supposed to call this function.
680 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200681static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 struct sctp_sock *sp = sctp_sk(sk);
684 struct sctp_endpoint *ep = sp->ep;
685 int cnt;
686 struct sctp_bind_addr *bp = &ep->base.bind_addr;
687 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800689 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct sctp_af *af;
691
Daniel Borkmannbb333812013-06-28 19:49:40 +0200692 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
693 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 addr_buf = addrs;
696 for (cnt = 0; cnt < addrcnt; cnt++) {
697 /* If the bind address list is empty or if there is only one
698 * bind address, there is nothing more to be removed (we need
699 * at least one address here).
700 */
701 if (list_empty(&bp->address_list) ||
702 (sctp_list_single_entry(&bp->address_list))) {
703 retval = -EBUSY;
704 goto err_bindx_rem;
705 }
706
Joe Perchesea110732011-06-13 16:21:26 +0000707 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800708 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (!af) {
710 retval = -EINVAL;
711 goto err_bindx_rem;
712 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700713
714 if (!af->addr_valid(sa_addr, sp, NULL)) {
715 retval = -EADDRNOTAVAIL;
716 goto err_bindx_rem;
717 }
718
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000719 if (sa_addr->v4.sin_port &&
720 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 retval = -EINVAL;
722 goto err_bindx_rem;
723 }
724
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000725 if (!sa_addr->v4.sin_port)
726 sa_addr->v4.sin_port = htons(bp->port);
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* FIXME - There is probably a need to check if sk->sk_saddr and
729 * sk->sk_rcv_addr are currently set to one of the addresses to
730 * be removed. This is something which needs to be looked into
731 * when we are fixing the outstanding issues with multi-homing
732 * socket routing and failover schemes. Refer to comments in
733 * sctp_do_bind(). -daisy
734 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400735 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 addr_buf += af->sockaddr_len;
738err_bindx_rem:
739 if (retval < 0) {
740 /* Failed. Add the ones that has been removed back */
741 if (cnt > 0)
742 sctp_bindx_add(sk, addrs, cnt);
743 return retval;
744 }
745 }
746
747 return retval;
748}
749
750/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
751 * the associations that are part of the endpoint indicating that a list of
752 * local addresses are removed from the endpoint.
753 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900754 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 * association, we do not send the chunk for that association. But it will not
756 * affect other associations.
757 *
758 * Only sctp_setsockopt_bindx() is supposed to call this function.
759 */
760static int sctp_send_asconf_del_ip(struct sock *sk,
761 struct sockaddr *addrs,
762 int addrcnt)
763{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000764 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct sctp_sock *sp;
766 struct sctp_endpoint *ep;
767 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700768 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 struct sctp_bind_addr *bp;
770 struct sctp_chunk *chunk;
771 union sctp_addr *laddr;
772 void *addr_buf;
773 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700774 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 int i;
776 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900777 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Michio Honda8a07eb02011-04-26 20:19:36 +0900779 chunk = NULL;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000780 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return retval;
782
783 sp = sctp_sk(sk);
784 ep = sp->ep;
785
Daniel Borkmannbb333812013-06-28 19:49:40 +0200786 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
787 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700789 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (!asoc->peer.asconf_capable)
792 continue;
793
794 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
795 continue;
796
797 if (!sctp_state(asoc, ESTABLISHED))
798 continue;
799
800 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900801 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 * If so, do not send the asconf chunk to its peer, but
803 * continue with other associations.
804 */
805 addr_buf = addrs;
806 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000807 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 af = sctp_get_af_specific(laddr->v4.sin_family);
809 if (!af) {
810 retval = -EINVAL;
811 goto out;
812 }
813
814 if (!sctp_assoc_lookup_laddr(asoc, laddr))
815 break;
816
817 addr_buf += af->sockaddr_len;
818 }
819 if (i < addrcnt)
820 continue;
821
822 /* Find one address in the association's bind address list
823 * that is not in the packed array of addresses. This is to
824 * make sure that we do not delete all the addresses in the
825 * association.
826 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 bp = &asoc->base.bind_addr;
828 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
829 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900830 if ((laddr == NULL) && (addrcnt == 1)) {
831 if (asoc->asconf_addr_del_pending)
832 continue;
833 asoc->asconf_addr_del_pending =
834 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900835 if (asoc->asconf_addr_del_pending == NULL) {
836 retval = -ENOMEM;
837 goto out;
838 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900839 asoc->asconf_addr_del_pending->sa.sa_family =
840 addrs->sa_family;
841 asoc->asconf_addr_del_pending->v4.sin_port =
842 htons(bp->port);
843 if (addrs->sa_family == AF_INET) {
844 struct sockaddr_in *sin;
845
846 sin = (struct sockaddr_in *)addrs;
847 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
848 } else if (addrs->sa_family == AF_INET6) {
849 struct sockaddr_in6 *sin6;
850
851 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000852 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900853 }
Daniel Borkmannbb333812013-06-28 19:49:40 +0200854
855 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
856 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
857 asoc->asconf_addr_del_pending);
858
Michio Honda8a07eb02011-04-26 20:19:36 +0900859 asoc->src_out_of_asoc_ok = 1;
860 stored = 1;
861 goto skip_mkasconf;
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Daniel Borkmann88362ad2013-09-07 20:51:21 +0200864 if (laddr == NULL)
865 return -EINVAL;
866
Vlad Yasevich559cf712007-09-16 16:03:28 -0700867 /* We do not need RCU protection throughout this loop
868 * because this is done under a socket lock from the
869 * setsockopt call.
870 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
872 SCTP_PARAM_DEL_IP);
873 if (!chunk) {
874 retval = -ENOMEM;
875 goto out;
876 }
877
Michio Honda8a07eb02011-04-26 20:19:36 +0900878skip_mkasconf:
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700879 /* Reset use_as_src flag for the addresses in the bind address
880 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700882 addr_buf = addrs;
883 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000884 laddr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700885 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700886 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a12006-11-20 17:05:23 -0800887 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800888 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700889 }
890 addr_buf += af->sockaddr_len;
891 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700892
893 /* Update the route and saddr entries for all the transports
894 * as some of the addresses in the bind address list are
895 * about to be deleted and cannot be used as source addresses.
896 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700897 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
898 transports) {
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700899 sctp_transport_route(transport, NULL,
900 sctp_sk(asoc->base.sk));
901 }
902
Michio Honda8a07eb02011-04-26 20:19:36 +0900903 if (stored)
904 /* We don't need to transmit ASCONF */
905 continue;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700906 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908out:
909 return retval;
910}
911
Michio Honda9f7d6532011-04-26 19:32:51 +0900912/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
913int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
914{
915 struct sock *sk = sctp_opt2sk(sp);
916 union sctp_addr *addr;
917 struct sctp_af *af;
918
919 /* It is safe to write port space in caller. */
920 addr = &addrw->a;
921 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
922 af = sctp_get_af_specific(addr->sa.sa_family);
923 if (!af)
924 return -EINVAL;
925 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
926 return -EINVAL;
927
928 if (addrw->state == SCTP_ADDR_NEW)
929 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
930 else
931 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
935 *
936 * API 8.1
937 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
938 * int flags);
939 *
940 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
941 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
942 * or IPv6 addresses.
943 *
944 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
945 * Section 3.1.2 for this usage.
946 *
947 * addrs is a pointer to an array of one or more socket addresses. Each
948 * address is contained in its appropriate structure (i.e. struct
949 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700950 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 * representation is termed a "packed array" of addresses). The caller
952 * specifies the number of addresses in the array with addrcnt.
953 *
954 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
955 * -1, and sets errno to the appropriate error code.
956 *
957 * For SCTP, the port given in each socket address must be the same, or
958 * sctp_bindx() will fail, setting errno to EINVAL.
959 *
960 * The flags parameter is formed from the bitwise OR of zero or more of
961 * the following currently defined flags:
962 *
963 * SCTP_BINDX_ADD_ADDR
964 *
965 * SCTP_BINDX_REM_ADDR
966 *
967 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
968 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
969 * addresses from the association. The two flags are mutually exclusive;
970 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
971 * not remove all addresses from an association; sctp_bindx() will
972 * reject such an attempt with EINVAL.
973 *
974 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
975 * additional addresses with an endpoint after calling bind(). Or use
976 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
977 * socket is associated with so that no new association accepted will be
978 * associated with those addresses. If the endpoint supports dynamic
979 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
980 * endpoint to send the appropriate message to the peer to change the
981 * peers address lists.
982 *
983 * Adding and removing addresses from a connected association is
984 * optional functionality. Implementations that do not support this
985 * functionality should return EOPNOTSUPP.
986 *
987 * Basically do nothing but copying the addresses from user to kernel
988 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700989 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
990 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
993 * it.
994 *
995 * sk The sk of the socket
996 * addrs The pointer to the addresses in user land
997 * addrssize Size of the addrs buffer
998 * op Operation to perform (add or remove, see the flags of
999 * sctp_bindx)
1000 *
1001 * Returns 0 if ok, <0 errno code on error.
1002 */
wangweidong26ac8e52013-12-23 12:16:51 +08001003static int sctp_setsockopt_bindx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001004 struct sockaddr __user *addrs,
1005 int addrs_size, int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 struct sockaddr *kaddrs;
1008 int err;
1009 int addrcnt = 0;
1010 int walk_size = 0;
1011 struct sockaddr *sa_addr;
1012 void *addr_buf;
1013 struct sctp_af *af;
1014
Daniel Borkmannbb333812013-06-28 19:49:40 +02001015 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1016 __func__, sk, addrs, addrs_size, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 if (unlikely(addrs_size <= 0))
1019 return -EINVAL;
1020
Al Viroc981f252018-01-07 13:19:09 -05001021 kaddrs = vmemdup_user(addrs, addrs_size);
1022 if (unlikely(IS_ERR(kaddrs)))
1023 return PTR_ERR(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001025 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 addr_buf = kaddrs;
1027 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001028 if (walk_size + sizeof(sa_family_t) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001029 kvfree(kaddrs);
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001030 return -EINVAL;
1031 }
1032
Joe Perchesea110732011-06-13 16:21:26 +00001033 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 af = sctp_get_af_specific(sa_addr->sa_family);
1035
1036 /* If the address family is not supported or if this address
1037 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001038 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001040 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return -EINVAL;
1042 }
1043 addrcnt++;
1044 addr_buf += af->sockaddr_len;
1045 walk_size += af->sockaddr_len;
1046 }
1047
1048 /* Do the work. */
1049 switch (op) {
1050 case SCTP_BINDX_ADD_ADDR:
Richard Haines2277c7c2018-02-13 20:56:24 +00001051 /* Allow security module to validate bindx addresses. */
1052 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1053 (struct sockaddr *)kaddrs,
1054 addrs_size);
1055 if (err)
1056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1058 if (err)
1059 goto out;
1060 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1061 break;
1062
1063 case SCTP_BINDX_REM_ADDR:
1064 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1065 if (err)
1066 goto out;
1067 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1068 break;
1069
1070 default:
1071 err = -EINVAL;
1072 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075out:
Al Viroc981f252018-01-07 13:19:09 -05001076 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 return err;
1079}
1080
Frank Filz3f7a87d2005-06-20 13:14:57 -07001081/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1082 *
1083 * Common routine for handling connect() and sctp_connectx().
1084 * Connect will come in with just a single address.
1085 */
wangweidong26ac8e52013-12-23 12:16:51 +08001086static int __sctp_connect(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001087 struct sockaddr *kaddrs,
Xin Long644fbde2018-05-20 16:39:10 +08001088 int addrs_size, int flags,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001089 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001090{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001091 struct net *net = sock_net(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001092 struct sctp_sock *sp;
1093 struct sctp_endpoint *ep;
1094 struct sctp_association *asoc = NULL;
1095 struct sctp_association *asoc2;
1096 struct sctp_transport *transport;
1097 union sctp_addr to;
Xin Long1c662012017-08-05 19:59:54 +08001098 enum sctp_scope scope;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001099 long timeo;
1100 int err = 0;
1101 int addrcnt = 0;
1102 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001103 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001104 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001105 unsigned short port;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001106
1107 sp = sctp_sk(sk);
1108 ep = sp->ep;
1109
1110 /* connect() cannot be done on a socket that is already in ESTABLISHED
1111 * state - UDP-style peeled off socket or a TCP-style socket that
1112 * is already connected.
1113 * It cannot be done even on a TCP-style listening socket.
1114 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03001115 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
Frank Filz3f7a87d2005-06-20 13:14:57 -07001116 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1117 err = -EISCONN;
1118 goto out_free;
1119 }
1120
1121 /* Walk through the addrs buffer and count the number of addresses. */
1122 addr_buf = kaddrs;
1123 while (walk_size < addrs_size) {
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001124 struct sctp_af *af;
1125
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001126 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1127 err = -EINVAL;
1128 goto out_free;
1129 }
1130
Joe Perchesea110732011-06-13 16:21:26 +00001131 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001132 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001133
1134 /* If the address family is not supported or if this address
1135 * causes the address buffer to overflow return EINVAL.
1136 */
1137 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1138 err = -EINVAL;
1139 goto out_free;
1140 }
1141
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001142 port = ntohs(sa_addr->v4.sin_port);
1143
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001144 /* Save current address so we can work with it */
1145 memcpy(&to, sa_addr, af->sockaddr_len);
1146
1147 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001148 if (err)
1149 goto out_free;
1150
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001151 /* Make sure the destination port is correctly set
1152 * in all addresses.
1153 */
Wei Yongjun524fba62013-04-03 03:02:28 +00001154 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1155 err = -EINVAL;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001156 goto out_free;
Wei Yongjun524fba62013-04-03 03:02:28 +00001157 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001158
1159 /* Check if there already is a matching association on the
1160 * endpoint (other than the one created here).
1161 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001162 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001163 if (asoc2 && asoc2 != asoc) {
1164 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1165 err = -EISCONN;
1166 else
1167 err = -EALREADY;
1168 goto out_free;
1169 }
1170
1171 /* If we could not find a matching association on the endpoint,
1172 * make sure that there is no peeled-off association matching
1173 * the peer address even on another socket.
1174 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001175 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001176 err = -EADDRNOTAVAIL;
1177 goto out_free;
1178 }
1179
1180 if (!asoc) {
1181 /* If a bind() or sctp_bindx() is not called prior to
1182 * an sctp_connectx() call, the system picks an
1183 * ephemeral port and will choose an address set
1184 * equivalent to binding with a wildcard address.
1185 */
1186 if (!ep->base.bind_addr.port) {
1187 if (sctp_autobind(sk)) {
1188 err = -EAGAIN;
1189 goto out_free;
1190 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001191 } else {
1192 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001193 * If an unprivileged user inherits a 1-many
1194 * style socket with open associations on a
1195 * privileged port, it MAY be permitted to
1196 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001197 * be permitted to open new associations.
1198 */
Krister Johansen4548b682017-01-20 17:49:11 -08001199 if (ep->base.bind_addr.port <
1200 inet_prot_sock(net) &&
1201 !ns_capable(net->user_ns,
1202 CAP_NET_BIND_SERVICE)) {
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001203 err = -EACCES;
1204 goto out_free;
1205 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001206 }
1207
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001208 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001209 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1210 if (!asoc) {
1211 err = -ENOMEM;
1212 goto out_free;
1213 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001214
1215 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1216 GFP_KERNEL);
1217 if (err < 0) {
1218 goto out_free;
1219 }
1220
Frank Filz3f7a87d2005-06-20 13:14:57 -07001221 }
1222
1223 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001224 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001225 SCTP_UNKNOWN);
1226 if (!transport) {
1227 err = -ENOMEM;
1228 goto out_free;
1229 }
1230
1231 addrcnt++;
1232 addr_buf += af->sockaddr_len;
1233 walk_size += af->sockaddr_len;
1234 }
1235
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001236 /* In case the user of sctp_connectx() wants an association
1237 * id back, assign one now.
1238 */
1239 if (assoc_id) {
1240 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1241 if (err < 0)
1242 goto out_free;
1243 }
1244
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001245 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001246 if (err < 0) {
1247 goto out_free;
1248 }
1249
1250 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001251 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001252 sp->pf->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001253 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001254
Xin Long644fbde2018-05-20 16:39:10 +08001255 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001256
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001257 if (assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001258 *assoc_id = asoc->assoc_id;
Richard Haines2277c7c2018-02-13 20:56:24 +00001259
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001260 err = sctp_wait_for_connect(asoc, &timeo);
1261 /* Note: the asoc may be freed after the return of
1262 * sctp_wait_for_connect.
1263 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001264
1265 /* Don't free association on exit. */
1266 asoc = NULL;
1267
1268out_free:
Daniel Borkmannbb333812013-06-28 19:49:40 +02001269 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1270 __func__, asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001271
Neil Horman2eebc1e2012-07-16 09:13:51 +00001272 if (asoc) {
1273 /* sctp_primitive_ASSOCIATE may have added this association
1274 * To the hash table, try to unhash it, just in case, its a noop
1275 * if it wasn't hashed so we're safe
1276 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001277 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001278 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001279 return err;
1280}
1281
1282/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1283 *
1284 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001285 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1286 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001287 *
1288 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1289 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1290 * or IPv6 addresses.
1291 *
1292 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1293 * Section 3.1.2 for this usage.
1294 *
1295 * addrs is a pointer to an array of one or more socket addresses. Each
1296 * address is contained in its appropriate structure (i.e. struct
1297 * sockaddr_in or struct sockaddr_in6) the family of the address type
1298 * must be used to distengish the address length (note that this
1299 * representation is termed a "packed array" of addresses). The caller
1300 * specifies the number of addresses in the array with addrcnt.
1301 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001302 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1303 * the association id of the new association. On failure, sctp_connectx()
1304 * returns -1, and sets errno to the appropriate error code. The assoc_id
1305 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001306 *
1307 * For SCTP, the port given in each socket address must be the same, or
1308 * sctp_connectx() will fail, setting errno to EINVAL.
1309 *
1310 * An application can use sctp_connectx to initiate an association with
1311 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1312 * allows a caller to specify multiple addresses at which a peer can be
1313 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001314 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001315 * specifies that the stack will try to make use of all the addresses in
1316 * the list when needed.
1317 *
1318 * Note that the list of addresses passed in is only used for setting up
1319 * the association. It does not necessarily equal the set of addresses
1320 * the peer uses for the resulting association. If the caller wants to
1321 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1322 * retrieve them after the association has been set up.
1323 *
1324 * Basically do nothing but copying the addresses from user to kernel
1325 * land and invoking either sctp_connectx(). This is used for tunneling
1326 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1327 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07001328 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1329 * it.
1330 *
1331 * sk The sk of the socket
1332 * addrs The pointer to the addresses in user land
1333 * addrssize Size of the addrs buffer
1334 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001335 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001336 */
wangweidong26ac8e52013-12-23 12:16:51 +08001337static int __sctp_setsockopt_connectx(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001338 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001339 int addrs_size,
1340 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001341{
Frank Filz3f7a87d2005-06-20 13:14:57 -07001342 struct sockaddr *kaddrs;
Xin Long644fbde2018-05-20 16:39:10 +08001343 int err = 0, flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001344
Daniel Borkmannbb333812013-06-28 19:49:40 +02001345 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1346 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001347
1348 if (unlikely(addrs_size <= 0))
1349 return -EINVAL;
1350
Al Viroc981f252018-01-07 13:19:09 -05001351 kaddrs = vmemdup_user(addrs, addrs_size);
1352 if (unlikely(IS_ERR(kaddrs)))
1353 return PTR_ERR(kaddrs);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001354
Richard Haines2277c7c2018-02-13 20:56:24 +00001355 /* Allow security module to validate connectx addresses. */
1356 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1357 (struct sockaddr *)kaddrs,
1358 addrs_size);
1359 if (err)
1360 goto out_free;
1361
Xin Long644fbde2018-05-20 16:39:10 +08001362 /* in-kernel sockets don't generally have a file allocated to them
1363 * if all they do is call sock_create_kern().
1364 */
1365 if (sk->sk_socket->file)
1366 flags = sk->sk_socket->file->f_flags;
1367
1368 err = __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
Richard Haines2277c7c2018-02-13 20:56:24 +00001369
1370out_free:
Al Viroc981f252018-01-07 13:19:09 -05001371 kvfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001372
Frank Filz3f7a87d2005-06-20 13:14:57 -07001373 return err;
1374}
1375
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001376/*
1377 * This is an older interface. It's kept for backward compatibility
1378 * to the option that doesn't provide association id.
1379 */
wangweidong26ac8e52013-12-23 12:16:51 +08001380static int sctp_setsockopt_connectx_old(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001381 struct sockaddr __user *addrs,
1382 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001383{
1384 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1385}
1386
1387/*
1388 * New interface for the API. The since the API is done with a socket
1389 * option, to make it simple we feed back the association id is as a return
1390 * indication to the call. Error is always negative and association id is
1391 * always positive.
1392 */
wangweidong26ac8e52013-12-23 12:16:51 +08001393static int sctp_setsockopt_connectx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001394 struct sockaddr __user *addrs,
1395 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001396{
1397 sctp_assoc_t assoc_id = 0;
1398 int err = 0;
1399
1400 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1401
1402 if (err)
1403 return err;
1404 else
1405 return assoc_id;
1406}
1407
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001408/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001409 * New (hopefully final) interface for the API.
1410 * We use the sctp_getaddrs_old structure so that use-space library
Daniel Borkmannffd59392014-02-17 12:11:11 +01001411 * can avoid any unnecessary allocations. The only different part
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001412 * is that we store the actual length of the address buffer into the
Daniel Borkmannffd59392014-02-17 12:11:11 +01001413 * addrs_num structure member. That way we can re-use the existing
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001414 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001415 */
Daniel Borkmannffd59392014-02-17 12:11:11 +01001416#ifdef CONFIG_COMPAT
1417struct compat_sctp_getaddrs_old {
1418 sctp_assoc_t assoc_id;
1419 s32 addr_num;
1420 compat_uptr_t addrs; /* struct sockaddr * */
1421};
1422#endif
1423
wangweidong26ac8e52013-12-23 12:16:51 +08001424static int sctp_getsockopt_connectx3(struct sock *sk, int len,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001425 char __user *optval,
1426 int __user *optlen)
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001427{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001428 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001429 sctp_assoc_t assoc_id = 0;
1430 int err = 0;
1431
Daniel Borkmannffd59392014-02-17 12:11:11 +01001432#ifdef CONFIG_COMPAT
Andy Lutomirski96c0e0a2016-03-22 14:25:07 -07001433 if (in_compat_syscall()) {
Daniel Borkmannffd59392014-02-17 12:11:11 +01001434 struct compat_sctp_getaddrs_old param32;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001435
Daniel Borkmannffd59392014-02-17 12:11:11 +01001436 if (len < sizeof(param32))
1437 return -EINVAL;
1438 if (copy_from_user(&param32, optval, sizeof(param32)))
1439 return -EFAULT;
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001440
Daniel Borkmannffd59392014-02-17 12:11:11 +01001441 param.assoc_id = param32.assoc_id;
1442 param.addr_num = param32.addr_num;
1443 param.addrs = compat_ptr(param32.addrs);
1444 } else
1445#endif
1446 {
1447 if (len < sizeof(param))
1448 return -EINVAL;
1449 if (copy_from_user(&param, optval, sizeof(param)))
1450 return -EFAULT;
1451 }
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001452
Daniel Borkmannffd59392014-02-17 12:11:11 +01001453 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1454 param.addrs, param.addr_num,
1455 &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001456 if (err == 0 || err == -EINPROGRESS) {
1457 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1458 return -EFAULT;
1459 if (put_user(sizeof(assoc_id), optlen))
1460 return -EFAULT;
1461 }
1462
1463 return err;
1464}
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466/* API 3.1.4 close() - UDP Style Syntax
1467 * Applications use close() to perform graceful shutdown (as described in
1468 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1469 * by a UDP-style socket.
1470 *
1471 * The syntax is
1472 *
1473 * ret = close(int sd);
1474 *
1475 * sd - the socket descriptor of the associations to be closed.
1476 *
1477 * To gracefully shutdown a specific association represented by the
1478 * UDP-style socket, an application should use the sendmsg() call,
1479 * passing no user data, but including the appropriate flag in the
1480 * ancillary data (see Section xxxx).
1481 *
1482 * If sd in the close() call is a branched-off socket representing only
1483 * one association, the shutdown is performed on that association only.
1484 *
1485 * 4.1.6 close() - TCP Style Syntax
1486 *
1487 * Applications use close() to gracefully close down an association.
1488 *
1489 * The syntax is:
1490 *
1491 * int close(int sd);
1492 *
1493 * sd - the socket descriptor of the association to be closed.
1494 *
1495 * After an application calls close() on a socket descriptor, no further
1496 * socket operations will succeed on that descriptor.
1497 *
1498 * API 7.1.4 SO_LINGER
1499 *
1500 * An application using the TCP-style socket can use this option to
1501 * perform the SCTP ABORT primitive. The linger option structure is:
1502 *
1503 * struct linger {
1504 * int l_onoff; // option on/off
1505 * int l_linger; // linger time
1506 * };
1507 *
1508 * To enable the option, set l_onoff to 1. If the l_linger value is set
1509 * to 0, calling close() is the same as the ABORT primitive. If the
1510 * value is set to a negative value, the setsockopt() call will return
1511 * an error. If the value is set to a positive value linger_time, the
1512 * close() can be blocked for at most linger_time ms. If the graceful
1513 * shutdown phase does not finish during this period, close() will
1514 * return but the graceful shutdown phase continues in the system.
1515 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02001516static void sctp_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001518 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct sctp_endpoint *ep;
1520 struct sctp_association *asoc;
1521 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001522 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Daniel Borkmannbb333812013-06-28 19:49:40 +02001524 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Xin Long6dfe4b92017-06-10 14:56:56 +08001526 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 sk->sk_shutdown = SHUTDOWN_MASK;
Yafang Shaocbabf462017-12-20 11:12:54 +08001528 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 ep = sctp_sk(sk)->ep;
1531
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001532 /* Clean up any skbs sitting on the receive queue. */
1533 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1534 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1535
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001536 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 list_for_each_safe(pos, temp, &ep->asocs) {
1538 asoc = list_entry(pos, struct sctp_association, asocs);
1539
1540 if (sctp_style(sk, TCP)) {
1541 /* A closed association can still be in the list if
1542 * it belongs to a TCP-style listening socket that is
1543 * not yet accepted. If so, free it. If not, send an
1544 * ABORT or SHUTDOWN based on the linger options.
1545 */
1546 if (sctp_state(asoc, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001548 continue;
1549 }
1550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001552 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1553 !skb_queue_empty(&asoc->ulpq.reasm) ||
Xin Long13228232017-12-08 21:04:09 +08001554 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001555 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001556 struct sctp_chunk *chunk;
1557
1558 chunk = sctp_make_abort_user(asoc, NULL, 0);
Xin Long068d8bd2015-12-29 17:49:25 +08001559 sctp_primitive_ABORT(net, asoc, chunk);
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001560 } else
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001561 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 /* On a TCP-style socket, block for at most linger_time if set. */
1565 if (sctp_style(sk, TCP) && timeout)
1566 sctp_wait_for_close(sk, timeout);
1567
1568 /* This will run the backlog queue. */
wangweidong048ed4b2014-01-21 15:44:11 +08001569 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 /* Supposedly, no process has access to the socket, but
1572 * the net layers still may.
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001573 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1574 * held and that should be grabbed before socket lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001576 spin_lock_bh(&net->sctp.addr_wq_lock);
Xin Long6dfe4b92017-06-10 14:56:56 +08001577 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 /* Hold the sock, since sk_common_release() will put sock_put()
1580 * and we have just a little more cleanup.
1581 */
1582 sock_hold(sk);
1583 sk_common_release(sk);
1584
wangweidong5bc1d1b2014-01-21 15:44:12 +08001585 bh_unlock_sock(sk);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001586 spin_unlock_bh(&net->sctp.addr_wq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 sock_put(sk);
1589
1590 SCTP_DBG_OBJCNT_DEC(sock);
1591}
1592
1593/* Handle EPIPE error. */
1594static int sctp_error(struct sock *sk, int flags, int err)
1595{
1596 if (err == -EPIPE)
1597 err = sock_error(sk) ? : -EPIPE;
1598 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1599 send_sig(SIGPIPE, current, 0);
1600 return err;
1601}
1602
1603/* API 3.1.3 sendmsg() - UDP Style Syntax
1604 *
1605 * An application uses sendmsg() and recvmsg() calls to transmit data to
1606 * and receive data from its peer.
1607 *
1608 * ssize_t sendmsg(int socket, const struct msghdr *message,
1609 * int flags);
1610 *
1611 * socket - the socket descriptor of the endpoint.
1612 * message - pointer to the msghdr structure which contains a single
1613 * user message and possibly some ancillary data.
1614 *
1615 * See Section 5 for complete description of the data
1616 * structures.
1617 *
1618 * flags - flags sent or received with the user message, see Section
1619 * 5 for complete description of the flags.
1620 *
1621 * Note: This function could use a rewrite especially when explicit
1622 * connect support comes in.
1623 */
1624/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1625
Xin Longa05437a2017-08-11 10:23:48 +08001626static int sctp_msghdr_parse(const struct msghdr *msg,
1627 struct sctp_cmsgs *cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Xin Long204f8172018-03-01 23:05:14 +08001629static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1630 struct sctp_sndrcvinfo *srinfo,
1631 const struct msghdr *msg, size_t msg_len)
1632{
1633 __u16 sflags;
1634 int err;
1635
1636 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1637 return -EPIPE;
1638
1639 if (msg_len > sk->sk_sndbuf)
1640 return -EMSGSIZE;
1641
1642 memset(cmsgs, 0, sizeof(*cmsgs));
1643 err = sctp_msghdr_parse(msg, cmsgs);
1644 if (err) {
1645 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1646 return err;
1647 }
1648
1649 memset(srinfo, 0, sizeof(*srinfo));
1650 if (cmsgs->srinfo) {
1651 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1652 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1653 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1654 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1655 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1656 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1657 }
1658
1659 if (cmsgs->sinfo) {
1660 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1661 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1662 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1663 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1664 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1665 }
1666
Xin Longed63afb2018-03-05 20:44:18 +08001667 if (cmsgs->prinfo) {
1668 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1669 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1670 cmsgs->prinfo->pr_policy);
1671 }
1672
Xin Long204f8172018-03-01 23:05:14 +08001673 sflags = srinfo->sinfo_flags;
1674 if (!sflags && msg_len)
1675 return 0;
1676
1677 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1678 return -EINVAL;
1679
1680 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1681 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1682 return -EINVAL;
1683
1684 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1685 return -EINVAL;
1686
1687 return 0;
1688}
1689
Xin Long2bfd80f2018-03-01 23:05:11 +08001690static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1691 struct sctp_cmsgs *cmsgs,
1692 union sctp_addr *daddr,
1693 struct sctp_transport **tp)
1694{
1695 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1696 struct net *net = sock_net(sk);
1697 struct sctp_association *asoc;
1698 enum sctp_scope scope;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001699 struct cmsghdr *cmsg;
Xin Long4be41392018-07-02 18:21:14 +08001700 __be32 flowinfo = 0;
Linus Torvalds9eda2d22018-04-06 15:39:26 -07001701 struct sctp_af *af;
Wei Yongjund98985d2018-03-13 03:03:30 +00001702 int err;
Xin Long2bfd80f2018-03-01 23:05:11 +08001703
1704 *tp = NULL;
1705
1706 if (sflags & (SCTP_EOF | SCTP_ABORT))
1707 return -EINVAL;
1708
1709 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1710 sctp_sstate(sk, CLOSING)))
1711 return -EADDRNOTAVAIL;
1712
1713 if (sctp_endpoint_is_peeled_off(ep, daddr))
1714 return -EADDRNOTAVAIL;
1715
1716 if (!ep->base.bind_addr.port) {
1717 if (sctp_autobind(sk))
1718 return -EAGAIN;
1719 } else {
1720 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1721 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1722 return -EACCES;
1723 }
1724
1725 scope = sctp_scope(daddr);
1726
Linus Torvalds9eda2d22018-04-06 15:39:26 -07001727 /* Label connection socket for first association 1-to-many
1728 * style for client sequence socket()->sendmsg(). This
1729 * needs to be done before sctp_assoc_add_peer() as that will
1730 * set up the initial packet that needs to account for any
1731 * security ip options (CIPSO/CALIPSO) added to the packet.
1732 */
1733 af = sctp_get_af_specific(daddr->sa.sa_family);
1734 if (!af)
1735 return -EINVAL;
1736 err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1737 (struct sockaddr *)daddr,
1738 af->sockaddr_len);
1739 if (err < 0)
1740 return err;
1741
Xin Long2bfd80f2018-03-01 23:05:11 +08001742 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1743 if (!asoc)
1744 return -ENOMEM;
1745
1746 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1747 err = -ENOMEM;
1748 goto free;
1749 }
1750
1751 if (cmsgs->init) {
1752 struct sctp_initmsg *init = cmsgs->init;
1753
1754 if (init->sinit_num_ostreams) {
1755 __u16 outcnt = init->sinit_num_ostreams;
1756
1757 asoc->c.sinit_num_ostreams = outcnt;
1758 /* outcnt has been changed, need to re-init stream */
1759 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1760 GFP_KERNEL);
1761 if (err)
1762 goto free;
1763 }
1764
1765 if (init->sinit_max_instreams)
1766 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1767
1768 if (init->sinit_max_attempts)
1769 asoc->max_init_attempts = init->sinit_max_attempts;
1770
1771 if (init->sinit_max_init_timeo)
1772 asoc->max_init_timeo =
1773 msecs_to_jiffies(init->sinit_max_init_timeo);
1774 }
1775
1776 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1777 if (!*tp) {
1778 err = -ENOMEM;
1779 goto free;
1780 }
1781
Xin Long2c0dbaa2018-03-05 20:44:19 +08001782 if (!cmsgs->addrs_msg)
1783 return 0;
1784
Xin Long4be41392018-07-02 18:21:14 +08001785 if (daddr->sa.sa_family == AF_INET6)
1786 flowinfo = daddr->v6.sin6_flowinfo;
1787
Xin Long2c0dbaa2018-03-05 20:44:19 +08001788 /* sendv addr list parse */
1789 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1790 struct sctp_transport *transport;
1791 struct sctp_association *old;
1792 union sctp_addr _daddr;
1793 int dlen;
1794
1795 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1796 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1797 cmsg->cmsg_type != SCTP_DSTADDRV6))
1798 continue;
1799
1800 daddr = &_daddr;
1801 memset(daddr, 0, sizeof(*daddr));
1802 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1803 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
Wei Yongjund98985d2018-03-13 03:03:30 +00001804 if (dlen < sizeof(struct in_addr)) {
1805 err = -EINVAL;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001806 goto free;
Wei Yongjund98985d2018-03-13 03:03:30 +00001807 }
Xin Long2c0dbaa2018-03-05 20:44:19 +08001808
1809 dlen = sizeof(struct in_addr);
1810 daddr->v4.sin_family = AF_INET;
1811 daddr->v4.sin_port = htons(asoc->peer.port);
1812 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1813 } else {
Wei Yongjund98985d2018-03-13 03:03:30 +00001814 if (dlen < sizeof(struct in6_addr)) {
1815 err = -EINVAL;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001816 goto free;
Wei Yongjund98985d2018-03-13 03:03:30 +00001817 }
Xin Long2c0dbaa2018-03-05 20:44:19 +08001818
1819 dlen = sizeof(struct in6_addr);
Xin Long4be41392018-07-02 18:21:14 +08001820 daddr->v6.sin6_flowinfo = flowinfo;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001821 daddr->v6.sin6_family = AF_INET6;
1822 daddr->v6.sin6_port = htons(asoc->peer.port);
1823 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1824 }
1825 err = sctp_verify_addr(sk, daddr, sizeof(*daddr));
1826 if (err)
1827 goto free;
1828
1829 old = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1830 if (old && old != asoc) {
1831 if (old->state >= SCTP_STATE_ESTABLISHED)
1832 err = -EISCONN;
1833 else
1834 err = -EALREADY;
1835 goto free;
1836 }
1837
1838 if (sctp_endpoint_is_peeled_off(ep, daddr)) {
1839 err = -EADDRNOTAVAIL;
1840 goto free;
1841 }
1842
1843 transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL,
1844 SCTP_UNKNOWN);
1845 if (!transport) {
1846 err = -ENOMEM;
1847 goto free;
1848 }
1849 }
1850
Xin Long2bfd80f2018-03-01 23:05:11 +08001851 return 0;
1852
1853free:
1854 sctp_association_free(asoc);
1855 return err;
1856}
1857
Xin Longc2666de2018-03-01 23:05:12 +08001858static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1859 __u16 sflags, struct msghdr *msg,
1860 size_t msg_len)
1861{
1862 struct sock *sk = asoc->base.sk;
1863 struct net *net = sock_net(sk);
1864
1865 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1866 return -EPIPE;
1867
Xin Long49102802018-03-05 20:44:20 +08001868 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1869 !sctp_state(asoc, ESTABLISHED))
1870 return 0;
1871
Xin Longc2666de2018-03-01 23:05:12 +08001872 if (sflags & SCTP_EOF) {
1873 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1874 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1875
1876 return 0;
1877 }
1878
1879 if (sflags & SCTP_ABORT) {
1880 struct sctp_chunk *chunk;
1881
1882 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1883 if (!chunk)
1884 return -ENOMEM;
1885
1886 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1887 sctp_primitive_ABORT(net, asoc, chunk);
1888
1889 return 0;
1890 }
1891
1892 return 1;
1893}
1894
Xin Longf84af332018-03-01 23:05:10 +08001895static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1896 struct msghdr *msg, size_t msg_len,
1897 struct sctp_transport *transport,
1898 struct sctp_sndrcvinfo *sinfo)
1899{
1900 struct sock *sk = asoc->base.sk;
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001901 struct sctp_sock *sp = sctp_sk(sk);
Xin Longf84af332018-03-01 23:05:10 +08001902 struct net *net = sock_net(sk);
1903 struct sctp_datamsg *datamsg;
1904 bool wait_connect = false;
1905 struct sctp_chunk *chunk;
1906 long timeo;
1907 int err;
1908
1909 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1910 err = -EINVAL;
1911 goto err;
1912 }
1913
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001914 if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
Xin Longf84af332018-03-01 23:05:10 +08001915 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1916 if (err)
1917 goto err;
1918 }
1919
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001920 if (sp->disable_fragments && msg_len > asoc->frag_point) {
Xin Longf84af332018-03-01 23:05:10 +08001921 err = -EMSGSIZE;
1922 goto err;
1923 }
1924
Marcelo Ricardo Leitner25216802018-04-26 16:58:56 -03001925 if (asoc->pmtu_pending) {
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001926 if (sp->param_flags & SPP_PMTUD_ENABLE)
1927 sctp_assoc_sync_pmtu(asoc);
Marcelo Ricardo Leitner25216802018-04-26 16:58:56 -03001928 asoc->pmtu_pending = 0;
1929 }
Neil Horman0aee4c22018-03-12 14:15:25 -04001930
1931 if (sctp_wspace(asoc) < msg_len)
1932 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1933
1934 if (!sctp_wspace(asoc)) {
1935 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1936 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1937 if (err)
1938 goto err;
1939 }
1940
Xin Longf84af332018-03-01 23:05:10 +08001941 if (sctp_state(asoc, CLOSED)) {
1942 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1943 if (err)
1944 goto err;
1945
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001946 if (sp->strm_interleave) {
Xin Longf84af332018-03-01 23:05:10 +08001947 timeo = sock_sndtimeo(sk, 0);
1948 err = sctp_wait_for_connect(asoc, &timeo);
1949 if (err)
1950 goto err;
1951 } else {
1952 wait_connect = true;
1953 }
1954
1955 pr_debug("%s: we associated primitively\n", __func__);
1956 }
1957
Xin Longf84af332018-03-01 23:05:10 +08001958 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1959 if (IS_ERR(datamsg)) {
1960 err = PTR_ERR(datamsg);
1961 goto err;
1962 }
1963
1964 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1965
1966 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1967 sctp_chunk_hold(chunk);
1968 sctp_set_owner_w(chunk);
1969 chunk->transport = transport;
1970 }
1971
1972 err = sctp_primitive_SEND(net, asoc, datamsg);
1973 if (err) {
1974 sctp_datamsg_free(datamsg);
1975 goto err;
1976 }
1977
1978 pr_debug("%s: we sent primitively\n", __func__);
1979
1980 sctp_datamsg_put(datamsg);
1981
1982 if (unlikely(wait_connect)) {
1983 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1984 sctp_wait_for_connect(asoc, &timeo);
1985 }
1986
1987 err = msg_len;
1988
1989err:
1990 return err;
1991}
1992
Xin Longbecef9b2018-03-01 23:05:13 +08001993static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1994 const struct msghdr *msg,
1995 struct sctp_cmsgs *cmsgs)
1996{
1997 union sctp_addr *daddr = NULL;
1998 int err;
1999
2000 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
2001 int len = msg->msg_namelen;
2002
2003 if (len > sizeof(*daddr))
2004 len = sizeof(*daddr);
2005
2006 daddr = (union sctp_addr *)msg->msg_name;
2007
2008 err = sctp_verify_addr(sk, daddr, len);
2009 if (err)
2010 return ERR_PTR(err);
2011 }
2012
2013 return daddr;
2014}
2015
Xin Longd42cb062018-03-01 23:05:15 +08002016static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
2017 struct sctp_sndrcvinfo *sinfo,
2018 struct sctp_cmsgs *cmsgs)
2019{
2020 if (!cmsgs->srinfo && !cmsgs->sinfo) {
2021 sinfo->sinfo_stream = asoc->default_stream;
2022 sinfo->sinfo_ppid = asoc->default_ppid;
2023 sinfo->sinfo_context = asoc->default_context;
2024 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
Xin Longed63afb2018-03-05 20:44:18 +08002025
2026 if (!cmsgs->prinfo)
2027 sinfo->sinfo_flags = asoc->default_flags;
Xin Longd42cb062018-03-01 23:05:15 +08002028 }
2029
Xin Longed63afb2018-03-05 20:44:18 +08002030 if (!cmsgs->srinfo && !cmsgs->prinfo)
Xin Longd42cb062018-03-01 23:05:15 +08002031 sinfo->sinfo_timetolive = asoc->default_timetolive;
Xin Long3ff547c2018-03-14 19:05:31 +08002032
2033 if (cmsgs->authinfo) {
2034 /* Reuse sinfo_tsn to indicate that authinfo was set and
2035 * sinfo_ssn to save the keyid on tx path.
2036 */
2037 sinfo->sinfo_tsn = 1;
2038 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
2039 }
Xin Longd42cb062018-03-01 23:05:15 +08002040}
2041
Ying Xue1b784142015-03-02 15:37:48 +08002042static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Xin Long204f8172018-03-01 23:05:14 +08002044 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Xin Long8e87c6e2018-03-01 23:05:16 +08002045 struct sctp_transport *transport = NULL;
Xin Long204f8172018-03-01 23:05:14 +08002046 struct sctp_sndrcvinfo _sinfo, *sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08002047 struct sctp_association *asoc;
2048 struct sctp_cmsgs cmsgs;
Xin Longbecef9b2018-03-01 23:05:13 +08002049 union sctp_addr *daddr;
Xin Long007b7e12018-03-01 23:05:17 +08002050 bool new = false;
2051 __u16 sflags;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02002052 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Xin Long204f8172018-03-01 23:05:14 +08002054 /* Parse and get snd_info */
2055 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
2056 if (err)
Xin Long007b7e12018-03-01 23:05:17 +08002057 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Xin Long204f8172018-03-01 23:05:14 +08002059 sinfo = &_sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08002060 sflags = sinfo->sinfo_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Xin Longbecef9b2018-03-01 23:05:13 +08002062 /* Get daddr from msg */
2063 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
2064 if (IS_ERR(daddr)) {
2065 err = PTR_ERR(daddr);
Xin Long007b7e12018-03-01 23:05:17 +08002066 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
2068
wangweidong048ed4b2014-01-21 15:44:11 +08002069 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Xin Long49102802018-03-05 20:44:20 +08002071 /* SCTP_SENDALL process */
2072 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
2073 list_for_each_entry(asoc, &ep->asocs, asocs) {
2074 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2075 msg_len);
2076 if (err == 0)
2077 continue;
2078 if (err < 0)
2079 goto out_unlock;
2080
2081 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2082
2083 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
2084 NULL, sinfo);
2085 if (err < 0)
2086 goto out_unlock;
2087
2088 iov_iter_revert(&msg->msg_iter, err);
2089 }
2090
2091 goto out_unlock;
2092 }
2093
Xin Long0a3920d2018-03-01 23:05:18 +08002094 /* Get and check or create asoc */
Xin Longbecef9b2018-03-01 23:05:13 +08002095 if (daddr) {
Xin Longbecef9b2018-03-01 23:05:13 +08002096 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
Xin Long0a3920d2018-03-01 23:05:18 +08002097 if (asoc) {
2098 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2099 msg_len);
2100 if (err <= 0)
2101 goto out_unlock;
2102 } else {
2103 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2104 &transport);
2105 if (err)
2106 goto out_unlock;
2107
2108 asoc = transport->asoc;
2109 new = true;
2110 }
2111
2112 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2113 transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 } else {
Xin Long007b7e12018-03-01 23:05:17 +08002115 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (!asoc) {
2117 err = -EPIPE;
2118 goto out_unlock;
2119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Xin Long007b7e12018-03-01 23:05:17 +08002121 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
Xin Longc2666de2018-03-01 23:05:12 +08002122 if (err <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125
Xin Longd42cb062018-03-01 23:05:15 +08002126 /* Update snd_info with the asoc */
2127 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Xin Longf84af332018-03-01 23:05:10 +08002129 /* Send msg to the asoc */
Xin Long8e87c6e2018-03-01 23:05:16 +08002130 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
Xin Long007b7e12018-03-01 23:05:17 +08002131 if (err < 0 && err != -ESRCH && new)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 sctp_association_free(asoc);
Xin Long8e87c6e2018-03-01 23:05:16 +08002133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134out_unlock:
wangweidong048ed4b2014-01-21 15:44:11 +08002135 release_sock(sk);
Xin Long007b7e12018-03-01 23:05:17 +08002136out:
Xin Longf84af332018-03-01 23:05:10 +08002137 return sctp_error(sk, msg->msg_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
2140/* This is an extended version of skb_pull() that removes the data from the
2141 * start of a skb even when data is spread across the list of skb's in the
2142 * frag_list. len specifies the total amount of data that needs to be removed.
2143 * when 'len' bytes could be removed from the skb, it returns 0.
2144 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2145 * could not be removed.
2146 */
2147static int sctp_skb_pull(struct sk_buff *skb, int len)
2148{
2149 struct sk_buff *list;
2150 int skb_len = skb_headlen(skb);
2151 int rlen;
2152
2153 if (len <= skb_len) {
2154 __skb_pull(skb, len);
2155 return 0;
2156 }
2157 len -= skb_len;
2158 __skb_pull(skb, skb_len);
2159
David S. Miller1b003be2009-06-09 00:22:35 -07002160 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 rlen = sctp_skb_pull(list, len);
2162 skb->len -= (len-rlen);
2163 skb->data_len -= (len-rlen);
2164
2165 if (!rlen)
2166 return 0;
2167
2168 len = rlen;
2169 }
2170
2171 return len;
2172}
2173
2174/* API 3.1.3 recvmsg() - UDP Style Syntax
2175 *
2176 * ssize_t recvmsg(int socket, struct msghdr *message,
2177 * int flags);
2178 *
2179 * socket - the socket descriptor of the endpoint.
2180 * message - pointer to the msghdr structure which contains a single
2181 * user message and possibly some ancillary data.
2182 *
2183 * See Section 5 for complete description of the data
2184 * structures.
2185 *
2186 * flags - flags sent or received with the user message, see Section
2187 * 5 for complete description of the flags.
2188 */
Ying Xue1b784142015-03-02 15:37:48 +08002189static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2190 int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
2192 struct sctp_ulpevent *event = NULL;
2193 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002194 struct sk_buff *skb, *head_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 int copied;
2196 int err = 0;
2197 int skb_len;
2198
Daniel Borkmannbb333812013-06-28 19:49:40 +02002199 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2200 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2201 addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
wangweidong048ed4b2014-01-21 15:44:11 +08002203 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03002205 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
Xin Longe0878692016-07-30 14:14:41 +08002206 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 err = -ENOTCONN;
2208 goto out;
2209 }
2210
2211 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2212 if (!skb)
2213 goto out;
2214
2215 /* Get the total length of the skb including any skb's in the
2216 * frag_list.
2217 */
2218 skb_len = skb->len;
2219
2220 copied = skb_len;
2221 if (copied > len)
2222 copied = len;
2223
David S. Miller51f3d022014-11-05 16:46:40 -05002224 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 event = sctp_skb2event(skb);
2227
2228 if (err)
2229 goto out_free;
2230
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002231 if (event->chunk && event->chunk->head_skb)
2232 head_skb = event->chunk->head_skb;
2233 else
2234 head_skb = skb;
2235 sock_recv_ts_and_drops(msg, sk, head_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 if (sctp_ulpevent_is_notification(event)) {
2237 msg->msg_flags |= MSG_NOTIFICATION;
2238 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2239 } else {
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002240 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02002243 /* Check if we allow SCTP_NXTINFO. */
2244 if (sp->recvnxtinfo)
2245 sctp_ulpevent_read_nxtinfo(event, msg, sk);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002246 /* Check if we allow SCTP_RCVINFO. */
2247 if (sp->recvrcvinfo)
2248 sctp_ulpevent_read_rcvinfo(event, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 /* Check if we allow SCTP_SNDRCVINFO. */
2250 if (sp->subscribe.sctp_data_io_event)
2251 sctp_ulpevent_read_sndrcvinfo(event, msg);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 err = copied;
2254
2255 /* If skb's length exceeds the user's buffer, update the skb and
2256 * push it back to the receive_queue so that the next call to
2257 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2258 */
2259 if (skb_len > copied) {
2260 msg->msg_flags &= ~MSG_EOR;
2261 if (flags & MSG_PEEK)
2262 goto out_free;
2263 sctp_skb_pull(skb, copied);
2264 skb_queue_head(&sk->sk_receive_queue, skb);
2265
Daniel Borkmann362d5202014-04-14 21:45:17 +02002266 /* When only partial message is copied to the user, increase
2267 * rwnd by that amount. If all the data in the skb is read,
2268 * rwnd is updated when the event is freed.
2269 */
2270 if (!sctp_ulpevent_is_notification(event))
2271 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 goto out;
2273 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2274 (event->msg_flags & MSG_EOR))
2275 msg->msg_flags |= MSG_EOR;
2276 else
2277 msg->msg_flags &= ~MSG_EOR;
2278
2279out_free:
2280 if (flags & MSG_PEEK) {
2281 /* Release the skb reference acquired after peeking the skb in
2282 * sctp_skb_recv_datagram().
2283 */
2284 kfree_skb(skb);
2285 } else {
2286 /* Free the event which includes releasing the reference to
2287 * the owner of the skb, freeing the skb and updating the
2288 * rwnd.
2289 */
2290 sctp_ulpevent_free(event);
2291 }
2292out:
wangweidong048ed4b2014-01-21 15:44:11 +08002293 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return err;
2295}
2296
2297/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2298 *
2299 * This option is a on/off flag. If enabled no SCTP message
2300 * fragmentation will be performed. Instead if a message being sent
2301 * exceeds the current PMTU size, the message will NOT be sent and
2302 * instead a error will be indicated to the user.
2303 */
2304static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002305 char __user *optval,
2306 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
2308 int val;
2309
2310 if (optlen < sizeof(int))
2311 return -EINVAL;
2312
2313 if (get_user(val, (int __user *)optval))
2314 return -EFAULT;
2315
2316 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2317
2318 return 0;
2319}
2320
2321static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002322 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Wei Yongjun94912302011-07-02 09:28:04 +00002324 struct sctp_association *asoc;
2325 struct sctp_ulpevent *event;
2326
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002327 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return -EINVAL;
2329 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2330 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002331
Daniel Borkmannbbbea412014-07-12 20:30:40 +02002332 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
Wei Yongjun94912302011-07-02 09:28:04 +00002333 * if there is no data to be sent or retransmit, the stack will
2334 * immediately send up this notification.
2335 */
2336 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2337 &sctp_sk(sk)->subscribe)) {
2338 asoc = sctp_id2assoc(sk, 0);
2339
2340 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2341 event = sctp_ulpevent_make_sender_dry_event(asoc,
Marcelo Ricardo Leitner2e83acb2018-01-08 19:02:27 -02002342 GFP_USER | __GFP_NOWARN);
Wei Yongjun94912302011-07-02 09:28:04 +00002343 if (!event)
2344 return -ENOMEM;
2345
Xin Long9162e0e2017-12-08 21:04:05 +08002346 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
Wei Yongjun94912302011-07-02 09:28:04 +00002347 }
2348 }
2349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return 0;
2351}
2352
2353/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2354 *
2355 * This socket option is applicable to the UDP-style socket only. When
2356 * set it will cause associations that are idle for more than the
2357 * specified number of seconds to automatically close. An association
2358 * being idle is defined an association that has NOT sent or received
2359 * user data. The special value of '0' indicates that no automatic
2360 * close of any associations should be performed. The option expects an
2361 * integer defining the number of seconds of idle time before an
2362 * association is closed.
2363 */
2364static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002365 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
2367 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman9f70f462013-12-10 06:48:15 -05002368 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 /* Applicable to UDP-style socket only */
2371 if (sctp_style(sk, TCP))
2372 return -EOPNOTSUPP;
2373 if (optlen != sizeof(int))
2374 return -EINVAL;
2375 if (copy_from_user(&sp->autoclose, optval, optlen))
2376 return -EFAULT;
2377
Neil Horman9f70f462013-12-10 06:48:15 -05002378 if (sp->autoclose > net->sctp.max_autoclose)
2379 sp->autoclose = net->sctp.max_autoclose;
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return 0;
2382}
2383
2384/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2385 *
2386 * Applications can enable or disable heartbeats for any peer address of
2387 * an association, modify an address's heartbeat interval, force a
2388 * heartbeat to be sent immediately, and adjust the address's maximum
2389 * number of retransmissions sent before an address is considered
2390 * unreachable. The following structure is used to access and modify an
2391 * address's parameters:
2392 *
2393 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002394 * sctp_assoc_t spp_assoc_id;
2395 * struct sockaddr_storage spp_address;
2396 * uint32_t spp_hbinterval;
2397 * uint16_t spp_pathmaxrxt;
2398 * uint32_t spp_pathmtu;
2399 * uint32_t spp_sackdelay;
2400 * uint32_t spp_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08002401 * uint32_t spp_ipv6_flowlabel;
2402 * uint8_t spp_dscp;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002403 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002405 * spp_assoc_id - (one-to-many style socket) This is filled in the
2406 * application, and identifies the association for
2407 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 * spp_address - This specifies which address is of interest.
2409 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002410 * in milliseconds. If a value of zero
2411 * is present in this field then no changes are to
2412 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 * spp_pathmaxrxt - This contains the maximum number of
2414 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002415 * considered unreachable. If a value of zero
2416 * is present in this field then no changes are to
2417 * be made to this parameter.
2418 * spp_pathmtu - When Path MTU discovery is disabled the value
2419 * specified here will be the "fixed" path mtu.
2420 * Note that if the spp_address field is empty
2421 * then all associations on this address will
2422 * have this fixed path mtu set upon them.
2423 *
2424 * spp_sackdelay - When delayed sack is enabled, this value specifies
2425 * the number of milliseconds that sacks will be delayed
2426 * for. This value will apply to all addresses of an
2427 * association if the spp_address field is empty. Note
2428 * also, that if delayed sack is enabled and this
2429 * value is set to 0, no change is made to the last
2430 * recorded delayed sack timer value.
2431 *
2432 * spp_flags - These flags are used to control various features
2433 * on an association. The flag field may contain
2434 * zero or more of the following options.
2435 *
2436 * SPP_HB_ENABLE - Enable heartbeats on the
2437 * specified address. Note that if the address
2438 * field is empty all addresses for the association
2439 * have heartbeats enabled upon them.
2440 *
2441 * SPP_HB_DISABLE - Disable heartbeats on the
2442 * speicifed address. Note that if the address
2443 * field is empty all addresses for the association
2444 * will have their heartbeats disabled. Note also
2445 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2446 * mutually exclusive, only one of these two should
2447 * be specified. Enabling both fields will have
2448 * undetermined results.
2449 *
2450 * SPP_HB_DEMAND - Request a user initiated heartbeat
2451 * to be made immediately.
2452 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002453 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2454 * heartbeat delayis to be set to the value of 0
2455 * milliseconds.
2456 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002457 * SPP_PMTUD_ENABLE - This field will enable PMTU
2458 * discovery upon the specified address. Note that
2459 * if the address feild is empty then all addresses
2460 * on the association are effected.
2461 *
2462 * SPP_PMTUD_DISABLE - This field will disable PMTU
2463 * discovery upon the specified address. Note that
2464 * if the address feild is empty then all addresses
2465 * on the association are effected. Not also that
2466 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2467 * exclusive. Enabling both will have undetermined
2468 * results.
2469 *
2470 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2471 * on delayed sack. The time specified in spp_sackdelay
2472 * is used to specify the sack delay for this address. Note
2473 * that if spp_address is empty then all addresses will
2474 * enable delayed sack and take on the sack delay
2475 * value specified in spp_sackdelay.
2476 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2477 * off delayed sack. If the spp_address field is blank then
2478 * delayed sack is disabled for the entire association. Note
2479 * also that this field is mutually exclusive to
2480 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2481 * results.
Xin Long0b0dce72018-07-02 18:21:13 +08002482 *
2483 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
2484 * setting of the IPV6 flow label value. The value is
2485 * contained in the spp_ipv6_flowlabel field.
2486 * Upon retrieval, this flag will be set to indicate that
2487 * the spp_ipv6_flowlabel field has a valid value returned.
2488 * If a specific destination address is set (in the
2489 * spp_address field), then the value returned is that of
2490 * the address. If just an association is specified (and
2491 * no address), then the association's default flow label
2492 * is returned. If neither an association nor a destination
2493 * is specified, then the socket's default flow label is
2494 * returned. For non-IPv6 sockets, this flag will be left
2495 * cleared.
2496 *
2497 * SPP_DSCP: Setting this flag enables the setting of the
2498 * Differentiated Services Code Point (DSCP) value
2499 * associated with either the association or a specific
2500 * address. The value is obtained in the spp_dscp field.
2501 * Upon retrieval, this flag will be set to indicate that
2502 * the spp_dscp field has a valid value returned. If a
2503 * specific destination address is set when called (in the
2504 * spp_address field), then that specific destination
2505 * address's DSCP value is returned. If just an association
2506 * is specified, then the association's default DSCP is
2507 * returned. If neither an association nor a destination is
2508 * specified, then the socket's default DSCP is returned.
2509 *
2510 * spp_ipv6_flowlabel
2511 * - This field is used in conjunction with the
2512 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
2513 * The 20 least significant bits are used for the flow
2514 * label. This setting has precedence over any IPv6-layer
2515 * setting.
2516 *
2517 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
2518 * and contains the DSCP. The 6 most significant bits are
2519 * used for the DSCP. This setting has precedence over any
2520 * IPv4- or IPv6- layer setting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 */
Adrian Bunk16164362006-09-18 00:40:38 -07002522static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2523 struct sctp_transport *trans,
2524 struct sctp_association *asoc,
2525 struct sctp_sock *sp,
2526 int hb_change,
2527 int pmtud_change,
2528 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 int error;
2531
Frank Filz52ccb8e2005-12-22 11:36:46 -08002532 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002533 struct net *net = sock_net(trans->asoc->base.sk);
2534
2535 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if (error)
2537 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
2539
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002540 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2541 * this field is ignored. Note also that a value of zero indicates
2542 * the current setting should be left unchanged.
2543 */
2544 if (params->spp_flags & SPP_HB_ENABLE) {
2545
2546 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2547 * set. This lets us use 0 value when this flag
2548 * is set.
2549 */
2550 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2551 params->spp_hbinterval = 0;
2552
2553 if (params->spp_hbinterval ||
2554 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2555 if (trans) {
2556 trans->hbinterval =
2557 msecs_to_jiffies(params->spp_hbinterval);
2558 } else if (asoc) {
2559 asoc->hbinterval =
2560 msecs_to_jiffies(params->spp_hbinterval);
2561 } else {
2562 sp->hbinterval = params->spp_hbinterval;
2563 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002564 }
2565 }
2566
2567 if (hb_change) {
2568 if (trans) {
2569 trans->param_flags =
2570 (trans->param_flags & ~SPP_HB) | hb_change;
2571 } else if (asoc) {
2572 asoc->param_flags =
2573 (asoc->param_flags & ~SPP_HB) | hb_change;
2574 } else {
2575 sp->param_flags =
2576 (sp->param_flags & ~SPP_HB) | hb_change;
2577 }
2578 }
2579
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002580 /* When Path MTU discovery is disabled the value specified here will
2581 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2582 * include the flag SPP_PMTUD_DISABLE for this field to have any
2583 * effect).
2584 */
2585 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002586 if (trans) {
2587 trans->pathmtu = params->spp_pathmtu;
Xin Long3ebfdf02017-04-04 13:39:55 +08002588 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002589 } else if (asoc) {
Marcelo Ricardo Leitnerc4b28932018-04-26 16:58:53 -03002590 sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002591 } else {
2592 sp->pathmtu = params->spp_pathmtu;
2593 }
2594 }
2595
2596 if (pmtud_change) {
2597 if (trans) {
2598 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2599 (params->spp_flags & SPP_PMTUD_ENABLE);
2600 trans->param_flags =
2601 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2602 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002603 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
Xin Long3ebfdf02017-04-04 13:39:55 +08002604 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002605 }
2606 } else if (asoc) {
2607 asoc->param_flags =
2608 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2609 } else {
2610 sp->param_flags =
2611 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2612 }
2613 }
2614
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002615 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2616 * value of this field is ignored. Note also that a value of zero
2617 * indicates the current setting should be left unchanged.
2618 */
2619 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002620 if (trans) {
2621 trans->sackdelay =
2622 msecs_to_jiffies(params->spp_sackdelay);
2623 } else if (asoc) {
2624 asoc->sackdelay =
2625 msecs_to_jiffies(params->spp_sackdelay);
2626 } else {
2627 sp->sackdelay = params->spp_sackdelay;
2628 }
2629 }
2630
2631 if (sackdelay_change) {
2632 if (trans) {
2633 trans->param_flags =
2634 (trans->param_flags & ~SPP_SACKDELAY) |
2635 sackdelay_change;
2636 } else if (asoc) {
2637 asoc->param_flags =
2638 (asoc->param_flags & ~SPP_SACKDELAY) |
2639 sackdelay_change;
2640 } else {
2641 sp->param_flags =
2642 (sp->param_flags & ~SPP_SACKDELAY) |
2643 sackdelay_change;
2644 }
2645 }
2646
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002647 /* Note that a value of zero indicates the current setting should be
2648 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002649 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002650 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002651 if (trans) {
2652 trans->pathmaxrxt = params->spp_pathmaxrxt;
2653 } else if (asoc) {
2654 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2655 } else {
2656 sp->pathmaxrxt = params->spp_pathmaxrxt;
2657 }
2658 }
2659
Xin Long0b0dce72018-07-02 18:21:13 +08002660 if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
2661 if (trans && trans->ipaddr.sa.sa_family == AF_INET6) {
2662 trans->flowlabel = params->spp_ipv6_flowlabel &
2663 SCTP_FLOWLABEL_VAL_MASK;
2664 trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2665 } else if (asoc) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002666 struct sctp_transport *t;
2667
2668 list_for_each_entry(t, &asoc->peer.transport_addr_list,
Xin Long0b0dce72018-07-02 18:21:13 +08002669 transports) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002670 if (t->ipaddr.sa.sa_family != AF_INET6)
Xin Long0b0dce72018-07-02 18:21:13 +08002671 continue;
Xin Longaf8a2b82018-09-03 15:47:10 +08002672 t->flowlabel = params->spp_ipv6_flowlabel &
2673 SCTP_FLOWLABEL_VAL_MASK;
2674 t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
Xin Long0b0dce72018-07-02 18:21:13 +08002675 }
2676 asoc->flowlabel = params->spp_ipv6_flowlabel &
2677 SCTP_FLOWLABEL_VAL_MASK;
2678 asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2679 } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
2680 sp->flowlabel = params->spp_ipv6_flowlabel &
2681 SCTP_FLOWLABEL_VAL_MASK;
2682 sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2683 }
2684 }
2685
2686 if (params->spp_flags & SPP_DSCP) {
2687 if (trans) {
2688 trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2689 trans->dscp |= SCTP_DSCP_SET_MASK;
2690 } else if (asoc) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002691 struct sctp_transport *t;
2692
2693 list_for_each_entry(t, &asoc->peer.transport_addr_list,
Xin Long0b0dce72018-07-02 18:21:13 +08002694 transports) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002695 t->dscp = params->spp_dscp &
2696 SCTP_DSCP_VAL_MASK;
2697 t->dscp |= SCTP_DSCP_SET_MASK;
Xin Long0b0dce72018-07-02 18:21:13 +08002698 }
2699 asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2700 asoc->dscp |= SCTP_DSCP_SET_MASK;
2701 } else {
2702 sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2703 sp->dscp |= SCTP_DSCP_SET_MASK;
2704 }
2705 }
2706
Frank Filz52ccb8e2005-12-22 11:36:46 -08002707 return 0;
2708}
2709
2710static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002711 char __user *optval,
2712 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002713{
2714 struct sctp_paddrparams params;
2715 struct sctp_transport *trans = NULL;
2716 struct sctp_association *asoc = NULL;
2717 struct sctp_sock *sp = sctp_sk(sk);
2718 int error;
2719 int hb_change, pmtud_change, sackdelay_change;
2720
Xin Long0b0dce72018-07-02 18:21:13 +08002721 if (optlen == sizeof(params)) {
2722 if (copy_from_user(&params, optval, optlen))
2723 return -EFAULT;
2724 } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
2725 spp_ipv6_flowlabel), 4)) {
2726 if (copy_from_user(&params, optval, optlen))
2727 return -EFAULT;
2728 if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
2729 return -EINVAL;
2730 } else {
wangweidongcb3f8372013-12-23 12:16:50 +08002731 return -EINVAL;
Xin Long0b0dce72018-07-02 18:21:13 +08002732 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002733
2734 /* Validate flags and value parameters. */
2735 hb_change = params.spp_flags & SPP_HB;
2736 pmtud_change = params.spp_flags & SPP_PMTUD;
2737 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2738
2739 if (hb_change == SPP_HB ||
2740 pmtud_change == SPP_PMTUD ||
2741 sackdelay_change == SPP_SACKDELAY ||
2742 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002743 (params.spp_pathmtu &&
2744 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002745 return -EINVAL;
2746
2747 /* If an address other than INADDR_ANY is specified, and
2748 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 */
wangweidongcb3f8372013-12-23 12:16:50 +08002750 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002751 trans = sctp_addr_id2transport(sk, &params.spp_address,
2752 params.spp_assoc_id);
2753 if (!trans)
2754 return -EINVAL;
2755 }
2756
2757 /* Get association, if assoc_id != 0 and the socket is a one
2758 * to many style socket, and an association was not found, then
2759 * the id was invalid.
2760 */
2761 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2762 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2763 return -EINVAL;
2764
2765 /* Heartbeat demand can only be sent on a transport or
2766 * association, but not a socket.
2767 */
2768 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2769 return -EINVAL;
2770
2771 /* Process parameters. */
2772 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2773 hb_change, pmtud_change,
2774 sackdelay_change);
2775
2776 if (error)
2777 return error;
2778
2779 /* If changes are for association, also apply parameters to each
2780 * transport.
2781 */
2782 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002783 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2784 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002785 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2786 hb_change, pmtud_change,
2787 sackdelay_change);
2788 }
2789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791 return 0;
2792}
2793
wangweidong0ea5e4d2014-01-15 17:24:01 +08002794static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2795{
2796 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2797}
2798
2799static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2800{
2801 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2802}
2803
Wei Yongjund364d922008-05-09 15:13:26 -07002804/*
2805 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002806 *
Wei Yongjund364d922008-05-09 15:13:26 -07002807 * This option will effect the way delayed acks are performed. This
2808 * option allows you to get or set the delayed ack time, in
2809 * milliseconds. It also allows changing the delayed ack frequency.
2810 * Changing the frequency to 1 disables the delayed sack algorithm. If
2811 * the assoc_id is 0, then this sets or gets the endpoints default
2812 * values. If the assoc_id field is non-zero, then the set or get
2813 * effects the specified association for the one to many model (the
2814 * assoc_id field is ignored by the one to one model). Note that if
2815 * sack_delay or sack_freq are 0 when setting this option, then the
2816 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002817 *
Wei Yongjund364d922008-05-09 15:13:26 -07002818 * struct sctp_sack_info {
2819 * sctp_assoc_t sack_assoc_id;
2820 * uint32_t sack_delay;
2821 * uint32_t sack_freq;
2822 * };
Frank Filz77086102005-12-22 11:37:30 -08002823 *
Wei Yongjund364d922008-05-09 15:13:26 -07002824 * sack_assoc_id - This parameter, indicates which association the user
2825 * is performing an action upon. Note that if this field's value is
2826 * zero then the endpoints default value is changed (effecting future
2827 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002828 *
Wei Yongjund364d922008-05-09 15:13:26 -07002829 * sack_delay - This parameter contains the number of milliseconds that
2830 * the user is requesting the delayed ACK timer be set to. Note that
2831 * this value is defined in the standard to be between 200 and 500
2832 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002833 *
Wei Yongjund364d922008-05-09 15:13:26 -07002834 * sack_freq - This parameter contains the number of packets that must
2835 * be received before a sack is sent without waiting for the delay
2836 * timer to expire. The default value for this is 2, setting this
2837 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002838 */
2839
Wei Yongjund364d922008-05-09 15:13:26 -07002840static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002841 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002842{
Wei Yongjund364d922008-05-09 15:13:26 -07002843 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002844 struct sctp_transport *trans = NULL;
2845 struct sctp_association *asoc = NULL;
2846 struct sctp_sock *sp = sctp_sk(sk);
2847
Wei Yongjund364d922008-05-09 15:13:26 -07002848 if (optlen == sizeof(struct sctp_sack_info)) {
2849 if (copy_from_user(&params, optval, optlen))
2850 return -EFAULT;
2851
2852 if (params.sack_delay == 0 && params.sack_freq == 0)
2853 return 0;
2854 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05002855 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05002856 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05002857 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05002858 "Use struct sctp_sack_info instead\n",
2859 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07002860 if (copy_from_user(&params, optval, optlen))
2861 return -EFAULT;
2862
2863 if (params.sack_delay == 0)
2864 params.sack_freq = 1;
2865 else
2866 params.sack_freq = 0;
2867 } else
wangweidongcb3f8372013-12-23 12:16:50 +08002868 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08002869
Frank Filz77086102005-12-22 11:37:30 -08002870 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002871 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002872 return -EINVAL;
2873
Wei Yongjund364d922008-05-09 15:13:26 -07002874 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002875 * to many style socket, and an association was not found, then
2876 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002877 */
Wei Yongjund364d922008-05-09 15:13:26 -07002878 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2879 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002880 return -EINVAL;
2881
Wei Yongjund364d922008-05-09 15:13:26 -07002882 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002883 if (asoc) {
2884 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002885 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002886 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002887 sctp_spp_sackdelay_enable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002888 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002889 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002890 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002891 sctp_spp_sackdelay_enable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002892 }
Wei Yongjund364d922008-05-09 15:13:26 -07002893 }
2894
2895 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002896 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002897 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002898 sctp_spp_sackdelay_disable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002899 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002900 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002901 sctp_spp_sackdelay_disable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002902 }
Wei Yongjund364d922008-05-09 15:13:26 -07002903 } else if (params.sack_freq > 1) {
2904 if (asoc) {
2905 asoc->sackfreq = params.sack_freq;
2906 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002907 sctp_spp_sackdelay_enable(asoc->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002908 } else {
2909 sp->sackfreq = params.sack_freq;
2910 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002911 sctp_spp_sackdelay_enable(sp->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002912 }
Frank Filz77086102005-12-22 11:37:30 -08002913 }
2914
2915 /* If change is for association, also apply to each transport. */
2916 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002917 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2918 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002919 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002920 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002921 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002922 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002923 sctp_spp_sackdelay_enable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002924 }
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07002925 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002926 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002927 sctp_spp_sackdelay_disable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002928 } else if (params.sack_freq > 1) {
2929 trans->sackfreq = params.sack_freq;
2930 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002931 sctp_spp_sackdelay_enable(trans->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002932 }
2933 }
2934 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002935
Frank Filz77086102005-12-22 11:37:30 -08002936 return 0;
2937}
2938
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2940 *
2941 * Applications can specify protocol parameters for the default association
2942 * initialization. The option name argument to setsockopt() and getsockopt()
2943 * is SCTP_INITMSG.
2944 *
2945 * Setting initialization parameters is effective only on an unconnected
2946 * socket (for UDP-style sockets only future associations are effected
2947 * by the change). With TCP-style sockets, this option is inherited by
2948 * sockets derived from a listener socket.
2949 */
David S. Millerb7058842009-09-30 16:12:20 -07002950static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951{
2952 struct sctp_initmsg sinit;
2953 struct sctp_sock *sp = sctp_sk(sk);
2954
2955 if (optlen != sizeof(struct sctp_initmsg))
2956 return -EINVAL;
2957 if (copy_from_user(&sinit, optval, optlen))
2958 return -EFAULT;
2959
2960 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002961 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002963 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002965 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002967 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
2969 return 0;
2970}
2971
2972/*
2973 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2974 *
2975 * Applications that wish to use the sendto() system call may wish to
2976 * specify a default set of parameters that would normally be supplied
2977 * through the inclusion of ancillary data. This socket option allows
2978 * such an application to set the default sctp_sndrcvinfo structure.
2979 * The application that wishes to use this socket option simply passes
2980 * in to this call the sctp_sndrcvinfo structure defined in Section
2981 * 5.2.2) The input parameters accepted by this call include
2982 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2983 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2984 * to this call if the caller is using the UDP model.
2985 */
2986static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002987 char __user *optval,
2988 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002991 struct sctp_association *asoc;
2992 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002994 if (optlen != sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 return -EINVAL;
2996 if (copy_from_user(&info, optval, optlen))
2997 return -EFAULT;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002998 if (info.sinfo_flags &
2999 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3000 SCTP_ABORT | SCTP_EOF))
3001 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
3003 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
3004 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
3005 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 if (asoc) {
3007 asoc->default_stream = info.sinfo_stream;
3008 asoc->default_flags = info.sinfo_flags;
3009 asoc->default_ppid = info.sinfo_ppid;
3010 asoc->default_context = info.sinfo_context;
3011 asoc->default_timetolive = info.sinfo_timetolive;
3012 } else {
3013 sp->default_stream = info.sinfo_stream;
3014 sp->default_flags = info.sinfo_flags;
3015 sp->default_ppid = info.sinfo_ppid;
3016 sp->default_context = info.sinfo_context;
3017 sp->default_timetolive = info.sinfo_timetolive;
3018 }
3019
3020 return 0;
3021}
3022
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003023/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
3024 * (SCTP_DEFAULT_SNDINFO)
3025 */
3026static int sctp_setsockopt_default_sndinfo(struct sock *sk,
3027 char __user *optval,
3028 unsigned int optlen)
3029{
3030 struct sctp_sock *sp = sctp_sk(sk);
3031 struct sctp_association *asoc;
3032 struct sctp_sndinfo info;
3033
3034 if (optlen != sizeof(info))
3035 return -EINVAL;
3036 if (copy_from_user(&info, optval, optlen))
3037 return -EFAULT;
3038 if (info.snd_flags &
3039 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3040 SCTP_ABORT | SCTP_EOF))
3041 return -EINVAL;
3042
3043 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
3044 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
3045 return -EINVAL;
3046 if (asoc) {
3047 asoc->default_stream = info.snd_sid;
3048 asoc->default_flags = info.snd_flags;
3049 asoc->default_ppid = info.snd_ppid;
3050 asoc->default_context = info.snd_context;
3051 } else {
3052 sp->default_stream = info.snd_sid;
3053 sp->default_flags = info.snd_flags;
3054 sp->default_ppid = info.snd_ppid;
3055 sp->default_context = info.snd_context;
3056 }
3057
3058 return 0;
3059}
3060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3062 *
3063 * Requests that the local SCTP stack use the enclosed peer address as
3064 * the association primary. The enclosed address must be one of the
3065 * association peer's addresses.
3066 */
3067static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003068 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
3070 struct sctp_prim prim;
3071 struct sctp_transport *trans;
Richard Haines2277c7c2018-02-13 20:56:24 +00003072 struct sctp_af *af;
3073 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
3075 if (optlen != sizeof(struct sctp_prim))
3076 return -EINVAL;
3077
3078 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3079 return -EFAULT;
3080
Richard Haines2277c7c2018-02-13 20:56:24 +00003081 /* Allow security module to validate address but need address len. */
3082 af = sctp_get_af_specific(prim.ssp_addr.ss_family);
3083 if (!af)
3084 return -EINVAL;
3085
3086 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
3087 (struct sockaddr *)&prim.ssp_addr,
3088 af->sockaddr_len);
3089 if (err)
3090 return err;
3091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
3093 if (!trans)
3094 return -EINVAL;
3095
3096 sctp_assoc_set_primary(trans->asoc, trans);
3097
3098 return 0;
3099}
3100
3101/*
3102 * 7.1.5 SCTP_NODELAY
3103 *
3104 * Turn on/off any Nagle-like algorithm. This means that packets are
3105 * generally sent as soon as possible and no unnecessary delays are
3106 * introduced, at the cost of more packets in the network. Expects an
3107 * integer boolean flag.
3108 */
3109static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003110 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
3112 int val;
3113
3114 if (optlen < sizeof(int))
3115 return -EINVAL;
3116 if (get_user(val, (int __user *)optval))
3117 return -EFAULT;
3118
3119 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3120 return 0;
3121}
3122
3123/*
3124 *
3125 * 7.1.1 SCTP_RTOINFO
3126 *
3127 * The protocol parameters used to initialize and bound retransmission
3128 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3129 * and modify these parameters.
3130 * All parameters are time values, in milliseconds. A value of 0, when
3131 * modifying the parameters, indicates that the current value should not
3132 * be changed.
3133 *
3134 */
David S. Millerb7058842009-09-30 16:12:20 -07003135static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3136{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 struct sctp_rtoinfo rtoinfo;
3138 struct sctp_association *asoc;
wangweidong85f935d2013-12-11 09:50:38 +08003139 unsigned long rto_min, rto_max;
3140 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
3142 if (optlen != sizeof (struct sctp_rtoinfo))
3143 return -EINVAL;
3144
3145 if (copy_from_user(&rtoinfo, optval, optlen))
3146 return -EFAULT;
3147
3148 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3149
3150 /* Set the values to the specific association */
3151 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3152 return -EINVAL;
3153
wangweidong85f935d2013-12-11 09:50:38 +08003154 rto_max = rtoinfo.srto_max;
3155 rto_min = rtoinfo.srto_min;
3156
3157 if (rto_max)
3158 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3159 else
3160 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3161
3162 if (rto_min)
3163 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3164 else
3165 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3166
3167 if (rto_min > rto_max)
3168 return -EINVAL;
3169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 if (asoc) {
3171 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003172 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 msecs_to_jiffies(rtoinfo.srto_initial);
wangweidong85f935d2013-12-11 09:50:38 +08003174 asoc->rto_max = rto_max;
3175 asoc->rto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 } else {
3177 /* If there is no association or the association-id = 0
3178 * set the values to the endpoint.
3179 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 if (rtoinfo.srto_initial != 0)
3181 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
wangweidong85f935d2013-12-11 09:50:38 +08003182 sp->rtoinfo.srto_max = rto_max;
3183 sp->rtoinfo.srto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 }
3185
3186 return 0;
3187}
3188
3189/*
3190 *
3191 * 7.1.2 SCTP_ASSOCINFO
3192 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02003193 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 * of the association.
3195 * Returns an error if the new association retransmission value is
3196 * greater than the sum of the retransmission value of the peer.
3197 * See [SCTP] for more information.
3198 *
3199 */
David S. Millerb7058842009-09-30 16:12:20 -07003200static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
3202
3203 struct sctp_assocparams assocparams;
3204 struct sctp_association *asoc;
3205
3206 if (optlen != sizeof(struct sctp_assocparams))
3207 return -EINVAL;
3208 if (copy_from_user(&assocparams, optval, optlen))
3209 return -EFAULT;
3210
3211 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3212
3213 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3214 return -EINVAL;
3215
3216 /* Set the values to the specific association */
3217 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003218 if (assocparams.sasoc_asocmaxrxt != 0) {
3219 __u32 path_sum = 0;
3220 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003221 struct sctp_transport *peer_addr;
3222
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07003223 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3224 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003225 path_sum += peer_addr->pathmaxrxt;
3226 paths++;
3227 }
3228
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003229 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003230 * one path/transport. We do this because path
3231 * retransmissions are only counted when we have more
3232 * then one path.
3233 */
3234 if (paths > 1 &&
3235 assocparams.sasoc_asocmaxrxt > path_sum)
3236 return -EINVAL;
3237
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003239 }
3240
Daniel Borkmann52db8822013-06-25 18:17:27 +02003241 if (assocparams.sasoc_cookie_life != 0)
3242 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 } else {
3244 /* Set the values to the endpoint */
3245 struct sctp_sock *sp = sctp_sk(sk);
3246
3247 if (assocparams.sasoc_asocmaxrxt != 0)
3248 sp->assocparams.sasoc_asocmaxrxt =
3249 assocparams.sasoc_asocmaxrxt;
3250 if (assocparams.sasoc_cookie_life != 0)
3251 sp->assocparams.sasoc_cookie_life =
3252 assocparams.sasoc_cookie_life;
3253 }
3254 return 0;
3255}
3256
3257/*
3258 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3259 *
3260 * This socket option is a boolean flag which turns on or off mapped V4
3261 * addresses. If this option is turned on and the socket is type
3262 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3263 * If this option is turned off, then no mapping will be done of V4
3264 * addresses and a user will receive both PF_INET6 and PF_INET type
3265 * addresses on the socket.
3266 */
David S. Millerb7058842009-09-30 16:12:20 -07003267static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
3269 int val;
3270 struct sctp_sock *sp = sctp_sk(sk);
3271
3272 if (optlen < sizeof(int))
3273 return -EINVAL;
3274 if (get_user(val, (int __user *)optval))
3275 return -EFAULT;
3276 if (val)
3277 sp->v4mapped = 1;
3278 else
3279 sp->v4mapped = 0;
3280
3281 return 0;
3282}
3283
3284/*
Wei Yongjune89c2092008-12-25 16:54:58 -08003285 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3286 * This option will get or set the maximum size to put in any outgoing
3287 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 * fragmented by SCTP into the specified size. Note that the underlying
3289 * SCTP implementation may fragment into smaller sized chunks when the
3290 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08003291 * the user. The default value for this option is '0' which indicates
3292 * the user is NOT limiting fragmentation and only the PMTU will effect
3293 * SCTP's choice of DATA chunk size. Note also that values set larger
3294 * than the maximum size of an IP datagram will effectively let SCTP
3295 * control fragmentation (i.e. the same as setting this option to 0).
3296 *
3297 * The following structure is used to access and modify this parameter:
3298 *
3299 * struct sctp_assoc_value {
3300 * sctp_assoc_t assoc_id;
3301 * uint32_t assoc_value;
3302 * };
3303 *
3304 * assoc_id: This parameter is ignored for one-to-one style sockets.
3305 * For one-to-many style sockets this parameter indicates which
3306 * association the user is performing an action upon. Note that if
3307 * this field's value is zero then the endpoints default value is
3308 * changed (effecting future associations only).
3309 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 */
David S. Millerb7058842009-09-30 16:12:20 -07003311static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
Xin Longecca8f82017-11-17 14:11:11 +08003313 struct sctp_sock *sp = sctp_sk(sk);
Wei Yongjune89c2092008-12-25 16:54:58 -08003314 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 int val;
3317
Wei Yongjune89c2092008-12-25 16:54:58 -08003318 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003319 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003320 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003321 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003322 "Use struct sctp_assoc_value instead\n",
3323 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08003324 if (copy_from_user(&val, optval, optlen))
3325 return -EFAULT;
3326 params.assoc_id = 0;
3327 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3328 if (copy_from_user(&params, optval, optlen))
3329 return -EFAULT;
3330 val = params.assoc_value;
Xin Longecca8f82017-11-17 14:11:11 +08003331 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 return -EINVAL;
Xin Longecca8f82017-11-17 14:11:11 +08003333 }
Wei Yongjune89c2092008-12-25 16:54:58 -08003334
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003335 asoc = sctp_id2assoc(sk, params.assoc_id);
3336
Xin Longecca8f82017-11-17 14:11:11 +08003337 if (val) {
3338 int min_len, max_len;
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003339 __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
3340 sizeof(struct sctp_data_chunk);
Xin Longecca8f82017-11-17 14:11:11 +08003341
Marcelo Ricardo Leitnerfeddd6c2018-04-26 16:58:54 -03003342 min_len = sctp_mtu_payload(sp, SCTP_DEFAULT_MINSEGMENT,
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003343 datasize);
3344 max_len = SCTP_MAX_CHUNK_LEN - datasize;
Xin Longecca8f82017-11-17 14:11:11 +08003345
3346 if (val < min_len || val > max_len)
3347 return -EINVAL;
3348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Wei Yongjune89c2092008-12-25 16:54:58 -08003350 if (asoc) {
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003351 asoc->user_frag = val;
Marcelo Ricardo Leitner2f5e3c92018-04-26 16:58:55 -03003352 sctp_assoc_update_frag_point(asoc);
Wei Yongjune89c2092008-12-25 16:54:58 -08003353 } else {
Xin Longecca8f82017-11-17 14:11:11 +08003354 if (params.assoc_id && sctp_style(sk, UDP))
3355 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003356 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 }
3358
3359 return 0;
3360}
3361
3362
3363/*
3364 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3365 *
3366 * Requests that the peer mark the enclosed address as the association
3367 * primary. The enclosed address must be one of the association's
3368 * locally bound addresses. The following structure is used to make a
3369 * set primary request:
3370 */
3371static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003372 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003374 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 struct sctp_association *asoc = NULL;
3377 struct sctp_setpeerprim prim;
3378 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003379 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 int err;
3381
3382 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003384 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return -EPERM;
3386
3387 if (optlen != sizeof(struct sctp_setpeerprim))
3388 return -EINVAL;
3389
3390 if (copy_from_user(&prim, optval, optlen))
3391 return -EFAULT;
3392
3393 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003394 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 return -EINVAL;
3396
3397 if (!asoc->peer.asconf_capable)
3398 return -EPERM;
3399
3400 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3401 return -EPERM;
3402
3403 if (!sctp_state(asoc, ESTABLISHED))
3404 return -ENOTCONN;
3405
Wei Yongjun40a01032010-12-07 17:11:09 +00003406 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3407 if (!af)
3408 return -EINVAL;
3409
3410 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3411 return -EADDRNOTAVAIL;
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3414 return -EADDRNOTAVAIL;
3415
Richard Haines2277c7c2018-02-13 20:56:24 +00003416 /* Allow security module to validate address. */
3417 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3418 (struct sockaddr *)&prim.sspp_addr,
3419 af->sockaddr_len);
3420 if (err)
3421 return err;
3422
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3424 chunk = sctp_make_asconf_set_prim(asoc,
3425 (union sctp_addr *)&prim.sspp_addr);
3426 if (!chunk)
3427 return -ENOMEM;
3428
3429 err = sctp_send_asconf(asoc, chunk);
3430
Daniel Borkmannbb333812013-06-28 19:49:40 +02003431 pr_debug("%s: we set peer primary addr primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
3433 return err;
3434}
3435
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003436static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003437 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003439 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003441 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003443 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 return -EFAULT;
3445
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003446 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
3448 return 0;
3449}
3450
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003451/*
3452 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3453 *
3454 * The context field in the sctp_sndrcvinfo structure is normally only
3455 * used when a failed message is retrieved holding the value that was
3456 * sent down on the actual send call. This option allows the setting of
3457 * a default context on an association basis that will be received on
3458 * reading messages from the peer. This is especially helpful in the
3459 * one-2-many model for an application to keep some reference to an
3460 * internal state machine that is processing messages on the
3461 * association. Note that the setting of this value only effects
3462 * received messages from the peer and does not effect the value that is
3463 * saved with outbound messages.
3464 */
3465static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003466 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003467{
3468 struct sctp_assoc_value params;
3469 struct sctp_sock *sp;
3470 struct sctp_association *asoc;
3471
3472 if (optlen != sizeof(struct sctp_assoc_value))
3473 return -EINVAL;
3474 if (copy_from_user(&params, optval, optlen))
3475 return -EFAULT;
3476
3477 sp = sctp_sk(sk);
3478
3479 if (params.assoc_id != 0) {
3480 asoc = sctp_id2assoc(sk, params.assoc_id);
3481 if (!asoc)
3482 return -EINVAL;
3483 asoc->default_rcv_context = params.assoc_value;
3484 } else {
3485 sp->default_rcv_context = params.assoc_value;
3486 }
3487
3488 return 0;
3489}
3490
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003491/*
3492 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3493 *
3494 * This options will at a minimum specify if the implementation is doing
3495 * fragmented interleave. Fragmented interleave, for a one to many
3496 * socket, is when subsequent calls to receive a message may return
3497 * parts of messages from different associations. Some implementations
3498 * may allow you to turn this value on or off. If so, when turned off,
3499 * no fragment interleave will occur (which will cause a head of line
3500 * blocking amongst multiple associations sharing the same one to many
3501 * socket). When this option is turned on, then each receive call may
3502 * come from a different association (thus the user must receive data
3503 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3504 * association each receive belongs to.
3505 *
3506 * This option takes a boolean value. A non-zero value indicates that
3507 * fragmented interleave is on. A value of zero indicates that
3508 * fragmented interleave is off.
3509 *
3510 * Note that it is important that an implementation that allows this
3511 * option to be turned on, have it off by default. Otherwise an unaware
3512 * application using the one to many model may become confused and act
3513 * incorrectly.
3514 */
3515static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3516 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003517 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003518{
3519 int val;
3520
3521 if (optlen != sizeof(int))
3522 return -EINVAL;
3523 if (get_user(val, (int __user *)optval))
3524 return -EFAULT;
3525
Xin Long772a5862017-12-08 21:03:58 +08003526 sctp_sk(sk)->frag_interleave = !!val;
3527
3528 if (!sctp_sk(sk)->frag_interleave)
3529 sctp_sk(sk)->strm_interleave = 0;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003530
3531 return 0;
3532}
3533
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003534/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003535 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003536 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003537 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003538 * This option will set or get the SCTP partial delivery point. This
3539 * point is the size of a message where the partial delivery API will be
3540 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003541 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003542 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003543 * point. Note also that the call will fail if the user attempts to set
3544 * this value larger than the socket receive buffer size.
3545 *
3546 * Note that any single message having a length smaller than or equal to
3547 * the SCTP partial delivery point will be delivered in one single read
3548 * call as long as the user provided buffer is large enough to hold the
3549 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003550 */
3551static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3552 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003553 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003554{
3555 u32 val;
3556
3557 if (optlen != sizeof(u32))
3558 return -EINVAL;
3559 if (get_user(val, (int __user *)optval))
3560 return -EFAULT;
3561
Wei Yongjun8510b932008-12-25 16:59:03 -08003562 /* Note: We double the receive buffer from what the user sets
3563 * it to be, also initial rwnd is based on rcvbuf/2.
3564 */
3565 if (val > (sk->sk_rcvbuf >> 1))
3566 return -EINVAL;
3567
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003568 sctp_sk(sk)->pd_point = val;
3569
3570 return 0; /* is this the right error code? */
3571}
3572
Vlad Yasevich70331572007-03-23 11:34:36 -07003573/*
3574 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3575 *
3576 * This option will allow a user to change the maximum burst of packets
3577 * that can be emitted by this association. Note that the default value
3578 * is 4, and some implementations may restrict this setting so that it
3579 * can only be lowered.
3580 *
3581 * NOTE: This text doesn't seem right. Do this on a socket basis with
3582 * future associations inheriting the socket value.
3583 */
3584static int sctp_setsockopt_maxburst(struct sock *sk,
3585 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003586 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003587{
Neil Horman219b99a2008-03-05 13:44:46 -08003588 struct sctp_assoc_value params;
3589 struct sctp_sock *sp;
3590 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003591 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003592 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003593
Neil Horman219b99a2008-03-05 13:44:46 -08003594 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003595 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003596 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003597 "Use of int in max_burst socket option deprecated.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003598 "Use struct sctp_assoc_value instead\n",
3599 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08003600 if (copy_from_user(&val, optval, optlen))
3601 return -EFAULT;
3602 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3603 if (copy_from_user(&params, optval, optlen))
3604 return -EFAULT;
3605 val = params.assoc_value;
3606 assoc_id = params.assoc_id;
3607 } else
3608 return -EINVAL;
3609
3610 sp = sctp_sk(sk);
3611
3612 if (assoc_id != 0) {
3613 asoc = sctp_id2assoc(sk, assoc_id);
3614 if (!asoc)
3615 return -EINVAL;
3616 asoc->max_burst = val;
3617 } else
3618 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003619
3620 return 0;
3621}
3622
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003623/*
3624 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3625 *
3626 * This set option adds a chunk type that the user is requesting to be
3627 * received only in an authenticated way. Changes to the list of chunks
3628 * will only effect future associations on the socket.
3629 */
3630static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003631 char __user *optval,
3632 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003633{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003634 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003635 struct sctp_authchunk val;
3636
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003637 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003638 return -EACCES;
3639
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003640 if (optlen != sizeof(struct sctp_authchunk))
3641 return -EINVAL;
3642 if (copy_from_user(&val, optval, optlen))
3643 return -EFAULT;
3644
3645 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003646 case SCTP_CID_INIT:
3647 case SCTP_CID_INIT_ACK:
3648 case SCTP_CID_SHUTDOWN_COMPLETE:
3649 case SCTP_CID_AUTH:
3650 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003651 }
3652
3653 /* add this chunk id to the endpoint */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003654 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003655}
3656
3657/*
3658 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3659 *
3660 * This option gets or sets the list of HMAC algorithms that the local
3661 * endpoint requires the peer to use.
3662 */
3663static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003664 char __user *optval,
3665 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003666{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003667 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003668 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003669 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003670 int err;
3671
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003672 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003673 return -EACCES;
3674
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003675 if (optlen < sizeof(struct sctp_hmacalgo))
3676 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003677 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3678 SCTP_AUTH_NUM_HMACS * sizeof(u16));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003679
wangweidongcb3f8372013-12-23 12:16:50 +08003680 hmacs = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003681 if (IS_ERR(hmacs))
3682 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003683
Vlad Yasevichd9724052008-08-27 16:09:49 -07003684 idents = hmacs->shmac_num_idents;
3685 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3686 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003687 err = -EINVAL;
3688 goto out;
3689 }
3690
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003691 err = sctp_auth_ep_set_hmacs(ep, hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003692out:
3693 kfree(hmacs);
3694 return err;
3695}
3696
3697/*
3698 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3699 *
3700 * This option will set a shared secret key which is used to build an
3701 * association shared key.
3702 */
3703static int sctp_setsockopt_auth_key(struct sock *sk,
3704 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003705 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003706{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003707 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003708 struct sctp_authkey *authkey;
3709 struct sctp_association *asoc;
3710 int ret;
3711
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003712 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003713 return -EACCES;
3714
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003715 if (optlen <= sizeof(struct sctp_authkey))
3716 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003717 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3718 * this.
3719 */
3720 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3721 sizeof(struct sctp_authkey));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003722
wangweidongcb3f8372013-12-23 12:16:50 +08003723 authkey = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003724 if (IS_ERR(authkey))
3725 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003726
Vlad Yasevich328fc472008-08-27 16:08:54 -07003727 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003728 ret = -EINVAL;
3729 goto out;
3730 }
3731
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003732 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3733 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3734 ret = -EINVAL;
3735 goto out;
3736 }
3737
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003738 ret = sctp_auth_set_key(ep, asoc, authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003739out:
Daniel Borkmann6ba542a2013-02-08 03:04:34 +00003740 kzfree(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003741 return ret;
3742}
3743
3744/*
3745 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3746 *
3747 * This option will get or set the active shared key to be used to build
3748 * the association shared key.
3749 */
3750static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003751 char __user *optval,
3752 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003753{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003754 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003755 struct sctp_authkeyid val;
3756 struct sctp_association *asoc;
3757
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003758 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003759 return -EACCES;
3760
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003761 if (optlen != sizeof(struct sctp_authkeyid))
3762 return -EINVAL;
3763 if (copy_from_user(&val, optval, optlen))
3764 return -EFAULT;
3765
3766 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3767 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3768 return -EINVAL;
3769
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003770 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003771}
3772
3773/*
3774 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3775 *
3776 * This set option will delete a shared secret key from use.
3777 */
3778static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003779 char __user *optval,
3780 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003781{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003782 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003783 struct sctp_authkeyid val;
3784 struct sctp_association *asoc;
3785
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003786 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003787 return -EACCES;
3788
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003789 if (optlen != sizeof(struct sctp_authkeyid))
3790 return -EINVAL;
3791 if (copy_from_user(&val, optval, optlen))
3792 return -EFAULT;
3793
3794 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3795 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3796 return -EINVAL;
3797
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003798 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003799
3800}
3801
Michio Honda7dc04d72011-04-26 20:16:31 +09003802/*
Xin Long601590e2018-03-14 19:05:32 +08003803 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
3804 *
3805 * This set option will deactivate a shared secret key.
3806 */
3807static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
3808 unsigned int optlen)
3809{
3810 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3811 struct sctp_authkeyid val;
3812 struct sctp_association *asoc;
3813
3814 if (!ep->auth_enable)
3815 return -EACCES;
3816
3817 if (optlen != sizeof(struct sctp_authkeyid))
3818 return -EINVAL;
3819 if (copy_from_user(&val, optval, optlen))
3820 return -EFAULT;
3821
3822 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3823 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3824 return -EINVAL;
3825
3826 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3827}
3828
3829/*
Michio Honda7dc04d72011-04-26 20:16:31 +09003830 * 8.1.23 SCTP_AUTO_ASCONF
3831 *
3832 * This option will enable or disable the use of the automatic generation of
3833 * ASCONF chunks to add and delete addresses to an existing association. Note
3834 * that this option has two caveats namely: a) it only affects sockets that
3835 * are bound to all addresses available to the SCTP stack, and b) the system
3836 * administrator may have an overriding control that turns the ASCONF feature
3837 * off no matter what setting the socket option may have.
3838 * This option expects an integer boolean flag, where a non-zero value turns on
3839 * the option, and a zero value turns off the option.
3840 * Note. In this implementation, socket operation overrides default parameter
3841 * being set by sysctl as well as FreeBSD implementation
3842 */
3843static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3844 unsigned int optlen)
3845{
3846 int val;
3847 struct sctp_sock *sp = sctp_sk(sk);
3848
3849 if (optlen < sizeof(int))
3850 return -EINVAL;
3851 if (get_user(val, (int __user *)optval))
3852 return -EFAULT;
3853 if (!sctp_is_ep_boundall(sk) && val)
3854 return -EINVAL;
3855 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3856 return 0;
3857
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003858 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003859 if (val == 0 && sp->do_auto_asconf) {
3860 list_del(&sp->auto_asconf_list);
3861 sp->do_auto_asconf = 0;
3862 } else if (val && !sp->do_auto_asconf) {
3863 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00003864 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09003865 sp->do_auto_asconf = 1;
3866 }
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003867 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003868 return 0;
3869}
3870
Neil Horman5aa93bc2012-07-21 07:56:07 +00003871/*
3872 * SCTP_PEER_ADDR_THLDS
3873 *
3874 * This option allows us to alter the partially failed threshold for one or all
3875 * transports in an association. See Section 6.1 of:
3876 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3877 */
3878static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3879 char __user *optval,
3880 unsigned int optlen)
3881{
3882 struct sctp_paddrthlds val;
3883 struct sctp_transport *trans;
3884 struct sctp_association *asoc;
3885
3886 if (optlen < sizeof(struct sctp_paddrthlds))
3887 return -EINVAL;
3888 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3889 sizeof(struct sctp_paddrthlds)))
3890 return -EFAULT;
3891
3892
3893 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3894 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3895 if (!asoc)
3896 return -ENOENT;
3897 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3898 transports) {
3899 if (val.spt_pathmaxrxt)
3900 trans->pathmaxrxt = val.spt_pathmaxrxt;
3901 trans->pf_retrans = val.spt_pathpfthld;
3902 }
3903
3904 if (val.spt_pathmaxrxt)
3905 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3906 asoc->pf_retrans = val.spt_pathpfthld;
3907 } else {
3908 trans = sctp_addr_id2transport(sk, &val.spt_address,
3909 val.spt_assoc_id);
3910 if (!trans)
3911 return -ENOENT;
3912
3913 if (val.spt_pathmaxrxt)
3914 trans->pathmaxrxt = val.spt_pathmaxrxt;
3915 trans->pf_retrans = val.spt_pathpfthld;
3916 }
3917
3918 return 0;
3919}
3920
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02003921static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3922 char __user *optval,
3923 unsigned int optlen)
3924{
3925 int val;
3926
3927 if (optlen < sizeof(int))
3928 return -EINVAL;
3929 if (get_user(val, (int __user *) optval))
3930 return -EFAULT;
3931
3932 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3933
3934 return 0;
3935}
3936
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02003937static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3938 char __user *optval,
3939 unsigned int optlen)
3940{
3941 int val;
3942
3943 if (optlen < sizeof(int))
3944 return -EINVAL;
3945 if (get_user(val, (int __user *) optval))
3946 return -EFAULT;
3947
3948 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3949
3950 return 0;
3951}
3952
Xin Long28aa4c22016-07-09 19:47:40 +08003953static int sctp_setsockopt_pr_supported(struct sock *sk,
3954 char __user *optval,
3955 unsigned int optlen)
3956{
3957 struct sctp_assoc_value params;
3958 struct sctp_association *asoc;
3959 int retval = -EINVAL;
3960
3961 if (optlen != sizeof(params))
3962 goto out;
3963
3964 if (copy_from_user(&params, optval, optlen)) {
3965 retval = -EFAULT;
3966 goto out;
3967 }
3968
3969 asoc = sctp_id2assoc(sk, params.assoc_id);
3970 if (asoc) {
3971 asoc->prsctp_enable = !!params.assoc_value;
3972 } else if (!params.assoc_id) {
3973 struct sctp_sock *sp = sctp_sk(sk);
3974
3975 sp->ep->prsctp_enable = !!params.assoc_value;
3976 } else {
3977 goto out;
3978 }
3979
3980 retval = 0;
3981
3982out:
3983 return retval;
3984}
3985
Xin Longf959fb42016-07-09 19:47:41 +08003986static int sctp_setsockopt_default_prinfo(struct sock *sk,
3987 char __user *optval,
3988 unsigned int optlen)
3989{
3990 struct sctp_default_prinfo info;
3991 struct sctp_association *asoc;
3992 int retval = -EINVAL;
3993
3994 if (optlen != sizeof(info))
3995 goto out;
3996
3997 if (copy_from_user(&info, optval, sizeof(info))) {
3998 retval = -EFAULT;
3999 goto out;
4000 }
4001
4002 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
4003 goto out;
4004
4005 if (info.pr_policy == SCTP_PR_SCTP_NONE)
4006 info.pr_value = 0;
4007
4008 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
4009 if (asoc) {
4010 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4011 asoc->default_timetolive = info.pr_value;
4012 } else if (!info.pr_assoc_id) {
4013 struct sctp_sock *sp = sctp_sk(sk);
4014
4015 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
4016 sp->default_timetolive = info.pr_value;
4017 } else {
4018 goto out;
4019 }
4020
4021 retval = 0;
4022
4023out:
4024 return retval;
4025}
4026
Xin Longc0d8bab2017-03-10 12:11:12 +08004027static int sctp_setsockopt_reconfig_supported(struct sock *sk,
4028 char __user *optval,
4029 unsigned int optlen)
4030{
4031 struct sctp_assoc_value params;
4032 struct sctp_association *asoc;
4033 int retval = -EINVAL;
4034
4035 if (optlen != sizeof(params))
4036 goto out;
4037
4038 if (copy_from_user(&params, optval, optlen)) {
4039 retval = -EFAULT;
4040 goto out;
4041 }
4042
4043 asoc = sctp_id2assoc(sk, params.assoc_id);
4044 if (asoc) {
4045 asoc->reconf_enable = !!params.assoc_value;
4046 } else if (!params.assoc_id) {
4047 struct sctp_sock *sp = sctp_sk(sk);
4048
4049 sp->ep->reconf_enable = !!params.assoc_value;
4050 } else {
4051 goto out;
4052 }
4053
4054 retval = 0;
4055
4056out:
4057 return retval;
4058}
4059
Xin Long9fb657a2017-01-18 00:44:46 +08004060static int sctp_setsockopt_enable_strreset(struct sock *sk,
4061 char __user *optval,
4062 unsigned int optlen)
4063{
4064 struct sctp_assoc_value params;
4065 struct sctp_association *asoc;
4066 int retval = -EINVAL;
4067
4068 if (optlen != sizeof(params))
4069 goto out;
4070
4071 if (copy_from_user(&params, optval, optlen)) {
4072 retval = -EFAULT;
4073 goto out;
4074 }
4075
4076 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
4077 goto out;
4078
4079 asoc = sctp_id2assoc(sk, params.assoc_id);
4080 if (asoc) {
4081 asoc->strreset_enable = params.assoc_value;
4082 } else if (!params.assoc_id) {
4083 struct sctp_sock *sp = sctp_sk(sk);
4084
4085 sp->ep->strreset_enable = params.assoc_value;
4086 } else {
4087 goto out;
4088 }
4089
4090 retval = 0;
4091
4092out:
4093 return retval;
4094}
4095
Xin Long7f9d68a2017-01-18 00:44:47 +08004096static int sctp_setsockopt_reset_streams(struct sock *sk,
4097 char __user *optval,
4098 unsigned int optlen)
4099{
4100 struct sctp_reset_streams *params;
4101 struct sctp_association *asoc;
4102 int retval = -EINVAL;
4103
Xin Long2342b8d2017-12-10 15:40:51 +08004104 if (optlen < sizeof(*params))
Xin Long7f9d68a2017-01-18 00:44:47 +08004105 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02004106 /* srs_number_streams is u16, so optlen can't be bigger than this. */
4107 optlen = min_t(unsigned int, optlen, USHRT_MAX +
4108 sizeof(__u16) * sizeof(*params));
Xin Long7f9d68a2017-01-18 00:44:47 +08004109
4110 params = memdup_user(optval, optlen);
4111 if (IS_ERR(params))
4112 return PTR_ERR(params);
4113
Xin Long2342b8d2017-12-10 15:40:51 +08004114 if (params->srs_number_streams * sizeof(__u16) >
4115 optlen - sizeof(*params))
4116 goto out;
4117
Xin Long7f9d68a2017-01-18 00:44:47 +08004118 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4119 if (!asoc)
4120 goto out;
4121
4122 retval = sctp_send_reset_streams(asoc, params);
4123
4124out:
4125 kfree(params);
4126 return retval;
4127}
4128
Xin Longa92ce1a2017-02-09 01:18:18 +08004129static int sctp_setsockopt_reset_assoc(struct sock *sk,
4130 char __user *optval,
4131 unsigned int optlen)
4132{
4133 struct sctp_association *asoc;
4134 sctp_assoc_t associd;
4135 int retval = -EINVAL;
4136
4137 if (optlen != sizeof(associd))
4138 goto out;
4139
4140 if (copy_from_user(&associd, optval, optlen)) {
4141 retval = -EFAULT;
4142 goto out;
4143 }
4144
4145 asoc = sctp_id2assoc(sk, associd);
4146 if (!asoc)
4147 goto out;
4148
4149 retval = sctp_send_reset_assoc(asoc);
4150
4151out:
4152 return retval;
4153}
4154
Xin Long242bd2d2017-02-09 01:18:20 +08004155static int sctp_setsockopt_add_streams(struct sock *sk,
4156 char __user *optval,
4157 unsigned int optlen)
4158{
4159 struct sctp_association *asoc;
4160 struct sctp_add_streams params;
4161 int retval = -EINVAL;
4162
4163 if (optlen != sizeof(params))
4164 goto out;
4165
4166 if (copy_from_user(&params, optval, optlen)) {
4167 retval = -EFAULT;
4168 goto out;
4169 }
4170
4171 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
4172 if (!asoc)
4173 goto out;
4174
4175 retval = sctp_send_add_streams(asoc, &params);
4176
4177out:
4178 return retval;
4179}
4180
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004181static int sctp_setsockopt_scheduler(struct sock *sk,
4182 char __user *optval,
4183 unsigned int optlen)
4184{
4185 struct sctp_association *asoc;
4186 struct sctp_assoc_value params;
4187 int retval = -EINVAL;
4188
4189 if (optlen < sizeof(params))
4190 goto out;
4191
4192 optlen = sizeof(params);
4193 if (copy_from_user(&params, optval, optlen)) {
4194 retval = -EFAULT;
4195 goto out;
4196 }
4197
4198 if (params.assoc_value > SCTP_SS_MAX)
4199 goto out;
4200
4201 asoc = sctp_id2assoc(sk, params.assoc_id);
4202 if (!asoc)
4203 goto out;
4204
4205 retval = sctp_sched_set_sched(asoc, params.assoc_value);
4206
4207out:
4208 return retval;
4209}
4210
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004211static int sctp_setsockopt_scheduler_value(struct sock *sk,
4212 char __user *optval,
4213 unsigned int optlen)
4214{
4215 struct sctp_association *asoc;
4216 struct sctp_stream_value params;
4217 int retval = -EINVAL;
4218
4219 if (optlen < sizeof(params))
4220 goto out;
4221
4222 optlen = sizeof(params);
4223 if (copy_from_user(&params, optval, optlen)) {
4224 retval = -EFAULT;
4225 goto out;
4226 }
4227
4228 asoc = sctp_id2assoc(sk, params.assoc_id);
4229 if (!asoc)
4230 goto out;
4231
4232 retval = sctp_sched_set_value(asoc, params.stream_id,
4233 params.stream_value, GFP_KERNEL);
4234
4235out:
4236 return retval;
4237}
4238
Xin Long772a5862017-12-08 21:03:58 +08004239static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4240 char __user *optval,
4241 unsigned int optlen)
4242{
4243 struct sctp_sock *sp = sctp_sk(sk);
4244 struct net *net = sock_net(sk);
4245 struct sctp_assoc_value params;
4246 int retval = -EINVAL;
4247
4248 if (optlen < sizeof(params))
4249 goto out;
4250
4251 optlen = sizeof(params);
4252 if (copy_from_user(&params, optval, optlen)) {
4253 retval = -EFAULT;
4254 goto out;
4255 }
4256
4257 if (params.assoc_id)
4258 goto out;
4259
4260 if (!net->sctp.intl_enable || !sp->frag_interleave) {
4261 retval = -EPERM;
4262 goto out;
4263 }
4264
4265 sp->strm_interleave = !!params.assoc_value;
4266
4267 retval = 0;
4268
4269out:
4270 return retval;
4271}
4272
Xin Longb0e9a2f2018-06-28 15:31:00 +08004273static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval,
4274 unsigned int optlen)
4275{
4276 int val;
4277
4278 if (!sctp_style(sk, TCP))
4279 return -EOPNOTSUPP;
4280
4281 if (sctp_sk(sk)->ep->base.bind_addr.port)
4282 return -EFAULT;
4283
4284 if (optlen < sizeof(int))
4285 return -EINVAL;
4286
4287 if (get_user(val, (int __user *)optval))
4288 return -EFAULT;
4289
4290 sctp_sk(sk)->reuse = !!val;
4291
4292 return 0;
4293}
4294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295/* API 6.2 setsockopt(), getsockopt()
4296 *
4297 * Applications use setsockopt() and getsockopt() to set or retrieve
4298 * socket options. Socket options are used to change the default
4299 * behavior of sockets calls. They are described in Section 7.
4300 *
4301 * The syntax is:
4302 *
4303 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4304 * int __user *optlen);
4305 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4306 * int optlen);
4307 *
4308 * sd - the socket descript.
4309 * level - set to IPPROTO_SCTP for all SCTP options.
4310 * optname - the option name.
4311 * optval - the buffer to store the value of the option.
4312 * optlen - the size of the buffer.
4313 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004314static int sctp_setsockopt(struct sock *sk, int level, int optname,
4315 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316{
4317 int retval = 0;
4318
Daniel Borkmannbb333812013-06-28 19:49:40 +02004319 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
4321 /* I can hardly begin to describe how wrong this is. This is
4322 * so broken as to be worse than useless. The API draft
4323 * REALLY is NOT helpful here... I am not convinced that the
4324 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4325 * are at all well-founded.
4326 */
4327 if (level != SOL_SCTP) {
4328 struct sctp_af *af = sctp_sk(sk)->pf->af;
4329 retval = af->setsockopt(sk, level, optname, optval, optlen);
4330 goto out_nounlock;
4331 }
4332
wangweidong048ed4b2014-01-21 15:44:11 +08004333 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
4335 switch (optname) {
4336 case SCTP_SOCKOPT_BINDX_ADD:
4337 /* 'optlen' is the size of the addresses buffer. */
4338 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4339 optlen, SCTP_BINDX_ADD_ADDR);
4340 break;
4341
4342 case SCTP_SOCKOPT_BINDX_REM:
4343 /* 'optlen' is the size of the addresses buffer. */
4344 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4345 optlen, SCTP_BINDX_REM_ADDR);
4346 break;
4347
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004348 case SCTP_SOCKOPT_CONNECTX_OLD:
4349 /* 'optlen' is the size of the addresses buffer. */
4350 retval = sctp_setsockopt_connectx_old(sk,
4351 (struct sockaddr __user *)optval,
4352 optlen);
4353 break;
4354
Frank Filz3f7a87d2005-06-20 13:14:57 -07004355 case SCTP_SOCKOPT_CONNECTX:
4356 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004357 retval = sctp_setsockopt_connectx(sk,
4358 (struct sockaddr __user *)optval,
4359 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004360 break;
4361
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 case SCTP_DISABLE_FRAGMENTS:
4363 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4364 break;
4365
4366 case SCTP_EVENTS:
4367 retval = sctp_setsockopt_events(sk, optval, optlen);
4368 break;
4369
4370 case SCTP_AUTOCLOSE:
4371 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4372 break;
4373
4374 case SCTP_PEER_ADDR_PARAMS:
4375 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4376 break;
4377
Shan Wei4580ccc2011-01-18 22:39:00 +00004378 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07004379 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08004380 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004381 case SCTP_PARTIAL_DELIVERY_POINT:
4382 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4383 break;
Frank Filz77086102005-12-22 11:37:30 -08004384
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 case SCTP_INITMSG:
4386 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4387 break;
4388 case SCTP_DEFAULT_SEND_PARAM:
4389 retval = sctp_setsockopt_default_send_param(sk, optval,
4390 optlen);
4391 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02004392 case SCTP_DEFAULT_SNDINFO:
4393 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 case SCTP_PRIMARY_ADDR:
4396 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4397 break;
4398 case SCTP_SET_PEER_PRIMARY_ADDR:
4399 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4400 break;
4401 case SCTP_NODELAY:
4402 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4403 break;
4404 case SCTP_RTOINFO:
4405 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4406 break;
4407 case SCTP_ASSOCINFO:
4408 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4409 break;
4410 case SCTP_I_WANT_MAPPED_V4_ADDR:
4411 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4412 break;
4413 case SCTP_MAXSEG:
4414 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4415 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004416 case SCTP_ADAPTATION_LAYER:
4417 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004419 case SCTP_CONTEXT:
4420 retval = sctp_setsockopt_context(sk, optval, optlen);
4421 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004422 case SCTP_FRAGMENT_INTERLEAVE:
4423 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4424 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07004425 case SCTP_MAX_BURST:
4426 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4427 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004428 case SCTP_AUTH_CHUNK:
4429 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4430 break;
4431 case SCTP_HMAC_IDENT:
4432 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4433 break;
4434 case SCTP_AUTH_KEY:
4435 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4436 break;
4437 case SCTP_AUTH_ACTIVE_KEY:
4438 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4439 break;
4440 case SCTP_AUTH_DELETE_KEY:
4441 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4442 break;
Xin Long601590e2018-03-14 19:05:32 +08004443 case SCTP_AUTH_DEACTIVATE_KEY:
4444 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
4445 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09004446 case SCTP_AUTO_ASCONF:
4447 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4448 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00004449 case SCTP_PEER_ADDR_THLDS:
4450 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4451 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004452 case SCTP_RECVRCVINFO:
4453 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4454 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004455 case SCTP_RECVNXTINFO:
4456 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4457 break;
Xin Long28aa4c22016-07-09 19:47:40 +08004458 case SCTP_PR_SUPPORTED:
4459 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4460 break;
Xin Longf959fb42016-07-09 19:47:41 +08004461 case SCTP_DEFAULT_PRINFO:
4462 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4463 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08004464 case SCTP_RECONFIG_SUPPORTED:
4465 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4466 break;
Xin Long9fb657a2017-01-18 00:44:46 +08004467 case SCTP_ENABLE_STREAM_RESET:
4468 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4469 break;
Xin Long7f9d68a2017-01-18 00:44:47 +08004470 case SCTP_RESET_STREAMS:
4471 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4472 break;
Xin Longa92ce1a2017-02-09 01:18:18 +08004473 case SCTP_RESET_ASSOC:
4474 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4475 break;
Xin Long242bd2d2017-02-09 01:18:20 +08004476 case SCTP_ADD_STREAMS:
4477 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4478 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004479 case SCTP_STREAM_SCHEDULER:
4480 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4481 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004482 case SCTP_STREAM_SCHEDULER_VALUE:
4483 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4484 break;
Xin Long772a5862017-12-08 21:03:58 +08004485 case SCTP_INTERLEAVING_SUPPORTED:
4486 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4487 optlen);
4488 break;
Xin Longb0e9a2f2018-06-28 15:31:00 +08004489 case SCTP_REUSE_PORT:
4490 retval = sctp_setsockopt_reuse_port(sk, optval, optlen);
4491 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 default:
4493 retval = -ENOPROTOOPT;
4494 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
wangweidong048ed4b2014-01-21 15:44:11 +08004497 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
4499out_nounlock:
4500 return retval;
4501}
4502
4503/* API 3.1.6 connect() - UDP Style Syntax
4504 *
4505 * An application may use the connect() call in the UDP model to initiate an
4506 * association without sending data.
4507 *
4508 * The syntax is:
4509 *
4510 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4511 *
4512 * sd: the socket descriptor to have a new association added to.
4513 *
4514 * nam: the address structure (either struct sockaddr_in or struct
4515 * sockaddr_in6 defined in RFC2553 [7]).
4516 *
4517 * len: the size of the address.
4518 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004519static int sctp_connect(struct sock *sk, struct sockaddr *addr,
Xin Long644fbde2018-05-20 16:39:10 +08004520 int addr_len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521{
Xin Long644fbde2018-05-20 16:39:10 +08004522 struct inet_sock *inet = inet_sk(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004523 struct sctp_af *af;
Xin Long644fbde2018-05-20 16:39:10 +08004524 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
wangweidong048ed4b2014-01-21 15:44:11 +08004526 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
Daniel Borkmannbb333812013-06-28 19:49:40 +02004528 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4529 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
Xin Long644fbde2018-05-20 16:39:10 +08004531 /* We may need to bind the socket. */
4532 if (!inet->inet_num) {
4533 if (sk->sk_prot->get_port(sk, 0)) {
4534 release_sock(sk);
4535 return -EAGAIN;
4536 }
4537 inet->inet_sport = htons(inet->inet_num);
4538 }
4539
Frank Filz3f7a87d2005-06-20 13:14:57 -07004540 /* Validate addr_len before calling common connect/connectx routine. */
4541 af = sctp_get_af_specific(addr->sa_family);
4542 if (!af || addr_len < af->sockaddr_len) {
4543 err = -EINVAL;
4544 } else {
4545 /* Pass correct addr len to common routine (so it knows there
4546 * is only one address being passed.
4547 */
Xin Long644fbde2018-05-20 16:39:10 +08004548 err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 }
4550
wangweidong048ed4b2014-01-21 15:44:11 +08004551 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 return err;
4553}
4554
Xin Long644fbde2018-05-20 16:39:10 +08004555int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
4556 int addr_len, int flags)
4557{
4558 if (addr_len < sizeof(uaddr->sa_family))
4559 return -EINVAL;
4560
4561 if (uaddr->sa_family == AF_UNSPEC)
4562 return -EOPNOTSUPP;
4563
4564 return sctp_connect(sock->sk, uaddr, addr_len, flags);
4565}
4566
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567/* FIXME: Write comments. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004568static int sctp_disconnect(struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569{
4570 return -EOPNOTSUPP; /* STUB */
4571}
4572
4573/* 4.1.4 accept() - TCP Style Syntax
4574 *
4575 * Applications use accept() call to remove an established SCTP
4576 * association from the accept queue of the endpoint. A new socket
4577 * descriptor will be returned from accept() to represent the newly
4578 * formed association.
4579 */
David Howellscdfbabf2017-03-09 08:09:05 +00004580static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581{
4582 struct sctp_sock *sp;
4583 struct sctp_endpoint *ep;
4584 struct sock *newsk = NULL;
4585 struct sctp_association *asoc;
4586 long timeo;
4587 int error = 0;
4588
wangweidong048ed4b2014-01-21 15:44:11 +08004589 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
4591 sp = sctp_sk(sk);
4592 ep = sp->ep;
4593
4594 if (!sctp_style(sk, TCP)) {
4595 error = -EOPNOTSUPP;
4596 goto out;
4597 }
4598
4599 if (!sctp_sstate(sk, LISTENING)) {
4600 error = -EINVAL;
4601 goto out;
4602 }
4603
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07004604 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
4606 error = sctp_wait_for_accept(sk, timeo);
4607 if (error)
4608 goto out;
4609
4610 /* We treat the list of associations on the endpoint as the accept
4611 * queue and pick the first association on the list.
4612 */
4613 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4614
David Howellscdfbabf2017-03-09 08:09:05 +00004615 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 if (!newsk) {
4617 error = -ENOMEM;
4618 goto out;
4619 }
4620
4621 /* Populate the fields of the newsk from the oldsk and migrate the
4622 * asoc to the newsk.
4623 */
4624 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4625
4626out:
wangweidong048ed4b2014-01-21 15:44:11 +08004627 release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004628 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 return newsk;
4630}
4631
4632/* The SCTP ioctl handler. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004633static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004635 int rc = -ENOTCONN;
4636
wangweidong048ed4b2014-01-21 15:44:11 +08004637 lock_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004638
4639 /*
4640 * SEQPACKET-style sockets in LISTENING state are valid, for
4641 * SCTP, so only discard TCP-style sockets in LISTENING state.
4642 */
4643 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4644 goto out;
4645
4646 switch (cmd) {
4647 case SIOCINQ: {
4648 struct sk_buff *skb;
4649 unsigned int amount = 0;
4650
4651 skb = skb_peek(&sk->sk_receive_queue);
4652 if (skb != NULL) {
4653 /*
4654 * We will only return the amount of this packet since
4655 * that is all that will be read.
4656 */
4657 amount = skb->len;
4658 }
4659 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004660 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07004661 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004662 default:
4663 rc = -ENOIOCTLCMD;
4664 break;
4665 }
4666out:
wangweidong048ed4b2014-01-21 15:44:11 +08004667 release_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004668 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669}
4670
4671/* This is the function which gets called during socket creation to
4672 * initialized the SCTP-specific portion of the sock.
4673 * The sock structure should already be zero-filled memory.
4674 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004675static int sctp_init_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004677 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 struct sctp_sock *sp;
4679
Daniel Borkmannbb333812013-06-28 19:49:40 +02004680 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
4682 sp = sctp_sk(sk);
4683
4684 /* Initialize the SCTP per socket area. */
4685 switch (sk->sk_type) {
4686 case SOCK_SEQPACKET:
4687 sp->type = SCTP_SOCKET_UDP;
4688 break;
4689 case SOCK_STREAM:
4690 sp->type = SCTP_SOCKET_TCP;
4691 break;
4692 default:
4693 return -ESOCKTNOSUPPORT;
4694 }
4695
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004696 sk->sk_gso_type = SKB_GSO_SCTP;
4697
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 /* Initialize default send parameters. These parameters can be
4699 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4700 */
4701 sp->default_stream = 0;
4702 sp->default_ppid = 0;
4703 sp->default_flags = 0;
4704 sp->default_context = 0;
4705 sp->default_timetolive = 0;
4706
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004707 sp->default_rcv_context = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004708 sp->max_burst = net->sctp.max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004709
Neil Horman3c681982012-10-24 09:20:03 +00004710 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4711
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 /* Initialize default setup parameters. These parameters
4713 * can be modified with the SCTP_INITMSG socket option or
4714 * overridden by the SCTP_INIT CMSG.
4715 */
4716 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4717 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004718 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4719 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
4721 /* Initialize default RTO related parameters. These parameters can
4722 * be modified for with the SCTP_RTOINFO socket option.
4723 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004724 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4725 sp->rtoinfo.srto_max = net->sctp.rto_max;
4726 sp->rtoinfo.srto_min = net->sctp.rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
4728 /* Initialize default association related parameters. These parameters
4729 * can be modified with the SCTP_ASSOCINFO socket option.
4730 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004731 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 sp->assocparams.sasoc_number_peer_destinations = 0;
4733 sp->assocparams.sasoc_peer_rwnd = 0;
4734 sp->assocparams.sasoc_local_rwnd = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004735 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
4737 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004738 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 */
4740 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4741
4742 /* Default Peer Address Parameters. These defaults can
4743 * be modified via SCTP_PEER_ADDR_PARAMS
4744 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004745 sp->hbinterval = net->sctp.hb_interval;
4746 sp->pathmaxrxt = net->sctp.max_retrans_path;
wangweidong4e2d52b2013-12-23 12:16:54 +08004747 sp->pathmtu = 0; /* allow default discovery */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004748 sp->sackdelay = net->sctp.sack_timeout;
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07004749 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004750 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004751 SPP_PMTUD_ENABLE |
4752 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
4754 /* If enabled no SCTP message fragmentation will be performed.
4755 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4756 */
4757 sp->disable_fragments = 0;
4758
Sridhar Samudrala208edef2006-09-29 17:08:01 -07004759 /* Enable Nagle algorithm by default. */
4760 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004762 sp->recvrcvinfo = 0;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004763 sp->recvnxtinfo = 0;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004764
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 /* Enable by default. */
4766 sp->v4mapped = 1;
4767
4768 /* Auto-close idle associations after the configured
4769 * number of seconds. A value of 0 disables this
4770 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4771 * for UDP-style sockets only.
4772 */
4773 sp->autoclose = 0;
4774
4775 /* User specified fragmentation limit. */
4776 sp->user_frag = 0;
4777
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004778 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
4780 sp->pf = sctp_get_pf_specific(sk->sk_family);
4781
4782 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004783 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004785 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
4787 /* Create a per socket endpoint structure. Even if we
4788 * change the data structure relationships, this may still
4789 * be useful for storing pre-connect address information.
4790 */
Daniel Borkmannc164b832013-06-14 18:24:06 +02004791 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4792 if (!sp->ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 return -ENOMEM;
4794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 sp->hmac = NULL;
4796
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004797 sk->sk_destruct = sctp_destruct_sock;
4798
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08004800
4801 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004802 sk_sockets_allocated_inc(sk);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004803 sock_prot_inuse_add(net, sk->sk_prot, 1);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004804
4805 /* Nothing can fail after this block, otherwise
4806 * sctp_destroy_sock() will be called without addr_wq_lock held
4807 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004808 if (net->sctp.default_auto_asconf) {
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004809 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda9f7d6532011-04-26 19:32:51 +09004810 list_add_tail(&sp->auto_asconf_list,
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004811 &net->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09004812 sp->do_auto_asconf = 1;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004813 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4814 } else {
Michio Honda9f7d6532011-04-26 19:32:51 +09004815 sp->do_auto_asconf = 0;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004816 }
4817
David S. Miller6f756a82008-11-23 17:34:03 -08004818 local_bh_enable();
4819
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 return 0;
4821}
4822
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004823/* Cleanup any SCTP per socket resources. Must be called with
4824 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4825 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004826static void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827{
Michio Honda9f7d6532011-04-26 19:32:51 +09004828 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
Daniel Borkmannbb333812013-06-28 19:49:40 +02004830 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
4832 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09004833 sp = sctp_sk(sk);
Daniel Borkmann1abd1652013-06-06 15:53:47 +02004834 /* This could happen during socket init, thus we bail out
4835 * early, since the rest of the below is not setup either.
4836 */
4837 if (sp->ep == NULL)
4838 return;
4839
Michio Honda9f7d6532011-04-26 19:32:51 +09004840 if (sp->do_auto_asconf) {
4841 sp->do_auto_asconf = 0;
4842 list_del(&sp->auto_asconf_list);
4843 }
4844 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004845 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004846 sk_sockets_allocated_dec(sk);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08004847 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004848 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849}
4850
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004851/* Triggered when there are no references on the socket anymore */
4852static void sctp_destruct_sock(struct sock *sk)
4853{
4854 struct sctp_sock *sp = sctp_sk(sk);
4855
4856 /* Free up the HMAC transform. */
Herbert Xu5821c762016-01-24 21:20:12 +08004857 crypto_free_shash(sp->hmac);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004858
4859 inet_sock_destruct(sk);
4860}
4861
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862/* API 4.1.7 shutdown() - TCP Style Syntax
4863 * int shutdown(int socket, int how);
4864 *
4865 * sd - the socket descriptor of the association to be closed.
4866 * how - Specifies the type of shutdown. The values are
4867 * as follows:
4868 * SHUT_RD
4869 * Disables further receive operations. No SCTP
4870 * protocol action is taken.
4871 * SHUT_WR
4872 * Disables further send operations, and initiates
4873 * the SCTP shutdown sequence.
4874 * SHUT_RDWR
4875 * Disables further send and receive operations
4876 * and initiates the SCTP shutdown sequence.
4877 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004878static void sctp_shutdown(struct sock *sk, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004880 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 struct sctp_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
4883 if (!sctp_style(sk, TCP))
4884 return;
4885
Xin Long5bf35dd2016-11-13 21:44:37 +08004886 ep = sctp_sk(sk)->ep;
4887 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4888 struct sctp_association *asoc;
4889
Yafang Shaocbabf462017-12-20 11:12:54 +08004890 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Xin Long5bf35dd2016-11-13 21:44:37 +08004891 asoc = list_entry(ep->asocs.next,
4892 struct sctp_association, asocs);
4893 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 }
4895}
4896
Xin Long52c52a62016-04-14 15:35:30 +08004897int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4898 struct sctp_info *info)
4899{
4900 struct sctp_transport *prim;
4901 struct list_head *pos;
4902 int mask;
4903
4904 memset(info, 0, sizeof(*info));
4905 if (!asoc) {
4906 struct sctp_sock *sp = sctp_sk(sk);
4907
4908 info->sctpi_s_autoclose = sp->autoclose;
4909 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4910 info->sctpi_s_pd_point = sp->pd_point;
4911 info->sctpi_s_nodelay = sp->nodelay;
4912 info->sctpi_s_disable_fragments = sp->disable_fragments;
4913 info->sctpi_s_v4mapped = sp->v4mapped;
4914 info->sctpi_s_frag_interleave = sp->frag_interleave;
Xin Long40eb90e92016-05-29 17:42:13 +08004915 info->sctpi_s_type = sp->type;
Xin Long52c52a62016-04-14 15:35:30 +08004916
4917 return 0;
4918 }
4919
4920 info->sctpi_tag = asoc->c.my_vtag;
4921 info->sctpi_state = asoc->state;
4922 info->sctpi_rwnd = asoc->a_rwnd;
4923 info->sctpi_unackdata = asoc->unack_data;
4924 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004925 info->sctpi_instrms = asoc->stream.incnt;
4926 info->sctpi_outstrms = asoc->stream.outcnt;
Xin Long52c52a62016-04-14 15:35:30 +08004927 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4928 info->sctpi_inqueue++;
4929 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4930 info->sctpi_outqueue++;
4931 info->sctpi_overall_error = asoc->overall_error_count;
4932 info->sctpi_max_burst = asoc->max_burst;
4933 info->sctpi_maxseg = asoc->frag_point;
4934 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4935 info->sctpi_peer_tag = asoc->c.peer_vtag;
4936
4937 mask = asoc->peer.ecn_capable << 1;
4938 mask = (mask | asoc->peer.ipv4_address) << 1;
4939 mask = (mask | asoc->peer.ipv6_address) << 1;
4940 mask = (mask | asoc->peer.hostname_address) << 1;
4941 mask = (mask | asoc->peer.asconf_capable) << 1;
4942 mask = (mask | asoc->peer.prsctp_capable) << 1;
4943 mask = (mask | asoc->peer.auth_capable);
4944 info->sctpi_peer_capable = mask;
4945 mask = asoc->peer.sack_needed << 1;
4946 mask = (mask | asoc->peer.sack_generation) << 1;
4947 mask = (mask | asoc->peer.zero_window_announced);
4948 info->sctpi_peer_sack = mask;
4949
4950 info->sctpi_isacks = asoc->stats.isacks;
4951 info->sctpi_osacks = asoc->stats.osacks;
4952 info->sctpi_opackets = asoc->stats.opackets;
4953 info->sctpi_ipackets = asoc->stats.ipackets;
4954 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4955 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4956 info->sctpi_idupchunks = asoc->stats.idupchunks;
4957 info->sctpi_gapcnt = asoc->stats.gapcnt;
4958 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4959 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4960 info->sctpi_oodchunks = asoc->stats.oodchunks;
4961 info->sctpi_iodchunks = asoc->stats.iodchunks;
4962 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4963 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4964
4965 prim = asoc->peer.primary_path;
Stefano Brivioee6c88b2017-08-23 13:27:13 +02004966 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
Xin Long52c52a62016-04-14 15:35:30 +08004967 info->sctpi_p_state = prim->state;
4968 info->sctpi_p_cwnd = prim->cwnd;
4969 info->sctpi_p_srtt = prim->srtt;
4970 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4971 info->sctpi_p_hbinterval = prim->hbinterval;
4972 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4973 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4974 info->sctpi_p_ssthresh = prim->ssthresh;
4975 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4976 info->sctpi_p_flight_size = prim->flight_size;
4977 info->sctpi_p_error = prim->error_count;
4978
4979 return 0;
4980}
4981EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4982
Xin Long626d16f2016-04-14 15:35:31 +08004983/* use callback to avoid exporting the core structure */
Tom Herbert97a6ec42017-12-04 10:31:41 -08004984void sctp_transport_walk_start(struct rhashtable_iter *iter)
Xin Long626d16f2016-04-14 15:35:31 +08004985{
Xin Long7fda702f2016-11-15 23:23:11 +08004986 rhltable_walk_enter(&sctp_transport_hashtable, iter);
Xin Long626d16f2016-04-14 15:35:31 +08004987
Tom Herbert97a6ec42017-12-04 10:31:41 -08004988 rhashtable_walk_start(iter);
Xin Long626d16f2016-04-14 15:35:31 +08004989}
4990
4991void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4992{
4993 rhashtable_walk_stop(iter);
4994 rhashtable_walk_exit(iter);
4995}
4996
4997struct sctp_transport *sctp_transport_get_next(struct net *net,
4998 struct rhashtable_iter *iter)
4999{
5000 struct sctp_transport *t;
5001
5002 t = rhashtable_walk_next(iter);
5003 for (; t; t = rhashtable_walk_next(iter)) {
5004 if (IS_ERR(t)) {
5005 if (PTR_ERR(t) == -EAGAIN)
5006 continue;
5007 break;
5008 }
5009
Xin Longbab1be72018-08-27 18:38:31 +08005010 if (!sctp_transport_hold(t))
5011 continue;
5012
Xin Long626d16f2016-04-14 15:35:31 +08005013 if (net_eq(sock_net(t->asoc->base.sk), net) &&
5014 t->asoc->peer.primary_path == t)
5015 break;
Xin Longbab1be72018-08-27 18:38:31 +08005016
5017 sctp_transport_put(t);
Xin Long626d16f2016-04-14 15:35:31 +08005018 }
5019
5020 return t;
5021}
5022
5023struct sctp_transport *sctp_transport_get_idx(struct net *net,
5024 struct rhashtable_iter *iter,
5025 int pos)
5026{
Xin Longbab1be72018-08-27 18:38:31 +08005027 struct sctp_transport *t;
Xin Long626d16f2016-04-14 15:35:31 +08005028
Xin Longbab1be72018-08-27 18:38:31 +08005029 if (!pos)
5030 return SEQ_START_TOKEN;
Xin Long626d16f2016-04-14 15:35:31 +08005031
Xin Longbab1be72018-08-27 18:38:31 +08005032 while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
5033 if (!--pos)
5034 break;
5035 sctp_transport_put(t);
5036 }
5037
5038 return t;
Xin Long626d16f2016-04-14 15:35:31 +08005039}
5040
5041int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
5042 void *p) {
5043 int err = 0;
5044 int hash = 0;
5045 struct sctp_ep_common *epb;
5046 struct sctp_hashbucket *head;
5047
5048 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
5049 hash++, head++) {
Xin Long581409d2017-06-10 14:48:14 +08005050 read_lock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08005051 sctp_for_each_hentry(epb, &head->chain) {
5052 err = cb(sctp_ep(epb), p);
5053 if (err)
5054 break;
5055 }
Xin Long581409d2017-06-10 14:48:14 +08005056 read_unlock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08005057 }
5058
5059 return err;
5060}
5061EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
5062
5063int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
5064 struct net *net,
5065 const union sctp_addr *laddr,
5066 const union sctp_addr *paddr, void *p)
5067{
5068 struct sctp_transport *transport;
Xin Long08abb792016-12-15 23:05:52 +08005069 int err;
Xin Long626d16f2016-04-14 15:35:31 +08005070
5071 rcu_read_lock();
5072 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
Xin Long626d16f2016-04-14 15:35:31 +08005073 rcu_read_unlock();
Xin Long08abb792016-12-15 23:05:52 +08005074 if (!transport)
5075 return -ENOENT;
5076
Xin Long1cceda782016-09-29 02:55:44 +08005077 err = cb(transport, p);
Xin Longcd26da42016-10-31 20:32:31 +08005078 sctp_transport_put(transport);
Xin Long1cceda782016-09-29 02:55:44 +08005079
Xin Long626d16f2016-04-14 15:35:31 +08005080 return err;
5081}
5082EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
5083
5084int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
Xin Longd25adbe2017-09-15 11:02:21 +08005085 int (*cb_done)(struct sctp_transport *, void *),
5086 struct net *net, int *pos, void *p) {
Xin Long626d16f2016-04-14 15:35:31 +08005087 struct rhashtable_iter hti;
Xin Longd25adbe2017-09-15 11:02:21 +08005088 struct sctp_transport *tsp;
5089 int ret;
Xin Long626d16f2016-04-14 15:35:31 +08005090
Xin Longd25adbe2017-09-15 11:02:21 +08005091again:
Xin Longf53d77e2018-01-23 18:22:25 +08005092 ret = 0;
Tom Herbert97a6ec42017-12-04 10:31:41 -08005093 sctp_transport_walk_start(&hti);
Xin Long626d16f2016-04-14 15:35:31 +08005094
Xin Longd25adbe2017-09-15 11:02:21 +08005095 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5096 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
Xin Longd25adbe2017-09-15 11:02:21 +08005097 ret = cb(tsp, p);
5098 if (ret)
Xin Long626d16f2016-04-14 15:35:31 +08005099 break;
Xin Longd25adbe2017-09-15 11:02:21 +08005100 (*pos)++;
5101 sctp_transport_put(tsp);
Xin Long626d16f2016-04-14 15:35:31 +08005102 }
Xin Long626d16f2016-04-14 15:35:31 +08005103 sctp_transport_walk_stop(&hti);
Xin Long53fa1032016-04-14 15:35:35 +08005104
Xin Longd25adbe2017-09-15 11:02:21 +08005105 if (ret) {
5106 if (cb_done && !cb_done(tsp, p)) {
5107 (*pos)++;
5108 sctp_transport_put(tsp);
5109 goto again;
5110 }
5111 sctp_transport_put(tsp);
5112 }
5113
5114 return ret;
Xin Long626d16f2016-04-14 15:35:31 +08005115}
5116EXPORT_SYMBOL_GPL(sctp_for_each_transport);
5117
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118/* 7.2.1 Association Status (SCTP_STATUS)
5119
5120 * Applications can retrieve current status information about an
5121 * association, including association state, peer receiver window size,
5122 * number of unacked data chunks, and number of data chunks pending
5123 * receipt. This information is read-only.
5124 */
5125static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5126 char __user *optval,
5127 int __user *optlen)
5128{
5129 struct sctp_status status;
5130 struct sctp_association *asoc = NULL;
5131 struct sctp_transport *transport;
5132 sctp_assoc_t associd;
5133 int retval = 0;
5134
Neil Horman408f22e2007-06-16 14:03:45 -04005135 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 retval = -EINVAL;
5137 goto out;
5138 }
5139
Neil Horman408f22e2007-06-16 14:03:45 -04005140 len = sizeof(status);
5141 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 retval = -EFAULT;
5143 goto out;
5144 }
5145
5146 associd = status.sstat_assoc_id;
5147 asoc = sctp_id2assoc(sk, associd);
5148 if (!asoc) {
5149 retval = -EINVAL;
5150 goto out;
5151 }
5152
5153 transport = asoc->peer.primary_path;
5154
5155 status.sstat_assoc_id = sctp_assoc2id(asoc);
Daniel Borkmann38ab1fa2014-08-28 15:28:26 +02005156 status.sstat_state = sctp_assoc_to_state(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 status.sstat_rwnd = asoc->peer.rwnd;
5158 status.sstat_unackdata = asoc->unack_data;
5159
5160 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08005161 status.sstat_instrms = asoc->stream.incnt;
5162 status.sstat_outstrms = asoc->stream.outcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 status.sstat_fragmentation_point = asoc->frag_point;
5164 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08005165 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5166 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 /* Map ipv4 address into v4-mapped-on-v6 address. */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005168 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005170 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 status.sstat_primary.spinfo_cwnd = transport->cwnd;
5172 status.sstat_primary.spinfo_srtt = transport->srtt;
5173 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005174 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175
Frank Filz3f7a87d2005-06-20 13:14:57 -07005176 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5177 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5178
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 if (put_user(len, optlen)) {
5180 retval = -EFAULT;
5181 goto out;
5182 }
5183
Daniel Borkmannbb333812013-06-28 19:49:40 +02005184 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5185 __func__, len, status.sstat_state, status.sstat_rwnd,
5186 status.sstat_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187
5188 if (copy_to_user(optval, &status, len)) {
5189 retval = -EFAULT;
5190 goto out;
5191 }
5192
5193out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00005194 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195}
5196
5197
5198/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5199 *
5200 * Applications can retrieve information about a specific peer address
5201 * of an association, including its reachability state, congestion
5202 * window, and retransmission timer values. This information is
5203 * read-only.
5204 */
5205static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5206 char __user *optval,
5207 int __user *optlen)
5208{
5209 struct sctp_paddrinfo pinfo;
5210 struct sctp_transport *transport;
5211 int retval = 0;
5212
Neil Horman408f22e2007-06-16 14:03:45 -04005213 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 retval = -EINVAL;
5215 goto out;
5216 }
5217
Neil Horman408f22e2007-06-16 14:03:45 -04005218 len = sizeof(pinfo);
5219 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 retval = -EFAULT;
5221 goto out;
5222 }
5223
5224 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5225 pinfo.spinfo_assoc_id);
5226 if (!transport)
5227 return -EINVAL;
5228
5229 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005230 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 pinfo.spinfo_cwnd = transport->cwnd;
5232 pinfo.spinfo_srtt = transport->srtt;
5233 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005234 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
Frank Filz3f7a87d2005-06-20 13:14:57 -07005236 if (pinfo.spinfo_state == SCTP_UNKNOWN)
5237 pinfo.spinfo_state = SCTP_ACTIVE;
5238
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 if (put_user(len, optlen)) {
5240 retval = -EFAULT;
5241 goto out;
5242 }
5243
5244 if (copy_to_user(optval, &pinfo, len)) {
5245 retval = -EFAULT;
5246 goto out;
5247 }
5248
5249out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00005250 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251}
5252
5253/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5254 *
5255 * This option is a on/off flag. If enabled no SCTP message
5256 * fragmentation will be performed. Instead if a message being sent
5257 * exceeds the current PMTU size, the message will NOT be sent and
5258 * instead a error will be indicated to the user.
5259 */
5260static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5261 char __user *optval, int __user *optlen)
5262{
5263 int val;
5264
5265 if (len < sizeof(int))
5266 return -EINVAL;
5267
5268 len = sizeof(int);
5269 val = (sctp_sk(sk)->disable_fragments == 1);
5270 if (put_user(len, optlen))
5271 return -EFAULT;
5272 if (copy_to_user(optval, &val, len))
5273 return -EFAULT;
5274 return 0;
5275}
5276
5277/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5278 *
5279 * This socket option is used to specify various notifications and
5280 * ancillary data the user wishes to receive.
5281 */
5282static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5283 int __user *optlen)
5284{
Jiri Slabya4b8e712016-10-21 14:13:24 +02005285 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00005287 if (len > sizeof(struct sctp_event_subscribe))
5288 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04005289 if (put_user(len, optlen))
5290 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
5292 return -EFAULT;
5293 return 0;
5294}
5295
5296/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5297 *
5298 * This socket option is applicable to the UDP-style socket only. When
5299 * set it will cause associations that are idle for more than the
5300 * specified number of seconds to automatically close. An association
5301 * being idle is defined an association that has NOT sent or received
5302 * user data. The special value of '0' indicates that no automatic
5303 * close of any associations should be performed. The option expects an
5304 * integer defining the number of seconds of idle time before an
5305 * association is closed.
5306 */
5307static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5308{
5309 /* Applicable to UDP-style socket only */
5310 if (sctp_style(sk, TCP))
5311 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04005312 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005314 len = sizeof(int);
5315 if (put_user(len, optlen))
5316 return -EFAULT;
David Windsorb2ce04c2017-06-10 22:50:43 -04005317 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 return -EFAULT;
5319 return 0;
5320}
5321
5322/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00005323int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324{
Benjamin Poirier0343c552012-03-08 05:55:58 +00005325 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005326 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 struct socket *sock;
5328 int err = 0;
5329
Xin Longdf80cd92017-10-17 23:26:10 +08005330 /* Do not peel off from one netns to another one. */
5331 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5332 return -EINVAL;
5333
Benjamin Poirier0343c552012-03-08 05:55:58 +00005334 if (!asoc)
5335 return -EINVAL;
5336
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 /* An association cannot be branched off from an already peeled-off
5338 * socket, nor is this supported for tcp style sockets.
5339 */
5340 if (!sctp_style(sk, UDP))
5341 return -EINVAL;
5342
5343 /* Create a new socket. */
5344 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5345 if (err < 0)
5346 return err;
5347
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005348 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005349
5350 /* Make peeled-off sockets more like 1-1 accepted sockets.
Richard Hainesb7e10c22018-02-24 16:18:51 +00005351 * Set the daddr and initialize id to something more random and also
5352 * copy over any ip options.
Vlad Yasevich4f444302006-10-30 18:54:32 -08005353 */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005354 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
Richard Hainesb7e10c22018-02-24 16:18:51 +00005355 sp->pf->copy_ip_options(sk, sock->sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005356
5357 /* Populate the fields of the newsk from the oldsk and migrate the
5358 * asoc to the newsk.
5359 */
5360 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005361
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 *sockp = sock;
5363
5364 return err;
5365}
Benjamin Poirier0343c552012-03-08 05:55:58 +00005366EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005368static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5369 struct file **newfile, unsigned flags)
5370{
5371 struct socket *newsock;
5372 int retval;
5373
5374 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5375 if (retval < 0)
5376 goto out;
5377
5378 /* Map the socket to an unused fd that can be returned to the user. */
5379 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5380 if (retval < 0) {
5381 sock_release(newsock);
5382 goto out;
5383 }
5384
5385 *newfile = sock_alloc_file(newsock, 0, NULL);
5386 if (IS_ERR(*newfile)) {
5387 put_unused_fd(retval);
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005388 retval = PTR_ERR(*newfile);
5389 *newfile = NULL;
5390 return retval;
5391 }
5392
5393 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5394 retval);
5395
5396 peeloff->sd = retval;
5397
5398 if (flags & SOCK_NONBLOCK)
5399 (*newfile)->f_flags |= O_NONBLOCK;
5400out:
5401 return retval;
5402}
5403
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5405{
5406 sctp_peeloff_arg_t peeloff;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005407 struct file *newfile = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
Neil Horman408f22e2007-06-16 14:03:45 -04005410 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005412 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 if (copy_from_user(&peeloff, optval, len))
5414 return -EFAULT;
5415
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005416 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 if (retval < 0)
5418 goto out;
5419
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04005421 if (put_user(len, optlen)) {
5422 fput(newfile);
5423 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04005424 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04005425 }
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005426
5427 if (copy_to_user(optval, &peeloff, len)) {
5428 fput(newfile);
5429 put_unused_fd(retval);
5430 return -EFAULT;
5431 }
5432 fd_install(retval, newfile);
5433out:
5434 return retval;
5435}
5436
5437static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5438 char __user *optval, int __user *optlen)
5439{
5440 sctp_peeloff_flags_arg_t peeloff;
5441 struct file *newfile = NULL;
5442 int retval = 0;
5443
5444 if (len < sizeof(sctp_peeloff_flags_arg_t))
5445 return -EINVAL;
5446 len = sizeof(sctp_peeloff_flags_arg_t);
5447 if (copy_from_user(&peeloff, optval, len))
5448 return -EFAULT;
5449
5450 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5451 &newfile, peeloff.flags);
5452 if (retval < 0)
5453 goto out;
5454
5455 /* Return the fd mapped to the new socket. */
5456 if (put_user(len, optlen)) {
5457 fput(newfile);
5458 put_unused_fd(retval);
5459 return -EFAULT;
5460 }
5461
Al Viro56b31d12012-08-18 00:25:51 -04005462 if (copy_to_user(optval, &peeloff, len)) {
5463 fput(newfile);
5464 put_unused_fd(retval);
5465 return -EFAULT;
5466 }
5467 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468out:
5469 return retval;
5470}
5471
5472/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5473 *
5474 * Applications can enable or disable heartbeats for any peer address of
5475 * an association, modify an address's heartbeat interval, force a
5476 * heartbeat to be sent immediately, and adjust the address's maximum
5477 * number of retransmissions sent before an address is considered
5478 * unreachable. The following structure is used to access and modify an
5479 * address's parameters:
5480 *
5481 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005482 * sctp_assoc_t spp_assoc_id;
5483 * struct sockaddr_storage spp_address;
5484 * uint32_t spp_hbinterval;
5485 * uint16_t spp_pathmaxrxt;
5486 * uint32_t spp_pathmtu;
5487 * uint32_t spp_sackdelay;
5488 * uint32_t spp_flags;
5489 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08005491 * spp_assoc_id - (one-to-many style socket) This is filled in the
5492 * application, and identifies the association for
5493 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 * spp_address - This specifies which address is of interest.
5495 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005496 * in milliseconds. If a value of zero
5497 * is present in this field then no changes are to
5498 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 * spp_pathmaxrxt - This contains the maximum number of
5500 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08005501 * considered unreachable. If a value of zero
5502 * is present in this field then no changes are to
5503 * be made to this parameter.
5504 * spp_pathmtu - When Path MTU discovery is disabled the value
5505 * specified here will be the "fixed" path mtu.
5506 * Note that if the spp_address field is empty
5507 * then all associations on this address will
5508 * have this fixed path mtu set upon them.
5509 *
5510 * spp_sackdelay - When delayed sack is enabled, this value specifies
5511 * the number of milliseconds that sacks will be delayed
5512 * for. This value will apply to all addresses of an
5513 * association if the spp_address field is empty. Note
5514 * also, that if delayed sack is enabled and this
5515 * value is set to 0, no change is made to the last
5516 * recorded delayed sack timer value.
5517 *
5518 * spp_flags - These flags are used to control various features
5519 * on an association. The flag field may contain
5520 * zero or more of the following options.
5521 *
5522 * SPP_HB_ENABLE - Enable heartbeats on the
5523 * specified address. Note that if the address
5524 * field is empty all addresses for the association
5525 * have heartbeats enabled upon them.
5526 *
5527 * SPP_HB_DISABLE - Disable heartbeats on the
5528 * speicifed address. Note that if the address
5529 * field is empty all addresses for the association
5530 * will have their heartbeats disabled. Note also
5531 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5532 * mutually exclusive, only one of these two should
5533 * be specified. Enabling both fields will have
5534 * undetermined results.
5535 *
5536 * SPP_HB_DEMAND - Request a user initiated heartbeat
5537 * to be made immediately.
5538 *
5539 * SPP_PMTUD_ENABLE - This field will enable PMTU
5540 * discovery upon the specified address. Note that
5541 * if the address feild is empty then all addresses
5542 * on the association are effected.
5543 *
5544 * SPP_PMTUD_DISABLE - This field will disable PMTU
5545 * discovery upon the specified address. Note that
5546 * if the address feild is empty then all addresses
5547 * on the association are effected. Not also that
5548 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5549 * exclusive. Enabling both will have undetermined
5550 * results.
5551 *
5552 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5553 * on delayed sack. The time specified in spp_sackdelay
5554 * is used to specify the sack delay for this address. Note
5555 * that if spp_address is empty then all addresses will
5556 * enable delayed sack and take on the sack delay
5557 * value specified in spp_sackdelay.
5558 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5559 * off delayed sack. If the spp_address field is blank then
5560 * delayed sack is disabled for the entire association. Note
5561 * also that this field is mutually exclusive to
5562 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5563 * results.
Xin Long0b0dce72018-07-02 18:21:13 +08005564 *
5565 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
5566 * setting of the IPV6 flow label value. The value is
5567 * contained in the spp_ipv6_flowlabel field.
5568 * Upon retrieval, this flag will be set to indicate that
5569 * the spp_ipv6_flowlabel field has a valid value returned.
5570 * If a specific destination address is set (in the
5571 * spp_address field), then the value returned is that of
5572 * the address. If just an association is specified (and
5573 * no address), then the association's default flow label
5574 * is returned. If neither an association nor a destination
5575 * is specified, then the socket's default flow label is
5576 * returned. For non-IPv6 sockets, this flag will be left
5577 * cleared.
5578 *
5579 * SPP_DSCP: Setting this flag enables the setting of the
5580 * Differentiated Services Code Point (DSCP) value
5581 * associated with either the association or a specific
5582 * address. The value is obtained in the spp_dscp field.
5583 * Upon retrieval, this flag will be set to indicate that
5584 * the spp_dscp field has a valid value returned. If a
5585 * specific destination address is set when called (in the
5586 * spp_address field), then that specific destination
5587 * address's DSCP value is returned. If just an association
5588 * is specified, then the association's default DSCP is
5589 * returned. If neither an association nor a destination is
5590 * specified, then the socket's default DSCP is returned.
5591 *
5592 * spp_ipv6_flowlabel
5593 * - This field is used in conjunction with the
5594 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
5595 * The 20 least significant bits are used for the flow
5596 * label. This setting has precedence over any IPv6-layer
5597 * setting.
5598 *
5599 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
5600 * and contains the DSCP. The 6 most significant bits are
5601 * used for the DSCP. This setting has precedence over any
5602 * IPv4- or IPv6- layer setting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 */
5604static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005605 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606{
Frank Filz52ccb8e2005-12-22 11:36:46 -08005607 struct sctp_paddrparams params;
5608 struct sctp_transport *trans = NULL;
5609 struct sctp_association *asoc = NULL;
5610 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611
Xin Long0b0dce72018-07-02 18:21:13 +08005612 if (len >= sizeof(params))
5613 len = sizeof(params);
5614 else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
5615 spp_ipv6_flowlabel), 4))
5616 len = ALIGN(offsetof(struct sctp_paddrparams,
5617 spp_ipv6_flowlabel), 4);
5618 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 return -EINVAL;
Xin Long0b0dce72018-07-02 18:21:13 +08005620
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 if (copy_from_user(&params, optval, len))
5622 return -EFAULT;
5623
Frank Filz52ccb8e2005-12-22 11:36:46 -08005624 /* If an address other than INADDR_ANY is specified, and
5625 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 */
wangweidongcb3f8372013-12-23 12:16:50 +08005627 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005628 trans = sctp_addr_id2transport(sk, &params.spp_address,
5629 params.spp_assoc_id);
5630 if (!trans) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005631 pr_debug("%s: failed no transport\n", __func__);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005632 return -EINVAL;
5633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 }
5635
Frank Filz52ccb8e2005-12-22 11:36:46 -08005636 /* Get association, if assoc_id != 0 and the socket is a one
5637 * to many style socket, and an association was not found, then
5638 * the id was invalid.
5639 */
5640 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5641 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005642 pr_debug("%s: failed no association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08005644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
Frank Filz52ccb8e2005-12-22 11:36:46 -08005646 if (trans) {
5647 /* Fetch transport values. */
5648 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5649 params.spp_pathmtu = trans->pathmtu;
5650 params.spp_pathmaxrxt = trans->pathmaxrxt;
5651 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
Frank Filz52ccb8e2005-12-22 11:36:46 -08005653 /*draft-11 doesn't say what to return in spp_flags*/
5654 params.spp_flags = trans->param_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08005655 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5656 params.spp_ipv6_flowlabel = trans->flowlabel &
5657 SCTP_FLOWLABEL_VAL_MASK;
5658 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5659 }
5660 if (trans->dscp & SCTP_DSCP_SET_MASK) {
5661 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
5662 params.spp_flags |= SPP_DSCP;
5663 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08005664 } else if (asoc) {
5665 /* Fetch association values. */
5666 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5667 params.spp_pathmtu = asoc->pathmtu;
5668 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5669 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
Frank Filz52ccb8e2005-12-22 11:36:46 -08005671 /*draft-11 doesn't say what to return in spp_flags*/
5672 params.spp_flags = asoc->param_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08005673 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5674 params.spp_ipv6_flowlabel = asoc->flowlabel &
5675 SCTP_FLOWLABEL_VAL_MASK;
5676 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5677 }
5678 if (asoc->dscp & SCTP_DSCP_SET_MASK) {
5679 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
5680 params.spp_flags |= SPP_DSCP;
5681 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08005682 } else {
5683 /* Fetch socket values. */
5684 params.spp_hbinterval = sp->hbinterval;
5685 params.spp_pathmtu = sp->pathmtu;
5686 params.spp_sackdelay = sp->sackdelay;
5687 params.spp_pathmaxrxt = sp->pathmaxrxt;
5688
5689 /*draft-11 doesn't say what to return in spp_flags*/
5690 params.spp_flags = sp->param_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08005691 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5692 params.spp_ipv6_flowlabel = sp->flowlabel &
5693 SCTP_FLOWLABEL_VAL_MASK;
5694 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5695 }
5696 if (sp->dscp & SCTP_DSCP_SET_MASK) {
5697 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
5698 params.spp_flags |= SPP_DSCP;
5699 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08005700 }
5701
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 if (copy_to_user(optval, &params, len))
5703 return -EFAULT;
5704
5705 if (put_user(len, optlen))
5706 return -EFAULT;
5707
5708 return 0;
5709}
5710
Wei Yongjund364d922008-05-09 15:13:26 -07005711/*
5712 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08005713 *
Wei Yongjund364d922008-05-09 15:13:26 -07005714 * This option will effect the way delayed acks are performed. This
5715 * option allows you to get or set the delayed ack time, in
5716 * milliseconds. It also allows changing the delayed ack frequency.
5717 * Changing the frequency to 1 disables the delayed sack algorithm. If
5718 * the assoc_id is 0, then this sets or gets the endpoints default
5719 * values. If the assoc_id field is non-zero, then the set or get
5720 * effects the specified association for the one to many model (the
5721 * assoc_id field is ignored by the one to one model). Note that if
5722 * sack_delay or sack_freq are 0 when setting this option, then the
5723 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08005724 *
Wei Yongjund364d922008-05-09 15:13:26 -07005725 * struct sctp_sack_info {
5726 * sctp_assoc_t sack_assoc_id;
5727 * uint32_t sack_delay;
5728 * uint32_t sack_freq;
5729 * };
Frank Filz77086102005-12-22 11:37:30 -08005730 *
Wei Yongjund364d922008-05-09 15:13:26 -07005731 * sack_assoc_id - This parameter, indicates which association the user
5732 * is performing an action upon. Note that if this field's value is
5733 * zero then the endpoints default value is changed (effecting future
5734 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08005735 *
Wei Yongjund364d922008-05-09 15:13:26 -07005736 * sack_delay - This parameter contains the number of milliseconds that
5737 * the user is requesting the delayed ACK timer be set to. Note that
5738 * this value is defined in the standard to be between 200 and 500
5739 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08005740 *
Wei Yongjund364d922008-05-09 15:13:26 -07005741 * sack_freq - This parameter contains the number of packets that must
5742 * be received before a sack is sent without waiting for the delay
5743 * timer to expire. The default value for this is 2, setting this
5744 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08005745 */
Wei Yongjund364d922008-05-09 15:13:26 -07005746static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08005747 char __user *optval,
5748 int __user *optlen)
5749{
Wei Yongjund364d922008-05-09 15:13:26 -07005750 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08005751 struct sctp_association *asoc = NULL;
5752 struct sctp_sock *sp = sctp_sk(sk);
5753
Wei Yongjund364d922008-05-09 15:13:26 -07005754 if (len >= sizeof(struct sctp_sack_info)) {
5755 len = sizeof(struct sctp_sack_info);
5756
5757 if (copy_from_user(&params, optval, len))
5758 return -EFAULT;
5759 } else if (len == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05005760 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005761 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005762 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005763 "Use struct sctp_sack_info instead\n",
5764 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07005765 if (copy_from_user(&params, optval, len))
5766 return -EFAULT;
5767 } else
wangweidongcb3f8372013-12-23 12:16:50 +08005768 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08005769
Wei Yongjund364d922008-05-09 15:13:26 -07005770 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08005771 * to many style socket, and an association was not found, then
5772 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005773 */
Wei Yongjund364d922008-05-09 15:13:26 -07005774 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5775 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08005776 return -EINVAL;
5777
5778 if (asoc) {
5779 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005780 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5781 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08005782 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07005783 params.sack_freq = asoc->sackfreq;
5784
5785 } else {
5786 params.sack_delay = 0;
5787 params.sack_freq = 1;
5788 }
Frank Filz77086102005-12-22 11:37:30 -08005789 } else {
5790 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005791 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5792 params.sack_delay = sp->sackdelay;
5793 params.sack_freq = sp->sackfreq;
5794 } else {
5795 params.sack_delay = 0;
5796 params.sack_freq = 1;
5797 }
Frank Filz77086102005-12-22 11:37:30 -08005798 }
5799
5800 if (copy_to_user(optval, &params, len))
5801 return -EFAULT;
5802
5803 if (put_user(len, optlen))
5804 return -EFAULT;
5805
5806 return 0;
5807}
5808
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5810 *
5811 * Applications can specify protocol parameters for the default association
5812 * initialization. The option name argument to setsockopt() and getsockopt()
5813 * is SCTP_INITMSG.
5814 *
5815 * Setting initialization parameters is effective only on an unconnected
5816 * socket (for UDP-style sockets only future associations are effected
5817 * by the change). With TCP-style sockets, this option is inherited by
5818 * sockets derived from a listener socket.
5819 */
5820static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5821{
Neil Horman408f22e2007-06-16 14:03:45 -04005822 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005824 len = sizeof(struct sctp_initmsg);
5825 if (put_user(len, optlen))
5826 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5828 return -EFAULT;
5829 return 0;
5830}
5831
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005833static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5834 char __user *optval, int __user *optlen)
5835{
5836 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005837 int cnt = 0;
5838 struct sctp_getaddrs getaddrs;
5839 struct sctp_transport *from;
5840 void __user *to;
5841 union sctp_addr temp;
5842 struct sctp_sock *sp = sctp_sk(sk);
5843 int addrlen;
5844 size_t space_left;
5845 int bytes_copied;
5846
5847 if (len < sizeof(struct sctp_getaddrs))
5848 return -EINVAL;
5849
5850 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5851 return -EFAULT;
5852
5853 /* For UDP-style sockets, id specifies the association to query. */
5854 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5855 if (!asoc)
5856 return -EINVAL;
5857
wangweidongcb3f8372013-12-23 12:16:50 +08005858 to = optval + offsetof(struct sctp_getaddrs, addrs);
5859 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005860
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07005861 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5862 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08005863 memcpy(&temp, &from->ipaddr, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005864 addrlen = sctp_get_pf_specific(sk->sk_family)
5865 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005866 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005867 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005868 if (copy_to_user(to, &temp, addrlen))
5869 return -EFAULT;
5870 to += addrlen;
5871 cnt++;
5872 space_left -= addrlen;
5873 }
5874
5875 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5876 return -EFAULT;
5877 bytes_copied = ((char __user *)to) - optval;
5878 if (put_user(bytes_copied, optlen))
5879 return -EFAULT;
5880
5881 return 0;
5882}
5883
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005884static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5885 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005886{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005887 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005888 union sctp_addr temp;
5889 int cnt = 0;
5890 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005891 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005892
Vlad Yasevich29303542007-09-16 16:02:12 -07005893 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005894 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07005895 if (!addr->valid)
5896 continue;
5897
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005898 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08005899 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005900 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005901 if ((PF_INET6 == sk->sk_family) &&
5902 inet_v6_ipv6only(sk) &&
5903 (AF_INET == addr->a.sa.sa_family))
5904 continue;
Al Viro6244be42006-11-20 17:21:44 -08005905 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05005906 if (!temp.v4.sin_port)
5907 temp.v4.sin_port = htons(port);
5908
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005909 addrlen = sctp_get_pf_specific(sk->sk_family)
5910 ->addr_to_user(sctp_sk(sk), &temp);
5911
Vlad Yasevich29303542007-09-16 16:02:12 -07005912 if (space_left < addrlen) {
5913 cnt = -ENOMEM;
5914 break;
5915 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005916 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08005917
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005918 to += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005919 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005920 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04005921 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005922 }
Vlad Yasevich29303542007-09-16 16:02:12 -07005923 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005924
5925 return cnt;
5926}
5927
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005929static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5930 char __user *optval, int __user *optlen)
5931{
5932 struct sctp_bind_addr *bp;
5933 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005934 int cnt = 0;
5935 struct sctp_getaddrs getaddrs;
5936 struct sctp_sockaddr_entry *addr;
5937 void __user *to;
5938 union sctp_addr temp;
5939 struct sctp_sock *sp = sctp_sk(sk);
5940 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005941 int err = 0;
5942 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005943 int bytes_copied = 0;
5944 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005945 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005946
Neil Horman408f22e2007-06-16 14:03:45 -04005947 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005948 return -EINVAL;
5949
5950 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5951 return -EFAULT;
5952
5953 /*
5954 * For UDP-style sockets, id specifies the association to query.
5955 * If the id field is set to the value '0' then the locally bound
5956 * addresses are returned without regard to any particular
5957 * association.
5958 */
5959 if (0 == getaddrs.assoc_id) {
5960 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005961 } else {
5962 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5963 if (!asoc)
5964 return -EINVAL;
5965 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005966 }
5967
wangweidongcb3f8372013-12-23 12:16:50 +08005968 to = optval + offsetof(struct sctp_getaddrs, addrs);
5969 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Neil Horman186e2342007-06-18 19:59:16 -04005970
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02005971 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005972 if (!addrs)
5973 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005974
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005975 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5976 * addresses from the global local address list.
5977 */
5978 if (sctp_list_single_entry(&bp->address_list)) {
5979 addr = list_entry(bp->address_list.next,
5980 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04005981 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005982 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5983 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005984 if (cnt < 0) {
5985 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005986 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005987 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005988 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005989 }
5990 }
5991
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005992 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005993 /* Protection on the bound address list is not needed since
5994 * in the socket option context we hold a socket lock and
5995 * thus the bound address list can't change.
5996 */
5997 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08005998 memcpy(&temp, &addr->a, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005999 addrlen = sctp_get_pf_specific(sk->sk_family)
6000 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006001 if (space_left < addrlen) {
6002 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07006003 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006004 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07006005 memcpy(buf, &temp, addrlen);
6006 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006007 bytes_copied += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08006008 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006009 space_left -= addrlen;
6010 }
6011
6012copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006013 if (copy_to_user(to, addrs, bytes_copied)) {
6014 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02006015 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006016 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04006017 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
6018 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02006019 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04006020 }
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006021 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
6022 * but we can't change it anymore.
6023 */
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006024 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04006025 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02006026out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006027 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006028 return err;
6029}
6030
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
6032 *
6033 * Requests that the local SCTP stack use the enclosed peer address as
6034 * the association primary. The enclosed address must be one of the
6035 * association peer's addresses.
6036 */
6037static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
6038 char __user *optval, int __user *optlen)
6039{
6040 struct sctp_prim prim;
6041 struct sctp_association *asoc;
6042 struct sctp_sock *sp = sctp_sk(sk);
6043
Neil Horman408f22e2007-06-16 14:03:45 -04006044 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 return -EINVAL;
6046
Neil Horman408f22e2007-06-16 14:03:45 -04006047 len = sizeof(struct sctp_prim);
6048
6049 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 return -EFAULT;
6051
6052 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
6053 if (!asoc)
6054 return -EINVAL;
6055
6056 if (!asoc->peer.primary_path)
6057 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006058
Al Viro8cec6b82006-11-20 17:23:01 -08006059 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
6060 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
Jason Gunthorpe299ee122014-07-30 12:40:53 -06006062 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 (union sctp_addr *)&prim.ssp_addr);
6064
Neil Horman408f22e2007-06-16 14:03:45 -04006065 if (put_user(len, optlen))
6066 return -EFAULT;
6067 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 return -EFAULT;
6069
6070 return 0;
6071}
6072
6073/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006074 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006076 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 * Indication parameter for all future INIT and INIT-ACK exchanges.
6078 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006079static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 char __user *optval, int __user *optlen)
6081{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006082 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083
Neil Horman408f22e2007-06-16 14:03:45 -04006084 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 return -EINVAL;
6086
Neil Horman408f22e2007-06-16 14:03:45 -04006087 len = sizeof(struct sctp_setadaptation);
6088
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006089 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04006090
6091 if (put_user(len, optlen))
6092 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006093 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07006095
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 return 0;
6097}
6098
6099/*
6100 *
6101 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
6102 *
6103 * Applications that wish to use the sendto() system call may wish to
6104 * specify a default set of parameters that would normally be supplied
6105 * through the inclusion of ancillary data. This socket option allows
6106 * such an application to set the default sctp_sndrcvinfo structure.
6107
6108
6109 * The application that wishes to use this socket option simply passes
6110 * in to this call the sctp_sndrcvinfo structure defined in Section
6111 * 5.2.2) The input parameters accepted by this call include
6112 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
6113 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
6114 * to this call if the caller is using the UDP model.
6115 *
6116 * For getsockopt, it get the default sctp_sndrcvinfo structure.
6117 */
6118static int sctp_getsockopt_default_send_param(struct sock *sk,
6119 int len, char __user *optval,
6120 int __user *optlen)
6121{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006123 struct sctp_association *asoc;
6124 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006126 if (len < sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04006128
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006129 len = sizeof(info);
Neil Horman408f22e2007-06-16 14:03:45 -04006130
6131 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 return -EFAULT;
6133
6134 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
6135 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
6136 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 if (asoc) {
6138 info.sinfo_stream = asoc->default_stream;
6139 info.sinfo_flags = asoc->default_flags;
6140 info.sinfo_ppid = asoc->default_ppid;
6141 info.sinfo_context = asoc->default_context;
6142 info.sinfo_timetolive = asoc->default_timetolive;
6143 } else {
6144 info.sinfo_stream = sp->default_stream;
6145 info.sinfo_flags = sp->default_flags;
6146 info.sinfo_ppid = sp->default_ppid;
6147 info.sinfo_context = sp->default_context;
6148 info.sinfo_timetolive = sp->default_timetolive;
6149 }
6150
Neil Horman408f22e2007-06-16 14:03:45 -04006151 if (put_user(len, optlen))
6152 return -EFAULT;
6153 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 return -EFAULT;
6155
6156 return 0;
6157}
6158
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006159/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
6160 * (SCTP_DEFAULT_SNDINFO)
6161 */
6162static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
6163 char __user *optval,
6164 int __user *optlen)
6165{
6166 struct sctp_sock *sp = sctp_sk(sk);
6167 struct sctp_association *asoc;
6168 struct sctp_sndinfo info;
6169
6170 if (len < sizeof(info))
6171 return -EINVAL;
6172
6173 len = sizeof(info);
6174
6175 if (copy_from_user(&info, optval, len))
6176 return -EFAULT;
6177
6178 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
6179 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
6180 return -EINVAL;
6181 if (asoc) {
6182 info.snd_sid = asoc->default_stream;
6183 info.snd_flags = asoc->default_flags;
6184 info.snd_ppid = asoc->default_ppid;
6185 info.snd_context = asoc->default_context;
6186 } else {
6187 info.snd_sid = sp->default_stream;
6188 info.snd_flags = sp->default_flags;
6189 info.snd_ppid = sp->default_ppid;
6190 info.snd_context = sp->default_context;
6191 }
6192
6193 if (put_user(len, optlen))
6194 return -EFAULT;
6195 if (copy_to_user(optval, &info, len))
6196 return -EFAULT;
6197
6198 return 0;
6199}
6200
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201/*
6202 *
6203 * 7.1.5 SCTP_NODELAY
6204 *
6205 * Turn on/off any Nagle-like algorithm. This means that packets are
6206 * generally sent as soon as possible and no unnecessary delays are
6207 * introduced, at the cost of more packets in the network. Expects an
6208 * integer boolean flag.
6209 */
6210
6211static int sctp_getsockopt_nodelay(struct sock *sk, int len,
6212 char __user *optval, int __user *optlen)
6213{
6214 int val;
6215
6216 if (len < sizeof(int))
6217 return -EINVAL;
6218
6219 len = sizeof(int);
6220 val = (sctp_sk(sk)->nodelay == 1);
6221 if (put_user(len, optlen))
6222 return -EFAULT;
6223 if (copy_to_user(optval, &val, len))
6224 return -EFAULT;
6225 return 0;
6226}
6227
6228/*
6229 *
6230 * 7.1.1 SCTP_RTOINFO
6231 *
6232 * The protocol parameters used to initialize and bound retransmission
6233 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
6234 * and modify these parameters.
6235 * All parameters are time values, in milliseconds. A value of 0, when
6236 * modifying the parameters, indicates that the current value should not
6237 * be changed.
6238 *
6239 */
6240static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6241 char __user *optval,
6242 int __user *optlen) {
6243 struct sctp_rtoinfo rtoinfo;
6244 struct sctp_association *asoc;
6245
Neil Horman408f22e2007-06-16 14:03:45 -04006246 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 return -EINVAL;
6248
Neil Horman408f22e2007-06-16 14:03:45 -04006249 len = sizeof(struct sctp_rtoinfo);
6250
6251 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 return -EFAULT;
6253
6254 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6255
6256 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
6257 return -EINVAL;
6258
6259 /* Values corresponding to the specific association. */
6260 if (asoc) {
6261 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6262 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6263 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6264 } else {
6265 /* Values corresponding to the endpoint. */
6266 struct sctp_sock *sp = sctp_sk(sk);
6267
6268 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6269 rtoinfo.srto_max = sp->rtoinfo.srto_max;
6270 rtoinfo.srto_min = sp->rtoinfo.srto_min;
6271 }
6272
6273 if (put_user(len, optlen))
6274 return -EFAULT;
6275
6276 if (copy_to_user(optval, &rtoinfo, len))
6277 return -EFAULT;
6278
6279 return 0;
6280}
6281
6282/*
6283 *
6284 * 7.1.2 SCTP_ASSOCINFO
6285 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02006286 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 * of the association.
6288 * Returns an error if the new association retransmission value is
6289 * greater than the sum of the retransmission value of the peer.
6290 * See [SCTP] for more information.
6291 *
6292 */
6293static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6294 char __user *optval,
6295 int __user *optlen)
6296{
6297
6298 struct sctp_assocparams assocparams;
6299 struct sctp_association *asoc;
6300 struct list_head *pos;
6301 int cnt = 0;
6302
Neil Horman408f22e2007-06-16 14:03:45 -04006303 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 return -EINVAL;
6305
Neil Horman408f22e2007-06-16 14:03:45 -04006306 len = sizeof(struct sctp_assocparams);
6307
6308 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 return -EFAULT;
6310
6311 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6312
6313 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
6314 return -EINVAL;
6315
6316 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07006317 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6319 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6320 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
Daniel Borkmann52db8822013-06-25 18:17:27 +02006321 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322
6323 list_for_each(pos, &asoc->peer.transport_addr_list) {
wangweidongcb3f8372013-12-23 12:16:50 +08006324 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 }
6326
6327 assocparams.sasoc_number_peer_destinations = cnt;
6328 } else {
6329 /* Values corresponding to the endpoint */
6330 struct sctp_sock *sp = sctp_sk(sk);
6331
6332 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6333 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6334 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6335 assocparams.sasoc_cookie_life =
6336 sp->assocparams.sasoc_cookie_life;
6337 assocparams.sasoc_number_peer_destinations =
6338 sp->assocparams.
6339 sasoc_number_peer_destinations;
6340 }
6341
6342 if (put_user(len, optlen))
6343 return -EFAULT;
6344
6345 if (copy_to_user(optval, &assocparams, len))
6346 return -EFAULT;
6347
6348 return 0;
6349}
6350
6351/*
6352 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6353 *
6354 * This socket option is a boolean flag which turns on or off mapped V4
6355 * addresses. If this option is turned on and the socket is type
6356 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6357 * If this option is turned off, then no mapping will be done of V4
6358 * addresses and a user will receive both PF_INET6 and PF_INET type
6359 * addresses on the socket.
6360 */
6361static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6362 char __user *optval, int __user *optlen)
6363{
6364 int val;
6365 struct sctp_sock *sp = sctp_sk(sk);
6366
6367 if (len < sizeof(int))
6368 return -EINVAL;
6369
6370 len = sizeof(int);
6371 val = sp->v4mapped;
6372 if (put_user(len, optlen))
6373 return -EFAULT;
6374 if (copy_to_user(optval, &val, len))
6375 return -EFAULT;
6376
6377 return 0;
6378}
6379
6380/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006381 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6382 * (chapter and verse is quoted at sctp_setsockopt_context())
6383 */
6384static int sctp_getsockopt_context(struct sock *sk, int len,
6385 char __user *optval, int __user *optlen)
6386{
6387 struct sctp_assoc_value params;
6388 struct sctp_sock *sp;
6389 struct sctp_association *asoc;
6390
Neil Horman408f22e2007-06-16 14:03:45 -04006391 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006392 return -EINVAL;
6393
Neil Horman408f22e2007-06-16 14:03:45 -04006394 len = sizeof(struct sctp_assoc_value);
6395
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006396 if (copy_from_user(&params, optval, len))
6397 return -EFAULT;
6398
6399 sp = sctp_sk(sk);
6400
6401 if (params.assoc_id != 0) {
6402 asoc = sctp_id2assoc(sk, params.assoc_id);
6403 if (!asoc)
6404 return -EINVAL;
6405 params.assoc_value = asoc->default_rcv_context;
6406 } else {
6407 params.assoc_value = sp->default_rcv_context;
6408 }
6409
6410 if (put_user(len, optlen))
6411 return -EFAULT;
6412 if (copy_to_user(optval, &params, len))
6413 return -EFAULT;
6414
6415 return 0;
6416}
6417
6418/*
Wei Yongjune89c2092008-12-25 16:54:58 -08006419 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6420 * This option will get or set the maximum size to put in any outgoing
6421 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 * fragmented by SCTP into the specified size. Note that the underlying
6423 * SCTP implementation may fragment into smaller sized chunks when the
6424 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08006425 * the user. The default value for this option is '0' which indicates
6426 * the user is NOT limiting fragmentation and only the PMTU will effect
6427 * SCTP's choice of DATA chunk size. Note also that values set larger
6428 * than the maximum size of an IP datagram will effectively let SCTP
6429 * control fragmentation (i.e. the same as setting this option to 0).
6430 *
6431 * The following structure is used to access and modify this parameter:
6432 *
6433 * struct sctp_assoc_value {
6434 * sctp_assoc_t assoc_id;
6435 * uint32_t assoc_value;
6436 * };
6437 *
6438 * assoc_id: This parameter is ignored for one-to-one style sockets.
6439 * For one-to-many style sockets this parameter indicates which
6440 * association the user is performing an action upon. Note that if
6441 * this field's value is zero then the endpoints default value is
6442 * changed (effecting future associations only).
6443 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444 */
6445static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6446 char __user *optval, int __user *optlen)
6447{
Wei Yongjune89c2092008-12-25 16:54:58 -08006448 struct sctp_assoc_value params;
6449 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450
Wei Yongjune89c2092008-12-25 16:54:58 -08006451 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006452 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006453 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006454 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006455 "Use struct sctp_assoc_value instead\n",
6456 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08006457 params.assoc_id = 0;
6458 } else if (len >= sizeof(struct sctp_assoc_value)) {
6459 len = sizeof(struct sctp_assoc_value);
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006460 if (copy_from_user(&params, optval, len))
Wei Yongjune89c2092008-12-25 16:54:58 -08006461 return -EFAULT;
6462 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 return -EINVAL;
6464
Wei Yongjune89c2092008-12-25 16:54:58 -08006465 asoc = sctp_id2assoc(sk, params.assoc_id);
6466 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
6467 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468
Wei Yongjune89c2092008-12-25 16:54:58 -08006469 if (asoc)
6470 params.assoc_value = asoc->frag_point;
6471 else
6472 params.assoc_value = sctp_sk(sk)->user_frag;
6473
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 if (put_user(len, optlen))
6475 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08006476 if (len == sizeof(int)) {
6477 if (copy_to_user(optval, &params.assoc_value, len))
6478 return -EFAULT;
6479 } else {
6480 if (copy_to_user(optval, &params, len))
6481 return -EFAULT;
6482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483
6484 return 0;
6485}
6486
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006487/*
6488 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6489 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6490 */
6491static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6492 char __user *optval, int __user *optlen)
6493{
6494 int val;
6495
6496 if (len < sizeof(int))
6497 return -EINVAL;
6498
6499 len = sizeof(int);
6500
6501 val = sctp_sk(sk)->frag_interleave;
6502 if (put_user(len, optlen))
6503 return -EFAULT;
6504 if (copy_to_user(optval, &val, len))
6505 return -EFAULT;
6506
6507 return 0;
6508}
6509
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006510/*
6511 * 7.1.25. Set or Get the sctp partial delivery point
6512 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6513 */
6514static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6515 char __user *optval,
6516 int __user *optlen)
6517{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09006518 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006519
6520 if (len < sizeof(u32))
6521 return -EINVAL;
6522
6523 len = sizeof(u32);
6524
6525 val = sctp_sk(sk)->pd_point;
6526 if (put_user(len, optlen))
6527 return -EFAULT;
6528 if (copy_to_user(optval, &val, len))
6529 return -EFAULT;
6530
Wei Yongjun7d743b72010-12-14 16:10:41 +00006531 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006532}
6533
Vlad Yasevich70331572007-03-23 11:34:36 -07006534/*
6535 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6536 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6537 */
6538static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6539 char __user *optval,
6540 int __user *optlen)
6541{
Neil Horman219b99a2008-03-05 13:44:46 -08006542 struct sctp_assoc_value params;
6543 struct sctp_sock *sp;
6544 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07006545
Neil Horman219b99a2008-03-05 13:44:46 -08006546 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006547 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006548 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006549 "Use of int in max_burst socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006550 "Use struct sctp_assoc_value instead\n",
6551 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08006552 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00006553 } else if (len >= sizeof(struct sctp_assoc_value)) {
6554 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08006555 if (copy_from_user(&params, optval, len))
6556 return -EFAULT;
6557 } else
6558 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07006559
Neil Horman219b99a2008-03-05 13:44:46 -08006560 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07006561
Neil Horman219b99a2008-03-05 13:44:46 -08006562 if (params.assoc_id != 0) {
6563 asoc = sctp_id2assoc(sk, params.assoc_id);
6564 if (!asoc)
6565 return -EINVAL;
6566 params.assoc_value = asoc->max_burst;
6567 } else
6568 params.assoc_value = sp->max_burst;
6569
6570 if (len == sizeof(int)) {
6571 if (copy_to_user(optval, &params.assoc_value, len))
6572 return -EFAULT;
6573 } else {
6574 if (copy_to_user(optval, &params, len))
6575 return -EFAULT;
6576 }
6577
6578 return 0;
6579
Vlad Yasevich70331572007-03-23 11:34:36 -07006580}
6581
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006582static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6583 char __user *optval, int __user *optlen)
6584{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006585 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006586 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006587 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006588 __u16 data_len = 0;
6589 u32 num_idents;
Xin Long7a84bd42016-02-03 23:33:30 +08006590 int i;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006591
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006592 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006593 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006594
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006595 hmacs = ep->auth_hmacs_list;
Xin Long3c918702017-06-30 11:52:16 +08006596 data_len = ntohs(hmacs->param_hdr.length) -
6597 sizeof(struct sctp_paramhdr);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006598
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006599 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006600 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006601
6602 len = sizeof(struct sctp_hmacalgo) + data_len;
6603 num_idents = data_len / sizeof(u16);
6604
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006605 if (put_user(len, optlen))
6606 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006607 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006608 return -EFAULT;
Xin Long7a84bd42016-02-03 23:33:30 +08006609 for (i = 0; i < num_idents; i++) {
6610 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6611
6612 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6613 return -EFAULT;
6614 }
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006615 return 0;
6616}
6617
6618static int sctp_getsockopt_active_key(struct sock *sk, int len,
6619 char __user *optval, int __user *optlen)
6620{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006621 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006622 struct sctp_authkeyid val;
6623 struct sctp_association *asoc;
6624
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006625 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006626 return -EACCES;
6627
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006628 if (len < sizeof(struct sctp_authkeyid))
6629 return -EINVAL;
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006630
6631 len = sizeof(struct sctp_authkeyid);
6632 if (copy_from_user(&val, optval, len))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006633 return -EFAULT;
6634
6635 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6636 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6637 return -EINVAL;
6638
6639 if (asoc)
6640 val.scact_keynumber = asoc->active_key_id;
6641 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006642 val.scact_keynumber = ep->active_key_id;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006643
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006644 if (put_user(len, optlen))
6645 return -EFAULT;
6646 if (copy_to_user(optval, &val, len))
6647 return -EFAULT;
6648
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006649 return 0;
6650}
6651
6652static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6653 char __user *optval, int __user *optlen)
6654{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006655 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006656 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006657 struct sctp_authchunks val;
6658 struct sctp_association *asoc;
6659 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006660 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006661 char __user *to;
6662
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006663 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006664 return -EACCES;
6665
6666 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006667 return -EINVAL;
6668
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006669 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006670 return -EFAULT;
6671
Al Viro411223c2007-10-14 19:21:20 +01006672 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006673 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6674 if (!asoc)
6675 return -EINVAL;
6676
6677 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006678 if (!ch)
6679 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006680
6681 /* See if the user provided enough room for all the data */
Xin Long3c918702017-06-30 11:52:16 +08006682 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevichb40db682008-02-27 14:40:37 -05006683 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006684 return -EINVAL;
6685
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006686 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006687 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006688num:
6689 len = sizeof(struct sctp_authchunks) + num_chunks;
wangweidong8d726512013-12-23 12:16:53 +08006690 if (put_user(len, optlen))
6691 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006692 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6693 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006694 return 0;
6695}
6696
6697static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6698 char __user *optval, int __user *optlen)
6699{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006700 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006701 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006702 struct sctp_authchunks val;
6703 struct sctp_association *asoc;
6704 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006705 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006706 char __user *to;
6707
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006708 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006709 return -EACCES;
6710
6711 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006712 return -EINVAL;
6713
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006714 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006715 return -EFAULT;
6716
Al Viro411223c2007-10-14 19:21:20 +01006717 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006718 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6719 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6720 return -EINVAL;
6721
6722 if (asoc)
wangweidong26ac8e52013-12-23 12:16:51 +08006723 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006724 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006725 ch = ep->auth_chunk_list;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006726
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006727 if (!ch)
6728 goto num;
6729
Xin Long3c918702017-06-30 11:52:16 +08006730 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006731 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006732 return -EINVAL;
6733
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006734 if (copy_to_user(to, ch->chunks, num_chunks))
6735 return -EFAULT;
6736num:
6737 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006738 if (put_user(len, optlen))
6739 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006740 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6741 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006742
6743 return 0;
6744}
6745
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08006746/*
6747 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6748 * This option gets the current number of associations that are attached
6749 * to a one-to-many style socket. The option value is an uint32_t.
6750 */
6751static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6752 char __user *optval, int __user *optlen)
6753{
6754 struct sctp_sock *sp = sctp_sk(sk);
6755 struct sctp_association *asoc;
6756 u32 val = 0;
6757
6758 if (sctp_style(sk, TCP))
6759 return -EOPNOTSUPP;
6760
6761 if (len < sizeof(u32))
6762 return -EINVAL;
6763
6764 len = sizeof(u32);
6765
6766 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6767 val++;
6768 }
6769
6770 if (put_user(len, optlen))
6771 return -EFAULT;
6772 if (copy_to_user(optval, &val, len))
6773 return -EFAULT;
6774
6775 return 0;
6776}
6777
Wei Yongjun209ba422011-04-17 17:27:08 +00006778/*
Michio Honda7dc04d72011-04-26 20:16:31 +09006779 * 8.1.23 SCTP_AUTO_ASCONF
6780 * See the corresponding setsockopt entry as description
6781 */
6782static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6783 char __user *optval, int __user *optlen)
6784{
6785 int val = 0;
6786
6787 if (len < sizeof(int))
6788 return -EINVAL;
6789
6790 len = sizeof(int);
6791 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6792 val = 1;
6793 if (put_user(len, optlen))
6794 return -EFAULT;
6795 if (copy_to_user(optval, &val, len))
6796 return -EFAULT;
6797 return 0;
6798}
6799
6800/*
Wei Yongjun209ba422011-04-17 17:27:08 +00006801 * 8.2.6. Get the Current Identifiers of Associations
6802 * (SCTP_GET_ASSOC_ID_LIST)
6803 *
6804 * This option gets the current list of SCTP association identifiers of
6805 * the SCTP associations handled by a one-to-many style socket.
6806 */
6807static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6808 char __user *optval, int __user *optlen)
6809{
6810 struct sctp_sock *sp = sctp_sk(sk);
6811 struct sctp_association *asoc;
6812 struct sctp_assoc_ids *ids;
6813 u32 num = 0;
6814
6815 if (sctp_style(sk, TCP))
6816 return -EOPNOTSUPP;
6817
6818 if (len < sizeof(struct sctp_assoc_ids))
6819 return -EINVAL;
6820
6821 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6822 num++;
6823 }
6824
6825 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6826 return -EINVAL;
6827
6828 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6829
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02006830 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
Wei Yongjun209ba422011-04-17 17:27:08 +00006831 if (unlikely(!ids))
6832 return -ENOMEM;
6833
6834 ids->gaids_number_of_ids = num;
6835 num = 0;
6836 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6837 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6838 }
6839
6840 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6841 kfree(ids);
6842 return -EFAULT;
6843 }
6844
6845 kfree(ids);
6846 return 0;
6847}
6848
Neil Horman5aa93bc2012-07-21 07:56:07 +00006849/*
6850 * SCTP_PEER_ADDR_THLDS
6851 *
6852 * This option allows us to fetch the partially failed threshold for one or all
6853 * transports in an association. See Section 6.1 of:
6854 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6855 */
6856static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6857 char __user *optval,
6858 int len,
6859 int __user *optlen)
6860{
6861 struct sctp_paddrthlds val;
6862 struct sctp_transport *trans;
6863 struct sctp_association *asoc;
6864
6865 if (len < sizeof(struct sctp_paddrthlds))
6866 return -EINVAL;
6867 len = sizeof(struct sctp_paddrthlds);
6868 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6869 return -EFAULT;
6870
6871 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6872 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6873 if (!asoc)
6874 return -ENOENT;
6875
6876 val.spt_pathpfthld = asoc->pf_retrans;
6877 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6878 } else {
6879 trans = sctp_addr_id2transport(sk, &val.spt_address,
6880 val.spt_assoc_id);
6881 if (!trans)
6882 return -ENOENT;
6883
6884 val.spt_pathmaxrxt = trans->pathmaxrxt;
6885 val.spt_pathpfthld = trans->pf_retrans;
6886 }
6887
6888 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6889 return -EFAULT;
6890
6891 return 0;
6892}
6893
Michele Baldessari196d6752012-12-01 04:49:42 +00006894/*
6895 * SCTP_GET_ASSOC_STATS
6896 *
6897 * This option retrieves local per endpoint statistics. It is modeled
6898 * after OpenSolaris' implementation
6899 */
6900static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6901 char __user *optval,
6902 int __user *optlen)
6903{
6904 struct sctp_assoc_stats sas;
6905 struct sctp_association *asoc = NULL;
6906
6907 /* User must provide at least the assoc id */
6908 if (len < sizeof(sctp_assoc_t))
6909 return -EINVAL;
6910
Guenter Roeck726bc6b2013-02-27 10:57:31 +00006911 /* Allow the struct to grow and fill in as much as possible */
6912 len = min_t(size_t, len, sizeof(sas));
6913
Michele Baldessari196d6752012-12-01 04:49:42 +00006914 if (copy_from_user(&sas, optval, len))
6915 return -EFAULT;
6916
6917 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6918 if (!asoc)
6919 return -EINVAL;
6920
6921 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6922 sas.sas_gapcnt = asoc->stats.gapcnt;
6923 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6924 sas.sas_osacks = asoc->stats.osacks;
6925 sas.sas_isacks = asoc->stats.isacks;
6926 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6927 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6928 sas.sas_oodchunks = asoc->stats.oodchunks;
6929 sas.sas_iodchunks = asoc->stats.iodchunks;
6930 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6931 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6932 sas.sas_idupchunks = asoc->stats.idupchunks;
6933 sas.sas_opackets = asoc->stats.opackets;
6934 sas.sas_ipackets = asoc->stats.ipackets;
6935
6936 /* New high max rto observed, will return 0 if not a single
6937 * RTO update took place. obs_rto_ipaddr will be bogus
6938 * in such a case
6939 */
6940 sas.sas_maxrto = asoc->stats.max_obs_rto;
6941 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6942 sizeof(struct sockaddr_storage));
6943
6944 /* Mark beginning of a new observation period */
6945 asoc->stats.max_obs_rto = asoc->rto_min;
6946
Michele Baldessari196d6752012-12-01 04:49:42 +00006947 if (put_user(len, optlen))
6948 return -EFAULT;
6949
Daniel Borkmannbb333812013-06-28 19:49:40 +02006950 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
Michele Baldessari196d6752012-12-01 04:49:42 +00006951
6952 if (copy_to_user(optval, &sas, len))
6953 return -EFAULT;
6954
6955 return 0;
6956}
6957
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02006958static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6959 char __user *optval,
6960 int __user *optlen)
6961{
6962 int val = 0;
6963
6964 if (len < sizeof(int))
6965 return -EINVAL;
6966
6967 len = sizeof(int);
6968 if (sctp_sk(sk)->recvrcvinfo)
6969 val = 1;
6970 if (put_user(len, optlen))
6971 return -EFAULT;
6972 if (copy_to_user(optval, &val, len))
6973 return -EFAULT;
6974
6975 return 0;
6976}
6977
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02006978static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6979 char __user *optval,
6980 int __user *optlen)
6981{
6982 int val = 0;
6983
6984 if (len < sizeof(int))
6985 return -EINVAL;
6986
6987 len = sizeof(int);
6988 if (sctp_sk(sk)->recvnxtinfo)
6989 val = 1;
6990 if (put_user(len, optlen))
6991 return -EFAULT;
6992 if (copy_to_user(optval, &val, len))
6993 return -EFAULT;
6994
6995 return 0;
6996}
6997
Xin Long28aa4c22016-07-09 19:47:40 +08006998static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6999 char __user *optval,
7000 int __user *optlen)
7001{
7002 struct sctp_assoc_value params;
7003 struct sctp_association *asoc;
7004 int retval = -EFAULT;
7005
7006 if (len < sizeof(params)) {
7007 retval = -EINVAL;
7008 goto out;
7009 }
7010
7011 len = sizeof(params);
7012 if (copy_from_user(&params, optval, len))
7013 goto out;
7014
7015 asoc = sctp_id2assoc(sk, params.assoc_id);
7016 if (asoc) {
7017 params.assoc_value = asoc->prsctp_enable;
7018 } else if (!params.assoc_id) {
7019 struct sctp_sock *sp = sctp_sk(sk);
7020
7021 params.assoc_value = sp->ep->prsctp_enable;
7022 } else {
7023 retval = -EINVAL;
7024 goto out;
7025 }
7026
7027 if (put_user(len, optlen))
7028 goto out;
7029
7030 if (copy_to_user(optval, &params, len))
7031 goto out;
7032
7033 retval = 0;
7034
7035out:
7036 return retval;
7037}
7038
Xin Longf959fb42016-07-09 19:47:41 +08007039static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
7040 char __user *optval,
7041 int __user *optlen)
7042{
7043 struct sctp_default_prinfo info;
7044 struct sctp_association *asoc;
7045 int retval = -EFAULT;
7046
7047 if (len < sizeof(info)) {
7048 retval = -EINVAL;
7049 goto out;
7050 }
7051
7052 len = sizeof(info);
7053 if (copy_from_user(&info, optval, len))
7054 goto out;
7055
7056 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
7057 if (asoc) {
7058 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
7059 info.pr_value = asoc->default_timetolive;
7060 } else if (!info.pr_assoc_id) {
7061 struct sctp_sock *sp = sctp_sk(sk);
7062
7063 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
7064 info.pr_value = sp->default_timetolive;
7065 } else {
7066 retval = -EINVAL;
7067 goto out;
7068 }
7069
7070 if (put_user(len, optlen))
7071 goto out;
7072
7073 if (copy_to_user(optval, &info, len))
7074 goto out;
7075
7076 retval = 0;
7077
7078out:
7079 return retval;
7080}
7081
Xin Long826d2532016-07-09 19:47:42 +08007082static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
7083 char __user *optval,
7084 int __user *optlen)
7085{
7086 struct sctp_prstatus params;
7087 struct sctp_association *asoc;
7088 int policy;
7089 int retval = -EINVAL;
7090
7091 if (len < sizeof(params))
7092 goto out;
7093
7094 len = sizeof(params);
7095 if (copy_from_user(&params, optval, len)) {
7096 retval = -EFAULT;
7097 goto out;
7098 }
7099
7100 policy = params.sprstat_policy;
7101 if (policy & ~SCTP_PR_SCTP_MASK)
7102 goto out;
7103
7104 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7105 if (!asoc)
7106 goto out;
7107
7108 if (policy == SCTP_PR_SCTP_NONE) {
7109 params.sprstat_abandoned_unsent = 0;
7110 params.sprstat_abandoned_sent = 0;
7111 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7112 params.sprstat_abandoned_unsent +=
7113 asoc->abandoned_unsent[policy];
7114 params.sprstat_abandoned_sent +=
7115 asoc->abandoned_sent[policy];
7116 }
7117 } else {
7118 params.sprstat_abandoned_unsent =
7119 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7120 params.sprstat_abandoned_sent =
7121 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
7122 }
7123
7124 if (put_user(len, optlen)) {
7125 retval = -EFAULT;
7126 goto out;
7127 }
7128
7129 if (copy_to_user(optval, &params, len)) {
7130 retval = -EFAULT;
7131 goto out;
7132 }
7133
7134 retval = 0;
7135
7136out:
7137 return retval;
7138}
7139
Xin Longd229d482017-04-01 17:07:46 +08007140static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
7141 char __user *optval,
7142 int __user *optlen)
7143{
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007144 struct sctp_stream_out_ext *streamoute;
Xin Longd229d482017-04-01 17:07:46 +08007145 struct sctp_association *asoc;
7146 struct sctp_prstatus params;
7147 int retval = -EINVAL;
7148 int policy;
7149
7150 if (len < sizeof(params))
7151 goto out;
7152
7153 len = sizeof(params);
7154 if (copy_from_user(&params, optval, len)) {
7155 retval = -EFAULT;
7156 goto out;
7157 }
7158
7159 policy = params.sprstat_policy;
7160 if (policy & ~SCTP_PR_SCTP_MASK)
7161 goto out;
7162
7163 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
Xin Longcee360a2017-05-31 16:36:31 +08007164 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
Xin Longd229d482017-04-01 17:07:46 +08007165 goto out;
7166
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03007167 streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007168 if (!streamoute) {
7169 /* Not allocated yet, means all stats are 0 */
7170 params.sprstat_abandoned_unsent = 0;
7171 params.sprstat_abandoned_sent = 0;
7172 retval = 0;
7173 goto out;
7174 }
7175
Xin Longd229d482017-04-01 17:07:46 +08007176 if (policy == SCTP_PR_SCTP_NONE) {
7177 params.sprstat_abandoned_unsent = 0;
7178 params.sprstat_abandoned_sent = 0;
7179 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7180 params.sprstat_abandoned_unsent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007181 streamoute->abandoned_unsent[policy];
Xin Longd229d482017-04-01 17:07:46 +08007182 params.sprstat_abandoned_sent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007183 streamoute->abandoned_sent[policy];
Xin Longd229d482017-04-01 17:07:46 +08007184 }
7185 } else {
7186 params.sprstat_abandoned_unsent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007187 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08007188 params.sprstat_abandoned_sent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007189 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08007190 }
7191
7192 if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
7193 retval = -EFAULT;
7194 goto out;
7195 }
7196
7197 retval = 0;
7198
7199out:
7200 return retval;
7201}
7202
Xin Longc0d8bab2017-03-10 12:11:12 +08007203static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
7204 char __user *optval,
7205 int __user *optlen)
7206{
7207 struct sctp_assoc_value params;
7208 struct sctp_association *asoc;
7209 int retval = -EFAULT;
7210
7211 if (len < sizeof(params)) {
7212 retval = -EINVAL;
7213 goto out;
7214 }
7215
7216 len = sizeof(params);
7217 if (copy_from_user(&params, optval, len))
7218 goto out;
7219
7220 asoc = sctp_id2assoc(sk, params.assoc_id);
7221 if (asoc) {
7222 params.assoc_value = asoc->reconf_enable;
7223 } else if (!params.assoc_id) {
7224 struct sctp_sock *sp = sctp_sk(sk);
7225
7226 params.assoc_value = sp->ep->reconf_enable;
7227 } else {
7228 retval = -EINVAL;
7229 goto out;
7230 }
7231
7232 if (put_user(len, optlen))
7233 goto out;
7234
7235 if (copy_to_user(optval, &params, len))
7236 goto out;
7237
7238 retval = 0;
7239
7240out:
7241 return retval;
7242}
7243
Xin Long9fb657a2017-01-18 00:44:46 +08007244static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7245 char __user *optval,
7246 int __user *optlen)
7247{
7248 struct sctp_assoc_value params;
7249 struct sctp_association *asoc;
7250 int retval = -EFAULT;
7251
7252 if (len < sizeof(params)) {
7253 retval = -EINVAL;
7254 goto out;
7255 }
7256
7257 len = sizeof(params);
7258 if (copy_from_user(&params, optval, len))
7259 goto out;
7260
7261 asoc = sctp_id2assoc(sk, params.assoc_id);
7262 if (asoc) {
7263 params.assoc_value = asoc->strreset_enable;
7264 } else if (!params.assoc_id) {
7265 struct sctp_sock *sp = sctp_sk(sk);
7266
7267 params.assoc_value = sp->ep->strreset_enable;
7268 } else {
7269 retval = -EINVAL;
7270 goto out;
7271 }
7272
7273 if (put_user(len, optlen))
7274 goto out;
7275
7276 if (copy_to_user(optval, &params, len))
7277 goto out;
7278
7279 retval = 0;
7280
7281out:
7282 return retval;
7283}
7284
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007285static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7286 char __user *optval,
7287 int __user *optlen)
7288{
7289 struct sctp_assoc_value params;
7290 struct sctp_association *asoc;
7291 int retval = -EFAULT;
7292
7293 if (len < sizeof(params)) {
7294 retval = -EINVAL;
7295 goto out;
7296 }
7297
7298 len = sizeof(params);
7299 if (copy_from_user(&params, optval, len))
7300 goto out;
7301
7302 asoc = sctp_id2assoc(sk, params.assoc_id);
7303 if (!asoc) {
7304 retval = -EINVAL;
7305 goto out;
7306 }
7307
7308 params.assoc_value = sctp_sched_get_sched(asoc);
7309
7310 if (put_user(len, optlen))
7311 goto out;
7312
7313 if (copy_to_user(optval, &params, len))
7314 goto out;
7315
7316 retval = 0;
7317
7318out:
7319 return retval;
7320}
7321
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007322static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7323 char __user *optval,
7324 int __user *optlen)
7325{
7326 struct sctp_stream_value params;
7327 struct sctp_association *asoc;
7328 int retval = -EFAULT;
7329
7330 if (len < sizeof(params)) {
7331 retval = -EINVAL;
7332 goto out;
7333 }
7334
7335 len = sizeof(params);
7336 if (copy_from_user(&params, optval, len))
7337 goto out;
7338
7339 asoc = sctp_id2assoc(sk, params.assoc_id);
7340 if (!asoc) {
7341 retval = -EINVAL;
7342 goto out;
7343 }
7344
7345 retval = sctp_sched_get_value(asoc, params.stream_id,
7346 &params.stream_value);
7347 if (retval)
7348 goto out;
7349
7350 if (put_user(len, optlen)) {
7351 retval = -EFAULT;
7352 goto out;
7353 }
7354
7355 if (copy_to_user(optval, &params, len)) {
7356 retval = -EFAULT;
7357 goto out;
7358 }
7359
7360out:
7361 return retval;
7362}
7363
Xin Long772a5862017-12-08 21:03:58 +08007364static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7365 char __user *optval,
7366 int __user *optlen)
7367{
7368 struct sctp_assoc_value params;
7369 struct sctp_association *asoc;
7370 int retval = -EFAULT;
7371
7372 if (len < sizeof(params)) {
7373 retval = -EINVAL;
7374 goto out;
7375 }
7376
7377 len = sizeof(params);
7378 if (copy_from_user(&params, optval, len))
7379 goto out;
7380
7381 asoc = sctp_id2assoc(sk, params.assoc_id);
7382 if (asoc) {
7383 params.assoc_value = asoc->intl_enable;
7384 } else if (!params.assoc_id) {
7385 struct sctp_sock *sp = sctp_sk(sk);
7386
7387 params.assoc_value = sp->strm_interleave;
7388 } else {
7389 retval = -EINVAL;
7390 goto out;
7391 }
7392
7393 if (put_user(len, optlen))
7394 goto out;
7395
7396 if (copy_to_user(optval, &params, len))
7397 goto out;
7398
7399 retval = 0;
7400
7401out:
7402 return retval;
7403}
7404
Xin Longb0e9a2f2018-06-28 15:31:00 +08007405static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
7406 char __user *optval,
7407 int __user *optlen)
7408{
7409 int val;
7410
7411 if (len < sizeof(int))
7412 return -EINVAL;
7413
7414 len = sizeof(int);
7415 val = sctp_sk(sk)->reuse;
7416 if (put_user(len, optlen))
7417 return -EFAULT;
7418
7419 if (copy_to_user(optval, &val, len))
7420 return -EFAULT;
7421
7422 return 0;
7423}
7424
Daniel Borkmanndda91922013-06-17 11:40:05 +02007425static int sctp_getsockopt(struct sock *sk, int level, int optname,
7426 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427{
7428 int retval = 0;
7429 int len;
7430
Daniel Borkmannbb333812013-06-28 19:49:40 +02007431 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432
7433 /* I can hardly begin to describe how wrong this is. This is
7434 * so broken as to be worse than useless. The API draft
7435 * REALLY is NOT helpful here... I am not convinced that the
7436 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7437 * are at all well-founded.
7438 */
7439 if (level != SOL_SCTP) {
7440 struct sctp_af *af = sctp_sk(sk)->pf->af;
7441
7442 retval = af->getsockopt(sk, level, optname, optval, optlen);
7443 return retval;
7444 }
7445
7446 if (get_user(len, optlen))
7447 return -EFAULT;
7448
Jiri Slabya4b8e712016-10-21 14:13:24 +02007449 if (len < 0)
7450 return -EINVAL;
7451
wangweidong048ed4b2014-01-21 15:44:11 +08007452 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453
7454 switch (optname) {
7455 case SCTP_STATUS:
7456 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7457 break;
7458 case SCTP_DISABLE_FRAGMENTS:
7459 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7460 optlen);
7461 break;
7462 case SCTP_EVENTS:
7463 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7464 break;
7465 case SCTP_AUTOCLOSE:
7466 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7467 break;
7468 case SCTP_SOCKOPT_PEELOFF:
7469 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7470 break;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04007471 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7472 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7473 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 case SCTP_PEER_ADDR_PARAMS:
7475 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7476 optlen);
7477 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00007478 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07007479 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08007480 optlen);
7481 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 case SCTP_INITMSG:
7483 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7484 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485 case SCTP_GET_PEER_ADDRS:
7486 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7487 optlen);
7488 break;
7489 case SCTP_GET_LOCAL_ADDRS:
7490 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7491 optlen);
7492 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04007493 case SCTP_SOCKOPT_CONNECTX3:
7494 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7495 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496 case SCTP_DEFAULT_SEND_PARAM:
7497 retval = sctp_getsockopt_default_send_param(sk, len,
7498 optval, optlen);
7499 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02007500 case SCTP_DEFAULT_SNDINFO:
7501 retval = sctp_getsockopt_default_sndinfo(sk, len,
7502 optval, optlen);
7503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 case SCTP_PRIMARY_ADDR:
7505 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7506 break;
7507 case SCTP_NODELAY:
7508 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7509 break;
7510 case SCTP_RTOINFO:
7511 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7512 break;
7513 case SCTP_ASSOCINFO:
7514 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7515 break;
7516 case SCTP_I_WANT_MAPPED_V4_ADDR:
7517 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7518 break;
7519 case SCTP_MAXSEG:
7520 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7521 break;
7522 case SCTP_GET_PEER_ADDR_INFO:
7523 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7524 optlen);
7525 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08007526 case SCTP_ADAPTATION_LAYER:
7527 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 optlen);
7529 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08007530 case SCTP_CONTEXT:
7531 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7532 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007533 case SCTP_FRAGMENT_INTERLEAVE:
7534 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7535 optlen);
7536 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07007537 case SCTP_PARTIAL_DELIVERY_POINT:
7538 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7539 optlen);
7540 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07007541 case SCTP_MAX_BURST:
7542 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7543 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007544 case SCTP_AUTH_KEY:
7545 case SCTP_AUTH_CHUNK:
7546 case SCTP_AUTH_DELETE_KEY:
Xin Long601590e2018-03-14 19:05:32 +08007547 case SCTP_AUTH_DEACTIVATE_KEY:
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007548 retval = -EOPNOTSUPP;
7549 break;
7550 case SCTP_HMAC_IDENT:
7551 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7552 break;
7553 case SCTP_AUTH_ACTIVE_KEY:
7554 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7555 break;
7556 case SCTP_PEER_AUTH_CHUNKS:
7557 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7558 optlen);
7559 break;
7560 case SCTP_LOCAL_AUTH_CHUNKS:
7561 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7562 optlen);
7563 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08007564 case SCTP_GET_ASSOC_NUMBER:
7565 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7566 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00007567 case SCTP_GET_ASSOC_ID_LIST:
7568 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7569 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09007570 case SCTP_AUTO_ASCONF:
7571 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7572 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00007573 case SCTP_PEER_ADDR_THLDS:
7574 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7575 break;
Michele Baldessari196d6752012-12-01 04:49:42 +00007576 case SCTP_GET_ASSOC_STATS:
7577 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7578 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02007579 case SCTP_RECVRCVINFO:
7580 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7581 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007582 case SCTP_RECVNXTINFO:
7583 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7584 break;
Xin Long28aa4c22016-07-09 19:47:40 +08007585 case SCTP_PR_SUPPORTED:
7586 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7587 break;
Xin Longf959fb42016-07-09 19:47:41 +08007588 case SCTP_DEFAULT_PRINFO:
7589 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7590 optlen);
7591 break;
Xin Long826d2532016-07-09 19:47:42 +08007592 case SCTP_PR_ASSOC_STATUS:
7593 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7594 optlen);
7595 break;
Xin Longd229d482017-04-01 17:07:46 +08007596 case SCTP_PR_STREAM_STATUS:
7597 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7598 optlen);
7599 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08007600 case SCTP_RECONFIG_SUPPORTED:
7601 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7602 optlen);
7603 break;
Xin Long9fb657a2017-01-18 00:44:46 +08007604 case SCTP_ENABLE_STREAM_RESET:
7605 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7606 optlen);
7607 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007608 case SCTP_STREAM_SCHEDULER:
7609 retval = sctp_getsockopt_scheduler(sk, len, optval,
7610 optlen);
7611 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007612 case SCTP_STREAM_SCHEDULER_VALUE:
7613 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7614 optlen);
7615 break;
Xin Long772a5862017-12-08 21:03:58 +08007616 case SCTP_INTERLEAVING_SUPPORTED:
7617 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7618 optlen);
7619 break;
Xin Longb0e9a2f2018-06-28 15:31:00 +08007620 case SCTP_REUSE_PORT:
7621 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
7622 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623 default:
7624 retval = -ENOPROTOOPT;
7625 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627
wangweidong048ed4b2014-01-21 15:44:11 +08007628 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629 return retval;
7630}
7631
Craig Gallek086c6532016-02-10 11:50:35 -05007632static int sctp_hash(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633{
7634 /* STUB */
Craig Gallek086c6532016-02-10 11:50:35 -05007635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636}
7637
7638static void sctp_unhash(struct sock *sk)
7639{
7640 /* STUB */
7641}
7642
7643/* Check if port is acceptable. Possibly find first available port.
7644 *
7645 * The port hash table (contained in the 'global' SCTP protocol storage
7646 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7647 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7648 * list (the list number is the port number hashed out, so as you
7649 * would expect from a hash function, all the ports in a given list have
7650 * such a number that hashes out to the same list number; you were
7651 * expecting that, right?); so each list has a set of ports, with a
7652 * link to the socket (struct sock) that uses it, the port number and
7653 * a fastreuse flag (FIXME: NPI ipg).
7654 */
7655static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007656 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657
7658static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
7659{
Xin Longb0e9a2f2018-06-28 15:31:00 +08007660 bool reuse = (sk->sk_reuse || sctp_sk(sk)->reuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661 struct sctp_bind_hashbucket *head; /* hash list */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007662 struct sctp_bind_bucket *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663 unsigned short snum;
7664 int ret;
7665
Al Viro04afd8b2006-11-20 17:02:01 -08007666 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667
Daniel Borkmannbb333812013-06-28 19:49:40 +02007668 pr_debug("%s: begins, snum:%d\n", __func__, snum);
7669
wangweidong79b91132014-01-21 15:44:07 +08007670 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671
7672 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07007673 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007674 int low, high, remaining, index;
7675 unsigned int rover;
WANG Cong122ff242014-05-12 16:04:53 -07007676 struct net *net = sock_net(sk);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007677
WANG Cong122ff242014-05-12 16:04:53 -07007678 inet_get_local_port_range(net, &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007679 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05007680 rover = prandom_u32() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682 do {
7683 rover++;
7684 if ((rover < low) || (rover > high))
7685 rover = low;
WANG Cong122ff242014-05-12 16:04:53 -07007686 if (inet_is_local_reserved_port(net, rover))
Amerigo Wange3826f12010-05-05 00:27:06 +00007687 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007688 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689 head = &sctp_port_hashtable[index];
wangweidong3c8e43b2014-01-21 15:44:08 +08007690 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007691 sctp_for_each_hentry(pp, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007692 if ((pp->port == rover) &&
7693 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694 goto next;
7695 break;
7696 next:
wangweidong3c8e43b2014-01-21 15:44:08 +08007697 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699
7700 /* Exhausted local port range during search? */
7701 ret = 1;
7702 if (remaining <= 0)
7703 goto fail;
7704
7705 /* OK, here is the one we will use. HEAD (the port
7706 * hash table list entry) is non-NULL and we hold it's
7707 * mutex.
7708 */
7709 snum = rover;
7710 } else {
7711 /* We are given an specific port number; we verify
7712 * that it is not being used. If it is used, we will
7713 * exahust the search in the hash list corresponding
7714 * to the port number (snum) - we detect that with the
7715 * port iterator, pp being NULL.
7716 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007717 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
wangweidong3c8e43b2014-01-21 15:44:08 +08007718 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007719 sctp_for_each_hentry(pp, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007720 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007721 goto pp_found;
7722 }
7723 }
7724 pp = NULL;
7725 goto pp_not_found;
7726pp_found:
7727 if (!hlist_empty(&pp->owner)) {
7728 /* We had a port hash table hit - there is an
7729 * available port (pp != NULL) and it is being
7730 * used by other socket (pp->owner not empty); that other
7731 * socket is going to be sk2.
7732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734
Daniel Borkmannbb333812013-06-28 19:49:40 +02007735 pr_debug("%s: found a possible match\n", __func__);
7736
Xin Longb0e9a2f2018-06-28 15:31:00 +08007737 if (pp->fastreuse && reuse && sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738 goto success;
7739
7740 /* Run through the list of sockets bound to the port
7741 * (pp->port) [via the pointers bind_next and
7742 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
7743 * we get the endpoint they describe and run through
7744 * the endpoint's list of IP (v4 or v6) addresses,
7745 * comparing each of the addresses with the address of
7746 * the socket sk. If we find a match, then that means
7747 * that this port/socket (sk) combination are already
7748 * in an endpoint.
7749 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007750 sk_for_each_bound(sk2, &pp->owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751 struct sctp_endpoint *ep2;
7752 ep2 = sctp_sk(sk2)->ep;
7753
Vlad Yasevich4e540642008-07-18 23:06:32 -07007754 if (sk == sk2 ||
Xin Longb0e9a2f2018-06-28 15:31:00 +08007755 (reuse && (sk2->sk_reuse || sctp_sk(sk2)->reuse) &&
Vlad Yasevich4e540642008-07-18 23:06:32 -07007756 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757 continue;
7758
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07007759 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
7760 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007761 ret = (long)sk2;
7762 goto fail_unlock;
7763 }
7764 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02007765
7766 pr_debug("%s: found a match\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767 }
7768pp_not_found:
7769 /* If there was a hash table miss, create a new port. */
7770 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007771 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772 goto fail_unlock;
7773
7774 /* In either case (hit or miss), make sure fastreuse is 1 only
7775 * if sk->sk_reuse is too (that is, if the caller requested
7776 * SO_REUSEADDR on this socket -sk-).
7777 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007778 if (hlist_empty(&pp->owner)) {
Xin Longb0e9a2f2018-06-28 15:31:00 +08007779 if (reuse && sk->sk_state != SCTP_SS_LISTENING)
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007780 pp->fastreuse = 1;
7781 else
7782 pp->fastreuse = 0;
7783 } else if (pp->fastreuse &&
Xin Longb0e9a2f2018-06-28 15:31:00 +08007784 (!reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785 pp->fastreuse = 0;
7786
7787 /* We are set, so fill up all the data in the hash table
7788 * entry, tie the socket list information with the rest of the
7789 * sockets FIXME: Blurry, NPI (ipg).
7790 */
7791success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007793 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794 sk_add_bind_node(sk, &pp->owner);
7795 sctp_sk(sk)->bind_hash = pp;
7796 }
7797 ret = 0;
7798
7799fail_unlock:
wangweidong3c8e43b2014-01-21 15:44:08 +08007800 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801
7802fail:
wangweidong79b91132014-01-21 15:44:07 +08007803 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804 return ret;
7805}
7806
7807/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
7808 * port is requested.
7809 */
7810static int sctp_get_port(struct sock *sk, unsigned short snum)
7811{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812 union sctp_addr addr;
7813 struct sctp_af *af = sctp_sk(sk)->pf->af;
7814
7815 /* Set up a dummy address struct from the sk. */
7816 af->from_sk(&addr, sk);
7817 addr.v4.sin_port = htons(snum);
7818
7819 /* Note: sk->sk_num gets filled in if ephemeral port request. */
Daniel Borkmann62208f12013-06-25 18:17:30 +02007820 return !!sctp_get_port_local(sk, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821}
7822
7823/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824 * Move a socket to LISTENING state.
7825 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02007826static int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007828 struct sctp_sock *sp = sctp_sk(sk);
7829 struct sctp_endpoint *ep = sp->ep;
Herbert Xu5821c762016-01-24 21:20:12 +08007830 struct crypto_shash *tfm = NULL;
Neil Horman3c681982012-10-24 09:20:03 +00007831 char alg[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832
7833 /* Allocate HMAC for generating cookie. */
Neil Horman3c681982012-10-24 09:20:03 +00007834 if (!sp->hmac && sp->sctp_hmac_alg) {
7835 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
Herbert Xu5821c762016-01-24 21:20:12 +08007836 tfm = crypto_alloc_shash(alg, 0, 0);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07007837 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00007838 net_info_ratelimited("failed to load transform for %s: %ld\n",
Neil Horman3c681982012-10-24 09:20:03 +00007839 sp->sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007840 return -ENOSYS;
7841 }
7842 sctp_sk(sk)->hmac = tfm;
7843 }
7844
7845 /*
7846 * If a bind() or sctp_bindx() is not called prior to a listen()
7847 * call that allows new associations to be accepted, the system
7848 * picks an ephemeral port and will choose an address set equivalent
7849 * to binding with a wildcard address.
7850 *
7851 * This is not currently spelled out in the SCTP sockets
7852 * extensions draft, but follows the practice as seen in TCP
7853 * sockets.
7854 *
7855 */
Yafang Shaocbabf462017-12-20 11:12:54 +08007856 inet_sk_set_state(sk, SCTP_SS_LISTENING);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007857 if (!ep->base.bind_addr.port) {
7858 if (sctp_autobind(sk))
7859 return -EAGAIN;
7860 } else {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007861 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08007862 inet_sk_set_state(sk, SCTP_SS_CLOSED);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007863 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864 }
7865 }
7866
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007867 sk->sk_max_ack_backlog = backlog;
7868 sctp_hash_endpoint(ep);
7869 return 0;
7870}
7871
7872/*
7873 * 4.1.3 / 5.1.3 listen()
7874 *
7875 * By default, new associations are not accepted for UDP style sockets.
7876 * An application uses listen() to mark a socket as being able to
7877 * accept new associations.
7878 *
7879 * On TCP style sockets, applications use listen() to ready the SCTP
7880 * endpoint for accepting inbound associations.
7881 *
7882 * On both types of endpoints a backlog of '0' disables listening.
7883 *
7884 * Move a socket to LISTENING state.
7885 */
7886int sctp_inet_listen(struct socket *sock, int backlog)
7887{
7888 struct sock *sk = sock->sk;
7889 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7890 int err = -EINVAL;
7891
7892 if (unlikely(backlog < 0))
7893 return err;
7894
wangweidong048ed4b2014-01-21 15:44:11 +08007895 lock_sock(sk);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007896
7897 /* Peeled-off sockets are not allowed to listen(). */
7898 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
7899 goto out;
7900
7901 if (sock->state != SS_UNCONNECTED)
7902 goto out;
7903
Xin Long34b27892017-04-06 13:10:52 +08007904 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
7905 goto out;
7906
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007907 /* If backlog is zero, disable listening. */
7908 if (!backlog) {
7909 if (sctp_sstate(sk, CLOSED))
7910 goto out;
7911
7912 err = 0;
7913 sctp_unhash_endpoint(ep);
7914 sk->sk_state = SCTP_SS_CLOSED;
Xin Longb0e9a2f2018-06-28 15:31:00 +08007915 if (sk->sk_reuse || sctp_sk(sk)->reuse)
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007916 sctp_sk(sk)->bind_hash->fastreuse = 1;
7917 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007918 }
7919
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007920 /* If we are already listening, just update the backlog */
7921 if (sctp_sstate(sk, LISTENING))
7922 sk->sk_max_ack_backlog = backlog;
7923 else {
7924 err = sctp_listen_start(sk, backlog);
7925 if (err)
7926 goto out;
7927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007929 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930out:
wangweidong048ed4b2014-01-21 15:44:11 +08007931 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933}
7934
7935/*
7936 * This function is done by modeling the current datagram_poll() and the
7937 * tcp_poll(). Note that, based on these implementations, we don't
7938 * lock the socket in this function, even though it seems that,
7939 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08007940 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941 * in this place. We assume that we don't need locks either until proven
7942 * otherwise.
7943 *
7944 * Another thing to note is that we include the Async I/O support
7945 * here, again, by modeling the current TCP/UDP code. We don't have
7946 * a good way to test with it yet.
7947 */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07007948__poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949{
7950 struct sock *sk = sock->sk;
7951 struct sctp_sock *sp = sctp_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -04007952 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07007954 poll_wait(file, sk_sleep(sk), wait);
7955
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03007956 sock_rps_record_flow(sk);
7957
Linus Torvalds1da177e2005-04-16 15:20:36 -07007958 /* A TCP-style listening socket becomes readable when the accept queue
7959 * is not empty.
7960 */
7961 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
7962 return (!list_empty(&sp->ep->asocs)) ?
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007963 (EPOLLIN | EPOLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964
7965 mask = 0;
7966
7967 /* Is there any exceptional events? */
7968 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007969 mask |= EPOLLERR |
7970 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
Davide Libenzif348d702006-03-25 03:07:39 -08007971 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007972 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007974 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007975
7976 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00007977 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007978 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979
7980 /* The association is either gone or not ready. */
7981 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
7982 return mask;
7983
7984 /* Is it writable? */
7985 if (sctp_writeable(sk)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007986 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007987 } else {
Eric Dumazet9cd3e072015-11-29 20:03:10 -08007988 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989 /*
7990 * Since the socket is not locked, the buffer
7991 * might be made available after the writeable check and
7992 * before the bit is set. This could cause a lost I/O
7993 * signal. tcp_poll() has a race breaker for this race
7994 * condition. Based on their implementation, we put
7995 * in the following code to cover it as well.
7996 */
7997 if (sctp_writeable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007998 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999 }
8000 return mask;
8001}
8002
8003/********************************************************************
8004 * 2nd Level Abstractions
8005 ********************************************************************/
8006
8007static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008008 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008009{
8010 struct sctp_bind_bucket *pp;
8011
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08008012 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07008014 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008015 pp->port = snum;
8016 pp->fastreuse = 0;
8017 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008018 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05008019 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008020 }
8021 return pp;
8022}
8023
8024/* Caller must hold hashbucket lock for this tb with local BH disabled */
8025static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
8026{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07008027 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05008028 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029 kmem_cache_free(sctp_bucket_cachep, pp);
8030 SCTP_DBG_OBJCNT_DEC(bind_bucket);
8031 }
8032}
8033
8034/* Release this socket's reference to a local port. */
8035static inline void __sctp_put_port(struct sock *sk)
8036{
8037 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008038 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
8039 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07008040 struct sctp_bind_bucket *pp;
8041
wangweidong3c8e43b2014-01-21 15:44:08 +08008042 spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008043 pp = sctp_sk(sk)->bind_hash;
8044 __sk_del_bind_node(sk);
8045 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008046 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008047 sctp_bucket_destroy(pp);
wangweidong3c8e43b2014-01-21 15:44:08 +08008048 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008049}
8050
8051void sctp_put_port(struct sock *sk)
8052{
wangweidong79b91132014-01-21 15:44:07 +08008053 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008054 __sctp_put_port(sk);
wangweidong79b91132014-01-21 15:44:07 +08008055 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056}
8057
8058/*
8059 * The system picks an ephemeral port and choose an address set equivalent
8060 * to binding with a wildcard address.
8061 * One of those addresses will be the primary address for the association.
8062 * This automatically enables the multihoming capability of SCTP.
8063 */
8064static int sctp_autobind(struct sock *sk)
8065{
8066 union sctp_addr autoaddr;
8067 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08008068 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008069
8070 /* Initialize a local sockaddr structure to INADDR_ANY. */
8071 af = sctp_sk(sk)->pf->af;
8072
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008073 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008074 af->inaddr_any(&autoaddr, port);
8075
8076 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
8077}
8078
8079/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
8080 *
8081 * From RFC 2292
8082 * 4.2 The cmsghdr Structure *
8083 *
8084 * When ancillary data is sent or received, any number of ancillary data
8085 * objects can be specified by the msg_control and msg_controllen members of
8086 * the msghdr structure, because each object is preceded by
8087 * a cmsghdr structure defining the object's length (the cmsg_len member).
8088 * Historically Berkeley-derived implementations have passed only one object
8089 * at a time, but this API allows multiple objects to be
8090 * passed in a single call to sendmsg() or recvmsg(). The following example
8091 * shows two ancillary data objects in a control buffer.
8092 *
8093 * |<--------------------------- msg_controllen -------------------------->|
8094 * | |
8095 *
8096 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
8097 *
8098 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
8099 * | | |
8100 *
8101 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
8102 *
8103 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
8104 * | | | | |
8105 *
8106 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8107 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
8108 *
8109 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
8110 *
8111 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8112 * ^
8113 * |
8114 *
8115 * msg_control
8116 * points here
8117 */
Xin Longa05437a2017-08-11 10:23:48 +08008118static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008119{
Vlad Yasevichab38fb02008-04-12 18:40:06 -07008120 struct msghdr *my_msg = (struct msghdr *)msg;
Xin Longa05437a2017-08-11 10:23:48 +08008121 struct cmsghdr *cmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008122
Gu Zhengf95b4142014-12-11 11:22:04 +08008123 for_each_cmsghdr(cmsg, my_msg) {
Vlad Yasevichab38fb02008-04-12 18:40:06 -07008124 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008125 return -EINVAL;
8126
8127 /* Should we parse this header or ignore? */
8128 if (cmsg->cmsg_level != IPPROTO_SCTP)
8129 continue;
8130
8131 /* Strictly check lengths following example in SCM code. */
8132 switch (cmsg->cmsg_type) {
8133 case SCTP_INIT:
8134 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008135 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008136 *
8137 * This cmsghdr structure provides information for
8138 * initializing new SCTP associations with sendmsg().
8139 * The SCTP_INITMSG socket option uses this same data
8140 * structure. This structure is not used for
8141 * recvmsg().
8142 *
8143 * cmsg_level cmsg_type cmsg_data[]
8144 * ------------ ------------ ----------------------
8145 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
8146 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008147 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008148 return -EINVAL;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008149
8150 cmsgs->init = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151 break;
8152
8153 case SCTP_SNDRCV:
8154 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008155 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008156 *
8157 * This cmsghdr structure specifies SCTP options for
8158 * sendmsg() and describes SCTP header information
8159 * about a received message through recvmsg().
8160 *
8161 * cmsg_level cmsg_type cmsg_data[]
8162 * ------------ ------------ ----------------------
8163 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
8164 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008165 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166 return -EINVAL;
8167
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008168 cmsgs->srinfo = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008170 if (cmsgs->srinfo->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07008171 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Long49102802018-03-05 20:44:20 +08008172 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8173 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008174 return -EINVAL;
8175 break;
8176
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008177 case SCTP_SNDINFO:
8178 /* SCTP Socket API Extension
8179 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
8180 *
8181 * This cmsghdr structure specifies SCTP options for
8182 * sendmsg(). This structure and SCTP_RCVINFO replaces
8183 * SCTP_SNDRCV which has been deprecated.
8184 *
8185 * cmsg_level cmsg_type cmsg_data[]
8186 * ------------ ------------ ---------------------
8187 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
8188 */
8189 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
8190 return -EINVAL;
8191
8192 cmsgs->sinfo = CMSG_DATA(cmsg);
8193
8194 if (cmsgs->sinfo->snd_flags &
8195 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Long49102802018-03-05 20:44:20 +08008196 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8197 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008198 return -EINVAL;
8199 break;
Xin Longed63afb2018-03-05 20:44:18 +08008200 case SCTP_PRINFO:
8201 /* SCTP Socket API Extension
8202 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
8203 *
8204 * This cmsghdr structure specifies SCTP options for sendmsg().
8205 *
8206 * cmsg_level cmsg_type cmsg_data[]
8207 * ------------ ------------ ---------------------
8208 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
8209 */
8210 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
8211 return -EINVAL;
8212
8213 cmsgs->prinfo = CMSG_DATA(cmsg);
8214 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
8215 return -EINVAL;
8216
8217 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
8218 cmsgs->prinfo->pr_value = 0;
8219 break;
Xin Long3ff547c2018-03-14 19:05:31 +08008220 case SCTP_AUTHINFO:
8221 /* SCTP Socket API Extension
8222 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
8223 *
8224 * This cmsghdr structure specifies SCTP options for sendmsg().
8225 *
8226 * cmsg_level cmsg_type cmsg_data[]
8227 * ------------ ------------ ---------------------
8228 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
8229 */
8230 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
8231 return -EINVAL;
8232
8233 cmsgs->authinfo = CMSG_DATA(cmsg);
8234 break;
Xin Long2c0dbaa2018-03-05 20:44:19 +08008235 case SCTP_DSTADDRV4:
8236 case SCTP_DSTADDRV6:
8237 /* SCTP Socket API Extension
8238 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
8239 *
8240 * This cmsghdr structure specifies SCTP options for sendmsg().
8241 *
8242 * cmsg_level cmsg_type cmsg_data[]
8243 * ------------ ------------ ---------------------
8244 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
8245 * ------------ ------------ ---------------------
8246 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
8247 */
8248 cmsgs->addrs_msg = my_msg;
8249 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008250 default:
8251 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07008252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253 }
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008254
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255 return 0;
8256}
8257
8258/*
8259 * Wait for a packet..
8260 * Note: This function is the same function as in core/datagram.c
8261 * with a few modifications to make lksctp work.
8262 */
wangweidong26ac8e52013-12-23 12:16:51 +08008263static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264{
8265 int error;
8266 DEFINE_WAIT(wait);
8267
Eric Dumazetaa395142010-04-20 13:03:51 +00008268 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269
8270 /* Socket errors? */
8271 error = sock_error(sk);
8272 if (error)
8273 goto out;
8274
8275 if (!skb_queue_empty(&sk->sk_receive_queue))
8276 goto ready;
8277
8278 /* Socket shut down? */
8279 if (sk->sk_shutdown & RCV_SHUTDOWN)
8280 goto out;
8281
8282 /* Sequenced packets can come disconnected. If so we report the
8283 * problem.
8284 */
8285 error = -ENOTCONN;
8286
8287 /* Is there a good reason to think that we may receive some data? */
8288 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8289 goto out;
8290
8291 /* Handle signals. */
8292 if (signal_pending(current))
8293 goto interrupted;
8294
8295 /* Let another process have a go. Since we are going to sleep
8296 * anyway. Note: This may cause odd behaviors if the message
8297 * does not fit in the user's buffer, but this seems to be the
8298 * only way to honor MSG_DONTWAIT realistically.
8299 */
wangweidong048ed4b2014-01-21 15:44:11 +08008300 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301 *timeo_p = schedule_timeout(*timeo_p);
wangweidong048ed4b2014-01-21 15:44:11 +08008302 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008303
8304ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00008305 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008306 return 0;
8307
8308interrupted:
8309 error = sock_intr_errno(*timeo_p);
8310
8311out:
Eric Dumazetaa395142010-04-20 13:03:51 +00008312 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313 *err = error;
8314 return error;
8315}
8316
8317/* Receive a datagram.
8318 * Note: This is pretty much the same routine as in core/datagram.c
8319 * with a few changes to make lksctp work.
8320 */
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02008321struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8322 int noblock, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008323{
8324 int error;
8325 struct sk_buff *skb;
8326 long timeo;
8327
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328 timeo = sock_rcvtimeo(sk, noblock);
8329
Daniel Borkmannbb333812013-06-28 19:49:40 +02008330 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8331 MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008332
8333 do {
8334 /* Again only user level code calls this function,
8335 * so nothing interrupt level
8336 * will suddenly eat the receive_queue.
8337 *
8338 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00008339 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008340 */
8341 if (flags & MSG_PEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008342 skb = skb_peek(&sk->sk_receive_queue);
8343 if (skb)
Reshetova, Elena63354792017-06-30 13:07:58 +03008344 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008345 } else {
Marcelo Ricardo Leitner311b2172016-04-13 19:12:29 -03008346 skb = __skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008347 }
8348
8349 if (skb)
8350 return skb;
8351
Neil Horman6736dc32005-12-02 20:30:06 -08008352 /* Caller is allowed not to check sk->sk_err before calling. */
8353 error = sock_error(sk);
8354 if (error)
8355 goto no_packet;
8356
Linus Torvalds1da177e2005-04-16 15:20:36 -07008357 if (sk->sk_shutdown & RCV_SHUTDOWN)
8358 break;
8359
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07008360 if (sk_can_busy_loop(sk)) {
8361 sk_busy_loop(sk, noblock);
8362
8363 if (!skb_queue_empty(&sk->sk_receive_queue))
8364 continue;
8365 }
Neil Horman8465a5f2014-04-17 15:26:51 -04008366
Linus Torvalds1da177e2005-04-16 15:20:36 -07008367 /* User doesn't want to wait. */
8368 error = -EAGAIN;
8369 if (!timeo)
8370 goto no_packet;
8371 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8372
8373 return NULL;
8374
8375no_packet:
8376 *err = error;
8377 return NULL;
8378}
8379
8380/* If sndbuf has changed, wake up per association sndbuf waiters. */
8381static void __sctp_write_space(struct sctp_association *asoc)
8382{
8383 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384
Eric Dumazetceb5d582015-11-29 20:03:11 -08008385 if (sctp_wspace(asoc) <= 0)
8386 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008387
Eric Dumazetceb5d582015-11-29 20:03:11 -08008388 if (waitqueue_active(&asoc->wait))
8389 wake_up_interruptible(&asoc->wait);
Eric Dumazeteaefd112011-02-18 03:26:36 +00008390
Eric Dumazetceb5d582015-11-29 20:03:11 -08008391 if (sctp_writeable(sk)) {
8392 struct socket_wq *wq;
8393
8394 rcu_read_lock();
8395 wq = rcu_dereference(sk->sk_wq);
8396 if (wq) {
8397 if (waitqueue_active(&wq->wait))
8398 wake_up_interruptible(&wq->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008399
8400 /* Note that we try to include the Async I/O support
8401 * here by modeling from the current TCP/UDP code.
8402 * We have not tested with it yet.
8403 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00008404 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Eric Dumazetceb5d582015-11-29 20:03:11 -08008405 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008406 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08008407 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408 }
8409}
8410
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008411static void sctp_wake_up_waiters(struct sock *sk,
8412 struct sctp_association *asoc)
8413{
8414 struct sctp_association *tmp = asoc;
8415
8416 /* We do accounting for the sndbuf space per association,
8417 * so we only need to wake our own association.
8418 */
8419 if (asoc->ep->sndbuf_policy)
8420 return __sctp_write_space(asoc);
8421
Daniel Borkmann1e1cdf82014-04-09 16:10:20 +02008422 /* If association goes down and is just flushing its
8423 * outq, then just normally notify others.
8424 */
8425 if (asoc->base.dead)
8426 return sctp_write_space(sk);
8427
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008428 /* Accounting for the sndbuf space is per socket, so we
8429 * need to wake up others, try to be fair and in case of
8430 * other associations, let them have a go first instead
8431 * of just doing a sctp_write_space() call.
8432 *
8433 * Note that we reach sctp_wake_up_waiters() only when
8434 * associations free up queued chunks, thus we are under
8435 * lock and the list of associations on a socket is
8436 * guaranteed not to change.
8437 */
8438 for (tmp = list_next_entry(tmp, asocs); 1;
8439 tmp = list_next_entry(tmp, asocs)) {
8440 /* Manually skip the head element. */
8441 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8442 continue;
8443 /* Wake up association. */
8444 __sctp_write_space(tmp);
8445 /* We've reached the end. */
8446 if (tmp == asoc)
8447 break;
8448 }
8449}
8450
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451/* Do accounting for the sndbuf space.
8452 * Decrement the used sndbuf space of the corresponding association by the
8453 * data size which was just transmitted(freed).
8454 */
8455static void sctp_wfree(struct sk_buff *skb)
8456{
Daniel Borkmannf869c912014-11-20 01:54:48 +01008457 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8458 struct sctp_association *asoc = chunk->asoc;
8459 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008460
Neil Horman4eb701d2005-04-28 12:02:04 -07008461 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
8462 sizeof(struct sk_buff) +
8463 sizeof(struct sctp_chunk);
8464
Reshetova, Elena14afee42017-06-30 13:08:00 +03008465 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
Neil Horman4eb701d2005-04-28 12:02:04 -07008466
Neil Horman4d93df02007-08-15 16:07:44 -07008467 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008468 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
Neil Horman4d93df02007-08-15 16:07:44 -07008469 */
8470 sk->sk_wmem_queued -= skb->truesize;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008471 sk_mem_uncharge(sk, skb->truesize);
Neil Horman4d93df02007-08-15 16:07:44 -07008472
Xin Longec2e5062018-03-14 19:05:33 +08008473 if (chunk->shkey) {
8474 struct sctp_shared_key *shkey = chunk->shkey;
8475
8476 /* refcnt == 2 and !list_empty mean after this release, it's
8477 * not being used anywhere, and it's time to notify userland
8478 * that this shkey can be freed if it's been deactivated.
8479 */
8480 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
8481 refcount_read(&shkey->refcnt) == 2) {
8482 struct sctp_ulpevent *ev;
8483
8484 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
8485 SCTP_AUTH_FREE_KEY,
8486 GFP_KERNEL);
8487 if (ev)
8488 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
8489 }
Xin Long1b1e0bc2018-03-14 19:05:30 +08008490 sctp_auth_shkey_release(chunk->shkey);
Xin Longec2e5062018-03-14 19:05:33 +08008491 }
Xin Long1b1e0bc2018-03-14 19:05:30 +08008492
Neil Horman4eb701d2005-04-28 12:02:04 -07008493 sock_wfree(skb);
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008494 sctp_wake_up_waiters(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008495
8496 sctp_association_put(asoc);
8497}
8498
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008499/* Do accounting for the receive space on the socket.
8500 * Accounting for the association is done in ulpevent.c
8501 * We set this as a destructor for the cloned data skbs so that
8502 * accounting is done at the correct time.
8503 */
8504void sctp_sock_rfree(struct sk_buff *skb)
8505{
8506 struct sock *sk = skb->sk;
8507 struct sctp_ulpevent *event = sctp_skb2event(skb);
8508
8509 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07008510
8511 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008512 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07008513 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008514 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008515}
8516
8517
Linus Torvalds1da177e2005-04-16 15:20:36 -07008518/* Helper function to wait for space in the sndbuf. */
8519static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +08008520 size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008521{
8522 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523 long current_timeo = *timeo_p;
8524 DEFINE_WAIT(wait);
Xin Longa0ff6602018-01-15 17:01:36 +08008525 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008526
Daniel Borkmannbb333812013-06-28 19:49:40 +02008527 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8528 *timeo_p, msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008529
8530 /* Increment the association's refcnt. */
8531 sctp_association_hold(asoc);
8532
8533 /* Wait on the association specific sndbuf space. */
8534 for (;;) {
8535 prepare_to_wait_exclusive(&asoc->wait, &wait,
8536 TASK_INTERRUPTIBLE);
Xin Longca3af4d2017-11-15 16:55:54 +08008537 if (asoc->base.dead)
8538 goto do_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008539 if (!*timeo_p)
8540 goto do_nonblock;
Xin Longca3af4d2017-11-15 16:55:54 +08008541 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008542 goto do_error;
8543 if (signal_pending(current))
8544 goto do_interrupted;
8545 if (msg_len <= sctp_wspace(asoc))
8546 break;
8547
8548 /* Let another process have a go. Since we are going
8549 * to sleep anyway.
8550 */
wangweidong048ed4b2014-01-21 15:44:11 +08008551 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008552 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008553 lock_sock(sk);
Xin Longa0ff6602018-01-15 17:01:36 +08008554 if (sk != asoc->base.sk)
8555 goto do_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556
8557 *timeo_p = current_timeo;
8558 }
8559
8560out:
8561 finish_wait(&asoc->wait, &wait);
8562
8563 /* Release the association's refcnt. */
8564 sctp_association_put(asoc);
8565
8566 return err;
8567
Xin Longca3af4d2017-11-15 16:55:54 +08008568do_dead:
8569 err = -ESRCH;
8570 goto out;
8571
Linus Torvalds1da177e2005-04-16 15:20:36 -07008572do_error:
8573 err = -EPIPE;
8574 goto out;
8575
8576do_interrupted:
8577 err = sock_intr_errno(*timeo_p);
8578 goto out;
8579
8580do_nonblock:
8581 err = -EAGAIN;
8582 goto out;
8583}
8584
David S. Miller676d2362014-04-11 16:15:36 -04008585void sctp_data_ready(struct sock *sk)
Wei Yongjun561b1732010-04-28 08:47:18 +00008586{
David S. Miller7ef52732010-05-02 21:43:40 -07008587 struct socket_wq *wq;
8588
8589 rcu_read_lock();
8590 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08008591 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008592 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8593 EPOLLRDNORM | EPOLLRDBAND);
Wei Yongjun561b1732010-04-28 08:47:18 +00008594 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07008595 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00008596}
8597
Linus Torvalds1da177e2005-04-16 15:20:36 -07008598/* If socket sndbuf has changed, wake up all per association waiters. */
8599void sctp_write_space(struct sock *sk)
8600{
8601 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008602
8603 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07008604 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008605 __sctp_write_space(asoc);
8606 }
8607}
8608
8609/* Is there any sndbuf space available on the socket?
8610 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08008611 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612 * associations on the same socket. For a UDP-style socket with
8613 * multiple associations, it is possible for it to be "unwriteable"
8614 * prematurely. I assume that this is acceptable because
8615 * a premature "unwriteable" is better than an accidental "writeable" which
8616 * would cause an unwanted block under certain circumstances. For the 1-1
8617 * UDP-style sockets or TCP-style sockets, this code should work.
8618 * - Daisy
8619 */
8620static int sctp_writeable(struct sock *sk)
8621{
8622 int amt = 0;
8623
Eric Dumazet31e6d362009-06-17 19:05:41 -07008624 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008625 if (amt < 0)
8626 amt = 0;
8627 return amt;
8628}
8629
8630/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8631 * returns immediately with EINPROGRESS.
8632 */
8633static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8634{
8635 struct sock *sk = asoc->base.sk;
8636 int err = 0;
8637 long current_timeo = *timeo_p;
8638 DEFINE_WAIT(wait);
8639
Daniel Borkmannbb333812013-06-28 19:49:40 +02008640 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641
8642 /* Increment the association's refcnt. */
8643 sctp_association_hold(asoc);
8644
8645 for (;;) {
8646 prepare_to_wait_exclusive(&asoc->wait, &wait,
8647 TASK_INTERRUPTIBLE);
8648 if (!*timeo_p)
8649 goto do_nonblock;
8650 if (sk->sk_shutdown & RCV_SHUTDOWN)
8651 break;
8652 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
8653 asoc->base.dead)
8654 goto do_error;
8655 if (signal_pending(current))
8656 goto do_interrupted;
8657
8658 if (sctp_state(asoc, ESTABLISHED))
8659 break;
8660
8661 /* Let another process have a go. Since we are going
8662 * to sleep anyway.
8663 */
wangweidong048ed4b2014-01-21 15:44:11 +08008664 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008666 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008667
8668 *timeo_p = current_timeo;
8669 }
8670
8671out:
8672 finish_wait(&asoc->wait, &wait);
8673
8674 /* Release the association's refcnt. */
8675 sctp_association_put(asoc);
8676
8677 return err;
8678
8679do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08008680 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008681 err = -ETIMEDOUT;
8682 else
8683 err = -ECONNREFUSED;
8684 goto out;
8685
8686do_interrupted:
8687 err = sock_intr_errno(*timeo_p);
8688 goto out;
8689
8690do_nonblock:
8691 err = -EINPROGRESS;
8692 goto out;
8693}
8694
8695static int sctp_wait_for_accept(struct sock *sk, long timeo)
8696{
8697 struct sctp_endpoint *ep;
8698 int err = 0;
8699 DEFINE_WAIT(wait);
8700
8701 ep = sctp_sk(sk)->ep;
8702
8703
8704 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00008705 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008706 TASK_INTERRUPTIBLE);
8707
8708 if (list_empty(&ep->asocs)) {
wangweidong048ed4b2014-01-21 15:44:11 +08008709 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710 timeo = schedule_timeout(timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008711 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008712 }
8713
8714 err = -EINVAL;
8715 if (!sctp_sstate(sk, LISTENING))
8716 break;
8717
8718 err = 0;
8719 if (!list_empty(&ep->asocs))
8720 break;
8721
8722 err = sock_intr_errno(timeo);
8723 if (signal_pending(current))
8724 break;
8725
8726 err = -EAGAIN;
8727 if (!timeo)
8728 break;
8729 }
8730
Eric Dumazetaa395142010-04-20 13:03:51 +00008731 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008732
8733 return err;
8734}
8735
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02008736static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008737{
8738 DEFINE_WAIT(wait);
8739
8740 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00008741 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008742 if (list_empty(&sctp_sk(sk)->ep->asocs))
8743 break;
wangweidong048ed4b2014-01-21 15:44:11 +08008744 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008745 timeout = schedule_timeout(timeout);
wangweidong048ed4b2014-01-21 15:44:11 +08008746 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008747 } while (!signal_pending(current) && timeout);
8748
Eric Dumazetaa395142010-04-20 13:03:51 +00008749 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008750}
8751
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008752static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
8753{
8754 struct sk_buff *frag;
8755
8756 if (!skb->data_len)
8757 goto done;
8758
8759 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07008760 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008761 sctp_skb_set_owner_r_frag(frag, sk);
8762
8763done:
8764 sctp_skb_set_owner_r(skb, sk);
8765}
8766
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008767void sctp_copy_sock(struct sock *newsk, struct sock *sk,
8768 struct sctp_association *asoc)
8769{
8770 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08008771 struct inet_sock *newinet;
Richard Haines2277c7c2018-02-13 20:56:24 +00008772 struct sctp_sock *sp = sctp_sk(sk);
8773 struct sctp_endpoint *ep = sp->ep;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008774
8775 newsk->sk_type = sk->sk_type;
8776 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
8777 newsk->sk_flags = sk->sk_flags;
Marcelo Ricardo Leitner50a5ffb2015-12-04 15:14:05 -02008778 newsk->sk_tsflags = sk->sk_tsflags;
Tom Herbert28448b82014-05-23 08:47:19 -07008779 newsk->sk_no_check_tx = sk->sk_no_check_tx;
8780 newsk->sk_no_check_rx = sk->sk_no_check_rx;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008781 newsk->sk_reuse = sk->sk_reuse;
Xin Longb0e9a2f2018-06-28 15:31:00 +08008782 sctp_sk(newsk)->reuse = sp->reuse;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008783
8784 newsk->sk_shutdown = sk->sk_shutdown;
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02008785 newsk->sk_destruct = sctp_destruct_sock;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008786 newsk->sk_family = sk->sk_family;
8787 newsk->sk_protocol = IPPROTO_SCTP;
8788 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
8789 newsk->sk_sndbuf = sk->sk_sndbuf;
8790 newsk->sk_rcvbuf = sk->sk_rcvbuf;
8791 newsk->sk_lingertime = sk->sk_lingertime;
8792 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
8793 newsk->sk_sndtimeo = sk->sk_sndtimeo;
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03008794 newsk->sk_rxhash = sk->sk_rxhash;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008795
8796 newinet = inet_sk(newsk);
8797
8798 /* Initialize sk's sport, dport, rcv_saddr and daddr for
8799 * getsockname() and getpeername()
8800 */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008801 newinet->inet_sport = inet->inet_sport;
8802 newinet->inet_saddr = inet->inet_saddr;
8803 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
8804 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008805 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008806 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008807
8808 newinet->uc_ttl = inet->uc_ttl;
8809 newinet->mc_loop = 1;
8810 newinet->mc_ttl = 1;
8811 newinet->mc_index = 0;
8812 newinet->mc_list = NULL;
Marcelo Ricardo Leitner01ce63c2015-12-04 15:14:04 -02008813
8814 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
8815 net_enable_timestamp();
Marcelo Ricardo Leitner3538a5c2015-12-23 16:44:09 -02008816
Richard Haines2277c7c2018-02-13 20:56:24 +00008817 /* Set newsk security attributes from orginal sk and connection
8818 * security attribute from ep.
8819 */
8820 security_sctp_sk_clone(ep, sk, newsk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008821}
8822
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008823static inline void sctp_copy_descendant(struct sock *sk_to,
8824 const struct sock *sk_from)
8825{
8826 int ancestor_size = sizeof(struct inet_sock) +
8827 sizeof(struct sctp_sock) -
8828 offsetof(struct sctp_sock, auto_asconf_list);
8829
8830 if (sk_from->sk_family == PF_INET6)
8831 ancestor_size += sizeof(struct ipv6_pinfo);
8832
8833 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
8834}
8835
Linus Torvalds1da177e2005-04-16 15:20:36 -07008836/* Populate the fields of the newsk from the oldsk and migrate the assoc
8837 * and its messages to the newsk.
8838 */
8839static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
8840 struct sctp_association *assoc,
Xin Longb7ef2612017-08-11 10:23:50 +08008841 enum sctp_socket_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008842{
8843 struct sctp_sock *oldsp = sctp_sk(oldsk);
8844 struct sctp_sock *newsp = sctp_sk(newsk);
8845 struct sctp_bind_bucket *pp; /* hash list port iterator */
8846 struct sctp_endpoint *newep = newsp->ep;
8847 struct sk_buff *skb, *tmp;
8848 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08008849 struct sctp_bind_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850
8851 /* Migrate socket buffer sizes and all the socket level options to the
8852 * new socket.
8853 */
8854 newsk->sk_sndbuf = oldsk->sk_sndbuf;
8855 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
8856 /* Brute force copy old sctp opt. */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008857 sctp_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858
8859 /* Restore the ep value that was overwritten with the above structure
8860 * copy.
8861 */
8862 newsp->ep = newep;
8863 newsp->hmac = NULL;
8864
8865 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008866 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
8867 inet_sk(oldsk)->inet_num)];
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008868 spin_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008869 pp = sctp_sk(oldsk)->bind_hash;
8870 sk_add_bind_node(newsk, &pp->owner);
8871 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008872 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008873 spin_unlock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008875 /* Copy the bind_addr list from the original endpoint to the new
8876 * endpoint so that we can handle restarts properly
8877 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08008878 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
8879 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008880
Linus Torvalds1da177e2005-04-16 15:20:36 -07008881 /* Move any messages in the old socket's receive queue that are for the
8882 * peeled off association to the new socket's receive queue.
8883 */
8884 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
8885 event = sctp_skb2event(skb);
8886 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008887 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008889 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890 }
8891 }
8892
8893 /* Clean up any messages pending delivery due to partial
8894 * delivery. Three cases:
8895 * 1) No partial deliver; no work.
8896 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
8897 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
8898 */
8899 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008900 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008901
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008902 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008903 struct sk_buff_head *queue;
8904
8905 /* Decide which queue to move pd_lobby skbs to. */
8906 if (assoc->ulpq.pd_mode) {
8907 queue = &newsp->pd_lobby;
8908 } else
8909 queue = &newsk->sk_receive_queue;
8910
8911 /* Walk through the pd_lobby, looking for skbs that
8912 * need moved to the new socket.
8913 */
8914 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
8915 event = sctp_skb2event(skb);
8916 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008917 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008918 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008919 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008920 }
8921 }
8922
8923 /* Clear up any skbs waiting for the partial
8924 * delivery to finish.
8925 */
8926 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008927 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008928
8929 }
8930
Xin Long13228232017-12-08 21:04:09 +08008931 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008932
Linus Torvalds1da177e2005-04-16 15:20:36 -07008933 /* Set the type of socket to indicate that it is peeled off from the
8934 * original UDP-style socket or created with the accept() call on a
8935 * TCP-style socket..
8936 */
8937 newsp->type = type;
8938
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008939 /* Mark the new socket "in-use" by the user so that any packets
8940 * that may arrive on the association after we've moved it are
8941 * queued to the backlog. This prevents a potential race between
8942 * backlog processing on the old socket and new-packet processing
8943 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07008944 *
8945 * The caller has just allocated newsk so we can guarantee that other
8946 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008947 */
Zach Brown5131a182007-06-22 15:14:46 -07008948 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Xin Longd04adf12017-10-28 02:13:29 +08008949 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008950 sctp_assoc_migrate(assoc, newsk);
Xin Longd04adf12017-10-28 02:13:29 +08008951 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008952
8953 /* If the association on the newsk is already closed before accept()
8954 * is called, set RCV_SHUTDOWN flag.
8955 */
Xin Longd46e4162016-06-09 22:48:18 +08008956 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08008957 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008958 newsk->sk_shutdown |= RCV_SHUTDOWN;
Xin Longd46e4162016-06-09 22:48:18 +08008959 } else {
Yafang Shaocbabf462017-12-20 11:12:54 +08008960 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
Xin Longd46e4162016-06-09 22:48:18 +08008961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008962
wangweidong048ed4b2014-01-21 15:44:11 +08008963 release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008964}
8965
Neil Horman4d93df02007-08-15 16:07:44 -07008966
Linus Torvalds1da177e2005-04-16 15:20:36 -07008967/* This proto struct describes the ULP interface for SCTP. */
8968struct proto sctp_prot = {
8969 .name = "SCTP",
8970 .owner = THIS_MODULE,
8971 .close = sctp_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008972 .disconnect = sctp_disconnect,
8973 .accept = sctp_accept,
8974 .ioctl = sctp_ioctl,
8975 .init = sctp_init_sock,
8976 .destroy = sctp_destroy_sock,
8977 .shutdown = sctp_shutdown,
8978 .setsockopt = sctp_setsockopt,
8979 .getsockopt = sctp_getsockopt,
8980 .sendmsg = sctp_sendmsg,
8981 .recvmsg = sctp_recvmsg,
8982 .bind = sctp_bind,
8983 .backlog_rcv = sctp_backlog_rcv,
8984 .hash = sctp_hash,
8985 .unhash = sctp_unhash,
8986 .get_port = sctp_get_port,
8987 .obj_size = sizeof(struct sctp_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07008988 .useroffset = offsetof(struct sctp_sock, subscribe),
8989 .usersize = offsetof(struct sctp_sock, initmsg) -
8990 offsetof(struct sctp_sock, subscribe) +
8991 sizeof_field(struct sctp_sock, initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07008992 .sysctl_mem = sysctl_sctp_mem,
8993 .sysctl_rmem = sysctl_sctp_rmem,
8994 .sysctl_wmem = sysctl_sctp_wmem,
8995 .memory_pressure = &sctp_memory_pressure,
8996 .enter_memory_pressure = sctp_enter_memory_pressure,
8997 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008998 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008999};
9000
Eric Dumazetdfd56b82011-12-10 09:48:31 +00009001#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08009002
Eric Dumazet602dd622015-12-01 07:20:07 -08009003#include <net/transp_v6.h>
9004static void sctp_v6_destroy_sock(struct sock *sk)
9005{
9006 sctp_destroy_sock(sk);
9007 inet6_destroy_sock(sk);
9008}
9009
Linus Torvalds1da177e2005-04-16 15:20:36 -07009010struct proto sctpv6_prot = {
9011 .name = "SCTPv6",
9012 .owner = THIS_MODULE,
9013 .close = sctp_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009014 .disconnect = sctp_disconnect,
9015 .accept = sctp_accept,
9016 .ioctl = sctp_ioctl,
9017 .init = sctp_init_sock,
Eric Dumazet602dd622015-12-01 07:20:07 -08009018 .destroy = sctp_v6_destroy_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009019 .shutdown = sctp_shutdown,
9020 .setsockopt = sctp_setsockopt,
9021 .getsockopt = sctp_getsockopt,
9022 .sendmsg = sctp_sendmsg,
9023 .recvmsg = sctp_recvmsg,
9024 .bind = sctp_bind,
9025 .backlog_rcv = sctp_backlog_rcv,
9026 .hash = sctp_hash,
9027 .unhash = sctp_unhash,
9028 .get_port = sctp_get_port,
9029 .obj_size = sizeof(struct sctp6_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07009030 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
9031 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
9032 offsetof(struct sctp6_sock, sctp.subscribe) +
9033 sizeof_field(struct sctp6_sock, sctp.initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07009034 .sysctl_mem = sysctl_sctp_mem,
9035 .sysctl_rmem = sysctl_sctp_rmem,
9036 .sysctl_wmem = sysctl_sctp_wmem,
9037 .memory_pressure = &sctp_memory_pressure,
9038 .enter_memory_pressure = sctp_enter_memory_pressure,
9039 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08009040 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009041};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00009042#endif /* IS_ENABLED(CONFIG_IPV6) */