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