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