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