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