blob: 58bb55dce8f68cd5501381608e2524da9708cffa [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 Long2bfd80f2018-03-01 23:05:11 +08001609static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1610 struct sctp_cmsgs *cmsgs,
1611 union sctp_addr *daddr,
1612 struct sctp_transport **tp)
1613{
1614 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1615 struct net *net = sock_net(sk);
1616 struct sctp_association *asoc;
1617 enum sctp_scope scope;
1618 int err = -EINVAL;
1619
1620 *tp = NULL;
1621
1622 if (sflags & (SCTP_EOF | SCTP_ABORT))
1623 return -EINVAL;
1624
1625 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1626 sctp_sstate(sk, CLOSING)))
1627 return -EADDRNOTAVAIL;
1628
1629 if (sctp_endpoint_is_peeled_off(ep, daddr))
1630 return -EADDRNOTAVAIL;
1631
1632 if (!ep->base.bind_addr.port) {
1633 if (sctp_autobind(sk))
1634 return -EAGAIN;
1635 } else {
1636 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1637 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1638 return -EACCES;
1639 }
1640
1641 scope = sctp_scope(daddr);
1642
1643 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1644 if (!asoc)
1645 return -ENOMEM;
1646
1647 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1648 err = -ENOMEM;
1649 goto free;
1650 }
1651
1652 if (cmsgs->init) {
1653 struct sctp_initmsg *init = cmsgs->init;
1654
1655 if (init->sinit_num_ostreams) {
1656 __u16 outcnt = init->sinit_num_ostreams;
1657
1658 asoc->c.sinit_num_ostreams = outcnt;
1659 /* outcnt has been changed, need to re-init stream */
1660 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1661 GFP_KERNEL);
1662 if (err)
1663 goto free;
1664 }
1665
1666 if (init->sinit_max_instreams)
1667 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1668
1669 if (init->sinit_max_attempts)
1670 asoc->max_init_attempts = init->sinit_max_attempts;
1671
1672 if (init->sinit_max_init_timeo)
1673 asoc->max_init_timeo =
1674 msecs_to_jiffies(init->sinit_max_init_timeo);
1675 }
1676
1677 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1678 if (!*tp) {
1679 err = -ENOMEM;
1680 goto free;
1681 }
1682
1683 return 0;
1684
1685free:
1686 sctp_association_free(asoc);
1687 return err;
1688}
1689
Xin Longf84af332018-03-01 23:05:10 +08001690static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1691 struct msghdr *msg, size_t msg_len,
1692 struct sctp_transport *transport,
1693 struct sctp_sndrcvinfo *sinfo)
1694{
1695 struct sock *sk = asoc->base.sk;
1696 struct net *net = sock_net(sk);
1697 struct sctp_datamsg *datamsg;
1698 bool wait_connect = false;
1699 struct sctp_chunk *chunk;
1700 long timeo;
1701 int err;
1702
1703 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1704 err = -EINVAL;
1705 goto err;
1706 }
1707
1708 if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) {
1709 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1710 if (err)
1711 goto err;
1712 }
1713
1714 if (sctp_sk(sk)->disable_fragments && msg_len > asoc->frag_point) {
1715 err = -EMSGSIZE;
1716 goto err;
1717 }
1718
1719 if (sctp_state(asoc, CLOSED)) {
1720 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1721 if (err)
1722 goto err;
1723
1724 if (sctp_sk(sk)->strm_interleave) {
1725 timeo = sock_sndtimeo(sk, 0);
1726 err = sctp_wait_for_connect(asoc, &timeo);
1727 if (err)
1728 goto err;
1729 } else {
1730 wait_connect = true;
1731 }
1732
1733 pr_debug("%s: we associated primitively\n", __func__);
1734 }
1735
1736 if (asoc->pmtu_pending)
1737 sctp_assoc_pending_pmtu(asoc);
1738
1739 if (sctp_wspace(asoc) < msg_len)
1740 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1741
1742 if (!sctp_wspace(asoc)) {
1743 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1744 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1745 if (err)
1746 goto err;
1747 }
1748
1749 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1750 if (IS_ERR(datamsg)) {
1751 err = PTR_ERR(datamsg);
1752 goto err;
1753 }
1754
1755 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1756
1757 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1758 sctp_chunk_hold(chunk);
1759 sctp_set_owner_w(chunk);
1760 chunk->transport = transport;
1761 }
1762
1763 err = sctp_primitive_SEND(net, asoc, datamsg);
1764 if (err) {
1765 sctp_datamsg_free(datamsg);
1766 goto err;
1767 }
1768
1769 pr_debug("%s: we sent primitively\n", __func__);
1770
1771 sctp_datamsg_put(datamsg);
1772
1773 if (unlikely(wait_connect)) {
1774 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1775 sctp_wait_for_connect(asoc, &timeo);
1776 }
1777
1778 err = msg_len;
1779
1780err:
1781 return err;
1782}
1783
Ying Xue1b784142015-03-02 15:37:48 +08001784static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001786 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 struct sctp_sock *sp;
1788 struct sctp_endpoint *ep;
wangweidongcb3f8372013-12-23 12:16:50 +08001789 struct sctp_association *new_asoc = NULL, *asoc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 struct sctp_transport *transport, *chunk_tp;
1791 struct sctp_chunk *chunk;
Al Virodce116a2006-11-20 17:25:15 -08001792 union sctp_addr to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 struct sockaddr *msg_name = NULL;
Joe Perches517aa0b2011-05-12 11:27:20 +00001794 struct sctp_sndrcvinfo default_sinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 struct sctp_sndrcvinfo *sinfo;
1796 struct sctp_initmsg *sinit;
1797 sctp_assoc_t associd = 0;
Xin Longa05437a2017-08-11 10:23:48 +08001798 struct sctp_cmsgs cmsgs = { NULL };
Xin Longf84af332018-03-01 23:05:10 +08001799 bool fill_sinfo_ttl = false;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001800 __u16 sinfo_flags = 0;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001801 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 err = 0;
1804 sp = sctp_sk(sk);
1805 ep = sp->ep;
1806
Daniel Borkmannbb333812013-06-28 19:49:40 +02001807 pr_debug("%s: sk:%p, msg:%p, msg_len:%zu ep:%p\n", __func__, sk,
1808 msg, msg_len, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 /* We cannot send a message over a TCP-style listening socket. */
1811 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1812 err = -EPIPE;
1813 goto out_nounlock;
1814 }
1815
1816 /* Parse out the SCTP CMSGs. */
1817 err = sctp_msghdr_parse(msg, &cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (err) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001819 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 goto out_nounlock;
1821 }
1822
1823 /* Fetch the destination address for this packet. This
1824 * address only selects the association--it is not necessarily
1825 * the address we will send to.
1826 * For a peeled-off socket, msg_name is ignored.
1827 */
1828 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1829 int msg_namelen = msg->msg_namelen;
1830
1831 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1832 msg_namelen);
1833 if (err)
1834 return err;
1835
1836 if (msg_namelen > sizeof(to))
1837 msg_namelen = sizeof(to);
1838 memcpy(&to, msg->msg_name, msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 msg_name = msg->msg_name;
1840 }
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 sinit = cmsgs.init;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001843 if (cmsgs.sinfo != NULL) {
1844 memset(&default_sinfo, 0, sizeof(default_sinfo));
1845 default_sinfo.sinfo_stream = cmsgs.sinfo->snd_sid;
1846 default_sinfo.sinfo_flags = cmsgs.sinfo->snd_flags;
1847 default_sinfo.sinfo_ppid = cmsgs.sinfo->snd_ppid;
1848 default_sinfo.sinfo_context = cmsgs.sinfo->snd_context;
1849 default_sinfo.sinfo_assoc_id = cmsgs.sinfo->snd_assoc_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001851 sinfo = &default_sinfo;
1852 fill_sinfo_ttl = true;
1853 } else {
1854 sinfo = cmsgs.srinfo;
1855 }
1856 /* Did the user specify SNDINFO/SNDRCVINFO? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (sinfo) {
1858 sinfo_flags = sinfo->sinfo_flags;
1859 associd = sinfo->sinfo_assoc_id;
1860 }
1861
Daniel Borkmannbb333812013-06-28 19:49:40 +02001862 pr_debug("%s: msg_len:%zu, sinfo_flags:0x%x\n", __func__,
1863 msg_len, sinfo_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001865 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1866 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 err = -EINVAL;
1868 goto out_nounlock;
1869 }
1870
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001871 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1872 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1873 * If SCTP_ABORT is set, the message length could be non zero with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 * the msg_iov set to the user abort reason.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001875 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001876 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1877 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 err = -EINVAL;
1879 goto out_nounlock;
1880 }
1881
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001882 /* If SCTP_ADDR_OVER is set, there must be an address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 * specified in msg_name.
1884 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001885 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 err = -EINVAL;
1887 goto out_nounlock;
1888 }
1889
1890 transport = NULL;
1891
Daniel Borkmannbb333812013-06-28 19:49:40 +02001892 pr_debug("%s: about to look up association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
wangweidong048ed4b2014-01-21 15:44:11 +08001894 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 /* If a msg_name has been specified, assume this is to be used. */
1897 if (msg_name) {
1898 /* Look for a matching association on the endpoint. */
Al Virodce116a2006-11-20 17:25:15 -08001899 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 } else {
1901 asoc = sctp_id2assoc(sk, associd);
1902 if (!asoc) {
1903 err = -EPIPE;
1904 goto out_unlock;
1905 }
1906 }
1907
1908 if (asoc) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001909 pr_debug("%s: just looked up association:%p\n", __func__, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1912 * socket that has an association in CLOSED state. This can
1913 * happen when an accepted socket has an association that is
1914 * already CLOSED.
1915 */
1916 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1917 err = -EPIPE;
1918 goto out_unlock;
1919 }
1920
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001921 if (sinfo_flags & SCTP_EOF) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001922 pr_debug("%s: shutting down association:%p\n",
1923 __func__, asoc);
1924
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001925 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 err = 0;
1927 goto out_unlock;
1928 }
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001929 if (sinfo_flags & SCTP_ABORT) {
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001930
1931 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1932 if (!chunk) {
1933 err = -ENOMEM;
1934 goto out_unlock;
1935 }
1936
Daniel Borkmannbb333812013-06-28 19:49:40 +02001937 pr_debug("%s: aborting association:%p\n",
1938 __func__, asoc);
1939
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001940 sctp_primitive_ABORT(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 err = 0;
1942 goto out_unlock;
1943 }
1944 }
1945
1946 /* Do we need to create the association? */
1947 if (!asoc) {
Xin Long2bfd80f2018-03-01 23:05:11 +08001948 err = sctp_sendmsg_new_asoc(sk, sinfo_flags, &cmsgs, &to,
1949 &transport);
1950 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Xin Long2bfd80f2018-03-01 23:05:11 +08001953 asoc = transport->asoc;
1954 new_asoc = asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
1956
1957 /* ASSERT: we have a valid association at this point. */
Daniel Borkmannbb333812013-06-28 19:49:40 +02001958 pr_debug("%s: we have a valid association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 if (!sinfo) {
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001961 /* If the user didn't specify SNDINFO/SNDRCVINFO, make up
1962 * one with some defaults.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 */
Joe Perches517aa0b2011-05-12 11:27:20 +00001964 memset(&default_sinfo, 0, sizeof(default_sinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 default_sinfo.sinfo_stream = asoc->default_stream;
1966 default_sinfo.sinfo_flags = asoc->default_flags;
1967 default_sinfo.sinfo_ppid = asoc->default_ppid;
1968 default_sinfo.sinfo_context = asoc->default_context;
1969 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1970 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 sinfo = &default_sinfo;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001973 } else if (fill_sinfo_ttl) {
1974 /* In case SNDINFO was specified, we still need to fill
1975 * it with a default ttl from the assoc here.
1976 */
1977 sinfo->sinfo_timetolive = asoc->default_timetolive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979
1980 /* API 7.1.7, the sndbuf size per association bounds the
1981 * maximum size of data that can be sent in a single send call.
1982 */
1983 if (msg_len > sk->sk_sndbuf) {
1984 err = -EMSGSIZE;
1985 goto out_free;
1986 }
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /* If an address is passed with the sendto/sendmsg call, it is used
1989 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001990 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 */
1992 if ((sctp_style(sk, TCP) && msg_name) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001993 (sinfo_flags & SCTP_ADDR_OVER)) {
Al Virodce116a2006-11-20 17:25:15 -08001994 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (!chunk_tp) {
1996 err = -EINVAL;
1997 goto out_free;
1998 }
1999 } else
2000 chunk_tp = NULL;
2001
Xin Longf84af332018-03-01 23:05:10 +08002002 /* Send msg to the asoc */
2003 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, chunk_tp, sinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005out_free:
Xin Longf84af332018-03-01 23:05:10 +08002006 if (err < 0 && err != -ESRCH && new_asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 sctp_association_free(asoc);
2008out_unlock:
wangweidong048ed4b2014-01-21 15:44:11 +08002009 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010out_nounlock:
Xin Longf84af332018-03-01 23:05:10 +08002011 return sctp_error(sk, msg->msg_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
2014/* This is an extended version of skb_pull() that removes the data from the
2015 * start of a skb even when data is spread across the list of skb's in the
2016 * frag_list. len specifies the total amount of data that needs to be removed.
2017 * when 'len' bytes could be removed from the skb, it returns 0.
2018 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2019 * could not be removed.
2020 */
2021static int sctp_skb_pull(struct sk_buff *skb, int len)
2022{
2023 struct sk_buff *list;
2024 int skb_len = skb_headlen(skb);
2025 int rlen;
2026
2027 if (len <= skb_len) {
2028 __skb_pull(skb, len);
2029 return 0;
2030 }
2031 len -= skb_len;
2032 __skb_pull(skb, skb_len);
2033
David S. Miller1b003be2009-06-09 00:22:35 -07002034 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 rlen = sctp_skb_pull(list, len);
2036 skb->len -= (len-rlen);
2037 skb->data_len -= (len-rlen);
2038
2039 if (!rlen)
2040 return 0;
2041
2042 len = rlen;
2043 }
2044
2045 return len;
2046}
2047
2048/* API 3.1.3 recvmsg() - UDP Style Syntax
2049 *
2050 * ssize_t recvmsg(int socket, struct msghdr *message,
2051 * int flags);
2052 *
2053 * socket - the socket descriptor of the endpoint.
2054 * message - pointer to the msghdr structure which contains a single
2055 * user message and possibly some ancillary data.
2056 *
2057 * See Section 5 for complete description of the data
2058 * structures.
2059 *
2060 * flags - flags sent or received with the user message, see Section
2061 * 5 for complete description of the flags.
2062 */
Ying Xue1b784142015-03-02 15:37:48 +08002063static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2064 int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 struct sctp_ulpevent *event = NULL;
2067 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002068 struct sk_buff *skb, *head_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 int copied;
2070 int err = 0;
2071 int skb_len;
2072
Daniel Borkmannbb333812013-06-28 19:49:40 +02002073 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2074 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2075 addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
wangweidong048ed4b2014-01-21 15:44:11 +08002077 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03002079 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
Xin Longe0878692016-07-30 14:14:41 +08002080 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 err = -ENOTCONN;
2082 goto out;
2083 }
2084
2085 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2086 if (!skb)
2087 goto out;
2088
2089 /* Get the total length of the skb including any skb's in the
2090 * frag_list.
2091 */
2092 skb_len = skb->len;
2093
2094 copied = skb_len;
2095 if (copied > len)
2096 copied = len;
2097
David S. Miller51f3d022014-11-05 16:46:40 -05002098 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 event = sctp_skb2event(skb);
2101
2102 if (err)
2103 goto out_free;
2104
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002105 if (event->chunk && event->chunk->head_skb)
2106 head_skb = event->chunk->head_skb;
2107 else
2108 head_skb = skb;
2109 sock_recv_ts_and_drops(msg, sk, head_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (sctp_ulpevent_is_notification(event)) {
2111 msg->msg_flags |= MSG_NOTIFICATION;
2112 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2113 } else {
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002114 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
2116
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02002117 /* Check if we allow SCTP_NXTINFO. */
2118 if (sp->recvnxtinfo)
2119 sctp_ulpevent_read_nxtinfo(event, msg, sk);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002120 /* Check if we allow SCTP_RCVINFO. */
2121 if (sp->recvrcvinfo)
2122 sctp_ulpevent_read_rcvinfo(event, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 /* Check if we allow SCTP_SNDRCVINFO. */
2124 if (sp->subscribe.sctp_data_io_event)
2125 sctp_ulpevent_read_sndrcvinfo(event, msg);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 err = copied;
2128
2129 /* If skb's length exceeds the user's buffer, update the skb and
2130 * push it back to the receive_queue so that the next call to
2131 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2132 */
2133 if (skb_len > copied) {
2134 msg->msg_flags &= ~MSG_EOR;
2135 if (flags & MSG_PEEK)
2136 goto out_free;
2137 sctp_skb_pull(skb, copied);
2138 skb_queue_head(&sk->sk_receive_queue, skb);
2139
Daniel Borkmann362d5202014-04-14 21:45:17 +02002140 /* When only partial message is copied to the user, increase
2141 * rwnd by that amount. If all the data in the skb is read,
2142 * rwnd is updated when the event is freed.
2143 */
2144 if (!sctp_ulpevent_is_notification(event))
2145 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 goto out;
2147 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2148 (event->msg_flags & MSG_EOR))
2149 msg->msg_flags |= MSG_EOR;
2150 else
2151 msg->msg_flags &= ~MSG_EOR;
2152
2153out_free:
2154 if (flags & MSG_PEEK) {
2155 /* Release the skb reference acquired after peeking the skb in
2156 * sctp_skb_recv_datagram().
2157 */
2158 kfree_skb(skb);
2159 } else {
2160 /* Free the event which includes releasing the reference to
2161 * the owner of the skb, freeing the skb and updating the
2162 * rwnd.
2163 */
2164 sctp_ulpevent_free(event);
2165 }
2166out:
wangweidong048ed4b2014-01-21 15:44:11 +08002167 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return err;
2169}
2170
2171/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2172 *
2173 * This option is a on/off flag. If enabled no SCTP message
2174 * fragmentation will be performed. Instead if a message being sent
2175 * exceeds the current PMTU size, the message will NOT be sent and
2176 * instead a error will be indicated to the user.
2177 */
2178static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002179 char __user *optval,
2180 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 int val;
2183
2184 if (optlen < sizeof(int))
2185 return -EINVAL;
2186
2187 if (get_user(val, (int __user *)optval))
2188 return -EFAULT;
2189
2190 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2191
2192 return 0;
2193}
2194
2195static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002196 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Wei Yongjun94912302011-07-02 09:28:04 +00002198 struct sctp_association *asoc;
2199 struct sctp_ulpevent *event;
2200
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002201 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return -EINVAL;
2203 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2204 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002205
Daniel Borkmannbbbea412014-07-12 20:30:40 +02002206 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
Wei Yongjun94912302011-07-02 09:28:04 +00002207 * if there is no data to be sent or retransmit, the stack will
2208 * immediately send up this notification.
2209 */
2210 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2211 &sctp_sk(sk)->subscribe)) {
2212 asoc = sctp_id2assoc(sk, 0);
2213
2214 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2215 event = sctp_ulpevent_make_sender_dry_event(asoc,
Marcelo Ricardo Leitner2e83acb2018-01-08 19:02:27 -02002216 GFP_USER | __GFP_NOWARN);
Wei Yongjun94912302011-07-02 09:28:04 +00002217 if (!event)
2218 return -ENOMEM;
2219
Xin Long9162e0e2017-12-08 21:04:05 +08002220 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
Wei Yongjun94912302011-07-02 09:28:04 +00002221 }
2222 }
2223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return 0;
2225}
2226
2227/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2228 *
2229 * This socket option is applicable to the UDP-style socket only. When
2230 * set it will cause associations that are idle for more than the
2231 * specified number of seconds to automatically close. An association
2232 * being idle is defined an association that has NOT sent or received
2233 * user data. The special value of '0' indicates that no automatic
2234 * close of any associations should be performed. The option expects an
2235 * integer defining the number of seconds of idle time before an
2236 * association is closed.
2237 */
2238static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002239 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
2241 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman9f70f462013-12-10 06:48:15 -05002242 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 /* Applicable to UDP-style socket only */
2245 if (sctp_style(sk, TCP))
2246 return -EOPNOTSUPP;
2247 if (optlen != sizeof(int))
2248 return -EINVAL;
2249 if (copy_from_user(&sp->autoclose, optval, optlen))
2250 return -EFAULT;
2251
Neil Horman9f70f462013-12-10 06:48:15 -05002252 if (sp->autoclose > net->sctp.max_autoclose)
2253 sp->autoclose = net->sctp.max_autoclose;
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 return 0;
2256}
2257
2258/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2259 *
2260 * Applications can enable or disable heartbeats for any peer address of
2261 * an association, modify an address's heartbeat interval, force a
2262 * heartbeat to be sent immediately, and adjust the address's maximum
2263 * number of retransmissions sent before an address is considered
2264 * unreachable. The following structure is used to access and modify an
2265 * address's parameters:
2266 *
2267 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002268 * sctp_assoc_t spp_assoc_id;
2269 * struct sockaddr_storage spp_address;
2270 * uint32_t spp_hbinterval;
2271 * uint16_t spp_pathmaxrxt;
2272 * uint32_t spp_pathmtu;
2273 * uint32_t spp_sackdelay;
2274 * uint32_t spp_flags;
2275 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002277 * spp_assoc_id - (one-to-many style socket) This is filled in the
2278 * application, and identifies the association for
2279 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 * spp_address - This specifies which address is of interest.
2281 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002282 * in milliseconds. If a value of zero
2283 * is present in this field then no changes are to
2284 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 * spp_pathmaxrxt - This contains the maximum number of
2286 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002287 * considered unreachable. If a value of zero
2288 * is present in this field then no changes are to
2289 * be made to this parameter.
2290 * spp_pathmtu - When Path MTU discovery is disabled the value
2291 * specified here will be the "fixed" path mtu.
2292 * Note that if the spp_address field is empty
2293 * then all associations on this address will
2294 * have this fixed path mtu set upon them.
2295 *
2296 * spp_sackdelay - When delayed sack is enabled, this value specifies
2297 * the number of milliseconds that sacks will be delayed
2298 * for. This value will apply to all addresses of an
2299 * association if the spp_address field is empty. Note
2300 * also, that if delayed sack is enabled and this
2301 * value is set to 0, no change is made to the last
2302 * recorded delayed sack timer value.
2303 *
2304 * spp_flags - These flags are used to control various features
2305 * on an association. The flag field may contain
2306 * zero or more of the following options.
2307 *
2308 * SPP_HB_ENABLE - Enable heartbeats on the
2309 * specified address. Note that if the address
2310 * field is empty all addresses for the association
2311 * have heartbeats enabled upon them.
2312 *
2313 * SPP_HB_DISABLE - Disable heartbeats on the
2314 * speicifed address. Note that if the address
2315 * field is empty all addresses for the association
2316 * will have their heartbeats disabled. Note also
2317 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2318 * mutually exclusive, only one of these two should
2319 * be specified. Enabling both fields will have
2320 * undetermined results.
2321 *
2322 * SPP_HB_DEMAND - Request a user initiated heartbeat
2323 * to be made immediately.
2324 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002325 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2326 * heartbeat delayis to be set to the value of 0
2327 * milliseconds.
2328 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002329 * SPP_PMTUD_ENABLE - This field will enable PMTU
2330 * discovery upon the specified address. Note that
2331 * if the address feild is empty then all addresses
2332 * on the association are effected.
2333 *
2334 * SPP_PMTUD_DISABLE - This field will disable PMTU
2335 * discovery upon the specified address. Note that
2336 * if the address feild is empty then all addresses
2337 * on the association are effected. Not also that
2338 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2339 * exclusive. Enabling both will have undetermined
2340 * results.
2341 *
2342 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2343 * on delayed sack. The time specified in spp_sackdelay
2344 * is used to specify the sack delay for this address. Note
2345 * that if spp_address is empty then all addresses will
2346 * enable delayed sack and take on the sack delay
2347 * value specified in spp_sackdelay.
2348 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2349 * off delayed sack. If the spp_address field is blank then
2350 * delayed sack is disabled for the entire association. Note
2351 * also that this field is mutually exclusive to
2352 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2353 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 */
Adrian Bunk16164362006-09-18 00:40:38 -07002355static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2356 struct sctp_transport *trans,
2357 struct sctp_association *asoc,
2358 struct sctp_sock *sp,
2359 int hb_change,
2360 int pmtud_change,
2361 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 int error;
2364
Frank Filz52ccb8e2005-12-22 11:36:46 -08002365 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002366 struct net *net = sock_net(trans->asoc->base.sk);
2367
2368 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (error)
2370 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
2372
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002373 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2374 * this field is ignored. Note also that a value of zero indicates
2375 * the current setting should be left unchanged.
2376 */
2377 if (params->spp_flags & SPP_HB_ENABLE) {
2378
2379 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2380 * set. This lets us use 0 value when this flag
2381 * is set.
2382 */
2383 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2384 params->spp_hbinterval = 0;
2385
2386 if (params->spp_hbinterval ||
2387 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2388 if (trans) {
2389 trans->hbinterval =
2390 msecs_to_jiffies(params->spp_hbinterval);
2391 } else if (asoc) {
2392 asoc->hbinterval =
2393 msecs_to_jiffies(params->spp_hbinterval);
2394 } else {
2395 sp->hbinterval = params->spp_hbinterval;
2396 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002397 }
2398 }
2399
2400 if (hb_change) {
2401 if (trans) {
2402 trans->param_flags =
2403 (trans->param_flags & ~SPP_HB) | hb_change;
2404 } else if (asoc) {
2405 asoc->param_flags =
2406 (asoc->param_flags & ~SPP_HB) | hb_change;
2407 } else {
2408 sp->param_flags =
2409 (sp->param_flags & ~SPP_HB) | hb_change;
2410 }
2411 }
2412
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002413 /* When Path MTU discovery is disabled the value specified here will
2414 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2415 * include the flag SPP_PMTUD_DISABLE for this field to have any
2416 * effect).
2417 */
2418 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002419 if (trans) {
2420 trans->pathmtu = params->spp_pathmtu;
Xin Long3ebfdf02017-04-04 13:39:55 +08002421 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002422 } else if (asoc) {
2423 asoc->pathmtu = params->spp_pathmtu;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002424 } else {
2425 sp->pathmtu = params->spp_pathmtu;
2426 }
2427 }
2428
2429 if (pmtud_change) {
2430 if (trans) {
2431 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2432 (params->spp_flags & SPP_PMTUD_ENABLE);
2433 trans->param_flags =
2434 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2435 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002436 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
Xin Long3ebfdf02017-04-04 13:39:55 +08002437 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002438 }
2439 } else if (asoc) {
2440 asoc->param_flags =
2441 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2442 } else {
2443 sp->param_flags =
2444 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2445 }
2446 }
2447
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002448 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2449 * value of this field is ignored. Note also that a value of zero
2450 * indicates the current setting should be left unchanged.
2451 */
2452 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002453 if (trans) {
2454 trans->sackdelay =
2455 msecs_to_jiffies(params->spp_sackdelay);
2456 } else if (asoc) {
2457 asoc->sackdelay =
2458 msecs_to_jiffies(params->spp_sackdelay);
2459 } else {
2460 sp->sackdelay = params->spp_sackdelay;
2461 }
2462 }
2463
2464 if (sackdelay_change) {
2465 if (trans) {
2466 trans->param_flags =
2467 (trans->param_flags & ~SPP_SACKDELAY) |
2468 sackdelay_change;
2469 } else if (asoc) {
2470 asoc->param_flags =
2471 (asoc->param_flags & ~SPP_SACKDELAY) |
2472 sackdelay_change;
2473 } else {
2474 sp->param_flags =
2475 (sp->param_flags & ~SPP_SACKDELAY) |
2476 sackdelay_change;
2477 }
2478 }
2479
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002480 /* Note that a value of zero indicates the current setting should be
2481 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002482 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002483 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002484 if (trans) {
2485 trans->pathmaxrxt = params->spp_pathmaxrxt;
2486 } else if (asoc) {
2487 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2488 } else {
2489 sp->pathmaxrxt = params->spp_pathmaxrxt;
2490 }
2491 }
2492
2493 return 0;
2494}
2495
2496static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002497 char __user *optval,
2498 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002499{
2500 struct sctp_paddrparams params;
2501 struct sctp_transport *trans = NULL;
2502 struct sctp_association *asoc = NULL;
2503 struct sctp_sock *sp = sctp_sk(sk);
2504 int error;
2505 int hb_change, pmtud_change, sackdelay_change;
2506
2507 if (optlen != sizeof(struct sctp_paddrparams))
wangweidongcb3f8372013-12-23 12:16:50 +08002508 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002509
2510 if (copy_from_user(&params, optval, optlen))
2511 return -EFAULT;
2512
2513 /* Validate flags and value parameters. */
2514 hb_change = params.spp_flags & SPP_HB;
2515 pmtud_change = params.spp_flags & SPP_PMTUD;
2516 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2517
2518 if (hb_change == SPP_HB ||
2519 pmtud_change == SPP_PMTUD ||
2520 sackdelay_change == SPP_SACKDELAY ||
2521 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002522 (params.spp_pathmtu &&
2523 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002524 return -EINVAL;
2525
2526 /* If an address other than INADDR_ANY is specified, and
2527 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 */
wangweidongcb3f8372013-12-23 12:16:50 +08002529 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002530 trans = sctp_addr_id2transport(sk, &params.spp_address,
2531 params.spp_assoc_id);
2532 if (!trans)
2533 return -EINVAL;
2534 }
2535
2536 /* Get association, if assoc_id != 0 and the socket is a one
2537 * to many style socket, and an association was not found, then
2538 * the id was invalid.
2539 */
2540 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2541 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2542 return -EINVAL;
2543
2544 /* Heartbeat demand can only be sent on a transport or
2545 * association, but not a socket.
2546 */
2547 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2548 return -EINVAL;
2549
2550 /* Process parameters. */
2551 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2552 hb_change, pmtud_change,
2553 sackdelay_change);
2554
2555 if (error)
2556 return error;
2557
2558 /* If changes are for association, also apply parameters to each
2559 * transport.
2560 */
2561 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002562 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2563 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002564 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2565 hb_change, pmtud_change,
2566 sackdelay_change);
2567 }
2568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
2570 return 0;
2571}
2572
wangweidong0ea5e4d2014-01-15 17:24:01 +08002573static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2574{
2575 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2576}
2577
2578static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2579{
2580 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2581}
2582
Wei Yongjund364d922008-05-09 15:13:26 -07002583/*
2584 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002585 *
Wei Yongjund364d922008-05-09 15:13:26 -07002586 * This option will effect the way delayed acks are performed. This
2587 * option allows you to get or set the delayed ack time, in
2588 * milliseconds. It also allows changing the delayed ack frequency.
2589 * Changing the frequency to 1 disables the delayed sack algorithm. If
2590 * the assoc_id is 0, then this sets or gets the endpoints default
2591 * values. If the assoc_id field is non-zero, then the set or get
2592 * effects the specified association for the one to many model (the
2593 * assoc_id field is ignored by the one to one model). Note that if
2594 * sack_delay or sack_freq are 0 when setting this option, then the
2595 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002596 *
Wei Yongjund364d922008-05-09 15:13:26 -07002597 * struct sctp_sack_info {
2598 * sctp_assoc_t sack_assoc_id;
2599 * uint32_t sack_delay;
2600 * uint32_t sack_freq;
2601 * };
Frank Filz77086102005-12-22 11:37:30 -08002602 *
Wei Yongjund364d922008-05-09 15:13:26 -07002603 * sack_assoc_id - This parameter, indicates which association the user
2604 * is performing an action upon. Note that if this field's value is
2605 * zero then the endpoints default value is changed (effecting future
2606 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002607 *
Wei Yongjund364d922008-05-09 15:13:26 -07002608 * sack_delay - This parameter contains the number of milliseconds that
2609 * the user is requesting the delayed ACK timer be set to. Note that
2610 * this value is defined in the standard to be between 200 and 500
2611 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002612 *
Wei Yongjund364d922008-05-09 15:13:26 -07002613 * sack_freq - This parameter contains the number of packets that must
2614 * be received before a sack is sent without waiting for the delay
2615 * timer to expire. The default value for this is 2, setting this
2616 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002617 */
2618
Wei Yongjund364d922008-05-09 15:13:26 -07002619static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002620 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002621{
Wei Yongjund364d922008-05-09 15:13:26 -07002622 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002623 struct sctp_transport *trans = NULL;
2624 struct sctp_association *asoc = NULL;
2625 struct sctp_sock *sp = sctp_sk(sk);
2626
Wei Yongjund364d922008-05-09 15:13:26 -07002627 if (optlen == sizeof(struct sctp_sack_info)) {
2628 if (copy_from_user(&params, optval, optlen))
2629 return -EFAULT;
2630
2631 if (params.sack_delay == 0 && params.sack_freq == 0)
2632 return 0;
2633 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05002634 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05002635 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05002636 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05002637 "Use struct sctp_sack_info instead\n",
2638 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07002639 if (copy_from_user(&params, optval, optlen))
2640 return -EFAULT;
2641
2642 if (params.sack_delay == 0)
2643 params.sack_freq = 1;
2644 else
2645 params.sack_freq = 0;
2646 } else
wangweidongcb3f8372013-12-23 12:16:50 +08002647 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08002648
Frank Filz77086102005-12-22 11:37:30 -08002649 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002650 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002651 return -EINVAL;
2652
Wei Yongjund364d922008-05-09 15:13:26 -07002653 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002654 * to many style socket, and an association was not found, then
2655 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002656 */
Wei Yongjund364d922008-05-09 15:13:26 -07002657 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2658 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002659 return -EINVAL;
2660
Wei Yongjund364d922008-05-09 15:13:26 -07002661 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002662 if (asoc) {
2663 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002664 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002665 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002666 sctp_spp_sackdelay_enable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002667 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002668 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002669 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002670 sctp_spp_sackdelay_enable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002671 }
Wei Yongjund364d922008-05-09 15:13:26 -07002672 }
2673
2674 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002675 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002676 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002677 sctp_spp_sackdelay_disable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002678 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002679 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002680 sctp_spp_sackdelay_disable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002681 }
Wei Yongjund364d922008-05-09 15:13:26 -07002682 } else if (params.sack_freq > 1) {
2683 if (asoc) {
2684 asoc->sackfreq = params.sack_freq;
2685 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002686 sctp_spp_sackdelay_enable(asoc->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002687 } else {
2688 sp->sackfreq = params.sack_freq;
2689 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002690 sctp_spp_sackdelay_enable(sp->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002691 }
Frank Filz77086102005-12-22 11:37:30 -08002692 }
2693
2694 /* If change is for association, also apply to each transport. */
2695 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002696 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2697 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002698 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002699 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002700 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002701 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002702 sctp_spp_sackdelay_enable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002703 }
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07002704 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002705 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002706 sctp_spp_sackdelay_disable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002707 } else if (params.sack_freq > 1) {
2708 trans->sackfreq = params.sack_freq;
2709 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002710 sctp_spp_sackdelay_enable(trans->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002711 }
2712 }
2713 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002714
Frank Filz77086102005-12-22 11:37:30 -08002715 return 0;
2716}
2717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2719 *
2720 * Applications can specify protocol parameters for the default association
2721 * initialization. The option name argument to setsockopt() and getsockopt()
2722 * is SCTP_INITMSG.
2723 *
2724 * Setting initialization parameters is effective only on an unconnected
2725 * socket (for UDP-style sockets only future associations are effected
2726 * by the change). With TCP-style sockets, this option is inherited by
2727 * sockets derived from a listener socket.
2728 */
David S. Millerb7058842009-09-30 16:12:20 -07002729static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
2731 struct sctp_initmsg sinit;
2732 struct sctp_sock *sp = sctp_sk(sk);
2733
2734 if (optlen != sizeof(struct sctp_initmsg))
2735 return -EINVAL;
2736 if (copy_from_user(&sinit, optval, optlen))
2737 return -EFAULT;
2738
2739 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002740 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002742 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002744 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002746 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 return 0;
2749}
2750
2751/*
2752 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2753 *
2754 * Applications that wish to use the sendto() system call may wish to
2755 * specify a default set of parameters that would normally be supplied
2756 * through the inclusion of ancillary data. This socket option allows
2757 * such an application to set the default sctp_sndrcvinfo structure.
2758 * The application that wishes to use this socket option simply passes
2759 * in to this call the sctp_sndrcvinfo structure defined in Section
2760 * 5.2.2) The input parameters accepted by this call include
2761 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2762 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2763 * to this call if the caller is using the UDP model.
2764 */
2765static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002766 char __user *optval,
2767 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002770 struct sctp_association *asoc;
2771 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002773 if (optlen != sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return -EINVAL;
2775 if (copy_from_user(&info, optval, optlen))
2776 return -EFAULT;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002777 if (info.sinfo_flags &
2778 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2779 SCTP_ABORT | SCTP_EOF))
2780 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2783 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2784 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 if (asoc) {
2786 asoc->default_stream = info.sinfo_stream;
2787 asoc->default_flags = info.sinfo_flags;
2788 asoc->default_ppid = info.sinfo_ppid;
2789 asoc->default_context = info.sinfo_context;
2790 asoc->default_timetolive = info.sinfo_timetolive;
2791 } else {
2792 sp->default_stream = info.sinfo_stream;
2793 sp->default_flags = info.sinfo_flags;
2794 sp->default_ppid = info.sinfo_ppid;
2795 sp->default_context = info.sinfo_context;
2796 sp->default_timetolive = info.sinfo_timetolive;
2797 }
2798
2799 return 0;
2800}
2801
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002802/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2803 * (SCTP_DEFAULT_SNDINFO)
2804 */
2805static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2806 char __user *optval,
2807 unsigned int optlen)
2808{
2809 struct sctp_sock *sp = sctp_sk(sk);
2810 struct sctp_association *asoc;
2811 struct sctp_sndinfo info;
2812
2813 if (optlen != sizeof(info))
2814 return -EINVAL;
2815 if (copy_from_user(&info, optval, optlen))
2816 return -EFAULT;
2817 if (info.snd_flags &
2818 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2819 SCTP_ABORT | SCTP_EOF))
2820 return -EINVAL;
2821
2822 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
2823 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
2824 return -EINVAL;
2825 if (asoc) {
2826 asoc->default_stream = info.snd_sid;
2827 asoc->default_flags = info.snd_flags;
2828 asoc->default_ppid = info.snd_ppid;
2829 asoc->default_context = info.snd_context;
2830 } else {
2831 sp->default_stream = info.snd_sid;
2832 sp->default_flags = info.snd_flags;
2833 sp->default_ppid = info.snd_ppid;
2834 sp->default_context = info.snd_context;
2835 }
2836
2837 return 0;
2838}
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2841 *
2842 * Requests that the local SCTP stack use the enclosed peer address as
2843 * the association primary. The enclosed address must be one of the
2844 * association peer's addresses.
2845 */
2846static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002847 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
2849 struct sctp_prim prim;
2850 struct sctp_transport *trans;
2851
2852 if (optlen != sizeof(struct sctp_prim))
2853 return -EINVAL;
2854
2855 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2856 return -EFAULT;
2857
2858 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2859 if (!trans)
2860 return -EINVAL;
2861
2862 sctp_assoc_set_primary(trans->asoc, trans);
2863
2864 return 0;
2865}
2866
2867/*
2868 * 7.1.5 SCTP_NODELAY
2869 *
2870 * Turn on/off any Nagle-like algorithm. This means that packets are
2871 * generally sent as soon as possible and no unnecessary delays are
2872 * introduced, at the cost of more packets in the network. Expects an
2873 * integer boolean flag.
2874 */
2875static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002876 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
2878 int val;
2879
2880 if (optlen < sizeof(int))
2881 return -EINVAL;
2882 if (get_user(val, (int __user *)optval))
2883 return -EFAULT;
2884
2885 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2886 return 0;
2887}
2888
2889/*
2890 *
2891 * 7.1.1 SCTP_RTOINFO
2892 *
2893 * The protocol parameters used to initialize and bound retransmission
2894 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2895 * and modify these parameters.
2896 * All parameters are time values, in milliseconds. A value of 0, when
2897 * modifying the parameters, indicates that the current value should not
2898 * be changed.
2899 *
2900 */
David S. Millerb7058842009-09-30 16:12:20 -07002901static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2902{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 struct sctp_rtoinfo rtoinfo;
2904 struct sctp_association *asoc;
wangweidong85f935d2013-12-11 09:50:38 +08002905 unsigned long rto_min, rto_max;
2906 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 if (optlen != sizeof (struct sctp_rtoinfo))
2909 return -EINVAL;
2910
2911 if (copy_from_user(&rtoinfo, optval, optlen))
2912 return -EFAULT;
2913
2914 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2915
2916 /* Set the values to the specific association */
2917 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2918 return -EINVAL;
2919
wangweidong85f935d2013-12-11 09:50:38 +08002920 rto_max = rtoinfo.srto_max;
2921 rto_min = rtoinfo.srto_min;
2922
2923 if (rto_max)
2924 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
2925 else
2926 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
2927
2928 if (rto_min)
2929 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
2930 else
2931 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
2932
2933 if (rto_min > rto_max)
2934 return -EINVAL;
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (asoc) {
2937 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002938 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 msecs_to_jiffies(rtoinfo.srto_initial);
wangweidong85f935d2013-12-11 09:50:38 +08002940 asoc->rto_max = rto_max;
2941 asoc->rto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 } else {
2943 /* If there is no association or the association-id = 0
2944 * set the values to the endpoint.
2945 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 if (rtoinfo.srto_initial != 0)
2947 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
wangweidong85f935d2013-12-11 09:50:38 +08002948 sp->rtoinfo.srto_max = rto_max;
2949 sp->rtoinfo.srto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 }
2951
2952 return 0;
2953}
2954
2955/*
2956 *
2957 * 7.1.2 SCTP_ASSOCINFO
2958 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02002959 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 * of the association.
2961 * Returns an error if the new association retransmission value is
2962 * greater than the sum of the retransmission value of the peer.
2963 * See [SCTP] for more information.
2964 *
2965 */
David S. Millerb7058842009-09-30 16:12:20 -07002966static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
2968
2969 struct sctp_assocparams assocparams;
2970 struct sctp_association *asoc;
2971
2972 if (optlen != sizeof(struct sctp_assocparams))
2973 return -EINVAL;
2974 if (copy_from_user(&assocparams, optval, optlen))
2975 return -EFAULT;
2976
2977 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2978
2979 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2980 return -EINVAL;
2981
2982 /* Set the values to the specific association */
2983 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002984 if (assocparams.sasoc_asocmaxrxt != 0) {
2985 __u32 path_sum = 0;
2986 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002987 struct sctp_transport *peer_addr;
2988
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002989 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
2990 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002991 path_sum += peer_addr->pathmaxrxt;
2992 paths++;
2993 }
2994
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002995 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002996 * one path/transport. We do this because path
2997 * retransmissions are only counted when we have more
2998 * then one path.
2999 */
3000 if (paths > 1 &&
3001 assocparams.sasoc_asocmaxrxt > path_sum)
3002 return -EINVAL;
3003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003005 }
3006
Daniel Borkmann52db8822013-06-25 18:17:27 +02003007 if (assocparams.sasoc_cookie_life != 0)
3008 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 } else {
3010 /* Set the values to the endpoint */
3011 struct sctp_sock *sp = sctp_sk(sk);
3012
3013 if (assocparams.sasoc_asocmaxrxt != 0)
3014 sp->assocparams.sasoc_asocmaxrxt =
3015 assocparams.sasoc_asocmaxrxt;
3016 if (assocparams.sasoc_cookie_life != 0)
3017 sp->assocparams.sasoc_cookie_life =
3018 assocparams.sasoc_cookie_life;
3019 }
3020 return 0;
3021}
3022
3023/*
3024 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3025 *
3026 * This socket option is a boolean flag which turns on or off mapped V4
3027 * addresses. If this option is turned on and the socket is type
3028 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3029 * If this option is turned off, then no mapping will be done of V4
3030 * addresses and a user will receive both PF_INET6 and PF_INET type
3031 * addresses on the socket.
3032 */
David S. Millerb7058842009-09-30 16:12:20 -07003033static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
3035 int val;
3036 struct sctp_sock *sp = sctp_sk(sk);
3037
3038 if (optlen < sizeof(int))
3039 return -EINVAL;
3040 if (get_user(val, (int __user *)optval))
3041 return -EFAULT;
3042 if (val)
3043 sp->v4mapped = 1;
3044 else
3045 sp->v4mapped = 0;
3046
3047 return 0;
3048}
3049
3050/*
Wei Yongjune89c2092008-12-25 16:54:58 -08003051 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3052 * This option will get or set the maximum size to put in any outgoing
3053 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 * fragmented by SCTP into the specified size. Note that the underlying
3055 * SCTP implementation may fragment into smaller sized chunks when the
3056 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08003057 * the user. The default value for this option is '0' which indicates
3058 * the user is NOT limiting fragmentation and only the PMTU will effect
3059 * SCTP's choice of DATA chunk size. Note also that values set larger
3060 * than the maximum size of an IP datagram will effectively let SCTP
3061 * control fragmentation (i.e. the same as setting this option to 0).
3062 *
3063 * The following structure is used to access and modify this parameter:
3064 *
3065 * struct sctp_assoc_value {
3066 * sctp_assoc_t assoc_id;
3067 * uint32_t assoc_value;
3068 * };
3069 *
3070 * assoc_id: This parameter is ignored for one-to-one style sockets.
3071 * For one-to-many style sockets this parameter indicates which
3072 * association the user is performing an action upon. Note that if
3073 * this field's value is zero then the endpoints default value is
3074 * changed (effecting future associations only).
3075 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 */
David S. Millerb7058842009-09-30 16:12:20 -07003077static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
Xin Longecca8f82017-11-17 14:11:11 +08003079 struct sctp_sock *sp = sctp_sk(sk);
Wei Yongjune89c2092008-12-25 16:54:58 -08003080 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 int val;
3083
Wei Yongjune89c2092008-12-25 16:54:58 -08003084 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003085 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003086 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003087 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003088 "Use struct sctp_assoc_value instead\n",
3089 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08003090 if (copy_from_user(&val, optval, optlen))
3091 return -EFAULT;
3092 params.assoc_id = 0;
3093 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3094 if (copy_from_user(&params, optval, optlen))
3095 return -EFAULT;
3096 val = params.assoc_value;
Xin Longecca8f82017-11-17 14:11:11 +08003097 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return -EINVAL;
Xin Longecca8f82017-11-17 14:11:11 +08003099 }
Wei Yongjune89c2092008-12-25 16:54:58 -08003100
Xin Longecca8f82017-11-17 14:11:11 +08003101 if (val) {
3102 int min_len, max_len;
3103
3104 min_len = SCTP_DEFAULT_MINSEGMENT - sp->pf->af->net_header_len;
3105 min_len -= sizeof(struct sctphdr) +
3106 sizeof(struct sctp_data_chunk);
3107
3108 max_len = SCTP_MAX_CHUNK_LEN - sizeof(struct sctp_data_chunk);
3109
3110 if (val < min_len || val > max_len)
3111 return -EINVAL;
3112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Wei Yongjune89c2092008-12-25 16:54:58 -08003114 asoc = sctp_id2assoc(sk, params.assoc_id);
Wei Yongjune89c2092008-12-25 16:54:58 -08003115 if (asoc) {
3116 if (val == 0) {
Xin Longecca8f82017-11-17 14:11:11 +08003117 val = asoc->pathmtu - sp->pf->af->net_header_len;
Wei Yongjune89c2092008-12-25 16:54:58 -08003118 val -= sizeof(struct sctphdr) +
Xin Long668c9be2017-12-08 21:04:02 +08003119 sctp_datachk_len(&asoc->stream);
Wei Yongjune89c2092008-12-25 16:54:58 -08003120 }
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003121 asoc->user_frag = val;
3122 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
Wei Yongjune89c2092008-12-25 16:54:58 -08003123 } else {
Xin Longecca8f82017-11-17 14:11:11 +08003124 if (params.assoc_id && sctp_style(sk, UDP))
3125 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003126 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 }
3128
3129 return 0;
3130}
3131
3132
3133/*
3134 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3135 *
3136 * Requests that the peer mark the enclosed address as the association
3137 * primary. The enclosed address must be one of the association's
3138 * locally bound addresses. The following structure is used to make a
3139 * set primary request:
3140 */
3141static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003142 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003144 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 struct sctp_association *asoc = NULL;
3147 struct sctp_setpeerprim prim;
3148 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003149 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 int err;
3151
3152 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003154 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return -EPERM;
3156
3157 if (optlen != sizeof(struct sctp_setpeerprim))
3158 return -EINVAL;
3159
3160 if (copy_from_user(&prim, optval, optlen))
3161 return -EFAULT;
3162
3163 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003164 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 return -EINVAL;
3166
3167 if (!asoc->peer.asconf_capable)
3168 return -EPERM;
3169
3170 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3171 return -EPERM;
3172
3173 if (!sctp_state(asoc, ESTABLISHED))
3174 return -ENOTCONN;
3175
Wei Yongjun40a01032010-12-07 17:11:09 +00003176 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3177 if (!af)
3178 return -EINVAL;
3179
3180 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3181 return -EADDRNOTAVAIL;
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3184 return -EADDRNOTAVAIL;
3185
3186 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3187 chunk = sctp_make_asconf_set_prim(asoc,
3188 (union sctp_addr *)&prim.sspp_addr);
3189 if (!chunk)
3190 return -ENOMEM;
3191
3192 err = sctp_send_asconf(asoc, chunk);
3193
Daniel Borkmannbb333812013-06-28 19:49:40 +02003194 pr_debug("%s: we set peer primary addr primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
3196 return err;
3197}
3198
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003199static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003200 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003202 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003204 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003206 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 return -EFAULT;
3208
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003209 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211 return 0;
3212}
3213
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003214/*
3215 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3216 *
3217 * The context field in the sctp_sndrcvinfo structure is normally only
3218 * used when a failed message is retrieved holding the value that was
3219 * sent down on the actual send call. This option allows the setting of
3220 * a default context on an association basis that will be received on
3221 * reading messages from the peer. This is especially helpful in the
3222 * one-2-many model for an application to keep some reference to an
3223 * internal state machine that is processing messages on the
3224 * association. Note that the setting of this value only effects
3225 * received messages from the peer and does not effect the value that is
3226 * saved with outbound messages.
3227 */
3228static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003229 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003230{
3231 struct sctp_assoc_value params;
3232 struct sctp_sock *sp;
3233 struct sctp_association *asoc;
3234
3235 if (optlen != sizeof(struct sctp_assoc_value))
3236 return -EINVAL;
3237 if (copy_from_user(&params, optval, optlen))
3238 return -EFAULT;
3239
3240 sp = sctp_sk(sk);
3241
3242 if (params.assoc_id != 0) {
3243 asoc = sctp_id2assoc(sk, params.assoc_id);
3244 if (!asoc)
3245 return -EINVAL;
3246 asoc->default_rcv_context = params.assoc_value;
3247 } else {
3248 sp->default_rcv_context = params.assoc_value;
3249 }
3250
3251 return 0;
3252}
3253
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003254/*
3255 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3256 *
3257 * This options will at a minimum specify if the implementation is doing
3258 * fragmented interleave. Fragmented interleave, for a one to many
3259 * socket, is when subsequent calls to receive a message may return
3260 * parts of messages from different associations. Some implementations
3261 * may allow you to turn this value on or off. If so, when turned off,
3262 * no fragment interleave will occur (which will cause a head of line
3263 * blocking amongst multiple associations sharing the same one to many
3264 * socket). When this option is turned on, then each receive call may
3265 * come from a different association (thus the user must receive data
3266 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3267 * association each receive belongs to.
3268 *
3269 * This option takes a boolean value. A non-zero value indicates that
3270 * fragmented interleave is on. A value of zero indicates that
3271 * fragmented interleave is off.
3272 *
3273 * Note that it is important that an implementation that allows this
3274 * option to be turned on, have it off by default. Otherwise an unaware
3275 * application using the one to many model may become confused and act
3276 * incorrectly.
3277 */
3278static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3279 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003280 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003281{
3282 int val;
3283
3284 if (optlen != sizeof(int))
3285 return -EINVAL;
3286 if (get_user(val, (int __user *)optval))
3287 return -EFAULT;
3288
Xin Long772a5862017-12-08 21:03:58 +08003289 sctp_sk(sk)->frag_interleave = !!val;
3290
3291 if (!sctp_sk(sk)->frag_interleave)
3292 sctp_sk(sk)->strm_interleave = 0;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003293
3294 return 0;
3295}
3296
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003297/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003298 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003299 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003300 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003301 * This option will set or get the SCTP partial delivery point. This
3302 * point is the size of a message where the partial delivery API will be
3303 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003304 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003305 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003306 * point. Note also that the call will fail if the user attempts to set
3307 * this value larger than the socket receive buffer size.
3308 *
3309 * Note that any single message having a length smaller than or equal to
3310 * the SCTP partial delivery point will be delivered in one single read
3311 * call as long as the user provided buffer is large enough to hold the
3312 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003313 */
3314static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3315 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003316 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003317{
3318 u32 val;
3319
3320 if (optlen != sizeof(u32))
3321 return -EINVAL;
3322 if (get_user(val, (int __user *)optval))
3323 return -EFAULT;
3324
Wei Yongjun8510b932008-12-25 16:59:03 -08003325 /* Note: We double the receive buffer from what the user sets
3326 * it to be, also initial rwnd is based on rcvbuf/2.
3327 */
3328 if (val > (sk->sk_rcvbuf >> 1))
3329 return -EINVAL;
3330
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003331 sctp_sk(sk)->pd_point = val;
3332
3333 return 0; /* is this the right error code? */
3334}
3335
Vlad Yasevich70331572007-03-23 11:34:36 -07003336/*
3337 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3338 *
3339 * This option will allow a user to change the maximum burst of packets
3340 * that can be emitted by this association. Note that the default value
3341 * is 4, and some implementations may restrict this setting so that it
3342 * can only be lowered.
3343 *
3344 * NOTE: This text doesn't seem right. Do this on a socket basis with
3345 * future associations inheriting the socket value.
3346 */
3347static int sctp_setsockopt_maxburst(struct sock *sk,
3348 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003349 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003350{
Neil Horman219b99a2008-03-05 13:44:46 -08003351 struct sctp_assoc_value params;
3352 struct sctp_sock *sp;
3353 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003354 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003355 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003356
Neil Horman219b99a2008-03-05 13:44:46 -08003357 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003358 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003359 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003360 "Use of int in max_burst socket option deprecated.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003361 "Use struct sctp_assoc_value instead\n",
3362 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08003363 if (copy_from_user(&val, optval, optlen))
3364 return -EFAULT;
3365 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3366 if (copy_from_user(&params, optval, optlen))
3367 return -EFAULT;
3368 val = params.assoc_value;
3369 assoc_id = params.assoc_id;
3370 } else
3371 return -EINVAL;
3372
3373 sp = sctp_sk(sk);
3374
3375 if (assoc_id != 0) {
3376 asoc = sctp_id2assoc(sk, assoc_id);
3377 if (!asoc)
3378 return -EINVAL;
3379 asoc->max_burst = val;
3380 } else
3381 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003382
3383 return 0;
3384}
3385
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003386/*
3387 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3388 *
3389 * This set option adds a chunk type that the user is requesting to be
3390 * received only in an authenticated way. Changes to the list of chunks
3391 * will only effect future associations on the socket.
3392 */
3393static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003394 char __user *optval,
3395 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003396{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003397 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003398 struct sctp_authchunk val;
3399
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003400 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003401 return -EACCES;
3402
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003403 if (optlen != sizeof(struct sctp_authchunk))
3404 return -EINVAL;
3405 if (copy_from_user(&val, optval, optlen))
3406 return -EFAULT;
3407
3408 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003409 case SCTP_CID_INIT:
3410 case SCTP_CID_INIT_ACK:
3411 case SCTP_CID_SHUTDOWN_COMPLETE:
3412 case SCTP_CID_AUTH:
3413 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003414 }
3415
3416 /* add this chunk id to the endpoint */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003417 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003418}
3419
3420/*
3421 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3422 *
3423 * This option gets or sets the list of HMAC algorithms that the local
3424 * endpoint requires the peer to use.
3425 */
3426static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003427 char __user *optval,
3428 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003429{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003430 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003431 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003432 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003433 int err;
3434
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003435 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003436 return -EACCES;
3437
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003438 if (optlen < sizeof(struct sctp_hmacalgo))
3439 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003440 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3441 SCTP_AUTH_NUM_HMACS * sizeof(u16));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003442
wangweidongcb3f8372013-12-23 12:16:50 +08003443 hmacs = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003444 if (IS_ERR(hmacs))
3445 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003446
Vlad Yasevichd9724052008-08-27 16:09:49 -07003447 idents = hmacs->shmac_num_idents;
3448 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3449 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003450 err = -EINVAL;
3451 goto out;
3452 }
3453
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003454 err = sctp_auth_ep_set_hmacs(ep, hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003455out:
3456 kfree(hmacs);
3457 return err;
3458}
3459
3460/*
3461 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3462 *
3463 * This option will set a shared secret key which is used to build an
3464 * association shared key.
3465 */
3466static int sctp_setsockopt_auth_key(struct sock *sk,
3467 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003468 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_authkey *authkey;
3472 struct sctp_association *asoc;
3473 int ret;
3474
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003475 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003476 return -EACCES;
3477
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003478 if (optlen <= sizeof(struct sctp_authkey))
3479 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003480 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3481 * this.
3482 */
3483 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3484 sizeof(struct sctp_authkey));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003485
wangweidongcb3f8372013-12-23 12:16:50 +08003486 authkey = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003487 if (IS_ERR(authkey))
3488 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003489
Vlad Yasevich328fc472008-08-27 16:08:54 -07003490 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003491 ret = -EINVAL;
3492 goto out;
3493 }
3494
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003495 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3496 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3497 ret = -EINVAL;
3498 goto out;
3499 }
3500
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003501 ret = sctp_auth_set_key(ep, asoc, authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003502out:
Daniel Borkmann6ba542a2013-02-08 03:04:34 +00003503 kzfree(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003504 return ret;
3505}
3506
3507/*
3508 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3509 *
3510 * This option will get or set the active shared key to be used to build
3511 * the association shared key.
3512 */
3513static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003514 char __user *optval,
3515 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003516{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003517 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003518 struct sctp_authkeyid val;
3519 struct sctp_association *asoc;
3520
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003521 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003522 return -EACCES;
3523
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003524 if (optlen != sizeof(struct sctp_authkeyid))
3525 return -EINVAL;
3526 if (copy_from_user(&val, optval, optlen))
3527 return -EFAULT;
3528
3529 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3530 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3531 return -EINVAL;
3532
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003533 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003534}
3535
3536/*
3537 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3538 *
3539 * This set option will delete a shared secret key from use.
3540 */
3541static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003542 char __user *optval,
3543 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003544{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003545 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003546 struct sctp_authkeyid val;
3547 struct sctp_association *asoc;
3548
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003549 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003550 return -EACCES;
3551
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003552 if (optlen != sizeof(struct sctp_authkeyid))
3553 return -EINVAL;
3554 if (copy_from_user(&val, optval, optlen))
3555 return -EFAULT;
3556
3557 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3558 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3559 return -EINVAL;
3560
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003561 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003562
3563}
3564
Michio Honda7dc04d72011-04-26 20:16:31 +09003565/*
3566 * 8.1.23 SCTP_AUTO_ASCONF
3567 *
3568 * This option will enable or disable the use of the automatic generation of
3569 * ASCONF chunks to add and delete addresses to an existing association. Note
3570 * that this option has two caveats namely: a) it only affects sockets that
3571 * are bound to all addresses available to the SCTP stack, and b) the system
3572 * administrator may have an overriding control that turns the ASCONF feature
3573 * off no matter what setting the socket option may have.
3574 * This option expects an integer boolean flag, where a non-zero value turns on
3575 * the option, and a zero value turns off the option.
3576 * Note. In this implementation, socket operation overrides default parameter
3577 * being set by sysctl as well as FreeBSD implementation
3578 */
3579static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3580 unsigned int optlen)
3581{
3582 int val;
3583 struct sctp_sock *sp = sctp_sk(sk);
3584
3585 if (optlen < sizeof(int))
3586 return -EINVAL;
3587 if (get_user(val, (int __user *)optval))
3588 return -EFAULT;
3589 if (!sctp_is_ep_boundall(sk) && val)
3590 return -EINVAL;
3591 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3592 return 0;
3593
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003594 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003595 if (val == 0 && sp->do_auto_asconf) {
3596 list_del(&sp->auto_asconf_list);
3597 sp->do_auto_asconf = 0;
3598 } else if (val && !sp->do_auto_asconf) {
3599 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00003600 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09003601 sp->do_auto_asconf = 1;
3602 }
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003603 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003604 return 0;
3605}
3606
Neil Horman5aa93bc2012-07-21 07:56:07 +00003607/*
3608 * SCTP_PEER_ADDR_THLDS
3609 *
3610 * This option allows us to alter the partially failed threshold for one or all
3611 * transports in an association. See Section 6.1 of:
3612 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3613 */
3614static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3615 char __user *optval,
3616 unsigned int optlen)
3617{
3618 struct sctp_paddrthlds val;
3619 struct sctp_transport *trans;
3620 struct sctp_association *asoc;
3621
3622 if (optlen < sizeof(struct sctp_paddrthlds))
3623 return -EINVAL;
3624 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3625 sizeof(struct sctp_paddrthlds)))
3626 return -EFAULT;
3627
3628
3629 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3630 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3631 if (!asoc)
3632 return -ENOENT;
3633 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3634 transports) {
3635 if (val.spt_pathmaxrxt)
3636 trans->pathmaxrxt = val.spt_pathmaxrxt;
3637 trans->pf_retrans = val.spt_pathpfthld;
3638 }
3639
3640 if (val.spt_pathmaxrxt)
3641 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3642 asoc->pf_retrans = val.spt_pathpfthld;
3643 } else {
3644 trans = sctp_addr_id2transport(sk, &val.spt_address,
3645 val.spt_assoc_id);
3646 if (!trans)
3647 return -ENOENT;
3648
3649 if (val.spt_pathmaxrxt)
3650 trans->pathmaxrxt = val.spt_pathmaxrxt;
3651 trans->pf_retrans = val.spt_pathpfthld;
3652 }
3653
3654 return 0;
3655}
3656
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02003657static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3658 char __user *optval,
3659 unsigned int optlen)
3660{
3661 int val;
3662
3663 if (optlen < sizeof(int))
3664 return -EINVAL;
3665 if (get_user(val, (int __user *) optval))
3666 return -EFAULT;
3667
3668 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3669
3670 return 0;
3671}
3672
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02003673static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3674 char __user *optval,
3675 unsigned int optlen)
3676{
3677 int val;
3678
3679 if (optlen < sizeof(int))
3680 return -EINVAL;
3681 if (get_user(val, (int __user *) optval))
3682 return -EFAULT;
3683
3684 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3685
3686 return 0;
3687}
3688
Xin Long28aa4c22016-07-09 19:47:40 +08003689static int sctp_setsockopt_pr_supported(struct sock *sk,
3690 char __user *optval,
3691 unsigned int optlen)
3692{
3693 struct sctp_assoc_value params;
3694 struct sctp_association *asoc;
3695 int retval = -EINVAL;
3696
3697 if (optlen != sizeof(params))
3698 goto out;
3699
3700 if (copy_from_user(&params, optval, optlen)) {
3701 retval = -EFAULT;
3702 goto out;
3703 }
3704
3705 asoc = sctp_id2assoc(sk, params.assoc_id);
3706 if (asoc) {
3707 asoc->prsctp_enable = !!params.assoc_value;
3708 } else if (!params.assoc_id) {
3709 struct sctp_sock *sp = sctp_sk(sk);
3710
3711 sp->ep->prsctp_enable = !!params.assoc_value;
3712 } else {
3713 goto out;
3714 }
3715
3716 retval = 0;
3717
3718out:
3719 return retval;
3720}
3721
Xin Longf959fb42016-07-09 19:47:41 +08003722static int sctp_setsockopt_default_prinfo(struct sock *sk,
3723 char __user *optval,
3724 unsigned int optlen)
3725{
3726 struct sctp_default_prinfo info;
3727 struct sctp_association *asoc;
3728 int retval = -EINVAL;
3729
3730 if (optlen != sizeof(info))
3731 goto out;
3732
3733 if (copy_from_user(&info, optval, sizeof(info))) {
3734 retval = -EFAULT;
3735 goto out;
3736 }
3737
3738 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
3739 goto out;
3740
3741 if (info.pr_policy == SCTP_PR_SCTP_NONE)
3742 info.pr_value = 0;
3743
3744 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
3745 if (asoc) {
3746 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
3747 asoc->default_timetolive = info.pr_value;
3748 } else if (!info.pr_assoc_id) {
3749 struct sctp_sock *sp = sctp_sk(sk);
3750
3751 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
3752 sp->default_timetolive = info.pr_value;
3753 } else {
3754 goto out;
3755 }
3756
3757 retval = 0;
3758
3759out:
3760 return retval;
3761}
3762
Xin Longc0d8bab2017-03-10 12:11:12 +08003763static int sctp_setsockopt_reconfig_supported(struct sock *sk,
3764 char __user *optval,
3765 unsigned int optlen)
3766{
3767 struct sctp_assoc_value params;
3768 struct sctp_association *asoc;
3769 int retval = -EINVAL;
3770
3771 if (optlen != sizeof(params))
3772 goto out;
3773
3774 if (copy_from_user(&params, optval, optlen)) {
3775 retval = -EFAULT;
3776 goto out;
3777 }
3778
3779 asoc = sctp_id2assoc(sk, params.assoc_id);
3780 if (asoc) {
3781 asoc->reconf_enable = !!params.assoc_value;
3782 } else if (!params.assoc_id) {
3783 struct sctp_sock *sp = sctp_sk(sk);
3784
3785 sp->ep->reconf_enable = !!params.assoc_value;
3786 } else {
3787 goto out;
3788 }
3789
3790 retval = 0;
3791
3792out:
3793 return retval;
3794}
3795
Xin Long9fb657a2017-01-18 00:44:46 +08003796static int sctp_setsockopt_enable_strreset(struct sock *sk,
3797 char __user *optval,
3798 unsigned int optlen)
3799{
3800 struct sctp_assoc_value params;
3801 struct sctp_association *asoc;
3802 int retval = -EINVAL;
3803
3804 if (optlen != sizeof(params))
3805 goto out;
3806
3807 if (copy_from_user(&params, optval, optlen)) {
3808 retval = -EFAULT;
3809 goto out;
3810 }
3811
3812 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
3813 goto out;
3814
3815 asoc = sctp_id2assoc(sk, params.assoc_id);
3816 if (asoc) {
3817 asoc->strreset_enable = params.assoc_value;
3818 } else if (!params.assoc_id) {
3819 struct sctp_sock *sp = sctp_sk(sk);
3820
3821 sp->ep->strreset_enable = params.assoc_value;
3822 } else {
3823 goto out;
3824 }
3825
3826 retval = 0;
3827
3828out:
3829 return retval;
3830}
3831
Xin Long7f9d68a2017-01-18 00:44:47 +08003832static int sctp_setsockopt_reset_streams(struct sock *sk,
3833 char __user *optval,
3834 unsigned int optlen)
3835{
3836 struct sctp_reset_streams *params;
3837 struct sctp_association *asoc;
3838 int retval = -EINVAL;
3839
Xin Long2342b8d2017-12-10 15:40:51 +08003840 if (optlen < sizeof(*params))
Xin Long7f9d68a2017-01-18 00:44:47 +08003841 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003842 /* srs_number_streams is u16, so optlen can't be bigger than this. */
3843 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3844 sizeof(__u16) * sizeof(*params));
Xin Long7f9d68a2017-01-18 00:44:47 +08003845
3846 params = memdup_user(optval, optlen);
3847 if (IS_ERR(params))
3848 return PTR_ERR(params);
3849
Xin Long2342b8d2017-12-10 15:40:51 +08003850 if (params->srs_number_streams * sizeof(__u16) >
3851 optlen - sizeof(*params))
3852 goto out;
3853
Xin Long7f9d68a2017-01-18 00:44:47 +08003854 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
3855 if (!asoc)
3856 goto out;
3857
3858 retval = sctp_send_reset_streams(asoc, params);
3859
3860out:
3861 kfree(params);
3862 return retval;
3863}
3864
Xin Longa92ce1a2017-02-09 01:18:18 +08003865static int sctp_setsockopt_reset_assoc(struct sock *sk,
3866 char __user *optval,
3867 unsigned int optlen)
3868{
3869 struct sctp_association *asoc;
3870 sctp_assoc_t associd;
3871 int retval = -EINVAL;
3872
3873 if (optlen != sizeof(associd))
3874 goto out;
3875
3876 if (copy_from_user(&associd, optval, optlen)) {
3877 retval = -EFAULT;
3878 goto out;
3879 }
3880
3881 asoc = sctp_id2assoc(sk, associd);
3882 if (!asoc)
3883 goto out;
3884
3885 retval = sctp_send_reset_assoc(asoc);
3886
3887out:
3888 return retval;
3889}
3890
Xin Long242bd2d2017-02-09 01:18:20 +08003891static int sctp_setsockopt_add_streams(struct sock *sk,
3892 char __user *optval,
3893 unsigned int optlen)
3894{
3895 struct sctp_association *asoc;
3896 struct sctp_add_streams params;
3897 int retval = -EINVAL;
3898
3899 if (optlen != sizeof(params))
3900 goto out;
3901
3902 if (copy_from_user(&params, optval, optlen)) {
3903 retval = -EFAULT;
3904 goto out;
3905 }
3906
3907 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
3908 if (!asoc)
3909 goto out;
3910
3911 retval = sctp_send_add_streams(asoc, &params);
3912
3913out:
3914 return retval;
3915}
3916
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03003917static int sctp_setsockopt_scheduler(struct sock *sk,
3918 char __user *optval,
3919 unsigned int optlen)
3920{
3921 struct sctp_association *asoc;
3922 struct sctp_assoc_value params;
3923 int retval = -EINVAL;
3924
3925 if (optlen < sizeof(params))
3926 goto out;
3927
3928 optlen = sizeof(params);
3929 if (copy_from_user(&params, optval, optlen)) {
3930 retval = -EFAULT;
3931 goto out;
3932 }
3933
3934 if (params.assoc_value > SCTP_SS_MAX)
3935 goto out;
3936
3937 asoc = sctp_id2assoc(sk, params.assoc_id);
3938 if (!asoc)
3939 goto out;
3940
3941 retval = sctp_sched_set_sched(asoc, params.assoc_value);
3942
3943out:
3944 return retval;
3945}
3946
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03003947static int sctp_setsockopt_scheduler_value(struct sock *sk,
3948 char __user *optval,
3949 unsigned int optlen)
3950{
3951 struct sctp_association *asoc;
3952 struct sctp_stream_value params;
3953 int retval = -EINVAL;
3954
3955 if (optlen < sizeof(params))
3956 goto out;
3957
3958 optlen = sizeof(params);
3959 if (copy_from_user(&params, optval, optlen)) {
3960 retval = -EFAULT;
3961 goto out;
3962 }
3963
3964 asoc = sctp_id2assoc(sk, params.assoc_id);
3965 if (!asoc)
3966 goto out;
3967
3968 retval = sctp_sched_set_value(asoc, params.stream_id,
3969 params.stream_value, GFP_KERNEL);
3970
3971out:
3972 return retval;
3973}
3974
Xin Long772a5862017-12-08 21:03:58 +08003975static int sctp_setsockopt_interleaving_supported(struct sock *sk,
3976 char __user *optval,
3977 unsigned int optlen)
3978{
3979 struct sctp_sock *sp = sctp_sk(sk);
3980 struct net *net = sock_net(sk);
3981 struct sctp_assoc_value params;
3982 int retval = -EINVAL;
3983
3984 if (optlen < sizeof(params))
3985 goto out;
3986
3987 optlen = sizeof(params);
3988 if (copy_from_user(&params, optval, optlen)) {
3989 retval = -EFAULT;
3990 goto out;
3991 }
3992
3993 if (params.assoc_id)
3994 goto out;
3995
3996 if (!net->sctp.intl_enable || !sp->frag_interleave) {
3997 retval = -EPERM;
3998 goto out;
3999 }
4000
4001 sp->strm_interleave = !!params.assoc_value;
4002
4003 retval = 0;
4004
4005out:
4006 return retval;
4007}
4008
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009/* API 6.2 setsockopt(), getsockopt()
4010 *
4011 * Applications use setsockopt() and getsockopt() to set or retrieve
4012 * socket options. Socket options are used to change the default
4013 * behavior of sockets calls. They are described in Section 7.
4014 *
4015 * The syntax is:
4016 *
4017 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4018 * int __user *optlen);
4019 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4020 * int optlen);
4021 *
4022 * sd - the socket descript.
4023 * level - set to IPPROTO_SCTP for all SCTP options.
4024 * optname - the option name.
4025 * optval - the buffer to store the value of the option.
4026 * optlen - the size of the buffer.
4027 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004028static int sctp_setsockopt(struct sock *sk, int level, int optname,
4029 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030{
4031 int retval = 0;
4032
Daniel Borkmannbb333812013-06-28 19:49:40 +02004033 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
4035 /* I can hardly begin to describe how wrong this is. This is
4036 * so broken as to be worse than useless. The API draft
4037 * REALLY is NOT helpful here... I am not convinced that the
4038 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4039 * are at all well-founded.
4040 */
4041 if (level != SOL_SCTP) {
4042 struct sctp_af *af = sctp_sk(sk)->pf->af;
4043 retval = af->setsockopt(sk, level, optname, optval, optlen);
4044 goto out_nounlock;
4045 }
4046
wangweidong048ed4b2014-01-21 15:44:11 +08004047 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
4049 switch (optname) {
4050 case SCTP_SOCKOPT_BINDX_ADD:
4051 /* 'optlen' is the size of the addresses buffer. */
4052 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4053 optlen, SCTP_BINDX_ADD_ADDR);
4054 break;
4055
4056 case SCTP_SOCKOPT_BINDX_REM:
4057 /* 'optlen' is the size of the addresses buffer. */
4058 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4059 optlen, SCTP_BINDX_REM_ADDR);
4060 break;
4061
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004062 case SCTP_SOCKOPT_CONNECTX_OLD:
4063 /* 'optlen' is the size of the addresses buffer. */
4064 retval = sctp_setsockopt_connectx_old(sk,
4065 (struct sockaddr __user *)optval,
4066 optlen);
4067 break;
4068
Frank Filz3f7a87d2005-06-20 13:14:57 -07004069 case SCTP_SOCKOPT_CONNECTX:
4070 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004071 retval = sctp_setsockopt_connectx(sk,
4072 (struct sockaddr __user *)optval,
4073 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004074 break;
4075
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 case SCTP_DISABLE_FRAGMENTS:
4077 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4078 break;
4079
4080 case SCTP_EVENTS:
4081 retval = sctp_setsockopt_events(sk, optval, optlen);
4082 break;
4083
4084 case SCTP_AUTOCLOSE:
4085 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4086 break;
4087
4088 case SCTP_PEER_ADDR_PARAMS:
4089 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4090 break;
4091
Shan Wei4580ccc2011-01-18 22:39:00 +00004092 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07004093 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08004094 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004095 case SCTP_PARTIAL_DELIVERY_POINT:
4096 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4097 break;
Frank Filz77086102005-12-22 11:37:30 -08004098
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 case SCTP_INITMSG:
4100 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4101 break;
4102 case SCTP_DEFAULT_SEND_PARAM:
4103 retval = sctp_setsockopt_default_send_param(sk, optval,
4104 optlen);
4105 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02004106 case SCTP_DEFAULT_SNDINFO:
4107 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4108 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 case SCTP_PRIMARY_ADDR:
4110 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4111 break;
4112 case SCTP_SET_PEER_PRIMARY_ADDR:
4113 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4114 break;
4115 case SCTP_NODELAY:
4116 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4117 break;
4118 case SCTP_RTOINFO:
4119 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4120 break;
4121 case SCTP_ASSOCINFO:
4122 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4123 break;
4124 case SCTP_I_WANT_MAPPED_V4_ADDR:
4125 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4126 break;
4127 case SCTP_MAXSEG:
4128 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4129 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004130 case SCTP_ADAPTATION_LAYER:
4131 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004133 case SCTP_CONTEXT:
4134 retval = sctp_setsockopt_context(sk, optval, optlen);
4135 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004136 case SCTP_FRAGMENT_INTERLEAVE:
4137 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4138 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07004139 case SCTP_MAX_BURST:
4140 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4141 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004142 case SCTP_AUTH_CHUNK:
4143 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4144 break;
4145 case SCTP_HMAC_IDENT:
4146 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4147 break;
4148 case SCTP_AUTH_KEY:
4149 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4150 break;
4151 case SCTP_AUTH_ACTIVE_KEY:
4152 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4153 break;
4154 case SCTP_AUTH_DELETE_KEY:
4155 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4156 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09004157 case SCTP_AUTO_ASCONF:
4158 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4159 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00004160 case SCTP_PEER_ADDR_THLDS:
4161 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4162 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004163 case SCTP_RECVRCVINFO:
4164 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4165 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004166 case SCTP_RECVNXTINFO:
4167 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4168 break;
Xin Long28aa4c22016-07-09 19:47:40 +08004169 case SCTP_PR_SUPPORTED:
4170 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4171 break;
Xin Longf959fb42016-07-09 19:47:41 +08004172 case SCTP_DEFAULT_PRINFO:
4173 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4174 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08004175 case SCTP_RECONFIG_SUPPORTED:
4176 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4177 break;
Xin Long9fb657a2017-01-18 00:44:46 +08004178 case SCTP_ENABLE_STREAM_RESET:
4179 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4180 break;
Xin Long7f9d68a2017-01-18 00:44:47 +08004181 case SCTP_RESET_STREAMS:
4182 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4183 break;
Xin Longa92ce1a2017-02-09 01:18:18 +08004184 case SCTP_RESET_ASSOC:
4185 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4186 break;
Xin Long242bd2d2017-02-09 01:18:20 +08004187 case SCTP_ADD_STREAMS:
4188 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4189 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004190 case SCTP_STREAM_SCHEDULER:
4191 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4192 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004193 case SCTP_STREAM_SCHEDULER_VALUE:
4194 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4195 break;
Xin Long772a5862017-12-08 21:03:58 +08004196 case SCTP_INTERLEAVING_SUPPORTED:
4197 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4198 optlen);
4199 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 default:
4201 retval = -ENOPROTOOPT;
4202 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
wangweidong048ed4b2014-01-21 15:44:11 +08004205 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
4207out_nounlock:
4208 return retval;
4209}
4210
4211/* API 3.1.6 connect() - UDP Style Syntax
4212 *
4213 * An application may use the connect() call in the UDP model to initiate an
4214 * association without sending data.
4215 *
4216 * The syntax is:
4217 *
4218 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4219 *
4220 * sd: the socket descriptor to have a new association added to.
4221 *
4222 * nam: the address structure (either struct sockaddr_in or struct
4223 * sockaddr_in6 defined in RFC2553 [7]).
4224 *
4225 * len: the size of the address.
4226 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004227static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4228 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07004231 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
wangweidong048ed4b2014-01-21 15:44:11 +08004233 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
Daniel Borkmannbb333812013-06-28 19:49:40 +02004235 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4236 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Frank Filz3f7a87d2005-06-20 13:14:57 -07004238 /* Validate addr_len before calling common connect/connectx routine. */
4239 af = sctp_get_af_specific(addr->sa_family);
4240 if (!af || addr_len < af->sockaddr_len) {
4241 err = -EINVAL;
4242 } else {
4243 /* Pass correct addr len to common routine (so it knows there
4244 * is only one address being passed.
4245 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004246 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 }
4248
wangweidong048ed4b2014-01-21 15:44:11 +08004249 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 return err;
4251}
4252
4253/* FIXME: Write comments. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004254static int sctp_disconnect(struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255{
4256 return -EOPNOTSUPP; /* STUB */
4257}
4258
4259/* 4.1.4 accept() - TCP Style Syntax
4260 *
4261 * Applications use accept() call to remove an established SCTP
4262 * association from the accept queue of the endpoint. A new socket
4263 * descriptor will be returned from accept() to represent the newly
4264 * formed association.
4265 */
David Howellscdfbabf2017-03-09 08:09:05 +00004266static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267{
4268 struct sctp_sock *sp;
4269 struct sctp_endpoint *ep;
4270 struct sock *newsk = NULL;
4271 struct sctp_association *asoc;
4272 long timeo;
4273 int error = 0;
4274
wangweidong048ed4b2014-01-21 15:44:11 +08004275 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
4277 sp = sctp_sk(sk);
4278 ep = sp->ep;
4279
4280 if (!sctp_style(sk, TCP)) {
4281 error = -EOPNOTSUPP;
4282 goto out;
4283 }
4284
4285 if (!sctp_sstate(sk, LISTENING)) {
4286 error = -EINVAL;
4287 goto out;
4288 }
4289
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07004290 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
4292 error = sctp_wait_for_accept(sk, timeo);
4293 if (error)
4294 goto out;
4295
4296 /* We treat the list of associations on the endpoint as the accept
4297 * queue and pick the first association on the list.
4298 */
4299 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4300
David Howellscdfbabf2017-03-09 08:09:05 +00004301 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 if (!newsk) {
4303 error = -ENOMEM;
4304 goto out;
4305 }
4306
4307 /* Populate the fields of the newsk from the oldsk and migrate the
4308 * asoc to the newsk.
4309 */
4310 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4311
4312out:
wangweidong048ed4b2014-01-21 15:44:11 +08004313 release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004314 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 return newsk;
4316}
4317
4318/* The SCTP ioctl handler. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004319static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004321 int rc = -ENOTCONN;
4322
wangweidong048ed4b2014-01-21 15:44:11 +08004323 lock_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004324
4325 /*
4326 * SEQPACKET-style sockets in LISTENING state are valid, for
4327 * SCTP, so only discard TCP-style sockets in LISTENING state.
4328 */
4329 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4330 goto out;
4331
4332 switch (cmd) {
4333 case SIOCINQ: {
4334 struct sk_buff *skb;
4335 unsigned int amount = 0;
4336
4337 skb = skb_peek(&sk->sk_receive_queue);
4338 if (skb != NULL) {
4339 /*
4340 * We will only return the amount of this packet since
4341 * that is all that will be read.
4342 */
4343 amount = skb->len;
4344 }
4345 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004346 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07004347 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004348 default:
4349 rc = -ENOIOCTLCMD;
4350 break;
4351 }
4352out:
wangweidong048ed4b2014-01-21 15:44:11 +08004353 release_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004354 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355}
4356
4357/* This is the function which gets called during socket creation to
4358 * initialized the SCTP-specific portion of the sock.
4359 * The sock structure should already be zero-filled memory.
4360 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004361static int sctp_init_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004363 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 struct sctp_sock *sp;
4365
Daniel Borkmannbb333812013-06-28 19:49:40 +02004366 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
4368 sp = sctp_sk(sk);
4369
4370 /* Initialize the SCTP per socket area. */
4371 switch (sk->sk_type) {
4372 case SOCK_SEQPACKET:
4373 sp->type = SCTP_SOCKET_UDP;
4374 break;
4375 case SOCK_STREAM:
4376 sp->type = SCTP_SOCKET_TCP;
4377 break;
4378 default:
4379 return -ESOCKTNOSUPPORT;
4380 }
4381
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004382 sk->sk_gso_type = SKB_GSO_SCTP;
4383
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 /* Initialize default send parameters. These parameters can be
4385 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4386 */
4387 sp->default_stream = 0;
4388 sp->default_ppid = 0;
4389 sp->default_flags = 0;
4390 sp->default_context = 0;
4391 sp->default_timetolive = 0;
4392
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004393 sp->default_rcv_context = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004394 sp->max_burst = net->sctp.max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004395
Neil Horman3c681982012-10-24 09:20:03 +00004396 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4397
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 /* Initialize default setup parameters. These parameters
4399 * can be modified with the SCTP_INITMSG socket option or
4400 * overridden by the SCTP_INIT CMSG.
4401 */
4402 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4403 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004404 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4405 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406
4407 /* Initialize default RTO related parameters. These parameters can
4408 * be modified for with the SCTP_RTOINFO socket option.
4409 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004410 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4411 sp->rtoinfo.srto_max = net->sctp.rto_max;
4412 sp->rtoinfo.srto_min = net->sctp.rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
4414 /* Initialize default association related parameters. These parameters
4415 * can be modified with the SCTP_ASSOCINFO socket option.
4416 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004417 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 sp->assocparams.sasoc_number_peer_destinations = 0;
4419 sp->assocparams.sasoc_peer_rwnd = 0;
4420 sp->assocparams.sasoc_local_rwnd = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004421 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
4423 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004424 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 */
4426 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4427
4428 /* Default Peer Address Parameters. These defaults can
4429 * be modified via SCTP_PEER_ADDR_PARAMS
4430 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004431 sp->hbinterval = net->sctp.hb_interval;
4432 sp->pathmaxrxt = net->sctp.max_retrans_path;
wangweidong4e2d52b2013-12-23 12:16:54 +08004433 sp->pathmtu = 0; /* allow default discovery */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004434 sp->sackdelay = net->sctp.sack_timeout;
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07004435 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004436 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004437 SPP_PMTUD_ENABLE |
4438 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
4440 /* If enabled no SCTP message fragmentation will be performed.
4441 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4442 */
4443 sp->disable_fragments = 0;
4444
Sridhar Samudrala208edef2006-09-29 17:08:01 -07004445 /* Enable Nagle algorithm by default. */
4446 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004448 sp->recvrcvinfo = 0;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004449 sp->recvnxtinfo = 0;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004450
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 /* Enable by default. */
4452 sp->v4mapped = 1;
4453
4454 /* Auto-close idle associations after the configured
4455 * number of seconds. A value of 0 disables this
4456 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4457 * for UDP-style sockets only.
4458 */
4459 sp->autoclose = 0;
4460
4461 /* User specified fragmentation limit. */
4462 sp->user_frag = 0;
4463
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004464 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465
4466 sp->pf = sctp_get_pf_specific(sk->sk_family);
4467
4468 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004469 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004471 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
4473 /* Create a per socket endpoint structure. Even if we
4474 * change the data structure relationships, this may still
4475 * be useful for storing pre-connect address information.
4476 */
Daniel Borkmannc164b832013-06-14 18:24:06 +02004477 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4478 if (!sp->ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 return -ENOMEM;
4480
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 sp->hmac = NULL;
4482
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004483 sk->sk_destruct = sctp_destruct_sock;
4484
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08004486
4487 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004488 sk_sockets_allocated_inc(sk);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004489 sock_prot_inuse_add(net, sk->sk_prot, 1);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004490
4491 /* Nothing can fail after this block, otherwise
4492 * sctp_destroy_sock() will be called without addr_wq_lock held
4493 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004494 if (net->sctp.default_auto_asconf) {
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004495 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda9f7d6532011-04-26 19:32:51 +09004496 list_add_tail(&sp->auto_asconf_list,
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004497 &net->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09004498 sp->do_auto_asconf = 1;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004499 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4500 } else {
Michio Honda9f7d6532011-04-26 19:32:51 +09004501 sp->do_auto_asconf = 0;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004502 }
4503
David S. Miller6f756a82008-11-23 17:34:03 -08004504 local_bh_enable();
4505
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 return 0;
4507}
4508
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004509/* Cleanup any SCTP per socket resources. Must be called with
4510 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4511 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004512static void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513{
Michio Honda9f7d6532011-04-26 19:32:51 +09004514 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Daniel Borkmannbb333812013-06-28 19:49:40 +02004516 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517
4518 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09004519 sp = sctp_sk(sk);
Daniel Borkmann1abd1652013-06-06 15:53:47 +02004520 /* This could happen during socket init, thus we bail out
4521 * early, since the rest of the below is not setup either.
4522 */
4523 if (sp->ep == NULL)
4524 return;
4525
Michio Honda9f7d6532011-04-26 19:32:51 +09004526 if (sp->do_auto_asconf) {
4527 sp->do_auto_asconf = 0;
4528 list_del(&sp->auto_asconf_list);
4529 }
4530 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004531 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004532 sk_sockets_allocated_dec(sk);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08004533 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004534 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535}
4536
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004537/* Triggered when there are no references on the socket anymore */
4538static void sctp_destruct_sock(struct sock *sk)
4539{
4540 struct sctp_sock *sp = sctp_sk(sk);
4541
4542 /* Free up the HMAC transform. */
Herbert Xu5821c762016-01-24 21:20:12 +08004543 crypto_free_shash(sp->hmac);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004544
4545 inet_sock_destruct(sk);
4546}
4547
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548/* API 4.1.7 shutdown() - TCP Style Syntax
4549 * int shutdown(int socket, int how);
4550 *
4551 * sd - the socket descriptor of the association to be closed.
4552 * how - Specifies the type of shutdown. The values are
4553 * as follows:
4554 * SHUT_RD
4555 * Disables further receive operations. No SCTP
4556 * protocol action is taken.
4557 * SHUT_WR
4558 * Disables further send operations, and initiates
4559 * the SCTP shutdown sequence.
4560 * SHUT_RDWR
4561 * Disables further send and receive operations
4562 * and initiates the SCTP shutdown sequence.
4563 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004564static void sctp_shutdown(struct sock *sk, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004566 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 struct sctp_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
4569 if (!sctp_style(sk, TCP))
4570 return;
4571
Xin Long5bf35dd2016-11-13 21:44:37 +08004572 ep = sctp_sk(sk)->ep;
4573 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4574 struct sctp_association *asoc;
4575
Yafang Shaocbabf462017-12-20 11:12:54 +08004576 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Xin Long5bf35dd2016-11-13 21:44:37 +08004577 asoc = list_entry(ep->asocs.next,
4578 struct sctp_association, asocs);
4579 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 }
4581}
4582
Xin Long52c52a62016-04-14 15:35:30 +08004583int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4584 struct sctp_info *info)
4585{
4586 struct sctp_transport *prim;
4587 struct list_head *pos;
4588 int mask;
4589
4590 memset(info, 0, sizeof(*info));
4591 if (!asoc) {
4592 struct sctp_sock *sp = sctp_sk(sk);
4593
4594 info->sctpi_s_autoclose = sp->autoclose;
4595 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4596 info->sctpi_s_pd_point = sp->pd_point;
4597 info->sctpi_s_nodelay = sp->nodelay;
4598 info->sctpi_s_disable_fragments = sp->disable_fragments;
4599 info->sctpi_s_v4mapped = sp->v4mapped;
4600 info->sctpi_s_frag_interleave = sp->frag_interleave;
Xin Long40eb90e92016-05-29 17:42:13 +08004601 info->sctpi_s_type = sp->type;
Xin Long52c52a62016-04-14 15:35:30 +08004602
4603 return 0;
4604 }
4605
4606 info->sctpi_tag = asoc->c.my_vtag;
4607 info->sctpi_state = asoc->state;
4608 info->sctpi_rwnd = asoc->a_rwnd;
4609 info->sctpi_unackdata = asoc->unack_data;
4610 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004611 info->sctpi_instrms = asoc->stream.incnt;
4612 info->sctpi_outstrms = asoc->stream.outcnt;
Xin Long52c52a62016-04-14 15:35:30 +08004613 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4614 info->sctpi_inqueue++;
4615 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4616 info->sctpi_outqueue++;
4617 info->sctpi_overall_error = asoc->overall_error_count;
4618 info->sctpi_max_burst = asoc->max_burst;
4619 info->sctpi_maxseg = asoc->frag_point;
4620 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4621 info->sctpi_peer_tag = asoc->c.peer_vtag;
4622
4623 mask = asoc->peer.ecn_capable << 1;
4624 mask = (mask | asoc->peer.ipv4_address) << 1;
4625 mask = (mask | asoc->peer.ipv6_address) << 1;
4626 mask = (mask | asoc->peer.hostname_address) << 1;
4627 mask = (mask | asoc->peer.asconf_capable) << 1;
4628 mask = (mask | asoc->peer.prsctp_capable) << 1;
4629 mask = (mask | asoc->peer.auth_capable);
4630 info->sctpi_peer_capable = mask;
4631 mask = asoc->peer.sack_needed << 1;
4632 mask = (mask | asoc->peer.sack_generation) << 1;
4633 mask = (mask | asoc->peer.zero_window_announced);
4634 info->sctpi_peer_sack = mask;
4635
4636 info->sctpi_isacks = asoc->stats.isacks;
4637 info->sctpi_osacks = asoc->stats.osacks;
4638 info->sctpi_opackets = asoc->stats.opackets;
4639 info->sctpi_ipackets = asoc->stats.ipackets;
4640 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4641 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4642 info->sctpi_idupchunks = asoc->stats.idupchunks;
4643 info->sctpi_gapcnt = asoc->stats.gapcnt;
4644 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4645 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4646 info->sctpi_oodchunks = asoc->stats.oodchunks;
4647 info->sctpi_iodchunks = asoc->stats.iodchunks;
4648 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4649 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4650
4651 prim = asoc->peer.primary_path;
Stefano Brivioee6c88b2017-08-23 13:27:13 +02004652 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
Xin Long52c52a62016-04-14 15:35:30 +08004653 info->sctpi_p_state = prim->state;
4654 info->sctpi_p_cwnd = prim->cwnd;
4655 info->sctpi_p_srtt = prim->srtt;
4656 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4657 info->sctpi_p_hbinterval = prim->hbinterval;
4658 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4659 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4660 info->sctpi_p_ssthresh = prim->ssthresh;
4661 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4662 info->sctpi_p_flight_size = prim->flight_size;
4663 info->sctpi_p_error = prim->error_count;
4664
4665 return 0;
4666}
4667EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4668
Xin Long626d16f2016-04-14 15:35:31 +08004669/* use callback to avoid exporting the core structure */
Tom Herbert97a6ec42017-12-04 10:31:41 -08004670void sctp_transport_walk_start(struct rhashtable_iter *iter)
Xin Long626d16f2016-04-14 15:35:31 +08004671{
Xin Long7fda702f2016-11-15 23:23:11 +08004672 rhltable_walk_enter(&sctp_transport_hashtable, iter);
Xin Long626d16f2016-04-14 15:35:31 +08004673
Tom Herbert97a6ec42017-12-04 10:31:41 -08004674 rhashtable_walk_start(iter);
Xin Long626d16f2016-04-14 15:35:31 +08004675}
4676
4677void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4678{
4679 rhashtable_walk_stop(iter);
4680 rhashtable_walk_exit(iter);
4681}
4682
4683struct sctp_transport *sctp_transport_get_next(struct net *net,
4684 struct rhashtable_iter *iter)
4685{
4686 struct sctp_transport *t;
4687
4688 t = rhashtable_walk_next(iter);
4689 for (; t; t = rhashtable_walk_next(iter)) {
4690 if (IS_ERR(t)) {
4691 if (PTR_ERR(t) == -EAGAIN)
4692 continue;
4693 break;
4694 }
4695
4696 if (net_eq(sock_net(t->asoc->base.sk), net) &&
4697 t->asoc->peer.primary_path == t)
4698 break;
4699 }
4700
4701 return t;
4702}
4703
4704struct sctp_transport *sctp_transport_get_idx(struct net *net,
4705 struct rhashtable_iter *iter,
4706 int pos)
4707{
4708 void *obj = SEQ_START_TOKEN;
4709
4710 while (pos && (obj = sctp_transport_get_next(net, iter)) &&
4711 !IS_ERR(obj))
4712 pos--;
4713
4714 return obj;
4715}
4716
4717int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
4718 void *p) {
4719 int err = 0;
4720 int hash = 0;
4721 struct sctp_ep_common *epb;
4722 struct sctp_hashbucket *head;
4723
4724 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
4725 hash++, head++) {
Xin Long581409d2017-06-10 14:48:14 +08004726 read_lock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004727 sctp_for_each_hentry(epb, &head->chain) {
4728 err = cb(sctp_ep(epb), p);
4729 if (err)
4730 break;
4731 }
Xin Long581409d2017-06-10 14:48:14 +08004732 read_unlock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004733 }
4734
4735 return err;
4736}
4737EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
4738
4739int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
4740 struct net *net,
4741 const union sctp_addr *laddr,
4742 const union sctp_addr *paddr, void *p)
4743{
4744 struct sctp_transport *transport;
Xin Long08abb792016-12-15 23:05:52 +08004745 int err;
Xin Long626d16f2016-04-14 15:35:31 +08004746
4747 rcu_read_lock();
4748 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
Xin Long626d16f2016-04-14 15:35:31 +08004749 rcu_read_unlock();
Xin Long08abb792016-12-15 23:05:52 +08004750 if (!transport)
4751 return -ENOENT;
4752
Xin Long1cceda782016-09-29 02:55:44 +08004753 err = cb(transport, p);
Xin Longcd26da42016-10-31 20:32:31 +08004754 sctp_transport_put(transport);
Xin Long1cceda782016-09-29 02:55:44 +08004755
Xin Long626d16f2016-04-14 15:35:31 +08004756 return err;
4757}
4758EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
4759
4760int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
Xin Longd25adbe2017-09-15 11:02:21 +08004761 int (*cb_done)(struct sctp_transport *, void *),
4762 struct net *net, int *pos, void *p) {
Xin Long626d16f2016-04-14 15:35:31 +08004763 struct rhashtable_iter hti;
Xin Longd25adbe2017-09-15 11:02:21 +08004764 struct sctp_transport *tsp;
4765 int ret;
Xin Long626d16f2016-04-14 15:35:31 +08004766
Xin Longd25adbe2017-09-15 11:02:21 +08004767again:
Xin Longf53d77e2018-01-23 18:22:25 +08004768 ret = 0;
Tom Herbert97a6ec42017-12-04 10:31:41 -08004769 sctp_transport_walk_start(&hti);
Xin Long626d16f2016-04-14 15:35:31 +08004770
Xin Longd25adbe2017-09-15 11:02:21 +08004771 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
4772 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
4773 if (!sctp_transport_hold(tsp))
Xin Long626d16f2016-04-14 15:35:31 +08004774 continue;
Xin Longd25adbe2017-09-15 11:02:21 +08004775 ret = cb(tsp, p);
4776 if (ret)
Xin Long626d16f2016-04-14 15:35:31 +08004777 break;
Xin Longd25adbe2017-09-15 11:02:21 +08004778 (*pos)++;
4779 sctp_transport_put(tsp);
Xin Long626d16f2016-04-14 15:35:31 +08004780 }
Xin Long626d16f2016-04-14 15:35:31 +08004781 sctp_transport_walk_stop(&hti);
Xin Long53fa1032016-04-14 15:35:35 +08004782
Xin Longd25adbe2017-09-15 11:02:21 +08004783 if (ret) {
4784 if (cb_done && !cb_done(tsp, p)) {
4785 (*pos)++;
4786 sctp_transport_put(tsp);
4787 goto again;
4788 }
4789 sctp_transport_put(tsp);
4790 }
4791
4792 return ret;
Xin Long626d16f2016-04-14 15:35:31 +08004793}
4794EXPORT_SYMBOL_GPL(sctp_for_each_transport);
4795
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796/* 7.2.1 Association Status (SCTP_STATUS)
4797
4798 * Applications can retrieve current status information about an
4799 * association, including association state, peer receiver window size,
4800 * number of unacked data chunks, and number of data chunks pending
4801 * receipt. This information is read-only.
4802 */
4803static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4804 char __user *optval,
4805 int __user *optlen)
4806{
4807 struct sctp_status status;
4808 struct sctp_association *asoc = NULL;
4809 struct sctp_transport *transport;
4810 sctp_assoc_t associd;
4811 int retval = 0;
4812
Neil Horman408f22e2007-06-16 14:03:45 -04004813 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 retval = -EINVAL;
4815 goto out;
4816 }
4817
Neil Horman408f22e2007-06-16 14:03:45 -04004818 len = sizeof(status);
4819 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 retval = -EFAULT;
4821 goto out;
4822 }
4823
4824 associd = status.sstat_assoc_id;
4825 asoc = sctp_id2assoc(sk, associd);
4826 if (!asoc) {
4827 retval = -EINVAL;
4828 goto out;
4829 }
4830
4831 transport = asoc->peer.primary_path;
4832
4833 status.sstat_assoc_id = sctp_assoc2id(asoc);
Daniel Borkmann38ab1fa2014-08-28 15:28:26 +02004834 status.sstat_state = sctp_assoc_to_state(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 status.sstat_rwnd = asoc->peer.rwnd;
4836 status.sstat_unackdata = asoc->unack_data;
4837
4838 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004839 status.sstat_instrms = asoc->stream.incnt;
4840 status.sstat_outstrms = asoc->stream.outcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 status.sstat_fragmentation_point = asoc->frag_point;
4842 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08004843 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4844 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 /* Map ipv4 address into v4-mapped-on-v6 address. */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06004846 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004848 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4850 status.sstat_primary.spinfo_srtt = transport->srtt;
4851 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004852 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
Frank Filz3f7a87d2005-06-20 13:14:57 -07004854 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4855 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4856
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 if (put_user(len, optlen)) {
4858 retval = -EFAULT;
4859 goto out;
4860 }
4861
Daniel Borkmannbb333812013-06-28 19:49:40 +02004862 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
4863 __func__, len, status.sstat_state, status.sstat_rwnd,
4864 status.sstat_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
4866 if (copy_to_user(optval, &status, len)) {
4867 retval = -EFAULT;
4868 goto out;
4869 }
4870
4871out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004872 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873}
4874
4875
4876/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4877 *
4878 * Applications can retrieve information about a specific peer address
4879 * of an association, including its reachability state, congestion
4880 * window, and retransmission timer values. This information is
4881 * read-only.
4882 */
4883static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4884 char __user *optval,
4885 int __user *optlen)
4886{
4887 struct sctp_paddrinfo pinfo;
4888 struct sctp_transport *transport;
4889 int retval = 0;
4890
Neil Horman408f22e2007-06-16 14:03:45 -04004891 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 retval = -EINVAL;
4893 goto out;
4894 }
4895
Neil Horman408f22e2007-06-16 14:03:45 -04004896 len = sizeof(pinfo);
4897 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 retval = -EFAULT;
4899 goto out;
4900 }
4901
4902 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4903 pinfo.spinfo_assoc_id);
4904 if (!transport)
4905 return -EINVAL;
4906
4907 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004908 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 pinfo.spinfo_cwnd = transport->cwnd;
4910 pinfo.spinfo_srtt = transport->srtt;
4911 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004912 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913
Frank Filz3f7a87d2005-06-20 13:14:57 -07004914 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4915 pinfo.spinfo_state = SCTP_ACTIVE;
4916
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 if (put_user(len, optlen)) {
4918 retval = -EFAULT;
4919 goto out;
4920 }
4921
4922 if (copy_to_user(optval, &pinfo, len)) {
4923 retval = -EFAULT;
4924 goto out;
4925 }
4926
4927out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004928 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929}
4930
4931/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4932 *
4933 * This option is a on/off flag. If enabled no SCTP message
4934 * fragmentation will be performed. Instead if a message being sent
4935 * exceeds the current PMTU size, the message will NOT be sent and
4936 * instead a error will be indicated to the user.
4937 */
4938static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4939 char __user *optval, int __user *optlen)
4940{
4941 int val;
4942
4943 if (len < sizeof(int))
4944 return -EINVAL;
4945
4946 len = sizeof(int);
4947 val = (sctp_sk(sk)->disable_fragments == 1);
4948 if (put_user(len, optlen))
4949 return -EFAULT;
4950 if (copy_to_user(optval, &val, len))
4951 return -EFAULT;
4952 return 0;
4953}
4954
4955/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4956 *
4957 * This socket option is used to specify various notifications and
4958 * ancillary data the user wishes to receive.
4959 */
4960static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
4961 int __user *optlen)
4962{
Jiri Slabya4b8e712016-10-21 14:13:24 +02004963 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00004965 if (len > sizeof(struct sctp_event_subscribe))
4966 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04004967 if (put_user(len, optlen))
4968 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4970 return -EFAULT;
4971 return 0;
4972}
4973
4974/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
4975 *
4976 * This socket option is applicable to the UDP-style socket only. When
4977 * set it will cause associations that are idle for more than the
4978 * specified number of seconds to automatically close. An association
4979 * being idle is defined an association that has NOT sent or received
4980 * user data. The special value of '0' indicates that no automatic
4981 * close of any associations should be performed. The option expects an
4982 * integer defining the number of seconds of idle time before an
4983 * association is closed.
4984 */
4985static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
4986{
4987 /* Applicable to UDP-style socket only */
4988 if (sctp_style(sk, TCP))
4989 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04004990 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004992 len = sizeof(int);
4993 if (put_user(len, optlen))
4994 return -EFAULT;
David Windsorb2ce04c2017-06-10 22:50:43 -04004995 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 return -EFAULT;
4997 return 0;
4998}
4999
5000/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00005001int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002{
Benjamin Poirier0343c552012-03-08 05:55:58 +00005003 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005004 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 struct socket *sock;
5006 int err = 0;
5007
Xin Longdf80cd92017-10-17 23:26:10 +08005008 /* Do not peel off from one netns to another one. */
5009 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5010 return -EINVAL;
5011
Benjamin Poirier0343c552012-03-08 05:55:58 +00005012 if (!asoc)
5013 return -EINVAL;
5014
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 /* An association cannot be branched off from an already peeled-off
5016 * socket, nor is this supported for tcp style sockets.
5017 */
5018 if (!sctp_style(sk, UDP))
5019 return -EINVAL;
5020
5021 /* Create a new socket. */
5022 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5023 if (err < 0)
5024 return err;
5025
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005026 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005027
5028 /* Make peeled-off sockets more like 1-1 accepted sockets.
5029 * Set the daddr and initialize id to something more random
5030 */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005031 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005032
5033 /* Populate the fields of the newsk from the oldsk and migrate the
5034 * asoc to the newsk.
5035 */
5036 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005037
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 *sockp = sock;
5039
5040 return err;
5041}
Benjamin Poirier0343c552012-03-08 05:55:58 +00005042EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005044static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5045 struct file **newfile, unsigned flags)
5046{
5047 struct socket *newsock;
5048 int retval;
5049
5050 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5051 if (retval < 0)
5052 goto out;
5053
5054 /* Map the socket to an unused fd that can be returned to the user. */
5055 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5056 if (retval < 0) {
5057 sock_release(newsock);
5058 goto out;
5059 }
5060
5061 *newfile = sock_alloc_file(newsock, 0, NULL);
5062 if (IS_ERR(*newfile)) {
5063 put_unused_fd(retval);
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005064 retval = PTR_ERR(*newfile);
5065 *newfile = NULL;
5066 return retval;
5067 }
5068
5069 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5070 retval);
5071
5072 peeloff->sd = retval;
5073
5074 if (flags & SOCK_NONBLOCK)
5075 (*newfile)->f_flags |= O_NONBLOCK;
5076out:
5077 return retval;
5078}
5079
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5081{
5082 sctp_peeloff_arg_t peeloff;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005083 struct file *newfile = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
Neil Horman408f22e2007-06-16 14:03:45 -04005086 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005088 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 if (copy_from_user(&peeloff, optval, len))
5090 return -EFAULT;
5091
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005092 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 if (retval < 0)
5094 goto out;
5095
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04005097 if (put_user(len, optlen)) {
5098 fput(newfile);
5099 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04005100 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04005101 }
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005102
5103 if (copy_to_user(optval, &peeloff, len)) {
5104 fput(newfile);
5105 put_unused_fd(retval);
5106 return -EFAULT;
5107 }
5108 fd_install(retval, newfile);
5109out:
5110 return retval;
5111}
5112
5113static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5114 char __user *optval, int __user *optlen)
5115{
5116 sctp_peeloff_flags_arg_t peeloff;
5117 struct file *newfile = NULL;
5118 int retval = 0;
5119
5120 if (len < sizeof(sctp_peeloff_flags_arg_t))
5121 return -EINVAL;
5122 len = sizeof(sctp_peeloff_flags_arg_t);
5123 if (copy_from_user(&peeloff, optval, len))
5124 return -EFAULT;
5125
5126 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5127 &newfile, peeloff.flags);
5128 if (retval < 0)
5129 goto out;
5130
5131 /* Return the fd mapped to the new socket. */
5132 if (put_user(len, optlen)) {
5133 fput(newfile);
5134 put_unused_fd(retval);
5135 return -EFAULT;
5136 }
5137
Al Viro56b31d12012-08-18 00:25:51 -04005138 if (copy_to_user(optval, &peeloff, len)) {
5139 fput(newfile);
5140 put_unused_fd(retval);
5141 return -EFAULT;
5142 }
5143 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144out:
5145 return retval;
5146}
5147
5148/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5149 *
5150 * Applications can enable or disable heartbeats for any peer address of
5151 * an association, modify an address's heartbeat interval, force a
5152 * heartbeat to be sent immediately, and adjust the address's maximum
5153 * number of retransmissions sent before an address is considered
5154 * unreachable. The following structure is used to access and modify an
5155 * address's parameters:
5156 *
5157 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005158 * sctp_assoc_t spp_assoc_id;
5159 * struct sockaddr_storage spp_address;
5160 * uint32_t spp_hbinterval;
5161 * uint16_t spp_pathmaxrxt;
5162 * uint32_t spp_pathmtu;
5163 * uint32_t spp_sackdelay;
5164 * uint32_t spp_flags;
5165 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08005167 * spp_assoc_id - (one-to-many style socket) This is filled in the
5168 * application, and identifies the association for
5169 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 * spp_address - This specifies which address is of interest.
5171 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005172 * in milliseconds. If a value of zero
5173 * is present in this field then no changes are to
5174 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 * spp_pathmaxrxt - This contains the maximum number of
5176 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08005177 * considered unreachable. If a value of zero
5178 * is present in this field then no changes are to
5179 * be made to this parameter.
5180 * spp_pathmtu - When Path MTU discovery is disabled the value
5181 * specified here will be the "fixed" path mtu.
5182 * Note that if the spp_address field is empty
5183 * then all associations on this address will
5184 * have this fixed path mtu set upon them.
5185 *
5186 * spp_sackdelay - When delayed sack is enabled, this value specifies
5187 * the number of milliseconds that sacks will be delayed
5188 * for. This value will apply to all addresses of an
5189 * association if the spp_address field is empty. Note
5190 * also, that if delayed sack is enabled and this
5191 * value is set to 0, no change is made to the last
5192 * recorded delayed sack timer value.
5193 *
5194 * spp_flags - These flags are used to control various features
5195 * on an association. The flag field may contain
5196 * zero or more of the following options.
5197 *
5198 * SPP_HB_ENABLE - Enable heartbeats on the
5199 * specified address. Note that if the address
5200 * field is empty all addresses for the association
5201 * have heartbeats enabled upon them.
5202 *
5203 * SPP_HB_DISABLE - Disable heartbeats on the
5204 * speicifed address. Note that if the address
5205 * field is empty all addresses for the association
5206 * will have their heartbeats disabled. Note also
5207 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5208 * mutually exclusive, only one of these two should
5209 * be specified. Enabling both fields will have
5210 * undetermined results.
5211 *
5212 * SPP_HB_DEMAND - Request a user initiated heartbeat
5213 * to be made immediately.
5214 *
5215 * SPP_PMTUD_ENABLE - This field will enable PMTU
5216 * discovery upon the specified address. Note that
5217 * if the address feild is empty then all addresses
5218 * on the association are effected.
5219 *
5220 * SPP_PMTUD_DISABLE - This field will disable PMTU
5221 * discovery upon the specified address. Note that
5222 * if the address feild is empty then all addresses
5223 * on the association are effected. Not also that
5224 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5225 * exclusive. Enabling both will have undetermined
5226 * results.
5227 *
5228 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5229 * on delayed sack. The time specified in spp_sackdelay
5230 * is used to specify the sack delay for this address. Note
5231 * that if spp_address is empty then all addresses will
5232 * enable delayed sack and take on the sack delay
5233 * value specified in spp_sackdelay.
5234 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5235 * off delayed sack. If the spp_address field is blank then
5236 * delayed sack is disabled for the entire association. Note
5237 * also that this field is mutually exclusive to
5238 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5239 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 */
5241static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005242 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243{
Frank Filz52ccb8e2005-12-22 11:36:46 -08005244 struct sctp_paddrparams params;
5245 struct sctp_transport *trans = NULL;
5246 struct sctp_association *asoc = NULL;
5247 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248
Neil Horman408f22e2007-06-16 14:03:45 -04005249 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005251 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 if (copy_from_user(&params, optval, len))
5253 return -EFAULT;
5254
Frank Filz52ccb8e2005-12-22 11:36:46 -08005255 /* If an address other than INADDR_ANY is specified, and
5256 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 */
wangweidongcb3f8372013-12-23 12:16:50 +08005258 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005259 trans = sctp_addr_id2transport(sk, &params.spp_address,
5260 params.spp_assoc_id);
5261 if (!trans) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005262 pr_debug("%s: failed no transport\n", __func__);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005263 return -EINVAL;
5264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 }
5266
Frank Filz52ccb8e2005-12-22 11:36:46 -08005267 /* Get association, if assoc_id != 0 and the socket is a one
5268 * to many style socket, and an association was not found, then
5269 * the id was invalid.
5270 */
5271 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5272 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005273 pr_debug("%s: failed no association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08005275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276
Frank Filz52ccb8e2005-12-22 11:36:46 -08005277 if (trans) {
5278 /* Fetch transport values. */
5279 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5280 params.spp_pathmtu = trans->pathmtu;
5281 params.spp_pathmaxrxt = trans->pathmaxrxt;
5282 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283
Frank Filz52ccb8e2005-12-22 11:36:46 -08005284 /*draft-11 doesn't say what to return in spp_flags*/
5285 params.spp_flags = trans->param_flags;
5286 } else if (asoc) {
5287 /* Fetch association values. */
5288 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5289 params.spp_pathmtu = asoc->pathmtu;
5290 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5291 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
Frank Filz52ccb8e2005-12-22 11:36:46 -08005293 /*draft-11 doesn't say what to return in spp_flags*/
5294 params.spp_flags = asoc->param_flags;
5295 } else {
5296 /* Fetch socket values. */
5297 params.spp_hbinterval = sp->hbinterval;
5298 params.spp_pathmtu = sp->pathmtu;
5299 params.spp_sackdelay = sp->sackdelay;
5300 params.spp_pathmaxrxt = sp->pathmaxrxt;
5301
5302 /*draft-11 doesn't say what to return in spp_flags*/
5303 params.spp_flags = sp->param_flags;
5304 }
5305
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 if (copy_to_user(optval, &params, len))
5307 return -EFAULT;
5308
5309 if (put_user(len, optlen))
5310 return -EFAULT;
5311
5312 return 0;
5313}
5314
Wei Yongjund364d922008-05-09 15:13:26 -07005315/*
5316 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08005317 *
Wei Yongjund364d922008-05-09 15:13:26 -07005318 * This option will effect the way delayed acks are performed. This
5319 * option allows you to get or set the delayed ack time, in
5320 * milliseconds. It also allows changing the delayed ack frequency.
5321 * Changing the frequency to 1 disables the delayed sack algorithm. If
5322 * the assoc_id is 0, then this sets or gets the endpoints default
5323 * values. If the assoc_id field is non-zero, then the set or get
5324 * effects the specified association for the one to many model (the
5325 * assoc_id field is ignored by the one to one model). Note that if
5326 * sack_delay or sack_freq are 0 when setting this option, then the
5327 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08005328 *
Wei Yongjund364d922008-05-09 15:13:26 -07005329 * struct sctp_sack_info {
5330 * sctp_assoc_t sack_assoc_id;
5331 * uint32_t sack_delay;
5332 * uint32_t sack_freq;
5333 * };
Frank Filz77086102005-12-22 11:37:30 -08005334 *
Wei Yongjund364d922008-05-09 15:13:26 -07005335 * sack_assoc_id - This parameter, indicates which association the user
5336 * is performing an action upon. Note that if this field's value is
5337 * zero then the endpoints default value is changed (effecting future
5338 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08005339 *
Wei Yongjund364d922008-05-09 15:13:26 -07005340 * sack_delay - This parameter contains the number of milliseconds that
5341 * the user is requesting the delayed ACK timer be set to. Note that
5342 * this value is defined in the standard to be between 200 and 500
5343 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08005344 *
Wei Yongjund364d922008-05-09 15:13:26 -07005345 * sack_freq - This parameter contains the number of packets that must
5346 * be received before a sack is sent without waiting for the delay
5347 * timer to expire. The default value for this is 2, setting this
5348 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08005349 */
Wei Yongjund364d922008-05-09 15:13:26 -07005350static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08005351 char __user *optval,
5352 int __user *optlen)
5353{
Wei Yongjund364d922008-05-09 15:13:26 -07005354 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08005355 struct sctp_association *asoc = NULL;
5356 struct sctp_sock *sp = sctp_sk(sk);
5357
Wei Yongjund364d922008-05-09 15:13:26 -07005358 if (len >= sizeof(struct sctp_sack_info)) {
5359 len = sizeof(struct sctp_sack_info);
5360
5361 if (copy_from_user(&params, optval, len))
5362 return -EFAULT;
5363 } else if (len == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05005364 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005365 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005366 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005367 "Use struct sctp_sack_info instead\n",
5368 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07005369 if (copy_from_user(&params, optval, len))
5370 return -EFAULT;
5371 } else
wangweidongcb3f8372013-12-23 12:16:50 +08005372 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08005373
Wei Yongjund364d922008-05-09 15:13:26 -07005374 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08005375 * to many style socket, and an association was not found, then
5376 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005377 */
Wei Yongjund364d922008-05-09 15:13:26 -07005378 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5379 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08005380 return -EINVAL;
5381
5382 if (asoc) {
5383 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005384 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5385 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08005386 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07005387 params.sack_freq = asoc->sackfreq;
5388
5389 } else {
5390 params.sack_delay = 0;
5391 params.sack_freq = 1;
5392 }
Frank Filz77086102005-12-22 11:37:30 -08005393 } else {
5394 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005395 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5396 params.sack_delay = sp->sackdelay;
5397 params.sack_freq = sp->sackfreq;
5398 } else {
5399 params.sack_delay = 0;
5400 params.sack_freq = 1;
5401 }
Frank Filz77086102005-12-22 11:37:30 -08005402 }
5403
5404 if (copy_to_user(optval, &params, len))
5405 return -EFAULT;
5406
5407 if (put_user(len, optlen))
5408 return -EFAULT;
5409
5410 return 0;
5411}
5412
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5414 *
5415 * Applications can specify protocol parameters for the default association
5416 * initialization. The option name argument to setsockopt() and getsockopt()
5417 * is SCTP_INITMSG.
5418 *
5419 * Setting initialization parameters is effective only on an unconnected
5420 * socket (for UDP-style sockets only future associations are effected
5421 * by the change). With TCP-style sockets, this option is inherited by
5422 * sockets derived from a listener socket.
5423 */
5424static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5425{
Neil Horman408f22e2007-06-16 14:03:45 -04005426 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005428 len = sizeof(struct sctp_initmsg);
5429 if (put_user(len, optlen))
5430 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5432 return -EFAULT;
5433 return 0;
5434}
5435
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005437static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5438 char __user *optval, int __user *optlen)
5439{
5440 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005441 int cnt = 0;
5442 struct sctp_getaddrs getaddrs;
5443 struct sctp_transport *from;
5444 void __user *to;
5445 union sctp_addr temp;
5446 struct sctp_sock *sp = sctp_sk(sk);
5447 int addrlen;
5448 size_t space_left;
5449 int bytes_copied;
5450
5451 if (len < sizeof(struct sctp_getaddrs))
5452 return -EINVAL;
5453
5454 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5455 return -EFAULT;
5456
5457 /* For UDP-style sockets, id specifies the association to query. */
5458 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5459 if (!asoc)
5460 return -EINVAL;
5461
wangweidongcb3f8372013-12-23 12:16:50 +08005462 to = optval + offsetof(struct sctp_getaddrs, addrs);
5463 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005464
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07005465 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5466 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08005467 memcpy(&temp, &from->ipaddr, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005468 addrlen = sctp_get_pf_specific(sk->sk_family)
5469 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005470 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005471 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005472 if (copy_to_user(to, &temp, addrlen))
5473 return -EFAULT;
5474 to += addrlen;
5475 cnt++;
5476 space_left -= addrlen;
5477 }
5478
5479 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5480 return -EFAULT;
5481 bytes_copied = ((char __user *)to) - optval;
5482 if (put_user(bytes_copied, optlen))
5483 return -EFAULT;
5484
5485 return 0;
5486}
5487
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005488static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5489 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005490{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005491 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005492 union sctp_addr temp;
5493 int cnt = 0;
5494 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005495 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005496
Vlad Yasevich29303542007-09-16 16:02:12 -07005497 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005498 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07005499 if (!addr->valid)
5500 continue;
5501
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005502 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08005503 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005504 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005505 if ((PF_INET6 == sk->sk_family) &&
5506 inet_v6_ipv6only(sk) &&
5507 (AF_INET == addr->a.sa.sa_family))
5508 continue;
Al Viro6244be42006-11-20 17:21:44 -08005509 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05005510 if (!temp.v4.sin_port)
5511 temp.v4.sin_port = htons(port);
5512
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005513 addrlen = sctp_get_pf_specific(sk->sk_family)
5514 ->addr_to_user(sctp_sk(sk), &temp);
5515
Vlad Yasevich29303542007-09-16 16:02:12 -07005516 if (space_left < addrlen) {
5517 cnt = -ENOMEM;
5518 break;
5519 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005520 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08005521
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005522 to += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005523 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005524 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04005525 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005526 }
Vlad Yasevich29303542007-09-16 16:02:12 -07005527 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005528
5529 return cnt;
5530}
5531
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005533static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5534 char __user *optval, int __user *optlen)
5535{
5536 struct sctp_bind_addr *bp;
5537 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005538 int cnt = 0;
5539 struct sctp_getaddrs getaddrs;
5540 struct sctp_sockaddr_entry *addr;
5541 void __user *to;
5542 union sctp_addr temp;
5543 struct sctp_sock *sp = sctp_sk(sk);
5544 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005545 int err = 0;
5546 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005547 int bytes_copied = 0;
5548 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005549 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005550
Neil Horman408f22e2007-06-16 14:03:45 -04005551 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005552 return -EINVAL;
5553
5554 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5555 return -EFAULT;
5556
5557 /*
5558 * For UDP-style sockets, id specifies the association to query.
5559 * If the id field is set to the value '0' then the locally bound
5560 * addresses are returned without regard to any particular
5561 * association.
5562 */
5563 if (0 == getaddrs.assoc_id) {
5564 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005565 } else {
5566 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5567 if (!asoc)
5568 return -EINVAL;
5569 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005570 }
5571
wangweidongcb3f8372013-12-23 12:16:50 +08005572 to = optval + offsetof(struct sctp_getaddrs, addrs);
5573 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Neil Horman186e2342007-06-18 19:59:16 -04005574
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02005575 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005576 if (!addrs)
5577 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005578
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005579 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5580 * addresses from the global local address list.
5581 */
5582 if (sctp_list_single_entry(&bp->address_list)) {
5583 addr = list_entry(bp->address_list.next,
5584 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04005585 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005586 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5587 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005588 if (cnt < 0) {
5589 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005590 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005591 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005592 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005593 }
5594 }
5595
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005596 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005597 /* Protection on the bound address list is not needed since
5598 * in the socket option context we hold a socket lock and
5599 * thus the bound address list can't change.
5600 */
5601 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08005602 memcpy(&temp, &addr->a, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005603 addrlen = sctp_get_pf_specific(sk->sk_family)
5604 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005605 if (space_left < addrlen) {
5606 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07005607 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005608 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005609 memcpy(buf, &temp, addrlen);
5610 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005611 bytes_copied += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005612 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005613 space_left -= addrlen;
5614 }
5615
5616copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005617 if (copy_to_user(to, addrs, bytes_copied)) {
5618 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005619 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005620 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005621 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
5622 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005623 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005624 }
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02005625 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
5626 * but we can't change it anymore.
5627 */
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005628 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005629 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005630out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005631 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005632 return err;
5633}
5634
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
5636 *
5637 * Requests that the local SCTP stack use the enclosed peer address as
5638 * the association primary. The enclosed address must be one of the
5639 * association peer's addresses.
5640 */
5641static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
5642 char __user *optval, int __user *optlen)
5643{
5644 struct sctp_prim prim;
5645 struct sctp_association *asoc;
5646 struct sctp_sock *sp = sctp_sk(sk);
5647
Neil Horman408f22e2007-06-16 14:03:45 -04005648 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 return -EINVAL;
5650
Neil Horman408f22e2007-06-16 14:03:45 -04005651 len = sizeof(struct sctp_prim);
5652
5653 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 return -EFAULT;
5655
5656 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
5657 if (!asoc)
5658 return -EINVAL;
5659
5660 if (!asoc->peer.primary_path)
5661 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005662
Al Viro8cec6b82006-11-20 17:23:01 -08005663 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
5664 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005666 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 (union sctp_addr *)&prim.ssp_addr);
5668
Neil Horman408f22e2007-06-16 14:03:45 -04005669 if (put_user(len, optlen))
5670 return -EFAULT;
5671 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 return -EFAULT;
5673
5674 return 0;
5675}
5676
5677/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005678 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005680 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 * Indication parameter for all future INIT and INIT-ACK exchanges.
5682 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005683static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 char __user *optval, int __user *optlen)
5685{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005686 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
Neil Horman408f22e2007-06-16 14:03:45 -04005688 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 return -EINVAL;
5690
Neil Horman408f22e2007-06-16 14:03:45 -04005691 len = sizeof(struct sctp_setadaptation);
5692
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005693 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04005694
5695 if (put_user(len, optlen))
5696 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005697 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07005699
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 return 0;
5701}
5702
5703/*
5704 *
5705 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
5706 *
5707 * Applications that wish to use the sendto() system call may wish to
5708 * specify a default set of parameters that would normally be supplied
5709 * through the inclusion of ancillary data. This socket option allows
5710 * such an application to set the default sctp_sndrcvinfo structure.
5711
5712
5713 * The application that wishes to use this socket option simply passes
5714 * in to this call the sctp_sndrcvinfo structure defined in Section
5715 * 5.2.2) The input parameters accepted by this call include
5716 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
5717 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
5718 * to this call if the caller is using the UDP model.
5719 *
5720 * For getsockopt, it get the default sctp_sndrcvinfo structure.
5721 */
5722static int sctp_getsockopt_default_send_param(struct sock *sk,
5723 int len, char __user *optval,
5724 int __user *optlen)
5725{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005727 struct sctp_association *asoc;
5728 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005730 if (len < sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005732
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005733 len = sizeof(info);
Neil Horman408f22e2007-06-16 14:03:45 -04005734
5735 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 return -EFAULT;
5737
5738 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
5739 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
5740 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 if (asoc) {
5742 info.sinfo_stream = asoc->default_stream;
5743 info.sinfo_flags = asoc->default_flags;
5744 info.sinfo_ppid = asoc->default_ppid;
5745 info.sinfo_context = asoc->default_context;
5746 info.sinfo_timetolive = asoc->default_timetolive;
5747 } else {
5748 info.sinfo_stream = sp->default_stream;
5749 info.sinfo_flags = sp->default_flags;
5750 info.sinfo_ppid = sp->default_ppid;
5751 info.sinfo_context = sp->default_context;
5752 info.sinfo_timetolive = sp->default_timetolive;
5753 }
5754
Neil Horman408f22e2007-06-16 14:03:45 -04005755 if (put_user(len, optlen))
5756 return -EFAULT;
5757 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 return -EFAULT;
5759
5760 return 0;
5761}
5762
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005763/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
5764 * (SCTP_DEFAULT_SNDINFO)
5765 */
5766static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
5767 char __user *optval,
5768 int __user *optlen)
5769{
5770 struct sctp_sock *sp = sctp_sk(sk);
5771 struct sctp_association *asoc;
5772 struct sctp_sndinfo info;
5773
5774 if (len < sizeof(info))
5775 return -EINVAL;
5776
5777 len = sizeof(info);
5778
5779 if (copy_from_user(&info, optval, len))
5780 return -EFAULT;
5781
5782 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
5783 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
5784 return -EINVAL;
5785 if (asoc) {
5786 info.snd_sid = asoc->default_stream;
5787 info.snd_flags = asoc->default_flags;
5788 info.snd_ppid = asoc->default_ppid;
5789 info.snd_context = asoc->default_context;
5790 } else {
5791 info.snd_sid = sp->default_stream;
5792 info.snd_flags = sp->default_flags;
5793 info.snd_ppid = sp->default_ppid;
5794 info.snd_context = sp->default_context;
5795 }
5796
5797 if (put_user(len, optlen))
5798 return -EFAULT;
5799 if (copy_to_user(optval, &info, len))
5800 return -EFAULT;
5801
5802 return 0;
5803}
5804
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805/*
5806 *
5807 * 7.1.5 SCTP_NODELAY
5808 *
5809 * Turn on/off any Nagle-like algorithm. This means that packets are
5810 * generally sent as soon as possible and no unnecessary delays are
5811 * introduced, at the cost of more packets in the network. Expects an
5812 * integer boolean flag.
5813 */
5814
5815static int sctp_getsockopt_nodelay(struct sock *sk, int len,
5816 char __user *optval, int __user *optlen)
5817{
5818 int val;
5819
5820 if (len < sizeof(int))
5821 return -EINVAL;
5822
5823 len = sizeof(int);
5824 val = (sctp_sk(sk)->nodelay == 1);
5825 if (put_user(len, optlen))
5826 return -EFAULT;
5827 if (copy_to_user(optval, &val, len))
5828 return -EFAULT;
5829 return 0;
5830}
5831
5832/*
5833 *
5834 * 7.1.1 SCTP_RTOINFO
5835 *
5836 * The protocol parameters used to initialize and bound retransmission
5837 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
5838 * and modify these parameters.
5839 * All parameters are time values, in milliseconds. A value of 0, when
5840 * modifying the parameters, indicates that the current value should not
5841 * be changed.
5842 *
5843 */
5844static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
5845 char __user *optval,
5846 int __user *optlen) {
5847 struct sctp_rtoinfo rtoinfo;
5848 struct sctp_association *asoc;
5849
Neil Horman408f22e2007-06-16 14:03:45 -04005850 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 return -EINVAL;
5852
Neil Horman408f22e2007-06-16 14:03:45 -04005853 len = sizeof(struct sctp_rtoinfo);
5854
5855 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 return -EFAULT;
5857
5858 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
5859
5860 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
5861 return -EINVAL;
5862
5863 /* Values corresponding to the specific association. */
5864 if (asoc) {
5865 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
5866 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
5867 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
5868 } else {
5869 /* Values corresponding to the endpoint. */
5870 struct sctp_sock *sp = sctp_sk(sk);
5871
5872 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
5873 rtoinfo.srto_max = sp->rtoinfo.srto_max;
5874 rtoinfo.srto_min = sp->rtoinfo.srto_min;
5875 }
5876
5877 if (put_user(len, optlen))
5878 return -EFAULT;
5879
5880 if (copy_to_user(optval, &rtoinfo, len))
5881 return -EFAULT;
5882
5883 return 0;
5884}
5885
5886/*
5887 *
5888 * 7.1.2 SCTP_ASSOCINFO
5889 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02005890 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 * of the association.
5892 * Returns an error if the new association retransmission value is
5893 * greater than the sum of the retransmission value of the peer.
5894 * See [SCTP] for more information.
5895 *
5896 */
5897static int sctp_getsockopt_associnfo(struct sock *sk, int len,
5898 char __user *optval,
5899 int __user *optlen)
5900{
5901
5902 struct sctp_assocparams assocparams;
5903 struct sctp_association *asoc;
5904 struct list_head *pos;
5905 int cnt = 0;
5906
Neil Horman408f22e2007-06-16 14:03:45 -04005907 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 return -EINVAL;
5909
Neil Horman408f22e2007-06-16 14:03:45 -04005910 len = sizeof(struct sctp_assocparams);
5911
5912 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 return -EFAULT;
5914
5915 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
5916
5917 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
5918 return -EINVAL;
5919
5920 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07005921 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
5923 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
5924 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
Daniel Borkmann52db8822013-06-25 18:17:27 +02005925 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
5927 list_for_each(pos, &asoc->peer.transport_addr_list) {
wangweidongcb3f8372013-12-23 12:16:50 +08005928 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 }
5930
5931 assocparams.sasoc_number_peer_destinations = cnt;
5932 } else {
5933 /* Values corresponding to the endpoint */
5934 struct sctp_sock *sp = sctp_sk(sk);
5935
5936 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
5937 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
5938 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
5939 assocparams.sasoc_cookie_life =
5940 sp->assocparams.sasoc_cookie_life;
5941 assocparams.sasoc_number_peer_destinations =
5942 sp->assocparams.
5943 sasoc_number_peer_destinations;
5944 }
5945
5946 if (put_user(len, optlen))
5947 return -EFAULT;
5948
5949 if (copy_to_user(optval, &assocparams, len))
5950 return -EFAULT;
5951
5952 return 0;
5953}
5954
5955/*
5956 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
5957 *
5958 * This socket option is a boolean flag which turns on or off mapped V4
5959 * addresses. If this option is turned on and the socket is type
5960 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
5961 * If this option is turned off, then no mapping will be done of V4
5962 * addresses and a user will receive both PF_INET6 and PF_INET type
5963 * addresses on the socket.
5964 */
5965static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
5966 char __user *optval, int __user *optlen)
5967{
5968 int val;
5969 struct sctp_sock *sp = sctp_sk(sk);
5970
5971 if (len < sizeof(int))
5972 return -EINVAL;
5973
5974 len = sizeof(int);
5975 val = sp->v4mapped;
5976 if (put_user(len, optlen))
5977 return -EFAULT;
5978 if (copy_to_user(optval, &val, len))
5979 return -EFAULT;
5980
5981 return 0;
5982}
5983
5984/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005985 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
5986 * (chapter and verse is quoted at sctp_setsockopt_context())
5987 */
5988static int sctp_getsockopt_context(struct sock *sk, int len,
5989 char __user *optval, int __user *optlen)
5990{
5991 struct sctp_assoc_value params;
5992 struct sctp_sock *sp;
5993 struct sctp_association *asoc;
5994
Neil Horman408f22e2007-06-16 14:03:45 -04005995 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005996 return -EINVAL;
5997
Neil Horman408f22e2007-06-16 14:03:45 -04005998 len = sizeof(struct sctp_assoc_value);
5999
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006000 if (copy_from_user(&params, optval, len))
6001 return -EFAULT;
6002
6003 sp = sctp_sk(sk);
6004
6005 if (params.assoc_id != 0) {
6006 asoc = sctp_id2assoc(sk, params.assoc_id);
6007 if (!asoc)
6008 return -EINVAL;
6009 params.assoc_value = asoc->default_rcv_context;
6010 } else {
6011 params.assoc_value = sp->default_rcv_context;
6012 }
6013
6014 if (put_user(len, optlen))
6015 return -EFAULT;
6016 if (copy_to_user(optval, &params, len))
6017 return -EFAULT;
6018
6019 return 0;
6020}
6021
6022/*
Wei Yongjune89c2092008-12-25 16:54:58 -08006023 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6024 * This option will get or set the maximum size to put in any outgoing
6025 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 * fragmented by SCTP into the specified size. Note that the underlying
6027 * SCTP implementation may fragment into smaller sized chunks when the
6028 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08006029 * the user. The default value for this option is '0' which indicates
6030 * the user is NOT limiting fragmentation and only the PMTU will effect
6031 * SCTP's choice of DATA chunk size. Note also that values set larger
6032 * than the maximum size of an IP datagram will effectively let SCTP
6033 * control fragmentation (i.e. the same as setting this option to 0).
6034 *
6035 * The following structure is used to access and modify this parameter:
6036 *
6037 * struct sctp_assoc_value {
6038 * sctp_assoc_t assoc_id;
6039 * uint32_t assoc_value;
6040 * };
6041 *
6042 * assoc_id: This parameter is ignored for one-to-one style sockets.
6043 * For one-to-many style sockets this parameter indicates which
6044 * association the user is performing an action upon. Note that if
6045 * this field's value is zero then the endpoints default value is
6046 * changed (effecting future associations only).
6047 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 */
6049static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6050 char __user *optval, int __user *optlen)
6051{
Wei Yongjune89c2092008-12-25 16:54:58 -08006052 struct sctp_assoc_value params;
6053 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054
Wei Yongjune89c2092008-12-25 16:54:58 -08006055 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006056 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006057 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006058 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006059 "Use struct sctp_assoc_value instead\n",
6060 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08006061 params.assoc_id = 0;
6062 } else if (len >= sizeof(struct sctp_assoc_value)) {
6063 len = sizeof(struct sctp_assoc_value);
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006064 if (copy_from_user(&params, optval, len))
Wei Yongjune89c2092008-12-25 16:54:58 -08006065 return -EFAULT;
6066 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 return -EINVAL;
6068
Wei Yongjune89c2092008-12-25 16:54:58 -08006069 asoc = sctp_id2assoc(sk, params.assoc_id);
6070 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
6071 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
Wei Yongjune89c2092008-12-25 16:54:58 -08006073 if (asoc)
6074 params.assoc_value = asoc->frag_point;
6075 else
6076 params.assoc_value = sctp_sk(sk)->user_frag;
6077
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 if (put_user(len, optlen))
6079 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08006080 if (len == sizeof(int)) {
6081 if (copy_to_user(optval, &params.assoc_value, len))
6082 return -EFAULT;
6083 } else {
6084 if (copy_to_user(optval, &params, len))
6085 return -EFAULT;
6086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087
6088 return 0;
6089}
6090
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006091/*
6092 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6093 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6094 */
6095static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6096 char __user *optval, int __user *optlen)
6097{
6098 int val;
6099
6100 if (len < sizeof(int))
6101 return -EINVAL;
6102
6103 len = sizeof(int);
6104
6105 val = sctp_sk(sk)->frag_interleave;
6106 if (put_user(len, optlen))
6107 return -EFAULT;
6108 if (copy_to_user(optval, &val, len))
6109 return -EFAULT;
6110
6111 return 0;
6112}
6113
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006114/*
6115 * 7.1.25. Set or Get the sctp partial delivery point
6116 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6117 */
6118static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6119 char __user *optval,
6120 int __user *optlen)
6121{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09006122 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006123
6124 if (len < sizeof(u32))
6125 return -EINVAL;
6126
6127 len = sizeof(u32);
6128
6129 val = sctp_sk(sk)->pd_point;
6130 if (put_user(len, optlen))
6131 return -EFAULT;
6132 if (copy_to_user(optval, &val, len))
6133 return -EFAULT;
6134
Wei Yongjun7d743b72010-12-14 16:10:41 +00006135 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006136}
6137
Vlad Yasevich70331572007-03-23 11:34:36 -07006138/*
6139 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6140 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6141 */
6142static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6143 char __user *optval,
6144 int __user *optlen)
6145{
Neil Horman219b99a2008-03-05 13:44:46 -08006146 struct sctp_assoc_value params;
6147 struct sctp_sock *sp;
6148 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07006149
Neil Horman219b99a2008-03-05 13:44:46 -08006150 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006151 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006152 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006153 "Use of int in max_burst socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006154 "Use struct sctp_assoc_value instead\n",
6155 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08006156 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00006157 } else if (len >= sizeof(struct sctp_assoc_value)) {
6158 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08006159 if (copy_from_user(&params, optval, len))
6160 return -EFAULT;
6161 } else
6162 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07006163
Neil Horman219b99a2008-03-05 13:44:46 -08006164 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07006165
Neil Horman219b99a2008-03-05 13:44:46 -08006166 if (params.assoc_id != 0) {
6167 asoc = sctp_id2assoc(sk, params.assoc_id);
6168 if (!asoc)
6169 return -EINVAL;
6170 params.assoc_value = asoc->max_burst;
6171 } else
6172 params.assoc_value = sp->max_burst;
6173
6174 if (len == sizeof(int)) {
6175 if (copy_to_user(optval, &params.assoc_value, len))
6176 return -EFAULT;
6177 } else {
6178 if (copy_to_user(optval, &params, len))
6179 return -EFAULT;
6180 }
6181
6182 return 0;
6183
Vlad Yasevich70331572007-03-23 11:34:36 -07006184}
6185
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006186static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6187 char __user *optval, int __user *optlen)
6188{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006189 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006190 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006191 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006192 __u16 data_len = 0;
6193 u32 num_idents;
Xin Long7a84bd42016-02-03 23:33:30 +08006194 int i;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006195
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006196 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006197 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006198
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006199 hmacs = ep->auth_hmacs_list;
Xin Long3c918702017-06-30 11:52:16 +08006200 data_len = ntohs(hmacs->param_hdr.length) -
6201 sizeof(struct sctp_paramhdr);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006202
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006203 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006204 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006205
6206 len = sizeof(struct sctp_hmacalgo) + data_len;
6207 num_idents = data_len / sizeof(u16);
6208
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006209 if (put_user(len, optlen))
6210 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006211 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006212 return -EFAULT;
Xin Long7a84bd42016-02-03 23:33:30 +08006213 for (i = 0; i < num_idents; i++) {
6214 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6215
6216 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6217 return -EFAULT;
6218 }
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006219 return 0;
6220}
6221
6222static int sctp_getsockopt_active_key(struct sock *sk, int len,
6223 char __user *optval, int __user *optlen)
6224{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006225 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006226 struct sctp_authkeyid val;
6227 struct sctp_association *asoc;
6228
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006229 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006230 return -EACCES;
6231
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006232 if (len < sizeof(struct sctp_authkeyid))
6233 return -EINVAL;
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006234
6235 len = sizeof(struct sctp_authkeyid);
6236 if (copy_from_user(&val, optval, len))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006237 return -EFAULT;
6238
6239 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6240 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6241 return -EINVAL;
6242
6243 if (asoc)
6244 val.scact_keynumber = asoc->active_key_id;
6245 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006246 val.scact_keynumber = ep->active_key_id;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006247
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006248 if (put_user(len, optlen))
6249 return -EFAULT;
6250 if (copy_to_user(optval, &val, len))
6251 return -EFAULT;
6252
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006253 return 0;
6254}
6255
6256static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6257 char __user *optval, int __user *optlen)
6258{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006259 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006260 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006261 struct sctp_authchunks val;
6262 struct sctp_association *asoc;
6263 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006264 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006265 char __user *to;
6266
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006267 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006268 return -EACCES;
6269
6270 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006271 return -EINVAL;
6272
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006273 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006274 return -EFAULT;
6275
Al Viro411223c2007-10-14 19:21:20 +01006276 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006277 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6278 if (!asoc)
6279 return -EINVAL;
6280
6281 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006282 if (!ch)
6283 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006284
6285 /* See if the user provided enough room for all the data */
Xin Long3c918702017-06-30 11:52:16 +08006286 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevichb40db682008-02-27 14:40:37 -05006287 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006288 return -EINVAL;
6289
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006290 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006291 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006292num:
6293 len = sizeof(struct sctp_authchunks) + num_chunks;
wangweidong8d726512013-12-23 12:16:53 +08006294 if (put_user(len, optlen))
6295 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006296 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6297 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006298 return 0;
6299}
6300
6301static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6302 char __user *optval, int __user *optlen)
6303{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006304 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006305 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006306 struct sctp_authchunks val;
6307 struct sctp_association *asoc;
6308 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006309 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006310 char __user *to;
6311
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006312 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006313 return -EACCES;
6314
6315 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006316 return -EINVAL;
6317
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006318 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006319 return -EFAULT;
6320
Al Viro411223c2007-10-14 19:21:20 +01006321 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006322 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6323 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6324 return -EINVAL;
6325
6326 if (asoc)
wangweidong26ac8e52013-12-23 12:16:51 +08006327 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006328 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006329 ch = ep->auth_chunk_list;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006330
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006331 if (!ch)
6332 goto num;
6333
Xin Long3c918702017-06-30 11:52:16 +08006334 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006335 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006336 return -EINVAL;
6337
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006338 if (copy_to_user(to, ch->chunks, num_chunks))
6339 return -EFAULT;
6340num:
6341 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006342 if (put_user(len, optlen))
6343 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006344 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6345 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006346
6347 return 0;
6348}
6349
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08006350/*
6351 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6352 * This option gets the current number of associations that are attached
6353 * to a one-to-many style socket. The option value is an uint32_t.
6354 */
6355static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6356 char __user *optval, int __user *optlen)
6357{
6358 struct sctp_sock *sp = sctp_sk(sk);
6359 struct sctp_association *asoc;
6360 u32 val = 0;
6361
6362 if (sctp_style(sk, TCP))
6363 return -EOPNOTSUPP;
6364
6365 if (len < sizeof(u32))
6366 return -EINVAL;
6367
6368 len = sizeof(u32);
6369
6370 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6371 val++;
6372 }
6373
6374 if (put_user(len, optlen))
6375 return -EFAULT;
6376 if (copy_to_user(optval, &val, len))
6377 return -EFAULT;
6378
6379 return 0;
6380}
6381
Wei Yongjun209ba422011-04-17 17:27:08 +00006382/*
Michio Honda7dc04d72011-04-26 20:16:31 +09006383 * 8.1.23 SCTP_AUTO_ASCONF
6384 * See the corresponding setsockopt entry as description
6385 */
6386static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6387 char __user *optval, int __user *optlen)
6388{
6389 int val = 0;
6390
6391 if (len < sizeof(int))
6392 return -EINVAL;
6393
6394 len = sizeof(int);
6395 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6396 val = 1;
6397 if (put_user(len, optlen))
6398 return -EFAULT;
6399 if (copy_to_user(optval, &val, len))
6400 return -EFAULT;
6401 return 0;
6402}
6403
6404/*
Wei Yongjun209ba422011-04-17 17:27:08 +00006405 * 8.2.6. Get the Current Identifiers of Associations
6406 * (SCTP_GET_ASSOC_ID_LIST)
6407 *
6408 * This option gets the current list of SCTP association identifiers of
6409 * the SCTP associations handled by a one-to-many style socket.
6410 */
6411static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6412 char __user *optval, int __user *optlen)
6413{
6414 struct sctp_sock *sp = sctp_sk(sk);
6415 struct sctp_association *asoc;
6416 struct sctp_assoc_ids *ids;
6417 u32 num = 0;
6418
6419 if (sctp_style(sk, TCP))
6420 return -EOPNOTSUPP;
6421
6422 if (len < sizeof(struct sctp_assoc_ids))
6423 return -EINVAL;
6424
6425 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6426 num++;
6427 }
6428
6429 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6430 return -EINVAL;
6431
6432 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6433
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02006434 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
Wei Yongjun209ba422011-04-17 17:27:08 +00006435 if (unlikely(!ids))
6436 return -ENOMEM;
6437
6438 ids->gaids_number_of_ids = num;
6439 num = 0;
6440 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6441 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6442 }
6443
6444 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6445 kfree(ids);
6446 return -EFAULT;
6447 }
6448
6449 kfree(ids);
6450 return 0;
6451}
6452
Neil Horman5aa93bc2012-07-21 07:56:07 +00006453/*
6454 * SCTP_PEER_ADDR_THLDS
6455 *
6456 * This option allows us to fetch the partially failed threshold for one or all
6457 * transports in an association. See Section 6.1 of:
6458 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6459 */
6460static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6461 char __user *optval,
6462 int len,
6463 int __user *optlen)
6464{
6465 struct sctp_paddrthlds val;
6466 struct sctp_transport *trans;
6467 struct sctp_association *asoc;
6468
6469 if (len < sizeof(struct sctp_paddrthlds))
6470 return -EINVAL;
6471 len = sizeof(struct sctp_paddrthlds);
6472 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6473 return -EFAULT;
6474
6475 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6476 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6477 if (!asoc)
6478 return -ENOENT;
6479
6480 val.spt_pathpfthld = asoc->pf_retrans;
6481 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6482 } else {
6483 trans = sctp_addr_id2transport(sk, &val.spt_address,
6484 val.spt_assoc_id);
6485 if (!trans)
6486 return -ENOENT;
6487
6488 val.spt_pathmaxrxt = trans->pathmaxrxt;
6489 val.spt_pathpfthld = trans->pf_retrans;
6490 }
6491
6492 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6493 return -EFAULT;
6494
6495 return 0;
6496}
6497
Michele Baldessari196d6752012-12-01 04:49:42 +00006498/*
6499 * SCTP_GET_ASSOC_STATS
6500 *
6501 * This option retrieves local per endpoint statistics. It is modeled
6502 * after OpenSolaris' implementation
6503 */
6504static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6505 char __user *optval,
6506 int __user *optlen)
6507{
6508 struct sctp_assoc_stats sas;
6509 struct sctp_association *asoc = NULL;
6510
6511 /* User must provide at least the assoc id */
6512 if (len < sizeof(sctp_assoc_t))
6513 return -EINVAL;
6514
Guenter Roeck726bc6b2013-02-27 10:57:31 +00006515 /* Allow the struct to grow and fill in as much as possible */
6516 len = min_t(size_t, len, sizeof(sas));
6517
Michele Baldessari196d6752012-12-01 04:49:42 +00006518 if (copy_from_user(&sas, optval, len))
6519 return -EFAULT;
6520
6521 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6522 if (!asoc)
6523 return -EINVAL;
6524
6525 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6526 sas.sas_gapcnt = asoc->stats.gapcnt;
6527 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6528 sas.sas_osacks = asoc->stats.osacks;
6529 sas.sas_isacks = asoc->stats.isacks;
6530 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6531 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6532 sas.sas_oodchunks = asoc->stats.oodchunks;
6533 sas.sas_iodchunks = asoc->stats.iodchunks;
6534 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6535 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6536 sas.sas_idupchunks = asoc->stats.idupchunks;
6537 sas.sas_opackets = asoc->stats.opackets;
6538 sas.sas_ipackets = asoc->stats.ipackets;
6539
6540 /* New high max rto observed, will return 0 if not a single
6541 * RTO update took place. obs_rto_ipaddr will be bogus
6542 * in such a case
6543 */
6544 sas.sas_maxrto = asoc->stats.max_obs_rto;
6545 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6546 sizeof(struct sockaddr_storage));
6547
6548 /* Mark beginning of a new observation period */
6549 asoc->stats.max_obs_rto = asoc->rto_min;
6550
Michele Baldessari196d6752012-12-01 04:49:42 +00006551 if (put_user(len, optlen))
6552 return -EFAULT;
6553
Daniel Borkmannbb333812013-06-28 19:49:40 +02006554 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
Michele Baldessari196d6752012-12-01 04:49:42 +00006555
6556 if (copy_to_user(optval, &sas, len))
6557 return -EFAULT;
6558
6559 return 0;
6560}
6561
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02006562static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6563 char __user *optval,
6564 int __user *optlen)
6565{
6566 int val = 0;
6567
6568 if (len < sizeof(int))
6569 return -EINVAL;
6570
6571 len = sizeof(int);
6572 if (sctp_sk(sk)->recvrcvinfo)
6573 val = 1;
6574 if (put_user(len, optlen))
6575 return -EFAULT;
6576 if (copy_to_user(optval, &val, len))
6577 return -EFAULT;
6578
6579 return 0;
6580}
6581
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02006582static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6583 char __user *optval,
6584 int __user *optlen)
6585{
6586 int val = 0;
6587
6588 if (len < sizeof(int))
6589 return -EINVAL;
6590
6591 len = sizeof(int);
6592 if (sctp_sk(sk)->recvnxtinfo)
6593 val = 1;
6594 if (put_user(len, optlen))
6595 return -EFAULT;
6596 if (copy_to_user(optval, &val, len))
6597 return -EFAULT;
6598
6599 return 0;
6600}
6601
Xin Long28aa4c22016-07-09 19:47:40 +08006602static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6603 char __user *optval,
6604 int __user *optlen)
6605{
6606 struct sctp_assoc_value params;
6607 struct sctp_association *asoc;
6608 int retval = -EFAULT;
6609
6610 if (len < sizeof(params)) {
6611 retval = -EINVAL;
6612 goto out;
6613 }
6614
6615 len = sizeof(params);
6616 if (copy_from_user(&params, optval, len))
6617 goto out;
6618
6619 asoc = sctp_id2assoc(sk, params.assoc_id);
6620 if (asoc) {
6621 params.assoc_value = asoc->prsctp_enable;
6622 } else if (!params.assoc_id) {
6623 struct sctp_sock *sp = sctp_sk(sk);
6624
6625 params.assoc_value = sp->ep->prsctp_enable;
6626 } else {
6627 retval = -EINVAL;
6628 goto out;
6629 }
6630
6631 if (put_user(len, optlen))
6632 goto out;
6633
6634 if (copy_to_user(optval, &params, len))
6635 goto out;
6636
6637 retval = 0;
6638
6639out:
6640 return retval;
6641}
6642
Xin Longf959fb42016-07-09 19:47:41 +08006643static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
6644 char __user *optval,
6645 int __user *optlen)
6646{
6647 struct sctp_default_prinfo info;
6648 struct sctp_association *asoc;
6649 int retval = -EFAULT;
6650
6651 if (len < sizeof(info)) {
6652 retval = -EINVAL;
6653 goto out;
6654 }
6655
6656 len = sizeof(info);
6657 if (copy_from_user(&info, optval, len))
6658 goto out;
6659
6660 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
6661 if (asoc) {
6662 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
6663 info.pr_value = asoc->default_timetolive;
6664 } else if (!info.pr_assoc_id) {
6665 struct sctp_sock *sp = sctp_sk(sk);
6666
6667 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
6668 info.pr_value = sp->default_timetolive;
6669 } else {
6670 retval = -EINVAL;
6671 goto out;
6672 }
6673
6674 if (put_user(len, optlen))
6675 goto out;
6676
6677 if (copy_to_user(optval, &info, len))
6678 goto out;
6679
6680 retval = 0;
6681
6682out:
6683 return retval;
6684}
6685
Xin Long826d2532016-07-09 19:47:42 +08006686static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
6687 char __user *optval,
6688 int __user *optlen)
6689{
6690 struct sctp_prstatus params;
6691 struct sctp_association *asoc;
6692 int policy;
6693 int retval = -EINVAL;
6694
6695 if (len < sizeof(params))
6696 goto out;
6697
6698 len = sizeof(params);
6699 if (copy_from_user(&params, optval, len)) {
6700 retval = -EFAULT;
6701 goto out;
6702 }
6703
6704 policy = params.sprstat_policy;
6705 if (policy & ~SCTP_PR_SCTP_MASK)
6706 goto out;
6707
6708 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
6709 if (!asoc)
6710 goto out;
6711
6712 if (policy == SCTP_PR_SCTP_NONE) {
6713 params.sprstat_abandoned_unsent = 0;
6714 params.sprstat_abandoned_sent = 0;
6715 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6716 params.sprstat_abandoned_unsent +=
6717 asoc->abandoned_unsent[policy];
6718 params.sprstat_abandoned_sent +=
6719 asoc->abandoned_sent[policy];
6720 }
6721 } else {
6722 params.sprstat_abandoned_unsent =
6723 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
6724 params.sprstat_abandoned_sent =
6725 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
6726 }
6727
6728 if (put_user(len, optlen)) {
6729 retval = -EFAULT;
6730 goto out;
6731 }
6732
6733 if (copy_to_user(optval, &params, len)) {
6734 retval = -EFAULT;
6735 goto out;
6736 }
6737
6738 retval = 0;
6739
6740out:
6741 return retval;
6742}
6743
Xin Longd229d482017-04-01 17:07:46 +08006744static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
6745 char __user *optval,
6746 int __user *optlen)
6747{
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006748 struct sctp_stream_out_ext *streamoute;
Xin Longd229d482017-04-01 17:07:46 +08006749 struct sctp_association *asoc;
6750 struct sctp_prstatus params;
6751 int retval = -EINVAL;
6752 int policy;
6753
6754 if (len < sizeof(params))
6755 goto out;
6756
6757 len = sizeof(params);
6758 if (copy_from_user(&params, optval, len)) {
6759 retval = -EFAULT;
6760 goto out;
6761 }
6762
6763 policy = params.sprstat_policy;
6764 if (policy & ~SCTP_PR_SCTP_MASK)
6765 goto out;
6766
6767 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
Xin Longcee360a2017-05-31 16:36:31 +08006768 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
Xin Longd229d482017-04-01 17:07:46 +08006769 goto out;
6770
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006771 streamoute = asoc->stream.out[params.sprstat_sid].ext;
6772 if (!streamoute) {
6773 /* Not allocated yet, means all stats are 0 */
6774 params.sprstat_abandoned_unsent = 0;
6775 params.sprstat_abandoned_sent = 0;
6776 retval = 0;
6777 goto out;
6778 }
6779
Xin Longd229d482017-04-01 17:07:46 +08006780 if (policy == SCTP_PR_SCTP_NONE) {
6781 params.sprstat_abandoned_unsent = 0;
6782 params.sprstat_abandoned_sent = 0;
6783 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6784 params.sprstat_abandoned_unsent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006785 streamoute->abandoned_unsent[policy];
Xin Longd229d482017-04-01 17:07:46 +08006786 params.sprstat_abandoned_sent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006787 streamoute->abandoned_sent[policy];
Xin Longd229d482017-04-01 17:07:46 +08006788 }
6789 } else {
6790 params.sprstat_abandoned_unsent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006791 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08006792 params.sprstat_abandoned_sent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006793 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08006794 }
6795
6796 if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
6797 retval = -EFAULT;
6798 goto out;
6799 }
6800
6801 retval = 0;
6802
6803out:
6804 return retval;
6805}
6806
Xin Longc0d8bab2017-03-10 12:11:12 +08006807static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
6808 char __user *optval,
6809 int __user *optlen)
6810{
6811 struct sctp_assoc_value params;
6812 struct sctp_association *asoc;
6813 int retval = -EFAULT;
6814
6815 if (len < sizeof(params)) {
6816 retval = -EINVAL;
6817 goto out;
6818 }
6819
6820 len = sizeof(params);
6821 if (copy_from_user(&params, optval, len))
6822 goto out;
6823
6824 asoc = sctp_id2assoc(sk, params.assoc_id);
6825 if (asoc) {
6826 params.assoc_value = asoc->reconf_enable;
6827 } else if (!params.assoc_id) {
6828 struct sctp_sock *sp = sctp_sk(sk);
6829
6830 params.assoc_value = sp->ep->reconf_enable;
6831 } else {
6832 retval = -EINVAL;
6833 goto out;
6834 }
6835
6836 if (put_user(len, optlen))
6837 goto out;
6838
6839 if (copy_to_user(optval, &params, len))
6840 goto out;
6841
6842 retval = 0;
6843
6844out:
6845 return retval;
6846}
6847
Xin Long9fb657a2017-01-18 00:44:46 +08006848static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
6849 char __user *optval,
6850 int __user *optlen)
6851{
6852 struct sctp_assoc_value params;
6853 struct sctp_association *asoc;
6854 int retval = -EFAULT;
6855
6856 if (len < sizeof(params)) {
6857 retval = -EINVAL;
6858 goto out;
6859 }
6860
6861 len = sizeof(params);
6862 if (copy_from_user(&params, optval, len))
6863 goto out;
6864
6865 asoc = sctp_id2assoc(sk, params.assoc_id);
6866 if (asoc) {
6867 params.assoc_value = asoc->strreset_enable;
6868 } else if (!params.assoc_id) {
6869 struct sctp_sock *sp = sctp_sk(sk);
6870
6871 params.assoc_value = sp->ep->strreset_enable;
6872 } else {
6873 retval = -EINVAL;
6874 goto out;
6875 }
6876
6877 if (put_user(len, optlen))
6878 goto out;
6879
6880 if (copy_to_user(optval, &params, len))
6881 goto out;
6882
6883 retval = 0;
6884
6885out:
6886 return retval;
6887}
6888
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03006889static int sctp_getsockopt_scheduler(struct sock *sk, int len,
6890 char __user *optval,
6891 int __user *optlen)
6892{
6893 struct sctp_assoc_value params;
6894 struct sctp_association *asoc;
6895 int retval = -EFAULT;
6896
6897 if (len < sizeof(params)) {
6898 retval = -EINVAL;
6899 goto out;
6900 }
6901
6902 len = sizeof(params);
6903 if (copy_from_user(&params, optval, len))
6904 goto out;
6905
6906 asoc = sctp_id2assoc(sk, params.assoc_id);
6907 if (!asoc) {
6908 retval = -EINVAL;
6909 goto out;
6910 }
6911
6912 params.assoc_value = sctp_sched_get_sched(asoc);
6913
6914 if (put_user(len, optlen))
6915 goto out;
6916
6917 if (copy_to_user(optval, &params, len))
6918 goto out;
6919
6920 retval = 0;
6921
6922out:
6923 return retval;
6924}
6925
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03006926static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
6927 char __user *optval,
6928 int __user *optlen)
6929{
6930 struct sctp_stream_value params;
6931 struct sctp_association *asoc;
6932 int retval = -EFAULT;
6933
6934 if (len < sizeof(params)) {
6935 retval = -EINVAL;
6936 goto out;
6937 }
6938
6939 len = sizeof(params);
6940 if (copy_from_user(&params, optval, len))
6941 goto out;
6942
6943 asoc = sctp_id2assoc(sk, params.assoc_id);
6944 if (!asoc) {
6945 retval = -EINVAL;
6946 goto out;
6947 }
6948
6949 retval = sctp_sched_get_value(asoc, params.stream_id,
6950 &params.stream_value);
6951 if (retval)
6952 goto out;
6953
6954 if (put_user(len, optlen)) {
6955 retval = -EFAULT;
6956 goto out;
6957 }
6958
6959 if (copy_to_user(optval, &params, len)) {
6960 retval = -EFAULT;
6961 goto out;
6962 }
6963
6964out:
6965 return retval;
6966}
6967
Xin Long772a5862017-12-08 21:03:58 +08006968static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
6969 char __user *optval,
6970 int __user *optlen)
6971{
6972 struct sctp_assoc_value params;
6973 struct sctp_association *asoc;
6974 int retval = -EFAULT;
6975
6976 if (len < sizeof(params)) {
6977 retval = -EINVAL;
6978 goto out;
6979 }
6980
6981 len = sizeof(params);
6982 if (copy_from_user(&params, optval, len))
6983 goto out;
6984
6985 asoc = sctp_id2assoc(sk, params.assoc_id);
6986 if (asoc) {
6987 params.assoc_value = asoc->intl_enable;
6988 } else if (!params.assoc_id) {
6989 struct sctp_sock *sp = sctp_sk(sk);
6990
6991 params.assoc_value = sp->strm_interleave;
6992 } else {
6993 retval = -EINVAL;
6994 goto out;
6995 }
6996
6997 if (put_user(len, optlen))
6998 goto out;
6999
7000 if (copy_to_user(optval, &params, len))
7001 goto out;
7002
7003 retval = 0;
7004
7005out:
7006 return retval;
7007}
7008
Daniel Borkmanndda91922013-06-17 11:40:05 +02007009static int sctp_getsockopt(struct sock *sk, int level, int optname,
7010 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011{
7012 int retval = 0;
7013 int len;
7014
Daniel Borkmannbb333812013-06-28 19:49:40 +02007015 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016
7017 /* I can hardly begin to describe how wrong this is. This is
7018 * so broken as to be worse than useless. The API draft
7019 * REALLY is NOT helpful here... I am not convinced that the
7020 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7021 * are at all well-founded.
7022 */
7023 if (level != SOL_SCTP) {
7024 struct sctp_af *af = sctp_sk(sk)->pf->af;
7025
7026 retval = af->getsockopt(sk, level, optname, optval, optlen);
7027 return retval;
7028 }
7029
7030 if (get_user(len, optlen))
7031 return -EFAULT;
7032
Jiri Slabya4b8e712016-10-21 14:13:24 +02007033 if (len < 0)
7034 return -EINVAL;
7035
wangweidong048ed4b2014-01-21 15:44:11 +08007036 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037
7038 switch (optname) {
7039 case SCTP_STATUS:
7040 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7041 break;
7042 case SCTP_DISABLE_FRAGMENTS:
7043 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7044 optlen);
7045 break;
7046 case SCTP_EVENTS:
7047 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7048 break;
7049 case SCTP_AUTOCLOSE:
7050 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7051 break;
7052 case SCTP_SOCKOPT_PEELOFF:
7053 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7054 break;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04007055 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7056 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7057 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 case SCTP_PEER_ADDR_PARAMS:
7059 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7060 optlen);
7061 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00007062 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07007063 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08007064 optlen);
7065 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066 case SCTP_INITMSG:
7067 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7068 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 case SCTP_GET_PEER_ADDRS:
7070 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7071 optlen);
7072 break;
7073 case SCTP_GET_LOCAL_ADDRS:
7074 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7075 optlen);
7076 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04007077 case SCTP_SOCKOPT_CONNECTX3:
7078 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7079 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 case SCTP_DEFAULT_SEND_PARAM:
7081 retval = sctp_getsockopt_default_send_param(sk, len,
7082 optval, optlen);
7083 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02007084 case SCTP_DEFAULT_SNDINFO:
7085 retval = sctp_getsockopt_default_sndinfo(sk, len,
7086 optval, optlen);
7087 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088 case SCTP_PRIMARY_ADDR:
7089 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7090 break;
7091 case SCTP_NODELAY:
7092 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7093 break;
7094 case SCTP_RTOINFO:
7095 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7096 break;
7097 case SCTP_ASSOCINFO:
7098 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7099 break;
7100 case SCTP_I_WANT_MAPPED_V4_ADDR:
7101 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7102 break;
7103 case SCTP_MAXSEG:
7104 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7105 break;
7106 case SCTP_GET_PEER_ADDR_INFO:
7107 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7108 optlen);
7109 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08007110 case SCTP_ADAPTATION_LAYER:
7111 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 optlen);
7113 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08007114 case SCTP_CONTEXT:
7115 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7116 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007117 case SCTP_FRAGMENT_INTERLEAVE:
7118 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7119 optlen);
7120 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07007121 case SCTP_PARTIAL_DELIVERY_POINT:
7122 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7123 optlen);
7124 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07007125 case SCTP_MAX_BURST:
7126 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7127 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007128 case SCTP_AUTH_KEY:
7129 case SCTP_AUTH_CHUNK:
7130 case SCTP_AUTH_DELETE_KEY:
7131 retval = -EOPNOTSUPP;
7132 break;
7133 case SCTP_HMAC_IDENT:
7134 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7135 break;
7136 case SCTP_AUTH_ACTIVE_KEY:
7137 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7138 break;
7139 case SCTP_PEER_AUTH_CHUNKS:
7140 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7141 optlen);
7142 break;
7143 case SCTP_LOCAL_AUTH_CHUNKS:
7144 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7145 optlen);
7146 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08007147 case SCTP_GET_ASSOC_NUMBER:
7148 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7149 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00007150 case SCTP_GET_ASSOC_ID_LIST:
7151 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7152 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09007153 case SCTP_AUTO_ASCONF:
7154 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7155 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00007156 case SCTP_PEER_ADDR_THLDS:
7157 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7158 break;
Michele Baldessari196d6752012-12-01 04:49:42 +00007159 case SCTP_GET_ASSOC_STATS:
7160 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7161 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02007162 case SCTP_RECVRCVINFO:
7163 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7164 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007165 case SCTP_RECVNXTINFO:
7166 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7167 break;
Xin Long28aa4c22016-07-09 19:47:40 +08007168 case SCTP_PR_SUPPORTED:
7169 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7170 break;
Xin Longf959fb42016-07-09 19:47:41 +08007171 case SCTP_DEFAULT_PRINFO:
7172 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7173 optlen);
7174 break;
Xin Long826d2532016-07-09 19:47:42 +08007175 case SCTP_PR_ASSOC_STATUS:
7176 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7177 optlen);
7178 break;
Xin Longd229d482017-04-01 17:07:46 +08007179 case SCTP_PR_STREAM_STATUS:
7180 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7181 optlen);
7182 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08007183 case SCTP_RECONFIG_SUPPORTED:
7184 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7185 optlen);
7186 break;
Xin Long9fb657a2017-01-18 00:44:46 +08007187 case SCTP_ENABLE_STREAM_RESET:
7188 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7189 optlen);
7190 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007191 case SCTP_STREAM_SCHEDULER:
7192 retval = sctp_getsockopt_scheduler(sk, len, optval,
7193 optlen);
7194 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007195 case SCTP_STREAM_SCHEDULER_VALUE:
7196 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7197 optlen);
7198 break;
Xin Long772a5862017-12-08 21:03:58 +08007199 case SCTP_INTERLEAVING_SUPPORTED:
7200 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7201 optlen);
7202 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 default:
7204 retval = -ENOPROTOOPT;
7205 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207
wangweidong048ed4b2014-01-21 15:44:11 +08007208 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 return retval;
7210}
7211
Craig Gallek086c6532016-02-10 11:50:35 -05007212static int sctp_hash(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213{
7214 /* STUB */
Craig Gallek086c6532016-02-10 11:50:35 -05007215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216}
7217
7218static void sctp_unhash(struct sock *sk)
7219{
7220 /* STUB */
7221}
7222
7223/* Check if port is acceptable. Possibly find first available port.
7224 *
7225 * The port hash table (contained in the 'global' SCTP protocol storage
7226 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7227 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7228 * list (the list number is the port number hashed out, so as you
7229 * would expect from a hash function, all the ports in a given list have
7230 * such a number that hashes out to the same list number; you were
7231 * expecting that, right?); so each list has a set of ports, with a
7232 * link to the socket (struct sock) that uses it, the port number and
7233 * a fastreuse flag (FIXME: NPI ipg).
7234 */
7235static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007236 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237
7238static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
7239{
7240 struct sctp_bind_hashbucket *head; /* hash list */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007241 struct sctp_bind_bucket *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242 unsigned short snum;
7243 int ret;
7244
Al Viro04afd8b2006-11-20 17:02:01 -08007245 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246
Daniel Borkmannbb333812013-06-28 19:49:40 +02007247 pr_debug("%s: begins, snum:%d\n", __func__, snum);
7248
wangweidong79b91132014-01-21 15:44:07 +08007249 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250
7251 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07007252 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007253 int low, high, remaining, index;
7254 unsigned int rover;
WANG Cong122ff242014-05-12 16:04:53 -07007255 struct net *net = sock_net(sk);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007256
WANG Cong122ff242014-05-12 16:04:53 -07007257 inet_get_local_port_range(net, &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007258 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05007259 rover = prandom_u32() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261 do {
7262 rover++;
7263 if ((rover < low) || (rover > high))
7264 rover = low;
WANG Cong122ff242014-05-12 16:04:53 -07007265 if (inet_is_local_reserved_port(net, rover))
Amerigo Wange3826f12010-05-05 00:27:06 +00007266 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007267 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268 head = &sctp_port_hashtable[index];
wangweidong3c8e43b2014-01-21 15:44:08 +08007269 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007270 sctp_for_each_hentry(pp, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007271 if ((pp->port == rover) &&
7272 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273 goto next;
7274 break;
7275 next:
wangweidong3c8e43b2014-01-21 15:44:08 +08007276 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278
7279 /* Exhausted local port range during search? */
7280 ret = 1;
7281 if (remaining <= 0)
7282 goto fail;
7283
7284 /* OK, here is the one we will use. HEAD (the port
7285 * hash table list entry) is non-NULL and we hold it's
7286 * mutex.
7287 */
7288 snum = rover;
7289 } else {
7290 /* We are given an specific port number; we verify
7291 * that it is not being used. If it is used, we will
7292 * exahust the search in the hash list corresponding
7293 * to the port number (snum) - we detect that with the
7294 * port iterator, pp being NULL.
7295 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007296 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
wangweidong3c8e43b2014-01-21 15:44:08 +08007297 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007298 sctp_for_each_hentry(pp, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007299 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 goto pp_found;
7301 }
7302 }
7303 pp = NULL;
7304 goto pp_not_found;
7305pp_found:
7306 if (!hlist_empty(&pp->owner)) {
7307 /* We had a port hash table hit - there is an
7308 * available port (pp != NULL) and it is being
7309 * used by other socket (pp->owner not empty); that other
7310 * socket is going to be sk2.
7311 */
7312 int reuse = sk->sk_reuse;
7313 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314
Daniel Borkmannbb333812013-06-28 19:49:40 +02007315 pr_debug("%s: found a possible match\n", __func__);
7316
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007317 if (pp->fastreuse && sk->sk_reuse &&
7318 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 goto success;
7320
7321 /* Run through the list of sockets bound to the port
7322 * (pp->port) [via the pointers bind_next and
7323 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
7324 * we get the endpoint they describe and run through
7325 * the endpoint's list of IP (v4 or v6) addresses,
7326 * comparing each of the addresses with the address of
7327 * the socket sk. If we find a match, then that means
7328 * that this port/socket (sk) combination are already
7329 * in an endpoint.
7330 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007331 sk_for_each_bound(sk2, &pp->owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332 struct sctp_endpoint *ep2;
7333 ep2 = sctp_sk(sk2)->ep;
7334
Vlad Yasevich4e540642008-07-18 23:06:32 -07007335 if (sk == sk2 ||
7336 (reuse && sk2->sk_reuse &&
7337 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338 continue;
7339
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07007340 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
7341 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 ret = (long)sk2;
7343 goto fail_unlock;
7344 }
7345 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02007346
7347 pr_debug("%s: found a match\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 }
7349pp_not_found:
7350 /* If there was a hash table miss, create a new port. */
7351 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007352 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 goto fail_unlock;
7354
7355 /* In either case (hit or miss), make sure fastreuse is 1 only
7356 * if sk->sk_reuse is too (that is, if the caller requested
7357 * SO_REUSEADDR on this socket -sk-).
7358 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007359 if (hlist_empty(&pp->owner)) {
7360 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
7361 pp->fastreuse = 1;
7362 else
7363 pp->fastreuse = 0;
7364 } else if (pp->fastreuse &&
7365 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366 pp->fastreuse = 0;
7367
7368 /* We are set, so fill up all the data in the hash table
7369 * entry, tie the socket list information with the rest of the
7370 * sockets FIXME: Blurry, NPI (ipg).
7371 */
7372success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007374 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 sk_add_bind_node(sk, &pp->owner);
7376 sctp_sk(sk)->bind_hash = pp;
7377 }
7378 ret = 0;
7379
7380fail_unlock:
wangweidong3c8e43b2014-01-21 15:44:08 +08007381 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382
7383fail:
wangweidong79b91132014-01-21 15:44:07 +08007384 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 return ret;
7386}
7387
7388/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
7389 * port is requested.
7390 */
7391static int sctp_get_port(struct sock *sk, unsigned short snum)
7392{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 union sctp_addr addr;
7394 struct sctp_af *af = sctp_sk(sk)->pf->af;
7395
7396 /* Set up a dummy address struct from the sk. */
7397 af->from_sk(&addr, sk);
7398 addr.v4.sin_port = htons(snum);
7399
7400 /* Note: sk->sk_num gets filled in if ephemeral port request. */
Daniel Borkmann62208f12013-06-25 18:17:30 +02007401 return !!sctp_get_port_local(sk, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402}
7403
7404/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405 * Move a socket to LISTENING state.
7406 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02007407static int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007409 struct sctp_sock *sp = sctp_sk(sk);
7410 struct sctp_endpoint *ep = sp->ep;
Herbert Xu5821c762016-01-24 21:20:12 +08007411 struct crypto_shash *tfm = NULL;
Neil Horman3c681982012-10-24 09:20:03 +00007412 char alg[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413
7414 /* Allocate HMAC for generating cookie. */
Neil Horman3c681982012-10-24 09:20:03 +00007415 if (!sp->hmac && sp->sctp_hmac_alg) {
7416 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
Herbert Xu5821c762016-01-24 21:20:12 +08007417 tfm = crypto_alloc_shash(alg, 0, 0);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07007418 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00007419 net_info_ratelimited("failed to load transform for %s: %ld\n",
Neil Horman3c681982012-10-24 09:20:03 +00007420 sp->sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007421 return -ENOSYS;
7422 }
7423 sctp_sk(sk)->hmac = tfm;
7424 }
7425
7426 /*
7427 * If a bind() or sctp_bindx() is not called prior to a listen()
7428 * call that allows new associations to be accepted, the system
7429 * picks an ephemeral port and will choose an address set equivalent
7430 * to binding with a wildcard address.
7431 *
7432 * This is not currently spelled out in the SCTP sockets
7433 * extensions draft, but follows the practice as seen in TCP
7434 * sockets.
7435 *
7436 */
Yafang Shaocbabf462017-12-20 11:12:54 +08007437 inet_sk_set_state(sk, SCTP_SS_LISTENING);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007438 if (!ep->base.bind_addr.port) {
7439 if (sctp_autobind(sk))
7440 return -EAGAIN;
7441 } else {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007442 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08007443 inet_sk_set_state(sk, SCTP_SS_CLOSED);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007444 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445 }
7446 }
7447
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007448 sk->sk_max_ack_backlog = backlog;
7449 sctp_hash_endpoint(ep);
7450 return 0;
7451}
7452
7453/*
7454 * 4.1.3 / 5.1.3 listen()
7455 *
7456 * By default, new associations are not accepted for UDP style sockets.
7457 * An application uses listen() to mark a socket as being able to
7458 * accept new associations.
7459 *
7460 * On TCP style sockets, applications use listen() to ready the SCTP
7461 * endpoint for accepting inbound associations.
7462 *
7463 * On both types of endpoints a backlog of '0' disables listening.
7464 *
7465 * Move a socket to LISTENING state.
7466 */
7467int sctp_inet_listen(struct socket *sock, int backlog)
7468{
7469 struct sock *sk = sock->sk;
7470 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7471 int err = -EINVAL;
7472
7473 if (unlikely(backlog < 0))
7474 return err;
7475
wangweidong048ed4b2014-01-21 15:44:11 +08007476 lock_sock(sk);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007477
7478 /* Peeled-off sockets are not allowed to listen(). */
7479 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
7480 goto out;
7481
7482 if (sock->state != SS_UNCONNECTED)
7483 goto out;
7484
Xin Long34b27892017-04-06 13:10:52 +08007485 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
7486 goto out;
7487
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007488 /* If backlog is zero, disable listening. */
7489 if (!backlog) {
7490 if (sctp_sstate(sk, CLOSED))
7491 goto out;
7492
7493 err = 0;
7494 sctp_unhash_endpoint(ep);
7495 sk->sk_state = SCTP_SS_CLOSED;
7496 if (sk->sk_reuse)
7497 sctp_sk(sk)->bind_hash->fastreuse = 1;
7498 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007499 }
7500
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007501 /* If we are already listening, just update the backlog */
7502 if (sctp_sstate(sk, LISTENING))
7503 sk->sk_max_ack_backlog = backlog;
7504 else {
7505 err = sctp_listen_start(sk, backlog);
7506 if (err)
7507 goto out;
7508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007510 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511out:
wangweidong048ed4b2014-01-21 15:44:11 +08007512 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514}
7515
7516/*
7517 * This function is done by modeling the current datagram_poll() and the
7518 * tcp_poll(). Note that, based on these implementations, we don't
7519 * lock the socket in this function, even though it seems that,
7520 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08007521 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522 * in this place. We assume that we don't need locks either until proven
7523 * otherwise.
7524 *
7525 * Another thing to note is that we include the Async I/O support
7526 * here, again, by modeling the current TCP/UDP code. We don't have
7527 * a good way to test with it yet.
7528 */
Al Viroade994f2017-07-03 00:01:49 -04007529__poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530{
7531 struct sock *sk = sock->sk;
7532 struct sctp_sock *sp = sctp_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -04007533 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534
Eric Dumazetaa395142010-04-20 13:03:51 +00007535 poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03007537 sock_rps_record_flow(sk);
7538
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539 /* A TCP-style listening socket becomes readable when the accept queue
7540 * is not empty.
7541 */
7542 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
7543 return (!list_empty(&sp->ep->asocs)) ?
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007544 (EPOLLIN | EPOLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545
7546 mask = 0;
7547
7548 /* Is there any exceptional events? */
7549 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007550 mask |= EPOLLERR |
7551 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
Davide Libenzif348d702006-03-25 03:07:39 -08007552 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007553 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007555 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556
7557 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00007558 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007559 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560
7561 /* The association is either gone or not ready. */
7562 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
7563 return mask;
7564
7565 /* Is it writable? */
7566 if (sctp_writeable(sk)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007567 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 } else {
Eric Dumazet9cd3e072015-11-29 20:03:10 -08007569 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 /*
7571 * Since the socket is not locked, the buffer
7572 * might be made available after the writeable check and
7573 * before the bit is set. This could cause a lost I/O
7574 * signal. tcp_poll() has a race breaker for this race
7575 * condition. Based on their implementation, we put
7576 * in the following code to cover it as well.
7577 */
7578 if (sctp_writeable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007579 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 }
7581 return mask;
7582}
7583
7584/********************************************************************
7585 * 2nd Level Abstractions
7586 ********************************************************************/
7587
7588static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007589 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590{
7591 struct sctp_bind_bucket *pp;
7592
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08007593 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07007595 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596 pp->port = snum;
7597 pp->fastreuse = 0;
7598 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007599 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007600 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007601 }
7602 return pp;
7603}
7604
7605/* Caller must hold hashbucket lock for this tb with local BH disabled */
7606static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
7607{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07007608 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007609 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 kmem_cache_free(sctp_bucket_cachep, pp);
7611 SCTP_DBG_OBJCNT_DEC(bind_bucket);
7612 }
7613}
7614
7615/* Release this socket's reference to a local port. */
7616static inline void __sctp_put_port(struct sock *sk)
7617{
7618 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007619 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
7620 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621 struct sctp_bind_bucket *pp;
7622
wangweidong3c8e43b2014-01-21 15:44:08 +08007623 spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007624 pp = sctp_sk(sk)->bind_hash;
7625 __sk_del_bind_node(sk);
7626 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007627 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628 sctp_bucket_destroy(pp);
wangweidong3c8e43b2014-01-21 15:44:08 +08007629 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630}
7631
7632void sctp_put_port(struct sock *sk)
7633{
wangweidong79b91132014-01-21 15:44:07 +08007634 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635 __sctp_put_port(sk);
wangweidong79b91132014-01-21 15:44:07 +08007636 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637}
7638
7639/*
7640 * The system picks an ephemeral port and choose an address set equivalent
7641 * to binding with a wildcard address.
7642 * One of those addresses will be the primary address for the association.
7643 * This automatically enables the multihoming capability of SCTP.
7644 */
7645static int sctp_autobind(struct sock *sk)
7646{
7647 union sctp_addr autoaddr;
7648 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08007649 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650
7651 /* Initialize a local sockaddr structure to INADDR_ANY. */
7652 af = sctp_sk(sk)->pf->af;
7653
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007654 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655 af->inaddr_any(&autoaddr, port);
7656
7657 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
7658}
7659
7660/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
7661 *
7662 * From RFC 2292
7663 * 4.2 The cmsghdr Structure *
7664 *
7665 * When ancillary data is sent or received, any number of ancillary data
7666 * objects can be specified by the msg_control and msg_controllen members of
7667 * the msghdr structure, because each object is preceded by
7668 * a cmsghdr structure defining the object's length (the cmsg_len member).
7669 * Historically Berkeley-derived implementations have passed only one object
7670 * at a time, but this API allows multiple objects to be
7671 * passed in a single call to sendmsg() or recvmsg(). The following example
7672 * shows two ancillary data objects in a control buffer.
7673 *
7674 * |<--------------------------- msg_controllen -------------------------->|
7675 * | |
7676 *
7677 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
7678 *
7679 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
7680 * | | |
7681 *
7682 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
7683 *
7684 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
7685 * | | | | |
7686 *
7687 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7688 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
7689 *
7690 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
7691 *
7692 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7693 * ^
7694 * |
7695 *
7696 * msg_control
7697 * points here
7698 */
Xin Longa05437a2017-08-11 10:23:48 +08007699static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700{
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007701 struct msghdr *my_msg = (struct msghdr *)msg;
Xin Longa05437a2017-08-11 10:23:48 +08007702 struct cmsghdr *cmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007703
Gu Zhengf95b4142014-12-11 11:22:04 +08007704 for_each_cmsghdr(cmsg, my_msg) {
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007705 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706 return -EINVAL;
7707
7708 /* Should we parse this header or ignore? */
7709 if (cmsg->cmsg_level != IPPROTO_SCTP)
7710 continue;
7711
7712 /* Strictly check lengths following example in SCM code. */
7713 switch (cmsg->cmsg_type) {
7714 case SCTP_INIT:
7715 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007716 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 *
7718 * This cmsghdr structure provides information for
7719 * initializing new SCTP associations with sendmsg().
7720 * The SCTP_INITMSG socket option uses this same data
7721 * structure. This structure is not used for
7722 * recvmsg().
7723 *
7724 * cmsg_level cmsg_type cmsg_data[]
7725 * ------------ ------------ ----------------------
7726 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
7727 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007728 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729 return -EINVAL;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007730
7731 cmsgs->init = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007732 break;
7733
7734 case SCTP_SNDRCV:
7735 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007736 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007737 *
7738 * This cmsghdr structure specifies SCTP options for
7739 * sendmsg() and describes SCTP header information
7740 * about a received message through recvmsg().
7741 *
7742 * cmsg_level cmsg_type cmsg_data[]
7743 * ------------ ------------ ----------------------
7744 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
7745 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007746 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007747 return -EINVAL;
7748
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007749 cmsgs->srinfo = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007751 if (cmsgs->srinfo->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007752 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Longa6c2f792016-07-09 19:47:43 +08007753 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007754 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755 return -EINVAL;
7756 break;
7757
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007758 case SCTP_SNDINFO:
7759 /* SCTP Socket API Extension
7760 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
7761 *
7762 * This cmsghdr structure specifies SCTP options for
7763 * sendmsg(). This structure and SCTP_RCVINFO replaces
7764 * SCTP_SNDRCV which has been deprecated.
7765 *
7766 * cmsg_level cmsg_type cmsg_data[]
7767 * ------------ ------------ ---------------------
7768 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
7769 */
7770 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
7771 return -EINVAL;
7772
7773 cmsgs->sinfo = CMSG_DATA(cmsg);
7774
7775 if (cmsgs->sinfo->snd_flags &
7776 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Longa6c2f792016-07-09 19:47:43 +08007777 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007778 SCTP_ABORT | SCTP_EOF))
7779 return -EINVAL;
7780 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781 default:
7782 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784 }
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007785
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786 return 0;
7787}
7788
7789/*
7790 * Wait for a packet..
7791 * Note: This function is the same function as in core/datagram.c
7792 * with a few modifications to make lksctp work.
7793 */
wangweidong26ac8e52013-12-23 12:16:51 +08007794static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795{
7796 int error;
7797 DEFINE_WAIT(wait);
7798
Eric Dumazetaa395142010-04-20 13:03:51 +00007799 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800
7801 /* Socket errors? */
7802 error = sock_error(sk);
7803 if (error)
7804 goto out;
7805
7806 if (!skb_queue_empty(&sk->sk_receive_queue))
7807 goto ready;
7808
7809 /* Socket shut down? */
7810 if (sk->sk_shutdown & RCV_SHUTDOWN)
7811 goto out;
7812
7813 /* Sequenced packets can come disconnected. If so we report the
7814 * problem.
7815 */
7816 error = -ENOTCONN;
7817
7818 /* Is there a good reason to think that we may receive some data? */
7819 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
7820 goto out;
7821
7822 /* Handle signals. */
7823 if (signal_pending(current))
7824 goto interrupted;
7825
7826 /* Let another process have a go. Since we are going to sleep
7827 * anyway. Note: This may cause odd behaviors if the message
7828 * does not fit in the user's buffer, but this seems to be the
7829 * only way to honor MSG_DONTWAIT realistically.
7830 */
wangweidong048ed4b2014-01-21 15:44:11 +08007831 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832 *timeo_p = schedule_timeout(*timeo_p);
wangweidong048ed4b2014-01-21 15:44:11 +08007833 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007834
7835ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00007836 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837 return 0;
7838
7839interrupted:
7840 error = sock_intr_errno(*timeo_p);
7841
7842out:
Eric Dumazetaa395142010-04-20 13:03:51 +00007843 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844 *err = error;
7845 return error;
7846}
7847
7848/* Receive a datagram.
7849 * Note: This is pretty much the same routine as in core/datagram.c
7850 * with a few changes to make lksctp work.
7851 */
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007852struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
7853 int noblock, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854{
7855 int error;
7856 struct sk_buff *skb;
7857 long timeo;
7858
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859 timeo = sock_rcvtimeo(sk, noblock);
7860
Daniel Borkmannbb333812013-06-28 19:49:40 +02007861 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
7862 MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863
7864 do {
7865 /* Again only user level code calls this function,
7866 * so nothing interrupt level
7867 * will suddenly eat the receive_queue.
7868 *
7869 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00007870 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871 */
7872 if (flags & MSG_PEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873 skb = skb_peek(&sk->sk_receive_queue);
7874 if (skb)
Reshetova, Elena63354792017-06-30 13:07:58 +03007875 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876 } else {
Marcelo Ricardo Leitner311b2172016-04-13 19:12:29 -03007877 skb = __skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 }
7879
7880 if (skb)
7881 return skb;
7882
Neil Horman6736dc32005-12-02 20:30:06 -08007883 /* Caller is allowed not to check sk->sk_err before calling. */
7884 error = sock_error(sk);
7885 if (error)
7886 goto no_packet;
7887
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888 if (sk->sk_shutdown & RCV_SHUTDOWN)
7889 break;
7890
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07007891 if (sk_can_busy_loop(sk)) {
7892 sk_busy_loop(sk, noblock);
7893
7894 if (!skb_queue_empty(&sk->sk_receive_queue))
7895 continue;
7896 }
Neil Horman8465a5f2014-04-17 15:26:51 -04007897
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898 /* User doesn't want to wait. */
7899 error = -EAGAIN;
7900 if (!timeo)
7901 goto no_packet;
7902 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
7903
7904 return NULL;
7905
7906no_packet:
7907 *err = error;
7908 return NULL;
7909}
7910
7911/* If sndbuf has changed, wake up per association sndbuf waiters. */
7912static void __sctp_write_space(struct sctp_association *asoc)
7913{
7914 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915
Eric Dumazetceb5d582015-11-29 20:03:11 -08007916 if (sctp_wspace(asoc) <= 0)
7917 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007918
Eric Dumazetceb5d582015-11-29 20:03:11 -08007919 if (waitqueue_active(&asoc->wait))
7920 wake_up_interruptible(&asoc->wait);
Eric Dumazeteaefd112011-02-18 03:26:36 +00007921
Eric Dumazetceb5d582015-11-29 20:03:11 -08007922 if (sctp_writeable(sk)) {
7923 struct socket_wq *wq;
7924
7925 rcu_read_lock();
7926 wq = rcu_dereference(sk->sk_wq);
7927 if (wq) {
7928 if (waitqueue_active(&wq->wait))
7929 wake_up_interruptible(&wq->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930
7931 /* Note that we try to include the Async I/O support
7932 * here by modeling from the current TCP/UDP code.
7933 * We have not tested with it yet.
7934 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00007935 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Eric Dumazetceb5d582015-11-29 20:03:11 -08007936 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08007938 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007939 }
7940}
7941
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007942static void sctp_wake_up_waiters(struct sock *sk,
7943 struct sctp_association *asoc)
7944{
7945 struct sctp_association *tmp = asoc;
7946
7947 /* We do accounting for the sndbuf space per association,
7948 * so we only need to wake our own association.
7949 */
7950 if (asoc->ep->sndbuf_policy)
7951 return __sctp_write_space(asoc);
7952
Daniel Borkmann1e1cdf82014-04-09 16:10:20 +02007953 /* If association goes down and is just flushing its
7954 * outq, then just normally notify others.
7955 */
7956 if (asoc->base.dead)
7957 return sctp_write_space(sk);
7958
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007959 /* Accounting for the sndbuf space is per socket, so we
7960 * need to wake up others, try to be fair and in case of
7961 * other associations, let them have a go first instead
7962 * of just doing a sctp_write_space() call.
7963 *
7964 * Note that we reach sctp_wake_up_waiters() only when
7965 * associations free up queued chunks, thus we are under
7966 * lock and the list of associations on a socket is
7967 * guaranteed not to change.
7968 */
7969 for (tmp = list_next_entry(tmp, asocs); 1;
7970 tmp = list_next_entry(tmp, asocs)) {
7971 /* Manually skip the head element. */
7972 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
7973 continue;
7974 /* Wake up association. */
7975 __sctp_write_space(tmp);
7976 /* We've reached the end. */
7977 if (tmp == asoc)
7978 break;
7979 }
7980}
7981
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982/* Do accounting for the sndbuf space.
7983 * Decrement the used sndbuf space of the corresponding association by the
7984 * data size which was just transmitted(freed).
7985 */
7986static void sctp_wfree(struct sk_buff *skb)
7987{
Daniel Borkmannf869c912014-11-20 01:54:48 +01007988 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
7989 struct sctp_association *asoc = chunk->asoc;
7990 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991
Neil Horman4eb701d2005-04-28 12:02:04 -07007992 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
7993 sizeof(struct sk_buff) +
7994 sizeof(struct sctp_chunk);
7995
Reshetova, Elena14afee42017-06-30 13:08:00 +03007996 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
Neil Horman4eb701d2005-04-28 12:02:04 -07007997
Neil Horman4d93df02007-08-15 16:07:44 -07007998 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007999 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
Neil Horman4d93df02007-08-15 16:07:44 -07008000 */
8001 sk->sk_wmem_queued -= skb->truesize;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008002 sk_mem_uncharge(sk, skb->truesize);
Neil Horman4d93df02007-08-15 16:07:44 -07008003
Neil Horman4eb701d2005-04-28 12:02:04 -07008004 sock_wfree(skb);
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008005 sctp_wake_up_waiters(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006
8007 sctp_association_put(asoc);
8008}
8009
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008010/* Do accounting for the receive space on the socket.
8011 * Accounting for the association is done in ulpevent.c
8012 * We set this as a destructor for the cloned data skbs so that
8013 * accounting is done at the correct time.
8014 */
8015void sctp_sock_rfree(struct sk_buff *skb)
8016{
8017 struct sock *sk = skb->sk;
8018 struct sctp_ulpevent *event = sctp_skb2event(skb);
8019
8020 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07008021
8022 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008023 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07008024 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008025 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008026}
8027
8028
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029/* Helper function to wait for space in the sndbuf. */
8030static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +08008031 size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008032{
8033 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034 long current_timeo = *timeo_p;
8035 DEFINE_WAIT(wait);
Xin Longa0ff6602018-01-15 17:01:36 +08008036 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008037
Daniel Borkmannbb333812013-06-28 19:49:40 +02008038 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8039 *timeo_p, msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008040
8041 /* Increment the association's refcnt. */
8042 sctp_association_hold(asoc);
8043
8044 /* Wait on the association specific sndbuf space. */
8045 for (;;) {
8046 prepare_to_wait_exclusive(&asoc->wait, &wait,
8047 TASK_INTERRUPTIBLE);
Xin Longca3af4d2017-11-15 16:55:54 +08008048 if (asoc->base.dead)
8049 goto do_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050 if (!*timeo_p)
8051 goto do_nonblock;
Xin Longca3af4d2017-11-15 16:55:54 +08008052 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008053 goto do_error;
8054 if (signal_pending(current))
8055 goto do_interrupted;
8056 if (msg_len <= sctp_wspace(asoc))
8057 break;
8058
8059 /* Let another process have a go. Since we are going
8060 * to sleep anyway.
8061 */
wangweidong048ed4b2014-01-21 15:44:11 +08008062 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008064 lock_sock(sk);
Xin Longa0ff6602018-01-15 17:01:36 +08008065 if (sk != asoc->base.sk)
8066 goto do_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008067
8068 *timeo_p = current_timeo;
8069 }
8070
8071out:
8072 finish_wait(&asoc->wait, &wait);
8073
8074 /* Release the association's refcnt. */
8075 sctp_association_put(asoc);
8076
8077 return err;
8078
Xin Longca3af4d2017-11-15 16:55:54 +08008079do_dead:
8080 err = -ESRCH;
8081 goto out;
8082
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083do_error:
8084 err = -EPIPE;
8085 goto out;
8086
8087do_interrupted:
8088 err = sock_intr_errno(*timeo_p);
8089 goto out;
8090
8091do_nonblock:
8092 err = -EAGAIN;
8093 goto out;
8094}
8095
David S. Miller676d2362014-04-11 16:15:36 -04008096void sctp_data_ready(struct sock *sk)
Wei Yongjun561b1732010-04-28 08:47:18 +00008097{
David S. Miller7ef52732010-05-02 21:43:40 -07008098 struct socket_wq *wq;
8099
8100 rcu_read_lock();
8101 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08008102 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008103 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8104 EPOLLRDNORM | EPOLLRDBAND);
Wei Yongjun561b1732010-04-28 08:47:18 +00008105 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07008106 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00008107}
8108
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109/* If socket sndbuf has changed, wake up all per association waiters. */
8110void sctp_write_space(struct sock *sk)
8111{
8112 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008113
8114 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07008115 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008116 __sctp_write_space(asoc);
8117 }
8118}
8119
8120/* Is there any sndbuf space available on the socket?
8121 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08008122 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07008123 * associations on the same socket. For a UDP-style socket with
8124 * multiple associations, it is possible for it to be "unwriteable"
8125 * prematurely. I assume that this is acceptable because
8126 * a premature "unwriteable" is better than an accidental "writeable" which
8127 * would cause an unwanted block under certain circumstances. For the 1-1
8128 * UDP-style sockets or TCP-style sockets, this code should work.
8129 * - Daisy
8130 */
8131static int sctp_writeable(struct sock *sk)
8132{
8133 int amt = 0;
8134
Eric Dumazet31e6d362009-06-17 19:05:41 -07008135 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008136 if (amt < 0)
8137 amt = 0;
8138 return amt;
8139}
8140
8141/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8142 * returns immediately with EINPROGRESS.
8143 */
8144static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8145{
8146 struct sock *sk = asoc->base.sk;
8147 int err = 0;
8148 long current_timeo = *timeo_p;
8149 DEFINE_WAIT(wait);
8150
Daniel Borkmannbb333812013-06-28 19:49:40 +02008151 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152
8153 /* Increment the association's refcnt. */
8154 sctp_association_hold(asoc);
8155
8156 for (;;) {
8157 prepare_to_wait_exclusive(&asoc->wait, &wait,
8158 TASK_INTERRUPTIBLE);
8159 if (!*timeo_p)
8160 goto do_nonblock;
8161 if (sk->sk_shutdown & RCV_SHUTDOWN)
8162 break;
8163 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
8164 asoc->base.dead)
8165 goto do_error;
8166 if (signal_pending(current))
8167 goto do_interrupted;
8168
8169 if (sctp_state(asoc, ESTABLISHED))
8170 break;
8171
8172 /* Let another process have a go. Since we are going
8173 * to sleep anyway.
8174 */
wangweidong048ed4b2014-01-21 15:44:11 +08008175 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008176 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008177 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178
8179 *timeo_p = current_timeo;
8180 }
8181
8182out:
8183 finish_wait(&asoc->wait, &wait);
8184
8185 /* Release the association's refcnt. */
8186 sctp_association_put(asoc);
8187
8188 return err;
8189
8190do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08008191 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192 err = -ETIMEDOUT;
8193 else
8194 err = -ECONNREFUSED;
8195 goto out;
8196
8197do_interrupted:
8198 err = sock_intr_errno(*timeo_p);
8199 goto out;
8200
8201do_nonblock:
8202 err = -EINPROGRESS;
8203 goto out;
8204}
8205
8206static int sctp_wait_for_accept(struct sock *sk, long timeo)
8207{
8208 struct sctp_endpoint *ep;
8209 int err = 0;
8210 DEFINE_WAIT(wait);
8211
8212 ep = sctp_sk(sk)->ep;
8213
8214
8215 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00008216 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008217 TASK_INTERRUPTIBLE);
8218
8219 if (list_empty(&ep->asocs)) {
wangweidong048ed4b2014-01-21 15:44:11 +08008220 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221 timeo = schedule_timeout(timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008222 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008223 }
8224
8225 err = -EINVAL;
8226 if (!sctp_sstate(sk, LISTENING))
8227 break;
8228
8229 err = 0;
8230 if (!list_empty(&ep->asocs))
8231 break;
8232
8233 err = sock_intr_errno(timeo);
8234 if (signal_pending(current))
8235 break;
8236
8237 err = -EAGAIN;
8238 if (!timeo)
8239 break;
8240 }
8241
Eric Dumazetaa395142010-04-20 13:03:51 +00008242 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243
8244 return err;
8245}
8246
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02008247static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248{
8249 DEFINE_WAIT(wait);
8250
8251 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00008252 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253 if (list_empty(&sctp_sk(sk)->ep->asocs))
8254 break;
wangweidong048ed4b2014-01-21 15:44:11 +08008255 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008256 timeout = schedule_timeout(timeout);
wangweidong048ed4b2014-01-21 15:44:11 +08008257 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008258 } while (!signal_pending(current) && timeout);
8259
Eric Dumazetaa395142010-04-20 13:03:51 +00008260 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261}
8262
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008263static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
8264{
8265 struct sk_buff *frag;
8266
8267 if (!skb->data_len)
8268 goto done;
8269
8270 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07008271 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008272 sctp_skb_set_owner_r_frag(frag, sk);
8273
8274done:
8275 sctp_skb_set_owner_r(skb, sk);
8276}
8277
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008278void sctp_copy_sock(struct sock *newsk, struct sock *sk,
8279 struct sctp_association *asoc)
8280{
8281 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08008282 struct inet_sock *newinet;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008283
8284 newsk->sk_type = sk->sk_type;
8285 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
8286 newsk->sk_flags = sk->sk_flags;
Marcelo Ricardo Leitner50a5ffb2015-12-04 15:14:05 -02008287 newsk->sk_tsflags = sk->sk_tsflags;
Tom Herbert28448b82014-05-23 08:47:19 -07008288 newsk->sk_no_check_tx = sk->sk_no_check_tx;
8289 newsk->sk_no_check_rx = sk->sk_no_check_rx;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008290 newsk->sk_reuse = sk->sk_reuse;
8291
8292 newsk->sk_shutdown = sk->sk_shutdown;
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02008293 newsk->sk_destruct = sctp_destruct_sock;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008294 newsk->sk_family = sk->sk_family;
8295 newsk->sk_protocol = IPPROTO_SCTP;
8296 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
8297 newsk->sk_sndbuf = sk->sk_sndbuf;
8298 newsk->sk_rcvbuf = sk->sk_rcvbuf;
8299 newsk->sk_lingertime = sk->sk_lingertime;
8300 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
8301 newsk->sk_sndtimeo = sk->sk_sndtimeo;
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03008302 newsk->sk_rxhash = sk->sk_rxhash;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008303
8304 newinet = inet_sk(newsk);
8305
8306 /* Initialize sk's sport, dport, rcv_saddr and daddr for
8307 * getsockname() and getpeername()
8308 */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008309 newinet->inet_sport = inet->inet_sport;
8310 newinet->inet_saddr = inet->inet_saddr;
8311 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
8312 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008313 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008314 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008315
8316 newinet->uc_ttl = inet->uc_ttl;
8317 newinet->mc_loop = 1;
8318 newinet->mc_ttl = 1;
8319 newinet->mc_index = 0;
8320 newinet->mc_list = NULL;
Marcelo Ricardo Leitner01ce63c2015-12-04 15:14:04 -02008321
8322 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
8323 net_enable_timestamp();
Marcelo Ricardo Leitner3538a5c2015-12-23 16:44:09 -02008324
8325 security_sk_clone(sk, newsk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008326}
8327
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008328static inline void sctp_copy_descendant(struct sock *sk_to,
8329 const struct sock *sk_from)
8330{
8331 int ancestor_size = sizeof(struct inet_sock) +
8332 sizeof(struct sctp_sock) -
8333 offsetof(struct sctp_sock, auto_asconf_list);
8334
8335 if (sk_from->sk_family == PF_INET6)
8336 ancestor_size += sizeof(struct ipv6_pinfo);
8337
8338 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
8339}
8340
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341/* Populate the fields of the newsk from the oldsk and migrate the assoc
8342 * and its messages to the newsk.
8343 */
8344static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
8345 struct sctp_association *assoc,
Xin Longb7ef2612017-08-11 10:23:50 +08008346 enum sctp_socket_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008347{
8348 struct sctp_sock *oldsp = sctp_sk(oldsk);
8349 struct sctp_sock *newsp = sctp_sk(newsk);
8350 struct sctp_bind_bucket *pp; /* hash list port iterator */
8351 struct sctp_endpoint *newep = newsp->ep;
8352 struct sk_buff *skb, *tmp;
8353 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08008354 struct sctp_bind_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355
8356 /* Migrate socket buffer sizes and all the socket level options to the
8357 * new socket.
8358 */
8359 newsk->sk_sndbuf = oldsk->sk_sndbuf;
8360 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
8361 /* Brute force copy old sctp opt. */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008362 sctp_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008363
8364 /* Restore the ep value that was overwritten with the above structure
8365 * copy.
8366 */
8367 newsp->ep = newep;
8368 newsp->hmac = NULL;
8369
8370 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008371 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
8372 inet_sk(oldsk)->inet_num)];
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008373 spin_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374 pp = sctp_sk(oldsk)->bind_hash;
8375 sk_add_bind_node(newsk, &pp->owner);
8376 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008377 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008378 spin_unlock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008380 /* Copy the bind_addr list from the original endpoint to the new
8381 * endpoint so that we can handle restarts properly
8382 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08008383 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
8384 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008385
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386 /* Move any messages in the old socket's receive queue that are for the
8387 * peeled off association to the new socket's receive queue.
8388 */
8389 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
8390 event = sctp_skb2event(skb);
8391 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008392 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008393 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008394 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008395 }
8396 }
8397
8398 /* Clean up any messages pending delivery due to partial
8399 * delivery. Three cases:
8400 * 1) No partial deliver; no work.
8401 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
8402 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
8403 */
8404 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008405 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008406
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008407 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408 struct sk_buff_head *queue;
8409
8410 /* Decide which queue to move pd_lobby skbs to. */
8411 if (assoc->ulpq.pd_mode) {
8412 queue = &newsp->pd_lobby;
8413 } else
8414 queue = &newsk->sk_receive_queue;
8415
8416 /* Walk through the pd_lobby, looking for skbs that
8417 * need moved to the new socket.
8418 */
8419 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
8420 event = sctp_skb2event(skb);
8421 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008422 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008423 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008424 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008425 }
8426 }
8427
8428 /* Clear up any skbs waiting for the partial
8429 * delivery to finish.
8430 */
8431 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008432 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433
8434 }
8435
Xin Long13228232017-12-08 21:04:09 +08008436 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008437
Linus Torvalds1da177e2005-04-16 15:20:36 -07008438 /* Set the type of socket to indicate that it is peeled off from the
8439 * original UDP-style socket or created with the accept() call on a
8440 * TCP-style socket..
8441 */
8442 newsp->type = type;
8443
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008444 /* Mark the new socket "in-use" by the user so that any packets
8445 * that may arrive on the association after we've moved it are
8446 * queued to the backlog. This prevents a potential race between
8447 * backlog processing on the old socket and new-packet processing
8448 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07008449 *
8450 * The caller has just allocated newsk so we can guarantee that other
8451 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008452 */
Zach Brown5131a182007-06-22 15:14:46 -07008453 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Xin Longd04adf12017-10-28 02:13:29 +08008454 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008455 sctp_assoc_migrate(assoc, newsk);
Xin Longd04adf12017-10-28 02:13:29 +08008456 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008457
8458 /* If the association on the newsk is already closed before accept()
8459 * is called, set RCV_SHUTDOWN flag.
8460 */
Xin Longd46e4162016-06-09 22:48:18 +08008461 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08008462 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463 newsk->sk_shutdown |= RCV_SHUTDOWN;
Xin Longd46e4162016-06-09 22:48:18 +08008464 } else {
Yafang Shaocbabf462017-12-20 11:12:54 +08008465 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
Xin Longd46e4162016-06-09 22:48:18 +08008466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008467
wangweidong048ed4b2014-01-21 15:44:11 +08008468 release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469}
8470
Neil Horman4d93df02007-08-15 16:07:44 -07008471
Linus Torvalds1da177e2005-04-16 15:20:36 -07008472/* This proto struct describes the ULP interface for SCTP. */
8473struct proto sctp_prot = {
8474 .name = "SCTP",
8475 .owner = THIS_MODULE,
8476 .close = sctp_close,
8477 .connect = sctp_connect,
8478 .disconnect = sctp_disconnect,
8479 .accept = sctp_accept,
8480 .ioctl = sctp_ioctl,
8481 .init = sctp_init_sock,
8482 .destroy = sctp_destroy_sock,
8483 .shutdown = sctp_shutdown,
8484 .setsockopt = sctp_setsockopt,
8485 .getsockopt = sctp_getsockopt,
8486 .sendmsg = sctp_sendmsg,
8487 .recvmsg = sctp_recvmsg,
8488 .bind = sctp_bind,
8489 .backlog_rcv = sctp_backlog_rcv,
8490 .hash = sctp_hash,
8491 .unhash = sctp_unhash,
8492 .get_port = sctp_get_port,
8493 .obj_size = sizeof(struct sctp_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07008494 .useroffset = offsetof(struct sctp_sock, subscribe),
8495 .usersize = offsetof(struct sctp_sock, initmsg) -
8496 offsetof(struct sctp_sock, subscribe) +
8497 sizeof_field(struct sctp_sock, initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07008498 .sysctl_mem = sysctl_sctp_mem,
8499 .sysctl_rmem = sysctl_sctp_rmem,
8500 .sysctl_wmem = sysctl_sctp_wmem,
8501 .memory_pressure = &sctp_memory_pressure,
8502 .enter_memory_pressure = sctp_enter_memory_pressure,
8503 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008504 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008505};
8506
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008507#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08008508
Eric Dumazet602dd622015-12-01 07:20:07 -08008509#include <net/transp_v6.h>
8510static void sctp_v6_destroy_sock(struct sock *sk)
8511{
8512 sctp_destroy_sock(sk);
8513 inet6_destroy_sock(sk);
8514}
8515
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516struct proto sctpv6_prot = {
8517 .name = "SCTPv6",
8518 .owner = THIS_MODULE,
8519 .close = sctp_close,
8520 .connect = sctp_connect,
8521 .disconnect = sctp_disconnect,
8522 .accept = sctp_accept,
8523 .ioctl = sctp_ioctl,
8524 .init = sctp_init_sock,
Eric Dumazet602dd622015-12-01 07:20:07 -08008525 .destroy = sctp_v6_destroy_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008526 .shutdown = sctp_shutdown,
8527 .setsockopt = sctp_setsockopt,
8528 .getsockopt = sctp_getsockopt,
8529 .sendmsg = sctp_sendmsg,
8530 .recvmsg = sctp_recvmsg,
8531 .bind = sctp_bind,
8532 .backlog_rcv = sctp_backlog_rcv,
8533 .hash = sctp_hash,
8534 .unhash = sctp_unhash,
8535 .get_port = sctp_get_port,
8536 .obj_size = sizeof(struct sctp6_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07008537 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
8538 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
8539 offsetof(struct sctp6_sock, sctp.subscribe) +
8540 sizeof_field(struct sctp6_sock, sctp.initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07008541 .sysctl_mem = sysctl_sctp_mem,
8542 .sysctl_rmem = sysctl_sctp_rmem,
8543 .sysctl_wmem = sysctl_sctp_wmem,
8544 .memory_pressure = &sctp_memory_pressure,
8545 .enter_memory_pressure = sctp_enter_memory_pressure,
8546 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008547 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008548};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008549#endif /* IS_ENABLED(CONFIG_IPV6) */