xref: /freebsd/sys/netinet/sctp_usrreq.c (revision 830940567b49bb0c08dfaed40418999e76616909)
1 /*-
2  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $	 */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 #include <netinet/sctp_os.h>
36 #include <sys/proc.h>
37 #include <netinet/sctp_pcb.h>
38 #include <netinet/sctp_header.h>
39 #include <netinet/sctp_var.h>
40 #if defined(INET6)
41 #endif
42 #include <netinet/sctp_sysctl.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_uio.h>
45 #include <netinet/sctp_asconf.h>
46 #include <netinet/sctputil.h>
47 #include <netinet/sctp_indata.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_auth.h>
50 #include <netinet/sctp_bsd_addr.h>
51 #include <netinet/sctp_cc_functions.h>
52 #include <netinet/udp.h>
53 
54 
55 
56 
57 void
58 sctp_init(void)
59 {
60 	u_long sb_max_adj;
61 
62 	bzero(&SCTP_BASE_STATS, sizeof(struct sctpstat));
63 
64 	/* Initialize and modify the sysctled variables */
65 	sctp_init_sysctls();
66 	if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
67 		SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) = (nmbclusters / 8);
68 	/*
69 	 * Allow a user to take no more than 1/2 the number of clusters or
70 	 * the SB_MAX whichever is smaller for the send window.
71 	 */
72 	sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
73 	SCTP_BASE_SYSCTL(sctp_sendspace) = min(sb_max_adj,
74 	    (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
75 	/*
76 	 * Now for the recv window, should we take the same amount? or
77 	 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
78 	 * now I will just copy.
79 	 */
80 	SCTP_BASE_SYSCTL(sctp_recvspace) = SCTP_BASE_SYSCTL(sctp_sendspace);
81 
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 }
93 
94 void
95 sctp_finish(void)
96 {
97 	sctp_pcb_finish();
98 }
99 
100 
101 
102 void
103 sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
104     struct sctp_tcb *stcb,
105     struct sctp_nets *net,
106     uint16_t nxtsz)
107 {
108 	struct sctp_tmit_chunk *chk;
109 
110 	/* Adjust that too */
111 	stcb->asoc.smallest_mtu = nxtsz;
112 	/* now off to subtract IP_DF flag if needed */
113 #ifdef SCTP_PRINT_FOR_B_AND_M
114 	SCTP_PRINTF("sctp_pathmtu_adjust called inp:%p stcb:%p net:%p nxtsz:%d\n",
115 	    inp, stcb, net, nxtsz);
116 #endif
117 	TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
118 		if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
119 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
120 		}
121 	}
122 	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
123 		if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
124 			/*
125 			 * For this guy we also mark for immediate resend
126 			 * since we sent to big of chunk
127 			 */
128 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
129 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
130 				sctp_flight_size_decrease(chk);
131 				sctp_total_flight_decrease(stcb, chk);
132 			}
133 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
134 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
135 			}
136 			chk->sent = SCTP_DATAGRAM_RESEND;
137 			chk->rec.data.doing_fast_retransmit = 0;
138 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
139 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
140 				    chk->whoTo->flight_size,
141 				    chk->book_size,
142 				    (uintptr_t) chk->whoTo,
143 				    chk->rec.data.TSN_seq);
144 			}
145 			/* Clear any time so NO RTT is being done */
146 			chk->do_rtt = 0;
147 		}
148 	}
149 }
150 
151 static void
152 sctp_notify_mbuf(struct sctp_inpcb *inp,
153     struct sctp_tcb *stcb,
154     struct sctp_nets *net,
155     struct ip *ip,
156     struct sctphdr *sh)
157 {
158 	struct icmp *icmph;
159 	int totsz, tmr_stopped = 0;
160 	uint16_t nxtsz;
161 
162 	/* protection */
163 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
164 	    (ip == NULL) || (sh == NULL)) {
165 		if (stcb != NULL) {
166 			SCTP_TCB_UNLOCK(stcb);
167 		}
168 		return;
169 	}
170 	/* First job is to verify the vtag matches what I would send */
171 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
172 		SCTP_TCB_UNLOCK(stcb);
173 		return;
174 	}
175 	icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
176 	    sizeof(struct ip)));
177 	if (icmph->icmp_type != ICMP_UNREACH) {
178 		/* We only care about unreachable */
179 		SCTP_TCB_UNLOCK(stcb);
180 		return;
181 	}
182 	if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
183 		/* not a unreachable message due to frag. */
184 		SCTP_TCB_UNLOCK(stcb);
185 		return;
186 	}
187 	totsz = ip->ip_len;
188 
189 	nxtsz = ntohs(icmph->icmp_nextmtu);
190 	if (nxtsz == 0) {
191 		/*
192 		 * old type router that does not tell us what the next size
193 		 * mtu is. Rats we will have to guess (in a educated fashion
194 		 * of course)
195 		 */
196 		nxtsz = find_next_best_mtu(totsz);
197 	}
198 	/* Stop any PMTU timer */
199 	if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
200 		tmr_stopped = 1;
201 		sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
202 		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
203 	}
204 	/* Adjust destination size limit */
205 	if (net->mtu > nxtsz) {
206 		net->mtu = nxtsz;
207 		if (net->port) {
208 			net->mtu -= sizeof(struct udphdr);
209 		}
210 	}
211 	/* now what about the ep? */
212 	if (stcb->asoc.smallest_mtu > nxtsz) {
213 #ifdef SCTP_PRINT_FOR_B_AND_M
214 		SCTP_PRINTF("notify_mbuf (ICMP) calls sctp_pathmtu_adjust mtu:%d\n",
215 		    nxtsz);
216 #endif
217 		sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
218 	}
219 	if (tmr_stopped)
220 		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
221 
222 	SCTP_TCB_UNLOCK(stcb);
223 }
224 
225 
226 void
227 sctp_notify(struct sctp_inpcb *inp,
228     struct ip *ip,
229     struct sctphdr *sh,
230     struct sockaddr *to,
231     struct sctp_tcb *stcb,
232     struct sctp_nets *net)
233 {
234 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
235 	struct socket *so;
236 
237 #endif
238 	/* protection */
239 	int reason;
240 	struct icmp *icmph;
241 
242 
243 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
244 	    (sh == NULL) || (to == NULL)) {
245 		if (stcb)
246 			SCTP_TCB_UNLOCK(stcb);
247 		return;
248 	}
249 	/* First job is to verify the vtag matches what I would send */
250 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
251 		SCTP_TCB_UNLOCK(stcb);
252 		return;
253 	}
254 	icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
255 	    sizeof(struct ip)));
256 	if (icmph->icmp_type != ICMP_UNREACH) {
257 		/* We only care about unreachable */
258 		SCTP_TCB_UNLOCK(stcb);
259 		return;
260 	}
261 	if ((icmph->icmp_code == ICMP_UNREACH_NET) ||
262 	    (icmph->icmp_code == ICMP_UNREACH_HOST) ||
263 	    (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
264 	    (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
265 	    (icmph->icmp_code == ICMP_UNREACH_ISOLATED) ||
266 	    (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) ||
267 	    (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
268 	    (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
269 
270 		/*
271 		 * Hmm reachablity problems we must examine closely. If its
272 		 * not reachable, we may have lost a network. Or if there is
273 		 * NO protocol at the other end named SCTP. well we consider
274 		 * it a OOTB abort.
275 		 */
276 		if (net->dest_state & SCTP_ADDR_REACHABLE) {
277 			/* Ok that destination is NOT reachable */
278 			SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
279 			    net->error_count,
280 			    net->failure_threshold,
281 			    net);
282 
283 			net->dest_state &= ~SCTP_ADDR_REACHABLE;
284 			net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
285 			/*
286 			 * JRS 5/14/07 - If a destination is unreachable,
287 			 * the PF bit is turned off.  This allows an
288 			 * unambiguous use of the PF bit for destinations
289 			 * that are reachable but potentially failed. If the
290 			 * destination is set to the unreachable state, also
291 			 * set the destination to the PF state.
292 			 */
293 			/*
294 			 * Add debug message here if destination is not in
295 			 * PF state.
296 			 */
297 			/* Stop any running T3 timers here? */
298 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
299 				net->dest_state &= ~SCTP_ADDR_PF;
300 				SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
301 				    net);
302 			}
303 			net->error_count = net->failure_threshold + 1;
304 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
305 			    stcb, SCTP_FAILED_THRESHOLD,
306 			    (void *)net, SCTP_SO_NOT_LOCKED);
307 		}
308 		SCTP_TCB_UNLOCK(stcb);
309 	} else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
310 	    (icmph->icmp_code == ICMP_UNREACH_PORT)) {
311 		/*
312 		 * Here the peer is either playing tricks on us, including
313 		 * an address that belongs to someone who does not support
314 		 * SCTP OR was a userland implementation that shutdown and
315 		 * now is dead. In either case treat it like a OOTB abort
316 		 * with no TCB
317 		 */
318 		reason = SCTP_PEER_FAULTY;
319 		sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
320 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
321 		so = SCTP_INP_SO(inp);
322 		atomic_add_int(&stcb->asoc.refcnt, 1);
323 		SCTP_TCB_UNLOCK(stcb);
324 		SCTP_SOCKET_LOCK(so, 1);
325 		SCTP_TCB_LOCK(stcb);
326 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
327 #endif
328 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
329 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
330 		SCTP_SOCKET_UNLOCK(so, 1);
331 		/* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
332 #endif
333 		/* no need to unlock here, since the TCB is gone */
334 	} else {
335 		SCTP_TCB_UNLOCK(stcb);
336 	}
337 }
338 
339 void
340 sctp_ctlinput(cmd, sa, vip)
341 	int cmd;
342 	struct sockaddr *sa;
343 	void *vip;
344 {
345 	struct ip *ip = vip;
346 	struct sctphdr *sh;
347 	uint32_t vrf_id;
348 
349 	/* FIX, for non-bsd is this right? */
350 	vrf_id = SCTP_DEFAULT_VRFID;
351 	if (sa->sa_family != AF_INET ||
352 	    ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
353 		return;
354 	}
355 	if (PRC_IS_REDIRECT(cmd)) {
356 		ip = 0;
357 	} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
358 		return;
359 	}
360 	if (ip) {
361 		struct sctp_inpcb *inp = NULL;
362 		struct sctp_tcb *stcb = NULL;
363 		struct sctp_nets *net = NULL;
364 		struct sockaddr_in to, from;
365 
366 		sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
367 		bzero(&to, sizeof(to));
368 		bzero(&from, sizeof(from));
369 		from.sin_family = to.sin_family = AF_INET;
370 		from.sin_len = to.sin_len = sizeof(to);
371 		from.sin_port = sh->src_port;
372 		from.sin_addr = ip->ip_src;
373 		to.sin_port = sh->dest_port;
374 		to.sin_addr = ip->ip_dst;
375 
376 		/*
377 		 * 'to' holds the dest of the packet that failed to be sent.
378 		 * 'from' holds our local endpoint address. Thus we reverse
379 		 * the to and the from in the lookup.
380 		 */
381 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
382 		    (struct sockaddr *)&to,
383 		    &inp, &net, 1, vrf_id);
384 		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
385 			if (cmd != PRC_MSGSIZE) {
386 				sctp_notify(inp, ip, sh,
387 				    (struct sockaddr *)&to, stcb,
388 				    net);
389 			} else {
390 				/* handle possible ICMP size messages */
391 				sctp_notify_mbuf(inp, stcb, net, ip, sh);
392 			}
393 		} else {
394 			if ((stcb == NULL) && (inp != NULL)) {
395 				/* reduce ref-count */
396 				SCTP_INP_WLOCK(inp);
397 				SCTP_INP_DECR_REF(inp);
398 				SCTP_INP_WUNLOCK(inp);
399 			}
400 		}
401 	}
402 	return;
403 }
404 
405 static int
406 sctp_getcred(SYSCTL_HANDLER_ARGS)
407 {
408 	struct xucred xuc;
409 	struct sockaddr_in addrs[2];
410 	struct sctp_inpcb *inp;
411 	struct sctp_nets *net;
412 	struct sctp_tcb *stcb;
413 	int error;
414 	uint32_t vrf_id;
415 
416 	/* FIX, for non-bsd is this right? */
417 	vrf_id = SCTP_DEFAULT_VRFID;
418 
419 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
420 
421 	if (error)
422 		return (error);
423 
424 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
425 	if (error)
426 		return (error);
427 
428 	stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
429 	    sintosa(&addrs[1]),
430 	    &inp, &net, 1, vrf_id);
431 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
432 		if ((inp != NULL) && (stcb == NULL)) {
433 			/* reduce ref-count */
434 			SCTP_INP_WLOCK(inp);
435 			SCTP_INP_DECR_REF(inp);
436 			goto cred_can_cont;
437 		}
438 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
439 		error = ENOENT;
440 		goto out;
441 	}
442 	SCTP_TCB_UNLOCK(stcb);
443 	/*
444 	 * We use the write lock here, only since in the error leg we need
445 	 * it. If we used RLOCK, then we would have to
446 	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
447 	 * Better to use higher wlock.
448 	 */
449 	SCTP_INP_WLOCK(inp);
450 cred_can_cont:
451 	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
452 	if (error) {
453 		SCTP_INP_WUNLOCK(inp);
454 		goto out;
455 	}
456 	cru2x(inp->sctp_socket->so_cred, &xuc);
457 	SCTP_INP_WUNLOCK(inp);
458 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
459 out:
460 	return (error);
461 }
462 
463 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
464     0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
465 
466 
467 static void
468 sctp_abort(struct socket *so)
469 {
470 	struct sctp_inpcb *inp;
471 	uint32_t flags;
472 
473 	inp = (struct sctp_inpcb *)so->so_pcb;
474 	if (inp == 0) {
475 		return;
476 	}
477 sctp_must_try_again:
478 	flags = inp->sctp_flags;
479 #ifdef SCTP_LOG_CLOSING
480 	sctp_log_closing(inp, NULL, 17);
481 #endif
482 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
483 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
484 #ifdef SCTP_LOG_CLOSING
485 		sctp_log_closing(inp, NULL, 16);
486 #endif
487 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
488 		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
489 		SOCK_LOCK(so);
490 		SCTP_SB_CLEAR(so->so_snd);
491 		/*
492 		 * same for the rcv ones, they are only here for the
493 		 * accounting/select.
494 		 */
495 		SCTP_SB_CLEAR(so->so_rcv);
496 
497 		/* Now null out the reference, we are completely detached. */
498 		so->so_pcb = NULL;
499 		SOCK_UNLOCK(so);
500 	} else {
501 		flags = inp->sctp_flags;
502 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
503 			goto sctp_must_try_again;
504 		}
505 	}
506 	return;
507 }
508 
509 static int
510 sctp_attach(struct socket *so, int proto, struct thread *p)
511 {
512 	struct sctp_inpcb *inp;
513 	struct inpcb *ip_inp;
514 	int error;
515 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
516 
517 #ifdef IPSEC
518 	uint32_t flags;
519 
520 #endif
521 
522 	inp = (struct sctp_inpcb *)so->so_pcb;
523 	if (inp != 0) {
524 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
525 		return EINVAL;
526 	}
527 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
528 		error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
529 		if (error) {
530 			return error;
531 		}
532 	}
533 	error = sctp_inpcb_alloc(so, vrf_id);
534 	if (error) {
535 		return error;
536 	}
537 	inp = (struct sctp_inpcb *)so->so_pcb;
538 	SCTP_INP_WLOCK(inp);
539 	inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;	/* I'm not v6! */
540 	ip_inp = &inp->ip_inp.inp;
541 	ip_inp->inp_vflag |= INP_IPV4;
542 	ip_inp->inp_ip_ttl = MODULE_GLOBAL(MOD_INET, ip_defttl);
543 #ifdef IPSEC
544 	error = ipsec_init_policy(so, &ip_inp->inp_sp);
545 #ifdef SCTP_LOG_CLOSING
546 	sctp_log_closing(inp, NULL, 17);
547 #endif
548 	if (error != 0) {
549 		flags = inp->sctp_flags;
550 		if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
551 		    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
552 #ifdef SCTP_LOG_CLOSING
553 			sctp_log_closing(inp, NULL, 15);
554 #endif
555 			SCTP_INP_WUNLOCK(inp);
556 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
557 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
558 		} else {
559 			SCTP_INP_WUNLOCK(inp);
560 		}
561 		return error;
562 	}
563 #endif				/* IPSEC */
564 	SCTP_INP_WUNLOCK(inp);
565 	return 0;
566 }
567 
568 static int
569 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
570 {
571 	struct sctp_inpcb *inp = NULL;
572 	int error;
573 
574 #ifdef INET6
575 	if (addr && addr->sa_family != AF_INET) {
576 		/* must be a v4 address! */
577 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
578 		return EINVAL;
579 	}
580 #endif				/* INET6 */
581 	if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
582 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
583 		return EINVAL;
584 	}
585 	inp = (struct sctp_inpcb *)so->so_pcb;
586 	if (inp == 0) {
587 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
588 		return EINVAL;
589 	}
590 	error = sctp_inpcb_bind(so, addr, NULL, p);
591 	return error;
592 }
593 
594 void
595 sctp_close(struct socket *so)
596 {
597 	struct sctp_inpcb *inp;
598 	uint32_t flags;
599 
600 	inp = (struct sctp_inpcb *)so->so_pcb;
601 	if (inp == 0)
602 		return;
603 
604 	/*
605 	 * Inform all the lower layer assoc that we are done.
606 	 */
607 sctp_must_try_again:
608 	flags = inp->sctp_flags;
609 #ifdef SCTP_LOG_CLOSING
610 	sctp_log_closing(inp, NULL, 17);
611 #endif
612 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
613 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
614 		if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
615 		    (so->so_rcv.sb_cc > 0)) {
616 #ifdef SCTP_LOG_CLOSING
617 			sctp_log_closing(inp, NULL, 13);
618 #endif
619 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
620 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
621 		} else {
622 #ifdef SCTP_LOG_CLOSING
623 			sctp_log_closing(inp, NULL, 14);
624 #endif
625 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
626 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
627 		}
628 		/*
629 		 * The socket is now detached, no matter what the state of
630 		 * the SCTP association.
631 		 */
632 		SOCK_LOCK(so);
633 		SCTP_SB_CLEAR(so->so_snd);
634 		/*
635 		 * same for the rcv ones, they are only here for the
636 		 * accounting/select.
637 		 */
638 		SCTP_SB_CLEAR(so->so_rcv);
639 
640 		/* Now null out the reference, we are completely detached. */
641 		so->so_pcb = NULL;
642 		SOCK_UNLOCK(so);
643 	} else {
644 		flags = inp->sctp_flags;
645 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
646 			goto sctp_must_try_again;
647 		}
648 	}
649 	return;
650 }
651 
652 
653 int
654 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
655     struct mbuf *control, struct thread *p);
656 
657 
658 int
659 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
660     struct mbuf *control, struct thread *p)
661 {
662 	struct sctp_inpcb *inp;
663 	int error;
664 
665 	inp = (struct sctp_inpcb *)so->so_pcb;
666 	if (inp == 0) {
667 		if (control) {
668 			sctp_m_freem(control);
669 			control = NULL;
670 		}
671 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
672 		sctp_m_freem(m);
673 		return EINVAL;
674 	}
675 	/* Got to have an to address if we are NOT a connected socket */
676 	if ((addr == NULL) &&
677 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
678 	    (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
679 	    ) {
680 		goto connected_type;
681 	} else if (addr == NULL) {
682 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
683 		error = EDESTADDRREQ;
684 		sctp_m_freem(m);
685 		if (control) {
686 			sctp_m_freem(control);
687 			control = NULL;
688 		}
689 		return (error);
690 	}
691 #ifdef INET6
692 	if (addr->sa_family != AF_INET) {
693 		/* must be a v4 address! */
694 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
695 		sctp_m_freem(m);
696 		if (control) {
697 			sctp_m_freem(control);
698 			control = NULL;
699 		}
700 		error = EDESTADDRREQ;
701 		return EDESTADDRREQ;
702 	}
703 #endif				/* INET6 */
704 connected_type:
705 	/* now what about control */
706 	if (control) {
707 		if (inp->control) {
708 			SCTP_PRINTF("huh? control set?\n");
709 			sctp_m_freem(inp->control);
710 			inp->control = NULL;
711 		}
712 		inp->control = control;
713 	}
714 	/* Place the data */
715 	if (inp->pkt) {
716 		SCTP_BUF_NEXT(inp->pkt_last) = m;
717 		inp->pkt_last = m;
718 	} else {
719 		inp->pkt_last = inp->pkt = m;
720 	}
721 	if (
722 	/* FreeBSD uses a flag passed */
723 	    ((flags & PRUS_MORETOCOME) == 0)
724 	    ) {
725 		/*
726 		 * note with the current version this code will only be used
727 		 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
728 		 * re-defining sosend to use the sctp_sosend. One can
729 		 * optionally switch back to this code (by changing back the
730 		 * definitions) but this is not advisable. This code is used
731 		 * by FreeBSD when sending a file with sendfile() though.
732 		 */
733 		int ret;
734 
735 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
736 		inp->pkt = NULL;
737 		inp->control = NULL;
738 		return (ret);
739 	} else {
740 		return (0);
741 	}
742 }
743 
744 int
745 sctp_disconnect(struct socket *so)
746 {
747 	struct sctp_inpcb *inp;
748 
749 	inp = (struct sctp_inpcb *)so->so_pcb;
750 	if (inp == NULL) {
751 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
752 		return (ENOTCONN);
753 	}
754 	SCTP_INP_RLOCK(inp);
755 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
756 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
757 		if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
758 			/* No connection */
759 			SCTP_INP_RUNLOCK(inp);
760 			return (0);
761 		} else {
762 			struct sctp_association *asoc;
763 			struct sctp_tcb *stcb;
764 
765 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
766 			if (stcb == NULL) {
767 				SCTP_INP_RUNLOCK(inp);
768 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
769 				return (EINVAL);
770 			}
771 			SCTP_TCB_LOCK(stcb);
772 			asoc = &stcb->asoc;
773 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
774 				/* We are about to be freed, out of here */
775 				SCTP_TCB_UNLOCK(stcb);
776 				SCTP_INP_RUNLOCK(inp);
777 				return (0);
778 			}
779 			if (((so->so_options & SO_LINGER) &&
780 			    (so->so_linger == 0)) ||
781 			    (so->so_rcv.sb_cc > 0)) {
782 				if (SCTP_GET_STATE(asoc) !=
783 				    SCTP_STATE_COOKIE_WAIT) {
784 					/* Left with Data unread */
785 					struct mbuf *err;
786 
787 					err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
788 					if (err) {
789 						/*
790 						 * Fill in the user
791 						 * initiated abort
792 						 */
793 						struct sctp_paramhdr *ph;
794 
795 						ph = mtod(err, struct sctp_paramhdr *);
796 						SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
797 						ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
798 						ph->param_length = htons(SCTP_BUF_LEN(err));
799 					}
800 #if defined(SCTP_PANIC_ON_ABORT)
801 					panic("disconnect does an abort");
802 #endif
803 					sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
804 					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
805 				}
806 				SCTP_INP_RUNLOCK(inp);
807 				if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
808 				    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
809 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
810 				}
811 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
812 				/* No unlock tcb assoc is gone */
813 				return (0);
814 			}
815 			if (TAILQ_EMPTY(&asoc->send_queue) &&
816 			    TAILQ_EMPTY(&asoc->sent_queue) &&
817 			    (asoc->stream_queue_cnt == 0)) {
818 				/* there is nothing queued to send, so done */
819 				if (asoc->locked_on_sending) {
820 					goto abort_anyway;
821 				}
822 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
823 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
824 					/* only send SHUTDOWN 1st time thru */
825 					sctp_stop_timers_for_shutdown(stcb);
826 					sctp_send_shutdown(stcb,
827 					    stcb->asoc.primary_destination);
828 					sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
829 					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
830 					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
831 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
832 					}
833 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
834 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
835 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
836 					    stcb->sctp_ep, stcb,
837 					    asoc->primary_destination);
838 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
839 					    stcb->sctp_ep, stcb,
840 					    asoc->primary_destination);
841 				}
842 			} else {
843 				/*
844 				 * we still got (or just got) data to send,
845 				 * so set SHUTDOWN_PENDING
846 				 */
847 				/*
848 				 * XXX sockets draft says that SCTP_EOF
849 				 * should be sent with no data. currently,
850 				 * we will allow user data to be sent first
851 				 * and move to SHUTDOWN-PENDING
852 				 */
853 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
854 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
855 				    asoc->primary_destination);
856 				if (asoc->locked_on_sending) {
857 					/* Locked to send out the data */
858 					struct sctp_stream_queue_pending *sp;
859 
860 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
861 					if (sp == NULL) {
862 						SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
863 						    asoc->locked_on_sending->stream_no);
864 					} else {
865 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
866 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
867 					}
868 				}
869 				if (TAILQ_EMPTY(&asoc->send_queue) &&
870 				    TAILQ_EMPTY(&asoc->sent_queue) &&
871 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
872 					struct mbuf *op_err;
873 
874 			abort_anyway:
875 					op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
876 					    0, M_DONTWAIT, 1, MT_DATA);
877 					if (op_err) {
878 						/*
879 						 * Fill in the user
880 						 * initiated abort
881 						 */
882 						struct sctp_paramhdr *ph;
883 						uint32_t *ippp;
884 
885 						SCTP_BUF_LEN(op_err) =
886 						    (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
887 						ph = mtod(op_err,
888 						    struct sctp_paramhdr *);
889 						ph->param_type = htons(
890 						    SCTP_CAUSE_USER_INITIATED_ABT);
891 						ph->param_length = htons(SCTP_BUF_LEN(op_err));
892 						ippp = (uint32_t *) (ph + 1);
893 						*ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
894 					}
895 #if defined(SCTP_PANIC_ON_ABORT)
896 					panic("disconnect does an abort");
897 #endif
898 
899 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
900 					sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
901 					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
902 					if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
903 					    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
904 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
905 					}
906 					SCTP_INP_RUNLOCK(inp);
907 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
908 					return (0);
909 				} else {
910 					sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
911 				}
912 			}
913 			soisdisconnecting(so);
914 			SCTP_TCB_UNLOCK(stcb);
915 			SCTP_INP_RUNLOCK(inp);
916 			return (0);
917 		}
918 		/* not reached */
919 	} else {
920 		/* UDP model does not support this */
921 		SCTP_INP_RUNLOCK(inp);
922 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
923 		return EOPNOTSUPP;
924 	}
925 }
926 
927 int
928 sctp_flush(struct socket *so, int how)
929 {
930 	/*
931 	 * We will just clear out the values and let subsequent close clear
932 	 * out the data, if any. Note if the user did a shutdown(SHUT_RD)
933 	 * they will not be able to read the data, the socket will block
934 	 * that from happening.
935 	 */
936 	if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
937 		/*
938 		 * First make sure the sb will be happy, we don't use these
939 		 * except maybe the count
940 		 */
941 		so->so_rcv.sb_cc = 0;
942 		so->so_rcv.sb_mbcnt = 0;
943 		so->so_rcv.sb_mb = NULL;
944 	}
945 	if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
946 		/*
947 		 * First make sure the sb will be happy, we don't use these
948 		 * except maybe the count
949 		 */
950 		so->so_snd.sb_cc = 0;
951 		so->so_snd.sb_mbcnt = 0;
952 		so->so_snd.sb_mb = NULL;
953 
954 	}
955 	return (0);
956 }
957 
958 int
959 sctp_shutdown(struct socket *so)
960 {
961 	struct sctp_inpcb *inp;
962 
963 	inp = (struct sctp_inpcb *)so->so_pcb;
964 	if (inp == 0) {
965 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
966 		return EINVAL;
967 	}
968 	SCTP_INP_RLOCK(inp);
969 	/* For UDP model this is a invalid call */
970 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
971 		/* Restore the flags that the soshutdown took away. */
972 		so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
973 		/* This proc will wakeup for read and do nothing (I hope) */
974 		SCTP_INP_RUNLOCK(inp);
975 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
976 		return (EOPNOTSUPP);
977 	}
978 	/*
979 	 * Ok if we reach here its the TCP model and it is either a SHUT_WR
980 	 * or SHUT_RDWR. This means we put the shutdown flag against it.
981 	 */
982 	{
983 		struct sctp_tcb *stcb;
984 		struct sctp_association *asoc;
985 
986 		if ((so->so_state &
987 		    (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
988 			SCTP_INP_RUNLOCK(inp);
989 			return (ENOTCONN);
990 		}
991 		socantsendmore(so);
992 
993 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
994 		if (stcb == NULL) {
995 			/*
996 			 * Ok we hit the case that the shutdown call was
997 			 * made after an abort or something. Nothing to do
998 			 * now.
999 			 */
1000 			SCTP_INP_RUNLOCK(inp);
1001 			return (0);
1002 		}
1003 		SCTP_TCB_LOCK(stcb);
1004 		asoc = &stcb->asoc;
1005 		if (TAILQ_EMPTY(&asoc->send_queue) &&
1006 		    TAILQ_EMPTY(&asoc->sent_queue) &&
1007 		    (asoc->stream_queue_cnt == 0)) {
1008 			if (asoc->locked_on_sending) {
1009 				goto abort_anyway;
1010 			}
1011 			/* there is nothing queued to send, so I'm done... */
1012 			if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1013 				/* only send SHUTDOWN the first time through */
1014 				sctp_stop_timers_for_shutdown(stcb);
1015 				sctp_send_shutdown(stcb,
1016 				    stcb->asoc.primary_destination);
1017 				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
1018 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1019 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1020 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1021 				}
1022 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1023 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1024 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1025 				    stcb->sctp_ep, stcb,
1026 				    asoc->primary_destination);
1027 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1028 				    stcb->sctp_ep, stcb,
1029 				    asoc->primary_destination);
1030 			}
1031 		} else {
1032 			/*
1033 			 * we still got (or just got) data to send, so set
1034 			 * SHUTDOWN_PENDING
1035 			 */
1036 			asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1037 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
1038 			    asoc->primary_destination);
1039 
1040 			if (asoc->locked_on_sending) {
1041 				/* Locked to send out the data */
1042 				struct sctp_stream_queue_pending *sp;
1043 
1044 				sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1045 				if (sp == NULL) {
1046 					SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1047 					    asoc->locked_on_sending->stream_no);
1048 				} else {
1049 					if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1050 						asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1051 					}
1052 				}
1053 			}
1054 			if (TAILQ_EMPTY(&asoc->send_queue) &&
1055 			    TAILQ_EMPTY(&asoc->sent_queue) &&
1056 			    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1057 				struct mbuf *op_err;
1058 
1059 		abort_anyway:
1060 				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1061 				    0, M_DONTWAIT, 1, MT_DATA);
1062 				if (op_err) {
1063 					/* Fill in the user initiated abort */
1064 					struct sctp_paramhdr *ph;
1065 					uint32_t *ippp;
1066 
1067 					SCTP_BUF_LEN(op_err) =
1068 					    sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
1069 					ph = mtod(op_err,
1070 					    struct sctp_paramhdr *);
1071 					ph->param_type = htons(
1072 					    SCTP_CAUSE_USER_INITIATED_ABT);
1073 					ph->param_length = htons(SCTP_BUF_LEN(op_err));
1074 					ippp = (uint32_t *) (ph + 1);
1075 					*ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1076 				}
1077 #if defined(SCTP_PANIC_ON_ABORT)
1078 				panic("shutdown does an abort");
1079 #endif
1080 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1081 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1082 				    SCTP_RESPONSE_TO_USER_REQ,
1083 				    op_err, SCTP_SO_LOCKED);
1084 				goto skip_unlock;
1085 			} else {
1086 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1087 			}
1088 		}
1089 		SCTP_TCB_UNLOCK(stcb);
1090 	}
1091 skip_unlock:
1092 	SCTP_INP_RUNLOCK(inp);
1093 	return 0;
1094 }
1095 
1096 /*
1097  * copies a "user" presentable address and removes embedded scope, etc.
1098  * returns 0 on success, 1 on error
1099  */
1100 static uint32_t
1101 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1102 {
1103 #ifdef INET6
1104 	struct sockaddr_in6 lsa6;
1105 
1106 	sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1107 	    &lsa6);
1108 #endif
1109 	memcpy(ss, sa, sa->sa_len);
1110 	return (0);
1111 }
1112 
1113 
1114 
1115 /*
1116  * NOTE: assumes addr lock is held
1117  */
1118 static size_t
1119 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1120     struct sctp_tcb *stcb,
1121     size_t limit,
1122     struct sockaddr_storage *sas,
1123     uint32_t vrf_id)
1124 {
1125 	struct sctp_ifn *sctp_ifn;
1126 	struct sctp_ifa *sctp_ifa;
1127 	int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1128 	size_t actual;
1129 	int ipv4_addr_legal, ipv6_addr_legal;
1130 	struct sctp_vrf *vrf;
1131 
1132 	actual = 0;
1133 	if (limit <= 0)
1134 		return (actual);
1135 
1136 	if (stcb) {
1137 		/* Turn on all the appropriate scope */
1138 		loopback_scope = stcb->asoc.loopback_scope;
1139 		ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1140 		local_scope = stcb->asoc.local_scope;
1141 		site_scope = stcb->asoc.site_scope;
1142 	} else {
1143 		/* Turn on ALL scope, since we look at the EP */
1144 		loopback_scope = ipv4_local_scope = local_scope =
1145 		    site_scope = 1;
1146 	}
1147 	ipv4_addr_legal = ipv6_addr_legal = 0;
1148 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1149 		ipv6_addr_legal = 1;
1150 		if (SCTP_IPV6_V6ONLY(inp) == 0) {
1151 			ipv4_addr_legal = 1;
1152 		}
1153 	} else {
1154 		ipv4_addr_legal = 1;
1155 	}
1156 	vrf = sctp_find_vrf(vrf_id);
1157 	if (vrf == NULL) {
1158 		return (0);
1159 	}
1160 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1161 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1162 			if ((loopback_scope == 0) &&
1163 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1164 				/* Skip loopback if loopback_scope not set */
1165 				continue;
1166 			}
1167 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1168 				if (stcb) {
1169 					/*
1170 					 * For the BOUND-ALL case, the list
1171 					 * associated with a TCB is Always
1172 					 * considered a reverse list.. i.e.
1173 					 * it lists addresses that are NOT
1174 					 * part of the association. If this
1175 					 * is one of those we must skip it.
1176 					 */
1177 					if (sctp_is_addr_restricted(stcb,
1178 					    sctp_ifa)) {
1179 						continue;
1180 					}
1181 				}
1182 				switch (sctp_ifa->address.sa.sa_family) {
1183 				case AF_INET:
1184 					if (ipv4_addr_legal) {
1185 						struct sockaddr_in *sin;
1186 
1187 						sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1188 						if (sin->sin_addr.s_addr == 0) {
1189 							/*
1190 							 * we skip
1191 							 * unspecifed
1192 							 * addresses
1193 							 */
1194 							continue;
1195 						}
1196 						if ((ipv4_local_scope == 0) &&
1197 						    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1198 							continue;
1199 						}
1200 #ifdef INET6
1201 						if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1202 							in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1203 							((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1204 							sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1205 							actual += sizeof(struct sockaddr_in6);
1206 						} else {
1207 #endif
1208 							memcpy(sas, sin, sizeof(*sin));
1209 							((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1210 							sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1211 							actual += sizeof(*sin);
1212 #ifdef INET6
1213 						}
1214 #endif
1215 						if (actual >= limit) {
1216 							return (actual);
1217 						}
1218 					} else {
1219 						continue;
1220 					}
1221 					break;
1222 #ifdef INET6
1223 				case AF_INET6:
1224 					if (ipv6_addr_legal) {
1225 						struct sockaddr_in6 *sin6;
1226 
1227 						sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1228 						if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1229 							/*
1230 							 * we skip
1231 							 * unspecifed
1232 							 * addresses
1233 							 */
1234 							continue;
1235 						}
1236 						if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1237 							if (local_scope == 0)
1238 								continue;
1239 							if (sin6->sin6_scope_id == 0) {
1240 								if (sa6_recoverscope(sin6) != 0)
1241 									/*
1242 									 *
1243 									 * bad
1244 									 *
1245 									 * li
1246 									 * nk
1247 									 *
1248 									 * loc
1249 									 * al
1250 									 *
1251 									 * add
1252 									 * re
1253 									 * ss
1254 									 * */
1255 									continue;
1256 							}
1257 						}
1258 						if ((site_scope == 0) &&
1259 						    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1260 							continue;
1261 						}
1262 						memcpy(sas, sin6, sizeof(*sin6));
1263 						((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1264 						sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1265 						actual += sizeof(*sin6);
1266 						if (actual >= limit) {
1267 							return (actual);
1268 						}
1269 					} else {
1270 						continue;
1271 					}
1272 					break;
1273 #endif
1274 				default:
1275 					/* TSNH */
1276 					break;
1277 				}
1278 			}
1279 		}
1280 	} else {
1281 		struct sctp_laddr *laddr;
1282 
1283 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1284 			if (stcb) {
1285 				if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1286 					continue;
1287 				}
1288 			}
1289 			if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1290 				continue;
1291 
1292 			((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1293 			sas = (struct sockaddr_storage *)((caddr_t)sas +
1294 			    laddr->ifa->address.sa.sa_len);
1295 			actual += laddr->ifa->address.sa.sa_len;
1296 			if (actual >= limit) {
1297 				return (actual);
1298 			}
1299 		}
1300 	}
1301 	return (actual);
1302 }
1303 
1304 static size_t
1305 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1306     struct sctp_tcb *stcb,
1307     size_t limit,
1308     struct sockaddr_storage *sas)
1309 {
1310 	size_t size = 0;
1311 
1312 	SCTP_IPI_ADDR_RLOCK();
1313 	/* fill up addresses for the endpoint's default vrf */
1314 	size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1315 	    inp->def_vrf_id);
1316 	SCTP_IPI_ADDR_RUNLOCK();
1317 	return (size);
1318 }
1319 
1320 /*
1321  * NOTE: assumes addr lock is held
1322  */
1323 static int
1324 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1325 {
1326 	int cnt = 0;
1327 	struct sctp_vrf *vrf = NULL;
1328 
1329 	/*
1330 	 * In both sub-set bound an bound_all cases we return the MAXIMUM
1331 	 * number of addresses that you COULD get. In reality the sub-set
1332 	 * bound may have an exclusion list for a given TCB OR in the
1333 	 * bound-all case a TCB may NOT include the loopback or other
1334 	 * addresses as well.
1335 	 */
1336 	vrf = sctp_find_vrf(vrf_id);
1337 	if (vrf == NULL) {
1338 		return (0);
1339 	}
1340 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1341 		struct sctp_ifn *sctp_ifn;
1342 		struct sctp_ifa *sctp_ifa;
1343 
1344 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1345 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1346 				/* Count them if they are the right type */
1347 				if (sctp_ifa->address.sa.sa_family == AF_INET) {
1348 					if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1349 						cnt += sizeof(struct sockaddr_in6);
1350 					else
1351 						cnt += sizeof(struct sockaddr_in);
1352 
1353 				} else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1354 					cnt += sizeof(struct sockaddr_in6);
1355 			}
1356 		}
1357 	} else {
1358 		struct sctp_laddr *laddr;
1359 
1360 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1361 			if (laddr->ifa->address.sa.sa_family == AF_INET) {
1362 				if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1363 					cnt += sizeof(struct sockaddr_in6);
1364 				else
1365 					cnt += sizeof(struct sockaddr_in);
1366 
1367 			} else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1368 				cnt += sizeof(struct sockaddr_in6);
1369 		}
1370 	}
1371 	return (cnt);
1372 }
1373 
1374 static int
1375 sctp_count_max_addresses(struct sctp_inpcb *inp)
1376 {
1377 	int cnt = 0;
1378 
1379 	SCTP_IPI_ADDR_RLOCK();
1380 	/* count addresses for the endpoint's default VRF */
1381 	cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1382 	SCTP_IPI_ADDR_RUNLOCK();
1383 	return (cnt);
1384 }
1385 
1386 static int
1387 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1388     size_t optsize, void *p, int delay)
1389 {
1390 	int error = 0;
1391 	int creat_lock_on = 0;
1392 	struct sctp_tcb *stcb = NULL;
1393 	struct sockaddr *sa;
1394 	int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1395 	int added = 0;
1396 	uint32_t vrf_id;
1397 	int bad_addresses = 0;
1398 	sctp_assoc_t *a_id;
1399 
1400 	SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1401 
1402 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1403 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1404 		/* We are already connected AND the TCP model */
1405 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1406 		return (EADDRINUSE);
1407 	}
1408 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
1409 	    (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1410 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1411 		return (EINVAL);
1412 	}
1413 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1414 		SCTP_INP_RLOCK(inp);
1415 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1416 		SCTP_INP_RUNLOCK(inp);
1417 	}
1418 	if (stcb) {
1419 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1420 		return (EALREADY);
1421 	}
1422 	SCTP_INP_INCR_REF(inp);
1423 	SCTP_ASOC_CREATE_LOCK(inp);
1424 	creat_lock_on = 1;
1425 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1426 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1427 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1428 		error = EFAULT;
1429 		goto out_now;
1430 	}
1431 	totaddrp = (int *)optval;
1432 	totaddr = *totaddrp;
1433 	sa = (struct sockaddr *)(totaddrp + 1);
1434 	stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1435 	if ((stcb != NULL) || bad_addresses) {
1436 		/* Already have or am bring up an association */
1437 		SCTP_ASOC_CREATE_UNLOCK(inp);
1438 		creat_lock_on = 0;
1439 		if (stcb)
1440 			SCTP_TCB_UNLOCK(stcb);
1441 		if (bad_addresses == 0) {
1442 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1443 			error = EALREADY;
1444 		}
1445 		goto out_now;
1446 	}
1447 #ifdef INET6
1448 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1449 	    (num_v6 > 0)) {
1450 		error = EINVAL;
1451 		goto out_now;
1452 	}
1453 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1454 	    (num_v4 > 0)) {
1455 		struct in6pcb *inp6;
1456 
1457 		inp6 = (struct in6pcb *)inp;
1458 		if (SCTP_IPV6_V6ONLY(inp6)) {
1459 			/*
1460 			 * if IPV6_V6ONLY flag, ignore connections destined
1461 			 * to a v4 addr or v4-mapped addr
1462 			 */
1463 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1464 			error = EINVAL;
1465 			goto out_now;
1466 		}
1467 	}
1468 #endif				/* INET6 */
1469 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1470 	    SCTP_PCB_FLAGS_UNBOUND) {
1471 		/* Bind a ephemeral port */
1472 		error = sctp_inpcb_bind(so, NULL, NULL, p);
1473 		if (error) {
1474 			goto out_now;
1475 		}
1476 	}
1477 	/* FIX ME: do we want to pass in a vrf on the connect call? */
1478 	vrf_id = inp->def_vrf_id;
1479 
1480 
1481 	/* We are GOOD to go */
1482 	stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id,
1483 	    (struct thread *)p
1484 	    );
1485 	if (stcb == NULL) {
1486 		/* Gak! no memory */
1487 		goto out_now;
1488 	}
1489 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1490 	/* move to second address */
1491 	if (sa->sa_family == AF_INET)
1492 		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1493 	else
1494 		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1495 
1496 	error = 0;
1497 	added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1498 	/* Fill in the return id */
1499 	if (error) {
1500 		(void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
1501 		goto out_now;
1502 	}
1503 	a_id = (sctp_assoc_t *) optval;
1504 	*a_id = sctp_get_associd(stcb);
1505 
1506 	/* initialize authentication parameters for the assoc */
1507 	sctp_initialize_auth_params(inp, stcb);
1508 
1509 	if (delay) {
1510 		/* doing delayed connection */
1511 		stcb->asoc.delayed_connection = 1;
1512 		sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1513 	} else {
1514 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1515 		sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1516 	}
1517 	SCTP_TCB_UNLOCK(stcb);
1518 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1519 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1520 		/* Set the connected flag so we can queue data */
1521 		soisconnecting(so);
1522 	}
1523 out_now:
1524 	if (creat_lock_on) {
1525 		SCTP_ASOC_CREATE_UNLOCK(inp);
1526 	}
1527 	SCTP_INP_DECR_REF(inp);
1528 	return error;
1529 }
1530 
1531 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1532 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1533 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1534 		SCTP_INP_RLOCK(inp); \
1535 		stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1536 		if (stcb) { \
1537 			SCTP_TCB_LOCK(stcb); \
1538                 } \
1539 		SCTP_INP_RUNLOCK(inp); \
1540 	} else if (assoc_id != 0) { \
1541 		stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1542 		if (stcb == NULL) { \
1543 		        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1544 			error = ENOENT; \
1545 			break; \
1546 		} \
1547 	} else { \
1548 		stcb = NULL; \
1549         } \
1550   }
1551 
1552 
1553 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size)  {\
1554 	if (size < sizeof(type)) { \
1555 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1556 		error = EINVAL; \
1557 		break; \
1558 	} else { \
1559 		destp = (type *)srcp; \
1560 	} \
1561       }
1562 
1563 static int
1564 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1565     void *p)
1566 {
1567 	struct sctp_inpcb *inp = NULL;
1568 	int error, val = 0;
1569 	struct sctp_tcb *stcb = NULL;
1570 
1571 	if (optval == NULL) {
1572 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1573 		return (EINVAL);
1574 	}
1575 	inp = (struct sctp_inpcb *)so->so_pcb;
1576 	if (inp == 0) {
1577 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1578 		return EINVAL;
1579 	}
1580 	error = 0;
1581 
1582 	switch (optname) {
1583 	case SCTP_NODELAY:
1584 	case SCTP_AUTOCLOSE:
1585 	case SCTP_EXPLICIT_EOR:
1586 	case SCTP_AUTO_ASCONF:
1587 	case SCTP_DISABLE_FRAGMENTS:
1588 	case SCTP_I_WANT_MAPPED_V4_ADDR:
1589 	case SCTP_USE_EXT_RCVINFO:
1590 		SCTP_INP_RLOCK(inp);
1591 		switch (optname) {
1592 		case SCTP_DISABLE_FRAGMENTS:
1593 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1594 			break;
1595 		case SCTP_I_WANT_MAPPED_V4_ADDR:
1596 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1597 			break;
1598 		case SCTP_AUTO_ASCONF:
1599 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1600 				/* only valid for bound all sockets */
1601 				val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1602 			} else {
1603 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1604 				error = EINVAL;
1605 				goto flags_out;
1606 			}
1607 			break;
1608 		case SCTP_EXPLICIT_EOR:
1609 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1610 			break;
1611 		case SCTP_NODELAY:
1612 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1613 			break;
1614 		case SCTP_USE_EXT_RCVINFO:
1615 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1616 			break;
1617 		case SCTP_AUTOCLOSE:
1618 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1619 				val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1620 			else
1621 				val = 0;
1622 			break;
1623 
1624 		default:
1625 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1626 			error = ENOPROTOOPT;
1627 		}		/* end switch (sopt->sopt_name) */
1628 		if (optname != SCTP_AUTOCLOSE) {
1629 			/* make it an "on/off" value */
1630 			val = (val != 0);
1631 		}
1632 		if (*optsize < sizeof(val)) {
1633 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1634 			error = EINVAL;
1635 		}
1636 flags_out:
1637 		SCTP_INP_RUNLOCK(inp);
1638 		if (error == 0) {
1639 			/* return the option value */
1640 			*(int *)optval = val;
1641 			*optsize = sizeof(val);
1642 		}
1643 		break;
1644 	case SCTP_GET_PACKET_LOG:
1645 		{
1646 #ifdef  SCTP_PACKET_LOGGING
1647 			uint8_t *target;
1648 			int ret;
1649 
1650 			SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1651 			ret = sctp_copy_out_packet_log(target, (int)*optsize);
1652 			*optsize = ret;
1653 #else
1654 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1655 			error = EOPNOTSUPP;
1656 #endif
1657 			break;
1658 		}
1659 	case SCTP_REUSE_PORT:
1660 		{
1661 			uint32_t *value;
1662 
1663 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1664 				/* Can't do this for a 1-m socket */
1665 				error = EINVAL;
1666 				break;
1667 			}
1668 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1669 			*value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1670 			*optsize = sizeof(uint32_t);
1671 		}
1672 		break;
1673 	case SCTP_PARTIAL_DELIVERY_POINT:
1674 		{
1675 			uint32_t *value;
1676 
1677 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1678 			*value = inp->partial_delivery_point;
1679 			*optsize = sizeof(uint32_t);
1680 		}
1681 		break;
1682 	case SCTP_FRAGMENT_INTERLEAVE:
1683 		{
1684 			uint32_t *value;
1685 
1686 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1687 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1688 				if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1689 					*value = SCTP_FRAG_LEVEL_2;
1690 				} else {
1691 					*value = SCTP_FRAG_LEVEL_1;
1692 				}
1693 			} else {
1694 				*value = SCTP_FRAG_LEVEL_0;
1695 			}
1696 			*optsize = sizeof(uint32_t);
1697 		}
1698 		break;
1699 	case SCTP_CMT_ON_OFF:
1700 		{
1701 			struct sctp_assoc_value *av;
1702 
1703 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1704 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
1705 				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1706 				if (stcb) {
1707 					av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1708 					SCTP_TCB_UNLOCK(stcb);
1709 
1710 				} else {
1711 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1712 					error = ENOTCONN;
1713 				}
1714 			} else {
1715 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1716 				error = ENOPROTOOPT;
1717 			}
1718 			*optsize = sizeof(*av);
1719 		}
1720 		break;
1721 		/* EY - set socket option for nr_sacks  */
1722 	case SCTP_NR_SACK_ON_OFF:
1723 		{
1724 			struct sctp_assoc_value *av;
1725 
1726 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1727 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
1728 				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1729 				if (stcb) {
1730 					av->assoc_value = stcb->asoc.sctp_nr_sack_on_off;
1731 					SCTP_TCB_UNLOCK(stcb);
1732 
1733 				} else {
1734 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1735 					error = ENOTCONN;
1736 				}
1737 			} else {
1738 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1739 				error = ENOPROTOOPT;
1740 			}
1741 			*optsize = sizeof(*av);
1742 		}
1743 		break;
1744 		/* JRS - Get socket option for pluggable congestion control */
1745 	case SCTP_PLUGGABLE_CC:
1746 		{
1747 			struct sctp_assoc_value *av;
1748 
1749 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1750 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1751 			if (stcb) {
1752 				av->assoc_value = stcb->asoc.congestion_control_module;
1753 				SCTP_TCB_UNLOCK(stcb);
1754 			} else {
1755 				av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1756 			}
1757 			*optsize = sizeof(*av);
1758 		}
1759 		break;
1760 	case SCTP_GET_ADDR_LEN:
1761 		{
1762 			struct sctp_assoc_value *av;
1763 
1764 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1765 			error = EINVAL;
1766 #ifdef INET
1767 			if (av->assoc_value == AF_INET) {
1768 				av->assoc_value = sizeof(struct sockaddr_in);
1769 				error = 0;
1770 			}
1771 #endif
1772 #ifdef INET6
1773 			if (av->assoc_value == AF_INET6) {
1774 				av->assoc_value = sizeof(struct sockaddr_in6);
1775 				error = 0;
1776 			}
1777 #endif
1778 			if (error) {
1779 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1780 			}
1781 			*optsize = sizeof(*av);
1782 		}
1783 		break;
1784 	case SCTP_GET_ASSOC_NUMBER:
1785 		{
1786 			uint32_t *value, cnt;
1787 
1788 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1789 			cnt = 0;
1790 			SCTP_INP_RLOCK(inp);
1791 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1792 				cnt++;
1793 			}
1794 			SCTP_INP_RUNLOCK(inp);
1795 			*value = cnt;
1796 			*optsize = sizeof(uint32_t);
1797 		}
1798 		break;
1799 
1800 	case SCTP_GET_ASSOC_ID_LIST:
1801 		{
1802 			struct sctp_assoc_ids *ids;
1803 			unsigned int at, limit;
1804 
1805 			SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1806 			at = 0;
1807 			limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1808 			SCTP_INP_RLOCK(inp);
1809 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1810 				if (at < limit) {
1811 					ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1812 				} else {
1813 					error = EINVAL;
1814 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1815 					break;
1816 				}
1817 			}
1818 			SCTP_INP_RUNLOCK(inp);
1819 			ids->gaids_number_of_ids = at;
1820 			*optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1821 		}
1822 		break;
1823 	case SCTP_CONTEXT:
1824 		{
1825 			struct sctp_assoc_value *av;
1826 
1827 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1828 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1829 
1830 			if (stcb) {
1831 				av->assoc_value = stcb->asoc.context;
1832 				SCTP_TCB_UNLOCK(stcb);
1833 			} else {
1834 				SCTP_INP_RLOCK(inp);
1835 				av->assoc_value = inp->sctp_context;
1836 				SCTP_INP_RUNLOCK(inp);
1837 			}
1838 			*optsize = sizeof(*av);
1839 		}
1840 		break;
1841 	case SCTP_VRF_ID:
1842 		{
1843 			uint32_t *default_vrfid;
1844 
1845 			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1846 			*default_vrfid = inp->def_vrf_id;
1847 			break;
1848 		}
1849 	case SCTP_GET_ASOC_VRF:
1850 		{
1851 			struct sctp_assoc_value *id;
1852 
1853 			SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1854 			SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1855 			if (stcb == NULL) {
1856 				error = EINVAL;
1857 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1858 				break;
1859 			}
1860 			id->assoc_value = stcb->asoc.vrf_id;
1861 			break;
1862 		}
1863 	case SCTP_GET_VRF_IDS:
1864 		{
1865 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1866 			error = EOPNOTSUPP;
1867 			break;
1868 		}
1869 	case SCTP_GET_NONCE_VALUES:
1870 		{
1871 			struct sctp_get_nonce_values *gnv;
1872 
1873 			SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1874 			SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1875 
1876 			if (stcb) {
1877 				gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1878 				gnv->gn_local_tag = stcb->asoc.my_vtag;
1879 				SCTP_TCB_UNLOCK(stcb);
1880 			} else {
1881 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1882 				error = ENOTCONN;
1883 			}
1884 			*optsize = sizeof(*gnv);
1885 		}
1886 		break;
1887 	case SCTP_DELAYED_SACK:
1888 		{
1889 			struct sctp_sack_info *sack;
1890 
1891 			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1892 			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1893 			if (stcb) {
1894 				sack->sack_delay = stcb->asoc.delayed_ack;
1895 				sack->sack_freq = stcb->asoc.sack_freq;
1896 				SCTP_TCB_UNLOCK(stcb);
1897 			} else {
1898 				SCTP_INP_RLOCK(inp);
1899 				sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1900 				sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1901 				SCTP_INP_RUNLOCK(inp);
1902 			}
1903 			*optsize = sizeof(*sack);
1904 		}
1905 		break;
1906 
1907 	case SCTP_GET_SNDBUF_USE:
1908 		{
1909 			struct sctp_sockstat *ss;
1910 
1911 			SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1912 			SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1913 
1914 			if (stcb) {
1915 				ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1916 				ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1917 				    stcb->asoc.size_on_all_streams);
1918 				SCTP_TCB_UNLOCK(stcb);
1919 			} else {
1920 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1921 				error = ENOTCONN;
1922 			}
1923 			*optsize = sizeof(struct sctp_sockstat);
1924 		}
1925 		break;
1926 	case SCTP_MAX_BURST:
1927 		{
1928 			uint8_t *value;
1929 
1930 			SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1931 
1932 			SCTP_INP_RLOCK(inp);
1933 			*value = inp->sctp_ep.max_burst;
1934 			SCTP_INP_RUNLOCK(inp);
1935 			*optsize = sizeof(uint8_t);
1936 		}
1937 		break;
1938 	case SCTP_MAXSEG:
1939 		{
1940 			struct sctp_assoc_value *av;
1941 			int ovh;
1942 
1943 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1944 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1945 
1946 			if (stcb) {
1947 				av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1948 				SCTP_TCB_UNLOCK(stcb);
1949 			} else {
1950 				SCTP_INP_RLOCK(inp);
1951 				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1952 					ovh = SCTP_MED_OVERHEAD;
1953 				} else {
1954 					ovh = SCTP_MED_V4_OVERHEAD;
1955 				}
1956 				if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
1957 					av->assoc_value = 0;
1958 				else
1959 					av->assoc_value = inp->sctp_frag_point - ovh;
1960 				SCTP_INP_RUNLOCK(inp);
1961 			}
1962 			*optsize = sizeof(struct sctp_assoc_value);
1963 		}
1964 		break;
1965 	case SCTP_GET_STAT_LOG:
1966 		error = sctp_fill_stat_log(optval, optsize);
1967 		break;
1968 	case SCTP_EVENTS:
1969 		{
1970 			struct sctp_event_subscribe *events;
1971 
1972 			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1973 			memset(events, 0, sizeof(*events));
1974 			SCTP_INP_RLOCK(inp);
1975 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1976 				events->sctp_data_io_event = 1;
1977 
1978 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1979 				events->sctp_association_event = 1;
1980 
1981 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1982 				events->sctp_address_event = 1;
1983 
1984 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1985 				events->sctp_send_failure_event = 1;
1986 
1987 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1988 				events->sctp_peer_error_event = 1;
1989 
1990 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1991 				events->sctp_shutdown_event = 1;
1992 
1993 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1994 				events->sctp_partial_delivery_event = 1;
1995 
1996 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1997 				events->sctp_adaptation_layer_event = 1;
1998 
1999 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2000 				events->sctp_authentication_event = 1;
2001 
2002 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2003 				events->sctp_sender_dry_event = 1;
2004 
2005 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2006 				events->sctp_stream_reset_events = 1;
2007 			SCTP_INP_RUNLOCK(inp);
2008 			*optsize = sizeof(struct sctp_event_subscribe);
2009 		}
2010 		break;
2011 
2012 	case SCTP_ADAPTATION_LAYER:
2013 		{
2014 			uint32_t *value;
2015 
2016 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2017 
2018 			SCTP_INP_RLOCK(inp);
2019 			*value = inp->sctp_ep.adaptation_layer_indicator;
2020 			SCTP_INP_RUNLOCK(inp);
2021 			*optsize = sizeof(uint32_t);
2022 		}
2023 		break;
2024 	case SCTP_SET_INITIAL_DBG_SEQ:
2025 		{
2026 			uint32_t *value;
2027 
2028 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2029 			SCTP_INP_RLOCK(inp);
2030 			*value = inp->sctp_ep.initial_sequence_debug;
2031 			SCTP_INP_RUNLOCK(inp);
2032 			*optsize = sizeof(uint32_t);
2033 		}
2034 		break;
2035 	case SCTP_GET_LOCAL_ADDR_SIZE:
2036 		{
2037 			uint32_t *value;
2038 
2039 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2040 			SCTP_INP_RLOCK(inp);
2041 			*value = sctp_count_max_addresses(inp);
2042 			SCTP_INP_RUNLOCK(inp);
2043 			*optsize = sizeof(uint32_t);
2044 		}
2045 		break;
2046 	case SCTP_GET_REMOTE_ADDR_SIZE:
2047 		{
2048 			uint32_t *value;
2049 			size_t size;
2050 			struct sctp_nets *net;
2051 
2052 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2053 			/* FIXME MT: change to sctp_assoc_value? */
2054 			SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2055 
2056 			if (stcb) {
2057 				size = 0;
2058 				/* Count the sizes */
2059 				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2060 					if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2061 					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2062 						size += sizeof(struct sockaddr_in6);
2063 					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2064 						size += sizeof(struct sockaddr_in);
2065 					} else {
2066 						/* huh */
2067 						break;
2068 					}
2069 				}
2070 				SCTP_TCB_UNLOCK(stcb);
2071 				*value = (uint32_t) size;
2072 			} else {
2073 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2074 				error = ENOTCONN;
2075 			}
2076 			*optsize = sizeof(uint32_t);
2077 		}
2078 		break;
2079 	case SCTP_GET_PEER_ADDRESSES:
2080 		/*
2081 		 * Get the address information, an array is passed in to
2082 		 * fill up we pack it.
2083 		 */
2084 		{
2085 			size_t cpsz, left;
2086 			struct sockaddr_storage *sas;
2087 			struct sctp_nets *net;
2088 			struct sctp_getaddresses *saddr;
2089 
2090 			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2091 			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2092 
2093 			if (stcb) {
2094 				left = (*optsize) - sizeof(struct sctp_getaddresses);
2095 				*optsize = sizeof(struct sctp_getaddresses);
2096 				sas = (struct sockaddr_storage *)&saddr->addr[0];
2097 
2098 				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2099 					if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2100 					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2101 						cpsz = sizeof(struct sockaddr_in6);
2102 					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2103 						cpsz = sizeof(struct sockaddr_in);
2104 					} else {
2105 						/* huh */
2106 						break;
2107 					}
2108 					if (left < cpsz) {
2109 						/* not enough room. */
2110 						break;
2111 					}
2112 #ifdef INET6
2113 					if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2114 					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2115 						/* Must map the address */
2116 						in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2117 						    (struct sockaddr_in6 *)sas);
2118 					} else {
2119 #endif
2120 						memcpy(sas, &net->ro._l_addr, cpsz);
2121 #ifdef INET6
2122 					}
2123 #endif
2124 					((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2125 
2126 					sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2127 					left -= cpsz;
2128 					*optsize += cpsz;
2129 				}
2130 				SCTP_TCB_UNLOCK(stcb);
2131 			} else {
2132 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2133 				error = ENOENT;
2134 			}
2135 		}
2136 		break;
2137 	case SCTP_GET_LOCAL_ADDRESSES:
2138 		{
2139 			size_t limit, actual;
2140 			struct sockaddr_storage *sas;
2141 			struct sctp_getaddresses *saddr;
2142 
2143 			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2144 			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2145 
2146 			sas = (struct sockaddr_storage *)&saddr->addr[0];
2147 			limit = *optsize - sizeof(sctp_assoc_t);
2148 			actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2149 			if (stcb) {
2150 				SCTP_TCB_UNLOCK(stcb);
2151 			}
2152 			*optsize = sizeof(struct sockaddr_storage) + actual;
2153 		}
2154 		break;
2155 	case SCTP_PEER_ADDR_PARAMS:
2156 		{
2157 			struct sctp_paddrparams *paddrp;
2158 			struct sctp_nets *net;
2159 
2160 			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2161 			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2162 
2163 			net = NULL;
2164 			if (stcb) {
2165 				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2166 			} else {
2167 				/*
2168 				 * We increment here since
2169 				 * sctp_findassociation_ep_addr() wil do a
2170 				 * decrement if it finds the stcb as long as
2171 				 * the locked tcb (last argument) is NOT a
2172 				 * TCB.. aka NULL.
2173 				 */
2174 				SCTP_INP_INCR_REF(inp);
2175 				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2176 				if (stcb == NULL) {
2177 					SCTP_INP_DECR_REF(inp);
2178 				}
2179 			}
2180 			if (stcb && (net == NULL)) {
2181 				struct sockaddr *sa;
2182 
2183 				sa = (struct sockaddr *)&paddrp->spp_address;
2184 				if (sa->sa_family == AF_INET) {
2185 					struct sockaddr_in *sin;
2186 
2187 					sin = (struct sockaddr_in *)sa;
2188 					if (sin->sin_addr.s_addr) {
2189 						error = EINVAL;
2190 						SCTP_TCB_UNLOCK(stcb);
2191 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2192 						break;
2193 					}
2194 				} else if (sa->sa_family == AF_INET6) {
2195 					struct sockaddr_in6 *sin6;
2196 
2197 					sin6 = (struct sockaddr_in6 *)sa;
2198 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2199 						error = EINVAL;
2200 						SCTP_TCB_UNLOCK(stcb);
2201 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2202 						break;
2203 					}
2204 				} else {
2205 					error = EAFNOSUPPORT;
2206 					SCTP_TCB_UNLOCK(stcb);
2207 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2208 					break;
2209 				}
2210 			}
2211 			if (stcb) {
2212 				/* Applys to the specific association */
2213 				paddrp->spp_flags = 0;
2214 				if (net) {
2215 					int ovh;
2216 
2217 					if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2218 						ovh = SCTP_MED_OVERHEAD;
2219 					} else {
2220 						ovh = SCTP_MED_V4_OVERHEAD;
2221 					}
2222 
2223 
2224 					paddrp->spp_pathmaxrxt = net->failure_threshold;
2225 					paddrp->spp_pathmtu = net->mtu - ovh;
2226 					/* get flags for HB */
2227 					if (net->dest_state & SCTP_ADDR_NOHB)
2228 						paddrp->spp_flags |= SPP_HB_DISABLE;
2229 					else
2230 						paddrp->spp_flags |= SPP_HB_ENABLE;
2231 					/* get flags for PMTU */
2232 					if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2233 						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2234 					} else {
2235 						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2236 					}
2237 #ifdef INET
2238 					if (net->ro._l_addr.sin.sin_family == AF_INET) {
2239 						paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2240 						paddrp->spp_flags |= SPP_IPV4_TOS;
2241 					}
2242 #endif
2243 #ifdef INET6
2244 					if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2245 						paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2246 						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2247 					}
2248 #endif
2249 				} else {
2250 					/*
2251 					 * No destination so return default
2252 					 * value
2253 					 */
2254 					int cnt = 0;
2255 
2256 					paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2257 					paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2258 #ifdef INET
2259 					paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2260 					paddrp->spp_flags |= SPP_IPV4_TOS;
2261 #endif
2262 #ifdef INET6
2263 					paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2264 					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2265 #endif
2266 					/* default settings should be these */
2267 					if (stcb->asoc.hb_is_disabled == 0) {
2268 						paddrp->spp_flags |= SPP_HB_ENABLE;
2269 					} else {
2270 						paddrp->spp_flags |= SPP_HB_DISABLE;
2271 					}
2272 					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2273 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2274 							cnt++;
2275 						}
2276 					}
2277 					if (cnt) {
2278 						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2279 					}
2280 				}
2281 				paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2282 				paddrp->spp_assoc_id = sctp_get_associd(stcb);
2283 				SCTP_TCB_UNLOCK(stcb);
2284 			} else {
2285 				/* Use endpoint defaults */
2286 				SCTP_INP_RLOCK(inp);
2287 				paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2288 				paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2289 				paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2290 				/* get inp's default */
2291 #ifdef INET
2292 				paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2293 				paddrp->spp_flags |= SPP_IPV4_TOS;
2294 #endif
2295 #ifdef INET6
2296 				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2297 					paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2298 					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2299 				}
2300 #endif
2301 				/* can't return this */
2302 				paddrp->spp_pathmtu = 0;
2303 
2304 				/* default behavior, no stcb */
2305 				paddrp->spp_flags = SPP_PMTUD_ENABLE;
2306 
2307 				if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2308 					paddrp->spp_flags |= SPP_HB_ENABLE;
2309 				} else {
2310 					paddrp->spp_flags |= SPP_HB_DISABLE;
2311 				}
2312 				SCTP_INP_RUNLOCK(inp);
2313 			}
2314 			*optsize = sizeof(struct sctp_paddrparams);
2315 		}
2316 		break;
2317 	case SCTP_GET_PEER_ADDR_INFO:
2318 		{
2319 			struct sctp_paddrinfo *paddri;
2320 			struct sctp_nets *net;
2321 
2322 			SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2323 			SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2324 
2325 			net = NULL;
2326 			if (stcb) {
2327 				net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2328 			} else {
2329 				/*
2330 				 * We increment here since
2331 				 * sctp_findassociation_ep_addr() wil do a
2332 				 * decrement if it finds the stcb as long as
2333 				 * the locked tcb (last argument) is NOT a
2334 				 * TCB.. aka NULL.
2335 				 */
2336 				SCTP_INP_INCR_REF(inp);
2337 				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2338 				if (stcb == NULL) {
2339 					SCTP_INP_DECR_REF(inp);
2340 				}
2341 			}
2342 
2343 			if ((stcb) && (net)) {
2344 				paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2345 				paddri->spinfo_cwnd = net->cwnd;
2346 				paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2347 				paddri->spinfo_rto = net->RTO;
2348 				paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2349 				SCTP_TCB_UNLOCK(stcb);
2350 			} else {
2351 				if (stcb) {
2352 					SCTP_TCB_UNLOCK(stcb);
2353 				}
2354 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2355 				error = ENOENT;
2356 			}
2357 			*optsize = sizeof(struct sctp_paddrinfo);
2358 		}
2359 		break;
2360 	case SCTP_PCB_STATUS:
2361 		{
2362 			struct sctp_pcbinfo *spcb;
2363 
2364 			SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2365 			sctp_fill_pcbinfo(spcb);
2366 			*optsize = sizeof(struct sctp_pcbinfo);
2367 		}
2368 		break;
2369 
2370 	case SCTP_STATUS:
2371 		{
2372 			struct sctp_nets *net;
2373 			struct sctp_status *sstat;
2374 
2375 			SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2376 			SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2377 
2378 			if (stcb == NULL) {
2379 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2380 				error = EINVAL;
2381 				break;
2382 			}
2383 			/*
2384 			 * I think passing the state is fine since
2385 			 * sctp_constants.h will be available to the user
2386 			 * land.
2387 			 */
2388 			sstat->sstat_state = stcb->asoc.state;
2389 			sstat->sstat_assoc_id = sctp_get_associd(stcb);
2390 			sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2391 			sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2392 			/*
2393 			 * We can't include chunks that have been passed to
2394 			 * the socket layer. Only things in queue.
2395 			 */
2396 			sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2397 			    stcb->asoc.cnt_on_all_streams);
2398 
2399 
2400 			sstat->sstat_instrms = stcb->asoc.streamincnt;
2401 			sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2402 			sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2403 			memcpy(&sstat->sstat_primary.spinfo_address,
2404 			    &stcb->asoc.primary_destination->ro._l_addr,
2405 			    ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2406 			net = stcb->asoc.primary_destination;
2407 			((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2408 			/*
2409 			 * Again the user can get info from sctp_constants.h
2410 			 * for what the state of the network is.
2411 			 */
2412 			sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2413 			sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2414 			sstat->sstat_primary.spinfo_srtt = net->lastsa;
2415 			sstat->sstat_primary.spinfo_rto = net->RTO;
2416 			sstat->sstat_primary.spinfo_mtu = net->mtu;
2417 			sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2418 			SCTP_TCB_UNLOCK(stcb);
2419 			*optsize = sizeof(*sstat);
2420 		}
2421 		break;
2422 	case SCTP_RTOINFO:
2423 		{
2424 			struct sctp_rtoinfo *srto;
2425 
2426 			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2427 			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2428 
2429 			if (stcb) {
2430 				srto->srto_initial = stcb->asoc.initial_rto;
2431 				srto->srto_max = stcb->asoc.maxrto;
2432 				srto->srto_min = stcb->asoc.minrto;
2433 				SCTP_TCB_UNLOCK(stcb);
2434 			} else {
2435 				SCTP_INP_RLOCK(inp);
2436 				srto->srto_initial = inp->sctp_ep.initial_rto;
2437 				srto->srto_max = inp->sctp_ep.sctp_maxrto;
2438 				srto->srto_min = inp->sctp_ep.sctp_minrto;
2439 				SCTP_INP_RUNLOCK(inp);
2440 			}
2441 			*optsize = sizeof(*srto);
2442 		}
2443 		break;
2444 	case SCTP_ASSOCINFO:
2445 		{
2446 			struct sctp_assocparams *sasoc;
2447 			uint32_t oldval;
2448 
2449 			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2450 			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2451 
2452 			if (stcb) {
2453 				oldval = sasoc->sasoc_cookie_life;
2454 				sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2455 				sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2456 				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2457 				sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2458 				sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2459 				SCTP_TCB_UNLOCK(stcb);
2460 			} else {
2461 				SCTP_INP_RLOCK(inp);
2462 				sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2463 				sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2464 				sasoc->sasoc_number_peer_destinations = 0;
2465 				sasoc->sasoc_peer_rwnd = 0;
2466 				sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2467 				SCTP_INP_RUNLOCK(inp);
2468 			}
2469 			*optsize = sizeof(*sasoc);
2470 		}
2471 		break;
2472 	case SCTP_DEFAULT_SEND_PARAM:
2473 		{
2474 			struct sctp_sndrcvinfo *s_info;
2475 
2476 			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2477 			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2478 
2479 			if (stcb) {
2480 				memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2481 				SCTP_TCB_UNLOCK(stcb);
2482 			} else {
2483 				SCTP_INP_RLOCK(inp);
2484 				memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2485 				SCTP_INP_RUNLOCK(inp);
2486 			}
2487 			*optsize = sizeof(*s_info);
2488 		}
2489 		break;
2490 	case SCTP_INITMSG:
2491 		{
2492 			struct sctp_initmsg *sinit;
2493 
2494 			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2495 			SCTP_INP_RLOCK(inp);
2496 			sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2497 			sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2498 			sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2499 			sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2500 			SCTP_INP_RUNLOCK(inp);
2501 			*optsize = sizeof(*sinit);
2502 		}
2503 		break;
2504 	case SCTP_PRIMARY_ADDR:
2505 		/* we allow a "get" operation on this */
2506 		{
2507 			struct sctp_setprim *ssp;
2508 
2509 			SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2510 			SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2511 
2512 			if (stcb) {
2513 				/* simply copy out the sockaddr_storage... */
2514 				int len;
2515 
2516 				len = *optsize;
2517 				if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2518 					len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2519 
2520 				memcpy(&ssp->ssp_addr,
2521 				    &stcb->asoc.primary_destination->ro._l_addr,
2522 				    len);
2523 				SCTP_TCB_UNLOCK(stcb);
2524 			} else {
2525 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2526 				error = EINVAL;
2527 			}
2528 			*optsize = sizeof(*ssp);
2529 		}
2530 		break;
2531 
2532 	case SCTP_HMAC_IDENT:
2533 		{
2534 			struct sctp_hmacalgo *shmac;
2535 			sctp_hmaclist_t *hmaclist;
2536 			uint32_t size;
2537 			int i;
2538 
2539 			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2540 
2541 			SCTP_INP_RLOCK(inp);
2542 			hmaclist = inp->sctp_ep.local_hmacs;
2543 			if (hmaclist == NULL) {
2544 				/* no HMACs to return */
2545 				*optsize = sizeof(*shmac);
2546 				SCTP_INP_RUNLOCK(inp);
2547 				break;
2548 			}
2549 			/* is there room for all of the hmac ids? */
2550 			size = sizeof(*shmac) + (hmaclist->num_algo *
2551 			    sizeof(shmac->shmac_idents[0]));
2552 			if ((size_t)(*optsize) < size) {
2553 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2554 				error = EINVAL;
2555 				SCTP_INP_RUNLOCK(inp);
2556 				break;
2557 			}
2558 			/* copy in the list */
2559 			shmac->shmac_number_of_idents = hmaclist->num_algo;
2560 			for (i = 0; i < hmaclist->num_algo; i++) {
2561 				shmac->shmac_idents[i] = hmaclist->hmac[i];
2562 			}
2563 			SCTP_INP_RUNLOCK(inp);
2564 			*optsize = size;
2565 			break;
2566 		}
2567 	case SCTP_AUTH_ACTIVE_KEY:
2568 		{
2569 			struct sctp_authkeyid *scact;
2570 
2571 			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2572 			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2573 
2574 			if (stcb) {
2575 				/* get the active key on the assoc */
2576 				scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2577 				SCTP_TCB_UNLOCK(stcb);
2578 			} else {
2579 				/* get the endpoint active key */
2580 				SCTP_INP_RLOCK(inp);
2581 				scact->scact_keynumber = inp->sctp_ep.default_keyid;
2582 				SCTP_INP_RUNLOCK(inp);
2583 			}
2584 			*optsize = sizeof(*scact);
2585 			break;
2586 		}
2587 	case SCTP_LOCAL_AUTH_CHUNKS:
2588 		{
2589 			struct sctp_authchunks *sac;
2590 			sctp_auth_chklist_t *chklist = NULL;
2591 			size_t size = 0;
2592 
2593 			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2594 			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2595 
2596 			if (stcb) {
2597 				/* get off the assoc */
2598 				chklist = stcb->asoc.local_auth_chunks;
2599 				/* is there enough space? */
2600 				size = sctp_auth_get_chklist_size(chklist);
2601 				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2602 					error = EINVAL;
2603 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2604 				} else {
2605 					/* copy in the chunks */
2606 					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2607 				}
2608 				SCTP_TCB_UNLOCK(stcb);
2609 			} else {
2610 				/* get off the endpoint */
2611 				SCTP_INP_RLOCK(inp);
2612 				chklist = inp->sctp_ep.local_auth_chunks;
2613 				/* is there enough space? */
2614 				size = sctp_auth_get_chklist_size(chklist);
2615 				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2616 					error = EINVAL;
2617 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2618 				} else {
2619 					/* copy in the chunks */
2620 					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2621 				}
2622 				SCTP_INP_RUNLOCK(inp);
2623 			}
2624 			*optsize = sizeof(struct sctp_authchunks) + size;
2625 			break;
2626 		}
2627 	case SCTP_PEER_AUTH_CHUNKS:
2628 		{
2629 			struct sctp_authchunks *sac;
2630 			sctp_auth_chklist_t *chklist = NULL;
2631 			size_t size = 0;
2632 
2633 			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2634 			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2635 
2636 			if (stcb) {
2637 				/* get off the assoc */
2638 				chklist = stcb->asoc.peer_auth_chunks;
2639 				/* is there enough space? */
2640 				size = sctp_auth_get_chklist_size(chklist);
2641 				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2642 					error = EINVAL;
2643 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2644 				} else {
2645 					/* copy in the chunks */
2646 					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2647 				}
2648 				SCTP_TCB_UNLOCK(stcb);
2649 			} else {
2650 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2651 				error = ENOENT;
2652 			}
2653 			*optsize = sizeof(struct sctp_authchunks) + size;
2654 			break;
2655 		}
2656 
2657 
2658 	default:
2659 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2660 		error = ENOPROTOOPT;
2661 		*optsize = 0;
2662 		break;
2663 	}			/* end switch (sopt->sopt_name) */
2664 	return (error);
2665 }
2666 
2667 static int
2668 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2669     void *p)
2670 {
2671 	int error, set_opt;
2672 	uint32_t *mopt;
2673 	struct sctp_tcb *stcb = NULL;
2674 	struct sctp_inpcb *inp = NULL;
2675 	uint32_t vrf_id;
2676 
2677 	if (optval == NULL) {
2678 		SCTP_PRINTF("optval is NULL\n");
2679 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2680 		return (EINVAL);
2681 	}
2682 	inp = (struct sctp_inpcb *)so->so_pcb;
2683 	if (inp == 0) {
2684 		SCTP_PRINTF("inp is NULL?\n");
2685 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2686 		return EINVAL;
2687 	}
2688 	vrf_id = inp->def_vrf_id;
2689 
2690 	error = 0;
2691 	switch (optname) {
2692 	case SCTP_NODELAY:
2693 	case SCTP_AUTOCLOSE:
2694 	case SCTP_AUTO_ASCONF:
2695 	case SCTP_EXPLICIT_EOR:
2696 	case SCTP_DISABLE_FRAGMENTS:
2697 	case SCTP_USE_EXT_RCVINFO:
2698 	case SCTP_I_WANT_MAPPED_V4_ADDR:
2699 		/* copy in the option value */
2700 		SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2701 		set_opt = 0;
2702 		if (error)
2703 			break;
2704 		switch (optname) {
2705 		case SCTP_DISABLE_FRAGMENTS:
2706 			set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2707 			break;
2708 		case SCTP_AUTO_ASCONF:
2709 			/*
2710 			 * NOTE: we don't really support this flag
2711 			 */
2712 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2713 				/* only valid for bound all sockets */
2714 				set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2715 			} else {
2716 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2717 				return (EINVAL);
2718 			}
2719 			break;
2720 		case SCTP_EXPLICIT_EOR:
2721 			set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2722 			break;
2723 		case SCTP_USE_EXT_RCVINFO:
2724 			set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2725 			break;
2726 		case SCTP_I_WANT_MAPPED_V4_ADDR:
2727 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2728 				set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2729 			} else {
2730 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2731 				return (EINVAL);
2732 			}
2733 			break;
2734 		case SCTP_NODELAY:
2735 			set_opt = SCTP_PCB_FLAGS_NODELAY;
2736 			break;
2737 		case SCTP_AUTOCLOSE:
2738 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2739 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2740 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2741 				return (EINVAL);
2742 			}
2743 			set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2744 			/*
2745 			 * The value is in ticks. Note this does not effect
2746 			 * old associations, only new ones.
2747 			 */
2748 			inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2749 			break;
2750 		}
2751 		SCTP_INP_WLOCK(inp);
2752 		if (*mopt != 0) {
2753 			sctp_feature_on(inp, set_opt);
2754 		} else {
2755 			sctp_feature_off(inp, set_opt);
2756 		}
2757 		SCTP_INP_WUNLOCK(inp);
2758 		break;
2759 	case SCTP_REUSE_PORT:
2760 		{
2761 			SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2762 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2763 				/* Can't set it after we are bound */
2764 				error = EINVAL;
2765 				break;
2766 			}
2767 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2768 				/* Can't do this for a 1-m socket */
2769 				error = EINVAL;
2770 				break;
2771 			}
2772 			if (optval)
2773 				sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
2774 			else
2775 				sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
2776 		}
2777 		break;
2778 	case SCTP_PARTIAL_DELIVERY_POINT:
2779 		{
2780 			uint32_t *value;
2781 
2782 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2783 			if (*value > SCTP_SB_LIMIT_RCV(so)) {
2784 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2785 				error = EINVAL;
2786 				break;
2787 			}
2788 			inp->partial_delivery_point = *value;
2789 		}
2790 		break;
2791 	case SCTP_FRAGMENT_INTERLEAVE:
2792 		/* not yet until we re-write sctp_recvmsg() */
2793 		{
2794 			uint32_t *level;
2795 
2796 			SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2797 			if (*level == SCTP_FRAG_LEVEL_2) {
2798 				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2799 				sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2800 			} else if (*level == SCTP_FRAG_LEVEL_1) {
2801 				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2802 				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2803 			} else if (*level == SCTP_FRAG_LEVEL_0) {
2804 				sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2805 				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2806 
2807 			} else {
2808 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2809 				error = EINVAL;
2810 			}
2811 		}
2812 		break;
2813 	case SCTP_CMT_ON_OFF:
2814 		{
2815 			struct sctp_assoc_value *av;
2816 
2817 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2818 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
2819 				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2820 				if (stcb) {
2821 					stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2822 					SCTP_TCB_UNLOCK(stcb);
2823 				} else {
2824 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2825 					error = ENOTCONN;
2826 				}
2827 			} else {
2828 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2829 				error = ENOPROTOOPT;
2830 			}
2831 		}
2832 		break;
2833 		/* EY nr_sack_on_off socket option */
2834 	case SCTP_NR_SACK_ON_OFF:
2835 		{
2836 			struct sctp_assoc_value *av;
2837 
2838 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2839 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
2840 				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2841 				if (stcb) {
2842 					stcb->asoc.sctp_nr_sack_on_off = (uint8_t) av->assoc_value;
2843 					SCTP_TCB_UNLOCK(stcb);
2844 				} else {
2845 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2846 					error = ENOTCONN;
2847 				}
2848 			} else {
2849 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2850 				error = ENOPROTOOPT;
2851 			}
2852 		}
2853 		break;
2854 		/* JRS - Set socket option for pluggable congestion control */
2855 	case SCTP_PLUGGABLE_CC:
2856 		{
2857 			struct sctp_assoc_value *av;
2858 
2859 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2860 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2861 			if (stcb) {
2862 				switch (av->assoc_value) {
2863 					/*
2864 					 * JRS - Standard TCP congestion
2865 					 * control
2866 					 */
2867 				case SCTP_CC_RFC2581:
2868 					{
2869 						stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
2870 						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2871 						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
2872 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
2873 						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2874 						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2875 						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2876 						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2877 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2878 						SCTP_TCB_UNLOCK(stcb);
2879 						break;
2880 					}
2881 					/*
2882 					 * JRS - High Speed TCP congestion
2883 					 * control (Floyd)
2884 					 */
2885 				case SCTP_CC_HSTCP:
2886 					{
2887 						stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
2888 						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2889 						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
2890 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
2891 						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2892 						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2893 						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2894 						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2895 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2896 						SCTP_TCB_UNLOCK(stcb);
2897 						break;
2898 					}
2899 					/* JRS - HTCP congestion control */
2900 				case SCTP_CC_HTCP:
2901 					{
2902 						stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
2903 						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
2904 						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
2905 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
2906 						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
2907 						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
2908 						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2909 						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2910 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
2911 						SCTP_TCB_UNLOCK(stcb);
2912 						break;
2913 					}
2914 					/*
2915 					 * JRS - All other values are
2916 					 * invalid
2917 					 */
2918 				default:
2919 					{
2920 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2921 						error = EINVAL;
2922 						SCTP_TCB_UNLOCK(stcb);
2923 						break;
2924 					}
2925 				}
2926 			} else {
2927 				switch (av->assoc_value) {
2928 				case SCTP_CC_RFC2581:
2929 				case SCTP_CC_HSTCP:
2930 				case SCTP_CC_HTCP:
2931 					inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2932 					break;
2933 				default:
2934 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2935 					error = EINVAL;
2936 					break;
2937 				};
2938 			}
2939 		}
2940 		break;
2941 	case SCTP_CLR_STAT_LOG:
2942 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2943 		error = EOPNOTSUPP;
2944 		break;
2945 	case SCTP_CONTEXT:
2946 		{
2947 			struct sctp_assoc_value *av;
2948 
2949 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2950 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2951 
2952 			if (stcb) {
2953 				stcb->asoc.context = av->assoc_value;
2954 				SCTP_TCB_UNLOCK(stcb);
2955 			} else {
2956 				SCTP_INP_WLOCK(inp);
2957 				inp->sctp_context = av->assoc_value;
2958 				SCTP_INP_WUNLOCK(inp);
2959 			}
2960 		}
2961 		break;
2962 	case SCTP_VRF_ID:
2963 		{
2964 			uint32_t *default_vrfid;
2965 
2966 			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2967 			if (*default_vrfid > SCTP_MAX_VRF_ID) {
2968 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2969 				error = EINVAL;
2970 				break;
2971 			}
2972 			inp->def_vrf_id = *default_vrfid;
2973 			break;
2974 		}
2975 	case SCTP_DEL_VRF_ID:
2976 		{
2977 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2978 			error = EOPNOTSUPP;
2979 			break;
2980 		}
2981 	case SCTP_ADD_VRF_ID:
2982 		{
2983 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2984 			error = EOPNOTSUPP;
2985 			break;
2986 		}
2987 	case SCTP_DELAYED_SACK:
2988 		{
2989 			struct sctp_sack_info *sack;
2990 
2991 			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
2992 			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2993 			if (sack->sack_delay) {
2994 				if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
2995 					sack->sack_delay = SCTP_MAX_SACK_DELAY;
2996 			}
2997 			if (stcb) {
2998 				if (sack->sack_delay) {
2999 					if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3000 						sack->sack_delay = TICKS_TO_MSEC(1);
3001 					}
3002 					stcb->asoc.delayed_ack = sack->sack_delay;
3003 				}
3004 				if (sack->sack_freq) {
3005 					stcb->asoc.sack_freq = sack->sack_freq;
3006 				}
3007 				SCTP_TCB_UNLOCK(stcb);
3008 			} else {
3009 				SCTP_INP_WLOCK(inp);
3010 				if (sack->sack_delay) {
3011 					if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3012 						sack->sack_delay = TICKS_TO_MSEC(1);
3013 					}
3014 					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
3015 				}
3016 				if (sack->sack_freq) {
3017 					inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
3018 				}
3019 				SCTP_INP_WUNLOCK(inp);
3020 			}
3021 			break;
3022 		}
3023 	case SCTP_AUTH_CHUNK:
3024 		{
3025 			struct sctp_authchunk *sauth;
3026 
3027 			SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
3028 
3029 			SCTP_INP_WLOCK(inp);
3030 			if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
3031 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3032 				error = EINVAL;
3033 			}
3034 			SCTP_INP_WUNLOCK(inp);
3035 			break;
3036 		}
3037 	case SCTP_AUTH_KEY:
3038 		{
3039 			struct sctp_authkey *sca;
3040 			struct sctp_keyhead *shared_keys;
3041 			sctp_sharedkey_t *shared_key;
3042 			sctp_key_t *key = NULL;
3043 			size_t size;
3044 
3045 			SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
3046 			SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
3047 			size = optsize - sizeof(*sca);
3048 
3049 			if (stcb) {
3050 				/* set it on the assoc */
3051 				shared_keys = &stcb->asoc.shared_keys;
3052 				/* clear the cached keys for this key id */
3053 				sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
3054 				/*
3055 				 * create the new shared key and
3056 				 * insert/replace it
3057 				 */
3058 				if (size > 0) {
3059 					key = sctp_set_key(sca->sca_key, (uint32_t) size);
3060 					if (key == NULL) {
3061 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3062 						error = ENOMEM;
3063 						SCTP_TCB_UNLOCK(stcb);
3064 						break;
3065 					}
3066 				}
3067 				shared_key = sctp_alloc_sharedkey();
3068 				if (shared_key == NULL) {
3069 					sctp_free_key(key);
3070 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3071 					error = ENOMEM;
3072 					SCTP_TCB_UNLOCK(stcb);
3073 					break;
3074 				}
3075 				shared_key->key = key;
3076 				shared_key->keyid = sca->sca_keynumber;
3077 				error = sctp_insert_sharedkey(shared_keys, shared_key);
3078 				SCTP_TCB_UNLOCK(stcb);
3079 			} else {
3080 				/* set it on the endpoint */
3081 				SCTP_INP_WLOCK(inp);
3082 				shared_keys = &inp->sctp_ep.shared_keys;
3083 				/*
3084 				 * clear the cached keys on all assocs for
3085 				 * this key id
3086 				 */
3087 				sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
3088 				/*
3089 				 * create the new shared key and
3090 				 * insert/replace it
3091 				 */
3092 				if (size > 0) {
3093 					key = sctp_set_key(sca->sca_key, (uint32_t) size);
3094 					if (key == NULL) {
3095 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3096 						error = ENOMEM;
3097 						SCTP_INP_WUNLOCK(inp);
3098 						break;
3099 					}
3100 				}
3101 				shared_key = sctp_alloc_sharedkey();
3102 				if (shared_key == NULL) {
3103 					sctp_free_key(key);
3104 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3105 					error = ENOMEM;
3106 					SCTP_INP_WUNLOCK(inp);
3107 					break;
3108 				}
3109 				shared_key->key = key;
3110 				shared_key->keyid = sca->sca_keynumber;
3111 				error = sctp_insert_sharedkey(shared_keys, shared_key);
3112 				SCTP_INP_WUNLOCK(inp);
3113 			}
3114 			break;
3115 		}
3116 	case SCTP_HMAC_IDENT:
3117 		{
3118 			struct sctp_hmacalgo *shmac;
3119 			sctp_hmaclist_t *hmaclist;
3120 			uint16_t hmacid;
3121 			uint32_t i;
3122 
3123 			size_t found;
3124 
3125 			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
3126 			if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) {
3127 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3128 				error = EINVAL;
3129 				break;
3130 			}
3131 			hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents);
3132 			if (hmaclist == NULL) {
3133 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3134 				error = ENOMEM;
3135 				break;
3136 			}
3137 			for (i = 0; i < shmac->shmac_number_of_idents; i++) {
3138 				hmacid = shmac->shmac_idents[i];
3139 				if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
3140 					 /* invalid HMACs were found */ ;
3141 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3142 					error = EINVAL;
3143 					sctp_free_hmaclist(hmaclist);
3144 					goto sctp_set_hmac_done;
3145 				}
3146 			}
3147 			found = 0;
3148 			for (i = 0; i < hmaclist->num_algo; i++) {
3149 				if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
3150 					/* already in list */
3151 					found = 1;
3152 				}
3153 			}
3154 			if (!found) {
3155 				sctp_free_hmaclist(hmaclist);
3156 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3157 				error = EINVAL;
3158 				break;
3159 			}
3160 			/* set it on the endpoint */
3161 			SCTP_INP_WLOCK(inp);
3162 			if (inp->sctp_ep.local_hmacs)
3163 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3164 			inp->sctp_ep.local_hmacs = hmaclist;
3165 			SCTP_INP_WUNLOCK(inp);
3166 	sctp_set_hmac_done:
3167 			break;
3168 		}
3169 	case SCTP_AUTH_ACTIVE_KEY:
3170 		{
3171 			struct sctp_authkeyid *scact;
3172 
3173 			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid,
3174 			    optsize);
3175 			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3176 
3177 			/* set the active key on the right place */
3178 			if (stcb) {
3179 				/* set the active key on the assoc */
3180 				if (sctp_auth_setactivekey(stcb,
3181 				    scact->scact_keynumber)) {
3182 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3183 					    SCTP_FROM_SCTP_USRREQ,
3184 					    EINVAL);
3185 					error = EINVAL;
3186 				}
3187 				SCTP_TCB_UNLOCK(stcb);
3188 			} else {
3189 				/* set the active key on the endpoint */
3190 				SCTP_INP_WLOCK(inp);
3191 				if (sctp_auth_setactivekey_ep(inp,
3192 				    scact->scact_keynumber)) {
3193 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3194 					    SCTP_FROM_SCTP_USRREQ,
3195 					    EINVAL);
3196 					error = EINVAL;
3197 				}
3198 				SCTP_INP_WUNLOCK(inp);
3199 			}
3200 			break;
3201 		}
3202 	case SCTP_AUTH_DELETE_KEY:
3203 		{
3204 			struct sctp_authkeyid *scdel;
3205 
3206 			SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid,
3207 			    optsize);
3208 			SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3209 
3210 			/* delete the key from the right place */
3211 			if (stcb) {
3212 				if (sctp_delete_sharedkey(stcb,
3213 				    scdel->scact_keynumber)) {
3214 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3215 					    SCTP_FROM_SCTP_USRREQ,
3216 					    EINVAL);
3217 					error = EINVAL;
3218 				}
3219 				SCTP_TCB_UNLOCK(stcb);
3220 			} else {
3221 				SCTP_INP_WLOCK(inp);
3222 				if (sctp_delete_sharedkey_ep(inp,
3223 				    scdel->scact_keynumber)) {
3224 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3225 					    SCTP_FROM_SCTP_USRREQ,
3226 					    EINVAL);
3227 					error = EINVAL;
3228 				}
3229 				SCTP_INP_WUNLOCK(inp);
3230 			}
3231 			break;
3232 		}
3233 	case SCTP_AUTH_DEACTIVATE_KEY:
3234 		{
3235 			struct sctp_authkeyid *keyid;
3236 
3237 			SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid,
3238 			    optsize);
3239 			SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
3240 
3241 			/* deactivate the key from the right place */
3242 			if (stcb) {
3243 				if (sctp_deact_sharedkey(stcb,
3244 				    keyid->scact_keynumber)) {
3245 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3246 					    SCTP_FROM_SCTP_USRREQ,
3247 					    EINVAL);
3248 					error = EINVAL;
3249 				}
3250 				SCTP_TCB_UNLOCK(stcb);
3251 			} else {
3252 				SCTP_INP_WLOCK(inp);
3253 				if (sctp_deact_sharedkey_ep(inp,
3254 				    keyid->scact_keynumber)) {
3255 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3256 					    SCTP_FROM_SCTP_USRREQ,
3257 					    EINVAL);
3258 					error = EINVAL;
3259 				}
3260 				SCTP_INP_WUNLOCK(inp);
3261 			}
3262 			break;
3263 		}
3264 
3265 	case SCTP_RESET_STREAMS:
3266 		{
3267 			struct sctp_stream_reset *strrst;
3268 			uint8_t send_in = 0, send_tsn = 0, send_out = 0,
3269 			        addstream = 0;
3270 			uint16_t addstrmcnt = 0;
3271 			int i;
3272 
3273 			SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
3274 			SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
3275 
3276 			if (stcb == NULL) {
3277 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3278 				error = ENOENT;
3279 				break;
3280 			}
3281 			if (stcb->asoc.peer_supports_strreset == 0) {
3282 				/*
3283 				 * Peer does not support it, we return
3284 				 * protocol not supported since this is true
3285 				 * for this feature and this peer, not the
3286 				 * socket request in general.
3287 				 */
3288 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
3289 				error = EPROTONOSUPPORT;
3290 				SCTP_TCB_UNLOCK(stcb);
3291 				break;
3292 			}
3293 			if (stcb->asoc.stream_reset_outstanding) {
3294 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3295 				error = EALREADY;
3296 				SCTP_TCB_UNLOCK(stcb);
3297 				break;
3298 			}
3299 			if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
3300 				send_in = 1;
3301 			} else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
3302 				send_out = 1;
3303 			} else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
3304 				send_in = 1;
3305 				send_out = 1;
3306 			} else if (strrst->strrst_flags == SCTP_RESET_TSN) {
3307 				send_tsn = 1;
3308 			} else if (strrst->strrst_flags == SCTP_RESET_ADD_STREAMS) {
3309 				if (send_tsn ||
3310 				    send_in ||
3311 				    send_out) {
3312 					/* We can't do that and add streams */
3313 					error = EINVAL;
3314 					goto skip_stuff;
3315 				}
3316 				if (stcb->asoc.stream_reset_outstanding) {
3317 					error = EBUSY;
3318 					goto skip_stuff;
3319 				}
3320 				addstream = 1;
3321 				/* We allocate here */
3322 				addstrmcnt = strrst->strrst_num_streams;
3323 				if ((int)(addstrmcnt + stcb->asoc.streamoutcnt) > 0xffff) {
3324 					/* You can't have more than 64k */
3325 					error = EINVAL;
3326 					goto skip_stuff;
3327 				}
3328 				if ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < addstrmcnt) {
3329 					/* Need to allocate more */
3330 					struct sctp_stream_out *oldstream;
3331 					struct sctp_stream_queue_pending *sp;
3332 					int removed;
3333 
3334 					oldstream = stcb->asoc.strmout;
3335 					/* get some more */
3336 					SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
3337 					    ((stcb->asoc.streamoutcnt + addstrmcnt) * sizeof(struct sctp_stream_out)),
3338 					    SCTP_M_STRMO);
3339 					if (stcb->asoc.strmout == NULL) {
3340 						stcb->asoc.strmout = oldstream;
3341 						error = ENOMEM;
3342 						goto skip_stuff;
3343 					}
3344 					/*
3345 					 * Ok now we proceed with copying
3346 					 * the old out stuff and
3347 					 * initializing the new stuff.
3348 					 */
3349 					SCTP_TCB_SEND_LOCK(stcb);
3350 					for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3351 						TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3352 						stcb->asoc.strmout[i].next_sequence_sent = oldstream[i].next_sequence_sent;
3353 						stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
3354 						stcb->asoc.strmout[i].stream_no = i;
3355 						if (oldstream[i].next_spoke.tqe_next) {
3356 							sctp_remove_from_wheel(stcb, &stcb->asoc, &oldstream[i], 1);
3357 							stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3358 							stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3359 							removed = 1;
3360 						} else {
3361 							/* not on out wheel */
3362 							stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3363 							stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3364 							removed = 0;
3365 						}
3366 						/*
3367 						 * now anything on those
3368 						 * queues?
3369 						 */
3370 						while (TAILQ_EMPTY(&oldstream[i].outqueue) == 0) {
3371 							sp = TAILQ_FIRST(&oldstream[i].outqueue);
3372 							TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
3373 							TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
3374 						}
3375 						/* Did we disrupt the wheel? */
3376 						if (removed) {
3377 							sctp_insert_on_wheel(stcb,
3378 							    &stcb->asoc,
3379 							    &stcb->asoc.strmout[i],
3380 							    1);
3381 						}
3382 						/*
3383 						 * Now move assoc pointers
3384 						 * too
3385 						 */
3386 						if (stcb->asoc.last_out_stream == &oldstream[i]) {
3387 							stcb->asoc.last_out_stream = &stcb->asoc.strmout[i];
3388 						}
3389 						if (stcb->asoc.locked_on_sending == &oldstream[i]) {
3390 							stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i];
3391 						}
3392 					}
3393 					/* now the new streams */
3394 					for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + addstrmcnt); i++) {
3395 						stcb->asoc.strmout[i].next_sequence_sent = 0x0;
3396 						TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3397 						stcb->asoc.strmout[i].stream_no = i;
3398 						stcb->asoc.strmout[i].last_msg_incomplete = 0;
3399 						stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3400 						stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3401 					}
3402 					stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + addstrmcnt;
3403 					SCTP_FREE(oldstream, SCTP_M_STRMO);
3404 				}
3405 				SCTP_TCB_SEND_UNLOCK(stcb);
3406 				goto skip_stuff;
3407 			} else {
3408 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3409 				error = EINVAL;
3410 				SCTP_TCB_UNLOCK(stcb);
3411 				break;
3412 			}
3413 			for (i = 0; i < strrst->strrst_num_streams; i++) {
3414 				if ((send_in) &&
3415 
3416 				    (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
3417 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3418 					error = EINVAL;
3419 					goto get_out;
3420 				}
3421 				if ((send_out) &&
3422 				    (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
3423 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3424 					error = EINVAL;
3425 					goto get_out;
3426 				}
3427 			}
3428 	skip_stuff:
3429 			if (error) {
3430 		get_out:
3431 				SCTP_TCB_UNLOCK(stcb);
3432 				break;
3433 			}
3434 			error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
3435 			    strrst->strrst_list,
3436 			    send_out, (stcb->asoc.str_reset_seq_in - 3),
3437 			    send_in, send_tsn, addstream, addstrmcnt);
3438 
3439 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
3440 			SCTP_TCB_UNLOCK(stcb);
3441 		}
3442 		break;
3443 
3444 	case SCTP_CONNECT_X:
3445 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3446 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3447 			error = EINVAL;
3448 			break;
3449 		}
3450 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
3451 		break;
3452 
3453 	case SCTP_CONNECT_X_DELAYED:
3454 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3455 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3456 			error = EINVAL;
3457 			break;
3458 		}
3459 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
3460 		break;
3461 
3462 	case SCTP_CONNECT_X_COMPLETE:
3463 		{
3464 			struct sockaddr *sa;
3465 			struct sctp_nets *net;
3466 
3467 			/* FIXME MT: check correct? */
3468 			SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
3469 
3470 			/* find tcb */
3471 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3472 				SCTP_INP_RLOCK(inp);
3473 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
3474 				if (stcb) {
3475 					SCTP_TCB_LOCK(stcb);
3476 					net = sctp_findnet(stcb, sa);
3477 				}
3478 				SCTP_INP_RUNLOCK(inp);
3479 			} else {
3480 				/*
3481 				 * We increment here since
3482 				 * sctp_findassociation_ep_addr() wil do a
3483 				 * decrement if it finds the stcb as long as
3484 				 * the locked tcb (last argument) is NOT a
3485 				 * TCB.. aka NULL.
3486 				 */
3487 				SCTP_INP_INCR_REF(inp);
3488 				stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3489 				if (stcb == NULL) {
3490 					SCTP_INP_DECR_REF(inp);
3491 				}
3492 			}
3493 
3494 			if (stcb == NULL) {
3495 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3496 				error = ENOENT;
3497 				break;
3498 			}
3499 			if (stcb->asoc.delayed_connection == 1) {
3500 				stcb->asoc.delayed_connection = 0;
3501 				(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3502 				sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3503 				    stcb->asoc.primary_destination,
3504 				    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3505 				sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
3506 			} else {
3507 				/*
3508 				 * already expired or did not use delayed
3509 				 * connectx
3510 				 */
3511 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3512 				error = EALREADY;
3513 			}
3514 			SCTP_TCB_UNLOCK(stcb);
3515 		}
3516 		break;
3517 	case SCTP_MAX_BURST:
3518 		{
3519 			uint8_t *burst;
3520 
3521 			SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3522 
3523 			SCTP_INP_WLOCK(inp);
3524 			if (*burst) {
3525 				inp->sctp_ep.max_burst = *burst;
3526 			}
3527 			SCTP_INP_WUNLOCK(inp);
3528 		}
3529 		break;
3530 	case SCTP_MAXSEG:
3531 		{
3532 			struct sctp_assoc_value *av;
3533 			int ovh;
3534 
3535 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3536 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3537 
3538 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3539 				ovh = SCTP_MED_OVERHEAD;
3540 			} else {
3541 				ovh = SCTP_MED_V4_OVERHEAD;
3542 			}
3543 			if (stcb) {
3544 				if (av->assoc_value) {
3545 					stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3546 				} else {
3547 					stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3548 				}
3549 				SCTP_TCB_UNLOCK(stcb);
3550 			} else {
3551 				SCTP_INP_WLOCK(inp);
3552 				/*
3553 				 * FIXME MT: I think this is not in tune
3554 				 * with the API ID
3555 				 */
3556 				if (av->assoc_value) {
3557 					inp->sctp_frag_point = (av->assoc_value + ovh);
3558 				} else {
3559 					inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3560 				}
3561 				SCTP_INP_WUNLOCK(inp);
3562 			}
3563 		}
3564 		break;
3565 	case SCTP_EVENTS:
3566 		{
3567 			struct sctp_event_subscribe *events;
3568 
3569 			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3570 
3571 			SCTP_INP_WLOCK(inp);
3572 			if (events->sctp_data_io_event) {
3573 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3574 			} else {
3575 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3576 			}
3577 
3578 			if (events->sctp_association_event) {
3579 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3580 			} else {
3581 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3582 			}
3583 
3584 			if (events->sctp_address_event) {
3585 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3586 			} else {
3587 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3588 			}
3589 
3590 			if (events->sctp_send_failure_event) {
3591 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3592 			} else {
3593 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3594 			}
3595 
3596 			if (events->sctp_peer_error_event) {
3597 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3598 			} else {
3599 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3600 			}
3601 
3602 			if (events->sctp_shutdown_event) {
3603 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3604 			} else {
3605 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3606 			}
3607 
3608 			if (events->sctp_partial_delivery_event) {
3609 				sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3610 			} else {
3611 				sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3612 			}
3613 
3614 			if (events->sctp_adaptation_layer_event) {
3615 				sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3616 			} else {
3617 				sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3618 			}
3619 
3620 			if (events->sctp_authentication_event) {
3621 				sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3622 			} else {
3623 				sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3624 			}
3625 
3626 			if (events->sctp_sender_dry_event) {
3627 				sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
3628 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3629 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3630 					stcb = LIST_FIRST(&inp->sctp_asoc_list);
3631 					if (stcb) {
3632 						SCTP_TCB_LOCK(stcb);
3633 					}
3634 					if (stcb &&
3635 					    TAILQ_EMPTY(&stcb->asoc.send_queue) &&
3636 					    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
3637 					    (stcb->asoc.stream_queue_cnt == 0)) {
3638 						sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
3639 					}
3640 					if (stcb) {
3641 						SCTP_TCB_UNLOCK(stcb);
3642 					}
3643 				}
3644 			} else {
3645 				sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
3646 			}
3647 
3648 			if (events->sctp_stream_reset_events) {
3649 				sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3650 			} else {
3651 				sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3652 			}
3653 			SCTP_INP_WUNLOCK(inp);
3654 		}
3655 		break;
3656 
3657 	case SCTP_ADAPTATION_LAYER:
3658 		{
3659 			struct sctp_setadaptation *adap_bits;
3660 
3661 			SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3662 			SCTP_INP_WLOCK(inp);
3663 			inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3664 			SCTP_INP_WUNLOCK(inp);
3665 		}
3666 		break;
3667 #ifdef SCTP_DEBUG
3668 	case SCTP_SET_INITIAL_DBG_SEQ:
3669 		{
3670 			uint32_t *vvv;
3671 
3672 			SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3673 			SCTP_INP_WLOCK(inp);
3674 			inp->sctp_ep.initial_sequence_debug = *vvv;
3675 			SCTP_INP_WUNLOCK(inp);
3676 		}
3677 		break;
3678 #endif
3679 	case SCTP_DEFAULT_SEND_PARAM:
3680 		{
3681 			struct sctp_sndrcvinfo *s_info;
3682 
3683 			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3684 			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3685 
3686 			if (stcb) {
3687 				if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3688 					memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3689 				} else {
3690 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3691 					error = EINVAL;
3692 				}
3693 				SCTP_TCB_UNLOCK(stcb);
3694 			} else {
3695 				SCTP_INP_WLOCK(inp);
3696 				memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3697 				SCTP_INP_WUNLOCK(inp);
3698 			}
3699 		}
3700 		break;
3701 	case SCTP_PEER_ADDR_PARAMS:
3702 		/* Applys to the specific association */
3703 		{
3704 			struct sctp_paddrparams *paddrp;
3705 			struct sctp_nets *net;
3706 
3707 			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3708 			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3709 			net = NULL;
3710 			if (stcb) {
3711 				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3712 			} else {
3713 				/*
3714 				 * We increment here since
3715 				 * sctp_findassociation_ep_addr() wil do a
3716 				 * decrement if it finds the stcb as long as
3717 				 * the locked tcb (last argument) is NOT a
3718 				 * TCB.. aka NULL.
3719 				 */
3720 				SCTP_INP_INCR_REF(inp);
3721 				stcb = sctp_findassociation_ep_addr(&inp,
3722 				    (struct sockaddr *)&paddrp->spp_address,
3723 				    &net, NULL, NULL);
3724 				if (stcb == NULL) {
3725 					SCTP_INP_DECR_REF(inp);
3726 				}
3727 			}
3728 			if (stcb && (net == NULL)) {
3729 				struct sockaddr *sa;
3730 
3731 				sa = (struct sockaddr *)&paddrp->spp_address;
3732 				if (sa->sa_family == AF_INET) {
3733 					struct sockaddr_in *sin;
3734 
3735 					sin = (struct sockaddr_in *)sa;
3736 					if (sin->sin_addr.s_addr) {
3737 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3738 						SCTP_TCB_UNLOCK(stcb);
3739 						error = EINVAL;
3740 						break;
3741 					}
3742 				} else if (sa->sa_family == AF_INET6) {
3743 					struct sockaddr_in6 *sin6;
3744 
3745 					sin6 = (struct sockaddr_in6 *)sa;
3746 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3747 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3748 						SCTP_TCB_UNLOCK(stcb);
3749 						error = EINVAL;
3750 						break;
3751 					}
3752 				} else {
3753 					error = EAFNOSUPPORT;
3754 					SCTP_TCB_UNLOCK(stcb);
3755 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3756 					break;
3757 				}
3758 			}
3759 			/* sanity checks */
3760 			if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3761 				if (stcb)
3762 					SCTP_TCB_UNLOCK(stcb);
3763 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3764 				return (EINVAL);
3765 			}
3766 			if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3767 				if (stcb)
3768 					SCTP_TCB_UNLOCK(stcb);
3769 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3770 				return (EINVAL);
3771 			}
3772 			if (stcb) {
3773 				/************************TCB SPECIFIC SET ******************/
3774 				/*
3775 				 * do we change the timer for HB, we run
3776 				 * only one?
3777 				 */
3778 				int ovh = 0;
3779 
3780 				if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3781 					ovh = SCTP_MED_OVERHEAD;
3782 				} else {
3783 					ovh = SCTP_MED_V4_OVERHEAD;
3784 				}
3785 
3786 				if (paddrp->spp_hbinterval)
3787 					stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3788 				else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3789 					stcb->asoc.heart_beat_delay = 0;
3790 
3791 				/* network sets ? */
3792 				if (net) {
3793 					/************************NET SPECIFIC SET ******************/
3794 					if (paddrp->spp_flags & SPP_HB_DEMAND) {
3795 						/* on demand HB */
3796 						if (sctp_send_hb(stcb, 1, net) < 0) {
3797 							/* asoc destroyed */
3798 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3799 							error = EINVAL;
3800 							break;
3801 						}
3802 					}
3803 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3804 						net->dest_state |= SCTP_ADDR_NOHB;
3805 					}
3806 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3807 						net->dest_state &= ~SCTP_ADDR_NOHB;
3808 					}
3809 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3810 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3811 							sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3812 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3813 						}
3814 						if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3815 							net->mtu = paddrp->spp_pathmtu + ovh;
3816 							if (net->mtu < stcb->asoc.smallest_mtu) {
3817 #ifdef SCTP_PRINT_FOR_B_AND_M
3818 								SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3819 								    net->mtu);
3820 #endif
3821 								sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3822 							}
3823 						}
3824 					}
3825 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3826 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3827 							sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3828 						}
3829 					}
3830 					if (paddrp->spp_pathmaxrxt)
3831 						net->failure_threshold = paddrp->spp_pathmaxrxt;
3832 #ifdef INET
3833 					if (paddrp->spp_flags & SPP_IPV4_TOS) {
3834 						if (net->ro._l_addr.sin.sin_family == AF_INET) {
3835 							net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3836 						}
3837 					}
3838 #endif
3839 #ifdef INET6
3840 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3841 						if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3842 							net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3843 						}
3844 					}
3845 #endif
3846 				} else {
3847 					/************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3848 					if (paddrp->spp_pathmaxrxt)
3849 						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3850 
3851 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3852 						/* Turn back on the timer */
3853 						stcb->asoc.hb_is_disabled = 0;
3854 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3855 					}
3856 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3857 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3858 							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3859 								sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3860 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3861 							}
3862 							if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3863 								net->mtu = paddrp->spp_pathmtu + ovh;
3864 								if (net->mtu < stcb->asoc.smallest_mtu) {
3865 #ifdef SCTP_PRINT_FOR_B_AND_M
3866 									SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3867 									    net->mtu);
3868 #endif
3869 									sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3870 								}
3871 							}
3872 						}
3873 					}
3874 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3875 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3876 							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3877 								sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3878 							}
3879 						}
3880 					}
3881 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3882 						int cnt_of_unconf = 0;
3883 						struct sctp_nets *lnet;
3884 
3885 						stcb->asoc.hb_is_disabled = 1;
3886 						TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3887 							if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3888 								cnt_of_unconf++;
3889 							}
3890 						}
3891 						/*
3892 						 * stop the timer ONLY if we
3893 						 * have no unconfirmed
3894 						 * addresses
3895 						 */
3896 						if (cnt_of_unconf == 0) {
3897 							TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3898 								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3899 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3900 							}
3901 						}
3902 					}
3903 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3904 						/* start up the timer. */
3905 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3906 							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3907 						}
3908 					}
3909 #ifdef INET
3910 					if (paddrp->spp_flags & SPP_IPV4_TOS)
3911 						stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3912 #endif
3913 #ifdef INET6
3914 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3915 						stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3916 #endif
3917 
3918 				}
3919 				SCTP_TCB_UNLOCK(stcb);
3920 			} else {
3921 				/************************NO TCB, SET TO default stuff ******************/
3922 				SCTP_INP_WLOCK(inp);
3923 				/*
3924 				 * For the TOS/FLOWLABEL stuff you set it
3925 				 * with the options on the socket
3926 				 */
3927 				if (paddrp->spp_pathmaxrxt) {
3928 					inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3929 				}
3930 				if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3931 					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3932 				else if (paddrp->spp_hbinterval) {
3933 					if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3934 						paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
3935 					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3936 				}
3937 				if (paddrp->spp_flags & SPP_HB_ENABLE) {
3938 					sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3939 
3940 				} else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3941 					sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3942 				}
3943 				SCTP_INP_WUNLOCK(inp);
3944 			}
3945 		}
3946 		break;
3947 	case SCTP_RTOINFO:
3948 		{
3949 			struct sctp_rtoinfo *srto;
3950 			uint32_t new_init, new_min, new_max;
3951 
3952 			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3953 			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3954 
3955 			if (stcb) {
3956 				if (srto->srto_initial)
3957 					new_init = srto->srto_initial;
3958 				else
3959 					new_init = stcb->asoc.initial_rto;
3960 				if (srto->srto_max)
3961 					new_max = srto->srto_max;
3962 				else
3963 					new_max = stcb->asoc.maxrto;
3964 				if (srto->srto_min)
3965 					new_min = srto->srto_min;
3966 				else
3967 					new_min = stcb->asoc.minrto;
3968 				if ((new_min <= new_init) && (new_init <= new_max)) {
3969 					stcb->asoc.initial_rto = new_init;
3970 					stcb->asoc.maxrto = new_max;
3971 					stcb->asoc.minrto = new_min;
3972 				} else {
3973 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3974 					error = EINVAL;
3975 				}
3976 				SCTP_TCB_UNLOCK(stcb);
3977 			} else {
3978 				SCTP_INP_WLOCK(inp);
3979 				if (srto->srto_initial)
3980 					new_init = srto->srto_initial;
3981 				else
3982 					new_init = inp->sctp_ep.initial_rto;
3983 				if (srto->srto_max)
3984 					new_max = srto->srto_max;
3985 				else
3986 					new_max = inp->sctp_ep.sctp_maxrto;
3987 				if (srto->srto_min)
3988 					new_min = srto->srto_min;
3989 				else
3990 					new_min = inp->sctp_ep.sctp_minrto;
3991 				if ((new_min <= new_init) && (new_init <= new_max)) {
3992 					inp->sctp_ep.initial_rto = new_init;
3993 					inp->sctp_ep.sctp_maxrto = new_max;
3994 					inp->sctp_ep.sctp_minrto = new_min;
3995 				} else {
3996 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3997 					error = EINVAL;
3998 				}
3999 				SCTP_INP_WUNLOCK(inp);
4000 			}
4001 		}
4002 		break;
4003 	case SCTP_ASSOCINFO:
4004 		{
4005 			struct sctp_assocparams *sasoc;
4006 
4007 			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
4008 			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
4009 			if (sasoc->sasoc_cookie_life) {
4010 				/* boundary check the cookie life */
4011 				if (sasoc->sasoc_cookie_life < 1000)
4012 					sasoc->sasoc_cookie_life = 1000;
4013 				if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
4014 					sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
4015 				}
4016 			}
4017 			if (stcb) {
4018 				if (sasoc->sasoc_asocmaxrxt)
4019 					stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
4020 				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
4021 				sasoc->sasoc_peer_rwnd = 0;
4022 				sasoc->sasoc_local_rwnd = 0;
4023 				if (sasoc->sasoc_cookie_life) {
4024 					stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4025 				}
4026 				SCTP_TCB_UNLOCK(stcb);
4027 			} else {
4028 				SCTP_INP_WLOCK(inp);
4029 				if (sasoc->sasoc_asocmaxrxt)
4030 					inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
4031 				sasoc->sasoc_number_peer_destinations = 0;
4032 				sasoc->sasoc_peer_rwnd = 0;
4033 				sasoc->sasoc_local_rwnd = 0;
4034 				if (sasoc->sasoc_cookie_life) {
4035 					inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4036 				}
4037 				SCTP_INP_WUNLOCK(inp);
4038 			}
4039 		}
4040 		break;
4041 	case SCTP_INITMSG:
4042 		{
4043 			struct sctp_initmsg *sinit;
4044 
4045 			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
4046 			SCTP_INP_WLOCK(inp);
4047 			if (sinit->sinit_num_ostreams)
4048 				inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
4049 
4050 			if (sinit->sinit_max_instreams)
4051 				inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
4052 
4053 			if (sinit->sinit_max_attempts)
4054 				inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
4055 
4056 			if (sinit->sinit_max_init_timeo)
4057 				inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
4058 			SCTP_INP_WUNLOCK(inp);
4059 		}
4060 		break;
4061 	case SCTP_PRIMARY_ADDR:
4062 		{
4063 			struct sctp_setprim *spa;
4064 			struct sctp_nets *net, *lnet;
4065 
4066 			SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
4067 			SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
4068 
4069 			net = NULL;
4070 			if (stcb) {
4071 				net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
4072 			} else {
4073 				/*
4074 				 * We increment here since
4075 				 * sctp_findassociation_ep_addr() wil do a
4076 				 * decrement if it finds the stcb as long as
4077 				 * the locked tcb (last argument) is NOT a
4078 				 * TCB.. aka NULL.
4079 				 */
4080 				SCTP_INP_INCR_REF(inp);
4081 				stcb = sctp_findassociation_ep_addr(&inp,
4082 				    (struct sockaddr *)&spa->ssp_addr,
4083 				    &net, NULL, NULL);
4084 				if (stcb == NULL) {
4085 					SCTP_INP_DECR_REF(inp);
4086 				}
4087 			}
4088 
4089 			if ((stcb) && (net)) {
4090 				if ((net != stcb->asoc.primary_destination) &&
4091 				    (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
4092 					/* Ok we need to set it */
4093 					lnet = stcb->asoc.primary_destination;
4094 					if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
4095 						if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
4096 							net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
4097 						}
4098 						net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
4099 					}
4100 				}
4101 			} else {
4102 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4103 				error = EINVAL;
4104 			}
4105 			if (stcb) {
4106 				SCTP_TCB_UNLOCK(stcb);
4107 			}
4108 		}
4109 		break;
4110 	case SCTP_SET_DYNAMIC_PRIMARY:
4111 		{
4112 			union sctp_sockstore *ss;
4113 
4114 			error = priv_check(curthread,
4115 			    PRIV_NETINET_RESERVEDPORT);
4116 			if (error)
4117 				break;
4118 
4119 			SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
4120 			/* SUPER USER CHECK? */
4121 			error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
4122 		}
4123 		break;
4124 	case SCTP_SET_PEER_PRIMARY_ADDR:
4125 		{
4126 			struct sctp_setpeerprim *sspp;
4127 
4128 			SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
4129 			SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
4130 			if (stcb != NULL) {
4131 				struct sctp_ifa *ifa;
4132 
4133 				ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
4134 				    stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
4135 				if (ifa == NULL) {
4136 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4137 					error = EINVAL;
4138 					goto out_of_it;
4139 				}
4140 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4141 					/*
4142 					 * Must validate the ifa found is in
4143 					 * our ep
4144 					 */
4145 					struct sctp_laddr *laddr;
4146 					int found = 0;
4147 
4148 					LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4149 						if (laddr->ifa == NULL) {
4150 							SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4151 							    __FUNCTION__);
4152 							continue;
4153 						}
4154 						if (laddr->ifa == ifa) {
4155 							found = 1;
4156 							break;
4157 						}
4158 					}
4159 					if (!found) {
4160 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4161 						error = EINVAL;
4162 						goto out_of_it;
4163 					}
4164 				}
4165 				if (sctp_set_primary_ip_address_sa(stcb,
4166 				    (struct sockaddr *)&sspp->sspp_addr) != 0) {
4167 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4168 					error = EINVAL;
4169 				}
4170 		out_of_it:
4171 				SCTP_TCB_UNLOCK(stcb);
4172 			} else {
4173 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4174 				error = EINVAL;
4175 			}
4176 
4177 		}
4178 		break;
4179 	case SCTP_BINDX_ADD_ADDR:
4180 		{
4181 			struct sctp_getaddresses *addrs;
4182 			size_t sz;
4183 			struct thread *td;
4184 
4185 			td = (struct thread *)p;
4186 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
4187 			    optsize);
4188 			if (addrs->addr->sa_family == AF_INET) {
4189 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4190 				if (optsize < sz) {
4191 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4192 					error = EINVAL;
4193 					break;
4194 				}
4195 				if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4196 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4197 					break;
4198 				}
4199 #ifdef INET6
4200 			} else if (addrs->addr->sa_family == AF_INET6) {
4201 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4202 				if (optsize < sz) {
4203 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4204 					error = EINVAL;
4205 					break;
4206 				}
4207 				if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4208 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4209 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4210 					break;
4211 				}
4212 #endif
4213 			} else {
4214 				error = EAFNOSUPPORT;
4215 				break;
4216 			}
4217 			sctp_bindx_add_address(so, inp, addrs->addr,
4218 			    addrs->sget_assoc_id, vrf_id,
4219 			    &error, p);
4220 		}
4221 		break;
4222 	case SCTP_BINDX_REM_ADDR:
4223 		{
4224 			struct sctp_getaddresses *addrs;
4225 			size_t sz;
4226 			struct thread *td;
4227 
4228 			td = (struct thread *)p;
4229 
4230 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
4231 			if (addrs->addr->sa_family == AF_INET) {
4232 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4233 				if (optsize < sz) {
4234 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4235 					error = EINVAL;
4236 					break;
4237 				}
4238 				if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4239 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4240 					break;
4241 				}
4242 #ifdef INET6
4243 			} else if (addrs->addr->sa_family == AF_INET6) {
4244 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4245 				if (optsize < sz) {
4246 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4247 					error = EINVAL;
4248 					break;
4249 				}
4250 				if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4251 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4252 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4253 					break;
4254 				}
4255 #endif
4256 			} else {
4257 				error = EAFNOSUPPORT;
4258 				break;
4259 			}
4260 			sctp_bindx_delete_address(so, inp, addrs->addr,
4261 			    addrs->sget_assoc_id, vrf_id,
4262 			    &error);
4263 		}
4264 		break;
4265 	default:
4266 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
4267 		error = ENOPROTOOPT;
4268 		break;
4269 	}			/* end switch (opt) */
4270 	return (error);
4271 }
4272 
4273 int
4274 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
4275 {
4276 	void *optval = NULL;
4277 	size_t optsize = 0;
4278 	struct sctp_inpcb *inp;
4279 	void *p;
4280 	int error = 0;
4281 
4282 	inp = (struct sctp_inpcb *)so->so_pcb;
4283 	if (inp == 0) {
4284 		/* I made the same as TCP since we are not setup? */
4285 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4286 		return (ECONNRESET);
4287 	}
4288 	if (sopt->sopt_level != IPPROTO_SCTP) {
4289 		/* wrong proto level... send back up to IP */
4290 #ifdef INET6
4291 		if (INP_CHECK_SOCKAF(so, AF_INET6))
4292 			error = ip6_ctloutput(so, sopt);
4293 		else
4294 #endif				/* INET6 */
4295 			error = ip_ctloutput(so, sopt);
4296 		return (error);
4297 	}
4298 	optsize = sopt->sopt_valsize;
4299 	if (optsize) {
4300 		SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
4301 		if (optval == NULL) {
4302 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
4303 			return (ENOBUFS);
4304 		}
4305 		error = sooptcopyin(sopt, optval, optsize, optsize);
4306 		if (error) {
4307 			SCTP_FREE(optval, SCTP_M_SOCKOPT);
4308 			goto out;
4309 		}
4310 	}
4311 	p = (void *)sopt->sopt_td;
4312 	if (sopt->sopt_dir == SOPT_SET) {
4313 		error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
4314 	} else if (sopt->sopt_dir == SOPT_GET) {
4315 		error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
4316 	} else {
4317 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4318 		error = EINVAL;
4319 	}
4320 	if ((error == 0) && (optval != NULL)) {
4321 		error = sooptcopyout(sopt, optval, optsize);
4322 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
4323 	} else if (optval != NULL) {
4324 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
4325 	}
4326 out:
4327 	return (error);
4328 }
4329 
4330 
4331 static int
4332 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
4333 {
4334 	int error = 0;
4335 	int create_lock_on = 0;
4336 	uint32_t vrf_id;
4337 	struct sctp_inpcb *inp;
4338 	struct sctp_tcb *stcb = NULL;
4339 
4340 	inp = (struct sctp_inpcb *)so->so_pcb;
4341 	if (inp == 0) {
4342 		/* I made the same as TCP since we are not setup? */
4343 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4344 		return (ECONNRESET);
4345 	}
4346 	if (addr == NULL) {
4347 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4348 		return EINVAL;
4349 	}
4350 #ifdef INET6
4351 	if (addr->sa_family == AF_INET6) {
4352 		struct sockaddr_in6 *sin6p;
4353 
4354 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
4355 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4356 			return (EINVAL);
4357 		}
4358 		sin6p = (struct sockaddr_in6 *)addr;
4359 		if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
4360 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4361 			return (error);
4362 		}
4363 	} else
4364 #endif
4365 	if (addr->sa_family == AF_INET) {
4366 		struct sockaddr_in *sinp;
4367 
4368 		if (addr->sa_len != sizeof(struct sockaddr_in)) {
4369 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4370 			return (EINVAL);
4371 		}
4372 		sinp = (struct sockaddr_in *)addr;
4373 		if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
4374 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4375 			return (error);
4376 		}
4377 	} else {
4378 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
4379 		return (EAFNOSUPPORT);
4380 	}
4381 	SCTP_INP_INCR_REF(inp);
4382 	SCTP_ASOC_CREATE_LOCK(inp);
4383 	create_lock_on = 1;
4384 
4385 
4386 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4387 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4388 		/* Should I really unlock ? */
4389 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
4390 		error = EFAULT;
4391 		goto out_now;
4392 	}
4393 #ifdef INET6
4394 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
4395 	    (addr->sa_family == AF_INET6)) {
4396 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4397 		error = EINVAL;
4398 		goto out_now;
4399 	}
4400 #endif				/* INET6 */
4401 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
4402 	    SCTP_PCB_FLAGS_UNBOUND) {
4403 		/* Bind a ephemeral port */
4404 		error = sctp_inpcb_bind(so, NULL, NULL, p);
4405 		if (error) {
4406 			goto out_now;
4407 		}
4408 	}
4409 	/* Now do we connect? */
4410 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4411 	    (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
4412 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4413 		error = EINVAL;
4414 		goto out_now;
4415 	}
4416 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4417 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4418 		/* We are already connected AND the TCP model */
4419 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4420 		error = EADDRINUSE;
4421 		goto out_now;
4422 	}
4423 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4424 		SCTP_INP_RLOCK(inp);
4425 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
4426 		SCTP_INP_RUNLOCK(inp);
4427 	} else {
4428 		/*
4429 		 * We increment here since sctp_findassociation_ep_addr()
4430 		 * will do a decrement if it finds the stcb as long as the
4431 		 * locked tcb (last argument) is NOT a TCB.. aka NULL.
4432 		 */
4433 		SCTP_INP_INCR_REF(inp);
4434 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
4435 		if (stcb == NULL) {
4436 			SCTP_INP_DECR_REF(inp);
4437 		} else {
4438 			SCTP_TCB_UNLOCK(stcb);
4439 		}
4440 	}
4441 	if (stcb != NULL) {
4442 		/* Already have or am bring up an association */
4443 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4444 		error = EALREADY;
4445 		goto out_now;
4446 	}
4447 	vrf_id = inp->def_vrf_id;
4448 	/* We are GOOD to go */
4449 	stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
4450 	if (stcb == NULL) {
4451 		/* Gak! no memory */
4452 		goto out_now;
4453 	}
4454 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
4455 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4456 		/* Set the connected flag so we can queue data */
4457 		soisconnecting(so);
4458 	}
4459 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
4460 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4461 
4462 	/* initialize authentication parameters for the assoc */
4463 	sctp_initialize_auth_params(inp, stcb);
4464 
4465 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4466 	SCTP_TCB_UNLOCK(stcb);
4467 out_now:
4468 	if (create_lock_on) {
4469 		SCTP_ASOC_CREATE_UNLOCK(inp);
4470 	}
4471 	SCTP_INP_DECR_REF(inp);
4472 	return error;
4473 }
4474 
4475 int
4476 sctp_listen(struct socket *so, int backlog, struct thread *p)
4477 {
4478 	/*
4479 	 * Note this module depends on the protocol processing being called
4480 	 * AFTER any socket level flags and backlog are applied to the
4481 	 * socket. The traditional way that the socket flags are applied is
4482 	 * AFTER protocol processing. We have made a change to the
4483 	 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
4484 	 * place if the socket API for SCTP is to work properly.
4485 	 */
4486 
4487 	int error = 0;
4488 	struct sctp_inpcb *inp;
4489 
4490 	inp = (struct sctp_inpcb *)so->so_pcb;
4491 	if (inp == 0) {
4492 		/* I made the same as TCP since we are not setup? */
4493 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4494 		return (ECONNRESET);
4495 	}
4496 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
4497 		/* See if we have a listener */
4498 		struct sctp_inpcb *tinp;
4499 		union sctp_sockstore store, *sp;
4500 
4501 		sp = &store;
4502 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4503 			/* not bound all */
4504 			struct sctp_laddr *laddr;
4505 
4506 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4507 				memcpy(&store, &laddr->ifa->address, sizeof(store));
4508 				sp->sin.sin_port = inp->sctp_lport;
4509 				tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4510 				if (tinp && (tinp != inp) &&
4511 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4512 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4513 				    (tinp->sctp_socket->so_qlimit)) {
4514 					/*
4515 					 * we have a listener already and
4516 					 * its not this inp.
4517 					 */
4518 					SCTP_INP_DECR_REF(tinp);
4519 					return (EADDRINUSE);
4520 				} else if (tinp) {
4521 					SCTP_INP_DECR_REF(tinp);
4522 				}
4523 			}
4524 		} else {
4525 			/* Setup a local addr bound all */
4526 			memset(&store, 0, sizeof(store));
4527 			store.sin.sin_port = inp->sctp_lport;
4528 #ifdef INET6
4529 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4530 				store.sa.sa_family = AF_INET6;
4531 				store.sa.sa_len = sizeof(struct sockaddr_in6);
4532 			}
4533 #endif
4534 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
4535 				store.sa.sa_family = AF_INET;
4536 				store.sa.sa_len = sizeof(struct sockaddr_in);
4537 			}
4538 			tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4539 			if (tinp && (tinp != inp) &&
4540 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4541 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4542 			    (tinp->sctp_socket->so_qlimit)) {
4543 				/*
4544 				 * we have a listener already and its not
4545 				 * this inp.
4546 				 */
4547 				SCTP_INP_DECR_REF(tinp);
4548 				return (EADDRINUSE);
4549 			} else if (tinp) {
4550 				SCTP_INP_DECR_REF(inp);
4551 			}
4552 		}
4553 	}
4554 	SCTP_INP_RLOCK(inp);
4555 #ifdef SCTP_LOCK_LOGGING
4556 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
4557 		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
4558 	}
4559 #endif
4560 	SOCK_LOCK(so);
4561 	error = solisten_proto_check(so);
4562 	if (error) {
4563 		SOCK_UNLOCK(so);
4564 		SCTP_INP_RUNLOCK(inp);
4565 		return (error);
4566 	}
4567 	if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
4568 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4569 		/*
4570 		 * The unlucky case - We are in the tcp pool with this guy.
4571 		 * - Someone else is in the main inp slot. - We must move
4572 		 * this guy (the listener) to the main slot - We must then
4573 		 * move the guy that was listener to the TCP Pool.
4574 		 */
4575 		if (sctp_swap_inpcb_for_listen(inp)) {
4576 			goto in_use;
4577 		}
4578 	}
4579 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4580 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4581 		/* We are already connected AND the TCP model */
4582 in_use:
4583 		SCTP_INP_RUNLOCK(inp);
4584 		SOCK_UNLOCK(so);
4585 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4586 		return (EADDRINUSE);
4587 	}
4588 	SCTP_INP_RUNLOCK(inp);
4589 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4590 		/* We must do a bind. */
4591 		SOCK_UNLOCK(so);
4592 		if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
4593 			/* bind error, probably perm */
4594 			return (error);
4595 		}
4596 		SOCK_LOCK(so);
4597 	}
4598 	/* It appears for 7.0 and on, we must always call this. */
4599 	solisten_proto(so, backlog);
4600 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4601 		/* remove the ACCEPTCONN flag for one-to-many sockets */
4602 		so->so_options &= ~SO_ACCEPTCONN;
4603 	}
4604 	if (backlog == 0) {
4605 		/* turning off listen */
4606 		so->so_options &= ~SO_ACCEPTCONN;
4607 	}
4608 	SOCK_UNLOCK(so);
4609 	return (error);
4610 }
4611 
4612 static int sctp_defered_wakeup_cnt = 0;
4613 
4614 int
4615 sctp_accept(struct socket *so, struct sockaddr **addr)
4616 {
4617 	struct sctp_tcb *stcb;
4618 	struct sctp_inpcb *inp;
4619 	union sctp_sockstore store;
4620 
4621 #ifdef INET6
4622 	int error;
4623 
4624 #endif
4625 	inp = (struct sctp_inpcb *)so->so_pcb;
4626 
4627 	if (inp == 0) {
4628 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4629 		return (ECONNRESET);
4630 	}
4631 	SCTP_INP_RLOCK(inp);
4632 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4633 		SCTP_INP_RUNLOCK(inp);
4634 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4635 		return (EOPNOTSUPP);
4636 	}
4637 	if (so->so_state & SS_ISDISCONNECTED) {
4638 		SCTP_INP_RUNLOCK(inp);
4639 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
4640 		return (ECONNABORTED);
4641 	}
4642 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
4643 	if (stcb == NULL) {
4644 		SCTP_INP_RUNLOCK(inp);
4645 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4646 		return (ECONNRESET);
4647 	}
4648 	SCTP_TCB_LOCK(stcb);
4649 	SCTP_INP_RUNLOCK(inp);
4650 	store = stcb->asoc.primary_destination->ro._l_addr;
4651 	SCTP_TCB_UNLOCK(stcb);
4652 	switch (store.sa.sa_family) {
4653 	case AF_INET:
4654 		{
4655 			struct sockaddr_in *sin;
4656 
4657 			SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4658 			sin->sin_family = AF_INET;
4659 			sin->sin_len = sizeof(*sin);
4660 			sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
4661 			sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
4662 			*addr = (struct sockaddr *)sin;
4663 			break;
4664 		}
4665 #ifdef INET6
4666 	case AF_INET6:
4667 		{
4668 			struct sockaddr_in6 *sin6;
4669 
4670 			SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
4671 			sin6->sin6_family = AF_INET6;
4672 			sin6->sin6_len = sizeof(*sin6);
4673 			sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
4674 
4675 			sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
4676 			if ((error = sa6_recoverscope(sin6)) != 0) {
4677 				SCTP_FREE_SONAME(sin6);
4678 				return (error);
4679 			}
4680 			*addr = (struct sockaddr *)sin6;
4681 			break;
4682 		}
4683 #endif
4684 	default:
4685 		/* TSNH */
4686 		break;
4687 	}
4688 	/* Wake any delayed sleep action */
4689 	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4690 		SCTP_INP_WLOCK(inp);
4691 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4692 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4693 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4694 			SCTP_INP_WUNLOCK(inp);
4695 			SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4696 			if (sowriteable(inp->sctp_socket)) {
4697 				sowwakeup_locked(inp->sctp_socket);
4698 			} else {
4699 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4700 			}
4701 			SCTP_INP_WLOCK(inp);
4702 		}
4703 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4704 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4705 			SCTP_INP_WUNLOCK(inp);
4706 			SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4707 			if (soreadable(inp->sctp_socket)) {
4708 				sctp_defered_wakeup_cnt++;
4709 				sorwakeup_locked(inp->sctp_socket);
4710 			} else {
4711 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4712 			}
4713 			SCTP_INP_WLOCK(inp);
4714 		}
4715 		SCTP_INP_WUNLOCK(inp);
4716 	}
4717 	return (0);
4718 }
4719 
4720 int
4721 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4722 {
4723 	struct sockaddr_in *sin;
4724 	uint32_t vrf_id;
4725 	struct sctp_inpcb *inp;
4726 	struct sctp_ifa *sctp_ifa;
4727 
4728 	/*
4729 	 * Do the malloc first in case it blocks.
4730 	 */
4731 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4732 	sin->sin_family = AF_INET;
4733 	sin->sin_len = sizeof(*sin);
4734 	inp = (struct sctp_inpcb *)so->so_pcb;
4735 	if (!inp) {
4736 		SCTP_FREE_SONAME(sin);
4737 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4738 		return ECONNRESET;
4739 	}
4740 	SCTP_INP_RLOCK(inp);
4741 	sin->sin_port = inp->sctp_lport;
4742 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4743 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4744 			struct sctp_tcb *stcb;
4745 			struct sockaddr_in *sin_a;
4746 			struct sctp_nets *net;
4747 			int fnd;
4748 
4749 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
4750 			if (stcb == NULL) {
4751 				goto notConn;
4752 			}
4753 			fnd = 0;
4754 			sin_a = NULL;
4755 			SCTP_TCB_LOCK(stcb);
4756 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4757 				sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4758 				if (sin_a == NULL)
4759 					/* this will make coverity happy */
4760 					continue;
4761 
4762 				if (sin_a->sin_family == AF_INET) {
4763 					fnd = 1;
4764 					break;
4765 				}
4766 			}
4767 			if ((!fnd) || (sin_a == NULL)) {
4768 				/* punt */
4769 				SCTP_TCB_UNLOCK(stcb);
4770 				goto notConn;
4771 			}
4772 			vrf_id = inp->def_vrf_id;
4773 			sctp_ifa = sctp_source_address_selection(inp,
4774 			    stcb,
4775 			    (sctp_route_t *) & net->ro,
4776 			    net, 0, vrf_id);
4777 			if (sctp_ifa) {
4778 				sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4779 				sctp_free_ifa(sctp_ifa);
4780 			}
4781 			SCTP_TCB_UNLOCK(stcb);
4782 		} else {
4783 			/* For the bound all case you get back 0 */
4784 	notConn:
4785 			sin->sin_addr.s_addr = 0;
4786 		}
4787 
4788 	} else {
4789 		/* Take the first IPv4 address in the list */
4790 		struct sctp_laddr *laddr;
4791 		int fnd = 0;
4792 
4793 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4794 			if (laddr->ifa->address.sa.sa_family == AF_INET) {
4795 				struct sockaddr_in *sin_a;
4796 
4797 				sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4798 				sin->sin_addr = sin_a->sin_addr;
4799 				fnd = 1;
4800 				break;
4801 			}
4802 		}
4803 		if (!fnd) {
4804 			SCTP_FREE_SONAME(sin);
4805 			SCTP_INP_RUNLOCK(inp);
4806 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4807 			return ENOENT;
4808 		}
4809 	}
4810 	SCTP_INP_RUNLOCK(inp);
4811 	(*addr) = (struct sockaddr *)sin;
4812 	return (0);
4813 }
4814 
4815 int
4816 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4817 {
4818 	struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4819 	int fnd;
4820 	struct sockaddr_in *sin_a;
4821 	struct sctp_inpcb *inp;
4822 	struct sctp_tcb *stcb;
4823 	struct sctp_nets *net;
4824 
4825 	/* Do the malloc first in case it blocks. */
4826 	inp = (struct sctp_inpcb *)so->so_pcb;
4827 	if ((inp == NULL) ||
4828 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4829 		/* UDP type and listeners will drop out here */
4830 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
4831 		return (ENOTCONN);
4832 	}
4833 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4834 	sin->sin_family = AF_INET;
4835 	sin->sin_len = sizeof(*sin);
4836 
4837 	/* We must recapture incase we blocked */
4838 	inp = (struct sctp_inpcb *)so->so_pcb;
4839 	if (!inp) {
4840 		SCTP_FREE_SONAME(sin);
4841 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4842 		return ECONNRESET;
4843 	}
4844 	SCTP_INP_RLOCK(inp);
4845 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
4846 	if (stcb) {
4847 		SCTP_TCB_LOCK(stcb);
4848 	}
4849 	SCTP_INP_RUNLOCK(inp);
4850 	if (stcb == NULL) {
4851 		SCTP_FREE_SONAME(sin);
4852 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4853 		return ECONNRESET;
4854 	}
4855 	fnd = 0;
4856 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4857 		sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4858 		if (sin_a->sin_family == AF_INET) {
4859 			fnd = 1;
4860 			sin->sin_port = stcb->rport;
4861 			sin->sin_addr = sin_a->sin_addr;
4862 			break;
4863 		}
4864 	}
4865 	SCTP_TCB_UNLOCK(stcb);
4866 	if (!fnd) {
4867 		/* No IPv4 address */
4868 		SCTP_FREE_SONAME(sin);
4869 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4870 		return ENOENT;
4871 	}
4872 	(*addr) = (struct sockaddr *)sin;
4873 	return (0);
4874 }
4875 
4876 struct pr_usrreqs sctp_usrreqs = {
4877 	.pru_abort = sctp_abort,
4878 	.pru_accept = sctp_accept,
4879 	.pru_attach = sctp_attach,
4880 	.pru_bind = sctp_bind,
4881 	.pru_connect = sctp_connect,
4882 	.pru_control = in_control,
4883 	.pru_close = sctp_close,
4884 	.pru_detach = sctp_close,
4885 	.pru_sopoll = sopoll_generic,
4886 	.pru_flush = sctp_flush,
4887 	.pru_disconnect = sctp_disconnect,
4888 	.pru_listen = sctp_listen,
4889 	.pru_peeraddr = sctp_peeraddr,
4890 	.pru_send = sctp_sendm,
4891 	.pru_shutdown = sctp_shutdown,
4892 	.pru_sockaddr = sctp_ingetaddr,
4893 	.pru_sosend = sctp_sosend,
4894 	.pru_soreceive = sctp_soreceive
4895 };
4896