projects/linux·blob assoc-array-fixes · refs

9743 lines · 265.5 KiB

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