blob: fdde697b37e735d7fa6acb24ed4260f93c86a50d [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
8 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05009 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
13 *
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
22 * any later version.
23 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050024 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080031 * along with GNU CC; see the file COPYING. If not, see
32 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
34 * Please send any bug reports or fixes you make to the
35 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020036 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * Written or modified by:
39 * La Monte H.P. Yarroll <piggy@acm.org>
40 * Narasimha Budihal <narsi@refcode.org>
41 * Karl Knutson <karl@athena.chicago.il.us>
42 * Jon Grimm <jgrimm@us.ibm.com>
43 * Xingang Guo <xingang.guo@intel.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Sridhar Samudrala <samudrala@us.ibm.com>
46 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
47 * Ardelle Fan <ardelle.fan@intel.com>
48 * Ryan Layer <rmlayer@us.ibm.com>
49 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
50 * Kevin Gao <kevin.gao@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
52
Joe Perches145ce502010-08-24 13:21:08 +000053#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
Herbert Xu5821c762016-01-24 21:20:12 +080055#include <crypto/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/types.h>
57#include <linux/kernel.h>
58#include <linux/wait.h>
59#include <linux/time.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010060#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/ip.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080062#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/fcntl.h>
64#include <linux/poll.h>
65#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090066#include <linux/slab.h>
Al Viro56b31d12012-08-18 00:25:51 -040067#include <linux/file.h>
Daniel Borkmannffd59392014-02-17 12:11:11 +010068#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#include <net/ip.h>
71#include <net/icmp.h>
72#include <net/route.h>
73#include <net/ipv6.h>
74#include <net/inet_common.h>
Neil Horman8465a5f2014-04-17 15:26:51 -040075#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#include <linux/socket.h> /* for sa_family_t */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040078#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <net/sock.h>
80#include <net/sctp/sctp.h>
81#include <net/sctp/sm.h>
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -030082#include <net/sctp/stream_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/* Forward declarations for internal helper functions. */
85static int sctp_writeable(struct sock *sk);
86static void sctp_wfree(struct sk_buff *skb);
Xin Longcea0cc82017-11-15 16:57:26 +080087static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +080088 size_t msg_len);
wangweidong26ac8e52013-12-23 12:16:51 +080089static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
91static int sctp_wait_for_accept(struct sock *sk, long timeo);
92static void sctp_wait_for_close(struct sock *sk, long timeo);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +020093static void sctp_destruct_sock(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
95 union sctp_addr *addr, int len);
96static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
97static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
98static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
99static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
100static int sctp_send_asconf(struct sctp_association *asoc,
101 struct sctp_chunk *chunk);
102static int sctp_do_bind(struct sock *, union sctp_addr *, int);
103static int sctp_autobind(struct sock *sk);
Xin Longb7ef2612017-08-11 10:23:50 +0800104static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
105 struct sctp_association *assoc,
106 enum sctp_socket_type type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Eric Dumazet06044752017-06-07 13:29:12 -0700108static unsigned long sctp_memory_pressure;
Eric Dumazet8d987e52010-11-09 23:24:26 +0000109static atomic_long_t sctp_memory_allocated;
Eric Dumazet17483762008-11-25 21:16:35 -0800110struct percpu_counter sctp_sockets_allocated;
Neil Horman4d93df02007-08-15 16:07:44 -0700111
Pavel Emelyanov5c52ba12008-07-16 20:28:10 -0700112static void sctp_enter_memory_pressure(struct sock *sk)
Neil Horman4d93df02007-08-15 16:07:44 -0700113{
114 sctp_memory_pressure = 1;
115}
116
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/* Get the sndbuf space available at the time on the association. */
119static inline int sctp_wspace(struct sctp_association *asoc)
120{
Neil Horman4d93df02007-08-15 16:07:44 -0700121 int amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Neil Horman4d93df02007-08-15 16:07:44 -0700123 if (asoc->ep->sndbuf_policy)
124 amt = asoc->sndbuf_used;
125 else
Eric Dumazet31e6d362009-06-17 19:05:41 -0700126 amt = sk_wmem_alloc_get(asoc->base.sk);
Neil Horman4d93df02007-08-15 16:07:44 -0700127
128 if (amt >= asoc->base.sk->sk_sndbuf) {
129 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
130 amt = 0;
131 else {
132 amt = sk_stream_wspace(asoc->base.sk);
133 if (amt < 0)
134 amt = 0;
135 }
Neil Horman4eb701d2005-04-28 12:02:04 -0700136 } else {
Neil Horman4d93df02007-08-15 16:07:44 -0700137 amt = asoc->base.sk->sk_sndbuf - amt;
Neil Horman4eb701d2005-04-28 12:02:04 -0700138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return amt;
140}
141
142/* Increment the used sndbuf space count of the corresponding association by
143 * the size of the outgoing data chunk.
144 * Also, set the skb destructor for sndbuf accounting later.
145 *
146 * Since it is always 1-1 between chunk and skb, and also a new skb is always
147 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
148 * destructor in the data chunk skb for the purpose of the sndbuf space
149 * tracking.
150 */
151static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
152{
153 struct sctp_association *asoc = chunk->asoc;
154 struct sock *sk = asoc->base.sk;
155
156 /* The sndbuf space is tracked per association. */
157 sctp_association_hold(asoc);
158
Neil Horman4eb701d2005-04-28 12:02:04 -0700159 skb_set_owner_w(chunk->skb, sk);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 chunk->skb->destructor = sctp_wfree;
162 /* Save the chunk pointer in skb for sctp_wfree to use later. */
Daniel Borkmannf869c912014-11-20 01:54:48 +0100163 skb_shinfo(chunk->skb)->destructor_arg = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Neil Horman4eb701d2005-04-28 12:02:04 -0700165 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
166 sizeof(struct sk_buff) +
167 sizeof(struct sctp_chunk);
168
Reshetova, Elena14afee42017-06-30 13:08:00 +0300169 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800170 sk->sk_wmem_queued += chunk->skb->truesize;
171 sk_mem_charge(sk, chunk->skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Xin Longd04adf12017-10-28 02:13:29 +0800174static void sctp_clear_owner_w(struct sctp_chunk *chunk)
175{
176 skb_orphan(chunk->skb);
177}
178
179static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
180 void (*cb)(struct sctp_chunk *))
181
182{
183 struct sctp_outq *q = &asoc->outqueue;
184 struct sctp_transport *t;
185 struct sctp_chunk *chunk;
186
187 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
188 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
189 cb(chunk);
190
Xin Longa8dd3972017-11-26 20:56:07 +0800191 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800192 cb(chunk);
193
Xin Longa8dd3972017-11-26 20:56:07 +0800194 list_for_each_entry(chunk, &q->sacked, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800195 cb(chunk);
196
Xin Longa8dd3972017-11-26 20:56:07 +0800197 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800198 cb(chunk);
199
200 list_for_each_entry(chunk, &q->out_chunk_list, list)
201 cb(chunk);
202}
203
Xin Long13228232017-12-08 21:04:09 +0800204static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
205 void (*cb)(struct sk_buff *, struct sock *))
206
207{
208 struct sk_buff *skb, *tmp;
209
210 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
211 cb(skb, sk);
212
213 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
214 cb(skb, sk);
215
216 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
217 cb(skb, sk);
218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/* Verify that this is a valid address. */
221static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
222 int len)
223{
224 struct sctp_af *af;
225
226 /* Verify basic sockaddr. */
227 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
228 if (!af)
229 return -EINVAL;
230
231 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700232 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -EINVAL;
234
235 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
236 return -EINVAL;
237
238 return 0;
239}
240
241/* Look up the association by its id. If this is not a UDP-style
242 * socket, the ID field is always ignored.
243 */
244struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
245{
246 struct sctp_association *asoc = NULL;
247
248 /* If this is not a UDP-style socket, assoc id should be ignored. */
249 if (!sctp_style(sk, UDP)) {
250 /* Return NULL if the socket state is not ESTABLISHED. It
251 * could be a TCP-style listening socket or a socket which
252 * hasn't yet called connect() to establish an association.
253 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -0300254 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return NULL;
256
257 /* Get the first and the only association from the list. */
258 if (!list_empty(&sctp_sk(sk)->ep->asocs))
259 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
260 struct sctp_association, asocs);
261 return asoc;
262 }
263
264 /* Otherwise this is a UDP-style socket. */
265 if (!id || (id == (sctp_assoc_t)-1))
266 return NULL;
267
268 spin_lock_bh(&sctp_assocs_id_lock);
269 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
270 spin_unlock_bh(&sctp_assocs_id_lock);
271
272 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
273 return NULL;
274
275 return asoc;
276}
277
278/* Look up the transport from an address and an assoc id. If both address and
279 * id are specified, the associations matching the address and the id should be
280 * the same.
281 */
282static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
283 struct sockaddr_storage *addr,
284 sctp_assoc_t id)
285{
286 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
Xin Long6f29a132017-01-24 14:01:53 +0800287 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 union sctp_addr *laddr = (union sctp_addr *)addr;
Xin Long6f29a132017-01-24 14:01:53 +0800289 struct sctp_transport *transport;
290
Xin Long912964e2017-02-07 20:56:08 +0800291 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
Xin Long6f29a132017-01-24 14:01:53 +0800292 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800295 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 if (!addr_asoc)
299 return NULL;
300
301 id_asoc = sctp_id2assoc(sk, id);
302 if (id_asoc && (id_asoc != addr_asoc))
303 return NULL;
304
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600305 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 (union sctp_addr *)addr);
307
308 return transport;
309}
310
311/* API 3.1.2 bind() - UDP Style Syntax
312 * The syntax of bind() is,
313 *
314 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
315 *
316 * sd - the socket descriptor returned by socket().
317 * addr - the address structure (struct sockaddr_in or struct
318 * sockaddr_in6 [RFC 2553]),
319 * addr_len - the size of the address structure.
320 */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200321static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 int retval = 0;
324
wangweidong048ed4b2014-01-21 15:44:11 +0800325 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Daniel Borkmannbb333812013-06-28 19:49:40 +0200327 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
328 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* Disallow binding twice. */
331 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700332 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 addr_len);
334 else
335 retval = -EINVAL;
336
wangweidong048ed4b2014-01-21 15:44:11 +0800337 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 return retval;
340}
341
342static long sctp_get_port_local(struct sock *, union sctp_addr *);
343
344/* Verify this is a valid sockaddr. */
345static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
346 union sctp_addr *addr, int len)
347{
348 struct sctp_af *af;
349
350 /* Check minimum size. */
351 if (len < sizeof (struct sockaddr))
352 return NULL;
353
Xin Longc5006b82018-01-15 17:02:00 +0800354 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
355 return NULL;
356
Vlad Yasevich7dab83d2008-07-18 23:05:40 -0700357 /* V4 mapped address are really of AF_INET family */
358 if (addr->sa.sa_family == AF_INET6 &&
Xin Longc5006b82018-01-15 17:02:00 +0800359 ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
360 !opt->pf->af_supported(AF_INET, opt))
361 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* If we get this far, af is valid. */
364 af = sctp_get_af_specific(addr->sa.sa_family);
365
366 if (len < af->sockaddr_len)
367 return NULL;
368
369 return af;
370}
371
372/* Bind a local address either to an endpoint or to an association. */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200373static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Eric W. Biederman35946982012-11-16 03:03:12 +0000375 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct sctp_sock *sp = sctp_sk(sk);
377 struct sctp_endpoint *ep = sp->ep;
378 struct sctp_bind_addr *bp = &ep->base.bind_addr;
379 struct sctp_af *af;
380 unsigned short snum;
381 int ret = 0;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* Common sockaddr verification. */
384 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700385 if (!af) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200386 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
387 __func__, sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700389 }
390
391 snum = ntohs(addr->v4.sin_port);
392
Daniel Borkmannbb333812013-06-28 19:49:40 +0200393 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
394 __func__, sk, &addr->sa, bp->port, snum, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /* PF specific bind() address verification. */
397 if (!sp->pf->bind_verify(sp, addr))
398 return -EADDRNOTAVAIL;
399
Vlad Yasevich8b358052007-05-15 17:14:58 -0400400 /* We must either be unbound, or bind to the same port.
401 * It's OK to allow 0 ports if we are already bound.
402 * We'll just inhert an already bound port in this case
403 */
404 if (bp->port) {
405 if (!snum)
406 snum = bp->port;
407 else if (snum != bp->port) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200408 pr_debug("%s: new port %d doesn't match existing port "
409 "%d\n", __func__, snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400410 return -EINVAL;
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
Krister Johansen4548b682017-01-20 17:49:11 -0800414 if (snum && snum < inet_prot_sock(net) &&
Eric W. Biederman35946982012-11-16 03:03:12 +0000415 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return -EACCES;
417
Vlad Yasevich4e540642008-07-18 23:06:32 -0700418 /* See if the address matches any of the addresses we may have
419 * already bound before checking against other endpoints.
420 */
421 if (sctp_bind_addr_match(bp, addr, sp))
422 return -EINVAL;
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* Make sure we are allowed to bind here.
425 * The function sctp_get_port_local() does duplicate address
426 * detection.
427 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900428 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if ((ret = sctp_get_port_local(sk, addr))) {
Vlad Yasevich4e540642008-07-18 23:06:32 -0700430 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
433 /* Refresh ephemeral port. */
434 if (!bp->port)
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000435 bp->port = inet_sk(sk)->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Vlad Yasevich559cf712007-09-16 16:03:28 -0700437 /* Add the address to the bind address list.
438 * Use GFP_ATOMIC since BHs will be disabled.
439 */
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300440 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
441 SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 /* Copy back into socket for getsockname() use. */
444 if (!ret) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000445 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600446 sp->pf->to_sk_saddr(addr, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
449 return ret;
450}
451
452 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
453 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900454 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900456 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900458 * subsequent ASCONF. Note this restriction binds each side, so at any
459 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * from each endpoint).
461 */
462static int sctp_send_asconf(struct sctp_association *asoc,
463 struct sctp_chunk *chunk)
464{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000465 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int retval = 0;
467
468 /* If there is an outstanding ASCONF chunk, queue it for later
469 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900470 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700472 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900473 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475
476 /* Hold the chunk until an ASCONF_ACK is received. */
477 sctp_chunk_hold(chunk);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000478 retval = sctp_primitive_ASCONF(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (retval)
480 sctp_chunk_free(chunk);
481 else
482 asoc->addip_last_asconf = chunk;
483
484out:
485 return retval;
486}
487
488/* Add a list of addresses as bind addresses to local endpoint or
489 * association.
490 *
491 * Basically run through each address specified in the addrs/addrcnt
492 * array/length pair, determine if it is IPv6 or IPv4 and call
493 * sctp_do_bind() on it.
494 *
495 * If any of them fails, then the operation will be reversed and the
496 * ones that were added will be removed.
497 *
498 * Only sctp_setsockopt_bindx() is supposed to call this function.
499 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200500static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 int cnt;
503 int retval = 0;
504 void *addr_buf;
505 struct sockaddr *sa_addr;
506 struct sctp_af *af;
507
Daniel Borkmannbb333812013-06-28 19:49:40 +0200508 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
509 addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 addr_buf = addrs;
512 for (cnt = 0; cnt < addrcnt; cnt++) {
513 /* The list may contain either IPv4 or IPv6 address;
514 * determine the address length for walking thru the list.
515 */
Joe Perchesea110732011-06-13 16:21:26 +0000516 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 af = sctp_get_af_specific(sa_addr->sa_family);
518 if (!af) {
519 retval = -EINVAL;
520 goto err_bindx_add;
521 }
522
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900523 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 af->sockaddr_len);
525
526 addr_buf += af->sockaddr_len;
527
528err_bindx_add:
529 if (retval < 0) {
530 /* Failed. Cleanup the ones that have been added */
531 if (cnt > 0)
532 sctp_bindx_rem(sk, addrs, cnt);
533 return retval;
534 }
535 }
536
537 return retval;
538}
539
540/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
541 * associations that are part of the endpoint indicating that a list of local
542 * addresses are added to the endpoint.
543 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900544 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 * association, we do not send the chunk for that association. But it will not
546 * affect other associations.
547 *
548 * Only sctp_setsockopt_bindx() is supposed to call this function.
549 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900550static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct sockaddr *addrs,
552 int addrcnt)
553{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000554 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct sctp_sock *sp;
556 struct sctp_endpoint *ep;
557 struct sctp_association *asoc;
558 struct sctp_bind_addr *bp;
559 struct sctp_chunk *chunk;
560 struct sctp_sockaddr_entry *laddr;
561 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700562 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 void *addr_buf;
564 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct list_head *p;
566 int i;
567 int retval = 0;
568
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000569 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return retval;
571
572 sp = sctp_sk(sk);
573 ep = sp->ep;
574
Daniel Borkmannbb333812013-06-28 19:49:40 +0200575 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
576 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700578 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (!asoc->peer.asconf_capable)
580 continue;
581
582 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
583 continue;
584
585 if (!sctp_state(asoc, ESTABLISHED))
586 continue;
587
588 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900589 * in the bind address list of the association. If so,
590 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * other associations.
592 */
593 addr_buf = addrs;
594 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000595 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 af = sctp_get_af_specific(addr->v4.sin_family);
597 if (!af) {
598 retval = -EINVAL;
599 goto out;
600 }
601
602 if (sctp_assoc_lookup_laddr(asoc, addr))
603 break;
604
605 addr_buf += af->sockaddr_len;
606 }
607 if (i < addrcnt)
608 continue;
609
Vlad Yasevich559cf712007-09-16 16:03:28 -0700610 /* Use the first valid address in bind addr list of
611 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 bp = &asoc->base.bind_addr;
614 p = bp->address_list.next;
615 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800616 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 addrcnt, SCTP_PARAM_ADD_IP);
618 if (!chunk) {
619 retval = -ENOMEM;
620 goto out;
621 }
622
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700623 /* Add the new addresses to the bind address list with
624 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700626 addr_buf = addrs;
627 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000628 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700629 af = sctp_get_af_specific(addr->v4.sin_family);
630 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800631 retval = sctp_add_bind_addr(bp, &saveaddr,
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300632 sizeof(saveaddr),
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800633 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700634 addr_buf += af->sockaddr_len;
635 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900636 if (asoc->src_out_of_asoc_ok) {
637 struct sctp_transport *trans;
638
639 list_for_each_entry(trans,
640 &asoc->peer.transport_addr_list, transports) {
641 /* Clear the source and route cache */
Julian Anastasovc86a7732017-02-06 23:14:13 +0200642 sctp_transport_dst_release(trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900643 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
644 2*asoc->pathmtu, 4380));
645 trans->ssthresh = asoc->peer.i.a_rwnd;
646 trans->rto = asoc->rto_initial;
Michele Baldessari196d6752012-12-01 04:49:42 +0000647 sctp_max_rto(asoc, trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900648 trans->rtt = trans->srtt = trans->rttvar = 0;
649 sctp_transport_route(trans, NULL,
650 sctp_sk(asoc->base.sk));
651 }
652 }
653 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655
656out:
657 return retval;
658}
659
660/* Remove a list of addresses from bind addresses list. Do not remove the
661 * last address.
662 *
663 * Basically run through each address specified in the addrs/addrcnt
664 * array/length pair, determine if it is IPv6 or IPv4 and call
665 * sctp_del_bind() on it.
666 *
667 * If any of them fails, then the operation will be reversed and the
668 * ones that were removed will be added back.
669 *
670 * At least one address has to be left; if only one address is
671 * available, the operation will return -EBUSY.
672 *
673 * Only sctp_setsockopt_bindx() is supposed to call this function.
674 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200675static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 struct sctp_sock *sp = sctp_sk(sk);
678 struct sctp_endpoint *ep = sp->ep;
679 int cnt;
680 struct sctp_bind_addr *bp = &ep->base.bind_addr;
681 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800683 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct sctp_af *af;
685
Daniel Borkmannbb333812013-06-28 19:49:40 +0200686 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
687 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 addr_buf = addrs;
690 for (cnt = 0; cnt < addrcnt; cnt++) {
691 /* If the bind address list is empty or if there is only one
692 * bind address, there is nothing more to be removed (we need
693 * at least one address here).
694 */
695 if (list_empty(&bp->address_list) ||
696 (sctp_list_single_entry(&bp->address_list))) {
697 retval = -EBUSY;
698 goto err_bindx_rem;
699 }
700
Joe Perchesea110732011-06-13 16:21:26 +0000701 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800702 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (!af) {
704 retval = -EINVAL;
705 goto err_bindx_rem;
706 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700707
708 if (!af->addr_valid(sa_addr, sp, NULL)) {
709 retval = -EADDRNOTAVAIL;
710 goto err_bindx_rem;
711 }
712
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000713 if (sa_addr->v4.sin_port &&
714 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 retval = -EINVAL;
716 goto err_bindx_rem;
717 }
718
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000719 if (!sa_addr->v4.sin_port)
720 sa_addr->v4.sin_port = htons(bp->port);
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* FIXME - There is probably a need to check if sk->sk_saddr and
723 * sk->sk_rcv_addr are currently set to one of the addresses to
724 * be removed. This is something which needs to be looked into
725 * when we are fixing the outstanding issues with multi-homing
726 * socket routing and failover schemes. Refer to comments in
727 * sctp_do_bind(). -daisy
728 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400729 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 addr_buf += af->sockaddr_len;
732err_bindx_rem:
733 if (retval < 0) {
734 /* Failed. Add the ones that has been removed back */
735 if (cnt > 0)
736 sctp_bindx_add(sk, addrs, cnt);
737 return retval;
738 }
739 }
740
741 return retval;
742}
743
744/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
745 * the associations that are part of the endpoint indicating that a list of
746 * local addresses are removed from the endpoint.
747 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900748 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * association, we do not send the chunk for that association. But it will not
750 * affect other associations.
751 *
752 * Only sctp_setsockopt_bindx() is supposed to call this function.
753 */
754static int sctp_send_asconf_del_ip(struct sock *sk,
755 struct sockaddr *addrs,
756 int addrcnt)
757{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000758 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 struct sctp_sock *sp;
760 struct sctp_endpoint *ep;
761 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700762 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct sctp_bind_addr *bp;
764 struct sctp_chunk *chunk;
765 union sctp_addr *laddr;
766 void *addr_buf;
767 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700768 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int i;
770 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900771 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Michio Honda8a07eb02011-04-26 20:19:36 +0900773 chunk = NULL;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000774 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return retval;
776
777 sp = sctp_sk(sk);
778 ep = sp->ep;
779
Daniel Borkmannbb333812013-06-28 19:49:40 +0200780 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
781 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700783 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 if (!asoc->peer.asconf_capable)
786 continue;
787
788 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
789 continue;
790
791 if (!sctp_state(asoc, ESTABLISHED))
792 continue;
793
794 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900795 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 * If so, do not send the asconf chunk to its peer, but
797 * continue with other associations.
798 */
799 addr_buf = addrs;
800 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000801 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 af = sctp_get_af_specific(laddr->v4.sin_family);
803 if (!af) {
804 retval = -EINVAL;
805 goto out;
806 }
807
808 if (!sctp_assoc_lookup_laddr(asoc, laddr))
809 break;
810
811 addr_buf += af->sockaddr_len;
812 }
813 if (i < addrcnt)
814 continue;
815
816 /* Find one address in the association's bind address list
817 * that is not in the packed array of addresses. This is to
818 * make sure that we do not delete all the addresses in the
819 * association.
820 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 bp = &asoc->base.bind_addr;
822 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
823 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900824 if ((laddr == NULL) && (addrcnt == 1)) {
825 if (asoc->asconf_addr_del_pending)
826 continue;
827 asoc->asconf_addr_del_pending =
828 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900829 if (asoc->asconf_addr_del_pending == NULL) {
830 retval = -ENOMEM;
831 goto out;
832 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900833 asoc->asconf_addr_del_pending->sa.sa_family =
834 addrs->sa_family;
835 asoc->asconf_addr_del_pending->v4.sin_port =
836 htons(bp->port);
837 if (addrs->sa_family == AF_INET) {
838 struct sockaddr_in *sin;
839
840 sin = (struct sockaddr_in *)addrs;
841 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
842 } else if (addrs->sa_family == AF_INET6) {
843 struct sockaddr_in6 *sin6;
844
845 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000846 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900847 }
Daniel Borkmannbb333812013-06-28 19:49:40 +0200848
849 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
850 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
851 asoc->asconf_addr_del_pending);
852
Michio Honda8a07eb02011-04-26 20:19:36 +0900853 asoc->src_out_of_asoc_ok = 1;
854 stored = 1;
855 goto skip_mkasconf;
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Daniel Borkmann88362ad2013-09-07 20:51:21 +0200858 if (laddr == NULL)
859 return -EINVAL;
860
Vlad Yasevich559cf712007-09-16 16:03:28 -0700861 /* We do not need RCU protection throughout this loop
862 * because this is done under a socket lock from the
863 * setsockopt call.
864 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
866 SCTP_PARAM_DEL_IP);
867 if (!chunk) {
868 retval = -ENOMEM;
869 goto out;
870 }
871
Michio Honda8a07eb02011-04-26 20:19:36 +0900872skip_mkasconf:
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700873 /* Reset use_as_src flag for the addresses in the bind address
874 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700876 addr_buf = addrs;
877 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000878 laddr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700879 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700880 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a132006-11-20 17:05:23 -0800881 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800882 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700883 }
884 addr_buf += af->sockaddr_len;
885 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700886
887 /* Update the route and saddr entries for all the transports
888 * as some of the addresses in the bind address list are
889 * about to be deleted and cannot be used as source addresses.
890 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700891 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
892 transports) {
Julian Anastasovc86a7732017-02-06 23:14:13 +0200893 sctp_transport_dst_release(transport);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700894 sctp_transport_route(transport, NULL,
895 sctp_sk(asoc->base.sk));
896 }
897
Michio Honda8a07eb02011-04-26 20:19:36 +0900898 if (stored)
899 /* We don't need to transmit ASCONF */
900 continue;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700901 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903out:
904 return retval;
905}
906
Michio Honda9f7d6532011-04-26 19:32:51 +0900907/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
908int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
909{
910 struct sock *sk = sctp_opt2sk(sp);
911 union sctp_addr *addr;
912 struct sctp_af *af;
913
914 /* It is safe to write port space in caller. */
915 addr = &addrw->a;
916 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
917 af = sctp_get_af_specific(addr->sa.sa_family);
918 if (!af)
919 return -EINVAL;
920 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
921 return -EINVAL;
922
923 if (addrw->state == SCTP_ADDR_NEW)
924 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
925 else
926 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
930 *
931 * API 8.1
932 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
933 * int flags);
934 *
935 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
936 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
937 * or IPv6 addresses.
938 *
939 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
940 * Section 3.1.2 for this usage.
941 *
942 * addrs is a pointer to an array of one or more socket addresses. Each
943 * address is contained in its appropriate structure (i.e. struct
944 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700945 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * representation is termed a "packed array" of addresses). The caller
947 * specifies the number of addresses in the array with addrcnt.
948 *
949 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
950 * -1, and sets errno to the appropriate error code.
951 *
952 * For SCTP, the port given in each socket address must be the same, or
953 * sctp_bindx() will fail, setting errno to EINVAL.
954 *
955 * The flags parameter is formed from the bitwise OR of zero or more of
956 * the following currently defined flags:
957 *
958 * SCTP_BINDX_ADD_ADDR
959 *
960 * SCTP_BINDX_REM_ADDR
961 *
962 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
963 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
964 * addresses from the association. The two flags are mutually exclusive;
965 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
966 * not remove all addresses from an association; sctp_bindx() will
967 * reject such an attempt with EINVAL.
968 *
969 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
970 * additional addresses with an endpoint after calling bind(). Or use
971 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
972 * socket is associated with so that no new association accepted will be
973 * associated with those addresses. If the endpoint supports dynamic
974 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
975 * endpoint to send the appropriate message to the peer to change the
976 * peers address lists.
977 *
978 * Adding and removing addresses from a connected association is
979 * optional functionality. Implementations that do not support this
980 * functionality should return EOPNOTSUPP.
981 *
982 * Basically do nothing but copying the addresses from user to kernel
983 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700984 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
985 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
988 * it.
989 *
990 * sk The sk of the socket
991 * addrs The pointer to the addresses in user land
992 * addrssize Size of the addrs buffer
993 * op Operation to perform (add or remove, see the flags of
994 * sctp_bindx)
995 *
996 * Returns 0 if ok, <0 errno code on error.
997 */
wangweidong26ac8e52013-12-23 12:16:51 +0800998static int sctp_setsockopt_bindx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +0200999 struct sockaddr __user *addrs,
1000 int addrs_size, int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 struct sockaddr *kaddrs;
1003 int err;
1004 int addrcnt = 0;
1005 int walk_size = 0;
1006 struct sockaddr *sa_addr;
1007 void *addr_buf;
1008 struct sctp_af *af;
1009
Daniel Borkmannbb333812013-06-28 19:49:40 +02001010 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1011 __func__, sk, addrs, addrs_size, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 if (unlikely(addrs_size <= 0))
1014 return -EINVAL;
1015
Al Viroc981f252018-01-07 13:19:09 -05001016 kaddrs = vmemdup_user(addrs, addrs_size);
1017 if (unlikely(IS_ERR(kaddrs)))
1018 return PTR_ERR(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001020 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 addr_buf = kaddrs;
1022 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001023 if (walk_size + sizeof(sa_family_t) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001024 kvfree(kaddrs);
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001025 return -EINVAL;
1026 }
1027
Joe Perchesea110732011-06-13 16:21:26 +00001028 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 af = sctp_get_af_specific(sa_addr->sa_family);
1030
1031 /* If the address family is not supported or if this address
1032 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001033 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001035 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return -EINVAL;
1037 }
1038 addrcnt++;
1039 addr_buf += af->sockaddr_len;
1040 walk_size += af->sockaddr_len;
1041 }
1042
1043 /* Do the work. */
1044 switch (op) {
1045 case SCTP_BINDX_ADD_ADDR:
1046 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1047 if (err)
1048 goto out;
1049 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1050 break;
1051
1052 case SCTP_BINDX_REM_ADDR:
1053 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1054 if (err)
1055 goto out;
1056 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1057 break;
1058
1059 default:
1060 err = -EINVAL;
1061 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064out:
Al Viroc981f252018-01-07 13:19:09 -05001065 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 return err;
1068}
1069
Frank Filz3f7a87d2005-06-20 13:14:57 -07001070/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1071 *
1072 * Common routine for handling connect() and sctp_connectx().
1073 * Connect will come in with just a single address.
1074 */
wangweidong26ac8e52013-12-23 12:16:51 +08001075static int __sctp_connect(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001076 struct sockaddr *kaddrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001077 int addrs_size,
1078 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001079{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001080 struct net *net = sock_net(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001081 struct sctp_sock *sp;
1082 struct sctp_endpoint *ep;
1083 struct sctp_association *asoc = NULL;
1084 struct sctp_association *asoc2;
1085 struct sctp_transport *transport;
1086 union sctp_addr to;
Xin Long1c662012017-08-05 19:59:54 +08001087 enum sctp_scope scope;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001088 long timeo;
1089 int err = 0;
1090 int addrcnt = 0;
1091 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001092 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001093 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001094 unsigned short port;
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001095 unsigned int f_flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001096
1097 sp = sctp_sk(sk);
1098 ep = sp->ep;
1099
1100 /* connect() cannot be done on a socket that is already in ESTABLISHED
1101 * state - UDP-style peeled off socket or a TCP-style socket that
1102 * is already connected.
1103 * It cannot be done even on a TCP-style listening socket.
1104 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03001105 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
Frank Filz3f7a87d2005-06-20 13:14:57 -07001106 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1107 err = -EISCONN;
1108 goto out_free;
1109 }
1110
1111 /* Walk through the addrs buffer and count the number of addresses. */
1112 addr_buf = kaddrs;
1113 while (walk_size < addrs_size) {
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001114 struct sctp_af *af;
1115
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001116 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1117 err = -EINVAL;
1118 goto out_free;
1119 }
1120
Joe Perchesea110732011-06-13 16:21:26 +00001121 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001122 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001123
1124 /* If the address family is not supported or if this address
1125 * causes the address buffer to overflow return EINVAL.
1126 */
1127 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1128 err = -EINVAL;
1129 goto out_free;
1130 }
1131
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001132 port = ntohs(sa_addr->v4.sin_port);
1133
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001134 /* Save current address so we can work with it */
1135 memcpy(&to, sa_addr, af->sockaddr_len);
1136
1137 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001138 if (err)
1139 goto out_free;
1140
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001141 /* Make sure the destination port is correctly set
1142 * in all addresses.
1143 */
Wei Yongjun524fba62013-04-03 03:02:28 +00001144 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1145 err = -EINVAL;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001146 goto out_free;
Wei Yongjun524fba62013-04-03 03:02:28 +00001147 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001148
1149 /* Check if there already is a matching association on the
1150 * endpoint (other than the one created here).
1151 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001152 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001153 if (asoc2 && asoc2 != asoc) {
1154 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1155 err = -EISCONN;
1156 else
1157 err = -EALREADY;
1158 goto out_free;
1159 }
1160
1161 /* If we could not find a matching association on the endpoint,
1162 * make sure that there is no peeled-off association matching
1163 * the peer address even on another socket.
1164 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001165 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001166 err = -EADDRNOTAVAIL;
1167 goto out_free;
1168 }
1169
1170 if (!asoc) {
1171 /* If a bind() or sctp_bindx() is not called prior to
1172 * an sctp_connectx() call, the system picks an
1173 * ephemeral port and will choose an address set
1174 * equivalent to binding with a wildcard address.
1175 */
1176 if (!ep->base.bind_addr.port) {
1177 if (sctp_autobind(sk)) {
1178 err = -EAGAIN;
1179 goto out_free;
1180 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001181 } else {
1182 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001183 * If an unprivileged user inherits a 1-many
1184 * style socket with open associations on a
1185 * privileged port, it MAY be permitted to
1186 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001187 * be permitted to open new associations.
1188 */
Krister Johansen4548b682017-01-20 17:49:11 -08001189 if (ep->base.bind_addr.port <
1190 inet_prot_sock(net) &&
1191 !ns_capable(net->user_ns,
1192 CAP_NET_BIND_SERVICE)) {
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001193 err = -EACCES;
1194 goto out_free;
1195 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001196 }
1197
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001198 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001199 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1200 if (!asoc) {
1201 err = -ENOMEM;
1202 goto out_free;
1203 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001204
1205 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1206 GFP_KERNEL);
1207 if (err < 0) {
1208 goto out_free;
1209 }
1210
Frank Filz3f7a87d2005-06-20 13:14:57 -07001211 }
1212
1213 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001214 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001215 SCTP_UNKNOWN);
1216 if (!transport) {
1217 err = -ENOMEM;
1218 goto out_free;
1219 }
1220
1221 addrcnt++;
1222 addr_buf += af->sockaddr_len;
1223 walk_size += af->sockaddr_len;
1224 }
1225
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001226 /* In case the user of sctp_connectx() wants an association
1227 * id back, assign one now.
1228 */
1229 if (assoc_id) {
1230 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1231 if (err < 0)
1232 goto out_free;
1233 }
1234
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001235 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001236 if (err < 0) {
1237 goto out_free;
1238 }
1239
1240 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001241 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001242 sp->pf->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001243 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001244
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001245 /* in-kernel sockets don't generally have a file allocated to them
1246 * if all they do is call sock_create_kern().
1247 */
1248 if (sk->sk_socket->file)
1249 f_flags = sk->sk_socket->file->f_flags;
1250
1251 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1252
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001253 if (assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001254 *assoc_id = asoc->assoc_id;
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001255 err = sctp_wait_for_connect(asoc, &timeo);
1256 /* Note: the asoc may be freed after the return of
1257 * sctp_wait_for_connect.
1258 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001259
1260 /* Don't free association on exit. */
1261 asoc = NULL;
1262
1263out_free:
Daniel Borkmannbb333812013-06-28 19:49:40 +02001264 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1265 __func__, asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001266
Neil Horman2eebc1e2012-07-16 09:13:51 +00001267 if (asoc) {
1268 /* sctp_primitive_ASSOCIATE may have added this association
1269 * To the hash table, try to unhash it, just in case, its a noop
1270 * if it wasn't hashed so we're safe
1271 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001272 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001273 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001274 return err;
1275}
1276
1277/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1278 *
1279 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001280 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1281 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001282 *
1283 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1284 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1285 * or IPv6 addresses.
1286 *
1287 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1288 * Section 3.1.2 for this usage.
1289 *
1290 * addrs is a pointer to an array of one or more socket addresses. Each
1291 * address is contained in its appropriate structure (i.e. struct
1292 * sockaddr_in or struct sockaddr_in6) the family of the address type
1293 * must be used to distengish the address length (note that this
1294 * representation is termed a "packed array" of addresses). The caller
1295 * specifies the number of addresses in the array with addrcnt.
1296 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001297 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1298 * the association id of the new association. On failure, sctp_connectx()
1299 * returns -1, and sets errno to the appropriate error code. The assoc_id
1300 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001301 *
1302 * For SCTP, the port given in each socket address must be the same, or
1303 * sctp_connectx() will fail, setting errno to EINVAL.
1304 *
1305 * An application can use sctp_connectx to initiate an association with
1306 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1307 * allows a caller to specify multiple addresses at which a peer can be
1308 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001309 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001310 * specifies that the stack will try to make use of all the addresses in
1311 * the list when needed.
1312 *
1313 * Note that the list of addresses passed in is only used for setting up
1314 * the association. It does not necessarily equal the set of addresses
1315 * the peer uses for the resulting association. If the caller wants to
1316 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1317 * retrieve them after the association has been set up.
1318 *
1319 * Basically do nothing but copying the addresses from user to kernel
1320 * land and invoking either sctp_connectx(). This is used for tunneling
1321 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1322 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07001323 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1324 * it.
1325 *
1326 * sk The sk of the socket
1327 * addrs The pointer to the addresses in user land
1328 * addrssize Size of the addrs buffer
1329 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001330 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001331 */
wangweidong26ac8e52013-12-23 12:16:51 +08001332static int __sctp_setsockopt_connectx(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001333 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001334 int addrs_size,
1335 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001336{
Frank Filz3f7a87d2005-06-20 13:14:57 -07001337 struct sockaddr *kaddrs;
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02001338 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001339
Daniel Borkmannbb333812013-06-28 19:49:40 +02001340 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1341 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001342
1343 if (unlikely(addrs_size <= 0))
1344 return -EINVAL;
1345
Al Viroc981f252018-01-07 13:19:09 -05001346 kaddrs = vmemdup_user(addrs, addrs_size);
1347 if (unlikely(IS_ERR(kaddrs)))
1348 return PTR_ERR(kaddrs);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001349
Al Viroc981f252018-01-07 13:19:09 -05001350 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
1351 kvfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001352
Frank Filz3f7a87d2005-06-20 13:14:57 -07001353 return err;
1354}
1355
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001356/*
1357 * This is an older interface. It's kept for backward compatibility
1358 * to the option that doesn't provide association id.
1359 */
wangweidong26ac8e52013-12-23 12:16:51 +08001360static int sctp_setsockopt_connectx_old(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001361 struct sockaddr __user *addrs,
1362 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001363{
1364 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1365}
1366
1367/*
1368 * New interface for the API. The since the API is done with a socket
1369 * option, to make it simple we feed back the association id is as a return
1370 * indication to the call. Error is always negative and association id is
1371 * always positive.
1372 */
wangweidong26ac8e52013-12-23 12:16:51 +08001373static int sctp_setsockopt_connectx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001374 struct sockaddr __user *addrs,
1375 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001376{
1377 sctp_assoc_t assoc_id = 0;
1378 int err = 0;
1379
1380 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1381
1382 if (err)
1383 return err;
1384 else
1385 return assoc_id;
1386}
1387
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001388/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001389 * New (hopefully final) interface for the API.
1390 * We use the sctp_getaddrs_old structure so that use-space library
Daniel Borkmannffd59392014-02-17 12:11:11 +01001391 * can avoid any unnecessary allocations. The only different part
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001392 * is that we store the actual length of the address buffer into the
Daniel Borkmannffd59392014-02-17 12:11:11 +01001393 * addrs_num structure member. That way we can re-use the existing
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001394 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001395 */
Daniel Borkmannffd59392014-02-17 12:11:11 +01001396#ifdef CONFIG_COMPAT
1397struct compat_sctp_getaddrs_old {
1398 sctp_assoc_t assoc_id;
1399 s32 addr_num;
1400 compat_uptr_t addrs; /* struct sockaddr * */
1401};
1402#endif
1403
wangweidong26ac8e52013-12-23 12:16:51 +08001404static int sctp_getsockopt_connectx3(struct sock *sk, int len,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001405 char __user *optval,
1406 int __user *optlen)
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001407{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001408 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001409 sctp_assoc_t assoc_id = 0;
1410 int err = 0;
1411
Daniel Borkmannffd59392014-02-17 12:11:11 +01001412#ifdef CONFIG_COMPAT
Andy Lutomirski96c0e0a2016-03-22 14:25:07 -07001413 if (in_compat_syscall()) {
Daniel Borkmannffd59392014-02-17 12:11:11 +01001414 struct compat_sctp_getaddrs_old param32;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001415
Daniel Borkmannffd59392014-02-17 12:11:11 +01001416 if (len < sizeof(param32))
1417 return -EINVAL;
1418 if (copy_from_user(&param32, optval, sizeof(param32)))
1419 return -EFAULT;
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001420
Daniel Borkmannffd59392014-02-17 12:11:11 +01001421 param.assoc_id = param32.assoc_id;
1422 param.addr_num = param32.addr_num;
1423 param.addrs = compat_ptr(param32.addrs);
1424 } else
1425#endif
1426 {
1427 if (len < sizeof(param))
1428 return -EINVAL;
1429 if (copy_from_user(&param, optval, sizeof(param)))
1430 return -EFAULT;
1431 }
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001432
Daniel Borkmannffd59392014-02-17 12:11:11 +01001433 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1434 param.addrs, param.addr_num,
1435 &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001436 if (err == 0 || err == -EINPROGRESS) {
1437 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1438 return -EFAULT;
1439 if (put_user(sizeof(assoc_id), optlen))
1440 return -EFAULT;
1441 }
1442
1443 return err;
1444}
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446/* API 3.1.4 close() - UDP Style Syntax
1447 * Applications use close() to perform graceful shutdown (as described in
1448 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1449 * by a UDP-style socket.
1450 *
1451 * The syntax is
1452 *
1453 * ret = close(int sd);
1454 *
1455 * sd - the socket descriptor of the associations to be closed.
1456 *
1457 * To gracefully shutdown a specific association represented by the
1458 * UDP-style socket, an application should use the sendmsg() call,
1459 * passing no user data, but including the appropriate flag in the
1460 * ancillary data (see Section xxxx).
1461 *
1462 * If sd in the close() call is a branched-off socket representing only
1463 * one association, the shutdown is performed on that association only.
1464 *
1465 * 4.1.6 close() - TCP Style Syntax
1466 *
1467 * Applications use close() to gracefully close down an association.
1468 *
1469 * The syntax is:
1470 *
1471 * int close(int sd);
1472 *
1473 * sd - the socket descriptor of the association to be closed.
1474 *
1475 * After an application calls close() on a socket descriptor, no further
1476 * socket operations will succeed on that descriptor.
1477 *
1478 * API 7.1.4 SO_LINGER
1479 *
1480 * An application using the TCP-style socket can use this option to
1481 * perform the SCTP ABORT primitive. The linger option structure is:
1482 *
1483 * struct linger {
1484 * int l_onoff; // option on/off
1485 * int l_linger; // linger time
1486 * };
1487 *
1488 * To enable the option, set l_onoff to 1. If the l_linger value is set
1489 * to 0, calling close() is the same as the ABORT primitive. If the
1490 * value is set to a negative value, the setsockopt() call will return
1491 * an error. If the value is set to a positive value linger_time, the
1492 * close() can be blocked for at most linger_time ms. If the graceful
1493 * shutdown phase does not finish during this period, close() will
1494 * return but the graceful shutdown phase continues in the system.
1495 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02001496static void sctp_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001498 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 struct sctp_endpoint *ep;
1500 struct sctp_association *asoc;
1501 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001502 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Daniel Borkmannbb333812013-06-28 19:49:40 +02001504 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Xin Long6dfe4b92017-06-10 14:56:56 +08001506 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 sk->sk_shutdown = SHUTDOWN_MASK;
Yafang Shaocbabf462017-12-20 11:12:54 +08001508 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 ep = sctp_sk(sk)->ep;
1511
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001512 /* Clean up any skbs sitting on the receive queue. */
1513 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1514 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1515
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001516 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 list_for_each_safe(pos, temp, &ep->asocs) {
1518 asoc = list_entry(pos, struct sctp_association, asocs);
1519
1520 if (sctp_style(sk, TCP)) {
1521 /* A closed association can still be in the list if
1522 * it belongs to a TCP-style listening socket that is
1523 * not yet accepted. If so, free it. If not, send an
1524 * ABORT or SHUTDOWN based on the linger options.
1525 */
1526 if (sctp_state(asoc, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001528 continue;
1529 }
1530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001532 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1533 !skb_queue_empty(&asoc->ulpq.reasm) ||
Xin Long13228232017-12-08 21:04:09 +08001534 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001535 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001536 struct sctp_chunk *chunk;
1537
1538 chunk = sctp_make_abort_user(asoc, NULL, 0);
Xin Long068d8bd2015-12-29 17:49:25 +08001539 sctp_primitive_ABORT(net, asoc, chunk);
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001540 } else
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001541 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /* On a TCP-style socket, block for at most linger_time if set. */
1545 if (sctp_style(sk, TCP) && timeout)
1546 sctp_wait_for_close(sk, timeout);
1547
1548 /* This will run the backlog queue. */
wangweidong048ed4b2014-01-21 15:44:11 +08001549 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 /* Supposedly, no process has access to the socket, but
1552 * the net layers still may.
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001553 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1554 * held and that should be grabbed before socket lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001556 spin_lock_bh(&net->sctp.addr_wq_lock);
Xin Long6dfe4b92017-06-10 14:56:56 +08001557 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 /* Hold the sock, since sk_common_release() will put sock_put()
1560 * and we have just a little more cleanup.
1561 */
1562 sock_hold(sk);
1563 sk_common_release(sk);
1564
wangweidong5bc1d1b2014-01-21 15:44:12 +08001565 bh_unlock_sock(sk);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001566 spin_unlock_bh(&net->sctp.addr_wq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 sock_put(sk);
1569
1570 SCTP_DBG_OBJCNT_DEC(sock);
1571}
1572
1573/* Handle EPIPE error. */
1574static int sctp_error(struct sock *sk, int flags, int err)
1575{
1576 if (err == -EPIPE)
1577 err = sock_error(sk) ? : -EPIPE;
1578 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1579 send_sig(SIGPIPE, current, 0);
1580 return err;
1581}
1582
1583/* API 3.1.3 sendmsg() - UDP Style Syntax
1584 *
1585 * An application uses sendmsg() and recvmsg() calls to transmit data to
1586 * and receive data from its peer.
1587 *
1588 * ssize_t sendmsg(int socket, const struct msghdr *message,
1589 * int flags);
1590 *
1591 * socket - the socket descriptor of the endpoint.
1592 * message - pointer to the msghdr structure which contains a single
1593 * user message and possibly some ancillary data.
1594 *
1595 * See Section 5 for complete description of the data
1596 * structures.
1597 *
1598 * flags - flags sent or received with the user message, see Section
1599 * 5 for complete description of the flags.
1600 *
1601 * Note: This function could use a rewrite especially when explicit
1602 * connect support comes in.
1603 */
1604/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1605
Xin Longa05437a2017-08-11 10:23:48 +08001606static int sctp_msghdr_parse(const struct msghdr *msg,
1607 struct sctp_cmsgs *cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Xin Long204f8172018-03-01 23:05:14 +08001609static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1610 struct sctp_sndrcvinfo *srinfo,
1611 const struct msghdr *msg, size_t msg_len)
1612{
1613 __u16 sflags;
1614 int err;
1615
1616 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1617 return -EPIPE;
1618
1619 if (msg_len > sk->sk_sndbuf)
1620 return -EMSGSIZE;
1621
1622 memset(cmsgs, 0, sizeof(*cmsgs));
1623 err = sctp_msghdr_parse(msg, cmsgs);
1624 if (err) {
1625 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1626 return err;
1627 }
1628
1629 memset(srinfo, 0, sizeof(*srinfo));
1630 if (cmsgs->srinfo) {
1631 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1632 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1633 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1634 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1635 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1636 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1637 }
1638
1639 if (cmsgs->sinfo) {
1640 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1641 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1642 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1643 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1644 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1645 }
1646
Xin Longed63afb2018-03-05 20:44:18 +08001647 if (cmsgs->prinfo) {
1648 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1649 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1650 cmsgs->prinfo->pr_policy);
1651 }
1652
Xin Long204f8172018-03-01 23:05:14 +08001653 sflags = srinfo->sinfo_flags;
1654 if (!sflags && msg_len)
1655 return 0;
1656
1657 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1658 return -EINVAL;
1659
1660 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1661 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1662 return -EINVAL;
1663
1664 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1665 return -EINVAL;
1666
1667 return 0;
1668}
1669
Xin Long2bfd80f2018-03-01 23:05:11 +08001670static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1671 struct sctp_cmsgs *cmsgs,
1672 union sctp_addr *daddr,
1673 struct sctp_transport **tp)
1674{
1675 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1676 struct net *net = sock_net(sk);
1677 struct sctp_association *asoc;
1678 enum sctp_scope scope;
1679 int err = -EINVAL;
1680
1681 *tp = NULL;
1682
1683 if (sflags & (SCTP_EOF | SCTP_ABORT))
1684 return -EINVAL;
1685
1686 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1687 sctp_sstate(sk, CLOSING)))
1688 return -EADDRNOTAVAIL;
1689
1690 if (sctp_endpoint_is_peeled_off(ep, daddr))
1691 return -EADDRNOTAVAIL;
1692
1693 if (!ep->base.bind_addr.port) {
1694 if (sctp_autobind(sk))
1695 return -EAGAIN;
1696 } else {
1697 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1698 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1699 return -EACCES;
1700 }
1701
1702 scope = sctp_scope(daddr);
1703
1704 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1705 if (!asoc)
1706 return -ENOMEM;
1707
1708 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1709 err = -ENOMEM;
1710 goto free;
1711 }
1712
1713 if (cmsgs->init) {
1714 struct sctp_initmsg *init = cmsgs->init;
1715
1716 if (init->sinit_num_ostreams) {
1717 __u16 outcnt = init->sinit_num_ostreams;
1718
1719 asoc->c.sinit_num_ostreams = outcnt;
1720 /* outcnt has been changed, need to re-init stream */
1721 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1722 GFP_KERNEL);
1723 if (err)
1724 goto free;
1725 }
1726
1727 if (init->sinit_max_instreams)
1728 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1729
1730 if (init->sinit_max_attempts)
1731 asoc->max_init_attempts = init->sinit_max_attempts;
1732
1733 if (init->sinit_max_init_timeo)
1734 asoc->max_init_timeo =
1735 msecs_to_jiffies(init->sinit_max_init_timeo);
1736 }
1737
1738 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1739 if (!*tp) {
1740 err = -ENOMEM;
1741 goto free;
1742 }
1743
1744 return 0;
1745
1746free:
1747 sctp_association_free(asoc);
1748 return err;
1749}
1750
Xin Longc2666de2018-03-01 23:05:12 +08001751static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1752 __u16 sflags, struct msghdr *msg,
1753 size_t msg_len)
1754{
1755 struct sock *sk = asoc->base.sk;
1756 struct net *net = sock_net(sk);
1757
1758 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1759 return -EPIPE;
1760
1761 if (sflags & SCTP_EOF) {
1762 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1763 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1764
1765 return 0;
1766 }
1767
1768 if (sflags & SCTP_ABORT) {
1769 struct sctp_chunk *chunk;
1770
1771 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1772 if (!chunk)
1773 return -ENOMEM;
1774
1775 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1776 sctp_primitive_ABORT(net, asoc, chunk);
1777
1778 return 0;
1779 }
1780
1781 return 1;
1782}
1783
Xin Longf84af332018-03-01 23:05:10 +08001784static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1785 struct msghdr *msg, size_t msg_len,
1786 struct sctp_transport *transport,
1787 struct sctp_sndrcvinfo *sinfo)
1788{
1789 struct sock *sk = asoc->base.sk;
1790 struct net *net = sock_net(sk);
1791 struct sctp_datamsg *datamsg;
1792 bool wait_connect = false;
1793 struct sctp_chunk *chunk;
1794 long timeo;
1795 int err;
1796
1797 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1798 err = -EINVAL;
1799 goto err;
1800 }
1801
1802 if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) {
1803 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1804 if (err)
1805 goto err;
1806 }
1807
1808 if (sctp_sk(sk)->disable_fragments && msg_len > asoc->frag_point) {
1809 err = -EMSGSIZE;
1810 goto err;
1811 }
1812
1813 if (sctp_state(asoc, CLOSED)) {
1814 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1815 if (err)
1816 goto err;
1817
1818 if (sctp_sk(sk)->strm_interleave) {
1819 timeo = sock_sndtimeo(sk, 0);
1820 err = sctp_wait_for_connect(asoc, &timeo);
1821 if (err)
1822 goto err;
1823 } else {
1824 wait_connect = true;
1825 }
1826
1827 pr_debug("%s: we associated primitively\n", __func__);
1828 }
1829
1830 if (asoc->pmtu_pending)
1831 sctp_assoc_pending_pmtu(asoc);
1832
1833 if (sctp_wspace(asoc) < msg_len)
1834 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1835
1836 if (!sctp_wspace(asoc)) {
1837 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1838 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1839 if (err)
1840 goto err;
1841 }
1842
1843 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1844 if (IS_ERR(datamsg)) {
1845 err = PTR_ERR(datamsg);
1846 goto err;
1847 }
1848
1849 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1850
1851 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1852 sctp_chunk_hold(chunk);
1853 sctp_set_owner_w(chunk);
1854 chunk->transport = transport;
1855 }
1856
1857 err = sctp_primitive_SEND(net, asoc, datamsg);
1858 if (err) {
1859 sctp_datamsg_free(datamsg);
1860 goto err;
1861 }
1862
1863 pr_debug("%s: we sent primitively\n", __func__);
1864
1865 sctp_datamsg_put(datamsg);
1866
1867 if (unlikely(wait_connect)) {
1868 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1869 sctp_wait_for_connect(asoc, &timeo);
1870 }
1871
1872 err = msg_len;
1873
1874err:
1875 return err;
1876}
1877
Xin Longbecef9b2018-03-01 23:05:13 +08001878static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1879 const struct msghdr *msg,
1880 struct sctp_cmsgs *cmsgs)
1881{
1882 union sctp_addr *daddr = NULL;
1883 int err;
1884
1885 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1886 int len = msg->msg_namelen;
1887
1888 if (len > sizeof(*daddr))
1889 len = sizeof(*daddr);
1890
1891 daddr = (union sctp_addr *)msg->msg_name;
1892
1893 err = sctp_verify_addr(sk, daddr, len);
1894 if (err)
1895 return ERR_PTR(err);
1896 }
1897
1898 return daddr;
1899}
1900
Xin Longd42cb062018-03-01 23:05:15 +08001901static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
1902 struct sctp_sndrcvinfo *sinfo,
1903 struct sctp_cmsgs *cmsgs)
1904{
1905 if (!cmsgs->srinfo && !cmsgs->sinfo) {
1906 sinfo->sinfo_stream = asoc->default_stream;
1907 sinfo->sinfo_ppid = asoc->default_ppid;
1908 sinfo->sinfo_context = asoc->default_context;
1909 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
Xin Longed63afb2018-03-05 20:44:18 +08001910
1911 if (!cmsgs->prinfo)
1912 sinfo->sinfo_flags = asoc->default_flags;
Xin Longd42cb062018-03-01 23:05:15 +08001913 }
1914
Xin Longed63afb2018-03-05 20:44:18 +08001915 if (!cmsgs->srinfo && !cmsgs->prinfo)
Xin Longd42cb062018-03-01 23:05:15 +08001916 sinfo->sinfo_timetolive = asoc->default_timetolive;
1917}
1918
Ying Xue1b784142015-03-02 15:37:48 +08001919static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Xin Long204f8172018-03-01 23:05:14 +08001921 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Xin Long8e87c6e2018-03-01 23:05:16 +08001922 struct sctp_transport *transport = NULL;
Xin Long204f8172018-03-01 23:05:14 +08001923 struct sctp_sndrcvinfo _sinfo, *sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08001924 struct sctp_association *asoc;
1925 struct sctp_cmsgs cmsgs;
Xin Longbecef9b2018-03-01 23:05:13 +08001926 union sctp_addr *daddr;
Xin Long007b7e12018-03-01 23:05:17 +08001927 bool new = false;
1928 __u16 sflags;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001929 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Xin Long204f8172018-03-01 23:05:14 +08001931 /* Parse and get snd_info */
1932 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
1933 if (err)
Xin Long007b7e12018-03-01 23:05:17 +08001934 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Xin Long204f8172018-03-01 23:05:14 +08001936 sinfo = &_sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08001937 sflags = sinfo->sinfo_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Xin Longbecef9b2018-03-01 23:05:13 +08001939 /* Get daddr from msg */
1940 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
1941 if (IS_ERR(daddr)) {
1942 err = PTR_ERR(daddr);
Xin Long007b7e12018-03-01 23:05:17 +08001943 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945
wangweidong048ed4b2014-01-21 15:44:11 +08001946 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Xin Long0a3920d2018-03-01 23:05:18 +08001948 /* Get and check or create asoc */
Xin Longbecef9b2018-03-01 23:05:13 +08001949 if (daddr) {
Xin Longbecef9b2018-03-01 23:05:13 +08001950 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
Xin Long0a3920d2018-03-01 23:05:18 +08001951 if (asoc) {
1952 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
1953 msg_len);
1954 if (err <= 0)
1955 goto out_unlock;
1956 } else {
1957 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
1958 &transport);
1959 if (err)
1960 goto out_unlock;
1961
1962 asoc = transport->asoc;
1963 new = true;
1964 }
1965
1966 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
1967 transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 } else {
Xin Long007b7e12018-03-01 23:05:17 +08001969 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (!asoc) {
1971 err = -EPIPE;
1972 goto out_unlock;
1973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Xin Long007b7e12018-03-01 23:05:17 +08001975 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
Xin Longc2666de2018-03-01 23:05:12 +08001976 if (err <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979
Xin Longd42cb062018-03-01 23:05:15 +08001980 /* Update snd_info with the asoc */
1981 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Xin Longf84af332018-03-01 23:05:10 +08001983 /* Send msg to the asoc */
Xin Long8e87c6e2018-03-01 23:05:16 +08001984 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
Xin Long007b7e12018-03-01 23:05:17 +08001985 if (err < 0 && err != -ESRCH && new)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 sctp_association_free(asoc);
Xin Long8e87c6e2018-03-01 23:05:16 +08001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988out_unlock:
wangweidong048ed4b2014-01-21 15:44:11 +08001989 release_sock(sk);
Xin Long007b7e12018-03-01 23:05:17 +08001990out:
Xin Longf84af332018-03-01 23:05:10 +08001991 return sctp_error(sk, msg->msg_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
1994/* This is an extended version of skb_pull() that removes the data from the
1995 * start of a skb even when data is spread across the list of skb's in the
1996 * frag_list. len specifies the total amount of data that needs to be removed.
1997 * when 'len' bytes could be removed from the skb, it returns 0.
1998 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1999 * could not be removed.
2000 */
2001static int sctp_skb_pull(struct sk_buff *skb, int len)
2002{
2003 struct sk_buff *list;
2004 int skb_len = skb_headlen(skb);
2005 int rlen;
2006
2007 if (len <= skb_len) {
2008 __skb_pull(skb, len);
2009 return 0;
2010 }
2011 len -= skb_len;
2012 __skb_pull(skb, skb_len);
2013
David S. Miller1b003be2009-06-09 00:22:35 -07002014 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 rlen = sctp_skb_pull(list, len);
2016 skb->len -= (len-rlen);
2017 skb->data_len -= (len-rlen);
2018
2019 if (!rlen)
2020 return 0;
2021
2022 len = rlen;
2023 }
2024
2025 return len;
2026}
2027
2028/* API 3.1.3 recvmsg() - UDP Style Syntax
2029 *
2030 * ssize_t recvmsg(int socket, struct msghdr *message,
2031 * int flags);
2032 *
2033 * socket - the socket descriptor of the endpoint.
2034 * message - pointer to the msghdr structure which contains a single
2035 * user message and possibly some ancillary data.
2036 *
2037 * See Section 5 for complete description of the data
2038 * structures.
2039 *
2040 * flags - flags sent or received with the user message, see Section
2041 * 5 for complete description of the flags.
2042 */
Ying Xue1b784142015-03-02 15:37:48 +08002043static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2044 int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 struct sctp_ulpevent *event = NULL;
2047 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002048 struct sk_buff *skb, *head_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 int copied;
2050 int err = 0;
2051 int skb_len;
2052
Daniel Borkmannbb333812013-06-28 19:49:40 +02002053 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2054 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2055 addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
wangweidong048ed4b2014-01-21 15:44:11 +08002057 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03002059 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
Xin Longe0878692016-07-30 14:14:41 +08002060 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 err = -ENOTCONN;
2062 goto out;
2063 }
2064
2065 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2066 if (!skb)
2067 goto out;
2068
2069 /* Get the total length of the skb including any skb's in the
2070 * frag_list.
2071 */
2072 skb_len = skb->len;
2073
2074 copied = skb_len;
2075 if (copied > len)
2076 copied = len;
2077
David S. Miller51f3d022014-11-05 16:46:40 -05002078 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 event = sctp_skb2event(skb);
2081
2082 if (err)
2083 goto out_free;
2084
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002085 if (event->chunk && event->chunk->head_skb)
2086 head_skb = event->chunk->head_skb;
2087 else
2088 head_skb = skb;
2089 sock_recv_ts_and_drops(msg, sk, head_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (sctp_ulpevent_is_notification(event)) {
2091 msg->msg_flags |= MSG_NOTIFICATION;
2092 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2093 } else {
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002094 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
2096
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02002097 /* Check if we allow SCTP_NXTINFO. */
2098 if (sp->recvnxtinfo)
2099 sctp_ulpevent_read_nxtinfo(event, msg, sk);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002100 /* Check if we allow SCTP_RCVINFO. */
2101 if (sp->recvrcvinfo)
2102 sctp_ulpevent_read_rcvinfo(event, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 /* Check if we allow SCTP_SNDRCVINFO. */
2104 if (sp->subscribe.sctp_data_io_event)
2105 sctp_ulpevent_read_sndrcvinfo(event, msg);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 err = copied;
2108
2109 /* If skb's length exceeds the user's buffer, update the skb and
2110 * push it back to the receive_queue so that the next call to
2111 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2112 */
2113 if (skb_len > copied) {
2114 msg->msg_flags &= ~MSG_EOR;
2115 if (flags & MSG_PEEK)
2116 goto out_free;
2117 sctp_skb_pull(skb, copied);
2118 skb_queue_head(&sk->sk_receive_queue, skb);
2119
Daniel Borkmann362d5202014-04-14 21:45:17 +02002120 /* When only partial message is copied to the user, increase
2121 * rwnd by that amount. If all the data in the skb is read,
2122 * rwnd is updated when the event is freed.
2123 */
2124 if (!sctp_ulpevent_is_notification(event))
2125 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 goto out;
2127 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2128 (event->msg_flags & MSG_EOR))
2129 msg->msg_flags |= MSG_EOR;
2130 else
2131 msg->msg_flags &= ~MSG_EOR;
2132
2133out_free:
2134 if (flags & MSG_PEEK) {
2135 /* Release the skb reference acquired after peeking the skb in
2136 * sctp_skb_recv_datagram().
2137 */
2138 kfree_skb(skb);
2139 } else {
2140 /* Free the event which includes releasing the reference to
2141 * the owner of the skb, freeing the skb and updating the
2142 * rwnd.
2143 */
2144 sctp_ulpevent_free(event);
2145 }
2146out:
wangweidong048ed4b2014-01-21 15:44:11 +08002147 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return err;
2149}
2150
2151/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2152 *
2153 * This option is a on/off flag. If enabled no SCTP message
2154 * fragmentation will be performed. Instead if a message being sent
2155 * exceeds the current PMTU size, the message will NOT be sent and
2156 * instead a error will be indicated to the user.
2157 */
2158static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002159 char __user *optval,
2160 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
2162 int val;
2163
2164 if (optlen < sizeof(int))
2165 return -EINVAL;
2166
2167 if (get_user(val, (int __user *)optval))
2168 return -EFAULT;
2169
2170 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2171
2172 return 0;
2173}
2174
2175static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002176 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Wei Yongjun94912302011-07-02 09:28:04 +00002178 struct sctp_association *asoc;
2179 struct sctp_ulpevent *event;
2180
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002181 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return -EINVAL;
2183 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2184 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002185
Daniel Borkmannbbbea412014-07-12 20:30:40 +02002186 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
Wei Yongjun94912302011-07-02 09:28:04 +00002187 * if there is no data to be sent or retransmit, the stack will
2188 * immediately send up this notification.
2189 */
2190 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2191 &sctp_sk(sk)->subscribe)) {
2192 asoc = sctp_id2assoc(sk, 0);
2193
2194 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2195 event = sctp_ulpevent_make_sender_dry_event(asoc,
Marcelo Ricardo Leitner2e83acb2018-01-08 19:02:27 -02002196 GFP_USER | __GFP_NOWARN);
Wei Yongjun94912302011-07-02 09:28:04 +00002197 if (!event)
2198 return -ENOMEM;
2199
Xin Long9162e0e2017-12-08 21:04:05 +08002200 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
Wei Yongjun94912302011-07-02 09:28:04 +00002201 }
2202 }
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 return 0;
2205}
2206
2207/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2208 *
2209 * This socket option is applicable to the UDP-style socket only. When
2210 * set it will cause associations that are idle for more than the
2211 * specified number of seconds to automatically close. An association
2212 * being idle is defined an association that has NOT sent or received
2213 * user data. The special value of '0' indicates that no automatic
2214 * close of any associations should be performed. The option expects an
2215 * integer defining the number of seconds of idle time before an
2216 * association is closed.
2217 */
2218static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002219 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
2221 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman9f70f462013-12-10 06:48:15 -05002222 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 /* Applicable to UDP-style socket only */
2225 if (sctp_style(sk, TCP))
2226 return -EOPNOTSUPP;
2227 if (optlen != sizeof(int))
2228 return -EINVAL;
2229 if (copy_from_user(&sp->autoclose, optval, optlen))
2230 return -EFAULT;
2231
Neil Horman9f70f462013-12-10 06:48:15 -05002232 if (sp->autoclose > net->sctp.max_autoclose)
2233 sp->autoclose = net->sctp.max_autoclose;
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return 0;
2236}
2237
2238/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2239 *
2240 * Applications can enable or disable heartbeats for any peer address of
2241 * an association, modify an address's heartbeat interval, force a
2242 * heartbeat to be sent immediately, and adjust the address's maximum
2243 * number of retransmissions sent before an address is considered
2244 * unreachable. The following structure is used to access and modify an
2245 * address's parameters:
2246 *
2247 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002248 * sctp_assoc_t spp_assoc_id;
2249 * struct sockaddr_storage spp_address;
2250 * uint32_t spp_hbinterval;
2251 * uint16_t spp_pathmaxrxt;
2252 * uint32_t spp_pathmtu;
2253 * uint32_t spp_sackdelay;
2254 * uint32_t spp_flags;
2255 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002257 * spp_assoc_id - (one-to-many style socket) This is filled in the
2258 * application, and identifies the association for
2259 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 * spp_address - This specifies which address is of interest.
2261 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002262 * in milliseconds. If a value of zero
2263 * is present in this field then no changes are to
2264 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 * spp_pathmaxrxt - This contains the maximum number of
2266 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002267 * considered unreachable. If a value of zero
2268 * is present in this field then no changes are to
2269 * be made to this parameter.
2270 * spp_pathmtu - When Path MTU discovery is disabled the value
2271 * specified here will be the "fixed" path mtu.
2272 * Note that if the spp_address field is empty
2273 * then all associations on this address will
2274 * have this fixed path mtu set upon them.
2275 *
2276 * spp_sackdelay - When delayed sack is enabled, this value specifies
2277 * the number of milliseconds that sacks will be delayed
2278 * for. This value will apply to all addresses of an
2279 * association if the spp_address field is empty. Note
2280 * also, that if delayed sack is enabled and this
2281 * value is set to 0, no change is made to the last
2282 * recorded delayed sack timer value.
2283 *
2284 * spp_flags - These flags are used to control various features
2285 * on an association. The flag field may contain
2286 * zero or more of the following options.
2287 *
2288 * SPP_HB_ENABLE - Enable heartbeats on the
2289 * specified address. Note that if the address
2290 * field is empty all addresses for the association
2291 * have heartbeats enabled upon them.
2292 *
2293 * SPP_HB_DISABLE - Disable heartbeats on the
2294 * speicifed address. Note that if the address
2295 * field is empty all addresses for the association
2296 * will have their heartbeats disabled. Note also
2297 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2298 * mutually exclusive, only one of these two should
2299 * be specified. Enabling both fields will have
2300 * undetermined results.
2301 *
2302 * SPP_HB_DEMAND - Request a user initiated heartbeat
2303 * to be made immediately.
2304 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002305 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2306 * heartbeat delayis to be set to the value of 0
2307 * milliseconds.
2308 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002309 * SPP_PMTUD_ENABLE - This field will enable PMTU
2310 * discovery upon the specified address. Note that
2311 * if the address feild is empty then all addresses
2312 * on the association are effected.
2313 *
2314 * SPP_PMTUD_DISABLE - This field will disable PMTU
2315 * discovery upon the specified address. Note that
2316 * if the address feild is empty then all addresses
2317 * on the association are effected. Not also that
2318 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2319 * exclusive. Enabling both will have undetermined
2320 * results.
2321 *
2322 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2323 * on delayed sack. The time specified in spp_sackdelay
2324 * is used to specify the sack delay for this address. Note
2325 * that if spp_address is empty then all addresses will
2326 * enable delayed sack and take on the sack delay
2327 * value specified in spp_sackdelay.
2328 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2329 * off delayed sack. If the spp_address field is blank then
2330 * delayed sack is disabled for the entire association. Note
2331 * also that this field is mutually exclusive to
2332 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2333 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 */
Adrian Bunk16164362006-09-18 00:40:38 -07002335static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2336 struct sctp_transport *trans,
2337 struct sctp_association *asoc,
2338 struct sctp_sock *sp,
2339 int hb_change,
2340 int pmtud_change,
2341 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 int error;
2344
Frank Filz52ccb8e2005-12-22 11:36:46 -08002345 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002346 struct net *net = sock_net(trans->asoc->base.sk);
2347
2348 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (error)
2350 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 }
2352
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002353 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2354 * this field is ignored. Note also that a value of zero indicates
2355 * the current setting should be left unchanged.
2356 */
2357 if (params->spp_flags & SPP_HB_ENABLE) {
2358
2359 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2360 * set. This lets us use 0 value when this flag
2361 * is set.
2362 */
2363 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2364 params->spp_hbinterval = 0;
2365
2366 if (params->spp_hbinterval ||
2367 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2368 if (trans) {
2369 trans->hbinterval =
2370 msecs_to_jiffies(params->spp_hbinterval);
2371 } else if (asoc) {
2372 asoc->hbinterval =
2373 msecs_to_jiffies(params->spp_hbinterval);
2374 } else {
2375 sp->hbinterval = params->spp_hbinterval;
2376 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002377 }
2378 }
2379
2380 if (hb_change) {
2381 if (trans) {
2382 trans->param_flags =
2383 (trans->param_flags & ~SPP_HB) | hb_change;
2384 } else if (asoc) {
2385 asoc->param_flags =
2386 (asoc->param_flags & ~SPP_HB) | hb_change;
2387 } else {
2388 sp->param_flags =
2389 (sp->param_flags & ~SPP_HB) | hb_change;
2390 }
2391 }
2392
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002393 /* When Path MTU discovery is disabled the value specified here will
2394 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2395 * include the flag SPP_PMTUD_DISABLE for this field to have any
2396 * effect).
2397 */
2398 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002399 if (trans) {
2400 trans->pathmtu = params->spp_pathmtu;
Xin Long3ebfdf02017-04-04 13:39:55 +08002401 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002402 } else if (asoc) {
2403 asoc->pathmtu = params->spp_pathmtu;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002404 } else {
2405 sp->pathmtu = params->spp_pathmtu;
2406 }
2407 }
2408
2409 if (pmtud_change) {
2410 if (trans) {
2411 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2412 (params->spp_flags & SPP_PMTUD_ENABLE);
2413 trans->param_flags =
2414 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2415 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002416 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
Xin Long3ebfdf02017-04-04 13:39:55 +08002417 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002418 }
2419 } else if (asoc) {
2420 asoc->param_flags =
2421 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2422 } else {
2423 sp->param_flags =
2424 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2425 }
2426 }
2427
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002428 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2429 * value of this field is ignored. Note also that a value of zero
2430 * indicates the current setting should be left unchanged.
2431 */
2432 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002433 if (trans) {
2434 trans->sackdelay =
2435 msecs_to_jiffies(params->spp_sackdelay);
2436 } else if (asoc) {
2437 asoc->sackdelay =
2438 msecs_to_jiffies(params->spp_sackdelay);
2439 } else {
2440 sp->sackdelay = params->spp_sackdelay;
2441 }
2442 }
2443
2444 if (sackdelay_change) {
2445 if (trans) {
2446 trans->param_flags =
2447 (trans->param_flags & ~SPP_SACKDELAY) |
2448 sackdelay_change;
2449 } else if (asoc) {
2450 asoc->param_flags =
2451 (asoc->param_flags & ~SPP_SACKDELAY) |
2452 sackdelay_change;
2453 } else {
2454 sp->param_flags =
2455 (sp->param_flags & ~SPP_SACKDELAY) |
2456 sackdelay_change;
2457 }
2458 }
2459
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002460 /* Note that a value of zero indicates the current setting should be
2461 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002462 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002463 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002464 if (trans) {
2465 trans->pathmaxrxt = params->spp_pathmaxrxt;
2466 } else if (asoc) {
2467 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2468 } else {
2469 sp->pathmaxrxt = params->spp_pathmaxrxt;
2470 }
2471 }
2472
2473 return 0;
2474}
2475
2476static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002477 char __user *optval,
2478 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002479{
2480 struct sctp_paddrparams params;
2481 struct sctp_transport *trans = NULL;
2482 struct sctp_association *asoc = NULL;
2483 struct sctp_sock *sp = sctp_sk(sk);
2484 int error;
2485 int hb_change, pmtud_change, sackdelay_change;
2486
2487 if (optlen != sizeof(struct sctp_paddrparams))
wangweidongcb3f8372013-12-23 12:16:50 +08002488 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002489
2490 if (copy_from_user(&params, optval, optlen))
2491 return -EFAULT;
2492
2493 /* Validate flags and value parameters. */
2494 hb_change = params.spp_flags & SPP_HB;
2495 pmtud_change = params.spp_flags & SPP_PMTUD;
2496 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2497
2498 if (hb_change == SPP_HB ||
2499 pmtud_change == SPP_PMTUD ||
2500 sackdelay_change == SPP_SACKDELAY ||
2501 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002502 (params.spp_pathmtu &&
2503 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002504 return -EINVAL;
2505
2506 /* If an address other than INADDR_ANY is specified, and
2507 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 */
wangweidongcb3f8372013-12-23 12:16:50 +08002509 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002510 trans = sctp_addr_id2transport(sk, &params.spp_address,
2511 params.spp_assoc_id);
2512 if (!trans)
2513 return -EINVAL;
2514 }
2515
2516 /* Get association, if assoc_id != 0 and the socket is a one
2517 * to many style socket, and an association was not found, then
2518 * the id was invalid.
2519 */
2520 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2521 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2522 return -EINVAL;
2523
2524 /* Heartbeat demand can only be sent on a transport or
2525 * association, but not a socket.
2526 */
2527 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2528 return -EINVAL;
2529
2530 /* Process parameters. */
2531 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2532 hb_change, pmtud_change,
2533 sackdelay_change);
2534
2535 if (error)
2536 return error;
2537
2538 /* If changes are for association, also apply parameters to each
2539 * transport.
2540 */
2541 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002542 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2543 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002544 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2545 hb_change, pmtud_change,
2546 sackdelay_change);
2547 }
2548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
2550 return 0;
2551}
2552
wangweidong0ea5e4d2014-01-15 17:24:01 +08002553static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2554{
2555 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2556}
2557
2558static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2559{
2560 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2561}
2562
Wei Yongjund364d922008-05-09 15:13:26 -07002563/*
2564 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002565 *
Wei Yongjund364d922008-05-09 15:13:26 -07002566 * This option will effect the way delayed acks are performed. This
2567 * option allows you to get or set the delayed ack time, in
2568 * milliseconds. It also allows changing the delayed ack frequency.
2569 * Changing the frequency to 1 disables the delayed sack algorithm. If
2570 * the assoc_id is 0, then this sets or gets the endpoints default
2571 * values. If the assoc_id field is non-zero, then the set or get
2572 * effects the specified association for the one to many model (the
2573 * assoc_id field is ignored by the one to one model). Note that if
2574 * sack_delay or sack_freq are 0 when setting this option, then the
2575 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002576 *
Wei Yongjund364d922008-05-09 15:13:26 -07002577 * struct sctp_sack_info {
2578 * sctp_assoc_t sack_assoc_id;
2579 * uint32_t sack_delay;
2580 * uint32_t sack_freq;
2581 * };
Frank Filz77086102005-12-22 11:37:30 -08002582 *
Wei Yongjund364d922008-05-09 15:13:26 -07002583 * sack_assoc_id - This parameter, indicates which association the user
2584 * is performing an action upon. Note that if this field's value is
2585 * zero then the endpoints default value is changed (effecting future
2586 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002587 *
Wei Yongjund364d922008-05-09 15:13:26 -07002588 * sack_delay - This parameter contains the number of milliseconds that
2589 * the user is requesting the delayed ACK timer be set to. Note that
2590 * this value is defined in the standard to be between 200 and 500
2591 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002592 *
Wei Yongjund364d922008-05-09 15:13:26 -07002593 * sack_freq - This parameter contains the number of packets that must
2594 * be received before a sack is sent without waiting for the delay
2595 * timer to expire. The default value for this is 2, setting this
2596 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002597 */
2598
Wei Yongjund364d922008-05-09 15:13:26 -07002599static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002600 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002601{
Wei Yongjund364d922008-05-09 15:13:26 -07002602 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002603 struct sctp_transport *trans = NULL;
2604 struct sctp_association *asoc = NULL;
2605 struct sctp_sock *sp = sctp_sk(sk);
2606
Wei Yongjund364d922008-05-09 15:13:26 -07002607 if (optlen == sizeof(struct sctp_sack_info)) {
2608 if (copy_from_user(&params, optval, optlen))
2609 return -EFAULT;
2610
2611 if (params.sack_delay == 0 && params.sack_freq == 0)
2612 return 0;
2613 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05002614 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05002615 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05002616 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05002617 "Use struct sctp_sack_info instead\n",
2618 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07002619 if (copy_from_user(&params, optval, optlen))
2620 return -EFAULT;
2621
2622 if (params.sack_delay == 0)
2623 params.sack_freq = 1;
2624 else
2625 params.sack_freq = 0;
2626 } else
wangweidongcb3f8372013-12-23 12:16:50 +08002627 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08002628
Frank Filz77086102005-12-22 11:37:30 -08002629 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002630 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002631 return -EINVAL;
2632
Wei Yongjund364d922008-05-09 15:13:26 -07002633 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002634 * to many style socket, and an association was not found, then
2635 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002636 */
Wei Yongjund364d922008-05-09 15:13:26 -07002637 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2638 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002639 return -EINVAL;
2640
Wei Yongjund364d922008-05-09 15:13:26 -07002641 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002642 if (asoc) {
2643 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002644 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002645 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002646 sctp_spp_sackdelay_enable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002647 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002648 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002649 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002650 sctp_spp_sackdelay_enable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002651 }
Wei Yongjund364d922008-05-09 15:13:26 -07002652 }
2653
2654 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002655 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002656 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002657 sctp_spp_sackdelay_disable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002658 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002659 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002660 sctp_spp_sackdelay_disable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002661 }
Wei Yongjund364d922008-05-09 15:13:26 -07002662 } else if (params.sack_freq > 1) {
2663 if (asoc) {
2664 asoc->sackfreq = params.sack_freq;
2665 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002666 sctp_spp_sackdelay_enable(asoc->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002667 } else {
2668 sp->sackfreq = params.sack_freq;
2669 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002670 sctp_spp_sackdelay_enable(sp->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002671 }
Frank Filz77086102005-12-22 11:37:30 -08002672 }
2673
2674 /* If change is for association, also apply to each transport. */
2675 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002676 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2677 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002678 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002679 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002680 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002681 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002682 sctp_spp_sackdelay_enable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002683 }
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07002684 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002685 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002686 sctp_spp_sackdelay_disable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002687 } else if (params.sack_freq > 1) {
2688 trans->sackfreq = params.sack_freq;
2689 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002690 sctp_spp_sackdelay_enable(trans->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002691 }
2692 }
2693 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002694
Frank Filz77086102005-12-22 11:37:30 -08002695 return 0;
2696}
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2699 *
2700 * Applications can specify protocol parameters for the default association
2701 * initialization. The option name argument to setsockopt() and getsockopt()
2702 * is SCTP_INITMSG.
2703 *
2704 * Setting initialization parameters is effective only on an unconnected
2705 * socket (for UDP-style sockets only future associations are effected
2706 * by the change). With TCP-style sockets, this option is inherited by
2707 * sockets derived from a listener socket.
2708 */
David S. Millerb7058842009-09-30 16:12:20 -07002709static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
2711 struct sctp_initmsg sinit;
2712 struct sctp_sock *sp = sctp_sk(sk);
2713
2714 if (optlen != sizeof(struct sctp_initmsg))
2715 return -EINVAL;
2716 if (copy_from_user(&sinit, optval, optlen))
2717 return -EFAULT;
2718
2719 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002720 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002722 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002724 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002726 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728 return 0;
2729}
2730
2731/*
2732 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2733 *
2734 * Applications that wish to use the sendto() system call may wish to
2735 * specify a default set of parameters that would normally be supplied
2736 * through the inclusion of ancillary data. This socket option allows
2737 * such an application to set the default sctp_sndrcvinfo structure.
2738 * The application that wishes to use this socket option simply passes
2739 * in to this call the sctp_sndrcvinfo structure defined in Section
2740 * 5.2.2) The input parameters accepted by this call include
2741 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2742 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2743 * to this call if the caller is using the UDP model.
2744 */
2745static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002746 char __user *optval,
2747 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002750 struct sctp_association *asoc;
2751 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002753 if (optlen != sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return -EINVAL;
2755 if (copy_from_user(&info, optval, optlen))
2756 return -EFAULT;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002757 if (info.sinfo_flags &
2758 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2759 SCTP_ABORT | SCTP_EOF))
2760 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
2762 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2763 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2764 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (asoc) {
2766 asoc->default_stream = info.sinfo_stream;
2767 asoc->default_flags = info.sinfo_flags;
2768 asoc->default_ppid = info.sinfo_ppid;
2769 asoc->default_context = info.sinfo_context;
2770 asoc->default_timetolive = info.sinfo_timetolive;
2771 } else {
2772 sp->default_stream = info.sinfo_stream;
2773 sp->default_flags = info.sinfo_flags;
2774 sp->default_ppid = info.sinfo_ppid;
2775 sp->default_context = info.sinfo_context;
2776 sp->default_timetolive = info.sinfo_timetolive;
2777 }
2778
2779 return 0;
2780}
2781
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002782/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2783 * (SCTP_DEFAULT_SNDINFO)
2784 */
2785static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2786 char __user *optval,
2787 unsigned int optlen)
2788{
2789 struct sctp_sock *sp = sctp_sk(sk);
2790 struct sctp_association *asoc;
2791 struct sctp_sndinfo info;
2792
2793 if (optlen != sizeof(info))
2794 return -EINVAL;
2795 if (copy_from_user(&info, optval, optlen))
2796 return -EFAULT;
2797 if (info.snd_flags &
2798 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2799 SCTP_ABORT | SCTP_EOF))
2800 return -EINVAL;
2801
2802 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
2803 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
2804 return -EINVAL;
2805 if (asoc) {
2806 asoc->default_stream = info.snd_sid;
2807 asoc->default_flags = info.snd_flags;
2808 asoc->default_ppid = info.snd_ppid;
2809 asoc->default_context = info.snd_context;
2810 } else {
2811 sp->default_stream = info.snd_sid;
2812 sp->default_flags = info.snd_flags;
2813 sp->default_ppid = info.snd_ppid;
2814 sp->default_context = info.snd_context;
2815 }
2816
2817 return 0;
2818}
2819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2821 *
2822 * Requests that the local SCTP stack use the enclosed peer address as
2823 * the association primary. The enclosed address must be one of the
2824 * association peer's addresses.
2825 */
2826static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002827 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
2829 struct sctp_prim prim;
2830 struct sctp_transport *trans;
2831
2832 if (optlen != sizeof(struct sctp_prim))
2833 return -EINVAL;
2834
2835 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2836 return -EFAULT;
2837
2838 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2839 if (!trans)
2840 return -EINVAL;
2841
2842 sctp_assoc_set_primary(trans->asoc, trans);
2843
2844 return 0;
2845}
2846
2847/*
2848 * 7.1.5 SCTP_NODELAY
2849 *
2850 * Turn on/off any Nagle-like algorithm. This means that packets are
2851 * generally sent as soon as possible and no unnecessary delays are
2852 * introduced, at the cost of more packets in the network. Expects an
2853 * integer boolean flag.
2854 */
2855static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002856 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
2858 int val;
2859
2860 if (optlen < sizeof(int))
2861 return -EINVAL;
2862 if (get_user(val, (int __user *)optval))
2863 return -EFAULT;
2864
2865 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2866 return 0;
2867}
2868
2869/*
2870 *
2871 * 7.1.1 SCTP_RTOINFO
2872 *
2873 * The protocol parameters used to initialize and bound retransmission
2874 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2875 * and modify these parameters.
2876 * All parameters are time values, in milliseconds. A value of 0, when
2877 * modifying the parameters, indicates that the current value should not
2878 * be changed.
2879 *
2880 */
David S. Millerb7058842009-09-30 16:12:20 -07002881static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2882{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 struct sctp_rtoinfo rtoinfo;
2884 struct sctp_association *asoc;
wangweidong85f935d2013-12-11 09:50:38 +08002885 unsigned long rto_min, rto_max;
2886 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
2888 if (optlen != sizeof (struct sctp_rtoinfo))
2889 return -EINVAL;
2890
2891 if (copy_from_user(&rtoinfo, optval, optlen))
2892 return -EFAULT;
2893
2894 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2895
2896 /* Set the values to the specific association */
2897 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2898 return -EINVAL;
2899
wangweidong85f935d2013-12-11 09:50:38 +08002900 rto_max = rtoinfo.srto_max;
2901 rto_min = rtoinfo.srto_min;
2902
2903 if (rto_max)
2904 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
2905 else
2906 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
2907
2908 if (rto_min)
2909 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
2910 else
2911 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
2912
2913 if (rto_min > rto_max)
2914 return -EINVAL;
2915
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (asoc) {
2917 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002918 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 msecs_to_jiffies(rtoinfo.srto_initial);
wangweidong85f935d2013-12-11 09:50:38 +08002920 asoc->rto_max = rto_max;
2921 asoc->rto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 } else {
2923 /* If there is no association or the association-id = 0
2924 * set the values to the endpoint.
2925 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 if (rtoinfo.srto_initial != 0)
2927 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
wangweidong85f935d2013-12-11 09:50:38 +08002928 sp->rtoinfo.srto_max = rto_max;
2929 sp->rtoinfo.srto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 }
2931
2932 return 0;
2933}
2934
2935/*
2936 *
2937 * 7.1.2 SCTP_ASSOCINFO
2938 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02002939 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 * of the association.
2941 * Returns an error if the new association retransmission value is
2942 * greater than the sum of the retransmission value of the peer.
2943 * See [SCTP] for more information.
2944 *
2945 */
David S. Millerb7058842009-09-30 16:12:20 -07002946static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
2948
2949 struct sctp_assocparams assocparams;
2950 struct sctp_association *asoc;
2951
2952 if (optlen != sizeof(struct sctp_assocparams))
2953 return -EINVAL;
2954 if (copy_from_user(&assocparams, optval, optlen))
2955 return -EFAULT;
2956
2957 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2958
2959 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2960 return -EINVAL;
2961
2962 /* Set the values to the specific association */
2963 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002964 if (assocparams.sasoc_asocmaxrxt != 0) {
2965 __u32 path_sum = 0;
2966 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002967 struct sctp_transport *peer_addr;
2968
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002969 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
2970 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002971 path_sum += peer_addr->pathmaxrxt;
2972 paths++;
2973 }
2974
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002975 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002976 * one path/transport. We do this because path
2977 * retransmissions are only counted when we have more
2978 * then one path.
2979 */
2980 if (paths > 1 &&
2981 assocparams.sasoc_asocmaxrxt > path_sum)
2982 return -EINVAL;
2983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002985 }
2986
Daniel Borkmann52db8822013-06-25 18:17:27 +02002987 if (assocparams.sasoc_cookie_life != 0)
2988 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 } else {
2990 /* Set the values to the endpoint */
2991 struct sctp_sock *sp = sctp_sk(sk);
2992
2993 if (assocparams.sasoc_asocmaxrxt != 0)
2994 sp->assocparams.sasoc_asocmaxrxt =
2995 assocparams.sasoc_asocmaxrxt;
2996 if (assocparams.sasoc_cookie_life != 0)
2997 sp->assocparams.sasoc_cookie_life =
2998 assocparams.sasoc_cookie_life;
2999 }
3000 return 0;
3001}
3002
3003/*
3004 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3005 *
3006 * This socket option is a boolean flag which turns on or off mapped V4
3007 * addresses. If this option is turned on and the socket is type
3008 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3009 * If this option is turned off, then no mapping will be done of V4
3010 * addresses and a user will receive both PF_INET6 and PF_INET type
3011 * addresses on the socket.
3012 */
David S. Millerb7058842009-09-30 16:12:20 -07003013static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
3015 int val;
3016 struct sctp_sock *sp = sctp_sk(sk);
3017
3018 if (optlen < sizeof(int))
3019 return -EINVAL;
3020 if (get_user(val, (int __user *)optval))
3021 return -EFAULT;
3022 if (val)
3023 sp->v4mapped = 1;
3024 else
3025 sp->v4mapped = 0;
3026
3027 return 0;
3028}
3029
3030/*
Wei Yongjune89c2092008-12-25 16:54:58 -08003031 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3032 * This option will get or set the maximum size to put in any outgoing
3033 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 * fragmented by SCTP into the specified size. Note that the underlying
3035 * SCTP implementation may fragment into smaller sized chunks when the
3036 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08003037 * the user. The default value for this option is '0' which indicates
3038 * the user is NOT limiting fragmentation and only the PMTU will effect
3039 * SCTP's choice of DATA chunk size. Note also that values set larger
3040 * than the maximum size of an IP datagram will effectively let SCTP
3041 * control fragmentation (i.e. the same as setting this option to 0).
3042 *
3043 * The following structure is used to access and modify this parameter:
3044 *
3045 * struct sctp_assoc_value {
3046 * sctp_assoc_t assoc_id;
3047 * uint32_t assoc_value;
3048 * };
3049 *
3050 * assoc_id: This parameter is ignored for one-to-one style sockets.
3051 * For one-to-many style sockets this parameter indicates which
3052 * association the user is performing an action upon. Note that if
3053 * this field's value is zero then the endpoints default value is
3054 * changed (effecting future associations only).
3055 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 */
David S. Millerb7058842009-09-30 16:12:20 -07003057static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
Xin Longecca8f82017-11-17 14:11:11 +08003059 struct sctp_sock *sp = sctp_sk(sk);
Wei Yongjune89c2092008-12-25 16:54:58 -08003060 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 int val;
3063
Wei Yongjune89c2092008-12-25 16:54:58 -08003064 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003065 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003066 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003067 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003068 "Use struct sctp_assoc_value instead\n",
3069 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08003070 if (copy_from_user(&val, optval, optlen))
3071 return -EFAULT;
3072 params.assoc_id = 0;
3073 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3074 if (copy_from_user(&params, optval, optlen))
3075 return -EFAULT;
3076 val = params.assoc_value;
Xin Longecca8f82017-11-17 14:11:11 +08003077 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 return -EINVAL;
Xin Longecca8f82017-11-17 14:11:11 +08003079 }
Wei Yongjune89c2092008-12-25 16:54:58 -08003080
Xin Longecca8f82017-11-17 14:11:11 +08003081 if (val) {
3082 int min_len, max_len;
3083
3084 min_len = SCTP_DEFAULT_MINSEGMENT - sp->pf->af->net_header_len;
3085 min_len -= sizeof(struct sctphdr) +
3086 sizeof(struct sctp_data_chunk);
3087
3088 max_len = SCTP_MAX_CHUNK_LEN - sizeof(struct sctp_data_chunk);
3089
3090 if (val < min_len || val > max_len)
3091 return -EINVAL;
3092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Wei Yongjune89c2092008-12-25 16:54:58 -08003094 asoc = sctp_id2assoc(sk, params.assoc_id);
Wei Yongjune89c2092008-12-25 16:54:58 -08003095 if (asoc) {
3096 if (val == 0) {
Xin Longecca8f82017-11-17 14:11:11 +08003097 val = asoc->pathmtu - sp->pf->af->net_header_len;
Wei Yongjune89c2092008-12-25 16:54:58 -08003098 val -= sizeof(struct sctphdr) +
Xin Long668c9be2017-12-08 21:04:02 +08003099 sctp_datachk_len(&asoc->stream);
Wei Yongjune89c2092008-12-25 16:54:58 -08003100 }
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003101 asoc->user_frag = val;
3102 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
Wei Yongjune89c2092008-12-25 16:54:58 -08003103 } else {
Xin Longecca8f82017-11-17 14:11:11 +08003104 if (params.assoc_id && sctp_style(sk, UDP))
3105 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003106 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 }
3108
3109 return 0;
3110}
3111
3112
3113/*
3114 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3115 *
3116 * Requests that the peer mark the enclosed address as the association
3117 * primary. The enclosed address must be one of the association's
3118 * locally bound addresses. The following structure is used to make a
3119 * set primary request:
3120 */
3121static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003122 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003124 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 struct sctp_association *asoc = NULL;
3127 struct sctp_setpeerprim prim;
3128 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003129 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 int err;
3131
3132 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003134 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 return -EPERM;
3136
3137 if (optlen != sizeof(struct sctp_setpeerprim))
3138 return -EINVAL;
3139
3140 if (copy_from_user(&prim, optval, optlen))
3141 return -EFAULT;
3142
3143 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003144 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 return -EINVAL;
3146
3147 if (!asoc->peer.asconf_capable)
3148 return -EPERM;
3149
3150 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3151 return -EPERM;
3152
3153 if (!sctp_state(asoc, ESTABLISHED))
3154 return -ENOTCONN;
3155
Wei Yongjun40a01032010-12-07 17:11:09 +00003156 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3157 if (!af)
3158 return -EINVAL;
3159
3160 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3161 return -EADDRNOTAVAIL;
3162
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3164 return -EADDRNOTAVAIL;
3165
3166 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3167 chunk = sctp_make_asconf_set_prim(asoc,
3168 (union sctp_addr *)&prim.sspp_addr);
3169 if (!chunk)
3170 return -ENOMEM;
3171
3172 err = sctp_send_asconf(asoc, chunk);
3173
Daniel Borkmannbb333812013-06-28 19:49:40 +02003174 pr_debug("%s: we set peer primary addr primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
3176 return err;
3177}
3178
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003179static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003180 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003182 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003184 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003186 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 return -EFAULT;
3188
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003189 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
3191 return 0;
3192}
3193
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003194/*
3195 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3196 *
3197 * The context field in the sctp_sndrcvinfo structure is normally only
3198 * used when a failed message is retrieved holding the value that was
3199 * sent down on the actual send call. This option allows the setting of
3200 * a default context on an association basis that will be received on
3201 * reading messages from the peer. This is especially helpful in the
3202 * one-2-many model for an application to keep some reference to an
3203 * internal state machine that is processing messages on the
3204 * association. Note that the setting of this value only effects
3205 * received messages from the peer and does not effect the value that is
3206 * saved with outbound messages.
3207 */
3208static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003209 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003210{
3211 struct sctp_assoc_value params;
3212 struct sctp_sock *sp;
3213 struct sctp_association *asoc;
3214
3215 if (optlen != sizeof(struct sctp_assoc_value))
3216 return -EINVAL;
3217 if (copy_from_user(&params, optval, optlen))
3218 return -EFAULT;
3219
3220 sp = sctp_sk(sk);
3221
3222 if (params.assoc_id != 0) {
3223 asoc = sctp_id2assoc(sk, params.assoc_id);
3224 if (!asoc)
3225 return -EINVAL;
3226 asoc->default_rcv_context = params.assoc_value;
3227 } else {
3228 sp->default_rcv_context = params.assoc_value;
3229 }
3230
3231 return 0;
3232}
3233
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003234/*
3235 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3236 *
3237 * This options will at a minimum specify if the implementation is doing
3238 * fragmented interleave. Fragmented interleave, for a one to many
3239 * socket, is when subsequent calls to receive a message may return
3240 * parts of messages from different associations. Some implementations
3241 * may allow you to turn this value on or off. If so, when turned off,
3242 * no fragment interleave will occur (which will cause a head of line
3243 * blocking amongst multiple associations sharing the same one to many
3244 * socket). When this option is turned on, then each receive call may
3245 * come from a different association (thus the user must receive data
3246 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3247 * association each receive belongs to.
3248 *
3249 * This option takes a boolean value. A non-zero value indicates that
3250 * fragmented interleave is on. A value of zero indicates that
3251 * fragmented interleave is off.
3252 *
3253 * Note that it is important that an implementation that allows this
3254 * option to be turned on, have it off by default. Otherwise an unaware
3255 * application using the one to many model may become confused and act
3256 * incorrectly.
3257 */
3258static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3259 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003260 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003261{
3262 int val;
3263
3264 if (optlen != sizeof(int))
3265 return -EINVAL;
3266 if (get_user(val, (int __user *)optval))
3267 return -EFAULT;
3268
Xin Long772a5862017-12-08 21:03:58 +08003269 sctp_sk(sk)->frag_interleave = !!val;
3270
3271 if (!sctp_sk(sk)->frag_interleave)
3272 sctp_sk(sk)->strm_interleave = 0;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003273
3274 return 0;
3275}
3276
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003277/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003278 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003279 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003280 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003281 * This option will set or get the SCTP partial delivery point. This
3282 * point is the size of a message where the partial delivery API will be
3283 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003284 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003285 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003286 * point. Note also that the call will fail if the user attempts to set
3287 * this value larger than the socket receive buffer size.
3288 *
3289 * Note that any single message having a length smaller than or equal to
3290 * the SCTP partial delivery point will be delivered in one single read
3291 * call as long as the user provided buffer is large enough to hold the
3292 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003293 */
3294static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3295 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003296 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003297{
3298 u32 val;
3299
3300 if (optlen != sizeof(u32))
3301 return -EINVAL;
3302 if (get_user(val, (int __user *)optval))
3303 return -EFAULT;
3304
Wei Yongjun8510b932008-12-25 16:59:03 -08003305 /* Note: We double the receive buffer from what the user sets
3306 * it to be, also initial rwnd is based on rcvbuf/2.
3307 */
3308 if (val > (sk->sk_rcvbuf >> 1))
3309 return -EINVAL;
3310
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003311 sctp_sk(sk)->pd_point = val;
3312
3313 return 0; /* is this the right error code? */
3314}
3315
Vlad Yasevich70331572007-03-23 11:34:36 -07003316/*
3317 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3318 *
3319 * This option will allow a user to change the maximum burst of packets
3320 * that can be emitted by this association. Note that the default value
3321 * is 4, and some implementations may restrict this setting so that it
3322 * can only be lowered.
3323 *
3324 * NOTE: This text doesn't seem right. Do this on a socket basis with
3325 * future associations inheriting the socket value.
3326 */
3327static int sctp_setsockopt_maxburst(struct sock *sk,
3328 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003329 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003330{
Neil Horman219b99a2008-03-05 13:44:46 -08003331 struct sctp_assoc_value params;
3332 struct sctp_sock *sp;
3333 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003334 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003335 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003336
Neil Horman219b99a2008-03-05 13:44:46 -08003337 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003338 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003339 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003340 "Use of int in max_burst socket option deprecated.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003341 "Use struct sctp_assoc_value instead\n",
3342 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08003343 if (copy_from_user(&val, optval, optlen))
3344 return -EFAULT;
3345 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3346 if (copy_from_user(&params, optval, optlen))
3347 return -EFAULT;
3348 val = params.assoc_value;
3349 assoc_id = params.assoc_id;
3350 } else
3351 return -EINVAL;
3352
3353 sp = sctp_sk(sk);
3354
3355 if (assoc_id != 0) {
3356 asoc = sctp_id2assoc(sk, assoc_id);
3357 if (!asoc)
3358 return -EINVAL;
3359 asoc->max_burst = val;
3360 } else
3361 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003362
3363 return 0;
3364}
3365
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003366/*
3367 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3368 *
3369 * This set option adds a chunk type that the user is requesting to be
3370 * received only in an authenticated way. Changes to the list of chunks
3371 * will only effect future associations on the socket.
3372 */
3373static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003374 char __user *optval,
3375 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003376{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003377 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003378 struct sctp_authchunk val;
3379
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003380 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003381 return -EACCES;
3382
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003383 if (optlen != sizeof(struct sctp_authchunk))
3384 return -EINVAL;
3385 if (copy_from_user(&val, optval, optlen))
3386 return -EFAULT;
3387
3388 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003389 case SCTP_CID_INIT:
3390 case SCTP_CID_INIT_ACK:
3391 case SCTP_CID_SHUTDOWN_COMPLETE:
3392 case SCTP_CID_AUTH:
3393 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003394 }
3395
3396 /* add this chunk id to the endpoint */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003397 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003398}
3399
3400/*
3401 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3402 *
3403 * This option gets or sets the list of HMAC algorithms that the local
3404 * endpoint requires the peer to use.
3405 */
3406static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003407 char __user *optval,
3408 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003409{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003410 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003411 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003412 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003413 int err;
3414
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003415 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003416 return -EACCES;
3417
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003418 if (optlen < sizeof(struct sctp_hmacalgo))
3419 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003420 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3421 SCTP_AUTH_NUM_HMACS * sizeof(u16));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003422
wangweidongcb3f8372013-12-23 12:16:50 +08003423 hmacs = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003424 if (IS_ERR(hmacs))
3425 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003426
Vlad Yasevichd9724052008-08-27 16:09:49 -07003427 idents = hmacs->shmac_num_idents;
3428 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3429 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003430 err = -EINVAL;
3431 goto out;
3432 }
3433
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003434 err = sctp_auth_ep_set_hmacs(ep, hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003435out:
3436 kfree(hmacs);
3437 return err;
3438}
3439
3440/*
3441 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3442 *
3443 * This option will set a shared secret key which is used to build an
3444 * association shared key.
3445 */
3446static int sctp_setsockopt_auth_key(struct sock *sk,
3447 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003448 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003449{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003450 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003451 struct sctp_authkey *authkey;
3452 struct sctp_association *asoc;
3453 int ret;
3454
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003455 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003456 return -EACCES;
3457
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003458 if (optlen <= sizeof(struct sctp_authkey))
3459 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003460 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3461 * this.
3462 */
3463 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3464 sizeof(struct sctp_authkey));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003465
wangweidongcb3f8372013-12-23 12:16:50 +08003466 authkey = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003467 if (IS_ERR(authkey))
3468 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003469
Vlad Yasevich328fc472008-08-27 16:08:54 -07003470 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003471 ret = -EINVAL;
3472 goto out;
3473 }
3474
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003475 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3476 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3477 ret = -EINVAL;
3478 goto out;
3479 }
3480
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003481 ret = sctp_auth_set_key(ep, asoc, authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003482out:
Daniel Borkmann6ba542a2013-02-08 03:04:34 +00003483 kzfree(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003484 return ret;
3485}
3486
3487/*
3488 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3489 *
3490 * This option will get or set the active shared key to be used to build
3491 * the association shared key.
3492 */
3493static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003494 char __user *optval,
3495 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003496{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003497 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003498 struct sctp_authkeyid val;
3499 struct sctp_association *asoc;
3500
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003501 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003502 return -EACCES;
3503
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003504 if (optlen != sizeof(struct sctp_authkeyid))
3505 return -EINVAL;
3506 if (copy_from_user(&val, optval, optlen))
3507 return -EFAULT;
3508
3509 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3510 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3511 return -EINVAL;
3512
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003513 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003514}
3515
3516/*
3517 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3518 *
3519 * This set option will delete a shared secret key from use.
3520 */
3521static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003522 char __user *optval,
3523 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003524{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003525 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003526 struct sctp_authkeyid val;
3527 struct sctp_association *asoc;
3528
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003529 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003530 return -EACCES;
3531
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003532 if (optlen != sizeof(struct sctp_authkeyid))
3533 return -EINVAL;
3534 if (copy_from_user(&val, optval, optlen))
3535 return -EFAULT;
3536
3537 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3538 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3539 return -EINVAL;
3540
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003541 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003542
3543}
3544
Michio Honda7dc04d72011-04-26 20:16:31 +09003545/*
3546 * 8.1.23 SCTP_AUTO_ASCONF
3547 *
3548 * This option will enable or disable the use of the automatic generation of
3549 * ASCONF chunks to add and delete addresses to an existing association. Note
3550 * that this option has two caveats namely: a) it only affects sockets that
3551 * are bound to all addresses available to the SCTP stack, and b) the system
3552 * administrator may have an overriding control that turns the ASCONF feature
3553 * off no matter what setting the socket option may have.
3554 * This option expects an integer boolean flag, where a non-zero value turns on
3555 * the option, and a zero value turns off the option.
3556 * Note. In this implementation, socket operation overrides default parameter
3557 * being set by sysctl as well as FreeBSD implementation
3558 */
3559static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3560 unsigned int optlen)
3561{
3562 int val;
3563 struct sctp_sock *sp = sctp_sk(sk);
3564
3565 if (optlen < sizeof(int))
3566 return -EINVAL;
3567 if (get_user(val, (int __user *)optval))
3568 return -EFAULT;
3569 if (!sctp_is_ep_boundall(sk) && val)
3570 return -EINVAL;
3571 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3572 return 0;
3573
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003574 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003575 if (val == 0 && sp->do_auto_asconf) {
3576 list_del(&sp->auto_asconf_list);
3577 sp->do_auto_asconf = 0;
3578 } else if (val && !sp->do_auto_asconf) {
3579 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00003580 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09003581 sp->do_auto_asconf = 1;
3582 }
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003583 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003584 return 0;
3585}
3586
Neil Horman5aa93bc2012-07-21 07:56:07 +00003587/*
3588 * SCTP_PEER_ADDR_THLDS
3589 *
3590 * This option allows us to alter the partially failed threshold for one or all
3591 * transports in an association. See Section 6.1 of:
3592 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3593 */
3594static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3595 char __user *optval,
3596 unsigned int optlen)
3597{
3598 struct sctp_paddrthlds val;
3599 struct sctp_transport *trans;
3600 struct sctp_association *asoc;
3601
3602 if (optlen < sizeof(struct sctp_paddrthlds))
3603 return -EINVAL;
3604 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3605 sizeof(struct sctp_paddrthlds)))
3606 return -EFAULT;
3607
3608
3609 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3610 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3611 if (!asoc)
3612 return -ENOENT;
3613 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3614 transports) {
3615 if (val.spt_pathmaxrxt)
3616 trans->pathmaxrxt = val.spt_pathmaxrxt;
3617 trans->pf_retrans = val.spt_pathpfthld;
3618 }
3619
3620 if (val.spt_pathmaxrxt)
3621 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3622 asoc->pf_retrans = val.spt_pathpfthld;
3623 } else {
3624 trans = sctp_addr_id2transport(sk, &val.spt_address,
3625 val.spt_assoc_id);
3626 if (!trans)
3627 return -ENOENT;
3628
3629 if (val.spt_pathmaxrxt)
3630 trans->pathmaxrxt = val.spt_pathmaxrxt;
3631 trans->pf_retrans = val.spt_pathpfthld;
3632 }
3633
3634 return 0;
3635}
3636
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02003637static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3638 char __user *optval,
3639 unsigned int optlen)
3640{
3641 int val;
3642
3643 if (optlen < sizeof(int))
3644 return -EINVAL;
3645 if (get_user(val, (int __user *) optval))
3646 return -EFAULT;
3647
3648 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3649
3650 return 0;
3651}
3652
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02003653static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3654 char __user *optval,
3655 unsigned int optlen)
3656{
3657 int val;
3658
3659 if (optlen < sizeof(int))
3660 return -EINVAL;
3661 if (get_user(val, (int __user *) optval))
3662 return -EFAULT;
3663
3664 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3665
3666 return 0;
3667}
3668
Xin Long28aa4c22016-07-09 19:47:40 +08003669static int sctp_setsockopt_pr_supported(struct sock *sk,
3670 char __user *optval,
3671 unsigned int optlen)
3672{
3673 struct sctp_assoc_value params;
3674 struct sctp_association *asoc;
3675 int retval = -EINVAL;
3676
3677 if (optlen != sizeof(params))
3678 goto out;
3679
3680 if (copy_from_user(&params, optval, optlen)) {
3681 retval = -EFAULT;
3682 goto out;
3683 }
3684
3685 asoc = sctp_id2assoc(sk, params.assoc_id);
3686 if (asoc) {
3687 asoc->prsctp_enable = !!params.assoc_value;
3688 } else if (!params.assoc_id) {
3689 struct sctp_sock *sp = sctp_sk(sk);
3690
3691 sp->ep->prsctp_enable = !!params.assoc_value;
3692 } else {
3693 goto out;
3694 }
3695
3696 retval = 0;
3697
3698out:
3699 return retval;
3700}
3701
Xin Longf959fb42016-07-09 19:47:41 +08003702static int sctp_setsockopt_default_prinfo(struct sock *sk,
3703 char __user *optval,
3704 unsigned int optlen)
3705{
3706 struct sctp_default_prinfo info;
3707 struct sctp_association *asoc;
3708 int retval = -EINVAL;
3709
3710 if (optlen != sizeof(info))
3711 goto out;
3712
3713 if (copy_from_user(&info, optval, sizeof(info))) {
3714 retval = -EFAULT;
3715 goto out;
3716 }
3717
3718 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
3719 goto out;
3720
3721 if (info.pr_policy == SCTP_PR_SCTP_NONE)
3722 info.pr_value = 0;
3723
3724 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
3725 if (asoc) {
3726 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
3727 asoc->default_timetolive = info.pr_value;
3728 } else if (!info.pr_assoc_id) {
3729 struct sctp_sock *sp = sctp_sk(sk);
3730
3731 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
3732 sp->default_timetolive = info.pr_value;
3733 } else {
3734 goto out;
3735 }
3736
3737 retval = 0;
3738
3739out:
3740 return retval;
3741}
3742
Xin Longc0d8bab2017-03-10 12:11:12 +08003743static int sctp_setsockopt_reconfig_supported(struct sock *sk,
3744 char __user *optval,
3745 unsigned int optlen)
3746{
3747 struct sctp_assoc_value params;
3748 struct sctp_association *asoc;
3749 int retval = -EINVAL;
3750
3751 if (optlen != sizeof(params))
3752 goto out;
3753
3754 if (copy_from_user(&params, optval, optlen)) {
3755 retval = -EFAULT;
3756 goto out;
3757 }
3758
3759 asoc = sctp_id2assoc(sk, params.assoc_id);
3760 if (asoc) {
3761 asoc->reconf_enable = !!params.assoc_value;
3762 } else if (!params.assoc_id) {
3763 struct sctp_sock *sp = sctp_sk(sk);
3764
3765 sp->ep->reconf_enable = !!params.assoc_value;
3766 } else {
3767 goto out;
3768 }
3769
3770 retval = 0;
3771
3772out:
3773 return retval;
3774}
3775
Xin Long9fb657a2017-01-18 00:44:46 +08003776static int sctp_setsockopt_enable_strreset(struct sock *sk,
3777 char __user *optval,
3778 unsigned int optlen)
3779{
3780 struct sctp_assoc_value params;
3781 struct sctp_association *asoc;
3782 int retval = -EINVAL;
3783
3784 if (optlen != sizeof(params))
3785 goto out;
3786
3787 if (copy_from_user(&params, optval, optlen)) {
3788 retval = -EFAULT;
3789 goto out;
3790 }
3791
3792 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
3793 goto out;
3794
3795 asoc = sctp_id2assoc(sk, params.assoc_id);
3796 if (asoc) {
3797 asoc->strreset_enable = params.assoc_value;
3798 } else if (!params.assoc_id) {
3799 struct sctp_sock *sp = sctp_sk(sk);
3800
3801 sp->ep->strreset_enable = params.assoc_value;
3802 } else {
3803 goto out;
3804 }
3805
3806 retval = 0;
3807
3808out:
3809 return retval;
3810}
3811
Xin Long7f9d68a2017-01-18 00:44:47 +08003812static int sctp_setsockopt_reset_streams(struct sock *sk,
3813 char __user *optval,
3814 unsigned int optlen)
3815{
3816 struct sctp_reset_streams *params;
3817 struct sctp_association *asoc;
3818 int retval = -EINVAL;
3819
Xin Long2342b8d2017-12-10 15:40:51 +08003820 if (optlen < sizeof(*params))
Xin Long7f9d68a2017-01-18 00:44:47 +08003821 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003822 /* srs_number_streams is u16, so optlen can't be bigger than this. */
3823 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3824 sizeof(__u16) * sizeof(*params));
Xin Long7f9d68a2017-01-18 00:44:47 +08003825
3826 params = memdup_user(optval, optlen);
3827 if (IS_ERR(params))
3828 return PTR_ERR(params);
3829
Xin Long2342b8d2017-12-10 15:40:51 +08003830 if (params->srs_number_streams * sizeof(__u16) >
3831 optlen - sizeof(*params))
3832 goto out;
3833
Xin Long7f9d68a2017-01-18 00:44:47 +08003834 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
3835 if (!asoc)
3836 goto out;
3837
3838 retval = sctp_send_reset_streams(asoc, params);
3839
3840out:
3841 kfree(params);
3842 return retval;
3843}
3844
Xin Longa92ce1a2017-02-09 01:18:18 +08003845static int sctp_setsockopt_reset_assoc(struct sock *sk,
3846 char __user *optval,
3847 unsigned int optlen)
3848{
3849 struct sctp_association *asoc;
3850 sctp_assoc_t associd;
3851 int retval = -EINVAL;
3852
3853 if (optlen != sizeof(associd))
3854 goto out;
3855
3856 if (copy_from_user(&associd, optval, optlen)) {
3857 retval = -EFAULT;
3858 goto out;
3859 }
3860
3861 asoc = sctp_id2assoc(sk, associd);
3862 if (!asoc)
3863 goto out;
3864
3865 retval = sctp_send_reset_assoc(asoc);
3866
3867out:
3868 return retval;
3869}
3870
Xin Long242bd2d2017-02-09 01:18:20 +08003871static int sctp_setsockopt_add_streams(struct sock *sk,
3872 char __user *optval,
3873 unsigned int optlen)
3874{
3875 struct sctp_association *asoc;
3876 struct sctp_add_streams params;
3877 int retval = -EINVAL;
3878
3879 if (optlen != sizeof(params))
3880 goto out;
3881
3882 if (copy_from_user(&params, optval, optlen)) {
3883 retval = -EFAULT;
3884 goto out;
3885 }
3886
3887 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
3888 if (!asoc)
3889 goto out;
3890
3891 retval = sctp_send_add_streams(asoc, &params);
3892
3893out:
3894 return retval;
3895}
3896
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03003897static int sctp_setsockopt_scheduler(struct sock *sk,
3898 char __user *optval,
3899 unsigned int optlen)
3900{
3901 struct sctp_association *asoc;
3902 struct sctp_assoc_value params;
3903 int retval = -EINVAL;
3904
3905 if (optlen < sizeof(params))
3906 goto out;
3907
3908 optlen = sizeof(params);
3909 if (copy_from_user(&params, optval, optlen)) {
3910 retval = -EFAULT;
3911 goto out;
3912 }
3913
3914 if (params.assoc_value > SCTP_SS_MAX)
3915 goto out;
3916
3917 asoc = sctp_id2assoc(sk, params.assoc_id);
3918 if (!asoc)
3919 goto out;
3920
3921 retval = sctp_sched_set_sched(asoc, params.assoc_value);
3922
3923out:
3924 return retval;
3925}
3926
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03003927static int sctp_setsockopt_scheduler_value(struct sock *sk,
3928 char __user *optval,
3929 unsigned int optlen)
3930{
3931 struct sctp_association *asoc;
3932 struct sctp_stream_value params;
3933 int retval = -EINVAL;
3934
3935 if (optlen < sizeof(params))
3936 goto out;
3937
3938 optlen = sizeof(params);
3939 if (copy_from_user(&params, optval, optlen)) {
3940 retval = -EFAULT;
3941 goto out;
3942 }
3943
3944 asoc = sctp_id2assoc(sk, params.assoc_id);
3945 if (!asoc)
3946 goto out;
3947
3948 retval = sctp_sched_set_value(asoc, params.stream_id,
3949 params.stream_value, GFP_KERNEL);
3950
3951out:
3952 return retval;
3953}
3954
Xin Long772a5862017-12-08 21:03:58 +08003955static int sctp_setsockopt_interleaving_supported(struct sock *sk,
3956 char __user *optval,
3957 unsigned int optlen)
3958{
3959 struct sctp_sock *sp = sctp_sk(sk);
3960 struct net *net = sock_net(sk);
3961 struct sctp_assoc_value params;
3962 int retval = -EINVAL;
3963
3964 if (optlen < sizeof(params))
3965 goto out;
3966
3967 optlen = sizeof(params);
3968 if (copy_from_user(&params, optval, optlen)) {
3969 retval = -EFAULT;
3970 goto out;
3971 }
3972
3973 if (params.assoc_id)
3974 goto out;
3975
3976 if (!net->sctp.intl_enable || !sp->frag_interleave) {
3977 retval = -EPERM;
3978 goto out;
3979 }
3980
3981 sp->strm_interleave = !!params.assoc_value;
3982
3983 retval = 0;
3984
3985out:
3986 return retval;
3987}
3988
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989/* API 6.2 setsockopt(), getsockopt()
3990 *
3991 * Applications use setsockopt() and getsockopt() to set or retrieve
3992 * socket options. Socket options are used to change the default
3993 * behavior of sockets calls. They are described in Section 7.
3994 *
3995 * The syntax is:
3996 *
3997 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3998 * int __user *optlen);
3999 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4000 * int optlen);
4001 *
4002 * sd - the socket descript.
4003 * level - set to IPPROTO_SCTP for all SCTP options.
4004 * optname - the option name.
4005 * optval - the buffer to store the value of the option.
4006 * optlen - the size of the buffer.
4007 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004008static int sctp_setsockopt(struct sock *sk, int level, int optname,
4009 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
4011 int retval = 0;
4012
Daniel Borkmannbb333812013-06-28 19:49:40 +02004013 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
4015 /* I can hardly begin to describe how wrong this is. This is
4016 * so broken as to be worse than useless. The API draft
4017 * REALLY is NOT helpful here... I am not convinced that the
4018 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4019 * are at all well-founded.
4020 */
4021 if (level != SOL_SCTP) {
4022 struct sctp_af *af = sctp_sk(sk)->pf->af;
4023 retval = af->setsockopt(sk, level, optname, optval, optlen);
4024 goto out_nounlock;
4025 }
4026
wangweidong048ed4b2014-01-21 15:44:11 +08004027 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
4029 switch (optname) {
4030 case SCTP_SOCKOPT_BINDX_ADD:
4031 /* 'optlen' is the size of the addresses buffer. */
4032 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4033 optlen, SCTP_BINDX_ADD_ADDR);
4034 break;
4035
4036 case SCTP_SOCKOPT_BINDX_REM:
4037 /* 'optlen' is the size of the addresses buffer. */
4038 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4039 optlen, SCTP_BINDX_REM_ADDR);
4040 break;
4041
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004042 case SCTP_SOCKOPT_CONNECTX_OLD:
4043 /* 'optlen' is the size of the addresses buffer. */
4044 retval = sctp_setsockopt_connectx_old(sk,
4045 (struct sockaddr __user *)optval,
4046 optlen);
4047 break;
4048
Frank Filz3f7a87d2005-06-20 13:14:57 -07004049 case SCTP_SOCKOPT_CONNECTX:
4050 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004051 retval = sctp_setsockopt_connectx(sk,
4052 (struct sockaddr __user *)optval,
4053 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004054 break;
4055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 case SCTP_DISABLE_FRAGMENTS:
4057 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4058 break;
4059
4060 case SCTP_EVENTS:
4061 retval = sctp_setsockopt_events(sk, optval, optlen);
4062 break;
4063
4064 case SCTP_AUTOCLOSE:
4065 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4066 break;
4067
4068 case SCTP_PEER_ADDR_PARAMS:
4069 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4070 break;
4071
Shan Wei4580ccc2011-01-18 22:39:00 +00004072 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07004073 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08004074 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004075 case SCTP_PARTIAL_DELIVERY_POINT:
4076 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4077 break;
Frank Filz77086102005-12-22 11:37:30 -08004078
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 case SCTP_INITMSG:
4080 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4081 break;
4082 case SCTP_DEFAULT_SEND_PARAM:
4083 retval = sctp_setsockopt_default_send_param(sk, optval,
4084 optlen);
4085 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02004086 case SCTP_DEFAULT_SNDINFO:
4087 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 case SCTP_PRIMARY_ADDR:
4090 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4091 break;
4092 case SCTP_SET_PEER_PRIMARY_ADDR:
4093 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4094 break;
4095 case SCTP_NODELAY:
4096 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4097 break;
4098 case SCTP_RTOINFO:
4099 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4100 break;
4101 case SCTP_ASSOCINFO:
4102 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4103 break;
4104 case SCTP_I_WANT_MAPPED_V4_ADDR:
4105 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4106 break;
4107 case SCTP_MAXSEG:
4108 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4109 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004110 case SCTP_ADAPTATION_LAYER:
4111 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004113 case SCTP_CONTEXT:
4114 retval = sctp_setsockopt_context(sk, optval, optlen);
4115 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004116 case SCTP_FRAGMENT_INTERLEAVE:
4117 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4118 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07004119 case SCTP_MAX_BURST:
4120 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4121 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004122 case SCTP_AUTH_CHUNK:
4123 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4124 break;
4125 case SCTP_HMAC_IDENT:
4126 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4127 break;
4128 case SCTP_AUTH_KEY:
4129 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4130 break;
4131 case SCTP_AUTH_ACTIVE_KEY:
4132 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4133 break;
4134 case SCTP_AUTH_DELETE_KEY:
4135 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4136 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09004137 case SCTP_AUTO_ASCONF:
4138 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4139 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00004140 case SCTP_PEER_ADDR_THLDS:
4141 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4142 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004143 case SCTP_RECVRCVINFO:
4144 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4145 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004146 case SCTP_RECVNXTINFO:
4147 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4148 break;
Xin Long28aa4c22016-07-09 19:47:40 +08004149 case SCTP_PR_SUPPORTED:
4150 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4151 break;
Xin Longf959fb42016-07-09 19:47:41 +08004152 case SCTP_DEFAULT_PRINFO:
4153 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4154 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08004155 case SCTP_RECONFIG_SUPPORTED:
4156 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4157 break;
Xin Long9fb657a2017-01-18 00:44:46 +08004158 case SCTP_ENABLE_STREAM_RESET:
4159 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4160 break;
Xin Long7f9d68a2017-01-18 00:44:47 +08004161 case SCTP_RESET_STREAMS:
4162 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4163 break;
Xin Longa92ce1a2017-02-09 01:18:18 +08004164 case SCTP_RESET_ASSOC:
4165 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4166 break;
Xin Long242bd2d2017-02-09 01:18:20 +08004167 case SCTP_ADD_STREAMS:
4168 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4169 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004170 case SCTP_STREAM_SCHEDULER:
4171 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4172 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004173 case SCTP_STREAM_SCHEDULER_VALUE:
4174 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4175 break;
Xin Long772a5862017-12-08 21:03:58 +08004176 case SCTP_INTERLEAVING_SUPPORTED:
4177 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4178 optlen);
4179 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 default:
4181 retval = -ENOPROTOOPT;
4182 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
wangweidong048ed4b2014-01-21 15:44:11 +08004185 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
4187out_nounlock:
4188 return retval;
4189}
4190
4191/* API 3.1.6 connect() - UDP Style Syntax
4192 *
4193 * An application may use the connect() call in the UDP model to initiate an
4194 * association without sending data.
4195 *
4196 * The syntax is:
4197 *
4198 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4199 *
4200 * sd: the socket descriptor to have a new association added to.
4201 *
4202 * nam: the address structure (either struct sockaddr_in or struct
4203 * sockaddr_in6 defined in RFC2553 [7]).
4204 *
4205 * len: the size of the address.
4206 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004207static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4208 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07004211 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
wangweidong048ed4b2014-01-21 15:44:11 +08004213 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Daniel Borkmannbb333812013-06-28 19:49:40 +02004215 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4216 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
Frank Filz3f7a87d2005-06-20 13:14:57 -07004218 /* Validate addr_len before calling common connect/connectx routine. */
4219 af = sctp_get_af_specific(addr->sa_family);
4220 if (!af || addr_len < af->sockaddr_len) {
4221 err = -EINVAL;
4222 } else {
4223 /* Pass correct addr len to common routine (so it knows there
4224 * is only one address being passed.
4225 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004226 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 }
4228
wangweidong048ed4b2014-01-21 15:44:11 +08004229 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 return err;
4231}
4232
4233/* FIXME: Write comments. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004234static int sctp_disconnect(struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235{
4236 return -EOPNOTSUPP; /* STUB */
4237}
4238
4239/* 4.1.4 accept() - TCP Style Syntax
4240 *
4241 * Applications use accept() call to remove an established SCTP
4242 * association from the accept queue of the endpoint. A new socket
4243 * descriptor will be returned from accept() to represent the newly
4244 * formed association.
4245 */
David Howellscdfbabf2017-03-09 08:09:05 +00004246static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247{
4248 struct sctp_sock *sp;
4249 struct sctp_endpoint *ep;
4250 struct sock *newsk = NULL;
4251 struct sctp_association *asoc;
4252 long timeo;
4253 int error = 0;
4254
wangweidong048ed4b2014-01-21 15:44:11 +08004255 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
4257 sp = sctp_sk(sk);
4258 ep = sp->ep;
4259
4260 if (!sctp_style(sk, TCP)) {
4261 error = -EOPNOTSUPP;
4262 goto out;
4263 }
4264
4265 if (!sctp_sstate(sk, LISTENING)) {
4266 error = -EINVAL;
4267 goto out;
4268 }
4269
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07004270 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
4272 error = sctp_wait_for_accept(sk, timeo);
4273 if (error)
4274 goto out;
4275
4276 /* We treat the list of associations on the endpoint as the accept
4277 * queue and pick the first association on the list.
4278 */
4279 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4280
David Howellscdfbabf2017-03-09 08:09:05 +00004281 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 if (!newsk) {
4283 error = -ENOMEM;
4284 goto out;
4285 }
4286
4287 /* Populate the fields of the newsk from the oldsk and migrate the
4288 * asoc to the newsk.
4289 */
4290 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4291
4292out:
wangweidong048ed4b2014-01-21 15:44:11 +08004293 release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004294 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 return newsk;
4296}
4297
4298/* The SCTP ioctl handler. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004299static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004301 int rc = -ENOTCONN;
4302
wangweidong048ed4b2014-01-21 15:44:11 +08004303 lock_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004304
4305 /*
4306 * SEQPACKET-style sockets in LISTENING state are valid, for
4307 * SCTP, so only discard TCP-style sockets in LISTENING state.
4308 */
4309 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4310 goto out;
4311
4312 switch (cmd) {
4313 case SIOCINQ: {
4314 struct sk_buff *skb;
4315 unsigned int amount = 0;
4316
4317 skb = skb_peek(&sk->sk_receive_queue);
4318 if (skb != NULL) {
4319 /*
4320 * We will only return the amount of this packet since
4321 * that is all that will be read.
4322 */
4323 amount = skb->len;
4324 }
4325 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004326 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07004327 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004328 default:
4329 rc = -ENOIOCTLCMD;
4330 break;
4331 }
4332out:
wangweidong048ed4b2014-01-21 15:44:11 +08004333 release_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004334 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335}
4336
4337/* This is the function which gets called during socket creation to
4338 * initialized the SCTP-specific portion of the sock.
4339 * The sock structure should already be zero-filled memory.
4340 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004341static int sctp_init_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004343 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 struct sctp_sock *sp;
4345
Daniel Borkmannbb333812013-06-28 19:49:40 +02004346 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
4348 sp = sctp_sk(sk);
4349
4350 /* Initialize the SCTP per socket area. */
4351 switch (sk->sk_type) {
4352 case SOCK_SEQPACKET:
4353 sp->type = SCTP_SOCKET_UDP;
4354 break;
4355 case SOCK_STREAM:
4356 sp->type = SCTP_SOCKET_TCP;
4357 break;
4358 default:
4359 return -ESOCKTNOSUPPORT;
4360 }
4361
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004362 sk->sk_gso_type = SKB_GSO_SCTP;
4363
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 /* Initialize default send parameters. These parameters can be
4365 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4366 */
4367 sp->default_stream = 0;
4368 sp->default_ppid = 0;
4369 sp->default_flags = 0;
4370 sp->default_context = 0;
4371 sp->default_timetolive = 0;
4372
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004373 sp->default_rcv_context = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004374 sp->max_burst = net->sctp.max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004375
Neil Horman3c681982012-10-24 09:20:03 +00004376 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4377
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 /* Initialize default setup parameters. These parameters
4379 * can be modified with the SCTP_INITMSG socket option or
4380 * overridden by the SCTP_INIT CMSG.
4381 */
4382 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4383 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004384 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4385 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
4387 /* Initialize default RTO related parameters. These parameters can
4388 * be modified for with the SCTP_RTOINFO socket option.
4389 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004390 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4391 sp->rtoinfo.srto_max = net->sctp.rto_max;
4392 sp->rtoinfo.srto_min = net->sctp.rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
4394 /* Initialize default association related parameters. These parameters
4395 * can be modified with the SCTP_ASSOCINFO socket option.
4396 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004397 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 sp->assocparams.sasoc_number_peer_destinations = 0;
4399 sp->assocparams.sasoc_peer_rwnd = 0;
4400 sp->assocparams.sasoc_local_rwnd = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004401 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402
4403 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004404 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 */
4406 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4407
4408 /* Default Peer Address Parameters. These defaults can
4409 * be modified via SCTP_PEER_ADDR_PARAMS
4410 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004411 sp->hbinterval = net->sctp.hb_interval;
4412 sp->pathmaxrxt = net->sctp.max_retrans_path;
wangweidong4e2d52b2013-12-23 12:16:54 +08004413 sp->pathmtu = 0; /* allow default discovery */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004414 sp->sackdelay = net->sctp.sack_timeout;
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07004415 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004416 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004417 SPP_PMTUD_ENABLE |
4418 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
4420 /* If enabled no SCTP message fragmentation will be performed.
4421 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4422 */
4423 sp->disable_fragments = 0;
4424
Sridhar Samudrala208edef2006-09-29 17:08:01 -07004425 /* Enable Nagle algorithm by default. */
4426 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004428 sp->recvrcvinfo = 0;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004429 sp->recvnxtinfo = 0;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004430
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 /* Enable by default. */
4432 sp->v4mapped = 1;
4433
4434 /* Auto-close idle associations after the configured
4435 * number of seconds. A value of 0 disables this
4436 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4437 * for UDP-style sockets only.
4438 */
4439 sp->autoclose = 0;
4440
4441 /* User specified fragmentation limit. */
4442 sp->user_frag = 0;
4443
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004444 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
4446 sp->pf = sctp_get_pf_specific(sk->sk_family);
4447
4448 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004449 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004451 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
4453 /* Create a per socket endpoint structure. Even if we
4454 * change the data structure relationships, this may still
4455 * be useful for storing pre-connect address information.
4456 */
Daniel Borkmannc164b832013-06-14 18:24:06 +02004457 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4458 if (!sp->ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 return -ENOMEM;
4460
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 sp->hmac = NULL;
4462
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004463 sk->sk_destruct = sctp_destruct_sock;
4464
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08004466
4467 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004468 sk_sockets_allocated_inc(sk);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004469 sock_prot_inuse_add(net, sk->sk_prot, 1);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004470
4471 /* Nothing can fail after this block, otherwise
4472 * sctp_destroy_sock() will be called without addr_wq_lock held
4473 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004474 if (net->sctp.default_auto_asconf) {
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004475 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda9f7d6532011-04-26 19:32:51 +09004476 list_add_tail(&sp->auto_asconf_list,
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004477 &net->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09004478 sp->do_auto_asconf = 1;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004479 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4480 } else {
Michio Honda9f7d6532011-04-26 19:32:51 +09004481 sp->do_auto_asconf = 0;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004482 }
4483
David S. Miller6f756a82008-11-23 17:34:03 -08004484 local_bh_enable();
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 return 0;
4487}
4488
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004489/* Cleanup any SCTP per socket resources. Must be called with
4490 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4491 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004492static void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493{
Michio Honda9f7d6532011-04-26 19:32:51 +09004494 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
Daniel Borkmannbb333812013-06-28 19:49:40 +02004496 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
4498 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09004499 sp = sctp_sk(sk);
Daniel Borkmann1abd1652013-06-06 15:53:47 +02004500 /* This could happen during socket init, thus we bail out
4501 * early, since the rest of the below is not setup either.
4502 */
4503 if (sp->ep == NULL)
4504 return;
4505
Michio Honda9f7d6532011-04-26 19:32:51 +09004506 if (sp->do_auto_asconf) {
4507 sp->do_auto_asconf = 0;
4508 list_del(&sp->auto_asconf_list);
4509 }
4510 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004511 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004512 sk_sockets_allocated_dec(sk);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08004513 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004514 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515}
4516
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004517/* Triggered when there are no references on the socket anymore */
4518static void sctp_destruct_sock(struct sock *sk)
4519{
4520 struct sctp_sock *sp = sctp_sk(sk);
4521
4522 /* Free up the HMAC transform. */
Herbert Xu5821c762016-01-24 21:20:12 +08004523 crypto_free_shash(sp->hmac);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004524
4525 inet_sock_destruct(sk);
4526}
4527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528/* API 4.1.7 shutdown() - TCP Style Syntax
4529 * int shutdown(int socket, int how);
4530 *
4531 * sd - the socket descriptor of the association to be closed.
4532 * how - Specifies the type of shutdown. The values are
4533 * as follows:
4534 * SHUT_RD
4535 * Disables further receive operations. No SCTP
4536 * protocol action is taken.
4537 * SHUT_WR
4538 * Disables further send operations, and initiates
4539 * the SCTP shutdown sequence.
4540 * SHUT_RDWR
4541 * Disables further send and receive operations
4542 * and initiates the SCTP shutdown sequence.
4543 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004544static void sctp_shutdown(struct sock *sk, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004546 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 struct sctp_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
4549 if (!sctp_style(sk, TCP))
4550 return;
4551
Xin Long5bf35dd2016-11-13 21:44:37 +08004552 ep = sctp_sk(sk)->ep;
4553 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4554 struct sctp_association *asoc;
4555
Yafang Shaocbabf462017-12-20 11:12:54 +08004556 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Xin Long5bf35dd2016-11-13 21:44:37 +08004557 asoc = list_entry(ep->asocs.next,
4558 struct sctp_association, asocs);
4559 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 }
4561}
4562
Xin Long52c52a62016-04-14 15:35:30 +08004563int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4564 struct sctp_info *info)
4565{
4566 struct sctp_transport *prim;
4567 struct list_head *pos;
4568 int mask;
4569
4570 memset(info, 0, sizeof(*info));
4571 if (!asoc) {
4572 struct sctp_sock *sp = sctp_sk(sk);
4573
4574 info->sctpi_s_autoclose = sp->autoclose;
4575 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4576 info->sctpi_s_pd_point = sp->pd_point;
4577 info->sctpi_s_nodelay = sp->nodelay;
4578 info->sctpi_s_disable_fragments = sp->disable_fragments;
4579 info->sctpi_s_v4mapped = sp->v4mapped;
4580 info->sctpi_s_frag_interleave = sp->frag_interleave;
Xin Long40eb90e92016-05-29 17:42:13 +08004581 info->sctpi_s_type = sp->type;
Xin Long52c52a62016-04-14 15:35:30 +08004582
4583 return 0;
4584 }
4585
4586 info->sctpi_tag = asoc->c.my_vtag;
4587 info->sctpi_state = asoc->state;
4588 info->sctpi_rwnd = asoc->a_rwnd;
4589 info->sctpi_unackdata = asoc->unack_data;
4590 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004591 info->sctpi_instrms = asoc->stream.incnt;
4592 info->sctpi_outstrms = asoc->stream.outcnt;
Xin Long52c52a62016-04-14 15:35:30 +08004593 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4594 info->sctpi_inqueue++;
4595 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4596 info->sctpi_outqueue++;
4597 info->sctpi_overall_error = asoc->overall_error_count;
4598 info->sctpi_max_burst = asoc->max_burst;
4599 info->sctpi_maxseg = asoc->frag_point;
4600 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4601 info->sctpi_peer_tag = asoc->c.peer_vtag;
4602
4603 mask = asoc->peer.ecn_capable << 1;
4604 mask = (mask | asoc->peer.ipv4_address) << 1;
4605 mask = (mask | asoc->peer.ipv6_address) << 1;
4606 mask = (mask | asoc->peer.hostname_address) << 1;
4607 mask = (mask | asoc->peer.asconf_capable) << 1;
4608 mask = (mask | asoc->peer.prsctp_capable) << 1;
4609 mask = (mask | asoc->peer.auth_capable);
4610 info->sctpi_peer_capable = mask;
4611 mask = asoc->peer.sack_needed << 1;
4612 mask = (mask | asoc->peer.sack_generation) << 1;
4613 mask = (mask | asoc->peer.zero_window_announced);
4614 info->sctpi_peer_sack = mask;
4615
4616 info->sctpi_isacks = asoc->stats.isacks;
4617 info->sctpi_osacks = asoc->stats.osacks;
4618 info->sctpi_opackets = asoc->stats.opackets;
4619 info->sctpi_ipackets = asoc->stats.ipackets;
4620 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4621 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4622 info->sctpi_idupchunks = asoc->stats.idupchunks;
4623 info->sctpi_gapcnt = asoc->stats.gapcnt;
4624 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4625 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4626 info->sctpi_oodchunks = asoc->stats.oodchunks;
4627 info->sctpi_iodchunks = asoc->stats.iodchunks;
4628 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4629 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4630
4631 prim = asoc->peer.primary_path;
Stefano Brivioee6c88b2017-08-23 13:27:13 +02004632 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
Xin Long52c52a62016-04-14 15:35:30 +08004633 info->sctpi_p_state = prim->state;
4634 info->sctpi_p_cwnd = prim->cwnd;
4635 info->sctpi_p_srtt = prim->srtt;
4636 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4637 info->sctpi_p_hbinterval = prim->hbinterval;
4638 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4639 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4640 info->sctpi_p_ssthresh = prim->ssthresh;
4641 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4642 info->sctpi_p_flight_size = prim->flight_size;
4643 info->sctpi_p_error = prim->error_count;
4644
4645 return 0;
4646}
4647EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4648
Xin Long626d16f2016-04-14 15:35:31 +08004649/* use callback to avoid exporting the core structure */
Tom Herbert97a6ec42017-12-04 10:31:41 -08004650void sctp_transport_walk_start(struct rhashtable_iter *iter)
Xin Long626d16f2016-04-14 15:35:31 +08004651{
Xin Long7fda7022016-11-15 23:23:11 +08004652 rhltable_walk_enter(&sctp_transport_hashtable, iter);
Xin Long626d16f2016-04-14 15:35:31 +08004653
Tom Herbert97a6ec42017-12-04 10:31:41 -08004654 rhashtable_walk_start(iter);
Xin Long626d16f2016-04-14 15:35:31 +08004655}
4656
4657void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4658{
4659 rhashtable_walk_stop(iter);
4660 rhashtable_walk_exit(iter);
4661}
4662
4663struct sctp_transport *sctp_transport_get_next(struct net *net,
4664 struct rhashtable_iter *iter)
4665{
4666 struct sctp_transport *t;
4667
4668 t = rhashtable_walk_next(iter);
4669 for (; t; t = rhashtable_walk_next(iter)) {
4670 if (IS_ERR(t)) {
4671 if (PTR_ERR(t) == -EAGAIN)
4672 continue;
4673 break;
4674 }
4675
4676 if (net_eq(sock_net(t->asoc->base.sk), net) &&
4677 t->asoc->peer.primary_path == t)
4678 break;
4679 }
4680
4681 return t;
4682}
4683
4684struct sctp_transport *sctp_transport_get_idx(struct net *net,
4685 struct rhashtable_iter *iter,
4686 int pos)
4687{
4688 void *obj = SEQ_START_TOKEN;
4689
4690 while (pos && (obj = sctp_transport_get_next(net, iter)) &&
4691 !IS_ERR(obj))
4692 pos--;
4693
4694 return obj;
4695}
4696
4697int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
4698 void *p) {
4699 int err = 0;
4700 int hash = 0;
4701 struct sctp_ep_common *epb;
4702 struct sctp_hashbucket *head;
4703
4704 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
4705 hash++, head++) {
Xin Long581409d2017-06-10 14:48:14 +08004706 read_lock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004707 sctp_for_each_hentry(epb, &head->chain) {
4708 err = cb(sctp_ep(epb), p);
4709 if (err)
4710 break;
4711 }
Xin Long581409d2017-06-10 14:48:14 +08004712 read_unlock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004713 }
4714
4715 return err;
4716}
4717EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
4718
4719int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
4720 struct net *net,
4721 const union sctp_addr *laddr,
4722 const union sctp_addr *paddr, void *p)
4723{
4724 struct sctp_transport *transport;
Xin Long08abb792016-12-15 23:05:52 +08004725 int err;
Xin Long626d16f2016-04-14 15:35:31 +08004726
4727 rcu_read_lock();
4728 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
Xin Long626d16f2016-04-14 15:35:31 +08004729 rcu_read_unlock();
Xin Long08abb792016-12-15 23:05:52 +08004730 if (!transport)
4731 return -ENOENT;
4732
Xin Long1cceda782016-09-29 02:55:44 +08004733 err = cb(transport, p);
Xin Longcd26da42016-10-31 20:32:31 +08004734 sctp_transport_put(transport);
Xin Long1cceda782016-09-29 02:55:44 +08004735
Xin Long626d16f2016-04-14 15:35:31 +08004736 return err;
4737}
4738EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
4739
4740int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
Xin Longd25adbe2017-09-15 11:02:21 +08004741 int (*cb_done)(struct sctp_transport *, void *),
4742 struct net *net, int *pos, void *p) {
Xin Long626d16f2016-04-14 15:35:31 +08004743 struct rhashtable_iter hti;
Xin Longd25adbe2017-09-15 11:02:21 +08004744 struct sctp_transport *tsp;
4745 int ret;
Xin Long626d16f2016-04-14 15:35:31 +08004746
Xin Longd25adbe2017-09-15 11:02:21 +08004747again:
Xin Longf53d77e2018-01-23 18:22:25 +08004748 ret = 0;
Tom Herbert97a6ec42017-12-04 10:31:41 -08004749 sctp_transport_walk_start(&hti);
Xin Long626d16f2016-04-14 15:35:31 +08004750
Xin Longd25adbe2017-09-15 11:02:21 +08004751 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
4752 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
4753 if (!sctp_transport_hold(tsp))
Xin Long626d16f2016-04-14 15:35:31 +08004754 continue;
Xin Longd25adbe2017-09-15 11:02:21 +08004755 ret = cb(tsp, p);
4756 if (ret)
Xin Long626d16f2016-04-14 15:35:31 +08004757 break;
Xin Longd25adbe2017-09-15 11:02:21 +08004758 (*pos)++;
4759 sctp_transport_put(tsp);
Xin Long626d16f2016-04-14 15:35:31 +08004760 }
Xin Long626d16f2016-04-14 15:35:31 +08004761 sctp_transport_walk_stop(&hti);
Xin Long53fa1032016-04-14 15:35:35 +08004762
Xin Longd25adbe2017-09-15 11:02:21 +08004763 if (ret) {
4764 if (cb_done && !cb_done(tsp, p)) {
4765 (*pos)++;
4766 sctp_transport_put(tsp);
4767 goto again;
4768 }
4769 sctp_transport_put(tsp);
4770 }
4771
4772 return ret;
Xin Long626d16f2016-04-14 15:35:31 +08004773}
4774EXPORT_SYMBOL_GPL(sctp_for_each_transport);
4775
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776/* 7.2.1 Association Status (SCTP_STATUS)
4777
4778 * Applications can retrieve current status information about an
4779 * association, including association state, peer receiver window size,
4780 * number of unacked data chunks, and number of data chunks pending
4781 * receipt. This information is read-only.
4782 */
4783static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4784 char __user *optval,
4785 int __user *optlen)
4786{
4787 struct sctp_status status;
4788 struct sctp_association *asoc = NULL;
4789 struct sctp_transport *transport;
4790 sctp_assoc_t associd;
4791 int retval = 0;
4792
Neil Horman408f22e2007-06-16 14:03:45 -04004793 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 retval = -EINVAL;
4795 goto out;
4796 }
4797
Neil Horman408f22e2007-06-16 14:03:45 -04004798 len = sizeof(status);
4799 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 retval = -EFAULT;
4801 goto out;
4802 }
4803
4804 associd = status.sstat_assoc_id;
4805 asoc = sctp_id2assoc(sk, associd);
4806 if (!asoc) {
4807 retval = -EINVAL;
4808 goto out;
4809 }
4810
4811 transport = asoc->peer.primary_path;
4812
4813 status.sstat_assoc_id = sctp_assoc2id(asoc);
Daniel Borkmann38ab1fa2014-08-28 15:28:26 +02004814 status.sstat_state = sctp_assoc_to_state(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 status.sstat_rwnd = asoc->peer.rwnd;
4816 status.sstat_unackdata = asoc->unack_data;
4817
4818 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004819 status.sstat_instrms = asoc->stream.incnt;
4820 status.sstat_outstrms = asoc->stream.outcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 status.sstat_fragmentation_point = asoc->frag_point;
4822 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08004823 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4824 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 /* Map ipv4 address into v4-mapped-on-v6 address. */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06004826 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004828 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4830 status.sstat_primary.spinfo_srtt = transport->srtt;
4831 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004832 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
Frank Filz3f7a87d2005-06-20 13:14:57 -07004834 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4835 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 if (put_user(len, optlen)) {
4838 retval = -EFAULT;
4839 goto out;
4840 }
4841
Daniel Borkmannbb333812013-06-28 19:49:40 +02004842 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
4843 __func__, len, status.sstat_state, status.sstat_rwnd,
4844 status.sstat_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
4846 if (copy_to_user(optval, &status, len)) {
4847 retval = -EFAULT;
4848 goto out;
4849 }
4850
4851out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004852 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853}
4854
4855
4856/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4857 *
4858 * Applications can retrieve information about a specific peer address
4859 * of an association, including its reachability state, congestion
4860 * window, and retransmission timer values. This information is
4861 * read-only.
4862 */
4863static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4864 char __user *optval,
4865 int __user *optlen)
4866{
4867 struct sctp_paddrinfo pinfo;
4868 struct sctp_transport *transport;
4869 int retval = 0;
4870
Neil Horman408f22e2007-06-16 14:03:45 -04004871 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 retval = -EINVAL;
4873 goto out;
4874 }
4875
Neil Horman408f22e2007-06-16 14:03:45 -04004876 len = sizeof(pinfo);
4877 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 retval = -EFAULT;
4879 goto out;
4880 }
4881
4882 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4883 pinfo.spinfo_assoc_id);
4884 if (!transport)
4885 return -EINVAL;
4886
4887 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004888 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 pinfo.spinfo_cwnd = transport->cwnd;
4890 pinfo.spinfo_srtt = transport->srtt;
4891 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004892 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
Frank Filz3f7a87d2005-06-20 13:14:57 -07004894 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4895 pinfo.spinfo_state = SCTP_ACTIVE;
4896
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 if (put_user(len, optlen)) {
4898 retval = -EFAULT;
4899 goto out;
4900 }
4901
4902 if (copy_to_user(optval, &pinfo, len)) {
4903 retval = -EFAULT;
4904 goto out;
4905 }
4906
4907out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004908 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909}
4910
4911/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4912 *
4913 * This option is a on/off flag. If enabled no SCTP message
4914 * fragmentation will be performed. Instead if a message being sent
4915 * exceeds the current PMTU size, the message will NOT be sent and
4916 * instead a error will be indicated to the user.
4917 */
4918static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4919 char __user *optval, int __user *optlen)
4920{
4921 int val;
4922
4923 if (len < sizeof(int))
4924 return -EINVAL;
4925
4926 len = sizeof(int);
4927 val = (sctp_sk(sk)->disable_fragments == 1);
4928 if (put_user(len, optlen))
4929 return -EFAULT;
4930 if (copy_to_user(optval, &val, len))
4931 return -EFAULT;
4932 return 0;
4933}
4934
4935/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4936 *
4937 * This socket option is used to specify various notifications and
4938 * ancillary data the user wishes to receive.
4939 */
4940static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
4941 int __user *optlen)
4942{
Jiri Slabya4b8e712016-10-21 14:13:24 +02004943 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00004945 if (len > sizeof(struct sctp_event_subscribe))
4946 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04004947 if (put_user(len, optlen))
4948 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4950 return -EFAULT;
4951 return 0;
4952}
4953
4954/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
4955 *
4956 * This socket option is applicable to the UDP-style socket only. When
4957 * set it will cause associations that are idle for more than the
4958 * specified number of seconds to automatically close. An association
4959 * being idle is defined an association that has NOT sent or received
4960 * user data. The special value of '0' indicates that no automatic
4961 * close of any associations should be performed. The option expects an
4962 * integer defining the number of seconds of idle time before an
4963 * association is closed.
4964 */
4965static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
4966{
4967 /* Applicable to UDP-style socket only */
4968 if (sctp_style(sk, TCP))
4969 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04004970 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004972 len = sizeof(int);
4973 if (put_user(len, optlen))
4974 return -EFAULT;
David Windsorb2ce04c2017-06-10 22:50:43 -04004975 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 return -EFAULT;
4977 return 0;
4978}
4979
4980/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00004981int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982{
Benjamin Poirier0343c552012-03-08 05:55:58 +00004983 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06004984 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 struct socket *sock;
4986 int err = 0;
4987
Xin Longdf80cd92017-10-17 23:26:10 +08004988 /* Do not peel off from one netns to another one. */
4989 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
4990 return -EINVAL;
4991
Benjamin Poirier0343c552012-03-08 05:55:58 +00004992 if (!asoc)
4993 return -EINVAL;
4994
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 /* An association cannot be branched off from an already peeled-off
4996 * socket, nor is this supported for tcp style sockets.
4997 */
4998 if (!sctp_style(sk, UDP))
4999 return -EINVAL;
5000
5001 /* Create a new socket. */
5002 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5003 if (err < 0)
5004 return err;
5005
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005006 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005007
5008 /* Make peeled-off sockets more like 1-1 accepted sockets.
5009 * Set the daddr and initialize id to something more random
5010 */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005011 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005012
5013 /* Populate the fields of the newsk from the oldsk and migrate the
5014 * asoc to the newsk.
5015 */
5016 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005017
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 *sockp = sock;
5019
5020 return err;
5021}
Benjamin Poirier0343c552012-03-08 05:55:58 +00005022EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005024static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5025 struct file **newfile, unsigned flags)
5026{
5027 struct socket *newsock;
5028 int retval;
5029
5030 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5031 if (retval < 0)
5032 goto out;
5033
5034 /* Map the socket to an unused fd that can be returned to the user. */
5035 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5036 if (retval < 0) {
5037 sock_release(newsock);
5038 goto out;
5039 }
5040
5041 *newfile = sock_alloc_file(newsock, 0, NULL);
5042 if (IS_ERR(*newfile)) {
5043 put_unused_fd(retval);
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005044 retval = PTR_ERR(*newfile);
5045 *newfile = NULL;
5046 return retval;
5047 }
5048
5049 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5050 retval);
5051
5052 peeloff->sd = retval;
5053
5054 if (flags & SOCK_NONBLOCK)
5055 (*newfile)->f_flags |= O_NONBLOCK;
5056out:
5057 return retval;
5058}
5059
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5061{
5062 sctp_peeloff_arg_t peeloff;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005063 struct file *newfile = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
Neil Horman408f22e2007-06-16 14:03:45 -04005066 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005068 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 if (copy_from_user(&peeloff, optval, len))
5070 return -EFAULT;
5071
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005072 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 if (retval < 0)
5074 goto out;
5075
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04005077 if (put_user(len, optlen)) {
5078 fput(newfile);
5079 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04005080 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04005081 }
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005082
5083 if (copy_to_user(optval, &peeloff, len)) {
5084 fput(newfile);
5085 put_unused_fd(retval);
5086 return -EFAULT;
5087 }
5088 fd_install(retval, newfile);
5089out:
5090 return retval;
5091}
5092
5093static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5094 char __user *optval, int __user *optlen)
5095{
5096 sctp_peeloff_flags_arg_t peeloff;
5097 struct file *newfile = NULL;
5098 int retval = 0;
5099
5100 if (len < sizeof(sctp_peeloff_flags_arg_t))
5101 return -EINVAL;
5102 len = sizeof(sctp_peeloff_flags_arg_t);
5103 if (copy_from_user(&peeloff, optval, len))
5104 return -EFAULT;
5105
5106 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5107 &newfile, peeloff.flags);
5108 if (retval < 0)
5109 goto out;
5110
5111 /* Return the fd mapped to the new socket. */
5112 if (put_user(len, optlen)) {
5113 fput(newfile);
5114 put_unused_fd(retval);
5115 return -EFAULT;
5116 }
5117
Al Viro56b31d12012-08-18 00:25:51 -04005118 if (copy_to_user(optval, &peeloff, len)) {
5119 fput(newfile);
5120 put_unused_fd(retval);
5121 return -EFAULT;
5122 }
5123 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124out:
5125 return retval;
5126}
5127
5128/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5129 *
5130 * Applications can enable or disable heartbeats for any peer address of
5131 * an association, modify an address's heartbeat interval, force a
5132 * heartbeat to be sent immediately, and adjust the address's maximum
5133 * number of retransmissions sent before an address is considered
5134 * unreachable. The following structure is used to access and modify an
5135 * address's parameters:
5136 *
5137 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005138 * sctp_assoc_t spp_assoc_id;
5139 * struct sockaddr_storage spp_address;
5140 * uint32_t spp_hbinterval;
5141 * uint16_t spp_pathmaxrxt;
5142 * uint32_t spp_pathmtu;
5143 * uint32_t spp_sackdelay;
5144 * uint32_t spp_flags;
5145 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08005147 * spp_assoc_id - (one-to-many style socket) This is filled in the
5148 * application, and identifies the association for
5149 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 * spp_address - This specifies which address is of interest.
5151 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005152 * in milliseconds. If a value of zero
5153 * is present in this field then no changes are to
5154 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 * spp_pathmaxrxt - This contains the maximum number of
5156 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08005157 * considered unreachable. If a value of zero
5158 * is present in this field then no changes are to
5159 * be made to this parameter.
5160 * spp_pathmtu - When Path MTU discovery is disabled the value
5161 * specified here will be the "fixed" path mtu.
5162 * Note that if the spp_address field is empty
5163 * then all associations on this address will
5164 * have this fixed path mtu set upon them.
5165 *
5166 * spp_sackdelay - When delayed sack is enabled, this value specifies
5167 * the number of milliseconds that sacks will be delayed
5168 * for. This value will apply to all addresses of an
5169 * association if the spp_address field is empty. Note
5170 * also, that if delayed sack is enabled and this
5171 * value is set to 0, no change is made to the last
5172 * recorded delayed sack timer value.
5173 *
5174 * spp_flags - These flags are used to control various features
5175 * on an association. The flag field may contain
5176 * zero or more of the following options.
5177 *
5178 * SPP_HB_ENABLE - Enable heartbeats on the
5179 * specified address. Note that if the address
5180 * field is empty all addresses for the association
5181 * have heartbeats enabled upon them.
5182 *
5183 * SPP_HB_DISABLE - Disable heartbeats on the
5184 * speicifed address. Note that if the address
5185 * field is empty all addresses for the association
5186 * will have their heartbeats disabled. Note also
5187 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5188 * mutually exclusive, only one of these two should
5189 * be specified. Enabling both fields will have
5190 * undetermined results.
5191 *
5192 * SPP_HB_DEMAND - Request a user initiated heartbeat
5193 * to be made immediately.
5194 *
5195 * SPP_PMTUD_ENABLE - This field will enable PMTU
5196 * discovery upon the specified address. Note that
5197 * if the address feild is empty then all addresses
5198 * on the association are effected.
5199 *
5200 * SPP_PMTUD_DISABLE - This field will disable PMTU
5201 * discovery upon the specified address. Note that
5202 * if the address feild is empty then all addresses
5203 * on the association are effected. Not also that
5204 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5205 * exclusive. Enabling both will have undetermined
5206 * results.
5207 *
5208 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5209 * on delayed sack. The time specified in spp_sackdelay
5210 * is used to specify the sack delay for this address. Note
5211 * that if spp_address is empty then all addresses will
5212 * enable delayed sack and take on the sack delay
5213 * value specified in spp_sackdelay.
5214 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5215 * off delayed sack. If the spp_address field is blank then
5216 * delayed sack is disabled for the entire association. Note
5217 * also that this field is mutually exclusive to
5218 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5219 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 */
5221static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005222 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223{
Frank Filz52ccb8e2005-12-22 11:36:46 -08005224 struct sctp_paddrparams params;
5225 struct sctp_transport *trans = NULL;
5226 struct sctp_association *asoc = NULL;
5227 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228
Neil Horman408f22e2007-06-16 14:03:45 -04005229 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005231 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 if (copy_from_user(&params, optval, len))
5233 return -EFAULT;
5234
Frank Filz52ccb8e2005-12-22 11:36:46 -08005235 /* If an address other than INADDR_ANY is specified, and
5236 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 */
wangweidongcb3f8372013-12-23 12:16:50 +08005238 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005239 trans = sctp_addr_id2transport(sk, &params.spp_address,
5240 params.spp_assoc_id);
5241 if (!trans) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005242 pr_debug("%s: failed no transport\n", __func__);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005243 return -EINVAL;
5244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 }
5246
Frank Filz52ccb8e2005-12-22 11:36:46 -08005247 /* Get association, if assoc_id != 0 and the socket is a one
5248 * to many style socket, and an association was not found, then
5249 * the id was invalid.
5250 */
5251 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5252 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005253 pr_debug("%s: failed no association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08005255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
Frank Filz52ccb8e2005-12-22 11:36:46 -08005257 if (trans) {
5258 /* Fetch transport values. */
5259 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5260 params.spp_pathmtu = trans->pathmtu;
5261 params.spp_pathmaxrxt = trans->pathmaxrxt;
5262 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263
Frank Filz52ccb8e2005-12-22 11:36:46 -08005264 /*draft-11 doesn't say what to return in spp_flags*/
5265 params.spp_flags = trans->param_flags;
5266 } else if (asoc) {
5267 /* Fetch association values. */
5268 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5269 params.spp_pathmtu = asoc->pathmtu;
5270 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5271 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272
Frank Filz52ccb8e2005-12-22 11:36:46 -08005273 /*draft-11 doesn't say what to return in spp_flags*/
5274 params.spp_flags = asoc->param_flags;
5275 } else {
5276 /* Fetch socket values. */
5277 params.spp_hbinterval = sp->hbinterval;
5278 params.spp_pathmtu = sp->pathmtu;
5279 params.spp_sackdelay = sp->sackdelay;
5280 params.spp_pathmaxrxt = sp->pathmaxrxt;
5281
5282 /*draft-11 doesn't say what to return in spp_flags*/
5283 params.spp_flags = sp->param_flags;
5284 }
5285
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 if (copy_to_user(optval, &params, len))
5287 return -EFAULT;
5288
5289 if (put_user(len, optlen))
5290 return -EFAULT;
5291
5292 return 0;
5293}
5294
Wei Yongjund364d922008-05-09 15:13:26 -07005295/*
5296 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08005297 *
Wei Yongjund364d922008-05-09 15:13:26 -07005298 * This option will effect the way delayed acks are performed. This
5299 * option allows you to get or set the delayed ack time, in
5300 * milliseconds. It also allows changing the delayed ack frequency.
5301 * Changing the frequency to 1 disables the delayed sack algorithm. If
5302 * the assoc_id is 0, then this sets or gets the endpoints default
5303 * values. If the assoc_id field is non-zero, then the set or get
5304 * effects the specified association for the one to many model (the
5305 * assoc_id field is ignored by the one to one model). Note that if
5306 * sack_delay or sack_freq are 0 when setting this option, then the
5307 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08005308 *
Wei Yongjund364d922008-05-09 15:13:26 -07005309 * struct sctp_sack_info {
5310 * sctp_assoc_t sack_assoc_id;
5311 * uint32_t sack_delay;
5312 * uint32_t sack_freq;
5313 * };
Frank Filz77086102005-12-22 11:37:30 -08005314 *
Wei Yongjund364d922008-05-09 15:13:26 -07005315 * sack_assoc_id - This parameter, indicates which association the user
5316 * is performing an action upon. Note that if this field's value is
5317 * zero then the endpoints default value is changed (effecting future
5318 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08005319 *
Wei Yongjund364d922008-05-09 15:13:26 -07005320 * sack_delay - This parameter contains the number of milliseconds that
5321 * the user is requesting the delayed ACK timer be set to. Note that
5322 * this value is defined in the standard to be between 200 and 500
5323 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08005324 *
Wei Yongjund364d922008-05-09 15:13:26 -07005325 * sack_freq - This parameter contains the number of packets that must
5326 * be received before a sack is sent without waiting for the delay
5327 * timer to expire. The default value for this is 2, setting this
5328 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08005329 */
Wei Yongjund364d922008-05-09 15:13:26 -07005330static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08005331 char __user *optval,
5332 int __user *optlen)
5333{
Wei Yongjund364d922008-05-09 15:13:26 -07005334 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08005335 struct sctp_association *asoc = NULL;
5336 struct sctp_sock *sp = sctp_sk(sk);
5337
Wei Yongjund364d922008-05-09 15:13:26 -07005338 if (len >= sizeof(struct sctp_sack_info)) {
5339 len = sizeof(struct sctp_sack_info);
5340
5341 if (copy_from_user(&params, optval, len))
5342 return -EFAULT;
5343 } else if (len == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05005344 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005345 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005346 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005347 "Use struct sctp_sack_info instead\n",
5348 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07005349 if (copy_from_user(&params, optval, len))
5350 return -EFAULT;
5351 } else
wangweidongcb3f8372013-12-23 12:16:50 +08005352 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08005353
Wei Yongjund364d922008-05-09 15:13:26 -07005354 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08005355 * to many style socket, and an association was not found, then
5356 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005357 */
Wei Yongjund364d922008-05-09 15:13:26 -07005358 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5359 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08005360 return -EINVAL;
5361
5362 if (asoc) {
5363 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005364 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5365 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08005366 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07005367 params.sack_freq = asoc->sackfreq;
5368
5369 } else {
5370 params.sack_delay = 0;
5371 params.sack_freq = 1;
5372 }
Frank Filz77086102005-12-22 11:37:30 -08005373 } else {
5374 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005375 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5376 params.sack_delay = sp->sackdelay;
5377 params.sack_freq = sp->sackfreq;
5378 } else {
5379 params.sack_delay = 0;
5380 params.sack_freq = 1;
5381 }
Frank Filz77086102005-12-22 11:37:30 -08005382 }
5383
5384 if (copy_to_user(optval, &params, len))
5385 return -EFAULT;
5386
5387 if (put_user(len, optlen))
5388 return -EFAULT;
5389
5390 return 0;
5391}
5392
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5394 *
5395 * Applications can specify protocol parameters for the default association
5396 * initialization. The option name argument to setsockopt() and getsockopt()
5397 * is SCTP_INITMSG.
5398 *
5399 * Setting initialization parameters is effective only on an unconnected
5400 * socket (for UDP-style sockets only future associations are effected
5401 * by the change). With TCP-style sockets, this option is inherited by
5402 * sockets derived from a listener socket.
5403 */
5404static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5405{
Neil Horman408f22e2007-06-16 14:03:45 -04005406 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005408 len = sizeof(struct sctp_initmsg);
5409 if (put_user(len, optlen))
5410 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5412 return -EFAULT;
5413 return 0;
5414}
5415
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005417static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5418 char __user *optval, int __user *optlen)
5419{
5420 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005421 int cnt = 0;
5422 struct sctp_getaddrs getaddrs;
5423 struct sctp_transport *from;
5424 void __user *to;
5425 union sctp_addr temp;
5426 struct sctp_sock *sp = sctp_sk(sk);
5427 int addrlen;
5428 size_t space_left;
5429 int bytes_copied;
5430
5431 if (len < sizeof(struct sctp_getaddrs))
5432 return -EINVAL;
5433
5434 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5435 return -EFAULT;
5436
5437 /* For UDP-style sockets, id specifies the association to query. */
5438 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5439 if (!asoc)
5440 return -EINVAL;
5441
wangweidongcb3f8372013-12-23 12:16:50 +08005442 to = optval + offsetof(struct sctp_getaddrs, addrs);
5443 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005444
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07005445 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5446 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08005447 memcpy(&temp, &from->ipaddr, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005448 addrlen = sctp_get_pf_specific(sk->sk_family)
5449 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005450 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005451 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005452 if (copy_to_user(to, &temp, addrlen))
5453 return -EFAULT;
5454 to += addrlen;
5455 cnt++;
5456 space_left -= addrlen;
5457 }
5458
5459 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5460 return -EFAULT;
5461 bytes_copied = ((char __user *)to) - optval;
5462 if (put_user(bytes_copied, optlen))
5463 return -EFAULT;
5464
5465 return 0;
5466}
5467
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005468static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5469 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005470{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005471 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005472 union sctp_addr temp;
5473 int cnt = 0;
5474 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005475 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005476
Vlad Yasevich29303542007-09-16 16:02:12 -07005477 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005478 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07005479 if (!addr->valid)
5480 continue;
5481
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005482 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08005483 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005484 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005485 if ((PF_INET6 == sk->sk_family) &&
5486 inet_v6_ipv6only(sk) &&
5487 (AF_INET == addr->a.sa.sa_family))
5488 continue;
Al Viro6244be42006-11-20 17:21:44 -08005489 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05005490 if (!temp.v4.sin_port)
5491 temp.v4.sin_port = htons(port);
5492
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005493 addrlen = sctp_get_pf_specific(sk->sk_family)
5494 ->addr_to_user(sctp_sk(sk), &temp);
5495
Vlad Yasevich29303542007-09-16 16:02:12 -07005496 if (space_left < addrlen) {
5497 cnt = -ENOMEM;
5498 break;
5499 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005500 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08005501
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005502 to += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005503 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005504 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04005505 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005506 }
Vlad Yasevich29303542007-09-16 16:02:12 -07005507 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005508
5509 return cnt;
5510}
5511
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005513static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5514 char __user *optval, int __user *optlen)
5515{
5516 struct sctp_bind_addr *bp;
5517 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005518 int cnt = 0;
5519 struct sctp_getaddrs getaddrs;
5520 struct sctp_sockaddr_entry *addr;
5521 void __user *to;
5522 union sctp_addr temp;
5523 struct sctp_sock *sp = sctp_sk(sk);
5524 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005525 int err = 0;
5526 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005527 int bytes_copied = 0;
5528 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005529 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005530
Neil Horman408f22e2007-06-16 14:03:45 -04005531 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005532 return -EINVAL;
5533
5534 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5535 return -EFAULT;
5536
5537 /*
5538 * For UDP-style sockets, id specifies the association to query.
5539 * If the id field is set to the value '0' then the locally bound
5540 * addresses are returned without regard to any particular
5541 * association.
5542 */
5543 if (0 == getaddrs.assoc_id) {
5544 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005545 } else {
5546 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5547 if (!asoc)
5548 return -EINVAL;
5549 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005550 }
5551
wangweidongcb3f8372013-12-23 12:16:50 +08005552 to = optval + offsetof(struct sctp_getaddrs, addrs);
5553 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Neil Horman186e2342007-06-18 19:59:16 -04005554
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02005555 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005556 if (!addrs)
5557 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005558
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005559 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5560 * addresses from the global local address list.
5561 */
5562 if (sctp_list_single_entry(&bp->address_list)) {
5563 addr = list_entry(bp->address_list.next,
5564 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04005565 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005566 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5567 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005568 if (cnt < 0) {
5569 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005570 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005571 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005572 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005573 }
5574 }
5575
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005576 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005577 /* Protection on the bound address list is not needed since
5578 * in the socket option context we hold a socket lock and
5579 * thus the bound address list can't change.
5580 */
5581 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08005582 memcpy(&temp, &addr->a, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005583 addrlen = sctp_get_pf_specific(sk->sk_family)
5584 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005585 if (space_left < addrlen) {
5586 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07005587 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005588 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005589 memcpy(buf, &temp, addrlen);
5590 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005591 bytes_copied += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005592 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005593 space_left -= addrlen;
5594 }
5595
5596copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005597 if (copy_to_user(to, addrs, bytes_copied)) {
5598 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005599 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005600 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005601 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
5602 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005603 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005604 }
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02005605 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
5606 * but we can't change it anymore.
5607 */
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005608 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005609 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005610out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005611 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005612 return err;
5613}
5614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
5616 *
5617 * Requests that the local SCTP stack use the enclosed peer address as
5618 * the association primary. The enclosed address must be one of the
5619 * association peer's addresses.
5620 */
5621static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
5622 char __user *optval, int __user *optlen)
5623{
5624 struct sctp_prim prim;
5625 struct sctp_association *asoc;
5626 struct sctp_sock *sp = sctp_sk(sk);
5627
Neil Horman408f22e2007-06-16 14:03:45 -04005628 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 return -EINVAL;
5630
Neil Horman408f22e2007-06-16 14:03:45 -04005631 len = sizeof(struct sctp_prim);
5632
5633 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 return -EFAULT;
5635
5636 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
5637 if (!asoc)
5638 return -EINVAL;
5639
5640 if (!asoc->peer.primary_path)
5641 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005642
Al Viro8cec6b82006-11-20 17:23:01 -08005643 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
5644 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005646 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 (union sctp_addr *)&prim.ssp_addr);
5648
Neil Horman408f22e2007-06-16 14:03:45 -04005649 if (put_user(len, optlen))
5650 return -EFAULT;
5651 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 return -EFAULT;
5653
5654 return 0;
5655}
5656
5657/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005658 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005660 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 * Indication parameter for all future INIT and INIT-ACK exchanges.
5662 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005663static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 char __user *optval, int __user *optlen)
5665{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005666 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
Neil Horman408f22e2007-06-16 14:03:45 -04005668 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 return -EINVAL;
5670
Neil Horman408f22e2007-06-16 14:03:45 -04005671 len = sizeof(struct sctp_setadaptation);
5672
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005673 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04005674
5675 if (put_user(len, optlen))
5676 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005677 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07005679
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 return 0;
5681}
5682
5683/*
5684 *
5685 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
5686 *
5687 * Applications that wish to use the sendto() system call may wish to
5688 * specify a default set of parameters that would normally be supplied
5689 * through the inclusion of ancillary data. This socket option allows
5690 * such an application to set the default sctp_sndrcvinfo structure.
5691
5692
5693 * The application that wishes to use this socket option simply passes
5694 * in to this call the sctp_sndrcvinfo structure defined in Section
5695 * 5.2.2) The input parameters accepted by this call include
5696 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
5697 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
5698 * to this call if the caller is using the UDP model.
5699 *
5700 * For getsockopt, it get the default sctp_sndrcvinfo structure.
5701 */
5702static int sctp_getsockopt_default_send_param(struct sock *sk,
5703 int len, char __user *optval,
5704 int __user *optlen)
5705{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005707 struct sctp_association *asoc;
5708 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005710 if (len < sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005712
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005713 len = sizeof(info);
Neil Horman408f22e2007-06-16 14:03:45 -04005714
5715 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 return -EFAULT;
5717
5718 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
5719 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
5720 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 if (asoc) {
5722 info.sinfo_stream = asoc->default_stream;
5723 info.sinfo_flags = asoc->default_flags;
5724 info.sinfo_ppid = asoc->default_ppid;
5725 info.sinfo_context = asoc->default_context;
5726 info.sinfo_timetolive = asoc->default_timetolive;
5727 } else {
5728 info.sinfo_stream = sp->default_stream;
5729 info.sinfo_flags = sp->default_flags;
5730 info.sinfo_ppid = sp->default_ppid;
5731 info.sinfo_context = sp->default_context;
5732 info.sinfo_timetolive = sp->default_timetolive;
5733 }
5734
Neil Horman408f22e2007-06-16 14:03:45 -04005735 if (put_user(len, optlen))
5736 return -EFAULT;
5737 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 return -EFAULT;
5739
5740 return 0;
5741}
5742
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005743/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
5744 * (SCTP_DEFAULT_SNDINFO)
5745 */
5746static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
5747 char __user *optval,
5748 int __user *optlen)
5749{
5750 struct sctp_sock *sp = sctp_sk(sk);
5751 struct sctp_association *asoc;
5752 struct sctp_sndinfo info;
5753
5754 if (len < sizeof(info))
5755 return -EINVAL;
5756
5757 len = sizeof(info);
5758
5759 if (copy_from_user(&info, optval, len))
5760 return -EFAULT;
5761
5762 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
5763 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
5764 return -EINVAL;
5765 if (asoc) {
5766 info.snd_sid = asoc->default_stream;
5767 info.snd_flags = asoc->default_flags;
5768 info.snd_ppid = asoc->default_ppid;
5769 info.snd_context = asoc->default_context;
5770 } else {
5771 info.snd_sid = sp->default_stream;
5772 info.snd_flags = sp->default_flags;
5773 info.snd_ppid = sp->default_ppid;
5774 info.snd_context = sp->default_context;
5775 }
5776
5777 if (put_user(len, optlen))
5778 return -EFAULT;
5779 if (copy_to_user(optval, &info, len))
5780 return -EFAULT;
5781
5782 return 0;
5783}
5784
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785/*
5786 *
5787 * 7.1.5 SCTP_NODELAY
5788 *
5789 * Turn on/off any Nagle-like algorithm. This means that packets are
5790 * generally sent as soon as possible and no unnecessary delays are
5791 * introduced, at the cost of more packets in the network. Expects an
5792 * integer boolean flag.
5793 */
5794
5795static int sctp_getsockopt_nodelay(struct sock *sk, int len,
5796 char __user *optval, int __user *optlen)
5797{
5798 int val;
5799
5800 if (len < sizeof(int))
5801 return -EINVAL;
5802
5803 len = sizeof(int);
5804 val = (sctp_sk(sk)->nodelay == 1);
5805 if (put_user(len, optlen))
5806 return -EFAULT;
5807 if (copy_to_user(optval, &val, len))
5808 return -EFAULT;
5809 return 0;
5810}
5811
5812/*
5813 *
5814 * 7.1.1 SCTP_RTOINFO
5815 *
5816 * The protocol parameters used to initialize and bound retransmission
5817 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
5818 * and modify these parameters.
5819 * All parameters are time values, in milliseconds. A value of 0, when
5820 * modifying the parameters, indicates that the current value should not
5821 * be changed.
5822 *
5823 */
5824static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
5825 char __user *optval,
5826 int __user *optlen) {
5827 struct sctp_rtoinfo rtoinfo;
5828 struct sctp_association *asoc;
5829
Neil Horman408f22e2007-06-16 14:03:45 -04005830 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 return -EINVAL;
5832
Neil Horman408f22e2007-06-16 14:03:45 -04005833 len = sizeof(struct sctp_rtoinfo);
5834
5835 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 return -EFAULT;
5837
5838 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
5839
5840 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
5841 return -EINVAL;
5842
5843 /* Values corresponding to the specific association. */
5844 if (asoc) {
5845 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
5846 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
5847 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
5848 } else {
5849 /* Values corresponding to the endpoint. */
5850 struct sctp_sock *sp = sctp_sk(sk);
5851
5852 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
5853 rtoinfo.srto_max = sp->rtoinfo.srto_max;
5854 rtoinfo.srto_min = sp->rtoinfo.srto_min;
5855 }
5856
5857 if (put_user(len, optlen))
5858 return -EFAULT;
5859
5860 if (copy_to_user(optval, &rtoinfo, len))
5861 return -EFAULT;
5862
5863 return 0;
5864}
5865
5866/*
5867 *
5868 * 7.1.2 SCTP_ASSOCINFO
5869 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02005870 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 * of the association.
5872 * Returns an error if the new association retransmission value is
5873 * greater than the sum of the retransmission value of the peer.
5874 * See [SCTP] for more information.
5875 *
5876 */
5877static int sctp_getsockopt_associnfo(struct sock *sk, int len,
5878 char __user *optval,
5879 int __user *optlen)
5880{
5881
5882 struct sctp_assocparams assocparams;
5883 struct sctp_association *asoc;
5884 struct list_head *pos;
5885 int cnt = 0;
5886
Neil Horman408f22e2007-06-16 14:03:45 -04005887 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 return -EINVAL;
5889
Neil Horman408f22e2007-06-16 14:03:45 -04005890 len = sizeof(struct sctp_assocparams);
5891
5892 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 return -EFAULT;
5894
5895 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
5896
5897 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
5898 return -EINVAL;
5899
5900 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07005901 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
5903 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
5904 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
Daniel Borkmann52db8822013-06-25 18:17:27 +02005905 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906
5907 list_for_each(pos, &asoc->peer.transport_addr_list) {
wangweidongcb3f8372013-12-23 12:16:50 +08005908 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 }
5910
5911 assocparams.sasoc_number_peer_destinations = cnt;
5912 } else {
5913 /* Values corresponding to the endpoint */
5914 struct sctp_sock *sp = sctp_sk(sk);
5915
5916 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
5917 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
5918 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
5919 assocparams.sasoc_cookie_life =
5920 sp->assocparams.sasoc_cookie_life;
5921 assocparams.sasoc_number_peer_destinations =
5922 sp->assocparams.
5923 sasoc_number_peer_destinations;
5924 }
5925
5926 if (put_user(len, optlen))
5927 return -EFAULT;
5928
5929 if (copy_to_user(optval, &assocparams, len))
5930 return -EFAULT;
5931
5932 return 0;
5933}
5934
5935/*
5936 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
5937 *
5938 * This socket option is a boolean flag which turns on or off mapped V4
5939 * addresses. If this option is turned on and the socket is type
5940 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
5941 * If this option is turned off, then no mapping will be done of V4
5942 * addresses and a user will receive both PF_INET6 and PF_INET type
5943 * addresses on the socket.
5944 */
5945static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
5946 char __user *optval, int __user *optlen)
5947{
5948 int val;
5949 struct sctp_sock *sp = sctp_sk(sk);
5950
5951 if (len < sizeof(int))
5952 return -EINVAL;
5953
5954 len = sizeof(int);
5955 val = sp->v4mapped;
5956 if (put_user(len, optlen))
5957 return -EFAULT;
5958 if (copy_to_user(optval, &val, len))
5959 return -EFAULT;
5960
5961 return 0;
5962}
5963
5964/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005965 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
5966 * (chapter and verse is quoted at sctp_setsockopt_context())
5967 */
5968static int sctp_getsockopt_context(struct sock *sk, int len,
5969 char __user *optval, int __user *optlen)
5970{
5971 struct sctp_assoc_value params;
5972 struct sctp_sock *sp;
5973 struct sctp_association *asoc;
5974
Neil Horman408f22e2007-06-16 14:03:45 -04005975 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005976 return -EINVAL;
5977
Neil Horman408f22e2007-06-16 14:03:45 -04005978 len = sizeof(struct sctp_assoc_value);
5979
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005980 if (copy_from_user(&params, optval, len))
5981 return -EFAULT;
5982
5983 sp = sctp_sk(sk);
5984
5985 if (params.assoc_id != 0) {
5986 asoc = sctp_id2assoc(sk, params.assoc_id);
5987 if (!asoc)
5988 return -EINVAL;
5989 params.assoc_value = asoc->default_rcv_context;
5990 } else {
5991 params.assoc_value = sp->default_rcv_context;
5992 }
5993
5994 if (put_user(len, optlen))
5995 return -EFAULT;
5996 if (copy_to_user(optval, &params, len))
5997 return -EFAULT;
5998
5999 return 0;
6000}
6001
6002/*
Wei Yongjune89c2092008-12-25 16:54:58 -08006003 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6004 * This option will get or set the maximum size to put in any outgoing
6005 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 * fragmented by SCTP into the specified size. Note that the underlying
6007 * SCTP implementation may fragment into smaller sized chunks when the
6008 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08006009 * the user. The default value for this option is '0' which indicates
6010 * the user is NOT limiting fragmentation and only the PMTU will effect
6011 * SCTP's choice of DATA chunk size. Note also that values set larger
6012 * than the maximum size of an IP datagram will effectively let SCTP
6013 * control fragmentation (i.e. the same as setting this option to 0).
6014 *
6015 * The following structure is used to access and modify this parameter:
6016 *
6017 * struct sctp_assoc_value {
6018 * sctp_assoc_t assoc_id;
6019 * uint32_t assoc_value;
6020 * };
6021 *
6022 * assoc_id: This parameter is ignored for one-to-one style sockets.
6023 * For one-to-many style sockets this parameter indicates which
6024 * association the user is performing an action upon. Note that if
6025 * this field's value is zero then the endpoints default value is
6026 * changed (effecting future associations only).
6027 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 */
6029static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6030 char __user *optval, int __user *optlen)
6031{
Wei Yongjune89c2092008-12-25 16:54:58 -08006032 struct sctp_assoc_value params;
6033 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
Wei Yongjune89c2092008-12-25 16:54:58 -08006035 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006036 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006037 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006038 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006039 "Use struct sctp_assoc_value instead\n",
6040 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08006041 params.assoc_id = 0;
6042 } else if (len >= sizeof(struct sctp_assoc_value)) {
6043 len = sizeof(struct sctp_assoc_value);
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006044 if (copy_from_user(&params, optval, len))
Wei Yongjune89c2092008-12-25 16:54:58 -08006045 return -EFAULT;
6046 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 return -EINVAL;
6048
Wei Yongjune89c2092008-12-25 16:54:58 -08006049 asoc = sctp_id2assoc(sk, params.assoc_id);
6050 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
6051 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
Wei Yongjune89c2092008-12-25 16:54:58 -08006053 if (asoc)
6054 params.assoc_value = asoc->frag_point;
6055 else
6056 params.assoc_value = sctp_sk(sk)->user_frag;
6057
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 if (put_user(len, optlen))
6059 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08006060 if (len == sizeof(int)) {
6061 if (copy_to_user(optval, &params.assoc_value, len))
6062 return -EFAULT;
6063 } else {
6064 if (copy_to_user(optval, &params, len))
6065 return -EFAULT;
6066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
6068 return 0;
6069}
6070
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006071/*
6072 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6073 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6074 */
6075static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6076 char __user *optval, int __user *optlen)
6077{
6078 int val;
6079
6080 if (len < sizeof(int))
6081 return -EINVAL;
6082
6083 len = sizeof(int);
6084
6085 val = sctp_sk(sk)->frag_interleave;
6086 if (put_user(len, optlen))
6087 return -EFAULT;
6088 if (copy_to_user(optval, &val, len))
6089 return -EFAULT;
6090
6091 return 0;
6092}
6093
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006094/*
6095 * 7.1.25. Set or Get the sctp partial delivery point
6096 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6097 */
6098static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6099 char __user *optval,
6100 int __user *optlen)
6101{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09006102 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006103
6104 if (len < sizeof(u32))
6105 return -EINVAL;
6106
6107 len = sizeof(u32);
6108
6109 val = sctp_sk(sk)->pd_point;
6110 if (put_user(len, optlen))
6111 return -EFAULT;
6112 if (copy_to_user(optval, &val, len))
6113 return -EFAULT;
6114
Wei Yongjun7d743b72010-12-14 16:10:41 +00006115 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006116}
6117
Vlad Yasevich70331572007-03-23 11:34:36 -07006118/*
6119 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6120 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6121 */
6122static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6123 char __user *optval,
6124 int __user *optlen)
6125{
Neil Horman219b99a2008-03-05 13:44:46 -08006126 struct sctp_assoc_value params;
6127 struct sctp_sock *sp;
6128 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07006129
Neil Horman219b99a2008-03-05 13:44:46 -08006130 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006131 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006132 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006133 "Use of int in max_burst socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006134 "Use struct sctp_assoc_value instead\n",
6135 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08006136 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00006137 } else if (len >= sizeof(struct sctp_assoc_value)) {
6138 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08006139 if (copy_from_user(&params, optval, len))
6140 return -EFAULT;
6141 } else
6142 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07006143
Neil Horman219b99a2008-03-05 13:44:46 -08006144 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07006145
Neil Horman219b99a2008-03-05 13:44:46 -08006146 if (params.assoc_id != 0) {
6147 asoc = sctp_id2assoc(sk, params.assoc_id);
6148 if (!asoc)
6149 return -EINVAL;
6150 params.assoc_value = asoc->max_burst;
6151 } else
6152 params.assoc_value = sp->max_burst;
6153
6154 if (len == sizeof(int)) {
6155 if (copy_to_user(optval, &params.assoc_value, len))
6156 return -EFAULT;
6157 } else {
6158 if (copy_to_user(optval, &params, len))
6159 return -EFAULT;
6160 }
6161
6162 return 0;
6163
Vlad Yasevich70331572007-03-23 11:34:36 -07006164}
6165
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006166static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6167 char __user *optval, int __user *optlen)
6168{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006169 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006170 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006171 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006172 __u16 data_len = 0;
6173 u32 num_idents;
Xin Long7a84bd42016-02-03 23:33:30 +08006174 int i;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006175
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006176 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006177 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006178
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006179 hmacs = ep->auth_hmacs_list;
Xin Long3c918702017-06-30 11:52:16 +08006180 data_len = ntohs(hmacs->param_hdr.length) -
6181 sizeof(struct sctp_paramhdr);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006182
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006183 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006184 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006185
6186 len = sizeof(struct sctp_hmacalgo) + data_len;
6187 num_idents = data_len / sizeof(u16);
6188
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006189 if (put_user(len, optlen))
6190 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006191 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006192 return -EFAULT;
Xin Long7a84bd42016-02-03 23:33:30 +08006193 for (i = 0; i < num_idents; i++) {
6194 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6195
6196 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6197 return -EFAULT;
6198 }
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006199 return 0;
6200}
6201
6202static int sctp_getsockopt_active_key(struct sock *sk, int len,
6203 char __user *optval, int __user *optlen)
6204{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006205 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006206 struct sctp_authkeyid val;
6207 struct sctp_association *asoc;
6208
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006209 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006210 return -EACCES;
6211
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006212 if (len < sizeof(struct sctp_authkeyid))
6213 return -EINVAL;
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006214
6215 len = sizeof(struct sctp_authkeyid);
6216 if (copy_from_user(&val, optval, len))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006217 return -EFAULT;
6218
6219 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6220 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6221 return -EINVAL;
6222
6223 if (asoc)
6224 val.scact_keynumber = asoc->active_key_id;
6225 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006226 val.scact_keynumber = ep->active_key_id;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006227
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006228 if (put_user(len, optlen))
6229 return -EFAULT;
6230 if (copy_to_user(optval, &val, len))
6231 return -EFAULT;
6232
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006233 return 0;
6234}
6235
6236static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6237 char __user *optval, int __user *optlen)
6238{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006239 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006240 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006241 struct sctp_authchunks val;
6242 struct sctp_association *asoc;
6243 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006244 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006245 char __user *to;
6246
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006247 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006248 return -EACCES;
6249
6250 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006251 return -EINVAL;
6252
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006253 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006254 return -EFAULT;
6255
Al Viro411223c2007-10-14 19:21:20 +01006256 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006257 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6258 if (!asoc)
6259 return -EINVAL;
6260
6261 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006262 if (!ch)
6263 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006264
6265 /* See if the user provided enough room for all the data */
Xin Long3c918702017-06-30 11:52:16 +08006266 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevichb40db682008-02-27 14:40:37 -05006267 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006268 return -EINVAL;
6269
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006270 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006271 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006272num:
6273 len = sizeof(struct sctp_authchunks) + num_chunks;
wangweidong8d726512013-12-23 12:16:53 +08006274 if (put_user(len, optlen))
6275 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006276 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6277 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006278 return 0;
6279}
6280
6281static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6282 char __user *optval, int __user *optlen)
6283{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006284 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006285 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006286 struct sctp_authchunks val;
6287 struct sctp_association *asoc;
6288 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006289 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006290 char __user *to;
6291
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006292 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006293 return -EACCES;
6294
6295 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006296 return -EINVAL;
6297
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006298 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006299 return -EFAULT;
6300
Al Viro411223c2007-10-14 19:21:20 +01006301 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006302 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6303 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6304 return -EINVAL;
6305
6306 if (asoc)
wangweidong26ac8e52013-12-23 12:16:51 +08006307 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006308 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006309 ch = ep->auth_chunk_list;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006310
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006311 if (!ch)
6312 goto num;
6313
Xin Long3c918702017-06-30 11:52:16 +08006314 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006315 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006316 return -EINVAL;
6317
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006318 if (copy_to_user(to, ch->chunks, num_chunks))
6319 return -EFAULT;
6320num:
6321 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006322 if (put_user(len, optlen))
6323 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006324 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6325 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006326
6327 return 0;
6328}
6329
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08006330/*
6331 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6332 * This option gets the current number of associations that are attached
6333 * to a one-to-many style socket. The option value is an uint32_t.
6334 */
6335static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6336 char __user *optval, int __user *optlen)
6337{
6338 struct sctp_sock *sp = sctp_sk(sk);
6339 struct sctp_association *asoc;
6340 u32 val = 0;
6341
6342 if (sctp_style(sk, TCP))
6343 return -EOPNOTSUPP;
6344
6345 if (len < sizeof(u32))
6346 return -EINVAL;
6347
6348 len = sizeof(u32);
6349
6350 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6351 val++;
6352 }
6353
6354 if (put_user(len, optlen))
6355 return -EFAULT;
6356 if (copy_to_user(optval, &val, len))
6357 return -EFAULT;
6358
6359 return 0;
6360}
6361
Wei Yongjun209ba422011-04-17 17:27:08 +00006362/*
Michio Honda7dc04d72011-04-26 20:16:31 +09006363 * 8.1.23 SCTP_AUTO_ASCONF
6364 * See the corresponding setsockopt entry as description
6365 */
6366static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6367 char __user *optval, int __user *optlen)
6368{
6369 int val = 0;
6370
6371 if (len < sizeof(int))
6372 return -EINVAL;
6373
6374 len = sizeof(int);
6375 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6376 val = 1;
6377 if (put_user(len, optlen))
6378 return -EFAULT;
6379 if (copy_to_user(optval, &val, len))
6380 return -EFAULT;
6381 return 0;
6382}
6383
6384/*
Wei Yongjun209ba422011-04-17 17:27:08 +00006385 * 8.2.6. Get the Current Identifiers of Associations
6386 * (SCTP_GET_ASSOC_ID_LIST)
6387 *
6388 * This option gets the current list of SCTP association identifiers of
6389 * the SCTP associations handled by a one-to-many style socket.
6390 */
6391static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6392 char __user *optval, int __user *optlen)
6393{
6394 struct sctp_sock *sp = sctp_sk(sk);
6395 struct sctp_association *asoc;
6396 struct sctp_assoc_ids *ids;
6397 u32 num = 0;
6398
6399 if (sctp_style(sk, TCP))
6400 return -EOPNOTSUPP;
6401
6402 if (len < sizeof(struct sctp_assoc_ids))
6403 return -EINVAL;
6404
6405 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6406 num++;
6407 }
6408
6409 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6410 return -EINVAL;
6411
6412 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6413
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02006414 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
Wei Yongjun209ba422011-04-17 17:27:08 +00006415 if (unlikely(!ids))
6416 return -ENOMEM;
6417
6418 ids->gaids_number_of_ids = num;
6419 num = 0;
6420 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6421 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6422 }
6423
6424 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6425 kfree(ids);
6426 return -EFAULT;
6427 }
6428
6429 kfree(ids);
6430 return 0;
6431}
6432
Neil Horman5aa93bc2012-07-21 07:56:07 +00006433/*
6434 * SCTP_PEER_ADDR_THLDS
6435 *
6436 * This option allows us to fetch the partially failed threshold for one or all
6437 * transports in an association. See Section 6.1 of:
6438 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6439 */
6440static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6441 char __user *optval,
6442 int len,
6443 int __user *optlen)
6444{
6445 struct sctp_paddrthlds val;
6446 struct sctp_transport *trans;
6447 struct sctp_association *asoc;
6448
6449 if (len < sizeof(struct sctp_paddrthlds))
6450 return -EINVAL;
6451 len = sizeof(struct sctp_paddrthlds);
6452 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6453 return -EFAULT;
6454
6455 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6456 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6457 if (!asoc)
6458 return -ENOENT;
6459
6460 val.spt_pathpfthld = asoc->pf_retrans;
6461 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6462 } else {
6463 trans = sctp_addr_id2transport(sk, &val.spt_address,
6464 val.spt_assoc_id);
6465 if (!trans)
6466 return -ENOENT;
6467
6468 val.spt_pathmaxrxt = trans->pathmaxrxt;
6469 val.spt_pathpfthld = trans->pf_retrans;
6470 }
6471
6472 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6473 return -EFAULT;
6474
6475 return 0;
6476}
6477
Michele Baldessari196d6752012-12-01 04:49:42 +00006478/*
6479 * SCTP_GET_ASSOC_STATS
6480 *
6481 * This option retrieves local per endpoint statistics. It is modeled
6482 * after OpenSolaris' implementation
6483 */
6484static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6485 char __user *optval,
6486 int __user *optlen)
6487{
6488 struct sctp_assoc_stats sas;
6489 struct sctp_association *asoc = NULL;
6490
6491 /* User must provide at least the assoc id */
6492 if (len < sizeof(sctp_assoc_t))
6493 return -EINVAL;
6494
Guenter Roeck726bc6b2013-02-27 10:57:31 +00006495 /* Allow the struct to grow and fill in as much as possible */
6496 len = min_t(size_t, len, sizeof(sas));
6497
Michele Baldessari196d6752012-12-01 04:49:42 +00006498 if (copy_from_user(&sas, optval, len))
6499 return -EFAULT;
6500
6501 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6502 if (!asoc)
6503 return -EINVAL;
6504
6505 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6506 sas.sas_gapcnt = asoc->stats.gapcnt;
6507 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6508 sas.sas_osacks = asoc->stats.osacks;
6509 sas.sas_isacks = asoc->stats.isacks;
6510 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6511 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6512 sas.sas_oodchunks = asoc->stats.oodchunks;
6513 sas.sas_iodchunks = asoc->stats.iodchunks;
6514 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6515 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6516 sas.sas_idupchunks = asoc->stats.idupchunks;
6517 sas.sas_opackets = asoc->stats.opackets;
6518 sas.sas_ipackets = asoc->stats.ipackets;
6519
6520 /* New high max rto observed, will return 0 if not a single
6521 * RTO update took place. obs_rto_ipaddr will be bogus
6522 * in such a case
6523 */
6524 sas.sas_maxrto = asoc->stats.max_obs_rto;
6525 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6526 sizeof(struct sockaddr_storage));
6527
6528 /* Mark beginning of a new observation period */
6529 asoc->stats.max_obs_rto = asoc->rto_min;
6530
Michele Baldessari196d6752012-12-01 04:49:42 +00006531 if (put_user(len, optlen))
6532 return -EFAULT;
6533
Daniel Borkmannbb333812013-06-28 19:49:40 +02006534 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
Michele Baldessari196d6752012-12-01 04:49:42 +00006535
6536 if (copy_to_user(optval, &sas, len))
6537 return -EFAULT;
6538
6539 return 0;
6540}
6541
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02006542static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6543 char __user *optval,
6544 int __user *optlen)
6545{
6546 int val = 0;
6547
6548 if (len < sizeof(int))
6549 return -EINVAL;
6550
6551 len = sizeof(int);
6552 if (sctp_sk(sk)->recvrcvinfo)
6553 val = 1;
6554 if (put_user(len, optlen))
6555 return -EFAULT;
6556 if (copy_to_user(optval, &val, len))
6557 return -EFAULT;
6558
6559 return 0;
6560}
6561
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02006562static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6563 char __user *optval,
6564 int __user *optlen)
6565{
6566 int val = 0;
6567
6568 if (len < sizeof(int))
6569 return -EINVAL;
6570
6571 len = sizeof(int);
6572 if (sctp_sk(sk)->recvnxtinfo)
6573 val = 1;
6574 if (put_user(len, optlen))
6575 return -EFAULT;
6576 if (copy_to_user(optval, &val, len))
6577 return -EFAULT;
6578
6579 return 0;
6580}
6581
Xin Long28aa4c22016-07-09 19:47:40 +08006582static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6583 char __user *optval,
6584 int __user *optlen)
6585{
6586 struct sctp_assoc_value params;
6587 struct sctp_association *asoc;
6588 int retval = -EFAULT;
6589
6590 if (len < sizeof(params)) {
6591 retval = -EINVAL;
6592 goto out;
6593 }
6594
6595 len = sizeof(params);
6596 if (copy_from_user(&params, optval, len))
6597 goto out;
6598
6599 asoc = sctp_id2assoc(sk, params.assoc_id);
6600 if (asoc) {
6601 params.assoc_value = asoc->prsctp_enable;
6602 } else if (!params.assoc_id) {
6603 struct sctp_sock *sp = sctp_sk(sk);
6604
6605 params.assoc_value = sp->ep->prsctp_enable;
6606 } else {
6607 retval = -EINVAL;
6608 goto out;
6609 }
6610
6611 if (put_user(len, optlen))
6612 goto out;
6613
6614 if (copy_to_user(optval, &params, len))
6615 goto out;
6616
6617 retval = 0;
6618
6619out:
6620 return retval;
6621}
6622
Xin Longf959fb42016-07-09 19:47:41 +08006623static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
6624 char __user *optval,
6625 int __user *optlen)
6626{
6627 struct sctp_default_prinfo info;
6628 struct sctp_association *asoc;
6629 int retval = -EFAULT;
6630
6631 if (len < sizeof(info)) {
6632 retval = -EINVAL;
6633 goto out;
6634 }
6635
6636 len = sizeof(info);
6637 if (copy_from_user(&info, optval, len))
6638 goto out;
6639
6640 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
6641 if (asoc) {
6642 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
6643 info.pr_value = asoc->default_timetolive;
6644 } else if (!info.pr_assoc_id) {
6645 struct sctp_sock *sp = sctp_sk(sk);
6646
6647 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
6648 info.pr_value = sp->default_timetolive;
6649 } else {
6650 retval = -EINVAL;
6651 goto out;
6652 }
6653
6654 if (put_user(len, optlen))
6655 goto out;
6656
6657 if (copy_to_user(optval, &info, len))
6658 goto out;
6659
6660 retval = 0;
6661
6662out:
6663 return retval;
6664}
6665
Xin Long826d2532016-07-09 19:47:42 +08006666static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
6667 char __user *optval,
6668 int __user *optlen)
6669{
6670 struct sctp_prstatus params;
6671 struct sctp_association *asoc;
6672 int policy;
6673 int retval = -EINVAL;
6674
6675 if (len < sizeof(params))
6676 goto out;
6677
6678 len = sizeof(params);
6679 if (copy_from_user(&params, optval, len)) {
6680 retval = -EFAULT;
6681 goto out;
6682 }
6683
6684 policy = params.sprstat_policy;
6685 if (policy & ~SCTP_PR_SCTP_MASK)
6686 goto out;
6687
6688 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
6689 if (!asoc)
6690 goto out;
6691
6692 if (policy == SCTP_PR_SCTP_NONE) {
6693 params.sprstat_abandoned_unsent = 0;
6694 params.sprstat_abandoned_sent = 0;
6695 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6696 params.sprstat_abandoned_unsent +=
6697 asoc->abandoned_unsent[policy];
6698 params.sprstat_abandoned_sent +=
6699 asoc->abandoned_sent[policy];
6700 }
6701 } else {
6702 params.sprstat_abandoned_unsent =
6703 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
6704 params.sprstat_abandoned_sent =
6705 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
6706 }
6707
6708 if (put_user(len, optlen)) {
6709 retval = -EFAULT;
6710 goto out;
6711 }
6712
6713 if (copy_to_user(optval, &params, len)) {
6714 retval = -EFAULT;
6715 goto out;
6716 }
6717
6718 retval = 0;
6719
6720out:
6721 return retval;
6722}
6723
Xin Longd229d482017-04-01 17:07:46 +08006724static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
6725 char __user *optval,
6726 int __user *optlen)
6727{
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006728 struct sctp_stream_out_ext *streamoute;
Xin Longd229d482017-04-01 17:07:46 +08006729 struct sctp_association *asoc;
6730 struct sctp_prstatus params;
6731 int retval = -EINVAL;
6732 int policy;
6733
6734 if (len < sizeof(params))
6735 goto out;
6736
6737 len = sizeof(params);
6738 if (copy_from_user(&params, optval, len)) {
6739 retval = -EFAULT;
6740 goto out;
6741 }
6742
6743 policy = params.sprstat_policy;
6744 if (policy & ~SCTP_PR_SCTP_MASK)
6745 goto out;
6746
6747 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
Xin Longcee360a2017-05-31 16:36:31 +08006748 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
Xin Longd229d482017-04-01 17:07:46 +08006749 goto out;
6750
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006751 streamoute = asoc->stream.out[params.sprstat_sid].ext;
6752 if (!streamoute) {
6753 /* Not allocated yet, means all stats are 0 */
6754 params.sprstat_abandoned_unsent = 0;
6755 params.sprstat_abandoned_sent = 0;
6756 retval = 0;
6757 goto out;
6758 }
6759
Xin Longd229d482017-04-01 17:07:46 +08006760 if (policy == SCTP_PR_SCTP_NONE) {
6761 params.sprstat_abandoned_unsent = 0;
6762 params.sprstat_abandoned_sent = 0;
6763 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6764 params.sprstat_abandoned_unsent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006765 streamoute->abandoned_unsent[policy];
Xin Longd229d482017-04-01 17:07:46 +08006766 params.sprstat_abandoned_sent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006767 streamoute->abandoned_sent[policy];
Xin Longd229d482017-04-01 17:07:46 +08006768 }
6769 } else {
6770 params.sprstat_abandoned_unsent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006771 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08006772 params.sprstat_abandoned_sent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006773 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08006774 }
6775
6776 if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
6777 retval = -EFAULT;
6778 goto out;
6779 }
6780
6781 retval = 0;
6782
6783out:
6784 return retval;
6785}
6786
Xin Longc0d8bab2017-03-10 12:11:12 +08006787static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
6788 char __user *optval,
6789 int __user *optlen)
6790{
6791 struct sctp_assoc_value params;
6792 struct sctp_association *asoc;
6793 int retval = -EFAULT;
6794
6795 if (len < sizeof(params)) {
6796 retval = -EINVAL;
6797 goto out;
6798 }
6799
6800 len = sizeof(params);
6801 if (copy_from_user(&params, optval, len))
6802 goto out;
6803
6804 asoc = sctp_id2assoc(sk, params.assoc_id);
6805 if (asoc) {
6806 params.assoc_value = asoc->reconf_enable;
6807 } else if (!params.assoc_id) {
6808 struct sctp_sock *sp = sctp_sk(sk);
6809
6810 params.assoc_value = sp->ep->reconf_enable;
6811 } else {
6812 retval = -EINVAL;
6813 goto out;
6814 }
6815
6816 if (put_user(len, optlen))
6817 goto out;
6818
6819 if (copy_to_user(optval, &params, len))
6820 goto out;
6821
6822 retval = 0;
6823
6824out:
6825 return retval;
6826}
6827
Xin Long9fb657a2017-01-18 00:44:46 +08006828static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
6829 char __user *optval,
6830 int __user *optlen)
6831{
6832 struct sctp_assoc_value params;
6833 struct sctp_association *asoc;
6834 int retval = -EFAULT;
6835
6836 if (len < sizeof(params)) {
6837 retval = -EINVAL;
6838 goto out;
6839 }
6840
6841 len = sizeof(params);
6842 if (copy_from_user(&params, optval, len))
6843 goto out;
6844
6845 asoc = sctp_id2assoc(sk, params.assoc_id);
6846 if (asoc) {
6847 params.assoc_value = asoc->strreset_enable;
6848 } else if (!params.assoc_id) {
6849 struct sctp_sock *sp = sctp_sk(sk);
6850
6851 params.assoc_value = sp->ep->strreset_enable;
6852 } else {
6853 retval = -EINVAL;
6854 goto out;
6855 }
6856
6857 if (put_user(len, optlen))
6858 goto out;
6859
6860 if (copy_to_user(optval, &params, len))
6861 goto out;
6862
6863 retval = 0;
6864
6865out:
6866 return retval;
6867}
6868
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03006869static int sctp_getsockopt_scheduler(struct sock *sk, int len,
6870 char __user *optval,
6871 int __user *optlen)
6872{
6873 struct sctp_assoc_value params;
6874 struct sctp_association *asoc;
6875 int retval = -EFAULT;
6876
6877 if (len < sizeof(params)) {
6878 retval = -EINVAL;
6879 goto out;
6880 }
6881
6882 len = sizeof(params);
6883 if (copy_from_user(&params, optval, len))
6884 goto out;
6885
6886 asoc = sctp_id2assoc(sk, params.assoc_id);
6887 if (!asoc) {
6888 retval = -EINVAL;
6889 goto out;
6890 }
6891
6892 params.assoc_value = sctp_sched_get_sched(asoc);
6893
6894 if (put_user(len, optlen))
6895 goto out;
6896
6897 if (copy_to_user(optval, &params, len))
6898 goto out;
6899
6900 retval = 0;
6901
6902out:
6903 return retval;
6904}
6905
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03006906static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
6907 char __user *optval,
6908 int __user *optlen)
6909{
6910 struct sctp_stream_value params;
6911 struct sctp_association *asoc;
6912 int retval = -EFAULT;
6913
6914 if (len < sizeof(params)) {
6915 retval = -EINVAL;
6916 goto out;
6917 }
6918
6919 len = sizeof(params);
6920 if (copy_from_user(&params, optval, len))
6921 goto out;
6922
6923 asoc = sctp_id2assoc(sk, params.assoc_id);
6924 if (!asoc) {
6925 retval = -EINVAL;
6926 goto out;
6927 }
6928
6929 retval = sctp_sched_get_value(asoc, params.stream_id,
6930 &params.stream_value);
6931 if (retval)
6932 goto out;
6933
6934 if (put_user(len, optlen)) {
6935 retval = -EFAULT;
6936 goto out;
6937 }
6938
6939 if (copy_to_user(optval, &params, len)) {
6940 retval = -EFAULT;
6941 goto out;
6942 }
6943
6944out:
6945 return retval;
6946}
6947
Xin Long772a5862017-12-08 21:03:58 +08006948static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
6949 char __user *optval,
6950 int __user *optlen)
6951{
6952 struct sctp_assoc_value params;
6953 struct sctp_association *asoc;
6954 int retval = -EFAULT;
6955
6956 if (len < sizeof(params)) {
6957 retval = -EINVAL;
6958 goto out;
6959 }
6960
6961 len = sizeof(params);
6962 if (copy_from_user(&params, optval, len))
6963 goto out;
6964
6965 asoc = sctp_id2assoc(sk, params.assoc_id);
6966 if (asoc) {
6967 params.assoc_value = asoc->intl_enable;
6968 } else if (!params.assoc_id) {
6969 struct sctp_sock *sp = sctp_sk(sk);
6970
6971 params.assoc_value = sp->strm_interleave;
6972 } else {
6973 retval = -EINVAL;
6974 goto out;
6975 }
6976
6977 if (put_user(len, optlen))
6978 goto out;
6979
6980 if (copy_to_user(optval, &params, len))
6981 goto out;
6982
6983 retval = 0;
6984
6985out:
6986 return retval;
6987}
6988
Daniel Borkmanndda91922013-06-17 11:40:05 +02006989static int sctp_getsockopt(struct sock *sk, int level, int optname,
6990 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991{
6992 int retval = 0;
6993 int len;
6994
Daniel Borkmannbb333812013-06-28 19:49:40 +02006995 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996
6997 /* I can hardly begin to describe how wrong this is. This is
6998 * so broken as to be worse than useless. The API draft
6999 * REALLY is NOT helpful here... I am not convinced that the
7000 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7001 * are at all well-founded.
7002 */
7003 if (level != SOL_SCTP) {
7004 struct sctp_af *af = sctp_sk(sk)->pf->af;
7005
7006 retval = af->getsockopt(sk, level, optname, optval, optlen);
7007 return retval;
7008 }
7009
7010 if (get_user(len, optlen))
7011 return -EFAULT;
7012
Jiri Slabya4b8e712016-10-21 14:13:24 +02007013 if (len < 0)
7014 return -EINVAL;
7015
wangweidong048ed4b2014-01-21 15:44:11 +08007016 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017
7018 switch (optname) {
7019 case SCTP_STATUS:
7020 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7021 break;
7022 case SCTP_DISABLE_FRAGMENTS:
7023 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7024 optlen);
7025 break;
7026 case SCTP_EVENTS:
7027 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7028 break;
7029 case SCTP_AUTOCLOSE:
7030 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7031 break;
7032 case SCTP_SOCKOPT_PEELOFF:
7033 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7034 break;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04007035 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7036 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7037 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038 case SCTP_PEER_ADDR_PARAMS:
7039 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7040 optlen);
7041 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00007042 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07007043 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08007044 optlen);
7045 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046 case SCTP_INITMSG:
7047 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7048 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 case SCTP_GET_PEER_ADDRS:
7050 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7051 optlen);
7052 break;
7053 case SCTP_GET_LOCAL_ADDRS:
7054 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7055 optlen);
7056 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04007057 case SCTP_SOCKOPT_CONNECTX3:
7058 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7059 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060 case SCTP_DEFAULT_SEND_PARAM:
7061 retval = sctp_getsockopt_default_send_param(sk, len,
7062 optval, optlen);
7063 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02007064 case SCTP_DEFAULT_SNDINFO:
7065 retval = sctp_getsockopt_default_sndinfo(sk, len,
7066 optval, optlen);
7067 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068 case SCTP_PRIMARY_ADDR:
7069 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7070 break;
7071 case SCTP_NODELAY:
7072 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7073 break;
7074 case SCTP_RTOINFO:
7075 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7076 break;
7077 case SCTP_ASSOCINFO:
7078 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7079 break;
7080 case SCTP_I_WANT_MAPPED_V4_ADDR:
7081 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7082 break;
7083 case SCTP_MAXSEG:
7084 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7085 break;
7086 case SCTP_GET_PEER_ADDR_INFO:
7087 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7088 optlen);
7089 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08007090 case SCTP_ADAPTATION_LAYER:
7091 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 optlen);
7093 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08007094 case SCTP_CONTEXT:
7095 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7096 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007097 case SCTP_FRAGMENT_INTERLEAVE:
7098 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7099 optlen);
7100 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07007101 case SCTP_PARTIAL_DELIVERY_POINT:
7102 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7103 optlen);
7104 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07007105 case SCTP_MAX_BURST:
7106 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7107 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007108 case SCTP_AUTH_KEY:
7109 case SCTP_AUTH_CHUNK:
7110 case SCTP_AUTH_DELETE_KEY:
7111 retval = -EOPNOTSUPP;
7112 break;
7113 case SCTP_HMAC_IDENT:
7114 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7115 break;
7116 case SCTP_AUTH_ACTIVE_KEY:
7117 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7118 break;
7119 case SCTP_PEER_AUTH_CHUNKS:
7120 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7121 optlen);
7122 break;
7123 case SCTP_LOCAL_AUTH_CHUNKS:
7124 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7125 optlen);
7126 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08007127 case SCTP_GET_ASSOC_NUMBER:
7128 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7129 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00007130 case SCTP_GET_ASSOC_ID_LIST:
7131 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7132 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09007133 case SCTP_AUTO_ASCONF:
7134 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7135 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00007136 case SCTP_PEER_ADDR_THLDS:
7137 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7138 break;
Michele Baldessari196d6752012-12-01 04:49:42 +00007139 case SCTP_GET_ASSOC_STATS:
7140 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7141 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02007142 case SCTP_RECVRCVINFO:
7143 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7144 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007145 case SCTP_RECVNXTINFO:
7146 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7147 break;
Xin Long28aa4c22016-07-09 19:47:40 +08007148 case SCTP_PR_SUPPORTED:
7149 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7150 break;
Xin Longf959fb42016-07-09 19:47:41 +08007151 case SCTP_DEFAULT_PRINFO:
7152 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7153 optlen);
7154 break;
Xin Long826d2532016-07-09 19:47:42 +08007155 case SCTP_PR_ASSOC_STATUS:
7156 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7157 optlen);
7158 break;
Xin Longd229d482017-04-01 17:07:46 +08007159 case SCTP_PR_STREAM_STATUS:
7160 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7161 optlen);
7162 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08007163 case SCTP_RECONFIG_SUPPORTED:
7164 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7165 optlen);
7166 break;
Xin Long9fb657a2017-01-18 00:44:46 +08007167 case SCTP_ENABLE_STREAM_RESET:
7168 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7169 optlen);
7170 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007171 case SCTP_STREAM_SCHEDULER:
7172 retval = sctp_getsockopt_scheduler(sk, len, optval,
7173 optlen);
7174 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007175 case SCTP_STREAM_SCHEDULER_VALUE:
7176 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7177 optlen);
7178 break;
Xin Long772a5862017-12-08 21:03:58 +08007179 case SCTP_INTERLEAVING_SUPPORTED:
7180 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7181 optlen);
7182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183 default:
7184 retval = -ENOPROTOOPT;
7185 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187
wangweidong048ed4b2014-01-21 15:44:11 +08007188 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189 return retval;
7190}
7191
Craig Gallek086c6532016-02-10 11:50:35 -05007192static int sctp_hash(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193{
7194 /* STUB */
Craig Gallek086c6532016-02-10 11:50:35 -05007195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196}
7197
7198static void sctp_unhash(struct sock *sk)
7199{
7200 /* STUB */
7201}
7202
7203/* Check if port is acceptable. Possibly find first available port.
7204 *
7205 * The port hash table (contained in the 'global' SCTP protocol storage
7206 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7207 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7208 * list (the list number is the port number hashed out, so as you
7209 * would expect from a hash function, all the ports in a given list have
7210 * such a number that hashes out to the same list number; you were
7211 * expecting that, right?); so each list has a set of ports, with a
7212 * link to the socket (struct sock) that uses it, the port number and
7213 * a fastreuse flag (FIXME: NPI ipg).
7214 */
7215static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007216 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217
7218static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
7219{
7220 struct sctp_bind_hashbucket *head; /* hash list */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007221 struct sctp_bind_bucket *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222 unsigned short snum;
7223 int ret;
7224
Al Viro04afd8b2006-11-20 17:02:01 -08007225 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226
Daniel Borkmannbb333812013-06-28 19:49:40 +02007227 pr_debug("%s: begins, snum:%d\n", __func__, snum);
7228
wangweidong79b91132014-01-21 15:44:07 +08007229 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230
7231 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07007232 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007233 int low, high, remaining, index;
7234 unsigned int rover;
WANG Cong122ff242014-05-12 16:04:53 -07007235 struct net *net = sock_net(sk);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007236
WANG Cong122ff242014-05-12 16:04:53 -07007237 inet_get_local_port_range(net, &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007238 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05007239 rover = prandom_u32() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241 do {
7242 rover++;
7243 if ((rover < low) || (rover > high))
7244 rover = low;
WANG Cong122ff242014-05-12 16:04:53 -07007245 if (inet_is_local_reserved_port(net, rover))
Amerigo Wange3826f12010-05-05 00:27:06 +00007246 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007247 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 head = &sctp_port_hashtable[index];
wangweidong3c8e43b2014-01-21 15:44:08 +08007249 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007250 sctp_for_each_hentry(pp, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007251 if ((pp->port == rover) &&
7252 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 goto next;
7254 break;
7255 next:
wangweidong3c8e43b2014-01-21 15:44:08 +08007256 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258
7259 /* Exhausted local port range during search? */
7260 ret = 1;
7261 if (remaining <= 0)
7262 goto fail;
7263
7264 /* OK, here is the one we will use. HEAD (the port
7265 * hash table list entry) is non-NULL and we hold it's
7266 * mutex.
7267 */
7268 snum = rover;
7269 } else {
7270 /* We are given an specific port number; we verify
7271 * that it is not being used. If it is used, we will
7272 * exahust the search in the hash list corresponding
7273 * to the port number (snum) - we detect that with the
7274 * port iterator, pp being NULL.
7275 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007276 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
wangweidong3c8e43b2014-01-21 15:44:08 +08007277 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007278 sctp_for_each_hentry(pp, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007279 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 goto pp_found;
7281 }
7282 }
7283 pp = NULL;
7284 goto pp_not_found;
7285pp_found:
7286 if (!hlist_empty(&pp->owner)) {
7287 /* We had a port hash table hit - there is an
7288 * available port (pp != NULL) and it is being
7289 * used by other socket (pp->owner not empty); that other
7290 * socket is going to be sk2.
7291 */
7292 int reuse = sk->sk_reuse;
7293 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294
Daniel Borkmannbb333812013-06-28 19:49:40 +02007295 pr_debug("%s: found a possible match\n", __func__);
7296
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007297 if (pp->fastreuse && sk->sk_reuse &&
7298 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299 goto success;
7300
7301 /* Run through the list of sockets bound to the port
7302 * (pp->port) [via the pointers bind_next and
7303 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
7304 * we get the endpoint they describe and run through
7305 * the endpoint's list of IP (v4 or v6) addresses,
7306 * comparing each of the addresses with the address of
7307 * the socket sk. If we find a match, then that means
7308 * that this port/socket (sk) combination are already
7309 * in an endpoint.
7310 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007311 sk_for_each_bound(sk2, &pp->owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 struct sctp_endpoint *ep2;
7313 ep2 = sctp_sk(sk2)->ep;
7314
Vlad Yasevich4e540642008-07-18 23:06:32 -07007315 if (sk == sk2 ||
7316 (reuse && sk2->sk_reuse &&
7317 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 continue;
7319
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07007320 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
7321 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322 ret = (long)sk2;
7323 goto fail_unlock;
7324 }
7325 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02007326
7327 pr_debug("%s: found a match\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328 }
7329pp_not_found:
7330 /* If there was a hash table miss, create a new port. */
7331 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007332 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 goto fail_unlock;
7334
7335 /* In either case (hit or miss), make sure fastreuse is 1 only
7336 * if sk->sk_reuse is too (that is, if the caller requested
7337 * SO_REUSEADDR on this socket -sk-).
7338 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007339 if (hlist_empty(&pp->owner)) {
7340 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
7341 pp->fastreuse = 1;
7342 else
7343 pp->fastreuse = 0;
7344 } else if (pp->fastreuse &&
7345 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 pp->fastreuse = 0;
7347
7348 /* We are set, so fill up all the data in the hash table
7349 * entry, tie the socket list information with the rest of the
7350 * sockets FIXME: Blurry, NPI (ipg).
7351 */
7352success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007354 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 sk_add_bind_node(sk, &pp->owner);
7356 sctp_sk(sk)->bind_hash = pp;
7357 }
7358 ret = 0;
7359
7360fail_unlock:
wangweidong3c8e43b2014-01-21 15:44:08 +08007361 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362
7363fail:
wangweidong79b91132014-01-21 15:44:07 +08007364 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 return ret;
7366}
7367
7368/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
7369 * port is requested.
7370 */
7371static int sctp_get_port(struct sock *sk, unsigned short snum)
7372{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 union sctp_addr addr;
7374 struct sctp_af *af = sctp_sk(sk)->pf->af;
7375
7376 /* Set up a dummy address struct from the sk. */
7377 af->from_sk(&addr, sk);
7378 addr.v4.sin_port = htons(snum);
7379
7380 /* Note: sk->sk_num gets filled in if ephemeral port request. */
Daniel Borkmann62208f12013-06-25 18:17:30 +02007381 return !!sctp_get_port_local(sk, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382}
7383
7384/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 * Move a socket to LISTENING state.
7386 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02007387static int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007389 struct sctp_sock *sp = sctp_sk(sk);
7390 struct sctp_endpoint *ep = sp->ep;
Herbert Xu5821c762016-01-24 21:20:12 +08007391 struct crypto_shash *tfm = NULL;
Neil Horman3c681982012-10-24 09:20:03 +00007392 char alg[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393
7394 /* Allocate HMAC for generating cookie. */
Neil Horman3c681982012-10-24 09:20:03 +00007395 if (!sp->hmac && sp->sctp_hmac_alg) {
7396 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
Herbert Xu5821c762016-01-24 21:20:12 +08007397 tfm = crypto_alloc_shash(alg, 0, 0);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07007398 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00007399 net_info_ratelimited("failed to load transform for %s: %ld\n",
Neil Horman3c681982012-10-24 09:20:03 +00007400 sp->sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007401 return -ENOSYS;
7402 }
7403 sctp_sk(sk)->hmac = tfm;
7404 }
7405
7406 /*
7407 * If a bind() or sctp_bindx() is not called prior to a listen()
7408 * call that allows new associations to be accepted, the system
7409 * picks an ephemeral port and will choose an address set equivalent
7410 * to binding with a wildcard address.
7411 *
7412 * This is not currently spelled out in the SCTP sockets
7413 * extensions draft, but follows the practice as seen in TCP
7414 * sockets.
7415 *
7416 */
Yafang Shaocbabf462017-12-20 11:12:54 +08007417 inet_sk_set_state(sk, SCTP_SS_LISTENING);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007418 if (!ep->base.bind_addr.port) {
7419 if (sctp_autobind(sk))
7420 return -EAGAIN;
7421 } else {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007422 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08007423 inet_sk_set_state(sk, SCTP_SS_CLOSED);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007424 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425 }
7426 }
7427
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007428 sk->sk_max_ack_backlog = backlog;
7429 sctp_hash_endpoint(ep);
7430 return 0;
7431}
7432
7433/*
7434 * 4.1.3 / 5.1.3 listen()
7435 *
7436 * By default, new associations are not accepted for UDP style sockets.
7437 * An application uses listen() to mark a socket as being able to
7438 * accept new associations.
7439 *
7440 * On TCP style sockets, applications use listen() to ready the SCTP
7441 * endpoint for accepting inbound associations.
7442 *
7443 * On both types of endpoints a backlog of '0' disables listening.
7444 *
7445 * Move a socket to LISTENING state.
7446 */
7447int sctp_inet_listen(struct socket *sock, int backlog)
7448{
7449 struct sock *sk = sock->sk;
7450 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7451 int err = -EINVAL;
7452
7453 if (unlikely(backlog < 0))
7454 return err;
7455
wangweidong048ed4b2014-01-21 15:44:11 +08007456 lock_sock(sk);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007457
7458 /* Peeled-off sockets are not allowed to listen(). */
7459 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
7460 goto out;
7461
7462 if (sock->state != SS_UNCONNECTED)
7463 goto out;
7464
Xin Long34b27892017-04-06 13:10:52 +08007465 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
7466 goto out;
7467
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007468 /* If backlog is zero, disable listening. */
7469 if (!backlog) {
7470 if (sctp_sstate(sk, CLOSED))
7471 goto out;
7472
7473 err = 0;
7474 sctp_unhash_endpoint(ep);
7475 sk->sk_state = SCTP_SS_CLOSED;
7476 if (sk->sk_reuse)
7477 sctp_sk(sk)->bind_hash->fastreuse = 1;
7478 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007479 }
7480
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007481 /* If we are already listening, just update the backlog */
7482 if (sctp_sstate(sk, LISTENING))
7483 sk->sk_max_ack_backlog = backlog;
7484 else {
7485 err = sctp_listen_start(sk, backlog);
7486 if (err)
7487 goto out;
7488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007490 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491out:
wangweidong048ed4b2014-01-21 15:44:11 +08007492 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494}
7495
7496/*
7497 * This function is done by modeling the current datagram_poll() and the
7498 * tcp_poll(). Note that, based on these implementations, we don't
7499 * lock the socket in this function, even though it seems that,
7500 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08007501 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502 * in this place. We assume that we don't need locks either until proven
7503 * otherwise.
7504 *
7505 * Another thing to note is that we include the Async I/O support
7506 * here, again, by modeling the current TCP/UDP code. We don't have
7507 * a good way to test with it yet.
7508 */
Al Viroade994f2017-07-03 00:01:49 -04007509__poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510{
7511 struct sock *sk = sock->sk;
7512 struct sctp_sock *sp = sctp_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -04007513 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514
Eric Dumazetaa395142010-04-20 13:03:51 +00007515 poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03007517 sock_rps_record_flow(sk);
7518
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519 /* A TCP-style listening socket becomes readable when the accept queue
7520 * is not empty.
7521 */
7522 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
7523 return (!list_empty(&sp->ep->asocs)) ?
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007524 (EPOLLIN | EPOLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525
7526 mask = 0;
7527
7528 /* Is there any exceptional events? */
7529 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007530 mask |= EPOLLERR |
7531 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
Davide Libenzif348d702006-03-25 03:07:39 -08007532 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007533 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007535 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536
7537 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00007538 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007539 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007540
7541 /* The association is either gone or not ready. */
7542 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
7543 return mask;
7544
7545 /* Is it writable? */
7546 if (sctp_writeable(sk)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007547 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 } else {
Eric Dumazet9cd3e072015-11-29 20:03:10 -08007549 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 /*
7551 * Since the socket is not locked, the buffer
7552 * might be made available after the writeable check and
7553 * before the bit is set. This could cause a lost I/O
7554 * signal. tcp_poll() has a race breaker for this race
7555 * condition. Based on their implementation, we put
7556 * in the following code to cover it as well.
7557 */
7558 if (sctp_writeable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007559 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 }
7561 return mask;
7562}
7563
7564/********************************************************************
7565 * 2nd Level Abstractions
7566 ********************************************************************/
7567
7568static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007569 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570{
7571 struct sctp_bind_bucket *pp;
7572
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08007573 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07007575 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576 pp->port = snum;
7577 pp->fastreuse = 0;
7578 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007579 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007580 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 }
7582 return pp;
7583}
7584
7585/* Caller must hold hashbucket lock for this tb with local BH disabled */
7586static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
7587{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07007588 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007589 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590 kmem_cache_free(sctp_bucket_cachep, pp);
7591 SCTP_DBG_OBJCNT_DEC(bind_bucket);
7592 }
7593}
7594
7595/* Release this socket's reference to a local port. */
7596static inline void __sctp_put_port(struct sock *sk)
7597{
7598 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007599 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
7600 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007601 struct sctp_bind_bucket *pp;
7602
wangweidong3c8e43b2014-01-21 15:44:08 +08007603 spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604 pp = sctp_sk(sk)->bind_hash;
7605 __sk_del_bind_node(sk);
7606 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007607 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007608 sctp_bucket_destroy(pp);
wangweidong3c8e43b2014-01-21 15:44:08 +08007609 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610}
7611
7612void sctp_put_port(struct sock *sk)
7613{
wangweidong79b91132014-01-21 15:44:07 +08007614 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615 __sctp_put_port(sk);
wangweidong79b91132014-01-21 15:44:07 +08007616 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617}
7618
7619/*
7620 * The system picks an ephemeral port and choose an address set equivalent
7621 * to binding with a wildcard address.
7622 * One of those addresses will be the primary address for the association.
7623 * This automatically enables the multihoming capability of SCTP.
7624 */
7625static int sctp_autobind(struct sock *sk)
7626{
7627 union sctp_addr autoaddr;
7628 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08007629 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630
7631 /* Initialize a local sockaddr structure to INADDR_ANY. */
7632 af = sctp_sk(sk)->pf->af;
7633
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007634 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635 af->inaddr_any(&autoaddr, port);
7636
7637 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
7638}
7639
7640/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
7641 *
7642 * From RFC 2292
7643 * 4.2 The cmsghdr Structure *
7644 *
7645 * When ancillary data is sent or received, any number of ancillary data
7646 * objects can be specified by the msg_control and msg_controllen members of
7647 * the msghdr structure, because each object is preceded by
7648 * a cmsghdr structure defining the object's length (the cmsg_len member).
7649 * Historically Berkeley-derived implementations have passed only one object
7650 * at a time, but this API allows multiple objects to be
7651 * passed in a single call to sendmsg() or recvmsg(). The following example
7652 * shows two ancillary data objects in a control buffer.
7653 *
7654 * |<--------------------------- msg_controllen -------------------------->|
7655 * | |
7656 *
7657 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
7658 *
7659 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
7660 * | | |
7661 *
7662 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
7663 *
7664 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
7665 * | | | | |
7666 *
7667 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7668 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
7669 *
7670 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
7671 *
7672 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7673 * ^
7674 * |
7675 *
7676 * msg_control
7677 * points here
7678 */
Xin Longa05437a2017-08-11 10:23:48 +08007679static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680{
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007681 struct msghdr *my_msg = (struct msghdr *)msg;
Xin Longa05437a2017-08-11 10:23:48 +08007682 struct cmsghdr *cmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683
Gu Zhengf95b4142014-12-11 11:22:04 +08007684 for_each_cmsghdr(cmsg, my_msg) {
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007685 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686 return -EINVAL;
7687
7688 /* Should we parse this header or ignore? */
7689 if (cmsg->cmsg_level != IPPROTO_SCTP)
7690 continue;
7691
7692 /* Strictly check lengths following example in SCM code. */
7693 switch (cmsg->cmsg_type) {
7694 case SCTP_INIT:
7695 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007696 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697 *
7698 * This cmsghdr structure provides information for
7699 * initializing new SCTP associations with sendmsg().
7700 * The SCTP_INITMSG socket option uses this same data
7701 * structure. This structure is not used for
7702 * recvmsg().
7703 *
7704 * cmsg_level cmsg_type cmsg_data[]
7705 * ------------ ------------ ----------------------
7706 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
7707 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007708 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709 return -EINVAL;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007710
7711 cmsgs->init = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712 break;
7713
7714 case SCTP_SNDRCV:
7715 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007716 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 *
7718 * This cmsghdr structure specifies SCTP options for
7719 * sendmsg() and describes SCTP header information
7720 * about a received message through recvmsg().
7721 *
7722 * cmsg_level cmsg_type cmsg_data[]
7723 * ------------ ------------ ----------------------
7724 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
7725 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007726 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727 return -EINVAL;
7728
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007729 cmsgs->srinfo = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007730
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007731 if (cmsgs->srinfo->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007732 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Longa6c2f792016-07-09 19:47:43 +08007733 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007734 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007735 return -EINVAL;
7736 break;
7737
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007738 case SCTP_SNDINFO:
7739 /* SCTP Socket API Extension
7740 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
7741 *
7742 * This cmsghdr structure specifies SCTP options for
7743 * sendmsg(). This structure and SCTP_RCVINFO replaces
7744 * SCTP_SNDRCV which has been deprecated.
7745 *
7746 * cmsg_level cmsg_type cmsg_data[]
7747 * ------------ ------------ ---------------------
7748 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
7749 */
7750 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
7751 return -EINVAL;
7752
7753 cmsgs->sinfo = CMSG_DATA(cmsg);
7754
7755 if (cmsgs->sinfo->snd_flags &
7756 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Longa6c2f792016-07-09 19:47:43 +08007757 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007758 SCTP_ABORT | SCTP_EOF))
7759 return -EINVAL;
7760 break;
Xin Longed63afb2018-03-05 20:44:18 +08007761 case SCTP_PRINFO:
7762 /* SCTP Socket API Extension
7763 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
7764 *
7765 * This cmsghdr structure specifies SCTP options for sendmsg().
7766 *
7767 * cmsg_level cmsg_type cmsg_data[]
7768 * ------------ ------------ ---------------------
7769 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
7770 */
7771 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
7772 return -EINVAL;
7773
7774 cmsgs->prinfo = CMSG_DATA(cmsg);
7775 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
7776 return -EINVAL;
7777
7778 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
7779 cmsgs->prinfo->pr_value = 0;
7780 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781 default:
7782 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784 }
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007785
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786 return 0;
7787}
7788
7789/*
7790 * Wait for a packet..
7791 * Note: This function is the same function as in core/datagram.c
7792 * with a few modifications to make lksctp work.
7793 */
wangweidong26ac8e52013-12-23 12:16:51 +08007794static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795{
7796 int error;
7797 DEFINE_WAIT(wait);
7798
Eric Dumazetaa395142010-04-20 13:03:51 +00007799 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800
7801 /* Socket errors? */
7802 error = sock_error(sk);
7803 if (error)
7804 goto out;
7805
7806 if (!skb_queue_empty(&sk->sk_receive_queue))
7807 goto ready;
7808
7809 /* Socket shut down? */
7810 if (sk->sk_shutdown & RCV_SHUTDOWN)
7811 goto out;
7812
7813 /* Sequenced packets can come disconnected. If so we report the
7814 * problem.
7815 */
7816 error = -ENOTCONN;
7817
7818 /* Is there a good reason to think that we may receive some data? */
7819 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
7820 goto out;
7821
7822 /* Handle signals. */
7823 if (signal_pending(current))
7824 goto interrupted;
7825
7826 /* Let another process have a go. Since we are going to sleep
7827 * anyway. Note: This may cause odd behaviors if the message
7828 * does not fit in the user's buffer, but this seems to be the
7829 * only way to honor MSG_DONTWAIT realistically.
7830 */
wangweidong048ed4b2014-01-21 15:44:11 +08007831 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832 *timeo_p = schedule_timeout(*timeo_p);
wangweidong048ed4b2014-01-21 15:44:11 +08007833 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007834
7835ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00007836 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837 return 0;
7838
7839interrupted:
7840 error = sock_intr_errno(*timeo_p);
7841
7842out:
Eric Dumazetaa395142010-04-20 13:03:51 +00007843 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844 *err = error;
7845 return error;
7846}
7847
7848/* Receive a datagram.
7849 * Note: This is pretty much the same routine as in core/datagram.c
7850 * with a few changes to make lksctp work.
7851 */
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007852struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
7853 int noblock, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854{
7855 int error;
7856 struct sk_buff *skb;
7857 long timeo;
7858
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859 timeo = sock_rcvtimeo(sk, noblock);
7860
Daniel Borkmannbb333812013-06-28 19:49:40 +02007861 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
7862 MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863
7864 do {
7865 /* Again only user level code calls this function,
7866 * so nothing interrupt level
7867 * will suddenly eat the receive_queue.
7868 *
7869 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00007870 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871 */
7872 if (flags & MSG_PEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873 skb = skb_peek(&sk->sk_receive_queue);
7874 if (skb)
Reshetova, Elena63354792017-06-30 13:07:58 +03007875 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876 } else {
Marcelo Ricardo Leitner311b2172016-04-13 19:12:29 -03007877 skb = __skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 }
7879
7880 if (skb)
7881 return skb;
7882
Neil Horman6736dc32005-12-02 20:30:06 -08007883 /* Caller is allowed not to check sk->sk_err before calling. */
7884 error = sock_error(sk);
7885 if (error)
7886 goto no_packet;
7887
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888 if (sk->sk_shutdown & RCV_SHUTDOWN)
7889 break;
7890
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07007891 if (sk_can_busy_loop(sk)) {
7892 sk_busy_loop(sk, noblock);
7893
7894 if (!skb_queue_empty(&sk->sk_receive_queue))
7895 continue;
7896 }
Neil Horman8465a5f2014-04-17 15:26:51 -04007897
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898 /* User doesn't want to wait. */
7899 error = -EAGAIN;
7900 if (!timeo)
7901 goto no_packet;
7902 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
7903
7904 return NULL;
7905
7906no_packet:
7907 *err = error;
7908 return NULL;
7909}
7910
7911/* If sndbuf has changed, wake up per association sndbuf waiters. */
7912static void __sctp_write_space(struct sctp_association *asoc)
7913{
7914 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915
Eric Dumazetceb5d582015-11-29 20:03:11 -08007916 if (sctp_wspace(asoc) <= 0)
7917 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007918
Eric Dumazetceb5d582015-11-29 20:03:11 -08007919 if (waitqueue_active(&asoc->wait))
7920 wake_up_interruptible(&asoc->wait);
Eric Dumazeteaefd112011-02-18 03:26:36 +00007921
Eric Dumazetceb5d582015-11-29 20:03:11 -08007922 if (sctp_writeable(sk)) {
7923 struct socket_wq *wq;
7924
7925 rcu_read_lock();
7926 wq = rcu_dereference(sk->sk_wq);
7927 if (wq) {
7928 if (waitqueue_active(&wq->wait))
7929 wake_up_interruptible(&wq->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930
7931 /* Note that we try to include the Async I/O support
7932 * here by modeling from the current TCP/UDP code.
7933 * We have not tested with it yet.
7934 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00007935 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Eric Dumazetceb5d582015-11-29 20:03:11 -08007936 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08007938 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007939 }
7940}
7941
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007942static void sctp_wake_up_waiters(struct sock *sk,
7943 struct sctp_association *asoc)
7944{
7945 struct sctp_association *tmp = asoc;
7946
7947 /* We do accounting for the sndbuf space per association,
7948 * so we only need to wake our own association.
7949 */
7950 if (asoc->ep->sndbuf_policy)
7951 return __sctp_write_space(asoc);
7952
Daniel Borkmann1e1cdf82014-04-09 16:10:20 +02007953 /* If association goes down and is just flushing its
7954 * outq, then just normally notify others.
7955 */
7956 if (asoc->base.dead)
7957 return sctp_write_space(sk);
7958
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007959 /* Accounting for the sndbuf space is per socket, so we
7960 * need to wake up others, try to be fair and in case of
7961 * other associations, let them have a go first instead
7962 * of just doing a sctp_write_space() call.
7963 *
7964 * Note that we reach sctp_wake_up_waiters() only when
7965 * associations free up queued chunks, thus we are under
7966 * lock and the list of associations on a socket is
7967 * guaranteed not to change.
7968 */
7969 for (tmp = list_next_entry(tmp, asocs); 1;
7970 tmp = list_next_entry(tmp, asocs)) {
7971 /* Manually skip the head element. */
7972 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
7973 continue;
7974 /* Wake up association. */
7975 __sctp_write_space(tmp);
7976 /* We've reached the end. */
7977 if (tmp == asoc)
7978 break;
7979 }
7980}
7981
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982/* Do accounting for the sndbuf space.
7983 * Decrement the used sndbuf space of the corresponding association by the
7984 * data size which was just transmitted(freed).
7985 */
7986static void sctp_wfree(struct sk_buff *skb)
7987{
Daniel Borkmannf869c912014-11-20 01:54:48 +01007988 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
7989 struct sctp_association *asoc = chunk->asoc;
7990 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991
Neil Horman4eb701d2005-04-28 12:02:04 -07007992 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
7993 sizeof(struct sk_buff) +
7994 sizeof(struct sctp_chunk);
7995
Reshetova, Elena14afee42017-06-30 13:08:00 +03007996 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
Neil Horman4eb701d2005-04-28 12:02:04 -07007997
Neil Horman4d93df02007-08-15 16:07:44 -07007998 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007999 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
Neil Horman4d93df02007-08-15 16:07:44 -07008000 */
8001 sk->sk_wmem_queued -= skb->truesize;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008002 sk_mem_uncharge(sk, skb->truesize);
Neil Horman4d93df02007-08-15 16:07:44 -07008003
Neil Horman4eb701d2005-04-28 12:02:04 -07008004 sock_wfree(skb);
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008005 sctp_wake_up_waiters(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006
8007 sctp_association_put(asoc);
8008}
8009
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008010/* Do accounting for the receive space on the socket.
8011 * Accounting for the association is done in ulpevent.c
8012 * We set this as a destructor for the cloned data skbs so that
8013 * accounting is done at the correct time.
8014 */
8015void sctp_sock_rfree(struct sk_buff *skb)
8016{
8017 struct sock *sk = skb->sk;
8018 struct sctp_ulpevent *event = sctp_skb2event(skb);
8019
8020 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07008021
8022 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008023 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07008024 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008025 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008026}
8027
8028
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029/* Helper function to wait for space in the sndbuf. */
8030static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +08008031 size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008032{
8033 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034 long current_timeo = *timeo_p;
8035 DEFINE_WAIT(wait);
Xin Longa0ff6602018-01-15 17:01:36 +08008036 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008037
Daniel Borkmannbb333812013-06-28 19:49:40 +02008038 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8039 *timeo_p, msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008040
8041 /* Increment the association's refcnt. */
8042 sctp_association_hold(asoc);
8043
8044 /* Wait on the association specific sndbuf space. */
8045 for (;;) {
8046 prepare_to_wait_exclusive(&asoc->wait, &wait,
8047 TASK_INTERRUPTIBLE);
Xin Longca3af4d2017-11-15 16:55:54 +08008048 if (asoc->base.dead)
8049 goto do_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050 if (!*timeo_p)
8051 goto do_nonblock;
Xin Longca3af4d2017-11-15 16:55:54 +08008052 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008053 goto do_error;
8054 if (signal_pending(current))
8055 goto do_interrupted;
8056 if (msg_len <= sctp_wspace(asoc))
8057 break;
8058
8059 /* Let another process have a go. Since we are going
8060 * to sleep anyway.
8061 */
wangweidong048ed4b2014-01-21 15:44:11 +08008062 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008064 lock_sock(sk);
Xin Longa0ff6602018-01-15 17:01:36 +08008065 if (sk != asoc->base.sk)
8066 goto do_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008067
8068 *timeo_p = current_timeo;
8069 }
8070
8071out:
8072 finish_wait(&asoc->wait, &wait);
8073
8074 /* Release the association's refcnt. */
8075 sctp_association_put(asoc);
8076
8077 return err;
8078
Xin Longca3af4d2017-11-15 16:55:54 +08008079do_dead:
8080 err = -ESRCH;
8081 goto out;
8082
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083do_error:
8084 err = -EPIPE;
8085 goto out;
8086
8087do_interrupted:
8088 err = sock_intr_errno(*timeo_p);
8089 goto out;
8090
8091do_nonblock:
8092 err = -EAGAIN;
8093 goto out;
8094}
8095
David S. Miller676d2362014-04-11 16:15:36 -04008096void sctp_data_ready(struct sock *sk)
Wei Yongjun561b1732010-04-28 08:47:18 +00008097{
David S. Miller7ef52732010-05-02 21:43:40 -07008098 struct socket_wq *wq;
8099
8100 rcu_read_lock();
8101 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08008102 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008103 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8104 EPOLLRDNORM | EPOLLRDBAND);
Wei Yongjun561b1732010-04-28 08:47:18 +00008105 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07008106 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00008107}
8108
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109/* If socket sndbuf has changed, wake up all per association waiters. */
8110void sctp_write_space(struct sock *sk)
8111{
8112 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008113
8114 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07008115 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008116 __sctp_write_space(asoc);
8117 }
8118}
8119
8120/* Is there any sndbuf space available on the socket?
8121 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08008122 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07008123 * associations on the same socket. For a UDP-style socket with
8124 * multiple associations, it is possible for it to be "unwriteable"
8125 * prematurely. I assume that this is acceptable because
8126 * a premature "unwriteable" is better than an accidental "writeable" which
8127 * would cause an unwanted block under certain circumstances. For the 1-1
8128 * UDP-style sockets or TCP-style sockets, this code should work.
8129 * - Daisy
8130 */
8131static int sctp_writeable(struct sock *sk)
8132{
8133 int amt = 0;
8134
Eric Dumazet31e6d362009-06-17 19:05:41 -07008135 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008136 if (amt < 0)
8137 amt = 0;
8138 return amt;
8139}
8140
8141/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8142 * returns immediately with EINPROGRESS.
8143 */
8144static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8145{
8146 struct sock *sk = asoc->base.sk;
8147 int err = 0;
8148 long current_timeo = *timeo_p;
8149 DEFINE_WAIT(wait);
8150
Daniel Borkmannbb333812013-06-28 19:49:40 +02008151 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152
8153 /* Increment the association's refcnt. */
8154 sctp_association_hold(asoc);
8155
8156 for (;;) {
8157 prepare_to_wait_exclusive(&asoc->wait, &wait,
8158 TASK_INTERRUPTIBLE);
8159 if (!*timeo_p)
8160 goto do_nonblock;
8161 if (sk->sk_shutdown & RCV_SHUTDOWN)
8162 break;
8163 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
8164 asoc->base.dead)
8165 goto do_error;
8166 if (signal_pending(current))
8167 goto do_interrupted;
8168
8169 if (sctp_state(asoc, ESTABLISHED))
8170 break;
8171
8172 /* Let another process have a go. Since we are going
8173 * to sleep anyway.
8174 */
wangweidong048ed4b2014-01-21 15:44:11 +08008175 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008176 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008177 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178
8179 *timeo_p = current_timeo;
8180 }
8181
8182out:
8183 finish_wait(&asoc->wait, &wait);
8184
8185 /* Release the association's refcnt. */
8186 sctp_association_put(asoc);
8187
8188 return err;
8189
8190do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08008191 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192 err = -ETIMEDOUT;
8193 else
8194 err = -ECONNREFUSED;
8195 goto out;
8196
8197do_interrupted:
8198 err = sock_intr_errno(*timeo_p);
8199 goto out;
8200
8201do_nonblock:
8202 err = -EINPROGRESS;
8203 goto out;
8204}
8205
8206static int sctp_wait_for_accept(struct sock *sk, long timeo)
8207{
8208 struct sctp_endpoint *ep;
8209 int err = 0;
8210 DEFINE_WAIT(wait);
8211
8212 ep = sctp_sk(sk)->ep;
8213
8214
8215 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00008216 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008217 TASK_INTERRUPTIBLE);
8218
8219 if (list_empty(&ep->asocs)) {
wangweidong048ed4b2014-01-21 15:44:11 +08008220 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221 timeo = schedule_timeout(timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008222 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008223 }
8224
8225 err = -EINVAL;
8226 if (!sctp_sstate(sk, LISTENING))
8227 break;
8228
8229 err = 0;
8230 if (!list_empty(&ep->asocs))
8231 break;
8232
8233 err = sock_intr_errno(timeo);
8234 if (signal_pending(current))
8235 break;
8236
8237 err = -EAGAIN;
8238 if (!timeo)
8239 break;
8240 }
8241
Eric Dumazetaa395142010-04-20 13:03:51 +00008242 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243
8244 return err;
8245}
8246
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02008247static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248{
8249 DEFINE_WAIT(wait);
8250
8251 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00008252 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253 if (list_empty(&sctp_sk(sk)->ep->asocs))
8254 break;
wangweidong048ed4b2014-01-21 15:44:11 +08008255 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008256 timeout = schedule_timeout(timeout);
wangweidong048ed4b2014-01-21 15:44:11 +08008257 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008258 } while (!signal_pending(current) && timeout);
8259
Eric Dumazetaa395142010-04-20 13:03:51 +00008260 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261}
8262
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008263static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
8264{
8265 struct sk_buff *frag;
8266
8267 if (!skb->data_len)
8268 goto done;
8269
8270 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07008271 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008272 sctp_skb_set_owner_r_frag(frag, sk);
8273
8274done:
8275 sctp_skb_set_owner_r(skb, sk);
8276}
8277
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008278void sctp_copy_sock(struct sock *newsk, struct sock *sk,
8279 struct sctp_association *asoc)
8280{
8281 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08008282 struct inet_sock *newinet;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008283
8284 newsk->sk_type = sk->sk_type;
8285 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
8286 newsk->sk_flags = sk->sk_flags;
Marcelo Ricardo Leitner50a5ffb2015-12-04 15:14:05 -02008287 newsk->sk_tsflags = sk->sk_tsflags;
Tom Herbert28448b82014-05-23 08:47:19 -07008288 newsk->sk_no_check_tx = sk->sk_no_check_tx;
8289 newsk->sk_no_check_rx = sk->sk_no_check_rx;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008290 newsk->sk_reuse = sk->sk_reuse;
8291
8292 newsk->sk_shutdown = sk->sk_shutdown;
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02008293 newsk->sk_destruct = sctp_destruct_sock;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008294 newsk->sk_family = sk->sk_family;
8295 newsk->sk_protocol = IPPROTO_SCTP;
8296 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
8297 newsk->sk_sndbuf = sk->sk_sndbuf;
8298 newsk->sk_rcvbuf = sk->sk_rcvbuf;
8299 newsk->sk_lingertime = sk->sk_lingertime;
8300 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
8301 newsk->sk_sndtimeo = sk->sk_sndtimeo;
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03008302 newsk->sk_rxhash = sk->sk_rxhash;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008303
8304 newinet = inet_sk(newsk);
8305
8306 /* Initialize sk's sport, dport, rcv_saddr and daddr for
8307 * getsockname() and getpeername()
8308 */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008309 newinet->inet_sport = inet->inet_sport;
8310 newinet->inet_saddr = inet->inet_saddr;
8311 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
8312 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008313 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008314 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008315
8316 newinet->uc_ttl = inet->uc_ttl;
8317 newinet->mc_loop = 1;
8318 newinet->mc_ttl = 1;
8319 newinet->mc_index = 0;
8320 newinet->mc_list = NULL;
Marcelo Ricardo Leitner01ce63c2015-12-04 15:14:04 -02008321
8322 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
8323 net_enable_timestamp();
Marcelo Ricardo Leitner3538a5c2015-12-23 16:44:09 -02008324
8325 security_sk_clone(sk, newsk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008326}
8327
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008328static inline void sctp_copy_descendant(struct sock *sk_to,
8329 const struct sock *sk_from)
8330{
8331 int ancestor_size = sizeof(struct inet_sock) +
8332 sizeof(struct sctp_sock) -
8333 offsetof(struct sctp_sock, auto_asconf_list);
8334
8335 if (sk_from->sk_family == PF_INET6)
8336 ancestor_size += sizeof(struct ipv6_pinfo);
8337
8338 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
8339}
8340
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341/* Populate the fields of the newsk from the oldsk and migrate the assoc
8342 * and its messages to the newsk.
8343 */
8344static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
8345 struct sctp_association *assoc,
Xin Longb7ef2612017-08-11 10:23:50 +08008346 enum sctp_socket_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008347{
8348 struct sctp_sock *oldsp = sctp_sk(oldsk);
8349 struct sctp_sock *newsp = sctp_sk(newsk);
8350 struct sctp_bind_bucket *pp; /* hash list port iterator */
8351 struct sctp_endpoint *newep = newsp->ep;
8352 struct sk_buff *skb, *tmp;
8353 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08008354 struct sctp_bind_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355
8356 /* Migrate socket buffer sizes and all the socket level options to the
8357 * new socket.
8358 */
8359 newsk->sk_sndbuf = oldsk->sk_sndbuf;
8360 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
8361 /* Brute force copy old sctp opt. */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008362 sctp_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008363
8364 /* Restore the ep value that was overwritten with the above structure
8365 * copy.
8366 */
8367 newsp->ep = newep;
8368 newsp->hmac = NULL;
8369
8370 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008371 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
8372 inet_sk(oldsk)->inet_num)];
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008373 spin_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374 pp = sctp_sk(oldsk)->bind_hash;
8375 sk_add_bind_node(newsk, &pp->owner);
8376 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008377 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008378 spin_unlock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008380 /* Copy the bind_addr list from the original endpoint to the new
8381 * endpoint so that we can handle restarts properly
8382 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08008383 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
8384 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008385
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386 /* Move any messages in the old socket's receive queue that are for the
8387 * peeled off association to the new socket's receive queue.
8388 */
8389 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
8390 event = sctp_skb2event(skb);
8391 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008392 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008393 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008394 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008395 }
8396 }
8397
8398 /* Clean up any messages pending delivery due to partial
8399 * delivery. Three cases:
8400 * 1) No partial deliver; no work.
8401 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
8402 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
8403 */
8404 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008405 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008406
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008407 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408 struct sk_buff_head *queue;
8409
8410 /* Decide which queue to move pd_lobby skbs to. */
8411 if (assoc->ulpq.pd_mode) {
8412 queue = &newsp->pd_lobby;
8413 } else
8414 queue = &newsk->sk_receive_queue;
8415
8416 /* Walk through the pd_lobby, looking for skbs that
8417 * need moved to the new socket.
8418 */
8419 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
8420 event = sctp_skb2event(skb);
8421 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008422 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008423 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008424 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008425 }
8426 }
8427
8428 /* Clear up any skbs waiting for the partial
8429 * delivery to finish.
8430 */
8431 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008432 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433
8434 }
8435
Xin Long13228232017-12-08 21:04:09 +08008436 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008437
Linus Torvalds1da177e2005-04-16 15:20:36 -07008438 /* Set the type of socket to indicate that it is peeled off from the
8439 * original UDP-style socket or created with the accept() call on a
8440 * TCP-style socket..
8441 */
8442 newsp->type = type;
8443
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008444 /* Mark the new socket "in-use" by the user so that any packets
8445 * that may arrive on the association after we've moved it are
8446 * queued to the backlog. This prevents a potential race between
8447 * backlog processing on the old socket and new-packet processing
8448 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07008449 *
8450 * The caller has just allocated newsk so we can guarantee that other
8451 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008452 */
Zach Brown5131a182007-06-22 15:14:46 -07008453 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Xin Longd04adf12017-10-28 02:13:29 +08008454 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008455 sctp_assoc_migrate(assoc, newsk);
Xin Longd04adf12017-10-28 02:13:29 +08008456 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008457
8458 /* If the association on the newsk is already closed before accept()
8459 * is called, set RCV_SHUTDOWN flag.
8460 */
Xin Longd46e4162016-06-09 22:48:18 +08008461 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08008462 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463 newsk->sk_shutdown |= RCV_SHUTDOWN;
Xin Longd46e4162016-06-09 22:48:18 +08008464 } else {
Yafang Shaocbabf462017-12-20 11:12:54 +08008465 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
Xin Longd46e4162016-06-09 22:48:18 +08008466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008467
wangweidong048ed4b2014-01-21 15:44:11 +08008468 release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469}
8470
Neil Horman4d93df02007-08-15 16:07:44 -07008471
Linus Torvalds1da177e2005-04-16 15:20:36 -07008472/* This proto struct describes the ULP interface for SCTP. */
8473struct proto sctp_prot = {
8474 .name = "SCTP",
8475 .owner = THIS_MODULE,
8476 .close = sctp_close,
8477 .connect = sctp_connect,
8478 .disconnect = sctp_disconnect,
8479 .accept = sctp_accept,
8480 .ioctl = sctp_ioctl,
8481 .init = sctp_init_sock,
8482 .destroy = sctp_destroy_sock,
8483 .shutdown = sctp_shutdown,
8484 .setsockopt = sctp_setsockopt,
8485 .getsockopt = sctp_getsockopt,
8486 .sendmsg = sctp_sendmsg,
8487 .recvmsg = sctp_recvmsg,
8488 .bind = sctp_bind,
8489 .backlog_rcv = sctp_backlog_rcv,
8490 .hash = sctp_hash,
8491 .unhash = sctp_unhash,
8492 .get_port = sctp_get_port,
8493 .obj_size = sizeof(struct sctp_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07008494 .useroffset = offsetof(struct sctp_sock, subscribe),
8495 .usersize = offsetof(struct sctp_sock, initmsg) -
8496 offsetof(struct sctp_sock, subscribe) +
8497 sizeof_field(struct sctp_sock, initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07008498 .sysctl_mem = sysctl_sctp_mem,
8499 .sysctl_rmem = sysctl_sctp_rmem,
8500 .sysctl_wmem = sysctl_sctp_wmem,
8501 .memory_pressure = &sctp_memory_pressure,
8502 .enter_memory_pressure = sctp_enter_memory_pressure,
8503 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008504 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008505};
8506
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008507#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08008508
Eric Dumazet602dd622015-12-01 07:20:07 -08008509#include <net/transp_v6.h>
8510static void sctp_v6_destroy_sock(struct sock *sk)
8511{
8512 sctp_destroy_sock(sk);
8513 inet6_destroy_sock(sk);
8514}
8515
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516struct proto sctpv6_prot = {
8517 .name = "SCTPv6",
8518 .owner = THIS_MODULE,
8519 .close = sctp_close,
8520 .connect = sctp_connect,
8521 .disconnect = sctp_disconnect,
8522 .accept = sctp_accept,
8523 .ioctl = sctp_ioctl,
8524 .init = sctp_init_sock,
Eric Dumazet602dd622015-12-01 07:20:07 -08008525 .destroy = sctp_v6_destroy_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008526 .shutdown = sctp_shutdown,
8527 .setsockopt = sctp_setsockopt,
8528 .getsockopt = sctp_getsockopt,
8529 .sendmsg = sctp_sendmsg,
8530 .recvmsg = sctp_recvmsg,
8531 .bind = sctp_bind,
8532 .backlog_rcv = sctp_backlog_rcv,
8533 .hash = sctp_hash,
8534 .unhash = sctp_unhash,
8535 .get_port = sctp_get_port,
8536 .obj_size = sizeof(struct sctp6_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07008537 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
8538 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
8539 offsetof(struct sctp6_sock, sctp.subscribe) +
8540 sizeof_field(struct sctp6_sock, sctp.initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07008541 .sysctl_mem = sysctl_sctp_mem,
8542 .sysctl_rmem = sysctl_sctp_rmem,
8543 .sysctl_wmem = sysctl_sctp_wmem,
8544 .memory_pressure = &sctp_memory_pressure,
8545 .enter_memory_pressure = sctp_enter_memory_pressure,
8546 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008547 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008548};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008549#endif /* IS_ENABLED(CONFIG_IPV6) */