blob: c472b8391dde45ba8920a4effbc50f699ca4cc10 [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>
60#include <linux/ip.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080061#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/fcntl.h>
63#include <linux/poll.h>
64#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090065#include <linux/slab.h>
Al Viro56b31d12012-08-18 00:25:51 -040066#include <linux/file.h>
Daniel Borkmannffd59392014-02-17 12:11:11 +010067#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#include <net/ip.h>
70#include <net/icmp.h>
71#include <net/route.h>
72#include <net/ipv6.h>
73#include <net/inet_common.h>
Neil Horman8465a5f2014-04-17 15:26:51 -040074#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76#include <linux/socket.h> /* for sa_family_t */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040077#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <net/sock.h>
79#include <net/sctp/sctp.h>
80#include <net/sctp/sm.h>
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* Forward declarations for internal helper functions. */
83static int sctp_writeable(struct sock *sk);
84static void sctp_wfree(struct sk_buff *skb);
Xin Long9904da52017-11-15 16:57:26 +080085static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Long2f056e72018-01-15 17:01:36 +080086 size_t msg_len);
wangweidong26ac8e52013-12-23 12:16:51 +080087static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
89static int sctp_wait_for_accept(struct sock *sk, long timeo);
90static void sctp_wait_for_close(struct sock *sk, long timeo);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +020091static void sctp_destruct_sock(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
93 union sctp_addr *addr, int len);
94static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
95static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
96static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
97static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
98static int sctp_send_asconf(struct sctp_association *asoc,
99 struct sctp_chunk *chunk);
100static int sctp_do_bind(struct sock *, union sctp_addr *, int);
101static int sctp_autobind(struct sock *sk);
102static void sctp_sock_migrate(struct sock *, struct sock *,
103 struct sctp_association *, sctp_socket_type_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Adrian Bunkb6fa1a42007-09-12 15:18:00 +0200105static int sctp_memory_pressure;
Eric Dumazet8d987e52010-11-09 23:24:26 +0000106static atomic_long_t sctp_memory_allocated;
Eric Dumazet17483762008-11-25 21:16:35 -0800107struct percpu_counter sctp_sockets_allocated;
Neil Horman4d93df02007-08-15 16:07:44 -0700108
Pavel Emelyanov5c52ba12008-07-16 20:28:10 -0700109static void sctp_enter_memory_pressure(struct sock *sk)
Neil Horman4d93df02007-08-15 16:07:44 -0700110{
111 sctp_memory_pressure = 1;
112}
113
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* Get the sndbuf space available at the time on the association. */
116static inline int sctp_wspace(struct sctp_association *asoc)
117{
Neil Horman4d93df02007-08-15 16:07:44 -0700118 int amt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Neil Horman4d93df02007-08-15 16:07:44 -0700120 if (asoc->ep->sndbuf_policy)
121 amt = asoc->sndbuf_used;
122 else
Eric Dumazet31e6d362009-06-17 19:05:41 -0700123 amt = sk_wmem_alloc_get(asoc->base.sk);
Neil Horman4d93df02007-08-15 16:07:44 -0700124
125 if (amt >= asoc->base.sk->sk_sndbuf) {
126 if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
127 amt = 0;
128 else {
129 amt = sk_stream_wspace(asoc->base.sk);
130 if (amt < 0)
131 amt = 0;
132 }
Neil Horman4eb701d2005-04-28 12:02:04 -0700133 } else {
Neil Horman4d93df02007-08-15 16:07:44 -0700134 amt = asoc->base.sk->sk_sndbuf - amt;
Neil Horman4eb701d2005-04-28 12:02:04 -0700135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return amt;
137}
138
139/* Increment the used sndbuf space count of the corresponding association by
140 * the size of the outgoing data chunk.
141 * Also, set the skb destructor for sndbuf accounting later.
142 *
143 * Since it is always 1-1 between chunk and skb, and also a new skb is always
144 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
145 * destructor in the data chunk skb for the purpose of the sndbuf space
146 * tracking.
147 */
148static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
149{
150 struct sctp_association *asoc = chunk->asoc;
151 struct sock *sk = asoc->base.sk;
152
153 /* The sndbuf space is tracked per association. */
154 sctp_association_hold(asoc);
155
Neil Horman4eb701d2005-04-28 12:02:04 -0700156 skb_set_owner_w(chunk->skb, sk);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 chunk->skb->destructor = sctp_wfree;
159 /* Save the chunk pointer in skb for sctp_wfree to use later. */
Daniel Borkmannf869c912014-11-20 01:54:48 +0100160 skb_shinfo(chunk->skb)->destructor_arg = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Neil Horman4eb701d2005-04-28 12:02:04 -0700162 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
163 sizeof(struct sk_buff) +
164 sizeof(struct sctp_chunk);
165
Neil Horman4eb701d2005-04-28 12:02:04 -0700166 atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800167 sk->sk_wmem_queued += chunk->skb->truesize;
168 sk_mem_charge(sk, chunk->skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Xin Longb89fc6a2017-10-28 02:13:29 +0800171static void sctp_clear_owner_w(struct sctp_chunk *chunk)
172{
173 skb_orphan(chunk->skb);
174}
175
176static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
177 void (*cb)(struct sctp_chunk *))
178
179{
180 struct sctp_outq *q = &asoc->outqueue;
181 struct sctp_transport *t;
182 struct sctp_chunk *chunk;
183
184 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
185 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
186 cb(chunk);
187
188 list_for_each_entry(chunk, &q->retransmit, list)
189 cb(chunk);
190
191 list_for_each_entry(chunk, &q->sacked, list)
192 cb(chunk);
193
194 list_for_each_entry(chunk, &q->abandoned, list)
195 cb(chunk);
196
197 list_for_each_entry(chunk, &q->out_chunk_list, list)
198 cb(chunk);
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/* Verify that this is a valid address. */
202static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
203 int len)
204{
205 struct sctp_af *af;
206
207 /* Verify basic sockaddr. */
208 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
209 if (!af)
210 return -EINVAL;
211
212 /* Is this a valid SCTP address? */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700213 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return -EINVAL;
215
216 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
217 return -EINVAL;
218
219 return 0;
220}
221
222/* Look up the association by its id. If this is not a UDP-style
223 * socket, the ID field is always ignored.
224 */
225struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
226{
227 struct sctp_association *asoc = NULL;
228
229 /* If this is not a UDP-style socket, assoc id should be ignored. */
230 if (!sctp_style(sk, UDP)) {
231 /* Return NULL if the socket state is not ESTABLISHED. It
232 * could be a TCP-style listening socket or a socket which
233 * hasn't yet called connect() to establish an association.
234 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -0300235 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return NULL;
237
238 /* Get the first and the only association from the list. */
239 if (!list_empty(&sctp_sk(sk)->ep->asocs))
240 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
241 struct sctp_association, asocs);
242 return asoc;
243 }
244
245 /* Otherwise this is a UDP-style socket. */
246 if (!id || (id == (sctp_assoc_t)-1))
247 return NULL;
248
249 spin_lock_bh(&sctp_assocs_id_lock);
250 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
251 spin_unlock_bh(&sctp_assocs_id_lock);
252
253 if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
254 return NULL;
255
256 return asoc;
257}
258
259/* Look up the transport from an address and an assoc id. If both address and
260 * id are specified, the associations matching the address and the id should be
261 * the same.
262 */
263static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
264 struct sockaddr_storage *addr,
265 sctp_assoc_t id)
266{
267 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
Xin Longfb72eca2017-01-24 14:01:53 +0800268 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 union sctp_addr *laddr = (union sctp_addr *)addr;
Xin Longfb72eca2017-01-24 14:01:53 +0800270 struct sctp_transport *transport;
271
Xin Long168bd512017-02-07 20:56:08 +0800272 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
Xin Longfb72eca2017-01-24 14:01:53 +0800273 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
Al Virocd4ff032006-11-20 17:11:33 -0800276 laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (!addr_asoc)
280 return NULL;
281
282 id_asoc = sctp_id2assoc(sk, id);
283 if (id_asoc && (id_asoc != addr_asoc))
284 return NULL;
285
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600286 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 (union sctp_addr *)addr);
288
289 return transport;
290}
291
292/* API 3.1.2 bind() - UDP Style Syntax
293 * The syntax of bind() is,
294 *
295 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
296 *
297 * sd - the socket descriptor returned by socket().
298 * addr - the address structure (struct sockaddr_in or struct
299 * sockaddr_in6 [RFC 2553]),
300 * addr_len - the size of the address structure.
301 */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200302static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 int retval = 0;
305
wangweidong048ed4b2014-01-21 15:44:11 +0800306 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Daniel Borkmannbb333812013-06-28 19:49:40 +0200308 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
309 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* Disallow binding twice. */
312 if (!sctp_sk(sk)->ep->base.bind_addr.port)
Frank Filz3f7a87d2005-06-20 13:14:57 -0700313 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 addr_len);
315 else
316 retval = -EINVAL;
317
wangweidong048ed4b2014-01-21 15:44:11 +0800318 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 return retval;
321}
322
323static long sctp_get_port_local(struct sock *, union sctp_addr *);
324
325/* Verify this is a valid sockaddr. */
326static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
327 union sctp_addr *addr, int len)
328{
329 struct sctp_af *af;
330
331 /* Check minimum size. */
332 if (len < sizeof (struct sockaddr))
333 return NULL;
334
Xin Long8e3534ea2018-01-15 17:02:00 +0800335 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
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 &&
Xin Long8e3534ea2018-01-15 17:02:00 +0800340 ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
341 !opt->pf->af_supported(AF_INET, opt))
342 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /* If we get this far, af is valid. */
345 af = sctp_get_af_specific(addr->sa.sa_family);
346
347 if (len < af->sockaddr_len)
348 return NULL;
349
350 return af;
351}
352
353/* Bind a local address either to an endpoint or to an association. */
Daniel Borkmanndda91922013-06-17 11:40:05 +0200354static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Eric W. Biederman35946982012-11-16 03:03:12 +0000356 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 struct sctp_sock *sp = sctp_sk(sk);
358 struct sctp_endpoint *ep = sp->ep;
359 struct sctp_bind_addr *bp = &ep->base.bind_addr;
360 struct sctp_af *af;
361 unsigned short snum;
362 int ret = 0;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /* Common sockaddr verification. */
365 af = sctp_sockaddr_af(sp, addr, len);
Frank Filz3f7a87d2005-06-20 13:14:57 -0700366 if (!af) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200367 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
368 __func__, sk, addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return -EINVAL;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700370 }
371
372 snum = ntohs(addr->v4.sin_port);
373
Daniel Borkmannbb333812013-06-28 19:49:40 +0200374 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
375 __func__, sk, &addr->sa, bp->port, snum, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /* PF specific bind() address verification. */
378 if (!sp->pf->bind_verify(sp, addr))
379 return -EADDRNOTAVAIL;
380
Vlad Yasevich8b358052007-05-15 17:14:58 -0400381 /* We must either be unbound, or bind to the same port.
382 * It's OK to allow 0 ports if we are already bound.
383 * We'll just inhert an already bound port in this case
384 */
385 if (bp->port) {
386 if (!snum)
387 snum = bp->port;
388 else if (snum != bp->port) {
Daniel Borkmannbb333812013-06-28 19:49:40 +0200389 pr_debug("%s: new port %d doesn't match existing port "
390 "%d\n", __func__, snum, bp->port);
Vlad Yasevich8b358052007-05-15 17:14:58 -0400391 return -EINVAL;
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
Eric W. Biederman35946982012-11-16 03:03:12 +0000395 if (snum && snum < PROT_SOCK &&
396 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return -EACCES;
398
Vlad Yasevich4e540642008-07-18 23:06:32 -0700399 /* See if the address matches any of the addresses we may have
400 * already bound before checking against other endpoints.
401 */
402 if (sctp_bind_addr_match(bp, addr, sp))
403 return -EINVAL;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* Make sure we are allowed to bind here.
406 * The function sctp_get_port_local() does duplicate address
407 * detection.
408 */
Vlad Yasevich2772b492007-08-21 14:24:30 +0900409 addr->v4.sin_port = htons(snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if ((ret = sctp_get_port_local(sk, addr))) {
Vlad Yasevich4e540642008-07-18 23:06:32 -0700411 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
414 /* Refresh ephemeral port. */
415 if (!bp->port)
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000416 bp->port = inet_sk(sk)->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Vlad Yasevich559cf712007-09-16 16:03:28 -0700418 /* Add the address to the bind address list.
419 * Use GFP_ATOMIC since BHs will be disabled.
420 */
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300421 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
422 SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /* Copy back into socket for getsockname() use. */
425 if (!ret) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000426 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
Jason Gunthorpe299ee122014-07-30 12:40:53 -0600427 sp->pf->to_sk_saddr(addr, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
430 return ret;
431}
432
433 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
434 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900435 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 * at any one time. If a sender, after sending an ASCONF chunk, decides
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900437 * it needs to transfer another ASCONF Chunk, it MUST wait until the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900439 * subsequent ASCONF. Note this restriction binds each side, so at any
440 * time two ASCONF may be in-transit on any given association (one sent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * from each endpoint).
442 */
443static int sctp_send_asconf(struct sctp_association *asoc,
444 struct sctp_chunk *chunk)
445{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000446 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int retval = 0;
448
449 /* If there is an outstanding ASCONF chunk, queue it for later
450 * transmission.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900451 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (asoc->addip_last_asconf) {
David S. Miller79af02c2005-07-08 21:47:49 -0700453 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900454 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456
457 /* Hold the chunk until an ASCONF_ACK is received. */
458 sctp_chunk_hold(chunk);
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000459 retval = sctp_primitive_ASCONF(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (retval)
461 sctp_chunk_free(chunk);
462 else
463 asoc->addip_last_asconf = chunk;
464
465out:
466 return retval;
467}
468
469/* Add a list of addresses as bind addresses to local endpoint or
470 * association.
471 *
472 * Basically run through each address specified in the addrs/addrcnt
473 * array/length pair, determine if it is IPv6 or IPv4 and call
474 * sctp_do_bind() on it.
475 *
476 * If any of them fails, then the operation will be reversed and the
477 * ones that were added will be removed.
478 *
479 * Only sctp_setsockopt_bindx() is supposed to call this function.
480 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200481static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 int cnt;
484 int retval = 0;
485 void *addr_buf;
486 struct sockaddr *sa_addr;
487 struct sctp_af *af;
488
Daniel Borkmannbb333812013-06-28 19:49:40 +0200489 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
490 addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 addr_buf = addrs;
493 for (cnt = 0; cnt < addrcnt; cnt++) {
494 /* The list may contain either IPv4 or IPv6 address;
495 * determine the address length for walking thru the list.
496 */
Joe Perchesea110732011-06-13 16:21:26 +0000497 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 af = sctp_get_af_specific(sa_addr->sa_family);
499 if (!af) {
500 retval = -EINVAL;
501 goto err_bindx_add;
502 }
503
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900504 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 af->sockaddr_len);
506
507 addr_buf += af->sockaddr_len;
508
509err_bindx_add:
510 if (retval < 0) {
511 /* Failed. Cleanup the ones that have been added */
512 if (cnt > 0)
513 sctp_bindx_rem(sk, addrs, cnt);
514 return retval;
515 }
516 }
517
518 return retval;
519}
520
521/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
522 * associations that are part of the endpoint indicating that a list of local
523 * addresses are added to the endpoint.
524 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900525 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 * association, we do not send the chunk for that association. But it will not
527 * affect other associations.
528 *
529 * Only sctp_setsockopt_bindx() is supposed to call this function.
530 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900531static int sctp_send_asconf_add_ip(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 struct sockaddr *addrs,
533 int addrcnt)
534{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000535 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 struct sctp_sock *sp;
537 struct sctp_endpoint *ep;
538 struct sctp_association *asoc;
539 struct sctp_bind_addr *bp;
540 struct sctp_chunk *chunk;
541 struct sctp_sockaddr_entry *laddr;
542 union sctp_addr *addr;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700543 union sctp_addr saveaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 void *addr_buf;
545 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct list_head *p;
547 int i;
548 int retval = 0;
549
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000550 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return retval;
552
553 sp = sctp_sk(sk);
554 ep = sp->ep;
555
Daniel Borkmannbb333812013-06-28 19:49:40 +0200556 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
557 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700559 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (!asoc->peer.asconf_capable)
561 continue;
562
563 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
564 continue;
565
566 if (!sctp_state(asoc, ESTABLISHED))
567 continue;
568
569 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900570 * in the bind address list of the association. If so,
571 * do not send the asconf chunk to its peer, but continue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 * other associations.
573 */
574 addr_buf = addrs;
575 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000576 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 af = sctp_get_af_specific(addr->v4.sin_family);
578 if (!af) {
579 retval = -EINVAL;
580 goto out;
581 }
582
583 if (sctp_assoc_lookup_laddr(asoc, addr))
584 break;
585
586 addr_buf += af->sockaddr_len;
587 }
588 if (i < addrcnt)
589 continue;
590
Vlad Yasevich559cf712007-09-16 16:03:28 -0700591 /* Use the first valid address in bind addr list of
592 * association as Address Parameter of ASCONF CHUNK.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 bp = &asoc->base.bind_addr;
595 p = bp->address_list.next;
596 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
Al Viro5ae955c2006-11-20 17:22:08 -0800597 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 addrcnt, SCTP_PARAM_ADD_IP);
599 if (!chunk) {
600 retval = -ENOMEM;
601 goto out;
602 }
603
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700604 /* Add the new addresses to the bind address list with
605 * use_as_src set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700607 addr_buf = addrs;
608 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000609 addr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700610 af = sctp_get_af_specific(addr->v4.sin_family);
611 memcpy(&saveaddr, addr, af->sockaddr_len);
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800612 retval = sctp_add_bind_addr(bp, &saveaddr,
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -0300613 sizeof(saveaddr),
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800614 SCTP_ADDR_NEW, GFP_ATOMIC);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700615 addr_buf += af->sockaddr_len;
616 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900617 if (asoc->src_out_of_asoc_ok) {
618 struct sctp_transport *trans;
619
620 list_for_each_entry(trans,
621 &asoc->peer.transport_addr_list, transports) {
622 /* Clear the source and route cache */
623 dst_release(trans->dst);
624 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
625 2*asoc->pathmtu, 4380));
626 trans->ssthresh = asoc->peer.i.a_rwnd;
627 trans->rto = asoc->rto_initial;
Michele Baldessari196d6752012-12-01 04:49:42 +0000628 sctp_max_rto(asoc, trans);
Michio Honda8a07eb02011-04-26 20:19:36 +0900629 trans->rtt = trans->srtt = trans->rttvar = 0;
630 sctp_transport_route(trans, NULL,
631 sctp_sk(asoc->base.sk));
632 }
633 }
634 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
637out:
638 return retval;
639}
640
641/* Remove a list of addresses from bind addresses list. Do not remove the
642 * last address.
643 *
644 * Basically run through each address specified in the addrs/addrcnt
645 * array/length pair, determine if it is IPv6 or IPv4 and call
646 * sctp_del_bind() on it.
647 *
648 * If any of them fails, then the operation will be reversed and the
649 * ones that were removed will be added back.
650 *
651 * At least one address has to be left; if only one address is
652 * available, the operation will return -EBUSY.
653 *
654 * Only sctp_setsockopt_bindx() is supposed to call this function.
655 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200656static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 struct sctp_sock *sp = sctp_sk(sk);
659 struct sctp_endpoint *ep = sp->ep;
660 int cnt;
661 struct sctp_bind_addr *bp = &ep->base.bind_addr;
662 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 void *addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800664 union sctp_addr *sa_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 struct sctp_af *af;
666
Daniel Borkmannbb333812013-06-28 19:49:40 +0200667 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
668 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 addr_buf = addrs;
671 for (cnt = 0; cnt < addrcnt; cnt++) {
672 /* If the bind address list is empty or if there is only one
673 * bind address, there is nothing more to be removed (we need
674 * at least one address here).
675 */
676 if (list_empty(&bp->address_list) ||
677 (sctp_list_single_entry(&bp->address_list))) {
678 retval = -EBUSY;
679 goto err_bindx_rem;
680 }
681
Joe Perchesea110732011-06-13 16:21:26 +0000682 sa_addr = addr_buf;
Al Viroc9a08502006-11-20 17:07:48 -0800683 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (!af) {
685 retval = -EINVAL;
686 goto err_bindx_rem;
687 }
Paolo Galtieri0304ff8a2007-04-17 12:52:36 -0700688
689 if (!af->addr_valid(sa_addr, sp, NULL)) {
690 retval = -EADDRNOTAVAIL;
691 goto err_bindx_rem;
692 }
693
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000694 if (sa_addr->v4.sin_port &&
695 sa_addr->v4.sin_port != htons(bp->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 retval = -EINVAL;
697 goto err_bindx_rem;
698 }
699
Vlad Yasevichee9cbac2011-04-18 19:14:47 +0000700 if (!sa_addr->v4.sin_port)
701 sa_addr->v4.sin_port = htons(bp->port);
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /* FIXME - There is probably a need to check if sk->sk_saddr and
704 * sk->sk_rcv_addr are currently set to one of the addresses to
705 * be removed. This is something which needs to be looked into
706 * when we are fixing the outstanding issues with multi-homing
707 * socket routing and failover schemes. Refer to comments in
708 * sctp_do_bind(). -daisy
709 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -0400710 retval = sctp_del_bind_addr(bp, sa_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 addr_buf += af->sockaddr_len;
713err_bindx_rem:
714 if (retval < 0) {
715 /* Failed. Add the ones that has been removed back */
716 if (cnt > 0)
717 sctp_bindx_add(sk, addrs, cnt);
718 return retval;
719 }
720 }
721
722 return retval;
723}
724
725/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
726 * the associations that are part of the endpoint indicating that a list of
727 * local addresses are removed from the endpoint.
728 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900729 * If any of the addresses is already in the bind address list of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 * association, we do not send the chunk for that association. But it will not
731 * affect other associations.
732 *
733 * Only sctp_setsockopt_bindx() is supposed to call this function.
734 */
735static int sctp_send_asconf_del_ip(struct sock *sk,
736 struct sockaddr *addrs,
737 int addrcnt)
738{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000739 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 struct sctp_sock *sp;
741 struct sctp_endpoint *ep;
742 struct sctp_association *asoc;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700743 struct sctp_transport *transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 struct sctp_bind_addr *bp;
745 struct sctp_chunk *chunk;
746 union sctp_addr *laddr;
747 void *addr_buf;
748 struct sctp_af *af;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700749 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 int i;
751 int retval = 0;
Michio Honda8a07eb02011-04-26 20:19:36 +0900752 int stored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Michio Honda8a07eb02011-04-26 20:19:36 +0900754 chunk = NULL;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000755 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return retval;
757
758 sp = sctp_sk(sk);
759 ep = sp->ep;
760
Daniel Borkmannbb333812013-06-28 19:49:40 +0200761 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
762 __func__, sk, addrs, addrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700764 list_for_each_entry(asoc, &ep->asocs, asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if (!asoc->peer.asconf_capable)
767 continue;
768
769 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
770 continue;
771
772 if (!sctp_state(asoc, ESTABLISHED))
773 continue;
774
775 /* Check if any address in the packed array of addresses is
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900776 * not present in the bind address list of the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * If so, do not send the asconf chunk to its peer, but
778 * continue with other associations.
779 */
780 addr_buf = addrs;
781 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000782 laddr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 af = sctp_get_af_specific(laddr->v4.sin_family);
784 if (!af) {
785 retval = -EINVAL;
786 goto out;
787 }
788
789 if (!sctp_assoc_lookup_laddr(asoc, laddr))
790 break;
791
792 addr_buf += af->sockaddr_len;
793 }
794 if (i < addrcnt)
795 continue;
796
797 /* Find one address in the association's bind address list
798 * that is not in the packed array of addresses. This is to
799 * make sure that we do not delete all the addresses in the
800 * association.
801 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 bp = &asoc->base.bind_addr;
803 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
804 addrcnt, sp);
Michio Honda8a07eb02011-04-26 20:19:36 +0900805 if ((laddr == NULL) && (addrcnt == 1)) {
806 if (asoc->asconf_addr_del_pending)
807 continue;
808 asoc->asconf_addr_del_pending =
809 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
Michio Honda6d65e5e2011-06-10 16:42:14 +0900810 if (asoc->asconf_addr_del_pending == NULL) {
811 retval = -ENOMEM;
812 goto out;
813 }
Michio Honda8a07eb02011-04-26 20:19:36 +0900814 asoc->asconf_addr_del_pending->sa.sa_family =
815 addrs->sa_family;
816 asoc->asconf_addr_del_pending->v4.sin_port =
817 htons(bp->port);
818 if (addrs->sa_family == AF_INET) {
819 struct sockaddr_in *sin;
820
821 sin = (struct sockaddr_in *)addrs;
822 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
823 } else if (addrs->sa_family == AF_INET6) {
824 struct sockaddr_in6 *sin6;
825
826 sin6 = (struct sockaddr_in6 *)addrs;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000827 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
Michio Honda8a07eb02011-04-26 20:19:36 +0900828 }
Daniel Borkmannbb333812013-06-28 19:49:40 +0200829
830 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
831 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
832 asoc->asconf_addr_del_pending);
833
Michio Honda8a07eb02011-04-26 20:19:36 +0900834 asoc->src_out_of_asoc_ok = 1;
835 stored = 1;
836 goto skip_mkasconf;
837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Daniel Borkmann88362ad2013-09-07 20:51:21 +0200839 if (laddr == NULL)
840 return -EINVAL;
841
Vlad Yasevich559cf712007-09-16 16:03:28 -0700842 /* We do not need RCU protection throughout this loop
843 * because this is done under a socket lock from the
844 * setsockopt call.
845 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
847 SCTP_PARAM_DEL_IP);
848 if (!chunk) {
849 retval = -ENOMEM;
850 goto out;
851 }
852
Michio Honda8a07eb02011-04-26 20:19:36 +0900853skip_mkasconf:
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700854 /* Reset use_as_src flag for the addresses in the bind address
855 * list that are to be deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700857 addr_buf = addrs;
858 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +0000859 laddr = addr_buf;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700860 af = sctp_get_af_specific(laddr->v4.sin_family);
Vlad Yasevich559cf712007-09-16 16:03:28 -0700861 list_for_each_entry(saddr, &bp->address_list, list) {
Al Viro5f242a12006-11-20 17:05:23 -0800862 if (sctp_cmp_addr_exact(&saddr->a, laddr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -0800863 saddr->state = SCTP_ADDR_DEL;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700864 }
865 addr_buf += af->sockaddr_len;
866 }
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700867
868 /* Update the route and saddr entries for all the transports
869 * as some of the addresses in the bind address list are
870 * about to be deleted and cannot be used as source addresses.
871 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700872 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
873 transports) {
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700874 dst_release(transport->dst);
875 sctp_transport_route(transport, NULL,
876 sctp_sk(asoc->base.sk));
877 }
878
Michio Honda8a07eb02011-04-26 20:19:36 +0900879 if (stored)
880 /* We don't need to transmit ASCONF */
881 continue;
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700882 retval = sctp_send_asconf(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884out:
885 return retval;
886}
887
Michio Honda9f7d6532011-04-26 19:32:51 +0900888/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
889int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
890{
891 struct sock *sk = sctp_opt2sk(sp);
892 union sctp_addr *addr;
893 struct sctp_af *af;
894
895 /* It is safe to write port space in caller. */
896 addr = &addrw->a;
897 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
898 af = sctp_get_af_specific(addr->sa.sa_family);
899 if (!af)
900 return -EINVAL;
901 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
902 return -EINVAL;
903
904 if (addrw->state == SCTP_ADDR_NEW)
905 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
906 else
907 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
908}
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
911 *
912 * API 8.1
913 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
914 * int flags);
915 *
916 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
917 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
918 * or IPv6 addresses.
919 *
920 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
921 * Section 3.1.2 for this usage.
922 *
923 * addrs is a pointer to an array of one or more socket addresses. Each
924 * address is contained in its appropriate structure (i.e. struct
925 * sockaddr_in or struct sockaddr_in6) the family of the address type
Ville Nuorvala23c435f2006-10-16 22:08:28 -0700926 * must be used to distinguish the address length (note that this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 * representation is termed a "packed array" of addresses). The caller
928 * specifies the number of addresses in the array with addrcnt.
929 *
930 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
931 * -1, and sets errno to the appropriate error code.
932 *
933 * For SCTP, the port given in each socket address must be the same, or
934 * sctp_bindx() will fail, setting errno to EINVAL.
935 *
936 * The flags parameter is formed from the bitwise OR of zero or more of
937 * the following currently defined flags:
938 *
939 * SCTP_BINDX_ADD_ADDR
940 *
941 * SCTP_BINDX_REM_ADDR
942 *
943 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
944 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
945 * addresses from the association. The two flags are mutually exclusive;
946 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
947 * not remove all addresses from an association; sctp_bindx() will
948 * reject such an attempt with EINVAL.
949 *
950 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
951 * additional addresses with an endpoint after calling bind(). Or use
952 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
953 * socket is associated with so that no new association accepted will be
954 * associated with those addresses. If the endpoint supports dynamic
955 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
956 * endpoint to send the appropriate message to the peer to change the
957 * peers address lists.
958 *
959 * Adding and removing addresses from a connected association is
960 * optional functionality. Implementations that do not support this
961 * functionality should return EOPNOTSUPP.
962 *
963 * Basically do nothing but copying the addresses from user to kernel
964 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
Frank Filz3f7a87d2005-06-20 13:14:57 -0700965 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
966 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 *
968 * We don't use copy_from_user() for optimization: we first do the
969 * sanity checks (buffer size -fast- and access check-healthy
970 * pointer); if all of those succeed, then we can alloc the memory
971 * (expensive operation) needed to copy the data to kernel. Then we do
972 * the copying without checking the user space area
973 * (__copy_from_user()).
974 *
975 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
976 * it.
977 *
978 * sk The sk of the socket
979 * addrs The pointer to the addresses in user land
980 * addrssize Size of the addrs buffer
981 * op Operation to perform (add or remove, see the flags of
982 * sctp_bindx)
983 *
984 * Returns 0 if ok, <0 errno code on error.
985 */
wangweidong26ac8e52013-12-23 12:16:51 +0800986static int sctp_setsockopt_bindx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +0200987 struct sockaddr __user *addrs,
988 int addrs_size, int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 struct sockaddr *kaddrs;
991 int err;
992 int addrcnt = 0;
993 int walk_size = 0;
994 struct sockaddr *sa_addr;
995 void *addr_buf;
996 struct sctp_af *af;
997
Daniel Borkmannbb333812013-06-28 19:49:40 +0200998 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
999 __func__, sk, addrs, addrs_size, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 if (unlikely(addrs_size <= 0))
1002 return -EINVAL;
1003
1004 /* Check the user passed a healthy pointer. */
1005 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1006 return -EFAULT;
1007
1008 /* Alloc space for the address array in kernel memory. */
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02001009 kaddrs = kmalloc(addrs_size, GFP_USER | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (unlikely(!kaddrs))
1011 return -ENOMEM;
1012
1013 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1014 kfree(kaddrs);
1015 return -EFAULT;
1016 }
1017
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001018 /* Walk through the addrs buffer and count the number of addresses. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 addr_buf = kaddrs;
1020 while (walk_size < addrs_size) {
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001021 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1022 kfree(kaddrs);
1023 return -EINVAL;
1024 }
1025
Joe Perchesea110732011-06-13 16:21:26 +00001026 sa_addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 af = sctp_get_af_specific(sa_addr->sa_family);
1028
1029 /* If the address family is not supported or if this address
1030 * causes the address buffer to overflow return EINVAL.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001031 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1033 kfree(kaddrs);
1034 return -EINVAL;
1035 }
1036 addrcnt++;
1037 addr_buf += af->sockaddr_len;
1038 walk_size += af->sockaddr_len;
1039 }
1040
1041 /* Do the work. */
1042 switch (op) {
1043 case SCTP_BINDX_ADD_ADDR:
1044 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1045 if (err)
1046 goto out;
1047 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1048 break;
1049
1050 case SCTP_BINDX_REM_ADDR:
1051 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1052 if (err)
1053 goto out;
1054 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1055 break;
1056
1057 default:
1058 err = -EINVAL;
1059 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062out:
1063 kfree(kaddrs);
1064
1065 return err;
1066}
1067
Frank Filz3f7a87d2005-06-20 13:14:57 -07001068/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1069 *
1070 * Common routine for handling connect() and sctp_connectx().
1071 * Connect will come in with just a single address.
1072 */
wangweidong26ac8e52013-12-23 12:16:51 +08001073static int __sctp_connect(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001074 struct sockaddr *kaddrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001075 int addrs_size,
1076 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001077{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001078 struct net *net = sock_net(sk);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001079 struct sctp_sock *sp;
1080 struct sctp_endpoint *ep;
1081 struct sctp_association *asoc = NULL;
1082 struct sctp_association *asoc2;
1083 struct sctp_transport *transport;
1084 union sctp_addr to;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001085 sctp_scope_t scope;
1086 long timeo;
1087 int err = 0;
1088 int addrcnt = 0;
1089 int walk_size = 0;
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001090 union sctp_addr *sa_addr = NULL;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001091 void *addr_buf;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001092 unsigned short port;
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001093 unsigned int f_flags = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001094
1095 sp = sctp_sk(sk);
1096 ep = sp->ep;
1097
1098 /* connect() cannot be done on a socket that is already in ESTABLISHED
1099 * state - UDP-style peeled off socket or a TCP-style socket that
1100 * is already connected.
1101 * It cannot be done even on a TCP-style listening socket.
1102 */
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03001103 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
Frank Filz3f7a87d2005-06-20 13:14:57 -07001104 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1105 err = -EISCONN;
1106 goto out_free;
1107 }
1108
1109 /* Walk through the addrs buffer and count the number of addresses. */
1110 addr_buf = kaddrs;
1111 while (walk_size < addrs_size) {
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001112 struct sctp_af *af;
1113
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001114 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1115 err = -EINVAL;
1116 goto out_free;
1117 }
1118
Joe Perchesea110732011-06-13 16:21:26 +00001119 sa_addr = addr_buf;
Al Viro4bdf4b52006-11-20 17:10:20 -08001120 af = sctp_get_af_specific(sa_addr->sa.sa_family);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001121
1122 /* If the address family is not supported or if this address
1123 * causes the address buffer to overflow return EINVAL.
1124 */
1125 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1126 err = -EINVAL;
1127 goto out_free;
1128 }
1129
Dan Rosenbergd7e0d192010-10-01 11:16:58 +00001130 port = ntohs(sa_addr->v4.sin_port);
1131
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001132 /* Save current address so we can work with it */
1133 memcpy(&to, sa_addr, af->sockaddr_len);
1134
1135 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001136 if (err)
1137 goto out_free;
1138
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001139 /* Make sure the destination port is correctly set
1140 * in all addresses.
1141 */
Wei Yongjun524fba62013-04-03 03:02:28 +00001142 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1143 err = -EINVAL;
Vlad Yasevich16d00fb2007-05-04 13:34:09 -07001144 goto out_free;
Wei Yongjun524fba62013-04-03 03:02:28 +00001145 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001146
1147 /* Check if there already is a matching association on the
1148 * endpoint (other than the one created here).
1149 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001150 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001151 if (asoc2 && asoc2 != asoc) {
1152 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1153 err = -EISCONN;
1154 else
1155 err = -EALREADY;
1156 goto out_free;
1157 }
1158
1159 /* If we could not find a matching association on the endpoint,
1160 * make sure that there is no peeled-off association matching
1161 * the peer address even on another socket.
1162 */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001163 if (sctp_endpoint_is_peeled_off(ep, &to)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07001164 err = -EADDRNOTAVAIL;
1165 goto out_free;
1166 }
1167
1168 if (!asoc) {
1169 /* If a bind() or sctp_bindx() is not called prior to
1170 * an sctp_connectx() call, the system picks an
1171 * ephemeral port and will choose an address set
1172 * equivalent to binding with a wildcard address.
1173 */
1174 if (!ep->base.bind_addr.port) {
1175 if (sctp_autobind(sk)) {
1176 err = -EAGAIN;
1177 goto out_free;
1178 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001179 } else {
1180 /*
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001181 * If an unprivileged user inherits a 1-many
1182 * style socket with open associations on a
1183 * privileged port, it MAY be permitted to
1184 * accept new associations, but it SHOULD NOT
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001185 * be permitted to open new associations.
1186 */
1187 if (ep->base.bind_addr.port < PROT_SOCK &&
Eric W. Biederman35946982012-11-16 03:03:12 +00001188 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) {
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001189 err = -EACCES;
1190 goto out_free;
1191 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001192 }
1193
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001194 scope = sctp_scope(&to);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001195 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1196 if (!asoc) {
1197 err = -ENOMEM;
1198 goto out_free;
1199 }
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001200
1201 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1202 GFP_KERNEL);
1203 if (err < 0) {
1204 goto out_free;
1205 }
1206
Frank Filz3f7a87d2005-06-20 13:14:57 -07001207 }
1208
1209 /* Prime the peer's transport structures. */
Vlad Yaseviche4d1fea2007-08-01 10:56:43 -04001210 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001211 SCTP_UNKNOWN);
1212 if (!transport) {
1213 err = -ENOMEM;
1214 goto out_free;
1215 }
1216
1217 addrcnt++;
1218 addr_buf += af->sockaddr_len;
1219 walk_size += af->sockaddr_len;
1220 }
1221
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001222 /* In case the user of sctp_connectx() wants an association
1223 * id back, assign one now.
1224 */
1225 if (assoc_id) {
1226 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1227 if (err < 0)
1228 goto out_free;
1229 }
1230
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001231 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001232 if (err < 0) {
1233 goto out_free;
1234 }
1235
1236 /* Initialize sk's dport and daddr for getpeername() */
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001237 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06001238 sp->pf->to_sk_daddr(sa_addr, sk);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001239 sk->sk_err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001240
Vlad Yasevichf50f95c2007-07-03 12:47:40 -04001241 /* in-kernel sockets don't generally have a file allocated to them
1242 * if all they do is call sock_create_kern().
1243 */
1244 if (sk->sk_socket->file)
1245 f_flags = sk->sk_socket->file->f_flags;
1246
1247 timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
1248
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001249 if (assoc_id)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001250 *assoc_id = asoc->assoc_id;
Marcelo Ricardo Leitner7233bc82016-11-03 17:03:41 -02001251 err = sctp_wait_for_connect(asoc, &timeo);
1252 /* Note: the asoc may be freed after the return of
1253 * sctp_wait_for_connect.
1254 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001255
1256 /* Don't free association on exit. */
1257 asoc = NULL;
1258
1259out_free:
Daniel Borkmannbb333812013-06-28 19:49:40 +02001260 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1261 __func__, asoc, kaddrs, err);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001262
Neil Horman2eebc1e2012-07-16 09:13:51 +00001263 if (asoc) {
1264 /* sctp_primitive_ASSOCIATE may have added this association
1265 * To the hash table, try to unhash it, just in case, its a noop
1266 * if it wasn't hashed so we're safe
1267 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001268 sctp_association_free(asoc);
Neil Horman2eebc1e2012-07-16 09:13:51 +00001269 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07001270 return err;
1271}
1272
1273/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1274 *
1275 * API 8.9
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001276 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1277 * sctp_assoc_t *asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001278 *
1279 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1280 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1281 * or IPv6 addresses.
1282 *
1283 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1284 * Section 3.1.2 for this usage.
1285 *
1286 * addrs is a pointer to an array of one or more socket addresses. Each
1287 * address is contained in its appropriate structure (i.e. struct
1288 * sockaddr_in or struct sockaddr_in6) the family of the address type
1289 * must be used to distengish the address length (note that this
1290 * representation is termed a "packed array" of addresses). The caller
1291 * specifies the number of addresses in the array with addrcnt.
1292 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001293 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1294 * the association id of the new association. On failure, sctp_connectx()
1295 * returns -1, and sets errno to the appropriate error code. The assoc_id
1296 * is not touched by the kernel.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001297 *
1298 * For SCTP, the port given in each socket address must be the same, or
1299 * sctp_connectx() will fail, setting errno to EINVAL.
1300 *
1301 * An application can use sctp_connectx to initiate an association with
1302 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1303 * allows a caller to specify multiple addresses at which a peer can be
1304 * reached. The way the SCTP stack uses the list of addresses to set up
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001305 * the association is implementation dependent. This function only
Frank Filz3f7a87d2005-06-20 13:14:57 -07001306 * specifies that the stack will try to make use of all the addresses in
1307 * the list when needed.
1308 *
1309 * Note that the list of addresses passed in is only used for setting up
1310 * the association. It does not necessarily equal the set of addresses
1311 * the peer uses for the resulting association. If the caller wants to
1312 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1313 * retrieve them after the association has been set up.
1314 *
1315 * Basically do nothing but copying the addresses from user to kernel
1316 * land and invoking either sctp_connectx(). This is used for tunneling
1317 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1318 *
1319 * We don't use copy_from_user() for optimization: we first do the
1320 * sanity checks (buffer size -fast- and access check-healthy
1321 * pointer); if all of those succeed, then we can alloc the memory
1322 * (expensive operation) needed to copy the data to kernel. Then we do
1323 * the copying without checking the user space area
1324 * (__copy_from_user()).
1325 *
1326 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1327 * it.
1328 *
1329 * sk The sk of the socket
1330 * addrs The pointer to the addresses in user land
1331 * addrssize Size of the addrs buffer
1332 *
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001333 * Returns >=0 if ok, <0 errno code on error.
Frank Filz3f7a87d2005-06-20 13:14:57 -07001334 */
wangweidong26ac8e52013-12-23 12:16:51 +08001335static int __sctp_setsockopt_connectx(struct sock *sk,
Frank Filz3f7a87d2005-06-20 13:14:57 -07001336 struct sockaddr __user *addrs,
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001337 int addrs_size,
1338 sctp_assoc_t *assoc_id)
Frank Filz3f7a87d2005-06-20 13:14:57 -07001339{
Frank Filz3f7a87d2005-06-20 13:14:57 -07001340 struct sockaddr *kaddrs;
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02001341 gfp_t gfp = GFP_KERNEL;
1342 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07001343
Daniel Borkmannbb333812013-06-28 19:49:40 +02001344 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1345 __func__, sk, addrs, addrs_size);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001346
1347 if (unlikely(addrs_size <= 0))
1348 return -EINVAL;
1349
1350 /* Check the user passed a healthy pointer. */
1351 if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
1352 return -EFAULT;
1353
1354 /* Alloc space for the address array in kernel memory. */
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02001355 if (sk->sk_socket->file)
1356 gfp = GFP_USER | __GFP_NOWARN;
1357 kaddrs = kmalloc(addrs_size, gfp);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001358 if (unlikely(!kaddrs))
1359 return -ENOMEM;
1360
1361 if (__copy_from_user(kaddrs, addrs, addrs_size)) {
1362 err = -EFAULT;
1363 } else {
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001364 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001365 }
1366
1367 kfree(kaddrs);
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001368
Frank Filz3f7a87d2005-06-20 13:14:57 -07001369 return err;
1370}
1371
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001372/*
1373 * This is an older interface. It's kept for backward compatibility
1374 * to the option that doesn't provide association id.
1375 */
wangweidong26ac8e52013-12-23 12:16:51 +08001376static int sctp_setsockopt_connectx_old(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001377 struct sockaddr __user *addrs,
1378 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001379{
1380 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1381}
1382
1383/*
1384 * New interface for the API. The since the API is done with a socket
1385 * option, to make it simple we feed back the association id is as a return
1386 * indication to the call. Error is always negative and association id is
1387 * always positive.
1388 */
wangweidong26ac8e52013-12-23 12:16:51 +08001389static int sctp_setsockopt_connectx(struct sock *sk,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001390 struct sockaddr __user *addrs,
1391 int addrs_size)
Vlad Yasevich88a0a942008-05-09 15:14:11 -07001392{
1393 sctp_assoc_t assoc_id = 0;
1394 int err = 0;
1395
1396 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1397
1398 if (err)
1399 return err;
1400 else
1401 return assoc_id;
1402}
1403
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001404/*
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001405 * New (hopefully final) interface for the API.
1406 * We use the sctp_getaddrs_old structure so that use-space library
Daniel Borkmannffd59392014-02-17 12:11:11 +01001407 * can avoid any unnecessary allocations. The only different part
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001408 * is that we store the actual length of the address buffer into the
Daniel Borkmannffd59392014-02-17 12:11:11 +01001409 * addrs_num structure member. That way we can re-use the existing
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001410 * code.
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001411 */
Daniel Borkmannffd59392014-02-17 12:11:11 +01001412#ifdef CONFIG_COMPAT
1413struct compat_sctp_getaddrs_old {
1414 sctp_assoc_t assoc_id;
1415 s32 addr_num;
1416 compat_uptr_t addrs; /* struct sockaddr * */
1417};
1418#endif
1419
wangweidong26ac8e52013-12-23 12:16:51 +08001420static int sctp_getsockopt_connectx3(struct sock *sk, int len,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001421 char __user *optval,
1422 int __user *optlen)
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001423{
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001424 struct sctp_getaddrs_old param;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001425 sctp_assoc_t assoc_id = 0;
1426 int err = 0;
1427
Daniel Borkmannffd59392014-02-17 12:11:11 +01001428#ifdef CONFIG_COMPAT
Andy Lutomirski96c0e0a2016-03-22 14:25:07 -07001429 if (in_compat_syscall()) {
Daniel Borkmannffd59392014-02-17 12:11:11 +01001430 struct compat_sctp_getaddrs_old param32;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001431
Daniel Borkmannffd59392014-02-17 12:11:11 +01001432 if (len < sizeof(param32))
1433 return -EINVAL;
1434 if (copy_from_user(&param32, optval, sizeof(param32)))
1435 return -EFAULT;
Vlad Yasevichf9c67812009-11-11 08:19:24 +00001436
Daniel Borkmannffd59392014-02-17 12:11:11 +01001437 param.assoc_id = param32.assoc_id;
1438 param.addr_num = param32.addr_num;
1439 param.addrs = compat_ptr(param32.addrs);
1440 } else
1441#endif
1442 {
1443 if (len < sizeof(param))
1444 return -EINVAL;
1445 if (copy_from_user(&param, optval, sizeof(param)))
1446 return -EFAULT;
1447 }
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001448
Daniel Borkmannffd59392014-02-17 12:11:11 +01001449 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1450 param.addrs, param.addr_num,
1451 &assoc_id);
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04001452 if (err == 0 || err == -EINPROGRESS) {
1453 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1454 return -EFAULT;
1455 if (put_user(sizeof(assoc_id), optlen))
1456 return -EFAULT;
1457 }
1458
1459 return err;
1460}
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462/* API 3.1.4 close() - UDP Style Syntax
1463 * Applications use close() to perform graceful shutdown (as described in
1464 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1465 * by a UDP-style socket.
1466 *
1467 * The syntax is
1468 *
1469 * ret = close(int sd);
1470 *
1471 * sd - the socket descriptor of the associations to be closed.
1472 *
1473 * To gracefully shutdown a specific association represented by the
1474 * UDP-style socket, an application should use the sendmsg() call,
1475 * passing no user data, but including the appropriate flag in the
1476 * ancillary data (see Section xxxx).
1477 *
1478 * If sd in the close() call is a branched-off socket representing only
1479 * one association, the shutdown is performed on that association only.
1480 *
1481 * 4.1.6 close() - TCP Style Syntax
1482 *
1483 * Applications use close() to gracefully close down an association.
1484 *
1485 * The syntax is:
1486 *
1487 * int close(int sd);
1488 *
1489 * sd - the socket descriptor of the association to be closed.
1490 *
1491 * After an application calls close() on a socket descriptor, no further
1492 * socket operations will succeed on that descriptor.
1493 *
1494 * API 7.1.4 SO_LINGER
1495 *
1496 * An application using the TCP-style socket can use this option to
1497 * perform the SCTP ABORT primitive. The linger option structure is:
1498 *
1499 * struct linger {
1500 * int l_onoff; // option on/off
1501 * int l_linger; // linger time
1502 * };
1503 *
1504 * To enable the option, set l_onoff to 1. If the l_linger value is set
1505 * to 0, calling close() is the same as the ABORT primitive. If the
1506 * value is set to a negative value, the setsockopt() call will return
1507 * an error. If the value is set to a positive value linger_time, the
1508 * close() can be blocked for at most linger_time ms. If the graceful
1509 * shutdown phase does not finish during this period, close() will
1510 * return but the graceful shutdown phase continues in the system.
1511 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02001512static void sctp_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001514 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 struct sctp_endpoint *ep;
1516 struct sctp_association *asoc;
1517 struct list_head *pos, *temp;
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001518 unsigned int data_was_unread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Daniel Borkmannbb333812013-06-28 19:49:40 +02001520 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
wangweidong048ed4b2014-01-21 15:44:11 +08001522 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 sk->sk_shutdown = SHUTDOWN_MASK;
Vlad Yasevichbec96402009-07-30 18:08:28 -04001524 sk->sk_state = SCTP_SS_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 ep = sctp_sk(sk)->ep;
1527
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001528 /* Clean up any skbs sitting on the receive queue. */
1529 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1530 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1531
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07001532 /* Walk all associations on an endpoint. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 list_for_each_safe(pos, temp, &ep->asocs) {
1534 asoc = list_entry(pos, struct sctp_association, asocs);
1535
1536 if (sctp_style(sk, TCP)) {
1537 /* A closed association can still be in the list if
1538 * it belongs to a TCP-style listening socket that is
1539 * not yet accepted. If so, free it. If not, send an
1540 * ABORT or SHUTDOWN based on the linger options.
1541 */
1542 if (sctp_state(asoc, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 sctp_association_free(asoc);
Vladislav Yasevichb89498a2006-05-19 14:32:06 -07001544 continue;
1545 }
1546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Thomas Grafcd4fcc72011-07-08 04:37:46 +00001548 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1549 !skb_queue_empty(&asoc->ulpq.reasm) ||
1550 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001551 struct sctp_chunk *chunk;
1552
1553 chunk = sctp_make_abort_user(asoc, NULL, 0);
Xin Long068d8bd2015-12-29 17:49:25 +08001554 sctp_primitive_ABORT(net, asoc, chunk);
Sridhar Samudralab9ac8672006-08-28 13:53:01 -07001555 } else
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001556 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /* On a TCP-style socket, block for at most linger_time if set. */
1560 if (sctp_style(sk, TCP) && timeout)
1561 sctp_wait_for_close(sk, timeout);
1562
1563 /* This will run the backlog queue. */
wangweidong048ed4b2014-01-21 15:44:11 +08001564 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 /* Supposedly, no process has access to the socket, but
1567 * the net layers still may.
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001568 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1569 * held and that should be grabbed before socket lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001571 spin_lock_bh(&net->sctp.addr_wq_lock);
wangweidong5bc1d1b2014-01-21 15:44:12 +08001572 bh_lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 /* Hold the sock, since sk_common_release() will put sock_put()
1575 * and we have just a little more cleanup.
1576 */
1577 sock_hold(sk);
1578 sk_common_release(sk);
1579
wangweidong5bc1d1b2014-01-21 15:44:12 +08001580 bh_unlock_sock(sk);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03001581 spin_unlock_bh(&net->sctp.addr_wq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 sock_put(sk);
1584
1585 SCTP_DBG_OBJCNT_DEC(sock);
1586}
1587
1588/* Handle EPIPE error. */
1589static int sctp_error(struct sock *sk, int flags, int err)
1590{
1591 if (err == -EPIPE)
1592 err = sock_error(sk) ? : -EPIPE;
1593 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1594 send_sig(SIGPIPE, current, 0);
1595 return err;
1596}
1597
1598/* API 3.1.3 sendmsg() - UDP Style Syntax
1599 *
1600 * An application uses sendmsg() and recvmsg() calls to transmit data to
1601 * and receive data from its peer.
1602 *
1603 * ssize_t sendmsg(int socket, const struct msghdr *message,
1604 * int flags);
1605 *
1606 * socket - the socket descriptor of the endpoint.
1607 * message - pointer to the msghdr structure which contains a single
1608 * user message and possibly some ancillary data.
1609 *
1610 * See Section 5 for complete description of the data
1611 * structures.
1612 *
1613 * flags - flags sent or received with the user message, see Section
1614 * 5 for complete description of the flags.
1615 *
1616 * Note: This function could use a rewrite especially when explicit
1617 * connect support comes in.
1618 */
1619/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1620
Daniel Borkmanndda91922013-06-17 11:40:05 +02001621static int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Ying Xue1b784142015-03-02 15:37:48 +08001623static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001625 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 struct sctp_sock *sp;
1627 struct sctp_endpoint *ep;
wangweidongcb3f8372013-12-23 12:16:50 +08001628 struct sctp_association *new_asoc = NULL, *asoc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 struct sctp_transport *transport, *chunk_tp;
1630 struct sctp_chunk *chunk;
Al Virodce116a2006-11-20 17:25:15 -08001631 union sctp_addr to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 struct sockaddr *msg_name = NULL;
Joe Perches517aa0b2011-05-12 11:27:20 +00001633 struct sctp_sndrcvinfo default_sinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 struct sctp_sndrcvinfo *sinfo;
1635 struct sctp_initmsg *sinit;
1636 sctp_assoc_t associd = 0;
1637 sctp_cmsgs_t cmsgs = { NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 sctp_scope_t scope;
Daniel Borkmann2061dcd2015-01-15 16:34:35 +01001639 bool fill_sinfo_ttl = false, wait_connect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 struct sctp_datamsg *datamsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 int msg_flags = msg->msg_flags;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001642 __u16 sinfo_flags = 0;
1643 long timeo;
1644 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 err = 0;
1647 sp = sctp_sk(sk);
1648 ep = sp->ep;
1649
Daniel Borkmannbb333812013-06-28 19:49:40 +02001650 pr_debug("%s: sk:%p, msg:%p, msg_len:%zu ep:%p\n", __func__, sk,
1651 msg, msg_len, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 /* We cannot send a message over a TCP-style listening socket. */
1654 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
1655 err = -EPIPE;
1656 goto out_nounlock;
1657 }
1658
1659 /* Parse out the SCTP CMSGs. */
1660 err = sctp_msghdr_parse(msg, &cmsgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (err) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001662 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 goto out_nounlock;
1664 }
1665
1666 /* Fetch the destination address for this packet. This
1667 * address only selects the association--it is not necessarily
1668 * the address we will send to.
1669 * For a peeled-off socket, msg_name is ignored.
1670 */
1671 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1672 int msg_namelen = msg->msg_namelen;
1673
1674 err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
1675 msg_namelen);
1676 if (err)
1677 return err;
1678
1679 if (msg_namelen > sizeof(to))
1680 msg_namelen = sizeof(to);
1681 memcpy(&to, msg->msg_name, msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 msg_name = msg->msg_name;
1683 }
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 sinit = cmsgs.init;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001686 if (cmsgs.sinfo != NULL) {
1687 memset(&default_sinfo, 0, sizeof(default_sinfo));
1688 default_sinfo.sinfo_stream = cmsgs.sinfo->snd_sid;
1689 default_sinfo.sinfo_flags = cmsgs.sinfo->snd_flags;
1690 default_sinfo.sinfo_ppid = cmsgs.sinfo->snd_ppid;
1691 default_sinfo.sinfo_context = cmsgs.sinfo->snd_context;
1692 default_sinfo.sinfo_assoc_id = cmsgs.sinfo->snd_assoc_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001694 sinfo = &default_sinfo;
1695 fill_sinfo_ttl = true;
1696 } else {
1697 sinfo = cmsgs.srinfo;
1698 }
1699 /* Did the user specify SNDINFO/SNDRCVINFO? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (sinfo) {
1701 sinfo_flags = sinfo->sinfo_flags;
1702 associd = sinfo->sinfo_assoc_id;
1703 }
1704
Daniel Borkmannbb333812013-06-28 19:49:40 +02001705 pr_debug("%s: msg_len:%zu, sinfo_flags:0x%x\n", __func__,
1706 msg_len, sinfo_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001708 /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
1709 if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 err = -EINVAL;
1711 goto out_nounlock;
1712 }
1713
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001714 /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
1715 * length messages when SCTP_EOF|SCTP_ABORT is not set.
1716 * If SCTP_ABORT is set, the message length could be non zero with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 * the msg_iov set to the user abort reason.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001718 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001719 if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
1720 (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 err = -EINVAL;
1722 goto out_nounlock;
1723 }
1724
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001725 /* If SCTP_ADDR_OVER is set, there must be an address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 * specified in msg_name.
1727 */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001728 if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 err = -EINVAL;
1730 goto out_nounlock;
1731 }
1732
1733 transport = NULL;
1734
Daniel Borkmannbb333812013-06-28 19:49:40 +02001735 pr_debug("%s: about to look up association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
wangweidong048ed4b2014-01-21 15:44:11 +08001737 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 /* If a msg_name has been specified, assume this is to be used. */
1740 if (msg_name) {
1741 /* Look for a matching association on the endpoint. */
Al Virodce116a2006-11-20 17:25:15 -08001742 asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03001743
1744 /* If we could not find a matching association on the
1745 * endpoint, make sure that it is not a TCP-style
1746 * socket that already has an association or there is
1747 * no peeled-off association on another socket.
1748 */
1749 if (!asoc &&
1750 ((sctp_style(sk, TCP) &&
1751 (sctp_sstate(sk, ESTABLISHED) ||
1752 sctp_sstate(sk, CLOSING))) ||
1753 sctp_endpoint_is_peeled_off(ep, &to))) {
1754 err = -EADDRNOTAVAIL;
1755 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757 } else {
1758 asoc = sctp_id2assoc(sk, associd);
1759 if (!asoc) {
1760 err = -EPIPE;
1761 goto out_unlock;
1762 }
1763 }
1764
1765 if (asoc) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001766 pr_debug("%s: just looked up association:%p\n", __func__, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
1769 * socket that has an association in CLOSED state. This can
1770 * happen when an accepted socket has an association that is
1771 * already CLOSED.
1772 */
1773 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
1774 err = -EPIPE;
1775 goto out_unlock;
1776 }
1777
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001778 if (sinfo_flags & SCTP_EOF) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001779 pr_debug("%s: shutting down association:%p\n",
1780 __func__, asoc);
1781
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001782 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 err = 0;
1784 goto out_unlock;
1785 }
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001786 if (sinfo_flags & SCTP_ABORT) {
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001787
1788 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1789 if (!chunk) {
1790 err = -ENOMEM;
1791 goto out_unlock;
1792 }
1793
Daniel Borkmannbb333812013-06-28 19:49:40 +02001794 pr_debug("%s: aborting association:%p\n",
1795 __func__, asoc);
1796
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001797 sctp_primitive_ABORT(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 err = 0;
1799 goto out_unlock;
1800 }
1801 }
1802
1803 /* Do we need to create the association? */
1804 if (!asoc) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001805 pr_debug("%s: there is no association yet\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001807 if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 err = -EINVAL;
1809 goto out_unlock;
1810 }
1811
1812 /* Check for invalid stream against the stream counts,
1813 * either the default or the user specified stream counts.
1814 */
1815 if (sinfo) {
Dan Carpenter0e864b22014-01-13 16:46:08 +03001816 if (!sinit || !sinit->sinit_num_ostreams) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 /* Check against the defaults. */
1818 if (sinfo->sinfo_stream >=
1819 sp->initmsg.sinit_num_ostreams) {
1820 err = -EINVAL;
1821 goto out_unlock;
1822 }
1823 } else {
1824 /* Check against the requested. */
1825 if (sinfo->sinfo_stream >=
1826 sinit->sinit_num_ostreams) {
1827 err = -EINVAL;
1828 goto out_unlock;
1829 }
1830 }
1831 }
1832
1833 /*
1834 * API 3.1.2 bind() - UDP Style Syntax
1835 * If a bind() or sctp_bindx() is not called prior to a
1836 * sendmsg() call that initiates a new association, the
1837 * system picks an ephemeral port and will choose an address
1838 * set equivalent to binding with a wildcard address.
1839 */
1840 if (!ep->base.bind_addr.port) {
1841 if (sctp_autobind(sk)) {
1842 err = -EAGAIN;
1843 goto out_unlock;
1844 }
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001845 } else {
1846 /*
1847 * If an unprivileged user inherits a one-to-many
1848 * style socket with open associations on a privileged
1849 * port, it MAY be permitted to accept new associations,
1850 * but it SHOULD NOT be permitted to open new
1851 * associations.
1852 */
1853 if (ep->base.bind_addr.port < PROT_SOCK &&
Eric W. Biederman35946982012-11-16 03:03:12 +00001854 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) {
Ivan Skytte Jorgensen64a0c1c2005-10-28 15:39:02 -07001855 err = -EACCES;
1856 goto out_unlock;
1857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859
1860 scope = sctp_scope(&to);
1861 new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1862 if (!new_asoc) {
1863 err = -ENOMEM;
1864 goto out_unlock;
1865 }
1866 asoc = new_asoc;
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001867 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1868 if (err < 0) {
1869 err = -ENOMEM;
1870 goto out_free;
1871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 /* If the SCTP_INIT ancillary data is specified, set all
1874 * the association init values accordingly.
1875 */
1876 if (sinit) {
1877 if (sinit->sinit_num_ostreams) {
1878 asoc->c.sinit_num_ostreams =
1879 sinit->sinit_num_ostreams;
1880 }
1881 if (sinit->sinit_max_instreams) {
1882 asoc->c.sinit_max_instreams =
1883 sinit->sinit_max_instreams;
1884 }
1885 if (sinit->sinit_max_attempts) {
1886 asoc->max_init_attempts
1887 = sinit->sinit_max_attempts;
1888 }
1889 if (sinit->sinit_max_init_timeo) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001890 asoc->max_init_timeo =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 msecs_to_jiffies(sinit->sinit_max_init_timeo);
1892 }
1893 }
1894
1895 /* Prime the peer's transport structures. */
Al Virodce116a2006-11-20 17:25:15 -08001896 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (!transport) {
1898 err = -ENOMEM;
1899 goto out_free;
1900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
1902
1903 /* ASSERT: we have a valid association at this point. */
Daniel Borkmannbb333812013-06-28 19:49:40 +02001904 pr_debug("%s: we have a valid association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 if (!sinfo) {
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001907 /* If the user didn't specify SNDINFO/SNDRCVINFO, make up
1908 * one with some defaults.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 */
Joe Perches517aa0b2011-05-12 11:27:20 +00001910 memset(&default_sinfo, 0, sizeof(default_sinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 default_sinfo.sinfo_stream = asoc->default_stream;
1912 default_sinfo.sinfo_flags = asoc->default_flags;
1913 default_sinfo.sinfo_ppid = asoc->default_ppid;
1914 default_sinfo.sinfo_context = asoc->default_context;
1915 default_sinfo.sinfo_timetolive = asoc->default_timetolive;
1916 default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 sinfo = &default_sinfo;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02001919 } else if (fill_sinfo_ttl) {
1920 /* In case SNDINFO was specified, we still need to fill
1921 * it with a default ttl from the assoc here.
1922 */
1923 sinfo->sinfo_timetolive = asoc->default_timetolive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925
1926 /* API 7.1.7, the sndbuf size per association bounds the
1927 * maximum size of data that can be sent in a single send call.
1928 */
1929 if (msg_len > sk->sk_sndbuf) {
1930 err = -EMSGSIZE;
1931 goto out_free;
1932 }
1933
Vlad Yasevich8a479492007-06-07 14:21:05 -04001934 if (asoc->pmtu_pending)
David S. Miller02f3d4c2012-07-16 03:57:14 -07001935 sctp_assoc_pending_pmtu(sk, asoc);
Vlad Yasevich8a479492007-06-07 14:21:05 -04001936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 /* If fragmentation is disabled and the message length exceeds the
1938 * association fragmentation point, return EMSGSIZE. The I-D
1939 * does not specify what this error is, but this looks like
1940 * a great fit.
1941 */
1942 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1943 err = -EMSGSIZE;
1944 goto out_free;
1945 }
1946
Joe Perchesafd76142011-05-12 09:19:10 +00001947 /* Check for invalid stream. */
1948 if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
1949 err = -EINVAL;
1950 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952
Xin Long8dbdf1f2016-07-09 19:47:45 +08001953 if (sctp_wspace(asoc) < msg_len)
1954 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1957 if (!sctp_wspace(asoc)) {
Xin Long9904da52017-11-15 16:57:26 +08001958 /* sk can be changed by peel off when waiting for buf. */
Xin Long2f056e72018-01-15 17:01:36 +08001959 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
Xin Long1158ecd52017-11-15 16:55:54 +08001960 if (err) {
1961 if (err == -ESRCH) {
1962 /* asoc is already dead. */
1963 new_asoc = NULL;
1964 err = -EPIPE;
1965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 goto out_free;
Xin Long1158ecd52017-11-15 16:55:54 +08001967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
1969
1970 /* If an address is passed with the sendto/sendmsg call, it is used
1971 * to override the primary destination address in the TCP model, or
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001972 * when SCTP_ADDR_OVER flag is set in the UDP model.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 */
1974 if ((sctp_style(sk, TCP) && msg_name) ||
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07001975 (sinfo_flags & SCTP_ADDR_OVER)) {
Al Virodce116a2006-11-20 17:25:15 -08001976 chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (!chunk_tp) {
1978 err = -EINVAL;
1979 goto out_free;
1980 }
1981 } else
1982 chunk_tp = NULL;
1983
1984 /* Auto-connect, if we aren't connected already. */
1985 if (sctp_state(asoc, CLOSED)) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00001986 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (err < 0)
1988 goto out_free;
Daniel Borkmannbb333812013-06-28 19:49:40 +02001989
Daniel Borkmann2061dcd2015-01-15 16:34:35 +01001990 wait_connect = true;
Daniel Borkmannbb333812013-06-28 19:49:40 +02001991 pr_debug("%s: we associated primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993
1994 /* Break the message into multiple chunks of maximum size. */
Al Viroc0371da2014-11-24 10:42:55 -05001995 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
Tommi Rantala6e51fe72012-11-22 03:23:16 +00001996 if (IS_ERR(datamsg)) {
1997 err = PTR_ERR(datamsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 goto out_free;
1999 }
2000
2001 /* Now send the (possibly) fragmented message. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002002 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
Xin Longb61c6542016-09-14 02:04:20 +08002003 sctp_chunk_hold(chunk);
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 /* Do accounting for the write space. */
2006 sctp_set_owner_w(chunk);
2007
2008 chunk->transport = chunk_tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04002011 /* Send it to the lower layers. Note: all chunks
2012 * must either fail or succeed. The lower layer
2013 * works that way today. Keep it that way or this
2014 * breaks.
2015 */
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002016 err = sctp_primitive_SEND(net, asoc, datamsg);
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04002017 /* Did the lower layer accept the chunk? */
Xin Longb61c6542016-09-14 02:04:20 +08002018 if (err) {
2019 sctp_datamsg_free(datamsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 goto out_free;
Xin Longb61c6542016-09-14 02:04:20 +08002021 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02002022
2023 pr_debug("%s: we sent primitively\n", __func__);
2024
Xin Longb61c6542016-09-14 02:04:20 +08002025 sctp_datamsg_put(datamsg);
Daniel Borkmannbb333812013-06-28 19:49:40 +02002026 err = msg_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Daniel Borkmann2061dcd2015-01-15 16:34:35 +01002028 if (unlikely(wait_connect)) {
2029 timeo = sock_sndtimeo(sk, msg_flags & MSG_DONTWAIT);
2030 sctp_wait_for_connect(asoc, &timeo);
2031 }
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 /* If we are already past ASSOCIATE, the lower
2034 * layers are responsible for association cleanup.
2035 */
2036 goto out_unlock;
2037
2038out_free:
Xin Longb5eff712015-12-30 23:50:49 +08002039 if (new_asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 sctp_association_free(asoc);
2041out_unlock:
wangweidong048ed4b2014-01-21 15:44:11 +08002042 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044out_nounlock:
2045 return sctp_error(sk, msg_flags, err);
2046
2047#if 0
2048do_sock_err:
2049 if (msg_len)
2050 err = msg_len;
2051 else
2052 err = sock_error(sk);
2053 goto out;
2054
2055do_interrupted:
2056 if (msg_len)
2057 err = msg_len;
2058 goto out;
2059#endif /* 0 */
2060}
2061
2062/* This is an extended version of skb_pull() that removes the data from the
2063 * start of a skb even when data is spread across the list of skb's in the
2064 * frag_list. len specifies the total amount of data that needs to be removed.
2065 * when 'len' bytes could be removed from the skb, it returns 0.
2066 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2067 * could not be removed.
2068 */
2069static int sctp_skb_pull(struct sk_buff *skb, int len)
2070{
2071 struct sk_buff *list;
2072 int skb_len = skb_headlen(skb);
2073 int rlen;
2074
2075 if (len <= skb_len) {
2076 __skb_pull(skb, len);
2077 return 0;
2078 }
2079 len -= skb_len;
2080 __skb_pull(skb, skb_len);
2081
David S. Miller1b003be2009-06-09 00:22:35 -07002082 skb_walk_frags(skb, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 rlen = sctp_skb_pull(list, len);
2084 skb->len -= (len-rlen);
2085 skb->data_len -= (len-rlen);
2086
2087 if (!rlen)
2088 return 0;
2089
2090 len = rlen;
2091 }
2092
2093 return len;
2094}
2095
2096/* API 3.1.3 recvmsg() - UDP Style Syntax
2097 *
2098 * ssize_t recvmsg(int socket, struct msghdr *message,
2099 * int flags);
2100 *
2101 * socket - the socket descriptor of the endpoint.
2102 * message - pointer to the msghdr structure which contains a single
2103 * user message and possibly some ancillary data.
2104 *
2105 * See Section 5 for complete description of the data
2106 * structures.
2107 *
2108 * flags - flags sent or received with the user message, see Section
2109 * 5 for complete description of the flags.
2110 */
Ying Xue1b784142015-03-02 15:37:48 +08002111static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2112 int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 struct sctp_ulpevent *event = NULL;
2115 struct sctp_sock *sp = sctp_sk(sk);
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002116 struct sk_buff *skb, *head_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 int copied;
2118 int err = 0;
2119 int skb_len;
2120
Daniel Borkmannbb333812013-06-28 19:49:40 +02002121 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2122 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2123 addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
wangweidong048ed4b2014-01-21 15:44:11 +08002125 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Marcelo Ricardo Leitnere5b13f32016-07-15 16:38:19 -03002127 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
Xin Longe0878692016-07-30 14:14:41 +08002128 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 err = -ENOTCONN;
2130 goto out;
2131 }
2132
2133 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2134 if (!skb)
2135 goto out;
2136
2137 /* Get the total length of the skb including any skb's in the
2138 * frag_list.
2139 */
2140 skb_len = skb->len;
2141
2142 copied = skb_len;
2143 if (copied > len)
2144 copied = len;
2145
David S. Miller51f3d022014-11-05 16:46:40 -05002146 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 event = sctp_skb2event(skb);
2149
2150 if (err)
2151 goto out_free;
2152
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002153 if (event->chunk && event->chunk->head_skb)
2154 head_skb = event->chunk->head_skb;
2155 else
2156 head_skb = skb;
2157 sock_recv_ts_and_drops(msg, sk, head_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (sctp_ulpevent_is_notification(event)) {
2159 msg->msg_flags |= MSG_NOTIFICATION;
2160 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2161 } else {
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -03002162 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
2164
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02002165 /* Check if we allow SCTP_NXTINFO. */
2166 if (sp->recvnxtinfo)
2167 sctp_ulpevent_read_nxtinfo(event, msg, sk);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002168 /* Check if we allow SCTP_RCVINFO. */
2169 if (sp->recvrcvinfo)
2170 sctp_ulpevent_read_rcvinfo(event, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 /* Check if we allow SCTP_SNDRCVINFO. */
2172 if (sp->subscribe.sctp_data_io_event)
2173 sctp_ulpevent_read_sndrcvinfo(event, msg);
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02002174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 err = copied;
2176
2177 /* If skb's length exceeds the user's buffer, update the skb and
2178 * push it back to the receive_queue so that the next call to
2179 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2180 */
2181 if (skb_len > copied) {
2182 msg->msg_flags &= ~MSG_EOR;
2183 if (flags & MSG_PEEK)
2184 goto out_free;
2185 sctp_skb_pull(skb, copied);
2186 skb_queue_head(&sk->sk_receive_queue, skb);
2187
Daniel Borkmann362d5202014-04-14 21:45:17 +02002188 /* When only partial message is copied to the user, increase
2189 * rwnd by that amount. If all the data in the skb is read,
2190 * rwnd is updated when the event is freed.
2191 */
2192 if (!sctp_ulpevent_is_notification(event))
2193 sctp_assoc_rwnd_increase(event->asoc, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 goto out;
2195 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2196 (event->msg_flags & MSG_EOR))
2197 msg->msg_flags |= MSG_EOR;
2198 else
2199 msg->msg_flags &= ~MSG_EOR;
2200
2201out_free:
2202 if (flags & MSG_PEEK) {
2203 /* Release the skb reference acquired after peeking the skb in
2204 * sctp_skb_recv_datagram().
2205 */
2206 kfree_skb(skb);
2207 } else {
2208 /* Free the event which includes releasing the reference to
2209 * the owner of the skb, freeing the skb and updating the
2210 * rwnd.
2211 */
2212 sctp_ulpevent_free(event);
2213 }
2214out:
wangweidong048ed4b2014-01-21 15:44:11 +08002215 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 return err;
2217}
2218
2219/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2220 *
2221 * This option is a on/off flag. If enabled no SCTP message
2222 * fragmentation will be performed. Instead if a message being sent
2223 * exceeds the current PMTU size, the message will NOT be sent and
2224 * instead a error will be indicated to the user.
2225 */
2226static int sctp_setsockopt_disable_fragments(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002227 char __user *optval,
2228 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
2230 int val;
2231
2232 if (optlen < sizeof(int))
2233 return -EINVAL;
2234
2235 if (get_user(val, (int __user *)optval))
2236 return -EFAULT;
2237
2238 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2239
2240 return 0;
2241}
2242
2243static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002244 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Wei Yongjun94912302011-07-02 09:28:04 +00002246 struct sctp_association *asoc;
2247 struct sctp_ulpevent *event;
2248
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05002249 if (optlen > sizeof(struct sctp_event_subscribe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return -EINVAL;
2251 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2252 return -EFAULT;
Wei Yongjun94912302011-07-02 09:28:04 +00002253
Daniel Borkmannbbbea412014-07-12 20:30:40 +02002254 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
Wei Yongjun94912302011-07-02 09:28:04 +00002255 * if there is no data to be sent or retransmit, the stack will
2256 * immediately send up this notification.
2257 */
2258 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2259 &sctp_sk(sk)->subscribe)) {
2260 asoc = sctp_id2assoc(sk, 0);
2261
2262 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2263 event = sctp_ulpevent_make_sender_dry_event(asoc,
2264 GFP_ATOMIC);
2265 if (!event)
2266 return -ENOMEM;
2267
2268 sctp_ulpq_tail_event(&asoc->ulpq, event);
2269 }
2270 }
2271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return 0;
2273}
2274
2275/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2276 *
2277 * This socket option is applicable to the UDP-style socket only. When
2278 * set it will cause associations that are idle for more than the
2279 * specified number of seconds to automatically close. An association
2280 * being idle is defined an association that has NOT sent or received
2281 * user data. The special value of '0' indicates that no automatic
2282 * close of any associations should be performed. The option expects an
2283 * integer defining the number of seconds of idle time before an
2284 * association is closed.
2285 */
2286static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002287 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
2289 struct sctp_sock *sp = sctp_sk(sk);
Neil Horman9f70f462013-12-10 06:48:15 -05002290 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 /* Applicable to UDP-style socket only */
2293 if (sctp_style(sk, TCP))
2294 return -EOPNOTSUPP;
2295 if (optlen != sizeof(int))
2296 return -EINVAL;
2297 if (copy_from_user(&sp->autoclose, optval, optlen))
2298 return -EFAULT;
2299
Neil Horman9f70f462013-12-10 06:48:15 -05002300 if (sp->autoclose > net->sctp.max_autoclose)
2301 sp->autoclose = net->sctp.max_autoclose;
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return 0;
2304}
2305
2306/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2307 *
2308 * Applications can enable or disable heartbeats for any peer address of
2309 * an association, modify an address's heartbeat interval, force a
2310 * heartbeat to be sent immediately, and adjust the address's maximum
2311 * number of retransmissions sent before an address is considered
2312 * unreachable. The following structure is used to access and modify an
2313 * address's parameters:
2314 *
2315 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002316 * sctp_assoc_t spp_assoc_id;
2317 * struct sockaddr_storage spp_address;
2318 * uint32_t spp_hbinterval;
2319 * uint16_t spp_pathmaxrxt;
2320 * uint32_t spp_pathmtu;
2321 * uint32_t spp_sackdelay;
2322 * uint32_t spp_flags;
2323 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002325 * spp_assoc_id - (one-to-many style socket) This is filled in the
2326 * application, and identifies the association for
2327 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 * spp_address - This specifies which address is of interest.
2329 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08002330 * in milliseconds. If a value of zero
2331 * is present in this field then no changes are to
2332 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 * spp_pathmaxrxt - This contains the maximum number of
2334 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08002335 * considered unreachable. If a value of zero
2336 * is present in this field then no changes are to
2337 * be made to this parameter.
2338 * spp_pathmtu - When Path MTU discovery is disabled the value
2339 * specified here will be the "fixed" path mtu.
2340 * Note that if the spp_address field is empty
2341 * then all associations on this address will
2342 * have this fixed path mtu set upon them.
2343 *
2344 * spp_sackdelay - When delayed sack is enabled, this value specifies
2345 * the number of milliseconds that sacks will be delayed
2346 * for. This value will apply to all addresses of an
2347 * association if the spp_address field is empty. Note
2348 * also, that if delayed sack is enabled and this
2349 * value is set to 0, no change is made to the last
2350 * recorded delayed sack timer value.
2351 *
2352 * spp_flags - These flags are used to control various features
2353 * on an association. The flag field may contain
2354 * zero or more of the following options.
2355 *
2356 * SPP_HB_ENABLE - Enable heartbeats on the
2357 * specified address. Note that if the address
2358 * field is empty all addresses for the association
2359 * have heartbeats enabled upon them.
2360 *
2361 * SPP_HB_DISABLE - Disable heartbeats on the
2362 * speicifed address. Note that if the address
2363 * field is empty all addresses for the association
2364 * will have their heartbeats disabled. Note also
2365 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2366 * mutually exclusive, only one of these two should
2367 * be specified. Enabling both fields will have
2368 * undetermined results.
2369 *
2370 * SPP_HB_DEMAND - Request a user initiated heartbeat
2371 * to be made immediately.
2372 *
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002373 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2374 * heartbeat delayis to be set to the value of 0
2375 * milliseconds.
2376 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08002377 * SPP_PMTUD_ENABLE - This field will enable PMTU
2378 * discovery upon the specified address. Note that
2379 * if the address feild is empty then all addresses
2380 * on the association are effected.
2381 *
2382 * SPP_PMTUD_DISABLE - This field will disable PMTU
2383 * discovery upon the specified address. Note that
2384 * if the address feild is empty then all addresses
2385 * on the association are effected. Not also that
2386 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2387 * exclusive. Enabling both will have undetermined
2388 * results.
2389 *
2390 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2391 * on delayed sack. The time specified in spp_sackdelay
2392 * is used to specify the sack delay for this address. Note
2393 * that if spp_address is empty then all addresses will
2394 * enable delayed sack and take on the sack delay
2395 * value specified in spp_sackdelay.
2396 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2397 * off delayed sack. If the spp_address field is blank then
2398 * delayed sack is disabled for the entire association. Note
2399 * also that this field is mutually exclusive to
2400 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2401 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 */
Adrian Bunk16164362006-09-18 00:40:38 -07002403static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2404 struct sctp_transport *trans,
2405 struct sctp_association *asoc,
2406 struct sctp_sock *sp,
2407 int hb_change,
2408 int pmtud_change,
2409 int sackdelay_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 int error;
2412
Frank Filz52ccb8e2005-12-22 11:36:46 -08002413 if (params->spp_flags & SPP_HB_DEMAND && trans) {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00002414 struct net *net = sock_net(trans->asoc->base.sk);
2415
2416 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 if (error)
2418 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
2420
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002421 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2422 * this field is ignored. Note also that a value of zero indicates
2423 * the current setting should be left unchanged.
2424 */
2425 if (params->spp_flags & SPP_HB_ENABLE) {
2426
2427 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2428 * set. This lets us use 0 value when this flag
2429 * is set.
2430 */
2431 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2432 params->spp_hbinterval = 0;
2433
2434 if (params->spp_hbinterval ||
2435 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2436 if (trans) {
2437 trans->hbinterval =
2438 msecs_to_jiffies(params->spp_hbinterval);
2439 } else if (asoc) {
2440 asoc->hbinterval =
2441 msecs_to_jiffies(params->spp_hbinterval);
2442 } else {
2443 sp->hbinterval = params->spp_hbinterval;
2444 }
Frank Filz52ccb8e2005-12-22 11:36:46 -08002445 }
2446 }
2447
2448 if (hb_change) {
2449 if (trans) {
2450 trans->param_flags =
2451 (trans->param_flags & ~SPP_HB) | hb_change;
2452 } else if (asoc) {
2453 asoc->param_flags =
2454 (asoc->param_flags & ~SPP_HB) | hb_change;
2455 } else {
2456 sp->param_flags =
2457 (sp->param_flags & ~SPP_HB) | hb_change;
2458 }
2459 }
2460
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002461 /* When Path MTU discovery is disabled the value specified here will
2462 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2463 * include the flag SPP_PMTUD_DISABLE for this field to have any
2464 * effect).
2465 */
2466 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002467 if (trans) {
2468 trans->pathmtu = params->spp_pathmtu;
David S. Miller02f3d4c2012-07-16 03:57:14 -07002469 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002470 } else if (asoc) {
2471 asoc->pathmtu = params->spp_pathmtu;
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04002472 sctp_frag_point(asoc, params->spp_pathmtu);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002473 } else {
2474 sp->pathmtu = params->spp_pathmtu;
2475 }
2476 }
2477
2478 if (pmtud_change) {
2479 if (trans) {
2480 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2481 (params->spp_flags & SPP_PMTUD_ENABLE);
2482 trans->param_flags =
2483 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2484 if (update) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +00002485 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
David S. Miller02f3d4c2012-07-16 03:57:14 -07002486 sctp_assoc_sync_pmtu(sctp_opt2sk(sp), asoc);
Frank Filz52ccb8e2005-12-22 11:36:46 -08002487 }
2488 } else if (asoc) {
2489 asoc->param_flags =
2490 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2491 } else {
2492 sp->param_flags =
2493 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2494 }
2495 }
2496
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002497 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2498 * value of this field is ignored. Note also that a value of zero
2499 * indicates the current setting should be left unchanged.
2500 */
2501 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002502 if (trans) {
2503 trans->sackdelay =
2504 msecs_to_jiffies(params->spp_sackdelay);
2505 } else if (asoc) {
2506 asoc->sackdelay =
2507 msecs_to_jiffies(params->spp_sackdelay);
2508 } else {
2509 sp->sackdelay = params->spp_sackdelay;
2510 }
2511 }
2512
2513 if (sackdelay_change) {
2514 if (trans) {
2515 trans->param_flags =
2516 (trans->param_flags & ~SPP_SACKDELAY) |
2517 sackdelay_change;
2518 } else if (asoc) {
2519 asoc->param_flags =
2520 (asoc->param_flags & ~SPP_SACKDELAY) |
2521 sackdelay_change;
2522 } else {
2523 sp->param_flags =
2524 (sp->param_flags & ~SPP_SACKDELAY) |
2525 sackdelay_change;
2526 }
2527 }
2528
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002529 /* Note that a value of zero indicates the current setting should be
2530 left unchanged.
Vlad Yasevichbdf30922007-03-23 11:33:12 -07002531 */
Andrei Pelinescu-Onciul37051f72009-11-23 15:53:57 -05002532 if (params->spp_pathmaxrxt) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002533 if (trans) {
2534 trans->pathmaxrxt = params->spp_pathmaxrxt;
2535 } else if (asoc) {
2536 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2537 } else {
2538 sp->pathmaxrxt = params->spp_pathmaxrxt;
2539 }
2540 }
2541
2542 return 0;
2543}
2544
2545static int sctp_setsockopt_peer_addr_params(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002546 char __user *optval,
2547 unsigned int optlen)
Frank Filz52ccb8e2005-12-22 11:36:46 -08002548{
2549 struct sctp_paddrparams params;
2550 struct sctp_transport *trans = NULL;
2551 struct sctp_association *asoc = NULL;
2552 struct sctp_sock *sp = sctp_sk(sk);
2553 int error;
2554 int hb_change, pmtud_change, sackdelay_change;
2555
2556 if (optlen != sizeof(struct sctp_paddrparams))
wangweidongcb3f8372013-12-23 12:16:50 +08002557 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08002558
2559 if (copy_from_user(&params, optval, optlen))
2560 return -EFAULT;
2561
2562 /* Validate flags and value parameters. */
2563 hb_change = params.spp_flags & SPP_HB;
2564 pmtud_change = params.spp_flags & SPP_PMTUD;
2565 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2566
2567 if (hb_change == SPP_HB ||
2568 pmtud_change == SPP_PMTUD ||
2569 sackdelay_change == SPP_SACKDELAY ||
2570 params.spp_sackdelay > 500 ||
Joe Perchesf64f9e72009-11-29 16:55:45 -08002571 (params.spp_pathmtu &&
2572 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
Frank Filz52ccb8e2005-12-22 11:36:46 -08002573 return -EINVAL;
2574
2575 /* If an address other than INADDR_ANY is specified, and
2576 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 */
wangweidongcb3f8372013-12-23 12:16:50 +08002578 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002579 trans = sctp_addr_id2transport(sk, &params.spp_address,
2580 params.spp_assoc_id);
2581 if (!trans)
2582 return -EINVAL;
2583 }
2584
2585 /* Get association, if assoc_id != 0 and the socket is a one
2586 * to many style socket, and an association was not found, then
2587 * the id was invalid.
2588 */
2589 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2590 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2591 return -EINVAL;
2592
2593 /* Heartbeat demand can only be sent on a transport or
2594 * association, but not a socket.
2595 */
2596 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2597 return -EINVAL;
2598
2599 /* Process parameters. */
2600 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2601 hb_change, pmtud_change,
2602 sackdelay_change);
2603
2604 if (error)
2605 return error;
2606
2607 /* If changes are for association, also apply parameters to each
2608 * transport.
2609 */
2610 if (!trans && asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002611 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2612 transports) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08002613 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2614 hb_change, pmtud_change,
2615 sackdelay_change);
2616 }
2617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
2619 return 0;
2620}
2621
wangweidong0ea5e4d2014-01-15 17:24:01 +08002622static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2623{
2624 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2625}
2626
2627static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2628{
2629 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2630}
2631
Wei Yongjund364d922008-05-09 15:13:26 -07002632/*
2633 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08002634 *
Wei Yongjund364d922008-05-09 15:13:26 -07002635 * This option will effect the way delayed acks are performed. This
2636 * option allows you to get or set the delayed ack time, in
2637 * milliseconds. It also allows changing the delayed ack frequency.
2638 * Changing the frequency to 1 disables the delayed sack algorithm. If
2639 * the assoc_id is 0, then this sets or gets the endpoints default
2640 * values. If the assoc_id field is non-zero, then the set or get
2641 * effects the specified association for the one to many model (the
2642 * assoc_id field is ignored by the one to one model). Note that if
2643 * sack_delay or sack_freq are 0 when setting this option, then the
2644 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08002645 *
Wei Yongjund364d922008-05-09 15:13:26 -07002646 * struct sctp_sack_info {
2647 * sctp_assoc_t sack_assoc_id;
2648 * uint32_t sack_delay;
2649 * uint32_t sack_freq;
2650 * };
Frank Filz77086102005-12-22 11:37:30 -08002651 *
Wei Yongjund364d922008-05-09 15:13:26 -07002652 * sack_assoc_id - This parameter, indicates which association the user
2653 * is performing an action upon. Note that if this field's value is
2654 * zero then the endpoints default value is changed (effecting future
2655 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08002656 *
Wei Yongjund364d922008-05-09 15:13:26 -07002657 * sack_delay - This parameter contains the number of milliseconds that
2658 * the user is requesting the delayed ACK timer be set to. Note that
2659 * this value is defined in the standard to be between 200 and 500
2660 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08002661 *
Wei Yongjund364d922008-05-09 15:13:26 -07002662 * sack_freq - This parameter contains the number of packets that must
2663 * be received before a sack is sent without waiting for the delay
2664 * timer to expire. The default value for this is 2, setting this
2665 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08002666 */
2667
Wei Yongjund364d922008-05-09 15:13:26 -07002668static int sctp_setsockopt_delayed_ack(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002669 char __user *optval, unsigned int optlen)
Frank Filz77086102005-12-22 11:37:30 -08002670{
Wei Yongjund364d922008-05-09 15:13:26 -07002671 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08002672 struct sctp_transport *trans = NULL;
2673 struct sctp_association *asoc = NULL;
2674 struct sctp_sock *sp = sctp_sk(sk);
2675
Wei Yongjund364d922008-05-09 15:13:26 -07002676 if (optlen == sizeof(struct sctp_sack_info)) {
2677 if (copy_from_user(&params, optval, optlen))
2678 return -EFAULT;
2679
2680 if (params.sack_delay == 0 && params.sack_freq == 0)
2681 return 0;
2682 } else if (optlen == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05002683 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05002684 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05002685 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05002686 "Use struct sctp_sack_info instead\n",
2687 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07002688 if (copy_from_user(&params, optval, optlen))
2689 return -EFAULT;
2690
2691 if (params.sack_delay == 0)
2692 params.sack_freq = 1;
2693 else
2694 params.sack_freq = 0;
2695 } else
wangweidongcb3f8372013-12-23 12:16:50 +08002696 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08002697
Frank Filz77086102005-12-22 11:37:30 -08002698 /* Validate value parameter. */
Wei Yongjund364d922008-05-09 15:13:26 -07002699 if (params.sack_delay > 500)
Frank Filz77086102005-12-22 11:37:30 -08002700 return -EINVAL;
2701
Wei Yongjund364d922008-05-09 15:13:26 -07002702 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08002703 * to many style socket, and an association was not found, then
2704 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002705 */
Wei Yongjund364d922008-05-09 15:13:26 -07002706 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2707 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08002708 return -EINVAL;
2709
Wei Yongjund364d922008-05-09 15:13:26 -07002710 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002711 if (asoc) {
2712 asoc->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002713 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002714 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002715 sctp_spp_sackdelay_enable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002716 } else {
Wei Yongjund364d922008-05-09 15:13:26 -07002717 sp->sackdelay = params.sack_delay;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002718 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002719 sctp_spp_sackdelay_enable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002720 }
Wei Yongjund364d922008-05-09 15:13:26 -07002721 }
2722
2723 if (params.sack_freq == 1) {
Frank Filz77086102005-12-22 11:37:30 -08002724 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002725 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002726 sctp_spp_sackdelay_disable(asoc->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002727 } else {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002728 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002729 sctp_spp_sackdelay_disable(sp->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002730 }
Wei Yongjund364d922008-05-09 15:13:26 -07002731 } else if (params.sack_freq > 1) {
2732 if (asoc) {
2733 asoc->sackfreq = params.sack_freq;
2734 asoc->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002735 sctp_spp_sackdelay_enable(asoc->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002736 } else {
2737 sp->sackfreq = params.sack_freq;
2738 sp->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002739 sctp_spp_sackdelay_enable(sp->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002740 }
Frank Filz77086102005-12-22 11:37:30 -08002741 }
2742
2743 /* If change is for association, also apply to each transport. */
2744 if (asoc) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002745 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2746 transports) {
Wei Yongjund364d922008-05-09 15:13:26 -07002747 if (params.sack_delay) {
Frank Filz77086102005-12-22 11:37:30 -08002748 trans->sackdelay =
Wei Yongjund364d922008-05-09 15:13:26 -07002749 msecs_to_jiffies(params.sack_delay);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002750 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002751 sctp_spp_sackdelay_enable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002752 }
Vlad Yasevich7bfe8bd2008-06-09 15:45:05 -07002753 if (params.sack_freq == 1) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002754 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002755 sctp_spp_sackdelay_disable(trans->param_flags);
Wei Yongjund364d922008-05-09 15:13:26 -07002756 } else if (params.sack_freq > 1) {
2757 trans->sackfreq = params.sack_freq;
2758 trans->param_flags =
wangweidong0ea5e4d2014-01-15 17:24:01 +08002759 sctp_spp_sackdelay_enable(trans->param_flags);
Frank Filz77086102005-12-22 11:37:30 -08002760 }
2761 }
2762 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002763
Frank Filz77086102005-12-22 11:37:30 -08002764 return 0;
2765}
2766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2768 *
2769 * Applications can specify protocol parameters for the default association
2770 * initialization. The option name argument to setsockopt() and getsockopt()
2771 * is SCTP_INITMSG.
2772 *
2773 * Setting initialization parameters is effective only on an unconnected
2774 * socket (for UDP-style sockets only future associations are effected
2775 * by the change). With TCP-style sockets, this option is inherited by
2776 * sockets derived from a listener socket.
2777 */
David S. Millerb7058842009-09-30 16:12:20 -07002778static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
2780 struct sctp_initmsg sinit;
2781 struct sctp_sock *sp = sctp_sk(sk);
2782
2783 if (optlen != sizeof(struct sctp_initmsg))
2784 return -EINVAL;
2785 if (copy_from_user(&sinit, optval, optlen))
2786 return -EFAULT;
2787
2788 if (sinit.sinit_num_ostreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002789 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 if (sinit.sinit_max_instreams)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002791 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (sinit.sinit_max_attempts)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002793 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (sinit.sinit_max_init_timeo)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002795 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797 return 0;
2798}
2799
2800/*
2801 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2802 *
2803 * Applications that wish to use the sendto() system call may wish to
2804 * specify a default set of parameters that would normally be supplied
2805 * through the inclusion of ancillary data. This socket option allows
2806 * such an application to set the default sctp_sndrcvinfo structure.
2807 * The application that wishes to use this socket option simply passes
2808 * in to this call the sctp_sndrcvinfo structure defined in Section
2809 * 5.2.2) The input parameters accepted by this call include
2810 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2811 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2812 * to this call if the caller is using the UDP model.
2813 */
2814static int sctp_setsockopt_default_send_param(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07002815 char __user *optval,
2816 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002819 struct sctp_association *asoc;
2820 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002822 if (optlen != sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return -EINVAL;
2824 if (copy_from_user(&info, optval, optlen))
2825 return -EFAULT;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002826 if (info.sinfo_flags &
2827 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2828 SCTP_ABORT | SCTP_EOF))
2829 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2832 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2833 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 if (asoc) {
2835 asoc->default_stream = info.sinfo_stream;
2836 asoc->default_flags = info.sinfo_flags;
2837 asoc->default_ppid = info.sinfo_ppid;
2838 asoc->default_context = info.sinfo_context;
2839 asoc->default_timetolive = info.sinfo_timetolive;
2840 } else {
2841 sp->default_stream = info.sinfo_stream;
2842 sp->default_flags = info.sinfo_flags;
2843 sp->default_ppid = info.sinfo_ppid;
2844 sp->default_context = info.sinfo_context;
2845 sp->default_timetolive = info.sinfo_timetolive;
2846 }
2847
2848 return 0;
2849}
2850
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02002851/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2852 * (SCTP_DEFAULT_SNDINFO)
2853 */
2854static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2855 char __user *optval,
2856 unsigned int optlen)
2857{
2858 struct sctp_sock *sp = sctp_sk(sk);
2859 struct sctp_association *asoc;
2860 struct sctp_sndinfo info;
2861
2862 if (optlen != sizeof(info))
2863 return -EINVAL;
2864 if (copy_from_user(&info, optval, optlen))
2865 return -EFAULT;
2866 if (info.snd_flags &
2867 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2868 SCTP_ABORT | SCTP_EOF))
2869 return -EINVAL;
2870
2871 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
2872 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
2873 return -EINVAL;
2874 if (asoc) {
2875 asoc->default_stream = info.snd_sid;
2876 asoc->default_flags = info.snd_flags;
2877 asoc->default_ppid = info.snd_ppid;
2878 asoc->default_context = info.snd_context;
2879 } else {
2880 sp->default_stream = info.snd_sid;
2881 sp->default_flags = info.snd_flags;
2882 sp->default_ppid = info.snd_ppid;
2883 sp->default_context = info.snd_context;
2884 }
2885
2886 return 0;
2887}
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
2890 *
2891 * Requests that the local SCTP stack use the enclosed peer address as
2892 * the association primary. The enclosed address must be one of the
2893 * association peer's addresses.
2894 */
2895static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002896 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897{
2898 struct sctp_prim prim;
2899 struct sctp_transport *trans;
2900
2901 if (optlen != sizeof(struct sctp_prim))
2902 return -EINVAL;
2903
2904 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
2905 return -EFAULT;
2906
2907 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
2908 if (!trans)
2909 return -EINVAL;
2910
2911 sctp_assoc_set_primary(trans->asoc, trans);
2912
2913 return 0;
2914}
2915
2916/*
2917 * 7.1.5 SCTP_NODELAY
2918 *
2919 * Turn on/off any Nagle-like algorithm. This means that packets are
2920 * generally sent as soon as possible and no unnecessary delays are
2921 * introduced, at the cost of more packets in the network. Expects an
2922 * integer boolean flag.
2923 */
2924static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07002925 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926{
2927 int val;
2928
2929 if (optlen < sizeof(int))
2930 return -EINVAL;
2931 if (get_user(val, (int __user *)optval))
2932 return -EFAULT;
2933
2934 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2935 return 0;
2936}
2937
2938/*
2939 *
2940 * 7.1.1 SCTP_RTOINFO
2941 *
2942 * The protocol parameters used to initialize and bound retransmission
2943 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
2944 * and modify these parameters.
2945 * All parameters are time values, in milliseconds. A value of 0, when
2946 * modifying the parameters, indicates that the current value should not
2947 * be changed.
2948 *
2949 */
David S. Millerb7058842009-09-30 16:12:20 -07002950static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
2951{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 struct sctp_rtoinfo rtoinfo;
2953 struct sctp_association *asoc;
wangweidong85f935d2013-12-11 09:50:38 +08002954 unsigned long rto_min, rto_max;
2955 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
2957 if (optlen != sizeof (struct sctp_rtoinfo))
2958 return -EINVAL;
2959
2960 if (copy_from_user(&rtoinfo, optval, optlen))
2961 return -EFAULT;
2962
2963 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
2964
2965 /* Set the values to the specific association */
2966 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2967 return -EINVAL;
2968
wangweidong85f935d2013-12-11 09:50:38 +08002969 rto_max = rtoinfo.srto_max;
2970 rto_min = rtoinfo.srto_min;
2971
2972 if (rto_max)
2973 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
2974 else
2975 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
2976
2977 if (rto_min)
2978 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
2979 else
2980 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
2981
2982 if (rto_min > rto_max)
2983 return -EINVAL;
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 if (asoc) {
2986 if (rtoinfo.srto_initial != 0)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002987 asoc->rto_initial =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 msecs_to_jiffies(rtoinfo.srto_initial);
wangweidong85f935d2013-12-11 09:50:38 +08002989 asoc->rto_max = rto_max;
2990 asoc->rto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 } else {
2992 /* If there is no association or the association-id = 0
2993 * set the values to the endpoint.
2994 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 if (rtoinfo.srto_initial != 0)
2996 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
wangweidong85f935d2013-12-11 09:50:38 +08002997 sp->rtoinfo.srto_max = rto_max;
2998 sp->rtoinfo.srto_min = rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 }
3000
3001 return 0;
3002}
3003
3004/*
3005 *
3006 * 7.1.2 SCTP_ASSOCINFO
3007 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02003008 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 * of the association.
3010 * Returns an error if the new association retransmission value is
3011 * greater than the sum of the retransmission value of the peer.
3012 * See [SCTP] for more information.
3013 *
3014 */
David S. Millerb7058842009-09-30 16:12:20 -07003015static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
3017
3018 struct sctp_assocparams assocparams;
3019 struct sctp_association *asoc;
3020
3021 if (optlen != sizeof(struct sctp_assocparams))
3022 return -EINVAL;
3023 if (copy_from_user(&assocparams, optval, optlen))
3024 return -EFAULT;
3025
3026 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3027
3028 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3029 return -EINVAL;
3030
3031 /* Set the values to the specific association */
3032 if (asoc) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003033 if (assocparams.sasoc_asocmaxrxt != 0) {
3034 __u32 path_sum = 0;
3035 int paths = 0;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003036 struct sctp_transport *peer_addr;
3037
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07003038 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3039 transports) {
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003040 path_sum += peer_addr->pathmaxrxt;
3041 paths++;
3042 }
3043
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003044 /* Only validate asocmaxrxt if we have more than
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003045 * one path/transport. We do this because path
3046 * retransmissions are only counted when we have more
3047 * then one path.
3048 */
3049 if (paths > 1 &&
3050 assocparams.sasoc_asocmaxrxt > path_sum)
3051 return -EINVAL;
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
Vlad Yasevich402d68c2006-06-17 22:54:51 -07003054 }
3055
Daniel Borkmann52db8822013-06-25 18:17:27 +02003056 if (assocparams.sasoc_cookie_life != 0)
3057 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 } else {
3059 /* Set the values to the endpoint */
3060 struct sctp_sock *sp = sctp_sk(sk);
3061
3062 if (assocparams.sasoc_asocmaxrxt != 0)
3063 sp->assocparams.sasoc_asocmaxrxt =
3064 assocparams.sasoc_asocmaxrxt;
3065 if (assocparams.sasoc_cookie_life != 0)
3066 sp->assocparams.sasoc_cookie_life =
3067 assocparams.sasoc_cookie_life;
3068 }
3069 return 0;
3070}
3071
3072/*
3073 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3074 *
3075 * This socket option is a boolean flag which turns on or off mapped V4
3076 * addresses. If this option is turned on and the socket is type
3077 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3078 * If this option is turned off, then no mapping will be done of V4
3079 * addresses and a user will receive both PF_INET6 and PF_INET type
3080 * addresses on the socket.
3081 */
David S. Millerb7058842009-09-30 16:12:20 -07003082static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
3084 int val;
3085 struct sctp_sock *sp = sctp_sk(sk);
3086
3087 if (optlen < sizeof(int))
3088 return -EINVAL;
3089 if (get_user(val, (int __user *)optval))
3090 return -EFAULT;
3091 if (val)
3092 sp->v4mapped = 1;
3093 else
3094 sp->v4mapped = 0;
3095
3096 return 0;
3097}
3098
3099/*
Wei Yongjune89c2092008-12-25 16:54:58 -08003100 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3101 * This option will get or set the maximum size to put in any outgoing
3102 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 * fragmented by SCTP into the specified size. Note that the underlying
3104 * SCTP implementation may fragment into smaller sized chunks when the
3105 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08003106 * the user. The default value for this option is '0' which indicates
3107 * the user is NOT limiting fragmentation and only the PMTU will effect
3108 * SCTP's choice of DATA chunk size. Note also that values set larger
3109 * than the maximum size of an IP datagram will effectively let SCTP
3110 * control fragmentation (i.e. the same as setting this option to 0).
3111 *
3112 * The following structure is used to access and modify this parameter:
3113 *
3114 * struct sctp_assoc_value {
3115 * sctp_assoc_t assoc_id;
3116 * uint32_t assoc_value;
3117 * };
3118 *
3119 * assoc_id: This parameter is ignored for one-to-one style sockets.
3120 * For one-to-many style sockets this parameter indicates which
3121 * association the user is performing an action upon. Note that if
3122 * this field's value is zero then the endpoints default value is
3123 * changed (effecting future associations only).
3124 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 */
David S. Millerb7058842009-09-30 16:12:20 -07003126static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
Wei Yongjune89c2092008-12-25 16:54:58 -08003128 struct sctp_assoc_value params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 struct sctp_sock *sp = sctp_sk(sk);
3131 int val;
3132
Wei Yongjune89c2092008-12-25 16:54:58 -08003133 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003134 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003135 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003136 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003137 "Use struct sctp_assoc_value instead\n",
3138 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08003139 if (copy_from_user(&val, optval, optlen))
3140 return -EFAULT;
3141 params.assoc_id = 0;
3142 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3143 if (copy_from_user(&params, optval, optlen))
3144 return -EFAULT;
3145 val = params.assoc_value;
3146 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 return -EINVAL;
Wei Yongjune89c2092008-12-25 16:54:58 -08003148
Ivan Skytte Jorgensen96a33992005-10-28 15:36:12 -07003149 if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Wei Yongjune89c2092008-12-25 16:54:58 -08003152 asoc = sctp_id2assoc(sk, params.assoc_id);
3153 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
3154 return -EINVAL;
3155
3156 if (asoc) {
3157 if (val == 0) {
3158 val = asoc->pathmtu;
3159 val -= sp->pf->af->net_header_len;
3160 val -= sizeof(struct sctphdr) +
3161 sizeof(struct sctp_data_chunk);
3162 }
Vlad Yasevichf68b2e02009-09-04 18:21:00 -04003163 asoc->user_frag = val;
3164 asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
Wei Yongjune89c2092008-12-25 16:54:58 -08003165 } else {
3166 sp->user_frag = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 }
3168
3169 return 0;
3170}
3171
3172
3173/*
3174 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3175 *
3176 * Requests that the peer mark the enclosed address as the association
3177 * primary. The enclosed address must be one of the association's
3178 * locally bound addresses. The following structure is used to make a
3179 * set primary request:
3180 */
3181static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003182 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003184 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 struct sctp_association *asoc = NULL;
3187 struct sctp_setpeerprim prim;
3188 struct sctp_chunk *chunk;
Wei Yongjun40a01032010-12-07 17:11:09 +00003189 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 int err;
3191
3192 sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003194 if (!net->sctp.addip_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 return -EPERM;
3196
3197 if (optlen != sizeof(struct sctp_setpeerprim))
3198 return -EINVAL;
3199
3200 if (copy_from_user(&prim, optval, optlen))
3201 return -EFAULT;
3202
3203 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003204 if (!asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return -EINVAL;
3206
3207 if (!asoc->peer.asconf_capable)
3208 return -EPERM;
3209
3210 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3211 return -EPERM;
3212
3213 if (!sctp_state(asoc, ESTABLISHED))
3214 return -ENOTCONN;
3215
Wei Yongjun40a01032010-12-07 17:11:09 +00003216 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3217 if (!af)
3218 return -EINVAL;
3219
3220 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3221 return -EADDRNOTAVAIL;
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3224 return -EADDRNOTAVAIL;
3225
3226 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3227 chunk = sctp_make_asconf_set_prim(asoc,
3228 (union sctp_addr *)&prim.sspp_addr);
3229 if (!chunk)
3230 return -ENOMEM;
3231
3232 err = sctp_send_asconf(asoc, chunk);
3233
Daniel Borkmannbb333812013-06-28 19:49:40 +02003234 pr_debug("%s: we set peer primary addr primitively\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
3236 return err;
3237}
3238
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003239static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003240 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003242 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003244 if (optlen != sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 return -EINVAL;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003246 if (copy_from_user(&adaptation, optval, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return -EFAULT;
3248
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003249 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
3251 return 0;
3252}
3253
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003254/*
3255 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3256 *
3257 * The context field in the sctp_sndrcvinfo structure is normally only
3258 * used when a failed message is retrieved holding the value that was
3259 * sent down on the actual send call. This option allows the setting of
3260 * a default context on an association basis that will be received on
3261 * reading messages from the peer. This is especially helpful in the
3262 * one-2-many model for an application to keep some reference to an
3263 * internal state machine that is processing messages on the
3264 * association. Note that the setting of this value only effects
3265 * received messages from the peer and does not effect the value that is
3266 * saved with outbound messages.
3267 */
3268static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003269 unsigned int optlen)
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003270{
3271 struct sctp_assoc_value params;
3272 struct sctp_sock *sp;
3273 struct sctp_association *asoc;
3274
3275 if (optlen != sizeof(struct sctp_assoc_value))
3276 return -EINVAL;
3277 if (copy_from_user(&params, optval, optlen))
3278 return -EFAULT;
3279
3280 sp = sctp_sk(sk);
3281
3282 if (params.assoc_id != 0) {
3283 asoc = sctp_id2assoc(sk, params.assoc_id);
3284 if (!asoc)
3285 return -EINVAL;
3286 asoc->default_rcv_context = params.assoc_value;
3287 } else {
3288 sp->default_rcv_context = params.assoc_value;
3289 }
3290
3291 return 0;
3292}
3293
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003294/*
3295 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3296 *
3297 * This options will at a minimum specify if the implementation is doing
3298 * fragmented interleave. Fragmented interleave, for a one to many
3299 * socket, is when subsequent calls to receive a message may return
3300 * parts of messages from different associations. Some implementations
3301 * may allow you to turn this value on or off. If so, when turned off,
3302 * no fragment interleave will occur (which will cause a head of line
3303 * blocking amongst multiple associations sharing the same one to many
3304 * socket). When this option is turned on, then each receive call may
3305 * come from a different association (thus the user must receive data
3306 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3307 * association each receive belongs to.
3308 *
3309 * This option takes a boolean value. A non-zero value indicates that
3310 * fragmented interleave is on. A value of zero indicates that
3311 * fragmented interleave is off.
3312 *
3313 * Note that it is important that an implementation that allows this
3314 * option to be turned on, have it off by default. Otherwise an unaware
3315 * application using the one to many model may become confused and act
3316 * incorrectly.
3317 */
3318static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3319 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003320 unsigned int optlen)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003321{
3322 int val;
3323
3324 if (optlen != sizeof(int))
3325 return -EINVAL;
3326 if (get_user(val, (int __user *)optval))
3327 return -EFAULT;
3328
3329 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
3330
3331 return 0;
3332}
3333
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003334/*
Wei Yongjun8510b932008-12-25 16:59:03 -08003335 * 8.1.21. Set or Get the SCTP Partial Delivery Point
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003336 * (SCTP_PARTIAL_DELIVERY_POINT)
Wei Yongjun8510b932008-12-25 16:59:03 -08003337 *
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003338 * This option will set or get the SCTP partial delivery point. This
3339 * point is the size of a message where the partial delivery API will be
3340 * invoked to help free up rwnd space for the peer. Setting this to a
Wei Yongjun8510b932008-12-25 16:59:03 -08003341 * lower value will cause partial deliveries to happen more often. The
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003342 * calls argument is an integer that sets or gets the partial delivery
Wei Yongjun8510b932008-12-25 16:59:03 -08003343 * point. Note also that the call will fail if the user attempts to set
3344 * this value larger than the socket receive buffer size.
3345 *
3346 * Note that any single message having a length smaller than or equal to
3347 * the SCTP partial delivery point will be delivered in one single read
3348 * call as long as the user provided buffer is large enough to hold the
3349 * message.
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003350 */
3351static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3352 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003353 unsigned int optlen)
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003354{
3355 u32 val;
3356
3357 if (optlen != sizeof(u32))
3358 return -EINVAL;
3359 if (get_user(val, (int __user *)optval))
3360 return -EFAULT;
3361
Wei Yongjun8510b932008-12-25 16:59:03 -08003362 /* Note: We double the receive buffer from what the user sets
3363 * it to be, also initial rwnd is based on rcvbuf/2.
3364 */
3365 if (val > (sk->sk_rcvbuf >> 1))
3366 return -EINVAL;
3367
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003368 sctp_sk(sk)->pd_point = val;
3369
3370 return 0; /* is this the right error code? */
3371}
3372
Vlad Yasevich70331572007-03-23 11:34:36 -07003373/*
3374 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3375 *
3376 * This option will allow a user to change the maximum burst of packets
3377 * that can be emitted by this association. Note that the default value
3378 * is 4, and some implementations may restrict this setting so that it
3379 * can only be lowered.
3380 *
3381 * NOTE: This text doesn't seem right. Do this on a socket basis with
3382 * future associations inheriting the socket value.
3383 */
3384static int sctp_setsockopt_maxburst(struct sock *sk,
3385 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003386 unsigned int optlen)
Vlad Yasevich70331572007-03-23 11:34:36 -07003387{
Neil Horman219b99a2008-03-05 13:44:46 -08003388 struct sctp_assoc_value params;
3389 struct sctp_sock *sp;
3390 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07003391 int val;
Neil Horman219b99a2008-03-05 13:44:46 -08003392 int assoc_id = 0;
Vlad Yasevich70331572007-03-23 11:34:36 -07003393
Neil Horman219b99a2008-03-05 13:44:46 -08003394 if (optlen == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05003395 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05003396 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05003397 "Use of int in max_burst socket option deprecated.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05003398 "Use struct sctp_assoc_value instead\n",
3399 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08003400 if (copy_from_user(&val, optval, optlen))
3401 return -EFAULT;
3402 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3403 if (copy_from_user(&params, optval, optlen))
3404 return -EFAULT;
3405 val = params.assoc_value;
3406 assoc_id = params.assoc_id;
3407 } else
3408 return -EINVAL;
3409
3410 sp = sctp_sk(sk);
3411
3412 if (assoc_id != 0) {
3413 asoc = sctp_id2assoc(sk, assoc_id);
3414 if (!asoc)
3415 return -EINVAL;
3416 asoc->max_burst = val;
3417 } else
3418 sp->max_burst = val;
Vlad Yasevich70331572007-03-23 11:34:36 -07003419
3420 return 0;
3421}
3422
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003423/*
3424 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3425 *
3426 * This set option adds a chunk type that the user is requesting to be
3427 * received only in an authenticated way. Changes to the list of chunks
3428 * will only effect future associations on the socket.
3429 */
3430static int sctp_setsockopt_auth_chunk(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003431 char __user *optval,
3432 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003433{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003434 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003435 struct sctp_authchunk val;
3436
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003437 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003438 return -EACCES;
3439
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003440 if (optlen != sizeof(struct sctp_authchunk))
3441 return -EINVAL;
3442 if (copy_from_user(&val, optval, optlen))
3443 return -EFAULT;
3444
3445 switch (val.sauth_chunk) {
Joe Perches7fd71b12011-07-01 09:43:11 +00003446 case SCTP_CID_INIT:
3447 case SCTP_CID_INIT_ACK:
3448 case SCTP_CID_SHUTDOWN_COMPLETE:
3449 case SCTP_CID_AUTH:
3450 return -EINVAL;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003451 }
3452
3453 /* add this chunk id to the endpoint */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003454 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003455}
3456
3457/*
3458 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3459 *
3460 * This option gets or sets the list of HMAC algorithms that the local
3461 * endpoint requires the peer to use.
3462 */
3463static int sctp_setsockopt_hmac_ident(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003464 char __user *optval,
3465 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003466{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003467 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003468 struct sctp_hmacalgo *hmacs;
Vlad Yasevichd9724052008-08-27 16:09:49 -07003469 u32 idents;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003470 int err;
3471
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003472 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003473 return -EACCES;
3474
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003475 if (optlen < sizeof(struct sctp_hmacalgo))
3476 return -EINVAL;
3477
wangweidongcb3f8372013-12-23 12:16:50 +08003478 hmacs = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003479 if (IS_ERR(hmacs))
3480 return PTR_ERR(hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003481
Vlad Yasevichd9724052008-08-27 16:09:49 -07003482 idents = hmacs->shmac_num_idents;
3483 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3484 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003485 err = -EINVAL;
3486 goto out;
3487 }
3488
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003489 err = sctp_auth_ep_set_hmacs(ep, hmacs);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003490out:
3491 kfree(hmacs);
3492 return err;
3493}
3494
3495/*
3496 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3497 *
3498 * This option will set a shared secret key which is used to build an
3499 * association shared key.
3500 */
3501static int sctp_setsockopt_auth_key(struct sock *sk,
3502 char __user *optval,
David S. Millerb7058842009-09-30 16:12:20 -07003503 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003504{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003505 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003506 struct sctp_authkey *authkey;
3507 struct sctp_association *asoc;
3508 int ret;
3509
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003510 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003511 return -EACCES;
3512
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003513 if (optlen <= sizeof(struct sctp_authkey))
3514 return -EINVAL;
3515
wangweidongcb3f8372013-12-23 12:16:50 +08003516 authkey = memdup_user(optval, optlen);
Shan Wei934253a2011-04-18 19:13:18 +00003517 if (IS_ERR(authkey))
3518 return PTR_ERR(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003519
Vlad Yasevich328fc472008-08-27 16:08:54 -07003520 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
Vlad Yasevich30c22352008-08-25 15:16:19 -07003521 ret = -EINVAL;
3522 goto out;
3523 }
3524
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003525 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3526 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3527 ret = -EINVAL;
3528 goto out;
3529 }
3530
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003531 ret = sctp_auth_set_key(ep, asoc, authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003532out:
Daniel Borkmann6ba542a2013-02-08 03:04:34 +00003533 kzfree(authkey);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003534 return ret;
3535}
3536
3537/*
3538 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3539 *
3540 * This option will get or set the active shared key to be used to build
3541 * the association shared key.
3542 */
3543static int sctp_setsockopt_active_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003544 char __user *optval,
3545 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003546{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003547 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003548 struct sctp_authkeyid val;
3549 struct sctp_association *asoc;
3550
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003551 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003552 return -EACCES;
3553
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003554 if (optlen != sizeof(struct sctp_authkeyid))
3555 return -EINVAL;
3556 if (copy_from_user(&val, optval, optlen))
3557 return -EFAULT;
3558
3559 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3560 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3561 return -EINVAL;
3562
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003563 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003564}
3565
3566/*
3567 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3568 *
3569 * This set option will delete a shared secret key from use.
3570 */
3571static int sctp_setsockopt_del_key(struct sock *sk,
David S. Millerb7058842009-09-30 16:12:20 -07003572 char __user *optval,
3573 unsigned int optlen)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003574{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003575 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003576 struct sctp_authkeyid val;
3577 struct sctp_association *asoc;
3578
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003579 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07003580 return -EACCES;
3581
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003582 if (optlen != sizeof(struct sctp_authkeyid))
3583 return -EINVAL;
3584 if (copy_from_user(&val, optval, optlen))
3585 return -EFAULT;
3586
3587 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3588 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3589 return -EINVAL;
3590
Vlad Yasevichb14878c2014-04-17 17:26:50 +02003591 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003592
3593}
3594
Michio Honda7dc04d72011-04-26 20:16:31 +09003595/*
3596 * 8.1.23 SCTP_AUTO_ASCONF
3597 *
3598 * This option will enable or disable the use of the automatic generation of
3599 * ASCONF chunks to add and delete addresses to an existing association. Note
3600 * that this option has two caveats namely: a) it only affects sockets that
3601 * are bound to all addresses available to the SCTP stack, and b) the system
3602 * administrator may have an overriding control that turns the ASCONF feature
3603 * off no matter what setting the socket option may have.
3604 * This option expects an integer boolean flag, where a non-zero value turns on
3605 * the option, and a zero value turns off the option.
3606 * Note. In this implementation, socket operation overrides default parameter
3607 * being set by sysctl as well as FreeBSD implementation
3608 */
3609static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3610 unsigned int optlen)
3611{
3612 int val;
3613 struct sctp_sock *sp = sctp_sk(sk);
3614
3615 if (optlen < sizeof(int))
3616 return -EINVAL;
3617 if (get_user(val, (int __user *)optval))
3618 return -EFAULT;
3619 if (!sctp_is_ep_boundall(sk) && val)
3620 return -EINVAL;
3621 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3622 return 0;
3623
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003624 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003625 if (val == 0 && sp->do_auto_asconf) {
3626 list_del(&sp->auto_asconf_list);
3627 sp->do_auto_asconf = 0;
3628 } else if (val && !sp->do_auto_asconf) {
3629 list_add_tail(&sp->auto_asconf_list,
Eric W. Biederman4db67e82012-08-06 08:42:04 +00003630 &sock_net(sk)->sctp.auto_asconf_splist);
Michio Honda7dc04d72011-04-26 20:16:31 +09003631 sp->do_auto_asconf = 1;
3632 }
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03003633 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda7dc04d72011-04-26 20:16:31 +09003634 return 0;
3635}
3636
Neil Horman5aa93bc2012-07-21 07:56:07 +00003637/*
3638 * SCTP_PEER_ADDR_THLDS
3639 *
3640 * This option allows us to alter the partially failed threshold for one or all
3641 * transports in an association. See Section 6.1 of:
3642 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3643 */
3644static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3645 char __user *optval,
3646 unsigned int optlen)
3647{
3648 struct sctp_paddrthlds val;
3649 struct sctp_transport *trans;
3650 struct sctp_association *asoc;
3651
3652 if (optlen < sizeof(struct sctp_paddrthlds))
3653 return -EINVAL;
3654 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3655 sizeof(struct sctp_paddrthlds)))
3656 return -EFAULT;
3657
3658
3659 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3660 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3661 if (!asoc)
3662 return -ENOENT;
3663 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3664 transports) {
3665 if (val.spt_pathmaxrxt)
3666 trans->pathmaxrxt = val.spt_pathmaxrxt;
3667 trans->pf_retrans = val.spt_pathpfthld;
3668 }
3669
3670 if (val.spt_pathmaxrxt)
3671 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3672 asoc->pf_retrans = val.spt_pathpfthld;
3673 } else {
3674 trans = sctp_addr_id2transport(sk, &val.spt_address,
3675 val.spt_assoc_id);
3676 if (!trans)
3677 return -ENOENT;
3678
3679 if (val.spt_pathmaxrxt)
3680 trans->pathmaxrxt = val.spt_pathmaxrxt;
3681 trans->pf_retrans = val.spt_pathpfthld;
3682 }
3683
3684 return 0;
3685}
3686
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02003687static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3688 char __user *optval,
3689 unsigned int optlen)
3690{
3691 int val;
3692
3693 if (optlen < sizeof(int))
3694 return -EINVAL;
3695 if (get_user(val, (int __user *) optval))
3696 return -EFAULT;
3697
3698 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3699
3700 return 0;
3701}
3702
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02003703static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3704 char __user *optval,
3705 unsigned int optlen)
3706{
3707 int val;
3708
3709 if (optlen < sizeof(int))
3710 return -EINVAL;
3711 if (get_user(val, (int __user *) optval))
3712 return -EFAULT;
3713
3714 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3715
3716 return 0;
3717}
3718
Xin Long28aa4c22016-07-09 19:47:40 +08003719static int sctp_setsockopt_pr_supported(struct sock *sk,
3720 char __user *optval,
3721 unsigned int optlen)
3722{
3723 struct sctp_assoc_value params;
3724 struct sctp_association *asoc;
3725 int retval = -EINVAL;
3726
3727 if (optlen != sizeof(params))
3728 goto out;
3729
3730 if (copy_from_user(&params, optval, optlen)) {
3731 retval = -EFAULT;
3732 goto out;
3733 }
3734
3735 asoc = sctp_id2assoc(sk, params.assoc_id);
3736 if (asoc) {
3737 asoc->prsctp_enable = !!params.assoc_value;
3738 } else if (!params.assoc_id) {
3739 struct sctp_sock *sp = sctp_sk(sk);
3740
3741 sp->ep->prsctp_enable = !!params.assoc_value;
3742 } else {
3743 goto out;
3744 }
3745
3746 retval = 0;
3747
3748out:
3749 return retval;
3750}
3751
Xin Longf959fb42016-07-09 19:47:41 +08003752static int sctp_setsockopt_default_prinfo(struct sock *sk,
3753 char __user *optval,
3754 unsigned int optlen)
3755{
3756 struct sctp_default_prinfo info;
3757 struct sctp_association *asoc;
3758 int retval = -EINVAL;
3759
3760 if (optlen != sizeof(info))
3761 goto out;
3762
3763 if (copy_from_user(&info, optval, sizeof(info))) {
3764 retval = -EFAULT;
3765 goto out;
3766 }
3767
3768 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
3769 goto out;
3770
3771 if (info.pr_policy == SCTP_PR_SCTP_NONE)
3772 info.pr_value = 0;
3773
3774 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
3775 if (asoc) {
3776 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
3777 asoc->default_timetolive = info.pr_value;
3778 } else if (!info.pr_assoc_id) {
3779 struct sctp_sock *sp = sctp_sk(sk);
3780
3781 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
3782 sp->default_timetolive = info.pr_value;
3783 } else {
3784 goto out;
3785 }
3786
3787 retval = 0;
3788
3789out:
3790 return retval;
3791}
3792
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793/* API 6.2 setsockopt(), getsockopt()
3794 *
3795 * Applications use setsockopt() and getsockopt() to set or retrieve
3796 * socket options. Socket options are used to change the default
3797 * behavior of sockets calls. They are described in Section 7.
3798 *
3799 * The syntax is:
3800 *
3801 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
3802 * int __user *optlen);
3803 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
3804 * int optlen);
3805 *
3806 * sd - the socket descript.
3807 * level - set to IPPROTO_SCTP for all SCTP options.
3808 * optname - the option name.
3809 * optval - the buffer to store the value of the option.
3810 * optlen - the size of the buffer.
3811 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02003812static int sctp_setsockopt(struct sock *sk, int level, int optname,
3813 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
3815 int retval = 0;
3816
Daniel Borkmannbb333812013-06-28 19:49:40 +02003817 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
3819 /* I can hardly begin to describe how wrong this is. This is
3820 * so broken as to be worse than useless. The API draft
3821 * REALLY is NOT helpful here... I am not convinced that the
3822 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
3823 * are at all well-founded.
3824 */
3825 if (level != SOL_SCTP) {
3826 struct sctp_af *af = sctp_sk(sk)->pf->af;
3827 retval = af->setsockopt(sk, level, optname, optval, optlen);
3828 goto out_nounlock;
3829 }
3830
wangweidong048ed4b2014-01-21 15:44:11 +08003831 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
3833 switch (optname) {
3834 case SCTP_SOCKOPT_BINDX_ADD:
3835 /* 'optlen' is the size of the addresses buffer. */
3836 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3837 optlen, SCTP_BINDX_ADD_ADDR);
3838 break;
3839
3840 case SCTP_SOCKOPT_BINDX_REM:
3841 /* 'optlen' is the size of the addresses buffer. */
3842 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
3843 optlen, SCTP_BINDX_REM_ADDR);
3844 break;
3845
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003846 case SCTP_SOCKOPT_CONNECTX_OLD:
3847 /* 'optlen' is the size of the addresses buffer. */
3848 retval = sctp_setsockopt_connectx_old(sk,
3849 (struct sockaddr __user *)optval,
3850 optlen);
3851 break;
3852
Frank Filz3f7a87d2005-06-20 13:14:57 -07003853 case SCTP_SOCKOPT_CONNECTX:
3854 /* 'optlen' is the size of the addresses buffer. */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07003855 retval = sctp_setsockopt_connectx(sk,
3856 (struct sockaddr __user *)optval,
3857 optlen);
Frank Filz3f7a87d2005-06-20 13:14:57 -07003858 break;
3859
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 case SCTP_DISABLE_FRAGMENTS:
3861 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3862 break;
3863
3864 case SCTP_EVENTS:
3865 retval = sctp_setsockopt_events(sk, optval, optlen);
3866 break;
3867
3868 case SCTP_AUTOCLOSE:
3869 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3870 break;
3871
3872 case SCTP_PEER_ADDR_PARAMS:
3873 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3874 break;
3875
Shan Wei4580ccc2011-01-18 22:39:00 +00003876 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07003877 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
Frank Filz77086102005-12-22 11:37:30 -08003878 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07003879 case SCTP_PARTIAL_DELIVERY_POINT:
3880 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3881 break;
Frank Filz77086102005-12-22 11:37:30 -08003882
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 case SCTP_INITMSG:
3884 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3885 break;
3886 case SCTP_DEFAULT_SEND_PARAM:
3887 retval = sctp_setsockopt_default_send_param(sk, optval,
3888 optlen);
3889 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02003890 case SCTP_DEFAULT_SNDINFO:
3891 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
3892 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 case SCTP_PRIMARY_ADDR:
3894 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3895 break;
3896 case SCTP_SET_PEER_PRIMARY_ADDR:
3897 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3898 break;
3899 case SCTP_NODELAY:
3900 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3901 break;
3902 case SCTP_RTOINFO:
3903 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3904 break;
3905 case SCTP_ASSOCINFO:
3906 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3907 break;
3908 case SCTP_I_WANT_MAPPED_V4_ADDR:
3909 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3910 break;
3911 case SCTP_MAXSEG:
3912 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3913 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08003914 case SCTP_ADAPTATION_LAYER:
3915 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08003917 case SCTP_CONTEXT:
3918 retval = sctp_setsockopt_context(sk, optval, optlen);
3919 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07003920 case SCTP_FRAGMENT_INTERLEAVE:
3921 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3922 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07003923 case SCTP_MAX_BURST:
3924 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3925 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003926 case SCTP_AUTH_CHUNK:
3927 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
3928 break;
3929 case SCTP_HMAC_IDENT:
3930 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
3931 break;
3932 case SCTP_AUTH_KEY:
3933 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
3934 break;
3935 case SCTP_AUTH_ACTIVE_KEY:
3936 retval = sctp_setsockopt_active_key(sk, optval, optlen);
3937 break;
3938 case SCTP_AUTH_DELETE_KEY:
3939 retval = sctp_setsockopt_del_key(sk, optval, optlen);
3940 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09003941 case SCTP_AUTO_ASCONF:
3942 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
3943 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00003944 case SCTP_PEER_ADDR_THLDS:
3945 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
3946 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02003947 case SCTP_RECVRCVINFO:
3948 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
3949 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02003950 case SCTP_RECVNXTINFO:
3951 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
3952 break;
Xin Long28aa4c22016-07-09 19:47:40 +08003953 case SCTP_PR_SUPPORTED:
3954 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
3955 break;
Xin Longf959fb42016-07-09 19:47:41 +08003956 case SCTP_DEFAULT_PRINFO:
3957 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
3958 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 default:
3960 retval = -ENOPROTOOPT;
3961 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
wangweidong048ed4b2014-01-21 15:44:11 +08003964 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
3966out_nounlock:
3967 return retval;
3968}
3969
3970/* API 3.1.6 connect() - UDP Style Syntax
3971 *
3972 * An application may use the connect() call in the UDP model to initiate an
3973 * association without sending data.
3974 *
3975 * The syntax is:
3976 *
3977 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
3978 *
3979 * sd: the socket descriptor to have a new association added to.
3980 *
3981 * nam: the address structure (either struct sockaddr_in or struct
3982 * sockaddr_in6 defined in RFC2553 [7]).
3983 *
3984 * len: the size of the address.
3985 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02003986static int sctp_connect(struct sock *sk, struct sockaddr *addr,
3987 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 int err = 0;
Frank Filz3f7a87d2005-06-20 13:14:57 -07003990 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
wangweidong048ed4b2014-01-21 15:44:11 +08003992 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Daniel Borkmannbb333812013-06-28 19:49:40 +02003994 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
3995 addr, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Frank Filz3f7a87d2005-06-20 13:14:57 -07003997 /* Validate addr_len before calling common connect/connectx routine. */
3998 af = sctp_get_af_specific(addr->sa_family);
3999 if (!af || addr_len < af->sockaddr_len) {
4000 err = -EINVAL;
4001 } else {
4002 /* Pass correct addr len to common routine (so it knows there
4003 * is only one address being passed.
4004 */
Vlad Yasevich88a0a942008-05-09 15:14:11 -07004005 err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 }
4007
wangweidong048ed4b2014-01-21 15:44:11 +08004008 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 return err;
4010}
4011
4012/* FIXME: Write comments. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004013static int sctp_disconnect(struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
4015 return -EOPNOTSUPP; /* STUB */
4016}
4017
4018/* 4.1.4 accept() - TCP Style Syntax
4019 *
4020 * Applications use accept() call to remove an established SCTP
4021 * association from the accept queue of the endpoint. A new socket
4022 * descriptor will be returned from accept() to represent the newly
4023 * formed association.
4024 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004025static struct sock *sctp_accept(struct sock *sk, int flags, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
4027 struct sctp_sock *sp;
4028 struct sctp_endpoint *ep;
4029 struct sock *newsk = NULL;
4030 struct sctp_association *asoc;
4031 long timeo;
4032 int error = 0;
4033
wangweidong048ed4b2014-01-21 15:44:11 +08004034 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
4036 sp = sctp_sk(sk);
4037 ep = sp->ep;
4038
4039 if (!sctp_style(sk, TCP)) {
4040 error = -EOPNOTSUPP;
4041 goto out;
4042 }
4043
4044 if (!sctp_sstate(sk, LISTENING)) {
4045 error = -EINVAL;
4046 goto out;
4047 }
4048
Sridhar Samudrala8abfedd2006-08-22 00:24:09 -07004049 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
4051 error = sctp_wait_for_accept(sk, timeo);
4052 if (error)
4053 goto out;
4054
4055 /* We treat the list of associations on the endpoint as the accept
4056 * queue and pick the first association on the list.
4057 */
4058 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4059
4060 newsk = sp->pf->create_accept_sk(sk, asoc);
4061 if (!newsk) {
4062 error = -ENOMEM;
4063 goto out;
4064 }
4065
4066 /* Populate the fields of the newsk from the oldsk and migrate the
4067 * asoc to the newsk.
4068 */
4069 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4070
4071out:
wangweidong048ed4b2014-01-21 15:44:11 +08004072 release_sock(sk);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004073 *err = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 return newsk;
4075}
4076
4077/* The SCTP ioctl handler. */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004078static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079{
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004080 int rc = -ENOTCONN;
4081
wangweidong048ed4b2014-01-21 15:44:11 +08004082 lock_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004083
4084 /*
4085 * SEQPACKET-style sockets in LISTENING state are valid, for
4086 * SCTP, so only discard TCP-style sockets in LISTENING state.
4087 */
4088 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4089 goto out;
4090
4091 switch (cmd) {
4092 case SIOCINQ: {
4093 struct sk_buff *skb;
4094 unsigned int amount = 0;
4095
4096 skb = skb_peek(&sk->sk_receive_queue);
4097 if (skb != NULL) {
4098 /*
4099 * We will only return the amount of this packet since
4100 * that is all that will be read.
4101 */
4102 amount = skb->len;
4103 }
4104 rc = put_user(amount, (int __user *)arg);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004105 break;
David S. Miller9a7241c2010-10-03 22:14:37 -07004106 }
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004107 default:
4108 rc = -ENOIOCTLCMD;
4109 break;
4110 }
4111out:
wangweidong048ed4b2014-01-21 15:44:11 +08004112 release_sock(sk);
Diego Elio 'Flameeyes' Pettenò65040c32010-09-03 03:47:03 +00004113 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114}
4115
4116/* This is the function which gets called during socket creation to
4117 * initialized the SCTP-specific portion of the sock.
4118 * The sock structure should already be zero-filled memory.
4119 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004120static int sctp_init_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004122 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 struct sctp_sock *sp;
4124
Daniel Borkmannbb333812013-06-28 19:49:40 +02004125 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
4127 sp = sctp_sk(sk);
4128
4129 /* Initialize the SCTP per socket area. */
4130 switch (sk->sk_type) {
4131 case SOCK_SEQPACKET:
4132 sp->type = SCTP_SOCKET_UDP;
4133 break;
4134 case SOCK_STREAM:
4135 sp->type = SCTP_SOCKET_TCP;
4136 break;
4137 default:
4138 return -ESOCKTNOSUPPORT;
4139 }
4140
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03004141 sk->sk_gso_type = SKB_GSO_SCTP;
4142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 /* Initialize default send parameters. These parameters can be
4144 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4145 */
4146 sp->default_stream = 0;
4147 sp->default_ppid = 0;
4148 sp->default_flags = 0;
4149 sp->default_context = 0;
4150 sp->default_timetolive = 0;
4151
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004152 sp->default_rcv_context = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004153 sp->max_burst = net->sctp.max_burst;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08004154
Neil Horman3c681982012-10-24 09:20:03 +00004155 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 /* Initialize default setup parameters. These parameters
4158 * can be modified with the SCTP_INITMSG socket option or
4159 * overridden by the SCTP_INIT CMSG.
4160 */
4161 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4162 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004163 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4164 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
4166 /* Initialize default RTO related parameters. These parameters can
4167 * be modified for with the SCTP_RTOINFO socket option.
4168 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004169 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4170 sp->rtoinfo.srto_max = net->sctp.rto_max;
4171 sp->rtoinfo.srto_min = net->sctp.rto_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
4173 /* Initialize default association related parameters. These parameters
4174 * can be modified with the SCTP_ASSOCINFO socket option.
4175 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004176 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 sp->assocparams.sasoc_number_peer_destinations = 0;
4178 sp->assocparams.sasoc_peer_rwnd = 0;
4179 sp->assocparams.sasoc_local_rwnd = 0;
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004180 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
4182 /* Initialize default event subscriptions. By default, all the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004183 * options are off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 */
4185 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4186
4187 /* Default Peer Address Parameters. These defaults can
4188 * be modified via SCTP_PEER_ADDR_PARAMS
4189 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004190 sp->hbinterval = net->sctp.hb_interval;
4191 sp->pathmaxrxt = net->sctp.max_retrans_path;
wangweidong4e2d52b2013-12-23 12:16:54 +08004192 sp->pathmtu = 0; /* allow default discovery */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004193 sp->sackdelay = net->sctp.sack_timeout;
Vlad Yasevich7bfe8bd2008-06-09 15:45:05 -07004194 sp->sackfreq = 2;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004195 sp->param_flags = SPP_HB_ENABLE |
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004196 SPP_PMTUD_ENABLE |
4197 SPP_SACKDELAY_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
4199 /* If enabled no SCTP message fragmentation will be performed.
4200 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4201 */
4202 sp->disable_fragments = 0;
4203
Sridhar Samudrala208edef2006-09-29 17:08:01 -07004204 /* Enable Nagle algorithm by default. */
4205 sp->nodelay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004207 sp->recvrcvinfo = 0;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02004208 sp->recvnxtinfo = 0;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02004209
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 /* Enable by default. */
4211 sp->v4mapped = 1;
4212
4213 /* Auto-close idle associations after the configured
4214 * number of seconds. A value of 0 disables this
4215 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4216 * for UDP-style sockets only.
4217 */
4218 sp->autoclose = 0;
4219
4220 /* User specified fragmentation limit. */
4221 sp->user_frag = 0;
4222
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08004223 sp->adaptation_ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
4225 sp->pf = sctp_get_pf_specific(sk->sk_family);
4226
4227 /* Control variables for partial data delivery. */
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004228 atomic_set(&sp->pd_mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 skb_queue_head_init(&sp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07004230 sp->frag_interleave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
4232 /* Create a per socket endpoint structure. Even if we
4233 * change the data structure relationships, this may still
4234 * be useful for storing pre-connect address information.
4235 */
Daniel Borkmannc164b832013-06-14 18:24:06 +02004236 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4237 if (!sp->ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 return -ENOMEM;
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 sp->hmac = NULL;
4241
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004242 sk->sk_destruct = sctp_destruct_sock;
4243
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 SCTP_DBG_OBJCNT_INC(sock);
David S. Miller6f756a82008-11-23 17:34:03 -08004245
4246 local_bh_disable();
Tonghao Zhangae67e542017-12-22 10:15:20 -08004247 sk_sockets_allocated_inc(sk);
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004248 sock_prot_inuse_add(net, sk->sk_prot, 1);
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004249
4250 /* Nothing can fail after this block, otherwise
4251 * sctp_destroy_sock() will be called without addr_wq_lock held
4252 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004253 if (net->sctp.default_auto_asconf) {
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004254 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
Michio Honda9f7d6532011-04-26 19:32:51 +09004255 list_add_tail(&sp->auto_asconf_list,
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00004256 &net->sctp.auto_asconf_splist);
Michio Honda9f7d6532011-04-26 19:32:51 +09004257 sp->do_auto_asconf = 1;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004258 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4259 } else {
Michio Honda9f7d6532011-04-26 19:32:51 +09004260 sp->do_auto_asconf = 0;
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004261 }
4262
David S. Miller6f756a82008-11-23 17:34:03 -08004263 local_bh_enable();
4264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 return 0;
4266}
4267
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03004268/* Cleanup any SCTP per socket resources. Must be called with
4269 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4270 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004271static void sctp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272{
Michio Honda9f7d6532011-04-26 19:32:51 +09004273 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
Daniel Borkmannbb333812013-06-28 19:49:40 +02004275 pr_debug("%s: sk:%p\n", __func__, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
4277 /* Release our hold on the endpoint. */
Michio Honda9f7d6532011-04-26 19:32:51 +09004278 sp = sctp_sk(sk);
Daniel Borkmann1abd1652013-06-06 15:53:47 +02004279 /* This could happen during socket init, thus we bail out
4280 * early, since the rest of the below is not setup either.
4281 */
4282 if (sp->ep == NULL)
4283 return;
4284
Michio Honda9f7d6532011-04-26 19:32:51 +09004285 if (sp->do_auto_asconf) {
4286 sp->do_auto_asconf = 0;
4287 list_del(&sp->auto_asconf_list);
4288 }
4289 sctp_endpoint_free(sp->ep);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004290 local_bh_disable();
Tonghao Zhangae67e542017-12-22 10:15:20 -08004291 sk_sockets_allocated_dec(sk);
Eric Dumazet9a57f7f2008-11-17 02:41:00 -08004292 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Eric Dumazet5bc0b3b2008-11-25 13:53:27 -08004293 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294}
4295
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004296/* Triggered when there are no references on the socket anymore */
4297static void sctp_destruct_sock(struct sock *sk)
4298{
4299 struct sctp_sock *sp = sctp_sk(sk);
4300
4301 /* Free up the HMAC transform. */
Herbert Xu5821c762016-01-24 21:20:12 +08004302 crypto_free_shash(sp->hmac);
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02004303
4304 inet_sock_destruct(sk);
4305}
4306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307/* API 4.1.7 shutdown() - TCP Style Syntax
4308 * int shutdown(int socket, int how);
4309 *
4310 * sd - the socket descriptor of the association to be closed.
4311 * how - Specifies the type of shutdown. The values are
4312 * as follows:
4313 * SHUT_RD
4314 * Disables further receive operations. No SCTP
4315 * protocol action is taken.
4316 * SHUT_WR
4317 * Disables further send operations, and initiates
4318 * the SCTP shutdown sequence.
4319 * SHUT_RDWR
4320 * Disables further send and receive operations
4321 * and initiates the SCTP shutdown sequence.
4322 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02004323static void sctp_shutdown(struct sock *sk, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324{
Eric W. Biederman55e26eb2012-08-07 07:25:24 +00004325 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 struct sctp_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328 if (!sctp_style(sk, TCP))
4329 return;
4330
Xin Long5bf35dd2016-11-13 21:44:37 +08004331 ep = sctp_sk(sk)->ep;
4332 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4333 struct sctp_association *asoc;
4334
Xin Longd46e4162016-06-09 22:48:18 +08004335 sk->sk_state = SCTP_SS_CLOSING;
Xin Long5bf35dd2016-11-13 21:44:37 +08004336 asoc = list_entry(ep->asocs.next,
4337 struct sctp_association, asocs);
4338 sctp_primitive_SHUTDOWN(net, asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 }
4340}
4341
Xin Long52c52a62016-04-14 15:35:30 +08004342int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4343 struct sctp_info *info)
4344{
4345 struct sctp_transport *prim;
4346 struct list_head *pos;
4347 int mask;
4348
4349 memset(info, 0, sizeof(*info));
4350 if (!asoc) {
4351 struct sctp_sock *sp = sctp_sk(sk);
4352
4353 info->sctpi_s_autoclose = sp->autoclose;
4354 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4355 info->sctpi_s_pd_point = sp->pd_point;
4356 info->sctpi_s_nodelay = sp->nodelay;
4357 info->sctpi_s_disable_fragments = sp->disable_fragments;
4358 info->sctpi_s_v4mapped = sp->v4mapped;
4359 info->sctpi_s_frag_interleave = sp->frag_interleave;
Xin Long40eb90e2016-05-29 17:42:13 +08004360 info->sctpi_s_type = sp->type;
Xin Long52c52a62016-04-14 15:35:30 +08004361
4362 return 0;
4363 }
4364
4365 info->sctpi_tag = asoc->c.my_vtag;
4366 info->sctpi_state = asoc->state;
4367 info->sctpi_rwnd = asoc->a_rwnd;
4368 info->sctpi_unackdata = asoc->unack_data;
4369 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4370 info->sctpi_instrms = asoc->c.sinit_max_instreams;
4371 info->sctpi_outstrms = asoc->c.sinit_num_ostreams;
4372 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4373 info->sctpi_inqueue++;
4374 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4375 info->sctpi_outqueue++;
4376 info->sctpi_overall_error = asoc->overall_error_count;
4377 info->sctpi_max_burst = asoc->max_burst;
4378 info->sctpi_maxseg = asoc->frag_point;
4379 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4380 info->sctpi_peer_tag = asoc->c.peer_vtag;
4381
4382 mask = asoc->peer.ecn_capable << 1;
4383 mask = (mask | asoc->peer.ipv4_address) << 1;
4384 mask = (mask | asoc->peer.ipv6_address) << 1;
4385 mask = (mask | asoc->peer.hostname_address) << 1;
4386 mask = (mask | asoc->peer.asconf_capable) << 1;
4387 mask = (mask | asoc->peer.prsctp_capable) << 1;
4388 mask = (mask | asoc->peer.auth_capable);
4389 info->sctpi_peer_capable = mask;
4390 mask = asoc->peer.sack_needed << 1;
4391 mask = (mask | asoc->peer.sack_generation) << 1;
4392 mask = (mask | asoc->peer.zero_window_announced);
4393 info->sctpi_peer_sack = mask;
4394
4395 info->sctpi_isacks = asoc->stats.isacks;
4396 info->sctpi_osacks = asoc->stats.osacks;
4397 info->sctpi_opackets = asoc->stats.opackets;
4398 info->sctpi_ipackets = asoc->stats.ipackets;
4399 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4400 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4401 info->sctpi_idupchunks = asoc->stats.idupchunks;
4402 info->sctpi_gapcnt = asoc->stats.gapcnt;
4403 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4404 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4405 info->sctpi_oodchunks = asoc->stats.oodchunks;
4406 info->sctpi_iodchunks = asoc->stats.iodchunks;
4407 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4408 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4409
4410 prim = asoc->peer.primary_path;
Stefano Brivio08d56d82017-08-23 13:27:13 +02004411 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
Xin Long52c52a62016-04-14 15:35:30 +08004412 info->sctpi_p_state = prim->state;
4413 info->sctpi_p_cwnd = prim->cwnd;
4414 info->sctpi_p_srtt = prim->srtt;
4415 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4416 info->sctpi_p_hbinterval = prim->hbinterval;
4417 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4418 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4419 info->sctpi_p_ssthresh = prim->ssthresh;
4420 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4421 info->sctpi_p_flight_size = prim->flight_size;
4422 info->sctpi_p_error = prim->error_count;
4423
4424 return 0;
4425}
4426EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4427
Xin Long626d16f2016-04-14 15:35:31 +08004428/* use callback to avoid exporting the core structure */
4429int sctp_transport_walk_start(struct rhashtable_iter *iter)
4430{
4431 int err;
4432
4433 err = rhashtable_walk_init(&sctp_transport_hashtable, iter,
4434 GFP_KERNEL);
4435 if (err)
4436 return err;
4437
4438 err = rhashtable_walk_start(iter);
Xin Long53fa1032016-04-14 15:35:35 +08004439 if (err && err != -EAGAIN) {
Vegard Nossum5fc382d2016-07-23 09:42:35 +02004440 rhashtable_walk_stop(iter);
Xin Long53fa1032016-04-14 15:35:35 +08004441 rhashtable_walk_exit(iter);
4442 return err;
4443 }
Xin Long626d16f2016-04-14 15:35:31 +08004444
Xin Long53fa1032016-04-14 15:35:35 +08004445 return 0;
Xin Long626d16f2016-04-14 15:35:31 +08004446}
4447
4448void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4449{
4450 rhashtable_walk_stop(iter);
4451 rhashtable_walk_exit(iter);
4452}
4453
4454struct sctp_transport *sctp_transport_get_next(struct net *net,
4455 struct rhashtable_iter *iter)
4456{
4457 struct sctp_transport *t;
4458
4459 t = rhashtable_walk_next(iter);
4460 for (; t; t = rhashtable_walk_next(iter)) {
4461 if (IS_ERR(t)) {
4462 if (PTR_ERR(t) == -EAGAIN)
4463 continue;
4464 break;
4465 }
4466
4467 if (net_eq(sock_net(t->asoc->base.sk), net) &&
4468 t->asoc->peer.primary_path == t)
4469 break;
4470 }
4471
4472 return t;
4473}
4474
4475struct sctp_transport *sctp_transport_get_idx(struct net *net,
4476 struct rhashtable_iter *iter,
4477 int pos)
4478{
4479 void *obj = SEQ_START_TOKEN;
4480
4481 while (pos && (obj = sctp_transport_get_next(net, iter)) &&
4482 !IS_ERR(obj))
4483 pos--;
4484
4485 return obj;
4486}
4487
4488int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
4489 void *p) {
4490 int err = 0;
4491 int hash = 0;
4492 struct sctp_ep_common *epb;
4493 struct sctp_hashbucket *head;
4494
4495 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
4496 hash++, head++) {
Xin Long8cda4262017-06-10 14:48:14 +08004497 read_lock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004498 sctp_for_each_hentry(epb, &head->chain) {
4499 err = cb(sctp_ep(epb), p);
4500 if (err)
4501 break;
4502 }
Xin Long8cda4262017-06-10 14:48:14 +08004503 read_unlock_bh(&head->lock);
Xin Long626d16f2016-04-14 15:35:31 +08004504 }
4505
4506 return err;
4507}
4508EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
4509
4510int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
4511 struct net *net,
4512 const union sctp_addr *laddr,
4513 const union sctp_addr *paddr, void *p)
4514{
4515 struct sctp_transport *transport;
Xin Long1cceda782016-09-29 02:55:44 +08004516 int err = -ENOENT;
Xin Long626d16f2016-04-14 15:35:31 +08004517
4518 rcu_read_lock();
4519 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
Xin Longed3cc322016-12-15 23:05:52 +08004520 if (!transport || !sctp_transport_hold(transport)) {
4521 rcu_read_unlock();
Xin Long626d16f2016-04-14 15:35:31 +08004522 goto out;
Xin Longed3cc322016-12-15 23:05:52 +08004523 }
Xin Long626d16f2016-04-14 15:35:31 +08004524 rcu_read_unlock();
Xin Long1cceda782016-09-29 02:55:44 +08004525 err = cb(transport, p);
Xin Longcd26da42016-10-31 20:32:31 +08004526 sctp_transport_put(transport);
Xin Long1cceda782016-09-29 02:55:44 +08004527
4528out:
Xin Long626d16f2016-04-14 15:35:31 +08004529 return err;
4530}
4531EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
4532
4533int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
4534 struct net *net, int pos, void *p) {
4535 struct rhashtable_iter hti;
Xin Long626d16f2016-04-14 15:35:31 +08004536 void *obj;
Xin Long53fa1032016-04-14 15:35:35 +08004537 int err;
Xin Long626d16f2016-04-14 15:35:31 +08004538
Xin Long53fa1032016-04-14 15:35:35 +08004539 err = sctp_transport_walk_start(&hti);
4540 if (err)
4541 return err;
Xin Long626d16f2016-04-14 15:35:31 +08004542
Xin Long4c246862017-06-15 17:49:08 +08004543 obj = sctp_transport_get_idx(net, &hti, pos + 1);
4544 for (; !IS_ERR_OR_NULL(obj); obj = sctp_transport_get_next(net, &hti)) {
Xin Long626d16f2016-04-14 15:35:31 +08004545 struct sctp_transport *transport = obj;
4546
4547 if (!sctp_transport_hold(transport))
4548 continue;
4549 err = cb(transport, p);
4550 sctp_transport_put(transport);
4551 if (err)
4552 break;
4553 }
Xin Long626d16f2016-04-14 15:35:31 +08004554 sctp_transport_walk_stop(&hti);
Xin Long53fa1032016-04-14 15:35:35 +08004555
Xin Long626d16f2016-04-14 15:35:31 +08004556 return err;
4557}
4558EXPORT_SYMBOL_GPL(sctp_for_each_transport);
4559
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560/* 7.2.1 Association Status (SCTP_STATUS)
4561
4562 * Applications can retrieve current status information about an
4563 * association, including association state, peer receiver window size,
4564 * number of unacked data chunks, and number of data chunks pending
4565 * receipt. This information is read-only.
4566 */
4567static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
4568 char __user *optval,
4569 int __user *optlen)
4570{
4571 struct sctp_status status;
4572 struct sctp_association *asoc = NULL;
4573 struct sctp_transport *transport;
4574 sctp_assoc_t associd;
4575 int retval = 0;
4576
Neil Horman408f22e2007-06-16 14:03:45 -04004577 if (len < sizeof(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 retval = -EINVAL;
4579 goto out;
4580 }
4581
Neil Horman408f22e2007-06-16 14:03:45 -04004582 len = sizeof(status);
4583 if (copy_from_user(&status, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 retval = -EFAULT;
4585 goto out;
4586 }
4587
4588 associd = status.sstat_assoc_id;
4589 asoc = sctp_id2assoc(sk, associd);
4590 if (!asoc) {
4591 retval = -EINVAL;
4592 goto out;
4593 }
4594
4595 transport = asoc->peer.primary_path;
4596
4597 status.sstat_assoc_id = sctp_assoc2id(asoc);
Daniel Borkmann38ab1fa2014-08-28 15:28:26 +02004598 status.sstat_state = sctp_assoc_to_state(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 status.sstat_rwnd = asoc->peer.rwnd;
4600 status.sstat_unackdata = asoc->unack_data;
4601
4602 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4603 status.sstat_instrms = asoc->c.sinit_max_instreams;
4604 status.sstat_outstrms = asoc->c.sinit_num_ostreams;
4605 status.sstat_fragmentation_point = asoc->frag_point;
4606 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Al Viro8cec6b82006-11-20 17:23:01 -08004607 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
4608 transport->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 /* Map ipv4 address into v4-mapped-on-v6 address. */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06004610 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 (union sctp_addr *)&status.sstat_primary.spinfo_address);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004612 status.sstat_primary.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 status.sstat_primary.spinfo_cwnd = transport->cwnd;
4614 status.sstat_primary.spinfo_srtt = transport->srtt;
4615 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004616 status.sstat_primary.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
Frank Filz3f7a87d2005-06-20 13:14:57 -07004618 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
4619 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
4620
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 if (put_user(len, optlen)) {
4622 retval = -EFAULT;
4623 goto out;
4624 }
4625
Daniel Borkmannbb333812013-06-28 19:49:40 +02004626 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
4627 __func__, len, status.sstat_state, status.sstat_rwnd,
4628 status.sstat_assoc_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
4630 if (copy_to_user(optval, &status, len)) {
4631 retval = -EFAULT;
4632 goto out;
4633 }
4634
4635out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004636 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637}
4638
4639
4640/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
4641 *
4642 * Applications can retrieve information about a specific peer address
4643 * of an association, including its reachability state, congestion
4644 * window, and retransmission timer values. This information is
4645 * read-only.
4646 */
4647static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
4648 char __user *optval,
4649 int __user *optlen)
4650{
4651 struct sctp_paddrinfo pinfo;
4652 struct sctp_transport *transport;
4653 int retval = 0;
4654
Neil Horman408f22e2007-06-16 14:03:45 -04004655 if (len < sizeof(pinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 retval = -EINVAL;
4657 goto out;
4658 }
4659
Neil Horman408f22e2007-06-16 14:03:45 -04004660 len = sizeof(pinfo);
4661 if (copy_from_user(&pinfo, optval, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 retval = -EFAULT;
4663 goto out;
4664 }
4665
4666 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
4667 pinfo.spinfo_assoc_id);
4668 if (!transport)
4669 return -EINVAL;
4670
4671 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
Frank Filz3f7a87d2005-06-20 13:14:57 -07004672 pinfo.spinfo_state = transport->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 pinfo.spinfo_cwnd = transport->cwnd;
4674 pinfo.spinfo_srtt = transport->srtt;
4675 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004676 pinfo.spinfo_mtu = transport->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Frank Filz3f7a87d2005-06-20 13:14:57 -07004678 if (pinfo.spinfo_state == SCTP_UNKNOWN)
4679 pinfo.spinfo_state = SCTP_ACTIVE;
4680
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 if (put_user(len, optlen)) {
4682 retval = -EFAULT;
4683 goto out;
4684 }
4685
4686 if (copy_to_user(optval, &pinfo, len)) {
4687 retval = -EFAULT;
4688 goto out;
4689 }
4690
4691out:
Eric Dumazeta02cec22010-09-22 20:43:57 +00004692 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693}
4694
4695/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
4696 *
4697 * This option is a on/off flag. If enabled no SCTP message
4698 * fragmentation will be performed. Instead if a message being sent
4699 * exceeds the current PMTU size, the message will NOT be sent and
4700 * instead a error will be indicated to the user.
4701 */
4702static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
4703 char __user *optval, int __user *optlen)
4704{
4705 int val;
4706
4707 if (len < sizeof(int))
4708 return -EINVAL;
4709
4710 len = sizeof(int);
4711 val = (sctp_sk(sk)->disable_fragments == 1);
4712 if (put_user(len, optlen))
4713 return -EFAULT;
4714 if (copy_to_user(optval, &val, len))
4715 return -EFAULT;
4716 return 0;
4717}
4718
4719/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
4720 *
4721 * This socket option is used to specify various notifications and
4722 * ancillary data the user wishes to receive.
4723 */
4724static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
4725 int __user *optlen)
4726{
Jiri Slabya4b8e712016-10-21 14:13:24 +02004727 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 return -EINVAL;
Thomas Grafacdd5982012-04-03 22:17:53 +00004729 if (len > sizeof(struct sctp_event_subscribe))
4730 len = sizeof(struct sctp_event_subscribe);
Neil Horman408f22e2007-06-16 14:03:45 -04004731 if (put_user(len, optlen))
4732 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4734 return -EFAULT;
4735 return 0;
4736}
4737
4738/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
4739 *
4740 * This socket option is applicable to the UDP-style socket only. When
4741 * set it will cause associations that are idle for more than the
4742 * specified number of seconds to automatically close. An association
4743 * being idle is defined an association that has NOT sent or received
4744 * user data. The special value of '0' indicates that no automatic
4745 * close of any associations should be performed. The option expects an
4746 * integer defining the number of seconds of idle time before an
4747 * association is closed.
4748 */
4749static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
4750{
4751 /* Applicable to UDP-style socket only */
4752 if (sctp_style(sk, TCP))
4753 return -EOPNOTSUPP;
Neil Horman408f22e2007-06-16 14:03:45 -04004754 if (len < sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004756 len = sizeof(int);
4757 if (put_user(len, optlen))
4758 return -EFAULT;
4759 if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 return -EFAULT;
4761 return 0;
4762}
4763
4764/* Helper routine to branch off an association to a new socket. */
Benjamin Poirier0343c552012-03-08 05:55:58 +00004765int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766{
Benjamin Poirier0343c552012-03-08 05:55:58 +00004767 struct sctp_association *asoc = sctp_id2assoc(sk, id);
Jason Gunthorpe299ee122014-07-30 12:40:53 -06004768 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 struct socket *sock;
4770 int err = 0;
4771
Xin Long362d2ce2017-10-17 23:26:10 +08004772 /* Do not peel off from one netns to another one. */
4773 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
4774 return -EINVAL;
4775
Benjamin Poirier0343c552012-03-08 05:55:58 +00004776 if (!asoc)
4777 return -EINVAL;
4778
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 /* An association cannot be branched off from an already peeled-off
4780 * socket, nor is this supported for tcp style sockets.
4781 */
4782 if (!sctp_style(sk, UDP))
4783 return -EINVAL;
4784
4785 /* Create a new socket. */
4786 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
4787 if (err < 0)
4788 return err;
4789
Vlad Yasevich914e1c82009-02-13 08:33:44 +00004790 sctp_copy_sock(sock->sk, sk, asoc);
Vlad Yasevich4f444302006-10-30 18:54:32 -08004791
4792 /* Make peeled-off sockets more like 1-1 accepted sockets.
4793 * Set the daddr and initialize id to something more random
4794 */
Jason Gunthorpe299ee122014-07-30 12:40:53 -06004795 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00004796
4797 /* Populate the fields of the newsk from the oldsk and migrate the
4798 * asoc to the newsk.
4799 */
4800 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
Vlad Yasevich4f444302006-10-30 18:54:32 -08004801
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 *sockp = sock;
4803
4804 return err;
4805}
Benjamin Poirier0343c552012-03-08 05:55:58 +00004806EXPORT_SYMBOL(sctp_do_peeloff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
4808static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
4809{
4810 sctp_peeloff_arg_t peeloff;
4811 struct socket *newsock;
Al Viro56b31d12012-08-18 00:25:51 -04004812 struct file *newfile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Neil Horman408f22e2007-06-16 14:03:45 -04004815 if (len < sizeof(sctp_peeloff_arg_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004817 len = sizeof(sctp_peeloff_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 if (copy_from_user(&peeloff, optval, len))
4819 return -EFAULT;
4820
Benjamin Poirier0343c552012-03-08 05:55:58 +00004821 retval = sctp_do_peeloff(sk, peeloff.associd, &newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 if (retval < 0)
4823 goto out;
4824
4825 /* Map the socket to an unused fd that can be returned to the user. */
Yann Droneaud8a59bd32013-07-02 18:39:36 +02004826 retval = get_unused_fd_flags(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 if (retval < 0) {
4828 sock_release(newsock);
4829 goto out;
4830 }
4831
Linus Torvaldsaab174f2012-10-02 20:25:04 -07004832 newfile = sock_alloc_file(newsock, 0, NULL);
Viresh Kumarb5ffe632015-08-12 15:59:47 +05304833 if (IS_ERR(newfile)) {
Al Viro56b31d12012-08-18 00:25:51 -04004834 put_unused_fd(retval);
4835 sock_release(newsock);
4836 return PTR_ERR(newfile);
4837 }
4838
Daniel Borkmannbb333812013-06-28 19:49:40 +02004839 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
4840 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
4842 /* Return the fd mapped to the new socket. */
Al Viro56b31d12012-08-18 00:25:51 -04004843 if (put_user(len, optlen)) {
4844 fput(newfile);
4845 put_unused_fd(retval);
Neil Horman408f22e2007-06-16 14:03:45 -04004846 return -EFAULT;
Al Viro56b31d12012-08-18 00:25:51 -04004847 }
4848 peeloff.sd = retval;
4849 if (copy_to_user(optval, &peeloff, len)) {
4850 fput(newfile);
4851 put_unused_fd(retval);
4852 return -EFAULT;
4853 }
4854 fd_install(retval, newfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855out:
4856 return retval;
4857}
4858
4859/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
4860 *
4861 * Applications can enable or disable heartbeats for any peer address of
4862 * an association, modify an address's heartbeat interval, force a
4863 * heartbeat to be sent immediately, and adjust the address's maximum
4864 * number of retransmissions sent before an address is considered
4865 * unreachable. The following structure is used to access and modify an
4866 * address's parameters:
4867 *
4868 * struct sctp_paddrparams {
Frank Filz52ccb8e2005-12-22 11:36:46 -08004869 * sctp_assoc_t spp_assoc_id;
4870 * struct sockaddr_storage spp_address;
4871 * uint32_t spp_hbinterval;
4872 * uint16_t spp_pathmaxrxt;
4873 * uint32_t spp_pathmtu;
4874 * uint32_t spp_sackdelay;
4875 * uint32_t spp_flags;
4876 * };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 *
Frank Filz52ccb8e2005-12-22 11:36:46 -08004878 * spp_assoc_id - (one-to-many style socket) This is filled in the
4879 * application, and identifies the association for
4880 * this query.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 * spp_address - This specifies which address is of interest.
4882 * spp_hbinterval - This contains the value of the heartbeat interval,
Frank Filz52ccb8e2005-12-22 11:36:46 -08004883 * in milliseconds. If a value of zero
4884 * is present in this field then no changes are to
4885 * be made to this parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 * spp_pathmaxrxt - This contains the maximum number of
4887 * retransmissions before this address shall be
Frank Filz52ccb8e2005-12-22 11:36:46 -08004888 * considered unreachable. If a value of zero
4889 * is present in this field then no changes are to
4890 * be made to this parameter.
4891 * spp_pathmtu - When Path MTU discovery is disabled the value
4892 * specified here will be the "fixed" path mtu.
4893 * Note that if the spp_address field is empty
4894 * then all associations on this address will
4895 * have this fixed path mtu set upon them.
4896 *
4897 * spp_sackdelay - When delayed sack is enabled, this value specifies
4898 * the number of milliseconds that sacks will be delayed
4899 * for. This value will apply to all addresses of an
4900 * association if the spp_address field is empty. Note
4901 * also, that if delayed sack is enabled and this
4902 * value is set to 0, no change is made to the last
4903 * recorded delayed sack timer value.
4904 *
4905 * spp_flags - These flags are used to control various features
4906 * on an association. The flag field may contain
4907 * zero or more of the following options.
4908 *
4909 * SPP_HB_ENABLE - Enable heartbeats on the
4910 * specified address. Note that if the address
4911 * field is empty all addresses for the association
4912 * have heartbeats enabled upon them.
4913 *
4914 * SPP_HB_DISABLE - Disable heartbeats on the
4915 * speicifed address. Note that if the address
4916 * field is empty all addresses for the association
4917 * will have their heartbeats disabled. Note also
4918 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
4919 * mutually exclusive, only one of these two should
4920 * be specified. Enabling both fields will have
4921 * undetermined results.
4922 *
4923 * SPP_HB_DEMAND - Request a user initiated heartbeat
4924 * to be made immediately.
4925 *
4926 * SPP_PMTUD_ENABLE - This field will enable PMTU
4927 * discovery upon the specified address. Note that
4928 * if the address feild is empty then all addresses
4929 * on the association are effected.
4930 *
4931 * SPP_PMTUD_DISABLE - This field will disable PMTU
4932 * discovery upon the specified address. Note that
4933 * if the address feild is empty then all addresses
4934 * on the association are effected. Not also that
4935 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
4936 * exclusive. Enabling both will have undetermined
4937 * results.
4938 *
4939 * SPP_SACKDELAY_ENABLE - Setting this flag turns
4940 * on delayed sack. The time specified in spp_sackdelay
4941 * is used to specify the sack delay for this address. Note
4942 * that if spp_address is empty then all addresses will
4943 * enable delayed sack and take on the sack delay
4944 * value specified in spp_sackdelay.
4945 * SPP_SACKDELAY_DISABLE - Setting this flag turns
4946 * off delayed sack. If the spp_address field is blank then
4947 * delayed sack is disabled for the entire association. Note
4948 * also that this field is mutually exclusive to
4949 * SPP_SACKDELAY_ENABLE, setting both will have undefined
4950 * results.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 */
4952static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
Frank Filz52ccb8e2005-12-22 11:36:46 -08004953 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954{
Frank Filz52ccb8e2005-12-22 11:36:46 -08004955 struct sctp_paddrparams params;
4956 struct sctp_transport *trans = NULL;
4957 struct sctp_association *asoc = NULL;
4958 struct sctp_sock *sp = sctp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
Neil Horman408f22e2007-06-16 14:03:45 -04004960 if (len < sizeof(struct sctp_paddrparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04004962 len = sizeof(struct sctp_paddrparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 if (copy_from_user(&params, optval, len))
4964 return -EFAULT;
4965
Frank Filz52ccb8e2005-12-22 11:36:46 -08004966 /* If an address other than INADDR_ANY is specified, and
4967 * no transport is found, then the request is invalid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 */
wangweidongcb3f8372013-12-23 12:16:50 +08004969 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
Frank Filz52ccb8e2005-12-22 11:36:46 -08004970 trans = sctp_addr_id2transport(sk, &params.spp_address,
4971 params.spp_assoc_id);
4972 if (!trans) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02004973 pr_debug("%s: failed no transport\n", __func__);
Frank Filz52ccb8e2005-12-22 11:36:46 -08004974 return -EINVAL;
4975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 }
4977
Frank Filz52ccb8e2005-12-22 11:36:46 -08004978 /* Get association, if assoc_id != 0 and the socket is a one
4979 * to many style socket, and an association was not found, then
4980 * the id was invalid.
4981 */
4982 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
4983 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02004984 pr_debug("%s: failed no association\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 return -EINVAL;
Frank Filz52ccb8e2005-12-22 11:36:46 -08004986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
Frank Filz52ccb8e2005-12-22 11:36:46 -08004988 if (trans) {
4989 /* Fetch transport values. */
4990 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
4991 params.spp_pathmtu = trans->pathmtu;
4992 params.spp_pathmaxrxt = trans->pathmaxrxt;
4993 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
Frank Filz52ccb8e2005-12-22 11:36:46 -08004995 /*draft-11 doesn't say what to return in spp_flags*/
4996 params.spp_flags = trans->param_flags;
4997 } else if (asoc) {
4998 /* Fetch association values. */
4999 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5000 params.spp_pathmtu = asoc->pathmtu;
5001 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5002 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003
Frank Filz52ccb8e2005-12-22 11:36:46 -08005004 /*draft-11 doesn't say what to return in spp_flags*/
5005 params.spp_flags = asoc->param_flags;
5006 } else {
5007 /* Fetch socket values. */
5008 params.spp_hbinterval = sp->hbinterval;
5009 params.spp_pathmtu = sp->pathmtu;
5010 params.spp_sackdelay = sp->sackdelay;
5011 params.spp_pathmaxrxt = sp->pathmaxrxt;
5012
5013 /*draft-11 doesn't say what to return in spp_flags*/
5014 params.spp_flags = sp->param_flags;
5015 }
5016
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 if (copy_to_user(optval, &params, len))
5018 return -EFAULT;
5019
5020 if (put_user(len, optlen))
5021 return -EFAULT;
5022
5023 return 0;
5024}
5025
Wei Yongjund364d922008-05-09 15:13:26 -07005026/*
5027 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
Frank Filz77086102005-12-22 11:37:30 -08005028 *
Wei Yongjund364d922008-05-09 15:13:26 -07005029 * This option will effect the way delayed acks are performed. This
5030 * option allows you to get or set the delayed ack time, in
5031 * milliseconds. It also allows changing the delayed ack frequency.
5032 * Changing the frequency to 1 disables the delayed sack algorithm. If
5033 * the assoc_id is 0, then this sets or gets the endpoints default
5034 * values. If the assoc_id field is non-zero, then the set or get
5035 * effects the specified association for the one to many model (the
5036 * assoc_id field is ignored by the one to one model). Note that if
5037 * sack_delay or sack_freq are 0 when setting this option, then the
5038 * current values will remain unchanged.
Frank Filz77086102005-12-22 11:37:30 -08005039 *
Wei Yongjund364d922008-05-09 15:13:26 -07005040 * struct sctp_sack_info {
5041 * sctp_assoc_t sack_assoc_id;
5042 * uint32_t sack_delay;
5043 * uint32_t sack_freq;
5044 * };
Frank Filz77086102005-12-22 11:37:30 -08005045 *
Wei Yongjund364d922008-05-09 15:13:26 -07005046 * sack_assoc_id - This parameter, indicates which association the user
5047 * is performing an action upon. Note that if this field's value is
5048 * zero then the endpoints default value is changed (effecting future
5049 * associations only).
Frank Filz77086102005-12-22 11:37:30 -08005050 *
Wei Yongjund364d922008-05-09 15:13:26 -07005051 * sack_delay - This parameter contains the number of milliseconds that
5052 * the user is requesting the delayed ACK timer be set to. Note that
5053 * this value is defined in the standard to be between 200 and 500
5054 * milliseconds.
Frank Filz77086102005-12-22 11:37:30 -08005055 *
Wei Yongjund364d922008-05-09 15:13:26 -07005056 * sack_freq - This parameter contains the number of packets that must
5057 * be received before a sack is sent without waiting for the delay
5058 * timer to expire. The default value for this is 2, setting this
5059 * value to 1 will disable the delayed sack algorithm.
Frank Filz77086102005-12-22 11:37:30 -08005060 */
Wei Yongjund364d922008-05-09 15:13:26 -07005061static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
Frank Filz77086102005-12-22 11:37:30 -08005062 char __user *optval,
5063 int __user *optlen)
5064{
Wei Yongjund364d922008-05-09 15:13:26 -07005065 struct sctp_sack_info params;
Frank Filz77086102005-12-22 11:37:30 -08005066 struct sctp_association *asoc = NULL;
5067 struct sctp_sock *sp = sctp_sk(sk);
5068
Wei Yongjund364d922008-05-09 15:13:26 -07005069 if (len >= sizeof(struct sctp_sack_info)) {
5070 len = sizeof(struct sctp_sack_info);
5071
5072 if (copy_from_user(&params, optval, len))
5073 return -EFAULT;
5074 } else if (len == sizeof(struct sctp_assoc_value)) {
Neil Horman94f65192013-12-23 08:29:43 -05005075 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005076 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005077 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005078 "Use struct sctp_sack_info instead\n",
5079 current->comm, task_pid_nr(current));
Wei Yongjund364d922008-05-09 15:13:26 -07005080 if (copy_from_user(&params, optval, len))
5081 return -EFAULT;
5082 } else
wangweidongcb3f8372013-12-23 12:16:50 +08005083 return -EINVAL;
Frank Filz77086102005-12-22 11:37:30 -08005084
Wei Yongjund364d922008-05-09 15:13:26 -07005085 /* Get association, if sack_assoc_id != 0 and the socket is a one
Frank Filz77086102005-12-22 11:37:30 -08005086 * to many style socket, and an association was not found, then
5087 * the id was invalid.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005088 */
Wei Yongjund364d922008-05-09 15:13:26 -07005089 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5090 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
Frank Filz77086102005-12-22 11:37:30 -08005091 return -EINVAL;
5092
5093 if (asoc) {
5094 /* Fetch association values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005095 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5096 params.sack_delay = jiffies_to_msecs(
Frank Filz77086102005-12-22 11:37:30 -08005097 asoc->sackdelay);
Wei Yongjund364d922008-05-09 15:13:26 -07005098 params.sack_freq = asoc->sackfreq;
5099
5100 } else {
5101 params.sack_delay = 0;
5102 params.sack_freq = 1;
5103 }
Frank Filz77086102005-12-22 11:37:30 -08005104 } else {
5105 /* Fetch socket values. */
Wei Yongjund364d922008-05-09 15:13:26 -07005106 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5107 params.sack_delay = sp->sackdelay;
5108 params.sack_freq = sp->sackfreq;
5109 } else {
5110 params.sack_delay = 0;
5111 params.sack_freq = 1;
5112 }
Frank Filz77086102005-12-22 11:37:30 -08005113 }
5114
5115 if (copy_to_user(optval, &params, len))
5116 return -EFAULT;
5117
5118 if (put_user(len, optlen))
5119 return -EFAULT;
5120
5121 return 0;
5122}
5123
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5125 *
5126 * Applications can specify protocol parameters for the default association
5127 * initialization. The option name argument to setsockopt() and getsockopt()
5128 * is SCTP_INITMSG.
5129 *
5130 * Setting initialization parameters is effective only on an unconnected
5131 * socket (for UDP-style sockets only future associations are effected
5132 * by the change). With TCP-style sockets, this option is inherited by
5133 * sockets derived from a listener socket.
5134 */
5135static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5136{
Neil Horman408f22e2007-06-16 14:03:45 -04005137 if (len < sizeof(struct sctp_initmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005139 len = sizeof(struct sctp_initmsg);
5140 if (put_user(len, optlen))
5141 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5143 return -EFAULT;
5144 return 0;
5145}
5146
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005148static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5149 char __user *optval, int __user *optlen)
5150{
5151 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005152 int cnt = 0;
5153 struct sctp_getaddrs getaddrs;
5154 struct sctp_transport *from;
5155 void __user *to;
5156 union sctp_addr temp;
5157 struct sctp_sock *sp = sctp_sk(sk);
5158 int addrlen;
5159 size_t space_left;
5160 int bytes_copied;
5161
5162 if (len < sizeof(struct sctp_getaddrs))
5163 return -EINVAL;
5164
5165 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5166 return -EFAULT;
5167
5168 /* For UDP-style sockets, id specifies the association to query. */
5169 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5170 if (!asoc)
5171 return -EINVAL;
5172
wangweidongcb3f8372013-12-23 12:16:50 +08005173 to = optval + offsetof(struct sctp_getaddrs, addrs);
5174 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005175
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07005176 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5177 transports) {
Al Virob3f5b3b2006-11-20 17:22:43 -08005178 memcpy(&temp, &from->ipaddr, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005179 addrlen = sctp_get_pf_specific(sk->sk_family)
5180 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005181 if (space_left < addrlen)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005182 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005183 if (copy_to_user(to, &temp, addrlen))
5184 return -EFAULT;
5185 to += addrlen;
5186 cnt++;
5187 space_left -= addrlen;
5188 }
5189
5190 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5191 return -EFAULT;
5192 bytes_copied = ((char __user *)to) - optval;
5193 if (put_user(bytes_copied, optlen))
5194 return -EFAULT;
5195
5196 return 0;
5197}
5198
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005199static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5200 size_t space_left, int *bytes_copied)
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005201{
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005202 struct sctp_sockaddr_entry *addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005203 union sctp_addr temp;
5204 int cnt = 0;
5205 int addrlen;
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005206 struct net *net = sock_net(sk);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005207
Vlad Yasevich29303542007-09-16 16:02:12 -07005208 rcu_read_lock();
Eric W. Biederman4db67e82012-08-06 08:42:04 +00005209 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
Vlad Yasevich29303542007-09-16 16:02:12 -07005210 if (!addr->valid)
5211 continue;
5212
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005213 if ((PF_INET == sk->sk_family) &&
Al Viro6244be42006-11-20 17:21:44 -08005214 (AF_INET6 == addr->a.sa.sa_family))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005215 continue;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07005216 if ((PF_INET6 == sk->sk_family) &&
5217 inet_v6_ipv6only(sk) &&
5218 (AF_INET == addr->a.sa.sa_family))
5219 continue;
Al Viro6244be42006-11-20 17:21:44 -08005220 memcpy(&temp, &addr->a, sizeof(temp));
Vlad Yasevichb46ae362008-01-28 14:25:36 -05005221 if (!temp.v4.sin_port)
5222 temp.v4.sin_port = htons(port);
5223
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005224 addrlen = sctp_get_pf_specific(sk->sk_family)
5225 ->addr_to_user(sctp_sk(sk), &temp);
5226
Vlad Yasevich29303542007-09-16 16:02:12 -07005227 if (space_left < addrlen) {
5228 cnt = -ENOMEM;
5229 break;
5230 }
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005231 memcpy(to, &temp, addrlen);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08005232
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005233 to += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005234 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005235 space_left -= addrlen;
Vlad Yasevich3663c302007-07-03 12:43:12 -04005236 *bytes_copied += addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005237 }
Vlad Yasevich29303542007-09-16 16:02:12 -07005238 rcu_read_unlock();
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005239
5240 return cnt;
5241}
5242
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005244static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5245 char __user *optval, int __user *optlen)
5246{
5247 struct sctp_bind_addr *bp;
5248 struct sctp_association *asoc;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005249 int cnt = 0;
5250 struct sctp_getaddrs getaddrs;
5251 struct sctp_sockaddr_entry *addr;
5252 void __user *to;
5253 union sctp_addr temp;
5254 struct sctp_sock *sp = sctp_sk(sk);
5255 int addrlen;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005256 int err = 0;
5257 size_t space_left;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005258 int bytes_copied = 0;
5259 void *addrs;
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005260 void *buf;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005261
Neil Horman408f22e2007-06-16 14:03:45 -04005262 if (len < sizeof(struct sctp_getaddrs))
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005263 return -EINVAL;
5264
5265 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5266 return -EFAULT;
5267
5268 /*
5269 * For UDP-style sockets, id specifies the association to query.
5270 * If the id field is set to the value '0' then the locally bound
5271 * addresses are returned without regard to any particular
5272 * association.
5273 */
5274 if (0 == getaddrs.assoc_id) {
5275 bp = &sctp_sk(sk)->ep->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005276 } else {
5277 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5278 if (!asoc)
5279 return -EINVAL;
5280 bp = &asoc->base.bind_addr;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005281 }
5282
wangweidongcb3f8372013-12-23 12:16:50 +08005283 to = optval + offsetof(struct sctp_getaddrs, addrs);
5284 space_left = len - offsetof(struct sctp_getaddrs, addrs);
Neil Horman186e2342007-06-18 19:59:16 -04005285
Marcelo Ricardo Leitnercacc0622015-11-30 14:32:54 -02005286 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005287 if (!addrs)
5288 return -ENOMEM;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005289
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005290 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5291 * addresses from the global local address list.
5292 */
5293 if (sctp_list_single_entry(&bp->address_list)) {
5294 addr = list_entry(bp->address_list.next,
5295 struct sctp_sockaddr_entry, list);
Vlad Yasevich52cae8f2008-08-18 10:34:34 -04005296 if (sctp_is_any(sk, &addr->a)) {
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005297 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5298 space_left, &bytes_copied);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005299 if (cnt < 0) {
5300 err = cnt;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005301 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005302 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005303 goto copy_getaddrs;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005304 }
5305 }
5306
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005307 buf = addrs;
Vlad Yasevich559cf712007-09-16 16:03:28 -07005308 /* Protection on the bound address list is not needed since
5309 * in the socket option context we hold a socket lock and
5310 * thus the bound address list can't change.
5311 */
5312 list_for_each_entry(addr, &bp->address_list, list) {
Al Viro6244be42006-11-20 17:21:44 -08005313 memcpy(&temp, &addr->a, sizeof(temp));
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005314 addrlen = sctp_get_pf_specific(sk->sk_family)
5315 ->addr_to_user(sp, &temp);
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005316 if (space_left < addrlen) {
5317 err = -ENOMEM; /*fixme: right error?*/
Vlad Yasevich559cf712007-09-16 16:03:28 -07005318 goto out;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005319 }
Vlad Yasevich70b57b82007-05-09 13:51:31 -07005320 memcpy(buf, &temp, addrlen);
5321 buf += addrlen;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005322 bytes_copied += addrlen;
wangweidongcb3f8372013-12-23 12:16:50 +08005323 cnt++;
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005324 space_left -= addrlen;
5325 }
5326
5327copy_getaddrs:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005328 if (copy_to_user(to, addrs, bytes_copied)) {
5329 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005330 goto out;
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005331 }
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005332 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
5333 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005334 goto out;
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005335 }
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005336 if (put_user(bytes_copied, optlen))
Vlad Yasevichfe979ac2007-05-23 11:11:37 -04005337 err = -EFAULT;
Sebastian Siewiord6f9fda2007-07-27 22:55:59 +02005338out:
Vlad Yasevichaad97f32007-04-28 21:09:04 -07005339 kfree(addrs);
Ivan Skytte Jørgensen5fe467e2005-10-06 21:36:17 -07005340 return err;
5341}
5342
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
5344 *
5345 * Requests that the local SCTP stack use the enclosed peer address as
5346 * the association primary. The enclosed address must be one of the
5347 * association peer's addresses.
5348 */
5349static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
5350 char __user *optval, int __user *optlen)
5351{
5352 struct sctp_prim prim;
5353 struct sctp_association *asoc;
5354 struct sctp_sock *sp = sctp_sk(sk);
5355
Neil Horman408f22e2007-06-16 14:03:45 -04005356 if (len < sizeof(struct sctp_prim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 return -EINVAL;
5358
Neil Horman408f22e2007-06-16 14:03:45 -04005359 len = sizeof(struct sctp_prim);
5360
5361 if (copy_from_user(&prim, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 return -EFAULT;
5363
5364 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
5365 if (!asoc)
5366 return -EINVAL;
5367
5368 if (!asoc->peer.primary_path)
5369 return -ENOTCONN;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005370
Al Viro8cec6b82006-11-20 17:23:01 -08005371 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
5372 asoc->peer.primary_path->af_specific->sockaddr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373
Jason Gunthorpe299ee122014-07-30 12:40:53 -06005374 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 (union sctp_addr *)&prim.ssp_addr);
5376
Neil Horman408f22e2007-06-16 14:03:45 -04005377 if (put_user(len, optlen))
5378 return -EFAULT;
5379 if (copy_to_user(optval, &prim, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 return -EFAULT;
5381
5382 return 0;
5383}
5384
5385/*
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005386 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 *
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005388 * Requests that the local endpoint set the specified Adaptation Layer
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 * Indication parameter for all future INIT and INIT-ACK exchanges.
5390 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005391static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 char __user *optval, int __user *optlen)
5393{
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005394 struct sctp_setadaptation adaptation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395
Neil Horman408f22e2007-06-16 14:03:45 -04005396 if (len < sizeof(struct sctp_setadaptation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 return -EINVAL;
5398
Neil Horman408f22e2007-06-16 14:03:45 -04005399 len = sizeof(struct sctp_setadaptation);
5400
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005401 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
Neil Horman408f22e2007-06-16 14:03:45 -04005402
5403 if (put_user(len, optlen))
5404 return -EFAULT;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08005405 if (copy_to_user(optval, &adaptation, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 return -EFAULT;
Ivan Skytte Jorgensena1ab3582005-10-28 15:33:24 -07005407
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 return 0;
5409}
5410
5411/*
5412 *
5413 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
5414 *
5415 * Applications that wish to use the sendto() system call may wish to
5416 * specify a default set of parameters that would normally be supplied
5417 * through the inclusion of ancillary data. This socket option allows
5418 * such an application to set the default sctp_sndrcvinfo structure.
5419
5420
5421 * The application that wishes to use this socket option simply passes
5422 * in to this call the sctp_sndrcvinfo structure defined in Section
5423 * 5.2.2) The input parameters accepted by this call include
5424 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
5425 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
5426 * to this call if the caller is using the UDP model.
5427 *
5428 * For getsockopt, it get the default sctp_sndrcvinfo structure.
5429 */
5430static int sctp_getsockopt_default_send_param(struct sock *sk,
5431 int len, char __user *optval,
5432 int __user *optlen)
5433{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 struct sctp_sock *sp = sctp_sk(sk);
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005435 struct sctp_association *asoc;
5436 struct sctp_sndrcvinfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005438 if (len < sizeof(info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 return -EINVAL;
Neil Horman408f22e2007-06-16 14:03:45 -04005440
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005441 len = sizeof(info);
Neil Horman408f22e2007-06-16 14:03:45 -04005442
5443 if (copy_from_user(&info, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 return -EFAULT;
5445
5446 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
5447 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
5448 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 if (asoc) {
5450 info.sinfo_stream = asoc->default_stream;
5451 info.sinfo_flags = asoc->default_flags;
5452 info.sinfo_ppid = asoc->default_ppid;
5453 info.sinfo_context = asoc->default_context;
5454 info.sinfo_timetolive = asoc->default_timetolive;
5455 } else {
5456 info.sinfo_stream = sp->default_stream;
5457 info.sinfo_flags = sp->default_flags;
5458 info.sinfo_ppid = sp->default_ppid;
5459 info.sinfo_context = sp->default_context;
5460 info.sinfo_timetolive = sp->default_timetolive;
5461 }
5462
Neil Horman408f22e2007-06-16 14:03:45 -04005463 if (put_user(len, optlen))
5464 return -EFAULT;
5465 if (copy_to_user(optval, &info, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 return -EFAULT;
5467
5468 return 0;
5469}
5470
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02005471/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
5472 * (SCTP_DEFAULT_SNDINFO)
5473 */
5474static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
5475 char __user *optval,
5476 int __user *optlen)
5477{
5478 struct sctp_sock *sp = sctp_sk(sk);
5479 struct sctp_association *asoc;
5480 struct sctp_sndinfo info;
5481
5482 if (len < sizeof(info))
5483 return -EINVAL;
5484
5485 len = sizeof(info);
5486
5487 if (copy_from_user(&info, optval, len))
5488 return -EFAULT;
5489
5490 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
5491 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
5492 return -EINVAL;
5493 if (asoc) {
5494 info.snd_sid = asoc->default_stream;
5495 info.snd_flags = asoc->default_flags;
5496 info.snd_ppid = asoc->default_ppid;
5497 info.snd_context = asoc->default_context;
5498 } else {
5499 info.snd_sid = sp->default_stream;
5500 info.snd_flags = sp->default_flags;
5501 info.snd_ppid = sp->default_ppid;
5502 info.snd_context = sp->default_context;
5503 }
5504
5505 if (put_user(len, optlen))
5506 return -EFAULT;
5507 if (copy_to_user(optval, &info, len))
5508 return -EFAULT;
5509
5510 return 0;
5511}
5512
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513/*
5514 *
5515 * 7.1.5 SCTP_NODELAY
5516 *
5517 * Turn on/off any Nagle-like algorithm. This means that packets are
5518 * generally sent as soon as possible and no unnecessary delays are
5519 * introduced, at the cost of more packets in the network. Expects an
5520 * integer boolean flag.
5521 */
5522
5523static int sctp_getsockopt_nodelay(struct sock *sk, int len,
5524 char __user *optval, int __user *optlen)
5525{
5526 int val;
5527
5528 if (len < sizeof(int))
5529 return -EINVAL;
5530
5531 len = sizeof(int);
5532 val = (sctp_sk(sk)->nodelay == 1);
5533 if (put_user(len, optlen))
5534 return -EFAULT;
5535 if (copy_to_user(optval, &val, len))
5536 return -EFAULT;
5537 return 0;
5538}
5539
5540/*
5541 *
5542 * 7.1.1 SCTP_RTOINFO
5543 *
5544 * The protocol parameters used to initialize and bound retransmission
5545 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
5546 * and modify these parameters.
5547 * All parameters are time values, in milliseconds. A value of 0, when
5548 * modifying the parameters, indicates that the current value should not
5549 * be changed.
5550 *
5551 */
5552static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
5553 char __user *optval,
5554 int __user *optlen) {
5555 struct sctp_rtoinfo rtoinfo;
5556 struct sctp_association *asoc;
5557
Neil Horman408f22e2007-06-16 14:03:45 -04005558 if (len < sizeof (struct sctp_rtoinfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 return -EINVAL;
5560
Neil Horman408f22e2007-06-16 14:03:45 -04005561 len = sizeof(struct sctp_rtoinfo);
5562
5563 if (copy_from_user(&rtoinfo, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 return -EFAULT;
5565
5566 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
5567
5568 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
5569 return -EINVAL;
5570
5571 /* Values corresponding to the specific association. */
5572 if (asoc) {
5573 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
5574 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
5575 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
5576 } else {
5577 /* Values corresponding to the endpoint. */
5578 struct sctp_sock *sp = sctp_sk(sk);
5579
5580 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
5581 rtoinfo.srto_max = sp->rtoinfo.srto_max;
5582 rtoinfo.srto_min = sp->rtoinfo.srto_min;
5583 }
5584
5585 if (put_user(len, optlen))
5586 return -EFAULT;
5587
5588 if (copy_to_user(optval, &rtoinfo, len))
5589 return -EFAULT;
5590
5591 return 0;
5592}
5593
5594/*
5595 *
5596 * 7.1.2 SCTP_ASSOCINFO
5597 *
Michael Opdenacker59c51592007-05-09 08:57:56 +02005598 * This option is used to tune the maximum retransmission attempts
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 * of the association.
5600 * Returns an error if the new association retransmission value is
5601 * greater than the sum of the retransmission value of the peer.
5602 * See [SCTP] for more information.
5603 *
5604 */
5605static int sctp_getsockopt_associnfo(struct sock *sk, int len,
5606 char __user *optval,
5607 int __user *optlen)
5608{
5609
5610 struct sctp_assocparams assocparams;
5611 struct sctp_association *asoc;
5612 struct list_head *pos;
5613 int cnt = 0;
5614
Neil Horman408f22e2007-06-16 14:03:45 -04005615 if (len < sizeof (struct sctp_assocparams))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 return -EINVAL;
5617
Neil Horman408f22e2007-06-16 14:03:45 -04005618 len = sizeof(struct sctp_assocparams);
5619
5620 if (copy_from_user(&assocparams, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 return -EFAULT;
5622
5623 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
5624
5625 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
5626 return -EINVAL;
5627
5628 /* Values correspoinding to the specific association */
Vladislav Yasevich17337212005-04-28 11:57:54 -07005629 if (asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
5631 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
5632 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
Daniel Borkmann52db8822013-06-25 18:17:27 +02005633 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634
5635 list_for_each(pos, &asoc->peer.transport_addr_list) {
wangweidongcb3f8372013-12-23 12:16:50 +08005636 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 }
5638
5639 assocparams.sasoc_number_peer_destinations = cnt;
5640 } else {
5641 /* Values corresponding to the endpoint */
5642 struct sctp_sock *sp = sctp_sk(sk);
5643
5644 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
5645 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
5646 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
5647 assocparams.sasoc_cookie_life =
5648 sp->assocparams.sasoc_cookie_life;
5649 assocparams.sasoc_number_peer_destinations =
5650 sp->assocparams.
5651 sasoc_number_peer_destinations;
5652 }
5653
5654 if (put_user(len, optlen))
5655 return -EFAULT;
5656
5657 if (copy_to_user(optval, &assocparams, len))
5658 return -EFAULT;
5659
5660 return 0;
5661}
5662
5663/*
5664 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
5665 *
5666 * This socket option is a boolean flag which turns on or off mapped V4
5667 * addresses. If this option is turned on and the socket is type
5668 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
5669 * If this option is turned off, then no mapping will be done of V4
5670 * addresses and a user will receive both PF_INET6 and PF_INET type
5671 * addresses on the socket.
5672 */
5673static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
5674 char __user *optval, int __user *optlen)
5675{
5676 int val;
5677 struct sctp_sock *sp = sctp_sk(sk);
5678
5679 if (len < sizeof(int))
5680 return -EINVAL;
5681
5682 len = sizeof(int);
5683 val = sp->v4mapped;
5684 if (put_user(len, optlen))
5685 return -EFAULT;
5686 if (copy_to_user(optval, &val, len))
5687 return -EFAULT;
5688
5689 return 0;
5690}
5691
5692/*
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005693 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
5694 * (chapter and verse is quoted at sctp_setsockopt_context())
5695 */
5696static int sctp_getsockopt_context(struct sock *sk, int len,
5697 char __user *optval, int __user *optlen)
5698{
5699 struct sctp_assoc_value params;
5700 struct sctp_sock *sp;
5701 struct sctp_association *asoc;
5702
Neil Horman408f22e2007-06-16 14:03:45 -04005703 if (len < sizeof(struct sctp_assoc_value))
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005704 return -EINVAL;
5705
Neil Horman408f22e2007-06-16 14:03:45 -04005706 len = sizeof(struct sctp_assoc_value);
5707
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08005708 if (copy_from_user(&params, optval, len))
5709 return -EFAULT;
5710
5711 sp = sctp_sk(sk);
5712
5713 if (params.assoc_id != 0) {
5714 asoc = sctp_id2assoc(sk, params.assoc_id);
5715 if (!asoc)
5716 return -EINVAL;
5717 params.assoc_value = asoc->default_rcv_context;
5718 } else {
5719 params.assoc_value = sp->default_rcv_context;
5720 }
5721
5722 if (put_user(len, optlen))
5723 return -EFAULT;
5724 if (copy_to_user(optval, &params, len))
5725 return -EFAULT;
5726
5727 return 0;
5728}
5729
5730/*
Wei Yongjune89c2092008-12-25 16:54:58 -08005731 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
5732 * This option will get or set the maximum size to put in any outgoing
5733 * SCTP DATA chunk. If a message is larger than this size it will be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 * fragmented by SCTP into the specified size. Note that the underlying
5735 * SCTP implementation may fragment into smaller sized chunks when the
5736 * PMTU of the underlying association is smaller than the value set by
Wei Yongjune89c2092008-12-25 16:54:58 -08005737 * the user. The default value for this option is '0' which indicates
5738 * the user is NOT limiting fragmentation and only the PMTU will effect
5739 * SCTP's choice of DATA chunk size. Note also that values set larger
5740 * than the maximum size of an IP datagram will effectively let SCTP
5741 * control fragmentation (i.e. the same as setting this option to 0).
5742 *
5743 * The following structure is used to access and modify this parameter:
5744 *
5745 * struct sctp_assoc_value {
5746 * sctp_assoc_t assoc_id;
5747 * uint32_t assoc_value;
5748 * };
5749 *
5750 * assoc_id: This parameter is ignored for one-to-one style sockets.
5751 * For one-to-many style sockets this parameter indicates which
5752 * association the user is performing an action upon. Note that if
5753 * this field's value is zero then the endpoints default value is
5754 * changed (effecting future associations only).
5755 * assoc_value: This parameter specifies the maximum size in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 */
5757static int sctp_getsockopt_maxseg(struct sock *sk, int len,
5758 char __user *optval, int __user *optlen)
5759{
Wei Yongjune89c2092008-12-25 16:54:58 -08005760 struct sctp_assoc_value params;
5761 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
Wei Yongjune89c2092008-12-25 16:54:58 -08005763 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05005764 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005765 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005766 "Use of int in maxseg socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005767 "Use struct sctp_assoc_value instead\n",
5768 current->comm, task_pid_nr(current));
Wei Yongjune89c2092008-12-25 16:54:58 -08005769 params.assoc_id = 0;
5770 } else if (len >= sizeof(struct sctp_assoc_value)) {
5771 len = sizeof(struct sctp_assoc_value);
5772 if (copy_from_user(&params, optval, sizeof(params)))
5773 return -EFAULT;
5774 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 return -EINVAL;
5776
Wei Yongjune89c2092008-12-25 16:54:58 -08005777 asoc = sctp_id2assoc(sk, params.assoc_id);
5778 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
5779 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780
Wei Yongjune89c2092008-12-25 16:54:58 -08005781 if (asoc)
5782 params.assoc_value = asoc->frag_point;
5783 else
5784 params.assoc_value = sctp_sk(sk)->user_frag;
5785
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 if (put_user(len, optlen))
5787 return -EFAULT;
Wei Yongjune89c2092008-12-25 16:54:58 -08005788 if (len == sizeof(int)) {
5789 if (copy_to_user(optval, &params.assoc_value, len))
5790 return -EFAULT;
5791 } else {
5792 if (copy_to_user(optval, &params, len))
5793 return -EFAULT;
5794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795
5796 return 0;
5797}
5798
Vlad Yasevichb6e13312007-04-20 12:23:15 -07005799/*
5800 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
5801 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
5802 */
5803static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
5804 char __user *optval, int __user *optlen)
5805{
5806 int val;
5807
5808 if (len < sizeof(int))
5809 return -EINVAL;
5810
5811 len = sizeof(int);
5812
5813 val = sctp_sk(sk)->frag_interleave;
5814 if (put_user(len, optlen))
5815 return -EFAULT;
5816 if (copy_to_user(optval, &val, len))
5817 return -EFAULT;
5818
5819 return 0;
5820}
5821
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005822/*
5823 * 7.1.25. Set or Get the sctp partial delivery point
5824 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
5825 */
5826static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
5827 char __user *optval,
5828 int __user *optlen)
5829{
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09005830 u32 val;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005831
5832 if (len < sizeof(u32))
5833 return -EINVAL;
5834
5835 len = sizeof(u32);
5836
5837 val = sctp_sk(sk)->pd_point;
5838 if (put_user(len, optlen))
5839 return -EFAULT;
5840 if (copy_to_user(optval, &val, len))
5841 return -EFAULT;
5842
Wei Yongjun7d743b72010-12-14 16:10:41 +00005843 return 0;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07005844}
5845
Vlad Yasevich70331572007-03-23 11:34:36 -07005846/*
5847 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
5848 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
5849 */
5850static int sctp_getsockopt_maxburst(struct sock *sk, int len,
5851 char __user *optval,
5852 int __user *optlen)
5853{
Neil Horman219b99a2008-03-05 13:44:46 -08005854 struct sctp_assoc_value params;
5855 struct sctp_sock *sp;
5856 struct sctp_association *asoc;
Vlad Yasevich70331572007-03-23 11:34:36 -07005857
Neil Horman219b99a2008-03-05 13:44:46 -08005858 if (len == sizeof(int)) {
Neil Horman94f65192013-12-23 08:29:43 -05005859 pr_warn_ratelimited(DEPRECATED
Neil Hormanf916ec92014-01-02 12:54:27 -05005860 "%s (pid %d) "
Neil Horman94f65192013-12-23 08:29:43 -05005861 "Use of int in max_burst socket option.\n"
Neil Hormanf916ec92014-01-02 12:54:27 -05005862 "Use struct sctp_assoc_value instead\n",
5863 current->comm, task_pid_nr(current));
Neil Horman219b99a2008-03-05 13:44:46 -08005864 params.assoc_id = 0;
Wei Yongjunc6db93a2009-03-02 09:46:12 +00005865 } else if (len >= sizeof(struct sctp_assoc_value)) {
5866 len = sizeof(struct sctp_assoc_value);
Neil Horman219b99a2008-03-05 13:44:46 -08005867 if (copy_from_user(&params, optval, len))
5868 return -EFAULT;
5869 } else
5870 return -EINVAL;
Vlad Yasevich70331572007-03-23 11:34:36 -07005871
Neil Horman219b99a2008-03-05 13:44:46 -08005872 sp = sctp_sk(sk);
Vlad Yasevich70331572007-03-23 11:34:36 -07005873
Neil Horman219b99a2008-03-05 13:44:46 -08005874 if (params.assoc_id != 0) {
5875 asoc = sctp_id2assoc(sk, params.assoc_id);
5876 if (!asoc)
5877 return -EINVAL;
5878 params.assoc_value = asoc->max_burst;
5879 } else
5880 params.assoc_value = sp->max_burst;
5881
5882 if (len == sizeof(int)) {
5883 if (copy_to_user(optval, &params.assoc_value, len))
5884 return -EFAULT;
5885 } else {
5886 if (copy_to_user(optval, &params, len))
5887 return -EFAULT;
5888 }
5889
5890 return 0;
5891
Vlad Yasevich70331572007-03-23 11:34:36 -07005892}
5893
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005894static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
5895 char __user *optval, int __user *optlen)
5896{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005897 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005898 struct sctp_hmacalgo __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005899 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005900 __u16 data_len = 0;
5901 u32 num_idents;
Xin Long7a84bd42016-02-03 23:33:30 +08005902 int i;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005903
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005904 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005905 return -EACCES;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005906
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005907 hmacs = ep->auth_hmacs_list;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005908 data_len = ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005909
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005910 if (len < sizeof(struct sctp_hmacalgo) + data_len)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005911 return -EINVAL;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005912
5913 len = sizeof(struct sctp_hmacalgo) + data_len;
5914 num_idents = data_len / sizeof(u16);
5915
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005916 if (put_user(len, optlen))
5917 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005918 if (put_user(num_idents, &p->shmac_num_idents))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005919 return -EFAULT;
Xin Long7a84bd42016-02-03 23:33:30 +08005920 for (i = 0; i < num_idents; i++) {
5921 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
5922
5923 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
5924 return -EFAULT;
5925 }
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005926 return 0;
5927}
5928
5929static int sctp_getsockopt_active_key(struct sock *sk, int len,
5930 char __user *optval, int __user *optlen)
5931{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005932 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005933 struct sctp_authkeyid val;
5934 struct sctp_association *asoc;
5935
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005936 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005937 return -EACCES;
5938
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005939 if (len < sizeof(struct sctp_authkeyid))
5940 return -EINVAL;
5941 if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid)))
5942 return -EFAULT;
5943
5944 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
5945 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
5946 return -EINVAL;
5947
5948 if (asoc)
5949 val.scact_keynumber = asoc->active_key_id;
5950 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005951 val.scact_keynumber = ep->active_key_id;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005952
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005953 len = sizeof(struct sctp_authkeyid);
5954 if (put_user(len, optlen))
5955 return -EFAULT;
5956 if (copy_to_user(optval, &val, len))
5957 return -EFAULT;
5958
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005959 return 0;
5960}
5961
5962static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
5963 char __user *optval, int __user *optlen)
5964{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005965 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01005966 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005967 struct sctp_authchunks val;
5968 struct sctp_association *asoc;
5969 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005970 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005971 char __user *to;
5972
Vlad Yasevichb14878c2014-04-17 17:26:50 +02005973 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005974 return -EACCES;
5975
5976 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005977 return -EINVAL;
5978
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005979 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005980 return -EFAULT;
5981
Al Viro411223c2007-10-14 19:21:20 +01005982 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005983 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
5984 if (!asoc)
5985 return -EINVAL;
5986
5987 ch = asoc->peer.peer_chunks;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005988 if (!ch)
5989 goto num;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005990
5991 /* See if the user provided enough room for all the data */
Vlad Yasevichb40db682008-02-27 14:40:37 -05005992 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
5993 if (len < num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005994 return -EINVAL;
5995
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005996 if (copy_to_user(to, ch->chunks, num_chunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07005997 return -EFAULT;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07005998num:
5999 len = sizeof(struct sctp_authchunks) + num_chunks;
wangweidong8d726512013-12-23 12:16:53 +08006000 if (put_user(len, optlen))
6001 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006002 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6003 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006004 return 0;
6005}
6006
6007static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6008 char __user *optval, int __user *optlen)
6009{
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006010 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
Al Viro411223c2007-10-14 19:21:20 +01006011 struct sctp_authchunks __user *p = (void __user *)optval;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006012 struct sctp_authchunks val;
6013 struct sctp_association *asoc;
6014 struct sctp_chunks_param *ch;
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006015 u32 num_chunks = 0;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006016 char __user *to;
6017
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006018 if (!ep->auth_enable)
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006019 return -EACCES;
6020
6021 if (len < sizeof(struct sctp_authchunks))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006022 return -EINVAL;
6023
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006024 if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006025 return -EFAULT;
6026
Al Viro411223c2007-10-14 19:21:20 +01006027 to = p->gauth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006028 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6029 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6030 return -EINVAL;
6031
6032 if (asoc)
wangweidong26ac8e52013-12-23 12:16:51 +08006033 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006034 else
Vlad Yasevichb14878c2014-04-17 17:26:50 +02006035 ch = ep->auth_chunk_list;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006036
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006037 if (!ch)
6038 goto num;
6039
Vlad Yasevichb40db682008-02-27 14:40:37 -05006040 num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t);
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006041 if (len < sizeof(struct sctp_authchunks) + num_chunks)
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006042 return -EINVAL;
6043
Vlad Yasevich5e739d12008-08-21 03:34:25 -07006044 if (copy_to_user(to, ch->chunks, num_chunks))
6045 return -EFAULT;
6046num:
6047 len = sizeof(struct sctp_authchunks) + num_chunks;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006048 if (put_user(len, optlen))
6049 return -EFAULT;
Vlad Yasevich7e8616d2008-02-27 16:04:52 -05006050 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6051 return -EFAULT;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006052
6053 return 0;
6054}
6055
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08006056/*
6057 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6058 * This option gets the current number of associations that are attached
6059 * to a one-to-many style socket. The option value is an uint32_t.
6060 */
6061static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6062 char __user *optval, int __user *optlen)
6063{
6064 struct sctp_sock *sp = sctp_sk(sk);
6065 struct sctp_association *asoc;
6066 u32 val = 0;
6067
6068 if (sctp_style(sk, TCP))
6069 return -EOPNOTSUPP;
6070
6071 if (len < sizeof(u32))
6072 return -EINVAL;
6073
6074 len = sizeof(u32);
6075
6076 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6077 val++;
6078 }
6079
6080 if (put_user(len, optlen))
6081 return -EFAULT;
6082 if (copy_to_user(optval, &val, len))
6083 return -EFAULT;
6084
6085 return 0;
6086}
6087
Wei Yongjun209ba422011-04-17 17:27:08 +00006088/*
Michio Honda7dc04d72011-04-26 20:16:31 +09006089 * 8.1.23 SCTP_AUTO_ASCONF
6090 * See the corresponding setsockopt entry as description
6091 */
6092static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6093 char __user *optval, int __user *optlen)
6094{
6095 int val = 0;
6096
6097 if (len < sizeof(int))
6098 return -EINVAL;
6099
6100 len = sizeof(int);
6101 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6102 val = 1;
6103 if (put_user(len, optlen))
6104 return -EFAULT;
6105 if (copy_to_user(optval, &val, len))
6106 return -EFAULT;
6107 return 0;
6108}
6109
6110/*
Wei Yongjun209ba422011-04-17 17:27:08 +00006111 * 8.2.6. Get the Current Identifiers of Associations
6112 * (SCTP_GET_ASSOC_ID_LIST)
6113 *
6114 * This option gets the current list of SCTP association identifiers of
6115 * the SCTP associations handled by a one-to-many style socket.
6116 */
6117static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6118 char __user *optval, int __user *optlen)
6119{
6120 struct sctp_sock *sp = sctp_sk(sk);
6121 struct sctp_association *asoc;
6122 struct sctp_assoc_ids *ids;
6123 u32 num = 0;
6124
6125 if (sctp_style(sk, TCP))
6126 return -EOPNOTSUPP;
6127
6128 if (len < sizeof(struct sctp_assoc_ids))
6129 return -EINVAL;
6130
6131 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6132 num++;
6133 }
6134
6135 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6136 return -EINVAL;
6137
6138 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6139
Marcelo Ricardo Leitner9ba0b962015-12-23 16:28:40 -02006140 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
Wei Yongjun209ba422011-04-17 17:27:08 +00006141 if (unlikely(!ids))
6142 return -ENOMEM;
6143
6144 ids->gaids_number_of_ids = num;
6145 num = 0;
6146 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6147 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6148 }
6149
6150 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6151 kfree(ids);
6152 return -EFAULT;
6153 }
6154
6155 kfree(ids);
6156 return 0;
6157}
6158
Neil Horman5aa93bc2012-07-21 07:56:07 +00006159/*
6160 * SCTP_PEER_ADDR_THLDS
6161 *
6162 * This option allows us to fetch the partially failed threshold for one or all
6163 * transports in an association. See Section 6.1 of:
6164 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6165 */
6166static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6167 char __user *optval,
6168 int len,
6169 int __user *optlen)
6170{
6171 struct sctp_paddrthlds val;
6172 struct sctp_transport *trans;
6173 struct sctp_association *asoc;
6174
6175 if (len < sizeof(struct sctp_paddrthlds))
6176 return -EINVAL;
6177 len = sizeof(struct sctp_paddrthlds);
6178 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6179 return -EFAULT;
6180
6181 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6182 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6183 if (!asoc)
6184 return -ENOENT;
6185
6186 val.spt_pathpfthld = asoc->pf_retrans;
6187 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6188 } else {
6189 trans = sctp_addr_id2transport(sk, &val.spt_address,
6190 val.spt_assoc_id);
6191 if (!trans)
6192 return -ENOENT;
6193
6194 val.spt_pathmaxrxt = trans->pathmaxrxt;
6195 val.spt_pathpfthld = trans->pf_retrans;
6196 }
6197
6198 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6199 return -EFAULT;
6200
6201 return 0;
6202}
6203
Michele Baldessari196d6752012-12-01 04:49:42 +00006204/*
6205 * SCTP_GET_ASSOC_STATS
6206 *
6207 * This option retrieves local per endpoint statistics. It is modeled
6208 * after OpenSolaris' implementation
6209 */
6210static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6211 char __user *optval,
6212 int __user *optlen)
6213{
6214 struct sctp_assoc_stats sas;
6215 struct sctp_association *asoc = NULL;
6216
6217 /* User must provide at least the assoc id */
6218 if (len < sizeof(sctp_assoc_t))
6219 return -EINVAL;
6220
Guenter Roeck726bc6b2013-02-27 10:57:31 +00006221 /* Allow the struct to grow and fill in as much as possible */
6222 len = min_t(size_t, len, sizeof(sas));
6223
Michele Baldessari196d6752012-12-01 04:49:42 +00006224 if (copy_from_user(&sas, optval, len))
6225 return -EFAULT;
6226
6227 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6228 if (!asoc)
6229 return -EINVAL;
6230
6231 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6232 sas.sas_gapcnt = asoc->stats.gapcnt;
6233 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6234 sas.sas_osacks = asoc->stats.osacks;
6235 sas.sas_isacks = asoc->stats.isacks;
6236 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6237 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6238 sas.sas_oodchunks = asoc->stats.oodchunks;
6239 sas.sas_iodchunks = asoc->stats.iodchunks;
6240 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6241 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6242 sas.sas_idupchunks = asoc->stats.idupchunks;
6243 sas.sas_opackets = asoc->stats.opackets;
6244 sas.sas_ipackets = asoc->stats.ipackets;
6245
6246 /* New high max rto observed, will return 0 if not a single
6247 * RTO update took place. obs_rto_ipaddr will be bogus
6248 * in such a case
6249 */
6250 sas.sas_maxrto = asoc->stats.max_obs_rto;
6251 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6252 sizeof(struct sockaddr_storage));
6253
6254 /* Mark beginning of a new observation period */
6255 asoc->stats.max_obs_rto = asoc->rto_min;
6256
Michele Baldessari196d6752012-12-01 04:49:42 +00006257 if (put_user(len, optlen))
6258 return -EFAULT;
6259
Daniel Borkmannbb333812013-06-28 19:49:40 +02006260 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
Michele Baldessari196d6752012-12-01 04:49:42 +00006261
6262 if (copy_to_user(optval, &sas, len))
6263 return -EFAULT;
6264
6265 return 0;
6266}
6267
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02006268static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6269 char __user *optval,
6270 int __user *optlen)
6271{
6272 int val = 0;
6273
6274 if (len < sizeof(int))
6275 return -EINVAL;
6276
6277 len = sizeof(int);
6278 if (sctp_sk(sk)->recvrcvinfo)
6279 val = 1;
6280 if (put_user(len, optlen))
6281 return -EFAULT;
6282 if (copy_to_user(optval, &val, len))
6283 return -EFAULT;
6284
6285 return 0;
6286}
6287
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02006288static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6289 char __user *optval,
6290 int __user *optlen)
6291{
6292 int val = 0;
6293
6294 if (len < sizeof(int))
6295 return -EINVAL;
6296
6297 len = sizeof(int);
6298 if (sctp_sk(sk)->recvnxtinfo)
6299 val = 1;
6300 if (put_user(len, optlen))
6301 return -EFAULT;
6302 if (copy_to_user(optval, &val, len))
6303 return -EFAULT;
6304
6305 return 0;
6306}
6307
Xin Long28aa4c22016-07-09 19:47:40 +08006308static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6309 char __user *optval,
6310 int __user *optlen)
6311{
6312 struct sctp_assoc_value params;
6313 struct sctp_association *asoc;
6314 int retval = -EFAULT;
6315
6316 if (len < sizeof(params)) {
6317 retval = -EINVAL;
6318 goto out;
6319 }
6320
6321 len = sizeof(params);
6322 if (copy_from_user(&params, optval, len))
6323 goto out;
6324
6325 asoc = sctp_id2assoc(sk, params.assoc_id);
6326 if (asoc) {
6327 params.assoc_value = asoc->prsctp_enable;
6328 } else if (!params.assoc_id) {
6329 struct sctp_sock *sp = sctp_sk(sk);
6330
6331 params.assoc_value = sp->ep->prsctp_enable;
6332 } else {
6333 retval = -EINVAL;
6334 goto out;
6335 }
6336
6337 if (put_user(len, optlen))
6338 goto out;
6339
6340 if (copy_to_user(optval, &params, len))
6341 goto out;
6342
6343 retval = 0;
6344
6345out:
6346 return retval;
6347}
6348
Xin Longf959fb42016-07-09 19:47:41 +08006349static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
6350 char __user *optval,
6351 int __user *optlen)
6352{
6353 struct sctp_default_prinfo info;
6354 struct sctp_association *asoc;
6355 int retval = -EFAULT;
6356
6357 if (len < sizeof(info)) {
6358 retval = -EINVAL;
6359 goto out;
6360 }
6361
6362 len = sizeof(info);
6363 if (copy_from_user(&info, optval, len))
6364 goto out;
6365
6366 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
6367 if (asoc) {
6368 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
6369 info.pr_value = asoc->default_timetolive;
6370 } else if (!info.pr_assoc_id) {
6371 struct sctp_sock *sp = sctp_sk(sk);
6372
6373 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
6374 info.pr_value = sp->default_timetolive;
6375 } else {
6376 retval = -EINVAL;
6377 goto out;
6378 }
6379
6380 if (put_user(len, optlen))
6381 goto out;
6382
6383 if (copy_to_user(optval, &info, len))
6384 goto out;
6385
6386 retval = 0;
6387
6388out:
6389 return retval;
6390}
6391
Xin Long826d2532016-07-09 19:47:42 +08006392static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
6393 char __user *optval,
6394 int __user *optlen)
6395{
6396 struct sctp_prstatus params;
6397 struct sctp_association *asoc;
6398 int policy;
6399 int retval = -EINVAL;
6400
6401 if (len < sizeof(params))
6402 goto out;
6403
6404 len = sizeof(params);
6405 if (copy_from_user(&params, optval, len)) {
6406 retval = -EFAULT;
6407 goto out;
6408 }
6409
6410 policy = params.sprstat_policy;
6411 if (policy & ~SCTP_PR_SCTP_MASK)
6412 goto out;
6413
6414 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
6415 if (!asoc)
6416 goto out;
6417
6418 if (policy == SCTP_PR_SCTP_NONE) {
6419 params.sprstat_abandoned_unsent = 0;
6420 params.sprstat_abandoned_sent = 0;
6421 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
6422 params.sprstat_abandoned_unsent +=
6423 asoc->abandoned_unsent[policy];
6424 params.sprstat_abandoned_sent +=
6425 asoc->abandoned_sent[policy];
6426 }
6427 } else {
6428 params.sprstat_abandoned_unsent =
6429 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
6430 params.sprstat_abandoned_sent =
6431 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
6432 }
6433
6434 if (put_user(len, optlen)) {
6435 retval = -EFAULT;
6436 goto out;
6437 }
6438
6439 if (copy_to_user(optval, &params, len)) {
6440 retval = -EFAULT;
6441 goto out;
6442 }
6443
6444 retval = 0;
6445
6446out:
6447 return retval;
6448}
6449
Daniel Borkmanndda91922013-06-17 11:40:05 +02006450static int sctp_getsockopt(struct sock *sk, int level, int optname,
6451 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452{
6453 int retval = 0;
6454 int len;
6455
Daniel Borkmannbb333812013-06-28 19:49:40 +02006456 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457
6458 /* I can hardly begin to describe how wrong this is. This is
6459 * so broken as to be worse than useless. The API draft
6460 * REALLY is NOT helpful here... I am not convinced that the
6461 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
6462 * are at all well-founded.
6463 */
6464 if (level != SOL_SCTP) {
6465 struct sctp_af *af = sctp_sk(sk)->pf->af;
6466
6467 retval = af->getsockopt(sk, level, optname, optval, optlen);
6468 return retval;
6469 }
6470
6471 if (get_user(len, optlen))
6472 return -EFAULT;
6473
Jiri Slabya4b8e712016-10-21 14:13:24 +02006474 if (len < 0)
6475 return -EINVAL;
6476
wangweidong048ed4b2014-01-21 15:44:11 +08006477 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478
6479 switch (optname) {
6480 case SCTP_STATUS:
6481 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
6482 break;
6483 case SCTP_DISABLE_FRAGMENTS:
6484 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
6485 optlen);
6486 break;
6487 case SCTP_EVENTS:
6488 retval = sctp_getsockopt_events(sk, len, optval, optlen);
6489 break;
6490 case SCTP_AUTOCLOSE:
6491 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
6492 break;
6493 case SCTP_SOCKOPT_PEELOFF:
6494 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
6495 break;
6496 case SCTP_PEER_ADDR_PARAMS:
6497 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
6498 optlen);
6499 break;
Shan Wei4580ccc2011-01-18 22:39:00 +00006500 case SCTP_DELAYED_SACK:
Wei Yongjund364d922008-05-09 15:13:26 -07006501 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
Frank Filz77086102005-12-22 11:37:30 -08006502 optlen);
6503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 case SCTP_INITMSG:
6505 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
6506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 case SCTP_GET_PEER_ADDRS:
6508 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
6509 optlen);
6510 break;
6511 case SCTP_GET_LOCAL_ADDRS:
6512 retval = sctp_getsockopt_local_addrs(sk, len, optval,
6513 optlen);
6514 break;
Vlad Yasevichc6ba68a2009-06-01 12:41:15 -04006515 case SCTP_SOCKOPT_CONNECTX3:
6516 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
6517 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 case SCTP_DEFAULT_SEND_PARAM:
6519 retval = sctp_getsockopt_default_send_param(sk, len,
6520 optval, optlen);
6521 break;
Geir Ola Vaagland6b3fd5f2014-07-12 20:30:39 +02006522 case SCTP_DEFAULT_SNDINFO:
6523 retval = sctp_getsockopt_default_sndinfo(sk, len,
6524 optval, optlen);
6525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526 case SCTP_PRIMARY_ADDR:
6527 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
6528 break;
6529 case SCTP_NODELAY:
6530 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
6531 break;
6532 case SCTP_RTOINFO:
6533 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
6534 break;
6535 case SCTP_ASSOCINFO:
6536 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
6537 break;
6538 case SCTP_I_WANT_MAPPED_V4_ADDR:
6539 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
6540 break;
6541 case SCTP_MAXSEG:
6542 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
6543 break;
6544 case SCTP_GET_PEER_ADDR_INFO:
6545 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
6546 optlen);
6547 break;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08006548 case SCTP_ADAPTATION_LAYER:
6549 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 optlen);
6551 break;
Ivan Skytte Jorgensen6ab792f2006-12-13 16:34:22 -08006552 case SCTP_CONTEXT:
6553 retval = sctp_getsockopt_context(sk, len, optval, optlen);
6554 break;
Vlad Yasevichb6e13312007-04-20 12:23:15 -07006555 case SCTP_FRAGMENT_INTERLEAVE:
6556 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
6557 optlen);
6558 break;
Vlad Yasevichd49d91d2007-03-23 11:32:00 -07006559 case SCTP_PARTIAL_DELIVERY_POINT:
6560 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
6561 optlen);
6562 break;
Vlad Yasevich70331572007-03-23 11:34:36 -07006563 case SCTP_MAX_BURST:
6564 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
6565 break;
Vlad Yasevich65b07e52007-09-16 19:34:00 -07006566 case SCTP_AUTH_KEY:
6567 case SCTP_AUTH_CHUNK:
6568 case SCTP_AUTH_DELETE_KEY:
6569 retval = -EOPNOTSUPP;
6570 break;
6571 case SCTP_HMAC_IDENT:
6572 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
6573 break;
6574 case SCTP_AUTH_ACTIVE_KEY:
6575 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
6576 break;
6577 case SCTP_PEER_AUTH_CHUNKS:
6578 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
6579 optlen);
6580 break;
6581 case SCTP_LOCAL_AUTH_CHUNKS:
6582 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
6583 optlen);
6584 break;
Wei Yongjunaea3c5c2008-12-25 16:57:24 -08006585 case SCTP_GET_ASSOC_NUMBER:
6586 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
6587 break;
Wei Yongjun209ba422011-04-17 17:27:08 +00006588 case SCTP_GET_ASSOC_ID_LIST:
6589 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
6590 break;
Michio Honda7dc04d72011-04-26 20:16:31 +09006591 case SCTP_AUTO_ASCONF:
6592 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
6593 break;
Neil Horman5aa93bc2012-07-21 07:56:07 +00006594 case SCTP_PEER_ADDR_THLDS:
6595 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
6596 break;
Michele Baldessari196d6752012-12-01 04:49:42 +00006597 case SCTP_GET_ASSOC_STATS:
6598 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
6599 break;
Geir Ola Vaagland0d3a4212014-07-12 20:30:37 +02006600 case SCTP_RECVRCVINFO:
6601 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
6602 break;
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02006603 case SCTP_RECVNXTINFO:
6604 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
6605 break;
Xin Long28aa4c22016-07-09 19:47:40 +08006606 case SCTP_PR_SUPPORTED:
6607 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
6608 break;
Xin Longf959fb42016-07-09 19:47:41 +08006609 case SCTP_DEFAULT_PRINFO:
6610 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
6611 optlen);
6612 break;
Xin Long826d2532016-07-09 19:47:42 +08006613 case SCTP_PR_ASSOC_STATUS:
6614 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
6615 optlen);
6616 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617 default:
6618 retval = -ENOPROTOOPT;
6619 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07006620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621
wangweidong048ed4b2014-01-21 15:44:11 +08006622 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 return retval;
6624}
6625
Craig Gallek086c6532016-02-10 11:50:35 -05006626static int sctp_hash(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627{
6628 /* STUB */
Craig Gallek086c6532016-02-10 11:50:35 -05006629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630}
6631
6632static void sctp_unhash(struct sock *sk)
6633{
6634 /* STUB */
6635}
6636
6637/* Check if port is acceptable. Possibly find first available port.
6638 *
6639 * The port hash table (contained in the 'global' SCTP protocol storage
6640 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
6641 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
6642 * list (the list number is the port number hashed out, so as you
6643 * would expect from a hash function, all the ports in a given list have
6644 * such a number that hashes out to the same list number; you were
6645 * expecting that, right?); so each list has a set of ports, with a
6646 * link to the socket (struct sock) that uses it, the port number and
6647 * a fastreuse flag (FIXME: NPI ipg).
6648 */
6649static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006650 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651
6652static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
6653{
6654 struct sctp_bind_hashbucket *head; /* hash list */
Sasha Levinb67bfe02013-02-27 17:06:00 -08006655 struct sctp_bind_bucket *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 unsigned short snum;
6657 int ret;
6658
Al Viro04afd8b2006-11-20 17:02:01 -08006659 snum = ntohs(addr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660
Daniel Borkmannbb333812013-06-28 19:49:40 +02006661 pr_debug("%s: begins, snum:%d\n", __func__, snum);
6662
wangweidong79b91132014-01-21 15:44:07 +08006663 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664
6665 if (snum == 0) {
Stephen Hemminger06393002007-10-10 17:30:18 -07006666 /* Search for an available port. */
Stephen Hemminger227b60f2007-10-10 17:30:46 -07006667 int low, high, remaining, index;
6668 unsigned int rover;
WANG Cong122ff242014-05-12 16:04:53 -07006669 struct net *net = sock_net(sk);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07006670
WANG Cong122ff242014-05-12 16:04:53 -07006671 inet_get_local_port_range(net, &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07006672 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05006673 rover = prandom_u32() % remaining + low;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 do {
6676 rover++;
6677 if ((rover < low) || (rover > high))
6678 rover = low;
WANG Cong122ff242014-05-12 16:04:53 -07006679 if (inet_is_local_reserved_port(net, rover))
Amerigo Wange3826f12010-05-05 00:27:06 +00006680 continue;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006681 index = sctp_phashfn(sock_net(sk), rover);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 head = &sctp_port_hashtable[index];
wangweidong3c8e43b2014-01-21 15:44:08 +08006683 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08006684 sctp_for_each_hentry(pp, &head->chain)
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006685 if ((pp->port == rover) &&
6686 net_eq(sock_net(sk), pp->net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687 goto next;
6688 break;
6689 next:
wangweidong3c8e43b2014-01-21 15:44:08 +08006690 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 } while (--remaining > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692
6693 /* Exhausted local port range during search? */
6694 ret = 1;
6695 if (remaining <= 0)
6696 goto fail;
6697
6698 /* OK, here is the one we will use. HEAD (the port
6699 * hash table list entry) is non-NULL and we hold it's
6700 * mutex.
6701 */
6702 snum = rover;
6703 } else {
6704 /* We are given an specific port number; we verify
6705 * that it is not being used. If it is used, we will
6706 * exahust the search in the hash list corresponding
6707 * to the port number (snum) - we detect that with the
6708 * port iterator, pp being NULL.
6709 */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006710 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
wangweidong3c8e43b2014-01-21 15:44:08 +08006711 spin_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08006712 sctp_for_each_hentry(pp, &head->chain) {
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006713 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 goto pp_found;
6715 }
6716 }
6717 pp = NULL;
6718 goto pp_not_found;
6719pp_found:
6720 if (!hlist_empty(&pp->owner)) {
6721 /* We had a port hash table hit - there is an
6722 * available port (pp != NULL) and it is being
6723 * used by other socket (pp->owner not empty); that other
6724 * socket is going to be sk2.
6725 */
6726 int reuse = sk->sk_reuse;
6727 struct sock *sk2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728
Daniel Borkmannbb333812013-06-28 19:49:40 +02006729 pr_debug("%s: found a possible match\n", __func__);
6730
Vlad Yasevichce5325c2007-05-04 13:34:49 -07006731 if (pp->fastreuse && sk->sk_reuse &&
6732 sk->sk_state != SCTP_SS_LISTENING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733 goto success;
6734
6735 /* Run through the list of sockets bound to the port
6736 * (pp->port) [via the pointers bind_next and
6737 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
6738 * we get the endpoint they describe and run through
6739 * the endpoint's list of IP (v4 or v6) addresses,
6740 * comparing each of the addresses with the address of
6741 * the socket sk. If we find a match, then that means
6742 * that this port/socket (sk) combination are already
6743 * in an endpoint.
6744 */
Sasha Levinb67bfe02013-02-27 17:06:00 -08006745 sk_for_each_bound(sk2, &pp->owner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 struct sctp_endpoint *ep2;
6747 ep2 = sctp_sk(sk2)->ep;
6748
Vlad Yasevich4e540642008-07-18 23:06:32 -07006749 if (sk == sk2 ||
6750 (reuse && sk2->sk_reuse &&
6751 sk2->sk_state != SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 continue;
6753
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07006754 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
6755 sctp_sk(sk2), sctp_sk(sk))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 ret = (long)sk2;
6757 goto fail_unlock;
6758 }
6759 }
Daniel Borkmannbb333812013-06-28 19:49:40 +02006760
6761 pr_debug("%s: found a match\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 }
6763pp_not_found:
6764 /* If there was a hash table miss, create a new port. */
6765 ret = 1;
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00006766 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 goto fail_unlock;
6768
6769 /* In either case (hit or miss), make sure fastreuse is 1 only
6770 * if sk->sk_reuse is too (that is, if the caller requested
6771 * SO_REUSEADDR on this socket -sk-).
6772 */
Vlad Yasevichce5325c2007-05-04 13:34:49 -07006773 if (hlist_empty(&pp->owner)) {
6774 if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
6775 pp->fastreuse = 1;
6776 else
6777 pp->fastreuse = 0;
6778 } else if (pp->fastreuse &&
6779 (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780 pp->fastreuse = 0;
6781
6782 /* We are set, so fill up all the data in the hash table
6783 * entry, tie the socket list information with the rest of the
6784 * sockets FIXME: Blurry, NPI (ipg).
6785 */
6786success:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 if (!sctp_sk(sk)->bind_hash) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006788 inet_sk(sk)->inet_num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 sk_add_bind_node(sk, &pp->owner);
6790 sctp_sk(sk)->bind_hash = pp;
6791 }
6792 ret = 0;
6793
6794fail_unlock:
wangweidong3c8e43b2014-01-21 15:44:08 +08006795 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796
6797fail:
wangweidong79b91132014-01-21 15:44:07 +08006798 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799 return ret;
6800}
6801
6802/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
6803 * port is requested.
6804 */
6805static int sctp_get_port(struct sock *sk, unsigned short snum)
6806{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 union sctp_addr addr;
6808 struct sctp_af *af = sctp_sk(sk)->pf->af;
6809
6810 /* Set up a dummy address struct from the sk. */
6811 af->from_sk(&addr, sk);
6812 addr.v4.sin_port = htons(snum);
6813
6814 /* Note: sk->sk_num gets filled in if ephemeral port request. */
Daniel Borkmann62208f12013-06-25 18:17:30 +02006815 return !!sctp_get_port_local(sk, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816}
6817
6818/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 * Move a socket to LISTENING state.
6820 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02006821static int sctp_listen_start(struct sock *sk, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822{
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006823 struct sctp_sock *sp = sctp_sk(sk);
6824 struct sctp_endpoint *ep = sp->ep;
Herbert Xu5821c762016-01-24 21:20:12 +08006825 struct crypto_shash *tfm = NULL;
Neil Horman3c681982012-10-24 09:20:03 +00006826 char alg[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827
6828 /* Allocate HMAC for generating cookie. */
Neil Horman3c681982012-10-24 09:20:03 +00006829 if (!sp->hmac && sp->sctp_hmac_alg) {
6830 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
Herbert Xu5821c762016-01-24 21:20:12 +08006831 tfm = crypto_alloc_shash(alg, 0, 0);
Vlad Yasevich8dc49842007-05-09 13:50:20 -07006832 if (IS_ERR(tfm)) {
Joe Perchese87cc472012-05-13 21:56:26 +00006833 net_info_ratelimited("failed to load transform for %s: %ld\n",
Neil Horman3c681982012-10-24 09:20:03 +00006834 sp->sctp_hmac_alg, PTR_ERR(tfm));
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006835 return -ENOSYS;
6836 }
6837 sctp_sk(sk)->hmac = tfm;
6838 }
6839
6840 /*
6841 * If a bind() or sctp_bindx() is not called prior to a listen()
6842 * call that allows new associations to be accepted, the system
6843 * picks an ephemeral port and will choose an address set equivalent
6844 * to binding with a wildcard address.
6845 *
6846 * This is not currently spelled out in the SCTP sockets
6847 * extensions draft, but follows the practice as seen in TCP
6848 * sockets.
6849 *
6850 */
6851 sk->sk_state = SCTP_SS_LISTENING;
6852 if (!ep->base.bind_addr.port) {
6853 if (sctp_autobind(sk))
6854 return -EAGAIN;
6855 } else {
Eric Dumazetc720c7e2009-10-15 06:30:45 +00006856 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006857 sk->sk_state = SCTP_SS_CLOSED;
6858 return -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859 }
6860 }
6861
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006862 sk->sk_max_ack_backlog = backlog;
6863 sctp_hash_endpoint(ep);
6864 return 0;
6865}
6866
6867/*
6868 * 4.1.3 / 5.1.3 listen()
6869 *
6870 * By default, new associations are not accepted for UDP style sockets.
6871 * An application uses listen() to mark a socket as being able to
6872 * accept new associations.
6873 *
6874 * On TCP style sockets, applications use listen() to ready the SCTP
6875 * endpoint for accepting inbound associations.
6876 *
6877 * On both types of endpoints a backlog of '0' disables listening.
6878 *
6879 * Move a socket to LISTENING state.
6880 */
6881int sctp_inet_listen(struct socket *sock, int backlog)
6882{
6883 struct sock *sk = sock->sk;
6884 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6885 int err = -EINVAL;
6886
6887 if (unlikely(backlog < 0))
6888 return err;
6889
wangweidong048ed4b2014-01-21 15:44:11 +08006890 lock_sock(sk);
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006891
6892 /* Peeled-off sockets are not allowed to listen(). */
6893 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
6894 goto out;
6895
6896 if (sock->state != SS_UNCONNECTED)
6897 goto out;
6898
Xin Long02f04302017-04-06 13:10:52 +08006899 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
6900 goto out;
6901
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006902 /* If backlog is zero, disable listening. */
6903 if (!backlog) {
6904 if (sctp_sstate(sk, CLOSED))
6905 goto out;
6906
6907 err = 0;
6908 sctp_unhash_endpoint(ep);
6909 sk->sk_state = SCTP_SS_CLOSED;
6910 if (sk->sk_reuse)
6911 sctp_sk(sk)->bind_hash->fastreuse = 1;
6912 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07006913 }
6914
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006915 /* If we are already listening, just update the backlog */
6916 if (sctp_sstate(sk, LISTENING))
6917 sk->sk_max_ack_backlog = backlog;
6918 else {
6919 err = sctp_listen_start(sk, backlog);
6920 if (err)
6921 goto out;
6922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923
Vlad Yasevich5e8f3f72009-03-12 09:49:17 +00006924 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925out:
wangweidong048ed4b2014-01-21 15:44:11 +08006926 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928}
6929
6930/*
6931 * This function is done by modeling the current datagram_poll() and the
6932 * tcp_poll(). Note that, based on these implementations, we don't
6933 * lock the socket in this function, even though it seems that,
6934 * ideally, locking or some other mechanisms can be used to ensure
Neil Horman9bffc4a2005-12-19 14:24:40 -08006935 * the integrity of the counters (sndbuf and wmem_alloc) used
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 * in this place. We assume that we don't need locks either until proven
6937 * otherwise.
6938 *
6939 * Another thing to note is that we include the Async I/O support
6940 * here, again, by modeling the current TCP/UDP code. We don't have
6941 * a good way to test with it yet.
6942 */
6943unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
6944{
6945 struct sock *sk = sock->sk;
6946 struct sctp_sock *sp = sctp_sk(sk);
6947 unsigned int mask;
6948
Eric Dumazetaa395142010-04-20 13:03:51 +00006949 poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03006951 sock_rps_record_flow(sk);
6952
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 /* A TCP-style listening socket becomes readable when the accept queue
6954 * is not empty.
6955 */
6956 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
6957 return (!list_empty(&sp->ep->asocs)) ?
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006958 (POLLIN | POLLRDNORM) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959
6960 mask = 0;
6961
6962 /* Is there any exceptional events? */
6963 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +00006964 mask |= POLLERR |
Daniel Borkmanna0fb05d2013-09-07 16:44:59 +02006965 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
Davide Libenzif348d702006-03-25 03:07:39 -08006966 if (sk->sk_shutdown & RCV_SHUTDOWN)
Eric Dumazetdb409802010-09-06 11:13:50 +00006967 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 if (sk->sk_shutdown == SHUTDOWN_MASK)
6969 mask |= POLLHUP;
6970
6971 /* Is it readable? Reconsider this code with TCP-style support. */
Eric Dumazetdb409802010-09-06 11:13:50 +00006972 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 mask |= POLLIN | POLLRDNORM;
6974
6975 /* The association is either gone or not ready. */
6976 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
6977 return mask;
6978
6979 /* Is it writable? */
6980 if (sctp_writeable(sk)) {
6981 mask |= POLLOUT | POLLWRNORM;
6982 } else {
Eric Dumazet9cd3e072015-11-29 20:03:10 -08006983 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984 /*
6985 * Since the socket is not locked, the buffer
6986 * might be made available after the writeable check and
6987 * before the bit is set. This could cause a lost I/O
6988 * signal. tcp_poll() has a race breaker for this race
6989 * condition. Based on their implementation, we put
6990 * in the following code to cover it as well.
6991 */
6992 if (sctp_writeable(sk))
6993 mask |= POLLOUT | POLLWRNORM;
6994 }
6995 return mask;
6996}
6997
6998/********************************************************************
6999 * 2nd Level Abstractions
7000 ********************************************************************/
7001
7002static struct sctp_bind_bucket *sctp_bucket_create(
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007003 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004{
7005 struct sctp_bind_bucket *pp;
7006
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08007007 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 if (pp) {
Li Zefan935a7f62008-04-10 01:58:06 -07007009 SCTP_DBG_OBJCNT_INC(bind_bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 pp->port = snum;
7011 pp->fastreuse = 0;
7012 INIT_HLIST_HEAD(&pp->owner);
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007013 pp->net = net;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007014 hlist_add_head(&pp->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015 }
7016 return pp;
7017}
7018
7019/* Caller must hold hashbucket lock for this tb with local BH disabled */
7020static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
7021{
Sridhar Samudrala37fa6872006-07-21 14:45:47 -07007022 if (pp && hlist_empty(&pp->owner)) {
Vlad Yasevichd970dbf2007-11-09 11:43:40 -05007023 __hlist_del(&pp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024 kmem_cache_free(sctp_bucket_cachep, pp);
7025 SCTP_DBG_OBJCNT_DEC(bind_bucket);
7026 }
7027}
7028
7029/* Release this socket's reference to a local port. */
7030static inline void __sctp_put_port(struct sock *sk)
7031{
7032 struct sctp_bind_hashbucket *head =
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007033 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
7034 inet_sk(sk)->inet_num)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 struct sctp_bind_bucket *pp;
7036
wangweidong3c8e43b2014-01-21 15:44:08 +08007037 spin_lock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038 pp = sctp_sk(sk)->bind_hash;
7039 __sk_del_bind_node(sk);
7040 sctp_sk(sk)->bind_hash = NULL;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00007041 inet_sk(sk)->inet_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042 sctp_bucket_destroy(pp);
wangweidong3c8e43b2014-01-21 15:44:08 +08007043 spin_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044}
7045
7046void sctp_put_port(struct sock *sk)
7047{
wangweidong79b91132014-01-21 15:44:07 +08007048 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 __sctp_put_port(sk);
wangweidong79b91132014-01-21 15:44:07 +08007050 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051}
7052
7053/*
7054 * The system picks an ephemeral port and choose an address set equivalent
7055 * to binding with a wildcard address.
7056 * One of those addresses will be the primary address for the association.
7057 * This automatically enables the multihoming capability of SCTP.
7058 */
7059static int sctp_autobind(struct sock *sk)
7060{
7061 union sctp_addr autoaddr;
7062 struct sctp_af *af;
Al Viro6fbfa9f2006-11-20 17:24:53 -08007063 __be16 port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064
7065 /* Initialize a local sockaddr structure to INADDR_ANY. */
7066 af = sctp_sk(sk)->pf->af;
7067
Eric Dumazetc720c7e2009-10-15 06:30:45 +00007068 port = htons(inet_sk(sk)->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 af->inaddr_any(&autoaddr, port);
7070
7071 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
7072}
7073
7074/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
7075 *
7076 * From RFC 2292
7077 * 4.2 The cmsghdr Structure *
7078 *
7079 * When ancillary data is sent or received, any number of ancillary data
7080 * objects can be specified by the msg_control and msg_controllen members of
7081 * the msghdr structure, because each object is preceded by
7082 * a cmsghdr structure defining the object's length (the cmsg_len member).
7083 * Historically Berkeley-derived implementations have passed only one object
7084 * at a time, but this API allows multiple objects to be
7085 * passed in a single call to sendmsg() or recvmsg(). The following example
7086 * shows two ancillary data objects in a control buffer.
7087 *
7088 * |<--------------------------- msg_controllen -------------------------->|
7089 * | |
7090 *
7091 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
7092 *
7093 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
7094 * | | |
7095 *
7096 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
7097 *
7098 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
7099 * | | | | |
7100 *
7101 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7102 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
7103 *
7104 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
7105 *
7106 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
7107 * ^
7108 * |
7109 *
7110 * msg_control
7111 * points here
7112 */
Daniel Borkmanndda91922013-06-17 11:40:05 +02007113static int sctp_msghdr_parse(const struct msghdr *msg, sctp_cmsgs_t *cmsgs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114{
7115 struct cmsghdr *cmsg;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007116 struct msghdr *my_msg = (struct msghdr *)msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117
Gu Zhengf95b4142014-12-11 11:22:04 +08007118 for_each_cmsghdr(cmsg, my_msg) {
Vlad Yasevichab38fb02008-04-12 18:40:06 -07007119 if (!CMSG_OK(my_msg, cmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 return -EINVAL;
7121
7122 /* Should we parse this header or ignore? */
7123 if (cmsg->cmsg_level != IPPROTO_SCTP)
7124 continue;
7125
7126 /* Strictly check lengths following example in SCM code. */
7127 switch (cmsg->cmsg_type) {
7128 case SCTP_INIT:
7129 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007130 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 *
7132 * This cmsghdr structure provides information for
7133 * initializing new SCTP associations with sendmsg().
7134 * The SCTP_INITMSG socket option uses this same data
7135 * structure. This structure is not used for
7136 * recvmsg().
7137 *
7138 * cmsg_level cmsg_type cmsg_data[]
7139 * ------------ ------------ ----------------------
7140 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
7141 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007142 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143 return -EINVAL;
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007144
7145 cmsgs->init = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 break;
7147
7148 case SCTP_SNDRCV:
7149 /* SCTP Socket API Extension
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007150 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151 *
7152 * This cmsghdr structure specifies SCTP options for
7153 * sendmsg() and describes SCTP header information
7154 * about a received message through recvmsg().
7155 *
7156 * cmsg_level cmsg_type cmsg_data[]
7157 * ------------ ------------ ----------------------
7158 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
7159 */
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007160 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 return -EINVAL;
7162
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007163 cmsgs->srinfo = CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007165 if (cmsgs->srinfo->sinfo_flags &
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007166 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Longa6c2f792016-07-09 19:47:43 +08007167 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -07007168 SCTP_ABORT | SCTP_EOF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169 return -EINVAL;
7170 break;
7171
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007172 case SCTP_SNDINFO:
7173 /* SCTP Socket API Extension
7174 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
7175 *
7176 * This cmsghdr structure specifies SCTP options for
7177 * sendmsg(). This structure and SCTP_RCVINFO replaces
7178 * SCTP_SNDRCV which has been deprecated.
7179 *
7180 * cmsg_level cmsg_type cmsg_data[]
7181 * ------------ ------------ ---------------------
7182 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
7183 */
7184 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
7185 return -EINVAL;
7186
7187 cmsgs->sinfo = CMSG_DATA(cmsg);
7188
7189 if (cmsgs->sinfo->snd_flags &
7190 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
Xin Longa6c2f792016-07-09 19:47:43 +08007191 SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007192 SCTP_ABORT | SCTP_EOF))
7193 return -EINVAL;
7194 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 default:
7196 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07007197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 }
Geir Ola Vaagland63b94932014-07-12 20:30:36 +02007199
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200 return 0;
7201}
7202
7203/*
7204 * Wait for a packet..
7205 * Note: This function is the same function as in core/datagram.c
7206 * with a few modifications to make lksctp work.
7207 */
wangweidong26ac8e52013-12-23 12:16:51 +08007208static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209{
7210 int error;
7211 DEFINE_WAIT(wait);
7212
Eric Dumazetaa395142010-04-20 13:03:51 +00007213 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214
7215 /* Socket errors? */
7216 error = sock_error(sk);
7217 if (error)
7218 goto out;
7219
7220 if (!skb_queue_empty(&sk->sk_receive_queue))
7221 goto ready;
7222
7223 /* Socket shut down? */
7224 if (sk->sk_shutdown & RCV_SHUTDOWN)
7225 goto out;
7226
7227 /* Sequenced packets can come disconnected. If so we report the
7228 * problem.
7229 */
7230 error = -ENOTCONN;
7231
7232 /* Is there a good reason to think that we may receive some data? */
7233 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
7234 goto out;
7235
7236 /* Handle signals. */
7237 if (signal_pending(current))
7238 goto interrupted;
7239
7240 /* Let another process have a go. Since we are going to sleep
7241 * anyway. Note: This may cause odd behaviors if the message
7242 * does not fit in the user's buffer, but this seems to be the
7243 * only way to honor MSG_DONTWAIT realistically.
7244 */
wangweidong048ed4b2014-01-21 15:44:11 +08007245 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 *timeo_p = schedule_timeout(*timeo_p);
wangweidong048ed4b2014-01-21 15:44:11 +08007247 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248
7249ready:
Eric Dumazetaa395142010-04-20 13:03:51 +00007250 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251 return 0;
7252
7253interrupted:
7254 error = sock_intr_errno(*timeo_p);
7255
7256out:
Eric Dumazetaa395142010-04-20 13:03:51 +00007257 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258 *err = error;
7259 return error;
7260}
7261
7262/* Receive a datagram.
7263 * Note: This is pretty much the same routine as in core/datagram.c
7264 * with a few changes to make lksctp work.
7265 */
Geir Ola Vaagland2347c802014-07-12 20:30:38 +02007266struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
7267 int noblock, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268{
7269 int error;
7270 struct sk_buff *skb;
7271 long timeo;
7272
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273 timeo = sock_rcvtimeo(sk, noblock);
7274
Daniel Borkmannbb333812013-06-28 19:49:40 +02007275 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
7276 MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277
7278 do {
7279 /* Again only user level code calls this function,
7280 * so nothing interrupt level
7281 * will suddenly eat the receive_queue.
7282 *
7283 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +00007284 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 */
7286 if (flags & MSG_PEEK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 skb = skb_peek(&sk->sk_receive_queue);
7288 if (skb)
7289 atomic_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290 } else {
Marcelo Ricardo Leitner311b2172016-04-13 19:12:29 -03007291 skb = __skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292 }
7293
7294 if (skb)
7295 return skb;
7296
Neil Horman6736dc32005-12-02 20:30:06 -08007297 /* Caller is allowed not to check sk->sk_err before calling. */
7298 error = sock_error(sk);
7299 if (error)
7300 goto no_packet;
7301
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302 if (sk->sk_shutdown & RCV_SHUTDOWN)
7303 break;
7304
Neil Horman8465a5f2014-04-17 15:26:51 -04007305 if (sk_can_busy_loop(sk) &&
7306 sk_busy_loop(sk, noblock))
7307 continue;
7308
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309 /* User doesn't want to wait. */
7310 error = -EAGAIN;
7311 if (!timeo)
7312 goto no_packet;
7313 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
7314
7315 return NULL;
7316
7317no_packet:
7318 *err = error;
7319 return NULL;
7320}
7321
7322/* If sndbuf has changed, wake up per association sndbuf waiters. */
7323static void __sctp_write_space(struct sctp_association *asoc)
7324{
7325 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326
Eric Dumazetceb5d582015-11-29 20:03:11 -08007327 if (sctp_wspace(asoc) <= 0)
7328 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329
Eric Dumazetceb5d582015-11-29 20:03:11 -08007330 if (waitqueue_active(&asoc->wait))
7331 wake_up_interruptible(&asoc->wait);
Eric Dumazeteaefd112011-02-18 03:26:36 +00007332
Eric Dumazetceb5d582015-11-29 20:03:11 -08007333 if (sctp_writeable(sk)) {
7334 struct socket_wq *wq;
7335
7336 rcu_read_lock();
7337 wq = rcu_dereference(sk->sk_wq);
7338 if (wq) {
7339 if (waitqueue_active(&wq->wait))
7340 wake_up_interruptible(&wq->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341
7342 /* Note that we try to include the Async I/O support
7343 * here by modeling from the current TCP/UDP code.
7344 * We have not tested with it yet.
7345 */
Eric Dumazeteaefd112011-02-18 03:26:36 +00007346 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
Eric Dumazetceb5d582015-11-29 20:03:11 -08007347 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 }
Eric Dumazetceb5d582015-11-29 20:03:11 -08007349 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 }
7351}
7352
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007353static void sctp_wake_up_waiters(struct sock *sk,
7354 struct sctp_association *asoc)
7355{
7356 struct sctp_association *tmp = asoc;
7357
7358 /* We do accounting for the sndbuf space per association,
7359 * so we only need to wake our own association.
7360 */
7361 if (asoc->ep->sndbuf_policy)
7362 return __sctp_write_space(asoc);
7363
Daniel Borkmann1e1cdf82014-04-09 16:10:20 +02007364 /* If association goes down and is just flushing its
7365 * outq, then just normally notify others.
7366 */
7367 if (asoc->base.dead)
7368 return sctp_write_space(sk);
7369
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007370 /* Accounting for the sndbuf space is per socket, so we
7371 * need to wake up others, try to be fair and in case of
7372 * other associations, let them have a go first instead
7373 * of just doing a sctp_write_space() call.
7374 *
7375 * Note that we reach sctp_wake_up_waiters() only when
7376 * associations free up queued chunks, thus we are under
7377 * lock and the list of associations on a socket is
7378 * guaranteed not to change.
7379 */
7380 for (tmp = list_next_entry(tmp, asocs); 1;
7381 tmp = list_next_entry(tmp, asocs)) {
7382 /* Manually skip the head element. */
7383 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
7384 continue;
7385 /* Wake up association. */
7386 __sctp_write_space(tmp);
7387 /* We've reached the end. */
7388 if (tmp == asoc)
7389 break;
7390 }
7391}
7392
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393/* Do accounting for the sndbuf space.
7394 * Decrement the used sndbuf space of the corresponding association by the
7395 * data size which was just transmitted(freed).
7396 */
7397static void sctp_wfree(struct sk_buff *skb)
7398{
Daniel Borkmannf869c912014-11-20 01:54:48 +01007399 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
7400 struct sctp_association *asoc = chunk->asoc;
7401 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402
Neil Horman4eb701d2005-04-28 12:02:04 -07007403 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
7404 sizeof(struct sk_buff) +
7405 sizeof(struct sctp_chunk);
7406
Neil Horman4eb701d2005-04-28 12:02:04 -07007407 atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
7408
Neil Horman4d93df02007-08-15 16:07:44 -07007409 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007410 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
Neil Horman4d93df02007-08-15 16:07:44 -07007411 */
7412 sk->sk_wmem_queued -= skb->truesize;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007413 sk_mem_uncharge(sk, skb->truesize);
Neil Horman4d93df02007-08-15 16:07:44 -07007414
Neil Horman4eb701d2005-04-28 12:02:04 -07007415 sock_wfree(skb);
Daniel Borkmann52c35be2014-04-08 17:26:13 +02007416 sctp_wake_up_waiters(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417
7418 sctp_association_put(asoc);
7419}
7420
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07007421/* Do accounting for the receive space on the socket.
7422 * Accounting for the association is done in ulpevent.c
7423 * We set this as a destructor for the cloned data skbs so that
7424 * accounting is done at the correct time.
7425 */
7426void sctp_sock_rfree(struct sk_buff *skb)
7427{
7428 struct sock *sk = skb->sk;
7429 struct sctp_ulpevent *event = sctp_skb2event(skb);
7430
7431 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
Neil Horman4d93df02007-08-15 16:07:44 -07007432
7433 /*
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007434 * Mimic the behavior of sock_rfree
Neil Horman4d93df02007-08-15 16:07:44 -07007435 */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08007436 sk_mem_uncharge(sk, event->rmem_len);
Vlad Yasevich331c4ee2006-10-09 21:34:04 -07007437}
7438
7439
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440/* Helper function to wait for space in the sndbuf. */
7441static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
Xin Long2f056e72018-01-15 17:01:36 +08007442 size_t msg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443{
7444 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445 long current_timeo = *timeo_p;
7446 DEFINE_WAIT(wait);
Xin Long2f056e72018-01-15 17:01:36 +08007447 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448
Daniel Borkmannbb333812013-06-28 19:49:40 +02007449 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
7450 *timeo_p, msg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451
7452 /* Increment the association's refcnt. */
7453 sctp_association_hold(asoc);
7454
7455 /* Wait on the association specific sndbuf space. */
7456 for (;;) {
7457 prepare_to_wait_exclusive(&asoc->wait, &wait,
7458 TASK_INTERRUPTIBLE);
Xin Long1158ecd52017-11-15 16:55:54 +08007459 if (asoc->base.dead)
7460 goto do_dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 if (!*timeo_p)
7462 goto do_nonblock;
Xin Long1158ecd52017-11-15 16:55:54 +08007463 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 goto do_error;
7465 if (signal_pending(current))
7466 goto do_interrupted;
7467 if (msg_len <= sctp_wspace(asoc))
7468 break;
7469
7470 /* Let another process have a go. Since we are going
7471 * to sleep anyway.
7472 */
wangweidong048ed4b2014-01-21 15:44:11 +08007473 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08007475 lock_sock(sk);
Xin Long2f056e72018-01-15 17:01:36 +08007476 if (sk != asoc->base.sk)
7477 goto do_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478
7479 *timeo_p = current_timeo;
7480 }
7481
7482out:
7483 finish_wait(&asoc->wait, &wait);
7484
7485 /* Release the association's refcnt. */
7486 sctp_association_put(asoc);
7487
7488 return err;
7489
Xin Long1158ecd52017-11-15 16:55:54 +08007490do_dead:
7491 err = -ESRCH;
7492 goto out;
7493
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494do_error:
7495 err = -EPIPE;
7496 goto out;
7497
7498do_interrupted:
7499 err = sock_intr_errno(*timeo_p);
7500 goto out;
7501
7502do_nonblock:
7503 err = -EAGAIN;
7504 goto out;
7505}
7506
David S. Miller676d2362014-04-11 16:15:36 -04007507void sctp_data_ready(struct sock *sk)
Wei Yongjun561b1732010-04-28 08:47:18 +00007508{
David S. Miller7ef52732010-05-02 21:43:40 -07007509 struct socket_wq *wq;
7510
7511 rcu_read_lock();
7512 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08007513 if (skwq_has_sleeper(wq))
David S. Miller7ef52732010-05-02 21:43:40 -07007514 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
Wei Yongjun561b1732010-04-28 08:47:18 +00007515 POLLRDNORM | POLLRDBAND);
7516 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
David S. Miller7ef52732010-05-02 21:43:40 -07007517 rcu_read_unlock();
Wei Yongjun561b1732010-04-28 08:47:18 +00007518}
7519
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520/* If socket sndbuf has changed, wake up all per association waiters. */
7521void sctp_write_space(struct sock *sk)
7522{
7523 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524
7525 /* Wake up the tasks in each wait queue. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07007526 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527 __sctp_write_space(asoc);
7528 }
7529}
7530
7531/* Is there any sndbuf space available on the socket?
7532 *
Neil Horman9bffc4a2005-12-19 14:24:40 -08007533 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534 * associations on the same socket. For a UDP-style socket with
7535 * multiple associations, it is possible for it to be "unwriteable"
7536 * prematurely. I assume that this is acceptable because
7537 * a premature "unwriteable" is better than an accidental "writeable" which
7538 * would cause an unwanted block under certain circumstances. For the 1-1
7539 * UDP-style sockets or TCP-style sockets, this code should work.
7540 * - Daisy
7541 */
7542static int sctp_writeable(struct sock *sk)
7543{
7544 int amt = 0;
7545
Eric Dumazet31e6d362009-06-17 19:05:41 -07007546 amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 if (amt < 0)
7548 amt = 0;
7549 return amt;
7550}
7551
7552/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
7553 * returns immediately with EINPROGRESS.
7554 */
7555static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
7556{
7557 struct sock *sk = asoc->base.sk;
7558 int err = 0;
7559 long current_timeo = *timeo_p;
7560 DEFINE_WAIT(wait);
7561
Daniel Borkmannbb333812013-06-28 19:49:40 +02007562 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563
7564 /* Increment the association's refcnt. */
7565 sctp_association_hold(asoc);
7566
7567 for (;;) {
7568 prepare_to_wait_exclusive(&asoc->wait, &wait,
7569 TASK_INTERRUPTIBLE);
7570 if (!*timeo_p)
7571 goto do_nonblock;
7572 if (sk->sk_shutdown & RCV_SHUTDOWN)
7573 break;
7574 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
7575 asoc->base.dead)
7576 goto do_error;
7577 if (signal_pending(current))
7578 goto do_interrupted;
7579
7580 if (sctp_state(asoc, ESTABLISHED))
7581 break;
7582
7583 /* Let another process have a go. Since we are going
7584 * to sleep anyway.
7585 */
wangweidong048ed4b2014-01-21 15:44:11 +08007586 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 current_timeo = schedule_timeout(current_timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08007588 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589
7590 *timeo_p = current_timeo;
7591 }
7592
7593out:
7594 finish_wait(&asoc->wait, &wait);
7595
7596 /* Release the association's refcnt. */
7597 sctp_association_put(asoc);
7598
7599 return err;
7600
7601do_error:
Vlad Yasevich81845c22006-01-30 15:59:54 -08007602 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603 err = -ETIMEDOUT;
7604 else
7605 err = -ECONNREFUSED;
7606 goto out;
7607
7608do_interrupted:
7609 err = sock_intr_errno(*timeo_p);
7610 goto out;
7611
7612do_nonblock:
7613 err = -EINPROGRESS;
7614 goto out;
7615}
7616
7617static int sctp_wait_for_accept(struct sock *sk, long timeo)
7618{
7619 struct sctp_endpoint *ep;
7620 int err = 0;
7621 DEFINE_WAIT(wait);
7622
7623 ep = sctp_sk(sk)->ep;
7624
7625
7626 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00007627 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628 TASK_INTERRUPTIBLE);
7629
7630 if (list_empty(&ep->asocs)) {
wangweidong048ed4b2014-01-21 15:44:11 +08007631 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632 timeo = schedule_timeout(timeo);
wangweidong048ed4b2014-01-21 15:44:11 +08007633 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634 }
7635
7636 err = -EINVAL;
7637 if (!sctp_sstate(sk, LISTENING))
7638 break;
7639
7640 err = 0;
7641 if (!list_empty(&ep->asocs))
7642 break;
7643
7644 err = sock_intr_errno(timeo);
7645 if (signal_pending(current))
7646 break;
7647
7648 err = -EAGAIN;
7649 if (!timeo)
7650 break;
7651 }
7652
Eric Dumazetaa395142010-04-20 13:03:51 +00007653 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007654
7655 return err;
7656}
7657
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +02007658static void sctp_wait_for_close(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007659{
7660 DEFINE_WAIT(wait);
7661
7662 do {
Eric Dumazetaa395142010-04-20 13:03:51 +00007663 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664 if (list_empty(&sctp_sk(sk)->ep->asocs))
7665 break;
wangweidong048ed4b2014-01-21 15:44:11 +08007666 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667 timeout = schedule_timeout(timeout);
wangweidong048ed4b2014-01-21 15:44:11 +08007668 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669 } while (!signal_pending(current) && timeout);
7670
Eric Dumazetaa395142010-04-20 13:03:51 +00007671 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672}
7673
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007674static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
7675{
7676 struct sk_buff *frag;
7677
7678 if (!skb->data_len)
7679 goto done;
7680
7681 /* Don't forget the fragments. */
David S. Miller1b003be2009-06-09 00:22:35 -07007682 skb_walk_frags(skb, frag)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007683 sctp_skb_set_owner_r_frag(frag, sk);
7684
7685done:
7686 sctp_skb_set_owner_r(skb, sk);
7687}
7688
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007689void sctp_copy_sock(struct sock *newsk, struct sock *sk,
7690 struct sctp_association *asoc)
7691{
7692 struct inet_sock *inet = inet_sk(sk);
Julia Lawall09cb47a2010-01-21 02:43:20 -08007693 struct inet_sock *newinet;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007694
7695 newsk->sk_type = sk->sk_type;
7696 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
7697 newsk->sk_flags = sk->sk_flags;
Marcelo Ricardo Leitner50a5ffb2015-12-04 15:14:05 -02007698 newsk->sk_tsflags = sk->sk_tsflags;
Tom Herbert28448b82014-05-23 08:47:19 -07007699 newsk->sk_no_check_tx = sk->sk_no_check_tx;
7700 newsk->sk_no_check_rx = sk->sk_no_check_rx;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007701 newsk->sk_reuse = sk->sk_reuse;
7702
7703 newsk->sk_shutdown = sk->sk_shutdown;
Daniel Borkmann0a2fbac2013-06-25 18:17:29 +02007704 newsk->sk_destruct = sctp_destruct_sock;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007705 newsk->sk_family = sk->sk_family;
7706 newsk->sk_protocol = IPPROTO_SCTP;
7707 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
7708 newsk->sk_sndbuf = sk->sk_sndbuf;
7709 newsk->sk_rcvbuf = sk->sk_rcvbuf;
7710 newsk->sk_lingertime = sk->sk_lingertime;
7711 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
7712 newsk->sk_sndtimeo = sk->sk_sndtimeo;
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -03007713 newsk->sk_rxhash = sk->sk_rxhash;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007714
7715 newinet = inet_sk(newsk);
7716
7717 /* Initialize sk's sport, dport, rcv_saddr and daddr for
7718 * getsockname() and getpeername()
7719 */
Eric Dumazetc720c7e2009-10-15 06:30:45 +00007720 newinet->inet_sport = inet->inet_sport;
7721 newinet->inet_saddr = inet->inet_saddr;
7722 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
7723 newinet->inet_dport = htons(asoc->peer.port);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007724 newinet->pmtudisc = inet->pmtudisc;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00007725 newinet->inet_id = asoc->next_tsn ^ jiffies;
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007726
7727 newinet->uc_ttl = inet->uc_ttl;
7728 newinet->mc_loop = 1;
7729 newinet->mc_ttl = 1;
7730 newinet->mc_index = 0;
7731 newinet->mc_list = NULL;
Marcelo Ricardo Leitner01ce63c2015-12-04 15:14:04 -02007732
7733 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
7734 net_enable_timestamp();
Marcelo Ricardo Leitner3538a5c2015-12-23 16:44:09 -02007735
7736 security_sk_clone(sk, newsk);
Vlad Yasevich914e1c82009-02-13 08:33:44 +00007737}
7738
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03007739static inline void sctp_copy_descendant(struct sock *sk_to,
7740 const struct sock *sk_from)
7741{
7742 int ancestor_size = sizeof(struct inet_sock) +
7743 sizeof(struct sctp_sock) -
7744 offsetof(struct sctp_sock, auto_asconf_list);
7745
7746 if (sk_from->sk_family == PF_INET6)
7747 ancestor_size += sizeof(struct ipv6_pinfo);
7748
7749 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
7750}
7751
Linus Torvalds1da177e2005-04-16 15:20:36 -07007752/* Populate the fields of the newsk from the oldsk and migrate the assoc
7753 * and its messages to the newsk.
7754 */
7755static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
7756 struct sctp_association *assoc,
7757 sctp_socket_type_t type)
7758{
7759 struct sctp_sock *oldsp = sctp_sk(oldsk);
7760 struct sctp_sock *newsp = sctp_sk(newsk);
7761 struct sctp_bind_bucket *pp; /* hash list port iterator */
7762 struct sctp_endpoint *newep = newsp->ep;
7763 struct sk_buff *skb, *tmp;
7764 struct sctp_ulpevent *event;
Vlad Yasevichf26f7c42007-12-06 22:50:27 -08007765 struct sctp_bind_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766
7767 /* Migrate socket buffer sizes and all the socket level options to the
7768 * new socket.
7769 */
7770 newsk->sk_sndbuf = oldsk->sk_sndbuf;
7771 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
7772 /* Brute force copy old sctp opt. */
Marcelo Ricardo Leitner2d45a022015-06-12 10:16:41 -03007773 sctp_copy_descendant(newsk, oldsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774
7775 /* Restore the ep value that was overwritten with the above structure
7776 * copy.
7777 */
7778 newsp->ep = newep;
7779 newsp->hmac = NULL;
7780
7781 /* Hook this new socket in to the bind_hash list. */
Eric W. Biedermanf1f43762012-08-06 08:39:38 +00007782 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
7783 inet_sk(oldsk)->inet_num)];
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01007784 spin_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785 pp = sctp_sk(oldsk)->bind_hash;
7786 sk_add_bind_node(newsk, &pp->owner);
7787 sctp_sk(newsk)->bind_hash = pp;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00007788 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
Nicholas Mc Guire489ce5f2016-03-13 11:48:24 +01007789 spin_unlock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07007791 /* Copy the bind_addr list from the original endpoint to the new
7792 * endpoint so that we can handle restarts properly
7793 */
Vlad Yasevich8e71a112007-12-06 22:50:54 -08007794 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
7795 &oldsp->ep->base.bind_addr, GFP_KERNEL);
Vladislav Yasevich4243cac2005-06-13 15:10:49 -07007796
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797 /* Move any messages in the old socket's receive queue that are for the
7798 * peeled off association to the new socket's receive queue.
7799 */
7800 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
7801 event = sctp_skb2event(skb);
7802 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07007803 __skb_unlink(skb, &oldsk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804 __skb_queue_tail(&newsk->sk_receive_queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007805 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007806 }
7807 }
7808
7809 /* Clean up any messages pending delivery due to partial
7810 * delivery. Three cases:
7811 * 1) No partial deliver; no work.
7812 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
7813 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
7814 */
7815 skb_queue_head_init(&newsp->pd_lobby);
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007816 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007818 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819 struct sk_buff_head *queue;
7820
7821 /* Decide which queue to move pd_lobby skbs to. */
7822 if (assoc->ulpq.pd_mode) {
7823 queue = &newsp->pd_lobby;
7824 } else
7825 queue = &newsk->sk_receive_queue;
7826
7827 /* Walk through the pd_lobby, looking for skbs that
7828 * need moved to the new socket.
7829 */
7830 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
7831 event = sctp_skb2event(skb);
7832 if (event->asoc == assoc) {
David S. Miller8728b832005-08-09 19:25:21 -07007833 __skb_unlink(skb, &oldsp->pd_lobby);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007834 __skb_queue_tail(queue, skb);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007835 sctp_skb_set_owner_r_frag(skb, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836 }
7837 }
7838
7839 /* Clear up any skbs waiting for the partial
7840 * delivery to finish.
7841 */
7842 if (assoc->ulpq.pd_mode)
Vlad Yasevichb6e13312007-04-20 12:23:15 -07007843 sctp_clear_pd(oldsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844
7845 }
7846
Wei Yongjun1bc4ee42009-07-05 19:45:48 +00007847 sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007848 sctp_skb_set_owner_r_frag(skb, newsk);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007849
Wei Yongjun1bc4ee42009-07-05 19:45:48 +00007850 sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp)
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007851 sctp_skb_set_owner_r_frag(skb, newsk);
Tsutomu Fujiiea2bc482007-04-17 12:49:53 -07007852
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853 /* Set the type of socket to indicate that it is peeled off from the
7854 * original UDP-style socket or created with the accept() call on a
7855 * TCP-style socket..
7856 */
7857 newsp->type = type;
7858
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07007859 /* Mark the new socket "in-use" by the user so that any packets
7860 * that may arrive on the association after we've moved it are
7861 * queued to the backlog. This prevents a potential race between
7862 * backlog processing on the old socket and new-packet processing
7863 * on the new socket.
Zach Brown5131a182007-06-22 15:14:46 -07007864 *
7865 * The caller has just allocated newsk so we can guarantee that other
7866 * paths won't try to lock it and then oldsk.
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -07007867 */
Zach Brown5131a182007-06-22 15:14:46 -07007868 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
Xin Longb89fc6a2017-10-28 02:13:29 +08007869 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870 sctp_assoc_migrate(assoc, newsk);
Xin Longb89fc6a2017-10-28 02:13:29 +08007871 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872
7873 /* If the association on the newsk is already closed before accept()
7874 * is called, set RCV_SHUTDOWN flag.
7875 */
Xin Longd46e4162016-06-09 22:48:18 +08007876 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
Xin Long141ddef2016-06-16 01:15:06 +08007877 newsk->sk_state = SCTP_SS_CLOSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 newsk->sk_shutdown |= RCV_SHUTDOWN;
Xin Longd46e4162016-06-09 22:48:18 +08007879 } else {
7880 newsk->sk_state = SCTP_SS_ESTABLISHED;
7881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882
wangweidong048ed4b2014-01-21 15:44:11 +08007883 release_sock(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007884}
7885
Neil Horman4d93df02007-08-15 16:07:44 -07007886
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887/* This proto struct describes the ULP interface for SCTP. */
7888struct proto sctp_prot = {
7889 .name = "SCTP",
7890 .owner = THIS_MODULE,
7891 .close = sctp_close,
7892 .connect = sctp_connect,
7893 .disconnect = sctp_disconnect,
7894 .accept = sctp_accept,
7895 .ioctl = sctp_ioctl,
7896 .init = sctp_init_sock,
7897 .destroy = sctp_destroy_sock,
7898 .shutdown = sctp_shutdown,
7899 .setsockopt = sctp_setsockopt,
7900 .getsockopt = sctp_getsockopt,
7901 .sendmsg = sctp_sendmsg,
7902 .recvmsg = sctp_recvmsg,
7903 .bind = sctp_bind,
7904 .backlog_rcv = sctp_backlog_rcv,
7905 .hash = sctp_hash,
7906 .unhash = sctp_unhash,
7907 .get_port = sctp_get_port,
7908 .obj_size = sizeof(struct sctp_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07007909 .sysctl_mem = sysctl_sctp_mem,
7910 .sysctl_rmem = sysctl_sctp_rmem,
7911 .sysctl_wmem = sysctl_sctp_wmem,
7912 .memory_pressure = &sctp_memory_pressure,
7913 .enter_memory_pressure = sctp_enter_memory_pressure,
7914 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08007915 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916};
7917
Eric Dumazetdfd56b82011-12-10 09:48:31 +00007918#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet8295b6d2007-11-05 23:40:28 -08007919
Eric Dumazet602dd622015-12-01 07:20:07 -08007920#include <net/transp_v6.h>
7921static void sctp_v6_destroy_sock(struct sock *sk)
7922{
7923 sctp_destroy_sock(sk);
7924 inet6_destroy_sock(sk);
7925}
7926
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927struct proto sctpv6_prot = {
7928 .name = "SCTPv6",
7929 .owner = THIS_MODULE,
7930 .close = sctp_close,
7931 .connect = sctp_connect,
7932 .disconnect = sctp_disconnect,
7933 .accept = sctp_accept,
7934 .ioctl = sctp_ioctl,
7935 .init = sctp_init_sock,
Eric Dumazet602dd622015-12-01 07:20:07 -08007936 .destroy = sctp_v6_destroy_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937 .shutdown = sctp_shutdown,
7938 .setsockopt = sctp_setsockopt,
7939 .getsockopt = sctp_getsockopt,
7940 .sendmsg = sctp_sendmsg,
7941 .recvmsg = sctp_recvmsg,
7942 .bind = sctp_bind,
7943 .backlog_rcv = sctp_backlog_rcv,
7944 .hash = sctp_hash,
7945 .unhash = sctp_unhash,
7946 .get_port = sctp_get_port,
7947 .obj_size = sizeof(struct sctp6_sock),
Neil Horman4d93df02007-08-15 16:07:44 -07007948 .sysctl_mem = sysctl_sctp_mem,
7949 .sysctl_rmem = sysctl_sctp_rmem,
7950 .sysctl_wmem = sysctl_sctp_wmem,
7951 .memory_pressure = &sctp_memory_pressure,
7952 .enter_memory_pressure = sctp_enter_memory_pressure,
7953 .memory_allocated = &sctp_memory_allocated,
Pavel Emelyanov5f318862008-02-20 00:23:01 -08007954 .sockets_allocated = &sctp_sockets_allocated,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007955};
Eric Dumazetdfd56b82011-12-10 09:48:31 +00007956#endif /* IS_ENABLED(CONFIG_IPV6) */