xref: /freebsd/sys/netinet/sctp_usrreq.c (revision f9478f91fbe737cd972cd2828a5ce75286bd83cf)
1 /*-
2  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * a) Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  * b) Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the distribution.
15  *
16  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <netinet/sctp_os.h>
37 #include <sys/proc.h>
38 #include <netinet/sctp_pcb.h>
39 #include <netinet/sctp_header.h>
40 #include <netinet/sctp_var.h>
41 #ifdef INET6
42 #endif
43 #include <netinet/sctp_sysctl.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctputil.h>
48 #include <netinet/sctp_indata.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_auth.h>
51 #include <netinet/sctp_bsd_addr.h>
52 #include <netinet/udp.h>
53 
54 
55 
56 extern struct sctp_cc_functions sctp_cc_functions[];
57 extern struct sctp_ss_functions sctp_ss_functions[];
58 
59 void
60 sctp_init(void)
61 {
62 	u_long sb_max_adj;
63 
64 	/* Initialize and modify the sysctled variables */
65 	sctp_init_sysctls();
66 	if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
67 		SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) = (nmbclusters / 8);
68 	/*
69 	 * Allow a user to take no more than 1/2 the number of clusters or
70 	 * the SB_MAX whichever is smaller for the send window.
71 	 */
72 	sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
73 	SCTP_BASE_SYSCTL(sctp_sendspace) = min(sb_max_adj,
74 	    (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
75 	/*
76 	 * Now for the recv window, should we take the same amount? or
77 	 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
78 	 * now I will just copy.
79 	 */
80 	SCTP_BASE_SYSCTL(sctp_recvspace) = SCTP_BASE_SYSCTL(sctp_sendspace);
81 	SCTP_BASE_VAR(first_time) = 0;
82 	SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
83 	sctp_pcb_init();
84 #if defined(SCTP_PACKET_LOGGING)
85 	SCTP_BASE_VAR(packet_log_writers) = 0;
86 	SCTP_BASE_VAR(packet_log_end) = 0;
87 	bzero(&SCTP_BASE_VAR(packet_log_buffer), SCTP_PACKET_LOG_SIZE);
88 #endif
89 }
90 
91 void
92 sctp_finish(void)
93 {
94 	sctp_pcb_finish();
95 }
96 
97 
98 
99 void
100 sctp_pathmtu_adjustment(struct sctp_tcb *stcb, uint16_t nxtsz)
101 {
102 	struct sctp_tmit_chunk *chk;
103 	uint16_t overhead;
104 
105 	/* Adjust that too */
106 	stcb->asoc.smallest_mtu = nxtsz;
107 	/* now off to subtract IP_DF flag if needed */
108 	overhead = IP_HDR_SIZE;
109 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
110 		overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
111 	}
112 	TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
113 		if ((chk->send_size + overhead) > nxtsz) {
114 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
115 		}
116 	}
117 	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
118 		if ((chk->send_size + overhead) > nxtsz) {
119 			/*
120 			 * For this guy we also mark for immediate resend
121 			 * since we sent to big of chunk
122 			 */
123 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
124 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
125 				sctp_flight_size_decrease(chk);
126 				sctp_total_flight_decrease(stcb, chk);
127 			}
128 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
129 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
130 			}
131 			chk->sent = SCTP_DATAGRAM_RESEND;
132 			chk->rec.data.doing_fast_retransmit = 0;
133 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
134 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
135 				    chk->whoTo->flight_size,
136 				    chk->book_size,
137 				    (uintptr_t) chk->whoTo,
138 				    chk->rec.data.TSN_seq);
139 			}
140 			/* Clear any time so NO RTT is being done */
141 			chk->do_rtt = 0;
142 		}
143 	}
144 }
145 
146 #ifdef INET
147 static void
148 sctp_notify_mbuf(struct sctp_inpcb *inp,
149     struct sctp_tcb *stcb,
150     struct sctp_nets *net,
151     struct ip *ip,
152     struct sctphdr *sh)
153 {
154 	struct icmp *icmph;
155 	int totsz, tmr_stopped = 0;
156 	uint16_t nxtsz;
157 
158 	/* protection */
159 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
160 	    (ip == NULL) || (sh == NULL)) {
161 		if (stcb != NULL) {
162 			SCTP_TCB_UNLOCK(stcb);
163 		}
164 		return;
165 	}
166 	/* First job is to verify the vtag matches what I would send */
167 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
168 		SCTP_TCB_UNLOCK(stcb);
169 		return;
170 	}
171 	icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
172 	    sizeof(struct ip)));
173 	if (icmph->icmp_type != ICMP_UNREACH) {
174 		/* We only care about unreachable */
175 		SCTP_TCB_UNLOCK(stcb);
176 		return;
177 	}
178 	if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
179 		/* not a unreachable message due to frag. */
180 		SCTP_TCB_UNLOCK(stcb);
181 		return;
182 	}
183 	totsz = ntohs(ip->ip_len);
184 
185 	nxtsz = ntohs(icmph->icmp_nextmtu);
186 	if (nxtsz == 0) {
187 		/*
188 		 * old type router that does not tell us what the next size
189 		 * mtu is. Rats we will have to guess (in a educated fashion
190 		 * of course)
191 		 */
192 		nxtsz = sctp_get_prev_mtu(totsz);
193 	}
194 	/* Stop any PMTU timer */
195 	if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
196 		tmr_stopped = 1;
197 		sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
198 		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
199 	}
200 	/* Adjust destination size limit */
201 	if (net->mtu > nxtsz) {
202 		net->mtu = nxtsz;
203 		if (net->port) {
204 			net->mtu -= sizeof(struct udphdr);
205 		}
206 	}
207 	/* now what about the ep? */
208 	if (stcb->asoc.smallest_mtu > nxtsz) {
209 		sctp_pathmtu_adjustment(stcb, nxtsz);
210 	}
211 	if (tmr_stopped)
212 		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
213 
214 	SCTP_TCB_UNLOCK(stcb);
215 }
216 
217 void
218 sctp_notify(struct sctp_inpcb *inp,
219     struct ip *ip,
220     struct sctphdr *sh,
221     struct sockaddr *to,
222     struct sctp_tcb *stcb,
223     struct sctp_nets *net)
224 {
225 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
226 	struct socket *so;
227 
228 #endif
229 	struct icmp *icmph;
230 
231 	/* protection */
232 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
233 	    (sh == NULL) || (to == NULL)) {
234 		if (stcb)
235 			SCTP_TCB_UNLOCK(stcb);
236 		return;
237 	}
238 	/* First job is to verify the vtag matches what I would send */
239 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
240 		SCTP_TCB_UNLOCK(stcb);
241 		return;
242 	}
243 	icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
244 	    sizeof(struct ip)));
245 	if (icmph->icmp_type != ICMP_UNREACH) {
246 		/* We only care about unreachable */
247 		SCTP_TCB_UNLOCK(stcb);
248 		return;
249 	}
250 	if ((icmph->icmp_code == ICMP_UNREACH_NET) ||
251 	    (icmph->icmp_code == ICMP_UNREACH_HOST) ||
252 	    (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
253 	    (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
254 	    (icmph->icmp_code == ICMP_UNREACH_ISOLATED) ||
255 	    (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) ||
256 	    (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
257 	    (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
258 
259 		/*
260 		 * Hmm reachablity problems we must examine closely. If its
261 		 * not reachable, we may have lost a network. Or if there is
262 		 * NO protocol at the other end named SCTP. well we consider
263 		 * it a OOTB abort.
264 		 */
265 		if (net->dest_state & SCTP_ADDR_REACHABLE) {
266 			/* Ok that destination is NOT reachable */
267 			net->dest_state &= ~SCTP_ADDR_REACHABLE;
268 			net->dest_state &= ~SCTP_ADDR_PF;
269 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
270 			    stcb, 0,
271 			    (void *)net, SCTP_SO_NOT_LOCKED);
272 		}
273 		SCTP_TCB_UNLOCK(stcb);
274 	} else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
275 	    (icmph->icmp_code == ICMP_UNREACH_PORT)) {
276 		/*
277 		 * Here the peer is either playing tricks on us, including
278 		 * an address that belongs to someone who does not support
279 		 * SCTP OR was a userland implementation that shutdown and
280 		 * now is dead. In either case treat it like a OOTB abort
281 		 * with no TCB
282 		 */
283 		sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
284 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
285 		so = SCTP_INP_SO(inp);
286 		atomic_add_int(&stcb->asoc.refcnt, 1);
287 		SCTP_TCB_UNLOCK(stcb);
288 		SCTP_SOCKET_LOCK(so, 1);
289 		SCTP_TCB_LOCK(stcb);
290 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
291 #endif
292 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
293 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
294 		SCTP_SOCKET_UNLOCK(so, 1);
295 		/* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
296 #endif
297 		/* no need to unlock here, since the TCB is gone */
298 	} else {
299 		SCTP_TCB_UNLOCK(stcb);
300 	}
301 }
302 
303 #endif
304 
305 #ifdef INET
306 void
307 sctp_ctlinput(cmd, sa, vip)
308 	int cmd;
309 	struct sockaddr *sa;
310 	void *vip;
311 {
312 	struct ip *ip = vip;
313 	struct sctphdr *sh;
314 	uint32_t vrf_id;
315 
316 	/* FIX, for non-bsd is this right? */
317 	vrf_id = SCTP_DEFAULT_VRFID;
318 	if (sa->sa_family != AF_INET ||
319 	    ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
320 		return;
321 	}
322 	if (PRC_IS_REDIRECT(cmd)) {
323 		ip = 0;
324 	} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
325 		return;
326 	}
327 	if (ip) {
328 		struct sctp_inpcb *inp = NULL;
329 		struct sctp_tcb *stcb = NULL;
330 		struct sctp_nets *net = NULL;
331 		struct sockaddr_in to, from;
332 
333 		sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
334 		bzero(&to, sizeof(to));
335 		bzero(&from, sizeof(from));
336 		from.sin_family = to.sin_family = AF_INET;
337 		from.sin_len = to.sin_len = sizeof(to);
338 		from.sin_port = sh->src_port;
339 		from.sin_addr = ip->ip_src;
340 		to.sin_port = sh->dest_port;
341 		to.sin_addr = ip->ip_dst;
342 
343 		/*
344 		 * 'to' holds the dest of the packet that failed to be sent.
345 		 * 'from' holds our local endpoint address. Thus we reverse
346 		 * the to and the from in the lookup.
347 		 */
348 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&to,
349 		    (struct sockaddr *)&from,
350 		    &inp, &net, 1, vrf_id);
351 		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
352 			if (cmd != PRC_MSGSIZE) {
353 				sctp_notify(inp, ip, sh,
354 				    (struct sockaddr *)&to, stcb,
355 				    net);
356 			} else {
357 				/* handle possible ICMP size messages */
358 				sctp_notify_mbuf(inp, stcb, net, ip, sh);
359 			}
360 		} else {
361 			if ((stcb == NULL) && (inp != NULL)) {
362 				/* reduce ref-count */
363 				SCTP_INP_WLOCK(inp);
364 				SCTP_INP_DECR_REF(inp);
365 				SCTP_INP_WUNLOCK(inp);
366 			}
367 			if (stcb) {
368 				SCTP_TCB_UNLOCK(stcb);
369 			}
370 		}
371 	}
372 	return;
373 }
374 
375 #endif
376 
377 static int
378 sctp_getcred(SYSCTL_HANDLER_ARGS)
379 {
380 	struct xucred xuc;
381 	struct sockaddr_in addrs[2];
382 	struct sctp_inpcb *inp;
383 	struct sctp_nets *net;
384 	struct sctp_tcb *stcb;
385 	int error;
386 	uint32_t vrf_id;
387 
388 	/* FIX, for non-bsd is this right? */
389 	vrf_id = SCTP_DEFAULT_VRFID;
390 
391 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
392 
393 	if (error)
394 		return (error);
395 
396 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
397 	if (error)
398 		return (error);
399 
400 	stcb = sctp_findassociation_addr_sa(sintosa(&addrs[1]),
401 	    sintosa(&addrs[0]),
402 	    &inp, &net, 1, vrf_id);
403 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
404 		if ((inp != NULL) && (stcb == NULL)) {
405 			/* reduce ref-count */
406 			SCTP_INP_WLOCK(inp);
407 			SCTP_INP_DECR_REF(inp);
408 			goto cred_can_cont;
409 		}
410 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
411 		error = ENOENT;
412 		goto out;
413 	}
414 	SCTP_TCB_UNLOCK(stcb);
415 	/*
416 	 * We use the write lock here, only since in the error leg we need
417 	 * it. If we used RLOCK, then we would have to
418 	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
419 	 * Better to use higher wlock.
420 	 */
421 	SCTP_INP_WLOCK(inp);
422 cred_can_cont:
423 	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
424 	if (error) {
425 		SCTP_INP_WUNLOCK(inp);
426 		goto out;
427 	}
428 	cru2x(inp->sctp_socket->so_cred, &xuc);
429 	SCTP_INP_WUNLOCK(inp);
430 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
431 out:
432 	return (error);
433 }
434 
435 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
436     0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
437 
438 
439 #ifdef INET
440 static void
441 sctp_abort(struct socket *so)
442 {
443 	struct sctp_inpcb *inp;
444 	uint32_t flags;
445 
446 	inp = (struct sctp_inpcb *)so->so_pcb;
447 	if (inp == NULL) {
448 		return;
449 	}
450 sctp_must_try_again:
451 	flags = inp->sctp_flags;
452 #ifdef SCTP_LOG_CLOSING
453 	sctp_log_closing(inp, NULL, 17);
454 #endif
455 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
456 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
457 #ifdef SCTP_LOG_CLOSING
458 		sctp_log_closing(inp, NULL, 16);
459 #endif
460 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
461 		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
462 		SOCK_LOCK(so);
463 		SCTP_SB_CLEAR(so->so_snd);
464 		/*
465 		 * same for the rcv ones, they are only here for the
466 		 * accounting/select.
467 		 */
468 		SCTP_SB_CLEAR(so->so_rcv);
469 
470 		/* Now null out the reference, we are completely detached. */
471 		so->so_pcb = NULL;
472 		SOCK_UNLOCK(so);
473 	} else {
474 		flags = inp->sctp_flags;
475 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
476 			goto sctp_must_try_again;
477 		}
478 	}
479 	return;
480 }
481 
482 static int
483 sctp_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
484 {
485 	struct sctp_inpcb *inp;
486 	struct inpcb *ip_inp;
487 	int error;
488 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
489 
490 #ifdef IPSEC
491 	uint32_t flags;
492 
493 #endif
494 
495 	inp = (struct sctp_inpcb *)so->so_pcb;
496 	if (inp != 0) {
497 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
498 		return (EINVAL);
499 	}
500 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
501 		error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
502 		if (error) {
503 			return (error);
504 		}
505 	}
506 	error = sctp_inpcb_alloc(so, vrf_id);
507 	if (error) {
508 		return (error);
509 	}
510 	inp = (struct sctp_inpcb *)so->so_pcb;
511 	SCTP_INP_WLOCK(inp);
512 	inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;	/* I'm not v6! */
513 	ip_inp = &inp->ip_inp.inp;
514 	ip_inp->inp_vflag |= INP_IPV4;
515 	ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
516 #ifdef IPSEC
517 	error = ipsec_init_policy(so, &ip_inp->inp_sp);
518 #ifdef SCTP_LOG_CLOSING
519 	sctp_log_closing(inp, NULL, 17);
520 #endif
521 	if (error != 0) {
522 try_again:
523 		flags = inp->sctp_flags;
524 		if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
525 		    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
526 #ifdef SCTP_LOG_CLOSING
527 			sctp_log_closing(inp, NULL, 15);
528 #endif
529 			SCTP_INP_WUNLOCK(inp);
530 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
531 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
532 		} else {
533 			flags = inp->sctp_flags;
534 			if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
535 				goto try_again;
536 			} else {
537 				SCTP_INP_WUNLOCK(inp);
538 			}
539 		}
540 		return (error);
541 	}
542 #endif				/* IPSEC */
543 	SCTP_INP_WUNLOCK(inp);
544 	return (0);
545 }
546 
547 static int
548 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
549 {
550 	struct sctp_inpcb *inp;
551 
552 	inp = (struct sctp_inpcb *)so->so_pcb;
553 	if (inp == NULL) {
554 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
555 		return (EINVAL);
556 	}
557 	if (addr != NULL) {
558 		if ((addr->sa_family != AF_INET) ||
559 		    (addr->sa_len != sizeof(struct sockaddr_in))) {
560 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
561 			return (EINVAL);
562 		}
563 	}
564 	return (sctp_inpcb_bind(so, addr, NULL, p));
565 }
566 
567 #endif
568 void
569 sctp_close(struct socket *so)
570 {
571 	struct sctp_inpcb *inp;
572 	uint32_t flags;
573 
574 	inp = (struct sctp_inpcb *)so->so_pcb;
575 	if (inp == NULL)
576 		return;
577 
578 	/*
579 	 * Inform all the lower layer assoc that we are done.
580 	 */
581 sctp_must_try_again:
582 	flags = inp->sctp_flags;
583 #ifdef SCTP_LOG_CLOSING
584 	sctp_log_closing(inp, NULL, 17);
585 #endif
586 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
587 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
588 		if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
589 		    (so->so_rcv.sb_cc > 0)) {
590 #ifdef SCTP_LOG_CLOSING
591 			sctp_log_closing(inp, NULL, 13);
592 #endif
593 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
594 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
595 		} else {
596 #ifdef SCTP_LOG_CLOSING
597 			sctp_log_closing(inp, NULL, 14);
598 #endif
599 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
600 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
601 		}
602 		/*
603 		 * The socket is now detached, no matter what the state of
604 		 * the SCTP association.
605 		 */
606 		SOCK_LOCK(so);
607 		SCTP_SB_CLEAR(so->so_snd);
608 		/*
609 		 * same for the rcv ones, they are only here for the
610 		 * accounting/select.
611 		 */
612 		SCTP_SB_CLEAR(so->so_rcv);
613 
614 		/* Now null out the reference, we are completely detached. */
615 		so->so_pcb = NULL;
616 		SOCK_UNLOCK(so);
617 	} else {
618 		flags = inp->sctp_flags;
619 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
620 			goto sctp_must_try_again;
621 		}
622 	}
623 	return;
624 }
625 
626 
627 int
628 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
629     struct mbuf *control, struct thread *p);
630 
631 
632 int
633 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
634     struct mbuf *control, struct thread *p)
635 {
636 	struct sctp_inpcb *inp;
637 	int error;
638 
639 	inp = (struct sctp_inpcb *)so->so_pcb;
640 	if (inp == NULL) {
641 		if (control) {
642 			sctp_m_freem(control);
643 			control = NULL;
644 		}
645 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
646 		sctp_m_freem(m);
647 		return (EINVAL);
648 	}
649 	/* Got to have an to address if we are NOT a connected socket */
650 	if ((addr == NULL) &&
651 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
652 	    (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))) {
653 		goto connected_type;
654 	} else if (addr == NULL) {
655 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
656 		error = EDESTADDRREQ;
657 		sctp_m_freem(m);
658 		if (control) {
659 			sctp_m_freem(control);
660 			control = NULL;
661 		}
662 		return (error);
663 	}
664 #ifdef INET6
665 	if (addr->sa_family != AF_INET) {
666 		/* must be a v4 address! */
667 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
668 		sctp_m_freem(m);
669 		if (control) {
670 			sctp_m_freem(control);
671 			control = NULL;
672 		}
673 		error = EDESTADDRREQ;
674 		return (error);
675 	}
676 #endif				/* INET6 */
677 connected_type:
678 	/* now what about control */
679 	if (control) {
680 		if (inp->control) {
681 			SCTP_PRINTF("huh? control set?\n");
682 			sctp_m_freem(inp->control);
683 			inp->control = NULL;
684 		}
685 		inp->control = control;
686 	}
687 	/* Place the data */
688 	if (inp->pkt) {
689 		SCTP_BUF_NEXT(inp->pkt_last) = m;
690 		inp->pkt_last = m;
691 	} else {
692 		inp->pkt_last = inp->pkt = m;
693 	}
694 	if (
695 	/* FreeBSD uses a flag passed */
696 	    ((flags & PRUS_MORETOCOME) == 0)
697 	    ) {
698 		/*
699 		 * note with the current version this code will only be used
700 		 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
701 		 * re-defining sosend to use the sctp_sosend. One can
702 		 * optionally switch back to this code (by changing back the
703 		 * definitions) but this is not advisable. This code is used
704 		 * by FreeBSD when sending a file with sendfile() though.
705 		 */
706 		int ret;
707 
708 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
709 		inp->pkt = NULL;
710 		inp->control = NULL;
711 		return (ret);
712 	} else {
713 		return (0);
714 	}
715 }
716 
717 int
718 sctp_disconnect(struct socket *so)
719 {
720 	struct sctp_inpcb *inp;
721 
722 	inp = (struct sctp_inpcb *)so->so_pcb;
723 	if (inp == NULL) {
724 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
725 		return (ENOTCONN);
726 	}
727 	SCTP_INP_RLOCK(inp);
728 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
729 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
730 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
731 			/* No connection */
732 			SCTP_INP_RUNLOCK(inp);
733 			return (0);
734 		} else {
735 			struct sctp_association *asoc;
736 			struct sctp_tcb *stcb;
737 
738 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
739 			if (stcb == NULL) {
740 				SCTP_INP_RUNLOCK(inp);
741 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
742 				return (EINVAL);
743 			}
744 			SCTP_TCB_LOCK(stcb);
745 			asoc = &stcb->asoc;
746 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
747 				/* We are about to be freed, out of here */
748 				SCTP_TCB_UNLOCK(stcb);
749 				SCTP_INP_RUNLOCK(inp);
750 				return (0);
751 			}
752 			if (((so->so_options & SO_LINGER) &&
753 			    (so->so_linger == 0)) ||
754 			    (so->so_rcv.sb_cc > 0)) {
755 				if (SCTP_GET_STATE(asoc) !=
756 				    SCTP_STATE_COOKIE_WAIT) {
757 					/* Left with Data unread */
758 					struct mbuf *err;
759 
760 					err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
761 					if (err) {
762 						/*
763 						 * Fill in the user
764 						 * initiated abort
765 						 */
766 						struct sctp_paramhdr *ph;
767 
768 						ph = mtod(err, struct sctp_paramhdr *);
769 						SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
770 						ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
771 						ph->param_length = htons(SCTP_BUF_LEN(err));
772 					}
773 					sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
774 					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
775 				}
776 				SCTP_INP_RUNLOCK(inp);
777 				if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
778 				    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
779 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
780 				}
781 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
782 				/* No unlock tcb assoc is gone */
783 				return (0);
784 			}
785 			if (TAILQ_EMPTY(&asoc->send_queue) &&
786 			    TAILQ_EMPTY(&asoc->sent_queue) &&
787 			    (asoc->stream_queue_cnt == 0)) {
788 				/* there is nothing queued to send, so done */
789 				if (asoc->locked_on_sending) {
790 					goto abort_anyway;
791 				}
792 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
793 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
794 					/* only send SHUTDOWN 1st time thru */
795 					struct sctp_nets *netp;
796 
797 					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
798 					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
799 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
800 					}
801 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
802 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
803 					sctp_stop_timers_for_shutdown(stcb);
804 					if (stcb->asoc.alternate) {
805 						netp = stcb->asoc.alternate;
806 					} else {
807 						netp = stcb->asoc.primary_destination;
808 					}
809 					sctp_send_shutdown(stcb, netp);
810 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
811 					    stcb->sctp_ep, stcb, netp);
812 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
813 					    stcb->sctp_ep, stcb, netp);
814 					sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
815 				}
816 			} else {
817 				/*
818 				 * we still got (or just got) data to send,
819 				 * so set SHUTDOWN_PENDING
820 				 */
821 				/*
822 				 * XXX sockets draft says that SCTP_EOF
823 				 * should be sent with no data. currently,
824 				 * we will allow user data to be sent first
825 				 * and move to SHUTDOWN-PENDING
826 				 */
827 				struct sctp_nets *netp;
828 
829 				if (stcb->asoc.alternate) {
830 					netp = stcb->asoc.alternate;
831 				} else {
832 					netp = stcb->asoc.primary_destination;
833 				}
834 
835 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
836 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
837 				    netp);
838 				if (asoc->locked_on_sending) {
839 					/* Locked to send out the data */
840 					struct sctp_stream_queue_pending *sp;
841 
842 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
843 					if (sp == NULL) {
844 						SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
845 						    asoc->locked_on_sending->stream_no);
846 					} else {
847 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
848 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
849 					}
850 				}
851 				if (TAILQ_EMPTY(&asoc->send_queue) &&
852 				    TAILQ_EMPTY(&asoc->sent_queue) &&
853 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
854 					struct mbuf *op_err;
855 
856 			abort_anyway:
857 					op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
858 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
859 					sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
860 					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
861 					if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
862 					    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
863 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
864 					}
865 					SCTP_INP_RUNLOCK(inp);
866 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
867 					return (0);
868 				} else {
869 					sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
870 				}
871 			}
872 			soisdisconnecting(so);
873 			SCTP_TCB_UNLOCK(stcb);
874 			SCTP_INP_RUNLOCK(inp);
875 			return (0);
876 		}
877 		/* not reached */
878 	} else {
879 		/* UDP model does not support this */
880 		SCTP_INP_RUNLOCK(inp);
881 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
882 		return (EOPNOTSUPP);
883 	}
884 }
885 
886 int
887 sctp_flush(struct socket *so, int how)
888 {
889 	/*
890 	 * We will just clear out the values and let subsequent close clear
891 	 * out the data, if any. Note if the user did a shutdown(SHUT_RD)
892 	 * they will not be able to read the data, the socket will block
893 	 * that from happening.
894 	 */
895 	struct sctp_inpcb *inp;
896 
897 	inp = (struct sctp_inpcb *)so->so_pcb;
898 	if (inp == NULL) {
899 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
900 		return (EINVAL);
901 	}
902 	SCTP_INP_RLOCK(inp);
903 	/* For the 1 to many model this does nothing */
904 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
905 		SCTP_INP_RUNLOCK(inp);
906 		return (0);
907 	}
908 	SCTP_INP_RUNLOCK(inp);
909 	if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
910 		/*
911 		 * First make sure the sb will be happy, we don't use these
912 		 * except maybe the count
913 		 */
914 		SCTP_INP_WLOCK(inp);
915 		SCTP_INP_READ_LOCK(inp);
916 		inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_CANT_READ;
917 		SCTP_INP_READ_UNLOCK(inp);
918 		SCTP_INP_WUNLOCK(inp);
919 		so->so_rcv.sb_cc = 0;
920 		so->so_rcv.sb_mbcnt = 0;
921 		so->so_rcv.sb_mb = NULL;
922 	}
923 	if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
924 		/*
925 		 * First make sure the sb will be happy, we don't use these
926 		 * except maybe the count
927 		 */
928 		so->so_snd.sb_cc = 0;
929 		so->so_snd.sb_mbcnt = 0;
930 		so->so_snd.sb_mb = NULL;
931 
932 	}
933 	return (0);
934 }
935 
936 int
937 sctp_shutdown(struct socket *so)
938 {
939 	struct sctp_inpcb *inp;
940 
941 	inp = (struct sctp_inpcb *)so->so_pcb;
942 	if (inp == NULL) {
943 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
944 		return (EINVAL);
945 	}
946 	SCTP_INP_RLOCK(inp);
947 	/* For UDP model this is a invalid call */
948 	if (!((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
949 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
950 		/* Restore the flags that the soshutdown took away. */
951 		SOCKBUF_LOCK(&so->so_rcv);
952 		so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
953 		SOCKBUF_UNLOCK(&so->so_rcv);
954 		/* This proc will wakeup for read and do nothing (I hope) */
955 		SCTP_INP_RUNLOCK(inp);
956 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
957 		return (EOPNOTSUPP);
958 	}
959 	/*
960 	 * Ok if we reach here its the TCP model and it is either a SHUT_WR
961 	 * or SHUT_RDWR. This means we put the shutdown flag against it.
962 	 */
963 	{
964 		struct sctp_tcb *stcb;
965 		struct sctp_association *asoc;
966 
967 		if ((so->so_state &
968 		    (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
969 			SCTP_INP_RUNLOCK(inp);
970 			return (ENOTCONN);
971 		}
972 		socantsendmore(so);
973 
974 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
975 		if (stcb == NULL) {
976 			/*
977 			 * Ok we hit the case that the shutdown call was
978 			 * made after an abort or something. Nothing to do
979 			 * now.
980 			 */
981 			SCTP_INP_RUNLOCK(inp);
982 			return (0);
983 		}
984 		SCTP_TCB_LOCK(stcb);
985 		asoc = &stcb->asoc;
986 		if (TAILQ_EMPTY(&asoc->send_queue) &&
987 		    TAILQ_EMPTY(&asoc->sent_queue) &&
988 		    (asoc->stream_queue_cnt == 0)) {
989 			if (asoc->locked_on_sending) {
990 				goto abort_anyway;
991 			}
992 			/* there is nothing queued to send, so I'm done... */
993 			if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
994 				/* only send SHUTDOWN the first time through */
995 				struct sctp_nets *netp;
996 
997 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
998 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
999 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1000 				}
1001 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1002 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1003 				sctp_stop_timers_for_shutdown(stcb);
1004 				if (stcb->asoc.alternate) {
1005 					netp = stcb->asoc.alternate;
1006 				} else {
1007 					netp = stcb->asoc.primary_destination;
1008 				}
1009 				sctp_send_shutdown(stcb, netp);
1010 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1011 				    stcb->sctp_ep, stcb, netp);
1012 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1013 				    stcb->sctp_ep, stcb, netp);
1014 				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
1015 			}
1016 		} else {
1017 			/*
1018 			 * we still got (or just got) data to send, so set
1019 			 * SHUTDOWN_PENDING
1020 			 */
1021 			struct sctp_nets *netp;
1022 
1023 			if (stcb->asoc.alternate) {
1024 				netp = stcb->asoc.alternate;
1025 			} else {
1026 				netp = stcb->asoc.primary_destination;
1027 			}
1028 
1029 			asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1030 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
1031 			    netp);
1032 
1033 			if (asoc->locked_on_sending) {
1034 				/* Locked to send out the data */
1035 				struct sctp_stream_queue_pending *sp;
1036 
1037 				sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1038 				if (sp == NULL) {
1039 					SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1040 					    asoc->locked_on_sending->stream_no);
1041 				} else {
1042 					if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1043 						asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1044 					}
1045 				}
1046 			}
1047 			if (TAILQ_EMPTY(&asoc->send_queue) &&
1048 			    TAILQ_EMPTY(&asoc->sent_queue) &&
1049 			    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1050 				struct mbuf *op_err;
1051 
1052 		abort_anyway:
1053 				op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
1054 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1055 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1056 				    op_err, SCTP_SO_LOCKED);
1057 				goto skip_unlock;
1058 			} else {
1059 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1060 			}
1061 		}
1062 		SCTP_TCB_UNLOCK(stcb);
1063 	}
1064 skip_unlock:
1065 	SCTP_INP_RUNLOCK(inp);
1066 	return (0);
1067 }
1068 
1069 /*
1070  * copies a "user" presentable address and removes embedded scope, etc.
1071  * returns 0 on success, 1 on error
1072  */
1073 static uint32_t
1074 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1075 {
1076 #ifdef INET6
1077 	struct sockaddr_in6 lsa6;
1078 
1079 	sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1080 	    &lsa6);
1081 #endif
1082 	memcpy(ss, sa, sa->sa_len);
1083 	return (0);
1084 }
1085 
1086 
1087 
1088 /*
1089  * NOTE: assumes addr lock is held
1090  */
1091 static size_t
1092 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1093     struct sctp_tcb *stcb,
1094     size_t limit,
1095     struct sockaddr_storage *sas,
1096     uint32_t vrf_id)
1097 {
1098 	struct sctp_ifn *sctp_ifn;
1099 	struct sctp_ifa *sctp_ifa;
1100 	size_t actual;
1101 	int loopback_scope;
1102 
1103 #if defined(INET)
1104 	int ipv4_local_scope, ipv4_addr_legal;
1105 
1106 #endif
1107 #if defined(INET6)
1108 	int local_scope, site_scope, ipv6_addr_legal;
1109 
1110 #endif
1111 	struct sctp_vrf *vrf;
1112 
1113 	actual = 0;
1114 	if (limit <= 0)
1115 		return (actual);
1116 
1117 	if (stcb) {
1118 		/* Turn on all the appropriate scope */
1119 		loopback_scope = stcb->asoc.scope.loopback_scope;
1120 #if defined(INET)
1121 		ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
1122 		ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
1123 #endif
1124 #if defined(INET6)
1125 		local_scope = stcb->asoc.scope.local_scope;
1126 		site_scope = stcb->asoc.scope.site_scope;
1127 		ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
1128 #endif
1129 	} else {
1130 		/* Use generic values for endpoints. */
1131 		loopback_scope = 1;
1132 #if defined(INET)
1133 		ipv4_local_scope = 1;
1134 #endif
1135 #if defined(INET6)
1136 		local_scope = 1;
1137 		site_scope = 1;
1138 #endif
1139 		if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1140 #if defined(INET6)
1141 			ipv6_addr_legal = 1;
1142 #endif
1143 #if defined(INET)
1144 			if (SCTP_IPV6_V6ONLY(inp)) {
1145 				ipv4_addr_legal = 0;
1146 			} else {
1147 				ipv4_addr_legal = 1;
1148 			}
1149 #endif
1150 		} else {
1151 #if defined(INET6)
1152 			ipv6_addr_legal = 0;
1153 #endif
1154 #if defined(INET)
1155 			ipv4_addr_legal = 1;
1156 #endif
1157 		}
1158 	}
1159 	vrf = sctp_find_vrf(vrf_id);
1160 	if (vrf == NULL) {
1161 		return (0);
1162 	}
1163 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1164 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1165 			if ((loopback_scope == 0) &&
1166 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1167 				/* Skip loopback if loopback_scope not set */
1168 				continue;
1169 			}
1170 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1171 				if (stcb) {
1172 					/*
1173 					 * For the BOUND-ALL case, the list
1174 					 * associated with a TCB is Always
1175 					 * considered a reverse list.. i.e.
1176 					 * it lists addresses that are NOT
1177 					 * part of the association. If this
1178 					 * is one of those we must skip it.
1179 					 */
1180 					if (sctp_is_addr_restricted(stcb,
1181 					    sctp_ifa)) {
1182 						continue;
1183 					}
1184 				}
1185 				switch (sctp_ifa->address.sa.sa_family) {
1186 #ifdef INET
1187 				case AF_INET:
1188 					if (ipv4_addr_legal) {
1189 						struct sockaddr_in *sin;
1190 
1191 						sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1192 						if (sin->sin_addr.s_addr == 0) {
1193 							/*
1194 							 * we skip
1195 							 * unspecifed
1196 							 * addresses
1197 							 */
1198 							continue;
1199 						}
1200 						if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1201 						    &sin->sin_addr) != 0) {
1202 							continue;
1203 						}
1204 						if ((ipv4_local_scope == 0) &&
1205 						    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1206 							continue;
1207 						}
1208 #ifdef INET6
1209 						if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1210 							in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1211 							((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1212 							sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1213 							actual += sizeof(struct sockaddr_in6);
1214 						} else {
1215 #endif
1216 							memcpy(sas, sin, sizeof(*sin));
1217 							((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1218 							sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1219 							actual += sizeof(*sin);
1220 #ifdef INET6
1221 						}
1222 #endif
1223 						if (actual >= limit) {
1224 							return (actual);
1225 						}
1226 					} else {
1227 						continue;
1228 					}
1229 					break;
1230 #endif
1231 #ifdef INET6
1232 				case AF_INET6:
1233 					if (ipv6_addr_legal) {
1234 						struct sockaddr_in6 *sin6;
1235 
1236 						sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1237 						if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1238 							/*
1239 							 * we skip
1240 							 * unspecifed
1241 							 * addresses
1242 							 */
1243 							continue;
1244 						}
1245 						if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1246 						    &sin6->sin6_addr) != 0) {
1247 							continue;
1248 						}
1249 						if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1250 							if (local_scope == 0)
1251 								continue;
1252 							if (sin6->sin6_scope_id == 0) {
1253 								if (sa6_recoverscope(sin6) != 0)
1254 									/*
1255 									 *
1256 									 * bad
1257 									 *
1258 									 * li
1259 									 * nk
1260 									 *
1261 									 * loc
1262 									 * al
1263 									 *
1264 									 * add
1265 									 * re
1266 									 * ss
1267 									 * */
1268 									continue;
1269 							}
1270 						}
1271 						if ((site_scope == 0) &&
1272 						    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1273 							continue;
1274 						}
1275 						memcpy(sas, sin6, sizeof(*sin6));
1276 						((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1277 						sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1278 						actual += sizeof(*sin6);
1279 						if (actual >= limit) {
1280 							return (actual);
1281 						}
1282 					} else {
1283 						continue;
1284 					}
1285 					break;
1286 #endif
1287 				default:
1288 					/* TSNH */
1289 					break;
1290 				}
1291 			}
1292 		}
1293 	} else {
1294 		struct sctp_laddr *laddr;
1295 
1296 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1297 			if (stcb) {
1298 				if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1299 					continue;
1300 				}
1301 			}
1302 			if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1303 				continue;
1304 			switch (laddr->ifa->address.sa.sa_family) {
1305 #ifdef INET
1306 			case AF_INET:
1307 				((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1308 				break;
1309 #endif
1310 #ifdef INET6
1311 			case AF_INET6:
1312 				((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1313 				break;
1314 #endif
1315 			default:
1316 				/* TSNH */
1317 				break;
1318 			}
1319 			sas = (struct sockaddr_storage *)((caddr_t)sas +
1320 			    laddr->ifa->address.sa.sa_len);
1321 			actual += laddr->ifa->address.sa.sa_len;
1322 			if (actual >= limit) {
1323 				return (actual);
1324 			}
1325 		}
1326 	}
1327 	return (actual);
1328 }
1329 
1330 static size_t
1331 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1332     struct sctp_tcb *stcb,
1333     size_t limit,
1334     struct sockaddr_storage *sas)
1335 {
1336 	size_t size = 0;
1337 
1338 	SCTP_IPI_ADDR_RLOCK();
1339 	/* fill up addresses for the endpoint's default vrf */
1340 	size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1341 	    inp->def_vrf_id);
1342 	SCTP_IPI_ADDR_RUNLOCK();
1343 	return (size);
1344 }
1345 
1346 /*
1347  * NOTE: assumes addr lock is held
1348  */
1349 static int
1350 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1351 {
1352 	int cnt = 0;
1353 	struct sctp_vrf *vrf = NULL;
1354 
1355 	/*
1356 	 * In both sub-set bound an bound_all cases we return the MAXIMUM
1357 	 * number of addresses that you COULD get. In reality the sub-set
1358 	 * bound may have an exclusion list for a given TCB OR in the
1359 	 * bound-all case a TCB may NOT include the loopback or other
1360 	 * addresses as well.
1361 	 */
1362 	vrf = sctp_find_vrf(vrf_id);
1363 	if (vrf == NULL) {
1364 		return (0);
1365 	}
1366 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1367 		struct sctp_ifn *sctp_ifn;
1368 		struct sctp_ifa *sctp_ifa;
1369 
1370 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1371 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1372 				/* Count them if they are the right type */
1373 				switch (sctp_ifa->address.sa.sa_family) {
1374 #ifdef INET
1375 				case AF_INET:
1376 					if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1377 						cnt += sizeof(struct sockaddr_in6);
1378 					else
1379 						cnt += sizeof(struct sockaddr_in);
1380 					break;
1381 #endif
1382 #ifdef INET6
1383 				case AF_INET6:
1384 					cnt += sizeof(struct sockaddr_in6);
1385 					break;
1386 #endif
1387 				default:
1388 					break;
1389 				}
1390 			}
1391 		}
1392 	} else {
1393 		struct sctp_laddr *laddr;
1394 
1395 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1396 			switch (laddr->ifa->address.sa.sa_family) {
1397 #ifdef INET
1398 			case AF_INET:
1399 				if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1400 					cnt += sizeof(struct sockaddr_in6);
1401 				else
1402 					cnt += sizeof(struct sockaddr_in);
1403 				break;
1404 #endif
1405 #ifdef INET6
1406 			case AF_INET6:
1407 				cnt += sizeof(struct sockaddr_in6);
1408 				break;
1409 #endif
1410 			default:
1411 				break;
1412 			}
1413 		}
1414 	}
1415 	return (cnt);
1416 }
1417 
1418 static int
1419 sctp_count_max_addresses(struct sctp_inpcb *inp)
1420 {
1421 	int cnt = 0;
1422 
1423 	SCTP_IPI_ADDR_RLOCK();
1424 	/* count addresses for the endpoint's default VRF */
1425 	cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1426 	SCTP_IPI_ADDR_RUNLOCK();
1427 	return (cnt);
1428 }
1429 
1430 static int
1431 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1432     size_t optsize, void *p, int delay)
1433 {
1434 	int error = 0;
1435 	int creat_lock_on = 0;
1436 	struct sctp_tcb *stcb = NULL;
1437 	struct sockaddr *sa;
1438 	int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1439 	uint32_t vrf_id;
1440 	int bad_addresses = 0;
1441 	sctp_assoc_t *a_id;
1442 
1443 	SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1444 
1445 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1446 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1447 		/* We are already connected AND the TCP model */
1448 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1449 		return (EADDRINUSE);
1450 	}
1451 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
1452 	    (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1453 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1454 		return (EINVAL);
1455 	}
1456 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1457 		SCTP_INP_RLOCK(inp);
1458 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1459 		SCTP_INP_RUNLOCK(inp);
1460 	}
1461 	if (stcb) {
1462 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1463 		return (EALREADY);
1464 	}
1465 	SCTP_INP_INCR_REF(inp);
1466 	SCTP_ASOC_CREATE_LOCK(inp);
1467 	creat_lock_on = 1;
1468 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1469 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1470 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1471 		error = EFAULT;
1472 		goto out_now;
1473 	}
1474 	totaddrp = (int *)optval;
1475 	totaddr = *totaddrp;
1476 	sa = (struct sockaddr *)(totaddrp + 1);
1477 	stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1478 	if ((stcb != NULL) || bad_addresses) {
1479 		/* Already have or am bring up an association */
1480 		SCTP_ASOC_CREATE_UNLOCK(inp);
1481 		creat_lock_on = 0;
1482 		if (stcb)
1483 			SCTP_TCB_UNLOCK(stcb);
1484 		if (bad_addresses == 0) {
1485 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1486 			error = EALREADY;
1487 		}
1488 		goto out_now;
1489 	}
1490 #ifdef INET6
1491 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1492 	    (num_v6 > 0)) {
1493 		error = EINVAL;
1494 		goto out_now;
1495 	}
1496 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1497 	    (num_v4 > 0)) {
1498 		struct in6pcb *inp6;
1499 
1500 		inp6 = (struct in6pcb *)inp;
1501 		if (SCTP_IPV6_V6ONLY(inp6)) {
1502 			/*
1503 			 * if IPV6_V6ONLY flag, ignore connections destined
1504 			 * to a v4 addr or v4-mapped addr
1505 			 */
1506 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1507 			error = EINVAL;
1508 			goto out_now;
1509 		}
1510 	}
1511 #endif				/* INET6 */
1512 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1513 	    SCTP_PCB_FLAGS_UNBOUND) {
1514 		/* Bind a ephemeral port */
1515 		error = sctp_inpcb_bind(so, NULL, NULL, p);
1516 		if (error) {
1517 			goto out_now;
1518 		}
1519 	}
1520 	/* FIX ME: do we want to pass in a vrf on the connect call? */
1521 	vrf_id = inp->def_vrf_id;
1522 
1523 
1524 	/* We are GOOD to go */
1525 	stcb = sctp_aloc_assoc(inp, sa, &error, 0, vrf_id,
1526 	    (struct thread *)p
1527 	    );
1528 	if (stcb == NULL) {
1529 		/* Gak! no memory */
1530 		goto out_now;
1531 	}
1532 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1533 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1534 		/* Set the connected flag so we can queue data */
1535 		soisconnecting(so);
1536 	}
1537 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1538 	/* move to second address */
1539 	switch (sa->sa_family) {
1540 #ifdef INET
1541 	case AF_INET:
1542 		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1543 		break;
1544 #endif
1545 #ifdef INET6
1546 	case AF_INET6:
1547 		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1548 		break;
1549 #endif
1550 	default:
1551 		break;
1552 	}
1553 
1554 	error = 0;
1555 	sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1556 	/* Fill in the return id */
1557 	if (error) {
1558 		(void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1559 		goto out_now;
1560 	}
1561 	a_id = (sctp_assoc_t *) optval;
1562 	*a_id = sctp_get_associd(stcb);
1563 
1564 	/* initialize authentication parameters for the assoc */
1565 	sctp_initialize_auth_params(inp, stcb);
1566 
1567 	if (delay) {
1568 		/* doing delayed connection */
1569 		stcb->asoc.delayed_connection = 1;
1570 		sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1571 	} else {
1572 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1573 		sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1574 	}
1575 	SCTP_TCB_UNLOCK(stcb);
1576 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1577 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1578 		/* Set the connected flag so we can queue data */
1579 		soisconnecting(so);
1580 	}
1581 out_now:
1582 	if (creat_lock_on) {
1583 		SCTP_ASOC_CREATE_UNLOCK(inp);
1584 	}
1585 	SCTP_INP_DECR_REF(inp);
1586 	return (error);
1587 }
1588 
1589 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1590 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1591 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1592 		SCTP_INP_RLOCK(inp); \
1593 		stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1594 		if (stcb) { \
1595 			SCTP_TCB_LOCK(stcb); \
1596                 } \
1597 		SCTP_INP_RUNLOCK(inp); \
1598 	} else if (assoc_id > SCTP_ALL_ASSOC) { \
1599 		stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1600 		if (stcb == NULL) { \
1601 		        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1602 			error = ENOENT; \
1603 			break; \
1604 		} \
1605 	} else { \
1606 		stcb = NULL; \
1607         } \
1608   }
1609 
1610 
1611 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1612 	if (size < sizeof(type)) { \
1613 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1614 		error = EINVAL; \
1615 		break; \
1616 	} else { \
1617 		destp = (type *)srcp; \
1618 	} \
1619       }
1620 
1621 static int
1622 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1623     void *p)
1624 {
1625 	struct sctp_inpcb *inp = NULL;
1626 	int error, val = 0;
1627 	struct sctp_tcb *stcb = NULL;
1628 
1629 	if (optval == NULL) {
1630 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1631 		return (EINVAL);
1632 	}
1633 	inp = (struct sctp_inpcb *)so->so_pcb;
1634 	if (inp == NULL) {
1635 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1636 		return EINVAL;
1637 	}
1638 	error = 0;
1639 
1640 	switch (optname) {
1641 	case SCTP_NODELAY:
1642 	case SCTP_AUTOCLOSE:
1643 	case SCTP_EXPLICIT_EOR:
1644 	case SCTP_AUTO_ASCONF:
1645 	case SCTP_DISABLE_FRAGMENTS:
1646 	case SCTP_I_WANT_MAPPED_V4_ADDR:
1647 	case SCTP_USE_EXT_RCVINFO:
1648 		SCTP_INP_RLOCK(inp);
1649 		switch (optname) {
1650 		case SCTP_DISABLE_FRAGMENTS:
1651 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1652 			break;
1653 		case SCTP_I_WANT_MAPPED_V4_ADDR:
1654 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1655 			break;
1656 		case SCTP_AUTO_ASCONF:
1657 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1658 				/* only valid for bound all sockets */
1659 				val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1660 			} else {
1661 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1662 				error = EINVAL;
1663 				goto flags_out;
1664 			}
1665 			break;
1666 		case SCTP_EXPLICIT_EOR:
1667 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1668 			break;
1669 		case SCTP_NODELAY:
1670 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1671 			break;
1672 		case SCTP_USE_EXT_RCVINFO:
1673 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1674 			break;
1675 		case SCTP_AUTOCLOSE:
1676 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1677 				val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1678 			else
1679 				val = 0;
1680 			break;
1681 
1682 		default:
1683 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1684 			error = ENOPROTOOPT;
1685 		}		/* end switch (sopt->sopt_name) */
1686 		if (*optsize < sizeof(val)) {
1687 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1688 			error = EINVAL;
1689 		}
1690 flags_out:
1691 		SCTP_INP_RUNLOCK(inp);
1692 		if (error == 0) {
1693 			/* return the option value */
1694 			*(int *)optval = val;
1695 			*optsize = sizeof(val);
1696 		}
1697 		break;
1698 	case SCTP_GET_PACKET_LOG:
1699 		{
1700 #ifdef  SCTP_PACKET_LOGGING
1701 			uint8_t *target;
1702 			int ret;
1703 
1704 			SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1705 			ret = sctp_copy_out_packet_log(target, (int)*optsize);
1706 			*optsize = ret;
1707 #else
1708 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1709 			error = EOPNOTSUPP;
1710 #endif
1711 			break;
1712 		}
1713 	case SCTP_REUSE_PORT:
1714 		{
1715 			uint32_t *value;
1716 
1717 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1718 				/* Can't do this for a 1-m socket */
1719 				error = EINVAL;
1720 				break;
1721 			}
1722 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1723 			*value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1724 			*optsize = sizeof(uint32_t);
1725 			break;
1726 		}
1727 	case SCTP_PARTIAL_DELIVERY_POINT:
1728 		{
1729 			uint32_t *value;
1730 
1731 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1732 			*value = inp->partial_delivery_point;
1733 			*optsize = sizeof(uint32_t);
1734 			break;
1735 		}
1736 	case SCTP_FRAGMENT_INTERLEAVE:
1737 		{
1738 			uint32_t *value;
1739 
1740 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1741 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1742 				if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1743 					*value = SCTP_FRAG_LEVEL_2;
1744 				} else {
1745 					*value = SCTP_FRAG_LEVEL_1;
1746 				}
1747 			} else {
1748 				*value = SCTP_FRAG_LEVEL_0;
1749 			}
1750 			*optsize = sizeof(uint32_t);
1751 			break;
1752 		}
1753 	case SCTP_CMT_ON_OFF:
1754 		{
1755 			struct sctp_assoc_value *av;
1756 
1757 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1758 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1759 			if (stcb) {
1760 				av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1761 				SCTP_TCB_UNLOCK(stcb);
1762 			} else {
1763 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1764 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1765 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1766 					SCTP_INP_RLOCK(inp);
1767 					av->assoc_value = inp->sctp_cmt_on_off;
1768 					SCTP_INP_RUNLOCK(inp);
1769 				} else {
1770 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1771 					error = EINVAL;
1772 				}
1773 			}
1774 			if (error == 0) {
1775 				*optsize = sizeof(struct sctp_assoc_value);
1776 			}
1777 			break;
1778 		}
1779 	case SCTP_PLUGGABLE_CC:
1780 		{
1781 			struct sctp_assoc_value *av;
1782 
1783 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1784 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1785 			if (stcb) {
1786 				av->assoc_value = stcb->asoc.congestion_control_module;
1787 				SCTP_TCB_UNLOCK(stcb);
1788 			} else {
1789 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1790 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1791 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1792 					SCTP_INP_RLOCK(inp);
1793 					av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1794 					SCTP_INP_RUNLOCK(inp);
1795 				} else {
1796 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1797 					error = EINVAL;
1798 				}
1799 			}
1800 			if (error == 0) {
1801 				*optsize = sizeof(struct sctp_assoc_value);
1802 			}
1803 			break;
1804 		}
1805 	case SCTP_CC_OPTION:
1806 		{
1807 			struct sctp_cc_option *cc_opt;
1808 
1809 			SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, *optsize);
1810 			SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
1811 			if (stcb == NULL) {
1812 				error = EINVAL;
1813 			} else {
1814 				if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
1815 					error = ENOTSUP;
1816 				} else {
1817 					error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 0, cc_opt);
1818 					*optsize = sizeof(struct sctp_cc_option);
1819 				}
1820 				SCTP_TCB_UNLOCK(stcb);
1821 			}
1822 			break;
1823 		}
1824 	case SCTP_PLUGGABLE_SS:
1825 		{
1826 			struct sctp_assoc_value *av;
1827 
1828 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1829 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1830 			if (stcb) {
1831 				av->assoc_value = stcb->asoc.stream_scheduling_module;
1832 				SCTP_TCB_UNLOCK(stcb);
1833 			} else {
1834 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1835 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1836 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1837 					SCTP_INP_RLOCK(inp);
1838 					av->assoc_value = inp->sctp_ep.sctp_default_ss_module;
1839 					SCTP_INP_RUNLOCK(inp);
1840 				} else {
1841 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1842 					error = EINVAL;
1843 				}
1844 			}
1845 			if (error == 0) {
1846 				*optsize = sizeof(struct sctp_assoc_value);
1847 			}
1848 			break;
1849 		}
1850 	case SCTP_SS_VALUE:
1851 		{
1852 			struct sctp_stream_value *av;
1853 
1854 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, *optsize);
1855 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1856 			if (stcb) {
1857 				if (stcb->asoc.ss_functions.sctp_ss_get_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
1858 				    &av->stream_value) < 0) {
1859 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1860 					error = EINVAL;
1861 				} else {
1862 					*optsize = sizeof(struct sctp_stream_value);
1863 				}
1864 				SCTP_TCB_UNLOCK(stcb);
1865 			} else {
1866 				/*
1867 				 * Can't get stream value without
1868 				 * association
1869 				 */
1870 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1871 				error = EINVAL;
1872 			}
1873 			break;
1874 		}
1875 	case SCTP_GET_ADDR_LEN:
1876 		{
1877 			struct sctp_assoc_value *av;
1878 
1879 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1880 			error = EINVAL;
1881 #ifdef INET
1882 			if (av->assoc_value == AF_INET) {
1883 				av->assoc_value = sizeof(struct sockaddr_in);
1884 				error = 0;
1885 			}
1886 #endif
1887 #ifdef INET6
1888 			if (av->assoc_value == AF_INET6) {
1889 				av->assoc_value = sizeof(struct sockaddr_in6);
1890 				error = 0;
1891 			}
1892 #endif
1893 			if (error) {
1894 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1895 			} else {
1896 				*optsize = sizeof(struct sctp_assoc_value);
1897 			}
1898 			break;
1899 		}
1900 	case SCTP_GET_ASSOC_NUMBER:
1901 		{
1902 			uint32_t *value, cnt;
1903 
1904 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1905 			cnt = 0;
1906 			SCTP_INP_RLOCK(inp);
1907 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1908 				cnt++;
1909 			}
1910 			SCTP_INP_RUNLOCK(inp);
1911 			*value = cnt;
1912 			*optsize = sizeof(uint32_t);
1913 			break;
1914 		}
1915 	case SCTP_GET_ASSOC_ID_LIST:
1916 		{
1917 			struct sctp_assoc_ids *ids;
1918 			unsigned int at, limit;
1919 
1920 			SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1921 			at = 0;
1922 			limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1923 			SCTP_INP_RLOCK(inp);
1924 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1925 				if (at < limit) {
1926 					ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1927 				} else {
1928 					error = EINVAL;
1929 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1930 					break;
1931 				}
1932 			}
1933 			SCTP_INP_RUNLOCK(inp);
1934 			if (error == 0) {
1935 				ids->gaids_number_of_ids = at;
1936 				*optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1937 			}
1938 			break;
1939 		}
1940 	case SCTP_CONTEXT:
1941 		{
1942 			struct sctp_assoc_value *av;
1943 
1944 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1945 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1946 
1947 			if (stcb) {
1948 				av->assoc_value = stcb->asoc.context;
1949 				SCTP_TCB_UNLOCK(stcb);
1950 			} else {
1951 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1952 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1953 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1954 					SCTP_INP_RLOCK(inp);
1955 					av->assoc_value = inp->sctp_context;
1956 					SCTP_INP_RUNLOCK(inp);
1957 				} else {
1958 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1959 					error = EINVAL;
1960 				}
1961 			}
1962 			if (error == 0) {
1963 				*optsize = sizeof(struct sctp_assoc_value);
1964 			}
1965 			break;
1966 		}
1967 	case SCTP_VRF_ID:
1968 		{
1969 			uint32_t *default_vrfid;
1970 
1971 			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1972 			*default_vrfid = inp->def_vrf_id;
1973 			*optsize = sizeof(uint32_t);
1974 			break;
1975 		}
1976 	case SCTP_GET_ASOC_VRF:
1977 		{
1978 			struct sctp_assoc_value *id;
1979 
1980 			SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1981 			SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1982 			if (stcb == NULL) {
1983 				error = EINVAL;
1984 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1985 			} else {
1986 				id->assoc_value = stcb->asoc.vrf_id;
1987 				*optsize = sizeof(struct sctp_assoc_value);
1988 			}
1989 			break;
1990 		}
1991 	case SCTP_GET_VRF_IDS:
1992 		{
1993 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1994 			error = EOPNOTSUPP;
1995 			break;
1996 		}
1997 	case SCTP_GET_NONCE_VALUES:
1998 		{
1999 			struct sctp_get_nonce_values *gnv;
2000 
2001 			SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
2002 			SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
2003 
2004 			if (stcb) {
2005 				gnv->gn_peers_tag = stcb->asoc.peer_vtag;
2006 				gnv->gn_local_tag = stcb->asoc.my_vtag;
2007 				SCTP_TCB_UNLOCK(stcb);
2008 				*optsize = sizeof(struct sctp_get_nonce_values);
2009 			} else {
2010 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2011 				error = ENOTCONN;
2012 			}
2013 			break;
2014 		}
2015 	case SCTP_DELAYED_SACK:
2016 		{
2017 			struct sctp_sack_info *sack;
2018 
2019 			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
2020 			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2021 			if (stcb) {
2022 				sack->sack_delay = stcb->asoc.delayed_ack;
2023 				sack->sack_freq = stcb->asoc.sack_freq;
2024 				SCTP_TCB_UNLOCK(stcb);
2025 			} else {
2026 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2027 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2028 				    (sack->sack_assoc_id == SCTP_FUTURE_ASSOC)) {
2029 					SCTP_INP_RLOCK(inp);
2030 					sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
2031 					sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
2032 					SCTP_INP_RUNLOCK(inp);
2033 				} else {
2034 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2035 					error = EINVAL;
2036 				}
2037 			}
2038 			if (error == 0) {
2039 				*optsize = sizeof(struct sctp_sack_info);
2040 			}
2041 			break;
2042 		}
2043 	case SCTP_GET_SNDBUF_USE:
2044 		{
2045 			struct sctp_sockstat *ss;
2046 
2047 			SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
2048 			SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
2049 
2050 			if (stcb) {
2051 				ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
2052 				ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
2053 				    stcb->asoc.size_on_all_streams);
2054 				SCTP_TCB_UNLOCK(stcb);
2055 				*optsize = sizeof(struct sctp_sockstat);
2056 			} else {
2057 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2058 				error = ENOTCONN;
2059 			}
2060 			break;
2061 		}
2062 	case SCTP_MAX_BURST:
2063 		{
2064 			struct sctp_assoc_value *av;
2065 
2066 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2067 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2068 
2069 			if (stcb) {
2070 				av->assoc_value = stcb->asoc.max_burst;
2071 				SCTP_TCB_UNLOCK(stcb);
2072 			} else {
2073 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2074 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2075 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2076 					SCTP_INP_RLOCK(inp);
2077 					av->assoc_value = inp->sctp_ep.max_burst;
2078 					SCTP_INP_RUNLOCK(inp);
2079 				} else {
2080 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2081 					error = EINVAL;
2082 				}
2083 			}
2084 			if (error == 0) {
2085 				*optsize = sizeof(struct sctp_assoc_value);
2086 			}
2087 			break;
2088 		}
2089 	case SCTP_MAXSEG:
2090 		{
2091 			struct sctp_assoc_value *av;
2092 			int ovh;
2093 
2094 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2095 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2096 
2097 			if (stcb) {
2098 				av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
2099 				SCTP_TCB_UNLOCK(stcb);
2100 			} else {
2101 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2102 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2103 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2104 					SCTP_INP_RLOCK(inp);
2105 					if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2106 						ovh = SCTP_MED_OVERHEAD;
2107 					} else {
2108 						ovh = SCTP_MED_V4_OVERHEAD;
2109 					}
2110 					if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
2111 						av->assoc_value = 0;
2112 					else
2113 						av->assoc_value = inp->sctp_frag_point - ovh;
2114 					SCTP_INP_RUNLOCK(inp);
2115 				} else {
2116 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2117 					error = EINVAL;
2118 				}
2119 			}
2120 			if (error == 0) {
2121 				*optsize = sizeof(struct sctp_assoc_value);
2122 			}
2123 			break;
2124 		}
2125 	case SCTP_GET_STAT_LOG:
2126 		error = sctp_fill_stat_log(optval, optsize);
2127 		break;
2128 	case SCTP_EVENTS:
2129 		{
2130 			struct sctp_event_subscribe *events;
2131 
2132 			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
2133 			memset(events, 0, sizeof(struct sctp_event_subscribe));
2134 			SCTP_INP_RLOCK(inp);
2135 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
2136 				events->sctp_data_io_event = 1;
2137 
2138 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
2139 				events->sctp_association_event = 1;
2140 
2141 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
2142 				events->sctp_address_event = 1;
2143 
2144 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2145 				events->sctp_send_failure_event = 1;
2146 
2147 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
2148 				events->sctp_peer_error_event = 1;
2149 
2150 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
2151 				events->sctp_shutdown_event = 1;
2152 
2153 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
2154 				events->sctp_partial_delivery_event = 1;
2155 
2156 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2157 				events->sctp_adaptation_layer_event = 1;
2158 
2159 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2160 				events->sctp_authentication_event = 1;
2161 
2162 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2163 				events->sctp_sender_dry_event = 1;
2164 
2165 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2166 				events->sctp_stream_reset_event = 1;
2167 			SCTP_INP_RUNLOCK(inp);
2168 			*optsize = sizeof(struct sctp_event_subscribe);
2169 			break;
2170 		}
2171 	case SCTP_ADAPTATION_LAYER:
2172 		{
2173 			uint32_t *value;
2174 
2175 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2176 
2177 			SCTP_INP_RLOCK(inp);
2178 			*value = inp->sctp_ep.adaptation_layer_indicator;
2179 			SCTP_INP_RUNLOCK(inp);
2180 			*optsize = sizeof(uint32_t);
2181 			break;
2182 		}
2183 	case SCTP_SET_INITIAL_DBG_SEQ:
2184 		{
2185 			uint32_t *value;
2186 
2187 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2188 			SCTP_INP_RLOCK(inp);
2189 			*value = inp->sctp_ep.initial_sequence_debug;
2190 			SCTP_INP_RUNLOCK(inp);
2191 			*optsize = sizeof(uint32_t);
2192 			break;
2193 		}
2194 	case SCTP_GET_LOCAL_ADDR_SIZE:
2195 		{
2196 			uint32_t *value;
2197 
2198 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2199 			SCTP_INP_RLOCK(inp);
2200 			*value = sctp_count_max_addresses(inp);
2201 			SCTP_INP_RUNLOCK(inp);
2202 			*optsize = sizeof(uint32_t);
2203 			break;
2204 		}
2205 	case SCTP_GET_REMOTE_ADDR_SIZE:
2206 		{
2207 			uint32_t *value;
2208 			size_t size;
2209 			struct sctp_nets *net;
2210 
2211 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2212 			/* FIXME MT: change to sctp_assoc_value? */
2213 			SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2214 
2215 			if (stcb) {
2216 				size = 0;
2217 				/* Count the sizes */
2218 				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2219 					if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2220 						size += sizeof(struct sockaddr_in6);
2221 					} else {
2222 						switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
2223 #ifdef INET
2224 						case AF_INET:
2225 							size += sizeof(struct sockaddr_in);
2226 							break;
2227 #endif
2228 #ifdef INET6
2229 						case AF_INET6:
2230 							size += sizeof(struct sockaddr_in6);
2231 							break;
2232 #endif
2233 						default:
2234 							break;
2235 						}
2236 					}
2237 				}
2238 				SCTP_TCB_UNLOCK(stcb);
2239 				*value = (uint32_t) size;
2240 				*optsize = sizeof(uint32_t);
2241 			} else {
2242 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2243 				error = ENOTCONN;
2244 			}
2245 			break;
2246 		}
2247 	case SCTP_GET_PEER_ADDRESSES:
2248 		/*
2249 		 * Get the address information, an array is passed in to
2250 		 * fill up we pack it.
2251 		 */
2252 		{
2253 			size_t cpsz, left;
2254 			struct sockaddr_storage *sas;
2255 			struct sctp_nets *net;
2256 			struct sctp_getaddresses *saddr;
2257 
2258 			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2259 			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2260 
2261 			if (stcb) {
2262 				left = (*optsize) - sizeof(struct sctp_getaddresses);
2263 				*optsize = sizeof(struct sctp_getaddresses);
2264 				sas = (struct sockaddr_storage *)&saddr->addr[0];
2265 
2266 				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2267 					if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2268 						cpsz = sizeof(struct sockaddr_in6);
2269 					} else {
2270 						switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
2271 #ifdef INET
2272 						case AF_INET:
2273 							cpsz = sizeof(struct sockaddr_in);
2274 							break;
2275 #endif
2276 #ifdef INET6
2277 						case AF_INET6:
2278 							cpsz = sizeof(struct sockaddr_in6);
2279 							break;
2280 #endif
2281 						default:
2282 							cpsz = 0;
2283 							break;
2284 						}
2285 					}
2286 					if (cpsz == 0) {
2287 						break;
2288 					}
2289 					if (left < cpsz) {
2290 						/* not enough room. */
2291 						break;
2292 					}
2293 #if defined(INET) && defined(INET6)
2294 					if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2295 					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2296 						/* Must map the address */
2297 						in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2298 						    (struct sockaddr_in6 *)sas);
2299 					} else {
2300 #endif
2301 						memcpy(sas, &net->ro._l_addr, cpsz);
2302 #if defined(INET) && defined(INET6)
2303 					}
2304 #endif
2305 					((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2306 
2307 					sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2308 					left -= cpsz;
2309 					*optsize += cpsz;
2310 				}
2311 				SCTP_TCB_UNLOCK(stcb);
2312 			} else {
2313 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2314 				error = ENOENT;
2315 			}
2316 			break;
2317 		}
2318 	case SCTP_GET_LOCAL_ADDRESSES:
2319 		{
2320 			size_t limit, actual;
2321 			struct sockaddr_storage *sas;
2322 			struct sctp_getaddresses *saddr;
2323 
2324 			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2325 			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2326 
2327 			sas = (struct sockaddr_storage *)&saddr->addr[0];
2328 			limit = *optsize - sizeof(sctp_assoc_t);
2329 			actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2330 			if (stcb) {
2331 				SCTP_TCB_UNLOCK(stcb);
2332 			}
2333 			*optsize = sizeof(struct sockaddr_storage) + actual;
2334 			break;
2335 		}
2336 	case SCTP_PEER_ADDR_PARAMS:
2337 		{
2338 			struct sctp_paddrparams *paddrp;
2339 			struct sctp_nets *net;
2340 
2341 			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2342 			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2343 
2344 			net = NULL;
2345 			if (stcb) {
2346 				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2347 			} else {
2348 				/*
2349 				 * We increment here since
2350 				 * sctp_findassociation_ep_addr() wil do a
2351 				 * decrement if it finds the stcb as long as
2352 				 * the locked tcb (last argument) is NOT a
2353 				 * TCB.. aka NULL.
2354 				 */
2355 				SCTP_INP_INCR_REF(inp);
2356 				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2357 				if (stcb == NULL) {
2358 					SCTP_INP_DECR_REF(inp);
2359 				}
2360 			}
2361 			if (stcb && (net == NULL)) {
2362 				struct sockaddr *sa;
2363 
2364 				sa = (struct sockaddr *)&paddrp->spp_address;
2365 #ifdef INET
2366 				if (sa->sa_family == AF_INET) {
2367 					struct sockaddr_in *sin;
2368 
2369 					sin = (struct sockaddr_in *)sa;
2370 					if (sin->sin_addr.s_addr) {
2371 						error = EINVAL;
2372 						SCTP_TCB_UNLOCK(stcb);
2373 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2374 						break;
2375 					}
2376 				} else
2377 #endif
2378 #ifdef INET6
2379 				if (sa->sa_family == AF_INET6) {
2380 					struct sockaddr_in6 *sin6;
2381 
2382 					sin6 = (struct sockaddr_in6 *)sa;
2383 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2384 						error = EINVAL;
2385 						SCTP_TCB_UNLOCK(stcb);
2386 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2387 						break;
2388 					}
2389 				} else
2390 #endif
2391 				{
2392 					error = EAFNOSUPPORT;
2393 					SCTP_TCB_UNLOCK(stcb);
2394 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2395 					break;
2396 				}
2397 			}
2398 			if (stcb) {
2399 				/* Applies to the specific association */
2400 				paddrp->spp_flags = 0;
2401 				if (net) {
2402 					int ovh;
2403 
2404 					if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2405 						ovh = SCTP_MED_OVERHEAD;
2406 					} else {
2407 						ovh = SCTP_MED_V4_OVERHEAD;
2408 					}
2409 
2410 					paddrp->spp_hbinterval = net->heart_beat_delay;
2411 					paddrp->spp_pathmaxrxt = net->failure_threshold;
2412 					paddrp->spp_pathmtu = net->mtu - ovh;
2413 					/* get flags for HB */
2414 					if (net->dest_state & SCTP_ADDR_NOHB) {
2415 						paddrp->spp_flags |= SPP_HB_DISABLE;
2416 					} else {
2417 						paddrp->spp_flags |= SPP_HB_ENABLE;
2418 					}
2419 					/* get flags for PMTU */
2420 					if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2421 						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2422 					} else {
2423 						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2424 					}
2425 					if (net->dscp & 0x01) {
2426 						paddrp->spp_dscp = net->dscp & 0xfc;
2427 						paddrp->spp_flags |= SPP_DSCP;
2428 					}
2429 #ifdef INET6
2430 					if ((net->ro._l_addr.sa.sa_family == AF_INET6) &&
2431 					    (net->flowlabel & 0x80000000)) {
2432 						paddrp->spp_ipv6_flowlabel = net->flowlabel & 0x000fffff;
2433 						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2434 					}
2435 #endif
2436 				} else {
2437 					/*
2438 					 * No destination so return default
2439 					 * value
2440 					 */
2441 					paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2442 					paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2443 					if (stcb->asoc.default_dscp & 0x01) {
2444 						paddrp->spp_dscp = stcb->asoc.default_dscp & 0xfc;
2445 						paddrp->spp_flags |= SPP_DSCP;
2446 					}
2447 #ifdef INET6
2448 					if (stcb->asoc.default_flowlabel & 0x80000000) {
2449 						paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel & 0x000fffff;
2450 						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2451 					}
2452 #endif
2453 					/* default settings should be these */
2454 					if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2455 						paddrp->spp_flags |= SPP_HB_DISABLE;
2456 					} else {
2457 						paddrp->spp_flags |= SPP_HB_ENABLE;
2458 					}
2459 					if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2460 						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2461 					} else {
2462 						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2463 					}
2464 					paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2465 				}
2466 				paddrp->spp_assoc_id = sctp_get_associd(stcb);
2467 				SCTP_TCB_UNLOCK(stcb);
2468 			} else {
2469 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2470 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2471 				    (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
2472 					/* Use endpoint defaults */
2473 					SCTP_INP_RLOCK(inp);
2474 					paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2475 					paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2476 					paddrp->spp_assoc_id = SCTP_FUTURE_ASSOC;
2477 					/* get inp's default */
2478 					if (inp->sctp_ep.default_dscp & 0x01) {
2479 						paddrp->spp_dscp = inp->sctp_ep.default_dscp & 0xfc;
2480 						paddrp->spp_flags |= SPP_DSCP;
2481 					}
2482 #ifdef INET6
2483 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2484 					    (inp->sctp_ep.default_flowlabel & 0x80000000)) {
2485 						paddrp->spp_ipv6_flowlabel = inp->sctp_ep.default_flowlabel & 0x000fffff;
2486 						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2487 					}
2488 #endif
2489 					/* can't return this */
2490 					paddrp->spp_pathmtu = 0;
2491 
2492 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2493 						paddrp->spp_flags |= SPP_HB_ENABLE;
2494 					} else {
2495 						paddrp->spp_flags |= SPP_HB_DISABLE;
2496 					}
2497 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2498 						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2499 					} else {
2500 						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2501 					}
2502 					SCTP_INP_RUNLOCK(inp);
2503 				} else {
2504 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2505 					error = EINVAL;
2506 				}
2507 			}
2508 			if (error == 0) {
2509 				*optsize = sizeof(struct sctp_paddrparams);
2510 			}
2511 			break;
2512 		}
2513 	case SCTP_GET_PEER_ADDR_INFO:
2514 		{
2515 			struct sctp_paddrinfo *paddri;
2516 			struct sctp_nets *net;
2517 
2518 			SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2519 			SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2520 
2521 			net = NULL;
2522 			if (stcb) {
2523 				net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2524 			} else {
2525 				/*
2526 				 * We increment here since
2527 				 * sctp_findassociation_ep_addr() wil do a
2528 				 * decrement if it finds the stcb as long as
2529 				 * the locked tcb (last argument) is NOT a
2530 				 * TCB.. aka NULL.
2531 				 */
2532 				SCTP_INP_INCR_REF(inp);
2533 				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2534 				if (stcb == NULL) {
2535 					SCTP_INP_DECR_REF(inp);
2536 				}
2537 			}
2538 
2539 			if ((stcb) && (net)) {
2540 				if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2541 					/* It's unconfirmed */
2542 					paddri->spinfo_state = SCTP_UNCONFIRMED;
2543 				} else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2544 					/* It's active */
2545 					paddri->spinfo_state = SCTP_ACTIVE;
2546 				} else {
2547 					/* It's inactive */
2548 					paddri->spinfo_state = SCTP_INACTIVE;
2549 				}
2550 				paddri->spinfo_cwnd = net->cwnd;
2551 				paddri->spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2552 				paddri->spinfo_rto = net->RTO;
2553 				paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2554 				paddri->spinfo_mtu = net->mtu;
2555 				SCTP_TCB_UNLOCK(stcb);
2556 				*optsize = sizeof(struct sctp_paddrinfo);
2557 			} else {
2558 				if (stcb) {
2559 					SCTP_TCB_UNLOCK(stcb);
2560 				}
2561 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2562 				error = ENOENT;
2563 			}
2564 			break;
2565 		}
2566 	case SCTP_PCB_STATUS:
2567 		{
2568 			struct sctp_pcbinfo *spcb;
2569 
2570 			SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2571 			sctp_fill_pcbinfo(spcb);
2572 			*optsize = sizeof(struct sctp_pcbinfo);
2573 			break;
2574 		}
2575 	case SCTP_STATUS:
2576 		{
2577 			struct sctp_nets *net;
2578 			struct sctp_status *sstat;
2579 
2580 			SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2581 			SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2582 
2583 			if (stcb == NULL) {
2584 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2585 				error = EINVAL;
2586 				break;
2587 			}
2588 			/*
2589 			 * I think passing the state is fine since
2590 			 * sctp_constants.h will be available to the user
2591 			 * land.
2592 			 */
2593 			sstat->sstat_state = stcb->asoc.state;
2594 			sstat->sstat_assoc_id = sctp_get_associd(stcb);
2595 			sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2596 			sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2597 			/*
2598 			 * We can't include chunks that have been passed to
2599 			 * the socket layer. Only things in queue.
2600 			 */
2601 			sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2602 			    stcb->asoc.cnt_on_all_streams);
2603 
2604 
2605 			sstat->sstat_instrms = stcb->asoc.streamincnt;
2606 			sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2607 			sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2608 			memcpy(&sstat->sstat_primary.spinfo_address,
2609 			    &stcb->asoc.primary_destination->ro._l_addr,
2610 			    ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2611 			net = stcb->asoc.primary_destination;
2612 			((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2613 			/*
2614 			 * Again the user can get info from sctp_constants.h
2615 			 * for what the state of the network is.
2616 			 */
2617 			if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2618 				/* It's unconfirmed */
2619 				sstat->sstat_primary.spinfo_state = SCTP_UNCONFIRMED;
2620 			} else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2621 				/* It's active */
2622 				sstat->sstat_primary.spinfo_state = SCTP_ACTIVE;
2623 			} else {
2624 				/* It's inactive */
2625 				sstat->sstat_primary.spinfo_state = SCTP_INACTIVE;
2626 			}
2627 			sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2628 			sstat->sstat_primary.spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2629 			sstat->sstat_primary.spinfo_rto = net->RTO;
2630 			sstat->sstat_primary.spinfo_mtu = net->mtu;
2631 			sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2632 			SCTP_TCB_UNLOCK(stcb);
2633 			*optsize = sizeof(struct sctp_status);
2634 			break;
2635 		}
2636 	case SCTP_RTOINFO:
2637 		{
2638 			struct sctp_rtoinfo *srto;
2639 
2640 			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2641 			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2642 
2643 			if (stcb) {
2644 				srto->srto_initial = stcb->asoc.initial_rto;
2645 				srto->srto_max = stcb->asoc.maxrto;
2646 				srto->srto_min = stcb->asoc.minrto;
2647 				SCTP_TCB_UNLOCK(stcb);
2648 			} else {
2649 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2650 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2651 				    (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
2652 					SCTP_INP_RLOCK(inp);
2653 					srto->srto_initial = inp->sctp_ep.initial_rto;
2654 					srto->srto_max = inp->sctp_ep.sctp_maxrto;
2655 					srto->srto_min = inp->sctp_ep.sctp_minrto;
2656 					SCTP_INP_RUNLOCK(inp);
2657 				} else {
2658 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2659 					error = EINVAL;
2660 				}
2661 			}
2662 			if (error == 0) {
2663 				*optsize = sizeof(struct sctp_rtoinfo);
2664 			}
2665 			break;
2666 		}
2667 	case SCTP_TIMEOUTS:
2668 		{
2669 			struct sctp_timeouts *stimo;
2670 
2671 			SCTP_CHECK_AND_CAST(stimo, optval, struct sctp_timeouts, *optsize);
2672 			SCTP_FIND_STCB(inp, stcb, stimo->stimo_assoc_id);
2673 
2674 			if (stcb) {
2675 				stimo->stimo_init = stcb->asoc.timoinit;
2676 				stimo->stimo_data = stcb->asoc.timodata;
2677 				stimo->stimo_sack = stcb->asoc.timosack;
2678 				stimo->stimo_shutdown = stcb->asoc.timoshutdown;
2679 				stimo->stimo_heartbeat = stcb->asoc.timoheartbeat;
2680 				stimo->stimo_cookie = stcb->asoc.timocookie;
2681 				stimo->stimo_shutdownack = stcb->asoc.timoshutdownack;
2682 				SCTP_TCB_UNLOCK(stcb);
2683 				*optsize = sizeof(struct sctp_timeouts);
2684 			} else {
2685 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2686 				error = EINVAL;
2687 			}
2688 			break;
2689 		}
2690 	case SCTP_ASSOCINFO:
2691 		{
2692 			struct sctp_assocparams *sasoc;
2693 
2694 			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2695 			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2696 
2697 			if (stcb) {
2698 				sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2699 				sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2700 				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2701 				sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2702 				sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2703 				SCTP_TCB_UNLOCK(stcb);
2704 			} else {
2705 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2706 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2707 				    (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
2708 					SCTP_INP_RLOCK(inp);
2709 					sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2710 					sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2711 					sasoc->sasoc_number_peer_destinations = 0;
2712 					sasoc->sasoc_peer_rwnd = 0;
2713 					sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2714 					SCTP_INP_RUNLOCK(inp);
2715 				} else {
2716 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2717 					error = EINVAL;
2718 				}
2719 			}
2720 			if (error == 0) {
2721 				*optsize = sizeof(struct sctp_assocparams);
2722 			}
2723 			break;
2724 		}
2725 	case SCTP_DEFAULT_SEND_PARAM:
2726 		{
2727 			struct sctp_sndrcvinfo *s_info;
2728 
2729 			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2730 			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2731 
2732 			if (stcb) {
2733 				memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2734 				SCTP_TCB_UNLOCK(stcb);
2735 			} else {
2736 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2737 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2738 				    (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC)) {
2739 					SCTP_INP_RLOCK(inp);
2740 					memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2741 					SCTP_INP_RUNLOCK(inp);
2742 				} else {
2743 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2744 					error = EINVAL;
2745 				}
2746 			}
2747 			if (error == 0) {
2748 				*optsize = sizeof(struct sctp_sndrcvinfo);
2749 			}
2750 			break;
2751 		}
2752 	case SCTP_INITMSG:
2753 		{
2754 			struct sctp_initmsg *sinit;
2755 
2756 			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2757 			SCTP_INP_RLOCK(inp);
2758 			sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2759 			sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2760 			sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2761 			sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2762 			SCTP_INP_RUNLOCK(inp);
2763 			*optsize = sizeof(struct sctp_initmsg);
2764 			break;
2765 		}
2766 	case SCTP_PRIMARY_ADDR:
2767 		/* we allow a "get" operation on this */
2768 		{
2769 			struct sctp_setprim *ssp;
2770 
2771 			SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2772 			SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2773 
2774 			if (stcb) {
2775 				/* simply copy out the sockaddr_storage... */
2776 				size_t len;
2777 
2778 				len = *optsize;
2779 				if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2780 					len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2781 
2782 				memcpy(&ssp->ssp_addr,
2783 				    &stcb->asoc.primary_destination->ro._l_addr,
2784 				    len);
2785 				SCTP_TCB_UNLOCK(stcb);
2786 				*optsize = sizeof(struct sctp_setprim);
2787 			} else {
2788 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2789 				error = EINVAL;
2790 			}
2791 			break;
2792 		}
2793 	case SCTP_HMAC_IDENT:
2794 		{
2795 			struct sctp_hmacalgo *shmac;
2796 			sctp_hmaclist_t *hmaclist;
2797 			uint32_t size;
2798 			int i;
2799 
2800 			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2801 
2802 			SCTP_INP_RLOCK(inp);
2803 			hmaclist = inp->sctp_ep.local_hmacs;
2804 			if (hmaclist == NULL) {
2805 				/* no HMACs to return */
2806 				*optsize = sizeof(*shmac);
2807 				SCTP_INP_RUNLOCK(inp);
2808 				break;
2809 			}
2810 			/* is there room for all of the hmac ids? */
2811 			size = sizeof(*shmac) + (hmaclist->num_algo *
2812 			    sizeof(shmac->shmac_idents[0]));
2813 			if ((size_t)(*optsize) < size) {
2814 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2815 				error = EINVAL;
2816 				SCTP_INP_RUNLOCK(inp);
2817 				break;
2818 			}
2819 			/* copy in the list */
2820 			shmac->shmac_number_of_idents = hmaclist->num_algo;
2821 			for (i = 0; i < hmaclist->num_algo; i++) {
2822 				shmac->shmac_idents[i] = hmaclist->hmac[i];
2823 			}
2824 			SCTP_INP_RUNLOCK(inp);
2825 			*optsize = size;
2826 			break;
2827 		}
2828 	case SCTP_AUTH_ACTIVE_KEY:
2829 		{
2830 			struct sctp_authkeyid *scact;
2831 
2832 			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2833 			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2834 
2835 			if (stcb) {
2836 				/* get the active key on the assoc */
2837 				scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2838 				SCTP_TCB_UNLOCK(stcb);
2839 			} else {
2840 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2841 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2842 				    (scact->scact_assoc_id == SCTP_FUTURE_ASSOC)) {
2843 					/* get the endpoint active key */
2844 					SCTP_INP_RLOCK(inp);
2845 					scact->scact_keynumber = inp->sctp_ep.default_keyid;
2846 					SCTP_INP_RUNLOCK(inp);
2847 				} else {
2848 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2849 					error = EINVAL;
2850 				}
2851 			}
2852 			if (error == 0) {
2853 				*optsize = sizeof(struct sctp_authkeyid);
2854 			}
2855 			break;
2856 		}
2857 	case SCTP_LOCAL_AUTH_CHUNKS:
2858 		{
2859 			struct sctp_authchunks *sac;
2860 			sctp_auth_chklist_t *chklist = NULL;
2861 			size_t size = 0;
2862 
2863 			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2864 			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2865 
2866 			if (stcb) {
2867 				/* get off the assoc */
2868 				chklist = stcb->asoc.local_auth_chunks;
2869 				/* is there enough space? */
2870 				size = sctp_auth_get_chklist_size(chklist);
2871 				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2872 					error = EINVAL;
2873 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2874 				} else {
2875 					/* copy in the chunks */
2876 					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2877 					sac->gauth_number_of_chunks = (uint32_t) size;
2878 					*optsize = sizeof(struct sctp_authchunks) + size;
2879 				}
2880 				SCTP_TCB_UNLOCK(stcb);
2881 			} else {
2882 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2883 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2884 				    (sac->gauth_assoc_id == SCTP_FUTURE_ASSOC)) {
2885 					/* get off the endpoint */
2886 					SCTP_INP_RLOCK(inp);
2887 					chklist = inp->sctp_ep.local_auth_chunks;
2888 					/* is there enough space? */
2889 					size = sctp_auth_get_chklist_size(chklist);
2890 					if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2891 						error = EINVAL;
2892 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2893 					} else {
2894 						/* copy in the chunks */
2895 						(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2896 						sac->gauth_number_of_chunks = (uint32_t) size;
2897 						*optsize = sizeof(struct sctp_authchunks) + size;
2898 					}
2899 					SCTP_INP_RUNLOCK(inp);
2900 				} else {
2901 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2902 					error = EINVAL;
2903 				}
2904 			}
2905 			break;
2906 		}
2907 	case SCTP_PEER_AUTH_CHUNKS:
2908 		{
2909 			struct sctp_authchunks *sac;
2910 			sctp_auth_chklist_t *chklist = NULL;
2911 			size_t size = 0;
2912 
2913 			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2914 			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2915 
2916 			if (stcb) {
2917 				/* get off the assoc */
2918 				chklist = stcb->asoc.peer_auth_chunks;
2919 				/* is there enough space? */
2920 				size = sctp_auth_get_chklist_size(chklist);
2921 				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2922 					error = EINVAL;
2923 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2924 				} else {
2925 					/* copy in the chunks */
2926 					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2927 					sac->gauth_number_of_chunks = (uint32_t) size;
2928 					*optsize = sizeof(struct sctp_authchunks) + size;
2929 				}
2930 				SCTP_TCB_UNLOCK(stcb);
2931 			} else {
2932 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2933 				error = ENOENT;
2934 			}
2935 			break;
2936 		}
2937 	case SCTP_EVENT:
2938 		{
2939 			struct sctp_event *event;
2940 			uint32_t event_type;
2941 
2942 			SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, *optsize);
2943 			SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
2944 
2945 			switch (event->se_type) {
2946 			case SCTP_ASSOC_CHANGE:
2947 				event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
2948 				break;
2949 			case SCTP_PEER_ADDR_CHANGE:
2950 				event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
2951 				break;
2952 			case SCTP_REMOTE_ERROR:
2953 				event_type = SCTP_PCB_FLAGS_RECVPEERERR;
2954 				break;
2955 			case SCTP_SEND_FAILED:
2956 				event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
2957 				break;
2958 			case SCTP_SHUTDOWN_EVENT:
2959 				event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
2960 				break;
2961 			case SCTP_ADAPTATION_INDICATION:
2962 				event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
2963 				break;
2964 			case SCTP_PARTIAL_DELIVERY_EVENT:
2965 				event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
2966 				break;
2967 			case SCTP_AUTHENTICATION_EVENT:
2968 				event_type = SCTP_PCB_FLAGS_AUTHEVNT;
2969 				break;
2970 			case SCTP_STREAM_RESET_EVENT:
2971 				event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
2972 				break;
2973 			case SCTP_SENDER_DRY_EVENT:
2974 				event_type = SCTP_PCB_FLAGS_DRYEVNT;
2975 				break;
2976 			case SCTP_NOTIFICATIONS_STOPPED_EVENT:
2977 				event_type = 0;
2978 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
2979 				error = ENOTSUP;
2980 				break;
2981 			case SCTP_ASSOC_RESET_EVENT:
2982 				event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
2983 				break;
2984 			case SCTP_STREAM_CHANGE_EVENT:
2985 				event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
2986 				break;
2987 			case SCTP_SEND_FAILED_EVENT:
2988 				event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
2989 				break;
2990 			default:
2991 				event_type = 0;
2992 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2993 				error = EINVAL;
2994 				break;
2995 			}
2996 			if (event_type > 0) {
2997 				if (stcb) {
2998 					event->se_on = sctp_stcb_is_feature_on(inp, stcb, event_type);
2999 					SCTP_TCB_UNLOCK(stcb);
3000 				} else {
3001 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3002 					    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3003 					    (event->se_assoc_id == SCTP_FUTURE_ASSOC)) {
3004 						SCTP_INP_RLOCK(inp);
3005 						event->se_on = sctp_is_feature_on(inp, event_type);
3006 						SCTP_INP_RUNLOCK(inp);
3007 					} else {
3008 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3009 						error = EINVAL;
3010 					}
3011 				}
3012 			}
3013 			if (error == 0) {
3014 				*optsize = sizeof(struct sctp_event);
3015 			}
3016 			break;
3017 		}
3018 	case SCTP_RECVRCVINFO:
3019 		{
3020 			int onoff;
3021 
3022 			if (*optsize < sizeof(int)) {
3023 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3024 				error = EINVAL;
3025 			} else {
3026 				SCTP_INP_RLOCK(inp);
3027 				onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
3028 				SCTP_INP_RUNLOCK(inp);
3029 			}
3030 			if (error == 0) {
3031 				/* return the option value */
3032 				*(int *)optval = onoff;
3033 				*optsize = sizeof(int);
3034 			}
3035 			break;
3036 		}
3037 	case SCTP_RECVNXTINFO:
3038 		{
3039 			int onoff;
3040 
3041 			if (*optsize < sizeof(int)) {
3042 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3043 				error = EINVAL;
3044 			} else {
3045 				SCTP_INP_RLOCK(inp);
3046 				onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
3047 				SCTP_INP_RUNLOCK(inp);
3048 			}
3049 			if (error == 0) {
3050 				/* return the option value */
3051 				*(int *)optval = onoff;
3052 				*optsize = sizeof(int);
3053 			}
3054 			break;
3055 		}
3056 	case SCTP_DEFAULT_SNDINFO:
3057 		{
3058 			struct sctp_sndinfo *info;
3059 
3060 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, *optsize);
3061 			SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
3062 
3063 			if (stcb) {
3064 				info->snd_sid = stcb->asoc.def_send.sinfo_stream;
3065 				info->snd_flags = stcb->asoc.def_send.sinfo_flags;
3066 				info->snd_flags &= 0xfff0;
3067 				info->snd_ppid = stcb->asoc.def_send.sinfo_ppid;
3068 				info->snd_context = stcb->asoc.def_send.sinfo_context;
3069 				SCTP_TCB_UNLOCK(stcb);
3070 			} else {
3071 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3072 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3073 				    (info->snd_assoc_id == SCTP_FUTURE_ASSOC)) {
3074 					SCTP_INP_RLOCK(inp);
3075 					info->snd_sid = inp->def_send.sinfo_stream;
3076 					info->snd_flags = inp->def_send.sinfo_flags;
3077 					info->snd_flags &= 0xfff0;
3078 					info->snd_ppid = inp->def_send.sinfo_ppid;
3079 					info->snd_context = inp->def_send.sinfo_context;
3080 					SCTP_INP_RUNLOCK(inp);
3081 				} else {
3082 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3083 					error = EINVAL;
3084 				}
3085 			}
3086 			if (error == 0) {
3087 				*optsize = sizeof(struct sctp_sndinfo);
3088 			}
3089 			break;
3090 		}
3091 	case SCTP_DEFAULT_PRINFO:
3092 		{
3093 			struct sctp_default_prinfo *info;
3094 
3095 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, *optsize);
3096 			SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
3097 
3098 			if (stcb) {
3099 				info->pr_policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
3100 				info->pr_value = stcb->asoc.def_send.sinfo_timetolive;
3101 				SCTP_TCB_UNLOCK(stcb);
3102 			} else {
3103 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3104 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3105 				    (info->pr_assoc_id == SCTP_FUTURE_ASSOC)) {
3106 					SCTP_INP_RLOCK(inp);
3107 					info->pr_policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
3108 					info->pr_value = inp->def_send.sinfo_timetolive;
3109 					SCTP_INP_RUNLOCK(inp);
3110 				} else {
3111 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3112 					error = EINVAL;
3113 				}
3114 			}
3115 			if (error == 0) {
3116 				*optsize = sizeof(struct sctp_default_prinfo);
3117 			}
3118 			break;
3119 		}
3120 	case SCTP_PEER_ADDR_THLDS:
3121 		{
3122 			struct sctp_paddrthlds *thlds;
3123 			struct sctp_nets *net;
3124 
3125 			SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, *optsize);
3126 			SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
3127 
3128 			net = NULL;
3129 			if (stcb) {
3130 				net = sctp_findnet(stcb, (struct sockaddr *)&thlds->spt_address);
3131 			} else {
3132 				/*
3133 				 * We increment here since
3134 				 * sctp_findassociation_ep_addr() wil do a
3135 				 * decrement if it finds the stcb as long as
3136 				 * the locked tcb (last argument) is NOT a
3137 				 * TCB.. aka NULL.
3138 				 */
3139 				SCTP_INP_INCR_REF(inp);
3140 				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&thlds->spt_address, &net, NULL, NULL);
3141 				if (stcb == NULL) {
3142 					SCTP_INP_DECR_REF(inp);
3143 				}
3144 			}
3145 			if (stcb && (net == NULL)) {
3146 				struct sockaddr *sa;
3147 
3148 				sa = (struct sockaddr *)&thlds->spt_address;
3149 #ifdef INET
3150 				if (sa->sa_family == AF_INET) {
3151 					struct sockaddr_in *sin;
3152 
3153 					sin = (struct sockaddr_in *)sa;
3154 					if (sin->sin_addr.s_addr) {
3155 						error = EINVAL;
3156 						SCTP_TCB_UNLOCK(stcb);
3157 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3158 						break;
3159 					}
3160 				} else
3161 #endif
3162 #ifdef INET6
3163 				if (sa->sa_family == AF_INET6) {
3164 					struct sockaddr_in6 *sin6;
3165 
3166 					sin6 = (struct sockaddr_in6 *)sa;
3167 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3168 						error = EINVAL;
3169 						SCTP_TCB_UNLOCK(stcb);
3170 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3171 						break;
3172 					}
3173 				} else
3174 #endif
3175 				{
3176 					error = EAFNOSUPPORT;
3177 					SCTP_TCB_UNLOCK(stcb);
3178 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3179 					break;
3180 				}
3181 			}
3182 			if (stcb) {
3183 				if (net) {
3184 					thlds->spt_pathmaxrxt = net->failure_threshold;
3185 					thlds->spt_pathpfthld = net->pf_threshold;
3186 				} else {
3187 					thlds->spt_pathmaxrxt = stcb->asoc.def_net_failure;
3188 					thlds->spt_pathpfthld = stcb->asoc.def_net_pf_threshold;
3189 				}
3190 				thlds->spt_assoc_id = sctp_get_associd(stcb);
3191 				SCTP_TCB_UNLOCK(stcb);
3192 			} else {
3193 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3194 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3195 				    (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
3196 					/* Use endpoint defaults */
3197 					SCTP_INP_RLOCK(inp);
3198 					thlds->spt_pathmaxrxt = inp->sctp_ep.def_net_failure;
3199 					thlds->spt_pathpfthld = inp->sctp_ep.def_net_pf_threshold;
3200 					SCTP_INP_RUNLOCK(inp);
3201 				} else {
3202 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3203 					error = EINVAL;
3204 				}
3205 			}
3206 			if (error == 0) {
3207 				*optsize = sizeof(struct sctp_paddrthlds);
3208 			}
3209 			break;
3210 		}
3211 	case SCTP_REMOTE_UDP_ENCAPS_PORT:
3212 		{
3213 			struct sctp_udpencaps *encaps;
3214 			struct sctp_nets *net;
3215 
3216 			SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, *optsize);
3217 			SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
3218 
3219 			if (stcb) {
3220 				net = sctp_findnet(stcb, (struct sockaddr *)&encaps->sue_address);
3221 			} else {
3222 				/*
3223 				 * We increment here since
3224 				 * sctp_findassociation_ep_addr() wil do a
3225 				 * decrement if it finds the stcb as long as
3226 				 * the locked tcb (last argument) is NOT a
3227 				 * TCB.. aka NULL.
3228 				 */
3229 				net = NULL;
3230 				SCTP_INP_INCR_REF(inp);
3231 				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&encaps->sue_address, &net, NULL, NULL);
3232 				if (stcb == NULL) {
3233 					SCTP_INP_DECR_REF(inp);
3234 				}
3235 			}
3236 			if (stcb && (net == NULL)) {
3237 				struct sockaddr *sa;
3238 
3239 				sa = (struct sockaddr *)&encaps->sue_address;
3240 #ifdef INET
3241 				if (sa->sa_family == AF_INET) {
3242 					struct sockaddr_in *sin;
3243 
3244 					sin = (struct sockaddr_in *)sa;
3245 					if (sin->sin_addr.s_addr) {
3246 						error = EINVAL;
3247 						SCTP_TCB_UNLOCK(stcb);
3248 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3249 						break;
3250 					}
3251 				} else
3252 #endif
3253 #ifdef INET6
3254 				if (sa->sa_family == AF_INET6) {
3255 					struct sockaddr_in6 *sin6;
3256 
3257 					sin6 = (struct sockaddr_in6 *)sa;
3258 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3259 						error = EINVAL;
3260 						SCTP_TCB_UNLOCK(stcb);
3261 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3262 						break;
3263 					}
3264 				} else
3265 #endif
3266 				{
3267 					error = EAFNOSUPPORT;
3268 					SCTP_TCB_UNLOCK(stcb);
3269 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3270 					break;
3271 				}
3272 			}
3273 			if (stcb) {
3274 				if (net) {
3275 					encaps->sue_port = net->port;
3276 				} else {
3277 					encaps->sue_port = stcb->asoc.port;
3278 				}
3279 				SCTP_TCB_UNLOCK(stcb);
3280 			} else {
3281 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3282 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3283 				    (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
3284 					SCTP_INP_RLOCK(inp);
3285 					encaps->sue_port = inp->sctp_ep.port;
3286 					SCTP_INP_RUNLOCK(inp);
3287 				} else {
3288 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3289 					error = EINVAL;
3290 				}
3291 			}
3292 			if (error == 0) {
3293 				*optsize = sizeof(struct sctp_udpencaps);
3294 			}
3295 			break;
3296 		}
3297 	case SCTP_ECN_SUPPORTED:
3298 		{
3299 			struct sctp_assoc_value *av;
3300 
3301 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3302 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3303 
3304 			if (stcb) {
3305 				av->assoc_value = stcb->asoc.ecn_supported;
3306 				SCTP_TCB_UNLOCK(stcb);
3307 			} else {
3308 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3309 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3310 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3311 					SCTP_INP_RLOCK(inp);
3312 					av->assoc_value = inp->ecn_supported;
3313 					SCTP_INP_RUNLOCK(inp);
3314 				} else {
3315 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3316 					error = EINVAL;
3317 				}
3318 			}
3319 			if (error == 0) {
3320 				*optsize = sizeof(struct sctp_assoc_value);
3321 			}
3322 			break;
3323 		}
3324 	case SCTP_PR_SUPPORTED:
3325 		{
3326 			struct sctp_assoc_value *av;
3327 
3328 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3329 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3330 
3331 			if (stcb) {
3332 				av->assoc_value = stcb->asoc.prsctp_supported;
3333 				SCTP_TCB_UNLOCK(stcb);
3334 			} else {
3335 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3336 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3337 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3338 					SCTP_INP_RLOCK(inp);
3339 					av->assoc_value = inp->prsctp_supported;
3340 					SCTP_INP_RUNLOCK(inp);
3341 				} else {
3342 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3343 					error = EINVAL;
3344 				}
3345 			}
3346 			if (error == 0) {
3347 				*optsize = sizeof(struct sctp_assoc_value);
3348 			}
3349 			break;
3350 		}
3351 	case SCTP_RECONFIG_SUPPORTED:
3352 		{
3353 			struct sctp_assoc_value *av;
3354 
3355 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3356 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3357 
3358 			if (stcb) {
3359 				av->assoc_value = stcb->asoc.reconfig_supported;
3360 				SCTP_TCB_UNLOCK(stcb);
3361 			} else {
3362 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3363 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3364 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3365 					SCTP_INP_RLOCK(inp);
3366 					av->assoc_value = inp->reconfig_supported;
3367 					SCTP_INP_RUNLOCK(inp);
3368 				} else {
3369 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3370 					error = EINVAL;
3371 				}
3372 			}
3373 			if (error == 0) {
3374 				*optsize = sizeof(struct sctp_assoc_value);
3375 			}
3376 			break;
3377 		}
3378 	case SCTP_NRSACK_SUPPORTED:
3379 		{
3380 			struct sctp_assoc_value *av;
3381 
3382 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3383 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3384 
3385 			if (stcb) {
3386 				av->assoc_value = stcb->asoc.nrsack_supported;
3387 				SCTP_TCB_UNLOCK(stcb);
3388 			} else {
3389 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3390 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3391 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3392 					SCTP_INP_RLOCK(inp);
3393 					av->assoc_value = inp->nrsack_supported;
3394 					SCTP_INP_RUNLOCK(inp);
3395 				} else {
3396 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3397 					error = EINVAL;
3398 				}
3399 			}
3400 			if (error == 0) {
3401 				*optsize = sizeof(struct sctp_assoc_value);
3402 			}
3403 			break;
3404 		}
3405 	case SCTP_PKTDROP_SUPPORTED:
3406 		{
3407 			struct sctp_assoc_value *av;
3408 
3409 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3410 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3411 
3412 			if (stcb) {
3413 				av->assoc_value = stcb->asoc.pktdrop_supported;
3414 				SCTP_TCB_UNLOCK(stcb);
3415 			} else {
3416 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3417 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3418 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3419 					SCTP_INP_RLOCK(inp);
3420 					av->assoc_value = inp->pktdrop_supported;
3421 					SCTP_INP_RUNLOCK(inp);
3422 				} else {
3423 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3424 					error = EINVAL;
3425 				}
3426 			}
3427 			if (error == 0) {
3428 				*optsize = sizeof(struct sctp_assoc_value);
3429 			}
3430 			break;
3431 		}
3432 	case SCTP_ENABLE_STREAM_RESET:
3433 		{
3434 			struct sctp_assoc_value *av;
3435 
3436 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3437 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3438 
3439 			if (stcb) {
3440 				av->assoc_value = (uint32_t) stcb->asoc.local_strreset_support;
3441 				SCTP_TCB_UNLOCK(stcb);
3442 			} else {
3443 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3444 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3445 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3446 					SCTP_INP_RLOCK(inp);
3447 					av->assoc_value = (uint32_t) inp->local_strreset_support;
3448 					SCTP_INP_RUNLOCK(inp);
3449 				} else {
3450 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3451 					error = EINVAL;
3452 				}
3453 			}
3454 			if (error == 0) {
3455 				*optsize = sizeof(struct sctp_assoc_value);
3456 			}
3457 			break;
3458 		}
3459 	default:
3460 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3461 		error = ENOPROTOOPT;
3462 		break;
3463 	}			/* end switch (sopt->sopt_name) */
3464 	if (error) {
3465 		*optsize = 0;
3466 	}
3467 	return (error);
3468 }
3469 
3470 static int
3471 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
3472     void *p)
3473 {
3474 	int error, set_opt;
3475 	uint32_t *mopt;
3476 	struct sctp_tcb *stcb = NULL;
3477 	struct sctp_inpcb *inp = NULL;
3478 	uint32_t vrf_id;
3479 
3480 	if (optval == NULL) {
3481 		SCTP_PRINTF("optval is NULL\n");
3482 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3483 		return (EINVAL);
3484 	}
3485 	inp = (struct sctp_inpcb *)so->so_pcb;
3486 	if (inp == NULL) {
3487 		SCTP_PRINTF("inp is NULL?\n");
3488 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3489 		return (EINVAL);
3490 	}
3491 	vrf_id = inp->def_vrf_id;
3492 
3493 	error = 0;
3494 	switch (optname) {
3495 	case SCTP_NODELAY:
3496 	case SCTP_AUTOCLOSE:
3497 	case SCTP_AUTO_ASCONF:
3498 	case SCTP_EXPLICIT_EOR:
3499 	case SCTP_DISABLE_FRAGMENTS:
3500 	case SCTP_USE_EXT_RCVINFO:
3501 	case SCTP_I_WANT_MAPPED_V4_ADDR:
3502 		/* copy in the option value */
3503 		SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3504 		set_opt = 0;
3505 		if (error)
3506 			break;
3507 		switch (optname) {
3508 		case SCTP_DISABLE_FRAGMENTS:
3509 			set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
3510 			break;
3511 		case SCTP_AUTO_ASCONF:
3512 			/*
3513 			 * NOTE: we don't really support this flag
3514 			 */
3515 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3516 				/* only valid for bound all sockets */
3517 				if ((SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) &&
3518 				    (*mopt != 0)) {
3519 					/* forbidden by admin */
3520 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPERM);
3521 					return (EPERM);
3522 				}
3523 				set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
3524 			} else {
3525 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3526 				return (EINVAL);
3527 			}
3528 			break;
3529 		case SCTP_EXPLICIT_EOR:
3530 			set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
3531 			break;
3532 		case SCTP_USE_EXT_RCVINFO:
3533 			set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
3534 			break;
3535 		case SCTP_I_WANT_MAPPED_V4_ADDR:
3536 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3537 				set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
3538 			} else {
3539 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3540 				return (EINVAL);
3541 			}
3542 			break;
3543 		case SCTP_NODELAY:
3544 			set_opt = SCTP_PCB_FLAGS_NODELAY;
3545 			break;
3546 		case SCTP_AUTOCLOSE:
3547 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3548 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3549 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3550 				return (EINVAL);
3551 			}
3552 			set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
3553 			/*
3554 			 * The value is in ticks. Note this does not effect
3555 			 * old associations, only new ones.
3556 			 */
3557 			inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
3558 			break;
3559 		}
3560 		SCTP_INP_WLOCK(inp);
3561 		if (*mopt != 0) {
3562 			sctp_feature_on(inp, set_opt);
3563 		} else {
3564 			sctp_feature_off(inp, set_opt);
3565 		}
3566 		SCTP_INP_WUNLOCK(inp);
3567 		break;
3568 	case SCTP_REUSE_PORT:
3569 		{
3570 			SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3571 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3572 				/* Can't set it after we are bound */
3573 				error = EINVAL;
3574 				break;
3575 			}
3576 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
3577 				/* Can't do this for a 1-m socket */
3578 				error = EINVAL;
3579 				break;
3580 			}
3581 			if (optval)
3582 				sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
3583 			else
3584 				sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
3585 			break;
3586 		}
3587 	case SCTP_PARTIAL_DELIVERY_POINT:
3588 		{
3589 			uint32_t *value;
3590 
3591 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
3592 			if (*value > SCTP_SB_LIMIT_RCV(so)) {
3593 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3594 				error = EINVAL;
3595 				break;
3596 			}
3597 			inp->partial_delivery_point = *value;
3598 			break;
3599 		}
3600 	case SCTP_FRAGMENT_INTERLEAVE:
3601 		/* not yet until we re-write sctp_recvmsg() */
3602 		{
3603 			uint32_t *level;
3604 
3605 			SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
3606 			if (*level == SCTP_FRAG_LEVEL_2) {
3607 				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3608 				sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3609 			} else if (*level == SCTP_FRAG_LEVEL_1) {
3610 				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3611 				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3612 			} else if (*level == SCTP_FRAG_LEVEL_0) {
3613 				sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3614 				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3615 
3616 			} else {
3617 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3618 				error = EINVAL;
3619 			}
3620 			break;
3621 		}
3622 	case SCTP_CMT_ON_OFF:
3623 		if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3624 			struct sctp_assoc_value *av;
3625 
3626 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3627 			if (av->assoc_value > SCTP_CMT_MAX) {
3628 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3629 				error = EINVAL;
3630 				break;
3631 			}
3632 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3633 			if (stcb) {
3634 				stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3635 				SCTP_TCB_UNLOCK(stcb);
3636 			} else {
3637 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3638 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3639 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3640 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
3641 					SCTP_INP_WLOCK(inp);
3642 					inp->sctp_cmt_on_off = av->assoc_value;
3643 					SCTP_INP_WUNLOCK(inp);
3644 				}
3645 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3646 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
3647 					SCTP_INP_RLOCK(inp);
3648 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3649 						SCTP_TCB_LOCK(stcb);
3650 						stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3651 						SCTP_TCB_UNLOCK(stcb);
3652 					}
3653 					SCTP_INP_RUNLOCK(inp);
3654 				}
3655 			}
3656 		} else {
3657 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3658 			error = ENOPROTOOPT;
3659 		}
3660 		break;
3661 	case SCTP_PLUGGABLE_CC:
3662 		{
3663 			struct sctp_assoc_value *av;
3664 			struct sctp_nets *net;
3665 
3666 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3667 			if ((av->assoc_value != SCTP_CC_RFC2581) &&
3668 			    (av->assoc_value != SCTP_CC_HSTCP) &&
3669 			    (av->assoc_value != SCTP_CC_HTCP) &&
3670 			    (av->assoc_value != SCTP_CC_RTCC)) {
3671 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3672 				error = EINVAL;
3673 				break;
3674 			}
3675 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3676 			if (stcb) {
3677 				stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
3678 				stcb->asoc.congestion_control_module = av->assoc_value;
3679 				if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
3680 					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3681 						stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3682 					}
3683 				}
3684 				SCTP_TCB_UNLOCK(stcb);
3685 			} else {
3686 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3687 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3688 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3689 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
3690 					SCTP_INP_WLOCK(inp);
3691 					inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
3692 					SCTP_INP_WUNLOCK(inp);
3693 				}
3694 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3695 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
3696 					SCTP_INP_RLOCK(inp);
3697 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3698 						SCTP_TCB_LOCK(stcb);
3699 						stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
3700 						stcb->asoc.congestion_control_module = av->assoc_value;
3701 						if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
3702 							TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3703 								stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3704 							}
3705 						}
3706 						SCTP_TCB_UNLOCK(stcb);
3707 					}
3708 					SCTP_INP_RUNLOCK(inp);
3709 				}
3710 			}
3711 			break;
3712 		}
3713 	case SCTP_CC_OPTION:
3714 		{
3715 			struct sctp_cc_option *cc_opt;
3716 
3717 			SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, optsize);
3718 			SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
3719 			if (stcb == NULL) {
3720 				if (cc_opt->aid_value.assoc_id == SCTP_CURRENT_ASSOC) {
3721 					SCTP_INP_RLOCK(inp);
3722 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3723 						SCTP_TCB_LOCK(stcb);
3724 						if (stcb->asoc.cc_functions.sctp_cwnd_socket_option) {
3725 							(*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1, cc_opt);
3726 						}
3727 						SCTP_TCB_UNLOCK(stcb);
3728 					}
3729 					SCTP_INP_RUNLOCK(inp);
3730 				} else {
3731 					error = EINVAL;
3732 				}
3733 			} else {
3734 				if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
3735 					error = ENOTSUP;
3736 				} else {
3737 					error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1,
3738 					    cc_opt);
3739 				}
3740 				SCTP_TCB_UNLOCK(stcb);
3741 			}
3742 			break;
3743 		}
3744 	case SCTP_PLUGGABLE_SS:
3745 		{
3746 			struct sctp_assoc_value *av;
3747 
3748 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3749 			if ((av->assoc_value != SCTP_SS_DEFAULT) &&
3750 			    (av->assoc_value != SCTP_SS_ROUND_ROBIN) &&
3751 			    (av->assoc_value != SCTP_SS_ROUND_ROBIN_PACKET) &&
3752 			    (av->assoc_value != SCTP_SS_PRIORITY) &&
3753 			    (av->assoc_value != SCTP_SS_FAIR_BANDWITH) &&
3754 			    (av->assoc_value != SCTP_SS_FIRST_COME)) {
3755 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3756 				error = EINVAL;
3757 				break;
3758 			}
3759 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3760 			if (stcb) {
3761 				stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
3762 				stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
3763 				stcb->asoc.stream_scheduling_module = av->assoc_value;
3764 				stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
3765 				SCTP_TCB_UNLOCK(stcb);
3766 			} else {
3767 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3768 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3769 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3770 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
3771 					SCTP_INP_WLOCK(inp);
3772 					inp->sctp_ep.sctp_default_ss_module = av->assoc_value;
3773 					SCTP_INP_WUNLOCK(inp);
3774 				}
3775 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3776 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
3777 					SCTP_INP_RLOCK(inp);
3778 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3779 						SCTP_TCB_LOCK(stcb);
3780 						stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
3781 						stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
3782 						stcb->asoc.stream_scheduling_module = av->assoc_value;
3783 						stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
3784 						SCTP_TCB_UNLOCK(stcb);
3785 					}
3786 					SCTP_INP_RUNLOCK(inp);
3787 				}
3788 			}
3789 			break;
3790 		}
3791 	case SCTP_SS_VALUE:
3792 		{
3793 			struct sctp_stream_value *av;
3794 
3795 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, optsize);
3796 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3797 			if (stcb) {
3798 				if (stcb->asoc.ss_functions.sctp_ss_set_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
3799 				    av->stream_value) < 0) {
3800 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3801 					error = EINVAL;
3802 				}
3803 				SCTP_TCB_UNLOCK(stcb);
3804 			} else {
3805 				if (av->assoc_id == SCTP_CURRENT_ASSOC) {
3806 					SCTP_INP_RLOCK(inp);
3807 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3808 						SCTP_TCB_LOCK(stcb);
3809 						stcb->asoc.ss_functions.sctp_ss_set_value(stcb,
3810 						    &stcb->asoc,
3811 						    &stcb->asoc.strmout[av->stream_id],
3812 						    av->stream_value);
3813 						SCTP_TCB_UNLOCK(stcb);
3814 					}
3815 					SCTP_INP_RUNLOCK(inp);
3816 
3817 				} else {
3818 					/*
3819 					 * Can't set stream value without
3820 					 * association
3821 					 */
3822 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3823 					error = EINVAL;
3824 				}
3825 			}
3826 			break;
3827 		}
3828 	case SCTP_CLR_STAT_LOG:
3829 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3830 		error = EOPNOTSUPP;
3831 		break;
3832 	case SCTP_CONTEXT:
3833 		{
3834 			struct sctp_assoc_value *av;
3835 
3836 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3837 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3838 
3839 			if (stcb) {
3840 				stcb->asoc.context = av->assoc_value;
3841 				SCTP_TCB_UNLOCK(stcb);
3842 			} else {
3843 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3844 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3845 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3846 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
3847 					SCTP_INP_WLOCK(inp);
3848 					inp->sctp_context = av->assoc_value;
3849 					SCTP_INP_WUNLOCK(inp);
3850 				}
3851 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3852 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
3853 					SCTP_INP_RLOCK(inp);
3854 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3855 						SCTP_TCB_LOCK(stcb);
3856 						stcb->asoc.context = av->assoc_value;
3857 						SCTP_TCB_UNLOCK(stcb);
3858 					}
3859 					SCTP_INP_RUNLOCK(inp);
3860 				}
3861 			}
3862 			break;
3863 		}
3864 	case SCTP_VRF_ID:
3865 		{
3866 			uint32_t *default_vrfid;
3867 
3868 			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
3869 			if (*default_vrfid > SCTP_MAX_VRF_ID) {
3870 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3871 				error = EINVAL;
3872 				break;
3873 			}
3874 			inp->def_vrf_id = *default_vrfid;
3875 			break;
3876 		}
3877 	case SCTP_DEL_VRF_ID:
3878 		{
3879 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3880 			error = EOPNOTSUPP;
3881 			break;
3882 		}
3883 	case SCTP_ADD_VRF_ID:
3884 		{
3885 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3886 			error = EOPNOTSUPP;
3887 			break;
3888 		}
3889 	case SCTP_DELAYED_SACK:
3890 		{
3891 			struct sctp_sack_info *sack;
3892 
3893 			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
3894 			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
3895 			if (sack->sack_delay) {
3896 				if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
3897 					sack->sack_delay = SCTP_MAX_SACK_DELAY;
3898 				if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3899 					sack->sack_delay = TICKS_TO_MSEC(1);
3900 				}
3901 			}
3902 			if (stcb) {
3903 				if (sack->sack_delay) {
3904 					stcb->asoc.delayed_ack = sack->sack_delay;
3905 				}
3906 				if (sack->sack_freq) {
3907 					stcb->asoc.sack_freq = sack->sack_freq;
3908 				}
3909 				SCTP_TCB_UNLOCK(stcb);
3910 			} else {
3911 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3912 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3913 				    (sack->sack_assoc_id == SCTP_FUTURE_ASSOC) ||
3914 				    (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
3915 					SCTP_INP_WLOCK(inp);
3916 					if (sack->sack_delay) {
3917 						inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
3918 					}
3919 					if (sack->sack_freq) {
3920 						inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
3921 					}
3922 					SCTP_INP_WUNLOCK(inp);
3923 				}
3924 				if ((sack->sack_assoc_id == SCTP_CURRENT_ASSOC) ||
3925 				    (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
3926 					SCTP_INP_RLOCK(inp);
3927 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3928 						SCTP_TCB_LOCK(stcb);
3929 						if (sack->sack_delay) {
3930 							stcb->asoc.delayed_ack = sack->sack_delay;
3931 						}
3932 						if (sack->sack_freq) {
3933 							stcb->asoc.sack_freq = sack->sack_freq;
3934 						}
3935 						SCTP_TCB_UNLOCK(stcb);
3936 					}
3937 					SCTP_INP_RUNLOCK(inp);
3938 				}
3939 			}
3940 			break;
3941 		}
3942 	case SCTP_AUTH_CHUNK:
3943 		{
3944 			struct sctp_authchunk *sauth;
3945 
3946 			SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
3947 
3948 			SCTP_INP_WLOCK(inp);
3949 			if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
3950 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3951 				error = EINVAL;
3952 			}
3953 			SCTP_INP_WUNLOCK(inp);
3954 			break;
3955 		}
3956 	case SCTP_AUTH_KEY:
3957 		{
3958 			struct sctp_authkey *sca;
3959 			struct sctp_keyhead *shared_keys;
3960 			sctp_sharedkey_t *shared_key;
3961 			sctp_key_t *key = NULL;
3962 			size_t size;
3963 
3964 			SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
3965 			if (sca->sca_keylength == 0) {
3966 				size = optsize - sizeof(struct sctp_authkey);
3967 			} else {
3968 				if (sca->sca_keylength + sizeof(struct sctp_authkey) <= optsize) {
3969 					size = sca->sca_keylength;
3970 				} else {
3971 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3972 					error = EINVAL;
3973 					break;
3974 				}
3975 			}
3976 			SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
3977 
3978 			if (stcb) {
3979 				shared_keys = &stcb->asoc.shared_keys;
3980 				/* clear the cached keys for this key id */
3981 				sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
3982 				/*
3983 				 * create the new shared key and
3984 				 * insert/replace it
3985 				 */
3986 				if (size > 0) {
3987 					key = sctp_set_key(sca->sca_key, (uint32_t) size);
3988 					if (key == NULL) {
3989 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3990 						error = ENOMEM;
3991 						SCTP_TCB_UNLOCK(stcb);
3992 						break;
3993 					}
3994 				}
3995 				shared_key = sctp_alloc_sharedkey();
3996 				if (shared_key == NULL) {
3997 					sctp_free_key(key);
3998 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3999 					error = ENOMEM;
4000 					SCTP_TCB_UNLOCK(stcb);
4001 					break;
4002 				}
4003 				shared_key->key = key;
4004 				shared_key->keyid = sca->sca_keynumber;
4005 				error = sctp_insert_sharedkey(shared_keys, shared_key);
4006 				SCTP_TCB_UNLOCK(stcb);
4007 			} else {
4008 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4009 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4010 				    (sca->sca_assoc_id == SCTP_FUTURE_ASSOC) ||
4011 				    (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4012 					SCTP_INP_WLOCK(inp);
4013 					shared_keys = &inp->sctp_ep.shared_keys;
4014 					/*
4015 					 * clear the cached keys on all
4016 					 * assocs for this key id
4017 					 */
4018 					sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
4019 					/*
4020 					 * create the new shared key and
4021 					 * insert/replace it
4022 					 */
4023 					if (size > 0) {
4024 						key = sctp_set_key(sca->sca_key, (uint32_t) size);
4025 						if (key == NULL) {
4026 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4027 							error = ENOMEM;
4028 							SCTP_INP_WUNLOCK(inp);
4029 							break;
4030 						}
4031 					}
4032 					shared_key = sctp_alloc_sharedkey();
4033 					if (shared_key == NULL) {
4034 						sctp_free_key(key);
4035 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4036 						error = ENOMEM;
4037 						SCTP_INP_WUNLOCK(inp);
4038 						break;
4039 					}
4040 					shared_key->key = key;
4041 					shared_key->keyid = sca->sca_keynumber;
4042 					error = sctp_insert_sharedkey(shared_keys, shared_key);
4043 					SCTP_INP_WUNLOCK(inp);
4044 				}
4045 				if ((sca->sca_assoc_id == SCTP_CURRENT_ASSOC) ||
4046 				    (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4047 					SCTP_INP_RLOCK(inp);
4048 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4049 						SCTP_TCB_LOCK(stcb);
4050 						shared_keys = &stcb->asoc.shared_keys;
4051 						/*
4052 						 * clear the cached keys for
4053 						 * this key id
4054 						 */
4055 						sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4056 						/*
4057 						 * create the new shared key
4058 						 * and insert/replace it
4059 						 */
4060 						if (size > 0) {
4061 							key = sctp_set_key(sca->sca_key, (uint32_t) size);
4062 							if (key == NULL) {
4063 								SCTP_TCB_UNLOCK(stcb);
4064 								continue;
4065 							}
4066 						}
4067 						shared_key = sctp_alloc_sharedkey();
4068 						if (shared_key == NULL) {
4069 							sctp_free_key(key);
4070 							SCTP_TCB_UNLOCK(stcb);
4071 							continue;
4072 						}
4073 						shared_key->key = key;
4074 						shared_key->keyid = sca->sca_keynumber;
4075 						error = sctp_insert_sharedkey(shared_keys, shared_key);
4076 						SCTP_TCB_UNLOCK(stcb);
4077 					}
4078 					SCTP_INP_RUNLOCK(inp);
4079 				}
4080 			}
4081 			break;
4082 		}
4083 	case SCTP_HMAC_IDENT:
4084 		{
4085 			struct sctp_hmacalgo *shmac;
4086 			sctp_hmaclist_t *hmaclist;
4087 			uint16_t hmacid;
4088 			uint32_t i;
4089 
4090 			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
4091 			if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) {
4092 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4093 				error = EINVAL;
4094 				break;
4095 			}
4096 			hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents);
4097 			if (hmaclist == NULL) {
4098 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4099 				error = ENOMEM;
4100 				break;
4101 			}
4102 			for (i = 0; i < shmac->shmac_number_of_idents; i++) {
4103 				hmacid = shmac->shmac_idents[i];
4104 				if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
4105 					 /* invalid HMACs were found */ ;
4106 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4107 					error = EINVAL;
4108 					sctp_free_hmaclist(hmaclist);
4109 					goto sctp_set_hmac_done;
4110 				}
4111 			}
4112 			for (i = 0; i < hmaclist->num_algo; i++) {
4113 				if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
4114 					/* already in list */
4115 					break;
4116 				}
4117 			}
4118 			if (i == hmaclist->num_algo) {
4119 				/* not found in list */
4120 				sctp_free_hmaclist(hmaclist);
4121 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4122 				error = EINVAL;
4123 				break;
4124 			}
4125 			/* set it on the endpoint */
4126 			SCTP_INP_WLOCK(inp);
4127 			if (inp->sctp_ep.local_hmacs)
4128 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4129 			inp->sctp_ep.local_hmacs = hmaclist;
4130 			SCTP_INP_WUNLOCK(inp);
4131 	sctp_set_hmac_done:
4132 			break;
4133 		}
4134 	case SCTP_AUTH_ACTIVE_KEY:
4135 		{
4136 			struct sctp_authkeyid *scact;
4137 
4138 			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
4139 			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
4140 
4141 			/* set the active key on the right place */
4142 			if (stcb) {
4143 				/* set the active key on the assoc */
4144 				if (sctp_auth_setactivekey(stcb,
4145 				    scact->scact_keynumber)) {
4146 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
4147 					    SCTP_FROM_SCTP_USRREQ,
4148 					    EINVAL);
4149 					error = EINVAL;
4150 				}
4151 				SCTP_TCB_UNLOCK(stcb);
4152 			} else {
4153 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4154 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4155 				    (scact->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4156 				    (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4157 					SCTP_INP_WLOCK(inp);
4158 					if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) {
4159 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4160 						error = EINVAL;
4161 					}
4162 					SCTP_INP_WUNLOCK(inp);
4163 				}
4164 				if ((scact->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4165 				    (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4166 					SCTP_INP_RLOCK(inp);
4167 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4168 						SCTP_TCB_LOCK(stcb);
4169 						sctp_auth_setactivekey(stcb, scact->scact_keynumber);
4170 						SCTP_TCB_UNLOCK(stcb);
4171 					}
4172 					SCTP_INP_RUNLOCK(inp);
4173 				}
4174 			}
4175 			break;
4176 		}
4177 	case SCTP_AUTH_DELETE_KEY:
4178 		{
4179 			struct sctp_authkeyid *scdel;
4180 
4181 			SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
4182 			SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
4183 
4184 			/* delete the key from the right place */
4185 			if (stcb) {
4186 				if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) {
4187 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4188 					error = EINVAL;
4189 				}
4190 				SCTP_TCB_UNLOCK(stcb);
4191 			} else {
4192 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4193 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4194 				    (scdel->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4195 				    (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4196 					SCTP_INP_WLOCK(inp);
4197 					if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) {
4198 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4199 						error = EINVAL;
4200 					}
4201 					SCTP_INP_WUNLOCK(inp);
4202 				}
4203 				if ((scdel->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4204 				    (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4205 					SCTP_INP_RLOCK(inp);
4206 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4207 						SCTP_TCB_LOCK(stcb);
4208 						sctp_delete_sharedkey(stcb, scdel->scact_keynumber);
4209 						SCTP_TCB_UNLOCK(stcb);
4210 					}
4211 					SCTP_INP_RUNLOCK(inp);
4212 				}
4213 			}
4214 			break;
4215 		}
4216 	case SCTP_AUTH_DEACTIVATE_KEY:
4217 		{
4218 			struct sctp_authkeyid *keyid;
4219 
4220 			SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid, optsize);
4221 			SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
4222 
4223 			/* deactivate the key from the right place */
4224 			if (stcb) {
4225 				if (sctp_deact_sharedkey(stcb, keyid->scact_keynumber)) {
4226 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4227 					error = EINVAL;
4228 				}
4229 				SCTP_TCB_UNLOCK(stcb);
4230 			} else {
4231 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4232 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4233 				    (keyid->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4234 				    (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4235 					SCTP_INP_WLOCK(inp);
4236 					if (sctp_deact_sharedkey_ep(inp, keyid->scact_keynumber)) {
4237 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4238 						error = EINVAL;
4239 					}
4240 					SCTP_INP_WUNLOCK(inp);
4241 				}
4242 				if ((keyid->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4243 				    (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4244 					SCTP_INP_RLOCK(inp);
4245 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4246 						SCTP_TCB_LOCK(stcb);
4247 						sctp_deact_sharedkey(stcb, keyid->scact_keynumber);
4248 						SCTP_TCB_UNLOCK(stcb);
4249 					}
4250 					SCTP_INP_RUNLOCK(inp);
4251 				}
4252 			}
4253 			break;
4254 		}
4255 	case SCTP_ENABLE_STREAM_RESET:
4256 		{
4257 			struct sctp_assoc_value *av;
4258 
4259 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4260 			if (av->assoc_value & (~SCTP_ENABLE_VALUE_MASK)) {
4261 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4262 				error = EINVAL;
4263 				break;
4264 			}
4265 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4266 			if (stcb) {
4267 				stcb->asoc.local_strreset_support = (uint8_t) av->assoc_value;
4268 				SCTP_TCB_UNLOCK(stcb);
4269 			} else {
4270 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4271 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4272 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4273 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4274 					SCTP_INP_WLOCK(inp);
4275 					inp->local_strreset_support = (uint8_t) av->assoc_value;
4276 					SCTP_INP_WUNLOCK(inp);
4277 				}
4278 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4279 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4280 					SCTP_INP_RLOCK(inp);
4281 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4282 						SCTP_TCB_LOCK(stcb);
4283 						stcb->asoc.local_strreset_support = (uint8_t) av->assoc_value;
4284 						SCTP_TCB_UNLOCK(stcb);
4285 					}
4286 					SCTP_INP_RUNLOCK(inp);
4287 				}
4288 			}
4289 			break;
4290 		}
4291 	case SCTP_RESET_STREAMS:
4292 		{
4293 			struct sctp_reset_streams *strrst;
4294 			int i, send_out = 0;
4295 			int send_in = 0;
4296 
4297 			SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_reset_streams, optsize);
4298 			SCTP_FIND_STCB(inp, stcb, strrst->srs_assoc_id);
4299 			if (stcb == NULL) {
4300 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4301 				error = ENOENT;
4302 				break;
4303 			}
4304 			if (stcb->asoc.reconfig_supported == 0) {
4305 				/*
4306 				 * Peer does not support the chunk type.
4307 				 */
4308 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4309 				error = EOPNOTSUPP;
4310 				SCTP_TCB_UNLOCK(stcb);
4311 				break;
4312 			}
4313 			if (stcb->asoc.stream_reset_outstanding) {
4314 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4315 				error = EALREADY;
4316 				SCTP_TCB_UNLOCK(stcb);
4317 				break;
4318 			}
4319 			if (strrst->srs_flags & SCTP_STREAM_RESET_INCOMING) {
4320 				send_in = 1;
4321 			}
4322 			if (strrst->srs_flags & SCTP_STREAM_RESET_OUTGOING) {
4323 				send_out = 1;
4324 			}
4325 			if ((send_in == 0) && (send_out == 0)) {
4326 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4327 				error = EINVAL;
4328 				SCTP_TCB_UNLOCK(stcb);
4329 				break;
4330 			}
4331 			for (i = 0; i < strrst->srs_number_streams; i++) {
4332 				if ((send_in) &&
4333 				    (strrst->srs_stream_list[i] > stcb->asoc.streamincnt)) {
4334 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4335 					error = EINVAL;
4336 					break;
4337 				}
4338 				if ((send_out) &&
4339 				    (strrst->srs_stream_list[i] > stcb->asoc.streamoutcnt)) {
4340 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4341 					error = EINVAL;
4342 					break;
4343 				}
4344 			}
4345 			if (error) {
4346 				SCTP_TCB_UNLOCK(stcb);
4347 				break;
4348 			}
4349 			error = sctp_send_str_reset_req(stcb, strrst->srs_number_streams,
4350 			    strrst->srs_stream_list,
4351 			    send_out, send_in, 0, 0, 0, 0, 0);
4352 
4353 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4354 			SCTP_TCB_UNLOCK(stcb);
4355 			break;
4356 		}
4357 	case SCTP_ADD_STREAMS:
4358 		{
4359 			struct sctp_add_streams *stradd;
4360 			uint8_t addstream = 0;
4361 			uint16_t add_o_strmcnt = 0;
4362 			uint16_t add_i_strmcnt = 0;
4363 
4364 			SCTP_CHECK_AND_CAST(stradd, optval, struct sctp_add_streams, optsize);
4365 			SCTP_FIND_STCB(inp, stcb, stradd->sas_assoc_id);
4366 			if (stcb == NULL) {
4367 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4368 				error = ENOENT;
4369 				break;
4370 			}
4371 			if (stcb->asoc.reconfig_supported == 0) {
4372 				/*
4373 				 * Peer does not support the chunk type.
4374 				 */
4375 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4376 				error = EOPNOTSUPP;
4377 				SCTP_TCB_UNLOCK(stcb);
4378 				break;
4379 			}
4380 			if (stcb->asoc.stream_reset_outstanding) {
4381 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4382 				error = EALREADY;
4383 				SCTP_TCB_UNLOCK(stcb);
4384 				break;
4385 			}
4386 			if ((stradd->sas_outstrms == 0) &&
4387 			    (stradd->sas_instrms == 0)) {
4388 				error = EINVAL;
4389 				goto skip_stuff;
4390 			}
4391 			if (stradd->sas_outstrms) {
4392 				addstream = 1;
4393 				/* We allocate here */
4394 				add_o_strmcnt = stradd->sas_outstrms;
4395 				if ((((int)add_o_strmcnt) + ((int)stcb->asoc.streamoutcnt)) > 0x0000ffff) {
4396 					/* You can't have more than 64k */
4397 					error = EINVAL;
4398 					goto skip_stuff;
4399 				}
4400 			}
4401 			if (stradd->sas_instrms) {
4402 				int cnt;
4403 
4404 				addstream |= 2;
4405 				/*
4406 				 * We allocate inside
4407 				 * sctp_send_str_reset_req()
4408 				 */
4409 				add_i_strmcnt = stradd->sas_instrms;
4410 				cnt = add_i_strmcnt;
4411 				cnt += stcb->asoc.streamincnt;
4412 				if (cnt > 0x0000ffff) {
4413 					/* You can't have more than 64k */
4414 					error = EINVAL;
4415 					goto skip_stuff;
4416 				}
4417 				if (cnt > (int)stcb->asoc.max_inbound_streams) {
4418 					/* More than you are allowed */
4419 					error = EINVAL;
4420 					goto skip_stuff;
4421 				}
4422 			}
4423 			error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, addstream, add_o_strmcnt, add_i_strmcnt, 0);
4424 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4425 	skip_stuff:
4426 			SCTP_TCB_UNLOCK(stcb);
4427 			break;
4428 		}
4429 	case SCTP_RESET_ASSOC:
4430 		{
4431 			uint32_t *value;
4432 
4433 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
4434 			SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
4435 			if (stcb == NULL) {
4436 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4437 				error = ENOENT;
4438 				break;
4439 			}
4440 			if (stcb->asoc.reconfig_supported == 0) {
4441 				/*
4442 				 * Peer does not support the chunk type.
4443 				 */
4444 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4445 				error = EOPNOTSUPP;
4446 				SCTP_TCB_UNLOCK(stcb);
4447 				break;
4448 			}
4449 			if (stcb->asoc.stream_reset_outstanding) {
4450 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4451 				error = EALREADY;
4452 				SCTP_TCB_UNLOCK(stcb);
4453 				break;
4454 			}
4455 			error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, 0, 0, 0, 0);
4456 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4457 			SCTP_TCB_UNLOCK(stcb);
4458 			break;
4459 		}
4460 	case SCTP_CONNECT_X:
4461 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4462 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4463 			error = EINVAL;
4464 			break;
4465 		}
4466 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
4467 		break;
4468 	case SCTP_CONNECT_X_DELAYED:
4469 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4470 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4471 			error = EINVAL;
4472 			break;
4473 		}
4474 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
4475 		break;
4476 	case SCTP_CONNECT_X_COMPLETE:
4477 		{
4478 			struct sockaddr *sa;
4479 			struct sctp_nets *net;
4480 
4481 			/* FIXME MT: check correct? */
4482 			SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
4483 
4484 			/* find tcb */
4485 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4486 				SCTP_INP_RLOCK(inp);
4487 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
4488 				if (stcb) {
4489 					SCTP_TCB_LOCK(stcb);
4490 					net = sctp_findnet(stcb, sa);
4491 				}
4492 				SCTP_INP_RUNLOCK(inp);
4493 			} else {
4494 				/*
4495 				 * We increment here since
4496 				 * sctp_findassociation_ep_addr() wil do a
4497 				 * decrement if it finds the stcb as long as
4498 				 * the locked tcb (last argument) is NOT a
4499 				 * TCB.. aka NULL.
4500 				 */
4501 				SCTP_INP_INCR_REF(inp);
4502 				stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
4503 				if (stcb == NULL) {
4504 					SCTP_INP_DECR_REF(inp);
4505 				}
4506 			}
4507 
4508 			if (stcb == NULL) {
4509 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4510 				error = ENOENT;
4511 				break;
4512 			}
4513 			if (stcb->asoc.delayed_connection == 1) {
4514 				stcb->asoc.delayed_connection = 0;
4515 				(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4516 				sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
4517 				    stcb->asoc.primary_destination,
4518 				    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
4519 				sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4520 			} else {
4521 				/*
4522 				 * already expired or did not use delayed
4523 				 * connectx
4524 				 */
4525 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4526 				error = EALREADY;
4527 			}
4528 			SCTP_TCB_UNLOCK(stcb);
4529 			break;
4530 		}
4531 	case SCTP_MAX_BURST:
4532 		{
4533 			struct sctp_assoc_value *av;
4534 
4535 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4536 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4537 
4538 			if (stcb) {
4539 				stcb->asoc.max_burst = av->assoc_value;
4540 				SCTP_TCB_UNLOCK(stcb);
4541 			} else {
4542 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4543 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4544 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4545 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4546 					SCTP_INP_WLOCK(inp);
4547 					inp->sctp_ep.max_burst = av->assoc_value;
4548 					SCTP_INP_WUNLOCK(inp);
4549 				}
4550 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4551 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4552 					SCTP_INP_RLOCK(inp);
4553 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4554 						SCTP_TCB_LOCK(stcb);
4555 						stcb->asoc.max_burst = av->assoc_value;
4556 						SCTP_TCB_UNLOCK(stcb);
4557 					}
4558 					SCTP_INP_RUNLOCK(inp);
4559 				}
4560 			}
4561 			break;
4562 		}
4563 	case SCTP_MAXSEG:
4564 		{
4565 			struct sctp_assoc_value *av;
4566 			int ovh;
4567 
4568 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4569 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4570 
4571 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4572 				ovh = SCTP_MED_OVERHEAD;
4573 			} else {
4574 				ovh = SCTP_MED_V4_OVERHEAD;
4575 			}
4576 			if (stcb) {
4577 				if (av->assoc_value) {
4578 					stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
4579 				} else {
4580 					stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4581 				}
4582 				SCTP_TCB_UNLOCK(stcb);
4583 			} else {
4584 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4585 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4586 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4587 					SCTP_INP_WLOCK(inp);
4588 					/*
4589 					 * FIXME MT: I think this is not in
4590 					 * tune with the API ID
4591 					 */
4592 					if (av->assoc_value) {
4593 						inp->sctp_frag_point = (av->assoc_value + ovh);
4594 					} else {
4595 						inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4596 					}
4597 					SCTP_INP_WUNLOCK(inp);
4598 				} else {
4599 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4600 					error = EINVAL;
4601 				}
4602 			}
4603 			break;
4604 		}
4605 	case SCTP_EVENTS:
4606 		{
4607 			struct sctp_event_subscribe *events;
4608 
4609 			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
4610 
4611 			SCTP_INP_WLOCK(inp);
4612 			if (events->sctp_data_io_event) {
4613 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
4614 			} else {
4615 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
4616 			}
4617 
4618 			if (events->sctp_association_event) {
4619 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4620 			} else {
4621 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4622 			}
4623 
4624 			if (events->sctp_address_event) {
4625 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
4626 			} else {
4627 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
4628 			}
4629 
4630 			if (events->sctp_send_failure_event) {
4631 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4632 			} else {
4633 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4634 			}
4635 
4636 			if (events->sctp_peer_error_event) {
4637 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
4638 			} else {
4639 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
4640 			}
4641 
4642 			if (events->sctp_shutdown_event) {
4643 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4644 			} else {
4645 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4646 			}
4647 
4648 			if (events->sctp_partial_delivery_event) {
4649 				sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
4650 			} else {
4651 				sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
4652 			}
4653 
4654 			if (events->sctp_adaptation_layer_event) {
4655 				sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4656 			} else {
4657 				sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4658 			}
4659 
4660 			if (events->sctp_authentication_event) {
4661 				sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
4662 			} else {
4663 				sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
4664 			}
4665 
4666 			if (events->sctp_sender_dry_event) {
4667 				sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
4668 			} else {
4669 				sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
4670 			}
4671 
4672 			if (events->sctp_stream_reset_event) {
4673 				sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
4674 			} else {
4675 				sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
4676 			}
4677 			SCTP_INP_WUNLOCK(inp);
4678 
4679 			SCTP_INP_RLOCK(inp);
4680 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4681 				SCTP_TCB_LOCK(stcb);
4682 				if (events->sctp_association_event) {
4683 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4684 				} else {
4685 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4686 				}
4687 				if (events->sctp_address_event) {
4688 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
4689 				} else {
4690 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
4691 				}
4692 				if (events->sctp_send_failure_event) {
4693 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4694 				} else {
4695 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4696 				}
4697 				if (events->sctp_peer_error_event) {
4698 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
4699 				} else {
4700 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
4701 				}
4702 				if (events->sctp_shutdown_event) {
4703 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4704 				} else {
4705 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4706 				}
4707 				if (events->sctp_partial_delivery_event) {
4708 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
4709 				} else {
4710 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
4711 				}
4712 				if (events->sctp_adaptation_layer_event) {
4713 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4714 				} else {
4715 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4716 				}
4717 				if (events->sctp_authentication_event) {
4718 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
4719 				} else {
4720 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
4721 				}
4722 				if (events->sctp_sender_dry_event) {
4723 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
4724 				} else {
4725 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
4726 				}
4727 				if (events->sctp_stream_reset_event) {
4728 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
4729 				} else {
4730 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
4731 				}
4732 				SCTP_TCB_UNLOCK(stcb);
4733 			}
4734 			/*
4735 			 * Send up the sender dry event only for 1-to-1
4736 			 * style sockets.
4737 			 */
4738 			if (events->sctp_sender_dry_event) {
4739 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4740 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4741 					stcb = LIST_FIRST(&inp->sctp_asoc_list);
4742 					if (stcb) {
4743 						SCTP_TCB_LOCK(stcb);
4744 						if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4745 						    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4746 						    (stcb->asoc.stream_queue_cnt == 0)) {
4747 							sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
4748 						}
4749 						SCTP_TCB_UNLOCK(stcb);
4750 					}
4751 				}
4752 			}
4753 			SCTP_INP_RUNLOCK(inp);
4754 			break;
4755 		}
4756 	case SCTP_ADAPTATION_LAYER:
4757 		{
4758 			struct sctp_setadaptation *adap_bits;
4759 
4760 			SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
4761 			SCTP_INP_WLOCK(inp);
4762 			inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
4763 			inp->sctp_ep.adaptation_layer_indicator_provided = 1;
4764 			SCTP_INP_WUNLOCK(inp);
4765 			break;
4766 		}
4767 #ifdef SCTP_DEBUG
4768 	case SCTP_SET_INITIAL_DBG_SEQ:
4769 		{
4770 			uint32_t *vvv;
4771 
4772 			SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
4773 			SCTP_INP_WLOCK(inp);
4774 			inp->sctp_ep.initial_sequence_debug = *vvv;
4775 			SCTP_INP_WUNLOCK(inp);
4776 			break;
4777 		}
4778 #endif
4779 	case SCTP_DEFAULT_SEND_PARAM:
4780 		{
4781 			struct sctp_sndrcvinfo *s_info;
4782 
4783 			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
4784 			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
4785 
4786 			if (stcb) {
4787 				if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
4788 					memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
4789 				} else {
4790 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4791 					error = EINVAL;
4792 				}
4793 				SCTP_TCB_UNLOCK(stcb);
4794 			} else {
4795 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4796 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4797 				    (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC) ||
4798 				    (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
4799 					SCTP_INP_WLOCK(inp);
4800 					memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
4801 					SCTP_INP_WUNLOCK(inp);
4802 				}
4803 				if ((s_info->sinfo_assoc_id == SCTP_CURRENT_ASSOC) ||
4804 				    (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
4805 					SCTP_INP_RLOCK(inp);
4806 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4807 						SCTP_TCB_LOCK(stcb);
4808 						if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
4809 							memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
4810 						}
4811 						SCTP_TCB_UNLOCK(stcb);
4812 					}
4813 					SCTP_INP_RUNLOCK(inp);
4814 				}
4815 			}
4816 			break;
4817 		}
4818 	case SCTP_PEER_ADDR_PARAMS:
4819 		{
4820 			struct sctp_paddrparams *paddrp;
4821 			struct sctp_nets *net;
4822 
4823 			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
4824 			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
4825 			net = NULL;
4826 			if (stcb) {
4827 				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
4828 			} else {
4829 				/*
4830 				 * We increment here since
4831 				 * sctp_findassociation_ep_addr() wil do a
4832 				 * decrement if it finds the stcb as long as
4833 				 * the locked tcb (last argument) is NOT a
4834 				 * TCB.. aka NULL.
4835 				 */
4836 				SCTP_INP_INCR_REF(inp);
4837 				stcb = sctp_findassociation_ep_addr(&inp,
4838 				    (struct sockaddr *)&paddrp->spp_address,
4839 				    &net, NULL, NULL);
4840 				if (stcb == NULL) {
4841 					SCTP_INP_DECR_REF(inp);
4842 				}
4843 			}
4844 			if (stcb && (net == NULL)) {
4845 				struct sockaddr *sa;
4846 
4847 				sa = (struct sockaddr *)&paddrp->spp_address;
4848 #ifdef INET
4849 				if (sa->sa_family == AF_INET) {
4850 
4851 					struct sockaddr_in *sin;
4852 
4853 					sin = (struct sockaddr_in *)sa;
4854 					if (sin->sin_addr.s_addr) {
4855 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4856 						SCTP_TCB_UNLOCK(stcb);
4857 						error = EINVAL;
4858 						break;
4859 					}
4860 				} else
4861 #endif
4862 #ifdef INET6
4863 				if (sa->sa_family == AF_INET6) {
4864 					struct sockaddr_in6 *sin6;
4865 
4866 					sin6 = (struct sockaddr_in6 *)sa;
4867 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
4868 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4869 						SCTP_TCB_UNLOCK(stcb);
4870 						error = EINVAL;
4871 						break;
4872 					}
4873 				} else
4874 #endif
4875 				{
4876 					error = EAFNOSUPPORT;
4877 					SCTP_TCB_UNLOCK(stcb);
4878 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4879 					break;
4880 				}
4881 			}
4882 			/* sanity checks */
4883 			if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
4884 				if (stcb)
4885 					SCTP_TCB_UNLOCK(stcb);
4886 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4887 				return (EINVAL);
4888 			}
4889 			if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
4890 				if (stcb)
4891 					SCTP_TCB_UNLOCK(stcb);
4892 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4893 				return (EINVAL);
4894 			}
4895 			if (stcb) {
4896 				/************************TCB SPECIFIC SET ******************/
4897 				/*
4898 				 * do we change the timer for HB, we run
4899 				 * only one?
4900 				 */
4901 				int ovh = 0;
4902 
4903 				if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4904 					ovh = SCTP_MED_OVERHEAD;
4905 				} else {
4906 					ovh = SCTP_MED_V4_OVERHEAD;
4907 				}
4908 
4909 				/* network sets ? */
4910 				if (net) {
4911 					/************************NET SPECIFIC SET ******************/
4912 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
4913 						if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
4914 						    !(net->dest_state & SCTP_ADDR_NOHB)) {
4915 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
4916 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
4917 						}
4918 						net->dest_state |= SCTP_ADDR_NOHB;
4919 					}
4920 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
4921 						if (paddrp->spp_hbinterval) {
4922 							net->heart_beat_delay = paddrp->spp_hbinterval;
4923 						} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
4924 							net->heart_beat_delay = 0;
4925 						}
4926 						sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
4927 						    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
4928 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
4929 						net->dest_state &= ~SCTP_ADDR_NOHB;
4930 					}
4931 					if (paddrp->spp_flags & SPP_HB_DEMAND) {
4932 						/* on demand HB */
4933 						sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
4934 						sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
4935 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
4936 					}
4937 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
4938 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
4939 							sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
4940 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
4941 						}
4942 						net->dest_state |= SCTP_ADDR_NO_PMTUD;
4943 						net->mtu = paddrp->spp_pathmtu + ovh;
4944 						if (net->mtu < stcb->asoc.smallest_mtu) {
4945 							sctp_pathmtu_adjustment(stcb, net->mtu);
4946 						}
4947 					}
4948 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
4949 						if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
4950 							sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
4951 						}
4952 						net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
4953 					}
4954 					if (paddrp->spp_pathmaxrxt) {
4955 						if (net->dest_state & SCTP_ADDR_PF) {
4956 							if (net->error_count > paddrp->spp_pathmaxrxt) {
4957 								net->dest_state &= ~SCTP_ADDR_PF;
4958 							}
4959 						} else {
4960 							if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
4961 							    (net->error_count > net->pf_threshold)) {
4962 								net->dest_state |= SCTP_ADDR_PF;
4963 								sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
4964 								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
4965 								sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4966 							}
4967 						}
4968 						if (net->dest_state & SCTP_ADDR_REACHABLE) {
4969 							if (net->error_count > paddrp->spp_pathmaxrxt) {
4970 								net->dest_state &= ~SCTP_ADDR_REACHABLE;
4971 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
4972 							}
4973 						} else {
4974 							if (net->error_count <= paddrp->spp_pathmaxrxt) {
4975 								net->dest_state |= SCTP_ADDR_REACHABLE;
4976 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
4977 							}
4978 						}
4979 						net->failure_threshold = paddrp->spp_pathmaxrxt;
4980 					}
4981 					if (paddrp->spp_flags & SPP_DSCP) {
4982 						net->dscp = paddrp->spp_dscp & 0xfc;
4983 						net->dscp |= 0x01;
4984 					}
4985 #ifdef INET6
4986 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
4987 						if (net->ro._l_addr.sa.sa_family == AF_INET6) {
4988 							net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
4989 							net->flowlabel |= 0x80000000;
4990 						}
4991 					}
4992 #endif
4993 				} else {
4994 					/************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
4995 					if (paddrp->spp_pathmaxrxt) {
4996 						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
4997 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4998 							if (net->dest_state & SCTP_ADDR_PF) {
4999 								if (net->error_count > paddrp->spp_pathmaxrxt) {
5000 									net->dest_state &= ~SCTP_ADDR_PF;
5001 								}
5002 							} else {
5003 								if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5004 								    (net->error_count > net->pf_threshold)) {
5005 									net->dest_state |= SCTP_ADDR_PF;
5006 									sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5007 									sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
5008 									sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5009 								}
5010 							}
5011 							if (net->dest_state & SCTP_ADDR_REACHABLE) {
5012 								if (net->error_count > paddrp->spp_pathmaxrxt) {
5013 									net->dest_state &= ~SCTP_ADDR_REACHABLE;
5014 									sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5015 								}
5016 							} else {
5017 								if (net->error_count <= paddrp->spp_pathmaxrxt) {
5018 									net->dest_state |= SCTP_ADDR_REACHABLE;
5019 									sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5020 								}
5021 							}
5022 							net->failure_threshold = paddrp->spp_pathmaxrxt;
5023 						}
5024 					}
5025 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
5026 						if (paddrp->spp_hbinterval) {
5027 							stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
5028 						} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5029 							stcb->asoc.heart_beat_delay = 0;
5030 						}
5031 						/* Turn back on the timer */
5032 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5033 							if (paddrp->spp_hbinterval) {
5034 								net->heart_beat_delay = paddrp->spp_hbinterval;
5035 							} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5036 								net->heart_beat_delay = 0;
5037 							}
5038 							if (net->dest_state & SCTP_ADDR_NOHB) {
5039 								net->dest_state &= ~SCTP_ADDR_NOHB;
5040 							}
5041 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5042 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5043 							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5044 						}
5045 						sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5046 					}
5047 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
5048 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5049 							if (!(net->dest_state & SCTP_ADDR_NOHB)) {
5050 								net->dest_state |= SCTP_ADDR_NOHB;
5051 								if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5052 									sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5053 								}
5054 							}
5055 						}
5056 						sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5057 					}
5058 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5059 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5060 							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5061 								sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5062 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5063 							}
5064 							net->dest_state |= SCTP_ADDR_NO_PMTUD;
5065 							net->mtu = paddrp->spp_pathmtu + ovh;
5066 							if (net->mtu < stcb->asoc.smallest_mtu) {
5067 								sctp_pathmtu_adjustment(stcb, net->mtu);
5068 							}
5069 						}
5070 						sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5071 					}
5072 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5073 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5074 							if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5075 								sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5076 							}
5077 							net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5078 						}
5079 						sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5080 					}
5081 					if (paddrp->spp_flags & SPP_DSCP) {
5082 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5083 							net->dscp = paddrp->spp_dscp & 0xfc;
5084 							net->dscp |= 0x01;
5085 						}
5086 						stcb->asoc.default_dscp = paddrp->spp_dscp & 0xfc;
5087 						stcb->asoc.default_dscp |= 0x01;
5088 					}
5089 #ifdef INET6
5090 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5091 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5092 							if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5093 								net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5094 								net->flowlabel |= 0x80000000;
5095 							}
5096 						}
5097 						stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5098 						stcb->asoc.default_flowlabel |= 0x80000000;
5099 					}
5100 #endif
5101 				}
5102 				SCTP_TCB_UNLOCK(stcb);
5103 			} else {
5104 				/************************NO TCB, SET TO default stuff ******************/
5105 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5106 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5107 				    (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
5108 					SCTP_INP_WLOCK(inp);
5109 					/*
5110 					 * For the TOS/FLOWLABEL stuff you
5111 					 * set it with the options on the
5112 					 * socket
5113 					 */
5114 					if (paddrp->spp_pathmaxrxt) {
5115 						inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
5116 					}
5117 					if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
5118 						inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5119 					else if (paddrp->spp_hbinterval) {
5120 						if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
5121 							paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
5122 						inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5123 					}
5124 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
5125 						if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5126 							inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5127 						} else if (paddrp->spp_hbinterval) {
5128 							inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5129 						}
5130 						sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5131 					} else if (paddrp->spp_flags & SPP_HB_DISABLE) {
5132 						sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5133 					}
5134 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5135 						sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5136 					} else if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
5137 						sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5138 					}
5139 					if (paddrp->spp_flags & SPP_DSCP) {
5140 						inp->sctp_ep.default_dscp = paddrp->spp_dscp & 0xfc;
5141 						inp->sctp_ep.default_dscp |= 0x01;
5142 					}
5143 #ifdef INET6
5144 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5145 						if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5146 							inp->sctp_ep.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5147 							inp->sctp_ep.default_flowlabel |= 0x80000000;
5148 						}
5149 					}
5150 #endif
5151 					SCTP_INP_WUNLOCK(inp);
5152 				} else {
5153 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5154 					error = EINVAL;
5155 				}
5156 			}
5157 			break;
5158 		}
5159 	case SCTP_RTOINFO:
5160 		{
5161 			struct sctp_rtoinfo *srto;
5162 			uint32_t new_init, new_min, new_max;
5163 
5164 			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
5165 			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
5166 
5167 			if (stcb) {
5168 				if (srto->srto_initial)
5169 					new_init = srto->srto_initial;
5170 				else
5171 					new_init = stcb->asoc.initial_rto;
5172 				if (srto->srto_max)
5173 					new_max = srto->srto_max;
5174 				else
5175 					new_max = stcb->asoc.maxrto;
5176 				if (srto->srto_min)
5177 					new_min = srto->srto_min;
5178 				else
5179 					new_min = stcb->asoc.minrto;
5180 				if ((new_min <= new_init) && (new_init <= new_max)) {
5181 					stcb->asoc.initial_rto = new_init;
5182 					stcb->asoc.maxrto = new_max;
5183 					stcb->asoc.minrto = new_min;
5184 				} else {
5185 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5186 					error = EINVAL;
5187 				}
5188 				SCTP_TCB_UNLOCK(stcb);
5189 			} else {
5190 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5191 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5192 				    (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
5193 					SCTP_INP_WLOCK(inp);
5194 					if (srto->srto_initial)
5195 						new_init = srto->srto_initial;
5196 					else
5197 						new_init = inp->sctp_ep.initial_rto;
5198 					if (srto->srto_max)
5199 						new_max = srto->srto_max;
5200 					else
5201 						new_max = inp->sctp_ep.sctp_maxrto;
5202 					if (srto->srto_min)
5203 						new_min = srto->srto_min;
5204 					else
5205 						new_min = inp->sctp_ep.sctp_minrto;
5206 					if ((new_min <= new_init) && (new_init <= new_max)) {
5207 						inp->sctp_ep.initial_rto = new_init;
5208 						inp->sctp_ep.sctp_maxrto = new_max;
5209 						inp->sctp_ep.sctp_minrto = new_min;
5210 					} else {
5211 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5212 						error = EINVAL;
5213 					}
5214 					SCTP_INP_WUNLOCK(inp);
5215 				} else {
5216 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5217 					error = EINVAL;
5218 				}
5219 			}
5220 			break;
5221 		}
5222 	case SCTP_ASSOCINFO:
5223 		{
5224 			struct sctp_assocparams *sasoc;
5225 
5226 			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
5227 			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
5228 			if (sasoc->sasoc_cookie_life) {
5229 				/* boundary check the cookie life */
5230 				if (sasoc->sasoc_cookie_life < 1000)
5231 					sasoc->sasoc_cookie_life = 1000;
5232 				if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
5233 					sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
5234 				}
5235 			}
5236 			if (stcb) {
5237 				if (sasoc->sasoc_asocmaxrxt)
5238 					stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
5239 				if (sasoc->sasoc_cookie_life) {
5240 					stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5241 				}
5242 				SCTP_TCB_UNLOCK(stcb);
5243 			} else {
5244 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5245 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5246 				    (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
5247 					SCTP_INP_WLOCK(inp);
5248 					if (sasoc->sasoc_asocmaxrxt)
5249 						inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
5250 					if (sasoc->sasoc_cookie_life) {
5251 						inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5252 					}
5253 					SCTP_INP_WUNLOCK(inp);
5254 				} else {
5255 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5256 					error = EINVAL;
5257 				}
5258 			}
5259 			break;
5260 		}
5261 	case SCTP_INITMSG:
5262 		{
5263 			struct sctp_initmsg *sinit;
5264 
5265 			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
5266 			SCTP_INP_WLOCK(inp);
5267 			if (sinit->sinit_num_ostreams)
5268 				inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
5269 
5270 			if (sinit->sinit_max_instreams)
5271 				inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
5272 
5273 			if (sinit->sinit_max_attempts)
5274 				inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
5275 
5276 			if (sinit->sinit_max_init_timeo)
5277 				inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
5278 			SCTP_INP_WUNLOCK(inp);
5279 			break;
5280 		}
5281 	case SCTP_PRIMARY_ADDR:
5282 		{
5283 			struct sctp_setprim *spa;
5284 			struct sctp_nets *net;
5285 
5286 			SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
5287 			SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
5288 
5289 			net = NULL;
5290 			if (stcb) {
5291 				net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
5292 			} else {
5293 				/*
5294 				 * We increment here since
5295 				 * sctp_findassociation_ep_addr() wil do a
5296 				 * decrement if it finds the stcb as long as
5297 				 * the locked tcb (last argument) is NOT a
5298 				 * TCB.. aka NULL.
5299 				 */
5300 				SCTP_INP_INCR_REF(inp);
5301 				stcb = sctp_findassociation_ep_addr(&inp,
5302 				    (struct sockaddr *)&spa->ssp_addr,
5303 				    &net, NULL, NULL);
5304 				if (stcb == NULL) {
5305 					SCTP_INP_DECR_REF(inp);
5306 				}
5307 			}
5308 
5309 			if ((stcb) && (net)) {
5310 				if ((net != stcb->asoc.primary_destination) &&
5311 				    (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
5312 					/* Ok we need to set it */
5313 					if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
5314 						if ((stcb->asoc.alternate) &&
5315 						    (!(net->dest_state & SCTP_ADDR_PF)) &&
5316 						    (net->dest_state & SCTP_ADDR_REACHABLE)) {
5317 							sctp_free_remote_addr(stcb->asoc.alternate);
5318 							stcb->asoc.alternate = NULL;
5319 						}
5320 					}
5321 				}
5322 			} else {
5323 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5324 				error = EINVAL;
5325 			}
5326 			if (stcb) {
5327 				SCTP_TCB_UNLOCK(stcb);
5328 			}
5329 			break;
5330 		}
5331 	case SCTP_SET_DYNAMIC_PRIMARY:
5332 		{
5333 			union sctp_sockstore *ss;
5334 
5335 			error = priv_check(curthread,
5336 			    PRIV_NETINET_RESERVEDPORT);
5337 			if (error)
5338 				break;
5339 
5340 			SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
5341 			/* SUPER USER CHECK? */
5342 			error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
5343 			break;
5344 		}
5345 	case SCTP_SET_PEER_PRIMARY_ADDR:
5346 		{
5347 			struct sctp_setpeerprim *sspp;
5348 
5349 			SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
5350 			SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
5351 			if (stcb != NULL) {
5352 				struct sctp_ifa *ifa;
5353 
5354 				ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
5355 				    stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
5356 				if (ifa == NULL) {
5357 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5358 					error = EINVAL;
5359 					goto out_of_it;
5360 				}
5361 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
5362 					/*
5363 					 * Must validate the ifa found is in
5364 					 * our ep
5365 					 */
5366 					struct sctp_laddr *laddr;
5367 					int found = 0;
5368 
5369 					LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5370 						if (laddr->ifa == NULL) {
5371 							SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
5372 							    __FUNCTION__);
5373 							continue;
5374 						}
5375 						if (laddr->ifa == ifa) {
5376 							found = 1;
5377 							break;
5378 						}
5379 					}
5380 					if (!found) {
5381 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5382 						error = EINVAL;
5383 						goto out_of_it;
5384 					}
5385 				} else {
5386 					switch (sspp->sspp_addr.ss_family) {
5387 #ifdef INET
5388 					case AF_INET:
5389 						{
5390 							struct sockaddr_in *sin;
5391 
5392 							sin = (struct sockaddr_in *)&sspp->sspp_addr;
5393 							if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
5394 							    &sin->sin_addr) != 0) {
5395 								SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5396 								error = EINVAL;
5397 								goto out_of_it;
5398 							}
5399 							break;
5400 						}
5401 #endif
5402 #ifdef INET6
5403 					case AF_INET6:
5404 						{
5405 							struct sockaddr_in6 *sin6;
5406 
5407 							sin6 = (struct sockaddr_in6 *)&sspp->sspp_addr;
5408 							if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
5409 							    &sin6->sin6_addr) != 0) {
5410 								SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5411 								error = EINVAL;
5412 								goto out_of_it;
5413 							}
5414 							break;
5415 						}
5416 #endif
5417 					default:
5418 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5419 						error = EINVAL;
5420 						goto out_of_it;
5421 					}
5422 				}
5423 				if (sctp_set_primary_ip_address_sa(stcb,
5424 				    (struct sockaddr *)&sspp->sspp_addr) != 0) {
5425 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5426 					error = EINVAL;
5427 				}
5428 		out_of_it:
5429 				SCTP_TCB_UNLOCK(stcb);
5430 			} else {
5431 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5432 				error = EINVAL;
5433 			}
5434 			break;
5435 		}
5436 	case SCTP_BINDX_ADD_ADDR:
5437 		{
5438 			struct sctp_getaddresses *addrs;
5439 			struct thread *td;
5440 
5441 			td = (struct thread *)p;
5442 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
5443 			    optsize);
5444 #ifdef INET
5445 			if (addrs->addr->sa_family == AF_INET) {
5446 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5447 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5448 					error = EINVAL;
5449 					break;
5450 				}
5451 				if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5452 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5453 					break;
5454 				}
5455 			} else
5456 #endif
5457 #ifdef INET6
5458 			if (addrs->addr->sa_family == AF_INET6) {
5459 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5460 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5461 					error = EINVAL;
5462 					break;
5463 				}
5464 				if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5465 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5466 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5467 					break;
5468 				}
5469 			} else
5470 #endif
5471 			{
5472 				error = EAFNOSUPPORT;
5473 				break;
5474 			}
5475 			sctp_bindx_add_address(so, inp, addrs->addr,
5476 			    addrs->sget_assoc_id, vrf_id,
5477 			    &error, p);
5478 			break;
5479 		}
5480 	case SCTP_BINDX_REM_ADDR:
5481 		{
5482 			struct sctp_getaddresses *addrs;
5483 			struct thread *td;
5484 
5485 			td = (struct thread *)p;
5486 
5487 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
5488 #ifdef INET
5489 			if (addrs->addr->sa_family == AF_INET) {
5490 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5491 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5492 					error = EINVAL;
5493 					break;
5494 				}
5495 				if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5496 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5497 					break;
5498 				}
5499 			} else
5500 #endif
5501 #ifdef INET6
5502 			if (addrs->addr->sa_family == AF_INET6) {
5503 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5504 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5505 					error = EINVAL;
5506 					break;
5507 				}
5508 				if (td != NULL &&
5509 				    (error = prison_local_ip6(td->td_ucred,
5510 				    &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5511 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5512 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5513 					break;
5514 				}
5515 			} else
5516 #endif
5517 			{
5518 				error = EAFNOSUPPORT;
5519 				break;
5520 			}
5521 			sctp_bindx_delete_address(inp, addrs->addr,
5522 			    addrs->sget_assoc_id, vrf_id,
5523 			    &error);
5524 			break;
5525 		}
5526 	case SCTP_EVENT:
5527 		{
5528 			struct sctp_event *event;
5529 			uint32_t event_type;
5530 
5531 			SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, optsize);
5532 			SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
5533 			switch (event->se_type) {
5534 			case SCTP_ASSOC_CHANGE:
5535 				event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
5536 				break;
5537 			case SCTP_PEER_ADDR_CHANGE:
5538 				event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
5539 				break;
5540 			case SCTP_REMOTE_ERROR:
5541 				event_type = SCTP_PCB_FLAGS_RECVPEERERR;
5542 				break;
5543 			case SCTP_SEND_FAILED:
5544 				event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
5545 				break;
5546 			case SCTP_SHUTDOWN_EVENT:
5547 				event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
5548 				break;
5549 			case SCTP_ADAPTATION_INDICATION:
5550 				event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
5551 				break;
5552 			case SCTP_PARTIAL_DELIVERY_EVENT:
5553 				event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
5554 				break;
5555 			case SCTP_AUTHENTICATION_EVENT:
5556 				event_type = SCTP_PCB_FLAGS_AUTHEVNT;
5557 				break;
5558 			case SCTP_STREAM_RESET_EVENT:
5559 				event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
5560 				break;
5561 			case SCTP_SENDER_DRY_EVENT:
5562 				event_type = SCTP_PCB_FLAGS_DRYEVNT;
5563 				break;
5564 			case SCTP_NOTIFICATIONS_STOPPED_EVENT:
5565 				event_type = 0;
5566 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
5567 				error = ENOTSUP;
5568 				break;
5569 			case SCTP_ASSOC_RESET_EVENT:
5570 				event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
5571 				break;
5572 			case SCTP_STREAM_CHANGE_EVENT:
5573 				event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
5574 				break;
5575 			case SCTP_SEND_FAILED_EVENT:
5576 				event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
5577 				break;
5578 			default:
5579 				event_type = 0;
5580 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5581 				error = EINVAL;
5582 				break;
5583 			}
5584 			if (event_type > 0) {
5585 				if (stcb) {
5586 					if (event->se_on) {
5587 						sctp_stcb_feature_on(inp, stcb, event_type);
5588 						if (event_type == SCTP_PCB_FLAGS_DRYEVNT) {
5589 							if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5590 							    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5591 							    (stcb->asoc.stream_queue_cnt == 0)) {
5592 								sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
5593 							}
5594 						}
5595 					} else {
5596 						sctp_stcb_feature_off(inp, stcb, event_type);
5597 					}
5598 					SCTP_TCB_UNLOCK(stcb);
5599 				} else {
5600 					/*
5601 					 * We don't want to send up a storm
5602 					 * of events, so return an error for
5603 					 * sender dry events
5604 					 */
5605 					if ((event_type == SCTP_PCB_FLAGS_DRYEVNT) &&
5606 					    ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) == 0) &&
5607 					    ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) &&
5608 					    ((event->se_assoc_id == SCTP_ALL_ASSOC) ||
5609 					    (event->se_assoc_id == SCTP_CURRENT_ASSOC))) {
5610 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
5611 						error = ENOTSUP;
5612 						break;
5613 					}
5614 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5615 					    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5616 					    (event->se_assoc_id == SCTP_FUTURE_ASSOC) ||
5617 					    (event->se_assoc_id == SCTP_ALL_ASSOC)) {
5618 						SCTP_INP_WLOCK(inp);
5619 						if (event->se_on) {
5620 							sctp_feature_on(inp, event_type);
5621 						} else {
5622 							sctp_feature_off(inp, event_type);
5623 						}
5624 						SCTP_INP_WUNLOCK(inp);
5625 					}
5626 					if ((event->se_assoc_id == SCTP_CURRENT_ASSOC) ||
5627 					    (event->se_assoc_id == SCTP_ALL_ASSOC)) {
5628 						SCTP_INP_RLOCK(inp);
5629 						LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5630 							SCTP_TCB_LOCK(stcb);
5631 							if (event->se_on) {
5632 								sctp_stcb_feature_on(inp, stcb, event_type);
5633 							} else {
5634 								sctp_stcb_feature_off(inp, stcb, event_type);
5635 							}
5636 							SCTP_TCB_UNLOCK(stcb);
5637 						}
5638 						SCTP_INP_RUNLOCK(inp);
5639 					}
5640 				}
5641 			}
5642 			break;
5643 		}
5644 	case SCTP_RECVRCVINFO:
5645 		{
5646 			int *onoff;
5647 
5648 			SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
5649 			SCTP_INP_WLOCK(inp);
5650 			if (*onoff != 0) {
5651 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
5652 			} else {
5653 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
5654 			}
5655 			SCTP_INP_WUNLOCK(inp);
5656 			break;
5657 		}
5658 	case SCTP_RECVNXTINFO:
5659 		{
5660 			int *onoff;
5661 
5662 			SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
5663 			SCTP_INP_WLOCK(inp);
5664 			if (*onoff != 0) {
5665 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
5666 			} else {
5667 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
5668 			}
5669 			SCTP_INP_WUNLOCK(inp);
5670 			break;
5671 		}
5672 	case SCTP_DEFAULT_SNDINFO:
5673 		{
5674 			struct sctp_sndinfo *info;
5675 			uint16_t policy;
5676 
5677 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, optsize);
5678 			SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
5679 
5680 			if (stcb) {
5681 				if (info->snd_sid < stcb->asoc.streamoutcnt) {
5682 					stcb->asoc.def_send.sinfo_stream = info->snd_sid;
5683 					policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
5684 					stcb->asoc.def_send.sinfo_flags = info->snd_flags;
5685 					stcb->asoc.def_send.sinfo_flags |= policy;
5686 					stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
5687 					stcb->asoc.def_send.sinfo_context = info->snd_context;
5688 				} else {
5689 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5690 					error = EINVAL;
5691 				}
5692 				SCTP_TCB_UNLOCK(stcb);
5693 			} else {
5694 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5695 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5696 				    (info->snd_assoc_id == SCTP_FUTURE_ASSOC) ||
5697 				    (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
5698 					SCTP_INP_WLOCK(inp);
5699 					inp->def_send.sinfo_stream = info->snd_sid;
5700 					policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
5701 					inp->def_send.sinfo_flags = info->snd_flags;
5702 					inp->def_send.sinfo_flags |= policy;
5703 					inp->def_send.sinfo_ppid = info->snd_ppid;
5704 					inp->def_send.sinfo_context = info->snd_context;
5705 					SCTP_INP_WUNLOCK(inp);
5706 				}
5707 				if ((info->snd_assoc_id == SCTP_CURRENT_ASSOC) ||
5708 				    (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
5709 					SCTP_INP_RLOCK(inp);
5710 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5711 						SCTP_TCB_LOCK(stcb);
5712 						if (info->snd_sid < stcb->asoc.streamoutcnt) {
5713 							stcb->asoc.def_send.sinfo_stream = info->snd_sid;
5714 							policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
5715 							stcb->asoc.def_send.sinfo_flags = info->snd_flags;
5716 							stcb->asoc.def_send.sinfo_flags |= policy;
5717 							stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
5718 							stcb->asoc.def_send.sinfo_context = info->snd_context;
5719 						}
5720 						SCTP_TCB_UNLOCK(stcb);
5721 					}
5722 					SCTP_INP_RUNLOCK(inp);
5723 				}
5724 			}
5725 			break;
5726 		}
5727 	case SCTP_DEFAULT_PRINFO:
5728 		{
5729 			struct sctp_default_prinfo *info;
5730 
5731 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, optsize);
5732 			SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
5733 
5734 			if (PR_SCTP_INVALID_POLICY(info->pr_policy)) {
5735 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5736 				error = EINVAL;
5737 				break;
5738 			}
5739 			if (stcb) {
5740 				stcb->asoc.def_send.sinfo_flags &= 0xfff0;
5741 				stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
5742 				stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
5743 				SCTP_TCB_UNLOCK(stcb);
5744 			} else {
5745 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5746 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5747 				    (info->pr_assoc_id == SCTP_FUTURE_ASSOC) ||
5748 				    (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
5749 					SCTP_INP_WLOCK(inp);
5750 					inp->def_send.sinfo_flags &= 0xfff0;
5751 					inp->def_send.sinfo_flags |= info->pr_policy;
5752 					inp->def_send.sinfo_timetolive = info->pr_value;
5753 					SCTP_INP_WUNLOCK(inp);
5754 				}
5755 				if ((info->pr_assoc_id == SCTP_CURRENT_ASSOC) ||
5756 				    (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
5757 					SCTP_INP_RLOCK(inp);
5758 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5759 						SCTP_TCB_LOCK(stcb);
5760 						stcb->asoc.def_send.sinfo_flags &= 0xfff0;
5761 						stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
5762 						stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
5763 						SCTP_TCB_UNLOCK(stcb);
5764 					}
5765 					SCTP_INP_RUNLOCK(inp);
5766 				}
5767 			}
5768 			break;
5769 		}
5770 	case SCTP_PEER_ADDR_THLDS:
5771 		/* Applies to the specific association */
5772 		{
5773 			struct sctp_paddrthlds *thlds;
5774 			struct sctp_nets *net;
5775 
5776 			SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, optsize);
5777 			SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
5778 			net = NULL;
5779 			if (stcb) {
5780 				net = sctp_findnet(stcb, (struct sockaddr *)&thlds->spt_address);
5781 			} else {
5782 				/*
5783 				 * We increment here since
5784 				 * sctp_findassociation_ep_addr() wil do a
5785 				 * decrement if it finds the stcb as long as
5786 				 * the locked tcb (last argument) is NOT a
5787 				 * TCB.. aka NULL.
5788 				 */
5789 				SCTP_INP_INCR_REF(inp);
5790 				stcb = sctp_findassociation_ep_addr(&inp,
5791 				    (struct sockaddr *)&thlds->spt_address,
5792 				    &net, NULL, NULL);
5793 				if (stcb == NULL) {
5794 					SCTP_INP_DECR_REF(inp);
5795 				}
5796 			}
5797 			if (stcb && (net == NULL)) {
5798 				struct sockaddr *sa;
5799 
5800 				sa = (struct sockaddr *)&thlds->spt_address;
5801 #ifdef INET
5802 				if (sa->sa_family == AF_INET) {
5803 
5804 					struct sockaddr_in *sin;
5805 
5806 					sin = (struct sockaddr_in *)sa;
5807 					if (sin->sin_addr.s_addr) {
5808 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5809 						SCTP_TCB_UNLOCK(stcb);
5810 						error = EINVAL;
5811 						break;
5812 					}
5813 				} else
5814 #endif
5815 #ifdef INET6
5816 				if (sa->sa_family == AF_INET6) {
5817 					struct sockaddr_in6 *sin6;
5818 
5819 					sin6 = (struct sockaddr_in6 *)sa;
5820 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
5821 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5822 						SCTP_TCB_UNLOCK(stcb);
5823 						error = EINVAL;
5824 						break;
5825 					}
5826 				} else
5827 #endif
5828 				{
5829 					error = EAFNOSUPPORT;
5830 					SCTP_TCB_UNLOCK(stcb);
5831 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
5832 					break;
5833 				}
5834 			}
5835 			if (stcb) {
5836 				if (net) {
5837 					if (net->dest_state & SCTP_ADDR_PF) {
5838 						if ((net->failure_threshold > thlds->spt_pathmaxrxt) ||
5839 						    (net->failure_threshold <= thlds->spt_pathpfthld)) {
5840 							net->dest_state &= ~SCTP_ADDR_PF;
5841 						}
5842 					} else {
5843 						if ((net->failure_threshold > thlds->spt_pathpfthld) &&
5844 						    (net->failure_threshold <= thlds->spt_pathmaxrxt)) {
5845 							net->dest_state |= SCTP_ADDR_PF;
5846 							sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5847 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
5848 							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5849 						}
5850 					}
5851 					if (net->dest_state & SCTP_ADDR_REACHABLE) {
5852 						if (net->failure_threshold > thlds->spt_pathmaxrxt) {
5853 							net->dest_state &= ~SCTP_ADDR_REACHABLE;
5854 							sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5855 						}
5856 					} else {
5857 						if (net->failure_threshold <= thlds->spt_pathmaxrxt) {
5858 							net->dest_state |= SCTP_ADDR_REACHABLE;
5859 							sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5860 						}
5861 					}
5862 					net->failure_threshold = thlds->spt_pathmaxrxt;
5863 					net->pf_threshold = thlds->spt_pathpfthld;
5864 				} else {
5865 					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5866 						if (net->dest_state & SCTP_ADDR_PF) {
5867 							if ((net->failure_threshold > thlds->spt_pathmaxrxt) ||
5868 							    (net->failure_threshold <= thlds->spt_pathpfthld)) {
5869 								net->dest_state &= ~SCTP_ADDR_PF;
5870 							}
5871 						} else {
5872 							if ((net->failure_threshold > thlds->spt_pathpfthld) &&
5873 							    (net->failure_threshold <= thlds->spt_pathmaxrxt)) {
5874 								net->dest_state |= SCTP_ADDR_PF;
5875 								sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5876 								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
5877 								sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5878 							}
5879 						}
5880 						if (net->dest_state & SCTP_ADDR_REACHABLE) {
5881 							if (net->failure_threshold > thlds->spt_pathmaxrxt) {
5882 								net->dest_state &= ~SCTP_ADDR_REACHABLE;
5883 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5884 							}
5885 						} else {
5886 							if (net->failure_threshold <= thlds->spt_pathmaxrxt) {
5887 								net->dest_state |= SCTP_ADDR_REACHABLE;
5888 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5889 							}
5890 						}
5891 						net->failure_threshold = thlds->spt_pathmaxrxt;
5892 						net->pf_threshold = thlds->spt_pathpfthld;
5893 					}
5894 					stcb->asoc.def_net_failure = thlds->spt_pathmaxrxt;
5895 					stcb->asoc.def_net_pf_threshold = thlds->spt_pathpfthld;
5896 				}
5897 			} else {
5898 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5899 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5900 				    (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
5901 					SCTP_INP_WLOCK(inp);
5902 					inp->sctp_ep.def_net_failure = thlds->spt_pathmaxrxt;
5903 					inp->sctp_ep.def_net_pf_threshold = thlds->spt_pathpfthld;
5904 					SCTP_INP_WUNLOCK(inp);
5905 				} else {
5906 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5907 					error = EINVAL;
5908 				}
5909 			}
5910 			break;
5911 		}
5912 	case SCTP_REMOTE_UDP_ENCAPS_PORT:
5913 		{
5914 			struct sctp_udpencaps *encaps;
5915 			struct sctp_nets *net;
5916 
5917 			SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, optsize);
5918 			SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
5919 			if (stcb) {
5920 				net = sctp_findnet(stcb, (struct sockaddr *)&encaps->sue_address);
5921 			} else {
5922 				/*
5923 				 * We increment here since
5924 				 * sctp_findassociation_ep_addr() wil do a
5925 				 * decrement if it finds the stcb as long as
5926 				 * the locked tcb (last argument) is NOT a
5927 				 * TCB.. aka NULL.
5928 				 */
5929 				net = NULL;
5930 				SCTP_INP_INCR_REF(inp);
5931 				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&encaps->sue_address, &net, NULL, NULL);
5932 				if (stcb == NULL) {
5933 					SCTP_INP_DECR_REF(inp);
5934 				}
5935 			}
5936 			if (stcb && (net == NULL)) {
5937 				struct sockaddr *sa;
5938 
5939 				sa = (struct sockaddr *)&encaps->sue_address;
5940 #ifdef INET
5941 				if (sa->sa_family == AF_INET) {
5942 
5943 					struct sockaddr_in *sin;
5944 
5945 					sin = (struct sockaddr_in *)sa;
5946 					if (sin->sin_addr.s_addr) {
5947 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5948 						SCTP_TCB_UNLOCK(stcb);
5949 						error = EINVAL;
5950 						break;
5951 					}
5952 				} else
5953 #endif
5954 #ifdef INET6
5955 				if (sa->sa_family == AF_INET6) {
5956 					struct sockaddr_in6 *sin6;
5957 
5958 					sin6 = (struct sockaddr_in6 *)sa;
5959 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
5960 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5961 						SCTP_TCB_UNLOCK(stcb);
5962 						error = EINVAL;
5963 						break;
5964 					}
5965 				} else
5966 #endif
5967 				{
5968 					error = EAFNOSUPPORT;
5969 					SCTP_TCB_UNLOCK(stcb);
5970 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
5971 					break;
5972 				}
5973 			}
5974 			if (stcb) {
5975 				if (net) {
5976 					net->port = encaps->sue_port;
5977 				} else {
5978 					stcb->asoc.port = encaps->sue_port;
5979 				}
5980 				SCTP_TCB_UNLOCK(stcb);
5981 			} else {
5982 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5983 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5984 				    (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
5985 					SCTP_INP_WLOCK(inp);
5986 					inp->sctp_ep.port = encaps->sue_port;
5987 					SCTP_INP_WUNLOCK(inp);
5988 				} else {
5989 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5990 					error = EINVAL;
5991 				}
5992 			}
5993 			break;
5994 		}
5995 	case SCTP_ECN_SUPPORTED:
5996 		{
5997 			struct sctp_assoc_value *av;
5998 
5999 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6000 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6001 
6002 			if (stcb) {
6003 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6004 				error = EINVAL;
6005 				SCTP_TCB_UNLOCK(stcb);
6006 			} else {
6007 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6008 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6009 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6010 					SCTP_INP_WLOCK(inp);
6011 					if (av->assoc_value == 0) {
6012 						inp->ecn_supported = 0;
6013 					} else {
6014 						inp->ecn_supported = 1;
6015 					}
6016 					SCTP_INP_WUNLOCK(inp);
6017 				} else {
6018 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6019 					error = EINVAL;
6020 				}
6021 			}
6022 			break;
6023 		}
6024 	case SCTP_PR_SUPPORTED:
6025 		{
6026 			struct sctp_assoc_value *av;
6027 
6028 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6029 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6030 
6031 			if (stcb) {
6032 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6033 				error = EINVAL;
6034 				SCTP_TCB_UNLOCK(stcb);
6035 			} else {
6036 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6037 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6038 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6039 					SCTP_INP_WLOCK(inp);
6040 					if (av->assoc_value == 0) {
6041 						inp->prsctp_supported = 0;
6042 					} else {
6043 						inp->prsctp_supported = 1;
6044 					}
6045 					SCTP_INP_WUNLOCK(inp);
6046 				} else {
6047 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6048 					error = EINVAL;
6049 				}
6050 			}
6051 			break;
6052 		}
6053 	case SCTP_RECONFIG_SUPPORTED:
6054 		{
6055 			struct sctp_assoc_value *av;
6056 
6057 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6058 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6059 
6060 			if (stcb) {
6061 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6062 				error = EINVAL;
6063 				SCTP_TCB_UNLOCK(stcb);
6064 			} else {
6065 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6066 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6067 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6068 					SCTP_INP_WLOCK(inp);
6069 					if (av->assoc_value == 0) {
6070 						inp->reconfig_supported = 0;
6071 					} else {
6072 						inp->reconfig_supported = 1;
6073 					}
6074 					SCTP_INP_WUNLOCK(inp);
6075 				} else {
6076 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6077 					error = EINVAL;
6078 				}
6079 			}
6080 			break;
6081 		}
6082 	case SCTP_NRSACK_SUPPORTED:
6083 		{
6084 			struct sctp_assoc_value *av;
6085 
6086 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6087 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6088 
6089 			if (stcb) {
6090 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6091 				error = EINVAL;
6092 				SCTP_TCB_UNLOCK(stcb);
6093 			} else {
6094 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6095 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6096 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6097 					SCTP_INP_WLOCK(inp);
6098 					if (av->assoc_value == 0) {
6099 						inp->nrsack_supported = 0;
6100 					} else {
6101 						inp->nrsack_supported = 1;
6102 					}
6103 					SCTP_INP_WUNLOCK(inp);
6104 				} else {
6105 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6106 					error = EINVAL;
6107 				}
6108 			}
6109 			break;
6110 		}
6111 	case SCTP_PKTDROP_SUPPORTED:
6112 		{
6113 			struct sctp_assoc_value *av;
6114 
6115 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6116 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6117 
6118 			if (stcb) {
6119 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6120 				error = EINVAL;
6121 				SCTP_TCB_UNLOCK(stcb);
6122 			} else {
6123 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6124 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6125 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6126 					SCTP_INP_WLOCK(inp);
6127 					if (av->assoc_value == 0) {
6128 						inp->pktdrop_supported = 0;
6129 					} else {
6130 						inp->pktdrop_supported = 1;
6131 					}
6132 					SCTP_INP_WUNLOCK(inp);
6133 				} else {
6134 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6135 					error = EINVAL;
6136 				}
6137 			}
6138 			break;
6139 		}
6140 	default:
6141 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
6142 		error = ENOPROTOOPT;
6143 		break;
6144 	}			/* end switch (opt) */
6145 	return (error);
6146 }
6147 
6148 int
6149 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
6150 {
6151 	void *optval = NULL;
6152 	size_t optsize = 0;
6153 	void *p;
6154 	int error = 0;
6155 
6156 	if (sopt->sopt_level != IPPROTO_SCTP) {
6157 		/* wrong proto level... send back up to IP */
6158 #ifdef INET6
6159 		if (INP_CHECK_SOCKAF(so, AF_INET6))
6160 			error = ip6_ctloutput(so, sopt);
6161 #endif				/* INET6 */
6162 #if defined(INET) && defined(INET6)
6163 		else
6164 #endif
6165 #ifdef INET
6166 			error = ip_ctloutput(so, sopt);
6167 #endif
6168 		return (error);
6169 	}
6170 	optsize = sopt->sopt_valsize;
6171 	if (optsize) {
6172 		SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
6173 		if (optval == NULL) {
6174 			SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6175 			return (ENOBUFS);
6176 		}
6177 		error = sooptcopyin(sopt, optval, optsize, optsize);
6178 		if (error) {
6179 			SCTP_FREE(optval, SCTP_M_SOCKOPT);
6180 			goto out;
6181 		}
6182 	}
6183 	p = (void *)sopt->sopt_td;
6184 	if (sopt->sopt_dir == SOPT_SET) {
6185 		error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
6186 	} else if (sopt->sopt_dir == SOPT_GET) {
6187 		error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
6188 	} else {
6189 		SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6190 		error = EINVAL;
6191 	}
6192 	if ((error == 0) && (optval != NULL)) {
6193 		error = sooptcopyout(sopt, optval, optsize);
6194 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
6195 	} else if (optval != NULL) {
6196 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
6197 	}
6198 out:
6199 	return (error);
6200 }
6201 
6202 #ifdef INET
6203 static int
6204 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
6205 {
6206 	int error = 0;
6207 	int create_lock_on = 0;
6208 	uint32_t vrf_id;
6209 	struct sctp_inpcb *inp;
6210 	struct sctp_tcb *stcb = NULL;
6211 
6212 	inp = (struct sctp_inpcb *)so->so_pcb;
6213 	if (inp == NULL) {
6214 		/* I made the same as TCP since we are not setup? */
6215 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6216 		return (ECONNRESET);
6217 	}
6218 	if (addr == NULL) {
6219 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6220 		return EINVAL;
6221 	}
6222 	switch (addr->sa_family) {
6223 #ifdef INET6
6224 	case AF_INET6:
6225 		{
6226 			struct sockaddr_in6 *sin6p;
6227 
6228 			if (addr->sa_len != sizeof(struct sockaddr_in6)) {
6229 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6230 				return (EINVAL);
6231 			}
6232 			sin6p = (struct sockaddr_in6 *)addr;
6233 			if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
6234 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6235 				return (error);
6236 			}
6237 			break;
6238 		}
6239 #endif
6240 #ifdef INET
6241 	case AF_INET:
6242 		{
6243 			struct sockaddr_in *sinp;
6244 
6245 			if (addr->sa_len != sizeof(struct sockaddr_in)) {
6246 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6247 				return (EINVAL);
6248 			}
6249 			sinp = (struct sockaddr_in *)addr;
6250 			if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
6251 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6252 				return (error);
6253 			}
6254 			break;
6255 		}
6256 #endif
6257 	default:
6258 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
6259 		return (EAFNOSUPPORT);
6260 	}
6261 	SCTP_INP_INCR_REF(inp);
6262 	SCTP_ASOC_CREATE_LOCK(inp);
6263 	create_lock_on = 1;
6264 
6265 
6266 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
6267 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
6268 		/* Should I really unlock ? */
6269 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
6270 		error = EFAULT;
6271 		goto out_now;
6272 	}
6273 #ifdef INET6
6274 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
6275 	    (addr->sa_family == AF_INET6)) {
6276 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6277 		error = EINVAL;
6278 		goto out_now;
6279 	}
6280 #endif
6281 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
6282 	    SCTP_PCB_FLAGS_UNBOUND) {
6283 		/* Bind a ephemeral port */
6284 		error = sctp_inpcb_bind(so, NULL, NULL, p);
6285 		if (error) {
6286 			goto out_now;
6287 		}
6288 	}
6289 	/* Now do we connect? */
6290 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
6291 	    (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
6292 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6293 		error = EINVAL;
6294 		goto out_now;
6295 	}
6296 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
6297 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
6298 		/* We are already connected AND the TCP model */
6299 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
6300 		error = EADDRINUSE;
6301 		goto out_now;
6302 	}
6303 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
6304 		SCTP_INP_RLOCK(inp);
6305 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
6306 		SCTP_INP_RUNLOCK(inp);
6307 	} else {
6308 		/*
6309 		 * We increment here since sctp_findassociation_ep_addr()
6310 		 * will do a decrement if it finds the stcb as long as the
6311 		 * locked tcb (last argument) is NOT a TCB.. aka NULL.
6312 		 */
6313 		SCTP_INP_INCR_REF(inp);
6314 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
6315 		if (stcb == NULL) {
6316 			SCTP_INP_DECR_REF(inp);
6317 		} else {
6318 			SCTP_TCB_UNLOCK(stcb);
6319 		}
6320 	}
6321 	if (stcb != NULL) {
6322 		/* Already have or am bring up an association */
6323 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
6324 		error = EALREADY;
6325 		goto out_now;
6326 	}
6327 	vrf_id = inp->def_vrf_id;
6328 	/* We are GOOD to go */
6329 	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
6330 	if (stcb == NULL) {
6331 		/* Gak! no memory */
6332 		goto out_now;
6333 	}
6334 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
6335 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
6336 		/* Set the connected flag so we can queue data */
6337 		soisconnecting(so);
6338 	}
6339 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
6340 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
6341 
6342 	/* initialize authentication parameters for the assoc */
6343 	sctp_initialize_auth_params(inp, stcb);
6344 
6345 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
6346 	SCTP_TCB_UNLOCK(stcb);
6347 out_now:
6348 	if (create_lock_on) {
6349 		SCTP_ASOC_CREATE_UNLOCK(inp);
6350 	}
6351 	SCTP_INP_DECR_REF(inp);
6352 	return (error);
6353 }
6354 
6355 #endif
6356 
6357 int
6358 sctp_listen(struct socket *so, int backlog, struct thread *p)
6359 {
6360 	/*
6361 	 * Note this module depends on the protocol processing being called
6362 	 * AFTER any socket level flags and backlog are applied to the
6363 	 * socket. The traditional way that the socket flags are applied is
6364 	 * AFTER protocol processing. We have made a change to the
6365 	 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
6366 	 * place if the socket API for SCTP is to work properly.
6367 	 */
6368 
6369 	int error = 0;
6370 	struct sctp_inpcb *inp;
6371 
6372 	inp = (struct sctp_inpcb *)so->so_pcb;
6373 	if (inp == NULL) {
6374 		/* I made the same as TCP since we are not setup? */
6375 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6376 		return (ECONNRESET);
6377 	}
6378 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
6379 		/* See if we have a listener */
6380 		struct sctp_inpcb *tinp;
6381 		union sctp_sockstore store;
6382 
6383 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
6384 			/* not bound all */
6385 			struct sctp_laddr *laddr;
6386 
6387 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6388 				memcpy(&store, &laddr->ifa->address, sizeof(store));
6389 				switch (store.sa.sa_family) {
6390 #ifdef INET
6391 				case AF_INET:
6392 					store.sin.sin_port = inp->sctp_lport;
6393 					break;
6394 #endif
6395 #ifdef INET6
6396 				case AF_INET6:
6397 					store.sin6.sin6_port = inp->sctp_lport;
6398 					break;
6399 #endif
6400 				default:
6401 					break;
6402 				}
6403 				tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
6404 				if (tinp && (tinp != inp) &&
6405 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
6406 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
6407 				    (tinp->sctp_socket->so_qlimit)) {
6408 					/*
6409 					 * we have a listener already and
6410 					 * its not this inp.
6411 					 */
6412 					SCTP_INP_DECR_REF(tinp);
6413 					return (EADDRINUSE);
6414 				} else if (tinp) {
6415 					SCTP_INP_DECR_REF(tinp);
6416 				}
6417 			}
6418 		} else {
6419 			/* Setup a local addr bound all */
6420 			memset(&store, 0, sizeof(store));
6421 #ifdef INET6
6422 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6423 				store.sa.sa_family = AF_INET6;
6424 				store.sa.sa_len = sizeof(struct sockaddr_in6);
6425 			}
6426 #endif
6427 #ifdef INET
6428 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6429 				store.sa.sa_family = AF_INET;
6430 				store.sa.sa_len = sizeof(struct sockaddr_in);
6431 			}
6432 #endif
6433 			switch (store.sa.sa_family) {
6434 #ifdef INET
6435 			case AF_INET:
6436 				store.sin.sin_port = inp->sctp_lport;
6437 				break;
6438 #endif
6439 #ifdef INET6
6440 			case AF_INET6:
6441 				store.sin6.sin6_port = inp->sctp_lport;
6442 				break;
6443 #endif
6444 			default:
6445 				break;
6446 			}
6447 			tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
6448 			if (tinp && (tinp != inp) &&
6449 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
6450 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
6451 			    (tinp->sctp_socket->so_qlimit)) {
6452 				/*
6453 				 * we have a listener already and its not
6454 				 * this inp.
6455 				 */
6456 				SCTP_INP_DECR_REF(tinp);
6457 				return (EADDRINUSE);
6458 			} else if (tinp) {
6459 				SCTP_INP_DECR_REF(inp);
6460 			}
6461 		}
6462 	}
6463 	SCTP_INP_RLOCK(inp);
6464 #ifdef SCTP_LOCK_LOGGING
6465 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
6466 		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
6467 	}
6468 #endif
6469 	SOCK_LOCK(so);
6470 	error = solisten_proto_check(so);
6471 	if (error) {
6472 		SOCK_UNLOCK(so);
6473 		SCTP_INP_RUNLOCK(inp);
6474 		return (error);
6475 	}
6476 	if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
6477 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
6478 		/*
6479 		 * The unlucky case - We are in the tcp pool with this guy.
6480 		 * - Someone else is in the main inp slot. - We must move
6481 		 * this guy (the listener) to the main slot - We must then
6482 		 * move the guy that was listener to the TCP Pool.
6483 		 */
6484 		if (sctp_swap_inpcb_for_listen(inp)) {
6485 			goto in_use;
6486 		}
6487 	}
6488 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
6489 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
6490 		/* We are already connected AND the TCP model */
6491 in_use:
6492 		SCTP_INP_RUNLOCK(inp);
6493 		SOCK_UNLOCK(so);
6494 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
6495 		return (EADDRINUSE);
6496 	}
6497 	SCTP_INP_RUNLOCK(inp);
6498 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
6499 		/* We must do a bind. */
6500 		SOCK_UNLOCK(so);
6501 		if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
6502 			/* bind error, probably perm */
6503 			return (error);
6504 		}
6505 		SOCK_LOCK(so);
6506 	}
6507 	/* It appears for 7.0 and on, we must always call this. */
6508 	solisten_proto(so, backlog);
6509 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
6510 		/* remove the ACCEPTCONN flag for one-to-many sockets */
6511 		so->so_options &= ~SO_ACCEPTCONN;
6512 	}
6513 	if (backlog == 0) {
6514 		/* turning off listen */
6515 		so->so_options &= ~SO_ACCEPTCONN;
6516 	}
6517 	SOCK_UNLOCK(so);
6518 	return (error);
6519 }
6520 
6521 static int sctp_defered_wakeup_cnt = 0;
6522 
6523 int
6524 sctp_accept(struct socket *so, struct sockaddr **addr)
6525 {
6526 	struct sctp_tcb *stcb;
6527 	struct sctp_inpcb *inp;
6528 	union sctp_sockstore store;
6529 
6530 #ifdef INET6
6531 	int error;
6532 
6533 #endif
6534 	inp = (struct sctp_inpcb *)so->so_pcb;
6535 
6536 	if (inp == NULL) {
6537 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6538 		return (ECONNRESET);
6539 	}
6540 	SCTP_INP_RLOCK(inp);
6541 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
6542 		SCTP_INP_RUNLOCK(inp);
6543 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
6544 		return (EOPNOTSUPP);
6545 	}
6546 	if (so->so_state & SS_ISDISCONNECTED) {
6547 		SCTP_INP_RUNLOCK(inp);
6548 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
6549 		return (ECONNABORTED);
6550 	}
6551 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
6552 	if (stcb == NULL) {
6553 		SCTP_INP_RUNLOCK(inp);
6554 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6555 		return (ECONNRESET);
6556 	}
6557 	SCTP_TCB_LOCK(stcb);
6558 	SCTP_INP_RUNLOCK(inp);
6559 	store = stcb->asoc.primary_destination->ro._l_addr;
6560 	stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
6561 	SCTP_TCB_UNLOCK(stcb);
6562 	switch (store.sa.sa_family) {
6563 #ifdef INET
6564 	case AF_INET:
6565 		{
6566 			struct sockaddr_in *sin;
6567 
6568 			SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
6569 			if (sin == NULL)
6570 				return (ENOMEM);
6571 			sin->sin_family = AF_INET;
6572 			sin->sin_len = sizeof(*sin);
6573 			sin->sin_port = store.sin.sin_port;
6574 			sin->sin_addr = store.sin.sin_addr;
6575 			*addr = (struct sockaddr *)sin;
6576 			break;
6577 		}
6578 #endif
6579 #ifdef INET6
6580 	case AF_INET6:
6581 		{
6582 			struct sockaddr_in6 *sin6;
6583 
6584 			SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
6585 			if (sin6 == NULL)
6586 				return (ENOMEM);
6587 			sin6->sin6_family = AF_INET6;
6588 			sin6->sin6_len = sizeof(*sin6);
6589 			sin6->sin6_port = store.sin6.sin6_port;
6590 			sin6->sin6_addr = store.sin6.sin6_addr;
6591 			if ((error = sa6_recoverscope(sin6)) != 0) {
6592 				SCTP_FREE_SONAME(sin6);
6593 				return (error);
6594 			}
6595 			*addr = (struct sockaddr *)sin6;
6596 			break;
6597 		}
6598 #endif
6599 	default:
6600 		/* TSNH */
6601 		break;
6602 	}
6603 	/* Wake any delayed sleep action */
6604 	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
6605 		SCTP_INP_WLOCK(inp);
6606 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
6607 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
6608 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
6609 			SCTP_INP_WUNLOCK(inp);
6610 			SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
6611 			if (sowriteable(inp->sctp_socket)) {
6612 				sowwakeup_locked(inp->sctp_socket);
6613 			} else {
6614 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
6615 			}
6616 			SCTP_INP_WLOCK(inp);
6617 		}
6618 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
6619 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
6620 			SCTP_INP_WUNLOCK(inp);
6621 			SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
6622 			if (soreadable(inp->sctp_socket)) {
6623 				sctp_defered_wakeup_cnt++;
6624 				sorwakeup_locked(inp->sctp_socket);
6625 			} else {
6626 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
6627 			}
6628 			SCTP_INP_WLOCK(inp);
6629 		}
6630 		SCTP_INP_WUNLOCK(inp);
6631 	}
6632 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
6633 		SCTP_TCB_LOCK(stcb);
6634 		sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
6635 	}
6636 	return (0);
6637 }
6638 
6639 #ifdef INET
6640 int
6641 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
6642 {
6643 	struct sockaddr_in *sin;
6644 	uint32_t vrf_id;
6645 	struct sctp_inpcb *inp;
6646 	struct sctp_ifa *sctp_ifa;
6647 
6648 	/*
6649 	 * Do the malloc first in case it blocks.
6650 	 */
6651 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
6652 	if (sin == NULL)
6653 		return (ENOMEM);
6654 	sin->sin_family = AF_INET;
6655 	sin->sin_len = sizeof(*sin);
6656 	inp = (struct sctp_inpcb *)so->so_pcb;
6657 	if (!inp) {
6658 		SCTP_FREE_SONAME(sin);
6659 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6660 		return (ECONNRESET);
6661 	}
6662 	SCTP_INP_RLOCK(inp);
6663 	sin->sin_port = inp->sctp_lport;
6664 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6665 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
6666 			struct sctp_tcb *stcb;
6667 			struct sockaddr_in *sin_a;
6668 			struct sctp_nets *net;
6669 			int fnd;
6670 
6671 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
6672 			if (stcb == NULL) {
6673 				goto notConn;
6674 			}
6675 			fnd = 0;
6676 			sin_a = NULL;
6677 			SCTP_TCB_LOCK(stcb);
6678 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6679 				sin_a = (struct sockaddr_in *)&net->ro._l_addr;
6680 				if (sin_a == NULL)
6681 					/* this will make coverity happy */
6682 					continue;
6683 
6684 				if (sin_a->sin_family == AF_INET) {
6685 					fnd = 1;
6686 					break;
6687 				}
6688 			}
6689 			if ((!fnd) || (sin_a == NULL)) {
6690 				/* punt */
6691 				SCTP_TCB_UNLOCK(stcb);
6692 				goto notConn;
6693 			}
6694 			vrf_id = inp->def_vrf_id;
6695 			sctp_ifa = sctp_source_address_selection(inp,
6696 			    stcb,
6697 			    (sctp_route_t *) & net->ro,
6698 			    net, 0, vrf_id);
6699 			if (sctp_ifa) {
6700 				sin->sin_addr = sctp_ifa->address.sin.sin_addr;
6701 				sctp_free_ifa(sctp_ifa);
6702 			}
6703 			SCTP_TCB_UNLOCK(stcb);
6704 		} else {
6705 			/* For the bound all case you get back 0 */
6706 	notConn:
6707 			sin->sin_addr.s_addr = 0;
6708 		}
6709 
6710 	} else {
6711 		/* Take the first IPv4 address in the list */
6712 		struct sctp_laddr *laddr;
6713 		int fnd = 0;
6714 
6715 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6716 			if (laddr->ifa->address.sa.sa_family == AF_INET) {
6717 				struct sockaddr_in *sin_a;
6718 
6719 				sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
6720 				sin->sin_addr = sin_a->sin_addr;
6721 				fnd = 1;
6722 				break;
6723 			}
6724 		}
6725 		if (!fnd) {
6726 			SCTP_FREE_SONAME(sin);
6727 			SCTP_INP_RUNLOCK(inp);
6728 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
6729 			return (ENOENT);
6730 		}
6731 	}
6732 	SCTP_INP_RUNLOCK(inp);
6733 	(*addr) = (struct sockaddr *)sin;
6734 	return (0);
6735 }
6736 
6737 int
6738 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
6739 {
6740 	struct sockaddr_in *sin;
6741 	int fnd;
6742 	struct sockaddr_in *sin_a;
6743 	struct sctp_inpcb *inp;
6744 	struct sctp_tcb *stcb;
6745 	struct sctp_nets *net;
6746 
6747 	/* Do the malloc first in case it blocks. */
6748 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
6749 	if (sin == NULL)
6750 		return (ENOMEM);
6751 	sin->sin_family = AF_INET;
6752 	sin->sin_len = sizeof(*sin);
6753 
6754 	inp = (struct sctp_inpcb *)so->so_pcb;
6755 	if ((inp == NULL) ||
6756 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
6757 		/* UDP type and listeners will drop out here */
6758 		SCTP_FREE_SONAME(sin);
6759 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
6760 		return (ENOTCONN);
6761 	}
6762 	SCTP_INP_RLOCK(inp);
6763 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
6764 	if (stcb) {
6765 		SCTP_TCB_LOCK(stcb);
6766 	}
6767 	SCTP_INP_RUNLOCK(inp);
6768 	if (stcb == NULL) {
6769 		SCTP_FREE_SONAME(sin);
6770 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6771 		return (ECONNRESET);
6772 	}
6773 	fnd = 0;
6774 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6775 		sin_a = (struct sockaddr_in *)&net->ro._l_addr;
6776 		if (sin_a->sin_family == AF_INET) {
6777 			fnd = 1;
6778 			sin->sin_port = stcb->rport;
6779 			sin->sin_addr = sin_a->sin_addr;
6780 			break;
6781 		}
6782 	}
6783 	SCTP_TCB_UNLOCK(stcb);
6784 	if (!fnd) {
6785 		/* No IPv4 address */
6786 		SCTP_FREE_SONAME(sin);
6787 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
6788 		return (ENOENT);
6789 	}
6790 	(*addr) = (struct sockaddr *)sin;
6791 	return (0);
6792 }
6793 
6794 struct pr_usrreqs sctp_usrreqs = {
6795 	.pru_abort = sctp_abort,
6796 	.pru_accept = sctp_accept,
6797 	.pru_attach = sctp_attach,
6798 	.pru_bind = sctp_bind,
6799 	.pru_connect = sctp_connect,
6800 	.pru_control = in_control,
6801 	.pru_close = sctp_close,
6802 	.pru_detach = sctp_close,
6803 	.pru_sopoll = sopoll_generic,
6804 	.pru_flush = sctp_flush,
6805 	.pru_disconnect = sctp_disconnect,
6806 	.pru_listen = sctp_listen,
6807 	.pru_peeraddr = sctp_peeraddr,
6808 	.pru_send = sctp_sendm,
6809 	.pru_shutdown = sctp_shutdown,
6810 	.pru_sockaddr = sctp_ingetaddr,
6811 	.pru_sosend = sctp_sosend,
6812 	.pru_soreceive = sctp_soreceive
6813 };
6814 
6815 #endif
6816