blob: 95f9068b8549721f922bb31a80e7fd7b2d52d9e7 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/* SCTP kernel implementation
2 * (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 *
9 * This file is part of the SCTP kernel implementation
10 *
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 *
18 * This SCTP implementation is free software;
19 * 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 *
24 * This SCTP implementation is distributed in the hope that it
25 * 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
31 * along with GNU CC; see the file COPYING. If not, see
32 * <http://www.gnu.org/licenses/>.
33 *
34 * Please send any bug reports or fixes you make to the
35 * email address(es):
36 * lksctp developers <linux-sctp@vger.kernel.org>
37 *
38 * 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>
51 */
52
53#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
55#include <crypto/hash.h>
56#include <linux/types.h>
57#include <linux/kernel.h>
58#include <linux/wait.h>
59#include <linux/time.h>
60#include <linux/sched/signal.h>
61#include <linux/ip.h>
62#include <linux/capability.h>
63#include <linux/fcntl.h>
64#include <linux/poll.h>
65#include <linux/init.h>
66#include <linux/slab.h>
67#include <linux/file.h>
68#include <linux/compat.h>
69#include <linux/rhashtable.h>
70
71#include <net/ip.h>
72#include <net/icmp.h>
73#include <net/route.h>
74#include <net/ipv6.h>
75#include <net/inet_common.h>
76#include <net/busy_poll.h>
77
78#include <linux/socket.h> /* for sa_family_t */
79#include <linux/export.h>
80#include <net/sock.h>
81#include <net/sctp/sctp.h>
82#include <net/sctp/sm.h>
83#include <net/sctp/stream_sched.h>
84
85/* Forward declarations for internal helper functions. */
86static bool sctp_writeable(struct sock *sk);
87static void sctp_wfree(struct sk_buff *skb);
88static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
89 size_t msg_len);
90static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
91static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
92static int sctp_wait_for_accept(struct sock *sk, long timeo);
93static void sctp_wait_for_close(struct sock *sk, long timeo);
94static void sctp_destruct_sock(struct sock *sk);
95static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
96 union sctp_addr *addr, int len);
97static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
98static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
99static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
100static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
101static int sctp_send_asconf(struct sctp_association *asoc,
102 struct sctp_chunk *chunk);
103static int sctp_do_bind(struct sock *, union sctp_addr *, int);
104static int sctp_autobind(struct sock *sk);
105static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
106 struct sctp_association *assoc,
107 enum sctp_socket_type type);
108
109static unsigned long sctp_memory_pressure;
110static atomic_long_t sctp_memory_allocated;
111struct percpu_counter sctp_sockets_allocated;
112
113static void sctp_enter_memory_pressure(struct sock *sk)
114{
115 sctp_memory_pressure = 1;
116}
117
118
119/* Get the sndbuf space available at the time on the association. */
120static inline int sctp_wspace(struct sctp_association *asoc)
121{
122 struct sock *sk = asoc->base.sk;
123
124 return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used
125 : sk_stream_wspace(sk);
126}
127
128/* Increment the used sndbuf space count of the corresponding association by
129 * the size of the outgoing data chunk.
130 * Also, set the skb destructor for sndbuf accounting later.
131 *
132 * Since it is always 1-1 between chunk and skb, and also a new skb is always
133 * allocated for chunk bundling in sctp_packet_transmit(), we can use the
134 * destructor in the data chunk skb for the purpose of the sndbuf space
135 * tracking.
136 */
137static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
138{
139 struct sctp_association *asoc = chunk->asoc;
140 struct sock *sk = asoc->base.sk;
141
142 /* The sndbuf space is tracked per association. */
143 sctp_association_hold(asoc);
144
145 if (chunk->shkey)
146 sctp_auth_shkey_hold(chunk->shkey);
147
148 skb_set_owner_w(chunk->skb, sk);
149
150 chunk->skb->destructor = sctp_wfree;
151 /* Save the chunk pointer in skb for sctp_wfree to use later. */
152 skb_shinfo(chunk->skb)->destructor_arg = chunk;
153
154 asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
155 sizeof(struct sk_buff) +
156 sizeof(struct sctp_chunk);
157
158 refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
159 sk->sk_wmem_queued += chunk->skb->truesize;
160 sk_mem_charge(sk, chunk->skb->truesize);
161}
162
163static void sctp_clear_owner_w(struct sctp_chunk *chunk)
164{
165 skb_orphan(chunk->skb);
166}
167
168static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
169 void (*cb)(struct sctp_chunk *))
170
171{
172 struct sctp_outq *q = &asoc->outqueue;
173 struct sctp_transport *t;
174 struct sctp_chunk *chunk;
175
176 list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
177 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
178 cb(chunk);
179
180 list_for_each_entry(chunk, &q->retransmit, transmitted_list)
181 cb(chunk);
182
183 list_for_each_entry(chunk, &q->sacked, transmitted_list)
184 cb(chunk);
185
186 list_for_each_entry(chunk, &q->abandoned, transmitted_list)
187 cb(chunk);
188
189 list_for_each_entry(chunk, &q->out_chunk_list, list)
190 cb(chunk);
191}
192
193static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
194 void (*cb)(struct sk_buff *, struct sock *))
195
196{
197 struct sk_buff *skb, *tmp;
198
199 sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
200 cb(skb, sk);
201
202 sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
203 cb(skb, sk);
204
205 sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
206 cb(skb, sk);
207}
208
209/* Verify that this is a valid address. */
210static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
211 int len)
212{
213 struct sctp_af *af;
214
215 /* Verify basic sockaddr. */
216 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
217 if (!af)
218 return -EINVAL;
219
220 /* Is this a valid SCTP address? */
221 if (!af->addr_valid(addr, sctp_sk(sk), NULL))
222 return -EINVAL;
223
224 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
225 return -EINVAL;
226
227 return 0;
228}
229
230/* Look up the association by its id. If this is not a UDP-style
231 * socket, the ID field is always ignored.
232 */
233struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
234{
235 struct sctp_association *asoc = NULL;
236
237 /* If this is not a UDP-style socket, assoc id should be ignored. */
238 if (!sctp_style(sk, UDP)) {
239 /* Return NULL if the socket state is not ESTABLISHED. It
240 * could be a TCP-style listening socket or a socket which
241 * hasn't yet called connect() to establish an association.
242 */
243 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
244 return NULL;
245
246 /* Get the first and the only association from the list. */
247 if (!list_empty(&sctp_sk(sk)->ep->asocs))
248 asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
249 struct sctp_association, asocs);
250 return asoc;
251 }
252
253 /* Otherwise this is a UDP-style socket. */
254 if (!id || (id == (sctp_assoc_t)-1))
255 return NULL;
256
257 spin_lock_bh(&sctp_assocs_id_lock);
258 asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
259 if (asoc && (asoc->base.sk != sk || asoc->base.dead))
260 asoc = NULL;
261 spin_unlock_bh(&sctp_assocs_id_lock);
262
263 return asoc;
264}
265
266/* Look up the transport from an address and an assoc id. If both address and
267 * id are specified, the associations matching the address and the id should be
268 * the same.
269 */
270static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
271 struct sockaddr_storage *addr,
272 sctp_assoc_t id)
273{
274 struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
275 struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
276 union sctp_addr *laddr = (union sctp_addr *)addr;
277 struct sctp_transport *transport;
278
279 if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
280 return NULL;
281
282 addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
283 laddr,
284 &transport);
285
286 if (!addr_asoc)
287 return NULL;
288
289 id_asoc = sctp_id2assoc(sk, id);
290 if (id_asoc && (id_asoc != addr_asoc))
291 return NULL;
292
293 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
294 (union sctp_addr *)addr);
295
296 return transport;
297}
298
299/* API 3.1.2 bind() - UDP Style Syntax
300 * The syntax of bind() is,
301 *
302 * ret = bind(int sd, struct sockaddr *addr, int addrlen);
303 *
304 * sd - the socket descriptor returned by socket().
305 * addr - the address structure (struct sockaddr_in or struct
306 * sockaddr_in6 [RFC 2553]),
307 * addr_len - the size of the address structure.
308 */
309static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
310{
311 int retval = 0;
312
313 lock_sock(sk);
314
315 pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
316 addr, addr_len);
317
318 /* Disallow binding twice. */
319 if (!sctp_sk(sk)->ep->base.bind_addr.port)
320 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
321 addr_len);
322 else
323 retval = -EINVAL;
324
325 release_sock(sk);
326
327 return retval;
328}
329
330static long sctp_get_port_local(struct sock *, union sctp_addr *);
331
332/* Verify this is a valid sockaddr. */
333static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
334 union sctp_addr *addr, int len)
335{
336 struct sctp_af *af;
337
338 /* Check minimum size. */
339 if (len < sizeof (struct sockaddr))
340 return NULL;
341
342 if (!opt->pf->af_supported(addr->sa.sa_family, opt))
343 return NULL;
344
345 if (addr->sa.sa_family == AF_INET6) {
346 if (len < SIN6_LEN_RFC2133)
347 return NULL;
348 /* V4 mapped address are really of AF_INET family */
349 if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
350 !opt->pf->af_supported(AF_INET, opt))
351 return NULL;
352 }
353
354 /* If we get this far, af is valid. */
355 af = sctp_get_af_specific(addr->sa.sa_family);
356
357 if (len < af->sockaddr_len)
358 return NULL;
359
360 return af;
361}
362
363/* Bind a local address either to an endpoint or to an association. */
364static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
365{
366 struct net *net = sock_net(sk);
367 struct sctp_sock *sp = sctp_sk(sk);
368 struct sctp_endpoint *ep = sp->ep;
369 struct sctp_bind_addr *bp = &ep->base.bind_addr;
370 struct sctp_af *af;
371 unsigned short snum;
372 int ret = 0;
373
374 /* Common sockaddr verification. */
375 af = sctp_sockaddr_af(sp, addr, len);
376 if (!af) {
377 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
378 __func__, sk, addr, len);
379 return -EINVAL;
380 }
381
382 snum = ntohs(addr->v4.sin_port);
383
384 pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
385 __func__, sk, &addr->sa, bp->port, snum, len);
386
387 /* PF specific bind() address verification. */
388 if (!sp->pf->bind_verify(sp, addr))
389 return -EADDRNOTAVAIL;
390
391 /* We must either be unbound, or bind to the same port.
392 * It's OK to allow 0 ports if we are already bound.
393 * We'll just inhert an already bound port in this case
394 */
395 if (bp->port) {
396 if (!snum)
397 snum = bp->port;
398 else if (snum != bp->port) {
399 pr_debug("%s: new port %d doesn't match existing port "
400 "%d\n", __func__, snum, bp->port);
401 return -EINVAL;
402 }
403 }
404
405 if (snum && snum < inet_prot_sock(net) &&
406 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
407 return -EACCES;
408
409 /* See if the address matches any of the addresses we may have
410 * already bound before checking against other endpoints.
411 */
412 if (sctp_bind_addr_match(bp, addr, sp))
413 return -EINVAL;
414
415 /* Make sure we are allowed to bind here.
416 * The function sctp_get_port_local() does duplicate address
417 * detection.
418 */
419 addr->v4.sin_port = htons(snum);
420 if ((ret = sctp_get_port_local(sk, addr))) {
421 return -EADDRINUSE;
422 }
423
424 /* Refresh ephemeral port. */
425 if (!bp->port)
426 bp->port = inet_sk(sk)->inet_num;
427
428 /* Add the address to the bind address list.
429 * Use GFP_ATOMIC since BHs will be disabled.
430 */
431 ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
432 SCTP_ADDR_SRC, GFP_ATOMIC);
433
434 /* Copy back into socket for getsockname() use. */
435 if (!ret) {
436 inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
437 sp->pf->to_sk_saddr(addr, sk);
438 }
439
440 return ret;
441}
442
443 /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
444 *
445 * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
446 * at any one time. If a sender, after sending an ASCONF chunk, decides
447 * it needs to transfer another ASCONF Chunk, it MUST wait until the
448 * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
449 * subsequent ASCONF. Note this restriction binds each side, so at any
450 * time two ASCONF may be in-transit on any given association (one sent
451 * from each endpoint).
452 */
453static int sctp_send_asconf(struct sctp_association *asoc,
454 struct sctp_chunk *chunk)
455{
456 struct net *net = sock_net(asoc->base.sk);
457 int retval = 0;
458
459 /* If there is an outstanding ASCONF chunk, queue it for later
460 * transmission.
461 */
462 if (asoc->addip_last_asconf) {
463 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
464 goto out;
465 }
466
467 /* Hold the chunk until an ASCONF_ACK is received. */
468 sctp_chunk_hold(chunk);
469 retval = sctp_primitive_ASCONF(net, asoc, chunk);
470 if (retval)
471 sctp_chunk_free(chunk);
472 else
473 asoc->addip_last_asconf = chunk;
474
475out:
476 return retval;
477}
478
479/* Add a list of addresses as bind addresses to local endpoint or
480 * association.
481 *
482 * Basically run through each address specified in the addrs/addrcnt
483 * array/length pair, determine if it is IPv6 or IPv4 and call
484 * sctp_do_bind() on it.
485 *
486 * If any of them fails, then the operation will be reversed and the
487 * ones that were added will be removed.
488 *
489 * Only sctp_setsockopt_bindx() is supposed to call this function.
490 */
491static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
492{
493 int cnt;
494 int retval = 0;
495 void *addr_buf;
496 struct sockaddr *sa_addr;
497 struct sctp_af *af;
498
499 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
500 addrs, addrcnt);
501
502 addr_buf = addrs;
503 for (cnt = 0; cnt < addrcnt; cnt++) {
504 /* The list may contain either IPv4 or IPv6 address;
505 * determine the address length for walking thru the list.
506 */
507 sa_addr = addr_buf;
508 af = sctp_get_af_specific(sa_addr->sa_family);
509 if (!af) {
510 retval = -EINVAL;
511 goto err_bindx_add;
512 }
513
514 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
515 af->sockaddr_len);
516
517 addr_buf += af->sockaddr_len;
518
519err_bindx_add:
520 if (retval < 0) {
521 /* Failed. Cleanup the ones that have been added */
522 if (cnt > 0)
523 sctp_bindx_rem(sk, addrs, cnt);
524 return retval;
525 }
526 }
527
528 return retval;
529}
530
531/* Send an ASCONF chunk with Add IP address parameters to all the peers of the
532 * associations that are part of the endpoint indicating that a list of local
533 * addresses are added to the endpoint.
534 *
535 * If any of the addresses is already in the bind address list of the
536 * association, we do not send the chunk for that association. But it will not
537 * affect other associations.
538 *
539 * Only sctp_setsockopt_bindx() is supposed to call this function.
540 */
541static int sctp_send_asconf_add_ip(struct sock *sk,
542 struct sockaddr *addrs,
543 int addrcnt)
544{
545 struct net *net = sock_net(sk);
546 struct sctp_sock *sp;
547 struct sctp_endpoint *ep;
548 struct sctp_association *asoc;
549 struct sctp_bind_addr *bp;
550 struct sctp_chunk *chunk;
551 struct sctp_sockaddr_entry *laddr;
552 union sctp_addr *addr;
553 union sctp_addr saveaddr;
554 void *addr_buf;
555 struct sctp_af *af;
556 struct list_head *p;
557 int i;
558 int retval = 0;
559
560 if (!net->sctp.addip_enable)
561 return retval;
562
563 sp = sctp_sk(sk);
564 ep = sp->ep;
565
566 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
567 __func__, sk, addrs, addrcnt);
568
569 list_for_each_entry(asoc, &ep->asocs, asocs) {
570 if (!asoc->peer.asconf_capable)
571 continue;
572
573 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
574 continue;
575
576 if (!sctp_state(asoc, ESTABLISHED))
577 continue;
578
579 /* Check if any address in the packed array of addresses is
580 * in the bind address list of the association. If so,
581 * do not send the asconf chunk to its peer, but continue with
582 * other associations.
583 */
584 addr_buf = addrs;
585 for (i = 0; i < addrcnt; i++) {
586 addr = addr_buf;
587 af = sctp_get_af_specific(addr->v4.sin_family);
588 if (!af) {
589 retval = -EINVAL;
590 goto out;
591 }
592
593 if (sctp_assoc_lookup_laddr(asoc, addr))
594 break;
595
596 addr_buf += af->sockaddr_len;
597 }
598 if (i < addrcnt)
599 continue;
600
601 /* Use the first valid address in bind addr list of
602 * association as Address Parameter of ASCONF CHUNK.
603 */
604 bp = &asoc->base.bind_addr;
605 p = bp->address_list.next;
606 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
607 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
608 addrcnt, SCTP_PARAM_ADD_IP);
609 if (!chunk) {
610 retval = -ENOMEM;
611 goto out;
612 }
613
614 /* Add the new addresses to the bind address list with
615 * use_as_src set to 0.
616 */
617 addr_buf = addrs;
618 for (i = 0; i < addrcnt; i++) {
619 addr = addr_buf;
620 af = sctp_get_af_specific(addr->v4.sin_family);
621 memcpy(&saveaddr, addr, af->sockaddr_len);
622 retval = sctp_add_bind_addr(bp, &saveaddr,
623 sizeof(saveaddr),
624 SCTP_ADDR_NEW, GFP_ATOMIC);
625 addr_buf += af->sockaddr_len;
626 }
627 if (asoc->src_out_of_asoc_ok) {
628 struct sctp_transport *trans;
629
630 list_for_each_entry(trans,
631 &asoc->peer.transport_addr_list, transports) {
632 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
633 2*asoc->pathmtu, 4380));
634 trans->ssthresh = asoc->peer.i.a_rwnd;
635 trans->rto = asoc->rto_initial;
636 sctp_max_rto(asoc, trans);
637 trans->rtt = trans->srtt = trans->rttvar = 0;
638 /* Clear the source and route cache */
639 sctp_transport_route(trans, NULL,
640 sctp_sk(asoc->base.sk));
641 }
642 }
643 retval = sctp_send_asconf(asoc, chunk);
644 }
645
646out:
647 return retval;
648}
649
650/* Remove a list of addresses from bind addresses list. Do not remove the
651 * last address.
652 *
653 * Basically run through each address specified in the addrs/addrcnt
654 * array/length pair, determine if it is IPv6 or IPv4 and call
655 * sctp_del_bind() on it.
656 *
657 * If any of them fails, then the operation will be reversed and the
658 * ones that were removed will be added back.
659 *
660 * At least one address has to be left; if only one address is
661 * available, the operation will return -EBUSY.
662 *
663 * Only sctp_setsockopt_bindx() is supposed to call this function.
664 */
665static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
666{
667 struct sctp_sock *sp = sctp_sk(sk);
668 struct sctp_endpoint *ep = sp->ep;
669 int cnt;
670 struct sctp_bind_addr *bp = &ep->base.bind_addr;
671 int retval = 0;
672 void *addr_buf;
673 union sctp_addr *sa_addr;
674 struct sctp_af *af;
675
676 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
677 __func__, sk, addrs, addrcnt);
678
679 addr_buf = addrs;
680 for (cnt = 0; cnt < addrcnt; cnt++) {
681 /* If the bind address list is empty or if there is only one
682 * bind address, there is nothing more to be removed (we need
683 * at least one address here).
684 */
685 if (list_empty(&bp->address_list) ||
686 (sctp_list_single_entry(&bp->address_list))) {
687 retval = -EBUSY;
688 goto err_bindx_rem;
689 }
690
691 sa_addr = addr_buf;
692 af = sctp_get_af_specific(sa_addr->sa.sa_family);
693 if (!af) {
694 retval = -EINVAL;
695 goto err_bindx_rem;
696 }
697
698 if (!af->addr_valid(sa_addr, sp, NULL)) {
699 retval = -EADDRNOTAVAIL;
700 goto err_bindx_rem;
701 }
702
703 if (sa_addr->v4.sin_port &&
704 sa_addr->v4.sin_port != htons(bp->port)) {
705 retval = -EINVAL;
706 goto err_bindx_rem;
707 }
708
709 if (!sa_addr->v4.sin_port)
710 sa_addr->v4.sin_port = htons(bp->port);
711
712 /* FIXME - There is probably a need to check if sk->sk_saddr and
713 * sk->sk_rcv_addr are currently set to one of the addresses to
714 * be removed. This is something which needs to be looked into
715 * when we are fixing the outstanding issues with multi-homing
716 * socket routing and failover schemes. Refer to comments in
717 * sctp_do_bind(). -daisy
718 */
719 retval = sctp_del_bind_addr(bp, sa_addr);
720
721 addr_buf += af->sockaddr_len;
722err_bindx_rem:
723 if (retval < 0) {
724 /* Failed. Add the ones that has been removed back */
725 if (cnt > 0)
726 sctp_bindx_add(sk, addrs, cnt);
727 return retval;
728 }
729 }
730
731 return retval;
732}
733
734/* Send an ASCONF chunk with Delete IP address parameters to all the peers of
735 * the associations that are part of the endpoint indicating that a list of
736 * local addresses are removed from the endpoint.
737 *
738 * If any of the addresses is already in the bind address list of the
739 * association, we do not send the chunk for that association. But it will not
740 * affect other associations.
741 *
742 * Only sctp_setsockopt_bindx() is supposed to call this function.
743 */
744static int sctp_send_asconf_del_ip(struct sock *sk,
745 struct sockaddr *addrs,
746 int addrcnt)
747{
748 struct net *net = sock_net(sk);
749 struct sctp_sock *sp;
750 struct sctp_endpoint *ep;
751 struct sctp_association *asoc;
752 struct sctp_transport *transport;
753 struct sctp_bind_addr *bp;
754 struct sctp_chunk *chunk;
755 union sctp_addr *laddr;
756 void *addr_buf;
757 struct sctp_af *af;
758 struct sctp_sockaddr_entry *saddr;
759 int i;
760 int retval = 0;
761 int stored = 0;
762
763 chunk = NULL;
764 if (!net->sctp.addip_enable)
765 return retval;
766
767 sp = sctp_sk(sk);
768 ep = sp->ep;
769
770 pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
771 __func__, sk, addrs, addrcnt);
772
773 list_for_each_entry(asoc, &ep->asocs, asocs) {
774
775 if (!asoc->peer.asconf_capable)
776 continue;
777
778 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
779 continue;
780
781 if (!sctp_state(asoc, ESTABLISHED))
782 continue;
783
784 /* Check if any address in the packed array of addresses is
785 * not present in the bind address list of the association.
786 * If so, do not send the asconf chunk to its peer, but
787 * continue with other associations.
788 */
789 addr_buf = addrs;
790 for (i = 0; i < addrcnt; i++) {
791 laddr = addr_buf;
792 af = sctp_get_af_specific(laddr->v4.sin_family);
793 if (!af) {
794 retval = -EINVAL;
795 goto out;
796 }
797
798 if (!sctp_assoc_lookup_laddr(asoc, laddr))
799 break;
800
801 addr_buf += af->sockaddr_len;
802 }
803 if (i < addrcnt)
804 continue;
805
806 /* Find one address in the association's bind address list
807 * that is not in the packed array of addresses. This is to
808 * make sure that we do not delete all the addresses in the
809 * association.
810 */
811 bp = &asoc->base.bind_addr;
812 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
813 addrcnt, sp);
814 if ((laddr == NULL) && (addrcnt == 1)) {
815 if (asoc->asconf_addr_del_pending)
816 continue;
817 asoc->asconf_addr_del_pending =
818 kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
819 if (asoc->asconf_addr_del_pending == NULL) {
820 retval = -ENOMEM;
821 goto out;
822 }
823 asoc->asconf_addr_del_pending->sa.sa_family =
824 addrs->sa_family;
825 asoc->asconf_addr_del_pending->v4.sin_port =
826 htons(bp->port);
827 if (addrs->sa_family == AF_INET) {
828 struct sockaddr_in *sin;
829
830 sin = (struct sockaddr_in *)addrs;
831 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
832 } else if (addrs->sa_family == AF_INET6) {
833 struct sockaddr_in6 *sin6;
834
835 sin6 = (struct sockaddr_in6 *)addrs;
836 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
837 }
838
839 pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
840 __func__, asoc, &asoc->asconf_addr_del_pending->sa,
841 asoc->asconf_addr_del_pending);
842
843 asoc->src_out_of_asoc_ok = 1;
844 stored = 1;
845 goto skip_mkasconf;
846 }
847
848 if (laddr == NULL)
849 return -EINVAL;
850
851 /* We do not need RCU protection throughout this loop
852 * because this is done under a socket lock from the
853 * setsockopt call.
854 */
855 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
856 SCTP_PARAM_DEL_IP);
857 if (!chunk) {
858 retval = -ENOMEM;
859 goto out;
860 }
861
862skip_mkasconf:
863 /* Reset use_as_src flag for the addresses in the bind address
864 * list that are to be deleted.
865 */
866 addr_buf = addrs;
867 for (i = 0; i < addrcnt; i++) {
868 laddr = addr_buf;
869 af = sctp_get_af_specific(laddr->v4.sin_family);
870 list_for_each_entry(saddr, &bp->address_list, list) {
871 if (sctp_cmp_addr_exact(&saddr->a, laddr))
872 saddr->state = SCTP_ADDR_DEL;
873 }
874 addr_buf += af->sockaddr_len;
875 }
876
877 /* Update the route and saddr entries for all the transports
878 * as some of the addresses in the bind address list are
879 * about to be deleted and cannot be used as source addresses.
880 */
881 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
882 transports) {
883 sctp_transport_route(transport, NULL,
884 sctp_sk(asoc->base.sk));
885 }
886
887 if (stored)
888 /* We don't need to transmit ASCONF */
889 continue;
890 retval = sctp_send_asconf(asoc, chunk);
891 }
892out:
893 return retval;
894}
895
896/* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
897int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
898{
899 struct sock *sk = sctp_opt2sk(sp);
900 union sctp_addr *addr;
901 struct sctp_af *af;
902
903 /* It is safe to write port space in caller. */
904 addr = &addrw->a;
905 addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
906 af = sctp_get_af_specific(addr->sa.sa_family);
907 if (!af)
908 return -EINVAL;
909 if (sctp_verify_addr(sk, addr, af->sockaddr_len))
910 return -EINVAL;
911
912 if (addrw->state == SCTP_ADDR_NEW)
913 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
914 else
915 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
916}
917
918/* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
919 *
920 * API 8.1
921 * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
922 * int flags);
923 *
924 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
925 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
926 * or IPv6 addresses.
927 *
928 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
929 * Section 3.1.2 for this usage.
930 *
931 * addrs is a pointer to an array of one or more socket addresses. Each
932 * address is contained in its appropriate structure (i.e. struct
933 * sockaddr_in or struct sockaddr_in6) the family of the address type
934 * must be used to distinguish the address length (note that this
935 * representation is termed a "packed array" of addresses). The caller
936 * specifies the number of addresses in the array with addrcnt.
937 *
938 * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
939 * -1, and sets errno to the appropriate error code.
940 *
941 * For SCTP, the port given in each socket address must be the same, or
942 * sctp_bindx() will fail, setting errno to EINVAL.
943 *
944 * The flags parameter is formed from the bitwise OR of zero or more of
945 * the following currently defined flags:
946 *
947 * SCTP_BINDX_ADD_ADDR
948 *
949 * SCTP_BINDX_REM_ADDR
950 *
951 * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
952 * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
953 * addresses from the association. The two flags are mutually exclusive;
954 * if both are given, sctp_bindx() will fail with EINVAL. A caller may
955 * not remove all addresses from an association; sctp_bindx() will
956 * reject such an attempt with EINVAL.
957 *
958 * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
959 * additional addresses with an endpoint after calling bind(). Or use
960 * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
961 * socket is associated with so that no new association accepted will be
962 * associated with those addresses. If the endpoint supports dynamic
963 * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
964 * endpoint to send the appropriate message to the peer to change the
965 * peers address lists.
966 *
967 * Adding and removing addresses from a connected association is
968 * optional functionality. Implementations that do not support this
969 * functionality should return EOPNOTSUPP.
970 *
971 * Basically do nothing but copying the addresses from user to kernel
972 * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
973 * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
974 * from userspace.
975 *
976 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
977 * it.
978 *
979 * sk The sk of the socket
980 * addrs The pointer to the addresses in user land
981 * addrssize Size of the addrs buffer
982 * op Operation to perform (add or remove, see the flags of
983 * sctp_bindx)
984 *
985 * Returns 0 if ok, <0 errno code on error.
986 */
987static int sctp_setsockopt_bindx(struct sock *sk,
988 struct sockaddr __user *addrs,
989 int addrs_size, int op)
990{
991 struct sockaddr *kaddrs;
992 int err;
993 int addrcnt = 0;
994 int walk_size = 0;
995 struct sockaddr *sa_addr;
996 void *addr_buf;
997 struct sctp_af *af;
998
999 pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1000 __func__, sk, addrs, addrs_size, op);
1001
1002 if (unlikely(addrs_size <= 0))
1003 return -EINVAL;
1004
1005 kaddrs = memdup_user(addrs, addrs_size);
1006 if (unlikely(IS_ERR(kaddrs)))
1007 return PTR_ERR(kaddrs);
1008
1009 /* Walk through the addrs buffer and count the number of addresses. */
1010 addr_buf = kaddrs;
1011 while (walk_size < addrs_size) {
1012 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1013 kfree(kaddrs);
1014 return -EINVAL;
1015 }
1016
1017 sa_addr = addr_buf;
1018 af = sctp_get_af_specific(sa_addr->sa_family);
1019
1020 /* If the address family is not supported or if this address
1021 * causes the address buffer to overflow return EINVAL.
1022 */
1023 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1024 kfree(kaddrs);
1025 return -EINVAL;
1026 }
1027 addrcnt++;
1028 addr_buf += af->sockaddr_len;
1029 walk_size += af->sockaddr_len;
1030 }
1031
1032 /* Do the work. */
1033 switch (op) {
1034 case SCTP_BINDX_ADD_ADDR:
1035 /* Allow security module to validate bindx addresses. */
1036 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1037 (struct sockaddr *)kaddrs,
1038 addrs_size);
1039 if (err)
1040 goto out;
1041 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1042 if (err)
1043 goto out;
1044 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1045 break;
1046
1047 case SCTP_BINDX_REM_ADDR:
1048 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1049 if (err)
1050 goto out;
1051 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1052 break;
1053
1054 default:
1055 err = -EINVAL;
1056 break;
1057 }
1058
1059out:
1060 kfree(kaddrs);
1061
1062 return err;
1063}
1064
1065/* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1066 *
1067 * Common routine for handling connect() and sctp_connectx().
1068 * Connect will come in with just a single address.
1069 */
1070static int __sctp_connect(struct sock *sk,
1071 struct sockaddr *kaddrs,
1072 int addrs_size, int flags,
1073 sctp_assoc_t *assoc_id)
1074{
1075 struct net *net = sock_net(sk);
1076 struct sctp_sock *sp;
1077 struct sctp_endpoint *ep;
1078 struct sctp_association *asoc = NULL;
1079 struct sctp_association *asoc2;
1080 struct sctp_transport *transport;
1081 union sctp_addr to;
1082 enum sctp_scope scope;
1083 long timeo;
1084 int err = 0;
1085 int addrcnt = 0;
1086 int walk_size = 0;
1087 union sctp_addr *sa_addr = NULL;
1088 void *addr_buf;
1089 unsigned short port;
1090
1091 sp = sctp_sk(sk);
1092 ep = sp->ep;
1093
1094 /* connect() cannot be done on a socket that is already in ESTABLISHED
1095 * state - UDP-style peeled off socket or a TCP-style socket that
1096 * is already connected.
1097 * It cannot be done even on a TCP-style listening socket.
1098 */
1099 if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
1100 (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
1101 err = -EISCONN;
1102 goto out_free;
1103 }
1104
1105 /* Walk through the addrs buffer and count the number of addresses. */
1106 addr_buf = kaddrs;
1107 while (walk_size < addrs_size) {
1108 struct sctp_af *af;
1109
1110 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1111 err = -EINVAL;
1112 goto out_free;
1113 }
1114
1115 sa_addr = addr_buf;
1116 af = sctp_get_af_specific(sa_addr->sa.sa_family);
1117
1118 /* If the address family is not supported or if this address
1119 * causes the address buffer to overflow return EINVAL.
1120 */
1121 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1122 err = -EINVAL;
1123 goto out_free;
1124 }
1125
1126 port = ntohs(sa_addr->v4.sin_port);
1127
1128 /* Save current address so we can work with it */
1129 memcpy(&to, sa_addr, af->sockaddr_len);
1130
1131 err = sctp_verify_addr(sk, &to, af->sockaddr_len);
1132 if (err)
1133 goto out_free;
1134
1135 /* Make sure the destination port is correctly set
1136 * in all addresses.
1137 */
1138 if (asoc && asoc->peer.port && asoc->peer.port != port) {
1139 err = -EINVAL;
1140 goto out_free;
1141 }
1142
1143 /* Check if there already is a matching association on the
1144 * endpoint (other than the one created here).
1145 */
1146 asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
1147 if (asoc2 && asoc2 != asoc) {
1148 if (asoc2->state >= SCTP_STATE_ESTABLISHED)
1149 err = -EISCONN;
1150 else
1151 err = -EALREADY;
1152 goto out_free;
1153 }
1154
1155 /* If we could not find a matching association on the endpoint,
1156 * make sure that there is no peeled-off association matching
1157 * the peer address even on another socket.
1158 */
1159 if (sctp_endpoint_is_peeled_off(ep, &to)) {
1160 err = -EADDRNOTAVAIL;
1161 goto out_free;
1162 }
1163
1164 if (!asoc) {
1165 /* If a bind() or sctp_bindx() is not called prior to
1166 * an sctp_connectx() call, the system picks an
1167 * ephemeral port and will choose an address set
1168 * equivalent to binding with a wildcard address.
1169 */
1170 if (!ep->base.bind_addr.port) {
1171 if (sctp_autobind(sk)) {
1172 err = -EAGAIN;
1173 goto out_free;
1174 }
1175 } else {
1176 /*
1177 * If an unprivileged user inherits a 1-many
1178 * style socket with open associations on a
1179 * privileged port, it MAY be permitted to
1180 * accept new associations, but it SHOULD NOT
1181 * be permitted to open new associations.
1182 */
1183 if (ep->base.bind_addr.port <
1184 inet_prot_sock(net) &&
1185 !ns_capable(net->user_ns,
1186 CAP_NET_BIND_SERVICE)) {
1187 err = -EACCES;
1188 goto out_free;
1189 }
1190 }
1191
1192 scope = sctp_scope(&to);
1193 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1194 if (!asoc) {
1195 err = -ENOMEM;
1196 goto out_free;
1197 }
1198
1199 err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
1200 GFP_KERNEL);
1201 if (err < 0) {
1202 goto out_free;
1203 }
1204
1205 }
1206
1207 /* Prime the peer's transport structures. */
1208 transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
1209 SCTP_UNKNOWN);
1210 if (!transport) {
1211 err = -ENOMEM;
1212 goto out_free;
1213 }
1214
1215 addrcnt++;
1216 addr_buf += af->sockaddr_len;
1217 walk_size += af->sockaddr_len;
1218 }
1219
1220 /* In case the user of sctp_connectx() wants an association
1221 * id back, assign one now.
1222 */
1223 if (assoc_id) {
1224 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1225 if (err < 0)
1226 goto out_free;
1227 }
1228
1229 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1230 if (err < 0) {
1231 goto out_free;
1232 }
1233
1234 /* Initialize sk's dport and daddr for getpeername() */
1235 inet_sk(sk)->inet_dport = htons(asoc->peer.port);
1236 sp->pf->to_sk_daddr(sa_addr, sk);
1237 sk->sk_err = 0;
1238
1239 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1240
1241 if (assoc_id)
1242 *assoc_id = asoc->assoc_id;
1243
1244 err = sctp_wait_for_connect(asoc, &timeo);
1245 /* Note: the asoc may be freed after the return of
1246 * sctp_wait_for_connect.
1247 */
1248
1249 /* Don't free association on exit. */
1250 asoc = NULL;
1251
1252out_free:
1253 pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1254 __func__, asoc, kaddrs, err);
1255
1256 if (asoc) {
1257 /* sctp_primitive_ASSOCIATE may have added this association
1258 * To the hash table, try to unhash it, just in case, its a noop
1259 * if it wasn't hashed so we're safe
1260 */
1261 sctp_association_free(asoc);
1262 }
1263 return err;
1264}
1265
1266/* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1267 *
1268 * API 8.9
1269 * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1270 * sctp_assoc_t *asoc);
1271 *
1272 * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1273 * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1274 * or IPv6 addresses.
1275 *
1276 * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1277 * Section 3.1.2 for this usage.
1278 *
1279 * addrs is a pointer to an array of one or more socket addresses. Each
1280 * address is contained in its appropriate structure (i.e. struct
1281 * sockaddr_in or struct sockaddr_in6) the family of the address type
1282 * must be used to distengish the address length (note that this
1283 * representation is termed a "packed array" of addresses). The caller
1284 * specifies the number of addresses in the array with addrcnt.
1285 *
1286 * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1287 * the association id of the new association. On failure, sctp_connectx()
1288 * returns -1, and sets errno to the appropriate error code. The assoc_id
1289 * is not touched by the kernel.
1290 *
1291 * For SCTP, the port given in each socket address must be the same, or
1292 * sctp_connectx() will fail, setting errno to EINVAL.
1293 *
1294 * An application can use sctp_connectx to initiate an association with
1295 * an endpoint that is multi-homed. Much like sctp_bindx() this call
1296 * allows a caller to specify multiple addresses at which a peer can be
1297 * reached. The way the SCTP stack uses the list of addresses to set up
1298 * the association is implementation dependent. This function only
1299 * specifies that the stack will try to make use of all the addresses in
1300 * the list when needed.
1301 *
1302 * Note that the list of addresses passed in is only used for setting up
1303 * the association. It does not necessarily equal the set of addresses
1304 * the peer uses for the resulting association. If the caller wants to
1305 * find out the set of peer addresses, it must use sctp_getpaddrs() to
1306 * retrieve them after the association has been set up.
1307 *
1308 * Basically do nothing but copying the addresses from user to kernel
1309 * land and invoking either sctp_connectx(). This is used for tunneling
1310 * the sctp_connectx() request through sctp_setsockopt() from userspace.
1311 *
1312 * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1313 * it.
1314 *
1315 * sk The sk of the socket
1316 * addrs The pointer to the addresses in user land
1317 * addrssize Size of the addrs buffer
1318 *
1319 * Returns >=0 if ok, <0 errno code on error.
1320 */
1321static int __sctp_setsockopt_connectx(struct sock *sk,
1322 struct sockaddr __user *addrs,
1323 int addrs_size,
1324 sctp_assoc_t *assoc_id)
1325{
1326 struct sockaddr *kaddrs;
1327 int err = 0, flags = 0;
1328
1329 pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1330 __func__, sk, addrs, addrs_size);
1331
1332 if (unlikely(addrs_size <= 0))
1333 return -EINVAL;
1334
1335 kaddrs = memdup_user(addrs, addrs_size);
1336 if (unlikely(IS_ERR(kaddrs)))
1337 return PTR_ERR(kaddrs);
1338
1339 /* Allow security module to validate connectx addresses. */
1340 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1341 (struct sockaddr *)kaddrs,
1342 addrs_size);
1343 if (err)
1344 goto out_free;
1345
1346 /* in-kernel sockets don't generally have a file allocated to them
1347 * if all they do is call sock_create_kern().
1348 */
1349 if (sk->sk_socket->file)
1350 flags = sk->sk_socket->file->f_flags;
1351
1352 err = __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
1353
1354out_free:
1355 kfree(kaddrs);
1356
1357 return err;
1358}
1359
1360/*
1361 * This is an older interface. It's kept for backward compatibility
1362 * to the option that doesn't provide association id.
1363 */
1364static int sctp_setsockopt_connectx_old(struct sock *sk,
1365 struct sockaddr __user *addrs,
1366 int addrs_size)
1367{
1368 return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1369}
1370
1371/*
1372 * New interface for the API. The since the API is done with a socket
1373 * option, to make it simple we feed back the association id is as a return
1374 * indication to the call. Error is always negative and association id is
1375 * always positive.
1376 */
1377static int sctp_setsockopt_connectx(struct sock *sk,
1378 struct sockaddr __user *addrs,
1379 int addrs_size)
1380{
1381 sctp_assoc_t assoc_id = 0;
1382 int err = 0;
1383
1384 err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1385
1386 if (err)
1387 return err;
1388 else
1389 return assoc_id;
1390}
1391
1392/*
1393 * New (hopefully final) interface for the API.
1394 * We use the sctp_getaddrs_old structure so that use-space library
1395 * can avoid any unnecessary allocations. The only different part
1396 * is that we store the actual length of the address buffer into the
1397 * addrs_num structure member. That way we can re-use the existing
1398 * code.
1399 */
1400#ifdef CONFIG_COMPAT
1401struct compat_sctp_getaddrs_old {
1402 sctp_assoc_t assoc_id;
1403 s32 addr_num;
1404 compat_uptr_t addrs; /* struct sockaddr * */
1405};
1406#endif
1407
1408static int sctp_getsockopt_connectx3(struct sock *sk, int len,
1409 char __user *optval,
1410 int __user *optlen)
1411{
1412 struct sctp_getaddrs_old param;
1413 sctp_assoc_t assoc_id = 0;
1414 int err = 0;
1415
1416#ifdef CONFIG_COMPAT
1417 if (in_compat_syscall()) {
1418 struct compat_sctp_getaddrs_old param32;
1419
1420 if (len < sizeof(param32))
1421 return -EINVAL;
1422 if (copy_from_user(&param32, optval, sizeof(param32)))
1423 return -EFAULT;
1424
1425 param.assoc_id = param32.assoc_id;
1426 param.addr_num = param32.addr_num;
1427 param.addrs = compat_ptr(param32.addrs);
1428 } else
1429#endif
1430 {
1431 if (len < sizeof(param))
1432 return -EINVAL;
1433 if (copy_from_user(&param, optval, sizeof(param)))
1434 return -EFAULT;
1435 }
1436
1437 err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1438 param.addrs, param.addr_num,
1439 &assoc_id);
1440 if (err == 0 || err == -EINPROGRESS) {
1441 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1442 return -EFAULT;
1443 if (put_user(sizeof(assoc_id), optlen))
1444 return -EFAULT;
1445 }
1446
1447 return err;
1448}
1449
1450/* API 3.1.4 close() - UDP Style Syntax
1451 * Applications use close() to perform graceful shutdown (as described in
1452 * Section 10.1 of [SCTP]) on ALL the associations currently represented
1453 * by a UDP-style socket.
1454 *
1455 * The syntax is
1456 *
1457 * ret = close(int sd);
1458 *
1459 * sd - the socket descriptor of the associations to be closed.
1460 *
1461 * To gracefully shutdown a specific association represented by the
1462 * UDP-style socket, an application should use the sendmsg() call,
1463 * passing no user data, but including the appropriate flag in the
1464 * ancillary data (see Section xxxx).
1465 *
1466 * If sd in the close() call is a branched-off socket representing only
1467 * one association, the shutdown is performed on that association only.
1468 *
1469 * 4.1.6 close() - TCP Style Syntax
1470 *
1471 * Applications use close() to gracefully close down an association.
1472 *
1473 * The syntax is:
1474 *
1475 * int close(int sd);
1476 *
1477 * sd - the socket descriptor of the association to be closed.
1478 *
1479 * After an application calls close() on a socket descriptor, no further
1480 * socket operations will succeed on that descriptor.
1481 *
1482 * API 7.1.4 SO_LINGER
1483 *
1484 * An application using the TCP-style socket can use this option to
1485 * perform the SCTP ABORT primitive. The linger option structure is:
1486 *
1487 * struct linger {
1488 * int l_onoff; // option on/off
1489 * int l_linger; // linger time
1490 * };
1491 *
1492 * To enable the option, set l_onoff to 1. If the l_linger value is set
1493 * to 0, calling close() is the same as the ABORT primitive. If the
1494 * value is set to a negative value, the setsockopt() call will return
1495 * an error. If the value is set to a positive value linger_time, the
1496 * close() can be blocked for at most linger_time ms. If the graceful
1497 * shutdown phase does not finish during this period, close() will
1498 * return but the graceful shutdown phase continues in the system.
1499 */
1500static void sctp_close(struct sock *sk, long timeout)
1501{
1502 struct net *net = sock_net(sk);
1503 struct sctp_endpoint *ep;
1504 struct sctp_association *asoc;
1505 struct list_head *pos, *temp;
1506 unsigned int data_was_unread;
1507
1508 pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
1509
1510 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1511 sk->sk_shutdown = SHUTDOWN_MASK;
1512 inet_sk_set_state(sk, SCTP_SS_CLOSING);
1513
1514 ep = sctp_sk(sk)->ep;
1515
1516 /* Clean up any skbs sitting on the receive queue. */
1517 data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1518 data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1519
1520 /* Walk all associations on an endpoint. */
1521 list_for_each_safe(pos, temp, &ep->asocs) {
1522 asoc = list_entry(pos, struct sctp_association, asocs);
1523
1524 if (sctp_style(sk, TCP)) {
1525 /* A closed association can still be in the list if
1526 * it belongs to a TCP-style listening socket that is
1527 * not yet accepted. If so, free it. If not, send an
1528 * ABORT or SHUTDOWN based on the linger options.
1529 */
1530 if (sctp_state(asoc, CLOSED)) {
1531 sctp_association_free(asoc);
1532 continue;
1533 }
1534 }
1535
1536 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1537 !skb_queue_empty(&asoc->ulpq.reasm) ||
1538 !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
1539 (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
1540 struct sctp_chunk *chunk;
1541
1542 chunk = sctp_make_abort_user(asoc, NULL, 0);
1543 sctp_primitive_ABORT(net, asoc, chunk);
1544 } else
1545 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1546 }
1547
1548 /* On a TCP-style socket, block for at most linger_time if set. */
1549 if (sctp_style(sk, TCP) && timeout)
1550 sctp_wait_for_close(sk, timeout);
1551
1552 /* This will run the backlog queue. */
1553 release_sock(sk);
1554
1555 /* Supposedly, no process has access to the socket, but
1556 * the net layers still may.
1557 * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1558 * held and that should be grabbed before socket lock.
1559 */
1560 spin_lock_bh(&net->sctp.addr_wq_lock);
1561 bh_lock_sock_nested(sk);
1562
1563 /* Hold the sock, since sk_common_release() will put sock_put()
1564 * and we have just a little more cleanup.
1565 */
1566 sock_hold(sk);
1567 sk_common_release(sk);
1568
1569 bh_unlock_sock(sk);
1570 spin_unlock_bh(&net->sctp.addr_wq_lock);
1571
1572 sock_put(sk);
1573
1574 SCTP_DBG_OBJCNT_DEC(sock);
1575}
1576
1577/* Handle EPIPE error. */
1578static int sctp_error(struct sock *sk, int flags, int err)
1579{
1580 if (err == -EPIPE)
1581 err = sock_error(sk) ? : -EPIPE;
1582 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1583 send_sig(SIGPIPE, current, 0);
1584 return err;
1585}
1586
1587/* API 3.1.3 sendmsg() - UDP Style Syntax
1588 *
1589 * An application uses sendmsg() and recvmsg() calls to transmit data to
1590 * and receive data from its peer.
1591 *
1592 * ssize_t sendmsg(int socket, const struct msghdr *message,
1593 * int flags);
1594 *
1595 * socket - the socket descriptor of the endpoint.
1596 * message - pointer to the msghdr structure which contains a single
1597 * user message and possibly some ancillary data.
1598 *
1599 * See Section 5 for complete description of the data
1600 * structures.
1601 *
1602 * flags - flags sent or received with the user message, see Section
1603 * 5 for complete description of the flags.
1604 *
1605 * Note: This function could use a rewrite especially when explicit
1606 * connect support comes in.
1607 */
1608/* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1609
1610static int sctp_msghdr_parse(const struct msghdr *msg,
1611 struct sctp_cmsgs *cmsgs);
1612
1613static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1614 struct sctp_sndrcvinfo *srinfo,
1615 const struct msghdr *msg, size_t msg_len)
1616{
1617 __u16 sflags;
1618 int err;
1619
1620 if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1621 return -EPIPE;
1622
1623 if (msg_len > sk->sk_sndbuf)
1624 return -EMSGSIZE;
1625
1626 memset(cmsgs, 0, sizeof(*cmsgs));
1627 err = sctp_msghdr_parse(msg, cmsgs);
1628 if (err) {
1629 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1630 return err;
1631 }
1632
1633 memset(srinfo, 0, sizeof(*srinfo));
1634 if (cmsgs->srinfo) {
1635 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1636 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1637 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1638 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1639 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1640 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1641 }
1642
1643 if (cmsgs->sinfo) {
1644 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1645 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1646 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1647 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1648 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1649 }
1650
1651 if (cmsgs->prinfo) {
1652 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1653 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1654 cmsgs->prinfo->pr_policy);
1655 }
1656
1657 sflags = srinfo->sinfo_flags;
1658 if (!sflags && msg_len)
1659 return 0;
1660
1661 if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1662 return -EINVAL;
1663
1664 if (((sflags & SCTP_EOF) && msg_len > 0) ||
1665 (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1666 return -EINVAL;
1667
1668 if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1669 return -EINVAL;
1670
1671 return 0;
1672}
1673
1674static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1675 struct sctp_cmsgs *cmsgs,
1676 union sctp_addr *daddr,
1677 struct sctp_transport **tp)
1678{
1679 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1680 struct net *net = sock_net(sk);
1681 struct sctp_association *asoc;
1682 enum sctp_scope scope;
1683 struct cmsghdr *cmsg;
1684 __be32 flowinfo = 0;
1685 struct sctp_af *af;
1686 int err;
1687
1688 *tp = NULL;
1689
1690 if (sflags & (SCTP_EOF | SCTP_ABORT))
1691 return -EINVAL;
1692
1693 if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1694 sctp_sstate(sk, CLOSING)))
1695 return -EADDRNOTAVAIL;
1696
1697 if (sctp_endpoint_is_peeled_off(ep, daddr))
1698 return -EADDRNOTAVAIL;
1699
1700 if (!ep->base.bind_addr.port) {
1701 if (sctp_autobind(sk))
1702 return -EAGAIN;
1703 } else {
1704 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1705 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1706 return -EACCES;
1707 }
1708
1709 scope = sctp_scope(daddr);
1710
1711 /* Label connection socket for first association 1-to-many
1712 * style for client sequence socket()->sendmsg(). This
1713 * needs to be done before sctp_assoc_add_peer() as that will
1714 * set up the initial packet that needs to account for any
1715 * security ip options (CIPSO/CALIPSO) added to the packet.
1716 */
1717 af = sctp_get_af_specific(daddr->sa.sa_family);
1718 if (!af)
1719 return -EINVAL;
1720 err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1721 (struct sockaddr *)daddr,
1722 af->sockaddr_len);
1723 if (err < 0)
1724 return err;
1725
1726 asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1727 if (!asoc)
1728 return -ENOMEM;
1729
1730 if (sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL) < 0) {
1731 err = -ENOMEM;
1732 goto free;
1733 }
1734
1735 if (cmsgs->init) {
1736 struct sctp_initmsg *init = cmsgs->init;
1737
1738 if (init->sinit_num_ostreams) {
1739 __u16 outcnt = init->sinit_num_ostreams;
1740
1741 asoc->c.sinit_num_ostreams = outcnt;
1742 /* outcnt has been changed, need to re-init stream */
1743 err = sctp_stream_init(&asoc->stream, outcnt, 0,
1744 GFP_KERNEL);
1745 if (err)
1746 goto free;
1747 }
1748
1749 if (init->sinit_max_instreams)
1750 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1751
1752 if (init->sinit_max_attempts)
1753 asoc->max_init_attempts = init->sinit_max_attempts;
1754
1755 if (init->sinit_max_init_timeo)
1756 asoc->max_init_timeo =
1757 msecs_to_jiffies(init->sinit_max_init_timeo);
1758 }
1759
1760 *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1761 if (!*tp) {
1762 err = -ENOMEM;
1763 goto free;
1764 }
1765
1766 if (!cmsgs->addrs_msg)
1767 return 0;
1768
1769 if (daddr->sa.sa_family == AF_INET6)
1770 flowinfo = daddr->v6.sin6_flowinfo;
1771
1772 /* sendv addr list parse */
1773 for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1774 struct sctp_transport *transport;
1775 struct sctp_association *old;
1776 union sctp_addr _daddr;
1777 int dlen;
1778
1779 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1780 (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1781 cmsg->cmsg_type != SCTP_DSTADDRV6))
1782 continue;
1783
1784 daddr = &_daddr;
1785 memset(daddr, 0, sizeof(*daddr));
1786 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1787 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
1788 if (dlen < sizeof(struct in_addr)) {
1789 err = -EINVAL;
1790 goto free;
1791 }
1792
1793 dlen = sizeof(struct in_addr);
1794 daddr->v4.sin_family = AF_INET;
1795 daddr->v4.sin_port = htons(asoc->peer.port);
1796 memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1797 } else {
1798 if (dlen < sizeof(struct in6_addr)) {
1799 err = -EINVAL;
1800 goto free;
1801 }
1802
1803 dlen = sizeof(struct in6_addr);
1804 daddr->v6.sin6_flowinfo = flowinfo;
1805 daddr->v6.sin6_family = AF_INET6;
1806 daddr->v6.sin6_port = htons(asoc->peer.port);
1807 memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1808 }
1809 err = sctp_verify_addr(sk, daddr, sizeof(*daddr));
1810 if (err)
1811 goto free;
1812
1813 old = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1814 if (old && old != asoc) {
1815 if (old->state >= SCTP_STATE_ESTABLISHED)
1816 err = -EISCONN;
1817 else
1818 err = -EALREADY;
1819 goto free;
1820 }
1821
1822 if (sctp_endpoint_is_peeled_off(ep, daddr)) {
1823 err = -EADDRNOTAVAIL;
1824 goto free;
1825 }
1826
1827 transport = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL,
1828 SCTP_UNKNOWN);
1829 if (!transport) {
1830 err = -ENOMEM;
1831 goto free;
1832 }
1833 }
1834
1835 return 0;
1836
1837free:
1838 sctp_association_free(asoc);
1839 return err;
1840}
1841
1842static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1843 __u16 sflags, struct msghdr *msg,
1844 size_t msg_len)
1845{
1846 struct sock *sk = asoc->base.sk;
1847 struct net *net = sock_net(sk);
1848
1849 if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1850 return -EPIPE;
1851
1852 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1853 !sctp_state(asoc, ESTABLISHED))
1854 return 0;
1855
1856 if (sflags & SCTP_EOF) {
1857 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1858 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1859
1860 return 0;
1861 }
1862
1863 if (sflags & SCTP_ABORT) {
1864 struct sctp_chunk *chunk;
1865
1866 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1867 if (!chunk)
1868 return -ENOMEM;
1869
1870 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1871 sctp_primitive_ABORT(net, asoc, chunk);
1872 iov_iter_revert(&msg->msg_iter, msg_len);
1873
1874 return 0;
1875 }
1876
1877 return 1;
1878}
1879
1880static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1881 struct msghdr *msg, size_t msg_len,
1882 struct sctp_transport *transport,
1883 struct sctp_sndrcvinfo *sinfo)
1884{
1885 struct sock *sk = asoc->base.sk;
1886 struct sctp_sock *sp = sctp_sk(sk);
1887 struct net *net = sock_net(sk);
1888 struct sctp_datamsg *datamsg;
1889 bool wait_connect = false;
1890 struct sctp_chunk *chunk;
1891 long timeo;
1892 int err;
1893
1894 if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1895 err = -EINVAL;
1896 goto err;
1897 }
1898
1899 if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
1900 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1901 if (err)
1902 goto err;
1903 }
1904
1905 if (sp->disable_fragments && msg_len > asoc->frag_point) {
1906 err = -EMSGSIZE;
1907 goto err;
1908 }
1909
1910 if (asoc->pmtu_pending) {
1911 if (sp->param_flags & SPP_PMTUD_ENABLE)
1912 sctp_assoc_sync_pmtu(asoc);
1913 asoc->pmtu_pending = 0;
1914 }
1915
1916 if (sctp_wspace(asoc) < (int)msg_len)
1917 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1918
1919 if (sctp_wspace(asoc) <= 0) {
1920 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1921 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1922 if (err)
1923 goto err;
1924 }
1925
1926 if (sctp_state(asoc, CLOSED)) {
1927 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1928 if (err)
1929 goto err;
1930
1931 if (sp->strm_interleave) {
1932 timeo = sock_sndtimeo(sk, 0);
1933 err = sctp_wait_for_connect(asoc, &timeo);
1934 if (err) {
1935 err = -ESRCH;
1936 goto err;
1937 }
1938 } else {
1939 wait_connect = true;
1940 }
1941
1942 pr_debug("%s: we associated primitively\n", __func__);
1943 }
1944
1945 datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1946 if (IS_ERR(datamsg)) {
1947 err = PTR_ERR(datamsg);
1948 goto err;
1949 }
1950
1951 asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1952
1953 list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1954 sctp_chunk_hold(chunk);
1955 sctp_set_owner_w(chunk);
1956 chunk->transport = transport;
1957 }
1958
1959 err = sctp_primitive_SEND(net, asoc, datamsg);
1960 if (err) {
1961 sctp_datamsg_free(datamsg);
1962 goto err;
1963 }
1964
1965 pr_debug("%s: we sent primitively\n", __func__);
1966
1967 sctp_datamsg_put(datamsg);
1968
1969 if (unlikely(wait_connect)) {
1970 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1971 sctp_wait_for_connect(asoc, &timeo);
1972 }
1973
1974 err = msg_len;
1975
1976err:
1977 return err;
1978}
1979
1980static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1981 const struct msghdr *msg,
1982 struct sctp_cmsgs *cmsgs)
1983{
1984 union sctp_addr *daddr = NULL;
1985 int err;
1986
1987 if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1988 int len = msg->msg_namelen;
1989
1990 if (len > sizeof(*daddr))
1991 len = sizeof(*daddr);
1992
1993 daddr = (union sctp_addr *)msg->msg_name;
1994
1995 err = sctp_verify_addr(sk, daddr, len);
1996 if (err)
1997 return ERR_PTR(err);
1998 }
1999
2000 return daddr;
2001}
2002
2003static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
2004 struct sctp_sndrcvinfo *sinfo,
2005 struct sctp_cmsgs *cmsgs)
2006{
2007 if (!cmsgs->srinfo && !cmsgs->sinfo) {
2008 sinfo->sinfo_stream = asoc->default_stream;
2009 sinfo->sinfo_ppid = asoc->default_ppid;
2010 sinfo->sinfo_context = asoc->default_context;
2011 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
2012
2013 if (!cmsgs->prinfo)
2014 sinfo->sinfo_flags = asoc->default_flags;
2015 }
2016
2017 if (!cmsgs->srinfo && !cmsgs->prinfo)
2018 sinfo->sinfo_timetolive = asoc->default_timetolive;
2019
2020 if (cmsgs->authinfo) {
2021 /* Reuse sinfo_tsn to indicate that authinfo was set and
2022 * sinfo_ssn to save the keyid on tx path.
2023 */
2024 sinfo->sinfo_tsn = 1;
2025 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
2026 }
2027}
2028
2029static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
2030{
2031 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
2032 struct sctp_transport *transport = NULL;
2033 struct sctp_sndrcvinfo _sinfo, *sinfo;
2034 struct sctp_association *asoc, *tmp;
2035 struct sctp_cmsgs cmsgs;
2036 union sctp_addr *daddr;
2037 bool new = false;
2038 __u16 sflags;
2039 int err;
2040
2041 /* Parse and get snd_info */
2042 err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
2043 if (err)
2044 goto out;
2045
2046 sinfo = &_sinfo;
2047 sflags = sinfo->sinfo_flags;
2048
2049 /* Get daddr from msg */
2050 daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
2051 if (IS_ERR(daddr)) {
2052 err = PTR_ERR(daddr);
2053 goto out;
2054 }
2055
2056 lock_sock(sk);
2057
2058 /* SCTP_SENDALL process */
2059 if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
2060 list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
2061 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2062 msg_len);
2063 if (err == 0)
2064 continue;
2065 if (err < 0)
2066 goto out_unlock;
2067
2068 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2069
2070 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
2071 NULL, sinfo);
2072 if (err < 0)
2073 goto out_unlock;
2074
2075 iov_iter_revert(&msg->msg_iter, err);
2076 }
2077
2078 goto out_unlock;
2079 }
2080
2081 /* Get and check or create asoc */
2082 if (daddr) {
2083 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
2084 if (asoc) {
2085 err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2086 msg_len);
2087 if (err <= 0)
2088 goto out_unlock;
2089 } else {
2090 err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2091 &transport);
2092 if (err)
2093 goto out_unlock;
2094
2095 asoc = transport->asoc;
2096 new = true;
2097 }
2098
2099 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2100 transport = NULL;
2101 } else {
2102 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
2103 if (!asoc) {
2104 err = -EPIPE;
2105 goto out_unlock;
2106 }
2107
2108 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
2109 if (err <= 0)
2110 goto out_unlock;
2111 }
2112
2113 /* Update snd_info with the asoc */
2114 sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2115
2116 /* Send msg to the asoc */
2117 err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
2118 if (err < 0 && err != -ESRCH && new)
2119 sctp_association_free(asoc);
2120
2121out_unlock:
2122 release_sock(sk);
2123out:
2124 return sctp_error(sk, msg->msg_flags, err);
2125}
2126
2127/* This is an extended version of skb_pull() that removes the data from the
2128 * start of a skb even when data is spread across the list of skb's in the
2129 * frag_list. len specifies the total amount of data that needs to be removed.
2130 * when 'len' bytes could be removed from the skb, it returns 0.
2131 * If 'len' exceeds the total skb length, it returns the no. of bytes that
2132 * could not be removed.
2133 */
2134static int sctp_skb_pull(struct sk_buff *skb, int len)
2135{
2136 struct sk_buff *list;
2137 int skb_len = skb_headlen(skb);
2138 int rlen;
2139
2140 if (len <= skb_len) {
2141 __skb_pull(skb, len);
2142 return 0;
2143 }
2144 len -= skb_len;
2145 __skb_pull(skb, skb_len);
2146
2147 skb_walk_frags(skb, list) {
2148 rlen = sctp_skb_pull(list, len);
2149 skb->len -= (len-rlen);
2150 skb->data_len -= (len-rlen);
2151
2152 if (!rlen)
2153 return 0;
2154
2155 len = rlen;
2156 }
2157
2158 return len;
2159}
2160
2161/* API 3.1.3 recvmsg() - UDP Style Syntax
2162 *
2163 * ssize_t recvmsg(int socket, struct msghdr *message,
2164 * int flags);
2165 *
2166 * socket - the socket descriptor of the endpoint.
2167 * message - pointer to the msghdr structure which contains a single
2168 * user message and possibly some ancillary data.
2169 *
2170 * See Section 5 for complete description of the data
2171 * structures.
2172 *
2173 * flags - flags sent or received with the user message, see Section
2174 * 5 for complete description of the flags.
2175 */
2176static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2177 int noblock, int flags, int *addr_len)
2178{
2179 struct sctp_ulpevent *event = NULL;
2180 struct sctp_sock *sp = sctp_sk(sk);
2181 struct sk_buff *skb, *head_skb;
2182 int copied;
2183 int err = 0;
2184 int skb_len;
2185
2186 pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2187 "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2188 addr_len);
2189
2190 lock_sock(sk);
2191
2192 if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
2193 !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
2194 err = -ENOTCONN;
2195 goto out;
2196 }
2197
2198 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2199 if (!skb)
2200 goto out;
2201
2202 /* Get the total length of the skb including any skb's in the
2203 * frag_list.
2204 */
2205 skb_len = skb->len;
2206
2207 copied = skb_len;
2208 if (copied > len)
2209 copied = len;
2210
2211 err = skb_copy_datagram_msg(skb, 0, msg, copied);
2212
2213 event = sctp_skb2event(skb);
2214
2215 if (err)
2216 goto out_free;
2217
2218 if (event->chunk && event->chunk->head_skb)
2219 head_skb = event->chunk->head_skb;
2220 else
2221 head_skb = skb;
2222 sock_recv_ts_and_drops(msg, sk, head_skb);
2223 if (sctp_ulpevent_is_notification(event)) {
2224 msg->msg_flags |= MSG_NOTIFICATION;
2225 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2226 } else {
2227 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
2228 }
2229
2230 /* Check if we allow SCTP_NXTINFO. */
2231 if (sp->recvnxtinfo)
2232 sctp_ulpevent_read_nxtinfo(event, msg, sk);
2233 /* Check if we allow SCTP_RCVINFO. */
2234 if (sp->recvrcvinfo)
2235 sctp_ulpevent_read_rcvinfo(event, msg);
2236 /* Check if we allow SCTP_SNDRCVINFO. */
2237 if (sp->subscribe.sctp_data_io_event)
2238 sctp_ulpevent_read_sndrcvinfo(event, msg);
2239
2240 err = copied;
2241
2242 /* If skb's length exceeds the user's buffer, update the skb and
2243 * push it back to the receive_queue so that the next call to
2244 * recvmsg() will return the remaining data. Don't set MSG_EOR.
2245 */
2246 if (skb_len > copied) {
2247 msg->msg_flags &= ~MSG_EOR;
2248 if (flags & MSG_PEEK)
2249 goto out_free;
2250 sctp_skb_pull(skb, copied);
2251 skb_queue_head(&sk->sk_receive_queue, skb);
2252
2253 /* When only partial message is copied to the user, increase
2254 * rwnd by that amount. If all the data in the skb is read,
2255 * rwnd is updated when the event is freed.
2256 */
2257 if (!sctp_ulpevent_is_notification(event))
2258 sctp_assoc_rwnd_increase(event->asoc, copied);
2259 goto out;
2260 } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2261 (event->msg_flags & MSG_EOR))
2262 msg->msg_flags |= MSG_EOR;
2263 else
2264 msg->msg_flags &= ~MSG_EOR;
2265
2266out_free:
2267 if (flags & MSG_PEEK) {
2268 /* Release the skb reference acquired after peeking the skb in
2269 * sctp_skb_recv_datagram().
2270 */
2271 kfree_skb(skb);
2272 } else {
2273 /* Free the event which includes releasing the reference to
2274 * the owner of the skb, freeing the skb and updating the
2275 * rwnd.
2276 */
2277 sctp_ulpevent_free(event);
2278 }
2279out:
2280 release_sock(sk);
2281 return err;
2282}
2283
2284/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2285 *
2286 * This option is a on/off flag. If enabled no SCTP message
2287 * fragmentation will be performed. Instead if a message being sent
2288 * exceeds the current PMTU size, the message will NOT be sent and
2289 * instead a error will be indicated to the user.
2290 */
2291static int sctp_setsockopt_disable_fragments(struct sock *sk,
2292 char __user *optval,
2293 unsigned int optlen)
2294{
2295 int val;
2296
2297 if (optlen < sizeof(int))
2298 return -EINVAL;
2299
2300 if (get_user(val, (int __user *)optval))
2301 return -EFAULT;
2302
2303 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2304
2305 return 0;
2306}
2307
2308static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
2309 unsigned int optlen)
2310{
2311 struct sctp_association *asoc;
2312 struct sctp_ulpevent *event;
2313
2314 if (optlen > sizeof(struct sctp_event_subscribe))
2315 return -EINVAL;
2316 if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
2317 return -EFAULT;
2318
2319 /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2320 * if there is no data to be sent or retransmit, the stack will
2321 * immediately send up this notification.
2322 */
2323 if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
2324 &sctp_sk(sk)->subscribe)) {
2325 asoc = sctp_id2assoc(sk, 0);
2326
2327 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2328 event = sctp_ulpevent_make_sender_dry_event(asoc,
2329 GFP_USER | __GFP_NOWARN);
2330 if (!event)
2331 return -ENOMEM;
2332
2333 asoc->stream.si->enqueue_event(&asoc->ulpq, event);
2334 }
2335 }
2336
2337 return 0;
2338}
2339
2340/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2341 *
2342 * This socket option is applicable to the UDP-style socket only. When
2343 * set it will cause associations that are idle for more than the
2344 * specified number of seconds to automatically close. An association
2345 * being idle is defined an association that has NOT sent or received
2346 * user data. The special value of '0' indicates that no automatic
2347 * close of any associations should be performed. The option expects an
2348 * integer defining the number of seconds of idle time before an
2349 * association is closed.
2350 */
2351static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2352 unsigned int optlen)
2353{
2354 struct sctp_sock *sp = sctp_sk(sk);
2355 struct net *net = sock_net(sk);
2356
2357 /* Applicable to UDP-style socket only */
2358 if (sctp_style(sk, TCP))
2359 return -EOPNOTSUPP;
2360 if (optlen != sizeof(int))
2361 return -EINVAL;
2362 if (copy_from_user(&sp->autoclose, optval, optlen))
2363 return -EFAULT;
2364
2365 if (sp->autoclose > net->sctp.max_autoclose)
2366 sp->autoclose = net->sctp.max_autoclose;
2367
2368 return 0;
2369}
2370
2371/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2372 *
2373 * Applications can enable or disable heartbeats for any peer address of
2374 * an association, modify an address's heartbeat interval, force a
2375 * heartbeat to be sent immediately, and adjust the address's maximum
2376 * number of retransmissions sent before an address is considered
2377 * unreachable. The following structure is used to access and modify an
2378 * address's parameters:
2379 *
2380 * struct sctp_paddrparams {
2381 * sctp_assoc_t spp_assoc_id;
2382 * struct sockaddr_storage spp_address;
2383 * uint32_t spp_hbinterval;
2384 * uint16_t spp_pathmaxrxt;
2385 * uint32_t spp_pathmtu;
2386 * uint32_t spp_sackdelay;
2387 * uint32_t spp_flags;
2388 * uint32_t spp_ipv6_flowlabel;
2389 * uint8_t spp_dscp;
2390 * };
2391 *
2392 * spp_assoc_id - (one-to-many style socket) This is filled in the
2393 * application, and identifies the association for
2394 * this query.
2395 * spp_address - This specifies which address is of interest.
2396 * spp_hbinterval - This contains the value of the heartbeat interval,
2397 * in milliseconds. If a value of zero
2398 * is present in this field then no changes are to
2399 * be made to this parameter.
2400 * spp_pathmaxrxt - This contains the maximum number of
2401 * retransmissions before this address shall be
2402 * considered unreachable. If a value of zero
2403 * is present in this field then no changes are to
2404 * be made to this parameter.
2405 * spp_pathmtu - When Path MTU discovery is disabled the value
2406 * specified here will be the "fixed" path mtu.
2407 * Note that if the spp_address field is empty
2408 * then all associations on this address will
2409 * have this fixed path mtu set upon them.
2410 *
2411 * spp_sackdelay - When delayed sack is enabled, this value specifies
2412 * the number of milliseconds that sacks will be delayed
2413 * for. This value will apply to all addresses of an
2414 * association if the spp_address field is empty. Note
2415 * also, that if delayed sack is enabled and this
2416 * value is set to 0, no change is made to the last
2417 * recorded delayed sack timer value.
2418 *
2419 * spp_flags - These flags are used to control various features
2420 * on an association. The flag field may contain
2421 * zero or more of the following options.
2422 *
2423 * SPP_HB_ENABLE - Enable heartbeats on the
2424 * specified address. Note that if the address
2425 * field is empty all addresses for the association
2426 * have heartbeats enabled upon them.
2427 *
2428 * SPP_HB_DISABLE - Disable heartbeats on the
2429 * speicifed address. Note that if the address
2430 * field is empty all addresses for the association
2431 * will have their heartbeats disabled. Note also
2432 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2433 * mutually exclusive, only one of these two should
2434 * be specified. Enabling both fields will have
2435 * undetermined results.
2436 *
2437 * SPP_HB_DEMAND - Request a user initiated heartbeat
2438 * to be made immediately.
2439 *
2440 * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2441 * heartbeat delayis to be set to the value of 0
2442 * milliseconds.
2443 *
2444 * SPP_PMTUD_ENABLE - This field will enable PMTU
2445 * discovery upon the specified address. Note that
2446 * if the address feild is empty then all addresses
2447 * on the association are effected.
2448 *
2449 * SPP_PMTUD_DISABLE - This field will disable PMTU
2450 * discovery upon the specified address. Note that
2451 * if the address feild is empty then all addresses
2452 * on the association are effected. Not also that
2453 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2454 * exclusive. Enabling both will have undetermined
2455 * results.
2456 *
2457 * SPP_SACKDELAY_ENABLE - Setting this flag turns
2458 * on delayed sack. The time specified in spp_sackdelay
2459 * is used to specify the sack delay for this address. Note
2460 * that if spp_address is empty then all addresses will
2461 * enable delayed sack and take on the sack delay
2462 * value specified in spp_sackdelay.
2463 * SPP_SACKDELAY_DISABLE - Setting this flag turns
2464 * off delayed sack. If the spp_address field is blank then
2465 * delayed sack is disabled for the entire association. Note
2466 * also that this field is mutually exclusive to
2467 * SPP_SACKDELAY_ENABLE, setting both will have undefined
2468 * results.
2469 *
2470 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
2471 * setting of the IPV6 flow label value. The value is
2472 * contained in the spp_ipv6_flowlabel field.
2473 * Upon retrieval, this flag will be set to indicate that
2474 * the spp_ipv6_flowlabel field has a valid value returned.
2475 * If a specific destination address is set (in the
2476 * spp_address field), then the value returned is that of
2477 * the address. If just an association is specified (and
2478 * no address), then the association's default flow label
2479 * is returned. If neither an association nor a destination
2480 * is specified, then the socket's default flow label is
2481 * returned. For non-IPv6 sockets, this flag will be left
2482 * cleared.
2483 *
2484 * SPP_DSCP: Setting this flag enables the setting of the
2485 * Differentiated Services Code Point (DSCP) value
2486 * associated with either the association or a specific
2487 * address. The value is obtained in the spp_dscp field.
2488 * Upon retrieval, this flag will be set to indicate that
2489 * the spp_dscp field has a valid value returned. If a
2490 * specific destination address is set when called (in the
2491 * spp_address field), then that specific destination
2492 * address's DSCP value is returned. If just an association
2493 * is specified, then the association's default DSCP is
2494 * returned. If neither an association nor a destination is
2495 * specified, then the socket's default DSCP is returned.
2496 *
2497 * spp_ipv6_flowlabel
2498 * - This field is used in conjunction with the
2499 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
2500 * The 20 least significant bits are used for the flow
2501 * label. This setting has precedence over any IPv6-layer
2502 * setting.
2503 *
2504 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
2505 * and contains the DSCP. The 6 most significant bits are
2506 * used for the DSCP. This setting has precedence over any
2507 * IPv4- or IPv6- layer setting.
2508 */
2509static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2510 struct sctp_transport *trans,
2511 struct sctp_association *asoc,
2512 struct sctp_sock *sp,
2513 int hb_change,
2514 int pmtud_change,
2515 int sackdelay_change)
2516{
2517 int error;
2518
2519 if (params->spp_flags & SPP_HB_DEMAND && trans) {
2520 struct net *net = sock_net(trans->asoc->base.sk);
2521
2522 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
2523 if (error)
2524 return error;
2525 }
2526
2527 /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2528 * this field is ignored. Note also that a value of zero indicates
2529 * the current setting should be left unchanged.
2530 */
2531 if (params->spp_flags & SPP_HB_ENABLE) {
2532
2533 /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2534 * set. This lets us use 0 value when this flag
2535 * is set.
2536 */
2537 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2538 params->spp_hbinterval = 0;
2539
2540 if (params->spp_hbinterval ||
2541 (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2542 if (trans) {
2543 trans->hbinterval =
2544 msecs_to_jiffies(params->spp_hbinterval);
2545 } else if (asoc) {
2546 asoc->hbinterval =
2547 msecs_to_jiffies(params->spp_hbinterval);
2548 } else {
2549 sp->hbinterval = params->spp_hbinterval;
2550 }
2551 }
2552 }
2553
2554 if (hb_change) {
2555 if (trans) {
2556 trans->param_flags =
2557 (trans->param_flags & ~SPP_HB) | hb_change;
2558 } else if (asoc) {
2559 asoc->param_flags =
2560 (asoc->param_flags & ~SPP_HB) | hb_change;
2561 } else {
2562 sp->param_flags =
2563 (sp->param_flags & ~SPP_HB) | hb_change;
2564 }
2565 }
2566
2567 /* When Path MTU discovery is disabled the value specified here will
2568 * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2569 * include the flag SPP_PMTUD_DISABLE for this field to have any
2570 * effect).
2571 */
2572 if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2573 if (trans) {
2574 trans->pathmtu = params->spp_pathmtu;
2575 sctp_assoc_sync_pmtu(asoc);
2576 } else if (asoc) {
2577 sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
2578 } else {
2579 sp->pathmtu = params->spp_pathmtu;
2580 }
2581 }
2582
2583 if (pmtud_change) {
2584 if (trans) {
2585 int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2586 (params->spp_flags & SPP_PMTUD_ENABLE);
2587 trans->param_flags =
2588 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2589 if (update) {
2590 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
2591 sctp_assoc_sync_pmtu(asoc);
2592 }
2593 } else if (asoc) {
2594 asoc->param_flags =
2595 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2596 } else {
2597 sp->param_flags =
2598 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2599 }
2600 }
2601
2602 /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2603 * value of this field is ignored. Note also that a value of zero
2604 * indicates the current setting should be left unchanged.
2605 */
2606 if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2607 if (trans) {
2608 trans->sackdelay =
2609 msecs_to_jiffies(params->spp_sackdelay);
2610 } else if (asoc) {
2611 asoc->sackdelay =
2612 msecs_to_jiffies(params->spp_sackdelay);
2613 } else {
2614 sp->sackdelay = params->spp_sackdelay;
2615 }
2616 }
2617
2618 if (sackdelay_change) {
2619 if (trans) {
2620 trans->param_flags =
2621 (trans->param_flags & ~SPP_SACKDELAY) |
2622 sackdelay_change;
2623 } else if (asoc) {
2624 asoc->param_flags =
2625 (asoc->param_flags & ~SPP_SACKDELAY) |
2626 sackdelay_change;
2627 } else {
2628 sp->param_flags =
2629 (sp->param_flags & ~SPP_SACKDELAY) |
2630 sackdelay_change;
2631 }
2632 }
2633
2634 /* Note that a value of zero indicates the current setting should be
2635 left unchanged.
2636 */
2637 if (params->spp_pathmaxrxt) {
2638 if (trans) {
2639 trans->pathmaxrxt = params->spp_pathmaxrxt;
2640 } else if (asoc) {
2641 asoc->pathmaxrxt = params->spp_pathmaxrxt;
2642 } else {
2643 sp->pathmaxrxt = params->spp_pathmaxrxt;
2644 }
2645 }
2646
2647 if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
2648 if (trans) {
2649 if (trans->ipaddr.sa.sa_family == AF_INET6) {
2650 trans->flowlabel = params->spp_ipv6_flowlabel &
2651 SCTP_FLOWLABEL_VAL_MASK;
2652 trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2653 }
2654 } else if (asoc) {
2655 struct sctp_transport *t;
2656
2657 list_for_each_entry(t, &asoc->peer.transport_addr_list,
2658 transports) {
2659 if (t->ipaddr.sa.sa_family != AF_INET6)
2660 continue;
2661 t->flowlabel = params->spp_ipv6_flowlabel &
2662 SCTP_FLOWLABEL_VAL_MASK;
2663 t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2664 }
2665 asoc->flowlabel = params->spp_ipv6_flowlabel &
2666 SCTP_FLOWLABEL_VAL_MASK;
2667 asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2668 } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
2669 sp->flowlabel = params->spp_ipv6_flowlabel &
2670 SCTP_FLOWLABEL_VAL_MASK;
2671 sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2672 }
2673 }
2674
2675 if (params->spp_flags & SPP_DSCP) {
2676 if (trans) {
2677 trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2678 trans->dscp |= SCTP_DSCP_SET_MASK;
2679 } else if (asoc) {
2680 struct sctp_transport *t;
2681
2682 list_for_each_entry(t, &asoc->peer.transport_addr_list,
2683 transports) {
2684 t->dscp = params->spp_dscp &
2685 SCTP_DSCP_VAL_MASK;
2686 t->dscp |= SCTP_DSCP_SET_MASK;
2687 }
2688 asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2689 asoc->dscp |= SCTP_DSCP_SET_MASK;
2690 } else {
2691 sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2692 sp->dscp |= SCTP_DSCP_SET_MASK;
2693 }
2694 }
2695
2696 return 0;
2697}
2698
2699static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2700 char __user *optval,
2701 unsigned int optlen)
2702{
2703 struct sctp_paddrparams params;
2704 struct sctp_transport *trans = NULL;
2705 struct sctp_association *asoc = NULL;
2706 struct sctp_sock *sp = sctp_sk(sk);
2707 int error;
2708 int hb_change, pmtud_change, sackdelay_change;
2709
2710 if (optlen == sizeof(params)) {
2711 if (copy_from_user(&params, optval, optlen))
2712 return -EFAULT;
2713 } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
2714 spp_ipv6_flowlabel), 4)) {
2715 if (copy_from_user(&params, optval, optlen))
2716 return -EFAULT;
2717 if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
2718 return -EINVAL;
2719 } else {
2720 return -EINVAL;
2721 }
2722
2723 /* Validate flags and value parameters. */
2724 hb_change = params.spp_flags & SPP_HB;
2725 pmtud_change = params.spp_flags & SPP_PMTUD;
2726 sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2727
2728 if (hb_change == SPP_HB ||
2729 pmtud_change == SPP_PMTUD ||
2730 sackdelay_change == SPP_SACKDELAY ||
2731 params.spp_sackdelay > 500 ||
2732 (params.spp_pathmtu &&
2733 params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2734 return -EINVAL;
2735
2736 /* If an address other than INADDR_ANY is specified, and
2737 * no transport is found, then the request is invalid.
2738 */
2739 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
2740 trans = sctp_addr_id2transport(sk, &params.spp_address,
2741 params.spp_assoc_id);
2742 if (!trans)
2743 return -EINVAL;
2744 }
2745
2746 /* Get association, if assoc_id != 0 and the socket is a one
2747 * to many style socket, and an association was not found, then
2748 * the id was invalid.
2749 */
2750 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2751 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
2752 return -EINVAL;
2753
2754 /* Heartbeat demand can only be sent on a transport or
2755 * association, but not a socket.
2756 */
2757 if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2758 return -EINVAL;
2759
2760 /* Process parameters. */
2761 error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2762 hb_change, pmtud_change,
2763 sackdelay_change);
2764
2765 if (error)
2766 return error;
2767
2768 /* If changes are for association, also apply parameters to each
2769 * transport.
2770 */
2771 if (!trans && asoc) {
2772 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2773 transports) {
2774 sctp_apply_peer_addr_params(&params, trans, asoc, sp,
2775 hb_change, pmtud_change,
2776 sackdelay_change);
2777 }
2778 }
2779
2780 return 0;
2781}
2782
2783static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2784{
2785 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2786}
2787
2788static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2789{
2790 return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2791}
2792
2793/*
2794 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2795 *
2796 * This option will effect the way delayed acks are performed. This
2797 * option allows you to get or set the delayed ack time, in
2798 * milliseconds. It also allows changing the delayed ack frequency.
2799 * Changing the frequency to 1 disables the delayed sack algorithm. If
2800 * the assoc_id is 0, then this sets or gets the endpoints default
2801 * values. If the assoc_id field is non-zero, then the set or get
2802 * effects the specified association for the one to many model (the
2803 * assoc_id field is ignored by the one to one model). Note that if
2804 * sack_delay or sack_freq are 0 when setting this option, then the
2805 * current values will remain unchanged.
2806 *
2807 * struct sctp_sack_info {
2808 * sctp_assoc_t sack_assoc_id;
2809 * uint32_t sack_delay;
2810 * uint32_t sack_freq;
2811 * };
2812 *
2813 * sack_assoc_id - This parameter, indicates which association the user
2814 * is performing an action upon. Note that if this field's value is
2815 * zero then the endpoints default value is changed (effecting future
2816 * associations only).
2817 *
2818 * sack_delay - This parameter contains the number of milliseconds that
2819 * the user is requesting the delayed ACK timer be set to. Note that
2820 * this value is defined in the standard to be between 200 and 500
2821 * milliseconds.
2822 *
2823 * sack_freq - This parameter contains the number of packets that must
2824 * be received before a sack is sent without waiting for the delay
2825 * timer to expire. The default value for this is 2, setting this
2826 * value to 1 will disable the delayed sack algorithm.
2827 */
2828
2829static int sctp_setsockopt_delayed_ack(struct sock *sk,
2830 char __user *optval, unsigned int optlen)
2831{
2832 struct sctp_sack_info params;
2833 struct sctp_transport *trans = NULL;
2834 struct sctp_association *asoc = NULL;
2835 struct sctp_sock *sp = sctp_sk(sk);
2836
2837 if (optlen == sizeof(struct sctp_sack_info)) {
2838 if (copy_from_user(&params, optval, optlen))
2839 return -EFAULT;
2840
2841 if (params.sack_delay == 0 && params.sack_freq == 0)
2842 return 0;
2843 } else if (optlen == sizeof(struct sctp_assoc_value)) {
2844 pr_warn_ratelimited(DEPRECATED
2845 "%s (pid %d) "
2846 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
2847 "Use struct sctp_sack_info instead\n",
2848 current->comm, task_pid_nr(current));
2849 if (copy_from_user(&params, optval, optlen))
2850 return -EFAULT;
2851
2852 if (params.sack_delay == 0)
2853 params.sack_freq = 1;
2854 else
2855 params.sack_freq = 0;
2856 } else
2857 return -EINVAL;
2858
2859 /* Validate value parameter. */
2860 if (params.sack_delay > 500)
2861 return -EINVAL;
2862
2863 /* Get association, if sack_assoc_id != 0 and the socket is a one
2864 * to many style socket, and an association was not found, then
2865 * the id was invalid.
2866 */
2867 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2868 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
2869 return -EINVAL;
2870
2871 if (params.sack_delay) {
2872 if (asoc) {
2873 asoc->sackdelay =
2874 msecs_to_jiffies(params.sack_delay);
2875 asoc->param_flags =
2876 sctp_spp_sackdelay_enable(asoc->param_flags);
2877 } else {
2878 sp->sackdelay = params.sack_delay;
2879 sp->param_flags =
2880 sctp_spp_sackdelay_enable(sp->param_flags);
2881 }
2882 }
2883
2884 if (params.sack_freq == 1) {
2885 if (asoc) {
2886 asoc->param_flags =
2887 sctp_spp_sackdelay_disable(asoc->param_flags);
2888 } else {
2889 sp->param_flags =
2890 sctp_spp_sackdelay_disable(sp->param_flags);
2891 }
2892 } else if (params.sack_freq > 1) {
2893 if (asoc) {
2894 asoc->sackfreq = params.sack_freq;
2895 asoc->param_flags =
2896 sctp_spp_sackdelay_enable(asoc->param_flags);
2897 } else {
2898 sp->sackfreq = params.sack_freq;
2899 sp->param_flags =
2900 sctp_spp_sackdelay_enable(sp->param_flags);
2901 }
2902 }
2903
2904 /* If change is for association, also apply to each transport. */
2905 if (asoc) {
2906 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2907 transports) {
2908 if (params.sack_delay) {
2909 trans->sackdelay =
2910 msecs_to_jiffies(params.sack_delay);
2911 trans->param_flags =
2912 sctp_spp_sackdelay_enable(trans->param_flags);
2913 }
2914 if (params.sack_freq == 1) {
2915 trans->param_flags =
2916 sctp_spp_sackdelay_disable(trans->param_flags);
2917 } else if (params.sack_freq > 1) {
2918 trans->sackfreq = params.sack_freq;
2919 trans->param_flags =
2920 sctp_spp_sackdelay_enable(trans->param_flags);
2921 }
2922 }
2923 }
2924
2925 return 0;
2926}
2927
2928/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2929 *
2930 * Applications can specify protocol parameters for the default association
2931 * initialization. The option name argument to setsockopt() and getsockopt()
2932 * is SCTP_INITMSG.
2933 *
2934 * Setting initialization parameters is effective only on an unconnected
2935 * socket (for UDP-style sockets only future associations are effected
2936 * by the change). With TCP-style sockets, this option is inherited by
2937 * sockets derived from a listener socket.
2938 */
2939static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
2940{
2941 struct sctp_initmsg sinit;
2942 struct sctp_sock *sp = sctp_sk(sk);
2943
2944 if (optlen != sizeof(struct sctp_initmsg))
2945 return -EINVAL;
2946 if (copy_from_user(&sinit, optval, optlen))
2947 return -EFAULT;
2948
2949 if (sinit.sinit_num_ostreams)
2950 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2951 if (sinit.sinit_max_instreams)
2952 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2953 if (sinit.sinit_max_attempts)
2954 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2955 if (sinit.sinit_max_init_timeo)
2956 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2957
2958 return 0;
2959}
2960
2961/*
2962 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2963 *
2964 * Applications that wish to use the sendto() system call may wish to
2965 * specify a default set of parameters that would normally be supplied
2966 * through the inclusion of ancillary data. This socket option allows
2967 * such an application to set the default sctp_sndrcvinfo structure.
2968 * The application that wishes to use this socket option simply passes
2969 * in to this call the sctp_sndrcvinfo structure defined in Section
2970 * 5.2.2) The input parameters accepted by this call include
2971 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2972 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2973 * to this call if the caller is using the UDP model.
2974 */
2975static int sctp_setsockopt_default_send_param(struct sock *sk,
2976 char __user *optval,
2977 unsigned int optlen)
2978{
2979 struct sctp_sock *sp = sctp_sk(sk);
2980 struct sctp_association *asoc;
2981 struct sctp_sndrcvinfo info;
2982
2983 if (optlen != sizeof(info))
2984 return -EINVAL;
2985 if (copy_from_user(&info, optval, optlen))
2986 return -EFAULT;
2987 if (info.sinfo_flags &
2988 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2989 SCTP_ABORT | SCTP_EOF))
2990 return -EINVAL;
2991
2992 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2993 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
2994 return -EINVAL;
2995 if (asoc) {
2996 asoc->default_stream = info.sinfo_stream;
2997 asoc->default_flags = info.sinfo_flags;
2998 asoc->default_ppid = info.sinfo_ppid;
2999 asoc->default_context = info.sinfo_context;
3000 asoc->default_timetolive = info.sinfo_timetolive;
3001 } else {
3002 sp->default_stream = info.sinfo_stream;
3003 sp->default_flags = info.sinfo_flags;
3004 sp->default_ppid = info.sinfo_ppid;
3005 sp->default_context = info.sinfo_context;
3006 sp->default_timetolive = info.sinfo_timetolive;
3007 }
3008
3009 return 0;
3010}
3011
3012/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
3013 * (SCTP_DEFAULT_SNDINFO)
3014 */
3015static int sctp_setsockopt_default_sndinfo(struct sock *sk,
3016 char __user *optval,
3017 unsigned int optlen)
3018{
3019 struct sctp_sock *sp = sctp_sk(sk);
3020 struct sctp_association *asoc;
3021 struct sctp_sndinfo info;
3022
3023 if (optlen != sizeof(info))
3024 return -EINVAL;
3025 if (copy_from_user(&info, optval, optlen))
3026 return -EFAULT;
3027 if (info.snd_flags &
3028 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
3029 SCTP_ABORT | SCTP_EOF))
3030 return -EINVAL;
3031
3032 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
3033 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
3034 return -EINVAL;
3035 if (asoc) {
3036 asoc->default_stream = info.snd_sid;
3037 asoc->default_flags = info.snd_flags;
3038 asoc->default_ppid = info.snd_ppid;
3039 asoc->default_context = info.snd_context;
3040 } else {
3041 sp->default_stream = info.snd_sid;
3042 sp->default_flags = info.snd_flags;
3043 sp->default_ppid = info.snd_ppid;
3044 sp->default_context = info.snd_context;
3045 }
3046
3047 return 0;
3048}
3049
3050/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3051 *
3052 * Requests that the local SCTP stack use the enclosed peer address as
3053 * the association primary. The enclosed address must be one of the
3054 * association peer's addresses.
3055 */
3056static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
3057 unsigned int optlen)
3058{
3059 struct sctp_prim prim;
3060 struct sctp_transport *trans;
3061 struct sctp_af *af;
3062 int err;
3063
3064 if (optlen != sizeof(struct sctp_prim))
3065 return -EINVAL;
3066
3067 if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3068 return -EFAULT;
3069
3070 /* Allow security module to validate address but need address len. */
3071 af = sctp_get_af_specific(prim.ssp_addr.ss_family);
3072 if (!af)
3073 return -EINVAL;
3074
3075 err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
3076 (struct sockaddr *)&prim.ssp_addr,
3077 af->sockaddr_len);
3078 if (err)
3079 return err;
3080
3081 trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
3082 if (!trans)
3083 return -EINVAL;
3084
3085 sctp_assoc_set_primary(trans->asoc, trans);
3086
3087 return 0;
3088}
3089
3090/*
3091 * 7.1.5 SCTP_NODELAY
3092 *
3093 * Turn on/off any Nagle-like algorithm. This means that packets are
3094 * generally sent as soon as possible and no unnecessary delays are
3095 * introduced, at the cost of more packets in the network. Expects an
3096 * integer boolean flag.
3097 */
3098static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
3099 unsigned int optlen)
3100{
3101 int val;
3102
3103 if (optlen < sizeof(int))
3104 return -EINVAL;
3105 if (get_user(val, (int __user *)optval))
3106 return -EFAULT;
3107
3108 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3109 return 0;
3110}
3111
3112/*
3113 *
3114 * 7.1.1 SCTP_RTOINFO
3115 *
3116 * The protocol parameters used to initialize and bound retransmission
3117 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3118 * and modify these parameters.
3119 * All parameters are time values, in milliseconds. A value of 0, when
3120 * modifying the parameters, indicates that the current value should not
3121 * be changed.
3122 *
3123 */
3124static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3125{
3126 struct sctp_rtoinfo rtoinfo;
3127 struct sctp_association *asoc;
3128 unsigned long rto_min, rto_max;
3129 struct sctp_sock *sp = sctp_sk(sk);
3130
3131 if (optlen != sizeof (struct sctp_rtoinfo))
3132 return -EINVAL;
3133
3134 if (copy_from_user(&rtoinfo, optval, optlen))
3135 return -EFAULT;
3136
3137 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3138
3139 /* Set the values to the specific association */
3140 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
3141 return -EINVAL;
3142
3143 rto_max = rtoinfo.srto_max;
3144 rto_min = rtoinfo.srto_min;
3145
3146 if (rto_max)
3147 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3148 else
3149 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3150
3151 if (rto_min)
3152 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3153 else
3154 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3155
3156 if (rto_min > rto_max)
3157 return -EINVAL;
3158
3159 if (asoc) {
3160 if (rtoinfo.srto_initial != 0)
3161 asoc->rto_initial =
3162 msecs_to_jiffies(rtoinfo.srto_initial);
3163 asoc->rto_max = rto_max;
3164 asoc->rto_min = rto_min;
3165 } else {
3166 /* If there is no association or the association-id = 0
3167 * set the values to the endpoint.
3168 */
3169 if (rtoinfo.srto_initial != 0)
3170 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
3171 sp->rtoinfo.srto_max = rto_max;
3172 sp->rtoinfo.srto_min = rto_min;
3173 }
3174
3175 return 0;
3176}
3177
3178/*
3179 *
3180 * 7.1.2 SCTP_ASSOCINFO
3181 *
3182 * This option is used to tune the maximum retransmission attempts
3183 * of the association.
3184 * Returns an error if the new association retransmission value is
3185 * greater than the sum of the retransmission value of the peer.
3186 * See [SCTP] for more information.
3187 *
3188 */
3189static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
3190{
3191
3192 struct sctp_assocparams assocparams;
3193 struct sctp_association *asoc;
3194
3195 if (optlen != sizeof(struct sctp_assocparams))
3196 return -EINVAL;
3197 if (copy_from_user(&assocparams, optval, optlen))
3198 return -EFAULT;
3199
3200 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3201
3202 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
3203 return -EINVAL;
3204
3205 /* Set the values to the specific association */
3206 if (asoc) {
3207 if (assocparams.sasoc_asocmaxrxt != 0) {
3208 __u32 path_sum = 0;
3209 int paths = 0;
3210 struct sctp_transport *peer_addr;
3211
3212 list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3213 transports) {
3214 path_sum += peer_addr->pathmaxrxt;
3215 paths++;
3216 }
3217
3218 /* Only validate asocmaxrxt if we have more than
3219 * one path/transport. We do this because path
3220 * retransmissions are only counted when we have more
3221 * then one path.
3222 */
3223 if (paths > 1 &&
3224 assocparams.sasoc_asocmaxrxt > path_sum)
3225 return -EINVAL;
3226
3227 asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
3228 }
3229
3230 if (assocparams.sasoc_cookie_life != 0)
3231 asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
3232 } else {
3233 /* Set the values to the endpoint */
3234 struct sctp_sock *sp = sctp_sk(sk);
3235
3236 if (assocparams.sasoc_asocmaxrxt != 0)
3237 sp->assocparams.sasoc_asocmaxrxt =
3238 assocparams.sasoc_asocmaxrxt;
3239 if (assocparams.sasoc_cookie_life != 0)
3240 sp->assocparams.sasoc_cookie_life =
3241 assocparams.sasoc_cookie_life;
3242 }
3243 return 0;
3244}
3245
3246/*
3247 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3248 *
3249 * This socket option is a boolean flag which turns on or off mapped V4
3250 * addresses. If this option is turned on and the socket is type
3251 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3252 * If this option is turned off, then no mapping will be done of V4
3253 * addresses and a user will receive both PF_INET6 and PF_INET type
3254 * addresses on the socket.
3255 */
3256static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
3257{
3258 int val;
3259 struct sctp_sock *sp = sctp_sk(sk);
3260
3261 if (optlen < sizeof(int))
3262 return -EINVAL;
3263 if (get_user(val, (int __user *)optval))
3264 return -EFAULT;
3265 if (val)
3266 sp->v4mapped = 1;
3267 else
3268 sp->v4mapped = 0;
3269
3270 return 0;
3271}
3272
3273/*
3274 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3275 * This option will get or set the maximum size to put in any outgoing
3276 * SCTP DATA chunk. If a message is larger than this size it will be
3277 * fragmented by SCTP into the specified size. Note that the underlying
3278 * SCTP implementation may fragment into smaller sized chunks when the
3279 * PMTU of the underlying association is smaller than the value set by
3280 * the user. The default value for this option is '0' which indicates
3281 * the user is NOT limiting fragmentation and only the PMTU will effect
3282 * SCTP's choice of DATA chunk size. Note also that values set larger
3283 * than the maximum size of an IP datagram will effectively let SCTP
3284 * control fragmentation (i.e. the same as setting this option to 0).
3285 *
3286 * The following structure is used to access and modify this parameter:
3287 *
3288 * struct sctp_assoc_value {
3289 * sctp_assoc_t assoc_id;
3290 * uint32_t assoc_value;
3291 * };
3292 *
3293 * assoc_id: This parameter is ignored for one-to-one style sockets.
3294 * For one-to-many style sockets this parameter indicates which
3295 * association the user is performing an action upon. Note that if
3296 * this field's value is zero then the endpoints default value is
3297 * changed (effecting future associations only).
3298 * assoc_value: This parameter specifies the maximum size in bytes.
3299 */
3300static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
3301{
3302 struct sctp_sock *sp = sctp_sk(sk);
3303 struct sctp_assoc_value params;
3304 struct sctp_association *asoc;
3305 int val;
3306
3307 if (optlen == sizeof(int)) {
3308 pr_warn_ratelimited(DEPRECATED
3309 "%s (pid %d) "
3310 "Use of int in maxseg socket option.\n"
3311 "Use struct sctp_assoc_value instead\n",
3312 current->comm, task_pid_nr(current));
3313 if (copy_from_user(&val, optval, optlen))
3314 return -EFAULT;
3315 params.assoc_id = 0;
3316 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3317 if (copy_from_user(&params, optval, optlen))
3318 return -EFAULT;
3319 val = params.assoc_value;
3320 } else {
3321 return -EINVAL;
3322 }
3323
3324 asoc = sctp_id2assoc(sk, params.assoc_id);
3325
3326 if (val) {
3327 int min_len, max_len;
3328 __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
3329 sizeof(struct sctp_data_chunk);
3330
3331 min_len = sctp_min_frag_point(sp, datasize);
3332 max_len = SCTP_MAX_CHUNK_LEN - datasize;
3333
3334 if (val < min_len || val > max_len)
3335 return -EINVAL;
3336 }
3337
3338 if (asoc) {
3339 asoc->user_frag = val;
3340 sctp_assoc_update_frag_point(asoc);
3341 } else {
3342 if (params.assoc_id && sctp_style(sk, UDP))
3343 return -EINVAL;
3344 sp->user_frag = val;
3345 }
3346
3347 return 0;
3348}
3349
3350
3351/*
3352 * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3353 *
3354 * Requests that the peer mark the enclosed address as the association
3355 * primary. The enclosed address must be one of the association's
3356 * locally bound addresses. The following structure is used to make a
3357 * set primary request:
3358 */
3359static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
3360 unsigned int optlen)
3361{
3362 struct net *net = sock_net(sk);
3363 struct sctp_sock *sp;
3364 struct sctp_association *asoc = NULL;
3365 struct sctp_setpeerprim prim;
3366 struct sctp_chunk *chunk;
3367 struct sctp_af *af;
3368 int err;
3369
3370 sp = sctp_sk(sk);
3371
3372 if (!net->sctp.addip_enable)
3373 return -EPERM;
3374
3375 if (optlen != sizeof(struct sctp_setpeerprim))
3376 return -EINVAL;
3377
3378 if (copy_from_user(&prim, optval, optlen))
3379 return -EFAULT;
3380
3381 asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
3382 if (!asoc)
3383 return -EINVAL;
3384
3385 if (!asoc->peer.asconf_capable)
3386 return -EPERM;
3387
3388 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3389 return -EPERM;
3390
3391 if (!sctp_state(asoc, ESTABLISHED))
3392 return -ENOTCONN;
3393
3394 af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3395 if (!af)
3396 return -EINVAL;
3397
3398 if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3399 return -EADDRNOTAVAIL;
3400
3401 if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3402 return -EADDRNOTAVAIL;
3403
3404 /* Allow security module to validate address. */
3405 err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3406 (struct sockaddr *)&prim.sspp_addr,
3407 af->sockaddr_len);
3408 if (err)
3409 return err;
3410
3411 /* Create an ASCONF chunk with SET_PRIMARY parameter */
3412 chunk = sctp_make_asconf_set_prim(asoc,
3413 (union sctp_addr *)&prim.sspp_addr);
3414 if (!chunk)
3415 return -ENOMEM;
3416
3417 err = sctp_send_asconf(asoc, chunk);
3418
3419 pr_debug("%s: we set peer primary addr primitively\n", __func__);
3420
3421 return err;
3422}
3423
3424static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
3425 unsigned int optlen)
3426{
3427 struct sctp_setadaptation adaptation;
3428
3429 if (optlen != sizeof(struct sctp_setadaptation))
3430 return -EINVAL;
3431 if (copy_from_user(&adaptation, optval, optlen))
3432 return -EFAULT;
3433
3434 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
3435
3436 return 0;
3437}
3438
3439/*
3440 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3441 *
3442 * The context field in the sctp_sndrcvinfo structure is normally only
3443 * used when a failed message is retrieved holding the value that was
3444 * sent down on the actual send call. This option allows the setting of
3445 * a default context on an association basis that will be received on
3446 * reading messages from the peer. This is especially helpful in the
3447 * one-2-many model for an application to keep some reference to an
3448 * internal state machine that is processing messages on the
3449 * association. Note that the setting of this value only effects
3450 * received messages from the peer and does not effect the value that is
3451 * saved with outbound messages.
3452 */
3453static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
3454 unsigned int optlen)
3455{
3456 struct sctp_assoc_value params;
3457 struct sctp_sock *sp;
3458 struct sctp_association *asoc;
3459
3460 if (optlen != sizeof(struct sctp_assoc_value))
3461 return -EINVAL;
3462 if (copy_from_user(&params, optval, optlen))
3463 return -EFAULT;
3464
3465 sp = sctp_sk(sk);
3466
3467 if (params.assoc_id != 0) {
3468 asoc = sctp_id2assoc(sk, params.assoc_id);
3469 if (!asoc)
3470 return -EINVAL;
3471 asoc->default_rcv_context = params.assoc_value;
3472 } else {
3473 sp->default_rcv_context = params.assoc_value;
3474 }
3475
3476 return 0;
3477}
3478
3479/*
3480 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3481 *
3482 * This options will at a minimum specify if the implementation is doing
3483 * fragmented interleave. Fragmented interleave, for a one to many
3484 * socket, is when subsequent calls to receive a message may return
3485 * parts of messages from different associations. Some implementations
3486 * may allow you to turn this value on or off. If so, when turned off,
3487 * no fragment interleave will occur (which will cause a head of line
3488 * blocking amongst multiple associations sharing the same one to many
3489 * socket). When this option is turned on, then each receive call may
3490 * come from a different association (thus the user must receive data
3491 * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3492 * association each receive belongs to.
3493 *
3494 * This option takes a boolean value. A non-zero value indicates that
3495 * fragmented interleave is on. A value of zero indicates that
3496 * fragmented interleave is off.
3497 *
3498 * Note that it is important that an implementation that allows this
3499 * option to be turned on, have it off by default. Otherwise an unaware
3500 * application using the one to many model may become confused and act
3501 * incorrectly.
3502 */
3503static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3504 char __user *optval,
3505 unsigned int optlen)
3506{
3507 int val;
3508
3509 if (optlen != sizeof(int))
3510 return -EINVAL;
3511 if (get_user(val, (int __user *)optval))
3512 return -EFAULT;
3513
3514 sctp_sk(sk)->frag_interleave = !!val;
3515
3516 if (!sctp_sk(sk)->frag_interleave)
3517 sctp_sk(sk)->strm_interleave = 0;
3518
3519 return 0;
3520}
3521
3522/*
3523 * 8.1.21. Set or Get the SCTP Partial Delivery Point
3524 * (SCTP_PARTIAL_DELIVERY_POINT)
3525 *
3526 * This option will set or get the SCTP partial delivery point. This
3527 * point is the size of a message where the partial delivery API will be
3528 * invoked to help free up rwnd space for the peer. Setting this to a
3529 * lower value will cause partial deliveries to happen more often. The
3530 * calls argument is an integer that sets or gets the partial delivery
3531 * point. Note also that the call will fail if the user attempts to set
3532 * this value larger than the socket receive buffer size.
3533 *
3534 * Note that any single message having a length smaller than or equal to
3535 * the SCTP partial delivery point will be delivered in one single read
3536 * call as long as the user provided buffer is large enough to hold the
3537 * message.
3538 */
3539static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3540 char __user *optval,
3541 unsigned int optlen)
3542{
3543 u32 val;
3544
3545 if (optlen != sizeof(u32))
3546 return -EINVAL;
3547 if (get_user(val, (int __user *)optval))
3548 return -EFAULT;
3549
3550 /* Note: We double the receive buffer from what the user sets
3551 * it to be, also initial rwnd is based on rcvbuf/2.
3552 */
3553 if (val > (sk->sk_rcvbuf >> 1))
3554 return -EINVAL;
3555
3556 sctp_sk(sk)->pd_point = val;
3557
3558 return 0; /* is this the right error code? */
3559}
3560
3561/*
3562 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3563 *
3564 * This option will allow a user to change the maximum burst of packets
3565 * that can be emitted by this association. Note that the default value
3566 * is 4, and some implementations may restrict this setting so that it
3567 * can only be lowered.
3568 *
3569 * NOTE: This text doesn't seem right. Do this on a socket basis with
3570 * future associations inheriting the socket value.
3571 */
3572static int sctp_setsockopt_maxburst(struct sock *sk,
3573 char __user *optval,
3574 unsigned int optlen)
3575{
3576 struct sctp_assoc_value params;
3577 struct sctp_sock *sp;
3578 struct sctp_association *asoc;
3579 int val;
3580 int assoc_id = 0;
3581
3582 if (optlen == sizeof(int)) {
3583 pr_warn_ratelimited(DEPRECATED
3584 "%s (pid %d) "
3585 "Use of int in max_burst socket option deprecated.\n"
3586 "Use struct sctp_assoc_value instead\n",
3587 current->comm, task_pid_nr(current));
3588 if (copy_from_user(&val, optval, optlen))
3589 return -EFAULT;
3590 } else if (optlen == sizeof(struct sctp_assoc_value)) {
3591 if (copy_from_user(&params, optval, optlen))
3592 return -EFAULT;
3593 val = params.assoc_value;
3594 assoc_id = params.assoc_id;
3595 } else
3596 return -EINVAL;
3597
3598 sp = sctp_sk(sk);
3599
3600 if (assoc_id != 0) {
3601 asoc = sctp_id2assoc(sk, assoc_id);
3602 if (!asoc)
3603 return -EINVAL;
3604 asoc->max_burst = val;
3605 } else
3606 sp->max_burst = val;
3607
3608 return 0;
3609}
3610
3611/*
3612 * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3613 *
3614 * This set option adds a chunk type that the user is requesting to be
3615 * received only in an authenticated way. Changes to the list of chunks
3616 * will only effect future associations on the socket.
3617 */
3618static int sctp_setsockopt_auth_chunk(struct sock *sk,
3619 char __user *optval,
3620 unsigned int optlen)
3621{
3622 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3623 struct sctp_authchunk val;
3624
3625 if (!ep->auth_enable)
3626 return -EACCES;
3627
3628 if (optlen != sizeof(struct sctp_authchunk))
3629 return -EINVAL;
3630 if (copy_from_user(&val, optval, optlen))
3631 return -EFAULT;
3632
3633 switch (val.sauth_chunk) {
3634 case SCTP_CID_INIT:
3635 case SCTP_CID_INIT_ACK:
3636 case SCTP_CID_SHUTDOWN_COMPLETE:
3637 case SCTP_CID_AUTH:
3638 return -EINVAL;
3639 }
3640
3641 /* add this chunk id to the endpoint */
3642 return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
3643}
3644
3645/*
3646 * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3647 *
3648 * This option gets or sets the list of HMAC algorithms that the local
3649 * endpoint requires the peer to use.
3650 */
3651static int sctp_setsockopt_hmac_ident(struct sock *sk,
3652 char __user *optval,
3653 unsigned int optlen)
3654{
3655 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3656 struct sctp_hmacalgo *hmacs;
3657 u32 idents;
3658 int err;
3659
3660 if (!ep->auth_enable)
3661 return -EACCES;
3662
3663 if (optlen < sizeof(struct sctp_hmacalgo))
3664 return -EINVAL;
3665 optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3666 SCTP_AUTH_NUM_HMACS * sizeof(u16));
3667
3668 hmacs = memdup_user(optval, optlen);
3669 if (IS_ERR(hmacs))
3670 return PTR_ERR(hmacs);
3671
3672 idents = hmacs->shmac_num_idents;
3673 if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3674 (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
3675 err = -EINVAL;
3676 goto out;
3677 }
3678
3679 err = sctp_auth_ep_set_hmacs(ep, hmacs);
3680out:
3681 kfree(hmacs);
3682 return err;
3683}
3684
3685/*
3686 * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3687 *
3688 * This option will set a shared secret key which is used to build an
3689 * association shared key.
3690 */
3691static int sctp_setsockopt_auth_key(struct sock *sk,
3692 char __user *optval,
3693 unsigned int optlen)
3694{
3695 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3696 struct sctp_authkey *authkey;
3697 struct sctp_association *asoc;
3698 int ret;
3699
3700 if (!ep->auth_enable)
3701 return -EACCES;
3702
3703 if (optlen <= sizeof(struct sctp_authkey))
3704 return -EINVAL;
3705 /* authkey->sca_keylength is u16, so optlen can't be bigger than
3706 * this.
3707 */
3708 optlen = min_t(unsigned int, optlen, USHRT_MAX +
3709 sizeof(struct sctp_authkey));
3710
3711 authkey = memdup_user(optval, optlen);
3712 if (IS_ERR(authkey))
3713 return PTR_ERR(authkey);
3714
3715 if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
3716 ret = -EINVAL;
3717 goto out;
3718 }
3719
3720 asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3721 if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
3722 ret = -EINVAL;
3723 goto out;
3724 }
3725
3726 ret = sctp_auth_set_key(ep, asoc, authkey);
3727out:
3728 kzfree(authkey);
3729 return ret;
3730}
3731
3732/*
3733 * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3734 *
3735 * This option will get or set the active shared key to be used to build
3736 * the association shared key.
3737 */
3738static int sctp_setsockopt_active_key(struct sock *sk,
3739 char __user *optval,
3740 unsigned int optlen)
3741{
3742 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3743 struct sctp_authkeyid val;
3744 struct sctp_association *asoc;
3745
3746 if (!ep->auth_enable)
3747 return -EACCES;
3748
3749 if (optlen != sizeof(struct sctp_authkeyid))
3750 return -EINVAL;
3751 if (copy_from_user(&val, optval, optlen))
3752 return -EFAULT;
3753
3754 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3755 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3756 return -EINVAL;
3757
3758 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3759}
3760
3761/*
3762 * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3763 *
3764 * This set option will delete a shared secret key from use.
3765 */
3766static int sctp_setsockopt_del_key(struct sock *sk,
3767 char __user *optval,
3768 unsigned int optlen)
3769{
3770 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3771 struct sctp_authkeyid val;
3772 struct sctp_association *asoc;
3773
3774 if (!ep->auth_enable)
3775 return -EACCES;
3776
3777 if (optlen != sizeof(struct sctp_authkeyid))
3778 return -EINVAL;
3779 if (copy_from_user(&val, optval, optlen))
3780 return -EFAULT;
3781
3782 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3783 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3784 return -EINVAL;
3785
3786 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3787
3788}
3789
3790/*
3791 * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
3792 *
3793 * This set option will deactivate a shared secret key.
3794 */
3795static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
3796 unsigned int optlen)
3797{
3798 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3799 struct sctp_authkeyid val;
3800 struct sctp_association *asoc;
3801
3802 if (!ep->auth_enable)
3803 return -EACCES;
3804
3805 if (optlen != sizeof(struct sctp_authkeyid))
3806 return -EINVAL;
3807 if (copy_from_user(&val, optval, optlen))
3808 return -EFAULT;
3809
3810 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3811 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
3812 return -EINVAL;
3813
3814 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3815}
3816
3817/*
3818 * 8.1.23 SCTP_AUTO_ASCONF
3819 *
3820 * This option will enable or disable the use of the automatic generation of
3821 * ASCONF chunks to add and delete addresses to an existing association. Note
3822 * that this option has two caveats namely: a) it only affects sockets that
3823 * are bound to all addresses available to the SCTP stack, and b) the system
3824 * administrator may have an overriding control that turns the ASCONF feature
3825 * off no matter what setting the socket option may have.
3826 * This option expects an integer boolean flag, where a non-zero value turns on
3827 * the option, and a zero value turns off the option.
3828 * Note. In this implementation, socket operation overrides default parameter
3829 * being set by sysctl as well as FreeBSD implementation
3830 */
3831static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3832 unsigned int optlen)
3833{
3834 int val;
3835 struct sctp_sock *sp = sctp_sk(sk);
3836
3837 if (optlen < sizeof(int))
3838 return -EINVAL;
3839 if (get_user(val, (int __user *)optval))
3840 return -EFAULT;
3841 if (!sctp_is_ep_boundall(sk) && val)
3842 return -EINVAL;
3843 if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3844 return 0;
3845
3846 spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3847 if (val == 0 && sp->do_auto_asconf) {
3848 list_del(&sp->auto_asconf_list);
3849 sp->do_auto_asconf = 0;
3850 } else if (val && !sp->do_auto_asconf) {
3851 list_add_tail(&sp->auto_asconf_list,
3852 &sock_net(sk)->sctp.auto_asconf_splist);
3853 sp->do_auto_asconf = 1;
3854 }
3855 spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3856 return 0;
3857}
3858
3859/*
3860 * SCTP_PEER_ADDR_THLDS
3861 *
3862 * This option allows us to alter the partially failed threshold for one or all
3863 * transports in an association. See Section 6.1 of:
3864 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3865 */
3866static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3867 char __user *optval,
3868 unsigned int optlen)
3869{
3870 struct sctp_paddrthlds val;
3871 struct sctp_transport *trans;
3872 struct sctp_association *asoc;
3873
3874 if (optlen < sizeof(struct sctp_paddrthlds))
3875 return -EINVAL;
3876 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3877 sizeof(struct sctp_paddrthlds)))
3878 return -EFAULT;
3879
3880
3881 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3882 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3883 if (!asoc)
3884 return -ENOENT;
3885 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3886 transports) {
3887 if (val.spt_pathmaxrxt)
3888 trans->pathmaxrxt = val.spt_pathmaxrxt;
3889 trans->pf_retrans = val.spt_pathpfthld;
3890 }
3891
3892 if (val.spt_pathmaxrxt)
3893 asoc->pathmaxrxt = val.spt_pathmaxrxt;
3894 asoc->pf_retrans = val.spt_pathpfthld;
3895 } else {
3896 trans = sctp_addr_id2transport(sk, &val.spt_address,
3897 val.spt_assoc_id);
3898 if (!trans)
3899 return -ENOENT;
3900
3901 if (val.spt_pathmaxrxt)
3902 trans->pathmaxrxt = val.spt_pathmaxrxt;
3903 trans->pf_retrans = val.spt_pathpfthld;
3904 }
3905
3906 return 0;
3907}
3908
3909static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
3910 char __user *optval,
3911 unsigned int optlen)
3912{
3913 int val;
3914
3915 if (optlen < sizeof(int))
3916 return -EINVAL;
3917 if (get_user(val, (int __user *) optval))
3918 return -EFAULT;
3919
3920 sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
3921
3922 return 0;
3923}
3924
3925static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
3926 char __user *optval,
3927 unsigned int optlen)
3928{
3929 int val;
3930
3931 if (optlen < sizeof(int))
3932 return -EINVAL;
3933 if (get_user(val, (int __user *) optval))
3934 return -EFAULT;
3935
3936 sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
3937
3938 return 0;
3939}
3940
3941static int sctp_setsockopt_pr_supported(struct sock *sk,
3942 char __user *optval,
3943 unsigned int optlen)
3944{
3945 struct sctp_assoc_value params;
3946
3947 if (optlen != sizeof(params))
3948 return -EINVAL;
3949
3950 if (copy_from_user(&params, optval, optlen))
3951 return -EFAULT;
3952
3953 sctp_sk(sk)->ep->prsctp_enable = !!params.assoc_value;
3954
3955 return 0;
3956}
3957
3958static int sctp_setsockopt_default_prinfo(struct sock *sk,
3959 char __user *optval,
3960 unsigned int optlen)
3961{
3962 struct sctp_default_prinfo info;
3963 struct sctp_association *asoc;
3964 int retval = -EINVAL;
3965
3966 if (optlen != sizeof(info))
3967 goto out;
3968
3969 if (copy_from_user(&info, optval, sizeof(info))) {
3970 retval = -EFAULT;
3971 goto out;
3972 }
3973
3974 if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
3975 goto out;
3976
3977 if (info.pr_policy == SCTP_PR_SCTP_NONE)
3978 info.pr_value = 0;
3979
3980 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
3981 if (asoc) {
3982 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
3983 asoc->default_timetolive = info.pr_value;
3984 } else if (!info.pr_assoc_id) {
3985 struct sctp_sock *sp = sctp_sk(sk);
3986
3987 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
3988 sp->default_timetolive = info.pr_value;
3989 } else {
3990 goto out;
3991 }
3992
3993 retval = 0;
3994
3995out:
3996 return retval;
3997}
3998
3999static int sctp_setsockopt_reconfig_supported(struct sock *sk,
4000 char __user *optval,
4001 unsigned int optlen)
4002{
4003 struct sctp_assoc_value params;
4004 struct sctp_association *asoc;
4005 int retval = -EINVAL;
4006
4007 if (optlen != sizeof(params))
4008 goto out;
4009
4010 if (copy_from_user(&params, optval, optlen)) {
4011 retval = -EFAULT;
4012 goto out;
4013 }
4014
4015 asoc = sctp_id2assoc(sk, params.assoc_id);
4016 if (asoc) {
4017 asoc->reconf_enable = !!params.assoc_value;
4018 } else if (!params.assoc_id) {
4019 struct sctp_sock *sp = sctp_sk(sk);
4020
4021 sp->ep->reconf_enable = !!params.assoc_value;
4022 } else {
4023 goto out;
4024 }
4025
4026 retval = 0;
4027
4028out:
4029 return retval;
4030}
4031
4032static int sctp_setsockopt_enable_strreset(struct sock *sk,
4033 char __user *optval,
4034 unsigned int optlen)
4035{
4036 struct sctp_assoc_value params;
4037 struct sctp_association *asoc;
4038 int retval = -EINVAL;
4039
4040 if (optlen != sizeof(params))
4041 goto out;
4042
4043 if (copy_from_user(&params, optval, optlen)) {
4044 retval = -EFAULT;
4045 goto out;
4046 }
4047
4048 if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
4049 goto out;
4050
4051 asoc = sctp_id2assoc(sk, params.assoc_id);
4052 if (asoc) {
4053 asoc->strreset_enable = params.assoc_value;
4054 } else if (!params.assoc_id) {
4055 struct sctp_sock *sp = sctp_sk(sk);
4056
4057 sp->ep->strreset_enable = params.assoc_value;
4058 } else {
4059 goto out;
4060 }
4061
4062 retval = 0;
4063
4064out:
4065 return retval;
4066}
4067
4068static int sctp_setsockopt_reset_streams(struct sock *sk,
4069 char __user *optval,
4070 unsigned int optlen)
4071{
4072 struct sctp_reset_streams *params;
4073 struct sctp_association *asoc;
4074 int retval = -EINVAL;
4075
4076 if (optlen < sizeof(*params))
4077 return -EINVAL;
4078 /* srs_number_streams is u16, so optlen can't be bigger than this. */
4079 optlen = min_t(unsigned int, optlen, USHRT_MAX +
4080 sizeof(__u16) * sizeof(*params));
4081
4082 params = memdup_user(optval, optlen);
4083 if (IS_ERR(params))
4084 return PTR_ERR(params);
4085
4086 if (params->srs_number_streams * sizeof(__u16) >
4087 optlen - sizeof(*params))
4088 goto out;
4089
4090 asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4091 if (!asoc)
4092 goto out;
4093
4094 retval = sctp_send_reset_streams(asoc, params);
4095
4096out:
4097 kfree(params);
4098 return retval;
4099}
4100
4101static int sctp_setsockopt_reset_assoc(struct sock *sk,
4102 char __user *optval,
4103 unsigned int optlen)
4104{
4105 struct sctp_association *asoc;
4106 sctp_assoc_t associd;
4107 int retval = -EINVAL;
4108
4109 if (optlen != sizeof(associd))
4110 goto out;
4111
4112 if (copy_from_user(&associd, optval, optlen)) {
4113 retval = -EFAULT;
4114 goto out;
4115 }
4116
4117 asoc = sctp_id2assoc(sk, associd);
4118 if (!asoc)
4119 goto out;
4120
4121 retval = sctp_send_reset_assoc(asoc);
4122
4123out:
4124 return retval;
4125}
4126
4127static int sctp_setsockopt_add_streams(struct sock *sk,
4128 char __user *optval,
4129 unsigned int optlen)
4130{
4131 struct sctp_association *asoc;
4132 struct sctp_add_streams params;
4133 int retval = -EINVAL;
4134
4135 if (optlen != sizeof(params))
4136 goto out;
4137
4138 if (copy_from_user(&params, optval, optlen)) {
4139 retval = -EFAULT;
4140 goto out;
4141 }
4142
4143 asoc = sctp_id2assoc(sk, params.sas_assoc_id);
4144 if (!asoc)
4145 goto out;
4146
4147 retval = sctp_send_add_streams(asoc, &params);
4148
4149out:
4150 return retval;
4151}
4152
4153static int sctp_setsockopt_scheduler(struct sock *sk,
4154 char __user *optval,
4155 unsigned int optlen)
4156{
4157 struct sctp_association *asoc;
4158 struct sctp_assoc_value params;
4159 int retval = -EINVAL;
4160
4161 if (optlen < sizeof(params))
4162 goto out;
4163
4164 optlen = sizeof(params);
4165 if (copy_from_user(&params, optval, optlen)) {
4166 retval = -EFAULT;
4167 goto out;
4168 }
4169
4170 if (params.assoc_value > SCTP_SS_MAX)
4171 goto out;
4172
4173 asoc = sctp_id2assoc(sk, params.assoc_id);
4174 if (!asoc)
4175 goto out;
4176
4177 retval = sctp_sched_set_sched(asoc, params.assoc_value);
4178
4179out:
4180 return retval;
4181}
4182
4183static int sctp_setsockopt_scheduler_value(struct sock *sk,
4184 char __user *optval,
4185 unsigned int optlen)
4186{
4187 struct sctp_association *asoc;
4188 struct sctp_stream_value params;
4189 int retval = -EINVAL;
4190
4191 if (optlen < sizeof(params))
4192 goto out;
4193
4194 optlen = sizeof(params);
4195 if (copy_from_user(&params, optval, optlen)) {
4196 retval = -EFAULT;
4197 goto out;
4198 }
4199
4200 asoc = sctp_id2assoc(sk, params.assoc_id);
4201 if (!asoc)
4202 goto out;
4203
4204 retval = sctp_sched_set_value(asoc, params.stream_id,
4205 params.stream_value, GFP_KERNEL);
4206
4207out:
4208 return retval;
4209}
4210
4211static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4212 char __user *optval,
4213 unsigned int optlen)
4214{
4215 struct sctp_sock *sp = sctp_sk(sk);
4216 struct net *net = sock_net(sk);
4217 struct sctp_assoc_value params;
4218 int retval = -EINVAL;
4219
4220 if (optlen < sizeof(params))
4221 goto out;
4222
4223 optlen = sizeof(params);
4224 if (copy_from_user(&params, optval, optlen)) {
4225 retval = -EFAULT;
4226 goto out;
4227 }
4228
4229 if (params.assoc_id)
4230 goto out;
4231
4232 if (!net->sctp.intl_enable || !sp->frag_interleave) {
4233 retval = -EPERM;
4234 goto out;
4235 }
4236
4237 sp->strm_interleave = !!params.assoc_value;
4238
4239 retval = 0;
4240
4241out:
4242 return retval;
4243}
4244
4245static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval,
4246 unsigned int optlen)
4247{
4248 int val;
4249
4250 if (!sctp_style(sk, TCP))
4251 return -EOPNOTSUPP;
4252
4253 if (sctp_sk(sk)->ep->base.bind_addr.port)
4254 return -EFAULT;
4255
4256 if (optlen < sizeof(int))
4257 return -EINVAL;
4258
4259 if (get_user(val, (int __user *)optval))
4260 return -EFAULT;
4261
4262 sctp_sk(sk)->reuse = !!val;
4263
4264 return 0;
4265}
4266
4267/* API 6.2 setsockopt(), getsockopt()
4268 *
4269 * Applications use setsockopt() and getsockopt() to set or retrieve
4270 * socket options. Socket options are used to change the default
4271 * behavior of sockets calls. They are described in Section 7.
4272 *
4273 * The syntax is:
4274 *
4275 * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4276 * int __user *optlen);
4277 * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4278 * int optlen);
4279 *
4280 * sd - the socket descript.
4281 * level - set to IPPROTO_SCTP for all SCTP options.
4282 * optname - the option name.
4283 * optval - the buffer to store the value of the option.
4284 * optlen - the size of the buffer.
4285 */
4286static int sctp_setsockopt(struct sock *sk, int level, int optname,
4287 char __user *optval, unsigned int optlen)
4288{
4289 int retval = 0;
4290
4291 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
4292
4293 /* I can hardly begin to describe how wrong this is. This is
4294 * so broken as to be worse than useless. The API draft
4295 * REALLY is NOT helpful here... I am not convinced that the
4296 * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4297 * are at all well-founded.
4298 */
4299 if (level != SOL_SCTP) {
4300 struct sctp_af *af = sctp_sk(sk)->pf->af;
4301 retval = af->setsockopt(sk, level, optname, optval, optlen);
4302 goto out_nounlock;
4303 }
4304
4305 lock_sock(sk);
4306
4307 switch (optname) {
4308 case SCTP_SOCKOPT_BINDX_ADD:
4309 /* 'optlen' is the size of the addresses buffer. */
4310 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4311 optlen, SCTP_BINDX_ADD_ADDR);
4312 break;
4313
4314 case SCTP_SOCKOPT_BINDX_REM:
4315 /* 'optlen' is the size of the addresses buffer. */
4316 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4317 optlen, SCTP_BINDX_REM_ADDR);
4318 break;
4319
4320 case SCTP_SOCKOPT_CONNECTX_OLD:
4321 /* 'optlen' is the size of the addresses buffer. */
4322 retval = sctp_setsockopt_connectx_old(sk,
4323 (struct sockaddr __user *)optval,
4324 optlen);
4325 break;
4326
4327 case SCTP_SOCKOPT_CONNECTX:
4328 /* 'optlen' is the size of the addresses buffer. */
4329 retval = sctp_setsockopt_connectx(sk,
4330 (struct sockaddr __user *)optval,
4331 optlen);
4332 break;
4333
4334 case SCTP_DISABLE_FRAGMENTS:
4335 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4336 break;
4337
4338 case SCTP_EVENTS:
4339 retval = sctp_setsockopt_events(sk, optval, optlen);
4340 break;
4341
4342 case SCTP_AUTOCLOSE:
4343 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4344 break;
4345
4346 case SCTP_PEER_ADDR_PARAMS:
4347 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4348 break;
4349
4350 case SCTP_DELAYED_SACK:
4351 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
4352 break;
4353 case SCTP_PARTIAL_DELIVERY_POINT:
4354 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4355 break;
4356
4357 case SCTP_INITMSG:
4358 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4359 break;
4360 case SCTP_DEFAULT_SEND_PARAM:
4361 retval = sctp_setsockopt_default_send_param(sk, optval,
4362 optlen);
4363 break;
4364 case SCTP_DEFAULT_SNDINFO:
4365 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4366 break;
4367 case SCTP_PRIMARY_ADDR:
4368 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4369 break;
4370 case SCTP_SET_PEER_PRIMARY_ADDR:
4371 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4372 break;
4373 case SCTP_NODELAY:
4374 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4375 break;
4376 case SCTP_RTOINFO:
4377 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4378 break;
4379 case SCTP_ASSOCINFO:
4380 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4381 break;
4382 case SCTP_I_WANT_MAPPED_V4_ADDR:
4383 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4384 break;
4385 case SCTP_MAXSEG:
4386 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4387 break;
4388 case SCTP_ADAPTATION_LAYER:
4389 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
4390 break;
4391 case SCTP_CONTEXT:
4392 retval = sctp_setsockopt_context(sk, optval, optlen);
4393 break;
4394 case SCTP_FRAGMENT_INTERLEAVE:
4395 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4396 break;
4397 case SCTP_MAX_BURST:
4398 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4399 break;
4400 case SCTP_AUTH_CHUNK:
4401 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4402 break;
4403 case SCTP_HMAC_IDENT:
4404 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4405 break;
4406 case SCTP_AUTH_KEY:
4407 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4408 break;
4409 case SCTP_AUTH_ACTIVE_KEY:
4410 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4411 break;
4412 case SCTP_AUTH_DELETE_KEY:
4413 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4414 break;
4415 case SCTP_AUTH_DEACTIVATE_KEY:
4416 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
4417 break;
4418 case SCTP_AUTO_ASCONF:
4419 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4420 break;
4421 case SCTP_PEER_ADDR_THLDS:
4422 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4423 break;
4424 case SCTP_RECVRCVINFO:
4425 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4426 break;
4427 case SCTP_RECVNXTINFO:
4428 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4429 break;
4430 case SCTP_PR_SUPPORTED:
4431 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4432 break;
4433 case SCTP_DEFAULT_PRINFO:
4434 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4435 break;
4436 case SCTP_RECONFIG_SUPPORTED:
4437 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4438 break;
4439 case SCTP_ENABLE_STREAM_RESET:
4440 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4441 break;
4442 case SCTP_RESET_STREAMS:
4443 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4444 break;
4445 case SCTP_RESET_ASSOC:
4446 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4447 break;
4448 case SCTP_ADD_STREAMS:
4449 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4450 break;
4451 case SCTP_STREAM_SCHEDULER:
4452 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4453 break;
4454 case SCTP_STREAM_SCHEDULER_VALUE:
4455 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4456 break;
4457 case SCTP_INTERLEAVING_SUPPORTED:
4458 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4459 optlen);
4460 break;
4461 case SCTP_REUSE_PORT:
4462 retval = sctp_setsockopt_reuse_port(sk, optval, optlen);
4463 break;
4464 default:
4465 retval = -ENOPROTOOPT;
4466 break;
4467 }
4468
4469 release_sock(sk);
4470
4471out_nounlock:
4472 return retval;
4473}
4474
4475/* API 3.1.6 connect() - UDP Style Syntax
4476 *
4477 * An application may use the connect() call in the UDP model to initiate an
4478 * association without sending data.
4479 *
4480 * The syntax is:
4481 *
4482 * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4483 *
4484 * sd: the socket descriptor to have a new association added to.
4485 *
4486 * nam: the address structure (either struct sockaddr_in or struct
4487 * sockaddr_in6 defined in RFC2553 [7]).
4488 *
4489 * len: the size of the address.
4490 */
4491static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4492 int addr_len, int flags)
4493{
4494 struct sctp_af *af;
4495 int err = -EINVAL;
4496
4497 lock_sock(sk);
4498
4499 pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4500 addr, addr_len);
4501
4502 /* Validate addr_len before calling common connect/connectx routine. */
4503 af = sctp_get_af_specific(addr->sa_family);
4504 if (af && addr_len >= af->sockaddr_len)
4505 err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
4506
4507 release_sock(sk);
4508 return err;
4509}
4510
4511int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
4512 int addr_len, int flags)
4513{
4514 if (addr_len < sizeof(uaddr->sa_family))
4515 return -EINVAL;
4516
4517 if (uaddr->sa_family == AF_UNSPEC)
4518 return -EOPNOTSUPP;
4519
4520 return sctp_connect(sock->sk, uaddr, addr_len, flags);
4521}
4522
4523/* FIXME: Write comments. */
4524static int sctp_disconnect(struct sock *sk, int flags)
4525{
4526 return -EOPNOTSUPP; /* STUB */
4527}
4528
4529/* 4.1.4 accept() - TCP Style Syntax
4530 *
4531 * Applications use accept() call to remove an established SCTP
4532 * association from the accept queue of the endpoint. A new socket
4533 * descriptor will be returned from accept() to represent the newly
4534 * formed association.
4535 */
4536static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
4537{
4538 struct sctp_sock *sp;
4539 struct sctp_endpoint *ep;
4540 struct sock *newsk = NULL;
4541 struct sctp_association *asoc;
4542 long timeo;
4543 int error = 0;
4544
4545 lock_sock(sk);
4546
4547 sp = sctp_sk(sk);
4548 ep = sp->ep;
4549
4550 if (!sctp_style(sk, TCP)) {
4551 error = -EOPNOTSUPP;
4552 goto out;
4553 }
4554
4555 if (!sctp_sstate(sk, LISTENING)) {
4556 error = -EINVAL;
4557 goto out;
4558 }
4559
4560 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
4561
4562 error = sctp_wait_for_accept(sk, timeo);
4563 if (error)
4564 goto out;
4565
4566 /* We treat the list of associations on the endpoint as the accept
4567 * queue and pick the first association on the list.
4568 */
4569 asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4570
4571 newsk = sp->pf->create_accept_sk(sk, asoc, kern);
4572 if (!newsk) {
4573 error = -ENOMEM;
4574 goto out;
4575 }
4576
4577 /* Populate the fields of the newsk from the oldsk and migrate the
4578 * asoc to the newsk.
4579 */
4580 sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4581
4582out:
4583 release_sock(sk);
4584 *err = error;
4585 return newsk;
4586}
4587
4588/* The SCTP ioctl handler. */
4589static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
4590{
4591 int rc = -ENOTCONN;
4592
4593 lock_sock(sk);
4594
4595 /*
4596 * SEQPACKET-style sockets in LISTENING state are valid, for
4597 * SCTP, so only discard TCP-style sockets in LISTENING state.
4598 */
4599 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4600 goto out;
4601
4602 switch (cmd) {
4603 case SIOCINQ: {
4604 struct sk_buff *skb;
4605 unsigned int amount = 0;
4606
4607 skb = skb_peek(&sk->sk_receive_queue);
4608 if (skb != NULL) {
4609 /*
4610 * We will only return the amount of this packet since
4611 * that is all that will be read.
4612 */
4613 amount = skb->len;
4614 }
4615 rc = put_user(amount, (int __user *)arg);
4616 break;
4617 }
4618 default:
4619 rc = -ENOIOCTLCMD;
4620 break;
4621 }
4622out:
4623 release_sock(sk);
4624 return rc;
4625}
4626
4627/* This is the function which gets called during socket creation to
4628 * initialized the SCTP-specific portion of the sock.
4629 * The sock structure should already be zero-filled memory.
4630 */
4631static int sctp_init_sock(struct sock *sk)
4632{
4633 struct net *net = sock_net(sk);
4634 struct sctp_sock *sp;
4635
4636 pr_debug("%s: sk:%p\n", __func__, sk);
4637
4638 sp = sctp_sk(sk);
4639
4640 /* Initialize the SCTP per socket area. */
4641 switch (sk->sk_type) {
4642 case SOCK_SEQPACKET:
4643 sp->type = SCTP_SOCKET_UDP;
4644 break;
4645 case SOCK_STREAM:
4646 sp->type = SCTP_SOCKET_TCP;
4647 break;
4648 default:
4649 return -ESOCKTNOSUPPORT;
4650 }
4651
4652 sk->sk_gso_type = SKB_GSO_SCTP;
4653
4654 /* Initialize default send parameters. These parameters can be
4655 * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4656 */
4657 sp->default_stream = 0;
4658 sp->default_ppid = 0;
4659 sp->default_flags = 0;
4660 sp->default_context = 0;
4661 sp->default_timetolive = 0;
4662
4663 sp->default_rcv_context = 0;
4664 sp->max_burst = net->sctp.max_burst;
4665
4666 sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4667
4668 /* Initialize default setup parameters. These parameters
4669 * can be modified with the SCTP_INITMSG socket option or
4670 * overridden by the SCTP_INIT CMSG.
4671 */
4672 sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4673 sp->initmsg.sinit_max_instreams = sctp_max_instreams;
4674 sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4675 sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
4676
4677 /* Initialize default RTO related parameters. These parameters can
4678 * be modified for with the SCTP_RTOINFO socket option.
4679 */
4680 sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4681 sp->rtoinfo.srto_max = net->sctp.rto_max;
4682 sp->rtoinfo.srto_min = net->sctp.rto_min;
4683
4684 /* Initialize default association related parameters. These parameters
4685 * can be modified with the SCTP_ASSOCINFO socket option.
4686 */
4687 sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
4688 sp->assocparams.sasoc_number_peer_destinations = 0;
4689 sp->assocparams.sasoc_peer_rwnd = 0;
4690 sp->assocparams.sasoc_local_rwnd = 0;
4691 sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
4692
4693 /* Initialize default event subscriptions. By default, all the
4694 * options are off.
4695 */
4696 memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
4697
4698 /* Default Peer Address Parameters. These defaults can
4699 * be modified via SCTP_PEER_ADDR_PARAMS
4700 */
4701 sp->hbinterval = net->sctp.hb_interval;
4702 sp->pathmaxrxt = net->sctp.max_retrans_path;
4703 sp->pathmtu = 0; /* allow default discovery */
4704 sp->sackdelay = net->sctp.sack_timeout;
4705 sp->sackfreq = 2;
4706 sp->param_flags = SPP_HB_ENABLE |
4707 SPP_PMTUD_ENABLE |
4708 SPP_SACKDELAY_ENABLE;
4709
4710 /* If enabled no SCTP message fragmentation will be performed.
4711 * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4712 */
4713 sp->disable_fragments = 0;
4714
4715 /* Enable Nagle algorithm by default. */
4716 sp->nodelay = 0;
4717
4718 sp->recvrcvinfo = 0;
4719 sp->recvnxtinfo = 0;
4720
4721 /* Enable by default. */
4722 sp->v4mapped = 1;
4723
4724 /* Auto-close idle associations after the configured
4725 * number of seconds. A value of 0 disables this
4726 * feature. Configure through the SCTP_AUTOCLOSE socket option,
4727 * for UDP-style sockets only.
4728 */
4729 sp->autoclose = 0;
4730
4731 /* User specified fragmentation limit. */
4732 sp->user_frag = 0;
4733
4734 sp->adaptation_ind = 0;
4735
4736 sp->pf = sctp_get_pf_specific(sk->sk_family);
4737
4738 /* Control variables for partial data delivery. */
4739 atomic_set(&sp->pd_mode, 0);
4740 skb_queue_head_init(&sp->pd_lobby);
4741 sp->frag_interleave = 0;
4742
4743 /* Create a per socket endpoint structure. Even if we
4744 * change the data structure relationships, this may still
4745 * be useful for storing pre-connect address information.
4746 */
4747 sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4748 if (!sp->ep)
4749 return -ENOMEM;
4750
4751 sp->hmac = NULL;
4752
4753 sk->sk_destruct = sctp_destruct_sock;
4754
4755 SCTP_DBG_OBJCNT_INC(sock);
4756
4757 local_bh_disable();
4758 sk_sockets_allocated_inc(sk);
4759 sock_prot_inuse_add(net, sk->sk_prot, 1);
4760
4761 /* Nothing can fail after this block, otherwise
4762 * sctp_destroy_sock() will be called without addr_wq_lock held
4763 */
4764 if (net->sctp.default_auto_asconf) {
4765 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
4766 list_add_tail(&sp->auto_asconf_list,
4767 &net->sctp.auto_asconf_splist);
4768 sp->do_auto_asconf = 1;
4769 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
4770 } else {
4771 sp->do_auto_asconf = 0;
4772 }
4773
4774 local_bh_enable();
4775
4776 return 0;
4777}
4778
4779/* Cleanup any SCTP per socket resources. Must be called with
4780 * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4781 */
4782static void sctp_destroy_sock(struct sock *sk)
4783{
4784 struct sctp_sock *sp;
4785
4786 pr_debug("%s: sk:%p\n", __func__, sk);
4787
4788 /* Release our hold on the endpoint. */
4789 sp = sctp_sk(sk);
4790 /* This could happen during socket init, thus we bail out
4791 * early, since the rest of the below is not setup either.
4792 */
4793 if (sp->ep == NULL)
4794 return;
4795
4796 if (sp->do_auto_asconf) {
4797 sp->do_auto_asconf = 0;
4798 list_del(&sp->auto_asconf_list);
4799 }
4800 sctp_endpoint_free(sp->ep);
4801 local_bh_disable();
4802 sk_sockets_allocated_dec(sk);
4803 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
4804 local_bh_enable();
4805}
4806
4807/* Triggered when there are no references on the socket anymore */
4808static void sctp_destruct_sock(struct sock *sk)
4809{
4810 struct sctp_sock *sp = sctp_sk(sk);
4811
4812 /* Free up the HMAC transform. */
4813 crypto_free_shash(sp->hmac);
4814
4815 inet_sock_destruct(sk);
4816}
4817
4818/* API 4.1.7 shutdown() - TCP Style Syntax
4819 * int shutdown(int socket, int how);
4820 *
4821 * sd - the socket descriptor of the association to be closed.
4822 * how - Specifies the type of shutdown. The values are
4823 * as follows:
4824 * SHUT_RD
4825 * Disables further receive operations. No SCTP
4826 * protocol action is taken.
4827 * SHUT_WR
4828 * Disables further send operations, and initiates
4829 * the SCTP shutdown sequence.
4830 * SHUT_RDWR
4831 * Disables further send and receive operations
4832 * and initiates the SCTP shutdown sequence.
4833 */
4834static void sctp_shutdown(struct sock *sk, int how)
4835{
4836 struct net *net = sock_net(sk);
4837 struct sctp_endpoint *ep;
4838
4839 if (!sctp_style(sk, TCP))
4840 return;
4841
4842 ep = sctp_sk(sk)->ep;
4843 if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
4844 struct sctp_association *asoc;
4845
4846 inet_sk_set_state(sk, SCTP_SS_CLOSING);
4847 asoc = list_entry(ep->asocs.next,
4848 struct sctp_association, asocs);
4849 sctp_primitive_SHUTDOWN(net, asoc, NULL);
4850 }
4851}
4852
4853int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
4854 struct sctp_info *info)
4855{
4856 struct sctp_transport *prim;
4857 struct list_head *pos;
4858 int mask;
4859
4860 memset(info, 0, sizeof(*info));
4861 if (!asoc) {
4862 struct sctp_sock *sp = sctp_sk(sk);
4863
4864 info->sctpi_s_autoclose = sp->autoclose;
4865 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
4866 info->sctpi_s_pd_point = sp->pd_point;
4867 info->sctpi_s_nodelay = sp->nodelay;
4868 info->sctpi_s_disable_fragments = sp->disable_fragments;
4869 info->sctpi_s_v4mapped = sp->v4mapped;
4870 info->sctpi_s_frag_interleave = sp->frag_interleave;
4871 info->sctpi_s_type = sp->type;
4872
4873 return 0;
4874 }
4875
4876 info->sctpi_tag = asoc->c.my_vtag;
4877 info->sctpi_state = asoc->state;
4878 info->sctpi_rwnd = asoc->a_rwnd;
4879 info->sctpi_unackdata = asoc->unack_data;
4880 info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
4881 info->sctpi_instrms = asoc->stream.incnt;
4882 info->sctpi_outstrms = asoc->stream.outcnt;
4883 list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
4884 info->sctpi_inqueue++;
4885 list_for_each(pos, &asoc->outqueue.out_chunk_list)
4886 info->sctpi_outqueue++;
4887 info->sctpi_overall_error = asoc->overall_error_count;
4888 info->sctpi_max_burst = asoc->max_burst;
4889 info->sctpi_maxseg = asoc->frag_point;
4890 info->sctpi_peer_rwnd = asoc->peer.rwnd;
4891 info->sctpi_peer_tag = asoc->c.peer_vtag;
4892
4893 mask = asoc->peer.ecn_capable << 1;
4894 mask = (mask | asoc->peer.ipv4_address) << 1;
4895 mask = (mask | asoc->peer.ipv6_address) << 1;
4896 mask = (mask | asoc->peer.hostname_address) << 1;
4897 mask = (mask | asoc->peer.asconf_capable) << 1;
4898 mask = (mask | asoc->peer.prsctp_capable) << 1;
4899 mask = (mask | asoc->peer.auth_capable);
4900 info->sctpi_peer_capable = mask;
4901 mask = asoc->peer.sack_needed << 1;
4902 mask = (mask | asoc->peer.sack_generation) << 1;
4903 mask = (mask | asoc->peer.zero_window_announced);
4904 info->sctpi_peer_sack = mask;
4905
4906 info->sctpi_isacks = asoc->stats.isacks;
4907 info->sctpi_osacks = asoc->stats.osacks;
4908 info->sctpi_opackets = asoc->stats.opackets;
4909 info->sctpi_ipackets = asoc->stats.ipackets;
4910 info->sctpi_rtxchunks = asoc->stats.rtxchunks;
4911 info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
4912 info->sctpi_idupchunks = asoc->stats.idupchunks;
4913 info->sctpi_gapcnt = asoc->stats.gapcnt;
4914 info->sctpi_ouodchunks = asoc->stats.ouodchunks;
4915 info->sctpi_iuodchunks = asoc->stats.iuodchunks;
4916 info->sctpi_oodchunks = asoc->stats.oodchunks;
4917 info->sctpi_iodchunks = asoc->stats.iodchunks;
4918 info->sctpi_octrlchunks = asoc->stats.octrlchunks;
4919 info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
4920
4921 prim = asoc->peer.primary_path;
4922 memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
4923 info->sctpi_p_state = prim->state;
4924 info->sctpi_p_cwnd = prim->cwnd;
4925 info->sctpi_p_srtt = prim->srtt;
4926 info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
4927 info->sctpi_p_hbinterval = prim->hbinterval;
4928 info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
4929 info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
4930 info->sctpi_p_ssthresh = prim->ssthresh;
4931 info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
4932 info->sctpi_p_flight_size = prim->flight_size;
4933 info->sctpi_p_error = prim->error_count;
4934
4935 return 0;
4936}
4937EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
4938
4939/* use callback to avoid exporting the core structure */
4940void sctp_transport_walk_start(struct rhashtable_iter *iter)
4941{
4942 rhltable_walk_enter(&sctp_transport_hashtable, iter);
4943
4944 rhashtable_walk_start(iter);
4945}
4946
4947void sctp_transport_walk_stop(struct rhashtable_iter *iter)
4948{
4949 rhashtable_walk_stop(iter);
4950 rhashtable_walk_exit(iter);
4951}
4952
4953struct sctp_transport *sctp_transport_get_next(struct net *net,
4954 struct rhashtable_iter *iter)
4955{
4956 struct sctp_transport *t;
4957
4958 t = rhashtable_walk_next(iter);
4959 for (; t; t = rhashtable_walk_next(iter)) {
4960 if (IS_ERR(t)) {
4961 if (PTR_ERR(t) == -EAGAIN)
4962 continue;
4963 break;
4964 }
4965
4966 if (!sctp_transport_hold(t))
4967 continue;
4968
4969 if (net_eq(sock_net(t->asoc->base.sk), net) &&
4970 t->asoc->peer.primary_path == t)
4971 break;
4972
4973 sctp_transport_put(t);
4974 }
4975
4976 return t;
4977}
4978
4979struct sctp_transport *sctp_transport_get_idx(struct net *net,
4980 struct rhashtable_iter *iter,
4981 int pos)
4982{
4983 struct sctp_transport *t;
4984
4985 if (!pos)
4986 return SEQ_START_TOKEN;
4987
4988 while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
4989 if (!--pos)
4990 break;
4991 sctp_transport_put(t);
4992 }
4993
4994 return t;
4995}
4996
4997int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
4998 void *p) {
4999 int err = 0;
5000 int hash = 0;
5001 struct sctp_ep_common *epb;
5002 struct sctp_hashbucket *head;
5003
5004 for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
5005 hash++, head++) {
5006 read_lock_bh(&head->lock);
5007 sctp_for_each_hentry(epb, &head->chain) {
5008 err = cb(sctp_ep(epb), p);
5009 if (err)
5010 break;
5011 }
5012 read_unlock_bh(&head->lock);
5013 }
5014
5015 return err;
5016}
5017EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
5018
5019int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
5020 struct net *net,
5021 const union sctp_addr *laddr,
5022 const union sctp_addr *paddr, void *p)
5023{
5024 struct sctp_transport *transport;
5025 int err;
5026
5027 rcu_read_lock();
5028 transport = sctp_addrs_lookup_transport(net, laddr, paddr);
5029 rcu_read_unlock();
5030 if (!transport)
5031 return -ENOENT;
5032
5033 err = cb(transport, p);
5034 sctp_transport_put(transport);
5035
5036 return err;
5037}
5038EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
5039
5040int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
5041 int (*cb_done)(struct sctp_transport *, void *),
5042 struct net *net, int *pos, void *p) {
5043 struct rhashtable_iter hti;
5044 struct sctp_transport *tsp;
5045 int ret;
5046
5047again:
5048 ret = 0;
5049 sctp_transport_walk_start(&hti);
5050
5051 tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5052 for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
5053 ret = cb(tsp, p);
5054 if (ret)
5055 break;
5056 (*pos)++;
5057 sctp_transport_put(tsp);
5058 }
5059 sctp_transport_walk_stop(&hti);
5060
5061 if (ret) {
5062 if (cb_done && !cb_done(tsp, p)) {
5063 (*pos)++;
5064 sctp_transport_put(tsp);
5065 goto again;
5066 }
5067 sctp_transport_put(tsp);
5068 }
5069
5070 return ret;
5071}
5072EXPORT_SYMBOL_GPL(sctp_for_each_transport);
5073
5074/* 7.2.1 Association Status (SCTP_STATUS)
5075
5076 * Applications can retrieve current status information about an
5077 * association, including association state, peer receiver window size,
5078 * number of unacked data chunks, and number of data chunks pending
5079 * receipt. This information is read-only.
5080 */
5081static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5082 char __user *optval,
5083 int __user *optlen)
5084{
5085 struct sctp_status status;
5086 struct sctp_association *asoc = NULL;
5087 struct sctp_transport *transport;
5088 sctp_assoc_t associd;
5089 int retval = 0;
5090
5091 if (len < sizeof(status)) {
5092 retval = -EINVAL;
5093 goto out;
5094 }
5095
5096 len = sizeof(status);
5097 if (copy_from_user(&status, optval, len)) {
5098 retval = -EFAULT;
5099 goto out;
5100 }
5101
5102 associd = status.sstat_assoc_id;
5103 asoc = sctp_id2assoc(sk, associd);
5104 if (!asoc) {
5105 retval = -EINVAL;
5106 goto out;
5107 }
5108
5109 transport = asoc->peer.primary_path;
5110
5111 status.sstat_assoc_id = sctp_assoc2id(asoc);
5112 status.sstat_state = sctp_assoc_to_state(asoc);
5113 status.sstat_rwnd = asoc->peer.rwnd;
5114 status.sstat_unackdata = asoc->unack_data;
5115
5116 status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5117 status.sstat_instrms = asoc->stream.incnt;
5118 status.sstat_outstrms = asoc->stream.outcnt;
5119 status.sstat_fragmentation_point = asoc->frag_point;
5120 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5121 memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5122 transport->af_specific->sockaddr_len);
5123 /* Map ipv4 address into v4-mapped-on-v6 address. */
5124 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
5125 (union sctp_addr *)&status.sstat_primary.spinfo_address);
5126 status.sstat_primary.spinfo_state = transport->state;
5127 status.sstat_primary.spinfo_cwnd = transport->cwnd;
5128 status.sstat_primary.spinfo_srtt = transport->srtt;
5129 status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
5130 status.sstat_primary.spinfo_mtu = transport->pathmtu;
5131
5132 if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5133 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5134
5135 if (put_user(len, optlen)) {
5136 retval = -EFAULT;
5137 goto out;
5138 }
5139
5140 pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5141 __func__, len, status.sstat_state, status.sstat_rwnd,
5142 status.sstat_assoc_id);
5143
5144 if (copy_to_user(optval, &status, len)) {
5145 retval = -EFAULT;
5146 goto out;
5147 }
5148
5149out:
5150 return retval;
5151}
5152
5153
5154/* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5155 *
5156 * Applications can retrieve information about a specific peer address
5157 * of an association, including its reachability state, congestion
5158 * window, and retransmission timer values. This information is
5159 * read-only.
5160 */
5161static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5162 char __user *optval,
5163 int __user *optlen)
5164{
5165 struct sctp_paddrinfo pinfo;
5166 struct sctp_transport *transport;
5167 int retval = 0;
5168
5169 if (len < sizeof(pinfo)) {
5170 retval = -EINVAL;
5171 goto out;
5172 }
5173
5174 len = sizeof(pinfo);
5175 if (copy_from_user(&pinfo, optval, len)) {
5176 retval = -EFAULT;
5177 goto out;
5178 }
5179
5180 transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5181 pinfo.spinfo_assoc_id);
5182 if (!transport)
5183 return -EINVAL;
5184
5185 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5186 pinfo.spinfo_state = transport->state;
5187 pinfo.spinfo_cwnd = transport->cwnd;
5188 pinfo.spinfo_srtt = transport->srtt;
5189 pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
5190 pinfo.spinfo_mtu = transport->pathmtu;
5191
5192 if (pinfo.spinfo_state == SCTP_UNKNOWN)
5193 pinfo.spinfo_state = SCTP_ACTIVE;
5194
5195 if (put_user(len, optlen)) {
5196 retval = -EFAULT;
5197 goto out;
5198 }
5199
5200 if (copy_to_user(optval, &pinfo, len)) {
5201 retval = -EFAULT;
5202 goto out;
5203 }
5204
5205out:
5206 return retval;
5207}
5208
5209/* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5210 *
5211 * This option is a on/off flag. If enabled no SCTP message
5212 * fragmentation will be performed. Instead if a message being sent
5213 * exceeds the current PMTU size, the message will NOT be sent and
5214 * instead a error will be indicated to the user.
5215 */
5216static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5217 char __user *optval, int __user *optlen)
5218{
5219 int val;
5220
5221 if (len < sizeof(int))
5222 return -EINVAL;
5223
5224 len = sizeof(int);
5225 val = (sctp_sk(sk)->disable_fragments == 1);
5226 if (put_user(len, optlen))
5227 return -EFAULT;
5228 if (copy_to_user(optval, &val, len))
5229 return -EFAULT;
5230 return 0;
5231}
5232
5233/* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5234 *
5235 * This socket option is used to specify various notifications and
5236 * ancillary data the user wishes to receive.
5237 */
5238static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5239 int __user *optlen)
5240{
5241 if (len == 0)
5242 return -EINVAL;
5243 if (len > sizeof(struct sctp_event_subscribe))
5244 len = sizeof(struct sctp_event_subscribe);
5245 if (put_user(len, optlen))
5246 return -EFAULT;
5247 if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
5248 return -EFAULT;
5249 return 0;
5250}
5251
5252/* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5253 *
5254 * This socket option is applicable to the UDP-style socket only. When
5255 * set it will cause associations that are idle for more than the
5256 * specified number of seconds to automatically close. An association
5257 * being idle is defined an association that has NOT sent or received
5258 * user data. The special value of '0' indicates that no automatic
5259 * close of any associations should be performed. The option expects an
5260 * integer defining the number of seconds of idle time before an
5261 * association is closed.
5262 */
5263static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5264{
5265 /* Applicable to UDP-style socket only */
5266 if (sctp_style(sk, TCP))
5267 return -EOPNOTSUPP;
5268 if (len < sizeof(int))
5269 return -EINVAL;
5270 len = sizeof(int);
5271 if (put_user(len, optlen))
5272 return -EFAULT;
5273 if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
5274 return -EFAULT;
5275 return 0;
5276}
5277
5278/* Helper routine to branch off an association to a new socket. */
5279int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
5280{
5281 struct sctp_association *asoc = sctp_id2assoc(sk, id);
5282 struct sctp_sock *sp = sctp_sk(sk);
5283 struct socket *sock;
5284 int err = 0;
5285
5286 /* Do not peel off from one netns to another one. */
5287 if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5288 return -EINVAL;
5289
5290 if (!asoc)
5291 return -EINVAL;
5292
5293 /* An association cannot be branched off from an already peeled-off
5294 * socket, nor is this supported for tcp style sockets.
5295 */
5296 if (!sctp_style(sk, UDP))
5297 return -EINVAL;
5298
5299 /* Create a new socket. */
5300 err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5301 if (err < 0)
5302 return err;
5303
5304 sctp_copy_sock(sock->sk, sk, asoc);
5305
5306 /* Make peeled-off sockets more like 1-1 accepted sockets.
5307 * Set the daddr and initialize id to something more random and also
5308 * copy over any ip options.
5309 */
5310 sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
5311 sp->pf->copy_ip_options(sk, sock->sk);
5312
5313 /* Populate the fields of the newsk from the oldsk and migrate the
5314 * asoc to the newsk.
5315 */
5316 sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
5317
5318 *sockp = sock;
5319
5320 return err;
5321}
5322EXPORT_SYMBOL(sctp_do_peeloff);
5323
5324static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5325 struct file **newfile, unsigned flags)
5326{
5327 struct socket *newsock;
5328 int retval;
5329
5330 retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5331 if (retval < 0)
5332 goto out;
5333
5334 /* Map the socket to an unused fd that can be returned to the user. */
5335 retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5336 if (retval < 0) {
5337 sock_release(newsock);
5338 goto out;
5339 }
5340
5341 *newfile = sock_alloc_file(newsock, 0, NULL);
5342 if (IS_ERR(*newfile)) {
5343 put_unused_fd(retval);
5344 retval = PTR_ERR(*newfile);
5345 *newfile = NULL;
5346 return retval;
5347 }
5348
5349 pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5350 retval);
5351
5352 peeloff->sd = retval;
5353
5354 if (flags & SOCK_NONBLOCK)
5355 (*newfile)->f_flags |= O_NONBLOCK;
5356out:
5357 return retval;
5358}
5359
5360static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5361{
5362 sctp_peeloff_arg_t peeloff;
5363 struct file *newfile = NULL;
5364 int retval = 0;
5365
5366 if (len < sizeof(sctp_peeloff_arg_t))
5367 return -EINVAL;
5368 len = sizeof(sctp_peeloff_arg_t);
5369 if (copy_from_user(&peeloff, optval, len))
5370 return -EFAULT;
5371
5372 retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
5373 if (retval < 0)
5374 goto out;
5375
5376 /* Return the fd mapped to the new socket. */
5377 if (put_user(len, optlen)) {
5378 fput(newfile);
5379 put_unused_fd(retval);
5380 return -EFAULT;
5381 }
5382
5383 if (copy_to_user(optval, &peeloff, len)) {
5384 fput(newfile);
5385 put_unused_fd(retval);
5386 return -EFAULT;
5387 }
5388 fd_install(retval, newfile);
5389out:
5390 return retval;
5391}
5392
5393static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5394 char __user *optval, int __user *optlen)
5395{
5396 sctp_peeloff_flags_arg_t peeloff;
5397 struct file *newfile = NULL;
5398 int retval = 0;
5399
5400 if (len < sizeof(sctp_peeloff_flags_arg_t))
5401 return -EINVAL;
5402 len = sizeof(sctp_peeloff_flags_arg_t);
5403 if (copy_from_user(&peeloff, optval, len))
5404 return -EFAULT;
5405
5406 retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5407 &newfile, peeloff.flags);
5408 if (retval < 0)
5409 goto out;
5410
5411 /* Return the fd mapped to the new socket. */
5412 if (put_user(len, optlen)) {
5413 fput(newfile);
5414 put_unused_fd(retval);
5415 return -EFAULT;
5416 }
5417
5418 if (copy_to_user(optval, &peeloff, len)) {
5419 fput(newfile);
5420 put_unused_fd(retval);
5421 return -EFAULT;
5422 }
5423 fd_install(retval, newfile);
5424out:
5425 return retval;
5426}
5427
5428/* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5429 *
5430 * Applications can enable or disable heartbeats for any peer address of
5431 * an association, modify an address's heartbeat interval, force a
5432 * heartbeat to be sent immediately, and adjust the address's maximum
5433 * number of retransmissions sent before an address is considered
5434 * unreachable. The following structure is used to access and modify an
5435 * address's parameters:
5436 *
5437 * struct sctp_paddrparams {
5438 * sctp_assoc_t spp_assoc_id;
5439 * struct sockaddr_storage spp_address;
5440 * uint32_t spp_hbinterval;
5441 * uint16_t spp_pathmaxrxt;
5442 * uint32_t spp_pathmtu;
5443 * uint32_t spp_sackdelay;
5444 * uint32_t spp_flags;
5445 * };
5446 *
5447 * spp_assoc_id - (one-to-many style socket) This is filled in the
5448 * application, and identifies the association for
5449 * this query.
5450 * spp_address - This specifies which address is of interest.
5451 * spp_hbinterval - This contains the value of the heartbeat interval,
5452 * in milliseconds. If a value of zero
5453 * is present in this field then no changes are to
5454 * be made to this parameter.
5455 * spp_pathmaxrxt - This contains the maximum number of
5456 * retransmissions before this address shall be
5457 * considered unreachable. If a value of zero
5458 * is present in this field then no changes are to
5459 * be made to this parameter.
5460 * spp_pathmtu - When Path MTU discovery is disabled the value
5461 * specified here will be the "fixed" path mtu.
5462 * Note that if the spp_address field is empty
5463 * then all associations on this address will
5464 * have this fixed path mtu set upon them.
5465 *
5466 * spp_sackdelay - When delayed sack is enabled, this value specifies
5467 * the number of milliseconds that sacks will be delayed
5468 * for. This value will apply to all addresses of an
5469 * association if the spp_address field is empty. Note
5470 * also, that if delayed sack is enabled and this
5471 * value is set to 0, no change is made to the last
5472 * recorded delayed sack timer value.
5473 *
5474 * spp_flags - These flags are used to control various features
5475 * on an association. The flag field may contain
5476 * zero or more of the following options.
5477 *
5478 * SPP_HB_ENABLE - Enable heartbeats on the
5479 * specified address. Note that if the address
5480 * field is empty all addresses for the association
5481 * have heartbeats enabled upon them.
5482 *
5483 * SPP_HB_DISABLE - Disable heartbeats on the
5484 * speicifed address. Note that if the address
5485 * field is empty all addresses for the association
5486 * will have their heartbeats disabled. Note also
5487 * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5488 * mutually exclusive, only one of these two should
5489 * be specified. Enabling both fields will have
5490 * undetermined results.
5491 *
5492 * SPP_HB_DEMAND - Request a user initiated heartbeat
5493 * to be made immediately.
5494 *
5495 * SPP_PMTUD_ENABLE - This field will enable PMTU
5496 * discovery upon the specified address. Note that
5497 * if the address feild is empty then all addresses
5498 * on the association are effected.
5499 *
5500 * SPP_PMTUD_DISABLE - This field will disable PMTU
5501 * discovery upon the specified address. Note that
5502 * if the address feild is empty then all addresses
5503 * on the association are effected. Not also that
5504 * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5505 * exclusive. Enabling both will have undetermined
5506 * results.
5507 *
5508 * SPP_SACKDELAY_ENABLE - Setting this flag turns
5509 * on delayed sack. The time specified in spp_sackdelay
5510 * is used to specify the sack delay for this address. Note
5511 * that if spp_address is empty then all addresses will
5512 * enable delayed sack and take on the sack delay
5513 * value specified in spp_sackdelay.
5514 * SPP_SACKDELAY_DISABLE - Setting this flag turns
5515 * off delayed sack. If the spp_address field is blank then
5516 * delayed sack is disabled for the entire association. Note
5517 * also that this field is mutually exclusive to
5518 * SPP_SACKDELAY_ENABLE, setting both will have undefined
5519 * results.
5520 *
5521 * SPP_IPV6_FLOWLABEL: Setting this flag enables the
5522 * setting of the IPV6 flow label value. The value is
5523 * contained in the spp_ipv6_flowlabel field.
5524 * Upon retrieval, this flag will be set to indicate that
5525 * the spp_ipv6_flowlabel field has a valid value returned.
5526 * If a specific destination address is set (in the
5527 * spp_address field), then the value returned is that of
5528 * the address. If just an association is specified (and
5529 * no address), then the association's default flow label
5530 * is returned. If neither an association nor a destination
5531 * is specified, then the socket's default flow label is
5532 * returned. For non-IPv6 sockets, this flag will be left
5533 * cleared.
5534 *
5535 * SPP_DSCP: Setting this flag enables the setting of the
5536 * Differentiated Services Code Point (DSCP) value
5537 * associated with either the association or a specific
5538 * address. The value is obtained in the spp_dscp field.
5539 * Upon retrieval, this flag will be set to indicate that
5540 * the spp_dscp field has a valid value returned. If a
5541 * specific destination address is set when called (in the
5542 * spp_address field), then that specific destination
5543 * address's DSCP value is returned. If just an association
5544 * is specified, then the association's default DSCP is
5545 * returned. If neither an association nor a destination is
5546 * specified, then the socket's default DSCP is returned.
5547 *
5548 * spp_ipv6_flowlabel
5549 * - This field is used in conjunction with the
5550 * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
5551 * The 20 least significant bits are used for the flow
5552 * label. This setting has precedence over any IPv6-layer
5553 * setting.
5554 *
5555 * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
5556 * and contains the DSCP. The 6 most significant bits are
5557 * used for the DSCP. This setting has precedence over any
5558 * IPv4- or IPv6- layer setting.
5559 */
5560static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
5561 char __user *optval, int __user *optlen)
5562{
5563 struct sctp_paddrparams params;
5564 struct sctp_transport *trans = NULL;
5565 struct sctp_association *asoc = NULL;
5566 struct sctp_sock *sp = sctp_sk(sk);
5567
5568 if (len >= sizeof(params))
5569 len = sizeof(params);
5570 else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
5571 spp_ipv6_flowlabel), 4))
5572 len = ALIGN(offsetof(struct sctp_paddrparams,
5573 spp_ipv6_flowlabel), 4);
5574 else
5575 return -EINVAL;
5576
5577 if (copy_from_user(&params, optval, len))
5578 return -EFAULT;
5579
5580 /* If an address other than INADDR_ANY is specified, and
5581 * no transport is found, then the request is invalid.
5582 */
5583 if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
5584 trans = sctp_addr_id2transport(sk, &params.spp_address,
5585 params.spp_assoc_id);
5586 if (!trans) {
5587 pr_debug("%s: failed no transport\n", __func__);
5588 return -EINVAL;
5589 }
5590 }
5591
5592 /* Get association, if assoc_id != 0 and the socket is a one
5593 * to many style socket, and an association was not found, then
5594 * the id was invalid.
5595 */
5596 asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5597 if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
5598 pr_debug("%s: failed no association\n", __func__);
5599 return -EINVAL;
5600 }
5601
5602 if (trans) {
5603 /* Fetch transport values. */
5604 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5605 params.spp_pathmtu = trans->pathmtu;
5606 params.spp_pathmaxrxt = trans->pathmaxrxt;
5607 params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
5608
5609 /*draft-11 doesn't say what to return in spp_flags*/
5610 params.spp_flags = trans->param_flags;
5611 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5612 params.spp_ipv6_flowlabel = trans->flowlabel &
5613 SCTP_FLOWLABEL_VAL_MASK;
5614 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5615 }
5616 if (trans->dscp & SCTP_DSCP_SET_MASK) {
5617 params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
5618 params.spp_flags |= SPP_DSCP;
5619 }
5620 } else if (asoc) {
5621 /* Fetch association values. */
5622 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5623 params.spp_pathmtu = asoc->pathmtu;
5624 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5625 params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
5626
5627 /*draft-11 doesn't say what to return in spp_flags*/
5628 params.spp_flags = asoc->param_flags;
5629 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5630 params.spp_ipv6_flowlabel = asoc->flowlabel &
5631 SCTP_FLOWLABEL_VAL_MASK;
5632 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5633 }
5634 if (asoc->dscp & SCTP_DSCP_SET_MASK) {
5635 params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
5636 params.spp_flags |= SPP_DSCP;
5637 }
5638 } else {
5639 /* Fetch socket values. */
5640 params.spp_hbinterval = sp->hbinterval;
5641 params.spp_pathmtu = sp->pathmtu;
5642 params.spp_sackdelay = sp->sackdelay;
5643 params.spp_pathmaxrxt = sp->pathmaxrxt;
5644
5645 /*draft-11 doesn't say what to return in spp_flags*/
5646 params.spp_flags = sp->param_flags;
5647 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5648 params.spp_ipv6_flowlabel = sp->flowlabel &
5649 SCTP_FLOWLABEL_VAL_MASK;
5650 params.spp_flags |= SPP_IPV6_FLOWLABEL;
5651 }
5652 if (sp->dscp & SCTP_DSCP_SET_MASK) {
5653 params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
5654 params.spp_flags |= SPP_DSCP;
5655 }
5656 }
5657
5658 if (copy_to_user(optval, &params, len))
5659 return -EFAULT;
5660
5661 if (put_user(len, optlen))
5662 return -EFAULT;
5663
5664 return 0;
5665}
5666
5667/*
5668 * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
5669 *
5670 * This option will effect the way delayed acks are performed. This
5671 * option allows you to get or set the delayed ack time, in
5672 * milliseconds. It also allows changing the delayed ack frequency.
5673 * Changing the frequency to 1 disables the delayed sack algorithm. If
5674 * the assoc_id is 0, then this sets or gets the endpoints default
5675 * values. If the assoc_id field is non-zero, then the set or get
5676 * effects the specified association for the one to many model (the
5677 * assoc_id field is ignored by the one to one model). Note that if
5678 * sack_delay or sack_freq are 0 when setting this option, then the
5679 * current values will remain unchanged.
5680 *
5681 * struct sctp_sack_info {
5682 * sctp_assoc_t sack_assoc_id;
5683 * uint32_t sack_delay;
5684 * uint32_t sack_freq;
5685 * };
5686 *
5687 * sack_assoc_id - This parameter, indicates which association the user
5688 * is performing an action upon. Note that if this field's value is
5689 * zero then the endpoints default value is changed (effecting future
5690 * associations only).
5691 *
5692 * sack_delay - This parameter contains the number of milliseconds that
5693 * the user is requesting the delayed ACK timer be set to. Note that
5694 * this value is defined in the standard to be between 200 and 500
5695 * milliseconds.
5696 *
5697 * sack_freq - This parameter contains the number of packets that must
5698 * be received before a sack is sent without waiting for the delay
5699 * timer to expire. The default value for this is 2, setting this
5700 * value to 1 will disable the delayed sack algorithm.
5701 */
5702static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
5703 char __user *optval,
5704 int __user *optlen)
5705{
5706 struct sctp_sack_info params;
5707 struct sctp_association *asoc = NULL;
5708 struct sctp_sock *sp = sctp_sk(sk);
5709
5710 if (len >= sizeof(struct sctp_sack_info)) {
5711 len = sizeof(struct sctp_sack_info);
5712
5713 if (copy_from_user(&params, optval, len))
5714 return -EFAULT;
5715 } else if (len == sizeof(struct sctp_assoc_value)) {
5716 pr_warn_ratelimited(DEPRECATED
5717 "%s (pid %d) "
5718 "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
5719 "Use struct sctp_sack_info instead\n",
5720 current->comm, task_pid_nr(current));
5721 if (copy_from_user(&params, optval, len))
5722 return -EFAULT;
5723 } else
5724 return -EINVAL;
5725
5726 /* Get association, if sack_assoc_id != 0 and the socket is a one
5727 * to many style socket, and an association was not found, then
5728 * the id was invalid.
5729 */
5730 asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5731 if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
5732 return -EINVAL;
5733
5734 if (asoc) {
5735 /* Fetch association values. */
5736 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5737 params.sack_delay = jiffies_to_msecs(
5738 asoc->sackdelay);
5739 params.sack_freq = asoc->sackfreq;
5740
5741 } else {
5742 params.sack_delay = 0;
5743 params.sack_freq = 1;
5744 }
5745 } else {
5746 /* Fetch socket values. */
5747 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5748 params.sack_delay = sp->sackdelay;
5749 params.sack_freq = sp->sackfreq;
5750 } else {
5751 params.sack_delay = 0;
5752 params.sack_freq = 1;
5753 }
5754 }
5755
5756 if (copy_to_user(optval, &params, len))
5757 return -EFAULT;
5758
5759 if (put_user(len, optlen))
5760 return -EFAULT;
5761
5762 return 0;
5763}
5764
5765/* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5766 *
5767 * Applications can specify protocol parameters for the default association
5768 * initialization. The option name argument to setsockopt() and getsockopt()
5769 * is SCTP_INITMSG.
5770 *
5771 * Setting initialization parameters is effective only on an unconnected
5772 * socket (for UDP-style sockets only future associations are effected
5773 * by the change). With TCP-style sockets, this option is inherited by
5774 * sockets derived from a listener socket.
5775 */
5776static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5777{
5778 if (len < sizeof(struct sctp_initmsg))
5779 return -EINVAL;
5780 len = sizeof(struct sctp_initmsg);
5781 if (put_user(len, optlen))
5782 return -EFAULT;
5783 if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
5784 return -EFAULT;
5785 return 0;
5786}
5787
5788
5789static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
5790 char __user *optval, int __user *optlen)
5791{
5792 struct sctp_association *asoc;
5793 int cnt = 0;
5794 struct sctp_getaddrs getaddrs;
5795 struct sctp_transport *from;
5796 void __user *to;
5797 union sctp_addr temp;
5798 struct sctp_sock *sp = sctp_sk(sk);
5799 int addrlen;
5800 size_t space_left;
5801 int bytes_copied;
5802
5803 if (len < sizeof(struct sctp_getaddrs))
5804 return -EINVAL;
5805
5806 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5807 return -EFAULT;
5808
5809 /* For UDP-style sockets, id specifies the association to query. */
5810 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5811 if (!asoc)
5812 return -EINVAL;
5813
5814 to = optval + offsetof(struct sctp_getaddrs, addrs);
5815 space_left = len - offsetof(struct sctp_getaddrs, addrs);
5816
5817 list_for_each_entry(from, &asoc->peer.transport_addr_list,
5818 transports) {
5819 memcpy(&temp, &from->ipaddr, sizeof(temp));
5820 addrlen = sctp_get_pf_specific(sk->sk_family)
5821 ->addr_to_user(sp, &temp);
5822 if (space_left < addrlen)
5823 return -ENOMEM;
5824 if (copy_to_user(to, &temp, addrlen))
5825 return -EFAULT;
5826 to += addrlen;
5827 cnt++;
5828 space_left -= addrlen;
5829 }
5830
5831 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
5832 return -EFAULT;
5833 bytes_copied = ((char __user *)to) - optval;
5834 if (put_user(bytes_copied, optlen))
5835 return -EFAULT;
5836
5837 return 0;
5838}
5839
5840static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
5841 size_t space_left, int *bytes_copied)
5842{
5843 struct sctp_sockaddr_entry *addr;
5844 union sctp_addr temp;
5845 int cnt = 0;
5846 int addrlen;
5847 struct net *net = sock_net(sk);
5848
5849 rcu_read_lock();
5850 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
5851 if (!addr->valid)
5852 continue;
5853
5854 if ((PF_INET == sk->sk_family) &&
5855 (AF_INET6 == addr->a.sa.sa_family))
5856 continue;
5857 if ((PF_INET6 == sk->sk_family) &&
5858 inet_v6_ipv6only(sk) &&
5859 (AF_INET == addr->a.sa.sa_family))
5860 continue;
5861 memcpy(&temp, &addr->a, sizeof(temp));
5862 if (!temp.v4.sin_port)
5863 temp.v4.sin_port = htons(port);
5864
5865 addrlen = sctp_get_pf_specific(sk->sk_family)
5866 ->addr_to_user(sctp_sk(sk), &temp);
5867
5868 if (space_left < addrlen) {
5869 cnt = -ENOMEM;
5870 break;
5871 }
5872 memcpy(to, &temp, addrlen);
5873
5874 to += addrlen;
5875 cnt++;
5876 space_left -= addrlen;
5877 *bytes_copied += addrlen;
5878 }
5879 rcu_read_unlock();
5880
5881 return cnt;
5882}
5883
5884
5885static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
5886 char __user *optval, int __user *optlen)
5887{
5888 struct sctp_bind_addr *bp;
5889 struct sctp_association *asoc;
5890 int cnt = 0;
5891 struct sctp_getaddrs getaddrs;
5892 struct sctp_sockaddr_entry *addr;
5893 void __user *to;
5894 union sctp_addr temp;
5895 struct sctp_sock *sp = sctp_sk(sk);
5896 int addrlen;
5897 int err = 0;
5898 size_t space_left;
5899 int bytes_copied = 0;
5900 void *addrs;
5901 void *buf;
5902
5903 if (len < sizeof(struct sctp_getaddrs))
5904 return -EINVAL;
5905
5906 if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
5907 return -EFAULT;
5908
5909 /*
5910 * For UDP-style sockets, id specifies the association to query.
5911 * If the id field is set to the value '0' then the locally bound
5912 * addresses are returned without regard to any particular
5913 * association.
5914 */
5915 if (0 == getaddrs.assoc_id) {
5916 bp = &sctp_sk(sk)->ep->base.bind_addr;
5917 } else {
5918 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
5919 if (!asoc)
5920 return -EINVAL;
5921 bp = &asoc->base.bind_addr;
5922 }
5923
5924 to = optval + offsetof(struct sctp_getaddrs, addrs);
5925 space_left = len - offsetof(struct sctp_getaddrs, addrs);
5926
5927 addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
5928 if (!addrs)
5929 return -ENOMEM;
5930
5931 /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
5932 * addresses from the global local address list.
5933 */
5934 if (sctp_list_single_entry(&bp->address_list)) {
5935 addr = list_entry(bp->address_list.next,
5936 struct sctp_sockaddr_entry, list);
5937 if (sctp_is_any(sk, &addr->a)) {
5938 cnt = sctp_copy_laddrs(sk, bp->port, addrs,
5939 space_left, &bytes_copied);
5940 if (cnt < 0) {
5941 err = cnt;
5942 goto out;
5943 }
5944 goto copy_getaddrs;
5945 }
5946 }
5947
5948 buf = addrs;
5949 /* Protection on the bound address list is not needed since
5950 * in the socket option context we hold a socket lock and
5951 * thus the bound address list can't change.
5952 */
5953 list_for_each_entry(addr, &bp->address_list, list) {
5954 memcpy(&temp, &addr->a, sizeof(temp));
5955 addrlen = sctp_get_pf_specific(sk->sk_family)
5956 ->addr_to_user(sp, &temp);
5957 if (space_left < addrlen) {
5958 err = -ENOMEM; /*fixme: right error?*/
5959 goto out;
5960 }
5961 memcpy(buf, &temp, addrlen);
5962 buf += addrlen;
5963 bytes_copied += addrlen;
5964 cnt++;
5965 space_left -= addrlen;
5966 }
5967
5968copy_getaddrs:
5969 if (copy_to_user(to, addrs, bytes_copied)) {
5970 err = -EFAULT;
5971 goto out;
5972 }
5973 if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
5974 err = -EFAULT;
5975 goto out;
5976 }
5977 /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
5978 * but we can't change it anymore.
5979 */
5980 if (put_user(bytes_copied, optlen))
5981 err = -EFAULT;
5982out:
5983 kfree(addrs);
5984 return err;
5985}
5986
5987/* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
5988 *
5989 * Requests that the local SCTP stack use the enclosed peer address as
5990 * the association primary. The enclosed address must be one of the
5991 * association peer's addresses.
5992 */
5993static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
5994 char __user *optval, int __user *optlen)
5995{
5996 struct sctp_prim prim;
5997 struct sctp_association *asoc;
5998 struct sctp_sock *sp = sctp_sk(sk);
5999
6000 if (len < sizeof(struct sctp_prim))
6001 return -EINVAL;
6002
6003 len = sizeof(struct sctp_prim);
6004
6005 if (copy_from_user(&prim, optval, len))
6006 return -EFAULT;
6007
6008 asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
6009 if (!asoc)
6010 return -EINVAL;
6011
6012 if (!asoc->peer.primary_path)
6013 return -ENOTCONN;
6014
6015 memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
6016 asoc->peer.primary_path->af_specific->sockaddr_len);
6017
6018 sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
6019 (union sctp_addr *)&prim.ssp_addr);
6020
6021 if (put_user(len, optlen))
6022 return -EFAULT;
6023 if (copy_to_user(optval, &prim, len))
6024 return -EFAULT;
6025
6026 return 0;
6027}
6028
6029/*
6030 * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
6031 *
6032 * Requests that the local endpoint set the specified Adaptation Layer
6033 * Indication parameter for all future INIT and INIT-ACK exchanges.
6034 */
6035static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
6036 char __user *optval, int __user *optlen)
6037{
6038 struct sctp_setadaptation adaptation;
6039
6040 if (len < sizeof(struct sctp_setadaptation))
6041 return -EINVAL;
6042
6043 len = sizeof(struct sctp_setadaptation);
6044
6045 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
6046
6047 if (put_user(len, optlen))
6048 return -EFAULT;
6049 if (copy_to_user(optval, &adaptation, len))
6050 return -EFAULT;
6051
6052 return 0;
6053}
6054
6055/*
6056 *
6057 * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
6058 *
6059 * Applications that wish to use the sendto() system call may wish to
6060 * specify a default set of parameters that would normally be supplied
6061 * through the inclusion of ancillary data. This socket option allows
6062 * such an application to set the default sctp_sndrcvinfo structure.
6063
6064
6065 * The application that wishes to use this socket option simply passes
6066 * in to this call the sctp_sndrcvinfo structure defined in Section
6067 * 5.2.2) The input parameters accepted by this call include
6068 * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
6069 * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
6070 * to this call if the caller is using the UDP model.
6071 *
6072 * For getsockopt, it get the default sctp_sndrcvinfo structure.
6073 */
6074static int sctp_getsockopt_default_send_param(struct sock *sk,
6075 int len, char __user *optval,
6076 int __user *optlen)
6077{
6078 struct sctp_sock *sp = sctp_sk(sk);
6079 struct sctp_association *asoc;
6080 struct sctp_sndrcvinfo info;
6081
6082 if (len < sizeof(info))
6083 return -EINVAL;
6084
6085 len = sizeof(info);
6086
6087 if (copy_from_user(&info, optval, len))
6088 return -EFAULT;
6089
6090 asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
6091 if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
6092 return -EINVAL;
6093 if (asoc) {
6094 info.sinfo_stream = asoc->default_stream;
6095 info.sinfo_flags = asoc->default_flags;
6096 info.sinfo_ppid = asoc->default_ppid;
6097 info.sinfo_context = asoc->default_context;
6098 info.sinfo_timetolive = asoc->default_timetolive;
6099 } else {
6100 info.sinfo_stream = sp->default_stream;
6101 info.sinfo_flags = sp->default_flags;
6102 info.sinfo_ppid = sp->default_ppid;
6103 info.sinfo_context = sp->default_context;
6104 info.sinfo_timetolive = sp->default_timetolive;
6105 }
6106
6107 if (put_user(len, optlen))
6108 return -EFAULT;
6109 if (copy_to_user(optval, &info, len))
6110 return -EFAULT;
6111
6112 return 0;
6113}
6114
6115/* RFC6458, Section 8.1.31. Set/get Default Send Parameters
6116 * (SCTP_DEFAULT_SNDINFO)
6117 */
6118static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
6119 char __user *optval,
6120 int __user *optlen)
6121{
6122 struct sctp_sock *sp = sctp_sk(sk);
6123 struct sctp_association *asoc;
6124 struct sctp_sndinfo info;
6125
6126 if (len < sizeof(info))
6127 return -EINVAL;
6128
6129 len = sizeof(info);
6130
6131 if (copy_from_user(&info, optval, len))
6132 return -EFAULT;
6133
6134 asoc = sctp_id2assoc(sk, info.snd_assoc_id);
6135 if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
6136 return -EINVAL;
6137 if (asoc) {
6138 info.snd_sid = asoc->default_stream;
6139 info.snd_flags = asoc->default_flags;
6140 info.snd_ppid = asoc->default_ppid;
6141 info.snd_context = asoc->default_context;
6142 } else {
6143 info.snd_sid = sp->default_stream;
6144 info.snd_flags = sp->default_flags;
6145 info.snd_ppid = sp->default_ppid;
6146 info.snd_context = sp->default_context;
6147 }
6148
6149 if (put_user(len, optlen))
6150 return -EFAULT;
6151 if (copy_to_user(optval, &info, len))
6152 return -EFAULT;
6153
6154 return 0;
6155}
6156
6157/*
6158 *
6159 * 7.1.5 SCTP_NODELAY
6160 *
6161 * Turn on/off any Nagle-like algorithm. This means that packets are
6162 * generally sent as soon as possible and no unnecessary delays are
6163 * introduced, at the cost of more packets in the network. Expects an
6164 * integer boolean flag.
6165 */
6166
6167static int sctp_getsockopt_nodelay(struct sock *sk, int len,
6168 char __user *optval, int __user *optlen)
6169{
6170 int val;
6171
6172 if (len < sizeof(int))
6173 return -EINVAL;
6174
6175 len = sizeof(int);
6176 val = (sctp_sk(sk)->nodelay == 1);
6177 if (put_user(len, optlen))
6178 return -EFAULT;
6179 if (copy_to_user(optval, &val, len))
6180 return -EFAULT;
6181 return 0;
6182}
6183
6184/*
6185 *
6186 * 7.1.1 SCTP_RTOINFO
6187 *
6188 * The protocol parameters used to initialize and bound retransmission
6189 * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
6190 * and modify these parameters.
6191 * All parameters are time values, in milliseconds. A value of 0, when
6192 * modifying the parameters, indicates that the current value should not
6193 * be changed.
6194 *
6195 */
6196static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6197 char __user *optval,
6198 int __user *optlen) {
6199 struct sctp_rtoinfo rtoinfo;
6200 struct sctp_association *asoc;
6201
6202 if (len < sizeof (struct sctp_rtoinfo))
6203 return -EINVAL;
6204
6205 len = sizeof(struct sctp_rtoinfo);
6206
6207 if (copy_from_user(&rtoinfo, optval, len))
6208 return -EFAULT;
6209
6210 asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6211
6212 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
6213 return -EINVAL;
6214
6215 /* Values corresponding to the specific association. */
6216 if (asoc) {
6217 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6218 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6219 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6220 } else {
6221 /* Values corresponding to the endpoint. */
6222 struct sctp_sock *sp = sctp_sk(sk);
6223
6224 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6225 rtoinfo.srto_max = sp->rtoinfo.srto_max;
6226 rtoinfo.srto_min = sp->rtoinfo.srto_min;
6227 }
6228
6229 if (put_user(len, optlen))
6230 return -EFAULT;
6231
6232 if (copy_to_user(optval, &rtoinfo, len))
6233 return -EFAULT;
6234
6235 return 0;
6236}
6237
6238/*
6239 *
6240 * 7.1.2 SCTP_ASSOCINFO
6241 *
6242 * This option is used to tune the maximum retransmission attempts
6243 * of the association.
6244 * Returns an error if the new association retransmission value is
6245 * greater than the sum of the retransmission value of the peer.
6246 * See [SCTP] for more information.
6247 *
6248 */
6249static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6250 char __user *optval,
6251 int __user *optlen)
6252{
6253
6254 struct sctp_assocparams assocparams;
6255 struct sctp_association *asoc;
6256 struct list_head *pos;
6257 int cnt = 0;
6258
6259 if (len < sizeof (struct sctp_assocparams))
6260 return -EINVAL;
6261
6262 len = sizeof(struct sctp_assocparams);
6263
6264 if (copy_from_user(&assocparams, optval, len))
6265 return -EFAULT;
6266
6267 asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6268
6269 if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
6270 return -EINVAL;
6271
6272 /* Values correspoinding to the specific association */
6273 if (asoc) {
6274 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6275 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6276 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
6277 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
6278
6279 list_for_each(pos, &asoc->peer.transport_addr_list) {
6280 cnt++;
6281 }
6282
6283 assocparams.sasoc_number_peer_destinations = cnt;
6284 } else {
6285 /* Values corresponding to the endpoint */
6286 struct sctp_sock *sp = sctp_sk(sk);
6287
6288 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6289 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6290 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6291 assocparams.sasoc_cookie_life =
6292 sp->assocparams.sasoc_cookie_life;
6293 assocparams.sasoc_number_peer_destinations =
6294 sp->assocparams.
6295 sasoc_number_peer_destinations;
6296 }
6297
6298 if (put_user(len, optlen))
6299 return -EFAULT;
6300
6301 if (copy_to_user(optval, &assocparams, len))
6302 return -EFAULT;
6303
6304 return 0;
6305}
6306
6307/*
6308 * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6309 *
6310 * This socket option is a boolean flag which turns on or off mapped V4
6311 * addresses. If this option is turned on and the socket is type
6312 * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6313 * If this option is turned off, then no mapping will be done of V4
6314 * addresses and a user will receive both PF_INET6 and PF_INET type
6315 * addresses on the socket.
6316 */
6317static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6318 char __user *optval, int __user *optlen)
6319{
6320 int val;
6321 struct sctp_sock *sp = sctp_sk(sk);
6322
6323 if (len < sizeof(int))
6324 return -EINVAL;
6325
6326 len = sizeof(int);
6327 val = sp->v4mapped;
6328 if (put_user(len, optlen))
6329 return -EFAULT;
6330 if (copy_to_user(optval, &val, len))
6331 return -EFAULT;
6332
6333 return 0;
6334}
6335
6336/*
6337 * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6338 * (chapter and verse is quoted at sctp_setsockopt_context())
6339 */
6340static int sctp_getsockopt_context(struct sock *sk, int len,
6341 char __user *optval, int __user *optlen)
6342{
6343 struct sctp_assoc_value params;
6344 struct sctp_sock *sp;
6345 struct sctp_association *asoc;
6346
6347 if (len < sizeof(struct sctp_assoc_value))
6348 return -EINVAL;
6349
6350 len = sizeof(struct sctp_assoc_value);
6351
6352 if (copy_from_user(&params, optval, len))
6353 return -EFAULT;
6354
6355 sp = sctp_sk(sk);
6356
6357 if (params.assoc_id != 0) {
6358 asoc = sctp_id2assoc(sk, params.assoc_id);
6359 if (!asoc)
6360 return -EINVAL;
6361 params.assoc_value = asoc->default_rcv_context;
6362 } else {
6363 params.assoc_value = sp->default_rcv_context;
6364 }
6365
6366 if (put_user(len, optlen))
6367 return -EFAULT;
6368 if (copy_to_user(optval, &params, len))
6369 return -EFAULT;
6370
6371 return 0;
6372}
6373
6374/*
6375 * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6376 * This option will get or set the maximum size to put in any outgoing
6377 * SCTP DATA chunk. If a message is larger than this size it will be
6378 * fragmented by SCTP into the specified size. Note that the underlying
6379 * SCTP implementation may fragment into smaller sized chunks when the
6380 * PMTU of the underlying association is smaller than the value set by
6381 * the user. The default value for this option is '0' which indicates
6382 * the user is NOT limiting fragmentation and only the PMTU will effect
6383 * SCTP's choice of DATA chunk size. Note also that values set larger
6384 * than the maximum size of an IP datagram will effectively let SCTP
6385 * control fragmentation (i.e. the same as setting this option to 0).
6386 *
6387 * The following structure is used to access and modify this parameter:
6388 *
6389 * struct sctp_assoc_value {
6390 * sctp_assoc_t assoc_id;
6391 * uint32_t assoc_value;
6392 * };
6393 *
6394 * assoc_id: This parameter is ignored for one-to-one style sockets.
6395 * For one-to-many style sockets this parameter indicates which
6396 * association the user is performing an action upon. Note that if
6397 * this field's value is zero then the endpoints default value is
6398 * changed (effecting future associations only).
6399 * assoc_value: This parameter specifies the maximum size in bytes.
6400 */
6401static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6402 char __user *optval, int __user *optlen)
6403{
6404 struct sctp_assoc_value params;
6405 struct sctp_association *asoc;
6406
6407 if (len == sizeof(int)) {
6408 pr_warn_ratelimited(DEPRECATED
6409 "%s (pid %d) "
6410 "Use of int in maxseg socket option.\n"
6411 "Use struct sctp_assoc_value instead\n",
6412 current->comm, task_pid_nr(current));
6413 params.assoc_id = 0;
6414 } else if (len >= sizeof(struct sctp_assoc_value)) {
6415 len = sizeof(struct sctp_assoc_value);
6416 if (copy_from_user(&params, optval, len))
6417 return -EFAULT;
6418 } else
6419 return -EINVAL;
6420
6421 asoc = sctp_id2assoc(sk, params.assoc_id);
6422 if (!asoc && params.assoc_id && sctp_style(sk, UDP))
6423 return -EINVAL;
6424
6425 if (asoc)
6426 params.assoc_value = asoc->frag_point;
6427 else
6428 params.assoc_value = sctp_sk(sk)->user_frag;
6429
6430 if (put_user(len, optlen))
6431 return -EFAULT;
6432 if (len == sizeof(int)) {
6433 if (copy_to_user(optval, &params.assoc_value, len))
6434 return -EFAULT;
6435 } else {
6436 if (copy_to_user(optval, &params, len))
6437 return -EFAULT;
6438 }
6439
6440 return 0;
6441}
6442
6443/*
6444 * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6445 * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6446 */
6447static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6448 char __user *optval, int __user *optlen)
6449{
6450 int val;
6451
6452 if (len < sizeof(int))
6453 return -EINVAL;
6454
6455 len = sizeof(int);
6456
6457 val = sctp_sk(sk)->frag_interleave;
6458 if (put_user(len, optlen))
6459 return -EFAULT;
6460 if (copy_to_user(optval, &val, len))
6461 return -EFAULT;
6462
6463 return 0;
6464}
6465
6466/*
6467 * 7.1.25. Set or Get the sctp partial delivery point
6468 * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6469 */
6470static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6471 char __user *optval,
6472 int __user *optlen)
6473{
6474 u32 val;
6475
6476 if (len < sizeof(u32))
6477 return -EINVAL;
6478
6479 len = sizeof(u32);
6480
6481 val = sctp_sk(sk)->pd_point;
6482 if (put_user(len, optlen))
6483 return -EFAULT;
6484 if (copy_to_user(optval, &val, len))
6485 return -EFAULT;
6486
6487 return 0;
6488}
6489
6490/*
6491 * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6492 * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6493 */
6494static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6495 char __user *optval,
6496 int __user *optlen)
6497{
6498 struct sctp_assoc_value params;
6499 struct sctp_sock *sp;
6500 struct sctp_association *asoc;
6501
6502 if (len == sizeof(int)) {
6503 pr_warn_ratelimited(DEPRECATED
6504 "%s (pid %d) "
6505 "Use of int in max_burst socket option.\n"
6506 "Use struct sctp_assoc_value instead\n",
6507 current->comm, task_pid_nr(current));
6508 params.assoc_id = 0;
6509 } else if (len >= sizeof(struct sctp_assoc_value)) {
6510 len = sizeof(struct sctp_assoc_value);
6511 if (copy_from_user(&params, optval, len))
6512 return -EFAULT;
6513 } else
6514 return -EINVAL;
6515
6516 sp = sctp_sk(sk);
6517
6518 if (params.assoc_id != 0) {
6519 asoc = sctp_id2assoc(sk, params.assoc_id);
6520 if (!asoc)
6521 return -EINVAL;
6522 params.assoc_value = asoc->max_burst;
6523 } else
6524 params.assoc_value = sp->max_burst;
6525
6526 if (len == sizeof(int)) {
6527 if (copy_to_user(optval, &params.assoc_value, len))
6528 return -EFAULT;
6529 } else {
6530 if (copy_to_user(optval, &params, len))
6531 return -EFAULT;
6532 }
6533
6534 return 0;
6535
6536}
6537
6538static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6539 char __user *optval, int __user *optlen)
6540{
6541 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6542 struct sctp_hmacalgo __user *p = (void __user *)optval;
6543 struct sctp_hmac_algo_param *hmacs;
6544 __u16 data_len = 0;
6545 u32 num_idents;
6546 int i;
6547
6548 if (!ep->auth_enable)
6549 return -EACCES;
6550
6551 hmacs = ep->auth_hmacs_list;
6552 data_len = ntohs(hmacs->param_hdr.length) -
6553 sizeof(struct sctp_paramhdr);
6554
6555 if (len < sizeof(struct sctp_hmacalgo) + data_len)
6556 return -EINVAL;
6557
6558 len = sizeof(struct sctp_hmacalgo) + data_len;
6559 num_idents = data_len / sizeof(u16);
6560
6561 if (put_user(len, optlen))
6562 return -EFAULT;
6563 if (put_user(num_idents, &p->shmac_num_idents))
6564 return -EFAULT;
6565 for (i = 0; i < num_idents; i++) {
6566 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6567
6568 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6569 return -EFAULT;
6570 }
6571 return 0;
6572}
6573
6574static int sctp_getsockopt_active_key(struct sock *sk, int len,
6575 char __user *optval, int __user *optlen)
6576{
6577 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6578 struct sctp_authkeyid val;
6579 struct sctp_association *asoc;
6580
6581 if (!ep->auth_enable)
6582 return -EACCES;
6583
6584 if (len < sizeof(struct sctp_authkeyid))
6585 return -EINVAL;
6586
6587 len = sizeof(struct sctp_authkeyid);
6588 if (copy_from_user(&val, optval, len))
6589 return -EFAULT;
6590
6591 asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6592 if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6593 return -EINVAL;
6594
6595 if (asoc)
6596 val.scact_keynumber = asoc->active_key_id;
6597 else
6598 val.scact_keynumber = ep->active_key_id;
6599
6600 if (put_user(len, optlen))
6601 return -EFAULT;
6602 if (copy_to_user(optval, &val, len))
6603 return -EFAULT;
6604
6605 return 0;
6606}
6607
6608static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6609 char __user *optval, int __user *optlen)
6610{
6611 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6612 struct sctp_authchunks __user *p = (void __user *)optval;
6613 struct sctp_authchunks val;
6614 struct sctp_association *asoc;
6615 struct sctp_chunks_param *ch;
6616 u32 num_chunks = 0;
6617 char __user *to;
6618
6619 if (!ep->auth_enable)
6620 return -EACCES;
6621
6622 if (len < sizeof(struct sctp_authchunks))
6623 return -EINVAL;
6624
6625 if (copy_from_user(&val, optval, sizeof(val)))
6626 return -EFAULT;
6627
6628 to = p->gauth_chunks;
6629 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6630 if (!asoc)
6631 return -EINVAL;
6632
6633 ch = asoc->peer.peer_chunks;
6634 if (!ch)
6635 goto num;
6636
6637 /* See if the user provided enough room for all the data */
6638 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
6639 if (len < num_chunks)
6640 return -EINVAL;
6641
6642 if (copy_to_user(to, ch->chunks, num_chunks))
6643 return -EFAULT;
6644num:
6645 len = sizeof(struct sctp_authchunks) + num_chunks;
6646 if (put_user(len, optlen))
6647 return -EFAULT;
6648 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6649 return -EFAULT;
6650 return 0;
6651}
6652
6653static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6654 char __user *optval, int __user *optlen)
6655{
6656 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6657 struct sctp_authchunks __user *p = (void __user *)optval;
6658 struct sctp_authchunks val;
6659 struct sctp_association *asoc;
6660 struct sctp_chunks_param *ch;
6661 u32 num_chunks = 0;
6662 char __user *to;
6663
6664 if (!ep->auth_enable)
6665 return -EACCES;
6666
6667 if (len < sizeof(struct sctp_authchunks))
6668 return -EINVAL;
6669
6670 if (copy_from_user(&val, optval, sizeof(val)))
6671 return -EFAULT;
6672
6673 to = p->gauth_chunks;
6674 asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6675 if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
6676 return -EINVAL;
6677
6678 if (asoc)
6679 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
6680 else
6681 ch = ep->auth_chunk_list;
6682
6683 if (!ch)
6684 goto num;
6685
6686 num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
6687 if (len < sizeof(struct sctp_authchunks) + num_chunks)
6688 return -EINVAL;
6689
6690 if (copy_to_user(to, ch->chunks, num_chunks))
6691 return -EFAULT;
6692num:
6693 len = sizeof(struct sctp_authchunks) + num_chunks;
6694 if (put_user(len, optlen))
6695 return -EFAULT;
6696 if (put_user(num_chunks, &p->gauth_number_of_chunks))
6697 return -EFAULT;
6698
6699 return 0;
6700}
6701
6702/*
6703 * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6704 * This option gets the current number of associations that are attached
6705 * to a one-to-many style socket. The option value is an uint32_t.
6706 */
6707static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6708 char __user *optval, int __user *optlen)
6709{
6710 struct sctp_sock *sp = sctp_sk(sk);
6711 struct sctp_association *asoc;
6712 u32 val = 0;
6713
6714 if (sctp_style(sk, TCP))
6715 return -EOPNOTSUPP;
6716
6717 if (len < sizeof(u32))
6718 return -EINVAL;
6719
6720 len = sizeof(u32);
6721
6722 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6723 val++;
6724 }
6725
6726 if (put_user(len, optlen))
6727 return -EFAULT;
6728 if (copy_to_user(optval, &val, len))
6729 return -EFAULT;
6730
6731 return 0;
6732}
6733
6734/*
6735 * 8.1.23 SCTP_AUTO_ASCONF
6736 * See the corresponding setsockopt entry as description
6737 */
6738static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6739 char __user *optval, int __user *optlen)
6740{
6741 int val = 0;
6742
6743 if (len < sizeof(int))
6744 return -EINVAL;
6745
6746 len = sizeof(int);
6747 if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6748 val = 1;
6749 if (put_user(len, optlen))
6750 return -EFAULT;
6751 if (copy_to_user(optval, &val, len))
6752 return -EFAULT;
6753 return 0;
6754}
6755
6756/*
6757 * 8.2.6. Get the Current Identifiers of Associations
6758 * (SCTP_GET_ASSOC_ID_LIST)
6759 *
6760 * This option gets the current list of SCTP association identifiers of
6761 * the SCTP associations handled by a one-to-many style socket.
6762 */
6763static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6764 char __user *optval, int __user *optlen)
6765{
6766 struct sctp_sock *sp = sctp_sk(sk);
6767 struct sctp_association *asoc;
6768 struct sctp_assoc_ids *ids;
6769 u32 num = 0;
6770
6771 if (sctp_style(sk, TCP))
6772 return -EOPNOTSUPP;
6773
6774 if (len < sizeof(struct sctp_assoc_ids))
6775 return -EINVAL;
6776
6777 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6778 num++;
6779 }
6780
6781 if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
6782 return -EINVAL;
6783
6784 len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
6785
6786 ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
6787 if (unlikely(!ids))
6788 return -ENOMEM;
6789
6790 ids->gaids_number_of_ids = num;
6791 num = 0;
6792 list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6793 ids->gaids_assoc_id[num++] = asoc->assoc_id;
6794 }
6795
6796 if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
6797 kfree(ids);
6798 return -EFAULT;
6799 }
6800
6801 kfree(ids);
6802 return 0;
6803}
6804
6805/*
6806 * SCTP_PEER_ADDR_THLDS
6807 *
6808 * This option allows us to fetch the partially failed threshold for one or all
6809 * transports in an association. See Section 6.1 of:
6810 * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
6811 */
6812static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
6813 char __user *optval,
6814 int len,
6815 int __user *optlen)
6816{
6817 struct sctp_paddrthlds val;
6818 struct sctp_transport *trans;
6819 struct sctp_association *asoc;
6820
6821 if (len < sizeof(struct sctp_paddrthlds))
6822 return -EINVAL;
6823 len = sizeof(struct sctp_paddrthlds);
6824 if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
6825 return -EFAULT;
6826
6827 if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
6828 asoc = sctp_id2assoc(sk, val.spt_assoc_id);
6829 if (!asoc)
6830 return -ENOENT;
6831
6832 val.spt_pathpfthld = asoc->pf_retrans;
6833 val.spt_pathmaxrxt = asoc->pathmaxrxt;
6834 } else {
6835 trans = sctp_addr_id2transport(sk, &val.spt_address,
6836 val.spt_assoc_id);
6837 if (!trans)
6838 return -ENOENT;
6839
6840 val.spt_pathmaxrxt = trans->pathmaxrxt;
6841 val.spt_pathpfthld = trans->pf_retrans;
6842 }
6843
6844 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6845 return -EFAULT;
6846
6847 return 0;
6848}
6849
6850/*
6851 * SCTP_GET_ASSOC_STATS
6852 *
6853 * This option retrieves local per endpoint statistics. It is modeled
6854 * after OpenSolaris' implementation
6855 */
6856static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
6857 char __user *optval,
6858 int __user *optlen)
6859{
6860 struct sctp_assoc_stats sas;
6861 struct sctp_association *asoc = NULL;
6862
6863 /* User must provide at least the assoc id */
6864 if (len < sizeof(sctp_assoc_t))
6865 return -EINVAL;
6866
6867 /* Allow the struct to grow and fill in as much as possible */
6868 len = min_t(size_t, len, sizeof(sas));
6869
6870 if (copy_from_user(&sas, optval, len))
6871 return -EFAULT;
6872
6873 asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
6874 if (!asoc)
6875 return -EINVAL;
6876
6877 sas.sas_rtxchunks = asoc->stats.rtxchunks;
6878 sas.sas_gapcnt = asoc->stats.gapcnt;
6879 sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
6880 sas.sas_osacks = asoc->stats.osacks;
6881 sas.sas_isacks = asoc->stats.isacks;
6882 sas.sas_octrlchunks = asoc->stats.octrlchunks;
6883 sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
6884 sas.sas_oodchunks = asoc->stats.oodchunks;
6885 sas.sas_iodchunks = asoc->stats.iodchunks;
6886 sas.sas_ouodchunks = asoc->stats.ouodchunks;
6887 sas.sas_iuodchunks = asoc->stats.iuodchunks;
6888 sas.sas_idupchunks = asoc->stats.idupchunks;
6889 sas.sas_opackets = asoc->stats.opackets;
6890 sas.sas_ipackets = asoc->stats.ipackets;
6891
6892 /* New high max rto observed, will return 0 if not a single
6893 * RTO update took place. obs_rto_ipaddr will be bogus
6894 * in such a case
6895 */
6896 sas.sas_maxrto = asoc->stats.max_obs_rto;
6897 memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
6898 sizeof(struct sockaddr_storage));
6899
6900 /* Mark beginning of a new observation period */
6901 asoc->stats.max_obs_rto = asoc->rto_min;
6902
6903 if (put_user(len, optlen))
6904 return -EFAULT;
6905
6906 pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
6907
6908 if (copy_to_user(optval, &sas, len))
6909 return -EFAULT;
6910
6911 return 0;
6912}
6913
6914static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
6915 char __user *optval,
6916 int __user *optlen)
6917{
6918 int val = 0;
6919
6920 if (len < sizeof(int))
6921 return -EINVAL;
6922
6923 len = sizeof(int);
6924 if (sctp_sk(sk)->recvrcvinfo)
6925 val = 1;
6926 if (put_user(len, optlen))
6927 return -EFAULT;
6928 if (copy_to_user(optval, &val, len))
6929 return -EFAULT;
6930
6931 return 0;
6932}
6933
6934static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
6935 char __user *optval,
6936 int __user *optlen)
6937{
6938 int val = 0;
6939
6940 if (len < sizeof(int))
6941 return -EINVAL;
6942
6943 len = sizeof(int);
6944 if (sctp_sk(sk)->recvnxtinfo)
6945 val = 1;
6946 if (put_user(len, optlen))
6947 return -EFAULT;
6948 if (copy_to_user(optval, &val, len))
6949 return -EFAULT;
6950
6951 return 0;
6952}
6953
6954static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
6955 char __user *optval,
6956 int __user *optlen)
6957{
6958 struct sctp_assoc_value params;
6959 struct sctp_association *asoc;
6960 int retval = -EFAULT;
6961
6962 if (len < sizeof(params)) {
6963 retval = -EINVAL;
6964 goto out;
6965 }
6966
6967 len = sizeof(params);
6968 if (copy_from_user(&params, optval, len))
6969 goto out;
6970
6971 asoc = sctp_id2assoc(sk, params.assoc_id);
6972 if (asoc) {
6973 params.assoc_value = asoc->prsctp_enable;
6974 } else if (!params.assoc_id) {
6975 struct sctp_sock *sp = sctp_sk(sk);
6976
6977 params.assoc_value = sp->ep->prsctp_enable;
6978 } else {
6979 retval = -EINVAL;
6980 goto out;
6981 }
6982
6983 if (put_user(len, optlen))
6984 goto out;
6985
6986 if (copy_to_user(optval, &params, len))
6987 goto out;
6988
6989 retval = 0;
6990
6991out:
6992 return retval;
6993}
6994
6995static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
6996 char __user *optval,
6997 int __user *optlen)
6998{
6999 struct sctp_default_prinfo info;
7000 struct sctp_association *asoc;
7001 int retval = -EFAULT;
7002
7003 if (len < sizeof(info)) {
7004 retval = -EINVAL;
7005 goto out;
7006 }
7007
7008 len = sizeof(info);
7009 if (copy_from_user(&info, optval, len))
7010 goto out;
7011
7012 asoc = sctp_id2assoc(sk, info.pr_assoc_id);
7013 if (asoc) {
7014 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
7015 info.pr_value = asoc->default_timetolive;
7016 } else if (!info.pr_assoc_id) {
7017 struct sctp_sock *sp = sctp_sk(sk);
7018
7019 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
7020 info.pr_value = sp->default_timetolive;
7021 } else {
7022 retval = -EINVAL;
7023 goto out;
7024 }
7025
7026 if (put_user(len, optlen))
7027 goto out;
7028
7029 if (copy_to_user(optval, &info, len))
7030 goto out;
7031
7032 retval = 0;
7033
7034out:
7035 return retval;
7036}
7037
7038static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
7039 char __user *optval,
7040 int __user *optlen)
7041{
7042 struct sctp_prstatus params;
7043 struct sctp_association *asoc;
7044 int policy;
7045 int retval = -EINVAL;
7046
7047 if (len < sizeof(params))
7048 goto out;
7049
7050 len = sizeof(params);
7051 if (copy_from_user(&params, optval, len)) {
7052 retval = -EFAULT;
7053 goto out;
7054 }
7055
7056 policy = params.sprstat_policy;
7057 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7058 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7059 goto out;
7060
7061 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7062 if (!asoc)
7063 goto out;
7064
7065 if (policy == SCTP_PR_SCTP_ALL) {
7066 params.sprstat_abandoned_unsent = 0;
7067 params.sprstat_abandoned_sent = 0;
7068 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7069 params.sprstat_abandoned_unsent +=
7070 asoc->abandoned_unsent[policy];
7071 params.sprstat_abandoned_sent +=
7072 asoc->abandoned_sent[policy];
7073 }
7074 } else {
7075 params.sprstat_abandoned_unsent =
7076 asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7077 params.sprstat_abandoned_sent =
7078 asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
7079 }
7080
7081 if (put_user(len, optlen)) {
7082 retval = -EFAULT;
7083 goto out;
7084 }
7085
7086 if (copy_to_user(optval, &params, len)) {
7087 retval = -EFAULT;
7088 goto out;
7089 }
7090
7091 retval = 0;
7092
7093out:
7094 return retval;
7095}
7096
7097static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
7098 char __user *optval,
7099 int __user *optlen)
7100{
7101 struct sctp_stream_out_ext *streamoute;
7102 struct sctp_association *asoc;
7103 struct sctp_prstatus params;
7104 int retval = -EINVAL;
7105 int policy;
7106
7107 if (len < sizeof(params))
7108 goto out;
7109
7110 len = sizeof(params);
7111 if (copy_from_user(&params, optval, len)) {
7112 retval = -EFAULT;
7113 goto out;
7114 }
7115
7116 policy = params.sprstat_policy;
7117 if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7118 ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7119 goto out;
7120
7121 asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7122 if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
7123 goto out;
7124
7125 streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
7126 if (!streamoute) {
7127 /* Not allocated yet, means all stats are 0 */
7128 params.sprstat_abandoned_unsent = 0;
7129 params.sprstat_abandoned_sent = 0;
7130 retval = 0;
7131 goto out;
7132 }
7133
7134 if (policy == SCTP_PR_SCTP_ALL) {
7135 params.sprstat_abandoned_unsent = 0;
7136 params.sprstat_abandoned_sent = 0;
7137 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7138 params.sprstat_abandoned_unsent +=
7139 streamoute->abandoned_unsent[policy];
7140 params.sprstat_abandoned_sent +=
7141 streamoute->abandoned_sent[policy];
7142 }
7143 } else {
7144 params.sprstat_abandoned_unsent =
7145 streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7146 params.sprstat_abandoned_sent =
7147 streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
7148 }
7149
7150 if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
7151 retval = -EFAULT;
7152 goto out;
7153 }
7154
7155 retval = 0;
7156
7157out:
7158 return retval;
7159}
7160
7161static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
7162 char __user *optval,
7163 int __user *optlen)
7164{
7165 struct sctp_assoc_value params;
7166 struct sctp_association *asoc;
7167 int retval = -EFAULT;
7168
7169 if (len < sizeof(params)) {
7170 retval = -EINVAL;
7171 goto out;
7172 }
7173
7174 len = sizeof(params);
7175 if (copy_from_user(&params, optval, len))
7176 goto out;
7177
7178 asoc = sctp_id2assoc(sk, params.assoc_id);
7179 if (asoc) {
7180 params.assoc_value = asoc->reconf_enable;
7181 } else if (!params.assoc_id) {
7182 struct sctp_sock *sp = sctp_sk(sk);
7183
7184 params.assoc_value = sp->ep->reconf_enable;
7185 } else {
7186 retval = -EINVAL;
7187 goto out;
7188 }
7189
7190 if (put_user(len, optlen))
7191 goto out;
7192
7193 if (copy_to_user(optval, &params, len))
7194 goto out;
7195
7196 retval = 0;
7197
7198out:
7199 return retval;
7200}
7201
7202static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7203 char __user *optval,
7204 int __user *optlen)
7205{
7206 struct sctp_assoc_value params;
7207 struct sctp_association *asoc;
7208 int retval = -EFAULT;
7209
7210 if (len < sizeof(params)) {
7211 retval = -EINVAL;
7212 goto out;
7213 }
7214
7215 len = sizeof(params);
7216 if (copy_from_user(&params, optval, len))
7217 goto out;
7218
7219 asoc = sctp_id2assoc(sk, params.assoc_id);
7220 if (asoc) {
7221 params.assoc_value = asoc->strreset_enable;
7222 } else if (!params.assoc_id) {
7223 struct sctp_sock *sp = sctp_sk(sk);
7224
7225 params.assoc_value = sp->ep->strreset_enable;
7226 } else {
7227 retval = -EINVAL;
7228 goto out;
7229 }
7230
7231 if (put_user(len, optlen))
7232 goto out;
7233
7234 if (copy_to_user(optval, &params, len))
7235 goto out;
7236
7237 retval = 0;
7238
7239out:
7240 return retval;
7241}
7242
7243static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7244 char __user *optval,
7245 int __user *optlen)
7246{
7247 struct sctp_assoc_value params;
7248 struct sctp_association *asoc;
7249 int retval = -EFAULT;
7250
7251 if (len < sizeof(params)) {
7252 retval = -EINVAL;
7253 goto out;
7254 }
7255
7256 len = sizeof(params);
7257 if (copy_from_user(&params, optval, len))
7258 goto out;
7259
7260 asoc = sctp_id2assoc(sk, params.assoc_id);
7261 if (!asoc) {
7262 retval = -EINVAL;
7263 goto out;
7264 }
7265
7266 params.assoc_value = sctp_sched_get_sched(asoc);
7267
7268 if (put_user(len, optlen))
7269 goto out;
7270
7271 if (copy_to_user(optval, &params, len))
7272 goto out;
7273
7274 retval = 0;
7275
7276out:
7277 return retval;
7278}
7279
7280static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7281 char __user *optval,
7282 int __user *optlen)
7283{
7284 struct sctp_stream_value params;
7285 struct sctp_association *asoc;
7286 int retval = -EFAULT;
7287
7288 if (len < sizeof(params)) {
7289 retval = -EINVAL;
7290 goto out;
7291 }
7292
7293 len = sizeof(params);
7294 if (copy_from_user(&params, optval, len))
7295 goto out;
7296
7297 asoc = sctp_id2assoc(sk, params.assoc_id);
7298 if (!asoc) {
7299 retval = -EINVAL;
7300 goto out;
7301 }
7302
7303 retval = sctp_sched_get_value(asoc, params.stream_id,
7304 &params.stream_value);
7305 if (retval)
7306 goto out;
7307
7308 if (put_user(len, optlen)) {
7309 retval = -EFAULT;
7310 goto out;
7311 }
7312
7313 if (copy_to_user(optval, &params, len)) {
7314 retval = -EFAULT;
7315 goto out;
7316 }
7317
7318out:
7319 return retval;
7320}
7321
7322static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7323 char __user *optval,
7324 int __user *optlen)
7325{
7326 struct sctp_assoc_value params;
7327 struct sctp_association *asoc;
7328 int retval = -EFAULT;
7329
7330 if (len < sizeof(params)) {
7331 retval = -EINVAL;
7332 goto out;
7333 }
7334
7335 len = sizeof(params);
7336 if (copy_from_user(&params, optval, len))
7337 goto out;
7338
7339 asoc = sctp_id2assoc(sk, params.assoc_id);
7340 if (asoc) {
7341 params.assoc_value = asoc->intl_enable;
7342 } else if (!params.assoc_id) {
7343 struct sctp_sock *sp = sctp_sk(sk);
7344
7345 params.assoc_value = sp->strm_interleave;
7346 } else {
7347 retval = -EINVAL;
7348 goto out;
7349 }
7350
7351 if (put_user(len, optlen))
7352 goto out;
7353
7354 if (copy_to_user(optval, &params, len))
7355 goto out;
7356
7357 retval = 0;
7358
7359out:
7360 return retval;
7361}
7362
7363static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
7364 char __user *optval,
7365 int __user *optlen)
7366{
7367 int val;
7368
7369 if (len < sizeof(int))
7370 return -EINVAL;
7371
7372 len = sizeof(int);
7373 val = sctp_sk(sk)->reuse;
7374 if (put_user(len, optlen))
7375 return -EFAULT;
7376
7377 if (copy_to_user(optval, &val, len))
7378 return -EFAULT;
7379
7380 return 0;
7381}
7382
7383static int sctp_getsockopt(struct sock *sk, int level, int optname,
7384 char __user *optval, int __user *optlen)
7385{
7386 int retval = 0;
7387 int len;
7388
7389 pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
7390
7391 /* I can hardly begin to describe how wrong this is. This is
7392 * so broken as to be worse than useless. The API draft
7393 * REALLY is NOT helpful here... I am not convinced that the
7394 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7395 * are at all well-founded.
7396 */
7397 if (level != SOL_SCTP) {
7398 struct sctp_af *af = sctp_sk(sk)->pf->af;
7399
7400 retval = af->getsockopt(sk, level, optname, optval, optlen);
7401 return retval;
7402 }
7403
7404 if (get_user(len, optlen))
7405 return -EFAULT;
7406
7407 if (len < 0)
7408 return -EINVAL;
7409
7410 lock_sock(sk);
7411
7412 switch (optname) {
7413 case SCTP_STATUS:
7414 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7415 break;
7416 case SCTP_DISABLE_FRAGMENTS:
7417 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7418 optlen);
7419 break;
7420 case SCTP_EVENTS:
7421 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7422 break;
7423 case SCTP_AUTOCLOSE:
7424 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7425 break;
7426 case SCTP_SOCKOPT_PEELOFF:
7427 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7428 break;
7429 case SCTP_SOCKOPT_PEELOFF_FLAGS:
7430 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7431 break;
7432 case SCTP_PEER_ADDR_PARAMS:
7433 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7434 optlen);
7435 break;
7436 case SCTP_DELAYED_SACK:
7437 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
7438 optlen);
7439 break;
7440 case SCTP_INITMSG:
7441 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7442 break;
7443 case SCTP_GET_PEER_ADDRS:
7444 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7445 optlen);
7446 break;
7447 case SCTP_GET_LOCAL_ADDRS:
7448 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7449 optlen);
7450 break;
7451 case SCTP_SOCKOPT_CONNECTX3:
7452 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7453 break;
7454 case SCTP_DEFAULT_SEND_PARAM:
7455 retval = sctp_getsockopt_default_send_param(sk, len,
7456 optval, optlen);
7457 break;
7458 case SCTP_DEFAULT_SNDINFO:
7459 retval = sctp_getsockopt_default_sndinfo(sk, len,
7460 optval, optlen);
7461 break;
7462 case SCTP_PRIMARY_ADDR:
7463 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7464 break;
7465 case SCTP_NODELAY:
7466 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7467 break;
7468 case SCTP_RTOINFO:
7469 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7470 break;
7471 case SCTP_ASSOCINFO:
7472 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7473 break;
7474 case SCTP_I_WANT_MAPPED_V4_ADDR:
7475 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7476 break;
7477 case SCTP_MAXSEG:
7478 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7479 break;
7480 case SCTP_GET_PEER_ADDR_INFO:
7481 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7482 optlen);
7483 break;
7484 case SCTP_ADAPTATION_LAYER:
7485 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
7486 optlen);
7487 break;
7488 case SCTP_CONTEXT:
7489 retval = sctp_getsockopt_context(sk, len, optval, optlen);
7490 break;
7491 case SCTP_FRAGMENT_INTERLEAVE:
7492 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7493 optlen);
7494 break;
7495 case SCTP_PARTIAL_DELIVERY_POINT:
7496 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7497 optlen);
7498 break;
7499 case SCTP_MAX_BURST:
7500 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7501 break;
7502 case SCTP_AUTH_KEY:
7503 case SCTP_AUTH_CHUNK:
7504 case SCTP_AUTH_DELETE_KEY:
7505 case SCTP_AUTH_DEACTIVATE_KEY:
7506 retval = -EOPNOTSUPP;
7507 break;
7508 case SCTP_HMAC_IDENT:
7509 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7510 break;
7511 case SCTP_AUTH_ACTIVE_KEY:
7512 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7513 break;
7514 case SCTP_PEER_AUTH_CHUNKS:
7515 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7516 optlen);
7517 break;
7518 case SCTP_LOCAL_AUTH_CHUNKS:
7519 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7520 optlen);
7521 break;
7522 case SCTP_GET_ASSOC_NUMBER:
7523 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7524 break;
7525 case SCTP_GET_ASSOC_ID_LIST:
7526 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7527 break;
7528 case SCTP_AUTO_ASCONF:
7529 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7530 break;
7531 case SCTP_PEER_ADDR_THLDS:
7532 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
7533 break;
7534 case SCTP_GET_ASSOC_STATS:
7535 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7536 break;
7537 case SCTP_RECVRCVINFO:
7538 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7539 break;
7540 case SCTP_RECVNXTINFO:
7541 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7542 break;
7543 case SCTP_PR_SUPPORTED:
7544 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7545 break;
7546 case SCTP_DEFAULT_PRINFO:
7547 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7548 optlen);
7549 break;
7550 case SCTP_PR_ASSOC_STATUS:
7551 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7552 optlen);
7553 break;
7554 case SCTP_PR_STREAM_STATUS:
7555 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7556 optlen);
7557 break;
7558 case SCTP_RECONFIG_SUPPORTED:
7559 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7560 optlen);
7561 break;
7562 case SCTP_ENABLE_STREAM_RESET:
7563 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7564 optlen);
7565 break;
7566 case SCTP_STREAM_SCHEDULER:
7567 retval = sctp_getsockopt_scheduler(sk, len, optval,
7568 optlen);
7569 break;
7570 case SCTP_STREAM_SCHEDULER_VALUE:
7571 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
7572 optlen);
7573 break;
7574 case SCTP_INTERLEAVING_SUPPORTED:
7575 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
7576 optlen);
7577 break;
7578 case SCTP_REUSE_PORT:
7579 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
7580 break;
7581 default:
7582 retval = -ENOPROTOOPT;
7583 break;
7584 }
7585
7586 release_sock(sk);
7587 return retval;
7588}
7589
7590static int sctp_hash(struct sock *sk)
7591{
7592 /* STUB */
7593 return 0;
7594}
7595
7596static void sctp_unhash(struct sock *sk)
7597{
7598 /* STUB */
7599}
7600
7601/* Check if port is acceptable. Possibly find first available port.
7602 *
7603 * The port hash table (contained in the 'global' SCTP protocol storage
7604 * returned by struct sctp_protocol *sctp_get_protocol()). The hash
7605 * table is an array of 4096 lists (sctp_bind_hashbucket). Each
7606 * list (the list number is the port number hashed out, so as you
7607 * would expect from a hash function, all the ports in a given list have
7608 * such a number that hashes out to the same list number; you were
7609 * expecting that, right?); so each list has a set of ports, with a
7610 * link to the socket (struct sock) that uses it, the port number and
7611 * a fastreuse flag (FIXME: NPI ipg).
7612 */
7613static struct sctp_bind_bucket *sctp_bucket_create(
7614 struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
7615
7616static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
7617{
7618 bool reuse = (sk->sk_reuse || sctp_sk(sk)->reuse);
7619 struct sctp_bind_hashbucket *head; /* hash list */
7620 struct sctp_bind_bucket *pp;
7621 unsigned short snum;
7622 int ret;
7623
7624 snum = ntohs(addr->v4.sin_port);
7625
7626 pr_debug("%s: begins, snum:%d\n", __func__, snum);
7627
7628 local_bh_disable();
7629
7630 if (snum == 0) {
7631 /* Search for an available port. */
7632 int low, high, remaining, index;
7633 unsigned int rover;
7634 struct net *net = sock_net(sk);
7635
7636 inet_get_local_port_range(net, &low, &high);
7637 remaining = (high - low) + 1;
7638 rover = prandom_u32() % remaining + low;
7639
7640 do {
7641 rover++;
7642 if ((rover < low) || (rover > high))
7643 rover = low;
7644 if (inet_is_local_reserved_port(net, rover))
7645 continue;
7646 index = sctp_phashfn(sock_net(sk), rover);
7647 head = &sctp_port_hashtable[index];
7648 spin_lock(&head->lock);
7649 sctp_for_each_hentry(pp, &head->chain)
7650 if ((pp->port == rover) &&
7651 net_eq(sock_net(sk), pp->net))
7652 goto next;
7653 break;
7654 next:
7655 spin_unlock(&head->lock);
7656 } while (--remaining > 0);
7657
7658 /* Exhausted local port range during search? */
7659 ret = 1;
7660 if (remaining <= 0)
7661 goto fail;
7662
7663 /* OK, here is the one we will use. HEAD (the port
7664 * hash table list entry) is non-NULL and we hold it's
7665 * mutex.
7666 */
7667 snum = rover;
7668 } else {
7669 /* We are given an specific port number; we verify
7670 * that it is not being used. If it is used, we will
7671 * exahust the search in the hash list corresponding
7672 * to the port number (snum) - we detect that with the
7673 * port iterator, pp being NULL.
7674 */
7675 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
7676 spin_lock(&head->lock);
7677 sctp_for_each_hentry(pp, &head->chain) {
7678 if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
7679 goto pp_found;
7680 }
7681 }
7682 pp = NULL;
7683 goto pp_not_found;
7684pp_found:
7685 if (!hlist_empty(&pp->owner)) {
7686 /* We had a port hash table hit - there is an
7687 * available port (pp != NULL) and it is being
7688 * used by other socket (pp->owner not empty); that other
7689 * socket is going to be sk2.
7690 */
7691 struct sock *sk2;
7692
7693 pr_debug("%s: found a possible match\n", __func__);
7694
7695 if (pp->fastreuse && reuse && sk->sk_state != SCTP_SS_LISTENING)
7696 goto success;
7697
7698 /* Run through the list of sockets bound to the port
7699 * (pp->port) [via the pointers bind_next and
7700 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
7701 * we get the endpoint they describe and run through
7702 * the endpoint's list of IP (v4 or v6) addresses,
7703 * comparing each of the addresses with the address of
7704 * the socket sk. If we find a match, then that means
7705 * that this port/socket (sk) combination are already
7706 * in an endpoint.
7707 */
7708 sk_for_each_bound(sk2, &pp->owner) {
7709 struct sctp_endpoint *ep2;
7710 ep2 = sctp_sk(sk2)->ep;
7711
7712 if (sk == sk2 ||
7713 (reuse && (sk2->sk_reuse || sctp_sk(sk2)->reuse) &&
7714 sk2->sk_state != SCTP_SS_LISTENING))
7715 continue;
7716
7717 if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
7718 sctp_sk(sk2), sctp_sk(sk))) {
7719 ret = (long)sk2;
7720 goto fail_unlock;
7721 }
7722 }
7723
7724 pr_debug("%s: found a match\n", __func__);
7725 }
7726pp_not_found:
7727 /* If there was a hash table miss, create a new port. */
7728 ret = 1;
7729 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
7730 goto fail_unlock;
7731
7732 /* In either case (hit or miss), make sure fastreuse is 1 only
7733 * if sk->sk_reuse is too (that is, if the caller requested
7734 * SO_REUSEADDR on this socket -sk-).
7735 */
7736 if (hlist_empty(&pp->owner)) {
7737 if (reuse && sk->sk_state != SCTP_SS_LISTENING)
7738 pp->fastreuse = 1;
7739 else
7740 pp->fastreuse = 0;
7741 } else if (pp->fastreuse &&
7742 (!reuse || sk->sk_state == SCTP_SS_LISTENING))
7743 pp->fastreuse = 0;
7744
7745 /* We are set, so fill up all the data in the hash table
7746 * entry, tie the socket list information with the rest of the
7747 * sockets FIXME: Blurry, NPI (ipg).
7748 */
7749success:
7750 if (!sctp_sk(sk)->bind_hash) {
7751 inet_sk(sk)->inet_num = snum;
7752 sk_add_bind_node(sk, &pp->owner);
7753 sctp_sk(sk)->bind_hash = pp;
7754 }
7755 ret = 0;
7756
7757fail_unlock:
7758 spin_unlock(&head->lock);
7759
7760fail:
7761 local_bh_enable();
7762 return ret;
7763}
7764
7765/* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
7766 * port is requested.
7767 */
7768static int sctp_get_port(struct sock *sk, unsigned short snum)
7769{
7770 union sctp_addr addr;
7771 struct sctp_af *af = sctp_sk(sk)->pf->af;
7772
7773 /* Set up a dummy address struct from the sk. */
7774 af->from_sk(&addr, sk);
7775 addr.v4.sin_port = htons(snum);
7776
7777 /* Note: sk->sk_num gets filled in if ephemeral port request. */
7778 return !!sctp_get_port_local(sk, &addr);
7779}
7780
7781/*
7782 * Move a socket to LISTENING state.
7783 */
7784static int sctp_listen_start(struct sock *sk, int backlog)
7785{
7786 struct sctp_sock *sp = sctp_sk(sk);
7787 struct sctp_endpoint *ep = sp->ep;
7788 struct crypto_shash *tfm = NULL;
7789 char alg[32];
7790
7791 /* Allocate HMAC for generating cookie. */
7792 if (!sp->hmac && sp->sctp_hmac_alg) {
7793 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
7794 tfm = crypto_alloc_shash(alg, 0, 0);
7795 if (IS_ERR(tfm)) {
7796 net_info_ratelimited("failed to load transform for %s: %ld\n",
7797 sp->sctp_hmac_alg, PTR_ERR(tfm));
7798 return -ENOSYS;
7799 }
7800 sctp_sk(sk)->hmac = tfm;
7801 }
7802
7803 /*
7804 * If a bind() or sctp_bindx() is not called prior to a listen()
7805 * call that allows new associations to be accepted, the system
7806 * picks an ephemeral port and will choose an address set equivalent
7807 * to binding with a wildcard address.
7808 *
7809 * This is not currently spelled out in the SCTP sockets
7810 * extensions draft, but follows the practice as seen in TCP
7811 * sockets.
7812 *
7813 */
7814 inet_sk_set_state(sk, SCTP_SS_LISTENING);
7815 if (!ep->base.bind_addr.port) {
7816 if (sctp_autobind(sk))
7817 return -EAGAIN;
7818 } else {
7819 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
7820 inet_sk_set_state(sk, SCTP_SS_CLOSED);
7821 return -EADDRINUSE;
7822 }
7823 }
7824
7825 sk->sk_max_ack_backlog = backlog;
7826 sctp_hash_endpoint(ep);
7827 return 0;
7828}
7829
7830/*
7831 * 4.1.3 / 5.1.3 listen()
7832 *
7833 * By default, new associations are not accepted for UDP style sockets.
7834 * An application uses listen() to mark a socket as being able to
7835 * accept new associations.
7836 *
7837 * On TCP style sockets, applications use listen() to ready the SCTP
7838 * endpoint for accepting inbound associations.
7839 *
7840 * On both types of endpoints a backlog of '0' disables listening.
7841 *
7842 * Move a socket to LISTENING state.
7843 */
7844int sctp_inet_listen(struct socket *sock, int backlog)
7845{
7846 struct sock *sk = sock->sk;
7847 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7848 int err = -EINVAL;
7849
7850 if (unlikely(backlog < 0))
7851 return err;
7852
7853 lock_sock(sk);
7854
7855 /* Peeled-off sockets are not allowed to listen(). */
7856 if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
7857 goto out;
7858
7859 if (sock->state != SS_UNCONNECTED)
7860 goto out;
7861
7862 if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
7863 goto out;
7864
7865 /* If backlog is zero, disable listening. */
7866 if (!backlog) {
7867 if (sctp_sstate(sk, CLOSED))
7868 goto out;
7869
7870 err = 0;
7871 sctp_unhash_endpoint(ep);
7872 sk->sk_state = SCTP_SS_CLOSED;
7873 if (sk->sk_reuse || sctp_sk(sk)->reuse)
7874 sctp_sk(sk)->bind_hash->fastreuse = 1;
7875 goto out;
7876 }
7877
7878 /* If we are already listening, just update the backlog */
7879 if (sctp_sstate(sk, LISTENING))
7880 sk->sk_max_ack_backlog = backlog;
7881 else {
7882 err = sctp_listen_start(sk, backlog);
7883 if (err)
7884 goto out;
7885 }
7886
7887 err = 0;
7888out:
7889 release_sock(sk);
7890 return err;
7891}
7892
7893/*
7894 * This function is done by modeling the current datagram_poll() and the
7895 * tcp_poll(). Note that, based on these implementations, we don't
7896 * lock the socket in this function, even though it seems that,
7897 * ideally, locking or some other mechanisms can be used to ensure
7898 * the integrity of the counters (sndbuf and wmem_alloc) used
7899 * in this place. We assume that we don't need locks either until proven
7900 * otherwise.
7901 *
7902 * Another thing to note is that we include the Async I/O support
7903 * here, again, by modeling the current TCP/UDP code. We don't have
7904 * a good way to test with it yet.
7905 */
7906__poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
7907{
7908 struct sock *sk = sock->sk;
7909 struct sctp_sock *sp = sctp_sk(sk);
7910 __poll_t mask;
7911
7912 poll_wait(file, sk_sleep(sk), wait);
7913
7914 sock_rps_record_flow(sk);
7915
7916 /* A TCP-style listening socket becomes readable when the accept queue
7917 * is not empty.
7918 */
7919 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
7920 return (!list_empty(&sp->ep->asocs)) ?
7921 (EPOLLIN | EPOLLRDNORM) : 0;
7922
7923 mask = 0;
7924
7925 /* Is there any exceptional events? */
7926 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
7927 mask |= EPOLLERR |
7928 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
7929 if (sk->sk_shutdown & RCV_SHUTDOWN)
7930 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
7931 if (sk->sk_shutdown == SHUTDOWN_MASK)
7932 mask |= EPOLLHUP;
7933
7934 /* Is it readable? Reconsider this code with TCP-style support. */
7935 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
7936 mask |= EPOLLIN | EPOLLRDNORM;
7937
7938 /* The association is either gone or not ready. */
7939 if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
7940 return mask;
7941
7942 /* Is it writable? */
7943 if (sctp_writeable(sk)) {
7944 mask |= EPOLLOUT | EPOLLWRNORM;
7945 } else {
7946 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
7947 /*
7948 * Since the socket is not locked, the buffer
7949 * might be made available after the writeable check and
7950 * before the bit is set. This could cause a lost I/O
7951 * signal. tcp_poll() has a race breaker for this race
7952 * condition. Based on their implementation, we put
7953 * in the following code to cover it as well.
7954 */
7955 if (sctp_writeable(sk))
7956 mask |= EPOLLOUT | EPOLLWRNORM;
7957 }
7958 return mask;
7959}
7960
7961/********************************************************************
7962 * 2nd Level Abstractions
7963 ********************************************************************/
7964
7965static struct sctp_bind_bucket *sctp_bucket_create(
7966 struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
7967{
7968 struct sctp_bind_bucket *pp;
7969
7970 pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
7971 if (pp) {
7972 SCTP_DBG_OBJCNT_INC(bind_bucket);
7973 pp->port = snum;
7974 pp->fastreuse = 0;
7975 INIT_HLIST_HEAD(&pp->owner);
7976 pp->net = net;
7977 hlist_add_head(&pp->node, &head->chain);
7978 }
7979 return pp;
7980}
7981
7982/* Caller must hold hashbucket lock for this tb with local BH disabled */
7983static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
7984{
7985 if (pp && hlist_empty(&pp->owner)) {
7986 __hlist_del(&pp->node);
7987 kmem_cache_free(sctp_bucket_cachep, pp);
7988 SCTP_DBG_OBJCNT_DEC(bind_bucket);
7989 }
7990}
7991
7992/* Release this socket's reference to a local port. */
7993static inline void __sctp_put_port(struct sock *sk)
7994{
7995 struct sctp_bind_hashbucket *head =
7996 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
7997 inet_sk(sk)->inet_num)];
7998 struct sctp_bind_bucket *pp;
7999
8000 spin_lock(&head->lock);
8001 pp = sctp_sk(sk)->bind_hash;
8002 __sk_del_bind_node(sk);
8003 sctp_sk(sk)->bind_hash = NULL;
8004 inet_sk(sk)->inet_num = 0;
8005 sctp_bucket_destroy(pp);
8006 spin_unlock(&head->lock);
8007}
8008
8009void sctp_put_port(struct sock *sk)
8010{
8011 local_bh_disable();
8012 __sctp_put_port(sk);
8013 local_bh_enable();
8014}
8015
8016/*
8017 * The system picks an ephemeral port and choose an address set equivalent
8018 * to binding with a wildcard address.
8019 * One of those addresses will be the primary address for the association.
8020 * This automatically enables the multihoming capability of SCTP.
8021 */
8022static int sctp_autobind(struct sock *sk)
8023{
8024 union sctp_addr autoaddr;
8025 struct sctp_af *af;
8026 __be16 port;
8027
8028 /* Initialize a local sockaddr structure to INADDR_ANY. */
8029 af = sctp_sk(sk)->pf->af;
8030
8031 port = htons(inet_sk(sk)->inet_num);
8032 af->inaddr_any(&autoaddr, port);
8033
8034 return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
8035}
8036
8037/* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
8038 *
8039 * From RFC 2292
8040 * 4.2 The cmsghdr Structure *
8041 *
8042 * When ancillary data is sent or received, any number of ancillary data
8043 * objects can be specified by the msg_control and msg_controllen members of
8044 * the msghdr structure, because each object is preceded by
8045 * a cmsghdr structure defining the object's length (the cmsg_len member).
8046 * Historically Berkeley-derived implementations have passed only one object
8047 * at a time, but this API allows multiple objects to be
8048 * passed in a single call to sendmsg() or recvmsg(). The following example
8049 * shows two ancillary data objects in a control buffer.
8050 *
8051 * |<--------------------------- msg_controllen -------------------------->|
8052 * | |
8053 *
8054 * |<----- ancillary data object ----->|<----- ancillary data object ----->|
8055 *
8056 * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
8057 * | | |
8058 *
8059 * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
8060 *
8061 * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
8062 * | | | | |
8063 *
8064 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8065 * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
8066 *
8067 * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
8068 *
8069 * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8070 * ^
8071 * |
8072 *
8073 * msg_control
8074 * points here
8075 */
8076static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
8077{
8078 struct msghdr *my_msg = (struct msghdr *)msg;
8079 struct cmsghdr *cmsg;
8080
8081 for_each_cmsghdr(cmsg, my_msg) {
8082 if (!CMSG_OK(my_msg, cmsg))
8083 return -EINVAL;
8084
8085 /* Should we parse this header or ignore? */
8086 if (cmsg->cmsg_level != IPPROTO_SCTP)
8087 continue;
8088
8089 /* Strictly check lengths following example in SCM code. */
8090 switch (cmsg->cmsg_type) {
8091 case SCTP_INIT:
8092 /* SCTP Socket API Extension
8093 * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
8094 *
8095 * This cmsghdr structure provides information for
8096 * initializing new SCTP associations with sendmsg().
8097 * The SCTP_INITMSG socket option uses this same data
8098 * structure. This structure is not used for
8099 * recvmsg().
8100 *
8101 * cmsg_level cmsg_type cmsg_data[]
8102 * ------------ ------------ ----------------------
8103 * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
8104 */
8105 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
8106 return -EINVAL;
8107
8108 cmsgs->init = CMSG_DATA(cmsg);
8109 break;
8110
8111 case SCTP_SNDRCV:
8112 /* SCTP Socket API Extension
8113 * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
8114 *
8115 * This cmsghdr structure specifies SCTP options for
8116 * sendmsg() and describes SCTP header information
8117 * about a received message through recvmsg().
8118 *
8119 * cmsg_level cmsg_type cmsg_data[]
8120 * ------------ ------------ ----------------------
8121 * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
8122 */
8123 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
8124 return -EINVAL;
8125
8126 cmsgs->srinfo = CMSG_DATA(cmsg);
8127
8128 if (cmsgs->srinfo->sinfo_flags &
8129 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8130 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8131 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8132 return -EINVAL;
8133 break;
8134
8135 case SCTP_SNDINFO:
8136 /* SCTP Socket API Extension
8137 * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
8138 *
8139 * This cmsghdr structure specifies SCTP options for
8140 * sendmsg(). This structure and SCTP_RCVINFO replaces
8141 * SCTP_SNDRCV which has been deprecated.
8142 *
8143 * cmsg_level cmsg_type cmsg_data[]
8144 * ------------ ------------ ---------------------
8145 * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
8146 */
8147 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
8148 return -EINVAL;
8149
8150 cmsgs->sinfo = CMSG_DATA(cmsg);
8151
8152 if (cmsgs->sinfo->snd_flags &
8153 ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8154 SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8155 SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8156 return -EINVAL;
8157 break;
8158 case SCTP_PRINFO:
8159 /* SCTP Socket API Extension
8160 * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
8161 *
8162 * This cmsghdr structure specifies SCTP options for sendmsg().
8163 *
8164 * cmsg_level cmsg_type cmsg_data[]
8165 * ------------ ------------ ---------------------
8166 * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
8167 */
8168 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
8169 return -EINVAL;
8170
8171 cmsgs->prinfo = CMSG_DATA(cmsg);
8172 if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
8173 return -EINVAL;
8174
8175 if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
8176 cmsgs->prinfo->pr_value = 0;
8177 break;
8178 case SCTP_AUTHINFO:
8179 /* SCTP Socket API Extension
8180 * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
8181 *
8182 * This cmsghdr structure specifies SCTP options for sendmsg().
8183 *
8184 * cmsg_level cmsg_type cmsg_data[]
8185 * ------------ ------------ ---------------------
8186 * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
8187 */
8188 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
8189 return -EINVAL;
8190
8191 cmsgs->authinfo = CMSG_DATA(cmsg);
8192 break;
8193 case SCTP_DSTADDRV4:
8194 case SCTP_DSTADDRV6:
8195 /* SCTP Socket API Extension
8196 * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
8197 *
8198 * This cmsghdr structure specifies SCTP options for sendmsg().
8199 *
8200 * cmsg_level cmsg_type cmsg_data[]
8201 * ------------ ------------ ---------------------
8202 * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
8203 * ------------ ------------ ---------------------
8204 * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
8205 */
8206 cmsgs->addrs_msg = my_msg;
8207 break;
8208 default:
8209 return -EINVAL;
8210 }
8211 }
8212
8213 return 0;
8214}
8215
8216/*
8217 * Wait for a packet..
8218 * Note: This function is the same function as in core/datagram.c
8219 * with a few modifications to make lksctp work.
8220 */
8221static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
8222{
8223 int error;
8224 DEFINE_WAIT(wait);
8225
8226 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
8227
8228 /* Socket errors? */
8229 error = sock_error(sk);
8230 if (error)
8231 goto out;
8232
8233 if (!skb_queue_empty(&sk->sk_receive_queue))
8234 goto ready;
8235
8236 /* Socket shut down? */
8237 if (sk->sk_shutdown & RCV_SHUTDOWN)
8238 goto out;
8239
8240 /* Sequenced packets can come disconnected. If so we report the
8241 * problem.
8242 */
8243 error = -ENOTCONN;
8244
8245 /* Is there a good reason to think that we may receive some data? */
8246 if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8247 goto out;
8248
8249 /* Handle signals. */
8250 if (signal_pending(current))
8251 goto interrupted;
8252
8253 /* Let another process have a go. Since we are going to sleep
8254 * anyway. Note: This may cause odd behaviors if the message
8255 * does not fit in the user's buffer, but this seems to be the
8256 * only way to honor MSG_DONTWAIT realistically.
8257 */
8258 release_sock(sk);
8259 *timeo_p = schedule_timeout(*timeo_p);
8260 lock_sock(sk);
8261
8262ready:
8263 finish_wait(sk_sleep(sk), &wait);
8264 return 0;
8265
8266interrupted:
8267 error = sock_intr_errno(*timeo_p);
8268
8269out:
8270 finish_wait(sk_sleep(sk), &wait);
8271 *err = error;
8272 return error;
8273}
8274
8275/* Receive a datagram.
8276 * Note: This is pretty much the same routine as in core/datagram.c
8277 * with a few changes to make lksctp work.
8278 */
8279struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8280 int noblock, int *err)
8281{
8282 int error;
8283 struct sk_buff *skb;
8284 long timeo;
8285
8286 timeo = sock_rcvtimeo(sk, noblock);
8287
8288 pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8289 MAX_SCHEDULE_TIMEOUT);
8290
8291 do {
8292 /* Again only user level code calls this function,
8293 * so nothing interrupt level
8294 * will suddenly eat the receive_queue.
8295 *
8296 * Look at current nfs client by the way...
8297 * However, this function was correct in any case. 8)
8298 */
8299 if (flags & MSG_PEEK) {
8300 skb = skb_peek(&sk->sk_receive_queue);
8301 if (skb)
8302 refcount_inc(&skb->users);
8303 } else {
8304 skb = __skb_dequeue(&sk->sk_receive_queue);
8305 }
8306
8307 if (skb)
8308 return skb;
8309
8310 /* Caller is allowed not to check sk->sk_err before calling. */
8311 error = sock_error(sk);
8312 if (error)
8313 goto no_packet;
8314
8315 if (sk->sk_shutdown & RCV_SHUTDOWN)
8316 break;
8317
8318 if (sk_can_busy_loop(sk)) {
8319 sk_busy_loop(sk, noblock);
8320
8321 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
8322 continue;
8323 }
8324
8325 /* User doesn't want to wait. */
8326 error = -EAGAIN;
8327 if (!timeo)
8328 goto no_packet;
8329 } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8330
8331 return NULL;
8332
8333no_packet:
8334 *err = error;
8335 return NULL;
8336}
8337
8338/* If sndbuf has changed, wake up per association sndbuf waiters. */
8339static void __sctp_write_space(struct sctp_association *asoc)
8340{
8341 struct sock *sk = asoc->base.sk;
8342
8343 if (sctp_wspace(asoc) <= 0)
8344 return;
8345
8346 if (waitqueue_active(&asoc->wait))
8347 wake_up_interruptible(&asoc->wait);
8348
8349 if (sctp_writeable(sk)) {
8350 struct socket_wq *wq;
8351
8352 rcu_read_lock();
8353 wq = rcu_dereference(sk->sk_wq);
8354 if (wq) {
8355 if (waitqueue_active(&wq->wait))
8356 wake_up_interruptible(&wq->wait);
8357
8358 /* Note that we try to include the Async I/O support
8359 * here by modeling from the current TCP/UDP code.
8360 * We have not tested with it yet.
8361 */
8362 if (!(sk->sk_shutdown & SEND_SHUTDOWN))
8363 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
8364 }
8365 rcu_read_unlock();
8366 }
8367}
8368
8369static void sctp_wake_up_waiters(struct sock *sk,
8370 struct sctp_association *asoc)
8371{
8372 struct sctp_association *tmp = asoc;
8373
8374 /* We do accounting for the sndbuf space per association,
8375 * so we only need to wake our own association.
8376 */
8377 if (asoc->ep->sndbuf_policy)
8378 return __sctp_write_space(asoc);
8379
8380 /* If association goes down and is just flushing its
8381 * outq, then just normally notify others.
8382 */
8383 if (asoc->base.dead)
8384 return sctp_write_space(sk);
8385
8386 /* Accounting for the sndbuf space is per socket, so we
8387 * need to wake up others, try to be fair and in case of
8388 * other associations, let them have a go first instead
8389 * of just doing a sctp_write_space() call.
8390 *
8391 * Note that we reach sctp_wake_up_waiters() only when
8392 * associations free up queued chunks, thus we are under
8393 * lock and the list of associations on a socket is
8394 * guaranteed not to change.
8395 */
8396 for (tmp = list_next_entry(tmp, asocs); 1;
8397 tmp = list_next_entry(tmp, asocs)) {
8398 /* Manually skip the head element. */
8399 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8400 continue;
8401 /* Wake up association. */
8402 __sctp_write_space(tmp);
8403 /* We've reached the end. */
8404 if (tmp == asoc)
8405 break;
8406 }
8407}
8408
8409/* Do accounting for the sndbuf space.
8410 * Decrement the used sndbuf space of the corresponding association by the
8411 * data size which was just transmitted(freed).
8412 */
8413static void sctp_wfree(struct sk_buff *skb)
8414{
8415 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8416 struct sctp_association *asoc = chunk->asoc;
8417 struct sock *sk = asoc->base.sk;
8418
8419 asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
8420 sizeof(struct sk_buff) +
8421 sizeof(struct sctp_chunk);
8422
8423 WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
8424
8425 /*
8426 * This undoes what is done via sctp_set_owner_w and sk_mem_charge
8427 */
8428 sk->sk_wmem_queued -= skb->truesize;
8429 sk_mem_uncharge(sk, skb->truesize);
8430
8431 if (chunk->shkey) {
8432 struct sctp_shared_key *shkey = chunk->shkey;
8433
8434 /* refcnt == 2 and !list_empty mean after this release, it's
8435 * not being used anywhere, and it's time to notify userland
8436 * that this shkey can be freed if it's been deactivated.
8437 */
8438 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
8439 refcount_read(&shkey->refcnt) == 2) {
8440 struct sctp_ulpevent *ev;
8441
8442 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
8443 SCTP_AUTH_FREE_KEY,
8444 GFP_KERNEL);
8445 if (ev)
8446 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
8447 }
8448 sctp_auth_shkey_release(chunk->shkey);
8449 }
8450
8451 sock_wfree(skb);
8452 sctp_wake_up_waiters(sk, asoc);
8453
8454 sctp_association_put(asoc);
8455}
8456
8457/* Do accounting for the receive space on the socket.
8458 * Accounting for the association is done in ulpevent.c
8459 * We set this as a destructor for the cloned data skbs so that
8460 * accounting is done at the correct time.
8461 */
8462void sctp_sock_rfree(struct sk_buff *skb)
8463{
8464 struct sock *sk = skb->sk;
8465 struct sctp_ulpevent *event = sctp_skb2event(skb);
8466
8467 atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
8468
8469 /*
8470 * Mimic the behavior of sock_rfree
8471 */
8472 sk_mem_uncharge(sk, event->rmem_len);
8473}
8474
8475
8476/* Helper function to wait for space in the sndbuf. */
8477static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
8478 size_t msg_len)
8479{
8480 struct sock *sk = asoc->base.sk;
8481 long current_timeo = *timeo_p;
8482 DEFINE_WAIT(wait);
8483 int err = 0;
8484
8485 pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8486 *timeo_p, msg_len);
8487
8488 /* Increment the association's refcnt. */
8489 sctp_association_hold(asoc);
8490
8491 /* Wait on the association specific sndbuf space. */
8492 for (;;) {
8493 prepare_to_wait_exclusive(&asoc->wait, &wait,
8494 TASK_INTERRUPTIBLE);
8495 if (asoc->base.dead)
8496 goto do_dead;
8497 if (!*timeo_p)
8498 goto do_nonblock;
8499 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
8500 goto do_error;
8501 if (signal_pending(current))
8502 goto do_interrupted;
8503 if ((int)msg_len <= sctp_wspace(asoc))
8504 break;
8505
8506 /* Let another process have a go. Since we are going
8507 * to sleep anyway.
8508 */
8509 release_sock(sk);
8510 current_timeo = schedule_timeout(current_timeo);
8511 lock_sock(sk);
8512 if (sk != asoc->base.sk)
8513 goto do_error;
8514
8515 *timeo_p = current_timeo;
8516 }
8517
8518out:
8519 finish_wait(&asoc->wait, &wait);
8520
8521 /* Release the association's refcnt. */
8522 sctp_association_put(asoc);
8523
8524 return err;
8525
8526do_dead:
8527 err = -ESRCH;
8528 goto out;
8529
8530do_error:
8531 err = -EPIPE;
8532 goto out;
8533
8534do_interrupted:
8535 err = sock_intr_errno(*timeo_p);
8536 goto out;
8537
8538do_nonblock:
8539 err = -EAGAIN;
8540 goto out;
8541}
8542
8543void sctp_data_ready(struct sock *sk)
8544{
8545 struct socket_wq *wq;
8546
8547 rcu_read_lock();
8548 wq = rcu_dereference(sk->sk_wq);
8549 if (skwq_has_sleeper(wq))
8550 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
8551 EPOLLRDNORM | EPOLLRDBAND);
8552 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
8553 rcu_read_unlock();
8554}
8555
8556/* If socket sndbuf has changed, wake up all per association waiters. */
8557void sctp_write_space(struct sock *sk)
8558{
8559 struct sctp_association *asoc;
8560
8561 /* Wake up the tasks in each wait queue. */
8562 list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
8563 __sctp_write_space(asoc);
8564 }
8565}
8566
8567/* Is there any sndbuf space available on the socket?
8568 *
8569 * Note that sk_wmem_alloc is the sum of the send buffers on all of the
8570 * associations on the same socket. For a UDP-style socket with
8571 * multiple associations, it is possible for it to be "unwriteable"
8572 * prematurely. I assume that this is acceptable because
8573 * a premature "unwriteable" is better than an accidental "writeable" which
8574 * would cause an unwanted block under certain circumstances. For the 1-1
8575 * UDP-style sockets or TCP-style sockets, this code should work.
8576 * - Daisy
8577 */
8578static bool sctp_writeable(struct sock *sk)
8579{
8580 return sk->sk_sndbuf > sk->sk_wmem_queued;
8581}
8582
8583/* Wait for an association to go into ESTABLISHED state. If timeout is 0,
8584 * returns immediately with EINPROGRESS.
8585 */
8586static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
8587{
8588 struct sock *sk = asoc->base.sk;
8589 int err = 0;
8590 long current_timeo = *timeo_p;
8591 DEFINE_WAIT(wait);
8592
8593 pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
8594
8595 /* Increment the association's refcnt. */
8596 sctp_association_hold(asoc);
8597
8598 for (;;) {
8599 prepare_to_wait_exclusive(&asoc->wait, &wait,
8600 TASK_INTERRUPTIBLE);
8601 if (!*timeo_p)
8602 goto do_nonblock;
8603 if (sk->sk_shutdown & RCV_SHUTDOWN)
8604 break;
8605 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
8606 asoc->base.dead)
8607 goto do_error;
8608 if (signal_pending(current))
8609 goto do_interrupted;
8610
8611 if (sctp_state(asoc, ESTABLISHED))
8612 break;
8613
8614 /* Let another process have a go. Since we are going
8615 * to sleep anyway.
8616 */
8617 release_sock(sk);
8618 current_timeo = schedule_timeout(current_timeo);
8619 lock_sock(sk);
8620
8621 *timeo_p = current_timeo;
8622 }
8623
8624out:
8625 finish_wait(&asoc->wait, &wait);
8626
8627 /* Release the association's refcnt. */
8628 sctp_association_put(asoc);
8629
8630 return err;
8631
8632do_error:
8633 if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
8634 err = -ETIMEDOUT;
8635 else
8636 err = -ECONNREFUSED;
8637 goto out;
8638
8639do_interrupted:
8640 err = sock_intr_errno(*timeo_p);
8641 goto out;
8642
8643do_nonblock:
8644 err = -EINPROGRESS;
8645 goto out;
8646}
8647
8648static int sctp_wait_for_accept(struct sock *sk, long timeo)
8649{
8650 struct sctp_endpoint *ep;
8651 int err = 0;
8652 DEFINE_WAIT(wait);
8653
8654 ep = sctp_sk(sk)->ep;
8655
8656
8657 for (;;) {
8658 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
8659 TASK_INTERRUPTIBLE);
8660
8661 if (list_empty(&ep->asocs)) {
8662 release_sock(sk);
8663 timeo = schedule_timeout(timeo);
8664 lock_sock(sk);
8665 }
8666
8667 err = -EINVAL;
8668 if (!sctp_sstate(sk, LISTENING))
8669 break;
8670
8671 err = 0;
8672 if (!list_empty(&ep->asocs))
8673 break;
8674
8675 err = sock_intr_errno(timeo);
8676 if (signal_pending(current))
8677 break;
8678
8679 err = -EAGAIN;
8680 if (!timeo)
8681 break;
8682 }
8683
8684 finish_wait(sk_sleep(sk), &wait);
8685
8686 return err;
8687}
8688
8689static void sctp_wait_for_close(struct sock *sk, long timeout)
8690{
8691 DEFINE_WAIT(wait);
8692
8693 do {
8694 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
8695 if (list_empty(&sctp_sk(sk)->ep->asocs))
8696 break;
8697 release_sock(sk);
8698 timeout = schedule_timeout(timeout);
8699 lock_sock(sk);
8700 } while (!signal_pending(current) && timeout);
8701
8702 finish_wait(sk_sleep(sk), &wait);
8703}
8704
8705static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
8706{
8707 struct sk_buff *frag;
8708
8709 if (!skb->data_len)
8710 goto done;
8711
8712 /* Don't forget the fragments. */
8713 skb_walk_frags(skb, frag)
8714 sctp_skb_set_owner_r_frag(frag, sk);
8715
8716done:
8717 sctp_skb_set_owner_r(skb, sk);
8718}
8719
8720void sctp_copy_sock(struct sock *newsk, struct sock *sk,
8721 struct sctp_association *asoc)
8722{
8723 struct inet_sock *inet = inet_sk(sk);
8724 struct inet_sock *newinet;
8725 struct sctp_sock *sp = sctp_sk(sk);
8726 struct sctp_endpoint *ep = sp->ep;
8727
8728 newsk->sk_type = sk->sk_type;
8729 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
8730 newsk->sk_flags = sk->sk_flags;
8731 newsk->sk_tsflags = sk->sk_tsflags;
8732 newsk->sk_no_check_tx = sk->sk_no_check_tx;
8733 newsk->sk_no_check_rx = sk->sk_no_check_rx;
8734 newsk->sk_reuse = sk->sk_reuse;
8735 sctp_sk(newsk)->reuse = sp->reuse;
8736
8737 newsk->sk_shutdown = sk->sk_shutdown;
8738 newsk->sk_destruct = sctp_destruct_sock;
8739 newsk->sk_family = sk->sk_family;
8740 newsk->sk_protocol = IPPROTO_SCTP;
8741 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
8742 newsk->sk_sndbuf = sk->sk_sndbuf;
8743 newsk->sk_rcvbuf = sk->sk_rcvbuf;
8744 newsk->sk_lingertime = sk->sk_lingertime;
8745 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
8746 newsk->sk_sndtimeo = sk->sk_sndtimeo;
8747 newsk->sk_rxhash = sk->sk_rxhash;
8748
8749 newinet = inet_sk(newsk);
8750
8751 /* Initialize sk's sport, dport, rcv_saddr and daddr for
8752 * getsockname() and getpeername()
8753 */
8754 newinet->inet_sport = inet->inet_sport;
8755 newinet->inet_saddr = inet->inet_saddr;
8756 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
8757 newinet->inet_dport = htons(asoc->peer.port);
8758 newinet->pmtudisc = inet->pmtudisc;
8759 newinet->inet_id = prandom_u32();
8760
8761 newinet->uc_ttl = inet->uc_ttl;
8762 newinet->mc_loop = 1;
8763 newinet->mc_ttl = 1;
8764 newinet->mc_index = 0;
8765 newinet->mc_list = NULL;
8766
8767 if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
8768 net_enable_timestamp();
8769
8770 /* Set newsk security attributes from orginal sk and connection
8771 * security attribute from ep.
8772 */
8773 security_sctp_sk_clone(ep, sk, newsk);
8774}
8775
8776static inline void sctp_copy_descendant(struct sock *sk_to,
8777 const struct sock *sk_from)
8778{
8779 int ancestor_size = sizeof(struct inet_sock) +
8780 sizeof(struct sctp_sock) -
8781 offsetof(struct sctp_sock, auto_asconf_list);
8782
8783 if (sk_from->sk_family == PF_INET6)
8784 ancestor_size += sizeof(struct ipv6_pinfo);
8785
8786 __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
8787}
8788
8789/* Populate the fields of the newsk from the oldsk and migrate the assoc
8790 * and its messages to the newsk.
8791 */
8792static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
8793 struct sctp_association *assoc,
8794 enum sctp_socket_type type)
8795{
8796 struct sctp_sock *oldsp = sctp_sk(oldsk);
8797 struct sctp_sock *newsp = sctp_sk(newsk);
8798 struct sctp_bind_bucket *pp; /* hash list port iterator */
8799 struct sctp_endpoint *newep = newsp->ep;
8800 struct sk_buff *skb, *tmp;
8801 struct sctp_ulpevent *event;
8802 struct sctp_bind_hashbucket *head;
8803
8804 /* Migrate socket buffer sizes and all the socket level options to the
8805 * new socket.
8806 */
8807 newsk->sk_sndbuf = oldsk->sk_sndbuf;
8808 newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
8809 /* Brute force copy old sctp opt. */
8810 sctp_copy_descendant(newsk, oldsk);
8811
8812 /* Restore the ep value that was overwritten with the above structure
8813 * copy.
8814 */
8815 newsp->ep = newep;
8816 newsp->hmac = NULL;
8817
8818 /* Hook this new socket in to the bind_hash list. */
8819 head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
8820 inet_sk(oldsk)->inet_num)];
8821 spin_lock_bh(&head->lock);
8822 pp = sctp_sk(oldsk)->bind_hash;
8823 sk_add_bind_node(newsk, &pp->owner);
8824 sctp_sk(newsk)->bind_hash = pp;
8825 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
8826 spin_unlock_bh(&head->lock);
8827
8828 /* Copy the bind_addr list from the original endpoint to the new
8829 * endpoint so that we can handle restarts properly
8830 */
8831 sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
8832 &oldsp->ep->base.bind_addr, GFP_KERNEL);
8833
8834 /* Move any messages in the old socket's receive queue that are for the
8835 * peeled off association to the new socket's receive queue.
8836 */
8837 sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
8838 event = sctp_skb2event(skb);
8839 if (event->asoc == assoc) {
8840 __skb_unlink(skb, &oldsk->sk_receive_queue);
8841 __skb_queue_tail(&newsk->sk_receive_queue, skb);
8842 sctp_skb_set_owner_r_frag(skb, newsk);
8843 }
8844 }
8845
8846 /* Clean up any messages pending delivery due to partial
8847 * delivery. Three cases:
8848 * 1) No partial deliver; no work.
8849 * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
8850 * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
8851 */
8852 skb_queue_head_init(&newsp->pd_lobby);
8853 atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
8854
8855 if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
8856 struct sk_buff_head *queue;
8857
8858 /* Decide which queue to move pd_lobby skbs to. */
8859 if (assoc->ulpq.pd_mode) {
8860 queue = &newsp->pd_lobby;
8861 } else
8862 queue = &newsk->sk_receive_queue;
8863
8864 /* Walk through the pd_lobby, looking for skbs that
8865 * need moved to the new socket.
8866 */
8867 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
8868 event = sctp_skb2event(skb);
8869 if (event->asoc == assoc) {
8870 __skb_unlink(skb, &oldsp->pd_lobby);
8871 __skb_queue_tail(queue, skb);
8872 sctp_skb_set_owner_r_frag(skb, newsk);
8873 }
8874 }
8875
8876 /* Clear up any skbs waiting for the partial
8877 * delivery to finish.
8878 */
8879 if (assoc->ulpq.pd_mode)
8880 sctp_clear_pd(oldsk, NULL);
8881
8882 }
8883
8884 sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
8885
8886 /* Set the type of socket to indicate that it is peeled off from the
8887 * original UDP-style socket or created with the accept() call on a
8888 * TCP-style socket..
8889 */
8890 newsp->type = type;
8891
8892 /* Mark the new socket "in-use" by the user so that any packets
8893 * that may arrive on the association after we've moved it are
8894 * queued to the backlog. This prevents a potential race between
8895 * backlog processing on the old socket and new-packet processing
8896 * on the new socket.
8897 *
8898 * The caller has just allocated newsk so we can guarantee that other
8899 * paths won't try to lock it and then oldsk.
8900 */
8901 lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
8902 sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
8903 sctp_assoc_migrate(assoc, newsk);
8904 sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
8905
8906 /* If the association on the newsk is already closed before accept()
8907 * is called, set RCV_SHUTDOWN flag.
8908 */
8909 if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
8910 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
8911 newsk->sk_shutdown |= RCV_SHUTDOWN;
8912 } else {
8913 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
8914 }
8915
8916 release_sock(newsk);
8917}
8918
8919
8920/* This proto struct describes the ULP interface for SCTP. */
8921struct proto sctp_prot = {
8922 .name = "SCTP",
8923 .owner = THIS_MODULE,
8924 .close = sctp_close,
8925 .disconnect = sctp_disconnect,
8926 .accept = sctp_accept,
8927 .ioctl = sctp_ioctl,
8928 .init = sctp_init_sock,
8929 .destroy = sctp_destroy_sock,
8930 .shutdown = sctp_shutdown,
8931 .setsockopt = sctp_setsockopt,
8932 .getsockopt = sctp_getsockopt,
8933 .sendmsg = sctp_sendmsg,
8934 .recvmsg = sctp_recvmsg,
8935 .bind = sctp_bind,
8936 .backlog_rcv = sctp_backlog_rcv,
8937 .hash = sctp_hash,
8938 .unhash = sctp_unhash,
8939 .no_autobind = true,
8940 .obj_size = sizeof(struct sctp_sock),
8941 .useroffset = offsetof(struct sctp_sock, subscribe),
8942 .usersize = offsetof(struct sctp_sock, initmsg) -
8943 offsetof(struct sctp_sock, subscribe) +
8944 sizeof_field(struct sctp_sock, initmsg),
8945 .sysctl_mem = sysctl_sctp_mem,
8946 .sysctl_rmem = sysctl_sctp_rmem,
8947 .sysctl_wmem = sysctl_sctp_wmem,
8948 .memory_pressure = &sctp_memory_pressure,
8949 .enter_memory_pressure = sctp_enter_memory_pressure,
8950 .memory_allocated = &sctp_memory_allocated,
8951 .sockets_allocated = &sctp_sockets_allocated,
8952};
8953
8954#if IS_ENABLED(CONFIG_IPV6)
8955
8956#include <net/transp_v6.h>
8957static void sctp_v6_destroy_sock(struct sock *sk)
8958{
8959 sctp_destroy_sock(sk);
8960 inet6_destroy_sock(sk);
8961}
8962
8963struct proto sctpv6_prot = {
8964 .name = "SCTPv6",
8965 .owner = THIS_MODULE,
8966 .close = sctp_close,
8967 .disconnect = sctp_disconnect,
8968 .accept = sctp_accept,
8969 .ioctl = sctp_ioctl,
8970 .init = sctp_init_sock,
8971 .destroy = sctp_v6_destroy_sock,
8972 .shutdown = sctp_shutdown,
8973 .setsockopt = sctp_setsockopt,
8974 .getsockopt = sctp_getsockopt,
8975 .sendmsg = sctp_sendmsg,
8976 .recvmsg = sctp_recvmsg,
8977 .bind = sctp_bind,
8978 .backlog_rcv = sctp_backlog_rcv,
8979 .hash = sctp_hash,
8980 .unhash = sctp_unhash,
8981 .no_autobind = true,
8982 .obj_size = sizeof(struct sctp6_sock),
8983 .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
8984 .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
8985 offsetof(struct sctp6_sock, sctp.subscribe) +
8986 sizeof_field(struct sctp6_sock, sctp.initmsg),
8987 .sysctl_mem = sysctl_sctp_mem,
8988 .sysctl_rmem = sysctl_sctp_rmem,
8989 .sysctl_wmem = sysctl_sctp_wmem,
8990 .memory_pressure = &sctp_memory_pressure,
8991 .enter_memory_pressure = sctp_enter_memory_pressure,
8992 .memory_allocated = &sctp_memory_allocated,
8993 .sockets_allocated = &sctp_sockets_allocated,
8994};
8995#endif /* IS_ENABLED(CONFIG_IPV6) */