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