blob: 111ebd89f0ab412680c1e35eb7cf2239d6e4245c [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
8 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05009 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
13 *
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
22 * any later version.
23 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050024 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080031 * along with GNU CC; see the file COPYING. If not, see
32 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
34 * Please send any bug reports or fixes you make to the
35 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020036 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * Written or modified by:
39 * La Monte H.P. Yarroll <piggy@acm.org>
40 * Narasimha Budihal <narsi@refcode.org>
41 * Karl Knutson <karl@athena.chicago.il.us>
42 * Jon Grimm <jgrimm@us.ibm.com>
43 * Xingang Guo <xingang.guo@intel.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Sridhar Samudrala <samudrala@us.ibm.com>
46 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
47 * Ardelle Fan <ardelle.fan@intel.com>
48 * Ryan Layer <rmlayer@us.ibm.com>
49 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
50 * Kevin Gao <kevin.gao@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
52
Joe Perches145ce502010-08-24 13:21:08 +000053#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
Herbert Xu5821c762016-01-24 21:20:12 +080055#include <crypto/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/types.h>
57#include <linux/kernel.h>
58#include <linux/wait.h>
59#include <linux/time.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010060#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/ip.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080062#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/fcntl.h>
64#include <linux/poll.h>
65#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090066#include <linux/slab.h>
Al Viro56b31d12012-08-18 00:25:51 -040067#include <linux/file.h>
Daniel Borkmannffd59392014-02-17 12:11:11 +010068#include <linux/compat.h>
NeilBrown0eb71a92018-06-18 12:52:50 +100069#include <linux/rhashtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71#include <net/ip.h>
72#include <net/icmp.h>
73#include <net/route.h>
74#include <net/ipv6.h>
75#include <net/inet_common.h>
Neil Horman8465a5f2014-04-17 15:26:51 -040076#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#include <linux/socket.h> /* for sa_family_t */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040079#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <net/sock.h>
81#include <net/sctp/sctp.h>
82#include <net/sctp/sm.h>
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -030083#include <net/sctp/stream_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* Forward declarations for internal helper functions. */
Xin Longcd305c72018-10-17 03:07:51 +080086static bool sctp_writeable(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static void sctp_wfree(struct sk_buff *skb);
Xin Longcea0cc82017-11-15 16:57:26 +080088static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +080089 size_t msg_len);
wangweidong26ac8e52013-12-23 12:16:51 +080090static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
92static int sctp_wait_for_accept(struct sock *sk, long timeo);
93static void sctp_wait_for_close(struct sock *sk, long timeo);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +020094static void sctp_destruct_sock(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
96 union sctp_addr *addr, int len);
97static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
98static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
99static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
100static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
101static int sctp_send_asconf(struct sctp_association *asoc,
102 struct sctp_chunk *chunk);
103static int sctp_do_bind(struct sock *, union sctp_addr *, int);
104static int sctp_autobind(struct sock *sk);
Xin 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{
Xin Longcd305c72018-10-17 03:07:51 +0800122 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Xin Longcd305c72018-10-17 03:07:51 +0800124 return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used
125 : sk_stream_wspace(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128/* Increment the used sndbuf space count of the corresponding association by
129 * the size of the outgoing data chunk.
130 * Also, set the skb destructor for sndbuf accounting later.
131 *
132 * Since it is always 1-1 between chunk and skb, and also a new skb is always
133 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
134 * destructor in the data chunk skb for the purpose of the sndbuf space
135 * tracking.
136 */
137static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
138{
139 struct sctp_association *asoc = chunk->asoc;
140 struct sock *sk = asoc->base.sk;
141
142 /* The sndbuf space is tracked per association. */
143 sctp_association_hold(asoc);
144
Xin Long1b1e0bc2018-03-14 19:05:30 +0800145 if (chunk->shkey)
146 sctp_auth_shkey_hold(chunk->shkey);
147
Neil Horman4eb701d2005-04-28 12:02:04 -0700148 skb_set_owner_w(chunk->skb, sk);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 chunk->skb->destructor = sctp_wfree;
151 /* Save the chunk pointer in skb for sctp_wfree to use later. */
Daniel Borkmannf869c912014-11-20 01:54:48 +0100152 skb_shinfo(chunk->skb)->destructor_arg = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Reshetova, Elena14afee42017-06-30 13:08:00 +0300154 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Xin Long605c0ac2018-10-17 03:07:50 +0800155 asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk);
156 sk->sk_wmem_queued += chunk->skb->truesize + sizeof(struct sctp_chunk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800157 sk_mem_charge(sk, chunk->skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Xin Longd04adf12017-10-28 02:13:29 +0800160static void sctp_clear_owner_w(struct sctp_chunk *chunk)
161{
162 skb_orphan(chunk->skb);
163}
164
165static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
166 void (*cb)(struct sctp_chunk *))
167
168{
169 struct sctp_outq *q = &asoc->outqueue;
170 struct sctp_transport *t;
171 struct sctp_chunk *chunk;
172
173 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
174 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
175 cb(chunk);
176
Xin Longa8dd3972017-11-26 20:56:07 +0800177 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800178 cb(chunk);
179
Xin Longa8dd3972017-11-26 20:56:07 +0800180 list_for_each_entry(chunk, &q->sacked, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800181 cb(chunk);
182
Xin Longa8dd3972017-11-26 20:56:07 +0800183 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800184 cb(chunk);
185
186 list_for_each_entry(chunk, &q->out_chunk_list, list)
187 cb(chunk);
188}
189
Xin Long13228232017-12-08 21:04:09 +0800190static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
191 void (*cb)(struct sk_buff *, struct sock *))
192
193{
194 struct sk_buff *skb, *tmp;
195
196 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
197 cb(skb, sk);
198
199 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
200 cb(skb, sk);
201
202 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
203 cb(skb, sk);
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/* Verify that this is a valid address. */
207static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
208 int len)
209{
210 struct sctp_af *af;
211
212 /* Verify basic sockaddr. */
213 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
214 if (!af)
215 return -EINVAL;
216
217 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700218 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return -EINVAL;
220
221 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
222 return -EINVAL;
223
224 return 0;
225}
226
227/* Look up the association by its id. If this is not a UDP-style
228 * socket, the ID field is always ignored.
229 */
230struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
231{
232 struct sctp_association *asoc = NULL;
233
234 /* If this is not a UDP-style socket, assoc id should be ignored. */
235 if (!sctp_style(sk, UDP)) {
236 /* Return NULL if the socket state is not ESTABLISHED. It
237 * could be a TCP-style listening socket or a socket which
238 * hasn't yet called connect() to establish an association.
239 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -0300240 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return NULL;
242
243 /* Get the first and the only association from the list. */
244 if (!list_empty(&sctp_sk(sk)->ep->asocs))
245 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
246 struct sctp_association, asocs);
247 return asoc;
248 }
249
250 /* Otherwise this is a UDP-style socket. */
251 if (!id || (id == (sctp_assoc_t)-1))
252 return NULL;
253
254 spin_lock_bh(&sctp_assocs_id_lock);
255 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
256 spin_unlock_bh(&sctp_assocs_id_lock);
257
258 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
259 return NULL;
260
261 return asoc;
262}
263
264/* Look up the transport from an address and an assoc id. If both address and
265 * id are specified, the associations matching the address and the id should be
266 * the same.
267 */
268static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
269 struct sockaddr_storage *addr,
270 sctp_assoc_t id)
271{
272 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
Xin Long6f29a132017-01-24 14:01:53 +0800273 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 union sctp_addr *laddr = (union sctp_addr *)addr;
Xin Long6f29a132017-01-24 14:01:53 +0800275 struct sctp_transport *transport;
276
Xin Long912964e2017-02-07 20:56:08 +0800277 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
Xin Long6f29a132017-01-24 14:01:53 +0800278 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800281 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (!addr_asoc)
285 return NULL;
286
287 id_asoc = sctp_id2assoc(sk, id);
288 if (id_asoc && (id_asoc != addr_asoc))
289 return NULL;
290
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600291 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 (union sctp_addr *)addr);
293
294 return transport;
295}
296
297/* API 3.1.2 bind() - UDP Style Syntax
298 * The syntax of bind() is,
299 *
300 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
301 *
302 * sd - the socket descriptor returned by socket().
303 * addr - the address structure (struct sockaddr_in or struct
304 * sockaddr_in6 [RFC 2553]),
305 * addr_len - the size of the address structure.
306 */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200307static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 int retval = 0;
310
wangweidong048ed4b2014-01-21 15:44:11 +0800311 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Daniel Borkmannbb333812013-06-28 19:49:40 +0200313 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
314 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /* Disallow binding twice. */
317 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700318 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 addr_len);
320 else
321 retval = -EINVAL;
322
wangweidong048ed4b2014-01-21 15:44:11 +0800323 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 return retval;
326}
327
328static long sctp_get_port_local(struct sock *, union sctp_addr *);
329
330/* Verify this is a valid sockaddr. */
331static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
332 union sctp_addr *addr, int len)
333{
334 struct sctp_af *af;
335
336 /* Check minimum size. */
337 if (len < sizeof (struct sockaddr))
338 return NULL;
339
Xin Longc5006b82018-01-15 17:02:00 +0800340 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
341 return NULL;
342
Eric Dumazet81e98372018-04-08 07:52:08 -0700343 if (addr->sa.sa_family == AF_INET6) {
344 if (len < SIN6_LEN_RFC2133)
345 return NULL;
346 /* V4 mapped address are really of AF_INET family */
347 if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
348 !opt->pf->af_supported(AF_INET, opt))
349 return NULL;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /* If we get this far, af is valid. */
353 af = sctp_get_af_specific(addr->sa.sa_family);
354
355 if (len < af->sockaddr_len)
356 return NULL;
357
358 return af;
359}
360
361/* Bind a local address either to an endpoint or to an association. */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200362static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Eric W. Biederman35946982012-11-16 03:03:12 +0000364 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct sctp_sock *sp = sctp_sk(sk);
366 struct sctp_endpoint *ep = sp->ep;
367 struct sctp_bind_addr *bp = &ep->base.bind_addr;
368 struct sctp_af *af;
369 unsigned short snum;
370 int ret = 0;
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /* Common sockaddr verification. */
373 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700374 if (!af) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200375 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
376 __func__, sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700378 }
379
380 snum = ntohs(addr->v4.sin_port);
381
Daniel Borkmannbb333812013-06-28 19:49:40 +0200382 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
383 __func__, sk, &addr->sa, bp->port, snum, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 /* PF specific bind() address verification. */
386 if (!sp->pf->bind_verify(sp, addr))
387 return -EADDRNOTAVAIL;
388
Vlad Yasevich8b358052007-05-15 17:14:58 -0400389 /* We must either be unbound, or bind to the same port.
390 * It's OK to allow 0 ports if we are already bound.
391 * We'll just inhert an already bound port in this case
392 */
393 if (bp->port) {
394 if (!snum)
395 snum = bp->port;
396 else if (snum != bp->port) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200397 pr_debug("%s: new port %d doesn't match existing port "
398 "%d\n", __func__, snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400399 return -EINVAL;
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
Krister Johansen4548b682017-01-20 17:49:11 -0800403 if (snum && snum < inet_prot_sock(net) &&
Eric W. Biederman35946982012-11-16 03:03:12 +0000404 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return -EACCES;
406
Vlad Yasevich4e540642008-07-18 23:06:32 -0700407 /* See if the address matches any of the addresses we may have
408 * already bound before checking against other endpoints.
409 */
410 if (sctp_bind_addr_match(bp, addr, sp))
411 return -EINVAL;
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* Make sure we are allowed to bind here.
414 * The function sctp_get_port_local() does duplicate address
415 * detection.
416 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900417 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if ((ret = sctp_get_port_local(sk, addr))) {
Vlad Yasevich4e540642008-07-18 23:06:32 -0700419 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 /* Refresh ephemeral port. */
423 if (!bp->port)
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000424 bp->port = inet_sk(sk)->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Vlad Yasevich559cf712007-09-16 16:03:28 -0700426 /* Add the address to the bind address list.
427 * Use GFP_ATOMIC since BHs will be disabled.
428 */
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300429 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
430 SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* Copy back into socket for getsockname() use. */
433 if (!ret) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000434 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600435 sp->pf->to_sk_saddr(addr, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437
438 return ret;
439}
440
441 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
442 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900443 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900445 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900447 * subsequent ASCONF. Note this restriction binds each side, so at any
448 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * from each endpoint).
450 */
451static int sctp_send_asconf(struct sctp_association *asoc,
452 struct sctp_chunk *chunk)
453{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000454 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 int retval = 0;
456
457 /* If there is an outstanding ASCONF chunk, queue it for later
458 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700461 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900462 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464
465 /* Hold the chunk until an ASCONF_ACK is received. */
466 sctp_chunk_hold(chunk);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000467 retval = sctp_primitive_ASCONF(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (retval)
469 sctp_chunk_free(chunk);
470 else
471 asoc->addip_last_asconf = chunk;
472
473out:
474 return retval;
475}
476
477/* Add a list of addresses as bind addresses to local endpoint or
478 * association.
479 *
480 * Basically run through each address specified in the addrs/addrcnt
481 * array/length pair, determine if it is IPv6 or IPv4 and call
482 * sctp_do_bind() on it.
483 *
484 * If any of them fails, then the operation will be reversed and the
485 * ones that were added will be removed.
486 *
487 * Only sctp_setsockopt_bindx() is supposed to call this function.
488 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200489static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 int cnt;
492 int retval = 0;
493 void *addr_buf;
494 struct sockaddr *sa_addr;
495 struct sctp_af *af;
496
Daniel Borkmannbb333812013-06-28 19:49:40 +0200497 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
498 addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 addr_buf = addrs;
501 for (cnt = 0; cnt < addrcnt; cnt++) {
502 /* The list may contain either IPv4 or IPv6 address;
503 * determine the address length for walking thru the list.
504 */
Joe Perchesea110732011-06-13 16:21:26 +0000505 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 af = sctp_get_af_specific(sa_addr->sa_family);
507 if (!af) {
508 retval = -EINVAL;
509 goto err_bindx_add;
510 }
511
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900512 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 af->sockaddr_len);
514
515 addr_buf += af->sockaddr_len;
516
517err_bindx_add:
518 if (retval < 0) {
519 /* Failed. Cleanup the ones that have been added */
520 if (cnt > 0)
521 sctp_bindx_rem(sk, addrs, cnt);
522 return retval;
523 }
524 }
525
526 return retval;
527}
528
529/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
530 * associations that are part of the endpoint indicating that a list of local
531 * addresses are added to the endpoint.
532 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900533 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 * association, we do not send the chunk for that association. But it will not
535 * affect other associations.
536 *
537 * Only sctp_setsockopt_bindx() is supposed to call this function.
538 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900539static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct sockaddr *addrs,
541 int addrcnt)
542{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000543 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct sctp_sock *sp;
545 struct sctp_endpoint *ep;
546 struct sctp_association *asoc;
547 struct sctp_bind_addr *bp;
548 struct sctp_chunk *chunk;
549 struct sctp_sockaddr_entry *laddr;
550 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700551 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 void *addr_buf;
553 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 struct list_head *p;
555 int i;
556 int retval = 0;
557
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000558 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return retval;
560
561 sp = sctp_sk(sk);
562 ep = sp->ep;
563
Daniel Borkmannbb333812013-06-28 19:49:40 +0200564 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
565 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700567 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!asoc->peer.asconf_capable)
569 continue;
570
571 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
572 continue;
573
574 if (!sctp_state(asoc, ESTABLISHED))
575 continue;
576
577 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900578 * in the bind address list of the association. If so,
579 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * other associations.
581 */
582 addr_buf = addrs;
583 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000584 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 af = sctp_get_af_specific(addr->v4.sin_family);
586 if (!af) {
587 retval = -EINVAL;
588 goto out;
589 }
590
591 if (sctp_assoc_lookup_laddr(asoc, addr))
592 break;
593
594 addr_buf += af->sockaddr_len;
595 }
596 if (i < addrcnt)
597 continue;
598
Vlad Yasevich559cf712007-09-16 16:03:28 -0700599 /* Use the first valid address in bind addr list of
600 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 bp = &asoc->base.bind_addr;
603 p = bp->address_list.next;
604 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800605 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 addrcnt, SCTP_PARAM_ADD_IP);
607 if (!chunk) {
608 retval = -ENOMEM;
609 goto out;
610 }
611
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700612 /* Add the new addresses to the bind address list with
613 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700615 addr_buf = addrs;
616 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000617 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700618 af = sctp_get_af_specific(addr->v4.sin_family);
619 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800620 retval = sctp_add_bind_addr(bp, &saveaddr,
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300621 sizeof(saveaddr),
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800622 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700623 addr_buf += af->sockaddr_len;
624 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900625 if (asoc->src_out_of_asoc_ok) {
626 struct sctp_transport *trans;
627
628 list_for_each_entry(trans,
629 &asoc->peer.transport_addr_list, transports) {
Michio Honda8a07eb02011-04-26 20:19:36 +0900630 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
631 2*asoc->pathmtu, 4380));
632 trans->ssthresh = asoc->peer.i.a_rwnd;
633 trans->rto = asoc->rto_initial;
Michele Baldessari196d6752012-12-01 04:49:42 +0000634 sctp_max_rto(asoc, trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900635 trans->rtt = trans->srtt = trans->rttvar = 0;
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300636 /* Clear the source and route cache */
Michio Honda8a07eb02011-04-26 20:19:36 +0900637 sctp_transport_route(trans, NULL,
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300638 sctp_sk(asoc->base.sk));
Michio Honda8a07eb02011-04-26 20:19:36 +0900639 }
640 }
641 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643
644out:
645 return retval;
646}
647
648/* Remove a list of addresses from bind addresses list. Do not remove the
649 * last address.
650 *
651 * Basically run through each address specified in the addrs/addrcnt
652 * array/length pair, determine if it is IPv6 or IPv4 and call
653 * sctp_del_bind() on it.
654 *
655 * If any of them fails, then the operation will be reversed and the
656 * ones that were removed will be added back.
657 *
658 * At least one address has to be left; if only one address is
659 * available, the operation will return -EBUSY.
660 *
661 * Only sctp_setsockopt_bindx() is supposed to call this function.
662 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200663static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct sctp_sock *sp = sctp_sk(sk);
666 struct sctp_endpoint *ep = sp->ep;
667 int cnt;
668 struct sctp_bind_addr *bp = &ep->base.bind_addr;
669 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800671 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct sctp_af *af;
673
Daniel Borkmannbb333812013-06-28 19:49:40 +0200674 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
675 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 addr_buf = addrs;
678 for (cnt = 0; cnt < addrcnt; cnt++) {
679 /* If the bind address list is empty or if there is only one
680 * bind address, there is nothing more to be removed (we need
681 * at least one address here).
682 */
683 if (list_empty(&bp->address_list) ||
684 (sctp_list_single_entry(&bp->address_list))) {
685 retval = -EBUSY;
686 goto err_bindx_rem;
687 }
688
Joe Perchesea110732011-06-13 16:21:26 +0000689 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800690 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (!af) {
692 retval = -EINVAL;
693 goto err_bindx_rem;
694 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700695
696 if (!af->addr_valid(sa_addr, sp, NULL)) {
697 retval = -EADDRNOTAVAIL;
698 goto err_bindx_rem;
699 }
700
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000701 if (sa_addr->v4.sin_port &&
702 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 retval = -EINVAL;
704 goto err_bindx_rem;
705 }
706
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000707 if (!sa_addr->v4.sin_port)
708 sa_addr->v4.sin_port = htons(bp->port);
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* FIXME - There is probably a need to check if sk->sk_saddr and
711 * sk->sk_rcv_addr are currently set to one of the addresses to
712 * be removed. This is something which needs to be looked into
713 * when we are fixing the outstanding issues with multi-homing
714 * socket routing and failover schemes. Refer to comments in
715 * sctp_do_bind(). -daisy
716 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400717 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 addr_buf += af->sockaddr_len;
720err_bindx_rem:
721 if (retval < 0) {
722 /* Failed. Add the ones that has been removed back */
723 if (cnt > 0)
724 sctp_bindx_add(sk, addrs, cnt);
725 return retval;
726 }
727 }
728
729 return retval;
730}
731
732/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
733 * the associations that are part of the endpoint indicating that a list of
734 * local addresses are removed from the endpoint.
735 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900736 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 * association, we do not send the chunk for that association. But it will not
738 * affect other associations.
739 *
740 * Only sctp_setsockopt_bindx() is supposed to call this function.
741 */
742static int sctp_send_asconf_del_ip(struct sock *sk,
743 struct sockaddr *addrs,
744 int addrcnt)
745{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000746 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 struct sctp_sock *sp;
748 struct sctp_endpoint *ep;
749 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700750 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 struct sctp_bind_addr *bp;
752 struct sctp_chunk *chunk;
753 union sctp_addr *laddr;
754 void *addr_buf;
755 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700756 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 int i;
758 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900759 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Michio Honda8a07eb02011-04-26 20:19:36 +0900761 chunk = NULL;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000762 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return retval;
764
765 sp = sctp_sk(sk);
766 ep = sp->ep;
767
Daniel Borkmannbb333812013-06-28 19:49:40 +0200768 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
769 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700771 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 if (!asoc->peer.asconf_capable)
774 continue;
775
776 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
777 continue;
778
779 if (!sctp_state(asoc, ESTABLISHED))
780 continue;
781
782 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900783 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * If so, do not send the asconf chunk to its peer, but
785 * continue with other associations.
786 */
787 addr_buf = addrs;
788 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000789 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 af = sctp_get_af_specific(laddr->v4.sin_family);
791 if (!af) {
792 retval = -EINVAL;
793 goto out;
794 }
795
796 if (!sctp_assoc_lookup_laddr(asoc, laddr))
797 break;
798
799 addr_buf += af->sockaddr_len;
800 }
801 if (i < addrcnt)
802 continue;
803
804 /* Find one address in the association's bind address list
805 * that is not in the packed array of addresses. This is to
806 * make sure that we do not delete all the addresses in the
807 * association.
808 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 bp = &asoc->base.bind_addr;
810 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
811 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900812 if ((laddr == NULL) && (addrcnt == 1)) {
813 if (asoc->asconf_addr_del_pending)
814 continue;
815 asoc->asconf_addr_del_pending =
816 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900817 if (asoc->asconf_addr_del_pending == NULL) {
818 retval = -ENOMEM;
819 goto out;
820 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900821 asoc->asconf_addr_del_pending->sa.sa_family =
822 addrs->sa_family;
823 asoc->asconf_addr_del_pending->v4.sin_port =
824 htons(bp->port);
825 if (addrs->sa_family == AF_INET) {
826 struct sockaddr_in *sin;
827
828 sin = (struct sockaddr_in *)addrs;
829 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
830 } else if (addrs->sa_family == AF_INET6) {
831 struct sockaddr_in6 *sin6;
832
833 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000834 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900835 }
Daniel Borkmannbb333812013-06-28 19:49:40 +0200836
837 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
838 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
839 asoc->asconf_addr_del_pending);
840
Michio Honda8a07eb02011-04-26 20:19:36 +0900841 asoc->src_out_of_asoc_ok = 1;
842 stored = 1;
843 goto skip_mkasconf;
844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Daniel Borkmann88362ad2013-09-07 20:51:21 +0200846 if (laddr == NULL)
847 return -EINVAL;
848
Vlad Yasevich559cf712007-09-16 16:03:28 -0700849 /* We do not need RCU protection throughout this loop
850 * because this is done under a socket lock from the
851 * setsockopt call.
852 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
854 SCTP_PARAM_DEL_IP);
855 if (!chunk) {
856 retval = -ENOMEM;
857 goto out;
858 }
859
Michio Honda8a07eb02011-04-26 20:19:36 +0900860skip_mkasconf:
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700861 /* Reset use_as_src flag for the addresses in the bind address
862 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700864 addr_buf = addrs;
865 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000866 laddr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700867 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700868 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a12006-11-20 17:05:23 -0800869 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800870 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700871 }
872 addr_buf += af->sockaddr_len;
873 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700874
875 /* Update the route and saddr entries for all the transports
876 * as some of the addresses in the bind address list are
877 * about to be deleted and cannot be used as source addresses.
878 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700879 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
880 transports) {
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700881 sctp_transport_route(transport, NULL,
882 sctp_sk(asoc->base.sk));
883 }
884
Michio Honda8a07eb02011-04-26 20:19:36 +0900885 if (stored)
886 /* We don't need to transmit ASCONF */
887 continue;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700888 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890out:
891 return retval;
892}
893
Michio Honda9f7d6532011-04-26 19:32:51 +0900894/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
895int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
896{
897 struct sock *sk = sctp_opt2sk(sp);
898 union sctp_addr *addr;
899 struct sctp_af *af;
900
901 /* It is safe to write port space in caller. */
902 addr = &addrw->a;
903 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
904 af = sctp_get_af_specific(addr->sa.sa_family);
905 if (!af)
906 return -EINVAL;
907 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
908 return -EINVAL;
909
910 if (addrw->state == SCTP_ADDR_NEW)
911 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
912 else
913 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
914}
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
917 *
918 * API 8.1
919 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
920 * int flags);
921 *
922 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
923 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
924 * or IPv6 addresses.
925 *
926 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
927 * Section 3.1.2 for this usage.
928 *
929 * addrs is a pointer to an array of one or more socket addresses. Each
930 * address is contained in its appropriate structure (i.e. struct
931 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700932 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 * representation is termed a "packed array" of addresses). The caller
934 * specifies the number of addresses in the array with addrcnt.
935 *
936 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
937 * -1, and sets errno to the appropriate error code.
938 *
939 * For SCTP, the port given in each socket address must be the same, or
940 * sctp_bindx() will fail, setting errno to EINVAL.
941 *
942 * The flags parameter is formed from the bitwise OR of zero or more of
943 * the following currently defined flags:
944 *
945 * SCTP_BINDX_ADD_ADDR
946 *
947 * SCTP_BINDX_REM_ADDR
948 *
949 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
950 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
951 * addresses from the association. The two flags are mutually exclusive;
952 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
953 * not remove all addresses from an association; sctp_bindx() will
954 * reject such an attempt with EINVAL.
955 *
956 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
957 * additional addresses with an endpoint after calling bind(). Or use
958 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
959 * socket is associated with so that no new association accepted will be
960 * associated with those addresses. If the endpoint supports dynamic
961 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
962 * endpoint to send the appropriate message to the peer to change the
963 * peers address lists.
964 *
965 * Adding and removing addresses from a connected association is
966 * optional functionality. Implementations that do not support this
967 * functionality should return EOPNOTSUPP.
968 *
969 * Basically do nothing but copying the addresses from user to kernel
970 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700971 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
972 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
975 * it.
976 *
977 * sk The sk of the socket
978 * addrs The pointer to the addresses in user land
979 * addrssize Size of the addrs buffer
980 * op Operation to perform (add or remove, see the flags of
981 * sctp_bindx)
982 *
983 * Returns 0 if ok, <0 errno code on error.
984 */
wangweidong26ac8e52013-12-23 12:16:51 +0800985static int sctp_setsockopt_bindx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +0200986 struct sockaddr __user *addrs,
987 int addrs_size, int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 struct sockaddr *kaddrs;
990 int err;
991 int addrcnt = 0;
992 int walk_size = 0;
993 struct sockaddr *sa_addr;
994 void *addr_buf;
995 struct sctp_af *af;
996
Daniel Borkmannbb333812013-06-28 19:49:40 +0200997 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
998 __func__, sk, addrs, addrs_size, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 if (unlikely(addrs_size <= 0))
1001 return -EINVAL;
1002
Al Viroc981f252018-01-07 13:19:09 -05001003 kaddrs = vmemdup_user(addrs, addrs_size);
1004 if (unlikely(IS_ERR(kaddrs)))
1005 return PTR_ERR(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001007 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 addr_buf = kaddrs;
1009 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001010 if (walk_size + sizeof(sa_family_t) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001011 kvfree(kaddrs);
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001012 return -EINVAL;
1013 }
1014
Joe Perchesea110732011-06-13 16:21:26 +00001015 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 af = sctp_get_af_specific(sa_addr->sa_family);
1017
1018 /* If the address family is not supported or if this address
1019 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001022 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return -EINVAL;
1024 }
1025 addrcnt++;
1026 addr_buf += af->sockaddr_len;
1027 walk_size += af->sockaddr_len;
1028 }
1029
1030 /* Do the work. */
1031 switch (op) {
1032 case SCTP_BINDX_ADD_ADDR:
Richard Haines2277c7c2018-02-13 20:56:24 +00001033 /* Allow security module to validate bindx addresses. */
1034 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1035 (struct sockaddr *)kaddrs,
1036 addrs_size);
1037 if (err)
1038 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1040 if (err)
1041 goto out;
1042 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1043 break;
1044
1045 case SCTP_BINDX_REM_ADDR:
1046 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1047 if (err)
1048 goto out;
1049 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1050 break;
1051
1052 default:
1053 err = -EINVAL;
1054 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057out:
Al Viroc981f252018-01-07 13:19:09 -05001058 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 return err;
1061}
1062
Frank Filz3f7a87d2005-06-20 13:14:57 -07001063/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1064 *
1065 * Common routine for handling connect() and sctp_connectx().
1066 * Connect will come in with just a single address.
1067 */
wangweidong26ac8e52013-12-23 12:16:51 +08001068static int __sctp_connect(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001069 struct sockaddr *kaddrs,
Xin Long644fbde2018-05-20 16:39:10 +08001070 int addrs_size, int flags,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001071 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001072{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001073 struct net *net = sock_net(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001074 struct sctp_sock *sp;
1075 struct sctp_endpoint *ep;
1076 struct sctp_association *asoc = NULL;
1077 struct sctp_association *asoc2;
1078 struct sctp_transport *transport;
1079 union sctp_addr to;
Xin Long1c662012017-08-05 19:59:54 +08001080 enum sctp_scope scope;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001081 long timeo;
1082 int err = 0;
1083 int addrcnt = 0;
1084 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001085 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001086 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001087 unsigned short port;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001088
1089 sp = sctp_sk(sk);
1090 ep = sp->ep;
1091
1092 /* connect() cannot be done on a socket that is already in ESTABLISHED
1093 * state - UDP-style peeled off socket or a TCP-style socket that
1094 * is already connected.
1095 * It cannot be done even on a TCP-style listening socket.
1096 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03001097 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
Frank Filz3f7a87d2005-06-20 13:14:57 -07001098 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1099 err = -EISCONN;
1100 goto out_free;
1101 }
1102
1103 /* Walk through the addrs buffer and count the number of addresses. */
1104 addr_buf = kaddrs;
1105 while (walk_size < addrs_size) {
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001106 struct sctp_af *af;
1107
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001108 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1109 err = -EINVAL;
1110 goto out_free;
1111 }
1112
Joe Perchesea110732011-06-13 16:21:26 +00001113 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001114 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001115
1116 /* If the address family is not supported or if this address
1117 * causes the address buffer to overflow return EINVAL.
1118 */
1119 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1120 err = -EINVAL;
1121 goto out_free;
1122 }
1123
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001124 port = ntohs(sa_addr->v4.sin_port);
1125
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001126 /* Save current address so we can work with it */
1127 memcpy(&to, sa_addr, af->sockaddr_len);
1128
1129 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001130 if (err)
1131 goto out_free;
1132
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001133 /* Make sure the destination port is correctly set
1134 * in all addresses.
1135 */
Wei Yongjun524fba62013-04-03 03:02:28 +00001136 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1137 err = -EINVAL;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001138 goto out_free;
Wei Yongjun524fba62013-04-03 03:02:28 +00001139 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001140
1141 /* Check if there already is a matching association on the
1142 * endpoint (other than the one created here).
1143 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001144 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001145 if (asoc2 && asoc2 != asoc) {
1146 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1147 err = -EISCONN;
1148 else
1149 err = -EALREADY;
1150 goto out_free;
1151 }
1152
1153 /* If we could not find a matching association on the endpoint,
1154 * make sure that there is no peeled-off association matching
1155 * the peer address even on another socket.
1156 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001157 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001158 err = -EADDRNOTAVAIL;
1159 goto out_free;
1160 }
1161
1162 if (!asoc) {
1163 /* If a bind() or sctp_bindx() is not called prior to
1164 * an sctp_connectx() call, the system picks an
1165 * ephemeral port and will choose an address set
1166 * equivalent to binding with a wildcard address.
1167 */
1168 if (!ep->base.bind_addr.port) {
1169 if (sctp_autobind(sk)) {
1170 err = -EAGAIN;
1171 goto out_free;
1172 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001173 } else {
1174 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001175 * If an unprivileged user inherits a 1-many
1176 * style socket with open associations on a
1177 * privileged port, it MAY be permitted to
1178 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001179 * be permitted to open new associations.
1180 */
Krister Johansen4548b682017-01-20 17:49:11 -08001181 if (ep->base.bind_addr.port <
1182 inet_prot_sock(net) &&
1183 !ns_capable(net->user_ns,
1184 CAP_NET_BIND_SERVICE)) {
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001185 err = -EACCES;
1186 goto out_free;
1187 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001188 }
1189
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001190 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001191 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1192 if (!asoc) {
1193 err = -ENOMEM;
1194 goto out_free;
1195 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001196
1197 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1198 GFP_KERNEL);
1199 if (err < 0) {
1200 goto out_free;
1201 }
1202
Frank Filz3f7a87d2005-06-20 13:14:57 -07001203 }
1204
1205 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001206 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001207 SCTP_UNKNOWN);
1208 if (!transport) {
1209 err = -ENOMEM;
1210 goto out_free;
1211 }
1212
1213 addrcnt++;
1214 addr_buf += af->sockaddr_len;
1215 walk_size += af->sockaddr_len;
1216 }
1217
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001218 /* In case the user of sctp_connectx() wants an association
1219 * id back, assign one now.
1220 */
1221 if (assoc_id) {
1222 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1223 if (err < 0)
1224 goto out_free;
1225 }
1226
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001227 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001228 if (err < 0) {
1229 goto out_free;
1230 }
1231
1232 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001233 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001234 sp->pf->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001235 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001236
Xin Long644fbde2018-05-20 16:39:10 +08001237 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001238
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001239 if (assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001240 *assoc_id = asoc->assoc_id;
Richard Haines2277c7c2018-02-13 20:56:24 +00001241
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001242 err = sctp_wait_for_connect(asoc, &timeo);
1243 /* Note: the asoc may be freed after the return of
1244 * sctp_wait_for_connect.
1245 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001246
1247 /* Don't free association on exit. */
1248 asoc = NULL;
1249
1250out_free:
Daniel Borkmannbb333812013-06-28 19:49:40 +02001251 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1252 __func__, asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001253
Neil Horman2eebc1e2012-07-16 09:13:51 +00001254 if (asoc) {
1255 /* sctp_primitive_ASSOCIATE may have added this association
1256 * To the hash table, try to unhash it, just in case, its a noop
1257 * if it wasn't hashed so we're safe
1258 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001259 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001260 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001261 return err;
1262}
1263
1264/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1265 *
1266 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001267 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1268 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001269 *
1270 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1271 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1272 * or IPv6 addresses.
1273 *
1274 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1275 * Section 3.1.2 for this usage.
1276 *
1277 * addrs is a pointer to an array of one or more socket addresses. Each
1278 * address is contained in its appropriate structure (i.e. struct
1279 * sockaddr_in or struct sockaddr_in6) the family of the address type
1280 * must be used to distengish the address length (note that this
1281 * representation is termed a "packed array" of addresses). The caller
1282 * specifies the number of addresses in the array with addrcnt.
1283 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001284 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1285 * the association id of the new association. On failure, sctp_connectx()
1286 * returns -1, and sets errno to the appropriate error code. The assoc_id
1287 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001288 *
1289 * For SCTP, the port given in each socket address must be the same, or
1290 * sctp_connectx() will fail, setting errno to EINVAL.
1291 *
1292 * An application can use sctp_connectx to initiate an association with
1293 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1294 * allows a caller to specify multiple addresses at which a peer can be
1295 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001296 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001297 * specifies that the stack will try to make use of all the addresses in
1298 * the list when needed.
1299 *
1300 * Note that the list of addresses passed in is only used for setting up
1301 * the association. It does not necessarily equal the set of addresses
1302 * the peer uses for the resulting association. If the caller wants to
1303 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1304 * retrieve them after the association has been set up.
1305 *
1306 * Basically do nothing but copying the addresses from user to kernel
1307 * land and invoking either sctp_connectx(). This is used for tunneling
1308 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1309 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07001310 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1311 * it.
1312 *
1313 * sk The sk of the socket
1314 * addrs The pointer to the addresses in user land
1315 * addrssize Size of the addrs buffer
1316 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001317 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001318 */
wangweidong26ac8e52013-12-23 12:16:51 +08001319static int __sctp_setsockopt_connectx(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001320 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001321 int addrs_size,
1322 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001323{
Frank Filz3f7a87d2005-06-20 13:14:57 -07001324 struct sockaddr *kaddrs;
Xin Long644fbde2018-05-20 16:39:10 +08001325 int err = 0, flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001326
Daniel Borkmannbb333812013-06-28 19:49:40 +02001327 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1328 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001329
1330 if (unlikely(addrs_size <= 0))
1331 return -EINVAL;
1332
Al Viroc981f252018-01-07 13:19:09 -05001333 kaddrs = vmemdup_user(addrs, addrs_size);
1334 if (unlikely(IS_ERR(kaddrs)))
1335 return PTR_ERR(kaddrs);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001336
Richard Haines2277c7c2018-02-13 20:56:24 +00001337 /* Allow security module to validate connectx addresses. */
1338 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1339 (struct sockaddr *)kaddrs,
1340 addrs_size);
1341 if (err)
1342 goto out_free;
1343
Xin Long644fbde2018-05-20 16:39:10 +08001344 /* in-kernel sockets don't generally have a file allocated to them
1345 * if all they do is call sock_create_kern().
1346 */
1347 if (sk->sk_socket->file)
1348 flags = sk->sk_socket->file->f_flags;
1349
1350 err = __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
Richard Haines2277c7c2018-02-13 20:56:24 +00001351
1352out_free:
Al Viroc981f252018-01-07 13:19:09 -05001353 kvfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001354
Frank Filz3f7a87d2005-06-20 13:14:57 -07001355 return err;
1356}
1357
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001358/*
1359 * This is an older interface. It's kept for backward compatibility
1360 * to the option that doesn't provide association id.
1361 */
wangweidong26ac8e52013-12-23 12:16:51 +08001362static int sctp_setsockopt_connectx_old(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001363 struct sockaddr __user *addrs,
1364 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001365{
1366 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1367}
1368
1369/*
1370 * New interface for the API. The since the API is done with a socket
1371 * option, to make it simple we feed back the association id is as a return
1372 * indication to the call. Error is always negative and association id is
1373 * always positive.
1374 */
wangweidong26ac8e52013-12-23 12:16:51 +08001375static int sctp_setsockopt_connectx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001376 struct sockaddr __user *addrs,
1377 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001378{
1379 sctp_assoc_t assoc_id = 0;
1380 int err = 0;
1381
1382 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1383
1384 if (err)
1385 return err;
1386 else
1387 return assoc_id;
1388}
1389
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001390/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001391 * New (hopefully final) interface for the API.
1392 * We use the sctp_getaddrs_old structure so that use-space library
Daniel Borkmannffd59392014-02-17 12:11:11 +01001393 * can avoid any unnecessary allocations. The only different part
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001394 * is that we store the actual length of the address buffer into the
Daniel Borkmannffd59392014-02-17 12:11:11 +01001395 * addrs_num structure member. That way we can re-use the existing
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001396 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001397 */
Daniel Borkmannffd59392014-02-17 12:11:11 +01001398#ifdef CONFIG_COMPAT
1399struct compat_sctp_getaddrs_old {
1400 sctp_assoc_t assoc_id;
1401 s32 addr_num;
1402 compat_uptr_t addrs; /* struct sockaddr * */
1403};
1404#endif
1405
wangweidong26ac8e52013-12-23 12:16:51 +08001406static int sctp_getsockopt_connectx3(struct sock *sk, int len,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001407 char __user *optval,
1408 int __user *optlen)
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001409{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001410 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001411 sctp_assoc_t assoc_id = 0;
1412 int err = 0;
1413
Daniel Borkmannffd59392014-02-17 12:11:11 +01001414#ifdef CONFIG_COMPAT
Andy Lutomirski96c0e0a2016-03-22 14:25:07 -07001415 if (in_compat_syscall()) {
Daniel Borkmannffd59392014-02-17 12:11:11 +01001416 struct compat_sctp_getaddrs_old param32;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001417
Daniel Borkmannffd59392014-02-17 12:11:11 +01001418 if (len < sizeof(param32))
1419 return -EINVAL;
1420 if (copy_from_user(&param32, optval, sizeof(param32)))
1421 return -EFAULT;
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001422
Daniel Borkmannffd59392014-02-17 12:11:11 +01001423 param.assoc_id = param32.assoc_id;
1424 param.addr_num = param32.addr_num;
1425 param.addrs = compat_ptr(param32.addrs);
1426 } else
1427#endif
1428 {
1429 if (len < sizeof(param))
1430 return -EINVAL;
1431 if (copy_from_user(&param, optval, sizeof(param)))
1432 return -EFAULT;
1433 }
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001434
Daniel Borkmannffd59392014-02-17 12:11:11 +01001435 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1436 param.addrs, param.addr_num,
1437 &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001438 if (err == 0 || err == -EINPROGRESS) {
1439 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1440 return -EFAULT;
1441 if (put_user(sizeof(assoc_id), optlen))
1442 return -EFAULT;
1443 }
1444
1445 return err;
1446}
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448/* API 3.1.4 close() - UDP Style Syntax
1449 * Applications use close() to perform graceful shutdown (as described in
1450 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1451 * by a UDP-style socket.
1452 *
1453 * The syntax is
1454 *
1455 * ret = close(int sd);
1456 *
1457 * sd - the socket descriptor of the associations to be closed.
1458 *
1459 * To gracefully shutdown a specific association represented by the
1460 * UDP-style socket, an application should use the sendmsg() call,
1461 * passing no user data, but including the appropriate flag in the
1462 * ancillary data (see Section xxxx).
1463 *
1464 * If sd in the close() call is a branched-off socket representing only
1465 * one association, the shutdown is performed on that association only.
1466 *
1467 * 4.1.6 close() - TCP Style Syntax
1468 *
1469 * Applications use close() to gracefully close down an association.
1470 *
1471 * The syntax is:
1472 *
1473 * int close(int sd);
1474 *
1475 * sd - the socket descriptor of the association to be closed.
1476 *
1477 * After an application calls close() on a socket descriptor, no further
1478 * socket operations will succeed on that descriptor.
1479 *
1480 * API 7.1.4 SO_LINGER
1481 *
1482 * An application using the TCP-style socket can use this option to
1483 * perform the SCTP ABORT primitive. The linger option structure is:
1484 *
1485 * struct linger {
1486 * int l_onoff; // option on/off
1487 * int l_linger; // linger time
1488 * };
1489 *
1490 * To enable the option, set l_onoff to 1. If the l_linger value is set
1491 * to 0, calling close() is the same as the ABORT primitive. If the
1492 * value is set to a negative value, the setsockopt() call will return
1493 * an error. If the value is set to a positive value linger_time, the
1494 * close() can be blocked for at most linger_time ms. If the graceful
1495 * shutdown phase does not finish during this period, close() will
1496 * return but the graceful shutdown phase continues in the system.
1497 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02001498static void sctp_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001500 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 struct sctp_endpoint *ep;
1502 struct sctp_association *asoc;
1503 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001504 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Daniel Borkmannbb333812013-06-28 19:49:40 +02001506 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Xin Long6dfe4b92017-06-10 14:56:56 +08001508 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 sk->sk_shutdown = SHUTDOWN_MASK;
Yafang Shaocbabf462017-12-20 11:12:54 +08001510 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 ep = sctp_sk(sk)->ep;
1513
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001514 /* Clean up any skbs sitting on the receive queue. */
1515 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1516 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1517
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001518 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 list_for_each_safe(pos, temp, &ep->asocs) {
1520 asoc = list_entry(pos, struct sctp_association, asocs);
1521
1522 if (sctp_style(sk, TCP)) {
1523 /* A closed association can still be in the list if
1524 * it belongs to a TCP-style listening socket that is
1525 * not yet accepted. If so, free it. If not, send an
1526 * ABORT or SHUTDOWN based on the linger options.
1527 */
1528 if (sctp_state(asoc, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001530 continue;
1531 }
1532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001534 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1535 !skb_queue_empty(&asoc->ulpq.reasm) ||
Xin Long13228232017-12-08 21:04:09 +08001536 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001537 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001538 struct sctp_chunk *chunk;
1539
1540 chunk = sctp_make_abort_user(asoc, NULL, 0);
Xin Long068d8bd2015-12-29 17:49:25 +08001541 sctp_primitive_ABORT(net, asoc, chunk);
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001542 } else
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001543 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 /* On a TCP-style socket, block for at most linger_time if set. */
1547 if (sctp_style(sk, TCP) && timeout)
1548 sctp_wait_for_close(sk, timeout);
1549
1550 /* This will run the backlog queue. */
wangweidong048ed4b2014-01-21 15:44:11 +08001551 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 /* Supposedly, no process has access to the socket, but
1554 * the net layers still may.
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001555 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1556 * held and that should be grabbed before socket lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001558 spin_lock_bh(&net->sctp.addr_wq_lock);
Xin Long6dfe4b92017-06-10 14:56:56 +08001559 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 /* Hold the sock, since sk_common_release() will put sock_put()
1562 * and we have just a little more cleanup.
1563 */
1564 sock_hold(sk);
1565 sk_common_release(sk);
1566
wangweidong5bc1d1b2014-01-21 15:44:12 +08001567 bh_unlock_sock(sk);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001568 spin_unlock_bh(&net->sctp.addr_wq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 sock_put(sk);
1571
1572 SCTP_DBG_OBJCNT_DEC(sock);
1573}
1574
1575/* Handle EPIPE error. */
1576static int sctp_error(struct sock *sk, int flags, int err)
1577{
1578 if (err == -EPIPE)
1579 err = sock_error(sk) ? : -EPIPE;
1580 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1581 send_sig(SIGPIPE, current, 0);
1582 return err;
1583}
1584
1585/* API 3.1.3 sendmsg() - UDP Style Syntax
1586 *
1587 * An application uses sendmsg() and recvmsg() calls to transmit data to
1588 * and receive data from its peer.
1589 *
1590 * ssize_t sendmsg(int socket, const struct msghdr *message,
1591 * int flags);
1592 *
1593 * socket - the socket descriptor of the endpoint.
1594 * message - pointer to the msghdr structure which contains a single
1595 * user message and possibly some ancillary data.
1596 *
1597 * See Section 5 for complete description of the data
1598 * structures.
1599 *
1600 * flags - flags sent or received with the user message, see Section
1601 * 5 for complete description of the flags.
1602 *
1603 * Note: This function could use a rewrite especially when explicit
1604 * connect support comes in.
1605 */
1606/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1607
Xin Longa05437a2017-08-11 10:23:48 +08001608static int sctp_msghdr_parse(const struct msghdr *msg,
1609 struct sctp_cmsgs *cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Xin Long204f8172018-03-01 23:05:14 +08001611static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1612 struct sctp_sndrcvinfo *srinfo,
1613 const struct msghdr *msg, size_t msg_len)
1614{
1615 __u16 sflags;
1616 int err;
1617
1618 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1619 return -EPIPE;
1620
1621 if (msg_len > sk->sk_sndbuf)
1622 return -EMSGSIZE;
1623
1624 memset(cmsgs, 0, sizeof(*cmsgs));
1625 err = sctp_msghdr_parse(msg, cmsgs);
1626 if (err) {
1627 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1628 return err;
1629 }
1630
1631 memset(srinfo, 0, sizeof(*srinfo));
1632 if (cmsgs->srinfo) {
1633 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1634 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1635 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1636 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1637 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1638 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1639 }
1640
1641 if (cmsgs->sinfo) {
1642 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1643 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1644 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1645 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1646 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1647 }
1648
Xin Longed63afb2018-03-05 20:44:18 +08001649 if (cmsgs->prinfo) {
1650 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1651 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1652 cmsgs->prinfo->pr_policy);
1653 }
1654
Xin Long204f8172018-03-01 23:05:14 +08001655 sflags = srinfo->sinfo_flags;
1656 if (!sflags && msg_len)
1657 return 0;
1658
1659 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1660 return -EINVAL;
1661
1662 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1663 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1664 return -EINVAL;
1665
1666 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1667 return -EINVAL;
1668
1669 return 0;
1670}
1671
Xin Long2bfd80f2018-03-01 23:05:11 +08001672static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1673 struct sctp_cmsgs *cmsgs,
1674 union sctp_addr *daddr,
1675 struct sctp_transport **tp)
1676{
1677 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1678 struct net *net = sock_net(sk);
1679 struct sctp_association *asoc;
1680 enum sctp_scope scope;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001681 struct cmsghdr *cmsg;
Xin Long4be41392018-07-02 18:21:14 +08001682 __be32 flowinfo = 0;
Linus Torvalds9eda2d22018-04-06 15:39:26 -07001683 struct sctp_af *af;
Wei Yongjund98985d2018-03-13 03:03:30 +00001684 int err;
Xin Long2bfd80f2018-03-01 23:05:11 +08001685
1686 *tp = NULL;
1687
1688 if (sflags & (SCTP_EOF | SCTP_ABORT))
1689 return -EINVAL;
1690
1691 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1692 sctp_sstate(sk, CLOSING)))
1693 return -EADDRNOTAVAIL;
1694
1695 if (sctp_endpoint_is_peeled_off(ep, daddr))
1696 return -EADDRNOTAVAIL;
1697
1698 if (!ep->base.bind_addr.port) {
1699 if (sctp_autobind(sk))
1700 return -EAGAIN;
1701 } else {
1702 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1703 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1704 return -EACCES;
1705 }
1706
1707 scope = sctp_scope(daddr);
1708
Linus Torvalds9eda2d22018-04-06 15:39:26 -07001709 /* Label connection socket for first association 1-to-many
1710 * style for client sequence socket()->sendmsg(). This
1711 * needs to be done before sctp_assoc_add_peer() as that will
1712 * set up the initial packet that needs to account for any
1713 * security ip options (CIPSO/CALIPSO) added to the packet.
1714 */
1715 af = sctp_get_af_specific(daddr->sa.sa_family);
1716 if (!af)
1717 return -EINVAL;
1718 err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1719 (struct sockaddr *)daddr,
1720 af->sockaddr_len);
1721 if (err < 0)
1722 return err;
1723
Xin Long2bfd80f2018-03-01 23:05:11 +08001724 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1725 if (!asoc)
1726 return -ENOMEM;
1727
1728 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1729 err = -ENOMEM;
1730 goto free;
1731 }
1732
1733 if (cmsgs->init) {
1734 struct sctp_initmsg *init = cmsgs->init;
1735
1736 if (init->sinit_num_ostreams) {
1737 __u16 outcnt = init->sinit_num_ostreams;
1738
1739 asoc->c.sinit_num_ostreams = outcnt;
1740 /* outcnt has been changed, need to re-init stream */
1741 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1742 GFP_KERNEL);
1743 if (err)
1744 goto free;
1745 }
1746
1747 if (init->sinit_max_instreams)
1748 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1749
1750 if (init->sinit_max_attempts)
1751 asoc->max_init_attempts = init->sinit_max_attempts;
1752
1753 if (init->sinit_max_init_timeo)
1754 asoc->max_init_timeo =
1755 msecs_to_jiffies(init->sinit_max_init_timeo);
1756 }
1757
1758 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1759 if (!*tp) {
1760 err = -ENOMEM;
1761 goto free;
1762 }
1763
Xin Long2c0dbaa2018-03-05 20:44:19 +08001764 if (!cmsgs->addrs_msg)
1765 return 0;
1766
Xin Long4be41392018-07-02 18:21:14 +08001767 if (daddr->sa.sa_family == AF_INET6)
1768 flowinfo = daddr->v6.sin6_flowinfo;
1769
Xin Long2c0dbaa2018-03-05 20:44:19 +08001770 /* sendv addr list parse */
1771 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1772 struct sctp_transport *transport;
1773 struct sctp_association *old;
1774 union sctp_addr _daddr;
1775 int dlen;
1776
1777 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1778 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1779 cmsg->cmsg_type != SCTP_DSTADDRV6))
1780 continue;
1781
1782 daddr = &_daddr;
1783 memset(daddr, 0, sizeof(*daddr));
1784 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1785 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
Wei Yongjund98985d2018-03-13 03:03:30 +00001786 if (dlen < sizeof(struct in_addr)) {
1787 err = -EINVAL;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001788 goto free;
Wei Yongjund98985d2018-03-13 03:03:30 +00001789 }
Xin Long2c0dbaa2018-03-05 20:44:19 +08001790
1791 dlen = sizeof(struct in_addr);
1792 daddr->v4.sin_family = AF_INET;
1793 daddr->v4.sin_port = htons(asoc->peer.port);
1794 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1795 } else {
Wei Yongjund98985d2018-03-13 03:03:30 +00001796 if (dlen < sizeof(struct in6_addr)) {
1797 err = -EINVAL;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001798 goto free;
Wei Yongjund98985d2018-03-13 03:03:30 +00001799 }
Xin Long2c0dbaa2018-03-05 20:44:19 +08001800
1801 dlen = sizeof(struct in6_addr);
Xin Long4be41392018-07-02 18:21:14 +08001802 daddr->v6.sin6_flowinfo = flowinfo;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001803 daddr->v6.sin6_family = AF_INET6;
1804 daddr->v6.sin6_port = htons(asoc->peer.port);
1805 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1806 }
1807 err = sctp_verify_addr(sk, daddr, sizeof(*daddr));
1808 if (err)
1809 goto free;
1810
1811 old = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1812 if (old && old != asoc) {
1813 if (old->state >= SCTP_STATE_ESTABLISHED)
1814 err = -EISCONN;
1815 else
1816 err = -EALREADY;
1817 goto free;
1818 }
1819
1820 if (sctp_endpoint_is_peeled_off(ep, daddr)) {
1821 err = -EADDRNOTAVAIL;
1822 goto free;
1823 }
1824
1825 transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL,
1826 SCTP_UNKNOWN);
1827 if (!transport) {
1828 err = -ENOMEM;
1829 goto free;
1830 }
1831 }
1832
Xin Long2bfd80f2018-03-01 23:05:11 +08001833 return 0;
1834
1835free:
1836 sctp_association_free(asoc);
1837 return err;
1838}
1839
Xin Longc2666de2018-03-01 23:05:12 +08001840static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1841 __u16 sflags, struct msghdr *msg,
1842 size_t msg_len)
1843{
1844 struct sock *sk = asoc->base.sk;
1845 struct net *net = sock_net(sk);
1846
1847 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1848 return -EPIPE;
1849
Xin Long49102802018-03-05 20:44:20 +08001850 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1851 !sctp_state(asoc, ESTABLISHED))
1852 return 0;
1853
Xin Longc2666de2018-03-01 23:05:12 +08001854 if (sflags & SCTP_EOF) {
1855 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1856 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1857
1858 return 0;
1859 }
1860
1861 if (sflags & SCTP_ABORT) {
1862 struct sctp_chunk *chunk;
1863
1864 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1865 if (!chunk)
1866 return -ENOMEM;
1867
1868 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1869 sctp_primitive_ABORT(net, asoc, chunk);
1870
1871 return 0;
1872 }
1873
1874 return 1;
1875}
1876
Xin Longf84af332018-03-01 23:05:10 +08001877static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1878 struct msghdr *msg, size_t msg_len,
1879 struct sctp_transport *transport,
1880 struct sctp_sndrcvinfo *sinfo)
1881{
1882 struct sock *sk = asoc->base.sk;
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001883 struct sctp_sock *sp = sctp_sk(sk);
Xin Longf84af332018-03-01 23:05:10 +08001884 struct net *net = sock_net(sk);
1885 struct sctp_datamsg *datamsg;
1886 bool wait_connect = false;
1887 struct sctp_chunk *chunk;
1888 long timeo;
1889 int err;
1890
1891 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1892 err = -EINVAL;
1893 goto err;
1894 }
1895
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001896 if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
Xin Longf84af332018-03-01 23:05:10 +08001897 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1898 if (err)
1899 goto err;
1900 }
1901
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001902 if (sp->disable_fragments && msg_len > asoc->frag_point) {
Xin Longf84af332018-03-01 23:05:10 +08001903 err = -EMSGSIZE;
1904 goto err;
1905 }
1906
Marcelo Ricardo Leitner25216802018-04-26 16:58:56 -03001907 if (asoc->pmtu_pending) {
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001908 if (sp->param_flags & SPP_PMTUD_ENABLE)
1909 sctp_assoc_sync_pmtu(asoc);
Marcelo Ricardo Leitner25216802018-04-26 16:58:56 -03001910 asoc->pmtu_pending = 0;
1911 }
Neil Horman0aee4c22018-03-12 14:15:25 -04001912
Xin Longcd305c72018-10-17 03:07:51 +08001913 if (sctp_wspace(asoc) < (int)msg_len)
Neil Horman0aee4c22018-03-12 14:15:25 -04001914 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1915
Xin Longcd305c72018-10-17 03:07:51 +08001916 if (sctp_wspace(asoc) <= 0) {
Neil Horman0aee4c22018-03-12 14:15:25 -04001917 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1918 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1919 if (err)
1920 goto err;
1921 }
1922
Xin Longf84af332018-03-01 23:05:10 +08001923 if (sctp_state(asoc, CLOSED)) {
1924 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1925 if (err)
1926 goto err;
1927
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001928 if (sp->strm_interleave) {
Xin Longf84af332018-03-01 23:05:10 +08001929 timeo = sock_sndtimeo(sk, 0);
1930 err = sctp_wait_for_connect(asoc, &timeo);
1931 if (err)
1932 goto err;
1933 } else {
1934 wait_connect = true;
1935 }
1936
1937 pr_debug("%s: we associated primitively\n", __func__);
1938 }
1939
Xin Longf84af332018-03-01 23:05:10 +08001940 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1941 if (IS_ERR(datamsg)) {
1942 err = PTR_ERR(datamsg);
1943 goto err;
1944 }
1945
1946 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1947
1948 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1949 sctp_chunk_hold(chunk);
1950 sctp_set_owner_w(chunk);
1951 chunk->transport = transport;
1952 }
1953
1954 err = sctp_primitive_SEND(net, asoc, datamsg);
1955 if (err) {
1956 sctp_datamsg_free(datamsg);
1957 goto err;
1958 }
1959
1960 pr_debug("%s: we sent primitively\n", __func__);
1961
1962 sctp_datamsg_put(datamsg);
1963
1964 if (unlikely(wait_connect)) {
1965 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1966 sctp_wait_for_connect(asoc, &timeo);
1967 }
1968
1969 err = msg_len;
1970
1971err:
1972 return err;
1973}
1974
Xin Longbecef9b2018-03-01 23:05:13 +08001975static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1976 const struct msghdr *msg,
1977 struct sctp_cmsgs *cmsgs)
1978{
1979 union sctp_addr *daddr = NULL;
1980 int err;
1981
1982 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1983 int len = msg->msg_namelen;
1984
1985 if (len > sizeof(*daddr))
1986 len = sizeof(*daddr);
1987
1988 daddr = (union sctp_addr *)msg->msg_name;
1989
1990 err = sctp_verify_addr(sk, daddr, len);
1991 if (err)
1992 return ERR_PTR(err);
1993 }
1994
1995 return daddr;
1996}
1997
Xin Longd42cb062018-03-01 23:05:15 +08001998static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
1999 struct sctp_sndrcvinfo *sinfo,
2000 struct sctp_cmsgs *cmsgs)
2001{
2002 if (!cmsgs->srinfo && !cmsgs->sinfo) {
2003 sinfo->sinfo_stream = asoc->default_stream;
2004 sinfo->sinfo_ppid = asoc->default_ppid;
2005 sinfo->sinfo_context = asoc->default_context;
2006 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
Xin Longed63afb2018-03-05 20:44:18 +08002007
2008 if (!cmsgs->prinfo)
2009 sinfo->sinfo_flags = asoc->default_flags;
Xin Longd42cb062018-03-01 23:05:15 +08002010 }
2011
Xin Longed63afb2018-03-05 20:44:18 +08002012 if (!cmsgs->srinfo && !cmsgs->prinfo)
Xin Longd42cb062018-03-01 23:05:15 +08002013 sinfo->sinfo_timetolive = asoc->default_timetolive;
Xin Long3ff547c2018-03-14 19:05:31 +08002014
2015 if (cmsgs->authinfo) {
2016 /* Reuse sinfo_tsn to indicate that authinfo was set and
2017 * sinfo_ssn to save the keyid on tx path.
2018 */
2019 sinfo->sinfo_tsn = 1;
2020 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
2021 }
Xin Longd42cb062018-03-01 23:05:15 +08002022}
2023
Ying Xue1b784142015-03-02 15:37:48 +08002024static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Xin Long204f8172018-03-01 23:05:14 +08002026 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Xin Long8e87c6e2018-03-01 23:05:16 +08002027 struct sctp_transport *transport = NULL;
Xin Long204f8172018-03-01 23:05:14 +08002028 struct sctp_sndrcvinfo _sinfo, *sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08002029 struct sctp_association *asoc;
2030 struct sctp_cmsgs cmsgs;
Xin Longbecef9b2018-03-01 23:05:13 +08002031 union sctp_addr *daddr;
Xin Long007b7e12018-03-01 23:05:17 +08002032 bool new = false;
2033 __u16 sflags;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02002034 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Xin Long204f8172018-03-01 23:05:14 +08002036 /* Parse and get snd_info */
2037 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
2038 if (err)
Xin Long007b7e12018-03-01 23:05:17 +08002039 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Xin Long204f8172018-03-01 23:05:14 +08002041 sinfo = &_sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08002042 sflags = sinfo->sinfo_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Xin Longbecef9b2018-03-01 23:05:13 +08002044 /* Get daddr from msg */
2045 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
2046 if (IS_ERR(daddr)) {
2047 err = PTR_ERR(daddr);
Xin Long007b7e12018-03-01 23:05:17 +08002048 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
2050
wangweidong048ed4b2014-01-21 15:44:11 +08002051 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Xin Long49102802018-03-05 20:44:20 +08002053 /* SCTP_SENDALL process */
2054 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
2055 list_for_each_entry(asoc, &ep->asocs, asocs) {
2056 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2057 msg_len);
2058 if (err == 0)
2059 continue;
2060 if (err < 0)
2061 goto out_unlock;
2062
2063 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2064
2065 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
2066 NULL, sinfo);
2067 if (err < 0)
2068 goto out_unlock;
2069
2070 iov_iter_revert(&msg->msg_iter, err);
2071 }
2072
2073 goto out_unlock;
2074 }
2075
Xin Long0a3920d2018-03-01 23:05:18 +08002076 /* Get and check or create asoc */
Xin Longbecef9b2018-03-01 23:05:13 +08002077 if (daddr) {
Xin Longbecef9b2018-03-01 23:05:13 +08002078 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
Xin Long0a3920d2018-03-01 23:05:18 +08002079 if (asoc) {
2080 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2081 msg_len);
2082 if (err <= 0)
2083 goto out_unlock;
2084 } else {
2085 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2086 &transport);
2087 if (err)
2088 goto out_unlock;
2089
2090 asoc = transport->asoc;
2091 new = true;
2092 }
2093
2094 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2095 transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 } else {
Xin Long007b7e12018-03-01 23:05:17 +08002097 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 if (!asoc) {
2099 err = -EPIPE;
2100 goto out_unlock;
2101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Xin Long007b7e12018-03-01 23:05:17 +08002103 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
Xin Longc2666de2018-03-01 23:05:12 +08002104 if (err <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
2107
Xin Longd42cb062018-03-01 23:05:15 +08002108 /* Update snd_info with the asoc */
2109 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Xin Longf84af332018-03-01 23:05:10 +08002111 /* Send msg to the asoc */
Xin Long8e87c6e2018-03-01 23:05:16 +08002112 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
Xin Long007b7e12018-03-01 23:05:17 +08002113 if (err < 0 && err != -ESRCH && new)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 sctp_association_free(asoc);
Xin Long8e87c6e2018-03-01 23:05:16 +08002115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116out_unlock:
wangweidong048ed4b2014-01-21 15:44:11 +08002117 release_sock(sk);
Xin Long007b7e12018-03-01 23:05:17 +08002118out:
Xin Longf84af332018-03-01 23:05:10 +08002119 return sctp_error(sk, msg->msg_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
2122/* This is an extended version of skb_pull() that removes the data from the
2123 * start of a skb even when data is spread across the list of skb's in the
2124 * frag_list. len specifies the total amount of data that needs to be removed.
2125 * when 'len' bytes could be removed from the skb, it returns 0.
2126 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2127 * could not be removed.
2128 */
2129static int sctp_skb_pull(struct sk_buff *skb, int len)
2130{
2131 struct sk_buff *list;
2132 int skb_len = skb_headlen(skb);
2133 int rlen;
2134
2135 if (len <= skb_len) {
2136 __skb_pull(skb, len);
2137 return 0;
2138 }
2139 len -= skb_len;
2140 __skb_pull(skb, skb_len);
2141
David S. Miller1b003be2009-06-09 00:22:35 -07002142 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 rlen = sctp_skb_pull(list, len);
2144 skb->len -= (len-rlen);
2145 skb->data_len -= (len-rlen);
2146
2147 if (!rlen)
2148 return 0;
2149
2150 len = rlen;
2151 }
2152
2153 return len;
2154}
2155
2156/* API 3.1.3 recvmsg() - UDP Style Syntax
2157 *
2158 * ssize_t recvmsg(int socket, struct msghdr *message,
2159 * int flags);
2160 *
2161 * socket - the socket descriptor of the endpoint.
2162 * message - pointer to the msghdr structure which contains a single
2163 * user message and possibly some ancillary data.
2164 *
2165 * See Section 5 for complete description of the data
2166 * structures.
2167 *
2168 * flags - flags sent or received with the user message, see Section
2169 * 5 for complete description of the flags.
2170 */
Ying Xue1b784142015-03-02 15:37:48 +08002171static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2172 int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 struct sctp_ulpevent *event = NULL;
2175 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002176 struct sk_buff *skb, *head_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 int copied;
2178 int err = 0;
2179 int skb_len;
2180
Daniel Borkmannbb333812013-06-28 19:49:40 +02002181 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2182 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2183 addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
wangweidong048ed4b2014-01-21 15:44:11 +08002185 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03002187 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
Xin Longe0878692016-07-30 14:14:41 +08002188 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 err = -ENOTCONN;
2190 goto out;
2191 }
2192
2193 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2194 if (!skb)
2195 goto out;
2196
2197 /* Get the total length of the skb including any skb's in the
2198 * frag_list.
2199 */
2200 skb_len = skb->len;
2201
2202 copied = skb_len;
2203 if (copied > len)
2204 copied = len;
2205
David S. Miller51f3d022014-11-05 16:46:40 -05002206 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 event = sctp_skb2event(skb);
2209
2210 if (err)
2211 goto out_free;
2212
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002213 if (event->chunk && event->chunk->head_skb)
2214 head_skb = event->chunk->head_skb;
2215 else
2216 head_skb = skb;
2217 sock_recv_ts_and_drops(msg, sk, head_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (sctp_ulpevent_is_notification(event)) {
2219 msg->msg_flags |= MSG_NOTIFICATION;
2220 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2221 } else {
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002222 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
2224
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02002225 /* Check if we allow SCTP_NXTINFO. */
2226 if (sp->recvnxtinfo)
2227 sctp_ulpevent_read_nxtinfo(event, msg, sk);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002228 /* Check if we allow SCTP_RCVINFO. */
2229 if (sp->recvrcvinfo)
2230 sctp_ulpevent_read_rcvinfo(event, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 /* Check if we allow SCTP_SNDRCVINFO. */
2232 if (sp->subscribe.sctp_data_io_event)
2233 sctp_ulpevent_read_sndrcvinfo(event, msg);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 err = copied;
2236
2237 /* If skb's length exceeds the user's buffer, update the skb and
2238 * push it back to the receive_queue so that the next call to
2239 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2240 */
2241 if (skb_len > copied) {
2242 msg->msg_flags &= ~MSG_EOR;
2243 if (flags & MSG_PEEK)
2244 goto out_free;
2245 sctp_skb_pull(skb, copied);
2246 skb_queue_head(&sk->sk_receive_queue, skb);
2247
Daniel Borkmann362d5202014-04-14 21:45:17 +02002248 /* When only partial message is copied to the user, increase
2249 * rwnd by that amount. If all the data in the skb is read,
2250 * rwnd is updated when the event is freed.
2251 */
2252 if (!sctp_ulpevent_is_notification(event))
2253 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 goto out;
2255 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2256 (event->msg_flags & MSG_EOR))
2257 msg->msg_flags |= MSG_EOR;
2258 else
2259 msg->msg_flags &= ~MSG_EOR;
2260
2261out_free:
2262 if (flags & MSG_PEEK) {
2263 /* Release the skb reference acquired after peeking the skb in
2264 * sctp_skb_recv_datagram().
2265 */
2266 kfree_skb(skb);
2267 } else {
2268 /* Free the event which includes releasing the reference to
2269 * the owner of the skb, freeing the skb and updating the
2270 * rwnd.
2271 */
2272 sctp_ulpevent_free(event);
2273 }
2274out:
wangweidong048ed4b2014-01-21 15:44:11 +08002275 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return err;
2277}
2278
2279/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2280 *
2281 * This option is a on/off flag. If enabled no SCTP message
2282 * fragmentation will be performed. Instead if a message being sent
2283 * exceeds the current PMTU size, the message will NOT be sent and
2284 * instead a error will be indicated to the user.
2285 */
2286static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002287 char __user *optval,
2288 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 int val;
2291
2292 if (optlen < sizeof(int))
2293 return -EINVAL;
2294
2295 if (get_user(val, (int __user *)optval))
2296 return -EFAULT;
2297
2298 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2299
2300 return 0;
2301}
2302
2303static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002304 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
Wei Yongjun94912302011-07-02 09:28:04 +00002306 struct sctp_association *asoc;
2307 struct sctp_ulpevent *event;
2308
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002309 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return -EINVAL;
2311 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2312 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002313
Daniel Borkmannbbbea412014-07-12 20:30:40 +02002314 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
Wei Yongjun94912302011-07-02 09:28:04 +00002315 * if there is no data to be sent or retransmit, the stack will
2316 * immediately send up this notification.
2317 */
2318 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2319 &sctp_sk(sk)->subscribe)) {
2320 asoc = sctp_id2assoc(sk, 0);
2321
2322 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2323 event = sctp_ulpevent_make_sender_dry_event(asoc,
Marcelo Ricardo Leitner2e83acb2018-01-08 19:02:27 -02002324 GFP_USER | __GFP_NOWARN);
Wei Yongjun94912302011-07-02 09:28:04 +00002325 if (!event)
2326 return -ENOMEM;
2327
Xin Long9162e0e2017-12-08 21:04:05 +08002328 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
Wei Yongjun94912302011-07-02 09:28:04 +00002329 }
2330 }
2331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return 0;
2333}
2334
2335/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2336 *
2337 * This socket option is applicable to the UDP-style socket only. When
2338 * set it will cause associations that are idle for more than the
2339 * specified number of seconds to automatically close. An association
2340 * being idle is defined an association that has NOT sent or received
2341 * user data. The special value of '0' indicates that no automatic
2342 * close of any associations should be performed. The option expects an
2343 * integer defining the number of seconds of idle time before an
2344 * association is closed.
2345 */
2346static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002347 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
2349 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman9f70f462013-12-10 06:48:15 -05002350 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
2352 /* Applicable to UDP-style socket only */
2353 if (sctp_style(sk, TCP))
2354 return -EOPNOTSUPP;
2355 if (optlen != sizeof(int))
2356 return -EINVAL;
2357 if (copy_from_user(&sp->autoclose, optval, optlen))
2358 return -EFAULT;
2359
Neil Horman9f70f462013-12-10 06:48:15 -05002360 if (sp->autoclose > net->sctp.max_autoclose)
2361 sp->autoclose = net->sctp.max_autoclose;
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 return 0;
2364}
2365
2366/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2367 *
2368 * Applications can enable or disable heartbeats for any peer address of
2369 * an association, modify an address's heartbeat interval, force a
2370 * heartbeat to be sent immediately, and adjust the address's maximum
2371 * number of retransmissions sent before an address is considered
2372 * unreachable. The following structure is used to access and modify an
2373 * address's parameters:
2374 *
2375 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002376 * sctp_assoc_t spp_assoc_id;
2377 * struct sockaddr_storage spp_address;
2378 * uint32_t spp_hbinterval;
2379 * uint16_t spp_pathmaxrxt;
2380 * uint32_t spp_pathmtu;
2381 * uint32_t spp_sackdelay;
2382 * uint32_t spp_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08002383 * uint32_t spp_ipv6_flowlabel;
2384 * uint8_t spp_dscp;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002385 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002387 * spp_assoc_id - (one-to-many style socket) This is filled in the
2388 * application, and identifies the association for
2389 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 * spp_address - This specifies which address is of interest.
2391 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002392 * in milliseconds. If a value of zero
2393 * is present in this field then no changes are to
2394 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 * spp_pathmaxrxt - This contains the maximum number of
2396 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002397 * considered unreachable. If a value of zero
2398 * is present in this field then no changes are to
2399 * be made to this parameter.
2400 * spp_pathmtu - When Path MTU discovery is disabled the value
2401 * specified here will be the "fixed" path mtu.
2402 * Note that if the spp_address field is empty
2403 * then all associations on this address will
2404 * have this fixed path mtu set upon them.
2405 *
2406 * spp_sackdelay - When delayed sack is enabled, this value specifies
2407 * the number of milliseconds that sacks will be delayed
2408 * for. This value will apply to all addresses of an
2409 * association if the spp_address field is empty. Note
2410 * also, that if delayed sack is enabled and this
2411 * value is set to 0, no change is made to the last
2412 * recorded delayed sack timer value.
2413 *
2414 * spp_flags - These flags are used to control various features
2415 * on an association. The flag field may contain
2416 * zero or more of the following options.
2417 *
2418 * SPP_HB_ENABLE - Enable heartbeats on the
2419 * specified address. Note that if the address
2420 * field is empty all addresses for the association
2421 * have heartbeats enabled upon them.
2422 *
2423 * SPP_HB_DISABLE - Disable heartbeats on the
2424 * speicifed address. Note that if the address
2425 * field is empty all addresses for the association
2426 * will have their heartbeats disabled. Note also
2427 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2428 * mutually exclusive, only one of these two should
2429 * be specified. Enabling both fields will have
2430 * undetermined results.
2431 *
2432 * SPP_HB_DEMAND - Request a user initiated heartbeat
2433 * to be made immediately.
2434 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002435 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2436 * heartbeat delayis to be set to the value of 0
2437 * milliseconds.
2438 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002439 * SPP_PMTUD_ENABLE - This field will enable PMTU
2440 * discovery upon the specified address. Note that
2441 * if the address feild is empty then all addresses
2442 * on the association are effected.
2443 *
2444 * SPP_PMTUD_DISABLE - This field will disable PMTU
2445 * discovery upon the specified address. Note that
2446 * if the address feild is empty then all addresses
2447 * on the association are effected. Not also that
2448 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2449 * exclusive. Enabling both will have undetermined
2450 * results.
2451 *
2452 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2453 * on delayed sack. The time specified in spp_sackdelay
2454 * is used to specify the sack delay for this address. Note
2455 * that if spp_address is empty then all addresses will
2456 * enable delayed sack and take on the sack delay
2457 * value specified in spp_sackdelay.
2458 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2459 * off delayed sack. If the spp_address field is blank then
2460 * delayed sack is disabled for the entire association. Note
2461 * also that this field is mutually exclusive to
2462 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2463 * results.
Xin Long0b0dce72018-07-02 18:21:13 +08002464 *
2465 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
2466 * setting of the IPV6 flow label value. The value is
2467 * contained in the spp_ipv6_flowlabel field.
2468 * Upon retrieval, this flag will be set to indicate that
2469 * the spp_ipv6_flowlabel field has a valid value returned.
2470 * If a specific destination address is set (in the
2471 * spp_address field), then the value returned is that of
2472 * the address. If just an association is specified (and
2473 * no address), then the association's default flow label
2474 * is returned. If neither an association nor a destination
2475 * is specified, then the socket's default flow label is
2476 * returned. For non-IPv6 sockets, this flag will be left
2477 * cleared.
2478 *
2479 * SPP_DSCP: Setting this flag enables the setting of the
2480 * Differentiated Services Code Point (DSCP) value
2481 * associated with either the association or a specific
2482 * address. The value is obtained in the spp_dscp field.
2483 * Upon retrieval, this flag will be set to indicate that
2484 * the spp_dscp field has a valid value returned. If a
2485 * specific destination address is set when called (in the
2486 * spp_address field), then that specific destination
2487 * address's DSCP value is returned. If just an association
2488 * is specified, then the association's default DSCP is
2489 * returned. If neither an association nor a destination is
2490 * specified, then the socket's default DSCP is returned.
2491 *
2492 * spp_ipv6_flowlabel
2493 * - This field is used in conjunction with the
2494 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
2495 * The 20 least significant bits are used for the flow
2496 * label. This setting has precedence over any IPv6-layer
2497 * setting.
2498 *
2499 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
2500 * and contains the DSCP. The 6 most significant bits are
2501 * used for the DSCP. This setting has precedence over any
2502 * IPv4- or IPv6- layer setting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 */
Adrian Bunk16164362006-09-18 00:40:38 -07002504static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2505 struct sctp_transport *trans,
2506 struct sctp_association *asoc,
2507 struct sctp_sock *sp,
2508 int hb_change,
2509 int pmtud_change,
2510 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 int error;
2513
Frank Filz52ccb8e2005-12-22 11:36:46 -08002514 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002515 struct net *net = sock_net(trans->asoc->base.sk);
2516
2517 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 if (error)
2519 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
2521
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002522 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2523 * this field is ignored. Note also that a value of zero indicates
2524 * the current setting should be left unchanged.
2525 */
2526 if (params->spp_flags & SPP_HB_ENABLE) {
2527
2528 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2529 * set. This lets us use 0 value when this flag
2530 * is set.
2531 */
2532 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2533 params->spp_hbinterval = 0;
2534
2535 if (params->spp_hbinterval ||
2536 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2537 if (trans) {
2538 trans->hbinterval =
2539 msecs_to_jiffies(params->spp_hbinterval);
2540 } else if (asoc) {
2541 asoc->hbinterval =
2542 msecs_to_jiffies(params->spp_hbinterval);
2543 } else {
2544 sp->hbinterval = params->spp_hbinterval;
2545 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002546 }
2547 }
2548
2549 if (hb_change) {
2550 if (trans) {
2551 trans->param_flags =
2552 (trans->param_flags & ~SPP_HB) | hb_change;
2553 } else if (asoc) {
2554 asoc->param_flags =
2555 (asoc->param_flags & ~SPP_HB) | hb_change;
2556 } else {
2557 sp->param_flags =
2558 (sp->param_flags & ~SPP_HB) | hb_change;
2559 }
2560 }
2561
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002562 /* When Path MTU discovery is disabled the value specified here will
2563 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2564 * include the flag SPP_PMTUD_DISABLE for this field to have any
2565 * effect).
2566 */
2567 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002568 if (trans) {
2569 trans->pathmtu = params->spp_pathmtu;
Xin Long3ebfdf02017-04-04 13:39:55 +08002570 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002571 } else if (asoc) {
Marcelo Ricardo Leitnerc4b28932018-04-26 16:58:53 -03002572 sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002573 } else {
2574 sp->pathmtu = params->spp_pathmtu;
2575 }
2576 }
2577
2578 if (pmtud_change) {
2579 if (trans) {
2580 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2581 (params->spp_flags & SPP_PMTUD_ENABLE);
2582 trans->param_flags =
2583 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2584 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002585 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
Xin Long3ebfdf02017-04-04 13:39:55 +08002586 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002587 }
2588 } else if (asoc) {
2589 asoc->param_flags =
2590 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2591 } else {
2592 sp->param_flags =
2593 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2594 }
2595 }
2596
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002597 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2598 * value of this field is ignored. Note also that a value of zero
2599 * indicates the current setting should be left unchanged.
2600 */
2601 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002602 if (trans) {
2603 trans->sackdelay =
2604 msecs_to_jiffies(params->spp_sackdelay);
2605 } else if (asoc) {
2606 asoc->sackdelay =
2607 msecs_to_jiffies(params->spp_sackdelay);
2608 } else {
2609 sp->sackdelay = params->spp_sackdelay;
2610 }
2611 }
2612
2613 if (sackdelay_change) {
2614 if (trans) {
2615 trans->param_flags =
2616 (trans->param_flags & ~SPP_SACKDELAY) |
2617 sackdelay_change;
2618 } else if (asoc) {
2619 asoc->param_flags =
2620 (asoc->param_flags & ~SPP_SACKDELAY) |
2621 sackdelay_change;
2622 } else {
2623 sp->param_flags =
2624 (sp->param_flags & ~SPP_SACKDELAY) |
2625 sackdelay_change;
2626 }
2627 }
2628
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002629 /* Note that a value of zero indicates the current setting should be
2630 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002631 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002632 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002633 if (trans) {
2634 trans->pathmaxrxt = params->spp_pathmaxrxt;
2635 } else if (asoc) {
2636 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2637 } else {
2638 sp->pathmaxrxt = params->spp_pathmaxrxt;
2639 }
2640 }
2641
Xin Long0b0dce72018-07-02 18:21:13 +08002642 if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
Xin Long741880e2018-09-03 15:47:11 +08002643 if (trans) {
2644 if (trans->ipaddr.sa.sa_family == AF_INET6) {
2645 trans->flowlabel = params->spp_ipv6_flowlabel &
2646 SCTP_FLOWLABEL_VAL_MASK;
2647 trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2648 }
Xin Long0b0dce72018-07-02 18:21:13 +08002649 } else if (asoc) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002650 struct sctp_transport *t;
2651
2652 list_for_each_entry(t, &asoc->peer.transport_addr_list,
Xin Long0b0dce72018-07-02 18:21:13 +08002653 transports) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002654 if (t->ipaddr.sa.sa_family != AF_INET6)
Xin Long0b0dce72018-07-02 18:21:13 +08002655 continue;
Xin Longaf8a2b82018-09-03 15:47:10 +08002656 t->flowlabel = params->spp_ipv6_flowlabel &
2657 SCTP_FLOWLABEL_VAL_MASK;
2658 t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
Xin Long0b0dce72018-07-02 18:21:13 +08002659 }
2660 asoc->flowlabel = params->spp_ipv6_flowlabel &
2661 SCTP_FLOWLABEL_VAL_MASK;
2662 asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2663 } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
2664 sp->flowlabel = params->spp_ipv6_flowlabel &
2665 SCTP_FLOWLABEL_VAL_MASK;
2666 sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2667 }
2668 }
2669
2670 if (params->spp_flags & SPP_DSCP) {
2671 if (trans) {
2672 trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2673 trans->dscp |= SCTP_DSCP_SET_MASK;
2674 } else if (asoc) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002675 struct sctp_transport *t;
2676
2677 list_for_each_entry(t, &asoc->peer.transport_addr_list,
Xin Long0b0dce72018-07-02 18:21:13 +08002678 transports) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002679 t->dscp = params->spp_dscp &
2680 SCTP_DSCP_VAL_MASK;
2681 t->dscp |= SCTP_DSCP_SET_MASK;
Xin Long0b0dce72018-07-02 18:21:13 +08002682 }
2683 asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2684 asoc->dscp |= SCTP_DSCP_SET_MASK;
2685 } else {
2686 sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2687 sp->dscp |= SCTP_DSCP_SET_MASK;
2688 }
2689 }
2690
Frank Filz52ccb8e2005-12-22 11:36:46 -08002691 return 0;
2692}
2693
2694static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002695 char __user *optval,
2696 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002697{
2698 struct sctp_paddrparams params;
2699 struct sctp_transport *trans = NULL;
2700 struct sctp_association *asoc = NULL;
2701 struct sctp_sock *sp = sctp_sk(sk);
2702 int error;
2703 int hb_change, pmtud_change, sackdelay_change;
2704
Xin Long0b0dce72018-07-02 18:21:13 +08002705 if (optlen == sizeof(params)) {
2706 if (copy_from_user(&params, optval, optlen))
2707 return -EFAULT;
2708 } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
2709 spp_ipv6_flowlabel), 4)) {
2710 if (copy_from_user(&params, optval, optlen))
2711 return -EFAULT;
2712 if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
2713 return -EINVAL;
2714 } else {
wangweidongcb3f8372013-12-23 12:16:50 +08002715 return -EINVAL;
Xin Long0b0dce72018-07-02 18:21:13 +08002716 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002717
2718 /* Validate flags and value parameters. */
2719 hb_change = params.spp_flags & SPP_HB;
2720 pmtud_change = params.spp_flags & SPP_PMTUD;
2721 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2722
2723 if (hb_change == SPP_HB ||
2724 pmtud_change == SPP_PMTUD ||
2725 sackdelay_change == SPP_SACKDELAY ||
2726 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002727 (params.spp_pathmtu &&
2728 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002729 return -EINVAL;
2730
2731 /* If an address other than INADDR_ANY is specified, and
2732 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 */
wangweidongcb3f8372013-12-23 12:16:50 +08002734 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002735 trans = sctp_addr_id2transport(sk, &params.spp_address,
2736 params.spp_assoc_id);
2737 if (!trans)
2738 return -EINVAL;
2739 }
2740
2741 /* Get association, if assoc_id != 0 and the socket is a one
2742 * to many style socket, and an association was not found, then
2743 * the id was invalid.
2744 */
2745 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2746 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2747 return -EINVAL;
2748
2749 /* Heartbeat demand can only be sent on a transport or
2750 * association, but not a socket.
2751 */
2752 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2753 return -EINVAL;
2754
2755 /* Process parameters. */
2756 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2757 hb_change, pmtud_change,
2758 sackdelay_change);
2759
2760 if (error)
2761 return error;
2762
2763 /* If changes are for association, also apply parameters to each
2764 * transport.
2765 */
2766 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002767 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2768 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002769 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2770 hb_change, pmtud_change,
2771 sackdelay_change);
2772 }
2773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 return 0;
2776}
2777
wangweidong0ea5e4d2014-01-15 17:24:01 +08002778static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2779{
2780 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2781}
2782
2783static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2784{
2785 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2786}
2787
Wei Yongjund364d922008-05-09 15:13:26 -07002788/*
2789 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002790 *
Wei Yongjund364d922008-05-09 15:13:26 -07002791 * This option will effect the way delayed acks are performed. This
2792 * option allows you to get or set the delayed ack time, in
2793 * milliseconds. It also allows changing the delayed ack frequency.
2794 * Changing the frequency to 1 disables the delayed sack algorithm. If
2795 * the assoc_id is 0, then this sets or gets the endpoints default
2796 * values. If the assoc_id field is non-zero, then the set or get
2797 * effects the specified association for the one to many model (the
2798 * assoc_id field is ignored by the one to one model). Note that if
2799 * sack_delay or sack_freq are 0 when setting this option, then the
2800 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002801 *
Wei Yongjund364d922008-05-09 15:13:26 -07002802 * struct sctp_sack_info {
2803 * sctp_assoc_t sack_assoc_id;
2804 * uint32_t sack_delay;
2805 * uint32_t sack_freq;
2806 * };
Frank Filz77086102005-12-22 11:37:30 -08002807 *
Wei Yongjund364d922008-05-09 15:13:26 -07002808 * sack_assoc_id - This parameter, indicates which association the user
2809 * is performing an action upon. Note that if this field's value is
2810 * zero then the endpoints default value is changed (effecting future
2811 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002812 *
Wei Yongjund364d922008-05-09 15:13:26 -07002813 * sack_delay - This parameter contains the number of milliseconds that
2814 * the user is requesting the delayed ACK timer be set to. Note that
2815 * this value is defined in the standard to be between 200 and 500
2816 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002817 *
Wei Yongjund364d922008-05-09 15:13:26 -07002818 * sack_freq - This parameter contains the number of packets that must
2819 * be received before a sack is sent without waiting for the delay
2820 * timer to expire. The default value for this is 2, setting this
2821 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002822 */
2823
Wei Yongjund364d922008-05-09 15:13:26 -07002824static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002825 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002826{
Wei Yongjund364d922008-05-09 15:13:26 -07002827 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002828 struct sctp_transport *trans = NULL;
2829 struct sctp_association *asoc = NULL;
2830 struct sctp_sock *sp = sctp_sk(sk);
2831
Wei Yongjund364d922008-05-09 15:13:26 -07002832 if (optlen == sizeof(struct sctp_sack_info)) {
2833 if (copy_from_user(&params, optval, optlen))
2834 return -EFAULT;
2835
2836 if (params.sack_delay == 0 && params.sack_freq == 0)
2837 return 0;
2838 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05002839 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05002840 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05002841 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05002842 "Use struct sctp_sack_info instead\n",
2843 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07002844 if (copy_from_user(&params, optval, optlen))
2845 return -EFAULT;
2846
2847 if (params.sack_delay == 0)
2848 params.sack_freq = 1;
2849 else
2850 params.sack_freq = 0;
2851 } else
wangweidongcb3f8372013-12-23 12:16:50 +08002852 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08002853
Frank Filz77086102005-12-22 11:37:30 -08002854 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002855 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002856 return -EINVAL;
2857
Wei Yongjund364d922008-05-09 15:13:26 -07002858 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002859 * to many style socket, and an association was not found, then
2860 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002861 */
Wei Yongjund364d922008-05-09 15:13:26 -07002862 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2863 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002864 return -EINVAL;
2865
Wei Yongjund364d922008-05-09 15:13:26 -07002866 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002867 if (asoc) {
2868 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002869 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002870 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002871 sctp_spp_sackdelay_enable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002872 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002873 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002874 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002875 sctp_spp_sackdelay_enable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002876 }
Wei Yongjund364d922008-05-09 15:13:26 -07002877 }
2878
2879 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002880 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002881 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002882 sctp_spp_sackdelay_disable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002883 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002884 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002885 sctp_spp_sackdelay_disable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002886 }
Wei Yongjund364d922008-05-09 15:13:26 -07002887 } else if (params.sack_freq > 1) {
2888 if (asoc) {
2889 asoc->sackfreq = params.sack_freq;
2890 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002891 sctp_spp_sackdelay_enable(asoc->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002892 } else {
2893 sp->sackfreq = params.sack_freq;
2894 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002895 sctp_spp_sackdelay_enable(sp->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002896 }
Frank Filz77086102005-12-22 11:37:30 -08002897 }
2898
2899 /* If change is for association, also apply to each transport. */
2900 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002901 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2902 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002903 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002904 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002905 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002906 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002907 sctp_spp_sackdelay_enable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002908 }
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07002909 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002910 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002911 sctp_spp_sackdelay_disable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002912 } else if (params.sack_freq > 1) {
2913 trans->sackfreq = params.sack_freq;
2914 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002915 sctp_spp_sackdelay_enable(trans->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002916 }
2917 }
2918 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002919
Frank Filz77086102005-12-22 11:37:30 -08002920 return 0;
2921}
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2924 *
2925 * Applications can specify protocol parameters for the default association
2926 * initialization. The option name argument to setsockopt() and getsockopt()
2927 * is SCTP_INITMSG.
2928 *
2929 * Setting initialization parameters is effective only on an unconnected
2930 * socket (for UDP-style sockets only future associations are effected
2931 * by the change). With TCP-style sockets, this option is inherited by
2932 * sockets derived from a listener socket.
2933 */
David S. Millerb7058842009-09-30 16:12:20 -07002934static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
2936 struct sctp_initmsg sinit;
2937 struct sctp_sock *sp = sctp_sk(sk);
2938
2939 if (optlen != sizeof(struct sctp_initmsg))
2940 return -EINVAL;
2941 if (copy_from_user(&sinit, optval, optlen))
2942 return -EFAULT;
2943
2944 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002945 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002947 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002949 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002951 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953 return 0;
2954}
2955
2956/*
2957 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2958 *
2959 * Applications that wish to use the sendto() system call may wish to
2960 * specify a default set of parameters that would normally be supplied
2961 * through the inclusion of ancillary data. This socket option allows
2962 * such an application to set the default sctp_sndrcvinfo structure.
2963 * The application that wishes to use this socket option simply passes
2964 * in to this call the sctp_sndrcvinfo structure defined in Section
2965 * 5.2.2) The input parameters accepted by this call include
2966 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2967 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2968 * to this call if the caller is using the UDP model.
2969 */
2970static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002971 char __user *optval,
2972 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002975 struct sctp_association *asoc;
2976 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002978 if (optlen != sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return -EINVAL;
2980 if (copy_from_user(&info, optval, optlen))
2981 return -EFAULT;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002982 if (info.sinfo_flags &
2983 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2984 SCTP_ABORT | SCTP_EOF))
2985 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2988 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2989 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 if (asoc) {
2991 asoc->default_stream = info.sinfo_stream;
2992 asoc->default_flags = info.sinfo_flags;
2993 asoc->default_ppid = info.sinfo_ppid;
2994 asoc->default_context = info.sinfo_context;
2995 asoc->default_timetolive = info.sinfo_timetolive;
2996 } else {
2997 sp->default_stream = info.sinfo_stream;
2998 sp->default_flags = info.sinfo_flags;
2999 sp->default_ppid = info.sinfo_ppid;
3000 sp->default_context = info.sinfo_context;
3001 sp->default_timetolive = info.sinfo_timetolive;
3002 }
3003
3004 return 0;
3005}
3006
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003007/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
3008 * (SCTP_DEFAULT_SNDINFO)
3009 */
3010static int sctp_setsockopt_default_sndinfo(struct sock *sk,
3011 char __user *optval,
3012 unsigned int optlen)
3013{
3014 struct sctp_sock *sp = sctp_sk(sk);
3015 struct sctp_association *asoc;
3016 struct sctp_sndinfo info;
3017
3018 if (optlen != sizeof(info))
3019 return -EINVAL;
3020 if (copy_from_user(&info, optval, optlen))
3021 return -EFAULT;
3022 if (info.snd_flags &
3023 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3024 SCTP_ABORT | SCTP_EOF))
3025 return -EINVAL;
3026
3027 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
3028 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
3029 return -EINVAL;
3030 if (asoc) {
3031 asoc->default_stream = info.snd_sid;
3032 asoc->default_flags = info.snd_flags;
3033 asoc->default_ppid = info.snd_ppid;
3034 asoc->default_context = info.snd_context;
3035 } else {
3036 sp->default_stream = info.snd_sid;
3037 sp->default_flags = info.snd_flags;
3038 sp->default_ppid = info.snd_ppid;
3039 sp->default_context = info.snd_context;
3040 }
3041
3042 return 0;
3043}
3044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3046 *
3047 * Requests that the local SCTP stack use the enclosed peer address as
3048 * the association primary. The enclosed address must be one of the
3049 * association peer's addresses.
3050 */
3051static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003052 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
3054 struct sctp_prim prim;
3055 struct sctp_transport *trans;
Richard Haines2277c7c2018-02-13 20:56:24 +00003056 struct sctp_af *af;
3057 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
3059 if (optlen != sizeof(struct sctp_prim))
3060 return -EINVAL;
3061
3062 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3063 return -EFAULT;
3064
Richard Haines2277c7c2018-02-13 20:56:24 +00003065 /* Allow security module to validate address but need address len. */
3066 af = sctp_get_af_specific(prim.ssp_addr.ss_family);
3067 if (!af)
3068 return -EINVAL;
3069
3070 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
3071 (struct sockaddr *)&prim.ssp_addr,
3072 af->sockaddr_len);
3073 if (err)
3074 return err;
3075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
3077 if (!trans)
3078 return -EINVAL;
3079
3080 sctp_assoc_set_primary(trans->asoc, trans);
3081
3082 return 0;
3083}
3084
3085/*
3086 * 7.1.5 SCTP_NODELAY
3087 *
3088 * Turn on/off any Nagle-like algorithm. This means that packets are
3089 * generally sent as soon as possible and no unnecessary delays are
3090 * introduced, at the cost of more packets in the network. Expects an
3091 * integer boolean flag.
3092 */
3093static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003094 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095{
3096 int val;
3097
3098 if (optlen < sizeof(int))
3099 return -EINVAL;
3100 if (get_user(val, (int __user *)optval))
3101 return -EFAULT;
3102
3103 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3104 return 0;
3105}
3106
3107/*
3108 *
3109 * 7.1.1 SCTP_RTOINFO
3110 *
3111 * The protocol parameters used to initialize and bound retransmission
3112 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3113 * and modify these parameters.
3114 * All parameters are time values, in milliseconds. A value of 0, when
3115 * modifying the parameters, indicates that the current value should not
3116 * be changed.
3117 *
3118 */
David S. Millerb7058842009-09-30 16:12:20 -07003119static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3120{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 struct sctp_rtoinfo rtoinfo;
3122 struct sctp_association *asoc;
wangweidong85f935d2013-12-11 09:50:38 +08003123 unsigned long rto_min, rto_max;
3124 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
3126 if (optlen != sizeof (struct sctp_rtoinfo))
3127 return -EINVAL;
3128
3129 if (copy_from_user(&rtoinfo, optval, optlen))
3130 return -EFAULT;
3131
3132 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3133
3134 /* Set the values to the specific association */
3135 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3136 return -EINVAL;
3137
wangweidong85f935d2013-12-11 09:50:38 +08003138 rto_max = rtoinfo.srto_max;
3139 rto_min = rtoinfo.srto_min;
3140
3141 if (rto_max)
3142 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3143 else
3144 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3145
3146 if (rto_min)
3147 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3148 else
3149 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3150
3151 if (rto_min > rto_max)
3152 return -EINVAL;
3153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 if (asoc) {
3155 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003156 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 msecs_to_jiffies(rtoinfo.srto_initial);
wangweidong85f935d2013-12-11 09:50:38 +08003158 asoc->rto_max = rto_max;
3159 asoc->rto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 } else {
3161 /* If there is no association or the association-id = 0
3162 * set the values to the endpoint.
3163 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 if (rtoinfo.srto_initial != 0)
3165 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
wangweidong85f935d2013-12-11 09:50:38 +08003166 sp->rtoinfo.srto_max = rto_max;
3167 sp->rtoinfo.srto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 }
3169
3170 return 0;
3171}
3172
3173/*
3174 *
3175 * 7.1.2 SCTP_ASSOCINFO
3176 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02003177 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 * of the association.
3179 * Returns an error if the new association retransmission value is
3180 * greater than the sum of the retransmission value of the peer.
3181 * See [SCTP] for more information.
3182 *
3183 */
David S. Millerb7058842009-09-30 16:12:20 -07003184static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
3186
3187 struct sctp_assocparams assocparams;
3188 struct sctp_association *asoc;
3189
3190 if (optlen != sizeof(struct sctp_assocparams))
3191 return -EINVAL;
3192 if (copy_from_user(&assocparams, optval, optlen))
3193 return -EFAULT;
3194
3195 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3196
3197 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3198 return -EINVAL;
3199
3200 /* Set the values to the specific association */
3201 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003202 if (assocparams.sasoc_asocmaxrxt != 0) {
3203 __u32 path_sum = 0;
3204 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003205 struct sctp_transport *peer_addr;
3206
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07003207 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3208 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003209 path_sum += peer_addr->pathmaxrxt;
3210 paths++;
3211 }
3212
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003213 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003214 * one path/transport. We do this because path
3215 * retransmissions are only counted when we have more
3216 * then one path.
3217 */
3218 if (paths > 1 &&
3219 assocparams.sasoc_asocmaxrxt > path_sum)
3220 return -EINVAL;
3221
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003223 }
3224
Daniel Borkmann52db8822013-06-25 18:17:27 +02003225 if (assocparams.sasoc_cookie_life != 0)
3226 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 } else {
3228 /* Set the values to the endpoint */
3229 struct sctp_sock *sp = sctp_sk(sk);
3230
3231 if (assocparams.sasoc_asocmaxrxt != 0)
3232 sp->assocparams.sasoc_asocmaxrxt =
3233 assocparams.sasoc_asocmaxrxt;
3234 if (assocparams.sasoc_cookie_life != 0)
3235 sp->assocparams.sasoc_cookie_life =
3236 assocparams.sasoc_cookie_life;
3237 }
3238 return 0;
3239}
3240
3241/*
3242 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3243 *
3244 * This socket option is a boolean flag which turns on or off mapped V4
3245 * addresses. If this option is turned on and the socket is type
3246 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3247 * If this option is turned off, then no mapping will be done of V4
3248 * addresses and a user will receive both PF_INET6 and PF_INET type
3249 * addresses on the socket.
3250 */
David S. Millerb7058842009-09-30 16:12:20 -07003251static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
3253 int val;
3254 struct sctp_sock *sp = sctp_sk(sk);
3255
3256 if (optlen < sizeof(int))
3257 return -EINVAL;
3258 if (get_user(val, (int __user *)optval))
3259 return -EFAULT;
3260 if (val)
3261 sp->v4mapped = 1;
3262 else
3263 sp->v4mapped = 0;
3264
3265 return 0;
3266}
3267
3268/*
Wei Yongjune89c2092008-12-25 16:54:58 -08003269 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3270 * This option will get or set the maximum size to put in any outgoing
3271 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 * fragmented by SCTP into the specified size. Note that the underlying
3273 * SCTP implementation may fragment into smaller sized chunks when the
3274 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08003275 * the user. The default value for this option is '0' which indicates
3276 * the user is NOT limiting fragmentation and only the PMTU will effect
3277 * SCTP's choice of DATA chunk size. Note also that values set larger
3278 * than the maximum size of an IP datagram will effectively let SCTP
3279 * control fragmentation (i.e. the same as setting this option to 0).
3280 *
3281 * The following structure is used to access and modify this parameter:
3282 *
3283 * struct sctp_assoc_value {
3284 * sctp_assoc_t assoc_id;
3285 * uint32_t assoc_value;
3286 * };
3287 *
3288 * assoc_id: This parameter is ignored for one-to-one style sockets.
3289 * For one-to-many style sockets this parameter indicates which
3290 * association the user is performing an action upon. Note that if
3291 * this field's value is zero then the endpoints default value is
3292 * changed (effecting future associations only).
3293 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 */
David S. Millerb7058842009-09-30 16:12:20 -07003295static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
Xin Longecca8f82017-11-17 14:11:11 +08003297 struct sctp_sock *sp = sctp_sk(sk);
Wei Yongjune89c2092008-12-25 16:54:58 -08003298 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 int val;
3301
Wei Yongjune89c2092008-12-25 16:54:58 -08003302 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003303 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003304 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003305 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003306 "Use struct sctp_assoc_value instead\n",
3307 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08003308 if (copy_from_user(&val, optval, optlen))
3309 return -EFAULT;
3310 params.assoc_id = 0;
3311 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3312 if (copy_from_user(&params, optval, optlen))
3313 return -EFAULT;
3314 val = params.assoc_value;
Xin Longecca8f82017-11-17 14:11:11 +08003315 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 return -EINVAL;
Xin Longecca8f82017-11-17 14:11:11 +08003317 }
Wei Yongjune89c2092008-12-25 16:54:58 -08003318
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003319 asoc = sctp_id2assoc(sk, params.assoc_id);
3320
Xin Longecca8f82017-11-17 14:11:11 +08003321 if (val) {
3322 int min_len, max_len;
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003323 __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
3324 sizeof(struct sctp_data_chunk);
Xin Longecca8f82017-11-17 14:11:11 +08003325
Marcelo Ricardo Leitnerfeddd6c2018-04-26 16:58:54 -03003326 min_len = sctp_mtu_payload(sp, SCTP_DEFAULT_MINSEGMENT,
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003327 datasize);
3328 max_len = SCTP_MAX_CHUNK_LEN - datasize;
Xin Longecca8f82017-11-17 14:11:11 +08003329
3330 if (val < min_len || val > max_len)
3331 return -EINVAL;
3332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Wei Yongjune89c2092008-12-25 16:54:58 -08003334 if (asoc) {
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003335 asoc->user_frag = val;
Marcelo Ricardo Leitner2f5e3c92018-04-26 16:58:55 -03003336 sctp_assoc_update_frag_point(asoc);
Wei Yongjune89c2092008-12-25 16:54:58 -08003337 } else {
Xin Longecca8f82017-11-17 14:11:11 +08003338 if (params.assoc_id && sctp_style(sk, UDP))
3339 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003340 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
3342
3343 return 0;
3344}
3345
3346
3347/*
3348 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3349 *
3350 * Requests that the peer mark the enclosed address as the association
3351 * primary. The enclosed address must be one of the association's
3352 * locally bound addresses. The following structure is used to make a
3353 * set primary request:
3354 */
3355static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003356 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003358 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 struct sctp_association *asoc = NULL;
3361 struct sctp_setpeerprim prim;
3362 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003363 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 int err;
3365
3366 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003368 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 return -EPERM;
3370
3371 if (optlen != sizeof(struct sctp_setpeerprim))
3372 return -EINVAL;
3373
3374 if (copy_from_user(&prim, optval, optlen))
3375 return -EFAULT;
3376
3377 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003378 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return -EINVAL;
3380
3381 if (!asoc->peer.asconf_capable)
3382 return -EPERM;
3383
3384 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3385 return -EPERM;
3386
3387 if (!sctp_state(asoc, ESTABLISHED))
3388 return -ENOTCONN;
3389
Wei Yongjun40a01032010-12-07 17:11:09 +00003390 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3391 if (!af)
3392 return -EINVAL;
3393
3394 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3395 return -EADDRNOTAVAIL;
3396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3398 return -EADDRNOTAVAIL;
3399
Richard Haines2277c7c2018-02-13 20:56:24 +00003400 /* Allow security module to validate address. */
3401 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3402 (struct sockaddr *)&prim.sspp_addr,
3403 af->sockaddr_len);
3404 if (err)
3405 return err;
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3408 chunk = sctp_make_asconf_set_prim(asoc,
3409 (union sctp_addr *)&prim.sspp_addr);
3410 if (!chunk)
3411 return -ENOMEM;
3412
3413 err = sctp_send_asconf(asoc, chunk);
3414
Daniel Borkmannbb333812013-06-28 19:49:40 +02003415 pr_debug("%s: we set peer primary addr primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
3417 return err;
3418}
3419
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003420static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003421 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003423 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003425 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003427 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return -EFAULT;
3429
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003430 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
3432 return 0;
3433}
3434
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003435/*
3436 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3437 *
3438 * The context field in the sctp_sndrcvinfo structure is normally only
3439 * used when a failed message is retrieved holding the value that was
3440 * sent down on the actual send call. This option allows the setting of
3441 * a default context on an association basis that will be received on
3442 * reading messages from the peer. This is especially helpful in the
3443 * one-2-many model for an application to keep some reference to an
3444 * internal state machine that is processing messages on the
3445 * association. Note that the setting of this value only effects
3446 * received messages from the peer and does not effect the value that is
3447 * saved with outbound messages.
3448 */
3449static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003450 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003451{
3452 struct sctp_assoc_value params;
3453 struct sctp_sock *sp;
3454 struct sctp_association *asoc;
3455
3456 if (optlen != sizeof(struct sctp_assoc_value))
3457 return -EINVAL;
3458 if (copy_from_user(&params, optval, optlen))
3459 return -EFAULT;
3460
3461 sp = sctp_sk(sk);
3462
3463 if (params.assoc_id != 0) {
3464 asoc = sctp_id2assoc(sk, params.assoc_id);
3465 if (!asoc)
3466 return -EINVAL;
3467 asoc->default_rcv_context = params.assoc_value;
3468 } else {
3469 sp->default_rcv_context = params.assoc_value;
3470 }
3471
3472 return 0;
3473}
3474
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003475/*
3476 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3477 *
3478 * This options will at a minimum specify if the implementation is doing
3479 * fragmented interleave. Fragmented interleave, for a one to many
3480 * socket, is when subsequent calls to receive a message may return
3481 * parts of messages from different associations. Some implementations
3482 * may allow you to turn this value on or off. If so, when turned off,
3483 * no fragment interleave will occur (which will cause a head of line
3484 * blocking amongst multiple associations sharing the same one to many
3485 * socket). When this option is turned on, then each receive call may
3486 * come from a different association (thus the user must receive data
3487 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3488 * association each receive belongs to.
3489 *
3490 * This option takes a boolean value. A non-zero value indicates that
3491 * fragmented interleave is on. A value of zero indicates that
3492 * fragmented interleave is off.
3493 *
3494 * Note that it is important that an implementation that allows this
3495 * option to be turned on, have it off by default. Otherwise an unaware
3496 * application using the one to many model may become confused and act
3497 * incorrectly.
3498 */
3499static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3500 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003501 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003502{
3503 int val;
3504
3505 if (optlen != sizeof(int))
3506 return -EINVAL;
3507 if (get_user(val, (int __user *)optval))
3508 return -EFAULT;
3509
Xin Long772a5862017-12-08 21:03:58 +08003510 sctp_sk(sk)->frag_interleave = !!val;
3511
3512 if (!sctp_sk(sk)->frag_interleave)
3513 sctp_sk(sk)->strm_interleave = 0;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003514
3515 return 0;
3516}
3517
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003518/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003519 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003520 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003521 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003522 * This option will set or get the SCTP partial delivery point. This
3523 * point is the size of a message where the partial delivery API will be
3524 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003525 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003526 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003527 * point. Note also that the call will fail if the user attempts to set
3528 * this value larger than the socket receive buffer size.
3529 *
3530 * Note that any single message having a length smaller than or equal to
3531 * the SCTP partial delivery point will be delivered in one single read
3532 * call as long as the user provided buffer is large enough to hold the
3533 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003534 */
3535static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3536 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003537 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003538{
3539 u32 val;
3540
3541 if (optlen != sizeof(u32))
3542 return -EINVAL;
3543 if (get_user(val, (int __user *)optval))
3544 return -EFAULT;
3545
Wei Yongjun8510b932008-12-25 16:59:03 -08003546 /* Note: We double the receive buffer from what the user sets
3547 * it to be, also initial rwnd is based on rcvbuf/2.
3548 */
3549 if (val > (sk->sk_rcvbuf >> 1))
3550 return -EINVAL;
3551
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003552 sctp_sk(sk)->pd_point = val;
3553
3554 return 0; /* is this the right error code? */
3555}
3556
Vlad Yasevich70331572007-03-23 11:34:36 -07003557/*
3558 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3559 *
3560 * This option will allow a user to change the maximum burst of packets
3561 * that can be emitted by this association. Note that the default value
3562 * is 4, and some implementations may restrict this setting so that it
3563 * can only be lowered.
3564 *
3565 * NOTE: This text doesn't seem right. Do this on a socket basis with
3566 * future associations inheriting the socket value.
3567 */
3568static int sctp_setsockopt_maxburst(struct sock *sk,
3569 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003570 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003571{
Neil Horman219b99a2008-03-05 13:44:46 -08003572 struct sctp_assoc_value params;
3573 struct sctp_sock *sp;
3574 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003575 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003576 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003577
Neil Horman219b99a2008-03-05 13:44:46 -08003578 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003579 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003580 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003581 "Use of int in max_burst socket option deprecated.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003582 "Use struct sctp_assoc_value instead\n",
3583 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08003584 if (copy_from_user(&val, optval, optlen))
3585 return -EFAULT;
3586 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3587 if (copy_from_user(&params, optval, optlen))
3588 return -EFAULT;
3589 val = params.assoc_value;
3590 assoc_id = params.assoc_id;
3591 } else
3592 return -EINVAL;
3593
3594 sp = sctp_sk(sk);
3595
3596 if (assoc_id != 0) {
3597 asoc = sctp_id2assoc(sk, assoc_id);
3598 if (!asoc)
3599 return -EINVAL;
3600 asoc->max_burst = val;
3601 } else
3602 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003603
3604 return 0;
3605}
3606
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003607/*
3608 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3609 *
3610 * This set option adds a chunk type that the user is requesting to be
3611 * received only in an authenticated way. Changes to the list of chunks
3612 * will only effect future associations on the socket.
3613 */
3614static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003615 char __user *optval,
3616 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003617{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003618 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003619 struct sctp_authchunk val;
3620
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003621 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003622 return -EACCES;
3623
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003624 if (optlen != sizeof(struct sctp_authchunk))
3625 return -EINVAL;
3626 if (copy_from_user(&val, optval, optlen))
3627 return -EFAULT;
3628
3629 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003630 case SCTP_CID_INIT:
3631 case SCTP_CID_INIT_ACK:
3632 case SCTP_CID_SHUTDOWN_COMPLETE:
3633 case SCTP_CID_AUTH:
3634 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003635 }
3636
3637 /* add this chunk id to the endpoint */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003638 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003639}
3640
3641/*
3642 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3643 *
3644 * This option gets or sets the list of HMAC algorithms that the local
3645 * endpoint requires the peer to use.
3646 */
3647static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003648 char __user *optval,
3649 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003650{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003651 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003652 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003653 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003654 int err;
3655
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003656 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003657 return -EACCES;
3658
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003659 if (optlen < sizeof(struct sctp_hmacalgo))
3660 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003661 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3662 SCTP_AUTH_NUM_HMACS * sizeof(u16));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003663
wangweidongcb3f8372013-12-23 12:16:50 +08003664 hmacs = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003665 if (IS_ERR(hmacs))
3666 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003667
Vlad Yasevichd9724052008-08-27 16:09:49 -07003668 idents = hmacs->shmac_num_idents;
3669 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3670 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003671 err = -EINVAL;
3672 goto out;
3673 }
3674
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003675 err = sctp_auth_ep_set_hmacs(ep, hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003676out:
3677 kfree(hmacs);
3678 return err;
3679}
3680
3681/*
3682 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3683 *
3684 * This option will set a shared secret key which is used to build an
3685 * association shared key.
3686 */
3687static int sctp_setsockopt_auth_key(struct sock *sk,
3688 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003689 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003690{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003691 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003692 struct sctp_authkey *authkey;
3693 struct sctp_association *asoc;
3694 int ret;
3695
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003696 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003697 return -EACCES;
3698
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003699 if (optlen <= sizeof(struct sctp_authkey))
3700 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003701 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3702 * this.
3703 */
3704 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3705 sizeof(struct sctp_authkey));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003706
wangweidongcb3f8372013-12-23 12:16:50 +08003707 authkey = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003708 if (IS_ERR(authkey))
3709 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003710
Vlad Yasevich328fc472008-08-27 16:08:54 -07003711 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003712 ret = -EINVAL;
3713 goto out;
3714 }
3715
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003716 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3717 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3718 ret = -EINVAL;
3719 goto out;
3720 }
3721
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003722 ret = sctp_auth_set_key(ep, asoc, authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003723out:
Daniel Borkmann6ba542a2013-02-08 03:04:34 +00003724 kzfree(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003725 return ret;
3726}
3727
3728/*
3729 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3730 *
3731 * This option will get or set the active shared key to be used to build
3732 * the association shared key.
3733 */
3734static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003735 char __user *optval,
3736 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003737{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003738 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003739 struct sctp_authkeyid val;
3740 struct sctp_association *asoc;
3741
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003742 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003743 return -EACCES;
3744
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003745 if (optlen != sizeof(struct sctp_authkeyid))
3746 return -EINVAL;
3747 if (copy_from_user(&val, optval, optlen))
3748 return -EFAULT;
3749
3750 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3751 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3752 return -EINVAL;
3753
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003754 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003755}
3756
3757/*
3758 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3759 *
3760 * This set option will delete a shared secret key from use.
3761 */
3762static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003763 char __user *optval,
3764 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003765{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003766 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003767 struct sctp_authkeyid val;
3768 struct sctp_association *asoc;
3769
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003770 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003771 return -EACCES;
3772
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003773 if (optlen != sizeof(struct sctp_authkeyid))
3774 return -EINVAL;
3775 if (copy_from_user(&val, optval, optlen))
3776 return -EFAULT;
3777
3778 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3779 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3780 return -EINVAL;
3781
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003782 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003783
3784}
3785
Michio Honda7dc04d72011-04-26 20:16:31 +09003786/*
Xin Long601590e2018-03-14 19:05:32 +08003787 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
3788 *
3789 * This set option will deactivate a shared secret key.
3790 */
3791static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
3792 unsigned int optlen)
3793{
3794 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3795 struct sctp_authkeyid val;
3796 struct sctp_association *asoc;
3797
3798 if (!ep->auth_enable)
3799 return -EACCES;
3800
3801 if (optlen != sizeof(struct sctp_authkeyid))
3802 return -EINVAL;
3803 if (copy_from_user(&val, optval, optlen))
3804 return -EFAULT;
3805
3806 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3807 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3808 return -EINVAL;
3809
3810 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3811}
3812
3813/*
Michio Honda7dc04d72011-04-26 20:16:31 +09003814 * 8.1.23 SCTP_AUTO_ASCONF
3815 *
3816 * This option will enable or disable the use of the automatic generation of
3817 * ASCONF chunks to add and delete addresses to an existing association. Note
3818 * that this option has two caveats namely: a) it only affects sockets that
3819 * are bound to all addresses available to the SCTP stack, and b) the system
3820 * administrator may have an overriding control that turns the ASCONF feature
3821 * off no matter what setting the socket option may have.
3822 * This option expects an integer boolean flag, where a non-zero value turns on
3823 * the option, and a zero value turns off the option.
3824 * Note. In this implementation, socket operation overrides default parameter
3825 * being set by sysctl as well as FreeBSD implementation
3826 */
3827static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3828 unsigned int optlen)
3829{
3830 int val;
3831 struct sctp_sock *sp = sctp_sk(sk);
3832
3833 if (optlen < sizeof(int))
3834 return -EINVAL;
3835 if (get_user(val, (int __user *)optval))
3836 return -EFAULT;
3837 if (!sctp_is_ep_boundall(sk) && val)
3838 return -EINVAL;
3839 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3840 return 0;
3841
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003842 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003843 if (val == 0 && sp->do_auto_asconf) {
3844 list_del(&sp->auto_asconf_list);
3845 sp->do_auto_asconf = 0;
3846 } else if (val && !sp->do_auto_asconf) {
3847 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00003848 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09003849 sp->do_auto_asconf = 1;
3850 }
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003851 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003852 return 0;
3853}
3854
Neil Horman5aa93bc2012-07-21 07:56:07 +00003855/*
3856 * SCTP_PEER_ADDR_THLDS
3857 *
3858 * This option allows us to alter the partially failed threshold for one or all
3859 * transports in an association. See Section 6.1 of:
3860 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3861 */
3862static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3863 char __user *optval,
3864 unsigned int optlen)
3865{
3866 struct sctp_paddrthlds val;
3867 struct sctp_transport *trans;
3868 struct sctp_association *asoc;
3869
3870 if (optlen < sizeof(struct sctp_paddrthlds))
3871 return -EINVAL;
3872 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3873 sizeof(struct sctp_paddrthlds)))
3874 return -EFAULT;
3875
3876
3877 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3878 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3879 if (!asoc)
3880 return -ENOENT;
3881 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3882 transports) {
3883 if (val.spt_pathmaxrxt)
3884 trans->pathmaxrxt = val.spt_pathmaxrxt;
3885 trans->pf_retrans = val.spt_pathpfthld;
3886 }
3887
3888 if (val.spt_pathmaxrxt)
3889 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3890 asoc->pf_retrans = val.spt_pathpfthld;
3891 } else {
3892 trans = sctp_addr_id2transport(sk, &val.spt_address,
3893 val.spt_assoc_id);
3894 if (!trans)
3895 return -ENOENT;
3896
3897 if (val.spt_pathmaxrxt)
3898 trans->pathmaxrxt = val.spt_pathmaxrxt;
3899 trans->pf_retrans = val.spt_pathpfthld;
3900 }
3901
3902 return 0;
3903}
3904
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02003905static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3906 char __user *optval,
3907 unsigned int optlen)
3908{
3909 int val;
3910
3911 if (optlen < sizeof(int))
3912 return -EINVAL;
3913 if (get_user(val, (int __user *) optval))
3914 return -EFAULT;
3915
3916 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3917
3918 return 0;
3919}
3920
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02003921static int sctp_setsockopt_recvnxtinfo(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)->recvnxtinfo = (val == 0) ? 0 : 1;
3933
3934 return 0;
3935}
3936
Xin Long28aa4c22016-07-09 19:47:40 +08003937static int sctp_setsockopt_pr_supported(struct sock *sk,
3938 char __user *optval,
3939 unsigned int optlen)
3940{
3941 struct sctp_assoc_value params;
3942 struct sctp_association *asoc;
3943 int retval = -EINVAL;
3944
3945 if (optlen != sizeof(params))
3946 goto out;
3947
3948 if (copy_from_user(&params, optval, optlen)) {
3949 retval = -EFAULT;
3950 goto out;
3951 }
3952
3953 asoc = sctp_id2assoc(sk, params.assoc_id);
3954 if (asoc) {
3955 asoc->prsctp_enable = !!params.assoc_value;
3956 } else if (!params.assoc_id) {
3957 struct sctp_sock *sp = sctp_sk(sk);
3958
3959 sp->ep->prsctp_enable = !!params.assoc_value;
3960 } else {
3961 goto out;
3962 }
3963
3964 retval = 0;
3965
3966out:
3967 return retval;
3968}
3969
Xin Longf959fb42016-07-09 19:47:41 +08003970static int sctp_setsockopt_default_prinfo(struct sock *sk,
3971 char __user *optval,
3972 unsigned int optlen)
3973{
3974 struct sctp_default_prinfo info;
3975 struct sctp_association *asoc;
3976 int retval = -EINVAL;
3977
3978 if (optlen != sizeof(info))
3979 goto out;
3980
3981 if (copy_from_user(&info, optval, sizeof(info))) {
3982 retval = -EFAULT;
3983 goto out;
3984 }
3985
3986 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
3987 goto out;
3988
3989 if (info.pr_policy == SCTP_PR_SCTP_NONE)
3990 info.pr_value = 0;
3991
3992 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
3993 if (asoc) {
3994 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
3995 asoc->default_timetolive = info.pr_value;
3996 } else if (!info.pr_assoc_id) {
3997 struct sctp_sock *sp = sctp_sk(sk);
3998
3999 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
4000 sp->default_timetolive = info.pr_value;
4001 } else {
4002 goto out;
4003 }
4004
4005 retval = 0;
4006
4007out:
4008 return retval;
4009}
4010
Xin Longc0d8bab2017-03-10 12:11:12 +08004011static int sctp_setsockopt_reconfig_supported(struct sock *sk,
4012 char __user *optval,
4013 unsigned int optlen)
4014{
4015 struct sctp_assoc_value params;
4016 struct sctp_association *asoc;
4017 int retval = -EINVAL;
4018
4019 if (optlen != sizeof(params))
4020 goto out;
4021
4022 if (copy_from_user(&params, optval, optlen)) {
4023 retval = -EFAULT;
4024 goto out;
4025 }
4026
4027 asoc = sctp_id2assoc(sk, params.assoc_id);
4028 if (asoc) {
4029 asoc->reconf_enable = !!params.assoc_value;
4030 } else if (!params.assoc_id) {
4031 struct sctp_sock *sp = sctp_sk(sk);
4032
4033 sp->ep->reconf_enable = !!params.assoc_value;
4034 } else {
4035 goto out;
4036 }
4037
4038 retval = 0;
4039
4040out:
4041 return retval;
4042}
4043
Xin Long9fb657a2017-01-18 00:44:46 +08004044static int sctp_setsockopt_enable_strreset(struct sock *sk,
4045 char __user *optval,
4046 unsigned int optlen)
4047{
4048 struct sctp_assoc_value params;
4049 struct sctp_association *asoc;
4050 int retval = -EINVAL;
4051
4052 if (optlen != sizeof(params))
4053 goto out;
4054
4055 if (copy_from_user(&params, optval, optlen)) {
4056 retval = -EFAULT;
4057 goto out;
4058 }
4059
4060 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
4061 goto out;
4062
4063 asoc = sctp_id2assoc(sk, params.assoc_id);
4064 if (asoc) {
4065 asoc->strreset_enable = params.assoc_value;
4066 } else if (!params.assoc_id) {
4067 struct sctp_sock *sp = sctp_sk(sk);
4068
4069 sp->ep->strreset_enable = params.assoc_value;
4070 } else {
4071 goto out;
4072 }
4073
4074 retval = 0;
4075
4076out:
4077 return retval;
4078}
4079
Xin Long7f9d68a2017-01-18 00:44:47 +08004080static int sctp_setsockopt_reset_streams(struct sock *sk,
4081 char __user *optval,
4082 unsigned int optlen)
4083{
4084 struct sctp_reset_streams *params;
4085 struct sctp_association *asoc;
4086 int retval = -EINVAL;
4087
Xin Long2342b8d2017-12-10 15:40:51 +08004088 if (optlen < sizeof(*params))
Xin Long7f9d68a2017-01-18 00:44:47 +08004089 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02004090 /* srs_number_streams is u16, so optlen can't be bigger than this. */
4091 optlen = min_t(unsigned int, optlen, USHRT_MAX +
4092 sizeof(__u16) * sizeof(*params));
Xin Long7f9d68a2017-01-18 00:44:47 +08004093
4094 params = memdup_user(optval, optlen);
4095 if (IS_ERR(params))
4096 return PTR_ERR(params);
4097
Xin Long2342b8d2017-12-10 15:40:51 +08004098 if (params->srs_number_streams * sizeof(__u16) >
4099 optlen - sizeof(*params))
4100 goto out;
4101
Xin Long7f9d68a2017-01-18 00:44:47 +08004102 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4103 if (!asoc)
4104 goto out;
4105
4106 retval = sctp_send_reset_streams(asoc, params);
4107
4108out:
4109 kfree(params);
4110 return retval;
4111}
4112
Xin Longa92ce1a2017-02-09 01:18:18 +08004113static int sctp_setsockopt_reset_assoc(struct sock *sk,
4114 char __user *optval,
4115 unsigned int optlen)
4116{
4117 struct sctp_association *asoc;
4118 sctp_assoc_t associd;
4119 int retval = -EINVAL;
4120
4121 if (optlen != sizeof(associd))
4122 goto out;
4123
4124 if (copy_from_user(&associd, optval, optlen)) {
4125 retval = -EFAULT;
4126 goto out;
4127 }
4128
4129 asoc = sctp_id2assoc(sk, associd);
4130 if (!asoc)
4131 goto out;
4132
4133 retval = sctp_send_reset_assoc(asoc);
4134
4135out:
4136 return retval;
4137}
4138
Xin Long242bd2d2017-02-09 01:18:20 +08004139static int sctp_setsockopt_add_streams(struct sock *sk,
4140 char __user *optval,
4141 unsigned int optlen)
4142{
4143 struct sctp_association *asoc;
4144 struct sctp_add_streams params;
4145 int retval = -EINVAL;
4146
4147 if (optlen != sizeof(params))
4148 goto out;
4149
4150 if (copy_from_user(&params, optval, optlen)) {
4151 retval = -EFAULT;
4152 goto out;
4153 }
4154
4155 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
4156 if (!asoc)
4157 goto out;
4158
4159 retval = sctp_send_add_streams(asoc, &params);
4160
4161out:
4162 return retval;
4163}
4164
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004165static int sctp_setsockopt_scheduler(struct sock *sk,
4166 char __user *optval,
4167 unsigned int optlen)
4168{
4169 struct sctp_association *asoc;
4170 struct sctp_assoc_value params;
4171 int retval = -EINVAL;
4172
4173 if (optlen < sizeof(params))
4174 goto out;
4175
4176 optlen = sizeof(params);
4177 if (copy_from_user(&params, optval, optlen)) {
4178 retval = -EFAULT;
4179 goto out;
4180 }
4181
4182 if (params.assoc_value > SCTP_SS_MAX)
4183 goto out;
4184
4185 asoc = sctp_id2assoc(sk, params.assoc_id);
4186 if (!asoc)
4187 goto out;
4188
4189 retval = sctp_sched_set_sched(asoc, params.assoc_value);
4190
4191out:
4192 return retval;
4193}
4194
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004195static int sctp_setsockopt_scheduler_value(struct sock *sk,
4196 char __user *optval,
4197 unsigned int optlen)
4198{
4199 struct sctp_association *asoc;
4200 struct sctp_stream_value params;
4201 int retval = -EINVAL;
4202
4203 if (optlen < sizeof(params))
4204 goto out;
4205
4206 optlen = sizeof(params);
4207 if (copy_from_user(&params, optval, optlen)) {
4208 retval = -EFAULT;
4209 goto out;
4210 }
4211
4212 asoc = sctp_id2assoc(sk, params.assoc_id);
4213 if (!asoc)
4214 goto out;
4215
4216 retval = sctp_sched_set_value(asoc, params.stream_id,
4217 params.stream_value, GFP_KERNEL);
4218
4219out:
4220 return retval;
4221}
4222
Xin Long772a5862017-12-08 21:03:58 +08004223static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4224 char __user *optval,
4225 unsigned int optlen)
4226{
4227 struct sctp_sock *sp = sctp_sk(sk);
4228 struct net *net = sock_net(sk);
4229 struct sctp_assoc_value params;
4230 int retval = -EINVAL;
4231
4232 if (optlen < sizeof(params))
4233 goto out;
4234
4235 optlen = sizeof(params);
4236 if (copy_from_user(&params, optval, optlen)) {
4237 retval = -EFAULT;
4238 goto out;
4239 }
4240
4241 if (params.assoc_id)
4242 goto out;
4243
4244 if (!net->sctp.intl_enable || !sp->frag_interleave) {
4245 retval = -EPERM;
4246 goto out;
4247 }
4248
4249 sp->strm_interleave = !!params.assoc_value;
4250
4251 retval = 0;
4252
4253out:
4254 return retval;
4255}
4256
Xin Longb0e9a2f2018-06-28 15:31:00 +08004257static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval,
4258 unsigned int optlen)
4259{
4260 int val;
4261
4262 if (!sctp_style(sk, TCP))
4263 return -EOPNOTSUPP;
4264
4265 if (sctp_sk(sk)->ep->base.bind_addr.port)
4266 return -EFAULT;
4267
4268 if (optlen < sizeof(int))
4269 return -EINVAL;
4270
4271 if (get_user(val, (int __user *)optval))
4272 return -EFAULT;
4273
4274 sctp_sk(sk)->reuse = !!val;
4275
4276 return 0;
4277}
4278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279/* API 6.2 setsockopt(), getsockopt()
4280 *
4281 * Applications use setsockopt() and getsockopt() to set or retrieve
4282 * socket options. Socket options are used to change the default
4283 * behavior of sockets calls. They are described in Section 7.
4284 *
4285 * The syntax is:
4286 *
4287 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4288 * int __user *optlen);
4289 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4290 * int optlen);
4291 *
4292 * sd - the socket descript.
4293 * level - set to IPPROTO_SCTP for all SCTP options.
4294 * optname - the option name.
4295 * optval - the buffer to store the value of the option.
4296 * optlen - the size of the buffer.
4297 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004298static int sctp_setsockopt(struct sock *sk, int level, int optname,
4299 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
4301 int retval = 0;
4302
Daniel Borkmannbb333812013-06-28 19:49:40 +02004303 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
4305 /* I can hardly begin to describe how wrong this is. This is
4306 * so broken as to be worse than useless. The API draft
4307 * REALLY is NOT helpful here... I am not convinced that the
4308 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4309 * are at all well-founded.
4310 */
4311 if (level != SOL_SCTP) {
4312 struct sctp_af *af = sctp_sk(sk)->pf->af;
4313 retval = af->setsockopt(sk, level, optname, optval, optlen);
4314 goto out_nounlock;
4315 }
4316
wangweidong048ed4b2014-01-21 15:44:11 +08004317 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
4319 switch (optname) {
4320 case SCTP_SOCKOPT_BINDX_ADD:
4321 /* 'optlen' is the size of the addresses buffer. */
4322 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4323 optlen, SCTP_BINDX_ADD_ADDR);
4324 break;
4325
4326 case SCTP_SOCKOPT_BINDX_REM:
4327 /* 'optlen' is the size of the addresses buffer. */
4328 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4329 optlen, SCTP_BINDX_REM_ADDR);
4330 break;
4331
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004332 case SCTP_SOCKOPT_CONNECTX_OLD:
4333 /* 'optlen' is the size of the addresses buffer. */
4334 retval = sctp_setsockopt_connectx_old(sk,
4335 (struct sockaddr __user *)optval,
4336 optlen);
4337 break;
4338
Frank Filz3f7a87d2005-06-20 13:14:57 -07004339 case SCTP_SOCKOPT_CONNECTX:
4340 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004341 retval = sctp_setsockopt_connectx(sk,
4342 (struct sockaddr __user *)optval,
4343 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004344 break;
4345
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 case SCTP_DISABLE_FRAGMENTS:
4347 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4348 break;
4349
4350 case SCTP_EVENTS:
4351 retval = sctp_setsockopt_events(sk, optval, optlen);
4352 break;
4353
4354 case SCTP_AUTOCLOSE:
4355 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4356 break;
4357
4358 case SCTP_PEER_ADDR_PARAMS:
4359 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4360 break;
4361
Shan Wei4580ccc2011-01-18 22:39:00 +00004362 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07004363 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08004364 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004365 case SCTP_PARTIAL_DELIVERY_POINT:
4366 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4367 break;
Frank Filz77086102005-12-22 11:37:30 -08004368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 case SCTP_INITMSG:
4370 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4371 break;
4372 case SCTP_DEFAULT_SEND_PARAM:
4373 retval = sctp_setsockopt_default_send_param(sk, optval,
4374 optlen);
4375 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02004376 case SCTP_DEFAULT_SNDINFO:
4377 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4378 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 case SCTP_PRIMARY_ADDR:
4380 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4381 break;
4382 case SCTP_SET_PEER_PRIMARY_ADDR:
4383 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4384 break;
4385 case SCTP_NODELAY:
4386 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4387 break;
4388 case SCTP_RTOINFO:
4389 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4390 break;
4391 case SCTP_ASSOCINFO:
4392 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4393 break;
4394 case SCTP_I_WANT_MAPPED_V4_ADDR:
4395 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4396 break;
4397 case SCTP_MAXSEG:
4398 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4399 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004400 case SCTP_ADAPTATION_LAYER:
4401 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004403 case SCTP_CONTEXT:
4404 retval = sctp_setsockopt_context(sk, optval, optlen);
4405 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004406 case SCTP_FRAGMENT_INTERLEAVE:
4407 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4408 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07004409 case SCTP_MAX_BURST:
4410 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4411 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004412 case SCTP_AUTH_CHUNK:
4413 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4414 break;
4415 case SCTP_HMAC_IDENT:
4416 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4417 break;
4418 case SCTP_AUTH_KEY:
4419 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4420 break;
4421 case SCTP_AUTH_ACTIVE_KEY:
4422 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4423 break;
4424 case SCTP_AUTH_DELETE_KEY:
4425 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4426 break;
Xin Long601590e2018-03-14 19:05:32 +08004427 case SCTP_AUTH_DEACTIVATE_KEY:
4428 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
4429 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09004430 case SCTP_AUTO_ASCONF:
4431 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4432 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00004433 case SCTP_PEER_ADDR_THLDS:
4434 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4435 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004436 case SCTP_RECVRCVINFO:
4437 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4438 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004439 case SCTP_RECVNXTINFO:
4440 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4441 break;
Xin Long28aa4c22016-07-09 19:47:40 +08004442 case SCTP_PR_SUPPORTED:
4443 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4444 break;
Xin Longf959fb42016-07-09 19:47:41 +08004445 case SCTP_DEFAULT_PRINFO:
4446 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4447 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08004448 case SCTP_RECONFIG_SUPPORTED:
4449 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4450 break;
Xin Long9fb657a2017-01-18 00:44:46 +08004451 case SCTP_ENABLE_STREAM_RESET:
4452 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4453 break;
Xin Long7f9d68a2017-01-18 00:44:47 +08004454 case SCTP_RESET_STREAMS:
4455 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4456 break;
Xin Longa92ce1a2017-02-09 01:18:18 +08004457 case SCTP_RESET_ASSOC:
4458 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4459 break;
Xin Long242bd2d2017-02-09 01:18:20 +08004460 case SCTP_ADD_STREAMS:
4461 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4462 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004463 case SCTP_STREAM_SCHEDULER:
4464 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4465 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004466 case SCTP_STREAM_SCHEDULER_VALUE:
4467 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4468 break;
Xin Long772a5862017-12-08 21:03:58 +08004469 case SCTP_INTERLEAVING_SUPPORTED:
4470 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4471 optlen);
4472 break;
Xin Longb0e9a2f2018-06-28 15:31:00 +08004473 case SCTP_REUSE_PORT:
4474 retval = sctp_setsockopt_reuse_port(sk, optval, optlen);
4475 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 default:
4477 retval = -ENOPROTOOPT;
4478 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
wangweidong048ed4b2014-01-21 15:44:11 +08004481 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
4483out_nounlock:
4484 return retval;
4485}
4486
4487/* API 3.1.6 connect() - UDP Style Syntax
4488 *
4489 * An application may use the connect() call in the UDP model to initiate an
4490 * association without sending data.
4491 *
4492 * The syntax is:
4493 *
4494 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4495 *
4496 * sd: the socket descriptor to have a new association added to.
4497 *
4498 * nam: the address structure (either struct sockaddr_in or struct
4499 * sockaddr_in6 defined in RFC2553 [7]).
4500 *
4501 * len: the size of the address.
4502 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004503static int sctp_connect(struct sock *sk, struct sockaddr *addr,
Xin Long644fbde2018-05-20 16:39:10 +08004504 int addr_len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505{
Xin Long644fbde2018-05-20 16:39:10 +08004506 struct inet_sock *inet = inet_sk(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004507 struct sctp_af *af;
Xin Long644fbde2018-05-20 16:39:10 +08004508 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
wangweidong048ed4b2014-01-21 15:44:11 +08004510 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
Daniel Borkmannbb333812013-06-28 19:49:40 +02004512 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4513 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
Xin Long644fbde2018-05-20 16:39:10 +08004515 /* We may need to bind the socket. */
4516 if (!inet->inet_num) {
4517 if (sk->sk_prot->get_port(sk, 0)) {
4518 release_sock(sk);
4519 return -EAGAIN;
4520 }
4521 inet->inet_sport = htons(inet->inet_num);
4522 }
4523
Frank Filz3f7a87d2005-06-20 13:14:57 -07004524 /* Validate addr_len before calling common connect/connectx routine. */
4525 af = sctp_get_af_specific(addr->sa_family);
4526 if (!af || addr_len < af->sockaddr_len) {
4527 err = -EINVAL;
4528 } else {
4529 /* Pass correct addr len to common routine (so it knows there
4530 * is only one address being passed.
4531 */
Xin Long644fbde2018-05-20 16:39:10 +08004532 err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 }
4534
wangweidong048ed4b2014-01-21 15:44:11 +08004535 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 return err;
4537}
4538
Xin Long644fbde2018-05-20 16:39:10 +08004539int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
4540 int addr_len, int flags)
4541{
4542 if (addr_len < sizeof(uaddr->sa_family))
4543 return -EINVAL;
4544
4545 if (uaddr->sa_family == AF_UNSPEC)
4546 return -EOPNOTSUPP;
4547
4548 return sctp_connect(sock->sk, uaddr, addr_len, flags);
4549}
4550
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551/* FIXME: Write comments. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004552static int sctp_disconnect(struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553{
4554 return -EOPNOTSUPP; /* STUB */
4555}
4556
4557/* 4.1.4 accept() - TCP Style Syntax
4558 *
4559 * Applications use accept() call to remove an established SCTP
4560 * association from the accept queue of the endpoint. A new socket
4561 * descriptor will be returned from accept() to represent the newly
4562 * formed association.
4563 */
David Howellscdfbabf2017-03-09 08:09:05 +00004564static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
4566 struct sctp_sock *sp;
4567 struct sctp_endpoint *ep;
4568 struct sock *newsk = NULL;
4569 struct sctp_association *asoc;
4570 long timeo;
4571 int error = 0;
4572
wangweidong048ed4b2014-01-21 15:44:11 +08004573 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
4575 sp = sctp_sk(sk);
4576 ep = sp->ep;
4577
4578 if (!sctp_style(sk, TCP)) {
4579 error = -EOPNOTSUPP;
4580 goto out;
4581 }
4582
4583 if (!sctp_sstate(sk, LISTENING)) {
4584 error = -EINVAL;
4585 goto out;
4586 }
4587
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07004588 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
4590 error = sctp_wait_for_accept(sk, timeo);
4591 if (error)
4592 goto out;
4593
4594 /* We treat the list of associations on the endpoint as the accept
4595 * queue and pick the first association on the list.
4596 */
4597 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4598
David Howellscdfbabf2017-03-09 08:09:05 +00004599 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 if (!newsk) {
4601 error = -ENOMEM;
4602 goto out;
4603 }
4604
4605 /* Populate the fields of the newsk from the oldsk and migrate the
4606 * asoc to the newsk.
4607 */
4608 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4609
4610out:
wangweidong048ed4b2014-01-21 15:44:11 +08004611 release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004612 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 return newsk;
4614}
4615
4616/* The SCTP ioctl handler. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004617static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004619 int rc = -ENOTCONN;
4620
wangweidong048ed4b2014-01-21 15:44:11 +08004621 lock_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004622
4623 /*
4624 * SEQPACKET-style sockets in LISTENING state are valid, for
4625 * SCTP, so only discard TCP-style sockets in LISTENING state.
4626 */
4627 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4628 goto out;
4629
4630 switch (cmd) {
4631 case SIOCINQ: {
4632 struct sk_buff *skb;
4633 unsigned int amount = 0;
4634
4635 skb = skb_peek(&sk->sk_receive_queue);
4636 if (skb != NULL) {
4637 /*
4638 * We will only return the amount of this packet since
4639 * that is all that will be read.
4640 */
4641 amount = skb->len;
4642 }
4643 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004644 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07004645 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004646 default:
4647 rc = -ENOIOCTLCMD;
4648 break;
4649 }
4650out:
wangweidong048ed4b2014-01-21 15:44:11 +08004651 release_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004652 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653}
4654
4655/* This is the function which gets called during socket creation to
4656 * initialized the SCTP-specific portion of the sock.
4657 * The sock structure should already be zero-filled memory.
4658 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004659static int sctp_init_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004661 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 struct sctp_sock *sp;
4663
Daniel Borkmannbb333812013-06-28 19:49:40 +02004664 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
4666 sp = sctp_sk(sk);
4667
4668 /* Initialize the SCTP per socket area. */
4669 switch (sk->sk_type) {
4670 case SOCK_SEQPACKET:
4671 sp->type = SCTP_SOCKET_UDP;
4672 break;
4673 case SOCK_STREAM:
4674 sp->type = SCTP_SOCKET_TCP;
4675 break;
4676 default:
4677 return -ESOCKTNOSUPPORT;
4678 }
4679
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004680 sk->sk_gso_type = SKB_GSO_SCTP;
4681
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 /* Initialize default send parameters. These parameters can be
4683 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4684 */
4685 sp->default_stream = 0;
4686 sp->default_ppid = 0;
4687 sp->default_flags = 0;
4688 sp->default_context = 0;
4689 sp->default_timetolive = 0;
4690
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004691 sp->default_rcv_context = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004692 sp->max_burst = net->sctp.max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004693
Neil Horman3c681982012-10-24 09:20:03 +00004694 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4695
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 /* Initialize default setup parameters. These parameters
4697 * can be modified with the SCTP_INITMSG socket option or
4698 * overridden by the SCTP_INIT CMSG.
4699 */
4700 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4701 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004702 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4703 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
4705 /* Initialize default RTO related parameters. These parameters can
4706 * be modified for with the SCTP_RTOINFO socket option.
4707 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004708 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4709 sp->rtoinfo.srto_max = net->sctp.rto_max;
4710 sp->rtoinfo.srto_min = net->sctp.rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
4712 /* Initialize default association related parameters. These parameters
4713 * can be modified with the SCTP_ASSOCINFO socket option.
4714 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004715 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 sp->assocparams.sasoc_number_peer_destinations = 0;
4717 sp->assocparams.sasoc_peer_rwnd = 0;
4718 sp->assocparams.sasoc_local_rwnd = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004719 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
4721 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004722 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 */
4724 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4725
4726 /* Default Peer Address Parameters. These defaults can
4727 * be modified via SCTP_PEER_ADDR_PARAMS
4728 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004729 sp->hbinterval = net->sctp.hb_interval;
4730 sp->pathmaxrxt = net->sctp.max_retrans_path;
wangweidong4e2d52b2013-12-23 12:16:54 +08004731 sp->pathmtu = 0; /* allow default discovery */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004732 sp->sackdelay = net->sctp.sack_timeout;
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07004733 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004734 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004735 SPP_PMTUD_ENABLE |
4736 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
4738 /* If enabled no SCTP message fragmentation will be performed.
4739 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4740 */
4741 sp->disable_fragments = 0;
4742
Sridhar Samudrala208edef2006-09-29 17:08:01 -07004743 /* Enable Nagle algorithm by default. */
4744 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004746 sp->recvrcvinfo = 0;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004747 sp->recvnxtinfo = 0;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004748
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 /* Enable by default. */
4750 sp->v4mapped = 1;
4751
4752 /* Auto-close idle associations after the configured
4753 * number of seconds. A value of 0 disables this
4754 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4755 * for UDP-style sockets only.
4756 */
4757 sp->autoclose = 0;
4758
4759 /* User specified fragmentation limit. */
4760 sp->user_frag = 0;
4761
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004762 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
4764 sp->pf = sctp_get_pf_specific(sk->sk_family);
4765
4766 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004767 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004769 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
4771 /* Create a per socket endpoint structure. Even if we
4772 * change the data structure relationships, this may still
4773 * be useful for storing pre-connect address information.
4774 */
Daniel Borkmannc164b832013-06-14 18:24:06 +02004775 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4776 if (!sp->ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 return -ENOMEM;
4778
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 sp->hmac = NULL;
4780
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004781 sk->sk_destruct = sctp_destruct_sock;
4782
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08004784
4785 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004786 sk_sockets_allocated_inc(sk);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004787 sock_prot_inuse_add(net, sk->sk_prot, 1);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004788
4789 /* Nothing can fail after this block, otherwise
4790 * sctp_destroy_sock() will be called without addr_wq_lock held
4791 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004792 if (net->sctp.default_auto_asconf) {
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004793 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda9f7d6532011-04-26 19:32:51 +09004794 list_add_tail(&sp->auto_asconf_list,
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004795 &net->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09004796 sp->do_auto_asconf = 1;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004797 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4798 } else {
Michio Honda9f7d6532011-04-26 19:32:51 +09004799 sp->do_auto_asconf = 0;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004800 }
4801
David S. Miller6f756a82008-11-23 17:34:03 -08004802 local_bh_enable();
4803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 return 0;
4805}
4806
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004807/* Cleanup any SCTP per socket resources. Must be called with
4808 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4809 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004810static void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811{
Michio Honda9f7d6532011-04-26 19:32:51 +09004812 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
Daniel Borkmannbb333812013-06-28 19:49:40 +02004814 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
4816 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09004817 sp = sctp_sk(sk);
Daniel Borkmann1abd1652013-06-06 15:53:47 +02004818 /* This could happen during socket init, thus we bail out
4819 * early, since the rest of the below is not setup either.
4820 */
4821 if (sp->ep == NULL)
4822 return;
4823
Michio Honda9f7d6532011-04-26 19:32:51 +09004824 if (sp->do_auto_asconf) {
4825 sp->do_auto_asconf = 0;
4826 list_del(&sp->auto_asconf_list);
4827 }
4828 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004829 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004830 sk_sockets_allocated_dec(sk);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08004831 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004832 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833}
4834
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004835/* Triggered when there are no references on the socket anymore */
4836static void sctp_destruct_sock(struct sock *sk)
4837{
4838 struct sctp_sock *sp = sctp_sk(sk);
4839
4840 /* Free up the HMAC transform. */
Herbert Xu5821c762016-01-24 21:20:12 +08004841 crypto_free_shash(sp->hmac);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004842
4843 inet_sock_destruct(sk);
4844}
4845
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846/* API 4.1.7 shutdown() - TCP Style Syntax
4847 * int shutdown(int socket, int how);
4848 *
4849 * sd - the socket descriptor of the association to be closed.
4850 * how - Specifies the type of shutdown. The values are
4851 * as follows:
4852 * SHUT_RD
4853 * Disables further receive operations. No SCTP
4854 * protocol action is taken.
4855 * SHUT_WR
4856 * Disables further send operations, and initiates
4857 * the SCTP shutdown sequence.
4858 * SHUT_RDWR
4859 * Disables further send and receive operations
4860 * and initiates the SCTP shutdown sequence.
4861 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004862static void sctp_shutdown(struct sock *sk, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004864 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 struct sctp_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
4867 if (!sctp_style(sk, TCP))
4868 return;
4869
Xin Long5bf35dd2016-11-13 21:44:37 +08004870 ep = sctp_sk(sk)->ep;
4871 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4872 struct sctp_association *asoc;
4873
Yafang Shaocbabf462017-12-20 11:12:54 +08004874 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Xin Long5bf35dd2016-11-13 21:44:37 +08004875 asoc = list_entry(ep->asocs.next,
4876 struct sctp_association, asocs);
4877 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 }
4879}
4880
Xin Long52c52a62016-04-14 15:35:30 +08004881int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4882 struct sctp_info *info)
4883{
4884 struct sctp_transport *prim;
4885 struct list_head *pos;
4886 int mask;
4887
4888 memset(info, 0, sizeof(*info));
4889 if (!asoc) {
4890 struct sctp_sock *sp = sctp_sk(sk);
4891
4892 info->sctpi_s_autoclose = sp->autoclose;
4893 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4894 info->sctpi_s_pd_point = sp->pd_point;
4895 info->sctpi_s_nodelay = sp->nodelay;
4896 info->sctpi_s_disable_fragments = sp->disable_fragments;
4897 info->sctpi_s_v4mapped = sp->v4mapped;
4898 info->sctpi_s_frag_interleave = sp->frag_interleave;
Xin Long40eb90e92016-05-29 17:42:13 +08004899 info->sctpi_s_type = sp->type;
Xin Long52c52a62016-04-14 15:35:30 +08004900
4901 return 0;
4902 }
4903
4904 info->sctpi_tag = asoc->c.my_vtag;
4905 info->sctpi_state = asoc->state;
4906 info->sctpi_rwnd = asoc->a_rwnd;
4907 info->sctpi_unackdata = asoc->unack_data;
4908 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004909 info->sctpi_instrms = asoc->stream.incnt;
4910 info->sctpi_outstrms = asoc->stream.outcnt;
Xin Long52c52a62016-04-14 15:35:30 +08004911 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4912 info->sctpi_inqueue++;
4913 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4914 info->sctpi_outqueue++;
4915 info->sctpi_overall_error = asoc->overall_error_count;
4916 info->sctpi_max_burst = asoc->max_burst;
4917 info->sctpi_maxseg = asoc->frag_point;
4918 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4919 info->sctpi_peer_tag = asoc->c.peer_vtag;
4920
4921 mask = asoc->peer.ecn_capable << 1;
4922 mask = (mask | asoc->peer.ipv4_address) << 1;
4923 mask = (mask | asoc->peer.ipv6_address) << 1;
4924 mask = (mask | asoc->peer.hostname_address) << 1;
4925 mask = (mask | asoc->peer.asconf_capable) << 1;
4926 mask = (mask | asoc->peer.prsctp_capable) << 1;
4927 mask = (mask | asoc->peer.auth_capable);
4928 info->sctpi_peer_capable = mask;
4929 mask = asoc->peer.sack_needed << 1;
4930 mask = (mask | asoc->peer.sack_generation) << 1;
4931 mask = (mask | asoc->peer.zero_window_announced);
4932 info->sctpi_peer_sack = mask;
4933
4934 info->sctpi_isacks = asoc->stats.isacks;
4935 info->sctpi_osacks = asoc->stats.osacks;
4936 info->sctpi_opackets = asoc->stats.opackets;
4937 info->sctpi_ipackets = asoc->stats.ipackets;
4938 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4939 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4940 info->sctpi_idupchunks = asoc->stats.idupchunks;
4941 info->sctpi_gapcnt = asoc->stats.gapcnt;
4942 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4943 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4944 info->sctpi_oodchunks = asoc->stats.oodchunks;
4945 info->sctpi_iodchunks = asoc->stats.iodchunks;
4946 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4947 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4948
4949 prim = asoc->peer.primary_path;
Stefano Brivioee6c88b2017-08-23 13:27:13 +02004950 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
Xin Long52c52a62016-04-14 15:35:30 +08004951 info->sctpi_p_state = prim->state;
4952 info->sctpi_p_cwnd = prim->cwnd;
4953 info->sctpi_p_srtt = prim->srtt;
4954 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4955 info->sctpi_p_hbinterval = prim->hbinterval;
4956 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4957 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4958 info->sctpi_p_ssthresh = prim->ssthresh;
4959 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4960 info->sctpi_p_flight_size = prim->flight_size;
4961 info->sctpi_p_error = prim->error_count;
4962
4963 return 0;
4964}
4965EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4966
Xin Long626d16f2016-04-14 15:35:31 +08004967/* use callback to avoid exporting the core structure */
Tom Herbert97a6ec42017-12-04 10:31:41 -08004968void sctp_transport_walk_start(struct rhashtable_iter *iter)
Xin Long626d16f2016-04-14 15:35:31 +08004969{
Xin Long7fda702f2016-11-15 23:23:11 +08004970 rhltable_walk_enter(&sctp_transport_hashtable, iter);
Xin Long626d16f2016-04-14 15:35:31 +08004971
Tom Herbert97a6ec42017-12-04 10:31:41 -08004972 rhashtable_walk_start(iter);
Xin Long626d16f2016-04-14 15:35:31 +08004973}
4974
4975void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4976{
4977 rhashtable_walk_stop(iter);
4978 rhashtable_walk_exit(iter);
4979}
4980
4981struct sctp_transport *sctp_transport_get_next(struct net *net,
4982 struct rhashtable_iter *iter)
4983{
4984 struct sctp_transport *t;
4985
4986 t = rhashtable_walk_next(iter);
4987 for (; t; t = rhashtable_walk_next(iter)) {
4988 if (IS_ERR(t)) {
4989 if (PTR_ERR(t) == -EAGAIN)
4990 continue;
4991 break;
4992 }
4993
Xin Longbab1be72018-08-27 18:38:31 +08004994 if (!sctp_transport_hold(t))
4995 continue;
4996
Xin Long626d16f2016-04-14 15:35:31 +08004997 if (net_eq(sock_net(t->asoc->base.sk), net) &&
4998 t->asoc->peer.primary_path == t)
4999 break;
Xin Longbab1be72018-08-27 18:38:31 +08005000
5001 sctp_transport_put(t);
Xin Long626d16f2016-04-14 15:35:31 +08005002 }
5003
5004 return t;
5005}
5006
5007struct sctp_transport *sctp_transport_get_idx(struct net *net,
5008 struct rhashtable_iter *iter,
5009 int pos)
5010{
Xin Longbab1be72018-08-27 18:38:31 +08005011 struct sctp_transport *t;
Xin Long626d16f2016-04-14 15:35:31 +08005012
Xin Longbab1be72018-08-27 18:38:31 +08005013 if (!pos)
5014 return SEQ_START_TOKEN;
Xin Long626d16f2016-04-14 15:35:31 +08005015
Xin Longbab1be72018-08-27 18:38:31 +08005016 while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
5017 if (!--pos)
5018 break;
5019 sctp_transport_put(t);
5020 }
5021
5022 return t;
Xin Long626d16f2016-04-14 15:35:31 +08005023}
5024
5025int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
5026 void *p) {
5027 int err = 0;
5028 int hash = 0;
5029 struct sctp_ep_common *epb;
5030 struct sctp_hashbucket *head;
5031
5032 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
5033 hash++, head++) {
Xin Long581409d2017-06-10 14:48:14 +08005034 read_lock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08005035 sctp_for_each_hentry(epb, &head->chain) {
5036 err = cb(sctp_ep(epb), p);
5037 if (err)
5038 break;
5039 }
Xin Long581409d2017-06-10 14:48:14 +08005040 read_unlock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08005041 }
5042
5043 return err;
5044}
5045EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
5046
5047int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
5048 struct net *net,
5049 const union sctp_addr *laddr,
5050 const union sctp_addr *paddr, void *p)
5051{
5052 struct sctp_transport *transport;
Xin Long08abb792016-12-15 23:05:52 +08005053 int err;
Xin Long626d16f2016-04-14 15:35:31 +08005054
5055 rcu_read_lock();
5056 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
Xin Long626d16f2016-04-14 15:35:31 +08005057 rcu_read_unlock();
Xin Long08abb792016-12-15 23:05:52 +08005058 if (!transport)
5059 return -ENOENT;
5060
Xin Long1cceda782016-09-29 02:55:44 +08005061 err = cb(transport, p);
Xin Longcd26da42016-10-31 20:32:31 +08005062 sctp_transport_put(transport);
Xin Long1cceda782016-09-29 02:55:44 +08005063
Xin Long626d16f2016-04-14 15:35:31 +08005064 return err;
5065}
5066EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
5067
5068int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
Xin Longd25adbe2017-09-15 11:02:21 +08005069 int (*cb_done)(struct sctp_transport *, void *),
5070 struct net *net, int *pos, void *p) {
Xin Long626d16f2016-04-14 15:35:31 +08005071 struct rhashtable_iter hti;
Xin Longd25adbe2017-09-15 11:02:21 +08005072 struct sctp_transport *tsp;
5073 int ret;
Xin Long626d16f2016-04-14 15:35:31 +08005074
Xin Longd25adbe2017-09-15 11:02:21 +08005075again:
Xin Longf53d77e2018-01-23 18:22:25 +08005076 ret = 0;
Tom Herbert97a6ec42017-12-04 10:31:41 -08005077 sctp_transport_walk_start(&hti);
Xin Long626d16f2016-04-14 15:35:31 +08005078
Xin Longd25adbe2017-09-15 11:02:21 +08005079 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5080 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
Xin Longd25adbe2017-09-15 11:02:21 +08005081 ret = cb(tsp, p);
5082 if (ret)
Xin Long626d16f2016-04-14 15:35:31 +08005083 break;
Xin Longd25adbe2017-09-15 11:02:21 +08005084 (*pos)++;
5085 sctp_transport_put(tsp);
Xin Long626d16f2016-04-14 15:35:31 +08005086 }
Xin Long626d16f2016-04-14 15:35:31 +08005087 sctp_transport_walk_stop(&hti);
Xin Long53fa1032016-04-14 15:35:35 +08005088
Xin Longd25adbe2017-09-15 11:02:21 +08005089 if (ret) {
5090 if (cb_done && !cb_done(tsp, p)) {
5091 (*pos)++;
5092 sctp_transport_put(tsp);
5093 goto again;
5094 }
5095 sctp_transport_put(tsp);
5096 }
5097
5098 return ret;
Xin Long626d16f2016-04-14 15:35:31 +08005099}
5100EXPORT_SYMBOL_GPL(sctp_for_each_transport);
5101
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102/* 7.2.1 Association Status (SCTP_STATUS)
5103
5104 * Applications can retrieve current status information about an
5105 * association, including association state, peer receiver window size,
5106 * number of unacked data chunks, and number of data chunks pending
5107 * receipt. This information is read-only.
5108 */
5109static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5110 char __user *optval,
5111 int __user *optlen)
5112{
5113 struct sctp_status status;
5114 struct sctp_association *asoc = NULL;
5115 struct sctp_transport *transport;
5116 sctp_assoc_t associd;
5117 int retval = 0;
5118
Neil Horman408f22e2007-06-16 14:03:45 -04005119 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 retval = -EINVAL;
5121 goto out;
5122 }
5123
Neil Horman408f22e2007-06-16 14:03:45 -04005124 len = sizeof(status);
5125 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 retval = -EFAULT;
5127 goto out;
5128 }
5129
5130 associd = status.sstat_assoc_id;
5131 asoc = sctp_id2assoc(sk, associd);
5132 if (!asoc) {
5133 retval = -EINVAL;
5134 goto out;
5135 }
5136
5137 transport = asoc->peer.primary_path;
5138
5139 status.sstat_assoc_id = sctp_assoc2id(asoc);
Daniel Borkmann38ab1fa2014-08-28 15:28:26 +02005140 status.sstat_state = sctp_assoc_to_state(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 status.sstat_rwnd = asoc->peer.rwnd;
5142 status.sstat_unackdata = asoc->unack_data;
5143
5144 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08005145 status.sstat_instrms = asoc->stream.incnt;
5146 status.sstat_outstrms = asoc->stream.outcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 status.sstat_fragmentation_point = asoc->frag_point;
5148 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08005149 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5150 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 /* Map ipv4 address into v4-mapped-on-v6 address. */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005152 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005154 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 status.sstat_primary.spinfo_cwnd = transport->cwnd;
5156 status.sstat_primary.spinfo_srtt = transport->srtt;
5157 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005158 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159
Frank Filz3f7a87d2005-06-20 13:14:57 -07005160 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5161 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5162
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 if (put_user(len, optlen)) {
5164 retval = -EFAULT;
5165 goto out;
5166 }
5167
Daniel Borkmannbb333812013-06-28 19:49:40 +02005168 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5169 __func__, len, status.sstat_state, status.sstat_rwnd,
5170 status.sstat_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
5172 if (copy_to_user(optval, &status, len)) {
5173 retval = -EFAULT;
5174 goto out;
5175 }
5176
5177out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00005178 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179}
5180
5181
5182/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5183 *
5184 * Applications can retrieve information about a specific peer address
5185 * of an association, including its reachability state, congestion
5186 * window, and retransmission timer values. This information is
5187 * read-only.
5188 */
5189static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5190 char __user *optval,
5191 int __user *optlen)
5192{
5193 struct sctp_paddrinfo pinfo;
5194 struct sctp_transport *transport;
5195 int retval = 0;
5196
Neil Horman408f22e2007-06-16 14:03:45 -04005197 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 retval = -EINVAL;
5199 goto out;
5200 }
5201
Neil Horman408f22e2007-06-16 14:03:45 -04005202 len = sizeof(pinfo);
5203 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 retval = -EFAULT;
5205 goto out;
5206 }
5207
5208 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5209 pinfo.spinfo_assoc_id);
5210 if (!transport)
5211 return -EINVAL;
5212
5213 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005214 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 pinfo.spinfo_cwnd = transport->cwnd;
5216 pinfo.spinfo_srtt = transport->srtt;
5217 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005218 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219
Frank Filz3f7a87d2005-06-20 13:14:57 -07005220 if (pinfo.spinfo_state == SCTP_UNKNOWN)
5221 pinfo.spinfo_state = SCTP_ACTIVE;
5222
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 if (put_user(len, optlen)) {
5224 retval = -EFAULT;
5225 goto out;
5226 }
5227
5228 if (copy_to_user(optval, &pinfo, len)) {
5229 retval = -EFAULT;
5230 goto out;
5231 }
5232
5233out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00005234 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235}
5236
5237/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5238 *
5239 * This option is a on/off flag. If enabled no SCTP message
5240 * fragmentation will be performed. Instead if a message being sent
5241 * exceeds the current PMTU size, the message will NOT be sent and
5242 * instead a error will be indicated to the user.
5243 */
5244static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5245 char __user *optval, int __user *optlen)
5246{
5247 int val;
5248
5249 if (len < sizeof(int))
5250 return -EINVAL;
5251
5252 len = sizeof(int);
5253 val = (sctp_sk(sk)->disable_fragments == 1);
5254 if (put_user(len, optlen))
5255 return -EFAULT;
5256 if (copy_to_user(optval, &val, len))
5257 return -EFAULT;
5258 return 0;
5259}
5260
5261/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5262 *
5263 * This socket option is used to specify various notifications and
5264 * ancillary data the user wishes to receive.
5265 */
5266static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5267 int __user *optlen)
5268{
Jiri Slabya4b8e712016-10-21 14:13:24 +02005269 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00005271 if (len > sizeof(struct sctp_event_subscribe))
5272 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04005273 if (put_user(len, optlen))
5274 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
5276 return -EFAULT;
5277 return 0;
5278}
5279
5280/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5281 *
5282 * This socket option is applicable to the UDP-style socket only. When
5283 * set it will cause associations that are idle for more than the
5284 * specified number of seconds to automatically close. An association
5285 * being idle is defined an association that has NOT sent or received
5286 * user data. The special value of '0' indicates that no automatic
5287 * close of any associations should be performed. The option expects an
5288 * integer defining the number of seconds of idle time before an
5289 * association is closed.
5290 */
5291static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5292{
5293 /* Applicable to UDP-style socket only */
5294 if (sctp_style(sk, TCP))
5295 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04005296 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005298 len = sizeof(int);
5299 if (put_user(len, optlen))
5300 return -EFAULT;
David Windsorb2ce04c2017-06-10 22:50:43 -04005301 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 return -EFAULT;
5303 return 0;
5304}
5305
5306/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00005307int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308{
Benjamin Poirier0343c552012-03-08 05:55:58 +00005309 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005310 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 struct socket *sock;
5312 int err = 0;
5313
Xin Longdf80cd92017-10-17 23:26:10 +08005314 /* Do not peel off from one netns to another one. */
5315 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5316 return -EINVAL;
5317
Benjamin Poirier0343c552012-03-08 05:55:58 +00005318 if (!asoc)
5319 return -EINVAL;
5320
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 /* An association cannot be branched off from an already peeled-off
5322 * socket, nor is this supported for tcp style sockets.
5323 */
5324 if (!sctp_style(sk, UDP))
5325 return -EINVAL;
5326
5327 /* Create a new socket. */
5328 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5329 if (err < 0)
5330 return err;
5331
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005332 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005333
5334 /* Make peeled-off sockets more like 1-1 accepted sockets.
Richard Hainesb7e10c22018-02-24 16:18:51 +00005335 * Set the daddr and initialize id to something more random and also
5336 * copy over any ip options.
Vlad Yasevich4f444302006-10-30 18:54:32 -08005337 */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005338 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
Richard Hainesb7e10c22018-02-24 16:18:51 +00005339 sp->pf->copy_ip_options(sk, sock->sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005340
5341 /* Populate the fields of the newsk from the oldsk and migrate the
5342 * asoc to the newsk.
5343 */
5344 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005345
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 *sockp = sock;
5347
5348 return err;
5349}
Benjamin Poirier0343c552012-03-08 05:55:58 +00005350EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005352static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5353 struct file **newfile, unsigned flags)
5354{
5355 struct socket *newsock;
5356 int retval;
5357
5358 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5359 if (retval < 0)
5360 goto out;
5361
5362 /* Map the socket to an unused fd that can be returned to the user. */
5363 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5364 if (retval < 0) {
5365 sock_release(newsock);
5366 goto out;
5367 }
5368
5369 *newfile = sock_alloc_file(newsock, 0, NULL);
5370 if (IS_ERR(*newfile)) {
5371 put_unused_fd(retval);
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005372 retval = PTR_ERR(*newfile);
5373 *newfile = NULL;
5374 return retval;
5375 }
5376
5377 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5378 retval);
5379
5380 peeloff->sd = retval;
5381
5382 if (flags & SOCK_NONBLOCK)
5383 (*newfile)->f_flags |= O_NONBLOCK;
5384out:
5385 return retval;
5386}
5387
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5389{
5390 sctp_peeloff_arg_t peeloff;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005391 struct file *newfile = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393
Neil Horman408f22e2007-06-16 14:03:45 -04005394 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005396 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 if (copy_from_user(&peeloff, optval, len))
5398 return -EFAULT;
5399
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005400 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 if (retval < 0)
5402 goto out;
5403
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04005405 if (put_user(len, optlen)) {
5406 fput(newfile);
5407 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04005408 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04005409 }
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005410
5411 if (copy_to_user(optval, &peeloff, len)) {
5412 fput(newfile);
5413 put_unused_fd(retval);
5414 return -EFAULT;
5415 }
5416 fd_install(retval, newfile);
5417out:
5418 return retval;
5419}
5420
5421static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5422 char __user *optval, int __user *optlen)
5423{
5424 sctp_peeloff_flags_arg_t peeloff;
5425 struct file *newfile = NULL;
5426 int retval = 0;
5427
5428 if (len < sizeof(sctp_peeloff_flags_arg_t))
5429 return -EINVAL;
5430 len = sizeof(sctp_peeloff_flags_arg_t);
5431 if (copy_from_user(&peeloff, optval, len))
5432 return -EFAULT;
5433
5434 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5435 &newfile, peeloff.flags);
5436 if (retval < 0)
5437 goto out;
5438
5439 /* Return the fd mapped to the new socket. */
5440 if (put_user(len, optlen)) {
5441 fput(newfile);
5442 put_unused_fd(retval);
5443 return -EFAULT;
5444 }
5445
Al Viro56b31d12012-08-18 00:25:51 -04005446 if (copy_to_user(optval, &peeloff, len)) {
5447 fput(newfile);
5448 put_unused_fd(retval);
5449 return -EFAULT;
5450 }
5451 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452out:
5453 return retval;
5454}
5455
5456/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5457 *
5458 * Applications can enable or disable heartbeats for any peer address of
5459 * an association, modify an address's heartbeat interval, force a
5460 * heartbeat to be sent immediately, and adjust the address's maximum
5461 * number of retransmissions sent before an address is considered
5462 * unreachable. The following structure is used to access and modify an
5463 * address's parameters:
5464 *
5465 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005466 * sctp_assoc_t spp_assoc_id;
5467 * struct sockaddr_storage spp_address;
5468 * uint32_t spp_hbinterval;
5469 * uint16_t spp_pathmaxrxt;
5470 * uint32_t spp_pathmtu;
5471 * uint32_t spp_sackdelay;
5472 * uint32_t spp_flags;
5473 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08005475 * spp_assoc_id - (one-to-many style socket) This is filled in the
5476 * application, and identifies the association for
5477 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 * spp_address - This specifies which address is of interest.
5479 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005480 * in milliseconds. If a value of zero
5481 * is present in this field then no changes are to
5482 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 * spp_pathmaxrxt - This contains the maximum number of
5484 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08005485 * considered unreachable. If a value of zero
5486 * is present in this field then no changes are to
5487 * be made to this parameter.
5488 * spp_pathmtu - When Path MTU discovery is disabled the value
5489 * specified here will be the "fixed" path mtu.
5490 * Note that if the spp_address field is empty
5491 * then all associations on this address will
5492 * have this fixed path mtu set upon them.
5493 *
5494 * spp_sackdelay - When delayed sack is enabled, this value specifies
5495 * the number of milliseconds that sacks will be delayed
5496 * for. This value will apply to all addresses of an
5497 * association if the spp_address field is empty. Note
5498 * also, that if delayed sack is enabled and this
5499 * value is set to 0, no change is made to the last
5500 * recorded delayed sack timer value.
5501 *
5502 * spp_flags - These flags are used to control various features
5503 * on an association. The flag field may contain
5504 * zero or more of the following options.
5505 *
5506 * SPP_HB_ENABLE - Enable heartbeats on the
5507 * specified address. Note that if the address
5508 * field is empty all addresses for the association
5509 * have heartbeats enabled upon them.
5510 *
5511 * SPP_HB_DISABLE - Disable heartbeats on the
5512 * speicifed address. Note that if the address
5513 * field is empty all addresses for the association
5514 * will have their heartbeats disabled. Note also
5515 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5516 * mutually exclusive, only one of these two should
5517 * be specified. Enabling both fields will have
5518 * undetermined results.
5519 *
5520 * SPP_HB_DEMAND - Request a user initiated heartbeat
5521 * to be made immediately.
5522 *
5523 * SPP_PMTUD_ENABLE - This field will enable PMTU
5524 * discovery upon the specified address. Note that
5525 * if the address feild is empty then all addresses
5526 * on the association are effected.
5527 *
5528 * SPP_PMTUD_DISABLE - This field will disable PMTU
5529 * discovery upon the specified address. Note that
5530 * if the address feild is empty then all addresses
5531 * on the association are effected. Not also that
5532 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5533 * exclusive. Enabling both will have undetermined
5534 * results.
5535 *
5536 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5537 * on delayed sack. The time specified in spp_sackdelay
5538 * is used to specify the sack delay for this address. Note
5539 * that if spp_address is empty then all addresses will
5540 * enable delayed sack and take on the sack delay
5541 * value specified in spp_sackdelay.
5542 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5543 * off delayed sack. If the spp_address field is blank then
5544 * delayed sack is disabled for the entire association. Note
5545 * also that this field is mutually exclusive to
5546 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5547 * results.
Xin Long0b0dce72018-07-02 18:21:13 +08005548 *
5549 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
5550 * setting of the IPV6 flow label value. The value is
5551 * contained in the spp_ipv6_flowlabel field.
5552 * Upon retrieval, this flag will be set to indicate that
5553 * the spp_ipv6_flowlabel field has a valid value returned.
5554 * If a specific destination address is set (in the
5555 * spp_address field), then the value returned is that of
5556 * the address. If just an association is specified (and
5557 * no address), then the association's default flow label
5558 * is returned. If neither an association nor a destination
5559 * is specified, then the socket's default flow label is
5560 * returned. For non-IPv6 sockets, this flag will be left
5561 * cleared.
5562 *
5563 * SPP_DSCP: Setting this flag enables the setting of the
5564 * Differentiated Services Code Point (DSCP) value
5565 * associated with either the association or a specific
5566 * address. The value is obtained in the spp_dscp field.
5567 * Upon retrieval, this flag will be set to indicate that
5568 * the spp_dscp field has a valid value returned. If a
5569 * specific destination address is set when called (in the
5570 * spp_address field), then that specific destination
5571 * address's DSCP value is returned. If just an association
5572 * is specified, then the association's default DSCP is
5573 * returned. If neither an association nor a destination is
5574 * specified, then the socket's default DSCP is returned.
5575 *
5576 * spp_ipv6_flowlabel
5577 * - This field is used in conjunction with the
5578 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
5579 * The 20 least significant bits are used for the flow
5580 * label. This setting has precedence over any IPv6-layer
5581 * setting.
5582 *
5583 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
5584 * and contains the DSCP. The 6 most significant bits are
5585 * used for the DSCP. This setting has precedence over any
5586 * IPv4- or IPv6- layer setting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 */
5588static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005589 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590{
Frank Filz52ccb8e2005-12-22 11:36:46 -08005591 struct sctp_paddrparams params;
5592 struct sctp_transport *trans = NULL;
5593 struct sctp_association *asoc = NULL;
5594 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
Xin Long0b0dce72018-07-02 18:21:13 +08005596 if (len >= sizeof(params))
5597 len = sizeof(params);
5598 else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
5599 spp_ipv6_flowlabel), 4))
5600 len = ALIGN(offsetof(struct sctp_paddrparams,
5601 spp_ipv6_flowlabel), 4);
5602 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 return -EINVAL;
Xin Long0b0dce72018-07-02 18:21:13 +08005604
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 if (copy_from_user(&params, optval, len))
5606 return -EFAULT;
5607
Frank Filz52ccb8e2005-12-22 11:36:46 -08005608 /* If an address other than INADDR_ANY is specified, and
5609 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 */
wangweidongcb3f8372013-12-23 12:16:50 +08005611 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005612 trans = sctp_addr_id2transport(sk, &params.spp_address,
5613 params.spp_assoc_id);
5614 if (!trans) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005615 pr_debug("%s: failed no transport\n", __func__);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005616 return -EINVAL;
5617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 }
5619
Frank Filz52ccb8e2005-12-22 11:36:46 -08005620 /* Get association, if assoc_id != 0 and the socket is a one
5621 * to many style socket, and an association was not found, then
5622 * the id was invalid.
5623 */
5624 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5625 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005626 pr_debug("%s: failed no association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08005628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
Frank Filz52ccb8e2005-12-22 11:36:46 -08005630 if (trans) {
5631 /* Fetch transport values. */
5632 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5633 params.spp_pathmtu = trans->pathmtu;
5634 params.spp_pathmaxrxt = trans->pathmaxrxt;
5635 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636
Frank Filz52ccb8e2005-12-22 11:36:46 -08005637 /*draft-11 doesn't say what to return in spp_flags*/
5638 params.spp_flags = trans->param_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08005639 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5640 params.spp_ipv6_flowlabel = trans->flowlabel &
5641 SCTP_FLOWLABEL_VAL_MASK;
5642 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5643 }
5644 if (trans->dscp & SCTP_DSCP_SET_MASK) {
5645 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
5646 params.spp_flags |= SPP_DSCP;
5647 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08005648 } else if (asoc) {
5649 /* Fetch association values. */
5650 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5651 params.spp_pathmtu = asoc->pathmtu;
5652 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5653 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654
Frank Filz52ccb8e2005-12-22 11:36:46 -08005655 /*draft-11 doesn't say what to return in spp_flags*/
5656 params.spp_flags = asoc->param_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08005657 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5658 params.spp_ipv6_flowlabel = asoc->flowlabel &
5659 SCTP_FLOWLABEL_VAL_MASK;
5660 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5661 }
5662 if (asoc->dscp & SCTP_DSCP_SET_MASK) {
5663 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
5664 params.spp_flags |= SPP_DSCP;
5665 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08005666 } else {
5667 /* Fetch socket values. */
5668 params.spp_hbinterval = sp->hbinterval;
5669 params.spp_pathmtu = sp->pathmtu;
5670 params.spp_sackdelay = sp->sackdelay;
5671 params.spp_pathmaxrxt = sp->pathmaxrxt;
5672
5673 /*draft-11 doesn't say what to return in spp_flags*/
5674 params.spp_flags = sp->param_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08005675 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5676 params.spp_ipv6_flowlabel = sp->flowlabel &
5677 SCTP_FLOWLABEL_VAL_MASK;
5678 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5679 }
5680 if (sp->dscp & SCTP_DSCP_SET_MASK) {
5681 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
5682 params.spp_flags |= SPP_DSCP;
5683 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08005684 }
5685
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 if (copy_to_user(optval, &params, len))
5687 return -EFAULT;
5688
5689 if (put_user(len, optlen))
5690 return -EFAULT;
5691
5692 return 0;
5693}
5694
Wei Yongjund364d922008-05-09 15:13:26 -07005695/*
5696 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08005697 *
Wei Yongjund364d922008-05-09 15:13:26 -07005698 * This option will effect the way delayed acks are performed. This
5699 * option allows you to get or set the delayed ack time, in
5700 * milliseconds. It also allows changing the delayed ack frequency.
5701 * Changing the frequency to 1 disables the delayed sack algorithm. If
5702 * the assoc_id is 0, then this sets or gets the endpoints default
5703 * values. If the assoc_id field is non-zero, then the set or get
5704 * effects the specified association for the one to many model (the
5705 * assoc_id field is ignored by the one to one model). Note that if
5706 * sack_delay or sack_freq are 0 when setting this option, then the
5707 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08005708 *
Wei Yongjund364d922008-05-09 15:13:26 -07005709 * struct sctp_sack_info {
5710 * sctp_assoc_t sack_assoc_id;
5711 * uint32_t sack_delay;
5712 * uint32_t sack_freq;
5713 * };
Frank Filz77086102005-12-22 11:37:30 -08005714 *
Wei Yongjund364d922008-05-09 15:13:26 -07005715 * sack_assoc_id - This parameter, indicates which association the user
5716 * is performing an action upon. Note that if this field's value is
5717 * zero then the endpoints default value is changed (effecting future
5718 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08005719 *
Wei Yongjund364d922008-05-09 15:13:26 -07005720 * sack_delay - This parameter contains the number of milliseconds that
5721 * the user is requesting the delayed ACK timer be set to. Note that
5722 * this value is defined in the standard to be between 200 and 500
5723 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08005724 *
Wei Yongjund364d922008-05-09 15:13:26 -07005725 * sack_freq - This parameter contains the number of packets that must
5726 * be received before a sack is sent without waiting for the delay
5727 * timer to expire. The default value for this is 2, setting this
5728 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08005729 */
Wei Yongjund364d922008-05-09 15:13:26 -07005730static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08005731 char __user *optval,
5732 int __user *optlen)
5733{
Wei Yongjund364d922008-05-09 15:13:26 -07005734 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08005735 struct sctp_association *asoc = NULL;
5736 struct sctp_sock *sp = sctp_sk(sk);
5737
Wei Yongjund364d922008-05-09 15:13:26 -07005738 if (len >= sizeof(struct sctp_sack_info)) {
5739 len = sizeof(struct sctp_sack_info);
5740
5741 if (copy_from_user(&params, optval, len))
5742 return -EFAULT;
5743 } else if (len == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05005744 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005745 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005746 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005747 "Use struct sctp_sack_info instead\n",
5748 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07005749 if (copy_from_user(&params, optval, len))
5750 return -EFAULT;
5751 } else
wangweidongcb3f8372013-12-23 12:16:50 +08005752 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08005753
Wei Yongjund364d922008-05-09 15:13:26 -07005754 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08005755 * to many style socket, and an association was not found, then
5756 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005757 */
Wei Yongjund364d922008-05-09 15:13:26 -07005758 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5759 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08005760 return -EINVAL;
5761
5762 if (asoc) {
5763 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005764 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5765 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08005766 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07005767 params.sack_freq = asoc->sackfreq;
5768
5769 } else {
5770 params.sack_delay = 0;
5771 params.sack_freq = 1;
5772 }
Frank Filz77086102005-12-22 11:37:30 -08005773 } else {
5774 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005775 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5776 params.sack_delay = sp->sackdelay;
5777 params.sack_freq = sp->sackfreq;
5778 } else {
5779 params.sack_delay = 0;
5780 params.sack_freq = 1;
5781 }
Frank Filz77086102005-12-22 11:37:30 -08005782 }
5783
5784 if (copy_to_user(optval, &params, len))
5785 return -EFAULT;
5786
5787 if (put_user(len, optlen))
5788 return -EFAULT;
5789
5790 return 0;
5791}
5792
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5794 *
5795 * Applications can specify protocol parameters for the default association
5796 * initialization. The option name argument to setsockopt() and getsockopt()
5797 * is SCTP_INITMSG.
5798 *
5799 * Setting initialization parameters is effective only on an unconnected
5800 * socket (for UDP-style sockets only future associations are effected
5801 * by the change). With TCP-style sockets, this option is inherited by
5802 * sockets derived from a listener socket.
5803 */
5804static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5805{
Neil Horman408f22e2007-06-16 14:03:45 -04005806 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005808 len = sizeof(struct sctp_initmsg);
5809 if (put_user(len, optlen))
5810 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5812 return -EFAULT;
5813 return 0;
5814}
5815
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005817static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5818 char __user *optval, int __user *optlen)
5819{
5820 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005821 int cnt = 0;
5822 struct sctp_getaddrs getaddrs;
5823 struct sctp_transport *from;
5824 void __user *to;
5825 union sctp_addr temp;
5826 struct sctp_sock *sp = sctp_sk(sk);
5827 int addrlen;
5828 size_t space_left;
5829 int bytes_copied;
5830
5831 if (len < sizeof(struct sctp_getaddrs))
5832 return -EINVAL;
5833
5834 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5835 return -EFAULT;
5836
5837 /* For UDP-style sockets, id specifies the association to query. */
5838 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5839 if (!asoc)
5840 return -EINVAL;
5841
wangweidongcb3f8372013-12-23 12:16:50 +08005842 to = optval + offsetof(struct sctp_getaddrs, addrs);
5843 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005844
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07005845 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5846 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08005847 memcpy(&temp, &from->ipaddr, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005848 addrlen = sctp_get_pf_specific(sk->sk_family)
5849 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005850 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005851 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005852 if (copy_to_user(to, &temp, addrlen))
5853 return -EFAULT;
5854 to += addrlen;
5855 cnt++;
5856 space_left -= addrlen;
5857 }
5858
5859 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5860 return -EFAULT;
5861 bytes_copied = ((char __user *)to) - optval;
5862 if (put_user(bytes_copied, optlen))
5863 return -EFAULT;
5864
5865 return 0;
5866}
5867
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005868static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5869 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005870{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005871 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005872 union sctp_addr temp;
5873 int cnt = 0;
5874 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005875 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005876
Vlad Yasevich29303542007-09-16 16:02:12 -07005877 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005878 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07005879 if (!addr->valid)
5880 continue;
5881
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005882 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08005883 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005884 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005885 if ((PF_INET6 == sk->sk_family) &&
5886 inet_v6_ipv6only(sk) &&
5887 (AF_INET == addr->a.sa.sa_family))
5888 continue;
Al Viro6244be42006-11-20 17:21:44 -08005889 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05005890 if (!temp.v4.sin_port)
5891 temp.v4.sin_port = htons(port);
5892
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005893 addrlen = sctp_get_pf_specific(sk->sk_family)
5894 ->addr_to_user(sctp_sk(sk), &temp);
5895
Vlad Yasevich29303542007-09-16 16:02:12 -07005896 if (space_left < addrlen) {
5897 cnt = -ENOMEM;
5898 break;
5899 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005900 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08005901
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005902 to += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005903 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005904 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04005905 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005906 }
Vlad Yasevich29303542007-09-16 16:02:12 -07005907 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005908
5909 return cnt;
5910}
5911
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005913static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5914 char __user *optval, int __user *optlen)
5915{
5916 struct sctp_bind_addr *bp;
5917 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005918 int cnt = 0;
5919 struct sctp_getaddrs getaddrs;
5920 struct sctp_sockaddr_entry *addr;
5921 void __user *to;
5922 union sctp_addr temp;
5923 struct sctp_sock *sp = sctp_sk(sk);
5924 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005925 int err = 0;
5926 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005927 int bytes_copied = 0;
5928 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005929 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005930
Neil Horman408f22e2007-06-16 14:03:45 -04005931 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005932 return -EINVAL;
5933
5934 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5935 return -EFAULT;
5936
5937 /*
5938 * For UDP-style sockets, id specifies the association to query.
5939 * If the id field is set to the value '0' then the locally bound
5940 * addresses are returned without regard to any particular
5941 * association.
5942 */
5943 if (0 == getaddrs.assoc_id) {
5944 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005945 } else {
5946 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5947 if (!asoc)
5948 return -EINVAL;
5949 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005950 }
5951
wangweidongcb3f8372013-12-23 12:16:50 +08005952 to = optval + offsetof(struct sctp_getaddrs, addrs);
5953 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Neil Horman186e2342007-06-18 19:59:16 -04005954
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02005955 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005956 if (!addrs)
5957 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005958
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005959 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5960 * addresses from the global local address list.
5961 */
5962 if (sctp_list_single_entry(&bp->address_list)) {
5963 addr = list_entry(bp->address_list.next,
5964 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04005965 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005966 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5967 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005968 if (cnt < 0) {
5969 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005970 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005971 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005972 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005973 }
5974 }
5975
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005976 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005977 /* Protection on the bound address list is not needed since
5978 * in the socket option context we hold a socket lock and
5979 * thus the bound address list can't change.
5980 */
5981 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08005982 memcpy(&temp, &addr->a, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005983 addrlen = sctp_get_pf_specific(sk->sk_family)
5984 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005985 if (space_left < addrlen) {
5986 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07005987 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005988 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005989 memcpy(buf, &temp, addrlen);
5990 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005991 bytes_copied += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005992 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005993 space_left -= addrlen;
5994 }
5995
5996copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005997 if (copy_to_user(to, addrs, bytes_copied)) {
5998 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005999 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006000 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04006001 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
6002 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02006003 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04006004 }
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006005 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
6006 * but we can't change it anymore.
6007 */
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006008 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04006009 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02006010out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006011 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006012 return err;
6013}
6014
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
6016 *
6017 * Requests that the local SCTP stack use the enclosed peer address as
6018 * the association primary. The enclosed address must be one of the
6019 * association peer's addresses.
6020 */
6021static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
6022 char __user *optval, int __user *optlen)
6023{
6024 struct sctp_prim prim;
6025 struct sctp_association *asoc;
6026 struct sctp_sock *sp = sctp_sk(sk);
6027
Neil Horman408f22e2007-06-16 14:03:45 -04006028 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 return -EINVAL;
6030
Neil Horman408f22e2007-06-16 14:03:45 -04006031 len = sizeof(struct sctp_prim);
6032
6033 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 return -EFAULT;
6035
6036 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
6037 if (!asoc)
6038 return -EINVAL;
6039
6040 if (!asoc->peer.primary_path)
6041 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006042
Al Viro8cec6b82006-11-20 17:23:01 -08006043 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
6044 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045
Jason Gunthorpe299ee122014-07-30 12:40:53 -06006046 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 (union sctp_addr *)&prim.ssp_addr);
6048
Neil Horman408f22e2007-06-16 14:03:45 -04006049 if (put_user(len, optlen))
6050 return -EFAULT;
6051 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 return -EFAULT;
6053
6054 return 0;
6055}
6056
6057/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006058 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006060 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 * Indication parameter for all future INIT and INIT-ACK exchanges.
6062 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006063static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 char __user *optval, int __user *optlen)
6065{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006066 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
Neil Horman408f22e2007-06-16 14:03:45 -04006068 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 return -EINVAL;
6070
Neil Horman408f22e2007-06-16 14:03:45 -04006071 len = sizeof(struct sctp_setadaptation);
6072
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006073 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04006074
6075 if (put_user(len, optlen))
6076 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006077 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07006079
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 return 0;
6081}
6082
6083/*
6084 *
6085 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
6086 *
6087 * Applications that wish to use the sendto() system call may wish to
6088 * specify a default set of parameters that would normally be supplied
6089 * through the inclusion of ancillary data. This socket option allows
6090 * such an application to set the default sctp_sndrcvinfo structure.
6091
6092
6093 * The application that wishes to use this socket option simply passes
6094 * in to this call the sctp_sndrcvinfo structure defined in Section
6095 * 5.2.2) The input parameters accepted by this call include
6096 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
6097 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
6098 * to this call if the caller is using the UDP model.
6099 *
6100 * For getsockopt, it get the default sctp_sndrcvinfo structure.
6101 */
6102static int sctp_getsockopt_default_send_param(struct sock *sk,
6103 int len, char __user *optval,
6104 int __user *optlen)
6105{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006107 struct sctp_association *asoc;
6108 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006110 if (len < sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04006112
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006113 len = sizeof(info);
Neil Horman408f22e2007-06-16 14:03:45 -04006114
6115 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 return -EFAULT;
6117
6118 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
6119 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
6120 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 if (asoc) {
6122 info.sinfo_stream = asoc->default_stream;
6123 info.sinfo_flags = asoc->default_flags;
6124 info.sinfo_ppid = asoc->default_ppid;
6125 info.sinfo_context = asoc->default_context;
6126 info.sinfo_timetolive = asoc->default_timetolive;
6127 } else {
6128 info.sinfo_stream = sp->default_stream;
6129 info.sinfo_flags = sp->default_flags;
6130 info.sinfo_ppid = sp->default_ppid;
6131 info.sinfo_context = sp->default_context;
6132 info.sinfo_timetolive = sp->default_timetolive;
6133 }
6134
Neil Horman408f22e2007-06-16 14:03:45 -04006135 if (put_user(len, optlen))
6136 return -EFAULT;
6137 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 return -EFAULT;
6139
6140 return 0;
6141}
6142
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006143/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
6144 * (SCTP_DEFAULT_SNDINFO)
6145 */
6146static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
6147 char __user *optval,
6148 int __user *optlen)
6149{
6150 struct sctp_sock *sp = sctp_sk(sk);
6151 struct sctp_association *asoc;
6152 struct sctp_sndinfo info;
6153
6154 if (len < sizeof(info))
6155 return -EINVAL;
6156
6157 len = sizeof(info);
6158
6159 if (copy_from_user(&info, optval, len))
6160 return -EFAULT;
6161
6162 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
6163 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
6164 return -EINVAL;
6165 if (asoc) {
6166 info.snd_sid = asoc->default_stream;
6167 info.snd_flags = asoc->default_flags;
6168 info.snd_ppid = asoc->default_ppid;
6169 info.snd_context = asoc->default_context;
6170 } else {
6171 info.snd_sid = sp->default_stream;
6172 info.snd_flags = sp->default_flags;
6173 info.snd_ppid = sp->default_ppid;
6174 info.snd_context = sp->default_context;
6175 }
6176
6177 if (put_user(len, optlen))
6178 return -EFAULT;
6179 if (copy_to_user(optval, &info, len))
6180 return -EFAULT;
6181
6182 return 0;
6183}
6184
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185/*
6186 *
6187 * 7.1.5 SCTP_NODELAY
6188 *
6189 * Turn on/off any Nagle-like algorithm. This means that packets are
6190 * generally sent as soon as possible and no unnecessary delays are
6191 * introduced, at the cost of more packets in the network. Expects an
6192 * integer boolean flag.
6193 */
6194
6195static int sctp_getsockopt_nodelay(struct sock *sk, int len,
6196 char __user *optval, int __user *optlen)
6197{
6198 int val;
6199
6200 if (len < sizeof(int))
6201 return -EINVAL;
6202
6203 len = sizeof(int);
6204 val = (sctp_sk(sk)->nodelay == 1);
6205 if (put_user(len, optlen))
6206 return -EFAULT;
6207 if (copy_to_user(optval, &val, len))
6208 return -EFAULT;
6209 return 0;
6210}
6211
6212/*
6213 *
6214 * 7.1.1 SCTP_RTOINFO
6215 *
6216 * The protocol parameters used to initialize and bound retransmission
6217 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
6218 * and modify these parameters.
6219 * All parameters are time values, in milliseconds. A value of 0, when
6220 * modifying the parameters, indicates that the current value should not
6221 * be changed.
6222 *
6223 */
6224static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6225 char __user *optval,
6226 int __user *optlen) {
6227 struct sctp_rtoinfo rtoinfo;
6228 struct sctp_association *asoc;
6229
Neil Horman408f22e2007-06-16 14:03:45 -04006230 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 return -EINVAL;
6232
Neil Horman408f22e2007-06-16 14:03:45 -04006233 len = sizeof(struct sctp_rtoinfo);
6234
6235 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 return -EFAULT;
6237
6238 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6239
6240 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
6241 return -EINVAL;
6242
6243 /* Values corresponding to the specific association. */
6244 if (asoc) {
6245 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6246 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6247 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6248 } else {
6249 /* Values corresponding to the endpoint. */
6250 struct sctp_sock *sp = sctp_sk(sk);
6251
6252 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6253 rtoinfo.srto_max = sp->rtoinfo.srto_max;
6254 rtoinfo.srto_min = sp->rtoinfo.srto_min;
6255 }
6256
6257 if (put_user(len, optlen))
6258 return -EFAULT;
6259
6260 if (copy_to_user(optval, &rtoinfo, len))
6261 return -EFAULT;
6262
6263 return 0;
6264}
6265
6266/*
6267 *
6268 * 7.1.2 SCTP_ASSOCINFO
6269 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02006270 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 * of the association.
6272 * Returns an error if the new association retransmission value is
6273 * greater than the sum of the retransmission value of the peer.
6274 * See [SCTP] for more information.
6275 *
6276 */
6277static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6278 char __user *optval,
6279 int __user *optlen)
6280{
6281
6282 struct sctp_assocparams assocparams;
6283 struct sctp_association *asoc;
6284 struct list_head *pos;
6285 int cnt = 0;
6286
Neil Horman408f22e2007-06-16 14:03:45 -04006287 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 return -EINVAL;
6289
Neil Horman408f22e2007-06-16 14:03:45 -04006290 len = sizeof(struct sctp_assocparams);
6291
6292 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 return -EFAULT;
6294
6295 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6296
6297 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
6298 return -EINVAL;
6299
6300 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07006301 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6303 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6304 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
Daniel Borkmann52db8822013-06-25 18:17:27 +02006305 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306
6307 list_for_each(pos, &asoc->peer.transport_addr_list) {
wangweidongcb3f8372013-12-23 12:16:50 +08006308 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 }
6310
6311 assocparams.sasoc_number_peer_destinations = cnt;
6312 } else {
6313 /* Values corresponding to the endpoint */
6314 struct sctp_sock *sp = sctp_sk(sk);
6315
6316 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6317 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6318 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6319 assocparams.sasoc_cookie_life =
6320 sp->assocparams.sasoc_cookie_life;
6321 assocparams.sasoc_number_peer_destinations =
6322 sp->assocparams.
6323 sasoc_number_peer_destinations;
6324 }
6325
6326 if (put_user(len, optlen))
6327 return -EFAULT;
6328
6329 if (copy_to_user(optval, &assocparams, len))
6330 return -EFAULT;
6331
6332 return 0;
6333}
6334
6335/*
6336 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6337 *
6338 * This socket option is a boolean flag which turns on or off mapped V4
6339 * addresses. If this option is turned on and the socket is type
6340 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6341 * If this option is turned off, then no mapping will be done of V4
6342 * addresses and a user will receive both PF_INET6 and PF_INET type
6343 * addresses on the socket.
6344 */
6345static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6346 char __user *optval, int __user *optlen)
6347{
6348 int val;
6349 struct sctp_sock *sp = sctp_sk(sk);
6350
6351 if (len < sizeof(int))
6352 return -EINVAL;
6353
6354 len = sizeof(int);
6355 val = sp->v4mapped;
6356 if (put_user(len, optlen))
6357 return -EFAULT;
6358 if (copy_to_user(optval, &val, len))
6359 return -EFAULT;
6360
6361 return 0;
6362}
6363
6364/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006365 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6366 * (chapter and verse is quoted at sctp_setsockopt_context())
6367 */
6368static int sctp_getsockopt_context(struct sock *sk, int len,
6369 char __user *optval, int __user *optlen)
6370{
6371 struct sctp_assoc_value params;
6372 struct sctp_sock *sp;
6373 struct sctp_association *asoc;
6374
Neil Horman408f22e2007-06-16 14:03:45 -04006375 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006376 return -EINVAL;
6377
Neil Horman408f22e2007-06-16 14:03:45 -04006378 len = sizeof(struct sctp_assoc_value);
6379
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006380 if (copy_from_user(&params, optval, len))
6381 return -EFAULT;
6382
6383 sp = sctp_sk(sk);
6384
6385 if (params.assoc_id != 0) {
6386 asoc = sctp_id2assoc(sk, params.assoc_id);
6387 if (!asoc)
6388 return -EINVAL;
6389 params.assoc_value = asoc->default_rcv_context;
6390 } else {
6391 params.assoc_value = sp->default_rcv_context;
6392 }
6393
6394 if (put_user(len, optlen))
6395 return -EFAULT;
6396 if (copy_to_user(optval, &params, len))
6397 return -EFAULT;
6398
6399 return 0;
6400}
6401
6402/*
Wei Yongjune89c2092008-12-25 16:54:58 -08006403 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6404 * This option will get or set the maximum size to put in any outgoing
6405 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 * fragmented by SCTP into the specified size. Note that the underlying
6407 * SCTP implementation may fragment into smaller sized chunks when the
6408 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08006409 * the user. The default value for this option is '0' which indicates
6410 * the user is NOT limiting fragmentation and only the PMTU will effect
6411 * SCTP's choice of DATA chunk size. Note also that values set larger
6412 * than the maximum size of an IP datagram will effectively let SCTP
6413 * control fragmentation (i.e. the same as setting this option to 0).
6414 *
6415 * The following structure is used to access and modify this parameter:
6416 *
6417 * struct sctp_assoc_value {
6418 * sctp_assoc_t assoc_id;
6419 * uint32_t assoc_value;
6420 * };
6421 *
6422 * assoc_id: This parameter is ignored for one-to-one style sockets.
6423 * For one-to-many style sockets this parameter indicates which
6424 * association the user is performing an action upon. Note that if
6425 * this field's value is zero then the endpoints default value is
6426 * changed (effecting future associations only).
6427 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 */
6429static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6430 char __user *optval, int __user *optlen)
6431{
Wei Yongjune89c2092008-12-25 16:54:58 -08006432 struct sctp_assoc_value params;
6433 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434
Wei Yongjune89c2092008-12-25 16:54:58 -08006435 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006436 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006437 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006438 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006439 "Use struct sctp_assoc_value instead\n",
6440 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08006441 params.assoc_id = 0;
6442 } else if (len >= sizeof(struct sctp_assoc_value)) {
6443 len = sizeof(struct sctp_assoc_value);
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006444 if (copy_from_user(&params, optval, len))
Wei Yongjune89c2092008-12-25 16:54:58 -08006445 return -EFAULT;
6446 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 return -EINVAL;
6448
Wei Yongjune89c2092008-12-25 16:54:58 -08006449 asoc = sctp_id2assoc(sk, params.assoc_id);
6450 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
6451 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452
Wei Yongjune89c2092008-12-25 16:54:58 -08006453 if (asoc)
6454 params.assoc_value = asoc->frag_point;
6455 else
6456 params.assoc_value = sctp_sk(sk)->user_frag;
6457
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 if (put_user(len, optlen))
6459 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08006460 if (len == sizeof(int)) {
6461 if (copy_to_user(optval, &params.assoc_value, len))
6462 return -EFAULT;
6463 } else {
6464 if (copy_to_user(optval, &params, len))
6465 return -EFAULT;
6466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467
6468 return 0;
6469}
6470
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006471/*
6472 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6473 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6474 */
6475static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6476 char __user *optval, int __user *optlen)
6477{
6478 int val;
6479
6480 if (len < sizeof(int))
6481 return -EINVAL;
6482
6483 len = sizeof(int);
6484
6485 val = sctp_sk(sk)->frag_interleave;
6486 if (put_user(len, optlen))
6487 return -EFAULT;
6488 if (copy_to_user(optval, &val, len))
6489 return -EFAULT;
6490
6491 return 0;
6492}
6493
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006494/*
6495 * 7.1.25. Set or Get the sctp partial delivery point
6496 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6497 */
6498static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6499 char __user *optval,
6500 int __user *optlen)
6501{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09006502 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006503
6504 if (len < sizeof(u32))
6505 return -EINVAL;
6506
6507 len = sizeof(u32);
6508
6509 val = sctp_sk(sk)->pd_point;
6510 if (put_user(len, optlen))
6511 return -EFAULT;
6512 if (copy_to_user(optval, &val, len))
6513 return -EFAULT;
6514
Wei Yongjun7d743b72010-12-14 16:10:41 +00006515 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006516}
6517
Vlad Yasevich70331572007-03-23 11:34:36 -07006518/*
6519 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6520 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6521 */
6522static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6523 char __user *optval,
6524 int __user *optlen)
6525{
Neil Horman219b99a2008-03-05 13:44:46 -08006526 struct sctp_assoc_value params;
6527 struct sctp_sock *sp;
6528 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07006529
Neil Horman219b99a2008-03-05 13:44:46 -08006530 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006531 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006532 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006533 "Use of int in max_burst socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006534 "Use struct sctp_assoc_value instead\n",
6535 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08006536 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00006537 } else if (len >= sizeof(struct sctp_assoc_value)) {
6538 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08006539 if (copy_from_user(&params, optval, len))
6540 return -EFAULT;
6541 } else
6542 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07006543
Neil Horman219b99a2008-03-05 13:44:46 -08006544 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07006545
Neil Horman219b99a2008-03-05 13:44:46 -08006546 if (params.assoc_id != 0) {
6547 asoc = sctp_id2assoc(sk, params.assoc_id);
6548 if (!asoc)
6549 return -EINVAL;
6550 params.assoc_value = asoc->max_burst;
6551 } else
6552 params.assoc_value = sp->max_burst;
6553
6554 if (len == sizeof(int)) {
6555 if (copy_to_user(optval, &params.assoc_value, len))
6556 return -EFAULT;
6557 } else {
6558 if (copy_to_user(optval, &params, len))
6559 return -EFAULT;
6560 }
6561
6562 return 0;
6563
Vlad Yasevich70331572007-03-23 11:34:36 -07006564}
6565
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006566static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6567 char __user *optval, int __user *optlen)
6568{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006569 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006570 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006571 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006572 __u16 data_len = 0;
6573 u32 num_idents;
Xin Long7a84bd42016-02-03 23:33:30 +08006574 int i;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006575
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006576 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006577 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006578
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006579 hmacs = ep->auth_hmacs_list;
Xin Long3c918702017-06-30 11:52:16 +08006580 data_len = ntohs(hmacs->param_hdr.length) -
6581 sizeof(struct sctp_paramhdr);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006582
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006583 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006584 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006585
6586 len = sizeof(struct sctp_hmacalgo) + data_len;
6587 num_idents = data_len / sizeof(u16);
6588
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006589 if (put_user(len, optlen))
6590 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006591 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006592 return -EFAULT;
Xin Long7a84bd42016-02-03 23:33:30 +08006593 for (i = 0; i < num_idents; i++) {
6594 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6595
6596 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6597 return -EFAULT;
6598 }
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006599 return 0;
6600}
6601
6602static int sctp_getsockopt_active_key(struct sock *sk, int len,
6603 char __user *optval, int __user *optlen)
6604{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006605 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006606 struct sctp_authkeyid val;
6607 struct sctp_association *asoc;
6608
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006609 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006610 return -EACCES;
6611
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006612 if (len < sizeof(struct sctp_authkeyid))
6613 return -EINVAL;
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006614
6615 len = sizeof(struct sctp_authkeyid);
6616 if (copy_from_user(&val, optval, len))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006617 return -EFAULT;
6618
6619 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6620 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6621 return -EINVAL;
6622
6623 if (asoc)
6624 val.scact_keynumber = asoc->active_key_id;
6625 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006626 val.scact_keynumber = ep->active_key_id;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006627
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006628 if (put_user(len, optlen))
6629 return -EFAULT;
6630 if (copy_to_user(optval, &val, len))
6631 return -EFAULT;
6632
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006633 return 0;
6634}
6635
6636static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6637 char __user *optval, int __user *optlen)
6638{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006639 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006640 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006641 struct sctp_authchunks val;
6642 struct sctp_association *asoc;
6643 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006644 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006645 char __user *to;
6646
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006647 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006648 return -EACCES;
6649
6650 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006651 return -EINVAL;
6652
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006653 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006654 return -EFAULT;
6655
Al Viro411223c2007-10-14 19:21:20 +01006656 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006657 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6658 if (!asoc)
6659 return -EINVAL;
6660
6661 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006662 if (!ch)
6663 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006664
6665 /* See if the user provided enough room for all the data */
Xin Long3c918702017-06-30 11:52:16 +08006666 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevichb40db682008-02-27 14:40:37 -05006667 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006668 return -EINVAL;
6669
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006670 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006671 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006672num:
6673 len = sizeof(struct sctp_authchunks) + num_chunks;
wangweidong8d726512013-12-23 12:16:53 +08006674 if (put_user(len, optlen))
6675 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006676 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6677 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006678 return 0;
6679}
6680
6681static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6682 char __user *optval, int __user *optlen)
6683{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006684 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006685 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006686 struct sctp_authchunks val;
6687 struct sctp_association *asoc;
6688 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006689 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006690 char __user *to;
6691
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006692 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006693 return -EACCES;
6694
6695 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006696 return -EINVAL;
6697
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006698 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006699 return -EFAULT;
6700
Al Viro411223c2007-10-14 19:21:20 +01006701 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006702 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6703 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6704 return -EINVAL;
6705
6706 if (asoc)
wangweidong26ac8e52013-12-23 12:16:51 +08006707 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006708 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006709 ch = ep->auth_chunk_list;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006710
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006711 if (!ch)
6712 goto num;
6713
Xin Long3c918702017-06-30 11:52:16 +08006714 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006715 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006716 return -EINVAL;
6717
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006718 if (copy_to_user(to, ch->chunks, num_chunks))
6719 return -EFAULT;
6720num:
6721 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006722 if (put_user(len, optlen))
6723 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006724 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6725 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006726
6727 return 0;
6728}
6729
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08006730/*
6731 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6732 * This option gets the current number of associations that are attached
6733 * to a one-to-many style socket. The option value is an uint32_t.
6734 */
6735static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6736 char __user *optval, int __user *optlen)
6737{
6738 struct sctp_sock *sp = sctp_sk(sk);
6739 struct sctp_association *asoc;
6740 u32 val = 0;
6741
6742 if (sctp_style(sk, TCP))
6743 return -EOPNOTSUPP;
6744
6745 if (len < sizeof(u32))
6746 return -EINVAL;
6747
6748 len = sizeof(u32);
6749
6750 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6751 val++;
6752 }
6753
6754 if (put_user(len, optlen))
6755 return -EFAULT;
6756 if (copy_to_user(optval, &val, len))
6757 return -EFAULT;
6758
6759 return 0;
6760}
6761
Wei Yongjun209ba422011-04-17 17:27:08 +00006762/*
Michio Honda7dc04d72011-04-26 20:16:31 +09006763 * 8.1.23 SCTP_AUTO_ASCONF
6764 * See the corresponding setsockopt entry as description
6765 */
6766static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6767 char __user *optval, int __user *optlen)
6768{
6769 int val = 0;
6770
6771 if (len < sizeof(int))
6772 return -EINVAL;
6773
6774 len = sizeof(int);
6775 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6776 val = 1;
6777 if (put_user(len, optlen))
6778 return -EFAULT;
6779 if (copy_to_user(optval, &val, len))
6780 return -EFAULT;
6781 return 0;
6782}
6783
6784/*
Wei Yongjun209ba422011-04-17 17:27:08 +00006785 * 8.2.6. Get the Current Identifiers of Associations
6786 * (SCTP_GET_ASSOC_ID_LIST)
6787 *
6788 * This option gets the current list of SCTP association identifiers of
6789 * the SCTP associations handled by a one-to-many style socket.
6790 */
6791static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6792 char __user *optval, int __user *optlen)
6793{
6794 struct sctp_sock *sp = sctp_sk(sk);
6795 struct sctp_association *asoc;
6796 struct sctp_assoc_ids *ids;
6797 u32 num = 0;
6798
6799 if (sctp_style(sk, TCP))
6800 return -EOPNOTSUPP;
6801
6802 if (len < sizeof(struct sctp_assoc_ids))
6803 return -EINVAL;
6804
6805 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6806 num++;
6807 }
6808
6809 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6810 return -EINVAL;
6811
6812 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6813
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02006814 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
Wei Yongjun209ba422011-04-17 17:27:08 +00006815 if (unlikely(!ids))
6816 return -ENOMEM;
6817
6818 ids->gaids_number_of_ids = num;
6819 num = 0;
6820 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6821 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6822 }
6823
6824 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6825 kfree(ids);
6826 return -EFAULT;
6827 }
6828
6829 kfree(ids);
6830 return 0;
6831}
6832
Neil Horman5aa93bc2012-07-21 07:56:07 +00006833/*
6834 * SCTP_PEER_ADDR_THLDS
6835 *
6836 * This option allows us to fetch the partially failed threshold for one or all
6837 * transports in an association. See Section 6.1 of:
6838 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6839 */
6840static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6841 char __user *optval,
6842 int len,
6843 int __user *optlen)
6844{
6845 struct sctp_paddrthlds val;
6846 struct sctp_transport *trans;
6847 struct sctp_association *asoc;
6848
6849 if (len < sizeof(struct sctp_paddrthlds))
6850 return -EINVAL;
6851 len = sizeof(struct sctp_paddrthlds);
6852 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6853 return -EFAULT;
6854
6855 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6856 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6857 if (!asoc)
6858 return -ENOENT;
6859
6860 val.spt_pathpfthld = asoc->pf_retrans;
6861 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6862 } else {
6863 trans = sctp_addr_id2transport(sk, &val.spt_address,
6864 val.spt_assoc_id);
6865 if (!trans)
6866 return -ENOENT;
6867
6868 val.spt_pathmaxrxt = trans->pathmaxrxt;
6869 val.spt_pathpfthld = trans->pf_retrans;
6870 }
6871
6872 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6873 return -EFAULT;
6874
6875 return 0;
6876}
6877
Michele Baldessari196d6752012-12-01 04:49:42 +00006878/*
6879 * SCTP_GET_ASSOC_STATS
6880 *
6881 * This option retrieves local per endpoint statistics. It is modeled
6882 * after OpenSolaris' implementation
6883 */
6884static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6885 char __user *optval,
6886 int __user *optlen)
6887{
6888 struct sctp_assoc_stats sas;
6889 struct sctp_association *asoc = NULL;
6890
6891 /* User must provide at least the assoc id */
6892 if (len < sizeof(sctp_assoc_t))
6893 return -EINVAL;
6894
Guenter Roeck726bc6b2013-02-27 10:57:31 +00006895 /* Allow the struct to grow and fill in as much as possible */
6896 len = min_t(size_t, len, sizeof(sas));
6897
Michele Baldessari196d6752012-12-01 04:49:42 +00006898 if (copy_from_user(&sas, optval, len))
6899 return -EFAULT;
6900
6901 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6902 if (!asoc)
6903 return -EINVAL;
6904
6905 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6906 sas.sas_gapcnt = asoc->stats.gapcnt;
6907 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6908 sas.sas_osacks = asoc->stats.osacks;
6909 sas.sas_isacks = asoc->stats.isacks;
6910 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6911 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6912 sas.sas_oodchunks = asoc->stats.oodchunks;
6913 sas.sas_iodchunks = asoc->stats.iodchunks;
6914 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6915 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6916 sas.sas_idupchunks = asoc->stats.idupchunks;
6917 sas.sas_opackets = asoc->stats.opackets;
6918 sas.sas_ipackets = asoc->stats.ipackets;
6919
6920 /* New high max rto observed, will return 0 if not a single
6921 * RTO update took place. obs_rto_ipaddr will be bogus
6922 * in such a case
6923 */
6924 sas.sas_maxrto = asoc->stats.max_obs_rto;
6925 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6926 sizeof(struct sockaddr_storage));
6927
6928 /* Mark beginning of a new observation period */
6929 asoc->stats.max_obs_rto = asoc->rto_min;
6930
Michele Baldessari196d6752012-12-01 04:49:42 +00006931 if (put_user(len, optlen))
6932 return -EFAULT;
6933
Daniel Borkmannbb333812013-06-28 19:49:40 +02006934 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
Michele Baldessari196d6752012-12-01 04:49:42 +00006935
6936 if (copy_to_user(optval, &sas, len))
6937 return -EFAULT;
6938
6939 return 0;
6940}
6941
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02006942static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6943 char __user *optval,
6944 int __user *optlen)
6945{
6946 int val = 0;
6947
6948 if (len < sizeof(int))
6949 return -EINVAL;
6950
6951 len = sizeof(int);
6952 if (sctp_sk(sk)->recvrcvinfo)
6953 val = 1;
6954 if (put_user(len, optlen))
6955 return -EFAULT;
6956 if (copy_to_user(optval, &val, len))
6957 return -EFAULT;
6958
6959 return 0;
6960}
6961
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02006962static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6963 char __user *optval,
6964 int __user *optlen)
6965{
6966 int val = 0;
6967
6968 if (len < sizeof(int))
6969 return -EINVAL;
6970
6971 len = sizeof(int);
6972 if (sctp_sk(sk)->recvnxtinfo)
6973 val = 1;
6974 if (put_user(len, optlen))
6975 return -EFAULT;
6976 if (copy_to_user(optval, &val, len))
6977 return -EFAULT;
6978
6979 return 0;
6980}
6981
Xin Long28aa4c22016-07-09 19:47:40 +08006982static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6983 char __user *optval,
6984 int __user *optlen)
6985{
6986 struct sctp_assoc_value params;
6987 struct sctp_association *asoc;
6988 int retval = -EFAULT;
6989
6990 if (len < sizeof(params)) {
6991 retval = -EINVAL;
6992 goto out;
6993 }
6994
6995 len = sizeof(params);
6996 if (copy_from_user(&params, optval, len))
6997 goto out;
6998
6999 asoc = sctp_id2assoc(sk, params.assoc_id);
7000 if (asoc) {
7001 params.assoc_value = asoc->prsctp_enable;
7002 } else if (!params.assoc_id) {
7003 struct sctp_sock *sp = sctp_sk(sk);
7004
7005 params.assoc_value = sp->ep->prsctp_enable;
7006 } else {
7007 retval = -EINVAL;
7008 goto out;
7009 }
7010
7011 if (put_user(len, optlen))
7012 goto out;
7013
7014 if (copy_to_user(optval, &params, len))
7015 goto out;
7016
7017 retval = 0;
7018
7019out:
7020 return retval;
7021}
7022
Xin Longf959fb42016-07-09 19:47:41 +08007023static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
7024 char __user *optval,
7025 int __user *optlen)
7026{
7027 struct sctp_default_prinfo info;
7028 struct sctp_association *asoc;
7029 int retval = -EFAULT;
7030
7031 if (len < sizeof(info)) {
7032 retval = -EINVAL;
7033 goto out;
7034 }
7035
7036 len = sizeof(info);
7037 if (copy_from_user(&info, optval, len))
7038 goto out;
7039
7040 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
7041 if (asoc) {
7042 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
7043 info.pr_value = asoc->default_timetolive;
7044 } else if (!info.pr_assoc_id) {
7045 struct sctp_sock *sp = sctp_sk(sk);
7046
7047 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
7048 info.pr_value = sp->default_timetolive;
7049 } else {
7050 retval = -EINVAL;
7051 goto out;
7052 }
7053
7054 if (put_user(len, optlen))
7055 goto out;
7056
7057 if (copy_to_user(optval, &info, len))
7058 goto out;
7059
7060 retval = 0;
7061
7062out:
7063 return retval;
7064}
7065
Xin Long826d2532016-07-09 19:47:42 +08007066static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
7067 char __user *optval,
7068 int __user *optlen)
7069{
7070 struct sctp_prstatus params;
7071 struct sctp_association *asoc;
7072 int policy;
7073 int retval = -EINVAL;
7074
7075 if (len < sizeof(params))
7076 goto out;
7077
7078 len = sizeof(params);
7079 if (copy_from_user(&params, optval, len)) {
7080 retval = -EFAULT;
7081 goto out;
7082 }
7083
7084 policy = params.sprstat_policy;
7085 if (policy & ~SCTP_PR_SCTP_MASK)
7086 goto out;
7087
7088 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7089 if (!asoc)
7090 goto out;
7091
7092 if (policy == SCTP_PR_SCTP_NONE) {
7093 params.sprstat_abandoned_unsent = 0;
7094 params.sprstat_abandoned_sent = 0;
7095 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7096 params.sprstat_abandoned_unsent +=
7097 asoc->abandoned_unsent[policy];
7098 params.sprstat_abandoned_sent +=
7099 asoc->abandoned_sent[policy];
7100 }
7101 } else {
7102 params.sprstat_abandoned_unsent =
7103 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7104 params.sprstat_abandoned_sent =
7105 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
7106 }
7107
7108 if (put_user(len, optlen)) {
7109 retval = -EFAULT;
7110 goto out;
7111 }
7112
7113 if (copy_to_user(optval, &params, len)) {
7114 retval = -EFAULT;
7115 goto out;
7116 }
7117
7118 retval = 0;
7119
7120out:
7121 return retval;
7122}
7123
Xin Longd229d482017-04-01 17:07:46 +08007124static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
7125 char __user *optval,
7126 int __user *optlen)
7127{
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007128 struct sctp_stream_out_ext *streamoute;
Xin Longd229d482017-04-01 17:07:46 +08007129 struct sctp_association *asoc;
7130 struct sctp_prstatus params;
7131 int retval = -EINVAL;
7132 int policy;
7133
7134 if (len < sizeof(params))
7135 goto out;
7136
7137 len = sizeof(params);
7138 if (copy_from_user(&params, optval, len)) {
7139 retval = -EFAULT;
7140 goto out;
7141 }
7142
7143 policy = params.sprstat_policy;
7144 if (policy & ~SCTP_PR_SCTP_MASK)
7145 goto out;
7146
7147 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
Xin Longcee360a2017-05-31 16:36:31 +08007148 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
Xin Longd229d482017-04-01 17:07:46 +08007149 goto out;
7150
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03007151 streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007152 if (!streamoute) {
7153 /* Not allocated yet, means all stats are 0 */
7154 params.sprstat_abandoned_unsent = 0;
7155 params.sprstat_abandoned_sent = 0;
7156 retval = 0;
7157 goto out;
7158 }
7159
Xin Longd229d482017-04-01 17:07:46 +08007160 if (policy == SCTP_PR_SCTP_NONE) {
7161 params.sprstat_abandoned_unsent = 0;
7162 params.sprstat_abandoned_sent = 0;
7163 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7164 params.sprstat_abandoned_unsent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007165 streamoute->abandoned_unsent[policy];
Xin Longd229d482017-04-01 17:07:46 +08007166 params.sprstat_abandoned_sent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007167 streamoute->abandoned_sent[policy];
Xin Longd229d482017-04-01 17:07:46 +08007168 }
7169 } else {
7170 params.sprstat_abandoned_unsent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007171 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08007172 params.sprstat_abandoned_sent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007173 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08007174 }
7175
7176 if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
7177 retval = -EFAULT;
7178 goto out;
7179 }
7180
7181 retval = 0;
7182
7183out:
7184 return retval;
7185}
7186
Xin Longc0d8bab2017-03-10 12:11:12 +08007187static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
7188 char __user *optval,
7189 int __user *optlen)
7190{
7191 struct sctp_assoc_value params;
7192 struct sctp_association *asoc;
7193 int retval = -EFAULT;
7194
7195 if (len < sizeof(params)) {
7196 retval = -EINVAL;
7197 goto out;
7198 }
7199
7200 len = sizeof(params);
7201 if (copy_from_user(&params, optval, len))
7202 goto out;
7203
7204 asoc = sctp_id2assoc(sk, params.assoc_id);
7205 if (asoc) {
7206 params.assoc_value = asoc->reconf_enable;
7207 } else if (!params.assoc_id) {
7208 struct sctp_sock *sp = sctp_sk(sk);
7209
7210 params.assoc_value = sp->ep->reconf_enable;
7211 } else {
7212 retval = -EINVAL;
7213 goto out;
7214 }
7215
7216 if (put_user(len, optlen))
7217 goto out;
7218
7219 if (copy_to_user(optval, &params, len))
7220 goto out;
7221
7222 retval = 0;
7223
7224out:
7225 return retval;
7226}
7227
Xin Long9fb657a2017-01-18 00:44:46 +08007228static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7229 char __user *optval,
7230 int __user *optlen)
7231{
7232 struct sctp_assoc_value params;
7233 struct sctp_association *asoc;
7234 int retval = -EFAULT;
7235
7236 if (len < sizeof(params)) {
7237 retval = -EINVAL;
7238 goto out;
7239 }
7240
7241 len = sizeof(params);
7242 if (copy_from_user(&params, optval, len))
7243 goto out;
7244
7245 asoc = sctp_id2assoc(sk, params.assoc_id);
7246 if (asoc) {
7247 params.assoc_value = asoc->strreset_enable;
7248 } else if (!params.assoc_id) {
7249 struct sctp_sock *sp = sctp_sk(sk);
7250
7251 params.assoc_value = sp->ep->strreset_enable;
7252 } else {
7253 retval = -EINVAL;
7254 goto out;
7255 }
7256
7257 if (put_user(len, optlen))
7258 goto out;
7259
7260 if (copy_to_user(optval, &params, len))
7261 goto out;
7262
7263 retval = 0;
7264
7265out:
7266 return retval;
7267}
7268
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007269static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7270 char __user *optval,
7271 int __user *optlen)
7272{
7273 struct sctp_assoc_value params;
7274 struct sctp_association *asoc;
7275 int retval = -EFAULT;
7276
7277 if (len < sizeof(params)) {
7278 retval = -EINVAL;
7279 goto out;
7280 }
7281
7282 len = sizeof(params);
7283 if (copy_from_user(&params, optval, len))
7284 goto out;
7285
7286 asoc = sctp_id2assoc(sk, params.assoc_id);
7287 if (!asoc) {
7288 retval = -EINVAL;
7289 goto out;
7290 }
7291
7292 params.assoc_value = sctp_sched_get_sched(asoc);
7293
7294 if (put_user(len, optlen))
7295 goto out;
7296
7297 if (copy_to_user(optval, &params, len))
7298 goto out;
7299
7300 retval = 0;
7301
7302out:
7303 return retval;
7304}
7305
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007306static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7307 char __user *optval,
7308 int __user *optlen)
7309{
7310 struct sctp_stream_value params;
7311 struct sctp_association *asoc;
7312 int retval = -EFAULT;
7313
7314 if (len < sizeof(params)) {
7315 retval = -EINVAL;
7316 goto out;
7317 }
7318
7319 len = sizeof(params);
7320 if (copy_from_user(&params, optval, len))
7321 goto out;
7322
7323 asoc = sctp_id2assoc(sk, params.assoc_id);
7324 if (!asoc) {
7325 retval = -EINVAL;
7326 goto out;
7327 }
7328
7329 retval = sctp_sched_get_value(asoc, params.stream_id,
7330 &params.stream_value);
7331 if (retval)
7332 goto out;
7333
7334 if (put_user(len, optlen)) {
7335 retval = -EFAULT;
7336 goto out;
7337 }
7338
7339 if (copy_to_user(optval, &params, len)) {
7340 retval = -EFAULT;
7341 goto out;
7342 }
7343
7344out:
7345 return retval;
7346}
7347
Xin Long772a5862017-12-08 21:03:58 +08007348static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7349 char __user *optval,
7350 int __user *optlen)
7351{
7352 struct sctp_assoc_value params;
7353 struct sctp_association *asoc;
7354 int retval = -EFAULT;
7355
7356 if (len < sizeof(params)) {
7357 retval = -EINVAL;
7358 goto out;
7359 }
7360
7361 len = sizeof(params);
7362 if (copy_from_user(&params, optval, len))
7363 goto out;
7364
7365 asoc = sctp_id2assoc(sk, params.assoc_id);
7366 if (asoc) {
7367 params.assoc_value = asoc->intl_enable;
7368 } else if (!params.assoc_id) {
7369 struct sctp_sock *sp = sctp_sk(sk);
7370
7371 params.assoc_value = sp->strm_interleave;
7372 } else {
7373 retval = -EINVAL;
7374 goto out;
7375 }
7376
7377 if (put_user(len, optlen))
7378 goto out;
7379
7380 if (copy_to_user(optval, &params, len))
7381 goto out;
7382
7383 retval = 0;
7384
7385out:
7386 return retval;
7387}
7388
Xin Longb0e9a2f2018-06-28 15:31:00 +08007389static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
7390 char __user *optval,
7391 int __user *optlen)
7392{
7393 int val;
7394
7395 if (len < sizeof(int))
7396 return -EINVAL;
7397
7398 len = sizeof(int);
7399 val = sctp_sk(sk)->reuse;
7400 if (put_user(len, optlen))
7401 return -EFAULT;
7402
7403 if (copy_to_user(optval, &val, len))
7404 return -EFAULT;
7405
7406 return 0;
7407}
7408
Daniel Borkmanndda91922013-06-17 11:40:05 +02007409static int sctp_getsockopt(struct sock *sk, int level, int optname,
7410 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411{
7412 int retval = 0;
7413 int len;
7414
Daniel Borkmannbb333812013-06-28 19:49:40 +02007415 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416
7417 /* I can hardly begin to describe how wrong this is. This is
7418 * so broken as to be worse than useless. The API draft
7419 * REALLY is NOT helpful here... I am not convinced that the
7420 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7421 * are at all well-founded.
7422 */
7423 if (level != SOL_SCTP) {
7424 struct sctp_af *af = sctp_sk(sk)->pf->af;
7425
7426 retval = af->getsockopt(sk, level, optname, optval, optlen);
7427 return retval;
7428 }
7429
7430 if (get_user(len, optlen))
7431 return -EFAULT;
7432
Jiri Slabya4b8e712016-10-21 14:13:24 +02007433 if (len < 0)
7434 return -EINVAL;
7435
wangweidong048ed4b2014-01-21 15:44:11 +08007436 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437
7438 switch (optname) {
7439 case SCTP_STATUS:
7440 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7441 break;
7442 case SCTP_DISABLE_FRAGMENTS:
7443 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7444 optlen);
7445 break;
7446 case SCTP_EVENTS:
7447 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7448 break;
7449 case SCTP_AUTOCLOSE:
7450 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7451 break;
7452 case SCTP_SOCKOPT_PEELOFF:
7453 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7454 break;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04007455 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7456 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7457 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 case SCTP_PEER_ADDR_PARAMS:
7459 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7460 optlen);
7461 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00007462 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07007463 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08007464 optlen);
7465 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 case SCTP_INITMSG:
7467 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7468 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 case SCTP_GET_PEER_ADDRS:
7470 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7471 optlen);
7472 break;
7473 case SCTP_GET_LOCAL_ADDRS:
7474 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7475 optlen);
7476 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04007477 case SCTP_SOCKOPT_CONNECTX3:
7478 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7479 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 case SCTP_DEFAULT_SEND_PARAM:
7481 retval = sctp_getsockopt_default_send_param(sk, len,
7482 optval, optlen);
7483 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02007484 case SCTP_DEFAULT_SNDINFO:
7485 retval = sctp_getsockopt_default_sndinfo(sk, len,
7486 optval, optlen);
7487 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488 case SCTP_PRIMARY_ADDR:
7489 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7490 break;
7491 case SCTP_NODELAY:
7492 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7493 break;
7494 case SCTP_RTOINFO:
7495 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7496 break;
7497 case SCTP_ASSOCINFO:
7498 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7499 break;
7500 case SCTP_I_WANT_MAPPED_V4_ADDR:
7501 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7502 break;
7503 case SCTP_MAXSEG:
7504 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7505 break;
7506 case SCTP_GET_PEER_ADDR_INFO:
7507 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7508 optlen);
7509 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08007510 case SCTP_ADAPTATION_LAYER:
7511 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 optlen);
7513 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08007514 case SCTP_CONTEXT:
7515 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7516 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007517 case SCTP_FRAGMENT_INTERLEAVE:
7518 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7519 optlen);
7520 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07007521 case SCTP_PARTIAL_DELIVERY_POINT:
7522 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7523 optlen);
7524 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07007525 case SCTP_MAX_BURST:
7526 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7527 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007528 case SCTP_AUTH_KEY:
7529 case SCTP_AUTH_CHUNK:
7530 case SCTP_AUTH_DELETE_KEY:
Xin Long601590e2018-03-14 19:05:32 +08007531 case SCTP_AUTH_DEACTIVATE_KEY:
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007532 retval = -EOPNOTSUPP;
7533 break;
7534 case SCTP_HMAC_IDENT:
7535 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7536 break;
7537 case SCTP_AUTH_ACTIVE_KEY:
7538 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7539 break;
7540 case SCTP_PEER_AUTH_CHUNKS:
7541 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7542 optlen);
7543 break;
7544 case SCTP_LOCAL_AUTH_CHUNKS:
7545 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7546 optlen);
7547 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08007548 case SCTP_GET_ASSOC_NUMBER:
7549 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7550 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00007551 case SCTP_GET_ASSOC_ID_LIST:
7552 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7553 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09007554 case SCTP_AUTO_ASCONF:
7555 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7556 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00007557 case SCTP_PEER_ADDR_THLDS:
7558 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7559 break;
Michele Baldessari196d6752012-12-01 04:49:42 +00007560 case SCTP_GET_ASSOC_STATS:
7561 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7562 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02007563 case SCTP_RECVRCVINFO:
7564 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7565 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007566 case SCTP_RECVNXTINFO:
7567 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7568 break;
Xin Long28aa4c22016-07-09 19:47:40 +08007569 case SCTP_PR_SUPPORTED:
7570 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7571 break;
Xin Longf959fb42016-07-09 19:47:41 +08007572 case SCTP_DEFAULT_PRINFO:
7573 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7574 optlen);
7575 break;
Xin Long826d2532016-07-09 19:47:42 +08007576 case SCTP_PR_ASSOC_STATUS:
7577 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7578 optlen);
7579 break;
Xin Longd229d482017-04-01 17:07:46 +08007580 case SCTP_PR_STREAM_STATUS:
7581 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7582 optlen);
7583 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08007584 case SCTP_RECONFIG_SUPPORTED:
7585 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7586 optlen);
7587 break;
Xin Long9fb657a2017-01-18 00:44:46 +08007588 case SCTP_ENABLE_STREAM_RESET:
7589 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7590 optlen);
7591 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007592 case SCTP_STREAM_SCHEDULER:
7593 retval = sctp_getsockopt_scheduler(sk, len, optval,
7594 optlen);
7595 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007596 case SCTP_STREAM_SCHEDULER_VALUE:
7597 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7598 optlen);
7599 break;
Xin Long772a5862017-12-08 21:03:58 +08007600 case SCTP_INTERLEAVING_SUPPORTED:
7601 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7602 optlen);
7603 break;
Xin Longb0e9a2f2018-06-28 15:31:00 +08007604 case SCTP_REUSE_PORT:
7605 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
7606 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607 default:
7608 retval = -ENOPROTOOPT;
7609 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611
wangweidong048ed4b2014-01-21 15:44:11 +08007612 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613 return retval;
7614}
7615
Craig Gallek086c6532016-02-10 11:50:35 -05007616static int sctp_hash(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617{
7618 /* STUB */
Craig Gallek086c6532016-02-10 11:50:35 -05007619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620}
7621
7622static void sctp_unhash(struct sock *sk)
7623{
7624 /* STUB */
7625}
7626
7627/* Check if port is acceptable. Possibly find first available port.
7628 *
7629 * The port hash table (contained in the 'global' SCTP protocol storage
7630 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7631 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7632 * list (the list number is the port number hashed out, so as you
7633 * would expect from a hash function, all the ports in a given list have
7634 * such a number that hashes out to the same list number; you were
7635 * expecting that, right?); so each list has a set of ports, with a
7636 * link to the socket (struct sock) that uses it, the port number and
7637 * a fastreuse flag (FIXME: NPI ipg).
7638 */
7639static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007640 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641
7642static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
7643{
Xin Longb0e9a2f2018-06-28 15:31:00 +08007644 bool reuse = (sk->sk_reuse || sctp_sk(sk)->reuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645 struct sctp_bind_hashbucket *head; /* hash list */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007646 struct sctp_bind_bucket *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647 unsigned short snum;
7648 int ret;
7649
Al Viro04afd8b2006-11-20 17:02:01 -08007650 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651
Daniel Borkmannbb333812013-06-28 19:49:40 +02007652 pr_debug("%s: begins, snum:%d\n", __func__, snum);
7653
wangweidong79b91132014-01-21 15:44:07 +08007654 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655
7656 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07007657 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007658 int low, high, remaining, index;
7659 unsigned int rover;
WANG Cong122ff242014-05-12 16:04:53 -07007660 struct net *net = sock_net(sk);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007661
WANG Cong122ff242014-05-12 16:04:53 -07007662 inet_get_local_port_range(net, &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007663 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05007664 rover = prandom_u32() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666 do {
7667 rover++;
7668 if ((rover < low) || (rover > high))
7669 rover = low;
WANG Cong122ff242014-05-12 16:04:53 -07007670 if (inet_is_local_reserved_port(net, rover))
Amerigo Wange3826f12010-05-05 00:27:06 +00007671 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007672 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673 head = &sctp_port_hashtable[index];
wangweidong3c8e43b2014-01-21 15:44:08 +08007674 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007675 sctp_for_each_hentry(pp, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007676 if ((pp->port == rover) &&
7677 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678 goto next;
7679 break;
7680 next:
wangweidong3c8e43b2014-01-21 15:44:08 +08007681 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683
7684 /* Exhausted local port range during search? */
7685 ret = 1;
7686 if (remaining <= 0)
7687 goto fail;
7688
7689 /* OK, here is the one we will use. HEAD (the port
7690 * hash table list entry) is non-NULL and we hold it's
7691 * mutex.
7692 */
7693 snum = rover;
7694 } else {
7695 /* We are given an specific port number; we verify
7696 * that it is not being used. If it is used, we will
7697 * exahust the search in the hash list corresponding
7698 * to the port number (snum) - we detect that with the
7699 * port iterator, pp being NULL.
7700 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007701 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
wangweidong3c8e43b2014-01-21 15:44:08 +08007702 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007703 sctp_for_each_hentry(pp, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007704 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705 goto pp_found;
7706 }
7707 }
7708 pp = NULL;
7709 goto pp_not_found;
7710pp_found:
7711 if (!hlist_empty(&pp->owner)) {
7712 /* We had a port hash table hit - there is an
7713 * available port (pp != NULL) and it is being
7714 * used by other socket (pp->owner not empty); that other
7715 * socket is going to be sk2.
7716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007718
Daniel Borkmannbb333812013-06-28 19:49:40 +02007719 pr_debug("%s: found a possible match\n", __func__);
7720
Xin Longb0e9a2f2018-06-28 15:31:00 +08007721 if (pp->fastreuse && reuse && sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007722 goto success;
7723
7724 /* Run through the list of sockets bound to the port
7725 * (pp->port) [via the pointers bind_next and
7726 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
7727 * we get the endpoint they describe and run through
7728 * the endpoint's list of IP (v4 or v6) addresses,
7729 * comparing each of the addresses with the address of
7730 * the socket sk. If we find a match, then that means
7731 * that this port/socket (sk) combination are already
7732 * in an endpoint.
7733 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007734 sk_for_each_bound(sk2, &pp->owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007735 struct sctp_endpoint *ep2;
7736 ep2 = sctp_sk(sk2)->ep;
7737
Vlad Yasevich4e540642008-07-18 23:06:32 -07007738 if (sk == sk2 ||
Xin Longb0e9a2f2018-06-28 15:31:00 +08007739 (reuse && (sk2->sk_reuse || sctp_sk(sk2)->reuse) &&
Vlad Yasevich4e540642008-07-18 23:06:32 -07007740 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007741 continue;
7742
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07007743 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
7744 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745 ret = (long)sk2;
7746 goto fail_unlock;
7747 }
7748 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02007749
7750 pr_debug("%s: found a match\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751 }
7752pp_not_found:
7753 /* If there was a hash table miss, create a new port. */
7754 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007755 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007756 goto fail_unlock;
7757
7758 /* In either case (hit or miss), make sure fastreuse is 1 only
7759 * if sk->sk_reuse is too (that is, if the caller requested
7760 * SO_REUSEADDR on this socket -sk-).
7761 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007762 if (hlist_empty(&pp->owner)) {
Xin Longb0e9a2f2018-06-28 15:31:00 +08007763 if (reuse && sk->sk_state != SCTP_SS_LISTENING)
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007764 pp->fastreuse = 1;
7765 else
7766 pp->fastreuse = 0;
7767 } else if (pp->fastreuse &&
Xin Longb0e9a2f2018-06-28 15:31:00 +08007768 (!reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769 pp->fastreuse = 0;
7770
7771 /* We are set, so fill up all the data in the hash table
7772 * entry, tie the socket list information with the rest of the
7773 * sockets FIXME: Blurry, NPI (ipg).
7774 */
7775success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007777 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007778 sk_add_bind_node(sk, &pp->owner);
7779 sctp_sk(sk)->bind_hash = pp;
7780 }
7781 ret = 0;
7782
7783fail_unlock:
wangweidong3c8e43b2014-01-21 15:44:08 +08007784 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785
7786fail:
wangweidong79b91132014-01-21 15:44:07 +08007787 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788 return ret;
7789}
7790
7791/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
7792 * port is requested.
7793 */
7794static int sctp_get_port(struct sock *sk, unsigned short snum)
7795{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796 union sctp_addr addr;
7797 struct sctp_af *af = sctp_sk(sk)->pf->af;
7798
7799 /* Set up a dummy address struct from the sk. */
7800 af->from_sk(&addr, sk);
7801 addr.v4.sin_port = htons(snum);
7802
7803 /* Note: sk->sk_num gets filled in if ephemeral port request. */
Daniel Borkmann62208f12013-06-25 18:17:30 +02007804 return !!sctp_get_port_local(sk, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007805}
7806
7807/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808 * Move a socket to LISTENING state.
7809 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02007810static int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007812 struct sctp_sock *sp = sctp_sk(sk);
7813 struct sctp_endpoint *ep = sp->ep;
Herbert Xu5821c762016-01-24 21:20:12 +08007814 struct crypto_shash *tfm = NULL;
Neil Horman3c681982012-10-24 09:20:03 +00007815 char alg[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816
7817 /* Allocate HMAC for generating cookie. */
Neil Horman3c681982012-10-24 09:20:03 +00007818 if (!sp->hmac && sp->sctp_hmac_alg) {
7819 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
Herbert Xu5821c762016-01-24 21:20:12 +08007820 tfm = crypto_alloc_shash(alg, 0, 0);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07007821 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00007822 net_info_ratelimited("failed to load transform for %s: %ld\n",
Neil Horman3c681982012-10-24 09:20:03 +00007823 sp->sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007824 return -ENOSYS;
7825 }
7826 sctp_sk(sk)->hmac = tfm;
7827 }
7828
7829 /*
7830 * If a bind() or sctp_bindx() is not called prior to a listen()
7831 * call that allows new associations to be accepted, the system
7832 * picks an ephemeral port and will choose an address set equivalent
7833 * to binding with a wildcard address.
7834 *
7835 * This is not currently spelled out in the SCTP sockets
7836 * extensions draft, but follows the practice as seen in TCP
7837 * sockets.
7838 *
7839 */
Yafang Shaocbabf462017-12-20 11:12:54 +08007840 inet_sk_set_state(sk, SCTP_SS_LISTENING);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007841 if (!ep->base.bind_addr.port) {
7842 if (sctp_autobind(sk))
7843 return -EAGAIN;
7844 } else {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007845 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08007846 inet_sk_set_state(sk, SCTP_SS_CLOSED);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007847 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007848 }
7849 }
7850
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007851 sk->sk_max_ack_backlog = backlog;
7852 sctp_hash_endpoint(ep);
7853 return 0;
7854}
7855
7856/*
7857 * 4.1.3 / 5.1.3 listen()
7858 *
7859 * By default, new associations are not accepted for UDP style sockets.
7860 * An application uses listen() to mark a socket as being able to
7861 * accept new associations.
7862 *
7863 * On TCP style sockets, applications use listen() to ready the SCTP
7864 * endpoint for accepting inbound associations.
7865 *
7866 * On both types of endpoints a backlog of '0' disables listening.
7867 *
7868 * Move a socket to LISTENING state.
7869 */
7870int sctp_inet_listen(struct socket *sock, int backlog)
7871{
7872 struct sock *sk = sock->sk;
7873 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7874 int err = -EINVAL;
7875
7876 if (unlikely(backlog < 0))
7877 return err;
7878
wangweidong048ed4b2014-01-21 15:44:11 +08007879 lock_sock(sk);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007880
7881 /* Peeled-off sockets are not allowed to listen(). */
7882 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
7883 goto out;
7884
7885 if (sock->state != SS_UNCONNECTED)
7886 goto out;
7887
Xin Long34b27892017-04-06 13:10:52 +08007888 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
7889 goto out;
7890
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007891 /* If backlog is zero, disable listening. */
7892 if (!backlog) {
7893 if (sctp_sstate(sk, CLOSED))
7894 goto out;
7895
7896 err = 0;
7897 sctp_unhash_endpoint(ep);
7898 sk->sk_state = SCTP_SS_CLOSED;
Xin Longb0e9a2f2018-06-28 15:31:00 +08007899 if (sk->sk_reuse || sctp_sk(sk)->reuse)
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007900 sctp_sk(sk)->bind_hash->fastreuse = 1;
7901 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007902 }
7903
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007904 /* If we are already listening, just update the backlog */
7905 if (sctp_sstate(sk, LISTENING))
7906 sk->sk_max_ack_backlog = backlog;
7907 else {
7908 err = sctp_listen_start(sk, backlog);
7909 if (err)
7910 goto out;
7911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007912
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007913 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914out:
wangweidong048ed4b2014-01-21 15:44:11 +08007915 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917}
7918
7919/*
7920 * This function is done by modeling the current datagram_poll() and the
7921 * tcp_poll(). Note that, based on these implementations, we don't
7922 * lock the socket in this function, even though it seems that,
7923 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08007924 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925 * in this place. We assume that we don't need locks either until proven
7926 * otherwise.
7927 *
7928 * Another thing to note is that we include the Async I/O support
7929 * here, again, by modeling the current TCP/UDP code. We don't have
7930 * a good way to test with it yet.
7931 */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07007932__poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933{
7934 struct sock *sk = sock->sk;
7935 struct sctp_sock *sp = sctp_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -04007936 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07007938 poll_wait(file, sk_sleep(sk), wait);
7939
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03007940 sock_rps_record_flow(sk);
7941
Linus Torvalds1da177e2005-04-16 15:20:36 -07007942 /* A TCP-style listening socket becomes readable when the accept queue
7943 * is not empty.
7944 */
7945 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
7946 return (!list_empty(&sp->ep->asocs)) ?
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007947 (EPOLLIN | EPOLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948
7949 mask = 0;
7950
7951 /* Is there any exceptional events? */
7952 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007953 mask |= EPOLLERR |
7954 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
Davide Libenzif348d702006-03-25 03:07:39 -08007955 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007956 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007958 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007959
7960 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00007961 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007962 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963
7964 /* The association is either gone or not ready. */
7965 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
7966 return mask;
7967
7968 /* Is it writable? */
7969 if (sctp_writeable(sk)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007970 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007971 } else {
Eric Dumazet9cd3e072015-11-29 20:03:10 -08007972 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973 /*
7974 * Since the socket is not locked, the buffer
7975 * might be made available after the writeable check and
7976 * before the bit is set. This could cause a lost I/O
7977 * signal. tcp_poll() has a race breaker for this race
7978 * condition. Based on their implementation, we put
7979 * in the following code to cover it as well.
7980 */
7981 if (sctp_writeable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007982 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007983 }
7984 return mask;
7985}
7986
7987/********************************************************************
7988 * 2nd Level Abstractions
7989 ********************************************************************/
7990
7991static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007992 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993{
7994 struct sctp_bind_bucket *pp;
7995
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08007996 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07007998 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999 pp->port = snum;
8000 pp->fastreuse = 0;
8001 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008002 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05008003 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008004 }
8005 return pp;
8006}
8007
8008/* Caller must hold hashbucket lock for this tb with local BH disabled */
8009static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
8010{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07008011 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05008012 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013 kmem_cache_free(sctp_bucket_cachep, pp);
8014 SCTP_DBG_OBJCNT_DEC(bind_bucket);
8015 }
8016}
8017
8018/* Release this socket's reference to a local port. */
8019static inline void __sctp_put_port(struct sock *sk)
8020{
8021 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008022 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
8023 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024 struct sctp_bind_bucket *pp;
8025
wangweidong3c8e43b2014-01-21 15:44:08 +08008026 spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027 pp = sctp_sk(sk)->bind_hash;
8028 __sk_del_bind_node(sk);
8029 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008030 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008031 sctp_bucket_destroy(pp);
wangweidong3c8e43b2014-01-21 15:44:08 +08008032 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008033}
8034
8035void sctp_put_port(struct sock *sk)
8036{
wangweidong79b91132014-01-21 15:44:07 +08008037 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008038 __sctp_put_port(sk);
wangweidong79b91132014-01-21 15:44:07 +08008039 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008040}
8041
8042/*
8043 * The system picks an ephemeral port and choose an address set equivalent
8044 * to binding with a wildcard address.
8045 * One of those addresses will be the primary address for the association.
8046 * This automatically enables the multihoming capability of SCTP.
8047 */
8048static int sctp_autobind(struct sock *sk)
8049{
8050 union sctp_addr autoaddr;
8051 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08008052 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008053
8054 /* Initialize a local sockaddr structure to INADDR_ANY. */
8055 af = sctp_sk(sk)->pf->af;
8056
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008057 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008058 af->inaddr_any(&autoaddr, port);
8059
8060 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
8061}
8062
8063/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
8064 *
8065 * From RFC 2292
8066 * 4.2 The cmsghdr Structure *
8067 *
8068 * When ancillary data is sent or received, any number of ancillary data
8069 * objects can be specified by the msg_control and msg_controllen members of
8070 * the msghdr structure, because each object is preceded by
8071 * a cmsghdr structure defining the object's length (the cmsg_len member).
8072 * Historically Berkeley-derived implementations have passed only one object
8073 * at a time, but this API allows multiple objects to be
8074 * passed in a single call to sendmsg() or recvmsg(). The following example
8075 * shows two ancillary data objects in a control buffer.
8076 *
8077 * |<--------------------------- msg_controllen -------------------------->|
8078 * | |
8079 *
8080 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
8081 *
8082 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
8083 * | | |
8084 *
8085 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
8086 *
8087 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
8088 * | | | | |
8089 *
8090 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8091 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
8092 *
8093 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
8094 *
8095 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8096 * ^
8097 * |
8098 *
8099 * msg_control
8100 * points here
8101 */
Xin Longa05437a2017-08-11 10:23:48 +08008102static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008103{
Vlad Yasevichab38fb02008-04-12 18:40:06 -07008104 struct msghdr *my_msg = (struct msghdr *)msg;
Xin Longa05437a2017-08-11 10:23:48 +08008105 struct cmsghdr *cmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008106
Gu Zhengf95b4142014-12-11 11:22:04 +08008107 for_each_cmsghdr(cmsg, my_msg) {
Vlad Yasevichab38fb02008-04-12 18:40:06 -07008108 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109 return -EINVAL;
8110
8111 /* Should we parse this header or ignore? */
8112 if (cmsg->cmsg_level != IPPROTO_SCTP)
8113 continue;
8114
8115 /* Strictly check lengths following example in SCM code. */
8116 switch (cmsg->cmsg_type) {
8117 case SCTP_INIT:
8118 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008119 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008120 *
8121 * This cmsghdr structure provides information for
8122 * initializing new SCTP associations with sendmsg().
8123 * The SCTP_INITMSG socket option uses this same data
8124 * structure. This structure is not used for
8125 * recvmsg().
8126 *
8127 * cmsg_level cmsg_type cmsg_data[]
8128 * ------------ ------------ ----------------------
8129 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
8130 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008131 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008132 return -EINVAL;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008133
8134 cmsgs->init = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008135 break;
8136
8137 case SCTP_SNDRCV:
8138 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008139 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008140 *
8141 * This cmsghdr structure specifies SCTP options for
8142 * sendmsg() and describes SCTP header information
8143 * about a received message through recvmsg().
8144 *
8145 * cmsg_level cmsg_type cmsg_data[]
8146 * ------------ ------------ ----------------------
8147 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
8148 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008149 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150 return -EINVAL;
8151
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008152 cmsgs->srinfo = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008153
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008154 if (cmsgs->srinfo->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07008155 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Long49102802018-03-05 20:44:20 +08008156 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8157 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008158 return -EINVAL;
8159 break;
8160
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008161 case SCTP_SNDINFO:
8162 /* SCTP Socket API Extension
8163 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
8164 *
8165 * This cmsghdr structure specifies SCTP options for
8166 * sendmsg(). This structure and SCTP_RCVINFO replaces
8167 * SCTP_SNDRCV which has been deprecated.
8168 *
8169 * cmsg_level cmsg_type cmsg_data[]
8170 * ------------ ------------ ---------------------
8171 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
8172 */
8173 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
8174 return -EINVAL;
8175
8176 cmsgs->sinfo = CMSG_DATA(cmsg);
8177
8178 if (cmsgs->sinfo->snd_flags &
8179 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Long49102802018-03-05 20:44:20 +08008180 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8181 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008182 return -EINVAL;
8183 break;
Xin Longed63afb2018-03-05 20:44:18 +08008184 case SCTP_PRINFO:
8185 /* SCTP Socket API Extension
8186 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
8187 *
8188 * This cmsghdr structure specifies SCTP options for sendmsg().
8189 *
8190 * cmsg_level cmsg_type cmsg_data[]
8191 * ------------ ------------ ---------------------
8192 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
8193 */
8194 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
8195 return -EINVAL;
8196
8197 cmsgs->prinfo = CMSG_DATA(cmsg);
8198 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
8199 return -EINVAL;
8200
8201 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
8202 cmsgs->prinfo->pr_value = 0;
8203 break;
Xin Long3ff547c2018-03-14 19:05:31 +08008204 case SCTP_AUTHINFO:
8205 /* SCTP Socket API Extension
8206 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
8207 *
8208 * This cmsghdr structure specifies SCTP options for sendmsg().
8209 *
8210 * cmsg_level cmsg_type cmsg_data[]
8211 * ------------ ------------ ---------------------
8212 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
8213 */
8214 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
8215 return -EINVAL;
8216
8217 cmsgs->authinfo = CMSG_DATA(cmsg);
8218 break;
Xin Long2c0dbaa2018-03-05 20:44:19 +08008219 case SCTP_DSTADDRV4:
8220 case SCTP_DSTADDRV6:
8221 /* SCTP Socket API Extension
8222 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
8223 *
8224 * This cmsghdr structure specifies SCTP options for sendmsg().
8225 *
8226 * cmsg_level cmsg_type cmsg_data[]
8227 * ------------ ------------ ---------------------
8228 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
8229 * ------------ ------------ ---------------------
8230 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
8231 */
8232 cmsgs->addrs_msg = my_msg;
8233 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008234 default:
8235 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07008236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237 }
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008238
Linus Torvalds1da177e2005-04-16 15:20:36 -07008239 return 0;
8240}
8241
8242/*
8243 * Wait for a packet..
8244 * Note: This function is the same function as in core/datagram.c
8245 * with a few modifications to make lksctp work.
8246 */
wangweidong26ac8e52013-12-23 12:16:51 +08008247static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248{
8249 int error;
8250 DEFINE_WAIT(wait);
8251
Eric Dumazetaa395142010-04-20 13:03:51 +00008252 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253
8254 /* Socket errors? */
8255 error = sock_error(sk);
8256 if (error)
8257 goto out;
8258
8259 if (!skb_queue_empty(&sk->sk_receive_queue))
8260 goto ready;
8261
8262 /* Socket shut down? */
8263 if (sk->sk_shutdown & RCV_SHUTDOWN)
8264 goto out;
8265
8266 /* Sequenced packets can come disconnected. If so we report the
8267 * problem.
8268 */
8269 error = -ENOTCONN;
8270
8271 /* Is there a good reason to think that we may receive some data? */
8272 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8273 goto out;
8274
8275 /* Handle signals. */
8276 if (signal_pending(current))
8277 goto interrupted;
8278
8279 /* Let another process have a go. Since we are going to sleep
8280 * anyway. Note: This may cause odd behaviors if the message
8281 * does not fit in the user's buffer, but this seems to be the
8282 * only way to honor MSG_DONTWAIT realistically.
8283 */
wangweidong048ed4b2014-01-21 15:44:11 +08008284 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008285 *timeo_p = schedule_timeout(*timeo_p);
wangweidong048ed4b2014-01-21 15:44:11 +08008286 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008287
8288ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00008289 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008290 return 0;
8291
8292interrupted:
8293 error = sock_intr_errno(*timeo_p);
8294
8295out:
Eric Dumazetaa395142010-04-20 13:03:51 +00008296 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008297 *err = error;
8298 return error;
8299}
8300
8301/* Receive a datagram.
8302 * Note: This is pretty much the same routine as in core/datagram.c
8303 * with a few changes to make lksctp work.
8304 */
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02008305struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8306 int noblock, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008307{
8308 int error;
8309 struct sk_buff *skb;
8310 long timeo;
8311
Linus Torvalds1da177e2005-04-16 15:20:36 -07008312 timeo = sock_rcvtimeo(sk, noblock);
8313
Daniel Borkmannbb333812013-06-28 19:49:40 +02008314 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8315 MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316
8317 do {
8318 /* Again only user level code calls this function,
8319 * so nothing interrupt level
8320 * will suddenly eat the receive_queue.
8321 *
8322 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00008323 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324 */
8325 if (flags & MSG_PEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008326 skb = skb_peek(&sk->sk_receive_queue);
8327 if (skb)
Reshetova, Elena63354792017-06-30 13:07:58 +03008328 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008329 } else {
Marcelo Ricardo Leitner311b2172016-04-13 19:12:29 -03008330 skb = __skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008331 }
8332
8333 if (skb)
8334 return skb;
8335
Neil Horman6736dc32005-12-02 20:30:06 -08008336 /* Caller is allowed not to check sk->sk_err before calling. */
8337 error = sock_error(sk);
8338 if (error)
8339 goto no_packet;
8340
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341 if (sk->sk_shutdown & RCV_SHUTDOWN)
8342 break;
8343
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07008344 if (sk_can_busy_loop(sk)) {
8345 sk_busy_loop(sk, noblock);
8346
8347 if (!skb_queue_empty(&sk->sk_receive_queue))
8348 continue;
8349 }
Neil Horman8465a5f2014-04-17 15:26:51 -04008350
Linus Torvalds1da177e2005-04-16 15:20:36 -07008351 /* User doesn't want to wait. */
8352 error = -EAGAIN;
8353 if (!timeo)
8354 goto no_packet;
8355 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8356
8357 return NULL;
8358
8359no_packet:
8360 *err = error;
8361 return NULL;
8362}
8363
8364/* If sndbuf has changed, wake up per association sndbuf waiters. */
8365static void __sctp_write_space(struct sctp_association *asoc)
8366{
8367 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008368
Eric Dumazetceb5d582015-11-29 20:03:11 -08008369 if (sctp_wspace(asoc) <= 0)
8370 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008371
Eric Dumazetceb5d582015-11-29 20:03:11 -08008372 if (waitqueue_active(&asoc->wait))
8373 wake_up_interruptible(&asoc->wait);
Eric Dumazeteaefd112011-02-18 03:26:36 +00008374
Eric Dumazetceb5d582015-11-29 20:03:11 -08008375 if (sctp_writeable(sk)) {
8376 struct socket_wq *wq;
8377
8378 rcu_read_lock();
8379 wq = rcu_dereference(sk->sk_wq);
8380 if (wq) {
8381 if (waitqueue_active(&wq->wait))
8382 wake_up_interruptible(&wq->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008383
8384 /* Note that we try to include the Async I/O support
8385 * here by modeling from the current TCP/UDP code.
8386 * We have not tested with it yet.
8387 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00008388 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Eric Dumazetceb5d582015-11-29 20:03:11 -08008389 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08008391 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392 }
8393}
8394
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008395static void sctp_wake_up_waiters(struct sock *sk,
8396 struct sctp_association *asoc)
8397{
8398 struct sctp_association *tmp = asoc;
8399
8400 /* We do accounting for the sndbuf space per association,
8401 * so we only need to wake our own association.
8402 */
8403 if (asoc->ep->sndbuf_policy)
8404 return __sctp_write_space(asoc);
8405
Daniel Borkmann1e1cdf82014-04-09 16:10:20 +02008406 /* If association goes down and is just flushing its
8407 * outq, then just normally notify others.
8408 */
8409 if (asoc->base.dead)
8410 return sctp_write_space(sk);
8411
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008412 /* Accounting for the sndbuf space is per socket, so we
8413 * need to wake up others, try to be fair and in case of
8414 * other associations, let them have a go first instead
8415 * of just doing a sctp_write_space() call.
8416 *
8417 * Note that we reach sctp_wake_up_waiters() only when
8418 * associations free up queued chunks, thus we are under
8419 * lock and the list of associations on a socket is
8420 * guaranteed not to change.
8421 */
8422 for (tmp = list_next_entry(tmp, asocs); 1;
8423 tmp = list_next_entry(tmp, asocs)) {
8424 /* Manually skip the head element. */
8425 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8426 continue;
8427 /* Wake up association. */
8428 __sctp_write_space(tmp);
8429 /* We've reached the end. */
8430 if (tmp == asoc)
8431 break;
8432 }
8433}
8434
Linus Torvalds1da177e2005-04-16 15:20:36 -07008435/* Do accounting for the sndbuf space.
8436 * Decrement the used sndbuf space of the corresponding association by the
8437 * data size which was just transmitted(freed).
8438 */
8439static void sctp_wfree(struct sk_buff *skb)
8440{
Daniel Borkmannf869c912014-11-20 01:54:48 +01008441 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8442 struct sctp_association *asoc = chunk->asoc;
8443 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008444
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008445 sk_mem_uncharge(sk, skb->truesize);
Xin Long605c0ac2018-10-17 03:07:50 +08008446 sk->sk_wmem_queued -= skb->truesize + sizeof(struct sctp_chunk);
8447 asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk);
8448 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk),
8449 &sk->sk_wmem_alloc));
Neil Horman4d93df02007-08-15 16:07:44 -07008450
Xin Longec2e5062018-03-14 19:05:33 +08008451 if (chunk->shkey) {
8452 struct sctp_shared_key *shkey = chunk->shkey;
8453
8454 /* refcnt == 2 and !list_empty mean after this release, it's
8455 * not being used anywhere, and it's time to notify userland
8456 * that this shkey can be freed if it's been deactivated.
8457 */
8458 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
8459 refcount_read(&shkey->refcnt) == 2) {
8460 struct sctp_ulpevent *ev;
8461
8462 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
8463 SCTP_AUTH_FREE_KEY,
8464 GFP_KERNEL);
8465 if (ev)
8466 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
8467 }
Xin Long1b1e0bc2018-03-14 19:05:30 +08008468 sctp_auth_shkey_release(chunk->shkey);
Xin Longec2e5062018-03-14 19:05:33 +08008469 }
Xin Long1b1e0bc2018-03-14 19:05:30 +08008470
Neil Horman4eb701d2005-04-28 12:02:04 -07008471 sock_wfree(skb);
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008472 sctp_wake_up_waiters(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008473
8474 sctp_association_put(asoc);
8475}
8476
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008477/* Do accounting for the receive space on the socket.
8478 * Accounting for the association is done in ulpevent.c
8479 * We set this as a destructor for the cloned data skbs so that
8480 * accounting is done at the correct time.
8481 */
8482void sctp_sock_rfree(struct sk_buff *skb)
8483{
8484 struct sock *sk = skb->sk;
8485 struct sctp_ulpevent *event = sctp_skb2event(skb);
8486
8487 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07008488
8489 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008490 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07008491 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008492 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008493}
8494
8495
Linus Torvalds1da177e2005-04-16 15:20:36 -07008496/* Helper function to wait for space in the sndbuf. */
8497static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +08008498 size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008499{
8500 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 long current_timeo = *timeo_p;
8502 DEFINE_WAIT(wait);
Xin Longa0ff6602018-01-15 17:01:36 +08008503 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504
Daniel Borkmannbb333812013-06-28 19:49:40 +02008505 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8506 *timeo_p, msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507
8508 /* Increment the association's refcnt. */
8509 sctp_association_hold(asoc);
8510
8511 /* Wait on the association specific sndbuf space. */
8512 for (;;) {
8513 prepare_to_wait_exclusive(&asoc->wait, &wait,
8514 TASK_INTERRUPTIBLE);
Xin Longca3af4d2017-11-15 16:55:54 +08008515 if (asoc->base.dead)
8516 goto do_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517 if (!*timeo_p)
8518 goto do_nonblock;
Xin Longca3af4d2017-11-15 16:55:54 +08008519 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520 goto do_error;
8521 if (signal_pending(current))
8522 goto do_interrupted;
Xin Longcd305c72018-10-17 03:07:51 +08008523 if ((int)msg_len <= sctp_wspace(asoc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008524 break;
8525
8526 /* Let another process have a go. Since we are going
8527 * to sleep anyway.
8528 */
wangweidong048ed4b2014-01-21 15:44:11 +08008529 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008531 lock_sock(sk);
Xin Longa0ff6602018-01-15 17:01:36 +08008532 if (sk != asoc->base.sk)
8533 goto do_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534
8535 *timeo_p = current_timeo;
8536 }
8537
8538out:
8539 finish_wait(&asoc->wait, &wait);
8540
8541 /* Release the association's refcnt. */
8542 sctp_association_put(asoc);
8543
8544 return err;
8545
Xin Longca3af4d2017-11-15 16:55:54 +08008546do_dead:
8547 err = -ESRCH;
8548 goto out;
8549
Linus Torvalds1da177e2005-04-16 15:20:36 -07008550do_error:
8551 err = -EPIPE;
8552 goto out;
8553
8554do_interrupted:
8555 err = sock_intr_errno(*timeo_p);
8556 goto out;
8557
8558do_nonblock:
8559 err = -EAGAIN;
8560 goto out;
8561}
8562
David S. Miller676d2362014-04-11 16:15:36 -04008563void sctp_data_ready(struct sock *sk)
Wei Yongjun561b1732010-04-28 08:47:18 +00008564{
David S. Miller7ef52732010-05-02 21:43:40 -07008565 struct socket_wq *wq;
8566
8567 rcu_read_lock();
8568 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08008569 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008570 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8571 EPOLLRDNORM | EPOLLRDBAND);
Wei Yongjun561b1732010-04-28 08:47:18 +00008572 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07008573 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00008574}
8575
Linus Torvalds1da177e2005-04-16 15:20:36 -07008576/* If socket sndbuf has changed, wake up all per association waiters. */
8577void sctp_write_space(struct sock *sk)
8578{
8579 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008580
8581 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07008582 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583 __sctp_write_space(asoc);
8584 }
8585}
8586
8587/* Is there any sndbuf space available on the socket?
8588 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08008589 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590 * associations on the same socket. For a UDP-style socket with
8591 * multiple associations, it is possible for it to be "unwriteable"
8592 * prematurely. I assume that this is acceptable because
8593 * a premature "unwriteable" is better than an accidental "writeable" which
8594 * would cause an unwanted block under certain circumstances. For the 1-1
8595 * UDP-style sockets or TCP-style sockets, this code should work.
8596 * - Daisy
8597 */
Xin Longcd305c72018-10-17 03:07:51 +08008598static bool sctp_writeable(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008599{
Xin Longcd305c72018-10-17 03:07:51 +08008600 return sk->sk_sndbuf > sk->sk_wmem_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601}
8602
8603/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8604 * returns immediately with EINPROGRESS.
8605 */
8606static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8607{
8608 struct sock *sk = asoc->base.sk;
8609 int err = 0;
8610 long current_timeo = *timeo_p;
8611 DEFINE_WAIT(wait);
8612
Daniel Borkmannbb333812013-06-28 19:49:40 +02008613 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008614
8615 /* Increment the association's refcnt. */
8616 sctp_association_hold(asoc);
8617
8618 for (;;) {
8619 prepare_to_wait_exclusive(&asoc->wait, &wait,
8620 TASK_INTERRUPTIBLE);
8621 if (!*timeo_p)
8622 goto do_nonblock;
8623 if (sk->sk_shutdown & RCV_SHUTDOWN)
8624 break;
8625 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
8626 asoc->base.dead)
8627 goto do_error;
8628 if (signal_pending(current))
8629 goto do_interrupted;
8630
8631 if (sctp_state(asoc, ESTABLISHED))
8632 break;
8633
8634 /* Let another process have a go. Since we are going
8635 * to sleep anyway.
8636 */
wangweidong048ed4b2014-01-21 15:44:11 +08008637 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008638 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008639 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640
8641 *timeo_p = current_timeo;
8642 }
8643
8644out:
8645 finish_wait(&asoc->wait, &wait);
8646
8647 /* Release the association's refcnt. */
8648 sctp_association_put(asoc);
8649
8650 return err;
8651
8652do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08008653 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008654 err = -ETIMEDOUT;
8655 else
8656 err = -ECONNREFUSED;
8657 goto out;
8658
8659do_interrupted:
8660 err = sock_intr_errno(*timeo_p);
8661 goto out;
8662
8663do_nonblock:
8664 err = -EINPROGRESS;
8665 goto out;
8666}
8667
8668static int sctp_wait_for_accept(struct sock *sk, long timeo)
8669{
8670 struct sctp_endpoint *ep;
8671 int err = 0;
8672 DEFINE_WAIT(wait);
8673
8674 ep = sctp_sk(sk)->ep;
8675
8676
8677 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00008678 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008679 TASK_INTERRUPTIBLE);
8680
8681 if (list_empty(&ep->asocs)) {
wangweidong048ed4b2014-01-21 15:44:11 +08008682 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008683 timeo = schedule_timeout(timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008684 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008685 }
8686
8687 err = -EINVAL;
8688 if (!sctp_sstate(sk, LISTENING))
8689 break;
8690
8691 err = 0;
8692 if (!list_empty(&ep->asocs))
8693 break;
8694
8695 err = sock_intr_errno(timeo);
8696 if (signal_pending(current))
8697 break;
8698
8699 err = -EAGAIN;
8700 if (!timeo)
8701 break;
8702 }
8703
Eric Dumazetaa395142010-04-20 13:03:51 +00008704 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008705
8706 return err;
8707}
8708
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02008709static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710{
8711 DEFINE_WAIT(wait);
8712
8713 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00008714 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008715 if (list_empty(&sctp_sk(sk)->ep->asocs))
8716 break;
wangweidong048ed4b2014-01-21 15:44:11 +08008717 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718 timeout = schedule_timeout(timeout);
wangweidong048ed4b2014-01-21 15:44:11 +08008719 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008720 } while (!signal_pending(current) && timeout);
8721
Eric Dumazetaa395142010-04-20 13:03:51 +00008722 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008723}
8724
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008725static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
8726{
8727 struct sk_buff *frag;
8728
8729 if (!skb->data_len)
8730 goto done;
8731
8732 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07008733 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008734 sctp_skb_set_owner_r_frag(frag, sk);
8735
8736done:
8737 sctp_skb_set_owner_r(skb, sk);
8738}
8739
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008740void sctp_copy_sock(struct sock *newsk, struct sock *sk,
8741 struct sctp_association *asoc)
8742{
8743 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08008744 struct inet_sock *newinet;
Richard Haines2277c7c2018-02-13 20:56:24 +00008745 struct sctp_sock *sp = sctp_sk(sk);
8746 struct sctp_endpoint *ep = sp->ep;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008747
8748 newsk->sk_type = sk->sk_type;
8749 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
8750 newsk->sk_flags = sk->sk_flags;
Marcelo Ricardo Leitner50a5ffb2015-12-04 15:14:05 -02008751 newsk->sk_tsflags = sk->sk_tsflags;
Tom Herbert28448b82014-05-23 08:47:19 -07008752 newsk->sk_no_check_tx = sk->sk_no_check_tx;
8753 newsk->sk_no_check_rx = sk->sk_no_check_rx;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008754 newsk->sk_reuse = sk->sk_reuse;
Xin Longb0e9a2f2018-06-28 15:31:00 +08008755 sctp_sk(newsk)->reuse = sp->reuse;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008756
8757 newsk->sk_shutdown = sk->sk_shutdown;
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02008758 newsk->sk_destruct = sctp_destruct_sock;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008759 newsk->sk_family = sk->sk_family;
8760 newsk->sk_protocol = IPPROTO_SCTP;
8761 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
8762 newsk->sk_sndbuf = sk->sk_sndbuf;
8763 newsk->sk_rcvbuf = sk->sk_rcvbuf;
8764 newsk->sk_lingertime = sk->sk_lingertime;
8765 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
8766 newsk->sk_sndtimeo = sk->sk_sndtimeo;
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03008767 newsk->sk_rxhash = sk->sk_rxhash;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008768
8769 newinet = inet_sk(newsk);
8770
8771 /* Initialize sk's sport, dport, rcv_saddr and daddr for
8772 * getsockname() and getpeername()
8773 */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008774 newinet->inet_sport = inet->inet_sport;
8775 newinet->inet_saddr = inet->inet_saddr;
8776 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
8777 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008778 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008779 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008780
8781 newinet->uc_ttl = inet->uc_ttl;
8782 newinet->mc_loop = 1;
8783 newinet->mc_ttl = 1;
8784 newinet->mc_index = 0;
8785 newinet->mc_list = NULL;
Marcelo Ricardo Leitner01ce63c2015-12-04 15:14:04 -02008786
8787 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
8788 net_enable_timestamp();
Marcelo Ricardo Leitner3538a5c2015-12-23 16:44:09 -02008789
Richard Haines2277c7c2018-02-13 20:56:24 +00008790 /* Set newsk security attributes from orginal sk and connection
8791 * security attribute from ep.
8792 */
8793 security_sctp_sk_clone(ep, sk, newsk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008794}
8795
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008796static inline void sctp_copy_descendant(struct sock *sk_to,
8797 const struct sock *sk_from)
8798{
8799 int ancestor_size = sizeof(struct inet_sock) +
8800 sizeof(struct sctp_sock) -
8801 offsetof(struct sctp_sock, auto_asconf_list);
8802
8803 if (sk_from->sk_family == PF_INET6)
8804 ancestor_size += sizeof(struct ipv6_pinfo);
8805
8806 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
8807}
8808
Linus Torvalds1da177e2005-04-16 15:20:36 -07008809/* Populate the fields of the newsk from the oldsk and migrate the assoc
8810 * and its messages to the newsk.
8811 */
8812static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
8813 struct sctp_association *assoc,
Xin Longb7ef2612017-08-11 10:23:50 +08008814 enum sctp_socket_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008815{
8816 struct sctp_sock *oldsp = sctp_sk(oldsk);
8817 struct sctp_sock *newsp = sctp_sk(newsk);
8818 struct sctp_bind_bucket *pp; /* hash list port iterator */
8819 struct sctp_endpoint *newep = newsp->ep;
8820 struct sk_buff *skb, *tmp;
8821 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08008822 struct sctp_bind_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008823
8824 /* Migrate socket buffer sizes and all the socket level options to the
8825 * new socket.
8826 */
8827 newsk->sk_sndbuf = oldsk->sk_sndbuf;
8828 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
8829 /* Brute force copy old sctp opt. */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008830 sctp_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008831
8832 /* Restore the ep value that was overwritten with the above structure
8833 * copy.
8834 */
8835 newsp->ep = newep;
8836 newsp->hmac = NULL;
8837
8838 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008839 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
8840 inet_sk(oldsk)->inet_num)];
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008841 spin_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008842 pp = sctp_sk(oldsk)->bind_hash;
8843 sk_add_bind_node(newsk, &pp->owner);
8844 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008845 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008846 spin_unlock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008847
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008848 /* Copy the bind_addr list from the original endpoint to the new
8849 * endpoint so that we can handle restarts properly
8850 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08008851 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
8852 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008853
Linus Torvalds1da177e2005-04-16 15:20:36 -07008854 /* Move any messages in the old socket's receive queue that are for the
8855 * peeled off association to the new socket's receive queue.
8856 */
8857 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
8858 event = sctp_skb2event(skb);
8859 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008860 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008861 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008862 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008863 }
8864 }
8865
8866 /* Clean up any messages pending delivery due to partial
8867 * delivery. Three cases:
8868 * 1) No partial deliver; no work.
8869 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
8870 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
8871 */
8872 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008873 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008875 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008876 struct sk_buff_head *queue;
8877
8878 /* Decide which queue to move pd_lobby skbs to. */
8879 if (assoc->ulpq.pd_mode) {
8880 queue = &newsp->pd_lobby;
8881 } else
8882 queue = &newsk->sk_receive_queue;
8883
8884 /* Walk through the pd_lobby, looking for skbs that
8885 * need moved to the new socket.
8886 */
8887 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
8888 event = sctp_skb2event(skb);
8889 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008890 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008891 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008892 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008893 }
8894 }
8895
8896 /* Clear up any skbs waiting for the partial
8897 * delivery to finish.
8898 */
8899 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008900 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008901
8902 }
8903
Xin Long13228232017-12-08 21:04:09 +08008904 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008905
Linus Torvalds1da177e2005-04-16 15:20:36 -07008906 /* Set the type of socket to indicate that it is peeled off from the
8907 * original UDP-style socket or created with the accept() call on a
8908 * TCP-style socket..
8909 */
8910 newsp->type = type;
8911
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008912 /* Mark the new socket "in-use" by the user so that any packets
8913 * that may arrive on the association after we've moved it are
8914 * queued to the backlog. This prevents a potential race between
8915 * backlog processing on the old socket and new-packet processing
8916 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07008917 *
8918 * The caller has just allocated newsk so we can guarantee that other
8919 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008920 */
Zach Brown5131a182007-06-22 15:14:46 -07008921 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Xin Longd04adf12017-10-28 02:13:29 +08008922 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008923 sctp_assoc_migrate(assoc, newsk);
Xin Longd04adf12017-10-28 02:13:29 +08008924 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925
8926 /* If the association on the newsk is already closed before accept()
8927 * is called, set RCV_SHUTDOWN flag.
8928 */
Xin Longd46e4162016-06-09 22:48:18 +08008929 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08008930 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931 newsk->sk_shutdown |= RCV_SHUTDOWN;
Xin Longd46e4162016-06-09 22:48:18 +08008932 } else {
Yafang Shaocbabf462017-12-20 11:12:54 +08008933 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
Xin Longd46e4162016-06-09 22:48:18 +08008934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008935
wangweidong048ed4b2014-01-21 15:44:11 +08008936 release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008937}
8938
Neil Horman4d93df02007-08-15 16:07:44 -07008939
Linus Torvalds1da177e2005-04-16 15:20:36 -07008940/* This proto struct describes the ULP interface for SCTP. */
8941struct proto sctp_prot = {
8942 .name = "SCTP",
8943 .owner = THIS_MODULE,
8944 .close = sctp_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945 .disconnect = sctp_disconnect,
8946 .accept = sctp_accept,
8947 .ioctl = sctp_ioctl,
8948 .init = sctp_init_sock,
8949 .destroy = sctp_destroy_sock,
8950 .shutdown = sctp_shutdown,
8951 .setsockopt = sctp_setsockopt,
8952 .getsockopt = sctp_getsockopt,
8953 .sendmsg = sctp_sendmsg,
8954 .recvmsg = sctp_recvmsg,
8955 .bind = sctp_bind,
8956 .backlog_rcv = sctp_backlog_rcv,
8957 .hash = sctp_hash,
8958 .unhash = sctp_unhash,
8959 .get_port = sctp_get_port,
8960 .obj_size = sizeof(struct sctp_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07008961 .useroffset = offsetof(struct sctp_sock, subscribe),
8962 .usersize = offsetof(struct sctp_sock, initmsg) -
8963 offsetof(struct sctp_sock, subscribe) +
8964 sizeof_field(struct sctp_sock, initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07008965 .sysctl_mem = sysctl_sctp_mem,
8966 .sysctl_rmem = sysctl_sctp_rmem,
8967 .sysctl_wmem = sysctl_sctp_wmem,
8968 .memory_pressure = &sctp_memory_pressure,
8969 .enter_memory_pressure = sctp_enter_memory_pressure,
8970 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008971 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008972};
8973
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008974#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08008975
Eric Dumazet602dd622015-12-01 07:20:07 -08008976#include <net/transp_v6.h>
8977static void sctp_v6_destroy_sock(struct sock *sk)
8978{
8979 sctp_destroy_sock(sk);
8980 inet6_destroy_sock(sk);
8981}
8982
Linus Torvalds1da177e2005-04-16 15:20:36 -07008983struct proto sctpv6_prot = {
8984 .name = "SCTPv6",
8985 .owner = THIS_MODULE,
8986 .close = sctp_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008987 .disconnect = sctp_disconnect,
8988 .accept = sctp_accept,
8989 .ioctl = sctp_ioctl,
8990 .init = sctp_init_sock,
Eric Dumazet602dd622015-12-01 07:20:07 -08008991 .destroy = sctp_v6_destroy_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008992 .shutdown = sctp_shutdown,
8993 .setsockopt = sctp_setsockopt,
8994 .getsockopt = sctp_getsockopt,
8995 .sendmsg = sctp_sendmsg,
8996 .recvmsg = sctp_recvmsg,
8997 .bind = sctp_bind,
8998 .backlog_rcv = sctp_backlog_rcv,
8999 .hash = sctp_hash,
9000 .unhash = sctp_unhash,
9001 .get_port = sctp_get_port,
9002 .obj_size = sizeof(struct sctp6_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07009003 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
9004 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
9005 offsetof(struct sctp6_sock, sctp.subscribe) +
9006 sizeof_field(struct sctp6_sock, sctp.initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07009007 .sysctl_mem = sysctl_sctp_mem,
9008 .sysctl_rmem = sysctl_sctp_rmem,
9009 .sysctl_wmem = sysctl_sctp_wmem,
9010 .memory_pressure = &sctp_memory_pressure,
9011 .enter_memory_pressure = sctp_enter_memory_pressure,
9012 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08009013 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009014};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00009015#endif /* IS_ENABLED(CONFIG_IPV6) */