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