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