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