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