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