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