xref: /freebsd/sys/netinet/sctp_usrreq.c (revision a9148abd9da5db2f1c682fb17bed791845fc41c9)
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 #endif
42 #include <netinet/sctp_sysctl.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_uio.h>
45 #include <netinet/sctp_asconf.h>
46 #include <netinet/sctputil.h>
47 #include <netinet/sctp_indata.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_auth.h>
50 #include <netinet/sctp_bsd_addr.h>
51 #include <netinet/sctp_cc_functions.h>
52 
53 
54 
55 
56 void
57 sctp_init(void)
58 {
59 	u_long sb_max_adj;
60 
61 	bzero(&SCTP_BASE_STATS, sizeof(struct sctpstat));
62 
63 	/* Initialize and modify the sysctled variables */
64 	sctp_init_sysctls();
65 	if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
66 		SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) = (nmbclusters / 8);
67 	/*
68 	 * Allow a user to take no more than 1/2 the number of clusters or
69 	 * the SB_MAX whichever is smaller for the send window.
70 	 */
71 	sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
72 	SCTP_BASE_SYSCTL(sctp_sendspace) = min(sb_max_adj,
73 	    (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
74 	/*
75 	 * Now for the recv window, should we take the same amount? or
76 	 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
77 	 * now I will just copy.
78 	 */
79 	SCTP_BASE_SYSCTL(sctp_recvspace) = SCTP_BASE_SYSCTL(sctp_sendspace);
80 
81 	SCTP_BASE_VAR(first_time) = 0;
82 	SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
83 	sctp_pcb_init();
84 #if defined(SCTP_PACKET_LOGGING)
85 	SCTP_BASE_VAR(packet_log_writers) = 0;
86 	SCTP_BASE_VAR(packet_log_end) = 0;
87 	bzero(&SCTP_BASE_VAR(packet_log_buffer), SCTP_PACKET_LOG_SIZE);
88 #endif
89 
90 
91 }
92 
93 void
94 sctp_finish(void)
95 {
96 	sctp_pcb_finish();
97 }
98 
99 
100 
101 void
102 sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
103     struct sctp_tcb *stcb,
104     struct sctp_nets *net,
105     uint16_t nxtsz)
106 {
107 	struct sctp_tmit_chunk *chk;
108 
109 	/* Adjust that too */
110 	stcb->asoc.smallest_mtu = nxtsz;
111 	/* now off to subtract IP_DF flag if needed */
112 #ifdef SCTP_PRINT_FOR_B_AND_M
113 	SCTP_PRINTF("sctp_pathmtu_adjust called inp:%p stcb:%p net:%p nxtsz:%d\n",
114 	    inp, stcb, net, nxtsz);
115 #endif
116 	TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
117 		if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
118 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
119 		}
120 	}
121 	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
122 		if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
123 			/*
124 			 * For this guy we also mark for immediate resend
125 			 * since we sent to big of chunk
126 			 */
127 			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
128 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
129 				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
130 			}
131 			chk->sent = SCTP_DATAGRAM_RESEND;
132 			chk->rec.data.doing_fast_retransmit = 0;
133 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
134 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
135 				    chk->whoTo->flight_size,
136 				    chk->book_size,
137 				    (uintptr_t) chk->whoTo,
138 				    chk->rec.data.TSN_seq);
139 			}
140 			/* Clear any time so NO RTT is being done */
141 			chk->do_rtt = 0;
142 			sctp_flight_size_decrease(chk);
143 			sctp_total_flight_decrease(stcb, chk);
144 		}
145 	}
146 }
147 
148 static void
149 sctp_notify_mbuf(struct sctp_inpcb *inp,
150     struct sctp_tcb *stcb,
151     struct sctp_nets *net,
152     struct ip *ip,
153     struct sctphdr *sh)
154 {
155 	struct icmp *icmph;
156 	int totsz, tmr_stopped = 0;
157 	uint16_t nxtsz;
158 
159 	/* protection */
160 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
161 	    (ip == NULL) || (sh == NULL)) {
162 		if (stcb != NULL) {
163 			SCTP_TCB_UNLOCK(stcb);
164 		}
165 		return;
166 	}
167 	/* First job is to verify the vtag matches what I would send */
168 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
169 		SCTP_TCB_UNLOCK(stcb);
170 		return;
171 	}
172 	icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
173 	    sizeof(struct ip)));
174 	if (icmph->icmp_type != ICMP_UNREACH) {
175 		/* We only care about unreachable */
176 		SCTP_TCB_UNLOCK(stcb);
177 		return;
178 	}
179 	if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
180 		/* not a unreachable message due to frag. */
181 		SCTP_TCB_UNLOCK(stcb);
182 		return;
183 	}
184 	totsz = ip->ip_len;
185 
186 	nxtsz = ntohs(icmph->icmp_nextmtu);
187 	if (nxtsz == 0) {
188 		/*
189 		 * old type router that does not tell us what the next size
190 		 * mtu is. Rats we will have to guess (in a educated fashion
191 		 * of course)
192 		 */
193 		nxtsz = find_next_best_mtu(totsz);
194 	}
195 	/* Stop any PMTU timer */
196 	if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
197 		tmr_stopped = 1;
198 		sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
199 		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
200 	}
201 	/* Adjust destination size limit */
202 	if (net->mtu > nxtsz) {
203 		net->mtu = nxtsz;
204 	}
205 	/* now what about the ep? */
206 	if (stcb->asoc.smallest_mtu > nxtsz) {
207 #ifdef SCTP_PRINT_FOR_B_AND_M
208 		SCTP_PRINTF("notify_mbuf (ICMP) calls sctp_pathmtu_adjust mtu:%d\n",
209 		    nxtsz);
210 #endif
211 		sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
212 	}
213 	if (tmr_stopped)
214 		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
215 
216 	SCTP_TCB_UNLOCK(stcb);
217 }
218 
219 
220 void
221 sctp_notify(struct sctp_inpcb *inp,
222     struct ip *ip,
223     struct sctphdr *sh,
224     struct sockaddr *to,
225     struct sctp_tcb *stcb,
226     struct sctp_nets *net)
227 {
228 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
229 	struct socket *so;
230 
231 #endif
232 	/* protection */
233 	int reason;
234 	struct icmp *icmph;
235 
236 
237 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
238 	    (sh == NULL) || (to == NULL)) {
239 		if (stcb)
240 			SCTP_TCB_UNLOCK(stcb);
241 		return;
242 	}
243 	/* First job is to verify the vtag matches what I would send */
244 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
245 		SCTP_TCB_UNLOCK(stcb);
246 		return;
247 	}
248 	icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
249 	    sizeof(struct ip)));
250 	if (icmph->icmp_type != ICMP_UNREACH) {
251 		/* We only care about unreachable */
252 		SCTP_TCB_UNLOCK(stcb);
253 		return;
254 	}
255 	if ((icmph->icmp_code == ICMP_UNREACH_NET) ||
256 	    (icmph->icmp_code == ICMP_UNREACH_HOST) ||
257 	    (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
258 	    (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
259 	    (icmph->icmp_code == ICMP_UNREACH_ISOLATED) ||
260 	    (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) ||
261 	    (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
262 	    (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
263 
264 		/*
265 		 * Hmm reachablity problems we must examine closely. If its
266 		 * not reachable, we may have lost a network. Or if there is
267 		 * NO protocol at the other end named SCTP. well we consider
268 		 * it a OOTB abort.
269 		 */
270 		if (net->dest_state & SCTP_ADDR_REACHABLE) {
271 			/* Ok that destination is NOT reachable */
272 			SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
273 			    net->error_count,
274 			    net->failure_threshold,
275 			    net);
276 
277 			net->dest_state &= ~SCTP_ADDR_REACHABLE;
278 			net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
279 			/*
280 			 * JRS 5/14/07 - If a destination is unreachable,
281 			 * the PF bit is turned off.  This allows an
282 			 * unambiguous use of the PF bit for destinations
283 			 * that are reachable but potentially failed. If the
284 			 * destination is set to the unreachable state, also
285 			 * set the destination to the PF state.
286 			 */
287 			/*
288 			 * Add debug message here if destination is not in
289 			 * PF state.
290 			 */
291 			/* Stop any running T3 timers here? */
292 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
293 				net->dest_state &= ~SCTP_ADDR_PF;
294 				SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
295 				    net);
296 			}
297 			net->error_count = net->failure_threshold + 1;
298 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
299 			    stcb, SCTP_FAILED_THRESHOLD,
300 			    (void *)net, SCTP_SO_NOT_LOCKED);
301 		}
302 		SCTP_TCB_UNLOCK(stcb);
303 	} else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
304 	    (icmph->icmp_code == ICMP_UNREACH_PORT)) {
305 		/*
306 		 * Here the peer is either playing tricks on us, including
307 		 * an address that belongs to someone who does not support
308 		 * SCTP OR was a userland implementation that shutdown and
309 		 * now is dead. In either case treat it like a OOTB abort
310 		 * with no TCB
311 		 */
312 		reason = SCTP_PEER_FAULTY;
313 		sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
314 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
315 		so = SCTP_INP_SO(inp);
316 		atomic_add_int(&stcb->asoc.refcnt, 1);
317 		SCTP_TCB_UNLOCK(stcb);
318 		SCTP_SOCKET_LOCK(so, 1);
319 		SCTP_TCB_LOCK(stcb);
320 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
321 #endif
322 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
323 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
324 		SCTP_SOCKET_UNLOCK(so, 1);
325 		/* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
326 #endif
327 		/* no need to unlock here, since the TCB is gone */
328 	} else {
329 		SCTP_TCB_UNLOCK(stcb);
330 	}
331 }
332 
333 void
334 sctp_ctlinput(cmd, sa, vip)
335 	int cmd;
336 	struct sockaddr *sa;
337 	void *vip;
338 {
339 	struct ip *ip = vip;
340 	struct sctphdr *sh;
341 	uint32_t vrf_id;
342 
343 	/* FIX, for non-bsd is this right? */
344 	vrf_id = SCTP_DEFAULT_VRFID;
345 	if (sa->sa_family != AF_INET ||
346 	    ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
347 		return;
348 	}
349 	if (PRC_IS_REDIRECT(cmd)) {
350 		ip = 0;
351 	} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
352 		return;
353 	}
354 	if (ip) {
355 		struct sctp_inpcb *inp = NULL;
356 		struct sctp_tcb *stcb = NULL;
357 		struct sctp_nets *net = NULL;
358 		struct sockaddr_in to, from;
359 
360 		sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
361 		bzero(&to, sizeof(to));
362 		bzero(&from, sizeof(from));
363 		from.sin_family = to.sin_family = AF_INET;
364 		from.sin_len = to.sin_len = sizeof(to);
365 		from.sin_port = sh->src_port;
366 		from.sin_addr = ip->ip_src;
367 		to.sin_port = sh->dest_port;
368 		to.sin_addr = ip->ip_dst;
369 
370 		/*
371 		 * 'to' holds the dest of the packet that failed to be sent.
372 		 * 'from' holds our local endpoint address. Thus we reverse
373 		 * the to and the from in the lookup.
374 		 */
375 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
376 		    (struct sockaddr *)&to,
377 		    &inp, &net, 1, vrf_id);
378 		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
379 			if (cmd != PRC_MSGSIZE) {
380 				sctp_notify(inp, ip, sh,
381 				    (struct sockaddr *)&to, stcb,
382 				    net);
383 			} else {
384 				/* handle possible ICMP size messages */
385 				sctp_notify_mbuf(inp, stcb, net, ip, sh);
386 			}
387 		} else {
388 			if ((stcb == NULL) && (inp != NULL)) {
389 				/* reduce ref-count */
390 				SCTP_INP_WLOCK(inp);
391 				SCTP_INP_DECR_REF(inp);
392 				SCTP_INP_WUNLOCK(inp);
393 			}
394 		}
395 	}
396 	return;
397 }
398 
399 static int
400 sctp_getcred(SYSCTL_HANDLER_ARGS)
401 {
402 	struct xucred xuc;
403 	struct sockaddr_in addrs[2];
404 	struct sctp_inpcb *inp;
405 	struct sctp_nets *net;
406 	struct sctp_tcb *stcb;
407 	int error;
408 	uint32_t vrf_id;
409 
410 	/* FIX, for non-bsd is this right? */
411 	vrf_id = SCTP_DEFAULT_VRFID;
412 
413 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
414 
415 	if (error)
416 		return (error);
417 
418 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
419 	if (error)
420 		return (error);
421 
422 	stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
423 	    sintosa(&addrs[1]),
424 	    &inp, &net, 1, vrf_id);
425 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
426 		if ((inp != NULL) && (stcb == NULL)) {
427 			/* reduce ref-count */
428 			SCTP_INP_WLOCK(inp);
429 			SCTP_INP_DECR_REF(inp);
430 			goto cred_can_cont;
431 		}
432 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
433 		error = ENOENT;
434 		goto out;
435 	}
436 	SCTP_TCB_UNLOCK(stcb);
437 	/*
438 	 * We use the write lock here, only since in the error leg we need
439 	 * it. If we used RLOCK, then we would have to
440 	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
441 	 * Better to use higher wlock.
442 	 */
443 	SCTP_INP_WLOCK(inp);
444 cred_can_cont:
445 	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
446 	if (error) {
447 		SCTP_INP_WUNLOCK(inp);
448 		goto out;
449 	}
450 	cru2x(inp->sctp_socket->so_cred, &xuc);
451 	SCTP_INP_WUNLOCK(inp);
452 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
453 out:
454 	return (error);
455 }
456 
457 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
458     0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
459 
460 
461 static void
462 sctp_abort(struct socket *so)
463 {
464 	struct sctp_inpcb *inp;
465 	uint32_t flags;
466 
467 	inp = (struct sctp_inpcb *)so->so_pcb;
468 	if (inp == 0) {
469 		return;
470 	}
471 sctp_must_try_again:
472 	flags = inp->sctp_flags;
473 #ifdef SCTP_LOG_CLOSING
474 	sctp_log_closing(inp, NULL, 17);
475 #endif
476 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
477 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
478 #ifdef SCTP_LOG_CLOSING
479 		sctp_log_closing(inp, NULL, 16);
480 #endif
481 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
482 		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
483 		SOCK_LOCK(so);
484 		SCTP_SB_CLEAR(so->so_snd);
485 		/*
486 		 * same for the rcv ones, they are only here for the
487 		 * accounting/select.
488 		 */
489 		SCTP_SB_CLEAR(so->so_rcv);
490 
491 		/* Now null out the reference, we are completely detached. */
492 		so->so_pcb = NULL;
493 		SOCK_UNLOCK(so);
494 	} else {
495 		flags = inp->sctp_flags;
496 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
497 			goto sctp_must_try_again;
498 		}
499 	}
500 	return;
501 }
502 
503 static int
504 sctp_attach(struct socket *so, int proto, struct thread *p)
505 {
506 	struct sctp_inpcb *inp;
507 	struct inpcb *ip_inp;
508 	int error;
509 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
510 
511 #ifdef IPSEC
512 	uint32_t flags;
513 
514 #endif
515 	inp = (struct sctp_inpcb *)so->so_pcb;
516 	if (inp != 0) {
517 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
518 		return EINVAL;
519 	}
520 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
521 		error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
522 		if (error) {
523 			return error;
524 		}
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 = MODULE_GLOBAL(MOD_INET, 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_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1397 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1398 		return (EINVAL);
1399 	}
1400 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1401 		SCTP_INP_RLOCK(inp);
1402 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1403 		SCTP_INP_RUNLOCK(inp);
1404 	}
1405 	if (stcb) {
1406 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1407 		return (EALREADY);
1408 	}
1409 	SCTP_INP_INCR_REF(inp);
1410 	SCTP_ASOC_CREATE_LOCK(inp);
1411 	creat_lock_on = 1;
1412 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1413 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1414 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1415 		error = EFAULT;
1416 		goto out_now;
1417 	}
1418 	totaddrp = (int *)optval;
1419 	totaddr = *totaddrp;
1420 	sa = (struct sockaddr *)(totaddrp + 1);
1421 	stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1422 	if ((stcb != NULL) || bad_addresses) {
1423 		/* Already have or am bring up an association */
1424 		SCTP_ASOC_CREATE_UNLOCK(inp);
1425 		creat_lock_on = 0;
1426 		if (stcb)
1427 			SCTP_TCB_UNLOCK(stcb);
1428 		if (bad_addresses == 0) {
1429 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1430 			error = EALREADY;
1431 		}
1432 		goto out_now;
1433 	}
1434 #ifdef INET6
1435 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1436 	    (num_v6 > 0)) {
1437 		error = EINVAL;
1438 		goto out_now;
1439 	}
1440 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1441 	    (num_v4 > 0)) {
1442 		struct in6pcb *inp6;
1443 
1444 		inp6 = (struct in6pcb *)inp;
1445 		if (SCTP_IPV6_V6ONLY(inp6)) {
1446 			/*
1447 			 * if IPV6_V6ONLY flag, ignore connections destined
1448 			 * to a v4 addr or v4-mapped addr
1449 			 */
1450 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1451 			error = EINVAL;
1452 			goto out_now;
1453 		}
1454 	}
1455 #endif				/* INET6 */
1456 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1457 	    SCTP_PCB_FLAGS_UNBOUND) {
1458 		/* Bind a ephemeral port */
1459 		error = sctp_inpcb_bind(so, NULL, NULL, p);
1460 		if (error) {
1461 			goto out_now;
1462 		}
1463 	}
1464 	/* FIX ME: do we want to pass in a vrf on the connect call? */
1465 	vrf_id = inp->def_vrf_id;
1466 
1467 
1468 	/* We are GOOD to go */
1469 	stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id,
1470 	    (struct thread *)p
1471 	    );
1472 	if (stcb == NULL) {
1473 		/* Gak! no memory */
1474 		goto out_now;
1475 	}
1476 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1477 	/* move to second address */
1478 	if (sa->sa_family == AF_INET)
1479 		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1480 	else
1481 		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1482 
1483 	error = 0;
1484 	added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1485 	/* Fill in the return id */
1486 	if (error) {
1487 		(void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
1488 		goto out_now;
1489 	}
1490 	a_id = (sctp_assoc_t *) optval;
1491 	*a_id = sctp_get_associd(stcb);
1492 
1493 	/* initialize authentication parameters for the assoc */
1494 	sctp_initialize_auth_params(inp, stcb);
1495 
1496 	if (delay) {
1497 		/* doing delayed connection */
1498 		stcb->asoc.delayed_connection = 1;
1499 		sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1500 	} else {
1501 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1502 		sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1503 	}
1504 	SCTP_TCB_UNLOCK(stcb);
1505 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1506 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1507 		/* Set the connected flag so we can queue data */
1508 		soisconnecting(so);
1509 	}
1510 out_now:
1511 	if (creat_lock_on) {
1512 		SCTP_ASOC_CREATE_UNLOCK(inp);
1513 	}
1514 	SCTP_INP_DECR_REF(inp);
1515 	return error;
1516 }
1517 
1518 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1519 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1520 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1521 		SCTP_INP_RLOCK(inp); \
1522 		stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1523 		if (stcb) { \
1524 			SCTP_TCB_LOCK(stcb); \
1525                 } \
1526 		SCTP_INP_RUNLOCK(inp); \
1527 	} else if (assoc_id != 0) { \
1528 		stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1529 		if (stcb == NULL) { \
1530 		        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1531 			error = ENOENT; \
1532 			break; \
1533 		} \
1534 	} else { \
1535 		stcb = NULL; \
1536         } \
1537   }
1538 
1539 
1540 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size)  {\
1541 	if (size < sizeof(type)) { \
1542 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1543 		error = EINVAL; \
1544 		break; \
1545 	} else { \
1546 		destp = (type *)srcp; \
1547 	} \
1548       }
1549 
1550 static int
1551 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1552     void *p)
1553 {
1554 	struct sctp_inpcb *inp = NULL;
1555 	int error, val = 0;
1556 	struct sctp_tcb *stcb = NULL;
1557 
1558 	if (optval == NULL) {
1559 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1560 		return (EINVAL);
1561 	}
1562 	inp = (struct sctp_inpcb *)so->so_pcb;
1563 	if (inp == 0) {
1564 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1565 		return EINVAL;
1566 	}
1567 	error = 0;
1568 
1569 	switch (optname) {
1570 	case SCTP_NODELAY:
1571 	case SCTP_AUTOCLOSE:
1572 	case SCTP_EXPLICIT_EOR:
1573 	case SCTP_AUTO_ASCONF:
1574 	case SCTP_DISABLE_FRAGMENTS:
1575 	case SCTP_I_WANT_MAPPED_V4_ADDR:
1576 	case SCTP_USE_EXT_RCVINFO:
1577 		SCTP_INP_RLOCK(inp);
1578 		switch (optname) {
1579 		case SCTP_DISABLE_FRAGMENTS:
1580 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1581 			break;
1582 		case SCTP_I_WANT_MAPPED_V4_ADDR:
1583 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1584 			break;
1585 		case SCTP_AUTO_ASCONF:
1586 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1587 				/* only valid for bound all sockets */
1588 				val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1589 			} else {
1590 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1591 				error = EINVAL;
1592 				goto flags_out;
1593 			}
1594 			break;
1595 		case SCTP_EXPLICIT_EOR:
1596 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1597 			break;
1598 		case SCTP_NODELAY:
1599 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1600 			break;
1601 		case SCTP_USE_EXT_RCVINFO:
1602 			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1603 			break;
1604 		case SCTP_AUTOCLOSE:
1605 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1606 				val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1607 			else
1608 				val = 0;
1609 			break;
1610 
1611 		default:
1612 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1613 			error = ENOPROTOOPT;
1614 		}		/* end switch (sopt->sopt_name) */
1615 		if (optname != SCTP_AUTOCLOSE) {
1616 			/* make it an "on/off" value */
1617 			val = (val != 0);
1618 		}
1619 		if (*optsize < sizeof(val)) {
1620 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1621 			error = EINVAL;
1622 		}
1623 flags_out:
1624 		SCTP_INP_RUNLOCK(inp);
1625 		if (error == 0) {
1626 			/* return the option value */
1627 			*(int *)optval = val;
1628 			*optsize = sizeof(val);
1629 		}
1630 		break;
1631 	case SCTP_GET_PACKET_LOG:
1632 		{
1633 #ifdef  SCTP_PACKET_LOGGING
1634 			uint8_t *target;
1635 			int ret;
1636 
1637 			SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1638 			ret = sctp_copy_out_packet_log(target, (int)*optsize);
1639 			*optsize = ret;
1640 #else
1641 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1642 			error = EOPNOTSUPP;
1643 #endif
1644 			break;
1645 		}
1646 	case SCTP_REUSE_PORT:
1647 		{
1648 			uint32_t *value;
1649 
1650 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1651 				/* Can't do this for a 1-m socket */
1652 				error = EINVAL;
1653 				break;
1654 			}
1655 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1656 			*value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1657 			*optsize = sizeof(uint32_t);
1658 		}
1659 		break;
1660 	case SCTP_PARTIAL_DELIVERY_POINT:
1661 		{
1662 			uint32_t *value;
1663 
1664 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1665 			*value = inp->partial_delivery_point;
1666 			*optsize = sizeof(uint32_t);
1667 		}
1668 		break;
1669 	case SCTP_FRAGMENT_INTERLEAVE:
1670 		{
1671 			uint32_t *value;
1672 
1673 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1674 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1675 				if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1676 					*value = SCTP_FRAG_LEVEL_2;
1677 				} else {
1678 					*value = SCTP_FRAG_LEVEL_1;
1679 				}
1680 			} else {
1681 				*value = SCTP_FRAG_LEVEL_0;
1682 			}
1683 			*optsize = sizeof(uint32_t);
1684 		}
1685 		break;
1686 	case SCTP_CMT_ON_OFF:
1687 		{
1688 			struct sctp_assoc_value *av;
1689 
1690 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1691 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
1692 				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1693 				if (stcb) {
1694 					av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1695 					SCTP_TCB_UNLOCK(stcb);
1696 
1697 				} else {
1698 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1699 					error = ENOTCONN;
1700 				}
1701 			} else {
1702 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1703 				error = ENOPROTOOPT;
1704 			}
1705 			*optsize = sizeof(*av);
1706 		}
1707 		break;
1708 		/* JRS - Get socket option for pluggable congestion control */
1709 	case SCTP_PLUGGABLE_CC:
1710 		{
1711 			struct sctp_assoc_value *av;
1712 
1713 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1714 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1715 			if (stcb) {
1716 				av->assoc_value = stcb->asoc.congestion_control_module;
1717 				SCTP_TCB_UNLOCK(stcb);
1718 			} else {
1719 				av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1720 			}
1721 			*optsize = sizeof(*av);
1722 		}
1723 		break;
1724 	case SCTP_GET_ADDR_LEN:
1725 		{
1726 			struct sctp_assoc_value *av;
1727 
1728 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1729 			error = EINVAL;
1730 #ifdef INET
1731 			if (av->assoc_value == AF_INET) {
1732 				av->assoc_value = sizeof(struct sockaddr_in);
1733 				error = 0;
1734 			}
1735 #endif
1736 #ifdef INET6
1737 			if (av->assoc_value == AF_INET6) {
1738 				av->assoc_value = sizeof(struct sockaddr_in6);
1739 				error = 0;
1740 			}
1741 #endif
1742 			if (error) {
1743 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1744 			}
1745 			*optsize = sizeof(*av);
1746 		}
1747 		break;
1748 	case SCTP_GET_ASSOC_NUMBER:
1749 		{
1750 			uint32_t *value, cnt;
1751 
1752 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1753 			cnt = 0;
1754 			SCTP_INP_RLOCK(inp);
1755 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1756 				cnt++;
1757 			}
1758 			SCTP_INP_RUNLOCK(inp);
1759 			*value = cnt;
1760 			*optsize = sizeof(uint32_t);
1761 		}
1762 		break;
1763 
1764 	case SCTP_GET_ASSOC_ID_LIST:
1765 		{
1766 			struct sctp_assoc_ids *ids;
1767 			unsigned int at, limit;
1768 
1769 			SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1770 			at = 0;
1771 			limit = *optsize / sizeof(sctp_assoc_t);
1772 			SCTP_INP_RLOCK(inp);
1773 			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1774 				if (at < limit) {
1775 					ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1776 				} else {
1777 					error = EINVAL;
1778 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1779 					break;
1780 				}
1781 			}
1782 			SCTP_INP_RUNLOCK(inp);
1783 			*optsize = at * sizeof(sctp_assoc_t);
1784 		}
1785 		break;
1786 	case SCTP_CONTEXT:
1787 		{
1788 			struct sctp_assoc_value *av;
1789 
1790 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1791 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1792 
1793 			if (stcb) {
1794 				av->assoc_value = stcb->asoc.context;
1795 				SCTP_TCB_UNLOCK(stcb);
1796 			} else {
1797 				SCTP_INP_RLOCK(inp);
1798 				av->assoc_value = inp->sctp_context;
1799 				SCTP_INP_RUNLOCK(inp);
1800 			}
1801 			*optsize = sizeof(*av);
1802 		}
1803 		break;
1804 	case SCTP_VRF_ID:
1805 		{
1806 			uint32_t *default_vrfid;
1807 
1808 			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1809 			*default_vrfid = inp->def_vrf_id;
1810 			break;
1811 		}
1812 	case SCTP_GET_ASOC_VRF:
1813 		{
1814 			struct sctp_assoc_value *id;
1815 
1816 			SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1817 			SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1818 			if (stcb == NULL) {
1819 				error = EINVAL;
1820 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1821 				break;
1822 			}
1823 			id->assoc_value = stcb->asoc.vrf_id;
1824 			break;
1825 		}
1826 	case SCTP_GET_VRF_IDS:
1827 		{
1828 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1829 			error = EOPNOTSUPP;
1830 			break;
1831 		}
1832 	case SCTP_GET_NONCE_VALUES:
1833 		{
1834 			struct sctp_get_nonce_values *gnv;
1835 
1836 			SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1837 			SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1838 
1839 			if (stcb) {
1840 				gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1841 				gnv->gn_local_tag = stcb->asoc.my_vtag;
1842 				SCTP_TCB_UNLOCK(stcb);
1843 			} else {
1844 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1845 				error = ENOTCONN;
1846 			}
1847 			*optsize = sizeof(*gnv);
1848 		}
1849 		break;
1850 	case SCTP_DELAYED_SACK:
1851 		{
1852 			struct sctp_sack_info *sack;
1853 
1854 			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1855 			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1856 			if (stcb) {
1857 				sack->sack_delay = stcb->asoc.delayed_ack;
1858 				sack->sack_freq = stcb->asoc.sack_freq;
1859 				SCTP_TCB_UNLOCK(stcb);
1860 			} else {
1861 				SCTP_INP_RLOCK(inp);
1862 				sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1863 				sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1864 				SCTP_INP_RUNLOCK(inp);
1865 			}
1866 			*optsize = sizeof(*sack);
1867 		}
1868 		break;
1869 
1870 	case SCTP_GET_SNDBUF_USE:
1871 		{
1872 			struct sctp_sockstat *ss;
1873 
1874 			SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1875 			SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1876 
1877 			if (stcb) {
1878 				ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1879 				ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1880 				    stcb->asoc.size_on_all_streams);
1881 				SCTP_TCB_UNLOCK(stcb);
1882 			} else {
1883 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1884 				error = ENOTCONN;
1885 			}
1886 			*optsize = sizeof(struct sctp_sockstat);
1887 		}
1888 		break;
1889 	case SCTP_MAX_BURST:
1890 		{
1891 			uint8_t *value;
1892 
1893 			SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1894 
1895 			SCTP_INP_RLOCK(inp);
1896 			*value = inp->sctp_ep.max_burst;
1897 			SCTP_INP_RUNLOCK(inp);
1898 			*optsize = sizeof(uint8_t);
1899 		}
1900 		break;
1901 	case SCTP_MAXSEG:
1902 		{
1903 			struct sctp_assoc_value *av;
1904 			int ovh;
1905 
1906 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1907 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1908 
1909 			if (stcb) {
1910 				av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1911 				SCTP_TCB_UNLOCK(stcb);
1912 			} else {
1913 				SCTP_INP_RLOCK(inp);
1914 				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1915 					ovh = SCTP_MED_OVERHEAD;
1916 				} else {
1917 					ovh = SCTP_MED_V4_OVERHEAD;
1918 				}
1919 				if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
1920 					av->assoc_value = 0;
1921 				else
1922 					av->assoc_value = inp->sctp_frag_point - ovh;
1923 				SCTP_INP_RUNLOCK(inp);
1924 			}
1925 			*optsize = sizeof(struct sctp_assoc_value);
1926 		}
1927 		break;
1928 	case SCTP_GET_STAT_LOG:
1929 		error = sctp_fill_stat_log(optval, optsize);
1930 		break;
1931 	case SCTP_EVENTS:
1932 		{
1933 			struct sctp_event_subscribe *events;
1934 
1935 			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1936 			memset(events, 0, sizeof(*events));
1937 			SCTP_INP_RLOCK(inp);
1938 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1939 				events->sctp_data_io_event = 1;
1940 
1941 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1942 				events->sctp_association_event = 1;
1943 
1944 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1945 				events->sctp_address_event = 1;
1946 
1947 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1948 				events->sctp_send_failure_event = 1;
1949 
1950 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1951 				events->sctp_peer_error_event = 1;
1952 
1953 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1954 				events->sctp_shutdown_event = 1;
1955 
1956 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1957 				events->sctp_partial_delivery_event = 1;
1958 
1959 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1960 				events->sctp_adaptation_layer_event = 1;
1961 
1962 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
1963 				events->sctp_authentication_event = 1;
1964 
1965 			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
1966 				events->sctp_stream_reset_events = 1;
1967 			SCTP_INP_RUNLOCK(inp);
1968 			*optsize = sizeof(struct sctp_event_subscribe);
1969 		}
1970 		break;
1971 
1972 	case SCTP_ADAPTATION_LAYER:
1973 		{
1974 			uint32_t *value;
1975 
1976 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1977 
1978 			SCTP_INP_RLOCK(inp);
1979 			*value = inp->sctp_ep.adaptation_layer_indicator;
1980 			SCTP_INP_RUNLOCK(inp);
1981 			*optsize = sizeof(uint32_t);
1982 		}
1983 		break;
1984 	case SCTP_SET_INITIAL_DBG_SEQ:
1985 		{
1986 			uint32_t *value;
1987 
1988 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1989 			SCTP_INP_RLOCK(inp);
1990 			*value = inp->sctp_ep.initial_sequence_debug;
1991 			SCTP_INP_RUNLOCK(inp);
1992 			*optsize = sizeof(uint32_t);
1993 		}
1994 		break;
1995 	case SCTP_GET_LOCAL_ADDR_SIZE:
1996 		{
1997 			uint32_t *value;
1998 
1999 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2000 			SCTP_INP_RLOCK(inp);
2001 			*value = sctp_count_max_addresses(inp);
2002 			SCTP_INP_RUNLOCK(inp);
2003 			*optsize = sizeof(uint32_t);
2004 		}
2005 		break;
2006 	case SCTP_GET_REMOTE_ADDR_SIZE:
2007 		{
2008 			uint32_t *value;
2009 			size_t size;
2010 			struct sctp_nets *net;
2011 
2012 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2013 			/* FIXME MT: change to sctp_assoc_value? */
2014 			SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2015 
2016 			if (stcb) {
2017 				size = 0;
2018 				/* Count the sizes */
2019 				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2020 					if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2021 					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2022 						size += sizeof(struct sockaddr_in6);
2023 					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2024 						size += sizeof(struct sockaddr_in);
2025 					} else {
2026 						/* huh */
2027 						break;
2028 					}
2029 				}
2030 				SCTP_TCB_UNLOCK(stcb);
2031 				*value = (uint32_t) size;
2032 			} else {
2033 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2034 				error = ENOTCONN;
2035 			}
2036 			*optsize = sizeof(uint32_t);
2037 		}
2038 		break;
2039 	case SCTP_GET_PEER_ADDRESSES:
2040 		/*
2041 		 * Get the address information, an array is passed in to
2042 		 * fill up we pack it.
2043 		 */
2044 		{
2045 			size_t cpsz, left;
2046 			struct sockaddr_storage *sas;
2047 			struct sctp_nets *net;
2048 			struct sctp_getaddresses *saddr;
2049 
2050 			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2051 			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2052 
2053 			if (stcb) {
2054 				left = (*optsize) - sizeof(struct sctp_getaddresses);
2055 				*optsize = sizeof(struct sctp_getaddresses);
2056 				sas = (struct sockaddr_storage *)&saddr->addr[0];
2057 
2058 				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2059 					if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2060 					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2061 						cpsz = sizeof(struct sockaddr_in6);
2062 					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2063 						cpsz = sizeof(struct sockaddr_in);
2064 					} else {
2065 						/* huh */
2066 						break;
2067 					}
2068 					if (left < cpsz) {
2069 						/* not enough room. */
2070 						break;
2071 					}
2072 #ifdef INET6
2073 					if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2074 					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2075 						/* Must map the address */
2076 						in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2077 						    (struct sockaddr_in6 *)sas);
2078 					} else {
2079 #endif
2080 						memcpy(sas, &net->ro._l_addr, cpsz);
2081 #ifdef INET6
2082 					}
2083 #endif
2084 					((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2085 
2086 					sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2087 					left -= cpsz;
2088 					*optsize += cpsz;
2089 				}
2090 				SCTP_TCB_UNLOCK(stcb);
2091 			} else {
2092 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2093 				error = ENOENT;
2094 			}
2095 		}
2096 		break;
2097 	case SCTP_GET_LOCAL_ADDRESSES:
2098 		{
2099 			size_t limit, actual;
2100 			struct sockaddr_storage *sas;
2101 			struct sctp_getaddresses *saddr;
2102 
2103 			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2104 			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2105 
2106 			sas = (struct sockaddr_storage *)&saddr->addr[0];
2107 			limit = *optsize - sizeof(sctp_assoc_t);
2108 			actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2109 			if (stcb) {
2110 				SCTP_TCB_UNLOCK(stcb);
2111 			}
2112 			*optsize = sizeof(struct sockaddr_storage) + actual;
2113 		}
2114 		break;
2115 	case SCTP_PEER_ADDR_PARAMS:
2116 		{
2117 			struct sctp_paddrparams *paddrp;
2118 			struct sctp_nets *net;
2119 
2120 			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2121 			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2122 
2123 			net = NULL;
2124 			if (stcb) {
2125 				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2126 			} else {
2127 				/*
2128 				 * We increment here since
2129 				 * sctp_findassociation_ep_addr() wil do a
2130 				 * decrement if it finds the stcb as long as
2131 				 * the locked tcb (last argument) is NOT a
2132 				 * TCB.. aka NULL.
2133 				 */
2134 				SCTP_INP_INCR_REF(inp);
2135 				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2136 				if (stcb == NULL) {
2137 					SCTP_INP_DECR_REF(inp);
2138 				}
2139 			}
2140 			if (stcb && (net == NULL)) {
2141 				struct sockaddr *sa;
2142 
2143 				sa = (struct sockaddr *)&paddrp->spp_address;
2144 				if (sa->sa_family == AF_INET) {
2145 					struct sockaddr_in *sin;
2146 
2147 					sin = (struct sockaddr_in *)sa;
2148 					if (sin->sin_addr.s_addr) {
2149 						error = EINVAL;
2150 						SCTP_TCB_UNLOCK(stcb);
2151 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2152 						break;
2153 					}
2154 				} else if (sa->sa_family == AF_INET6) {
2155 					struct sockaddr_in6 *sin6;
2156 
2157 					sin6 = (struct sockaddr_in6 *)sa;
2158 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2159 						error = EINVAL;
2160 						SCTP_TCB_UNLOCK(stcb);
2161 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2162 						break;
2163 					}
2164 				} else {
2165 					error = EAFNOSUPPORT;
2166 					SCTP_TCB_UNLOCK(stcb);
2167 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2168 					break;
2169 				}
2170 			}
2171 			if (stcb) {
2172 				/* Applys to the specific association */
2173 				paddrp->spp_flags = 0;
2174 				if (net) {
2175 					int ovh;
2176 
2177 					if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2178 						ovh = SCTP_MED_OVERHEAD;
2179 					} else {
2180 						ovh = SCTP_MED_V4_OVERHEAD;
2181 					}
2182 
2183 
2184 					paddrp->spp_pathmaxrxt = net->failure_threshold;
2185 					paddrp->spp_pathmtu = net->mtu - ovh;
2186 					/* get flags for HB */
2187 					if (net->dest_state & SCTP_ADDR_NOHB)
2188 						paddrp->spp_flags |= SPP_HB_DISABLE;
2189 					else
2190 						paddrp->spp_flags |= SPP_HB_ENABLE;
2191 					/* get flags for PMTU */
2192 					if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2193 						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2194 					} else {
2195 						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2196 					}
2197 #ifdef INET
2198 					if (net->ro._l_addr.sin.sin_family == AF_INET) {
2199 						paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2200 						paddrp->spp_flags |= SPP_IPV4_TOS;
2201 					}
2202 #endif
2203 #ifdef INET6
2204 					if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2205 						paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2206 						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2207 					}
2208 #endif
2209 				} else {
2210 					/*
2211 					 * No destination so return default
2212 					 * value
2213 					 */
2214 					int cnt = 0;
2215 
2216 					paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2217 					paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2218 #ifdef INET
2219 					paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2220 					paddrp->spp_flags |= SPP_IPV4_TOS;
2221 #endif
2222 #ifdef INET6
2223 					paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2224 					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2225 #endif
2226 					/* default settings should be these */
2227 					if (stcb->asoc.hb_is_disabled == 0) {
2228 						paddrp->spp_flags |= SPP_HB_ENABLE;
2229 					} else {
2230 						paddrp->spp_flags |= SPP_HB_DISABLE;
2231 					}
2232 					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2233 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2234 							cnt++;
2235 						}
2236 					}
2237 					if (cnt) {
2238 						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2239 					}
2240 				}
2241 				paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2242 				paddrp->spp_assoc_id = sctp_get_associd(stcb);
2243 				SCTP_TCB_UNLOCK(stcb);
2244 			} else {
2245 				/* Use endpoint defaults */
2246 				SCTP_INP_RLOCK(inp);
2247 				paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2248 				paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2249 				paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2250 				/* get inp's default */
2251 #ifdef INET
2252 				paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2253 				paddrp->spp_flags |= SPP_IPV4_TOS;
2254 #endif
2255 #ifdef INET6
2256 				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2257 					paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2258 					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2259 				}
2260 #endif
2261 				/* can't return this */
2262 				paddrp->spp_pathmtu = 0;
2263 
2264 				/* default behavior, no stcb */
2265 				paddrp->spp_flags = SPP_PMTUD_ENABLE;
2266 
2267 				if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2268 					paddrp->spp_flags |= SPP_HB_ENABLE;
2269 				} else {
2270 					paddrp->spp_flags |= SPP_HB_DISABLE;
2271 				}
2272 				SCTP_INP_RUNLOCK(inp);
2273 			}
2274 			*optsize = sizeof(struct sctp_paddrparams);
2275 		}
2276 		break;
2277 	case SCTP_GET_PEER_ADDR_INFO:
2278 		{
2279 			struct sctp_paddrinfo *paddri;
2280 			struct sctp_nets *net;
2281 
2282 			SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2283 			SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2284 
2285 			net = NULL;
2286 			if (stcb) {
2287 				net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2288 			} else {
2289 				/*
2290 				 * We increment here since
2291 				 * sctp_findassociation_ep_addr() wil do a
2292 				 * decrement if it finds the stcb as long as
2293 				 * the locked tcb (last argument) is NOT a
2294 				 * TCB.. aka NULL.
2295 				 */
2296 				SCTP_INP_INCR_REF(inp);
2297 				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2298 				if (stcb == NULL) {
2299 					SCTP_INP_DECR_REF(inp);
2300 				}
2301 			}
2302 
2303 			if ((stcb) && (net)) {
2304 				paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2305 				paddri->spinfo_cwnd = net->cwnd;
2306 				paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2307 				paddri->spinfo_rto = net->RTO;
2308 				paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2309 				SCTP_TCB_UNLOCK(stcb);
2310 			} else {
2311 				if (stcb) {
2312 					SCTP_TCB_UNLOCK(stcb);
2313 				}
2314 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2315 				error = ENOENT;
2316 			}
2317 			*optsize = sizeof(struct sctp_paddrinfo);
2318 		}
2319 		break;
2320 	case SCTP_PCB_STATUS:
2321 		{
2322 			struct sctp_pcbinfo *spcb;
2323 
2324 			SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2325 			sctp_fill_pcbinfo(spcb);
2326 			*optsize = sizeof(struct sctp_pcbinfo);
2327 		}
2328 		break;
2329 
2330 	case SCTP_STATUS:
2331 		{
2332 			struct sctp_nets *net;
2333 			struct sctp_status *sstat;
2334 
2335 			SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2336 			SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2337 
2338 			if (stcb == NULL) {
2339 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2340 				error = EINVAL;
2341 				break;
2342 			}
2343 			/*
2344 			 * I think passing the state is fine since
2345 			 * sctp_constants.h will be available to the user
2346 			 * land.
2347 			 */
2348 			sstat->sstat_state = stcb->asoc.state;
2349 			sstat->sstat_assoc_id = sctp_get_associd(stcb);
2350 			sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2351 			sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2352 			/*
2353 			 * We can't include chunks that have been passed to
2354 			 * the socket layer. Only things in queue.
2355 			 */
2356 			sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2357 			    stcb->asoc.cnt_on_all_streams);
2358 
2359 
2360 			sstat->sstat_instrms = stcb->asoc.streamincnt;
2361 			sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2362 			sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2363 			memcpy(&sstat->sstat_primary.spinfo_address,
2364 			    &stcb->asoc.primary_destination->ro._l_addr,
2365 			    ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2366 			net = stcb->asoc.primary_destination;
2367 			((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2368 			/*
2369 			 * Again the user can get info from sctp_constants.h
2370 			 * for what the state of the network is.
2371 			 */
2372 			sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2373 			sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2374 			sstat->sstat_primary.spinfo_srtt = net->lastsa;
2375 			sstat->sstat_primary.spinfo_rto = net->RTO;
2376 			sstat->sstat_primary.spinfo_mtu = net->mtu;
2377 			sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2378 			SCTP_TCB_UNLOCK(stcb);
2379 			*optsize = sizeof(*sstat);
2380 		}
2381 		break;
2382 	case SCTP_RTOINFO:
2383 		{
2384 			struct sctp_rtoinfo *srto;
2385 
2386 			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2387 			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2388 
2389 			if (stcb) {
2390 				srto->srto_initial = stcb->asoc.initial_rto;
2391 				srto->srto_max = stcb->asoc.maxrto;
2392 				srto->srto_min = stcb->asoc.minrto;
2393 				SCTP_TCB_UNLOCK(stcb);
2394 			} else {
2395 				SCTP_INP_RLOCK(inp);
2396 				srto->srto_initial = inp->sctp_ep.initial_rto;
2397 				srto->srto_max = inp->sctp_ep.sctp_maxrto;
2398 				srto->srto_min = inp->sctp_ep.sctp_minrto;
2399 				SCTP_INP_RUNLOCK(inp);
2400 			}
2401 			*optsize = sizeof(*srto);
2402 		}
2403 		break;
2404 	case SCTP_ASSOCINFO:
2405 		{
2406 			struct sctp_assocparams *sasoc;
2407 			uint32_t oldval;
2408 
2409 			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2410 			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2411 
2412 			if (stcb) {
2413 				oldval = sasoc->sasoc_cookie_life;
2414 				sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2415 				sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2416 				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2417 				sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2418 				sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2419 				SCTP_TCB_UNLOCK(stcb);
2420 			} else {
2421 				SCTP_INP_RLOCK(inp);
2422 				sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2423 				sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2424 				sasoc->sasoc_number_peer_destinations = 0;
2425 				sasoc->sasoc_peer_rwnd = 0;
2426 				sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2427 				SCTP_INP_RUNLOCK(inp);
2428 			}
2429 			*optsize = sizeof(*sasoc);
2430 		}
2431 		break;
2432 	case SCTP_DEFAULT_SEND_PARAM:
2433 		{
2434 			struct sctp_sndrcvinfo *s_info;
2435 
2436 			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2437 			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2438 
2439 			if (stcb) {
2440 				memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2441 				SCTP_TCB_UNLOCK(stcb);
2442 			} else {
2443 				SCTP_INP_RLOCK(inp);
2444 				memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2445 				SCTP_INP_RUNLOCK(inp);
2446 			}
2447 			*optsize = sizeof(*s_info);
2448 		}
2449 		break;
2450 	case SCTP_INITMSG:
2451 		{
2452 			struct sctp_initmsg *sinit;
2453 
2454 			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2455 			SCTP_INP_RLOCK(inp);
2456 			sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2457 			sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2458 			sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2459 			sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2460 			SCTP_INP_RUNLOCK(inp);
2461 			*optsize = sizeof(*sinit);
2462 		}
2463 		break;
2464 	case SCTP_PRIMARY_ADDR:
2465 		/* we allow a "get" operation on this */
2466 		{
2467 			struct sctp_setprim *ssp;
2468 
2469 			SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2470 			SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2471 
2472 			if (stcb) {
2473 				/* simply copy out the sockaddr_storage... */
2474 				int len;
2475 
2476 				len = *optsize;
2477 				if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2478 					len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2479 
2480 				memcpy(&ssp->ssp_addr,
2481 				    &stcb->asoc.primary_destination->ro._l_addr,
2482 				    len);
2483 				SCTP_TCB_UNLOCK(stcb);
2484 			} else {
2485 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2486 				error = EINVAL;
2487 			}
2488 			*optsize = sizeof(*ssp);
2489 		}
2490 		break;
2491 
2492 	case SCTP_HMAC_IDENT:
2493 		{
2494 			struct sctp_hmacalgo *shmac;
2495 			sctp_hmaclist_t *hmaclist;
2496 			uint32_t size;
2497 			int i;
2498 
2499 			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2500 
2501 			SCTP_INP_RLOCK(inp);
2502 			hmaclist = inp->sctp_ep.local_hmacs;
2503 			if (hmaclist == NULL) {
2504 				/* no HMACs to return */
2505 				*optsize = sizeof(*shmac);
2506 				SCTP_INP_RUNLOCK(inp);
2507 				break;
2508 			}
2509 			/* is there room for all of the hmac ids? */
2510 			size = sizeof(*shmac) + (hmaclist->num_algo *
2511 			    sizeof(shmac->shmac_idents[0]));
2512 			if ((size_t)(*optsize) < size) {
2513 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2514 				error = EINVAL;
2515 				SCTP_INP_RUNLOCK(inp);
2516 				break;
2517 			}
2518 			/* copy in the list */
2519 			shmac->shmac_number_of_idents = hmaclist->num_algo;
2520 			for (i = 0; i < hmaclist->num_algo; i++) {
2521 				shmac->shmac_idents[i] = hmaclist->hmac[i];
2522 			}
2523 			SCTP_INP_RUNLOCK(inp);
2524 			*optsize = size;
2525 			break;
2526 		}
2527 	case SCTP_AUTH_ACTIVE_KEY:
2528 		{
2529 			struct sctp_authkeyid *scact;
2530 
2531 			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2532 			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2533 
2534 			if (stcb) {
2535 				/* get the active key on the assoc */
2536 				scact->scact_keynumber = stcb->asoc.authinfo.assoc_keyid;
2537 				SCTP_TCB_UNLOCK(stcb);
2538 			} else {
2539 				/* get the endpoint active key */
2540 				SCTP_INP_RLOCK(inp);
2541 				scact->scact_keynumber = inp->sctp_ep.default_keyid;
2542 				SCTP_INP_RUNLOCK(inp);
2543 			}
2544 			*optsize = sizeof(*scact);
2545 			break;
2546 		}
2547 	case SCTP_LOCAL_AUTH_CHUNKS:
2548 		{
2549 			struct sctp_authchunks *sac;
2550 			sctp_auth_chklist_t *chklist = NULL;
2551 			size_t size = 0;
2552 
2553 			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2554 			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2555 
2556 			if (stcb) {
2557 				/* get off the assoc */
2558 				chklist = stcb->asoc.local_auth_chunks;
2559 				/* is there enough space? */
2560 				size = sctp_auth_get_chklist_size(chklist);
2561 				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2562 					error = EINVAL;
2563 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2564 				} else {
2565 					/* copy in the chunks */
2566 					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2567 				}
2568 				SCTP_TCB_UNLOCK(stcb);
2569 			} else {
2570 				/* get off the endpoint */
2571 				SCTP_INP_RLOCK(inp);
2572 				chklist = inp->sctp_ep.local_auth_chunks;
2573 				/* is there enough space? */
2574 				size = sctp_auth_get_chklist_size(chklist);
2575 				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2576 					error = EINVAL;
2577 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2578 				} else {
2579 					/* copy in the chunks */
2580 					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2581 				}
2582 				SCTP_INP_RUNLOCK(inp);
2583 			}
2584 			*optsize = sizeof(struct sctp_authchunks) + size;
2585 			break;
2586 		}
2587 	case SCTP_PEER_AUTH_CHUNKS:
2588 		{
2589 			struct sctp_authchunks *sac;
2590 			sctp_auth_chklist_t *chklist = NULL;
2591 			size_t size = 0;
2592 
2593 			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2594 			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2595 
2596 			if (stcb) {
2597 				/* get off the assoc */
2598 				chklist = stcb->asoc.peer_auth_chunks;
2599 				/* is there enough space? */
2600 				size = sctp_auth_get_chklist_size(chklist);
2601 				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2602 					error = EINVAL;
2603 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2604 				} else {
2605 					/* copy in the chunks */
2606 					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2607 				}
2608 				SCTP_TCB_UNLOCK(stcb);
2609 			} else {
2610 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2611 				error = ENOENT;
2612 			}
2613 			*optsize = sizeof(struct sctp_authchunks) + size;
2614 			break;
2615 		}
2616 
2617 
2618 	default:
2619 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2620 		error = ENOPROTOOPT;
2621 		*optsize = 0;
2622 		break;
2623 	}			/* end switch (sopt->sopt_name) */
2624 	return (error);
2625 }
2626 
2627 static int
2628 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2629     void *p)
2630 {
2631 	int error, set_opt;
2632 	uint32_t *mopt;
2633 	struct sctp_tcb *stcb = NULL;
2634 	struct sctp_inpcb *inp = NULL;
2635 	uint32_t vrf_id;
2636 
2637 	if (optval == NULL) {
2638 		SCTP_PRINTF("optval is NULL\n");
2639 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2640 		return (EINVAL);
2641 	}
2642 	inp = (struct sctp_inpcb *)so->so_pcb;
2643 	if (inp == 0) {
2644 		SCTP_PRINTF("inp is NULL?\n");
2645 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2646 		return EINVAL;
2647 	}
2648 	vrf_id = inp->def_vrf_id;
2649 
2650 	error = 0;
2651 	switch (optname) {
2652 	case SCTP_NODELAY:
2653 	case SCTP_AUTOCLOSE:
2654 	case SCTP_AUTO_ASCONF:
2655 	case SCTP_EXPLICIT_EOR:
2656 	case SCTP_DISABLE_FRAGMENTS:
2657 	case SCTP_USE_EXT_RCVINFO:
2658 	case SCTP_I_WANT_MAPPED_V4_ADDR:
2659 		/* copy in the option value */
2660 		SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2661 		set_opt = 0;
2662 		if (error)
2663 			break;
2664 		switch (optname) {
2665 		case SCTP_DISABLE_FRAGMENTS:
2666 			set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2667 			break;
2668 		case SCTP_AUTO_ASCONF:
2669 			/*
2670 			 * NOTE: we don't really support this flag
2671 			 */
2672 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2673 				/* only valid for bound all sockets */
2674 				set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2675 			} else {
2676 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2677 				return (EINVAL);
2678 			}
2679 			break;
2680 		case SCTP_EXPLICIT_EOR:
2681 			set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2682 			break;
2683 		case SCTP_USE_EXT_RCVINFO:
2684 			set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2685 			break;
2686 		case SCTP_I_WANT_MAPPED_V4_ADDR:
2687 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2688 				set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2689 			} else {
2690 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2691 				return (EINVAL);
2692 			}
2693 			break;
2694 		case SCTP_NODELAY:
2695 			set_opt = SCTP_PCB_FLAGS_NODELAY;
2696 			break;
2697 		case SCTP_AUTOCLOSE:
2698 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2699 			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2700 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2701 				return (EINVAL);
2702 			}
2703 			set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2704 			/*
2705 			 * The value is in ticks. Note this does not effect
2706 			 * old associations, only new ones.
2707 			 */
2708 			inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2709 			break;
2710 		}
2711 		SCTP_INP_WLOCK(inp);
2712 		if (*mopt != 0) {
2713 			sctp_feature_on(inp, set_opt);
2714 		} else {
2715 			sctp_feature_off(inp, set_opt);
2716 		}
2717 		SCTP_INP_WUNLOCK(inp);
2718 		break;
2719 	case SCTP_REUSE_PORT:
2720 		{
2721 			SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2722 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2723 				/* Can't set it after we are bound */
2724 				error = EINVAL;
2725 				break;
2726 			}
2727 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2728 				/* Can't do this for a 1-m socket */
2729 				error = EINVAL;
2730 				break;
2731 			}
2732 			if (optval)
2733 				sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
2734 			else
2735 				sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
2736 		}
2737 		break;
2738 	case SCTP_PARTIAL_DELIVERY_POINT:
2739 		{
2740 			uint32_t *value;
2741 
2742 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2743 			if (*value > SCTP_SB_LIMIT_RCV(so)) {
2744 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2745 				error = EINVAL;
2746 				break;
2747 			}
2748 			inp->partial_delivery_point = *value;
2749 		}
2750 		break;
2751 	case SCTP_FRAGMENT_INTERLEAVE:
2752 		/* not yet until we re-write sctp_recvmsg() */
2753 		{
2754 			uint32_t *level;
2755 
2756 			SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2757 			if (*level == SCTP_FRAG_LEVEL_2) {
2758 				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2759 				sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2760 			} else if (*level == SCTP_FRAG_LEVEL_1) {
2761 				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2762 				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2763 			} else if (*level == SCTP_FRAG_LEVEL_0) {
2764 				sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2765 				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2766 
2767 			} else {
2768 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2769 				error = EINVAL;
2770 			}
2771 		}
2772 		break;
2773 	case SCTP_CMT_ON_OFF:
2774 		{
2775 			struct sctp_assoc_value *av;
2776 
2777 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2778 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
2779 				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2780 				if (stcb) {
2781 					stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2782 					SCTP_TCB_UNLOCK(stcb);
2783 				} else {
2784 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2785 					error = ENOTCONN;
2786 				}
2787 			} else {
2788 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2789 				error = ENOPROTOOPT;
2790 			}
2791 		}
2792 		break;
2793 		/* JRS - Set socket option for pluggable congestion control */
2794 	case SCTP_PLUGGABLE_CC:
2795 		{
2796 			struct sctp_assoc_value *av;
2797 
2798 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2799 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2800 			if (stcb) {
2801 				switch (av->assoc_value) {
2802 					/*
2803 					 * JRS - Standard TCP congestion
2804 					 * control
2805 					 */
2806 				case SCTP_CC_RFC2581:
2807 					{
2808 						stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
2809 						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2810 						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
2811 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
2812 						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2813 						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2814 						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2815 						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2816 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2817 						SCTP_TCB_UNLOCK(stcb);
2818 						break;
2819 					}
2820 					/*
2821 					 * JRS - High Speed TCP congestion
2822 					 * control (Floyd)
2823 					 */
2824 				case SCTP_CC_HSTCP:
2825 					{
2826 						stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
2827 						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2828 						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
2829 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
2830 						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2831 						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2832 						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2833 						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2834 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2835 						SCTP_TCB_UNLOCK(stcb);
2836 						break;
2837 					}
2838 					/* JRS - HTCP congestion control */
2839 				case SCTP_CC_HTCP:
2840 					{
2841 						stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
2842 						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
2843 						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
2844 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
2845 						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
2846 						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
2847 						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2848 						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2849 						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
2850 						SCTP_TCB_UNLOCK(stcb);
2851 						break;
2852 					}
2853 					/*
2854 					 * JRS - All other values are
2855 					 * invalid
2856 					 */
2857 				default:
2858 					{
2859 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2860 						error = EINVAL;
2861 						SCTP_TCB_UNLOCK(stcb);
2862 						break;
2863 					}
2864 				}
2865 			} else {
2866 				switch (av->assoc_value) {
2867 				case SCTP_CC_RFC2581:
2868 				case SCTP_CC_HSTCP:
2869 				case SCTP_CC_HTCP:
2870 					inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2871 					break;
2872 				default:
2873 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2874 					error = EINVAL;
2875 					break;
2876 				};
2877 			}
2878 		}
2879 		break;
2880 	case SCTP_CLR_STAT_LOG:
2881 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2882 		error = EOPNOTSUPP;
2883 		break;
2884 	case SCTP_CONTEXT:
2885 		{
2886 			struct sctp_assoc_value *av;
2887 
2888 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2889 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2890 
2891 			if (stcb) {
2892 				stcb->asoc.context = av->assoc_value;
2893 				SCTP_TCB_UNLOCK(stcb);
2894 			} else {
2895 				SCTP_INP_WLOCK(inp);
2896 				inp->sctp_context = av->assoc_value;
2897 				SCTP_INP_WUNLOCK(inp);
2898 			}
2899 		}
2900 		break;
2901 	case SCTP_VRF_ID:
2902 		{
2903 			uint32_t *default_vrfid;
2904 
2905 			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2906 			if (*default_vrfid > SCTP_MAX_VRF_ID) {
2907 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2908 				error = EINVAL;
2909 				break;
2910 			}
2911 			inp->def_vrf_id = *default_vrfid;
2912 			break;
2913 		}
2914 	case SCTP_DEL_VRF_ID:
2915 		{
2916 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2917 			error = EOPNOTSUPP;
2918 			break;
2919 		}
2920 	case SCTP_ADD_VRF_ID:
2921 		{
2922 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2923 			error = EOPNOTSUPP;
2924 			break;
2925 		}
2926 	case SCTP_DELAYED_SACK:
2927 		{
2928 			struct sctp_sack_info *sack;
2929 
2930 			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
2931 			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2932 			if (sack->sack_delay) {
2933 				if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
2934 					sack->sack_delay = SCTP_MAX_SACK_DELAY;
2935 			}
2936 			if (stcb) {
2937 				if (sack->sack_delay) {
2938 					if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2939 						sack->sack_delay = TICKS_TO_MSEC(1);
2940 					}
2941 					stcb->asoc.delayed_ack = sack->sack_delay;
2942 				}
2943 				if (sack->sack_freq) {
2944 					stcb->asoc.sack_freq = sack->sack_freq;
2945 				}
2946 				SCTP_TCB_UNLOCK(stcb);
2947 			} else {
2948 				SCTP_INP_WLOCK(inp);
2949 				if (sack->sack_delay) {
2950 					if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2951 						sack->sack_delay = TICKS_TO_MSEC(1);
2952 					}
2953 					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
2954 				}
2955 				if (sack->sack_freq) {
2956 					inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
2957 				}
2958 				SCTP_INP_WUNLOCK(inp);
2959 			}
2960 			break;
2961 		}
2962 	case SCTP_AUTH_CHUNK:
2963 		{
2964 			struct sctp_authchunk *sauth;
2965 
2966 			SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
2967 
2968 			SCTP_INP_WLOCK(inp);
2969 			if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
2970 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2971 				error = EINVAL;
2972 			}
2973 			SCTP_INP_WUNLOCK(inp);
2974 			break;
2975 		}
2976 	case SCTP_AUTH_KEY:
2977 		{
2978 			struct sctp_authkey *sca;
2979 			struct sctp_keyhead *shared_keys;
2980 			sctp_sharedkey_t *shared_key;
2981 			sctp_key_t *key = NULL;
2982 			size_t size;
2983 
2984 			SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
2985 			SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
2986 			size = optsize - sizeof(*sca);
2987 
2988 			if (stcb) {
2989 				/* set it on the assoc */
2990 				shared_keys = &stcb->asoc.shared_keys;
2991 				/* clear the cached keys for this key id */
2992 				sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
2993 				/*
2994 				 * create the new shared key and
2995 				 * insert/replace it
2996 				 */
2997 				if (size > 0) {
2998 					key = sctp_set_key(sca->sca_key, (uint32_t) size);
2999 					if (key == NULL) {
3000 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3001 						error = ENOMEM;
3002 						SCTP_TCB_UNLOCK(stcb);
3003 						break;
3004 					}
3005 				}
3006 				shared_key = sctp_alloc_sharedkey();
3007 				if (shared_key == NULL) {
3008 					sctp_free_key(key);
3009 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3010 					error = ENOMEM;
3011 					SCTP_TCB_UNLOCK(stcb);
3012 					break;
3013 				}
3014 				shared_key->key = key;
3015 				shared_key->keyid = sca->sca_keynumber;
3016 				sctp_insert_sharedkey(shared_keys, shared_key);
3017 				SCTP_TCB_UNLOCK(stcb);
3018 			} else {
3019 				/* set it on the endpoint */
3020 				SCTP_INP_WLOCK(inp);
3021 				shared_keys = &inp->sctp_ep.shared_keys;
3022 				/*
3023 				 * clear the cached keys on all assocs for
3024 				 * this key id
3025 				 */
3026 				sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
3027 				/*
3028 				 * create the new shared key and
3029 				 * insert/replace it
3030 				 */
3031 				if (size > 0) {
3032 					key = sctp_set_key(sca->sca_key, (uint32_t) size);
3033 					if (key == NULL) {
3034 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3035 						error = ENOMEM;
3036 						SCTP_INP_WUNLOCK(inp);
3037 						break;
3038 					}
3039 				}
3040 				shared_key = sctp_alloc_sharedkey();
3041 				if (shared_key == NULL) {
3042 					sctp_free_key(key);
3043 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3044 					error = ENOMEM;
3045 					SCTP_INP_WUNLOCK(inp);
3046 					break;
3047 				}
3048 				shared_key->key = key;
3049 				shared_key->keyid = sca->sca_keynumber;
3050 				sctp_insert_sharedkey(shared_keys, shared_key);
3051 				SCTP_INP_WUNLOCK(inp);
3052 			}
3053 			break;
3054 		}
3055 	case SCTP_HMAC_IDENT:
3056 		{
3057 			struct sctp_hmacalgo *shmac;
3058 			sctp_hmaclist_t *hmaclist;
3059 			uint16_t hmacid;
3060 			uint32_t i;
3061 
3062 			size_t found;
3063 
3064 			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
3065 			if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) {
3066 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3067 				error = EINVAL;
3068 				break;
3069 			}
3070 			hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents);
3071 			if (hmaclist == NULL) {
3072 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3073 				error = ENOMEM;
3074 				break;
3075 			}
3076 			for (i = 0; i < shmac->shmac_number_of_idents; i++) {
3077 				hmacid = shmac->shmac_idents[i];
3078 				if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
3079 					 /* invalid HMACs were found */ ;
3080 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3081 					error = EINVAL;
3082 					sctp_free_hmaclist(hmaclist);
3083 					goto sctp_set_hmac_done;
3084 				}
3085 			}
3086 			found = 0;
3087 			for (i = 0; i < hmaclist->num_algo; i++) {
3088 				if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
3089 					/* already in list */
3090 					found = 1;
3091 				}
3092 			}
3093 			if (!found) {
3094 				sctp_free_hmaclist(hmaclist);
3095 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3096 				error = EINVAL;
3097 				break;
3098 			}
3099 			/* set it on the endpoint */
3100 			SCTP_INP_WLOCK(inp);
3101 			if (inp->sctp_ep.local_hmacs)
3102 				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3103 			inp->sctp_ep.local_hmacs = hmaclist;
3104 			SCTP_INP_WUNLOCK(inp);
3105 	sctp_set_hmac_done:
3106 			break;
3107 		}
3108 	case SCTP_AUTH_ACTIVE_KEY:
3109 		{
3110 			struct sctp_authkeyid *scact;
3111 
3112 			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
3113 			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3114 
3115 			/* set the active key on the right place */
3116 			if (stcb) {
3117 				/* set the active key on the assoc */
3118 				if (sctp_auth_setactivekey(stcb, scact->scact_keynumber)) {
3119 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3120 					error = EINVAL;
3121 				}
3122 				SCTP_TCB_UNLOCK(stcb);
3123 			} else {
3124 				/* set the active key on the endpoint */
3125 				SCTP_INP_WLOCK(inp);
3126 				if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) {
3127 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3128 					error = EINVAL;
3129 				}
3130 				SCTP_INP_WUNLOCK(inp);
3131 			}
3132 			break;
3133 		}
3134 	case SCTP_AUTH_DELETE_KEY:
3135 		{
3136 			struct sctp_authkeyid *scdel;
3137 
3138 			SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
3139 			SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3140 
3141 			/* delete the key from the right place */
3142 			if (stcb) {
3143 				if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) {
3144 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3145 					error = EINVAL;
3146 				}
3147 				SCTP_TCB_UNLOCK(stcb);
3148 			} else {
3149 				SCTP_INP_WLOCK(inp);
3150 				if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) {
3151 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3152 					error = EINVAL;
3153 				}
3154 				SCTP_INP_WUNLOCK(inp);
3155 			}
3156 			break;
3157 		}
3158 
3159 	case SCTP_RESET_STREAMS:
3160 		{
3161 			struct sctp_stream_reset *strrst;
3162 			uint8_t send_in = 0, send_tsn = 0, send_out = 0;
3163 			int i;
3164 
3165 			SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
3166 			SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
3167 
3168 			if (stcb == NULL) {
3169 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3170 				error = ENOENT;
3171 				break;
3172 			}
3173 			if (stcb->asoc.peer_supports_strreset == 0) {
3174 				/*
3175 				 * Peer does not support it, we return
3176 				 * protocol not supported since this is true
3177 				 * for this feature and this peer, not the
3178 				 * socket request in general.
3179 				 */
3180 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
3181 				error = EPROTONOSUPPORT;
3182 				SCTP_TCB_UNLOCK(stcb);
3183 				break;
3184 			}
3185 			if (stcb->asoc.stream_reset_outstanding) {
3186 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3187 				error = EALREADY;
3188 				SCTP_TCB_UNLOCK(stcb);
3189 				break;
3190 			}
3191 			if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
3192 				send_in = 1;
3193 			} else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
3194 				send_out = 1;
3195 			} else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
3196 				send_in = 1;
3197 				send_out = 1;
3198 			} else if (strrst->strrst_flags == SCTP_RESET_TSN) {
3199 				send_tsn = 1;
3200 			} else {
3201 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3202 				error = EINVAL;
3203 				SCTP_TCB_UNLOCK(stcb);
3204 				break;
3205 			}
3206 			for (i = 0; i < strrst->strrst_num_streams; i++) {
3207 				if ((send_in) &&
3208 
3209 				    (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
3210 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3211 					error = EINVAL;
3212 					goto get_out;
3213 				}
3214 				if ((send_out) &&
3215 				    (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
3216 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3217 					error = EINVAL;
3218 					goto get_out;
3219 				}
3220 			}
3221 			if (error) {
3222 		get_out:
3223 				SCTP_TCB_UNLOCK(stcb);
3224 				break;
3225 			}
3226 			error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
3227 			    strrst->strrst_list,
3228 			    send_out, (stcb->asoc.str_reset_seq_in - 3),
3229 			    send_in, send_tsn);
3230 
3231 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
3232 			SCTP_TCB_UNLOCK(stcb);
3233 		}
3234 		break;
3235 
3236 	case SCTP_CONNECT_X:
3237 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3238 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3239 			error = EINVAL;
3240 			break;
3241 		}
3242 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
3243 		break;
3244 
3245 	case SCTP_CONNECT_X_DELAYED:
3246 		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3247 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3248 			error = EINVAL;
3249 			break;
3250 		}
3251 		error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
3252 		break;
3253 
3254 	case SCTP_CONNECT_X_COMPLETE:
3255 		{
3256 			struct sockaddr *sa;
3257 			struct sctp_nets *net;
3258 
3259 			/* FIXME MT: check correct? */
3260 			SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
3261 
3262 			/* find tcb */
3263 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3264 				SCTP_INP_RLOCK(inp);
3265 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
3266 				if (stcb) {
3267 					SCTP_TCB_LOCK(stcb);
3268 					net = sctp_findnet(stcb, sa);
3269 				}
3270 				SCTP_INP_RUNLOCK(inp);
3271 			} else {
3272 				/*
3273 				 * We increment here since
3274 				 * sctp_findassociation_ep_addr() wil do a
3275 				 * decrement if it finds the stcb as long as
3276 				 * the locked tcb (last argument) is NOT a
3277 				 * TCB.. aka NULL.
3278 				 */
3279 				SCTP_INP_INCR_REF(inp);
3280 				stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3281 				if (stcb == NULL) {
3282 					SCTP_INP_DECR_REF(inp);
3283 				}
3284 			}
3285 
3286 			if (stcb == NULL) {
3287 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3288 				error = ENOENT;
3289 				break;
3290 			}
3291 			if (stcb->asoc.delayed_connection == 1) {
3292 				stcb->asoc.delayed_connection = 0;
3293 				(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3294 				sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3295 				    stcb->asoc.primary_destination,
3296 				    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3297 				sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
3298 			} else {
3299 				/*
3300 				 * already expired or did not use delayed
3301 				 * connectx
3302 				 */
3303 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3304 				error = EALREADY;
3305 			}
3306 			SCTP_TCB_UNLOCK(stcb);
3307 		}
3308 		break;
3309 	case SCTP_MAX_BURST:
3310 		{
3311 			uint8_t *burst;
3312 
3313 			SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3314 
3315 			SCTP_INP_WLOCK(inp);
3316 			if (*burst) {
3317 				inp->sctp_ep.max_burst = *burst;
3318 			}
3319 			SCTP_INP_WUNLOCK(inp);
3320 		}
3321 		break;
3322 	case SCTP_MAXSEG:
3323 		{
3324 			struct sctp_assoc_value *av;
3325 			int ovh;
3326 
3327 			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3328 			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3329 
3330 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3331 				ovh = SCTP_MED_OVERHEAD;
3332 			} else {
3333 				ovh = SCTP_MED_V4_OVERHEAD;
3334 			}
3335 			if (stcb) {
3336 				if (av->assoc_value) {
3337 					stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3338 				} else {
3339 					stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3340 				}
3341 				SCTP_TCB_UNLOCK(stcb);
3342 			} else {
3343 				SCTP_INP_WLOCK(inp);
3344 				/*
3345 				 * FIXME MT: I think this is not in tune
3346 				 * with the API ID
3347 				 */
3348 				if (av->assoc_value) {
3349 					inp->sctp_frag_point = (av->assoc_value + ovh);
3350 				} else {
3351 					inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3352 				}
3353 				SCTP_INP_WUNLOCK(inp);
3354 			}
3355 		}
3356 		break;
3357 	case SCTP_EVENTS:
3358 		{
3359 			struct sctp_event_subscribe *events;
3360 
3361 			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3362 
3363 			SCTP_INP_WLOCK(inp);
3364 			if (events->sctp_data_io_event) {
3365 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3366 			} else {
3367 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3368 			}
3369 
3370 			if (events->sctp_association_event) {
3371 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3372 			} else {
3373 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3374 			}
3375 
3376 			if (events->sctp_address_event) {
3377 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3378 			} else {
3379 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3380 			}
3381 
3382 			if (events->sctp_send_failure_event) {
3383 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3384 			} else {
3385 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3386 			}
3387 
3388 			if (events->sctp_peer_error_event) {
3389 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3390 			} else {
3391 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3392 			}
3393 
3394 			if (events->sctp_shutdown_event) {
3395 				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3396 			} else {
3397 				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3398 			}
3399 
3400 			if (events->sctp_partial_delivery_event) {
3401 				sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3402 			} else {
3403 				sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3404 			}
3405 
3406 			if (events->sctp_adaptation_layer_event) {
3407 				sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3408 			} else {
3409 				sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3410 			}
3411 
3412 			if (events->sctp_authentication_event) {
3413 				sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3414 			} else {
3415 				sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3416 			}
3417 
3418 			if (events->sctp_stream_reset_events) {
3419 				sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3420 			} else {
3421 				sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3422 			}
3423 			SCTP_INP_WUNLOCK(inp);
3424 		}
3425 		break;
3426 
3427 	case SCTP_ADAPTATION_LAYER:
3428 		{
3429 			struct sctp_setadaptation *adap_bits;
3430 
3431 			SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3432 			SCTP_INP_WLOCK(inp);
3433 			inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3434 			SCTP_INP_WUNLOCK(inp);
3435 		}
3436 		break;
3437 #ifdef SCTP_DEBUG
3438 	case SCTP_SET_INITIAL_DBG_SEQ:
3439 		{
3440 			uint32_t *vvv;
3441 
3442 			SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3443 			SCTP_INP_WLOCK(inp);
3444 			inp->sctp_ep.initial_sequence_debug = *vvv;
3445 			SCTP_INP_WUNLOCK(inp);
3446 		}
3447 		break;
3448 #endif
3449 	case SCTP_DEFAULT_SEND_PARAM:
3450 		{
3451 			struct sctp_sndrcvinfo *s_info;
3452 
3453 			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3454 			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3455 
3456 			if (stcb) {
3457 				if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3458 					memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3459 				} else {
3460 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3461 					error = EINVAL;
3462 				}
3463 				SCTP_TCB_UNLOCK(stcb);
3464 			} else {
3465 				SCTP_INP_WLOCK(inp);
3466 				memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3467 				SCTP_INP_WUNLOCK(inp);
3468 			}
3469 		}
3470 		break;
3471 	case SCTP_PEER_ADDR_PARAMS:
3472 		/* Applys to the specific association */
3473 		{
3474 			struct sctp_paddrparams *paddrp;
3475 			struct sctp_nets *net;
3476 
3477 			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3478 			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3479 			net = NULL;
3480 			if (stcb) {
3481 				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3482 			} else {
3483 				/*
3484 				 * We increment here since
3485 				 * sctp_findassociation_ep_addr() wil do a
3486 				 * decrement if it finds the stcb as long as
3487 				 * the locked tcb (last argument) is NOT a
3488 				 * TCB.. aka NULL.
3489 				 */
3490 				SCTP_INP_INCR_REF(inp);
3491 				stcb = sctp_findassociation_ep_addr(&inp,
3492 				    (struct sockaddr *)&paddrp->spp_address,
3493 				    &net, NULL, NULL);
3494 				if (stcb == NULL) {
3495 					SCTP_INP_DECR_REF(inp);
3496 				}
3497 			}
3498 			if (stcb && (net == NULL)) {
3499 				struct sockaddr *sa;
3500 
3501 				sa = (struct sockaddr *)&paddrp->spp_address;
3502 				if (sa->sa_family == AF_INET) {
3503 					struct sockaddr_in *sin;
3504 
3505 					sin = (struct sockaddr_in *)sa;
3506 					if (sin->sin_addr.s_addr) {
3507 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3508 						SCTP_TCB_UNLOCK(stcb);
3509 						error = EINVAL;
3510 						break;
3511 					}
3512 				} else if (sa->sa_family == AF_INET6) {
3513 					struct sockaddr_in6 *sin6;
3514 
3515 					sin6 = (struct sockaddr_in6 *)sa;
3516 					if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3517 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3518 						SCTP_TCB_UNLOCK(stcb);
3519 						error = EINVAL;
3520 						break;
3521 					}
3522 				} else {
3523 					error = EAFNOSUPPORT;
3524 					SCTP_TCB_UNLOCK(stcb);
3525 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3526 					break;
3527 				}
3528 			}
3529 			/* sanity checks */
3530 			if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3531 				if (stcb)
3532 					SCTP_TCB_UNLOCK(stcb);
3533 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3534 				return (EINVAL);
3535 			}
3536 			if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3537 				if (stcb)
3538 					SCTP_TCB_UNLOCK(stcb);
3539 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3540 				return (EINVAL);
3541 			}
3542 			if (stcb) {
3543 				/************************TCB SPECIFIC SET ******************/
3544 				/*
3545 				 * do we change the timer for HB, we run
3546 				 * only one?
3547 				 */
3548 				int ovh = 0;
3549 
3550 				if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3551 					ovh = SCTP_MED_OVERHEAD;
3552 				} else {
3553 					ovh = SCTP_MED_V4_OVERHEAD;
3554 				}
3555 
3556 				if (paddrp->spp_hbinterval)
3557 					stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3558 				else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3559 					stcb->asoc.heart_beat_delay = 0;
3560 
3561 				/* network sets ? */
3562 				if (net) {
3563 					/************************NET SPECIFIC SET ******************/
3564 					if (paddrp->spp_flags & SPP_HB_DEMAND) {
3565 						/* on demand HB */
3566 						if (sctp_send_hb(stcb, 1, net) < 0) {
3567 							/* asoc destroyed */
3568 							SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3569 							error = EINVAL;
3570 							break;
3571 						}
3572 					}
3573 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3574 						net->dest_state |= SCTP_ADDR_NOHB;
3575 					}
3576 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3577 						net->dest_state &= ~SCTP_ADDR_NOHB;
3578 					}
3579 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3580 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3581 							sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3582 							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3583 						}
3584 						if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3585 							net->mtu = paddrp->spp_pathmtu + ovh;
3586 							if (net->mtu < stcb->asoc.smallest_mtu) {
3587 #ifdef SCTP_PRINT_FOR_B_AND_M
3588 								SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3589 								    net->mtu);
3590 #endif
3591 								sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3592 							}
3593 						}
3594 					}
3595 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3596 						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3597 							sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3598 						}
3599 					}
3600 					if (paddrp->spp_pathmaxrxt)
3601 						net->failure_threshold = paddrp->spp_pathmaxrxt;
3602 #ifdef INET
3603 					if (paddrp->spp_flags & SPP_IPV4_TOS) {
3604 						if (net->ro._l_addr.sin.sin_family == AF_INET) {
3605 							net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3606 						}
3607 					}
3608 #endif
3609 #ifdef INET6
3610 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3611 						if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3612 							net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3613 						}
3614 					}
3615 #endif
3616 				} else {
3617 					/************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3618 					if (paddrp->spp_pathmaxrxt)
3619 						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3620 
3621 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3622 						/* Turn back on the timer */
3623 						stcb->asoc.hb_is_disabled = 0;
3624 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3625 					}
3626 					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3627 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3628 							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3629 								sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3630 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3631 							}
3632 							if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3633 								net->mtu = paddrp->spp_pathmtu + ovh;
3634 								if (net->mtu < stcb->asoc.smallest_mtu) {
3635 #ifdef SCTP_PRINT_FOR_B_AND_M
3636 									SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3637 									    net->mtu);
3638 #endif
3639 									sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3640 								}
3641 							}
3642 						}
3643 					}
3644 					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3645 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3646 							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3647 								sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3648 							}
3649 						}
3650 					}
3651 					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3652 						int cnt_of_unconf = 0;
3653 						struct sctp_nets *lnet;
3654 
3655 						stcb->asoc.hb_is_disabled = 1;
3656 						TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3657 							if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3658 								cnt_of_unconf++;
3659 							}
3660 						}
3661 						/*
3662 						 * stop the timer ONLY if we
3663 						 * have no unconfirmed
3664 						 * addresses
3665 						 */
3666 						if (cnt_of_unconf == 0) {
3667 							TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3668 								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3669 								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3670 							}
3671 						}
3672 					}
3673 					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3674 						/* start up the timer. */
3675 						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3676 							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3677 						}
3678 					}
3679 #ifdef INET
3680 					if (paddrp->spp_flags & SPP_IPV4_TOS)
3681 						stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3682 #endif
3683 #ifdef INET6
3684 					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3685 						stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3686 #endif
3687 
3688 				}
3689 				SCTP_TCB_UNLOCK(stcb);
3690 			} else {
3691 				/************************NO TCB, SET TO default stuff ******************/
3692 				SCTP_INP_WLOCK(inp);
3693 				/*
3694 				 * For the TOS/FLOWLABEL stuff you set it
3695 				 * with the options on the socket
3696 				 */
3697 				if (paddrp->spp_pathmaxrxt) {
3698 					inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3699 				}
3700 				if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3701 					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3702 				else if (paddrp->spp_hbinterval) {
3703 					if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3704 						paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
3705 					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3706 				}
3707 				if (paddrp->spp_flags & SPP_HB_ENABLE) {
3708 					sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3709 
3710 				} else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3711 					sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3712 				}
3713 				SCTP_INP_WUNLOCK(inp);
3714 			}
3715 		}
3716 		break;
3717 	case SCTP_RTOINFO:
3718 		{
3719 			struct sctp_rtoinfo *srto;
3720 			uint32_t new_init, new_min, new_max;
3721 
3722 			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3723 			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3724 
3725 			if (stcb) {
3726 				if (srto->srto_initial)
3727 					new_init = srto->srto_initial;
3728 				else
3729 					new_init = stcb->asoc.initial_rto;
3730 				if (srto->srto_max)
3731 					new_max = srto->srto_max;
3732 				else
3733 					new_max = stcb->asoc.maxrto;
3734 				if (srto->srto_min)
3735 					new_min = srto->srto_min;
3736 				else
3737 					new_min = stcb->asoc.minrto;
3738 				if ((new_min <= new_init) && (new_init <= new_max)) {
3739 					stcb->asoc.initial_rto = new_init;
3740 					stcb->asoc.maxrto = new_max;
3741 					stcb->asoc.minrto = new_min;
3742 				} else {
3743 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3744 					error = EINVAL;
3745 				}
3746 				SCTP_TCB_UNLOCK(stcb);
3747 			} else {
3748 				SCTP_INP_WLOCK(inp);
3749 				if (srto->srto_initial)
3750 					new_init = srto->srto_initial;
3751 				else
3752 					new_init = inp->sctp_ep.initial_rto;
3753 				if (srto->srto_max)
3754 					new_max = srto->srto_max;
3755 				else
3756 					new_max = inp->sctp_ep.sctp_maxrto;
3757 				if (srto->srto_min)
3758 					new_min = srto->srto_min;
3759 				else
3760 					new_min = inp->sctp_ep.sctp_minrto;
3761 				if ((new_min <= new_init) && (new_init <= new_max)) {
3762 					inp->sctp_ep.initial_rto = new_init;
3763 					inp->sctp_ep.sctp_maxrto = new_max;
3764 					inp->sctp_ep.sctp_minrto = new_min;
3765 				} else {
3766 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3767 					error = EINVAL;
3768 				}
3769 				SCTP_INP_WUNLOCK(inp);
3770 			}
3771 		}
3772 		break;
3773 	case SCTP_ASSOCINFO:
3774 		{
3775 			struct sctp_assocparams *sasoc;
3776 
3777 			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3778 			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3779 			if (sasoc->sasoc_cookie_life) {
3780 				/* boundary check the cookie life */
3781 				if (sasoc->sasoc_cookie_life < 1000)
3782 					sasoc->sasoc_cookie_life = 1000;
3783 				if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
3784 					sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
3785 				}
3786 			}
3787 			if (stcb) {
3788 				if (sasoc->sasoc_asocmaxrxt)
3789 					stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3790 				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3791 				sasoc->sasoc_peer_rwnd = 0;
3792 				sasoc->sasoc_local_rwnd = 0;
3793 				if (sasoc->sasoc_cookie_life) {
3794 					stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3795 				}
3796 				SCTP_TCB_UNLOCK(stcb);
3797 			} else {
3798 				SCTP_INP_WLOCK(inp);
3799 				if (sasoc->sasoc_asocmaxrxt)
3800 					inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3801 				sasoc->sasoc_number_peer_destinations = 0;
3802 				sasoc->sasoc_peer_rwnd = 0;
3803 				sasoc->sasoc_local_rwnd = 0;
3804 				if (sasoc->sasoc_cookie_life) {
3805 					inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3806 				}
3807 				SCTP_INP_WUNLOCK(inp);
3808 			}
3809 		}
3810 		break;
3811 	case SCTP_INITMSG:
3812 		{
3813 			struct sctp_initmsg *sinit;
3814 
3815 			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
3816 			SCTP_INP_WLOCK(inp);
3817 			if (sinit->sinit_num_ostreams)
3818 				inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3819 
3820 			if (sinit->sinit_max_instreams)
3821 				inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3822 
3823 			if (sinit->sinit_max_attempts)
3824 				inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3825 
3826 			if (sinit->sinit_max_init_timeo)
3827 				inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3828 			SCTP_INP_WUNLOCK(inp);
3829 		}
3830 		break;
3831 	case SCTP_PRIMARY_ADDR:
3832 		{
3833 			struct sctp_setprim *spa;
3834 			struct sctp_nets *net, *lnet;
3835 
3836 			SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
3837 			SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
3838 
3839 			net = NULL;
3840 			if (stcb) {
3841 				net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3842 			} else {
3843 				/*
3844 				 * We increment here since
3845 				 * sctp_findassociation_ep_addr() wil do a
3846 				 * decrement if it finds the stcb as long as
3847 				 * the locked tcb (last argument) is NOT a
3848 				 * TCB.. aka NULL.
3849 				 */
3850 				SCTP_INP_INCR_REF(inp);
3851 				stcb = sctp_findassociation_ep_addr(&inp,
3852 				    (struct sockaddr *)&spa->ssp_addr,
3853 				    &net, NULL, NULL);
3854 				if (stcb == NULL) {
3855 					SCTP_INP_DECR_REF(inp);
3856 				}
3857 			}
3858 
3859 			if ((stcb) && (net)) {
3860 				if ((net != stcb->asoc.primary_destination) &&
3861 				    (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3862 					/* Ok we need to set it */
3863 					lnet = stcb->asoc.primary_destination;
3864 					if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
3865 						if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3866 							net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3867 						}
3868 						net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3869 					}
3870 				}
3871 			} else {
3872 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3873 				error = EINVAL;
3874 			}
3875 			if (stcb) {
3876 				SCTP_TCB_UNLOCK(stcb);
3877 			}
3878 		}
3879 		break;
3880 	case SCTP_SET_DYNAMIC_PRIMARY:
3881 		{
3882 			union sctp_sockstore *ss;
3883 
3884 			error = priv_check(curthread,
3885 			    PRIV_NETINET_RESERVEDPORT);
3886 			if (error)
3887 				break;
3888 
3889 			SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
3890 			/* SUPER USER CHECK? */
3891 			error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
3892 		}
3893 		break;
3894 	case SCTP_SET_PEER_PRIMARY_ADDR:
3895 		{
3896 			struct sctp_setpeerprim *sspp;
3897 
3898 			SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
3899 			SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
3900 			if (stcb != NULL) {
3901 				struct sctp_ifa *ifa;
3902 
3903 				ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
3904 				    stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
3905 				if (ifa == NULL) {
3906 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3907 					error = EINVAL;
3908 					goto out_of_it;
3909 				}
3910 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3911 					/*
3912 					 * Must validate the ifa found is in
3913 					 * our ep
3914 					 */
3915 					struct sctp_laddr *laddr;
3916 					int found = 0;
3917 
3918 					LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3919 						if (laddr->ifa == NULL) {
3920 							SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
3921 							    __FUNCTION__);
3922 							continue;
3923 						}
3924 						if (laddr->ifa == ifa) {
3925 							found = 1;
3926 							break;
3927 						}
3928 					}
3929 					if (!found) {
3930 						SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3931 						error = EINVAL;
3932 						goto out_of_it;
3933 					}
3934 				}
3935 				if (sctp_set_primary_ip_address_sa(stcb,
3936 				    (struct sockaddr *)&sspp->sspp_addr) != 0) {
3937 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3938 					error = EINVAL;
3939 				}
3940 		out_of_it:
3941 				SCTP_TCB_UNLOCK(stcb);
3942 			} else {
3943 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3944 				error = EINVAL;
3945 			}
3946 
3947 		}
3948 		break;
3949 	case SCTP_BINDX_ADD_ADDR:
3950 		{
3951 			struct sctp_getaddresses *addrs;
3952 			size_t sz;
3953 			struct thread *td;
3954 			int prison = 0;
3955 
3956 			td = (struct thread *)p;
3957 			if (jailed(td->td_ucred)) {
3958 				prison = 1;
3959 			}
3960 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
3961 			    optsize);
3962 			if (addrs->addr->sa_family == AF_INET) {
3963 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
3964 				if (optsize < sz) {
3965 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3966 					error = EINVAL;
3967 					break;
3968 				}
3969 				if (prison && prison_ip(td->td_ucred, 0, &(((struct sockaddr_in *)(addrs->addr))->sin_addr.s_addr))) {
3970 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRNOTAVAIL);
3971 					error = EADDRNOTAVAIL;
3972 				}
3973 			} else if (addrs->addr->sa_family == AF_INET6) {
3974 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
3975 				if (optsize < sz) {
3976 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3977 					error = EINVAL;
3978 					break;
3979 				}
3980 				/* JAIL XXXX Add else here for V6 */
3981 			}
3982 			sctp_bindx_add_address(so, inp, addrs->addr,
3983 			    addrs->sget_assoc_id, vrf_id,
3984 			    &error, p);
3985 		}
3986 		break;
3987 	case SCTP_BINDX_REM_ADDR:
3988 		{
3989 			struct sctp_getaddresses *addrs;
3990 			size_t sz;
3991 			struct thread *td;
3992 			int prison = 0;
3993 
3994 			td = (struct thread *)p;
3995 			if (jailed(td->td_ucred)) {
3996 				prison = 1;
3997 			}
3998 			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3999 			if (addrs->addr->sa_family == AF_INET) {
4000 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4001 				if (optsize < sz) {
4002 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4003 					error = EINVAL;
4004 					break;
4005 				}
4006 				if (prison && prison_ip(td->td_ucred, 0, &(((struct sockaddr_in *)(addrs->addr))->sin_addr.s_addr))) {
4007 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRNOTAVAIL);
4008 					error = EADDRNOTAVAIL;
4009 				}
4010 			} else if (addrs->addr->sa_family == AF_INET6) {
4011 				sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4012 				if (optsize < sz) {
4013 					SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4014 					error = EINVAL;
4015 					break;
4016 				}
4017 				/* JAIL XXXX Add else here for V6 */
4018 			}
4019 			sctp_bindx_delete_address(so, inp, addrs->addr,
4020 			    addrs->sget_assoc_id, vrf_id,
4021 			    &error);
4022 		}
4023 		break;
4024 	default:
4025 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
4026 		error = ENOPROTOOPT;
4027 		break;
4028 	}			/* end switch (opt) */
4029 	return (error);
4030 }
4031 
4032 int
4033 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
4034 {
4035 	void *optval = NULL;
4036 	size_t optsize = 0;
4037 	struct sctp_inpcb *inp;
4038 	void *p;
4039 	int error = 0;
4040 
4041 	inp = (struct sctp_inpcb *)so->so_pcb;
4042 	if (inp == 0) {
4043 		/* I made the same as TCP since we are not setup? */
4044 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4045 		return (ECONNRESET);
4046 	}
4047 	if (sopt->sopt_level != IPPROTO_SCTP) {
4048 		/* wrong proto level... send back up to IP */
4049 #ifdef INET6
4050 		if (INP_CHECK_SOCKAF(so, AF_INET6))
4051 			error = ip6_ctloutput(so, sopt);
4052 		else
4053 #endif				/* INET6 */
4054 			error = ip_ctloutput(so, sopt);
4055 		return (error);
4056 	}
4057 	optsize = sopt->sopt_valsize;
4058 	if (optsize) {
4059 		SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
4060 		if (optval == NULL) {
4061 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
4062 			return (ENOBUFS);
4063 		}
4064 		error = sooptcopyin(sopt, optval, optsize, optsize);
4065 		if (error) {
4066 			SCTP_FREE(optval, SCTP_M_SOCKOPT);
4067 			goto out;
4068 		}
4069 	}
4070 	p = (void *)sopt->sopt_td;
4071 	if (sopt->sopt_dir == SOPT_SET) {
4072 		error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
4073 	} else if (sopt->sopt_dir == SOPT_GET) {
4074 		error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
4075 	} else {
4076 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4077 		error = EINVAL;
4078 	}
4079 	if ((error == 0) && (optval != NULL)) {
4080 		error = sooptcopyout(sopt, optval, optsize);
4081 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
4082 	} else if (optval != NULL) {
4083 		SCTP_FREE(optval, SCTP_M_SOCKOPT);
4084 	}
4085 out:
4086 	return (error);
4087 }
4088 
4089 
4090 static int
4091 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
4092 {
4093 	int error = 0;
4094 	int create_lock_on = 0;
4095 	uint32_t vrf_id;
4096 	struct sctp_inpcb *inp;
4097 	struct sctp_tcb *stcb = NULL;
4098 
4099 	inp = (struct sctp_inpcb *)so->so_pcb;
4100 	if (inp == 0) {
4101 		/* I made the same as TCP since we are not setup? */
4102 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4103 		return (ECONNRESET);
4104 	}
4105 	if (addr == NULL) {
4106 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4107 		return EINVAL;
4108 	}
4109 	if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
4110 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4111 		return (EINVAL);
4112 	}
4113 	if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
4114 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4115 		return (EINVAL);
4116 	}
4117 	SCTP_INP_INCR_REF(inp);
4118 	SCTP_ASOC_CREATE_LOCK(inp);
4119 	create_lock_on = 1;
4120 
4121 
4122 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4123 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4124 		/* Should I really unlock ? */
4125 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
4126 		error = EFAULT;
4127 		goto out_now;
4128 	}
4129 #ifdef INET6
4130 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
4131 	    (addr->sa_family == AF_INET6)) {
4132 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4133 		error = EINVAL;
4134 		goto out_now;
4135 	}
4136 #endif				/* INET6 */
4137 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
4138 	    SCTP_PCB_FLAGS_UNBOUND) {
4139 		/* Bind a ephemeral port */
4140 		error = sctp_inpcb_bind(so, NULL, NULL, p);
4141 		if (error) {
4142 			goto out_now;
4143 		}
4144 	}
4145 	/* Now do we connect? */
4146 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4147 	    (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
4148 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4149 		error = EINVAL;
4150 		goto out_now;
4151 	}
4152 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4153 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4154 		/* We are already connected AND the TCP model */
4155 		SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4156 		error = EADDRINUSE;
4157 		goto out_now;
4158 	}
4159 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4160 		SCTP_INP_RLOCK(inp);
4161 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
4162 		SCTP_INP_RUNLOCK(inp);
4163 	} else {
4164 		/*
4165 		 * We increment here since sctp_findassociation_ep_addr()
4166 		 * will do a decrement if it finds the stcb as long as the
4167 		 * locked tcb (last argument) is NOT a TCB.. aka NULL.
4168 		 */
4169 		SCTP_INP_INCR_REF(inp);
4170 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
4171 		if (stcb == NULL) {
4172 			SCTP_INP_DECR_REF(inp);
4173 		} else {
4174 			SCTP_TCB_UNLOCK(stcb);
4175 		}
4176 	}
4177 	if (stcb != NULL) {
4178 		/* Already have or am bring up an association */
4179 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4180 		error = EALREADY;
4181 		goto out_now;
4182 	}
4183 	vrf_id = inp->def_vrf_id;
4184 	/* We are GOOD to go */
4185 	stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
4186 	if (stcb == NULL) {
4187 		/* Gak! no memory */
4188 		goto out_now;
4189 	}
4190 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
4191 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4192 		/* Set the connected flag so we can queue data */
4193 		soisconnecting(so);
4194 	}
4195 	SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
4196 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4197 
4198 	/* initialize authentication parameters for the assoc */
4199 	sctp_initialize_auth_params(inp, stcb);
4200 
4201 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4202 	SCTP_TCB_UNLOCK(stcb);
4203 out_now:
4204 	if (create_lock_on) {
4205 		SCTP_ASOC_CREATE_UNLOCK(inp);
4206 	}
4207 	SCTP_INP_DECR_REF(inp);
4208 	return error;
4209 }
4210 
4211 int
4212 sctp_listen(struct socket *so, int backlog, struct thread *p)
4213 {
4214 	/*
4215 	 * Note this module depends on the protocol processing being called
4216 	 * AFTER any socket level flags and backlog are applied to the
4217 	 * socket. The traditional way that the socket flags are applied is
4218 	 * AFTER protocol processing. We have made a change to the
4219 	 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
4220 	 * place if the socket API for SCTP is to work properly.
4221 	 */
4222 
4223 	int error = 0;
4224 	struct sctp_inpcb *inp;
4225 
4226 	inp = (struct sctp_inpcb *)so->so_pcb;
4227 	if (inp == 0) {
4228 		/* I made the same as TCP since we are not setup? */
4229 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4230 		return (ECONNRESET);
4231 	}
4232 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
4233 		/* See if we have a listener */
4234 		struct sctp_inpcb *tinp;
4235 		union sctp_sockstore store, *sp;
4236 
4237 		sp = &store;
4238 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4239 			/* not bound all */
4240 			struct sctp_laddr *laddr;
4241 
4242 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4243 				memcpy(&store, &laddr->ifa->address, sizeof(store));
4244 				sp->sin.sin_port = inp->sctp_lport;
4245 				tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4246 				if (tinp && (tinp != inp) &&
4247 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4248 				    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4249 				    (tinp->sctp_socket->so_qlimit)) {
4250 					/*
4251 					 * we have a listener already and
4252 					 * its not this inp.
4253 					 */
4254 					SCTP_INP_DECR_REF(tinp);
4255 					return (EADDRINUSE);
4256 				} else if (tinp) {
4257 					SCTP_INP_DECR_REF(tinp);
4258 				}
4259 			}
4260 		} else {
4261 			/* Setup a local addr bound all */
4262 			memset(&store, 0, sizeof(store));
4263 			store.sin.sin_port = inp->sctp_lport;
4264 #ifdef INET6
4265 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4266 				store.sa.sa_family = AF_INET6;
4267 				store.sa.sa_len = sizeof(struct sockaddr_in6);
4268 			}
4269 #endif
4270 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
4271 				store.sa.sa_family = AF_INET;
4272 				store.sa.sa_len = sizeof(struct sockaddr_in);
4273 			}
4274 			tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4275 			if (tinp && (tinp != inp) &&
4276 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4277 			    ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4278 			    (tinp->sctp_socket->so_qlimit)) {
4279 				/*
4280 				 * we have a listener already and its not
4281 				 * this inp.
4282 				 */
4283 				SCTP_INP_DECR_REF(tinp);
4284 				return (EADDRINUSE);
4285 			} else if (tinp) {
4286 				SCTP_INP_DECR_REF(inp);
4287 			}
4288 		}
4289 	}
4290 	SCTP_INP_RLOCK(inp);
4291 #ifdef SCTP_LOCK_LOGGING
4292 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
4293 		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
4294 	}
4295 #endif
4296 	SOCK_LOCK(so);
4297 	error = solisten_proto_check(so);
4298 	if (error) {
4299 		SOCK_UNLOCK(so);
4300 		SCTP_INP_RUNLOCK(inp);
4301 		return (error);
4302 	}
4303 	if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
4304 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4305 		/*
4306 		 * The unlucky case - We are in the tcp pool with this guy.
4307 		 * - Someone else is in the main inp slot. - We must move
4308 		 * this guy (the listener) to the main slot - We must then
4309 		 * move the guy that was listener to the TCP Pool.
4310 		 */
4311 		if (sctp_swap_inpcb_for_listen(inp)) {
4312 			goto in_use;
4313 		}
4314 	}
4315 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4316 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4317 		/* We are already connected AND the TCP model */
4318 in_use:
4319 		SCTP_INP_RUNLOCK(inp);
4320 		SOCK_UNLOCK(so);
4321 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4322 		return (EADDRINUSE);
4323 	}
4324 	SCTP_INP_RUNLOCK(inp);
4325 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4326 		/* We must do a bind. */
4327 		SOCK_UNLOCK(so);
4328 		if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
4329 			/* bind error, probably perm */
4330 			return (error);
4331 		}
4332 		SOCK_LOCK(so);
4333 	}
4334 	/* It appears for 7.0 and on, we must always call this. */
4335 	solisten_proto(so, backlog);
4336 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4337 		/* remove the ACCEPTCONN flag for one-to-many sockets */
4338 		so->so_options &= ~SO_ACCEPTCONN;
4339 	}
4340 	if (backlog == 0) {
4341 		/* turning off listen */
4342 		so->so_options &= ~SO_ACCEPTCONN;
4343 	}
4344 	SOCK_UNLOCK(so);
4345 	return (error);
4346 }
4347 
4348 static int sctp_defered_wakeup_cnt = 0;
4349 
4350 int
4351 sctp_accept(struct socket *so, struct sockaddr **addr)
4352 {
4353 	struct sctp_tcb *stcb;
4354 	struct sctp_inpcb *inp;
4355 	union sctp_sockstore store;
4356 
4357 #ifdef INET6
4358 	int error;
4359 
4360 #endif
4361 	inp = (struct sctp_inpcb *)so->so_pcb;
4362 
4363 	if (inp == 0) {
4364 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4365 		return (ECONNRESET);
4366 	}
4367 	SCTP_INP_RLOCK(inp);
4368 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4369 		SCTP_INP_RUNLOCK(inp);
4370 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4371 		return (EOPNOTSUPP);
4372 	}
4373 	if (so->so_state & SS_ISDISCONNECTED) {
4374 		SCTP_INP_RUNLOCK(inp);
4375 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
4376 		return (ECONNABORTED);
4377 	}
4378 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
4379 	if (stcb == NULL) {
4380 		SCTP_INP_RUNLOCK(inp);
4381 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4382 		return (ECONNRESET);
4383 	}
4384 	SCTP_TCB_LOCK(stcb);
4385 	SCTP_INP_RUNLOCK(inp);
4386 	store = stcb->asoc.primary_destination->ro._l_addr;
4387 	SCTP_TCB_UNLOCK(stcb);
4388 	switch (store.sa.sa_family) {
4389 	case AF_INET:
4390 		{
4391 			struct sockaddr_in *sin;
4392 
4393 			SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4394 			sin->sin_family = AF_INET;
4395 			sin->sin_len = sizeof(*sin);
4396 			sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
4397 			sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
4398 			*addr = (struct sockaddr *)sin;
4399 			break;
4400 		}
4401 #ifdef INET6
4402 	case AF_INET6:
4403 		{
4404 			struct sockaddr_in6 *sin6;
4405 
4406 			SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
4407 			sin6->sin6_family = AF_INET6;
4408 			sin6->sin6_len = sizeof(*sin6);
4409 			sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
4410 
4411 			sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
4412 			if ((error = sa6_recoverscope(sin6)) != 0) {
4413 				SCTP_FREE_SONAME(sin6);
4414 				return (error);
4415 			}
4416 			*addr = (struct sockaddr *)sin6;
4417 			break;
4418 		}
4419 #endif
4420 	default:
4421 		/* TSNH */
4422 		break;
4423 	}
4424 	/* Wake any delayed sleep action */
4425 	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4426 		SCTP_INP_WLOCK(inp);
4427 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4428 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4429 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4430 			SCTP_INP_WUNLOCK(inp);
4431 			SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4432 			if (sowriteable(inp->sctp_socket)) {
4433 				sowwakeup_locked(inp->sctp_socket);
4434 			} else {
4435 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4436 			}
4437 			SCTP_INP_WLOCK(inp);
4438 		}
4439 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4440 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4441 			SCTP_INP_WUNLOCK(inp);
4442 			SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4443 			if (soreadable(inp->sctp_socket)) {
4444 				sctp_defered_wakeup_cnt++;
4445 				sorwakeup_locked(inp->sctp_socket);
4446 			} else {
4447 				SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4448 			}
4449 			SCTP_INP_WLOCK(inp);
4450 		}
4451 		SCTP_INP_WUNLOCK(inp);
4452 	}
4453 	return (0);
4454 }
4455 
4456 int
4457 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4458 {
4459 	struct sockaddr_in *sin;
4460 	uint32_t vrf_id;
4461 	struct sctp_inpcb *inp;
4462 	struct sctp_ifa *sctp_ifa;
4463 
4464 	/*
4465 	 * Do the malloc first in case it blocks.
4466 	 */
4467 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4468 	sin->sin_family = AF_INET;
4469 	sin->sin_len = sizeof(*sin);
4470 	inp = (struct sctp_inpcb *)so->so_pcb;
4471 	if (!inp) {
4472 		SCTP_FREE_SONAME(sin);
4473 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4474 		return ECONNRESET;
4475 	}
4476 	SCTP_INP_RLOCK(inp);
4477 	sin->sin_port = inp->sctp_lport;
4478 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4479 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4480 			struct sctp_tcb *stcb;
4481 			struct sockaddr_in *sin_a;
4482 			struct sctp_nets *net;
4483 			int fnd;
4484 
4485 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
4486 			if (stcb == NULL) {
4487 				goto notConn;
4488 			}
4489 			fnd = 0;
4490 			sin_a = NULL;
4491 			SCTP_TCB_LOCK(stcb);
4492 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4493 				sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4494 				if (sin_a == NULL)
4495 					/* this will make coverity happy */
4496 					continue;
4497 
4498 				if (sin_a->sin_family == AF_INET) {
4499 					fnd = 1;
4500 					break;
4501 				}
4502 			}
4503 			if ((!fnd) || (sin_a == NULL)) {
4504 				/* punt */
4505 				SCTP_TCB_UNLOCK(stcb);
4506 				goto notConn;
4507 			}
4508 			vrf_id = inp->def_vrf_id;
4509 			sctp_ifa = sctp_source_address_selection(inp,
4510 			    stcb,
4511 			    (sctp_route_t *) & net->ro,
4512 			    net, 0, vrf_id);
4513 			if (sctp_ifa) {
4514 				sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4515 				sctp_free_ifa(sctp_ifa);
4516 			}
4517 			SCTP_TCB_UNLOCK(stcb);
4518 		} else {
4519 			/* For the bound all case you get back 0 */
4520 	notConn:
4521 			sin->sin_addr.s_addr = 0;
4522 		}
4523 
4524 	} else {
4525 		/* Take the first IPv4 address in the list */
4526 		struct sctp_laddr *laddr;
4527 		int fnd = 0;
4528 
4529 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4530 			if (laddr->ifa->address.sa.sa_family == AF_INET) {
4531 				struct sockaddr_in *sin_a;
4532 
4533 				sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4534 				sin->sin_addr = sin_a->sin_addr;
4535 				fnd = 1;
4536 				break;
4537 			}
4538 		}
4539 		if (!fnd) {
4540 			SCTP_FREE_SONAME(sin);
4541 			SCTP_INP_RUNLOCK(inp);
4542 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4543 			return ENOENT;
4544 		}
4545 	}
4546 	SCTP_INP_RUNLOCK(inp);
4547 	(*addr) = (struct sockaddr *)sin;
4548 	return (0);
4549 }
4550 
4551 int
4552 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4553 {
4554 	struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4555 	int fnd;
4556 	struct sockaddr_in *sin_a;
4557 	struct sctp_inpcb *inp;
4558 	struct sctp_tcb *stcb;
4559 	struct sctp_nets *net;
4560 
4561 	/* Do the malloc first in case it blocks. */
4562 	inp = (struct sctp_inpcb *)so->so_pcb;
4563 	if ((inp == NULL) ||
4564 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4565 		/* UDP type and listeners will drop out here */
4566 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
4567 		return (ENOTCONN);
4568 	}
4569 	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4570 	sin->sin_family = AF_INET;
4571 	sin->sin_len = sizeof(*sin);
4572 
4573 	/* We must recapture incase we blocked */
4574 	inp = (struct sctp_inpcb *)so->so_pcb;
4575 	if (!inp) {
4576 		SCTP_FREE_SONAME(sin);
4577 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4578 		return ECONNRESET;
4579 	}
4580 	SCTP_INP_RLOCK(inp);
4581 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
4582 	if (stcb) {
4583 		SCTP_TCB_LOCK(stcb);
4584 	}
4585 	SCTP_INP_RUNLOCK(inp);
4586 	if (stcb == NULL) {
4587 		SCTP_FREE_SONAME(sin);
4588 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4589 		return ECONNRESET;
4590 	}
4591 	fnd = 0;
4592 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4593 		sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4594 		if (sin_a->sin_family == AF_INET) {
4595 			fnd = 1;
4596 			sin->sin_port = stcb->rport;
4597 			sin->sin_addr = sin_a->sin_addr;
4598 			break;
4599 		}
4600 	}
4601 	SCTP_TCB_UNLOCK(stcb);
4602 	if (!fnd) {
4603 		/* No IPv4 address */
4604 		SCTP_FREE_SONAME(sin);
4605 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4606 		return ENOENT;
4607 	}
4608 	(*addr) = (struct sockaddr *)sin;
4609 	return (0);
4610 }
4611 
4612 struct pr_usrreqs sctp_usrreqs = {
4613 	.pru_abort = sctp_abort,
4614 	.pru_accept = sctp_accept,
4615 	.pru_attach = sctp_attach,
4616 	.pru_bind = sctp_bind,
4617 	.pru_connect = sctp_connect,
4618 	.pru_control = in_control,
4619 	.pru_close = sctp_close,
4620 	.pru_detach = sctp_close,
4621 	.pru_sopoll = sopoll_generic,
4622 	.pru_flush = sctp_flush,
4623 	.pru_disconnect = sctp_disconnect,
4624 	.pru_listen = sctp_listen,
4625 	.pru_peeraddr = sctp_peeraddr,
4626 	.pru_send = sctp_sendm,
4627 	.pru_shutdown = sctp_shutdown,
4628 	.pru_sockaddr = sctp_ingetaddr,
4629 	.pru_sosend = sctp_sosend,
4630 	.pru_soreceive = sctp_soreceive
4631 };
4632