xref: /freebsd/sys/netinet/sctp_usrreq.c (revision a3cf0ef5a295c885c895fabfd56470c0d1db322d)
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 	uint16_t overhead;
110 
111 	/* Adjust that too */
112 	stcb->asoc.smallest_mtu = nxtsz;
113 	/* now off to subtract IP_DF flag if needed */
114 	overhead = IP_HDR_SIZE;
115 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
116 		overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
117 	}
118 	TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
119 		if ((chk->send_size + overhead) > nxtsz) {
120 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
121 		}
122 	}
123 	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
124 		if ((chk->send_size + overhead) > nxtsz) {
125 			/*
126 			 * For this guy we also mark for immediate resend
127 			 * since we sent to big of chunk
128 			 */
129 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
130 			if (chk->sent < SCTP_DATAGRAM_RESEND) {
131 				sctp_flight_size_decrease(chk);
132 				sctp_total_flight_decrease(stcb, chk);
133 			}
134 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
135 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
136 			}
137 			chk->sent = SCTP_DATAGRAM_RESEND;
138 			chk->rec.data.doing_fast_retransmit = 0;
139 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
140 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
141 				    chk->whoTo->flight_size,
142 				    chk->book_size,
143 				    (uintptr_t) chk->whoTo,
144 				    chk->rec.data.TSN_seq);
145 			}
146 			/* Clear any time so NO RTT is being done */
147 			chk->do_rtt = 0;
148 		}
149 	}
150 }
151 
152 static void
153 sctp_notify_mbuf(struct sctp_inpcb *inp,
154     struct sctp_tcb *stcb,
155     struct sctp_nets *net,
156     struct ip *ip,
157     struct sctphdr *sh)
158 {
159 	struct icmp *icmph;
160 	int totsz, tmr_stopped = 0;
161 	uint16_t nxtsz;
162 
163 	/* protection */
164 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
165 	    (ip == NULL) || (sh == NULL)) {
166 		if (stcb != NULL) {
167 			SCTP_TCB_UNLOCK(stcb);
168 		}
169 		return;
170 	}
171 	/* First job is to verify the vtag matches what I would send */
172 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
173 		SCTP_TCB_UNLOCK(stcb);
174 		return;
175 	}
176 	icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
177 	    sizeof(struct ip)));
178 	if (icmph->icmp_type != ICMP_UNREACH) {
179 		/* We only care about unreachable */
180 		SCTP_TCB_UNLOCK(stcb);
181 		return;
182 	}
183 	if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
184 		/* not a unreachable message due to frag. */
185 		SCTP_TCB_UNLOCK(stcb);
186 		return;
187 	}
188 	totsz = ip->ip_len;
189 
190 	nxtsz = ntohs(icmph->icmp_nextmtu);
191 	if (nxtsz == 0) {
192 		/*
193 		 * old type router that does not tell us what the next size
194 		 * mtu is. Rats we will have to guess (in a educated fashion
195 		 * of course)
196 		 */
197 		nxtsz = sctp_get_prev_mtu(totsz);
198 	}
199 	/* Stop any PMTU timer */
200 	if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
201 		tmr_stopped = 1;
202 		sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
203 		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
204 	}
205 	/* Adjust destination size limit */
206 	if (net->mtu > nxtsz) {
207 		net->mtu = nxtsz;
208 		if (net->port) {
209 			net->mtu -= sizeof(struct udphdr);
210 		}
211 	}
212 	/* now what about the ep? */
213 	if (stcb->asoc.smallest_mtu > nxtsz) {
214 		sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
215 	}
216 	if (tmr_stopped)
217 		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
218 
219 	SCTP_TCB_UNLOCK(stcb);
220 }
221 
222 
223 void
224 sctp_notify(struct sctp_inpcb *inp,
225     struct ip *ip,
226     struct sctphdr *sh,
227     struct sockaddr *to,
228     struct sctp_tcb *stcb,
229     struct sctp_nets *net)
230 {
231 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
232 	struct socket *so;
233 
234 #endif
235 	/* protection */
236 	int reason;
237 	struct icmp *icmph;
238 
239 
240 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
241 	    (sh == NULL) || (to == NULL)) {
242 		if (stcb)
243 			SCTP_TCB_UNLOCK(stcb);
244 		return;
245 	}
246 	/* First job is to verify the vtag matches what I would send */
247 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
248 		SCTP_TCB_UNLOCK(stcb);
249 		return;
250 	}
251 	icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
252 	    sizeof(struct ip)));
253 	if (icmph->icmp_type != ICMP_UNREACH) {
254 		/* We only care about unreachable */
255 		SCTP_TCB_UNLOCK(stcb);
256 		return;
257 	}
258 	if ((icmph->icmp_code == ICMP_UNREACH_NET) ||
259 	    (icmph->icmp_code == ICMP_UNREACH_HOST) ||
260 	    (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
261 	    (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
262 	    (icmph->icmp_code == ICMP_UNREACH_ISOLATED) ||
263 	    (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) ||
264 	    (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
265 	    (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
266 
267 		/*
268 		 * Hmm reachablity problems we must examine closely. If its
269 		 * not reachable, we may have lost a network. Or if there is
270 		 * NO protocol at the other end named SCTP. well we consider
271 		 * it a OOTB abort.
272 		 */
273 		if (net->dest_state & SCTP_ADDR_REACHABLE) {
274 			/* Ok that destination is NOT reachable */
275 			SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
276 			    net->error_count,
277 			    net->failure_threshold,
278 			    net);
279 
280 			net->dest_state &= ~SCTP_ADDR_REACHABLE;
281 			net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
282 			/*
283 			 * JRS 5/14/07 - If a destination is unreachable,
284 			 * the PF bit is turned off.  This allows an
285 			 * unambiguous use of the PF bit for destinations
286 			 * that are reachable but potentially failed. If the
287 			 * destination is set to the unreachable state, also
288 			 * set the destination to the PF state.
289 			 */
290 			/*
291 			 * Add debug message here if destination is not in
292 			 * PF state.
293 			 */
294 			/* Stop any running T3 timers here? */
295 			if ((stcb->asoc.sctp_cmt_on_off == 1) &&
296 			    (stcb->asoc.sctp_cmt_pf > 0)) {
297 				net->dest_state &= ~SCTP_ADDR_PF;
298 				SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
299 				    net);
300 			}
301 			net->error_count = net->failure_threshold + 1;
302 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
303 			    stcb, SCTP_FAILED_THRESHOLD,
304 			    (void *)net, SCTP_SO_NOT_LOCKED);
305 		}
306 		SCTP_TCB_UNLOCK(stcb);
307 	} else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
308 	    (icmph->icmp_code == ICMP_UNREACH_PORT)) {
309 		/*
310 		 * Here the peer is either playing tricks on us, including
311 		 * an address that belongs to someone who does not support
312 		 * SCTP OR was a userland implementation that shutdown and
313 		 * now is dead. In either case treat it like a OOTB abort
314 		 * with no TCB
315 		 */
316 		reason = SCTP_PEER_FAULTY;
317 		sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
318 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
319 		so = SCTP_INP_SO(inp);
320 		atomic_add_int(&stcb->asoc.refcnt, 1);
321 		SCTP_TCB_UNLOCK(stcb);
322 		SCTP_SOCKET_LOCK(so, 1);
323 		SCTP_TCB_LOCK(stcb);
324 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
325 #endif
326 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
327 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
328 		SCTP_SOCKET_UNLOCK(so, 1);
329 		/* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
330 #endif
331 		/* no need to unlock here, since the TCB is gone */
332 	} else {
333 		SCTP_TCB_UNLOCK(stcb);
334 	}
335 }
336 
337 void
338 sctp_ctlinput(cmd, sa, vip)
339 	int cmd;
340 	struct sockaddr *sa;
341 	void *vip;
342 {
343 	struct ip *ip = vip;
344 	struct sctphdr *sh;
345 	uint32_t vrf_id;
346 
347 	/* FIX, for non-bsd is this right? */
348 	vrf_id = SCTP_DEFAULT_VRFID;
349 	if (sa->sa_family != AF_INET ||
350 	    ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
351 		return;
352 	}
353 	if (PRC_IS_REDIRECT(cmd)) {
354 		ip = 0;
355 	} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
356 		return;
357 	}
358 	if (ip) {
359 		struct sctp_inpcb *inp = NULL;
360 		struct sctp_tcb *stcb = NULL;
361 		struct sctp_nets *net = NULL;
362 		struct sockaddr_in to, from;
363 
364 		sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
365 		bzero(&to, sizeof(to));
366 		bzero(&from, sizeof(from));
367 		from.sin_family = to.sin_family = AF_INET;
368 		from.sin_len = to.sin_len = sizeof(to);
369 		from.sin_port = sh->src_port;
370 		from.sin_addr = ip->ip_src;
371 		to.sin_port = sh->dest_port;
372 		to.sin_addr = ip->ip_dst;
373 
374 		/*
375 		 * 'to' holds the dest of the packet that failed to be sent.
376 		 * 'from' holds our local endpoint address. Thus we reverse
377 		 * the to and the from in the lookup.
378 		 */
379 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
380 		    (struct sockaddr *)&to,
381 		    &inp, &net, 1, vrf_id);
382 		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
383 			if (cmd != PRC_MSGSIZE) {
384 				sctp_notify(inp, ip, sh,
385 				    (struct sockaddr *)&to, stcb,
386 				    net);
387 			} else {
388 				/* handle possible ICMP size messages */
389 				sctp_notify_mbuf(inp, stcb, net, ip, sh);
390 			}
391 		} else {
392 			if ((stcb == NULL) && (inp != NULL)) {
393 				/* reduce ref-count */
394 				SCTP_INP_WLOCK(inp);
395 				SCTP_INP_DECR_REF(inp);
396 				SCTP_INP_WUNLOCK(inp);
397 			}
398 			if (stcb) {
399 				SCTP_TCB_UNLOCK(stcb);
400 			}
401 		}
402 	}
403 	return;
404 }
405 
406 static int
407 sctp_getcred(SYSCTL_HANDLER_ARGS)
408 {
409 	struct xucred xuc;
410 	struct sockaddr_in addrs[2];
411 	struct sctp_inpcb *inp;
412 	struct sctp_nets *net;
413 	struct sctp_tcb *stcb;
414 	int error;
415 	uint32_t vrf_id;
416 
417 	/* FIX, for non-bsd is this right? */
418 	vrf_id = SCTP_DEFAULT_VRFID;
419 
420 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
421 
422 	if (error)
423 		return (error);
424 
425 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
426 	if (error)
427 		return (error);
428 
429 	stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
430 	    sintosa(&addrs[1]),
431 	    &inp, &net, 1, vrf_id);
432 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
433 		if ((inp != NULL) && (stcb == NULL)) {
434 			/* reduce ref-count */
435 			SCTP_INP_WLOCK(inp);
436 			SCTP_INP_DECR_REF(inp);
437 			goto cred_can_cont;
438 		}
439 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
440 		error = ENOENT;
441 		goto out;
442 	}
443 	SCTP_TCB_UNLOCK(stcb);
444 	/*
445 	 * We use the write lock here, only since in the error leg we need
446 	 * it. If we used RLOCK, then we would have to
447 	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
448 	 * Better to use higher wlock.
449 	 */
450 	SCTP_INP_WLOCK(inp);
451 cred_can_cont:
452 	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
453 	if (error) {
454 		SCTP_INP_WUNLOCK(inp);
455 		goto out;
456 	}
457 	cru2x(inp->sctp_socket->so_cred, &xuc);
458 	SCTP_INP_WUNLOCK(inp);
459 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
460 out:
461 	return (error);
462 }
463 
464 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
465     0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
466 
467 
468 static void
469 sctp_abort(struct socket *so)
470 {
471 	struct sctp_inpcb *inp;
472 	uint32_t flags;
473 
474 	inp = (struct sctp_inpcb *)so->so_pcb;
475 	if (inp == 0) {
476 		return;
477 	}
478 sctp_must_try_again:
479 	flags = inp->sctp_flags;
480 #ifdef SCTP_LOG_CLOSING
481 	sctp_log_closing(inp, NULL, 17);
482 #endif
483 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
484 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
485 #ifdef SCTP_LOG_CLOSING
486 		sctp_log_closing(inp, NULL, 16);
487 #endif
488 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
489 		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
490 		SOCK_LOCK(so);
491 		SCTP_SB_CLEAR(so->so_snd);
492 		/*
493 		 * same for the rcv ones, they are only here for the
494 		 * accounting/select.
495 		 */
496 		SCTP_SB_CLEAR(so->so_rcv);
497 
498 		/* Now null out the reference, we are completely detached. */
499 		so->so_pcb = NULL;
500 		SOCK_UNLOCK(so);
501 	} else {
502 		flags = inp->sctp_flags;
503 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
504 			goto sctp_must_try_again;
505 		}
506 	}
507 	return;
508 }
509 
510 static int
511 sctp_attach(struct socket *so, int proto, struct thread *p)
512 {
513 	struct sctp_inpcb *inp;
514 	struct inpcb *ip_inp;
515 	int error;
516 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
517 
518 #ifdef IPSEC
519 	uint32_t flags;
520 
521 #endif
522 
523 	inp = (struct sctp_inpcb *)so->so_pcb;
524 	if (inp != 0) {
525 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
526 		return EINVAL;
527 	}
528 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
529 		error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
530 		if (error) {
531 			return error;
532 		}
533 	}
534 	error = sctp_inpcb_alloc(so, vrf_id);
535 	if (error) {
536 		return error;
537 	}
538 	inp = (struct sctp_inpcb *)so->so_pcb;
539 	SCTP_INP_WLOCK(inp);
540 	inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;	/* I'm not v6! */
541 	ip_inp = &inp->ip_inp.inp;
542 	ip_inp->inp_vflag |= INP_IPV4;
543 	ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
544 #ifdef IPSEC
545 	error = ipsec_init_policy(so, &ip_inp->inp_sp);
546 #ifdef SCTP_LOG_CLOSING
547 	sctp_log_closing(inp, NULL, 17);
548 #endif
549 	if (error != 0) {
550 try_again:
551 		flags = inp->sctp_flags;
552 		if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
553 		    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
554 #ifdef SCTP_LOG_CLOSING
555 			sctp_log_closing(inp, NULL, 15);
556 #endif
557 			SCTP_INP_WUNLOCK(inp);
558 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
559 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
560 		} else {
561 			flags = inp->sctp_flags;
562 			if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
563 				goto try_again;
564 			} else {
565 				SCTP_INP_WUNLOCK(inp);
566 			}
567 		}
568 		return error;
569 	}
570 #endif				/* IPSEC */
571 	SCTP_INP_WUNLOCK(inp);
572 	return 0;
573 }
574 
575 static int
576 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
577 {
578 	struct sctp_inpcb *inp = NULL;
579 	int error;
580 
581 #ifdef INET6
582 	if (addr && addr->sa_family != AF_INET) {
583 		/* must be a v4 address! */
584 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
585 		return EINVAL;
586 	}
587 #endif				/* INET6 */
588 	if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
589 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
590 		return EINVAL;
591 	}
592 	inp = (struct sctp_inpcb *)so->so_pcb;
593 	if (inp == 0) {
594 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
595 		return EINVAL;
596 	}
597 	error = sctp_inpcb_bind(so, addr, NULL, p);
598 	return error;
599 }
600 
601 void
602 sctp_close(struct socket *so)
603 {
604 	struct sctp_inpcb *inp;
605 	uint32_t flags;
606 
607 	inp = (struct sctp_inpcb *)so->so_pcb;
608 	if (inp == 0)
609 		return;
610 
611 	/*
612 	 * Inform all the lower layer assoc that we are done.
613 	 */
614 sctp_must_try_again:
615 	flags = inp->sctp_flags;
616 #ifdef SCTP_LOG_CLOSING
617 	sctp_log_closing(inp, NULL, 17);
618 #endif
619 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
620 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
621 		if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
622 		    (so->so_rcv.sb_cc > 0)) {
623 #ifdef SCTP_LOG_CLOSING
624 			sctp_log_closing(inp, NULL, 13);
625 #endif
626 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
627 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
628 		} else {
629 #ifdef SCTP_LOG_CLOSING
630 			sctp_log_closing(inp, NULL, 14);
631 #endif
632 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
633 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
634 		}
635 		/*
636 		 * The socket is now detached, no matter what the state of
637 		 * the SCTP association.
638 		 */
639 		SOCK_LOCK(so);
640 		SCTP_SB_CLEAR(so->so_snd);
641 		/*
642 		 * same for the rcv ones, they are only here for the
643 		 * accounting/select.
644 		 */
645 		SCTP_SB_CLEAR(so->so_rcv);
646 
647 		/* Now null out the reference, we are completely detached. */
648 		so->so_pcb = NULL;
649 		SOCK_UNLOCK(so);
650 	} else {
651 		flags = inp->sctp_flags;
652 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
653 			goto sctp_must_try_again;
654 		}
655 	}
656 	return;
657 }
658 
659 
660 int
661 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
662     struct mbuf *control, struct thread *p);
663 
664 
665 int
666 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
667     struct mbuf *control, struct thread *p)
668 {
669 	struct sctp_inpcb *inp;
670 	int error;
671 
672 	inp = (struct sctp_inpcb *)so->so_pcb;
673 	if (inp == 0) {
674 		if (control) {
675 			sctp_m_freem(control);
676 			control = NULL;
677 		}
678 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
679 		sctp_m_freem(m);
680 		return EINVAL;
681 	}
682 	/* Got to have an to address if we are NOT a connected socket */
683 	if ((addr == NULL) &&
684 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
685 	    (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
686 	    ) {
687 		goto connected_type;
688 	} else if (addr == NULL) {
689 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
690 		error = EDESTADDRREQ;
691 		sctp_m_freem(m);
692 		if (control) {
693 			sctp_m_freem(control);
694 			control = NULL;
695 		}
696 		return (error);
697 	}
698 #ifdef INET6
699 	if (addr->sa_family != AF_INET) {
700 		/* must be a v4 address! */
701 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
702 		sctp_m_freem(m);
703 		if (control) {
704 			sctp_m_freem(control);
705 			control = NULL;
706 		}
707 		error = EDESTADDRREQ;
708 		return EDESTADDRREQ;
709 	}
710 #endif				/* INET6 */
711 connected_type:
712 	/* now what about control */
713 	if (control) {
714 		if (inp->control) {
715 			SCTP_PRINTF("huh? control set?\n");
716 			sctp_m_freem(inp->control);
717 			inp->control = NULL;
718 		}
719 		inp->control = control;
720 	}
721 	/* Place the data */
722 	if (inp->pkt) {
723 		SCTP_BUF_NEXT(inp->pkt_last) = m;
724 		inp->pkt_last = m;
725 	} else {
726 		inp->pkt_last = inp->pkt = m;
727 	}
728 	if (
729 	/* FreeBSD uses a flag passed */
730 	    ((flags & PRUS_MORETOCOME) == 0)
731 	    ) {
732 		/*
733 		 * note with the current version this code will only be used
734 		 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
735 		 * re-defining sosend to use the sctp_sosend. One can
736 		 * optionally switch back to this code (by changing back the
737 		 * definitions) but this is not advisable. This code is used
738 		 * by FreeBSD when sending a file with sendfile() though.
739 		 */
740 		int ret;
741 
742 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
743 		inp->pkt = NULL;
744 		inp->control = NULL;
745 		return (ret);
746 	} else {
747 		return (0);
748 	}
749 }
750 
751 int
752 sctp_disconnect(struct socket *so)
753 {
754 	struct sctp_inpcb *inp;
755 
756 	inp = (struct sctp_inpcb *)so->so_pcb;
757 	if (inp == NULL) {
758 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
759 		return (ENOTCONN);
760 	}
761 	SCTP_INP_RLOCK(inp);
762 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
763 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
764 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
765 			/* No connection */
766 			SCTP_INP_RUNLOCK(inp);
767 			return (0);
768 		} else {
769 			struct sctp_association *asoc;
770 			struct sctp_tcb *stcb;
771 
772 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
773 			if (stcb == NULL) {
774 				SCTP_INP_RUNLOCK(inp);
775 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
776 				return (EINVAL);
777 			}
778 			SCTP_TCB_LOCK(stcb);
779 			asoc = &stcb->asoc;
780 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
781 				/* We are about to be freed, out of here */
782 				SCTP_TCB_UNLOCK(stcb);
783 				SCTP_INP_RUNLOCK(inp);
784 				return (0);
785 			}
786 			if (((so->so_options & SO_LINGER) &&
787 			    (so->so_linger == 0)) ||
788 			    (so->so_rcv.sb_cc > 0)) {
789 				if (SCTP_GET_STATE(asoc) !=
790 				    SCTP_STATE_COOKIE_WAIT) {
791 					/* Left with Data unread */
792 					struct mbuf *err;
793 
794 					err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
795 					if (err) {
796 						/*
797 						 * Fill in the user
798 						 * initiated abort
799 						 */
800 						struct sctp_paramhdr *ph;
801 
802 						ph = mtod(err, struct sctp_paramhdr *);
803 						SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
804 						ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
805 						ph->param_length = htons(SCTP_BUF_LEN(err));
806 					}
807 #if defined(SCTP_PANIC_ON_ABORT)
808 					panic("disconnect does an abort");
809 #endif
810 					sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
811 					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
812 				}
813 				SCTP_INP_RUNLOCK(inp);
814 				if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
815 				    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
816 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
817 				}
818 				(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
819 				/* No unlock tcb assoc is gone */
820 				return (0);
821 			}
822 			if (TAILQ_EMPTY(&asoc->send_queue) &&
823 			    TAILQ_EMPTY(&asoc->sent_queue) &&
824 			    (asoc->stream_queue_cnt == 0)) {
825 				/* there is nothing queued to send, so done */
826 				if (asoc->locked_on_sending) {
827 					goto abort_anyway;
828 				}
829 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
830 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
831 					/* only send SHUTDOWN 1st time thru */
832 					sctp_stop_timers_for_shutdown(stcb);
833 					sctp_send_shutdown(stcb,
834 					    stcb->asoc.primary_destination);
835 					sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
836 					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
837 					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
838 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
839 					}
840 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
841 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
842 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
843 					    stcb->sctp_ep, stcb,
844 					    asoc->primary_destination);
845 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
846 					    stcb->sctp_ep, stcb,
847 					    asoc->primary_destination);
848 				}
849 			} else {
850 				/*
851 				 * we still got (or just got) data to send,
852 				 * so set SHUTDOWN_PENDING
853 				 */
854 				/*
855 				 * XXX sockets draft says that SCTP_EOF
856 				 * should be sent with no data. currently,
857 				 * we will allow user data to be sent first
858 				 * and move to SHUTDOWN-PENDING
859 				 */
860 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
861 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
862 				    asoc->primary_destination);
863 				if (asoc->locked_on_sending) {
864 					/* Locked to send out the data */
865 					struct sctp_stream_queue_pending *sp;
866 
867 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
868 					if (sp == NULL) {
869 						SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
870 						    asoc->locked_on_sending->stream_no);
871 					} else {
872 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
873 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
874 					}
875 				}
876 				if (TAILQ_EMPTY(&asoc->send_queue) &&
877 				    TAILQ_EMPTY(&asoc->sent_queue) &&
878 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
879 					struct mbuf *op_err;
880 
881 			abort_anyway:
882 					op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
883 					    0, M_DONTWAIT, 1, MT_DATA);
884 					if (op_err) {
885 						/*
886 						 * Fill in the user
887 						 * initiated abort
888 						 */
889 						struct sctp_paramhdr *ph;
890 						uint32_t *ippp;
891 
892 						SCTP_BUF_LEN(op_err) =
893 						    (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
894 						ph = mtod(op_err,
895 						    struct sctp_paramhdr *);
896 						ph->param_type = htons(
897 						    SCTP_CAUSE_USER_INITIATED_ABT);
898 						ph->param_length = htons(SCTP_BUF_LEN(op_err));
899 						ippp = (uint32_t *) (ph + 1);
900 						*ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
901 					}
902 #if defined(SCTP_PANIC_ON_ABORT)
903 					panic("disconnect does an abort");
904 #endif
905 
906 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
907 					sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
908 					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
909 					if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
910 					    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
911 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
912 					}
913 					SCTP_INP_RUNLOCK(inp);
914 					(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
915 					return (0);
916 				} else {
917 					sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
918 				}
919 			}
920 			soisdisconnecting(so);
921 			SCTP_TCB_UNLOCK(stcb);
922 			SCTP_INP_RUNLOCK(inp);
923 			return (0);
924 		}
925 		/* not reached */
926 	} else {
927 		/* UDP model does not support this */
928 		SCTP_INP_RUNLOCK(inp);
929 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
930 		return EOPNOTSUPP;
931 	}
932 }
933 
934 int
935 sctp_flush(struct socket *so, int how)
936 {
937 	/*
938 	 * We will just clear out the values and let subsequent close clear
939 	 * out the data, if any. Note if the user did a shutdown(SHUT_RD)
940 	 * they will not be able to read the data, the socket will block
941 	 * that from happening.
942 	 */
943 	struct sctp_inpcb *inp;
944 
945 	inp = (struct sctp_inpcb *)so->so_pcb;
946 	if (inp == NULL) {
947 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
948 		return EINVAL;
949 	}
950 	SCTP_INP_RLOCK(inp);
951 	/* For the 1 to many model this does nothing */
952 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
953 		SCTP_INP_RUNLOCK(inp);
954 		return (0);
955 	}
956 	SCTP_INP_RUNLOCK(inp);
957 	if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
958 		/*
959 		 * First make sure the sb will be happy, we don't use these
960 		 * except maybe the count
961 		 */
962 		SCTP_INP_WLOCK(inp);
963 		SCTP_INP_READ_LOCK(inp);
964 		inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_CANT_READ;
965 		SCTP_INP_READ_UNLOCK(inp);
966 		SCTP_INP_WUNLOCK(inp);
967 		so->so_rcv.sb_cc = 0;
968 		so->so_rcv.sb_mbcnt = 0;
969 		so->so_rcv.sb_mb = NULL;
970 	}
971 	if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
972 		/*
973 		 * First make sure the sb will be happy, we don't use these
974 		 * except maybe the count
975 		 */
976 		so->so_snd.sb_cc = 0;
977 		so->so_snd.sb_mbcnt = 0;
978 		so->so_snd.sb_mb = NULL;
979 
980 	}
981 	return (0);
982 }
983 
984 int
985 sctp_shutdown(struct socket *so)
986 {
987 	struct sctp_inpcb *inp;
988 
989 	inp = (struct sctp_inpcb *)so->so_pcb;
990 	if (inp == 0) {
991 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
992 		return EINVAL;
993 	}
994 	SCTP_INP_RLOCK(inp);
995 	/* For UDP model this is a invalid call */
996 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
997 		/* Restore the flags that the soshutdown took away. */
998 		SOCKBUF_LOCK(&so->so_rcv);
999 		so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
1000 		SOCKBUF_UNLOCK(&so->so_rcv);
1001 		/* This proc will wakeup for read and do nothing (I hope) */
1002 		SCTP_INP_RUNLOCK(inp);
1003 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1004 		return (EOPNOTSUPP);
1005 	}
1006 	/*
1007 	 * Ok if we reach here its the TCP model and it is either a SHUT_WR
1008 	 * or SHUT_RDWR. This means we put the shutdown flag against it.
1009 	 */
1010 	{
1011 		struct sctp_tcb *stcb;
1012 		struct sctp_association *asoc;
1013 
1014 		if ((so->so_state &
1015 		    (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
1016 			SCTP_INP_RUNLOCK(inp);
1017 			return (ENOTCONN);
1018 		}
1019 		socantsendmore(so);
1020 
1021 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1022 		if (stcb == NULL) {
1023 			/*
1024 			 * Ok we hit the case that the shutdown call was
1025 			 * made after an abort or something. Nothing to do
1026 			 * now.
1027 			 */
1028 			SCTP_INP_RUNLOCK(inp);
1029 			return (0);
1030 		}
1031 		SCTP_TCB_LOCK(stcb);
1032 		asoc = &stcb->asoc;
1033 		if (TAILQ_EMPTY(&asoc->send_queue) &&
1034 		    TAILQ_EMPTY(&asoc->sent_queue) &&
1035 		    (asoc->stream_queue_cnt == 0)) {
1036 			if (asoc->locked_on_sending) {
1037 				goto abort_anyway;
1038 			}
1039 			/* there is nothing queued to send, so I'm done... */
1040 			if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1041 				/* only send SHUTDOWN the first time through */
1042 				sctp_stop_timers_for_shutdown(stcb);
1043 				sctp_send_shutdown(stcb,
1044 				    stcb->asoc.primary_destination);
1045 				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
1046 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1047 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1048 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1049 				}
1050 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1051 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1052 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1053 				    stcb->sctp_ep, stcb,
1054 				    asoc->primary_destination);
1055 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1056 				    stcb->sctp_ep, stcb,
1057 				    asoc->primary_destination);
1058 			}
1059 		} else {
1060 			/*
1061 			 * we still got (or just got) data to send, so set
1062 			 * SHUTDOWN_PENDING
1063 			 */
1064 			asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1065 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
1066 			    asoc->primary_destination);
1067 
1068 			if (asoc->locked_on_sending) {
1069 				/* Locked to send out the data */
1070 				struct sctp_stream_queue_pending *sp;
1071 
1072 				sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1073 				if (sp == NULL) {
1074 					SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1075 					    asoc->locked_on_sending->stream_no);
1076 				} else {
1077 					if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1078 						asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1079 					}
1080 				}
1081 			}
1082 			if (TAILQ_EMPTY(&asoc->send_queue) &&
1083 			    TAILQ_EMPTY(&asoc->sent_queue) &&
1084 			    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1085 				struct mbuf *op_err;
1086 
1087 		abort_anyway:
1088 				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1089 				    0, M_DONTWAIT, 1, MT_DATA);
1090 				if (op_err) {
1091 					/* Fill in the user initiated abort */
1092 					struct sctp_paramhdr *ph;
1093 					uint32_t *ippp;
1094 
1095 					SCTP_BUF_LEN(op_err) =
1096 					    sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
1097 					ph = mtod(op_err,
1098 					    struct sctp_paramhdr *);
1099 					ph->param_type = htons(
1100 					    SCTP_CAUSE_USER_INITIATED_ABT);
1101 					ph->param_length = htons(SCTP_BUF_LEN(op_err));
1102 					ippp = (uint32_t *) (ph + 1);
1103 					*ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1104 				}
1105 #if defined(SCTP_PANIC_ON_ABORT)
1106 				panic("shutdown does an abort");
1107 #endif
1108 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1109 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1110 				    SCTP_RESPONSE_TO_USER_REQ,
1111 				    op_err, SCTP_SO_LOCKED);
1112 				goto skip_unlock;
1113 			} else {
1114 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1115 			}
1116 		}
1117 		SCTP_TCB_UNLOCK(stcb);
1118 	}
1119 skip_unlock:
1120 	SCTP_INP_RUNLOCK(inp);
1121 	return 0;
1122 }
1123 
1124 /*
1125  * copies a "user" presentable address and removes embedded scope, etc.
1126  * returns 0 on success, 1 on error
1127  */
1128 static uint32_t
1129 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1130 {
1131 #ifdef INET6
1132 	struct sockaddr_in6 lsa6;
1133 
1134 	sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1135 	    &lsa6);
1136 #endif
1137 	memcpy(ss, sa, sa->sa_len);
1138 	return (0);
1139 }
1140 
1141 
1142 
1143 /*
1144  * NOTE: assumes addr lock is held
1145  */
1146 static size_t
1147 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1148     struct sctp_tcb *stcb,
1149     size_t limit,
1150     struct sockaddr_storage *sas,
1151     uint32_t vrf_id)
1152 {
1153 	struct sctp_ifn *sctp_ifn;
1154 	struct sctp_ifa *sctp_ifa;
1155 	int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1156 	size_t actual;
1157 	int ipv4_addr_legal, ipv6_addr_legal;
1158 	struct sctp_vrf *vrf;
1159 
1160 	actual = 0;
1161 	if (limit <= 0)
1162 		return (actual);
1163 
1164 	if (stcb) {
1165 		/* Turn on all the appropriate scope */
1166 		loopback_scope = stcb->asoc.loopback_scope;
1167 		ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1168 		local_scope = stcb->asoc.local_scope;
1169 		site_scope = stcb->asoc.site_scope;
1170 	} else {
1171 		/* Turn on ALL scope, since we look at the EP */
1172 		loopback_scope = ipv4_local_scope = local_scope =
1173 		    site_scope = 1;
1174 	}
1175 	ipv4_addr_legal = ipv6_addr_legal = 0;
1176 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1177 		ipv6_addr_legal = 1;
1178 		if (SCTP_IPV6_V6ONLY(inp) == 0) {
1179 			ipv4_addr_legal = 1;
1180 		}
1181 	} else {
1182 		ipv4_addr_legal = 1;
1183 	}
1184 	vrf = sctp_find_vrf(vrf_id);
1185 	if (vrf == NULL) {
1186 		return (0);
1187 	}
1188 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1189 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1190 			if ((loopback_scope == 0) &&
1191 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1192 				/* Skip loopback if loopback_scope not set */
1193 				continue;
1194 			}
1195 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1196 				if (stcb) {
1197 					/*
1198 					 * For the BOUND-ALL case, the list
1199 					 * associated with a TCB is Always
1200 					 * considered a reverse list.. i.e.
1201 					 * it lists addresses that are NOT
1202 					 * part of the association. If this
1203 					 * is one of those we must skip it.
1204 					 */
1205 					if (sctp_is_addr_restricted(stcb,
1206 					    sctp_ifa)) {
1207 						continue;
1208 					}
1209 				}
1210 				switch (sctp_ifa->address.sa.sa_family) {
1211 				case AF_INET:
1212 					if (ipv4_addr_legal) {
1213 						struct sockaddr_in *sin;
1214 
1215 						sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1216 						if (sin->sin_addr.s_addr == 0) {
1217 							/*
1218 							 * we skip
1219 							 * unspecifed
1220 							 * addresses
1221 							 */
1222 							continue;
1223 						}
1224 						if ((ipv4_local_scope == 0) &&
1225 						    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1226 							continue;
1227 						}
1228 #ifdef INET6
1229 						if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1230 							in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1231 							((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1232 							sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1233 							actual += sizeof(struct sockaddr_in6);
1234 						} else {
1235 #endif
1236 							memcpy(sas, sin, sizeof(*sin));
1237 							((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1238 							sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1239 							actual += sizeof(*sin);
1240 #ifdef INET6
1241 						}
1242 #endif
1243 						if (actual >= limit) {
1244 							return (actual);
1245 						}
1246 					} else {
1247 						continue;
1248 					}
1249 					break;
1250 #ifdef INET6
1251 				case AF_INET6:
1252 					if (ipv6_addr_legal) {
1253 						struct sockaddr_in6 *sin6;
1254 
1255 						sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1256 						if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1257 							/*
1258 							 * we skip
1259 							 * unspecifed
1260 							 * addresses
1261 							 */
1262 							continue;
1263 						}
1264 						if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1265 							if (local_scope == 0)
1266 								continue;
1267 							if (sin6->sin6_scope_id == 0) {
1268 								if (sa6_recoverscope(sin6) != 0)
1269 									/*
1270 									 *
1271 									 * bad
1272 									 *
1273 									 * li
1274 									 * nk
1275 									 *
1276 									 * loc
1277 									 * al
1278 									 *
1279 									 * add
1280 									 * re
1281 									 * ss
1282 									 * */
1283 									continue;
1284 							}
1285 						}
1286 						if ((site_scope == 0) &&
1287 						    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1288 							continue;
1289 						}
1290 						memcpy(sas, sin6, sizeof(*sin6));
1291 						((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1292 						sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1293 						actual += sizeof(*sin6);
1294 						if (actual >= limit) {
1295 							return (actual);
1296 						}
1297 					} else {
1298 						continue;
1299 					}
1300 					break;
1301 #endif
1302 				default:
1303 					/* TSNH */
1304 					break;
1305 				}
1306 			}
1307 		}
1308 	} else {
1309 		struct sctp_laddr *laddr;
1310 
1311 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1312 			if (stcb) {
1313 				if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1314 					continue;
1315 				}
1316 			}
1317 			if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1318 				continue;
1319 
1320 			((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1321 			sas = (struct sockaddr_storage *)((caddr_t)sas +
1322 			    laddr->ifa->address.sa.sa_len);
1323 			actual += laddr->ifa->address.sa.sa_len;
1324 			if (actual >= limit) {
1325 				return (actual);
1326 			}
1327 		}
1328 	}
1329 	return (actual);
1330 }
1331 
1332 static size_t
1333 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1334     struct sctp_tcb *stcb,
1335     size_t limit,
1336     struct sockaddr_storage *sas)
1337 {
1338 	size_t size = 0;
1339 
1340 	SCTP_IPI_ADDR_RLOCK();
1341 	/* fill up addresses for the endpoint's default vrf */
1342 	size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1343 	    inp->def_vrf_id);
1344 	SCTP_IPI_ADDR_RUNLOCK();
1345 	return (size);
1346 }
1347 
1348 /*
1349  * NOTE: assumes addr lock is held
1350  */
1351 static int
1352 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1353 {
1354 	int cnt = 0;
1355 	struct sctp_vrf *vrf = NULL;
1356 
1357 	/*
1358 	 * In both sub-set bound an bound_all cases we return the MAXIMUM
1359 	 * number of addresses that you COULD get. In reality the sub-set
1360 	 * bound may have an exclusion list for a given TCB OR in the
1361 	 * bound-all case a TCB may NOT include the loopback or other
1362 	 * addresses as well.
1363 	 */
1364 	vrf = sctp_find_vrf(vrf_id);
1365 	if (vrf == NULL) {
1366 		return (0);
1367 	}
1368 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1369 		struct sctp_ifn *sctp_ifn;
1370 		struct sctp_ifa *sctp_ifa;
1371 
1372 		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1373 			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1374 				/* Count them if they are the right type */
1375 				if (sctp_ifa->address.sa.sa_family == AF_INET) {
1376 					if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1377 						cnt += sizeof(struct sockaddr_in6);
1378 					else
1379 						cnt += sizeof(struct sockaddr_in);
1380 
1381 				} else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1382 					cnt += sizeof(struct sockaddr_in6);
1383 			}
1384 		}
1385 	} else {
1386 		struct sctp_laddr *laddr;
1387 
1388 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1389 			if (laddr->ifa->address.sa.sa_family == AF_INET) {
1390 				if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1391 					cnt += sizeof(struct sockaddr_in6);
1392 				else
1393 					cnt += sizeof(struct sockaddr_in);
1394 
1395 			} else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1396 				cnt += sizeof(struct sockaddr_in6);
1397 		}
1398 	}
1399 	return (cnt);
1400 }
1401 
1402 static int
1403 sctp_count_max_addresses(struct sctp_inpcb *inp)
1404 {
1405 	int cnt = 0;
1406 
1407 	SCTP_IPI_ADDR_RLOCK();
1408 	/* count addresses for the endpoint's default VRF */
1409 	cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1410 	SCTP_IPI_ADDR_RUNLOCK();
1411 	return (cnt);
1412 }
1413 
1414 static int
1415 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1416     size_t optsize, void *p, int delay)
1417 {
1418 	int error = 0;
1419 	int creat_lock_on = 0;
1420 	struct sctp_tcb *stcb = NULL;
1421 	struct sockaddr *sa;
1422 	int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1423 	int added = 0;
1424 	uint32_t vrf_id;
1425 	int bad_addresses = 0;
1426 	sctp_assoc_t *a_id;
1427 
1428 	SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1429 
1430 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1431 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1432 		/* We are already connected AND the TCP model */
1433 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1434 		return (EADDRINUSE);
1435 	}
1436 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
1437 	    (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1438 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1439 		return (EINVAL);
1440 	}
1441 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1442 		SCTP_INP_RLOCK(inp);
1443 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1444 		SCTP_INP_RUNLOCK(inp);
1445 	}
1446 	if (stcb) {
1447 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1448 		return (EALREADY);
1449 	}
1450 	SCTP_INP_INCR_REF(inp);
1451 	SCTP_ASOC_CREATE_LOCK(inp);
1452 	creat_lock_on = 1;
1453 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1454 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1455 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1456 		error = EFAULT;
1457 		goto out_now;
1458 	}
1459 	totaddrp = (int *)optval;
1460 	totaddr = *totaddrp;
1461 	sa = (struct sockaddr *)(totaddrp + 1);
1462 	stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1463 	if ((stcb != NULL) || bad_addresses) {
1464 		/* Already have or am bring up an association */
1465 		SCTP_ASOC_CREATE_UNLOCK(inp);
1466 		creat_lock_on = 0;
1467 		if (stcb)
1468 			SCTP_TCB_UNLOCK(stcb);
1469 		if (bad_addresses == 0) {
1470 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1471 			error = EALREADY;
1472 		}
1473 		goto out_now;
1474 	}
1475 #ifdef INET6
1476 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1477 	    (num_v6 > 0)) {
1478 		error = EINVAL;
1479 		goto out_now;
1480 	}
1481 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1482 	    (num_v4 > 0)) {
1483 		struct in6pcb *inp6;
1484 
1485 		inp6 = (struct in6pcb *)inp;
1486 		if (SCTP_IPV6_V6ONLY(inp6)) {
1487 			/*
1488 			 * if IPV6_V6ONLY flag, ignore connections destined
1489 			 * to a v4 addr or v4-mapped addr
1490 			 */
1491 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1492 			error = EINVAL;
1493 			goto out_now;
1494 		}
1495 	}
1496 #endif				/* INET6 */
1497 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1498 	    SCTP_PCB_FLAGS_UNBOUND) {
1499 		/* Bind a ephemeral port */
1500 		error = sctp_inpcb_bind(so, NULL, NULL, p);
1501 		if (error) {
1502 			goto out_now;
1503 		}
1504 	}
1505 	/* FIX ME: do we want to pass in a vrf on the connect call? */
1506 	vrf_id = inp->def_vrf_id;
1507 
1508 
1509 	/* We are GOOD to go */
1510 	stcb = sctp_aloc_assoc(inp, sa, &error, 0, vrf_id,
1511 	    (struct thread *)p
1512 	    );
1513 	if (stcb == NULL) {
1514 		/* Gak! no memory */
1515 		goto out_now;
1516 	}
1517 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1518 	/* move to second address */
1519 	if (sa->sa_family == AF_INET)
1520 		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1521 	else
1522 		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1523 
1524 	error = 0;
1525 	added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1526 	/* Fill in the return id */
1527 	if (error) {
1528 		(void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1529 		goto out_now;
1530 	}
1531 	a_id = (sctp_assoc_t *) optval;
1532 	*a_id = sctp_get_associd(stcb);
1533 
1534 	/* initialize authentication parameters for the assoc */
1535 	sctp_initialize_auth_params(inp, stcb);
1536 
1537 	if (delay) {
1538 		/* doing delayed connection */
1539 		stcb->asoc.delayed_connection = 1;
1540 		sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1541 	} else {
1542 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1543 		sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1544 	}
1545 	SCTP_TCB_UNLOCK(stcb);
1546 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1547 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1548 		/* Set the connected flag so we can queue data */
1549 		soisconnecting(so);
1550 	}
1551 out_now:
1552 	if (creat_lock_on) {
1553 		SCTP_ASOC_CREATE_UNLOCK(inp);
1554 	}
1555 	SCTP_INP_DECR_REF(inp);
1556 	return error;
1557 }
1558 
1559 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1560 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1561 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1562 		SCTP_INP_RLOCK(inp); \
1563 		stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1564 		if (stcb) { \
1565 			SCTP_TCB_LOCK(stcb); \
1566                 } \
1567 		SCTP_INP_RUNLOCK(inp); \
1568 	} else if (assoc_id != 0) { \
1569 		stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1570 		if (stcb == NULL) { \
1571 		        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1572 			error = ENOENT; \
1573 			break; \
1574 		} \
1575 	} else { \
1576 		stcb = NULL; \
1577         } \
1578   }
1579 
1580 
1581 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size)  {\
1582 	if (size < sizeof(type)) { \
1583 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1584 		error = EINVAL; \
1585 		break; \
1586 	} else { \
1587 		destp = (type *)srcp; \
1588 	} \
1589       }
1590 
1591 static int
1592 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1593     void *p)
1594 {
1595 	struct sctp_inpcb *inp = NULL;
1596 	int error, val = 0;
1597 	struct sctp_tcb *stcb = NULL;
1598 
1599 	if (optval == NULL) {
1600 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1601 		return (EINVAL);
1602 	}
1603 	inp = (struct sctp_inpcb *)so->so_pcb;
1604 	if (inp == 0) {
1605 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1606 		return EINVAL;
1607 	}
1608 	error = 0;
1609 
1610 	switch (optname) {
1611 	case SCTP_NODELAY:
1612 	case SCTP_AUTOCLOSE:
1613 	case SCTP_EXPLICIT_EOR:
1614 	case SCTP_AUTO_ASCONF:
1615 	case SCTP_DISABLE_FRAGMENTS:
1616 	case SCTP_I_WANT_MAPPED_V4_ADDR:
1617 	case SCTP_USE_EXT_RCVINFO:
1618 		SCTP_INP_RLOCK(inp);
1619 		switch (optname) {
1620 		case SCTP_DISABLE_FRAGMENTS:
1621 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1622 			break;
1623 		case SCTP_I_WANT_MAPPED_V4_ADDR:
1624 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1625 			break;
1626 		case SCTP_AUTO_ASCONF:
1627 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1628 				/* only valid for bound all sockets */
1629 				val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1630 			} else {
1631 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1632 				error = EINVAL;
1633 				goto flags_out;
1634 			}
1635 			break;
1636 		case SCTP_EXPLICIT_EOR:
1637 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1638 			break;
1639 		case SCTP_NODELAY:
1640 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1641 			break;
1642 		case SCTP_USE_EXT_RCVINFO:
1643 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1644 			break;
1645 		case SCTP_AUTOCLOSE:
1646 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1647 				val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1648 			else
1649 				val = 0;
1650 			break;
1651 
1652 		default:
1653 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1654 			error = ENOPROTOOPT;
1655 		}		/* end switch (sopt->sopt_name) */
1656 		if (optname != SCTP_AUTOCLOSE) {
1657 			/* make it an "on/off" value */
1658 			val = (val != 0);
1659 		}
1660 		if (*optsize < sizeof(val)) {
1661 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1662 			error = EINVAL;
1663 		}
1664 flags_out:
1665 		SCTP_INP_RUNLOCK(inp);
1666 		if (error == 0) {
1667 			/* return the option value */
1668 			*(int *)optval = val;
1669 			*optsize = sizeof(val);
1670 		}
1671 		break;
1672 	case SCTP_GET_PACKET_LOG:
1673 		{
1674 #ifdef  SCTP_PACKET_LOGGING
1675 			uint8_t *target;
1676 			int ret;
1677 
1678 			SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1679 			ret = sctp_copy_out_packet_log(target, (int)*optsize);
1680 			*optsize = ret;
1681 #else
1682 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1683 			error = EOPNOTSUPP;
1684 #endif
1685 			break;
1686 		}
1687 	case SCTP_REUSE_PORT:
1688 		{
1689 			uint32_t *value;
1690 
1691 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1692 				/* Can't do this for a 1-m socket */
1693 				error = EINVAL;
1694 				break;
1695 			}
1696 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1697 			*value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1698 			*optsize = sizeof(uint32_t);
1699 		}
1700 		break;
1701 	case SCTP_PARTIAL_DELIVERY_POINT:
1702 		{
1703 			uint32_t *value;
1704 
1705 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1706 			*value = inp->partial_delivery_point;
1707 			*optsize = sizeof(uint32_t);
1708 		}
1709 		break;
1710 	case SCTP_FRAGMENT_INTERLEAVE:
1711 		{
1712 			uint32_t *value;
1713 
1714 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1715 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1716 				if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1717 					*value = SCTP_FRAG_LEVEL_2;
1718 				} else {
1719 					*value = SCTP_FRAG_LEVEL_1;
1720 				}
1721 			} else {
1722 				*value = SCTP_FRAG_LEVEL_0;
1723 			}
1724 			*optsize = sizeof(uint32_t);
1725 		}
1726 		break;
1727 	case SCTP_CMT_ON_OFF:
1728 		{
1729 			struct sctp_assoc_value *av;
1730 
1731 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1732 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1733 			if (stcb) {
1734 				av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1735 				SCTP_TCB_UNLOCK(stcb);
1736 			} else {
1737 				SCTP_INP_RLOCK(inp);
1738 				av->assoc_value = inp->sctp_cmt_on_off;
1739 				SCTP_INP_RUNLOCK(inp);
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_event = 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 		if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
2815 			struct sctp_assoc_value *av;
2816 
2817 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2818 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2819 			if (stcb) {
2820 				if (av->assoc_value != 0)
2821 					stcb->asoc.sctp_cmt_on_off = 1;
2822 				else
2823 					stcb->asoc.sctp_cmt_on_off = 0;
2824 				SCTP_TCB_UNLOCK(stcb);
2825 			} else {
2826 				SCTP_INP_WLOCK(inp);
2827 				if (av->assoc_value != 0)
2828 					inp->sctp_cmt_on_off = 1;
2829 				else
2830 					inp->sctp_cmt_on_off = 0;
2831 				SCTP_INP_WUNLOCK(inp);
2832 			}
2833 		} else {
2834 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2835 			error = ENOPROTOOPT;
2836 		}
2837 		break;
2838 		/* JRS - Set socket option for pluggable congestion control */
2839 	case SCTP_PLUGGABLE_CC:
2840 		{
2841 			struct sctp_assoc_value *av;
2842 
2843 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2844 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2845 			if (stcb) {
2846 				switch (av->assoc_value) {
2847 					/*
2848 					 * JRS - Standard TCP congestion
2849 					 * control
2850 					 */
2851 				case SCTP_CC_RFC2581:
2852 					{
2853 						stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
2854 						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2855 						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
2856 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
2857 						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2858 						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2859 						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2860 						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2861 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2862 						SCTP_TCB_UNLOCK(stcb);
2863 						break;
2864 					}
2865 					/*
2866 					 * JRS - High Speed TCP congestion
2867 					 * control (Floyd)
2868 					 */
2869 				case SCTP_CC_HSTCP:
2870 					{
2871 						stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
2872 						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2873 						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
2874 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
2875 						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2876 						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2877 						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2878 						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2879 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2880 						SCTP_TCB_UNLOCK(stcb);
2881 						break;
2882 					}
2883 					/* JRS - HTCP congestion control */
2884 				case SCTP_CC_HTCP:
2885 					{
2886 						stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
2887 						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
2888 						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
2889 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
2890 						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
2891 						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
2892 						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2893 						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2894 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
2895 						SCTP_TCB_UNLOCK(stcb);
2896 						break;
2897 					}
2898 					/*
2899 					 * JRS - All other values are
2900 					 * invalid
2901 					 */
2902 				default:
2903 					{
2904 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2905 						error = EINVAL;
2906 						SCTP_TCB_UNLOCK(stcb);
2907 						break;
2908 					}
2909 				}
2910 			} else {
2911 				switch (av->assoc_value) {
2912 				case SCTP_CC_RFC2581:
2913 				case SCTP_CC_HSTCP:
2914 				case SCTP_CC_HTCP:
2915 					inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2916 					break;
2917 				default:
2918 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2919 					error = EINVAL;
2920 					break;
2921 				};
2922 			}
2923 		}
2924 		break;
2925 	case SCTP_CLR_STAT_LOG:
2926 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2927 		error = EOPNOTSUPP;
2928 		break;
2929 	case SCTP_CONTEXT:
2930 		{
2931 			struct sctp_assoc_value *av;
2932 
2933 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2934 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2935 
2936 			if (stcb) {
2937 				stcb->asoc.context = av->assoc_value;
2938 				SCTP_TCB_UNLOCK(stcb);
2939 			} else {
2940 				SCTP_INP_WLOCK(inp);
2941 				inp->sctp_context = av->assoc_value;
2942 				SCTP_INP_WUNLOCK(inp);
2943 			}
2944 		}
2945 		break;
2946 	case SCTP_VRF_ID:
2947 		{
2948 			uint32_t *default_vrfid;
2949 
2950 			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2951 			if (*default_vrfid > SCTP_MAX_VRF_ID) {
2952 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2953 				error = EINVAL;
2954 				break;
2955 			}
2956 			inp->def_vrf_id = *default_vrfid;
2957 			break;
2958 		}
2959 	case SCTP_DEL_VRF_ID:
2960 		{
2961 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2962 			error = EOPNOTSUPP;
2963 			break;
2964 		}
2965 	case SCTP_ADD_VRF_ID:
2966 		{
2967 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2968 			error = EOPNOTSUPP;
2969 			break;
2970 		}
2971 	case SCTP_DELAYED_SACK:
2972 		{
2973 			struct sctp_sack_info *sack;
2974 
2975 			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
2976 			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2977 			if (sack->sack_delay) {
2978 				if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
2979 					sack->sack_delay = SCTP_MAX_SACK_DELAY;
2980 			}
2981 			if (stcb) {
2982 				if (sack->sack_delay) {
2983 					if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2984 						sack->sack_delay = TICKS_TO_MSEC(1);
2985 					}
2986 					stcb->asoc.delayed_ack = sack->sack_delay;
2987 				}
2988 				if (sack->sack_freq) {
2989 					stcb->asoc.sack_freq = sack->sack_freq;
2990 				}
2991 				SCTP_TCB_UNLOCK(stcb);
2992 			} else {
2993 				SCTP_INP_WLOCK(inp);
2994 				if (sack->sack_delay) {
2995 					if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2996 						sack->sack_delay = TICKS_TO_MSEC(1);
2997 					}
2998 					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
2999 				}
3000 				if (sack->sack_freq) {
3001 					inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
3002 				}
3003 				SCTP_INP_WUNLOCK(inp);
3004 			}
3005 			break;
3006 		}
3007 	case SCTP_AUTH_CHUNK:
3008 		{
3009 			struct sctp_authchunk *sauth;
3010 
3011 			SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
3012 
3013 			SCTP_INP_WLOCK(inp);
3014 			if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
3015 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3016 				error = EINVAL;
3017 			}
3018 			SCTP_INP_WUNLOCK(inp);
3019 			break;
3020 		}
3021 	case SCTP_AUTH_KEY:
3022 		{
3023 			struct sctp_authkey *sca;
3024 			struct sctp_keyhead *shared_keys;
3025 			sctp_sharedkey_t *shared_key;
3026 			sctp_key_t *key = NULL;
3027 			size_t size;
3028 
3029 			SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
3030 			SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
3031 			size = optsize - sizeof(*sca);
3032 
3033 			if (stcb) {
3034 				/* set it on the assoc */
3035 				shared_keys = &stcb->asoc.shared_keys;
3036 				/* clear the cached keys for this key id */
3037 				sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
3038 				/*
3039 				 * create the new shared key and
3040 				 * insert/replace it
3041 				 */
3042 				if (size > 0) {
3043 					key = sctp_set_key(sca->sca_key, (uint32_t) size);
3044 					if (key == NULL) {
3045 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3046 						error = ENOMEM;
3047 						SCTP_TCB_UNLOCK(stcb);
3048 						break;
3049 					}
3050 				}
3051 				shared_key = sctp_alloc_sharedkey();
3052 				if (shared_key == NULL) {
3053 					sctp_free_key(key);
3054 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3055 					error = ENOMEM;
3056 					SCTP_TCB_UNLOCK(stcb);
3057 					break;
3058 				}
3059 				shared_key->key = key;
3060 				shared_key->keyid = sca->sca_keynumber;
3061 				error = sctp_insert_sharedkey(shared_keys, shared_key);
3062 				SCTP_TCB_UNLOCK(stcb);
3063 			} else {
3064 				/* set it on the endpoint */
3065 				SCTP_INP_WLOCK(inp);
3066 				shared_keys = &inp->sctp_ep.shared_keys;
3067 				/*
3068 				 * clear the cached keys on all assocs for
3069 				 * this key id
3070 				 */
3071 				sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
3072 				/*
3073 				 * create the new shared key and
3074 				 * insert/replace it
3075 				 */
3076 				if (size > 0) {
3077 					key = sctp_set_key(sca->sca_key, (uint32_t) size);
3078 					if (key == NULL) {
3079 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3080 						error = ENOMEM;
3081 						SCTP_INP_WUNLOCK(inp);
3082 						break;
3083 					}
3084 				}
3085 				shared_key = sctp_alloc_sharedkey();
3086 				if (shared_key == NULL) {
3087 					sctp_free_key(key);
3088 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3089 					error = ENOMEM;
3090 					SCTP_INP_WUNLOCK(inp);
3091 					break;
3092 				}
3093 				shared_key->key = key;
3094 				shared_key->keyid = sca->sca_keynumber;
3095 				error = sctp_insert_sharedkey(shared_keys, shared_key);
3096 				SCTP_INP_WUNLOCK(inp);
3097 			}
3098 			break;
3099 		}
3100 	case SCTP_HMAC_IDENT:
3101 		{
3102 			struct sctp_hmacalgo *shmac;
3103 			sctp_hmaclist_t *hmaclist;
3104 			uint16_t hmacid;
3105 			uint32_t i;
3106 
3107 			size_t found;
3108 
3109 			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
3110 			if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) {
3111 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3112 				error = EINVAL;
3113 				break;
3114 			}
3115 			hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents);
3116 			if (hmaclist == NULL) {
3117 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3118 				error = ENOMEM;
3119 				break;
3120 			}
3121 			for (i = 0; i < shmac->shmac_number_of_idents; i++) {
3122 				hmacid = shmac->shmac_idents[i];
3123 				if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
3124 					 /* invalid HMACs were found */ ;
3125 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3126 					error = EINVAL;
3127 					sctp_free_hmaclist(hmaclist);
3128 					goto sctp_set_hmac_done;
3129 				}
3130 			}
3131 			found = 0;
3132 			for (i = 0; i < hmaclist->num_algo; i++) {
3133 				if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
3134 					/* already in list */
3135 					found = 1;
3136 				}
3137 			}
3138 			if (!found) {
3139 				sctp_free_hmaclist(hmaclist);
3140 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3141 				error = EINVAL;
3142 				break;
3143 			}
3144 			/* set it on the endpoint */
3145 			SCTP_INP_WLOCK(inp);
3146 			if (inp->sctp_ep.local_hmacs)
3147 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3148 			inp->sctp_ep.local_hmacs = hmaclist;
3149 			SCTP_INP_WUNLOCK(inp);
3150 	sctp_set_hmac_done:
3151 			break;
3152 		}
3153 	case SCTP_AUTH_ACTIVE_KEY:
3154 		{
3155 			struct sctp_authkeyid *scact;
3156 
3157 			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid,
3158 			    optsize);
3159 			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3160 
3161 			/* set the active key on the right place */
3162 			if (stcb) {
3163 				/* set the active key on the assoc */
3164 				if (sctp_auth_setactivekey(stcb,
3165 				    scact->scact_keynumber)) {
3166 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3167 					    SCTP_FROM_SCTP_USRREQ,
3168 					    EINVAL);
3169 					error = EINVAL;
3170 				}
3171 				SCTP_TCB_UNLOCK(stcb);
3172 			} else {
3173 				/* set the active key on the endpoint */
3174 				SCTP_INP_WLOCK(inp);
3175 				if (sctp_auth_setactivekey_ep(inp,
3176 				    scact->scact_keynumber)) {
3177 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3178 					    SCTP_FROM_SCTP_USRREQ,
3179 					    EINVAL);
3180 					error = EINVAL;
3181 				}
3182 				SCTP_INP_WUNLOCK(inp);
3183 			}
3184 			break;
3185 		}
3186 	case SCTP_AUTH_DELETE_KEY:
3187 		{
3188 			struct sctp_authkeyid *scdel;
3189 
3190 			SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid,
3191 			    optsize);
3192 			SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3193 
3194 			/* delete the key from the right place */
3195 			if (stcb) {
3196 				if (sctp_delete_sharedkey(stcb,
3197 				    scdel->scact_keynumber)) {
3198 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3199 					    SCTP_FROM_SCTP_USRREQ,
3200 					    EINVAL);
3201 					error = EINVAL;
3202 				}
3203 				SCTP_TCB_UNLOCK(stcb);
3204 			} else {
3205 				SCTP_INP_WLOCK(inp);
3206 				if (sctp_delete_sharedkey_ep(inp,
3207 				    scdel->scact_keynumber)) {
3208 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3209 					    SCTP_FROM_SCTP_USRREQ,
3210 					    EINVAL);
3211 					error = EINVAL;
3212 				}
3213 				SCTP_INP_WUNLOCK(inp);
3214 			}
3215 			break;
3216 		}
3217 	case SCTP_AUTH_DEACTIVATE_KEY:
3218 		{
3219 			struct sctp_authkeyid *keyid;
3220 
3221 			SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid,
3222 			    optsize);
3223 			SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
3224 
3225 			/* deactivate the key from the right place */
3226 			if (stcb) {
3227 				if (sctp_deact_sharedkey(stcb,
3228 				    keyid->scact_keynumber)) {
3229 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3230 					    SCTP_FROM_SCTP_USRREQ,
3231 					    EINVAL);
3232 					error = EINVAL;
3233 				}
3234 				SCTP_TCB_UNLOCK(stcb);
3235 			} else {
3236 				SCTP_INP_WLOCK(inp);
3237 				if (sctp_deact_sharedkey_ep(inp,
3238 				    keyid->scact_keynumber)) {
3239 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3240 					    SCTP_FROM_SCTP_USRREQ,
3241 					    EINVAL);
3242 					error = EINVAL;
3243 				}
3244 				SCTP_INP_WUNLOCK(inp);
3245 			}
3246 			break;
3247 		}
3248 
3249 	case SCTP_RESET_STREAMS:
3250 		{
3251 			struct sctp_stream_reset *strrst;
3252 			uint8_t send_in = 0, send_tsn = 0, send_out = 0,
3253 			        addstream = 0;
3254 			uint16_t addstrmcnt = 0;
3255 			int i;
3256 
3257 			SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
3258 			SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
3259 
3260 			if (stcb == NULL) {
3261 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3262 				error = ENOENT;
3263 				break;
3264 			}
3265 			if (stcb->asoc.peer_supports_strreset == 0) {
3266 				/*
3267 				 * Peer does not support it, we return
3268 				 * protocol not supported since this is true
3269 				 * for this feature and this peer, not the
3270 				 * socket request in general.
3271 				 */
3272 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
3273 				error = EPROTONOSUPPORT;
3274 				SCTP_TCB_UNLOCK(stcb);
3275 				break;
3276 			}
3277 			if (stcb->asoc.stream_reset_outstanding) {
3278 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3279 				error = EALREADY;
3280 				SCTP_TCB_UNLOCK(stcb);
3281 				break;
3282 			}
3283 			if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
3284 				send_in = 1;
3285 			} else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
3286 				send_out = 1;
3287 			} else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
3288 				send_in = 1;
3289 				send_out = 1;
3290 			} else if (strrst->strrst_flags == SCTP_RESET_TSN) {
3291 				send_tsn = 1;
3292 			} else if (strrst->strrst_flags == SCTP_RESET_ADD_STREAMS) {
3293 				if (send_tsn ||
3294 				    send_in ||
3295 				    send_out) {
3296 					/* We can't do that and add streams */
3297 					error = EINVAL;
3298 					goto skip_stuff;
3299 				}
3300 				if (stcb->asoc.stream_reset_outstanding) {
3301 					error = EBUSY;
3302 					goto skip_stuff;
3303 				}
3304 				addstream = 1;
3305 				/* We allocate here */
3306 				addstrmcnt = strrst->strrst_num_streams;
3307 				if ((int)(addstrmcnt + stcb->asoc.streamoutcnt) > 0xffff) {
3308 					/* You can't have more than 64k */
3309 					error = EINVAL;
3310 					goto skip_stuff;
3311 				}
3312 				if ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < addstrmcnt) {
3313 					/* Need to allocate more */
3314 					struct sctp_stream_out *oldstream;
3315 					struct sctp_stream_queue_pending *sp;
3316 					int removed;
3317 
3318 					oldstream = stcb->asoc.strmout;
3319 					/* get some more */
3320 					SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
3321 					    ((stcb->asoc.streamoutcnt + addstrmcnt) * sizeof(struct sctp_stream_out)),
3322 					    SCTP_M_STRMO);
3323 					if (stcb->asoc.strmout == NULL) {
3324 						stcb->asoc.strmout = oldstream;
3325 						error = ENOMEM;
3326 						goto skip_stuff;
3327 					}
3328 					/*
3329 					 * Ok now we proceed with copying
3330 					 * the old out stuff and
3331 					 * initializing the new stuff.
3332 					 */
3333 					SCTP_TCB_SEND_LOCK(stcb);
3334 					for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3335 						TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3336 						stcb->asoc.strmout[i].next_sequence_sent = oldstream[i].next_sequence_sent;
3337 						stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
3338 						stcb->asoc.strmout[i].stream_no = i;
3339 						if (oldstream[i].next_spoke.tqe_next) {
3340 							sctp_remove_from_wheel(stcb, &stcb->asoc, &oldstream[i], 1);
3341 							stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3342 							stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3343 							removed = 1;
3344 						} else {
3345 							/* not on out wheel */
3346 							stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3347 							stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3348 							removed = 0;
3349 						}
3350 						/*
3351 						 * now anything on those
3352 						 * queues?
3353 						 */
3354 						while (TAILQ_EMPTY(&oldstream[i].outqueue) == 0) {
3355 							sp = TAILQ_FIRST(&oldstream[i].outqueue);
3356 							TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
3357 							TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
3358 						}
3359 						/* Did we disrupt the wheel? */
3360 						if (removed) {
3361 							sctp_insert_on_wheel(stcb,
3362 							    &stcb->asoc,
3363 							    &stcb->asoc.strmout[i],
3364 							    1);
3365 						}
3366 						/*
3367 						 * Now move assoc pointers
3368 						 * too
3369 						 */
3370 						if (stcb->asoc.last_out_stream == &oldstream[i]) {
3371 							stcb->asoc.last_out_stream = &stcb->asoc.strmout[i];
3372 						}
3373 						if (stcb->asoc.locked_on_sending == &oldstream[i]) {
3374 							stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i];
3375 						}
3376 					}
3377 					/* now the new streams */
3378 					for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + addstrmcnt); i++) {
3379 						stcb->asoc.strmout[i].next_sequence_sent = 0x0;
3380 						TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3381 						stcb->asoc.strmout[i].stream_no = i;
3382 						stcb->asoc.strmout[i].last_msg_incomplete = 0;
3383 						stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3384 						stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3385 					}
3386 					stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + addstrmcnt;
3387 					SCTP_FREE(oldstream, SCTP_M_STRMO);
3388 				}
3389 				SCTP_TCB_SEND_UNLOCK(stcb);
3390 				goto skip_stuff;
3391 			} else {
3392 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3393 				error = EINVAL;
3394 				SCTP_TCB_UNLOCK(stcb);
3395 				break;
3396 			}
3397 			for (i = 0; i < strrst->strrst_num_streams; i++) {
3398 				if ((send_in) &&
3399 
3400 				    (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
3401 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3402 					error = EINVAL;
3403 					goto get_out;
3404 				}
3405 				if ((send_out) &&
3406 				    (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
3407 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3408 					error = EINVAL;
3409 					goto get_out;
3410 				}
3411 			}
3412 	skip_stuff:
3413 			if (error) {
3414 		get_out:
3415 				SCTP_TCB_UNLOCK(stcb);
3416 				break;
3417 			}
3418 			error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
3419 			    strrst->strrst_list,
3420 			    send_out, (stcb->asoc.str_reset_seq_in - 3),
3421 			    send_in, send_tsn, addstream, addstrmcnt);
3422 
3423 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
3424 			SCTP_TCB_UNLOCK(stcb);
3425 		}
3426 		break;
3427 
3428 	case SCTP_CONNECT_X:
3429 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3430 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3431 			error = EINVAL;
3432 			break;
3433 		}
3434 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
3435 		break;
3436 
3437 	case SCTP_CONNECT_X_DELAYED:
3438 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3439 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3440 			error = EINVAL;
3441 			break;
3442 		}
3443 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
3444 		break;
3445 
3446 	case SCTP_CONNECT_X_COMPLETE:
3447 		{
3448 			struct sockaddr *sa;
3449 			struct sctp_nets *net;
3450 
3451 			/* FIXME MT: check correct? */
3452 			SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
3453 
3454 			/* find tcb */
3455 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3456 				SCTP_INP_RLOCK(inp);
3457 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
3458 				if (stcb) {
3459 					SCTP_TCB_LOCK(stcb);
3460 					net = sctp_findnet(stcb, sa);
3461 				}
3462 				SCTP_INP_RUNLOCK(inp);
3463 			} else {
3464 				/*
3465 				 * We increment here since
3466 				 * sctp_findassociation_ep_addr() wil do a
3467 				 * decrement if it finds the stcb as long as
3468 				 * the locked tcb (last argument) is NOT a
3469 				 * TCB.. aka NULL.
3470 				 */
3471 				SCTP_INP_INCR_REF(inp);
3472 				stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3473 				if (stcb == NULL) {
3474 					SCTP_INP_DECR_REF(inp);
3475 				}
3476 			}
3477 
3478 			if (stcb == NULL) {
3479 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3480 				error = ENOENT;
3481 				break;
3482 			}
3483 			if (stcb->asoc.delayed_connection == 1) {
3484 				stcb->asoc.delayed_connection = 0;
3485 				(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3486 				sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3487 				    stcb->asoc.primary_destination,
3488 				    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3489 				sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
3490 			} else {
3491 				/*
3492 				 * already expired or did not use delayed
3493 				 * connectx
3494 				 */
3495 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3496 				error = EALREADY;
3497 			}
3498 			SCTP_TCB_UNLOCK(stcb);
3499 		}
3500 		break;
3501 	case SCTP_MAX_BURST:
3502 		{
3503 			uint8_t *burst;
3504 
3505 			SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3506 
3507 			SCTP_INP_WLOCK(inp);
3508 			if (*burst) {
3509 				inp->sctp_ep.max_burst = *burst;
3510 			}
3511 			SCTP_INP_WUNLOCK(inp);
3512 		}
3513 		break;
3514 	case SCTP_MAXSEG:
3515 		{
3516 			struct sctp_assoc_value *av;
3517 			int ovh;
3518 
3519 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3520 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3521 
3522 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3523 				ovh = SCTP_MED_OVERHEAD;
3524 			} else {
3525 				ovh = SCTP_MED_V4_OVERHEAD;
3526 			}
3527 			if (stcb) {
3528 				if (av->assoc_value) {
3529 					stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3530 				} else {
3531 					stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3532 				}
3533 				SCTP_TCB_UNLOCK(stcb);
3534 			} else {
3535 				SCTP_INP_WLOCK(inp);
3536 				/*
3537 				 * FIXME MT: I think this is not in tune
3538 				 * with the API ID
3539 				 */
3540 				if (av->assoc_value) {
3541 					inp->sctp_frag_point = (av->assoc_value + ovh);
3542 				} else {
3543 					inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3544 				}
3545 				SCTP_INP_WUNLOCK(inp);
3546 			}
3547 		}
3548 		break;
3549 	case SCTP_EVENTS:
3550 		{
3551 			struct sctp_event_subscribe *events;
3552 
3553 			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3554 
3555 			SCTP_INP_WLOCK(inp);
3556 			if (events->sctp_data_io_event) {
3557 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3558 			} else {
3559 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3560 			}
3561 
3562 			if (events->sctp_association_event) {
3563 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3564 			} else {
3565 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3566 			}
3567 
3568 			if (events->sctp_address_event) {
3569 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3570 			} else {
3571 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3572 			}
3573 
3574 			if (events->sctp_send_failure_event) {
3575 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3576 			} else {
3577 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3578 			}
3579 
3580 			if (events->sctp_peer_error_event) {
3581 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3582 			} else {
3583 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3584 			}
3585 
3586 			if (events->sctp_shutdown_event) {
3587 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3588 			} else {
3589 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3590 			}
3591 
3592 			if (events->sctp_partial_delivery_event) {
3593 				sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3594 			} else {
3595 				sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3596 			}
3597 
3598 			if (events->sctp_adaptation_layer_event) {
3599 				sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3600 			} else {
3601 				sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3602 			}
3603 
3604 			if (events->sctp_authentication_event) {
3605 				sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3606 			} else {
3607 				sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3608 			}
3609 
3610 			if (events->sctp_sender_dry_event) {
3611 				sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
3612 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3613 				    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3614 					stcb = LIST_FIRST(&inp->sctp_asoc_list);
3615 					if (stcb) {
3616 						SCTP_TCB_LOCK(stcb);
3617 					}
3618 					if (stcb &&
3619 					    TAILQ_EMPTY(&stcb->asoc.send_queue) &&
3620 					    TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
3621 					    (stcb->asoc.stream_queue_cnt == 0)) {
3622 						sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
3623 					}
3624 					if (stcb) {
3625 						SCTP_TCB_UNLOCK(stcb);
3626 					}
3627 				}
3628 			} else {
3629 				sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
3630 			}
3631 
3632 			if (events->sctp_stream_reset_event) {
3633 				sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3634 			} else {
3635 				sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3636 			}
3637 			SCTP_INP_WUNLOCK(inp);
3638 		}
3639 		break;
3640 
3641 	case SCTP_ADAPTATION_LAYER:
3642 		{
3643 			struct sctp_setadaptation *adap_bits;
3644 
3645 			SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3646 			SCTP_INP_WLOCK(inp);
3647 			inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3648 			SCTP_INP_WUNLOCK(inp);
3649 		}
3650 		break;
3651 #ifdef SCTP_DEBUG
3652 	case SCTP_SET_INITIAL_DBG_SEQ:
3653 		{
3654 			uint32_t *vvv;
3655 
3656 			SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3657 			SCTP_INP_WLOCK(inp);
3658 			inp->sctp_ep.initial_sequence_debug = *vvv;
3659 			SCTP_INP_WUNLOCK(inp);
3660 		}
3661 		break;
3662 #endif
3663 	case SCTP_DEFAULT_SEND_PARAM:
3664 		{
3665 			struct sctp_sndrcvinfo *s_info;
3666 
3667 			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3668 			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3669 
3670 			if (stcb) {
3671 				if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3672 					memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3673 				} else {
3674 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3675 					error = EINVAL;
3676 				}
3677 				SCTP_TCB_UNLOCK(stcb);
3678 			} else {
3679 				SCTP_INP_WLOCK(inp);
3680 				memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3681 				SCTP_INP_WUNLOCK(inp);
3682 			}
3683 		}
3684 		break;
3685 	case SCTP_PEER_ADDR_PARAMS:
3686 		/* Applys to the specific association */
3687 		{
3688 			struct sctp_paddrparams *paddrp;
3689 			struct sctp_nets *net;
3690 
3691 			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3692 			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3693 			net = NULL;
3694 			if (stcb) {
3695 				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3696 			} else {
3697 				/*
3698 				 * We increment here since
3699 				 * sctp_findassociation_ep_addr() wil do a
3700 				 * decrement if it finds the stcb as long as
3701 				 * the locked tcb (last argument) is NOT a
3702 				 * TCB.. aka NULL.
3703 				 */
3704 				SCTP_INP_INCR_REF(inp);
3705 				stcb = sctp_findassociation_ep_addr(&inp,
3706 				    (struct sockaddr *)&paddrp->spp_address,
3707 				    &net, NULL, NULL);
3708 				if (stcb == NULL) {
3709 					SCTP_INP_DECR_REF(inp);
3710 				}
3711 			}
3712 			if (stcb && (net == NULL)) {
3713 				struct sockaddr *sa;
3714 
3715 				sa = (struct sockaddr *)&paddrp->spp_address;
3716 				if (sa->sa_family == AF_INET) {
3717 					struct sockaddr_in *sin;
3718 
3719 					sin = (struct sockaddr_in *)sa;
3720 					if (sin->sin_addr.s_addr) {
3721 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3722 						SCTP_TCB_UNLOCK(stcb);
3723 						error = EINVAL;
3724 						break;
3725 					}
3726 				} else if (sa->sa_family == AF_INET6) {
3727 					struct sockaddr_in6 *sin6;
3728 
3729 					sin6 = (struct sockaddr_in6 *)sa;
3730 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3731 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3732 						SCTP_TCB_UNLOCK(stcb);
3733 						error = EINVAL;
3734 						break;
3735 					}
3736 				} else {
3737 					error = EAFNOSUPPORT;
3738 					SCTP_TCB_UNLOCK(stcb);
3739 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3740 					break;
3741 				}
3742 			}
3743 			/* sanity checks */
3744 			if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3745 				if (stcb)
3746 					SCTP_TCB_UNLOCK(stcb);
3747 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3748 				return (EINVAL);
3749 			}
3750 			if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3751 				if (stcb)
3752 					SCTP_TCB_UNLOCK(stcb);
3753 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3754 				return (EINVAL);
3755 			}
3756 			if (stcb) {
3757 				/************************TCB SPECIFIC SET ******************/
3758 				/*
3759 				 * do we change the timer for HB, we run
3760 				 * only one?
3761 				 */
3762 				int ovh = 0;
3763 
3764 				if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3765 					ovh = SCTP_MED_OVERHEAD;
3766 				} else {
3767 					ovh = SCTP_MED_V4_OVERHEAD;
3768 				}
3769 
3770 				if (paddrp->spp_hbinterval)
3771 					stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3772 				else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3773 					stcb->asoc.heart_beat_delay = 0;
3774 
3775 				/* network sets ? */
3776 				if (net) {
3777 					/************************NET SPECIFIC SET ******************/
3778 					if (paddrp->spp_flags & SPP_HB_DEMAND) {
3779 						/* on demand HB */
3780 						if (sctp_send_hb(stcb, 1, net) < 0) {
3781 							/* asoc destroyed */
3782 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3783 							error = EINVAL;
3784 							break;
3785 						}
3786 					}
3787 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3788 						net->dest_state |= SCTP_ADDR_NOHB;
3789 					}
3790 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3791 						net->dest_state &= ~SCTP_ADDR_NOHB;
3792 					}
3793 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3794 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3795 							sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3796 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3797 						}
3798 						if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3799 							net->mtu = paddrp->spp_pathmtu + ovh;
3800 							if (net->mtu < stcb->asoc.smallest_mtu) {
3801 								sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3802 							}
3803 						}
3804 					}
3805 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3806 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3807 							sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3808 						}
3809 					}
3810 					if (paddrp->spp_pathmaxrxt)
3811 						net->failure_threshold = paddrp->spp_pathmaxrxt;
3812 #ifdef INET
3813 					if (paddrp->spp_flags & SPP_IPV4_TOS) {
3814 						if (net->ro._l_addr.sin.sin_family == AF_INET) {
3815 							net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3816 						}
3817 					}
3818 #endif
3819 #ifdef INET6
3820 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3821 						if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3822 							net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3823 						}
3824 					}
3825 #endif
3826 				} else {
3827 					/************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3828 					if (paddrp->spp_pathmaxrxt)
3829 						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3830 
3831 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3832 						/* Turn back on the timer */
3833 						stcb->asoc.hb_is_disabled = 0;
3834 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3835 					}
3836 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3837 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3838 							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3839 								sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3840 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3841 							}
3842 							if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3843 								net->mtu = paddrp->spp_pathmtu + ovh;
3844 								if (net->mtu < stcb->asoc.smallest_mtu) {
3845 									sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3846 								}
3847 							}
3848 						}
3849 					}
3850 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3851 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3852 							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3853 								sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3854 							}
3855 						}
3856 					}
3857 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3858 						int cnt_of_unconf = 0;
3859 						struct sctp_nets *lnet;
3860 
3861 						stcb->asoc.hb_is_disabled = 1;
3862 						TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3863 							if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3864 								cnt_of_unconf++;
3865 							}
3866 						}
3867 						/*
3868 						 * stop the timer ONLY if we
3869 						 * have no unconfirmed
3870 						 * addresses
3871 						 */
3872 						if (cnt_of_unconf == 0) {
3873 							TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3874 								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3875 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3876 							}
3877 						}
3878 					}
3879 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3880 						/* start up the timer. */
3881 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3882 							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3883 						}
3884 					}
3885 #ifdef INET
3886 					if (paddrp->spp_flags & SPP_IPV4_TOS)
3887 						stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3888 #endif
3889 #ifdef INET6
3890 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3891 						stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3892 #endif
3893 
3894 				}
3895 				SCTP_TCB_UNLOCK(stcb);
3896 			} else {
3897 				/************************NO TCB, SET TO default stuff ******************/
3898 				SCTP_INP_WLOCK(inp);
3899 				/*
3900 				 * For the TOS/FLOWLABEL stuff you set it
3901 				 * with the options on the socket
3902 				 */
3903 				if (paddrp->spp_pathmaxrxt) {
3904 					inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3905 				}
3906 				if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3907 					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3908 				else if (paddrp->spp_hbinterval) {
3909 					if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3910 						paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
3911 					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3912 				}
3913 				if (paddrp->spp_flags & SPP_HB_ENABLE) {
3914 					sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3915 
3916 				} else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3917 					sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3918 				}
3919 				SCTP_INP_WUNLOCK(inp);
3920 			}
3921 		}
3922 		break;
3923 	case SCTP_RTOINFO:
3924 		{
3925 			struct sctp_rtoinfo *srto;
3926 			uint32_t new_init, new_min, new_max;
3927 
3928 			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3929 			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3930 
3931 			if (stcb) {
3932 				if (srto->srto_initial)
3933 					new_init = srto->srto_initial;
3934 				else
3935 					new_init = stcb->asoc.initial_rto;
3936 				if (srto->srto_max)
3937 					new_max = srto->srto_max;
3938 				else
3939 					new_max = stcb->asoc.maxrto;
3940 				if (srto->srto_min)
3941 					new_min = srto->srto_min;
3942 				else
3943 					new_min = stcb->asoc.minrto;
3944 				if ((new_min <= new_init) && (new_init <= new_max)) {
3945 					stcb->asoc.initial_rto = new_init;
3946 					stcb->asoc.maxrto = new_max;
3947 					stcb->asoc.minrto = new_min;
3948 				} else {
3949 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3950 					error = EINVAL;
3951 				}
3952 				SCTP_TCB_UNLOCK(stcb);
3953 			} else {
3954 				SCTP_INP_WLOCK(inp);
3955 				if (srto->srto_initial)
3956 					new_init = srto->srto_initial;
3957 				else
3958 					new_init = inp->sctp_ep.initial_rto;
3959 				if (srto->srto_max)
3960 					new_max = srto->srto_max;
3961 				else
3962 					new_max = inp->sctp_ep.sctp_maxrto;
3963 				if (srto->srto_min)
3964 					new_min = srto->srto_min;
3965 				else
3966 					new_min = inp->sctp_ep.sctp_minrto;
3967 				if ((new_min <= new_init) && (new_init <= new_max)) {
3968 					inp->sctp_ep.initial_rto = new_init;
3969 					inp->sctp_ep.sctp_maxrto = new_max;
3970 					inp->sctp_ep.sctp_minrto = new_min;
3971 				} else {
3972 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3973 					error = EINVAL;
3974 				}
3975 				SCTP_INP_WUNLOCK(inp);
3976 			}
3977 		}
3978 		break;
3979 	case SCTP_ASSOCINFO:
3980 		{
3981 			struct sctp_assocparams *sasoc;
3982 
3983 			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3984 			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3985 			if (sasoc->sasoc_cookie_life) {
3986 				/* boundary check the cookie life */
3987 				if (sasoc->sasoc_cookie_life < 1000)
3988 					sasoc->sasoc_cookie_life = 1000;
3989 				if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
3990 					sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
3991 				}
3992 			}
3993 			if (stcb) {
3994 				if (sasoc->sasoc_asocmaxrxt)
3995 					stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3996 				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3997 				sasoc->sasoc_peer_rwnd = 0;
3998 				sasoc->sasoc_local_rwnd = 0;
3999 				if (sasoc->sasoc_cookie_life) {
4000 					stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4001 				}
4002 				SCTP_TCB_UNLOCK(stcb);
4003 			} else {
4004 				SCTP_INP_WLOCK(inp);
4005 				if (sasoc->sasoc_asocmaxrxt)
4006 					inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
4007 				sasoc->sasoc_number_peer_destinations = 0;
4008 				sasoc->sasoc_peer_rwnd = 0;
4009 				sasoc->sasoc_local_rwnd = 0;
4010 				if (sasoc->sasoc_cookie_life) {
4011 					inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4012 				}
4013 				SCTP_INP_WUNLOCK(inp);
4014 			}
4015 		}
4016 		break;
4017 	case SCTP_INITMSG:
4018 		{
4019 			struct sctp_initmsg *sinit;
4020 
4021 			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
4022 			SCTP_INP_WLOCK(inp);
4023 			if (sinit->sinit_num_ostreams)
4024 				inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
4025 
4026 			if (sinit->sinit_max_instreams)
4027 				inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
4028 
4029 			if (sinit->sinit_max_attempts)
4030 				inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
4031 
4032 			if (sinit->sinit_max_init_timeo)
4033 				inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
4034 			SCTP_INP_WUNLOCK(inp);
4035 		}
4036 		break;
4037 	case SCTP_PRIMARY_ADDR:
4038 		{
4039 			struct sctp_setprim *spa;
4040 			struct sctp_nets *net, *lnet;
4041 
4042 			SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
4043 			SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
4044 
4045 			net = NULL;
4046 			if (stcb) {
4047 				net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
4048 			} else {
4049 				/*
4050 				 * We increment here since
4051 				 * sctp_findassociation_ep_addr() wil do a
4052 				 * decrement if it finds the stcb as long as
4053 				 * the locked tcb (last argument) is NOT a
4054 				 * TCB.. aka NULL.
4055 				 */
4056 				SCTP_INP_INCR_REF(inp);
4057 				stcb = sctp_findassociation_ep_addr(&inp,
4058 				    (struct sockaddr *)&spa->ssp_addr,
4059 				    &net, NULL, NULL);
4060 				if (stcb == NULL) {
4061 					SCTP_INP_DECR_REF(inp);
4062 				}
4063 			}
4064 
4065 			if ((stcb) && (net)) {
4066 				if ((net != stcb->asoc.primary_destination) &&
4067 				    (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
4068 					/* Ok we need to set it */
4069 					lnet = stcb->asoc.primary_destination;
4070 					if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
4071 						if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
4072 							net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
4073 						}
4074 						net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
4075 					}
4076 				}
4077 			} else {
4078 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4079 				error = EINVAL;
4080 			}
4081 			if (stcb) {
4082 				SCTP_TCB_UNLOCK(stcb);
4083 			}
4084 		}
4085 		break;
4086 	case SCTP_SET_DYNAMIC_PRIMARY:
4087 		{
4088 			union sctp_sockstore *ss;
4089 
4090 			error = priv_check(curthread,
4091 			    PRIV_NETINET_RESERVEDPORT);
4092 			if (error)
4093 				break;
4094 
4095 			SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
4096 			/* SUPER USER CHECK? */
4097 			error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
4098 		}
4099 		break;
4100 	case SCTP_SET_PEER_PRIMARY_ADDR:
4101 		{
4102 			struct sctp_setpeerprim *sspp;
4103 
4104 			SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
4105 			SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
4106 			if (stcb != NULL) {
4107 				struct sctp_ifa *ifa;
4108 
4109 				ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
4110 				    stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
4111 				if (ifa == NULL) {
4112 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4113 					error = EINVAL;
4114 					goto out_of_it;
4115 				}
4116 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4117 					/*
4118 					 * Must validate the ifa found is in
4119 					 * our ep
4120 					 */
4121 					struct sctp_laddr *laddr;
4122 					int found = 0;
4123 
4124 					LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4125 						if (laddr->ifa == NULL) {
4126 							SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4127 							    __FUNCTION__);
4128 							continue;
4129 						}
4130 						if (laddr->ifa == ifa) {
4131 							found = 1;
4132 							break;
4133 						}
4134 					}
4135 					if (!found) {
4136 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4137 						error = EINVAL;
4138 						goto out_of_it;
4139 					}
4140 				}
4141 				if (sctp_set_primary_ip_address_sa(stcb,
4142 				    (struct sockaddr *)&sspp->sspp_addr) != 0) {
4143 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4144 					error = EINVAL;
4145 				}
4146 		out_of_it:
4147 				SCTP_TCB_UNLOCK(stcb);
4148 			} else {
4149 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4150 				error = EINVAL;
4151 			}
4152 
4153 		}
4154 		break;
4155 	case SCTP_BINDX_ADD_ADDR:
4156 		{
4157 			struct sctp_getaddresses *addrs;
4158 			size_t sz;
4159 			struct thread *td;
4160 
4161 			td = (struct thread *)p;
4162 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
4163 			    optsize);
4164 			if (addrs->addr->sa_family == AF_INET) {
4165 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4166 				if (optsize < sz) {
4167 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4168 					error = EINVAL;
4169 					break;
4170 				}
4171 				if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4172 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4173 					break;
4174 				}
4175 #ifdef INET6
4176 			} else if (addrs->addr->sa_family == AF_INET6) {
4177 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4178 				if (optsize < sz) {
4179 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4180 					error = EINVAL;
4181 					break;
4182 				}
4183 				if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4184 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4185 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4186 					break;
4187 				}
4188 #endif
4189 			} else {
4190 				error = EAFNOSUPPORT;
4191 				break;
4192 			}
4193 			sctp_bindx_add_address(so, inp, addrs->addr,
4194 			    addrs->sget_assoc_id, vrf_id,
4195 			    &error, p);
4196 		}
4197 		break;
4198 	case SCTP_BINDX_REM_ADDR:
4199 		{
4200 			struct sctp_getaddresses *addrs;
4201 			size_t sz;
4202 			struct thread *td;
4203 
4204 			td = (struct thread *)p;
4205 
4206 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
4207 			if (addrs->addr->sa_family == AF_INET) {
4208 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4209 				if (optsize < sz) {
4210 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4211 					error = EINVAL;
4212 					break;
4213 				}
4214 				if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4215 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4216 					break;
4217 				}
4218 #ifdef INET6
4219 			} else if (addrs->addr->sa_family == AF_INET6) {
4220 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4221 				if (optsize < sz) {
4222 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4223 					error = EINVAL;
4224 					break;
4225 				}
4226 				if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4227 				    (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4228 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4229 					break;
4230 				}
4231 #endif
4232 			} else {
4233 				error = EAFNOSUPPORT;
4234 				break;
4235 			}
4236 			sctp_bindx_delete_address(so, inp, addrs->addr,
4237 			    addrs->sget_assoc_id, vrf_id,
4238 			    &error);
4239 		}
4240 		break;
4241 	default:
4242 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
4243 		error = ENOPROTOOPT;
4244 		break;
4245 	}			/* end switch (opt) */
4246 	return (error);
4247 }
4248 
4249 int
4250 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
4251 {
4252 	void *optval = NULL;
4253 	size_t optsize = 0;
4254 	struct sctp_inpcb *inp;
4255 	void *p;
4256 	int error = 0;
4257 
4258 	inp = (struct sctp_inpcb *)so->so_pcb;
4259 	if (inp == 0) {
4260 		/* I made the same as TCP since we are not setup? */
4261 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4262 		return (ECONNRESET);
4263 	}
4264 	if (sopt->sopt_level != IPPROTO_SCTP) {
4265 		/* wrong proto level... send back up to IP */
4266 #ifdef INET6
4267 		if (INP_CHECK_SOCKAF(so, AF_INET6))
4268 			error = ip6_ctloutput(so, sopt);
4269 		else
4270 #endif				/* INET6 */
4271 			error = ip_ctloutput(so, sopt);
4272 		return (error);
4273 	}
4274 	optsize = sopt->sopt_valsize;
4275 	if (optsize) {
4276 		SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
4277 		if (optval == NULL) {
4278 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
4279 			return (ENOBUFS);
4280 		}
4281 		error = sooptcopyin(sopt, optval, optsize, optsize);
4282 		if (error) {
4283 			SCTP_FREE(optval, SCTP_M_SOCKOPT);
4284 			goto out;
4285 		}
4286 	}
4287 	p = (void *)sopt->sopt_td;
4288 	if (sopt->sopt_dir == SOPT_SET) {
4289 		error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
4290 	} else if (sopt->sopt_dir == SOPT_GET) {
4291 		error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
4292 	} else {
4293 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4294 		error = EINVAL;
4295 	}
4296 	if ((error == 0) && (optval != NULL)) {
4297 		error = sooptcopyout(sopt, optval, optsize);
4298 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
4299 	} else if (optval != NULL) {
4300 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
4301 	}
4302 out:
4303 	return (error);
4304 }
4305 
4306 
4307 static int
4308 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
4309 {
4310 	int error = 0;
4311 	int create_lock_on = 0;
4312 	uint32_t vrf_id;
4313 	struct sctp_inpcb *inp;
4314 	struct sctp_tcb *stcb = NULL;
4315 
4316 	inp = (struct sctp_inpcb *)so->so_pcb;
4317 	if (inp == 0) {
4318 		/* I made the same as TCP since we are not setup? */
4319 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4320 		return (ECONNRESET);
4321 	}
4322 	if (addr == NULL) {
4323 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4324 		return EINVAL;
4325 	}
4326 #ifdef INET6
4327 	if (addr->sa_family == AF_INET6) {
4328 		struct sockaddr_in6 *sin6p;
4329 
4330 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
4331 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4332 			return (EINVAL);
4333 		}
4334 		sin6p = (struct sockaddr_in6 *)addr;
4335 		if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
4336 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4337 			return (error);
4338 		}
4339 	} else
4340 #endif
4341 	if (addr->sa_family == AF_INET) {
4342 		struct sockaddr_in *sinp;
4343 
4344 		if (addr->sa_len != sizeof(struct sockaddr_in)) {
4345 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4346 			return (EINVAL);
4347 		}
4348 		sinp = (struct sockaddr_in *)addr;
4349 		if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
4350 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4351 			return (error);
4352 		}
4353 	} else {
4354 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
4355 		return (EAFNOSUPPORT);
4356 	}
4357 	SCTP_INP_INCR_REF(inp);
4358 	SCTP_ASOC_CREATE_LOCK(inp);
4359 	create_lock_on = 1;
4360 
4361 
4362 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4363 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4364 		/* Should I really unlock ? */
4365 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
4366 		error = EFAULT;
4367 		goto out_now;
4368 	}
4369 #ifdef INET6
4370 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
4371 	    (addr->sa_family == AF_INET6)) {
4372 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4373 		error = EINVAL;
4374 		goto out_now;
4375 	}
4376 #endif				/* INET6 */
4377 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
4378 	    SCTP_PCB_FLAGS_UNBOUND) {
4379 		/* Bind a ephemeral port */
4380 		error = sctp_inpcb_bind(so, NULL, NULL, p);
4381 		if (error) {
4382 			goto out_now;
4383 		}
4384 	}
4385 	/* Now do we connect? */
4386 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4387 	    (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
4388 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4389 		error = EINVAL;
4390 		goto out_now;
4391 	}
4392 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4393 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4394 		/* We are already connected AND the TCP model */
4395 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4396 		error = EADDRINUSE;
4397 		goto out_now;
4398 	}
4399 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4400 		SCTP_INP_RLOCK(inp);
4401 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
4402 		SCTP_INP_RUNLOCK(inp);
4403 	} else {
4404 		/*
4405 		 * We increment here since sctp_findassociation_ep_addr()
4406 		 * will do a decrement if it finds the stcb as long as the
4407 		 * locked tcb (last argument) is NOT a TCB.. aka NULL.
4408 		 */
4409 		SCTP_INP_INCR_REF(inp);
4410 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
4411 		if (stcb == NULL) {
4412 			SCTP_INP_DECR_REF(inp);
4413 		} else {
4414 			SCTP_TCB_UNLOCK(stcb);
4415 		}
4416 	}
4417 	if (stcb != NULL) {
4418 		/* Already have or am bring up an association */
4419 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4420 		error = EALREADY;
4421 		goto out_now;
4422 	}
4423 	vrf_id = inp->def_vrf_id;
4424 	/* We are GOOD to go */
4425 	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
4426 	if (stcb == NULL) {
4427 		/* Gak! no memory */
4428 		goto out_now;
4429 	}
4430 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
4431 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4432 		/* Set the connected flag so we can queue data */
4433 		SOCKBUF_LOCK(&so->so_rcv);
4434 		so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
4435 		SOCKBUF_UNLOCK(&so->so_rcv);
4436 		SOCKBUF_LOCK(&so->so_snd);
4437 		so->so_snd.sb_state &= ~SBS_CANTSENDMORE;
4438 		SOCKBUF_UNLOCK(&so->so_snd);
4439 		SOCK_LOCK(so);
4440 		so->so_state &= ~SS_ISDISCONNECTING;
4441 		SOCK_UNLOCK(so);
4442 		soisconnecting(so);
4443 	}
4444 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
4445 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4446 
4447 	/* initialize authentication parameters for the assoc */
4448 	sctp_initialize_auth_params(inp, stcb);
4449 
4450 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4451 	SCTP_TCB_UNLOCK(stcb);
4452 out_now:
4453 	if (create_lock_on) {
4454 		SCTP_ASOC_CREATE_UNLOCK(inp);
4455 	}
4456 	SCTP_INP_DECR_REF(inp);
4457 	return error;
4458 }
4459 
4460 int
4461 sctp_listen(struct socket *so, int backlog, struct thread *p)
4462 {
4463 	/*
4464 	 * Note this module depends on the protocol processing being called
4465 	 * AFTER any socket level flags and backlog are applied to the
4466 	 * socket. The traditional way that the socket flags are applied is
4467 	 * AFTER protocol processing. We have made a change to the
4468 	 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
4469 	 * place if the socket API for SCTP is to work properly.
4470 	 */
4471 
4472 	int error = 0;
4473 	struct sctp_inpcb *inp;
4474 
4475 	inp = (struct sctp_inpcb *)so->so_pcb;
4476 	if (inp == 0) {
4477 		/* I made the same as TCP since we are not setup? */
4478 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4479 		return (ECONNRESET);
4480 	}
4481 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
4482 		/* See if we have a listener */
4483 		struct sctp_inpcb *tinp;
4484 		union sctp_sockstore store, *sp;
4485 
4486 		sp = &store;
4487 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4488 			/* not bound all */
4489 			struct sctp_laddr *laddr;
4490 
4491 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4492 				memcpy(&store, &laddr->ifa->address, sizeof(store));
4493 				sp->sin.sin_port = inp->sctp_lport;
4494 				tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4495 				if (tinp && (tinp != inp) &&
4496 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4497 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4498 				    (tinp->sctp_socket->so_qlimit)) {
4499 					/*
4500 					 * we have a listener already and
4501 					 * its not this inp.
4502 					 */
4503 					SCTP_INP_DECR_REF(tinp);
4504 					return (EADDRINUSE);
4505 				} else if (tinp) {
4506 					SCTP_INP_DECR_REF(tinp);
4507 				}
4508 			}
4509 		} else {
4510 			/* Setup a local addr bound all */
4511 			memset(&store, 0, sizeof(store));
4512 			store.sin.sin_port = inp->sctp_lport;
4513 #ifdef INET6
4514 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4515 				store.sa.sa_family = AF_INET6;
4516 				store.sa.sa_len = sizeof(struct sockaddr_in6);
4517 			}
4518 #endif
4519 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
4520 				store.sa.sa_family = AF_INET;
4521 				store.sa.sa_len = sizeof(struct sockaddr_in);
4522 			}
4523 			tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4524 			if (tinp && (tinp != inp) &&
4525 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4526 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4527 			    (tinp->sctp_socket->so_qlimit)) {
4528 				/*
4529 				 * we have a listener already and its not
4530 				 * this inp.
4531 				 */
4532 				SCTP_INP_DECR_REF(tinp);
4533 				return (EADDRINUSE);
4534 			} else if (tinp) {
4535 				SCTP_INP_DECR_REF(inp);
4536 			}
4537 		}
4538 	}
4539 	SCTP_INP_RLOCK(inp);
4540 #ifdef SCTP_LOCK_LOGGING
4541 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
4542 		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
4543 	}
4544 #endif
4545 	SOCK_LOCK(so);
4546 	error = solisten_proto_check(so);
4547 	if (error) {
4548 		SOCK_UNLOCK(so);
4549 		SCTP_INP_RUNLOCK(inp);
4550 		return (error);
4551 	}
4552 	if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
4553 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4554 		/*
4555 		 * The unlucky case - We are in the tcp pool with this guy.
4556 		 * - Someone else is in the main inp slot. - We must move
4557 		 * this guy (the listener) to the main slot - We must then
4558 		 * move the guy that was listener to the TCP Pool.
4559 		 */
4560 		if (sctp_swap_inpcb_for_listen(inp)) {
4561 			goto in_use;
4562 		}
4563 	}
4564 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4565 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4566 		/* We are already connected AND the TCP model */
4567 in_use:
4568 		SCTP_INP_RUNLOCK(inp);
4569 		SOCK_UNLOCK(so);
4570 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4571 		return (EADDRINUSE);
4572 	}
4573 	SCTP_INP_RUNLOCK(inp);
4574 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4575 		/* We must do a bind. */
4576 		SOCK_UNLOCK(so);
4577 		if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
4578 			/* bind error, probably perm */
4579 			return (error);
4580 		}
4581 		SOCK_LOCK(so);
4582 	}
4583 	/* It appears for 7.0 and on, we must always call this. */
4584 	solisten_proto(so, backlog);
4585 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4586 		/* remove the ACCEPTCONN flag for one-to-many sockets */
4587 		so->so_options &= ~SO_ACCEPTCONN;
4588 	}
4589 	if (backlog == 0) {
4590 		/* turning off listen */
4591 		so->so_options &= ~SO_ACCEPTCONN;
4592 	}
4593 	SOCK_UNLOCK(so);
4594 	return (error);
4595 }
4596 
4597 static int sctp_defered_wakeup_cnt = 0;
4598 
4599 int
4600 sctp_accept(struct socket *so, struct sockaddr **addr)
4601 {
4602 	struct sctp_tcb *stcb;
4603 	struct sctp_inpcb *inp;
4604 	union sctp_sockstore store;
4605 
4606 #ifdef INET6
4607 	int error;
4608 
4609 #endif
4610 	inp = (struct sctp_inpcb *)so->so_pcb;
4611 
4612 	if (inp == 0) {
4613 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4614 		return (ECONNRESET);
4615 	}
4616 	SCTP_INP_RLOCK(inp);
4617 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4618 		SCTP_INP_RUNLOCK(inp);
4619 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4620 		return (EOPNOTSUPP);
4621 	}
4622 	if (so->so_state & SS_ISDISCONNECTED) {
4623 		SCTP_INP_RUNLOCK(inp);
4624 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
4625 		return (ECONNABORTED);
4626 	}
4627 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
4628 	if (stcb == NULL) {
4629 		SCTP_INP_RUNLOCK(inp);
4630 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4631 		return (ECONNRESET);
4632 	}
4633 	SCTP_TCB_LOCK(stcb);
4634 	SCTP_INP_RUNLOCK(inp);
4635 	store = stcb->asoc.primary_destination->ro._l_addr;
4636 	stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
4637 	SCTP_TCB_UNLOCK(stcb);
4638 	switch (store.sa.sa_family) {
4639 	case AF_INET:
4640 		{
4641 			struct sockaddr_in *sin;
4642 
4643 			SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4644 			if (sin == NULL)
4645 				return (ENOMEM);
4646 			sin->sin_family = AF_INET;
4647 			sin->sin_len = sizeof(*sin);
4648 			sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
4649 			sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
4650 			*addr = (struct sockaddr *)sin;
4651 			break;
4652 		}
4653 #ifdef INET6
4654 	case AF_INET6:
4655 		{
4656 			struct sockaddr_in6 *sin6;
4657 
4658 			SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
4659 			if (sin6 == NULL)
4660 				return (ENOMEM);
4661 			sin6->sin6_family = AF_INET6;
4662 			sin6->sin6_len = sizeof(*sin6);
4663 			sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
4664 
4665 			sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
4666 			if ((error = sa6_recoverscope(sin6)) != 0) {
4667 				SCTP_FREE_SONAME(sin6);
4668 				return (error);
4669 			}
4670 			*addr = (struct sockaddr *)sin6;
4671 			break;
4672 		}
4673 #endif
4674 	default:
4675 		/* TSNH */
4676 		break;
4677 	}
4678 	/* Wake any delayed sleep action */
4679 	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4680 		SCTP_INP_WLOCK(inp);
4681 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4682 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4683 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4684 			SCTP_INP_WUNLOCK(inp);
4685 			SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4686 			if (sowriteable(inp->sctp_socket)) {
4687 				sowwakeup_locked(inp->sctp_socket);
4688 			} else {
4689 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4690 			}
4691 			SCTP_INP_WLOCK(inp);
4692 		}
4693 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4694 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4695 			SCTP_INP_WUNLOCK(inp);
4696 			SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4697 			if (soreadable(inp->sctp_socket)) {
4698 				sctp_defered_wakeup_cnt++;
4699 				sorwakeup_locked(inp->sctp_socket);
4700 			} else {
4701 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4702 			}
4703 			SCTP_INP_WLOCK(inp);
4704 		}
4705 		SCTP_INP_WUNLOCK(inp);
4706 	}
4707 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4708 		SCTP_TCB_LOCK(stcb);
4709 		sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
4710 	}
4711 	return (0);
4712 }
4713 
4714 int
4715 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4716 {
4717 	struct sockaddr_in *sin;
4718 	uint32_t vrf_id;
4719 	struct sctp_inpcb *inp;
4720 	struct sctp_ifa *sctp_ifa;
4721 
4722 	/*
4723 	 * Do the malloc first in case it blocks.
4724 	 */
4725 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4726 	if (sin == NULL)
4727 		return (ENOMEM);
4728 	sin->sin_family = AF_INET;
4729 	sin->sin_len = sizeof(*sin);
4730 	inp = (struct sctp_inpcb *)so->so_pcb;
4731 	if (!inp) {
4732 		SCTP_FREE_SONAME(sin);
4733 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4734 		return ECONNRESET;
4735 	}
4736 	SCTP_INP_RLOCK(inp);
4737 	sin->sin_port = inp->sctp_lport;
4738 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4739 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4740 			struct sctp_tcb *stcb;
4741 			struct sockaddr_in *sin_a;
4742 			struct sctp_nets *net;
4743 			int fnd;
4744 
4745 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
4746 			if (stcb == NULL) {
4747 				goto notConn;
4748 			}
4749 			fnd = 0;
4750 			sin_a = NULL;
4751 			SCTP_TCB_LOCK(stcb);
4752 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4753 				sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4754 				if (sin_a == NULL)
4755 					/* this will make coverity happy */
4756 					continue;
4757 
4758 				if (sin_a->sin_family == AF_INET) {
4759 					fnd = 1;
4760 					break;
4761 				}
4762 			}
4763 			if ((!fnd) || (sin_a == NULL)) {
4764 				/* punt */
4765 				SCTP_TCB_UNLOCK(stcb);
4766 				goto notConn;
4767 			}
4768 			vrf_id = inp->def_vrf_id;
4769 			sctp_ifa = sctp_source_address_selection(inp,
4770 			    stcb,
4771 			    (sctp_route_t *) & net->ro,
4772 			    net, 0, vrf_id);
4773 			if (sctp_ifa) {
4774 				sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4775 				sctp_free_ifa(sctp_ifa);
4776 			}
4777 			SCTP_TCB_UNLOCK(stcb);
4778 		} else {
4779 			/* For the bound all case you get back 0 */
4780 	notConn:
4781 			sin->sin_addr.s_addr = 0;
4782 		}
4783 
4784 	} else {
4785 		/* Take the first IPv4 address in the list */
4786 		struct sctp_laddr *laddr;
4787 		int fnd = 0;
4788 
4789 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4790 			if (laddr->ifa->address.sa.sa_family == AF_INET) {
4791 				struct sockaddr_in *sin_a;
4792 
4793 				sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4794 				sin->sin_addr = sin_a->sin_addr;
4795 				fnd = 1;
4796 				break;
4797 			}
4798 		}
4799 		if (!fnd) {
4800 			SCTP_FREE_SONAME(sin);
4801 			SCTP_INP_RUNLOCK(inp);
4802 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4803 			return ENOENT;
4804 		}
4805 	}
4806 	SCTP_INP_RUNLOCK(inp);
4807 	(*addr) = (struct sockaddr *)sin;
4808 	return (0);
4809 }
4810 
4811 int
4812 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4813 {
4814 	struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4815 	int fnd;
4816 	struct sockaddr_in *sin_a;
4817 	struct sctp_inpcb *inp;
4818 	struct sctp_tcb *stcb;
4819 	struct sctp_nets *net;
4820 
4821 	/* Do the malloc first in case it blocks. */
4822 	inp = (struct sctp_inpcb *)so->so_pcb;
4823 	if ((inp == NULL) ||
4824 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4825 		/* UDP type and listeners will drop out here */
4826 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
4827 		return (ENOTCONN);
4828 	}
4829 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4830 	if (sin == NULL)
4831 		return (ENOMEM);
4832 	sin->sin_family = AF_INET;
4833 	sin->sin_len = sizeof(*sin);
4834 
4835 	/* We must recapture incase we blocked */
4836 	inp = (struct sctp_inpcb *)so->so_pcb;
4837 	if (!inp) {
4838 		SCTP_FREE_SONAME(sin);
4839 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4840 		return ECONNRESET;
4841 	}
4842 	SCTP_INP_RLOCK(inp);
4843 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
4844 	if (stcb) {
4845 		SCTP_TCB_LOCK(stcb);
4846 	}
4847 	SCTP_INP_RUNLOCK(inp);
4848 	if (stcb == NULL) {
4849 		SCTP_FREE_SONAME(sin);
4850 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4851 		return ECONNRESET;
4852 	}
4853 	fnd = 0;
4854 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4855 		sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4856 		if (sin_a->sin_family == AF_INET) {
4857 			fnd = 1;
4858 			sin->sin_port = stcb->rport;
4859 			sin->sin_addr = sin_a->sin_addr;
4860 			break;
4861 		}
4862 	}
4863 	SCTP_TCB_UNLOCK(stcb);
4864 	if (!fnd) {
4865 		/* No IPv4 address */
4866 		SCTP_FREE_SONAME(sin);
4867 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4868 		return ENOENT;
4869 	}
4870 	(*addr) = (struct sockaddr *)sin;
4871 	return (0);
4872 }
4873 
4874 struct pr_usrreqs sctp_usrreqs = {
4875 	.pru_abort = sctp_abort,
4876 	.pru_accept = sctp_accept,
4877 	.pru_attach = sctp_attach,
4878 	.pru_bind = sctp_bind,
4879 	.pru_connect = sctp_connect,
4880 	.pru_control = in_control,
4881 	.pru_close = sctp_close,
4882 	.pru_detach = sctp_close,
4883 	.pru_sopoll = sopoll_generic,
4884 	.pru_flush = sctp_flush,
4885 	.pru_disconnect = sctp_disconnect,
4886 	.pru_listen = sctp_listen,
4887 	.pru_peeraddr = sctp_peeraddr,
4888 	.pru_send = sctp_sendm,
4889 	.pru_shutdown = sctp_shutdown,
4890 	.pru_sockaddr = sctp_ingetaddr,
4891 	.pru_sosend = sctp_sosend,
4892 	.pru_soreceive = sctp_soreceive
4893 };
4894