blob: af5cf29b0c659c2fc3f67473578b09431fe272ab [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 Viro5f242a12006-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;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001679 struct cmsghdr *cmsg;
Wei Yongjund98985d2018-03-13 03:03:30 +00001680 int err;
Xin Long2bfd80f2018-03-01 23:05:11 +08001681
1682 *tp = NULL;
1683
1684 if (sflags & (SCTP_EOF | SCTP_ABORT))
1685 return -EINVAL;
1686
1687 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1688 sctp_sstate(sk, CLOSING)))
1689 return -EADDRNOTAVAIL;
1690
1691 if (sctp_endpoint_is_peeled_off(ep, daddr))
1692 return -EADDRNOTAVAIL;
1693
1694 if (!ep->base.bind_addr.port) {
1695 if (sctp_autobind(sk))
1696 return -EAGAIN;
1697 } else {
1698 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1699 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1700 return -EACCES;
1701 }
1702
1703 scope = sctp_scope(daddr);
1704
1705 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1706 if (!asoc)
1707 return -ENOMEM;
1708
1709 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1710 err = -ENOMEM;
1711 goto free;
1712 }
1713
1714 if (cmsgs->init) {
1715 struct sctp_initmsg *init = cmsgs->init;
1716
1717 if (init->sinit_num_ostreams) {
1718 __u16 outcnt = init->sinit_num_ostreams;
1719
1720 asoc->c.sinit_num_ostreams = outcnt;
1721 /* outcnt has been changed, need to re-init stream */
1722 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1723 GFP_KERNEL);
1724 if (err)
1725 goto free;
1726 }
1727
1728 if (init->sinit_max_instreams)
1729 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1730
1731 if (init->sinit_max_attempts)
1732 asoc->max_init_attempts = init->sinit_max_attempts;
1733
1734 if (init->sinit_max_init_timeo)
1735 asoc->max_init_timeo =
1736 msecs_to_jiffies(init->sinit_max_init_timeo);
1737 }
1738
1739 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1740 if (!*tp) {
1741 err = -ENOMEM;
1742 goto free;
1743 }
1744
Xin Long2c0dbaa2018-03-05 20:44:19 +08001745 if (!cmsgs->addrs_msg)
1746 return 0;
1747
1748 /* sendv addr list parse */
1749 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1750 struct sctp_transport *transport;
1751 struct sctp_association *old;
1752 union sctp_addr _daddr;
1753 int dlen;
1754
1755 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1756 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1757 cmsg->cmsg_type != SCTP_DSTADDRV6))
1758 continue;
1759
1760 daddr = &_daddr;
1761 memset(daddr, 0, sizeof(*daddr));
1762 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1763 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
Wei Yongjund98985d2018-03-13 03:03:30 +00001764 if (dlen < sizeof(struct in_addr)) {
1765 err = -EINVAL;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001766 goto free;
Wei Yongjund98985d2018-03-13 03:03:30 +00001767 }
Xin Long2c0dbaa2018-03-05 20:44:19 +08001768
1769 dlen = sizeof(struct in_addr);
1770 daddr->v4.sin_family = AF_INET;
1771 daddr->v4.sin_port = htons(asoc->peer.port);
1772 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1773 } else {
Wei Yongjund98985d2018-03-13 03:03:30 +00001774 if (dlen < sizeof(struct in6_addr)) {
1775 err = -EINVAL;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001776 goto free;
Wei Yongjund98985d2018-03-13 03:03:30 +00001777 }
Xin Long2c0dbaa2018-03-05 20:44:19 +08001778
1779 dlen = sizeof(struct in6_addr);
1780 daddr->v6.sin6_family = AF_INET6;
1781 daddr->v6.sin6_port = htons(asoc->peer.port);
1782 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1783 }
1784 err = sctp_verify_addr(sk, daddr, sizeof(*daddr));
1785 if (err)
1786 goto free;
1787
1788 old = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1789 if (old && old != asoc) {
1790 if (old->state >= SCTP_STATE_ESTABLISHED)
1791 err = -EISCONN;
1792 else
1793 err = -EALREADY;
1794 goto free;
1795 }
1796
1797 if (sctp_endpoint_is_peeled_off(ep, daddr)) {
1798 err = -EADDRNOTAVAIL;
1799 goto free;
1800 }
1801
1802 transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL,
1803 SCTP_UNKNOWN);
1804 if (!transport) {
1805 err = -ENOMEM;
1806 goto free;
1807 }
1808 }
1809
Xin Long2bfd80f2018-03-01 23:05:11 +08001810 return 0;
1811
1812free:
1813 sctp_association_free(asoc);
1814 return err;
1815}
1816
Xin Longc2666de2018-03-01 23:05:12 +08001817static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1818 __u16 sflags, struct msghdr *msg,
1819 size_t msg_len)
1820{
1821 struct sock *sk = asoc->base.sk;
1822 struct net *net = sock_net(sk);
1823
1824 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1825 return -EPIPE;
1826
Xin Long49102802018-03-05 20:44:20 +08001827 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1828 !sctp_state(asoc, ESTABLISHED))
1829 return 0;
1830
Xin Longc2666de2018-03-01 23:05:12 +08001831 if (sflags & SCTP_EOF) {
1832 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1833 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1834
1835 return 0;
1836 }
1837
1838 if (sflags & SCTP_ABORT) {
1839 struct sctp_chunk *chunk;
1840
1841 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1842 if (!chunk)
1843 return -ENOMEM;
1844
1845 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1846 sctp_primitive_ABORT(net, asoc, chunk);
1847
1848 return 0;
1849 }
1850
1851 return 1;
1852}
1853
Xin Longf84af332018-03-01 23:05:10 +08001854static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1855 struct msghdr *msg, size_t msg_len,
1856 struct sctp_transport *transport,
1857 struct sctp_sndrcvinfo *sinfo)
1858{
1859 struct sock *sk = asoc->base.sk;
1860 struct net *net = sock_net(sk);
1861 struct sctp_datamsg *datamsg;
1862 bool wait_connect = false;
1863 struct sctp_chunk *chunk;
1864 long timeo;
1865 int err;
1866
1867 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1868 err = -EINVAL;
1869 goto err;
1870 }
1871
1872 if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) {
1873 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1874 if (err)
1875 goto err;
1876 }
1877
1878 if (sctp_sk(sk)->disable_fragments && msg_len > asoc->frag_point) {
1879 err = -EMSGSIZE;
1880 goto err;
1881 }
1882
1883 if (sctp_state(asoc, CLOSED)) {
1884 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1885 if (err)
1886 goto err;
1887
1888 if (sctp_sk(sk)->strm_interleave) {
1889 timeo = sock_sndtimeo(sk, 0);
1890 err = sctp_wait_for_connect(asoc, &timeo);
1891 if (err)
1892 goto err;
1893 } else {
1894 wait_connect = true;
1895 }
1896
1897 pr_debug("%s: we associated primitively\n", __func__);
1898 }
1899
1900 if (asoc->pmtu_pending)
1901 sctp_assoc_pending_pmtu(asoc);
1902
1903 if (sctp_wspace(asoc) < msg_len)
1904 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1905
1906 if (!sctp_wspace(asoc)) {
1907 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1908 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1909 if (err)
1910 goto err;
1911 }
1912
1913 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1914 if (IS_ERR(datamsg)) {
1915 err = PTR_ERR(datamsg);
1916 goto err;
1917 }
1918
1919 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1920
1921 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1922 sctp_chunk_hold(chunk);
1923 sctp_set_owner_w(chunk);
1924 chunk->transport = transport;
1925 }
1926
1927 err = sctp_primitive_SEND(net, asoc, datamsg);
1928 if (err) {
1929 sctp_datamsg_free(datamsg);
1930 goto err;
1931 }
1932
1933 pr_debug("%s: we sent primitively\n", __func__);
1934
1935 sctp_datamsg_put(datamsg);
1936
1937 if (unlikely(wait_connect)) {
1938 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1939 sctp_wait_for_connect(asoc, &timeo);
1940 }
1941
1942 err = msg_len;
1943
1944err:
1945 return err;
1946}
1947
Xin Longbecef9b2018-03-01 23:05:13 +08001948static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1949 const struct msghdr *msg,
1950 struct sctp_cmsgs *cmsgs)
1951{
1952 union sctp_addr *daddr = NULL;
1953 int err;
1954
1955 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1956 int len = msg->msg_namelen;
1957
1958 if (len > sizeof(*daddr))
1959 len = sizeof(*daddr);
1960
1961 daddr = (union sctp_addr *)msg->msg_name;
1962
1963 err = sctp_verify_addr(sk, daddr, len);
1964 if (err)
1965 return ERR_PTR(err);
1966 }
1967
1968 return daddr;
1969}
1970
Xin Longd42cb062018-03-01 23:05:15 +08001971static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
1972 struct sctp_sndrcvinfo *sinfo,
1973 struct sctp_cmsgs *cmsgs)
1974{
1975 if (!cmsgs->srinfo && !cmsgs->sinfo) {
1976 sinfo->sinfo_stream = asoc->default_stream;
1977 sinfo->sinfo_ppid = asoc->default_ppid;
1978 sinfo->sinfo_context = asoc->default_context;
1979 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
Xin Longed63afb2018-03-05 20:44:18 +08001980
1981 if (!cmsgs->prinfo)
1982 sinfo->sinfo_flags = asoc->default_flags;
Xin Longd42cb062018-03-01 23:05:15 +08001983 }
1984
Xin Longed63afb2018-03-05 20:44:18 +08001985 if (!cmsgs->srinfo && !cmsgs->prinfo)
Xin Longd42cb062018-03-01 23:05:15 +08001986 sinfo->sinfo_timetolive = asoc->default_timetolive;
1987}
1988
Ying Xue1b784142015-03-02 15:37:48 +08001989static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
Xin Long204f8172018-03-01 23:05:14 +08001991 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Xin Long8e87c6e2018-03-01 23:05:16 +08001992 struct sctp_transport *transport = NULL;
Xin Long204f8172018-03-01 23:05:14 +08001993 struct sctp_sndrcvinfo _sinfo, *sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08001994 struct sctp_association *asoc;
1995 struct sctp_cmsgs cmsgs;
Xin Longbecef9b2018-03-01 23:05:13 +08001996 union sctp_addr *daddr;
Xin Long007b7e12018-03-01 23:05:17 +08001997 bool new = false;
1998 __u16 sflags;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001999 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Xin Long204f8172018-03-01 23:05:14 +08002001 /* Parse and get snd_info */
2002 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
2003 if (err)
Xin Long007b7e12018-03-01 23:05:17 +08002004 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Xin Long204f8172018-03-01 23:05:14 +08002006 sinfo = &_sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08002007 sflags = sinfo->sinfo_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Xin Longbecef9b2018-03-01 23:05:13 +08002009 /* Get daddr from msg */
2010 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
2011 if (IS_ERR(daddr)) {
2012 err = PTR_ERR(daddr);
Xin Long007b7e12018-03-01 23:05:17 +08002013 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015
wangweidong048ed4b2014-01-21 15:44:11 +08002016 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Xin Long49102802018-03-05 20:44:20 +08002018 /* SCTP_SENDALL process */
2019 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
2020 list_for_each_entry(asoc, &ep->asocs, asocs) {
2021 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2022 msg_len);
2023 if (err == 0)
2024 continue;
2025 if (err < 0)
2026 goto out_unlock;
2027
2028 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2029
2030 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
2031 NULL, sinfo);
2032 if (err < 0)
2033 goto out_unlock;
2034
2035 iov_iter_revert(&msg->msg_iter, err);
2036 }
2037
2038 goto out_unlock;
2039 }
2040
Xin Long0a3920d2018-03-01 23:05:18 +08002041 /* Get and check or create asoc */
Xin Longbecef9b2018-03-01 23:05:13 +08002042 if (daddr) {
Xin Longbecef9b2018-03-01 23:05:13 +08002043 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
Xin Long0a3920d2018-03-01 23:05:18 +08002044 if (asoc) {
2045 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2046 msg_len);
2047 if (err <= 0)
2048 goto out_unlock;
2049 } else {
2050 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2051 &transport);
2052 if (err)
2053 goto out_unlock;
2054
2055 asoc = transport->asoc;
2056 new = true;
2057 }
2058
2059 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2060 transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 } else {
Xin Long007b7e12018-03-01 23:05:17 +08002062 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (!asoc) {
2064 err = -EPIPE;
2065 goto out_unlock;
2066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Xin Long007b7e12018-03-01 23:05:17 +08002068 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
Xin Longc2666de2018-03-01 23:05:12 +08002069 if (err <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
2072
Xin Longd42cb062018-03-01 23:05:15 +08002073 /* Update snd_info with the asoc */
2074 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Xin Longf84af332018-03-01 23:05:10 +08002076 /* Send msg to the asoc */
Xin Long8e87c6e2018-03-01 23:05:16 +08002077 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
Xin Long007b7e12018-03-01 23:05:17 +08002078 if (err < 0 && err != -ESRCH && new)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 sctp_association_free(asoc);
Xin Long8e87c6e2018-03-01 23:05:16 +08002080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081out_unlock:
wangweidong048ed4b2014-01-21 15:44:11 +08002082 release_sock(sk);
Xin Long007b7e12018-03-01 23:05:17 +08002083out:
Xin Longf84af332018-03-01 23:05:10 +08002084 return sctp_error(sk, msg->msg_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
2087/* This is an extended version of skb_pull() that removes the data from the
2088 * start of a skb even when data is spread across the list of skb's in the
2089 * frag_list. len specifies the total amount of data that needs to be removed.
2090 * when 'len' bytes could be removed from the skb, it returns 0.
2091 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2092 * could not be removed.
2093 */
2094static int sctp_skb_pull(struct sk_buff *skb, int len)
2095{
2096 struct sk_buff *list;
2097 int skb_len = skb_headlen(skb);
2098 int rlen;
2099
2100 if (len <= skb_len) {
2101 __skb_pull(skb, len);
2102 return 0;
2103 }
2104 len -= skb_len;
2105 __skb_pull(skb, skb_len);
2106
David S. Miller1b003be2009-06-09 00:22:35 -07002107 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 rlen = sctp_skb_pull(list, len);
2109 skb->len -= (len-rlen);
2110 skb->data_len -= (len-rlen);
2111
2112 if (!rlen)
2113 return 0;
2114
2115 len = rlen;
2116 }
2117
2118 return len;
2119}
2120
2121/* API 3.1.3 recvmsg() - UDP Style Syntax
2122 *
2123 * ssize_t recvmsg(int socket, struct msghdr *message,
2124 * int flags);
2125 *
2126 * socket - the socket descriptor of the endpoint.
2127 * message - pointer to the msghdr structure which contains a single
2128 * user message and possibly some ancillary data.
2129 *
2130 * See Section 5 for complete description of the data
2131 * structures.
2132 *
2133 * flags - flags sent or received with the user message, see Section
2134 * 5 for complete description of the flags.
2135 */
Ying Xue1b784142015-03-02 15:37:48 +08002136static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2137 int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
2139 struct sctp_ulpevent *event = NULL;
2140 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002141 struct sk_buff *skb, *head_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 int copied;
2143 int err = 0;
2144 int skb_len;
2145
Daniel Borkmannbb333812013-06-28 19:49:40 +02002146 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2147 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2148 addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
wangweidong048ed4b2014-01-21 15:44:11 +08002150 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03002152 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
Xin Longe0878692016-07-30 14:14:41 +08002153 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 err = -ENOTCONN;
2155 goto out;
2156 }
2157
2158 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2159 if (!skb)
2160 goto out;
2161
2162 /* Get the total length of the skb including any skb's in the
2163 * frag_list.
2164 */
2165 skb_len = skb->len;
2166
2167 copied = skb_len;
2168 if (copied > len)
2169 copied = len;
2170
David S. Miller51f3d022014-11-05 16:46:40 -05002171 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 event = sctp_skb2event(skb);
2174
2175 if (err)
2176 goto out_free;
2177
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002178 if (event->chunk && event->chunk->head_skb)
2179 head_skb = event->chunk->head_skb;
2180 else
2181 head_skb = skb;
2182 sock_recv_ts_and_drops(msg, sk, head_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (sctp_ulpevent_is_notification(event)) {
2184 msg->msg_flags |= MSG_NOTIFICATION;
2185 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2186 } else {
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002187 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
2189
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02002190 /* Check if we allow SCTP_NXTINFO. */
2191 if (sp->recvnxtinfo)
2192 sctp_ulpevent_read_nxtinfo(event, msg, sk);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002193 /* Check if we allow SCTP_RCVINFO. */
2194 if (sp->recvrcvinfo)
2195 sctp_ulpevent_read_rcvinfo(event, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 /* Check if we allow SCTP_SNDRCVINFO. */
2197 if (sp->subscribe.sctp_data_io_event)
2198 sctp_ulpevent_read_sndrcvinfo(event, msg);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 err = copied;
2201
2202 /* If skb's length exceeds the user's buffer, update the skb and
2203 * push it back to the receive_queue so that the next call to
2204 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2205 */
2206 if (skb_len > copied) {
2207 msg->msg_flags &= ~MSG_EOR;
2208 if (flags & MSG_PEEK)
2209 goto out_free;
2210 sctp_skb_pull(skb, copied);
2211 skb_queue_head(&sk->sk_receive_queue, skb);
2212
Daniel Borkmann362d5202014-04-14 21:45:17 +02002213 /* When only partial message is copied to the user, increase
2214 * rwnd by that amount. If all the data in the skb is read,
2215 * rwnd is updated when the event is freed.
2216 */
2217 if (!sctp_ulpevent_is_notification(event))
2218 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 goto out;
2220 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2221 (event->msg_flags & MSG_EOR))
2222 msg->msg_flags |= MSG_EOR;
2223 else
2224 msg->msg_flags &= ~MSG_EOR;
2225
2226out_free:
2227 if (flags & MSG_PEEK) {
2228 /* Release the skb reference acquired after peeking the skb in
2229 * sctp_skb_recv_datagram().
2230 */
2231 kfree_skb(skb);
2232 } else {
2233 /* Free the event which includes releasing the reference to
2234 * the owner of the skb, freeing the skb and updating the
2235 * rwnd.
2236 */
2237 sctp_ulpevent_free(event);
2238 }
2239out:
wangweidong048ed4b2014-01-21 15:44:11 +08002240 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return err;
2242}
2243
2244/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2245 *
2246 * This option is a on/off flag. If enabled no SCTP message
2247 * fragmentation will be performed. Instead if a message being sent
2248 * exceeds the current PMTU size, the message will NOT be sent and
2249 * instead a error will be indicated to the user.
2250 */
2251static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002252 char __user *optval,
2253 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
2255 int val;
2256
2257 if (optlen < sizeof(int))
2258 return -EINVAL;
2259
2260 if (get_user(val, (int __user *)optval))
2261 return -EFAULT;
2262
2263 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2264
2265 return 0;
2266}
2267
2268static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002269 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Wei Yongjun94912302011-07-02 09:28:04 +00002271 struct sctp_association *asoc;
2272 struct sctp_ulpevent *event;
2273
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002274 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return -EINVAL;
2276 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2277 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002278
Daniel Borkmannbbbea412014-07-12 20:30:40 +02002279 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
Wei Yongjun94912302011-07-02 09:28:04 +00002280 * if there is no data to be sent or retransmit, the stack will
2281 * immediately send up this notification.
2282 */
2283 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2284 &sctp_sk(sk)->subscribe)) {
2285 asoc = sctp_id2assoc(sk, 0);
2286
2287 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2288 event = sctp_ulpevent_make_sender_dry_event(asoc,
Marcelo Ricardo Leitner2e83acb2018-01-08 19:02:27 -02002289 GFP_USER | __GFP_NOWARN);
Wei Yongjun94912302011-07-02 09:28:04 +00002290 if (!event)
2291 return -ENOMEM;
2292
Xin Long9162e0e2017-12-08 21:04:05 +08002293 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
Wei Yongjun94912302011-07-02 09:28:04 +00002294 }
2295 }
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return 0;
2298}
2299
2300/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2301 *
2302 * This socket option is applicable to the UDP-style socket only. When
2303 * set it will cause associations that are idle for more than the
2304 * specified number of seconds to automatically close. An association
2305 * being idle is defined an association that has NOT sent or received
2306 * user data. The special value of '0' indicates that no automatic
2307 * close of any associations should be performed. The option expects an
2308 * integer defining the number of seconds of idle time before an
2309 * association is closed.
2310 */
2311static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002312 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
2314 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman9f70f462013-12-10 06:48:15 -05002315 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 /* Applicable to UDP-style socket only */
2318 if (sctp_style(sk, TCP))
2319 return -EOPNOTSUPP;
2320 if (optlen != sizeof(int))
2321 return -EINVAL;
2322 if (copy_from_user(&sp->autoclose, optval, optlen))
2323 return -EFAULT;
2324
Neil Horman9f70f462013-12-10 06:48:15 -05002325 if (sp->autoclose > net->sctp.max_autoclose)
2326 sp->autoclose = net->sctp.max_autoclose;
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return 0;
2329}
2330
2331/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2332 *
2333 * Applications can enable or disable heartbeats for any peer address of
2334 * an association, modify an address's heartbeat interval, force a
2335 * heartbeat to be sent immediately, and adjust the address's maximum
2336 * number of retransmissions sent before an address is considered
2337 * unreachable. The following structure is used to access and modify an
2338 * address's parameters:
2339 *
2340 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002341 * sctp_assoc_t spp_assoc_id;
2342 * struct sockaddr_storage spp_address;
2343 * uint32_t spp_hbinterval;
2344 * uint16_t spp_pathmaxrxt;
2345 * uint32_t spp_pathmtu;
2346 * uint32_t spp_sackdelay;
2347 * uint32_t spp_flags;
2348 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002350 * spp_assoc_id - (one-to-many style socket) This is filled in the
2351 * application, and identifies the association for
2352 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 * spp_address - This specifies which address is of interest.
2354 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002355 * in milliseconds. If a value of zero
2356 * is present in this field then no changes are to
2357 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 * spp_pathmaxrxt - This contains the maximum number of
2359 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002360 * considered unreachable. If a value of zero
2361 * is present in this field then no changes are to
2362 * be made to this parameter.
2363 * spp_pathmtu - When Path MTU discovery is disabled the value
2364 * specified here will be the "fixed" path mtu.
2365 * Note that if the spp_address field is empty
2366 * then all associations on this address will
2367 * have this fixed path mtu set upon them.
2368 *
2369 * spp_sackdelay - When delayed sack is enabled, this value specifies
2370 * the number of milliseconds that sacks will be delayed
2371 * for. This value will apply to all addresses of an
2372 * association if the spp_address field is empty. Note
2373 * also, that if delayed sack is enabled and this
2374 * value is set to 0, no change is made to the last
2375 * recorded delayed sack timer value.
2376 *
2377 * spp_flags - These flags are used to control various features
2378 * on an association. The flag field may contain
2379 * zero or more of the following options.
2380 *
2381 * SPP_HB_ENABLE - Enable heartbeats on the
2382 * specified address. Note that if the address
2383 * field is empty all addresses for the association
2384 * have heartbeats enabled upon them.
2385 *
2386 * SPP_HB_DISABLE - Disable heartbeats on the
2387 * speicifed address. Note that if the address
2388 * field is empty all addresses for the association
2389 * will have their heartbeats disabled. Note also
2390 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2391 * mutually exclusive, only one of these two should
2392 * be specified. Enabling both fields will have
2393 * undetermined results.
2394 *
2395 * SPP_HB_DEMAND - Request a user initiated heartbeat
2396 * to be made immediately.
2397 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002398 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2399 * heartbeat delayis to be set to the value of 0
2400 * milliseconds.
2401 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002402 * SPP_PMTUD_ENABLE - This field will enable PMTU
2403 * discovery upon the specified address. Note that
2404 * if the address feild is empty then all addresses
2405 * on the association are effected.
2406 *
2407 * SPP_PMTUD_DISABLE - This field will disable PMTU
2408 * discovery upon the specified address. Note that
2409 * if the address feild is empty then all addresses
2410 * on the association are effected. Not also that
2411 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2412 * exclusive. Enabling both will have undetermined
2413 * results.
2414 *
2415 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2416 * on delayed sack. The time specified in spp_sackdelay
2417 * is used to specify the sack delay for this address. Note
2418 * that if spp_address is empty then all addresses will
2419 * enable delayed sack and take on the sack delay
2420 * value specified in spp_sackdelay.
2421 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2422 * off delayed sack. If the spp_address field is blank then
2423 * delayed sack is disabled for the entire association. Note
2424 * also that this field is mutually exclusive to
2425 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2426 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 */
Adrian Bunk16164362006-09-18 00:40:38 -07002428static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2429 struct sctp_transport *trans,
2430 struct sctp_association *asoc,
2431 struct sctp_sock *sp,
2432 int hb_change,
2433 int pmtud_change,
2434 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 int error;
2437
Frank Filz52ccb8e2005-12-22 11:36:46 -08002438 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002439 struct net *net = sock_net(trans->asoc->base.sk);
2440
2441 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (error)
2443 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002446 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2447 * this field is ignored. Note also that a value of zero indicates
2448 * the current setting should be left unchanged.
2449 */
2450 if (params->spp_flags & SPP_HB_ENABLE) {
2451
2452 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2453 * set. This lets us use 0 value when this flag
2454 * is set.
2455 */
2456 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2457 params->spp_hbinterval = 0;
2458
2459 if (params->spp_hbinterval ||
2460 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2461 if (trans) {
2462 trans->hbinterval =
2463 msecs_to_jiffies(params->spp_hbinterval);
2464 } else if (asoc) {
2465 asoc->hbinterval =
2466 msecs_to_jiffies(params->spp_hbinterval);
2467 } else {
2468 sp->hbinterval = params->spp_hbinterval;
2469 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002470 }
2471 }
2472
2473 if (hb_change) {
2474 if (trans) {
2475 trans->param_flags =
2476 (trans->param_flags & ~SPP_HB) | hb_change;
2477 } else if (asoc) {
2478 asoc->param_flags =
2479 (asoc->param_flags & ~SPP_HB) | hb_change;
2480 } else {
2481 sp->param_flags =
2482 (sp->param_flags & ~SPP_HB) | hb_change;
2483 }
2484 }
2485
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002486 /* When Path MTU discovery is disabled the value specified here will
2487 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2488 * include the flag SPP_PMTUD_DISABLE for this field to have any
2489 * effect).
2490 */
2491 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002492 if (trans) {
2493 trans->pathmtu = params->spp_pathmtu;
Xin Long3ebfdf02017-04-04 13:39:55 +08002494 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002495 } else if (asoc) {
2496 asoc->pathmtu = params->spp_pathmtu;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002497 } else {
2498 sp->pathmtu = params->spp_pathmtu;
2499 }
2500 }
2501
2502 if (pmtud_change) {
2503 if (trans) {
2504 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2505 (params->spp_flags & SPP_PMTUD_ENABLE);
2506 trans->param_flags =
2507 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2508 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002509 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
Xin Long3ebfdf02017-04-04 13:39:55 +08002510 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002511 }
2512 } else if (asoc) {
2513 asoc->param_flags =
2514 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2515 } else {
2516 sp->param_flags =
2517 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2518 }
2519 }
2520
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002521 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2522 * value of this field is ignored. Note also that a value of zero
2523 * indicates the current setting should be left unchanged.
2524 */
2525 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002526 if (trans) {
2527 trans->sackdelay =
2528 msecs_to_jiffies(params->spp_sackdelay);
2529 } else if (asoc) {
2530 asoc->sackdelay =
2531 msecs_to_jiffies(params->spp_sackdelay);
2532 } else {
2533 sp->sackdelay = params->spp_sackdelay;
2534 }
2535 }
2536
2537 if (sackdelay_change) {
2538 if (trans) {
2539 trans->param_flags =
2540 (trans->param_flags & ~SPP_SACKDELAY) |
2541 sackdelay_change;
2542 } else if (asoc) {
2543 asoc->param_flags =
2544 (asoc->param_flags & ~SPP_SACKDELAY) |
2545 sackdelay_change;
2546 } else {
2547 sp->param_flags =
2548 (sp->param_flags & ~SPP_SACKDELAY) |
2549 sackdelay_change;
2550 }
2551 }
2552
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002553 /* Note that a value of zero indicates the current setting should be
2554 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002555 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002556 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002557 if (trans) {
2558 trans->pathmaxrxt = params->spp_pathmaxrxt;
2559 } else if (asoc) {
2560 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2561 } else {
2562 sp->pathmaxrxt = params->spp_pathmaxrxt;
2563 }
2564 }
2565
2566 return 0;
2567}
2568
2569static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002570 char __user *optval,
2571 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002572{
2573 struct sctp_paddrparams params;
2574 struct sctp_transport *trans = NULL;
2575 struct sctp_association *asoc = NULL;
2576 struct sctp_sock *sp = sctp_sk(sk);
2577 int error;
2578 int hb_change, pmtud_change, sackdelay_change;
2579
2580 if (optlen != sizeof(struct sctp_paddrparams))
wangweidongcb3f8372013-12-23 12:16:50 +08002581 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002582
2583 if (copy_from_user(&params, optval, optlen))
2584 return -EFAULT;
2585
2586 /* Validate flags and value parameters. */
2587 hb_change = params.spp_flags & SPP_HB;
2588 pmtud_change = params.spp_flags & SPP_PMTUD;
2589 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2590
2591 if (hb_change == SPP_HB ||
2592 pmtud_change == SPP_PMTUD ||
2593 sackdelay_change == SPP_SACKDELAY ||
2594 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002595 (params.spp_pathmtu &&
2596 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002597 return -EINVAL;
2598
2599 /* If an address other than INADDR_ANY is specified, and
2600 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 */
wangweidongcb3f8372013-12-23 12:16:50 +08002602 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002603 trans = sctp_addr_id2transport(sk, &params.spp_address,
2604 params.spp_assoc_id);
2605 if (!trans)
2606 return -EINVAL;
2607 }
2608
2609 /* Get association, if assoc_id != 0 and the socket is a one
2610 * to many style socket, and an association was not found, then
2611 * the id was invalid.
2612 */
2613 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2614 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2615 return -EINVAL;
2616
2617 /* Heartbeat demand can only be sent on a transport or
2618 * association, but not a socket.
2619 */
2620 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2621 return -EINVAL;
2622
2623 /* Process parameters. */
2624 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2625 hb_change, pmtud_change,
2626 sackdelay_change);
2627
2628 if (error)
2629 return error;
2630
2631 /* If changes are for association, also apply parameters to each
2632 * transport.
2633 */
2634 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002635 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2636 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002637 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2638 hb_change, pmtud_change,
2639 sackdelay_change);
2640 }
2641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 return 0;
2644}
2645
wangweidong0ea5e4d2014-01-15 17:24:01 +08002646static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2647{
2648 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2649}
2650
2651static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2652{
2653 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2654}
2655
Wei Yongjund364d922008-05-09 15:13:26 -07002656/*
2657 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002658 *
Wei Yongjund364d922008-05-09 15:13:26 -07002659 * This option will effect the way delayed acks are performed. This
2660 * option allows you to get or set the delayed ack time, in
2661 * milliseconds. It also allows changing the delayed ack frequency.
2662 * Changing the frequency to 1 disables the delayed sack algorithm. If
2663 * the assoc_id is 0, then this sets or gets the endpoints default
2664 * values. If the assoc_id field is non-zero, then the set or get
2665 * effects the specified association for the one to many model (the
2666 * assoc_id field is ignored by the one to one model). Note that if
2667 * sack_delay or sack_freq are 0 when setting this option, then the
2668 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002669 *
Wei Yongjund364d922008-05-09 15:13:26 -07002670 * struct sctp_sack_info {
2671 * sctp_assoc_t sack_assoc_id;
2672 * uint32_t sack_delay;
2673 * uint32_t sack_freq;
2674 * };
Frank Filz77086102005-12-22 11:37:30 -08002675 *
Wei Yongjund364d922008-05-09 15:13:26 -07002676 * sack_assoc_id - This parameter, indicates which association the user
2677 * is performing an action upon. Note that if this field's value is
2678 * zero then the endpoints default value is changed (effecting future
2679 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002680 *
Wei Yongjund364d922008-05-09 15:13:26 -07002681 * sack_delay - This parameter contains the number of milliseconds that
2682 * the user is requesting the delayed ACK timer be set to. Note that
2683 * this value is defined in the standard to be between 200 and 500
2684 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002685 *
Wei Yongjund364d922008-05-09 15:13:26 -07002686 * sack_freq - This parameter contains the number of packets that must
2687 * be received before a sack is sent without waiting for the delay
2688 * timer to expire. The default value for this is 2, setting this
2689 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002690 */
2691
Wei Yongjund364d922008-05-09 15:13:26 -07002692static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002693 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002694{
Wei Yongjund364d922008-05-09 15:13:26 -07002695 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002696 struct sctp_transport *trans = NULL;
2697 struct sctp_association *asoc = NULL;
2698 struct sctp_sock *sp = sctp_sk(sk);
2699
Wei Yongjund364d922008-05-09 15:13:26 -07002700 if (optlen == sizeof(struct sctp_sack_info)) {
2701 if (copy_from_user(&params, optval, optlen))
2702 return -EFAULT;
2703
2704 if (params.sack_delay == 0 && params.sack_freq == 0)
2705 return 0;
2706 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05002707 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05002708 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05002709 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05002710 "Use struct sctp_sack_info instead\n",
2711 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07002712 if (copy_from_user(&params, optval, optlen))
2713 return -EFAULT;
2714
2715 if (params.sack_delay == 0)
2716 params.sack_freq = 1;
2717 else
2718 params.sack_freq = 0;
2719 } else
wangweidongcb3f8372013-12-23 12:16:50 +08002720 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08002721
Frank Filz77086102005-12-22 11:37:30 -08002722 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002723 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002724 return -EINVAL;
2725
Wei Yongjund364d922008-05-09 15:13:26 -07002726 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002727 * to many style socket, and an association was not found, then
2728 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002729 */
Wei Yongjund364d922008-05-09 15:13:26 -07002730 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2731 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002732 return -EINVAL;
2733
Wei Yongjund364d922008-05-09 15:13:26 -07002734 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002735 if (asoc) {
2736 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002737 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002738 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002739 sctp_spp_sackdelay_enable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002740 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002741 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002742 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002743 sctp_spp_sackdelay_enable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002744 }
Wei Yongjund364d922008-05-09 15:13:26 -07002745 }
2746
2747 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002748 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002749 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002750 sctp_spp_sackdelay_disable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002751 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002752 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002753 sctp_spp_sackdelay_disable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002754 }
Wei Yongjund364d922008-05-09 15:13:26 -07002755 } else if (params.sack_freq > 1) {
2756 if (asoc) {
2757 asoc->sackfreq = params.sack_freq;
2758 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002759 sctp_spp_sackdelay_enable(asoc->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002760 } else {
2761 sp->sackfreq = params.sack_freq;
2762 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002763 sctp_spp_sackdelay_enable(sp->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002764 }
Frank Filz77086102005-12-22 11:37:30 -08002765 }
2766
2767 /* If change is for association, also apply to each transport. */
2768 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002769 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2770 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002771 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002772 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002773 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002774 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002775 sctp_spp_sackdelay_enable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002776 }
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07002777 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002778 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002779 sctp_spp_sackdelay_disable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002780 } else if (params.sack_freq > 1) {
2781 trans->sackfreq = params.sack_freq;
2782 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002783 sctp_spp_sackdelay_enable(trans->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002784 }
2785 }
2786 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002787
Frank Filz77086102005-12-22 11:37:30 -08002788 return 0;
2789}
2790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2792 *
2793 * Applications can specify protocol parameters for the default association
2794 * initialization. The option name argument to setsockopt() and getsockopt()
2795 * is SCTP_INITMSG.
2796 *
2797 * Setting initialization parameters is effective only on an unconnected
2798 * socket (for UDP-style sockets only future associations are effected
2799 * by the change). With TCP-style sockets, this option is inherited by
2800 * sockets derived from a listener socket.
2801 */
David S. Millerb7058842009-09-30 16:12:20 -07002802static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
2804 struct sctp_initmsg sinit;
2805 struct sctp_sock *sp = sctp_sk(sk);
2806
2807 if (optlen != sizeof(struct sctp_initmsg))
2808 return -EINVAL;
2809 if (copy_from_user(&sinit, optval, optlen))
2810 return -EFAULT;
2811
2812 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002813 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002815 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002817 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002819 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 return 0;
2822}
2823
2824/*
2825 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2826 *
2827 * Applications that wish to use the sendto() system call may wish to
2828 * specify a default set of parameters that would normally be supplied
2829 * through the inclusion of ancillary data. This socket option allows
2830 * such an application to set the default sctp_sndrcvinfo structure.
2831 * The application that wishes to use this socket option simply passes
2832 * in to this call the sctp_sndrcvinfo structure defined in Section
2833 * 5.2.2) The input parameters accepted by this call include
2834 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2835 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2836 * to this call if the caller is using the UDP model.
2837 */
2838static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002839 char __user *optval,
2840 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002843 struct sctp_association *asoc;
2844 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002846 if (optlen != sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return -EINVAL;
2848 if (copy_from_user(&info, optval, optlen))
2849 return -EFAULT;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002850 if (info.sinfo_flags &
2851 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2852 SCTP_ABORT | SCTP_EOF))
2853 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
2855 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2856 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2857 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 if (asoc) {
2859 asoc->default_stream = info.sinfo_stream;
2860 asoc->default_flags = info.sinfo_flags;
2861 asoc->default_ppid = info.sinfo_ppid;
2862 asoc->default_context = info.sinfo_context;
2863 asoc->default_timetolive = info.sinfo_timetolive;
2864 } else {
2865 sp->default_stream = info.sinfo_stream;
2866 sp->default_flags = info.sinfo_flags;
2867 sp->default_ppid = info.sinfo_ppid;
2868 sp->default_context = info.sinfo_context;
2869 sp->default_timetolive = info.sinfo_timetolive;
2870 }
2871
2872 return 0;
2873}
2874
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002875/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2876 * (SCTP_DEFAULT_SNDINFO)
2877 */
2878static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2879 char __user *optval,
2880 unsigned int optlen)
2881{
2882 struct sctp_sock *sp = sctp_sk(sk);
2883 struct sctp_association *asoc;
2884 struct sctp_sndinfo info;
2885
2886 if (optlen != sizeof(info))
2887 return -EINVAL;
2888 if (copy_from_user(&info, optval, optlen))
2889 return -EFAULT;
2890 if (info.snd_flags &
2891 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2892 SCTP_ABORT | SCTP_EOF))
2893 return -EINVAL;
2894
2895 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
2896 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
2897 return -EINVAL;
2898 if (asoc) {
2899 asoc->default_stream = info.snd_sid;
2900 asoc->default_flags = info.snd_flags;
2901 asoc->default_ppid = info.snd_ppid;
2902 asoc->default_context = info.snd_context;
2903 } else {
2904 sp->default_stream = info.snd_sid;
2905 sp->default_flags = info.snd_flags;
2906 sp->default_ppid = info.snd_ppid;
2907 sp->default_context = info.snd_context;
2908 }
2909
2910 return 0;
2911}
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2914 *
2915 * Requests that the local SCTP stack use the enclosed peer address as
2916 * the association primary. The enclosed address must be one of the
2917 * association peer's addresses.
2918 */
2919static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002920 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
2922 struct sctp_prim prim;
2923 struct sctp_transport *trans;
2924
2925 if (optlen != sizeof(struct sctp_prim))
2926 return -EINVAL;
2927
2928 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2929 return -EFAULT;
2930
2931 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2932 if (!trans)
2933 return -EINVAL;
2934
2935 sctp_assoc_set_primary(trans->asoc, trans);
2936
2937 return 0;
2938}
2939
2940/*
2941 * 7.1.5 SCTP_NODELAY
2942 *
2943 * Turn on/off any Nagle-like algorithm. This means that packets are
2944 * generally sent as soon as possible and no unnecessary delays are
2945 * introduced, at the cost of more packets in the network. Expects an
2946 * integer boolean flag.
2947 */
2948static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002949 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950{
2951 int val;
2952
2953 if (optlen < sizeof(int))
2954 return -EINVAL;
2955 if (get_user(val, (int __user *)optval))
2956 return -EFAULT;
2957
2958 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2959 return 0;
2960}
2961
2962/*
2963 *
2964 * 7.1.1 SCTP_RTOINFO
2965 *
2966 * The protocol parameters used to initialize and bound retransmission
2967 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2968 * and modify these parameters.
2969 * All parameters are time values, in milliseconds. A value of 0, when
2970 * modifying the parameters, indicates that the current value should not
2971 * be changed.
2972 *
2973 */
David S. Millerb7058842009-09-30 16:12:20 -07002974static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2975{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 struct sctp_rtoinfo rtoinfo;
2977 struct sctp_association *asoc;
wangweidong85f935d2013-12-11 09:50:38 +08002978 unsigned long rto_min, rto_max;
2979 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 if (optlen != sizeof (struct sctp_rtoinfo))
2982 return -EINVAL;
2983
2984 if (copy_from_user(&rtoinfo, optval, optlen))
2985 return -EFAULT;
2986
2987 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2988
2989 /* Set the values to the specific association */
2990 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2991 return -EINVAL;
2992
wangweidong85f935d2013-12-11 09:50:38 +08002993 rto_max = rtoinfo.srto_max;
2994 rto_min = rtoinfo.srto_min;
2995
2996 if (rto_max)
2997 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
2998 else
2999 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3000
3001 if (rto_min)
3002 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3003 else
3004 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3005
3006 if (rto_min > rto_max)
3007 return -EINVAL;
3008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 if (asoc) {
3010 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003011 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 msecs_to_jiffies(rtoinfo.srto_initial);
wangweidong85f935d2013-12-11 09:50:38 +08003013 asoc->rto_max = rto_max;
3014 asoc->rto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 } else {
3016 /* If there is no association or the association-id = 0
3017 * set the values to the endpoint.
3018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 if (rtoinfo.srto_initial != 0)
3020 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
wangweidong85f935d2013-12-11 09:50:38 +08003021 sp->rtoinfo.srto_max = rto_max;
3022 sp->rtoinfo.srto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 }
3024
3025 return 0;
3026}
3027
3028/*
3029 *
3030 * 7.1.2 SCTP_ASSOCINFO
3031 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02003032 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 * of the association.
3034 * Returns an error if the new association retransmission value is
3035 * greater than the sum of the retransmission value of the peer.
3036 * See [SCTP] for more information.
3037 *
3038 */
David S. Millerb7058842009-09-30 16:12:20 -07003039static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
3041
3042 struct sctp_assocparams assocparams;
3043 struct sctp_association *asoc;
3044
3045 if (optlen != sizeof(struct sctp_assocparams))
3046 return -EINVAL;
3047 if (copy_from_user(&assocparams, optval, optlen))
3048 return -EFAULT;
3049
3050 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3051
3052 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3053 return -EINVAL;
3054
3055 /* Set the values to the specific association */
3056 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003057 if (assocparams.sasoc_asocmaxrxt != 0) {
3058 __u32 path_sum = 0;
3059 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003060 struct sctp_transport *peer_addr;
3061
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07003062 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3063 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003064 path_sum += peer_addr->pathmaxrxt;
3065 paths++;
3066 }
3067
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003068 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003069 * one path/transport. We do this because path
3070 * retransmissions are only counted when we have more
3071 * then one path.
3072 */
3073 if (paths > 1 &&
3074 assocparams.sasoc_asocmaxrxt > path_sum)
3075 return -EINVAL;
3076
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003078 }
3079
Daniel Borkmann52db8822013-06-25 18:17:27 +02003080 if (assocparams.sasoc_cookie_life != 0)
3081 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 } else {
3083 /* Set the values to the endpoint */
3084 struct sctp_sock *sp = sctp_sk(sk);
3085
3086 if (assocparams.sasoc_asocmaxrxt != 0)
3087 sp->assocparams.sasoc_asocmaxrxt =
3088 assocparams.sasoc_asocmaxrxt;
3089 if (assocparams.sasoc_cookie_life != 0)
3090 sp->assocparams.sasoc_cookie_life =
3091 assocparams.sasoc_cookie_life;
3092 }
3093 return 0;
3094}
3095
3096/*
3097 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3098 *
3099 * This socket option is a boolean flag which turns on or off mapped V4
3100 * addresses. If this option is turned on and the socket is type
3101 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3102 * If this option is turned off, then no mapping will be done of V4
3103 * addresses and a user will receive both PF_INET6 and PF_INET type
3104 * addresses on the socket.
3105 */
David S. Millerb7058842009-09-30 16:12:20 -07003106static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
3108 int val;
3109 struct sctp_sock *sp = sctp_sk(sk);
3110
3111 if (optlen < sizeof(int))
3112 return -EINVAL;
3113 if (get_user(val, (int __user *)optval))
3114 return -EFAULT;
3115 if (val)
3116 sp->v4mapped = 1;
3117 else
3118 sp->v4mapped = 0;
3119
3120 return 0;
3121}
3122
3123/*
Wei Yongjune89c2092008-12-25 16:54:58 -08003124 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3125 * This option will get or set the maximum size to put in any outgoing
3126 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 * fragmented by SCTP into the specified size. Note that the underlying
3128 * SCTP implementation may fragment into smaller sized chunks when the
3129 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08003130 * the user. The default value for this option is '0' which indicates
3131 * the user is NOT limiting fragmentation and only the PMTU will effect
3132 * SCTP's choice of DATA chunk size. Note also that values set larger
3133 * than the maximum size of an IP datagram will effectively let SCTP
3134 * control fragmentation (i.e. the same as setting this option to 0).
3135 *
3136 * The following structure is used to access and modify this parameter:
3137 *
3138 * struct sctp_assoc_value {
3139 * sctp_assoc_t assoc_id;
3140 * uint32_t assoc_value;
3141 * };
3142 *
3143 * assoc_id: This parameter is ignored for one-to-one style sockets.
3144 * For one-to-many style sockets this parameter indicates which
3145 * association the user is performing an action upon. Note that if
3146 * this field's value is zero then the endpoints default value is
3147 * changed (effecting future associations only).
3148 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 */
David S. Millerb7058842009-09-30 16:12:20 -07003150static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
Xin Longecca8f82017-11-17 14:11:11 +08003152 struct sctp_sock *sp = sctp_sk(sk);
Wei Yongjune89c2092008-12-25 16:54:58 -08003153 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 int val;
3156
Wei Yongjune89c2092008-12-25 16:54:58 -08003157 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003158 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003159 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003160 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003161 "Use struct sctp_assoc_value instead\n",
3162 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08003163 if (copy_from_user(&val, optval, optlen))
3164 return -EFAULT;
3165 params.assoc_id = 0;
3166 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3167 if (copy_from_user(&params, optval, optlen))
3168 return -EFAULT;
3169 val = params.assoc_value;
Xin Longecca8f82017-11-17 14:11:11 +08003170 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return -EINVAL;
Xin Longecca8f82017-11-17 14:11:11 +08003172 }
Wei Yongjune89c2092008-12-25 16:54:58 -08003173
Xin Longecca8f82017-11-17 14:11:11 +08003174 if (val) {
3175 int min_len, max_len;
3176
3177 min_len = SCTP_DEFAULT_MINSEGMENT - sp->pf->af->net_header_len;
3178 min_len -= sizeof(struct sctphdr) +
3179 sizeof(struct sctp_data_chunk);
3180
3181 max_len = SCTP_MAX_CHUNK_LEN - sizeof(struct sctp_data_chunk);
3182
3183 if (val < min_len || val > max_len)
3184 return -EINVAL;
3185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Wei Yongjune89c2092008-12-25 16:54:58 -08003187 asoc = sctp_id2assoc(sk, params.assoc_id);
Wei Yongjune89c2092008-12-25 16:54:58 -08003188 if (asoc) {
3189 if (val == 0) {
Xin Longecca8f82017-11-17 14:11:11 +08003190 val = asoc->pathmtu - sp->pf->af->net_header_len;
Wei Yongjune89c2092008-12-25 16:54:58 -08003191 val -= sizeof(struct sctphdr) +
Xin Long668c9be2017-12-08 21:04:02 +08003192 sctp_datachk_len(&asoc->stream);
Wei Yongjune89c2092008-12-25 16:54:58 -08003193 }
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003194 asoc->user_frag = val;
3195 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
Wei Yongjune89c2092008-12-25 16:54:58 -08003196 } else {
Xin Longecca8f82017-11-17 14:11:11 +08003197 if (params.assoc_id && sctp_style(sk, UDP))
3198 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003199 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 }
3201
3202 return 0;
3203}
3204
3205
3206/*
3207 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3208 *
3209 * Requests that the peer mark the enclosed address as the association
3210 * primary. The enclosed address must be one of the association's
3211 * locally bound addresses. The following structure is used to make a
3212 * set primary request:
3213 */
3214static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003215 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003217 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 struct sctp_association *asoc = NULL;
3220 struct sctp_setpeerprim prim;
3221 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003222 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 int err;
3224
3225 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003227 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return -EPERM;
3229
3230 if (optlen != sizeof(struct sctp_setpeerprim))
3231 return -EINVAL;
3232
3233 if (copy_from_user(&prim, optval, optlen))
3234 return -EFAULT;
3235
3236 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003237 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 return -EINVAL;
3239
3240 if (!asoc->peer.asconf_capable)
3241 return -EPERM;
3242
3243 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3244 return -EPERM;
3245
3246 if (!sctp_state(asoc, ESTABLISHED))
3247 return -ENOTCONN;
3248
Wei Yongjun40a01032010-12-07 17:11:09 +00003249 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3250 if (!af)
3251 return -EINVAL;
3252
3253 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3254 return -EADDRNOTAVAIL;
3255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3257 return -EADDRNOTAVAIL;
3258
3259 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3260 chunk = sctp_make_asconf_set_prim(asoc,
3261 (union sctp_addr *)&prim.sspp_addr);
3262 if (!chunk)
3263 return -ENOMEM;
3264
3265 err = sctp_send_asconf(asoc, chunk);
3266
Daniel Borkmannbb333812013-06-28 19:49:40 +02003267 pr_debug("%s: we set peer primary addr primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
3269 return err;
3270}
3271
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003272static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003273 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003275 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003277 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003279 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 return -EFAULT;
3281
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003282 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
3284 return 0;
3285}
3286
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003287/*
3288 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3289 *
3290 * The context field in the sctp_sndrcvinfo structure is normally only
3291 * used when a failed message is retrieved holding the value that was
3292 * sent down on the actual send call. This option allows the setting of
3293 * a default context on an association basis that will be received on
3294 * reading messages from the peer. This is especially helpful in the
3295 * one-2-many model for an application to keep some reference to an
3296 * internal state machine that is processing messages on the
3297 * association. Note that the setting of this value only effects
3298 * received messages from the peer and does not effect the value that is
3299 * saved with outbound messages.
3300 */
3301static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003302 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003303{
3304 struct sctp_assoc_value params;
3305 struct sctp_sock *sp;
3306 struct sctp_association *asoc;
3307
3308 if (optlen != sizeof(struct sctp_assoc_value))
3309 return -EINVAL;
3310 if (copy_from_user(&params, optval, optlen))
3311 return -EFAULT;
3312
3313 sp = sctp_sk(sk);
3314
3315 if (params.assoc_id != 0) {
3316 asoc = sctp_id2assoc(sk, params.assoc_id);
3317 if (!asoc)
3318 return -EINVAL;
3319 asoc->default_rcv_context = params.assoc_value;
3320 } else {
3321 sp->default_rcv_context = params.assoc_value;
3322 }
3323
3324 return 0;
3325}
3326
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003327/*
3328 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3329 *
3330 * This options will at a minimum specify if the implementation is doing
3331 * fragmented interleave. Fragmented interleave, for a one to many
3332 * socket, is when subsequent calls to receive a message may return
3333 * parts of messages from different associations. Some implementations
3334 * may allow you to turn this value on or off. If so, when turned off,
3335 * no fragment interleave will occur (which will cause a head of line
3336 * blocking amongst multiple associations sharing the same one to many
3337 * socket). When this option is turned on, then each receive call may
3338 * come from a different association (thus the user must receive data
3339 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3340 * association each receive belongs to.
3341 *
3342 * This option takes a boolean value. A non-zero value indicates that
3343 * fragmented interleave is on. A value of zero indicates that
3344 * fragmented interleave is off.
3345 *
3346 * Note that it is important that an implementation that allows this
3347 * option to be turned on, have it off by default. Otherwise an unaware
3348 * application using the one to many model may become confused and act
3349 * incorrectly.
3350 */
3351static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3352 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003353 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003354{
3355 int val;
3356
3357 if (optlen != sizeof(int))
3358 return -EINVAL;
3359 if (get_user(val, (int __user *)optval))
3360 return -EFAULT;
3361
Xin Long772a5862017-12-08 21:03:58 +08003362 sctp_sk(sk)->frag_interleave = !!val;
3363
3364 if (!sctp_sk(sk)->frag_interleave)
3365 sctp_sk(sk)->strm_interleave = 0;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003366
3367 return 0;
3368}
3369
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003370/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003371 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003372 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003373 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003374 * This option will set or get the SCTP partial delivery point. This
3375 * point is the size of a message where the partial delivery API will be
3376 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003377 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003378 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003379 * point. Note also that the call will fail if the user attempts to set
3380 * this value larger than the socket receive buffer size.
3381 *
3382 * Note that any single message having a length smaller than or equal to
3383 * the SCTP partial delivery point will be delivered in one single read
3384 * call as long as the user provided buffer is large enough to hold the
3385 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003386 */
3387static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3388 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003389 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003390{
3391 u32 val;
3392
3393 if (optlen != sizeof(u32))
3394 return -EINVAL;
3395 if (get_user(val, (int __user *)optval))
3396 return -EFAULT;
3397
Wei Yongjun8510b932008-12-25 16:59:03 -08003398 /* Note: We double the receive buffer from what the user sets
3399 * it to be, also initial rwnd is based on rcvbuf/2.
3400 */
3401 if (val > (sk->sk_rcvbuf >> 1))
3402 return -EINVAL;
3403
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003404 sctp_sk(sk)->pd_point = val;
3405
3406 return 0; /* is this the right error code? */
3407}
3408
Vlad Yasevich70331572007-03-23 11:34:36 -07003409/*
3410 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3411 *
3412 * This option will allow a user to change the maximum burst of packets
3413 * that can be emitted by this association. Note that the default value
3414 * is 4, and some implementations may restrict this setting so that it
3415 * can only be lowered.
3416 *
3417 * NOTE: This text doesn't seem right. Do this on a socket basis with
3418 * future associations inheriting the socket value.
3419 */
3420static int sctp_setsockopt_maxburst(struct sock *sk,
3421 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003422 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003423{
Neil Horman219b99a2008-03-05 13:44:46 -08003424 struct sctp_assoc_value params;
3425 struct sctp_sock *sp;
3426 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003427 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003428 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003429
Neil Horman219b99a2008-03-05 13:44:46 -08003430 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003431 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003432 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003433 "Use of int in max_burst socket option deprecated.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003434 "Use struct sctp_assoc_value instead\n",
3435 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08003436 if (copy_from_user(&val, optval, optlen))
3437 return -EFAULT;
3438 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3439 if (copy_from_user(&params, optval, optlen))
3440 return -EFAULT;
3441 val = params.assoc_value;
3442 assoc_id = params.assoc_id;
3443 } else
3444 return -EINVAL;
3445
3446 sp = sctp_sk(sk);
3447
3448 if (assoc_id != 0) {
3449 asoc = sctp_id2assoc(sk, assoc_id);
3450 if (!asoc)
3451 return -EINVAL;
3452 asoc->max_burst = val;
3453 } else
3454 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003455
3456 return 0;
3457}
3458
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003459/*
3460 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3461 *
3462 * This set option adds a chunk type that the user is requesting to be
3463 * received only in an authenticated way. Changes to the list of chunks
3464 * will only effect future associations on the socket.
3465 */
3466static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003467 char __user *optval,
3468 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003469{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003470 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003471 struct sctp_authchunk val;
3472
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003473 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003474 return -EACCES;
3475
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003476 if (optlen != sizeof(struct sctp_authchunk))
3477 return -EINVAL;
3478 if (copy_from_user(&val, optval, optlen))
3479 return -EFAULT;
3480
3481 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003482 case SCTP_CID_INIT:
3483 case SCTP_CID_INIT_ACK:
3484 case SCTP_CID_SHUTDOWN_COMPLETE:
3485 case SCTP_CID_AUTH:
3486 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003487 }
3488
3489 /* add this chunk id to the endpoint */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003490 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003491}
3492
3493/*
3494 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3495 *
3496 * This option gets or sets the list of HMAC algorithms that the local
3497 * endpoint requires the peer to use.
3498 */
3499static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003500 char __user *optval,
3501 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003502{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003503 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003504 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003505 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003506 int err;
3507
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003508 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003509 return -EACCES;
3510
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003511 if (optlen < sizeof(struct sctp_hmacalgo))
3512 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003513 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3514 SCTP_AUTH_NUM_HMACS * sizeof(u16));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003515
wangweidongcb3f8372013-12-23 12:16:50 +08003516 hmacs = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003517 if (IS_ERR(hmacs))
3518 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003519
Vlad Yasevichd9724052008-08-27 16:09:49 -07003520 idents = hmacs->shmac_num_idents;
3521 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3522 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003523 err = -EINVAL;
3524 goto out;
3525 }
3526
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003527 err = sctp_auth_ep_set_hmacs(ep, hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003528out:
3529 kfree(hmacs);
3530 return err;
3531}
3532
3533/*
3534 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3535 *
3536 * This option will set a shared secret key which is used to build an
3537 * association shared key.
3538 */
3539static int sctp_setsockopt_auth_key(struct sock *sk,
3540 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003541 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003542{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003543 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003544 struct sctp_authkey *authkey;
3545 struct sctp_association *asoc;
3546 int ret;
3547
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003548 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003549 return -EACCES;
3550
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003551 if (optlen <= sizeof(struct sctp_authkey))
3552 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003553 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3554 * this.
3555 */
3556 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3557 sizeof(struct sctp_authkey));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003558
wangweidongcb3f8372013-12-23 12:16:50 +08003559 authkey = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003560 if (IS_ERR(authkey))
3561 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003562
Vlad Yasevich328fc472008-08-27 16:08:54 -07003563 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003564 ret = -EINVAL;
3565 goto out;
3566 }
3567
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003568 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3569 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3570 ret = -EINVAL;
3571 goto out;
3572 }
3573
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003574 ret = sctp_auth_set_key(ep, asoc, authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003575out:
Daniel Borkmann6ba542a2013-02-08 03:04:34 +00003576 kzfree(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003577 return ret;
3578}
3579
3580/*
3581 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3582 *
3583 * This option will get or set the active shared key to be used to build
3584 * the association shared key.
3585 */
3586static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003587 char __user *optval,
3588 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003589{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003590 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003591 struct sctp_authkeyid val;
3592 struct sctp_association *asoc;
3593
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003594 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003595 return -EACCES;
3596
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003597 if (optlen != sizeof(struct sctp_authkeyid))
3598 return -EINVAL;
3599 if (copy_from_user(&val, optval, optlen))
3600 return -EFAULT;
3601
3602 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3603 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3604 return -EINVAL;
3605
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003606 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003607}
3608
3609/*
3610 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3611 *
3612 * This set option will delete a shared secret key from use.
3613 */
3614static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003615 char __user *optval,
3616 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003617{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003618 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003619 struct sctp_authkeyid val;
3620 struct sctp_association *asoc;
3621
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003622 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003623 return -EACCES;
3624
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003625 if (optlen != sizeof(struct sctp_authkeyid))
3626 return -EINVAL;
3627 if (copy_from_user(&val, optval, optlen))
3628 return -EFAULT;
3629
3630 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3631 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3632 return -EINVAL;
3633
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003634 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003635
3636}
3637
Michio Honda7dc04d72011-04-26 20:16:31 +09003638/*
3639 * 8.1.23 SCTP_AUTO_ASCONF
3640 *
3641 * This option will enable or disable the use of the automatic generation of
3642 * ASCONF chunks to add and delete addresses to an existing association. Note
3643 * that this option has two caveats namely: a) it only affects sockets that
3644 * are bound to all addresses available to the SCTP stack, and b) the system
3645 * administrator may have an overriding control that turns the ASCONF feature
3646 * off no matter what setting the socket option may have.
3647 * This option expects an integer boolean flag, where a non-zero value turns on
3648 * the option, and a zero value turns off the option.
3649 * Note. In this implementation, socket operation overrides default parameter
3650 * being set by sysctl as well as FreeBSD implementation
3651 */
3652static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3653 unsigned int optlen)
3654{
3655 int val;
3656 struct sctp_sock *sp = sctp_sk(sk);
3657
3658 if (optlen < sizeof(int))
3659 return -EINVAL;
3660 if (get_user(val, (int __user *)optval))
3661 return -EFAULT;
3662 if (!sctp_is_ep_boundall(sk) && val)
3663 return -EINVAL;
3664 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3665 return 0;
3666
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003667 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003668 if (val == 0 && sp->do_auto_asconf) {
3669 list_del(&sp->auto_asconf_list);
3670 sp->do_auto_asconf = 0;
3671 } else if (val && !sp->do_auto_asconf) {
3672 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00003673 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09003674 sp->do_auto_asconf = 1;
3675 }
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003676 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003677 return 0;
3678}
3679
Neil Horman5aa93bc2012-07-21 07:56:07 +00003680/*
3681 * SCTP_PEER_ADDR_THLDS
3682 *
3683 * This option allows us to alter the partially failed threshold for one or all
3684 * transports in an association. See Section 6.1 of:
3685 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3686 */
3687static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3688 char __user *optval,
3689 unsigned int optlen)
3690{
3691 struct sctp_paddrthlds val;
3692 struct sctp_transport *trans;
3693 struct sctp_association *asoc;
3694
3695 if (optlen < sizeof(struct sctp_paddrthlds))
3696 return -EINVAL;
3697 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3698 sizeof(struct sctp_paddrthlds)))
3699 return -EFAULT;
3700
3701
3702 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3703 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3704 if (!asoc)
3705 return -ENOENT;
3706 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3707 transports) {
3708 if (val.spt_pathmaxrxt)
3709 trans->pathmaxrxt = val.spt_pathmaxrxt;
3710 trans->pf_retrans = val.spt_pathpfthld;
3711 }
3712
3713 if (val.spt_pathmaxrxt)
3714 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3715 asoc->pf_retrans = val.spt_pathpfthld;
3716 } else {
3717 trans = sctp_addr_id2transport(sk, &val.spt_address,
3718 val.spt_assoc_id);
3719 if (!trans)
3720 return -ENOENT;
3721
3722 if (val.spt_pathmaxrxt)
3723 trans->pathmaxrxt = val.spt_pathmaxrxt;
3724 trans->pf_retrans = val.spt_pathpfthld;
3725 }
3726
3727 return 0;
3728}
3729
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02003730static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3731 char __user *optval,
3732 unsigned int optlen)
3733{
3734 int val;
3735
3736 if (optlen < sizeof(int))
3737 return -EINVAL;
3738 if (get_user(val, (int __user *) optval))
3739 return -EFAULT;
3740
3741 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3742
3743 return 0;
3744}
3745
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02003746static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3747 char __user *optval,
3748 unsigned int optlen)
3749{
3750 int val;
3751
3752 if (optlen < sizeof(int))
3753 return -EINVAL;
3754 if (get_user(val, (int __user *) optval))
3755 return -EFAULT;
3756
3757 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3758
3759 return 0;
3760}
3761
Xin Long28aa4c22016-07-09 19:47:40 +08003762static int sctp_setsockopt_pr_supported(struct sock *sk,
3763 char __user *optval,
3764 unsigned int optlen)
3765{
3766 struct sctp_assoc_value params;
3767 struct sctp_association *asoc;
3768 int retval = -EINVAL;
3769
3770 if (optlen != sizeof(params))
3771 goto out;
3772
3773 if (copy_from_user(&params, optval, optlen)) {
3774 retval = -EFAULT;
3775 goto out;
3776 }
3777
3778 asoc = sctp_id2assoc(sk, params.assoc_id);
3779 if (asoc) {
3780 asoc->prsctp_enable = !!params.assoc_value;
3781 } else if (!params.assoc_id) {
3782 struct sctp_sock *sp = sctp_sk(sk);
3783
3784 sp->ep->prsctp_enable = !!params.assoc_value;
3785 } else {
3786 goto out;
3787 }
3788
3789 retval = 0;
3790
3791out:
3792 return retval;
3793}
3794
Xin Longf959fb42016-07-09 19:47:41 +08003795static int sctp_setsockopt_default_prinfo(struct sock *sk,
3796 char __user *optval,
3797 unsigned int optlen)
3798{
3799 struct sctp_default_prinfo info;
3800 struct sctp_association *asoc;
3801 int retval = -EINVAL;
3802
3803 if (optlen != sizeof(info))
3804 goto out;
3805
3806 if (copy_from_user(&info, optval, sizeof(info))) {
3807 retval = -EFAULT;
3808 goto out;
3809 }
3810
3811 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
3812 goto out;
3813
3814 if (info.pr_policy == SCTP_PR_SCTP_NONE)
3815 info.pr_value = 0;
3816
3817 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
3818 if (asoc) {
3819 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
3820 asoc->default_timetolive = info.pr_value;
3821 } else if (!info.pr_assoc_id) {
3822 struct sctp_sock *sp = sctp_sk(sk);
3823
3824 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
3825 sp->default_timetolive = info.pr_value;
3826 } else {
3827 goto out;
3828 }
3829
3830 retval = 0;
3831
3832out:
3833 return retval;
3834}
3835
Xin Longc0d8bab2017-03-10 12:11:12 +08003836static int sctp_setsockopt_reconfig_supported(struct sock *sk,
3837 char __user *optval,
3838 unsigned int optlen)
3839{
3840 struct sctp_assoc_value params;
3841 struct sctp_association *asoc;
3842 int retval = -EINVAL;
3843
3844 if (optlen != sizeof(params))
3845 goto out;
3846
3847 if (copy_from_user(&params, optval, optlen)) {
3848 retval = -EFAULT;
3849 goto out;
3850 }
3851
3852 asoc = sctp_id2assoc(sk, params.assoc_id);
3853 if (asoc) {
3854 asoc->reconf_enable = !!params.assoc_value;
3855 } else if (!params.assoc_id) {
3856 struct sctp_sock *sp = sctp_sk(sk);
3857
3858 sp->ep->reconf_enable = !!params.assoc_value;
3859 } else {
3860 goto out;
3861 }
3862
3863 retval = 0;
3864
3865out:
3866 return retval;
3867}
3868
Xin Long9fb657a2017-01-18 00:44:46 +08003869static int sctp_setsockopt_enable_strreset(struct sock *sk,
3870 char __user *optval,
3871 unsigned int optlen)
3872{
3873 struct sctp_assoc_value params;
3874 struct sctp_association *asoc;
3875 int retval = -EINVAL;
3876
3877 if (optlen != sizeof(params))
3878 goto out;
3879
3880 if (copy_from_user(&params, optval, optlen)) {
3881 retval = -EFAULT;
3882 goto out;
3883 }
3884
3885 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
3886 goto out;
3887
3888 asoc = sctp_id2assoc(sk, params.assoc_id);
3889 if (asoc) {
3890 asoc->strreset_enable = params.assoc_value;
3891 } else if (!params.assoc_id) {
3892 struct sctp_sock *sp = sctp_sk(sk);
3893
3894 sp->ep->strreset_enable = params.assoc_value;
3895 } else {
3896 goto out;
3897 }
3898
3899 retval = 0;
3900
3901out:
3902 return retval;
3903}
3904
Xin Long7f9d68a2017-01-18 00:44:47 +08003905static int sctp_setsockopt_reset_streams(struct sock *sk,
3906 char __user *optval,
3907 unsigned int optlen)
3908{
3909 struct sctp_reset_streams *params;
3910 struct sctp_association *asoc;
3911 int retval = -EINVAL;
3912
Xin Long2342b8d2017-12-10 15:40:51 +08003913 if (optlen < sizeof(*params))
Xin Long7f9d68a2017-01-18 00:44:47 +08003914 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003915 /* srs_number_streams is u16, so optlen can't be bigger than this. */
3916 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3917 sizeof(__u16) * sizeof(*params));
Xin Long7f9d68a2017-01-18 00:44:47 +08003918
3919 params = memdup_user(optval, optlen);
3920 if (IS_ERR(params))
3921 return PTR_ERR(params);
3922
Xin Long2342b8d2017-12-10 15:40:51 +08003923 if (params->srs_number_streams * sizeof(__u16) >
3924 optlen - sizeof(*params))
3925 goto out;
3926
Xin Long7f9d68a2017-01-18 00:44:47 +08003927 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
3928 if (!asoc)
3929 goto out;
3930
3931 retval = sctp_send_reset_streams(asoc, params);
3932
3933out:
3934 kfree(params);
3935 return retval;
3936}
3937
Xin Longa92ce1a2017-02-09 01:18:18 +08003938static int sctp_setsockopt_reset_assoc(struct sock *sk,
3939 char __user *optval,
3940 unsigned int optlen)
3941{
3942 struct sctp_association *asoc;
3943 sctp_assoc_t associd;
3944 int retval = -EINVAL;
3945
3946 if (optlen != sizeof(associd))
3947 goto out;
3948
3949 if (copy_from_user(&associd, optval, optlen)) {
3950 retval = -EFAULT;
3951 goto out;
3952 }
3953
3954 asoc = sctp_id2assoc(sk, associd);
3955 if (!asoc)
3956 goto out;
3957
3958 retval = sctp_send_reset_assoc(asoc);
3959
3960out:
3961 return retval;
3962}
3963
Xin Long242bd2d2017-02-09 01:18:20 +08003964static int sctp_setsockopt_add_streams(struct sock *sk,
3965 char __user *optval,
3966 unsigned int optlen)
3967{
3968 struct sctp_association *asoc;
3969 struct sctp_add_streams params;
3970 int retval = -EINVAL;
3971
3972 if (optlen != sizeof(params))
3973 goto out;
3974
3975 if (copy_from_user(&params, optval, optlen)) {
3976 retval = -EFAULT;
3977 goto out;
3978 }
3979
3980 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
3981 if (!asoc)
3982 goto out;
3983
3984 retval = sctp_send_add_streams(asoc, &params);
3985
3986out:
3987 return retval;
3988}
3989
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03003990static int sctp_setsockopt_scheduler(struct sock *sk,
3991 char __user *optval,
3992 unsigned int optlen)
3993{
3994 struct sctp_association *asoc;
3995 struct sctp_assoc_value params;
3996 int retval = -EINVAL;
3997
3998 if (optlen < sizeof(params))
3999 goto out;
4000
4001 optlen = sizeof(params);
4002 if (copy_from_user(&params, optval, optlen)) {
4003 retval = -EFAULT;
4004 goto out;
4005 }
4006
4007 if (params.assoc_value > SCTP_SS_MAX)
4008 goto out;
4009
4010 asoc = sctp_id2assoc(sk, params.assoc_id);
4011 if (!asoc)
4012 goto out;
4013
4014 retval = sctp_sched_set_sched(asoc, params.assoc_value);
4015
4016out:
4017 return retval;
4018}
4019
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004020static int sctp_setsockopt_scheduler_value(struct sock *sk,
4021 char __user *optval,
4022 unsigned int optlen)
4023{
4024 struct sctp_association *asoc;
4025 struct sctp_stream_value params;
4026 int retval = -EINVAL;
4027
4028 if (optlen < sizeof(params))
4029 goto out;
4030
4031 optlen = sizeof(params);
4032 if (copy_from_user(&params, optval, optlen)) {
4033 retval = -EFAULT;
4034 goto out;
4035 }
4036
4037 asoc = sctp_id2assoc(sk, params.assoc_id);
4038 if (!asoc)
4039 goto out;
4040
4041 retval = sctp_sched_set_value(asoc, params.stream_id,
4042 params.stream_value, GFP_KERNEL);
4043
4044out:
4045 return retval;
4046}
4047
Xin Long772a5862017-12-08 21:03:58 +08004048static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4049 char __user *optval,
4050 unsigned int optlen)
4051{
4052 struct sctp_sock *sp = sctp_sk(sk);
4053 struct net *net = sock_net(sk);
4054 struct sctp_assoc_value params;
4055 int retval = -EINVAL;
4056
4057 if (optlen < sizeof(params))
4058 goto out;
4059
4060 optlen = sizeof(params);
4061 if (copy_from_user(&params, optval, optlen)) {
4062 retval = -EFAULT;
4063 goto out;
4064 }
4065
4066 if (params.assoc_id)
4067 goto out;
4068
4069 if (!net->sctp.intl_enable || !sp->frag_interleave) {
4070 retval = -EPERM;
4071 goto out;
4072 }
4073
4074 sp->strm_interleave = !!params.assoc_value;
4075
4076 retval = 0;
4077
4078out:
4079 return retval;
4080}
4081
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082/* API 6.2 setsockopt(), getsockopt()
4083 *
4084 * Applications use setsockopt() and getsockopt() to set or retrieve
4085 * socket options. Socket options are used to change the default
4086 * behavior of sockets calls. They are described in Section 7.
4087 *
4088 * The syntax is:
4089 *
4090 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4091 * int __user *optlen);
4092 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4093 * int optlen);
4094 *
4095 * sd - the socket descript.
4096 * level - set to IPPROTO_SCTP for all SCTP options.
4097 * optname - the option name.
4098 * optval - the buffer to store the value of the option.
4099 * optlen - the size of the buffer.
4100 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004101static int sctp_setsockopt(struct sock *sk, int level, int optname,
4102 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103{
4104 int retval = 0;
4105
Daniel Borkmannbb333812013-06-28 19:49:40 +02004106 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
4108 /* I can hardly begin to describe how wrong this is. This is
4109 * so broken as to be worse than useless. The API draft
4110 * REALLY is NOT helpful here... I am not convinced that the
4111 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4112 * are at all well-founded.
4113 */
4114 if (level != SOL_SCTP) {
4115 struct sctp_af *af = sctp_sk(sk)->pf->af;
4116 retval = af->setsockopt(sk, level, optname, optval, optlen);
4117 goto out_nounlock;
4118 }
4119
wangweidong048ed4b2014-01-21 15:44:11 +08004120 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
4122 switch (optname) {
4123 case SCTP_SOCKOPT_BINDX_ADD:
4124 /* 'optlen' is the size of the addresses buffer. */
4125 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4126 optlen, SCTP_BINDX_ADD_ADDR);
4127 break;
4128
4129 case SCTP_SOCKOPT_BINDX_REM:
4130 /* 'optlen' is the size of the addresses buffer. */
4131 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4132 optlen, SCTP_BINDX_REM_ADDR);
4133 break;
4134
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004135 case SCTP_SOCKOPT_CONNECTX_OLD:
4136 /* 'optlen' is the size of the addresses buffer. */
4137 retval = sctp_setsockopt_connectx_old(sk,
4138 (struct sockaddr __user *)optval,
4139 optlen);
4140 break;
4141
Frank Filz3f7a87d2005-06-20 13:14:57 -07004142 case SCTP_SOCKOPT_CONNECTX:
4143 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004144 retval = sctp_setsockopt_connectx(sk,
4145 (struct sockaddr __user *)optval,
4146 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004147 break;
4148
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 case SCTP_DISABLE_FRAGMENTS:
4150 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4151 break;
4152
4153 case SCTP_EVENTS:
4154 retval = sctp_setsockopt_events(sk, optval, optlen);
4155 break;
4156
4157 case SCTP_AUTOCLOSE:
4158 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4159 break;
4160
4161 case SCTP_PEER_ADDR_PARAMS:
4162 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4163 break;
4164
Shan Wei4580ccc2011-01-18 22:39:00 +00004165 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07004166 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08004167 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004168 case SCTP_PARTIAL_DELIVERY_POINT:
4169 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4170 break;
Frank Filz77086102005-12-22 11:37:30 -08004171
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 case SCTP_INITMSG:
4173 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4174 break;
4175 case SCTP_DEFAULT_SEND_PARAM:
4176 retval = sctp_setsockopt_default_send_param(sk, optval,
4177 optlen);
4178 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02004179 case SCTP_DEFAULT_SNDINFO:
4180 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 case SCTP_PRIMARY_ADDR:
4183 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4184 break;
4185 case SCTP_SET_PEER_PRIMARY_ADDR:
4186 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4187 break;
4188 case SCTP_NODELAY:
4189 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4190 break;
4191 case SCTP_RTOINFO:
4192 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4193 break;
4194 case SCTP_ASSOCINFO:
4195 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4196 break;
4197 case SCTP_I_WANT_MAPPED_V4_ADDR:
4198 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4199 break;
4200 case SCTP_MAXSEG:
4201 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4202 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004203 case SCTP_ADAPTATION_LAYER:
4204 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004206 case SCTP_CONTEXT:
4207 retval = sctp_setsockopt_context(sk, optval, optlen);
4208 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004209 case SCTP_FRAGMENT_INTERLEAVE:
4210 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4211 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07004212 case SCTP_MAX_BURST:
4213 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4214 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004215 case SCTP_AUTH_CHUNK:
4216 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4217 break;
4218 case SCTP_HMAC_IDENT:
4219 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4220 break;
4221 case SCTP_AUTH_KEY:
4222 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4223 break;
4224 case SCTP_AUTH_ACTIVE_KEY:
4225 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4226 break;
4227 case SCTP_AUTH_DELETE_KEY:
4228 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4229 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09004230 case SCTP_AUTO_ASCONF:
4231 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4232 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00004233 case SCTP_PEER_ADDR_THLDS:
4234 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4235 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004236 case SCTP_RECVRCVINFO:
4237 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4238 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004239 case SCTP_RECVNXTINFO:
4240 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4241 break;
Xin Long28aa4c22016-07-09 19:47:40 +08004242 case SCTP_PR_SUPPORTED:
4243 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4244 break;
Xin Longf959fb42016-07-09 19:47:41 +08004245 case SCTP_DEFAULT_PRINFO:
4246 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4247 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08004248 case SCTP_RECONFIG_SUPPORTED:
4249 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4250 break;
Xin Long9fb657a2017-01-18 00:44:46 +08004251 case SCTP_ENABLE_STREAM_RESET:
4252 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4253 break;
Xin Long7f9d68a2017-01-18 00:44:47 +08004254 case SCTP_RESET_STREAMS:
4255 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4256 break;
Xin Longa92ce1a2017-02-09 01:18:18 +08004257 case SCTP_RESET_ASSOC:
4258 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4259 break;
Xin Long242bd2d2017-02-09 01:18:20 +08004260 case SCTP_ADD_STREAMS:
4261 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4262 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004263 case SCTP_STREAM_SCHEDULER:
4264 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4265 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004266 case SCTP_STREAM_SCHEDULER_VALUE:
4267 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4268 break;
Xin Long772a5862017-12-08 21:03:58 +08004269 case SCTP_INTERLEAVING_SUPPORTED:
4270 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4271 optlen);
4272 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 default:
4274 retval = -ENOPROTOOPT;
4275 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
wangweidong048ed4b2014-01-21 15:44:11 +08004278 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279
4280out_nounlock:
4281 return retval;
4282}
4283
4284/* API 3.1.6 connect() - UDP Style Syntax
4285 *
4286 * An application may use the connect() call in the UDP model to initiate an
4287 * association without sending data.
4288 *
4289 * The syntax is:
4290 *
4291 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4292 *
4293 * sd: the socket descriptor to have a new association added to.
4294 *
4295 * nam: the address structure (either struct sockaddr_in or struct
4296 * sockaddr_in6 defined in RFC2553 [7]).
4297 *
4298 * len: the size of the address.
4299 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004300static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4301 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07004304 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
wangweidong048ed4b2014-01-21 15:44:11 +08004306 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
Daniel Borkmannbb333812013-06-28 19:49:40 +02004308 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4309 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
Frank Filz3f7a87d2005-06-20 13:14:57 -07004311 /* Validate addr_len before calling common connect/connectx routine. */
4312 af = sctp_get_af_specific(addr->sa_family);
4313 if (!af || addr_len < af->sockaddr_len) {
4314 err = -EINVAL;
4315 } else {
4316 /* Pass correct addr len to common routine (so it knows there
4317 * is only one address being passed.
4318 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004319 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 }
4321
wangweidong048ed4b2014-01-21 15:44:11 +08004322 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return err;
4324}
4325
4326/* FIXME: Write comments. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004327static int sctp_disconnect(struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328{
4329 return -EOPNOTSUPP; /* STUB */
4330}
4331
4332/* 4.1.4 accept() - TCP Style Syntax
4333 *
4334 * Applications use accept() call to remove an established SCTP
4335 * association from the accept queue of the endpoint. A new socket
4336 * descriptor will be returned from accept() to represent the newly
4337 * formed association.
4338 */
David Howellscdfbabf2017-03-09 08:09:05 +00004339static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340{
4341 struct sctp_sock *sp;
4342 struct sctp_endpoint *ep;
4343 struct sock *newsk = NULL;
4344 struct sctp_association *asoc;
4345 long timeo;
4346 int error = 0;
4347
wangweidong048ed4b2014-01-21 15:44:11 +08004348 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
4350 sp = sctp_sk(sk);
4351 ep = sp->ep;
4352
4353 if (!sctp_style(sk, TCP)) {
4354 error = -EOPNOTSUPP;
4355 goto out;
4356 }
4357
4358 if (!sctp_sstate(sk, LISTENING)) {
4359 error = -EINVAL;
4360 goto out;
4361 }
4362
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07004363 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
4365 error = sctp_wait_for_accept(sk, timeo);
4366 if (error)
4367 goto out;
4368
4369 /* We treat the list of associations on the endpoint as the accept
4370 * queue and pick the first association on the list.
4371 */
4372 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4373
David Howellscdfbabf2017-03-09 08:09:05 +00004374 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 if (!newsk) {
4376 error = -ENOMEM;
4377 goto out;
4378 }
4379
4380 /* Populate the fields of the newsk from the oldsk and migrate the
4381 * asoc to the newsk.
4382 */
4383 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4384
4385out:
wangweidong048ed4b2014-01-21 15:44:11 +08004386 release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004387 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 return newsk;
4389}
4390
4391/* The SCTP ioctl handler. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004392static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004394 int rc = -ENOTCONN;
4395
wangweidong048ed4b2014-01-21 15:44:11 +08004396 lock_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004397
4398 /*
4399 * SEQPACKET-style sockets in LISTENING state are valid, for
4400 * SCTP, so only discard TCP-style sockets in LISTENING state.
4401 */
4402 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4403 goto out;
4404
4405 switch (cmd) {
4406 case SIOCINQ: {
4407 struct sk_buff *skb;
4408 unsigned int amount = 0;
4409
4410 skb = skb_peek(&sk->sk_receive_queue);
4411 if (skb != NULL) {
4412 /*
4413 * We will only return the amount of this packet since
4414 * that is all that will be read.
4415 */
4416 amount = skb->len;
4417 }
4418 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004419 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07004420 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004421 default:
4422 rc = -ENOIOCTLCMD;
4423 break;
4424 }
4425out:
wangweidong048ed4b2014-01-21 15:44:11 +08004426 release_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004427 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428}
4429
4430/* This is the function which gets called during socket creation to
4431 * initialized the SCTP-specific portion of the sock.
4432 * The sock structure should already be zero-filled memory.
4433 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004434static int sctp_init_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004436 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 struct sctp_sock *sp;
4438
Daniel Borkmannbb333812013-06-28 19:49:40 +02004439 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
4441 sp = sctp_sk(sk);
4442
4443 /* Initialize the SCTP per socket area. */
4444 switch (sk->sk_type) {
4445 case SOCK_SEQPACKET:
4446 sp->type = SCTP_SOCKET_UDP;
4447 break;
4448 case SOCK_STREAM:
4449 sp->type = SCTP_SOCKET_TCP;
4450 break;
4451 default:
4452 return -ESOCKTNOSUPPORT;
4453 }
4454
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004455 sk->sk_gso_type = SKB_GSO_SCTP;
4456
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 /* Initialize default send parameters. These parameters can be
4458 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4459 */
4460 sp->default_stream = 0;
4461 sp->default_ppid = 0;
4462 sp->default_flags = 0;
4463 sp->default_context = 0;
4464 sp->default_timetolive = 0;
4465
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004466 sp->default_rcv_context = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004467 sp->max_burst = net->sctp.max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004468
Neil Horman3c681982012-10-24 09:20:03 +00004469 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 /* Initialize default setup parameters. These parameters
4472 * can be modified with the SCTP_INITMSG socket option or
4473 * overridden by the SCTP_INIT CMSG.
4474 */
4475 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4476 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004477 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4478 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
4480 /* Initialize default RTO related parameters. These parameters can
4481 * be modified for with the SCTP_RTOINFO socket option.
4482 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004483 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4484 sp->rtoinfo.srto_max = net->sctp.rto_max;
4485 sp->rtoinfo.srto_min = net->sctp.rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
4487 /* Initialize default association related parameters. These parameters
4488 * can be modified with the SCTP_ASSOCINFO socket option.
4489 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004490 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 sp->assocparams.sasoc_number_peer_destinations = 0;
4492 sp->assocparams.sasoc_peer_rwnd = 0;
4493 sp->assocparams.sasoc_local_rwnd = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004494 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
4496 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004497 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 */
4499 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4500
4501 /* Default Peer Address Parameters. These defaults can
4502 * be modified via SCTP_PEER_ADDR_PARAMS
4503 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004504 sp->hbinterval = net->sctp.hb_interval;
4505 sp->pathmaxrxt = net->sctp.max_retrans_path;
wangweidong4e2d52b2013-12-23 12:16:54 +08004506 sp->pathmtu = 0; /* allow default discovery */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004507 sp->sackdelay = net->sctp.sack_timeout;
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07004508 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004509 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004510 SPP_PMTUD_ENABLE |
4511 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
4513 /* If enabled no SCTP message fragmentation will be performed.
4514 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4515 */
4516 sp->disable_fragments = 0;
4517
Sridhar Samudrala208edef2006-09-29 17:08:01 -07004518 /* Enable Nagle algorithm by default. */
4519 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004521 sp->recvrcvinfo = 0;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004522 sp->recvnxtinfo = 0;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004523
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 /* Enable by default. */
4525 sp->v4mapped = 1;
4526
4527 /* Auto-close idle associations after the configured
4528 * number of seconds. A value of 0 disables this
4529 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4530 * for UDP-style sockets only.
4531 */
4532 sp->autoclose = 0;
4533
4534 /* User specified fragmentation limit. */
4535 sp->user_frag = 0;
4536
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004537 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
4539 sp->pf = sctp_get_pf_specific(sk->sk_family);
4540
4541 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004542 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004544 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545
4546 /* Create a per socket endpoint structure. Even if we
4547 * change the data structure relationships, this may still
4548 * be useful for storing pre-connect address information.
4549 */
Daniel Borkmannc164b832013-06-14 18:24:06 +02004550 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4551 if (!sp->ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 return -ENOMEM;
4553
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 sp->hmac = NULL;
4555
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004556 sk->sk_destruct = sctp_destruct_sock;
4557
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08004559
4560 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004561 sk_sockets_allocated_inc(sk);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004562 sock_prot_inuse_add(net, sk->sk_prot, 1);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004563
4564 /* Nothing can fail after this block, otherwise
4565 * sctp_destroy_sock() will be called without addr_wq_lock held
4566 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004567 if (net->sctp.default_auto_asconf) {
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004568 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda9f7d6532011-04-26 19:32:51 +09004569 list_add_tail(&sp->auto_asconf_list,
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004570 &net->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09004571 sp->do_auto_asconf = 1;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004572 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4573 } else {
Michio Honda9f7d6532011-04-26 19:32:51 +09004574 sp->do_auto_asconf = 0;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004575 }
4576
David S. Miller6f756a82008-11-23 17:34:03 -08004577 local_bh_enable();
4578
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 return 0;
4580}
4581
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004582/* Cleanup any SCTP per socket resources. Must be called with
4583 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4584 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004585static void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586{
Michio Honda9f7d6532011-04-26 19:32:51 +09004587 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
Daniel Borkmannbb333812013-06-28 19:49:40 +02004589 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
4591 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09004592 sp = sctp_sk(sk);
Daniel Borkmann1abd1652013-06-06 15:53:47 +02004593 /* This could happen during socket init, thus we bail out
4594 * early, since the rest of the below is not setup either.
4595 */
4596 if (sp->ep == NULL)
4597 return;
4598
Michio Honda9f7d6532011-04-26 19:32:51 +09004599 if (sp->do_auto_asconf) {
4600 sp->do_auto_asconf = 0;
4601 list_del(&sp->auto_asconf_list);
4602 }
4603 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004604 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004605 sk_sockets_allocated_dec(sk);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08004606 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004607 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608}
4609
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004610/* Triggered when there are no references on the socket anymore */
4611static void sctp_destruct_sock(struct sock *sk)
4612{
4613 struct sctp_sock *sp = sctp_sk(sk);
4614
4615 /* Free up the HMAC transform. */
Herbert Xu5821c762016-01-24 21:20:12 +08004616 crypto_free_shash(sp->hmac);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004617
4618 inet_sock_destruct(sk);
4619}
4620
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621/* API 4.1.7 shutdown() - TCP Style Syntax
4622 * int shutdown(int socket, int how);
4623 *
4624 * sd - the socket descriptor of the association to be closed.
4625 * how - Specifies the type of shutdown. The values are
4626 * as follows:
4627 * SHUT_RD
4628 * Disables further receive operations. No SCTP
4629 * protocol action is taken.
4630 * SHUT_WR
4631 * Disables further send operations, and initiates
4632 * the SCTP shutdown sequence.
4633 * SHUT_RDWR
4634 * Disables further send and receive operations
4635 * and initiates the SCTP shutdown sequence.
4636 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004637static void sctp_shutdown(struct sock *sk, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004639 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 struct sctp_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
4642 if (!sctp_style(sk, TCP))
4643 return;
4644
Xin Long5bf35dd2016-11-13 21:44:37 +08004645 ep = sctp_sk(sk)->ep;
4646 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4647 struct sctp_association *asoc;
4648
Yafang Shaocbabf462017-12-20 11:12:54 +08004649 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Xin Long5bf35dd2016-11-13 21:44:37 +08004650 asoc = list_entry(ep->asocs.next,
4651 struct sctp_association, asocs);
4652 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 }
4654}
4655
Xin Long52c52a62016-04-14 15:35:30 +08004656int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4657 struct sctp_info *info)
4658{
4659 struct sctp_transport *prim;
4660 struct list_head *pos;
4661 int mask;
4662
4663 memset(info, 0, sizeof(*info));
4664 if (!asoc) {
4665 struct sctp_sock *sp = sctp_sk(sk);
4666
4667 info->sctpi_s_autoclose = sp->autoclose;
4668 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4669 info->sctpi_s_pd_point = sp->pd_point;
4670 info->sctpi_s_nodelay = sp->nodelay;
4671 info->sctpi_s_disable_fragments = sp->disable_fragments;
4672 info->sctpi_s_v4mapped = sp->v4mapped;
4673 info->sctpi_s_frag_interleave = sp->frag_interleave;
Xin Long40eb90e92016-05-29 17:42:13 +08004674 info->sctpi_s_type = sp->type;
Xin Long52c52a62016-04-14 15:35:30 +08004675
4676 return 0;
4677 }
4678
4679 info->sctpi_tag = asoc->c.my_vtag;
4680 info->sctpi_state = asoc->state;
4681 info->sctpi_rwnd = asoc->a_rwnd;
4682 info->sctpi_unackdata = asoc->unack_data;
4683 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004684 info->sctpi_instrms = asoc->stream.incnt;
4685 info->sctpi_outstrms = asoc->stream.outcnt;
Xin Long52c52a62016-04-14 15:35:30 +08004686 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4687 info->sctpi_inqueue++;
4688 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4689 info->sctpi_outqueue++;
4690 info->sctpi_overall_error = asoc->overall_error_count;
4691 info->sctpi_max_burst = asoc->max_burst;
4692 info->sctpi_maxseg = asoc->frag_point;
4693 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4694 info->sctpi_peer_tag = asoc->c.peer_vtag;
4695
4696 mask = asoc->peer.ecn_capable << 1;
4697 mask = (mask | asoc->peer.ipv4_address) << 1;
4698 mask = (mask | asoc->peer.ipv6_address) << 1;
4699 mask = (mask | asoc->peer.hostname_address) << 1;
4700 mask = (mask | asoc->peer.asconf_capable) << 1;
4701 mask = (mask | asoc->peer.prsctp_capable) << 1;
4702 mask = (mask | asoc->peer.auth_capable);
4703 info->sctpi_peer_capable = mask;
4704 mask = asoc->peer.sack_needed << 1;
4705 mask = (mask | asoc->peer.sack_generation) << 1;
4706 mask = (mask | asoc->peer.zero_window_announced);
4707 info->sctpi_peer_sack = mask;
4708
4709 info->sctpi_isacks = asoc->stats.isacks;
4710 info->sctpi_osacks = asoc->stats.osacks;
4711 info->sctpi_opackets = asoc->stats.opackets;
4712 info->sctpi_ipackets = asoc->stats.ipackets;
4713 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4714 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4715 info->sctpi_idupchunks = asoc->stats.idupchunks;
4716 info->sctpi_gapcnt = asoc->stats.gapcnt;
4717 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4718 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4719 info->sctpi_oodchunks = asoc->stats.oodchunks;
4720 info->sctpi_iodchunks = asoc->stats.iodchunks;
4721 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4722 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4723
4724 prim = asoc->peer.primary_path;
Stefano Brivioee6c88b2017-08-23 13:27:13 +02004725 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
Xin Long52c52a62016-04-14 15:35:30 +08004726 info->sctpi_p_state = prim->state;
4727 info->sctpi_p_cwnd = prim->cwnd;
4728 info->sctpi_p_srtt = prim->srtt;
4729 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4730 info->sctpi_p_hbinterval = prim->hbinterval;
4731 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4732 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4733 info->sctpi_p_ssthresh = prim->ssthresh;
4734 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4735 info->sctpi_p_flight_size = prim->flight_size;
4736 info->sctpi_p_error = prim->error_count;
4737
4738 return 0;
4739}
4740EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4741
Xin Long626d16f2016-04-14 15:35:31 +08004742/* use callback to avoid exporting the core structure */
Tom Herbert97a6ec42017-12-04 10:31:41 -08004743void sctp_transport_walk_start(struct rhashtable_iter *iter)
Xin Long626d16f2016-04-14 15:35:31 +08004744{
Xin Long7fda702f2016-11-15 23:23:11 +08004745 rhltable_walk_enter(&sctp_transport_hashtable, iter);
Xin Long626d16f2016-04-14 15:35:31 +08004746
Tom Herbert97a6ec42017-12-04 10:31:41 -08004747 rhashtable_walk_start(iter);
Xin Long626d16f2016-04-14 15:35:31 +08004748}
4749
4750void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4751{
4752 rhashtable_walk_stop(iter);
4753 rhashtable_walk_exit(iter);
4754}
4755
4756struct sctp_transport *sctp_transport_get_next(struct net *net,
4757 struct rhashtable_iter *iter)
4758{
4759 struct sctp_transport *t;
4760
4761 t = rhashtable_walk_next(iter);
4762 for (; t; t = rhashtable_walk_next(iter)) {
4763 if (IS_ERR(t)) {
4764 if (PTR_ERR(t) == -EAGAIN)
4765 continue;
4766 break;
4767 }
4768
4769 if (net_eq(sock_net(t->asoc->base.sk), net) &&
4770 t->asoc->peer.primary_path == t)
4771 break;
4772 }
4773
4774 return t;
4775}
4776
4777struct sctp_transport *sctp_transport_get_idx(struct net *net,
4778 struct rhashtable_iter *iter,
4779 int pos)
4780{
4781 void *obj = SEQ_START_TOKEN;
4782
4783 while (pos && (obj = sctp_transport_get_next(net, iter)) &&
4784 !IS_ERR(obj))
4785 pos--;
4786
4787 return obj;
4788}
4789
4790int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
4791 void *p) {
4792 int err = 0;
4793 int hash = 0;
4794 struct sctp_ep_common *epb;
4795 struct sctp_hashbucket *head;
4796
4797 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
4798 hash++, head++) {
Xin Long581409d2017-06-10 14:48:14 +08004799 read_lock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004800 sctp_for_each_hentry(epb, &head->chain) {
4801 err = cb(sctp_ep(epb), p);
4802 if (err)
4803 break;
4804 }
Xin Long581409d2017-06-10 14:48:14 +08004805 read_unlock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004806 }
4807
4808 return err;
4809}
4810EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
4811
4812int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
4813 struct net *net,
4814 const union sctp_addr *laddr,
4815 const union sctp_addr *paddr, void *p)
4816{
4817 struct sctp_transport *transport;
Xin Long08abb792016-12-15 23:05:52 +08004818 int err;
Xin Long626d16f2016-04-14 15:35:31 +08004819
4820 rcu_read_lock();
4821 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
Xin Long626d16f2016-04-14 15:35:31 +08004822 rcu_read_unlock();
Xin Long08abb792016-12-15 23:05:52 +08004823 if (!transport)
4824 return -ENOENT;
4825
Xin Long1cceda782016-09-29 02:55:44 +08004826 err = cb(transport, p);
Xin Longcd26da42016-10-31 20:32:31 +08004827 sctp_transport_put(transport);
Xin Long1cceda782016-09-29 02:55:44 +08004828
Xin Long626d16f2016-04-14 15:35:31 +08004829 return err;
4830}
4831EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
4832
4833int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
Xin Longd25adbe2017-09-15 11:02:21 +08004834 int (*cb_done)(struct sctp_transport *, void *),
4835 struct net *net, int *pos, void *p) {
Xin Long626d16f2016-04-14 15:35:31 +08004836 struct rhashtable_iter hti;
Xin Longd25adbe2017-09-15 11:02:21 +08004837 struct sctp_transport *tsp;
4838 int ret;
Xin Long626d16f2016-04-14 15:35:31 +08004839
Xin Longd25adbe2017-09-15 11:02:21 +08004840again:
Xin Longf53d77e2018-01-23 18:22:25 +08004841 ret = 0;
Tom Herbert97a6ec42017-12-04 10:31:41 -08004842 sctp_transport_walk_start(&hti);
Xin Long626d16f2016-04-14 15:35:31 +08004843
Xin Longd25adbe2017-09-15 11:02:21 +08004844 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
4845 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
4846 if (!sctp_transport_hold(tsp))
Xin Long626d16f2016-04-14 15:35:31 +08004847 continue;
Xin Longd25adbe2017-09-15 11:02:21 +08004848 ret = cb(tsp, p);
4849 if (ret)
Xin Long626d16f2016-04-14 15:35:31 +08004850 break;
Xin Longd25adbe2017-09-15 11:02:21 +08004851 (*pos)++;
4852 sctp_transport_put(tsp);
Xin Long626d16f2016-04-14 15:35:31 +08004853 }
Xin Long626d16f2016-04-14 15:35:31 +08004854 sctp_transport_walk_stop(&hti);
Xin Long53fa1032016-04-14 15:35:35 +08004855
Xin Longd25adbe2017-09-15 11:02:21 +08004856 if (ret) {
4857 if (cb_done && !cb_done(tsp, p)) {
4858 (*pos)++;
4859 sctp_transport_put(tsp);
4860 goto again;
4861 }
4862 sctp_transport_put(tsp);
4863 }
4864
4865 return ret;
Xin Long626d16f2016-04-14 15:35:31 +08004866}
4867EXPORT_SYMBOL_GPL(sctp_for_each_transport);
4868
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869/* 7.2.1 Association Status (SCTP_STATUS)
4870
4871 * Applications can retrieve current status information about an
4872 * association, including association state, peer receiver window size,
4873 * number of unacked data chunks, and number of data chunks pending
4874 * receipt. This information is read-only.
4875 */
4876static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4877 char __user *optval,
4878 int __user *optlen)
4879{
4880 struct sctp_status status;
4881 struct sctp_association *asoc = NULL;
4882 struct sctp_transport *transport;
4883 sctp_assoc_t associd;
4884 int retval = 0;
4885
Neil Horman408f22e2007-06-16 14:03:45 -04004886 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 retval = -EINVAL;
4888 goto out;
4889 }
4890
Neil Horman408f22e2007-06-16 14:03:45 -04004891 len = sizeof(status);
4892 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 retval = -EFAULT;
4894 goto out;
4895 }
4896
4897 associd = status.sstat_assoc_id;
4898 asoc = sctp_id2assoc(sk, associd);
4899 if (!asoc) {
4900 retval = -EINVAL;
4901 goto out;
4902 }
4903
4904 transport = asoc->peer.primary_path;
4905
4906 status.sstat_assoc_id = sctp_assoc2id(asoc);
Daniel Borkmann38ab1fa2014-08-28 15:28:26 +02004907 status.sstat_state = sctp_assoc_to_state(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 status.sstat_rwnd = asoc->peer.rwnd;
4909 status.sstat_unackdata = asoc->unack_data;
4910
4911 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004912 status.sstat_instrms = asoc->stream.incnt;
4913 status.sstat_outstrms = asoc->stream.outcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 status.sstat_fragmentation_point = asoc->frag_point;
4915 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08004916 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4917 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 /* Map ipv4 address into v4-mapped-on-v6 address. */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06004919 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004921 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4923 status.sstat_primary.spinfo_srtt = transport->srtt;
4924 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004925 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
Frank Filz3f7a87d2005-06-20 13:14:57 -07004927 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4928 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4929
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 if (put_user(len, optlen)) {
4931 retval = -EFAULT;
4932 goto out;
4933 }
4934
Daniel Borkmannbb333812013-06-28 19:49:40 +02004935 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
4936 __func__, len, status.sstat_state, status.sstat_rwnd,
4937 status.sstat_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
4939 if (copy_to_user(optval, &status, len)) {
4940 retval = -EFAULT;
4941 goto out;
4942 }
4943
4944out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004945 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946}
4947
4948
4949/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4950 *
4951 * Applications can retrieve information about a specific peer address
4952 * of an association, including its reachability state, congestion
4953 * window, and retransmission timer values. This information is
4954 * read-only.
4955 */
4956static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4957 char __user *optval,
4958 int __user *optlen)
4959{
4960 struct sctp_paddrinfo pinfo;
4961 struct sctp_transport *transport;
4962 int retval = 0;
4963
Neil Horman408f22e2007-06-16 14:03:45 -04004964 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 retval = -EINVAL;
4966 goto out;
4967 }
4968
Neil Horman408f22e2007-06-16 14:03:45 -04004969 len = sizeof(pinfo);
4970 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 retval = -EFAULT;
4972 goto out;
4973 }
4974
4975 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4976 pinfo.spinfo_assoc_id);
4977 if (!transport)
4978 return -EINVAL;
4979
4980 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004981 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 pinfo.spinfo_cwnd = transport->cwnd;
4983 pinfo.spinfo_srtt = transport->srtt;
4984 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004985 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
Frank Filz3f7a87d2005-06-20 13:14:57 -07004987 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4988 pinfo.spinfo_state = SCTP_ACTIVE;
4989
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 if (put_user(len, optlen)) {
4991 retval = -EFAULT;
4992 goto out;
4993 }
4994
4995 if (copy_to_user(optval, &pinfo, len)) {
4996 retval = -EFAULT;
4997 goto out;
4998 }
4999
5000out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00005001 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002}
5003
5004/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5005 *
5006 * This option is a on/off flag. If enabled no SCTP message
5007 * fragmentation will be performed. Instead if a message being sent
5008 * exceeds the current PMTU size, the message will NOT be sent and
5009 * instead a error will be indicated to the user.
5010 */
5011static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5012 char __user *optval, int __user *optlen)
5013{
5014 int val;
5015
5016 if (len < sizeof(int))
5017 return -EINVAL;
5018
5019 len = sizeof(int);
5020 val = (sctp_sk(sk)->disable_fragments == 1);
5021 if (put_user(len, optlen))
5022 return -EFAULT;
5023 if (copy_to_user(optval, &val, len))
5024 return -EFAULT;
5025 return 0;
5026}
5027
5028/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5029 *
5030 * This socket option is used to specify various notifications and
5031 * ancillary data the user wishes to receive.
5032 */
5033static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5034 int __user *optlen)
5035{
Jiri Slabya4b8e712016-10-21 14:13:24 +02005036 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00005038 if (len > sizeof(struct sctp_event_subscribe))
5039 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04005040 if (put_user(len, optlen))
5041 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
5043 return -EFAULT;
5044 return 0;
5045}
5046
5047/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5048 *
5049 * This socket option is applicable to the UDP-style socket only. When
5050 * set it will cause associations that are idle for more than the
5051 * specified number of seconds to automatically close. An association
5052 * being idle is defined an association that has NOT sent or received
5053 * user data. The special value of '0' indicates that no automatic
5054 * close of any associations should be performed. The option expects an
5055 * integer defining the number of seconds of idle time before an
5056 * association is closed.
5057 */
5058static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5059{
5060 /* Applicable to UDP-style socket only */
5061 if (sctp_style(sk, TCP))
5062 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04005063 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005065 len = sizeof(int);
5066 if (put_user(len, optlen))
5067 return -EFAULT;
David Windsorb2ce04c2017-06-10 22:50:43 -04005068 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 return -EFAULT;
5070 return 0;
5071}
5072
5073/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00005074int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075{
Benjamin Poirier0343c552012-03-08 05:55:58 +00005076 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005077 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 struct socket *sock;
5079 int err = 0;
5080
Xin Longdf80cd92017-10-17 23:26:10 +08005081 /* Do not peel off from one netns to another one. */
5082 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5083 return -EINVAL;
5084
Benjamin Poirier0343c552012-03-08 05:55:58 +00005085 if (!asoc)
5086 return -EINVAL;
5087
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 /* An association cannot be branched off from an already peeled-off
5089 * socket, nor is this supported for tcp style sockets.
5090 */
5091 if (!sctp_style(sk, UDP))
5092 return -EINVAL;
5093
5094 /* Create a new socket. */
5095 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5096 if (err < 0)
5097 return err;
5098
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005099 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005100
5101 /* Make peeled-off sockets more like 1-1 accepted sockets.
5102 * Set the daddr and initialize id to something more random
5103 */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005104 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005105
5106 /* Populate the fields of the newsk from the oldsk and migrate the
5107 * asoc to the newsk.
5108 */
5109 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005110
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 *sockp = sock;
5112
5113 return err;
5114}
Benjamin Poirier0343c552012-03-08 05:55:58 +00005115EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005117static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5118 struct file **newfile, unsigned flags)
5119{
5120 struct socket *newsock;
5121 int retval;
5122
5123 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5124 if (retval < 0)
5125 goto out;
5126
5127 /* Map the socket to an unused fd that can be returned to the user. */
5128 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5129 if (retval < 0) {
5130 sock_release(newsock);
5131 goto out;
5132 }
5133
5134 *newfile = sock_alloc_file(newsock, 0, NULL);
5135 if (IS_ERR(*newfile)) {
5136 put_unused_fd(retval);
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005137 retval = PTR_ERR(*newfile);
5138 *newfile = NULL;
5139 return retval;
5140 }
5141
5142 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5143 retval);
5144
5145 peeloff->sd = retval;
5146
5147 if (flags & SOCK_NONBLOCK)
5148 (*newfile)->f_flags |= O_NONBLOCK;
5149out:
5150 return retval;
5151}
5152
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5154{
5155 sctp_peeloff_arg_t peeloff;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005156 struct file *newfile = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158
Neil Horman408f22e2007-06-16 14:03:45 -04005159 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005161 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 if (copy_from_user(&peeloff, optval, len))
5163 return -EFAULT;
5164
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005165 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 if (retval < 0)
5167 goto out;
5168
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04005170 if (put_user(len, optlen)) {
5171 fput(newfile);
5172 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04005173 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04005174 }
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005175
5176 if (copy_to_user(optval, &peeloff, len)) {
5177 fput(newfile);
5178 put_unused_fd(retval);
5179 return -EFAULT;
5180 }
5181 fd_install(retval, newfile);
5182out:
5183 return retval;
5184}
5185
5186static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5187 char __user *optval, int __user *optlen)
5188{
5189 sctp_peeloff_flags_arg_t peeloff;
5190 struct file *newfile = NULL;
5191 int retval = 0;
5192
5193 if (len < sizeof(sctp_peeloff_flags_arg_t))
5194 return -EINVAL;
5195 len = sizeof(sctp_peeloff_flags_arg_t);
5196 if (copy_from_user(&peeloff, optval, len))
5197 return -EFAULT;
5198
5199 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5200 &newfile, peeloff.flags);
5201 if (retval < 0)
5202 goto out;
5203
5204 /* Return the fd mapped to the new socket. */
5205 if (put_user(len, optlen)) {
5206 fput(newfile);
5207 put_unused_fd(retval);
5208 return -EFAULT;
5209 }
5210
Al Viro56b31d12012-08-18 00:25:51 -04005211 if (copy_to_user(optval, &peeloff, len)) {
5212 fput(newfile);
5213 put_unused_fd(retval);
5214 return -EFAULT;
5215 }
5216 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217out:
5218 return retval;
5219}
5220
5221/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5222 *
5223 * Applications can enable or disable heartbeats for any peer address of
5224 * an association, modify an address's heartbeat interval, force a
5225 * heartbeat to be sent immediately, and adjust the address's maximum
5226 * number of retransmissions sent before an address is considered
5227 * unreachable. The following structure is used to access and modify an
5228 * address's parameters:
5229 *
5230 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005231 * sctp_assoc_t spp_assoc_id;
5232 * struct sockaddr_storage spp_address;
5233 * uint32_t spp_hbinterval;
5234 * uint16_t spp_pathmaxrxt;
5235 * uint32_t spp_pathmtu;
5236 * uint32_t spp_sackdelay;
5237 * uint32_t spp_flags;
5238 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08005240 * spp_assoc_id - (one-to-many style socket) This is filled in the
5241 * application, and identifies the association for
5242 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 * spp_address - This specifies which address is of interest.
5244 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005245 * in milliseconds. If a value of zero
5246 * is present in this field then no changes are to
5247 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 * spp_pathmaxrxt - This contains the maximum number of
5249 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08005250 * considered unreachable. If a value of zero
5251 * is present in this field then no changes are to
5252 * be made to this parameter.
5253 * spp_pathmtu - When Path MTU discovery is disabled the value
5254 * specified here will be the "fixed" path mtu.
5255 * Note that if the spp_address field is empty
5256 * then all associations on this address will
5257 * have this fixed path mtu set upon them.
5258 *
5259 * spp_sackdelay - When delayed sack is enabled, this value specifies
5260 * the number of milliseconds that sacks will be delayed
5261 * for. This value will apply to all addresses of an
5262 * association if the spp_address field is empty. Note
5263 * also, that if delayed sack is enabled and this
5264 * value is set to 0, no change is made to the last
5265 * recorded delayed sack timer value.
5266 *
5267 * spp_flags - These flags are used to control various features
5268 * on an association. The flag field may contain
5269 * zero or more of the following options.
5270 *
5271 * SPP_HB_ENABLE - Enable heartbeats on the
5272 * specified address. Note that if the address
5273 * field is empty all addresses for the association
5274 * have heartbeats enabled upon them.
5275 *
5276 * SPP_HB_DISABLE - Disable heartbeats on the
5277 * speicifed address. Note that if the address
5278 * field is empty all addresses for the association
5279 * will have their heartbeats disabled. Note also
5280 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5281 * mutually exclusive, only one of these two should
5282 * be specified. Enabling both fields will have
5283 * undetermined results.
5284 *
5285 * SPP_HB_DEMAND - Request a user initiated heartbeat
5286 * to be made immediately.
5287 *
5288 * SPP_PMTUD_ENABLE - This field will enable PMTU
5289 * discovery upon the specified address. Note that
5290 * if the address feild is empty then all addresses
5291 * on the association are effected.
5292 *
5293 * SPP_PMTUD_DISABLE - This field will disable PMTU
5294 * discovery upon the specified address. Note that
5295 * if the address feild is empty then all addresses
5296 * on the association are effected. Not also that
5297 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5298 * exclusive. Enabling both will have undetermined
5299 * results.
5300 *
5301 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5302 * on delayed sack. The time specified in spp_sackdelay
5303 * is used to specify the sack delay for this address. Note
5304 * that if spp_address is empty then all addresses will
5305 * enable delayed sack and take on the sack delay
5306 * value specified in spp_sackdelay.
5307 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5308 * off delayed sack. If the spp_address field is blank then
5309 * delayed sack is disabled for the entire association. Note
5310 * also that this field is mutually exclusive to
5311 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5312 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 */
5314static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005315 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316{
Frank Filz52ccb8e2005-12-22 11:36:46 -08005317 struct sctp_paddrparams params;
5318 struct sctp_transport *trans = NULL;
5319 struct sctp_association *asoc = NULL;
5320 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321
Neil Horman408f22e2007-06-16 14:03:45 -04005322 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005324 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 if (copy_from_user(&params, optval, len))
5326 return -EFAULT;
5327
Frank Filz52ccb8e2005-12-22 11:36:46 -08005328 /* If an address other than INADDR_ANY is specified, and
5329 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 */
wangweidongcb3f8372013-12-23 12:16:50 +08005331 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005332 trans = sctp_addr_id2transport(sk, &params.spp_address,
5333 params.spp_assoc_id);
5334 if (!trans) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005335 pr_debug("%s: failed no transport\n", __func__);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005336 return -EINVAL;
5337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 }
5339
Frank Filz52ccb8e2005-12-22 11:36:46 -08005340 /* Get association, if assoc_id != 0 and the socket is a one
5341 * to many style socket, and an association was not found, then
5342 * the id was invalid.
5343 */
5344 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5345 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005346 pr_debug("%s: failed no association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08005348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349
Frank Filz52ccb8e2005-12-22 11:36:46 -08005350 if (trans) {
5351 /* Fetch transport values. */
5352 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5353 params.spp_pathmtu = trans->pathmtu;
5354 params.spp_pathmaxrxt = trans->pathmaxrxt;
5355 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356
Frank Filz52ccb8e2005-12-22 11:36:46 -08005357 /*draft-11 doesn't say what to return in spp_flags*/
5358 params.spp_flags = trans->param_flags;
5359 } else if (asoc) {
5360 /* Fetch association values. */
5361 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5362 params.spp_pathmtu = asoc->pathmtu;
5363 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5364 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
Frank Filz52ccb8e2005-12-22 11:36:46 -08005366 /*draft-11 doesn't say what to return in spp_flags*/
5367 params.spp_flags = asoc->param_flags;
5368 } else {
5369 /* Fetch socket values. */
5370 params.spp_hbinterval = sp->hbinterval;
5371 params.spp_pathmtu = sp->pathmtu;
5372 params.spp_sackdelay = sp->sackdelay;
5373 params.spp_pathmaxrxt = sp->pathmaxrxt;
5374
5375 /*draft-11 doesn't say what to return in spp_flags*/
5376 params.spp_flags = sp->param_flags;
5377 }
5378
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 if (copy_to_user(optval, &params, len))
5380 return -EFAULT;
5381
5382 if (put_user(len, optlen))
5383 return -EFAULT;
5384
5385 return 0;
5386}
5387
Wei Yongjund364d922008-05-09 15:13:26 -07005388/*
5389 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08005390 *
Wei Yongjund364d922008-05-09 15:13:26 -07005391 * This option will effect the way delayed acks are performed. This
5392 * option allows you to get or set the delayed ack time, in
5393 * milliseconds. It also allows changing the delayed ack frequency.
5394 * Changing the frequency to 1 disables the delayed sack algorithm. If
5395 * the assoc_id is 0, then this sets or gets the endpoints default
5396 * values. If the assoc_id field is non-zero, then the set or get
5397 * effects the specified association for the one to many model (the
5398 * assoc_id field is ignored by the one to one model). Note that if
5399 * sack_delay or sack_freq are 0 when setting this option, then the
5400 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08005401 *
Wei Yongjund364d922008-05-09 15:13:26 -07005402 * struct sctp_sack_info {
5403 * sctp_assoc_t sack_assoc_id;
5404 * uint32_t sack_delay;
5405 * uint32_t sack_freq;
5406 * };
Frank Filz77086102005-12-22 11:37:30 -08005407 *
Wei Yongjund364d922008-05-09 15:13:26 -07005408 * sack_assoc_id - This parameter, indicates which association the user
5409 * is performing an action upon. Note that if this field's value is
5410 * zero then the endpoints default value is changed (effecting future
5411 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08005412 *
Wei Yongjund364d922008-05-09 15:13:26 -07005413 * sack_delay - This parameter contains the number of milliseconds that
5414 * the user is requesting the delayed ACK timer be set to. Note that
5415 * this value is defined in the standard to be between 200 and 500
5416 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08005417 *
Wei Yongjund364d922008-05-09 15:13:26 -07005418 * sack_freq - This parameter contains the number of packets that must
5419 * be received before a sack is sent without waiting for the delay
5420 * timer to expire. The default value for this is 2, setting this
5421 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08005422 */
Wei Yongjund364d922008-05-09 15:13:26 -07005423static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08005424 char __user *optval,
5425 int __user *optlen)
5426{
Wei Yongjund364d922008-05-09 15:13:26 -07005427 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08005428 struct sctp_association *asoc = NULL;
5429 struct sctp_sock *sp = sctp_sk(sk);
5430
Wei Yongjund364d922008-05-09 15:13:26 -07005431 if (len >= sizeof(struct sctp_sack_info)) {
5432 len = sizeof(struct sctp_sack_info);
5433
5434 if (copy_from_user(&params, optval, len))
5435 return -EFAULT;
5436 } else if (len == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05005437 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005438 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005439 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005440 "Use struct sctp_sack_info instead\n",
5441 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07005442 if (copy_from_user(&params, optval, len))
5443 return -EFAULT;
5444 } else
wangweidongcb3f8372013-12-23 12:16:50 +08005445 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08005446
Wei Yongjund364d922008-05-09 15:13:26 -07005447 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08005448 * to many style socket, and an association was not found, then
5449 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005450 */
Wei Yongjund364d922008-05-09 15:13:26 -07005451 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5452 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08005453 return -EINVAL;
5454
5455 if (asoc) {
5456 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005457 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5458 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08005459 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07005460 params.sack_freq = asoc->sackfreq;
5461
5462 } else {
5463 params.sack_delay = 0;
5464 params.sack_freq = 1;
5465 }
Frank Filz77086102005-12-22 11:37:30 -08005466 } else {
5467 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005468 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5469 params.sack_delay = sp->sackdelay;
5470 params.sack_freq = sp->sackfreq;
5471 } else {
5472 params.sack_delay = 0;
5473 params.sack_freq = 1;
5474 }
Frank Filz77086102005-12-22 11:37:30 -08005475 }
5476
5477 if (copy_to_user(optval, &params, len))
5478 return -EFAULT;
5479
5480 if (put_user(len, optlen))
5481 return -EFAULT;
5482
5483 return 0;
5484}
5485
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5487 *
5488 * Applications can specify protocol parameters for the default association
5489 * initialization. The option name argument to setsockopt() and getsockopt()
5490 * is SCTP_INITMSG.
5491 *
5492 * Setting initialization parameters is effective only on an unconnected
5493 * socket (for UDP-style sockets only future associations are effected
5494 * by the change). With TCP-style sockets, this option is inherited by
5495 * sockets derived from a listener socket.
5496 */
5497static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5498{
Neil Horman408f22e2007-06-16 14:03:45 -04005499 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005501 len = sizeof(struct sctp_initmsg);
5502 if (put_user(len, optlen))
5503 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5505 return -EFAULT;
5506 return 0;
5507}
5508
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005510static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5511 char __user *optval, int __user *optlen)
5512{
5513 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005514 int cnt = 0;
5515 struct sctp_getaddrs getaddrs;
5516 struct sctp_transport *from;
5517 void __user *to;
5518 union sctp_addr temp;
5519 struct sctp_sock *sp = sctp_sk(sk);
5520 int addrlen;
5521 size_t space_left;
5522 int bytes_copied;
5523
5524 if (len < sizeof(struct sctp_getaddrs))
5525 return -EINVAL;
5526
5527 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5528 return -EFAULT;
5529
5530 /* For UDP-style sockets, id specifies the association to query. */
5531 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5532 if (!asoc)
5533 return -EINVAL;
5534
wangweidongcb3f8372013-12-23 12:16:50 +08005535 to = optval + offsetof(struct sctp_getaddrs, addrs);
5536 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005537
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07005538 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5539 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08005540 memcpy(&temp, &from->ipaddr, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005541 addrlen = sctp_get_pf_specific(sk->sk_family)
5542 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005543 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005544 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005545 if (copy_to_user(to, &temp, addrlen))
5546 return -EFAULT;
5547 to += addrlen;
5548 cnt++;
5549 space_left -= addrlen;
5550 }
5551
5552 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5553 return -EFAULT;
5554 bytes_copied = ((char __user *)to) - optval;
5555 if (put_user(bytes_copied, optlen))
5556 return -EFAULT;
5557
5558 return 0;
5559}
5560
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005561static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5562 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005563{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005564 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005565 union sctp_addr temp;
5566 int cnt = 0;
5567 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005568 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005569
Vlad Yasevich29303542007-09-16 16:02:12 -07005570 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005571 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07005572 if (!addr->valid)
5573 continue;
5574
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005575 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08005576 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005577 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005578 if ((PF_INET6 == sk->sk_family) &&
5579 inet_v6_ipv6only(sk) &&
5580 (AF_INET == addr->a.sa.sa_family))
5581 continue;
Al Viro6244be42006-11-20 17:21:44 -08005582 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05005583 if (!temp.v4.sin_port)
5584 temp.v4.sin_port = htons(port);
5585
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005586 addrlen = sctp_get_pf_specific(sk->sk_family)
5587 ->addr_to_user(sctp_sk(sk), &temp);
5588
Vlad Yasevich29303542007-09-16 16:02:12 -07005589 if (space_left < addrlen) {
5590 cnt = -ENOMEM;
5591 break;
5592 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005593 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08005594
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005595 to += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005596 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005597 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04005598 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005599 }
Vlad Yasevich29303542007-09-16 16:02:12 -07005600 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005601
5602 return cnt;
5603}
5604
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005606static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5607 char __user *optval, int __user *optlen)
5608{
5609 struct sctp_bind_addr *bp;
5610 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005611 int cnt = 0;
5612 struct sctp_getaddrs getaddrs;
5613 struct sctp_sockaddr_entry *addr;
5614 void __user *to;
5615 union sctp_addr temp;
5616 struct sctp_sock *sp = sctp_sk(sk);
5617 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005618 int err = 0;
5619 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005620 int bytes_copied = 0;
5621 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005622 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005623
Neil Horman408f22e2007-06-16 14:03:45 -04005624 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005625 return -EINVAL;
5626
5627 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5628 return -EFAULT;
5629
5630 /*
5631 * For UDP-style sockets, id specifies the association to query.
5632 * If the id field is set to the value '0' then the locally bound
5633 * addresses are returned without regard to any particular
5634 * association.
5635 */
5636 if (0 == getaddrs.assoc_id) {
5637 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005638 } else {
5639 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5640 if (!asoc)
5641 return -EINVAL;
5642 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005643 }
5644
wangweidongcb3f8372013-12-23 12:16:50 +08005645 to = optval + offsetof(struct sctp_getaddrs, addrs);
5646 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Neil Horman186e2342007-06-18 19:59:16 -04005647
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02005648 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005649 if (!addrs)
5650 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005651
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005652 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5653 * addresses from the global local address list.
5654 */
5655 if (sctp_list_single_entry(&bp->address_list)) {
5656 addr = list_entry(bp->address_list.next,
5657 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04005658 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005659 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5660 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005661 if (cnt < 0) {
5662 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005663 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005664 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005665 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005666 }
5667 }
5668
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005669 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005670 /* Protection on the bound address list is not needed since
5671 * in the socket option context we hold a socket lock and
5672 * thus the bound address list can't change.
5673 */
5674 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08005675 memcpy(&temp, &addr->a, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005676 addrlen = sctp_get_pf_specific(sk->sk_family)
5677 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005678 if (space_left < addrlen) {
5679 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07005680 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005681 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005682 memcpy(buf, &temp, addrlen);
5683 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005684 bytes_copied += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005685 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005686 space_left -= addrlen;
5687 }
5688
5689copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005690 if (copy_to_user(to, addrs, bytes_copied)) {
5691 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005692 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005693 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005694 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
5695 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005696 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005697 }
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02005698 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
5699 * but we can't change it anymore.
5700 */
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005701 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005702 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005703out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005704 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005705 return err;
5706}
5707
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
5709 *
5710 * Requests that the local SCTP stack use the enclosed peer address as
5711 * the association primary. The enclosed address must be one of the
5712 * association peer's addresses.
5713 */
5714static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
5715 char __user *optval, int __user *optlen)
5716{
5717 struct sctp_prim prim;
5718 struct sctp_association *asoc;
5719 struct sctp_sock *sp = sctp_sk(sk);
5720
Neil Horman408f22e2007-06-16 14:03:45 -04005721 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 return -EINVAL;
5723
Neil Horman408f22e2007-06-16 14:03:45 -04005724 len = sizeof(struct sctp_prim);
5725
5726 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 return -EFAULT;
5728
5729 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
5730 if (!asoc)
5731 return -EINVAL;
5732
5733 if (!asoc->peer.primary_path)
5734 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005735
Al Viro8cec6b82006-11-20 17:23:01 -08005736 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
5737 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005739 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 (union sctp_addr *)&prim.ssp_addr);
5741
Neil Horman408f22e2007-06-16 14:03:45 -04005742 if (put_user(len, optlen))
5743 return -EFAULT;
5744 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 return -EFAULT;
5746
5747 return 0;
5748}
5749
5750/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005751 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005753 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 * Indication parameter for all future INIT and INIT-ACK exchanges.
5755 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005756static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 char __user *optval, int __user *optlen)
5758{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005759 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760
Neil Horman408f22e2007-06-16 14:03:45 -04005761 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 return -EINVAL;
5763
Neil Horman408f22e2007-06-16 14:03:45 -04005764 len = sizeof(struct sctp_setadaptation);
5765
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005766 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04005767
5768 if (put_user(len, optlen))
5769 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005770 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07005772
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 return 0;
5774}
5775
5776/*
5777 *
5778 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
5779 *
5780 * Applications that wish to use the sendto() system call may wish to
5781 * specify a default set of parameters that would normally be supplied
5782 * through the inclusion of ancillary data. This socket option allows
5783 * such an application to set the default sctp_sndrcvinfo structure.
5784
5785
5786 * The application that wishes to use this socket option simply passes
5787 * in to this call the sctp_sndrcvinfo structure defined in Section
5788 * 5.2.2) The input parameters accepted by this call include
5789 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
5790 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
5791 * to this call if the caller is using the UDP model.
5792 *
5793 * For getsockopt, it get the default sctp_sndrcvinfo structure.
5794 */
5795static int sctp_getsockopt_default_send_param(struct sock *sk,
5796 int len, char __user *optval,
5797 int __user *optlen)
5798{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005800 struct sctp_association *asoc;
5801 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005803 if (len < sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005805
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005806 len = sizeof(info);
Neil Horman408f22e2007-06-16 14:03:45 -04005807
5808 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 return -EFAULT;
5810
5811 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
5812 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
5813 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 if (asoc) {
5815 info.sinfo_stream = asoc->default_stream;
5816 info.sinfo_flags = asoc->default_flags;
5817 info.sinfo_ppid = asoc->default_ppid;
5818 info.sinfo_context = asoc->default_context;
5819 info.sinfo_timetolive = asoc->default_timetolive;
5820 } else {
5821 info.sinfo_stream = sp->default_stream;
5822 info.sinfo_flags = sp->default_flags;
5823 info.sinfo_ppid = sp->default_ppid;
5824 info.sinfo_context = sp->default_context;
5825 info.sinfo_timetolive = sp->default_timetolive;
5826 }
5827
Neil Horman408f22e2007-06-16 14:03:45 -04005828 if (put_user(len, optlen))
5829 return -EFAULT;
5830 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 return -EFAULT;
5832
5833 return 0;
5834}
5835
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005836/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
5837 * (SCTP_DEFAULT_SNDINFO)
5838 */
5839static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
5840 char __user *optval,
5841 int __user *optlen)
5842{
5843 struct sctp_sock *sp = sctp_sk(sk);
5844 struct sctp_association *asoc;
5845 struct sctp_sndinfo info;
5846
5847 if (len < sizeof(info))
5848 return -EINVAL;
5849
5850 len = sizeof(info);
5851
5852 if (copy_from_user(&info, optval, len))
5853 return -EFAULT;
5854
5855 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
5856 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
5857 return -EINVAL;
5858 if (asoc) {
5859 info.snd_sid = asoc->default_stream;
5860 info.snd_flags = asoc->default_flags;
5861 info.snd_ppid = asoc->default_ppid;
5862 info.snd_context = asoc->default_context;
5863 } else {
5864 info.snd_sid = sp->default_stream;
5865 info.snd_flags = sp->default_flags;
5866 info.snd_ppid = sp->default_ppid;
5867 info.snd_context = sp->default_context;
5868 }
5869
5870 if (put_user(len, optlen))
5871 return -EFAULT;
5872 if (copy_to_user(optval, &info, len))
5873 return -EFAULT;
5874
5875 return 0;
5876}
5877
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878/*
5879 *
5880 * 7.1.5 SCTP_NODELAY
5881 *
5882 * Turn on/off any Nagle-like algorithm. This means that packets are
5883 * generally sent as soon as possible and no unnecessary delays are
5884 * introduced, at the cost of more packets in the network. Expects an
5885 * integer boolean flag.
5886 */
5887
5888static int sctp_getsockopt_nodelay(struct sock *sk, int len,
5889 char __user *optval, int __user *optlen)
5890{
5891 int val;
5892
5893 if (len < sizeof(int))
5894 return -EINVAL;
5895
5896 len = sizeof(int);
5897 val = (sctp_sk(sk)->nodelay == 1);
5898 if (put_user(len, optlen))
5899 return -EFAULT;
5900 if (copy_to_user(optval, &val, len))
5901 return -EFAULT;
5902 return 0;
5903}
5904
5905/*
5906 *
5907 * 7.1.1 SCTP_RTOINFO
5908 *
5909 * The protocol parameters used to initialize and bound retransmission
5910 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
5911 * and modify these parameters.
5912 * All parameters are time values, in milliseconds. A value of 0, when
5913 * modifying the parameters, indicates that the current value should not
5914 * be changed.
5915 *
5916 */
5917static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
5918 char __user *optval,
5919 int __user *optlen) {
5920 struct sctp_rtoinfo rtoinfo;
5921 struct sctp_association *asoc;
5922
Neil Horman408f22e2007-06-16 14:03:45 -04005923 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 return -EINVAL;
5925
Neil Horman408f22e2007-06-16 14:03:45 -04005926 len = sizeof(struct sctp_rtoinfo);
5927
5928 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 return -EFAULT;
5930
5931 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
5932
5933 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
5934 return -EINVAL;
5935
5936 /* Values corresponding to the specific association. */
5937 if (asoc) {
5938 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
5939 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
5940 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
5941 } else {
5942 /* Values corresponding to the endpoint. */
5943 struct sctp_sock *sp = sctp_sk(sk);
5944
5945 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
5946 rtoinfo.srto_max = sp->rtoinfo.srto_max;
5947 rtoinfo.srto_min = sp->rtoinfo.srto_min;
5948 }
5949
5950 if (put_user(len, optlen))
5951 return -EFAULT;
5952
5953 if (copy_to_user(optval, &rtoinfo, len))
5954 return -EFAULT;
5955
5956 return 0;
5957}
5958
5959/*
5960 *
5961 * 7.1.2 SCTP_ASSOCINFO
5962 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02005963 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 * of the association.
5965 * Returns an error if the new association retransmission value is
5966 * greater than the sum of the retransmission value of the peer.
5967 * See [SCTP] for more information.
5968 *
5969 */
5970static int sctp_getsockopt_associnfo(struct sock *sk, int len,
5971 char __user *optval,
5972 int __user *optlen)
5973{
5974
5975 struct sctp_assocparams assocparams;
5976 struct sctp_association *asoc;
5977 struct list_head *pos;
5978 int cnt = 0;
5979
Neil Horman408f22e2007-06-16 14:03:45 -04005980 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 return -EINVAL;
5982
Neil Horman408f22e2007-06-16 14:03:45 -04005983 len = sizeof(struct sctp_assocparams);
5984
5985 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 return -EFAULT;
5987
5988 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
5989
5990 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
5991 return -EINVAL;
5992
5993 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07005994 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
5996 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
5997 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
Daniel Borkmann52db8822013-06-25 18:17:27 +02005998 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
6000 list_for_each(pos, &asoc->peer.transport_addr_list) {
wangweidongcb3f8372013-12-23 12:16:50 +08006001 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 }
6003
6004 assocparams.sasoc_number_peer_destinations = cnt;
6005 } else {
6006 /* Values corresponding to the endpoint */
6007 struct sctp_sock *sp = sctp_sk(sk);
6008
6009 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6010 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6011 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6012 assocparams.sasoc_cookie_life =
6013 sp->assocparams.sasoc_cookie_life;
6014 assocparams.sasoc_number_peer_destinations =
6015 sp->assocparams.
6016 sasoc_number_peer_destinations;
6017 }
6018
6019 if (put_user(len, optlen))
6020 return -EFAULT;
6021
6022 if (copy_to_user(optval, &assocparams, len))
6023 return -EFAULT;
6024
6025 return 0;
6026}
6027
6028/*
6029 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6030 *
6031 * This socket option is a boolean flag which turns on or off mapped V4
6032 * addresses. If this option is turned on and the socket is type
6033 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6034 * If this option is turned off, then no mapping will be done of V4
6035 * addresses and a user will receive both PF_INET6 and PF_INET type
6036 * addresses on the socket.
6037 */
6038static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6039 char __user *optval, int __user *optlen)
6040{
6041 int val;
6042 struct sctp_sock *sp = sctp_sk(sk);
6043
6044 if (len < sizeof(int))
6045 return -EINVAL;
6046
6047 len = sizeof(int);
6048 val = sp->v4mapped;
6049 if (put_user(len, optlen))
6050 return -EFAULT;
6051 if (copy_to_user(optval, &val, len))
6052 return -EFAULT;
6053
6054 return 0;
6055}
6056
6057/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006058 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6059 * (chapter and verse is quoted at sctp_setsockopt_context())
6060 */
6061static int sctp_getsockopt_context(struct sock *sk, int len,
6062 char __user *optval, int __user *optlen)
6063{
6064 struct sctp_assoc_value params;
6065 struct sctp_sock *sp;
6066 struct sctp_association *asoc;
6067
Neil Horman408f22e2007-06-16 14:03:45 -04006068 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006069 return -EINVAL;
6070
Neil Horman408f22e2007-06-16 14:03:45 -04006071 len = sizeof(struct sctp_assoc_value);
6072
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006073 if (copy_from_user(&params, optval, len))
6074 return -EFAULT;
6075
6076 sp = sctp_sk(sk);
6077
6078 if (params.assoc_id != 0) {
6079 asoc = sctp_id2assoc(sk, params.assoc_id);
6080 if (!asoc)
6081 return -EINVAL;
6082 params.assoc_value = asoc->default_rcv_context;
6083 } else {
6084 params.assoc_value = sp->default_rcv_context;
6085 }
6086
6087 if (put_user(len, optlen))
6088 return -EFAULT;
6089 if (copy_to_user(optval, &params, len))
6090 return -EFAULT;
6091
6092 return 0;
6093}
6094
6095/*
Wei Yongjune89c2092008-12-25 16:54:58 -08006096 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6097 * This option will get or set the maximum size to put in any outgoing
6098 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 * fragmented by SCTP into the specified size. Note that the underlying
6100 * SCTP implementation may fragment into smaller sized chunks when the
6101 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08006102 * the user. The default value for this option is '0' which indicates
6103 * the user is NOT limiting fragmentation and only the PMTU will effect
6104 * SCTP's choice of DATA chunk size. Note also that values set larger
6105 * than the maximum size of an IP datagram will effectively let SCTP
6106 * control fragmentation (i.e. the same as setting this option to 0).
6107 *
6108 * The following structure is used to access and modify this parameter:
6109 *
6110 * struct sctp_assoc_value {
6111 * sctp_assoc_t assoc_id;
6112 * uint32_t assoc_value;
6113 * };
6114 *
6115 * assoc_id: This parameter is ignored for one-to-one style sockets.
6116 * For one-to-many style sockets this parameter indicates which
6117 * association the user is performing an action upon. Note that if
6118 * this field's value is zero then the endpoints default value is
6119 * changed (effecting future associations only).
6120 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 */
6122static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6123 char __user *optval, int __user *optlen)
6124{
Wei Yongjune89c2092008-12-25 16:54:58 -08006125 struct sctp_assoc_value params;
6126 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127
Wei Yongjune89c2092008-12-25 16:54:58 -08006128 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006129 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006130 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006131 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006132 "Use struct sctp_assoc_value instead\n",
6133 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08006134 params.assoc_id = 0;
6135 } else if (len >= sizeof(struct sctp_assoc_value)) {
6136 len = sizeof(struct sctp_assoc_value);
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006137 if (copy_from_user(&params, optval, len))
Wei Yongjune89c2092008-12-25 16:54:58 -08006138 return -EFAULT;
6139 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 return -EINVAL;
6141
Wei Yongjune89c2092008-12-25 16:54:58 -08006142 asoc = sctp_id2assoc(sk, params.assoc_id);
6143 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
6144 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145
Wei Yongjune89c2092008-12-25 16:54:58 -08006146 if (asoc)
6147 params.assoc_value = asoc->frag_point;
6148 else
6149 params.assoc_value = sctp_sk(sk)->user_frag;
6150
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 if (put_user(len, optlen))
6152 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08006153 if (len == sizeof(int)) {
6154 if (copy_to_user(optval, &params.assoc_value, len))
6155 return -EFAULT;
6156 } else {
6157 if (copy_to_user(optval, &params, len))
6158 return -EFAULT;
6159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
6161 return 0;
6162}
6163
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006164/*
6165 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6166 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6167 */
6168static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6169 char __user *optval, int __user *optlen)
6170{
6171 int val;
6172
6173 if (len < sizeof(int))
6174 return -EINVAL;
6175
6176 len = sizeof(int);
6177
6178 val = sctp_sk(sk)->frag_interleave;
6179 if (put_user(len, optlen))
6180 return -EFAULT;
6181 if (copy_to_user(optval, &val, len))
6182 return -EFAULT;
6183
6184 return 0;
6185}
6186
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006187/*
6188 * 7.1.25. Set or Get the sctp partial delivery point
6189 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6190 */
6191static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6192 char __user *optval,
6193 int __user *optlen)
6194{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09006195 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006196
6197 if (len < sizeof(u32))
6198 return -EINVAL;
6199
6200 len = sizeof(u32);
6201
6202 val = sctp_sk(sk)->pd_point;
6203 if (put_user(len, optlen))
6204 return -EFAULT;
6205 if (copy_to_user(optval, &val, len))
6206 return -EFAULT;
6207
Wei Yongjun7d743b72010-12-14 16:10:41 +00006208 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006209}
6210
Vlad Yasevich70331572007-03-23 11:34:36 -07006211/*
6212 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6213 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6214 */
6215static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6216 char __user *optval,
6217 int __user *optlen)
6218{
Neil Horman219b99a2008-03-05 13:44:46 -08006219 struct sctp_assoc_value params;
6220 struct sctp_sock *sp;
6221 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07006222
Neil Horman219b99a2008-03-05 13:44:46 -08006223 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006224 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006225 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006226 "Use of int in max_burst socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006227 "Use struct sctp_assoc_value instead\n",
6228 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08006229 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00006230 } else if (len >= sizeof(struct sctp_assoc_value)) {
6231 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08006232 if (copy_from_user(&params, optval, len))
6233 return -EFAULT;
6234 } else
6235 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07006236
Neil Horman219b99a2008-03-05 13:44:46 -08006237 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07006238
Neil Horman219b99a2008-03-05 13:44:46 -08006239 if (params.assoc_id != 0) {
6240 asoc = sctp_id2assoc(sk, params.assoc_id);
6241 if (!asoc)
6242 return -EINVAL;
6243 params.assoc_value = asoc->max_burst;
6244 } else
6245 params.assoc_value = sp->max_burst;
6246
6247 if (len == sizeof(int)) {
6248 if (copy_to_user(optval, &params.assoc_value, len))
6249 return -EFAULT;
6250 } else {
6251 if (copy_to_user(optval, &params, len))
6252 return -EFAULT;
6253 }
6254
6255 return 0;
6256
Vlad Yasevich70331572007-03-23 11:34:36 -07006257}
6258
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006259static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6260 char __user *optval, int __user *optlen)
6261{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006262 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006263 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006264 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006265 __u16 data_len = 0;
6266 u32 num_idents;
Xin Long7a84bd42016-02-03 23:33:30 +08006267 int i;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006268
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006269 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006270 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006271
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006272 hmacs = ep->auth_hmacs_list;
Xin Long3c918702017-06-30 11:52:16 +08006273 data_len = ntohs(hmacs->param_hdr.length) -
6274 sizeof(struct sctp_paramhdr);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006275
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006276 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006277 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006278
6279 len = sizeof(struct sctp_hmacalgo) + data_len;
6280 num_idents = data_len / sizeof(u16);
6281
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006282 if (put_user(len, optlen))
6283 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006284 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006285 return -EFAULT;
Xin Long7a84bd42016-02-03 23:33:30 +08006286 for (i = 0; i < num_idents; i++) {
6287 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6288
6289 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6290 return -EFAULT;
6291 }
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006292 return 0;
6293}
6294
6295static int sctp_getsockopt_active_key(struct sock *sk, int len,
6296 char __user *optval, int __user *optlen)
6297{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006298 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006299 struct sctp_authkeyid val;
6300 struct sctp_association *asoc;
6301
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006302 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006303 return -EACCES;
6304
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006305 if (len < sizeof(struct sctp_authkeyid))
6306 return -EINVAL;
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006307
6308 len = sizeof(struct sctp_authkeyid);
6309 if (copy_from_user(&val, optval, len))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006310 return -EFAULT;
6311
6312 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6313 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6314 return -EINVAL;
6315
6316 if (asoc)
6317 val.scact_keynumber = asoc->active_key_id;
6318 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006319 val.scact_keynumber = ep->active_key_id;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006320
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006321 if (put_user(len, optlen))
6322 return -EFAULT;
6323 if (copy_to_user(optval, &val, len))
6324 return -EFAULT;
6325
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006326 return 0;
6327}
6328
6329static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6330 char __user *optval, int __user *optlen)
6331{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006332 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006333 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006334 struct sctp_authchunks val;
6335 struct sctp_association *asoc;
6336 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006337 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006338 char __user *to;
6339
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006340 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006341 return -EACCES;
6342
6343 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006344 return -EINVAL;
6345
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006346 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006347 return -EFAULT;
6348
Al Viro411223c2007-10-14 19:21:20 +01006349 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006350 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6351 if (!asoc)
6352 return -EINVAL;
6353
6354 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006355 if (!ch)
6356 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006357
6358 /* See if the user provided enough room for all the data */
Xin Long3c918702017-06-30 11:52:16 +08006359 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevichb40db682008-02-27 14:40:37 -05006360 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006361 return -EINVAL;
6362
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006363 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006364 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006365num:
6366 len = sizeof(struct sctp_authchunks) + num_chunks;
wangweidong8d726512013-12-23 12:16:53 +08006367 if (put_user(len, optlen))
6368 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006369 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6370 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006371 return 0;
6372}
6373
6374static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6375 char __user *optval, int __user *optlen)
6376{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006377 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006378 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006379 struct sctp_authchunks val;
6380 struct sctp_association *asoc;
6381 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006382 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006383 char __user *to;
6384
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006385 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006386 return -EACCES;
6387
6388 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006389 return -EINVAL;
6390
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006391 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006392 return -EFAULT;
6393
Al Viro411223c2007-10-14 19:21:20 +01006394 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006395 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6396 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6397 return -EINVAL;
6398
6399 if (asoc)
wangweidong26ac8e52013-12-23 12:16:51 +08006400 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006401 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006402 ch = ep->auth_chunk_list;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006403
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006404 if (!ch)
6405 goto num;
6406
Xin Long3c918702017-06-30 11:52:16 +08006407 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006408 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006409 return -EINVAL;
6410
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006411 if (copy_to_user(to, ch->chunks, num_chunks))
6412 return -EFAULT;
6413num:
6414 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006415 if (put_user(len, optlen))
6416 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006417 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6418 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006419
6420 return 0;
6421}
6422
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08006423/*
6424 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6425 * This option gets the current number of associations that are attached
6426 * to a one-to-many style socket. The option value is an uint32_t.
6427 */
6428static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6429 char __user *optval, int __user *optlen)
6430{
6431 struct sctp_sock *sp = sctp_sk(sk);
6432 struct sctp_association *asoc;
6433 u32 val = 0;
6434
6435 if (sctp_style(sk, TCP))
6436 return -EOPNOTSUPP;
6437
6438 if (len < sizeof(u32))
6439 return -EINVAL;
6440
6441 len = sizeof(u32);
6442
6443 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6444 val++;
6445 }
6446
6447 if (put_user(len, optlen))
6448 return -EFAULT;
6449 if (copy_to_user(optval, &val, len))
6450 return -EFAULT;
6451
6452 return 0;
6453}
6454
Wei Yongjun209ba422011-04-17 17:27:08 +00006455/*
Michio Honda7dc04d72011-04-26 20:16:31 +09006456 * 8.1.23 SCTP_AUTO_ASCONF
6457 * See the corresponding setsockopt entry as description
6458 */
6459static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6460 char __user *optval, int __user *optlen)
6461{
6462 int val = 0;
6463
6464 if (len < sizeof(int))
6465 return -EINVAL;
6466
6467 len = sizeof(int);
6468 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6469 val = 1;
6470 if (put_user(len, optlen))
6471 return -EFAULT;
6472 if (copy_to_user(optval, &val, len))
6473 return -EFAULT;
6474 return 0;
6475}
6476
6477/*
Wei Yongjun209ba422011-04-17 17:27:08 +00006478 * 8.2.6. Get the Current Identifiers of Associations
6479 * (SCTP_GET_ASSOC_ID_LIST)
6480 *
6481 * This option gets the current list of SCTP association identifiers of
6482 * the SCTP associations handled by a one-to-many style socket.
6483 */
6484static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6485 char __user *optval, int __user *optlen)
6486{
6487 struct sctp_sock *sp = sctp_sk(sk);
6488 struct sctp_association *asoc;
6489 struct sctp_assoc_ids *ids;
6490 u32 num = 0;
6491
6492 if (sctp_style(sk, TCP))
6493 return -EOPNOTSUPP;
6494
6495 if (len < sizeof(struct sctp_assoc_ids))
6496 return -EINVAL;
6497
6498 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6499 num++;
6500 }
6501
6502 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6503 return -EINVAL;
6504
6505 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6506
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02006507 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
Wei Yongjun209ba422011-04-17 17:27:08 +00006508 if (unlikely(!ids))
6509 return -ENOMEM;
6510
6511 ids->gaids_number_of_ids = num;
6512 num = 0;
6513 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6514 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6515 }
6516
6517 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6518 kfree(ids);
6519 return -EFAULT;
6520 }
6521
6522 kfree(ids);
6523 return 0;
6524}
6525
Neil Horman5aa93bc2012-07-21 07:56:07 +00006526/*
6527 * SCTP_PEER_ADDR_THLDS
6528 *
6529 * This option allows us to fetch the partially failed threshold for one or all
6530 * transports in an association. See Section 6.1 of:
6531 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6532 */
6533static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6534 char __user *optval,
6535 int len,
6536 int __user *optlen)
6537{
6538 struct sctp_paddrthlds val;
6539 struct sctp_transport *trans;
6540 struct sctp_association *asoc;
6541
6542 if (len < sizeof(struct sctp_paddrthlds))
6543 return -EINVAL;
6544 len = sizeof(struct sctp_paddrthlds);
6545 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6546 return -EFAULT;
6547
6548 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6549 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6550 if (!asoc)
6551 return -ENOENT;
6552
6553 val.spt_pathpfthld = asoc->pf_retrans;
6554 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6555 } else {
6556 trans = sctp_addr_id2transport(sk, &val.spt_address,
6557 val.spt_assoc_id);
6558 if (!trans)
6559 return -ENOENT;
6560
6561 val.spt_pathmaxrxt = trans->pathmaxrxt;
6562 val.spt_pathpfthld = trans->pf_retrans;
6563 }
6564
6565 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6566 return -EFAULT;
6567
6568 return 0;
6569}
6570
Michele Baldessari196d6752012-12-01 04:49:42 +00006571/*
6572 * SCTP_GET_ASSOC_STATS
6573 *
6574 * This option retrieves local per endpoint statistics. It is modeled
6575 * after OpenSolaris' implementation
6576 */
6577static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6578 char __user *optval,
6579 int __user *optlen)
6580{
6581 struct sctp_assoc_stats sas;
6582 struct sctp_association *asoc = NULL;
6583
6584 /* User must provide at least the assoc id */
6585 if (len < sizeof(sctp_assoc_t))
6586 return -EINVAL;
6587
Guenter Roeck726bc6b2013-02-27 10:57:31 +00006588 /* Allow the struct to grow and fill in as much as possible */
6589 len = min_t(size_t, len, sizeof(sas));
6590
Michele Baldessari196d6752012-12-01 04:49:42 +00006591 if (copy_from_user(&sas, optval, len))
6592 return -EFAULT;
6593
6594 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6595 if (!asoc)
6596 return -EINVAL;
6597
6598 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6599 sas.sas_gapcnt = asoc->stats.gapcnt;
6600 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6601 sas.sas_osacks = asoc->stats.osacks;
6602 sas.sas_isacks = asoc->stats.isacks;
6603 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6604 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6605 sas.sas_oodchunks = asoc->stats.oodchunks;
6606 sas.sas_iodchunks = asoc->stats.iodchunks;
6607 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6608 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6609 sas.sas_idupchunks = asoc->stats.idupchunks;
6610 sas.sas_opackets = asoc->stats.opackets;
6611 sas.sas_ipackets = asoc->stats.ipackets;
6612
6613 /* New high max rto observed, will return 0 if not a single
6614 * RTO update took place. obs_rto_ipaddr will be bogus
6615 * in such a case
6616 */
6617 sas.sas_maxrto = asoc->stats.max_obs_rto;
6618 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6619 sizeof(struct sockaddr_storage));
6620
6621 /* Mark beginning of a new observation period */
6622 asoc->stats.max_obs_rto = asoc->rto_min;
6623
Michele Baldessari196d6752012-12-01 04:49:42 +00006624 if (put_user(len, optlen))
6625 return -EFAULT;
6626
Daniel Borkmannbb333812013-06-28 19:49:40 +02006627 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
Michele Baldessari196d6752012-12-01 04:49:42 +00006628
6629 if (copy_to_user(optval, &sas, len))
6630 return -EFAULT;
6631
6632 return 0;
6633}
6634
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02006635static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6636 char __user *optval,
6637 int __user *optlen)
6638{
6639 int val = 0;
6640
6641 if (len < sizeof(int))
6642 return -EINVAL;
6643
6644 len = sizeof(int);
6645 if (sctp_sk(sk)->recvrcvinfo)
6646 val = 1;
6647 if (put_user(len, optlen))
6648 return -EFAULT;
6649 if (copy_to_user(optval, &val, len))
6650 return -EFAULT;
6651
6652 return 0;
6653}
6654
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02006655static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6656 char __user *optval,
6657 int __user *optlen)
6658{
6659 int val = 0;
6660
6661 if (len < sizeof(int))
6662 return -EINVAL;
6663
6664 len = sizeof(int);
6665 if (sctp_sk(sk)->recvnxtinfo)
6666 val = 1;
6667 if (put_user(len, optlen))
6668 return -EFAULT;
6669 if (copy_to_user(optval, &val, len))
6670 return -EFAULT;
6671
6672 return 0;
6673}
6674
Xin Long28aa4c22016-07-09 19:47:40 +08006675static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6676 char __user *optval,
6677 int __user *optlen)
6678{
6679 struct sctp_assoc_value params;
6680 struct sctp_association *asoc;
6681 int retval = -EFAULT;
6682
6683 if (len < sizeof(params)) {
6684 retval = -EINVAL;
6685 goto out;
6686 }
6687
6688 len = sizeof(params);
6689 if (copy_from_user(&params, optval, len))
6690 goto out;
6691
6692 asoc = sctp_id2assoc(sk, params.assoc_id);
6693 if (asoc) {
6694 params.assoc_value = asoc->prsctp_enable;
6695 } else if (!params.assoc_id) {
6696 struct sctp_sock *sp = sctp_sk(sk);
6697
6698 params.assoc_value = sp->ep->prsctp_enable;
6699 } else {
6700 retval = -EINVAL;
6701 goto out;
6702 }
6703
6704 if (put_user(len, optlen))
6705 goto out;
6706
6707 if (copy_to_user(optval, &params, len))
6708 goto out;
6709
6710 retval = 0;
6711
6712out:
6713 return retval;
6714}
6715
Xin Longf959fb42016-07-09 19:47:41 +08006716static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
6717 char __user *optval,
6718 int __user *optlen)
6719{
6720 struct sctp_default_prinfo info;
6721 struct sctp_association *asoc;
6722 int retval = -EFAULT;
6723
6724 if (len < sizeof(info)) {
6725 retval = -EINVAL;
6726 goto out;
6727 }
6728
6729 len = sizeof(info);
6730 if (copy_from_user(&info, optval, len))
6731 goto out;
6732
6733 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
6734 if (asoc) {
6735 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
6736 info.pr_value = asoc->default_timetolive;
6737 } else if (!info.pr_assoc_id) {
6738 struct sctp_sock *sp = sctp_sk(sk);
6739
6740 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
6741 info.pr_value = sp->default_timetolive;
6742 } else {
6743 retval = -EINVAL;
6744 goto out;
6745 }
6746
6747 if (put_user(len, optlen))
6748 goto out;
6749
6750 if (copy_to_user(optval, &info, len))
6751 goto out;
6752
6753 retval = 0;
6754
6755out:
6756 return retval;
6757}
6758
Xin Long826d2532016-07-09 19:47:42 +08006759static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
6760 char __user *optval,
6761 int __user *optlen)
6762{
6763 struct sctp_prstatus params;
6764 struct sctp_association *asoc;
6765 int policy;
6766 int retval = -EINVAL;
6767
6768 if (len < sizeof(params))
6769 goto out;
6770
6771 len = sizeof(params);
6772 if (copy_from_user(&params, optval, len)) {
6773 retval = -EFAULT;
6774 goto out;
6775 }
6776
6777 policy = params.sprstat_policy;
6778 if (policy & ~SCTP_PR_SCTP_MASK)
6779 goto out;
6780
6781 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
6782 if (!asoc)
6783 goto out;
6784
6785 if (policy == SCTP_PR_SCTP_NONE) {
6786 params.sprstat_abandoned_unsent = 0;
6787 params.sprstat_abandoned_sent = 0;
6788 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6789 params.sprstat_abandoned_unsent +=
6790 asoc->abandoned_unsent[policy];
6791 params.sprstat_abandoned_sent +=
6792 asoc->abandoned_sent[policy];
6793 }
6794 } else {
6795 params.sprstat_abandoned_unsent =
6796 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
6797 params.sprstat_abandoned_sent =
6798 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
6799 }
6800
6801 if (put_user(len, optlen)) {
6802 retval = -EFAULT;
6803 goto out;
6804 }
6805
6806 if (copy_to_user(optval, &params, len)) {
6807 retval = -EFAULT;
6808 goto out;
6809 }
6810
6811 retval = 0;
6812
6813out:
6814 return retval;
6815}
6816
Xin Longd229d482017-04-01 17:07:46 +08006817static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
6818 char __user *optval,
6819 int __user *optlen)
6820{
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006821 struct sctp_stream_out_ext *streamoute;
Xin Longd229d482017-04-01 17:07:46 +08006822 struct sctp_association *asoc;
6823 struct sctp_prstatus params;
6824 int retval = -EINVAL;
6825 int policy;
6826
6827 if (len < sizeof(params))
6828 goto out;
6829
6830 len = sizeof(params);
6831 if (copy_from_user(&params, optval, len)) {
6832 retval = -EFAULT;
6833 goto out;
6834 }
6835
6836 policy = params.sprstat_policy;
6837 if (policy & ~SCTP_PR_SCTP_MASK)
6838 goto out;
6839
6840 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
Xin Longcee360a2017-05-31 16:36:31 +08006841 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
Xin Longd229d482017-04-01 17:07:46 +08006842 goto out;
6843
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006844 streamoute = asoc->stream.out[params.sprstat_sid].ext;
6845 if (!streamoute) {
6846 /* Not allocated yet, means all stats are 0 */
6847 params.sprstat_abandoned_unsent = 0;
6848 params.sprstat_abandoned_sent = 0;
6849 retval = 0;
6850 goto out;
6851 }
6852
Xin Longd229d482017-04-01 17:07:46 +08006853 if (policy == SCTP_PR_SCTP_NONE) {
6854 params.sprstat_abandoned_unsent = 0;
6855 params.sprstat_abandoned_sent = 0;
6856 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6857 params.sprstat_abandoned_unsent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006858 streamoute->abandoned_unsent[policy];
Xin Longd229d482017-04-01 17:07:46 +08006859 params.sprstat_abandoned_sent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006860 streamoute->abandoned_sent[policy];
Xin Longd229d482017-04-01 17:07:46 +08006861 }
6862 } else {
6863 params.sprstat_abandoned_unsent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006864 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08006865 params.sprstat_abandoned_sent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006866 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08006867 }
6868
6869 if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
6870 retval = -EFAULT;
6871 goto out;
6872 }
6873
6874 retval = 0;
6875
6876out:
6877 return retval;
6878}
6879
Xin Longc0d8bab2017-03-10 12:11:12 +08006880static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
6881 char __user *optval,
6882 int __user *optlen)
6883{
6884 struct sctp_assoc_value params;
6885 struct sctp_association *asoc;
6886 int retval = -EFAULT;
6887
6888 if (len < sizeof(params)) {
6889 retval = -EINVAL;
6890 goto out;
6891 }
6892
6893 len = sizeof(params);
6894 if (copy_from_user(&params, optval, len))
6895 goto out;
6896
6897 asoc = sctp_id2assoc(sk, params.assoc_id);
6898 if (asoc) {
6899 params.assoc_value = asoc->reconf_enable;
6900 } else if (!params.assoc_id) {
6901 struct sctp_sock *sp = sctp_sk(sk);
6902
6903 params.assoc_value = sp->ep->reconf_enable;
6904 } else {
6905 retval = -EINVAL;
6906 goto out;
6907 }
6908
6909 if (put_user(len, optlen))
6910 goto out;
6911
6912 if (copy_to_user(optval, &params, len))
6913 goto out;
6914
6915 retval = 0;
6916
6917out:
6918 return retval;
6919}
6920
Xin Long9fb657a2017-01-18 00:44:46 +08006921static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
6922 char __user *optval,
6923 int __user *optlen)
6924{
6925 struct sctp_assoc_value params;
6926 struct sctp_association *asoc;
6927 int retval = -EFAULT;
6928
6929 if (len < sizeof(params)) {
6930 retval = -EINVAL;
6931 goto out;
6932 }
6933
6934 len = sizeof(params);
6935 if (copy_from_user(&params, optval, len))
6936 goto out;
6937
6938 asoc = sctp_id2assoc(sk, params.assoc_id);
6939 if (asoc) {
6940 params.assoc_value = asoc->strreset_enable;
6941 } else if (!params.assoc_id) {
6942 struct sctp_sock *sp = sctp_sk(sk);
6943
6944 params.assoc_value = sp->ep->strreset_enable;
6945 } else {
6946 retval = -EINVAL;
6947 goto out;
6948 }
6949
6950 if (put_user(len, optlen))
6951 goto out;
6952
6953 if (copy_to_user(optval, &params, len))
6954 goto out;
6955
6956 retval = 0;
6957
6958out:
6959 return retval;
6960}
6961
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03006962static int sctp_getsockopt_scheduler(struct sock *sk, int len,
6963 char __user *optval,
6964 int __user *optlen)
6965{
6966 struct sctp_assoc_value params;
6967 struct sctp_association *asoc;
6968 int retval = -EFAULT;
6969
6970 if (len < sizeof(params)) {
6971 retval = -EINVAL;
6972 goto out;
6973 }
6974
6975 len = sizeof(params);
6976 if (copy_from_user(&params, optval, len))
6977 goto out;
6978
6979 asoc = sctp_id2assoc(sk, params.assoc_id);
6980 if (!asoc) {
6981 retval = -EINVAL;
6982 goto out;
6983 }
6984
6985 params.assoc_value = sctp_sched_get_sched(asoc);
6986
6987 if (put_user(len, optlen))
6988 goto out;
6989
6990 if (copy_to_user(optval, &params, len))
6991 goto out;
6992
6993 retval = 0;
6994
6995out:
6996 return retval;
6997}
6998
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03006999static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7000 char __user *optval,
7001 int __user *optlen)
7002{
7003 struct sctp_stream_value params;
7004 struct sctp_association *asoc;
7005 int retval = -EFAULT;
7006
7007 if (len < sizeof(params)) {
7008 retval = -EINVAL;
7009 goto out;
7010 }
7011
7012 len = sizeof(params);
7013 if (copy_from_user(&params, optval, len))
7014 goto out;
7015
7016 asoc = sctp_id2assoc(sk, params.assoc_id);
7017 if (!asoc) {
7018 retval = -EINVAL;
7019 goto out;
7020 }
7021
7022 retval = sctp_sched_get_value(asoc, params.stream_id,
7023 &params.stream_value);
7024 if (retval)
7025 goto out;
7026
7027 if (put_user(len, optlen)) {
7028 retval = -EFAULT;
7029 goto out;
7030 }
7031
7032 if (copy_to_user(optval, &params, len)) {
7033 retval = -EFAULT;
7034 goto out;
7035 }
7036
7037out:
7038 return retval;
7039}
7040
Xin Long772a5862017-12-08 21:03:58 +08007041static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7042 char __user *optval,
7043 int __user *optlen)
7044{
7045 struct sctp_assoc_value params;
7046 struct sctp_association *asoc;
7047 int retval = -EFAULT;
7048
7049 if (len < sizeof(params)) {
7050 retval = -EINVAL;
7051 goto out;
7052 }
7053
7054 len = sizeof(params);
7055 if (copy_from_user(&params, optval, len))
7056 goto out;
7057
7058 asoc = sctp_id2assoc(sk, params.assoc_id);
7059 if (asoc) {
7060 params.assoc_value = asoc->intl_enable;
7061 } else if (!params.assoc_id) {
7062 struct sctp_sock *sp = sctp_sk(sk);
7063
7064 params.assoc_value = sp->strm_interleave;
7065 } else {
7066 retval = -EINVAL;
7067 goto out;
7068 }
7069
7070 if (put_user(len, optlen))
7071 goto out;
7072
7073 if (copy_to_user(optval, &params, len))
7074 goto out;
7075
7076 retval = 0;
7077
7078out:
7079 return retval;
7080}
7081
Daniel Borkmanndda91922013-06-17 11:40:05 +02007082static int sctp_getsockopt(struct sock *sk, int level, int optname,
7083 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084{
7085 int retval = 0;
7086 int len;
7087
Daniel Borkmannbb333812013-06-28 19:49:40 +02007088 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089
7090 /* I can hardly begin to describe how wrong this is. This is
7091 * so broken as to be worse than useless. The API draft
7092 * REALLY is NOT helpful here... I am not convinced that the
7093 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7094 * are at all well-founded.
7095 */
7096 if (level != SOL_SCTP) {
7097 struct sctp_af *af = sctp_sk(sk)->pf->af;
7098
7099 retval = af->getsockopt(sk, level, optname, optval, optlen);
7100 return retval;
7101 }
7102
7103 if (get_user(len, optlen))
7104 return -EFAULT;
7105
Jiri Slabya4b8e712016-10-21 14:13:24 +02007106 if (len < 0)
7107 return -EINVAL;
7108
wangweidong048ed4b2014-01-21 15:44:11 +08007109 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110
7111 switch (optname) {
7112 case SCTP_STATUS:
7113 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7114 break;
7115 case SCTP_DISABLE_FRAGMENTS:
7116 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7117 optlen);
7118 break;
7119 case SCTP_EVENTS:
7120 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7121 break;
7122 case SCTP_AUTOCLOSE:
7123 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7124 break;
7125 case SCTP_SOCKOPT_PEELOFF:
7126 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7127 break;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04007128 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7129 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7130 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 case SCTP_PEER_ADDR_PARAMS:
7132 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7133 optlen);
7134 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00007135 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07007136 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08007137 optlen);
7138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 case SCTP_INITMSG:
7140 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7141 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142 case SCTP_GET_PEER_ADDRS:
7143 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7144 optlen);
7145 break;
7146 case SCTP_GET_LOCAL_ADDRS:
7147 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7148 optlen);
7149 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04007150 case SCTP_SOCKOPT_CONNECTX3:
7151 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 case SCTP_DEFAULT_SEND_PARAM:
7154 retval = sctp_getsockopt_default_send_param(sk, len,
7155 optval, optlen);
7156 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02007157 case SCTP_DEFAULT_SNDINFO:
7158 retval = sctp_getsockopt_default_sndinfo(sk, len,
7159 optval, optlen);
7160 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 case SCTP_PRIMARY_ADDR:
7162 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7163 break;
7164 case SCTP_NODELAY:
7165 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7166 break;
7167 case SCTP_RTOINFO:
7168 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7169 break;
7170 case SCTP_ASSOCINFO:
7171 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7172 break;
7173 case SCTP_I_WANT_MAPPED_V4_ADDR:
7174 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7175 break;
7176 case SCTP_MAXSEG:
7177 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7178 break;
7179 case SCTP_GET_PEER_ADDR_INFO:
7180 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7181 optlen);
7182 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08007183 case SCTP_ADAPTATION_LAYER:
7184 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 optlen);
7186 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08007187 case SCTP_CONTEXT:
7188 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7189 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007190 case SCTP_FRAGMENT_INTERLEAVE:
7191 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7192 optlen);
7193 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07007194 case SCTP_PARTIAL_DELIVERY_POINT:
7195 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7196 optlen);
7197 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07007198 case SCTP_MAX_BURST:
7199 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7200 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007201 case SCTP_AUTH_KEY:
7202 case SCTP_AUTH_CHUNK:
7203 case SCTP_AUTH_DELETE_KEY:
7204 retval = -EOPNOTSUPP;
7205 break;
7206 case SCTP_HMAC_IDENT:
7207 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7208 break;
7209 case SCTP_AUTH_ACTIVE_KEY:
7210 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7211 break;
7212 case SCTP_PEER_AUTH_CHUNKS:
7213 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7214 optlen);
7215 break;
7216 case SCTP_LOCAL_AUTH_CHUNKS:
7217 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7218 optlen);
7219 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08007220 case SCTP_GET_ASSOC_NUMBER:
7221 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7222 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00007223 case SCTP_GET_ASSOC_ID_LIST:
7224 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7225 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09007226 case SCTP_AUTO_ASCONF:
7227 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7228 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00007229 case SCTP_PEER_ADDR_THLDS:
7230 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7231 break;
Michele Baldessari196d6752012-12-01 04:49:42 +00007232 case SCTP_GET_ASSOC_STATS:
7233 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7234 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02007235 case SCTP_RECVRCVINFO:
7236 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7237 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007238 case SCTP_RECVNXTINFO:
7239 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7240 break;
Xin Long28aa4c22016-07-09 19:47:40 +08007241 case SCTP_PR_SUPPORTED:
7242 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7243 break;
Xin Longf959fb42016-07-09 19:47:41 +08007244 case SCTP_DEFAULT_PRINFO:
7245 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7246 optlen);
7247 break;
Xin Long826d2532016-07-09 19:47:42 +08007248 case SCTP_PR_ASSOC_STATUS:
7249 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7250 optlen);
7251 break;
Xin Longd229d482017-04-01 17:07:46 +08007252 case SCTP_PR_STREAM_STATUS:
7253 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7254 optlen);
7255 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08007256 case SCTP_RECONFIG_SUPPORTED:
7257 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7258 optlen);
7259 break;
Xin Long9fb657a2017-01-18 00:44:46 +08007260 case SCTP_ENABLE_STREAM_RESET:
7261 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7262 optlen);
7263 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007264 case SCTP_STREAM_SCHEDULER:
7265 retval = sctp_getsockopt_scheduler(sk, len, optval,
7266 optlen);
7267 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007268 case SCTP_STREAM_SCHEDULER_VALUE:
7269 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7270 optlen);
7271 break;
Xin Long772a5862017-12-08 21:03:58 +08007272 case SCTP_INTERLEAVING_SUPPORTED:
7273 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7274 optlen);
7275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 default:
7277 retval = -ENOPROTOOPT;
7278 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280
wangweidong048ed4b2014-01-21 15:44:11 +08007281 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 return retval;
7283}
7284
Craig Gallek086c6532016-02-10 11:50:35 -05007285static int sctp_hash(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286{
7287 /* STUB */
Craig Gallek086c6532016-02-10 11:50:35 -05007288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289}
7290
7291static void sctp_unhash(struct sock *sk)
7292{
7293 /* STUB */
7294}
7295
7296/* Check if port is acceptable. Possibly find first available port.
7297 *
7298 * The port hash table (contained in the 'global' SCTP protocol storage
7299 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7300 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7301 * list (the list number is the port number hashed out, so as you
7302 * would expect from a hash function, all the ports in a given list have
7303 * such a number that hashes out to the same list number; you were
7304 * expecting that, right?); so each list has a set of ports, with a
7305 * link to the socket (struct sock) that uses it, the port number and
7306 * a fastreuse flag (FIXME: NPI ipg).
7307 */
7308static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007309 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310
7311static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
7312{
7313 struct sctp_bind_hashbucket *head; /* hash list */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007314 struct sctp_bind_bucket *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315 unsigned short snum;
7316 int ret;
7317
Al Viro04afd8b2006-11-20 17:02:01 -08007318 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319
Daniel Borkmannbb333812013-06-28 19:49:40 +02007320 pr_debug("%s: begins, snum:%d\n", __func__, snum);
7321
wangweidong79b91132014-01-21 15:44:07 +08007322 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323
7324 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07007325 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007326 int low, high, remaining, index;
7327 unsigned int rover;
WANG Cong122ff242014-05-12 16:04:53 -07007328 struct net *net = sock_net(sk);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007329
WANG Cong122ff242014-05-12 16:04:53 -07007330 inet_get_local_port_range(net, &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007331 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05007332 rover = prandom_u32() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 do {
7335 rover++;
7336 if ((rover < low) || (rover > high))
7337 rover = low;
WANG Cong122ff242014-05-12 16:04:53 -07007338 if (inet_is_local_reserved_port(net, rover))
Amerigo Wange3826f12010-05-05 00:27:06 +00007339 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007340 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 head = &sctp_port_hashtable[index];
wangweidong3c8e43b2014-01-21 15:44:08 +08007342 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007343 sctp_for_each_hentry(pp, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007344 if ((pp->port == rover) &&
7345 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 goto next;
7347 break;
7348 next:
wangweidong3c8e43b2014-01-21 15:44:08 +08007349 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351
7352 /* Exhausted local port range during search? */
7353 ret = 1;
7354 if (remaining <= 0)
7355 goto fail;
7356
7357 /* OK, here is the one we will use. HEAD (the port
7358 * hash table list entry) is non-NULL and we hold it's
7359 * mutex.
7360 */
7361 snum = rover;
7362 } else {
7363 /* We are given an specific port number; we verify
7364 * that it is not being used. If it is used, we will
7365 * exahust the search in the hash list corresponding
7366 * to the port number (snum) - we detect that with the
7367 * port iterator, pp being NULL.
7368 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007369 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
wangweidong3c8e43b2014-01-21 15:44:08 +08007370 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007371 sctp_for_each_hentry(pp, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007372 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 goto pp_found;
7374 }
7375 }
7376 pp = NULL;
7377 goto pp_not_found;
7378pp_found:
7379 if (!hlist_empty(&pp->owner)) {
7380 /* We had a port hash table hit - there is an
7381 * available port (pp != NULL) and it is being
7382 * used by other socket (pp->owner not empty); that other
7383 * socket is going to be sk2.
7384 */
7385 int reuse = sk->sk_reuse;
7386 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387
Daniel Borkmannbb333812013-06-28 19:49:40 +02007388 pr_debug("%s: found a possible match\n", __func__);
7389
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007390 if (pp->fastreuse && sk->sk_reuse &&
7391 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392 goto success;
7393
7394 /* Run through the list of sockets bound to the port
7395 * (pp->port) [via the pointers bind_next and
7396 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
7397 * we get the endpoint they describe and run through
7398 * the endpoint's list of IP (v4 or v6) addresses,
7399 * comparing each of the addresses with the address of
7400 * the socket sk. If we find a match, then that means
7401 * that this port/socket (sk) combination are already
7402 * in an endpoint.
7403 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007404 sk_for_each_bound(sk2, &pp->owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405 struct sctp_endpoint *ep2;
7406 ep2 = sctp_sk(sk2)->ep;
7407
Vlad Yasevich4e540642008-07-18 23:06:32 -07007408 if (sk == sk2 ||
7409 (reuse && sk2->sk_reuse &&
7410 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 continue;
7412
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07007413 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
7414 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415 ret = (long)sk2;
7416 goto fail_unlock;
7417 }
7418 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02007419
7420 pr_debug("%s: found a match\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 }
7422pp_not_found:
7423 /* If there was a hash table miss, create a new port. */
7424 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007425 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 goto fail_unlock;
7427
7428 /* In either case (hit or miss), make sure fastreuse is 1 only
7429 * if sk->sk_reuse is too (that is, if the caller requested
7430 * SO_REUSEADDR on this socket -sk-).
7431 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007432 if (hlist_empty(&pp->owner)) {
7433 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
7434 pp->fastreuse = 1;
7435 else
7436 pp->fastreuse = 0;
7437 } else if (pp->fastreuse &&
7438 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439 pp->fastreuse = 0;
7440
7441 /* We are set, so fill up all the data in the hash table
7442 * entry, tie the socket list information with the rest of the
7443 * sockets FIXME: Blurry, NPI (ipg).
7444 */
7445success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007447 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 sk_add_bind_node(sk, &pp->owner);
7449 sctp_sk(sk)->bind_hash = pp;
7450 }
7451 ret = 0;
7452
7453fail_unlock:
wangweidong3c8e43b2014-01-21 15:44:08 +08007454 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455
7456fail:
wangweidong79b91132014-01-21 15:44:07 +08007457 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 return ret;
7459}
7460
7461/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
7462 * port is requested.
7463 */
7464static int sctp_get_port(struct sock *sk, unsigned short snum)
7465{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 union sctp_addr addr;
7467 struct sctp_af *af = sctp_sk(sk)->pf->af;
7468
7469 /* Set up a dummy address struct from the sk. */
7470 af->from_sk(&addr, sk);
7471 addr.v4.sin_port = htons(snum);
7472
7473 /* Note: sk->sk_num gets filled in if ephemeral port request. */
Daniel Borkmann62208f12013-06-25 18:17:30 +02007474 return !!sctp_get_port_local(sk, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475}
7476
7477/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 * Move a socket to LISTENING state.
7479 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02007480static int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007482 struct sctp_sock *sp = sctp_sk(sk);
7483 struct sctp_endpoint *ep = sp->ep;
Herbert Xu5821c762016-01-24 21:20:12 +08007484 struct crypto_shash *tfm = NULL;
Neil Horman3c681982012-10-24 09:20:03 +00007485 char alg[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007486
7487 /* Allocate HMAC for generating cookie. */
Neil Horman3c681982012-10-24 09:20:03 +00007488 if (!sp->hmac && sp->sctp_hmac_alg) {
7489 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
Herbert Xu5821c762016-01-24 21:20:12 +08007490 tfm = crypto_alloc_shash(alg, 0, 0);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07007491 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00007492 net_info_ratelimited("failed to load transform for %s: %ld\n",
Neil Horman3c681982012-10-24 09:20:03 +00007493 sp->sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007494 return -ENOSYS;
7495 }
7496 sctp_sk(sk)->hmac = tfm;
7497 }
7498
7499 /*
7500 * If a bind() or sctp_bindx() is not called prior to a listen()
7501 * call that allows new associations to be accepted, the system
7502 * picks an ephemeral port and will choose an address set equivalent
7503 * to binding with a wildcard address.
7504 *
7505 * This is not currently spelled out in the SCTP sockets
7506 * extensions draft, but follows the practice as seen in TCP
7507 * sockets.
7508 *
7509 */
Yafang Shaocbabf462017-12-20 11:12:54 +08007510 inet_sk_set_state(sk, SCTP_SS_LISTENING);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007511 if (!ep->base.bind_addr.port) {
7512 if (sctp_autobind(sk))
7513 return -EAGAIN;
7514 } else {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007515 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08007516 inet_sk_set_state(sk, SCTP_SS_CLOSED);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007517 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 }
7519 }
7520
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007521 sk->sk_max_ack_backlog = backlog;
7522 sctp_hash_endpoint(ep);
7523 return 0;
7524}
7525
7526/*
7527 * 4.1.3 / 5.1.3 listen()
7528 *
7529 * By default, new associations are not accepted for UDP style sockets.
7530 * An application uses listen() to mark a socket as being able to
7531 * accept new associations.
7532 *
7533 * On TCP style sockets, applications use listen() to ready the SCTP
7534 * endpoint for accepting inbound associations.
7535 *
7536 * On both types of endpoints a backlog of '0' disables listening.
7537 *
7538 * Move a socket to LISTENING state.
7539 */
7540int sctp_inet_listen(struct socket *sock, int backlog)
7541{
7542 struct sock *sk = sock->sk;
7543 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7544 int err = -EINVAL;
7545
7546 if (unlikely(backlog < 0))
7547 return err;
7548
wangweidong048ed4b2014-01-21 15:44:11 +08007549 lock_sock(sk);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007550
7551 /* Peeled-off sockets are not allowed to listen(). */
7552 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
7553 goto out;
7554
7555 if (sock->state != SS_UNCONNECTED)
7556 goto out;
7557
Xin Long34b27892017-04-06 13:10:52 +08007558 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
7559 goto out;
7560
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007561 /* If backlog is zero, disable listening. */
7562 if (!backlog) {
7563 if (sctp_sstate(sk, CLOSED))
7564 goto out;
7565
7566 err = 0;
7567 sctp_unhash_endpoint(ep);
7568 sk->sk_state = SCTP_SS_CLOSED;
7569 if (sk->sk_reuse)
7570 sctp_sk(sk)->bind_hash->fastreuse = 1;
7571 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007572 }
7573
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007574 /* If we are already listening, just update the backlog */
7575 if (sctp_sstate(sk, LISTENING))
7576 sk->sk_max_ack_backlog = backlog;
7577 else {
7578 err = sctp_listen_start(sk, backlog);
7579 if (err)
7580 goto out;
7581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007583 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584out:
wangweidong048ed4b2014-01-21 15:44:11 +08007585 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587}
7588
7589/*
7590 * This function is done by modeling the current datagram_poll() and the
7591 * tcp_poll(). Note that, based on these implementations, we don't
7592 * lock the socket in this function, even though it seems that,
7593 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08007594 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595 * in this place. We assume that we don't need locks either until proven
7596 * otherwise.
7597 *
7598 * Another thing to note is that we include the Async I/O support
7599 * here, again, by modeling the current TCP/UDP code. We don't have
7600 * a good way to test with it yet.
7601 */
Al Viroade994f2017-07-03 00:01:49 -04007602__poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603{
7604 struct sock *sk = sock->sk;
7605 struct sctp_sock *sp = sctp_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -04007606 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607
Eric Dumazetaa395142010-04-20 13:03:51 +00007608 poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03007610 sock_rps_record_flow(sk);
7611
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612 /* A TCP-style listening socket becomes readable when the accept queue
7613 * is not empty.
7614 */
7615 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
7616 return (!list_empty(&sp->ep->asocs)) ?
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007617 (EPOLLIN | EPOLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618
7619 mask = 0;
7620
7621 /* Is there any exceptional events? */
7622 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007623 mask |= EPOLLERR |
7624 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
Davide Libenzif348d702006-03-25 03:07:39 -08007625 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007626 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007628 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629
7630 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00007631 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007632 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633
7634 /* The association is either gone or not ready. */
7635 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
7636 return mask;
7637
7638 /* Is it writable? */
7639 if (sctp_writeable(sk)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007640 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641 } else {
Eric Dumazet9cd3e072015-11-29 20:03:10 -08007642 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643 /*
7644 * Since the socket is not locked, the buffer
7645 * might be made available after the writeable check and
7646 * before the bit is set. This could cause a lost I/O
7647 * signal. tcp_poll() has a race breaker for this race
7648 * condition. Based on their implementation, we put
7649 * in the following code to cover it as well.
7650 */
7651 if (sctp_writeable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007652 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653 }
7654 return mask;
7655}
7656
7657/********************************************************************
7658 * 2nd Level Abstractions
7659 ********************************************************************/
7660
7661static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007662 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663{
7664 struct sctp_bind_bucket *pp;
7665
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08007666 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07007668 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669 pp->port = snum;
7670 pp->fastreuse = 0;
7671 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007672 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007673 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674 }
7675 return pp;
7676}
7677
7678/* Caller must hold hashbucket lock for this tb with local BH disabled */
7679static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
7680{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07007681 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007682 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683 kmem_cache_free(sctp_bucket_cachep, pp);
7684 SCTP_DBG_OBJCNT_DEC(bind_bucket);
7685 }
7686}
7687
7688/* Release this socket's reference to a local port. */
7689static inline void __sctp_put_port(struct sock *sk)
7690{
7691 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007692 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
7693 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694 struct sctp_bind_bucket *pp;
7695
wangweidong3c8e43b2014-01-21 15:44:08 +08007696 spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697 pp = sctp_sk(sk)->bind_hash;
7698 __sk_del_bind_node(sk);
7699 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007700 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007701 sctp_bucket_destroy(pp);
wangweidong3c8e43b2014-01-21 15:44:08 +08007702 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007703}
7704
7705void sctp_put_port(struct sock *sk)
7706{
wangweidong79b91132014-01-21 15:44:07 +08007707 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708 __sctp_put_port(sk);
wangweidong79b91132014-01-21 15:44:07 +08007709 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710}
7711
7712/*
7713 * The system picks an ephemeral port and choose an address set equivalent
7714 * to binding with a wildcard address.
7715 * One of those addresses will be the primary address for the association.
7716 * This automatically enables the multihoming capability of SCTP.
7717 */
7718static int sctp_autobind(struct sock *sk)
7719{
7720 union sctp_addr autoaddr;
7721 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08007722 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723
7724 /* Initialize a local sockaddr structure to INADDR_ANY. */
7725 af = sctp_sk(sk)->pf->af;
7726
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007727 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007728 af->inaddr_any(&autoaddr, port);
7729
7730 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
7731}
7732
7733/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
7734 *
7735 * From RFC 2292
7736 * 4.2 The cmsghdr Structure *
7737 *
7738 * When ancillary data is sent or received, any number of ancillary data
7739 * objects can be specified by the msg_control and msg_controllen members of
7740 * the msghdr structure, because each object is preceded by
7741 * a cmsghdr structure defining the object's length (the cmsg_len member).
7742 * Historically Berkeley-derived implementations have passed only one object
7743 * at a time, but this API allows multiple objects to be
7744 * passed in a single call to sendmsg() or recvmsg(). The following example
7745 * shows two ancillary data objects in a control buffer.
7746 *
7747 * |<--------------------------- msg_controllen -------------------------->|
7748 * | |
7749 *
7750 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
7751 *
7752 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
7753 * | | |
7754 *
7755 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
7756 *
7757 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
7758 * | | | | |
7759 *
7760 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7761 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
7762 *
7763 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
7764 *
7765 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7766 * ^
7767 * |
7768 *
7769 * msg_control
7770 * points here
7771 */
Xin Longa05437a2017-08-11 10:23:48 +08007772static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773{
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007774 struct msghdr *my_msg = (struct msghdr *)msg;
Xin Longa05437a2017-08-11 10:23:48 +08007775 struct cmsghdr *cmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776
Gu Zhengf95b4142014-12-11 11:22:04 +08007777 for_each_cmsghdr(cmsg, my_msg) {
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007778 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779 return -EINVAL;
7780
7781 /* Should we parse this header or ignore? */
7782 if (cmsg->cmsg_level != IPPROTO_SCTP)
7783 continue;
7784
7785 /* Strictly check lengths following example in SCM code. */
7786 switch (cmsg->cmsg_type) {
7787 case SCTP_INIT:
7788 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007789 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790 *
7791 * This cmsghdr structure provides information for
7792 * initializing new SCTP associations with sendmsg().
7793 * The SCTP_INITMSG socket option uses this same data
7794 * structure. This structure is not used for
7795 * recvmsg().
7796 *
7797 * cmsg_level cmsg_type cmsg_data[]
7798 * ------------ ------------ ----------------------
7799 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
7800 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007801 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007802 return -EINVAL;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007803
7804 cmsgs->init = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007805 break;
7806
7807 case SCTP_SNDRCV:
7808 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007809 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007810 *
7811 * This cmsghdr structure specifies SCTP options for
7812 * sendmsg() and describes SCTP header information
7813 * about a received message through recvmsg().
7814 *
7815 * cmsg_level cmsg_type cmsg_data[]
7816 * ------------ ------------ ----------------------
7817 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
7818 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007819 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820 return -EINVAL;
7821
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007822 cmsgs->srinfo = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007824 if (cmsgs->srinfo->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007825 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Long49102802018-03-05 20:44:20 +08007826 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
7827 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007828 return -EINVAL;
7829 break;
7830
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007831 case SCTP_SNDINFO:
7832 /* SCTP Socket API Extension
7833 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
7834 *
7835 * This cmsghdr structure specifies SCTP options for
7836 * sendmsg(). This structure and SCTP_RCVINFO replaces
7837 * SCTP_SNDRCV which has been deprecated.
7838 *
7839 * cmsg_level cmsg_type cmsg_data[]
7840 * ------------ ------------ ---------------------
7841 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
7842 */
7843 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
7844 return -EINVAL;
7845
7846 cmsgs->sinfo = CMSG_DATA(cmsg);
7847
7848 if (cmsgs->sinfo->snd_flags &
7849 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Long49102802018-03-05 20:44:20 +08007850 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
7851 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007852 return -EINVAL;
7853 break;
Xin Longed63afb2018-03-05 20:44:18 +08007854 case SCTP_PRINFO:
7855 /* SCTP Socket API Extension
7856 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
7857 *
7858 * This cmsghdr structure specifies SCTP options for sendmsg().
7859 *
7860 * cmsg_level cmsg_type cmsg_data[]
7861 * ------------ ------------ ---------------------
7862 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
7863 */
7864 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
7865 return -EINVAL;
7866
7867 cmsgs->prinfo = CMSG_DATA(cmsg);
7868 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
7869 return -EINVAL;
7870
7871 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
7872 cmsgs->prinfo->pr_value = 0;
7873 break;
Xin Long2c0dbaa2018-03-05 20:44:19 +08007874 case SCTP_DSTADDRV4:
7875 case SCTP_DSTADDRV6:
7876 /* SCTP Socket API Extension
7877 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
7878 *
7879 * This cmsghdr structure specifies SCTP options for sendmsg().
7880 *
7881 * cmsg_level cmsg_type cmsg_data[]
7882 * ------------ ------------ ---------------------
7883 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
7884 * ------------ ------------ ---------------------
7885 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
7886 */
7887 cmsgs->addrs_msg = my_msg;
7888 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 default:
7890 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892 }
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007893
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894 return 0;
7895}
7896
7897/*
7898 * Wait for a packet..
7899 * Note: This function is the same function as in core/datagram.c
7900 * with a few modifications to make lksctp work.
7901 */
wangweidong26ac8e52013-12-23 12:16:51 +08007902static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903{
7904 int error;
7905 DEFINE_WAIT(wait);
7906
Eric Dumazetaa395142010-04-20 13:03:51 +00007907 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908
7909 /* Socket errors? */
7910 error = sock_error(sk);
7911 if (error)
7912 goto out;
7913
7914 if (!skb_queue_empty(&sk->sk_receive_queue))
7915 goto ready;
7916
7917 /* Socket shut down? */
7918 if (sk->sk_shutdown & RCV_SHUTDOWN)
7919 goto out;
7920
7921 /* Sequenced packets can come disconnected. If so we report the
7922 * problem.
7923 */
7924 error = -ENOTCONN;
7925
7926 /* Is there a good reason to think that we may receive some data? */
7927 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
7928 goto out;
7929
7930 /* Handle signals. */
7931 if (signal_pending(current))
7932 goto interrupted;
7933
7934 /* Let another process have a go. Since we are going to sleep
7935 * anyway. Note: This may cause odd behaviors if the message
7936 * does not fit in the user's buffer, but this seems to be the
7937 * only way to honor MSG_DONTWAIT realistically.
7938 */
wangweidong048ed4b2014-01-21 15:44:11 +08007939 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940 *timeo_p = schedule_timeout(*timeo_p);
wangweidong048ed4b2014-01-21 15:44:11 +08007941 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007942
7943ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00007944 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945 return 0;
7946
7947interrupted:
7948 error = sock_intr_errno(*timeo_p);
7949
7950out:
Eric Dumazetaa395142010-04-20 13:03:51 +00007951 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952 *err = error;
7953 return error;
7954}
7955
7956/* Receive a datagram.
7957 * Note: This is pretty much the same routine as in core/datagram.c
7958 * with a few changes to make lksctp work.
7959 */
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007960struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
7961 int noblock, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007962{
7963 int error;
7964 struct sk_buff *skb;
7965 long timeo;
7966
Linus Torvalds1da177e2005-04-16 15:20:36 -07007967 timeo = sock_rcvtimeo(sk, noblock);
7968
Daniel Borkmannbb333812013-06-28 19:49:40 +02007969 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
7970 MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007971
7972 do {
7973 /* Again only user level code calls this function,
7974 * so nothing interrupt level
7975 * will suddenly eat the receive_queue.
7976 *
7977 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00007978 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979 */
7980 if (flags & MSG_PEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007981 skb = skb_peek(&sk->sk_receive_queue);
7982 if (skb)
Reshetova, Elena63354792017-06-30 13:07:58 +03007983 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007984 } else {
Marcelo Ricardo Leitner311b2172016-04-13 19:12:29 -03007985 skb = __skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986 }
7987
7988 if (skb)
7989 return skb;
7990
Neil Horman6736dc32005-12-02 20:30:06 -08007991 /* Caller is allowed not to check sk->sk_err before calling. */
7992 error = sock_error(sk);
7993 if (error)
7994 goto no_packet;
7995
Linus Torvalds1da177e2005-04-16 15:20:36 -07007996 if (sk->sk_shutdown & RCV_SHUTDOWN)
7997 break;
7998
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07007999 if (sk_can_busy_loop(sk)) {
8000 sk_busy_loop(sk, noblock);
8001
8002 if (!skb_queue_empty(&sk->sk_receive_queue))
8003 continue;
8004 }
Neil Horman8465a5f2014-04-17 15:26:51 -04008005
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006 /* User doesn't want to wait. */
8007 error = -EAGAIN;
8008 if (!timeo)
8009 goto no_packet;
8010 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8011
8012 return NULL;
8013
8014no_packet:
8015 *err = error;
8016 return NULL;
8017}
8018
8019/* If sndbuf has changed, wake up per association sndbuf waiters. */
8020static void __sctp_write_space(struct sctp_association *asoc)
8021{
8022 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023
Eric Dumazetceb5d582015-11-29 20:03:11 -08008024 if (sctp_wspace(asoc) <= 0)
8025 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008026
Eric Dumazetceb5d582015-11-29 20:03:11 -08008027 if (waitqueue_active(&asoc->wait))
8028 wake_up_interruptible(&asoc->wait);
Eric Dumazeteaefd112011-02-18 03:26:36 +00008029
Eric Dumazetceb5d582015-11-29 20:03:11 -08008030 if (sctp_writeable(sk)) {
8031 struct socket_wq *wq;
8032
8033 rcu_read_lock();
8034 wq = rcu_dereference(sk->sk_wq);
8035 if (wq) {
8036 if (waitqueue_active(&wq->wait))
8037 wake_up_interruptible(&wq->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008038
8039 /* Note that we try to include the Async I/O support
8040 * here by modeling from the current TCP/UDP code.
8041 * We have not tested with it yet.
8042 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00008043 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Eric Dumazetceb5d582015-11-29 20:03:11 -08008044 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08008046 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008047 }
8048}
8049
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008050static void sctp_wake_up_waiters(struct sock *sk,
8051 struct sctp_association *asoc)
8052{
8053 struct sctp_association *tmp = asoc;
8054
8055 /* We do accounting for the sndbuf space per association,
8056 * so we only need to wake our own association.
8057 */
8058 if (asoc->ep->sndbuf_policy)
8059 return __sctp_write_space(asoc);
8060
Daniel Borkmann1e1cdf82014-04-09 16:10:20 +02008061 /* If association goes down and is just flushing its
8062 * outq, then just normally notify others.
8063 */
8064 if (asoc->base.dead)
8065 return sctp_write_space(sk);
8066
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008067 /* Accounting for the sndbuf space is per socket, so we
8068 * need to wake up others, try to be fair and in case of
8069 * other associations, let them have a go first instead
8070 * of just doing a sctp_write_space() call.
8071 *
8072 * Note that we reach sctp_wake_up_waiters() only when
8073 * associations free up queued chunks, thus we are under
8074 * lock and the list of associations on a socket is
8075 * guaranteed not to change.
8076 */
8077 for (tmp = list_next_entry(tmp, asocs); 1;
8078 tmp = list_next_entry(tmp, asocs)) {
8079 /* Manually skip the head element. */
8080 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8081 continue;
8082 /* Wake up association. */
8083 __sctp_write_space(tmp);
8084 /* We've reached the end. */
8085 if (tmp == asoc)
8086 break;
8087 }
8088}
8089
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090/* Do accounting for the sndbuf space.
8091 * Decrement the used sndbuf space of the corresponding association by the
8092 * data size which was just transmitted(freed).
8093 */
8094static void sctp_wfree(struct sk_buff *skb)
8095{
Daniel Borkmannf869c912014-11-20 01:54:48 +01008096 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8097 struct sctp_association *asoc = chunk->asoc;
8098 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099
Neil Horman4eb701d2005-04-28 12:02:04 -07008100 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
8101 sizeof(struct sk_buff) +
8102 sizeof(struct sctp_chunk);
8103
Reshetova, Elena14afee42017-06-30 13:08:00 +03008104 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
Neil Horman4eb701d2005-04-28 12:02:04 -07008105
Neil Horman4d93df02007-08-15 16:07:44 -07008106 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008107 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
Neil Horman4d93df02007-08-15 16:07:44 -07008108 */
8109 sk->sk_wmem_queued -= skb->truesize;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008110 sk_mem_uncharge(sk, skb->truesize);
Neil Horman4d93df02007-08-15 16:07:44 -07008111
Neil Horman4eb701d2005-04-28 12:02:04 -07008112 sock_wfree(skb);
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008113 sctp_wake_up_waiters(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114
8115 sctp_association_put(asoc);
8116}
8117
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008118/* Do accounting for the receive space on the socket.
8119 * Accounting for the association is done in ulpevent.c
8120 * We set this as a destructor for the cloned data skbs so that
8121 * accounting is done at the correct time.
8122 */
8123void sctp_sock_rfree(struct sk_buff *skb)
8124{
8125 struct sock *sk = skb->sk;
8126 struct sctp_ulpevent *event = sctp_skb2event(skb);
8127
8128 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07008129
8130 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008131 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07008132 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008133 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008134}
8135
8136
Linus Torvalds1da177e2005-04-16 15:20:36 -07008137/* Helper function to wait for space in the sndbuf. */
8138static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +08008139 size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008140{
8141 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142 long current_timeo = *timeo_p;
8143 DEFINE_WAIT(wait);
Xin Longa0ff6602018-01-15 17:01:36 +08008144 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145
Daniel Borkmannbb333812013-06-28 19:49:40 +02008146 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8147 *timeo_p, msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008148
8149 /* Increment the association's refcnt. */
8150 sctp_association_hold(asoc);
8151
8152 /* Wait on the association specific sndbuf space. */
8153 for (;;) {
8154 prepare_to_wait_exclusive(&asoc->wait, &wait,
8155 TASK_INTERRUPTIBLE);
Xin Longca3af4d2017-11-15 16:55:54 +08008156 if (asoc->base.dead)
8157 goto do_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008158 if (!*timeo_p)
8159 goto do_nonblock;
Xin Longca3af4d2017-11-15 16:55:54 +08008160 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 goto do_error;
8162 if (signal_pending(current))
8163 goto do_interrupted;
8164 if (msg_len <= sctp_wspace(asoc))
8165 break;
8166
8167 /* Let another process have a go. Since we are going
8168 * to sleep anyway.
8169 */
wangweidong048ed4b2014-01-21 15:44:11 +08008170 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008172 lock_sock(sk);
Xin Longa0ff6602018-01-15 17:01:36 +08008173 if (sk != asoc->base.sk)
8174 goto do_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175
8176 *timeo_p = current_timeo;
8177 }
8178
8179out:
8180 finish_wait(&asoc->wait, &wait);
8181
8182 /* Release the association's refcnt. */
8183 sctp_association_put(asoc);
8184
8185 return err;
8186
Xin Longca3af4d2017-11-15 16:55:54 +08008187do_dead:
8188 err = -ESRCH;
8189 goto out;
8190
Linus Torvalds1da177e2005-04-16 15:20:36 -07008191do_error:
8192 err = -EPIPE;
8193 goto out;
8194
8195do_interrupted:
8196 err = sock_intr_errno(*timeo_p);
8197 goto out;
8198
8199do_nonblock:
8200 err = -EAGAIN;
8201 goto out;
8202}
8203
David S. Miller676d2362014-04-11 16:15:36 -04008204void sctp_data_ready(struct sock *sk)
Wei Yongjun561b1732010-04-28 08:47:18 +00008205{
David S. Miller7ef52732010-05-02 21:43:40 -07008206 struct socket_wq *wq;
8207
8208 rcu_read_lock();
8209 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08008210 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008211 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8212 EPOLLRDNORM | EPOLLRDBAND);
Wei Yongjun561b1732010-04-28 08:47:18 +00008213 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07008214 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00008215}
8216
Linus Torvalds1da177e2005-04-16 15:20:36 -07008217/* If socket sndbuf has changed, wake up all per association waiters. */
8218void sctp_write_space(struct sock *sk)
8219{
8220 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221
8222 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07008223 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224 __sctp_write_space(asoc);
8225 }
8226}
8227
8228/* Is there any sndbuf space available on the socket?
8229 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08008230 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07008231 * associations on the same socket. For a UDP-style socket with
8232 * multiple associations, it is possible for it to be "unwriteable"
8233 * prematurely. I assume that this is acceptable because
8234 * a premature "unwriteable" is better than an accidental "writeable" which
8235 * would cause an unwanted block under certain circumstances. For the 1-1
8236 * UDP-style sockets or TCP-style sockets, this code should work.
8237 * - Daisy
8238 */
8239static int sctp_writeable(struct sock *sk)
8240{
8241 int amt = 0;
8242
Eric Dumazet31e6d362009-06-17 19:05:41 -07008243 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244 if (amt < 0)
8245 amt = 0;
8246 return amt;
8247}
8248
8249/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8250 * returns immediately with EINPROGRESS.
8251 */
8252static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8253{
8254 struct sock *sk = asoc->base.sk;
8255 int err = 0;
8256 long current_timeo = *timeo_p;
8257 DEFINE_WAIT(wait);
8258
Daniel Borkmannbb333812013-06-28 19:49:40 +02008259 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008260
8261 /* Increment the association's refcnt. */
8262 sctp_association_hold(asoc);
8263
8264 for (;;) {
8265 prepare_to_wait_exclusive(&asoc->wait, &wait,
8266 TASK_INTERRUPTIBLE);
8267 if (!*timeo_p)
8268 goto do_nonblock;
8269 if (sk->sk_shutdown & RCV_SHUTDOWN)
8270 break;
8271 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
8272 asoc->base.dead)
8273 goto do_error;
8274 if (signal_pending(current))
8275 goto do_interrupted;
8276
8277 if (sctp_state(asoc, ESTABLISHED))
8278 break;
8279
8280 /* Let another process have a go. Since we are going
8281 * to sleep anyway.
8282 */
wangweidong048ed4b2014-01-21 15:44:11 +08008283 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008284 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008285 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008286
8287 *timeo_p = current_timeo;
8288 }
8289
8290out:
8291 finish_wait(&asoc->wait, &wait);
8292
8293 /* Release the association's refcnt. */
8294 sctp_association_put(asoc);
8295
8296 return err;
8297
8298do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08008299 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008300 err = -ETIMEDOUT;
8301 else
8302 err = -ECONNREFUSED;
8303 goto out;
8304
8305do_interrupted:
8306 err = sock_intr_errno(*timeo_p);
8307 goto out;
8308
8309do_nonblock:
8310 err = -EINPROGRESS;
8311 goto out;
8312}
8313
8314static int sctp_wait_for_accept(struct sock *sk, long timeo)
8315{
8316 struct sctp_endpoint *ep;
8317 int err = 0;
8318 DEFINE_WAIT(wait);
8319
8320 ep = sctp_sk(sk)->ep;
8321
8322
8323 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00008324 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008325 TASK_INTERRUPTIBLE);
8326
8327 if (list_empty(&ep->asocs)) {
wangweidong048ed4b2014-01-21 15:44:11 +08008328 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008329 timeo = schedule_timeout(timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008330 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008331 }
8332
8333 err = -EINVAL;
8334 if (!sctp_sstate(sk, LISTENING))
8335 break;
8336
8337 err = 0;
8338 if (!list_empty(&ep->asocs))
8339 break;
8340
8341 err = sock_intr_errno(timeo);
8342 if (signal_pending(current))
8343 break;
8344
8345 err = -EAGAIN;
8346 if (!timeo)
8347 break;
8348 }
8349
Eric Dumazetaa395142010-04-20 13:03:51 +00008350 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008351
8352 return err;
8353}
8354
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02008355static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008356{
8357 DEFINE_WAIT(wait);
8358
8359 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00008360 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008361 if (list_empty(&sctp_sk(sk)->ep->asocs))
8362 break;
wangweidong048ed4b2014-01-21 15:44:11 +08008363 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008364 timeout = schedule_timeout(timeout);
wangweidong048ed4b2014-01-21 15:44:11 +08008365 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008366 } while (!signal_pending(current) && timeout);
8367
Eric Dumazetaa395142010-04-20 13:03:51 +00008368 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008369}
8370
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008371static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
8372{
8373 struct sk_buff *frag;
8374
8375 if (!skb->data_len)
8376 goto done;
8377
8378 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07008379 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008380 sctp_skb_set_owner_r_frag(frag, sk);
8381
8382done:
8383 sctp_skb_set_owner_r(skb, sk);
8384}
8385
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008386void sctp_copy_sock(struct sock *newsk, struct sock *sk,
8387 struct sctp_association *asoc)
8388{
8389 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08008390 struct inet_sock *newinet;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008391
8392 newsk->sk_type = sk->sk_type;
8393 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
8394 newsk->sk_flags = sk->sk_flags;
Marcelo Ricardo Leitner50a5ffb2015-12-04 15:14:05 -02008395 newsk->sk_tsflags = sk->sk_tsflags;
Tom Herbert28448b82014-05-23 08:47:19 -07008396 newsk->sk_no_check_tx = sk->sk_no_check_tx;
8397 newsk->sk_no_check_rx = sk->sk_no_check_rx;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008398 newsk->sk_reuse = sk->sk_reuse;
8399
8400 newsk->sk_shutdown = sk->sk_shutdown;
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02008401 newsk->sk_destruct = sctp_destruct_sock;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008402 newsk->sk_family = sk->sk_family;
8403 newsk->sk_protocol = IPPROTO_SCTP;
8404 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
8405 newsk->sk_sndbuf = sk->sk_sndbuf;
8406 newsk->sk_rcvbuf = sk->sk_rcvbuf;
8407 newsk->sk_lingertime = sk->sk_lingertime;
8408 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
8409 newsk->sk_sndtimeo = sk->sk_sndtimeo;
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03008410 newsk->sk_rxhash = sk->sk_rxhash;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008411
8412 newinet = inet_sk(newsk);
8413
8414 /* Initialize sk's sport, dport, rcv_saddr and daddr for
8415 * getsockname() and getpeername()
8416 */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008417 newinet->inet_sport = inet->inet_sport;
8418 newinet->inet_saddr = inet->inet_saddr;
8419 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
8420 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008421 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008422 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008423
8424 newinet->uc_ttl = inet->uc_ttl;
8425 newinet->mc_loop = 1;
8426 newinet->mc_ttl = 1;
8427 newinet->mc_index = 0;
8428 newinet->mc_list = NULL;
Marcelo Ricardo Leitner01ce63c2015-12-04 15:14:04 -02008429
8430 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
8431 net_enable_timestamp();
Marcelo Ricardo Leitner3538a5c2015-12-23 16:44:09 -02008432
8433 security_sk_clone(sk, newsk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008434}
8435
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008436static inline void sctp_copy_descendant(struct sock *sk_to,
8437 const struct sock *sk_from)
8438{
8439 int ancestor_size = sizeof(struct inet_sock) +
8440 sizeof(struct sctp_sock) -
8441 offsetof(struct sctp_sock, auto_asconf_list);
8442
8443 if (sk_from->sk_family == PF_INET6)
8444 ancestor_size += sizeof(struct ipv6_pinfo);
8445
8446 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
8447}
8448
Linus Torvalds1da177e2005-04-16 15:20:36 -07008449/* Populate the fields of the newsk from the oldsk and migrate the assoc
8450 * and its messages to the newsk.
8451 */
8452static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
8453 struct sctp_association *assoc,
Xin Longb7ef2612017-08-11 10:23:50 +08008454 enum sctp_socket_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008455{
8456 struct sctp_sock *oldsp = sctp_sk(oldsk);
8457 struct sctp_sock *newsp = sctp_sk(newsk);
8458 struct sctp_bind_bucket *pp; /* hash list port iterator */
8459 struct sctp_endpoint *newep = newsp->ep;
8460 struct sk_buff *skb, *tmp;
8461 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08008462 struct sctp_bind_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463
8464 /* Migrate socket buffer sizes and all the socket level options to the
8465 * new socket.
8466 */
8467 newsk->sk_sndbuf = oldsk->sk_sndbuf;
8468 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
8469 /* Brute force copy old sctp opt. */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008470 sctp_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471
8472 /* Restore the ep value that was overwritten with the above structure
8473 * copy.
8474 */
8475 newsp->ep = newep;
8476 newsp->hmac = NULL;
8477
8478 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008479 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
8480 inet_sk(oldsk)->inet_num)];
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008481 spin_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008482 pp = sctp_sk(oldsk)->bind_hash;
8483 sk_add_bind_node(newsk, &pp->owner);
8484 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008485 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008486 spin_unlock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008487
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008488 /* Copy the bind_addr list from the original endpoint to the new
8489 * endpoint so that we can handle restarts properly
8490 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08008491 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
8492 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008493
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494 /* Move any messages in the old socket's receive queue that are for the
8495 * peeled off association to the new socket's receive queue.
8496 */
8497 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
8498 event = sctp_skb2event(skb);
8499 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008500 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008502 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503 }
8504 }
8505
8506 /* Clean up any messages pending delivery due to partial
8507 * delivery. Three cases:
8508 * 1) No partial deliver; no work.
8509 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
8510 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
8511 */
8512 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008513 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008515 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 struct sk_buff_head *queue;
8517
8518 /* Decide which queue to move pd_lobby skbs to. */
8519 if (assoc->ulpq.pd_mode) {
8520 queue = &newsp->pd_lobby;
8521 } else
8522 queue = &newsk->sk_receive_queue;
8523
8524 /* Walk through the pd_lobby, looking for skbs that
8525 * need moved to the new socket.
8526 */
8527 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
8528 event = sctp_skb2event(skb);
8529 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008530 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008532 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533 }
8534 }
8535
8536 /* Clear up any skbs waiting for the partial
8537 * delivery to finish.
8538 */
8539 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008540 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541
8542 }
8543
Xin Long13228232017-12-08 21:04:09 +08008544 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008545
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546 /* Set the type of socket to indicate that it is peeled off from the
8547 * original UDP-style socket or created with the accept() call on a
8548 * TCP-style socket..
8549 */
8550 newsp->type = type;
8551
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008552 /* Mark the new socket "in-use" by the user so that any packets
8553 * that may arrive on the association after we've moved it are
8554 * queued to the backlog. This prevents a potential race between
8555 * backlog processing on the old socket and new-packet processing
8556 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07008557 *
8558 * The caller has just allocated newsk so we can guarantee that other
8559 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008560 */
Zach Brown5131a182007-06-22 15:14:46 -07008561 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Xin Longd04adf12017-10-28 02:13:29 +08008562 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008563 sctp_assoc_migrate(assoc, newsk);
Xin Longd04adf12017-10-28 02:13:29 +08008564 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565
8566 /* If the association on the newsk is already closed before accept()
8567 * is called, set RCV_SHUTDOWN flag.
8568 */
Xin Longd46e4162016-06-09 22:48:18 +08008569 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08008570 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571 newsk->sk_shutdown |= RCV_SHUTDOWN;
Xin Longd46e4162016-06-09 22:48:18 +08008572 } else {
Yafang Shaocbabf462017-12-20 11:12:54 +08008573 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
Xin Longd46e4162016-06-09 22:48:18 +08008574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008575
wangweidong048ed4b2014-01-21 15:44:11 +08008576 release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008577}
8578
Neil Horman4d93df02007-08-15 16:07:44 -07008579
Linus Torvalds1da177e2005-04-16 15:20:36 -07008580/* This proto struct describes the ULP interface for SCTP. */
8581struct proto sctp_prot = {
8582 .name = "SCTP",
8583 .owner = THIS_MODULE,
8584 .close = sctp_close,
8585 .connect = sctp_connect,
8586 .disconnect = sctp_disconnect,
8587 .accept = sctp_accept,
8588 .ioctl = sctp_ioctl,
8589 .init = sctp_init_sock,
8590 .destroy = sctp_destroy_sock,
8591 .shutdown = sctp_shutdown,
8592 .setsockopt = sctp_setsockopt,
8593 .getsockopt = sctp_getsockopt,
8594 .sendmsg = sctp_sendmsg,
8595 .recvmsg = sctp_recvmsg,
8596 .bind = sctp_bind,
8597 .backlog_rcv = sctp_backlog_rcv,
8598 .hash = sctp_hash,
8599 .unhash = sctp_unhash,
8600 .get_port = sctp_get_port,
8601 .obj_size = sizeof(struct sctp_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07008602 .useroffset = offsetof(struct sctp_sock, subscribe),
8603 .usersize = offsetof(struct sctp_sock, initmsg) -
8604 offsetof(struct sctp_sock, subscribe) +
8605 sizeof_field(struct sctp_sock, initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07008606 .sysctl_mem = sysctl_sctp_mem,
8607 .sysctl_rmem = sysctl_sctp_rmem,
8608 .sysctl_wmem = sysctl_sctp_wmem,
8609 .memory_pressure = &sctp_memory_pressure,
8610 .enter_memory_pressure = sctp_enter_memory_pressure,
8611 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008612 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613};
8614
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008615#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08008616
Eric Dumazet602dd622015-12-01 07:20:07 -08008617#include <net/transp_v6.h>
8618static void sctp_v6_destroy_sock(struct sock *sk)
8619{
8620 sctp_destroy_sock(sk);
8621 inet6_destroy_sock(sk);
8622}
8623
Linus Torvalds1da177e2005-04-16 15:20:36 -07008624struct proto sctpv6_prot = {
8625 .name = "SCTPv6",
8626 .owner = THIS_MODULE,
8627 .close = sctp_close,
8628 .connect = sctp_connect,
8629 .disconnect = sctp_disconnect,
8630 .accept = sctp_accept,
8631 .ioctl = sctp_ioctl,
8632 .init = sctp_init_sock,
Eric Dumazet602dd622015-12-01 07:20:07 -08008633 .destroy = sctp_v6_destroy_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008634 .shutdown = sctp_shutdown,
8635 .setsockopt = sctp_setsockopt,
8636 .getsockopt = sctp_getsockopt,
8637 .sendmsg = sctp_sendmsg,
8638 .recvmsg = sctp_recvmsg,
8639 .bind = sctp_bind,
8640 .backlog_rcv = sctp_backlog_rcv,
8641 .hash = sctp_hash,
8642 .unhash = sctp_unhash,
8643 .get_port = sctp_get_port,
8644 .obj_size = sizeof(struct sctp6_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07008645 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
8646 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
8647 offsetof(struct sctp6_sock, sctp.subscribe) +
8648 sizeof_field(struct sctp6_sock, sctp.initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07008649 .sysctl_mem = sysctl_sctp_mem,
8650 .sysctl_rmem = sysctl_sctp_rmem,
8651 .sysctl_wmem = sysctl_sctp_wmem,
8652 .memory_pressure = &sctp_memory_pressure,
8653 .enter_memory_pressure = sctp_enter_memory_pressure,
8654 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008655 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008656};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008657#endif /* IS_ENABLED(CONFIG_IPV6) */