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