blob: c6f29505c34d62ad37533ef58ec6b22bcbe0e68a [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
8 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05009 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
13 *
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
22 * any later version.
23 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050024 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080031 * along with GNU CC; see the file COPYING. If not, see
32 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
34 * Please send any bug reports or fixes you make to the
35 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020036 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * Written or modified by:
39 * La Monte H.P. Yarroll <piggy@acm.org>
40 * Narasimha Budihal <narsi@refcode.org>
41 * Karl Knutson <karl@athena.chicago.il.us>
42 * Jon Grimm <jgrimm@us.ibm.com>
43 * Xingang Guo <xingang.guo@intel.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Sridhar Samudrala <samudrala@us.ibm.com>
46 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
47 * Ardelle Fan <ardelle.fan@intel.com>
48 * Ryan Layer <rmlayer@us.ibm.com>
49 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
50 * Kevin Gao <kevin.gao@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
52
Joe Perches145ce502010-08-24 13:21:08 +000053#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
Herbert Xu5821c762016-01-24 21:20:12 +080055#include <crypto/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/types.h>
57#include <linux/kernel.h>
58#include <linux/wait.h>
59#include <linux/time.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010060#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/ip.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080062#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/fcntl.h>
64#include <linux/poll.h>
65#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090066#include <linux/slab.h>
Al Viro56b31d12012-08-18 00:25:51 -040067#include <linux/file.h>
Daniel Borkmannffd59392014-02-17 12:11:11 +010068#include <linux/compat.h>
NeilBrown0eb71a92018-06-18 12:52:50 +100069#include <linux/rhashtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71#include <net/ip.h>
72#include <net/icmp.h>
73#include <net/route.h>
74#include <net/ipv6.h>
75#include <net/inet_common.h>
Neil Horman8465a5f2014-04-17 15:26:51 -040076#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#include <linux/socket.h> /* for sa_family_t */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040079#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <net/sock.h>
81#include <net/sctp/sctp.h>
82#include <net/sctp/sm.h>
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -030083#include <net/sctp/stream_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* Forward declarations for internal helper functions. */
86static int sctp_writeable(struct sock *sk);
87static void sctp_wfree(struct sk_buff *skb);
Xin Longcea0cc82017-11-15 16:57:26 +080088static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +080089 size_t msg_len);
wangweidong26ac8e52013-12-23 12:16:51 +080090static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
92static int sctp_wait_for_accept(struct sock *sk, long timeo);
93static void sctp_wait_for_close(struct sock *sk, long timeo);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +020094static void sctp_destruct_sock(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
96 union sctp_addr *addr, int len);
97static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
98static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
99static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
100static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
101static int sctp_send_asconf(struct sctp_association *asoc,
102 struct sctp_chunk *chunk);
103static int sctp_do_bind(struct sock *, union sctp_addr *, int);
104static int sctp_autobind(struct sock *sk);
Xin Longb7ef2612017-08-11 10:23:50 +0800105static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
106 struct sctp_association *assoc,
107 enum sctp_socket_type type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Eric Dumazet06044752017-06-07 13:29:12 -0700109static unsigned long sctp_memory_pressure;
Eric Dumazet8d987e52010-11-09 23:24:26 +0000110static atomic_long_t sctp_memory_allocated;
Eric Dumazet17483762008-11-25 21:16:35 -0800111struct percpu_counter sctp_sockets_allocated;
Neil Horman4d93df02007-08-15 16:07:44 -0700112
Pavel Emelyanov5c52ba12008-07-16 20:28:10 -0700113static void sctp_enter_memory_pressure(struct sock *sk)
Neil Horman4d93df02007-08-15 16:07:44 -0700114{
115 sctp_memory_pressure = 1;
116}
117
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* Get the sndbuf space available at the time on the association. */
120static inline int sctp_wspace(struct sctp_association *asoc)
121{
Neil Horman4d93df02007-08-15 16:07:44 -0700122 int amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Neil Horman4d93df02007-08-15 16:07:44 -0700124 if (asoc->ep->sndbuf_policy)
125 amt = asoc->sndbuf_used;
126 else
Eric Dumazet31e6d362009-06-17 19:05:41 -0700127 amt = sk_wmem_alloc_get(asoc->base.sk);
Neil Horman4d93df02007-08-15 16:07:44 -0700128
129 if (amt >= asoc->base.sk->sk_sndbuf) {
130 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
131 amt = 0;
132 else {
133 amt = sk_stream_wspace(asoc->base.sk);
134 if (amt < 0)
135 amt = 0;
136 }
Neil Horman4eb701d2005-04-28 12:02:04 -0700137 } else {
Neil Horman4d93df02007-08-15 16:07:44 -0700138 amt = asoc->base.sk->sk_sndbuf - amt;
Neil Horman4eb701d2005-04-28 12:02:04 -0700139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return amt;
141}
142
143/* Increment the used sndbuf space count of the corresponding association by
144 * the size of the outgoing data chunk.
145 * Also, set the skb destructor for sndbuf accounting later.
146 *
147 * Since it is always 1-1 between chunk and skb, and also a new skb is always
148 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
149 * destructor in the data chunk skb for the purpose of the sndbuf space
150 * tracking.
151 */
152static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
153{
154 struct sctp_association *asoc = chunk->asoc;
155 struct sock *sk = asoc->base.sk;
156
157 /* The sndbuf space is tracked per association. */
158 sctp_association_hold(asoc);
159
Xin Long1b1e0bc2018-03-14 19:05:30 +0800160 if (chunk->shkey)
161 sctp_auth_shkey_hold(chunk->shkey);
162
Neil Horman4eb701d2005-04-28 12:02:04 -0700163 skb_set_owner_w(chunk->skb, sk);
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 chunk->skb->destructor = sctp_wfree;
166 /* Save the chunk pointer in skb for sctp_wfree to use later. */
Daniel Borkmannf869c912014-11-20 01:54:48 +0100167 skb_shinfo(chunk->skb)->destructor_arg = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Reshetova, Elena14afee42017-06-30 13:08:00 +0300169 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Xin Long605c0ac2018-10-17 03:07:50 +0800170 asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk);
171 sk->sk_wmem_queued += chunk->skb->truesize + sizeof(struct sctp_chunk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800172 sk_mem_charge(sk, chunk->skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Xin Longd04adf12017-10-28 02:13:29 +0800175static void sctp_clear_owner_w(struct sctp_chunk *chunk)
176{
177 skb_orphan(chunk->skb);
178}
179
180static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
181 void (*cb)(struct sctp_chunk *))
182
183{
184 struct sctp_outq *q = &asoc->outqueue;
185 struct sctp_transport *t;
186 struct sctp_chunk *chunk;
187
188 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
189 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
190 cb(chunk);
191
Xin Longa8dd3972017-11-26 20:56:07 +0800192 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800193 cb(chunk);
194
Xin Longa8dd3972017-11-26 20:56:07 +0800195 list_for_each_entry(chunk, &q->sacked, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800196 cb(chunk);
197
Xin Longa8dd3972017-11-26 20:56:07 +0800198 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800199 cb(chunk);
200
201 list_for_each_entry(chunk, &q->out_chunk_list, list)
202 cb(chunk);
203}
204
Xin Long13228232017-12-08 21:04:09 +0800205static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
206 void (*cb)(struct sk_buff *, struct sock *))
207
208{
209 struct sk_buff *skb, *tmp;
210
211 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
212 cb(skb, sk);
213
214 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
215 cb(skb, sk);
216
217 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
218 cb(skb, sk);
219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/* Verify that this is a valid address. */
222static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
223 int len)
224{
225 struct sctp_af *af;
226
227 /* Verify basic sockaddr. */
228 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
229 if (!af)
230 return -EINVAL;
231
232 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700233 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return -EINVAL;
235
236 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
237 return -EINVAL;
238
239 return 0;
240}
241
242/* Look up the association by its id. If this is not a UDP-style
243 * socket, the ID field is always ignored.
244 */
245struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
246{
247 struct sctp_association *asoc = NULL;
248
249 /* If this is not a UDP-style socket, assoc id should be ignored. */
250 if (!sctp_style(sk, UDP)) {
251 /* Return NULL if the socket state is not ESTABLISHED. It
252 * could be a TCP-style listening socket or a socket which
253 * hasn't yet called connect() to establish an association.
254 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -0300255 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return NULL;
257
258 /* Get the first and the only association from the list. */
259 if (!list_empty(&sctp_sk(sk)->ep->asocs))
260 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
261 struct sctp_association, asocs);
262 return asoc;
263 }
264
265 /* Otherwise this is a UDP-style socket. */
266 if (!id || (id == (sctp_assoc_t)-1))
267 return NULL;
268
269 spin_lock_bh(&sctp_assocs_id_lock);
270 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
271 spin_unlock_bh(&sctp_assocs_id_lock);
272
273 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
274 return NULL;
275
276 return asoc;
277}
278
279/* Look up the transport from an address and an assoc id. If both address and
280 * id are specified, the associations matching the address and the id should be
281 * the same.
282 */
283static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
284 struct sockaddr_storage *addr,
285 sctp_assoc_t id)
286{
287 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
Xin Long6f29a132017-01-24 14:01:53 +0800288 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 union sctp_addr *laddr = (union sctp_addr *)addr;
Xin Long6f29a132017-01-24 14:01:53 +0800290 struct sctp_transport *transport;
291
Xin Long912964e2017-02-07 20:56:08 +0800292 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
Xin Long6f29a132017-01-24 14:01:53 +0800293 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800296 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 if (!addr_asoc)
300 return NULL;
301
302 id_asoc = sctp_id2assoc(sk, id);
303 if (id_asoc && (id_asoc != addr_asoc))
304 return NULL;
305
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600306 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 (union sctp_addr *)addr);
308
309 return transport;
310}
311
312/* API 3.1.2 bind() - UDP Style Syntax
313 * The syntax of bind() is,
314 *
315 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
316 *
317 * sd - the socket descriptor returned by socket().
318 * addr - the address structure (struct sockaddr_in or struct
319 * sockaddr_in6 [RFC 2553]),
320 * addr_len - the size of the address structure.
321 */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200322static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 int retval = 0;
325
wangweidong048ed4b2014-01-21 15:44:11 +0800326 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Daniel Borkmannbb333812013-06-28 19:49:40 +0200328 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
329 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* Disallow binding twice. */
332 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700333 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 addr_len);
335 else
336 retval = -EINVAL;
337
wangweidong048ed4b2014-01-21 15:44:11 +0800338 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 return retval;
341}
342
343static long sctp_get_port_local(struct sock *, union sctp_addr *);
344
345/* Verify this is a valid sockaddr. */
346static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
347 union sctp_addr *addr, int len)
348{
349 struct sctp_af *af;
350
351 /* Check minimum size. */
352 if (len < sizeof (struct sockaddr))
353 return NULL;
354
Xin Longc5006b82018-01-15 17:02:00 +0800355 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
356 return NULL;
357
Eric Dumazet81e98372018-04-08 07:52:08 -0700358 if (addr->sa.sa_family == AF_INET6) {
359 if (len < SIN6_LEN_RFC2133)
360 return NULL;
361 /* V4 mapped address are really of AF_INET family */
362 if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
363 !opt->pf->af_supported(AF_INET, opt))
364 return NULL;
365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /* If we get this far, af is valid. */
368 af = sctp_get_af_specific(addr->sa.sa_family);
369
370 if (len < af->sockaddr_len)
371 return NULL;
372
373 return af;
374}
375
376/* Bind a local address either to an endpoint or to an association. */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200377static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Eric W. Biederman35946982012-11-16 03:03:12 +0000379 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 struct sctp_sock *sp = sctp_sk(sk);
381 struct sctp_endpoint *ep = sp->ep;
382 struct sctp_bind_addr *bp = &ep->base.bind_addr;
383 struct sctp_af *af;
384 unsigned short snum;
385 int ret = 0;
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /* Common sockaddr verification. */
388 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700389 if (!af) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200390 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
391 __func__, sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700393 }
394
395 snum = ntohs(addr->v4.sin_port);
396
Daniel Borkmannbb333812013-06-28 19:49:40 +0200397 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
398 __func__, sk, &addr->sa, bp->port, snum, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /* PF specific bind() address verification. */
401 if (!sp->pf->bind_verify(sp, addr))
402 return -EADDRNOTAVAIL;
403
Vlad Yasevich8b358052007-05-15 17:14:58 -0400404 /* We must either be unbound, or bind to the same port.
405 * It's OK to allow 0 ports if we are already bound.
406 * We'll just inhert an already bound port in this case
407 */
408 if (bp->port) {
409 if (!snum)
410 snum = bp->port;
411 else if (snum != bp->port) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200412 pr_debug("%s: new port %d doesn't match existing port "
413 "%d\n", __func__, snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400414 return -EINVAL;
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
Krister Johansen4548b682017-01-20 17:49:11 -0800418 if (snum && snum < inet_prot_sock(net) &&
Eric W. Biederman35946982012-11-16 03:03:12 +0000419 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return -EACCES;
421
Vlad Yasevich4e540642008-07-18 23:06:32 -0700422 /* See if the address matches any of the addresses we may have
423 * already bound before checking against other endpoints.
424 */
425 if (sctp_bind_addr_match(bp, addr, sp))
426 return -EINVAL;
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* Make sure we are allowed to bind here.
429 * The function sctp_get_port_local() does duplicate address
430 * detection.
431 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900432 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if ((ret = sctp_get_port_local(sk, addr))) {
Vlad Yasevich4e540642008-07-18 23:06:32 -0700434 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
437 /* Refresh ephemeral port. */
438 if (!bp->port)
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000439 bp->port = inet_sk(sk)->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Vlad Yasevich559cf712007-09-16 16:03:28 -0700441 /* Add the address to the bind address list.
442 * Use GFP_ATOMIC since BHs will be disabled.
443 */
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300444 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
445 SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /* Copy back into socket for getsockname() use. */
448 if (!ret) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000449 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600450 sp->pf->to_sk_saddr(addr, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452
453 return ret;
454}
455
456 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
457 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900458 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900460 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900462 * subsequent ASCONF. Note this restriction binds each side, so at any
463 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * from each endpoint).
465 */
466static int sctp_send_asconf(struct sctp_association *asoc,
467 struct sctp_chunk *chunk)
468{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000469 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int retval = 0;
471
472 /* If there is an outstanding ASCONF chunk, queue it for later
473 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900474 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700476 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900477 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479
480 /* Hold the chunk until an ASCONF_ACK is received. */
481 sctp_chunk_hold(chunk);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000482 retval = sctp_primitive_ASCONF(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (retval)
484 sctp_chunk_free(chunk);
485 else
486 asoc->addip_last_asconf = chunk;
487
488out:
489 return retval;
490}
491
492/* Add a list of addresses as bind addresses to local endpoint or
493 * association.
494 *
495 * Basically run through each address specified in the addrs/addrcnt
496 * array/length pair, determine if it is IPv6 or IPv4 and call
497 * sctp_do_bind() on it.
498 *
499 * If any of them fails, then the operation will be reversed and the
500 * ones that were added will be removed.
501 *
502 * Only sctp_setsockopt_bindx() is supposed to call this function.
503 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200504static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 int cnt;
507 int retval = 0;
508 void *addr_buf;
509 struct sockaddr *sa_addr;
510 struct sctp_af *af;
511
Daniel Borkmannbb333812013-06-28 19:49:40 +0200512 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
513 addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 addr_buf = addrs;
516 for (cnt = 0; cnt < addrcnt; cnt++) {
517 /* The list may contain either IPv4 or IPv6 address;
518 * determine the address length for walking thru the list.
519 */
Joe Perchesea110732011-06-13 16:21:26 +0000520 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 af = sctp_get_af_specific(sa_addr->sa_family);
522 if (!af) {
523 retval = -EINVAL;
524 goto err_bindx_add;
525 }
526
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900527 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 af->sockaddr_len);
529
530 addr_buf += af->sockaddr_len;
531
532err_bindx_add:
533 if (retval < 0) {
534 /* Failed. Cleanup the ones that have been added */
535 if (cnt > 0)
536 sctp_bindx_rem(sk, addrs, cnt);
537 return retval;
538 }
539 }
540
541 return retval;
542}
543
544/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
545 * associations that are part of the endpoint indicating that a list of local
546 * addresses are added to the endpoint.
547 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900548 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * association, we do not send the chunk for that association. But it will not
550 * affect other associations.
551 *
552 * Only sctp_setsockopt_bindx() is supposed to call this function.
553 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900554static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct sockaddr *addrs,
556 int addrcnt)
557{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000558 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct sctp_sock *sp;
560 struct sctp_endpoint *ep;
561 struct sctp_association *asoc;
562 struct sctp_bind_addr *bp;
563 struct sctp_chunk *chunk;
564 struct sctp_sockaddr_entry *laddr;
565 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700566 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 void *addr_buf;
568 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct list_head *p;
570 int i;
571 int retval = 0;
572
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000573 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return retval;
575
576 sp = sctp_sk(sk);
577 ep = sp->ep;
578
Daniel Borkmannbb333812013-06-28 19:49:40 +0200579 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
580 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700582 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (!asoc->peer.asconf_capable)
584 continue;
585
586 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
587 continue;
588
589 if (!sctp_state(asoc, ESTABLISHED))
590 continue;
591
592 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900593 * in the bind address list of the association. If so,
594 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * other associations.
596 */
597 addr_buf = addrs;
598 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000599 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 af = sctp_get_af_specific(addr->v4.sin_family);
601 if (!af) {
602 retval = -EINVAL;
603 goto out;
604 }
605
606 if (sctp_assoc_lookup_laddr(asoc, addr))
607 break;
608
609 addr_buf += af->sockaddr_len;
610 }
611 if (i < addrcnt)
612 continue;
613
Vlad Yasevich559cf712007-09-16 16:03:28 -0700614 /* Use the first valid address in bind addr list of
615 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 bp = &asoc->base.bind_addr;
618 p = bp->address_list.next;
619 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800620 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 addrcnt, SCTP_PARAM_ADD_IP);
622 if (!chunk) {
623 retval = -ENOMEM;
624 goto out;
625 }
626
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700627 /* Add the new addresses to the bind address list with
628 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700630 addr_buf = addrs;
631 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000632 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700633 af = sctp_get_af_specific(addr->v4.sin_family);
634 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800635 retval = sctp_add_bind_addr(bp, &saveaddr,
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300636 sizeof(saveaddr),
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800637 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700638 addr_buf += af->sockaddr_len;
639 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900640 if (asoc->src_out_of_asoc_ok) {
641 struct sctp_transport *trans;
642
643 list_for_each_entry(trans,
644 &asoc->peer.transport_addr_list, transports) {
Michio Honda8a07eb02011-04-26 20:19:36 +0900645 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
646 2*asoc->pathmtu, 4380));
647 trans->ssthresh = asoc->peer.i.a_rwnd;
648 trans->rto = asoc->rto_initial;
Michele Baldessari196d6752012-12-01 04:49:42 +0000649 sctp_max_rto(asoc, trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900650 trans->rtt = trans->srtt = trans->rttvar = 0;
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300651 /* Clear the source and route cache */
Michio Honda8a07eb02011-04-26 20:19:36 +0900652 sctp_transport_route(trans, NULL,
Marcelo Ricardo Leitner6e91b572018-04-26 16:58:59 -0300653 sctp_sk(asoc->base.sk));
Michio Honda8a07eb02011-04-26 20:19:36 +0900654 }
655 }
656 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
659out:
660 return retval;
661}
662
663/* Remove a list of addresses from bind addresses list. Do not remove the
664 * last address.
665 *
666 * Basically run through each address specified in the addrs/addrcnt
667 * array/length pair, determine if it is IPv6 or IPv4 and call
668 * sctp_del_bind() on it.
669 *
670 * If any of them fails, then the operation will be reversed and the
671 * ones that were removed will be added back.
672 *
673 * At least one address has to be left; if only one address is
674 * available, the operation will return -EBUSY.
675 *
676 * Only sctp_setsockopt_bindx() is supposed to call this function.
677 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200678static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct sctp_sock *sp = sctp_sk(sk);
681 struct sctp_endpoint *ep = sp->ep;
682 int cnt;
683 struct sctp_bind_addr *bp = &ep->base.bind_addr;
684 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800686 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct sctp_af *af;
688
Daniel Borkmannbb333812013-06-28 19:49:40 +0200689 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
690 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 addr_buf = addrs;
693 for (cnt = 0; cnt < addrcnt; cnt++) {
694 /* If the bind address list is empty or if there is only one
695 * bind address, there is nothing more to be removed (we need
696 * at least one address here).
697 */
698 if (list_empty(&bp->address_list) ||
699 (sctp_list_single_entry(&bp->address_list))) {
700 retval = -EBUSY;
701 goto err_bindx_rem;
702 }
703
Joe Perchesea110732011-06-13 16:21:26 +0000704 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800705 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (!af) {
707 retval = -EINVAL;
708 goto err_bindx_rem;
709 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700710
711 if (!af->addr_valid(sa_addr, sp, NULL)) {
712 retval = -EADDRNOTAVAIL;
713 goto err_bindx_rem;
714 }
715
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000716 if (sa_addr->v4.sin_port &&
717 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 retval = -EINVAL;
719 goto err_bindx_rem;
720 }
721
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000722 if (!sa_addr->v4.sin_port)
723 sa_addr->v4.sin_port = htons(bp->port);
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /* FIXME - There is probably a need to check if sk->sk_saddr and
726 * sk->sk_rcv_addr are currently set to one of the addresses to
727 * be removed. This is something which needs to be looked into
728 * when we are fixing the outstanding issues with multi-homing
729 * socket routing and failover schemes. Refer to comments in
730 * sctp_do_bind(). -daisy
731 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400732 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 addr_buf += af->sockaddr_len;
735err_bindx_rem:
736 if (retval < 0) {
737 /* Failed. Add the ones that has been removed back */
738 if (cnt > 0)
739 sctp_bindx_add(sk, addrs, cnt);
740 return retval;
741 }
742 }
743
744 return retval;
745}
746
747/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
748 * the associations that are part of the endpoint indicating that a list of
749 * local addresses are removed from the endpoint.
750 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900751 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * association, we do not send the chunk for that association. But it will not
753 * affect other associations.
754 *
755 * Only sctp_setsockopt_bindx() is supposed to call this function.
756 */
757static int sctp_send_asconf_del_ip(struct sock *sk,
758 struct sockaddr *addrs,
759 int addrcnt)
760{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000761 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct sctp_sock *sp;
763 struct sctp_endpoint *ep;
764 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700765 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct sctp_bind_addr *bp;
767 struct sctp_chunk *chunk;
768 union sctp_addr *laddr;
769 void *addr_buf;
770 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700771 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 int i;
773 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900774 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Michio Honda8a07eb02011-04-26 20:19:36 +0900776 chunk = NULL;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000777 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return retval;
779
780 sp = sctp_sk(sk);
781 ep = sp->ep;
782
Daniel Borkmannbb333812013-06-28 19:49:40 +0200783 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
784 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700786 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 if (!asoc->peer.asconf_capable)
789 continue;
790
791 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
792 continue;
793
794 if (!sctp_state(asoc, ESTABLISHED))
795 continue;
796
797 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900798 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 * If so, do not send the asconf chunk to its peer, but
800 * continue with other associations.
801 */
802 addr_buf = addrs;
803 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000804 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 af = sctp_get_af_specific(laddr->v4.sin_family);
806 if (!af) {
807 retval = -EINVAL;
808 goto out;
809 }
810
811 if (!sctp_assoc_lookup_laddr(asoc, laddr))
812 break;
813
814 addr_buf += af->sockaddr_len;
815 }
816 if (i < addrcnt)
817 continue;
818
819 /* Find one address in the association's bind address list
820 * that is not in the packed array of addresses. This is to
821 * make sure that we do not delete all the addresses in the
822 * association.
823 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 bp = &asoc->base.bind_addr;
825 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
826 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900827 if ((laddr == NULL) && (addrcnt == 1)) {
828 if (asoc->asconf_addr_del_pending)
829 continue;
830 asoc->asconf_addr_del_pending =
831 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900832 if (asoc->asconf_addr_del_pending == NULL) {
833 retval = -ENOMEM;
834 goto out;
835 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900836 asoc->asconf_addr_del_pending->sa.sa_family =
837 addrs->sa_family;
838 asoc->asconf_addr_del_pending->v4.sin_port =
839 htons(bp->port);
840 if (addrs->sa_family == AF_INET) {
841 struct sockaddr_in *sin;
842
843 sin = (struct sockaddr_in *)addrs;
844 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
845 } else if (addrs->sa_family == AF_INET6) {
846 struct sockaddr_in6 *sin6;
847
848 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000849 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900850 }
Daniel Borkmannbb333812013-06-28 19:49:40 +0200851
852 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
853 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
854 asoc->asconf_addr_del_pending);
855
Michio Honda8a07eb02011-04-26 20:19:36 +0900856 asoc->src_out_of_asoc_ok = 1;
857 stored = 1;
858 goto skip_mkasconf;
859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Daniel Borkmann88362ad2013-09-07 20:51:21 +0200861 if (laddr == NULL)
862 return -EINVAL;
863
Vlad Yasevich559cf712007-09-16 16:03:28 -0700864 /* We do not need RCU protection throughout this loop
865 * because this is done under a socket lock from the
866 * setsockopt call.
867 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
869 SCTP_PARAM_DEL_IP);
870 if (!chunk) {
871 retval = -ENOMEM;
872 goto out;
873 }
874
Michio Honda8a07eb02011-04-26 20:19:36 +0900875skip_mkasconf:
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700876 /* Reset use_as_src flag for the addresses in the bind address
877 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700879 addr_buf = addrs;
880 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000881 laddr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700882 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700883 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a12006-11-20 17:05:23 -0800884 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800885 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700886 }
887 addr_buf += af->sockaddr_len;
888 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700889
890 /* Update the route and saddr entries for all the transports
891 * as some of the addresses in the bind address list are
892 * about to be deleted and cannot be used as source addresses.
893 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700894 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
895 transports) {
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700896 sctp_transport_route(transport, NULL,
897 sctp_sk(asoc->base.sk));
898 }
899
Michio Honda8a07eb02011-04-26 20:19:36 +0900900 if (stored)
901 /* We don't need to transmit ASCONF */
902 continue;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700903 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905out:
906 return retval;
907}
908
Michio Honda9f7d6532011-04-26 19:32:51 +0900909/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
910int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
911{
912 struct sock *sk = sctp_opt2sk(sp);
913 union sctp_addr *addr;
914 struct sctp_af *af;
915
916 /* It is safe to write port space in caller. */
917 addr = &addrw->a;
918 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
919 af = sctp_get_af_specific(addr->sa.sa_family);
920 if (!af)
921 return -EINVAL;
922 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
923 return -EINVAL;
924
925 if (addrw->state == SCTP_ADDR_NEW)
926 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
927 else
928 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
929}
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
932 *
933 * API 8.1
934 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
935 * int flags);
936 *
937 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
938 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
939 * or IPv6 addresses.
940 *
941 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
942 * Section 3.1.2 for this usage.
943 *
944 * addrs is a pointer to an array of one or more socket addresses. Each
945 * address is contained in its appropriate structure (i.e. struct
946 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700947 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * representation is termed a "packed array" of addresses). The caller
949 * specifies the number of addresses in the array with addrcnt.
950 *
951 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
952 * -1, and sets errno to the appropriate error code.
953 *
954 * For SCTP, the port given in each socket address must be the same, or
955 * sctp_bindx() will fail, setting errno to EINVAL.
956 *
957 * The flags parameter is formed from the bitwise OR of zero or more of
958 * the following currently defined flags:
959 *
960 * SCTP_BINDX_ADD_ADDR
961 *
962 * SCTP_BINDX_REM_ADDR
963 *
964 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
965 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
966 * addresses from the association. The two flags are mutually exclusive;
967 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
968 * not remove all addresses from an association; sctp_bindx() will
969 * reject such an attempt with EINVAL.
970 *
971 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
972 * additional addresses with an endpoint after calling bind(). Or use
973 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
974 * socket is associated with so that no new association accepted will be
975 * associated with those addresses. If the endpoint supports dynamic
976 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
977 * endpoint to send the appropriate message to the peer to change the
978 * peers address lists.
979 *
980 * Adding and removing addresses from a connected association is
981 * optional functionality. Implementations that do not support this
982 * functionality should return EOPNOTSUPP.
983 *
984 * Basically do nothing but copying the addresses from user to kernel
985 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700986 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
987 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
990 * it.
991 *
992 * sk The sk of the socket
993 * addrs The pointer to the addresses in user land
994 * addrssize Size of the addrs buffer
995 * op Operation to perform (add or remove, see the flags of
996 * sctp_bindx)
997 *
998 * Returns 0 if ok, <0 errno code on error.
999 */
wangweidong26ac8e52013-12-23 12:16:51 +08001000static int sctp_setsockopt_bindx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001001 struct sockaddr __user *addrs,
1002 int addrs_size, int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 struct sockaddr *kaddrs;
1005 int err;
1006 int addrcnt = 0;
1007 int walk_size = 0;
1008 struct sockaddr *sa_addr;
1009 void *addr_buf;
1010 struct sctp_af *af;
1011
Daniel Borkmannbb333812013-06-28 19:49:40 +02001012 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1013 __func__, sk, addrs, addrs_size, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 if (unlikely(addrs_size <= 0))
1016 return -EINVAL;
1017
Al Viroc981f252018-01-07 13:19:09 -05001018 kaddrs = vmemdup_user(addrs, addrs_size);
1019 if (unlikely(IS_ERR(kaddrs)))
1020 return PTR_ERR(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001022 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 addr_buf = kaddrs;
1024 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001025 if (walk_size + sizeof(sa_family_t) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001026 kvfree(kaddrs);
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001027 return -EINVAL;
1028 }
1029
Joe Perchesea110732011-06-13 16:21:26 +00001030 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 af = sctp_get_af_specific(sa_addr->sa_family);
1032
1033 /* If the address family is not supported or if this address
1034 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001035 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001037 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return -EINVAL;
1039 }
1040 addrcnt++;
1041 addr_buf += af->sockaddr_len;
1042 walk_size += af->sockaddr_len;
1043 }
1044
1045 /* Do the work. */
1046 switch (op) {
1047 case SCTP_BINDX_ADD_ADDR:
Richard Haines2277c7c2018-02-13 20:56:24 +00001048 /* Allow security module to validate bindx addresses. */
1049 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1050 (struct sockaddr *)kaddrs,
1051 addrs_size);
1052 if (err)
1053 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1055 if (err)
1056 goto out;
1057 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1058 break;
1059
1060 case SCTP_BINDX_REM_ADDR:
1061 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1062 if (err)
1063 goto out;
1064 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1065 break;
1066
1067 default:
1068 err = -EINVAL;
1069 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072out:
Al Viroc981f252018-01-07 13:19:09 -05001073 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 return err;
1076}
1077
Frank Filz3f7a87d2005-06-20 13:14:57 -07001078/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1079 *
1080 * Common routine for handling connect() and sctp_connectx().
1081 * Connect will come in with just a single address.
1082 */
wangweidong26ac8e52013-12-23 12:16:51 +08001083static int __sctp_connect(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001084 struct sockaddr *kaddrs,
Xin Long644fbde2018-05-20 16:39:10 +08001085 int addrs_size, int flags,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001086 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001087{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001088 struct net *net = sock_net(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001089 struct sctp_sock *sp;
1090 struct sctp_endpoint *ep;
1091 struct sctp_association *asoc = NULL;
1092 struct sctp_association *asoc2;
1093 struct sctp_transport *transport;
1094 union sctp_addr to;
Xin Long1c662012017-08-05 19:59:54 +08001095 enum sctp_scope scope;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001096 long timeo;
1097 int err = 0;
1098 int addrcnt = 0;
1099 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001100 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001101 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001102 unsigned short port;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001103
1104 sp = sctp_sk(sk);
1105 ep = sp->ep;
1106
1107 /* connect() cannot be done on a socket that is already in ESTABLISHED
1108 * state - UDP-style peeled off socket or a TCP-style socket that
1109 * is already connected.
1110 * It cannot be done even on a TCP-style listening socket.
1111 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03001112 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
Frank Filz3f7a87d2005-06-20 13:14:57 -07001113 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1114 err = -EISCONN;
1115 goto out_free;
1116 }
1117
1118 /* Walk through the addrs buffer and count the number of addresses. */
1119 addr_buf = kaddrs;
1120 while (walk_size < addrs_size) {
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001121 struct sctp_af *af;
1122
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001123 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1124 err = -EINVAL;
1125 goto out_free;
1126 }
1127
Joe Perchesea110732011-06-13 16:21:26 +00001128 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001129 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001130
1131 /* If the address family is not supported or if this address
1132 * causes the address buffer to overflow return EINVAL.
1133 */
1134 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1135 err = -EINVAL;
1136 goto out_free;
1137 }
1138
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001139 port = ntohs(sa_addr->v4.sin_port);
1140
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001141 /* Save current address so we can work with it */
1142 memcpy(&to, sa_addr, af->sockaddr_len);
1143
1144 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001145 if (err)
1146 goto out_free;
1147
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001148 /* Make sure the destination port is correctly set
1149 * in all addresses.
1150 */
Wei Yongjun524fba62013-04-03 03:02:28 +00001151 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1152 err = -EINVAL;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001153 goto out_free;
Wei Yongjun524fba62013-04-03 03:02:28 +00001154 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001155
1156 /* Check if there already is a matching association on the
1157 * endpoint (other than the one created here).
1158 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001159 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001160 if (asoc2 && asoc2 != asoc) {
1161 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1162 err = -EISCONN;
1163 else
1164 err = -EALREADY;
1165 goto out_free;
1166 }
1167
1168 /* If we could not find a matching association on the endpoint,
1169 * make sure that there is no peeled-off association matching
1170 * the peer address even on another socket.
1171 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001172 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001173 err = -EADDRNOTAVAIL;
1174 goto out_free;
1175 }
1176
1177 if (!asoc) {
1178 /* If a bind() or sctp_bindx() is not called prior to
1179 * an sctp_connectx() call, the system picks an
1180 * ephemeral port and will choose an address set
1181 * equivalent to binding with a wildcard address.
1182 */
1183 if (!ep->base.bind_addr.port) {
1184 if (sctp_autobind(sk)) {
1185 err = -EAGAIN;
1186 goto out_free;
1187 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001188 } else {
1189 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001190 * If an unprivileged user inherits a 1-many
1191 * style socket with open associations on a
1192 * privileged port, it MAY be permitted to
1193 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001194 * be permitted to open new associations.
1195 */
Krister Johansen4548b682017-01-20 17:49:11 -08001196 if (ep->base.bind_addr.port <
1197 inet_prot_sock(net) &&
1198 !ns_capable(net->user_ns,
1199 CAP_NET_BIND_SERVICE)) {
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001200 err = -EACCES;
1201 goto out_free;
1202 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001203 }
1204
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001205 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001206 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1207 if (!asoc) {
1208 err = -ENOMEM;
1209 goto out_free;
1210 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001211
1212 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1213 GFP_KERNEL);
1214 if (err < 0) {
1215 goto out_free;
1216 }
1217
Frank Filz3f7a87d2005-06-20 13:14:57 -07001218 }
1219
1220 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001221 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001222 SCTP_UNKNOWN);
1223 if (!transport) {
1224 err = -ENOMEM;
1225 goto out_free;
1226 }
1227
1228 addrcnt++;
1229 addr_buf += af->sockaddr_len;
1230 walk_size += af->sockaddr_len;
1231 }
1232
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001233 /* In case the user of sctp_connectx() wants an association
1234 * id back, assign one now.
1235 */
1236 if (assoc_id) {
1237 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1238 if (err < 0)
1239 goto out_free;
1240 }
1241
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001242 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001243 if (err < 0) {
1244 goto out_free;
1245 }
1246
1247 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001248 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001249 sp->pf->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001250 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001251
Xin Long644fbde2018-05-20 16:39:10 +08001252 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001253
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001254 if (assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001255 *assoc_id = asoc->assoc_id;
Richard Haines2277c7c2018-02-13 20:56:24 +00001256
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001257 err = sctp_wait_for_connect(asoc, &timeo);
1258 /* Note: the asoc may be freed after the return of
1259 * sctp_wait_for_connect.
1260 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001261
1262 /* Don't free association on exit. */
1263 asoc = NULL;
1264
1265out_free:
Daniel Borkmannbb333812013-06-28 19:49:40 +02001266 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1267 __func__, asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001268
Neil Horman2eebc1e2012-07-16 09:13:51 +00001269 if (asoc) {
1270 /* sctp_primitive_ASSOCIATE may have added this association
1271 * To the hash table, try to unhash it, just in case, its a noop
1272 * if it wasn't hashed so we're safe
1273 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001274 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001275 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001276 return err;
1277}
1278
1279/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1280 *
1281 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001282 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1283 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001284 *
1285 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1286 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1287 * or IPv6 addresses.
1288 *
1289 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1290 * Section 3.1.2 for this usage.
1291 *
1292 * addrs is a pointer to an array of one or more socket addresses. Each
1293 * address is contained in its appropriate structure (i.e. struct
1294 * sockaddr_in or struct sockaddr_in6) the family of the address type
1295 * must be used to distengish the address length (note that this
1296 * representation is termed a "packed array" of addresses). The caller
1297 * specifies the number of addresses in the array with addrcnt.
1298 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001299 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1300 * the association id of the new association. On failure, sctp_connectx()
1301 * returns -1, and sets errno to the appropriate error code. The assoc_id
1302 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001303 *
1304 * For SCTP, the port given in each socket address must be the same, or
1305 * sctp_connectx() will fail, setting errno to EINVAL.
1306 *
1307 * An application can use sctp_connectx to initiate an association with
1308 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1309 * allows a caller to specify multiple addresses at which a peer can be
1310 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001311 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001312 * specifies that the stack will try to make use of all the addresses in
1313 * the list when needed.
1314 *
1315 * Note that the list of addresses passed in is only used for setting up
1316 * the association. It does not necessarily equal the set of addresses
1317 * the peer uses for the resulting association. If the caller wants to
1318 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1319 * retrieve them after the association has been set up.
1320 *
1321 * Basically do nothing but copying the addresses from user to kernel
1322 * land and invoking either sctp_connectx(). This is used for tunneling
1323 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1324 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07001325 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1326 * it.
1327 *
1328 * sk The sk of the socket
1329 * addrs The pointer to the addresses in user land
1330 * addrssize Size of the addrs buffer
1331 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001332 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001333 */
wangweidong26ac8e52013-12-23 12:16:51 +08001334static int __sctp_setsockopt_connectx(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001335 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001336 int addrs_size,
1337 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001338{
Frank Filz3f7a87d2005-06-20 13:14:57 -07001339 struct sockaddr *kaddrs;
Xin Long644fbde2018-05-20 16:39:10 +08001340 int err = 0, flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001341
Daniel Borkmannbb333812013-06-28 19:49:40 +02001342 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1343 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001344
1345 if (unlikely(addrs_size <= 0))
1346 return -EINVAL;
1347
Al Viroc981f252018-01-07 13:19:09 -05001348 kaddrs = vmemdup_user(addrs, addrs_size);
1349 if (unlikely(IS_ERR(kaddrs)))
1350 return PTR_ERR(kaddrs);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001351
Richard Haines2277c7c2018-02-13 20:56:24 +00001352 /* Allow security module to validate connectx addresses. */
1353 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1354 (struct sockaddr *)kaddrs,
1355 addrs_size);
1356 if (err)
1357 goto out_free;
1358
Xin Long644fbde2018-05-20 16:39:10 +08001359 /* in-kernel sockets don't generally have a file allocated to them
1360 * if all they do is call sock_create_kern().
1361 */
1362 if (sk->sk_socket->file)
1363 flags = sk->sk_socket->file->f_flags;
1364
1365 err = __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
Richard Haines2277c7c2018-02-13 20:56:24 +00001366
1367out_free:
Al Viroc981f252018-01-07 13:19:09 -05001368 kvfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001369
Frank Filz3f7a87d2005-06-20 13:14:57 -07001370 return err;
1371}
1372
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001373/*
1374 * This is an older interface. It's kept for backward compatibility
1375 * to the option that doesn't provide association id.
1376 */
wangweidong26ac8e52013-12-23 12:16:51 +08001377static int sctp_setsockopt_connectx_old(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001378 struct sockaddr __user *addrs,
1379 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001380{
1381 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1382}
1383
1384/*
1385 * New interface for the API. The since the API is done with a socket
1386 * option, to make it simple we feed back the association id is as a return
1387 * indication to the call. Error is always negative and association id is
1388 * always positive.
1389 */
wangweidong26ac8e52013-12-23 12:16:51 +08001390static int sctp_setsockopt_connectx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001391 struct sockaddr __user *addrs,
1392 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001393{
1394 sctp_assoc_t assoc_id = 0;
1395 int err = 0;
1396
1397 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1398
1399 if (err)
1400 return err;
1401 else
1402 return assoc_id;
1403}
1404
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001405/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001406 * New (hopefully final) interface for the API.
1407 * We use the sctp_getaddrs_old structure so that use-space library
Daniel Borkmannffd59392014-02-17 12:11:11 +01001408 * can avoid any unnecessary allocations. The only different part
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001409 * is that we store the actual length of the address buffer into the
Daniel Borkmannffd59392014-02-17 12:11:11 +01001410 * addrs_num structure member. That way we can re-use the existing
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001411 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001412 */
Daniel Borkmannffd59392014-02-17 12:11:11 +01001413#ifdef CONFIG_COMPAT
1414struct compat_sctp_getaddrs_old {
1415 sctp_assoc_t assoc_id;
1416 s32 addr_num;
1417 compat_uptr_t addrs; /* struct sockaddr * */
1418};
1419#endif
1420
wangweidong26ac8e52013-12-23 12:16:51 +08001421static int sctp_getsockopt_connectx3(struct sock *sk, int len,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001422 char __user *optval,
1423 int __user *optlen)
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001424{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001425 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001426 sctp_assoc_t assoc_id = 0;
1427 int err = 0;
1428
Daniel Borkmannffd59392014-02-17 12:11:11 +01001429#ifdef CONFIG_COMPAT
Andy Lutomirski96c0e0a2016-03-22 14:25:07 -07001430 if (in_compat_syscall()) {
Daniel Borkmannffd59392014-02-17 12:11:11 +01001431 struct compat_sctp_getaddrs_old param32;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001432
Daniel Borkmannffd59392014-02-17 12:11:11 +01001433 if (len < sizeof(param32))
1434 return -EINVAL;
1435 if (copy_from_user(&param32, optval, sizeof(param32)))
1436 return -EFAULT;
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001437
Daniel Borkmannffd59392014-02-17 12:11:11 +01001438 param.assoc_id = param32.assoc_id;
1439 param.addr_num = param32.addr_num;
1440 param.addrs = compat_ptr(param32.addrs);
1441 } else
1442#endif
1443 {
1444 if (len < sizeof(param))
1445 return -EINVAL;
1446 if (copy_from_user(&param, optval, sizeof(param)))
1447 return -EFAULT;
1448 }
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001449
Daniel Borkmannffd59392014-02-17 12:11:11 +01001450 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1451 param.addrs, param.addr_num,
1452 &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001453 if (err == 0 || err == -EINPROGRESS) {
1454 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1455 return -EFAULT;
1456 if (put_user(sizeof(assoc_id), optlen))
1457 return -EFAULT;
1458 }
1459
1460 return err;
1461}
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463/* API 3.1.4 close() - UDP Style Syntax
1464 * Applications use close() to perform graceful shutdown (as described in
1465 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1466 * by a UDP-style socket.
1467 *
1468 * The syntax is
1469 *
1470 * ret = close(int sd);
1471 *
1472 * sd - the socket descriptor of the associations to be closed.
1473 *
1474 * To gracefully shutdown a specific association represented by the
1475 * UDP-style socket, an application should use the sendmsg() call,
1476 * passing no user data, but including the appropriate flag in the
1477 * ancillary data (see Section xxxx).
1478 *
1479 * If sd in the close() call is a branched-off socket representing only
1480 * one association, the shutdown is performed on that association only.
1481 *
1482 * 4.1.6 close() - TCP Style Syntax
1483 *
1484 * Applications use close() to gracefully close down an association.
1485 *
1486 * The syntax is:
1487 *
1488 * int close(int sd);
1489 *
1490 * sd - the socket descriptor of the association to be closed.
1491 *
1492 * After an application calls close() on a socket descriptor, no further
1493 * socket operations will succeed on that descriptor.
1494 *
1495 * API 7.1.4 SO_LINGER
1496 *
1497 * An application using the TCP-style socket can use this option to
1498 * perform the SCTP ABORT primitive. The linger option structure is:
1499 *
1500 * struct linger {
1501 * int l_onoff; // option on/off
1502 * int l_linger; // linger time
1503 * };
1504 *
1505 * To enable the option, set l_onoff to 1. If the l_linger value is set
1506 * to 0, calling close() is the same as the ABORT primitive. If the
1507 * value is set to a negative value, the setsockopt() call will return
1508 * an error. If the value is set to a positive value linger_time, the
1509 * close() can be blocked for at most linger_time ms. If the graceful
1510 * shutdown phase does not finish during this period, close() will
1511 * return but the graceful shutdown phase continues in the system.
1512 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02001513static void sctp_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001515 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 struct sctp_endpoint *ep;
1517 struct sctp_association *asoc;
1518 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001519 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Daniel Borkmannbb333812013-06-28 19:49:40 +02001521 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Xin Long6dfe4b92017-06-10 14:56:56 +08001523 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 sk->sk_shutdown = SHUTDOWN_MASK;
Yafang Shaocbabf462017-12-20 11:12:54 +08001525 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 ep = sctp_sk(sk)->ep;
1528
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001529 /* Clean up any skbs sitting on the receive queue. */
1530 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1531 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1532
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001533 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 list_for_each_safe(pos, temp, &ep->asocs) {
1535 asoc = list_entry(pos, struct sctp_association, asocs);
1536
1537 if (sctp_style(sk, TCP)) {
1538 /* A closed association can still be in the list if
1539 * it belongs to a TCP-style listening socket that is
1540 * not yet accepted. If so, free it. If not, send an
1541 * ABORT or SHUTDOWN based on the linger options.
1542 */
1543 if (sctp_state(asoc, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001545 continue;
1546 }
1547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001549 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1550 !skb_queue_empty(&asoc->ulpq.reasm) ||
Xin Long13228232017-12-08 21:04:09 +08001551 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001552 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001553 struct sctp_chunk *chunk;
1554
1555 chunk = sctp_make_abort_user(asoc, NULL, 0);
Xin Long068d8bd2015-12-29 17:49:25 +08001556 sctp_primitive_ABORT(net, asoc, chunk);
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001557 } else
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001558 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 /* On a TCP-style socket, block for at most linger_time if set. */
1562 if (sctp_style(sk, TCP) && timeout)
1563 sctp_wait_for_close(sk, timeout);
1564
1565 /* This will run the backlog queue. */
wangweidong048ed4b2014-01-21 15:44:11 +08001566 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 /* Supposedly, no process has access to the socket, but
1569 * the net layers still may.
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001570 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1571 * held and that should be grabbed before socket lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001573 spin_lock_bh(&net->sctp.addr_wq_lock);
Xin Long6dfe4b92017-06-10 14:56:56 +08001574 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 /* Hold the sock, since sk_common_release() will put sock_put()
1577 * and we have just a little more cleanup.
1578 */
1579 sock_hold(sk);
1580 sk_common_release(sk);
1581
wangweidong5bc1d1b2014-01-21 15:44:12 +08001582 bh_unlock_sock(sk);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001583 spin_unlock_bh(&net->sctp.addr_wq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 sock_put(sk);
1586
1587 SCTP_DBG_OBJCNT_DEC(sock);
1588}
1589
1590/* Handle EPIPE error. */
1591static int sctp_error(struct sock *sk, int flags, int err)
1592{
1593 if (err == -EPIPE)
1594 err = sock_error(sk) ? : -EPIPE;
1595 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1596 send_sig(SIGPIPE, current, 0);
1597 return err;
1598}
1599
1600/* API 3.1.3 sendmsg() - UDP Style Syntax
1601 *
1602 * An application uses sendmsg() and recvmsg() calls to transmit data to
1603 * and receive data from its peer.
1604 *
1605 * ssize_t sendmsg(int socket, const struct msghdr *message,
1606 * int flags);
1607 *
1608 * socket - the socket descriptor of the endpoint.
1609 * message - pointer to the msghdr structure which contains a single
1610 * user message and possibly some ancillary data.
1611 *
1612 * See Section 5 for complete description of the data
1613 * structures.
1614 *
1615 * flags - flags sent or received with the user message, see Section
1616 * 5 for complete description of the flags.
1617 *
1618 * Note: This function could use a rewrite especially when explicit
1619 * connect support comes in.
1620 */
1621/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1622
Xin Longa05437a2017-08-11 10:23:48 +08001623static int sctp_msghdr_parse(const struct msghdr *msg,
1624 struct sctp_cmsgs *cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Xin Long204f8172018-03-01 23:05:14 +08001626static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1627 struct sctp_sndrcvinfo *srinfo,
1628 const struct msghdr *msg, size_t msg_len)
1629{
1630 __u16 sflags;
1631 int err;
1632
1633 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1634 return -EPIPE;
1635
1636 if (msg_len > sk->sk_sndbuf)
1637 return -EMSGSIZE;
1638
1639 memset(cmsgs, 0, sizeof(*cmsgs));
1640 err = sctp_msghdr_parse(msg, cmsgs);
1641 if (err) {
1642 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1643 return err;
1644 }
1645
1646 memset(srinfo, 0, sizeof(*srinfo));
1647 if (cmsgs->srinfo) {
1648 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1649 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1650 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1651 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1652 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1653 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1654 }
1655
1656 if (cmsgs->sinfo) {
1657 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1658 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1659 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1660 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1661 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1662 }
1663
Xin Longed63afb2018-03-05 20:44:18 +08001664 if (cmsgs->prinfo) {
1665 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1666 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1667 cmsgs->prinfo->pr_policy);
1668 }
1669
Xin Long204f8172018-03-01 23:05:14 +08001670 sflags = srinfo->sinfo_flags;
1671 if (!sflags && msg_len)
1672 return 0;
1673
1674 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1675 return -EINVAL;
1676
1677 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1678 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1679 return -EINVAL;
1680
1681 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1682 return -EINVAL;
1683
1684 return 0;
1685}
1686
Xin Long2bfd80f2018-03-01 23:05:11 +08001687static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1688 struct sctp_cmsgs *cmsgs,
1689 union sctp_addr *daddr,
1690 struct sctp_transport **tp)
1691{
1692 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1693 struct net *net = sock_net(sk);
1694 struct sctp_association *asoc;
1695 enum sctp_scope scope;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001696 struct cmsghdr *cmsg;
Xin Long4be41392018-07-02 18:21:14 +08001697 __be32 flowinfo = 0;
Linus Torvalds9eda2d22018-04-06 15:39:26 -07001698 struct sctp_af *af;
Wei Yongjund98985d2018-03-13 03:03:30 +00001699 int err;
Xin Long2bfd80f2018-03-01 23:05:11 +08001700
1701 *tp = NULL;
1702
1703 if (sflags & (SCTP_EOF | SCTP_ABORT))
1704 return -EINVAL;
1705
1706 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1707 sctp_sstate(sk, CLOSING)))
1708 return -EADDRNOTAVAIL;
1709
1710 if (sctp_endpoint_is_peeled_off(ep, daddr))
1711 return -EADDRNOTAVAIL;
1712
1713 if (!ep->base.bind_addr.port) {
1714 if (sctp_autobind(sk))
1715 return -EAGAIN;
1716 } else {
1717 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1718 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1719 return -EACCES;
1720 }
1721
1722 scope = sctp_scope(daddr);
1723
Linus Torvalds9eda2d22018-04-06 15:39:26 -07001724 /* Label connection socket for first association 1-to-many
1725 * style for client sequence socket()->sendmsg(). This
1726 * needs to be done before sctp_assoc_add_peer() as that will
1727 * set up the initial packet that needs to account for any
1728 * security ip options (CIPSO/CALIPSO) added to the packet.
1729 */
1730 af = sctp_get_af_specific(daddr->sa.sa_family);
1731 if (!af)
1732 return -EINVAL;
1733 err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1734 (struct sockaddr *)daddr,
1735 af->sockaddr_len);
1736 if (err < 0)
1737 return err;
1738
Xin Long2bfd80f2018-03-01 23:05:11 +08001739 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1740 if (!asoc)
1741 return -ENOMEM;
1742
1743 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1744 err = -ENOMEM;
1745 goto free;
1746 }
1747
1748 if (cmsgs->init) {
1749 struct sctp_initmsg *init = cmsgs->init;
1750
1751 if (init->sinit_num_ostreams) {
1752 __u16 outcnt = init->sinit_num_ostreams;
1753
1754 asoc->c.sinit_num_ostreams = outcnt;
1755 /* outcnt has been changed, need to re-init stream */
1756 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1757 GFP_KERNEL);
1758 if (err)
1759 goto free;
1760 }
1761
1762 if (init->sinit_max_instreams)
1763 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1764
1765 if (init->sinit_max_attempts)
1766 asoc->max_init_attempts = init->sinit_max_attempts;
1767
1768 if (init->sinit_max_init_timeo)
1769 asoc->max_init_timeo =
1770 msecs_to_jiffies(init->sinit_max_init_timeo);
1771 }
1772
1773 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1774 if (!*tp) {
1775 err = -ENOMEM;
1776 goto free;
1777 }
1778
Xin Long2c0dbaa2018-03-05 20:44:19 +08001779 if (!cmsgs->addrs_msg)
1780 return 0;
1781
Xin Long4be41392018-07-02 18:21:14 +08001782 if (daddr->sa.sa_family == AF_INET6)
1783 flowinfo = daddr->v6.sin6_flowinfo;
1784
Xin Long2c0dbaa2018-03-05 20:44:19 +08001785 /* sendv addr list parse */
1786 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1787 struct sctp_transport *transport;
1788 struct sctp_association *old;
1789 union sctp_addr _daddr;
1790 int dlen;
1791
1792 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1793 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1794 cmsg->cmsg_type != SCTP_DSTADDRV6))
1795 continue;
1796
1797 daddr = &_daddr;
1798 memset(daddr, 0, sizeof(*daddr));
1799 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1800 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
Wei Yongjund98985d2018-03-13 03:03:30 +00001801 if (dlen < sizeof(struct in_addr)) {
1802 err = -EINVAL;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001803 goto free;
Wei Yongjund98985d2018-03-13 03:03:30 +00001804 }
Xin Long2c0dbaa2018-03-05 20:44:19 +08001805
1806 dlen = sizeof(struct in_addr);
1807 daddr->v4.sin_family = AF_INET;
1808 daddr->v4.sin_port = htons(asoc->peer.port);
1809 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1810 } else {
Wei Yongjund98985d2018-03-13 03:03:30 +00001811 if (dlen < sizeof(struct in6_addr)) {
1812 err = -EINVAL;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001813 goto free;
Wei Yongjund98985d2018-03-13 03:03:30 +00001814 }
Xin Long2c0dbaa2018-03-05 20:44:19 +08001815
1816 dlen = sizeof(struct in6_addr);
Xin Long4be41392018-07-02 18:21:14 +08001817 daddr->v6.sin6_flowinfo = flowinfo;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001818 daddr->v6.sin6_family = AF_INET6;
1819 daddr->v6.sin6_port = htons(asoc->peer.port);
1820 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1821 }
1822 err = sctp_verify_addr(sk, daddr, sizeof(*daddr));
1823 if (err)
1824 goto free;
1825
1826 old = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1827 if (old && old != asoc) {
1828 if (old->state >= SCTP_STATE_ESTABLISHED)
1829 err = -EISCONN;
1830 else
1831 err = -EALREADY;
1832 goto free;
1833 }
1834
1835 if (sctp_endpoint_is_peeled_off(ep, daddr)) {
1836 err = -EADDRNOTAVAIL;
1837 goto free;
1838 }
1839
1840 transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL,
1841 SCTP_UNKNOWN);
1842 if (!transport) {
1843 err = -ENOMEM;
1844 goto free;
1845 }
1846 }
1847
Xin Long2bfd80f2018-03-01 23:05:11 +08001848 return 0;
1849
1850free:
1851 sctp_association_free(asoc);
1852 return err;
1853}
1854
Xin Longc2666de2018-03-01 23:05:12 +08001855static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1856 __u16 sflags, struct msghdr *msg,
1857 size_t msg_len)
1858{
1859 struct sock *sk = asoc->base.sk;
1860 struct net *net = sock_net(sk);
1861
1862 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1863 return -EPIPE;
1864
Xin Long49102802018-03-05 20:44:20 +08001865 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1866 !sctp_state(asoc, ESTABLISHED))
1867 return 0;
1868
Xin Longc2666de2018-03-01 23:05:12 +08001869 if (sflags & SCTP_EOF) {
1870 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1871 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1872
1873 return 0;
1874 }
1875
1876 if (sflags & SCTP_ABORT) {
1877 struct sctp_chunk *chunk;
1878
1879 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1880 if (!chunk)
1881 return -ENOMEM;
1882
1883 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1884 sctp_primitive_ABORT(net, asoc, chunk);
1885
1886 return 0;
1887 }
1888
1889 return 1;
1890}
1891
Xin Longf84af332018-03-01 23:05:10 +08001892static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1893 struct msghdr *msg, size_t msg_len,
1894 struct sctp_transport *transport,
1895 struct sctp_sndrcvinfo *sinfo)
1896{
1897 struct sock *sk = asoc->base.sk;
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001898 struct sctp_sock *sp = sctp_sk(sk);
Xin Longf84af332018-03-01 23:05:10 +08001899 struct net *net = sock_net(sk);
1900 struct sctp_datamsg *datamsg;
1901 bool wait_connect = false;
1902 struct sctp_chunk *chunk;
1903 long timeo;
1904 int err;
1905
1906 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1907 err = -EINVAL;
1908 goto err;
1909 }
1910
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001911 if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
Xin Longf84af332018-03-01 23:05:10 +08001912 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1913 if (err)
1914 goto err;
1915 }
1916
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001917 if (sp->disable_fragments && msg_len > asoc->frag_point) {
Xin Longf84af332018-03-01 23:05:10 +08001918 err = -EMSGSIZE;
1919 goto err;
1920 }
1921
Marcelo Ricardo Leitner25216802018-04-26 16:58:56 -03001922 if (asoc->pmtu_pending) {
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001923 if (sp->param_flags & SPP_PMTUD_ENABLE)
1924 sctp_assoc_sync_pmtu(asoc);
Marcelo Ricardo Leitner25216802018-04-26 16:58:56 -03001925 asoc->pmtu_pending = 0;
1926 }
Neil Horman0aee4c22018-03-12 14:15:25 -04001927
1928 if (sctp_wspace(asoc) < msg_len)
1929 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1930
1931 if (!sctp_wspace(asoc)) {
1932 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1933 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1934 if (err)
1935 goto err;
1936 }
1937
Xin Longf84af332018-03-01 23:05:10 +08001938 if (sctp_state(asoc, CLOSED)) {
1939 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1940 if (err)
1941 goto err;
1942
Marcelo Ricardo Leitner63d01332018-04-26 16:59:00 -03001943 if (sp->strm_interleave) {
Xin Longf84af332018-03-01 23:05:10 +08001944 timeo = sock_sndtimeo(sk, 0);
1945 err = sctp_wait_for_connect(asoc, &timeo);
1946 if (err)
1947 goto err;
1948 } else {
1949 wait_connect = true;
1950 }
1951
1952 pr_debug("%s: we associated primitively\n", __func__);
1953 }
1954
Xin Longf84af332018-03-01 23:05:10 +08001955 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1956 if (IS_ERR(datamsg)) {
1957 err = PTR_ERR(datamsg);
1958 goto err;
1959 }
1960
1961 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1962
1963 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1964 sctp_chunk_hold(chunk);
1965 sctp_set_owner_w(chunk);
1966 chunk->transport = transport;
1967 }
1968
1969 err = sctp_primitive_SEND(net, asoc, datamsg);
1970 if (err) {
1971 sctp_datamsg_free(datamsg);
1972 goto err;
1973 }
1974
1975 pr_debug("%s: we sent primitively\n", __func__);
1976
1977 sctp_datamsg_put(datamsg);
1978
1979 if (unlikely(wait_connect)) {
1980 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1981 sctp_wait_for_connect(asoc, &timeo);
1982 }
1983
1984 err = msg_len;
1985
1986err:
1987 return err;
1988}
1989
Xin Longbecef9b2018-03-01 23:05:13 +08001990static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1991 const struct msghdr *msg,
1992 struct sctp_cmsgs *cmsgs)
1993{
1994 union sctp_addr *daddr = NULL;
1995 int err;
1996
1997 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1998 int len = msg->msg_namelen;
1999
2000 if (len > sizeof(*daddr))
2001 len = sizeof(*daddr);
2002
2003 daddr = (union sctp_addr *)msg->msg_name;
2004
2005 err = sctp_verify_addr(sk, daddr, len);
2006 if (err)
2007 return ERR_PTR(err);
2008 }
2009
2010 return daddr;
2011}
2012
Xin Longd42cb062018-03-01 23:05:15 +08002013static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
2014 struct sctp_sndrcvinfo *sinfo,
2015 struct sctp_cmsgs *cmsgs)
2016{
2017 if (!cmsgs->srinfo && !cmsgs->sinfo) {
2018 sinfo->sinfo_stream = asoc->default_stream;
2019 sinfo->sinfo_ppid = asoc->default_ppid;
2020 sinfo->sinfo_context = asoc->default_context;
2021 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
Xin Longed63afb2018-03-05 20:44:18 +08002022
2023 if (!cmsgs->prinfo)
2024 sinfo->sinfo_flags = asoc->default_flags;
Xin Longd42cb062018-03-01 23:05:15 +08002025 }
2026
Xin Longed63afb2018-03-05 20:44:18 +08002027 if (!cmsgs->srinfo && !cmsgs->prinfo)
Xin Longd42cb062018-03-01 23:05:15 +08002028 sinfo->sinfo_timetolive = asoc->default_timetolive;
Xin Long3ff547c2018-03-14 19:05:31 +08002029
2030 if (cmsgs->authinfo) {
2031 /* Reuse sinfo_tsn to indicate that authinfo was set and
2032 * sinfo_ssn to save the keyid on tx path.
2033 */
2034 sinfo->sinfo_tsn = 1;
2035 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
2036 }
Xin Longd42cb062018-03-01 23:05:15 +08002037}
2038
Ying Xue1b784142015-03-02 15:37:48 +08002039static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Xin Long204f8172018-03-01 23:05:14 +08002041 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Xin Long8e87c6e2018-03-01 23:05:16 +08002042 struct sctp_transport *transport = NULL;
Xin Long204f8172018-03-01 23:05:14 +08002043 struct sctp_sndrcvinfo _sinfo, *sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08002044 struct sctp_association *asoc;
2045 struct sctp_cmsgs cmsgs;
Xin Longbecef9b2018-03-01 23:05:13 +08002046 union sctp_addr *daddr;
Xin Long007b7e12018-03-01 23:05:17 +08002047 bool new = false;
2048 __u16 sflags;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02002049 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Xin Long204f8172018-03-01 23:05:14 +08002051 /* Parse and get snd_info */
2052 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
2053 if (err)
Xin Long007b7e12018-03-01 23:05:17 +08002054 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Xin Long204f8172018-03-01 23:05:14 +08002056 sinfo = &_sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08002057 sflags = sinfo->sinfo_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Xin Longbecef9b2018-03-01 23:05:13 +08002059 /* Get daddr from msg */
2060 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
2061 if (IS_ERR(daddr)) {
2062 err = PTR_ERR(daddr);
Xin Long007b7e12018-03-01 23:05:17 +08002063 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065
wangweidong048ed4b2014-01-21 15:44:11 +08002066 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Xin Long49102802018-03-05 20:44:20 +08002068 /* SCTP_SENDALL process */
2069 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
2070 list_for_each_entry(asoc, &ep->asocs, asocs) {
2071 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2072 msg_len);
2073 if (err == 0)
2074 continue;
2075 if (err < 0)
2076 goto out_unlock;
2077
2078 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2079
2080 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
2081 NULL, sinfo);
2082 if (err < 0)
2083 goto out_unlock;
2084
2085 iov_iter_revert(&msg->msg_iter, err);
2086 }
2087
2088 goto out_unlock;
2089 }
2090
Xin Long0a3920d2018-03-01 23:05:18 +08002091 /* Get and check or create asoc */
Xin Longbecef9b2018-03-01 23:05:13 +08002092 if (daddr) {
Xin Longbecef9b2018-03-01 23:05:13 +08002093 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
Xin Long0a3920d2018-03-01 23:05:18 +08002094 if (asoc) {
2095 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2096 msg_len);
2097 if (err <= 0)
2098 goto out_unlock;
2099 } else {
2100 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2101 &transport);
2102 if (err)
2103 goto out_unlock;
2104
2105 asoc = transport->asoc;
2106 new = true;
2107 }
2108
2109 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2110 transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 } else {
Xin Long007b7e12018-03-01 23:05:17 +08002112 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (!asoc) {
2114 err = -EPIPE;
2115 goto out_unlock;
2116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Xin Long007b7e12018-03-01 23:05:17 +08002118 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
Xin Longc2666de2018-03-01 23:05:12 +08002119 if (err <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
2122
Xin Longd42cb062018-03-01 23:05:15 +08002123 /* Update snd_info with the asoc */
2124 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Xin Longf84af332018-03-01 23:05:10 +08002126 /* Send msg to the asoc */
Xin Long8e87c6e2018-03-01 23:05:16 +08002127 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
Xin Long007b7e12018-03-01 23:05:17 +08002128 if (err < 0 && err != -ESRCH && new)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 sctp_association_free(asoc);
Xin Long8e87c6e2018-03-01 23:05:16 +08002130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131out_unlock:
wangweidong048ed4b2014-01-21 15:44:11 +08002132 release_sock(sk);
Xin Long007b7e12018-03-01 23:05:17 +08002133out:
Xin Longf84af332018-03-01 23:05:10 +08002134 return sctp_error(sk, msg->msg_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
2137/* This is an extended version of skb_pull() that removes the data from the
2138 * start of a skb even when data is spread across the list of skb's in the
2139 * frag_list. len specifies the total amount of data that needs to be removed.
2140 * when 'len' bytes could be removed from the skb, it returns 0.
2141 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2142 * could not be removed.
2143 */
2144static int sctp_skb_pull(struct sk_buff *skb, int len)
2145{
2146 struct sk_buff *list;
2147 int skb_len = skb_headlen(skb);
2148 int rlen;
2149
2150 if (len <= skb_len) {
2151 __skb_pull(skb, len);
2152 return 0;
2153 }
2154 len -= skb_len;
2155 __skb_pull(skb, skb_len);
2156
David S. Miller1b003be2009-06-09 00:22:35 -07002157 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 rlen = sctp_skb_pull(list, len);
2159 skb->len -= (len-rlen);
2160 skb->data_len -= (len-rlen);
2161
2162 if (!rlen)
2163 return 0;
2164
2165 len = rlen;
2166 }
2167
2168 return len;
2169}
2170
2171/* API 3.1.3 recvmsg() - UDP Style Syntax
2172 *
2173 * ssize_t recvmsg(int socket, struct msghdr *message,
2174 * int flags);
2175 *
2176 * socket - the socket descriptor of the endpoint.
2177 * message - pointer to the msghdr structure which contains a single
2178 * user message and possibly some ancillary data.
2179 *
2180 * See Section 5 for complete description of the data
2181 * structures.
2182 *
2183 * flags - flags sent or received with the user message, see Section
2184 * 5 for complete description of the flags.
2185 */
Ying Xue1b784142015-03-02 15:37:48 +08002186static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2187 int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
2189 struct sctp_ulpevent *event = NULL;
2190 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002191 struct sk_buff *skb, *head_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 int copied;
2193 int err = 0;
2194 int skb_len;
2195
Daniel Borkmannbb333812013-06-28 19:49:40 +02002196 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2197 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2198 addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
wangweidong048ed4b2014-01-21 15:44:11 +08002200 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03002202 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
Xin Longe0878692016-07-30 14:14:41 +08002203 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 err = -ENOTCONN;
2205 goto out;
2206 }
2207
2208 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2209 if (!skb)
2210 goto out;
2211
2212 /* Get the total length of the skb including any skb's in the
2213 * frag_list.
2214 */
2215 skb_len = skb->len;
2216
2217 copied = skb_len;
2218 if (copied > len)
2219 copied = len;
2220
David S. Miller51f3d022014-11-05 16:46:40 -05002221 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223 event = sctp_skb2event(skb);
2224
2225 if (err)
2226 goto out_free;
2227
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002228 if (event->chunk && event->chunk->head_skb)
2229 head_skb = event->chunk->head_skb;
2230 else
2231 head_skb = skb;
2232 sock_recv_ts_and_drops(msg, sk, head_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (sctp_ulpevent_is_notification(event)) {
2234 msg->msg_flags |= MSG_NOTIFICATION;
2235 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2236 } else {
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002237 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
2239
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02002240 /* Check if we allow SCTP_NXTINFO. */
2241 if (sp->recvnxtinfo)
2242 sctp_ulpevent_read_nxtinfo(event, msg, sk);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002243 /* Check if we allow SCTP_RCVINFO. */
2244 if (sp->recvrcvinfo)
2245 sctp_ulpevent_read_rcvinfo(event, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 /* Check if we allow SCTP_SNDRCVINFO. */
2247 if (sp->subscribe.sctp_data_io_event)
2248 sctp_ulpevent_read_sndrcvinfo(event, msg);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 err = copied;
2251
2252 /* If skb's length exceeds the user's buffer, update the skb and
2253 * push it back to the receive_queue so that the next call to
2254 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2255 */
2256 if (skb_len > copied) {
2257 msg->msg_flags &= ~MSG_EOR;
2258 if (flags & MSG_PEEK)
2259 goto out_free;
2260 sctp_skb_pull(skb, copied);
2261 skb_queue_head(&sk->sk_receive_queue, skb);
2262
Daniel Borkmann362d5202014-04-14 21:45:17 +02002263 /* When only partial message is copied to the user, increase
2264 * rwnd by that amount. If all the data in the skb is read,
2265 * rwnd is updated when the event is freed.
2266 */
2267 if (!sctp_ulpevent_is_notification(event))
2268 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 goto out;
2270 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2271 (event->msg_flags & MSG_EOR))
2272 msg->msg_flags |= MSG_EOR;
2273 else
2274 msg->msg_flags &= ~MSG_EOR;
2275
2276out_free:
2277 if (flags & MSG_PEEK) {
2278 /* Release the skb reference acquired after peeking the skb in
2279 * sctp_skb_recv_datagram().
2280 */
2281 kfree_skb(skb);
2282 } else {
2283 /* Free the event which includes releasing the reference to
2284 * the owner of the skb, freeing the skb and updating the
2285 * rwnd.
2286 */
2287 sctp_ulpevent_free(event);
2288 }
2289out:
wangweidong048ed4b2014-01-21 15:44:11 +08002290 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return err;
2292}
2293
2294/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2295 *
2296 * This option is a on/off flag. If enabled no SCTP message
2297 * fragmentation will be performed. Instead if a message being sent
2298 * exceeds the current PMTU size, the message will NOT be sent and
2299 * instead a error will be indicated to the user.
2300 */
2301static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002302 char __user *optval,
2303 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
2305 int val;
2306
2307 if (optlen < sizeof(int))
2308 return -EINVAL;
2309
2310 if (get_user(val, (int __user *)optval))
2311 return -EFAULT;
2312
2313 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2314
2315 return 0;
2316}
2317
2318static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002319 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Wei Yongjun94912302011-07-02 09:28:04 +00002321 struct sctp_association *asoc;
2322 struct sctp_ulpevent *event;
2323
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002324 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return -EINVAL;
2326 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2327 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002328
Daniel Borkmannbbbea412014-07-12 20:30:40 +02002329 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
Wei Yongjun94912302011-07-02 09:28:04 +00002330 * if there is no data to be sent or retransmit, the stack will
2331 * immediately send up this notification.
2332 */
2333 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2334 &sctp_sk(sk)->subscribe)) {
2335 asoc = sctp_id2assoc(sk, 0);
2336
2337 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2338 event = sctp_ulpevent_make_sender_dry_event(asoc,
Marcelo Ricardo Leitner2e83acb2018-01-08 19:02:27 -02002339 GFP_USER | __GFP_NOWARN);
Wei Yongjun94912302011-07-02 09:28:04 +00002340 if (!event)
2341 return -ENOMEM;
2342
Xin Long9162e0e2017-12-08 21:04:05 +08002343 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
Wei Yongjun94912302011-07-02 09:28:04 +00002344 }
2345 }
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return 0;
2348}
2349
2350/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2351 *
2352 * This socket option is applicable to the UDP-style socket only. When
2353 * set it will cause associations that are idle for more than the
2354 * specified number of seconds to automatically close. An association
2355 * being idle is defined an association that has NOT sent or received
2356 * user data. The special value of '0' indicates that no automatic
2357 * close of any associations should be performed. The option expects an
2358 * integer defining the number of seconds of idle time before an
2359 * association is closed.
2360 */
2361static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002362 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
2364 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman9f70f462013-12-10 06:48:15 -05002365 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 /* Applicable to UDP-style socket only */
2368 if (sctp_style(sk, TCP))
2369 return -EOPNOTSUPP;
2370 if (optlen != sizeof(int))
2371 return -EINVAL;
2372 if (copy_from_user(&sp->autoclose, optval, optlen))
2373 return -EFAULT;
2374
Neil Horman9f70f462013-12-10 06:48:15 -05002375 if (sp->autoclose > net->sctp.max_autoclose)
2376 sp->autoclose = net->sctp.max_autoclose;
2377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 return 0;
2379}
2380
2381/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2382 *
2383 * Applications can enable or disable heartbeats for any peer address of
2384 * an association, modify an address's heartbeat interval, force a
2385 * heartbeat to be sent immediately, and adjust the address's maximum
2386 * number of retransmissions sent before an address is considered
2387 * unreachable. The following structure is used to access and modify an
2388 * address's parameters:
2389 *
2390 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002391 * sctp_assoc_t spp_assoc_id;
2392 * struct sockaddr_storage spp_address;
2393 * uint32_t spp_hbinterval;
2394 * uint16_t spp_pathmaxrxt;
2395 * uint32_t spp_pathmtu;
2396 * uint32_t spp_sackdelay;
2397 * uint32_t spp_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08002398 * uint32_t spp_ipv6_flowlabel;
2399 * uint8_t spp_dscp;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002400 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002402 * spp_assoc_id - (one-to-many style socket) This is filled in the
2403 * application, and identifies the association for
2404 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 * spp_address - This specifies which address is of interest.
2406 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002407 * in milliseconds. If a value of zero
2408 * is present in this field then no changes are to
2409 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 * spp_pathmaxrxt - This contains the maximum number of
2411 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002412 * considered unreachable. If a value of zero
2413 * is present in this field then no changes are to
2414 * be made to this parameter.
2415 * spp_pathmtu - When Path MTU discovery is disabled the value
2416 * specified here will be the "fixed" path mtu.
2417 * Note that if the spp_address field is empty
2418 * then all associations on this address will
2419 * have this fixed path mtu set upon them.
2420 *
2421 * spp_sackdelay - When delayed sack is enabled, this value specifies
2422 * the number of milliseconds that sacks will be delayed
2423 * for. This value will apply to all addresses of an
2424 * association if the spp_address field is empty. Note
2425 * also, that if delayed sack is enabled and this
2426 * value is set to 0, no change is made to the last
2427 * recorded delayed sack timer value.
2428 *
2429 * spp_flags - These flags are used to control various features
2430 * on an association. The flag field may contain
2431 * zero or more of the following options.
2432 *
2433 * SPP_HB_ENABLE - Enable heartbeats on the
2434 * specified address. Note that if the address
2435 * field is empty all addresses for the association
2436 * have heartbeats enabled upon them.
2437 *
2438 * SPP_HB_DISABLE - Disable heartbeats on the
2439 * speicifed address. Note that if the address
2440 * field is empty all addresses for the association
2441 * will have their heartbeats disabled. Note also
2442 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2443 * mutually exclusive, only one of these two should
2444 * be specified. Enabling both fields will have
2445 * undetermined results.
2446 *
2447 * SPP_HB_DEMAND - Request a user initiated heartbeat
2448 * to be made immediately.
2449 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002450 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2451 * heartbeat delayis to be set to the value of 0
2452 * milliseconds.
2453 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002454 * SPP_PMTUD_ENABLE - This field will enable PMTU
2455 * discovery upon the specified address. Note that
2456 * if the address feild is empty then all addresses
2457 * on the association are effected.
2458 *
2459 * SPP_PMTUD_DISABLE - This field will disable PMTU
2460 * discovery upon the specified address. Note that
2461 * if the address feild is empty then all addresses
2462 * on the association are effected. Not also that
2463 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2464 * exclusive. Enabling both will have undetermined
2465 * results.
2466 *
2467 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2468 * on delayed sack. The time specified in spp_sackdelay
2469 * is used to specify the sack delay for this address. Note
2470 * that if spp_address is empty then all addresses will
2471 * enable delayed sack and take on the sack delay
2472 * value specified in spp_sackdelay.
2473 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2474 * off delayed sack. If the spp_address field is blank then
2475 * delayed sack is disabled for the entire association. Note
2476 * also that this field is mutually exclusive to
2477 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2478 * results.
Xin Long0b0dce72018-07-02 18:21:13 +08002479 *
2480 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
2481 * setting of the IPV6 flow label value. The value is
2482 * contained in the spp_ipv6_flowlabel field.
2483 * Upon retrieval, this flag will be set to indicate that
2484 * the spp_ipv6_flowlabel field has a valid value returned.
2485 * If a specific destination address is set (in the
2486 * spp_address field), then the value returned is that of
2487 * the address. If just an association is specified (and
2488 * no address), then the association's default flow label
2489 * is returned. If neither an association nor a destination
2490 * is specified, then the socket's default flow label is
2491 * returned. For non-IPv6 sockets, this flag will be left
2492 * cleared.
2493 *
2494 * SPP_DSCP: Setting this flag enables the setting of the
2495 * Differentiated Services Code Point (DSCP) value
2496 * associated with either the association or a specific
2497 * address. The value is obtained in the spp_dscp field.
2498 * Upon retrieval, this flag will be set to indicate that
2499 * the spp_dscp field has a valid value returned. If a
2500 * specific destination address is set when called (in the
2501 * spp_address field), then that specific destination
2502 * address's DSCP value is returned. If just an association
2503 * is specified, then the association's default DSCP is
2504 * returned. If neither an association nor a destination is
2505 * specified, then the socket's default DSCP is returned.
2506 *
2507 * spp_ipv6_flowlabel
2508 * - This field is used in conjunction with the
2509 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
2510 * The 20 least significant bits are used for the flow
2511 * label. This setting has precedence over any IPv6-layer
2512 * setting.
2513 *
2514 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
2515 * and contains the DSCP. The 6 most significant bits are
2516 * used for the DSCP. This setting has precedence over any
2517 * IPv4- or IPv6- layer setting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 */
Adrian Bunk16164362006-09-18 00:40:38 -07002519static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2520 struct sctp_transport *trans,
2521 struct sctp_association *asoc,
2522 struct sctp_sock *sp,
2523 int hb_change,
2524 int pmtud_change,
2525 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 int error;
2528
Frank Filz52ccb8e2005-12-22 11:36:46 -08002529 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002530 struct net *net = sock_net(trans->asoc->base.sk);
2531
2532 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 if (error)
2534 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
2536
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002537 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2538 * this field is ignored. Note also that a value of zero indicates
2539 * the current setting should be left unchanged.
2540 */
2541 if (params->spp_flags & SPP_HB_ENABLE) {
2542
2543 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2544 * set. This lets us use 0 value when this flag
2545 * is set.
2546 */
2547 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2548 params->spp_hbinterval = 0;
2549
2550 if (params->spp_hbinterval ||
2551 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2552 if (trans) {
2553 trans->hbinterval =
2554 msecs_to_jiffies(params->spp_hbinterval);
2555 } else if (asoc) {
2556 asoc->hbinterval =
2557 msecs_to_jiffies(params->spp_hbinterval);
2558 } else {
2559 sp->hbinterval = params->spp_hbinterval;
2560 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002561 }
2562 }
2563
2564 if (hb_change) {
2565 if (trans) {
2566 trans->param_flags =
2567 (trans->param_flags & ~SPP_HB) | hb_change;
2568 } else if (asoc) {
2569 asoc->param_flags =
2570 (asoc->param_flags & ~SPP_HB) | hb_change;
2571 } else {
2572 sp->param_flags =
2573 (sp->param_flags & ~SPP_HB) | hb_change;
2574 }
2575 }
2576
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002577 /* When Path MTU discovery is disabled the value specified here will
2578 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2579 * include the flag SPP_PMTUD_DISABLE for this field to have any
2580 * effect).
2581 */
2582 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002583 if (trans) {
2584 trans->pathmtu = params->spp_pathmtu;
Xin Long3ebfdf02017-04-04 13:39:55 +08002585 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002586 } else if (asoc) {
Marcelo Ricardo Leitnerc4b28932018-04-26 16:58:53 -03002587 sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002588 } else {
2589 sp->pathmtu = params->spp_pathmtu;
2590 }
2591 }
2592
2593 if (pmtud_change) {
2594 if (trans) {
2595 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2596 (params->spp_flags & SPP_PMTUD_ENABLE);
2597 trans->param_flags =
2598 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2599 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002600 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
Xin Long3ebfdf02017-04-04 13:39:55 +08002601 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002602 }
2603 } else if (asoc) {
2604 asoc->param_flags =
2605 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2606 } else {
2607 sp->param_flags =
2608 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2609 }
2610 }
2611
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002612 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2613 * value of this field is ignored. Note also that a value of zero
2614 * indicates the current setting should be left unchanged.
2615 */
2616 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002617 if (trans) {
2618 trans->sackdelay =
2619 msecs_to_jiffies(params->spp_sackdelay);
2620 } else if (asoc) {
2621 asoc->sackdelay =
2622 msecs_to_jiffies(params->spp_sackdelay);
2623 } else {
2624 sp->sackdelay = params->spp_sackdelay;
2625 }
2626 }
2627
2628 if (sackdelay_change) {
2629 if (trans) {
2630 trans->param_flags =
2631 (trans->param_flags & ~SPP_SACKDELAY) |
2632 sackdelay_change;
2633 } else if (asoc) {
2634 asoc->param_flags =
2635 (asoc->param_flags & ~SPP_SACKDELAY) |
2636 sackdelay_change;
2637 } else {
2638 sp->param_flags =
2639 (sp->param_flags & ~SPP_SACKDELAY) |
2640 sackdelay_change;
2641 }
2642 }
2643
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002644 /* Note that a value of zero indicates the current setting should be
2645 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002646 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002647 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002648 if (trans) {
2649 trans->pathmaxrxt = params->spp_pathmaxrxt;
2650 } else if (asoc) {
2651 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2652 } else {
2653 sp->pathmaxrxt = params->spp_pathmaxrxt;
2654 }
2655 }
2656
Xin Long0b0dce72018-07-02 18:21:13 +08002657 if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
Xin Long741880e2018-09-03 15:47:11 +08002658 if (trans) {
2659 if (trans->ipaddr.sa.sa_family == AF_INET6) {
2660 trans->flowlabel = params->spp_ipv6_flowlabel &
2661 SCTP_FLOWLABEL_VAL_MASK;
2662 trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2663 }
Xin Long0b0dce72018-07-02 18:21:13 +08002664 } else if (asoc) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002665 struct sctp_transport *t;
2666
2667 list_for_each_entry(t, &asoc->peer.transport_addr_list,
Xin Long0b0dce72018-07-02 18:21:13 +08002668 transports) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002669 if (t->ipaddr.sa.sa_family != AF_INET6)
Xin Long0b0dce72018-07-02 18:21:13 +08002670 continue;
Xin Longaf8a2b82018-09-03 15:47:10 +08002671 t->flowlabel = params->spp_ipv6_flowlabel &
2672 SCTP_FLOWLABEL_VAL_MASK;
2673 t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
Xin Long0b0dce72018-07-02 18:21:13 +08002674 }
2675 asoc->flowlabel = params->spp_ipv6_flowlabel &
2676 SCTP_FLOWLABEL_VAL_MASK;
2677 asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2678 } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
2679 sp->flowlabel = params->spp_ipv6_flowlabel &
2680 SCTP_FLOWLABEL_VAL_MASK;
2681 sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2682 }
2683 }
2684
2685 if (params->spp_flags & SPP_DSCP) {
2686 if (trans) {
2687 trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2688 trans->dscp |= SCTP_DSCP_SET_MASK;
2689 } else if (asoc) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002690 struct sctp_transport *t;
2691
2692 list_for_each_entry(t, &asoc->peer.transport_addr_list,
Xin Long0b0dce72018-07-02 18:21:13 +08002693 transports) {
Xin Longaf8a2b82018-09-03 15:47:10 +08002694 t->dscp = params->spp_dscp &
2695 SCTP_DSCP_VAL_MASK;
2696 t->dscp |= SCTP_DSCP_SET_MASK;
Xin Long0b0dce72018-07-02 18:21:13 +08002697 }
2698 asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2699 asoc->dscp |= SCTP_DSCP_SET_MASK;
2700 } else {
2701 sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2702 sp->dscp |= SCTP_DSCP_SET_MASK;
2703 }
2704 }
2705
Frank Filz52ccb8e2005-12-22 11:36:46 -08002706 return 0;
2707}
2708
2709static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002710 char __user *optval,
2711 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002712{
2713 struct sctp_paddrparams params;
2714 struct sctp_transport *trans = NULL;
2715 struct sctp_association *asoc = NULL;
2716 struct sctp_sock *sp = sctp_sk(sk);
2717 int error;
2718 int hb_change, pmtud_change, sackdelay_change;
2719
Xin Long0b0dce72018-07-02 18:21:13 +08002720 if (optlen == sizeof(params)) {
2721 if (copy_from_user(&params, optval, optlen))
2722 return -EFAULT;
2723 } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
2724 spp_ipv6_flowlabel), 4)) {
2725 if (copy_from_user(&params, optval, optlen))
2726 return -EFAULT;
2727 if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
2728 return -EINVAL;
2729 } else {
wangweidongcb3f8372013-12-23 12:16:50 +08002730 return -EINVAL;
Xin Long0b0dce72018-07-02 18:21:13 +08002731 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002732
2733 /* Validate flags and value parameters. */
2734 hb_change = params.spp_flags & SPP_HB;
2735 pmtud_change = params.spp_flags & SPP_PMTUD;
2736 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2737
2738 if (hb_change == SPP_HB ||
2739 pmtud_change == SPP_PMTUD ||
2740 sackdelay_change == SPP_SACKDELAY ||
2741 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002742 (params.spp_pathmtu &&
2743 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002744 return -EINVAL;
2745
2746 /* If an address other than INADDR_ANY is specified, and
2747 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 */
wangweidongcb3f8372013-12-23 12:16:50 +08002749 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002750 trans = sctp_addr_id2transport(sk, &params.spp_address,
2751 params.spp_assoc_id);
2752 if (!trans)
2753 return -EINVAL;
2754 }
2755
2756 /* Get association, if assoc_id != 0 and the socket is a one
2757 * to many style socket, and an association was not found, then
2758 * the id was invalid.
2759 */
2760 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2761 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2762 return -EINVAL;
2763
2764 /* Heartbeat demand can only be sent on a transport or
2765 * association, but not a socket.
2766 */
2767 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2768 return -EINVAL;
2769
2770 /* Process parameters. */
2771 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2772 hb_change, pmtud_change,
2773 sackdelay_change);
2774
2775 if (error)
2776 return error;
2777
2778 /* If changes are for association, also apply parameters to each
2779 * transport.
2780 */
2781 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002782 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2783 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002784 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2785 hb_change, pmtud_change,
2786 sackdelay_change);
2787 }
2788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
2790 return 0;
2791}
2792
wangweidong0ea5e4d2014-01-15 17:24:01 +08002793static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2794{
2795 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2796}
2797
2798static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2799{
2800 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2801}
2802
Wei Yongjund364d922008-05-09 15:13:26 -07002803/*
2804 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002805 *
Wei Yongjund364d922008-05-09 15:13:26 -07002806 * This option will effect the way delayed acks are performed. This
2807 * option allows you to get or set the delayed ack time, in
2808 * milliseconds. It also allows changing the delayed ack frequency.
2809 * Changing the frequency to 1 disables the delayed sack algorithm. If
2810 * the assoc_id is 0, then this sets or gets the endpoints default
2811 * values. If the assoc_id field is non-zero, then the set or get
2812 * effects the specified association for the one to many model (the
2813 * assoc_id field is ignored by the one to one model). Note that if
2814 * sack_delay or sack_freq are 0 when setting this option, then the
2815 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002816 *
Wei Yongjund364d922008-05-09 15:13:26 -07002817 * struct sctp_sack_info {
2818 * sctp_assoc_t sack_assoc_id;
2819 * uint32_t sack_delay;
2820 * uint32_t sack_freq;
2821 * };
Frank Filz77086102005-12-22 11:37:30 -08002822 *
Wei Yongjund364d922008-05-09 15:13:26 -07002823 * sack_assoc_id - This parameter, indicates which association the user
2824 * is performing an action upon. Note that if this field's value is
2825 * zero then the endpoints default value is changed (effecting future
2826 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002827 *
Wei Yongjund364d922008-05-09 15:13:26 -07002828 * sack_delay - This parameter contains the number of milliseconds that
2829 * the user is requesting the delayed ACK timer be set to. Note that
2830 * this value is defined in the standard to be between 200 and 500
2831 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002832 *
Wei Yongjund364d922008-05-09 15:13:26 -07002833 * sack_freq - This parameter contains the number of packets that must
2834 * be received before a sack is sent without waiting for the delay
2835 * timer to expire. The default value for this is 2, setting this
2836 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002837 */
2838
Wei Yongjund364d922008-05-09 15:13:26 -07002839static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002840 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002841{
Wei Yongjund364d922008-05-09 15:13:26 -07002842 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002843 struct sctp_transport *trans = NULL;
2844 struct sctp_association *asoc = NULL;
2845 struct sctp_sock *sp = sctp_sk(sk);
2846
Wei Yongjund364d922008-05-09 15:13:26 -07002847 if (optlen == sizeof(struct sctp_sack_info)) {
2848 if (copy_from_user(&params, optval, optlen))
2849 return -EFAULT;
2850
2851 if (params.sack_delay == 0 && params.sack_freq == 0)
2852 return 0;
2853 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05002854 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05002855 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05002856 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05002857 "Use struct sctp_sack_info instead\n",
2858 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07002859 if (copy_from_user(&params, optval, optlen))
2860 return -EFAULT;
2861
2862 if (params.sack_delay == 0)
2863 params.sack_freq = 1;
2864 else
2865 params.sack_freq = 0;
2866 } else
wangweidongcb3f8372013-12-23 12:16:50 +08002867 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08002868
Frank Filz77086102005-12-22 11:37:30 -08002869 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002870 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002871 return -EINVAL;
2872
Wei Yongjund364d922008-05-09 15:13:26 -07002873 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002874 * to many style socket, and an association was not found, then
2875 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002876 */
Wei Yongjund364d922008-05-09 15:13:26 -07002877 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2878 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002879 return -EINVAL;
2880
Wei Yongjund364d922008-05-09 15:13:26 -07002881 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002882 if (asoc) {
2883 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002884 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002885 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002886 sctp_spp_sackdelay_enable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002887 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002888 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002889 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002890 sctp_spp_sackdelay_enable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002891 }
Wei Yongjund364d922008-05-09 15:13:26 -07002892 }
2893
2894 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002895 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002896 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002897 sctp_spp_sackdelay_disable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002898 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002899 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002900 sctp_spp_sackdelay_disable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002901 }
Wei Yongjund364d922008-05-09 15:13:26 -07002902 } else if (params.sack_freq > 1) {
2903 if (asoc) {
2904 asoc->sackfreq = params.sack_freq;
2905 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002906 sctp_spp_sackdelay_enable(asoc->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002907 } else {
2908 sp->sackfreq = params.sack_freq;
2909 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002910 sctp_spp_sackdelay_enable(sp->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002911 }
Frank Filz77086102005-12-22 11:37:30 -08002912 }
2913
2914 /* If change is for association, also apply to each transport. */
2915 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002916 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2917 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002918 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002919 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002920 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002921 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002922 sctp_spp_sackdelay_enable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002923 }
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07002924 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002925 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002926 sctp_spp_sackdelay_disable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002927 } else if (params.sack_freq > 1) {
2928 trans->sackfreq = params.sack_freq;
2929 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002930 sctp_spp_sackdelay_enable(trans->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002931 }
2932 }
2933 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002934
Frank Filz77086102005-12-22 11:37:30 -08002935 return 0;
2936}
2937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2939 *
2940 * Applications can specify protocol parameters for the default association
2941 * initialization. The option name argument to setsockopt() and getsockopt()
2942 * is SCTP_INITMSG.
2943 *
2944 * Setting initialization parameters is effective only on an unconnected
2945 * socket (for UDP-style sockets only future associations are effected
2946 * by the change). With TCP-style sockets, this option is inherited by
2947 * sockets derived from a listener socket.
2948 */
David S. Millerb7058842009-09-30 16:12:20 -07002949static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950{
2951 struct sctp_initmsg sinit;
2952 struct sctp_sock *sp = sctp_sk(sk);
2953
2954 if (optlen != sizeof(struct sctp_initmsg))
2955 return -EINVAL;
2956 if (copy_from_user(&sinit, optval, optlen))
2957 return -EFAULT;
2958
2959 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002960 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002962 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002964 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002966 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 return 0;
2969}
2970
2971/*
2972 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2973 *
2974 * Applications that wish to use the sendto() system call may wish to
2975 * specify a default set of parameters that would normally be supplied
2976 * through the inclusion of ancillary data. This socket option allows
2977 * such an application to set the default sctp_sndrcvinfo structure.
2978 * The application that wishes to use this socket option simply passes
2979 * in to this call the sctp_sndrcvinfo structure defined in Section
2980 * 5.2.2) The input parameters accepted by this call include
2981 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2982 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2983 * to this call if the caller is using the UDP model.
2984 */
2985static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002986 char __user *optval,
2987 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002990 struct sctp_association *asoc;
2991 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002993 if (optlen != sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 return -EINVAL;
2995 if (copy_from_user(&info, optval, optlen))
2996 return -EFAULT;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002997 if (info.sinfo_flags &
2998 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2999 SCTP_ABORT | SCTP_EOF))
3000 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
3002 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
3003 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
3004 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 if (asoc) {
3006 asoc->default_stream = info.sinfo_stream;
3007 asoc->default_flags = info.sinfo_flags;
3008 asoc->default_ppid = info.sinfo_ppid;
3009 asoc->default_context = info.sinfo_context;
3010 asoc->default_timetolive = info.sinfo_timetolive;
3011 } else {
3012 sp->default_stream = info.sinfo_stream;
3013 sp->default_flags = info.sinfo_flags;
3014 sp->default_ppid = info.sinfo_ppid;
3015 sp->default_context = info.sinfo_context;
3016 sp->default_timetolive = info.sinfo_timetolive;
3017 }
3018
3019 return 0;
3020}
3021
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003022/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
3023 * (SCTP_DEFAULT_SNDINFO)
3024 */
3025static int sctp_setsockopt_default_sndinfo(struct sock *sk,
3026 char __user *optval,
3027 unsigned int optlen)
3028{
3029 struct sctp_sock *sp = sctp_sk(sk);
3030 struct sctp_association *asoc;
3031 struct sctp_sndinfo info;
3032
3033 if (optlen != sizeof(info))
3034 return -EINVAL;
3035 if (copy_from_user(&info, optval, optlen))
3036 return -EFAULT;
3037 if (info.snd_flags &
3038 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3039 SCTP_ABORT | SCTP_EOF))
3040 return -EINVAL;
3041
3042 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
3043 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
3044 return -EINVAL;
3045 if (asoc) {
3046 asoc->default_stream = info.snd_sid;
3047 asoc->default_flags = info.snd_flags;
3048 asoc->default_ppid = info.snd_ppid;
3049 asoc->default_context = info.snd_context;
3050 } else {
3051 sp->default_stream = info.snd_sid;
3052 sp->default_flags = info.snd_flags;
3053 sp->default_ppid = info.snd_ppid;
3054 sp->default_context = info.snd_context;
3055 }
3056
3057 return 0;
3058}
3059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3061 *
3062 * Requests that the local SCTP stack use the enclosed peer address as
3063 * the association primary. The enclosed address must be one of the
3064 * association peer's addresses.
3065 */
3066static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003067 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
3069 struct sctp_prim prim;
3070 struct sctp_transport *trans;
Richard Haines2277c7c2018-02-13 20:56:24 +00003071 struct sctp_af *af;
3072 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
3074 if (optlen != sizeof(struct sctp_prim))
3075 return -EINVAL;
3076
3077 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3078 return -EFAULT;
3079
Richard Haines2277c7c2018-02-13 20:56:24 +00003080 /* Allow security module to validate address but need address len. */
3081 af = sctp_get_af_specific(prim.ssp_addr.ss_family);
3082 if (!af)
3083 return -EINVAL;
3084
3085 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
3086 (struct sockaddr *)&prim.ssp_addr,
3087 af->sockaddr_len);
3088 if (err)
3089 return err;
3090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
3092 if (!trans)
3093 return -EINVAL;
3094
3095 sctp_assoc_set_primary(trans->asoc, trans);
3096
3097 return 0;
3098}
3099
3100/*
3101 * 7.1.5 SCTP_NODELAY
3102 *
3103 * Turn on/off any Nagle-like algorithm. This means that packets are
3104 * generally sent as soon as possible and no unnecessary delays are
3105 * introduced, at the cost of more packets in the network. Expects an
3106 * integer boolean flag.
3107 */
3108static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003109 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
3111 int val;
3112
3113 if (optlen < sizeof(int))
3114 return -EINVAL;
3115 if (get_user(val, (int __user *)optval))
3116 return -EFAULT;
3117
3118 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3119 return 0;
3120}
3121
3122/*
3123 *
3124 * 7.1.1 SCTP_RTOINFO
3125 *
3126 * The protocol parameters used to initialize and bound retransmission
3127 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3128 * and modify these parameters.
3129 * All parameters are time values, in milliseconds. A value of 0, when
3130 * modifying the parameters, indicates that the current value should not
3131 * be changed.
3132 *
3133 */
David S. Millerb7058842009-09-30 16:12:20 -07003134static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3135{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 struct sctp_rtoinfo rtoinfo;
3137 struct sctp_association *asoc;
wangweidong85f935d2013-12-11 09:50:38 +08003138 unsigned long rto_min, rto_max;
3139 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
3141 if (optlen != sizeof (struct sctp_rtoinfo))
3142 return -EINVAL;
3143
3144 if (copy_from_user(&rtoinfo, optval, optlen))
3145 return -EFAULT;
3146
3147 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3148
3149 /* Set the values to the specific association */
3150 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3151 return -EINVAL;
3152
wangweidong85f935d2013-12-11 09:50:38 +08003153 rto_max = rtoinfo.srto_max;
3154 rto_min = rtoinfo.srto_min;
3155
3156 if (rto_max)
3157 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3158 else
3159 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3160
3161 if (rto_min)
3162 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3163 else
3164 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3165
3166 if (rto_min > rto_max)
3167 return -EINVAL;
3168
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 if (asoc) {
3170 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003171 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 msecs_to_jiffies(rtoinfo.srto_initial);
wangweidong85f935d2013-12-11 09:50:38 +08003173 asoc->rto_max = rto_max;
3174 asoc->rto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 } else {
3176 /* If there is no association or the association-id = 0
3177 * set the values to the endpoint.
3178 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 if (rtoinfo.srto_initial != 0)
3180 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
wangweidong85f935d2013-12-11 09:50:38 +08003181 sp->rtoinfo.srto_max = rto_max;
3182 sp->rtoinfo.srto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 }
3184
3185 return 0;
3186}
3187
3188/*
3189 *
3190 * 7.1.2 SCTP_ASSOCINFO
3191 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02003192 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 * of the association.
3194 * Returns an error if the new association retransmission value is
3195 * greater than the sum of the retransmission value of the peer.
3196 * See [SCTP] for more information.
3197 *
3198 */
David S. Millerb7058842009-09-30 16:12:20 -07003199static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
3201
3202 struct sctp_assocparams assocparams;
3203 struct sctp_association *asoc;
3204
3205 if (optlen != sizeof(struct sctp_assocparams))
3206 return -EINVAL;
3207 if (copy_from_user(&assocparams, optval, optlen))
3208 return -EFAULT;
3209
3210 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3211
3212 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3213 return -EINVAL;
3214
3215 /* Set the values to the specific association */
3216 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003217 if (assocparams.sasoc_asocmaxrxt != 0) {
3218 __u32 path_sum = 0;
3219 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003220 struct sctp_transport *peer_addr;
3221
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07003222 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3223 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003224 path_sum += peer_addr->pathmaxrxt;
3225 paths++;
3226 }
3227
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003228 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003229 * one path/transport. We do this because path
3230 * retransmissions are only counted when we have more
3231 * then one path.
3232 */
3233 if (paths > 1 &&
3234 assocparams.sasoc_asocmaxrxt > path_sum)
3235 return -EINVAL;
3236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003238 }
3239
Daniel Borkmann52db8822013-06-25 18:17:27 +02003240 if (assocparams.sasoc_cookie_life != 0)
3241 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 } else {
3243 /* Set the values to the endpoint */
3244 struct sctp_sock *sp = sctp_sk(sk);
3245
3246 if (assocparams.sasoc_asocmaxrxt != 0)
3247 sp->assocparams.sasoc_asocmaxrxt =
3248 assocparams.sasoc_asocmaxrxt;
3249 if (assocparams.sasoc_cookie_life != 0)
3250 sp->assocparams.sasoc_cookie_life =
3251 assocparams.sasoc_cookie_life;
3252 }
3253 return 0;
3254}
3255
3256/*
3257 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3258 *
3259 * This socket option is a boolean flag which turns on or off mapped V4
3260 * addresses. If this option is turned on and the socket is type
3261 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3262 * If this option is turned off, then no mapping will be done of V4
3263 * addresses and a user will receive both PF_INET6 and PF_INET type
3264 * addresses on the socket.
3265 */
David S. Millerb7058842009-09-30 16:12:20 -07003266static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
3268 int val;
3269 struct sctp_sock *sp = sctp_sk(sk);
3270
3271 if (optlen < sizeof(int))
3272 return -EINVAL;
3273 if (get_user(val, (int __user *)optval))
3274 return -EFAULT;
3275 if (val)
3276 sp->v4mapped = 1;
3277 else
3278 sp->v4mapped = 0;
3279
3280 return 0;
3281}
3282
3283/*
Wei Yongjune89c2092008-12-25 16:54:58 -08003284 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3285 * This option will get or set the maximum size to put in any outgoing
3286 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 * fragmented by SCTP into the specified size. Note that the underlying
3288 * SCTP implementation may fragment into smaller sized chunks when the
3289 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08003290 * the user. The default value for this option is '0' which indicates
3291 * the user is NOT limiting fragmentation and only the PMTU will effect
3292 * SCTP's choice of DATA chunk size. Note also that values set larger
3293 * than the maximum size of an IP datagram will effectively let SCTP
3294 * control fragmentation (i.e. the same as setting this option to 0).
3295 *
3296 * The following structure is used to access and modify this parameter:
3297 *
3298 * struct sctp_assoc_value {
3299 * sctp_assoc_t assoc_id;
3300 * uint32_t assoc_value;
3301 * };
3302 *
3303 * assoc_id: This parameter is ignored for one-to-one style sockets.
3304 * For one-to-many style sockets this parameter indicates which
3305 * association the user is performing an action upon. Note that if
3306 * this field's value is zero then the endpoints default value is
3307 * changed (effecting future associations only).
3308 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 */
David S. Millerb7058842009-09-30 16:12:20 -07003310static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
Xin Longecca8f82017-11-17 14:11:11 +08003312 struct sctp_sock *sp = sctp_sk(sk);
Wei Yongjune89c2092008-12-25 16:54:58 -08003313 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 int val;
3316
Wei Yongjune89c2092008-12-25 16:54:58 -08003317 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003318 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003319 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003320 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003321 "Use struct sctp_assoc_value instead\n",
3322 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08003323 if (copy_from_user(&val, optval, optlen))
3324 return -EFAULT;
3325 params.assoc_id = 0;
3326 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3327 if (copy_from_user(&params, optval, optlen))
3328 return -EFAULT;
3329 val = params.assoc_value;
Xin Longecca8f82017-11-17 14:11:11 +08003330 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 return -EINVAL;
Xin Longecca8f82017-11-17 14:11:11 +08003332 }
Wei Yongjune89c2092008-12-25 16:54:58 -08003333
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003334 asoc = sctp_id2assoc(sk, params.assoc_id);
3335
Xin Longecca8f82017-11-17 14:11:11 +08003336 if (val) {
3337 int min_len, max_len;
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003338 __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
3339 sizeof(struct sctp_data_chunk);
Xin Longecca8f82017-11-17 14:11:11 +08003340
Marcelo Ricardo Leitnerfeddd6c2018-04-26 16:58:54 -03003341 min_len = sctp_mtu_payload(sp, SCTP_DEFAULT_MINSEGMENT,
Marcelo Ricardo Leitner439ef032018-04-26 16:59:01 -03003342 datasize);
3343 max_len = SCTP_MAX_CHUNK_LEN - datasize;
Xin Longecca8f82017-11-17 14:11:11 +08003344
3345 if (val < min_len || val > max_len)
3346 return -EINVAL;
3347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Wei Yongjune89c2092008-12-25 16:54:58 -08003349 if (asoc) {
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003350 asoc->user_frag = val;
Marcelo Ricardo Leitner2f5e3c92018-04-26 16:58:55 -03003351 sctp_assoc_update_frag_point(asoc);
Wei Yongjune89c2092008-12-25 16:54:58 -08003352 } else {
Xin Longecca8f82017-11-17 14:11:11 +08003353 if (params.assoc_id && sctp_style(sk, UDP))
3354 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003355 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 }
3357
3358 return 0;
3359}
3360
3361
3362/*
3363 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3364 *
3365 * Requests that the peer mark the enclosed address as the association
3366 * primary. The enclosed address must be one of the association's
3367 * locally bound addresses. The following structure is used to make a
3368 * set primary request:
3369 */
3370static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003371 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003373 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 struct sctp_association *asoc = NULL;
3376 struct sctp_setpeerprim prim;
3377 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003378 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 int err;
3380
3381 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003383 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 return -EPERM;
3385
3386 if (optlen != sizeof(struct sctp_setpeerprim))
3387 return -EINVAL;
3388
3389 if (copy_from_user(&prim, optval, optlen))
3390 return -EFAULT;
3391
3392 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003393 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 return -EINVAL;
3395
3396 if (!asoc->peer.asconf_capable)
3397 return -EPERM;
3398
3399 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3400 return -EPERM;
3401
3402 if (!sctp_state(asoc, ESTABLISHED))
3403 return -ENOTCONN;
3404
Wei Yongjun40a01032010-12-07 17:11:09 +00003405 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3406 if (!af)
3407 return -EINVAL;
3408
3409 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3410 return -EADDRNOTAVAIL;
3411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3413 return -EADDRNOTAVAIL;
3414
Richard Haines2277c7c2018-02-13 20:56:24 +00003415 /* Allow security module to validate address. */
3416 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3417 (struct sockaddr *)&prim.sspp_addr,
3418 af->sockaddr_len);
3419 if (err)
3420 return err;
3421
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3423 chunk = sctp_make_asconf_set_prim(asoc,
3424 (union sctp_addr *)&prim.sspp_addr);
3425 if (!chunk)
3426 return -ENOMEM;
3427
3428 err = sctp_send_asconf(asoc, chunk);
3429
Daniel Borkmannbb333812013-06-28 19:49:40 +02003430 pr_debug("%s: we set peer primary addr primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
3432 return err;
3433}
3434
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003435static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003436 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003438 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003440 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003442 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 return -EFAULT;
3444
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003445 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
3447 return 0;
3448}
3449
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003450/*
3451 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3452 *
3453 * The context field in the sctp_sndrcvinfo structure is normally only
3454 * used when a failed message is retrieved holding the value that was
3455 * sent down on the actual send call. This option allows the setting of
3456 * a default context on an association basis that will be received on
3457 * reading messages from the peer. This is especially helpful in the
3458 * one-2-many model for an application to keep some reference to an
3459 * internal state machine that is processing messages on the
3460 * association. Note that the setting of this value only effects
3461 * received messages from the peer and does not effect the value that is
3462 * saved with outbound messages.
3463 */
3464static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003465 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003466{
3467 struct sctp_assoc_value params;
3468 struct sctp_sock *sp;
3469 struct sctp_association *asoc;
3470
3471 if (optlen != sizeof(struct sctp_assoc_value))
3472 return -EINVAL;
3473 if (copy_from_user(&params, optval, optlen))
3474 return -EFAULT;
3475
3476 sp = sctp_sk(sk);
3477
3478 if (params.assoc_id != 0) {
3479 asoc = sctp_id2assoc(sk, params.assoc_id);
3480 if (!asoc)
3481 return -EINVAL;
3482 asoc->default_rcv_context = params.assoc_value;
3483 } else {
3484 sp->default_rcv_context = params.assoc_value;
3485 }
3486
3487 return 0;
3488}
3489
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003490/*
3491 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3492 *
3493 * This options will at a minimum specify if the implementation is doing
3494 * fragmented interleave. Fragmented interleave, for a one to many
3495 * socket, is when subsequent calls to receive a message may return
3496 * parts of messages from different associations. Some implementations
3497 * may allow you to turn this value on or off. If so, when turned off,
3498 * no fragment interleave will occur (which will cause a head of line
3499 * blocking amongst multiple associations sharing the same one to many
3500 * socket). When this option is turned on, then each receive call may
3501 * come from a different association (thus the user must receive data
3502 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3503 * association each receive belongs to.
3504 *
3505 * This option takes a boolean value. A non-zero value indicates that
3506 * fragmented interleave is on. A value of zero indicates that
3507 * fragmented interleave is off.
3508 *
3509 * Note that it is important that an implementation that allows this
3510 * option to be turned on, have it off by default. Otherwise an unaware
3511 * application using the one to many model may become confused and act
3512 * incorrectly.
3513 */
3514static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3515 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003516 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003517{
3518 int val;
3519
3520 if (optlen != sizeof(int))
3521 return -EINVAL;
3522 if (get_user(val, (int __user *)optval))
3523 return -EFAULT;
3524
Xin Long772a5862017-12-08 21:03:58 +08003525 sctp_sk(sk)->frag_interleave = !!val;
3526
3527 if (!sctp_sk(sk)->frag_interleave)
3528 sctp_sk(sk)->strm_interleave = 0;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003529
3530 return 0;
3531}
3532
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003533/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003534 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003535 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003536 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003537 * This option will set or get the SCTP partial delivery point. This
3538 * point is the size of a message where the partial delivery API will be
3539 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003540 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003541 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003542 * point. Note also that the call will fail if the user attempts to set
3543 * this value larger than the socket receive buffer size.
3544 *
3545 * Note that any single message having a length smaller than or equal to
3546 * the SCTP partial delivery point will be delivered in one single read
3547 * call as long as the user provided buffer is large enough to hold the
3548 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003549 */
3550static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3551 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003552 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003553{
3554 u32 val;
3555
3556 if (optlen != sizeof(u32))
3557 return -EINVAL;
3558 if (get_user(val, (int __user *)optval))
3559 return -EFAULT;
3560
Wei Yongjun8510b932008-12-25 16:59:03 -08003561 /* Note: We double the receive buffer from what the user sets
3562 * it to be, also initial rwnd is based on rcvbuf/2.
3563 */
3564 if (val > (sk->sk_rcvbuf >> 1))
3565 return -EINVAL;
3566
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003567 sctp_sk(sk)->pd_point = val;
3568
3569 return 0; /* is this the right error code? */
3570}
3571
Vlad Yasevich70331572007-03-23 11:34:36 -07003572/*
3573 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3574 *
3575 * This option will allow a user to change the maximum burst of packets
3576 * that can be emitted by this association. Note that the default value
3577 * is 4, and some implementations may restrict this setting so that it
3578 * can only be lowered.
3579 *
3580 * NOTE: This text doesn't seem right. Do this on a socket basis with
3581 * future associations inheriting the socket value.
3582 */
3583static int sctp_setsockopt_maxburst(struct sock *sk,
3584 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003585 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003586{
Neil Horman219b99a2008-03-05 13:44:46 -08003587 struct sctp_assoc_value params;
3588 struct sctp_sock *sp;
3589 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003590 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003591 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003592
Neil Horman219b99a2008-03-05 13:44:46 -08003593 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003594 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003595 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003596 "Use of int in max_burst socket option deprecated.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003597 "Use struct sctp_assoc_value instead\n",
3598 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08003599 if (copy_from_user(&val, optval, optlen))
3600 return -EFAULT;
3601 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3602 if (copy_from_user(&params, optval, optlen))
3603 return -EFAULT;
3604 val = params.assoc_value;
3605 assoc_id = params.assoc_id;
3606 } else
3607 return -EINVAL;
3608
3609 sp = sctp_sk(sk);
3610
3611 if (assoc_id != 0) {
3612 asoc = sctp_id2assoc(sk, assoc_id);
3613 if (!asoc)
3614 return -EINVAL;
3615 asoc->max_burst = val;
3616 } else
3617 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003618
3619 return 0;
3620}
3621
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003622/*
3623 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3624 *
3625 * This set option adds a chunk type that the user is requesting to be
3626 * received only in an authenticated way. Changes to the list of chunks
3627 * will only effect future associations on the socket.
3628 */
3629static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003630 char __user *optval,
3631 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003632{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003633 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003634 struct sctp_authchunk val;
3635
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003636 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003637 return -EACCES;
3638
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003639 if (optlen != sizeof(struct sctp_authchunk))
3640 return -EINVAL;
3641 if (copy_from_user(&val, optval, optlen))
3642 return -EFAULT;
3643
3644 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003645 case SCTP_CID_INIT:
3646 case SCTP_CID_INIT_ACK:
3647 case SCTP_CID_SHUTDOWN_COMPLETE:
3648 case SCTP_CID_AUTH:
3649 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003650 }
3651
3652 /* add this chunk id to the endpoint */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003653 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003654}
3655
3656/*
3657 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3658 *
3659 * This option gets or sets the list of HMAC algorithms that the local
3660 * endpoint requires the peer to use.
3661 */
3662static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003663 char __user *optval,
3664 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003665{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003666 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003667 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003668 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003669 int err;
3670
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003671 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003672 return -EACCES;
3673
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003674 if (optlen < sizeof(struct sctp_hmacalgo))
3675 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003676 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3677 SCTP_AUTH_NUM_HMACS * sizeof(u16));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003678
wangweidongcb3f8372013-12-23 12:16:50 +08003679 hmacs = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003680 if (IS_ERR(hmacs))
3681 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003682
Vlad Yasevichd9724052008-08-27 16:09:49 -07003683 idents = hmacs->shmac_num_idents;
3684 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3685 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003686 err = -EINVAL;
3687 goto out;
3688 }
3689
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003690 err = sctp_auth_ep_set_hmacs(ep, hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003691out:
3692 kfree(hmacs);
3693 return err;
3694}
3695
3696/*
3697 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3698 *
3699 * This option will set a shared secret key which is used to build an
3700 * association shared key.
3701 */
3702static int sctp_setsockopt_auth_key(struct sock *sk,
3703 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003704 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003705{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003706 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003707 struct sctp_authkey *authkey;
3708 struct sctp_association *asoc;
3709 int ret;
3710
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003711 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003712 return -EACCES;
3713
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003714 if (optlen <= sizeof(struct sctp_authkey))
3715 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003716 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3717 * this.
3718 */
3719 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3720 sizeof(struct sctp_authkey));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003721
wangweidongcb3f8372013-12-23 12:16:50 +08003722 authkey = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003723 if (IS_ERR(authkey))
3724 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003725
Vlad Yasevich328fc472008-08-27 16:08:54 -07003726 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003727 ret = -EINVAL;
3728 goto out;
3729 }
3730
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003731 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3732 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3733 ret = -EINVAL;
3734 goto out;
3735 }
3736
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003737 ret = sctp_auth_set_key(ep, asoc, authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003738out:
Daniel Borkmann6ba542a2013-02-08 03:04:34 +00003739 kzfree(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003740 return ret;
3741}
3742
3743/*
3744 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3745 *
3746 * This option will get or set the active shared key to be used to build
3747 * the association shared key.
3748 */
3749static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003750 char __user *optval,
3751 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003752{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003753 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003754 struct sctp_authkeyid val;
3755 struct sctp_association *asoc;
3756
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003757 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003758 return -EACCES;
3759
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003760 if (optlen != sizeof(struct sctp_authkeyid))
3761 return -EINVAL;
3762 if (copy_from_user(&val, optval, optlen))
3763 return -EFAULT;
3764
3765 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3766 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3767 return -EINVAL;
3768
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003769 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003770}
3771
3772/*
3773 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3774 *
3775 * This set option will delete a shared secret key from use.
3776 */
3777static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003778 char __user *optval,
3779 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003780{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003781 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003782 struct sctp_authkeyid val;
3783 struct sctp_association *asoc;
3784
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003785 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003786 return -EACCES;
3787
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003788 if (optlen != sizeof(struct sctp_authkeyid))
3789 return -EINVAL;
3790 if (copy_from_user(&val, optval, optlen))
3791 return -EFAULT;
3792
3793 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3794 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3795 return -EINVAL;
3796
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003797 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003798
3799}
3800
Michio Honda7dc04d72011-04-26 20:16:31 +09003801/*
Xin Long601590e2018-03-14 19:05:32 +08003802 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
3803 *
3804 * This set option will deactivate a shared secret key.
3805 */
3806static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
3807 unsigned int optlen)
3808{
3809 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3810 struct sctp_authkeyid val;
3811 struct sctp_association *asoc;
3812
3813 if (!ep->auth_enable)
3814 return -EACCES;
3815
3816 if (optlen != sizeof(struct sctp_authkeyid))
3817 return -EINVAL;
3818 if (copy_from_user(&val, optval, optlen))
3819 return -EFAULT;
3820
3821 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3822 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3823 return -EINVAL;
3824
3825 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3826}
3827
3828/*
Michio Honda7dc04d72011-04-26 20:16:31 +09003829 * 8.1.23 SCTP_AUTO_ASCONF
3830 *
3831 * This option will enable or disable the use of the automatic generation of
3832 * ASCONF chunks to add and delete addresses to an existing association. Note
3833 * that this option has two caveats namely: a) it only affects sockets that
3834 * are bound to all addresses available to the SCTP stack, and b) the system
3835 * administrator may have an overriding control that turns the ASCONF feature
3836 * off no matter what setting the socket option may have.
3837 * This option expects an integer boolean flag, where a non-zero value turns on
3838 * the option, and a zero value turns off the option.
3839 * Note. In this implementation, socket operation overrides default parameter
3840 * being set by sysctl as well as FreeBSD implementation
3841 */
3842static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3843 unsigned int optlen)
3844{
3845 int val;
3846 struct sctp_sock *sp = sctp_sk(sk);
3847
3848 if (optlen < sizeof(int))
3849 return -EINVAL;
3850 if (get_user(val, (int __user *)optval))
3851 return -EFAULT;
3852 if (!sctp_is_ep_boundall(sk) && val)
3853 return -EINVAL;
3854 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3855 return 0;
3856
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003857 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003858 if (val == 0 && sp->do_auto_asconf) {
3859 list_del(&sp->auto_asconf_list);
3860 sp->do_auto_asconf = 0;
3861 } else if (val && !sp->do_auto_asconf) {
3862 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00003863 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09003864 sp->do_auto_asconf = 1;
3865 }
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003866 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003867 return 0;
3868}
3869
Neil Horman5aa93bc2012-07-21 07:56:07 +00003870/*
3871 * SCTP_PEER_ADDR_THLDS
3872 *
3873 * This option allows us to alter the partially failed threshold for one or all
3874 * transports in an association. See Section 6.1 of:
3875 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3876 */
3877static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3878 char __user *optval,
3879 unsigned int optlen)
3880{
3881 struct sctp_paddrthlds val;
3882 struct sctp_transport *trans;
3883 struct sctp_association *asoc;
3884
3885 if (optlen < sizeof(struct sctp_paddrthlds))
3886 return -EINVAL;
3887 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3888 sizeof(struct sctp_paddrthlds)))
3889 return -EFAULT;
3890
3891
3892 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3893 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3894 if (!asoc)
3895 return -ENOENT;
3896 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3897 transports) {
3898 if (val.spt_pathmaxrxt)
3899 trans->pathmaxrxt = val.spt_pathmaxrxt;
3900 trans->pf_retrans = val.spt_pathpfthld;
3901 }
3902
3903 if (val.spt_pathmaxrxt)
3904 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3905 asoc->pf_retrans = val.spt_pathpfthld;
3906 } else {
3907 trans = sctp_addr_id2transport(sk, &val.spt_address,
3908 val.spt_assoc_id);
3909 if (!trans)
3910 return -ENOENT;
3911
3912 if (val.spt_pathmaxrxt)
3913 trans->pathmaxrxt = val.spt_pathmaxrxt;
3914 trans->pf_retrans = val.spt_pathpfthld;
3915 }
3916
3917 return 0;
3918}
3919
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02003920static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3921 char __user *optval,
3922 unsigned int optlen)
3923{
3924 int val;
3925
3926 if (optlen < sizeof(int))
3927 return -EINVAL;
3928 if (get_user(val, (int __user *) optval))
3929 return -EFAULT;
3930
3931 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3932
3933 return 0;
3934}
3935
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02003936static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3937 char __user *optval,
3938 unsigned int optlen)
3939{
3940 int val;
3941
3942 if (optlen < sizeof(int))
3943 return -EINVAL;
3944 if (get_user(val, (int __user *) optval))
3945 return -EFAULT;
3946
3947 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3948
3949 return 0;
3950}
3951
Xin Long28aa4c22016-07-09 19:47:40 +08003952static int sctp_setsockopt_pr_supported(struct sock *sk,
3953 char __user *optval,
3954 unsigned int optlen)
3955{
3956 struct sctp_assoc_value params;
3957 struct sctp_association *asoc;
3958 int retval = -EINVAL;
3959
3960 if (optlen != sizeof(params))
3961 goto out;
3962
3963 if (copy_from_user(&params, optval, optlen)) {
3964 retval = -EFAULT;
3965 goto out;
3966 }
3967
3968 asoc = sctp_id2assoc(sk, params.assoc_id);
3969 if (asoc) {
3970 asoc->prsctp_enable = !!params.assoc_value;
3971 } else if (!params.assoc_id) {
3972 struct sctp_sock *sp = sctp_sk(sk);
3973
3974 sp->ep->prsctp_enable = !!params.assoc_value;
3975 } else {
3976 goto out;
3977 }
3978
3979 retval = 0;
3980
3981out:
3982 return retval;
3983}
3984
Xin Longf959fb42016-07-09 19:47:41 +08003985static int sctp_setsockopt_default_prinfo(struct sock *sk,
3986 char __user *optval,
3987 unsigned int optlen)
3988{
3989 struct sctp_default_prinfo info;
3990 struct sctp_association *asoc;
3991 int retval = -EINVAL;
3992
3993 if (optlen != sizeof(info))
3994 goto out;
3995
3996 if (copy_from_user(&info, optval, sizeof(info))) {
3997 retval = -EFAULT;
3998 goto out;
3999 }
4000
4001 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
4002 goto out;
4003
4004 if (info.pr_policy == SCTP_PR_SCTP_NONE)
4005 info.pr_value = 0;
4006
4007 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
4008 if (asoc) {
4009 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4010 asoc->default_timetolive = info.pr_value;
4011 } else if (!info.pr_assoc_id) {
4012 struct sctp_sock *sp = sctp_sk(sk);
4013
4014 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
4015 sp->default_timetolive = info.pr_value;
4016 } else {
4017 goto out;
4018 }
4019
4020 retval = 0;
4021
4022out:
4023 return retval;
4024}
4025
Xin Longc0d8bab2017-03-10 12:11:12 +08004026static int sctp_setsockopt_reconfig_supported(struct sock *sk,
4027 char __user *optval,
4028 unsigned int optlen)
4029{
4030 struct sctp_assoc_value params;
4031 struct sctp_association *asoc;
4032 int retval = -EINVAL;
4033
4034 if (optlen != sizeof(params))
4035 goto out;
4036
4037 if (copy_from_user(&params, optval, optlen)) {
4038 retval = -EFAULT;
4039 goto out;
4040 }
4041
4042 asoc = sctp_id2assoc(sk, params.assoc_id);
4043 if (asoc) {
4044 asoc->reconf_enable = !!params.assoc_value;
4045 } else if (!params.assoc_id) {
4046 struct sctp_sock *sp = sctp_sk(sk);
4047
4048 sp->ep->reconf_enable = !!params.assoc_value;
4049 } else {
4050 goto out;
4051 }
4052
4053 retval = 0;
4054
4055out:
4056 return retval;
4057}
4058
Xin Long9fb657a2017-01-18 00:44:46 +08004059static int sctp_setsockopt_enable_strreset(struct sock *sk,
4060 char __user *optval,
4061 unsigned int optlen)
4062{
4063 struct sctp_assoc_value params;
4064 struct sctp_association *asoc;
4065 int retval = -EINVAL;
4066
4067 if (optlen != sizeof(params))
4068 goto out;
4069
4070 if (copy_from_user(&params, optval, optlen)) {
4071 retval = -EFAULT;
4072 goto out;
4073 }
4074
4075 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
4076 goto out;
4077
4078 asoc = sctp_id2assoc(sk, params.assoc_id);
4079 if (asoc) {
4080 asoc->strreset_enable = params.assoc_value;
4081 } else if (!params.assoc_id) {
4082 struct sctp_sock *sp = sctp_sk(sk);
4083
4084 sp->ep->strreset_enable = params.assoc_value;
4085 } else {
4086 goto out;
4087 }
4088
4089 retval = 0;
4090
4091out:
4092 return retval;
4093}
4094
Xin Long7f9d68a2017-01-18 00:44:47 +08004095static int sctp_setsockopt_reset_streams(struct sock *sk,
4096 char __user *optval,
4097 unsigned int optlen)
4098{
4099 struct sctp_reset_streams *params;
4100 struct sctp_association *asoc;
4101 int retval = -EINVAL;
4102
Xin Long2342b8d2017-12-10 15:40:51 +08004103 if (optlen < sizeof(*params))
Xin Long7f9d68a2017-01-18 00:44:47 +08004104 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02004105 /* srs_number_streams is u16, so optlen can't be bigger than this. */
4106 optlen = min_t(unsigned int, optlen, USHRT_MAX +
4107 sizeof(__u16) * sizeof(*params));
Xin Long7f9d68a2017-01-18 00:44:47 +08004108
4109 params = memdup_user(optval, optlen);
4110 if (IS_ERR(params))
4111 return PTR_ERR(params);
4112
Xin Long2342b8d2017-12-10 15:40:51 +08004113 if (params->srs_number_streams * sizeof(__u16) >
4114 optlen - sizeof(*params))
4115 goto out;
4116
Xin Long7f9d68a2017-01-18 00:44:47 +08004117 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4118 if (!asoc)
4119 goto out;
4120
4121 retval = sctp_send_reset_streams(asoc, params);
4122
4123out:
4124 kfree(params);
4125 return retval;
4126}
4127
Xin Longa92ce1a2017-02-09 01:18:18 +08004128static int sctp_setsockopt_reset_assoc(struct sock *sk,
4129 char __user *optval,
4130 unsigned int optlen)
4131{
4132 struct sctp_association *asoc;
4133 sctp_assoc_t associd;
4134 int retval = -EINVAL;
4135
4136 if (optlen != sizeof(associd))
4137 goto out;
4138
4139 if (copy_from_user(&associd, optval, optlen)) {
4140 retval = -EFAULT;
4141 goto out;
4142 }
4143
4144 asoc = sctp_id2assoc(sk, associd);
4145 if (!asoc)
4146 goto out;
4147
4148 retval = sctp_send_reset_assoc(asoc);
4149
4150out:
4151 return retval;
4152}
4153
Xin Long242bd2d2017-02-09 01:18:20 +08004154static int sctp_setsockopt_add_streams(struct sock *sk,
4155 char __user *optval,
4156 unsigned int optlen)
4157{
4158 struct sctp_association *asoc;
4159 struct sctp_add_streams params;
4160 int retval = -EINVAL;
4161
4162 if (optlen != sizeof(params))
4163 goto out;
4164
4165 if (copy_from_user(&params, optval, optlen)) {
4166 retval = -EFAULT;
4167 goto out;
4168 }
4169
4170 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
4171 if (!asoc)
4172 goto out;
4173
4174 retval = sctp_send_add_streams(asoc, &params);
4175
4176out:
4177 return retval;
4178}
4179
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004180static int sctp_setsockopt_scheduler(struct sock *sk,
4181 char __user *optval,
4182 unsigned int optlen)
4183{
4184 struct sctp_association *asoc;
4185 struct sctp_assoc_value params;
4186 int retval = -EINVAL;
4187
4188 if (optlen < sizeof(params))
4189 goto out;
4190
4191 optlen = sizeof(params);
4192 if (copy_from_user(&params, optval, optlen)) {
4193 retval = -EFAULT;
4194 goto out;
4195 }
4196
4197 if (params.assoc_value > SCTP_SS_MAX)
4198 goto out;
4199
4200 asoc = sctp_id2assoc(sk, params.assoc_id);
4201 if (!asoc)
4202 goto out;
4203
4204 retval = sctp_sched_set_sched(asoc, params.assoc_value);
4205
4206out:
4207 return retval;
4208}
4209
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004210static int sctp_setsockopt_scheduler_value(struct sock *sk,
4211 char __user *optval,
4212 unsigned int optlen)
4213{
4214 struct sctp_association *asoc;
4215 struct sctp_stream_value params;
4216 int retval = -EINVAL;
4217
4218 if (optlen < sizeof(params))
4219 goto out;
4220
4221 optlen = sizeof(params);
4222 if (copy_from_user(&params, optval, optlen)) {
4223 retval = -EFAULT;
4224 goto out;
4225 }
4226
4227 asoc = sctp_id2assoc(sk, params.assoc_id);
4228 if (!asoc)
4229 goto out;
4230
4231 retval = sctp_sched_set_value(asoc, params.stream_id,
4232 params.stream_value, GFP_KERNEL);
4233
4234out:
4235 return retval;
4236}
4237
Xin Long772a5862017-12-08 21:03:58 +08004238static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4239 char __user *optval,
4240 unsigned int optlen)
4241{
4242 struct sctp_sock *sp = sctp_sk(sk);
4243 struct net *net = sock_net(sk);
4244 struct sctp_assoc_value params;
4245 int retval = -EINVAL;
4246
4247 if (optlen < sizeof(params))
4248 goto out;
4249
4250 optlen = sizeof(params);
4251 if (copy_from_user(&params, optval, optlen)) {
4252 retval = -EFAULT;
4253 goto out;
4254 }
4255
4256 if (params.assoc_id)
4257 goto out;
4258
4259 if (!net->sctp.intl_enable || !sp->frag_interleave) {
4260 retval = -EPERM;
4261 goto out;
4262 }
4263
4264 sp->strm_interleave = !!params.assoc_value;
4265
4266 retval = 0;
4267
4268out:
4269 return retval;
4270}
4271
Xin Longb0e9a2f2018-06-28 15:31:00 +08004272static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval,
4273 unsigned int optlen)
4274{
4275 int val;
4276
4277 if (!sctp_style(sk, TCP))
4278 return -EOPNOTSUPP;
4279
4280 if (sctp_sk(sk)->ep->base.bind_addr.port)
4281 return -EFAULT;
4282
4283 if (optlen < sizeof(int))
4284 return -EINVAL;
4285
4286 if (get_user(val, (int __user *)optval))
4287 return -EFAULT;
4288
4289 sctp_sk(sk)->reuse = !!val;
4290
4291 return 0;
4292}
4293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294/* API 6.2 setsockopt(), getsockopt()
4295 *
4296 * Applications use setsockopt() and getsockopt() to set or retrieve
4297 * socket options. Socket options are used to change the default
4298 * behavior of sockets calls. They are described in Section 7.
4299 *
4300 * The syntax is:
4301 *
4302 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4303 * int __user *optlen);
4304 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4305 * int optlen);
4306 *
4307 * sd - the socket descript.
4308 * level - set to IPPROTO_SCTP for all SCTP options.
4309 * optname - the option name.
4310 * optval - the buffer to store the value of the option.
4311 * optlen - the size of the buffer.
4312 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004313static int sctp_setsockopt(struct sock *sk, int level, int optname,
4314 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315{
4316 int retval = 0;
4317
Daniel Borkmannbb333812013-06-28 19:49:40 +02004318 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319
4320 /* I can hardly begin to describe how wrong this is. This is
4321 * so broken as to be worse than useless. The API draft
4322 * REALLY is NOT helpful here... I am not convinced that the
4323 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4324 * are at all well-founded.
4325 */
4326 if (level != SOL_SCTP) {
4327 struct sctp_af *af = sctp_sk(sk)->pf->af;
4328 retval = af->setsockopt(sk, level, optname, optval, optlen);
4329 goto out_nounlock;
4330 }
4331
wangweidong048ed4b2014-01-21 15:44:11 +08004332 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
4334 switch (optname) {
4335 case SCTP_SOCKOPT_BINDX_ADD:
4336 /* 'optlen' is the size of the addresses buffer. */
4337 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4338 optlen, SCTP_BINDX_ADD_ADDR);
4339 break;
4340
4341 case SCTP_SOCKOPT_BINDX_REM:
4342 /* 'optlen' is the size of the addresses buffer. */
4343 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4344 optlen, SCTP_BINDX_REM_ADDR);
4345 break;
4346
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004347 case SCTP_SOCKOPT_CONNECTX_OLD:
4348 /* 'optlen' is the size of the addresses buffer. */
4349 retval = sctp_setsockopt_connectx_old(sk,
4350 (struct sockaddr __user *)optval,
4351 optlen);
4352 break;
4353
Frank Filz3f7a87d2005-06-20 13:14:57 -07004354 case SCTP_SOCKOPT_CONNECTX:
4355 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004356 retval = sctp_setsockopt_connectx(sk,
4357 (struct sockaddr __user *)optval,
4358 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004359 break;
4360
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 case SCTP_DISABLE_FRAGMENTS:
4362 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4363 break;
4364
4365 case SCTP_EVENTS:
4366 retval = sctp_setsockopt_events(sk, optval, optlen);
4367 break;
4368
4369 case SCTP_AUTOCLOSE:
4370 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4371 break;
4372
4373 case SCTP_PEER_ADDR_PARAMS:
4374 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4375 break;
4376
Shan Wei4580ccc2011-01-18 22:39:00 +00004377 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07004378 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08004379 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004380 case SCTP_PARTIAL_DELIVERY_POINT:
4381 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4382 break;
Frank Filz77086102005-12-22 11:37:30 -08004383
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 case SCTP_INITMSG:
4385 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4386 break;
4387 case SCTP_DEFAULT_SEND_PARAM:
4388 retval = sctp_setsockopt_default_send_param(sk, optval,
4389 optlen);
4390 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02004391 case SCTP_DEFAULT_SNDINFO:
4392 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4393 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 case SCTP_PRIMARY_ADDR:
4395 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4396 break;
4397 case SCTP_SET_PEER_PRIMARY_ADDR:
4398 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4399 break;
4400 case SCTP_NODELAY:
4401 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4402 break;
4403 case SCTP_RTOINFO:
4404 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4405 break;
4406 case SCTP_ASSOCINFO:
4407 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4408 break;
4409 case SCTP_I_WANT_MAPPED_V4_ADDR:
4410 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4411 break;
4412 case SCTP_MAXSEG:
4413 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4414 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004415 case SCTP_ADAPTATION_LAYER:
4416 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004418 case SCTP_CONTEXT:
4419 retval = sctp_setsockopt_context(sk, optval, optlen);
4420 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004421 case SCTP_FRAGMENT_INTERLEAVE:
4422 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4423 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07004424 case SCTP_MAX_BURST:
4425 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4426 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004427 case SCTP_AUTH_CHUNK:
4428 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4429 break;
4430 case SCTP_HMAC_IDENT:
4431 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4432 break;
4433 case SCTP_AUTH_KEY:
4434 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4435 break;
4436 case SCTP_AUTH_ACTIVE_KEY:
4437 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4438 break;
4439 case SCTP_AUTH_DELETE_KEY:
4440 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4441 break;
Xin Long601590e2018-03-14 19:05:32 +08004442 case SCTP_AUTH_DEACTIVATE_KEY:
4443 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
4444 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09004445 case SCTP_AUTO_ASCONF:
4446 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4447 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00004448 case SCTP_PEER_ADDR_THLDS:
4449 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4450 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004451 case SCTP_RECVRCVINFO:
4452 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4453 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004454 case SCTP_RECVNXTINFO:
4455 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4456 break;
Xin Long28aa4c22016-07-09 19:47:40 +08004457 case SCTP_PR_SUPPORTED:
4458 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4459 break;
Xin Longf959fb42016-07-09 19:47:41 +08004460 case SCTP_DEFAULT_PRINFO:
4461 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4462 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08004463 case SCTP_RECONFIG_SUPPORTED:
4464 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4465 break;
Xin Long9fb657a2017-01-18 00:44:46 +08004466 case SCTP_ENABLE_STREAM_RESET:
4467 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4468 break;
Xin Long7f9d68a2017-01-18 00:44:47 +08004469 case SCTP_RESET_STREAMS:
4470 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4471 break;
Xin Longa92ce1a2017-02-09 01:18:18 +08004472 case SCTP_RESET_ASSOC:
4473 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4474 break;
Xin Long242bd2d2017-02-09 01:18:20 +08004475 case SCTP_ADD_STREAMS:
4476 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4477 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004478 case SCTP_STREAM_SCHEDULER:
4479 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4480 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004481 case SCTP_STREAM_SCHEDULER_VALUE:
4482 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4483 break;
Xin Long772a5862017-12-08 21:03:58 +08004484 case SCTP_INTERLEAVING_SUPPORTED:
4485 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4486 optlen);
4487 break;
Xin Longb0e9a2f2018-06-28 15:31:00 +08004488 case SCTP_REUSE_PORT:
4489 retval = sctp_setsockopt_reuse_port(sk, optval, optlen);
4490 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 default:
4492 retval = -ENOPROTOOPT;
4493 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
wangweidong048ed4b2014-01-21 15:44:11 +08004496 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
4498out_nounlock:
4499 return retval;
4500}
4501
4502/* API 3.1.6 connect() - UDP Style Syntax
4503 *
4504 * An application may use the connect() call in the UDP model to initiate an
4505 * association without sending data.
4506 *
4507 * The syntax is:
4508 *
4509 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4510 *
4511 * sd: the socket descriptor to have a new association added to.
4512 *
4513 * nam: the address structure (either struct sockaddr_in or struct
4514 * sockaddr_in6 defined in RFC2553 [7]).
4515 *
4516 * len: the size of the address.
4517 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004518static int sctp_connect(struct sock *sk, struct sockaddr *addr,
Xin Long644fbde2018-05-20 16:39:10 +08004519 int addr_len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520{
Xin Long644fbde2018-05-20 16:39:10 +08004521 struct inet_sock *inet = inet_sk(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004522 struct sctp_af *af;
Xin Long644fbde2018-05-20 16:39:10 +08004523 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
wangweidong048ed4b2014-01-21 15:44:11 +08004525 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526
Daniel Borkmannbb333812013-06-28 19:49:40 +02004527 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4528 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
Xin Long644fbde2018-05-20 16:39:10 +08004530 /* We may need to bind the socket. */
4531 if (!inet->inet_num) {
4532 if (sk->sk_prot->get_port(sk, 0)) {
4533 release_sock(sk);
4534 return -EAGAIN;
4535 }
4536 inet->inet_sport = htons(inet->inet_num);
4537 }
4538
Frank Filz3f7a87d2005-06-20 13:14:57 -07004539 /* Validate addr_len before calling common connect/connectx routine. */
4540 af = sctp_get_af_specific(addr->sa_family);
4541 if (!af || addr_len < af->sockaddr_len) {
4542 err = -EINVAL;
4543 } else {
4544 /* Pass correct addr len to common routine (so it knows there
4545 * is only one address being passed.
4546 */
Xin Long644fbde2018-05-20 16:39:10 +08004547 err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 }
4549
wangweidong048ed4b2014-01-21 15:44:11 +08004550 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 return err;
4552}
4553
Xin Long644fbde2018-05-20 16:39:10 +08004554int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
4555 int addr_len, int flags)
4556{
4557 if (addr_len < sizeof(uaddr->sa_family))
4558 return -EINVAL;
4559
4560 if (uaddr->sa_family == AF_UNSPEC)
4561 return -EOPNOTSUPP;
4562
4563 return sctp_connect(sock->sk, uaddr, addr_len, flags);
4564}
4565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566/* FIXME: Write comments. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004567static int sctp_disconnect(struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568{
4569 return -EOPNOTSUPP; /* STUB */
4570}
4571
4572/* 4.1.4 accept() - TCP Style Syntax
4573 *
4574 * Applications use accept() call to remove an established SCTP
4575 * association from the accept queue of the endpoint. A new socket
4576 * descriptor will be returned from accept() to represent the newly
4577 * formed association.
4578 */
David Howellscdfbabf2017-03-09 08:09:05 +00004579static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580{
4581 struct sctp_sock *sp;
4582 struct sctp_endpoint *ep;
4583 struct sock *newsk = NULL;
4584 struct sctp_association *asoc;
4585 long timeo;
4586 int error = 0;
4587
wangweidong048ed4b2014-01-21 15:44:11 +08004588 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
4590 sp = sctp_sk(sk);
4591 ep = sp->ep;
4592
4593 if (!sctp_style(sk, TCP)) {
4594 error = -EOPNOTSUPP;
4595 goto out;
4596 }
4597
4598 if (!sctp_sstate(sk, LISTENING)) {
4599 error = -EINVAL;
4600 goto out;
4601 }
4602
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07004603 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
4605 error = sctp_wait_for_accept(sk, timeo);
4606 if (error)
4607 goto out;
4608
4609 /* We treat the list of associations on the endpoint as the accept
4610 * queue and pick the first association on the list.
4611 */
4612 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4613
David Howellscdfbabf2017-03-09 08:09:05 +00004614 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 if (!newsk) {
4616 error = -ENOMEM;
4617 goto out;
4618 }
4619
4620 /* Populate the fields of the newsk from the oldsk and migrate the
4621 * asoc to the newsk.
4622 */
4623 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4624
4625out:
wangweidong048ed4b2014-01-21 15:44:11 +08004626 release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004627 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 return newsk;
4629}
4630
4631/* The SCTP ioctl handler. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004632static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004634 int rc = -ENOTCONN;
4635
wangweidong048ed4b2014-01-21 15:44:11 +08004636 lock_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004637
4638 /*
4639 * SEQPACKET-style sockets in LISTENING state are valid, for
4640 * SCTP, so only discard TCP-style sockets in LISTENING state.
4641 */
4642 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4643 goto out;
4644
4645 switch (cmd) {
4646 case SIOCINQ: {
4647 struct sk_buff *skb;
4648 unsigned int amount = 0;
4649
4650 skb = skb_peek(&sk->sk_receive_queue);
4651 if (skb != NULL) {
4652 /*
4653 * We will only return the amount of this packet since
4654 * that is all that will be read.
4655 */
4656 amount = skb->len;
4657 }
4658 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004659 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07004660 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004661 default:
4662 rc = -ENOIOCTLCMD;
4663 break;
4664 }
4665out:
wangweidong048ed4b2014-01-21 15:44:11 +08004666 release_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004667 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668}
4669
4670/* This is the function which gets called during socket creation to
4671 * initialized the SCTP-specific portion of the sock.
4672 * The sock structure should already be zero-filled memory.
4673 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004674static int sctp_init_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004676 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 struct sctp_sock *sp;
4678
Daniel Borkmannbb333812013-06-28 19:49:40 +02004679 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
4681 sp = sctp_sk(sk);
4682
4683 /* Initialize the SCTP per socket area. */
4684 switch (sk->sk_type) {
4685 case SOCK_SEQPACKET:
4686 sp->type = SCTP_SOCKET_UDP;
4687 break;
4688 case SOCK_STREAM:
4689 sp->type = SCTP_SOCKET_TCP;
4690 break;
4691 default:
4692 return -ESOCKTNOSUPPORT;
4693 }
4694
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004695 sk->sk_gso_type = SKB_GSO_SCTP;
4696
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 /* Initialize default send parameters. These parameters can be
4698 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4699 */
4700 sp->default_stream = 0;
4701 sp->default_ppid = 0;
4702 sp->default_flags = 0;
4703 sp->default_context = 0;
4704 sp->default_timetolive = 0;
4705
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004706 sp->default_rcv_context = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004707 sp->max_burst = net->sctp.max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004708
Neil Horman3c681982012-10-24 09:20:03 +00004709 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 /* Initialize default setup parameters. These parameters
4712 * can be modified with the SCTP_INITMSG socket option or
4713 * overridden by the SCTP_INIT CMSG.
4714 */
4715 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4716 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004717 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4718 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
4720 /* Initialize default RTO related parameters. These parameters can
4721 * be modified for with the SCTP_RTOINFO socket option.
4722 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004723 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4724 sp->rtoinfo.srto_max = net->sctp.rto_max;
4725 sp->rtoinfo.srto_min = net->sctp.rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
4727 /* Initialize default association related parameters. These parameters
4728 * can be modified with the SCTP_ASSOCINFO socket option.
4729 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004730 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 sp->assocparams.sasoc_number_peer_destinations = 0;
4732 sp->assocparams.sasoc_peer_rwnd = 0;
4733 sp->assocparams.sasoc_local_rwnd = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004734 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735
4736 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004737 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 */
4739 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4740
4741 /* Default Peer Address Parameters. These defaults can
4742 * be modified via SCTP_PEER_ADDR_PARAMS
4743 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004744 sp->hbinterval = net->sctp.hb_interval;
4745 sp->pathmaxrxt = net->sctp.max_retrans_path;
wangweidong4e2d52b2013-12-23 12:16:54 +08004746 sp->pathmtu = 0; /* allow default discovery */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004747 sp->sackdelay = net->sctp.sack_timeout;
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07004748 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004749 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004750 SPP_PMTUD_ENABLE |
4751 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
4753 /* If enabled no SCTP message fragmentation will be performed.
4754 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4755 */
4756 sp->disable_fragments = 0;
4757
Sridhar Samudrala208edef2006-09-29 17:08:01 -07004758 /* Enable Nagle algorithm by default. */
4759 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004761 sp->recvrcvinfo = 0;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004762 sp->recvnxtinfo = 0;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004763
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 /* Enable by default. */
4765 sp->v4mapped = 1;
4766
4767 /* Auto-close idle associations after the configured
4768 * number of seconds. A value of 0 disables this
4769 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4770 * for UDP-style sockets only.
4771 */
4772 sp->autoclose = 0;
4773
4774 /* User specified fragmentation limit. */
4775 sp->user_frag = 0;
4776
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004777 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
4779 sp->pf = sctp_get_pf_specific(sk->sk_family);
4780
4781 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004782 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004784 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
4786 /* Create a per socket endpoint structure. Even if we
4787 * change the data structure relationships, this may still
4788 * be useful for storing pre-connect address information.
4789 */
Daniel Borkmannc164b832013-06-14 18:24:06 +02004790 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4791 if (!sp->ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 return -ENOMEM;
4793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 sp->hmac = NULL;
4795
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004796 sk->sk_destruct = sctp_destruct_sock;
4797
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08004799
4800 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004801 sk_sockets_allocated_inc(sk);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004802 sock_prot_inuse_add(net, sk->sk_prot, 1);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004803
4804 /* Nothing can fail after this block, otherwise
4805 * sctp_destroy_sock() will be called without addr_wq_lock held
4806 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004807 if (net->sctp.default_auto_asconf) {
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004808 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda9f7d6532011-04-26 19:32:51 +09004809 list_add_tail(&sp->auto_asconf_list,
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004810 &net->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09004811 sp->do_auto_asconf = 1;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004812 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4813 } else {
Michio Honda9f7d6532011-04-26 19:32:51 +09004814 sp->do_auto_asconf = 0;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004815 }
4816
David S. Miller6f756a82008-11-23 17:34:03 -08004817 local_bh_enable();
4818
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 return 0;
4820}
4821
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004822/* Cleanup any SCTP per socket resources. Must be called with
4823 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4824 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004825static void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826{
Michio Honda9f7d6532011-04-26 19:32:51 +09004827 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Daniel Borkmannbb333812013-06-28 19:49:40 +02004829 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
4831 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09004832 sp = sctp_sk(sk);
Daniel Borkmann1abd1652013-06-06 15:53:47 +02004833 /* This could happen during socket init, thus we bail out
4834 * early, since the rest of the below is not setup either.
4835 */
4836 if (sp->ep == NULL)
4837 return;
4838
Michio Honda9f7d6532011-04-26 19:32:51 +09004839 if (sp->do_auto_asconf) {
4840 sp->do_auto_asconf = 0;
4841 list_del(&sp->auto_asconf_list);
4842 }
4843 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004844 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004845 sk_sockets_allocated_dec(sk);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08004846 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004847 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848}
4849
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004850/* Triggered when there are no references on the socket anymore */
4851static void sctp_destruct_sock(struct sock *sk)
4852{
4853 struct sctp_sock *sp = sctp_sk(sk);
4854
4855 /* Free up the HMAC transform. */
Herbert Xu5821c762016-01-24 21:20:12 +08004856 crypto_free_shash(sp->hmac);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004857
4858 inet_sock_destruct(sk);
4859}
4860
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861/* API 4.1.7 shutdown() - TCP Style Syntax
4862 * int shutdown(int socket, int how);
4863 *
4864 * sd - the socket descriptor of the association to be closed.
4865 * how - Specifies the type of shutdown. The values are
4866 * as follows:
4867 * SHUT_RD
4868 * Disables further receive operations. No SCTP
4869 * protocol action is taken.
4870 * SHUT_WR
4871 * Disables further send operations, and initiates
4872 * the SCTP shutdown sequence.
4873 * SHUT_RDWR
4874 * Disables further send and receive operations
4875 * and initiates the SCTP shutdown sequence.
4876 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004877static void sctp_shutdown(struct sock *sk, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004879 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 struct sctp_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
4882 if (!sctp_style(sk, TCP))
4883 return;
4884
Xin Long5bf35dd2016-11-13 21:44:37 +08004885 ep = sctp_sk(sk)->ep;
4886 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4887 struct sctp_association *asoc;
4888
Yafang Shaocbabf462017-12-20 11:12:54 +08004889 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Xin Long5bf35dd2016-11-13 21:44:37 +08004890 asoc = list_entry(ep->asocs.next,
4891 struct sctp_association, asocs);
4892 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 }
4894}
4895
Xin Long52c52a62016-04-14 15:35:30 +08004896int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4897 struct sctp_info *info)
4898{
4899 struct sctp_transport *prim;
4900 struct list_head *pos;
4901 int mask;
4902
4903 memset(info, 0, sizeof(*info));
4904 if (!asoc) {
4905 struct sctp_sock *sp = sctp_sk(sk);
4906
4907 info->sctpi_s_autoclose = sp->autoclose;
4908 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4909 info->sctpi_s_pd_point = sp->pd_point;
4910 info->sctpi_s_nodelay = sp->nodelay;
4911 info->sctpi_s_disable_fragments = sp->disable_fragments;
4912 info->sctpi_s_v4mapped = sp->v4mapped;
4913 info->sctpi_s_frag_interleave = sp->frag_interleave;
Xin Long40eb90e92016-05-29 17:42:13 +08004914 info->sctpi_s_type = sp->type;
Xin Long52c52a62016-04-14 15:35:30 +08004915
4916 return 0;
4917 }
4918
4919 info->sctpi_tag = asoc->c.my_vtag;
4920 info->sctpi_state = asoc->state;
4921 info->sctpi_rwnd = asoc->a_rwnd;
4922 info->sctpi_unackdata = asoc->unack_data;
4923 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004924 info->sctpi_instrms = asoc->stream.incnt;
4925 info->sctpi_outstrms = asoc->stream.outcnt;
Xin Long52c52a62016-04-14 15:35:30 +08004926 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4927 info->sctpi_inqueue++;
4928 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4929 info->sctpi_outqueue++;
4930 info->sctpi_overall_error = asoc->overall_error_count;
4931 info->sctpi_max_burst = asoc->max_burst;
4932 info->sctpi_maxseg = asoc->frag_point;
4933 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4934 info->sctpi_peer_tag = asoc->c.peer_vtag;
4935
4936 mask = asoc->peer.ecn_capable << 1;
4937 mask = (mask | asoc->peer.ipv4_address) << 1;
4938 mask = (mask | asoc->peer.ipv6_address) << 1;
4939 mask = (mask | asoc->peer.hostname_address) << 1;
4940 mask = (mask | asoc->peer.asconf_capable) << 1;
4941 mask = (mask | asoc->peer.prsctp_capable) << 1;
4942 mask = (mask | asoc->peer.auth_capable);
4943 info->sctpi_peer_capable = mask;
4944 mask = asoc->peer.sack_needed << 1;
4945 mask = (mask | asoc->peer.sack_generation) << 1;
4946 mask = (mask | asoc->peer.zero_window_announced);
4947 info->sctpi_peer_sack = mask;
4948
4949 info->sctpi_isacks = asoc->stats.isacks;
4950 info->sctpi_osacks = asoc->stats.osacks;
4951 info->sctpi_opackets = asoc->stats.opackets;
4952 info->sctpi_ipackets = asoc->stats.ipackets;
4953 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4954 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4955 info->sctpi_idupchunks = asoc->stats.idupchunks;
4956 info->sctpi_gapcnt = asoc->stats.gapcnt;
4957 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4958 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4959 info->sctpi_oodchunks = asoc->stats.oodchunks;
4960 info->sctpi_iodchunks = asoc->stats.iodchunks;
4961 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4962 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4963
4964 prim = asoc->peer.primary_path;
Stefano Brivioee6c88b2017-08-23 13:27:13 +02004965 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
Xin Long52c52a62016-04-14 15:35:30 +08004966 info->sctpi_p_state = prim->state;
4967 info->sctpi_p_cwnd = prim->cwnd;
4968 info->sctpi_p_srtt = prim->srtt;
4969 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4970 info->sctpi_p_hbinterval = prim->hbinterval;
4971 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4972 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4973 info->sctpi_p_ssthresh = prim->ssthresh;
4974 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4975 info->sctpi_p_flight_size = prim->flight_size;
4976 info->sctpi_p_error = prim->error_count;
4977
4978 return 0;
4979}
4980EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4981
Xin Long626d16f2016-04-14 15:35:31 +08004982/* use callback to avoid exporting the core structure */
Tom Herbert97a6ec42017-12-04 10:31:41 -08004983void sctp_transport_walk_start(struct rhashtable_iter *iter)
Xin Long626d16f2016-04-14 15:35:31 +08004984{
Xin Long7fda702f2016-11-15 23:23:11 +08004985 rhltable_walk_enter(&sctp_transport_hashtable, iter);
Xin Long626d16f2016-04-14 15:35:31 +08004986
Tom Herbert97a6ec42017-12-04 10:31:41 -08004987 rhashtable_walk_start(iter);
Xin Long626d16f2016-04-14 15:35:31 +08004988}
4989
4990void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4991{
4992 rhashtable_walk_stop(iter);
4993 rhashtable_walk_exit(iter);
4994}
4995
4996struct sctp_transport *sctp_transport_get_next(struct net *net,
4997 struct rhashtable_iter *iter)
4998{
4999 struct sctp_transport *t;
5000
5001 t = rhashtable_walk_next(iter);
5002 for (; t; t = rhashtable_walk_next(iter)) {
5003 if (IS_ERR(t)) {
5004 if (PTR_ERR(t) == -EAGAIN)
5005 continue;
5006 break;
5007 }
5008
Xin Longbab1be72018-08-27 18:38:31 +08005009 if (!sctp_transport_hold(t))
5010 continue;
5011
Xin Long626d16f2016-04-14 15:35:31 +08005012 if (net_eq(sock_net(t->asoc->base.sk), net) &&
5013 t->asoc->peer.primary_path == t)
5014 break;
Xin Longbab1be72018-08-27 18:38:31 +08005015
5016 sctp_transport_put(t);
Xin Long626d16f2016-04-14 15:35:31 +08005017 }
5018
5019 return t;
5020}
5021
5022struct sctp_transport *sctp_transport_get_idx(struct net *net,
5023 struct rhashtable_iter *iter,
5024 int pos)
5025{
Xin Longbab1be72018-08-27 18:38:31 +08005026 struct sctp_transport *t;
Xin Long626d16f2016-04-14 15:35:31 +08005027
Xin Longbab1be72018-08-27 18:38:31 +08005028 if (!pos)
5029 return SEQ_START_TOKEN;
Xin Long626d16f2016-04-14 15:35:31 +08005030
Xin Longbab1be72018-08-27 18:38:31 +08005031 while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
5032 if (!--pos)
5033 break;
5034 sctp_transport_put(t);
5035 }
5036
5037 return t;
Xin Long626d16f2016-04-14 15:35:31 +08005038}
5039
5040int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
5041 void *p) {
5042 int err = 0;
5043 int hash = 0;
5044 struct sctp_ep_common *epb;
5045 struct sctp_hashbucket *head;
5046
5047 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
5048 hash++, head++) {
Xin Long581409d2017-06-10 14:48:14 +08005049 read_lock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08005050 sctp_for_each_hentry(epb, &head->chain) {
5051 err = cb(sctp_ep(epb), p);
5052 if (err)
5053 break;
5054 }
Xin Long581409d2017-06-10 14:48:14 +08005055 read_unlock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08005056 }
5057
5058 return err;
5059}
5060EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
5061
5062int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
5063 struct net *net,
5064 const union sctp_addr *laddr,
5065 const union sctp_addr *paddr, void *p)
5066{
5067 struct sctp_transport *transport;
Xin Long08abb792016-12-15 23:05:52 +08005068 int err;
Xin Long626d16f2016-04-14 15:35:31 +08005069
5070 rcu_read_lock();
5071 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
Xin Long626d16f2016-04-14 15:35:31 +08005072 rcu_read_unlock();
Xin Long08abb792016-12-15 23:05:52 +08005073 if (!transport)
5074 return -ENOENT;
5075
Xin Long1cceda782016-09-29 02:55:44 +08005076 err = cb(transport, p);
Xin Longcd26da42016-10-31 20:32:31 +08005077 sctp_transport_put(transport);
Xin Long1cceda782016-09-29 02:55:44 +08005078
Xin Long626d16f2016-04-14 15:35:31 +08005079 return err;
5080}
5081EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
5082
5083int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
Xin Longd25adbe2017-09-15 11:02:21 +08005084 int (*cb_done)(struct sctp_transport *, void *),
5085 struct net *net, int *pos, void *p) {
Xin Long626d16f2016-04-14 15:35:31 +08005086 struct rhashtable_iter hti;
Xin Longd25adbe2017-09-15 11:02:21 +08005087 struct sctp_transport *tsp;
5088 int ret;
Xin Long626d16f2016-04-14 15:35:31 +08005089
Xin Longd25adbe2017-09-15 11:02:21 +08005090again:
Xin Longf53d77e2018-01-23 18:22:25 +08005091 ret = 0;
Tom Herbert97a6ec42017-12-04 10:31:41 -08005092 sctp_transport_walk_start(&hti);
Xin Long626d16f2016-04-14 15:35:31 +08005093
Xin Longd25adbe2017-09-15 11:02:21 +08005094 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5095 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
Xin Longd25adbe2017-09-15 11:02:21 +08005096 ret = cb(tsp, p);
5097 if (ret)
Xin Long626d16f2016-04-14 15:35:31 +08005098 break;
Xin Longd25adbe2017-09-15 11:02:21 +08005099 (*pos)++;
5100 sctp_transport_put(tsp);
Xin Long626d16f2016-04-14 15:35:31 +08005101 }
Xin Long626d16f2016-04-14 15:35:31 +08005102 sctp_transport_walk_stop(&hti);
Xin Long53fa1032016-04-14 15:35:35 +08005103
Xin Longd25adbe2017-09-15 11:02:21 +08005104 if (ret) {
5105 if (cb_done && !cb_done(tsp, p)) {
5106 (*pos)++;
5107 sctp_transport_put(tsp);
5108 goto again;
5109 }
5110 sctp_transport_put(tsp);
5111 }
5112
5113 return ret;
Xin Long626d16f2016-04-14 15:35:31 +08005114}
5115EXPORT_SYMBOL_GPL(sctp_for_each_transport);
5116
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117/* 7.2.1 Association Status (SCTP_STATUS)
5118
5119 * Applications can retrieve current status information about an
5120 * association, including association state, peer receiver window size,
5121 * number of unacked data chunks, and number of data chunks pending
5122 * receipt. This information is read-only.
5123 */
5124static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5125 char __user *optval,
5126 int __user *optlen)
5127{
5128 struct sctp_status status;
5129 struct sctp_association *asoc = NULL;
5130 struct sctp_transport *transport;
5131 sctp_assoc_t associd;
5132 int retval = 0;
5133
Neil Horman408f22e2007-06-16 14:03:45 -04005134 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 retval = -EINVAL;
5136 goto out;
5137 }
5138
Neil Horman408f22e2007-06-16 14:03:45 -04005139 len = sizeof(status);
5140 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 retval = -EFAULT;
5142 goto out;
5143 }
5144
5145 associd = status.sstat_assoc_id;
5146 asoc = sctp_id2assoc(sk, associd);
5147 if (!asoc) {
5148 retval = -EINVAL;
5149 goto out;
5150 }
5151
5152 transport = asoc->peer.primary_path;
5153
5154 status.sstat_assoc_id = sctp_assoc2id(asoc);
Daniel Borkmann38ab1fa2014-08-28 15:28:26 +02005155 status.sstat_state = sctp_assoc_to_state(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 status.sstat_rwnd = asoc->peer.rwnd;
5157 status.sstat_unackdata = asoc->unack_data;
5158
5159 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08005160 status.sstat_instrms = asoc->stream.incnt;
5161 status.sstat_outstrms = asoc->stream.outcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 status.sstat_fragmentation_point = asoc->frag_point;
5163 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08005164 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5165 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 /* Map ipv4 address into v4-mapped-on-v6 address. */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005167 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005169 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 status.sstat_primary.spinfo_cwnd = transport->cwnd;
5171 status.sstat_primary.spinfo_srtt = transport->srtt;
5172 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005173 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
Frank Filz3f7a87d2005-06-20 13:14:57 -07005175 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5176 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5177
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 if (put_user(len, optlen)) {
5179 retval = -EFAULT;
5180 goto out;
5181 }
5182
Daniel Borkmannbb333812013-06-28 19:49:40 +02005183 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5184 __func__, len, status.sstat_state, status.sstat_rwnd,
5185 status.sstat_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
5187 if (copy_to_user(optval, &status, len)) {
5188 retval = -EFAULT;
5189 goto out;
5190 }
5191
5192out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00005193 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194}
5195
5196
5197/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5198 *
5199 * Applications can retrieve information about a specific peer address
5200 * of an association, including its reachability state, congestion
5201 * window, and retransmission timer values. This information is
5202 * read-only.
5203 */
5204static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5205 char __user *optval,
5206 int __user *optlen)
5207{
5208 struct sctp_paddrinfo pinfo;
5209 struct sctp_transport *transport;
5210 int retval = 0;
5211
Neil Horman408f22e2007-06-16 14:03:45 -04005212 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 retval = -EINVAL;
5214 goto out;
5215 }
5216
Neil Horman408f22e2007-06-16 14:03:45 -04005217 len = sizeof(pinfo);
5218 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 retval = -EFAULT;
5220 goto out;
5221 }
5222
5223 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5224 pinfo.spinfo_assoc_id);
5225 if (!transport)
5226 return -EINVAL;
5227
5228 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005229 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 pinfo.spinfo_cwnd = transport->cwnd;
5231 pinfo.spinfo_srtt = transport->srtt;
5232 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005233 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234
Frank Filz3f7a87d2005-06-20 13:14:57 -07005235 if (pinfo.spinfo_state == SCTP_UNKNOWN)
5236 pinfo.spinfo_state = SCTP_ACTIVE;
5237
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 if (put_user(len, optlen)) {
5239 retval = -EFAULT;
5240 goto out;
5241 }
5242
5243 if (copy_to_user(optval, &pinfo, len)) {
5244 retval = -EFAULT;
5245 goto out;
5246 }
5247
5248out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00005249 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250}
5251
5252/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5253 *
5254 * This option is a on/off flag. If enabled no SCTP message
5255 * fragmentation will be performed. Instead if a message being sent
5256 * exceeds the current PMTU size, the message will NOT be sent and
5257 * instead a error will be indicated to the user.
5258 */
5259static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5260 char __user *optval, int __user *optlen)
5261{
5262 int val;
5263
5264 if (len < sizeof(int))
5265 return -EINVAL;
5266
5267 len = sizeof(int);
5268 val = (sctp_sk(sk)->disable_fragments == 1);
5269 if (put_user(len, optlen))
5270 return -EFAULT;
5271 if (copy_to_user(optval, &val, len))
5272 return -EFAULT;
5273 return 0;
5274}
5275
5276/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5277 *
5278 * This socket option is used to specify various notifications and
5279 * ancillary data the user wishes to receive.
5280 */
5281static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5282 int __user *optlen)
5283{
Jiri Slabya4b8e712016-10-21 14:13:24 +02005284 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00005286 if (len > sizeof(struct sctp_event_subscribe))
5287 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04005288 if (put_user(len, optlen))
5289 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
5291 return -EFAULT;
5292 return 0;
5293}
5294
5295/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5296 *
5297 * This socket option is applicable to the UDP-style socket only. When
5298 * set it will cause associations that are idle for more than the
5299 * specified number of seconds to automatically close. An association
5300 * being idle is defined an association that has NOT sent or received
5301 * user data. The special value of '0' indicates that no automatic
5302 * close of any associations should be performed. The option expects an
5303 * integer defining the number of seconds of idle time before an
5304 * association is closed.
5305 */
5306static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5307{
5308 /* Applicable to UDP-style socket only */
5309 if (sctp_style(sk, TCP))
5310 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04005311 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005313 len = sizeof(int);
5314 if (put_user(len, optlen))
5315 return -EFAULT;
David Windsorb2ce04c2017-06-10 22:50:43 -04005316 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 return -EFAULT;
5318 return 0;
5319}
5320
5321/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00005322int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323{
Benjamin Poirier0343c552012-03-08 05:55:58 +00005324 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005325 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 struct socket *sock;
5327 int err = 0;
5328
Xin Longdf80cd92017-10-17 23:26:10 +08005329 /* Do not peel off from one netns to another one. */
5330 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5331 return -EINVAL;
5332
Benjamin Poirier0343c552012-03-08 05:55:58 +00005333 if (!asoc)
5334 return -EINVAL;
5335
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 /* An association cannot be branched off from an already peeled-off
5337 * socket, nor is this supported for tcp style sockets.
5338 */
5339 if (!sctp_style(sk, UDP))
5340 return -EINVAL;
5341
5342 /* Create a new socket. */
5343 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5344 if (err < 0)
5345 return err;
5346
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005347 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005348
5349 /* Make peeled-off sockets more like 1-1 accepted sockets.
Richard Hainesb7e10c22018-02-24 16:18:51 +00005350 * Set the daddr and initialize id to something more random and also
5351 * copy over any ip options.
Vlad Yasevich4f444302006-10-30 18:54:32 -08005352 */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005353 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
Richard Hainesb7e10c22018-02-24 16:18:51 +00005354 sp->pf->copy_ip_options(sk, sock->sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005355
5356 /* Populate the fields of the newsk from the oldsk and migrate the
5357 * asoc to the newsk.
5358 */
5359 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005360
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 *sockp = sock;
5362
5363 return err;
5364}
Benjamin Poirier0343c552012-03-08 05:55:58 +00005365EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005367static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5368 struct file **newfile, unsigned flags)
5369{
5370 struct socket *newsock;
5371 int retval;
5372
5373 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5374 if (retval < 0)
5375 goto out;
5376
5377 /* Map the socket to an unused fd that can be returned to the user. */
5378 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5379 if (retval < 0) {
5380 sock_release(newsock);
5381 goto out;
5382 }
5383
5384 *newfile = sock_alloc_file(newsock, 0, NULL);
5385 if (IS_ERR(*newfile)) {
5386 put_unused_fd(retval);
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005387 retval = PTR_ERR(*newfile);
5388 *newfile = NULL;
5389 return retval;
5390 }
5391
5392 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5393 retval);
5394
5395 peeloff->sd = retval;
5396
5397 if (flags & SOCK_NONBLOCK)
5398 (*newfile)->f_flags |= O_NONBLOCK;
5399out:
5400 return retval;
5401}
5402
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5404{
5405 sctp_peeloff_arg_t peeloff;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005406 struct file *newfile = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408
Neil Horman408f22e2007-06-16 14:03:45 -04005409 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005411 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 if (copy_from_user(&peeloff, optval, len))
5413 return -EFAULT;
5414
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005415 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 if (retval < 0)
5417 goto out;
5418
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04005420 if (put_user(len, optlen)) {
5421 fput(newfile);
5422 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04005423 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04005424 }
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005425
5426 if (copy_to_user(optval, &peeloff, len)) {
5427 fput(newfile);
5428 put_unused_fd(retval);
5429 return -EFAULT;
5430 }
5431 fd_install(retval, newfile);
5432out:
5433 return retval;
5434}
5435
5436static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5437 char __user *optval, int __user *optlen)
5438{
5439 sctp_peeloff_flags_arg_t peeloff;
5440 struct file *newfile = NULL;
5441 int retval = 0;
5442
5443 if (len < sizeof(sctp_peeloff_flags_arg_t))
5444 return -EINVAL;
5445 len = sizeof(sctp_peeloff_flags_arg_t);
5446 if (copy_from_user(&peeloff, optval, len))
5447 return -EFAULT;
5448
5449 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5450 &newfile, peeloff.flags);
5451 if (retval < 0)
5452 goto out;
5453
5454 /* Return the fd mapped to the new socket. */
5455 if (put_user(len, optlen)) {
5456 fput(newfile);
5457 put_unused_fd(retval);
5458 return -EFAULT;
5459 }
5460
Al Viro56b31d12012-08-18 00:25:51 -04005461 if (copy_to_user(optval, &peeloff, len)) {
5462 fput(newfile);
5463 put_unused_fd(retval);
5464 return -EFAULT;
5465 }
5466 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467out:
5468 return retval;
5469}
5470
5471/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5472 *
5473 * Applications can enable or disable heartbeats for any peer address of
5474 * an association, modify an address's heartbeat interval, force a
5475 * heartbeat to be sent immediately, and adjust the address's maximum
5476 * number of retransmissions sent before an address is considered
5477 * unreachable. The following structure is used to access and modify an
5478 * address's parameters:
5479 *
5480 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005481 * sctp_assoc_t spp_assoc_id;
5482 * struct sockaddr_storage spp_address;
5483 * uint32_t spp_hbinterval;
5484 * uint16_t spp_pathmaxrxt;
5485 * uint32_t spp_pathmtu;
5486 * uint32_t spp_sackdelay;
5487 * uint32_t spp_flags;
5488 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08005490 * spp_assoc_id - (one-to-many style socket) This is filled in the
5491 * application, and identifies the association for
5492 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 * spp_address - This specifies which address is of interest.
5494 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005495 * in milliseconds. If a value of zero
5496 * is present in this field then no changes are to
5497 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 * spp_pathmaxrxt - This contains the maximum number of
5499 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08005500 * considered unreachable. If a value of zero
5501 * is present in this field then no changes are to
5502 * be made to this parameter.
5503 * spp_pathmtu - When Path MTU discovery is disabled the value
5504 * specified here will be the "fixed" path mtu.
5505 * Note that if the spp_address field is empty
5506 * then all associations on this address will
5507 * have this fixed path mtu set upon them.
5508 *
5509 * spp_sackdelay - When delayed sack is enabled, this value specifies
5510 * the number of milliseconds that sacks will be delayed
5511 * for. This value will apply to all addresses of an
5512 * association if the spp_address field is empty. Note
5513 * also, that if delayed sack is enabled and this
5514 * value is set to 0, no change is made to the last
5515 * recorded delayed sack timer value.
5516 *
5517 * spp_flags - These flags are used to control various features
5518 * on an association. The flag field may contain
5519 * zero or more of the following options.
5520 *
5521 * SPP_HB_ENABLE - Enable heartbeats on the
5522 * specified address. Note that if the address
5523 * field is empty all addresses for the association
5524 * have heartbeats enabled upon them.
5525 *
5526 * SPP_HB_DISABLE - Disable heartbeats on the
5527 * speicifed address. Note that if the address
5528 * field is empty all addresses for the association
5529 * will have their heartbeats disabled. Note also
5530 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5531 * mutually exclusive, only one of these two should
5532 * be specified. Enabling both fields will have
5533 * undetermined results.
5534 *
5535 * SPP_HB_DEMAND - Request a user initiated heartbeat
5536 * to be made immediately.
5537 *
5538 * SPP_PMTUD_ENABLE - This field will enable PMTU
5539 * discovery upon the specified address. Note that
5540 * if the address feild is empty then all addresses
5541 * on the association are effected.
5542 *
5543 * SPP_PMTUD_DISABLE - This field will disable PMTU
5544 * discovery upon the specified address. Note that
5545 * if the address feild is empty then all addresses
5546 * on the association are effected. Not also that
5547 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5548 * exclusive. Enabling both will have undetermined
5549 * results.
5550 *
5551 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5552 * on delayed sack. The time specified in spp_sackdelay
5553 * is used to specify the sack delay for this address. Note
5554 * that if spp_address is empty then all addresses will
5555 * enable delayed sack and take on the sack delay
5556 * value specified in spp_sackdelay.
5557 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5558 * off delayed sack. If the spp_address field is blank then
5559 * delayed sack is disabled for the entire association. Note
5560 * also that this field is mutually exclusive to
5561 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5562 * results.
Xin Long0b0dce72018-07-02 18:21:13 +08005563 *
5564 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
5565 * setting of the IPV6 flow label value. The value is
5566 * contained in the spp_ipv6_flowlabel field.
5567 * Upon retrieval, this flag will be set to indicate that
5568 * the spp_ipv6_flowlabel field has a valid value returned.
5569 * If a specific destination address is set (in the
5570 * spp_address field), then the value returned is that of
5571 * the address. If just an association is specified (and
5572 * no address), then the association's default flow label
5573 * is returned. If neither an association nor a destination
5574 * is specified, then the socket's default flow label is
5575 * returned. For non-IPv6 sockets, this flag will be left
5576 * cleared.
5577 *
5578 * SPP_DSCP: Setting this flag enables the setting of the
5579 * Differentiated Services Code Point (DSCP) value
5580 * associated with either the association or a specific
5581 * address. The value is obtained in the spp_dscp field.
5582 * Upon retrieval, this flag will be set to indicate that
5583 * the spp_dscp field has a valid value returned. If a
5584 * specific destination address is set when called (in the
5585 * spp_address field), then that specific destination
5586 * address's DSCP value is returned. If just an association
5587 * is specified, then the association's default DSCP is
5588 * returned. If neither an association nor a destination is
5589 * specified, then the socket's default DSCP is returned.
5590 *
5591 * spp_ipv6_flowlabel
5592 * - This field is used in conjunction with the
5593 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
5594 * The 20 least significant bits are used for the flow
5595 * label. This setting has precedence over any IPv6-layer
5596 * setting.
5597 *
5598 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
5599 * and contains the DSCP. The 6 most significant bits are
5600 * used for the DSCP. This setting has precedence over any
5601 * IPv4- or IPv6- layer setting.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 */
5603static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005604 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605{
Frank Filz52ccb8e2005-12-22 11:36:46 -08005606 struct sctp_paddrparams params;
5607 struct sctp_transport *trans = NULL;
5608 struct sctp_association *asoc = NULL;
5609 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
Xin Long0b0dce72018-07-02 18:21:13 +08005611 if (len >= sizeof(params))
5612 len = sizeof(params);
5613 else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
5614 spp_ipv6_flowlabel), 4))
5615 len = ALIGN(offsetof(struct sctp_paddrparams,
5616 spp_ipv6_flowlabel), 4);
5617 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 return -EINVAL;
Xin Long0b0dce72018-07-02 18:21:13 +08005619
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 if (copy_from_user(&params, optval, len))
5621 return -EFAULT;
5622
Frank Filz52ccb8e2005-12-22 11:36:46 -08005623 /* If an address other than INADDR_ANY is specified, and
5624 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 */
wangweidongcb3f8372013-12-23 12:16:50 +08005626 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005627 trans = sctp_addr_id2transport(sk, &params.spp_address,
5628 params.spp_assoc_id);
5629 if (!trans) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005630 pr_debug("%s: failed no transport\n", __func__);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005631 return -EINVAL;
5632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 }
5634
Frank Filz52ccb8e2005-12-22 11:36:46 -08005635 /* Get association, if assoc_id != 0 and the socket is a one
5636 * to many style socket, and an association was not found, then
5637 * the id was invalid.
5638 */
5639 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5640 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005641 pr_debug("%s: failed no association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08005643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644
Frank Filz52ccb8e2005-12-22 11:36:46 -08005645 if (trans) {
5646 /* Fetch transport values. */
5647 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5648 params.spp_pathmtu = trans->pathmtu;
5649 params.spp_pathmaxrxt = trans->pathmaxrxt;
5650 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651
Frank Filz52ccb8e2005-12-22 11:36:46 -08005652 /*draft-11 doesn't say what to return in spp_flags*/
5653 params.spp_flags = trans->param_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08005654 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5655 params.spp_ipv6_flowlabel = trans->flowlabel &
5656 SCTP_FLOWLABEL_VAL_MASK;
5657 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5658 }
5659 if (trans->dscp & SCTP_DSCP_SET_MASK) {
5660 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
5661 params.spp_flags |= SPP_DSCP;
5662 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08005663 } else if (asoc) {
5664 /* Fetch association values. */
5665 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5666 params.spp_pathmtu = asoc->pathmtu;
5667 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5668 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669
Frank Filz52ccb8e2005-12-22 11:36:46 -08005670 /*draft-11 doesn't say what to return in spp_flags*/
5671 params.spp_flags = asoc->param_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08005672 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5673 params.spp_ipv6_flowlabel = asoc->flowlabel &
5674 SCTP_FLOWLABEL_VAL_MASK;
5675 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5676 }
5677 if (asoc->dscp & SCTP_DSCP_SET_MASK) {
5678 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
5679 params.spp_flags |= SPP_DSCP;
5680 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08005681 } else {
5682 /* Fetch socket values. */
5683 params.spp_hbinterval = sp->hbinterval;
5684 params.spp_pathmtu = sp->pathmtu;
5685 params.spp_sackdelay = sp->sackdelay;
5686 params.spp_pathmaxrxt = sp->pathmaxrxt;
5687
5688 /*draft-11 doesn't say what to return in spp_flags*/
5689 params.spp_flags = sp->param_flags;
Xin Long0b0dce72018-07-02 18:21:13 +08005690 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5691 params.spp_ipv6_flowlabel = sp->flowlabel &
5692 SCTP_FLOWLABEL_VAL_MASK;
5693 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5694 }
5695 if (sp->dscp & SCTP_DSCP_SET_MASK) {
5696 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
5697 params.spp_flags |= SPP_DSCP;
5698 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08005699 }
5700
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 if (copy_to_user(optval, &params, len))
5702 return -EFAULT;
5703
5704 if (put_user(len, optlen))
5705 return -EFAULT;
5706
5707 return 0;
5708}
5709
Wei Yongjund364d922008-05-09 15:13:26 -07005710/*
5711 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08005712 *
Wei Yongjund364d922008-05-09 15:13:26 -07005713 * This option will effect the way delayed acks are performed. This
5714 * option allows you to get or set the delayed ack time, in
5715 * milliseconds. It also allows changing the delayed ack frequency.
5716 * Changing the frequency to 1 disables the delayed sack algorithm. If
5717 * the assoc_id is 0, then this sets or gets the endpoints default
5718 * values. If the assoc_id field is non-zero, then the set or get
5719 * effects the specified association for the one to many model (the
5720 * assoc_id field is ignored by the one to one model). Note that if
5721 * sack_delay or sack_freq are 0 when setting this option, then the
5722 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08005723 *
Wei Yongjund364d922008-05-09 15:13:26 -07005724 * struct sctp_sack_info {
5725 * sctp_assoc_t sack_assoc_id;
5726 * uint32_t sack_delay;
5727 * uint32_t sack_freq;
5728 * };
Frank Filz77086102005-12-22 11:37:30 -08005729 *
Wei Yongjund364d922008-05-09 15:13:26 -07005730 * sack_assoc_id - This parameter, indicates which association the user
5731 * is performing an action upon. Note that if this field's value is
5732 * zero then the endpoints default value is changed (effecting future
5733 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08005734 *
Wei Yongjund364d922008-05-09 15:13:26 -07005735 * sack_delay - This parameter contains the number of milliseconds that
5736 * the user is requesting the delayed ACK timer be set to. Note that
5737 * this value is defined in the standard to be between 200 and 500
5738 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08005739 *
Wei Yongjund364d922008-05-09 15:13:26 -07005740 * sack_freq - This parameter contains the number of packets that must
5741 * be received before a sack is sent without waiting for the delay
5742 * timer to expire. The default value for this is 2, setting this
5743 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08005744 */
Wei Yongjund364d922008-05-09 15:13:26 -07005745static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08005746 char __user *optval,
5747 int __user *optlen)
5748{
Wei Yongjund364d922008-05-09 15:13:26 -07005749 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08005750 struct sctp_association *asoc = NULL;
5751 struct sctp_sock *sp = sctp_sk(sk);
5752
Wei Yongjund364d922008-05-09 15:13:26 -07005753 if (len >= sizeof(struct sctp_sack_info)) {
5754 len = sizeof(struct sctp_sack_info);
5755
5756 if (copy_from_user(&params, optval, len))
5757 return -EFAULT;
5758 } else if (len == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05005759 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005760 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005761 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005762 "Use struct sctp_sack_info instead\n",
5763 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07005764 if (copy_from_user(&params, optval, len))
5765 return -EFAULT;
5766 } else
wangweidongcb3f8372013-12-23 12:16:50 +08005767 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08005768
Wei Yongjund364d922008-05-09 15:13:26 -07005769 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08005770 * to many style socket, and an association was not found, then
5771 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005772 */
Wei Yongjund364d922008-05-09 15:13:26 -07005773 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5774 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08005775 return -EINVAL;
5776
5777 if (asoc) {
5778 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005779 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5780 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08005781 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07005782 params.sack_freq = asoc->sackfreq;
5783
5784 } else {
5785 params.sack_delay = 0;
5786 params.sack_freq = 1;
5787 }
Frank Filz77086102005-12-22 11:37:30 -08005788 } else {
5789 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005790 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5791 params.sack_delay = sp->sackdelay;
5792 params.sack_freq = sp->sackfreq;
5793 } else {
5794 params.sack_delay = 0;
5795 params.sack_freq = 1;
5796 }
Frank Filz77086102005-12-22 11:37:30 -08005797 }
5798
5799 if (copy_to_user(optval, &params, len))
5800 return -EFAULT;
5801
5802 if (put_user(len, optlen))
5803 return -EFAULT;
5804
5805 return 0;
5806}
5807
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5809 *
5810 * Applications can specify protocol parameters for the default association
5811 * initialization. The option name argument to setsockopt() and getsockopt()
5812 * is SCTP_INITMSG.
5813 *
5814 * Setting initialization parameters is effective only on an unconnected
5815 * socket (for UDP-style sockets only future associations are effected
5816 * by the change). With TCP-style sockets, this option is inherited by
5817 * sockets derived from a listener socket.
5818 */
5819static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5820{
Neil Horman408f22e2007-06-16 14:03:45 -04005821 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005823 len = sizeof(struct sctp_initmsg);
5824 if (put_user(len, optlen))
5825 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5827 return -EFAULT;
5828 return 0;
5829}
5830
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005832static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5833 char __user *optval, int __user *optlen)
5834{
5835 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005836 int cnt = 0;
5837 struct sctp_getaddrs getaddrs;
5838 struct sctp_transport *from;
5839 void __user *to;
5840 union sctp_addr temp;
5841 struct sctp_sock *sp = sctp_sk(sk);
5842 int addrlen;
5843 size_t space_left;
5844 int bytes_copied;
5845
5846 if (len < sizeof(struct sctp_getaddrs))
5847 return -EINVAL;
5848
5849 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5850 return -EFAULT;
5851
5852 /* For UDP-style sockets, id specifies the association to query. */
5853 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5854 if (!asoc)
5855 return -EINVAL;
5856
wangweidongcb3f8372013-12-23 12:16:50 +08005857 to = optval + offsetof(struct sctp_getaddrs, addrs);
5858 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005859
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07005860 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5861 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08005862 memcpy(&temp, &from->ipaddr, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005863 addrlen = sctp_get_pf_specific(sk->sk_family)
5864 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005865 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005866 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005867 if (copy_to_user(to, &temp, addrlen))
5868 return -EFAULT;
5869 to += addrlen;
5870 cnt++;
5871 space_left -= addrlen;
5872 }
5873
5874 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5875 return -EFAULT;
5876 bytes_copied = ((char __user *)to) - optval;
5877 if (put_user(bytes_copied, optlen))
5878 return -EFAULT;
5879
5880 return 0;
5881}
5882
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005883static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5884 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005885{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005886 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005887 union sctp_addr temp;
5888 int cnt = 0;
5889 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005890 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005891
Vlad Yasevich29303542007-09-16 16:02:12 -07005892 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005893 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07005894 if (!addr->valid)
5895 continue;
5896
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005897 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08005898 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005899 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005900 if ((PF_INET6 == sk->sk_family) &&
5901 inet_v6_ipv6only(sk) &&
5902 (AF_INET == addr->a.sa.sa_family))
5903 continue;
Al Viro6244be42006-11-20 17:21:44 -08005904 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05005905 if (!temp.v4.sin_port)
5906 temp.v4.sin_port = htons(port);
5907
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005908 addrlen = sctp_get_pf_specific(sk->sk_family)
5909 ->addr_to_user(sctp_sk(sk), &temp);
5910
Vlad Yasevich29303542007-09-16 16:02:12 -07005911 if (space_left < addrlen) {
5912 cnt = -ENOMEM;
5913 break;
5914 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005915 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08005916
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005917 to += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005918 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005919 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04005920 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005921 }
Vlad Yasevich29303542007-09-16 16:02:12 -07005922 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005923
5924 return cnt;
5925}
5926
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005928static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5929 char __user *optval, int __user *optlen)
5930{
5931 struct sctp_bind_addr *bp;
5932 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005933 int cnt = 0;
5934 struct sctp_getaddrs getaddrs;
5935 struct sctp_sockaddr_entry *addr;
5936 void __user *to;
5937 union sctp_addr temp;
5938 struct sctp_sock *sp = sctp_sk(sk);
5939 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005940 int err = 0;
5941 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005942 int bytes_copied = 0;
5943 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005944 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005945
Neil Horman408f22e2007-06-16 14:03:45 -04005946 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005947 return -EINVAL;
5948
5949 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5950 return -EFAULT;
5951
5952 /*
5953 * For UDP-style sockets, id specifies the association to query.
5954 * If the id field is set to the value '0' then the locally bound
5955 * addresses are returned without regard to any particular
5956 * association.
5957 */
5958 if (0 == getaddrs.assoc_id) {
5959 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005960 } else {
5961 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5962 if (!asoc)
5963 return -EINVAL;
5964 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005965 }
5966
wangweidongcb3f8372013-12-23 12:16:50 +08005967 to = optval + offsetof(struct sctp_getaddrs, addrs);
5968 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Neil Horman186e2342007-06-18 19:59:16 -04005969
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02005970 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005971 if (!addrs)
5972 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005973
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005974 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5975 * addresses from the global local address list.
5976 */
5977 if (sctp_list_single_entry(&bp->address_list)) {
5978 addr = list_entry(bp->address_list.next,
5979 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04005980 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005981 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5982 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005983 if (cnt < 0) {
5984 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005985 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005986 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005987 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005988 }
5989 }
5990
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005991 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005992 /* Protection on the bound address list is not needed since
5993 * in the socket option context we hold a socket lock and
5994 * thus the bound address list can't change.
5995 */
5996 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08005997 memcpy(&temp, &addr->a, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005998 addrlen = sctp_get_pf_specific(sk->sk_family)
5999 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006000 if (space_left < addrlen) {
6001 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07006002 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006003 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07006004 memcpy(buf, &temp, addrlen);
6005 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006006 bytes_copied += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08006007 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006008 space_left -= addrlen;
6009 }
6010
6011copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006012 if (copy_to_user(to, addrs, bytes_copied)) {
6013 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02006014 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006015 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04006016 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
6017 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02006018 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04006019 }
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006020 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
6021 * but we can't change it anymore.
6022 */
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006023 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04006024 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02006025out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07006026 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07006027 return err;
6028}
6029
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
6031 *
6032 * Requests that the local SCTP stack use the enclosed peer address as
6033 * the association primary. The enclosed address must be one of the
6034 * association peer's addresses.
6035 */
6036static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
6037 char __user *optval, int __user *optlen)
6038{
6039 struct sctp_prim prim;
6040 struct sctp_association *asoc;
6041 struct sctp_sock *sp = sctp_sk(sk);
6042
Neil Horman408f22e2007-06-16 14:03:45 -04006043 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 return -EINVAL;
6045
Neil Horman408f22e2007-06-16 14:03:45 -04006046 len = sizeof(struct sctp_prim);
6047
6048 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 return -EFAULT;
6050
6051 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
6052 if (!asoc)
6053 return -EINVAL;
6054
6055 if (!asoc->peer.primary_path)
6056 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006057
Al Viro8cec6b82006-11-20 17:23:01 -08006058 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
6059 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
Jason Gunthorpe299ee122014-07-30 12:40:53 -06006061 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 (union sctp_addr *)&prim.ssp_addr);
6063
Neil Horman408f22e2007-06-16 14:03:45 -04006064 if (put_user(len, optlen))
6065 return -EFAULT;
6066 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 return -EFAULT;
6068
6069 return 0;
6070}
6071
6072/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006073 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006075 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 * Indication parameter for all future INIT and INIT-ACK exchanges.
6077 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006078static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 char __user *optval, int __user *optlen)
6080{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006081 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082
Neil Horman408f22e2007-06-16 14:03:45 -04006083 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 return -EINVAL;
6085
Neil Horman408f22e2007-06-16 14:03:45 -04006086 len = sizeof(struct sctp_setadaptation);
6087
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006088 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04006089
6090 if (put_user(len, optlen))
6091 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006092 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07006094
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 return 0;
6096}
6097
6098/*
6099 *
6100 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
6101 *
6102 * Applications that wish to use the sendto() system call may wish to
6103 * specify a default set of parameters that would normally be supplied
6104 * through the inclusion of ancillary data. This socket option allows
6105 * such an application to set the default sctp_sndrcvinfo structure.
6106
6107
6108 * The application that wishes to use this socket option simply passes
6109 * in to this call the sctp_sndrcvinfo structure defined in Section
6110 * 5.2.2) The input parameters accepted by this call include
6111 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
6112 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
6113 * to this call if the caller is using the UDP model.
6114 *
6115 * For getsockopt, it get the default sctp_sndrcvinfo structure.
6116 */
6117static int sctp_getsockopt_default_send_param(struct sock *sk,
6118 int len, char __user *optval,
6119 int __user *optlen)
6120{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006122 struct sctp_association *asoc;
6123 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006125 if (len < sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04006127
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006128 len = sizeof(info);
Neil Horman408f22e2007-06-16 14:03:45 -04006129
6130 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 return -EFAULT;
6132
6133 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
6134 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
6135 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 if (asoc) {
6137 info.sinfo_stream = asoc->default_stream;
6138 info.sinfo_flags = asoc->default_flags;
6139 info.sinfo_ppid = asoc->default_ppid;
6140 info.sinfo_context = asoc->default_context;
6141 info.sinfo_timetolive = asoc->default_timetolive;
6142 } else {
6143 info.sinfo_stream = sp->default_stream;
6144 info.sinfo_flags = sp->default_flags;
6145 info.sinfo_ppid = sp->default_ppid;
6146 info.sinfo_context = sp->default_context;
6147 info.sinfo_timetolive = sp->default_timetolive;
6148 }
6149
Neil Horman408f22e2007-06-16 14:03:45 -04006150 if (put_user(len, optlen))
6151 return -EFAULT;
6152 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 return -EFAULT;
6154
6155 return 0;
6156}
6157
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006158/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
6159 * (SCTP_DEFAULT_SNDINFO)
6160 */
6161static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
6162 char __user *optval,
6163 int __user *optlen)
6164{
6165 struct sctp_sock *sp = sctp_sk(sk);
6166 struct sctp_association *asoc;
6167 struct sctp_sndinfo info;
6168
6169 if (len < sizeof(info))
6170 return -EINVAL;
6171
6172 len = sizeof(info);
6173
6174 if (copy_from_user(&info, optval, len))
6175 return -EFAULT;
6176
6177 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
6178 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
6179 return -EINVAL;
6180 if (asoc) {
6181 info.snd_sid = asoc->default_stream;
6182 info.snd_flags = asoc->default_flags;
6183 info.snd_ppid = asoc->default_ppid;
6184 info.snd_context = asoc->default_context;
6185 } else {
6186 info.snd_sid = sp->default_stream;
6187 info.snd_flags = sp->default_flags;
6188 info.snd_ppid = sp->default_ppid;
6189 info.snd_context = sp->default_context;
6190 }
6191
6192 if (put_user(len, optlen))
6193 return -EFAULT;
6194 if (copy_to_user(optval, &info, len))
6195 return -EFAULT;
6196
6197 return 0;
6198}
6199
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200/*
6201 *
6202 * 7.1.5 SCTP_NODELAY
6203 *
6204 * Turn on/off any Nagle-like algorithm. This means that packets are
6205 * generally sent as soon as possible and no unnecessary delays are
6206 * introduced, at the cost of more packets in the network. Expects an
6207 * integer boolean flag.
6208 */
6209
6210static int sctp_getsockopt_nodelay(struct sock *sk, int len,
6211 char __user *optval, int __user *optlen)
6212{
6213 int val;
6214
6215 if (len < sizeof(int))
6216 return -EINVAL;
6217
6218 len = sizeof(int);
6219 val = (sctp_sk(sk)->nodelay == 1);
6220 if (put_user(len, optlen))
6221 return -EFAULT;
6222 if (copy_to_user(optval, &val, len))
6223 return -EFAULT;
6224 return 0;
6225}
6226
6227/*
6228 *
6229 * 7.1.1 SCTP_RTOINFO
6230 *
6231 * The protocol parameters used to initialize and bound retransmission
6232 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
6233 * and modify these parameters.
6234 * All parameters are time values, in milliseconds. A value of 0, when
6235 * modifying the parameters, indicates that the current value should not
6236 * be changed.
6237 *
6238 */
6239static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6240 char __user *optval,
6241 int __user *optlen) {
6242 struct sctp_rtoinfo rtoinfo;
6243 struct sctp_association *asoc;
6244
Neil Horman408f22e2007-06-16 14:03:45 -04006245 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 return -EINVAL;
6247
Neil Horman408f22e2007-06-16 14:03:45 -04006248 len = sizeof(struct sctp_rtoinfo);
6249
6250 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 return -EFAULT;
6252
6253 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6254
6255 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
6256 return -EINVAL;
6257
6258 /* Values corresponding to the specific association. */
6259 if (asoc) {
6260 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6261 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6262 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6263 } else {
6264 /* Values corresponding to the endpoint. */
6265 struct sctp_sock *sp = sctp_sk(sk);
6266
6267 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6268 rtoinfo.srto_max = sp->rtoinfo.srto_max;
6269 rtoinfo.srto_min = sp->rtoinfo.srto_min;
6270 }
6271
6272 if (put_user(len, optlen))
6273 return -EFAULT;
6274
6275 if (copy_to_user(optval, &rtoinfo, len))
6276 return -EFAULT;
6277
6278 return 0;
6279}
6280
6281/*
6282 *
6283 * 7.1.2 SCTP_ASSOCINFO
6284 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02006285 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 * of the association.
6287 * Returns an error if the new association retransmission value is
6288 * greater than the sum of the retransmission value of the peer.
6289 * See [SCTP] for more information.
6290 *
6291 */
6292static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6293 char __user *optval,
6294 int __user *optlen)
6295{
6296
6297 struct sctp_assocparams assocparams;
6298 struct sctp_association *asoc;
6299 struct list_head *pos;
6300 int cnt = 0;
6301
Neil Horman408f22e2007-06-16 14:03:45 -04006302 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303 return -EINVAL;
6304
Neil Horman408f22e2007-06-16 14:03:45 -04006305 len = sizeof(struct sctp_assocparams);
6306
6307 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 return -EFAULT;
6309
6310 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6311
6312 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
6313 return -EINVAL;
6314
6315 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07006316 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6318 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6319 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
Daniel Borkmann52db8822013-06-25 18:17:27 +02006320 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321
6322 list_for_each(pos, &asoc->peer.transport_addr_list) {
wangweidongcb3f8372013-12-23 12:16:50 +08006323 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 }
6325
6326 assocparams.sasoc_number_peer_destinations = cnt;
6327 } else {
6328 /* Values corresponding to the endpoint */
6329 struct sctp_sock *sp = sctp_sk(sk);
6330
6331 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6332 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6333 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6334 assocparams.sasoc_cookie_life =
6335 sp->assocparams.sasoc_cookie_life;
6336 assocparams.sasoc_number_peer_destinations =
6337 sp->assocparams.
6338 sasoc_number_peer_destinations;
6339 }
6340
6341 if (put_user(len, optlen))
6342 return -EFAULT;
6343
6344 if (copy_to_user(optval, &assocparams, len))
6345 return -EFAULT;
6346
6347 return 0;
6348}
6349
6350/*
6351 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6352 *
6353 * This socket option is a boolean flag which turns on or off mapped V4
6354 * addresses. If this option is turned on and the socket is type
6355 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6356 * If this option is turned off, then no mapping will be done of V4
6357 * addresses and a user will receive both PF_INET6 and PF_INET type
6358 * addresses on the socket.
6359 */
6360static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6361 char __user *optval, int __user *optlen)
6362{
6363 int val;
6364 struct sctp_sock *sp = sctp_sk(sk);
6365
6366 if (len < sizeof(int))
6367 return -EINVAL;
6368
6369 len = sizeof(int);
6370 val = sp->v4mapped;
6371 if (put_user(len, optlen))
6372 return -EFAULT;
6373 if (copy_to_user(optval, &val, len))
6374 return -EFAULT;
6375
6376 return 0;
6377}
6378
6379/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006380 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6381 * (chapter and verse is quoted at sctp_setsockopt_context())
6382 */
6383static int sctp_getsockopt_context(struct sock *sk, int len,
6384 char __user *optval, int __user *optlen)
6385{
6386 struct sctp_assoc_value params;
6387 struct sctp_sock *sp;
6388 struct sctp_association *asoc;
6389
Neil Horman408f22e2007-06-16 14:03:45 -04006390 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006391 return -EINVAL;
6392
Neil Horman408f22e2007-06-16 14:03:45 -04006393 len = sizeof(struct sctp_assoc_value);
6394
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006395 if (copy_from_user(&params, optval, len))
6396 return -EFAULT;
6397
6398 sp = sctp_sk(sk);
6399
6400 if (params.assoc_id != 0) {
6401 asoc = sctp_id2assoc(sk, params.assoc_id);
6402 if (!asoc)
6403 return -EINVAL;
6404 params.assoc_value = asoc->default_rcv_context;
6405 } else {
6406 params.assoc_value = sp->default_rcv_context;
6407 }
6408
6409 if (put_user(len, optlen))
6410 return -EFAULT;
6411 if (copy_to_user(optval, &params, len))
6412 return -EFAULT;
6413
6414 return 0;
6415}
6416
6417/*
Wei Yongjune89c2092008-12-25 16:54:58 -08006418 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6419 * This option will get or set the maximum size to put in any outgoing
6420 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 * fragmented by SCTP into the specified size. Note that the underlying
6422 * SCTP implementation may fragment into smaller sized chunks when the
6423 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08006424 * the user. The default value for this option is '0' which indicates
6425 * the user is NOT limiting fragmentation and only the PMTU will effect
6426 * SCTP's choice of DATA chunk size. Note also that values set larger
6427 * than the maximum size of an IP datagram will effectively let SCTP
6428 * control fragmentation (i.e. the same as setting this option to 0).
6429 *
6430 * The following structure is used to access and modify this parameter:
6431 *
6432 * struct sctp_assoc_value {
6433 * sctp_assoc_t assoc_id;
6434 * uint32_t assoc_value;
6435 * };
6436 *
6437 * assoc_id: This parameter is ignored for one-to-one style sockets.
6438 * For one-to-many style sockets this parameter indicates which
6439 * association the user is performing an action upon. Note that if
6440 * this field's value is zero then the endpoints default value is
6441 * changed (effecting future associations only).
6442 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 */
6444static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6445 char __user *optval, int __user *optlen)
6446{
Wei Yongjune89c2092008-12-25 16:54:58 -08006447 struct sctp_assoc_value params;
6448 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449
Wei Yongjune89c2092008-12-25 16:54:58 -08006450 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006451 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006452 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006453 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006454 "Use struct sctp_assoc_value instead\n",
6455 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08006456 params.assoc_id = 0;
6457 } else if (len >= sizeof(struct sctp_assoc_value)) {
6458 len = sizeof(struct sctp_assoc_value);
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006459 if (copy_from_user(&params, optval, len))
Wei Yongjune89c2092008-12-25 16:54:58 -08006460 return -EFAULT;
6461 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 return -EINVAL;
6463
Wei Yongjune89c2092008-12-25 16:54:58 -08006464 asoc = sctp_id2assoc(sk, params.assoc_id);
6465 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
6466 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467
Wei Yongjune89c2092008-12-25 16:54:58 -08006468 if (asoc)
6469 params.assoc_value = asoc->frag_point;
6470 else
6471 params.assoc_value = sctp_sk(sk)->user_frag;
6472
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 if (put_user(len, optlen))
6474 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08006475 if (len == sizeof(int)) {
6476 if (copy_to_user(optval, &params.assoc_value, len))
6477 return -EFAULT;
6478 } else {
6479 if (copy_to_user(optval, &params, len))
6480 return -EFAULT;
6481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482
6483 return 0;
6484}
6485
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006486/*
6487 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6488 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6489 */
6490static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6491 char __user *optval, int __user *optlen)
6492{
6493 int val;
6494
6495 if (len < sizeof(int))
6496 return -EINVAL;
6497
6498 len = sizeof(int);
6499
6500 val = sctp_sk(sk)->frag_interleave;
6501 if (put_user(len, optlen))
6502 return -EFAULT;
6503 if (copy_to_user(optval, &val, len))
6504 return -EFAULT;
6505
6506 return 0;
6507}
6508
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006509/*
6510 * 7.1.25. Set or Get the sctp partial delivery point
6511 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6512 */
6513static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6514 char __user *optval,
6515 int __user *optlen)
6516{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09006517 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006518
6519 if (len < sizeof(u32))
6520 return -EINVAL;
6521
6522 len = sizeof(u32);
6523
6524 val = sctp_sk(sk)->pd_point;
6525 if (put_user(len, optlen))
6526 return -EFAULT;
6527 if (copy_to_user(optval, &val, len))
6528 return -EFAULT;
6529
Wei Yongjun7d743b72010-12-14 16:10:41 +00006530 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006531}
6532
Vlad Yasevich70331572007-03-23 11:34:36 -07006533/*
6534 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6535 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6536 */
6537static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6538 char __user *optval,
6539 int __user *optlen)
6540{
Neil Horman219b99a2008-03-05 13:44:46 -08006541 struct sctp_assoc_value params;
6542 struct sctp_sock *sp;
6543 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07006544
Neil Horman219b99a2008-03-05 13:44:46 -08006545 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006546 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006547 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006548 "Use of int in max_burst socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006549 "Use struct sctp_assoc_value instead\n",
6550 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08006551 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00006552 } else if (len >= sizeof(struct sctp_assoc_value)) {
6553 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08006554 if (copy_from_user(&params, optval, len))
6555 return -EFAULT;
6556 } else
6557 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07006558
Neil Horman219b99a2008-03-05 13:44:46 -08006559 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07006560
Neil Horman219b99a2008-03-05 13:44:46 -08006561 if (params.assoc_id != 0) {
6562 asoc = sctp_id2assoc(sk, params.assoc_id);
6563 if (!asoc)
6564 return -EINVAL;
6565 params.assoc_value = asoc->max_burst;
6566 } else
6567 params.assoc_value = sp->max_burst;
6568
6569 if (len == sizeof(int)) {
6570 if (copy_to_user(optval, &params.assoc_value, len))
6571 return -EFAULT;
6572 } else {
6573 if (copy_to_user(optval, &params, len))
6574 return -EFAULT;
6575 }
6576
6577 return 0;
6578
Vlad Yasevich70331572007-03-23 11:34:36 -07006579}
6580
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006581static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6582 char __user *optval, int __user *optlen)
6583{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006584 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006585 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006586 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006587 __u16 data_len = 0;
6588 u32 num_idents;
Xin Long7a84bd42016-02-03 23:33:30 +08006589 int i;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006590
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006591 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006592 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006593
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006594 hmacs = ep->auth_hmacs_list;
Xin Long3c918702017-06-30 11:52:16 +08006595 data_len = ntohs(hmacs->param_hdr.length) -
6596 sizeof(struct sctp_paramhdr);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006597
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006598 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006599 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006600
6601 len = sizeof(struct sctp_hmacalgo) + data_len;
6602 num_idents = data_len / sizeof(u16);
6603
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006604 if (put_user(len, optlen))
6605 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006606 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006607 return -EFAULT;
Xin Long7a84bd42016-02-03 23:33:30 +08006608 for (i = 0; i < num_idents; i++) {
6609 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6610
6611 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6612 return -EFAULT;
6613 }
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006614 return 0;
6615}
6616
6617static int sctp_getsockopt_active_key(struct sock *sk, int len,
6618 char __user *optval, int __user *optlen)
6619{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006620 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006621 struct sctp_authkeyid val;
6622 struct sctp_association *asoc;
6623
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006624 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006625 return -EACCES;
6626
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006627 if (len < sizeof(struct sctp_authkeyid))
6628 return -EINVAL;
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006629
6630 len = sizeof(struct sctp_authkeyid);
6631 if (copy_from_user(&val, optval, len))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006632 return -EFAULT;
6633
6634 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6635 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6636 return -EINVAL;
6637
6638 if (asoc)
6639 val.scact_keynumber = asoc->active_key_id;
6640 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006641 val.scact_keynumber = ep->active_key_id;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006642
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006643 if (put_user(len, optlen))
6644 return -EFAULT;
6645 if (copy_to_user(optval, &val, len))
6646 return -EFAULT;
6647
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006648 return 0;
6649}
6650
6651static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6652 char __user *optval, int __user *optlen)
6653{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006654 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006655 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006656 struct sctp_authchunks val;
6657 struct sctp_association *asoc;
6658 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006659 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006660 char __user *to;
6661
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006662 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006663 return -EACCES;
6664
6665 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006666 return -EINVAL;
6667
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006668 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006669 return -EFAULT;
6670
Al Viro411223c2007-10-14 19:21:20 +01006671 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006672 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6673 if (!asoc)
6674 return -EINVAL;
6675
6676 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006677 if (!ch)
6678 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006679
6680 /* See if the user provided enough room for all the data */
Xin Long3c918702017-06-30 11:52:16 +08006681 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevichb40db682008-02-27 14:40:37 -05006682 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006683 return -EINVAL;
6684
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006685 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006686 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006687num:
6688 len = sizeof(struct sctp_authchunks) + num_chunks;
wangweidong8d726512013-12-23 12:16:53 +08006689 if (put_user(len, optlen))
6690 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006691 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6692 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006693 return 0;
6694}
6695
6696static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6697 char __user *optval, int __user *optlen)
6698{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006699 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006700 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006701 struct sctp_authchunks val;
6702 struct sctp_association *asoc;
6703 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006704 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006705 char __user *to;
6706
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006707 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006708 return -EACCES;
6709
6710 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006711 return -EINVAL;
6712
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006713 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006714 return -EFAULT;
6715
Al Viro411223c2007-10-14 19:21:20 +01006716 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006717 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6718 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6719 return -EINVAL;
6720
6721 if (asoc)
wangweidong26ac8e52013-12-23 12:16:51 +08006722 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006723 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006724 ch = ep->auth_chunk_list;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006725
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006726 if (!ch)
6727 goto num;
6728
Xin Long3c918702017-06-30 11:52:16 +08006729 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006730 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006731 return -EINVAL;
6732
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006733 if (copy_to_user(to, ch->chunks, num_chunks))
6734 return -EFAULT;
6735num:
6736 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006737 if (put_user(len, optlen))
6738 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006739 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6740 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006741
6742 return 0;
6743}
6744
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08006745/*
6746 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6747 * This option gets the current number of associations that are attached
6748 * to a one-to-many style socket. The option value is an uint32_t.
6749 */
6750static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6751 char __user *optval, int __user *optlen)
6752{
6753 struct sctp_sock *sp = sctp_sk(sk);
6754 struct sctp_association *asoc;
6755 u32 val = 0;
6756
6757 if (sctp_style(sk, TCP))
6758 return -EOPNOTSUPP;
6759
6760 if (len < sizeof(u32))
6761 return -EINVAL;
6762
6763 len = sizeof(u32);
6764
6765 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6766 val++;
6767 }
6768
6769 if (put_user(len, optlen))
6770 return -EFAULT;
6771 if (copy_to_user(optval, &val, len))
6772 return -EFAULT;
6773
6774 return 0;
6775}
6776
Wei Yongjun209ba422011-04-17 17:27:08 +00006777/*
Michio Honda7dc04d72011-04-26 20:16:31 +09006778 * 8.1.23 SCTP_AUTO_ASCONF
6779 * See the corresponding setsockopt entry as description
6780 */
6781static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6782 char __user *optval, int __user *optlen)
6783{
6784 int val = 0;
6785
6786 if (len < sizeof(int))
6787 return -EINVAL;
6788
6789 len = sizeof(int);
6790 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6791 val = 1;
6792 if (put_user(len, optlen))
6793 return -EFAULT;
6794 if (copy_to_user(optval, &val, len))
6795 return -EFAULT;
6796 return 0;
6797}
6798
6799/*
Wei Yongjun209ba422011-04-17 17:27:08 +00006800 * 8.2.6. Get the Current Identifiers of Associations
6801 * (SCTP_GET_ASSOC_ID_LIST)
6802 *
6803 * This option gets the current list of SCTP association identifiers of
6804 * the SCTP associations handled by a one-to-many style socket.
6805 */
6806static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6807 char __user *optval, int __user *optlen)
6808{
6809 struct sctp_sock *sp = sctp_sk(sk);
6810 struct sctp_association *asoc;
6811 struct sctp_assoc_ids *ids;
6812 u32 num = 0;
6813
6814 if (sctp_style(sk, TCP))
6815 return -EOPNOTSUPP;
6816
6817 if (len < sizeof(struct sctp_assoc_ids))
6818 return -EINVAL;
6819
6820 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6821 num++;
6822 }
6823
6824 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6825 return -EINVAL;
6826
6827 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6828
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02006829 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
Wei Yongjun209ba422011-04-17 17:27:08 +00006830 if (unlikely(!ids))
6831 return -ENOMEM;
6832
6833 ids->gaids_number_of_ids = num;
6834 num = 0;
6835 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6836 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6837 }
6838
6839 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6840 kfree(ids);
6841 return -EFAULT;
6842 }
6843
6844 kfree(ids);
6845 return 0;
6846}
6847
Neil Horman5aa93bc2012-07-21 07:56:07 +00006848/*
6849 * SCTP_PEER_ADDR_THLDS
6850 *
6851 * This option allows us to fetch the partially failed threshold for one or all
6852 * transports in an association. See Section 6.1 of:
6853 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6854 */
6855static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6856 char __user *optval,
6857 int len,
6858 int __user *optlen)
6859{
6860 struct sctp_paddrthlds val;
6861 struct sctp_transport *trans;
6862 struct sctp_association *asoc;
6863
6864 if (len < sizeof(struct sctp_paddrthlds))
6865 return -EINVAL;
6866 len = sizeof(struct sctp_paddrthlds);
6867 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6868 return -EFAULT;
6869
6870 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6871 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6872 if (!asoc)
6873 return -ENOENT;
6874
6875 val.spt_pathpfthld = asoc->pf_retrans;
6876 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6877 } else {
6878 trans = sctp_addr_id2transport(sk, &val.spt_address,
6879 val.spt_assoc_id);
6880 if (!trans)
6881 return -ENOENT;
6882
6883 val.spt_pathmaxrxt = trans->pathmaxrxt;
6884 val.spt_pathpfthld = trans->pf_retrans;
6885 }
6886
6887 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6888 return -EFAULT;
6889
6890 return 0;
6891}
6892
Michele Baldessari196d6752012-12-01 04:49:42 +00006893/*
6894 * SCTP_GET_ASSOC_STATS
6895 *
6896 * This option retrieves local per endpoint statistics. It is modeled
6897 * after OpenSolaris' implementation
6898 */
6899static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6900 char __user *optval,
6901 int __user *optlen)
6902{
6903 struct sctp_assoc_stats sas;
6904 struct sctp_association *asoc = NULL;
6905
6906 /* User must provide at least the assoc id */
6907 if (len < sizeof(sctp_assoc_t))
6908 return -EINVAL;
6909
Guenter Roeck726bc6b2013-02-27 10:57:31 +00006910 /* Allow the struct to grow and fill in as much as possible */
6911 len = min_t(size_t, len, sizeof(sas));
6912
Michele Baldessari196d6752012-12-01 04:49:42 +00006913 if (copy_from_user(&sas, optval, len))
6914 return -EFAULT;
6915
6916 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6917 if (!asoc)
6918 return -EINVAL;
6919
6920 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6921 sas.sas_gapcnt = asoc->stats.gapcnt;
6922 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6923 sas.sas_osacks = asoc->stats.osacks;
6924 sas.sas_isacks = asoc->stats.isacks;
6925 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6926 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6927 sas.sas_oodchunks = asoc->stats.oodchunks;
6928 sas.sas_iodchunks = asoc->stats.iodchunks;
6929 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6930 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6931 sas.sas_idupchunks = asoc->stats.idupchunks;
6932 sas.sas_opackets = asoc->stats.opackets;
6933 sas.sas_ipackets = asoc->stats.ipackets;
6934
6935 /* New high max rto observed, will return 0 if not a single
6936 * RTO update took place. obs_rto_ipaddr will be bogus
6937 * in such a case
6938 */
6939 sas.sas_maxrto = asoc->stats.max_obs_rto;
6940 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6941 sizeof(struct sockaddr_storage));
6942
6943 /* Mark beginning of a new observation period */
6944 asoc->stats.max_obs_rto = asoc->rto_min;
6945
Michele Baldessari196d6752012-12-01 04:49:42 +00006946 if (put_user(len, optlen))
6947 return -EFAULT;
6948
Daniel Borkmannbb333812013-06-28 19:49:40 +02006949 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
Michele Baldessari196d6752012-12-01 04:49:42 +00006950
6951 if (copy_to_user(optval, &sas, len))
6952 return -EFAULT;
6953
6954 return 0;
6955}
6956
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02006957static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6958 char __user *optval,
6959 int __user *optlen)
6960{
6961 int val = 0;
6962
6963 if (len < sizeof(int))
6964 return -EINVAL;
6965
6966 len = sizeof(int);
6967 if (sctp_sk(sk)->recvrcvinfo)
6968 val = 1;
6969 if (put_user(len, optlen))
6970 return -EFAULT;
6971 if (copy_to_user(optval, &val, len))
6972 return -EFAULT;
6973
6974 return 0;
6975}
6976
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02006977static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6978 char __user *optval,
6979 int __user *optlen)
6980{
6981 int val = 0;
6982
6983 if (len < sizeof(int))
6984 return -EINVAL;
6985
6986 len = sizeof(int);
6987 if (sctp_sk(sk)->recvnxtinfo)
6988 val = 1;
6989 if (put_user(len, optlen))
6990 return -EFAULT;
6991 if (copy_to_user(optval, &val, len))
6992 return -EFAULT;
6993
6994 return 0;
6995}
6996
Xin Long28aa4c22016-07-09 19:47:40 +08006997static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6998 char __user *optval,
6999 int __user *optlen)
7000{
7001 struct sctp_assoc_value params;
7002 struct sctp_association *asoc;
7003 int retval = -EFAULT;
7004
7005 if (len < sizeof(params)) {
7006 retval = -EINVAL;
7007 goto out;
7008 }
7009
7010 len = sizeof(params);
7011 if (copy_from_user(&params, optval, len))
7012 goto out;
7013
7014 asoc = sctp_id2assoc(sk, params.assoc_id);
7015 if (asoc) {
7016 params.assoc_value = asoc->prsctp_enable;
7017 } else if (!params.assoc_id) {
7018 struct sctp_sock *sp = sctp_sk(sk);
7019
7020 params.assoc_value = sp->ep->prsctp_enable;
7021 } else {
7022 retval = -EINVAL;
7023 goto out;
7024 }
7025
7026 if (put_user(len, optlen))
7027 goto out;
7028
7029 if (copy_to_user(optval, &params, len))
7030 goto out;
7031
7032 retval = 0;
7033
7034out:
7035 return retval;
7036}
7037
Xin Longf959fb42016-07-09 19:47:41 +08007038static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
7039 char __user *optval,
7040 int __user *optlen)
7041{
7042 struct sctp_default_prinfo info;
7043 struct sctp_association *asoc;
7044 int retval = -EFAULT;
7045
7046 if (len < sizeof(info)) {
7047 retval = -EINVAL;
7048 goto out;
7049 }
7050
7051 len = sizeof(info);
7052 if (copy_from_user(&info, optval, len))
7053 goto out;
7054
7055 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
7056 if (asoc) {
7057 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
7058 info.pr_value = asoc->default_timetolive;
7059 } else if (!info.pr_assoc_id) {
7060 struct sctp_sock *sp = sctp_sk(sk);
7061
7062 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
7063 info.pr_value = sp->default_timetolive;
7064 } else {
7065 retval = -EINVAL;
7066 goto out;
7067 }
7068
7069 if (put_user(len, optlen))
7070 goto out;
7071
7072 if (copy_to_user(optval, &info, len))
7073 goto out;
7074
7075 retval = 0;
7076
7077out:
7078 return retval;
7079}
7080
Xin Long826d2532016-07-09 19:47:42 +08007081static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
7082 char __user *optval,
7083 int __user *optlen)
7084{
7085 struct sctp_prstatus params;
7086 struct sctp_association *asoc;
7087 int policy;
7088 int retval = -EINVAL;
7089
7090 if (len < sizeof(params))
7091 goto out;
7092
7093 len = sizeof(params);
7094 if (copy_from_user(&params, optval, len)) {
7095 retval = -EFAULT;
7096 goto out;
7097 }
7098
7099 policy = params.sprstat_policy;
7100 if (policy & ~SCTP_PR_SCTP_MASK)
7101 goto out;
7102
7103 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7104 if (!asoc)
7105 goto out;
7106
7107 if (policy == SCTP_PR_SCTP_NONE) {
7108 params.sprstat_abandoned_unsent = 0;
7109 params.sprstat_abandoned_sent = 0;
7110 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7111 params.sprstat_abandoned_unsent +=
7112 asoc->abandoned_unsent[policy];
7113 params.sprstat_abandoned_sent +=
7114 asoc->abandoned_sent[policy];
7115 }
7116 } else {
7117 params.sprstat_abandoned_unsent =
7118 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7119 params.sprstat_abandoned_sent =
7120 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
7121 }
7122
7123 if (put_user(len, optlen)) {
7124 retval = -EFAULT;
7125 goto out;
7126 }
7127
7128 if (copy_to_user(optval, &params, len)) {
7129 retval = -EFAULT;
7130 goto out;
7131 }
7132
7133 retval = 0;
7134
7135out:
7136 return retval;
7137}
7138
Xin Longd229d482017-04-01 17:07:46 +08007139static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
7140 char __user *optval,
7141 int __user *optlen)
7142{
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007143 struct sctp_stream_out_ext *streamoute;
Xin Longd229d482017-04-01 17:07:46 +08007144 struct sctp_association *asoc;
7145 struct sctp_prstatus params;
7146 int retval = -EINVAL;
7147 int policy;
7148
7149 if (len < sizeof(params))
7150 goto out;
7151
7152 len = sizeof(params);
7153 if (copy_from_user(&params, optval, len)) {
7154 retval = -EFAULT;
7155 goto out;
7156 }
7157
7158 policy = params.sprstat_policy;
7159 if (policy & ~SCTP_PR_SCTP_MASK)
7160 goto out;
7161
7162 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
Xin Longcee360a2017-05-31 16:36:31 +08007163 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
Xin Longd229d482017-04-01 17:07:46 +08007164 goto out;
7165
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03007166 streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007167 if (!streamoute) {
7168 /* Not allocated yet, means all stats are 0 */
7169 params.sprstat_abandoned_unsent = 0;
7170 params.sprstat_abandoned_sent = 0;
7171 retval = 0;
7172 goto out;
7173 }
7174
Xin Longd229d482017-04-01 17:07:46 +08007175 if (policy == SCTP_PR_SCTP_NONE) {
7176 params.sprstat_abandoned_unsent = 0;
7177 params.sprstat_abandoned_sent = 0;
7178 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7179 params.sprstat_abandoned_unsent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007180 streamoute->abandoned_unsent[policy];
Xin Longd229d482017-04-01 17:07:46 +08007181 params.sprstat_abandoned_sent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007182 streamoute->abandoned_sent[policy];
Xin Longd229d482017-04-01 17:07:46 +08007183 }
7184 } else {
7185 params.sprstat_abandoned_unsent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007186 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08007187 params.sprstat_abandoned_sent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03007188 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08007189 }
7190
7191 if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
7192 retval = -EFAULT;
7193 goto out;
7194 }
7195
7196 retval = 0;
7197
7198out:
7199 return retval;
7200}
7201
Xin Longc0d8bab2017-03-10 12:11:12 +08007202static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
7203 char __user *optval,
7204 int __user *optlen)
7205{
7206 struct sctp_assoc_value params;
7207 struct sctp_association *asoc;
7208 int retval = -EFAULT;
7209
7210 if (len < sizeof(params)) {
7211 retval = -EINVAL;
7212 goto out;
7213 }
7214
7215 len = sizeof(params);
7216 if (copy_from_user(&params, optval, len))
7217 goto out;
7218
7219 asoc = sctp_id2assoc(sk, params.assoc_id);
7220 if (asoc) {
7221 params.assoc_value = asoc->reconf_enable;
7222 } else if (!params.assoc_id) {
7223 struct sctp_sock *sp = sctp_sk(sk);
7224
7225 params.assoc_value = sp->ep->reconf_enable;
7226 } else {
7227 retval = -EINVAL;
7228 goto out;
7229 }
7230
7231 if (put_user(len, optlen))
7232 goto out;
7233
7234 if (copy_to_user(optval, &params, len))
7235 goto out;
7236
7237 retval = 0;
7238
7239out:
7240 return retval;
7241}
7242
Xin Long9fb657a2017-01-18 00:44:46 +08007243static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7244 char __user *optval,
7245 int __user *optlen)
7246{
7247 struct sctp_assoc_value params;
7248 struct sctp_association *asoc;
7249 int retval = -EFAULT;
7250
7251 if (len < sizeof(params)) {
7252 retval = -EINVAL;
7253 goto out;
7254 }
7255
7256 len = sizeof(params);
7257 if (copy_from_user(&params, optval, len))
7258 goto out;
7259
7260 asoc = sctp_id2assoc(sk, params.assoc_id);
7261 if (asoc) {
7262 params.assoc_value = asoc->strreset_enable;
7263 } else if (!params.assoc_id) {
7264 struct sctp_sock *sp = sctp_sk(sk);
7265
7266 params.assoc_value = sp->ep->strreset_enable;
7267 } else {
7268 retval = -EINVAL;
7269 goto out;
7270 }
7271
7272 if (put_user(len, optlen))
7273 goto out;
7274
7275 if (copy_to_user(optval, &params, len))
7276 goto out;
7277
7278 retval = 0;
7279
7280out:
7281 return retval;
7282}
7283
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007284static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7285 char __user *optval,
7286 int __user *optlen)
7287{
7288 struct sctp_assoc_value params;
7289 struct sctp_association *asoc;
7290 int retval = -EFAULT;
7291
7292 if (len < sizeof(params)) {
7293 retval = -EINVAL;
7294 goto out;
7295 }
7296
7297 len = sizeof(params);
7298 if (copy_from_user(&params, optval, len))
7299 goto out;
7300
7301 asoc = sctp_id2assoc(sk, params.assoc_id);
7302 if (!asoc) {
7303 retval = -EINVAL;
7304 goto out;
7305 }
7306
7307 params.assoc_value = sctp_sched_get_sched(asoc);
7308
7309 if (put_user(len, optlen))
7310 goto out;
7311
7312 if (copy_to_user(optval, &params, len))
7313 goto out;
7314
7315 retval = 0;
7316
7317out:
7318 return retval;
7319}
7320
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007321static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7322 char __user *optval,
7323 int __user *optlen)
7324{
7325 struct sctp_stream_value params;
7326 struct sctp_association *asoc;
7327 int retval = -EFAULT;
7328
7329 if (len < sizeof(params)) {
7330 retval = -EINVAL;
7331 goto out;
7332 }
7333
7334 len = sizeof(params);
7335 if (copy_from_user(&params, optval, len))
7336 goto out;
7337
7338 asoc = sctp_id2assoc(sk, params.assoc_id);
7339 if (!asoc) {
7340 retval = -EINVAL;
7341 goto out;
7342 }
7343
7344 retval = sctp_sched_get_value(asoc, params.stream_id,
7345 &params.stream_value);
7346 if (retval)
7347 goto out;
7348
7349 if (put_user(len, optlen)) {
7350 retval = -EFAULT;
7351 goto out;
7352 }
7353
7354 if (copy_to_user(optval, &params, len)) {
7355 retval = -EFAULT;
7356 goto out;
7357 }
7358
7359out:
7360 return retval;
7361}
7362
Xin Long772a5862017-12-08 21:03:58 +08007363static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7364 char __user *optval,
7365 int __user *optlen)
7366{
7367 struct sctp_assoc_value params;
7368 struct sctp_association *asoc;
7369 int retval = -EFAULT;
7370
7371 if (len < sizeof(params)) {
7372 retval = -EINVAL;
7373 goto out;
7374 }
7375
7376 len = sizeof(params);
7377 if (copy_from_user(&params, optval, len))
7378 goto out;
7379
7380 asoc = sctp_id2assoc(sk, params.assoc_id);
7381 if (asoc) {
7382 params.assoc_value = asoc->intl_enable;
7383 } else if (!params.assoc_id) {
7384 struct sctp_sock *sp = sctp_sk(sk);
7385
7386 params.assoc_value = sp->strm_interleave;
7387 } else {
7388 retval = -EINVAL;
7389 goto out;
7390 }
7391
7392 if (put_user(len, optlen))
7393 goto out;
7394
7395 if (copy_to_user(optval, &params, len))
7396 goto out;
7397
7398 retval = 0;
7399
7400out:
7401 return retval;
7402}
7403
Xin Longb0e9a2f2018-06-28 15:31:00 +08007404static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
7405 char __user *optval,
7406 int __user *optlen)
7407{
7408 int val;
7409
7410 if (len < sizeof(int))
7411 return -EINVAL;
7412
7413 len = sizeof(int);
7414 val = sctp_sk(sk)->reuse;
7415 if (put_user(len, optlen))
7416 return -EFAULT;
7417
7418 if (copy_to_user(optval, &val, len))
7419 return -EFAULT;
7420
7421 return 0;
7422}
7423
Daniel Borkmanndda91922013-06-17 11:40:05 +02007424static int sctp_getsockopt(struct sock *sk, int level, int optname,
7425 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426{
7427 int retval = 0;
7428 int len;
7429
Daniel Borkmannbb333812013-06-28 19:49:40 +02007430 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431
7432 /* I can hardly begin to describe how wrong this is. This is
7433 * so broken as to be worse than useless. The API draft
7434 * REALLY is NOT helpful here... I am not convinced that the
7435 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7436 * are at all well-founded.
7437 */
7438 if (level != SOL_SCTP) {
7439 struct sctp_af *af = sctp_sk(sk)->pf->af;
7440
7441 retval = af->getsockopt(sk, level, optname, optval, optlen);
7442 return retval;
7443 }
7444
7445 if (get_user(len, optlen))
7446 return -EFAULT;
7447
Jiri Slabya4b8e712016-10-21 14:13:24 +02007448 if (len < 0)
7449 return -EINVAL;
7450
wangweidong048ed4b2014-01-21 15:44:11 +08007451 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452
7453 switch (optname) {
7454 case SCTP_STATUS:
7455 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7456 break;
7457 case SCTP_DISABLE_FRAGMENTS:
7458 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7459 optlen);
7460 break;
7461 case SCTP_EVENTS:
7462 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7463 break;
7464 case SCTP_AUTOCLOSE:
7465 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7466 break;
7467 case SCTP_SOCKOPT_PEELOFF:
7468 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7469 break;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04007470 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7471 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7472 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 case SCTP_PEER_ADDR_PARAMS:
7474 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7475 optlen);
7476 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00007477 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07007478 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08007479 optlen);
7480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481 case SCTP_INITMSG:
7482 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7483 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 case SCTP_GET_PEER_ADDRS:
7485 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7486 optlen);
7487 break;
7488 case SCTP_GET_LOCAL_ADDRS:
7489 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7490 optlen);
7491 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04007492 case SCTP_SOCKOPT_CONNECTX3:
7493 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7494 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495 case SCTP_DEFAULT_SEND_PARAM:
7496 retval = sctp_getsockopt_default_send_param(sk, len,
7497 optval, optlen);
7498 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02007499 case SCTP_DEFAULT_SNDINFO:
7500 retval = sctp_getsockopt_default_sndinfo(sk, len,
7501 optval, optlen);
7502 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 case SCTP_PRIMARY_ADDR:
7504 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7505 break;
7506 case SCTP_NODELAY:
7507 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7508 break;
7509 case SCTP_RTOINFO:
7510 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7511 break;
7512 case SCTP_ASSOCINFO:
7513 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7514 break;
7515 case SCTP_I_WANT_MAPPED_V4_ADDR:
7516 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7517 break;
7518 case SCTP_MAXSEG:
7519 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7520 break;
7521 case SCTP_GET_PEER_ADDR_INFO:
7522 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7523 optlen);
7524 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08007525 case SCTP_ADAPTATION_LAYER:
7526 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527 optlen);
7528 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08007529 case SCTP_CONTEXT:
7530 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7531 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007532 case SCTP_FRAGMENT_INTERLEAVE:
7533 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7534 optlen);
7535 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07007536 case SCTP_PARTIAL_DELIVERY_POINT:
7537 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7538 optlen);
7539 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07007540 case SCTP_MAX_BURST:
7541 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7542 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007543 case SCTP_AUTH_KEY:
7544 case SCTP_AUTH_CHUNK:
7545 case SCTP_AUTH_DELETE_KEY:
Xin Long601590e2018-03-14 19:05:32 +08007546 case SCTP_AUTH_DEACTIVATE_KEY:
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007547 retval = -EOPNOTSUPP;
7548 break;
7549 case SCTP_HMAC_IDENT:
7550 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7551 break;
7552 case SCTP_AUTH_ACTIVE_KEY:
7553 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7554 break;
7555 case SCTP_PEER_AUTH_CHUNKS:
7556 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7557 optlen);
7558 break;
7559 case SCTP_LOCAL_AUTH_CHUNKS:
7560 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7561 optlen);
7562 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08007563 case SCTP_GET_ASSOC_NUMBER:
7564 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7565 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00007566 case SCTP_GET_ASSOC_ID_LIST:
7567 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7568 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09007569 case SCTP_AUTO_ASCONF:
7570 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7571 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00007572 case SCTP_PEER_ADDR_THLDS:
7573 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7574 break;
Michele Baldessari196d6752012-12-01 04:49:42 +00007575 case SCTP_GET_ASSOC_STATS:
7576 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7577 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02007578 case SCTP_RECVRCVINFO:
7579 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7580 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007581 case SCTP_RECVNXTINFO:
7582 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7583 break;
Xin Long28aa4c22016-07-09 19:47:40 +08007584 case SCTP_PR_SUPPORTED:
7585 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7586 break;
Xin Longf959fb42016-07-09 19:47:41 +08007587 case SCTP_DEFAULT_PRINFO:
7588 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7589 optlen);
7590 break;
Xin Long826d2532016-07-09 19:47:42 +08007591 case SCTP_PR_ASSOC_STATUS:
7592 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7593 optlen);
7594 break;
Xin Longd229d482017-04-01 17:07:46 +08007595 case SCTP_PR_STREAM_STATUS:
7596 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7597 optlen);
7598 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08007599 case SCTP_RECONFIG_SUPPORTED:
7600 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7601 optlen);
7602 break;
Xin Long9fb657a2017-01-18 00:44:46 +08007603 case SCTP_ENABLE_STREAM_RESET:
7604 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7605 optlen);
7606 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007607 case SCTP_STREAM_SCHEDULER:
7608 retval = sctp_getsockopt_scheduler(sk, len, optval,
7609 optlen);
7610 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007611 case SCTP_STREAM_SCHEDULER_VALUE:
7612 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7613 optlen);
7614 break;
Xin Long772a5862017-12-08 21:03:58 +08007615 case SCTP_INTERLEAVING_SUPPORTED:
7616 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7617 optlen);
7618 break;
Xin Longb0e9a2f2018-06-28 15:31:00 +08007619 case SCTP_REUSE_PORT:
7620 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
7621 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622 default:
7623 retval = -ENOPROTOOPT;
7624 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626
wangweidong048ed4b2014-01-21 15:44:11 +08007627 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628 return retval;
7629}
7630
Craig Gallek086c6532016-02-10 11:50:35 -05007631static int sctp_hash(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632{
7633 /* STUB */
Craig Gallek086c6532016-02-10 11:50:35 -05007634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635}
7636
7637static void sctp_unhash(struct sock *sk)
7638{
7639 /* STUB */
7640}
7641
7642/* Check if port is acceptable. Possibly find first available port.
7643 *
7644 * The port hash table (contained in the 'global' SCTP protocol storage
7645 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7646 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7647 * list (the list number is the port number hashed out, so as you
7648 * would expect from a hash function, all the ports in a given list have
7649 * such a number that hashes out to the same list number; you were
7650 * expecting that, right?); so each list has a set of ports, with a
7651 * link to the socket (struct sock) that uses it, the port number and
7652 * a fastreuse flag (FIXME: NPI ipg).
7653 */
7654static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007655 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656
7657static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
7658{
Xin Longb0e9a2f2018-06-28 15:31:00 +08007659 bool reuse = (sk->sk_reuse || sctp_sk(sk)->reuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660 struct sctp_bind_hashbucket *head; /* hash list */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007661 struct sctp_bind_bucket *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007662 unsigned short snum;
7663 int ret;
7664
Al Viro04afd8b2006-11-20 17:02:01 -08007665 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666
Daniel Borkmannbb333812013-06-28 19:49:40 +02007667 pr_debug("%s: begins, snum:%d\n", __func__, snum);
7668
wangweidong79b91132014-01-21 15:44:07 +08007669 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670
7671 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07007672 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007673 int low, high, remaining, index;
7674 unsigned int rover;
WANG Cong122ff242014-05-12 16:04:53 -07007675 struct net *net = sock_net(sk);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007676
WANG Cong122ff242014-05-12 16:04:53 -07007677 inet_get_local_port_range(net, &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007678 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05007679 rover = prandom_u32() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681 do {
7682 rover++;
7683 if ((rover < low) || (rover > high))
7684 rover = low;
WANG Cong122ff242014-05-12 16:04:53 -07007685 if (inet_is_local_reserved_port(net, rover))
Amerigo Wange3826f12010-05-05 00:27:06 +00007686 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007687 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688 head = &sctp_port_hashtable[index];
wangweidong3c8e43b2014-01-21 15:44:08 +08007689 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007690 sctp_for_each_hentry(pp, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007691 if ((pp->port == rover) &&
7692 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693 goto next;
7694 break;
7695 next:
wangweidong3c8e43b2014-01-21 15:44:08 +08007696 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698
7699 /* Exhausted local port range during search? */
7700 ret = 1;
7701 if (remaining <= 0)
7702 goto fail;
7703
7704 /* OK, here is the one we will use. HEAD (the port
7705 * hash table list entry) is non-NULL and we hold it's
7706 * mutex.
7707 */
7708 snum = rover;
7709 } else {
7710 /* We are given an specific port number; we verify
7711 * that it is not being used. If it is used, we will
7712 * exahust the search in the hash list corresponding
7713 * to the port number (snum) - we detect that with the
7714 * port iterator, pp being NULL.
7715 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007716 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
wangweidong3c8e43b2014-01-21 15:44:08 +08007717 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007718 sctp_for_each_hentry(pp, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007719 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720 goto pp_found;
7721 }
7722 }
7723 pp = NULL;
7724 goto pp_not_found;
7725pp_found:
7726 if (!hlist_empty(&pp->owner)) {
7727 /* We had a port hash table hit - there is an
7728 * available port (pp != NULL) and it is being
7729 * used by other socket (pp->owner not empty); that other
7730 * socket is going to be sk2.
7731 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007732 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733
Daniel Borkmannbb333812013-06-28 19:49:40 +02007734 pr_debug("%s: found a possible match\n", __func__);
7735
Xin Longb0e9a2f2018-06-28 15:31:00 +08007736 if (pp->fastreuse && reuse && sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007737 goto success;
7738
7739 /* Run through the list of sockets bound to the port
7740 * (pp->port) [via the pointers bind_next and
7741 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
7742 * we get the endpoint they describe and run through
7743 * the endpoint's list of IP (v4 or v6) addresses,
7744 * comparing each of the addresses with the address of
7745 * the socket sk. If we find a match, then that means
7746 * that this port/socket (sk) combination are already
7747 * in an endpoint.
7748 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007749 sk_for_each_bound(sk2, &pp->owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750 struct sctp_endpoint *ep2;
7751 ep2 = sctp_sk(sk2)->ep;
7752
Vlad Yasevich4e540642008-07-18 23:06:32 -07007753 if (sk == sk2 ||
Xin Longb0e9a2f2018-06-28 15:31:00 +08007754 (reuse && (sk2->sk_reuse || sctp_sk(sk2)->reuse) &&
Vlad Yasevich4e540642008-07-18 23:06:32 -07007755 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007756 continue;
7757
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07007758 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
7759 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760 ret = (long)sk2;
7761 goto fail_unlock;
7762 }
7763 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02007764
7765 pr_debug("%s: found a match\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766 }
7767pp_not_found:
7768 /* If there was a hash table miss, create a new port. */
7769 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007770 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771 goto fail_unlock;
7772
7773 /* In either case (hit or miss), make sure fastreuse is 1 only
7774 * if sk->sk_reuse is too (that is, if the caller requested
7775 * SO_REUSEADDR on this socket -sk-).
7776 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007777 if (hlist_empty(&pp->owner)) {
Xin Longb0e9a2f2018-06-28 15:31:00 +08007778 if (reuse && sk->sk_state != SCTP_SS_LISTENING)
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007779 pp->fastreuse = 1;
7780 else
7781 pp->fastreuse = 0;
7782 } else if (pp->fastreuse &&
Xin Longb0e9a2f2018-06-28 15:31:00 +08007783 (!reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784 pp->fastreuse = 0;
7785
7786 /* We are set, so fill up all the data in the hash table
7787 * entry, tie the socket list information with the rest of the
7788 * sockets FIXME: Blurry, NPI (ipg).
7789 */
7790success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007792 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793 sk_add_bind_node(sk, &pp->owner);
7794 sctp_sk(sk)->bind_hash = pp;
7795 }
7796 ret = 0;
7797
7798fail_unlock:
wangweidong3c8e43b2014-01-21 15:44:08 +08007799 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800
7801fail:
wangweidong79b91132014-01-21 15:44:07 +08007802 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803 return ret;
7804}
7805
7806/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
7807 * port is requested.
7808 */
7809static int sctp_get_port(struct sock *sk, unsigned short snum)
7810{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811 union sctp_addr addr;
7812 struct sctp_af *af = sctp_sk(sk)->pf->af;
7813
7814 /* Set up a dummy address struct from the sk. */
7815 af->from_sk(&addr, sk);
7816 addr.v4.sin_port = htons(snum);
7817
7818 /* Note: sk->sk_num gets filled in if ephemeral port request. */
Daniel Borkmann62208f12013-06-25 18:17:30 +02007819 return !!sctp_get_port_local(sk, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820}
7821
7822/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823 * Move a socket to LISTENING state.
7824 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02007825static int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007827 struct sctp_sock *sp = sctp_sk(sk);
7828 struct sctp_endpoint *ep = sp->ep;
Herbert Xu5821c762016-01-24 21:20:12 +08007829 struct crypto_shash *tfm = NULL;
Neil Horman3c681982012-10-24 09:20:03 +00007830 char alg[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007831
7832 /* Allocate HMAC for generating cookie. */
Neil Horman3c681982012-10-24 09:20:03 +00007833 if (!sp->hmac && sp->sctp_hmac_alg) {
7834 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
Herbert Xu5821c762016-01-24 21:20:12 +08007835 tfm = crypto_alloc_shash(alg, 0, 0);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07007836 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00007837 net_info_ratelimited("failed to load transform for %s: %ld\n",
Neil Horman3c681982012-10-24 09:20:03 +00007838 sp->sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007839 return -ENOSYS;
7840 }
7841 sctp_sk(sk)->hmac = tfm;
7842 }
7843
7844 /*
7845 * If a bind() or sctp_bindx() is not called prior to a listen()
7846 * call that allows new associations to be accepted, the system
7847 * picks an ephemeral port and will choose an address set equivalent
7848 * to binding with a wildcard address.
7849 *
7850 * This is not currently spelled out in the SCTP sockets
7851 * extensions draft, but follows the practice as seen in TCP
7852 * sockets.
7853 *
7854 */
Yafang Shaocbabf462017-12-20 11:12:54 +08007855 inet_sk_set_state(sk, SCTP_SS_LISTENING);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007856 if (!ep->base.bind_addr.port) {
7857 if (sctp_autobind(sk))
7858 return -EAGAIN;
7859 } else {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007860 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08007861 inet_sk_set_state(sk, SCTP_SS_CLOSED);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007862 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 }
7864 }
7865
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007866 sk->sk_max_ack_backlog = backlog;
7867 sctp_hash_endpoint(ep);
7868 return 0;
7869}
7870
7871/*
7872 * 4.1.3 / 5.1.3 listen()
7873 *
7874 * By default, new associations are not accepted for UDP style sockets.
7875 * An application uses listen() to mark a socket as being able to
7876 * accept new associations.
7877 *
7878 * On TCP style sockets, applications use listen() to ready the SCTP
7879 * endpoint for accepting inbound associations.
7880 *
7881 * On both types of endpoints a backlog of '0' disables listening.
7882 *
7883 * Move a socket to LISTENING state.
7884 */
7885int sctp_inet_listen(struct socket *sock, int backlog)
7886{
7887 struct sock *sk = sock->sk;
7888 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7889 int err = -EINVAL;
7890
7891 if (unlikely(backlog < 0))
7892 return err;
7893
wangweidong048ed4b2014-01-21 15:44:11 +08007894 lock_sock(sk);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007895
7896 /* Peeled-off sockets are not allowed to listen(). */
7897 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
7898 goto out;
7899
7900 if (sock->state != SS_UNCONNECTED)
7901 goto out;
7902
Xin Long34b27892017-04-06 13:10:52 +08007903 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
7904 goto out;
7905
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007906 /* If backlog is zero, disable listening. */
7907 if (!backlog) {
7908 if (sctp_sstate(sk, CLOSED))
7909 goto out;
7910
7911 err = 0;
7912 sctp_unhash_endpoint(ep);
7913 sk->sk_state = SCTP_SS_CLOSED;
Xin Longb0e9a2f2018-06-28 15:31:00 +08007914 if (sk->sk_reuse || sctp_sk(sk)->reuse)
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007915 sctp_sk(sk)->bind_hash->fastreuse = 1;
7916 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007917 }
7918
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007919 /* If we are already listening, just update the backlog */
7920 if (sctp_sstate(sk, LISTENING))
7921 sk->sk_max_ack_backlog = backlog;
7922 else {
7923 err = sctp_listen_start(sk, backlog);
7924 if (err)
7925 goto out;
7926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007928 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929out:
wangweidong048ed4b2014-01-21 15:44:11 +08007930 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932}
7933
7934/*
7935 * This function is done by modeling the current datagram_poll() and the
7936 * tcp_poll(). Note that, based on these implementations, we don't
7937 * lock the socket in this function, even though it seems that,
7938 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08007939 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940 * in this place. We assume that we don't need locks either until proven
7941 * otherwise.
7942 *
7943 * Another thing to note is that we include the Async I/O support
7944 * here, again, by modeling the current TCP/UDP code. We don't have
7945 * a good way to test with it yet.
7946 */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07007947__poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948{
7949 struct sock *sk = sock->sk;
7950 struct sctp_sock *sp = sctp_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -04007951 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07007953 poll_wait(file, sk_sleep(sk), wait);
7954
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03007955 sock_rps_record_flow(sk);
7956
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957 /* A TCP-style listening socket becomes readable when the accept queue
7958 * is not empty.
7959 */
7960 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
7961 return (!list_empty(&sp->ep->asocs)) ?
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007962 (EPOLLIN | EPOLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963
7964 mask = 0;
7965
7966 /* Is there any exceptional events? */
7967 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007968 mask |= EPOLLERR |
7969 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
Davide Libenzif348d702006-03-25 03:07:39 -08007970 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007971 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007973 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007974
7975 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00007976 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007977 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007978
7979 /* The association is either gone or not ready. */
7980 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
7981 return mask;
7982
7983 /* Is it writable? */
7984 if (sctp_writeable(sk)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007985 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986 } else {
Eric Dumazet9cd3e072015-11-29 20:03:10 -08007987 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988 /*
7989 * Since the socket is not locked, the buffer
7990 * might be made available after the writeable check and
7991 * before the bit is set. This could cause a lost I/O
7992 * signal. tcp_poll() has a race breaker for this race
7993 * condition. Based on their implementation, we put
7994 * in the following code to cover it as well.
7995 */
7996 if (sctp_writeable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007997 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998 }
7999 return mask;
8000}
8001
8002/********************************************************************
8003 * 2nd Level Abstractions
8004 ********************************************************************/
8005
8006static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008007 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008008{
8009 struct sctp_bind_bucket *pp;
8010
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08008011 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07008013 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008014 pp->port = snum;
8015 pp->fastreuse = 0;
8016 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008017 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05008018 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008019 }
8020 return pp;
8021}
8022
8023/* Caller must hold hashbucket lock for this tb with local BH disabled */
8024static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
8025{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07008026 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05008027 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028 kmem_cache_free(sctp_bucket_cachep, pp);
8029 SCTP_DBG_OBJCNT_DEC(bind_bucket);
8030 }
8031}
8032
8033/* Release this socket's reference to a local port. */
8034static inline void __sctp_put_port(struct sock *sk)
8035{
8036 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008037 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
8038 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07008039 struct sctp_bind_bucket *pp;
8040
wangweidong3c8e43b2014-01-21 15:44:08 +08008041 spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008042 pp = sctp_sk(sk)->bind_hash;
8043 __sk_del_bind_node(sk);
8044 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008045 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008046 sctp_bucket_destroy(pp);
wangweidong3c8e43b2014-01-21 15:44:08 +08008047 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008048}
8049
8050void sctp_put_port(struct sock *sk)
8051{
wangweidong79b91132014-01-21 15:44:07 +08008052 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008053 __sctp_put_port(sk);
wangweidong79b91132014-01-21 15:44:07 +08008054 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055}
8056
8057/*
8058 * The system picks an ephemeral port and choose an address set equivalent
8059 * to binding with a wildcard address.
8060 * One of those addresses will be the primary address for the association.
8061 * This automatically enables the multihoming capability of SCTP.
8062 */
8063static int sctp_autobind(struct sock *sk)
8064{
8065 union sctp_addr autoaddr;
8066 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08008067 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068
8069 /* Initialize a local sockaddr structure to INADDR_ANY. */
8070 af = sctp_sk(sk)->pf->af;
8071
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008072 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073 af->inaddr_any(&autoaddr, port);
8074
8075 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
8076}
8077
8078/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
8079 *
8080 * From RFC 2292
8081 * 4.2 The cmsghdr Structure *
8082 *
8083 * When ancillary data is sent or received, any number of ancillary data
8084 * objects can be specified by the msg_control and msg_controllen members of
8085 * the msghdr structure, because each object is preceded by
8086 * a cmsghdr structure defining the object's length (the cmsg_len member).
8087 * Historically Berkeley-derived implementations have passed only one object
8088 * at a time, but this API allows multiple objects to be
8089 * passed in a single call to sendmsg() or recvmsg(). The following example
8090 * shows two ancillary data objects in a control buffer.
8091 *
8092 * |<--------------------------- msg_controllen -------------------------->|
8093 * | |
8094 *
8095 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
8096 *
8097 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
8098 * | | |
8099 *
8100 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
8101 *
8102 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
8103 * | | | | |
8104 *
8105 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8106 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
8107 *
8108 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
8109 *
8110 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8111 * ^
8112 * |
8113 *
8114 * msg_control
8115 * points here
8116 */
Xin Longa05437a2017-08-11 10:23:48 +08008117static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118{
Vlad Yasevichab38fb02008-04-12 18:40:06 -07008119 struct msghdr *my_msg = (struct msghdr *)msg;
Xin Longa05437a2017-08-11 10:23:48 +08008120 struct cmsghdr *cmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008121
Gu Zhengf95b4142014-12-11 11:22:04 +08008122 for_each_cmsghdr(cmsg, my_msg) {
Vlad Yasevichab38fb02008-04-12 18:40:06 -07008123 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124 return -EINVAL;
8125
8126 /* Should we parse this header or ignore? */
8127 if (cmsg->cmsg_level != IPPROTO_SCTP)
8128 continue;
8129
8130 /* Strictly check lengths following example in SCM code. */
8131 switch (cmsg->cmsg_type) {
8132 case SCTP_INIT:
8133 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008134 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008135 *
8136 * This cmsghdr structure provides information for
8137 * initializing new SCTP associations with sendmsg().
8138 * The SCTP_INITMSG socket option uses this same data
8139 * structure. This structure is not used for
8140 * recvmsg().
8141 *
8142 * cmsg_level cmsg_type cmsg_data[]
8143 * ------------ ------------ ----------------------
8144 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
8145 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008146 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008147 return -EINVAL;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008148
8149 cmsgs->init = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150 break;
8151
8152 case SCTP_SNDRCV:
8153 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008154 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155 *
8156 * This cmsghdr structure specifies SCTP options for
8157 * sendmsg() and describes SCTP header information
8158 * about a received message through recvmsg().
8159 *
8160 * cmsg_level cmsg_type cmsg_data[]
8161 * ------------ ------------ ----------------------
8162 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
8163 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008164 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165 return -EINVAL;
8166
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008167 cmsgs->srinfo = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008169 if (cmsgs->srinfo->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07008170 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Long49102802018-03-05 20:44:20 +08008171 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8172 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008173 return -EINVAL;
8174 break;
8175
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008176 case SCTP_SNDINFO:
8177 /* SCTP Socket API Extension
8178 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
8179 *
8180 * This cmsghdr structure specifies SCTP options for
8181 * sendmsg(). This structure and SCTP_RCVINFO replaces
8182 * SCTP_SNDRCV which has been deprecated.
8183 *
8184 * cmsg_level cmsg_type cmsg_data[]
8185 * ------------ ------------ ---------------------
8186 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
8187 */
8188 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
8189 return -EINVAL;
8190
8191 cmsgs->sinfo = CMSG_DATA(cmsg);
8192
8193 if (cmsgs->sinfo->snd_flags &
8194 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Long49102802018-03-05 20:44:20 +08008195 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8196 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008197 return -EINVAL;
8198 break;
Xin Longed63afb2018-03-05 20:44:18 +08008199 case SCTP_PRINFO:
8200 /* SCTP Socket API Extension
8201 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
8202 *
8203 * This cmsghdr structure specifies SCTP options for sendmsg().
8204 *
8205 * cmsg_level cmsg_type cmsg_data[]
8206 * ------------ ------------ ---------------------
8207 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
8208 */
8209 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
8210 return -EINVAL;
8211
8212 cmsgs->prinfo = CMSG_DATA(cmsg);
8213 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
8214 return -EINVAL;
8215
8216 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
8217 cmsgs->prinfo->pr_value = 0;
8218 break;
Xin Long3ff547c2018-03-14 19:05:31 +08008219 case SCTP_AUTHINFO:
8220 /* SCTP Socket API Extension
8221 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
8222 *
8223 * This cmsghdr structure specifies SCTP options for sendmsg().
8224 *
8225 * cmsg_level cmsg_type cmsg_data[]
8226 * ------------ ------------ ---------------------
8227 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
8228 */
8229 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
8230 return -EINVAL;
8231
8232 cmsgs->authinfo = CMSG_DATA(cmsg);
8233 break;
Xin Long2c0dbaa2018-03-05 20:44:19 +08008234 case SCTP_DSTADDRV4:
8235 case SCTP_DSTADDRV6:
8236 /* SCTP Socket API Extension
8237 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
8238 *
8239 * This cmsghdr structure specifies SCTP options for sendmsg().
8240 *
8241 * cmsg_level cmsg_type cmsg_data[]
8242 * ------------ ------------ ---------------------
8243 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
8244 * ------------ ------------ ---------------------
8245 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
8246 */
8247 cmsgs->addrs_msg = my_msg;
8248 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249 default:
8250 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07008251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008252 }
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02008253
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254 return 0;
8255}
8256
8257/*
8258 * Wait for a packet..
8259 * Note: This function is the same function as in core/datagram.c
8260 * with a few modifications to make lksctp work.
8261 */
wangweidong26ac8e52013-12-23 12:16:51 +08008262static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263{
8264 int error;
8265 DEFINE_WAIT(wait);
8266
Eric Dumazetaa395142010-04-20 13:03:51 +00008267 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008268
8269 /* Socket errors? */
8270 error = sock_error(sk);
8271 if (error)
8272 goto out;
8273
8274 if (!skb_queue_empty(&sk->sk_receive_queue))
8275 goto ready;
8276
8277 /* Socket shut down? */
8278 if (sk->sk_shutdown & RCV_SHUTDOWN)
8279 goto out;
8280
8281 /* Sequenced packets can come disconnected. If so we report the
8282 * problem.
8283 */
8284 error = -ENOTCONN;
8285
8286 /* Is there a good reason to think that we may receive some data? */
8287 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8288 goto out;
8289
8290 /* Handle signals. */
8291 if (signal_pending(current))
8292 goto interrupted;
8293
8294 /* Let another process have a go. Since we are going to sleep
8295 * anyway. Note: This may cause odd behaviors if the message
8296 * does not fit in the user's buffer, but this seems to be the
8297 * only way to honor MSG_DONTWAIT realistically.
8298 */
wangweidong048ed4b2014-01-21 15:44:11 +08008299 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008300 *timeo_p = schedule_timeout(*timeo_p);
wangweidong048ed4b2014-01-21 15:44:11 +08008301 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008302
8303ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00008304 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305 return 0;
8306
8307interrupted:
8308 error = sock_intr_errno(*timeo_p);
8309
8310out:
Eric Dumazetaa395142010-04-20 13:03:51 +00008311 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008312 *err = error;
8313 return error;
8314}
8315
8316/* Receive a datagram.
8317 * Note: This is pretty much the same routine as in core/datagram.c
8318 * with a few changes to make lksctp work.
8319 */
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02008320struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8321 int noblock, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008322{
8323 int error;
8324 struct sk_buff *skb;
8325 long timeo;
8326
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327 timeo = sock_rcvtimeo(sk, noblock);
8328
Daniel Borkmannbb333812013-06-28 19:49:40 +02008329 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8330 MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008331
8332 do {
8333 /* Again only user level code calls this function,
8334 * so nothing interrupt level
8335 * will suddenly eat the receive_queue.
8336 *
8337 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00008338 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339 */
8340 if (flags & MSG_PEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341 skb = skb_peek(&sk->sk_receive_queue);
8342 if (skb)
Reshetova, Elena63354792017-06-30 13:07:58 +03008343 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008344 } else {
Marcelo Ricardo Leitner311b2172016-04-13 19:12:29 -03008345 skb = __skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008346 }
8347
8348 if (skb)
8349 return skb;
8350
Neil Horman6736dc32005-12-02 20:30:06 -08008351 /* Caller is allowed not to check sk->sk_err before calling. */
8352 error = sock_error(sk);
8353 if (error)
8354 goto no_packet;
8355
Linus Torvalds1da177e2005-04-16 15:20:36 -07008356 if (sk->sk_shutdown & RCV_SHUTDOWN)
8357 break;
8358
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07008359 if (sk_can_busy_loop(sk)) {
8360 sk_busy_loop(sk, noblock);
8361
8362 if (!skb_queue_empty(&sk->sk_receive_queue))
8363 continue;
8364 }
Neil Horman8465a5f2014-04-17 15:26:51 -04008365
Linus Torvalds1da177e2005-04-16 15:20:36 -07008366 /* User doesn't want to wait. */
8367 error = -EAGAIN;
8368 if (!timeo)
8369 goto no_packet;
8370 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8371
8372 return NULL;
8373
8374no_packet:
8375 *err = error;
8376 return NULL;
8377}
8378
8379/* If sndbuf has changed, wake up per association sndbuf waiters. */
8380static void __sctp_write_space(struct sctp_association *asoc)
8381{
8382 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008383
Eric Dumazetceb5d582015-11-29 20:03:11 -08008384 if (sctp_wspace(asoc) <= 0)
8385 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386
Eric Dumazetceb5d582015-11-29 20:03:11 -08008387 if (waitqueue_active(&asoc->wait))
8388 wake_up_interruptible(&asoc->wait);
Eric Dumazeteaefd112011-02-18 03:26:36 +00008389
Eric Dumazetceb5d582015-11-29 20:03:11 -08008390 if (sctp_writeable(sk)) {
8391 struct socket_wq *wq;
8392
8393 rcu_read_lock();
8394 wq = rcu_dereference(sk->sk_wq);
8395 if (wq) {
8396 if (waitqueue_active(&wq->wait))
8397 wake_up_interruptible(&wq->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008398
8399 /* Note that we try to include the Async I/O support
8400 * here by modeling from the current TCP/UDP code.
8401 * We have not tested with it yet.
8402 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00008403 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Eric Dumazetceb5d582015-11-29 20:03:11 -08008404 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08008406 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407 }
8408}
8409
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008410static void sctp_wake_up_waiters(struct sock *sk,
8411 struct sctp_association *asoc)
8412{
8413 struct sctp_association *tmp = asoc;
8414
8415 /* We do accounting for the sndbuf space per association,
8416 * so we only need to wake our own association.
8417 */
8418 if (asoc->ep->sndbuf_policy)
8419 return __sctp_write_space(asoc);
8420
Daniel Borkmann1e1cdf82014-04-09 16:10:20 +02008421 /* If association goes down and is just flushing its
8422 * outq, then just normally notify others.
8423 */
8424 if (asoc->base.dead)
8425 return sctp_write_space(sk);
8426
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008427 /* Accounting for the sndbuf space is per socket, so we
8428 * need to wake up others, try to be fair and in case of
8429 * other associations, let them have a go first instead
8430 * of just doing a sctp_write_space() call.
8431 *
8432 * Note that we reach sctp_wake_up_waiters() only when
8433 * associations free up queued chunks, thus we are under
8434 * lock and the list of associations on a socket is
8435 * guaranteed not to change.
8436 */
8437 for (tmp = list_next_entry(tmp, asocs); 1;
8438 tmp = list_next_entry(tmp, asocs)) {
8439 /* Manually skip the head element. */
8440 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8441 continue;
8442 /* Wake up association. */
8443 __sctp_write_space(tmp);
8444 /* We've reached the end. */
8445 if (tmp == asoc)
8446 break;
8447 }
8448}
8449
Linus Torvalds1da177e2005-04-16 15:20:36 -07008450/* Do accounting for the sndbuf space.
8451 * Decrement the used sndbuf space of the corresponding association by the
8452 * data size which was just transmitted(freed).
8453 */
8454static void sctp_wfree(struct sk_buff *skb)
8455{
Daniel Borkmannf869c912014-11-20 01:54:48 +01008456 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8457 struct sctp_association *asoc = chunk->asoc;
8458 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008460 sk_mem_uncharge(sk, skb->truesize);
Xin Long605c0ac2018-10-17 03:07:50 +08008461 sk->sk_wmem_queued -= skb->truesize + sizeof(struct sctp_chunk);
8462 asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk);
8463 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk),
8464 &sk->sk_wmem_alloc));
Neil Horman4d93df02007-08-15 16:07:44 -07008465
Xin Longec2e5062018-03-14 19:05:33 +08008466 if (chunk->shkey) {
8467 struct sctp_shared_key *shkey = chunk->shkey;
8468
8469 /* refcnt == 2 and !list_empty mean after this release, it's
8470 * not being used anywhere, and it's time to notify userland
8471 * that this shkey can be freed if it's been deactivated.
8472 */
8473 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
8474 refcount_read(&shkey->refcnt) == 2) {
8475 struct sctp_ulpevent *ev;
8476
8477 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
8478 SCTP_AUTH_FREE_KEY,
8479 GFP_KERNEL);
8480 if (ev)
8481 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
8482 }
Xin Long1b1e0bc2018-03-14 19:05:30 +08008483 sctp_auth_shkey_release(chunk->shkey);
Xin Longec2e5062018-03-14 19:05:33 +08008484 }
Xin Long1b1e0bc2018-03-14 19:05:30 +08008485
Neil Horman4eb701d2005-04-28 12:02:04 -07008486 sock_wfree(skb);
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008487 sctp_wake_up_waiters(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008488
8489 sctp_association_put(asoc);
8490}
8491
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008492/* Do accounting for the receive space on the socket.
8493 * Accounting for the association is done in ulpevent.c
8494 * We set this as a destructor for the cloned data skbs so that
8495 * accounting is done at the correct time.
8496 */
8497void sctp_sock_rfree(struct sk_buff *skb)
8498{
8499 struct sock *sk = skb->sk;
8500 struct sctp_ulpevent *event = sctp_skb2event(skb);
8501
8502 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07008503
8504 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008505 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07008506 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008507 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008508}
8509
8510
Linus Torvalds1da177e2005-04-16 15:20:36 -07008511/* Helper function to wait for space in the sndbuf. */
8512static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +08008513 size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514{
8515 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 long current_timeo = *timeo_p;
8517 DEFINE_WAIT(wait);
Xin Longa0ff6602018-01-15 17:01:36 +08008518 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008519
Daniel Borkmannbb333812013-06-28 19:49:40 +02008520 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8521 *timeo_p, msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008522
8523 /* Increment the association's refcnt. */
8524 sctp_association_hold(asoc);
8525
8526 /* Wait on the association specific sndbuf space. */
8527 for (;;) {
8528 prepare_to_wait_exclusive(&asoc->wait, &wait,
8529 TASK_INTERRUPTIBLE);
Xin Longca3af4d2017-11-15 16:55:54 +08008530 if (asoc->base.dead)
8531 goto do_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008532 if (!*timeo_p)
8533 goto do_nonblock;
Xin Longca3af4d2017-11-15 16:55:54 +08008534 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008535 goto do_error;
8536 if (signal_pending(current))
8537 goto do_interrupted;
8538 if (msg_len <= sctp_wspace(asoc))
8539 break;
8540
8541 /* Let another process have a go. Since we are going
8542 * to sleep anyway.
8543 */
wangweidong048ed4b2014-01-21 15:44:11 +08008544 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008545 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008546 lock_sock(sk);
Xin Longa0ff6602018-01-15 17:01:36 +08008547 if (sk != asoc->base.sk)
8548 goto do_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008549
8550 *timeo_p = current_timeo;
8551 }
8552
8553out:
8554 finish_wait(&asoc->wait, &wait);
8555
8556 /* Release the association's refcnt. */
8557 sctp_association_put(asoc);
8558
8559 return err;
8560
Xin Longca3af4d2017-11-15 16:55:54 +08008561do_dead:
8562 err = -ESRCH;
8563 goto out;
8564
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565do_error:
8566 err = -EPIPE;
8567 goto out;
8568
8569do_interrupted:
8570 err = sock_intr_errno(*timeo_p);
8571 goto out;
8572
8573do_nonblock:
8574 err = -EAGAIN;
8575 goto out;
8576}
8577
David S. Miller676d2362014-04-11 16:15:36 -04008578void sctp_data_ready(struct sock *sk)
Wei Yongjun561b1732010-04-28 08:47:18 +00008579{
David S. Miller7ef52732010-05-02 21:43:40 -07008580 struct socket_wq *wq;
8581
8582 rcu_read_lock();
8583 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08008584 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008585 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8586 EPOLLRDNORM | EPOLLRDBAND);
Wei Yongjun561b1732010-04-28 08:47:18 +00008587 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07008588 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00008589}
8590
Linus Torvalds1da177e2005-04-16 15:20:36 -07008591/* If socket sndbuf has changed, wake up all per association waiters. */
8592void sctp_write_space(struct sock *sk)
8593{
8594 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595
8596 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07008597 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008598 __sctp_write_space(asoc);
8599 }
8600}
8601
8602/* Is there any sndbuf space available on the socket?
8603 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08008604 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07008605 * associations on the same socket. For a UDP-style socket with
8606 * multiple associations, it is possible for it to be "unwriteable"
8607 * prematurely. I assume that this is acceptable because
8608 * a premature "unwriteable" is better than an accidental "writeable" which
8609 * would cause an unwanted block under certain circumstances. For the 1-1
8610 * UDP-style sockets or TCP-style sockets, this code should work.
8611 * - Daisy
8612 */
8613static int sctp_writeable(struct sock *sk)
8614{
8615 int amt = 0;
8616
Eric Dumazet31e6d362009-06-17 19:05:41 -07008617 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008618 if (amt < 0)
8619 amt = 0;
8620 return amt;
8621}
8622
8623/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8624 * returns immediately with EINPROGRESS.
8625 */
8626static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8627{
8628 struct sock *sk = asoc->base.sk;
8629 int err = 0;
8630 long current_timeo = *timeo_p;
8631 DEFINE_WAIT(wait);
8632
Daniel Borkmannbb333812013-06-28 19:49:40 +02008633 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008634
8635 /* Increment the association's refcnt. */
8636 sctp_association_hold(asoc);
8637
8638 for (;;) {
8639 prepare_to_wait_exclusive(&asoc->wait, &wait,
8640 TASK_INTERRUPTIBLE);
8641 if (!*timeo_p)
8642 goto do_nonblock;
8643 if (sk->sk_shutdown & RCV_SHUTDOWN)
8644 break;
8645 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
8646 asoc->base.dead)
8647 goto do_error;
8648 if (signal_pending(current))
8649 goto do_interrupted;
8650
8651 if (sctp_state(asoc, ESTABLISHED))
8652 break;
8653
8654 /* Let another process have a go. Since we are going
8655 * to sleep anyway.
8656 */
wangweidong048ed4b2014-01-21 15:44:11 +08008657 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008658 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008659 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008660
8661 *timeo_p = current_timeo;
8662 }
8663
8664out:
8665 finish_wait(&asoc->wait, &wait);
8666
8667 /* Release the association's refcnt. */
8668 sctp_association_put(asoc);
8669
8670 return err;
8671
8672do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08008673 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008674 err = -ETIMEDOUT;
8675 else
8676 err = -ECONNREFUSED;
8677 goto out;
8678
8679do_interrupted:
8680 err = sock_intr_errno(*timeo_p);
8681 goto out;
8682
8683do_nonblock:
8684 err = -EINPROGRESS;
8685 goto out;
8686}
8687
8688static int sctp_wait_for_accept(struct sock *sk, long timeo)
8689{
8690 struct sctp_endpoint *ep;
8691 int err = 0;
8692 DEFINE_WAIT(wait);
8693
8694 ep = sctp_sk(sk)->ep;
8695
8696
8697 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00008698 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008699 TASK_INTERRUPTIBLE);
8700
8701 if (list_empty(&ep->asocs)) {
wangweidong048ed4b2014-01-21 15:44:11 +08008702 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703 timeo = schedule_timeout(timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008704 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008705 }
8706
8707 err = -EINVAL;
8708 if (!sctp_sstate(sk, LISTENING))
8709 break;
8710
8711 err = 0;
8712 if (!list_empty(&ep->asocs))
8713 break;
8714
8715 err = sock_intr_errno(timeo);
8716 if (signal_pending(current))
8717 break;
8718
8719 err = -EAGAIN;
8720 if (!timeo)
8721 break;
8722 }
8723
Eric Dumazetaa395142010-04-20 13:03:51 +00008724 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008725
8726 return err;
8727}
8728
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02008729static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008730{
8731 DEFINE_WAIT(wait);
8732
8733 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00008734 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008735 if (list_empty(&sctp_sk(sk)->ep->asocs))
8736 break;
wangweidong048ed4b2014-01-21 15:44:11 +08008737 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008738 timeout = schedule_timeout(timeout);
wangweidong048ed4b2014-01-21 15:44:11 +08008739 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 } while (!signal_pending(current) && timeout);
8741
Eric Dumazetaa395142010-04-20 13:03:51 +00008742 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743}
8744
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008745static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
8746{
8747 struct sk_buff *frag;
8748
8749 if (!skb->data_len)
8750 goto done;
8751
8752 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07008753 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008754 sctp_skb_set_owner_r_frag(frag, sk);
8755
8756done:
8757 sctp_skb_set_owner_r(skb, sk);
8758}
8759
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008760void sctp_copy_sock(struct sock *newsk, struct sock *sk,
8761 struct sctp_association *asoc)
8762{
8763 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08008764 struct inet_sock *newinet;
Richard Haines2277c7c2018-02-13 20:56:24 +00008765 struct sctp_sock *sp = sctp_sk(sk);
8766 struct sctp_endpoint *ep = sp->ep;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008767
8768 newsk->sk_type = sk->sk_type;
8769 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
8770 newsk->sk_flags = sk->sk_flags;
Marcelo Ricardo Leitner50a5ffb2015-12-04 15:14:05 -02008771 newsk->sk_tsflags = sk->sk_tsflags;
Tom Herbert28448b82014-05-23 08:47:19 -07008772 newsk->sk_no_check_tx = sk->sk_no_check_tx;
8773 newsk->sk_no_check_rx = sk->sk_no_check_rx;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008774 newsk->sk_reuse = sk->sk_reuse;
Xin Longb0e9a2f2018-06-28 15:31:00 +08008775 sctp_sk(newsk)->reuse = sp->reuse;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008776
8777 newsk->sk_shutdown = sk->sk_shutdown;
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02008778 newsk->sk_destruct = sctp_destruct_sock;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008779 newsk->sk_family = sk->sk_family;
8780 newsk->sk_protocol = IPPROTO_SCTP;
8781 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
8782 newsk->sk_sndbuf = sk->sk_sndbuf;
8783 newsk->sk_rcvbuf = sk->sk_rcvbuf;
8784 newsk->sk_lingertime = sk->sk_lingertime;
8785 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
8786 newsk->sk_sndtimeo = sk->sk_sndtimeo;
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03008787 newsk->sk_rxhash = sk->sk_rxhash;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008788
8789 newinet = inet_sk(newsk);
8790
8791 /* Initialize sk's sport, dport, rcv_saddr and daddr for
8792 * getsockname() and getpeername()
8793 */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008794 newinet->inet_sport = inet->inet_sport;
8795 newinet->inet_saddr = inet->inet_saddr;
8796 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
8797 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008798 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008799 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008800
8801 newinet->uc_ttl = inet->uc_ttl;
8802 newinet->mc_loop = 1;
8803 newinet->mc_ttl = 1;
8804 newinet->mc_index = 0;
8805 newinet->mc_list = NULL;
Marcelo Ricardo Leitner01ce63c2015-12-04 15:14:04 -02008806
8807 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
8808 net_enable_timestamp();
Marcelo Ricardo Leitner3538a5c2015-12-23 16:44:09 -02008809
Richard Haines2277c7c2018-02-13 20:56:24 +00008810 /* Set newsk security attributes from orginal sk and connection
8811 * security attribute from ep.
8812 */
8813 security_sctp_sk_clone(ep, sk, newsk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008814}
8815
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008816static inline void sctp_copy_descendant(struct sock *sk_to,
8817 const struct sock *sk_from)
8818{
8819 int ancestor_size = sizeof(struct inet_sock) +
8820 sizeof(struct sctp_sock) -
8821 offsetof(struct sctp_sock, auto_asconf_list);
8822
8823 if (sk_from->sk_family == PF_INET6)
8824 ancestor_size += sizeof(struct ipv6_pinfo);
8825
8826 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
8827}
8828
Linus Torvalds1da177e2005-04-16 15:20:36 -07008829/* Populate the fields of the newsk from the oldsk and migrate the assoc
8830 * and its messages to the newsk.
8831 */
8832static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
8833 struct sctp_association *assoc,
Xin Longb7ef2612017-08-11 10:23:50 +08008834 enum sctp_socket_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008835{
8836 struct sctp_sock *oldsp = sctp_sk(oldsk);
8837 struct sctp_sock *newsp = sctp_sk(newsk);
8838 struct sctp_bind_bucket *pp; /* hash list port iterator */
8839 struct sctp_endpoint *newep = newsp->ep;
8840 struct sk_buff *skb, *tmp;
8841 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08008842 struct sctp_bind_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008843
8844 /* Migrate socket buffer sizes and all the socket level options to the
8845 * new socket.
8846 */
8847 newsk->sk_sndbuf = oldsk->sk_sndbuf;
8848 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
8849 /* Brute force copy old sctp opt. */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008850 sctp_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008851
8852 /* Restore the ep value that was overwritten with the above structure
8853 * copy.
8854 */
8855 newsp->ep = newep;
8856 newsp->hmac = NULL;
8857
8858 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008859 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
8860 inet_sk(oldsk)->inet_num)];
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008861 spin_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008862 pp = sctp_sk(oldsk)->bind_hash;
8863 sk_add_bind_node(newsk, &pp->owner);
8864 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008865 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008866 spin_unlock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008867
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008868 /* Copy the bind_addr list from the original endpoint to the new
8869 * endpoint so that we can handle restarts properly
8870 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08008871 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
8872 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008873
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874 /* Move any messages in the old socket's receive queue that are for the
8875 * peeled off association to the new socket's receive queue.
8876 */
8877 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
8878 event = sctp_skb2event(skb);
8879 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008880 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008881 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008882 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008883 }
8884 }
8885
8886 /* Clean up any messages pending delivery due to partial
8887 * delivery. Three cases:
8888 * 1) No partial deliver; no work.
8889 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
8890 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
8891 */
8892 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008893 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008895 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896 struct sk_buff_head *queue;
8897
8898 /* Decide which queue to move pd_lobby skbs to. */
8899 if (assoc->ulpq.pd_mode) {
8900 queue = &newsp->pd_lobby;
8901 } else
8902 queue = &newsk->sk_receive_queue;
8903
8904 /* Walk through the pd_lobby, looking for skbs that
8905 * need moved to the new socket.
8906 */
8907 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
8908 event = sctp_skb2event(skb);
8909 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008910 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008911 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008912 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008913 }
8914 }
8915
8916 /* Clear up any skbs waiting for the partial
8917 * delivery to finish.
8918 */
8919 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008920 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921
8922 }
8923
Xin Long13228232017-12-08 21:04:09 +08008924 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008925
Linus Torvalds1da177e2005-04-16 15:20:36 -07008926 /* Set the type of socket to indicate that it is peeled off from the
8927 * original UDP-style socket or created with the accept() call on a
8928 * TCP-style socket..
8929 */
8930 newsp->type = type;
8931
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008932 /* Mark the new socket "in-use" by the user so that any packets
8933 * that may arrive on the association after we've moved it are
8934 * queued to the backlog. This prevents a potential race between
8935 * backlog processing on the old socket and new-packet processing
8936 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07008937 *
8938 * The caller has just allocated newsk so we can guarantee that other
8939 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008940 */
Zach Brown5131a182007-06-22 15:14:46 -07008941 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Xin Longd04adf12017-10-28 02:13:29 +08008942 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943 sctp_assoc_migrate(assoc, newsk);
Xin Longd04adf12017-10-28 02:13:29 +08008944 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945
8946 /* If the association on the newsk is already closed before accept()
8947 * is called, set RCV_SHUTDOWN flag.
8948 */
Xin Longd46e4162016-06-09 22:48:18 +08008949 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08008950 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008951 newsk->sk_shutdown |= RCV_SHUTDOWN;
Xin Longd46e4162016-06-09 22:48:18 +08008952 } else {
Yafang Shaocbabf462017-12-20 11:12:54 +08008953 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
Xin Longd46e4162016-06-09 22:48:18 +08008954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955
wangweidong048ed4b2014-01-21 15:44:11 +08008956 release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008957}
8958
Neil Horman4d93df02007-08-15 16:07:44 -07008959
Linus Torvalds1da177e2005-04-16 15:20:36 -07008960/* This proto struct describes the ULP interface for SCTP. */
8961struct proto sctp_prot = {
8962 .name = "SCTP",
8963 .owner = THIS_MODULE,
8964 .close = sctp_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008965 .disconnect = sctp_disconnect,
8966 .accept = sctp_accept,
8967 .ioctl = sctp_ioctl,
8968 .init = sctp_init_sock,
8969 .destroy = sctp_destroy_sock,
8970 .shutdown = sctp_shutdown,
8971 .setsockopt = sctp_setsockopt,
8972 .getsockopt = sctp_getsockopt,
8973 .sendmsg = sctp_sendmsg,
8974 .recvmsg = sctp_recvmsg,
8975 .bind = sctp_bind,
8976 .backlog_rcv = sctp_backlog_rcv,
8977 .hash = sctp_hash,
8978 .unhash = sctp_unhash,
8979 .get_port = sctp_get_port,
8980 .obj_size = sizeof(struct sctp_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07008981 .useroffset = offsetof(struct sctp_sock, subscribe),
8982 .usersize = offsetof(struct sctp_sock, initmsg) -
8983 offsetof(struct sctp_sock, subscribe) +
8984 sizeof_field(struct sctp_sock, initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07008985 .sysctl_mem = sysctl_sctp_mem,
8986 .sysctl_rmem = sysctl_sctp_rmem,
8987 .sysctl_wmem = sysctl_sctp_wmem,
8988 .memory_pressure = &sctp_memory_pressure,
8989 .enter_memory_pressure = sctp_enter_memory_pressure,
8990 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008991 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008992};
8993
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008994#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08008995
Eric Dumazet602dd622015-12-01 07:20:07 -08008996#include <net/transp_v6.h>
8997static void sctp_v6_destroy_sock(struct sock *sk)
8998{
8999 sctp_destroy_sock(sk);
9000 inet6_destroy_sock(sk);
9001}
9002
Linus Torvalds1da177e2005-04-16 15:20:36 -07009003struct proto sctpv6_prot = {
9004 .name = "SCTPv6",
9005 .owner = THIS_MODULE,
9006 .close = sctp_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009007 .disconnect = sctp_disconnect,
9008 .accept = sctp_accept,
9009 .ioctl = sctp_ioctl,
9010 .init = sctp_init_sock,
Eric Dumazet602dd622015-12-01 07:20:07 -08009011 .destroy = sctp_v6_destroy_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009012 .shutdown = sctp_shutdown,
9013 .setsockopt = sctp_setsockopt,
9014 .getsockopt = sctp_getsockopt,
9015 .sendmsg = sctp_sendmsg,
9016 .recvmsg = sctp_recvmsg,
9017 .bind = sctp_bind,
9018 .backlog_rcv = sctp_backlog_rcv,
9019 .hash = sctp_hash,
9020 .unhash = sctp_unhash,
9021 .get_port = sctp_get_port,
9022 .obj_size = sizeof(struct sctp6_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07009023 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
9024 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
9025 offsetof(struct sctp6_sock, sctp.subscribe) +
9026 sizeof_field(struct sctp6_sock, sctp.initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07009027 .sysctl_mem = sysctl_sctp_mem,
9028 .sysctl_rmem = sysctl_sctp_rmem,
9029 .sysctl_wmem = sysctl_sctp_wmem,
9030 .memory_pressure = &sctp_memory_pressure,
9031 .enter_memory_pressure = sctp_enter_memory_pressure,
9032 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08009033 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009034};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00009035#endif /* IS_ENABLED(CONFIG_IPV6) */