blob: 3e55daa37e66533b8aa76133a5ab8beb3e72ad4f [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,
88 size_t msg_len, struct sock **orig_sk);
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/* Verify that this is a valid address. */
205static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
206 int len)
207{
208 struct sctp_af *af;
209
210 /* Verify basic sockaddr. */
211 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
212 if (!af)
213 return -EINVAL;
214
215 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700216 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return -EINVAL;
218
219 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
220 return -EINVAL;
221
222 return 0;
223}
224
225/* Look up the association by its id. If this is not a UDP-style
226 * socket, the ID field is always ignored.
227 */
228struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
229{
230 struct sctp_association *asoc = NULL;
231
232 /* If this is not a UDP-style socket, assoc id should be ignored. */
233 if (!sctp_style(sk, UDP)) {
234 /* Return NULL if the socket state is not ESTABLISHED. It
235 * could be a TCP-style listening socket or a socket which
236 * hasn't yet called connect() to establish an association.
237 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -0300238 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return NULL;
240
241 /* Get the first and the only association from the list. */
242 if (!list_empty(&sctp_sk(sk)->ep->asocs))
243 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
244 struct sctp_association, asocs);
245 return asoc;
246 }
247
248 /* Otherwise this is a UDP-style socket. */
249 if (!id || (id == (sctp_assoc_t)-1))
250 return NULL;
251
252 spin_lock_bh(&sctp_assocs_id_lock);
253 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
254 spin_unlock_bh(&sctp_assocs_id_lock);
255
256 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
257 return NULL;
258
259 return asoc;
260}
261
262/* Look up the transport from an address and an assoc id. If both address and
263 * id are specified, the associations matching the address and the id should be
264 * the same.
265 */
266static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
267 struct sockaddr_storage *addr,
268 sctp_assoc_t id)
269{
270 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
Xin Long6f29a132017-01-24 14:01:53 +0800271 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 union sctp_addr *laddr = (union sctp_addr *)addr;
Xin Long6f29a132017-01-24 14:01:53 +0800273 struct sctp_transport *transport;
274
Xin Long912964e2017-02-07 20:56:08 +0800275 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
Xin Long6f29a132017-01-24 14:01:53 +0800276 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800279 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (!addr_asoc)
283 return NULL;
284
285 id_asoc = sctp_id2assoc(sk, id);
286 if (id_asoc && (id_asoc != addr_asoc))
287 return NULL;
288
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600289 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 (union sctp_addr *)addr);
291
292 return transport;
293}
294
295/* API 3.1.2 bind() - UDP Style Syntax
296 * The syntax of bind() is,
297 *
298 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
299 *
300 * sd - the socket descriptor returned by socket().
301 * addr - the address structure (struct sockaddr_in or struct
302 * sockaddr_in6 [RFC 2553]),
303 * addr_len - the size of the address structure.
304 */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200305static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 int retval = 0;
308
wangweidong048ed4b2014-01-21 15:44:11 +0800309 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Daniel Borkmannbb333812013-06-28 19:49:40 +0200311 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
312 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* Disallow binding twice. */
315 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700316 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 addr_len);
318 else
319 retval = -EINVAL;
320
wangweidong048ed4b2014-01-21 15:44:11 +0800321 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 return retval;
324}
325
326static long sctp_get_port_local(struct sock *, union sctp_addr *);
327
328/* Verify this is a valid sockaddr. */
329static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
330 union sctp_addr *addr, int len)
331{
332 struct sctp_af *af;
333
334 /* Check minimum size. */
335 if (len < sizeof (struct sockaddr))
336 return NULL;
337
Vlad Yasevich7dab83d2008-07-18 23:05:40 -0700338 /* V4 mapped address are really of AF_INET family */
339 if (addr->sa.sa_family == AF_INET6 &&
340 ipv6_addr_v4mapped(&addr->v6.sin6_addr)) {
341 if (!opt->pf->af_supported(AF_INET, opt))
342 return NULL;
343 } else {
344 /* Does this PF support this AF? */
345 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
346 return NULL;
347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 /* If we get this far, af is valid. */
350 af = sctp_get_af_specific(addr->sa.sa_family);
351
352 if (len < af->sockaddr_len)
353 return NULL;
354
355 return af;
356}
357
358/* Bind a local address either to an endpoint or to an association. */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200359static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Eric W. Biederman35946982012-11-16 03:03:12 +0000361 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct sctp_sock *sp = sctp_sk(sk);
363 struct sctp_endpoint *ep = sp->ep;
364 struct sctp_bind_addr *bp = &ep->base.bind_addr;
365 struct sctp_af *af;
366 unsigned short snum;
367 int ret = 0;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* Common sockaddr verification. */
370 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700371 if (!af) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200372 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
373 __func__, sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700375 }
376
377 snum = ntohs(addr->v4.sin_port);
378
Daniel Borkmannbb333812013-06-28 19:49:40 +0200379 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
380 __func__, sk, &addr->sa, bp->port, snum, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /* PF specific bind() address verification. */
383 if (!sp->pf->bind_verify(sp, addr))
384 return -EADDRNOTAVAIL;
385
Vlad Yasevich8b358052007-05-15 17:14:58 -0400386 /* We must either be unbound, or bind to the same port.
387 * It's OK to allow 0 ports if we are already bound.
388 * We'll just inhert an already bound port in this case
389 */
390 if (bp->port) {
391 if (!snum)
392 snum = bp->port;
393 else if (snum != bp->port) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200394 pr_debug("%s: new port %d doesn't match existing port "
395 "%d\n", __func__, snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400396 return -EINVAL;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
Krister Johansen4548b682017-01-20 17:49:11 -0800400 if (snum && snum < inet_prot_sock(net) &&
Eric W. Biederman35946982012-11-16 03:03:12 +0000401 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return -EACCES;
403
Vlad Yasevich4e540642008-07-18 23:06:32 -0700404 /* See if the address matches any of the addresses we may have
405 * already bound before checking against other endpoints.
406 */
407 if (sctp_bind_addr_match(bp, addr, sp))
408 return -EINVAL;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* Make sure we are allowed to bind here.
411 * The function sctp_get_port_local() does duplicate address
412 * detection.
413 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900414 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if ((ret = sctp_get_port_local(sk, addr))) {
Vlad Yasevich4e540642008-07-18 23:06:32 -0700416 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
419 /* Refresh ephemeral port. */
420 if (!bp->port)
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000421 bp->port = inet_sk(sk)->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Vlad Yasevich559cf712007-09-16 16:03:28 -0700423 /* Add the address to the bind address list.
424 * Use GFP_ATOMIC since BHs will be disabled.
425 */
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300426 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
427 SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /* Copy back into socket for getsockname() use. */
430 if (!ret) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000431 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600432 sp->pf->to_sk_saddr(addr, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
435 return ret;
436}
437
438 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
439 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900440 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900442 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900444 * subsequent ASCONF. Note this restriction binds each side, so at any
445 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * from each endpoint).
447 */
448static int sctp_send_asconf(struct sctp_association *asoc,
449 struct sctp_chunk *chunk)
450{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000451 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int retval = 0;
453
454 /* If there is an outstanding ASCONF chunk, queue it for later
455 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900456 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700458 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900459 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
462 /* Hold the chunk until an ASCONF_ACK is received. */
463 sctp_chunk_hold(chunk);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000464 retval = sctp_primitive_ASCONF(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (retval)
466 sctp_chunk_free(chunk);
467 else
468 asoc->addip_last_asconf = chunk;
469
470out:
471 return retval;
472}
473
474/* Add a list of addresses as bind addresses to local endpoint or
475 * association.
476 *
477 * Basically run through each address specified in the addrs/addrcnt
478 * array/length pair, determine if it is IPv6 or IPv4 and call
479 * sctp_do_bind() on it.
480 *
481 * If any of them fails, then the operation will be reversed and the
482 * ones that were added will be removed.
483 *
484 * Only sctp_setsockopt_bindx() is supposed to call this function.
485 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200486static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 int cnt;
489 int retval = 0;
490 void *addr_buf;
491 struct sockaddr *sa_addr;
492 struct sctp_af *af;
493
Daniel Borkmannbb333812013-06-28 19:49:40 +0200494 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
495 addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 addr_buf = addrs;
498 for (cnt = 0; cnt < addrcnt; cnt++) {
499 /* The list may contain either IPv4 or IPv6 address;
500 * determine the address length for walking thru the list.
501 */
Joe Perchesea110732011-06-13 16:21:26 +0000502 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 af = sctp_get_af_specific(sa_addr->sa_family);
504 if (!af) {
505 retval = -EINVAL;
506 goto err_bindx_add;
507 }
508
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900509 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 af->sockaddr_len);
511
512 addr_buf += af->sockaddr_len;
513
514err_bindx_add:
515 if (retval < 0) {
516 /* Failed. Cleanup the ones that have been added */
517 if (cnt > 0)
518 sctp_bindx_rem(sk, addrs, cnt);
519 return retval;
520 }
521 }
522
523 return retval;
524}
525
526/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
527 * associations that are part of the endpoint indicating that a list of local
528 * addresses are added to the endpoint.
529 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900530 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * association, we do not send the chunk for that association. But it will not
532 * affect other associations.
533 *
534 * Only sctp_setsockopt_bindx() is supposed to call this function.
535 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900536static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct sockaddr *addrs,
538 int addrcnt)
539{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000540 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct sctp_sock *sp;
542 struct sctp_endpoint *ep;
543 struct sctp_association *asoc;
544 struct sctp_bind_addr *bp;
545 struct sctp_chunk *chunk;
546 struct sctp_sockaddr_entry *laddr;
547 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700548 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 void *addr_buf;
550 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct list_head *p;
552 int i;
553 int retval = 0;
554
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000555 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return retval;
557
558 sp = sctp_sk(sk);
559 ep = sp->ep;
560
Daniel Borkmannbb333812013-06-28 19:49:40 +0200561 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
562 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700564 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (!asoc->peer.asconf_capable)
566 continue;
567
568 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
569 continue;
570
571 if (!sctp_state(asoc, ESTABLISHED))
572 continue;
573
574 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900575 * in the bind address list of the association. If so,
576 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 * other associations.
578 */
579 addr_buf = addrs;
580 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000581 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 af = sctp_get_af_specific(addr->v4.sin_family);
583 if (!af) {
584 retval = -EINVAL;
585 goto out;
586 }
587
588 if (sctp_assoc_lookup_laddr(asoc, addr))
589 break;
590
591 addr_buf += af->sockaddr_len;
592 }
593 if (i < addrcnt)
594 continue;
595
Vlad Yasevich559cf712007-09-16 16:03:28 -0700596 /* Use the first valid address in bind addr list of
597 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 bp = &asoc->base.bind_addr;
600 p = bp->address_list.next;
601 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800602 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 addrcnt, SCTP_PARAM_ADD_IP);
604 if (!chunk) {
605 retval = -ENOMEM;
606 goto out;
607 }
608
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700609 /* Add the new addresses to the bind address list with
610 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700612 addr_buf = addrs;
613 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000614 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700615 af = sctp_get_af_specific(addr->v4.sin_family);
616 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800617 retval = sctp_add_bind_addr(bp, &saveaddr,
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300618 sizeof(saveaddr),
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800619 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700620 addr_buf += af->sockaddr_len;
621 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900622 if (asoc->src_out_of_asoc_ok) {
623 struct sctp_transport *trans;
624
625 list_for_each_entry(trans,
626 &asoc->peer.transport_addr_list, transports) {
627 /* Clear the source and route cache */
Julian Anastasovc86a7732017-02-06 23:14:13 +0200628 sctp_transport_dst_release(trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900629 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
630 2*asoc->pathmtu, 4380));
631 trans->ssthresh = asoc->peer.i.a_rwnd;
632 trans->rto = asoc->rto_initial;
Michele Baldessari196d6752012-12-01 04:49:42 +0000633 sctp_max_rto(asoc, trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900634 trans->rtt = trans->srtt = trans->rttvar = 0;
635 sctp_transport_route(trans, NULL,
636 sctp_sk(asoc->base.sk));
637 }
638 }
639 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
642out:
643 return retval;
644}
645
646/* Remove a list of addresses from bind addresses list. Do not remove the
647 * last address.
648 *
649 * Basically run through each address specified in the addrs/addrcnt
650 * array/length pair, determine if it is IPv6 or IPv4 and call
651 * sctp_del_bind() on it.
652 *
653 * If any of them fails, then the operation will be reversed and the
654 * ones that were removed will be added back.
655 *
656 * At least one address has to be left; if only one address is
657 * available, the operation will return -EBUSY.
658 *
659 * Only sctp_setsockopt_bindx() is supposed to call this function.
660 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200661static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct sctp_sock *sp = sctp_sk(sk);
664 struct sctp_endpoint *ep = sp->ep;
665 int cnt;
666 struct sctp_bind_addr *bp = &ep->base.bind_addr;
667 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800669 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct sctp_af *af;
671
Daniel Borkmannbb333812013-06-28 19:49:40 +0200672 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
673 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 addr_buf = addrs;
676 for (cnt = 0; cnt < addrcnt; cnt++) {
677 /* If the bind address list is empty or if there is only one
678 * bind address, there is nothing more to be removed (we need
679 * at least one address here).
680 */
681 if (list_empty(&bp->address_list) ||
682 (sctp_list_single_entry(&bp->address_list))) {
683 retval = -EBUSY;
684 goto err_bindx_rem;
685 }
686
Joe Perchesea110732011-06-13 16:21:26 +0000687 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800688 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (!af) {
690 retval = -EINVAL;
691 goto err_bindx_rem;
692 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700693
694 if (!af->addr_valid(sa_addr, sp, NULL)) {
695 retval = -EADDRNOTAVAIL;
696 goto err_bindx_rem;
697 }
698
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000699 if (sa_addr->v4.sin_port &&
700 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 retval = -EINVAL;
702 goto err_bindx_rem;
703 }
704
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000705 if (!sa_addr->v4.sin_port)
706 sa_addr->v4.sin_port = htons(bp->port);
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /* FIXME - There is probably a need to check if sk->sk_saddr and
709 * sk->sk_rcv_addr are currently set to one of the addresses to
710 * be removed. This is something which needs to be looked into
711 * when we are fixing the outstanding issues with multi-homing
712 * socket routing and failover schemes. Refer to comments in
713 * sctp_do_bind(). -daisy
714 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400715 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 addr_buf += af->sockaddr_len;
718err_bindx_rem:
719 if (retval < 0) {
720 /* Failed. Add the ones that has been removed back */
721 if (cnt > 0)
722 sctp_bindx_add(sk, addrs, cnt);
723 return retval;
724 }
725 }
726
727 return retval;
728}
729
730/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
731 * the associations that are part of the endpoint indicating that a list of
732 * local addresses are removed from the endpoint.
733 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900734 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * association, we do not send the chunk for that association. But it will not
736 * affect other associations.
737 *
738 * Only sctp_setsockopt_bindx() is supposed to call this function.
739 */
740static int sctp_send_asconf_del_ip(struct sock *sk,
741 struct sockaddr *addrs,
742 int addrcnt)
743{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000744 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct sctp_sock *sp;
746 struct sctp_endpoint *ep;
747 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700748 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 struct sctp_bind_addr *bp;
750 struct sctp_chunk *chunk;
751 union sctp_addr *laddr;
752 void *addr_buf;
753 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700754 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 int i;
756 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900757 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Michio Honda8a07eb02011-04-26 20:19:36 +0900759 chunk = NULL;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000760 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return retval;
762
763 sp = sctp_sk(sk);
764 ep = sp->ep;
765
Daniel Borkmannbb333812013-06-28 19:49:40 +0200766 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
767 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700769 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 if (!asoc->peer.asconf_capable)
772 continue;
773
774 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
775 continue;
776
777 if (!sctp_state(asoc, ESTABLISHED))
778 continue;
779
780 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900781 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 * If so, do not send the asconf chunk to its peer, but
783 * continue with other associations.
784 */
785 addr_buf = addrs;
786 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000787 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 af = sctp_get_af_specific(laddr->v4.sin_family);
789 if (!af) {
790 retval = -EINVAL;
791 goto out;
792 }
793
794 if (!sctp_assoc_lookup_laddr(asoc, laddr))
795 break;
796
797 addr_buf += af->sockaddr_len;
798 }
799 if (i < addrcnt)
800 continue;
801
802 /* Find one address in the association's bind address list
803 * that is not in the packed array of addresses. This is to
804 * make sure that we do not delete all the addresses in the
805 * association.
806 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 bp = &asoc->base.bind_addr;
808 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
809 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900810 if ((laddr == NULL) && (addrcnt == 1)) {
811 if (asoc->asconf_addr_del_pending)
812 continue;
813 asoc->asconf_addr_del_pending =
814 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900815 if (asoc->asconf_addr_del_pending == NULL) {
816 retval = -ENOMEM;
817 goto out;
818 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900819 asoc->asconf_addr_del_pending->sa.sa_family =
820 addrs->sa_family;
821 asoc->asconf_addr_del_pending->v4.sin_port =
822 htons(bp->port);
823 if (addrs->sa_family == AF_INET) {
824 struct sockaddr_in *sin;
825
826 sin = (struct sockaddr_in *)addrs;
827 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
828 } else if (addrs->sa_family == AF_INET6) {
829 struct sockaddr_in6 *sin6;
830
831 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000832 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900833 }
Daniel Borkmannbb333812013-06-28 19:49:40 +0200834
835 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
836 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
837 asoc->asconf_addr_del_pending);
838
Michio Honda8a07eb02011-04-26 20:19:36 +0900839 asoc->src_out_of_asoc_ok = 1;
840 stored = 1;
841 goto skip_mkasconf;
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Daniel Borkmann88362ad2013-09-07 20:51:21 +0200844 if (laddr == NULL)
845 return -EINVAL;
846
Vlad Yasevich559cf712007-09-16 16:03:28 -0700847 /* We do not need RCU protection throughout this loop
848 * because this is done under a socket lock from the
849 * setsockopt call.
850 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
852 SCTP_PARAM_DEL_IP);
853 if (!chunk) {
854 retval = -ENOMEM;
855 goto out;
856 }
857
Michio Honda8a07eb02011-04-26 20:19:36 +0900858skip_mkasconf:
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700859 /* Reset use_as_src flag for the addresses in the bind address
860 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700862 addr_buf = addrs;
863 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000864 laddr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700865 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700866 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a12006-11-20 17:05:23 -0800867 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800868 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700869 }
870 addr_buf += af->sockaddr_len;
871 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700872
873 /* Update the route and saddr entries for all the transports
874 * as some of the addresses in the bind address list are
875 * about to be deleted and cannot be used as source addresses.
876 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700877 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
878 transports) {
Julian Anastasovc86a7732017-02-06 23:14:13 +0200879 sctp_transport_dst_release(transport);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700880 sctp_transport_route(transport, NULL,
881 sctp_sk(asoc->base.sk));
882 }
883
Michio Honda8a07eb02011-04-26 20:19:36 +0900884 if (stored)
885 /* We don't need to transmit ASCONF */
886 continue;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700887 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889out:
890 return retval;
891}
892
Michio Honda9f7d6532011-04-26 19:32:51 +0900893/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
894int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
895{
896 struct sock *sk = sctp_opt2sk(sp);
897 union sctp_addr *addr;
898 struct sctp_af *af;
899
900 /* It is safe to write port space in caller. */
901 addr = &addrw->a;
902 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
903 af = sctp_get_af_specific(addr->sa.sa_family);
904 if (!af)
905 return -EINVAL;
906 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
907 return -EINVAL;
908
909 if (addrw->state == SCTP_ADDR_NEW)
910 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
911 else
912 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
913}
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
916 *
917 * API 8.1
918 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
919 * int flags);
920 *
921 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
922 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
923 * or IPv6 addresses.
924 *
925 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
926 * Section 3.1.2 for this usage.
927 *
928 * addrs is a pointer to an array of one or more socket addresses. Each
929 * address is contained in its appropriate structure (i.e. struct
930 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700931 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 * representation is termed a "packed array" of addresses). The caller
933 * specifies the number of addresses in the array with addrcnt.
934 *
935 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
936 * -1, and sets errno to the appropriate error code.
937 *
938 * For SCTP, the port given in each socket address must be the same, or
939 * sctp_bindx() will fail, setting errno to EINVAL.
940 *
941 * The flags parameter is formed from the bitwise OR of zero or more of
942 * the following currently defined flags:
943 *
944 * SCTP_BINDX_ADD_ADDR
945 *
946 * SCTP_BINDX_REM_ADDR
947 *
948 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
949 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
950 * addresses from the association. The two flags are mutually exclusive;
951 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
952 * not remove all addresses from an association; sctp_bindx() will
953 * reject such an attempt with EINVAL.
954 *
955 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
956 * additional addresses with an endpoint after calling bind(). Or use
957 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
958 * socket is associated with so that no new association accepted will be
959 * associated with those addresses. If the endpoint supports dynamic
960 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
961 * endpoint to send the appropriate message to the peer to change the
962 * peers address lists.
963 *
964 * Adding and removing addresses from a connected association is
965 * optional functionality. Implementations that do not support this
966 * functionality should return EOPNOTSUPP.
967 *
968 * Basically do nothing but copying the addresses from user to kernel
969 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700970 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
971 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 *
973 * We don't use copy_from_user() for optimization: we first do the
974 * sanity checks (buffer size -fast- and access check-healthy
975 * pointer); if all of those succeed, then we can alloc the memory
976 * (expensive operation) needed to copy the data to kernel. Then we do
977 * the copying without checking the user space area
978 * (__copy_from_user()).
979 *
980 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
981 * it.
982 *
983 * sk The sk of the socket
984 * addrs The pointer to the addresses in user land
985 * addrssize Size of the addrs buffer
986 * op Operation to perform (add or remove, see the flags of
987 * sctp_bindx)
988 *
989 * Returns 0 if ok, <0 errno code on error.
990 */
wangweidong26ac8e52013-12-23 12:16:51 +0800991static int sctp_setsockopt_bindx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +0200992 struct sockaddr __user *addrs,
993 int addrs_size, int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 struct sockaddr *kaddrs;
996 int err;
997 int addrcnt = 0;
998 int walk_size = 0;
999 struct sockaddr *sa_addr;
1000 void *addr_buf;
1001 struct sctp_af *af;
1002
Daniel Borkmannbb333812013-06-28 19:49:40 +02001003 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1004 __func__, sk, addrs, addrs_size, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 if (unlikely(addrs_size <= 0))
1007 return -EINVAL;
1008
1009 /* Check the user passed a healthy pointer. */
1010 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1011 return -EFAULT;
1012
1013 /* Alloc space for the address array in kernel memory. */
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02001014 kaddrs = kmalloc(addrs_size, GFP_USER | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (unlikely(!kaddrs))
1016 return -ENOMEM;
1017
1018 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1019 kfree(kaddrs);
1020 return -EFAULT;
1021 }
1022
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001023 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 addr_buf = kaddrs;
1025 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001026 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1027 kfree(kaddrs);
1028 return -EINVAL;
1029 }
1030
Joe Perchesea110732011-06-13 16:21:26 +00001031 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 af = sctp_get_af_specific(sa_addr->sa_family);
1033
1034 /* If the address family is not supported or if this address
1035 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001036 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1038 kfree(kaddrs);
1039 return -EINVAL;
1040 }
1041 addrcnt++;
1042 addr_buf += af->sockaddr_len;
1043 walk_size += af->sockaddr_len;
1044 }
1045
1046 /* Do the work. */
1047 switch (op) {
1048 case SCTP_BINDX_ADD_ADDR:
1049 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1050 if (err)
1051 goto out;
1052 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1053 break;
1054
1055 case SCTP_BINDX_REM_ADDR:
1056 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1057 if (err)
1058 goto out;
1059 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1060 break;
1061
1062 default:
1063 err = -EINVAL;
1064 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067out:
1068 kfree(kaddrs);
1069
1070 return err;
1071}
1072
Frank Filz3f7a87d2005-06-20 13:14:57 -07001073/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1074 *
1075 * Common routine for handling connect() and sctp_connectx().
1076 * Connect will come in with just a single address.
1077 */
wangweidong26ac8e52013-12-23 12:16:51 +08001078static int __sctp_connect(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001079 struct sockaddr *kaddrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001080 int addrs_size,
1081 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001082{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001083 struct net *net = sock_net(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001084 struct sctp_sock *sp;
1085 struct sctp_endpoint *ep;
1086 struct sctp_association *asoc = NULL;
1087 struct sctp_association *asoc2;
1088 struct sctp_transport *transport;
1089 union sctp_addr to;
Xin Long1c662012017-08-05 19:59:54 +08001090 enum sctp_scope scope;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001091 long timeo;
1092 int err = 0;
1093 int addrcnt = 0;
1094 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001095 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001096 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001097 unsigned short port;
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001098 unsigned int f_flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001099
1100 sp = sctp_sk(sk);
1101 ep = sp->ep;
1102
1103 /* connect() cannot be done on a socket that is already in ESTABLISHED
1104 * state - UDP-style peeled off socket or a TCP-style socket that
1105 * is already connected.
1106 * It cannot be done even on a TCP-style listening socket.
1107 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03001108 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
Frank Filz3f7a87d2005-06-20 13:14:57 -07001109 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1110 err = -EISCONN;
1111 goto out_free;
1112 }
1113
1114 /* Walk through the addrs buffer and count the number of addresses. */
1115 addr_buf = kaddrs;
1116 while (walk_size < addrs_size) {
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001117 struct sctp_af *af;
1118
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001119 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1120 err = -EINVAL;
1121 goto out_free;
1122 }
1123
Joe Perchesea110732011-06-13 16:21:26 +00001124 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001125 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001126
1127 /* If the address family is not supported or if this address
1128 * causes the address buffer to overflow return EINVAL.
1129 */
1130 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1131 err = -EINVAL;
1132 goto out_free;
1133 }
1134
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001135 port = ntohs(sa_addr->v4.sin_port);
1136
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001137 /* Save current address so we can work with it */
1138 memcpy(&to, sa_addr, af->sockaddr_len);
1139
1140 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001141 if (err)
1142 goto out_free;
1143
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001144 /* Make sure the destination port is correctly set
1145 * in all addresses.
1146 */
Wei Yongjun524fba62013-04-03 03:02:28 +00001147 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1148 err = -EINVAL;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001149 goto out_free;
Wei Yongjun524fba62013-04-03 03:02:28 +00001150 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001151
1152 /* Check if there already is a matching association on the
1153 * endpoint (other than the one created here).
1154 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001155 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001156 if (asoc2 && asoc2 != asoc) {
1157 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1158 err = -EISCONN;
1159 else
1160 err = -EALREADY;
1161 goto out_free;
1162 }
1163
1164 /* If we could not find a matching association on the endpoint,
1165 * make sure that there is no peeled-off association matching
1166 * the peer address even on another socket.
1167 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001168 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001169 err = -EADDRNOTAVAIL;
1170 goto out_free;
1171 }
1172
1173 if (!asoc) {
1174 /* If a bind() or sctp_bindx() is not called prior to
1175 * an sctp_connectx() call, the system picks an
1176 * ephemeral port and will choose an address set
1177 * equivalent to binding with a wildcard address.
1178 */
1179 if (!ep->base.bind_addr.port) {
1180 if (sctp_autobind(sk)) {
1181 err = -EAGAIN;
1182 goto out_free;
1183 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001184 } else {
1185 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001186 * If an unprivileged user inherits a 1-many
1187 * style socket with open associations on a
1188 * privileged port, it MAY be permitted to
1189 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001190 * be permitted to open new associations.
1191 */
Krister Johansen4548b682017-01-20 17:49:11 -08001192 if (ep->base.bind_addr.port <
1193 inet_prot_sock(net) &&
1194 !ns_capable(net->user_ns,
1195 CAP_NET_BIND_SERVICE)) {
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001196 err = -EACCES;
1197 goto out_free;
1198 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001199 }
1200
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001201 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001202 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1203 if (!asoc) {
1204 err = -ENOMEM;
1205 goto out_free;
1206 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001207
1208 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1209 GFP_KERNEL);
1210 if (err < 0) {
1211 goto out_free;
1212 }
1213
Frank Filz3f7a87d2005-06-20 13:14:57 -07001214 }
1215
1216 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001217 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001218 SCTP_UNKNOWN);
1219 if (!transport) {
1220 err = -ENOMEM;
1221 goto out_free;
1222 }
1223
1224 addrcnt++;
1225 addr_buf += af->sockaddr_len;
1226 walk_size += af->sockaddr_len;
1227 }
1228
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001229 /* In case the user of sctp_connectx() wants an association
1230 * id back, assign one now.
1231 */
1232 if (assoc_id) {
1233 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1234 if (err < 0)
1235 goto out_free;
1236 }
1237
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001238 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001239 if (err < 0) {
1240 goto out_free;
1241 }
1242
1243 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001244 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001245 sp->pf->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001246 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001247
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001248 /* in-kernel sockets don't generally have a file allocated to them
1249 * if all they do is call sock_create_kern().
1250 */
1251 if (sk->sk_socket->file)
1252 f_flags = sk->sk_socket->file->f_flags;
1253
1254 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1255
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001256 if (assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001257 *assoc_id = asoc->assoc_id;
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001258 err = sctp_wait_for_connect(asoc, &timeo);
1259 /* Note: the asoc may be freed after the return of
1260 * sctp_wait_for_connect.
1261 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001262
1263 /* Don't free association on exit. */
1264 asoc = NULL;
1265
1266out_free:
Daniel Borkmannbb333812013-06-28 19:49:40 +02001267 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1268 __func__, asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001269
Neil Horman2eebc1e2012-07-16 09:13:51 +00001270 if (asoc) {
1271 /* sctp_primitive_ASSOCIATE may have added this association
1272 * To the hash table, try to unhash it, just in case, its a noop
1273 * if it wasn't hashed so we're safe
1274 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001275 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001276 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001277 return err;
1278}
1279
1280/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1281 *
1282 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001283 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1284 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001285 *
1286 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1287 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1288 * or IPv6 addresses.
1289 *
1290 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1291 * Section 3.1.2 for this usage.
1292 *
1293 * addrs is a pointer to an array of one or more socket addresses. Each
1294 * address is contained in its appropriate structure (i.e. struct
1295 * sockaddr_in or struct sockaddr_in6) the family of the address type
1296 * must be used to distengish the address length (note that this
1297 * representation is termed a "packed array" of addresses). The caller
1298 * specifies the number of addresses in the array with addrcnt.
1299 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001300 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1301 * the association id of the new association. On failure, sctp_connectx()
1302 * returns -1, and sets errno to the appropriate error code. The assoc_id
1303 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001304 *
1305 * For SCTP, the port given in each socket address must be the same, or
1306 * sctp_connectx() will fail, setting errno to EINVAL.
1307 *
1308 * An application can use sctp_connectx to initiate an association with
1309 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1310 * allows a caller to specify multiple addresses at which a peer can be
1311 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001312 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001313 * specifies that the stack will try to make use of all the addresses in
1314 * the list when needed.
1315 *
1316 * Note that the list of addresses passed in is only used for setting up
1317 * the association. It does not necessarily equal the set of addresses
1318 * the peer uses for the resulting association. If the caller wants to
1319 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1320 * retrieve them after the association has been set up.
1321 *
1322 * Basically do nothing but copying the addresses from user to kernel
1323 * land and invoking either sctp_connectx(). This is used for tunneling
1324 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1325 *
1326 * We don't use copy_from_user() for optimization: we first do the
1327 * sanity checks (buffer size -fast- and access check-healthy
1328 * pointer); if all of those succeed, then we can alloc the memory
1329 * (expensive operation) needed to copy the data to kernel. Then we do
1330 * the copying without checking the user space area
1331 * (__copy_from_user()).
1332 *
1333 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1334 * it.
1335 *
1336 * sk The sk of the socket
1337 * addrs The pointer to the addresses in user land
1338 * addrssize Size of the addrs buffer
1339 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001340 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001341 */
wangweidong26ac8e52013-12-23 12:16:51 +08001342static int __sctp_setsockopt_connectx(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001343 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001344 int addrs_size,
1345 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001346{
Frank Filz3f7a87d2005-06-20 13:14:57 -07001347 struct sockaddr *kaddrs;
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02001348 gfp_t gfp = GFP_KERNEL;
1349 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001350
Daniel Borkmannbb333812013-06-28 19:49:40 +02001351 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1352 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001353
1354 if (unlikely(addrs_size <= 0))
1355 return -EINVAL;
1356
1357 /* Check the user passed a healthy pointer. */
1358 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1359 return -EFAULT;
1360
1361 /* Alloc space for the address array in kernel memory. */
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02001362 if (sk->sk_socket->file)
1363 gfp = GFP_USER | __GFP_NOWARN;
1364 kaddrs = kmalloc(addrs_size, gfp);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001365 if (unlikely(!kaddrs))
1366 return -ENOMEM;
1367
1368 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1369 err = -EFAULT;
1370 } else {
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001371 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001372 }
1373
1374 kfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001375
Frank Filz3f7a87d2005-06-20 13:14:57 -07001376 return err;
1377}
1378
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001379/*
1380 * This is an older interface. It's kept for backward compatibility
1381 * to the option that doesn't provide association id.
1382 */
wangweidong26ac8e52013-12-23 12:16:51 +08001383static int sctp_setsockopt_connectx_old(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001384 struct sockaddr __user *addrs,
1385 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001386{
1387 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1388}
1389
1390/*
1391 * New interface for the API. The since the API is done with a socket
1392 * option, to make it simple we feed back the association id is as a return
1393 * indication to the call. Error is always negative and association id is
1394 * always positive.
1395 */
wangweidong26ac8e52013-12-23 12:16:51 +08001396static int sctp_setsockopt_connectx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001397 struct sockaddr __user *addrs,
1398 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001399{
1400 sctp_assoc_t assoc_id = 0;
1401 int err = 0;
1402
1403 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1404
1405 if (err)
1406 return err;
1407 else
1408 return assoc_id;
1409}
1410
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001411/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001412 * New (hopefully final) interface for the API.
1413 * We use the sctp_getaddrs_old structure so that use-space library
Daniel Borkmannffd59392014-02-17 12:11:11 +01001414 * can avoid any unnecessary allocations. The only different part
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001415 * is that we store the actual length of the address buffer into the
Daniel Borkmannffd59392014-02-17 12:11:11 +01001416 * addrs_num structure member. That way we can re-use the existing
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001417 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001418 */
Daniel Borkmannffd59392014-02-17 12:11:11 +01001419#ifdef CONFIG_COMPAT
1420struct compat_sctp_getaddrs_old {
1421 sctp_assoc_t assoc_id;
1422 s32 addr_num;
1423 compat_uptr_t addrs; /* struct sockaddr * */
1424};
1425#endif
1426
wangweidong26ac8e52013-12-23 12:16:51 +08001427static int sctp_getsockopt_connectx3(struct sock *sk, int len,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001428 char __user *optval,
1429 int __user *optlen)
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001430{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001431 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001432 sctp_assoc_t assoc_id = 0;
1433 int err = 0;
1434
Daniel Borkmannffd59392014-02-17 12:11:11 +01001435#ifdef CONFIG_COMPAT
Andy Lutomirski96c0e0a2016-03-22 14:25:07 -07001436 if (in_compat_syscall()) {
Daniel Borkmannffd59392014-02-17 12:11:11 +01001437 struct compat_sctp_getaddrs_old param32;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001438
Daniel Borkmannffd59392014-02-17 12:11:11 +01001439 if (len < sizeof(param32))
1440 return -EINVAL;
1441 if (copy_from_user(&param32, optval, sizeof(param32)))
1442 return -EFAULT;
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001443
Daniel Borkmannffd59392014-02-17 12:11:11 +01001444 param.assoc_id = param32.assoc_id;
1445 param.addr_num = param32.addr_num;
1446 param.addrs = compat_ptr(param32.addrs);
1447 } else
1448#endif
1449 {
1450 if (len < sizeof(param))
1451 return -EINVAL;
1452 if (copy_from_user(&param, optval, sizeof(param)))
1453 return -EFAULT;
1454 }
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001455
Daniel Borkmannffd59392014-02-17 12:11:11 +01001456 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1457 param.addrs, param.addr_num,
1458 &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001459 if (err == 0 || err == -EINPROGRESS) {
1460 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1461 return -EFAULT;
1462 if (put_user(sizeof(assoc_id), optlen))
1463 return -EFAULT;
1464 }
1465
1466 return err;
1467}
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469/* API 3.1.4 close() - UDP Style Syntax
1470 * Applications use close() to perform graceful shutdown (as described in
1471 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1472 * by a UDP-style socket.
1473 *
1474 * The syntax is
1475 *
1476 * ret = close(int sd);
1477 *
1478 * sd - the socket descriptor of the associations to be closed.
1479 *
1480 * To gracefully shutdown a specific association represented by the
1481 * UDP-style socket, an application should use the sendmsg() call,
1482 * passing no user data, but including the appropriate flag in the
1483 * ancillary data (see Section xxxx).
1484 *
1485 * If sd in the close() call is a branched-off socket representing only
1486 * one association, the shutdown is performed on that association only.
1487 *
1488 * 4.1.6 close() - TCP Style Syntax
1489 *
1490 * Applications use close() to gracefully close down an association.
1491 *
1492 * The syntax is:
1493 *
1494 * int close(int sd);
1495 *
1496 * sd - the socket descriptor of the association to be closed.
1497 *
1498 * After an application calls close() on a socket descriptor, no further
1499 * socket operations will succeed on that descriptor.
1500 *
1501 * API 7.1.4 SO_LINGER
1502 *
1503 * An application using the TCP-style socket can use this option to
1504 * perform the SCTP ABORT primitive. The linger option structure is:
1505 *
1506 * struct linger {
1507 * int l_onoff; // option on/off
1508 * int l_linger; // linger time
1509 * };
1510 *
1511 * To enable the option, set l_onoff to 1. If the l_linger value is set
1512 * to 0, calling close() is the same as the ABORT primitive. If the
1513 * value is set to a negative value, the setsockopt() call will return
1514 * an error. If the value is set to a positive value linger_time, the
1515 * close() can be blocked for at most linger_time ms. If the graceful
1516 * shutdown phase does not finish during this period, close() will
1517 * return but the graceful shutdown phase continues in the system.
1518 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02001519static void sctp_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001521 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 struct sctp_endpoint *ep;
1523 struct sctp_association *asoc;
1524 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001525 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Daniel Borkmannbb333812013-06-28 19:49:40 +02001527 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Xin Long6dfe4b92017-06-10 14:56:56 +08001529 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 sk->sk_shutdown = SHUTDOWN_MASK;
Vlad Yasevichbec96402009-07-30 18:08:28 -04001531 sk->sk_state = SCTP_SS_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 ep = sctp_sk(sk)->ep;
1534
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001535 /* Clean up any skbs sitting on the receive queue. */
1536 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1537 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1538
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001539 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 list_for_each_safe(pos, temp, &ep->asocs) {
1541 asoc = list_entry(pos, struct sctp_association, asocs);
1542
1543 if (sctp_style(sk, TCP)) {
1544 /* A closed association can still be in the list if
1545 * it belongs to a TCP-style listening socket that is
1546 * not yet accepted. If so, free it. If not, send an
1547 * ABORT or SHUTDOWN based on the linger options.
1548 */
1549 if (sctp_state(asoc, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001551 continue;
1552 }
1553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001555 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1556 !skb_queue_empty(&asoc->ulpq.reasm) ||
1557 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001558 struct sctp_chunk *chunk;
1559
1560 chunk = sctp_make_abort_user(asoc, NULL, 0);
Xin Long068d8bd2015-12-29 17:49:25 +08001561 sctp_primitive_ABORT(net, asoc, chunk);
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001562 } else
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001563 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 /* On a TCP-style socket, block for at most linger_time if set. */
1567 if (sctp_style(sk, TCP) && timeout)
1568 sctp_wait_for_close(sk, timeout);
1569
1570 /* This will run the backlog queue. */
wangweidong048ed4b2014-01-21 15:44:11 +08001571 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 /* Supposedly, no process has access to the socket, but
1574 * the net layers still may.
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001575 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1576 * held and that should be grabbed before socket lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001578 spin_lock_bh(&net->sctp.addr_wq_lock);
Xin Long6dfe4b92017-06-10 14:56:56 +08001579 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 /* Hold the sock, since sk_common_release() will put sock_put()
1582 * and we have just a little more cleanup.
1583 */
1584 sock_hold(sk);
1585 sk_common_release(sk);
1586
wangweidong5bc1d1b2014-01-21 15:44:12 +08001587 bh_unlock_sock(sk);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001588 spin_unlock_bh(&net->sctp.addr_wq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 sock_put(sk);
1591
1592 SCTP_DBG_OBJCNT_DEC(sock);
1593}
1594
1595/* Handle EPIPE error. */
1596static int sctp_error(struct sock *sk, int flags, int err)
1597{
1598 if (err == -EPIPE)
1599 err = sock_error(sk) ? : -EPIPE;
1600 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1601 send_sig(SIGPIPE, current, 0);
1602 return err;
1603}
1604
1605/* API 3.1.3 sendmsg() - UDP Style Syntax
1606 *
1607 * An application uses sendmsg() and recvmsg() calls to transmit data to
1608 * and receive data from its peer.
1609 *
1610 * ssize_t sendmsg(int socket, const struct msghdr *message,
1611 * int flags);
1612 *
1613 * socket - the socket descriptor of the endpoint.
1614 * message - pointer to the msghdr structure which contains a single
1615 * user message and possibly some ancillary data.
1616 *
1617 * See Section 5 for complete description of the data
1618 * structures.
1619 *
1620 * flags - flags sent or received with the user message, see Section
1621 * 5 for complete description of the flags.
1622 *
1623 * Note: This function could use a rewrite especially when explicit
1624 * connect support comes in.
1625 */
1626/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1627
Xin Longa05437a2017-08-11 10:23:48 +08001628static int sctp_msghdr_parse(const struct msghdr *msg,
1629 struct sctp_cmsgs *cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Ying Xue1b784142015-03-02 15:37:48 +08001631static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001633 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 struct sctp_sock *sp;
1635 struct sctp_endpoint *ep;
wangweidongcb3f8372013-12-23 12:16:50 +08001636 struct sctp_association *new_asoc = NULL, *asoc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 struct sctp_transport *transport, *chunk_tp;
1638 struct sctp_chunk *chunk;
Al Virodce116a2006-11-20 17:25:15 -08001639 union sctp_addr to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 struct sockaddr *msg_name = NULL;
Joe Perches517aa0b2011-05-12 11:27:20 +00001641 struct sctp_sndrcvinfo default_sinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 struct sctp_sndrcvinfo *sinfo;
1643 struct sctp_initmsg *sinit;
1644 sctp_assoc_t associd = 0;
Xin Longa05437a2017-08-11 10:23:48 +08001645 struct sctp_cmsgs cmsgs = { NULL };
Xin Long1c662012017-08-05 19:59:54 +08001646 enum sctp_scope scope;
Daniel Borkmann2061dcd2015-01-15 16:34:35 +01001647 bool fill_sinfo_ttl = false, wait_connect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 struct sctp_datamsg *datamsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 int msg_flags = msg->msg_flags;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001650 __u16 sinfo_flags = 0;
1651 long timeo;
1652 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 err = 0;
1655 sp = sctp_sk(sk);
1656 ep = sp->ep;
1657
Daniel Borkmannbb333812013-06-28 19:49:40 +02001658 pr_debug("%s: sk:%p, msg:%p, msg_len:%zu ep:%p\n", __func__, sk,
1659 msg, msg_len, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 /* We cannot send a message over a TCP-style listening socket. */
1662 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1663 err = -EPIPE;
1664 goto out_nounlock;
1665 }
1666
1667 /* Parse out the SCTP CMSGs. */
1668 err = sctp_msghdr_parse(msg, &cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (err) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001670 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 goto out_nounlock;
1672 }
1673
1674 /* Fetch the destination address for this packet. This
1675 * address only selects the association--it is not necessarily
1676 * the address we will send to.
1677 * For a peeled-off socket, msg_name is ignored.
1678 */
1679 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1680 int msg_namelen = msg->msg_namelen;
1681
1682 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1683 msg_namelen);
1684 if (err)
1685 return err;
1686
1687 if (msg_namelen > sizeof(to))
1688 msg_namelen = sizeof(to);
1689 memcpy(&to, msg->msg_name, msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 msg_name = msg->msg_name;
1691 }
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 sinit = cmsgs.init;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001694 if (cmsgs.sinfo != NULL) {
1695 memset(&default_sinfo, 0, sizeof(default_sinfo));
1696 default_sinfo.sinfo_stream = cmsgs.sinfo->snd_sid;
1697 default_sinfo.sinfo_flags = cmsgs.sinfo->snd_flags;
1698 default_sinfo.sinfo_ppid = cmsgs.sinfo->snd_ppid;
1699 default_sinfo.sinfo_context = cmsgs.sinfo->snd_context;
1700 default_sinfo.sinfo_assoc_id = cmsgs.sinfo->snd_assoc_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001702 sinfo = &default_sinfo;
1703 fill_sinfo_ttl = true;
1704 } else {
1705 sinfo = cmsgs.srinfo;
1706 }
1707 /* Did the user specify SNDINFO/SNDRCVINFO? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (sinfo) {
1709 sinfo_flags = sinfo->sinfo_flags;
1710 associd = sinfo->sinfo_assoc_id;
1711 }
1712
Daniel Borkmannbb333812013-06-28 19:49:40 +02001713 pr_debug("%s: msg_len:%zu, sinfo_flags:0x%x\n", __func__,
1714 msg_len, sinfo_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001716 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1717 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 err = -EINVAL;
1719 goto out_nounlock;
1720 }
1721
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001722 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1723 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1724 * If SCTP_ABORT is set, the message length could be non zero with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 * the msg_iov set to the user abort reason.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001726 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001727 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1728 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 err = -EINVAL;
1730 goto out_nounlock;
1731 }
1732
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001733 /* If SCTP_ADDR_OVER is set, there must be an address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 * specified in msg_name.
1735 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001736 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 err = -EINVAL;
1738 goto out_nounlock;
1739 }
1740
1741 transport = NULL;
1742
Daniel Borkmannbb333812013-06-28 19:49:40 +02001743 pr_debug("%s: about to look up association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
wangweidong048ed4b2014-01-21 15:44:11 +08001745 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 /* If a msg_name has been specified, assume this is to be used. */
1748 if (msg_name) {
1749 /* Look for a matching association on the endpoint. */
Al Virodce116a2006-11-20 17:25:15 -08001750 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03001751
1752 /* If we could not find a matching association on the
1753 * endpoint, make sure that it is not a TCP-style
1754 * socket that already has an association or there is
1755 * no peeled-off association on another socket.
1756 */
1757 if (!asoc &&
1758 ((sctp_style(sk, TCP) &&
1759 (sctp_sstate(sk, ESTABLISHED) ||
1760 sctp_sstate(sk, CLOSING))) ||
1761 sctp_endpoint_is_peeled_off(ep, &to))) {
1762 err = -EADDRNOTAVAIL;
1763 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765 } else {
1766 asoc = sctp_id2assoc(sk, associd);
1767 if (!asoc) {
1768 err = -EPIPE;
1769 goto out_unlock;
1770 }
1771 }
1772
1773 if (asoc) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001774 pr_debug("%s: just looked up association:%p\n", __func__, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1777 * socket that has an association in CLOSED state. This can
1778 * happen when an accepted socket has an association that is
1779 * already CLOSED.
1780 */
1781 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1782 err = -EPIPE;
1783 goto out_unlock;
1784 }
1785
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001786 if (sinfo_flags & SCTP_EOF) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001787 pr_debug("%s: shutting down association:%p\n",
1788 __func__, asoc);
1789
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001790 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 err = 0;
1792 goto out_unlock;
1793 }
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001794 if (sinfo_flags & SCTP_ABORT) {
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001795
1796 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1797 if (!chunk) {
1798 err = -ENOMEM;
1799 goto out_unlock;
1800 }
1801
Daniel Borkmannbb333812013-06-28 19:49:40 +02001802 pr_debug("%s: aborting association:%p\n",
1803 __func__, asoc);
1804
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001805 sctp_primitive_ABORT(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 err = 0;
1807 goto out_unlock;
1808 }
1809 }
1810
1811 /* Do we need to create the association? */
1812 if (!asoc) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001813 pr_debug("%s: there is no association yet\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001815 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 err = -EINVAL;
1817 goto out_unlock;
1818 }
1819
1820 /* Check for invalid stream against the stream counts,
1821 * either the default or the user specified stream counts.
1822 */
1823 if (sinfo) {
Dan Carpenter0e864b22014-01-13 16:46:08 +03001824 if (!sinit || !sinit->sinit_num_ostreams) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 /* Check against the defaults. */
1826 if (sinfo->sinfo_stream >=
1827 sp->initmsg.sinit_num_ostreams) {
1828 err = -EINVAL;
1829 goto out_unlock;
1830 }
1831 } else {
1832 /* Check against the requested. */
1833 if (sinfo->sinfo_stream >=
1834 sinit->sinit_num_ostreams) {
1835 err = -EINVAL;
1836 goto out_unlock;
1837 }
1838 }
1839 }
1840
1841 /*
1842 * API 3.1.2 bind() - UDP Style Syntax
1843 * If a bind() or sctp_bindx() is not called prior to a
1844 * sendmsg() call that initiates a new association, the
1845 * system picks an ephemeral port and will choose an address
1846 * set equivalent to binding with a wildcard address.
1847 */
1848 if (!ep->base.bind_addr.port) {
1849 if (sctp_autobind(sk)) {
1850 err = -EAGAIN;
1851 goto out_unlock;
1852 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001853 } else {
1854 /*
1855 * If an unprivileged user inherits a one-to-many
1856 * style socket with open associations on a privileged
1857 * port, it MAY be permitted to accept new associations,
1858 * but it SHOULD NOT be permitted to open new
1859 * associations.
1860 */
Krister Johansen4548b682017-01-20 17:49:11 -08001861 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
Eric W. Biederman35946982012-11-16 03:03:12 +00001862 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) {
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001863 err = -EACCES;
1864 goto out_unlock;
1865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867
1868 scope = sctp_scope(&to);
1869 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1870 if (!new_asoc) {
1871 err = -ENOMEM;
1872 goto out_unlock;
1873 }
1874 asoc = new_asoc;
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001875 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1876 if (err < 0) {
1877 err = -ENOMEM;
1878 goto out_free;
1879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 /* If the SCTP_INIT ancillary data is specified, set all
1882 * the association init values accordingly.
1883 */
1884 if (sinit) {
1885 if (sinit->sinit_num_ostreams) {
1886 asoc->c.sinit_num_ostreams =
1887 sinit->sinit_num_ostreams;
1888 }
1889 if (sinit->sinit_max_instreams) {
1890 asoc->c.sinit_max_instreams =
1891 sinit->sinit_max_instreams;
1892 }
1893 if (sinit->sinit_max_attempts) {
1894 asoc->max_init_attempts
1895 = sinit->sinit_max_attempts;
1896 }
1897 if (sinit->sinit_max_init_timeo) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001898 asoc->max_init_timeo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1900 }
1901 }
1902
1903 /* Prime the peer's transport structures. */
Al Virodce116a2006-11-20 17:25:15 -08001904 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (!transport) {
1906 err = -ENOMEM;
1907 goto out_free;
1908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910
1911 /* ASSERT: we have a valid association at this point. */
Daniel Borkmannbb333812013-06-28 19:49:40 +02001912 pr_debug("%s: we have a valid association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 if (!sinfo) {
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001915 /* If the user didn't specify SNDINFO/SNDRCVINFO, make up
1916 * one with some defaults.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 */
Joe Perches517aa0b2011-05-12 11:27:20 +00001918 memset(&default_sinfo, 0, sizeof(default_sinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 default_sinfo.sinfo_stream = asoc->default_stream;
1920 default_sinfo.sinfo_flags = asoc->default_flags;
1921 default_sinfo.sinfo_ppid = asoc->default_ppid;
1922 default_sinfo.sinfo_context = asoc->default_context;
1923 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1924 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 sinfo = &default_sinfo;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001927 } else if (fill_sinfo_ttl) {
1928 /* In case SNDINFO was specified, we still need to fill
1929 * it with a default ttl from the assoc here.
1930 */
1931 sinfo->sinfo_timetolive = asoc->default_timetolive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933
1934 /* API 7.1.7, the sndbuf size per association bounds the
1935 * maximum size of data that can be sent in a single send call.
1936 */
1937 if (msg_len > sk->sk_sndbuf) {
1938 err = -EMSGSIZE;
1939 goto out_free;
1940 }
1941
Vlad Yasevich8a479492007-06-07 14:21:05 -04001942 if (asoc->pmtu_pending)
Xin Long3ebfdf02017-04-04 13:39:55 +08001943 sctp_assoc_pending_pmtu(asoc);
Vlad Yasevich8a479492007-06-07 14:21:05 -04001944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 /* If fragmentation is disabled and the message length exceeds the
1946 * association fragmentation point, return EMSGSIZE. The I-D
1947 * does not specify what this error is, but this looks like
1948 * a great fit.
1949 */
1950 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1951 err = -EMSGSIZE;
1952 goto out_free;
1953 }
1954
Joe Perchesafd76142011-05-12 09:19:10 +00001955 /* Check for invalid stream. */
Xin Longcee360a2017-05-31 16:36:31 +08001956 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
Joe Perchesafd76142011-05-12 09:19:10 +00001957 err = -EINVAL;
1958 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
1960
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03001961 /* Allocate sctp_stream_out_ext if not already done */
1962 if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) {
1963 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1964 if (err)
1965 goto out_free;
1966 }
1967
Xin Long8dbdf1f2016-07-09 19:47:45 +08001968 if (sctp_wspace(asoc) < msg_len)
1969 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1972 if (!sctp_wspace(asoc)) {
Xin Longcea0cc82017-11-15 16:57:26 +08001973 /* sk can be changed by peel off when waiting for buf. */
1974 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len, &sk);
Xin Longca3af4d2017-11-15 16:55:54 +08001975 if (err) {
1976 if (err == -ESRCH) {
1977 /* asoc is already dead. */
1978 new_asoc = NULL;
1979 err = -EPIPE;
1980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 goto out_free;
Xin Longca3af4d2017-11-15 16:55:54 +08001982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984
1985 /* If an address is passed with the sendto/sendmsg call, it is used
1986 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001987 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 */
1989 if ((sctp_style(sk, TCP) && msg_name) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001990 (sinfo_flags & SCTP_ADDR_OVER)) {
Al Virodce116a2006-11-20 17:25:15 -08001991 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (!chunk_tp) {
1993 err = -EINVAL;
1994 goto out_free;
1995 }
1996 } else
1997 chunk_tp = NULL;
1998
1999 /* Auto-connect, if we aren't connected already. */
2000 if (sctp_state(asoc, CLOSED)) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002001 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (err < 0)
2003 goto out_free;
Daniel Borkmannbb333812013-06-28 19:49:40 +02002004
Daniel Borkmann2061dcd2015-01-15 16:34:35 +01002005 wait_connect = true;
Daniel Borkmannbb333812013-06-28 19:49:40 +02002006 pr_debug("%s: we associated primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
2008
2009 /* Break the message into multiple chunks of maximum size. */
Al Viroc0371da2014-11-24 10:42:55 -05002010 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
Tommi Rantala6e51fe72012-11-22 03:23:16 +00002011 if (IS_ERR(datamsg)) {
2012 err = PTR_ERR(datamsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 goto out_free;
2014 }
Xin Longf9ba3502017-03-27 00:21:15 +08002015 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 /* Now send the (possibly) fragmented message. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002018 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
Xin Longb61c6542016-09-14 02:04:20 +08002019 sctp_chunk_hold(chunk);
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 /* Do accounting for the write space. */
2022 sctp_set_owner_w(chunk);
2023
2024 chunk->transport = chunk_tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
2026
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04002027 /* Send it to the lower layers. Note: all chunks
2028 * must either fail or succeed. The lower layer
2029 * works that way today. Keep it that way or this
2030 * breaks.
2031 */
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002032 err = sctp_primitive_SEND(net, asoc, datamsg);
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04002033 /* Did the lower layer accept the chunk? */
Xin Longb61c6542016-09-14 02:04:20 +08002034 if (err) {
2035 sctp_datamsg_free(datamsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 goto out_free;
Xin Longb61c6542016-09-14 02:04:20 +08002037 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02002038
2039 pr_debug("%s: we sent primitively\n", __func__);
2040
Xin Longb61c6542016-09-14 02:04:20 +08002041 sctp_datamsg_put(datamsg);
Daniel Borkmannbb333812013-06-28 19:49:40 +02002042 err = msg_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Daniel Borkmann2061dcd2015-01-15 16:34:35 +01002044 if (unlikely(wait_connect)) {
2045 timeo = sock_sndtimeo(sk, msg_flags & MSG_DONTWAIT);
2046 sctp_wait_for_connect(asoc, &timeo);
2047 }
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 /* If we are already past ASSOCIATE, the lower
2050 * layers are responsible for association cleanup.
2051 */
2052 goto out_unlock;
2053
2054out_free:
Xin Longb5eff712015-12-30 23:50:49 +08002055 if (new_asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 sctp_association_free(asoc);
2057out_unlock:
wangweidong048ed4b2014-01-21 15:44:11 +08002058 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060out_nounlock:
2061 return sctp_error(sk, msg_flags, err);
2062
2063#if 0
2064do_sock_err:
2065 if (msg_len)
2066 err = msg_len;
2067 else
2068 err = sock_error(sk);
2069 goto out;
2070
2071do_interrupted:
2072 if (msg_len)
2073 err = msg_len;
2074 goto out;
2075#endif /* 0 */
2076}
2077
2078/* This is an extended version of skb_pull() that removes the data from the
2079 * start of a skb even when data is spread across the list of skb's in the
2080 * frag_list. len specifies the total amount of data that needs to be removed.
2081 * when 'len' bytes could be removed from the skb, it returns 0.
2082 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2083 * could not be removed.
2084 */
2085static int sctp_skb_pull(struct sk_buff *skb, int len)
2086{
2087 struct sk_buff *list;
2088 int skb_len = skb_headlen(skb);
2089 int rlen;
2090
2091 if (len <= skb_len) {
2092 __skb_pull(skb, len);
2093 return 0;
2094 }
2095 len -= skb_len;
2096 __skb_pull(skb, skb_len);
2097
David S. Miller1b003be2009-06-09 00:22:35 -07002098 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 rlen = sctp_skb_pull(list, len);
2100 skb->len -= (len-rlen);
2101 skb->data_len -= (len-rlen);
2102
2103 if (!rlen)
2104 return 0;
2105
2106 len = rlen;
2107 }
2108
2109 return len;
2110}
2111
2112/* API 3.1.3 recvmsg() - UDP Style Syntax
2113 *
2114 * ssize_t recvmsg(int socket, struct msghdr *message,
2115 * int flags);
2116 *
2117 * socket - the socket descriptor of the endpoint.
2118 * message - pointer to the msghdr structure which contains a single
2119 * user message and possibly some ancillary data.
2120 *
2121 * See Section 5 for complete description of the data
2122 * structures.
2123 *
2124 * flags - flags sent or received with the user message, see Section
2125 * 5 for complete description of the flags.
2126 */
Ying Xue1b784142015-03-02 15:37:48 +08002127static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2128 int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 struct sctp_ulpevent *event = NULL;
2131 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002132 struct sk_buff *skb, *head_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 int copied;
2134 int err = 0;
2135 int skb_len;
2136
Daniel Borkmannbb333812013-06-28 19:49:40 +02002137 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2138 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2139 addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
wangweidong048ed4b2014-01-21 15:44:11 +08002141 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03002143 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
Xin Longe0878692016-07-30 14:14:41 +08002144 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 err = -ENOTCONN;
2146 goto out;
2147 }
2148
2149 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2150 if (!skb)
2151 goto out;
2152
2153 /* Get the total length of the skb including any skb's in the
2154 * frag_list.
2155 */
2156 skb_len = skb->len;
2157
2158 copied = skb_len;
2159 if (copied > len)
2160 copied = len;
2161
David S. Miller51f3d022014-11-05 16:46:40 -05002162 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 event = sctp_skb2event(skb);
2165
2166 if (err)
2167 goto out_free;
2168
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002169 if (event->chunk && event->chunk->head_skb)
2170 head_skb = event->chunk->head_skb;
2171 else
2172 head_skb = skb;
2173 sock_recv_ts_and_drops(msg, sk, head_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (sctp_ulpevent_is_notification(event)) {
2175 msg->msg_flags |= MSG_NOTIFICATION;
2176 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2177 } else {
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002178 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
2180
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02002181 /* Check if we allow SCTP_NXTINFO. */
2182 if (sp->recvnxtinfo)
2183 sctp_ulpevent_read_nxtinfo(event, msg, sk);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002184 /* Check if we allow SCTP_RCVINFO. */
2185 if (sp->recvrcvinfo)
2186 sctp_ulpevent_read_rcvinfo(event, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 /* Check if we allow SCTP_SNDRCVINFO. */
2188 if (sp->subscribe.sctp_data_io_event)
2189 sctp_ulpevent_read_sndrcvinfo(event, msg);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 err = copied;
2192
2193 /* If skb's length exceeds the user's buffer, update the skb and
2194 * push it back to the receive_queue so that the next call to
2195 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2196 */
2197 if (skb_len > copied) {
2198 msg->msg_flags &= ~MSG_EOR;
2199 if (flags & MSG_PEEK)
2200 goto out_free;
2201 sctp_skb_pull(skb, copied);
2202 skb_queue_head(&sk->sk_receive_queue, skb);
2203
Daniel Borkmann362d5202014-04-14 21:45:17 +02002204 /* When only partial message is copied to the user, increase
2205 * rwnd by that amount. If all the data in the skb is read,
2206 * rwnd is updated when the event is freed.
2207 */
2208 if (!sctp_ulpevent_is_notification(event))
2209 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 goto out;
2211 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2212 (event->msg_flags & MSG_EOR))
2213 msg->msg_flags |= MSG_EOR;
2214 else
2215 msg->msg_flags &= ~MSG_EOR;
2216
2217out_free:
2218 if (flags & MSG_PEEK) {
2219 /* Release the skb reference acquired after peeking the skb in
2220 * sctp_skb_recv_datagram().
2221 */
2222 kfree_skb(skb);
2223 } else {
2224 /* Free the event which includes releasing the reference to
2225 * the owner of the skb, freeing the skb and updating the
2226 * rwnd.
2227 */
2228 sctp_ulpevent_free(event);
2229 }
2230out:
wangweidong048ed4b2014-01-21 15:44:11 +08002231 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return err;
2233}
2234
2235/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2236 *
2237 * This option is a on/off flag. If enabled no SCTP message
2238 * fragmentation will be performed. Instead if a message being sent
2239 * exceeds the current PMTU size, the message will NOT be sent and
2240 * instead a error will be indicated to the user.
2241 */
2242static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002243 char __user *optval,
2244 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
2246 int val;
2247
2248 if (optlen < sizeof(int))
2249 return -EINVAL;
2250
2251 if (get_user(val, (int __user *)optval))
2252 return -EFAULT;
2253
2254 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2255
2256 return 0;
2257}
2258
2259static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002260 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Wei Yongjun94912302011-07-02 09:28:04 +00002262 struct sctp_association *asoc;
2263 struct sctp_ulpevent *event;
2264
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002265 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return -EINVAL;
2267 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2268 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002269
Daniel Borkmannbbbea412014-07-12 20:30:40 +02002270 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
Wei Yongjun94912302011-07-02 09:28:04 +00002271 * if there is no data to be sent or retransmit, the stack will
2272 * immediately send up this notification.
2273 */
2274 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2275 &sctp_sk(sk)->subscribe)) {
2276 asoc = sctp_id2assoc(sk, 0);
2277
2278 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2279 event = sctp_ulpevent_make_sender_dry_event(asoc,
2280 GFP_ATOMIC);
2281 if (!event)
2282 return -ENOMEM;
2283
2284 sctp_ulpq_tail_event(&asoc->ulpq, event);
2285 }
2286 }
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return 0;
2289}
2290
2291/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2292 *
2293 * This socket option is applicable to the UDP-style socket only. When
2294 * set it will cause associations that are idle for more than the
2295 * specified number of seconds to automatically close. An association
2296 * being idle is defined an association that has NOT sent or received
2297 * user data. The special value of '0' indicates that no automatic
2298 * close of any associations should be performed. The option expects an
2299 * integer defining the number of seconds of idle time before an
2300 * association is closed.
2301 */
2302static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002303 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
2305 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman9f70f462013-12-10 06:48:15 -05002306 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 /* Applicable to UDP-style socket only */
2309 if (sctp_style(sk, TCP))
2310 return -EOPNOTSUPP;
2311 if (optlen != sizeof(int))
2312 return -EINVAL;
2313 if (copy_from_user(&sp->autoclose, optval, optlen))
2314 return -EFAULT;
2315
Neil Horman9f70f462013-12-10 06:48:15 -05002316 if (sp->autoclose > net->sctp.max_autoclose)
2317 sp->autoclose = net->sctp.max_autoclose;
2318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return 0;
2320}
2321
2322/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2323 *
2324 * Applications can enable or disable heartbeats for any peer address of
2325 * an association, modify an address's heartbeat interval, force a
2326 * heartbeat to be sent immediately, and adjust the address's maximum
2327 * number of retransmissions sent before an address is considered
2328 * unreachable. The following structure is used to access and modify an
2329 * address's parameters:
2330 *
2331 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002332 * sctp_assoc_t spp_assoc_id;
2333 * struct sockaddr_storage spp_address;
2334 * uint32_t spp_hbinterval;
2335 * uint16_t spp_pathmaxrxt;
2336 * uint32_t spp_pathmtu;
2337 * uint32_t spp_sackdelay;
2338 * uint32_t spp_flags;
2339 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002341 * spp_assoc_id - (one-to-many style socket) This is filled in the
2342 * application, and identifies the association for
2343 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 * spp_address - This specifies which address is of interest.
2345 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002346 * in milliseconds. If a value of zero
2347 * is present in this field then no changes are to
2348 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 * spp_pathmaxrxt - This contains the maximum number of
2350 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002351 * considered unreachable. If a value of zero
2352 * is present in this field then no changes are to
2353 * be made to this parameter.
2354 * spp_pathmtu - When Path MTU discovery is disabled the value
2355 * specified here will be the "fixed" path mtu.
2356 * Note that if the spp_address field is empty
2357 * then all associations on this address will
2358 * have this fixed path mtu set upon them.
2359 *
2360 * spp_sackdelay - When delayed sack is enabled, this value specifies
2361 * the number of milliseconds that sacks will be delayed
2362 * for. This value will apply to all addresses of an
2363 * association if the spp_address field is empty. Note
2364 * also, that if delayed sack is enabled and this
2365 * value is set to 0, no change is made to the last
2366 * recorded delayed sack timer value.
2367 *
2368 * spp_flags - These flags are used to control various features
2369 * on an association. The flag field may contain
2370 * zero or more of the following options.
2371 *
2372 * SPP_HB_ENABLE - Enable heartbeats on the
2373 * specified address. Note that if the address
2374 * field is empty all addresses for the association
2375 * have heartbeats enabled upon them.
2376 *
2377 * SPP_HB_DISABLE - Disable heartbeats on the
2378 * speicifed address. Note that if the address
2379 * field is empty all addresses for the association
2380 * will have their heartbeats disabled. Note also
2381 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2382 * mutually exclusive, only one of these two should
2383 * be specified. Enabling both fields will have
2384 * undetermined results.
2385 *
2386 * SPP_HB_DEMAND - Request a user initiated heartbeat
2387 * to be made immediately.
2388 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002389 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2390 * heartbeat delayis to be set to the value of 0
2391 * milliseconds.
2392 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002393 * SPP_PMTUD_ENABLE - This field will enable PMTU
2394 * discovery upon the specified address. Note that
2395 * if the address feild is empty then all addresses
2396 * on the association are effected.
2397 *
2398 * SPP_PMTUD_DISABLE - This field will disable PMTU
2399 * discovery upon the specified address. Note that
2400 * if the address feild is empty then all addresses
2401 * on the association are effected. Not also that
2402 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2403 * exclusive. Enabling both will have undetermined
2404 * results.
2405 *
2406 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2407 * on delayed sack. The time specified in spp_sackdelay
2408 * is used to specify the sack delay for this address. Note
2409 * that if spp_address is empty then all addresses will
2410 * enable delayed sack and take on the sack delay
2411 * value specified in spp_sackdelay.
2412 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2413 * off delayed sack. If the spp_address field is blank then
2414 * delayed sack is disabled for the entire association. Note
2415 * also that this field is mutually exclusive to
2416 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2417 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 */
Adrian Bunk16164362006-09-18 00:40:38 -07002419static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2420 struct sctp_transport *trans,
2421 struct sctp_association *asoc,
2422 struct sctp_sock *sp,
2423 int hb_change,
2424 int pmtud_change,
2425 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 int error;
2428
Frank Filz52ccb8e2005-12-22 11:36:46 -08002429 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002430 struct net *net = sock_net(trans->asoc->base.sk);
2431
2432 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (error)
2434 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 }
2436
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002437 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2438 * this field is ignored. Note also that a value of zero indicates
2439 * the current setting should be left unchanged.
2440 */
2441 if (params->spp_flags & SPP_HB_ENABLE) {
2442
2443 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2444 * set. This lets us use 0 value when this flag
2445 * is set.
2446 */
2447 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2448 params->spp_hbinterval = 0;
2449
2450 if (params->spp_hbinterval ||
2451 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2452 if (trans) {
2453 trans->hbinterval =
2454 msecs_to_jiffies(params->spp_hbinterval);
2455 } else if (asoc) {
2456 asoc->hbinterval =
2457 msecs_to_jiffies(params->spp_hbinterval);
2458 } else {
2459 sp->hbinterval = params->spp_hbinterval;
2460 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002461 }
2462 }
2463
2464 if (hb_change) {
2465 if (trans) {
2466 trans->param_flags =
2467 (trans->param_flags & ~SPP_HB) | hb_change;
2468 } else if (asoc) {
2469 asoc->param_flags =
2470 (asoc->param_flags & ~SPP_HB) | hb_change;
2471 } else {
2472 sp->param_flags =
2473 (sp->param_flags & ~SPP_HB) | hb_change;
2474 }
2475 }
2476
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002477 /* When Path MTU discovery is disabled the value specified here will
2478 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2479 * include the flag SPP_PMTUD_DISABLE for this field to have any
2480 * effect).
2481 */
2482 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002483 if (trans) {
2484 trans->pathmtu = params->spp_pathmtu;
Xin Long3ebfdf02017-04-04 13:39:55 +08002485 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002486 } else if (asoc) {
2487 asoc->pathmtu = params->spp_pathmtu;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002488 } else {
2489 sp->pathmtu = params->spp_pathmtu;
2490 }
2491 }
2492
2493 if (pmtud_change) {
2494 if (trans) {
2495 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2496 (params->spp_flags & SPP_PMTUD_ENABLE);
2497 trans->param_flags =
2498 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2499 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002500 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
Xin Long3ebfdf02017-04-04 13:39:55 +08002501 sctp_assoc_sync_pmtu(asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002502 }
2503 } else if (asoc) {
2504 asoc->param_flags =
2505 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2506 } else {
2507 sp->param_flags =
2508 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2509 }
2510 }
2511
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002512 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2513 * value of this field is ignored. Note also that a value of zero
2514 * indicates the current setting should be left unchanged.
2515 */
2516 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002517 if (trans) {
2518 trans->sackdelay =
2519 msecs_to_jiffies(params->spp_sackdelay);
2520 } else if (asoc) {
2521 asoc->sackdelay =
2522 msecs_to_jiffies(params->spp_sackdelay);
2523 } else {
2524 sp->sackdelay = params->spp_sackdelay;
2525 }
2526 }
2527
2528 if (sackdelay_change) {
2529 if (trans) {
2530 trans->param_flags =
2531 (trans->param_flags & ~SPP_SACKDELAY) |
2532 sackdelay_change;
2533 } else if (asoc) {
2534 asoc->param_flags =
2535 (asoc->param_flags & ~SPP_SACKDELAY) |
2536 sackdelay_change;
2537 } else {
2538 sp->param_flags =
2539 (sp->param_flags & ~SPP_SACKDELAY) |
2540 sackdelay_change;
2541 }
2542 }
2543
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002544 /* Note that a value of zero indicates the current setting should be
2545 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002546 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002547 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002548 if (trans) {
2549 trans->pathmaxrxt = params->spp_pathmaxrxt;
2550 } else if (asoc) {
2551 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2552 } else {
2553 sp->pathmaxrxt = params->spp_pathmaxrxt;
2554 }
2555 }
2556
2557 return 0;
2558}
2559
2560static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002561 char __user *optval,
2562 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002563{
2564 struct sctp_paddrparams params;
2565 struct sctp_transport *trans = NULL;
2566 struct sctp_association *asoc = NULL;
2567 struct sctp_sock *sp = sctp_sk(sk);
2568 int error;
2569 int hb_change, pmtud_change, sackdelay_change;
2570
2571 if (optlen != sizeof(struct sctp_paddrparams))
wangweidongcb3f8372013-12-23 12:16:50 +08002572 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002573
2574 if (copy_from_user(&params, optval, optlen))
2575 return -EFAULT;
2576
2577 /* Validate flags and value parameters. */
2578 hb_change = params.spp_flags & SPP_HB;
2579 pmtud_change = params.spp_flags & SPP_PMTUD;
2580 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2581
2582 if (hb_change == SPP_HB ||
2583 pmtud_change == SPP_PMTUD ||
2584 sackdelay_change == SPP_SACKDELAY ||
2585 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002586 (params.spp_pathmtu &&
2587 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002588 return -EINVAL;
2589
2590 /* If an address other than INADDR_ANY is specified, and
2591 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 */
wangweidongcb3f8372013-12-23 12:16:50 +08002593 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002594 trans = sctp_addr_id2transport(sk, &params.spp_address,
2595 params.spp_assoc_id);
2596 if (!trans)
2597 return -EINVAL;
2598 }
2599
2600 /* Get association, if assoc_id != 0 and the socket is a one
2601 * to many style socket, and an association was not found, then
2602 * the id was invalid.
2603 */
2604 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2605 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2606 return -EINVAL;
2607
2608 /* Heartbeat demand can only be sent on a transport or
2609 * association, but not a socket.
2610 */
2611 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2612 return -EINVAL;
2613
2614 /* Process parameters. */
2615 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2616 hb_change, pmtud_change,
2617 sackdelay_change);
2618
2619 if (error)
2620 return error;
2621
2622 /* If changes are for association, also apply parameters to each
2623 * transport.
2624 */
2625 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002626 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2627 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002628 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2629 hb_change, pmtud_change,
2630 sackdelay_change);
2631 }
2632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 return 0;
2635}
2636
wangweidong0ea5e4d2014-01-15 17:24:01 +08002637static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2638{
2639 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2640}
2641
2642static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2643{
2644 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2645}
2646
Wei Yongjund364d922008-05-09 15:13:26 -07002647/*
2648 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002649 *
Wei Yongjund364d922008-05-09 15:13:26 -07002650 * This option will effect the way delayed acks are performed. This
2651 * option allows you to get or set the delayed ack time, in
2652 * milliseconds. It also allows changing the delayed ack frequency.
2653 * Changing the frequency to 1 disables the delayed sack algorithm. If
2654 * the assoc_id is 0, then this sets or gets the endpoints default
2655 * values. If the assoc_id field is non-zero, then the set or get
2656 * effects the specified association for the one to many model (the
2657 * assoc_id field is ignored by the one to one model). Note that if
2658 * sack_delay or sack_freq are 0 when setting this option, then the
2659 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002660 *
Wei Yongjund364d922008-05-09 15:13:26 -07002661 * struct sctp_sack_info {
2662 * sctp_assoc_t sack_assoc_id;
2663 * uint32_t sack_delay;
2664 * uint32_t sack_freq;
2665 * };
Frank Filz77086102005-12-22 11:37:30 -08002666 *
Wei Yongjund364d922008-05-09 15:13:26 -07002667 * sack_assoc_id - This parameter, indicates which association the user
2668 * is performing an action upon. Note that if this field's value is
2669 * zero then the endpoints default value is changed (effecting future
2670 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002671 *
Wei Yongjund364d922008-05-09 15:13:26 -07002672 * sack_delay - This parameter contains the number of milliseconds that
2673 * the user is requesting the delayed ACK timer be set to. Note that
2674 * this value is defined in the standard to be between 200 and 500
2675 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002676 *
Wei Yongjund364d922008-05-09 15:13:26 -07002677 * sack_freq - This parameter contains the number of packets that must
2678 * be received before a sack is sent without waiting for the delay
2679 * timer to expire. The default value for this is 2, setting this
2680 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002681 */
2682
Wei Yongjund364d922008-05-09 15:13:26 -07002683static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002684 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002685{
Wei Yongjund364d922008-05-09 15:13:26 -07002686 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002687 struct sctp_transport *trans = NULL;
2688 struct sctp_association *asoc = NULL;
2689 struct sctp_sock *sp = sctp_sk(sk);
2690
Wei Yongjund364d922008-05-09 15:13:26 -07002691 if (optlen == sizeof(struct sctp_sack_info)) {
2692 if (copy_from_user(&params, optval, optlen))
2693 return -EFAULT;
2694
2695 if (params.sack_delay == 0 && params.sack_freq == 0)
2696 return 0;
2697 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05002698 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05002699 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05002700 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05002701 "Use struct sctp_sack_info instead\n",
2702 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07002703 if (copy_from_user(&params, optval, optlen))
2704 return -EFAULT;
2705
2706 if (params.sack_delay == 0)
2707 params.sack_freq = 1;
2708 else
2709 params.sack_freq = 0;
2710 } else
wangweidongcb3f8372013-12-23 12:16:50 +08002711 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08002712
Frank Filz77086102005-12-22 11:37:30 -08002713 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002714 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002715 return -EINVAL;
2716
Wei Yongjund364d922008-05-09 15:13:26 -07002717 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002718 * to many style socket, and an association was not found, then
2719 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002720 */
Wei Yongjund364d922008-05-09 15:13:26 -07002721 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2722 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002723 return -EINVAL;
2724
Wei Yongjund364d922008-05-09 15:13:26 -07002725 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002726 if (asoc) {
2727 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002728 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002729 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002730 sctp_spp_sackdelay_enable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002731 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002732 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002733 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002734 sctp_spp_sackdelay_enable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002735 }
Wei Yongjund364d922008-05-09 15:13:26 -07002736 }
2737
2738 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002739 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002740 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002741 sctp_spp_sackdelay_disable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002742 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002743 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002744 sctp_spp_sackdelay_disable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002745 }
Wei Yongjund364d922008-05-09 15:13:26 -07002746 } else if (params.sack_freq > 1) {
2747 if (asoc) {
2748 asoc->sackfreq = params.sack_freq;
2749 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002750 sctp_spp_sackdelay_enable(asoc->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002751 } else {
2752 sp->sackfreq = params.sack_freq;
2753 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002754 sctp_spp_sackdelay_enable(sp->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002755 }
Frank Filz77086102005-12-22 11:37:30 -08002756 }
2757
2758 /* If change is for association, also apply to each transport. */
2759 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002760 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2761 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002762 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002763 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002764 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002765 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002766 sctp_spp_sackdelay_enable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002767 }
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07002768 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002769 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002770 sctp_spp_sackdelay_disable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002771 } else if (params.sack_freq > 1) {
2772 trans->sackfreq = params.sack_freq;
2773 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002774 sctp_spp_sackdelay_enable(trans->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002775 }
2776 }
2777 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002778
Frank Filz77086102005-12-22 11:37:30 -08002779 return 0;
2780}
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2783 *
2784 * Applications can specify protocol parameters for the default association
2785 * initialization. The option name argument to setsockopt() and getsockopt()
2786 * is SCTP_INITMSG.
2787 *
2788 * Setting initialization parameters is effective only on an unconnected
2789 * socket (for UDP-style sockets only future associations are effected
2790 * by the change). With TCP-style sockets, this option is inherited by
2791 * sockets derived from a listener socket.
2792 */
David S. Millerb7058842009-09-30 16:12:20 -07002793static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
2795 struct sctp_initmsg sinit;
2796 struct sctp_sock *sp = sctp_sk(sk);
2797
2798 if (optlen != sizeof(struct sctp_initmsg))
2799 return -EINVAL;
2800 if (copy_from_user(&sinit, optval, optlen))
2801 return -EFAULT;
2802
2803 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002804 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002806 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002808 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002810 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 return 0;
2813}
2814
2815/*
2816 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2817 *
2818 * Applications that wish to use the sendto() system call may wish to
2819 * specify a default set of parameters that would normally be supplied
2820 * through the inclusion of ancillary data. This socket option allows
2821 * such an application to set the default sctp_sndrcvinfo structure.
2822 * The application that wishes to use this socket option simply passes
2823 * in to this call the sctp_sndrcvinfo structure defined in Section
2824 * 5.2.2) The input parameters accepted by this call include
2825 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2826 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2827 * to this call if the caller is using the UDP model.
2828 */
2829static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002830 char __user *optval,
2831 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002834 struct sctp_association *asoc;
2835 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002837 if (optlen != sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 return -EINVAL;
2839 if (copy_from_user(&info, optval, optlen))
2840 return -EFAULT;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002841 if (info.sinfo_flags &
2842 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2843 SCTP_ABORT | SCTP_EOF))
2844 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2847 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2848 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 if (asoc) {
2850 asoc->default_stream = info.sinfo_stream;
2851 asoc->default_flags = info.sinfo_flags;
2852 asoc->default_ppid = info.sinfo_ppid;
2853 asoc->default_context = info.sinfo_context;
2854 asoc->default_timetolive = info.sinfo_timetolive;
2855 } else {
2856 sp->default_stream = info.sinfo_stream;
2857 sp->default_flags = info.sinfo_flags;
2858 sp->default_ppid = info.sinfo_ppid;
2859 sp->default_context = info.sinfo_context;
2860 sp->default_timetolive = info.sinfo_timetolive;
2861 }
2862
2863 return 0;
2864}
2865
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002866/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2867 * (SCTP_DEFAULT_SNDINFO)
2868 */
2869static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2870 char __user *optval,
2871 unsigned int optlen)
2872{
2873 struct sctp_sock *sp = sctp_sk(sk);
2874 struct sctp_association *asoc;
2875 struct sctp_sndinfo info;
2876
2877 if (optlen != sizeof(info))
2878 return -EINVAL;
2879 if (copy_from_user(&info, optval, optlen))
2880 return -EFAULT;
2881 if (info.snd_flags &
2882 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2883 SCTP_ABORT | SCTP_EOF))
2884 return -EINVAL;
2885
2886 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
2887 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
2888 return -EINVAL;
2889 if (asoc) {
2890 asoc->default_stream = info.snd_sid;
2891 asoc->default_flags = info.snd_flags;
2892 asoc->default_ppid = info.snd_ppid;
2893 asoc->default_context = info.snd_context;
2894 } else {
2895 sp->default_stream = info.snd_sid;
2896 sp->default_flags = info.snd_flags;
2897 sp->default_ppid = info.snd_ppid;
2898 sp->default_context = info.snd_context;
2899 }
2900
2901 return 0;
2902}
2903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2905 *
2906 * Requests that the local SCTP stack use the enclosed peer address as
2907 * the association primary. The enclosed address must be one of the
2908 * association peer's addresses.
2909 */
2910static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002911 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
2913 struct sctp_prim prim;
2914 struct sctp_transport *trans;
2915
2916 if (optlen != sizeof(struct sctp_prim))
2917 return -EINVAL;
2918
2919 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2920 return -EFAULT;
2921
2922 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2923 if (!trans)
2924 return -EINVAL;
2925
2926 sctp_assoc_set_primary(trans->asoc, trans);
2927
2928 return 0;
2929}
2930
2931/*
2932 * 7.1.5 SCTP_NODELAY
2933 *
2934 * Turn on/off any Nagle-like algorithm. This means that packets are
2935 * generally sent as soon as possible and no unnecessary delays are
2936 * introduced, at the cost of more packets in the network. Expects an
2937 * integer boolean flag.
2938 */
2939static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002940 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
2942 int val;
2943
2944 if (optlen < sizeof(int))
2945 return -EINVAL;
2946 if (get_user(val, (int __user *)optval))
2947 return -EFAULT;
2948
2949 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2950 return 0;
2951}
2952
2953/*
2954 *
2955 * 7.1.1 SCTP_RTOINFO
2956 *
2957 * The protocol parameters used to initialize and bound retransmission
2958 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2959 * and modify these parameters.
2960 * All parameters are time values, in milliseconds. A value of 0, when
2961 * modifying the parameters, indicates that the current value should not
2962 * be changed.
2963 *
2964 */
David S. Millerb7058842009-09-30 16:12:20 -07002965static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2966{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 struct sctp_rtoinfo rtoinfo;
2968 struct sctp_association *asoc;
wangweidong85f935d2013-12-11 09:50:38 +08002969 unsigned long rto_min, rto_max;
2970 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
2972 if (optlen != sizeof (struct sctp_rtoinfo))
2973 return -EINVAL;
2974
2975 if (copy_from_user(&rtoinfo, optval, optlen))
2976 return -EFAULT;
2977
2978 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2979
2980 /* Set the values to the specific association */
2981 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2982 return -EINVAL;
2983
wangweidong85f935d2013-12-11 09:50:38 +08002984 rto_max = rtoinfo.srto_max;
2985 rto_min = rtoinfo.srto_min;
2986
2987 if (rto_max)
2988 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
2989 else
2990 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
2991
2992 if (rto_min)
2993 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
2994 else
2995 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
2996
2997 if (rto_min > rto_max)
2998 return -EINVAL;
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (asoc) {
3001 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003002 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 msecs_to_jiffies(rtoinfo.srto_initial);
wangweidong85f935d2013-12-11 09:50:38 +08003004 asoc->rto_max = rto_max;
3005 asoc->rto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 } else {
3007 /* If there is no association or the association-id = 0
3008 * set the values to the endpoint.
3009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 if (rtoinfo.srto_initial != 0)
3011 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
wangweidong85f935d2013-12-11 09:50:38 +08003012 sp->rtoinfo.srto_max = rto_max;
3013 sp->rtoinfo.srto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 }
3015
3016 return 0;
3017}
3018
3019/*
3020 *
3021 * 7.1.2 SCTP_ASSOCINFO
3022 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02003023 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 * of the association.
3025 * Returns an error if the new association retransmission value is
3026 * greater than the sum of the retransmission value of the peer.
3027 * See [SCTP] for more information.
3028 *
3029 */
David S. Millerb7058842009-09-30 16:12:20 -07003030static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
3032
3033 struct sctp_assocparams assocparams;
3034 struct sctp_association *asoc;
3035
3036 if (optlen != sizeof(struct sctp_assocparams))
3037 return -EINVAL;
3038 if (copy_from_user(&assocparams, optval, optlen))
3039 return -EFAULT;
3040
3041 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3042
3043 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3044 return -EINVAL;
3045
3046 /* Set the values to the specific association */
3047 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003048 if (assocparams.sasoc_asocmaxrxt != 0) {
3049 __u32 path_sum = 0;
3050 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003051 struct sctp_transport *peer_addr;
3052
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07003053 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3054 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003055 path_sum += peer_addr->pathmaxrxt;
3056 paths++;
3057 }
3058
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003059 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003060 * one path/transport. We do this because path
3061 * retransmissions are only counted when we have more
3062 * then one path.
3063 */
3064 if (paths > 1 &&
3065 assocparams.sasoc_asocmaxrxt > path_sum)
3066 return -EINVAL;
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003069 }
3070
Daniel Borkmann52db8822013-06-25 18:17:27 +02003071 if (assocparams.sasoc_cookie_life != 0)
3072 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 } else {
3074 /* Set the values to the endpoint */
3075 struct sctp_sock *sp = sctp_sk(sk);
3076
3077 if (assocparams.sasoc_asocmaxrxt != 0)
3078 sp->assocparams.sasoc_asocmaxrxt =
3079 assocparams.sasoc_asocmaxrxt;
3080 if (assocparams.sasoc_cookie_life != 0)
3081 sp->assocparams.sasoc_cookie_life =
3082 assocparams.sasoc_cookie_life;
3083 }
3084 return 0;
3085}
3086
3087/*
3088 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3089 *
3090 * This socket option is a boolean flag which turns on or off mapped V4
3091 * addresses. If this option is turned on and the socket is type
3092 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3093 * If this option is turned off, then no mapping will be done of V4
3094 * addresses and a user will receive both PF_INET6 and PF_INET type
3095 * addresses on the socket.
3096 */
David S. Millerb7058842009-09-30 16:12:20 -07003097static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
3099 int val;
3100 struct sctp_sock *sp = sctp_sk(sk);
3101
3102 if (optlen < sizeof(int))
3103 return -EINVAL;
3104 if (get_user(val, (int __user *)optval))
3105 return -EFAULT;
3106 if (val)
3107 sp->v4mapped = 1;
3108 else
3109 sp->v4mapped = 0;
3110
3111 return 0;
3112}
3113
3114/*
Wei Yongjune89c2092008-12-25 16:54:58 -08003115 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3116 * This option will get or set the maximum size to put in any outgoing
3117 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 * fragmented by SCTP into the specified size. Note that the underlying
3119 * SCTP implementation may fragment into smaller sized chunks when the
3120 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08003121 * the user. The default value for this option is '0' which indicates
3122 * the user is NOT limiting fragmentation and only the PMTU will effect
3123 * SCTP's choice of DATA chunk size. Note also that values set larger
3124 * than the maximum size of an IP datagram will effectively let SCTP
3125 * control fragmentation (i.e. the same as setting this option to 0).
3126 *
3127 * The following structure is used to access and modify this parameter:
3128 *
3129 * struct sctp_assoc_value {
3130 * sctp_assoc_t assoc_id;
3131 * uint32_t assoc_value;
3132 * };
3133 *
3134 * assoc_id: This parameter is ignored for one-to-one style sockets.
3135 * For one-to-many style sockets this parameter indicates which
3136 * association the user is performing an action upon. Note that if
3137 * this field's value is zero then the endpoints default value is
3138 * changed (effecting future associations only).
3139 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 */
David S. Millerb7058842009-09-30 16:12:20 -07003141static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
Xin Longecca8f82017-11-17 14:11:11 +08003143 struct sctp_sock *sp = sctp_sk(sk);
Wei Yongjune89c2092008-12-25 16:54:58 -08003144 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 int val;
3147
Wei Yongjune89c2092008-12-25 16:54:58 -08003148 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003149 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003150 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003151 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003152 "Use struct sctp_assoc_value instead\n",
3153 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08003154 if (copy_from_user(&val, optval, optlen))
3155 return -EFAULT;
3156 params.assoc_id = 0;
3157 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3158 if (copy_from_user(&params, optval, optlen))
3159 return -EFAULT;
3160 val = params.assoc_value;
Xin Longecca8f82017-11-17 14:11:11 +08003161 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 return -EINVAL;
Xin Longecca8f82017-11-17 14:11:11 +08003163 }
Wei Yongjune89c2092008-12-25 16:54:58 -08003164
Xin Longecca8f82017-11-17 14:11:11 +08003165 if (val) {
3166 int min_len, max_len;
3167
3168 min_len = SCTP_DEFAULT_MINSEGMENT - sp->pf->af->net_header_len;
3169 min_len -= sizeof(struct sctphdr) +
3170 sizeof(struct sctp_data_chunk);
3171
3172 max_len = SCTP_MAX_CHUNK_LEN - sizeof(struct sctp_data_chunk);
3173
3174 if (val < min_len || val > max_len)
3175 return -EINVAL;
3176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Wei Yongjune89c2092008-12-25 16:54:58 -08003178 asoc = sctp_id2assoc(sk, params.assoc_id);
Wei Yongjune89c2092008-12-25 16:54:58 -08003179 if (asoc) {
3180 if (val == 0) {
Xin Longecca8f82017-11-17 14:11:11 +08003181 val = asoc->pathmtu - sp->pf->af->net_header_len;
Wei Yongjune89c2092008-12-25 16:54:58 -08003182 val -= sizeof(struct sctphdr) +
Xin Longecca8f82017-11-17 14:11:11 +08003183 sizeof(struct sctp_data_chunk);
Wei Yongjune89c2092008-12-25 16:54:58 -08003184 }
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003185 asoc->user_frag = val;
3186 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
Wei Yongjune89c2092008-12-25 16:54:58 -08003187 } else {
Xin Longecca8f82017-11-17 14:11:11 +08003188 if (params.assoc_id && sctp_style(sk, UDP))
3189 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003190 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
3192
3193 return 0;
3194}
3195
3196
3197/*
3198 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3199 *
3200 * Requests that the peer mark the enclosed address as the association
3201 * primary. The enclosed address must be one of the association's
3202 * locally bound addresses. The following structure is used to make a
3203 * set primary request:
3204 */
3205static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003206 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003208 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 struct sctp_association *asoc = NULL;
3211 struct sctp_setpeerprim prim;
3212 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003213 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 int err;
3215
3216 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003218 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 return -EPERM;
3220
3221 if (optlen != sizeof(struct sctp_setpeerprim))
3222 return -EINVAL;
3223
3224 if (copy_from_user(&prim, optval, optlen))
3225 return -EFAULT;
3226
3227 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003228 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 return -EINVAL;
3230
3231 if (!asoc->peer.asconf_capable)
3232 return -EPERM;
3233
3234 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3235 return -EPERM;
3236
3237 if (!sctp_state(asoc, ESTABLISHED))
3238 return -ENOTCONN;
3239
Wei Yongjun40a01032010-12-07 17:11:09 +00003240 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3241 if (!af)
3242 return -EINVAL;
3243
3244 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3245 return -EADDRNOTAVAIL;
3246
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3248 return -EADDRNOTAVAIL;
3249
3250 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3251 chunk = sctp_make_asconf_set_prim(asoc,
3252 (union sctp_addr *)&prim.sspp_addr);
3253 if (!chunk)
3254 return -ENOMEM;
3255
3256 err = sctp_send_asconf(asoc, chunk);
3257
Daniel Borkmannbb333812013-06-28 19:49:40 +02003258 pr_debug("%s: we set peer primary addr primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
3260 return err;
3261}
3262
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003263static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003264 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003266 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003268 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003270 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 return -EFAULT;
3272
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003273 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
3275 return 0;
3276}
3277
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003278/*
3279 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3280 *
3281 * The context field in the sctp_sndrcvinfo structure is normally only
3282 * used when a failed message is retrieved holding the value that was
3283 * sent down on the actual send call. This option allows the setting of
3284 * a default context on an association basis that will be received on
3285 * reading messages from the peer. This is especially helpful in the
3286 * one-2-many model for an application to keep some reference to an
3287 * internal state machine that is processing messages on the
3288 * association. Note that the setting of this value only effects
3289 * received messages from the peer and does not effect the value that is
3290 * saved with outbound messages.
3291 */
3292static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003293 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003294{
3295 struct sctp_assoc_value params;
3296 struct sctp_sock *sp;
3297 struct sctp_association *asoc;
3298
3299 if (optlen != sizeof(struct sctp_assoc_value))
3300 return -EINVAL;
3301 if (copy_from_user(&params, optval, optlen))
3302 return -EFAULT;
3303
3304 sp = sctp_sk(sk);
3305
3306 if (params.assoc_id != 0) {
3307 asoc = sctp_id2assoc(sk, params.assoc_id);
3308 if (!asoc)
3309 return -EINVAL;
3310 asoc->default_rcv_context = params.assoc_value;
3311 } else {
3312 sp->default_rcv_context = params.assoc_value;
3313 }
3314
3315 return 0;
3316}
3317
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003318/*
3319 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3320 *
3321 * This options will at a minimum specify if the implementation is doing
3322 * fragmented interleave. Fragmented interleave, for a one to many
3323 * socket, is when subsequent calls to receive a message may return
3324 * parts of messages from different associations. Some implementations
3325 * may allow you to turn this value on or off. If so, when turned off,
3326 * no fragment interleave will occur (which will cause a head of line
3327 * blocking amongst multiple associations sharing the same one to many
3328 * socket). When this option is turned on, then each receive call may
3329 * come from a different association (thus the user must receive data
3330 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3331 * association each receive belongs to.
3332 *
3333 * This option takes a boolean value. A non-zero value indicates that
3334 * fragmented interleave is on. A value of zero indicates that
3335 * fragmented interleave is off.
3336 *
3337 * Note that it is important that an implementation that allows this
3338 * option to be turned on, have it off by default. Otherwise an unaware
3339 * application using the one to many model may become confused and act
3340 * incorrectly.
3341 */
3342static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3343 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003344 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003345{
3346 int val;
3347
3348 if (optlen != sizeof(int))
3349 return -EINVAL;
3350 if (get_user(val, (int __user *)optval))
3351 return -EFAULT;
3352
3353 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
3354
3355 return 0;
3356}
3357
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003358/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003359 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003360 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003361 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003362 * This option will set or get the SCTP partial delivery point. This
3363 * point is the size of a message where the partial delivery API will be
3364 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003365 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003366 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003367 * point. Note also that the call will fail if the user attempts to set
3368 * this value larger than the socket receive buffer size.
3369 *
3370 * Note that any single message having a length smaller than or equal to
3371 * the SCTP partial delivery point will be delivered in one single read
3372 * call as long as the user provided buffer is large enough to hold the
3373 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003374 */
3375static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3376 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003377 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003378{
3379 u32 val;
3380
3381 if (optlen != sizeof(u32))
3382 return -EINVAL;
3383 if (get_user(val, (int __user *)optval))
3384 return -EFAULT;
3385
Wei Yongjun8510b932008-12-25 16:59:03 -08003386 /* Note: We double the receive buffer from what the user sets
3387 * it to be, also initial rwnd is based on rcvbuf/2.
3388 */
3389 if (val > (sk->sk_rcvbuf >> 1))
3390 return -EINVAL;
3391
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003392 sctp_sk(sk)->pd_point = val;
3393
3394 return 0; /* is this the right error code? */
3395}
3396
Vlad Yasevich70331572007-03-23 11:34:36 -07003397/*
3398 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3399 *
3400 * This option will allow a user to change the maximum burst of packets
3401 * that can be emitted by this association. Note that the default value
3402 * is 4, and some implementations may restrict this setting so that it
3403 * can only be lowered.
3404 *
3405 * NOTE: This text doesn't seem right. Do this on a socket basis with
3406 * future associations inheriting the socket value.
3407 */
3408static int sctp_setsockopt_maxburst(struct sock *sk,
3409 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003410 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003411{
Neil Horman219b99a2008-03-05 13:44:46 -08003412 struct sctp_assoc_value params;
3413 struct sctp_sock *sp;
3414 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003415 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003416 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003417
Neil Horman219b99a2008-03-05 13:44:46 -08003418 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003419 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003420 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003421 "Use of int in max_burst socket option deprecated.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003422 "Use struct sctp_assoc_value instead\n",
3423 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08003424 if (copy_from_user(&val, optval, optlen))
3425 return -EFAULT;
3426 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3427 if (copy_from_user(&params, optval, optlen))
3428 return -EFAULT;
3429 val = params.assoc_value;
3430 assoc_id = params.assoc_id;
3431 } else
3432 return -EINVAL;
3433
3434 sp = sctp_sk(sk);
3435
3436 if (assoc_id != 0) {
3437 asoc = sctp_id2assoc(sk, assoc_id);
3438 if (!asoc)
3439 return -EINVAL;
3440 asoc->max_burst = val;
3441 } else
3442 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003443
3444 return 0;
3445}
3446
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003447/*
3448 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3449 *
3450 * This set option adds a chunk type that the user is requesting to be
3451 * received only in an authenticated way. Changes to the list of chunks
3452 * will only effect future associations on the socket.
3453 */
3454static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003455 char __user *optval,
3456 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003457{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003458 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003459 struct sctp_authchunk val;
3460
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003461 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003462 return -EACCES;
3463
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003464 if (optlen != sizeof(struct sctp_authchunk))
3465 return -EINVAL;
3466 if (copy_from_user(&val, optval, optlen))
3467 return -EFAULT;
3468
3469 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003470 case SCTP_CID_INIT:
3471 case SCTP_CID_INIT_ACK:
3472 case SCTP_CID_SHUTDOWN_COMPLETE:
3473 case SCTP_CID_AUTH:
3474 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003475 }
3476
3477 /* add this chunk id to the endpoint */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003478 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003479}
3480
3481/*
3482 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3483 *
3484 * This option gets or sets the list of HMAC algorithms that the local
3485 * endpoint requires the peer to use.
3486 */
3487static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003488 char __user *optval,
3489 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003490{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003491 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003492 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003493 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003494 int err;
3495
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003496 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003497 return -EACCES;
3498
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003499 if (optlen < sizeof(struct sctp_hmacalgo))
3500 return -EINVAL;
3501
wangweidongcb3f8372013-12-23 12:16:50 +08003502 hmacs = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003503 if (IS_ERR(hmacs))
3504 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003505
Vlad Yasevichd9724052008-08-27 16:09:49 -07003506 idents = hmacs->shmac_num_idents;
3507 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3508 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003509 err = -EINVAL;
3510 goto out;
3511 }
3512
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003513 err = sctp_auth_ep_set_hmacs(ep, hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003514out:
3515 kfree(hmacs);
3516 return err;
3517}
3518
3519/*
3520 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3521 *
3522 * This option will set a shared secret key which is used to build an
3523 * association shared key.
3524 */
3525static int sctp_setsockopt_auth_key(struct sock *sk,
3526 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003527 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003528{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003529 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003530 struct sctp_authkey *authkey;
3531 struct sctp_association *asoc;
3532 int ret;
3533
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003534 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003535 return -EACCES;
3536
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003537 if (optlen <= sizeof(struct sctp_authkey))
3538 return -EINVAL;
3539
wangweidongcb3f8372013-12-23 12:16:50 +08003540 authkey = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003541 if (IS_ERR(authkey))
3542 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003543
Vlad Yasevich328fc472008-08-27 16:08:54 -07003544 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003545 ret = -EINVAL;
3546 goto out;
3547 }
3548
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003549 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3550 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3551 ret = -EINVAL;
3552 goto out;
3553 }
3554
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003555 ret = sctp_auth_set_key(ep, asoc, authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003556out:
Daniel Borkmann6ba542a2013-02-08 03:04:34 +00003557 kzfree(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003558 return ret;
3559}
3560
3561/*
3562 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3563 *
3564 * This option will get or set the active shared key to be used to build
3565 * the association shared key.
3566 */
3567static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003568 char __user *optval,
3569 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003570{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003571 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003572 struct sctp_authkeyid val;
3573 struct sctp_association *asoc;
3574
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003575 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003576 return -EACCES;
3577
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003578 if (optlen != sizeof(struct sctp_authkeyid))
3579 return -EINVAL;
3580 if (copy_from_user(&val, optval, optlen))
3581 return -EFAULT;
3582
3583 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3584 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3585 return -EINVAL;
3586
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003587 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003588}
3589
3590/*
3591 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3592 *
3593 * This set option will delete a shared secret key from use.
3594 */
3595static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003596 char __user *optval,
3597 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003598{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003599 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003600 struct sctp_authkeyid val;
3601 struct sctp_association *asoc;
3602
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003603 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003604 return -EACCES;
3605
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003606 if (optlen != sizeof(struct sctp_authkeyid))
3607 return -EINVAL;
3608 if (copy_from_user(&val, optval, optlen))
3609 return -EFAULT;
3610
3611 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3612 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3613 return -EINVAL;
3614
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003615 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003616
3617}
3618
Michio Honda7dc04d72011-04-26 20:16:31 +09003619/*
3620 * 8.1.23 SCTP_AUTO_ASCONF
3621 *
3622 * This option will enable or disable the use of the automatic generation of
3623 * ASCONF chunks to add and delete addresses to an existing association. Note
3624 * that this option has two caveats namely: a) it only affects sockets that
3625 * are bound to all addresses available to the SCTP stack, and b) the system
3626 * administrator may have an overriding control that turns the ASCONF feature
3627 * off no matter what setting the socket option may have.
3628 * This option expects an integer boolean flag, where a non-zero value turns on
3629 * the option, and a zero value turns off the option.
3630 * Note. In this implementation, socket operation overrides default parameter
3631 * being set by sysctl as well as FreeBSD implementation
3632 */
3633static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3634 unsigned int optlen)
3635{
3636 int val;
3637 struct sctp_sock *sp = sctp_sk(sk);
3638
3639 if (optlen < sizeof(int))
3640 return -EINVAL;
3641 if (get_user(val, (int __user *)optval))
3642 return -EFAULT;
3643 if (!sctp_is_ep_boundall(sk) && val)
3644 return -EINVAL;
3645 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3646 return 0;
3647
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003648 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003649 if (val == 0 && sp->do_auto_asconf) {
3650 list_del(&sp->auto_asconf_list);
3651 sp->do_auto_asconf = 0;
3652 } else if (val && !sp->do_auto_asconf) {
3653 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00003654 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09003655 sp->do_auto_asconf = 1;
3656 }
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003657 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003658 return 0;
3659}
3660
Neil Horman5aa93bc2012-07-21 07:56:07 +00003661/*
3662 * SCTP_PEER_ADDR_THLDS
3663 *
3664 * This option allows us to alter the partially failed threshold for one or all
3665 * transports in an association. See Section 6.1 of:
3666 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3667 */
3668static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3669 char __user *optval,
3670 unsigned int optlen)
3671{
3672 struct sctp_paddrthlds val;
3673 struct sctp_transport *trans;
3674 struct sctp_association *asoc;
3675
3676 if (optlen < sizeof(struct sctp_paddrthlds))
3677 return -EINVAL;
3678 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3679 sizeof(struct sctp_paddrthlds)))
3680 return -EFAULT;
3681
3682
3683 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3684 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3685 if (!asoc)
3686 return -ENOENT;
3687 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3688 transports) {
3689 if (val.spt_pathmaxrxt)
3690 trans->pathmaxrxt = val.spt_pathmaxrxt;
3691 trans->pf_retrans = val.spt_pathpfthld;
3692 }
3693
3694 if (val.spt_pathmaxrxt)
3695 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3696 asoc->pf_retrans = val.spt_pathpfthld;
3697 } else {
3698 trans = sctp_addr_id2transport(sk, &val.spt_address,
3699 val.spt_assoc_id);
3700 if (!trans)
3701 return -ENOENT;
3702
3703 if (val.spt_pathmaxrxt)
3704 trans->pathmaxrxt = val.spt_pathmaxrxt;
3705 trans->pf_retrans = val.spt_pathpfthld;
3706 }
3707
3708 return 0;
3709}
3710
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02003711static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3712 char __user *optval,
3713 unsigned int optlen)
3714{
3715 int val;
3716
3717 if (optlen < sizeof(int))
3718 return -EINVAL;
3719 if (get_user(val, (int __user *) optval))
3720 return -EFAULT;
3721
3722 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3723
3724 return 0;
3725}
3726
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02003727static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3728 char __user *optval,
3729 unsigned int optlen)
3730{
3731 int val;
3732
3733 if (optlen < sizeof(int))
3734 return -EINVAL;
3735 if (get_user(val, (int __user *) optval))
3736 return -EFAULT;
3737
3738 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3739
3740 return 0;
3741}
3742
Xin Long28aa4c22016-07-09 19:47:40 +08003743static int sctp_setsockopt_pr_supported(struct sock *sk,
3744 char __user *optval,
3745 unsigned int optlen)
3746{
3747 struct sctp_assoc_value params;
3748 struct sctp_association *asoc;
3749 int retval = -EINVAL;
3750
3751 if (optlen != sizeof(params))
3752 goto out;
3753
3754 if (copy_from_user(&params, optval, optlen)) {
3755 retval = -EFAULT;
3756 goto out;
3757 }
3758
3759 asoc = sctp_id2assoc(sk, params.assoc_id);
3760 if (asoc) {
3761 asoc->prsctp_enable = !!params.assoc_value;
3762 } else if (!params.assoc_id) {
3763 struct sctp_sock *sp = sctp_sk(sk);
3764
3765 sp->ep->prsctp_enable = !!params.assoc_value;
3766 } else {
3767 goto out;
3768 }
3769
3770 retval = 0;
3771
3772out:
3773 return retval;
3774}
3775
Xin Longf959fb42016-07-09 19:47:41 +08003776static int sctp_setsockopt_default_prinfo(struct sock *sk,
3777 char __user *optval,
3778 unsigned int optlen)
3779{
3780 struct sctp_default_prinfo info;
3781 struct sctp_association *asoc;
3782 int retval = -EINVAL;
3783
3784 if (optlen != sizeof(info))
3785 goto out;
3786
3787 if (copy_from_user(&info, optval, sizeof(info))) {
3788 retval = -EFAULT;
3789 goto out;
3790 }
3791
3792 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
3793 goto out;
3794
3795 if (info.pr_policy == SCTP_PR_SCTP_NONE)
3796 info.pr_value = 0;
3797
3798 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
3799 if (asoc) {
3800 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
3801 asoc->default_timetolive = info.pr_value;
3802 } else if (!info.pr_assoc_id) {
3803 struct sctp_sock *sp = sctp_sk(sk);
3804
3805 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
3806 sp->default_timetolive = info.pr_value;
3807 } else {
3808 goto out;
3809 }
3810
3811 retval = 0;
3812
3813out:
3814 return retval;
3815}
3816
Xin Longc0d8bab2017-03-10 12:11:12 +08003817static int sctp_setsockopt_reconfig_supported(struct sock *sk,
3818 char __user *optval,
3819 unsigned int optlen)
3820{
3821 struct sctp_assoc_value params;
3822 struct sctp_association *asoc;
3823 int retval = -EINVAL;
3824
3825 if (optlen != sizeof(params))
3826 goto out;
3827
3828 if (copy_from_user(&params, optval, optlen)) {
3829 retval = -EFAULT;
3830 goto out;
3831 }
3832
3833 asoc = sctp_id2assoc(sk, params.assoc_id);
3834 if (asoc) {
3835 asoc->reconf_enable = !!params.assoc_value;
3836 } else if (!params.assoc_id) {
3837 struct sctp_sock *sp = sctp_sk(sk);
3838
3839 sp->ep->reconf_enable = !!params.assoc_value;
3840 } else {
3841 goto out;
3842 }
3843
3844 retval = 0;
3845
3846out:
3847 return retval;
3848}
3849
Xin Long9fb657a2017-01-18 00:44:46 +08003850static int sctp_setsockopt_enable_strreset(struct sock *sk,
3851 char __user *optval,
3852 unsigned int optlen)
3853{
3854 struct sctp_assoc_value params;
3855 struct sctp_association *asoc;
3856 int retval = -EINVAL;
3857
3858 if (optlen != sizeof(params))
3859 goto out;
3860
3861 if (copy_from_user(&params, optval, optlen)) {
3862 retval = -EFAULT;
3863 goto out;
3864 }
3865
3866 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
3867 goto out;
3868
3869 asoc = sctp_id2assoc(sk, params.assoc_id);
3870 if (asoc) {
3871 asoc->strreset_enable = params.assoc_value;
3872 } else if (!params.assoc_id) {
3873 struct sctp_sock *sp = sctp_sk(sk);
3874
3875 sp->ep->strreset_enable = params.assoc_value;
3876 } else {
3877 goto out;
3878 }
3879
3880 retval = 0;
3881
3882out:
3883 return retval;
3884}
3885
Xin Long7f9d68a2017-01-18 00:44:47 +08003886static int sctp_setsockopt_reset_streams(struct sock *sk,
3887 char __user *optval,
3888 unsigned int optlen)
3889{
3890 struct sctp_reset_streams *params;
3891 struct sctp_association *asoc;
3892 int retval = -EINVAL;
3893
3894 if (optlen < sizeof(struct sctp_reset_streams))
3895 return -EINVAL;
3896
3897 params = memdup_user(optval, optlen);
3898 if (IS_ERR(params))
3899 return PTR_ERR(params);
3900
3901 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
3902 if (!asoc)
3903 goto out;
3904
3905 retval = sctp_send_reset_streams(asoc, params);
3906
3907out:
3908 kfree(params);
3909 return retval;
3910}
3911
Xin Longa92ce1a2017-02-09 01:18:18 +08003912static int sctp_setsockopt_reset_assoc(struct sock *sk,
3913 char __user *optval,
3914 unsigned int optlen)
3915{
3916 struct sctp_association *asoc;
3917 sctp_assoc_t associd;
3918 int retval = -EINVAL;
3919
3920 if (optlen != sizeof(associd))
3921 goto out;
3922
3923 if (copy_from_user(&associd, optval, optlen)) {
3924 retval = -EFAULT;
3925 goto out;
3926 }
3927
3928 asoc = sctp_id2assoc(sk, associd);
3929 if (!asoc)
3930 goto out;
3931
3932 retval = sctp_send_reset_assoc(asoc);
3933
3934out:
3935 return retval;
3936}
3937
Xin Long242bd2d2017-02-09 01:18:20 +08003938static int sctp_setsockopt_add_streams(struct sock *sk,
3939 char __user *optval,
3940 unsigned int optlen)
3941{
3942 struct sctp_association *asoc;
3943 struct sctp_add_streams params;
3944 int retval = -EINVAL;
3945
3946 if (optlen != sizeof(params))
3947 goto out;
3948
3949 if (copy_from_user(&params, optval, optlen)) {
3950 retval = -EFAULT;
3951 goto out;
3952 }
3953
3954 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
3955 if (!asoc)
3956 goto out;
3957
3958 retval = sctp_send_add_streams(asoc, &params);
3959
3960out:
3961 return retval;
3962}
3963
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03003964static int sctp_setsockopt_scheduler(struct sock *sk,
3965 char __user *optval,
3966 unsigned int optlen)
3967{
3968 struct sctp_association *asoc;
3969 struct sctp_assoc_value params;
3970 int retval = -EINVAL;
3971
3972 if (optlen < sizeof(params))
3973 goto out;
3974
3975 optlen = sizeof(params);
3976 if (copy_from_user(&params, optval, optlen)) {
3977 retval = -EFAULT;
3978 goto out;
3979 }
3980
3981 if (params.assoc_value > SCTP_SS_MAX)
3982 goto out;
3983
3984 asoc = sctp_id2assoc(sk, params.assoc_id);
3985 if (!asoc)
3986 goto out;
3987
3988 retval = sctp_sched_set_sched(asoc, params.assoc_value);
3989
3990out:
3991 return retval;
3992}
3993
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03003994static int sctp_setsockopt_scheduler_value(struct sock *sk,
3995 char __user *optval,
3996 unsigned int optlen)
3997{
3998 struct sctp_association *asoc;
3999 struct sctp_stream_value params;
4000 int retval = -EINVAL;
4001
4002 if (optlen < sizeof(params))
4003 goto out;
4004
4005 optlen = sizeof(params);
4006 if (copy_from_user(&params, optval, optlen)) {
4007 retval = -EFAULT;
4008 goto out;
4009 }
4010
4011 asoc = sctp_id2assoc(sk, params.assoc_id);
4012 if (!asoc)
4013 goto out;
4014
4015 retval = sctp_sched_set_value(asoc, params.stream_id,
4016 params.stream_value, GFP_KERNEL);
4017
4018out:
4019 return retval;
4020}
4021
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022/* API 6.2 setsockopt(), getsockopt()
4023 *
4024 * Applications use setsockopt() and getsockopt() to set or retrieve
4025 * socket options. Socket options are used to change the default
4026 * behavior of sockets calls. They are described in Section 7.
4027 *
4028 * The syntax is:
4029 *
4030 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4031 * int __user *optlen);
4032 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4033 * int optlen);
4034 *
4035 * sd - the socket descript.
4036 * level - set to IPPROTO_SCTP for all SCTP options.
4037 * optname - the option name.
4038 * optval - the buffer to store the value of the option.
4039 * optlen - the size of the buffer.
4040 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004041static int sctp_setsockopt(struct sock *sk, int level, int optname,
4042 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043{
4044 int retval = 0;
4045
Daniel Borkmannbb333812013-06-28 19:49:40 +02004046 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
4048 /* I can hardly begin to describe how wrong this is. This is
4049 * so broken as to be worse than useless. The API draft
4050 * REALLY is NOT helpful here... I am not convinced that the
4051 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4052 * are at all well-founded.
4053 */
4054 if (level != SOL_SCTP) {
4055 struct sctp_af *af = sctp_sk(sk)->pf->af;
4056 retval = af->setsockopt(sk, level, optname, optval, optlen);
4057 goto out_nounlock;
4058 }
4059
wangweidong048ed4b2014-01-21 15:44:11 +08004060 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061
4062 switch (optname) {
4063 case SCTP_SOCKOPT_BINDX_ADD:
4064 /* 'optlen' is the size of the addresses buffer. */
4065 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4066 optlen, SCTP_BINDX_ADD_ADDR);
4067 break;
4068
4069 case SCTP_SOCKOPT_BINDX_REM:
4070 /* 'optlen' is the size of the addresses buffer. */
4071 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4072 optlen, SCTP_BINDX_REM_ADDR);
4073 break;
4074
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004075 case SCTP_SOCKOPT_CONNECTX_OLD:
4076 /* 'optlen' is the size of the addresses buffer. */
4077 retval = sctp_setsockopt_connectx_old(sk,
4078 (struct sockaddr __user *)optval,
4079 optlen);
4080 break;
4081
Frank Filz3f7a87d2005-06-20 13:14:57 -07004082 case SCTP_SOCKOPT_CONNECTX:
4083 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004084 retval = sctp_setsockopt_connectx(sk,
4085 (struct sockaddr __user *)optval,
4086 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004087 break;
4088
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 case SCTP_DISABLE_FRAGMENTS:
4090 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4091 break;
4092
4093 case SCTP_EVENTS:
4094 retval = sctp_setsockopt_events(sk, optval, optlen);
4095 break;
4096
4097 case SCTP_AUTOCLOSE:
4098 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4099 break;
4100
4101 case SCTP_PEER_ADDR_PARAMS:
4102 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4103 break;
4104
Shan Wei4580ccc2011-01-18 22:39:00 +00004105 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07004106 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08004107 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07004108 case SCTP_PARTIAL_DELIVERY_POINT:
4109 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4110 break;
Frank Filz77086102005-12-22 11:37:30 -08004111
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 case SCTP_INITMSG:
4113 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4114 break;
4115 case SCTP_DEFAULT_SEND_PARAM:
4116 retval = sctp_setsockopt_default_send_param(sk, optval,
4117 optlen);
4118 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02004119 case SCTP_DEFAULT_SNDINFO:
4120 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4121 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 case SCTP_PRIMARY_ADDR:
4123 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4124 break;
4125 case SCTP_SET_PEER_PRIMARY_ADDR:
4126 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4127 break;
4128 case SCTP_NODELAY:
4129 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4130 break;
4131 case SCTP_RTOINFO:
4132 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4133 break;
4134 case SCTP_ASSOCINFO:
4135 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4136 break;
4137 case SCTP_I_WANT_MAPPED_V4_ADDR:
4138 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4139 break;
4140 case SCTP_MAXSEG:
4141 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4142 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004143 case SCTP_ADAPTATION_LAYER:
4144 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004146 case SCTP_CONTEXT:
4147 retval = sctp_setsockopt_context(sk, optval, optlen);
4148 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004149 case SCTP_FRAGMENT_INTERLEAVE:
4150 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4151 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07004152 case SCTP_MAX_BURST:
4153 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4154 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004155 case SCTP_AUTH_CHUNK:
4156 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4157 break;
4158 case SCTP_HMAC_IDENT:
4159 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4160 break;
4161 case SCTP_AUTH_KEY:
4162 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4163 break;
4164 case SCTP_AUTH_ACTIVE_KEY:
4165 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4166 break;
4167 case SCTP_AUTH_DELETE_KEY:
4168 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4169 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09004170 case SCTP_AUTO_ASCONF:
4171 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4172 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00004173 case SCTP_PEER_ADDR_THLDS:
4174 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4175 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004176 case SCTP_RECVRCVINFO:
4177 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4178 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004179 case SCTP_RECVNXTINFO:
4180 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4181 break;
Xin Long28aa4c22016-07-09 19:47:40 +08004182 case SCTP_PR_SUPPORTED:
4183 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4184 break;
Xin Longf959fb42016-07-09 19:47:41 +08004185 case SCTP_DEFAULT_PRINFO:
4186 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4187 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08004188 case SCTP_RECONFIG_SUPPORTED:
4189 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4190 break;
Xin Long9fb657a2017-01-18 00:44:46 +08004191 case SCTP_ENABLE_STREAM_RESET:
4192 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4193 break;
Xin Long7f9d68a2017-01-18 00:44:47 +08004194 case SCTP_RESET_STREAMS:
4195 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4196 break;
Xin Longa92ce1a2017-02-09 01:18:18 +08004197 case SCTP_RESET_ASSOC:
4198 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4199 break;
Xin Long242bd2d2017-02-09 01:18:20 +08004200 case SCTP_ADD_STREAMS:
4201 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4202 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03004203 case SCTP_STREAM_SCHEDULER:
4204 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4205 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03004206 case SCTP_STREAM_SCHEDULER_VALUE:
4207 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4208 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 default:
4210 retval = -ENOPROTOOPT;
4211 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
wangweidong048ed4b2014-01-21 15:44:11 +08004214 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
4216out_nounlock:
4217 return retval;
4218}
4219
4220/* API 3.1.6 connect() - UDP Style Syntax
4221 *
4222 * An application may use the connect() call in the UDP model to initiate an
4223 * association without sending data.
4224 *
4225 * The syntax is:
4226 *
4227 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4228 *
4229 * sd: the socket descriptor to have a new association added to.
4230 *
4231 * nam: the address structure (either struct sockaddr_in or struct
4232 * sockaddr_in6 defined in RFC2553 [7]).
4233 *
4234 * len: the size of the address.
4235 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004236static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4237 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07004240 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
wangweidong048ed4b2014-01-21 15:44:11 +08004242 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Daniel Borkmannbb333812013-06-28 19:49:40 +02004244 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4245 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
Frank Filz3f7a87d2005-06-20 13:14:57 -07004247 /* Validate addr_len before calling common connect/connectx routine. */
4248 af = sctp_get_af_specific(addr->sa_family);
4249 if (!af || addr_len < af->sockaddr_len) {
4250 err = -EINVAL;
4251 } else {
4252 /* Pass correct addr len to common routine (so it knows there
4253 * is only one address being passed.
4254 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004255 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 }
4257
wangweidong048ed4b2014-01-21 15:44:11 +08004258 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 return err;
4260}
4261
4262/* FIXME: Write comments. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004263static int sctp_disconnect(struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264{
4265 return -EOPNOTSUPP; /* STUB */
4266}
4267
4268/* 4.1.4 accept() - TCP Style Syntax
4269 *
4270 * Applications use accept() call to remove an established SCTP
4271 * association from the accept queue of the endpoint. A new socket
4272 * descriptor will be returned from accept() to represent the newly
4273 * formed association.
4274 */
David Howellscdfbabf2017-03-09 08:09:05 +00004275static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276{
4277 struct sctp_sock *sp;
4278 struct sctp_endpoint *ep;
4279 struct sock *newsk = NULL;
4280 struct sctp_association *asoc;
4281 long timeo;
4282 int error = 0;
4283
wangweidong048ed4b2014-01-21 15:44:11 +08004284 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
4286 sp = sctp_sk(sk);
4287 ep = sp->ep;
4288
4289 if (!sctp_style(sk, TCP)) {
4290 error = -EOPNOTSUPP;
4291 goto out;
4292 }
4293
4294 if (!sctp_sstate(sk, LISTENING)) {
4295 error = -EINVAL;
4296 goto out;
4297 }
4298
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07004299 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
4301 error = sctp_wait_for_accept(sk, timeo);
4302 if (error)
4303 goto out;
4304
4305 /* We treat the list of associations on the endpoint as the accept
4306 * queue and pick the first association on the list.
4307 */
4308 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4309
David Howellscdfbabf2017-03-09 08:09:05 +00004310 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 if (!newsk) {
4312 error = -ENOMEM;
4313 goto out;
4314 }
4315
4316 /* Populate the fields of the newsk from the oldsk and migrate the
4317 * asoc to the newsk.
4318 */
4319 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4320
4321out:
wangweidong048ed4b2014-01-21 15:44:11 +08004322 release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004323 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 return newsk;
4325}
4326
4327/* The SCTP ioctl handler. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004328static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004330 int rc = -ENOTCONN;
4331
wangweidong048ed4b2014-01-21 15:44:11 +08004332 lock_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004333
4334 /*
4335 * SEQPACKET-style sockets in LISTENING state are valid, for
4336 * SCTP, so only discard TCP-style sockets in LISTENING state.
4337 */
4338 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4339 goto out;
4340
4341 switch (cmd) {
4342 case SIOCINQ: {
4343 struct sk_buff *skb;
4344 unsigned int amount = 0;
4345
4346 skb = skb_peek(&sk->sk_receive_queue);
4347 if (skb != NULL) {
4348 /*
4349 * We will only return the amount of this packet since
4350 * that is all that will be read.
4351 */
4352 amount = skb->len;
4353 }
4354 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004355 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07004356 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004357 default:
4358 rc = -ENOIOCTLCMD;
4359 break;
4360 }
4361out:
wangweidong048ed4b2014-01-21 15:44:11 +08004362 release_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004363 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364}
4365
4366/* This is the function which gets called during socket creation to
4367 * initialized the SCTP-specific portion of the sock.
4368 * The sock structure should already be zero-filled memory.
4369 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004370static int sctp_init_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004372 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 struct sctp_sock *sp;
4374
Daniel Borkmannbb333812013-06-28 19:49:40 +02004375 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376
4377 sp = sctp_sk(sk);
4378
4379 /* Initialize the SCTP per socket area. */
4380 switch (sk->sk_type) {
4381 case SOCK_SEQPACKET:
4382 sp->type = SCTP_SOCKET_UDP;
4383 break;
4384 case SOCK_STREAM:
4385 sp->type = SCTP_SOCKET_TCP;
4386 break;
4387 default:
4388 return -ESOCKTNOSUPPORT;
4389 }
4390
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004391 sk->sk_gso_type = SKB_GSO_SCTP;
4392
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 /* Initialize default send parameters. These parameters can be
4394 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4395 */
4396 sp->default_stream = 0;
4397 sp->default_ppid = 0;
4398 sp->default_flags = 0;
4399 sp->default_context = 0;
4400 sp->default_timetolive = 0;
4401
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004402 sp->default_rcv_context = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004403 sp->max_burst = net->sctp.max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004404
Neil Horman3c681982012-10-24 09:20:03 +00004405 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4406
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 /* Initialize default setup parameters. These parameters
4408 * can be modified with the SCTP_INITMSG socket option or
4409 * overridden by the SCTP_INIT CMSG.
4410 */
4411 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4412 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004413 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4414 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
4416 /* Initialize default RTO related parameters. These parameters can
4417 * be modified for with the SCTP_RTOINFO socket option.
4418 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004419 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4420 sp->rtoinfo.srto_max = net->sctp.rto_max;
4421 sp->rtoinfo.srto_min = net->sctp.rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
4423 /* Initialize default association related parameters. These parameters
4424 * can be modified with the SCTP_ASSOCINFO socket option.
4425 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004426 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 sp->assocparams.sasoc_number_peer_destinations = 0;
4428 sp->assocparams.sasoc_peer_rwnd = 0;
4429 sp->assocparams.sasoc_local_rwnd = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004430 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
4432 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004433 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 */
4435 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4436
4437 /* Default Peer Address Parameters. These defaults can
4438 * be modified via SCTP_PEER_ADDR_PARAMS
4439 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004440 sp->hbinterval = net->sctp.hb_interval;
4441 sp->pathmaxrxt = net->sctp.max_retrans_path;
wangweidong4e2d52b2013-12-23 12:16:54 +08004442 sp->pathmtu = 0; /* allow default discovery */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004443 sp->sackdelay = net->sctp.sack_timeout;
Vlad Yasevich7bfe8bdb2008-06-09 15:45:05 -07004444 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004445 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004446 SPP_PMTUD_ENABLE |
4447 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448
4449 /* If enabled no SCTP message fragmentation will be performed.
4450 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4451 */
4452 sp->disable_fragments = 0;
4453
Sridhar Samudrala208edef2006-09-29 17:08:01 -07004454 /* Enable Nagle algorithm by default. */
4455 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004457 sp->recvrcvinfo = 0;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004458 sp->recvnxtinfo = 0;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004459
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 /* Enable by default. */
4461 sp->v4mapped = 1;
4462
4463 /* Auto-close idle associations after the configured
4464 * number of seconds. A value of 0 disables this
4465 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4466 * for UDP-style sockets only.
4467 */
4468 sp->autoclose = 0;
4469
4470 /* User specified fragmentation limit. */
4471 sp->user_frag = 0;
4472
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004473 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
4475 sp->pf = sctp_get_pf_specific(sk->sk_family);
4476
4477 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004478 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004480 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
4482 /* Create a per socket endpoint structure. Even if we
4483 * change the data structure relationships, this may still
4484 * be useful for storing pre-connect address information.
4485 */
Daniel Borkmannc164b832013-06-14 18:24:06 +02004486 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4487 if (!sp->ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 return -ENOMEM;
4489
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 sp->hmac = NULL;
4491
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004492 sk->sk_destruct = sctp_destruct_sock;
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08004495
4496 local_bh_disable();
Vlad Yasevich81419d82010-04-28 08:47:20 +00004497 percpu_counter_inc(&sctp_sockets_allocated);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004498 sock_prot_inuse_add(net, sk->sk_prot, 1);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004499
4500 /* Nothing can fail after this block, otherwise
4501 * sctp_destroy_sock() will be called without addr_wq_lock held
4502 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004503 if (net->sctp.default_auto_asconf) {
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004504 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda9f7d6532011-04-26 19:32:51 +09004505 list_add_tail(&sp->auto_asconf_list,
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004506 &net->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09004507 sp->do_auto_asconf = 1;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004508 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4509 } else {
Michio Honda9f7d6532011-04-26 19:32:51 +09004510 sp->do_auto_asconf = 0;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004511 }
4512
David S. Miller6f756a82008-11-23 17:34:03 -08004513 local_bh_enable();
4514
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 return 0;
4516}
4517
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004518/* Cleanup any SCTP per socket resources. Must be called with
4519 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4520 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004521static void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522{
Michio Honda9f7d6532011-04-26 19:32:51 +09004523 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
Daniel Borkmannbb333812013-06-28 19:49:40 +02004525 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526
4527 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09004528 sp = sctp_sk(sk);
Daniel Borkmann1abd1652013-06-06 15:53:47 +02004529 /* This could happen during socket init, thus we bail out
4530 * early, since the rest of the below is not setup either.
4531 */
4532 if (sp->ep == NULL)
4533 return;
4534
Michio Honda9f7d6532011-04-26 19:32:51 +09004535 if (sp->do_auto_asconf) {
4536 sp->do_auto_asconf = 0;
4537 list_del(&sp->auto_asconf_list);
4538 }
4539 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004540 local_bh_disable();
Vlad Yasevich81419d82010-04-28 08:47:20 +00004541 percpu_counter_dec(&sctp_sockets_allocated);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08004542 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004543 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544}
4545
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004546/* Triggered when there are no references on the socket anymore */
4547static void sctp_destruct_sock(struct sock *sk)
4548{
4549 struct sctp_sock *sp = sctp_sk(sk);
4550
4551 /* Free up the HMAC transform. */
Herbert Xu5821c762016-01-24 21:20:12 +08004552 crypto_free_shash(sp->hmac);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004553
4554 inet_sock_destruct(sk);
4555}
4556
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557/* API 4.1.7 shutdown() - TCP Style Syntax
4558 * int shutdown(int socket, int how);
4559 *
4560 * sd - the socket descriptor of the association to be closed.
4561 * how - Specifies the type of shutdown. The values are
4562 * as follows:
4563 * SHUT_RD
4564 * Disables further receive operations. No SCTP
4565 * protocol action is taken.
4566 * SHUT_WR
4567 * Disables further send operations, and initiates
4568 * the SCTP shutdown sequence.
4569 * SHUT_RDWR
4570 * Disables further send and receive operations
4571 * and initiates the SCTP shutdown sequence.
4572 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004573static void sctp_shutdown(struct sock *sk, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004575 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 struct sctp_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577
4578 if (!sctp_style(sk, TCP))
4579 return;
4580
Xin Long5bf35dd2016-11-13 21:44:37 +08004581 ep = sctp_sk(sk)->ep;
4582 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4583 struct sctp_association *asoc;
4584
Xin Longd46e4162016-06-09 22:48:18 +08004585 sk->sk_state = SCTP_SS_CLOSING;
Xin Long5bf35dd2016-11-13 21:44:37 +08004586 asoc = list_entry(ep->asocs.next,
4587 struct sctp_association, asocs);
4588 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 }
4590}
4591
Xin Long52c52a62016-04-14 15:35:30 +08004592int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4593 struct sctp_info *info)
4594{
4595 struct sctp_transport *prim;
4596 struct list_head *pos;
4597 int mask;
4598
4599 memset(info, 0, sizeof(*info));
4600 if (!asoc) {
4601 struct sctp_sock *sp = sctp_sk(sk);
4602
4603 info->sctpi_s_autoclose = sp->autoclose;
4604 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4605 info->sctpi_s_pd_point = sp->pd_point;
4606 info->sctpi_s_nodelay = sp->nodelay;
4607 info->sctpi_s_disable_fragments = sp->disable_fragments;
4608 info->sctpi_s_v4mapped = sp->v4mapped;
4609 info->sctpi_s_frag_interleave = sp->frag_interleave;
Xin Long40eb90e92016-05-29 17:42:13 +08004610 info->sctpi_s_type = sp->type;
Xin Long52c52a62016-04-14 15:35:30 +08004611
4612 return 0;
4613 }
4614
4615 info->sctpi_tag = asoc->c.my_vtag;
4616 info->sctpi_state = asoc->state;
4617 info->sctpi_rwnd = asoc->a_rwnd;
4618 info->sctpi_unackdata = asoc->unack_data;
4619 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004620 info->sctpi_instrms = asoc->stream.incnt;
4621 info->sctpi_outstrms = asoc->stream.outcnt;
Xin Long52c52a62016-04-14 15:35:30 +08004622 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4623 info->sctpi_inqueue++;
4624 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4625 info->sctpi_outqueue++;
4626 info->sctpi_overall_error = asoc->overall_error_count;
4627 info->sctpi_max_burst = asoc->max_burst;
4628 info->sctpi_maxseg = asoc->frag_point;
4629 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4630 info->sctpi_peer_tag = asoc->c.peer_vtag;
4631
4632 mask = asoc->peer.ecn_capable << 1;
4633 mask = (mask | asoc->peer.ipv4_address) << 1;
4634 mask = (mask | asoc->peer.ipv6_address) << 1;
4635 mask = (mask | asoc->peer.hostname_address) << 1;
4636 mask = (mask | asoc->peer.asconf_capable) << 1;
4637 mask = (mask | asoc->peer.prsctp_capable) << 1;
4638 mask = (mask | asoc->peer.auth_capable);
4639 info->sctpi_peer_capable = mask;
4640 mask = asoc->peer.sack_needed << 1;
4641 mask = (mask | asoc->peer.sack_generation) << 1;
4642 mask = (mask | asoc->peer.zero_window_announced);
4643 info->sctpi_peer_sack = mask;
4644
4645 info->sctpi_isacks = asoc->stats.isacks;
4646 info->sctpi_osacks = asoc->stats.osacks;
4647 info->sctpi_opackets = asoc->stats.opackets;
4648 info->sctpi_ipackets = asoc->stats.ipackets;
4649 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4650 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4651 info->sctpi_idupchunks = asoc->stats.idupchunks;
4652 info->sctpi_gapcnt = asoc->stats.gapcnt;
4653 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4654 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4655 info->sctpi_oodchunks = asoc->stats.oodchunks;
4656 info->sctpi_iodchunks = asoc->stats.iodchunks;
4657 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4658 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4659
4660 prim = asoc->peer.primary_path;
Stefano Brivioee6c88b2017-08-23 13:27:13 +02004661 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
Xin Long52c52a62016-04-14 15:35:30 +08004662 info->sctpi_p_state = prim->state;
4663 info->sctpi_p_cwnd = prim->cwnd;
4664 info->sctpi_p_srtt = prim->srtt;
4665 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4666 info->sctpi_p_hbinterval = prim->hbinterval;
4667 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4668 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4669 info->sctpi_p_ssthresh = prim->ssthresh;
4670 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4671 info->sctpi_p_flight_size = prim->flight_size;
4672 info->sctpi_p_error = prim->error_count;
4673
4674 return 0;
4675}
4676EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4677
Xin Long626d16f2016-04-14 15:35:31 +08004678/* use callback to avoid exporting the core structure */
Tom Herbert97a6ec42017-12-04 10:31:41 -08004679void sctp_transport_walk_start(struct rhashtable_iter *iter)
Xin Long626d16f2016-04-14 15:35:31 +08004680{
Xin Long7fda702f2016-11-15 23:23:11 +08004681 rhltable_walk_enter(&sctp_transport_hashtable, iter);
Xin Long626d16f2016-04-14 15:35:31 +08004682
Tom Herbert97a6ec42017-12-04 10:31:41 -08004683 rhashtable_walk_start(iter);
Xin Long626d16f2016-04-14 15:35:31 +08004684}
4685
4686void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4687{
4688 rhashtable_walk_stop(iter);
4689 rhashtable_walk_exit(iter);
4690}
4691
4692struct sctp_transport *sctp_transport_get_next(struct net *net,
4693 struct rhashtable_iter *iter)
4694{
4695 struct sctp_transport *t;
4696
4697 t = rhashtable_walk_next(iter);
4698 for (; t; t = rhashtable_walk_next(iter)) {
4699 if (IS_ERR(t)) {
4700 if (PTR_ERR(t) == -EAGAIN)
4701 continue;
4702 break;
4703 }
4704
4705 if (net_eq(sock_net(t->asoc->base.sk), net) &&
4706 t->asoc->peer.primary_path == t)
4707 break;
4708 }
4709
4710 return t;
4711}
4712
4713struct sctp_transport *sctp_transport_get_idx(struct net *net,
4714 struct rhashtable_iter *iter,
4715 int pos)
4716{
4717 void *obj = SEQ_START_TOKEN;
4718
4719 while (pos && (obj = sctp_transport_get_next(net, iter)) &&
4720 !IS_ERR(obj))
4721 pos--;
4722
4723 return obj;
4724}
4725
4726int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
4727 void *p) {
4728 int err = 0;
4729 int hash = 0;
4730 struct sctp_ep_common *epb;
4731 struct sctp_hashbucket *head;
4732
4733 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
4734 hash++, head++) {
Xin Long581409d2017-06-10 14:48:14 +08004735 read_lock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004736 sctp_for_each_hentry(epb, &head->chain) {
4737 err = cb(sctp_ep(epb), p);
4738 if (err)
4739 break;
4740 }
Xin Long581409d2017-06-10 14:48:14 +08004741 read_unlock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004742 }
4743
4744 return err;
4745}
4746EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
4747
4748int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
4749 struct net *net,
4750 const union sctp_addr *laddr,
4751 const union sctp_addr *paddr, void *p)
4752{
4753 struct sctp_transport *transport;
Xin Long08abb792016-12-15 23:05:52 +08004754 int err;
Xin Long626d16f2016-04-14 15:35:31 +08004755
4756 rcu_read_lock();
4757 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
Xin Long626d16f2016-04-14 15:35:31 +08004758 rcu_read_unlock();
Xin Long08abb792016-12-15 23:05:52 +08004759 if (!transport)
4760 return -ENOENT;
4761
Xin Long1cceda782016-09-29 02:55:44 +08004762 err = cb(transport, p);
Xin Longcd26da42016-10-31 20:32:31 +08004763 sctp_transport_put(transport);
Xin Long1cceda782016-09-29 02:55:44 +08004764
Xin Long626d16f2016-04-14 15:35:31 +08004765 return err;
4766}
4767EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
4768
4769int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
Xin Longd25adbe2017-09-15 11:02:21 +08004770 int (*cb_done)(struct sctp_transport *, void *),
4771 struct net *net, int *pos, void *p) {
Xin Long626d16f2016-04-14 15:35:31 +08004772 struct rhashtable_iter hti;
Xin Longd25adbe2017-09-15 11:02:21 +08004773 struct sctp_transport *tsp;
Tom Herbert97a6ec42017-12-04 10:31:41 -08004774 int ret = 0;
Xin Long626d16f2016-04-14 15:35:31 +08004775
Xin Longd25adbe2017-09-15 11:02:21 +08004776again:
Tom Herbert97a6ec42017-12-04 10:31:41 -08004777 sctp_transport_walk_start(&hti);
Xin Long626d16f2016-04-14 15:35:31 +08004778
Xin Longd25adbe2017-09-15 11:02:21 +08004779 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
4780 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
4781 if (!sctp_transport_hold(tsp))
Xin Long626d16f2016-04-14 15:35:31 +08004782 continue;
Xin Longd25adbe2017-09-15 11:02:21 +08004783 ret = cb(tsp, p);
4784 if (ret)
Xin Long626d16f2016-04-14 15:35:31 +08004785 break;
Xin Longd25adbe2017-09-15 11:02:21 +08004786 (*pos)++;
4787 sctp_transport_put(tsp);
Xin Long626d16f2016-04-14 15:35:31 +08004788 }
Xin Long626d16f2016-04-14 15:35:31 +08004789 sctp_transport_walk_stop(&hti);
Xin Long53fa1032016-04-14 15:35:35 +08004790
Xin Longd25adbe2017-09-15 11:02:21 +08004791 if (ret) {
4792 if (cb_done && !cb_done(tsp, p)) {
4793 (*pos)++;
4794 sctp_transport_put(tsp);
4795 goto again;
4796 }
4797 sctp_transport_put(tsp);
4798 }
4799
4800 return ret;
Xin Long626d16f2016-04-14 15:35:31 +08004801}
4802EXPORT_SYMBOL_GPL(sctp_for_each_transport);
4803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804/* 7.2.1 Association Status (SCTP_STATUS)
4805
4806 * Applications can retrieve current status information about an
4807 * association, including association state, peer receiver window size,
4808 * number of unacked data chunks, and number of data chunks pending
4809 * receipt. This information is read-only.
4810 */
4811static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4812 char __user *optval,
4813 int __user *optlen)
4814{
4815 struct sctp_status status;
4816 struct sctp_association *asoc = NULL;
4817 struct sctp_transport *transport;
4818 sctp_assoc_t associd;
4819 int retval = 0;
4820
Neil Horman408f22e2007-06-16 14:03:45 -04004821 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 retval = -EINVAL;
4823 goto out;
4824 }
4825
Neil Horman408f22e2007-06-16 14:03:45 -04004826 len = sizeof(status);
4827 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 retval = -EFAULT;
4829 goto out;
4830 }
4831
4832 associd = status.sstat_assoc_id;
4833 asoc = sctp_id2assoc(sk, associd);
4834 if (!asoc) {
4835 retval = -EINVAL;
4836 goto out;
4837 }
4838
4839 transport = asoc->peer.primary_path;
4840
4841 status.sstat_assoc_id = sctp_assoc2id(asoc);
Daniel Borkmann38ab1fa2014-08-28 15:28:26 +02004842 status.sstat_state = sctp_assoc_to_state(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 status.sstat_rwnd = asoc->peer.rwnd;
4844 status.sstat_unackdata = asoc->unack_data;
4845
4846 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
Xin Longcee360a2017-05-31 16:36:31 +08004847 status.sstat_instrms = asoc->stream.incnt;
4848 status.sstat_outstrms = asoc->stream.outcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 status.sstat_fragmentation_point = asoc->frag_point;
4850 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08004851 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4852 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 /* Map ipv4 address into v4-mapped-on-v6 address. */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06004854 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004856 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4858 status.sstat_primary.spinfo_srtt = transport->srtt;
4859 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004860 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
Frank Filz3f7a87d2005-06-20 13:14:57 -07004862 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4863 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4864
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 if (put_user(len, optlen)) {
4866 retval = -EFAULT;
4867 goto out;
4868 }
4869
Daniel Borkmannbb333812013-06-28 19:49:40 +02004870 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
4871 __func__, len, status.sstat_state, status.sstat_rwnd,
4872 status.sstat_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
4874 if (copy_to_user(optval, &status, len)) {
4875 retval = -EFAULT;
4876 goto out;
4877 }
4878
4879out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004880 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881}
4882
4883
4884/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4885 *
4886 * Applications can retrieve information about a specific peer address
4887 * of an association, including its reachability state, congestion
4888 * window, and retransmission timer values. This information is
4889 * read-only.
4890 */
4891static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4892 char __user *optval,
4893 int __user *optlen)
4894{
4895 struct sctp_paddrinfo pinfo;
4896 struct sctp_transport *transport;
4897 int retval = 0;
4898
Neil Horman408f22e2007-06-16 14:03:45 -04004899 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 retval = -EINVAL;
4901 goto out;
4902 }
4903
Neil Horman408f22e2007-06-16 14:03:45 -04004904 len = sizeof(pinfo);
4905 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 retval = -EFAULT;
4907 goto out;
4908 }
4909
4910 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4911 pinfo.spinfo_assoc_id);
4912 if (!transport)
4913 return -EINVAL;
4914
4915 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004916 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 pinfo.spinfo_cwnd = transport->cwnd;
4918 pinfo.spinfo_srtt = transport->srtt;
4919 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004920 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Frank Filz3f7a87d2005-06-20 13:14:57 -07004922 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4923 pinfo.spinfo_state = SCTP_ACTIVE;
4924
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 if (put_user(len, optlen)) {
4926 retval = -EFAULT;
4927 goto out;
4928 }
4929
4930 if (copy_to_user(optval, &pinfo, len)) {
4931 retval = -EFAULT;
4932 goto out;
4933 }
4934
4935out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004936 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937}
4938
4939/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4940 *
4941 * This option is a on/off flag. If enabled no SCTP message
4942 * fragmentation will be performed. Instead if a message being sent
4943 * exceeds the current PMTU size, the message will NOT be sent and
4944 * instead a error will be indicated to the user.
4945 */
4946static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4947 char __user *optval, int __user *optlen)
4948{
4949 int val;
4950
4951 if (len < sizeof(int))
4952 return -EINVAL;
4953
4954 len = sizeof(int);
4955 val = (sctp_sk(sk)->disable_fragments == 1);
4956 if (put_user(len, optlen))
4957 return -EFAULT;
4958 if (copy_to_user(optval, &val, len))
4959 return -EFAULT;
4960 return 0;
4961}
4962
4963/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4964 *
4965 * This socket option is used to specify various notifications and
4966 * ancillary data the user wishes to receive.
4967 */
4968static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
4969 int __user *optlen)
4970{
Jiri Slabya4b8e712016-10-21 14:13:24 +02004971 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00004973 if (len > sizeof(struct sctp_event_subscribe))
4974 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04004975 if (put_user(len, optlen))
4976 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4978 return -EFAULT;
4979 return 0;
4980}
4981
4982/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
4983 *
4984 * This socket option is applicable to the UDP-style socket only. When
4985 * set it will cause associations that are idle for more than the
4986 * specified number of seconds to automatically close. An association
4987 * being idle is defined an association that has NOT sent or received
4988 * user data. The special value of '0' indicates that no automatic
4989 * close of any associations should be performed. The option expects an
4990 * integer defining the number of seconds of idle time before an
4991 * association is closed.
4992 */
4993static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
4994{
4995 /* Applicable to UDP-style socket only */
4996 if (sctp_style(sk, TCP))
4997 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04004998 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005000 len = sizeof(int);
5001 if (put_user(len, optlen))
5002 return -EFAULT;
5003 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 return -EFAULT;
5005 return 0;
5006}
5007
5008/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00005009int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010{
Benjamin Poirier0343c552012-03-08 05:55:58 +00005011 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005012 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 struct socket *sock;
5014 int err = 0;
5015
Xin Longdf80cd92017-10-17 23:26:10 +08005016 /* Do not peel off from one netns to another one. */
5017 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5018 return -EINVAL;
5019
Benjamin Poirier0343c552012-03-08 05:55:58 +00005020 if (!asoc)
5021 return -EINVAL;
5022
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 /* An association cannot be branched off from an already peeled-off
5024 * socket, nor is this supported for tcp style sockets.
5025 */
5026 if (!sctp_style(sk, UDP))
5027 return -EINVAL;
5028
5029 /* Create a new socket. */
5030 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5031 if (err < 0)
5032 return err;
5033
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005034 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005035
5036 /* Make peeled-off sockets more like 1-1 accepted sockets.
5037 * Set the daddr and initialize id to something more random
5038 */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005039 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00005040
5041 /* Populate the fields of the newsk from the oldsk and migrate the
5042 * asoc to the newsk.
5043 */
5044 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08005045
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 *sockp = sock;
5047
5048 return err;
5049}
Benjamin Poirier0343c552012-03-08 05:55:58 +00005050EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005052static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5053 struct file **newfile, unsigned flags)
5054{
5055 struct socket *newsock;
5056 int retval;
5057
5058 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5059 if (retval < 0)
5060 goto out;
5061
5062 /* Map the socket to an unused fd that can be returned to the user. */
5063 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5064 if (retval < 0) {
5065 sock_release(newsock);
5066 goto out;
5067 }
5068
5069 *newfile = sock_alloc_file(newsock, 0, NULL);
5070 if (IS_ERR(*newfile)) {
5071 put_unused_fd(retval);
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005072 retval = PTR_ERR(*newfile);
5073 *newfile = NULL;
5074 return retval;
5075 }
5076
5077 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5078 retval);
5079
5080 peeloff->sd = retval;
5081
5082 if (flags & SOCK_NONBLOCK)
5083 (*newfile)->f_flags |= O_NONBLOCK;
5084out:
5085 return retval;
5086}
5087
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5089{
5090 sctp_peeloff_arg_t peeloff;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005091 struct file *newfile = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093
Neil Horman408f22e2007-06-16 14:03:45 -04005094 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005096 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 if (copy_from_user(&peeloff, optval, len))
5098 return -EFAULT;
5099
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005100 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 if (retval < 0)
5102 goto out;
5103
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04005105 if (put_user(len, optlen)) {
5106 fput(newfile);
5107 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04005108 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04005109 }
Neil Horman2cb5c8e2017-06-30 13:32:57 -04005110
5111 if (copy_to_user(optval, &peeloff, len)) {
5112 fput(newfile);
5113 put_unused_fd(retval);
5114 return -EFAULT;
5115 }
5116 fd_install(retval, newfile);
5117out:
5118 return retval;
5119}
5120
5121static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5122 char __user *optval, int __user *optlen)
5123{
5124 sctp_peeloff_flags_arg_t peeloff;
5125 struct file *newfile = NULL;
5126 int retval = 0;
5127
5128 if (len < sizeof(sctp_peeloff_flags_arg_t))
5129 return -EINVAL;
5130 len = sizeof(sctp_peeloff_flags_arg_t);
5131 if (copy_from_user(&peeloff, optval, len))
5132 return -EFAULT;
5133
5134 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5135 &newfile, peeloff.flags);
5136 if (retval < 0)
5137 goto out;
5138
5139 /* Return the fd mapped to the new socket. */
5140 if (put_user(len, optlen)) {
5141 fput(newfile);
5142 put_unused_fd(retval);
5143 return -EFAULT;
5144 }
5145
Al Viro56b31d12012-08-18 00:25:51 -04005146 if (copy_to_user(optval, &peeloff, len)) {
5147 fput(newfile);
5148 put_unused_fd(retval);
5149 return -EFAULT;
5150 }
5151 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152out:
5153 return retval;
5154}
5155
5156/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5157 *
5158 * Applications can enable or disable heartbeats for any peer address of
5159 * an association, modify an address's heartbeat interval, force a
5160 * heartbeat to be sent immediately, and adjust the address's maximum
5161 * number of retransmissions sent before an address is considered
5162 * unreachable. The following structure is used to access and modify an
5163 * address's parameters:
5164 *
5165 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005166 * sctp_assoc_t spp_assoc_id;
5167 * struct sockaddr_storage spp_address;
5168 * uint32_t spp_hbinterval;
5169 * uint16_t spp_pathmaxrxt;
5170 * uint32_t spp_pathmtu;
5171 * uint32_t spp_sackdelay;
5172 * uint32_t spp_flags;
5173 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08005175 * spp_assoc_id - (one-to-many style socket) This is filled in the
5176 * application, and identifies the association for
5177 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 * spp_address - This specifies which address is of interest.
5179 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005180 * in milliseconds. If a value of zero
5181 * is present in this field then no changes are to
5182 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 * spp_pathmaxrxt - This contains the maximum number of
5184 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08005185 * considered unreachable. If a value of zero
5186 * is present in this field then no changes are to
5187 * be made to this parameter.
5188 * spp_pathmtu - When Path MTU discovery is disabled the value
5189 * specified here will be the "fixed" path mtu.
5190 * Note that if the spp_address field is empty
5191 * then all associations on this address will
5192 * have this fixed path mtu set upon them.
5193 *
5194 * spp_sackdelay - When delayed sack is enabled, this value specifies
5195 * the number of milliseconds that sacks will be delayed
5196 * for. This value will apply to all addresses of an
5197 * association if the spp_address field is empty. Note
5198 * also, that if delayed sack is enabled and this
5199 * value is set to 0, no change is made to the last
5200 * recorded delayed sack timer value.
5201 *
5202 * spp_flags - These flags are used to control various features
5203 * on an association. The flag field may contain
5204 * zero or more of the following options.
5205 *
5206 * SPP_HB_ENABLE - Enable heartbeats on the
5207 * specified address. Note that if the address
5208 * field is empty all addresses for the association
5209 * have heartbeats enabled upon them.
5210 *
5211 * SPP_HB_DISABLE - Disable heartbeats on the
5212 * speicifed address. Note that if the address
5213 * field is empty all addresses for the association
5214 * will have their heartbeats disabled. Note also
5215 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5216 * mutually exclusive, only one of these two should
5217 * be specified. Enabling both fields will have
5218 * undetermined results.
5219 *
5220 * SPP_HB_DEMAND - Request a user initiated heartbeat
5221 * to be made immediately.
5222 *
5223 * SPP_PMTUD_ENABLE - This field will enable PMTU
5224 * discovery upon the specified address. Note that
5225 * if the address feild is empty then all addresses
5226 * on the association are effected.
5227 *
5228 * SPP_PMTUD_DISABLE - This field will disable PMTU
5229 * discovery upon the specified address. Note that
5230 * if the address feild is empty then all addresses
5231 * on the association are effected. Not also that
5232 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5233 * exclusive. Enabling both will have undetermined
5234 * results.
5235 *
5236 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5237 * on delayed sack. The time specified in spp_sackdelay
5238 * is used to specify the sack delay for this address. Note
5239 * that if spp_address is empty then all addresses will
5240 * enable delayed sack and take on the sack delay
5241 * value specified in spp_sackdelay.
5242 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5243 * off delayed sack. If the spp_address field is blank then
5244 * delayed sack is disabled for the entire association. Note
5245 * also that this field is mutually exclusive to
5246 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5247 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 */
5249static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08005250 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251{
Frank Filz52ccb8e2005-12-22 11:36:46 -08005252 struct sctp_paddrparams params;
5253 struct sctp_transport *trans = NULL;
5254 struct sctp_association *asoc = NULL;
5255 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
Neil Horman408f22e2007-06-16 14:03:45 -04005257 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005259 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 if (copy_from_user(&params, optval, len))
5261 return -EFAULT;
5262
Frank Filz52ccb8e2005-12-22 11:36:46 -08005263 /* If an address other than INADDR_ANY is specified, and
5264 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 */
wangweidongcb3f8372013-12-23 12:16:50 +08005266 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08005267 trans = sctp_addr_id2transport(sk, &params.spp_address,
5268 params.spp_assoc_id);
5269 if (!trans) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005270 pr_debug("%s: failed no transport\n", __func__);
Frank Filz52ccb8e2005-12-22 11:36:46 -08005271 return -EINVAL;
5272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 }
5274
Frank Filz52ccb8e2005-12-22 11:36:46 -08005275 /* Get association, if assoc_id != 0 and the socket is a one
5276 * to many style socket, and an association was not found, then
5277 * the id was invalid.
5278 */
5279 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5280 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005281 pr_debug("%s: failed no association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08005283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284
Frank Filz52ccb8e2005-12-22 11:36:46 -08005285 if (trans) {
5286 /* Fetch transport values. */
5287 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5288 params.spp_pathmtu = trans->pathmtu;
5289 params.spp_pathmaxrxt = trans->pathmaxrxt;
5290 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291
Frank Filz52ccb8e2005-12-22 11:36:46 -08005292 /*draft-11 doesn't say what to return in spp_flags*/
5293 params.spp_flags = trans->param_flags;
5294 } else if (asoc) {
5295 /* Fetch association values. */
5296 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5297 params.spp_pathmtu = asoc->pathmtu;
5298 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5299 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300
Frank Filz52ccb8e2005-12-22 11:36:46 -08005301 /*draft-11 doesn't say what to return in spp_flags*/
5302 params.spp_flags = asoc->param_flags;
5303 } else {
5304 /* Fetch socket values. */
5305 params.spp_hbinterval = sp->hbinterval;
5306 params.spp_pathmtu = sp->pathmtu;
5307 params.spp_sackdelay = sp->sackdelay;
5308 params.spp_pathmaxrxt = sp->pathmaxrxt;
5309
5310 /*draft-11 doesn't say what to return in spp_flags*/
5311 params.spp_flags = sp->param_flags;
5312 }
5313
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 if (copy_to_user(optval, &params, len))
5315 return -EFAULT;
5316
5317 if (put_user(len, optlen))
5318 return -EFAULT;
5319
5320 return 0;
5321}
5322
Wei Yongjund364d922008-05-09 15:13:26 -07005323/*
5324 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08005325 *
Wei Yongjund364d922008-05-09 15:13:26 -07005326 * This option will effect the way delayed acks are performed. This
5327 * option allows you to get or set the delayed ack time, in
5328 * milliseconds. It also allows changing the delayed ack frequency.
5329 * Changing the frequency to 1 disables the delayed sack algorithm. If
5330 * the assoc_id is 0, then this sets or gets the endpoints default
5331 * values. If the assoc_id field is non-zero, then the set or get
5332 * effects the specified association for the one to many model (the
5333 * assoc_id field is ignored by the one to one model). Note that if
5334 * sack_delay or sack_freq are 0 when setting this option, then the
5335 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08005336 *
Wei Yongjund364d922008-05-09 15:13:26 -07005337 * struct sctp_sack_info {
5338 * sctp_assoc_t sack_assoc_id;
5339 * uint32_t sack_delay;
5340 * uint32_t sack_freq;
5341 * };
Frank Filz77086102005-12-22 11:37:30 -08005342 *
Wei Yongjund364d922008-05-09 15:13:26 -07005343 * sack_assoc_id - This parameter, indicates which association the user
5344 * is performing an action upon. Note that if this field's value is
5345 * zero then the endpoints default value is changed (effecting future
5346 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08005347 *
Wei Yongjund364d922008-05-09 15:13:26 -07005348 * sack_delay - This parameter contains the number of milliseconds that
5349 * the user is requesting the delayed ACK timer be set to. Note that
5350 * this value is defined in the standard to be between 200 and 500
5351 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08005352 *
Wei Yongjund364d922008-05-09 15:13:26 -07005353 * sack_freq - This parameter contains the number of packets that must
5354 * be received before a sack is sent without waiting for the delay
5355 * timer to expire. The default value for this is 2, setting this
5356 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08005357 */
Wei Yongjund364d922008-05-09 15:13:26 -07005358static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08005359 char __user *optval,
5360 int __user *optlen)
5361{
Wei Yongjund364d922008-05-09 15:13:26 -07005362 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08005363 struct sctp_association *asoc = NULL;
5364 struct sctp_sock *sp = sctp_sk(sk);
5365
Wei Yongjund364d922008-05-09 15:13:26 -07005366 if (len >= sizeof(struct sctp_sack_info)) {
5367 len = sizeof(struct sctp_sack_info);
5368
5369 if (copy_from_user(&params, optval, len))
5370 return -EFAULT;
5371 } else if (len == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05005372 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005373 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005374 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005375 "Use struct sctp_sack_info instead\n",
5376 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07005377 if (copy_from_user(&params, optval, len))
5378 return -EFAULT;
5379 } else
wangweidongcb3f8372013-12-23 12:16:50 +08005380 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08005381
Wei Yongjund364d922008-05-09 15:13:26 -07005382 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08005383 * to many style socket, and an association was not found, then
5384 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005385 */
Wei Yongjund364d922008-05-09 15:13:26 -07005386 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5387 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08005388 return -EINVAL;
5389
5390 if (asoc) {
5391 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005392 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5393 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08005394 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07005395 params.sack_freq = asoc->sackfreq;
5396
5397 } else {
5398 params.sack_delay = 0;
5399 params.sack_freq = 1;
5400 }
Frank Filz77086102005-12-22 11:37:30 -08005401 } else {
5402 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005403 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5404 params.sack_delay = sp->sackdelay;
5405 params.sack_freq = sp->sackfreq;
5406 } else {
5407 params.sack_delay = 0;
5408 params.sack_freq = 1;
5409 }
Frank Filz77086102005-12-22 11:37:30 -08005410 }
5411
5412 if (copy_to_user(optval, &params, len))
5413 return -EFAULT;
5414
5415 if (put_user(len, optlen))
5416 return -EFAULT;
5417
5418 return 0;
5419}
5420
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5422 *
5423 * Applications can specify protocol parameters for the default association
5424 * initialization. The option name argument to setsockopt() and getsockopt()
5425 * is SCTP_INITMSG.
5426 *
5427 * Setting initialization parameters is effective only on an unconnected
5428 * socket (for UDP-style sockets only future associations are effected
5429 * by the change). With TCP-style sockets, this option is inherited by
5430 * sockets derived from a listener socket.
5431 */
5432static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5433{
Neil Horman408f22e2007-06-16 14:03:45 -04005434 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005436 len = sizeof(struct sctp_initmsg);
5437 if (put_user(len, optlen))
5438 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5440 return -EFAULT;
5441 return 0;
5442}
5443
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005445static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5446 char __user *optval, int __user *optlen)
5447{
5448 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005449 int cnt = 0;
5450 struct sctp_getaddrs getaddrs;
5451 struct sctp_transport *from;
5452 void __user *to;
5453 union sctp_addr temp;
5454 struct sctp_sock *sp = sctp_sk(sk);
5455 int addrlen;
5456 size_t space_left;
5457 int bytes_copied;
5458
5459 if (len < sizeof(struct sctp_getaddrs))
5460 return -EINVAL;
5461
5462 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5463 return -EFAULT;
5464
5465 /* For UDP-style sockets, id specifies the association to query. */
5466 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5467 if (!asoc)
5468 return -EINVAL;
5469
wangweidongcb3f8372013-12-23 12:16:50 +08005470 to = optval + offsetof(struct sctp_getaddrs, addrs);
5471 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005472
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07005473 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5474 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08005475 memcpy(&temp, &from->ipaddr, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005476 addrlen = sctp_get_pf_specific(sk->sk_family)
5477 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005478 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005479 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005480 if (copy_to_user(to, &temp, addrlen))
5481 return -EFAULT;
5482 to += addrlen;
5483 cnt++;
5484 space_left -= addrlen;
5485 }
5486
5487 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5488 return -EFAULT;
5489 bytes_copied = ((char __user *)to) - optval;
5490 if (put_user(bytes_copied, optlen))
5491 return -EFAULT;
5492
5493 return 0;
5494}
5495
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005496static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5497 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005498{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005499 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005500 union sctp_addr temp;
5501 int cnt = 0;
5502 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005503 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005504
Vlad Yasevich29303542007-09-16 16:02:12 -07005505 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005506 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07005507 if (!addr->valid)
5508 continue;
5509
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005510 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08005511 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005512 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005513 if ((PF_INET6 == sk->sk_family) &&
5514 inet_v6_ipv6only(sk) &&
5515 (AF_INET == addr->a.sa.sa_family))
5516 continue;
Al Viro6244be42006-11-20 17:21:44 -08005517 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05005518 if (!temp.v4.sin_port)
5519 temp.v4.sin_port = htons(port);
5520
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005521 addrlen = sctp_get_pf_specific(sk->sk_family)
5522 ->addr_to_user(sctp_sk(sk), &temp);
5523
Vlad Yasevich29303542007-09-16 16:02:12 -07005524 if (space_left < addrlen) {
5525 cnt = -ENOMEM;
5526 break;
5527 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005528 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08005529
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005530 to += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005531 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005532 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04005533 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005534 }
Vlad Yasevich29303542007-09-16 16:02:12 -07005535 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005536
5537 return cnt;
5538}
5539
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005541static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5542 char __user *optval, int __user *optlen)
5543{
5544 struct sctp_bind_addr *bp;
5545 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005546 int cnt = 0;
5547 struct sctp_getaddrs getaddrs;
5548 struct sctp_sockaddr_entry *addr;
5549 void __user *to;
5550 union sctp_addr temp;
5551 struct sctp_sock *sp = sctp_sk(sk);
5552 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005553 int err = 0;
5554 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005555 int bytes_copied = 0;
5556 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005557 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005558
Neil Horman408f22e2007-06-16 14:03:45 -04005559 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005560 return -EINVAL;
5561
5562 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5563 return -EFAULT;
5564
5565 /*
5566 * For UDP-style sockets, id specifies the association to query.
5567 * If the id field is set to the value '0' then the locally bound
5568 * addresses are returned without regard to any particular
5569 * association.
5570 */
5571 if (0 == getaddrs.assoc_id) {
5572 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005573 } else {
5574 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5575 if (!asoc)
5576 return -EINVAL;
5577 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005578 }
5579
wangweidongcb3f8372013-12-23 12:16:50 +08005580 to = optval + offsetof(struct sctp_getaddrs, addrs);
5581 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Neil Horman186e2342007-06-18 19:59:16 -04005582
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02005583 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005584 if (!addrs)
5585 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005586
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005587 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5588 * addresses from the global local address list.
5589 */
5590 if (sctp_list_single_entry(&bp->address_list)) {
5591 addr = list_entry(bp->address_list.next,
5592 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04005593 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005594 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5595 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005596 if (cnt < 0) {
5597 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005598 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005599 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005600 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005601 }
5602 }
5603
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005604 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005605 /* Protection on the bound address list is not needed since
5606 * in the socket option context we hold a socket lock and
5607 * thus the bound address list can't change.
5608 */
5609 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08005610 memcpy(&temp, &addr->a, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005611 addrlen = sctp_get_pf_specific(sk->sk_family)
5612 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005613 if (space_left < addrlen) {
5614 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07005615 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005616 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005617 memcpy(buf, &temp, addrlen);
5618 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005619 bytes_copied += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005620 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005621 space_left -= addrlen;
5622 }
5623
5624copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005625 if (copy_to_user(to, addrs, bytes_copied)) {
5626 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005627 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005628 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005629 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
5630 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005631 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005632 }
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005633 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005634 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005635out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005636 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005637 return err;
5638}
5639
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
5641 *
5642 * Requests that the local SCTP stack use the enclosed peer address as
5643 * the association primary. The enclosed address must be one of the
5644 * association peer's addresses.
5645 */
5646static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
5647 char __user *optval, int __user *optlen)
5648{
5649 struct sctp_prim prim;
5650 struct sctp_association *asoc;
5651 struct sctp_sock *sp = sctp_sk(sk);
5652
Neil Horman408f22e2007-06-16 14:03:45 -04005653 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 return -EINVAL;
5655
Neil Horman408f22e2007-06-16 14:03:45 -04005656 len = sizeof(struct sctp_prim);
5657
5658 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 return -EFAULT;
5660
5661 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
5662 if (!asoc)
5663 return -EINVAL;
5664
5665 if (!asoc->peer.primary_path)
5666 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005667
Al Viro8cec6b82006-11-20 17:23:01 -08005668 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
5669 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005671 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 (union sctp_addr *)&prim.ssp_addr);
5673
Neil Horman408f22e2007-06-16 14:03:45 -04005674 if (put_user(len, optlen))
5675 return -EFAULT;
5676 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 return -EFAULT;
5678
5679 return 0;
5680}
5681
5682/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005683 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005685 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 * Indication parameter for all future INIT and INIT-ACK exchanges.
5687 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005688static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 char __user *optval, int __user *optlen)
5690{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005691 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
Neil Horman408f22e2007-06-16 14:03:45 -04005693 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 return -EINVAL;
5695
Neil Horman408f22e2007-06-16 14:03:45 -04005696 len = sizeof(struct sctp_setadaptation);
5697
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005698 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04005699
5700 if (put_user(len, optlen))
5701 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005702 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07005704
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 return 0;
5706}
5707
5708/*
5709 *
5710 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
5711 *
5712 * Applications that wish to use the sendto() system call may wish to
5713 * specify a default set of parameters that would normally be supplied
5714 * through the inclusion of ancillary data. This socket option allows
5715 * such an application to set the default sctp_sndrcvinfo structure.
5716
5717
5718 * The application that wishes to use this socket option simply passes
5719 * in to this call the sctp_sndrcvinfo structure defined in Section
5720 * 5.2.2) The input parameters accepted by this call include
5721 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
5722 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
5723 * to this call if the caller is using the UDP model.
5724 *
5725 * For getsockopt, it get the default sctp_sndrcvinfo structure.
5726 */
5727static int sctp_getsockopt_default_send_param(struct sock *sk,
5728 int len, char __user *optval,
5729 int __user *optlen)
5730{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005732 struct sctp_association *asoc;
5733 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005735 if (len < sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005737
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005738 len = sizeof(info);
Neil Horman408f22e2007-06-16 14:03:45 -04005739
5740 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 return -EFAULT;
5742
5743 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
5744 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
5745 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 if (asoc) {
5747 info.sinfo_stream = asoc->default_stream;
5748 info.sinfo_flags = asoc->default_flags;
5749 info.sinfo_ppid = asoc->default_ppid;
5750 info.sinfo_context = asoc->default_context;
5751 info.sinfo_timetolive = asoc->default_timetolive;
5752 } else {
5753 info.sinfo_stream = sp->default_stream;
5754 info.sinfo_flags = sp->default_flags;
5755 info.sinfo_ppid = sp->default_ppid;
5756 info.sinfo_context = sp->default_context;
5757 info.sinfo_timetolive = sp->default_timetolive;
5758 }
5759
Neil Horman408f22e2007-06-16 14:03:45 -04005760 if (put_user(len, optlen))
5761 return -EFAULT;
5762 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 return -EFAULT;
5764
5765 return 0;
5766}
5767
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005768/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
5769 * (SCTP_DEFAULT_SNDINFO)
5770 */
5771static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
5772 char __user *optval,
5773 int __user *optlen)
5774{
5775 struct sctp_sock *sp = sctp_sk(sk);
5776 struct sctp_association *asoc;
5777 struct sctp_sndinfo info;
5778
5779 if (len < sizeof(info))
5780 return -EINVAL;
5781
5782 len = sizeof(info);
5783
5784 if (copy_from_user(&info, optval, len))
5785 return -EFAULT;
5786
5787 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
5788 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
5789 return -EINVAL;
5790 if (asoc) {
5791 info.snd_sid = asoc->default_stream;
5792 info.snd_flags = asoc->default_flags;
5793 info.snd_ppid = asoc->default_ppid;
5794 info.snd_context = asoc->default_context;
5795 } else {
5796 info.snd_sid = sp->default_stream;
5797 info.snd_flags = sp->default_flags;
5798 info.snd_ppid = sp->default_ppid;
5799 info.snd_context = sp->default_context;
5800 }
5801
5802 if (put_user(len, optlen))
5803 return -EFAULT;
5804 if (copy_to_user(optval, &info, len))
5805 return -EFAULT;
5806
5807 return 0;
5808}
5809
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810/*
5811 *
5812 * 7.1.5 SCTP_NODELAY
5813 *
5814 * Turn on/off any Nagle-like algorithm. This means that packets are
5815 * generally sent as soon as possible and no unnecessary delays are
5816 * introduced, at the cost of more packets in the network. Expects an
5817 * integer boolean flag.
5818 */
5819
5820static int sctp_getsockopt_nodelay(struct sock *sk, int len,
5821 char __user *optval, int __user *optlen)
5822{
5823 int val;
5824
5825 if (len < sizeof(int))
5826 return -EINVAL;
5827
5828 len = sizeof(int);
5829 val = (sctp_sk(sk)->nodelay == 1);
5830 if (put_user(len, optlen))
5831 return -EFAULT;
5832 if (copy_to_user(optval, &val, len))
5833 return -EFAULT;
5834 return 0;
5835}
5836
5837/*
5838 *
5839 * 7.1.1 SCTP_RTOINFO
5840 *
5841 * The protocol parameters used to initialize and bound retransmission
5842 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
5843 * and modify these parameters.
5844 * All parameters are time values, in milliseconds. A value of 0, when
5845 * modifying the parameters, indicates that the current value should not
5846 * be changed.
5847 *
5848 */
5849static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
5850 char __user *optval,
5851 int __user *optlen) {
5852 struct sctp_rtoinfo rtoinfo;
5853 struct sctp_association *asoc;
5854
Neil Horman408f22e2007-06-16 14:03:45 -04005855 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 return -EINVAL;
5857
Neil Horman408f22e2007-06-16 14:03:45 -04005858 len = sizeof(struct sctp_rtoinfo);
5859
5860 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 return -EFAULT;
5862
5863 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
5864
5865 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
5866 return -EINVAL;
5867
5868 /* Values corresponding to the specific association. */
5869 if (asoc) {
5870 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
5871 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
5872 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
5873 } else {
5874 /* Values corresponding to the endpoint. */
5875 struct sctp_sock *sp = sctp_sk(sk);
5876
5877 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
5878 rtoinfo.srto_max = sp->rtoinfo.srto_max;
5879 rtoinfo.srto_min = sp->rtoinfo.srto_min;
5880 }
5881
5882 if (put_user(len, optlen))
5883 return -EFAULT;
5884
5885 if (copy_to_user(optval, &rtoinfo, len))
5886 return -EFAULT;
5887
5888 return 0;
5889}
5890
5891/*
5892 *
5893 * 7.1.2 SCTP_ASSOCINFO
5894 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02005895 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 * of the association.
5897 * Returns an error if the new association retransmission value is
5898 * greater than the sum of the retransmission value of the peer.
5899 * See [SCTP] for more information.
5900 *
5901 */
5902static int sctp_getsockopt_associnfo(struct sock *sk, int len,
5903 char __user *optval,
5904 int __user *optlen)
5905{
5906
5907 struct sctp_assocparams assocparams;
5908 struct sctp_association *asoc;
5909 struct list_head *pos;
5910 int cnt = 0;
5911
Neil Horman408f22e2007-06-16 14:03:45 -04005912 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 return -EINVAL;
5914
Neil Horman408f22e2007-06-16 14:03:45 -04005915 len = sizeof(struct sctp_assocparams);
5916
5917 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 return -EFAULT;
5919
5920 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
5921
5922 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
5923 return -EINVAL;
5924
5925 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07005926 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
5928 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
5929 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
Daniel Borkmann52db8822013-06-25 18:17:27 +02005930 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
5932 list_for_each(pos, &asoc->peer.transport_addr_list) {
wangweidongcb3f8372013-12-23 12:16:50 +08005933 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 }
5935
5936 assocparams.sasoc_number_peer_destinations = cnt;
5937 } else {
5938 /* Values corresponding to the endpoint */
5939 struct sctp_sock *sp = sctp_sk(sk);
5940
5941 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
5942 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
5943 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
5944 assocparams.sasoc_cookie_life =
5945 sp->assocparams.sasoc_cookie_life;
5946 assocparams.sasoc_number_peer_destinations =
5947 sp->assocparams.
5948 sasoc_number_peer_destinations;
5949 }
5950
5951 if (put_user(len, optlen))
5952 return -EFAULT;
5953
5954 if (copy_to_user(optval, &assocparams, len))
5955 return -EFAULT;
5956
5957 return 0;
5958}
5959
5960/*
5961 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
5962 *
5963 * This socket option is a boolean flag which turns on or off mapped V4
5964 * addresses. If this option is turned on and the socket is type
5965 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
5966 * If this option is turned off, then no mapping will be done of V4
5967 * addresses and a user will receive both PF_INET6 and PF_INET type
5968 * addresses on the socket.
5969 */
5970static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
5971 char __user *optval, int __user *optlen)
5972{
5973 int val;
5974 struct sctp_sock *sp = sctp_sk(sk);
5975
5976 if (len < sizeof(int))
5977 return -EINVAL;
5978
5979 len = sizeof(int);
5980 val = sp->v4mapped;
5981 if (put_user(len, optlen))
5982 return -EFAULT;
5983 if (copy_to_user(optval, &val, len))
5984 return -EFAULT;
5985
5986 return 0;
5987}
5988
5989/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005990 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
5991 * (chapter and verse is quoted at sctp_setsockopt_context())
5992 */
5993static int sctp_getsockopt_context(struct sock *sk, int len,
5994 char __user *optval, int __user *optlen)
5995{
5996 struct sctp_assoc_value params;
5997 struct sctp_sock *sp;
5998 struct sctp_association *asoc;
5999
Neil Horman408f22e2007-06-16 14:03:45 -04006000 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006001 return -EINVAL;
6002
Neil Horman408f22e2007-06-16 14:03:45 -04006003 len = sizeof(struct sctp_assoc_value);
6004
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006005 if (copy_from_user(&params, optval, len))
6006 return -EFAULT;
6007
6008 sp = sctp_sk(sk);
6009
6010 if (params.assoc_id != 0) {
6011 asoc = sctp_id2assoc(sk, params.assoc_id);
6012 if (!asoc)
6013 return -EINVAL;
6014 params.assoc_value = asoc->default_rcv_context;
6015 } else {
6016 params.assoc_value = sp->default_rcv_context;
6017 }
6018
6019 if (put_user(len, optlen))
6020 return -EFAULT;
6021 if (copy_to_user(optval, &params, len))
6022 return -EFAULT;
6023
6024 return 0;
6025}
6026
6027/*
Wei Yongjune89c2092008-12-25 16:54:58 -08006028 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6029 * This option will get or set the maximum size to put in any outgoing
6030 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 * fragmented by SCTP into the specified size. Note that the underlying
6032 * SCTP implementation may fragment into smaller sized chunks when the
6033 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08006034 * the user. The default value for this option is '0' which indicates
6035 * the user is NOT limiting fragmentation and only the PMTU will effect
6036 * SCTP's choice of DATA chunk size. Note also that values set larger
6037 * than the maximum size of an IP datagram will effectively let SCTP
6038 * control fragmentation (i.e. the same as setting this option to 0).
6039 *
6040 * The following structure is used to access and modify this parameter:
6041 *
6042 * struct sctp_assoc_value {
6043 * sctp_assoc_t assoc_id;
6044 * uint32_t assoc_value;
6045 * };
6046 *
6047 * assoc_id: This parameter is ignored for one-to-one style sockets.
6048 * For one-to-many style sockets this parameter indicates which
6049 * association the user is performing an action upon. Note that if
6050 * this field's value is zero then the endpoints default value is
6051 * changed (effecting future associations only).
6052 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 */
6054static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6055 char __user *optval, int __user *optlen)
6056{
Wei Yongjune89c2092008-12-25 16:54:58 -08006057 struct sctp_assoc_value params;
6058 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
Wei Yongjune89c2092008-12-25 16:54:58 -08006060 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006061 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006062 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006063 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006064 "Use struct sctp_assoc_value instead\n",
6065 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08006066 params.assoc_id = 0;
6067 } else if (len >= sizeof(struct sctp_assoc_value)) {
6068 len = sizeof(struct sctp_assoc_value);
6069 if (copy_from_user(&params, optval, sizeof(params)))
6070 return -EFAULT;
6071 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 return -EINVAL;
6073
Wei Yongjune89c2092008-12-25 16:54:58 -08006074 asoc = sctp_id2assoc(sk, params.assoc_id);
6075 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
6076 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
Wei Yongjune89c2092008-12-25 16:54:58 -08006078 if (asoc)
6079 params.assoc_value = asoc->frag_point;
6080 else
6081 params.assoc_value = sctp_sk(sk)->user_frag;
6082
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 if (put_user(len, optlen))
6084 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08006085 if (len == sizeof(int)) {
6086 if (copy_to_user(optval, &params.assoc_value, len))
6087 return -EFAULT;
6088 } else {
6089 if (copy_to_user(optval, &params, len))
6090 return -EFAULT;
6091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092
6093 return 0;
6094}
6095
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006096/*
6097 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6098 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6099 */
6100static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6101 char __user *optval, int __user *optlen)
6102{
6103 int val;
6104
6105 if (len < sizeof(int))
6106 return -EINVAL;
6107
6108 len = sizeof(int);
6109
6110 val = sctp_sk(sk)->frag_interleave;
6111 if (put_user(len, optlen))
6112 return -EFAULT;
6113 if (copy_to_user(optval, &val, len))
6114 return -EFAULT;
6115
6116 return 0;
6117}
6118
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006119/*
6120 * 7.1.25. Set or Get the sctp partial delivery point
6121 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6122 */
6123static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6124 char __user *optval,
6125 int __user *optlen)
6126{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09006127 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006128
6129 if (len < sizeof(u32))
6130 return -EINVAL;
6131
6132 len = sizeof(u32);
6133
6134 val = sctp_sk(sk)->pd_point;
6135 if (put_user(len, optlen))
6136 return -EFAULT;
6137 if (copy_to_user(optval, &val, len))
6138 return -EFAULT;
6139
Wei Yongjun7d743b72010-12-14 16:10:41 +00006140 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006141}
6142
Vlad Yasevich70331572007-03-23 11:34:36 -07006143/*
6144 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6145 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6146 */
6147static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6148 char __user *optval,
6149 int __user *optlen)
6150{
Neil Horman219b99a2008-03-05 13:44:46 -08006151 struct sctp_assoc_value params;
6152 struct sctp_sock *sp;
6153 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07006154
Neil Horman219b99a2008-03-05 13:44:46 -08006155 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05006156 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05006157 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05006158 "Use of int in max_burst socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05006159 "Use struct sctp_assoc_value instead\n",
6160 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08006161 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00006162 } else if (len >= sizeof(struct sctp_assoc_value)) {
6163 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08006164 if (copy_from_user(&params, optval, len))
6165 return -EFAULT;
6166 } else
6167 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07006168
Neil Horman219b99a2008-03-05 13:44:46 -08006169 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07006170
Neil Horman219b99a2008-03-05 13:44:46 -08006171 if (params.assoc_id != 0) {
6172 asoc = sctp_id2assoc(sk, params.assoc_id);
6173 if (!asoc)
6174 return -EINVAL;
6175 params.assoc_value = asoc->max_burst;
6176 } else
6177 params.assoc_value = sp->max_burst;
6178
6179 if (len == sizeof(int)) {
6180 if (copy_to_user(optval, &params.assoc_value, len))
6181 return -EFAULT;
6182 } else {
6183 if (copy_to_user(optval, &params, len))
6184 return -EFAULT;
6185 }
6186
6187 return 0;
6188
Vlad Yasevich70331572007-03-23 11:34:36 -07006189}
6190
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006191static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6192 char __user *optval, int __user *optlen)
6193{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006194 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006195 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006196 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006197 __u16 data_len = 0;
6198 u32 num_idents;
Xin Long7a84bd42016-02-03 23:33:30 +08006199 int i;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006200
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006201 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006202 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006203
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006204 hmacs = ep->auth_hmacs_list;
Xin Long3c918702017-06-30 11:52:16 +08006205 data_len = ntohs(hmacs->param_hdr.length) -
6206 sizeof(struct sctp_paramhdr);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006207
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006208 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006209 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006210
6211 len = sizeof(struct sctp_hmacalgo) + data_len;
6212 num_idents = data_len / sizeof(u16);
6213
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006214 if (put_user(len, optlen))
6215 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006216 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006217 return -EFAULT;
Xin Long7a84bd42016-02-03 23:33:30 +08006218 for (i = 0; i < num_idents; i++) {
6219 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6220
6221 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6222 return -EFAULT;
6223 }
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006224 return 0;
6225}
6226
6227static int sctp_getsockopt_active_key(struct sock *sk, int len,
6228 char __user *optval, int __user *optlen)
6229{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006230 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006231 struct sctp_authkeyid val;
6232 struct sctp_association *asoc;
6233
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006234 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006235 return -EACCES;
6236
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006237 if (len < sizeof(struct sctp_authkeyid))
6238 return -EINVAL;
6239 if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid)))
6240 return -EFAULT;
6241
6242 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6243 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6244 return -EINVAL;
6245
6246 if (asoc)
6247 val.scact_keynumber = asoc->active_key_id;
6248 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006249 val.scact_keynumber = ep->active_key_id;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006250
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006251 len = sizeof(struct sctp_authkeyid);
6252 if (put_user(len, optlen))
6253 return -EFAULT;
6254 if (copy_to_user(optval, &val, len))
6255 return -EFAULT;
6256
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006257 return 0;
6258}
6259
6260static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6261 char __user *optval, int __user *optlen)
6262{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006263 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006264 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006265 struct sctp_authchunks val;
6266 struct sctp_association *asoc;
6267 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006268 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006269 char __user *to;
6270
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006271 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006272 return -EACCES;
6273
6274 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006275 return -EINVAL;
6276
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006277 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006278 return -EFAULT;
6279
Al Viro411223c2007-10-14 19:21:20 +01006280 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006281 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6282 if (!asoc)
6283 return -EINVAL;
6284
6285 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006286 if (!ch)
6287 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006288
6289 /* See if the user provided enough room for all the data */
Xin Long3c918702017-06-30 11:52:16 +08006290 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevichb40db682008-02-27 14:40:37 -05006291 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006292 return -EINVAL;
6293
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006294 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006295 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006296num:
6297 len = sizeof(struct sctp_authchunks) + num_chunks;
wangweidong8d726512013-12-23 12:16:53 +08006298 if (put_user(len, optlen))
6299 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006300 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6301 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006302 return 0;
6303}
6304
6305static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6306 char __user *optval, int __user *optlen)
6307{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006308 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006309 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006310 struct sctp_authchunks val;
6311 struct sctp_association *asoc;
6312 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006313 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006314 char __user *to;
6315
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006316 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006317 return -EACCES;
6318
6319 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006320 return -EINVAL;
6321
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006322 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006323 return -EFAULT;
6324
Al Viro411223c2007-10-14 19:21:20 +01006325 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006326 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6327 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6328 return -EINVAL;
6329
6330 if (asoc)
wangweidong26ac8e52013-12-23 12:16:51 +08006331 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006332 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006333 ch = ep->auth_chunk_list;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006334
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006335 if (!ch)
6336 goto num;
6337
Xin Long3c918702017-06-30 11:52:16 +08006338 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006339 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006340 return -EINVAL;
6341
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006342 if (copy_to_user(to, ch->chunks, num_chunks))
6343 return -EFAULT;
6344num:
6345 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006346 if (put_user(len, optlen))
6347 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006348 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6349 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006350
6351 return 0;
6352}
6353
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08006354/*
6355 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6356 * This option gets the current number of associations that are attached
6357 * to a one-to-many style socket. The option value is an uint32_t.
6358 */
6359static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6360 char __user *optval, int __user *optlen)
6361{
6362 struct sctp_sock *sp = sctp_sk(sk);
6363 struct sctp_association *asoc;
6364 u32 val = 0;
6365
6366 if (sctp_style(sk, TCP))
6367 return -EOPNOTSUPP;
6368
6369 if (len < sizeof(u32))
6370 return -EINVAL;
6371
6372 len = sizeof(u32);
6373
6374 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6375 val++;
6376 }
6377
6378 if (put_user(len, optlen))
6379 return -EFAULT;
6380 if (copy_to_user(optval, &val, len))
6381 return -EFAULT;
6382
6383 return 0;
6384}
6385
Wei Yongjun209ba422011-04-17 17:27:08 +00006386/*
Michio Honda7dc04d72011-04-26 20:16:31 +09006387 * 8.1.23 SCTP_AUTO_ASCONF
6388 * See the corresponding setsockopt entry as description
6389 */
6390static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6391 char __user *optval, int __user *optlen)
6392{
6393 int val = 0;
6394
6395 if (len < sizeof(int))
6396 return -EINVAL;
6397
6398 len = sizeof(int);
6399 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6400 val = 1;
6401 if (put_user(len, optlen))
6402 return -EFAULT;
6403 if (copy_to_user(optval, &val, len))
6404 return -EFAULT;
6405 return 0;
6406}
6407
6408/*
Wei Yongjun209ba422011-04-17 17:27:08 +00006409 * 8.2.6. Get the Current Identifiers of Associations
6410 * (SCTP_GET_ASSOC_ID_LIST)
6411 *
6412 * This option gets the current list of SCTP association identifiers of
6413 * the SCTP associations handled by a one-to-many style socket.
6414 */
6415static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6416 char __user *optval, int __user *optlen)
6417{
6418 struct sctp_sock *sp = sctp_sk(sk);
6419 struct sctp_association *asoc;
6420 struct sctp_assoc_ids *ids;
6421 u32 num = 0;
6422
6423 if (sctp_style(sk, TCP))
6424 return -EOPNOTSUPP;
6425
6426 if (len < sizeof(struct sctp_assoc_ids))
6427 return -EINVAL;
6428
6429 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6430 num++;
6431 }
6432
6433 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6434 return -EINVAL;
6435
6436 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6437
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02006438 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
Wei Yongjun209ba422011-04-17 17:27:08 +00006439 if (unlikely(!ids))
6440 return -ENOMEM;
6441
6442 ids->gaids_number_of_ids = num;
6443 num = 0;
6444 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6445 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6446 }
6447
6448 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6449 kfree(ids);
6450 return -EFAULT;
6451 }
6452
6453 kfree(ids);
6454 return 0;
6455}
6456
Neil Horman5aa93bc2012-07-21 07:56:07 +00006457/*
6458 * SCTP_PEER_ADDR_THLDS
6459 *
6460 * This option allows us to fetch the partially failed threshold for one or all
6461 * transports in an association. See Section 6.1 of:
6462 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6463 */
6464static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6465 char __user *optval,
6466 int len,
6467 int __user *optlen)
6468{
6469 struct sctp_paddrthlds val;
6470 struct sctp_transport *trans;
6471 struct sctp_association *asoc;
6472
6473 if (len < sizeof(struct sctp_paddrthlds))
6474 return -EINVAL;
6475 len = sizeof(struct sctp_paddrthlds);
6476 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6477 return -EFAULT;
6478
6479 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6480 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6481 if (!asoc)
6482 return -ENOENT;
6483
6484 val.spt_pathpfthld = asoc->pf_retrans;
6485 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6486 } else {
6487 trans = sctp_addr_id2transport(sk, &val.spt_address,
6488 val.spt_assoc_id);
6489 if (!trans)
6490 return -ENOENT;
6491
6492 val.spt_pathmaxrxt = trans->pathmaxrxt;
6493 val.spt_pathpfthld = trans->pf_retrans;
6494 }
6495
6496 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6497 return -EFAULT;
6498
6499 return 0;
6500}
6501
Michele Baldessari196d6752012-12-01 04:49:42 +00006502/*
6503 * SCTP_GET_ASSOC_STATS
6504 *
6505 * This option retrieves local per endpoint statistics. It is modeled
6506 * after OpenSolaris' implementation
6507 */
6508static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6509 char __user *optval,
6510 int __user *optlen)
6511{
6512 struct sctp_assoc_stats sas;
6513 struct sctp_association *asoc = NULL;
6514
6515 /* User must provide at least the assoc id */
6516 if (len < sizeof(sctp_assoc_t))
6517 return -EINVAL;
6518
Guenter Roeck726bc6b2013-02-27 10:57:31 +00006519 /* Allow the struct to grow and fill in as much as possible */
6520 len = min_t(size_t, len, sizeof(sas));
6521
Michele Baldessari196d6752012-12-01 04:49:42 +00006522 if (copy_from_user(&sas, optval, len))
6523 return -EFAULT;
6524
6525 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6526 if (!asoc)
6527 return -EINVAL;
6528
6529 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6530 sas.sas_gapcnt = asoc->stats.gapcnt;
6531 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6532 sas.sas_osacks = asoc->stats.osacks;
6533 sas.sas_isacks = asoc->stats.isacks;
6534 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6535 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6536 sas.sas_oodchunks = asoc->stats.oodchunks;
6537 sas.sas_iodchunks = asoc->stats.iodchunks;
6538 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6539 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6540 sas.sas_idupchunks = asoc->stats.idupchunks;
6541 sas.sas_opackets = asoc->stats.opackets;
6542 sas.sas_ipackets = asoc->stats.ipackets;
6543
6544 /* New high max rto observed, will return 0 if not a single
6545 * RTO update took place. obs_rto_ipaddr will be bogus
6546 * in such a case
6547 */
6548 sas.sas_maxrto = asoc->stats.max_obs_rto;
6549 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6550 sizeof(struct sockaddr_storage));
6551
6552 /* Mark beginning of a new observation period */
6553 asoc->stats.max_obs_rto = asoc->rto_min;
6554
Michele Baldessari196d6752012-12-01 04:49:42 +00006555 if (put_user(len, optlen))
6556 return -EFAULT;
6557
Daniel Borkmannbb333812013-06-28 19:49:40 +02006558 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
Michele Baldessari196d6752012-12-01 04:49:42 +00006559
6560 if (copy_to_user(optval, &sas, len))
6561 return -EFAULT;
6562
6563 return 0;
6564}
6565
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02006566static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6567 char __user *optval,
6568 int __user *optlen)
6569{
6570 int val = 0;
6571
6572 if (len < sizeof(int))
6573 return -EINVAL;
6574
6575 len = sizeof(int);
6576 if (sctp_sk(sk)->recvrcvinfo)
6577 val = 1;
6578 if (put_user(len, optlen))
6579 return -EFAULT;
6580 if (copy_to_user(optval, &val, len))
6581 return -EFAULT;
6582
6583 return 0;
6584}
6585
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02006586static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6587 char __user *optval,
6588 int __user *optlen)
6589{
6590 int val = 0;
6591
6592 if (len < sizeof(int))
6593 return -EINVAL;
6594
6595 len = sizeof(int);
6596 if (sctp_sk(sk)->recvnxtinfo)
6597 val = 1;
6598 if (put_user(len, optlen))
6599 return -EFAULT;
6600 if (copy_to_user(optval, &val, len))
6601 return -EFAULT;
6602
6603 return 0;
6604}
6605
Xin Long28aa4c22016-07-09 19:47:40 +08006606static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6607 char __user *optval,
6608 int __user *optlen)
6609{
6610 struct sctp_assoc_value params;
6611 struct sctp_association *asoc;
6612 int retval = -EFAULT;
6613
6614 if (len < sizeof(params)) {
6615 retval = -EINVAL;
6616 goto out;
6617 }
6618
6619 len = sizeof(params);
6620 if (copy_from_user(&params, optval, len))
6621 goto out;
6622
6623 asoc = sctp_id2assoc(sk, params.assoc_id);
6624 if (asoc) {
6625 params.assoc_value = asoc->prsctp_enable;
6626 } else if (!params.assoc_id) {
6627 struct sctp_sock *sp = sctp_sk(sk);
6628
6629 params.assoc_value = sp->ep->prsctp_enable;
6630 } else {
6631 retval = -EINVAL;
6632 goto out;
6633 }
6634
6635 if (put_user(len, optlen))
6636 goto out;
6637
6638 if (copy_to_user(optval, &params, len))
6639 goto out;
6640
6641 retval = 0;
6642
6643out:
6644 return retval;
6645}
6646
Xin Longf959fb42016-07-09 19:47:41 +08006647static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
6648 char __user *optval,
6649 int __user *optlen)
6650{
6651 struct sctp_default_prinfo info;
6652 struct sctp_association *asoc;
6653 int retval = -EFAULT;
6654
6655 if (len < sizeof(info)) {
6656 retval = -EINVAL;
6657 goto out;
6658 }
6659
6660 len = sizeof(info);
6661 if (copy_from_user(&info, optval, len))
6662 goto out;
6663
6664 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
6665 if (asoc) {
6666 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
6667 info.pr_value = asoc->default_timetolive;
6668 } else if (!info.pr_assoc_id) {
6669 struct sctp_sock *sp = sctp_sk(sk);
6670
6671 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
6672 info.pr_value = sp->default_timetolive;
6673 } else {
6674 retval = -EINVAL;
6675 goto out;
6676 }
6677
6678 if (put_user(len, optlen))
6679 goto out;
6680
6681 if (copy_to_user(optval, &info, len))
6682 goto out;
6683
6684 retval = 0;
6685
6686out:
6687 return retval;
6688}
6689
Xin Long826d2532016-07-09 19:47:42 +08006690static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
6691 char __user *optval,
6692 int __user *optlen)
6693{
6694 struct sctp_prstatus params;
6695 struct sctp_association *asoc;
6696 int policy;
6697 int retval = -EINVAL;
6698
6699 if (len < sizeof(params))
6700 goto out;
6701
6702 len = sizeof(params);
6703 if (copy_from_user(&params, optval, len)) {
6704 retval = -EFAULT;
6705 goto out;
6706 }
6707
6708 policy = params.sprstat_policy;
6709 if (policy & ~SCTP_PR_SCTP_MASK)
6710 goto out;
6711
6712 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
6713 if (!asoc)
6714 goto out;
6715
6716 if (policy == SCTP_PR_SCTP_NONE) {
6717 params.sprstat_abandoned_unsent = 0;
6718 params.sprstat_abandoned_sent = 0;
6719 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6720 params.sprstat_abandoned_unsent +=
6721 asoc->abandoned_unsent[policy];
6722 params.sprstat_abandoned_sent +=
6723 asoc->abandoned_sent[policy];
6724 }
6725 } else {
6726 params.sprstat_abandoned_unsent =
6727 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
6728 params.sprstat_abandoned_sent =
6729 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
6730 }
6731
6732 if (put_user(len, optlen)) {
6733 retval = -EFAULT;
6734 goto out;
6735 }
6736
6737 if (copy_to_user(optval, &params, len)) {
6738 retval = -EFAULT;
6739 goto out;
6740 }
6741
6742 retval = 0;
6743
6744out:
6745 return retval;
6746}
6747
Xin Longd229d482017-04-01 17:07:46 +08006748static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
6749 char __user *optval,
6750 int __user *optlen)
6751{
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006752 struct sctp_stream_out_ext *streamoute;
Xin Longd229d482017-04-01 17:07:46 +08006753 struct sctp_association *asoc;
6754 struct sctp_prstatus params;
6755 int retval = -EINVAL;
6756 int policy;
6757
6758 if (len < sizeof(params))
6759 goto out;
6760
6761 len = sizeof(params);
6762 if (copy_from_user(&params, optval, len)) {
6763 retval = -EFAULT;
6764 goto out;
6765 }
6766
6767 policy = params.sprstat_policy;
6768 if (policy & ~SCTP_PR_SCTP_MASK)
6769 goto out;
6770
6771 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
Xin Longcee360a2017-05-31 16:36:31 +08006772 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
Xin Longd229d482017-04-01 17:07:46 +08006773 goto out;
6774
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006775 streamoute = asoc->stream.out[params.sprstat_sid].ext;
6776 if (!streamoute) {
6777 /* Not allocated yet, means all stats are 0 */
6778 params.sprstat_abandoned_unsent = 0;
6779 params.sprstat_abandoned_sent = 0;
6780 retval = 0;
6781 goto out;
6782 }
6783
Xin Longd229d482017-04-01 17:07:46 +08006784 if (policy == SCTP_PR_SCTP_NONE) {
6785 params.sprstat_abandoned_unsent = 0;
6786 params.sprstat_abandoned_sent = 0;
6787 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6788 params.sprstat_abandoned_unsent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006789 streamoute->abandoned_unsent[policy];
Xin Longd229d482017-04-01 17:07:46 +08006790 params.sprstat_abandoned_sent +=
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006791 streamoute->abandoned_sent[policy];
Xin Longd229d482017-04-01 17:07:46 +08006792 }
6793 } else {
6794 params.sprstat_abandoned_unsent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006795 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08006796 params.sprstat_abandoned_sent =
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -03006797 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
Xin Longd229d482017-04-01 17:07:46 +08006798 }
6799
6800 if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
6801 retval = -EFAULT;
6802 goto out;
6803 }
6804
6805 retval = 0;
6806
6807out:
6808 return retval;
6809}
6810
Xin Longc0d8bab2017-03-10 12:11:12 +08006811static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
6812 char __user *optval,
6813 int __user *optlen)
6814{
6815 struct sctp_assoc_value params;
6816 struct sctp_association *asoc;
6817 int retval = -EFAULT;
6818
6819 if (len < sizeof(params)) {
6820 retval = -EINVAL;
6821 goto out;
6822 }
6823
6824 len = sizeof(params);
6825 if (copy_from_user(&params, optval, len))
6826 goto out;
6827
6828 asoc = sctp_id2assoc(sk, params.assoc_id);
6829 if (asoc) {
6830 params.assoc_value = asoc->reconf_enable;
6831 } else if (!params.assoc_id) {
6832 struct sctp_sock *sp = sctp_sk(sk);
6833
6834 params.assoc_value = sp->ep->reconf_enable;
6835 } else {
6836 retval = -EINVAL;
6837 goto out;
6838 }
6839
6840 if (put_user(len, optlen))
6841 goto out;
6842
6843 if (copy_to_user(optval, &params, len))
6844 goto out;
6845
6846 retval = 0;
6847
6848out:
6849 return retval;
6850}
6851
Xin Long9fb657a2017-01-18 00:44:46 +08006852static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
6853 char __user *optval,
6854 int __user *optlen)
6855{
6856 struct sctp_assoc_value params;
6857 struct sctp_association *asoc;
6858 int retval = -EFAULT;
6859
6860 if (len < sizeof(params)) {
6861 retval = -EINVAL;
6862 goto out;
6863 }
6864
6865 len = sizeof(params);
6866 if (copy_from_user(&params, optval, len))
6867 goto out;
6868
6869 asoc = sctp_id2assoc(sk, params.assoc_id);
6870 if (asoc) {
6871 params.assoc_value = asoc->strreset_enable;
6872 } else if (!params.assoc_id) {
6873 struct sctp_sock *sp = sctp_sk(sk);
6874
6875 params.assoc_value = sp->ep->strreset_enable;
6876 } else {
6877 retval = -EINVAL;
6878 goto out;
6879 }
6880
6881 if (put_user(len, optlen))
6882 goto out;
6883
6884 if (copy_to_user(optval, &params, len))
6885 goto out;
6886
6887 retval = 0;
6888
6889out:
6890 return retval;
6891}
6892
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03006893static int sctp_getsockopt_scheduler(struct sock *sk, int len,
6894 char __user *optval,
6895 int __user *optlen)
6896{
6897 struct sctp_assoc_value params;
6898 struct sctp_association *asoc;
6899 int retval = -EFAULT;
6900
6901 if (len < sizeof(params)) {
6902 retval = -EINVAL;
6903 goto out;
6904 }
6905
6906 len = sizeof(params);
6907 if (copy_from_user(&params, optval, len))
6908 goto out;
6909
6910 asoc = sctp_id2assoc(sk, params.assoc_id);
6911 if (!asoc) {
6912 retval = -EINVAL;
6913 goto out;
6914 }
6915
6916 params.assoc_value = sctp_sched_get_sched(asoc);
6917
6918 if (put_user(len, optlen))
6919 goto out;
6920
6921 if (copy_to_user(optval, &params, len))
6922 goto out;
6923
6924 retval = 0;
6925
6926out:
6927 return retval;
6928}
6929
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03006930static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
6931 char __user *optval,
6932 int __user *optlen)
6933{
6934 struct sctp_stream_value params;
6935 struct sctp_association *asoc;
6936 int retval = -EFAULT;
6937
6938 if (len < sizeof(params)) {
6939 retval = -EINVAL;
6940 goto out;
6941 }
6942
6943 len = sizeof(params);
6944 if (copy_from_user(&params, optval, len))
6945 goto out;
6946
6947 asoc = sctp_id2assoc(sk, params.assoc_id);
6948 if (!asoc) {
6949 retval = -EINVAL;
6950 goto out;
6951 }
6952
6953 retval = sctp_sched_get_value(asoc, params.stream_id,
6954 &params.stream_value);
6955 if (retval)
6956 goto out;
6957
6958 if (put_user(len, optlen)) {
6959 retval = -EFAULT;
6960 goto out;
6961 }
6962
6963 if (copy_to_user(optval, &params, len)) {
6964 retval = -EFAULT;
6965 goto out;
6966 }
6967
6968out:
6969 return retval;
6970}
6971
Daniel Borkmanndda91922013-06-17 11:40:05 +02006972static int sctp_getsockopt(struct sock *sk, int level, int optname,
6973 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974{
6975 int retval = 0;
6976 int len;
6977
Daniel Borkmannbb333812013-06-28 19:49:40 +02006978 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979
6980 /* I can hardly begin to describe how wrong this is. This is
6981 * so broken as to be worse than useless. The API draft
6982 * REALLY is NOT helpful here... I am not convinced that the
6983 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
6984 * are at all well-founded.
6985 */
6986 if (level != SOL_SCTP) {
6987 struct sctp_af *af = sctp_sk(sk)->pf->af;
6988
6989 retval = af->getsockopt(sk, level, optname, optval, optlen);
6990 return retval;
6991 }
6992
6993 if (get_user(len, optlen))
6994 return -EFAULT;
6995
Jiri Slabya4b8e712016-10-21 14:13:24 +02006996 if (len < 0)
6997 return -EINVAL;
6998
wangweidong048ed4b2014-01-21 15:44:11 +08006999 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000
7001 switch (optname) {
7002 case SCTP_STATUS:
7003 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7004 break;
7005 case SCTP_DISABLE_FRAGMENTS:
7006 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7007 optlen);
7008 break;
7009 case SCTP_EVENTS:
7010 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7011 break;
7012 case SCTP_AUTOCLOSE:
7013 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7014 break;
7015 case SCTP_SOCKOPT_PEELOFF:
7016 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7017 break;
Neil Horman2cb5c8e2017-06-30 13:32:57 -04007018 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7019 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7020 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021 case SCTP_PEER_ADDR_PARAMS:
7022 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7023 optlen);
7024 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00007025 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07007026 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08007027 optlen);
7028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029 case SCTP_INITMSG:
7030 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 case SCTP_GET_PEER_ADDRS:
7033 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7034 optlen);
7035 break;
7036 case SCTP_GET_LOCAL_ADDRS:
7037 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7038 optlen);
7039 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04007040 case SCTP_SOCKOPT_CONNECTX3:
7041 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7042 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 case SCTP_DEFAULT_SEND_PARAM:
7044 retval = sctp_getsockopt_default_send_param(sk, len,
7045 optval, optlen);
7046 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02007047 case SCTP_DEFAULT_SNDINFO:
7048 retval = sctp_getsockopt_default_sndinfo(sk, len,
7049 optval, optlen);
7050 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 case SCTP_PRIMARY_ADDR:
7052 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7053 break;
7054 case SCTP_NODELAY:
7055 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7056 break;
7057 case SCTP_RTOINFO:
7058 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7059 break;
7060 case SCTP_ASSOCINFO:
7061 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7062 break;
7063 case SCTP_I_WANT_MAPPED_V4_ADDR:
7064 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7065 break;
7066 case SCTP_MAXSEG:
7067 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7068 break;
7069 case SCTP_GET_PEER_ADDR_INFO:
7070 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7071 optlen);
7072 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08007073 case SCTP_ADAPTATION_LAYER:
7074 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 optlen);
7076 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08007077 case SCTP_CONTEXT:
7078 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7079 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007080 case SCTP_FRAGMENT_INTERLEAVE:
7081 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7082 optlen);
7083 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07007084 case SCTP_PARTIAL_DELIVERY_POINT:
7085 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7086 optlen);
7087 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07007088 case SCTP_MAX_BURST:
7089 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7090 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07007091 case SCTP_AUTH_KEY:
7092 case SCTP_AUTH_CHUNK:
7093 case SCTP_AUTH_DELETE_KEY:
7094 retval = -EOPNOTSUPP;
7095 break;
7096 case SCTP_HMAC_IDENT:
7097 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7098 break;
7099 case SCTP_AUTH_ACTIVE_KEY:
7100 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7101 break;
7102 case SCTP_PEER_AUTH_CHUNKS:
7103 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7104 optlen);
7105 break;
7106 case SCTP_LOCAL_AUTH_CHUNKS:
7107 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7108 optlen);
7109 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08007110 case SCTP_GET_ASSOC_NUMBER:
7111 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7112 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00007113 case SCTP_GET_ASSOC_ID_LIST:
7114 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7115 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09007116 case SCTP_AUTO_ASCONF:
7117 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7118 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00007119 case SCTP_PEER_ADDR_THLDS:
7120 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7121 break;
Michele Baldessari196d6752012-12-01 04:49:42 +00007122 case SCTP_GET_ASSOC_STATS:
7123 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7124 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02007125 case SCTP_RECVRCVINFO:
7126 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7127 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007128 case SCTP_RECVNXTINFO:
7129 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7130 break;
Xin Long28aa4c22016-07-09 19:47:40 +08007131 case SCTP_PR_SUPPORTED:
7132 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7133 break;
Xin Longf959fb42016-07-09 19:47:41 +08007134 case SCTP_DEFAULT_PRINFO:
7135 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7136 optlen);
7137 break;
Xin Long826d2532016-07-09 19:47:42 +08007138 case SCTP_PR_ASSOC_STATUS:
7139 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7140 optlen);
7141 break;
Xin Longd229d482017-04-01 17:07:46 +08007142 case SCTP_PR_STREAM_STATUS:
7143 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7144 optlen);
7145 break;
Xin Longc0d8bab2017-03-10 12:11:12 +08007146 case SCTP_RECONFIG_SUPPORTED:
7147 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7148 optlen);
7149 break;
Xin Long9fb657a2017-01-18 00:44:46 +08007150 case SCTP_ENABLE_STREAM_RESET:
7151 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7152 optlen);
7153 break;
Marcelo Ricardo Leitner13aa8772017-10-03 19:20:14 -03007154 case SCTP_STREAM_SCHEDULER:
7155 retval = sctp_getsockopt_scheduler(sk, len, optval,
7156 optlen);
7157 break;
Marcelo Ricardo Leitner0ccdf3c2017-10-03 19:20:15 -03007158 case SCTP_STREAM_SCHEDULER_VALUE:
7159 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7160 optlen);
7161 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 default:
7163 retval = -ENOPROTOOPT;
7164 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166
wangweidong048ed4b2014-01-21 15:44:11 +08007167 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 return retval;
7169}
7170
Craig Gallek086c6532016-02-10 11:50:35 -05007171static int sctp_hash(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172{
7173 /* STUB */
Craig Gallek086c6532016-02-10 11:50:35 -05007174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175}
7176
7177static void sctp_unhash(struct sock *sk)
7178{
7179 /* STUB */
7180}
7181
7182/* Check if port is acceptable. Possibly find first available port.
7183 *
7184 * The port hash table (contained in the 'global' SCTP protocol storage
7185 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7186 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7187 * list (the list number is the port number hashed out, so as you
7188 * would expect from a hash function, all the ports in a given list have
7189 * such a number that hashes out to the same list number; you were
7190 * expecting that, right?); so each list has a set of ports, with a
7191 * link to the socket (struct sock) that uses it, the port number and
7192 * a fastreuse flag (FIXME: NPI ipg).
7193 */
7194static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007195 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196
7197static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
7198{
7199 struct sctp_bind_hashbucket *head; /* hash list */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007200 struct sctp_bind_bucket *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 unsigned short snum;
7202 int ret;
7203
Al Viro04afd8b2006-11-20 17:02:01 -08007204 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205
Daniel Borkmannbb333812013-06-28 19:49:40 +02007206 pr_debug("%s: begins, snum:%d\n", __func__, snum);
7207
wangweidong79b91132014-01-21 15:44:07 +08007208 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209
7210 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07007211 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007212 int low, high, remaining, index;
7213 unsigned int rover;
WANG Cong122ff242014-05-12 16:04:53 -07007214 struct net *net = sock_net(sk);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007215
WANG Cong122ff242014-05-12 16:04:53 -07007216 inet_get_local_port_range(net, &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07007217 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05007218 rover = prandom_u32() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 do {
7221 rover++;
7222 if ((rover < low) || (rover > high))
7223 rover = low;
WANG Cong122ff242014-05-12 16:04:53 -07007224 if (inet_is_local_reserved_port(net, rover))
Amerigo Wange3826f12010-05-05 00:27:06 +00007225 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007226 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 head = &sctp_port_hashtable[index];
wangweidong3c8e43b2014-01-21 15:44:08 +08007228 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007229 sctp_for_each_hentry(pp, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007230 if ((pp->port == rover) &&
7231 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232 goto next;
7233 break;
7234 next:
wangweidong3c8e43b2014-01-21 15:44:08 +08007235 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237
7238 /* Exhausted local port range during search? */
7239 ret = 1;
7240 if (remaining <= 0)
7241 goto fail;
7242
7243 /* OK, here is the one we will use. HEAD (the port
7244 * hash table list entry) is non-NULL and we hold it's
7245 * mutex.
7246 */
7247 snum = rover;
7248 } else {
7249 /* We are given an specific port number; we verify
7250 * that it is not being used. If it is used, we will
7251 * exahust the search in the hash list corresponding
7252 * to the port number (snum) - we detect that with the
7253 * port iterator, pp being NULL.
7254 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007255 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
wangweidong3c8e43b2014-01-21 15:44:08 +08007256 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08007257 sctp_for_each_hentry(pp, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007258 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259 goto pp_found;
7260 }
7261 }
7262 pp = NULL;
7263 goto pp_not_found;
7264pp_found:
7265 if (!hlist_empty(&pp->owner)) {
7266 /* We had a port hash table hit - there is an
7267 * available port (pp != NULL) and it is being
7268 * used by other socket (pp->owner not empty); that other
7269 * socket is going to be sk2.
7270 */
7271 int reuse = sk->sk_reuse;
7272 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273
Daniel Borkmannbb333812013-06-28 19:49:40 +02007274 pr_debug("%s: found a possible match\n", __func__);
7275
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007276 if (pp->fastreuse && sk->sk_reuse &&
7277 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278 goto success;
7279
7280 /* Run through the list of sockets bound to the port
7281 * (pp->port) [via the pointers bind_next and
7282 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
7283 * we get the endpoint they describe and run through
7284 * the endpoint's list of IP (v4 or v6) addresses,
7285 * comparing each of the addresses with the address of
7286 * the socket sk. If we find a match, then that means
7287 * that this port/socket (sk) combination are already
7288 * in an endpoint.
7289 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08007290 sk_for_each_bound(sk2, &pp->owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291 struct sctp_endpoint *ep2;
7292 ep2 = sctp_sk(sk2)->ep;
7293
Vlad Yasevich4e540642008-07-18 23:06:32 -07007294 if (sk == sk2 ||
7295 (reuse && sk2->sk_reuse &&
7296 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297 continue;
7298
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07007299 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
7300 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301 ret = (long)sk2;
7302 goto fail_unlock;
7303 }
7304 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02007305
7306 pr_debug("%s: found a match\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 }
7308pp_not_found:
7309 /* If there was a hash table miss, create a new port. */
7310 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007311 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 goto fail_unlock;
7313
7314 /* In either case (hit or miss), make sure fastreuse is 1 only
7315 * if sk->sk_reuse is too (that is, if the caller requested
7316 * SO_REUSEADDR on this socket -sk-).
7317 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07007318 if (hlist_empty(&pp->owner)) {
7319 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
7320 pp->fastreuse = 1;
7321 else
7322 pp->fastreuse = 0;
7323 } else if (pp->fastreuse &&
7324 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325 pp->fastreuse = 0;
7326
7327 /* We are set, so fill up all the data in the hash table
7328 * entry, tie the socket list information with the rest of the
7329 * sockets FIXME: Blurry, NPI (ipg).
7330 */
7331success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007333 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 sk_add_bind_node(sk, &pp->owner);
7335 sctp_sk(sk)->bind_hash = pp;
7336 }
7337 ret = 0;
7338
7339fail_unlock:
wangweidong3c8e43b2014-01-21 15:44:08 +08007340 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341
7342fail:
wangweidong79b91132014-01-21 15:44:07 +08007343 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 return ret;
7345}
7346
7347/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
7348 * port is requested.
7349 */
7350static int sctp_get_port(struct sock *sk, unsigned short snum)
7351{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 union sctp_addr addr;
7353 struct sctp_af *af = sctp_sk(sk)->pf->af;
7354
7355 /* Set up a dummy address struct from the sk. */
7356 af->from_sk(&addr, sk);
7357 addr.v4.sin_port = htons(snum);
7358
7359 /* Note: sk->sk_num gets filled in if ephemeral port request. */
Daniel Borkmann62208f12013-06-25 18:17:30 +02007360 return !!sctp_get_port_local(sk, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361}
7362
7363/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 * Move a socket to LISTENING state.
7365 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02007366static int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007368 struct sctp_sock *sp = sctp_sk(sk);
7369 struct sctp_endpoint *ep = sp->ep;
Herbert Xu5821c762016-01-24 21:20:12 +08007370 struct crypto_shash *tfm = NULL;
Neil Horman3c681982012-10-24 09:20:03 +00007371 char alg[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372
7373 /* Allocate HMAC for generating cookie. */
Neil Horman3c681982012-10-24 09:20:03 +00007374 if (!sp->hmac && sp->sctp_hmac_alg) {
7375 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
Herbert Xu5821c762016-01-24 21:20:12 +08007376 tfm = crypto_alloc_shash(alg, 0, 0);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07007377 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00007378 net_info_ratelimited("failed to load transform for %s: %ld\n",
Neil Horman3c681982012-10-24 09:20:03 +00007379 sp->sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007380 return -ENOSYS;
7381 }
7382 sctp_sk(sk)->hmac = tfm;
7383 }
7384
7385 /*
7386 * If a bind() or sctp_bindx() is not called prior to a listen()
7387 * call that allows new associations to be accepted, the system
7388 * picks an ephemeral port and will choose an address set equivalent
7389 * to binding with a wildcard address.
7390 *
7391 * This is not currently spelled out in the SCTP sockets
7392 * extensions draft, but follows the practice as seen in TCP
7393 * sockets.
7394 *
7395 */
7396 sk->sk_state = SCTP_SS_LISTENING;
7397 if (!ep->base.bind_addr.port) {
7398 if (sctp_autobind(sk))
7399 return -EAGAIN;
7400 } else {
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007401 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007402 sk->sk_state = SCTP_SS_CLOSED;
7403 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 }
7405 }
7406
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007407 sk->sk_max_ack_backlog = backlog;
7408 sctp_hash_endpoint(ep);
7409 return 0;
7410}
7411
7412/*
7413 * 4.1.3 / 5.1.3 listen()
7414 *
7415 * By default, new associations are not accepted for UDP style sockets.
7416 * An application uses listen() to mark a socket as being able to
7417 * accept new associations.
7418 *
7419 * On TCP style sockets, applications use listen() to ready the SCTP
7420 * endpoint for accepting inbound associations.
7421 *
7422 * On both types of endpoints a backlog of '0' disables listening.
7423 *
7424 * Move a socket to LISTENING state.
7425 */
7426int sctp_inet_listen(struct socket *sock, int backlog)
7427{
7428 struct sock *sk = sock->sk;
7429 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7430 int err = -EINVAL;
7431
7432 if (unlikely(backlog < 0))
7433 return err;
7434
wangweidong048ed4b2014-01-21 15:44:11 +08007435 lock_sock(sk);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007436
7437 /* Peeled-off sockets are not allowed to listen(). */
7438 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
7439 goto out;
7440
7441 if (sock->state != SS_UNCONNECTED)
7442 goto out;
7443
Xin Long34b27892017-04-06 13:10:52 +08007444 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
7445 goto out;
7446
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007447 /* If backlog is zero, disable listening. */
7448 if (!backlog) {
7449 if (sctp_sstate(sk, CLOSED))
7450 goto out;
7451
7452 err = 0;
7453 sctp_unhash_endpoint(ep);
7454 sk->sk_state = SCTP_SS_CLOSED;
7455 if (sk->sk_reuse)
7456 sctp_sk(sk)->bind_hash->fastreuse = 1;
7457 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007458 }
7459
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007460 /* If we are already listening, just update the backlog */
7461 if (sctp_sstate(sk, LISTENING))
7462 sk->sk_max_ack_backlog = backlog;
7463 else {
7464 err = sctp_listen_start(sk, backlog);
7465 if (err)
7466 goto out;
7467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00007469 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470out:
wangweidong048ed4b2014-01-21 15:44:11 +08007471 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473}
7474
7475/*
7476 * This function is done by modeling the current datagram_poll() and the
7477 * tcp_poll(). Note that, based on these implementations, we don't
7478 * lock the socket in this function, even though it seems that,
7479 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08007480 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481 * in this place. We assume that we don't need locks either until proven
7482 * otherwise.
7483 *
7484 * Another thing to note is that we include the Async I/O support
7485 * here, again, by modeling the current TCP/UDP code. We don't have
7486 * a good way to test with it yet.
7487 */
7488unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
7489{
7490 struct sock *sk = sock->sk;
7491 struct sctp_sock *sp = sctp_sk(sk);
7492 unsigned int mask;
7493
Eric Dumazetaa395142010-04-20 13:03:51 +00007494 poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03007496 sock_rps_record_flow(sk);
7497
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498 /* A TCP-style listening socket becomes readable when the accept queue
7499 * is not empty.
7500 */
7501 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
7502 return (!list_empty(&sp->ep->asocs)) ?
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09007503 (POLLIN | POLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504
7505 mask = 0;
7506
7507 /* Is there any exceptional events? */
7508 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +00007509 mask |= POLLERR |
Daniel Borkmanna0fb05d2013-09-07 16:44:59 +02007510 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
Davide Libenzif348d702006-03-25 03:07:39 -08007511 if (sk->sk_shutdown & RCV_SHUTDOWN)
Eric Dumazetdb409802010-09-06 11:13:50 +00007512 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 if (sk->sk_shutdown == SHUTDOWN_MASK)
7514 mask |= POLLHUP;
7515
7516 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00007517 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 mask |= POLLIN | POLLRDNORM;
7519
7520 /* The association is either gone or not ready. */
7521 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
7522 return mask;
7523
7524 /* Is it writable? */
7525 if (sctp_writeable(sk)) {
7526 mask |= POLLOUT | POLLWRNORM;
7527 } else {
Eric Dumazet9cd3e072015-11-29 20:03:10 -08007528 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007529 /*
7530 * Since the socket is not locked, the buffer
7531 * might be made available after the writeable check and
7532 * before the bit is set. This could cause a lost I/O
7533 * signal. tcp_poll() has a race breaker for this race
7534 * condition. Based on their implementation, we put
7535 * in the following code to cover it as well.
7536 */
7537 if (sctp_writeable(sk))
7538 mask |= POLLOUT | POLLWRNORM;
7539 }
7540 return mask;
7541}
7542
7543/********************************************************************
7544 * 2nd Level Abstractions
7545 ********************************************************************/
7546
7547static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007548 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549{
7550 struct sctp_bind_bucket *pp;
7551
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08007552 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07007554 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555 pp->port = snum;
7556 pp->fastreuse = 0;
7557 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007558 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007559 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 }
7561 return pp;
7562}
7563
7564/* Caller must hold hashbucket lock for this tb with local BH disabled */
7565static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
7566{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07007567 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007568 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 kmem_cache_free(sctp_bucket_cachep, pp);
7570 SCTP_DBG_OBJCNT_DEC(bind_bucket);
7571 }
7572}
7573
7574/* Release this socket's reference to a local port. */
7575static inline void __sctp_put_port(struct sock *sk)
7576{
7577 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007578 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
7579 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 struct sctp_bind_bucket *pp;
7581
wangweidong3c8e43b2014-01-21 15:44:08 +08007582 spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 pp = sctp_sk(sk)->bind_hash;
7584 __sk_del_bind_node(sk);
7585 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007586 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 sctp_bucket_destroy(pp);
wangweidong3c8e43b2014-01-21 15:44:08 +08007588 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589}
7590
7591void sctp_put_port(struct sock *sk)
7592{
wangweidong79b91132014-01-21 15:44:07 +08007593 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 __sctp_put_port(sk);
wangweidong79b91132014-01-21 15:44:07 +08007595 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596}
7597
7598/*
7599 * The system picks an ephemeral port and choose an address set equivalent
7600 * to binding with a wildcard address.
7601 * One of those addresses will be the primary address for the association.
7602 * This automatically enables the multihoming capability of SCTP.
7603 */
7604static int sctp_autobind(struct sock *sk)
7605{
7606 union sctp_addr autoaddr;
7607 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08007608 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609
7610 /* Initialize a local sockaddr structure to INADDR_ANY. */
7611 af = sctp_sk(sk)->pf->af;
7612
Eric Dumazetc720c7e82009-10-15 06:30:45 +00007613 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614 af->inaddr_any(&autoaddr, port);
7615
7616 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
7617}
7618
7619/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
7620 *
7621 * From RFC 2292
7622 * 4.2 The cmsghdr Structure *
7623 *
7624 * When ancillary data is sent or received, any number of ancillary data
7625 * objects can be specified by the msg_control and msg_controllen members of
7626 * the msghdr structure, because each object is preceded by
7627 * a cmsghdr structure defining the object's length (the cmsg_len member).
7628 * Historically Berkeley-derived implementations have passed only one object
7629 * at a time, but this API allows multiple objects to be
7630 * passed in a single call to sendmsg() or recvmsg(). The following example
7631 * shows two ancillary data objects in a control buffer.
7632 *
7633 * |<--------------------------- msg_controllen -------------------------->|
7634 * | |
7635 *
7636 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
7637 *
7638 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
7639 * | | |
7640 *
7641 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
7642 *
7643 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
7644 * | | | | |
7645 *
7646 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7647 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
7648 *
7649 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
7650 *
7651 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7652 * ^
7653 * |
7654 *
7655 * msg_control
7656 * points here
7657 */
Xin Longa05437a2017-08-11 10:23:48 +08007658static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007659{
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007660 struct msghdr *my_msg = (struct msghdr *)msg;
Xin Longa05437a2017-08-11 10:23:48 +08007661 struct cmsghdr *cmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007662
Gu Zhengf95b4142014-12-11 11:22:04 +08007663 for_each_cmsghdr(cmsg, my_msg) {
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007664 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665 return -EINVAL;
7666
7667 /* Should we parse this header or ignore? */
7668 if (cmsg->cmsg_level != IPPROTO_SCTP)
7669 continue;
7670
7671 /* Strictly check lengths following example in SCM code. */
7672 switch (cmsg->cmsg_type) {
7673 case SCTP_INIT:
7674 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007675 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676 *
7677 * This cmsghdr structure provides information for
7678 * initializing new SCTP associations with sendmsg().
7679 * The SCTP_INITMSG socket option uses this same data
7680 * structure. This structure is not used for
7681 * recvmsg().
7682 *
7683 * cmsg_level cmsg_type cmsg_data[]
7684 * ------------ ------------ ----------------------
7685 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
7686 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007687 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688 return -EINVAL;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007689
7690 cmsgs->init = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007691 break;
7692
7693 case SCTP_SNDRCV:
7694 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007695 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696 *
7697 * This cmsghdr structure specifies SCTP options for
7698 * sendmsg() and describes SCTP header information
7699 * about a received message through recvmsg().
7700 *
7701 * cmsg_level cmsg_type cmsg_data[]
7702 * ------------ ------------ ----------------------
7703 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
7704 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007705 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706 return -EINVAL;
7707
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007708 cmsgs->srinfo = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007710 if (cmsgs->srinfo->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007711 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Longa6c2f792016-07-09 19:47:43 +08007712 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007713 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714 return -EINVAL;
7715 break;
7716
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007717 case SCTP_SNDINFO:
7718 /* SCTP Socket API Extension
7719 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
7720 *
7721 * This cmsghdr structure specifies SCTP options for
7722 * sendmsg(). This structure and SCTP_RCVINFO replaces
7723 * SCTP_SNDRCV which has been deprecated.
7724 *
7725 * cmsg_level cmsg_type cmsg_data[]
7726 * ------------ ------------ ---------------------
7727 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
7728 */
7729 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
7730 return -EINVAL;
7731
7732 cmsgs->sinfo = CMSG_DATA(cmsg);
7733
7734 if (cmsgs->sinfo->snd_flags &
7735 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Longa6c2f792016-07-09 19:47:43 +08007736 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007737 SCTP_ABORT | SCTP_EOF))
7738 return -EINVAL;
7739 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740 default:
7741 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743 }
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007744
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745 return 0;
7746}
7747
7748/*
7749 * Wait for a packet..
7750 * Note: This function is the same function as in core/datagram.c
7751 * with a few modifications to make lksctp work.
7752 */
wangweidong26ac8e52013-12-23 12:16:51 +08007753static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007754{
7755 int error;
7756 DEFINE_WAIT(wait);
7757
Eric Dumazetaa395142010-04-20 13:03:51 +00007758 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007759
7760 /* Socket errors? */
7761 error = sock_error(sk);
7762 if (error)
7763 goto out;
7764
7765 if (!skb_queue_empty(&sk->sk_receive_queue))
7766 goto ready;
7767
7768 /* Socket shut down? */
7769 if (sk->sk_shutdown & RCV_SHUTDOWN)
7770 goto out;
7771
7772 /* Sequenced packets can come disconnected. If so we report the
7773 * problem.
7774 */
7775 error = -ENOTCONN;
7776
7777 /* Is there a good reason to think that we may receive some data? */
7778 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
7779 goto out;
7780
7781 /* Handle signals. */
7782 if (signal_pending(current))
7783 goto interrupted;
7784
7785 /* Let another process have a go. Since we are going to sleep
7786 * anyway. Note: This may cause odd behaviors if the message
7787 * does not fit in the user's buffer, but this seems to be the
7788 * only way to honor MSG_DONTWAIT realistically.
7789 */
wangweidong048ed4b2014-01-21 15:44:11 +08007790 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 *timeo_p = schedule_timeout(*timeo_p);
wangweidong048ed4b2014-01-21 15:44:11 +08007792 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793
7794ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00007795 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796 return 0;
7797
7798interrupted:
7799 error = sock_intr_errno(*timeo_p);
7800
7801out:
Eric Dumazetaa395142010-04-20 13:03:51 +00007802 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803 *err = error;
7804 return error;
7805}
7806
7807/* Receive a datagram.
7808 * Note: This is pretty much the same routine as in core/datagram.c
7809 * with a few changes to make lksctp work.
7810 */
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007811struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
7812 int noblock, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813{
7814 int error;
7815 struct sk_buff *skb;
7816 long timeo;
7817
Linus Torvalds1da177e2005-04-16 15:20:36 -07007818 timeo = sock_rcvtimeo(sk, noblock);
7819
Daniel Borkmannbb333812013-06-28 19:49:40 +02007820 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
7821 MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822
7823 do {
7824 /* Again only user level code calls this function,
7825 * so nothing interrupt level
7826 * will suddenly eat the receive_queue.
7827 *
7828 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00007829 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830 */
7831 if (flags & MSG_PEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832 skb = skb_peek(&sk->sk_receive_queue);
7833 if (skb)
Reshetova, Elena63354792017-06-30 13:07:58 +03007834 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835 } else {
Marcelo Ricardo Leitner311b2172016-04-13 19:12:29 -03007836 skb = __skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837 }
7838
7839 if (skb)
7840 return skb;
7841
Neil Horman6736dc32005-12-02 20:30:06 -08007842 /* Caller is allowed not to check sk->sk_err before calling. */
7843 error = sock_error(sk);
7844 if (error)
7845 goto no_packet;
7846
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847 if (sk->sk_shutdown & RCV_SHUTDOWN)
7848 break;
7849
Alexander Duyck2b5cd0d2017-03-24 10:08:12 -07007850 if (sk_can_busy_loop(sk)) {
7851 sk_busy_loop(sk, noblock);
7852
7853 if (!skb_queue_empty(&sk->sk_receive_queue))
7854 continue;
7855 }
Neil Horman8465a5f2014-04-17 15:26:51 -04007856
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857 /* User doesn't want to wait. */
7858 error = -EAGAIN;
7859 if (!timeo)
7860 goto no_packet;
7861 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
7862
7863 return NULL;
7864
7865no_packet:
7866 *err = error;
7867 return NULL;
7868}
7869
7870/* If sndbuf has changed, wake up per association sndbuf waiters. */
7871static void __sctp_write_space(struct sctp_association *asoc)
7872{
7873 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874
Eric Dumazetceb5d582015-11-29 20:03:11 -08007875 if (sctp_wspace(asoc) <= 0)
7876 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877
Eric Dumazetceb5d582015-11-29 20:03:11 -08007878 if (waitqueue_active(&asoc->wait))
7879 wake_up_interruptible(&asoc->wait);
Eric Dumazeteaefd112011-02-18 03:26:36 +00007880
Eric Dumazetceb5d582015-11-29 20:03:11 -08007881 if (sctp_writeable(sk)) {
7882 struct socket_wq *wq;
7883
7884 rcu_read_lock();
7885 wq = rcu_dereference(sk->sk_wq);
7886 if (wq) {
7887 if (waitqueue_active(&wq->wait))
7888 wake_up_interruptible(&wq->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889
7890 /* Note that we try to include the Async I/O support
7891 * here by modeling from the current TCP/UDP code.
7892 * We have not tested with it yet.
7893 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00007894 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Eric Dumazetceb5d582015-11-29 20:03:11 -08007895 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007896 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08007897 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898 }
7899}
7900
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007901static void sctp_wake_up_waiters(struct sock *sk,
7902 struct sctp_association *asoc)
7903{
7904 struct sctp_association *tmp = asoc;
7905
7906 /* We do accounting for the sndbuf space per association,
7907 * so we only need to wake our own association.
7908 */
7909 if (asoc->ep->sndbuf_policy)
7910 return __sctp_write_space(asoc);
7911
Daniel Borkmann1e1cdf82014-04-09 16:10:20 +02007912 /* If association goes down and is just flushing its
7913 * outq, then just normally notify others.
7914 */
7915 if (asoc->base.dead)
7916 return sctp_write_space(sk);
7917
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007918 /* Accounting for the sndbuf space is per socket, so we
7919 * need to wake up others, try to be fair and in case of
7920 * other associations, let them have a go first instead
7921 * of just doing a sctp_write_space() call.
7922 *
7923 * Note that we reach sctp_wake_up_waiters() only when
7924 * associations free up queued chunks, thus we are under
7925 * lock and the list of associations on a socket is
7926 * guaranteed not to change.
7927 */
7928 for (tmp = list_next_entry(tmp, asocs); 1;
7929 tmp = list_next_entry(tmp, asocs)) {
7930 /* Manually skip the head element. */
7931 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
7932 continue;
7933 /* Wake up association. */
7934 __sctp_write_space(tmp);
7935 /* We've reached the end. */
7936 if (tmp == asoc)
7937 break;
7938 }
7939}
7940
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941/* Do accounting for the sndbuf space.
7942 * Decrement the used sndbuf space of the corresponding association by the
7943 * data size which was just transmitted(freed).
7944 */
7945static void sctp_wfree(struct sk_buff *skb)
7946{
Daniel Borkmannf869c912014-11-20 01:54:48 +01007947 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
7948 struct sctp_association *asoc = chunk->asoc;
7949 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950
Neil Horman4eb701d2005-04-28 12:02:04 -07007951 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
7952 sizeof(struct sk_buff) +
7953 sizeof(struct sctp_chunk);
7954
Reshetova, Elena14afee42017-06-30 13:08:00 +03007955 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
Neil Horman4eb701d2005-04-28 12:02:04 -07007956
Neil Horman4d93df02007-08-15 16:07:44 -07007957 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007958 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
Neil Horman4d93df02007-08-15 16:07:44 -07007959 */
7960 sk->sk_wmem_queued -= skb->truesize;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007961 sk_mem_uncharge(sk, skb->truesize);
Neil Horman4d93df02007-08-15 16:07:44 -07007962
Neil Horman4eb701d2005-04-28 12:02:04 -07007963 sock_wfree(skb);
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007964 sctp_wake_up_waiters(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965
7966 sctp_association_put(asoc);
7967}
7968
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07007969/* Do accounting for the receive space on the socket.
7970 * Accounting for the association is done in ulpevent.c
7971 * We set this as a destructor for the cloned data skbs so that
7972 * accounting is done at the correct time.
7973 */
7974void sctp_sock_rfree(struct sk_buff *skb)
7975{
7976 struct sock *sk = skb->sk;
7977 struct sctp_ulpevent *event = sctp_skb2event(skb);
7978
7979 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07007980
7981 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007982 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07007983 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007984 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07007985}
7986
7987
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988/* Helper function to wait for space in the sndbuf. */
7989static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Longcea0cc82017-11-15 16:57:26 +08007990 size_t msg_len, struct sock **orig_sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991{
7992 struct sock *sk = asoc->base.sk;
7993 int err = 0;
7994 long current_timeo = *timeo_p;
7995 DEFINE_WAIT(wait);
7996
Daniel Borkmannbb333812013-06-28 19:49:40 +02007997 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
7998 *timeo_p, msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999
8000 /* Increment the association's refcnt. */
8001 sctp_association_hold(asoc);
8002
8003 /* Wait on the association specific sndbuf space. */
8004 for (;;) {
8005 prepare_to_wait_exclusive(&asoc->wait, &wait,
8006 TASK_INTERRUPTIBLE);
Xin Longca3af4d2017-11-15 16:55:54 +08008007 if (asoc->base.dead)
8008 goto do_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008009 if (!*timeo_p)
8010 goto do_nonblock;
Xin Longca3af4d2017-11-15 16:55:54 +08008011 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012 goto do_error;
8013 if (signal_pending(current))
8014 goto do_interrupted;
8015 if (msg_len <= sctp_wspace(asoc))
8016 break;
8017
8018 /* Let another process have a go. Since we are going
8019 * to sleep anyway.
8020 */
wangweidong048ed4b2014-01-21 15:44:11 +08008021 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008022 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008023 lock_sock(sk);
Xin Longcea0cc82017-11-15 16:57:26 +08008024 if (sk != asoc->base.sk) {
8025 release_sock(sk);
8026 sk = asoc->base.sk;
8027 lock_sock(sk);
8028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029
8030 *timeo_p = current_timeo;
8031 }
8032
8033out:
Xin Longcea0cc82017-11-15 16:57:26 +08008034 *orig_sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008035 finish_wait(&asoc->wait, &wait);
8036
8037 /* Release the association's refcnt. */
8038 sctp_association_put(asoc);
8039
8040 return err;
8041
Xin Longca3af4d2017-11-15 16:55:54 +08008042do_dead:
8043 err = -ESRCH;
8044 goto out;
8045
Linus Torvalds1da177e2005-04-16 15:20:36 -07008046do_error:
8047 err = -EPIPE;
8048 goto out;
8049
8050do_interrupted:
8051 err = sock_intr_errno(*timeo_p);
8052 goto out;
8053
8054do_nonblock:
8055 err = -EAGAIN;
8056 goto out;
8057}
8058
David S. Miller676d2362014-04-11 16:15:36 -04008059void sctp_data_ready(struct sock *sk)
Wei Yongjun561b1732010-04-28 08:47:18 +00008060{
David S. Miller7ef52732010-05-02 21:43:40 -07008061 struct socket_wq *wq;
8062
8063 rcu_read_lock();
8064 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08008065 if (skwq_has_sleeper(wq))
David S. Miller7ef52732010-05-02 21:43:40 -07008066 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
Wei Yongjun561b1732010-04-28 08:47:18 +00008067 POLLRDNORM | POLLRDBAND);
8068 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07008069 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00008070}
8071
Linus Torvalds1da177e2005-04-16 15:20:36 -07008072/* If socket sndbuf has changed, wake up all per association waiters. */
8073void sctp_write_space(struct sock *sk)
8074{
8075 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008076
8077 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07008078 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008079 __sctp_write_space(asoc);
8080 }
8081}
8082
8083/* Is there any sndbuf space available on the socket?
8084 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08008085 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07008086 * associations on the same socket. For a UDP-style socket with
8087 * multiple associations, it is possible for it to be "unwriteable"
8088 * prematurely. I assume that this is acceptable because
8089 * a premature "unwriteable" is better than an accidental "writeable" which
8090 * would cause an unwanted block under certain circumstances. For the 1-1
8091 * UDP-style sockets or TCP-style sockets, this code should work.
8092 * - Daisy
8093 */
8094static int sctp_writeable(struct sock *sk)
8095{
8096 int amt = 0;
8097
Eric Dumazet31e6d362009-06-17 19:05:41 -07008098 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099 if (amt < 0)
8100 amt = 0;
8101 return amt;
8102}
8103
8104/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8105 * returns immediately with EINPROGRESS.
8106 */
8107static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8108{
8109 struct sock *sk = asoc->base.sk;
8110 int err = 0;
8111 long current_timeo = *timeo_p;
8112 DEFINE_WAIT(wait);
8113
Daniel Borkmannbb333812013-06-28 19:49:40 +02008114 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008115
8116 /* Increment the association's refcnt. */
8117 sctp_association_hold(asoc);
8118
8119 for (;;) {
8120 prepare_to_wait_exclusive(&asoc->wait, &wait,
8121 TASK_INTERRUPTIBLE);
8122 if (!*timeo_p)
8123 goto do_nonblock;
8124 if (sk->sk_shutdown & RCV_SHUTDOWN)
8125 break;
8126 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
8127 asoc->base.dead)
8128 goto do_error;
8129 if (signal_pending(current))
8130 goto do_interrupted;
8131
8132 if (sctp_state(asoc, ESTABLISHED))
8133 break;
8134
8135 /* Let another process have a go. Since we are going
8136 * to sleep anyway.
8137 */
wangweidong048ed4b2014-01-21 15:44:11 +08008138 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008140 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008141
8142 *timeo_p = current_timeo;
8143 }
8144
8145out:
8146 finish_wait(&asoc->wait, &wait);
8147
8148 /* Release the association's refcnt. */
8149 sctp_association_put(asoc);
8150
8151 return err;
8152
8153do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08008154 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155 err = -ETIMEDOUT;
8156 else
8157 err = -ECONNREFUSED;
8158 goto out;
8159
8160do_interrupted:
8161 err = sock_intr_errno(*timeo_p);
8162 goto out;
8163
8164do_nonblock:
8165 err = -EINPROGRESS;
8166 goto out;
8167}
8168
8169static int sctp_wait_for_accept(struct sock *sk, long timeo)
8170{
8171 struct sctp_endpoint *ep;
8172 int err = 0;
8173 DEFINE_WAIT(wait);
8174
8175 ep = sctp_sk(sk)->ep;
8176
8177
8178 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00008179 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008180 TASK_INTERRUPTIBLE);
8181
8182 if (list_empty(&ep->asocs)) {
wangweidong048ed4b2014-01-21 15:44:11 +08008183 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008184 timeo = schedule_timeout(timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08008185 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008186 }
8187
8188 err = -EINVAL;
8189 if (!sctp_sstate(sk, LISTENING))
8190 break;
8191
8192 err = 0;
8193 if (!list_empty(&ep->asocs))
8194 break;
8195
8196 err = sock_intr_errno(timeo);
8197 if (signal_pending(current))
8198 break;
8199
8200 err = -EAGAIN;
8201 if (!timeo)
8202 break;
8203 }
8204
Eric Dumazetaa395142010-04-20 13:03:51 +00008205 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008206
8207 return err;
8208}
8209
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02008210static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008211{
8212 DEFINE_WAIT(wait);
8213
8214 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00008215 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216 if (list_empty(&sctp_sk(sk)->ep->asocs))
8217 break;
wangweidong048ed4b2014-01-21 15:44:11 +08008218 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008219 timeout = schedule_timeout(timeout);
wangweidong048ed4b2014-01-21 15:44:11 +08008220 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221 } while (!signal_pending(current) && timeout);
8222
Eric Dumazetaa395142010-04-20 13:03:51 +00008223 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224}
8225
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008226static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
8227{
8228 struct sk_buff *frag;
8229
8230 if (!skb->data_len)
8231 goto done;
8232
8233 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07008234 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008235 sctp_skb_set_owner_r_frag(frag, sk);
8236
8237done:
8238 sctp_skb_set_owner_r(skb, sk);
8239}
8240
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008241void sctp_copy_sock(struct sock *newsk, struct sock *sk,
8242 struct sctp_association *asoc)
8243{
8244 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08008245 struct inet_sock *newinet;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008246
8247 newsk->sk_type = sk->sk_type;
8248 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
8249 newsk->sk_flags = sk->sk_flags;
Marcelo Ricardo Leitner50a5ffb2015-12-04 15:14:05 -02008250 newsk->sk_tsflags = sk->sk_tsflags;
Tom Herbert28448b82014-05-23 08:47:19 -07008251 newsk->sk_no_check_tx = sk->sk_no_check_tx;
8252 newsk->sk_no_check_rx = sk->sk_no_check_rx;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008253 newsk->sk_reuse = sk->sk_reuse;
8254
8255 newsk->sk_shutdown = sk->sk_shutdown;
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02008256 newsk->sk_destruct = sctp_destruct_sock;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008257 newsk->sk_family = sk->sk_family;
8258 newsk->sk_protocol = IPPROTO_SCTP;
8259 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
8260 newsk->sk_sndbuf = sk->sk_sndbuf;
8261 newsk->sk_rcvbuf = sk->sk_rcvbuf;
8262 newsk->sk_lingertime = sk->sk_lingertime;
8263 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
8264 newsk->sk_sndtimeo = sk->sk_sndtimeo;
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03008265 newsk->sk_rxhash = sk->sk_rxhash;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008266
8267 newinet = inet_sk(newsk);
8268
8269 /* Initialize sk's sport, dport, rcv_saddr and daddr for
8270 * getsockname() and getpeername()
8271 */
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008272 newinet->inet_sport = inet->inet_sport;
8273 newinet->inet_saddr = inet->inet_saddr;
8274 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
8275 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008276 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008277 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008278
8279 newinet->uc_ttl = inet->uc_ttl;
8280 newinet->mc_loop = 1;
8281 newinet->mc_ttl = 1;
8282 newinet->mc_index = 0;
8283 newinet->mc_list = NULL;
Marcelo Ricardo Leitner01ce63c2015-12-04 15:14:04 -02008284
8285 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
8286 net_enable_timestamp();
Marcelo Ricardo Leitner3538a5c2015-12-23 16:44:09 -02008287
8288 security_sk_clone(sk, newsk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00008289}
8290
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008291static inline void sctp_copy_descendant(struct sock *sk_to,
8292 const struct sock *sk_from)
8293{
8294 int ancestor_size = sizeof(struct inet_sock) +
8295 sizeof(struct sctp_sock) -
8296 offsetof(struct sctp_sock, auto_asconf_list);
8297
8298 if (sk_from->sk_family == PF_INET6)
8299 ancestor_size += sizeof(struct ipv6_pinfo);
8300
8301 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
8302}
8303
Linus Torvalds1da177e2005-04-16 15:20:36 -07008304/* Populate the fields of the newsk from the oldsk and migrate the assoc
8305 * and its messages to the newsk.
8306 */
8307static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
8308 struct sctp_association *assoc,
Xin Longb7ef2612017-08-11 10:23:50 +08008309 enum sctp_socket_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008310{
8311 struct sctp_sock *oldsp = sctp_sk(oldsk);
8312 struct sctp_sock *newsp = sctp_sk(newsk);
8313 struct sctp_bind_bucket *pp; /* hash list port iterator */
8314 struct sctp_endpoint *newep = newsp->ep;
8315 struct sk_buff *skb, *tmp;
8316 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08008317 struct sctp_bind_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008318
8319 /* Migrate socket buffer sizes and all the socket level options to the
8320 * new socket.
8321 */
8322 newsk->sk_sndbuf = oldsk->sk_sndbuf;
8323 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
8324 /* Brute force copy old sctp opt. */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03008325 sctp_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008326
8327 /* Restore the ep value that was overwritten with the above structure
8328 * copy.
8329 */
8330 newsp->ep = newep;
8331 newsp->hmac = NULL;
8332
8333 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00008334 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
8335 inet_sk(oldsk)->inet_num)];
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008336 spin_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008337 pp = sctp_sk(oldsk)->bind_hash;
8338 sk_add_bind_node(newsk, &pp->owner);
8339 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00008340 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01008341 spin_unlock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008342
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008343 /* Copy the bind_addr list from the original endpoint to the new
8344 * endpoint so that we can handle restarts properly
8345 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08008346 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
8347 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07008348
Linus Torvalds1da177e2005-04-16 15:20:36 -07008349 /* Move any messages in the old socket's receive queue that are for the
8350 * peeled off association to the new socket's receive queue.
8351 */
8352 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
8353 event = sctp_skb2event(skb);
8354 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008355 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008356 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008357 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008358 }
8359 }
8360
8361 /* Clean up any messages pending delivery due to partial
8362 * delivery. Three cases:
8363 * 1) No partial deliver; no work.
8364 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
8365 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
8366 */
8367 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008368 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008369
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008370 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008371 struct sk_buff_head *queue;
8372
8373 /* Decide which queue to move pd_lobby skbs to. */
8374 if (assoc->ulpq.pd_mode) {
8375 queue = &newsp->pd_lobby;
8376 } else
8377 queue = &newsk->sk_receive_queue;
8378
8379 /* Walk through the pd_lobby, looking for skbs that
8380 * need moved to the new socket.
8381 */
8382 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
8383 event = sctp_skb2event(skb);
8384 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07008385 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008387 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388 }
8389 }
8390
8391 /* Clear up any skbs waiting for the partial
8392 * delivery to finish.
8393 */
8394 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07008395 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008396
8397 }
8398
Wei Yongjun1bc4ee42009-07-05 19:45:48 +00008399 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008400 sctp_skb_set_owner_r_frag(skb, newsk);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008401
Wei Yongjun1bc4ee42009-07-05 19:45:48 +00008402 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008403 sctp_skb_set_owner_r_frag(skb, newsk);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07008404
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405 /* Set the type of socket to indicate that it is peeled off from the
8406 * original UDP-style socket or created with the accept() call on a
8407 * TCP-style socket..
8408 */
8409 newsp->type = type;
8410
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008411 /* Mark the new socket "in-use" by the user so that any packets
8412 * that may arrive on the association after we've moved it are
8413 * queued to the backlog. This prevents a potential race between
8414 * backlog processing on the old socket and new-packet processing
8415 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07008416 *
8417 * The caller has just allocated newsk so we can guarantee that other
8418 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07008419 */
Zach Brown5131a182007-06-22 15:14:46 -07008420 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Xin Longd04adf12017-10-28 02:13:29 +08008421 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008422 sctp_assoc_migrate(assoc, newsk);
Xin Longd04adf12017-10-28 02:13:29 +08008423 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008424
8425 /* If the association on the newsk is already closed before accept()
8426 * is called, set RCV_SHUTDOWN flag.
8427 */
Xin Longd46e4162016-06-09 22:48:18 +08008428 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
Xin Long141ddef2016-06-16 01:15:06 +08008429 newsk->sk_state = SCTP_SS_CLOSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008430 newsk->sk_shutdown |= RCV_SHUTDOWN;
Xin Longd46e4162016-06-09 22:48:18 +08008431 } else {
8432 newsk->sk_state = SCTP_SS_ESTABLISHED;
8433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008434
wangweidong048ed4b2014-01-21 15:44:11 +08008435 release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008436}
8437
Neil Horman4d93df02007-08-15 16:07:44 -07008438
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439/* This proto struct describes the ULP interface for SCTP. */
8440struct proto sctp_prot = {
8441 .name = "SCTP",
8442 .owner = THIS_MODULE,
8443 .close = sctp_close,
8444 .connect = sctp_connect,
8445 .disconnect = sctp_disconnect,
8446 .accept = sctp_accept,
8447 .ioctl = sctp_ioctl,
8448 .init = sctp_init_sock,
8449 .destroy = sctp_destroy_sock,
8450 .shutdown = sctp_shutdown,
8451 .setsockopt = sctp_setsockopt,
8452 .getsockopt = sctp_getsockopt,
8453 .sendmsg = sctp_sendmsg,
8454 .recvmsg = sctp_recvmsg,
8455 .bind = sctp_bind,
8456 .backlog_rcv = sctp_backlog_rcv,
8457 .hash = sctp_hash,
8458 .unhash = sctp_unhash,
8459 .get_port = sctp_get_port,
8460 .obj_size = sizeof(struct sctp_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07008461 .sysctl_mem = sysctl_sctp_mem,
8462 .sysctl_rmem = sysctl_sctp_rmem,
8463 .sysctl_wmem = sysctl_sctp_wmem,
8464 .memory_pressure = &sctp_memory_pressure,
8465 .enter_memory_pressure = sctp_enter_memory_pressure,
8466 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008467 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008468};
8469
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008470#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08008471
Eric Dumazet602dd622015-12-01 07:20:07 -08008472#include <net/transp_v6.h>
8473static void sctp_v6_destroy_sock(struct sock *sk)
8474{
8475 sctp_destroy_sock(sk);
8476 inet6_destroy_sock(sk);
8477}
8478
Linus Torvalds1da177e2005-04-16 15:20:36 -07008479struct proto sctpv6_prot = {
8480 .name = "SCTPv6",
8481 .owner = THIS_MODULE,
8482 .close = sctp_close,
8483 .connect = sctp_connect,
8484 .disconnect = sctp_disconnect,
8485 .accept = sctp_accept,
8486 .ioctl = sctp_ioctl,
8487 .init = sctp_init_sock,
Eric Dumazet602dd622015-12-01 07:20:07 -08008488 .destroy = sctp_v6_destroy_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008489 .shutdown = sctp_shutdown,
8490 .setsockopt = sctp_setsockopt,
8491 .getsockopt = sctp_getsockopt,
8492 .sendmsg = sctp_sendmsg,
8493 .recvmsg = sctp_recvmsg,
8494 .bind = sctp_bind,
8495 .backlog_rcv = sctp_backlog_rcv,
8496 .hash = sctp_hash,
8497 .unhash = sctp_unhash,
8498 .get_port = sctp_get_port,
8499 .obj_size = sizeof(struct sctp6_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07008500 .sysctl_mem = sysctl_sctp_mem,
8501 .sysctl_rmem = sysctl_sctp_rmem,
8502 .sysctl_wmem = sysctl_sctp_wmem,
8503 .memory_pressure = &sctp_memory_pressure,
8504 .enter_memory_pressure = sctp_enter_memory_pressure,
8505 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08008506 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00008508#endif /* IS_ENABLED(CONFIG_IPV6) */