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