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