xref: /freebsd/sys/netinet/sctp_usrreq.c (revision a812392203d7c4c3f0db9d8a0f3391374c49c71f)
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 			struct sctp_nets *net;
4724 
4725 			/* FIXME MT: check correct? */
4726 			SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
4727 
4728 			/* find tcb */
4729 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4730 				SCTP_INP_RLOCK(inp);
4731 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
4732 				if (stcb) {
4733 					SCTP_TCB_LOCK(stcb);
4734 					net = sctp_findnet(stcb, sa);
4735 				}
4736 				SCTP_INP_RUNLOCK(inp);
4737 			} else {
4738 				/*
4739 				 * We increment here since
4740 				 * sctp_findassociation_ep_addr() wil do a
4741 				 * decrement if it finds the stcb as long as
4742 				 * the locked tcb (last argument) is NOT a
4743 				 * TCB.. aka NULL.
4744 				 */
4745 				SCTP_INP_INCR_REF(inp);
4746 				stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
4747 				if (stcb == NULL) {
4748 					SCTP_INP_DECR_REF(inp);
4749 				}
4750 			}
4751 
4752 			if (stcb == NULL) {
4753 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4754 				error = ENOENT;
4755 				break;
4756 			}
4757 			if (stcb->asoc.delayed_connection == 1) {
4758 				stcb->asoc.delayed_connection = 0;
4759 				(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4760 				sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
4761 				    stcb->asoc.primary_destination,
4762 				    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
4763 				sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4764 			} else {
4765 				/*
4766 				 * already expired or did not use delayed
4767 				 * connectx
4768 				 */
4769 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4770 				error = EALREADY;
4771 			}
4772 			SCTP_TCB_UNLOCK(stcb);
4773 			break;
4774 		}
4775 	case SCTP_MAX_BURST:
4776 		{
4777 			struct sctp_assoc_value *av;
4778 
4779 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4780 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4781 
4782 			if (stcb) {
4783 				stcb->asoc.max_burst = av->assoc_value;
4784 				SCTP_TCB_UNLOCK(stcb);
4785 			} else {
4786 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4787 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4788 				    (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4789 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4790 					SCTP_INP_WLOCK(inp);
4791 					inp->sctp_ep.max_burst = av->assoc_value;
4792 					SCTP_INP_WUNLOCK(inp);
4793 				}
4794 				if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4795 				    (av->assoc_id == SCTP_ALL_ASSOC)) {
4796 					SCTP_INP_RLOCK(inp);
4797 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4798 						SCTP_TCB_LOCK(stcb);
4799 						stcb->asoc.max_burst = av->assoc_value;
4800 						SCTP_TCB_UNLOCK(stcb);
4801 					}
4802 					SCTP_INP_RUNLOCK(inp);
4803 				}
4804 			}
4805 			break;
4806 		}
4807 	case SCTP_MAXSEG:
4808 		{
4809 			struct sctp_assoc_value *av;
4810 			int ovh;
4811 
4812 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4813 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4814 
4815 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4816 				ovh = SCTP_MED_OVERHEAD;
4817 			} else {
4818 				ovh = SCTP_MED_V4_OVERHEAD;
4819 			}
4820 			if (stcb) {
4821 				if (av->assoc_value) {
4822 					stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
4823 				} else {
4824 					stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4825 				}
4826 				SCTP_TCB_UNLOCK(stcb);
4827 			} else {
4828 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4829 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4830 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4831 					SCTP_INP_WLOCK(inp);
4832 					/*
4833 					 * FIXME MT: I think this is not in
4834 					 * tune with the API ID
4835 					 */
4836 					if (av->assoc_value) {
4837 						inp->sctp_frag_point = (av->assoc_value + ovh);
4838 					} else {
4839 						inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4840 					}
4841 					SCTP_INP_WUNLOCK(inp);
4842 				} else {
4843 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4844 					error = EINVAL;
4845 				}
4846 			}
4847 			break;
4848 		}
4849 	case SCTP_EVENTS:
4850 		{
4851 			struct sctp_event_subscribe *events;
4852 
4853 			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
4854 
4855 			SCTP_INP_WLOCK(inp);
4856 			if (events->sctp_data_io_event) {
4857 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
4858 			} else {
4859 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
4860 			}
4861 
4862 			if (events->sctp_association_event) {
4863 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4864 			} else {
4865 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4866 			}
4867 
4868 			if (events->sctp_address_event) {
4869 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
4870 			} else {
4871 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
4872 			}
4873 
4874 			if (events->sctp_send_failure_event) {
4875 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4876 			} else {
4877 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4878 			}
4879 
4880 			if (events->sctp_peer_error_event) {
4881 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
4882 			} else {
4883 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
4884 			}
4885 
4886 			if (events->sctp_shutdown_event) {
4887 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4888 			} else {
4889 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4890 			}
4891 
4892 			if (events->sctp_partial_delivery_event) {
4893 				sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
4894 			} else {
4895 				sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
4896 			}
4897 
4898 			if (events->sctp_adaptation_layer_event) {
4899 				sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4900 			} else {
4901 				sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4902 			}
4903 
4904 			if (events->sctp_authentication_event) {
4905 				sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
4906 			} else {
4907 				sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
4908 			}
4909 
4910 			if (events->sctp_sender_dry_event) {
4911 				sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
4912 			} else {
4913 				sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
4914 			}
4915 
4916 			if (events->sctp_stream_reset_event) {
4917 				sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
4918 			} else {
4919 				sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
4920 			}
4921 			SCTP_INP_WUNLOCK(inp);
4922 
4923 			SCTP_INP_RLOCK(inp);
4924 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4925 				SCTP_TCB_LOCK(stcb);
4926 				if (events->sctp_association_event) {
4927 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4928 				} else {
4929 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4930 				}
4931 				if (events->sctp_address_event) {
4932 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
4933 				} else {
4934 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
4935 				}
4936 				if (events->sctp_send_failure_event) {
4937 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4938 				} else {
4939 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4940 				}
4941 				if (events->sctp_peer_error_event) {
4942 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
4943 				} else {
4944 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
4945 				}
4946 				if (events->sctp_shutdown_event) {
4947 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4948 				} else {
4949 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4950 				}
4951 				if (events->sctp_partial_delivery_event) {
4952 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
4953 				} else {
4954 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
4955 				}
4956 				if (events->sctp_adaptation_layer_event) {
4957 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4958 				} else {
4959 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4960 				}
4961 				if (events->sctp_authentication_event) {
4962 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
4963 				} else {
4964 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
4965 				}
4966 				if (events->sctp_sender_dry_event) {
4967 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
4968 				} else {
4969 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
4970 				}
4971 				if (events->sctp_stream_reset_event) {
4972 					sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
4973 				} else {
4974 					sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
4975 				}
4976 				SCTP_TCB_UNLOCK(stcb);
4977 			}
4978 			/*
4979 			 * Send up the sender dry event only for 1-to-1
4980 			 * style sockets.
4981 			 */
4982 			if (events->sctp_sender_dry_event) {
4983 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4984 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4985 					stcb = LIST_FIRST(&inp->sctp_asoc_list);
4986 					if (stcb) {
4987 						SCTP_TCB_LOCK(stcb);
4988 						if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4989 						    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4990 						    (stcb->asoc.stream_queue_cnt == 0)) {
4991 							sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
4992 						}
4993 						SCTP_TCB_UNLOCK(stcb);
4994 					}
4995 				}
4996 			}
4997 			SCTP_INP_RUNLOCK(inp);
4998 			break;
4999 		}
5000 	case SCTP_ADAPTATION_LAYER:
5001 		{
5002 			struct sctp_setadaptation *adap_bits;
5003 
5004 			SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
5005 			SCTP_INP_WLOCK(inp);
5006 			inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
5007 			inp->sctp_ep.adaptation_layer_indicator_provided = 1;
5008 			SCTP_INP_WUNLOCK(inp);
5009 			break;
5010 		}
5011 #ifdef SCTP_DEBUG
5012 	case SCTP_SET_INITIAL_DBG_SEQ:
5013 		{
5014 			uint32_t *vvv;
5015 
5016 			SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
5017 			SCTP_INP_WLOCK(inp);
5018 			inp->sctp_ep.initial_sequence_debug = *vvv;
5019 			SCTP_INP_WUNLOCK(inp);
5020 			break;
5021 		}
5022 #endif
5023 	case SCTP_DEFAULT_SEND_PARAM:
5024 		{
5025 			struct sctp_sndrcvinfo *s_info;
5026 
5027 			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
5028 			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
5029 
5030 			if (stcb) {
5031 				if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5032 					memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5033 				} else {
5034 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5035 					error = EINVAL;
5036 				}
5037 				SCTP_TCB_UNLOCK(stcb);
5038 			} else {
5039 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5040 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5041 				    (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC) ||
5042 				    (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5043 					SCTP_INP_WLOCK(inp);
5044 					memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
5045 					SCTP_INP_WUNLOCK(inp);
5046 				}
5047 				if ((s_info->sinfo_assoc_id == SCTP_CURRENT_ASSOC) ||
5048 				    (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5049 					SCTP_INP_RLOCK(inp);
5050 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5051 						SCTP_TCB_LOCK(stcb);
5052 						if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5053 							memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5054 						}
5055 						SCTP_TCB_UNLOCK(stcb);
5056 					}
5057 					SCTP_INP_RUNLOCK(inp);
5058 				}
5059 			}
5060 			break;
5061 		}
5062 	case SCTP_PEER_ADDR_PARAMS:
5063 		{
5064 			struct sctp_paddrparams *paddrp;
5065 			struct sctp_nets *net;
5066 			struct sockaddr *addr;
5067 
5068 #if defined(INET) && defined(INET6)
5069 			struct sockaddr_in sin_store;
5070 
5071 #endif
5072 
5073 			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
5074 			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
5075 
5076 #if defined(INET) && defined(INET6)
5077 			if (paddrp->spp_address.ss_family == AF_INET6) {
5078 				struct sockaddr_in6 *sin6;
5079 
5080 				sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
5081 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5082 					in6_sin6_2_sin(&sin_store, sin6);
5083 					addr = (struct sockaddr *)&sin_store;
5084 				} else {
5085 					addr = (struct sockaddr *)&paddrp->spp_address;
5086 				}
5087 			} else {
5088 				addr = (struct sockaddr *)&paddrp->spp_address;
5089 			}
5090 #else
5091 			addr = (struct sockaddr *)&paddrp->spp_address;
5092 #endif
5093 			if (stcb != NULL) {
5094 				net = sctp_findnet(stcb, addr);
5095 			} else {
5096 				/*
5097 				 * We increment here since
5098 				 * sctp_findassociation_ep_addr() wil do a
5099 				 * decrement if it finds the stcb as long as
5100 				 * the locked tcb (last argument) is NOT a
5101 				 * TCB.. aka NULL.
5102 				 */
5103 				net = NULL;
5104 				SCTP_INP_INCR_REF(inp);
5105 				stcb = sctp_findassociation_ep_addr(&inp, addr,
5106 				    &net, NULL, NULL);
5107 				if (stcb == NULL) {
5108 					SCTP_INP_DECR_REF(inp);
5109 				}
5110 			}
5111 			if ((stcb != NULL) && (net == NULL)) {
5112 #ifdef INET
5113 				if (addr->sa_family == AF_INET) {
5114 
5115 					struct sockaddr_in *sin;
5116 
5117 					sin = (struct sockaddr_in *)addr;
5118 					if (sin->sin_addr.s_addr != INADDR_ANY) {
5119 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5120 						SCTP_TCB_UNLOCK(stcb);
5121 						error = EINVAL;
5122 						break;
5123 					}
5124 				} else
5125 #endif
5126 #ifdef INET6
5127 				if (addr->sa_family == AF_INET6) {
5128 					struct sockaddr_in6 *sin6;
5129 
5130 					sin6 = (struct sockaddr_in6 *)addr;
5131 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
5132 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5133 						SCTP_TCB_UNLOCK(stcb);
5134 						error = EINVAL;
5135 						break;
5136 					}
5137 				} else
5138 #endif
5139 				{
5140 					error = EAFNOSUPPORT;
5141 					SCTP_TCB_UNLOCK(stcb);
5142 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
5143 					break;
5144 				}
5145 			}
5146 			/* sanity checks */
5147 			if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
5148 				if (stcb)
5149 					SCTP_TCB_UNLOCK(stcb);
5150 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5151 				return (EINVAL);
5152 			}
5153 			if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
5154 				if (stcb)
5155 					SCTP_TCB_UNLOCK(stcb);
5156 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5157 				return (EINVAL);
5158 			}
5159 			if (stcb != NULL) {
5160 				/************************TCB SPECIFIC SET ******************/
5161 				/*
5162 				 * do we change the timer for HB, we run
5163 				 * only one?
5164 				 */
5165 				int ovh = 0;
5166 
5167 				if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5168 					ovh = SCTP_MED_OVERHEAD;
5169 				} else {
5170 					ovh = SCTP_MED_V4_OVERHEAD;
5171 				}
5172 
5173 				/* network sets ? */
5174 				if (net != NULL) {
5175 					/************************NET SPECIFIC SET ******************/
5176 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
5177 						if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
5178 						    !(net->dest_state & SCTP_ADDR_NOHB)) {
5179 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5180 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5181 						}
5182 						net->dest_state |= SCTP_ADDR_NOHB;
5183 					}
5184 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
5185 						if (paddrp->spp_hbinterval) {
5186 							net->heart_beat_delay = paddrp->spp_hbinterval;
5187 						} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5188 							net->heart_beat_delay = 0;
5189 						}
5190 						sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5191 						    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5192 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5193 						net->dest_state &= ~SCTP_ADDR_NOHB;
5194 					}
5195 					if (paddrp->spp_flags & SPP_HB_DEMAND) {
5196 						/* on demand HB */
5197 						sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5198 						sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
5199 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5200 					}
5201 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5202 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5203 							sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5204 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5205 						}
5206 						net->dest_state |= SCTP_ADDR_NO_PMTUD;
5207 						net->mtu = paddrp->spp_pathmtu + ovh;
5208 						if (net->mtu < stcb->asoc.smallest_mtu) {
5209 							sctp_pathmtu_adjustment(stcb, net->mtu);
5210 						}
5211 					}
5212 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5213 						if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5214 							sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5215 						}
5216 						net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5217 					}
5218 					if (paddrp->spp_pathmaxrxt) {
5219 						if (net->dest_state & SCTP_ADDR_PF) {
5220 							if (net->error_count > paddrp->spp_pathmaxrxt) {
5221 								net->dest_state &= ~SCTP_ADDR_PF;
5222 							}
5223 						} else {
5224 							if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5225 							    (net->error_count > net->pf_threshold)) {
5226 								net->dest_state |= SCTP_ADDR_PF;
5227 								sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5228 								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
5229 								sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5230 							}
5231 						}
5232 						if (net->dest_state & SCTP_ADDR_REACHABLE) {
5233 							if (net->error_count > paddrp->spp_pathmaxrxt) {
5234 								net->dest_state &= ~SCTP_ADDR_REACHABLE;
5235 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5236 							}
5237 						} else {
5238 							if (net->error_count <= paddrp->spp_pathmaxrxt) {
5239 								net->dest_state |= SCTP_ADDR_REACHABLE;
5240 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5241 							}
5242 						}
5243 						net->failure_threshold = paddrp->spp_pathmaxrxt;
5244 					}
5245 					if (paddrp->spp_flags & SPP_DSCP) {
5246 						net->dscp = paddrp->spp_dscp & 0xfc;
5247 						net->dscp |= 0x01;
5248 					}
5249 #ifdef INET6
5250 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5251 						if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5252 							net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5253 							net->flowlabel |= 0x80000000;
5254 						}
5255 					}
5256 #endif
5257 				} else {
5258 					/************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
5259 					if (paddrp->spp_pathmaxrxt != 0) {
5260 						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
5261 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5262 							if (net->dest_state & SCTP_ADDR_PF) {
5263 								if (net->error_count > paddrp->spp_pathmaxrxt) {
5264 									net->dest_state &= ~SCTP_ADDR_PF;
5265 								}
5266 							} else {
5267 								if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5268 								    (net->error_count > net->pf_threshold)) {
5269 									net->dest_state |= SCTP_ADDR_PF;
5270 									sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5271 									sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
5272 									sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5273 								}
5274 							}
5275 							if (net->dest_state & SCTP_ADDR_REACHABLE) {
5276 								if (net->error_count > paddrp->spp_pathmaxrxt) {
5277 									net->dest_state &= ~SCTP_ADDR_REACHABLE;
5278 									sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5279 								}
5280 							} else {
5281 								if (net->error_count <= paddrp->spp_pathmaxrxt) {
5282 									net->dest_state |= SCTP_ADDR_REACHABLE;
5283 									sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5284 								}
5285 							}
5286 							net->failure_threshold = paddrp->spp_pathmaxrxt;
5287 						}
5288 					}
5289 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
5290 						if (paddrp->spp_hbinterval != 0) {
5291 							stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
5292 						} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5293 							stcb->asoc.heart_beat_delay = 0;
5294 						}
5295 						/* Turn back on the timer */
5296 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5297 							if (paddrp->spp_hbinterval != 0) {
5298 								net->heart_beat_delay = paddrp->spp_hbinterval;
5299 							} else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5300 								net->heart_beat_delay = 0;
5301 							}
5302 							if (net->dest_state & SCTP_ADDR_NOHB) {
5303 								net->dest_state &= ~SCTP_ADDR_NOHB;
5304 							}
5305 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5306 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5307 							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5308 						}
5309 						sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5310 					}
5311 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
5312 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5313 							if (!(net->dest_state & SCTP_ADDR_NOHB)) {
5314 								net->dest_state |= SCTP_ADDR_NOHB;
5315 								if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5316 									sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5317 								}
5318 							}
5319 						}
5320 						sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5321 					}
5322 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5323 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5324 							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5325 								sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5326 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5327 							}
5328 							net->dest_state |= SCTP_ADDR_NO_PMTUD;
5329 							net->mtu = paddrp->spp_pathmtu + ovh;
5330 							if (net->mtu < stcb->asoc.smallest_mtu) {
5331 								sctp_pathmtu_adjustment(stcb, net->mtu);
5332 							}
5333 						}
5334 						sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5335 					}
5336 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5337 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5338 							if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5339 								sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5340 							}
5341 							net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5342 						}
5343 						sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5344 					}
5345 					if (paddrp->spp_flags & SPP_DSCP) {
5346 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5347 							net->dscp = paddrp->spp_dscp & 0xfc;
5348 							net->dscp |= 0x01;
5349 						}
5350 						stcb->asoc.default_dscp = paddrp->spp_dscp & 0xfc;
5351 						stcb->asoc.default_dscp |= 0x01;
5352 					}
5353 #ifdef INET6
5354 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5355 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5356 							if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5357 								net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5358 								net->flowlabel |= 0x80000000;
5359 							}
5360 						}
5361 						stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5362 						stcb->asoc.default_flowlabel |= 0x80000000;
5363 					}
5364 #endif
5365 				}
5366 				SCTP_TCB_UNLOCK(stcb);
5367 			} else {
5368 				/************************NO TCB, SET TO default stuff ******************/
5369 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5370 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5371 				    (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
5372 					SCTP_INP_WLOCK(inp);
5373 					/*
5374 					 * For the TOS/FLOWLABEL stuff you
5375 					 * set it with the options on the
5376 					 * socket
5377 					 */
5378 					if (paddrp->spp_pathmaxrxt != 0) {
5379 						inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
5380 					}
5381 					if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
5382 						inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5383 					else if (paddrp->spp_hbinterval != 0) {
5384 						if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
5385 							paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
5386 						inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5387 					}
5388 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
5389 						if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5390 							inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5391 						} else if (paddrp->spp_hbinterval) {
5392 							inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5393 						}
5394 						sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5395 					} else if (paddrp->spp_flags & SPP_HB_DISABLE) {
5396 						sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5397 					}
5398 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5399 						sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5400 					} else if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
5401 						sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5402 					}
5403 					if (paddrp->spp_flags & SPP_DSCP) {
5404 						inp->sctp_ep.default_dscp = paddrp->spp_dscp & 0xfc;
5405 						inp->sctp_ep.default_dscp |= 0x01;
5406 					}
5407 #ifdef INET6
5408 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5409 						if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5410 							inp->sctp_ep.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5411 							inp->sctp_ep.default_flowlabel |= 0x80000000;
5412 						}
5413 					}
5414 #endif
5415 					SCTP_INP_WUNLOCK(inp);
5416 				} else {
5417 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5418 					error = EINVAL;
5419 				}
5420 			}
5421 			break;
5422 		}
5423 	case SCTP_RTOINFO:
5424 		{
5425 			struct sctp_rtoinfo *srto;
5426 			uint32_t new_init, new_min, new_max;
5427 
5428 			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
5429 			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
5430 
5431 			if (stcb) {
5432 				if (srto->srto_initial)
5433 					new_init = srto->srto_initial;
5434 				else
5435 					new_init = stcb->asoc.initial_rto;
5436 				if (srto->srto_max)
5437 					new_max = srto->srto_max;
5438 				else
5439 					new_max = stcb->asoc.maxrto;
5440 				if (srto->srto_min)
5441 					new_min = srto->srto_min;
5442 				else
5443 					new_min = stcb->asoc.minrto;
5444 				if ((new_min <= new_init) && (new_init <= new_max)) {
5445 					stcb->asoc.initial_rto = new_init;
5446 					stcb->asoc.maxrto = new_max;
5447 					stcb->asoc.minrto = new_min;
5448 				} else {
5449 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5450 					error = EINVAL;
5451 				}
5452 				SCTP_TCB_UNLOCK(stcb);
5453 			} else {
5454 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5455 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5456 				    (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
5457 					SCTP_INP_WLOCK(inp);
5458 					if (srto->srto_initial)
5459 						new_init = srto->srto_initial;
5460 					else
5461 						new_init = inp->sctp_ep.initial_rto;
5462 					if (srto->srto_max)
5463 						new_max = srto->srto_max;
5464 					else
5465 						new_max = inp->sctp_ep.sctp_maxrto;
5466 					if (srto->srto_min)
5467 						new_min = srto->srto_min;
5468 					else
5469 						new_min = inp->sctp_ep.sctp_minrto;
5470 					if ((new_min <= new_init) && (new_init <= new_max)) {
5471 						inp->sctp_ep.initial_rto = new_init;
5472 						inp->sctp_ep.sctp_maxrto = new_max;
5473 						inp->sctp_ep.sctp_minrto = new_min;
5474 					} else {
5475 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5476 						error = EINVAL;
5477 					}
5478 					SCTP_INP_WUNLOCK(inp);
5479 				} else {
5480 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5481 					error = EINVAL;
5482 				}
5483 			}
5484 			break;
5485 		}
5486 	case SCTP_ASSOCINFO:
5487 		{
5488 			struct sctp_assocparams *sasoc;
5489 
5490 			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
5491 			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
5492 			if (sasoc->sasoc_cookie_life) {
5493 				/* boundary check the cookie life */
5494 				if (sasoc->sasoc_cookie_life < 1000)
5495 					sasoc->sasoc_cookie_life = 1000;
5496 				if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
5497 					sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
5498 				}
5499 			}
5500 			if (stcb) {
5501 				if (sasoc->sasoc_asocmaxrxt)
5502 					stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
5503 				if (sasoc->sasoc_cookie_life) {
5504 					stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5505 				}
5506 				SCTP_TCB_UNLOCK(stcb);
5507 			} else {
5508 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5509 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5510 				    (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
5511 					SCTP_INP_WLOCK(inp);
5512 					if (sasoc->sasoc_asocmaxrxt)
5513 						inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
5514 					if (sasoc->sasoc_cookie_life) {
5515 						inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5516 					}
5517 					SCTP_INP_WUNLOCK(inp);
5518 				} else {
5519 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5520 					error = EINVAL;
5521 				}
5522 			}
5523 			break;
5524 		}
5525 	case SCTP_INITMSG:
5526 		{
5527 			struct sctp_initmsg *sinit;
5528 
5529 			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
5530 			SCTP_INP_WLOCK(inp);
5531 			if (sinit->sinit_num_ostreams)
5532 				inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
5533 
5534 			if (sinit->sinit_max_instreams)
5535 				inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
5536 
5537 			if (sinit->sinit_max_attempts)
5538 				inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
5539 
5540 			if (sinit->sinit_max_init_timeo)
5541 				inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
5542 			SCTP_INP_WUNLOCK(inp);
5543 			break;
5544 		}
5545 	case SCTP_PRIMARY_ADDR:
5546 		{
5547 			struct sctp_setprim *spa;
5548 			struct sctp_nets *net;
5549 			struct sockaddr *addr;
5550 
5551 #if defined(INET) && defined(INET6)
5552 			struct sockaddr_in sin_store;
5553 
5554 #endif
5555 
5556 			SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
5557 			SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
5558 
5559 #if defined(INET) && defined(INET6)
5560 			if (spa->ssp_addr.ss_family == AF_INET6) {
5561 				struct sockaddr_in6 *sin6;
5562 
5563 				sin6 = (struct sockaddr_in6 *)&spa->ssp_addr;
5564 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5565 					in6_sin6_2_sin(&sin_store, sin6);
5566 					addr = (struct sockaddr *)&sin_store;
5567 				} else {
5568 					addr = (struct sockaddr *)&spa->ssp_addr;
5569 				}
5570 			} else {
5571 				addr = (struct sockaddr *)&spa->ssp_addr;
5572 			}
5573 #else
5574 			addr = (struct sockaddr *)&spa->ssp_addr;
5575 #endif
5576 			if (stcb != NULL) {
5577 				net = sctp_findnet(stcb, addr);
5578 			} else {
5579 				/*
5580 				 * We increment here since
5581 				 * sctp_findassociation_ep_addr() wil do a
5582 				 * decrement if it finds the stcb as long as
5583 				 * the locked tcb (last argument) is NOT a
5584 				 * TCB.. aka NULL.
5585 				 */
5586 				net = NULL;
5587 				SCTP_INP_INCR_REF(inp);
5588 				stcb = sctp_findassociation_ep_addr(&inp, addr,
5589 				    &net, NULL, NULL);
5590 				if (stcb == NULL) {
5591 					SCTP_INP_DECR_REF(inp);
5592 				}
5593 			}
5594 
5595 			if ((stcb != NULL) && (net != NULL)) {
5596 				if ((net != stcb->asoc.primary_destination) &&
5597 				    (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
5598 					/* Ok we need to set it */
5599 					if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
5600 						if ((stcb->asoc.alternate) &&
5601 						    (!(net->dest_state & SCTP_ADDR_PF)) &&
5602 						    (net->dest_state & SCTP_ADDR_REACHABLE)) {
5603 							sctp_free_remote_addr(stcb->asoc.alternate);
5604 							stcb->asoc.alternate = NULL;
5605 						}
5606 					}
5607 				}
5608 			} else {
5609 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5610 				error = EINVAL;
5611 			}
5612 			if (stcb != NULL) {
5613 				SCTP_TCB_UNLOCK(stcb);
5614 			}
5615 			break;
5616 		}
5617 	case SCTP_SET_DYNAMIC_PRIMARY:
5618 		{
5619 			union sctp_sockstore *ss;
5620 
5621 			error = priv_check(curthread,
5622 			    PRIV_NETINET_RESERVEDPORT);
5623 			if (error)
5624 				break;
5625 
5626 			SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
5627 			/* SUPER USER CHECK? */
5628 			error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
5629 			break;
5630 		}
5631 	case SCTP_SET_PEER_PRIMARY_ADDR:
5632 		{
5633 			struct sctp_setpeerprim *sspp;
5634 			struct sockaddr *addr;
5635 
5636 #if defined(INET) && defined(INET6)
5637 			struct sockaddr_in sin_store;
5638 
5639 #endif
5640 
5641 			SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
5642 			SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
5643 			if (stcb != NULL) {
5644 				struct sctp_ifa *ifa;
5645 
5646 #if defined(INET) && defined(INET6)
5647 				if (sspp->sspp_addr.ss_family == AF_INET6) {
5648 					struct sockaddr_in6 *sin6;
5649 
5650 					sin6 = (struct sockaddr_in6 *)&sspp->sspp_addr;
5651 					if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5652 						in6_sin6_2_sin(&sin_store, sin6);
5653 						addr = (struct sockaddr *)&sin_store;
5654 					} else {
5655 						addr = (struct sockaddr *)&sspp->sspp_addr;
5656 					}
5657 				} else {
5658 					addr = (struct sockaddr *)&sspp->sspp_addr;
5659 				}
5660 #else
5661 				addr = (struct sockaddr *)&sspp->sspp_addr;
5662 #endif
5663 				ifa = sctp_find_ifa_by_addr(addr, stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
5664 				if (ifa == NULL) {
5665 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5666 					error = EINVAL;
5667 					goto out_of_it;
5668 				}
5669 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
5670 					/*
5671 					 * Must validate the ifa found is in
5672 					 * our ep
5673 					 */
5674 					struct sctp_laddr *laddr;
5675 					int found = 0;
5676 
5677 					LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5678 						if (laddr->ifa == NULL) {
5679 							SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
5680 							    __FUNCTION__);
5681 							continue;
5682 						}
5683 						if (laddr->ifa == ifa) {
5684 							found = 1;
5685 							break;
5686 						}
5687 					}
5688 					if (!found) {
5689 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5690 						error = EINVAL;
5691 						goto out_of_it;
5692 					}
5693 				} else {
5694 					switch (addr->sa_family) {
5695 #ifdef INET
5696 					case AF_INET:
5697 						{
5698 							struct sockaddr_in *sin;
5699 
5700 							sin = (struct sockaddr_in *)addr;
5701 							if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
5702 							    &sin->sin_addr) != 0) {
5703 								SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5704 								error = EINVAL;
5705 								goto out_of_it;
5706 							}
5707 							break;
5708 						}
5709 #endif
5710 #ifdef INET6
5711 					case AF_INET6:
5712 						{
5713 							struct sockaddr_in6 *sin6;
5714 
5715 							sin6 = (struct sockaddr_in6 *)addr;
5716 							if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
5717 							    &sin6->sin6_addr) != 0) {
5718 								SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5719 								error = EINVAL;
5720 								goto out_of_it;
5721 							}
5722 							break;
5723 						}
5724 #endif
5725 					default:
5726 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5727 						error = EINVAL;
5728 						goto out_of_it;
5729 					}
5730 				}
5731 				if (sctp_set_primary_ip_address_sa(stcb, addr) != 0) {
5732 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5733 					error = EINVAL;
5734 				}
5735 		out_of_it:
5736 				SCTP_TCB_UNLOCK(stcb);
5737 			} else {
5738 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5739 				error = EINVAL;
5740 			}
5741 			break;
5742 		}
5743 	case SCTP_BINDX_ADD_ADDR:
5744 		{
5745 			struct sctp_getaddresses *addrs;
5746 			struct thread *td;
5747 
5748 			td = (struct thread *)p;
5749 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
5750 			    optsize);
5751 #ifdef INET
5752 			if (addrs->addr->sa_family == AF_INET) {
5753 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5754 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5755 					error = EINVAL;
5756 					break;
5757 				}
5758 				if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5759 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5760 					break;
5761 				}
5762 			} else
5763 #endif
5764 #ifdef INET6
5765 			if (addrs->addr->sa_family == AF_INET6) {
5766 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5767 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5768 					error = EINVAL;
5769 					break;
5770 				}
5771 				if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5772 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5773 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5774 					break;
5775 				}
5776 			} else
5777 #endif
5778 			{
5779 				error = EAFNOSUPPORT;
5780 				break;
5781 			}
5782 			sctp_bindx_add_address(so, inp, addrs->addr,
5783 			    addrs->sget_assoc_id, vrf_id,
5784 			    &error, p);
5785 			break;
5786 		}
5787 	case SCTP_BINDX_REM_ADDR:
5788 		{
5789 			struct sctp_getaddresses *addrs;
5790 			struct thread *td;
5791 
5792 			td = (struct thread *)p;
5793 
5794 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
5795 #ifdef INET
5796 			if (addrs->addr->sa_family == AF_INET) {
5797 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5798 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5799 					error = EINVAL;
5800 					break;
5801 				}
5802 				if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5803 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5804 					break;
5805 				}
5806 			} else
5807 #endif
5808 #ifdef INET6
5809 			if (addrs->addr->sa_family == AF_INET6) {
5810 				if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5811 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5812 					error = EINVAL;
5813 					break;
5814 				}
5815 				if (td != NULL &&
5816 				    (error = prison_local_ip6(td->td_ucred,
5817 				    &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5818 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5819 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5820 					break;
5821 				}
5822 			} else
5823 #endif
5824 			{
5825 				error = EAFNOSUPPORT;
5826 				break;
5827 			}
5828 			sctp_bindx_delete_address(inp, addrs->addr,
5829 			    addrs->sget_assoc_id, vrf_id,
5830 			    &error);
5831 			break;
5832 		}
5833 	case SCTP_EVENT:
5834 		{
5835 			struct sctp_event *event;
5836 			uint32_t event_type;
5837 
5838 			SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, optsize);
5839 			SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
5840 			switch (event->se_type) {
5841 			case SCTP_ASSOC_CHANGE:
5842 				event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
5843 				break;
5844 			case SCTP_PEER_ADDR_CHANGE:
5845 				event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
5846 				break;
5847 			case SCTP_REMOTE_ERROR:
5848 				event_type = SCTP_PCB_FLAGS_RECVPEERERR;
5849 				break;
5850 			case SCTP_SEND_FAILED:
5851 				event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
5852 				break;
5853 			case SCTP_SHUTDOWN_EVENT:
5854 				event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
5855 				break;
5856 			case SCTP_ADAPTATION_INDICATION:
5857 				event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
5858 				break;
5859 			case SCTP_PARTIAL_DELIVERY_EVENT:
5860 				event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
5861 				break;
5862 			case SCTP_AUTHENTICATION_EVENT:
5863 				event_type = SCTP_PCB_FLAGS_AUTHEVNT;
5864 				break;
5865 			case SCTP_STREAM_RESET_EVENT:
5866 				event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
5867 				break;
5868 			case SCTP_SENDER_DRY_EVENT:
5869 				event_type = SCTP_PCB_FLAGS_DRYEVNT;
5870 				break;
5871 			case SCTP_NOTIFICATIONS_STOPPED_EVENT:
5872 				event_type = 0;
5873 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
5874 				error = ENOTSUP;
5875 				break;
5876 			case SCTP_ASSOC_RESET_EVENT:
5877 				event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
5878 				break;
5879 			case SCTP_STREAM_CHANGE_EVENT:
5880 				event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
5881 				break;
5882 			case SCTP_SEND_FAILED_EVENT:
5883 				event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
5884 				break;
5885 			default:
5886 				event_type = 0;
5887 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5888 				error = EINVAL;
5889 				break;
5890 			}
5891 			if (event_type > 0) {
5892 				if (stcb) {
5893 					if (event->se_on) {
5894 						sctp_stcb_feature_on(inp, stcb, event_type);
5895 						if (event_type == SCTP_PCB_FLAGS_DRYEVNT) {
5896 							if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5897 							    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5898 							    (stcb->asoc.stream_queue_cnt == 0)) {
5899 								sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
5900 							}
5901 						}
5902 					} else {
5903 						sctp_stcb_feature_off(inp, stcb, event_type);
5904 					}
5905 					SCTP_TCB_UNLOCK(stcb);
5906 				} else {
5907 					/*
5908 					 * We don't want to send up a storm
5909 					 * of events, so return an error for
5910 					 * sender dry events
5911 					 */
5912 					if ((event_type == SCTP_PCB_FLAGS_DRYEVNT) &&
5913 					    ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) == 0) &&
5914 					    ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) &&
5915 					    ((event->se_assoc_id == SCTP_ALL_ASSOC) ||
5916 					    (event->se_assoc_id == SCTP_CURRENT_ASSOC))) {
5917 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
5918 						error = ENOTSUP;
5919 						break;
5920 					}
5921 					if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5922 					    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5923 					    (event->se_assoc_id == SCTP_FUTURE_ASSOC) ||
5924 					    (event->se_assoc_id == SCTP_ALL_ASSOC)) {
5925 						SCTP_INP_WLOCK(inp);
5926 						if (event->se_on) {
5927 							sctp_feature_on(inp, event_type);
5928 						} else {
5929 							sctp_feature_off(inp, event_type);
5930 						}
5931 						SCTP_INP_WUNLOCK(inp);
5932 					}
5933 					if ((event->se_assoc_id == SCTP_CURRENT_ASSOC) ||
5934 					    (event->se_assoc_id == SCTP_ALL_ASSOC)) {
5935 						SCTP_INP_RLOCK(inp);
5936 						LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5937 							SCTP_TCB_LOCK(stcb);
5938 							if (event->se_on) {
5939 								sctp_stcb_feature_on(inp, stcb, event_type);
5940 							} else {
5941 								sctp_stcb_feature_off(inp, stcb, event_type);
5942 							}
5943 							SCTP_TCB_UNLOCK(stcb);
5944 						}
5945 						SCTP_INP_RUNLOCK(inp);
5946 					}
5947 				}
5948 			}
5949 			break;
5950 		}
5951 	case SCTP_RECVRCVINFO:
5952 		{
5953 			int *onoff;
5954 
5955 			SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
5956 			SCTP_INP_WLOCK(inp);
5957 			if (*onoff != 0) {
5958 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
5959 			} else {
5960 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
5961 			}
5962 			SCTP_INP_WUNLOCK(inp);
5963 			break;
5964 		}
5965 	case SCTP_RECVNXTINFO:
5966 		{
5967 			int *onoff;
5968 
5969 			SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
5970 			SCTP_INP_WLOCK(inp);
5971 			if (*onoff != 0) {
5972 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
5973 			} else {
5974 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
5975 			}
5976 			SCTP_INP_WUNLOCK(inp);
5977 			break;
5978 		}
5979 	case SCTP_DEFAULT_SNDINFO:
5980 		{
5981 			struct sctp_sndinfo *info;
5982 			uint16_t policy;
5983 
5984 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, optsize);
5985 			SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
5986 
5987 			if (stcb) {
5988 				if (info->snd_sid < stcb->asoc.streamoutcnt) {
5989 					stcb->asoc.def_send.sinfo_stream = info->snd_sid;
5990 					policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
5991 					stcb->asoc.def_send.sinfo_flags = info->snd_flags;
5992 					stcb->asoc.def_send.sinfo_flags |= policy;
5993 					stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
5994 					stcb->asoc.def_send.sinfo_context = info->snd_context;
5995 				} else {
5996 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5997 					error = EINVAL;
5998 				}
5999 				SCTP_TCB_UNLOCK(stcb);
6000 			} else {
6001 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6002 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6003 				    (info->snd_assoc_id == SCTP_FUTURE_ASSOC) ||
6004 				    (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6005 					SCTP_INP_WLOCK(inp);
6006 					inp->def_send.sinfo_stream = info->snd_sid;
6007 					policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
6008 					inp->def_send.sinfo_flags = info->snd_flags;
6009 					inp->def_send.sinfo_flags |= policy;
6010 					inp->def_send.sinfo_ppid = info->snd_ppid;
6011 					inp->def_send.sinfo_context = info->snd_context;
6012 					SCTP_INP_WUNLOCK(inp);
6013 				}
6014 				if ((info->snd_assoc_id == SCTP_CURRENT_ASSOC) ||
6015 				    (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6016 					SCTP_INP_RLOCK(inp);
6017 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6018 						SCTP_TCB_LOCK(stcb);
6019 						if (info->snd_sid < stcb->asoc.streamoutcnt) {
6020 							stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6021 							policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6022 							stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6023 							stcb->asoc.def_send.sinfo_flags |= policy;
6024 							stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6025 							stcb->asoc.def_send.sinfo_context = info->snd_context;
6026 						}
6027 						SCTP_TCB_UNLOCK(stcb);
6028 					}
6029 					SCTP_INP_RUNLOCK(inp);
6030 				}
6031 			}
6032 			break;
6033 		}
6034 	case SCTP_DEFAULT_PRINFO:
6035 		{
6036 			struct sctp_default_prinfo *info;
6037 
6038 			SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, optsize);
6039 			SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
6040 
6041 			if (info->pr_policy > SCTP_PR_SCTP_MAX) {
6042 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6043 				error = EINVAL;
6044 				break;
6045 			}
6046 			if (stcb) {
6047 				stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6048 				stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6049 				stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6050 				SCTP_TCB_UNLOCK(stcb);
6051 			} else {
6052 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6053 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6054 				    (info->pr_assoc_id == SCTP_FUTURE_ASSOC) ||
6055 				    (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6056 					SCTP_INP_WLOCK(inp);
6057 					inp->def_send.sinfo_flags &= 0xfff0;
6058 					inp->def_send.sinfo_flags |= info->pr_policy;
6059 					inp->def_send.sinfo_timetolive = info->pr_value;
6060 					SCTP_INP_WUNLOCK(inp);
6061 				}
6062 				if ((info->pr_assoc_id == SCTP_CURRENT_ASSOC) ||
6063 				    (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6064 					SCTP_INP_RLOCK(inp);
6065 					LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6066 						SCTP_TCB_LOCK(stcb);
6067 						stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6068 						stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6069 						stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6070 						SCTP_TCB_UNLOCK(stcb);
6071 					}
6072 					SCTP_INP_RUNLOCK(inp);
6073 				}
6074 			}
6075 			break;
6076 		}
6077 	case SCTP_PEER_ADDR_THLDS:
6078 		/* Applies to the specific association */
6079 		{
6080 			struct sctp_paddrthlds *thlds;
6081 			struct sctp_nets *net;
6082 			struct sockaddr *addr;
6083 
6084 #if defined(INET) && defined(INET6)
6085 			struct sockaddr_in sin_store;
6086 
6087 #endif
6088 
6089 			SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, optsize);
6090 			SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
6091 
6092 #if defined(INET) && defined(INET6)
6093 			if (thlds->spt_address.ss_family == AF_INET6) {
6094 				struct sockaddr_in6 *sin6;
6095 
6096 				sin6 = (struct sockaddr_in6 *)&thlds->spt_address;
6097 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6098 					in6_sin6_2_sin(&sin_store, sin6);
6099 					addr = (struct sockaddr *)&sin_store;
6100 				} else {
6101 					addr = (struct sockaddr *)&thlds->spt_address;
6102 				}
6103 			} else {
6104 				addr = (struct sockaddr *)&thlds->spt_address;
6105 			}
6106 #else
6107 			addr = (struct sockaddr *)&thlds->spt_address;
6108 #endif
6109 			if (stcb != NULL) {
6110 				net = sctp_findnet(stcb, addr);
6111 			} else {
6112 				/*
6113 				 * We increment here since
6114 				 * sctp_findassociation_ep_addr() wil do a
6115 				 * decrement if it finds the stcb as long as
6116 				 * the locked tcb (last argument) is NOT a
6117 				 * TCB.. aka NULL.
6118 				 */
6119 				net = NULL;
6120 				SCTP_INP_INCR_REF(inp);
6121 				stcb = sctp_findassociation_ep_addr(&inp, addr,
6122 				    &net, NULL, NULL);
6123 				if (stcb == NULL) {
6124 					SCTP_INP_DECR_REF(inp);
6125 				}
6126 			}
6127 			if ((stcb != NULL) && (net == NULL)) {
6128 #ifdef INET
6129 				if (addr->sa_family == AF_INET) {
6130 
6131 					struct sockaddr_in *sin;
6132 
6133 					sin = (struct sockaddr_in *)addr;
6134 					if (sin->sin_addr.s_addr != INADDR_ANY) {
6135 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6136 						SCTP_TCB_UNLOCK(stcb);
6137 						error = EINVAL;
6138 						break;
6139 					}
6140 				} else
6141 #endif
6142 #ifdef INET6
6143 				if (addr->sa_family == AF_INET6) {
6144 					struct sockaddr_in6 *sin6;
6145 
6146 					sin6 = (struct sockaddr_in6 *)addr;
6147 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6148 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6149 						SCTP_TCB_UNLOCK(stcb);
6150 						error = EINVAL;
6151 						break;
6152 					}
6153 				} else
6154 #endif
6155 				{
6156 					error = EAFNOSUPPORT;
6157 					SCTP_TCB_UNLOCK(stcb);
6158 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6159 					break;
6160 				}
6161 			}
6162 			if (stcb != NULL) {
6163 				if (net != NULL) {
6164 					if (net->dest_state & SCTP_ADDR_PF) {
6165 						if ((net->failure_threshold > thlds->spt_pathmaxrxt) ||
6166 						    (net->failure_threshold <= thlds->spt_pathpfthld)) {
6167 							net->dest_state &= ~SCTP_ADDR_PF;
6168 						}
6169 					} else {
6170 						if ((net->failure_threshold > thlds->spt_pathpfthld) &&
6171 						    (net->failure_threshold <= thlds->spt_pathmaxrxt)) {
6172 							net->dest_state |= SCTP_ADDR_PF;
6173 							sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6174 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
6175 							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6176 						}
6177 					}
6178 					if (net->dest_state & SCTP_ADDR_REACHABLE) {
6179 						if (net->failure_threshold > thlds->spt_pathmaxrxt) {
6180 							net->dest_state &= ~SCTP_ADDR_REACHABLE;
6181 							sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6182 						}
6183 					} else {
6184 						if (net->failure_threshold <= thlds->spt_pathmaxrxt) {
6185 							net->dest_state |= SCTP_ADDR_REACHABLE;
6186 							sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6187 						}
6188 					}
6189 					net->failure_threshold = thlds->spt_pathmaxrxt;
6190 					net->pf_threshold = thlds->spt_pathpfthld;
6191 				} else {
6192 					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6193 						if (net->dest_state & SCTP_ADDR_PF) {
6194 							if ((net->failure_threshold > thlds->spt_pathmaxrxt) ||
6195 							    (net->failure_threshold <= thlds->spt_pathpfthld)) {
6196 								net->dest_state &= ~SCTP_ADDR_PF;
6197 							}
6198 						} else {
6199 							if ((net->failure_threshold > thlds->spt_pathpfthld) &&
6200 							    (net->failure_threshold <= thlds->spt_pathmaxrxt)) {
6201 								net->dest_state |= SCTP_ADDR_PF;
6202 								sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6203 								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
6204 								sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6205 							}
6206 						}
6207 						if (net->dest_state & SCTP_ADDR_REACHABLE) {
6208 							if (net->failure_threshold > thlds->spt_pathmaxrxt) {
6209 								net->dest_state &= ~SCTP_ADDR_REACHABLE;
6210 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6211 							}
6212 						} else {
6213 							if (net->failure_threshold <= thlds->spt_pathmaxrxt) {
6214 								net->dest_state |= SCTP_ADDR_REACHABLE;
6215 								sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6216 							}
6217 						}
6218 						net->failure_threshold = thlds->spt_pathmaxrxt;
6219 						net->pf_threshold = thlds->spt_pathpfthld;
6220 					}
6221 					stcb->asoc.def_net_failure = thlds->spt_pathmaxrxt;
6222 					stcb->asoc.def_net_pf_threshold = thlds->spt_pathpfthld;
6223 				}
6224 			} else {
6225 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6226 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6227 				    (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
6228 					SCTP_INP_WLOCK(inp);
6229 					inp->sctp_ep.def_net_failure = thlds->spt_pathmaxrxt;
6230 					inp->sctp_ep.def_net_pf_threshold = thlds->spt_pathpfthld;
6231 					SCTP_INP_WUNLOCK(inp);
6232 				} else {
6233 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6234 					error = EINVAL;
6235 				}
6236 			}
6237 			break;
6238 		}
6239 	case SCTP_REMOTE_UDP_ENCAPS_PORT:
6240 		{
6241 			struct sctp_udpencaps *encaps;
6242 			struct sctp_nets *net;
6243 			struct sockaddr *addr;
6244 
6245 #if defined(INET) && defined(INET6)
6246 			struct sockaddr_in sin_store;
6247 
6248 #endif
6249 
6250 			SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, optsize);
6251 			SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
6252 
6253 #if defined(INET) && defined(INET6)
6254 			if (encaps->sue_address.ss_family == AF_INET6) {
6255 				struct sockaddr_in6 *sin6;
6256 
6257 				sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
6258 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6259 					in6_sin6_2_sin(&sin_store, sin6);
6260 					addr = (struct sockaddr *)&sin_store;
6261 				} else {
6262 					addr = (struct sockaddr *)&encaps->sue_address;
6263 				}
6264 			} else {
6265 				addr = (struct sockaddr *)&encaps->sue_address;
6266 			}
6267 #else
6268 			addr = (struct sockaddr *)&encaps->sue_address;
6269 #endif
6270 			if (stcb != NULL) {
6271 				net = sctp_findnet(stcb, addr);
6272 			} else {
6273 				/*
6274 				 * We increment here since
6275 				 * sctp_findassociation_ep_addr() wil do a
6276 				 * decrement if it finds the stcb as long as
6277 				 * the locked tcb (last argument) is NOT a
6278 				 * TCB.. aka NULL.
6279 				 */
6280 				net = NULL;
6281 				SCTP_INP_INCR_REF(inp);
6282 				stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
6283 				if (stcb == NULL) {
6284 					SCTP_INP_DECR_REF(inp);
6285 				}
6286 			}
6287 			if ((stcb != NULL) && (net == NULL)) {
6288 #ifdef INET
6289 				if (addr->sa_family == AF_INET) {
6290 
6291 					struct sockaddr_in *sin;
6292 
6293 					sin = (struct sockaddr_in *)addr;
6294 					if (sin->sin_addr.s_addr != INADDR_ANY) {
6295 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6296 						SCTP_TCB_UNLOCK(stcb);
6297 						error = EINVAL;
6298 						break;
6299 					}
6300 				} else
6301 #endif
6302 #ifdef INET6
6303 				if (addr->sa_family == AF_INET6) {
6304 					struct sockaddr_in6 *sin6;
6305 
6306 					sin6 = (struct sockaddr_in6 *)addr;
6307 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6308 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6309 						SCTP_TCB_UNLOCK(stcb);
6310 						error = EINVAL;
6311 						break;
6312 					}
6313 				} else
6314 #endif
6315 				{
6316 					error = EAFNOSUPPORT;
6317 					SCTP_TCB_UNLOCK(stcb);
6318 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6319 					break;
6320 				}
6321 			}
6322 			if (stcb != NULL) {
6323 				if (net != NULL) {
6324 					net->port = encaps->sue_port;
6325 				} else {
6326 					stcb->asoc.port = encaps->sue_port;
6327 				}
6328 				SCTP_TCB_UNLOCK(stcb);
6329 			} else {
6330 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6331 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6332 				    (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
6333 					SCTP_INP_WLOCK(inp);
6334 					inp->sctp_ep.port = encaps->sue_port;
6335 					SCTP_INP_WUNLOCK(inp);
6336 				} else {
6337 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6338 					error = EINVAL;
6339 				}
6340 			}
6341 			break;
6342 		}
6343 	case SCTP_ECN_SUPPORTED:
6344 		{
6345 			struct sctp_assoc_value *av;
6346 
6347 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6348 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6349 
6350 			if (stcb) {
6351 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6352 				error = EINVAL;
6353 				SCTP_TCB_UNLOCK(stcb);
6354 			} else {
6355 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6356 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6357 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6358 					SCTP_INP_WLOCK(inp);
6359 					if (av->assoc_value == 0) {
6360 						inp->ecn_supported = 0;
6361 					} else {
6362 						inp->ecn_supported = 1;
6363 					}
6364 					SCTP_INP_WUNLOCK(inp);
6365 				} else {
6366 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6367 					error = EINVAL;
6368 				}
6369 			}
6370 			break;
6371 		}
6372 	case SCTP_PR_SUPPORTED:
6373 		{
6374 			struct sctp_assoc_value *av;
6375 
6376 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6377 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6378 
6379 			if (stcb) {
6380 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6381 				error = EINVAL;
6382 				SCTP_TCB_UNLOCK(stcb);
6383 			} else {
6384 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6385 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6386 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6387 					SCTP_INP_WLOCK(inp);
6388 					if (av->assoc_value == 0) {
6389 						inp->prsctp_supported = 0;
6390 					} else {
6391 						inp->prsctp_supported = 1;
6392 					}
6393 					SCTP_INP_WUNLOCK(inp);
6394 				} else {
6395 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6396 					error = EINVAL;
6397 				}
6398 			}
6399 			break;
6400 		}
6401 	case SCTP_AUTH_SUPPORTED:
6402 		{
6403 			struct sctp_assoc_value *av;
6404 
6405 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6406 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6407 
6408 			if (stcb) {
6409 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6410 				error = EINVAL;
6411 				SCTP_TCB_UNLOCK(stcb);
6412 			} else {
6413 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6414 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6415 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6416 					if ((av->assoc_value == 0) &&
6417 					    (inp->asconf_supported == 1)) {
6418 						/*
6419 						 * AUTH is required for
6420 						 * ASCONF
6421 						 */
6422 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6423 						error = EINVAL;
6424 					} else {
6425 						SCTP_INP_WLOCK(inp);
6426 						if (av->assoc_value == 0) {
6427 							inp->auth_supported = 0;
6428 						} else {
6429 							inp->auth_supported = 1;
6430 						}
6431 						SCTP_INP_WUNLOCK(inp);
6432 					}
6433 				} else {
6434 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6435 					error = EINVAL;
6436 				}
6437 			}
6438 			break;
6439 		}
6440 	case SCTP_ASCONF_SUPPORTED:
6441 		{
6442 			struct sctp_assoc_value *av;
6443 
6444 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6445 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6446 
6447 			if (stcb) {
6448 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6449 				error = EINVAL;
6450 				SCTP_TCB_UNLOCK(stcb);
6451 			} else {
6452 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6453 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6454 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6455 					if ((av->assoc_value != 0) &&
6456 					    (inp->auth_supported == 0)) {
6457 						/*
6458 						 * AUTH is required for
6459 						 * ASCONF
6460 						 */
6461 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6462 						error = EINVAL;
6463 					} else {
6464 						SCTP_INP_WLOCK(inp);
6465 						if (av->assoc_value == 0) {
6466 							inp->asconf_supported = 0;
6467 							sctp_auth_delete_chunk(SCTP_ASCONF,
6468 							    inp->sctp_ep.local_auth_chunks);
6469 							sctp_auth_delete_chunk(SCTP_ASCONF_ACK,
6470 							    inp->sctp_ep.local_auth_chunks);
6471 						} else {
6472 							inp->asconf_supported = 1;
6473 							sctp_auth_add_chunk(SCTP_ASCONF,
6474 							    inp->sctp_ep.local_auth_chunks);
6475 							sctp_auth_add_chunk(SCTP_ASCONF_ACK,
6476 							    inp->sctp_ep.local_auth_chunks);
6477 						}
6478 						SCTP_INP_WUNLOCK(inp);
6479 					}
6480 				} else {
6481 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6482 					error = EINVAL;
6483 				}
6484 			}
6485 			break;
6486 		}
6487 	case SCTP_RECONFIG_SUPPORTED:
6488 		{
6489 			struct sctp_assoc_value *av;
6490 
6491 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6492 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6493 
6494 			if (stcb) {
6495 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6496 				error = EINVAL;
6497 				SCTP_TCB_UNLOCK(stcb);
6498 			} else {
6499 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6500 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6501 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6502 					SCTP_INP_WLOCK(inp);
6503 					if (av->assoc_value == 0) {
6504 						inp->reconfig_supported = 0;
6505 					} else {
6506 						inp->reconfig_supported = 1;
6507 					}
6508 					SCTP_INP_WUNLOCK(inp);
6509 				} else {
6510 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6511 					error = EINVAL;
6512 				}
6513 			}
6514 			break;
6515 		}
6516 	case SCTP_NRSACK_SUPPORTED:
6517 		{
6518 			struct sctp_assoc_value *av;
6519 
6520 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6521 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6522 
6523 			if (stcb) {
6524 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6525 				error = EINVAL;
6526 				SCTP_TCB_UNLOCK(stcb);
6527 			} else {
6528 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6529 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6530 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6531 					SCTP_INP_WLOCK(inp);
6532 					if (av->assoc_value == 0) {
6533 						inp->nrsack_supported = 0;
6534 					} else {
6535 						inp->nrsack_supported = 1;
6536 					}
6537 					SCTP_INP_WUNLOCK(inp);
6538 				} else {
6539 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6540 					error = EINVAL;
6541 				}
6542 			}
6543 			break;
6544 		}
6545 	case SCTP_PKTDROP_SUPPORTED:
6546 		{
6547 			struct sctp_assoc_value *av;
6548 
6549 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6550 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6551 
6552 			if (stcb) {
6553 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6554 				error = EINVAL;
6555 				SCTP_TCB_UNLOCK(stcb);
6556 			} else {
6557 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6558 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6559 				    (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6560 					SCTP_INP_WLOCK(inp);
6561 					if (av->assoc_value == 0) {
6562 						inp->pktdrop_supported = 0;
6563 					} else {
6564 						inp->pktdrop_supported = 1;
6565 					}
6566 					SCTP_INP_WUNLOCK(inp);
6567 				} else {
6568 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6569 					error = EINVAL;
6570 				}
6571 			}
6572 			break;
6573 		}
6574 	default:
6575 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
6576 		error = ENOPROTOOPT;
6577 		break;
6578 	}			/* end switch (opt) */
6579 	return (error);
6580 }
6581 
6582 int
6583 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
6584 {
6585 	void *optval = NULL;
6586 	size_t optsize = 0;
6587 	void *p;
6588 	int error = 0;
6589 
6590 	if (sopt->sopt_level != IPPROTO_SCTP) {
6591 		/* wrong proto level... send back up to IP */
6592 #ifdef INET6
6593 		if (INP_CHECK_SOCKAF(so, AF_INET6))
6594 			error = ip6_ctloutput(so, sopt);
6595 #endif				/* INET6 */
6596 #if defined(INET) && defined(INET6)
6597 		else
6598 #endif
6599 #ifdef INET
6600 			error = ip_ctloutput(so, sopt);
6601 #endif
6602 		return (error);
6603 	}
6604 	optsize = sopt->sopt_valsize;
6605 	if (optsize) {
6606 		SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
6607 		if (optval == NULL) {
6608 			SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6609 			return (ENOBUFS);
6610 		}
6611 		error = sooptcopyin(sopt, optval, optsize, optsize);
6612 		if (error) {
6613 			SCTP_FREE(optval, SCTP_M_SOCKOPT);
6614 			goto out;
6615 		}
6616 	}
6617 	p = (void *)sopt->sopt_td;
6618 	if (sopt->sopt_dir == SOPT_SET) {
6619 		error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
6620 	} else if (sopt->sopt_dir == SOPT_GET) {
6621 		error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
6622 	} else {
6623 		SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6624 		error = EINVAL;
6625 	}
6626 	if ((error == 0) && (optval != NULL)) {
6627 		error = sooptcopyout(sopt, optval, optsize);
6628 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
6629 	} else if (optval != NULL) {
6630 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
6631 	}
6632 out:
6633 	return (error);
6634 }
6635 
6636 #ifdef INET
6637 static int
6638 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
6639 {
6640 	int error = 0;
6641 	int create_lock_on = 0;
6642 	uint32_t vrf_id;
6643 	struct sctp_inpcb *inp;
6644 	struct sctp_tcb *stcb = NULL;
6645 
6646 	inp = (struct sctp_inpcb *)so->so_pcb;
6647 	if (inp == NULL) {
6648 		/* I made the same as TCP since we are not setup? */
6649 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6650 		return (ECONNRESET);
6651 	}
6652 	if (addr == NULL) {
6653 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6654 		return EINVAL;
6655 	}
6656 	switch (addr->sa_family) {
6657 #ifdef INET6
6658 	case AF_INET6:
6659 		{
6660 			struct sockaddr_in6 *sin6p;
6661 
6662 			if (addr->sa_len != sizeof(struct sockaddr_in6)) {
6663 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6664 				return (EINVAL);
6665 			}
6666 			sin6p = (struct sockaddr_in6 *)addr;
6667 			if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
6668 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6669 				return (error);
6670 			}
6671 			break;
6672 		}
6673 #endif
6674 #ifdef INET
6675 	case AF_INET:
6676 		{
6677 			struct sockaddr_in *sinp;
6678 
6679 			if (addr->sa_len != sizeof(struct sockaddr_in)) {
6680 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6681 				return (EINVAL);
6682 			}
6683 			sinp = (struct sockaddr_in *)addr;
6684 			if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
6685 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6686 				return (error);
6687 			}
6688 			break;
6689 		}
6690 #endif
6691 	default:
6692 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
6693 		return (EAFNOSUPPORT);
6694 	}
6695 	SCTP_INP_INCR_REF(inp);
6696 	SCTP_ASOC_CREATE_LOCK(inp);
6697 	create_lock_on = 1;
6698 
6699 
6700 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
6701 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
6702 		/* Should I really unlock ? */
6703 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
6704 		error = EFAULT;
6705 		goto out_now;
6706 	}
6707 #ifdef INET6
6708 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
6709 	    (addr->sa_family == AF_INET6)) {
6710 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6711 		error = EINVAL;
6712 		goto out_now;
6713 	}
6714 #endif
6715 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
6716 	    SCTP_PCB_FLAGS_UNBOUND) {
6717 		/* Bind a ephemeral port */
6718 		error = sctp_inpcb_bind(so, NULL, NULL, p);
6719 		if (error) {
6720 			goto out_now;
6721 		}
6722 	}
6723 	/* Now do we connect? */
6724 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
6725 	    (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
6726 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6727 		error = EINVAL;
6728 		goto out_now;
6729 	}
6730 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
6731 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
6732 		/* We are already connected AND the TCP model */
6733 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
6734 		error = EADDRINUSE;
6735 		goto out_now;
6736 	}
6737 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
6738 		SCTP_INP_RLOCK(inp);
6739 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
6740 		SCTP_INP_RUNLOCK(inp);
6741 	} else {
6742 		/*
6743 		 * We increment here since sctp_findassociation_ep_addr()
6744 		 * will do a decrement if it finds the stcb as long as the
6745 		 * locked tcb (last argument) is NOT a TCB.. aka NULL.
6746 		 */
6747 		SCTP_INP_INCR_REF(inp);
6748 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
6749 		if (stcb == NULL) {
6750 			SCTP_INP_DECR_REF(inp);
6751 		} else {
6752 			SCTP_TCB_UNLOCK(stcb);
6753 		}
6754 	}
6755 	if (stcb != NULL) {
6756 		/* Already have or am bring up an association */
6757 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
6758 		error = EALREADY;
6759 		goto out_now;
6760 	}
6761 	vrf_id = inp->def_vrf_id;
6762 	/* We are GOOD to go */
6763 	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
6764 	if (stcb == NULL) {
6765 		/* Gak! no memory */
6766 		goto out_now;
6767 	}
6768 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
6769 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
6770 		/* Set the connected flag so we can queue data */
6771 		soisconnecting(so);
6772 	}
6773 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
6774 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
6775 
6776 	/* initialize authentication parameters for the assoc */
6777 	sctp_initialize_auth_params(inp, stcb);
6778 
6779 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
6780 	SCTP_TCB_UNLOCK(stcb);
6781 out_now:
6782 	if (create_lock_on) {
6783 		SCTP_ASOC_CREATE_UNLOCK(inp);
6784 	}
6785 	SCTP_INP_DECR_REF(inp);
6786 	return (error);
6787 }
6788 
6789 #endif
6790 
6791 int
6792 sctp_listen(struct socket *so, int backlog, struct thread *p)
6793 {
6794 	/*
6795 	 * Note this module depends on the protocol processing being called
6796 	 * AFTER any socket level flags and backlog are applied to the
6797 	 * socket. The traditional way that the socket flags are applied is
6798 	 * AFTER protocol processing. We have made a change to the
6799 	 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
6800 	 * place if the socket API for SCTP is to work properly.
6801 	 */
6802 
6803 	int error = 0;
6804 	struct sctp_inpcb *inp;
6805 
6806 	inp = (struct sctp_inpcb *)so->so_pcb;
6807 	if (inp == NULL) {
6808 		/* I made the same as TCP since we are not setup? */
6809 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6810 		return (ECONNRESET);
6811 	}
6812 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
6813 		/* See if we have a listener */
6814 		struct sctp_inpcb *tinp;
6815 		union sctp_sockstore store;
6816 
6817 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
6818 			/* not bound all */
6819 			struct sctp_laddr *laddr;
6820 
6821 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6822 				memcpy(&store, &laddr->ifa->address, sizeof(store));
6823 				switch (store.sa.sa_family) {
6824 #ifdef INET
6825 				case AF_INET:
6826 					store.sin.sin_port = inp->sctp_lport;
6827 					break;
6828 #endif
6829 #ifdef INET6
6830 				case AF_INET6:
6831 					store.sin6.sin6_port = inp->sctp_lport;
6832 					break;
6833 #endif
6834 				default:
6835 					break;
6836 				}
6837 				tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
6838 				if (tinp && (tinp != inp) &&
6839 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
6840 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
6841 				    (tinp->sctp_socket->so_qlimit)) {
6842 					/*
6843 					 * we have a listener already and
6844 					 * its not this inp.
6845 					 */
6846 					SCTP_INP_DECR_REF(tinp);
6847 					return (EADDRINUSE);
6848 				} else if (tinp) {
6849 					SCTP_INP_DECR_REF(tinp);
6850 				}
6851 			}
6852 		} else {
6853 			/* Setup a local addr bound all */
6854 			memset(&store, 0, sizeof(store));
6855 #ifdef INET6
6856 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6857 				store.sa.sa_family = AF_INET6;
6858 				store.sa.sa_len = sizeof(struct sockaddr_in6);
6859 			}
6860 #endif
6861 #ifdef INET
6862 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
6863 				store.sa.sa_family = AF_INET;
6864 				store.sa.sa_len = sizeof(struct sockaddr_in);
6865 			}
6866 #endif
6867 			switch (store.sa.sa_family) {
6868 #ifdef INET
6869 			case AF_INET:
6870 				store.sin.sin_port = inp->sctp_lport;
6871 				break;
6872 #endif
6873 #ifdef INET6
6874 			case AF_INET6:
6875 				store.sin6.sin6_port = inp->sctp_lport;
6876 				break;
6877 #endif
6878 			default:
6879 				break;
6880 			}
6881 			tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
6882 			if (tinp && (tinp != inp) &&
6883 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
6884 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
6885 			    (tinp->sctp_socket->so_qlimit)) {
6886 				/*
6887 				 * we have a listener already and its not
6888 				 * this inp.
6889 				 */
6890 				SCTP_INP_DECR_REF(tinp);
6891 				return (EADDRINUSE);
6892 			} else if (tinp) {
6893 				SCTP_INP_DECR_REF(inp);
6894 			}
6895 		}
6896 	}
6897 	SCTP_INP_RLOCK(inp);
6898 #ifdef SCTP_LOCK_LOGGING
6899 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
6900 		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
6901 	}
6902 #endif
6903 	SOCK_LOCK(so);
6904 	error = solisten_proto_check(so);
6905 	if (error) {
6906 		SOCK_UNLOCK(so);
6907 		SCTP_INP_RUNLOCK(inp);
6908 		return (error);
6909 	}
6910 	if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
6911 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
6912 		/*
6913 		 * The unlucky case - We are in the tcp pool with this guy.
6914 		 * - Someone else is in the main inp slot. - We must move
6915 		 * this guy (the listener) to the main slot - We must then
6916 		 * move the guy that was listener to the TCP Pool.
6917 		 */
6918 		if (sctp_swap_inpcb_for_listen(inp)) {
6919 			goto in_use;
6920 		}
6921 	}
6922 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
6923 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
6924 		/* We are already connected AND the TCP model */
6925 in_use:
6926 		SCTP_INP_RUNLOCK(inp);
6927 		SOCK_UNLOCK(so);
6928 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
6929 		return (EADDRINUSE);
6930 	}
6931 	SCTP_INP_RUNLOCK(inp);
6932 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
6933 		/* We must do a bind. */
6934 		SOCK_UNLOCK(so);
6935 		if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
6936 			/* bind error, probably perm */
6937 			return (error);
6938 		}
6939 		SOCK_LOCK(so);
6940 	}
6941 	/* It appears for 7.0 and on, we must always call this. */
6942 	solisten_proto(so, backlog);
6943 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
6944 		/* remove the ACCEPTCONN flag for one-to-many sockets */
6945 		so->so_options &= ~SO_ACCEPTCONN;
6946 	}
6947 	if (backlog == 0) {
6948 		/* turning off listen */
6949 		so->so_options &= ~SO_ACCEPTCONN;
6950 	}
6951 	SOCK_UNLOCK(so);
6952 	return (error);
6953 }
6954 
6955 static int sctp_defered_wakeup_cnt = 0;
6956 
6957 int
6958 sctp_accept(struct socket *so, struct sockaddr **addr)
6959 {
6960 	struct sctp_tcb *stcb;
6961 	struct sctp_inpcb *inp;
6962 	union sctp_sockstore store;
6963 
6964 #ifdef INET6
6965 	int error;
6966 
6967 #endif
6968 	inp = (struct sctp_inpcb *)so->so_pcb;
6969 
6970 	if (inp == NULL) {
6971 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6972 		return (ECONNRESET);
6973 	}
6974 	SCTP_INP_RLOCK(inp);
6975 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
6976 		SCTP_INP_RUNLOCK(inp);
6977 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
6978 		return (EOPNOTSUPP);
6979 	}
6980 	if (so->so_state & SS_ISDISCONNECTED) {
6981 		SCTP_INP_RUNLOCK(inp);
6982 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
6983 		return (ECONNABORTED);
6984 	}
6985 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
6986 	if (stcb == NULL) {
6987 		SCTP_INP_RUNLOCK(inp);
6988 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6989 		return (ECONNRESET);
6990 	}
6991 	SCTP_TCB_LOCK(stcb);
6992 	SCTP_INP_RUNLOCK(inp);
6993 	store = stcb->asoc.primary_destination->ro._l_addr;
6994 	stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
6995 	SCTP_TCB_UNLOCK(stcb);
6996 	switch (store.sa.sa_family) {
6997 #ifdef INET
6998 	case AF_INET:
6999 		{
7000 			struct sockaddr_in *sin;
7001 
7002 			SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7003 			if (sin == NULL)
7004 				return (ENOMEM);
7005 			sin->sin_family = AF_INET;
7006 			sin->sin_len = sizeof(*sin);
7007 			sin->sin_port = store.sin.sin_port;
7008 			sin->sin_addr = store.sin.sin_addr;
7009 			*addr = (struct sockaddr *)sin;
7010 			break;
7011 		}
7012 #endif
7013 #ifdef INET6
7014 	case AF_INET6:
7015 		{
7016 			struct sockaddr_in6 *sin6;
7017 
7018 			SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
7019 			if (sin6 == NULL)
7020 				return (ENOMEM);
7021 			sin6->sin6_family = AF_INET6;
7022 			sin6->sin6_len = sizeof(*sin6);
7023 			sin6->sin6_port = store.sin6.sin6_port;
7024 			sin6->sin6_addr = store.sin6.sin6_addr;
7025 			if ((error = sa6_recoverscope(sin6)) != 0) {
7026 				SCTP_FREE_SONAME(sin6);
7027 				return (error);
7028 			}
7029 			*addr = (struct sockaddr *)sin6;
7030 			break;
7031 		}
7032 #endif
7033 	default:
7034 		/* TSNH */
7035 		break;
7036 	}
7037 	/* Wake any delayed sleep action */
7038 	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
7039 		SCTP_INP_WLOCK(inp);
7040 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
7041 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
7042 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
7043 			SCTP_INP_WUNLOCK(inp);
7044 			SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
7045 			if (sowriteable(inp->sctp_socket)) {
7046 				sowwakeup_locked(inp->sctp_socket);
7047 			} else {
7048 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
7049 			}
7050 			SCTP_INP_WLOCK(inp);
7051 		}
7052 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
7053 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
7054 			SCTP_INP_WUNLOCK(inp);
7055 			SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
7056 			if (soreadable(inp->sctp_socket)) {
7057 				sctp_defered_wakeup_cnt++;
7058 				sorwakeup_locked(inp->sctp_socket);
7059 			} else {
7060 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
7061 			}
7062 			SCTP_INP_WLOCK(inp);
7063 		}
7064 		SCTP_INP_WUNLOCK(inp);
7065 	}
7066 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
7067 		SCTP_TCB_LOCK(stcb);
7068 		sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
7069 	}
7070 	return (0);
7071 }
7072 
7073 #ifdef INET
7074 int
7075 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
7076 {
7077 	struct sockaddr_in *sin;
7078 	uint32_t vrf_id;
7079 	struct sctp_inpcb *inp;
7080 	struct sctp_ifa *sctp_ifa;
7081 
7082 	/*
7083 	 * Do the malloc first in case it blocks.
7084 	 */
7085 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7086 	if (sin == NULL)
7087 		return (ENOMEM);
7088 	sin->sin_family = AF_INET;
7089 	sin->sin_len = sizeof(*sin);
7090 	inp = (struct sctp_inpcb *)so->so_pcb;
7091 	if (!inp) {
7092 		SCTP_FREE_SONAME(sin);
7093 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7094 		return (ECONNRESET);
7095 	}
7096 	SCTP_INP_RLOCK(inp);
7097 	sin->sin_port = inp->sctp_lport;
7098 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7099 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7100 			struct sctp_tcb *stcb;
7101 			struct sockaddr_in *sin_a;
7102 			struct sctp_nets *net;
7103 			int fnd;
7104 
7105 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
7106 			if (stcb == NULL) {
7107 				goto notConn;
7108 			}
7109 			fnd = 0;
7110 			sin_a = NULL;
7111 			SCTP_TCB_LOCK(stcb);
7112 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7113 				sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7114 				if (sin_a == NULL)
7115 					/* this will make coverity happy */
7116 					continue;
7117 
7118 				if (sin_a->sin_family == AF_INET) {
7119 					fnd = 1;
7120 					break;
7121 				}
7122 			}
7123 			if ((!fnd) || (sin_a == NULL)) {
7124 				/* punt */
7125 				SCTP_TCB_UNLOCK(stcb);
7126 				goto notConn;
7127 			}
7128 			vrf_id = inp->def_vrf_id;
7129 			sctp_ifa = sctp_source_address_selection(inp,
7130 			    stcb,
7131 			    (sctp_route_t *) & net->ro,
7132 			    net, 0, vrf_id);
7133 			if (sctp_ifa) {
7134 				sin->sin_addr = sctp_ifa->address.sin.sin_addr;
7135 				sctp_free_ifa(sctp_ifa);
7136 			}
7137 			SCTP_TCB_UNLOCK(stcb);
7138 		} else {
7139 			/* For the bound all case you get back 0 */
7140 	notConn:
7141 			sin->sin_addr.s_addr = 0;
7142 		}
7143 
7144 	} else {
7145 		/* Take the first IPv4 address in the list */
7146 		struct sctp_laddr *laddr;
7147 		int fnd = 0;
7148 
7149 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
7150 			if (laddr->ifa->address.sa.sa_family == AF_INET) {
7151 				struct sockaddr_in *sin_a;
7152 
7153 				sin_a = &laddr->ifa->address.sin;
7154 				sin->sin_addr = sin_a->sin_addr;
7155 				fnd = 1;
7156 				break;
7157 			}
7158 		}
7159 		if (!fnd) {
7160 			SCTP_FREE_SONAME(sin);
7161 			SCTP_INP_RUNLOCK(inp);
7162 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7163 			return (ENOENT);
7164 		}
7165 	}
7166 	SCTP_INP_RUNLOCK(inp);
7167 	(*addr) = (struct sockaddr *)sin;
7168 	return (0);
7169 }
7170 
7171 int
7172 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
7173 {
7174 	struct sockaddr_in *sin;
7175 	int fnd;
7176 	struct sockaddr_in *sin_a;
7177 	struct sctp_inpcb *inp;
7178 	struct sctp_tcb *stcb;
7179 	struct sctp_nets *net;
7180 
7181 	/* Do the malloc first in case it blocks. */
7182 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7183 	if (sin == NULL)
7184 		return (ENOMEM);
7185 	sin->sin_family = AF_INET;
7186 	sin->sin_len = sizeof(*sin);
7187 
7188 	inp = (struct sctp_inpcb *)so->so_pcb;
7189 	if ((inp == NULL) ||
7190 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
7191 		/* UDP type and listeners will drop out here */
7192 		SCTP_FREE_SONAME(sin);
7193 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
7194 		return (ENOTCONN);
7195 	}
7196 	SCTP_INP_RLOCK(inp);
7197 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
7198 	if (stcb) {
7199 		SCTP_TCB_LOCK(stcb);
7200 	}
7201 	SCTP_INP_RUNLOCK(inp);
7202 	if (stcb == NULL) {
7203 		SCTP_FREE_SONAME(sin);
7204 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7205 		return (ECONNRESET);
7206 	}
7207 	fnd = 0;
7208 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7209 		sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7210 		if (sin_a->sin_family == AF_INET) {
7211 			fnd = 1;
7212 			sin->sin_port = stcb->rport;
7213 			sin->sin_addr = sin_a->sin_addr;
7214 			break;
7215 		}
7216 	}
7217 	SCTP_TCB_UNLOCK(stcb);
7218 	if (!fnd) {
7219 		/* No IPv4 address */
7220 		SCTP_FREE_SONAME(sin);
7221 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7222 		return (ENOENT);
7223 	}
7224 	(*addr) = (struct sockaddr *)sin;
7225 	return (0);
7226 }
7227 
7228 struct pr_usrreqs sctp_usrreqs = {
7229 	.pru_abort = sctp_abort,
7230 	.pru_accept = sctp_accept,
7231 	.pru_attach = sctp_attach,
7232 	.pru_bind = sctp_bind,
7233 	.pru_connect = sctp_connect,
7234 	.pru_control = in_control,
7235 	.pru_close = sctp_close,
7236 	.pru_detach = sctp_close,
7237 	.pru_sopoll = sopoll_generic,
7238 	.pru_flush = sctp_flush,
7239 	.pru_disconnect = sctp_disconnect,
7240 	.pru_listen = sctp_listen,
7241 	.pru_peeraddr = sctp_peeraddr,
7242 	.pru_send = sctp_sendm,
7243 	.pru_shutdown = sctp_shutdown,
7244 	.pru_sockaddr = sctp_ingetaddr,
7245 	.pru_sosend = sctp_sosend,
7246 	.pru_soreceive = sctp_soreceive
7247 };
7248 
7249 #endif
7250