blob: 067b57a330b129e2ce7e8218e44193d3e5e8561f [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 * Copyright (c) 2001-2002 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
8 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05009 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * These functions interface with the sockets layer to implement the
12 * SCTP Extensions for the Sockets API.
13 *
14 * Note that the descriptions from the specification are USER level
15 * functions--this file is the functions which populate the struct proto
16 * for SCTP which is the BOTTOM of the sockets interface.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * you can redistribute it and/or modify it under the terms of
20 * the GNU General Public License as published by
21 * the Free Software Foundation; either version 2, or (at your option)
22 * any later version.
23 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050024 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26 * ************************
27 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28 * See the GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080031 * along with GNU CC; see the file COPYING. If not, see
32 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
34 * Please send any bug reports or fixes you make to the
35 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020036 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * Written or modified by:
39 * La Monte H.P. Yarroll <piggy@acm.org>
40 * Narasimha Budihal <narsi@refcode.org>
41 * Karl Knutson <karl@athena.chicago.il.us>
42 * Jon Grimm <jgrimm@us.ibm.com>
43 * Xingang Guo <xingang.guo@intel.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Sridhar Samudrala <samudrala@us.ibm.com>
46 * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
47 * Ardelle Fan <ardelle.fan@intel.com>
48 * Ryan Layer <rmlayer@us.ibm.com>
49 * Anup Pemmaiah <pemmaiah@cc.usu.edu>
50 * Kevin Gao <kevin.gao@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
52
Joe Perches145ce502010-08-24 13:21:08 +000053#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
Herbert Xu5821c762016-01-24 21:20:12 +080055#include <crypto/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/types.h>
57#include <linux/kernel.h>
58#include <linux/wait.h>
59#include <linux/time.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010060#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/ip.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080062#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/fcntl.h>
64#include <linux/poll.h>
65#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090066#include <linux/slab.h>
Al Viro56b31d12012-08-18 00:25:51 -040067#include <linux/file.h>
Daniel Borkmannffd59392014-02-17 12:11:11 +010068#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#include <net/ip.h>
71#include <net/icmp.h>
72#include <net/route.h>
73#include <net/ipv6.h>
74#include <net/inet_common.h>
Neil Horman8465a5f2014-04-17 15:26:51 -040075#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#include <linux/socket.h> /* for sa_family_t */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040078#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <net/sock.h>
80#include <net/sctp/sctp.h>
81#include <net/sctp/sm.h>
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -030082#include <net/sctp/stream_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/* Forward declarations for internal helper functions. */
85static int sctp_writeable(struct sock *sk);
86static void sctp_wfree(struct sk_buff *skb);
Xin Longcea0cc82017-11-15 16:57:26 +080087static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +080088 size_t msg_len);
wangweidong26ac8e52013-12-23 12:16:51 +080089static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
91static int sctp_wait_for_accept(struct sock *sk, long timeo);
92static void sctp_wait_for_close(struct sock *sk, long timeo);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +020093static void sctp_destruct_sock(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
95 union sctp_addr *addr, int len);
96static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
97static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
98static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
99static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
100static int sctp_send_asconf(struct sctp_association *asoc,
101 struct sctp_chunk *chunk);
102static int sctp_do_bind(struct sock *, union sctp_addr *, int);
103static int sctp_autobind(struct sock *sk);
Xin Longb7ef2612017-08-11 10:23:50 +0800104static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
105 struct sctp_association *assoc,
106 enum sctp_socket_type type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Eric Dumazet06044752017-06-07 13:29:12 -0700108static unsigned long sctp_memory_pressure;
Eric Dumazet8d987e52010-11-09 23:24:26 +0000109static atomic_long_t sctp_memory_allocated;
Eric Dumazet17483762008-11-25 21:16:35 -0800110struct percpu_counter sctp_sockets_allocated;
Neil Horman4d93df02007-08-15 16:07:44 -0700111
Pavel Emelyanov5c52ba12008-07-16 20:28:10 -0700112static void sctp_enter_memory_pressure(struct sock *sk)
Neil Horman4d93df02007-08-15 16:07:44 -0700113{
114 sctp_memory_pressure = 1;
115}
116
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/* Get the sndbuf space available at the time on the association. */
119static inline int sctp_wspace(struct sctp_association *asoc)
120{
Neil Horman4d93df02007-08-15 16:07:44 -0700121 int amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Neil Horman4d93df02007-08-15 16:07:44 -0700123 if (asoc->ep->sndbuf_policy)
124 amt = asoc->sndbuf_used;
125 else
Eric Dumazet31e6d362009-06-17 19:05:41 -0700126 amt = sk_wmem_alloc_get(asoc->base.sk);
Neil Horman4d93df02007-08-15 16:07:44 -0700127
128 if (amt >= asoc->base.sk->sk_sndbuf) {
129 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
130 amt = 0;
131 else {
132 amt = sk_stream_wspace(asoc->base.sk);
133 if (amt < 0)
134 amt = 0;
135 }
Neil Horman4eb701d2005-04-28 12:02:04 -0700136 } else {
Neil Horman4d93df02007-08-15 16:07:44 -0700137 amt = asoc->base.sk->sk_sndbuf - amt;
Neil Horman4eb701d2005-04-28 12:02:04 -0700138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return amt;
140}
141
142/* Increment the used sndbuf space count of the corresponding association by
143 * the size of the outgoing data chunk.
144 * Also, set the skb destructor for sndbuf accounting later.
145 *
146 * Since it is always 1-1 between chunk and skb, and also a new skb is always
147 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
148 * destructor in the data chunk skb for the purpose of the sndbuf space
149 * tracking.
150 */
151static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
152{
153 struct sctp_association *asoc = chunk->asoc;
154 struct sock *sk = asoc->base.sk;
155
156 /* The sndbuf space is tracked per association. */
157 sctp_association_hold(asoc);
158
Neil Horman4eb701d2005-04-28 12:02:04 -0700159 skb_set_owner_w(chunk->skb, sk);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 chunk->skb->destructor = sctp_wfree;
162 /* Save the chunk pointer in skb for sctp_wfree to use later. */
Daniel Borkmannf869c912014-11-20 01:54:48 +0100163 skb_shinfo(chunk->skb)->destructor_arg = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Neil Horman4eb701d2005-04-28 12:02:04 -0700165 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
166 sizeof(struct sk_buff) +
167 sizeof(struct sctp_chunk);
168
Reshetova, Elena14afee42017-06-30 13:08:00 +0300169 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800170 sk->sk_wmem_queued += chunk->skb->truesize;
171 sk_mem_charge(sk, chunk->skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Xin Longd04adf12017-10-28 02:13:29 +0800174static void sctp_clear_owner_w(struct sctp_chunk *chunk)
175{
176 skb_orphan(chunk->skb);
177}
178
179static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
180 void (*cb)(struct sctp_chunk *))
181
182{
183 struct sctp_outq *q = &asoc->outqueue;
184 struct sctp_transport *t;
185 struct sctp_chunk *chunk;
186
187 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
188 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
189 cb(chunk);
190
Xin Longa8dd3972017-11-26 20:56:07 +0800191 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800192 cb(chunk);
193
Xin Longa8dd3972017-11-26 20:56:07 +0800194 list_for_each_entry(chunk, &q->sacked, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800195 cb(chunk);
196
Xin Longa8dd3972017-11-26 20:56:07 +0800197 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
Xin Longd04adf12017-10-28 02:13:29 +0800198 cb(chunk);
199
200 list_for_each_entry(chunk, &q->out_chunk_list, list)
201 cb(chunk);
202}
203
Xin Long13228232017-12-08 21:04:09 +0800204static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
205 void (*cb)(struct sk_buff *, struct sock *))
206
207{
208 struct sk_buff *skb, *tmp;
209
210 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
211 cb(skb, sk);
212
213 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
214 cb(skb, sk);
215
216 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
217 cb(skb, sk);
218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/* Verify that this is a valid address. */
221static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
222 int len)
223{
224 struct sctp_af *af;
225
226 /* Verify basic sockaddr. */
227 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
228 if (!af)
229 return -EINVAL;
230
231 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700232 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -EINVAL;
234
235 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
236 return -EINVAL;
237
238 return 0;
239}
240
241/* Look up the association by its id. If this is not a UDP-style
242 * socket, the ID field is always ignored.
243 */
244struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
245{
246 struct sctp_association *asoc = NULL;
247
248 /* If this is not a UDP-style socket, assoc id should be ignored. */
249 if (!sctp_style(sk, UDP)) {
250 /* Return NULL if the socket state is not ESTABLISHED. It
251 * could be a TCP-style listening socket or a socket which
252 * hasn't yet called connect() to establish an association.
253 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -0300254 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return NULL;
256
257 /* Get the first and the only association from the list. */
258 if (!list_empty(&sctp_sk(sk)->ep->asocs))
259 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
260 struct sctp_association, asocs);
261 return asoc;
262 }
263
264 /* Otherwise this is a UDP-style socket. */
265 if (!id || (id == (sctp_assoc_t)-1))
266 return NULL;
267
268 spin_lock_bh(&sctp_assocs_id_lock);
269 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
270 spin_unlock_bh(&sctp_assocs_id_lock);
271
272 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
273 return NULL;
274
275 return asoc;
276}
277
278/* Look up the transport from an address and an assoc id. If both address and
279 * id are specified, the associations matching the address and the id should be
280 * the same.
281 */
282static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
283 struct sockaddr_storage *addr,
284 sctp_assoc_t id)
285{
286 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
Xin Long6f29a132017-01-24 14:01:53 +0800287 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 union sctp_addr *laddr = (union sctp_addr *)addr;
Xin Long6f29a132017-01-24 14:01:53 +0800289 struct sctp_transport *transport;
290
Xin Long912964e2017-02-07 20:56:08 +0800291 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
Xin Long6f29a132017-01-24 14:01:53 +0800292 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800295 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 if (!addr_asoc)
299 return NULL;
300
301 id_asoc = sctp_id2assoc(sk, id);
302 if (id_asoc && (id_asoc != addr_asoc))
303 return NULL;
304
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600305 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 (union sctp_addr *)addr);
307
308 return transport;
309}
310
311/* API 3.1.2 bind() - UDP Style Syntax
312 * The syntax of bind() is,
313 *
314 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
315 *
316 * sd - the socket descriptor returned by socket().
317 * addr - the address structure (struct sockaddr_in or struct
318 * sockaddr_in6 [RFC 2553]),
319 * addr_len - the size of the address structure.
320 */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200321static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 int retval = 0;
324
wangweidong048ed4b2014-01-21 15:44:11 +0800325 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Daniel Borkmannbb333812013-06-28 19:49:40 +0200327 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
328 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* Disallow binding twice. */
331 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700332 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 addr_len);
334 else
335 retval = -EINVAL;
336
wangweidong048ed4b2014-01-21 15:44:11 +0800337 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 return retval;
340}
341
342static long sctp_get_port_local(struct sock *, union sctp_addr *);
343
344/* Verify this is a valid sockaddr. */
345static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
346 union sctp_addr *addr, int len)
347{
348 struct sctp_af *af;
349
350 /* Check minimum size. */
351 if (len < sizeof (struct sockaddr))
352 return NULL;
353
Xin Longc5006b82018-01-15 17:02:00 +0800354 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
355 return NULL;
356
Vlad Yasevich7dab83d2008-07-18 23:05:40 -0700357 /* V4 mapped address are really of AF_INET family */
358 if (addr->sa.sa_family == AF_INET6 &&
Xin Longc5006b82018-01-15 17:02:00 +0800359 ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
360 !opt->pf->af_supported(AF_INET, opt))
361 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* If we get this far, af is valid. */
364 af = sctp_get_af_specific(addr->sa.sa_family);
365
366 if (len < af->sockaddr_len)
367 return NULL;
368
369 return af;
370}
371
372/* Bind a local address either to an endpoint or to an association. */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200373static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Eric W. Biederman35946982012-11-16 03:03:12 +0000375 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct sctp_sock *sp = sctp_sk(sk);
377 struct sctp_endpoint *ep = sp->ep;
378 struct sctp_bind_addr *bp = &ep->base.bind_addr;
379 struct sctp_af *af;
380 unsigned short snum;
381 int ret = 0;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* Common sockaddr verification. */
384 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700385 if (!af) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200386 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
387 __func__, sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700389 }
390
391 snum = ntohs(addr->v4.sin_port);
392
Daniel Borkmannbb333812013-06-28 19:49:40 +0200393 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
394 __func__, sk, &addr->sa, bp->port, snum, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /* PF specific bind() address verification. */
397 if (!sp->pf->bind_verify(sp, addr))
398 return -EADDRNOTAVAIL;
399
Vlad Yasevich8b358052007-05-15 17:14:58 -0400400 /* We must either be unbound, or bind to the same port.
401 * It's OK to allow 0 ports if we are already bound.
402 * We'll just inhert an already bound port in this case
403 */
404 if (bp->port) {
405 if (!snum)
406 snum = bp->port;
407 else if (snum != bp->port) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200408 pr_debug("%s: new port %d doesn't match existing port "
409 "%d\n", __func__, snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400410 return -EINVAL;
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
Krister Johansen4548b682017-01-20 17:49:11 -0800414 if (snum && snum < inet_prot_sock(net) &&
Eric W. Biederman35946982012-11-16 03:03:12 +0000415 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return -EACCES;
417
Vlad Yasevich4e540642008-07-18 23:06:32 -0700418 /* See if the address matches any of the addresses we may have
419 * already bound before checking against other endpoints.
420 */
421 if (sctp_bind_addr_match(bp, addr, sp))
422 return -EINVAL;
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* Make sure we are allowed to bind here.
425 * The function sctp_get_port_local() does duplicate address
426 * detection.
427 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900428 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if ((ret = sctp_get_port_local(sk, addr))) {
Vlad Yasevich4e540642008-07-18 23:06:32 -0700430 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
433 /* Refresh ephemeral port. */
434 if (!bp->port)
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000435 bp->port = inet_sk(sk)->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Vlad Yasevich559cf712007-09-16 16:03:28 -0700437 /* Add the address to the bind address list.
438 * Use GFP_ATOMIC since BHs will be disabled.
439 */
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300440 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
441 SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 /* Copy back into socket for getsockname() use. */
444 if (!ret) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000445 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600446 sp->pf->to_sk_saddr(addr, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
449 return ret;
450}
451
452 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
453 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900454 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900456 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900458 * subsequent ASCONF. Note this restriction binds each side, so at any
459 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * from each endpoint).
461 */
462static int sctp_send_asconf(struct sctp_association *asoc,
463 struct sctp_chunk *chunk)
464{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000465 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int retval = 0;
467
468 /* If there is an outstanding ASCONF chunk, queue it for later
469 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900470 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700472 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900473 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475
476 /* Hold the chunk until an ASCONF_ACK is received. */
477 sctp_chunk_hold(chunk);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000478 retval = sctp_primitive_ASCONF(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (retval)
480 sctp_chunk_free(chunk);
481 else
482 asoc->addip_last_asconf = chunk;
483
484out:
485 return retval;
486}
487
488/* Add a list of addresses as bind addresses to local endpoint or
489 * association.
490 *
491 * Basically run through each address specified in the addrs/addrcnt
492 * array/length pair, determine if it is IPv6 or IPv4 and call
493 * sctp_do_bind() on it.
494 *
495 * If any of them fails, then the operation will be reversed and the
496 * ones that were added will be removed.
497 *
498 * Only sctp_setsockopt_bindx() is supposed to call this function.
499 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200500static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 int cnt;
503 int retval = 0;
504 void *addr_buf;
505 struct sockaddr *sa_addr;
506 struct sctp_af *af;
507
Daniel Borkmannbb333812013-06-28 19:49:40 +0200508 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
509 addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 addr_buf = addrs;
512 for (cnt = 0; cnt < addrcnt; cnt++) {
513 /* The list may contain either IPv4 or IPv6 address;
514 * determine the address length for walking thru the list.
515 */
Joe Perchesea110732011-06-13 16:21:26 +0000516 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 af = sctp_get_af_specific(sa_addr->sa_family);
518 if (!af) {
519 retval = -EINVAL;
520 goto err_bindx_add;
521 }
522
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900523 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 af->sockaddr_len);
525
526 addr_buf += af->sockaddr_len;
527
528err_bindx_add:
529 if (retval < 0) {
530 /* Failed. Cleanup the ones that have been added */
531 if (cnt > 0)
532 sctp_bindx_rem(sk, addrs, cnt);
533 return retval;
534 }
535 }
536
537 return retval;
538}
539
540/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
541 * associations that are part of the endpoint indicating that a list of local
542 * addresses are added to the endpoint.
543 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900544 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 * association, we do not send the chunk for that association. But it will not
546 * affect other associations.
547 *
548 * Only sctp_setsockopt_bindx() is supposed to call this function.
549 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900550static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct sockaddr *addrs,
552 int addrcnt)
553{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000554 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct sctp_sock *sp;
556 struct sctp_endpoint *ep;
557 struct sctp_association *asoc;
558 struct sctp_bind_addr *bp;
559 struct sctp_chunk *chunk;
560 struct sctp_sockaddr_entry *laddr;
561 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700562 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 void *addr_buf;
564 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct list_head *p;
566 int i;
567 int retval = 0;
568
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000569 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return retval;
571
572 sp = sctp_sk(sk);
573 ep = sp->ep;
574
Daniel Borkmannbb333812013-06-28 19:49:40 +0200575 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
576 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700578 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (!asoc->peer.asconf_capable)
580 continue;
581
582 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
583 continue;
584
585 if (!sctp_state(asoc, ESTABLISHED))
586 continue;
587
588 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900589 * in the bind address list of the association. If so,
590 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * other associations.
592 */
593 addr_buf = addrs;
594 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000595 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 af = sctp_get_af_specific(addr->v4.sin_family);
597 if (!af) {
598 retval = -EINVAL;
599 goto out;
600 }
601
602 if (sctp_assoc_lookup_laddr(asoc, addr))
603 break;
604
605 addr_buf += af->sockaddr_len;
606 }
607 if (i < addrcnt)
608 continue;
609
Vlad Yasevich559cf712007-09-16 16:03:28 -0700610 /* Use the first valid address in bind addr list of
611 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 bp = &asoc->base.bind_addr;
614 p = bp->address_list.next;
615 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800616 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 addrcnt, SCTP_PARAM_ADD_IP);
618 if (!chunk) {
619 retval = -ENOMEM;
620 goto out;
621 }
622
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700623 /* Add the new addresses to the bind address list with
624 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700626 addr_buf = addrs;
627 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000628 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700629 af = sctp_get_af_specific(addr->v4.sin_family);
630 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800631 retval = sctp_add_bind_addr(bp, &saveaddr,
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300632 sizeof(saveaddr),
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800633 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700634 addr_buf += af->sockaddr_len;
635 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900636 if (asoc->src_out_of_asoc_ok) {
637 struct sctp_transport *trans;
638
639 list_for_each_entry(trans,
640 &asoc->peer.transport_addr_list, transports) {
641 /* Clear the source and route cache */
Julian Anastasovc86a7732017-02-06 23:14:13 +0200642 sctp_transport_dst_release(trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900643 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
644 2*asoc->pathmtu, 4380));
645 trans->ssthresh = asoc->peer.i.a_rwnd;
646 trans->rto = asoc->rto_initial;
Michele Baldessari196d6752012-12-01 04:49:42 +0000647 sctp_max_rto(asoc, trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900648 trans->rtt = trans->srtt = trans->rttvar = 0;
649 sctp_transport_route(trans, NULL,
650 sctp_sk(asoc->base.sk));
651 }
652 }
653 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655
656out:
657 return retval;
658}
659
660/* Remove a list of addresses from bind addresses list. Do not remove the
661 * last address.
662 *
663 * Basically run through each address specified in the addrs/addrcnt
664 * array/length pair, determine if it is IPv6 or IPv4 and call
665 * sctp_del_bind() on it.
666 *
667 * If any of them fails, then the operation will be reversed and the
668 * ones that were removed will be added back.
669 *
670 * At least one address has to be left; if only one address is
671 * available, the operation will return -EBUSY.
672 *
673 * Only sctp_setsockopt_bindx() is supposed to call this function.
674 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200675static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 struct sctp_sock *sp = sctp_sk(sk);
678 struct sctp_endpoint *ep = sp->ep;
679 int cnt;
680 struct sctp_bind_addr *bp = &ep->base.bind_addr;
681 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800683 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct sctp_af *af;
685
Daniel Borkmannbb333812013-06-28 19:49:40 +0200686 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
687 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 addr_buf = addrs;
690 for (cnt = 0; cnt < addrcnt; cnt++) {
691 /* If the bind address list is empty or if there is only one
692 * bind address, there is nothing more to be removed (we need
693 * at least one address here).
694 */
695 if (list_empty(&bp->address_list) ||
696 (sctp_list_single_entry(&bp->address_list))) {
697 retval = -EBUSY;
698 goto err_bindx_rem;
699 }
700
Joe Perchesea110732011-06-13 16:21:26 +0000701 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800702 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (!af) {
704 retval = -EINVAL;
705 goto err_bindx_rem;
706 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700707
708 if (!af->addr_valid(sa_addr, sp, NULL)) {
709 retval = -EADDRNOTAVAIL;
710 goto err_bindx_rem;
711 }
712
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000713 if (sa_addr->v4.sin_port &&
714 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 retval = -EINVAL;
716 goto err_bindx_rem;
717 }
718
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000719 if (!sa_addr->v4.sin_port)
720 sa_addr->v4.sin_port = htons(bp->port);
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* FIXME - There is probably a need to check if sk->sk_saddr and
723 * sk->sk_rcv_addr are currently set to one of the addresses to
724 * be removed. This is something which needs to be looked into
725 * when we are fixing the outstanding issues with multi-homing
726 * socket routing and failover schemes. Refer to comments in
727 * sctp_do_bind(). -daisy
728 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400729 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 addr_buf += af->sockaddr_len;
732err_bindx_rem:
733 if (retval < 0) {
734 /* Failed. Add the ones that has been removed back */
735 if (cnt > 0)
736 sctp_bindx_add(sk, addrs, cnt);
737 return retval;
738 }
739 }
740
741 return retval;
742}
743
744/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
745 * the associations that are part of the endpoint indicating that a list of
746 * local addresses are removed from the endpoint.
747 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900748 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * association, we do not send the chunk for that association. But it will not
750 * affect other associations.
751 *
752 * Only sctp_setsockopt_bindx() is supposed to call this function.
753 */
754static int sctp_send_asconf_del_ip(struct sock *sk,
755 struct sockaddr *addrs,
756 int addrcnt)
757{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000758 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 struct sctp_sock *sp;
760 struct sctp_endpoint *ep;
761 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700762 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct sctp_bind_addr *bp;
764 struct sctp_chunk *chunk;
765 union sctp_addr *laddr;
766 void *addr_buf;
767 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700768 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int i;
770 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900771 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Michio Honda8a07eb02011-04-26 20:19:36 +0900773 chunk = NULL;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000774 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return retval;
776
777 sp = sctp_sk(sk);
778 ep = sp->ep;
779
Daniel Borkmannbb333812013-06-28 19:49:40 +0200780 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
781 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700783 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 if (!asoc->peer.asconf_capable)
786 continue;
787
788 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
789 continue;
790
791 if (!sctp_state(asoc, ESTABLISHED))
792 continue;
793
794 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900795 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 * If so, do not send the asconf chunk to its peer, but
797 * continue with other associations.
798 */
799 addr_buf = addrs;
800 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000801 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 af = sctp_get_af_specific(laddr->v4.sin_family);
803 if (!af) {
804 retval = -EINVAL;
805 goto out;
806 }
807
808 if (!sctp_assoc_lookup_laddr(asoc, laddr))
809 break;
810
811 addr_buf += af->sockaddr_len;
812 }
813 if (i < addrcnt)
814 continue;
815
816 /* Find one address in the association's bind address list
817 * that is not in the packed array of addresses. This is to
818 * make sure that we do not delete all the addresses in the
819 * association.
820 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 bp = &asoc->base.bind_addr;
822 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
823 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900824 if ((laddr == NULL) && (addrcnt == 1)) {
825 if (asoc->asconf_addr_del_pending)
826 continue;
827 asoc->asconf_addr_del_pending =
828 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900829 if (asoc->asconf_addr_del_pending == NULL) {
830 retval = -ENOMEM;
831 goto out;
832 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900833 asoc->asconf_addr_del_pending->sa.sa_family =
834 addrs->sa_family;
835 asoc->asconf_addr_del_pending->v4.sin_port =
836 htons(bp->port);
837 if (addrs->sa_family == AF_INET) {
838 struct sockaddr_in *sin;
839
840 sin = (struct sockaddr_in *)addrs;
841 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
842 } else if (addrs->sa_family == AF_INET6) {
843 struct sockaddr_in6 *sin6;
844
845 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000846 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900847 }
Daniel Borkmannbb333812013-06-28 19:49:40 +0200848
849 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
850 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
851 asoc->asconf_addr_del_pending);
852
Michio Honda8a07eb02011-04-26 20:19:36 +0900853 asoc->src_out_of_asoc_ok = 1;
854 stored = 1;
855 goto skip_mkasconf;
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Daniel Borkmann88362ad2013-09-07 20:51:21 +0200858 if (laddr == NULL)
859 return -EINVAL;
860
Vlad Yasevich559cf712007-09-16 16:03:28 -0700861 /* We do not need RCU protection throughout this loop
862 * because this is done under a socket lock from the
863 * setsockopt call.
864 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
866 SCTP_PARAM_DEL_IP);
867 if (!chunk) {
868 retval = -ENOMEM;
869 goto out;
870 }
871
Michio Honda8a07eb02011-04-26 20:19:36 +0900872skip_mkasconf:
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700873 /* Reset use_as_src flag for the addresses in the bind address
874 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700876 addr_buf = addrs;
877 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000878 laddr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700879 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700880 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a132006-11-20 17:05:23 -0800881 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800882 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700883 }
884 addr_buf += af->sockaddr_len;
885 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700886
887 /* Update the route and saddr entries for all the transports
888 * as some of the addresses in the bind address list are
889 * about to be deleted and cannot be used as source addresses.
890 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700891 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
892 transports) {
Julian Anastasovc86a7732017-02-06 23:14:13 +0200893 sctp_transport_dst_release(transport);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700894 sctp_transport_route(transport, NULL,
895 sctp_sk(asoc->base.sk));
896 }
897
Michio Honda8a07eb02011-04-26 20:19:36 +0900898 if (stored)
899 /* We don't need to transmit ASCONF */
900 continue;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700901 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903out:
904 return retval;
905}
906
Michio Honda9f7d6532011-04-26 19:32:51 +0900907/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
908int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
909{
910 struct sock *sk = sctp_opt2sk(sp);
911 union sctp_addr *addr;
912 struct sctp_af *af;
913
914 /* It is safe to write port space in caller. */
915 addr = &addrw->a;
916 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
917 af = sctp_get_af_specific(addr->sa.sa_family);
918 if (!af)
919 return -EINVAL;
920 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
921 return -EINVAL;
922
923 if (addrw->state == SCTP_ADDR_NEW)
924 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
925 else
926 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
930 *
931 * API 8.1
932 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
933 * int flags);
934 *
935 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
936 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
937 * or IPv6 addresses.
938 *
939 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
940 * Section 3.1.2 for this usage.
941 *
942 * addrs is a pointer to an array of one or more socket addresses. Each
943 * address is contained in its appropriate structure (i.e. struct
944 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700945 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * representation is termed a "packed array" of addresses). The caller
947 * specifies the number of addresses in the array with addrcnt.
948 *
949 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
950 * -1, and sets errno to the appropriate error code.
951 *
952 * For SCTP, the port given in each socket address must be the same, or
953 * sctp_bindx() will fail, setting errno to EINVAL.
954 *
955 * The flags parameter is formed from the bitwise OR of zero or more of
956 * the following currently defined flags:
957 *
958 * SCTP_BINDX_ADD_ADDR
959 *
960 * SCTP_BINDX_REM_ADDR
961 *
962 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
963 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
964 * addresses from the association. The two flags are mutually exclusive;
965 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
966 * not remove all addresses from an association; sctp_bindx() will
967 * reject such an attempt with EINVAL.
968 *
969 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
970 * additional addresses with an endpoint after calling bind(). Or use
971 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
972 * socket is associated with so that no new association accepted will be
973 * associated with those addresses. If the endpoint supports dynamic
974 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
975 * endpoint to send the appropriate message to the peer to change the
976 * peers address lists.
977 *
978 * Adding and removing addresses from a connected association is
979 * optional functionality. Implementations that do not support this
980 * functionality should return EOPNOTSUPP.
981 *
982 * Basically do nothing but copying the addresses from user to kernel
983 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700984 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
985 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
988 * it.
989 *
990 * sk The sk of the socket
991 * addrs The pointer to the addresses in user land
992 * addrssize Size of the addrs buffer
993 * op Operation to perform (add or remove, see the flags of
994 * sctp_bindx)
995 *
996 * Returns 0 if ok, <0 errno code on error.
997 */
wangweidong26ac8e52013-12-23 12:16:51 +0800998static int sctp_setsockopt_bindx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +0200999 struct sockaddr __user *addrs,
1000 int addrs_size, int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 struct sockaddr *kaddrs;
1003 int err;
1004 int addrcnt = 0;
1005 int walk_size = 0;
1006 struct sockaddr *sa_addr;
1007 void *addr_buf;
1008 struct sctp_af *af;
1009
Daniel Borkmannbb333812013-06-28 19:49:40 +02001010 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1011 __func__, sk, addrs, addrs_size, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 if (unlikely(addrs_size <= 0))
1014 return -EINVAL;
1015
Al Viroc981f252018-01-07 13:19:09 -05001016 kaddrs = vmemdup_user(addrs, addrs_size);
1017 if (unlikely(IS_ERR(kaddrs)))
1018 return PTR_ERR(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001020 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 addr_buf = kaddrs;
1022 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001023 if (walk_size + sizeof(sa_family_t) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001024 kvfree(kaddrs);
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001025 return -EINVAL;
1026 }
1027
Joe Perchesea110732011-06-13 16:21:26 +00001028 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 af = sctp_get_af_specific(sa_addr->sa_family);
1030
1031 /* If the address family is not supported or if this address
1032 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001033 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
Al Viroc981f252018-01-07 13:19:09 -05001035 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return -EINVAL;
1037 }
1038 addrcnt++;
1039 addr_buf += af->sockaddr_len;
1040 walk_size += af->sockaddr_len;
1041 }
1042
1043 /* Do the work. */
1044 switch (op) {
1045 case SCTP_BINDX_ADD_ADDR:
1046 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1047 if (err)
1048 goto out;
1049 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1050 break;
1051
1052 case SCTP_BINDX_REM_ADDR:
1053 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1054 if (err)
1055 goto out;
1056 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1057 break;
1058
1059 default:
1060 err = -EINVAL;
1061 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064out:
Al Viroc981f252018-01-07 13:19:09 -05001065 kvfree(kaddrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 return err;
1068}
1069
Frank Filz3f7a87d2005-06-20 13:14:57 -07001070/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1071 *
1072 * Common routine for handling connect() and sctp_connectx().
1073 * Connect will come in with just a single address.
1074 */
wangweidong26ac8e52013-12-23 12:16:51 +08001075static int __sctp_connect(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001076 struct sockaddr *kaddrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001077 int addrs_size,
1078 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001079{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001080 struct net *net = sock_net(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001081 struct sctp_sock *sp;
1082 struct sctp_endpoint *ep;
1083 struct sctp_association *asoc = NULL;
1084 struct sctp_association *asoc2;
1085 struct sctp_transport *transport;
1086 union sctp_addr to;
Xin Long1c662012017-08-05 19:59:54 +08001087 enum sctp_scope scope;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001088 long timeo;
1089 int err = 0;
1090 int addrcnt = 0;
1091 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001092 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001093 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001094 unsigned short port;
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001095 unsigned int f_flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001096
1097 sp = sctp_sk(sk);
1098 ep = sp->ep;
1099
1100 /* connect() cannot be done on a socket that is already in ESTABLISHED
1101 * state - UDP-style peeled off socket or a TCP-style socket that
1102 * is already connected.
1103 * It cannot be done even on a TCP-style listening socket.
1104 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03001105 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
Frank Filz3f7a87d2005-06-20 13:14:57 -07001106 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1107 err = -EISCONN;
1108 goto out_free;
1109 }
1110
1111 /* Walk through the addrs buffer and count the number of addresses. */
1112 addr_buf = kaddrs;
1113 while (walk_size < addrs_size) {
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001114 struct sctp_af *af;
1115
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001116 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1117 err = -EINVAL;
1118 goto out_free;
1119 }
1120
Joe Perchesea110732011-06-13 16:21:26 +00001121 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001122 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001123
1124 /* If the address family is not supported or if this address
1125 * causes the address buffer to overflow return EINVAL.
1126 */
1127 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1128 err = -EINVAL;
1129 goto out_free;
1130 }
1131
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001132 port = ntohs(sa_addr->v4.sin_port);
1133
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001134 /* Save current address so we can work with it */
1135 memcpy(&to, sa_addr, af->sockaddr_len);
1136
1137 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001138 if (err)
1139 goto out_free;
1140
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001141 /* Make sure the destination port is correctly set
1142 * in all addresses.
1143 */
Wei Yongjun524fba62013-04-03 03:02:28 +00001144 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1145 err = -EINVAL;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001146 goto out_free;
Wei Yongjun524fba62013-04-03 03:02:28 +00001147 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001148
1149 /* Check if there already is a matching association on the
1150 * endpoint (other than the one created here).
1151 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001152 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001153 if (asoc2 && asoc2 != asoc) {
1154 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1155 err = -EISCONN;
1156 else
1157 err = -EALREADY;
1158 goto out_free;
1159 }
1160
1161 /* If we could not find a matching association on the endpoint,
1162 * make sure that there is no peeled-off association matching
1163 * the peer address even on another socket.
1164 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001165 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001166 err = -EADDRNOTAVAIL;
1167 goto out_free;
1168 }
1169
1170 if (!asoc) {
1171 /* If a bind() or sctp_bindx() is not called prior to
1172 * an sctp_connectx() call, the system picks an
1173 * ephemeral port and will choose an address set
1174 * equivalent to binding with a wildcard address.
1175 */
1176 if (!ep->base.bind_addr.port) {
1177 if (sctp_autobind(sk)) {
1178 err = -EAGAIN;
1179 goto out_free;
1180 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001181 } else {
1182 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001183 * If an unprivileged user inherits a 1-many
1184 * style socket with open associations on a
1185 * privileged port, it MAY be permitted to
1186 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001187 * be permitted to open new associations.
1188 */
Krister Johansen4548b682017-01-20 17:49:11 -08001189 if (ep->base.bind_addr.port <
1190 inet_prot_sock(net) &&
1191 !ns_capable(net->user_ns,
1192 CAP_NET_BIND_SERVICE)) {
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001193 err = -EACCES;
1194 goto out_free;
1195 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001196 }
1197
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001198 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001199 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1200 if (!asoc) {
1201 err = -ENOMEM;
1202 goto out_free;
1203 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001204
1205 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1206 GFP_KERNEL);
1207 if (err < 0) {
1208 goto out_free;
1209 }
1210
Frank Filz3f7a87d2005-06-20 13:14:57 -07001211 }
1212
1213 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001214 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001215 SCTP_UNKNOWN);
1216 if (!transport) {
1217 err = -ENOMEM;
1218 goto out_free;
1219 }
1220
1221 addrcnt++;
1222 addr_buf += af->sockaddr_len;
1223 walk_size += af->sockaddr_len;
1224 }
1225
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001226 /* In case the user of sctp_connectx() wants an association
1227 * id back, assign one now.
1228 */
1229 if (assoc_id) {
1230 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1231 if (err < 0)
1232 goto out_free;
1233 }
1234
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001235 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001236 if (err < 0) {
1237 goto out_free;
1238 }
1239
1240 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001241 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001242 sp->pf->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001243 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001244
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001245 /* in-kernel sockets don't generally have a file allocated to them
1246 * if all they do is call sock_create_kern().
1247 */
1248 if (sk->sk_socket->file)
1249 f_flags = sk->sk_socket->file->f_flags;
1250
1251 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1252
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001253 if (assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001254 *assoc_id = asoc->assoc_id;
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001255 err = sctp_wait_for_connect(asoc, &timeo);
1256 /* Note: the asoc may be freed after the return of
1257 * sctp_wait_for_connect.
1258 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001259
1260 /* Don't free association on exit. */
1261 asoc = NULL;
1262
1263out_free:
Daniel Borkmannbb333812013-06-28 19:49:40 +02001264 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1265 __func__, asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001266
Neil Horman2eebc1e2012-07-16 09:13:51 +00001267 if (asoc) {
1268 /* sctp_primitive_ASSOCIATE may have added this association
1269 * To the hash table, try to unhash it, just in case, its a noop
1270 * if it wasn't hashed so we're safe
1271 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001272 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001273 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001274 return err;
1275}
1276
1277/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1278 *
1279 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001280 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1281 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001282 *
1283 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1284 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1285 * or IPv6 addresses.
1286 *
1287 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1288 * Section 3.1.2 for this usage.
1289 *
1290 * addrs is a pointer to an array of one or more socket addresses. Each
1291 * address is contained in its appropriate structure (i.e. struct
1292 * sockaddr_in or struct sockaddr_in6) the family of the address type
1293 * must be used to distengish the address length (note that this
1294 * representation is termed a "packed array" of addresses). The caller
1295 * specifies the number of addresses in the array with addrcnt.
1296 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001297 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1298 * the association id of the new association. On failure, sctp_connectx()
1299 * returns -1, and sets errno to the appropriate error code. The assoc_id
1300 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001301 *
1302 * For SCTP, the port given in each socket address must be the same, or
1303 * sctp_connectx() will fail, setting errno to EINVAL.
1304 *
1305 * An application can use sctp_connectx to initiate an association with
1306 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1307 * allows a caller to specify multiple addresses at which a peer can be
1308 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001309 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001310 * specifies that the stack will try to make use of all the addresses in
1311 * the list when needed.
1312 *
1313 * Note that the list of addresses passed in is only used for setting up
1314 * the association. It does not necessarily equal the set of addresses
1315 * the peer uses for the resulting association. If the caller wants to
1316 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1317 * retrieve them after the association has been set up.
1318 *
1319 * Basically do nothing but copying the addresses from user to kernel
1320 * land and invoking either sctp_connectx(). This is used for tunneling
1321 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1322 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07001323 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1324 * it.
1325 *
1326 * sk The sk of the socket
1327 * addrs The pointer to the addresses in user land
1328 * addrssize Size of the addrs buffer
1329 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001330 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001331 */
wangweidong26ac8e52013-12-23 12:16:51 +08001332static int __sctp_setsockopt_connectx(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001333 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001334 int addrs_size,
1335 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001336{
Frank Filz3f7a87d2005-06-20 13:14:57 -07001337 struct sockaddr *kaddrs;
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02001338 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001339
Daniel Borkmannbb333812013-06-28 19:49:40 +02001340 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1341 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001342
1343 if (unlikely(addrs_size <= 0))
1344 return -EINVAL;
1345
Al Viroc981f252018-01-07 13:19:09 -05001346 kaddrs = vmemdup_user(addrs, addrs_size);
1347 if (unlikely(IS_ERR(kaddrs)))
1348 return PTR_ERR(kaddrs);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001349
Al Viroc981f252018-01-07 13:19:09 -05001350 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
1351 kvfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001352
Frank Filz3f7a87d2005-06-20 13:14:57 -07001353 return err;
1354}
1355
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001356/*
1357 * This is an older interface. It's kept for backward compatibility
1358 * to the option that doesn't provide association id.
1359 */
wangweidong26ac8e52013-12-23 12:16:51 +08001360static int sctp_setsockopt_connectx_old(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001361 struct sockaddr __user *addrs,
1362 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001363{
1364 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1365}
1366
1367/*
1368 * New interface for the API. The since the API is done with a socket
1369 * option, to make it simple we feed back the association id is as a return
1370 * indication to the call. Error is always negative and association id is
1371 * always positive.
1372 */
wangweidong26ac8e52013-12-23 12:16:51 +08001373static int sctp_setsockopt_connectx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001374 struct sockaddr __user *addrs,
1375 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001376{
1377 sctp_assoc_t assoc_id = 0;
1378 int err = 0;
1379
1380 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1381
1382 if (err)
1383 return err;
1384 else
1385 return assoc_id;
1386}
1387
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001388/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001389 * New (hopefully final) interface for the API.
1390 * We use the sctp_getaddrs_old structure so that use-space library
Daniel Borkmannffd59392014-02-17 12:11:11 +01001391 * can avoid any unnecessary allocations. The only different part
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001392 * is that we store the actual length of the address buffer into the
Daniel Borkmannffd59392014-02-17 12:11:11 +01001393 * addrs_num structure member. That way we can re-use the existing
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001394 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001395 */
Daniel Borkmannffd59392014-02-17 12:11:11 +01001396#ifdef CONFIG_COMPAT
1397struct compat_sctp_getaddrs_old {
1398 sctp_assoc_t assoc_id;
1399 s32 addr_num;
1400 compat_uptr_t addrs; /* struct sockaddr * */
1401};
1402#endif
1403
wangweidong26ac8e52013-12-23 12:16:51 +08001404static int sctp_getsockopt_connectx3(struct sock *sk, int len,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001405 char __user *optval,
1406 int __user *optlen)
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001407{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001408 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001409 sctp_assoc_t assoc_id = 0;
1410 int err = 0;
1411
Daniel Borkmannffd59392014-02-17 12:11:11 +01001412#ifdef CONFIG_COMPAT
Andy Lutomirski96c0e0a2016-03-22 14:25:07 -07001413 if (in_compat_syscall()) {
Daniel Borkmannffd59392014-02-17 12:11:11 +01001414 struct compat_sctp_getaddrs_old param32;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001415
Daniel Borkmannffd59392014-02-17 12:11:11 +01001416 if (len < sizeof(param32))
1417 return -EINVAL;
1418 if (copy_from_user(&param32, optval, sizeof(param32)))
1419 return -EFAULT;
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001420
Daniel Borkmannffd59392014-02-17 12:11:11 +01001421 param.assoc_id = param32.assoc_id;
1422 param.addr_num = param32.addr_num;
1423 param.addrs = compat_ptr(param32.addrs);
1424 } else
1425#endif
1426 {
1427 if (len < sizeof(param))
1428 return -EINVAL;
1429 if (copy_from_user(&param, optval, sizeof(param)))
1430 return -EFAULT;
1431 }
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001432
Daniel Borkmannffd59392014-02-17 12:11:11 +01001433 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1434 param.addrs, param.addr_num,
1435 &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001436 if (err == 0 || err == -EINPROGRESS) {
1437 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1438 return -EFAULT;
1439 if (put_user(sizeof(assoc_id), optlen))
1440 return -EFAULT;
1441 }
1442
1443 return err;
1444}
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446/* API 3.1.4 close() - UDP Style Syntax
1447 * Applications use close() to perform graceful shutdown (as described in
1448 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1449 * by a UDP-style socket.
1450 *
1451 * The syntax is
1452 *
1453 * ret = close(int sd);
1454 *
1455 * sd - the socket descriptor of the associations to be closed.
1456 *
1457 * To gracefully shutdown a specific association represented by the
1458 * UDP-style socket, an application should use the sendmsg() call,
1459 * passing no user data, but including the appropriate flag in the
1460 * ancillary data (see Section xxxx).
1461 *
1462 * If sd in the close() call is a branched-off socket representing only
1463 * one association, the shutdown is performed on that association only.
1464 *
1465 * 4.1.6 close() - TCP Style Syntax
1466 *
1467 * Applications use close() to gracefully close down an association.
1468 *
1469 * The syntax is:
1470 *
1471 * int close(int sd);
1472 *
1473 * sd - the socket descriptor of the association to be closed.
1474 *
1475 * After an application calls close() on a socket descriptor, no further
1476 * socket operations will succeed on that descriptor.
1477 *
1478 * API 7.1.4 SO_LINGER
1479 *
1480 * An application using the TCP-style socket can use this option to
1481 * perform the SCTP ABORT primitive. The linger option structure is:
1482 *
1483 * struct linger {
1484 * int l_onoff; // option on/off
1485 * int l_linger; // linger time
1486 * };
1487 *
1488 * To enable the option, set l_onoff to 1. If the l_linger value is set
1489 * to 0, calling close() is the same as the ABORT primitive. If the
1490 * value is set to a negative value, the setsockopt() call will return
1491 * an error. If the value is set to a positive value linger_time, the
1492 * close() can be blocked for at most linger_time ms. If the graceful
1493 * shutdown phase does not finish during this period, close() will
1494 * return but the graceful shutdown phase continues in the system.
1495 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02001496static void sctp_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001498 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 struct sctp_endpoint *ep;
1500 struct sctp_association *asoc;
1501 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001502 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Daniel Borkmannbb333812013-06-28 19:49:40 +02001504 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Xin Long6dfe4b92017-06-10 14:56:56 +08001506 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 sk->sk_shutdown = SHUTDOWN_MASK;
Yafang Shaocbabf462017-12-20 11:12:54 +08001508 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 ep = sctp_sk(sk)->ep;
1511
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001512 /* Clean up any skbs sitting on the receive queue. */
1513 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1514 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1515
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001516 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 list_for_each_safe(pos, temp, &ep->asocs) {
1518 asoc = list_entry(pos, struct sctp_association, asocs);
1519
1520 if (sctp_style(sk, TCP)) {
1521 /* A closed association can still be in the list if
1522 * it belongs to a TCP-style listening socket that is
1523 * not yet accepted. If so, free it. If not, send an
1524 * ABORT or SHUTDOWN based on the linger options.
1525 */
1526 if (sctp_state(asoc, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001528 continue;
1529 }
1530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001532 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1533 !skb_queue_empty(&asoc->ulpq.reasm) ||
Xin Long13228232017-12-08 21:04:09 +08001534 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001535 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001536 struct sctp_chunk *chunk;
1537
1538 chunk = sctp_make_abort_user(asoc, NULL, 0);
Xin Long068d8bd2015-12-29 17:49:25 +08001539 sctp_primitive_ABORT(net, asoc, chunk);
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001540 } else
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001541 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /* On a TCP-style socket, block for at most linger_time if set. */
1545 if (sctp_style(sk, TCP) && timeout)
1546 sctp_wait_for_close(sk, timeout);
1547
1548 /* This will run the backlog queue. */
wangweidong048ed4b2014-01-21 15:44:11 +08001549 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 /* Supposedly, no process has access to the socket, but
1552 * the net layers still may.
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001553 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1554 * held and that should be grabbed before socket lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001556 spin_lock_bh(&net->sctp.addr_wq_lock);
Xin Long6dfe4b92017-06-10 14:56:56 +08001557 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 /* Hold the sock, since sk_common_release() will put sock_put()
1560 * and we have just a little more cleanup.
1561 */
1562 sock_hold(sk);
1563 sk_common_release(sk);
1564
wangweidong5bc1d1b2014-01-21 15:44:12 +08001565 bh_unlock_sock(sk);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001566 spin_unlock_bh(&net->sctp.addr_wq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 sock_put(sk);
1569
1570 SCTP_DBG_OBJCNT_DEC(sock);
1571}
1572
1573/* Handle EPIPE error. */
1574static int sctp_error(struct sock *sk, int flags, int err)
1575{
1576 if (err == -EPIPE)
1577 err = sock_error(sk) ? : -EPIPE;
1578 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1579 send_sig(SIGPIPE, current, 0);
1580 return err;
1581}
1582
1583/* API 3.1.3 sendmsg() - UDP Style Syntax
1584 *
1585 * An application uses sendmsg() and recvmsg() calls to transmit data to
1586 * and receive data from its peer.
1587 *
1588 * ssize_t sendmsg(int socket, const struct msghdr *message,
1589 * int flags);
1590 *
1591 * socket - the socket descriptor of the endpoint.
1592 * message - pointer to the msghdr structure which contains a single
1593 * user message and possibly some ancillary data.
1594 *
1595 * See Section 5 for complete description of the data
1596 * structures.
1597 *
1598 * flags - flags sent or received with the user message, see Section
1599 * 5 for complete description of the flags.
1600 *
1601 * Note: This function could use a rewrite especially when explicit
1602 * connect support comes in.
1603 */
1604/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1605
Xin Longa05437a2017-08-11 10:23:48 +08001606static int sctp_msghdr_parse(const struct msghdr *msg,
1607 struct sctp_cmsgs *cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Xin Long204f8172018-03-01 23:05:14 +08001609static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1610 struct sctp_sndrcvinfo *srinfo,
1611 const struct msghdr *msg, size_t msg_len)
1612{
1613 __u16 sflags;
1614 int err;
1615
1616 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1617 return -EPIPE;
1618
1619 if (msg_len > sk->sk_sndbuf)
1620 return -EMSGSIZE;
1621
1622 memset(cmsgs, 0, sizeof(*cmsgs));
1623 err = sctp_msghdr_parse(msg, cmsgs);
1624 if (err) {
1625 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1626 return err;
1627 }
1628
1629 memset(srinfo, 0, sizeof(*srinfo));
1630 if (cmsgs->srinfo) {
1631 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1632 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1633 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1634 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1635 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1636 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1637 }
1638
1639 if (cmsgs->sinfo) {
1640 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1641 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1642 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1643 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1644 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1645 }
1646
Xin Longed63afb2018-03-05 20:44:18 +08001647 if (cmsgs->prinfo) {
1648 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1649 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1650 cmsgs->prinfo->pr_policy);
1651 }
1652
Xin Long204f8172018-03-01 23:05:14 +08001653 sflags = srinfo->sinfo_flags;
1654 if (!sflags && msg_len)
1655 return 0;
1656
1657 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1658 return -EINVAL;
1659
1660 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1661 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1662 return -EINVAL;
1663
1664 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1665 return -EINVAL;
1666
1667 return 0;
1668}
1669
Xin Long2bfd80f2018-03-01 23:05:11 +08001670static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1671 struct sctp_cmsgs *cmsgs,
1672 union sctp_addr *daddr,
1673 struct sctp_transport **tp)
1674{
1675 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1676 struct net *net = sock_net(sk);
1677 struct sctp_association *asoc;
1678 enum sctp_scope scope;
Xin Long2c0dbaa2018-03-05 20:44:19 +08001679 struct cmsghdr *cmsg;
Xin Long2bfd80f2018-03-01 23:05:11 +08001680 int err = -EINVAL;
1681
1682 *tp = NULL;
1683
1684 if (sflags & (SCTP_EOF | SCTP_ABORT))
1685 return -EINVAL;
1686
1687 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1688 sctp_sstate(sk, CLOSING)))
1689 return -EADDRNOTAVAIL;
1690
1691 if (sctp_endpoint_is_peeled_off(ep, daddr))
1692 return -EADDRNOTAVAIL;
1693
1694 if (!ep->base.bind_addr.port) {
1695 if (sctp_autobind(sk))
1696 return -EAGAIN;
1697 } else {
1698 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1699 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1700 return -EACCES;
1701 }
1702
1703 scope = sctp_scope(daddr);
1704
1705 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1706 if (!asoc)
1707 return -ENOMEM;
1708
1709 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1710 err = -ENOMEM;
1711 goto free;
1712 }
1713
1714 if (cmsgs->init) {
1715 struct sctp_initmsg *init = cmsgs->init;
1716
1717 if (init->sinit_num_ostreams) {
1718 __u16 outcnt = init->sinit_num_ostreams;
1719
1720 asoc->c.sinit_num_ostreams = outcnt;
1721 /* outcnt has been changed, need to re-init stream */
1722 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1723 GFP_KERNEL);
1724 if (err)
1725 goto free;
1726 }
1727
1728 if (init->sinit_max_instreams)
1729 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1730
1731 if (init->sinit_max_attempts)
1732 asoc->max_init_attempts = init->sinit_max_attempts;
1733
1734 if (init->sinit_max_init_timeo)
1735 asoc->max_init_timeo =
1736 msecs_to_jiffies(init->sinit_max_init_timeo);
1737 }
1738
1739 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1740 if (!*tp) {
1741 err = -ENOMEM;
1742 goto free;
1743 }
1744
Xin Long2c0dbaa2018-03-05 20:44:19 +08001745 if (!cmsgs->addrs_msg)
1746 return 0;
1747
1748 /* sendv addr list parse */
1749 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1750 struct sctp_transport *transport;
1751 struct sctp_association *old;
1752 union sctp_addr _daddr;
1753 int dlen;
1754
1755 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1756 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1757 cmsg->cmsg_type != SCTP_DSTADDRV6))
1758 continue;
1759
1760 daddr = &_daddr;
1761 memset(daddr, 0, sizeof(*daddr));
1762 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1763 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
1764 if (dlen < sizeof(struct in_addr))
1765 goto free;
1766
1767 dlen = sizeof(struct in_addr);
1768 daddr->v4.sin_family = AF_INET;
1769 daddr->v4.sin_port = htons(asoc->peer.port);
1770 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1771 } else {
1772 if (dlen < sizeof(struct in6_addr))
1773 goto free;
1774
1775 dlen = sizeof(struct in6_addr);
1776 daddr->v6.sin6_family = AF_INET6;
1777 daddr->v6.sin6_port = htons(asoc->peer.port);
1778 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1779 }
1780 err = sctp_verify_addr(sk, daddr, sizeof(*daddr));
1781 if (err)
1782 goto free;
1783
1784 old = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1785 if (old && old != asoc) {
1786 if (old->state >= SCTP_STATE_ESTABLISHED)
1787 err = -EISCONN;
1788 else
1789 err = -EALREADY;
1790 goto free;
1791 }
1792
1793 if (sctp_endpoint_is_peeled_off(ep, daddr)) {
1794 err = -EADDRNOTAVAIL;
1795 goto free;
1796 }
1797
1798 transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL,
1799 SCTP_UNKNOWN);
1800 if (!transport) {
1801 err = -ENOMEM;
1802 goto free;
1803 }
1804 }
1805
Xin Long2bfd80f2018-03-01 23:05:11 +08001806 return 0;
1807
1808free:
1809 sctp_association_free(asoc);
1810 return err;
1811}
1812
Xin Longc2666de2018-03-01 23:05:12 +08001813static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1814 __u16 sflags, struct msghdr *msg,
1815 size_t msg_len)
1816{
1817 struct sock *sk = asoc->base.sk;
1818 struct net *net = sock_net(sk);
1819
1820 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1821 return -EPIPE;
1822
1823 if (sflags & SCTP_EOF) {
1824 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1825 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1826
1827 return 0;
1828 }
1829
1830 if (sflags & SCTP_ABORT) {
1831 struct sctp_chunk *chunk;
1832
1833 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1834 if (!chunk)
1835 return -ENOMEM;
1836
1837 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1838 sctp_primitive_ABORT(net, asoc, chunk);
1839
1840 return 0;
1841 }
1842
1843 return 1;
1844}
1845
Xin Longf84af332018-03-01 23:05:10 +08001846static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1847 struct msghdr *msg, size_t msg_len,
1848 struct sctp_transport *transport,
1849 struct sctp_sndrcvinfo *sinfo)
1850{
1851 struct sock *sk = asoc->base.sk;
1852 struct net *net = sock_net(sk);
1853 struct sctp_datamsg *datamsg;
1854 bool wait_connect = false;
1855 struct sctp_chunk *chunk;
1856 long timeo;
1857 int err;
1858
1859 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1860 err = -EINVAL;
1861 goto err;
1862 }
1863
1864 if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) {
1865 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1866 if (err)
1867 goto err;
1868 }
1869
1870 if (sctp_sk(sk)->disable_fragments && msg_len > asoc->frag_point) {
1871 err = -EMSGSIZE;
1872 goto err;
1873 }
1874
1875 if (sctp_state(asoc, CLOSED)) {
1876 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1877 if (err)
1878 goto err;
1879
1880 if (sctp_sk(sk)->strm_interleave) {
1881 timeo = sock_sndtimeo(sk, 0);
1882 err = sctp_wait_for_connect(asoc, &timeo);
1883 if (err)
1884 goto err;
1885 } else {
1886 wait_connect = true;
1887 }
1888
1889 pr_debug("%s: we associated primitively\n", __func__);
1890 }
1891
1892 if (asoc->pmtu_pending)
1893 sctp_assoc_pending_pmtu(asoc);
1894
1895 if (sctp_wspace(asoc) < msg_len)
1896 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1897
1898 if (!sctp_wspace(asoc)) {
1899 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1900 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1901 if (err)
1902 goto err;
1903 }
1904
1905 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1906 if (IS_ERR(datamsg)) {
1907 err = PTR_ERR(datamsg);
1908 goto err;
1909 }
1910
1911 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1912
1913 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1914 sctp_chunk_hold(chunk);
1915 sctp_set_owner_w(chunk);
1916 chunk->transport = transport;
1917 }
1918
1919 err = sctp_primitive_SEND(net, asoc, datamsg);
1920 if (err) {
1921 sctp_datamsg_free(datamsg);
1922 goto err;
1923 }
1924
1925 pr_debug("%s: we sent primitively\n", __func__);
1926
1927 sctp_datamsg_put(datamsg);
1928
1929 if (unlikely(wait_connect)) {
1930 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1931 sctp_wait_for_connect(asoc, &timeo);
1932 }
1933
1934 err = msg_len;
1935
1936err:
1937 return err;
1938}
1939
Xin Longbecef9b2018-03-01 23:05:13 +08001940static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1941 const struct msghdr *msg,
1942 struct sctp_cmsgs *cmsgs)
1943{
1944 union sctp_addr *daddr = NULL;
1945 int err;
1946
1947 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1948 int len = msg->msg_namelen;
1949
1950 if (len > sizeof(*daddr))
1951 len = sizeof(*daddr);
1952
1953 daddr = (union sctp_addr *)msg->msg_name;
1954
1955 err = sctp_verify_addr(sk, daddr, len);
1956 if (err)
1957 return ERR_PTR(err);
1958 }
1959
1960 return daddr;
1961}
1962
Xin Longd42cb062018-03-01 23:05:15 +08001963static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
1964 struct sctp_sndrcvinfo *sinfo,
1965 struct sctp_cmsgs *cmsgs)
1966{
1967 if (!cmsgs->srinfo && !cmsgs->sinfo) {
1968 sinfo->sinfo_stream = asoc->default_stream;
1969 sinfo->sinfo_ppid = asoc->default_ppid;
1970 sinfo->sinfo_context = asoc->default_context;
1971 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
Xin Longed63afb2018-03-05 20:44:18 +08001972
1973 if (!cmsgs->prinfo)
1974 sinfo->sinfo_flags = asoc->default_flags;
Xin Longd42cb062018-03-01 23:05:15 +08001975 }
1976
Xin Longed63afb2018-03-05 20:44:18 +08001977 if (!cmsgs->srinfo && !cmsgs->prinfo)
Xin Longd42cb062018-03-01 23:05:15 +08001978 sinfo->sinfo_timetolive = asoc->default_timetolive;
1979}
1980
Ying Xue1b784142015-03-02 15:37:48 +08001981static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Xin Long204f8172018-03-01 23:05:14 +08001983 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Xin Long8e87c6e2018-03-01 23:05:16 +08001984 struct sctp_transport *transport = NULL;
Xin Long204f8172018-03-01 23:05:14 +08001985 struct sctp_sndrcvinfo _sinfo, *sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08001986 struct sctp_association *asoc;
1987 struct sctp_cmsgs cmsgs;
Xin Longbecef9b2018-03-01 23:05:13 +08001988 union sctp_addr *daddr;
Xin Long007b7e12018-03-01 23:05:17 +08001989 bool new = false;
1990 __u16 sflags;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001991 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Xin Long204f8172018-03-01 23:05:14 +08001993 /* Parse and get snd_info */
1994 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
1995 if (err)
Xin Long007b7e12018-03-01 23:05:17 +08001996 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Xin Long204f8172018-03-01 23:05:14 +08001998 sinfo = &_sinfo;
Xin Long007b7e12018-03-01 23:05:17 +08001999 sflags = sinfo->sinfo_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Xin Longbecef9b2018-03-01 23:05:13 +08002001 /* Get daddr from msg */
2002 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
2003 if (IS_ERR(daddr)) {
2004 err = PTR_ERR(daddr);
Xin Long007b7e12018-03-01 23:05:17 +08002005 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
2007
wangweidong048ed4b2014-01-21 15:44:11 +08002008 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Xin Long0a3920d2018-03-01 23:05:18 +08002010 /* Get and check or create asoc */
Xin Longbecef9b2018-03-01 23:05:13 +08002011 if (daddr) {
Xin Longbecef9b2018-03-01 23:05:13 +08002012 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
Xin Long0a3920d2018-03-01 23:05:18 +08002013 if (asoc) {
2014 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2015 msg_len);
2016 if (err <= 0)
2017 goto out_unlock;
2018 } else {
2019 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2020 &transport);
2021 if (err)
2022 goto out_unlock;
2023
2024 asoc = transport->asoc;
2025 new = true;
2026 }
2027
2028 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2029 transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 } else {
Xin Long007b7e12018-03-01 23:05:17 +08002031 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (!asoc) {
2033 err = -EPIPE;
2034 goto out_unlock;
2035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Xin Long007b7e12018-03-01 23:05:17 +08002037 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
Xin Longc2666de2018-03-01 23:05:12 +08002038 if (err <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
2041
Xin Longd42cb062018-03-01 23:05:15 +08002042 /* Update snd_info with the asoc */
2043 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Xin Longf84af332018-03-01 23:05:10 +08002045 /* Send msg to the asoc */
Xin Long8e87c6e2018-03-01 23:05:16 +08002046 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
Xin Long007b7e12018-03-01 23:05:17 +08002047 if (err < 0 && err != -ESRCH && new)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 sctp_association_free(asoc);
Xin Long8e87c6e2018-03-01 23:05:16 +08002049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050out_unlock:
wangweidong048ed4b2014-01-21 15:44:11 +08002051 release_sock(sk);
Xin Long007b7e12018-03-01 23:05:17 +08002052out:
Xin Longf84af332018-03-01 23:05:10 +08002053 return sctp_error(sk, msg->msg_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
2056/* This is an extended version of skb_pull() that removes the data from the
2057 * start of a skb even when data is spread across the list of skb's in the
2058 * frag_list. len specifies the total amount of data that needs to be removed.
2059 * when 'len' bytes could be removed from the skb, it returns 0.
2060 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2061 * could not be removed.
2062 */
2063static int sctp_skb_pull(struct sk_buff *skb, int len)
2064{
2065 struct sk_buff *list;
2066 int skb_len = skb_headlen(skb);
2067 int rlen;
2068
2069 if (len <= skb_len) {
2070 __skb_pull(skb, len);
2071 return 0;
2072 }
2073 len -= skb_len;
2074 __skb_pull(skb, skb_len);
2075
David S. Miller1b003be2009-06-09 00:22:35 -07002076 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 rlen = sctp_skb_pull(list, len);
2078 skb->len -= (len-rlen);
2079 skb->data_len -= (len-rlen);
2080
2081 if (!rlen)
2082 return 0;
2083
2084 len = rlen;
2085 }
2086
2087 return len;
2088}
2089
2090/* API 3.1.3 recvmsg() - UDP Style Syntax
2091 *
2092 * ssize_t recvmsg(int socket, struct msghdr *message,
2093 * int flags);
2094 *
2095 * socket - the socket descriptor of the endpoint.
2096 * message - pointer to the msghdr structure which contains a single
2097 * user message and possibly some ancillary data.
2098 *
2099 * See Section 5 for complete description of the data
2100 * structures.
2101 *
2102 * flags - flags sent or received with the user message, see Section
2103 * 5 for complete description of the flags.
2104 */
Ying Xue1b784142015-03-02 15:37:48 +08002105static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2106 int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
2108 struct sctp_ulpevent *event = NULL;
2109 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002110 struct sk_buff *skb, *head_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 int copied;
2112 int err = 0;
2113 int skb_len;
2114
Daniel Borkmannbb333812013-06-28 19:49:40 +02002115 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2116 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2117 addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
wangweidong048ed4b2014-01-21 15:44:11 +08002119 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03002121 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
Xin Longe0878692016-07-30 14:14:41 +08002122 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 err = -ENOTCONN;
2124 goto out;
2125 }
2126
2127 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2128 if (!skb)
2129 goto out;
2130
2131 /* Get the total length of the skb including any skb's in the
2132 * frag_list.
2133 */
2134 skb_len = skb->len;
2135
2136 copied = skb_len;
2137 if (copied > len)
2138 copied = len;
2139
David S. Miller51f3d022014-11-05 16:46:40 -05002140 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 event = sctp_skb2event(skb);
2143
2144 if (err)
2145 goto out_free;
2146
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002147 if (event->chunk && event->chunk->head_skb)
2148 head_skb = event->chunk->head_skb;
2149 else
2150 head_skb = skb;
2151 sock_recv_ts_and_drops(msg, sk, head_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (sctp_ulpevent_is_notification(event)) {
2153 msg->msg_flags |= MSG_NOTIFICATION;
2154 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2155 } else {
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002156 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
2158
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02002159 /* Check if we allow SCTP_NXTINFO. */
2160 if (sp->recvnxtinfo)
2161 sctp_ulpevent_read_nxtinfo(event, msg, sk);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002162 /* Check if we allow SCTP_RCVINFO. */
2163 if (sp->recvrcvinfo)
2164 sctp_ulpevent_read_rcvinfo(event, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 /* Check if we allow SCTP_SNDRCVINFO. */
2166 if (sp->subscribe.sctp_data_io_event)
2167 sctp_ulpevent_read_sndrcvinfo(event, msg);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 err = copied;
2170
2171 /* If skb's length exceeds the user's buffer, update the skb and
2172 * push it back to the receive_queue so that the next call to
2173 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2174 */
2175 if (skb_len > copied) {
2176 msg->msg_flags &= ~MSG_EOR;
2177 if (flags & MSG_PEEK)
2178 goto out_free;
2179 sctp_skb_pull(skb, copied);
2180 skb_queue_head(&sk->sk_receive_queue, skb);
2181
Daniel Borkmann362d5202014-04-14 21:45:17 +02002182 /* When only partial message is copied to the user, increase
2183 * rwnd by that amount. If all the data in the skb is read,
2184 * rwnd is updated when the event is freed.
2185 */
2186 if (!sctp_ulpevent_is_notification(event))
2187 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 goto out;
2189 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2190 (event->msg_flags & MSG_EOR))
2191 msg->msg_flags |= MSG_EOR;
2192 else
2193 msg->msg_flags &= ~MSG_EOR;
2194
2195out_free:
2196 if (flags & MSG_PEEK) {
2197 /* Release the skb reference acquired after peeking the skb in
2198 * sctp_skb_recv_datagram().
2199 */
2200 kfree_skb(skb);
2201 } else {
2202 /* Free the event which includes releasing the reference to
2203 * the owner of the skb, freeing the skb and updating the
2204 * rwnd.
2205 */
2206 sctp_ulpevent_free(event);
2207 }
2208out:
wangweidong048ed4b2014-01-21 15:44:11 +08002209 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return err;
2211}
2212
2213/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2214 *
2215 * This option is a on/off flag. If enabled no SCTP message
2216 * fragmentation will be performed. Instead if a message being sent
2217 * exceeds the current PMTU size, the message will NOT be sent and
2218 * instead a error will be indicated to the user.
2219 */
2220static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002221 char __user *optval,
2222 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
2224 int val;
2225
2226 if (optlen < sizeof(int))
2227 return -EINVAL;
2228
2229 if (get_user(val, (int __user *)optval))
2230 return -EFAULT;
2231
2232 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2233
2234 return 0;
2235}
2236
2237static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002238 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Wei Yongjun94912302011-07-02 09:28:04 +00002240 struct sctp_association *asoc;
2241 struct sctp_ulpevent *event;
2242
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002243 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return -EINVAL;
2245 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2246 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002247
Daniel Borkmannbbbea412014-07-12 20:30:40 +02002248 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
Wei Yongjun94912302011-07-02 09:28:04 +00002249 * if there is no data to be sent or retransmit, the stack will
2250 * immediately send up this notification.
2251 */
2252 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2253 &sctp_sk(sk)->subscribe)) {
2254 asoc = sctp_id2assoc(sk, 0);
2255
2256 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2257 event = sctp_ulpevent_make_sender_dry_event(asoc,
Marcelo Ricardo Leitner2e83acb2018-01-08 19:02:27 -02002258 GFP_USER | __GFP_NOWARN);
Wei Yongjun94912302011-07-02 09:28:04 +00002259 if (!event)
2260 return -ENOMEM;
2261
Xin Long9162e0e2017-12-08 21:04:05 +08002262 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
Wei Yongjun94912302011-07-02 09:28:04 +00002263 }
2264 }
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return 0;
2267}
2268
2269/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2270 *
2271 * This socket option is applicable to the UDP-style socket only. When
2272 * set it will cause associations that are idle for more than the
2273 * specified number of seconds to automatically close. An association
2274 * being idle is defined an association that has NOT sent or received
2275 * user data. The special value of '0' indicates that no automatic
2276 * close of any associations should be performed. The option expects an
2277 * integer defining the number of seconds of idle time before an
2278 * association is closed.
2279 */
2280static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002281 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
2283 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman9f70f462013-12-10 06:48:15 -05002284 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 /* Applicable to UDP-style socket only */
2287 if (sctp_style(sk, TCP))
2288 return -EOPNOTSUPP;
2289 if (optlen != sizeof(int))
2290 return -EINVAL;
2291 if (copy_from_user(&sp->autoclose, optval, optlen))
2292 return -EFAULT;
2293
Neil Horman9f70f462013-12-10 06:48:15 -05002294 if (sp->autoclose > net->sctp.max_autoclose)
2295 sp->autoclose = net->sctp.max_autoclose;
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return 0;
2298}
2299
2300/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2301 *
2302 * Applications can enable or disable heartbeats for any peer address of
2303 * an association, modify an address's heartbeat interval, force a
2304 * heartbeat to be sent immediately, and adjust the address's maximum
2305 * number of retransmissions sent before an address is considered
2306 * unreachable. The following structure is used to access and modify an
2307 * address's parameters:
2308 *
2309 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002310 * sctp_assoc_t spp_assoc_id;
2311 * struct sockaddr_storage spp_address;
2312 * uint32_t spp_hbinterval;
2313 * uint16_t spp_pathmaxrxt;
2314 * uint32_t spp_pathmtu;
2315 * uint32_t spp_sackdelay;
2316 * uint32_t spp_flags;
2317 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002319 * spp_assoc_id - (one-to-many style socket) This is filled in the
2320 * application, and identifies the association for
2321 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 * spp_address - This specifies which address is of interest.
2323 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002324 * in milliseconds. If a value of zero
2325 * is present in this field then no changes are to
2326 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 * spp_pathmaxrxt - This contains the maximum number of
2328 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002329 * considered unreachable. If a value of zero
2330 * is present in this field then no changes are to
2331 * be made to this parameter.
2332 * spp_pathmtu - When Path MTU discovery is disabled the value
2333 * specified here will be the "fixed" path mtu.
2334 * Note that if the spp_address field is empty
2335 * then all associations on this address will
2336 * have this fixed path mtu set upon them.
2337 *
2338 * spp_sackdelay - When delayed sack is enabled, this value specifies
2339 * the number of milliseconds that sacks will be delayed
2340 * for. This value will apply to all addresses of an
2341 * association if the spp_address field is empty. Note
2342 * also, that if delayed sack is enabled and this
2343 * value is set to 0, no change is made to the last
2344 * recorded delayed sack timer value.
2345 *
2346 * spp_flags - These flags are used to control various features
2347 * on an association. The flag field may contain
2348 * zero or more of the following options.
2349 *
2350 * SPP_HB_ENABLE - Enable heartbeats on the
2351 * specified address. Note that if the address
2352 * field is empty all addresses for the association
2353 * have heartbeats enabled upon them.
2354 *
2355 * SPP_HB_DISABLE - Disable heartbeats on the
2356 * speicifed address. Note that if the address
2357 * field is empty all addresses for the association
2358 * will have their heartbeats disabled. Note also
2359 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2360 * mutually exclusive, only one of these two should
2361 * be specified. Enabling both fields will have
2362 * undetermined results.
2363 *
2364 * SPP_HB_DEMAND - Request a user initiated heartbeat
2365 * to be made immediately.
2366 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002367 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2368 * heartbeat delayis to be set to the value of 0
2369 * milliseconds.
2370 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002371 * SPP_PMTUD_ENABLE - This field will enable PMTU
2372 * discovery upon the specified address. Note that
2373 * if the address feild is empty then all addresses
2374 * on the association are effected.
2375 *
2376 * SPP_PMTUD_DISABLE - This field will disable PMTU
2377 * discovery upon the specified address. Note that
2378 * if the address feild is empty then all addresses
2379 * on the association are effected. Not also that
2380 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2381 * exclusive. Enabling both will have undetermined
2382 * results.
2383 *
2384 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2385 * on delayed sack. The time specified in spp_sackdelay
2386 * is used to specify the sack delay for this address. Note
2387 * that if spp_address is empty then all addresses will
2388 * enable delayed sack and take on the sack delay
2389 * value specified in spp_sackdelay.
2390 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2391 * off delayed sack. If the spp_address field is blank then
2392 * delayed sack is disabled for the entire association. Note
2393 * also that this field is mutually exclusive to
2394 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2395 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 */
Adrian Bunk16164362006-09-18 00:40:38 -07002397static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2398 struct sctp_transport *trans,
2399 struct sctp_association *asoc,
2400 struct sctp_sock *sp,
2401 int hb_change,
2402 int pmtud_change,
2403 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 int error;
2406
Frank Filz52ccb8e2005-12-22 11:36:46 -08002407 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002408 struct net *net = sock_net(trans->asoc->base.sk);
2409
2410 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (error)
2412 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 }
2414
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002415 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2416 * this field is ignored. Note also that a value of zero indicates
2417 * the current setting should be left unchanged.
2418 */
2419 if (params->spp_flags & SPP_HB_ENABLE) {
2420
2421 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2422 * set. This lets us use 0 value when this flag
2423 * is set.
2424 */
2425 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2426 params->spp_hbinterval = 0;
2427
2428 if (params->spp_hbinterval ||
2429 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2430 if (trans) {
2431 trans->hbinterval =
2432 msecs_to_jiffies(params->spp_hbinterval);
2433 } else if (asoc) {
2434 asoc->hbinterval =
2435 msecs_to_jiffies(params->spp_hbinterval);
2436 } else {
2437 sp->hbinterval = params->spp_hbinterval;
2438 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002439 }
2440 }
2441
2442 if (hb_change) {
2443 if (trans) {
2444 trans->param_flags =
2445 (trans->param_flags & ~SPP_HB) | hb_change;
2446 } else if (asoc) {
2447 asoc->param_flags =
2448 (asoc->param_flags & ~SPP_HB) | hb_change;
2449 } else {
2450 sp->param_flags =
2451 (sp->param_flags & ~SPP_HB) | hb_change;
2452 }
2453 }
2454
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002455 /* When Path MTU discovery is disabled the value specified here will
2456 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2457 * include the flag SPP_PMTUD_DISABLE for this field to have any
2458 * effect).
2459 */
2460 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002461 if (trans) {
2462 trans->pathmtu = params->spp_pathmtu;
Xin Long3ebfdf02017-04-04 13:39:55 +08002463 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002464 } else if (asoc) {
2465 asoc->pathmtu = params->spp_pathmtu;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002466 } else {
2467 sp->pathmtu = params->spp_pathmtu;
2468 }
2469 }
2470
2471 if (pmtud_change) {
2472 if (trans) {
2473 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2474 (params->spp_flags & SPP_PMTUD_ENABLE);
2475 trans->param_flags =
2476 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2477 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002478 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
Xin Long3ebfdf02017-04-04 13:39:55 +08002479 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002480 }
2481 } else if (asoc) {
2482 asoc->param_flags =
2483 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2484 } else {
2485 sp->param_flags =
2486 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2487 }
2488 }
2489
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002490 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2491 * value of this field is ignored. Note also that a value of zero
2492 * indicates the current setting should be left unchanged.
2493 */
2494 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002495 if (trans) {
2496 trans->sackdelay =
2497 msecs_to_jiffies(params->spp_sackdelay);
2498 } else if (asoc) {
2499 asoc->sackdelay =
2500 msecs_to_jiffies(params->spp_sackdelay);
2501 } else {
2502 sp->sackdelay = params->spp_sackdelay;
2503 }
2504 }
2505
2506 if (sackdelay_change) {
2507 if (trans) {
2508 trans->param_flags =
2509 (trans->param_flags & ~SPP_SACKDELAY) |
2510 sackdelay_change;
2511 } else if (asoc) {
2512 asoc->param_flags =
2513 (asoc->param_flags & ~SPP_SACKDELAY) |
2514 sackdelay_change;
2515 } else {
2516 sp->param_flags =
2517 (sp->param_flags & ~SPP_SACKDELAY) |
2518 sackdelay_change;
2519 }
2520 }
2521
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002522 /* Note that a value of zero indicates the current setting should be
2523 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002524 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002525 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002526 if (trans) {
2527 trans->pathmaxrxt = params->spp_pathmaxrxt;
2528 } else if (asoc) {
2529 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2530 } else {
2531 sp->pathmaxrxt = params->spp_pathmaxrxt;
2532 }
2533 }
2534
2535 return 0;
2536}
2537
2538static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002539 char __user *optval,
2540 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002541{
2542 struct sctp_paddrparams params;
2543 struct sctp_transport *trans = NULL;
2544 struct sctp_association *asoc = NULL;
2545 struct sctp_sock *sp = sctp_sk(sk);
2546 int error;
2547 int hb_change, pmtud_change, sackdelay_change;
2548
2549 if (optlen != sizeof(struct sctp_paddrparams))
wangweidongcb3f8372013-12-23 12:16:50 +08002550 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002551
2552 if (copy_from_user(&params, optval, optlen))
2553 return -EFAULT;
2554
2555 /* Validate flags and value parameters. */
2556 hb_change = params.spp_flags & SPP_HB;
2557 pmtud_change = params.spp_flags & SPP_PMTUD;
2558 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2559
2560 if (hb_change == SPP_HB ||
2561 pmtud_change == SPP_PMTUD ||
2562 sackdelay_change == SPP_SACKDELAY ||
2563 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002564 (params.spp_pathmtu &&
2565 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002566 return -EINVAL;
2567
2568 /* If an address other than INADDR_ANY is specified, and
2569 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 */
wangweidongcb3f8372013-12-23 12:16:50 +08002571 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002572 trans = sctp_addr_id2transport(sk, &params.spp_address,
2573 params.spp_assoc_id);
2574 if (!trans)
2575 return -EINVAL;
2576 }
2577
2578 /* Get association, if assoc_id != 0 and the socket is a one
2579 * to many style socket, and an association was not found, then
2580 * the id was invalid.
2581 */
2582 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2583 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2584 return -EINVAL;
2585
2586 /* Heartbeat demand can only be sent on a transport or
2587 * association, but not a socket.
2588 */
2589 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2590 return -EINVAL;
2591
2592 /* Process parameters. */
2593 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2594 hb_change, pmtud_change,
2595 sackdelay_change);
2596
2597 if (error)
2598 return error;
2599
2600 /* If changes are for association, also apply parameters to each
2601 * transport.
2602 */
2603 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002604 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2605 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002606 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2607 hb_change, pmtud_change,
2608 sackdelay_change);
2609 }
2610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
2612 return 0;
2613}
2614
wangweidong0ea5e4d2014-01-15 17:24:01 +08002615static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2616{
2617 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2618}
2619
2620static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2621{
2622 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2623}
2624
Wei Yongjund364d922008-05-09 15:13:26 -07002625/*
2626 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002627 *
Wei Yongjund364d922008-05-09 15:13:26 -07002628 * This option will effect the way delayed acks are performed. This
2629 * option allows you to get or set the delayed ack time, in
2630 * milliseconds. It also allows changing the delayed ack frequency.
2631 * Changing the frequency to 1 disables the delayed sack algorithm. If
2632 * the assoc_id is 0, then this sets or gets the endpoints default
2633 * values. If the assoc_id field is non-zero, then the set or get
2634 * effects the specified association for the one to many model (the
2635 * assoc_id field is ignored by the one to one model). Note that if
2636 * sack_delay or sack_freq are 0 when setting this option, then the
2637 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002638 *
Wei Yongjund364d922008-05-09 15:13:26 -07002639 * struct sctp_sack_info {
2640 * sctp_assoc_t sack_assoc_id;
2641 * uint32_t sack_delay;
2642 * uint32_t sack_freq;
2643 * };
Frank Filz77086102005-12-22 11:37:30 -08002644 *
Wei Yongjund364d922008-05-09 15:13:26 -07002645 * sack_assoc_id - This parameter, indicates which association the user
2646 * is performing an action upon. Note that if this field's value is
2647 * zero then the endpoints default value is changed (effecting future
2648 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002649 *
Wei Yongjund364d922008-05-09 15:13:26 -07002650 * sack_delay - This parameter contains the number of milliseconds that
2651 * the user is requesting the delayed ACK timer be set to. Note that
2652 * this value is defined in the standard to be between 200 and 500
2653 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002654 *
Wei Yongjund364d922008-05-09 15:13:26 -07002655 * sack_freq - This parameter contains the number of packets that must
2656 * be received before a sack is sent without waiting for the delay
2657 * timer to expire. The default value for this is 2, setting this
2658 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002659 */
2660
Wei Yongjund364d922008-05-09 15:13:26 -07002661static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002662 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002663{
Wei Yongjund364d922008-05-09 15:13:26 -07002664 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002665 struct sctp_transport *trans = NULL;
2666 struct sctp_association *asoc = NULL;
2667 struct sctp_sock *sp = sctp_sk(sk);
2668
Wei Yongjund364d922008-05-09 15:13:26 -07002669 if (optlen == sizeof(struct sctp_sack_info)) {
2670 if (copy_from_user(&params, optval, optlen))
2671 return -EFAULT;
2672
2673 if (params.sack_delay == 0 && params.sack_freq == 0)
2674 return 0;
2675 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05002676 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05002677 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05002678 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05002679 "Use struct sctp_sack_info instead\n",
2680 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07002681 if (copy_from_user(&params, optval, optlen))
2682 return -EFAULT;
2683
2684 if (params.sack_delay == 0)
2685 params.sack_freq = 1;
2686 else
2687 params.sack_freq = 0;
2688 } else
wangweidongcb3f8372013-12-23 12:16:50 +08002689 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08002690
Frank Filz77086102005-12-22 11:37:30 -08002691 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002692 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002693 return -EINVAL;
2694
Wei Yongjund364d922008-05-09 15:13:26 -07002695 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002696 * to many style socket, and an association was not found, then
2697 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002698 */
Wei Yongjund364d922008-05-09 15:13:26 -07002699 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2700 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002701 return -EINVAL;
2702
Wei Yongjund364d922008-05-09 15:13:26 -07002703 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002704 if (asoc) {
2705 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002706 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002707 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002708 sctp_spp_sackdelay_enable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002709 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002710 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002711 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002712 sctp_spp_sackdelay_enable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002713 }
Wei Yongjund364d922008-05-09 15:13:26 -07002714 }
2715
2716 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002717 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002718 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002719 sctp_spp_sackdelay_disable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002720 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002721 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002722 sctp_spp_sackdelay_disable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002723 }
Wei Yongjund364d922008-05-09 15:13:26 -07002724 } else if (params.sack_freq > 1) {
2725 if (asoc) {
2726 asoc->sackfreq = params.sack_freq;
2727 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002728 sctp_spp_sackdelay_enable(asoc->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002729 } else {
2730 sp->sackfreq = params.sack_freq;
2731 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002732 sctp_spp_sackdelay_enable(sp->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002733 }
Frank Filz77086102005-12-22 11:37:30 -08002734 }
2735
2736 /* If change is for association, also apply to each transport. */
2737 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002738 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2739 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002740 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002741 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002742 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002743 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002744 sctp_spp_sackdelay_enable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002745 }
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07002746 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002747 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002748 sctp_spp_sackdelay_disable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002749 } else if (params.sack_freq > 1) {
2750 trans->sackfreq = params.sack_freq;
2751 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002752 sctp_spp_sackdelay_enable(trans->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002753 }
2754 }
2755 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002756
Frank Filz77086102005-12-22 11:37:30 -08002757 return 0;
2758}
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2761 *
2762 * Applications can specify protocol parameters for the default association
2763 * initialization. The option name argument to setsockopt() and getsockopt()
2764 * is SCTP_INITMSG.
2765 *
2766 * Setting initialization parameters is effective only on an unconnected
2767 * socket (for UDP-style sockets only future associations are effected
2768 * by the change). With TCP-style sockets, this option is inherited by
2769 * sockets derived from a listener socket.
2770 */
David S. Millerb7058842009-09-30 16:12:20 -07002771static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
2773 struct sctp_initmsg sinit;
2774 struct sctp_sock *sp = sctp_sk(sk);
2775
2776 if (optlen != sizeof(struct sctp_initmsg))
2777 return -EINVAL;
2778 if (copy_from_user(&sinit, optval, optlen))
2779 return -EFAULT;
2780
2781 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002782 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002784 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002786 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002788 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
2790 return 0;
2791}
2792
2793/*
2794 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2795 *
2796 * Applications that wish to use the sendto() system call may wish to
2797 * specify a default set of parameters that would normally be supplied
2798 * through the inclusion of ancillary data. This socket option allows
2799 * such an application to set the default sctp_sndrcvinfo structure.
2800 * The application that wishes to use this socket option simply passes
2801 * in to this call the sctp_sndrcvinfo structure defined in Section
2802 * 5.2.2) The input parameters accepted by this call include
2803 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2804 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2805 * to this call if the caller is using the UDP model.
2806 */
2807static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002808 char __user *optval,
2809 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002812 struct sctp_association *asoc;
2813 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002815 if (optlen != sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return -EINVAL;
2817 if (copy_from_user(&info, optval, optlen))
2818 return -EFAULT;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002819 if (info.sinfo_flags &
2820 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2821 SCTP_ABORT | SCTP_EOF))
2822 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2825 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2826 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 if (asoc) {
2828 asoc->default_stream = info.sinfo_stream;
2829 asoc->default_flags = info.sinfo_flags;
2830 asoc->default_ppid = info.sinfo_ppid;
2831 asoc->default_context = info.sinfo_context;
2832 asoc->default_timetolive = info.sinfo_timetolive;
2833 } else {
2834 sp->default_stream = info.sinfo_stream;
2835 sp->default_flags = info.sinfo_flags;
2836 sp->default_ppid = info.sinfo_ppid;
2837 sp->default_context = info.sinfo_context;
2838 sp->default_timetolive = info.sinfo_timetolive;
2839 }
2840
2841 return 0;
2842}
2843
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002844/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2845 * (SCTP_DEFAULT_SNDINFO)
2846 */
2847static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2848 char __user *optval,
2849 unsigned int optlen)
2850{
2851 struct sctp_sock *sp = sctp_sk(sk);
2852 struct sctp_association *asoc;
2853 struct sctp_sndinfo info;
2854
2855 if (optlen != sizeof(info))
2856 return -EINVAL;
2857 if (copy_from_user(&info, optval, optlen))
2858 return -EFAULT;
2859 if (info.snd_flags &
2860 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2861 SCTP_ABORT | SCTP_EOF))
2862 return -EINVAL;
2863
2864 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
2865 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
2866 return -EINVAL;
2867 if (asoc) {
2868 asoc->default_stream = info.snd_sid;
2869 asoc->default_flags = info.snd_flags;
2870 asoc->default_ppid = info.snd_ppid;
2871 asoc->default_context = info.snd_context;
2872 } else {
2873 sp->default_stream = info.snd_sid;
2874 sp->default_flags = info.snd_flags;
2875 sp->default_ppid = info.snd_ppid;
2876 sp->default_context = info.snd_context;
2877 }
2878
2879 return 0;
2880}
2881
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2883 *
2884 * Requests that the local SCTP stack use the enclosed peer address as
2885 * the association primary. The enclosed address must be one of the
2886 * association peer's addresses.
2887 */
2888static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002889 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
2891 struct sctp_prim prim;
2892 struct sctp_transport *trans;
2893
2894 if (optlen != sizeof(struct sctp_prim))
2895 return -EINVAL;
2896
2897 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2898 return -EFAULT;
2899
2900 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2901 if (!trans)
2902 return -EINVAL;
2903
2904 sctp_assoc_set_primary(trans->asoc, trans);
2905
2906 return 0;
2907}
2908
2909/*
2910 * 7.1.5 SCTP_NODELAY
2911 *
2912 * Turn on/off any Nagle-like algorithm. This means that packets are
2913 * generally sent as soon as possible and no unnecessary delays are
2914 * introduced, at the cost of more packets in the network. Expects an
2915 * integer boolean flag.
2916 */
2917static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002918 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
2920 int val;
2921
2922 if (optlen < sizeof(int))
2923 return -EINVAL;
2924 if (get_user(val, (int __user *)optval))
2925 return -EFAULT;
2926
2927 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2928 return 0;
2929}
2930
2931/*
2932 *
2933 * 7.1.1 SCTP_RTOINFO
2934 *
2935 * The protocol parameters used to initialize and bound retransmission
2936 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2937 * and modify these parameters.
2938 * All parameters are time values, in milliseconds. A value of 0, when
2939 * modifying the parameters, indicates that the current value should not
2940 * be changed.
2941 *
2942 */
David S. Millerb7058842009-09-30 16:12:20 -07002943static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2944{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 struct sctp_rtoinfo rtoinfo;
2946 struct sctp_association *asoc;
wangweidong85f935d2013-12-11 09:50:38 +08002947 unsigned long rto_min, rto_max;
2948 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
2950 if (optlen != sizeof (struct sctp_rtoinfo))
2951 return -EINVAL;
2952
2953 if (copy_from_user(&rtoinfo, optval, optlen))
2954 return -EFAULT;
2955
2956 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2957
2958 /* Set the values to the specific association */
2959 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2960 return -EINVAL;
2961
wangweidong85f935d2013-12-11 09:50:38 +08002962 rto_max = rtoinfo.srto_max;
2963 rto_min = rtoinfo.srto_min;
2964
2965 if (rto_max)
2966 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
2967 else
2968 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
2969
2970 if (rto_min)
2971 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
2972 else
2973 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
2974
2975 if (rto_min > rto_max)
2976 return -EINVAL;
2977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (asoc) {
2979 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002980 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 msecs_to_jiffies(rtoinfo.srto_initial);
wangweidong85f935d2013-12-11 09:50:38 +08002982 asoc->rto_max = rto_max;
2983 asoc->rto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 } else {
2985 /* If there is no association or the association-id = 0
2986 * set the values to the endpoint.
2987 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (rtoinfo.srto_initial != 0)
2989 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
wangweidong85f935d2013-12-11 09:50:38 +08002990 sp->rtoinfo.srto_max = rto_max;
2991 sp->rtoinfo.srto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 }
2993
2994 return 0;
2995}
2996
2997/*
2998 *
2999 * 7.1.2 SCTP_ASSOCINFO
3000 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02003001 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 * of the association.
3003 * Returns an error if the new association retransmission value is
3004 * greater than the sum of the retransmission value of the peer.
3005 * See [SCTP] for more information.
3006 *
3007 */
David S. Millerb7058842009-09-30 16:12:20 -07003008static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
3010
3011 struct sctp_assocparams assocparams;
3012 struct sctp_association *asoc;
3013
3014 if (optlen != sizeof(struct sctp_assocparams))
3015 return -EINVAL;
3016 if (copy_from_user(&assocparams, optval, optlen))
3017 return -EFAULT;
3018
3019 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3020
3021 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3022 return -EINVAL;
3023
3024 /* Set the values to the specific association */
3025 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003026 if (assocparams.sasoc_asocmaxrxt != 0) {
3027 __u32 path_sum = 0;
3028 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003029 struct sctp_transport *peer_addr;
3030
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07003031 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3032 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003033 path_sum += peer_addr->pathmaxrxt;
3034 paths++;
3035 }
3036
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003037 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003038 * one path/transport. We do this because path
3039 * retransmissions are only counted when we have more
3040 * then one path.
3041 */
3042 if (paths > 1 &&
3043 assocparams.sasoc_asocmaxrxt > path_sum)
3044 return -EINVAL;
3045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003047 }
3048
Daniel Borkmann52db8822013-06-25 18:17:27 +02003049 if (assocparams.sasoc_cookie_life != 0)
3050 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 } else {
3052 /* Set the values to the endpoint */
3053 struct sctp_sock *sp = sctp_sk(sk);
3054
3055 if (assocparams.sasoc_asocmaxrxt != 0)
3056 sp->assocparams.sasoc_asocmaxrxt =
3057 assocparams.sasoc_asocmaxrxt;
3058 if (assocparams.sasoc_cookie_life != 0)
3059 sp->assocparams.sasoc_cookie_life =
3060 assocparams.sasoc_cookie_life;
3061 }
3062 return 0;
3063}
3064
3065/*
3066 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3067 *
3068 * This socket option is a boolean flag which turns on or off mapped V4
3069 * addresses. If this option is turned on and the socket is type
3070 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3071 * If this option is turned off, then no mapping will be done of V4
3072 * addresses and a user will receive both PF_INET6 and PF_INET type
3073 * addresses on the socket.
3074 */
David S. Millerb7058842009-09-30 16:12:20 -07003075static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
3077 int val;
3078 struct sctp_sock *sp = sctp_sk(sk);
3079
3080 if (optlen < sizeof(int))
3081 return -EINVAL;
3082 if (get_user(val, (int __user *)optval))
3083 return -EFAULT;
3084 if (val)
3085 sp->v4mapped = 1;
3086 else
3087 sp->v4mapped = 0;
3088
3089 return 0;
3090}
3091
3092/*
Wei Yongjune89c2092008-12-25 16:54:58 -08003093 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3094 * This option will get or set the maximum size to put in any outgoing
3095 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 * fragmented by SCTP into the specified size. Note that the underlying
3097 * SCTP implementation may fragment into smaller sized chunks when the
3098 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08003099 * the user. The default value for this option is '0' which indicates
3100 * the user is NOT limiting fragmentation and only the PMTU will effect
3101 * SCTP's choice of DATA chunk size. Note also that values set larger
3102 * than the maximum size of an IP datagram will effectively let SCTP
3103 * control fragmentation (i.e. the same as setting this option to 0).
3104 *
3105 * The following structure is used to access and modify this parameter:
3106 *
3107 * struct sctp_assoc_value {
3108 * sctp_assoc_t assoc_id;
3109 * uint32_t assoc_value;
3110 * };
3111 *
3112 * assoc_id: This parameter is ignored for one-to-one style sockets.
3113 * For one-to-many style sockets this parameter indicates which
3114 * association the user is performing an action upon. Note that if
3115 * this field's value is zero then the endpoints default value is
3116 * changed (effecting future associations only).
3117 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 */
David S. Millerb7058842009-09-30 16:12:20 -07003119static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
Xin Longecca8f82017-11-17 14:11:11 +08003121 struct sctp_sock *sp = sctp_sk(sk);
Wei Yongjune89c2092008-12-25 16:54:58 -08003122 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 int val;
3125
Wei Yongjune89c2092008-12-25 16:54:58 -08003126 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003127 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003128 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003129 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003130 "Use struct sctp_assoc_value instead\n",
3131 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08003132 if (copy_from_user(&val, optval, optlen))
3133 return -EFAULT;
3134 params.assoc_id = 0;
3135 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3136 if (copy_from_user(&params, optval, optlen))
3137 return -EFAULT;
3138 val = params.assoc_value;
Xin Longecca8f82017-11-17 14:11:11 +08003139 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return -EINVAL;
Xin Longecca8f82017-11-17 14:11:11 +08003141 }
Wei Yongjune89c2092008-12-25 16:54:58 -08003142
Xin Longecca8f82017-11-17 14:11:11 +08003143 if (val) {
3144 int min_len, max_len;
3145
3146 min_len = SCTP_DEFAULT_MINSEGMENT - sp->pf->af->net_header_len;
3147 min_len -= sizeof(struct sctphdr) +
3148 sizeof(struct sctp_data_chunk);
3149
3150 max_len = SCTP_MAX_CHUNK_LEN - sizeof(struct sctp_data_chunk);
3151
3152 if (val < min_len || val > max_len)
3153 return -EINVAL;
3154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Wei Yongjune89c2092008-12-25 16:54:58 -08003156 asoc = sctp_id2assoc(sk, params.assoc_id);
Wei Yongjune89c2092008-12-25 16:54:58 -08003157 if (asoc) {
3158 if (val == 0) {
Xin Longecca8f82017-11-17 14:11:11 +08003159 val = asoc->pathmtu - sp->pf->af->net_header_len;
Wei Yongjune89c2092008-12-25 16:54:58 -08003160 val -= sizeof(struct sctphdr) +
Xin Long668c9be2017-12-08 21:04:02 +08003161 sctp_datachk_len(&asoc->stream);
Wei Yongjune89c2092008-12-25 16:54:58 -08003162 }
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003163 asoc->user_frag = val;
3164 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
Wei Yongjune89c2092008-12-25 16:54:58 -08003165 } else {
Xin Longecca8f82017-11-17 14:11:11 +08003166 if (params.assoc_id && sctp_style(sk, UDP))
3167 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003168 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 }
3170
3171 return 0;
3172}
3173
3174
3175/*
3176 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3177 *
3178 * Requests that the peer mark the enclosed address as the association
3179 * primary. The enclosed address must be one of the association's
3180 * locally bound addresses. The following structure is used to make a
3181 * set primary request:
3182 */
3183static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003184 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003186 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 struct sctp_association *asoc = NULL;
3189 struct sctp_setpeerprim prim;
3190 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003191 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 int err;
3193
3194 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003196 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return -EPERM;
3198
3199 if (optlen != sizeof(struct sctp_setpeerprim))
3200 return -EINVAL;
3201
3202 if (copy_from_user(&prim, optval, optlen))
3203 return -EFAULT;
3204
3205 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003206 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 return -EINVAL;
3208
3209 if (!asoc->peer.asconf_capable)
3210 return -EPERM;
3211
3212 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3213 return -EPERM;
3214
3215 if (!sctp_state(asoc, ESTABLISHED))
3216 return -ENOTCONN;
3217
Wei Yongjun40a01032010-12-07 17:11:09 +00003218 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3219 if (!af)
3220 return -EINVAL;
3221
3222 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3223 return -EADDRNOTAVAIL;
3224
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3226 return -EADDRNOTAVAIL;
3227
3228 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3229 chunk = sctp_make_asconf_set_prim(asoc,
3230 (union sctp_addr *)&prim.sspp_addr);
3231 if (!chunk)
3232 return -ENOMEM;
3233
3234 err = sctp_send_asconf(asoc, chunk);
3235
Daniel Borkmannbb333812013-06-28 19:49:40 +02003236 pr_debug("%s: we set peer primary addr primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
3238 return err;
3239}
3240
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003241static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003242 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003244 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003246 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003248 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 return -EFAULT;
3250
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003251 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
3253 return 0;
3254}
3255
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003256/*
3257 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3258 *
3259 * The context field in the sctp_sndrcvinfo structure is normally only
3260 * used when a failed message is retrieved holding the value that was
3261 * sent down on the actual send call. This option allows the setting of
3262 * a default context on an association basis that will be received on
3263 * reading messages from the peer. This is especially helpful in the
3264 * one-2-many model for an application to keep some reference to an
3265 * internal state machine that is processing messages on the
3266 * association. Note that the setting of this value only effects
3267 * received messages from the peer and does not effect the value that is
3268 * saved with outbound messages.
3269 */
3270static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003271 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003272{
3273 struct sctp_assoc_value params;
3274 struct sctp_sock *sp;
3275 struct sctp_association *asoc;
3276
3277 if (optlen != sizeof(struct sctp_assoc_value))
3278 return -EINVAL;
3279 if (copy_from_user(&params, optval, optlen))
3280 return -EFAULT;
3281
3282 sp = sctp_sk(sk);
3283
3284 if (params.assoc_id != 0) {
3285 asoc = sctp_id2assoc(sk, params.assoc_id);
3286 if (!asoc)
3287 return -EINVAL;
3288 asoc->default_rcv_context = params.assoc_value;
3289 } else {
3290 sp->default_rcv_context = params.assoc_value;
3291 }
3292
3293 return 0;
3294}
3295
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003296/*
3297 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3298 *
3299 * This options will at a minimum specify if the implementation is doing
3300 * fragmented interleave. Fragmented interleave, for a one to many
3301 * socket, is when subsequent calls to receive a message may return
3302 * parts of messages from different associations. Some implementations
3303 * may allow you to turn this value on or off. If so, when turned off,
3304 * no fragment interleave will occur (which will cause a head of line
3305 * blocking amongst multiple associations sharing the same one to many
3306 * socket). When this option is turned on, then each receive call may
3307 * come from a different association (thus the user must receive data
3308 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3309 * association each receive belongs to.
3310 *
3311 * This option takes a boolean value. A non-zero value indicates that
3312 * fragmented interleave is on. A value of zero indicates that
3313 * fragmented interleave is off.
3314 *
3315 * Note that it is important that an implementation that allows this
3316 * option to be turned on, have it off by default. Otherwise an unaware
3317 * application using the one to many model may become confused and act
3318 * incorrectly.
3319 */
3320static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3321 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003322 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003323{
3324 int val;
3325
3326 if (optlen != sizeof(int))
3327 return -EINVAL;
3328 if (get_user(val, (int __user *)optval))
3329 return -EFAULT;
3330
Xin Long772a5862017-12-08 21:03:58 +08003331 sctp_sk(sk)->frag_interleave = !!val;
3332
3333 if (!sctp_sk(sk)->frag_interleave)
3334 sctp_sk(sk)->strm_interleave = 0;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003335
3336 return 0;
3337}
3338
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003339/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003340 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003341 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003342 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003343 * This option will set or get the SCTP partial delivery point. This
3344 * point is the size of a message where the partial delivery API will be
3345 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003346 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003347 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003348 * point. Note also that the call will fail if the user attempts to set
3349 * this value larger than the socket receive buffer size.
3350 *
3351 * Note that any single message having a length smaller than or equal to
3352 * the SCTP partial delivery point will be delivered in one single read
3353 * call as long as the user provided buffer is large enough to hold the
3354 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003355 */
3356static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3357 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003358 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003359{
3360 u32 val;
3361
3362 if (optlen != sizeof(u32))
3363 return -EINVAL;
3364 if (get_user(val, (int __user *)optval))
3365 return -EFAULT;
3366
Wei Yongjun8510b932008-12-25 16:59:03 -08003367 /* Note: We double the receive buffer from what the user sets
3368 * it to be, also initial rwnd is based on rcvbuf/2.
3369 */
3370 if (val > (sk->sk_rcvbuf >> 1))
3371 return -EINVAL;
3372
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003373 sctp_sk(sk)->pd_point = val;
3374
3375 return 0; /* is this the right error code? */
3376}
3377
Vlad Yasevich70331572007-03-23 11:34:36 -07003378/*
3379 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3380 *
3381 * This option will allow a user to change the maximum burst of packets
3382 * that can be emitted by this association. Note that the default value
3383 * is 4, and some implementations may restrict this setting so that it
3384 * can only be lowered.
3385 *
3386 * NOTE: This text doesn't seem right. Do this on a socket basis with
3387 * future associations inheriting the socket value.
3388 */
3389static int sctp_setsockopt_maxburst(struct sock *sk,
3390 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003391 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003392{
Neil Horman219b99a2008-03-05 13:44:46 -08003393 struct sctp_assoc_value params;
3394 struct sctp_sock *sp;
3395 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003396 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003397 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003398
Neil Horman219b99a2008-03-05 13:44:46 -08003399 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003400 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003401 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003402 "Use of int in max_burst socket option deprecated.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003403 "Use struct sctp_assoc_value instead\n",
3404 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08003405 if (copy_from_user(&val, optval, optlen))
3406 return -EFAULT;
3407 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3408 if (copy_from_user(&params, optval, optlen))
3409 return -EFAULT;
3410 val = params.assoc_value;
3411 assoc_id = params.assoc_id;
3412 } else
3413 return -EINVAL;
3414
3415 sp = sctp_sk(sk);
3416
3417 if (assoc_id != 0) {
3418 asoc = sctp_id2assoc(sk, assoc_id);
3419 if (!asoc)
3420 return -EINVAL;
3421 asoc->max_burst = val;
3422 } else
3423 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003424
3425 return 0;
3426}
3427
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003428/*
3429 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3430 *
3431 * This set option adds a chunk type that the user is requesting to be
3432 * received only in an authenticated way. Changes to the list of chunks
3433 * will only effect future associations on the socket.
3434 */
3435static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003436 char __user *optval,
3437 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003438{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003439 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003440 struct sctp_authchunk val;
3441
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003442 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003443 return -EACCES;
3444
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003445 if (optlen != sizeof(struct sctp_authchunk))
3446 return -EINVAL;
3447 if (copy_from_user(&val, optval, optlen))
3448 return -EFAULT;
3449
3450 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003451 case SCTP_CID_INIT:
3452 case SCTP_CID_INIT_ACK:
3453 case SCTP_CID_SHUTDOWN_COMPLETE:
3454 case SCTP_CID_AUTH:
3455 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003456 }
3457
3458 /* add this chunk id to the endpoint */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003459 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003460}
3461
3462/*
3463 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3464 *
3465 * This option gets or sets the list of HMAC algorithms that the local
3466 * endpoint requires the peer to use.
3467 */
3468static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003469 char __user *optval,
3470 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003471{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003472 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003473 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003474 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003475 int err;
3476
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003477 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003478 return -EACCES;
3479
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003480 if (optlen < sizeof(struct sctp_hmacalgo))
3481 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003482 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3483 SCTP_AUTH_NUM_HMACS * sizeof(u16));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003484
wangweidongcb3f8372013-12-23 12:16:50 +08003485 hmacs = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003486 if (IS_ERR(hmacs))
3487 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003488
Vlad Yasevichd9724052008-08-27 16:09:49 -07003489 idents = hmacs->shmac_num_idents;
3490 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3491 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003492 err = -EINVAL;
3493 goto out;
3494 }
3495
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003496 err = sctp_auth_ep_set_hmacs(ep, hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003497out:
3498 kfree(hmacs);
3499 return err;
3500}
3501
3502/*
3503 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3504 *
3505 * This option will set a shared secret key which is used to build an
3506 * association shared key.
3507 */
3508static int sctp_setsockopt_auth_key(struct sock *sk,
3509 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003510 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003511{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003512 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003513 struct sctp_authkey *authkey;
3514 struct sctp_association *asoc;
3515 int ret;
3516
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003517 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003518 return -EACCES;
3519
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003520 if (optlen <= sizeof(struct sctp_authkey))
3521 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003522 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3523 * this.
3524 */
3525 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3526 sizeof(struct sctp_authkey));
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003527
wangweidongcb3f8372013-12-23 12:16:50 +08003528 authkey = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003529 if (IS_ERR(authkey))
3530 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003531
Vlad Yasevich328fc472008-08-27 16:08:54 -07003532 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003533 ret = -EINVAL;
3534 goto out;
3535 }
3536
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003537 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3538 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3539 ret = -EINVAL;
3540 goto out;
3541 }
3542
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003543 ret = sctp_auth_set_key(ep, asoc, authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003544out:
Daniel Borkmann6ba542a2013-02-08 03:04:34 +00003545 kzfree(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003546 return ret;
3547}
3548
3549/*
3550 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3551 *
3552 * This option will get or set the active shared key to be used to build
3553 * the association shared key.
3554 */
3555static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003556 char __user *optval,
3557 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003558{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003559 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003560 struct sctp_authkeyid val;
3561 struct sctp_association *asoc;
3562
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003563 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003564 return -EACCES;
3565
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003566 if (optlen != sizeof(struct sctp_authkeyid))
3567 return -EINVAL;
3568 if (copy_from_user(&val, optval, optlen))
3569 return -EFAULT;
3570
3571 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3572 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3573 return -EINVAL;
3574
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003575 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003576}
3577
3578/*
3579 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3580 *
3581 * This set option will delete a shared secret key from use.
3582 */
3583static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003584 char __user *optval,
3585 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003586{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003587 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003588 struct sctp_authkeyid val;
3589 struct sctp_association *asoc;
3590
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003591 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003592 return -EACCES;
3593
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003594 if (optlen != sizeof(struct sctp_authkeyid))
3595 return -EINVAL;
3596 if (copy_from_user(&val, optval, optlen))
3597 return -EFAULT;
3598
3599 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3600 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3601 return -EINVAL;
3602
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003603 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003604
3605}
3606
Michio Honda7dc04d72011-04-26 20:16:31 +09003607/*
3608 * 8.1.23 SCTP_AUTO_ASCONF
3609 *
3610 * This option will enable or disable the use of the automatic generation of
3611 * ASCONF chunks to add and delete addresses to an existing association. Note
3612 * that this option has two caveats namely: a) it only affects sockets that
3613 * are bound to all addresses available to the SCTP stack, and b) the system
3614 * administrator may have an overriding control that turns the ASCONF feature
3615 * off no matter what setting the socket option may have.
3616 * This option expects an integer boolean flag, where a non-zero value turns on
3617 * the option, and a zero value turns off the option.
3618 * Note. In this implementation, socket operation overrides default parameter
3619 * being set by sysctl as well as FreeBSD implementation
3620 */
3621static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3622 unsigned int optlen)
3623{
3624 int val;
3625 struct sctp_sock *sp = sctp_sk(sk);
3626
3627 if (optlen < sizeof(int))
3628 return -EINVAL;
3629 if (get_user(val, (int __user *)optval))
3630 return -EFAULT;
3631 if (!sctp_is_ep_boundall(sk) && val)
3632 return -EINVAL;
3633 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3634 return 0;
3635
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003636 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003637 if (val == 0 && sp->do_auto_asconf) {
3638 list_del(&sp->auto_asconf_list);
3639 sp->do_auto_asconf = 0;
3640 } else if (val && !sp->do_auto_asconf) {
3641 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00003642 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09003643 sp->do_auto_asconf = 1;
3644 }
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003645 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003646 return 0;
3647}
3648
Neil Horman5aa93bc2012-07-21 07:56:07 +00003649/*
3650 * SCTP_PEER_ADDR_THLDS
3651 *
3652 * This option allows us to alter the partially failed threshold for one or all
3653 * transports in an association. See Section 6.1 of:
3654 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3655 */
3656static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3657 char __user *optval,
3658 unsigned int optlen)
3659{
3660 struct sctp_paddrthlds val;
3661 struct sctp_transport *trans;
3662 struct sctp_association *asoc;
3663
3664 if (optlen < sizeof(struct sctp_paddrthlds))
3665 return -EINVAL;
3666 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3667 sizeof(struct sctp_paddrthlds)))
3668 return -EFAULT;
3669
3670
3671 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3672 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3673 if (!asoc)
3674 return -ENOENT;
3675 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3676 transports) {
3677 if (val.spt_pathmaxrxt)
3678 trans->pathmaxrxt = val.spt_pathmaxrxt;
3679 trans->pf_retrans = val.spt_pathpfthld;
3680 }
3681
3682 if (val.spt_pathmaxrxt)
3683 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3684 asoc->pf_retrans = val.spt_pathpfthld;
3685 } else {
3686 trans = sctp_addr_id2transport(sk, &val.spt_address,
3687 val.spt_assoc_id);
3688 if (!trans)
3689 return -ENOENT;
3690
3691 if (val.spt_pathmaxrxt)
3692 trans->pathmaxrxt = val.spt_pathmaxrxt;
3693 trans->pf_retrans = val.spt_pathpfthld;
3694 }
3695
3696 return 0;
3697}
3698
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02003699static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3700 char __user *optval,
3701 unsigned int optlen)
3702{
3703 int val;
3704
3705 if (optlen < sizeof(int))
3706 return -EINVAL;
3707 if (get_user(val, (int __user *) optval))
3708 return -EFAULT;
3709
3710 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3711
3712 return 0;
3713}
3714
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02003715static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3716 char __user *optval,
3717 unsigned int optlen)
3718{
3719 int val;
3720
3721 if (optlen < sizeof(int))
3722 return -EINVAL;
3723 if (get_user(val, (int __user *) optval))
3724 return -EFAULT;
3725
3726 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3727
3728 return 0;
3729}
3730
Xin Long28aa4c22016-07-09 19:47:40 +08003731static int sctp_setsockopt_pr_supported(struct sock *sk,
3732 char __user *optval,
3733 unsigned int optlen)
3734{
3735 struct sctp_assoc_value params;
3736 struct sctp_association *asoc;
3737 int retval = -EINVAL;
3738
3739 if (optlen != sizeof(params))
3740 goto out;
3741
3742 if (copy_from_user(&params, optval, optlen)) {
3743 retval = -EFAULT;
3744 goto out;
3745 }
3746
3747 asoc = sctp_id2assoc(sk, params.assoc_id);
3748 if (asoc) {
3749 asoc->prsctp_enable = !!params.assoc_value;
3750 } else if (!params.assoc_id) {
3751 struct sctp_sock *sp = sctp_sk(sk);
3752
3753 sp->ep->prsctp_enable = !!params.assoc_value;
3754 } else {
3755 goto out;
3756 }
3757
3758 retval = 0;
3759
3760out:
3761 return retval;
3762}
3763
Xin Longf959fb42016-07-09 19:47:41 +08003764static int sctp_setsockopt_default_prinfo(struct sock *sk,
3765 char __user *optval,
3766 unsigned int optlen)
3767{
3768 struct sctp_default_prinfo info;
3769 struct sctp_association *asoc;
3770 int retval = -EINVAL;
3771
3772 if (optlen != sizeof(info))
3773 goto out;
3774
3775 if (copy_from_user(&info, optval, sizeof(info))) {
3776 retval = -EFAULT;
3777 goto out;
3778 }
3779
3780 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
3781 goto out;
3782
3783 if (info.pr_policy == SCTP_PR_SCTP_NONE)
3784 info.pr_value = 0;
3785
3786 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
3787 if (asoc) {
3788 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
3789 asoc->default_timetolive = info.pr_value;
3790 } else if (!info.pr_assoc_id) {
3791 struct sctp_sock *sp = sctp_sk(sk);
3792
3793 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
3794 sp->default_timetolive = info.pr_value;
3795 } else {
3796 goto out;
3797 }
3798
3799 retval = 0;
3800
3801out:
3802 return retval;
3803}
3804
Xin Longc0d8bab2017-03-10 12:11:12 +08003805static int sctp_setsockopt_reconfig_supported(struct sock *sk,
3806 char __user *optval,
3807 unsigned int optlen)
3808{
3809 struct sctp_assoc_value params;
3810 struct sctp_association *asoc;
3811 int retval = -EINVAL;
3812
3813 if (optlen != sizeof(params))
3814 goto out;
3815
3816 if (copy_from_user(&params, optval, optlen)) {
3817 retval = -EFAULT;
3818 goto out;
3819 }
3820
3821 asoc = sctp_id2assoc(sk, params.assoc_id);
3822 if (asoc) {
3823 asoc->reconf_enable = !!params.assoc_value;
3824 } else if (!params.assoc_id) {
3825 struct sctp_sock *sp = sctp_sk(sk);
3826
3827 sp->ep->reconf_enable = !!params.assoc_value;
3828 } else {
3829 goto out;
3830 }
3831
3832 retval = 0;
3833
3834out:
3835 return retval;
3836}
3837
Xin Long9fb657a2017-01-18 00:44:46 +08003838static int sctp_setsockopt_enable_strreset(struct sock *sk,
3839 char __user *optval,
3840 unsigned int optlen)
3841{
3842 struct sctp_assoc_value params;
3843 struct sctp_association *asoc;
3844 int retval = -EINVAL;
3845
3846 if (optlen != sizeof(params))
3847 goto out;
3848
3849 if (copy_from_user(&params, optval, optlen)) {
3850 retval = -EFAULT;
3851 goto out;
3852 }
3853
3854 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
3855 goto out;
3856
3857 asoc = sctp_id2assoc(sk, params.assoc_id);
3858 if (asoc) {
3859 asoc->strreset_enable = params.assoc_value;
3860 } else if (!params.assoc_id) {
3861 struct sctp_sock *sp = sctp_sk(sk);
3862
3863 sp->ep->strreset_enable = params.assoc_value;
3864 } else {
3865 goto out;
3866 }
3867
3868 retval = 0;
3869
3870out:
3871 return retval;
3872}
3873
Xin Long7f9d68a2017-01-18 00:44:47 +08003874static int sctp_setsockopt_reset_streams(struct sock *sk,
3875 char __user *optval,
3876 unsigned int optlen)
3877{
3878 struct sctp_reset_streams *params;
3879 struct sctp_association *asoc;
3880 int retval = -EINVAL;
3881
Xin Long2342b8d2017-12-10 15:40:51 +08003882 if (optlen < sizeof(*params))
Xin Long7f9d68a2017-01-18 00:44:47 +08003883 return -EINVAL;
Marcelo Ricardo Leitner5960cef2018-01-08 19:02:28 -02003884 /* srs_number_streams is u16, so optlen can't be bigger than this. */
3885 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3886 sizeof(__u16) * sizeof(*params));
Xin Long7f9d68a2017-01-18 00:44:47 +08003887
3888 params = memdup_user(optval, optlen);
3889 if (IS_ERR(params))
3890 return PTR_ERR(params);
3891
Xin Long2342b8d2017-12-10 15:40:51 +08003892 if (params->srs_number_streams * sizeof(__u16) >
3893 optlen - sizeof(*params))
3894 goto out;
3895
Xin Long7f9d68a2017-01-18 00:44:47 +08003896 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
3897 if (!asoc)
3898 goto out;
3899
3900 retval = sctp_send_reset_streams(asoc, params);
3901
3902out:
3903 kfree(params);
3904 return retval;
3905}
3906
Xin Longa92ce1a2017-02-09 01:18:18 +08003907static int sctp_setsockopt_reset_assoc(struct sock *sk,
3908 char __user *optval,
3909 unsigned int optlen)
3910{
3911 struct sctp_association *asoc;
3912 sctp_assoc_t associd;
3913 int retval = -EINVAL;
3914
3915 if (optlen != sizeof(associd))
3916 goto out;
3917
3918 if (copy_from_user(&associd, optval, optlen)) {
3919 retval = -EFAULT;
3920 goto out;
3921 }
3922
3923 asoc = sctp_id2assoc(sk, associd);
3924 if (!asoc)
3925 goto out;
3926
3927 retval = sctp_send_reset_assoc(asoc);
3928
3929out:
3930 return retval;
3931}
3932
Xin Long242bd2d2017-02-09 01:18:20 +08003933static int sctp_setsockopt_add_streams(struct sock *sk,
3934 char __user *optval,
3935 unsigned int optlen)
3936{
3937 struct sctp_association *asoc;
3938 struct sctp_add_streams params;
3939 int retval = -EINVAL;
3940
3941 if (optlen != sizeof(params))
3942 goto out;
3943
3944 if (copy_from_user(&params, optval, optlen)) {
3945 retval = -EFAULT;
3946 goto out;
3947 }
3948
3949 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
3950 if (!asoc)
3951 goto out;
3952
3953 retval = sctp_send_add_streams(asoc, &params);
3954
3955out:
3956 return retval;
3957}
3958
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03003959static int sctp_setsockopt_scheduler(struct sock *sk,
3960 char __user *optval,
3961 unsigned int optlen)
3962{
3963 struct sctp_association *asoc;
3964 struct sctp_assoc_value params;
3965 int retval = -EINVAL;
3966
3967 if (optlen < sizeof(params))
3968 goto out;
3969
3970 optlen = sizeof(params);
3971 if (copy_from_user(&params, optval, optlen)) {
3972 retval = -EFAULT;
3973 goto out;
3974 }
3975
3976 if (params.assoc_value > SCTP_SS_MAX)
3977 goto out;
3978
3979 asoc = sctp_id2assoc(sk, params.assoc_id);
3980 if (!asoc)
3981 goto out;
3982
3983 retval = sctp_sched_set_sched(asoc, params.assoc_value);
3984
3985out:
3986 return retval;
3987}
3988
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03003989static int sctp_setsockopt_scheduler_value(struct sock *sk,
3990 char __user *optval,
3991 unsigned int optlen)
3992{
3993 struct sctp_association *asoc;
3994 struct sctp_stream_value params;
3995 int retval = -EINVAL;
3996
3997 if (optlen < sizeof(params))
3998 goto out;
3999
4000 optlen = sizeof(params);
4001 if (copy_from_user(&params, optval, optlen)) {
4002 retval = -EFAULT;
4003 goto out;
4004 }
4005
4006 asoc = sctp_id2assoc(sk, params.assoc_id);
4007 if (!asoc)
4008 goto out;
4009
4010 retval = sctp_sched_set_value(asoc, params.stream_id,
4011 params.stream_value, GFP_KERNEL);
4012
4013out:
4014 return retval;
4015}
4016
Xin Long772a5862017-12-08 21:03:58 +08004017static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4018 char __user *optval,
4019 unsigned int optlen)
4020{
4021 struct sctp_sock *sp = sctp_sk(sk);
4022 struct net *net = sock_net(sk);
4023 struct sctp_assoc_value params;
4024 int retval = -EINVAL;
4025
4026 if (optlen < sizeof(params))
4027 goto out;
4028
4029 optlen = sizeof(params);
4030 if (copy_from_user(&params, optval, optlen)) {
4031 retval = -EFAULT;
4032 goto out;
4033 }
4034
4035 if (params.assoc_id)
4036 goto out;
4037
4038 if (!net->sctp.intl_enable || !sp->frag_interleave) {
4039 retval = -EPERM;
4040 goto out;
4041 }
4042
4043 sp->strm_interleave = !!params.assoc_value;
4044
4045 retval = 0;
4046
4047out:
4048 return retval;
4049}
4050
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051/* API 6.2 setsockopt(), getsockopt()
4052 *
4053 * Applications use setsockopt() and getsockopt() to set or retrieve
4054 * socket options. Socket options are used to change the default
4055 * behavior of sockets calls. They are described in Section 7.
4056 *
4057 * The syntax is:
4058 *
4059 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4060 * int __user *optlen);
4061 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4062 * int optlen);
4063 *
4064 * sd - the socket descript.
4065 * level - set to IPPROTO_SCTP for all SCTP options.
4066 * optname - the option name.
4067 * optval - the buffer to store the value of the option.
4068 * optlen - the size of the buffer.
4069 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004070static int sctp_setsockopt(struct sock *sk, int level, int optname,
4071 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072{
4073 int retval = 0;
4074
Daniel Borkmannbb333812013-06-28 19:49:40 +02004075 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
4077 /* I can hardly begin to describe how wrong this is. This is
4078 * so broken as to be worse than useless. The API draft
4079 * REALLY is NOT helpful here... I am not convinced that the
4080 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4081 * are at all well-founded.
4082 */
4083 if (level != SOL_SCTP) {
4084 struct sctp_af *af = sctp_sk(sk)->pf->af;
4085 retval = af->setsockopt(sk, level, optname, optval, optlen);
4086 goto out_nounlock;
4087 }
4088
wangweidong048ed4b2014-01-21 15:44:11 +08004089 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
4091 switch (optname) {
4092 case SCTP_SOCKOPT_BINDX_ADD:
4093 /* 'optlen' is the size of the addresses buffer. */
4094 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4095 optlen, SCTP_BINDX_ADD_ADDR);
4096 break;
4097
4098 case SCTP_SOCKOPT_BINDX_REM:
4099 /* 'optlen' is the size of the addresses buffer. */
4100 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4101 optlen, SCTP_BINDX_REM_ADDR);
4102 break;
4103
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004104 case SCTP_SOCKOPT_CONNECTX_OLD:
4105 /* 'optlen' is the size of the addresses buffer. */
4106 retval = sctp_setsockopt_connectx_old(sk,
4107 (struct sockaddr __user *)optval,
4108 optlen);
4109 break;
4110
Frank Filz3f7a87d2005-06-20 13:14:57 -07004111 case SCTP_SOCKOPT_CONNECTX:
4112 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004113 retval = sctp_setsockopt_connectx(sk,
4114 (struct sockaddr __user *)optval,
4115 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004116 break;
4117
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 case SCTP_DISABLE_FRAGMENTS:
4119 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4120 break;
4121
4122 case SCTP_EVENTS:
4123 retval = sctp_setsockopt_events(sk, optval, optlen);
4124 break;
4125
4126 case SCTP_AUTOCLOSE:
4127 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4128 break;
4129
4130 case SCTP_PEER_ADDR_PARAMS:
4131 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4132 break;
4133
Shan Wei4580ccc2011-01-18 22:39:00 +00004134 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07004135 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08004136 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004137 case SCTP_PARTIAL_DELIVERY_POINT:
4138 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4139 break;
Frank Filz77086102005-12-22 11:37:30 -08004140
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 case SCTP_INITMSG:
4142 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4143 break;
4144 case SCTP_DEFAULT_SEND_PARAM:
4145 retval = sctp_setsockopt_default_send_param(sk, optval,
4146 optlen);
4147 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02004148 case SCTP_DEFAULT_SNDINFO:
4149 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4150 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 case SCTP_PRIMARY_ADDR:
4152 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4153 break;
4154 case SCTP_SET_PEER_PRIMARY_ADDR:
4155 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4156 break;
4157 case SCTP_NODELAY:
4158 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4159 break;
4160 case SCTP_RTOINFO:
4161 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4162 break;
4163 case SCTP_ASSOCINFO:
4164 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4165 break;
4166 case SCTP_I_WANT_MAPPED_V4_ADDR:
4167 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4168 break;
4169 case SCTP_MAXSEG:
4170 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4171 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004172 case SCTP_ADAPTATION_LAYER:
4173 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004175 case SCTP_CONTEXT:
4176 retval = sctp_setsockopt_context(sk, optval, optlen);
4177 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004178 case SCTP_FRAGMENT_INTERLEAVE:
4179 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4180 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07004181 case SCTP_MAX_BURST:
4182 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4183 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004184 case SCTP_AUTH_CHUNK:
4185 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4186 break;
4187 case SCTP_HMAC_IDENT:
4188 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4189 break;
4190 case SCTP_AUTH_KEY:
4191 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4192 break;
4193 case SCTP_AUTH_ACTIVE_KEY:
4194 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4195 break;
4196 case SCTP_AUTH_DELETE_KEY:
4197 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4198 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09004199 case SCTP_AUTO_ASCONF:
4200 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4201 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00004202 case SCTP_PEER_ADDR_THLDS:
4203 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4204 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004205 case SCTP_RECVRCVINFO:
4206 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4207 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004208 case SCTP_RECVNXTINFO:
4209 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4210 break;
Xin Long28aa4c22016-07-09 19:47:40 +08004211 case SCTP_PR_SUPPORTED:
4212 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4213 break;
Xin Longf959fb42016-07-09 19:47:41 +08004214 case SCTP_DEFAULT_PRINFO:
4215 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4216 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08004217 case SCTP_RECONFIG_SUPPORTED:
4218 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4219 break;
Xin Long9fb657a2017-01-18 00:44:46 +08004220 case SCTP_ENABLE_STREAM_RESET:
4221 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4222 break;
Xin Long7f9d68a2017-01-18 00:44:47 +08004223 case SCTP_RESET_STREAMS:
4224 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4225 break;
Xin Longa92ce1a2017-02-09 01:18:18 +08004226 case SCTP_RESET_ASSOC:
4227 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4228 break;
Xin Long242bd2d2017-02-09 01:18:20 +08004229 case SCTP_ADD_STREAMS:
4230 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4231 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004232 case SCTP_STREAM_SCHEDULER:
4233 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4234 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004235 case SCTP_STREAM_SCHEDULER_VALUE:
4236 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4237 break;
Xin Long772a5862017-12-08 21:03:58 +08004238 case SCTP_INTERLEAVING_SUPPORTED:
4239 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4240 optlen);
4241 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 default:
4243 retval = -ENOPROTOOPT;
4244 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
wangweidong048ed4b2014-01-21 15:44:11 +08004247 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
4249out_nounlock:
4250 return retval;
4251}
4252
4253/* API 3.1.6 connect() - UDP Style Syntax
4254 *
4255 * An application may use the connect() call in the UDP model to initiate an
4256 * association without sending data.
4257 *
4258 * The syntax is:
4259 *
4260 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4261 *
4262 * sd: the socket descriptor to have a new association added to.
4263 *
4264 * nam: the address structure (either struct sockaddr_in or struct
4265 * sockaddr_in6 defined in RFC2553 [7]).
4266 *
4267 * len: the size of the address.
4268 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004269static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4270 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07004273 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
wangweidong048ed4b2014-01-21 15:44:11 +08004275 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
Daniel Borkmannbb333812013-06-28 19:49:40 +02004277 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4278 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279
Frank Filz3f7a87d2005-06-20 13:14:57 -07004280 /* Validate addr_len before calling common connect/connectx routine. */
4281 af = sctp_get_af_specific(addr->sa_family);
4282 if (!af || addr_len < af->sockaddr_len) {
4283 err = -EINVAL;
4284 } else {
4285 /* Pass correct addr len to common routine (so it knows there
4286 * is only one address being passed.
4287 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004288 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 }
4290
wangweidong048ed4b2014-01-21 15:44:11 +08004291 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 return err;
4293}
4294
4295/* FIXME: Write comments. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004296static int sctp_disconnect(struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297{
4298 return -EOPNOTSUPP; /* STUB */
4299}
4300
4301/* 4.1.4 accept() - TCP Style Syntax
4302 *
4303 * Applications use accept() call to remove an established SCTP
4304 * association from the accept queue of the endpoint. A new socket
4305 * descriptor will be returned from accept() to represent the newly
4306 * formed association.
4307 */
David Howellscdfbabf2017-03-09 08:09:05 +00004308static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309{
4310 struct sctp_sock *sp;
4311 struct sctp_endpoint *ep;
4312 struct sock *newsk = NULL;
4313 struct sctp_association *asoc;
4314 long timeo;
4315 int error = 0;
4316
wangweidong048ed4b2014-01-21 15:44:11 +08004317 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
4319 sp = sctp_sk(sk);
4320 ep = sp->ep;
4321
4322 if (!sctp_style(sk, TCP)) {
4323 error = -EOPNOTSUPP;
4324 goto out;
4325 }
4326
4327 if (!sctp_sstate(sk, LISTENING)) {
4328 error = -EINVAL;
4329 goto out;
4330 }
4331
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07004332 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
4334 error = sctp_wait_for_accept(sk, timeo);
4335 if (error)
4336 goto out;
4337
4338 /* We treat the list of associations on the endpoint as the accept
4339 * queue and pick the first association on the list.
4340 */
4341 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4342
David Howellscdfbabf2017-03-09 08:09:05 +00004343 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 if (!newsk) {
4345 error = -ENOMEM;
4346 goto out;
4347 }
4348
4349 /* Populate the fields of the newsk from the oldsk and migrate the
4350 * asoc to the newsk.
4351 */
4352 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4353
4354out:
wangweidong048ed4b2014-01-21 15:44:11 +08004355 release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004356 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 return newsk;
4358}
4359
4360/* The SCTP ioctl handler. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004361static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004363 int rc = -ENOTCONN;
4364
wangweidong048ed4b2014-01-21 15:44:11 +08004365 lock_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004366
4367 /*
4368 * SEQPACKET-style sockets in LISTENING state are valid, for
4369 * SCTP, so only discard TCP-style sockets in LISTENING state.
4370 */
4371 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4372 goto out;
4373
4374 switch (cmd) {
4375 case SIOCINQ: {
4376 struct sk_buff *skb;
4377 unsigned int amount = 0;
4378
4379 skb = skb_peek(&sk->sk_receive_queue);
4380 if (skb != NULL) {
4381 /*
4382 * We will only return the amount of this packet since
4383 * that is all that will be read.
4384 */
4385 amount = skb->len;
4386 }
4387 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004388 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07004389 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004390 default:
4391 rc = -ENOIOCTLCMD;
4392 break;
4393 }
4394out:
wangweidong048ed4b2014-01-21 15:44:11 +08004395 release_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004396 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397}
4398
4399/* This is the function which gets called during socket creation to
4400 * initialized the SCTP-specific portion of the sock.
4401 * The sock structure should already be zero-filled memory.
4402 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004403static int sctp_init_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004405 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 struct sctp_sock *sp;
4407
Daniel Borkmannbb333812013-06-28 19:49:40 +02004408 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
4410 sp = sctp_sk(sk);
4411
4412 /* Initialize the SCTP per socket area. */
4413 switch (sk->sk_type) {
4414 case SOCK_SEQPACKET:
4415 sp->type = SCTP_SOCKET_UDP;
4416 break;
4417 case SOCK_STREAM:
4418 sp->type = SCTP_SOCKET_TCP;
4419 break;
4420 default:
4421 return -ESOCKTNOSUPPORT;
4422 }
4423
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004424 sk->sk_gso_type = SKB_GSO_SCTP;
4425
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 /* Initialize default send parameters. These parameters can be
4427 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4428 */
4429 sp->default_stream = 0;
4430 sp->default_ppid = 0;
4431 sp->default_flags = 0;
4432 sp->default_context = 0;
4433 sp->default_timetolive = 0;
4434
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004435 sp->default_rcv_context = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004436 sp->max_burst = net->sctp.max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004437
Neil Horman3c681982012-10-24 09:20:03 +00004438 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4439
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 /* Initialize default setup parameters. These parameters
4441 * can be modified with the SCTP_INITMSG socket option or
4442 * overridden by the SCTP_INIT CMSG.
4443 */
4444 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4445 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004446 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4447 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448
4449 /* Initialize default RTO related parameters. These parameters can
4450 * be modified for with the SCTP_RTOINFO socket option.
4451 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004452 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4453 sp->rtoinfo.srto_max = net->sctp.rto_max;
4454 sp->rtoinfo.srto_min = net->sctp.rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455
4456 /* Initialize default association related parameters. These parameters
4457 * can be modified with the SCTP_ASSOCINFO socket option.
4458 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004459 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 sp->assocparams.sasoc_number_peer_destinations = 0;
4461 sp->assocparams.sasoc_peer_rwnd = 0;
4462 sp->assocparams.sasoc_local_rwnd = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004463 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
4465 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004466 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 */
4468 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4469
4470 /* Default Peer Address Parameters. These defaults can
4471 * be modified via SCTP_PEER_ADDR_PARAMS
4472 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004473 sp->hbinterval = net->sctp.hb_interval;
4474 sp->pathmaxrxt = net->sctp.max_retrans_path;
wangweidong4e2d52b2013-12-23 12:16:54 +08004475 sp->pathmtu = 0; /* allow default discovery */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004476 sp->sackdelay = net->sctp.sack_timeout;
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07004477 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004478 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004479 SPP_PMTUD_ENABLE |
4480 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
4482 /* If enabled no SCTP message fragmentation will be performed.
4483 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4484 */
4485 sp->disable_fragments = 0;
4486
Sridhar Samudrala208edef2006-09-29 17:08:01 -07004487 /* Enable Nagle algorithm by default. */
4488 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004490 sp->recvrcvinfo = 0;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004491 sp->recvnxtinfo = 0;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004492
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 /* Enable by default. */
4494 sp->v4mapped = 1;
4495
4496 /* Auto-close idle associations after the configured
4497 * number of seconds. A value of 0 disables this
4498 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4499 * for UDP-style sockets only.
4500 */
4501 sp->autoclose = 0;
4502
4503 /* User specified fragmentation limit. */
4504 sp->user_frag = 0;
4505
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004506 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
4508 sp->pf = sctp_get_pf_specific(sk->sk_family);
4509
4510 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004511 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004513 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
4515 /* Create a per socket endpoint structure. Even if we
4516 * change the data structure relationships, this may still
4517 * be useful for storing pre-connect address information.
4518 */
Daniel Borkmannc164b832013-06-14 18:24:06 +02004519 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4520 if (!sp->ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 return -ENOMEM;
4522
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 sp->hmac = NULL;
4524
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004525 sk->sk_destruct = sctp_destruct_sock;
4526
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08004528
4529 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004530 sk_sockets_allocated_inc(sk);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004531 sock_prot_inuse_add(net, sk->sk_prot, 1);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004532
4533 /* Nothing can fail after this block, otherwise
4534 * sctp_destroy_sock() will be called without addr_wq_lock held
4535 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004536 if (net->sctp.default_auto_asconf) {
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004537 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda9f7d6532011-04-26 19:32:51 +09004538 list_add_tail(&sp->auto_asconf_list,
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004539 &net->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09004540 sp->do_auto_asconf = 1;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004541 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4542 } else {
Michio Honda9f7d6532011-04-26 19:32:51 +09004543 sp->do_auto_asconf = 0;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004544 }
4545
David S. Miller6f756a82008-11-23 17:34:03 -08004546 local_bh_enable();
4547
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 return 0;
4549}
4550
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004551/* Cleanup any SCTP per socket resources. Must be called with
4552 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4553 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004554static void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555{
Michio Honda9f7d6532011-04-26 19:32:51 +09004556 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
Daniel Borkmannbb333812013-06-28 19:49:40 +02004558 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
4560 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09004561 sp = sctp_sk(sk);
Daniel Borkmann1abd1652013-06-06 15:53:47 +02004562 /* This could happen during socket init, thus we bail out
4563 * early, since the rest of the below is not setup either.
4564 */
4565 if (sp->ep == NULL)
4566 return;
4567
Michio Honda9f7d6532011-04-26 19:32:51 +09004568 if (sp->do_auto_asconf) {
4569 sp->do_auto_asconf = 0;
4570 list_del(&sp->auto_asconf_list);
4571 }
4572 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004573 local_bh_disable();
Tonghao Zhang8cb38a62017-12-22 10:15:20 -08004574 sk_sockets_allocated_dec(sk);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08004575 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004576 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577}
4578
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004579/* Triggered when there are no references on the socket anymore */
4580static void sctp_destruct_sock(struct sock *sk)
4581{
4582 struct sctp_sock *sp = sctp_sk(sk);
4583
4584 /* Free up the HMAC transform. */
Herbert Xu5821c762016-01-24 21:20:12 +08004585 crypto_free_shash(sp->hmac);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004586
4587 inet_sock_destruct(sk);
4588}
4589
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590/* API 4.1.7 shutdown() - TCP Style Syntax
4591 * int shutdown(int socket, int how);
4592 *
4593 * sd - the socket descriptor of the association to be closed.
4594 * how - Specifies the type of shutdown. The values are
4595 * as follows:
4596 * SHUT_RD
4597 * Disables further receive operations. No SCTP
4598 * protocol action is taken.
4599 * SHUT_WR
4600 * Disables further send operations, and initiates
4601 * the SCTP shutdown sequence.
4602 * SHUT_RDWR
4603 * Disables further send and receive operations
4604 * and initiates the SCTP shutdown sequence.
4605 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004606static void sctp_shutdown(struct sock *sk, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004608 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 struct sctp_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
4611 if (!sctp_style(sk, TCP))
4612 return;
4613
Xin Long5bf35dd2016-11-13 21:44:37 +08004614 ep = sctp_sk(sk)->ep;
4615 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4616 struct sctp_association *asoc;
4617
Yafang Shaocbabf462017-12-20 11:12:54 +08004618 inet_sk_set_state(sk, SCTP_SS_CLOSING);
Xin Long5bf35dd2016-11-13 21:44:37 +08004619 asoc = list_entry(ep->asocs.next,
4620 struct sctp_association, asocs);
4621 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 }
4623}
4624
Xin Long52c52a62016-04-14 15:35:30 +08004625int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4626 struct sctp_info *info)
4627{
4628 struct sctp_transport *prim;
4629 struct list_head *pos;
4630 int mask;
4631
4632 memset(info, 0, sizeof(*info));
4633 if (!asoc) {
4634 struct sctp_sock *sp = sctp_sk(sk);
4635
4636 info->sctpi_s_autoclose = sp->autoclose;
4637 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4638 info->sctpi_s_pd_point = sp->pd_point;
4639 info->sctpi_s_nodelay = sp->nodelay;
4640 info->sctpi_s_disable_fragments = sp->disable_fragments;
4641 info->sctpi_s_v4mapped = sp->v4mapped;
4642 info->sctpi_s_frag_interleave = sp->frag_interleave;
Xin Long40eb90e92016-05-29 17:42:13 +08004643 info->sctpi_s_type = sp->type;
Xin Long52c52a62016-04-14 15:35:30 +08004644
4645 return 0;
4646 }
4647
4648 info->sctpi_tag = asoc->c.my_vtag;
4649 info->sctpi_state = asoc->state;
4650 info->sctpi_rwnd = asoc->a_rwnd;
4651 info->sctpi_unackdata = asoc->unack_data;
4652 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004653 info->sctpi_instrms = asoc->stream.incnt;
4654 info->sctpi_outstrms = asoc->stream.outcnt;
Xin Long52c52a62016-04-14 15:35:30 +08004655 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4656 info->sctpi_inqueue++;
4657 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4658 info->sctpi_outqueue++;
4659 info->sctpi_overall_error = asoc->overall_error_count;
4660 info->sctpi_max_burst = asoc->max_burst;
4661 info->sctpi_maxseg = asoc->frag_point;
4662 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4663 info->sctpi_peer_tag = asoc->c.peer_vtag;
4664
4665 mask = asoc->peer.ecn_capable << 1;
4666 mask = (mask | asoc->peer.ipv4_address) << 1;
4667 mask = (mask | asoc->peer.ipv6_address) << 1;
4668 mask = (mask | asoc->peer.hostname_address) << 1;
4669 mask = (mask | asoc->peer.asconf_capable) << 1;
4670 mask = (mask | asoc->peer.prsctp_capable) << 1;
4671 mask = (mask | asoc->peer.auth_capable);
4672 info->sctpi_peer_capable = mask;
4673 mask = asoc->peer.sack_needed << 1;
4674 mask = (mask | asoc->peer.sack_generation) << 1;
4675 mask = (mask | asoc->peer.zero_window_announced);
4676 info->sctpi_peer_sack = mask;
4677
4678 info->sctpi_isacks = asoc->stats.isacks;
4679 info->sctpi_osacks = asoc->stats.osacks;
4680 info->sctpi_opackets = asoc->stats.opackets;
4681 info->sctpi_ipackets = asoc->stats.ipackets;
4682 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4683 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4684 info->sctpi_idupchunks = asoc->stats.idupchunks;
4685 info->sctpi_gapcnt = asoc->stats.gapcnt;
4686 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4687 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4688 info->sctpi_oodchunks = asoc->stats.oodchunks;
4689 info->sctpi_iodchunks = asoc->stats.iodchunks;
4690 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4691 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4692
4693 prim = asoc->peer.primary_path;
Stefano Brivioee6c88b2017-08-23 13:27:13 +02004694 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
Xin Long52c52a62016-04-14 15:35:30 +08004695 info->sctpi_p_state = prim->state;
4696 info->sctpi_p_cwnd = prim->cwnd;
4697 info->sctpi_p_srtt = prim->srtt;
4698 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4699 info->sctpi_p_hbinterval = prim->hbinterval;
4700 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4701 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4702 info->sctpi_p_ssthresh = prim->ssthresh;
4703 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4704 info->sctpi_p_flight_size = prim->flight_size;
4705 info->sctpi_p_error = prim->error_count;
4706
4707 return 0;
4708}
4709EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4710
Xin Long626d16f2016-04-14 15:35:31 +08004711/* use callback to avoid exporting the core structure */
Tom Herbert97a6ec42017-12-04 10:31:41 -08004712void sctp_transport_walk_start(struct rhashtable_iter *iter)
Xin Long626d16f2016-04-14 15:35:31 +08004713{
Xin Long7fda7022016-11-15 23:23:11 +08004714 rhltable_walk_enter(&sctp_transport_hashtable, iter);
Xin Long626d16f2016-04-14 15:35:31 +08004715
Tom Herbert97a6ec42017-12-04 10:31:41 -08004716 rhashtable_walk_start(iter);
Xin Long626d16f2016-04-14 15:35:31 +08004717}
4718
4719void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4720{
4721 rhashtable_walk_stop(iter);
4722 rhashtable_walk_exit(iter);
4723}
4724
4725struct sctp_transport *sctp_transport_get_next(struct net *net,
4726 struct rhashtable_iter *iter)
4727{
4728 struct sctp_transport *t;
4729
4730 t = rhashtable_walk_next(iter);
4731 for (; t; t = rhashtable_walk_next(iter)) {
4732 if (IS_ERR(t)) {
4733 if (PTR_ERR(t) == -EAGAIN)
4734 continue;
4735 break;
4736 }
4737
4738 if (net_eq(sock_net(t->asoc->base.sk), net) &&
4739 t->asoc->peer.primary_path == t)
4740 break;
4741 }
4742
4743 return t;
4744}
4745
4746struct sctp_transport *sctp_transport_get_idx(struct net *net,
4747 struct rhashtable_iter *iter,
4748 int pos)
4749{
4750 void *obj = SEQ_START_TOKEN;
4751
4752 while (pos && (obj = sctp_transport_get_next(net, iter)) &&
4753 !IS_ERR(obj))
4754 pos--;
4755
4756 return obj;
4757}
4758
4759int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
4760 void *p) {
4761 int err = 0;
4762 int hash = 0;
4763 struct sctp_ep_common *epb;
4764 struct sctp_hashbucket *head;
4765
4766 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
4767 hash++, head++) {
Xin Long581409d2017-06-10 14:48:14 +08004768 read_lock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004769 sctp_for_each_hentry(epb, &head->chain) {
4770 err = cb(sctp_ep(epb), p);
4771 if (err)
4772 break;
4773 }
Xin Long581409d2017-06-10 14:48:14 +08004774 read_unlock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004775 }
4776
4777 return err;
4778}
4779EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
4780
4781int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
4782 struct net *net,
4783 const union sctp_addr *laddr,
4784 const union sctp_addr *paddr, void *p)
4785{
4786 struct sctp_transport *transport;
Xin Long08abb792016-12-15 23:05:52 +08004787 int err;
Xin Long626d16f2016-04-14 15:35:31 +08004788
4789 rcu_read_lock();
4790 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
Xin Long626d16f2016-04-14 15:35:31 +08004791 rcu_read_unlock();
Xin Long08abb792016-12-15 23:05:52 +08004792 if (!transport)
4793 return -ENOENT;
4794
Xin Long1cceda782016-09-29 02:55:44 +08004795 err = cb(transport, p);
Xin Longcd26da42016-10-31 20:32:31 +08004796 sctp_transport_put(transport);
Xin Long1cceda782016-09-29 02:55:44 +08004797
Xin Long626d16f2016-04-14 15:35:31 +08004798 return err;
4799}
4800EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
4801
4802int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
Xin Longd25adbe2017-09-15 11:02:21 +08004803 int (*cb_done)(struct sctp_transport *, void *),
4804 struct net *net, int *pos, void *p) {
Xin Long626d16f2016-04-14 15:35:31 +08004805 struct rhashtable_iter hti;
Xin Longd25adbe2017-09-15 11:02:21 +08004806 struct sctp_transport *tsp;
4807 int ret;
Xin Long626d16f2016-04-14 15:35:31 +08004808
Xin Longd25adbe2017-09-15 11:02:21 +08004809again:
Xin Longf53d77e2018-01-23 18:22:25 +08004810 ret = 0;
Tom Herbert97a6ec42017-12-04 10:31:41 -08004811 sctp_transport_walk_start(&hti);
Xin Long626d16f2016-04-14 15:35:31 +08004812
Xin Longd25adbe2017-09-15 11:02:21 +08004813 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
4814 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
4815 if (!sctp_transport_hold(tsp))
Xin Long626d16f2016-04-14 15:35:31 +08004816 continue;
Xin Longd25adbe2017-09-15 11:02:21 +08004817 ret = cb(tsp, p);
4818 if (ret)
Xin Long626d16f2016-04-14 15:35:31 +08004819 break;
Xin Longd25adbe2017-09-15 11:02:21 +08004820 (*pos)++;
4821 sctp_transport_put(tsp);
Xin Long626d16f2016-04-14 15:35:31 +08004822 }
Xin Long626d16f2016-04-14 15:35:31 +08004823 sctp_transport_walk_stop(&hti);
Xin Long53fa1032016-04-14 15:35:35 +08004824
Xin Longd25adbe2017-09-15 11:02:21 +08004825 if (ret) {
4826 if (cb_done && !cb_done(tsp, p)) {
4827 (*pos)++;
4828 sctp_transport_put(tsp);
4829 goto again;
4830 }
4831 sctp_transport_put(tsp);
4832 }
4833
4834 return ret;
Xin Long626d16f2016-04-14 15:35:31 +08004835}
4836EXPORT_SYMBOL_GPL(sctp_for_each_transport);
4837
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838/* 7.2.1 Association Status (SCTP_STATUS)
4839
4840 * Applications can retrieve current status information about an
4841 * association, including association state, peer receiver window size,
4842 * number of unacked data chunks, and number of data chunks pending
4843 * receipt. This information is read-only.
4844 */
4845static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4846 char __user *optval,
4847 int __user *optlen)
4848{
4849 struct sctp_status status;
4850 struct sctp_association *asoc = NULL;
4851 struct sctp_transport *transport;
4852 sctp_assoc_t associd;
4853 int retval = 0;
4854
Neil Horman408f22e2007-06-16 14:03:45 -04004855 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 retval = -EINVAL;
4857 goto out;
4858 }
4859
Neil Horman408f22e2007-06-16 14:03:45 -04004860 len = sizeof(status);
4861 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 retval = -EFAULT;
4863 goto out;
4864 }
4865
4866 associd = status.sstat_assoc_id;
4867 asoc = sctp_id2assoc(sk, associd);
4868 if (!asoc) {
4869 retval = -EINVAL;
4870 goto out;
4871 }
4872
4873 transport = asoc->peer.primary_path;
4874
4875 status.sstat_assoc_id = sctp_assoc2id(asoc);
Daniel Borkmann38ab1fa2014-08-28 15:28:26 +02004876 status.sstat_state = sctp_assoc_to_state(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 status.sstat_rwnd = asoc->peer.rwnd;
4878 status.sstat_unackdata = asoc->unack_data;
4879
4880 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004881 status.sstat_instrms = asoc->stream.incnt;
4882 status.sstat_outstrms = asoc->stream.outcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 status.sstat_fragmentation_point = asoc->frag_point;
4884 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08004885 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4886 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 /* Map ipv4 address into v4-mapped-on-v6 address. */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06004888 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004890 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4892 status.sstat_primary.spinfo_srtt = transport->srtt;
4893 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004894 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
Frank Filz3f7a87d2005-06-20 13:14:57 -07004896 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4897 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4898
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 if (put_user(len, optlen)) {
4900 retval = -EFAULT;
4901 goto out;
4902 }
4903
Daniel Borkmannbb333812013-06-28 19:49:40 +02004904 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
4905 __func__, len, status.sstat_state, status.sstat_rwnd,
4906 status.sstat_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
4908 if (copy_to_user(optval, &status, len)) {
4909 retval = -EFAULT;
4910 goto out;
4911 }
4912
4913out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004914 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915}
4916
4917
4918/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4919 *
4920 * Applications can retrieve information about a specific peer address
4921 * of an association, including its reachability state, congestion
4922 * window, and retransmission timer values. This information is
4923 * read-only.
4924 */
4925static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4926 char __user *optval,
4927 int __user *optlen)
4928{
4929 struct sctp_paddrinfo pinfo;
4930 struct sctp_transport *transport;
4931 int retval = 0;
4932
Neil Horman408f22e2007-06-16 14:03:45 -04004933 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 retval = -EINVAL;
4935 goto out;
4936 }
4937
Neil Horman408f22e2007-06-16 14:03:45 -04004938 len = sizeof(pinfo);
4939 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 retval = -EFAULT;
4941 goto out;
4942 }
4943
4944 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4945 pinfo.spinfo_assoc_id);
4946 if (!transport)
4947 return -EINVAL;
4948
4949 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004950 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 pinfo.spinfo_cwnd = transport->cwnd;
4952 pinfo.spinfo_srtt = transport->srtt;
4953 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004954 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
Frank Filz3f7a87d2005-06-20 13:14:57 -07004956 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4957 pinfo.spinfo_state = SCTP_ACTIVE;
4958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 if (put_user(len, optlen)) {
4960 retval = -EFAULT;
4961 goto out;
4962 }
4963
4964 if (copy_to_user(optval, &pinfo, len)) {
4965 retval = -EFAULT;
4966 goto out;
4967 }
4968
4969out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004970 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971}
4972
4973/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4974 *
4975 * This option is a on/off flag. If enabled no SCTP message
4976 * fragmentation will be performed. Instead if a message being sent
4977 * exceeds the current PMTU size, the message will NOT be sent and
4978 * instead a error will be indicated to the user.
4979 */
4980static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4981 char __user *optval, int __user *optlen)
4982{
4983 int val;
4984
4985 if (len < sizeof(int))
4986 return -EINVAL;
4987
4988 len = sizeof(int);
4989 val = (sctp_sk(sk)->disable_fragments == 1);
4990 if (put_user(len, optlen))
4991 return -EFAULT;
4992 if (copy_to_user(optval, &val, len))
4993 return -EFAULT;
4994 return 0;
4995}
4996
4997/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4998 *
4999 * This socket option is used to specify various notifications and
5000 * ancillary data the user wishes to receive.
5001 */
5002static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5003 int __user *optlen)
5004{
Jiri Slabya4b8e712016-10-21 14:13:24 +02005005 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00005007 if (len > sizeof(struct sctp_event_subscribe))
5008 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04005009 if (put_user(len, optlen))
5010 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
5012 return -EFAULT;
5013 return 0;
5014}
5015
5016/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5017 *
5018 * This socket option is applicable to the UDP-style socket only. When
5019 * set it will cause associations that are idle for more than the
5020 * specified number of seconds to automatically close. An association
5021 * being idle is defined an association that has NOT sent or received
5022 * user data. The special value of '0' indicates that no automatic
5023 * close of any associations should be performed. The option expects an
5024 * integer defining the number of seconds of idle time before an
5025 * association is closed.
5026 */
5027static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5028{
5029 /* Applicable to UDP-style socket only */
5030 if (sctp_style(sk, TCP))
5031 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04005032 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005034 len = sizeof(int);
5035 if (put_user(len, optlen))
5036 return -EFAULT;
David Windsorb2ce04c2017-06-10 22:50:43 -04005037 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 return -EFAULT;
5039 return 0;
5040}
5041
5042/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00005043int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044{
Benjamin Poirier0343c552012-03-08 05:55:58 +00005045 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005046 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 struct socket *sock;
5048 int err = 0;
5049
Xin Longdf80cd92017-10-17 23:26:10 +08005050 /* Do not peel off from one netns to another one. */
5051 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5052 return -EINVAL;
5053
Benjamin Poirier0343c552012-03-08 05:55:58 +00005054 if (!asoc)
5055 return -EINVAL;
5056
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 /* An association cannot be branched off from an already peeled-off
5058 * socket, nor is this supported for tcp style sockets.
5059 */
5060 if (!sctp_style(sk, UDP))
5061 return -EINVAL;
5062
5063 /* Create a new socket. */
5064 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5065 if (err < 0)
5066 return err;
5067
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005068 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005069
5070 /* Make peeled-off sockets more like 1-1 accepted sockets.
5071 * Set the daddr and initialize id to something more random
5072 */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005073 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005074
5075 /* Populate the fields of the newsk from the oldsk and migrate the
5076 * asoc to the newsk.
5077 */
5078 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005079
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 *sockp = sock;
5081
5082 return err;
5083}
Benjamin Poirier0343c552012-03-08 05:55:58 +00005084EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005086static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5087 struct file **newfile, unsigned flags)
5088{
5089 struct socket *newsock;
5090 int retval;
5091
5092 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5093 if (retval < 0)
5094 goto out;
5095
5096 /* Map the socket to an unused fd that can be returned to the user. */
5097 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5098 if (retval < 0) {
5099 sock_release(newsock);
5100 goto out;
5101 }
5102
5103 *newfile = sock_alloc_file(newsock, 0, NULL);
5104 if (IS_ERR(*newfile)) {
5105 put_unused_fd(retval);
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005106 retval = PTR_ERR(*newfile);
5107 *newfile = NULL;
5108 return retval;
5109 }
5110
5111 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5112 retval);
5113
5114 peeloff->sd = retval;
5115
5116 if (flags & SOCK_NONBLOCK)
5117 (*newfile)->f_flags |= O_NONBLOCK;
5118out:
5119 return retval;
5120}
5121
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5123{
5124 sctp_peeloff_arg_t peeloff;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005125 struct file *newfile = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
Neil Horman408f22e2007-06-16 14:03:45 -04005128 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005130 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 if (copy_from_user(&peeloff, optval, len))
5132 return -EFAULT;
5133
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005134 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 if (retval < 0)
5136 goto out;
5137
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04005139 if (put_user(len, optlen)) {
5140 fput(newfile);
5141 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04005142 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04005143 }
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005144
5145 if (copy_to_user(optval, &peeloff, len)) {
5146 fput(newfile);
5147 put_unused_fd(retval);
5148 return -EFAULT;
5149 }
5150 fd_install(retval, newfile);
5151out:
5152 return retval;
5153}
5154
5155static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5156 char __user *optval, int __user *optlen)
5157{
5158 sctp_peeloff_flags_arg_t peeloff;
5159 struct file *newfile = NULL;
5160 int retval = 0;
5161
5162 if (len < sizeof(sctp_peeloff_flags_arg_t))
5163 return -EINVAL;
5164 len = sizeof(sctp_peeloff_flags_arg_t);
5165 if (copy_from_user(&peeloff, optval, len))
5166 return -EFAULT;
5167
5168 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5169 &newfile, peeloff.flags);
5170 if (retval < 0)
5171 goto out;
5172
5173 /* Return the fd mapped to the new socket. */
5174 if (put_user(len, optlen)) {
5175 fput(newfile);
5176 put_unused_fd(retval);
5177 return -EFAULT;
5178 }
5179
Al Viro56b31d12012-08-18 00:25:51 -04005180 if (copy_to_user(optval, &peeloff, len)) {
5181 fput(newfile);
5182 put_unused_fd(retval);
5183 return -EFAULT;
5184 }
5185 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186out:
5187 return retval;
5188}
5189
5190/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5191 *
5192 * Applications can enable or disable heartbeats for any peer address of
5193 * an association, modify an address's heartbeat interval, force a
5194 * heartbeat to be sent immediately, and adjust the address's maximum
5195 * number of retransmissions sent before an address is considered
5196 * unreachable. The following structure is used to access and modify an
5197 * address's parameters:
5198 *
5199 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005200 * sctp_assoc_t spp_assoc_id;
5201 * struct sockaddr_storage spp_address;
5202 * uint32_t spp_hbinterval;
5203 * uint16_t spp_pathmaxrxt;
5204 * uint32_t spp_pathmtu;
5205 * uint32_t spp_sackdelay;
5206 * uint32_t spp_flags;
5207 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08005209 * spp_assoc_id - (one-to-many style socket) This is filled in the
5210 * application, and identifies the association for
5211 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 * spp_address - This specifies which address is of interest.
5213 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005214 * in milliseconds. If a value of zero
5215 * is present in this field then no changes are to
5216 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 * spp_pathmaxrxt - This contains the maximum number of
5218 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08005219 * considered unreachable. If a value of zero
5220 * is present in this field then no changes are to
5221 * be made to this parameter.
5222 * spp_pathmtu - When Path MTU discovery is disabled the value
5223 * specified here will be the "fixed" path mtu.
5224 * Note that if the spp_address field is empty
5225 * then all associations on this address will
5226 * have this fixed path mtu set upon them.
5227 *
5228 * spp_sackdelay - When delayed sack is enabled, this value specifies
5229 * the number of milliseconds that sacks will be delayed
5230 * for. This value will apply to all addresses of an
5231 * association if the spp_address field is empty. Note
5232 * also, that if delayed sack is enabled and this
5233 * value is set to 0, no change is made to the last
5234 * recorded delayed sack timer value.
5235 *
5236 * spp_flags - These flags are used to control various features
5237 * on an association. The flag field may contain
5238 * zero or more of the following options.
5239 *
5240 * SPP_HB_ENABLE - Enable heartbeats on the
5241 * specified address. Note that if the address
5242 * field is empty all addresses for the association
5243 * have heartbeats enabled upon them.
5244 *
5245 * SPP_HB_DISABLE - Disable heartbeats on the
5246 * speicifed address. Note that if the address
5247 * field is empty all addresses for the association
5248 * will have their heartbeats disabled. Note also
5249 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5250 * mutually exclusive, only one of these two should
5251 * be specified. Enabling both fields will have
5252 * undetermined results.
5253 *
5254 * SPP_HB_DEMAND - Request a user initiated heartbeat
5255 * to be made immediately.
5256 *
5257 * SPP_PMTUD_ENABLE - This field will enable PMTU
5258 * discovery upon the specified address. Note that
5259 * if the address feild is empty then all addresses
5260 * on the association are effected.
5261 *
5262 * SPP_PMTUD_DISABLE - This field will disable PMTU
5263 * discovery upon the specified address. Note that
5264 * if the address feild is empty then all addresses
5265 * on the association are effected. Not also that
5266 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5267 * exclusive. Enabling both will have undetermined
5268 * results.
5269 *
5270 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5271 * on delayed sack. The time specified in spp_sackdelay
5272 * is used to specify the sack delay for this address. Note
5273 * that if spp_address is empty then all addresses will
5274 * enable delayed sack and take on the sack delay
5275 * value specified in spp_sackdelay.
5276 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5277 * off delayed sack. If the spp_address field is blank then
5278 * delayed sack is disabled for the entire association. Note
5279 * also that this field is mutually exclusive to
5280 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5281 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 */
5283static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005284 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285{
Frank Filz52ccb8e2005-12-22 11:36:46 -08005286 struct sctp_paddrparams params;
5287 struct sctp_transport *trans = NULL;
5288 struct sctp_association *asoc = NULL;
5289 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
Neil Horman408f22e2007-06-16 14:03:45 -04005291 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005293 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 if (copy_from_user(&params, optval, len))
5295 return -EFAULT;
5296
Frank Filz52ccb8e2005-12-22 11:36:46 -08005297 /* If an address other than INADDR_ANY is specified, and
5298 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 */
wangweidongcb3f8372013-12-23 12:16:50 +08005300 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005301 trans = sctp_addr_id2transport(sk, &params.spp_address,
5302 params.spp_assoc_id);
5303 if (!trans) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005304 pr_debug("%s: failed no transport\n", __func__);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005305 return -EINVAL;
5306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 }
5308
Frank Filz52ccb8e2005-12-22 11:36:46 -08005309 /* Get association, if assoc_id != 0 and the socket is a one
5310 * to many style socket, and an association was not found, then
5311 * the id was invalid.
5312 */
5313 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5314 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005315 pr_debug("%s: failed no association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08005317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318
Frank Filz52ccb8e2005-12-22 11:36:46 -08005319 if (trans) {
5320 /* Fetch transport values. */
5321 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5322 params.spp_pathmtu = trans->pathmtu;
5323 params.spp_pathmaxrxt = trans->pathmaxrxt;
5324 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
Frank Filz52ccb8e2005-12-22 11:36:46 -08005326 /*draft-11 doesn't say what to return in spp_flags*/
5327 params.spp_flags = trans->param_flags;
5328 } else if (asoc) {
5329 /* Fetch association values. */
5330 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5331 params.spp_pathmtu = asoc->pathmtu;
5332 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5333 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
Frank Filz52ccb8e2005-12-22 11:36:46 -08005335 /*draft-11 doesn't say what to return in spp_flags*/
5336 params.spp_flags = asoc->param_flags;
5337 } else {
5338 /* Fetch socket values. */
5339 params.spp_hbinterval = sp->hbinterval;
5340 params.spp_pathmtu = sp->pathmtu;
5341 params.spp_sackdelay = sp->sackdelay;
5342 params.spp_pathmaxrxt = sp->pathmaxrxt;
5343
5344 /*draft-11 doesn't say what to return in spp_flags*/
5345 params.spp_flags = sp->param_flags;
5346 }
5347
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 if (copy_to_user(optval, &params, len))
5349 return -EFAULT;
5350
5351 if (put_user(len, optlen))
5352 return -EFAULT;
5353
5354 return 0;
5355}
5356
Wei Yongjund364d922008-05-09 15:13:26 -07005357/*
5358 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08005359 *
Wei Yongjund364d922008-05-09 15:13:26 -07005360 * This option will effect the way delayed acks are performed. This
5361 * option allows you to get or set the delayed ack time, in
5362 * milliseconds. It also allows changing the delayed ack frequency.
5363 * Changing the frequency to 1 disables the delayed sack algorithm. If
5364 * the assoc_id is 0, then this sets or gets the endpoints default
5365 * values. If the assoc_id field is non-zero, then the set or get
5366 * effects the specified association for the one to many model (the
5367 * assoc_id field is ignored by the one to one model). Note that if
5368 * sack_delay or sack_freq are 0 when setting this option, then the
5369 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08005370 *
Wei Yongjund364d922008-05-09 15:13:26 -07005371 * struct sctp_sack_info {
5372 * sctp_assoc_t sack_assoc_id;
5373 * uint32_t sack_delay;
5374 * uint32_t sack_freq;
5375 * };
Frank Filz77086102005-12-22 11:37:30 -08005376 *
Wei Yongjund364d922008-05-09 15:13:26 -07005377 * sack_assoc_id - This parameter, indicates which association the user
5378 * is performing an action upon. Note that if this field's value is
5379 * zero then the endpoints default value is changed (effecting future
5380 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08005381 *
Wei Yongjund364d922008-05-09 15:13:26 -07005382 * sack_delay - This parameter contains the number of milliseconds that
5383 * the user is requesting the delayed ACK timer be set to. Note that
5384 * this value is defined in the standard to be between 200 and 500
5385 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08005386 *
Wei Yongjund364d922008-05-09 15:13:26 -07005387 * sack_freq - This parameter contains the number of packets that must
5388 * be received before a sack is sent without waiting for the delay
5389 * timer to expire. The default value for this is 2, setting this
5390 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08005391 */
Wei Yongjund364d922008-05-09 15:13:26 -07005392static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08005393 char __user *optval,
5394 int __user *optlen)
5395{
Wei Yongjund364d922008-05-09 15:13:26 -07005396 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08005397 struct sctp_association *asoc = NULL;
5398 struct sctp_sock *sp = sctp_sk(sk);
5399
Wei Yongjund364d922008-05-09 15:13:26 -07005400 if (len >= sizeof(struct sctp_sack_info)) {
5401 len = sizeof(struct sctp_sack_info);
5402
5403 if (copy_from_user(&params, optval, len))
5404 return -EFAULT;
5405 } else if (len == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05005406 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005407 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005408 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005409 "Use struct sctp_sack_info instead\n",
5410 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07005411 if (copy_from_user(&params, optval, len))
5412 return -EFAULT;
5413 } else
wangweidongcb3f8372013-12-23 12:16:50 +08005414 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08005415
Wei Yongjund364d922008-05-09 15:13:26 -07005416 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08005417 * to many style socket, and an association was not found, then
5418 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005419 */
Wei Yongjund364d922008-05-09 15:13:26 -07005420 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5421 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08005422 return -EINVAL;
5423
5424 if (asoc) {
5425 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005426 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5427 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08005428 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07005429 params.sack_freq = asoc->sackfreq;
5430
5431 } else {
5432 params.sack_delay = 0;
5433 params.sack_freq = 1;
5434 }
Frank Filz77086102005-12-22 11:37:30 -08005435 } else {
5436 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005437 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5438 params.sack_delay = sp->sackdelay;
5439 params.sack_freq = sp->sackfreq;
5440 } else {
5441 params.sack_delay = 0;
5442 params.sack_freq = 1;
5443 }
Frank Filz77086102005-12-22 11:37:30 -08005444 }
5445
5446 if (copy_to_user(optval, &params, len))
5447 return -EFAULT;
5448
5449 if (put_user(len, optlen))
5450 return -EFAULT;
5451
5452 return 0;
5453}
5454
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5456 *
5457 * Applications can specify protocol parameters for the default association
5458 * initialization. The option name argument to setsockopt() and getsockopt()
5459 * is SCTP_INITMSG.
5460 *
5461 * Setting initialization parameters is effective only on an unconnected
5462 * socket (for UDP-style sockets only future associations are effected
5463 * by the change). With TCP-style sockets, this option is inherited by
5464 * sockets derived from a listener socket.
5465 */
5466static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5467{
Neil Horman408f22e2007-06-16 14:03:45 -04005468 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005470 len = sizeof(struct sctp_initmsg);
5471 if (put_user(len, optlen))
5472 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5474 return -EFAULT;
5475 return 0;
5476}
5477
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005479static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5480 char __user *optval, int __user *optlen)
5481{
5482 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005483 int cnt = 0;
5484 struct sctp_getaddrs getaddrs;
5485 struct sctp_transport *from;
5486 void __user *to;
5487 union sctp_addr temp;
5488 struct sctp_sock *sp = sctp_sk(sk);
5489 int addrlen;
5490 size_t space_left;
5491 int bytes_copied;
5492
5493 if (len < sizeof(struct sctp_getaddrs))
5494 return -EINVAL;
5495
5496 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5497 return -EFAULT;
5498
5499 /* For UDP-style sockets, id specifies the association to query. */
5500 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5501 if (!asoc)
5502 return -EINVAL;
5503
wangweidongcb3f8372013-12-23 12:16:50 +08005504 to = optval + offsetof(struct sctp_getaddrs, addrs);
5505 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005506
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07005507 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5508 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08005509 memcpy(&temp, &from->ipaddr, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005510 addrlen = sctp_get_pf_specific(sk->sk_family)
5511 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005512 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005513 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005514 if (copy_to_user(to, &temp, addrlen))
5515 return -EFAULT;
5516 to += addrlen;
5517 cnt++;
5518 space_left -= addrlen;
5519 }
5520
5521 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5522 return -EFAULT;
5523 bytes_copied = ((char __user *)to) - optval;
5524 if (put_user(bytes_copied, optlen))
5525 return -EFAULT;
5526
5527 return 0;
5528}
5529
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005530static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5531 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005532{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005533 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005534 union sctp_addr temp;
5535 int cnt = 0;
5536 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005537 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005538
Vlad Yasevich29303542007-09-16 16:02:12 -07005539 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005540 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07005541 if (!addr->valid)
5542 continue;
5543
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005544 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08005545 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005546 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005547 if ((PF_INET6 == sk->sk_family) &&
5548 inet_v6_ipv6only(sk) &&
5549 (AF_INET == addr->a.sa.sa_family))
5550 continue;
Al Viro6244be42006-11-20 17:21:44 -08005551 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05005552 if (!temp.v4.sin_port)
5553 temp.v4.sin_port = htons(port);
5554
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005555 addrlen = sctp_get_pf_specific(sk->sk_family)
5556 ->addr_to_user(sctp_sk(sk), &temp);
5557
Vlad Yasevich29303542007-09-16 16:02:12 -07005558 if (space_left < addrlen) {
5559 cnt = -ENOMEM;
5560 break;
5561 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005562 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08005563
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005564 to += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005565 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005566 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04005567 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005568 }
Vlad Yasevich29303542007-09-16 16:02:12 -07005569 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005570
5571 return cnt;
5572}
5573
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005575static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5576 char __user *optval, int __user *optlen)
5577{
5578 struct sctp_bind_addr *bp;
5579 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005580 int cnt = 0;
5581 struct sctp_getaddrs getaddrs;
5582 struct sctp_sockaddr_entry *addr;
5583 void __user *to;
5584 union sctp_addr temp;
5585 struct sctp_sock *sp = sctp_sk(sk);
5586 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005587 int err = 0;
5588 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005589 int bytes_copied = 0;
5590 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005591 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005592
Neil Horman408f22e2007-06-16 14:03:45 -04005593 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005594 return -EINVAL;
5595
5596 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5597 return -EFAULT;
5598
5599 /*
5600 * For UDP-style sockets, id specifies the association to query.
5601 * If the id field is set to the value '0' then the locally bound
5602 * addresses are returned without regard to any particular
5603 * association.
5604 */
5605 if (0 == getaddrs.assoc_id) {
5606 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005607 } else {
5608 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5609 if (!asoc)
5610 return -EINVAL;
5611 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005612 }
5613
wangweidongcb3f8372013-12-23 12:16:50 +08005614 to = optval + offsetof(struct sctp_getaddrs, addrs);
5615 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Neil Horman186e2342007-06-18 19:59:16 -04005616
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02005617 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005618 if (!addrs)
5619 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005620
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005621 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5622 * addresses from the global local address list.
5623 */
5624 if (sctp_list_single_entry(&bp->address_list)) {
5625 addr = list_entry(bp->address_list.next,
5626 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04005627 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005628 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5629 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005630 if (cnt < 0) {
5631 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005632 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005633 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005634 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005635 }
5636 }
5637
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005638 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005639 /* Protection on the bound address list is not needed since
5640 * in the socket option context we hold a socket lock and
5641 * thus the bound address list can't change.
5642 */
5643 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08005644 memcpy(&temp, &addr->a, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005645 addrlen = sctp_get_pf_specific(sk->sk_family)
5646 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005647 if (space_left < addrlen) {
5648 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07005649 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005650 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005651 memcpy(buf, &temp, addrlen);
5652 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005653 bytes_copied += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005654 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005655 space_left -= addrlen;
5656 }
5657
5658copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005659 if (copy_to_user(to, addrs, bytes_copied)) {
5660 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005661 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005662 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005663 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
5664 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005665 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005666 }
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02005667 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
5668 * but we can't change it anymore.
5669 */
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005670 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005671 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005672out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005673 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005674 return err;
5675}
5676
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
5678 *
5679 * Requests that the local SCTP stack use the enclosed peer address as
5680 * the association primary. The enclosed address must be one of the
5681 * association peer's addresses.
5682 */
5683static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
5684 char __user *optval, int __user *optlen)
5685{
5686 struct sctp_prim prim;
5687 struct sctp_association *asoc;
5688 struct sctp_sock *sp = sctp_sk(sk);
5689
Neil Horman408f22e2007-06-16 14:03:45 -04005690 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 return -EINVAL;
5692
Neil Horman408f22e2007-06-16 14:03:45 -04005693 len = sizeof(struct sctp_prim);
5694
5695 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 return -EFAULT;
5697
5698 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
5699 if (!asoc)
5700 return -EINVAL;
5701
5702 if (!asoc->peer.primary_path)
5703 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005704
Al Viro8cec6b82006-11-20 17:23:01 -08005705 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
5706 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005708 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 (union sctp_addr *)&prim.ssp_addr);
5710
Neil Horman408f22e2007-06-16 14:03:45 -04005711 if (put_user(len, optlen))
5712 return -EFAULT;
5713 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 return -EFAULT;
5715
5716 return 0;
5717}
5718
5719/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005720 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005722 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 * Indication parameter for all future INIT and INIT-ACK exchanges.
5724 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005725static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 char __user *optval, int __user *optlen)
5727{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005728 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729
Neil Horman408f22e2007-06-16 14:03:45 -04005730 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 return -EINVAL;
5732
Neil Horman408f22e2007-06-16 14:03:45 -04005733 len = sizeof(struct sctp_setadaptation);
5734
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005735 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04005736
5737 if (put_user(len, optlen))
5738 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005739 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07005741
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 return 0;
5743}
5744
5745/*
5746 *
5747 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
5748 *
5749 * Applications that wish to use the sendto() system call may wish to
5750 * specify a default set of parameters that would normally be supplied
5751 * through the inclusion of ancillary data. This socket option allows
5752 * such an application to set the default sctp_sndrcvinfo structure.
5753
5754
5755 * The application that wishes to use this socket option simply passes
5756 * in to this call the sctp_sndrcvinfo structure defined in Section
5757 * 5.2.2) The input parameters accepted by this call include
5758 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
5759 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
5760 * to this call if the caller is using the UDP model.
5761 *
5762 * For getsockopt, it get the default sctp_sndrcvinfo structure.
5763 */
5764static int sctp_getsockopt_default_send_param(struct sock *sk,
5765 int len, char __user *optval,
5766 int __user *optlen)
5767{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005769 struct sctp_association *asoc;
5770 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005772 if (len < sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005774
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005775 len = sizeof(info);
Neil Horman408f22e2007-06-16 14:03:45 -04005776
5777 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 return -EFAULT;
5779
5780 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
5781 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
5782 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 if (asoc) {
5784 info.sinfo_stream = asoc->default_stream;
5785 info.sinfo_flags = asoc->default_flags;
5786 info.sinfo_ppid = asoc->default_ppid;
5787 info.sinfo_context = asoc->default_context;
5788 info.sinfo_timetolive = asoc->default_timetolive;
5789 } else {
5790 info.sinfo_stream = sp->default_stream;
5791 info.sinfo_flags = sp->default_flags;
5792 info.sinfo_ppid = sp->default_ppid;
5793 info.sinfo_context = sp->default_context;
5794 info.sinfo_timetolive = sp->default_timetolive;
5795 }
5796
Neil Horman408f22e2007-06-16 14:03:45 -04005797 if (put_user(len, optlen))
5798 return -EFAULT;
5799 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 return -EFAULT;
5801
5802 return 0;
5803}
5804
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005805/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
5806 * (SCTP_DEFAULT_SNDINFO)
5807 */
5808static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
5809 char __user *optval,
5810 int __user *optlen)
5811{
5812 struct sctp_sock *sp = sctp_sk(sk);
5813 struct sctp_association *asoc;
5814 struct sctp_sndinfo info;
5815
5816 if (len < sizeof(info))
5817 return -EINVAL;
5818
5819 len = sizeof(info);
5820
5821 if (copy_from_user(&info, optval, len))
5822 return -EFAULT;
5823
5824 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
5825 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
5826 return -EINVAL;
5827 if (asoc) {
5828 info.snd_sid = asoc->default_stream;
5829 info.snd_flags = asoc->default_flags;
5830 info.snd_ppid = asoc->default_ppid;
5831 info.snd_context = asoc->default_context;
5832 } else {
5833 info.snd_sid = sp->default_stream;
5834 info.snd_flags = sp->default_flags;
5835 info.snd_ppid = sp->default_ppid;
5836 info.snd_context = sp->default_context;
5837 }
5838
5839 if (put_user(len, optlen))
5840 return -EFAULT;
5841 if (copy_to_user(optval, &info, len))
5842 return -EFAULT;
5843
5844 return 0;
5845}
5846
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847/*
5848 *
5849 * 7.1.5 SCTP_NODELAY
5850 *
5851 * Turn on/off any Nagle-like algorithm. This means that packets are
5852 * generally sent as soon as possible and no unnecessary delays are
5853 * introduced, at the cost of more packets in the network. Expects an
5854 * integer boolean flag.
5855 */
5856
5857static int sctp_getsockopt_nodelay(struct sock *sk, int len,
5858 char __user *optval, int __user *optlen)
5859{
5860 int val;
5861
5862 if (len < sizeof(int))
5863 return -EINVAL;
5864
5865 len = sizeof(int);
5866 val = (sctp_sk(sk)->nodelay == 1);
5867 if (put_user(len, optlen))
5868 return -EFAULT;
5869 if (copy_to_user(optval, &val, len))
5870 return -EFAULT;
5871 return 0;
5872}
5873
5874/*
5875 *
5876 * 7.1.1 SCTP_RTOINFO
5877 *
5878 * The protocol parameters used to initialize and bound retransmission
5879 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
5880 * and modify these parameters.
5881 * All parameters are time values, in milliseconds. A value of 0, when
5882 * modifying the parameters, indicates that the current value should not
5883 * be changed.
5884 *
5885 */
5886static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
5887 char __user *optval,
5888 int __user *optlen) {
5889 struct sctp_rtoinfo rtoinfo;
5890 struct sctp_association *asoc;
5891
Neil Horman408f22e2007-06-16 14:03:45 -04005892 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 return -EINVAL;
5894
Neil Horman408f22e2007-06-16 14:03:45 -04005895 len = sizeof(struct sctp_rtoinfo);
5896
5897 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 return -EFAULT;
5899
5900 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
5901
5902 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
5903 return -EINVAL;
5904
5905 /* Values corresponding to the specific association. */
5906 if (asoc) {
5907 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
5908 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
5909 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
5910 } else {
5911 /* Values corresponding to the endpoint. */
5912 struct sctp_sock *sp = sctp_sk(sk);
5913
5914 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
5915 rtoinfo.srto_max = sp->rtoinfo.srto_max;
5916 rtoinfo.srto_min = sp->rtoinfo.srto_min;
5917 }
5918
5919 if (put_user(len, optlen))
5920 return -EFAULT;
5921
5922 if (copy_to_user(optval, &rtoinfo, len))
5923 return -EFAULT;
5924
5925 return 0;
5926}
5927
5928/*
5929 *
5930 * 7.1.2 SCTP_ASSOCINFO
5931 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02005932 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 * of the association.
5934 * Returns an error if the new association retransmission value is
5935 * greater than the sum of the retransmission value of the peer.
5936 * See [SCTP] for more information.
5937 *
5938 */
5939static int sctp_getsockopt_associnfo(struct sock *sk, int len,
5940 char __user *optval,
5941 int __user *optlen)
5942{
5943
5944 struct sctp_assocparams assocparams;
5945 struct sctp_association *asoc;
5946 struct list_head *pos;
5947 int cnt = 0;
5948
Neil Horman408f22e2007-06-16 14:03:45 -04005949 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 return -EINVAL;
5951
Neil Horman408f22e2007-06-16 14:03:45 -04005952 len = sizeof(struct sctp_assocparams);
5953
5954 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 return -EFAULT;
5956
5957 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
5958
5959 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
5960 return -EINVAL;
5961
5962 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07005963 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
5965 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
5966 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
Daniel Borkmann52db8822013-06-25 18:17:27 +02005967 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968
5969 list_for_each(pos, &asoc->peer.transport_addr_list) {
wangweidongcb3f8372013-12-23 12:16:50 +08005970 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 }
5972
5973 assocparams.sasoc_number_peer_destinations = cnt;
5974 } else {
5975 /* Values corresponding to the endpoint */
5976 struct sctp_sock *sp = sctp_sk(sk);
5977
5978 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
5979 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
5980 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
5981 assocparams.sasoc_cookie_life =
5982 sp->assocparams.sasoc_cookie_life;
5983 assocparams.sasoc_number_peer_destinations =
5984 sp->assocparams.
5985 sasoc_number_peer_destinations;
5986 }
5987
5988 if (put_user(len, optlen))
5989 return -EFAULT;
5990
5991 if (copy_to_user(optval, &assocparams, len))
5992 return -EFAULT;
5993
5994 return 0;
5995}
5996
5997/*
5998 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
5999 *
6000 * This socket option is a boolean flag which turns on or off mapped V4
6001 * addresses. If this option is turned on and the socket is type
6002 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6003 * If this option is turned off, then no mapping will be done of V4
6004 * addresses and a user will receive both PF_INET6 and PF_INET type
6005 * addresses on the socket.
6006 */
6007static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6008 char __user *optval, int __user *optlen)
6009{
6010 int val;
6011 struct sctp_sock *sp = sctp_sk(sk);
6012
6013 if (len < sizeof(int))
6014 return -EINVAL;
6015
6016 len = sizeof(int);
6017 val = sp->v4mapped;
6018 if (put_user(len, optlen))
6019 return -EFAULT;
6020 if (copy_to_user(optval, &val, len))
6021 return -EFAULT;
6022
6023 return 0;
6024}
6025
6026/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006027 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6028 * (chapter and verse is quoted at sctp_setsockopt_context())
6029 */
6030static int sctp_getsockopt_context(struct sock *sk, int len,
6031 char __user *optval, int __user *optlen)
6032{
6033 struct sctp_assoc_value params;
6034 struct sctp_sock *sp;
6035 struct sctp_association *asoc;
6036
Neil Horman408f22e2007-06-16 14:03:45 -04006037 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006038 return -EINVAL;
6039
Neil Horman408f22e2007-06-16 14:03:45 -04006040 len = sizeof(struct sctp_assoc_value);
6041
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006042 if (copy_from_user(&params, optval, len))
6043 return -EFAULT;
6044
6045 sp = sctp_sk(sk);
6046
6047 if (params.assoc_id != 0) {
6048 asoc = sctp_id2assoc(sk, params.assoc_id);
6049 if (!asoc)
6050 return -EINVAL;
6051 params.assoc_value = asoc->default_rcv_context;
6052 } else {
6053 params.assoc_value = sp->default_rcv_context;
6054 }
6055
6056 if (put_user(len, optlen))
6057 return -EFAULT;
6058 if (copy_to_user(optval, &params, len))
6059 return -EFAULT;
6060
6061 return 0;
6062}
6063
6064/*
Wei Yongjune89c2092008-12-25 16:54:58 -08006065 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6066 * This option will get or set the maximum size to put in any outgoing
6067 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 * fragmented by SCTP into the specified size. Note that the underlying
6069 * SCTP implementation may fragment into smaller sized chunks when the
6070 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08006071 * the user. The default value for this option is '0' which indicates
6072 * the user is NOT limiting fragmentation and only the PMTU will effect
6073 * SCTP's choice of DATA chunk size. Note also that values set larger
6074 * than the maximum size of an IP datagram will effectively let SCTP
6075 * control fragmentation (i.e. the same as setting this option to 0).
6076 *
6077 * The following structure is used to access and modify this parameter:
6078 *
6079 * struct sctp_assoc_value {
6080 * sctp_assoc_t assoc_id;
6081 * uint32_t assoc_value;
6082 * };
6083 *
6084 * assoc_id: This parameter is ignored for one-to-one style sockets.
6085 * For one-to-many style sockets this parameter indicates which
6086 * association the user is performing an action upon. Note that if
6087 * this field's value is zero then the endpoints default value is
6088 * changed (effecting future associations only).
6089 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 */
6091static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6092 char __user *optval, int __user *optlen)
6093{
Wei Yongjune89c2092008-12-25 16:54:58 -08006094 struct sctp_assoc_value params;
6095 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096
Wei Yongjune89c2092008-12-25 16:54:58 -08006097 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006098 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006099 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006100 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006101 "Use struct sctp_assoc_value instead\n",
6102 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08006103 params.assoc_id = 0;
6104 } else if (len >= sizeof(struct sctp_assoc_value)) {
6105 len = sizeof(struct sctp_assoc_value);
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006106 if (copy_from_user(&params, optval, len))
Wei Yongjune89c2092008-12-25 16:54:58 -08006107 return -EFAULT;
6108 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 return -EINVAL;
6110
Wei Yongjune89c2092008-12-25 16:54:58 -08006111 asoc = sctp_id2assoc(sk, params.assoc_id);
6112 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
6113 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114
Wei Yongjune89c2092008-12-25 16:54:58 -08006115 if (asoc)
6116 params.assoc_value = asoc->frag_point;
6117 else
6118 params.assoc_value = sctp_sk(sk)->user_frag;
6119
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 if (put_user(len, optlen))
6121 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08006122 if (len == sizeof(int)) {
6123 if (copy_to_user(optval, &params.assoc_value, len))
6124 return -EFAULT;
6125 } else {
6126 if (copy_to_user(optval, &params, len))
6127 return -EFAULT;
6128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129
6130 return 0;
6131}
6132
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006133/*
6134 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6135 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6136 */
6137static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6138 char __user *optval, int __user *optlen)
6139{
6140 int val;
6141
6142 if (len < sizeof(int))
6143 return -EINVAL;
6144
6145 len = sizeof(int);
6146
6147 val = sctp_sk(sk)->frag_interleave;
6148 if (put_user(len, optlen))
6149 return -EFAULT;
6150 if (copy_to_user(optval, &val, len))
6151 return -EFAULT;
6152
6153 return 0;
6154}
6155
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006156/*
6157 * 7.1.25. Set or Get the sctp partial delivery point
6158 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6159 */
6160static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6161 char __user *optval,
6162 int __user *optlen)
6163{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09006164 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006165
6166 if (len < sizeof(u32))
6167 return -EINVAL;
6168
6169 len = sizeof(u32);
6170
6171 val = sctp_sk(sk)->pd_point;
6172 if (put_user(len, optlen))
6173 return -EFAULT;
6174 if (copy_to_user(optval, &val, len))
6175 return -EFAULT;
6176
Wei Yongjun7d743b72010-12-14 16:10:41 +00006177 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006178}
6179
Vlad Yasevich70331572007-03-23 11:34:36 -07006180/*
6181 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6182 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6183 */
6184static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6185 char __user *optval,
6186 int __user *optlen)
6187{
Neil Horman219b99a2008-03-05 13:44:46 -08006188 struct sctp_assoc_value params;
6189 struct sctp_sock *sp;
6190 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07006191
Neil Horman219b99a2008-03-05 13:44:46 -08006192 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006193 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006194 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006195 "Use of int in max_burst socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006196 "Use struct sctp_assoc_value instead\n",
6197 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08006198 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00006199 } else if (len >= sizeof(struct sctp_assoc_value)) {
6200 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08006201 if (copy_from_user(&params, optval, len))
6202 return -EFAULT;
6203 } else
6204 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07006205
Neil Horman219b99a2008-03-05 13:44:46 -08006206 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07006207
Neil Horman219b99a2008-03-05 13:44:46 -08006208 if (params.assoc_id != 0) {
6209 asoc = sctp_id2assoc(sk, params.assoc_id);
6210 if (!asoc)
6211 return -EINVAL;
6212 params.assoc_value = asoc->max_burst;
6213 } else
6214 params.assoc_value = sp->max_burst;
6215
6216 if (len == sizeof(int)) {
6217 if (copy_to_user(optval, &params.assoc_value, len))
6218 return -EFAULT;
6219 } else {
6220 if (copy_to_user(optval, &params, len))
6221 return -EFAULT;
6222 }
6223
6224 return 0;
6225
Vlad Yasevich70331572007-03-23 11:34:36 -07006226}
6227
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006228static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6229 char __user *optval, int __user *optlen)
6230{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006231 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006232 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006233 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006234 __u16 data_len = 0;
6235 u32 num_idents;
Xin Long7a84bd42016-02-03 23:33:30 +08006236 int i;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006237
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006238 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006239 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006240
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006241 hmacs = ep->auth_hmacs_list;
Xin Long3c918702017-06-30 11:52:16 +08006242 data_len = ntohs(hmacs->param_hdr.length) -
6243 sizeof(struct sctp_paramhdr);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006244
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006245 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006246 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006247
6248 len = sizeof(struct sctp_hmacalgo) + data_len;
6249 num_idents = data_len / sizeof(u16);
6250
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006251 if (put_user(len, optlen))
6252 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006253 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006254 return -EFAULT;
Xin Long7a84bd42016-02-03 23:33:30 +08006255 for (i = 0; i < num_idents; i++) {
6256 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6257
6258 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6259 return -EFAULT;
6260 }
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006261 return 0;
6262}
6263
6264static int sctp_getsockopt_active_key(struct sock *sk, int len,
6265 char __user *optval, int __user *optlen)
6266{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006267 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006268 struct sctp_authkeyid val;
6269 struct sctp_association *asoc;
6270
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006271 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006272 return -EACCES;
6273
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006274 if (len < sizeof(struct sctp_authkeyid))
6275 return -EINVAL;
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006276
6277 len = sizeof(struct sctp_authkeyid);
6278 if (copy_from_user(&val, optval, len))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006279 return -EFAULT;
6280
6281 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6282 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6283 return -EINVAL;
6284
6285 if (asoc)
6286 val.scact_keynumber = asoc->active_key_id;
6287 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006288 val.scact_keynumber = ep->active_key_id;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006289
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006290 if (put_user(len, optlen))
6291 return -EFAULT;
6292 if (copy_to_user(optval, &val, len))
6293 return -EFAULT;
6294
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006295 return 0;
6296}
6297
6298static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6299 char __user *optval, int __user *optlen)
6300{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006301 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006302 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006303 struct sctp_authchunks val;
6304 struct sctp_association *asoc;
6305 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006306 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006307 char __user *to;
6308
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006309 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006310 return -EACCES;
6311
6312 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006313 return -EINVAL;
6314
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006315 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006316 return -EFAULT;
6317
Al Viro411223c2007-10-14 19:21:20 +01006318 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006319 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6320 if (!asoc)
6321 return -EINVAL;
6322
6323 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006324 if (!ch)
6325 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006326
6327 /* See if the user provided enough room for all the data */
Xin Long3c918702017-06-30 11:52:16 +08006328 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevichb40db682008-02-27 14:40:37 -05006329 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006330 return -EINVAL;
6331
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006332 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006333 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006334num:
6335 len = sizeof(struct sctp_authchunks) + num_chunks;
wangweidong8d726512013-12-23 12:16:53 +08006336 if (put_user(len, optlen))
6337 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006338 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6339 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006340 return 0;
6341}
6342
6343static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6344 char __user *optval, int __user *optlen)
6345{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006346 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006347 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006348 struct sctp_authchunks val;
6349 struct sctp_association *asoc;
6350 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006351 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006352 char __user *to;
6353
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006354 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006355 return -EACCES;
6356
6357 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006358 return -EINVAL;
6359
Marcelo Ricardo Leitnerc76f97c2018-01-08 19:02:29 -02006360 if (copy_from_user(&val, optval, sizeof(val)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006361 return -EFAULT;
6362
Al Viro411223c2007-10-14 19:21:20 +01006363 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006364 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6365 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6366 return -EINVAL;
6367
6368 if (asoc)
wangweidong26ac8e52013-12-23 12:16:51 +08006369 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006370 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006371 ch = ep->auth_chunk_list;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006372
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006373 if (!ch)
6374 goto num;
6375
Xin Long3c918702017-06-30 11:52:16 +08006376 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006377 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006378 return -EINVAL;
6379
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006380 if (copy_to_user(to, ch->chunks, num_chunks))
6381 return -EFAULT;
6382num:
6383 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006384 if (put_user(len, optlen))
6385 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006386 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6387 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006388
6389 return 0;
6390}
6391
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08006392/*
6393 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6394 * This option gets the current number of associations that are attached
6395 * to a one-to-many style socket. The option value is an uint32_t.
6396 */
6397static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6398 char __user *optval, int __user *optlen)
6399{
6400 struct sctp_sock *sp = sctp_sk(sk);
6401 struct sctp_association *asoc;
6402 u32 val = 0;
6403
6404 if (sctp_style(sk, TCP))
6405 return -EOPNOTSUPP;
6406
6407 if (len < sizeof(u32))
6408 return -EINVAL;
6409
6410 len = sizeof(u32);
6411
6412 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6413 val++;
6414 }
6415
6416 if (put_user(len, optlen))
6417 return -EFAULT;
6418 if (copy_to_user(optval, &val, len))
6419 return -EFAULT;
6420
6421 return 0;
6422}
6423
Wei Yongjun209ba422011-04-17 17:27:08 +00006424/*
Michio Honda7dc04d72011-04-26 20:16:31 +09006425 * 8.1.23 SCTP_AUTO_ASCONF
6426 * See the corresponding setsockopt entry as description
6427 */
6428static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6429 char __user *optval, int __user *optlen)
6430{
6431 int val = 0;
6432
6433 if (len < sizeof(int))
6434 return -EINVAL;
6435
6436 len = sizeof(int);
6437 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6438 val = 1;
6439 if (put_user(len, optlen))
6440 return -EFAULT;
6441 if (copy_to_user(optval, &val, len))
6442 return -EFAULT;
6443 return 0;
6444}
6445
6446/*
Wei Yongjun209ba422011-04-17 17:27:08 +00006447 * 8.2.6. Get the Current Identifiers of Associations
6448 * (SCTP_GET_ASSOC_ID_LIST)
6449 *
6450 * This option gets the current list of SCTP association identifiers of
6451 * the SCTP associations handled by a one-to-many style socket.
6452 */
6453static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6454 char __user *optval, int __user *optlen)
6455{
6456 struct sctp_sock *sp = sctp_sk(sk);
6457 struct sctp_association *asoc;
6458 struct sctp_assoc_ids *ids;
6459 u32 num = 0;
6460
6461 if (sctp_style(sk, TCP))
6462 return -EOPNOTSUPP;
6463
6464 if (len < sizeof(struct sctp_assoc_ids))
6465 return -EINVAL;
6466
6467 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6468 num++;
6469 }
6470
6471 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6472 return -EINVAL;
6473
6474 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6475
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02006476 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
Wei Yongjun209ba422011-04-17 17:27:08 +00006477 if (unlikely(!ids))
6478 return -ENOMEM;
6479
6480 ids->gaids_number_of_ids = num;
6481 num = 0;
6482 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6483 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6484 }
6485
6486 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6487 kfree(ids);
6488 return -EFAULT;
6489 }
6490
6491 kfree(ids);
6492 return 0;
6493}
6494
Neil Horman5aa93bc2012-07-21 07:56:07 +00006495/*
6496 * SCTP_PEER_ADDR_THLDS
6497 *
6498 * This option allows us to fetch the partially failed threshold for one or all
6499 * transports in an association. See Section 6.1 of:
6500 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6501 */
6502static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6503 char __user *optval,
6504 int len,
6505 int __user *optlen)
6506{
6507 struct sctp_paddrthlds val;
6508 struct sctp_transport *trans;
6509 struct sctp_association *asoc;
6510
6511 if (len < sizeof(struct sctp_paddrthlds))
6512 return -EINVAL;
6513 len = sizeof(struct sctp_paddrthlds);
6514 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6515 return -EFAULT;
6516
6517 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6518 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6519 if (!asoc)
6520 return -ENOENT;
6521
6522 val.spt_pathpfthld = asoc->pf_retrans;
6523 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6524 } else {
6525 trans = sctp_addr_id2transport(sk, &val.spt_address,
6526 val.spt_assoc_id);
6527 if (!trans)
6528 return -ENOENT;
6529
6530 val.spt_pathmaxrxt = trans->pathmaxrxt;
6531 val.spt_pathpfthld = trans->pf_retrans;
6532 }
6533
6534 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6535 return -EFAULT;
6536
6537 return 0;
6538}
6539
Michele Baldessari196d6752012-12-01 04:49:42 +00006540/*
6541 * SCTP_GET_ASSOC_STATS
6542 *
6543 * This option retrieves local per endpoint statistics. It is modeled
6544 * after OpenSolaris' implementation
6545 */
6546static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6547 char __user *optval,
6548 int __user *optlen)
6549{
6550 struct sctp_assoc_stats sas;
6551 struct sctp_association *asoc = NULL;
6552
6553 /* User must provide at least the assoc id */
6554 if (len < sizeof(sctp_assoc_t))
6555 return -EINVAL;
6556
Guenter Roeck726bc6b2013-02-27 10:57:31 +00006557 /* Allow the struct to grow and fill in as much as possible */
6558 len = min_t(size_t, len, sizeof(sas));
6559
Michele Baldessari196d6752012-12-01 04:49:42 +00006560 if (copy_from_user(&sas, optval, len))
6561 return -EFAULT;
6562
6563 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6564 if (!asoc)
6565 return -EINVAL;
6566
6567 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6568 sas.sas_gapcnt = asoc->stats.gapcnt;
6569 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6570 sas.sas_osacks = asoc->stats.osacks;
6571 sas.sas_isacks = asoc->stats.isacks;
6572 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6573 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6574 sas.sas_oodchunks = asoc->stats.oodchunks;
6575 sas.sas_iodchunks = asoc->stats.iodchunks;
6576 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6577 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6578 sas.sas_idupchunks = asoc->stats.idupchunks;
6579 sas.sas_opackets = asoc->stats.opackets;
6580 sas.sas_ipackets = asoc->stats.ipackets;
6581
6582 /* New high max rto observed, will return 0 if not a single
6583 * RTO update took place. obs_rto_ipaddr will be bogus
6584 * in such a case
6585 */
6586 sas.sas_maxrto = asoc->stats.max_obs_rto;
6587 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6588 sizeof(struct sockaddr_storage));
6589
6590 /* Mark beginning of a new observation period */
6591 asoc->stats.max_obs_rto = asoc->rto_min;
6592
Michele Baldessari196d6752012-12-01 04:49:42 +00006593 if (put_user(len, optlen))
6594 return -EFAULT;
6595
Daniel Borkmannbb333812013-06-28 19:49:40 +02006596 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
Michele Baldessari196d6752012-12-01 04:49:42 +00006597
6598 if (copy_to_user(optval, &sas, len))
6599 return -EFAULT;
6600
6601 return 0;
6602}
6603
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02006604static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6605 char __user *optval,
6606 int __user *optlen)
6607{
6608 int val = 0;
6609
6610 if (len < sizeof(int))
6611 return -EINVAL;
6612
6613 len = sizeof(int);
6614 if (sctp_sk(sk)->recvrcvinfo)
6615 val = 1;
6616 if (put_user(len, optlen))
6617 return -EFAULT;
6618 if (copy_to_user(optval, &val, len))
6619 return -EFAULT;
6620
6621 return 0;
6622}
6623
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02006624static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6625 char __user *optval,
6626 int __user *optlen)
6627{
6628 int val = 0;
6629
6630 if (len < sizeof(int))
6631 return -EINVAL;
6632
6633 len = sizeof(int);
6634 if (sctp_sk(sk)->recvnxtinfo)
6635 val = 1;
6636 if (put_user(len, optlen))
6637 return -EFAULT;
6638 if (copy_to_user(optval, &val, len))
6639 return -EFAULT;
6640
6641 return 0;
6642}
6643
Xin Long28aa4c22016-07-09 19:47:40 +08006644static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6645 char __user *optval,
6646 int __user *optlen)
6647{
6648 struct sctp_assoc_value params;
6649 struct sctp_association *asoc;
6650 int retval = -EFAULT;
6651
6652 if (len < sizeof(params)) {
6653 retval = -EINVAL;
6654 goto out;
6655 }
6656
6657 len = sizeof(params);
6658 if (copy_from_user(&params, optval, len))
6659 goto out;
6660
6661 asoc = sctp_id2assoc(sk, params.assoc_id);
6662 if (asoc) {
6663 params.assoc_value = asoc->prsctp_enable;
6664 } else if (!params.assoc_id) {
6665 struct sctp_sock *sp = sctp_sk(sk);
6666
6667 params.assoc_value = sp->ep->prsctp_enable;
6668 } else {
6669 retval = -EINVAL;
6670 goto out;
6671 }
6672
6673 if (put_user(len, optlen))
6674 goto out;
6675
6676 if (copy_to_user(optval, &params, len))
6677 goto out;
6678
6679 retval = 0;
6680
6681out:
6682 return retval;
6683}
6684
Xin Longf959fb42016-07-09 19:47:41 +08006685static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
6686 char __user *optval,
6687 int __user *optlen)
6688{
6689 struct sctp_default_prinfo info;
6690 struct sctp_association *asoc;
6691 int retval = -EFAULT;
6692
6693 if (len < sizeof(info)) {
6694 retval = -EINVAL;
6695 goto out;
6696 }
6697
6698 len = sizeof(info);
6699 if (copy_from_user(&info, optval, len))
6700 goto out;
6701
6702 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
6703 if (asoc) {
6704 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
6705 info.pr_value = asoc->default_timetolive;
6706 } else if (!info.pr_assoc_id) {
6707 struct sctp_sock *sp = sctp_sk(sk);
6708
6709 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
6710 info.pr_value = sp->default_timetolive;
6711 } else {
6712 retval = -EINVAL;
6713 goto out;
6714 }
6715
6716 if (put_user(len, optlen))
6717 goto out;
6718
6719 if (copy_to_user(optval, &info, len))
6720 goto out;
6721
6722 retval = 0;
6723
6724out:
6725 return retval;
6726}
6727
Xin Long826d2532016-07-09 19:47:42 +08006728static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
6729 char __user *optval,
6730 int __user *optlen)
6731{
6732 struct sctp_prstatus params;
6733 struct sctp_association *asoc;
6734 int policy;
6735 int retval = -EINVAL;
6736
6737 if (len < sizeof(params))
6738 goto out;
6739
6740 len = sizeof(params);
6741 if (copy_from_user(&params, optval, len)) {
6742 retval = -EFAULT;
6743 goto out;
6744 }
6745
6746 policy = params.sprstat_policy;
6747 if (policy & ~SCTP_PR_SCTP_MASK)
6748 goto out;
6749
6750 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
6751 if (!asoc)
6752 goto out;
6753
6754 if (policy == SCTP_PR_SCTP_NONE) {
6755 params.sprstat_abandoned_unsent = 0;
6756 params.sprstat_abandoned_sent = 0;
6757 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6758 params.sprstat_abandoned_unsent +=
6759 asoc->abandoned_unsent[policy];
6760 params.sprstat_abandoned_sent +=
6761 asoc->abandoned_sent[policy];
6762 }
6763 } else {
6764 params.sprstat_abandoned_unsent =
6765 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
6766 params.sprstat_abandoned_sent =
6767 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
6768 }
6769
6770 if (put_user(len, optlen)) {
6771 retval = -EFAULT;
6772 goto out;
6773 }
6774
6775 if (copy_to_user(optval, &params, len)) {
6776 retval = -EFAULT;
6777 goto out;
6778 }
6779
6780 retval = 0;
6781
6782out:
6783 return retval;
6784}
6785
Xin Longd229d482017-04-01 17:07:46 +08006786static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
6787 char __user *optval,
6788 int __user *optlen)
6789{
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006790 struct sctp_stream_out_ext *streamoute;
Xin Longd229d482017-04-01 17:07:46 +08006791 struct sctp_association *asoc;
6792 struct sctp_prstatus params;
6793 int retval = -EINVAL;
6794 int policy;
6795
6796 if (len < sizeof(params))
6797 goto out;
6798
6799 len = sizeof(params);
6800 if (copy_from_user(&params, optval, len)) {
6801 retval = -EFAULT;
6802 goto out;
6803 }
6804
6805 policy = params.sprstat_policy;
6806 if (policy & ~SCTP_PR_SCTP_MASK)
6807 goto out;
6808
6809 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
Xin Longcee360a2017-05-31 16:36:31 +08006810 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
Xin Longd229d482017-04-01 17:07:46 +08006811 goto out;
6812
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006813 streamoute = asoc->stream.out[params.sprstat_sid].ext;
6814 if (!streamoute) {
6815 /* Not allocated yet, means all stats are 0 */
6816 params.sprstat_abandoned_unsent = 0;
6817 params.sprstat_abandoned_sent = 0;
6818 retval = 0;
6819 goto out;
6820 }
6821
Xin Longd229d482017-04-01 17:07:46 +08006822 if (policy == SCTP_PR_SCTP_NONE) {
6823 params.sprstat_abandoned_unsent = 0;
6824 params.sprstat_abandoned_sent = 0;
6825 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6826 params.sprstat_abandoned_unsent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006827 streamoute->abandoned_unsent[policy];
Xin Longd229d482017-04-01 17:07:46 +08006828 params.sprstat_abandoned_sent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006829 streamoute->abandoned_sent[policy];
Xin Longd229d482017-04-01 17:07:46 +08006830 }
6831 } else {
6832 params.sprstat_abandoned_unsent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006833 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08006834 params.sprstat_abandoned_sent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006835 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08006836 }
6837
6838 if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
6839 retval = -EFAULT;
6840 goto out;
6841 }
6842
6843 retval = 0;
6844
6845out:
6846 return retval;
6847}
6848
Xin Longc0d8bab2017-03-10 12:11:12 +08006849static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
6850 char __user *optval,
6851 int __user *optlen)
6852{
6853 struct sctp_assoc_value params;
6854 struct sctp_association *asoc;
6855 int retval = -EFAULT;
6856
6857 if (len < sizeof(params)) {
6858 retval = -EINVAL;
6859 goto out;
6860 }
6861
6862 len = sizeof(params);
6863 if (copy_from_user(&params, optval, len))
6864 goto out;
6865
6866 asoc = sctp_id2assoc(sk, params.assoc_id);
6867 if (asoc) {
6868 params.assoc_value = asoc->reconf_enable;
6869 } else if (!params.assoc_id) {
6870 struct sctp_sock *sp = sctp_sk(sk);
6871
6872 params.assoc_value = sp->ep->reconf_enable;
6873 } else {
6874 retval = -EINVAL;
6875 goto out;
6876 }
6877
6878 if (put_user(len, optlen))
6879 goto out;
6880
6881 if (copy_to_user(optval, &params, len))
6882 goto out;
6883
6884 retval = 0;
6885
6886out:
6887 return retval;
6888}
6889
Xin Long9fb657a2017-01-18 00:44:46 +08006890static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
6891 char __user *optval,
6892 int __user *optlen)
6893{
6894 struct sctp_assoc_value params;
6895 struct sctp_association *asoc;
6896 int retval = -EFAULT;
6897
6898 if (len < sizeof(params)) {
6899 retval = -EINVAL;
6900 goto out;
6901 }
6902
6903 len = sizeof(params);
6904 if (copy_from_user(&params, optval, len))
6905 goto out;
6906
6907 asoc = sctp_id2assoc(sk, params.assoc_id);
6908 if (asoc) {
6909 params.assoc_value = asoc->strreset_enable;
6910 } else if (!params.assoc_id) {
6911 struct sctp_sock *sp = sctp_sk(sk);
6912
6913 params.assoc_value = sp->ep->strreset_enable;
6914 } else {
6915 retval = -EINVAL;
6916 goto out;
6917 }
6918
6919 if (put_user(len, optlen))
6920 goto out;
6921
6922 if (copy_to_user(optval, &params, len))
6923 goto out;
6924
6925 retval = 0;
6926
6927out:
6928 return retval;
6929}
6930
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03006931static int sctp_getsockopt_scheduler(struct sock *sk, int len,
6932 char __user *optval,
6933 int __user *optlen)
6934{
6935 struct sctp_assoc_value params;
6936 struct sctp_association *asoc;
6937 int retval = -EFAULT;
6938
6939 if (len < sizeof(params)) {
6940 retval = -EINVAL;
6941 goto out;
6942 }
6943
6944 len = sizeof(params);
6945 if (copy_from_user(&params, optval, len))
6946 goto out;
6947
6948 asoc = sctp_id2assoc(sk, params.assoc_id);
6949 if (!asoc) {
6950 retval = -EINVAL;
6951 goto out;
6952 }
6953
6954 params.assoc_value = sctp_sched_get_sched(asoc);
6955
6956 if (put_user(len, optlen))
6957 goto out;
6958
6959 if (copy_to_user(optval, &params, len))
6960 goto out;
6961
6962 retval = 0;
6963
6964out:
6965 return retval;
6966}
6967
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03006968static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
6969 char __user *optval,
6970 int __user *optlen)
6971{
6972 struct sctp_stream_value params;
6973 struct sctp_association *asoc;
6974 int retval = -EFAULT;
6975
6976 if (len < sizeof(params)) {
6977 retval = -EINVAL;
6978 goto out;
6979 }
6980
6981 len = sizeof(params);
6982 if (copy_from_user(&params, optval, len))
6983 goto out;
6984
6985 asoc = sctp_id2assoc(sk, params.assoc_id);
6986 if (!asoc) {
6987 retval = -EINVAL;
6988 goto out;
6989 }
6990
6991 retval = sctp_sched_get_value(asoc, params.stream_id,
6992 &params.stream_value);
6993 if (retval)
6994 goto out;
6995
6996 if (put_user(len, optlen)) {
6997 retval = -EFAULT;
6998 goto out;
6999 }
7000
7001 if (copy_to_user(optval, &params, len)) {
7002 retval = -EFAULT;
7003 goto out;
7004 }
7005
7006out:
7007 return retval;
7008}
7009
Xin Long772a5862017-12-08 21:03:58 +08007010static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7011 char __user *optval,
7012 int __user *optlen)
7013{
7014 struct sctp_assoc_value params;
7015 struct sctp_association *asoc;
7016 int retval = -EFAULT;
7017
7018 if (len < sizeof(params)) {
7019 retval = -EINVAL;
7020 goto out;
7021 }
7022
7023 len = sizeof(params);
7024 if (copy_from_user(&params, optval, len))
7025 goto out;
7026
7027 asoc = sctp_id2assoc(sk, params.assoc_id);
7028 if (asoc) {
7029 params.assoc_value = asoc->intl_enable;
7030 } else if (!params.assoc_id) {
7031 struct sctp_sock *sp = sctp_sk(sk);
7032
7033 params.assoc_value = sp->strm_interleave;
7034 } else {
7035 retval = -EINVAL;
7036 goto out;
7037 }
7038
7039 if (put_user(len, optlen))
7040 goto out;
7041
7042 if (copy_to_user(optval, &params, len))
7043 goto out;
7044
7045 retval = 0;
7046
7047out:
7048 return retval;
7049}
7050
Daniel Borkmanndda91922013-06-17 11:40:05 +02007051static int sctp_getsockopt(struct sock *sk, int level, int optname,
7052 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053{
7054 int retval = 0;
7055 int len;
7056
Daniel Borkmannbb333812013-06-28 19:49:40 +02007057 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058
7059 /* I can hardly begin to describe how wrong this is. This is
7060 * so broken as to be worse than useless. The API draft
7061 * REALLY is NOT helpful here... I am not convinced that the
7062 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7063 * are at all well-founded.
7064 */
7065 if (level != SOL_SCTP) {
7066 struct sctp_af *af = sctp_sk(sk)->pf->af;
7067
7068 retval = af->getsockopt(sk, level, optname, optval, optlen);
7069 return retval;
7070 }
7071
7072 if (get_user(len, optlen))
7073 return -EFAULT;
7074
Jiri Slabya4b8e712016-10-21 14:13:24 +02007075 if (len < 0)
7076 return -EINVAL;
7077
wangweidong048ed4b2014-01-21 15:44:11 +08007078 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079
7080 switch (optname) {
7081 case SCTP_STATUS:
7082 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7083 break;
7084 case SCTP_DISABLE_FRAGMENTS:
7085 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7086 optlen);
7087 break;
7088 case SCTP_EVENTS:
7089 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7090 break;
7091 case SCTP_AUTOCLOSE:
7092 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7093 break;
7094 case SCTP_SOCKOPT_PEELOFF:
7095 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7096 break;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04007097 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7098 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7099 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100 case SCTP_PEER_ADDR_PARAMS:
7101 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7102 optlen);
7103 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00007104 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07007105 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08007106 optlen);
7107 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108 case SCTP_INITMSG:
7109 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111 case SCTP_GET_PEER_ADDRS:
7112 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7113 optlen);
7114 break;
7115 case SCTP_GET_LOCAL_ADDRS:
7116 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7117 optlen);
7118 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04007119 case SCTP_SOCKOPT_CONNECTX3:
7120 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7121 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122 case SCTP_DEFAULT_SEND_PARAM:
7123 retval = sctp_getsockopt_default_send_param(sk, len,
7124 optval, optlen);
7125 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02007126 case SCTP_DEFAULT_SNDINFO:
7127 retval = sctp_getsockopt_default_sndinfo(sk, len,
7128 optval, optlen);
7129 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130 case SCTP_PRIMARY_ADDR:
7131 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7132 break;
7133 case SCTP_NODELAY:
7134 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7135 break;
7136 case SCTP_RTOINFO:
7137 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7138 break;
7139 case SCTP_ASSOCINFO:
7140 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7141 break;
7142 case SCTP_I_WANT_MAPPED_V4_ADDR:
7143 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7144 break;
7145 case SCTP_MAXSEG:
7146 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7147 break;
7148 case SCTP_GET_PEER_ADDR_INFO:
7149 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7150 optlen);
7151 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08007152 case SCTP_ADAPTATION_LAYER:
7153 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154 optlen);
7155 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08007156 case SCTP_CONTEXT:
7157 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7158 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007159 case SCTP_FRAGMENT_INTERLEAVE:
7160 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7161 optlen);
7162 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07007163 case SCTP_PARTIAL_DELIVERY_POINT:
7164 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7165 optlen);
7166 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07007167 case SCTP_MAX_BURST:
7168 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7169 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007170 case SCTP_AUTH_KEY:
7171 case SCTP_AUTH_CHUNK:
7172 case SCTP_AUTH_DELETE_KEY:
7173 retval = -EOPNOTSUPP;
7174 break;
7175 case SCTP_HMAC_IDENT:
7176 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7177 break;
7178 case SCTP_AUTH_ACTIVE_KEY:
7179 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7180 break;
7181 case SCTP_PEER_AUTH_CHUNKS:
7182 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7183 optlen);
7184 break;
7185 case SCTP_LOCAL_AUTH_CHUNKS:
7186 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7187 optlen);
7188 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08007189 case SCTP_GET_ASSOC_NUMBER:
7190 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7191 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00007192 case SCTP_GET_ASSOC_ID_LIST:
7193 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7194 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09007195 case SCTP_AUTO_ASCONF:
7196 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7197 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00007198 case SCTP_PEER_ADDR_THLDS:
7199 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7200 break;
Michele Baldessari196d6752012-12-01 04:49:42 +00007201 case SCTP_GET_ASSOC_STATS:
7202 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7203 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02007204 case SCTP_RECVRCVINFO:
7205 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7206 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007207 case SCTP_RECVNXTINFO:
7208 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7209 break;
Xin Long28aa4c22016-07-09 19:47:40 +08007210 case SCTP_PR_SUPPORTED:
7211 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7212 break;
Xin Longf959fb42016-07-09 19:47:41 +08007213 case SCTP_DEFAULT_PRINFO:
7214 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7215 optlen);
7216 break;
Xin Long826d2532016-07-09 19:47:42 +08007217 case SCTP_PR_ASSOC_STATUS:
7218 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7219 optlen);
7220 break;
Xin Longd229d482017-04-01 17:07:46 +08007221 case SCTP_PR_STREAM_STATUS:
7222 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7223 optlen);
7224 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08007225 case SCTP_RECONFIG_SUPPORTED:
7226 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7227 optlen);
7228 break;
Xin Long9fb657a2017-01-18 00:44:46 +08007229 case SCTP_ENABLE_STREAM_RESET:
7230 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7231 optlen);
7232 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007233 case SCTP_STREAM_SCHEDULER:
7234 retval = sctp_getsockopt_scheduler(sk, len, optval,
7235 optlen);
7236 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007237 case SCTP_STREAM_SCHEDULER_VALUE:
7238 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7239 optlen);
7240 break;
Xin Long772a5862017-12-08 21:03:58 +08007241 case SCTP_INTERLEAVING_SUPPORTED:
7242 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7243 optlen);
7244 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245 default:
7246 retval = -ENOPROTOOPT;
7247 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249
wangweidong048ed4b2014-01-21 15:44:11 +08007250 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251 return retval;
7252}
7253
Craig Gallek086c6532016-02-10 11:50:35 -05007254static int sctp_hash(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255{
7256 /* STUB */
Craig Gallek086c6532016-02-10 11:50:35 -05007257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258}
7259
7260static void sctp_unhash(struct sock *sk)
7261{
7262 /* STUB */
7263}
7264
7265/* Check if port is acceptable. Possibly find first available port.
7266 *
7267 * The port hash table (contained in the 'global' SCTP protocol storage
7268 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7269 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7270 * list (the list number is the port number hashed out, so as you
7271 * would expect from a hash function, all the ports in a given list have
7272 * such a number that hashes out to the same list number; you were
7273 * expecting that, right?); so each list has a set of ports, with a
7274 * link to the socket (struct sock) that uses it, the port number and
7275 * a fastreuse flag (FIXME: NPI ipg).
7276 */
7277static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007278 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279
7280static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
7281{
7282 struct sctp_bind_hashbucket *head; /* hash list */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007283 struct sctp_bind_bucket *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284 unsigned short snum;
7285 int ret;
7286
Al Viro04afd8b2006-11-20 17:02:01 -08007287 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288
Daniel Borkmannbb333812013-06-28 19:49:40 +02007289 pr_debug("%s: begins, snum:%d\n", __func__, snum);
7290
wangweidong79b91132014-01-21 15:44:07 +08007291 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292
7293 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07007294 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007295 int low, high, remaining, index;
7296 unsigned int rover;
WANG Cong122ff242014-05-12 16:04:53 -07007297 struct net *net = sock_net(sk);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007298
WANG Cong122ff242014-05-12 16:04:53 -07007299 inet_get_local_port_range(net, &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007300 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05007301 rover = prandom_u32() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303 do {
7304 rover++;
7305 if ((rover < low) || (rover > high))
7306 rover = low;
WANG Cong122ff242014-05-12 16:04:53 -07007307 if (inet_is_local_reserved_port(net, rover))
Amerigo Wange3826f12010-05-05 00:27:06 +00007308 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007309 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 head = &sctp_port_hashtable[index];
wangweidong3c8e43b2014-01-21 15:44:08 +08007311 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007312 sctp_for_each_hentry(pp, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007313 if ((pp->port == rover) &&
7314 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315 goto next;
7316 break;
7317 next:
wangweidong3c8e43b2014-01-21 15:44:08 +08007318 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320
7321 /* Exhausted local port range during search? */
7322 ret = 1;
7323 if (remaining <= 0)
7324 goto fail;
7325
7326 /* OK, here is the one we will use. HEAD (the port
7327 * hash table list entry) is non-NULL and we hold it's
7328 * mutex.
7329 */
7330 snum = rover;
7331 } else {
7332 /* We are given an specific port number; we verify
7333 * that it is not being used. If it is used, we will
7334 * exahust the search in the hash list corresponding
7335 * to the port number (snum) - we detect that with the
7336 * port iterator, pp being NULL.
7337 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007338 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
wangweidong3c8e43b2014-01-21 15:44:08 +08007339 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007340 sctp_for_each_hentry(pp, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007341 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 goto pp_found;
7343 }
7344 }
7345 pp = NULL;
7346 goto pp_not_found;
7347pp_found:
7348 if (!hlist_empty(&pp->owner)) {
7349 /* We had a port hash table hit - there is an
7350 * available port (pp != NULL) and it is being
7351 * used by other socket (pp->owner not empty); that other
7352 * socket is going to be sk2.
7353 */
7354 int reuse = sk->sk_reuse;
7355 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356
Daniel Borkmannbb333812013-06-28 19:49:40 +02007357 pr_debug("%s: found a possible match\n", __func__);
7358
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007359 if (pp->fastreuse && sk->sk_reuse &&
7360 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361 goto success;
7362
7363 /* Run through the list of sockets bound to the port
7364 * (pp->port) [via the pointers bind_next and
7365 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
7366 * we get the endpoint they describe and run through
7367 * the endpoint's list of IP (v4 or v6) addresses,
7368 * comparing each of the addresses with the address of
7369 * the socket sk. If we find a match, then that means
7370 * that this port/socket (sk) combination are already
7371 * in an endpoint.
7372 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007373 sk_for_each_bound(sk2, &pp->owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 struct sctp_endpoint *ep2;
7375 ep2 = sctp_sk(sk2)->ep;
7376
Vlad Yasevich4e540642008-07-18 23:06:32 -07007377 if (sk == sk2 ||
7378 (reuse && sk2->sk_reuse &&
7379 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 continue;
7381
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07007382 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
7383 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384 ret = (long)sk2;
7385 goto fail_unlock;
7386 }
7387 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02007388
7389 pr_debug("%s: found a match\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390 }
7391pp_not_found:
7392 /* If there was a hash table miss, create a new port. */
7393 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007394 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395 goto fail_unlock;
7396
7397 /* In either case (hit or miss), make sure fastreuse is 1 only
7398 * if sk->sk_reuse is too (that is, if the caller requested
7399 * SO_REUSEADDR on this socket -sk-).
7400 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007401 if (hlist_empty(&pp->owner)) {
7402 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
7403 pp->fastreuse = 1;
7404 else
7405 pp->fastreuse = 0;
7406 } else if (pp->fastreuse &&
7407 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 pp->fastreuse = 0;
7409
7410 /* We are set, so fill up all the data in the hash table
7411 * entry, tie the socket list information with the rest of the
7412 * sockets FIXME: Blurry, NPI (ipg).
7413 */
7414success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007416 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417 sk_add_bind_node(sk, &pp->owner);
7418 sctp_sk(sk)->bind_hash = pp;
7419 }
7420 ret = 0;
7421
7422fail_unlock:
wangweidong3c8e43b2014-01-21 15:44:08 +08007423 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424
7425fail:
wangweidong79b91132014-01-21 15:44:07 +08007426 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 return ret;
7428}
7429
7430/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
7431 * port is requested.
7432 */
7433static int sctp_get_port(struct sock *sk, unsigned short snum)
7434{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435 union sctp_addr addr;
7436 struct sctp_af *af = sctp_sk(sk)->pf->af;
7437
7438 /* Set up a dummy address struct from the sk. */
7439 af->from_sk(&addr, sk);
7440 addr.v4.sin_port = htons(snum);
7441
7442 /* Note: sk->sk_num gets filled in if ephemeral port request. */
Daniel Borkmann62208f12013-06-25 18:17:30 +02007443 return !!sctp_get_port_local(sk, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444}
7445
7446/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447 * Move a socket to LISTENING state.
7448 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02007449static int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007451 struct sctp_sock *sp = sctp_sk(sk);
7452 struct sctp_endpoint *ep = sp->ep;
Herbert Xu5821c762016-01-24 21:20:12 +08007453 struct crypto_shash *tfm = NULL;
Neil Horman3c681982012-10-24 09:20:03 +00007454 char alg[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455
7456 /* Allocate HMAC for generating cookie. */
Neil Horman3c681982012-10-24 09:20:03 +00007457 if (!sp->hmac && sp->sctp_hmac_alg) {
7458 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
Herbert Xu5821c762016-01-24 21:20:12 +08007459 tfm = crypto_alloc_shash(alg, 0, 0);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07007460 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00007461 net_info_ratelimited("failed to load transform for %s: %ld\n",
Neil Horman3c681982012-10-24 09:20:03 +00007462 sp->sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007463 return -ENOSYS;
7464 }
7465 sctp_sk(sk)->hmac = tfm;
7466 }
7467
7468 /*
7469 * If a bind() or sctp_bindx() is not called prior to a listen()
7470 * call that allows new associations to be accepted, the system
7471 * picks an ephemeral port and will choose an address set equivalent
7472 * to binding with a wildcard address.
7473 *
7474 * This is not currently spelled out in the SCTP sockets
7475 * extensions draft, but follows the practice as seen in TCP
7476 * sockets.
7477 *
7478 */
Yafang Shaocbabf462017-12-20 11:12:54 +08007479 inet_sk_set_state(sk, SCTP_SS_LISTENING);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007480 if (!ep->base.bind_addr.port) {
7481 if (sctp_autobind(sk))
7482 return -EAGAIN;
7483 } else {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007484 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08007485 inet_sk_set_state(sk, SCTP_SS_CLOSED);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007486 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487 }
7488 }
7489
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007490 sk->sk_max_ack_backlog = backlog;
7491 sctp_hash_endpoint(ep);
7492 return 0;
7493}
7494
7495/*
7496 * 4.1.3 / 5.1.3 listen()
7497 *
7498 * By default, new associations are not accepted for UDP style sockets.
7499 * An application uses listen() to mark a socket as being able to
7500 * accept new associations.
7501 *
7502 * On TCP style sockets, applications use listen() to ready the SCTP
7503 * endpoint for accepting inbound associations.
7504 *
7505 * On both types of endpoints a backlog of '0' disables listening.
7506 *
7507 * Move a socket to LISTENING state.
7508 */
7509int sctp_inet_listen(struct socket *sock, int backlog)
7510{
7511 struct sock *sk = sock->sk;
7512 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7513 int err = -EINVAL;
7514
7515 if (unlikely(backlog < 0))
7516 return err;
7517
wangweidong048ed4b2014-01-21 15:44:11 +08007518 lock_sock(sk);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007519
7520 /* Peeled-off sockets are not allowed to listen(). */
7521 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
7522 goto out;
7523
7524 if (sock->state != SS_UNCONNECTED)
7525 goto out;
7526
Xin Long34b27892017-04-06 13:10:52 +08007527 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
7528 goto out;
7529
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007530 /* If backlog is zero, disable listening. */
7531 if (!backlog) {
7532 if (sctp_sstate(sk, CLOSED))
7533 goto out;
7534
7535 err = 0;
7536 sctp_unhash_endpoint(ep);
7537 sk->sk_state = SCTP_SS_CLOSED;
7538 if (sk->sk_reuse)
7539 sctp_sk(sk)->bind_hash->fastreuse = 1;
7540 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007541 }
7542
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007543 /* If we are already listening, just update the backlog */
7544 if (sctp_sstate(sk, LISTENING))
7545 sk->sk_max_ack_backlog = backlog;
7546 else {
7547 err = sctp_listen_start(sk, backlog);
7548 if (err)
7549 goto out;
7550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007552 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553out:
wangweidong048ed4b2014-01-21 15:44:11 +08007554 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556}
7557
7558/*
7559 * This function is done by modeling the current datagram_poll() and the
7560 * tcp_poll(). Note that, based on these implementations, we don't
7561 * lock the socket in this function, even though it seems that,
7562 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08007563 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564 * in this place. We assume that we don't need locks either until proven
7565 * otherwise.
7566 *
7567 * Another thing to note is that we include the Async I/O support
7568 * here, again, by modeling the current TCP/UDP code. We don't have
7569 * a good way to test with it yet.
7570 */
Al Viroade994f2017-07-03 00:01:49 -04007571__poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572{
7573 struct sock *sk = sock->sk;
7574 struct sctp_sock *sp = sctp_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -04007575 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576
Eric Dumazetaa395142010-04-20 13:03:51 +00007577 poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03007579 sock_rps_record_flow(sk);
7580
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 /* A TCP-style listening socket becomes readable when the accept queue
7582 * is not empty.
7583 */
7584 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
7585 return (!list_empty(&sp->ep->asocs)) ?
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007586 (EPOLLIN | EPOLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587
7588 mask = 0;
7589
7590 /* Is there any exceptional events? */
7591 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007592 mask |= EPOLLERR |
7593 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
Davide Libenzif348d702006-03-25 03:07:39 -08007594 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007595 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007597 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007598
7599 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00007600 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007601 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602
7603 /* The association is either gone or not ready. */
7604 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
7605 return mask;
7606
7607 /* Is it writable? */
7608 if (sctp_writeable(sk)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007609 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 } else {
Eric Dumazet9cd3e072015-11-29 20:03:10 -08007611 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612 /*
7613 * Since the socket is not locked, the buffer
7614 * might be made available after the writeable check and
7615 * before the bit is set. This could cause a lost I/O
7616 * signal. tcp_poll() has a race breaker for this race
7617 * condition. Based on their implementation, we put
7618 * in the following code to cover it as well.
7619 */
7620 if (sctp_writeable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007621 mask |= EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622 }
7623 return mask;
7624}
7625
7626/********************************************************************
7627 * 2nd Level Abstractions
7628 ********************************************************************/
7629
7630static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007631 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632{
7633 struct sctp_bind_bucket *pp;
7634
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08007635 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07007637 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638 pp->port = snum;
7639 pp->fastreuse = 0;
7640 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007641 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007642 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643 }
7644 return pp;
7645}
7646
7647/* Caller must hold hashbucket lock for this tb with local BH disabled */
7648static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
7649{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07007650 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007651 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652 kmem_cache_free(sctp_bucket_cachep, pp);
7653 SCTP_DBG_OBJCNT_DEC(bind_bucket);
7654 }
7655}
7656
7657/* Release this socket's reference to a local port. */
7658static inline void __sctp_put_port(struct sock *sk)
7659{
7660 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007661 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
7662 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663 struct sctp_bind_bucket *pp;
7664
wangweidong3c8e43b2014-01-21 15:44:08 +08007665 spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666 pp = sctp_sk(sk)->bind_hash;
7667 __sk_del_bind_node(sk);
7668 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007669 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670 sctp_bucket_destroy(pp);
wangweidong3c8e43b2014-01-21 15:44:08 +08007671 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672}
7673
7674void sctp_put_port(struct sock *sk)
7675{
wangweidong79b91132014-01-21 15:44:07 +08007676 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677 __sctp_put_port(sk);
wangweidong79b91132014-01-21 15:44:07 +08007678 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679}
7680
7681/*
7682 * The system picks an ephemeral port and choose an address set equivalent
7683 * to binding with a wildcard address.
7684 * One of those addresses will be the primary address for the association.
7685 * This automatically enables the multihoming capability of SCTP.
7686 */
7687static int sctp_autobind(struct sock *sk)
7688{
7689 union sctp_addr autoaddr;
7690 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08007691 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692
7693 /* Initialize a local sockaddr structure to INADDR_ANY. */
7694 af = sctp_sk(sk)->pf->af;
7695
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007696 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697 af->inaddr_any(&autoaddr, port);
7698
7699 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
7700}
7701
7702/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
7703 *
7704 * From RFC 2292
7705 * 4.2 The cmsghdr Structure *
7706 *
7707 * When ancillary data is sent or received, any number of ancillary data
7708 * objects can be specified by the msg_control and msg_controllen members of
7709 * the msghdr structure, because each object is preceded by
7710 * a cmsghdr structure defining the object's length (the cmsg_len member).
7711 * Historically Berkeley-derived implementations have passed only one object
7712 * at a time, but this API allows multiple objects to be
7713 * passed in a single call to sendmsg() or recvmsg(). The following example
7714 * shows two ancillary data objects in a control buffer.
7715 *
7716 * |<--------------------------- msg_controllen -------------------------->|
7717 * | |
7718 *
7719 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
7720 *
7721 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
7722 * | | |
7723 *
7724 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
7725 *
7726 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
7727 * | | | | |
7728 *
7729 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7730 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
7731 *
7732 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
7733 *
7734 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7735 * ^
7736 * |
7737 *
7738 * msg_control
7739 * points here
7740 */
Xin Longa05437a2017-08-11 10:23:48 +08007741static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742{
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007743 struct msghdr *my_msg = (struct msghdr *)msg;
Xin Longa05437a2017-08-11 10:23:48 +08007744 struct cmsghdr *cmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745
Gu Zhengf95b4142014-12-11 11:22:04 +08007746 for_each_cmsghdr(cmsg, my_msg) {
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007747 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007748 return -EINVAL;
7749
7750 /* Should we parse this header or ignore? */
7751 if (cmsg->cmsg_level != IPPROTO_SCTP)
7752 continue;
7753
7754 /* Strictly check lengths following example in SCM code. */
7755 switch (cmsg->cmsg_type) {
7756 case SCTP_INIT:
7757 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007758 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007759 *
7760 * This cmsghdr structure provides information for
7761 * initializing new SCTP associations with sendmsg().
7762 * The SCTP_INITMSG socket option uses this same data
7763 * structure. This structure is not used for
7764 * recvmsg().
7765 *
7766 * cmsg_level cmsg_type cmsg_data[]
7767 * ------------ ------------ ----------------------
7768 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
7769 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007770 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771 return -EINVAL;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007772
7773 cmsgs->init = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774 break;
7775
7776 case SCTP_SNDRCV:
7777 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007778 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779 *
7780 * This cmsghdr structure specifies SCTP options for
7781 * sendmsg() and describes SCTP header information
7782 * about a received message through recvmsg().
7783 *
7784 * cmsg_level cmsg_type cmsg_data[]
7785 * ------------ ------------ ----------------------
7786 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
7787 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007788 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007789 return -EINVAL;
7790
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007791 cmsgs->srinfo = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007793 if (cmsgs->srinfo->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007794 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Longa6c2f792016-07-09 19:47:43 +08007795 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007796 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797 return -EINVAL;
7798 break;
7799
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007800 case SCTP_SNDINFO:
7801 /* SCTP Socket API Extension
7802 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
7803 *
7804 * This cmsghdr structure specifies SCTP options for
7805 * sendmsg(). This structure and SCTP_RCVINFO replaces
7806 * SCTP_SNDRCV which has been deprecated.
7807 *
7808 * cmsg_level cmsg_type cmsg_data[]
7809 * ------------ ------------ ---------------------
7810 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
7811 */
7812 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
7813 return -EINVAL;
7814
7815 cmsgs->sinfo = CMSG_DATA(cmsg);
7816
7817 if (cmsgs->sinfo->snd_flags &
7818 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Longa6c2f792016-07-09 19:47:43 +08007819 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007820 SCTP_ABORT | SCTP_EOF))
7821 return -EINVAL;
7822 break;
Xin Longed63afb2018-03-05 20:44:18 +08007823 case SCTP_PRINFO:
7824 /* SCTP Socket API Extension
7825 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
7826 *
7827 * This cmsghdr structure specifies SCTP options for sendmsg().
7828 *
7829 * cmsg_level cmsg_type cmsg_data[]
7830 * ------------ ------------ ---------------------
7831 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
7832 */
7833 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
7834 return -EINVAL;
7835
7836 cmsgs->prinfo = CMSG_DATA(cmsg);
7837 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
7838 return -EINVAL;
7839
7840 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
7841 cmsgs->prinfo->pr_value = 0;
7842 break;
Xin Long2c0dbaa2018-03-05 20:44:19 +08007843 case SCTP_DSTADDRV4:
7844 case SCTP_DSTADDRV6:
7845 /* SCTP Socket API Extension
7846 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
7847 *
7848 * This cmsghdr structure specifies SCTP options for sendmsg().
7849 *
7850 * cmsg_level cmsg_type cmsg_data[]
7851 * ------------ ------------ ---------------------
7852 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
7853 * ------------ ------------ ---------------------
7854 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
7855 */
7856 cmsgs->addrs_msg = my_msg;
7857 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007858 default:
7859 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861 }
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007862
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 return 0;
7864}
7865
7866/*
7867 * Wait for a packet..
7868 * Note: This function is the same function as in core/datagram.c
7869 * with a few modifications to make lksctp work.
7870 */
wangweidong26ac8e52013-12-23 12:16:51 +08007871static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872{
7873 int error;
7874 DEFINE_WAIT(wait);
7875
Eric Dumazetaa395142010-04-20 13:03:51 +00007876 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877
7878 /* Socket errors? */
7879 error = sock_error(sk);
7880 if (error)
7881 goto out;
7882
7883 if (!skb_queue_empty(&sk->sk_receive_queue))
7884 goto ready;
7885
7886 /* Socket shut down? */
7887 if (sk->sk_shutdown & RCV_SHUTDOWN)
7888 goto out;
7889
7890 /* Sequenced packets can come disconnected. If so we report the
7891 * problem.
7892 */
7893 error = -ENOTCONN;
7894
7895 /* Is there a good reason to think that we may receive some data? */
7896 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
7897 goto out;
7898
7899 /* Handle signals. */
7900 if (signal_pending(current))
7901 goto interrupted;
7902
7903 /* Let another process have a go. Since we are going to sleep
7904 * anyway. Note: This may cause odd behaviors if the message
7905 * does not fit in the user's buffer, but this seems to be the
7906 * only way to honor MSG_DONTWAIT realistically.
7907 */
wangweidong048ed4b2014-01-21 15:44:11 +08007908 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 *timeo_p = schedule_timeout(*timeo_p);
wangweidong048ed4b2014-01-21 15:44:11 +08007910 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911
7912ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00007913 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914 return 0;
7915
7916interrupted:
7917 error = sock_intr_errno(*timeo_p);
7918
7919out:
Eric Dumazetaa395142010-04-20 13:03:51 +00007920 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921 *err = error;
7922 return error;
7923}
7924
7925/* Receive a datagram.
7926 * Note: This is pretty much the same routine as in core/datagram.c
7927 * with a few changes to make lksctp work.
7928 */
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007929struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
7930 int noblock, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931{
7932 int error;
7933 struct sk_buff *skb;
7934 long timeo;
7935
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936 timeo = sock_rcvtimeo(sk, noblock);
7937
Daniel Borkmannbb333812013-06-28 19:49:40 +02007938 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
7939 MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940
7941 do {
7942 /* Again only user level code calls this function,
7943 * so nothing interrupt level
7944 * will suddenly eat the receive_queue.
7945 *
7946 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00007947 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948 */
7949 if (flags & MSG_PEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950 skb = skb_peek(&sk->sk_receive_queue);
7951 if (skb)
Reshetova, Elena63354792017-06-30 13:07:58 +03007952 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953 } else {
Marcelo Ricardo Leitner311b2172016-04-13 19:12:29 -03007954 skb = __skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007955 }
7956
7957 if (skb)
7958 return skb;
7959
Neil Horman6736dc32005-12-02 20:30:06 -08007960 /* Caller is allowed not to check sk->sk_err before calling. */
7961 error = sock_error(sk);
7962 if (error)
7963 goto no_packet;
7964
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965 if (sk->sk_shutdown & RCV_SHUTDOWN)
7966 break;
7967
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07007968 if (sk_can_busy_loop(sk)) {
7969 sk_busy_loop(sk, noblock);
7970
7971 if (!skb_queue_empty(&sk->sk_receive_queue))
7972 continue;
7973 }
Neil Horman8465a5f2014-04-17 15:26:51 -04007974
Linus Torvalds1da177e2005-04-16 15:20:36 -07007975 /* User doesn't want to wait. */
7976 error = -EAGAIN;
7977 if (!timeo)
7978 goto no_packet;
7979 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
7980
7981 return NULL;
7982
7983no_packet:
7984 *err = error;
7985 return NULL;
7986}
7987
7988/* If sndbuf has changed, wake up per association sndbuf waiters. */
7989static void __sctp_write_space(struct sctp_association *asoc)
7990{
7991 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007992
Eric Dumazetceb5d582015-11-29 20:03:11 -08007993 if (sctp_wspace(asoc) <= 0)
7994 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995
Eric Dumazetceb5d582015-11-29 20:03:11 -08007996 if (waitqueue_active(&asoc->wait))
7997 wake_up_interruptible(&asoc->wait);
Eric Dumazeteaefd112011-02-18 03:26:36 +00007998
Eric Dumazetceb5d582015-11-29 20:03:11 -08007999 if (sctp_writeable(sk)) {
8000 struct socket_wq *wq;
8001
8002 rcu_read_lock();
8003 wq = rcu_dereference(sk->sk_wq);
8004 if (wq) {
8005 if (waitqueue_active(&wq->wait))
8006 wake_up_interruptible(&wq->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007
8008 /* Note that we try to include the Async I/O support
8009 * here by modeling from the current TCP/UDP code.
8010 * We have not tested with it yet.
8011 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00008012 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Eric Dumazetceb5d582015-11-29 20:03:11 -08008013 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008014 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08008015 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008016 }
8017}
8018
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008019static void sctp_wake_up_waiters(struct sock *sk,
8020 struct sctp_association *asoc)
8021{
8022 struct sctp_association *tmp = asoc;
8023
8024 /* We do accounting for the sndbuf space per association,
8025 * so we only need to wake our own association.
8026 */
8027 if (asoc->ep->sndbuf_policy)
8028 return __sctp_write_space(asoc);
8029
Daniel Borkmann1e1cdf82014-04-09 16:10:20 +02008030 /* If association goes down and is just flushing its
8031 * outq, then just normally notify others.
8032 */
8033 if (asoc->base.dead)
8034 return sctp_write_space(sk);
8035
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008036 /* Accounting for the sndbuf space is per socket, so we
8037 * need to wake up others, try to be fair and in case of
8038 * other associations, let them have a go first instead
8039 * of just doing a sctp_write_space() call.
8040 *
8041 * Note that we reach sctp_wake_up_waiters() only when
8042 * associations free up queued chunks, thus we are under
8043 * lock and the list of associations on a socket is
8044 * guaranteed not to change.
8045 */
8046 for (tmp = list_next_entry(tmp, asocs); 1;
8047 tmp = list_next_entry(tmp, asocs)) {
8048 /* Manually skip the head element. */
8049 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8050 continue;
8051 /* Wake up association. */
8052 __sctp_write_space(tmp);
8053 /* We've reached the end. */
8054 if (tmp == asoc)
8055 break;
8056 }
8057}
8058
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059/* Do accounting for the sndbuf space.
8060 * Decrement the used sndbuf space of the corresponding association by the
8061 * data size which was just transmitted(freed).
8062 */
8063static void sctp_wfree(struct sk_buff *skb)
8064{
Daniel Borkmannf869c912014-11-20 01:54:48 +01008065 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8066 struct sctp_association *asoc = chunk->asoc;
8067 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068
Neil Horman4eb701d2005-04-28 12:02:04 -07008069 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
8070 sizeof(struct sk_buff) +
8071 sizeof(struct sctp_chunk);
8072
Reshetova, Elena14afee42017-06-30 13:08:00 +03008073 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
Neil Horman4eb701d2005-04-28 12:02:04 -07008074
Neil Horman4d93df02007-08-15 16:07:44 -07008075 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008076 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
Neil Horman4d93df02007-08-15 16:07:44 -07008077 */
8078 sk->sk_wmem_queued -= skb->truesize;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008079 sk_mem_uncharge(sk, skb->truesize);
Neil Horman4d93df02007-08-15 16:07:44 -07008080
Neil Horman4eb701d2005-04-28 12:02:04 -07008081 sock_wfree(skb);
Daniel Borkmann52c35be2014-04-08 17:26:13 +02008082 sctp_wake_up_waiters(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083
8084 sctp_association_put(asoc);
8085}
8086
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008087/* Do accounting for the receive space on the socket.
8088 * Accounting for the association is done in ulpevent.c
8089 * We set this as a destructor for the cloned data skbs so that
8090 * accounting is done at the correct time.
8091 */
8092void sctp_sock_rfree(struct sk_buff *skb)
8093{
8094 struct sock *sk = skb->sk;
8095 struct sctp_ulpevent *event = sctp_skb2event(skb);
8096
8097 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07008098
8099 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008100 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07008101 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08008102 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07008103}
8104
8105
Linus Torvalds1da177e2005-04-16 15:20:36 -07008106/* Helper function to wait for space in the sndbuf. */
8107static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longa0ff6602018-01-15 17:01:36 +08008108 size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109{
8110 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008111 long current_timeo = *timeo_p;
8112 DEFINE_WAIT(wait);
Xin Longa0ff6602018-01-15 17:01:36 +08008113 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114
Daniel Borkmannbb333812013-06-28 19:49:40 +02008115 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8116 *timeo_p, msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117
8118 /* Increment the association's refcnt. */
8119 sctp_association_hold(asoc);
8120
8121 /* Wait on the association specific sndbuf space. */
8122 for (;;) {
8123 prepare_to_wait_exclusive(&asoc->wait, &wait,
8124 TASK_INTERRUPTIBLE);
Xin Longca3af4d2017-11-15 16:55:54 +08008125 if (asoc->base.dead)
8126 goto do_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008127 if (!*timeo_p)
8128 goto do_nonblock;
Xin Longca3af4d2017-11-15 16:55:54 +08008129 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008130 goto do_error;
8131 if (signal_pending(current))
8132 goto do_interrupted;
8133 if (msg_len <= sctp_wspace(asoc))
8134 break;
8135
8136 /* Let another process have a go. Since we are going
8137 * to sleep anyway.
8138 */
wangweidong048ed4b2014-01-21 15:44:11 +08008139 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008140 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008141 lock_sock(sk);
Xin Longa0ff6602018-01-15 17:01:36 +08008142 if (sk != asoc->base.sk)
8143 goto do_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008144
8145 *timeo_p = current_timeo;
8146 }
8147
8148out:
8149 finish_wait(&asoc->wait, &wait);
8150
8151 /* Release the association's refcnt. */
8152 sctp_association_put(asoc);
8153
8154 return err;
8155
Xin Longca3af4d2017-11-15 16:55:54 +08008156do_dead:
8157 err = -ESRCH;
8158 goto out;
8159
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160do_error:
8161 err = -EPIPE;
8162 goto out;
8163
8164do_interrupted:
8165 err = sock_intr_errno(*timeo_p);
8166 goto out;
8167
8168do_nonblock:
8169 err = -EAGAIN;
8170 goto out;
8171}
8172
David S. Miller676d2362014-04-11 16:15:36 -04008173void sctp_data_ready(struct sock *sk)
Wei Yongjun561b1732010-04-28 08:47:18 +00008174{
David S. Miller7ef52732010-05-02 21:43:40 -07008175 struct socket_wq *wq;
8176
8177 rcu_read_lock();
8178 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08008179 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008180 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8181 EPOLLRDNORM | EPOLLRDBAND);
Wei Yongjun561b1732010-04-28 08:47:18 +00008182 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07008183 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00008184}
8185
Linus Torvalds1da177e2005-04-16 15:20:36 -07008186/* If socket sndbuf has changed, wake up all per association waiters. */
8187void sctp_write_space(struct sock *sk)
8188{
8189 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008190
8191 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07008192 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193 __sctp_write_space(asoc);
8194 }
8195}
8196
8197/* Is there any sndbuf space available on the socket?
8198 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08008199 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07008200 * associations on the same socket. For a UDP-style socket with
8201 * multiple associations, it is possible for it to be "unwriteable"
8202 * prematurely. I assume that this is acceptable because
8203 * a premature "unwriteable" is better than an accidental "writeable" which
8204 * would cause an unwanted block under certain circumstances. For the 1-1
8205 * UDP-style sockets or TCP-style sockets, this code should work.
8206 * - Daisy
8207 */
8208static int sctp_writeable(struct sock *sk)
8209{
8210 int amt = 0;
8211
Eric Dumazet31e6d362009-06-17 19:05:41 -07008212 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008213 if (amt < 0)
8214 amt = 0;
8215 return amt;
8216}
8217
8218/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8219 * returns immediately with EINPROGRESS.
8220 */
8221static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8222{
8223 struct sock *sk = asoc->base.sk;
8224 int err = 0;
8225 long current_timeo = *timeo_p;
8226 DEFINE_WAIT(wait);
8227
Daniel Borkmannbb333812013-06-28 19:49:40 +02008228 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229
8230 /* Increment the association's refcnt. */
8231 sctp_association_hold(asoc);
8232
8233 for (;;) {
8234 prepare_to_wait_exclusive(&asoc->wait, &wait,
8235 TASK_INTERRUPTIBLE);
8236 if (!*timeo_p)
8237 goto do_nonblock;
8238 if (sk->sk_shutdown & RCV_SHUTDOWN)
8239 break;
8240 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
8241 asoc->base.dead)
8242 goto do_error;
8243 if (signal_pending(current))
8244 goto do_interrupted;
8245
8246 if (sctp_state(asoc, ESTABLISHED))
8247 break;
8248
8249 /* Let another process have a go. Since we are going
8250 * to sleep anyway.
8251 */
wangweidong048ed4b2014-01-21 15:44:11 +08008252 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008254 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255
8256 *timeo_p = current_timeo;
8257 }
8258
8259out:
8260 finish_wait(&asoc->wait, &wait);
8261
8262 /* Release the association's refcnt. */
8263 sctp_association_put(asoc);
8264
8265 return err;
8266
8267do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08008268 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269 err = -ETIMEDOUT;
8270 else
8271 err = -ECONNREFUSED;
8272 goto out;
8273
8274do_interrupted:
8275 err = sock_intr_errno(*timeo_p);
8276 goto out;
8277
8278do_nonblock:
8279 err = -EINPROGRESS;
8280 goto out;
8281}
8282
8283static int sctp_wait_for_accept(struct sock *sk, long timeo)
8284{
8285 struct sctp_endpoint *ep;
8286 int err = 0;
8287 DEFINE_WAIT(wait);
8288
8289 ep = sctp_sk(sk)->ep;
8290
8291
8292 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00008293 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294 TASK_INTERRUPTIBLE);
8295
8296 if (list_empty(&ep->asocs)) {
wangweidong048ed4b2014-01-21 15:44:11 +08008297 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008298 timeo = schedule_timeout(timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008299 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008300 }
8301
8302 err = -EINVAL;
8303 if (!sctp_sstate(sk, LISTENING))
8304 break;
8305
8306 err = 0;
8307 if (!list_empty(&ep->asocs))
8308 break;
8309
8310 err = sock_intr_errno(timeo);
8311 if (signal_pending(current))
8312 break;
8313
8314 err = -EAGAIN;
8315 if (!timeo)
8316 break;
8317 }
8318
Eric Dumazetaa395142010-04-20 13:03:51 +00008319 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008320
8321 return err;
8322}
8323
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02008324static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008325{
8326 DEFINE_WAIT(wait);
8327
8328 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00008329 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008330 if (list_empty(&sctp_sk(sk)->ep->asocs))
8331 break;
wangweidong048ed4b2014-01-21 15:44:11 +08008332 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008333 timeout = schedule_timeout(timeout);
wangweidong048ed4b2014-01-21 15:44:11 +08008334 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008335 } while (!signal_pending(current) && timeout);
8336
Eric Dumazetaa395142010-04-20 13:03:51 +00008337 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338}
8339
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008340static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
8341{
8342 struct sk_buff *frag;
8343
8344 if (!skb->data_len)
8345 goto done;
8346
8347 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07008348 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008349 sctp_skb_set_owner_r_frag(frag, sk);
8350
8351done:
8352 sctp_skb_set_owner_r(skb, sk);
8353}
8354
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008355void sctp_copy_sock(struct sock *newsk, struct sock *sk,
8356 struct sctp_association *asoc)
8357{
8358 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08008359 struct inet_sock *newinet;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008360
8361 newsk->sk_type = sk->sk_type;
8362 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
8363 newsk->sk_flags = sk->sk_flags;
Marcelo Ricardo Leitner50a5ffb2015-12-04 15:14:05 -02008364 newsk->sk_tsflags = sk->sk_tsflags;
Tom Herbert28448b82014-05-23 08:47:19 -07008365 newsk->sk_no_check_tx = sk->sk_no_check_tx;
8366 newsk->sk_no_check_rx = sk->sk_no_check_rx;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008367 newsk->sk_reuse = sk->sk_reuse;
8368
8369 newsk->sk_shutdown = sk->sk_shutdown;
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02008370 newsk->sk_destruct = sctp_destruct_sock;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008371 newsk->sk_family = sk->sk_family;
8372 newsk->sk_protocol = IPPROTO_SCTP;
8373 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
8374 newsk->sk_sndbuf = sk->sk_sndbuf;
8375 newsk->sk_rcvbuf = sk->sk_rcvbuf;
8376 newsk->sk_lingertime = sk->sk_lingertime;
8377 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
8378 newsk->sk_sndtimeo = sk->sk_sndtimeo;
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03008379 newsk->sk_rxhash = sk->sk_rxhash;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008380
8381 newinet = inet_sk(newsk);
8382
8383 /* Initialize sk's sport, dport, rcv_saddr and daddr for
8384 * getsockname() and getpeername()
8385 */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008386 newinet->inet_sport = inet->inet_sport;
8387 newinet->inet_saddr = inet->inet_saddr;
8388 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
8389 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008390 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008391 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008392
8393 newinet->uc_ttl = inet->uc_ttl;
8394 newinet->mc_loop = 1;
8395 newinet->mc_ttl = 1;
8396 newinet->mc_index = 0;
8397 newinet->mc_list = NULL;
Marcelo Ricardo Leitner01ce63c2015-12-04 15:14:04 -02008398
8399 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
8400 net_enable_timestamp();
Marcelo Ricardo Leitner3538a5c2015-12-23 16:44:09 -02008401
8402 security_sk_clone(sk, newsk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008403}
8404
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008405static inline void sctp_copy_descendant(struct sock *sk_to,
8406 const struct sock *sk_from)
8407{
8408 int ancestor_size = sizeof(struct inet_sock) +
8409 sizeof(struct sctp_sock) -
8410 offsetof(struct sctp_sock, auto_asconf_list);
8411
8412 if (sk_from->sk_family == PF_INET6)
8413 ancestor_size += sizeof(struct ipv6_pinfo);
8414
8415 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
8416}
8417
Linus Torvalds1da177e2005-04-16 15:20:36 -07008418/* Populate the fields of the newsk from the oldsk and migrate the assoc
8419 * and its messages to the newsk.
8420 */
8421static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
8422 struct sctp_association *assoc,
Xin Longb7ef2612017-08-11 10:23:50 +08008423 enum sctp_socket_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008424{
8425 struct sctp_sock *oldsp = sctp_sk(oldsk);
8426 struct sctp_sock *newsp = sctp_sk(newsk);
8427 struct sctp_bind_bucket *pp; /* hash list port iterator */
8428 struct sctp_endpoint *newep = newsp->ep;
8429 struct sk_buff *skb, *tmp;
8430 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08008431 struct sctp_bind_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008432
8433 /* Migrate socket buffer sizes and all the socket level options to the
8434 * new socket.
8435 */
8436 newsk->sk_sndbuf = oldsk->sk_sndbuf;
8437 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
8438 /* Brute force copy old sctp opt. */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008439 sctp_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008440
8441 /* Restore the ep value that was overwritten with the above structure
8442 * copy.
8443 */
8444 newsp->ep = newep;
8445 newsp->hmac = NULL;
8446
8447 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008448 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
8449 inet_sk(oldsk)->inet_num)];
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008450 spin_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451 pp = sctp_sk(oldsk)->bind_hash;
8452 sk_add_bind_node(newsk, &pp->owner);
8453 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008454 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008455 spin_unlock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008456
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008457 /* Copy the bind_addr list from the original endpoint to the new
8458 * endpoint so that we can handle restarts properly
8459 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08008460 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
8461 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008462
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463 /* Move any messages in the old socket's receive queue that are for the
8464 * peeled off association to the new socket's receive queue.
8465 */
8466 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
8467 event = sctp_skb2event(skb);
8468 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008469 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008470 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008471 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008472 }
8473 }
8474
8475 /* Clean up any messages pending delivery due to partial
8476 * delivery. Three cases:
8477 * 1) No partial deliver; no work.
8478 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
8479 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
8480 */
8481 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008482 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008483
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008484 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485 struct sk_buff_head *queue;
8486
8487 /* Decide which queue to move pd_lobby skbs to. */
8488 if (assoc->ulpq.pd_mode) {
8489 queue = &newsp->pd_lobby;
8490 } else
8491 queue = &newsk->sk_receive_queue;
8492
8493 /* Walk through the pd_lobby, looking for skbs that
8494 * need moved to the new socket.
8495 */
8496 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
8497 event = sctp_skb2event(skb);
8498 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008499 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008500 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008501 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008502 }
8503 }
8504
8505 /* Clear up any skbs waiting for the partial
8506 * delivery to finish.
8507 */
8508 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008509 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510
8511 }
8512
Xin Long13228232017-12-08 21:04:09 +08008513 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008514
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515 /* Set the type of socket to indicate that it is peeled off from the
8516 * original UDP-style socket or created with the accept() call on a
8517 * TCP-style socket..
8518 */
8519 newsp->type = type;
8520
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008521 /* Mark the new socket "in-use" by the user so that any packets
8522 * that may arrive on the association after we've moved it are
8523 * queued to the backlog. This prevents a potential race between
8524 * backlog processing on the old socket and new-packet processing
8525 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07008526 *
8527 * The caller has just allocated newsk so we can guarantee that other
8528 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008529 */
Zach Brown5131a182007-06-22 15:14:46 -07008530 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Xin Longd04adf12017-10-28 02:13:29 +08008531 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008532 sctp_assoc_migrate(assoc, newsk);
Xin Longd04adf12017-10-28 02:13:29 +08008533 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534
8535 /* If the association on the newsk is already closed before accept()
8536 * is called, set RCV_SHUTDOWN flag.
8537 */
Xin Longd46e4162016-06-09 22:48:18 +08008538 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
Yafang Shaocbabf462017-12-20 11:12:54 +08008539 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540 newsk->sk_shutdown |= RCV_SHUTDOWN;
Xin Longd46e4162016-06-09 22:48:18 +08008541 } else {
Yafang Shaocbabf462017-12-20 11:12:54 +08008542 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
Xin Longd46e4162016-06-09 22:48:18 +08008543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008544
wangweidong048ed4b2014-01-21 15:44:11 +08008545 release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546}
8547
Neil Horman4d93df02007-08-15 16:07:44 -07008548
Linus Torvalds1da177e2005-04-16 15:20:36 -07008549/* This proto struct describes the ULP interface for SCTP. */
8550struct proto sctp_prot = {
8551 .name = "SCTP",
8552 .owner = THIS_MODULE,
8553 .close = sctp_close,
8554 .connect = sctp_connect,
8555 .disconnect = sctp_disconnect,
8556 .accept = sctp_accept,
8557 .ioctl = sctp_ioctl,
8558 .init = sctp_init_sock,
8559 .destroy = sctp_destroy_sock,
8560 .shutdown = sctp_shutdown,
8561 .setsockopt = sctp_setsockopt,
8562 .getsockopt = sctp_getsockopt,
8563 .sendmsg = sctp_sendmsg,
8564 .recvmsg = sctp_recvmsg,
8565 .bind = sctp_bind,
8566 .backlog_rcv = sctp_backlog_rcv,
8567 .hash = sctp_hash,
8568 .unhash = sctp_unhash,
8569 .get_port = sctp_get_port,
8570 .obj_size = sizeof(struct sctp_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07008571 .useroffset = offsetof(struct sctp_sock, subscribe),
8572 .usersize = offsetof(struct sctp_sock, initmsg) -
8573 offsetof(struct sctp_sock, subscribe) +
8574 sizeof_field(struct sctp_sock, initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07008575 .sysctl_mem = sysctl_sctp_mem,
8576 .sysctl_rmem = sysctl_sctp_rmem,
8577 .sysctl_wmem = sysctl_sctp_wmem,
8578 .memory_pressure = &sctp_memory_pressure,
8579 .enter_memory_pressure = sctp_enter_memory_pressure,
8580 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008581 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582};
8583
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008584#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08008585
Eric Dumazet602dd622015-12-01 07:20:07 -08008586#include <net/transp_v6.h>
8587static void sctp_v6_destroy_sock(struct sock *sk)
8588{
8589 sctp_destroy_sock(sk);
8590 inet6_destroy_sock(sk);
8591}
8592
Linus Torvalds1da177e2005-04-16 15:20:36 -07008593struct proto sctpv6_prot = {
8594 .name = "SCTPv6",
8595 .owner = THIS_MODULE,
8596 .close = sctp_close,
8597 .connect = sctp_connect,
8598 .disconnect = sctp_disconnect,
8599 .accept = sctp_accept,
8600 .ioctl = sctp_ioctl,
8601 .init = sctp_init_sock,
Eric Dumazet602dd622015-12-01 07:20:07 -08008602 .destroy = sctp_v6_destroy_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008603 .shutdown = sctp_shutdown,
8604 .setsockopt = sctp_setsockopt,
8605 .getsockopt = sctp_getsockopt,
8606 .sendmsg = sctp_sendmsg,
8607 .recvmsg = sctp_recvmsg,
8608 .bind = sctp_bind,
8609 .backlog_rcv = sctp_backlog_rcv,
8610 .hash = sctp_hash,
8611 .unhash = sctp_unhash,
8612 .get_port = sctp_get_port,
8613 .obj_size = sizeof(struct sctp6_sock),
David Windsorab9ee8e2017-08-24 16:57:57 -07008614 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
8615 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
8616 offsetof(struct sctp6_sock, sctp.subscribe) +
8617 sizeof_field(struct sctp6_sock, sctp.initmsg),
Neil Horman4d93df02007-08-15 16:07:44 -07008618 .sysctl_mem = sysctl_sctp_mem,
8619 .sysctl_rmem = sysctl_sctp_rmem,
8620 .sysctl_wmem = sysctl_sctp_wmem,
8621 .memory_pressure = &sctp_memory_pressure,
8622 .enter_memory_pressure = sctp_enter_memory_pressure,
8623 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008624 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008625};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008626#endif /* IS_ENABLED(CONFIG_IPV6) */