blob: bf089e59c79261d9bb2354a3b2883a7717a69fab [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
8 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05009 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
13 *
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
22 * any later version.
23 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050024 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080031 * along with GNU CC; see the file COPYING. If not, see
32 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
34 * Please send any bug reports or fixes you make to the
35 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020036 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * Written or modified by:
39 * La Monte H.P. Yarroll <piggy@acm.org>
40 * Narasimha Budihal <narsi@refcode.org>
41 * Karl Knutson <karl@athena.chicago.il.us>
42 * Jon Grimm <jgrimm@us.ibm.com>
43 * Xingang Guo <xingang.guo@intel.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Sridhar Samudrala <samudrala@us.ibm.com>
46 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
47 * Ardelle Fan <ardelle.fan@intel.com>
48 * Ryan Layer <rmlayer@us.ibm.com>
49 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
50 * Kevin Gao <kevin.gao@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
52
Joe Perches145ce502010-08-24 13:21:08 +000053#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
Herbert Xu5821c762016-01-24 21:20:12 +080055#include <crypto/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/types.h>
57#include <linux/kernel.h>
58#include <linux/wait.h>
59#include <linux/time.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010060#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/ip.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080062#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/fcntl.h>
64#include <linux/poll.h>
65#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090066#include <linux/slab.h>
Al Viro56b31d12012-08-18 00:25:51 -040067#include <linux/file.h>
Daniel Borkmannffd59392014-02-17 12:11:11 +010068#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#include <net/ip.h>
71#include <net/icmp.h>
72#include <net/route.h>
73#include <net/ipv6.h>
74#include <net/inet_common.h>
Neil Horman8465a5f2014-04-17 15:26:51 -040075#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#include <linux/socket.h> /* for sa_family_t */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040078#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <net/sock.h>
80#include <net/sctp/sctp.h>
81#include <net/sctp/sm.h>
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -030082#include <net/sctp/stream_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/* Forward declarations for internal helper functions. */
85static int sctp_writeable(struct sock *sk);
86static void sctp_wfree(struct sk_buff *skb);
Xin Longcea0cc82017-11-15 16:57:26 +080087static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +080088 size_t msg_len);
wangweidong26ac8e52013-12-23 12:16:51 +080089static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
91static int sctp_wait_for_accept(struct sock *sk, long timeo);
92static void sctp_wait_for_close(struct sock *sk, long timeo);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +020093static void sctp_destruct_sock(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
95 union sctp_addr *addr, int len);
96static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
97static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
98static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
99static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
100static int sctp_send_asconf(struct sctp_association *asoc,
101 struct sctp_chunk *chunk);
102static int sctp_do_bind(struct sock *, union sctp_addr *, int);
103static int sctp_autobind(struct sock *sk);
Xin Longb7ef2612017-08-11 10:23:50 +0800104static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
105 struct sctp_association *assoc,
106 enum sctp_socket_type type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Eric Dumazet06044752017-06-07 13:29:12 -0700108static unsigned long sctp_memory_pressure;
Eric Dumazet8d987e52010-11-09 23:24:26 +0000109static atomic_long_t sctp_memory_allocated;
Eric Dumazet17483762008-11-25 21:16:35 -0800110struct percpu_counter sctp_sockets_allocated;
Neil Horman4d93df02007-08-15 16:07:44 -0700111
Pavel Emelyanov5c52ba12008-07-16 20:28:10 -0700112static void sctp_enter_memory_pressure(struct sock *sk)
Neil Horman4d93df02007-08-15 16:07:44 -0700113{
114 sctp_memory_pressure = 1;
115}
116
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/* Get the sndbuf space available at the time on the association. */
119static inline int sctp_wspace(struct sctp_association *asoc)
120{
Neil Horman4d93df02007-08-15 16:07:44 -0700121 int amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Neil Horman4d93df02007-08-15 16:07:44 -0700123 if (asoc->ep->sndbuf_policy)
124 amt = asoc->sndbuf_used;
125 else
Eric Dumazet31e6d362009-06-17 19:05:41 -0700126 amt = sk_wmem_alloc_get(asoc->base.sk);
Neil Horman4d93df02007-08-15 16:07:44 -0700127
128 if (amt >= asoc->base.sk->sk_sndbuf) {
129 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
130 amt = 0;
131 else {
132 amt = sk_stream_wspace(asoc->base.sk);
133 if (amt < 0)
134 amt = 0;
135 }
Neil Horman4eb701d2005-04-28 12:02:04 -0700136 } else {
Neil Horman4d93df02007-08-15 16:07:44 -0700137 amt = asoc->base.sk->sk_sndbuf - amt;
Neil Horman4eb701d2005-04-28 12:02:04 -0700138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return amt;
140}
141
142/* Increment the used sndbuf space count of the corresponding association by
143 * the size of the outgoing data chunk.
144 * Also, set the skb destructor for sndbuf accounting later.
145 *
146 * Since it is always 1-1 between chunk and skb, and also a new skb is always
147 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
148 * destructor in the data chunk skb for the purpose of the sndbuf space
149 * tracking.
150 */
151static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
152{
153 struct sctp_association *asoc = chunk->asoc;
154 struct sock *sk = asoc->base.sk;
155
156 /* The sndbuf space is tracked per association. */
157 sctp_association_hold(asoc);
158
Neil Horman4eb701d2005-04-28 12:02:04 -0700159 skb_set_owner_w(chunk->skb, sk);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 chunk->skb->destructor = sctp_wfree;
162 /* Save the chunk pointer in skb for sctp_wfree to use later. */
Daniel Borkmannf869c912014-11-20 01:54:48 +0100163 skb_shinfo(chunk->skb)->destructor_arg = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Neil Horman4eb701d2005-04-28 12:02:04 -0700165 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
166 sizeof(struct sk_buff) +
167 sizeof(struct sctp_chunk);
168
Reshetova, Elena14afee42017-06-30 13:08:00 +0300169 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800170 sk->sk_wmem_queued += chunk->skb->truesize;
171 sk_mem_charge(sk, chunk->skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Xin Longd04adf12017-10-28 02:13:29 +0800174static void sctp_clear_owner_w(struct sctp_chunk *chunk)
175{
176 skb_orphan(chunk->skb);
177}
178
179static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
180 void (*cb)(struct sctp_chunk *))
181
182{
183 struct sctp_outq *q = &asoc->outqueue;
184 struct sctp_transport *t;
185 struct sctp_chunk *chunk;
186
187 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
188 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
189 cb(chunk);
190
Xin Longa8dd3972017-11-26 20:56:07 +0800191 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800192 cb(chunk);
193
Xin Longa8dd3972017-11-26 20:56:07 +0800194 list_for_each_entry(chunk, &q->sacked, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800195 cb(chunk);
196
Xin Longa8dd3972017-11-26 20:56:07 +0800197 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800198 cb(chunk);
199
200 list_for_each_entry(chunk, &q->out_chunk_list, list)
201 cb(chunk);
202}
203
Xin Long13228232017-12-08 21:04:09 +0800204static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
205 void (*cb)(struct sk_buff *, struct sock *))
206
207{
208 struct sk_buff *skb, *tmp;
209
210 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
211 cb(skb, sk);
212
213 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
214 cb(skb, sk);
215
216 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
217 cb(skb, sk);
218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/* Verify that this is a valid address. */
221static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
222 int len)
223{
224 struct sctp_af *af;
225
226 /* Verify basic sockaddr. */
227 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
228 if (!af)
229 return -EINVAL;
230
231 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700232 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -EINVAL;
234
235 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
236 return -EINVAL;
237
238 return 0;
239}
240
241/* Look up the association by its id. If this is not a UDP-style
242 * socket, the ID field is always ignored.
243 */
244struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
245{
246 struct sctp_association *asoc = NULL;
247
248 /* If this is not a UDP-style socket, assoc id should be ignored. */
249 if (!sctp_style(sk, UDP)) {
250 /* Return NULL if the socket state is not ESTABLISHED. It
251 * could be a TCP-style listening socket or a socket which
252 * hasn't yet called connect() to establish an association.
253 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -0300254 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return NULL;
256
257 /* Get the first and the only association from the list. */
258 if (!list_empty(&sctp_sk(sk)->ep->asocs))
259 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
260 struct sctp_association, asocs);
261 return asoc;
262 }
263
264 /* Otherwise this is a UDP-style socket. */
265 if (!id || (id == (sctp_assoc_t)-1))
266 return NULL;
267
268 spin_lock_bh(&sctp_assocs_id_lock);
269 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
270 spin_unlock_bh(&sctp_assocs_id_lock);
271
272 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
273 return NULL;
274
275 return asoc;
276}
277
278/* Look up the transport from an address and an assoc id. If both address and
279 * id are specified, the associations matching the address and the id should be
280 * the same.
281 */
282static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
283 struct sockaddr_storage *addr,
284 sctp_assoc_t id)
285{
286 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
Xin Long6f29a132017-01-24 14:01:53 +0800287 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 union sctp_addr *laddr = (union sctp_addr *)addr;
Xin Long6f29a132017-01-24 14:01:53 +0800289 struct sctp_transport *transport;
290
Xin Long912964e2017-02-07 20:56:08 +0800291 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
Xin Long6f29a132017-01-24 14:01:53 +0800292 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800295 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 if (!addr_asoc)
299 return NULL;
300
301 id_asoc = sctp_id2assoc(sk, id);
302 if (id_asoc && (id_asoc != addr_asoc))
303 return NULL;
304
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600305 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 (union sctp_addr *)addr);
307
308 return transport;
309}
310
311/* API 3.1.2 bind() - UDP Style Syntax
312 * The syntax of bind() is,
313 *
314 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
315 *
316 * sd - the socket descriptor returned by socket().
317 * addr - the address structure (struct sockaddr_in or struct
318 * sockaddr_in6 [RFC 2553]),
319 * addr_len - the size of the address structure.
320 */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200321static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 int retval = 0;
324
wangweidong048ed4b2014-01-21 15:44:11 +0800325 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Daniel Borkmannbb333812013-06-28 19:49:40 +0200327 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
328 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* Disallow binding twice. */
331 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700332 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 addr_len);
334 else
335 retval = -EINVAL;
336
wangweidong048ed4b2014-01-21 15:44:11 +0800337 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 return retval;
340}
341
342static long sctp_get_port_local(struct sock *, union sctp_addr *);
343
344/* Verify this is a valid sockaddr. */
345static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
346 union sctp_addr *addr, int len)
347{
348 struct sctp_af *af;
349
350 /* Check minimum size. */
351 if (len < sizeof (struct sockaddr))
352 return NULL;
353
Xin Longc5006b82018-01-15 17:02:00 +0800354 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
355 return NULL;
356
Vlad Yasevich7dab83d2008-07-18 23:05:40 -0700357 /* V4 mapped address are really of AF_INET family */
358 if (addr->sa.sa_family == AF_INET6 &&
Xin Longc5006b82018-01-15 17:02:00 +0800359 ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
360 !opt->pf->af_supported(AF_INET, opt))
361 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* If we get this far, af is valid. */
364 af = sctp_get_af_specific(addr->sa.sa_family);
365
366 if (len < af->sockaddr_len)
367 return NULL;
368
369 return af;
370}
371
372/* Bind a local address either to an endpoint or to an association. */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200373static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Eric W. Biederman35946982012-11-16 03:03:12 +0000375 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct sctp_sock *sp = sctp_sk(sk);
377 struct sctp_endpoint *ep = sp->ep;
378 struct sctp_bind_addr *bp = &ep->base.bind_addr;
379 struct sctp_af *af;
380 unsigned short snum;
381 int ret = 0;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* Common sockaddr verification. */
384 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700385 if (!af) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200386 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
387 __func__, sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700389 }
390
391 snum = ntohs(addr->v4.sin_port);
392
Daniel Borkmannbb333812013-06-28 19:49:40 +0200393 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
394 __func__, sk, &addr->sa, bp->port, snum, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /* PF specific bind() address verification. */
397 if (!sp->pf->bind_verify(sp, addr))
398 return -EADDRNOTAVAIL;
399
Vlad Yasevich8b358052007-05-15 17:14:58 -0400400 /* We must either be unbound, or bind to the same port.
401 * It's OK to allow 0 ports if we are already bound.
402 * We'll just inhert an already bound port in this case
403 */
404 if (bp->port) {
405 if (!snum)
406 snum = bp->port;
407 else if (snum != bp->port) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200408 pr_debug("%s: new port %d doesn't match existing port "
409 "%d\n", __func__, snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400410 return -EINVAL;
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
Krister Johansen4548b682017-01-20 17:49:11 -0800414 if (snum && snum < inet_prot_sock(net) &&
Eric W. Biederman35946982012-11-16 03:03:12 +0000415 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return -EACCES;
417
Vlad Yasevich4e540642008-07-18 23:06:32 -0700418 /* See if the address matches any of the addresses we may have
419 * already bound before checking against other endpoints.
420 */
421 if (sctp_bind_addr_match(bp, addr, sp))
422 return -EINVAL;
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* Make sure we are allowed to bind here.
425 * The function sctp_get_port_local() does duplicate address
426 * detection.
427 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900428 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if ((ret = sctp_get_port_local(sk, addr))) {
Vlad Yasevich4e540642008-07-18 23:06:32 -0700430 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
433 /* Refresh ephemeral port. */
434 if (!bp->port)
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000435 bp->port = inet_sk(sk)->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Vlad Yasevich559cf712007-09-16 16:03:28 -0700437 /* Add the address to the bind address list.
438 * Use GFP_ATOMIC since BHs will be disabled.
439 */
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300440 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
441 SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 /* Copy back into socket for getsockname() use. */
444 if (!ret) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000445 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600446 sp->pf->to_sk_saddr(addr, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
449 return ret;
450}
451
452 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
453 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900454 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900456 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900458 * subsequent ASCONF. Note this restriction binds each side, so at any
459 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * from each endpoint).
461 */
462static int sctp_send_asconf(struct sctp_association *asoc,
463 struct sctp_chunk *chunk)
464{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000465 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int retval = 0;
467
468 /* If there is an outstanding ASCONF chunk, queue it for later
469 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900470 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700472 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900473 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475
476 /* Hold the chunk until an ASCONF_ACK is received. */
477 sctp_chunk_hold(chunk);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000478 retval = sctp_primitive_ASCONF(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (retval)
480 sctp_chunk_free(chunk);
481 else
482 asoc->addip_last_asconf = chunk;
483
484out:
485 return retval;
486}
487
488/* Add a list of addresses as bind addresses to local endpoint or
489 * association.
490 *
491 * Basically run through each address specified in the addrs/addrcnt
492 * array/length pair, determine if it is IPv6 or IPv4 and call
493 * sctp_do_bind() on it.
494 *
495 * If any of them fails, then the operation will be reversed and the
496 * ones that were added will be removed.
497 *
498 * Only sctp_setsockopt_bindx() is supposed to call this function.
499 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200500static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 int cnt;
503 int retval = 0;
504 void *addr_buf;
505 struct sockaddr *sa_addr;
506 struct sctp_af *af;
507
Daniel Borkmannbb333812013-06-28 19:49:40 +0200508 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
509 addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 addr_buf = addrs;
512 for (cnt = 0; cnt < addrcnt; cnt++) {
513 /* The list may contain either IPv4 or IPv6 address;
514 * determine the address length for walking thru the list.
515 */
Joe Perchesea110732011-06-13 16:21:26 +0000516 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 af = sctp_get_af_specific(sa_addr->sa_family);
518 if (!af) {
519 retval = -EINVAL;
520 goto err_bindx_add;
521 }
522
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900523 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 af->sockaddr_len);
525
526 addr_buf += af->sockaddr_len;
527
528err_bindx_add:
529 if (retval < 0) {
530 /* Failed. Cleanup the ones that have been added */
531 if (cnt > 0)
532 sctp_bindx_rem(sk, addrs, cnt);
533 return retval;
534 }
535 }
536
537 return retval;
538}
539
540/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
541 * associations that are part of the endpoint indicating that a list of local
542 * addresses are added to the endpoint.
543 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900544 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 * association, we do not send the chunk for that association. But it will not
546 * affect other associations.
547 *
548 * Only sctp_setsockopt_bindx() is supposed to call this function.
549 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900550static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct sockaddr *addrs,
552 int addrcnt)
553{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000554 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct sctp_sock *sp;
556 struct sctp_endpoint *ep;
557 struct sctp_association *asoc;
558 struct sctp_bind_addr *bp;
559 struct sctp_chunk *chunk;
560 struct sctp_sockaddr_entry *laddr;
561 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700562 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 void *addr_buf;
564 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct list_head *p;
566 int i;
567 int retval = 0;
568
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000569 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return retval;
571
572 sp = sctp_sk(sk);
573 ep = sp->ep;
574
Daniel Borkmannbb333812013-06-28 19:49:40 +0200575 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
576 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700578 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (!asoc->peer.asconf_capable)
580 continue;
581
582 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
583 continue;
584
585 if (!sctp_state(asoc, ESTABLISHED))
586 continue;
587
588 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900589 * in the bind address list of the association. If so,
590 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * other associations.
592 */
593 addr_buf = addrs;
594 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000595 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 af = sctp_get_af_specific(addr->v4.sin_family);
597 if (!af) {
598 retval = -EINVAL;
599 goto out;
600 }
601
602 if (sctp_assoc_lookup_laddr(asoc, addr))
603 break;
604
605 addr_buf += af->sockaddr_len;
606 }
607 if (i < addrcnt)
608 continue;
609
Vlad Yasevich559cf712007-09-16 16:03:28 -0700610 /* Use the first valid address in bind addr list of
611 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 bp = &asoc->base.bind_addr;
614 p = bp->address_list.next;
615 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800616 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 addrcnt, SCTP_PARAM_ADD_IP);
618 if (!chunk) {
619 retval = -ENOMEM;
620 goto out;
621 }
622
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700623 /* Add the new addresses to the bind address list with
624 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700626 addr_buf = addrs;
627 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000628 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700629 af = sctp_get_af_specific(addr->v4.sin_family);
630 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800631 retval = sctp_add_bind_addr(bp, &saveaddr,
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300632 sizeof(saveaddr),
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800633 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700634 addr_buf += af->sockaddr_len;
635 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900636 if (asoc->src_out_of_asoc_ok) {
637 struct sctp_transport *trans;
638
639 list_for_each_entry(trans,
640 &asoc->peer.transport_addr_list, transports) {
641 /* Clear the source and route cache */
Julian Anastasovc86a7732017-02-06 23:14:13 +0200642 sctp_transport_dst_release(trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900643 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
644 2*asoc->pathmtu, 4380));
645 trans->ssthresh = asoc->peer.i.a_rwnd;
646 trans->rto = asoc->rto_initial;
Michele Baldessari196d6752012-12-01 04:49:42 +0000647 sctp_max_rto(asoc, trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900648 trans->rtt = trans->srtt = trans->rttvar = 0;
649 sctp_transport_route(trans, NULL,
650 sctp_sk(asoc->base.sk));
651 }
652 }
653 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655
656out:
657 return retval;
658}
659
660/* Remove a list of addresses from bind addresses list. Do not remove the
661 * last address.
662 *
663 * Basically run through each address specified in the addrs/addrcnt
664 * array/length pair, determine if it is IPv6 or IPv4 and call
665 * sctp_del_bind() on it.
666 *
667 * If any of them fails, then the operation will be reversed and the
668 * ones that were removed will be added back.
669 *
670 * At least one address has to be left; if only one address is
671 * available, the operation will return -EBUSY.
672 *
673 * Only sctp_setsockopt_bindx() is supposed to call this function.
674 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200675static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 struct sctp_sock *sp = sctp_sk(sk);
678 struct sctp_endpoint *ep = sp->ep;
679 int cnt;
680 struct sctp_bind_addr *bp = &ep->base.bind_addr;
681 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800683 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct sctp_af *af;
685
Daniel Borkmannbb333812013-06-28 19:49:40 +0200686 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
687 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 addr_buf = addrs;
690 for (cnt = 0; cnt < addrcnt; cnt++) {
691 /* If the bind address list is empty or if there is only one
692 * bind address, there is nothing more to be removed (we need
693 * at least one address here).
694 */
695 if (list_empty(&bp->address_list) ||
696 (sctp_list_single_entry(&bp->address_list))) {
697 retval = -EBUSY;
698 goto err_bindx_rem;
699 }
700
Joe Perchesea110732011-06-13 16:21:26 +0000701 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800702 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (!af) {
704 retval = -EINVAL;
705 goto err_bindx_rem;
706 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700707
708 if (!af->addr_valid(sa_addr, sp, NULL)) {
709 retval = -EADDRNOTAVAIL;
710 goto err_bindx_rem;
711 }
712
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000713 if (sa_addr->v4.sin_port &&
714 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 retval = -EINVAL;
716 goto err_bindx_rem;
717 }
718
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000719 if (!sa_addr->v4.sin_port)
720 sa_addr->v4.sin_port = htons(bp->port);
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* FIXME - There is probably a need to check if sk->sk_saddr and
723 * sk->sk_rcv_addr are currently set to one of the addresses to
724 * be removed. This is something which needs to be looked into
725 * when we are fixing the outstanding issues with multi-homing
726 * socket routing and failover schemes. Refer to comments in
727 * sctp_do_bind(). -daisy
728 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400729 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 addr_buf += af->sockaddr_len;
732err_bindx_rem:
733 if (retval < 0) {
734 /* Failed. Add the ones that has been removed back */
735 if (cnt > 0)
736 sctp_bindx_add(sk, addrs, cnt);
737 return retval;
738 }
739 }
740
741 return retval;
742}
743
744/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
745 * the associations that are part of the endpoint indicating that a list of
746 * local addresses are removed from the endpoint.
747 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900748 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * association, we do not send the chunk for that association. But it will not
750 * affect other associations.
751 *
752 * Only sctp_setsockopt_bindx() is supposed to call this function.
753 */
754static int sctp_send_asconf_del_ip(struct sock *sk,
755 struct sockaddr *addrs,
756 int addrcnt)
757{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000758 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 struct sctp_sock *sp;
760 struct sctp_endpoint *ep;
761 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700762 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct sctp_bind_addr *bp;
764 struct sctp_chunk *chunk;
765 union sctp_addr *laddr;
766 void *addr_buf;
767 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700768 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int i;
770 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900771 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Michio Honda8a07eb02011-04-26 20:19:36 +0900773 chunk = NULL;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000774 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return retval;
776
777 sp = sctp_sk(sk);
778 ep = sp->ep;
779
Daniel Borkmannbb333812013-06-28 19:49:40 +0200780 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
781 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700783 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 if (!asoc->peer.asconf_capable)
786 continue;
787
788 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
789 continue;
790
791 if (!sctp_state(asoc, ESTABLISHED))
792 continue;
793
794 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900795 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 * If so, do not send the asconf chunk to its peer, but
797 * continue with other associations.
798 */
799 addr_buf = addrs;
800 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000801 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 af = sctp_get_af_specific(laddr->v4.sin_family);
803 if (!af) {
804 retval = -EINVAL;
805 goto out;
806 }
807
808 if (!sctp_assoc_lookup_laddr(asoc, laddr))
809 break;
810
811 addr_buf += af->sockaddr_len;
812 }
813 if (i < addrcnt)
814 continue;
815
816 /* Find one address in the association's bind address list
817 * that is not in the packed array of addresses. This is to
818 * make sure that we do not delete all the addresses in the
819 * association.
820 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 bp = &asoc->base.bind_addr;
822 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
823 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900824 if ((laddr == NULL) && (addrcnt == 1)) {
825 if (asoc->asconf_addr_del_pending)
826 continue;
827 asoc->asconf_addr_del_pending =
828 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900829 if (asoc->asconf_addr_del_pending == NULL) {
830 retval = -ENOMEM;
831 goto out;
832 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900833 asoc->asconf_addr_del_pending->sa.sa_family =
834 addrs->sa_family;
835 asoc->asconf_addr_del_pending->v4.sin_port =
836 htons(bp->port);
837 if (addrs->sa_family == AF_INET) {
838 struct sockaddr_in *sin;
839
840 sin = (struct sockaddr_in *)addrs;
841 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
842 } else if (addrs->sa_family == AF_INET6) {
843 struct sockaddr_in6 *sin6;
844
845 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000846 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900847 }
Daniel Borkmannbb333812013-06-28 19:49:40 +0200848
849 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
850 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
851 asoc->asconf_addr_del_pending);
852
Michio Honda8a07eb02011-04-26 20:19:36 +0900853 asoc->src_out_of_asoc_ok = 1;
854 stored = 1;
855 goto skip_mkasconf;
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Daniel Borkmann88362ad2013-09-07 20:51:21 +0200858 if (laddr == NULL)
859 return -EINVAL;
860
Vlad Yasevich559cf712007-09-16 16:03:28 -0700861 /* We do not need RCU protection throughout this loop
862 * because this is done under a socket lock from the
863 * setsockopt call.
864 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
866 SCTP_PARAM_DEL_IP);
867 if (!chunk) {
868 retval = -ENOMEM;
869 goto out;
870 }
871
Michio Honda8a07eb02011-04-26 20:19:36 +0900872skip_mkasconf:
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700873 /* Reset use_as_src flag for the addresses in the bind address
874 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700876 addr_buf = addrs;
877 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000878 laddr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700879 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700880 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a12006-11-20 17:05:23 -0800881 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800882 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700883 }
884 addr_buf += af->sockaddr_len;
885 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700886
887 /* Update the route and saddr entries for all the transports
888 * as some of the addresses in the bind address list are
889 * about to be deleted and cannot be used as source addresses.
890 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700891 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
892 transports) {
Julian Anastasovc86a7732017-02-06 23:14:13 +0200893 sctp_transport_dst_release(transport);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700894 sctp_transport_route(transport, NULL,
895 sctp_sk(asoc->base.sk));
896 }
897
Michio Honda8a07eb02011-04-26 20:19:36 +0900898 if (stored)
899 /* We don't need to transmit ASCONF */
900 continue;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700901 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903out:
904 return retval;
905}
906
Michio Honda9f7d6532011-04-26 19:32:51 +0900907/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
908int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
909{
910 struct sock *sk = sctp_opt2sk(sp);
911 union sctp_addr *addr;
912 struct sctp_af *af;
913
914 /* It is safe to write port space in caller. */
915 addr = &addrw->a;
916 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
917 af = sctp_get_af_specific(addr->sa.sa_family);
918 if (!af)
919 return -EINVAL;
920 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
921 return -EINVAL;
922
923 if (addrw->state == SCTP_ADDR_NEW)
924 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
925 else
926 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
930 *
931 * API 8.1
932 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
933 * int flags);
934 *
935 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
936 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
937 * or IPv6 addresses.
938 *
939 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
940 * Section 3.1.2 for this usage.
941 *
942 * addrs is a pointer to an array of one or more socket addresses. Each
943 * address is contained in its appropriate structure (i.e. struct
944 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700945 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * representation is termed a "packed array" of addresses). The caller
947 * specifies the number of addresses in the array with addrcnt.
948 *
949 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
950 * -1, and sets errno to the appropriate error code.
951 *
952 * For SCTP, the port given in each socket address must be the same, or
953 * sctp_bindx() will fail, setting errno to EINVAL.
954 *
955 * The flags parameter is formed from the bitwise OR of zero or more of
956 * the following currently defined flags:
957 *
958 * SCTP_BINDX_ADD_ADDR
959 *
960 * SCTP_BINDX_REM_ADDR
961 *
962 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
963 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
964 * addresses from the association. The two flags are mutually exclusive;
965 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
966 * not remove all addresses from an association; sctp_bindx() will
967 * reject such an attempt with EINVAL.
968 *
969 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
970 * additional addresses with an endpoint after calling bind(). Or use
971 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
972 * socket is associated with so that no new association accepted will be
973 * associated with those addresses. If the endpoint supports dynamic
974 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
975 * endpoint to send the appropriate message to the peer to change the
976 * peers address lists.
977 *
978 * Adding and removing addresses from a connected association is
979 * optional functionality. Implementations that do not support this
980 * functionality should return EOPNOTSUPP.
981 *
982 * Basically do nothing but copying the addresses from user to kernel
983 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700984 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
985 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
988 * it.
989 *
990 * sk The sk of the socket
991 * addrs The pointer to the addresses in user land
992 * addrssize Size of the addrs buffer
993 * op Operation to perform (add or remove, see the flags of
994 * sctp_bindx)
995 *
996 * Returns 0 if ok, <0 errno code on error.
997 */
wangweidong26ac8e52013-12-23 12:16:51 +0800998static int sctp_setsockopt_bindx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +0200999 struct sockaddr __user *addrs,
1000 int addrs_size, int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 struct sockaddr *kaddrs;
1003 int err;
1004 int addrcnt = 0;
1005 int walk_size = 0;
1006 struct sockaddr *sa_addr;
1007 void *addr_buf;
1008 struct sctp_af *af;
1009
Daniel Borkmannbb333812013-06-28 19:49:40 +02001010 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1011 __func__, sk, addrs, addrs_size, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 if (unlikely(addrs_size <= 0))
1014 return -EINVAL;
1015
Al Viroc981f252018-01-07 13:19:09 -05001016 kaddrs = vmemdup_user(addrs, addrs_size);
1017 if (unlikely(IS_ERR(kaddrs)))
1018 return PTR_ERR(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001020 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 addr_buf = kaddrs;
1022 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001023 if (walk_size + sizeof(sa_family_t) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001024 kvfree(kaddrs);
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001025 return -EINVAL;
1026 }
1027
Joe Perchesea110732011-06-13 16:21:26 +00001028 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 af = sctp_get_af_specific(sa_addr->sa_family);
1030
1031 /* If the address family is not supported or if this address
1032 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001033 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001035 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return -EINVAL;
1037 }
1038 addrcnt++;
1039 addr_buf += af->sockaddr_len;
1040 walk_size += af->sockaddr_len;
1041 }
1042
1043 /* Do the work. */
1044 switch (op) {
1045 case SCTP_BINDX_ADD_ADDR:
1046 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1047 if (err)
1048 goto out;
1049 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1050 break;
1051
1052 case SCTP_BINDX_REM_ADDR:
1053 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1054 if (err)
1055 goto out;
1056 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1057 break;
1058
1059 default:
1060 err = -EINVAL;
1061 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064out:
Al Viroc981f252018-01-07 13:19:09 -05001065 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 return err;
1068}
1069
Frank Filz3f7a87d2005-06-20 13:14:57 -07001070/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1071 *
1072 * Common routine for handling connect() and sctp_connectx().
1073 * Connect will come in with just a single address.
1074 */
wangweidong26ac8e52013-12-23 12:16:51 +08001075static int __sctp_connect(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001076 struct sockaddr *kaddrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001077 int addrs_size,
1078 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001079{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001080 struct net *net = sock_net(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001081 struct sctp_sock *sp;
1082 struct sctp_endpoint *ep;
1083 struct sctp_association *asoc = NULL;
1084 struct sctp_association *asoc2;
1085 struct sctp_transport *transport;
1086 union sctp_addr to;
Xin Long1c662012017-08-05 19:59:54 +08001087 enum sctp_scope scope;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001088 long timeo;
1089 int err = 0;
1090 int addrcnt = 0;
1091 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001092 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001093 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001094 unsigned short port;
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001095 unsigned int f_flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001096
1097 sp = sctp_sk(sk);
1098 ep = sp->ep;
1099
1100 /* connect() cannot be done on a socket that is already in ESTABLISHED
1101 * state - UDP-style peeled off socket or a TCP-style socket that
1102 * is already connected.
1103 * It cannot be done even on a TCP-style listening socket.
1104 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03001105 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
Frank Filz3f7a87d2005-06-20 13:14:57 -07001106 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1107 err = -EISCONN;
1108 goto out_free;
1109 }
1110
1111 /* Walk through the addrs buffer and count the number of addresses. */
1112 addr_buf = kaddrs;
1113 while (walk_size < addrs_size) {
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001114 struct sctp_af *af;
1115
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001116 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1117 err = -EINVAL;
1118 goto out_free;
1119 }
1120
Joe Perchesea110732011-06-13 16:21:26 +00001121 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001122 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001123
1124 /* If the address family is not supported or if this address
1125 * causes the address buffer to overflow return EINVAL.
1126 */
1127 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1128 err = -EINVAL;
1129 goto out_free;
1130 }
1131
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001132 port = ntohs(sa_addr->v4.sin_port);
1133
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001134 /* Save current address so we can work with it */
1135 memcpy(&to, sa_addr, af->sockaddr_len);
1136
1137 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001138 if (err)
1139 goto out_free;
1140
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001141 /* Make sure the destination port is correctly set
1142 * in all addresses.
1143 */
Wei Yongjun524fba62013-04-03 03:02:28 +00001144 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1145 err = -EINVAL;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001146 goto out_free;
Wei Yongjun524fba62013-04-03 03:02:28 +00001147 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001148
1149 /* Check if there already is a matching association on the
1150 * endpoint (other than the one created here).
1151 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001152 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001153 if (asoc2 && asoc2 != asoc) {
1154 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1155 err = -EISCONN;
1156 else
1157 err = -EALREADY;
1158 goto out_free;
1159 }
1160
1161 /* If we could not find a matching association on the endpoint,
1162 * make sure that there is no peeled-off association matching
1163 * the peer address even on another socket.
1164 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001165 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001166 err = -EADDRNOTAVAIL;
1167 goto out_free;
1168 }
1169
1170 if (!asoc) {
1171 /* If a bind() or sctp_bindx() is not called prior to
1172 * an sctp_connectx() call, the system picks an
1173 * ephemeral port and will choose an address set
1174 * equivalent to binding with a wildcard address.
1175 */
1176 if (!ep->base.bind_addr.port) {
1177 if (sctp_autobind(sk)) {
1178 err = -EAGAIN;
1179 goto out_free;
1180 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001181 } else {
1182 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001183 * If an unprivileged user inherits a 1-many
1184 * style socket with open associations on a
1185 * privileged port, it MAY be permitted to
1186 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001187 * be permitted to open new associations.
1188 */
Krister Johansen4548b682017-01-20 17:49:11 -08001189 if (ep->base.bind_addr.port <
1190 inet_prot_sock(net) &&
1191 !ns_capable(net->user_ns,
1192 CAP_NET_BIND_SERVICE)) {
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001193 err = -EACCES;
1194 goto out_free;
1195 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001196 }
1197
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001198 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001199 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1200 if (!asoc) {
1201 err = -ENOMEM;
1202 goto out_free;
1203 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001204
1205 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1206 GFP_KERNEL);
1207 if (err < 0) {
1208 goto out_free;
1209 }
1210
Frank Filz3f7a87d2005-06-20 13:14:57 -07001211 }
1212
1213 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001214 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001215 SCTP_UNKNOWN);
1216 if (!transport) {
1217 err = -ENOMEM;
1218 goto out_free;
1219 }
1220
1221 addrcnt++;
1222 addr_buf += af->sockaddr_len;
1223 walk_size += af->sockaddr_len;
1224 }
1225
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001226 /* In case the user of sctp_connectx() wants an association
1227 * id back, assign one now.
1228 */
1229 if (assoc_id) {
1230 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1231 if (err < 0)
1232 goto out_free;
1233 }
1234
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001235 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001236 if (err < 0) {
1237 goto out_free;
1238 }
1239
1240 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001241 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001242 sp->pf->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001243 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001244
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001245 /* in-kernel sockets don't generally have a file allocated to them
1246 * if all they do is call sock_create_kern().
1247 */
1248 if (sk->sk_socket->file)
1249 f_flags = sk->sk_socket->file->f_flags;
1250
1251 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1252
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001253 if (assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001254 *assoc_id = asoc->assoc_id;
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001255 err = sctp_wait_for_connect(asoc, &timeo);
1256 /* Note: the asoc may be freed after the return of
1257 * sctp_wait_for_connect.
1258 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001259
1260 /* Don't free association on exit. */
1261 asoc = NULL;
1262
1263out_free:
Daniel Borkmannbb333812013-06-28 19:49:40 +02001264 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1265 __func__, asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001266
Neil Horman2eebc1e2012-07-16 09:13:51 +00001267 if (asoc) {
1268 /* sctp_primitive_ASSOCIATE may have added this association
1269 * To the hash table, try to unhash it, just in case, its a noop
1270 * if it wasn't hashed so we're safe
1271 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001272 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001273 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001274 return err;
1275}
1276
1277/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1278 *
1279 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001280 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1281 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001282 *
1283 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1284 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1285 * or IPv6 addresses.
1286 *
1287 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1288 * Section 3.1.2 for this usage.
1289 *
1290 * addrs is a pointer to an array of one or more socket addresses. Each
1291 * address is contained in its appropriate structure (i.e. struct
1292 * sockaddr_in or struct sockaddr_in6) the family of the address type
1293 * must be used to distengish the address length (note that this
1294 * representation is termed a "packed array" of addresses). The caller
1295 * specifies the number of addresses in the array with addrcnt.
1296 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001297 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1298 * the association id of the new association. On failure, sctp_connectx()
1299 * returns -1, and sets errno to the appropriate error code. The assoc_id
1300 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001301 *
1302 * For SCTP, the port given in each socket address must be the same, or
1303 * sctp_connectx() will fail, setting errno to EINVAL.
1304 *
1305 * An application can use sctp_connectx to initiate an association with
1306 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1307 * allows a caller to specify multiple addresses at which a peer can be
1308 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001309 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001310 * specifies that the stack will try to make use of all the addresses in
1311 * the list when needed.
1312 *
1313 * Note that the list of addresses passed in is only used for setting up
1314 * the association. It does not necessarily equal the set of addresses
1315 * the peer uses for the resulting association. If the caller wants to
1316 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1317 * retrieve them after the association has been set up.
1318 *
1319 * Basically do nothing but copying the addresses from user to kernel
1320 * land and invoking either sctp_connectx(). This is used for tunneling
1321 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1322 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07001323 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1324 * it.
1325 *
1326 * sk The sk of the socket
1327 * addrs The pointer to the addresses in user land
1328 * addrssize Size of the addrs buffer
1329 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001330 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001331 */
wangweidong26ac8e52013-12-23 12:16:51 +08001332static int __sctp_setsockopt_connectx(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001333 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001334 int addrs_size,
1335 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001336{
Frank Filz3f7a87d2005-06-20 13:14:57 -07001337 struct sockaddr *kaddrs;
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02001338 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001339
Daniel Borkmannbb333812013-06-28 19:49:40 +02001340 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1341 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001342
1343 if (unlikely(addrs_size <= 0))
1344 return -EINVAL;
1345
Al Viroc981f252018-01-07 13:19:09 -05001346 kaddrs = vmemdup_user(addrs, addrs_size);
1347 if (unlikely(IS_ERR(kaddrs)))
1348 return PTR_ERR(kaddrs);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001349
Al Viroc981f252018-01-07 13:19:09 -05001350 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
1351 kvfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001352
Frank Filz3f7a87d2005-06-20 13:14:57 -07001353 return err;
1354}
1355
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001356/*
1357 * This is an older interface. It's kept for backward compatibility
1358 * to the option that doesn't provide association id.
1359 */
wangweidong26ac8e52013-12-23 12:16:51 +08001360static int sctp_setsockopt_connectx_old(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001361 struct sockaddr __user *addrs,
1362 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001363{
1364 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1365}
1366
1367/*
1368 * New interface for the API. The since the API is done with a socket
1369 * option, to make it simple we feed back the association id is as a return
1370 * indication to the call. Error is always negative and association id is
1371 * always positive.
1372 */
wangweidong26ac8e52013-12-23 12:16:51 +08001373static int sctp_setsockopt_connectx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001374 struct sockaddr __user *addrs,
1375 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001376{
1377 sctp_assoc_t assoc_id = 0;
1378 int err = 0;
1379
1380 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1381
1382 if (err)
1383 return err;
1384 else
1385 return assoc_id;
1386}
1387
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001388/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001389 * New (hopefully final) interface for the API.
1390 * We use the sctp_getaddrs_old structure so that use-space library
Daniel Borkmannffd59392014-02-17 12:11:11 +01001391 * can avoid any unnecessary allocations. The only different part
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001392 * is that we store the actual length of the address buffer into the
Daniel Borkmannffd59392014-02-17 12:11:11 +01001393 * addrs_num structure member. That way we can re-use the existing
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001394 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001395 */
Daniel Borkmannffd59392014-02-17 12:11:11 +01001396#ifdef CONFIG_COMPAT
1397struct compat_sctp_getaddrs_old {
1398 sctp_assoc_t assoc_id;
1399 s32 addr_num;
1400 compat_uptr_t addrs; /* struct sockaddr * */
1401};
1402#endif
1403
wangweidong26ac8e52013-12-23 12:16:51 +08001404static int sctp_getsockopt_connectx3(struct sock *sk, int len,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001405 char __user *optval,
1406 int __user *optlen)
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001407{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001408 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001409 sctp_assoc_t assoc_id = 0;
1410 int err = 0;
1411
Daniel Borkmannffd59392014-02-17 12:11:11 +01001412#ifdef CONFIG_COMPAT
Andy Lutomirski96c0e0a2016-03-22 14:25:07 -07001413 if (in_compat_syscall()) {
Daniel Borkmannffd59392014-02-17 12:11:11 +01001414 struct compat_sctp_getaddrs_old param32;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001415
Daniel Borkmannffd59392014-02-17 12:11:11 +01001416 if (len < sizeof(param32))
1417 return -EINVAL;
1418 if (copy_from_user(&param32, optval, sizeof(param32)))
1419 return -EFAULT;
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001420
Daniel Borkmannffd59392014-02-17 12:11:11 +01001421 param.assoc_id = param32.assoc_id;
1422 param.addr_num = param32.addr_num;
1423 param.addrs = compat_ptr(param32.addrs);
1424 } else
1425#endif
1426 {
1427 if (len < sizeof(param))
1428 return -EINVAL;
1429 if (copy_from_user(&param, optval, sizeof(param)))
1430 return -EFAULT;
1431 }
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001432
Daniel Borkmannffd59392014-02-17 12:11:11 +01001433 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1434 param.addrs, param.addr_num,
1435 &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001436 if (err == 0 || err == -EINPROGRESS) {
1437 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1438 return -EFAULT;
1439 if (put_user(sizeof(assoc_id), optlen))
1440 return -EFAULT;
1441 }
1442
1443 return err;
1444}
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446/* API 3.1.4 close() - UDP Style Syntax
1447 * Applications use close() to perform graceful shutdown (as described in
1448 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1449 * by a UDP-style socket.
1450 *
1451 * The syntax is
1452 *
1453 * ret = close(int sd);
1454 *
1455 * sd - the socket descriptor of the associations to be closed.
1456 *
1457 * To gracefully shutdown a specific association represented by the
1458 * UDP-style socket, an application should use the sendmsg() call,
1459 * passing no user data, but including the appropriate flag in the
1460 * ancillary data (see Section xxxx).
1461 *
1462 * If sd in the close() call is a branched-off socket representing only
1463 * one association, the shutdown is performed on that association only.
1464 *
1465 * 4.1.6 close() - TCP Style Syntax
1466 *
1467 * Applications use close() to gracefully close down an association.
1468 *
1469 * The syntax is:
1470 *
1471 * int close(int sd);
1472 *
1473 * sd - the socket descriptor of the association to be closed.
1474 *
1475 * After an application calls close() on a socket descriptor, no further
1476 * socket operations will succeed on that descriptor.
1477 *
1478 * API 7.1.4 SO_LINGER
1479 *
1480 * An application using the TCP-style socket can use this option to
1481 * perform the SCTP ABORT primitive. The linger option structure is:
1482 *
1483 * struct linger {
1484 * int l_onoff; // option on/off
1485 * int l_linger; // linger time
1486 * };
1487 *
1488 * To enable the option, set l_onoff to 1. If the l_linger value is set
1489 * to 0, calling close() is the same as the ABORT primitive. If the
1490 * value is set to a negative value, the setsockopt() call will return
1491 * an error. If the value is set to a positive value linger_time, the
1492 * close() can be blocked for at most linger_time ms. If the graceful
1493 * shutdown phase does not finish during this period, close() will
1494 * return but the graceful shutdown phase continues in the system.
1495 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02001496static void sctp_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001498 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 struct sctp_endpoint *ep;
1500 struct sctp_association *asoc;
1501 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001502 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Daniel Borkmannbb333812013-06-28 19:49:40 +02001504 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Xin Long6dfe4b92017-06-10 14:56:56 +08001506 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 sk->sk_shutdown = SHUTDOWN_MASK;
Yafang Shaocbabf462017-12-20 11:12:54 +08001508 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 ep = sctp_sk(sk)->ep;
1511
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001512 /* Clean up any skbs sitting on the receive queue. */
1513 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1514 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1515
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001516 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 list_for_each_safe(pos, temp, &ep->asocs) {
1518 asoc = list_entry(pos, struct sctp_association, asocs);
1519
1520 if (sctp_style(sk, TCP)) {
1521 /* A closed association can still be in the list if
1522 * it belongs to a TCP-style listening socket that is
1523 * not yet accepted. If so, free it. If not, send an
1524 * ABORT or SHUTDOWN based on the linger options.
1525 */
1526 if (sctp_state(asoc, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001528 continue;
1529 }
1530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001532 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1533 !skb_queue_empty(&asoc->ulpq.reasm) ||
Xin Long13228232017-12-08 21:04:09 +08001534 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001535 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001536 struct sctp_chunk *chunk;
1537
1538 chunk = sctp_make_abort_user(asoc, NULL, 0);
Xin Long068d8bd2015-12-29 17:49:25 +08001539 sctp_primitive_ABORT(net, asoc, chunk);
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001540 } else
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001541 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /* On a TCP-style socket, block for at most linger_time if set. */
1545 if (sctp_style(sk, TCP) && timeout)
1546 sctp_wait_for_close(sk, timeout);
1547
1548 /* This will run the backlog queue. */
wangweidong048ed4b2014-01-21 15:44:11 +08001549 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 /* Supposedly, no process has access to the socket, but
1552 * the net layers still may.
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001553 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1554 * held and that should be grabbed before socket lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001556 spin_lock_bh(&net->sctp.addr_wq_lock);
Xin Long6dfe4b92017-06-10 14:56:56 +08001557 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 /* Hold the sock, since sk_common_release() will put sock_put()
1560 * and we have just a little more cleanup.
1561 */
1562 sock_hold(sk);
1563 sk_common_release(sk);
1564
wangweidong5bc1d1b2014-01-21 15:44:12 +08001565 bh_unlock_sock(sk);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001566 spin_unlock_bh(&net->sctp.addr_wq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 sock_put(sk);
1569
1570 SCTP_DBG_OBJCNT_DEC(sock);
1571}
1572
1573/* Handle EPIPE error. */
1574static int sctp_error(struct sock *sk, int flags, int err)
1575{
1576 if (err == -EPIPE)
1577 err = sock_error(sk) ? : -EPIPE;
1578 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1579 send_sig(SIGPIPE, current, 0);
1580 return err;
1581}
1582
1583/* API 3.1.3 sendmsg() - UDP Style Syntax
1584 *
1585 * An application uses sendmsg() and recvmsg() calls to transmit data to
1586 * and receive data from its peer.
1587 *
1588 * ssize_t sendmsg(int socket, const struct msghdr *message,
1589 * int flags);
1590 *
1591 * socket - the socket descriptor of the endpoint.
1592 * message - pointer to the msghdr structure which contains a single
1593 * user message and possibly some ancillary data.
1594 *
1595 * See Section 5 for complete description of the data
1596 * structures.
1597 *
1598 * flags - flags sent or received with the user message, see Section
1599 * 5 for complete description of the flags.
1600 *
1601 * Note: This function could use a rewrite especially when explicit
1602 * connect support comes in.
1603 */
1604/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1605
Xin Longa05437a2017-08-11 10:23:48 +08001606static int sctp_msghdr_parse(const struct msghdr *msg,
1607 struct sctp_cmsgs *cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Xin Long204f8172018-03-01 23:05:14 +08001609static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1610 struct sctp_sndrcvinfo *srinfo,
1611 const struct msghdr *msg, size_t msg_len)
1612{
1613 __u16 sflags;
1614 int err;
1615
1616 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1617 return -EPIPE;
1618
1619 if (msg_len > sk->sk_sndbuf)
1620 return -EMSGSIZE;
1621
1622 memset(cmsgs, 0, sizeof(*cmsgs));
1623 err = sctp_msghdr_parse(msg, cmsgs);
1624 if (err) {
1625 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1626 return err;
1627 }
1628
1629 memset(srinfo, 0, sizeof(*srinfo));
1630 if (cmsgs->srinfo) {
1631 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1632 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1633 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1634 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1635 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1636 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1637 }
1638
1639 if (cmsgs->sinfo) {
1640 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1641 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1642 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1643 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1644 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1645 }
1646
1647 sflags = srinfo->sinfo_flags;
1648 if (!sflags && msg_len)
1649 return 0;
1650
1651 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1652 return -EINVAL;
1653
1654 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1655 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1656 return -EINVAL;
1657
1658 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1659 return -EINVAL;
1660
1661 return 0;
1662}
1663
Xin Long2bfd80f2018-03-01 23:05:11 +08001664static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1665 struct sctp_cmsgs *cmsgs,
1666 union sctp_addr *daddr,
1667 struct sctp_transport **tp)
1668{
1669 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1670 struct net *net = sock_net(sk);
1671 struct sctp_association *asoc;
1672 enum sctp_scope scope;
1673 int err = -EINVAL;
1674
1675 *tp = NULL;
1676
1677 if (sflags & (SCTP_EOF | SCTP_ABORT))
1678 return -EINVAL;
1679
1680 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1681 sctp_sstate(sk, CLOSING)))
1682 return -EADDRNOTAVAIL;
1683
1684 if (sctp_endpoint_is_peeled_off(ep, daddr))
1685 return -EADDRNOTAVAIL;
1686
1687 if (!ep->base.bind_addr.port) {
1688 if (sctp_autobind(sk))
1689 return -EAGAIN;
1690 } else {
1691 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1692 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1693 return -EACCES;
1694 }
1695
1696 scope = sctp_scope(daddr);
1697
1698 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1699 if (!asoc)
1700 return -ENOMEM;
1701
1702 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1703 err = -ENOMEM;
1704 goto free;
1705 }
1706
1707 if (cmsgs->init) {
1708 struct sctp_initmsg *init = cmsgs->init;
1709
1710 if (init->sinit_num_ostreams) {
1711 __u16 outcnt = init->sinit_num_ostreams;
1712
1713 asoc->c.sinit_num_ostreams = outcnt;
1714 /* outcnt has been changed, need to re-init stream */
1715 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1716 GFP_KERNEL);
1717 if (err)
1718 goto free;
1719 }
1720
1721 if (init->sinit_max_instreams)
1722 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1723
1724 if (init->sinit_max_attempts)
1725 asoc->max_init_attempts = init->sinit_max_attempts;
1726
1727 if (init->sinit_max_init_timeo)
1728 asoc->max_init_timeo =
1729 msecs_to_jiffies(init->sinit_max_init_timeo);
1730 }
1731
1732 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1733 if (!*tp) {
1734 err = -ENOMEM;
1735 goto free;
1736 }
1737
1738 return 0;
1739
1740free:
1741 sctp_association_free(asoc);
1742 return err;
1743}
1744
Xin Longc2666de2018-03-01 23:05:12 +08001745static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1746 __u16 sflags, struct msghdr *msg,
1747 size_t msg_len)
1748{
1749 struct sock *sk = asoc->base.sk;
1750 struct net *net = sock_net(sk);
1751
1752 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1753 return -EPIPE;
1754
1755 if (sflags & SCTP_EOF) {
1756 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1757 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1758
1759 return 0;
1760 }
1761
1762 if (sflags & SCTP_ABORT) {
1763 struct sctp_chunk *chunk;
1764
1765 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1766 if (!chunk)
1767 return -ENOMEM;
1768
1769 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1770 sctp_primitive_ABORT(net, asoc, chunk);
1771
1772 return 0;
1773 }
1774
1775 return 1;
1776}
1777
Xin Longf84af332018-03-01 23:05:10 +08001778static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1779 struct msghdr *msg, size_t msg_len,
1780 struct sctp_transport *transport,
1781 struct sctp_sndrcvinfo *sinfo)
1782{
1783 struct sock *sk = asoc->base.sk;
1784 struct net *net = sock_net(sk);
1785 struct sctp_datamsg *datamsg;
1786 bool wait_connect = false;
1787 struct sctp_chunk *chunk;
1788 long timeo;
1789 int err;
1790
1791 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1792 err = -EINVAL;
1793 goto err;
1794 }
1795
1796 if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) {
1797 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1798 if (err)
1799 goto err;
1800 }
1801
1802 if (sctp_sk(sk)->disable_fragments && msg_len > asoc->frag_point) {
1803 err = -EMSGSIZE;
1804 goto err;
1805 }
1806
1807 if (sctp_state(asoc, CLOSED)) {
1808 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1809 if (err)
1810 goto err;
1811
1812 if (sctp_sk(sk)->strm_interleave) {
1813 timeo = sock_sndtimeo(sk, 0);
1814 err = sctp_wait_for_connect(asoc, &timeo);
1815 if (err)
1816 goto err;
1817 } else {
1818 wait_connect = true;
1819 }
1820
1821 pr_debug("%s: we associated primitively\n", __func__);
1822 }
1823
1824 if (asoc->pmtu_pending)
1825 sctp_assoc_pending_pmtu(asoc);
1826
1827 if (sctp_wspace(asoc) < msg_len)
1828 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1829
1830 if (!sctp_wspace(asoc)) {
1831 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1832 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1833 if (err)
1834 goto err;
1835 }
1836
1837 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1838 if (IS_ERR(datamsg)) {
1839 err = PTR_ERR(datamsg);
1840 goto err;
1841 }
1842
1843 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1844
1845 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1846 sctp_chunk_hold(chunk);
1847 sctp_set_owner_w(chunk);
1848 chunk->transport = transport;
1849 }
1850
1851 err = sctp_primitive_SEND(net, asoc, datamsg);
1852 if (err) {
1853 sctp_datamsg_free(datamsg);
1854 goto err;
1855 }
1856
1857 pr_debug("%s: we sent primitively\n", __func__);
1858
1859 sctp_datamsg_put(datamsg);
1860
1861 if (unlikely(wait_connect)) {
1862 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1863 sctp_wait_for_connect(asoc, &timeo);
1864 }
1865
1866 err = msg_len;
1867
1868err:
1869 return err;
1870}
1871
Xin Longbecef9b2018-03-01 23:05:13 +08001872static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1873 const struct msghdr *msg,
1874 struct sctp_cmsgs *cmsgs)
1875{
1876 union sctp_addr *daddr = NULL;
1877 int err;
1878
1879 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1880 int len = msg->msg_namelen;
1881
1882 if (len > sizeof(*daddr))
1883 len = sizeof(*daddr);
1884
1885 daddr = (union sctp_addr *)msg->msg_name;
1886
1887 err = sctp_verify_addr(sk, daddr, len);
1888 if (err)
1889 return ERR_PTR(err);
1890 }
1891
1892 return daddr;
1893}
1894
Ying Xue1b784142015-03-02 15:37:48 +08001895static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Xin Long204f8172018-03-01 23:05:14 +08001897 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
wangweidongcb3f8372013-12-23 12:16:50 +08001898 struct sctp_association *new_asoc = NULL, *asoc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 struct sctp_transport *transport, *chunk_tp;
Xin Long204f8172018-03-01 23:05:14 +08001900 struct sctp_sndrcvinfo _sinfo, *sinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 sctp_assoc_t associd = 0;
Xin Longa05437a2017-08-11 10:23:48 +08001902 struct sctp_cmsgs cmsgs = { NULL };
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001903 __u16 sinfo_flags = 0;
Xin Longbecef9b2018-03-01 23:05:13 +08001904 union sctp_addr *daddr;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001905 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Xin Long204f8172018-03-01 23:05:14 +08001907 /* Parse and get snd_info */
1908 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
1909 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 goto out_nounlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Xin Long204f8172018-03-01 23:05:14 +08001912 sinfo = &_sinfo;
1913 sinfo_flags = sinfo->sinfo_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Xin Longbecef9b2018-03-01 23:05:13 +08001915 /* Get daddr from msg */
1916 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
1917 if (IS_ERR(daddr)) {
1918 err = PTR_ERR(daddr);
1919 goto out_nounlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921
wangweidong048ed4b2014-01-21 15:44:11 +08001922 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 /* If a msg_name has been specified, assume this is to be used. */
Xin Longbecef9b2018-03-01 23:05:13 +08001925 if (daddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 /* Look for a matching association on the endpoint. */
Xin Longbecef9b2018-03-01 23:05:13 +08001927 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 } else {
1929 asoc = sctp_id2assoc(sk, associd);
1930 if (!asoc) {
1931 err = -EPIPE;
1932 goto out_unlock;
1933 }
1934 }
1935
1936 if (asoc) {
Xin Longc2666de2018-03-01 23:05:12 +08001937 err = sctp_sendmsg_check_sflags(asoc, sinfo_flags, msg,
1938 msg_len);
1939 if (err <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942
1943 /* Do we need to create the association? */
1944 if (!asoc) {
Xin Longbecef9b2018-03-01 23:05:13 +08001945 err = sctp_sendmsg_new_asoc(sk, sinfo_flags, &cmsgs, daddr,
Xin Long2bfd80f2018-03-01 23:05:11 +08001946 &transport);
1947 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Xin Long2bfd80f2018-03-01 23:05:11 +08001950 asoc = transport->asoc;
1951 new_asoc = asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953
Xin Long204f8172018-03-01 23:05:14 +08001954 if (!cmsgs.srinfo && !cmsgs.sinfo) {
1955 sinfo->sinfo_stream = asoc->default_stream;
1956 sinfo->sinfo_ppid = asoc->default_ppid;
1957 sinfo->sinfo_context = asoc->default_context;
1958 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
1959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Xin Long204f8172018-03-01 23:05:14 +08001961 if (!cmsgs.srinfo)
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001962 sinfo->sinfo_timetolive = asoc->default_timetolive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 /* If an address is passed with the sendto/sendmsg call, it is used
1965 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001966 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 */
Xin Longbecef9b2018-03-01 23:05:13 +08001968 if ((sctp_style(sk, TCP) && daddr) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001969 (sinfo_flags & SCTP_ADDR_OVER)) {
Xin Longbecef9b2018-03-01 23:05:13 +08001970 chunk_tp = sctp_assoc_lookup_paddr(asoc, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (!chunk_tp) {
1972 err = -EINVAL;
1973 goto out_free;
1974 }
1975 } else
1976 chunk_tp = NULL;
1977
Xin Longf84af332018-03-01 23:05:10 +08001978 /* Send msg to the asoc */
1979 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, chunk_tp, sinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981out_free:
Xin Longf84af332018-03-01 23:05:10 +08001982 if (err < 0 && err != -ESRCH && new_asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 sctp_association_free(asoc);
1984out_unlock:
wangweidong048ed4b2014-01-21 15:44:11 +08001985 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986out_nounlock:
Xin Longf84af332018-03-01 23:05:10 +08001987 return sctp_error(sk, msg->msg_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989
1990/* This is an extended version of skb_pull() that removes the data from the
1991 * start of a skb even when data is spread across the list of skb's in the
1992 * frag_list. len specifies the total amount of data that needs to be removed.
1993 * when 'len' bytes could be removed from the skb, it returns 0.
1994 * If 'len' exceeds the total skb length, it returns the no. of bytes that
1995 * could not be removed.
1996 */
1997static int sctp_skb_pull(struct sk_buff *skb, int len)
1998{
1999 struct sk_buff *list;
2000 int skb_len = skb_headlen(skb);
2001 int rlen;
2002
2003 if (len <= skb_len) {
2004 __skb_pull(skb, len);
2005 return 0;
2006 }
2007 len -= skb_len;
2008 __skb_pull(skb, skb_len);
2009
David S. Miller1b003be2009-06-09 00:22:35 -07002010 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 rlen = sctp_skb_pull(list, len);
2012 skb->len -= (len-rlen);
2013 skb->data_len -= (len-rlen);
2014
2015 if (!rlen)
2016 return 0;
2017
2018 len = rlen;
2019 }
2020
2021 return len;
2022}
2023
2024/* API 3.1.3 recvmsg() - UDP Style Syntax
2025 *
2026 * ssize_t recvmsg(int socket, struct msghdr *message,
2027 * int flags);
2028 *
2029 * socket - the socket descriptor of the endpoint.
2030 * message - pointer to the msghdr structure which contains a single
2031 * user message and possibly some ancillary data.
2032 *
2033 * See Section 5 for complete description of the data
2034 * structures.
2035 *
2036 * flags - flags sent or received with the user message, see Section
2037 * 5 for complete description of the flags.
2038 */
Ying Xue1b784142015-03-02 15:37:48 +08002039static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2040 int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
2042 struct sctp_ulpevent *event = NULL;
2043 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002044 struct sk_buff *skb, *head_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int copied;
2046 int err = 0;
2047 int skb_len;
2048
Daniel Borkmannbb333812013-06-28 19:49:40 +02002049 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2050 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2051 addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
wangweidong048ed4b2014-01-21 15:44:11 +08002053 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03002055 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
Xin Longe0878692016-07-30 14:14:41 +08002056 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 err = -ENOTCONN;
2058 goto out;
2059 }
2060
2061 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2062 if (!skb)
2063 goto out;
2064
2065 /* Get the total length of the skb including any skb's in the
2066 * frag_list.
2067 */
2068 skb_len = skb->len;
2069
2070 copied = skb_len;
2071 if (copied > len)
2072 copied = len;
2073
David S. Miller51f3d022014-11-05 16:46:40 -05002074 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 event = sctp_skb2event(skb);
2077
2078 if (err)
2079 goto out_free;
2080
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002081 if (event->chunk && event->chunk->head_skb)
2082 head_skb = event->chunk->head_skb;
2083 else
2084 head_skb = skb;
2085 sock_recv_ts_and_drops(msg, sk, head_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (sctp_ulpevent_is_notification(event)) {
2087 msg->msg_flags |= MSG_NOTIFICATION;
2088 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2089 } else {
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002090 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
2092
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02002093 /* Check if we allow SCTP_NXTINFO. */
2094 if (sp->recvnxtinfo)
2095 sctp_ulpevent_read_nxtinfo(event, msg, sk);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002096 /* Check if we allow SCTP_RCVINFO. */
2097 if (sp->recvrcvinfo)
2098 sctp_ulpevent_read_rcvinfo(event, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 /* Check if we allow SCTP_SNDRCVINFO. */
2100 if (sp->subscribe.sctp_data_io_event)
2101 sctp_ulpevent_read_sndrcvinfo(event, msg);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 err = copied;
2104
2105 /* If skb's length exceeds the user's buffer, update the skb and
2106 * push it back to the receive_queue so that the next call to
2107 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2108 */
2109 if (skb_len > copied) {
2110 msg->msg_flags &= ~MSG_EOR;
2111 if (flags & MSG_PEEK)
2112 goto out_free;
2113 sctp_skb_pull(skb, copied);
2114 skb_queue_head(&sk->sk_receive_queue, skb);
2115
Daniel Borkmann362d5202014-04-14 21:45:17 +02002116 /* When only partial message is copied to the user, increase
2117 * rwnd by that amount. If all the data in the skb is read,
2118 * rwnd is updated when the event is freed.
2119 */
2120 if (!sctp_ulpevent_is_notification(event))
2121 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 goto out;
2123 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2124 (event->msg_flags & MSG_EOR))
2125 msg->msg_flags |= MSG_EOR;
2126 else
2127 msg->msg_flags &= ~MSG_EOR;
2128
2129out_free:
2130 if (flags & MSG_PEEK) {
2131 /* Release the skb reference acquired after peeking the skb in
2132 * sctp_skb_recv_datagram().
2133 */
2134 kfree_skb(skb);
2135 } else {
2136 /* Free the event which includes releasing the reference to
2137 * the owner of the skb, freeing the skb and updating the
2138 * rwnd.
2139 */
2140 sctp_ulpevent_free(event);
2141 }
2142out:
wangweidong048ed4b2014-01-21 15:44:11 +08002143 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return err;
2145}
2146
2147/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2148 *
2149 * This option is a on/off flag. If enabled no SCTP message
2150 * fragmentation will be performed. Instead if a message being sent
2151 * exceeds the current PMTU size, the message will NOT be sent and
2152 * instead a error will be indicated to the user.
2153 */
2154static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002155 char __user *optval,
2156 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
2158 int val;
2159
2160 if (optlen < sizeof(int))
2161 return -EINVAL;
2162
2163 if (get_user(val, (int __user *)optval))
2164 return -EFAULT;
2165
2166 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2167
2168 return 0;
2169}
2170
2171static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002172 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
Wei Yongjun94912302011-07-02 09:28:04 +00002174 struct sctp_association *asoc;
2175 struct sctp_ulpevent *event;
2176
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002177 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return -EINVAL;
2179 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2180 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002181
Daniel Borkmannbbbea412014-07-12 20:30:40 +02002182 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
Wei Yongjun94912302011-07-02 09:28:04 +00002183 * if there is no data to be sent or retransmit, the stack will
2184 * immediately send up this notification.
2185 */
2186 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2187 &sctp_sk(sk)->subscribe)) {
2188 asoc = sctp_id2assoc(sk, 0);
2189
2190 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2191 event = sctp_ulpevent_make_sender_dry_event(asoc,
Marcelo Ricardo Leitner2e83acb2018-01-08 19:02:27 -02002192 GFP_USER | __GFP_NOWARN);
Wei Yongjun94912302011-07-02 09:28:04 +00002193 if (!event)
2194 return -ENOMEM;
2195
Xin Long9162e0e2017-12-08 21:04:05 +08002196 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
Wei Yongjun94912302011-07-02 09:28:04 +00002197 }
2198 }
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 return 0;
2201}
2202
2203/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2204 *
2205 * This socket option is applicable to the UDP-style socket only. When
2206 * set it will cause associations that are idle for more than the
2207 * specified number of seconds to automatically close. An association
2208 * being idle is defined an association that has NOT sent or received
2209 * user data. The special value of '0' indicates that no automatic
2210 * close of any associations should be performed. The option expects an
2211 * integer defining the number of seconds of idle time before an
2212 * association is closed.
2213 */
2214static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002215 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
2217 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman9f70f462013-12-10 06:48:15 -05002218 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220 /* Applicable to UDP-style socket only */
2221 if (sctp_style(sk, TCP))
2222 return -EOPNOTSUPP;
2223 if (optlen != sizeof(int))
2224 return -EINVAL;
2225 if (copy_from_user(&sp->autoclose, optval, optlen))
2226 return -EFAULT;
2227
Neil Horman9f70f462013-12-10 06:48:15 -05002228 if (sp->autoclose > net->sctp.max_autoclose)
2229 sp->autoclose = net->sctp.max_autoclose;
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return 0;
2232}
2233
2234/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2235 *
2236 * Applications can enable or disable heartbeats for any peer address of
2237 * an association, modify an address's heartbeat interval, force a
2238 * heartbeat to be sent immediately, and adjust the address's maximum
2239 * number of retransmissions sent before an address is considered
2240 * unreachable. The following structure is used to access and modify an
2241 * address's parameters:
2242 *
2243 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002244 * sctp_assoc_t spp_assoc_id;
2245 * struct sockaddr_storage spp_address;
2246 * uint32_t spp_hbinterval;
2247 * uint16_t spp_pathmaxrxt;
2248 * uint32_t spp_pathmtu;
2249 * uint32_t spp_sackdelay;
2250 * uint32_t spp_flags;
2251 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002253 * spp_assoc_id - (one-to-many style socket) This is filled in the
2254 * application, and identifies the association for
2255 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 * spp_address - This specifies which address is of interest.
2257 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002258 * in milliseconds. If a value of zero
2259 * is present in this field then no changes are to
2260 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 * spp_pathmaxrxt - This contains the maximum number of
2262 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002263 * considered unreachable. If a value of zero
2264 * is present in this field then no changes are to
2265 * be made to this parameter.
2266 * spp_pathmtu - When Path MTU discovery is disabled the value
2267 * specified here will be the "fixed" path mtu.
2268 * Note that if the spp_address field is empty
2269 * then all associations on this address will
2270 * have this fixed path mtu set upon them.
2271 *
2272 * spp_sackdelay - When delayed sack is enabled, this value specifies
2273 * the number of milliseconds that sacks will be delayed
2274 * for. This value will apply to all addresses of an
2275 * association if the spp_address field is empty. Note
2276 * also, that if delayed sack is enabled and this
2277 * value is set to 0, no change is made to the last
2278 * recorded delayed sack timer value.
2279 *
2280 * spp_flags - These flags are used to control various features
2281 * on an association. The flag field may contain
2282 * zero or more of the following options.
2283 *
2284 * SPP_HB_ENABLE - Enable heartbeats on the
2285 * specified address. Note that if the address
2286 * field is empty all addresses for the association
2287 * have heartbeats enabled upon them.
2288 *
2289 * SPP_HB_DISABLE - Disable heartbeats on the
2290 * speicifed address. Note that if the address
2291 * field is empty all addresses for the association
2292 * will have their heartbeats disabled. Note also
2293 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2294 * mutually exclusive, only one of these two should
2295 * be specified. Enabling both fields will have
2296 * undetermined results.
2297 *
2298 * SPP_HB_DEMAND - Request a user initiated heartbeat
2299 * to be made immediately.
2300 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002301 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2302 * heartbeat delayis to be set to the value of 0
2303 * milliseconds.
2304 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002305 * SPP_PMTUD_ENABLE - This field will enable PMTU
2306 * discovery upon the specified address. Note that
2307 * if the address feild is empty then all addresses
2308 * on the association are effected.
2309 *
2310 * SPP_PMTUD_DISABLE - This field will disable PMTU
2311 * discovery upon the specified address. Note that
2312 * if the address feild is empty then all addresses
2313 * on the association are effected. Not also that
2314 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2315 * exclusive. Enabling both will have undetermined
2316 * results.
2317 *
2318 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2319 * on delayed sack. The time specified in spp_sackdelay
2320 * is used to specify the sack delay for this address. Note
2321 * that if spp_address is empty then all addresses will
2322 * enable delayed sack and take on the sack delay
2323 * value specified in spp_sackdelay.
2324 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2325 * off delayed sack. If the spp_address field is blank then
2326 * delayed sack is disabled for the entire association. Note
2327 * also that this field is mutually exclusive to
2328 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2329 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 */
Adrian Bunk16164362006-09-18 00:40:38 -07002331static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2332 struct sctp_transport *trans,
2333 struct sctp_association *asoc,
2334 struct sctp_sock *sp,
2335 int hb_change,
2336 int pmtud_change,
2337 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 int error;
2340
Frank Filz52ccb8e2005-12-22 11:36:46 -08002341 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002342 struct net *net = sock_net(trans->asoc->base.sk);
2343
2344 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (error)
2346 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
2348
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002349 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2350 * this field is ignored. Note also that a value of zero indicates
2351 * the current setting should be left unchanged.
2352 */
2353 if (params->spp_flags & SPP_HB_ENABLE) {
2354
2355 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2356 * set. This lets us use 0 value when this flag
2357 * is set.
2358 */
2359 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2360 params->spp_hbinterval = 0;
2361
2362 if (params->spp_hbinterval ||
2363 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2364 if (trans) {
2365 trans->hbinterval =
2366 msecs_to_jiffies(params->spp_hbinterval);
2367 } else if (asoc) {
2368 asoc->hbinterval =
2369 msecs_to_jiffies(params->spp_hbinterval);
2370 } else {
2371 sp->hbinterval = params->spp_hbinterval;
2372 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002373 }
2374 }
2375
2376 if (hb_change) {
2377 if (trans) {
2378 trans->param_flags =
2379 (trans->param_flags & ~SPP_HB) | hb_change;
2380 } else if (asoc) {
2381 asoc->param_flags =
2382 (asoc->param_flags & ~SPP_HB) | hb_change;
2383 } else {
2384 sp->param_flags =
2385 (sp->param_flags & ~SPP_HB) | hb_change;
2386 }
2387 }
2388
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002389 /* When Path MTU discovery is disabled the value specified here will
2390 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2391 * include the flag SPP_PMTUD_DISABLE for this field to have any
2392 * effect).
2393 */
2394 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002395 if (trans) {
2396 trans->pathmtu = params->spp_pathmtu;
Xin Long3ebfdf02017-04-04 13:39:55 +08002397 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002398 } else if (asoc) {
2399 asoc->pathmtu = params->spp_pathmtu;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002400 } else {
2401 sp->pathmtu = params->spp_pathmtu;
2402 }
2403 }
2404
2405 if (pmtud_change) {
2406 if (trans) {
2407 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2408 (params->spp_flags & SPP_PMTUD_ENABLE);
2409 trans->param_flags =
2410 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2411 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002412 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
Xin Long3ebfdf02017-04-04 13:39:55 +08002413 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002414 }
2415 } else if (asoc) {
2416 asoc->param_flags =
2417 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2418 } else {
2419 sp->param_flags =
2420 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2421 }
2422 }
2423
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002424 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2425 * value of this field is ignored. Note also that a value of zero
2426 * indicates the current setting should be left unchanged.
2427 */
2428 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002429 if (trans) {
2430 trans->sackdelay =
2431 msecs_to_jiffies(params->spp_sackdelay);
2432 } else if (asoc) {
2433 asoc->sackdelay =
2434 msecs_to_jiffies(params->spp_sackdelay);
2435 } else {
2436 sp->sackdelay = params->spp_sackdelay;
2437 }
2438 }
2439
2440 if (sackdelay_change) {
2441 if (trans) {
2442 trans->param_flags =
2443 (trans->param_flags & ~SPP_SACKDELAY) |
2444 sackdelay_change;
2445 } else if (asoc) {
2446 asoc->param_flags =
2447 (asoc->param_flags & ~SPP_SACKDELAY) |
2448 sackdelay_change;
2449 } else {
2450 sp->param_flags =
2451 (sp->param_flags & ~SPP_SACKDELAY) |
2452 sackdelay_change;
2453 }
2454 }
2455
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002456 /* Note that a value of zero indicates the current setting should be
2457 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002458 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002459 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002460 if (trans) {
2461 trans->pathmaxrxt = params->spp_pathmaxrxt;
2462 } else if (asoc) {
2463 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2464 } else {
2465 sp->pathmaxrxt = params->spp_pathmaxrxt;
2466 }
2467 }
2468
2469 return 0;
2470}
2471
2472static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002473 char __user *optval,
2474 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002475{
2476 struct sctp_paddrparams params;
2477 struct sctp_transport *trans = NULL;
2478 struct sctp_association *asoc = NULL;
2479 struct sctp_sock *sp = sctp_sk(sk);
2480 int error;
2481 int hb_change, pmtud_change, sackdelay_change;
2482
2483 if (optlen != sizeof(struct sctp_paddrparams))
wangweidongcb3f8372013-12-23 12:16:50 +08002484 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002485
2486 if (copy_from_user(&params, optval, optlen))
2487 return -EFAULT;
2488
2489 /* Validate flags and value parameters. */
2490 hb_change = params.spp_flags & SPP_HB;
2491 pmtud_change = params.spp_flags & SPP_PMTUD;
2492 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2493
2494 if (hb_change == SPP_HB ||
2495 pmtud_change == SPP_PMTUD ||
2496 sackdelay_change == SPP_SACKDELAY ||
2497 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002498 (params.spp_pathmtu &&
2499 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002500 return -EINVAL;
2501
2502 /* If an address other than INADDR_ANY is specified, and
2503 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 */
wangweidongcb3f8372013-12-23 12:16:50 +08002505 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002506 trans = sctp_addr_id2transport(sk, &params.spp_address,
2507 params.spp_assoc_id);
2508 if (!trans)
2509 return -EINVAL;
2510 }
2511
2512 /* Get association, if assoc_id != 0 and the socket is a one
2513 * to many style socket, and an association was not found, then
2514 * the id was invalid.
2515 */
2516 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2517 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2518 return -EINVAL;
2519
2520 /* Heartbeat demand can only be sent on a transport or
2521 * association, but not a socket.
2522 */
2523 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2524 return -EINVAL;
2525
2526 /* Process parameters. */
2527 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2528 hb_change, pmtud_change,
2529 sackdelay_change);
2530
2531 if (error)
2532 return error;
2533
2534 /* If changes are for association, also apply parameters to each
2535 * transport.
2536 */
2537 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002538 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2539 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002540 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2541 hb_change, pmtud_change,
2542 sackdelay_change);
2543 }
2544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
2546 return 0;
2547}
2548
wangweidong0ea5e4d2014-01-15 17:24:01 +08002549static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2550{
2551 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2552}
2553
2554static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2555{
2556 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2557}
2558
Wei Yongjund364d922008-05-09 15:13:26 -07002559/*
2560 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002561 *
Wei Yongjund364d922008-05-09 15:13:26 -07002562 * This option will effect the way delayed acks are performed. This
2563 * option allows you to get or set the delayed ack time, in
2564 * milliseconds. It also allows changing the delayed ack frequency.
2565 * Changing the frequency to 1 disables the delayed sack algorithm. If
2566 * the assoc_id is 0, then this sets or gets the endpoints default
2567 * values. If the assoc_id field is non-zero, then the set or get
2568 * effects the specified association for the one to many model (the
2569 * assoc_id field is ignored by the one to one model). Note that if
2570 * sack_delay or sack_freq are 0 when setting this option, then the
2571 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002572 *
Wei Yongjund364d922008-05-09 15:13:26 -07002573 * struct sctp_sack_info {
2574 * sctp_assoc_t sack_assoc_id;
2575 * uint32_t sack_delay;
2576 * uint32_t sack_freq;
2577 * };
Frank Filz77086102005-12-22 11:37:30 -08002578 *
Wei Yongjund364d922008-05-09 15:13:26 -07002579 * sack_assoc_id - This parameter, indicates which association the user
2580 * is performing an action upon. Note that if this field's value is
2581 * zero then the endpoints default value is changed (effecting future
2582 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002583 *
Wei Yongjund364d922008-05-09 15:13:26 -07002584 * sack_delay - This parameter contains the number of milliseconds that
2585 * the user is requesting the delayed ACK timer be set to. Note that
2586 * this value is defined in the standard to be between 200 and 500
2587 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002588 *
Wei Yongjund364d922008-05-09 15:13:26 -07002589 * sack_freq - This parameter contains the number of packets that must
2590 * be received before a sack is sent without waiting for the delay
2591 * timer to expire. The default value for this is 2, setting this
2592 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002593 */
2594
Wei Yongjund364d922008-05-09 15:13:26 -07002595static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002596 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002597{
Wei Yongjund364d922008-05-09 15:13:26 -07002598 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002599 struct sctp_transport *trans = NULL;
2600 struct sctp_association *asoc = NULL;
2601 struct sctp_sock *sp = sctp_sk(sk);
2602
Wei Yongjund364d922008-05-09 15:13:26 -07002603 if (optlen == sizeof(struct sctp_sack_info)) {
2604 if (copy_from_user(&params, optval, optlen))
2605 return -EFAULT;
2606
2607 if (params.sack_delay == 0 && params.sack_freq == 0)
2608 return 0;
2609 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05002610 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05002611 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05002612 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05002613 "Use struct sctp_sack_info instead\n",
2614 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07002615 if (copy_from_user(&params, optval, optlen))
2616 return -EFAULT;
2617
2618 if (params.sack_delay == 0)
2619 params.sack_freq = 1;
2620 else
2621 params.sack_freq = 0;
2622 } else
wangweidongcb3f8372013-12-23 12:16:50 +08002623 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08002624
Frank Filz77086102005-12-22 11:37:30 -08002625 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002626 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002627 return -EINVAL;
2628
Wei Yongjund364d922008-05-09 15:13:26 -07002629 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002630 * to many style socket, and an association was not found, then
2631 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002632 */
Wei Yongjund364d922008-05-09 15:13:26 -07002633 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2634 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002635 return -EINVAL;
2636
Wei Yongjund364d922008-05-09 15:13:26 -07002637 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002638 if (asoc) {
2639 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002640 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002641 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002642 sctp_spp_sackdelay_enable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002643 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002644 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002645 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002646 sctp_spp_sackdelay_enable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002647 }
Wei Yongjund364d922008-05-09 15:13:26 -07002648 }
2649
2650 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002651 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002652 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002653 sctp_spp_sackdelay_disable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002654 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002655 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002656 sctp_spp_sackdelay_disable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002657 }
Wei Yongjund364d922008-05-09 15:13:26 -07002658 } else if (params.sack_freq > 1) {
2659 if (asoc) {
2660 asoc->sackfreq = params.sack_freq;
2661 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002662 sctp_spp_sackdelay_enable(asoc->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002663 } else {
2664 sp->sackfreq = params.sack_freq;
2665 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002666 sctp_spp_sackdelay_enable(sp->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002667 }
Frank Filz77086102005-12-22 11:37:30 -08002668 }
2669
2670 /* If change is for association, also apply to each transport. */
2671 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002672 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2673 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002674 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002675 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002676 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002677 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002678 sctp_spp_sackdelay_enable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002679 }
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07002680 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002681 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002682 sctp_spp_sackdelay_disable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002683 } else if (params.sack_freq > 1) {
2684 trans->sackfreq = params.sack_freq;
2685 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002686 sctp_spp_sackdelay_enable(trans->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002687 }
2688 }
2689 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002690
Frank Filz77086102005-12-22 11:37:30 -08002691 return 0;
2692}
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2695 *
2696 * Applications can specify protocol parameters for the default association
2697 * initialization. The option name argument to setsockopt() and getsockopt()
2698 * is SCTP_INITMSG.
2699 *
2700 * Setting initialization parameters is effective only on an unconnected
2701 * socket (for UDP-style sockets only future associations are effected
2702 * by the change). With TCP-style sockets, this option is inherited by
2703 * sockets derived from a listener socket.
2704 */
David S. Millerb7058842009-09-30 16:12:20 -07002705static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
2707 struct sctp_initmsg sinit;
2708 struct sctp_sock *sp = sctp_sk(sk);
2709
2710 if (optlen != sizeof(struct sctp_initmsg))
2711 return -EINVAL;
2712 if (copy_from_user(&sinit, optval, optlen))
2713 return -EFAULT;
2714
2715 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002716 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002718 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002720 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002722 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
2724 return 0;
2725}
2726
2727/*
2728 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2729 *
2730 * Applications that wish to use the sendto() system call may wish to
2731 * specify a default set of parameters that would normally be supplied
2732 * through the inclusion of ancillary data. This socket option allows
2733 * such an application to set the default sctp_sndrcvinfo structure.
2734 * The application that wishes to use this socket option simply passes
2735 * in to this call the sctp_sndrcvinfo structure defined in Section
2736 * 5.2.2) The input parameters accepted by this call include
2737 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2738 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2739 * to this call if the caller is using the UDP model.
2740 */
2741static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002742 char __user *optval,
2743 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002746 struct sctp_association *asoc;
2747 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002749 if (optlen != sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 return -EINVAL;
2751 if (copy_from_user(&info, optval, optlen))
2752 return -EFAULT;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002753 if (info.sinfo_flags &
2754 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2755 SCTP_ABORT | SCTP_EOF))
2756 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2759 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2760 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 if (asoc) {
2762 asoc->default_stream = info.sinfo_stream;
2763 asoc->default_flags = info.sinfo_flags;
2764 asoc->default_ppid = info.sinfo_ppid;
2765 asoc->default_context = info.sinfo_context;
2766 asoc->default_timetolive = info.sinfo_timetolive;
2767 } else {
2768 sp->default_stream = info.sinfo_stream;
2769 sp->default_flags = info.sinfo_flags;
2770 sp->default_ppid = info.sinfo_ppid;
2771 sp->default_context = info.sinfo_context;
2772 sp->default_timetolive = info.sinfo_timetolive;
2773 }
2774
2775 return 0;
2776}
2777
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002778/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2779 * (SCTP_DEFAULT_SNDINFO)
2780 */
2781static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2782 char __user *optval,
2783 unsigned int optlen)
2784{
2785 struct sctp_sock *sp = sctp_sk(sk);
2786 struct sctp_association *asoc;
2787 struct sctp_sndinfo info;
2788
2789 if (optlen != sizeof(info))
2790 return -EINVAL;
2791 if (copy_from_user(&info, optval, optlen))
2792 return -EFAULT;
2793 if (info.snd_flags &
2794 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2795 SCTP_ABORT | SCTP_EOF))
2796 return -EINVAL;
2797
2798 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
2799 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
2800 return -EINVAL;
2801 if (asoc) {
2802 asoc->default_stream = info.snd_sid;
2803 asoc->default_flags = info.snd_flags;
2804 asoc->default_ppid = info.snd_ppid;
2805 asoc->default_context = info.snd_context;
2806 } else {
2807 sp->default_stream = info.snd_sid;
2808 sp->default_flags = info.snd_flags;
2809 sp->default_ppid = info.snd_ppid;
2810 sp->default_context = info.snd_context;
2811 }
2812
2813 return 0;
2814}
2815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2817 *
2818 * Requests that the local SCTP stack use the enclosed peer address as
2819 * the association primary. The enclosed address must be one of the
2820 * association peer's addresses.
2821 */
2822static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002823 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
2825 struct sctp_prim prim;
2826 struct sctp_transport *trans;
2827
2828 if (optlen != sizeof(struct sctp_prim))
2829 return -EINVAL;
2830
2831 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2832 return -EFAULT;
2833
2834 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2835 if (!trans)
2836 return -EINVAL;
2837
2838 sctp_assoc_set_primary(trans->asoc, trans);
2839
2840 return 0;
2841}
2842
2843/*
2844 * 7.1.5 SCTP_NODELAY
2845 *
2846 * Turn on/off any Nagle-like algorithm. This means that packets are
2847 * generally sent as soon as possible and no unnecessary delays are
2848 * introduced, at the cost of more packets in the network. Expects an
2849 * integer boolean flag.
2850 */
2851static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002852 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
2854 int val;
2855
2856 if (optlen < sizeof(int))
2857 return -EINVAL;
2858 if (get_user(val, (int __user *)optval))
2859 return -EFAULT;
2860
2861 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2862 return 0;
2863}
2864
2865/*
2866 *
2867 * 7.1.1 SCTP_RTOINFO
2868 *
2869 * The protocol parameters used to initialize and bound retransmission
2870 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2871 * and modify these parameters.
2872 * All parameters are time values, in milliseconds. A value of 0, when
2873 * modifying the parameters, indicates that the current value should not
2874 * be changed.
2875 *
2876 */
David S. Millerb7058842009-09-30 16:12:20 -07002877static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2878{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 struct sctp_rtoinfo rtoinfo;
2880 struct sctp_association *asoc;
wangweidong85f935d2013-12-11 09:50:38 +08002881 unsigned long rto_min, rto_max;
2882 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
2884 if (optlen != sizeof (struct sctp_rtoinfo))
2885 return -EINVAL;
2886
2887 if (copy_from_user(&rtoinfo, optval, optlen))
2888 return -EFAULT;
2889
2890 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2891
2892 /* Set the values to the specific association */
2893 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2894 return -EINVAL;
2895
wangweidong85f935d2013-12-11 09:50:38 +08002896 rto_max = rtoinfo.srto_max;
2897 rto_min = rtoinfo.srto_min;
2898
2899 if (rto_max)
2900 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
2901 else
2902 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
2903
2904 if (rto_min)
2905 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
2906 else
2907 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
2908
2909 if (rto_min > rto_max)
2910 return -EINVAL;
2911
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 if (asoc) {
2913 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002914 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 msecs_to_jiffies(rtoinfo.srto_initial);
wangweidong85f935d2013-12-11 09:50:38 +08002916 asoc->rto_max = rto_max;
2917 asoc->rto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 } else {
2919 /* If there is no association or the association-id = 0
2920 * set the values to the endpoint.
2921 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 if (rtoinfo.srto_initial != 0)
2923 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
wangweidong85f935d2013-12-11 09:50:38 +08002924 sp->rtoinfo.srto_max = rto_max;
2925 sp->rtoinfo.srto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
2927
2928 return 0;
2929}
2930
2931/*
2932 *
2933 * 7.1.2 SCTP_ASSOCINFO
2934 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02002935 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 * of the association.
2937 * Returns an error if the new association retransmission value is
2938 * greater than the sum of the retransmission value of the peer.
2939 * See [SCTP] for more information.
2940 *
2941 */
David S. Millerb7058842009-09-30 16:12:20 -07002942static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
2944
2945 struct sctp_assocparams assocparams;
2946 struct sctp_association *asoc;
2947
2948 if (optlen != sizeof(struct sctp_assocparams))
2949 return -EINVAL;
2950 if (copy_from_user(&assocparams, optval, optlen))
2951 return -EFAULT;
2952
2953 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
2954
2955 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
2956 return -EINVAL;
2957
2958 /* Set the values to the specific association */
2959 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002960 if (assocparams.sasoc_asocmaxrxt != 0) {
2961 __u32 path_sum = 0;
2962 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002963 struct sctp_transport *peer_addr;
2964
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002965 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
2966 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002967 path_sum += peer_addr->pathmaxrxt;
2968 paths++;
2969 }
2970
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002971 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002972 * one path/transport. We do this because path
2973 * retransmissions are only counted when we have more
2974 * then one path.
2975 */
2976 if (paths > 1 &&
2977 assocparams.sasoc_asocmaxrxt > path_sum)
2978 return -EINVAL;
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07002981 }
2982
Daniel Borkmann52db8822013-06-25 18:17:27 +02002983 if (assocparams.sasoc_cookie_life != 0)
2984 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 } else {
2986 /* Set the values to the endpoint */
2987 struct sctp_sock *sp = sctp_sk(sk);
2988
2989 if (assocparams.sasoc_asocmaxrxt != 0)
2990 sp->assocparams.sasoc_asocmaxrxt =
2991 assocparams.sasoc_asocmaxrxt;
2992 if (assocparams.sasoc_cookie_life != 0)
2993 sp->assocparams.sasoc_cookie_life =
2994 assocparams.sasoc_cookie_life;
2995 }
2996 return 0;
2997}
2998
2999/*
3000 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3001 *
3002 * This socket option is a boolean flag which turns on or off mapped V4
3003 * addresses. If this option is turned on and the socket is type
3004 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3005 * If this option is turned off, then no mapping will be done of V4
3006 * addresses and a user will receive both PF_INET6 and PF_INET type
3007 * addresses on the socket.
3008 */
David S. Millerb7058842009-09-30 16:12:20 -07003009static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
3011 int val;
3012 struct sctp_sock *sp = sctp_sk(sk);
3013
3014 if (optlen < sizeof(int))
3015 return -EINVAL;
3016 if (get_user(val, (int __user *)optval))
3017 return -EFAULT;
3018 if (val)
3019 sp->v4mapped = 1;
3020 else
3021 sp->v4mapped = 0;
3022
3023 return 0;
3024}
3025
3026/*
Wei Yongjune89c2092008-12-25 16:54:58 -08003027 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3028 * This option will get or set the maximum size to put in any outgoing
3029 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 * fragmented by SCTP into the specified size. Note that the underlying
3031 * SCTP implementation may fragment into smaller sized chunks when the
3032 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08003033 * the user. The default value for this option is '0' which indicates
3034 * the user is NOT limiting fragmentation and only the PMTU will effect
3035 * SCTP's choice of DATA chunk size. Note also that values set larger
3036 * than the maximum size of an IP datagram will effectively let SCTP
3037 * control fragmentation (i.e. the same as setting this option to 0).
3038 *
3039 * The following structure is used to access and modify this parameter:
3040 *
3041 * struct sctp_assoc_value {
3042 * sctp_assoc_t assoc_id;
3043 * uint32_t assoc_value;
3044 * };
3045 *
3046 * assoc_id: This parameter is ignored for one-to-one style sockets.
3047 * For one-to-many style sockets this parameter indicates which
3048 * association the user is performing an action upon. Note that if
3049 * this field's value is zero then the endpoints default value is
3050 * changed (effecting future associations only).
3051 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 */
David S. Millerb7058842009-09-30 16:12:20 -07003053static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
Xin Longecca8f82017-11-17 14:11:11 +08003055 struct sctp_sock *sp = sctp_sk(sk);
Wei Yongjune89c2092008-12-25 16:54:58 -08003056 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 int val;
3059
Wei Yongjune89c2092008-12-25 16:54:58 -08003060 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003061 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003062 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003063 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003064 "Use struct sctp_assoc_value instead\n",
3065 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08003066 if (copy_from_user(&val, optval, optlen))
3067 return -EFAULT;
3068 params.assoc_id = 0;
3069 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3070 if (copy_from_user(&params, optval, optlen))
3071 return -EFAULT;
3072 val = params.assoc_value;
Xin Longecca8f82017-11-17 14:11:11 +08003073 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 return -EINVAL;
Xin Longecca8f82017-11-17 14:11:11 +08003075 }
Wei Yongjune89c2092008-12-25 16:54:58 -08003076
Xin Longecca8f82017-11-17 14:11:11 +08003077 if (val) {
3078 int min_len, max_len;
3079
3080 min_len = SCTP_DEFAULT_MINSEGMENT - sp->pf->af->net_header_len;
3081 min_len -= sizeof(struct sctphdr) +
3082 sizeof(struct sctp_data_chunk);
3083
3084 max_len = SCTP_MAX_CHUNK_LEN - sizeof(struct sctp_data_chunk);
3085
3086 if (val < min_len || val > max_len)
3087 return -EINVAL;
3088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Wei Yongjune89c2092008-12-25 16:54:58 -08003090 asoc = sctp_id2assoc(sk, params.assoc_id);
Wei Yongjune89c2092008-12-25 16:54:58 -08003091 if (asoc) {
3092 if (val == 0) {
Xin Longecca8f82017-11-17 14:11:11 +08003093 val = asoc->pathmtu - sp->pf->af->net_header_len;
Wei Yongjune89c2092008-12-25 16:54:58 -08003094 val -= sizeof(struct sctphdr) +
Xin Long668c9be2017-12-08 21:04:02 +08003095 sctp_datachk_len(&asoc->stream);
Wei Yongjune89c2092008-12-25 16:54:58 -08003096 }
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003097 asoc->user_frag = val;
3098 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
Wei Yongjune89c2092008-12-25 16:54:58 -08003099 } else {
Xin Longecca8f82017-11-17 14:11:11 +08003100 if (params.assoc_id && sctp_style(sk, UDP))
3101 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003102 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 }
3104
3105 return 0;
3106}
3107
3108
3109/*
3110 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3111 *
3112 * Requests that the peer mark the enclosed address as the association
3113 * primary. The enclosed address must be one of the association's
3114 * locally bound addresses. The following structure is used to make a
3115 * set primary request:
3116 */
3117static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003118 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003120 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 struct sctp_association *asoc = NULL;
3123 struct sctp_setpeerprim prim;
3124 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003125 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 int err;
3127
3128 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003130 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 return -EPERM;
3132
3133 if (optlen != sizeof(struct sctp_setpeerprim))
3134 return -EINVAL;
3135
3136 if (copy_from_user(&prim, optval, optlen))
3137 return -EFAULT;
3138
3139 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003140 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 return -EINVAL;
3142
3143 if (!asoc->peer.asconf_capable)
3144 return -EPERM;
3145
3146 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3147 return -EPERM;
3148
3149 if (!sctp_state(asoc, ESTABLISHED))
3150 return -ENOTCONN;
3151
Wei Yongjun40a01032010-12-07 17:11:09 +00003152 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3153 if (!af)
3154 return -EINVAL;
3155
3156 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3157 return -EADDRNOTAVAIL;
3158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3160 return -EADDRNOTAVAIL;
3161
3162 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3163 chunk = sctp_make_asconf_set_prim(asoc,
3164 (union sctp_addr *)&prim.sspp_addr);
3165 if (!chunk)
3166 return -ENOMEM;
3167
3168 err = sctp_send_asconf(asoc, chunk);
3169
Daniel Borkmannbb333812013-06-28 19:49:40 +02003170 pr_debug("%s: we set peer primary addr primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
3172 return err;
3173}
3174
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003175static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003176 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003178 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003180 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003182 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 return -EFAULT;
3184
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003185 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
3187 return 0;
3188}
3189
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003190/*
3191 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3192 *
3193 * The context field in the sctp_sndrcvinfo structure is normally only
3194 * used when a failed message is retrieved holding the value that was
3195 * sent down on the actual send call. This option allows the setting of
3196 * a default context on an association basis that will be received on
3197 * reading messages from the peer. This is especially helpful in the
3198 * one-2-many model for an application to keep some reference to an
3199 * internal state machine that is processing messages on the
3200 * association. Note that the setting of this value only effects
3201 * received messages from the peer and does not effect the value that is
3202 * saved with outbound messages.
3203 */
3204static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003205 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003206{
3207 struct sctp_assoc_value params;
3208 struct sctp_sock *sp;
3209 struct sctp_association *asoc;
3210
3211 if (optlen != sizeof(struct sctp_assoc_value))
3212 return -EINVAL;
3213 if (copy_from_user(&params, optval, optlen))
3214 return -EFAULT;
3215
3216 sp = sctp_sk(sk);
3217
3218 if (params.assoc_id != 0) {
3219 asoc = sctp_id2assoc(sk, params.assoc_id);
3220 if (!asoc)
3221 return -EINVAL;
3222 asoc->default_rcv_context = params.assoc_value;
3223 } else {
3224 sp->default_rcv_context = params.assoc_value;
3225 }
3226
3227 return 0;
3228}
3229
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003230/*
3231 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3232 *
3233 * This options will at a minimum specify if the implementation is doing
3234 * fragmented interleave. Fragmented interleave, for a one to many
3235 * socket, is when subsequent calls to receive a message may return
3236 * parts of messages from different associations. Some implementations
3237 * may allow you to turn this value on or off. If so, when turned off,
3238 * no fragment interleave will occur (which will cause a head of line
3239 * blocking amongst multiple associations sharing the same one to many
3240 * socket). When this option is turned on, then each receive call may
3241 * come from a different association (thus the user must receive data
3242 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3243 * association each receive belongs to.
3244 *
3245 * This option takes a boolean value. A non-zero value indicates that
3246 * fragmented interleave is on. A value of zero indicates that
3247 * fragmented interleave is off.
3248 *
3249 * Note that it is important that an implementation that allows this
3250 * option to be turned on, have it off by default. Otherwise an unaware
3251 * application using the one to many model may become confused and act
3252 * incorrectly.
3253 */
3254static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3255 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003256 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003257{
3258 int val;
3259
3260 if (optlen != sizeof(int))
3261 return -EINVAL;
3262 if (get_user(val, (int __user *)optval))
3263 return -EFAULT;
3264
Xin Long772a5862017-12-08 21:03:58 +08003265 sctp_sk(sk)->frag_interleave = !!val;
3266
3267 if (!sctp_sk(sk)->frag_interleave)
3268 sctp_sk(sk)->strm_interleave = 0;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003269
3270 return 0;
3271}
3272
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003273/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003274 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003275 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003276 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003277 * This option will set or get the SCTP partial delivery point. This
3278 * point is the size of a message where the partial delivery API will be
3279 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003280 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003281 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003282 * point. Note also that the call will fail if the user attempts to set
3283 * this value larger than the socket receive buffer size.
3284 *
3285 * Note that any single message having a length smaller than or equal to
3286 * the SCTP partial delivery point will be delivered in one single read
3287 * call as long as the user provided buffer is large enough to hold the
3288 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003289 */
3290static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3291 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003292 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003293{
3294 u32 val;
3295
3296 if (optlen != sizeof(u32))
3297 return -EINVAL;
3298 if (get_user(val, (int __user *)optval))
3299 return -EFAULT;
3300
Wei Yongjun8510b932008-12-25 16:59:03 -08003301 /* Note: We double the receive buffer from what the user sets
3302 * it to be, also initial rwnd is based on rcvbuf/2.
3303 */
3304 if (val > (sk->sk_rcvbuf >> 1))
3305 return -EINVAL;
3306
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003307 sctp_sk(sk)->pd_point = val;
3308
3309 return 0; /* is this the right error code? */
3310}
3311
Vlad Yasevich70331572007-03-23 11:34:36 -07003312/*
3313 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3314 *
3315 * This option will allow a user to change the maximum burst of packets
3316 * that can be emitted by this association. Note that the default value
3317 * is 4, and some implementations may restrict this setting so that it
3318 * can only be lowered.
3319 *
3320 * NOTE: This text doesn't seem right. Do this on a socket basis with
3321 * future associations inheriting the socket value.
3322 */
3323static int sctp_setsockopt_maxburst(struct sock *sk,
3324 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003325 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003326{
Neil Horman219b99a2008-03-05 13:44:46 -08003327 struct sctp_assoc_value params;
3328 struct sctp_sock *sp;
3329 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003330 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003331 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003332
Neil Horman219b99a2008-03-05 13:44:46 -08003333 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003334 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003335 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003336 "Use of int in max_burst socket option deprecated.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003337 "Use struct sctp_assoc_value instead\n",
3338 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08003339 if (copy_from_user(&val, optval, optlen))
3340 return -EFAULT;
3341 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3342 if (copy_from_user(&params, optval, optlen))
3343 return -EFAULT;
3344 val = params.assoc_value;
3345 assoc_id = params.assoc_id;
3346 } else
3347 return -EINVAL;
3348
3349 sp = sctp_sk(sk);
3350
3351 if (assoc_id != 0) {
3352 asoc = sctp_id2assoc(sk, assoc_id);
3353 if (!asoc)
3354 return -EINVAL;
3355 asoc->max_burst = val;
3356 } else
3357 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003358
3359 return 0;
3360}
3361
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003362/*
3363 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3364 *
3365 * This set option adds a chunk type that the user is requesting to be
3366 * received only in an authenticated way. Changes to the list of chunks
3367 * will only effect future associations on the socket.
3368 */
3369static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003370 char __user *optval,
3371 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003372{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003373 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003374 struct sctp_authchunk val;
3375
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003376 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003377 return -EACCES;
3378
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003379 if (optlen != sizeof(struct sctp_authchunk))
3380 return -EINVAL;
3381 if (copy_from_user(&val, optval, optlen))
3382 return -EFAULT;
3383
3384 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003385 case SCTP_CID_INIT:
3386 case SCTP_CID_INIT_ACK:
3387 case SCTP_CID_SHUTDOWN_COMPLETE:
3388 case SCTP_CID_AUTH:
3389 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003390 }
3391
3392 /* add this chunk id to the endpoint */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003393 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003394}
3395
3396/*
3397 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3398 *
3399 * This option gets or sets the list of HMAC algorithms that the local
3400 * endpoint requires the peer to use.
3401 */
3402static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003403 char __user *optval,
3404 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003405{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003406 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003407 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003408 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003409 int err;
3410
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003411 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003412 return -EACCES;
3413
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003414 if (optlen < sizeof(struct sctp_hmacalgo))
3415 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003416 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3417 SCTP_AUTH_NUM_HMACS * sizeof(u16));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003418
wangweidongcb3f8372013-12-23 12:16:50 +08003419 hmacs = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003420 if (IS_ERR(hmacs))
3421 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003422
Vlad Yasevichd9724052008-08-27 16:09:49 -07003423 idents = hmacs->shmac_num_idents;
3424 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3425 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003426 err = -EINVAL;
3427 goto out;
3428 }
3429
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003430 err = sctp_auth_ep_set_hmacs(ep, hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003431out:
3432 kfree(hmacs);
3433 return err;
3434}
3435
3436/*
3437 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3438 *
3439 * This option will set a shared secret key which is used to build an
3440 * association shared key.
3441 */
3442static int sctp_setsockopt_auth_key(struct sock *sk,
3443 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003444 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003445{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003446 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003447 struct sctp_authkey *authkey;
3448 struct sctp_association *asoc;
3449 int ret;
3450
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003451 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003452 return -EACCES;
3453
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003454 if (optlen <= sizeof(struct sctp_authkey))
3455 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003456 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3457 * this.
3458 */
3459 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3460 sizeof(struct sctp_authkey));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003461
wangweidongcb3f8372013-12-23 12:16:50 +08003462 authkey = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003463 if (IS_ERR(authkey))
3464 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003465
Vlad Yasevich328fc472008-08-27 16:08:54 -07003466 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003467 ret = -EINVAL;
3468 goto out;
3469 }
3470
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003471 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3472 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3473 ret = -EINVAL;
3474 goto out;
3475 }
3476
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003477 ret = sctp_auth_set_key(ep, asoc, authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003478out:
Daniel Borkmann6ba542a2013-02-08 03:04:34 +00003479 kzfree(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003480 return ret;
3481}
3482
3483/*
3484 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3485 *
3486 * This option will get or set the active shared key to be used to build
3487 * the association shared key.
3488 */
3489static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003490 char __user *optval,
3491 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003492{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003493 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003494 struct sctp_authkeyid val;
3495 struct sctp_association *asoc;
3496
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003497 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003498 return -EACCES;
3499
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003500 if (optlen != sizeof(struct sctp_authkeyid))
3501 return -EINVAL;
3502 if (copy_from_user(&val, optval, optlen))
3503 return -EFAULT;
3504
3505 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3506 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3507 return -EINVAL;
3508
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003509 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003510}
3511
3512/*
3513 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3514 *
3515 * This set option will delete a shared secret key from use.
3516 */
3517static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003518 char __user *optval,
3519 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003520{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003521 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003522 struct sctp_authkeyid val;
3523 struct sctp_association *asoc;
3524
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003525 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003526 return -EACCES;
3527
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003528 if (optlen != sizeof(struct sctp_authkeyid))
3529 return -EINVAL;
3530 if (copy_from_user(&val, optval, optlen))
3531 return -EFAULT;
3532
3533 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3534 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3535 return -EINVAL;
3536
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003537 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003538
3539}
3540
Michio Honda7dc04d72011-04-26 20:16:31 +09003541/*
3542 * 8.1.23 SCTP_AUTO_ASCONF
3543 *
3544 * This option will enable or disable the use of the automatic generation of
3545 * ASCONF chunks to add and delete addresses to an existing association. Note
3546 * that this option has two caveats namely: a) it only affects sockets that
3547 * are bound to all addresses available to the SCTP stack, and b) the system
3548 * administrator may have an overriding control that turns the ASCONF feature
3549 * off no matter what setting the socket option may have.
3550 * This option expects an integer boolean flag, where a non-zero value turns on
3551 * the option, and a zero value turns off the option.
3552 * Note. In this implementation, socket operation overrides default parameter
3553 * being set by sysctl as well as FreeBSD implementation
3554 */
3555static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3556 unsigned int optlen)
3557{
3558 int val;
3559 struct sctp_sock *sp = sctp_sk(sk);
3560
3561 if (optlen < sizeof(int))
3562 return -EINVAL;
3563 if (get_user(val, (int __user *)optval))
3564 return -EFAULT;
3565 if (!sctp_is_ep_boundall(sk) && val)
3566 return -EINVAL;
3567 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3568 return 0;
3569
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003570 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003571 if (val == 0 && sp->do_auto_asconf) {
3572 list_del(&sp->auto_asconf_list);
3573 sp->do_auto_asconf = 0;
3574 } else if (val && !sp->do_auto_asconf) {
3575 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00003576 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09003577 sp->do_auto_asconf = 1;
3578 }
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003579 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003580 return 0;
3581}
3582
Neil Horman5aa93bc2012-07-21 07:56:07 +00003583/*
3584 * SCTP_PEER_ADDR_THLDS
3585 *
3586 * This option allows us to alter the partially failed threshold for one or all
3587 * transports in an association. See Section 6.1 of:
3588 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3589 */
3590static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3591 char __user *optval,
3592 unsigned int optlen)
3593{
3594 struct sctp_paddrthlds val;
3595 struct sctp_transport *trans;
3596 struct sctp_association *asoc;
3597
3598 if (optlen < sizeof(struct sctp_paddrthlds))
3599 return -EINVAL;
3600 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3601 sizeof(struct sctp_paddrthlds)))
3602 return -EFAULT;
3603
3604
3605 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3606 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3607 if (!asoc)
3608 return -ENOENT;
3609 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3610 transports) {
3611 if (val.spt_pathmaxrxt)
3612 trans->pathmaxrxt = val.spt_pathmaxrxt;
3613 trans->pf_retrans = val.spt_pathpfthld;
3614 }
3615
3616 if (val.spt_pathmaxrxt)
3617 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3618 asoc->pf_retrans = val.spt_pathpfthld;
3619 } else {
3620 trans = sctp_addr_id2transport(sk, &val.spt_address,
3621 val.spt_assoc_id);
3622 if (!trans)
3623 return -ENOENT;
3624
3625 if (val.spt_pathmaxrxt)
3626 trans->pathmaxrxt = val.spt_pathmaxrxt;
3627 trans->pf_retrans = val.spt_pathpfthld;
3628 }
3629
3630 return 0;
3631}
3632
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02003633static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3634 char __user *optval,
3635 unsigned int optlen)
3636{
3637 int val;
3638
3639 if (optlen < sizeof(int))
3640 return -EINVAL;
3641 if (get_user(val, (int __user *) optval))
3642 return -EFAULT;
3643
3644 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3645
3646 return 0;
3647}
3648
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02003649static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3650 char __user *optval,
3651 unsigned int optlen)
3652{
3653 int val;
3654
3655 if (optlen < sizeof(int))
3656 return -EINVAL;
3657 if (get_user(val, (int __user *) optval))
3658 return -EFAULT;
3659
3660 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3661
3662 return 0;
3663}
3664
Xin Long28aa4c22016-07-09 19:47:40 +08003665static int sctp_setsockopt_pr_supported(struct sock *sk,
3666 char __user *optval,
3667 unsigned int optlen)
3668{
3669 struct sctp_assoc_value params;
3670 struct sctp_association *asoc;
3671 int retval = -EINVAL;
3672
3673 if (optlen != sizeof(params))
3674 goto out;
3675
3676 if (copy_from_user(&params, optval, optlen)) {
3677 retval = -EFAULT;
3678 goto out;
3679 }
3680
3681 asoc = sctp_id2assoc(sk, params.assoc_id);
3682 if (asoc) {
3683 asoc->prsctp_enable = !!params.assoc_value;
3684 } else if (!params.assoc_id) {
3685 struct sctp_sock *sp = sctp_sk(sk);
3686
3687 sp->ep->prsctp_enable = !!params.assoc_value;
3688 } else {
3689 goto out;
3690 }
3691
3692 retval = 0;
3693
3694out:
3695 return retval;
3696}
3697
Xin Longf959fb42016-07-09 19:47:41 +08003698static int sctp_setsockopt_default_prinfo(struct sock *sk,
3699 char __user *optval,
3700 unsigned int optlen)
3701{
3702 struct sctp_default_prinfo info;
3703 struct sctp_association *asoc;
3704 int retval = -EINVAL;
3705
3706 if (optlen != sizeof(info))
3707 goto out;
3708
3709 if (copy_from_user(&info, optval, sizeof(info))) {
3710 retval = -EFAULT;
3711 goto out;
3712 }
3713
3714 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
3715 goto out;
3716
3717 if (info.pr_policy == SCTP_PR_SCTP_NONE)
3718 info.pr_value = 0;
3719
3720 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
3721 if (asoc) {
3722 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
3723 asoc->default_timetolive = info.pr_value;
3724 } else if (!info.pr_assoc_id) {
3725 struct sctp_sock *sp = sctp_sk(sk);
3726
3727 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
3728 sp->default_timetolive = info.pr_value;
3729 } else {
3730 goto out;
3731 }
3732
3733 retval = 0;
3734
3735out:
3736 return retval;
3737}
3738
Xin Longc0d8bab2017-03-10 12:11:12 +08003739static int sctp_setsockopt_reconfig_supported(struct sock *sk,
3740 char __user *optval,
3741 unsigned int optlen)
3742{
3743 struct sctp_assoc_value params;
3744 struct sctp_association *asoc;
3745 int retval = -EINVAL;
3746
3747 if (optlen != sizeof(params))
3748 goto out;
3749
3750 if (copy_from_user(&params, optval, optlen)) {
3751 retval = -EFAULT;
3752 goto out;
3753 }
3754
3755 asoc = sctp_id2assoc(sk, params.assoc_id);
3756 if (asoc) {
3757 asoc->reconf_enable = !!params.assoc_value;
3758 } else if (!params.assoc_id) {
3759 struct sctp_sock *sp = sctp_sk(sk);
3760
3761 sp->ep->reconf_enable = !!params.assoc_value;
3762 } else {
3763 goto out;
3764 }
3765
3766 retval = 0;
3767
3768out:
3769 return retval;
3770}
3771
Xin Long9fb657a2017-01-18 00:44:46 +08003772static int sctp_setsockopt_enable_strreset(struct sock *sk,
3773 char __user *optval,
3774 unsigned int optlen)
3775{
3776 struct sctp_assoc_value params;
3777 struct sctp_association *asoc;
3778 int retval = -EINVAL;
3779
3780 if (optlen != sizeof(params))
3781 goto out;
3782
3783 if (copy_from_user(&params, optval, optlen)) {
3784 retval = -EFAULT;
3785 goto out;
3786 }
3787
3788 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
3789 goto out;
3790
3791 asoc = sctp_id2assoc(sk, params.assoc_id);
3792 if (asoc) {
3793 asoc->strreset_enable = params.assoc_value;
3794 } else if (!params.assoc_id) {
3795 struct sctp_sock *sp = sctp_sk(sk);
3796
3797 sp->ep->strreset_enable = params.assoc_value;
3798 } else {
3799 goto out;
3800 }
3801
3802 retval = 0;
3803
3804out:
3805 return retval;
3806}
3807
Xin Long7f9d68a2017-01-18 00:44:47 +08003808static int sctp_setsockopt_reset_streams(struct sock *sk,
3809 char __user *optval,
3810 unsigned int optlen)
3811{
3812 struct sctp_reset_streams *params;
3813 struct sctp_association *asoc;
3814 int retval = -EINVAL;
3815
Xin Long2342b8d2017-12-10 15:40:51 +08003816 if (optlen < sizeof(*params))
Xin Long7f9d68a2017-01-18 00:44:47 +08003817 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003818 /* srs_number_streams is u16, so optlen can't be bigger than this. */
3819 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3820 sizeof(__u16) * sizeof(*params));
Xin Long7f9d68a2017-01-18 00:44:47 +08003821
3822 params = memdup_user(optval, optlen);
3823 if (IS_ERR(params))
3824 return PTR_ERR(params);
3825
Xin Long2342b8d2017-12-10 15:40:51 +08003826 if (params->srs_number_streams * sizeof(__u16) >
3827 optlen - sizeof(*params))
3828 goto out;
3829
Xin Long7f9d68a2017-01-18 00:44:47 +08003830 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
3831 if (!asoc)
3832 goto out;
3833
3834 retval = sctp_send_reset_streams(asoc, params);
3835
3836out:
3837 kfree(params);
3838 return retval;
3839}
3840
Xin Longa92ce1a2017-02-09 01:18:18 +08003841static int sctp_setsockopt_reset_assoc(struct sock *sk,
3842 char __user *optval,
3843 unsigned int optlen)
3844{
3845 struct sctp_association *asoc;
3846 sctp_assoc_t associd;
3847 int retval = -EINVAL;
3848
3849 if (optlen != sizeof(associd))
3850 goto out;
3851
3852 if (copy_from_user(&associd, optval, optlen)) {
3853 retval = -EFAULT;
3854 goto out;
3855 }
3856
3857 asoc = sctp_id2assoc(sk, associd);
3858 if (!asoc)
3859 goto out;
3860
3861 retval = sctp_send_reset_assoc(asoc);
3862
3863out:
3864 return retval;
3865}
3866
Xin Long242bd2d2017-02-09 01:18:20 +08003867static int sctp_setsockopt_add_streams(struct sock *sk,
3868 char __user *optval,
3869 unsigned int optlen)
3870{
3871 struct sctp_association *asoc;
3872 struct sctp_add_streams params;
3873 int retval = -EINVAL;
3874
3875 if (optlen != sizeof(params))
3876 goto out;
3877
3878 if (copy_from_user(&params, optval, optlen)) {
3879 retval = -EFAULT;
3880 goto out;
3881 }
3882
3883 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
3884 if (!asoc)
3885 goto out;
3886
3887 retval = sctp_send_add_streams(asoc, &params);
3888
3889out:
3890 return retval;
3891}
3892
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03003893static int sctp_setsockopt_scheduler(struct sock *sk,
3894 char __user *optval,
3895 unsigned int optlen)
3896{
3897 struct sctp_association *asoc;
3898 struct sctp_assoc_value params;
3899 int retval = -EINVAL;
3900
3901 if (optlen < sizeof(params))
3902 goto out;
3903
3904 optlen = sizeof(params);
3905 if (copy_from_user(&params, optval, optlen)) {
3906 retval = -EFAULT;
3907 goto out;
3908 }
3909
3910 if (params.assoc_value > SCTP_SS_MAX)
3911 goto out;
3912
3913 asoc = sctp_id2assoc(sk, params.assoc_id);
3914 if (!asoc)
3915 goto out;
3916
3917 retval = sctp_sched_set_sched(asoc, params.assoc_value);
3918
3919out:
3920 return retval;
3921}
3922
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03003923static int sctp_setsockopt_scheduler_value(struct sock *sk,
3924 char __user *optval,
3925 unsigned int optlen)
3926{
3927 struct sctp_association *asoc;
3928 struct sctp_stream_value params;
3929 int retval = -EINVAL;
3930
3931 if (optlen < sizeof(params))
3932 goto out;
3933
3934 optlen = sizeof(params);
3935 if (copy_from_user(&params, optval, optlen)) {
3936 retval = -EFAULT;
3937 goto out;
3938 }
3939
3940 asoc = sctp_id2assoc(sk, params.assoc_id);
3941 if (!asoc)
3942 goto out;
3943
3944 retval = sctp_sched_set_value(asoc, params.stream_id,
3945 params.stream_value, GFP_KERNEL);
3946
3947out:
3948 return retval;
3949}
3950
Xin Long772a5862017-12-08 21:03:58 +08003951static int sctp_setsockopt_interleaving_supported(struct sock *sk,
3952 char __user *optval,
3953 unsigned int optlen)
3954{
3955 struct sctp_sock *sp = sctp_sk(sk);
3956 struct net *net = sock_net(sk);
3957 struct sctp_assoc_value params;
3958 int retval = -EINVAL;
3959
3960 if (optlen < sizeof(params))
3961 goto out;
3962
3963 optlen = sizeof(params);
3964 if (copy_from_user(&params, optval, optlen)) {
3965 retval = -EFAULT;
3966 goto out;
3967 }
3968
3969 if (params.assoc_id)
3970 goto out;
3971
3972 if (!net->sctp.intl_enable || !sp->frag_interleave) {
3973 retval = -EPERM;
3974 goto out;
3975 }
3976
3977 sp->strm_interleave = !!params.assoc_value;
3978
3979 retval = 0;
3980
3981out:
3982 return retval;
3983}
3984
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985/* API 6.2 setsockopt(), getsockopt()
3986 *
3987 * Applications use setsockopt() and getsockopt() to set or retrieve
3988 * socket options. Socket options are used to change the default
3989 * behavior of sockets calls. They are described in Section 7.
3990 *
3991 * The syntax is:
3992 *
3993 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3994 * int __user *optlen);
3995 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
3996 * int optlen);
3997 *
3998 * sd - the socket descript.
3999 * level - set to IPPROTO_SCTP for all SCTP options.
4000 * optname - the option name.
4001 * optval - the buffer to store the value of the option.
4002 * optlen - the size of the buffer.
4003 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004004static int sctp_setsockopt(struct sock *sk, int level, int optname,
4005 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006{
4007 int retval = 0;
4008
Daniel Borkmannbb333812013-06-28 19:49:40 +02004009 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
4011 /* I can hardly begin to describe how wrong this is. This is
4012 * so broken as to be worse than useless. The API draft
4013 * REALLY is NOT helpful here... I am not convinced that the
4014 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4015 * are at all well-founded.
4016 */
4017 if (level != SOL_SCTP) {
4018 struct sctp_af *af = sctp_sk(sk)->pf->af;
4019 retval = af->setsockopt(sk, level, optname, optval, optlen);
4020 goto out_nounlock;
4021 }
4022
wangweidong048ed4b2014-01-21 15:44:11 +08004023 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
4025 switch (optname) {
4026 case SCTP_SOCKOPT_BINDX_ADD:
4027 /* 'optlen' is the size of the addresses buffer. */
4028 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4029 optlen, SCTP_BINDX_ADD_ADDR);
4030 break;
4031
4032 case SCTP_SOCKOPT_BINDX_REM:
4033 /* 'optlen' is the size of the addresses buffer. */
4034 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4035 optlen, SCTP_BINDX_REM_ADDR);
4036 break;
4037
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004038 case SCTP_SOCKOPT_CONNECTX_OLD:
4039 /* 'optlen' is the size of the addresses buffer. */
4040 retval = sctp_setsockopt_connectx_old(sk,
4041 (struct sockaddr __user *)optval,
4042 optlen);
4043 break;
4044
Frank Filz3f7a87d2005-06-20 13:14:57 -07004045 case SCTP_SOCKOPT_CONNECTX:
4046 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004047 retval = sctp_setsockopt_connectx(sk,
4048 (struct sockaddr __user *)optval,
4049 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004050 break;
4051
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 case SCTP_DISABLE_FRAGMENTS:
4053 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4054 break;
4055
4056 case SCTP_EVENTS:
4057 retval = sctp_setsockopt_events(sk, optval, optlen);
4058 break;
4059
4060 case SCTP_AUTOCLOSE:
4061 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4062 break;
4063
4064 case SCTP_PEER_ADDR_PARAMS:
4065 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4066 break;
4067
Shan Wei4580ccc2011-01-18 22:39:00 +00004068 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07004069 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08004070 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004071 case SCTP_PARTIAL_DELIVERY_POINT:
4072 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4073 break;
Frank Filz77086102005-12-22 11:37:30 -08004074
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 case SCTP_INITMSG:
4076 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4077 break;
4078 case SCTP_DEFAULT_SEND_PARAM:
4079 retval = sctp_setsockopt_default_send_param(sk, optval,
4080 optlen);
4081 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02004082 case SCTP_DEFAULT_SNDINFO:
4083 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4084 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 case SCTP_PRIMARY_ADDR:
4086 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4087 break;
4088 case SCTP_SET_PEER_PRIMARY_ADDR:
4089 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4090 break;
4091 case SCTP_NODELAY:
4092 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4093 break;
4094 case SCTP_RTOINFO:
4095 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4096 break;
4097 case SCTP_ASSOCINFO:
4098 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4099 break;
4100 case SCTP_I_WANT_MAPPED_V4_ADDR:
4101 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4102 break;
4103 case SCTP_MAXSEG:
4104 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4105 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004106 case SCTP_ADAPTATION_LAYER:
4107 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004109 case SCTP_CONTEXT:
4110 retval = sctp_setsockopt_context(sk, optval, optlen);
4111 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004112 case SCTP_FRAGMENT_INTERLEAVE:
4113 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4114 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07004115 case SCTP_MAX_BURST:
4116 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4117 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004118 case SCTP_AUTH_CHUNK:
4119 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4120 break;
4121 case SCTP_HMAC_IDENT:
4122 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4123 break;
4124 case SCTP_AUTH_KEY:
4125 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4126 break;
4127 case SCTP_AUTH_ACTIVE_KEY:
4128 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4129 break;
4130 case SCTP_AUTH_DELETE_KEY:
4131 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4132 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09004133 case SCTP_AUTO_ASCONF:
4134 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4135 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00004136 case SCTP_PEER_ADDR_THLDS:
4137 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4138 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004139 case SCTP_RECVRCVINFO:
4140 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4141 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004142 case SCTP_RECVNXTINFO:
4143 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4144 break;
Xin Long28aa4c22016-07-09 19:47:40 +08004145 case SCTP_PR_SUPPORTED:
4146 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4147 break;
Xin Longf959fb42016-07-09 19:47:41 +08004148 case SCTP_DEFAULT_PRINFO:
4149 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4150 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08004151 case SCTP_RECONFIG_SUPPORTED:
4152 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4153 break;
Xin Long9fb657a2017-01-18 00:44:46 +08004154 case SCTP_ENABLE_STREAM_RESET:
4155 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4156 break;
Xin Long7f9d68a2017-01-18 00:44:47 +08004157 case SCTP_RESET_STREAMS:
4158 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4159 break;
Xin Longa92ce1a2017-02-09 01:18:18 +08004160 case SCTP_RESET_ASSOC:
4161 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4162 break;
Xin Long242bd2d2017-02-09 01:18:20 +08004163 case SCTP_ADD_STREAMS:
4164 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4165 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004166 case SCTP_STREAM_SCHEDULER:
4167 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4168 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004169 case SCTP_STREAM_SCHEDULER_VALUE:
4170 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4171 break;
Xin Long772a5862017-12-08 21:03:58 +08004172 case SCTP_INTERLEAVING_SUPPORTED:
4173 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4174 optlen);
4175 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 default:
4177 retval = -ENOPROTOOPT;
4178 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
wangweidong048ed4b2014-01-21 15:44:11 +08004181 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
4183out_nounlock:
4184 return retval;
4185}
4186
4187/* API 3.1.6 connect() - UDP Style Syntax
4188 *
4189 * An application may use the connect() call in the UDP model to initiate an
4190 * association without sending data.
4191 *
4192 * The syntax is:
4193 *
4194 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4195 *
4196 * sd: the socket descriptor to have a new association added to.
4197 *
4198 * nam: the address structure (either struct sockaddr_in or struct
4199 * sockaddr_in6 defined in RFC2553 [7]).
4200 *
4201 * len: the size of the address.
4202 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004203static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4204 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07004207 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
wangweidong048ed4b2014-01-21 15:44:11 +08004209 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Daniel Borkmannbb333812013-06-28 19:49:40 +02004211 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4212 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
Frank Filz3f7a87d2005-06-20 13:14:57 -07004214 /* Validate addr_len before calling common connect/connectx routine. */
4215 af = sctp_get_af_specific(addr->sa_family);
4216 if (!af || addr_len < af->sockaddr_len) {
4217 err = -EINVAL;
4218 } else {
4219 /* Pass correct addr len to common routine (so it knows there
4220 * is only one address being passed.
4221 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004222 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 }
4224
wangweidong048ed4b2014-01-21 15:44:11 +08004225 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 return err;
4227}
4228
4229/* FIXME: Write comments. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004230static int sctp_disconnect(struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231{
4232 return -EOPNOTSUPP; /* STUB */
4233}
4234
4235/* 4.1.4 accept() - TCP Style Syntax
4236 *
4237 * Applications use accept() call to remove an established SCTP
4238 * association from the accept queue of the endpoint. A new socket
4239 * descriptor will be returned from accept() to represent the newly
4240 * formed association.
4241 */
David Howellscdfbabf2017-03-09 08:09:05 +00004242static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
4244 struct sctp_sock *sp;
4245 struct sctp_endpoint *ep;
4246 struct sock *newsk = NULL;
4247 struct sctp_association *asoc;
4248 long timeo;
4249 int error = 0;
4250
wangweidong048ed4b2014-01-21 15:44:11 +08004251 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
4253 sp = sctp_sk(sk);
4254 ep = sp->ep;
4255
4256 if (!sctp_style(sk, TCP)) {
4257 error = -EOPNOTSUPP;
4258 goto out;
4259 }
4260
4261 if (!sctp_sstate(sk, LISTENING)) {
4262 error = -EINVAL;
4263 goto out;
4264 }
4265
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07004266 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
4268 error = sctp_wait_for_accept(sk, timeo);
4269 if (error)
4270 goto out;
4271
4272 /* We treat the list of associations on the endpoint as the accept
4273 * queue and pick the first association on the list.
4274 */
4275 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4276
David Howellscdfbabf2017-03-09 08:09:05 +00004277 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 if (!newsk) {
4279 error = -ENOMEM;
4280 goto out;
4281 }
4282
4283 /* Populate the fields of the newsk from the oldsk and migrate the
4284 * asoc to the newsk.
4285 */
4286 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4287
4288out:
wangweidong048ed4b2014-01-21 15:44:11 +08004289 release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004290 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 return newsk;
4292}
4293
4294/* The SCTP ioctl handler. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004295static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004297 int rc = -ENOTCONN;
4298
wangweidong048ed4b2014-01-21 15:44:11 +08004299 lock_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004300
4301 /*
4302 * SEQPACKET-style sockets in LISTENING state are valid, for
4303 * SCTP, so only discard TCP-style sockets in LISTENING state.
4304 */
4305 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4306 goto out;
4307
4308 switch (cmd) {
4309 case SIOCINQ: {
4310 struct sk_buff *skb;
4311 unsigned int amount = 0;
4312
4313 skb = skb_peek(&sk->sk_receive_queue);
4314 if (skb != NULL) {
4315 /*
4316 * We will only return the amount of this packet since
4317 * that is all that will be read.
4318 */
4319 amount = skb->len;
4320 }
4321 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004322 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07004323 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004324 default:
4325 rc = -ENOIOCTLCMD;
4326 break;
4327 }
4328out:
wangweidong048ed4b2014-01-21 15:44:11 +08004329 release_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004330 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331}
4332
4333/* This is the function which gets called during socket creation to
4334 * initialized the SCTP-specific portion of the sock.
4335 * The sock structure should already be zero-filled memory.
4336 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004337static int sctp_init_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004339 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 struct sctp_sock *sp;
4341
Daniel Borkmannbb333812013-06-28 19:49:40 +02004342 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
4344 sp = sctp_sk(sk);
4345
4346 /* Initialize the SCTP per socket area. */
4347 switch (sk->sk_type) {
4348 case SOCK_SEQPACKET:
4349 sp->type = SCTP_SOCKET_UDP;
4350 break;
4351 case SOCK_STREAM:
4352 sp->type = SCTP_SOCKET_TCP;
4353 break;
4354 default:
4355 return -ESOCKTNOSUPPORT;
4356 }
4357
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004358 sk->sk_gso_type = SKB_GSO_SCTP;
4359
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 /* Initialize default send parameters. These parameters can be
4361 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4362 */
4363 sp->default_stream = 0;
4364 sp->default_ppid = 0;
4365 sp->default_flags = 0;
4366 sp->default_context = 0;
4367 sp->default_timetolive = 0;
4368
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004369 sp->default_rcv_context = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004370 sp->max_burst = net->sctp.max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004371
Neil Horman3c681982012-10-24 09:20:03 +00004372 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 /* Initialize default setup parameters. These parameters
4375 * can be modified with the SCTP_INITMSG socket option or
4376 * overridden by the SCTP_INIT CMSG.
4377 */
4378 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4379 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004380 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4381 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
4383 /* Initialize default RTO related parameters. These parameters can
4384 * be modified for with the SCTP_RTOINFO socket option.
4385 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004386 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4387 sp->rtoinfo.srto_max = net->sctp.rto_max;
4388 sp->rtoinfo.srto_min = net->sctp.rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
4390 /* Initialize default association related parameters. These parameters
4391 * can be modified with the SCTP_ASSOCINFO socket option.
4392 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004393 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 sp->assocparams.sasoc_number_peer_destinations = 0;
4395 sp->assocparams.sasoc_peer_rwnd = 0;
4396 sp->assocparams.sasoc_local_rwnd = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004397 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
4399 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004400 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 */
4402 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4403
4404 /* Default Peer Address Parameters. These defaults can
4405 * be modified via SCTP_PEER_ADDR_PARAMS
4406 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004407 sp->hbinterval = net->sctp.hb_interval;
4408 sp->pathmaxrxt = net->sctp.max_retrans_path;
wangweidong4e2d52b2013-12-23 12:16:54 +08004409 sp->pathmtu = 0; /* allow default discovery */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004410 sp->sackdelay = net->sctp.sack_timeout;
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07004411 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004412 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004413 SPP_PMTUD_ENABLE |
4414 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
4416 /* If enabled no SCTP message fragmentation will be performed.
4417 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4418 */
4419 sp->disable_fragments = 0;
4420
Sridhar Samudrala208edef2006-09-29 17:08:01 -07004421 /* Enable Nagle algorithm by default. */
4422 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004424 sp->recvrcvinfo = 0;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004425 sp->recvnxtinfo = 0;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 /* Enable by default. */
4428 sp->v4mapped = 1;
4429
4430 /* Auto-close idle associations after the configured
4431 * number of seconds. A value of 0 disables this
4432 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4433 * for UDP-style sockets only.
4434 */
4435 sp->autoclose = 0;
4436
4437 /* User specified fragmentation limit. */
4438 sp->user_frag = 0;
4439
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004440 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
4442 sp->pf = sctp_get_pf_specific(sk->sk_family);
4443
4444 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004445 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004447 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448
4449 /* Create a per socket endpoint structure. Even if we
4450 * change the data structure relationships, this may still
4451 * be useful for storing pre-connect address information.
4452 */
Daniel Borkmannc164b832013-06-14 18:24:06 +02004453 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4454 if (!sp->ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 return -ENOMEM;
4456
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 sp->hmac = NULL;
4458
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004459 sk->sk_destruct = sctp_destruct_sock;
4460
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08004462
4463 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004464 sk_sockets_allocated_inc(sk);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004465 sock_prot_inuse_add(net, sk->sk_prot, 1);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004466
4467 /* Nothing can fail after this block, otherwise
4468 * sctp_destroy_sock() will be called without addr_wq_lock held
4469 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004470 if (net->sctp.default_auto_asconf) {
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004471 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda9f7d6532011-04-26 19:32:51 +09004472 list_add_tail(&sp->auto_asconf_list,
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004473 &net->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09004474 sp->do_auto_asconf = 1;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004475 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4476 } else {
Michio Honda9f7d6532011-04-26 19:32:51 +09004477 sp->do_auto_asconf = 0;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004478 }
4479
David S. Miller6f756a82008-11-23 17:34:03 -08004480 local_bh_enable();
4481
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 return 0;
4483}
4484
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004485/* Cleanup any SCTP per socket resources. Must be called with
4486 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4487 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004488static void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489{
Michio Honda9f7d6532011-04-26 19:32:51 +09004490 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
Daniel Borkmannbb333812013-06-28 19:49:40 +02004492 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
4494 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09004495 sp = sctp_sk(sk);
Daniel Borkmann1abd1652013-06-06 15:53:47 +02004496 /* This could happen during socket init, thus we bail out
4497 * early, since the rest of the below is not setup either.
4498 */
4499 if (sp->ep == NULL)
4500 return;
4501
Michio Honda9f7d6532011-04-26 19:32:51 +09004502 if (sp->do_auto_asconf) {
4503 sp->do_auto_asconf = 0;
4504 list_del(&sp->auto_asconf_list);
4505 }
4506 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004507 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004508 sk_sockets_allocated_dec(sk);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08004509 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004510 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511}
4512
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004513/* Triggered when there are no references on the socket anymore */
4514static void sctp_destruct_sock(struct sock *sk)
4515{
4516 struct sctp_sock *sp = sctp_sk(sk);
4517
4518 /* Free up the HMAC transform. */
Herbert Xu5821c762016-01-24 21:20:12 +08004519 crypto_free_shash(sp->hmac);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004520
4521 inet_sock_destruct(sk);
4522}
4523
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524/* API 4.1.7 shutdown() - TCP Style Syntax
4525 * int shutdown(int socket, int how);
4526 *
4527 * sd - the socket descriptor of the association to be closed.
4528 * how - Specifies the type of shutdown. The values are
4529 * as follows:
4530 * SHUT_RD
4531 * Disables further receive operations. No SCTP
4532 * protocol action is taken.
4533 * SHUT_WR
4534 * Disables further send operations, and initiates
4535 * the SCTP shutdown sequence.
4536 * SHUT_RDWR
4537 * Disables further send and receive operations
4538 * and initiates the SCTP shutdown sequence.
4539 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004540static void sctp_shutdown(struct sock *sk, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004542 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 struct sctp_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
4545 if (!sctp_style(sk, TCP))
4546 return;
4547
Xin Long5bf35dd2016-11-13 21:44:37 +08004548 ep = sctp_sk(sk)->ep;
4549 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4550 struct sctp_association *asoc;
4551
Yafang Shaocbabf462017-12-20 11:12:54 +08004552 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Xin Long5bf35dd2016-11-13 21:44:37 +08004553 asoc = list_entry(ep->asocs.next,
4554 struct sctp_association, asocs);
4555 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 }
4557}
4558
Xin Long52c52a62016-04-14 15:35:30 +08004559int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4560 struct sctp_info *info)
4561{
4562 struct sctp_transport *prim;
4563 struct list_head *pos;
4564 int mask;
4565
4566 memset(info, 0, sizeof(*info));
4567 if (!asoc) {
4568 struct sctp_sock *sp = sctp_sk(sk);
4569
4570 info->sctpi_s_autoclose = sp->autoclose;
4571 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4572 info->sctpi_s_pd_point = sp->pd_point;
4573 info->sctpi_s_nodelay = sp->nodelay;
4574 info->sctpi_s_disable_fragments = sp->disable_fragments;
4575 info->sctpi_s_v4mapped = sp->v4mapped;
4576 info->sctpi_s_frag_interleave = sp->frag_interleave;
Xin Long40eb90e92016-05-29 17:42:13 +08004577 info->sctpi_s_type = sp->type;
Xin Long52c52a62016-04-14 15:35:30 +08004578
4579 return 0;
4580 }
4581
4582 info->sctpi_tag = asoc->c.my_vtag;
4583 info->sctpi_state = asoc->state;
4584 info->sctpi_rwnd = asoc->a_rwnd;
4585 info->sctpi_unackdata = asoc->unack_data;
4586 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004587 info->sctpi_instrms = asoc->stream.incnt;
4588 info->sctpi_outstrms = asoc->stream.outcnt;
Xin Long52c52a62016-04-14 15:35:30 +08004589 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4590 info->sctpi_inqueue++;
4591 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4592 info->sctpi_outqueue++;
4593 info->sctpi_overall_error = asoc->overall_error_count;
4594 info->sctpi_max_burst = asoc->max_burst;
4595 info->sctpi_maxseg = asoc->frag_point;
4596 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4597 info->sctpi_peer_tag = asoc->c.peer_vtag;
4598
4599 mask = asoc->peer.ecn_capable << 1;
4600 mask = (mask | asoc->peer.ipv4_address) << 1;
4601 mask = (mask | asoc->peer.ipv6_address) << 1;
4602 mask = (mask | asoc->peer.hostname_address) << 1;
4603 mask = (mask | asoc->peer.asconf_capable) << 1;
4604 mask = (mask | asoc->peer.prsctp_capable) << 1;
4605 mask = (mask | asoc->peer.auth_capable);
4606 info->sctpi_peer_capable = mask;
4607 mask = asoc->peer.sack_needed << 1;
4608 mask = (mask | asoc->peer.sack_generation) << 1;
4609 mask = (mask | asoc->peer.zero_window_announced);
4610 info->sctpi_peer_sack = mask;
4611
4612 info->sctpi_isacks = asoc->stats.isacks;
4613 info->sctpi_osacks = asoc->stats.osacks;
4614 info->sctpi_opackets = asoc->stats.opackets;
4615 info->sctpi_ipackets = asoc->stats.ipackets;
4616 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4617 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4618 info->sctpi_idupchunks = asoc->stats.idupchunks;
4619 info->sctpi_gapcnt = asoc->stats.gapcnt;
4620 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4621 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4622 info->sctpi_oodchunks = asoc->stats.oodchunks;
4623 info->sctpi_iodchunks = asoc->stats.iodchunks;
4624 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4625 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4626
4627 prim = asoc->peer.primary_path;
Stefano Brivioee6c88b2017-08-23 13:27:13 +02004628 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
Xin Long52c52a62016-04-14 15:35:30 +08004629 info->sctpi_p_state = prim->state;
4630 info->sctpi_p_cwnd = prim->cwnd;
4631 info->sctpi_p_srtt = prim->srtt;
4632 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4633 info->sctpi_p_hbinterval = prim->hbinterval;
4634 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4635 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4636 info->sctpi_p_ssthresh = prim->ssthresh;
4637 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4638 info->sctpi_p_flight_size = prim->flight_size;
4639 info->sctpi_p_error = prim->error_count;
4640
4641 return 0;
4642}
4643EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4644
Xin Long626d16f2016-04-14 15:35:31 +08004645/* use callback to avoid exporting the core structure */
Tom Herbert97a6ec42017-12-04 10:31:41 -08004646void sctp_transport_walk_start(struct rhashtable_iter *iter)
Xin Long626d16f2016-04-14 15:35:31 +08004647{
Xin Long7fda702f2016-11-15 23:23:11 +08004648 rhltable_walk_enter(&sctp_transport_hashtable, iter);
Xin Long626d16f2016-04-14 15:35:31 +08004649
Tom Herbert97a6ec42017-12-04 10:31:41 -08004650 rhashtable_walk_start(iter);
Xin Long626d16f2016-04-14 15:35:31 +08004651}
4652
4653void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4654{
4655 rhashtable_walk_stop(iter);
4656 rhashtable_walk_exit(iter);
4657}
4658
4659struct sctp_transport *sctp_transport_get_next(struct net *net,
4660 struct rhashtable_iter *iter)
4661{
4662 struct sctp_transport *t;
4663
4664 t = rhashtable_walk_next(iter);
4665 for (; t; t = rhashtable_walk_next(iter)) {
4666 if (IS_ERR(t)) {
4667 if (PTR_ERR(t) == -EAGAIN)
4668 continue;
4669 break;
4670 }
4671
4672 if (net_eq(sock_net(t->asoc->base.sk), net) &&
4673 t->asoc->peer.primary_path == t)
4674 break;
4675 }
4676
4677 return t;
4678}
4679
4680struct sctp_transport *sctp_transport_get_idx(struct net *net,
4681 struct rhashtable_iter *iter,
4682 int pos)
4683{
4684 void *obj = SEQ_START_TOKEN;
4685
4686 while (pos && (obj = sctp_transport_get_next(net, iter)) &&
4687 !IS_ERR(obj))
4688 pos--;
4689
4690 return obj;
4691}
4692
4693int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
4694 void *p) {
4695 int err = 0;
4696 int hash = 0;
4697 struct sctp_ep_common *epb;
4698 struct sctp_hashbucket *head;
4699
4700 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
4701 hash++, head++) {
Xin Long581409d2017-06-10 14:48:14 +08004702 read_lock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004703 sctp_for_each_hentry(epb, &head->chain) {
4704 err = cb(sctp_ep(epb), p);
4705 if (err)
4706 break;
4707 }
Xin Long581409d2017-06-10 14:48:14 +08004708 read_unlock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004709 }
4710
4711 return err;
4712}
4713EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
4714
4715int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
4716 struct net *net,
4717 const union sctp_addr *laddr,
4718 const union sctp_addr *paddr, void *p)
4719{
4720 struct sctp_transport *transport;
Xin Long08abb792016-12-15 23:05:52 +08004721 int err;
Xin Long626d16f2016-04-14 15:35:31 +08004722
4723 rcu_read_lock();
4724 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
Xin Long626d16f2016-04-14 15:35:31 +08004725 rcu_read_unlock();
Xin Long08abb792016-12-15 23:05:52 +08004726 if (!transport)
4727 return -ENOENT;
4728
Xin Long1cceda782016-09-29 02:55:44 +08004729 err = cb(transport, p);
Xin Longcd26da42016-10-31 20:32:31 +08004730 sctp_transport_put(transport);
Xin Long1cceda782016-09-29 02:55:44 +08004731
Xin Long626d16f2016-04-14 15:35:31 +08004732 return err;
4733}
4734EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
4735
4736int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
Xin Longd25adbe2017-09-15 11:02:21 +08004737 int (*cb_done)(struct sctp_transport *, void *),
4738 struct net *net, int *pos, void *p) {
Xin Long626d16f2016-04-14 15:35:31 +08004739 struct rhashtable_iter hti;
Xin Longd25adbe2017-09-15 11:02:21 +08004740 struct sctp_transport *tsp;
4741 int ret;
Xin Long626d16f2016-04-14 15:35:31 +08004742
Xin Longd25adbe2017-09-15 11:02:21 +08004743again:
Xin Longf53d77e2018-01-23 18:22:25 +08004744 ret = 0;
Tom Herbert97a6ec42017-12-04 10:31:41 -08004745 sctp_transport_walk_start(&hti);
Xin Long626d16f2016-04-14 15:35:31 +08004746
Xin Longd25adbe2017-09-15 11:02:21 +08004747 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
4748 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
4749 if (!sctp_transport_hold(tsp))
Xin Long626d16f2016-04-14 15:35:31 +08004750 continue;
Xin Longd25adbe2017-09-15 11:02:21 +08004751 ret = cb(tsp, p);
4752 if (ret)
Xin Long626d16f2016-04-14 15:35:31 +08004753 break;
Xin Longd25adbe2017-09-15 11:02:21 +08004754 (*pos)++;
4755 sctp_transport_put(tsp);
Xin Long626d16f2016-04-14 15:35:31 +08004756 }
Xin Long626d16f2016-04-14 15:35:31 +08004757 sctp_transport_walk_stop(&hti);
Xin Long53fa1032016-04-14 15:35:35 +08004758
Xin Longd25adbe2017-09-15 11:02:21 +08004759 if (ret) {
4760 if (cb_done && !cb_done(tsp, p)) {
4761 (*pos)++;
4762 sctp_transport_put(tsp);
4763 goto again;
4764 }
4765 sctp_transport_put(tsp);
4766 }
4767
4768 return ret;
Xin Long626d16f2016-04-14 15:35:31 +08004769}
4770EXPORT_SYMBOL_GPL(sctp_for_each_transport);
4771
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772/* 7.2.1 Association Status (SCTP_STATUS)
4773
4774 * Applications can retrieve current status information about an
4775 * association, including association state, peer receiver window size,
4776 * number of unacked data chunks, and number of data chunks pending
4777 * receipt. This information is read-only.
4778 */
4779static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4780 char __user *optval,
4781 int __user *optlen)
4782{
4783 struct sctp_status status;
4784 struct sctp_association *asoc = NULL;
4785 struct sctp_transport *transport;
4786 sctp_assoc_t associd;
4787 int retval = 0;
4788
Neil Horman408f22e2007-06-16 14:03:45 -04004789 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 retval = -EINVAL;
4791 goto out;
4792 }
4793
Neil Horman408f22e2007-06-16 14:03:45 -04004794 len = sizeof(status);
4795 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 retval = -EFAULT;
4797 goto out;
4798 }
4799
4800 associd = status.sstat_assoc_id;
4801 asoc = sctp_id2assoc(sk, associd);
4802 if (!asoc) {
4803 retval = -EINVAL;
4804 goto out;
4805 }
4806
4807 transport = asoc->peer.primary_path;
4808
4809 status.sstat_assoc_id = sctp_assoc2id(asoc);
Daniel Borkmann38ab1fa2014-08-28 15:28:26 +02004810 status.sstat_state = sctp_assoc_to_state(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 status.sstat_rwnd = asoc->peer.rwnd;
4812 status.sstat_unackdata = asoc->unack_data;
4813
4814 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004815 status.sstat_instrms = asoc->stream.incnt;
4816 status.sstat_outstrms = asoc->stream.outcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 status.sstat_fragmentation_point = asoc->frag_point;
4818 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08004819 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4820 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 /* Map ipv4 address into v4-mapped-on-v6 address. */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06004822 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004824 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4826 status.sstat_primary.spinfo_srtt = transport->srtt;
4827 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004828 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
Frank Filz3f7a87d2005-06-20 13:14:57 -07004830 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4831 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4832
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 if (put_user(len, optlen)) {
4834 retval = -EFAULT;
4835 goto out;
4836 }
4837
Daniel Borkmannbb333812013-06-28 19:49:40 +02004838 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
4839 __func__, len, status.sstat_state, status.sstat_rwnd,
4840 status.sstat_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
4842 if (copy_to_user(optval, &status, len)) {
4843 retval = -EFAULT;
4844 goto out;
4845 }
4846
4847out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004848 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849}
4850
4851
4852/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4853 *
4854 * Applications can retrieve information about a specific peer address
4855 * of an association, including its reachability state, congestion
4856 * window, and retransmission timer values. This information is
4857 * read-only.
4858 */
4859static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4860 char __user *optval,
4861 int __user *optlen)
4862{
4863 struct sctp_paddrinfo pinfo;
4864 struct sctp_transport *transport;
4865 int retval = 0;
4866
Neil Horman408f22e2007-06-16 14:03:45 -04004867 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 retval = -EINVAL;
4869 goto out;
4870 }
4871
Neil Horman408f22e2007-06-16 14:03:45 -04004872 len = sizeof(pinfo);
4873 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 retval = -EFAULT;
4875 goto out;
4876 }
4877
4878 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4879 pinfo.spinfo_assoc_id);
4880 if (!transport)
4881 return -EINVAL;
4882
4883 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004884 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 pinfo.spinfo_cwnd = transport->cwnd;
4886 pinfo.spinfo_srtt = transport->srtt;
4887 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004888 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Frank Filz3f7a87d2005-06-20 13:14:57 -07004890 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4891 pinfo.spinfo_state = SCTP_ACTIVE;
4892
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 if (put_user(len, optlen)) {
4894 retval = -EFAULT;
4895 goto out;
4896 }
4897
4898 if (copy_to_user(optval, &pinfo, len)) {
4899 retval = -EFAULT;
4900 goto out;
4901 }
4902
4903out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004904 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905}
4906
4907/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4908 *
4909 * This option is a on/off flag. If enabled no SCTP message
4910 * fragmentation will be performed. Instead if a message being sent
4911 * exceeds the current PMTU size, the message will NOT be sent and
4912 * instead a error will be indicated to the user.
4913 */
4914static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4915 char __user *optval, int __user *optlen)
4916{
4917 int val;
4918
4919 if (len < sizeof(int))
4920 return -EINVAL;
4921
4922 len = sizeof(int);
4923 val = (sctp_sk(sk)->disable_fragments == 1);
4924 if (put_user(len, optlen))
4925 return -EFAULT;
4926 if (copy_to_user(optval, &val, len))
4927 return -EFAULT;
4928 return 0;
4929}
4930
4931/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4932 *
4933 * This socket option is used to specify various notifications and
4934 * ancillary data the user wishes to receive.
4935 */
4936static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
4937 int __user *optlen)
4938{
Jiri Slabya4b8e712016-10-21 14:13:24 +02004939 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00004941 if (len > sizeof(struct sctp_event_subscribe))
4942 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04004943 if (put_user(len, optlen))
4944 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4946 return -EFAULT;
4947 return 0;
4948}
4949
4950/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
4951 *
4952 * This socket option is applicable to the UDP-style socket only. When
4953 * set it will cause associations that are idle for more than the
4954 * specified number of seconds to automatically close. An association
4955 * being idle is defined an association that has NOT sent or received
4956 * user data. The special value of '0' indicates that no automatic
4957 * close of any associations should be performed. The option expects an
4958 * integer defining the number of seconds of idle time before an
4959 * association is closed.
4960 */
4961static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
4962{
4963 /* Applicable to UDP-style socket only */
4964 if (sctp_style(sk, TCP))
4965 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04004966 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004968 len = sizeof(int);
4969 if (put_user(len, optlen))
4970 return -EFAULT;
David Windsorb2ce04c2017-06-10 22:50:43 -04004971 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 return -EFAULT;
4973 return 0;
4974}
4975
4976/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00004977int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978{
Benjamin Poirier0343c552012-03-08 05:55:58 +00004979 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06004980 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 struct socket *sock;
4982 int err = 0;
4983
Xin Longdf80cd92017-10-17 23:26:10 +08004984 /* Do not peel off from one netns to another one. */
4985 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
4986 return -EINVAL;
4987
Benjamin Poirier0343c552012-03-08 05:55:58 +00004988 if (!asoc)
4989 return -EINVAL;
4990
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 /* An association cannot be branched off from an already peeled-off
4992 * socket, nor is this supported for tcp style sockets.
4993 */
4994 if (!sctp_style(sk, UDP))
4995 return -EINVAL;
4996
4997 /* Create a new socket. */
4998 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
4999 if (err < 0)
5000 return err;
5001
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005002 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005003
5004 /* Make peeled-off sockets more like 1-1 accepted sockets.
5005 * Set the daddr and initialize id to something more random
5006 */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005007 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005008
5009 /* Populate the fields of the newsk from the oldsk and migrate the
5010 * asoc to the newsk.
5011 */
5012 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005013
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 *sockp = sock;
5015
5016 return err;
5017}
Benjamin Poirier0343c552012-03-08 05:55:58 +00005018EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005020static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5021 struct file **newfile, unsigned flags)
5022{
5023 struct socket *newsock;
5024 int retval;
5025
5026 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5027 if (retval < 0)
5028 goto out;
5029
5030 /* Map the socket to an unused fd that can be returned to the user. */
5031 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5032 if (retval < 0) {
5033 sock_release(newsock);
5034 goto out;
5035 }
5036
5037 *newfile = sock_alloc_file(newsock, 0, NULL);
5038 if (IS_ERR(*newfile)) {
5039 put_unused_fd(retval);
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005040 retval = PTR_ERR(*newfile);
5041 *newfile = NULL;
5042 return retval;
5043 }
5044
5045 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5046 retval);
5047
5048 peeloff->sd = retval;
5049
5050 if (flags & SOCK_NONBLOCK)
5051 (*newfile)->f_flags |= O_NONBLOCK;
5052out:
5053 return retval;
5054}
5055
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5057{
5058 sctp_peeloff_arg_t peeloff;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005059 struct file *newfile = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
Neil Horman408f22e2007-06-16 14:03:45 -04005062 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005064 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 if (copy_from_user(&peeloff, optval, len))
5066 return -EFAULT;
5067
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005068 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 if (retval < 0)
5070 goto out;
5071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04005073 if (put_user(len, optlen)) {
5074 fput(newfile);
5075 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04005076 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04005077 }
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005078
5079 if (copy_to_user(optval, &peeloff, len)) {
5080 fput(newfile);
5081 put_unused_fd(retval);
5082 return -EFAULT;
5083 }
5084 fd_install(retval, newfile);
5085out:
5086 return retval;
5087}
5088
5089static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5090 char __user *optval, int __user *optlen)
5091{
5092 sctp_peeloff_flags_arg_t peeloff;
5093 struct file *newfile = NULL;
5094 int retval = 0;
5095
5096 if (len < sizeof(sctp_peeloff_flags_arg_t))
5097 return -EINVAL;
5098 len = sizeof(sctp_peeloff_flags_arg_t);
5099 if (copy_from_user(&peeloff, optval, len))
5100 return -EFAULT;
5101
5102 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5103 &newfile, peeloff.flags);
5104 if (retval < 0)
5105 goto out;
5106
5107 /* Return the fd mapped to the new socket. */
5108 if (put_user(len, optlen)) {
5109 fput(newfile);
5110 put_unused_fd(retval);
5111 return -EFAULT;
5112 }
5113
Al Viro56b31d12012-08-18 00:25:51 -04005114 if (copy_to_user(optval, &peeloff, len)) {
5115 fput(newfile);
5116 put_unused_fd(retval);
5117 return -EFAULT;
5118 }
5119 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120out:
5121 return retval;
5122}
5123
5124/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5125 *
5126 * Applications can enable or disable heartbeats for any peer address of
5127 * an association, modify an address's heartbeat interval, force a
5128 * heartbeat to be sent immediately, and adjust the address's maximum
5129 * number of retransmissions sent before an address is considered
5130 * unreachable. The following structure is used to access and modify an
5131 * address's parameters:
5132 *
5133 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005134 * sctp_assoc_t spp_assoc_id;
5135 * struct sockaddr_storage spp_address;
5136 * uint32_t spp_hbinterval;
5137 * uint16_t spp_pathmaxrxt;
5138 * uint32_t spp_pathmtu;
5139 * uint32_t spp_sackdelay;
5140 * uint32_t spp_flags;
5141 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08005143 * spp_assoc_id - (one-to-many style socket) This is filled in the
5144 * application, and identifies the association for
5145 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 * spp_address - This specifies which address is of interest.
5147 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005148 * in milliseconds. If a value of zero
5149 * is present in this field then no changes are to
5150 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 * spp_pathmaxrxt - This contains the maximum number of
5152 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08005153 * considered unreachable. If a value of zero
5154 * is present in this field then no changes are to
5155 * be made to this parameter.
5156 * spp_pathmtu - When Path MTU discovery is disabled the value
5157 * specified here will be the "fixed" path mtu.
5158 * Note that if the spp_address field is empty
5159 * then all associations on this address will
5160 * have this fixed path mtu set upon them.
5161 *
5162 * spp_sackdelay - When delayed sack is enabled, this value specifies
5163 * the number of milliseconds that sacks will be delayed
5164 * for. This value will apply to all addresses of an
5165 * association if the spp_address field is empty. Note
5166 * also, that if delayed sack is enabled and this
5167 * value is set to 0, no change is made to the last
5168 * recorded delayed sack timer value.
5169 *
5170 * spp_flags - These flags are used to control various features
5171 * on an association. The flag field may contain
5172 * zero or more of the following options.
5173 *
5174 * SPP_HB_ENABLE - Enable heartbeats on the
5175 * specified address. Note that if the address
5176 * field is empty all addresses for the association
5177 * have heartbeats enabled upon them.
5178 *
5179 * SPP_HB_DISABLE - Disable heartbeats on the
5180 * speicifed address. Note that if the address
5181 * field is empty all addresses for the association
5182 * will have their heartbeats disabled. Note also
5183 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5184 * mutually exclusive, only one of these two should
5185 * be specified. Enabling both fields will have
5186 * undetermined results.
5187 *
5188 * SPP_HB_DEMAND - Request a user initiated heartbeat
5189 * to be made immediately.
5190 *
5191 * SPP_PMTUD_ENABLE - This field will enable PMTU
5192 * discovery upon the specified address. Note that
5193 * if the address feild is empty then all addresses
5194 * on the association are effected.
5195 *
5196 * SPP_PMTUD_DISABLE - This field will disable PMTU
5197 * discovery upon the specified address. Note that
5198 * if the address feild is empty then all addresses
5199 * on the association are effected. Not also that
5200 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5201 * exclusive. Enabling both will have undetermined
5202 * results.
5203 *
5204 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5205 * on delayed sack. The time specified in spp_sackdelay
5206 * is used to specify the sack delay for this address. Note
5207 * that if spp_address is empty then all addresses will
5208 * enable delayed sack and take on the sack delay
5209 * value specified in spp_sackdelay.
5210 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5211 * off delayed sack. If the spp_address field is blank then
5212 * delayed sack is disabled for the entire association. Note
5213 * also that this field is mutually exclusive to
5214 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5215 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 */
5217static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005218 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219{
Frank Filz52ccb8e2005-12-22 11:36:46 -08005220 struct sctp_paddrparams params;
5221 struct sctp_transport *trans = NULL;
5222 struct sctp_association *asoc = NULL;
5223 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
Neil Horman408f22e2007-06-16 14:03:45 -04005225 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005227 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 if (copy_from_user(&params, optval, len))
5229 return -EFAULT;
5230
Frank Filz52ccb8e2005-12-22 11:36:46 -08005231 /* If an address other than INADDR_ANY is specified, and
5232 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 */
wangweidongcb3f8372013-12-23 12:16:50 +08005234 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005235 trans = sctp_addr_id2transport(sk, &params.spp_address,
5236 params.spp_assoc_id);
5237 if (!trans) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005238 pr_debug("%s: failed no transport\n", __func__);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005239 return -EINVAL;
5240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 }
5242
Frank Filz52ccb8e2005-12-22 11:36:46 -08005243 /* Get association, if assoc_id != 0 and the socket is a one
5244 * to many style socket, and an association was not found, then
5245 * the id was invalid.
5246 */
5247 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5248 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005249 pr_debug("%s: failed no association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08005251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
Frank Filz52ccb8e2005-12-22 11:36:46 -08005253 if (trans) {
5254 /* Fetch transport values. */
5255 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5256 params.spp_pathmtu = trans->pathmtu;
5257 params.spp_pathmaxrxt = trans->pathmaxrxt;
5258 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259
Frank Filz52ccb8e2005-12-22 11:36:46 -08005260 /*draft-11 doesn't say what to return in spp_flags*/
5261 params.spp_flags = trans->param_flags;
5262 } else if (asoc) {
5263 /* Fetch association values. */
5264 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5265 params.spp_pathmtu = asoc->pathmtu;
5266 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5267 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
Frank Filz52ccb8e2005-12-22 11:36:46 -08005269 /*draft-11 doesn't say what to return in spp_flags*/
5270 params.spp_flags = asoc->param_flags;
5271 } else {
5272 /* Fetch socket values. */
5273 params.spp_hbinterval = sp->hbinterval;
5274 params.spp_pathmtu = sp->pathmtu;
5275 params.spp_sackdelay = sp->sackdelay;
5276 params.spp_pathmaxrxt = sp->pathmaxrxt;
5277
5278 /*draft-11 doesn't say what to return in spp_flags*/
5279 params.spp_flags = sp->param_flags;
5280 }
5281
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 if (copy_to_user(optval, &params, len))
5283 return -EFAULT;
5284
5285 if (put_user(len, optlen))
5286 return -EFAULT;
5287
5288 return 0;
5289}
5290
Wei Yongjund364d922008-05-09 15:13:26 -07005291/*
5292 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08005293 *
Wei Yongjund364d922008-05-09 15:13:26 -07005294 * This option will effect the way delayed acks are performed. This
5295 * option allows you to get or set the delayed ack time, in
5296 * milliseconds. It also allows changing the delayed ack frequency.
5297 * Changing the frequency to 1 disables the delayed sack algorithm. If
5298 * the assoc_id is 0, then this sets or gets the endpoints default
5299 * values. If the assoc_id field is non-zero, then the set or get
5300 * effects the specified association for the one to many model (the
5301 * assoc_id field is ignored by the one to one model). Note that if
5302 * sack_delay or sack_freq are 0 when setting this option, then the
5303 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08005304 *
Wei Yongjund364d922008-05-09 15:13:26 -07005305 * struct sctp_sack_info {
5306 * sctp_assoc_t sack_assoc_id;
5307 * uint32_t sack_delay;
5308 * uint32_t sack_freq;
5309 * };
Frank Filz77086102005-12-22 11:37:30 -08005310 *
Wei Yongjund364d922008-05-09 15:13:26 -07005311 * sack_assoc_id - This parameter, indicates which association the user
5312 * is performing an action upon. Note that if this field's value is
5313 * zero then the endpoints default value is changed (effecting future
5314 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08005315 *
Wei Yongjund364d922008-05-09 15:13:26 -07005316 * sack_delay - This parameter contains the number of milliseconds that
5317 * the user is requesting the delayed ACK timer be set to. Note that
5318 * this value is defined in the standard to be between 200 and 500
5319 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08005320 *
Wei Yongjund364d922008-05-09 15:13:26 -07005321 * sack_freq - This parameter contains the number of packets that must
5322 * be received before a sack is sent without waiting for the delay
5323 * timer to expire. The default value for this is 2, setting this
5324 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08005325 */
Wei Yongjund364d922008-05-09 15:13:26 -07005326static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08005327 char __user *optval,
5328 int __user *optlen)
5329{
Wei Yongjund364d922008-05-09 15:13:26 -07005330 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08005331 struct sctp_association *asoc = NULL;
5332 struct sctp_sock *sp = sctp_sk(sk);
5333
Wei Yongjund364d922008-05-09 15:13:26 -07005334 if (len >= sizeof(struct sctp_sack_info)) {
5335 len = sizeof(struct sctp_sack_info);
5336
5337 if (copy_from_user(&params, optval, len))
5338 return -EFAULT;
5339 } else if (len == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05005340 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005341 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005342 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005343 "Use struct sctp_sack_info instead\n",
5344 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07005345 if (copy_from_user(&params, optval, len))
5346 return -EFAULT;
5347 } else
wangweidongcb3f8372013-12-23 12:16:50 +08005348 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08005349
Wei Yongjund364d922008-05-09 15:13:26 -07005350 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08005351 * to many style socket, and an association was not found, then
5352 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005353 */
Wei Yongjund364d922008-05-09 15:13:26 -07005354 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5355 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08005356 return -EINVAL;
5357
5358 if (asoc) {
5359 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005360 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5361 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08005362 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07005363 params.sack_freq = asoc->sackfreq;
5364
5365 } else {
5366 params.sack_delay = 0;
5367 params.sack_freq = 1;
5368 }
Frank Filz77086102005-12-22 11:37:30 -08005369 } else {
5370 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005371 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5372 params.sack_delay = sp->sackdelay;
5373 params.sack_freq = sp->sackfreq;
5374 } else {
5375 params.sack_delay = 0;
5376 params.sack_freq = 1;
5377 }
Frank Filz77086102005-12-22 11:37:30 -08005378 }
5379
5380 if (copy_to_user(optval, &params, len))
5381 return -EFAULT;
5382
5383 if (put_user(len, optlen))
5384 return -EFAULT;
5385
5386 return 0;
5387}
5388
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5390 *
5391 * Applications can specify protocol parameters for the default association
5392 * initialization. The option name argument to setsockopt() and getsockopt()
5393 * is SCTP_INITMSG.
5394 *
5395 * Setting initialization parameters is effective only on an unconnected
5396 * socket (for UDP-style sockets only future associations are effected
5397 * by the change). With TCP-style sockets, this option is inherited by
5398 * sockets derived from a listener socket.
5399 */
5400static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5401{
Neil Horman408f22e2007-06-16 14:03:45 -04005402 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005404 len = sizeof(struct sctp_initmsg);
5405 if (put_user(len, optlen))
5406 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5408 return -EFAULT;
5409 return 0;
5410}
5411
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005413static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5414 char __user *optval, int __user *optlen)
5415{
5416 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005417 int cnt = 0;
5418 struct sctp_getaddrs getaddrs;
5419 struct sctp_transport *from;
5420 void __user *to;
5421 union sctp_addr temp;
5422 struct sctp_sock *sp = sctp_sk(sk);
5423 int addrlen;
5424 size_t space_left;
5425 int bytes_copied;
5426
5427 if (len < sizeof(struct sctp_getaddrs))
5428 return -EINVAL;
5429
5430 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5431 return -EFAULT;
5432
5433 /* For UDP-style sockets, id specifies the association to query. */
5434 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5435 if (!asoc)
5436 return -EINVAL;
5437
wangweidongcb3f8372013-12-23 12:16:50 +08005438 to = optval + offsetof(struct sctp_getaddrs, addrs);
5439 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005440
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07005441 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5442 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08005443 memcpy(&temp, &from->ipaddr, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005444 addrlen = sctp_get_pf_specific(sk->sk_family)
5445 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005446 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005447 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005448 if (copy_to_user(to, &temp, addrlen))
5449 return -EFAULT;
5450 to += addrlen;
5451 cnt++;
5452 space_left -= addrlen;
5453 }
5454
5455 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5456 return -EFAULT;
5457 bytes_copied = ((char __user *)to) - optval;
5458 if (put_user(bytes_copied, optlen))
5459 return -EFAULT;
5460
5461 return 0;
5462}
5463
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005464static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5465 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005466{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005467 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005468 union sctp_addr temp;
5469 int cnt = 0;
5470 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005471 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005472
Vlad Yasevich29303542007-09-16 16:02:12 -07005473 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005474 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07005475 if (!addr->valid)
5476 continue;
5477
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005478 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08005479 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005480 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005481 if ((PF_INET6 == sk->sk_family) &&
5482 inet_v6_ipv6only(sk) &&
5483 (AF_INET == addr->a.sa.sa_family))
5484 continue;
Al Viro6244be42006-11-20 17:21:44 -08005485 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05005486 if (!temp.v4.sin_port)
5487 temp.v4.sin_port = htons(port);
5488
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005489 addrlen = sctp_get_pf_specific(sk->sk_family)
5490 ->addr_to_user(sctp_sk(sk), &temp);
5491
Vlad Yasevich29303542007-09-16 16:02:12 -07005492 if (space_left < addrlen) {
5493 cnt = -ENOMEM;
5494 break;
5495 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005496 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08005497
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005498 to += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005499 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005500 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04005501 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005502 }
Vlad Yasevich29303542007-09-16 16:02:12 -07005503 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005504
5505 return cnt;
5506}
5507
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005509static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5510 char __user *optval, int __user *optlen)
5511{
5512 struct sctp_bind_addr *bp;
5513 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005514 int cnt = 0;
5515 struct sctp_getaddrs getaddrs;
5516 struct sctp_sockaddr_entry *addr;
5517 void __user *to;
5518 union sctp_addr temp;
5519 struct sctp_sock *sp = sctp_sk(sk);
5520 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005521 int err = 0;
5522 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005523 int bytes_copied = 0;
5524 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005525 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005526
Neil Horman408f22e2007-06-16 14:03:45 -04005527 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005528 return -EINVAL;
5529
5530 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5531 return -EFAULT;
5532
5533 /*
5534 * For UDP-style sockets, id specifies the association to query.
5535 * If the id field is set to the value '0' then the locally bound
5536 * addresses are returned without regard to any particular
5537 * association.
5538 */
5539 if (0 == getaddrs.assoc_id) {
5540 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005541 } else {
5542 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5543 if (!asoc)
5544 return -EINVAL;
5545 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005546 }
5547
wangweidongcb3f8372013-12-23 12:16:50 +08005548 to = optval + offsetof(struct sctp_getaddrs, addrs);
5549 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Neil Horman186e2342007-06-18 19:59:16 -04005550
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02005551 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005552 if (!addrs)
5553 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005554
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005555 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5556 * addresses from the global local address list.
5557 */
5558 if (sctp_list_single_entry(&bp->address_list)) {
5559 addr = list_entry(bp->address_list.next,
5560 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04005561 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005562 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5563 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005564 if (cnt < 0) {
5565 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005566 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005567 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005568 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005569 }
5570 }
5571
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005572 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005573 /* Protection on the bound address list is not needed since
5574 * in the socket option context we hold a socket lock and
5575 * thus the bound address list can't change.
5576 */
5577 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08005578 memcpy(&temp, &addr->a, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005579 addrlen = sctp_get_pf_specific(sk->sk_family)
5580 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005581 if (space_left < addrlen) {
5582 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07005583 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005584 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005585 memcpy(buf, &temp, addrlen);
5586 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005587 bytes_copied += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005588 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005589 space_left -= addrlen;
5590 }
5591
5592copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005593 if (copy_to_user(to, addrs, bytes_copied)) {
5594 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005595 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005596 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005597 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
5598 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005599 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005600 }
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02005601 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
5602 * but we can't change it anymore.
5603 */
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005604 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005605 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005606out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005607 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005608 return err;
5609}
5610
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
5612 *
5613 * Requests that the local SCTP stack use the enclosed peer address as
5614 * the association primary. The enclosed address must be one of the
5615 * association peer's addresses.
5616 */
5617static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
5618 char __user *optval, int __user *optlen)
5619{
5620 struct sctp_prim prim;
5621 struct sctp_association *asoc;
5622 struct sctp_sock *sp = sctp_sk(sk);
5623
Neil Horman408f22e2007-06-16 14:03:45 -04005624 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 return -EINVAL;
5626
Neil Horman408f22e2007-06-16 14:03:45 -04005627 len = sizeof(struct sctp_prim);
5628
5629 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 return -EFAULT;
5631
5632 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
5633 if (!asoc)
5634 return -EINVAL;
5635
5636 if (!asoc->peer.primary_path)
5637 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005638
Al Viro8cec6b82006-11-20 17:23:01 -08005639 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
5640 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005642 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 (union sctp_addr *)&prim.ssp_addr);
5644
Neil Horman408f22e2007-06-16 14:03:45 -04005645 if (put_user(len, optlen))
5646 return -EFAULT;
5647 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 return -EFAULT;
5649
5650 return 0;
5651}
5652
5653/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005654 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005656 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 * Indication parameter for all future INIT and INIT-ACK exchanges.
5658 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005659static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 char __user *optval, int __user *optlen)
5661{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005662 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663
Neil Horman408f22e2007-06-16 14:03:45 -04005664 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 return -EINVAL;
5666
Neil Horman408f22e2007-06-16 14:03:45 -04005667 len = sizeof(struct sctp_setadaptation);
5668
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005669 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04005670
5671 if (put_user(len, optlen))
5672 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005673 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07005675
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 return 0;
5677}
5678
5679/*
5680 *
5681 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
5682 *
5683 * Applications that wish to use the sendto() system call may wish to
5684 * specify a default set of parameters that would normally be supplied
5685 * through the inclusion of ancillary data. This socket option allows
5686 * such an application to set the default sctp_sndrcvinfo structure.
5687
5688
5689 * The application that wishes to use this socket option simply passes
5690 * in to this call the sctp_sndrcvinfo structure defined in Section
5691 * 5.2.2) The input parameters accepted by this call include
5692 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
5693 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
5694 * to this call if the caller is using the UDP model.
5695 *
5696 * For getsockopt, it get the default sctp_sndrcvinfo structure.
5697 */
5698static int sctp_getsockopt_default_send_param(struct sock *sk,
5699 int len, char __user *optval,
5700 int __user *optlen)
5701{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005703 struct sctp_association *asoc;
5704 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005706 if (len < sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005708
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005709 len = sizeof(info);
Neil Horman408f22e2007-06-16 14:03:45 -04005710
5711 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 return -EFAULT;
5713
5714 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
5715 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
5716 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 if (asoc) {
5718 info.sinfo_stream = asoc->default_stream;
5719 info.sinfo_flags = asoc->default_flags;
5720 info.sinfo_ppid = asoc->default_ppid;
5721 info.sinfo_context = asoc->default_context;
5722 info.sinfo_timetolive = asoc->default_timetolive;
5723 } else {
5724 info.sinfo_stream = sp->default_stream;
5725 info.sinfo_flags = sp->default_flags;
5726 info.sinfo_ppid = sp->default_ppid;
5727 info.sinfo_context = sp->default_context;
5728 info.sinfo_timetolive = sp->default_timetolive;
5729 }
5730
Neil Horman408f22e2007-06-16 14:03:45 -04005731 if (put_user(len, optlen))
5732 return -EFAULT;
5733 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 return -EFAULT;
5735
5736 return 0;
5737}
5738
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005739/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
5740 * (SCTP_DEFAULT_SNDINFO)
5741 */
5742static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
5743 char __user *optval,
5744 int __user *optlen)
5745{
5746 struct sctp_sock *sp = sctp_sk(sk);
5747 struct sctp_association *asoc;
5748 struct sctp_sndinfo info;
5749
5750 if (len < sizeof(info))
5751 return -EINVAL;
5752
5753 len = sizeof(info);
5754
5755 if (copy_from_user(&info, optval, len))
5756 return -EFAULT;
5757
5758 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
5759 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
5760 return -EINVAL;
5761 if (asoc) {
5762 info.snd_sid = asoc->default_stream;
5763 info.snd_flags = asoc->default_flags;
5764 info.snd_ppid = asoc->default_ppid;
5765 info.snd_context = asoc->default_context;
5766 } else {
5767 info.snd_sid = sp->default_stream;
5768 info.snd_flags = sp->default_flags;
5769 info.snd_ppid = sp->default_ppid;
5770 info.snd_context = sp->default_context;
5771 }
5772
5773 if (put_user(len, optlen))
5774 return -EFAULT;
5775 if (copy_to_user(optval, &info, len))
5776 return -EFAULT;
5777
5778 return 0;
5779}
5780
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781/*
5782 *
5783 * 7.1.5 SCTP_NODELAY
5784 *
5785 * Turn on/off any Nagle-like algorithm. This means that packets are
5786 * generally sent as soon as possible and no unnecessary delays are
5787 * introduced, at the cost of more packets in the network. Expects an
5788 * integer boolean flag.
5789 */
5790
5791static int sctp_getsockopt_nodelay(struct sock *sk, int len,
5792 char __user *optval, int __user *optlen)
5793{
5794 int val;
5795
5796 if (len < sizeof(int))
5797 return -EINVAL;
5798
5799 len = sizeof(int);
5800 val = (sctp_sk(sk)->nodelay == 1);
5801 if (put_user(len, optlen))
5802 return -EFAULT;
5803 if (copy_to_user(optval, &val, len))
5804 return -EFAULT;
5805 return 0;
5806}
5807
5808/*
5809 *
5810 * 7.1.1 SCTP_RTOINFO
5811 *
5812 * The protocol parameters used to initialize and bound retransmission
5813 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
5814 * and modify these parameters.
5815 * All parameters are time values, in milliseconds. A value of 0, when
5816 * modifying the parameters, indicates that the current value should not
5817 * be changed.
5818 *
5819 */
5820static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
5821 char __user *optval,
5822 int __user *optlen) {
5823 struct sctp_rtoinfo rtoinfo;
5824 struct sctp_association *asoc;
5825
Neil Horman408f22e2007-06-16 14:03:45 -04005826 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 return -EINVAL;
5828
Neil Horman408f22e2007-06-16 14:03:45 -04005829 len = sizeof(struct sctp_rtoinfo);
5830
5831 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 return -EFAULT;
5833
5834 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
5835
5836 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
5837 return -EINVAL;
5838
5839 /* Values corresponding to the specific association. */
5840 if (asoc) {
5841 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
5842 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
5843 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
5844 } else {
5845 /* Values corresponding to the endpoint. */
5846 struct sctp_sock *sp = sctp_sk(sk);
5847
5848 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
5849 rtoinfo.srto_max = sp->rtoinfo.srto_max;
5850 rtoinfo.srto_min = sp->rtoinfo.srto_min;
5851 }
5852
5853 if (put_user(len, optlen))
5854 return -EFAULT;
5855
5856 if (copy_to_user(optval, &rtoinfo, len))
5857 return -EFAULT;
5858
5859 return 0;
5860}
5861
5862/*
5863 *
5864 * 7.1.2 SCTP_ASSOCINFO
5865 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02005866 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 * of the association.
5868 * Returns an error if the new association retransmission value is
5869 * greater than the sum of the retransmission value of the peer.
5870 * See [SCTP] for more information.
5871 *
5872 */
5873static int sctp_getsockopt_associnfo(struct sock *sk, int len,
5874 char __user *optval,
5875 int __user *optlen)
5876{
5877
5878 struct sctp_assocparams assocparams;
5879 struct sctp_association *asoc;
5880 struct list_head *pos;
5881 int cnt = 0;
5882
Neil Horman408f22e2007-06-16 14:03:45 -04005883 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 return -EINVAL;
5885
Neil Horman408f22e2007-06-16 14:03:45 -04005886 len = sizeof(struct sctp_assocparams);
5887
5888 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 return -EFAULT;
5890
5891 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
5892
5893 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
5894 return -EINVAL;
5895
5896 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07005897 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
5899 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
5900 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
Daniel Borkmann52db8822013-06-25 18:17:27 +02005901 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
5903 list_for_each(pos, &asoc->peer.transport_addr_list) {
wangweidongcb3f8372013-12-23 12:16:50 +08005904 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 }
5906
5907 assocparams.sasoc_number_peer_destinations = cnt;
5908 } else {
5909 /* Values corresponding to the endpoint */
5910 struct sctp_sock *sp = sctp_sk(sk);
5911
5912 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
5913 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
5914 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
5915 assocparams.sasoc_cookie_life =
5916 sp->assocparams.sasoc_cookie_life;
5917 assocparams.sasoc_number_peer_destinations =
5918 sp->assocparams.
5919 sasoc_number_peer_destinations;
5920 }
5921
5922 if (put_user(len, optlen))
5923 return -EFAULT;
5924
5925 if (copy_to_user(optval, &assocparams, len))
5926 return -EFAULT;
5927
5928 return 0;
5929}
5930
5931/*
5932 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
5933 *
5934 * This socket option is a boolean flag which turns on or off mapped V4
5935 * addresses. If this option is turned on and the socket is type
5936 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
5937 * If this option is turned off, then no mapping will be done of V4
5938 * addresses and a user will receive both PF_INET6 and PF_INET type
5939 * addresses on the socket.
5940 */
5941static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
5942 char __user *optval, int __user *optlen)
5943{
5944 int val;
5945 struct sctp_sock *sp = sctp_sk(sk);
5946
5947 if (len < sizeof(int))
5948 return -EINVAL;
5949
5950 len = sizeof(int);
5951 val = sp->v4mapped;
5952 if (put_user(len, optlen))
5953 return -EFAULT;
5954 if (copy_to_user(optval, &val, len))
5955 return -EFAULT;
5956
5957 return 0;
5958}
5959
5960/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005961 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
5962 * (chapter and verse is quoted at sctp_setsockopt_context())
5963 */
5964static int sctp_getsockopt_context(struct sock *sk, int len,
5965 char __user *optval, int __user *optlen)
5966{
5967 struct sctp_assoc_value params;
5968 struct sctp_sock *sp;
5969 struct sctp_association *asoc;
5970
Neil Horman408f22e2007-06-16 14:03:45 -04005971 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005972 return -EINVAL;
5973
Neil Horman408f22e2007-06-16 14:03:45 -04005974 len = sizeof(struct sctp_assoc_value);
5975
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005976 if (copy_from_user(&params, optval, len))
5977 return -EFAULT;
5978
5979 sp = sctp_sk(sk);
5980
5981 if (params.assoc_id != 0) {
5982 asoc = sctp_id2assoc(sk, params.assoc_id);
5983 if (!asoc)
5984 return -EINVAL;
5985 params.assoc_value = asoc->default_rcv_context;
5986 } else {
5987 params.assoc_value = sp->default_rcv_context;
5988 }
5989
5990 if (put_user(len, optlen))
5991 return -EFAULT;
5992 if (copy_to_user(optval, &params, len))
5993 return -EFAULT;
5994
5995 return 0;
5996}
5997
5998/*
Wei Yongjune89c2092008-12-25 16:54:58 -08005999 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6000 * This option will get or set the maximum size to put in any outgoing
6001 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 * fragmented by SCTP into the specified size. Note that the underlying
6003 * SCTP implementation may fragment into smaller sized chunks when the
6004 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08006005 * the user. The default value for this option is '0' which indicates
6006 * the user is NOT limiting fragmentation and only the PMTU will effect
6007 * SCTP's choice of DATA chunk size. Note also that values set larger
6008 * than the maximum size of an IP datagram will effectively let SCTP
6009 * control fragmentation (i.e. the same as setting this option to 0).
6010 *
6011 * The following structure is used to access and modify this parameter:
6012 *
6013 * struct sctp_assoc_value {
6014 * sctp_assoc_t assoc_id;
6015 * uint32_t assoc_value;
6016 * };
6017 *
6018 * assoc_id: This parameter is ignored for one-to-one style sockets.
6019 * For one-to-many style sockets this parameter indicates which
6020 * association the user is performing an action upon. Note that if
6021 * this field's value is zero then the endpoints default value is
6022 * changed (effecting future associations only).
6023 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 */
6025static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6026 char __user *optval, int __user *optlen)
6027{
Wei Yongjune89c2092008-12-25 16:54:58 -08006028 struct sctp_assoc_value params;
6029 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
Wei Yongjune89c2092008-12-25 16:54:58 -08006031 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006032 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006033 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006034 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006035 "Use struct sctp_assoc_value instead\n",
6036 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08006037 params.assoc_id = 0;
6038 } else if (len >= sizeof(struct sctp_assoc_value)) {
6039 len = sizeof(struct sctp_assoc_value);
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006040 if (copy_from_user(&params, optval, len))
Wei Yongjune89c2092008-12-25 16:54:58 -08006041 return -EFAULT;
6042 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 return -EINVAL;
6044
Wei Yongjune89c2092008-12-25 16:54:58 -08006045 asoc = sctp_id2assoc(sk, params.assoc_id);
6046 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
6047 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
Wei Yongjune89c2092008-12-25 16:54:58 -08006049 if (asoc)
6050 params.assoc_value = asoc->frag_point;
6051 else
6052 params.assoc_value = sctp_sk(sk)->user_frag;
6053
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 if (put_user(len, optlen))
6055 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08006056 if (len == sizeof(int)) {
6057 if (copy_to_user(optval, &params.assoc_value, len))
6058 return -EFAULT;
6059 } else {
6060 if (copy_to_user(optval, &params, len))
6061 return -EFAULT;
6062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063
6064 return 0;
6065}
6066
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006067/*
6068 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6069 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6070 */
6071static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6072 char __user *optval, int __user *optlen)
6073{
6074 int val;
6075
6076 if (len < sizeof(int))
6077 return -EINVAL;
6078
6079 len = sizeof(int);
6080
6081 val = sctp_sk(sk)->frag_interleave;
6082 if (put_user(len, optlen))
6083 return -EFAULT;
6084 if (copy_to_user(optval, &val, len))
6085 return -EFAULT;
6086
6087 return 0;
6088}
6089
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006090/*
6091 * 7.1.25. Set or Get the sctp partial delivery point
6092 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6093 */
6094static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6095 char __user *optval,
6096 int __user *optlen)
6097{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09006098 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006099
6100 if (len < sizeof(u32))
6101 return -EINVAL;
6102
6103 len = sizeof(u32);
6104
6105 val = sctp_sk(sk)->pd_point;
6106 if (put_user(len, optlen))
6107 return -EFAULT;
6108 if (copy_to_user(optval, &val, len))
6109 return -EFAULT;
6110
Wei Yongjun7d743b72010-12-14 16:10:41 +00006111 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006112}
6113
Vlad Yasevich70331572007-03-23 11:34:36 -07006114/*
6115 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6116 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6117 */
6118static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6119 char __user *optval,
6120 int __user *optlen)
6121{
Neil Horman219b99a2008-03-05 13:44:46 -08006122 struct sctp_assoc_value params;
6123 struct sctp_sock *sp;
6124 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07006125
Neil Horman219b99a2008-03-05 13:44:46 -08006126 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006127 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006128 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006129 "Use of int in max_burst socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006130 "Use struct sctp_assoc_value instead\n",
6131 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08006132 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00006133 } else if (len >= sizeof(struct sctp_assoc_value)) {
6134 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08006135 if (copy_from_user(&params, optval, len))
6136 return -EFAULT;
6137 } else
6138 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07006139
Neil Horman219b99a2008-03-05 13:44:46 -08006140 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07006141
Neil Horman219b99a2008-03-05 13:44:46 -08006142 if (params.assoc_id != 0) {
6143 asoc = sctp_id2assoc(sk, params.assoc_id);
6144 if (!asoc)
6145 return -EINVAL;
6146 params.assoc_value = asoc->max_burst;
6147 } else
6148 params.assoc_value = sp->max_burst;
6149
6150 if (len == sizeof(int)) {
6151 if (copy_to_user(optval, &params.assoc_value, len))
6152 return -EFAULT;
6153 } else {
6154 if (copy_to_user(optval, &params, len))
6155 return -EFAULT;
6156 }
6157
6158 return 0;
6159
Vlad Yasevich70331572007-03-23 11:34:36 -07006160}
6161
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006162static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6163 char __user *optval, int __user *optlen)
6164{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006165 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006166 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006167 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006168 __u16 data_len = 0;
6169 u32 num_idents;
Xin Long7a84bd42016-02-03 23:33:30 +08006170 int i;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006171
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006172 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006173 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006174
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006175 hmacs = ep->auth_hmacs_list;
Xin Long3c918702017-06-30 11:52:16 +08006176 data_len = ntohs(hmacs->param_hdr.length) -
6177 sizeof(struct sctp_paramhdr);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006178
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006179 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006180 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006181
6182 len = sizeof(struct sctp_hmacalgo) + data_len;
6183 num_idents = data_len / sizeof(u16);
6184
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006185 if (put_user(len, optlen))
6186 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006187 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006188 return -EFAULT;
Xin Long7a84bd42016-02-03 23:33:30 +08006189 for (i = 0; i < num_idents; i++) {
6190 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6191
6192 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6193 return -EFAULT;
6194 }
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006195 return 0;
6196}
6197
6198static int sctp_getsockopt_active_key(struct sock *sk, int len,
6199 char __user *optval, int __user *optlen)
6200{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006201 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006202 struct sctp_authkeyid val;
6203 struct sctp_association *asoc;
6204
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006205 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006206 return -EACCES;
6207
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006208 if (len < sizeof(struct sctp_authkeyid))
6209 return -EINVAL;
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006210
6211 len = sizeof(struct sctp_authkeyid);
6212 if (copy_from_user(&val, optval, len))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006213 return -EFAULT;
6214
6215 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6216 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6217 return -EINVAL;
6218
6219 if (asoc)
6220 val.scact_keynumber = asoc->active_key_id;
6221 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006222 val.scact_keynumber = ep->active_key_id;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006223
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006224 if (put_user(len, optlen))
6225 return -EFAULT;
6226 if (copy_to_user(optval, &val, len))
6227 return -EFAULT;
6228
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006229 return 0;
6230}
6231
6232static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6233 char __user *optval, int __user *optlen)
6234{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006235 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006236 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006237 struct sctp_authchunks val;
6238 struct sctp_association *asoc;
6239 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006240 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006241 char __user *to;
6242
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006243 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006244 return -EACCES;
6245
6246 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006247 return -EINVAL;
6248
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006249 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006250 return -EFAULT;
6251
Al Viro411223c2007-10-14 19:21:20 +01006252 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006253 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6254 if (!asoc)
6255 return -EINVAL;
6256
6257 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006258 if (!ch)
6259 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006260
6261 /* See if the user provided enough room for all the data */
Xin Long3c918702017-06-30 11:52:16 +08006262 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevichb40db682008-02-27 14:40:37 -05006263 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006264 return -EINVAL;
6265
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006266 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006267 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006268num:
6269 len = sizeof(struct sctp_authchunks) + num_chunks;
wangweidong8d726512013-12-23 12:16:53 +08006270 if (put_user(len, optlen))
6271 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006272 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6273 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006274 return 0;
6275}
6276
6277static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6278 char __user *optval, int __user *optlen)
6279{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006280 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006281 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006282 struct sctp_authchunks val;
6283 struct sctp_association *asoc;
6284 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006285 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006286 char __user *to;
6287
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006288 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006289 return -EACCES;
6290
6291 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006292 return -EINVAL;
6293
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006294 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006295 return -EFAULT;
6296
Al Viro411223c2007-10-14 19:21:20 +01006297 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006298 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6299 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6300 return -EINVAL;
6301
6302 if (asoc)
wangweidong26ac8e52013-12-23 12:16:51 +08006303 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006304 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006305 ch = ep->auth_chunk_list;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006306
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006307 if (!ch)
6308 goto num;
6309
Xin Long3c918702017-06-30 11:52:16 +08006310 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006311 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006312 return -EINVAL;
6313
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006314 if (copy_to_user(to, ch->chunks, num_chunks))
6315 return -EFAULT;
6316num:
6317 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006318 if (put_user(len, optlen))
6319 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006320 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6321 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006322
6323 return 0;
6324}
6325
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08006326/*
6327 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6328 * This option gets the current number of associations that are attached
6329 * to a one-to-many style socket. The option value is an uint32_t.
6330 */
6331static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6332 char __user *optval, int __user *optlen)
6333{
6334 struct sctp_sock *sp = sctp_sk(sk);
6335 struct sctp_association *asoc;
6336 u32 val = 0;
6337
6338 if (sctp_style(sk, TCP))
6339 return -EOPNOTSUPP;
6340
6341 if (len < sizeof(u32))
6342 return -EINVAL;
6343
6344 len = sizeof(u32);
6345
6346 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6347 val++;
6348 }
6349
6350 if (put_user(len, optlen))
6351 return -EFAULT;
6352 if (copy_to_user(optval, &val, len))
6353 return -EFAULT;
6354
6355 return 0;
6356}
6357
Wei Yongjun209ba422011-04-17 17:27:08 +00006358/*
Michio Honda7dc04d72011-04-26 20:16:31 +09006359 * 8.1.23 SCTP_AUTO_ASCONF
6360 * See the corresponding setsockopt entry as description
6361 */
6362static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6363 char __user *optval, int __user *optlen)
6364{
6365 int val = 0;
6366
6367 if (len < sizeof(int))
6368 return -EINVAL;
6369
6370 len = sizeof(int);
6371 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6372 val = 1;
6373 if (put_user(len, optlen))
6374 return -EFAULT;
6375 if (copy_to_user(optval, &val, len))
6376 return -EFAULT;
6377 return 0;
6378}
6379
6380/*
Wei Yongjun209ba422011-04-17 17:27:08 +00006381 * 8.2.6. Get the Current Identifiers of Associations
6382 * (SCTP_GET_ASSOC_ID_LIST)
6383 *
6384 * This option gets the current list of SCTP association identifiers of
6385 * the SCTP associations handled by a one-to-many style socket.
6386 */
6387static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6388 char __user *optval, int __user *optlen)
6389{
6390 struct sctp_sock *sp = sctp_sk(sk);
6391 struct sctp_association *asoc;
6392 struct sctp_assoc_ids *ids;
6393 u32 num = 0;
6394
6395 if (sctp_style(sk, TCP))
6396 return -EOPNOTSUPP;
6397
6398 if (len < sizeof(struct sctp_assoc_ids))
6399 return -EINVAL;
6400
6401 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6402 num++;
6403 }
6404
6405 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6406 return -EINVAL;
6407
6408 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6409
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02006410 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
Wei Yongjun209ba422011-04-17 17:27:08 +00006411 if (unlikely(!ids))
6412 return -ENOMEM;
6413
6414 ids->gaids_number_of_ids = num;
6415 num = 0;
6416 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6417 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6418 }
6419
6420 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6421 kfree(ids);
6422 return -EFAULT;
6423 }
6424
6425 kfree(ids);
6426 return 0;
6427}
6428
Neil Horman5aa93bc2012-07-21 07:56:07 +00006429/*
6430 * SCTP_PEER_ADDR_THLDS
6431 *
6432 * This option allows us to fetch the partially failed threshold for one or all
6433 * transports in an association. See Section 6.1 of:
6434 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6435 */
6436static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6437 char __user *optval,
6438 int len,
6439 int __user *optlen)
6440{
6441 struct sctp_paddrthlds val;
6442 struct sctp_transport *trans;
6443 struct sctp_association *asoc;
6444
6445 if (len < sizeof(struct sctp_paddrthlds))
6446 return -EINVAL;
6447 len = sizeof(struct sctp_paddrthlds);
6448 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6449 return -EFAULT;
6450
6451 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6452 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6453 if (!asoc)
6454 return -ENOENT;
6455
6456 val.spt_pathpfthld = asoc->pf_retrans;
6457 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6458 } else {
6459 trans = sctp_addr_id2transport(sk, &val.spt_address,
6460 val.spt_assoc_id);
6461 if (!trans)
6462 return -ENOENT;
6463
6464 val.spt_pathmaxrxt = trans->pathmaxrxt;
6465 val.spt_pathpfthld = trans->pf_retrans;
6466 }
6467
6468 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6469 return -EFAULT;
6470
6471 return 0;
6472}
6473
Michele Baldessari196d6752012-12-01 04:49:42 +00006474/*
6475 * SCTP_GET_ASSOC_STATS
6476 *
6477 * This option retrieves local per endpoint statistics. It is modeled
6478 * after OpenSolaris' implementation
6479 */
6480static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6481 char __user *optval,
6482 int __user *optlen)
6483{
6484 struct sctp_assoc_stats sas;
6485 struct sctp_association *asoc = NULL;
6486
6487 /* User must provide at least the assoc id */
6488 if (len < sizeof(sctp_assoc_t))
6489 return -EINVAL;
6490
Guenter Roeck726bc6b2013-02-27 10:57:31 +00006491 /* Allow the struct to grow and fill in as much as possible */
6492 len = min_t(size_t, len, sizeof(sas));
6493
Michele Baldessari196d6752012-12-01 04:49:42 +00006494 if (copy_from_user(&sas, optval, len))
6495 return -EFAULT;
6496
6497 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6498 if (!asoc)
6499 return -EINVAL;
6500
6501 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6502 sas.sas_gapcnt = asoc->stats.gapcnt;
6503 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6504 sas.sas_osacks = asoc->stats.osacks;
6505 sas.sas_isacks = asoc->stats.isacks;
6506 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6507 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6508 sas.sas_oodchunks = asoc->stats.oodchunks;
6509 sas.sas_iodchunks = asoc->stats.iodchunks;
6510 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6511 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6512 sas.sas_idupchunks = asoc->stats.idupchunks;
6513 sas.sas_opackets = asoc->stats.opackets;
6514 sas.sas_ipackets = asoc->stats.ipackets;
6515
6516 /* New high max rto observed, will return 0 if not a single
6517 * RTO update took place. obs_rto_ipaddr will be bogus
6518 * in such a case
6519 */
6520 sas.sas_maxrto = asoc->stats.max_obs_rto;
6521 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6522 sizeof(struct sockaddr_storage));
6523
6524 /* Mark beginning of a new observation period */
6525 asoc->stats.max_obs_rto = asoc->rto_min;
6526
Michele Baldessari196d6752012-12-01 04:49:42 +00006527 if (put_user(len, optlen))
6528 return -EFAULT;
6529
Daniel Borkmannbb333812013-06-28 19:49:40 +02006530 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
Michele Baldessari196d6752012-12-01 04:49:42 +00006531
6532 if (copy_to_user(optval, &sas, len))
6533 return -EFAULT;
6534
6535 return 0;
6536}
6537
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02006538static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6539 char __user *optval,
6540 int __user *optlen)
6541{
6542 int val = 0;
6543
6544 if (len < sizeof(int))
6545 return -EINVAL;
6546
6547 len = sizeof(int);
6548 if (sctp_sk(sk)->recvrcvinfo)
6549 val = 1;
6550 if (put_user(len, optlen))
6551 return -EFAULT;
6552 if (copy_to_user(optval, &val, len))
6553 return -EFAULT;
6554
6555 return 0;
6556}
6557
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02006558static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6559 char __user *optval,
6560 int __user *optlen)
6561{
6562 int val = 0;
6563
6564 if (len < sizeof(int))
6565 return -EINVAL;
6566
6567 len = sizeof(int);
6568 if (sctp_sk(sk)->recvnxtinfo)
6569 val = 1;
6570 if (put_user(len, optlen))
6571 return -EFAULT;
6572 if (copy_to_user(optval, &val, len))
6573 return -EFAULT;
6574
6575 return 0;
6576}
6577
Xin Long28aa4c22016-07-09 19:47:40 +08006578static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6579 char __user *optval,
6580 int __user *optlen)
6581{
6582 struct sctp_assoc_value params;
6583 struct sctp_association *asoc;
6584 int retval = -EFAULT;
6585
6586 if (len < sizeof(params)) {
6587 retval = -EINVAL;
6588 goto out;
6589 }
6590
6591 len = sizeof(params);
6592 if (copy_from_user(&params, optval, len))
6593 goto out;
6594
6595 asoc = sctp_id2assoc(sk, params.assoc_id);
6596 if (asoc) {
6597 params.assoc_value = asoc->prsctp_enable;
6598 } else if (!params.assoc_id) {
6599 struct sctp_sock *sp = sctp_sk(sk);
6600
6601 params.assoc_value = sp->ep->prsctp_enable;
6602 } else {
6603 retval = -EINVAL;
6604 goto out;
6605 }
6606
6607 if (put_user(len, optlen))
6608 goto out;
6609
6610 if (copy_to_user(optval, &params, len))
6611 goto out;
6612
6613 retval = 0;
6614
6615out:
6616 return retval;
6617}
6618
Xin Longf959fb42016-07-09 19:47:41 +08006619static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
6620 char __user *optval,
6621 int __user *optlen)
6622{
6623 struct sctp_default_prinfo info;
6624 struct sctp_association *asoc;
6625 int retval = -EFAULT;
6626
6627 if (len < sizeof(info)) {
6628 retval = -EINVAL;
6629 goto out;
6630 }
6631
6632 len = sizeof(info);
6633 if (copy_from_user(&info, optval, len))
6634 goto out;
6635
6636 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
6637 if (asoc) {
6638 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
6639 info.pr_value = asoc->default_timetolive;
6640 } else if (!info.pr_assoc_id) {
6641 struct sctp_sock *sp = sctp_sk(sk);
6642
6643 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
6644 info.pr_value = sp->default_timetolive;
6645 } else {
6646 retval = -EINVAL;
6647 goto out;
6648 }
6649
6650 if (put_user(len, optlen))
6651 goto out;
6652
6653 if (copy_to_user(optval, &info, len))
6654 goto out;
6655
6656 retval = 0;
6657
6658out:
6659 return retval;
6660}
6661
Xin Long826d2532016-07-09 19:47:42 +08006662static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
6663 char __user *optval,
6664 int __user *optlen)
6665{
6666 struct sctp_prstatus params;
6667 struct sctp_association *asoc;
6668 int policy;
6669 int retval = -EINVAL;
6670
6671 if (len < sizeof(params))
6672 goto out;
6673
6674 len = sizeof(params);
6675 if (copy_from_user(&params, optval, len)) {
6676 retval = -EFAULT;
6677 goto out;
6678 }
6679
6680 policy = params.sprstat_policy;
6681 if (policy & ~SCTP_PR_SCTP_MASK)
6682 goto out;
6683
6684 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
6685 if (!asoc)
6686 goto out;
6687
6688 if (policy == SCTP_PR_SCTP_NONE) {
6689 params.sprstat_abandoned_unsent = 0;
6690 params.sprstat_abandoned_sent = 0;
6691 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6692 params.sprstat_abandoned_unsent +=
6693 asoc->abandoned_unsent[policy];
6694 params.sprstat_abandoned_sent +=
6695 asoc->abandoned_sent[policy];
6696 }
6697 } else {
6698 params.sprstat_abandoned_unsent =
6699 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
6700 params.sprstat_abandoned_sent =
6701 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
6702 }
6703
6704 if (put_user(len, optlen)) {
6705 retval = -EFAULT;
6706 goto out;
6707 }
6708
6709 if (copy_to_user(optval, &params, len)) {
6710 retval = -EFAULT;
6711 goto out;
6712 }
6713
6714 retval = 0;
6715
6716out:
6717 return retval;
6718}
6719
Xin Longd229d482017-04-01 17:07:46 +08006720static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
6721 char __user *optval,
6722 int __user *optlen)
6723{
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006724 struct sctp_stream_out_ext *streamoute;
Xin Longd229d482017-04-01 17:07:46 +08006725 struct sctp_association *asoc;
6726 struct sctp_prstatus params;
6727 int retval = -EINVAL;
6728 int policy;
6729
6730 if (len < sizeof(params))
6731 goto out;
6732
6733 len = sizeof(params);
6734 if (copy_from_user(&params, optval, len)) {
6735 retval = -EFAULT;
6736 goto out;
6737 }
6738
6739 policy = params.sprstat_policy;
6740 if (policy & ~SCTP_PR_SCTP_MASK)
6741 goto out;
6742
6743 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
Xin Longcee360a2017-05-31 16:36:31 +08006744 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
Xin Longd229d482017-04-01 17:07:46 +08006745 goto out;
6746
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006747 streamoute = asoc->stream.out[params.sprstat_sid].ext;
6748 if (!streamoute) {
6749 /* Not allocated yet, means all stats are 0 */
6750 params.sprstat_abandoned_unsent = 0;
6751 params.sprstat_abandoned_sent = 0;
6752 retval = 0;
6753 goto out;
6754 }
6755
Xin Longd229d482017-04-01 17:07:46 +08006756 if (policy == SCTP_PR_SCTP_NONE) {
6757 params.sprstat_abandoned_unsent = 0;
6758 params.sprstat_abandoned_sent = 0;
6759 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6760 params.sprstat_abandoned_unsent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006761 streamoute->abandoned_unsent[policy];
Xin Longd229d482017-04-01 17:07:46 +08006762 params.sprstat_abandoned_sent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006763 streamoute->abandoned_sent[policy];
Xin Longd229d482017-04-01 17:07:46 +08006764 }
6765 } else {
6766 params.sprstat_abandoned_unsent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006767 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08006768 params.sprstat_abandoned_sent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006769 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08006770 }
6771
6772 if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
6773 retval = -EFAULT;
6774 goto out;
6775 }
6776
6777 retval = 0;
6778
6779out:
6780 return retval;
6781}
6782
Xin Longc0d8bab2017-03-10 12:11:12 +08006783static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
6784 char __user *optval,
6785 int __user *optlen)
6786{
6787 struct sctp_assoc_value params;
6788 struct sctp_association *asoc;
6789 int retval = -EFAULT;
6790
6791 if (len < sizeof(params)) {
6792 retval = -EINVAL;
6793 goto out;
6794 }
6795
6796 len = sizeof(params);
6797 if (copy_from_user(&params, optval, len))
6798 goto out;
6799
6800 asoc = sctp_id2assoc(sk, params.assoc_id);
6801 if (asoc) {
6802 params.assoc_value = asoc->reconf_enable;
6803 } else if (!params.assoc_id) {
6804 struct sctp_sock *sp = sctp_sk(sk);
6805
6806 params.assoc_value = sp->ep->reconf_enable;
6807 } else {
6808 retval = -EINVAL;
6809 goto out;
6810 }
6811
6812 if (put_user(len, optlen))
6813 goto out;
6814
6815 if (copy_to_user(optval, &params, len))
6816 goto out;
6817
6818 retval = 0;
6819
6820out:
6821 return retval;
6822}
6823
Xin Long9fb657a2017-01-18 00:44:46 +08006824static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
6825 char __user *optval,
6826 int __user *optlen)
6827{
6828 struct sctp_assoc_value params;
6829 struct sctp_association *asoc;
6830 int retval = -EFAULT;
6831
6832 if (len < sizeof(params)) {
6833 retval = -EINVAL;
6834 goto out;
6835 }
6836
6837 len = sizeof(params);
6838 if (copy_from_user(&params, optval, len))
6839 goto out;
6840
6841 asoc = sctp_id2assoc(sk, params.assoc_id);
6842 if (asoc) {
6843 params.assoc_value = asoc->strreset_enable;
6844 } else if (!params.assoc_id) {
6845 struct sctp_sock *sp = sctp_sk(sk);
6846
6847 params.assoc_value = sp->ep->strreset_enable;
6848 } else {
6849 retval = -EINVAL;
6850 goto out;
6851 }
6852
6853 if (put_user(len, optlen))
6854 goto out;
6855
6856 if (copy_to_user(optval, &params, len))
6857 goto out;
6858
6859 retval = 0;
6860
6861out:
6862 return retval;
6863}
6864
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03006865static int sctp_getsockopt_scheduler(struct sock *sk, int len,
6866 char __user *optval,
6867 int __user *optlen)
6868{
6869 struct sctp_assoc_value params;
6870 struct sctp_association *asoc;
6871 int retval = -EFAULT;
6872
6873 if (len < sizeof(params)) {
6874 retval = -EINVAL;
6875 goto out;
6876 }
6877
6878 len = sizeof(params);
6879 if (copy_from_user(&params, optval, len))
6880 goto out;
6881
6882 asoc = sctp_id2assoc(sk, params.assoc_id);
6883 if (!asoc) {
6884 retval = -EINVAL;
6885 goto out;
6886 }
6887
6888 params.assoc_value = sctp_sched_get_sched(asoc);
6889
6890 if (put_user(len, optlen))
6891 goto out;
6892
6893 if (copy_to_user(optval, &params, len))
6894 goto out;
6895
6896 retval = 0;
6897
6898out:
6899 return retval;
6900}
6901
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03006902static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
6903 char __user *optval,
6904 int __user *optlen)
6905{
6906 struct sctp_stream_value params;
6907 struct sctp_association *asoc;
6908 int retval = -EFAULT;
6909
6910 if (len < sizeof(params)) {
6911 retval = -EINVAL;
6912 goto out;
6913 }
6914
6915 len = sizeof(params);
6916 if (copy_from_user(&params, optval, len))
6917 goto out;
6918
6919 asoc = sctp_id2assoc(sk, params.assoc_id);
6920 if (!asoc) {
6921 retval = -EINVAL;
6922 goto out;
6923 }
6924
6925 retval = sctp_sched_get_value(asoc, params.stream_id,
6926 &params.stream_value);
6927 if (retval)
6928 goto out;
6929
6930 if (put_user(len, optlen)) {
6931 retval = -EFAULT;
6932 goto out;
6933 }
6934
6935 if (copy_to_user(optval, &params, len)) {
6936 retval = -EFAULT;
6937 goto out;
6938 }
6939
6940out:
6941 return retval;
6942}
6943
Xin Long772a5862017-12-08 21:03:58 +08006944static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
6945 char __user *optval,
6946 int __user *optlen)
6947{
6948 struct sctp_assoc_value params;
6949 struct sctp_association *asoc;
6950 int retval = -EFAULT;
6951
6952 if (len < sizeof(params)) {
6953 retval = -EINVAL;
6954 goto out;
6955 }
6956
6957 len = sizeof(params);
6958 if (copy_from_user(&params, optval, len))
6959 goto out;
6960
6961 asoc = sctp_id2assoc(sk, params.assoc_id);
6962 if (asoc) {
6963 params.assoc_value = asoc->intl_enable;
6964 } else if (!params.assoc_id) {
6965 struct sctp_sock *sp = sctp_sk(sk);
6966
6967 params.assoc_value = sp->strm_interleave;
6968 } else {
6969 retval = -EINVAL;
6970 goto out;
6971 }
6972
6973 if (put_user(len, optlen))
6974 goto out;
6975
6976 if (copy_to_user(optval, &params, len))
6977 goto out;
6978
6979 retval = 0;
6980
6981out:
6982 return retval;
6983}
6984
Daniel Borkmanndda91922013-06-17 11:40:05 +02006985static int sctp_getsockopt(struct sock *sk, int level, int optname,
6986 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987{
6988 int retval = 0;
6989 int len;
6990
Daniel Borkmannbb333812013-06-28 19:49:40 +02006991 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992
6993 /* I can hardly begin to describe how wrong this is. This is
6994 * so broken as to be worse than useless. The API draft
6995 * REALLY is NOT helpful here... I am not convinced that the
6996 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
6997 * are at all well-founded.
6998 */
6999 if (level != SOL_SCTP) {
7000 struct sctp_af *af = sctp_sk(sk)->pf->af;
7001
7002 retval = af->getsockopt(sk, level, optname, optval, optlen);
7003 return retval;
7004 }
7005
7006 if (get_user(len, optlen))
7007 return -EFAULT;
7008
Jiri Slabya4b8e712016-10-21 14:13:24 +02007009 if (len < 0)
7010 return -EINVAL;
7011
wangweidong048ed4b2014-01-21 15:44:11 +08007012 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013
7014 switch (optname) {
7015 case SCTP_STATUS:
7016 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7017 break;
7018 case SCTP_DISABLE_FRAGMENTS:
7019 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7020 optlen);
7021 break;
7022 case SCTP_EVENTS:
7023 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7024 break;
7025 case SCTP_AUTOCLOSE:
7026 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7027 break;
7028 case SCTP_SOCKOPT_PEELOFF:
7029 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7030 break;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04007031 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7032 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7033 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 case SCTP_PEER_ADDR_PARAMS:
7035 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7036 optlen);
7037 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00007038 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07007039 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08007040 optlen);
7041 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042 case SCTP_INITMSG:
7043 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7044 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 case SCTP_GET_PEER_ADDRS:
7046 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7047 optlen);
7048 break;
7049 case SCTP_GET_LOCAL_ADDRS:
7050 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7051 optlen);
7052 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04007053 case SCTP_SOCKOPT_CONNECTX3:
7054 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7055 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 case SCTP_DEFAULT_SEND_PARAM:
7057 retval = sctp_getsockopt_default_send_param(sk, len,
7058 optval, optlen);
7059 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02007060 case SCTP_DEFAULT_SNDINFO:
7061 retval = sctp_getsockopt_default_sndinfo(sk, len,
7062 optval, optlen);
7063 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 case SCTP_PRIMARY_ADDR:
7065 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7066 break;
7067 case SCTP_NODELAY:
7068 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7069 break;
7070 case SCTP_RTOINFO:
7071 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7072 break;
7073 case SCTP_ASSOCINFO:
7074 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7075 break;
7076 case SCTP_I_WANT_MAPPED_V4_ADDR:
7077 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7078 break;
7079 case SCTP_MAXSEG:
7080 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7081 break;
7082 case SCTP_GET_PEER_ADDR_INFO:
7083 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7084 optlen);
7085 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08007086 case SCTP_ADAPTATION_LAYER:
7087 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088 optlen);
7089 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08007090 case SCTP_CONTEXT:
7091 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7092 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007093 case SCTP_FRAGMENT_INTERLEAVE:
7094 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7095 optlen);
7096 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07007097 case SCTP_PARTIAL_DELIVERY_POINT:
7098 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7099 optlen);
7100 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07007101 case SCTP_MAX_BURST:
7102 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7103 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007104 case SCTP_AUTH_KEY:
7105 case SCTP_AUTH_CHUNK:
7106 case SCTP_AUTH_DELETE_KEY:
7107 retval = -EOPNOTSUPP;
7108 break;
7109 case SCTP_HMAC_IDENT:
7110 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7111 break;
7112 case SCTP_AUTH_ACTIVE_KEY:
7113 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7114 break;
7115 case SCTP_PEER_AUTH_CHUNKS:
7116 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7117 optlen);
7118 break;
7119 case SCTP_LOCAL_AUTH_CHUNKS:
7120 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7121 optlen);
7122 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08007123 case SCTP_GET_ASSOC_NUMBER:
7124 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7125 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00007126 case SCTP_GET_ASSOC_ID_LIST:
7127 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7128 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09007129 case SCTP_AUTO_ASCONF:
7130 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7131 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00007132 case SCTP_PEER_ADDR_THLDS:
7133 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7134 break;
Michele Baldessari196d6752012-12-01 04:49:42 +00007135 case SCTP_GET_ASSOC_STATS:
7136 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7137 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02007138 case SCTP_RECVRCVINFO:
7139 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7140 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007141 case SCTP_RECVNXTINFO:
7142 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7143 break;
Xin Long28aa4c22016-07-09 19:47:40 +08007144 case SCTP_PR_SUPPORTED:
7145 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7146 break;
Xin Longf959fb42016-07-09 19:47:41 +08007147 case SCTP_DEFAULT_PRINFO:
7148 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7149 optlen);
7150 break;
Xin Long826d2532016-07-09 19:47:42 +08007151 case SCTP_PR_ASSOC_STATUS:
7152 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7153 optlen);
7154 break;
Xin Longd229d482017-04-01 17:07:46 +08007155 case SCTP_PR_STREAM_STATUS:
7156 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7157 optlen);
7158 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08007159 case SCTP_RECONFIG_SUPPORTED:
7160 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7161 optlen);
7162 break;
Xin Long9fb657a2017-01-18 00:44:46 +08007163 case SCTP_ENABLE_STREAM_RESET:
7164 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7165 optlen);
7166 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007167 case SCTP_STREAM_SCHEDULER:
7168 retval = sctp_getsockopt_scheduler(sk, len, optval,
7169 optlen);
7170 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007171 case SCTP_STREAM_SCHEDULER_VALUE:
7172 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7173 optlen);
7174 break;
Xin Long772a5862017-12-08 21:03:58 +08007175 case SCTP_INTERLEAVING_SUPPORTED:
7176 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7177 optlen);
7178 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179 default:
7180 retval = -ENOPROTOOPT;
7181 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183
wangweidong048ed4b2014-01-21 15:44:11 +08007184 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 return retval;
7186}
7187
Craig Gallek086c6532016-02-10 11:50:35 -05007188static int sctp_hash(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189{
7190 /* STUB */
Craig Gallek086c6532016-02-10 11:50:35 -05007191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192}
7193
7194static void sctp_unhash(struct sock *sk)
7195{
7196 /* STUB */
7197}
7198
7199/* Check if port is acceptable. Possibly find first available port.
7200 *
7201 * The port hash table (contained in the 'global' SCTP protocol storage
7202 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7203 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7204 * list (the list number is the port number hashed out, so as you
7205 * would expect from a hash function, all the ports in a given list have
7206 * such a number that hashes out to the same list number; you were
7207 * expecting that, right?); so each list has a set of ports, with a
7208 * link to the socket (struct sock) that uses it, the port number and
7209 * a fastreuse flag (FIXME: NPI ipg).
7210 */
7211static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007212 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213
7214static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
7215{
7216 struct sctp_bind_hashbucket *head; /* hash list */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007217 struct sctp_bind_bucket *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 unsigned short snum;
7219 int ret;
7220
Al Viro04afd8b2006-11-20 17:02:01 -08007221 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222
Daniel Borkmannbb333812013-06-28 19:49:40 +02007223 pr_debug("%s: begins, snum:%d\n", __func__, snum);
7224
wangweidong79b91132014-01-21 15:44:07 +08007225 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226
7227 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07007228 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007229 int low, high, remaining, index;
7230 unsigned int rover;
WANG Cong122ff242014-05-12 16:04:53 -07007231 struct net *net = sock_net(sk);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007232
WANG Cong122ff242014-05-12 16:04:53 -07007233 inet_get_local_port_range(net, &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007234 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05007235 rover = prandom_u32() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 do {
7238 rover++;
7239 if ((rover < low) || (rover > high))
7240 rover = low;
WANG Cong122ff242014-05-12 16:04:53 -07007241 if (inet_is_local_reserved_port(net, rover))
Amerigo Wange3826f12010-05-05 00:27:06 +00007242 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007243 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244 head = &sctp_port_hashtable[index];
wangweidong3c8e43b2014-01-21 15:44:08 +08007245 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007246 sctp_for_each_hentry(pp, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007247 if ((pp->port == rover) &&
7248 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 goto next;
7250 break;
7251 next:
wangweidong3c8e43b2014-01-21 15:44:08 +08007252 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254
7255 /* Exhausted local port range during search? */
7256 ret = 1;
7257 if (remaining <= 0)
7258 goto fail;
7259
7260 /* OK, here is the one we will use. HEAD (the port
7261 * hash table list entry) is non-NULL and we hold it's
7262 * mutex.
7263 */
7264 snum = rover;
7265 } else {
7266 /* We are given an specific port number; we verify
7267 * that it is not being used. If it is used, we will
7268 * exahust the search in the hash list corresponding
7269 * to the port number (snum) - we detect that with the
7270 * port iterator, pp being NULL.
7271 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007272 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
wangweidong3c8e43b2014-01-21 15:44:08 +08007273 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007274 sctp_for_each_hentry(pp, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007275 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 goto pp_found;
7277 }
7278 }
7279 pp = NULL;
7280 goto pp_not_found;
7281pp_found:
7282 if (!hlist_empty(&pp->owner)) {
7283 /* We had a port hash table hit - there is an
7284 * available port (pp != NULL) and it is being
7285 * used by other socket (pp->owner not empty); that other
7286 * socket is going to be sk2.
7287 */
7288 int reuse = sk->sk_reuse;
7289 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290
Daniel Borkmannbb333812013-06-28 19:49:40 +02007291 pr_debug("%s: found a possible match\n", __func__);
7292
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007293 if (pp->fastreuse && sk->sk_reuse &&
7294 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295 goto success;
7296
7297 /* Run through the list of sockets bound to the port
7298 * (pp->port) [via the pointers bind_next and
7299 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
7300 * we get the endpoint they describe and run through
7301 * the endpoint's list of IP (v4 or v6) addresses,
7302 * comparing each of the addresses with the address of
7303 * the socket sk. If we find a match, then that means
7304 * that this port/socket (sk) combination are already
7305 * in an endpoint.
7306 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007307 sk_for_each_bound(sk2, &pp->owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 struct sctp_endpoint *ep2;
7309 ep2 = sctp_sk(sk2)->ep;
7310
Vlad Yasevich4e540642008-07-18 23:06:32 -07007311 if (sk == sk2 ||
7312 (reuse && sk2->sk_reuse &&
7313 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314 continue;
7315
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07007316 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
7317 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 ret = (long)sk2;
7319 goto fail_unlock;
7320 }
7321 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02007322
7323 pr_debug("%s: found a match\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324 }
7325pp_not_found:
7326 /* If there was a hash table miss, create a new port. */
7327 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007328 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329 goto fail_unlock;
7330
7331 /* In either case (hit or miss), make sure fastreuse is 1 only
7332 * if sk->sk_reuse is too (that is, if the caller requested
7333 * SO_REUSEADDR on this socket -sk-).
7334 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007335 if (hlist_empty(&pp->owner)) {
7336 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
7337 pp->fastreuse = 1;
7338 else
7339 pp->fastreuse = 0;
7340 } else if (pp->fastreuse &&
7341 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 pp->fastreuse = 0;
7343
7344 /* We are set, so fill up all the data in the hash table
7345 * entry, tie the socket list information with the rest of the
7346 * sockets FIXME: Blurry, NPI (ipg).
7347 */
7348success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007350 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351 sk_add_bind_node(sk, &pp->owner);
7352 sctp_sk(sk)->bind_hash = pp;
7353 }
7354 ret = 0;
7355
7356fail_unlock:
wangweidong3c8e43b2014-01-21 15:44:08 +08007357 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358
7359fail:
wangweidong79b91132014-01-21 15:44:07 +08007360 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361 return ret;
7362}
7363
7364/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
7365 * port is requested.
7366 */
7367static int sctp_get_port(struct sock *sk, unsigned short snum)
7368{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 union sctp_addr addr;
7370 struct sctp_af *af = sctp_sk(sk)->pf->af;
7371
7372 /* Set up a dummy address struct from the sk. */
7373 af->from_sk(&addr, sk);
7374 addr.v4.sin_port = htons(snum);
7375
7376 /* Note: sk->sk_num gets filled in if ephemeral port request. */
Daniel Borkmann62208f12013-06-25 18:17:30 +02007377 return !!sctp_get_port_local(sk, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378}
7379
7380/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 * Move a socket to LISTENING state.
7382 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02007383static int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007385 struct sctp_sock *sp = sctp_sk(sk);
7386 struct sctp_endpoint *ep = sp->ep;
Herbert Xu5821c762016-01-24 21:20:12 +08007387 struct crypto_shash *tfm = NULL;
Neil Horman3c681982012-10-24 09:20:03 +00007388 char alg[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389
7390 /* Allocate HMAC for generating cookie. */
Neil Horman3c681982012-10-24 09:20:03 +00007391 if (!sp->hmac && sp->sctp_hmac_alg) {
7392 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
Herbert Xu5821c762016-01-24 21:20:12 +08007393 tfm = crypto_alloc_shash(alg, 0, 0);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07007394 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00007395 net_info_ratelimited("failed to load transform for %s: %ld\n",
Neil Horman3c681982012-10-24 09:20:03 +00007396 sp->sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007397 return -ENOSYS;
7398 }
7399 sctp_sk(sk)->hmac = tfm;
7400 }
7401
7402 /*
7403 * If a bind() or sctp_bindx() is not called prior to a listen()
7404 * call that allows new associations to be accepted, the system
7405 * picks an ephemeral port and will choose an address set equivalent
7406 * to binding with a wildcard address.
7407 *
7408 * This is not currently spelled out in the SCTP sockets
7409 * extensions draft, but follows the practice as seen in TCP
7410 * sockets.
7411 *
7412 */
Yafang Shaocbabf462017-12-20 11:12:54 +08007413 inet_sk_set_state(sk, SCTP_SS_LISTENING);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007414 if (!ep->base.bind_addr.port) {
7415 if (sctp_autobind(sk))
7416 return -EAGAIN;
7417 } else {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007418 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08007419 inet_sk_set_state(sk, SCTP_SS_CLOSED);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007420 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 }
7422 }
7423
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007424 sk->sk_max_ack_backlog = backlog;
7425 sctp_hash_endpoint(ep);
7426 return 0;
7427}
7428
7429/*
7430 * 4.1.3 / 5.1.3 listen()
7431 *
7432 * By default, new associations are not accepted for UDP style sockets.
7433 * An application uses listen() to mark a socket as being able to
7434 * accept new associations.
7435 *
7436 * On TCP style sockets, applications use listen() to ready the SCTP
7437 * endpoint for accepting inbound associations.
7438 *
7439 * On both types of endpoints a backlog of '0' disables listening.
7440 *
7441 * Move a socket to LISTENING state.
7442 */
7443int sctp_inet_listen(struct socket *sock, int backlog)
7444{
7445 struct sock *sk = sock->sk;
7446 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7447 int err = -EINVAL;
7448
7449 if (unlikely(backlog < 0))
7450 return err;
7451
wangweidong048ed4b2014-01-21 15:44:11 +08007452 lock_sock(sk);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007453
7454 /* Peeled-off sockets are not allowed to listen(). */
7455 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
7456 goto out;
7457
7458 if (sock->state != SS_UNCONNECTED)
7459 goto out;
7460
Xin Long34b27892017-04-06 13:10:52 +08007461 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
7462 goto out;
7463
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007464 /* If backlog is zero, disable listening. */
7465 if (!backlog) {
7466 if (sctp_sstate(sk, CLOSED))
7467 goto out;
7468
7469 err = 0;
7470 sctp_unhash_endpoint(ep);
7471 sk->sk_state = SCTP_SS_CLOSED;
7472 if (sk->sk_reuse)
7473 sctp_sk(sk)->bind_hash->fastreuse = 1;
7474 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007475 }
7476
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007477 /* If we are already listening, just update the backlog */
7478 if (sctp_sstate(sk, LISTENING))
7479 sk->sk_max_ack_backlog = backlog;
7480 else {
7481 err = sctp_listen_start(sk, backlog);
7482 if (err)
7483 goto out;
7484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007486 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487out:
wangweidong048ed4b2014-01-21 15:44:11 +08007488 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490}
7491
7492/*
7493 * This function is done by modeling the current datagram_poll() and the
7494 * tcp_poll(). Note that, based on these implementations, we don't
7495 * lock the socket in this function, even though it seems that,
7496 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08007497 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498 * in this place. We assume that we don't need locks either until proven
7499 * otherwise.
7500 *
7501 * Another thing to note is that we include the Async I/O support
7502 * here, again, by modeling the current TCP/UDP code. We don't have
7503 * a good way to test with it yet.
7504 */
Al Viroade994f2017-07-03 00:01:49 -04007505__poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506{
7507 struct sock *sk = sock->sk;
7508 struct sctp_sock *sp = sctp_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -04007509 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510
Eric Dumazetaa395142010-04-20 13:03:51 +00007511 poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03007513 sock_rps_record_flow(sk);
7514
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 /* A TCP-style listening socket becomes readable when the accept queue
7516 * is not empty.
7517 */
7518 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
7519 return (!list_empty(&sp->ep->asocs)) ?
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007520 (EPOLLIN | EPOLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521
7522 mask = 0;
7523
7524 /* Is there any exceptional events? */
7525 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007526 mask |= EPOLLERR |
7527 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
Davide Libenzif348d702006-03-25 03:07:39 -08007528 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007529 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007531 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532
7533 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00007534 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007535 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536
7537 /* The association is either gone or not ready. */
7538 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
7539 return mask;
7540
7541 /* Is it writable? */
7542 if (sctp_writeable(sk)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007543 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 } else {
Eric Dumazet9cd3e072015-11-29 20:03:10 -08007545 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 /*
7547 * Since the socket is not locked, the buffer
7548 * might be made available after the writeable check and
7549 * before the bit is set. This could cause a lost I/O
7550 * signal. tcp_poll() has a race breaker for this race
7551 * condition. Based on their implementation, we put
7552 * in the following code to cover it as well.
7553 */
7554 if (sctp_writeable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007555 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556 }
7557 return mask;
7558}
7559
7560/********************************************************************
7561 * 2nd Level Abstractions
7562 ********************************************************************/
7563
7564static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007565 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566{
7567 struct sctp_bind_bucket *pp;
7568
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08007569 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07007571 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572 pp->port = snum;
7573 pp->fastreuse = 0;
7574 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007575 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007576 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577 }
7578 return pp;
7579}
7580
7581/* Caller must hold hashbucket lock for this tb with local BH disabled */
7582static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
7583{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07007584 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007585 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 kmem_cache_free(sctp_bucket_cachep, pp);
7587 SCTP_DBG_OBJCNT_DEC(bind_bucket);
7588 }
7589}
7590
7591/* Release this socket's reference to a local port. */
7592static inline void __sctp_put_port(struct sock *sk)
7593{
7594 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007595 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
7596 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597 struct sctp_bind_bucket *pp;
7598
wangweidong3c8e43b2014-01-21 15:44:08 +08007599 spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007600 pp = sctp_sk(sk)->bind_hash;
7601 __sk_del_bind_node(sk);
7602 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007603 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604 sctp_bucket_destroy(pp);
wangweidong3c8e43b2014-01-21 15:44:08 +08007605 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007606}
7607
7608void sctp_put_port(struct sock *sk)
7609{
wangweidong79b91132014-01-21 15:44:07 +08007610 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611 __sctp_put_port(sk);
wangweidong79b91132014-01-21 15:44:07 +08007612 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613}
7614
7615/*
7616 * The system picks an ephemeral port and choose an address set equivalent
7617 * to binding with a wildcard address.
7618 * One of those addresses will be the primary address for the association.
7619 * This automatically enables the multihoming capability of SCTP.
7620 */
7621static int sctp_autobind(struct sock *sk)
7622{
7623 union sctp_addr autoaddr;
7624 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08007625 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626
7627 /* Initialize a local sockaddr structure to INADDR_ANY. */
7628 af = sctp_sk(sk)->pf->af;
7629
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007630 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631 af->inaddr_any(&autoaddr, port);
7632
7633 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
7634}
7635
7636/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
7637 *
7638 * From RFC 2292
7639 * 4.2 The cmsghdr Structure *
7640 *
7641 * When ancillary data is sent or received, any number of ancillary data
7642 * objects can be specified by the msg_control and msg_controllen members of
7643 * the msghdr structure, because each object is preceded by
7644 * a cmsghdr structure defining the object's length (the cmsg_len member).
7645 * Historically Berkeley-derived implementations have passed only one object
7646 * at a time, but this API allows multiple objects to be
7647 * passed in a single call to sendmsg() or recvmsg(). The following example
7648 * shows two ancillary data objects in a control buffer.
7649 *
7650 * |<--------------------------- msg_controllen -------------------------->|
7651 * | |
7652 *
7653 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
7654 *
7655 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
7656 * | | |
7657 *
7658 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
7659 *
7660 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
7661 * | | | | |
7662 *
7663 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7664 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
7665 *
7666 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
7667 *
7668 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7669 * ^
7670 * |
7671 *
7672 * msg_control
7673 * points here
7674 */
Xin Longa05437a2017-08-11 10:23:48 +08007675static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676{
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007677 struct msghdr *my_msg = (struct msghdr *)msg;
Xin Longa05437a2017-08-11 10:23:48 +08007678 struct cmsghdr *cmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679
Gu Zhengf95b4142014-12-11 11:22:04 +08007680 for_each_cmsghdr(cmsg, my_msg) {
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007681 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682 return -EINVAL;
7683
7684 /* Should we parse this header or ignore? */
7685 if (cmsg->cmsg_level != IPPROTO_SCTP)
7686 continue;
7687
7688 /* Strictly check lengths following example in SCM code. */
7689 switch (cmsg->cmsg_type) {
7690 case SCTP_INIT:
7691 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007692 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693 *
7694 * This cmsghdr structure provides information for
7695 * initializing new SCTP associations with sendmsg().
7696 * The SCTP_INITMSG socket option uses this same data
7697 * structure. This structure is not used for
7698 * recvmsg().
7699 *
7700 * cmsg_level cmsg_type cmsg_data[]
7701 * ------------ ------------ ----------------------
7702 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
7703 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007704 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705 return -EINVAL;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007706
7707 cmsgs->init = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708 break;
7709
7710 case SCTP_SNDRCV:
7711 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007712 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713 *
7714 * This cmsghdr structure specifies SCTP options for
7715 * sendmsg() and describes SCTP header information
7716 * about a received message through recvmsg().
7717 *
7718 * cmsg_level cmsg_type cmsg_data[]
7719 * ------------ ------------ ----------------------
7720 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
7721 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007722 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723 return -EINVAL;
7724
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007725 cmsgs->srinfo = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007726
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007727 if (cmsgs->srinfo->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007728 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Longa6c2f792016-07-09 19:47:43 +08007729 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007730 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007731 return -EINVAL;
7732 break;
7733
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007734 case SCTP_SNDINFO:
7735 /* SCTP Socket API Extension
7736 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
7737 *
7738 * This cmsghdr structure specifies SCTP options for
7739 * sendmsg(). This structure and SCTP_RCVINFO replaces
7740 * SCTP_SNDRCV which has been deprecated.
7741 *
7742 * cmsg_level cmsg_type cmsg_data[]
7743 * ------------ ------------ ---------------------
7744 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
7745 */
7746 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
7747 return -EINVAL;
7748
7749 cmsgs->sinfo = CMSG_DATA(cmsg);
7750
7751 if (cmsgs->sinfo->snd_flags &
7752 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Longa6c2f792016-07-09 19:47:43 +08007753 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007754 SCTP_ABORT | SCTP_EOF))
7755 return -EINVAL;
7756 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757 default:
7758 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760 }
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007761
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762 return 0;
7763}
7764
7765/*
7766 * Wait for a packet..
7767 * Note: This function is the same function as in core/datagram.c
7768 * with a few modifications to make lksctp work.
7769 */
wangweidong26ac8e52013-12-23 12:16:51 +08007770static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771{
7772 int error;
7773 DEFINE_WAIT(wait);
7774
Eric Dumazetaa395142010-04-20 13:03:51 +00007775 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776
7777 /* Socket errors? */
7778 error = sock_error(sk);
7779 if (error)
7780 goto out;
7781
7782 if (!skb_queue_empty(&sk->sk_receive_queue))
7783 goto ready;
7784
7785 /* Socket shut down? */
7786 if (sk->sk_shutdown & RCV_SHUTDOWN)
7787 goto out;
7788
7789 /* Sequenced packets can come disconnected. If so we report the
7790 * problem.
7791 */
7792 error = -ENOTCONN;
7793
7794 /* Is there a good reason to think that we may receive some data? */
7795 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
7796 goto out;
7797
7798 /* Handle signals. */
7799 if (signal_pending(current))
7800 goto interrupted;
7801
7802 /* Let another process have a go. Since we are going to sleep
7803 * anyway. Note: This may cause odd behaviors if the message
7804 * does not fit in the user's buffer, but this seems to be the
7805 * only way to honor MSG_DONTWAIT realistically.
7806 */
wangweidong048ed4b2014-01-21 15:44:11 +08007807 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808 *timeo_p = schedule_timeout(*timeo_p);
wangweidong048ed4b2014-01-21 15:44:11 +08007809 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007810
7811ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00007812 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813 return 0;
7814
7815interrupted:
7816 error = sock_intr_errno(*timeo_p);
7817
7818out:
Eric Dumazetaa395142010-04-20 13:03:51 +00007819 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820 *err = error;
7821 return error;
7822}
7823
7824/* Receive a datagram.
7825 * Note: This is pretty much the same routine as in core/datagram.c
7826 * with a few changes to make lksctp work.
7827 */
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007828struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
7829 int noblock, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830{
7831 int error;
7832 struct sk_buff *skb;
7833 long timeo;
7834
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835 timeo = sock_rcvtimeo(sk, noblock);
7836
Daniel Borkmannbb333812013-06-28 19:49:40 +02007837 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
7838 MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839
7840 do {
7841 /* Again only user level code calls this function,
7842 * so nothing interrupt level
7843 * will suddenly eat the receive_queue.
7844 *
7845 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00007846 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847 */
7848 if (flags & MSG_PEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849 skb = skb_peek(&sk->sk_receive_queue);
7850 if (skb)
Reshetova, Elena63354792017-06-30 13:07:58 +03007851 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852 } else {
Marcelo Ricardo Leitner311b2172016-04-13 19:12:29 -03007853 skb = __skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854 }
7855
7856 if (skb)
7857 return skb;
7858
Neil Horman6736dc32005-12-02 20:30:06 -08007859 /* Caller is allowed not to check sk->sk_err before calling. */
7860 error = sock_error(sk);
7861 if (error)
7862 goto no_packet;
7863
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864 if (sk->sk_shutdown & RCV_SHUTDOWN)
7865 break;
7866
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07007867 if (sk_can_busy_loop(sk)) {
7868 sk_busy_loop(sk, noblock);
7869
7870 if (!skb_queue_empty(&sk->sk_receive_queue))
7871 continue;
7872 }
Neil Horman8465a5f2014-04-17 15:26:51 -04007873
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874 /* User doesn't want to wait. */
7875 error = -EAGAIN;
7876 if (!timeo)
7877 goto no_packet;
7878 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
7879
7880 return NULL;
7881
7882no_packet:
7883 *err = error;
7884 return NULL;
7885}
7886
7887/* If sndbuf has changed, wake up per association sndbuf waiters. */
7888static void __sctp_write_space(struct sctp_association *asoc)
7889{
7890 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891
Eric Dumazetceb5d582015-11-29 20:03:11 -08007892 if (sctp_wspace(asoc) <= 0)
7893 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894
Eric Dumazetceb5d582015-11-29 20:03:11 -08007895 if (waitqueue_active(&asoc->wait))
7896 wake_up_interruptible(&asoc->wait);
Eric Dumazeteaefd112011-02-18 03:26:36 +00007897
Eric Dumazetceb5d582015-11-29 20:03:11 -08007898 if (sctp_writeable(sk)) {
7899 struct socket_wq *wq;
7900
7901 rcu_read_lock();
7902 wq = rcu_dereference(sk->sk_wq);
7903 if (wq) {
7904 if (waitqueue_active(&wq->wait))
7905 wake_up_interruptible(&wq->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906
7907 /* Note that we try to include the Async I/O support
7908 * here by modeling from the current TCP/UDP code.
7909 * We have not tested with it yet.
7910 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00007911 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Eric Dumazetceb5d582015-11-29 20:03:11 -08007912 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08007914 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915 }
7916}
7917
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007918static void sctp_wake_up_waiters(struct sock *sk,
7919 struct sctp_association *asoc)
7920{
7921 struct sctp_association *tmp = asoc;
7922
7923 /* We do accounting for the sndbuf space per association,
7924 * so we only need to wake our own association.
7925 */
7926 if (asoc->ep->sndbuf_policy)
7927 return __sctp_write_space(asoc);
7928
Daniel Borkmann1e1cdf82014-04-09 16:10:20 +02007929 /* If association goes down and is just flushing its
7930 * outq, then just normally notify others.
7931 */
7932 if (asoc->base.dead)
7933 return sctp_write_space(sk);
7934
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007935 /* Accounting for the sndbuf space is per socket, so we
7936 * need to wake up others, try to be fair and in case of
7937 * other associations, let them have a go first instead
7938 * of just doing a sctp_write_space() call.
7939 *
7940 * Note that we reach sctp_wake_up_waiters() only when
7941 * associations free up queued chunks, thus we are under
7942 * lock and the list of associations on a socket is
7943 * guaranteed not to change.
7944 */
7945 for (tmp = list_next_entry(tmp, asocs); 1;
7946 tmp = list_next_entry(tmp, asocs)) {
7947 /* Manually skip the head element. */
7948 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
7949 continue;
7950 /* Wake up association. */
7951 __sctp_write_space(tmp);
7952 /* We've reached the end. */
7953 if (tmp == asoc)
7954 break;
7955 }
7956}
7957
Linus Torvalds1da177e2005-04-16 15:20:36 -07007958/* Do accounting for the sndbuf space.
7959 * Decrement the used sndbuf space of the corresponding association by the
7960 * data size which was just transmitted(freed).
7961 */
7962static void sctp_wfree(struct sk_buff *skb)
7963{
Daniel Borkmannf869c912014-11-20 01:54:48 +01007964 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
7965 struct sctp_association *asoc = chunk->asoc;
7966 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007967
Neil Horman4eb701d2005-04-28 12:02:04 -07007968 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
7969 sizeof(struct sk_buff) +
7970 sizeof(struct sctp_chunk);
7971
Reshetova, Elena14afee42017-06-30 13:08:00 +03007972 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
Neil Horman4eb701d2005-04-28 12:02:04 -07007973
Neil Horman4d93df02007-08-15 16:07:44 -07007974 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007975 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
Neil Horman4d93df02007-08-15 16:07:44 -07007976 */
7977 sk->sk_wmem_queued -= skb->truesize;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007978 sk_mem_uncharge(sk, skb->truesize);
Neil Horman4d93df02007-08-15 16:07:44 -07007979
Neil Horman4eb701d2005-04-28 12:02:04 -07007980 sock_wfree(skb);
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007981 sctp_wake_up_waiters(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982
7983 sctp_association_put(asoc);
7984}
7985
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07007986/* Do accounting for the receive space on the socket.
7987 * Accounting for the association is done in ulpevent.c
7988 * We set this as a destructor for the cloned data skbs so that
7989 * accounting is done at the correct time.
7990 */
7991void sctp_sock_rfree(struct sk_buff *skb)
7992{
7993 struct sock *sk = skb->sk;
7994 struct sctp_ulpevent *event = sctp_skb2event(skb);
7995
7996 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07007997
7998 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007999 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07008000 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008001 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008002}
8003
8004
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005/* Helper function to wait for space in the sndbuf. */
8006static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +08008007 size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008008{
8009 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008010 long current_timeo = *timeo_p;
8011 DEFINE_WAIT(wait);
Xin Longa0ff6602018-01-15 17:01:36 +08008012 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013
Daniel Borkmannbb333812013-06-28 19:49:40 +02008014 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8015 *timeo_p, msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008016
8017 /* Increment the association's refcnt. */
8018 sctp_association_hold(asoc);
8019
8020 /* Wait on the association specific sndbuf space. */
8021 for (;;) {
8022 prepare_to_wait_exclusive(&asoc->wait, &wait,
8023 TASK_INTERRUPTIBLE);
Xin Longca3af4d2017-11-15 16:55:54 +08008024 if (asoc->base.dead)
8025 goto do_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008026 if (!*timeo_p)
8027 goto do_nonblock;
Xin Longca3af4d2017-11-15 16:55:54 +08008028 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029 goto do_error;
8030 if (signal_pending(current))
8031 goto do_interrupted;
8032 if (msg_len <= sctp_wspace(asoc))
8033 break;
8034
8035 /* Let another process have a go. Since we are going
8036 * to sleep anyway.
8037 */
wangweidong048ed4b2014-01-21 15:44:11 +08008038 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008039 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008040 lock_sock(sk);
Xin Longa0ff6602018-01-15 17:01:36 +08008041 if (sk != asoc->base.sk)
8042 goto do_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008043
8044 *timeo_p = current_timeo;
8045 }
8046
8047out:
8048 finish_wait(&asoc->wait, &wait);
8049
8050 /* Release the association's refcnt. */
8051 sctp_association_put(asoc);
8052
8053 return err;
8054
Xin Longca3af4d2017-11-15 16:55:54 +08008055do_dead:
8056 err = -ESRCH;
8057 goto out;
8058
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059do_error:
8060 err = -EPIPE;
8061 goto out;
8062
8063do_interrupted:
8064 err = sock_intr_errno(*timeo_p);
8065 goto out;
8066
8067do_nonblock:
8068 err = -EAGAIN;
8069 goto out;
8070}
8071
David S. Miller676d2362014-04-11 16:15:36 -04008072void sctp_data_ready(struct sock *sk)
Wei Yongjun561b1732010-04-28 08:47:18 +00008073{
David S. Miller7ef52732010-05-02 21:43:40 -07008074 struct socket_wq *wq;
8075
8076 rcu_read_lock();
8077 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08008078 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008079 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8080 EPOLLRDNORM | EPOLLRDBAND);
Wei Yongjun561b1732010-04-28 08:47:18 +00008081 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07008082 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00008083}
8084
Linus Torvalds1da177e2005-04-16 15:20:36 -07008085/* If socket sndbuf has changed, wake up all per association waiters. */
8086void sctp_write_space(struct sock *sk)
8087{
8088 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008089
8090 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07008091 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092 __sctp_write_space(asoc);
8093 }
8094}
8095
8096/* Is there any sndbuf space available on the socket?
8097 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08008098 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099 * associations on the same socket. For a UDP-style socket with
8100 * multiple associations, it is possible for it to be "unwriteable"
8101 * prematurely. I assume that this is acceptable because
8102 * a premature "unwriteable" is better than an accidental "writeable" which
8103 * would cause an unwanted block under certain circumstances. For the 1-1
8104 * UDP-style sockets or TCP-style sockets, this code should work.
8105 * - Daisy
8106 */
8107static int sctp_writeable(struct sock *sk)
8108{
8109 int amt = 0;
8110
Eric Dumazet31e6d362009-06-17 19:05:41 -07008111 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008112 if (amt < 0)
8113 amt = 0;
8114 return amt;
8115}
8116
8117/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8118 * returns immediately with EINPROGRESS.
8119 */
8120static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8121{
8122 struct sock *sk = asoc->base.sk;
8123 int err = 0;
8124 long current_timeo = *timeo_p;
8125 DEFINE_WAIT(wait);
8126
Daniel Borkmannbb333812013-06-28 19:49:40 +02008127 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008128
8129 /* Increment the association's refcnt. */
8130 sctp_association_hold(asoc);
8131
8132 for (;;) {
8133 prepare_to_wait_exclusive(&asoc->wait, &wait,
8134 TASK_INTERRUPTIBLE);
8135 if (!*timeo_p)
8136 goto do_nonblock;
8137 if (sk->sk_shutdown & RCV_SHUTDOWN)
8138 break;
8139 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
8140 asoc->base.dead)
8141 goto do_error;
8142 if (signal_pending(current))
8143 goto do_interrupted;
8144
8145 if (sctp_state(asoc, ESTABLISHED))
8146 break;
8147
8148 /* Let another process have a go. Since we are going
8149 * to sleep anyway.
8150 */
wangweidong048ed4b2014-01-21 15:44:11 +08008151 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008153 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008154
8155 *timeo_p = current_timeo;
8156 }
8157
8158out:
8159 finish_wait(&asoc->wait, &wait);
8160
8161 /* Release the association's refcnt. */
8162 sctp_association_put(asoc);
8163
8164 return err;
8165
8166do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08008167 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168 err = -ETIMEDOUT;
8169 else
8170 err = -ECONNREFUSED;
8171 goto out;
8172
8173do_interrupted:
8174 err = sock_intr_errno(*timeo_p);
8175 goto out;
8176
8177do_nonblock:
8178 err = -EINPROGRESS;
8179 goto out;
8180}
8181
8182static int sctp_wait_for_accept(struct sock *sk, long timeo)
8183{
8184 struct sctp_endpoint *ep;
8185 int err = 0;
8186 DEFINE_WAIT(wait);
8187
8188 ep = sctp_sk(sk)->ep;
8189
8190
8191 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00008192 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193 TASK_INTERRUPTIBLE);
8194
8195 if (list_empty(&ep->asocs)) {
wangweidong048ed4b2014-01-21 15:44:11 +08008196 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197 timeo = schedule_timeout(timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008198 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199 }
8200
8201 err = -EINVAL;
8202 if (!sctp_sstate(sk, LISTENING))
8203 break;
8204
8205 err = 0;
8206 if (!list_empty(&ep->asocs))
8207 break;
8208
8209 err = sock_intr_errno(timeo);
8210 if (signal_pending(current))
8211 break;
8212
8213 err = -EAGAIN;
8214 if (!timeo)
8215 break;
8216 }
8217
Eric Dumazetaa395142010-04-20 13:03:51 +00008218 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008219
8220 return err;
8221}
8222
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02008223static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224{
8225 DEFINE_WAIT(wait);
8226
8227 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00008228 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229 if (list_empty(&sctp_sk(sk)->ep->asocs))
8230 break;
wangweidong048ed4b2014-01-21 15:44:11 +08008231 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008232 timeout = schedule_timeout(timeout);
wangweidong048ed4b2014-01-21 15:44:11 +08008233 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008234 } while (!signal_pending(current) && timeout);
8235
Eric Dumazetaa395142010-04-20 13:03:51 +00008236 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237}
8238
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008239static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
8240{
8241 struct sk_buff *frag;
8242
8243 if (!skb->data_len)
8244 goto done;
8245
8246 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07008247 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008248 sctp_skb_set_owner_r_frag(frag, sk);
8249
8250done:
8251 sctp_skb_set_owner_r(skb, sk);
8252}
8253
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008254void sctp_copy_sock(struct sock *newsk, struct sock *sk,
8255 struct sctp_association *asoc)
8256{
8257 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08008258 struct inet_sock *newinet;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008259
8260 newsk->sk_type = sk->sk_type;
8261 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
8262 newsk->sk_flags = sk->sk_flags;
Marcelo Ricardo Leitner50a5ffb2015-12-04 15:14:05 -02008263 newsk->sk_tsflags = sk->sk_tsflags;
Tom Herbert28448b82014-05-23 08:47:19 -07008264 newsk->sk_no_check_tx = sk->sk_no_check_tx;
8265 newsk->sk_no_check_rx = sk->sk_no_check_rx;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008266 newsk->sk_reuse = sk->sk_reuse;
8267
8268 newsk->sk_shutdown = sk->sk_shutdown;
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02008269 newsk->sk_destruct = sctp_destruct_sock;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008270 newsk->sk_family = sk->sk_family;
8271 newsk->sk_protocol = IPPROTO_SCTP;
8272 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
8273 newsk->sk_sndbuf = sk->sk_sndbuf;
8274 newsk->sk_rcvbuf = sk->sk_rcvbuf;
8275 newsk->sk_lingertime = sk->sk_lingertime;
8276 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
8277 newsk->sk_sndtimeo = sk->sk_sndtimeo;
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03008278 newsk->sk_rxhash = sk->sk_rxhash;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008279
8280 newinet = inet_sk(newsk);
8281
8282 /* Initialize sk's sport, dport, rcv_saddr and daddr for
8283 * getsockname() and getpeername()
8284 */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008285 newinet->inet_sport = inet->inet_sport;
8286 newinet->inet_saddr = inet->inet_saddr;
8287 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
8288 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008289 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008290 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008291
8292 newinet->uc_ttl = inet->uc_ttl;
8293 newinet->mc_loop = 1;
8294 newinet->mc_ttl = 1;
8295 newinet->mc_index = 0;
8296 newinet->mc_list = NULL;
Marcelo Ricardo Leitner01ce63c2015-12-04 15:14:04 -02008297
8298 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
8299 net_enable_timestamp();
Marcelo Ricardo Leitner3538a5c2015-12-23 16:44:09 -02008300
8301 security_sk_clone(sk, newsk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008302}
8303
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008304static inline void sctp_copy_descendant(struct sock *sk_to,
8305 const struct sock *sk_from)
8306{
8307 int ancestor_size = sizeof(struct inet_sock) +
8308 sizeof(struct sctp_sock) -
8309 offsetof(struct sctp_sock, auto_asconf_list);
8310
8311 if (sk_from->sk_family == PF_INET6)
8312 ancestor_size += sizeof(struct ipv6_pinfo);
8313
8314 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
8315}
8316
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317/* Populate the fields of the newsk from the oldsk and migrate the assoc
8318 * and its messages to the newsk.
8319 */
8320static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
8321 struct sctp_association *assoc,
Xin Longb7ef2612017-08-11 10:23:50 +08008322 enum sctp_socket_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008323{
8324 struct sctp_sock *oldsp = sctp_sk(oldsk);
8325 struct sctp_sock *newsp = sctp_sk(newsk);
8326 struct sctp_bind_bucket *pp; /* hash list port iterator */
8327 struct sctp_endpoint *newep = newsp->ep;
8328 struct sk_buff *skb, *tmp;
8329 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08008330 struct sctp_bind_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008331
8332 /* Migrate socket buffer sizes and all the socket level options to the
8333 * new socket.
8334 */
8335 newsk->sk_sndbuf = oldsk->sk_sndbuf;
8336 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
8337 /* Brute force copy old sctp opt. */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008338 sctp_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339
8340 /* Restore the ep value that was overwritten with the above structure
8341 * copy.
8342 */
8343 newsp->ep = newep;
8344 newsp->hmac = NULL;
8345
8346 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008347 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
8348 inet_sk(oldsk)->inet_num)];
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008349 spin_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008350 pp = sctp_sk(oldsk)->bind_hash;
8351 sk_add_bind_node(newsk, &pp->owner);
8352 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008353 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008354 spin_unlock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008356 /* Copy the bind_addr list from the original endpoint to the new
8357 * endpoint so that we can handle restarts properly
8358 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08008359 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
8360 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008361
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362 /* Move any messages in the old socket's receive queue that are for the
8363 * peeled off association to the new socket's receive queue.
8364 */
8365 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
8366 event = sctp_skb2event(skb);
8367 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008368 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008369 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008370 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008371 }
8372 }
8373
8374 /* Clean up any messages pending delivery due to partial
8375 * delivery. Three cases:
8376 * 1) No partial deliver; no work.
8377 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
8378 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
8379 */
8380 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008381 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008383 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384 struct sk_buff_head *queue;
8385
8386 /* Decide which queue to move pd_lobby skbs to. */
8387 if (assoc->ulpq.pd_mode) {
8388 queue = &newsp->pd_lobby;
8389 } else
8390 queue = &newsk->sk_receive_queue;
8391
8392 /* Walk through the pd_lobby, looking for skbs that
8393 * need moved to the new socket.
8394 */
8395 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
8396 event = sctp_skb2event(skb);
8397 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008398 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008399 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008400 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008401 }
8402 }
8403
8404 /* Clear up any skbs waiting for the partial
8405 * delivery to finish.
8406 */
8407 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008408 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008409
8410 }
8411
Xin Long13228232017-12-08 21:04:09 +08008412 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008413
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414 /* Set the type of socket to indicate that it is peeled off from the
8415 * original UDP-style socket or created with the accept() call on a
8416 * TCP-style socket..
8417 */
8418 newsp->type = type;
8419
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008420 /* Mark the new socket "in-use" by the user so that any packets
8421 * that may arrive on the association after we've moved it are
8422 * queued to the backlog. This prevents a potential race between
8423 * backlog processing on the old socket and new-packet processing
8424 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07008425 *
8426 * The caller has just allocated newsk so we can guarantee that other
8427 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008428 */
Zach Brown5131a182007-06-22 15:14:46 -07008429 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Xin Longd04adf12017-10-28 02:13:29 +08008430 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008431 sctp_assoc_migrate(assoc, newsk);
Xin Longd04adf12017-10-28 02:13:29 +08008432 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433
8434 /* If the association on the newsk is already closed before accept()
8435 * is called, set RCV_SHUTDOWN flag.
8436 */
Xin Longd46e4162016-06-09 22:48:18 +08008437 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08008438 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439 newsk->sk_shutdown |= RCV_SHUTDOWN;
Xin Longd46e4162016-06-09 22:48:18 +08008440 } else {
Yafang Shaocbabf462017-12-20 11:12:54 +08008441 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
Xin Longd46e4162016-06-09 22:48:18 +08008442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443
wangweidong048ed4b2014-01-21 15:44:11 +08008444 release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445}
8446
Neil Horman4d93df02007-08-15 16:07:44 -07008447
Linus Torvalds1da177e2005-04-16 15:20:36 -07008448/* This proto struct describes the ULP interface for SCTP. */
8449struct proto sctp_prot = {
8450 .name = "SCTP",
8451 .owner = THIS_MODULE,
8452 .close = sctp_close,
8453 .connect = sctp_connect,
8454 .disconnect = sctp_disconnect,
8455 .accept = sctp_accept,
8456 .ioctl = sctp_ioctl,
8457 .init = sctp_init_sock,
8458 .destroy = sctp_destroy_sock,
8459 .shutdown = sctp_shutdown,
8460 .setsockopt = sctp_setsockopt,
8461 .getsockopt = sctp_getsockopt,
8462 .sendmsg = sctp_sendmsg,
8463 .recvmsg = sctp_recvmsg,
8464 .bind = sctp_bind,
8465 .backlog_rcv = sctp_backlog_rcv,
8466 .hash = sctp_hash,
8467 .unhash = sctp_unhash,
8468 .get_port = sctp_get_port,
8469 .obj_size = sizeof(struct sctp_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07008470 .useroffset = offsetof(struct sctp_sock, subscribe),
8471 .usersize = offsetof(struct sctp_sock, initmsg) -
8472 offsetof(struct sctp_sock, subscribe) +
8473 sizeof_field(struct sctp_sock, initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07008474 .sysctl_mem = sysctl_sctp_mem,
8475 .sysctl_rmem = sysctl_sctp_rmem,
8476 .sysctl_wmem = sysctl_sctp_wmem,
8477 .memory_pressure = &sctp_memory_pressure,
8478 .enter_memory_pressure = sctp_enter_memory_pressure,
8479 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008480 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481};
8482
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008483#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08008484
Eric Dumazet602dd622015-12-01 07:20:07 -08008485#include <net/transp_v6.h>
8486static void sctp_v6_destroy_sock(struct sock *sk)
8487{
8488 sctp_destroy_sock(sk);
8489 inet6_destroy_sock(sk);
8490}
8491
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492struct proto sctpv6_prot = {
8493 .name = "SCTPv6",
8494 .owner = THIS_MODULE,
8495 .close = sctp_close,
8496 .connect = sctp_connect,
8497 .disconnect = sctp_disconnect,
8498 .accept = sctp_accept,
8499 .ioctl = sctp_ioctl,
8500 .init = sctp_init_sock,
Eric Dumazet602dd622015-12-01 07:20:07 -08008501 .destroy = sctp_v6_destroy_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008502 .shutdown = sctp_shutdown,
8503 .setsockopt = sctp_setsockopt,
8504 .getsockopt = sctp_getsockopt,
8505 .sendmsg = sctp_sendmsg,
8506 .recvmsg = sctp_recvmsg,
8507 .bind = sctp_bind,
8508 .backlog_rcv = sctp_backlog_rcv,
8509 .hash = sctp_hash,
8510 .unhash = sctp_unhash,
8511 .get_port = sctp_get_port,
8512 .obj_size = sizeof(struct sctp6_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07008513 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
8514 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
8515 offsetof(struct sctp6_sock, sctp.subscribe) +
8516 sizeof_field(struct sctp6_sock, sctp.initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07008517 .sysctl_mem = sysctl_sctp_mem,
8518 .sysctl_rmem = sysctl_sctp_rmem,
8519 .sysctl_wmem = sysctl_sctp_wmem,
8520 .memory_pressure = &sctp_memory_pressure,
8521 .enter_memory_pressure = sctp_enter_memory_pressure,
8522 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008523 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008524};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008525#endif /* IS_ENABLED(CONFIG_IPV6) */