sctp_usrreq.c revision 171440
1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3163953Srrs *
4163953Srrs * Redistribution and use in source and binary forms, with or without
5163953Srrs * modification, are permitted provided that the following conditions are met:
6163953Srrs *
7163953Srrs * a) Redistributions of source code must retain the above copyright notice,
8163953Srrs *   this list of conditions and the following disclaimer.
9163953Srrs *
10163953Srrs * b) Redistributions in binary form must reproduce the above copyright
11163953Srrs *    notice, this list of conditions and the following disclaimer in
12163953Srrs *   the documentation and/or other materials provided with the distribution.
13163953Srrs *
14163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
15163953Srrs *    contributors may be used to endorse or promote products derived
16163953Srrs *    from this software without specific prior written permission.
17163953Srrs *
18163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
29163953Srrs */
30163953Srrs
31163953Srrs/* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $	 */
32163953Srrs
33163953Srrs#include <sys/cdefs.h>
34163953Srrs__FBSDID("$FreeBSD: head/sys/netinet/sctp_usrreq.c 171440 2007-07-14 09:36:28Z rrs $");
35166086Srrs#include <netinet/sctp_os.h>
36163953Srrs#include <sys/proc.h>
37163953Srrs#include <netinet/sctp_pcb.h>
38163953Srrs#include <netinet/sctp_header.h>
39163953Srrs#include <netinet/sctp_var.h>
40167695Srrs#if defined(INET6)
41167695Srrs#include <netinet6/sctp6_var.h>
42167695Srrs#endif
43167598Srrs#include <netinet/sctp_sysctl.h>
44163953Srrs#include <netinet/sctp_output.h>
45163953Srrs#include <netinet/sctp_uio.h>
46163953Srrs#include <netinet/sctp_asconf.h>
47163953Srrs#include <netinet/sctputil.h>
48163953Srrs#include <netinet/sctp_indata.h>
49163953Srrs#include <netinet/sctp_timer.h>
50163953Srrs#include <netinet/sctp_auth.h>
51170091Srrs#include <netinet/sctp_bsd_addr.h>
52171440Srrs#include <netinet/sctp_cc_functions.h>
53164085Srrs
54163953Srrs
55163953Srrs
56170091Srrs
57163953Srrsvoid
58163953Srrssctp_init(void)
59163953Srrs{
60163953Srrs	/* Init the SCTP pcb in sctp_pcb.c */
61163953Srrs	u_long sb_max_adj;
62163953Srrs
63163953Srrs	sctp_pcb_init();
64163953Srrs
65171440Srrs
66163953Srrs	if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
67163953Srrs		sctp_max_chunks_on_queue = (nmbclusters / 8);
68163953Srrs	/*
69163953Srrs	 * Allow a user to take no more than 1/2 the number of clusters or
70163953Srrs	 * the SB_MAX whichever is smaller for the send window.
71163953Srrs	 */
72163953Srrs	sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
73163953Srrs	sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
74170056Srrs	    (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
75163953Srrs	/*
76163953Srrs	 * Now for the recv window, should we take the same amount? or
77163953Srrs	 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
78163953Srrs	 * now I will just copy.
79163953Srrs	 */
80163953Srrs	sctp_recvspace = sctp_sendspace;
81163953Srrs
82163953Srrs}
83163953Srrs
84163953Srrs
85166023Srrs
86166023Srrs/*
87166023Srrs * cleanup of the sctppcbinfo structure.
88166023Srrs * Assumes that the sctppcbinfo lock is held.
89166023Srrs */
90166023Srrsvoid
91166023Srrssctp_pcbinfo_cleanup(void)
92166023Srrs{
93166023Srrs	/* free the hash tables */
94166023Srrs	if (sctppcbinfo.sctp_asochash != NULL)
95166023Srrs		SCTP_HASH_FREE(sctppcbinfo.sctp_asochash, sctppcbinfo.hashasocmark);
96166023Srrs	if (sctppcbinfo.sctp_ephash != NULL)
97166023Srrs		SCTP_HASH_FREE(sctppcbinfo.sctp_ephash, sctppcbinfo.hashmark);
98166023Srrs	if (sctppcbinfo.sctp_tcpephash != NULL)
99166023Srrs		SCTP_HASH_FREE(sctppcbinfo.sctp_tcpephash, sctppcbinfo.hashtcpmark);
100166023Srrs	if (sctppcbinfo.sctp_restarthash != NULL)
101166023Srrs		SCTP_HASH_FREE(sctppcbinfo.sctp_restarthash, sctppcbinfo.hashrestartmark);
102166023Srrs}
103166023Srrs
104163953Srrs
105163953Srrsstatic void
106167695Srrssctp_pathmtu_adjustment(struct sctp_inpcb *inp,
107163953Srrs    struct sctp_tcb *stcb,
108163953Srrs    struct sctp_nets *net,
109163953Srrs    uint16_t nxtsz)
110163953Srrs{
111163953Srrs	struct sctp_tmit_chunk *chk;
112163953Srrs
113163953Srrs	/* Adjust that too */
114163953Srrs	stcb->asoc.smallest_mtu = nxtsz;
115163953Srrs	/* now off to subtract IP_DF flag if needed */
116169352Srrs#ifdef SCTP_PRINT_FOR_B_AND_M
117169420Srrs	SCTP_PRINTF("sctp_pathmtu_adjust called inp:%p stcb:%p net:%p nxtsz:%d\n",
118169352Srrs	    inp, stcb, net, nxtsz);
119169352Srrs#endif
120163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
121163953Srrs		if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
122163953Srrs			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
123163953Srrs		}
124163953Srrs	}
125163953Srrs	TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
126163953Srrs		if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
127163953Srrs			/*
128163953Srrs			 * For this guy we also mark for immediate resend
129163953Srrs			 * since we sent to big of chunk
130163953Srrs			 */
131163953Srrs			chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
132163953Srrs			if (chk->sent != SCTP_DATAGRAM_RESEND) {
133163953Srrs				sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
134163953Srrs			}
135163953Srrs			chk->sent = SCTP_DATAGRAM_RESEND;
136163953Srrs			chk->rec.data.doing_fast_retransmit = 0;
137170744Srrs			if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
138170744Srrs				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
139170744Srrs				    chk->whoTo->flight_size,
140170744Srrs				    chk->book_size,
141170744Srrs				    (uintptr_t) chk->whoTo,
142170744Srrs				    chk->rec.data.TSN_seq);
143170744Srrs			}
144163953Srrs			/* Clear any time so NO RTT is being done */
145163953Srrs			chk->do_rtt = 0;
146168709Srrs			sctp_flight_size_decrease(chk);
147168709Srrs			sctp_total_flight_decrease(stcb, chk);
148163953Srrs		}
149163953Srrs	}
150163953Srrs}
151163953Srrs
152163953Srrsstatic void
153163953Srrssctp_notify_mbuf(struct sctp_inpcb *inp,
154163953Srrs    struct sctp_tcb *stcb,
155163953Srrs    struct sctp_nets *net,
156163953Srrs    struct ip *ip,
157163953Srrs    struct sctphdr *sh)
158163953Srrs{
159163953Srrs	struct icmp *icmph;
160163953Srrs	int totsz, tmr_stopped = 0;
161163953Srrs	uint16_t nxtsz;
162163953Srrs
163163953Srrs	/* protection */
164163953Srrs	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
165163953Srrs	    (ip == NULL) || (sh == NULL)) {
166169420Srrs		if (stcb != NULL) {
167163953Srrs			SCTP_TCB_UNLOCK(stcb);
168169420Srrs		}
169163953Srrs		return;
170163953Srrs	}
171163953Srrs	/* First job is to verify the vtag matches what I would send */
172163953Srrs	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
173163953Srrs		SCTP_TCB_UNLOCK(stcb);
174163953Srrs		return;
175163953Srrs	}
176163953Srrs	icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
177163953Srrs	    sizeof(struct ip)));
178163953Srrs	if (icmph->icmp_type != ICMP_UNREACH) {
179163953Srrs		/* We only care about unreachable */
180163953Srrs		SCTP_TCB_UNLOCK(stcb);
181163953Srrs		return;
182163953Srrs	}
183163953Srrs	if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
184163953Srrs		/* not a unreachable message due to frag. */
185163953Srrs		SCTP_TCB_UNLOCK(stcb);
186163953Srrs		return;
187163953Srrs	}
188163953Srrs	totsz = ip->ip_len;
189163953Srrs
190163953Srrs	nxtsz = ntohs(icmph->icmp_seq);
191163953Srrs	if (nxtsz == 0) {
192163953Srrs		/*
193163953Srrs		 * old type router that does not tell us what the next size
194163953Srrs		 * mtu is. Rats we will have to guess (in a educated fashion
195163953Srrs		 * of course)
196163953Srrs		 */
197163953Srrs		nxtsz = find_next_best_mtu(totsz);
198163953Srrs	}
199163953Srrs	/* Stop any PMTU timer */
200165647Srrs	if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
201163953Srrs		tmr_stopped = 1;
202165220Srrs		sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
203165220Srrs		    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
204163953Srrs	}
205163953Srrs	/* Adjust destination size limit */
206163953Srrs	if (net->mtu > nxtsz) {
207163953Srrs		net->mtu = nxtsz;
208163953Srrs	}
209163953Srrs	/* now what about the ep? */
210163953Srrs	if (stcb->asoc.smallest_mtu > nxtsz) {
211169352Srrs#ifdef SCTP_PRINT_FOR_B_AND_M
212169420Srrs		SCTP_PRINTF("notify_mbuf (ICMP) calls sctp_pathmtu_adjust mtu:%d\n",
213169352Srrs		    nxtsz);
214169352Srrs#endif
215167695Srrs		sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
216163953Srrs	}
217163953Srrs	if (tmr_stopped)
218163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
219163953Srrs
220163953Srrs	SCTP_TCB_UNLOCK(stcb);
221163953Srrs}
222163953Srrs
223163953Srrs
224163953Srrsvoid
225163953Srrssctp_notify(struct sctp_inpcb *inp,
226167695Srrs    int error,
227163953Srrs    struct sctphdr *sh,
228163953Srrs    struct sockaddr *to,
229163953Srrs    struct sctp_tcb *stcb,
230163953Srrs    struct sctp_nets *net)
231163953Srrs{
232163953Srrs	/* protection */
233163953Srrs	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
234163953Srrs	    (sh == NULL) || (to == NULL)) {
235163953Srrs		return;
236163953Srrs	}
237163953Srrs	/* First job is to verify the vtag matches what I would send */
238163953Srrs	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
239163953Srrs		return;
240163953Srrs	}
241163953Srrs	/* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */
242163953Srrs
243167695Srrs	if ((error == EHOSTUNREACH) ||	/* Host is not reachable */
244167695Srrs	    (error == EHOSTDOWN) ||	/* Host is down */
245167695Srrs	    (error == ECONNREFUSED) ||	/* Host refused the connection, (not
246163953Srrs					 * an abort?) */
247167695Srrs	    (error == ENOPROTOOPT)	/* SCTP is not present on host */
248163953Srrs	    ) {
249163953Srrs		/*
250163953Srrs		 * Hmm reachablity problems we must examine closely. If its
251163953Srrs		 * not reachable, we may have lost a network. Or if there is
252163953Srrs		 * NO protocol at the other end named SCTP. well we consider
253163953Srrs		 * it a OOTB abort.
254163953Srrs		 */
255167695Srrs		if ((error == EHOSTUNREACH) || (error == EHOSTDOWN)) {
256163953Srrs			if (net->dest_state & SCTP_ADDR_REACHABLE) {
257163953Srrs				/* Ok that destination is NOT reachable */
258169420Srrs				SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
259167598Srrs				    net->error_count,
260167598Srrs				    net->failure_threshold,
261167598Srrs				    net);
262167598Srrs
263163953Srrs				net->dest_state &= ~SCTP_ADDR_REACHABLE;
264163953Srrs				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
265171440Srrs				/*
266171440Srrs				 * JRS 5/14/07 - If a destination is
267171440Srrs				 * unreachable, the PF bit is turned off.
268171440Srrs				 * This allows an unambiguous use of the PF
269171440Srrs				 * bit for destinations that are reachable
270171440Srrs				 * but potentially failed. If the
271171440Srrs				 * destination is set to the unreachable
272171440Srrs				 * state, also set the destination to the PF
273171440Srrs				 * state.
274171440Srrs				 */
275171440Srrs				/*
276171440Srrs				 * Add debug message here if destination is
277171440Srrs				 * not in PF state.
278171440Srrs				 */
279171440Srrs				/* Stop any running T3 timers here? */
280171440Srrs				if (sctp_cmt_pf) {
281171440Srrs					net->dest_state &= ~SCTP_ADDR_PF;
282171440Srrs					SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
283171440Srrs					    net);
284171440Srrs				}
285163953Srrs				net->error_count = net->failure_threshold + 1;
286163953Srrs				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
287163953Srrs				    stcb, SCTP_FAILED_THRESHOLD,
288163953Srrs				    (void *)net);
289163953Srrs			}
290169420Srrs			if (stcb) {
291163953Srrs				SCTP_TCB_UNLOCK(stcb);
292169420Srrs			}
293163953Srrs		} else {
294163953Srrs			/*
295163953Srrs			 * Here the peer is either playing tricks on us,
296163953Srrs			 * including an address that belongs to someone who
297163953Srrs			 * does not support SCTP OR was a userland
298163953Srrs			 * implementation that shutdown and now is dead. In
299163953Srrs			 * either case treat it like a OOTB abort with no
300163953Srrs			 * TCB
301163953Srrs			 */
302163953Srrs			sctp_abort_notification(stcb, SCTP_PEER_FAULTY);
303165220Srrs			sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
304163953Srrs			/* no need to unlock here, since the TCB is gone */
305163953Srrs		}
306163953Srrs	} else {
307163953Srrs		/* Send all others to the app */
308169420Srrs		if (stcb) {
309163953Srrs			SCTP_TCB_UNLOCK(stcb);
310169420Srrs		}
311163953Srrs		if (inp->sctp_socket) {
312163953Srrs#ifdef SCTP_LOCK_LOGGING
313170744Srrs			if (sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE) {
314170744Srrs				sctp_log_lock(inp, stcb, SCTP_LOG_LOCK_SOCK);
315170744Srrs			}
316163953Srrs#endif
317163953Srrs			SOCK_LOCK(inp->sctp_socket);
318167695Srrs			inp->sctp_socket->so_error = error;
319163953Srrs			sctp_sowwakeup(inp, inp->sctp_socket);
320163953Srrs			SOCK_UNLOCK(inp->sctp_socket);
321163953Srrs		}
322163953Srrs	}
323163953Srrs}
324163953Srrs
325163953Srrsvoid
326163953Srrssctp_ctlinput(cmd, sa, vip)
327163953Srrs	int cmd;
328163953Srrs	struct sockaddr *sa;
329163953Srrs	void *vip;
330163953Srrs{
331163953Srrs	struct ip *ip = vip;
332163953Srrs	struct sctphdr *sh;
333167598Srrs	uint32_t vrf_id;
334163953Srrs
335168299Srrs	/* FIX, for non-bsd is this right? */
336167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
337163953Srrs	if (sa->sa_family != AF_INET ||
338163953Srrs	    ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
339163953Srrs		return;
340163953Srrs	}
341163953Srrs	if (PRC_IS_REDIRECT(cmd)) {
342163953Srrs		ip = 0;
343163953Srrs	} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
344163953Srrs		return;
345163953Srrs	}
346163953Srrs	if (ip) {
347163953Srrs		struct sctp_inpcb *inp = NULL;
348163953Srrs		struct sctp_tcb *stcb = NULL;
349163953Srrs		struct sctp_nets *net = NULL;
350163953Srrs		struct sockaddr_in to, from;
351163953Srrs
352163953Srrs		sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
353163953Srrs		bzero(&to, sizeof(to));
354163953Srrs		bzero(&from, sizeof(from));
355163953Srrs		from.sin_family = to.sin_family = AF_INET;
356163953Srrs		from.sin_len = to.sin_len = sizeof(to);
357163953Srrs		from.sin_port = sh->src_port;
358163953Srrs		from.sin_addr = ip->ip_src;
359163953Srrs		to.sin_port = sh->dest_port;
360163953Srrs		to.sin_addr = ip->ip_dst;
361163953Srrs
362163953Srrs		/*
363163953Srrs		 * 'to' holds the dest of the packet that failed to be sent.
364163953Srrs		 * 'from' holds our local endpoint address. Thus we reverse
365163953Srrs		 * the to and the from in the lookup.
366163953Srrs		 */
367163953Srrs		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
368163953Srrs		    (struct sockaddr *)&to,
369167598Srrs		    &inp, &net, 1, vrf_id);
370163953Srrs		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
371163953Srrs			if (cmd != PRC_MSGSIZE) {
372163953Srrs				int cm;
373163953Srrs
374163953Srrs				if (cmd == PRC_HOSTDEAD) {
375163953Srrs					cm = EHOSTUNREACH;
376163953Srrs				} else {
377163953Srrs					cm = inetctlerrmap[cmd];
378163953Srrs				}
379163953Srrs				sctp_notify(inp, cm, sh,
380163953Srrs				    (struct sockaddr *)&to, stcb,
381163953Srrs				    net);
382163953Srrs			} else {
383163953Srrs				/* handle possible ICMP size messages */
384163953Srrs				sctp_notify_mbuf(inp, stcb, net, ip, sh);
385163953Srrs			}
386163953Srrs		} else {
387163953Srrs			if ((stcb == NULL) && (inp != NULL)) {
388163953Srrs				/* reduce ref-count */
389163953Srrs				SCTP_INP_WLOCK(inp);
390163953Srrs				SCTP_INP_DECR_REF(inp);
391163953Srrs				SCTP_INP_WUNLOCK(inp);
392163953Srrs			}
393163953Srrs		}
394163953Srrs	}
395163953Srrs	return;
396163953Srrs}
397163953Srrs
398163953Srrsstatic int
399163953Srrssctp_getcred(SYSCTL_HANDLER_ARGS)
400163953Srrs{
401164085Srrs	struct xucred xuc;
402163953Srrs	struct sockaddr_in addrs[2];
403163953Srrs	struct sctp_inpcb *inp;
404163953Srrs	struct sctp_nets *net;
405163953Srrs	struct sctp_tcb *stcb;
406164085Srrs	int error;
407167598Srrs	uint32_t vrf_id;
408163953Srrs
409168299Srrs	/* FIX, for non-bsd is this right? */
410167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
411168299Srrs
412170587Srwatson	error = priv_check(req->td, PRIV_NETINET_GETCRED);
413170587Srwatson
414163953Srrs	if (error)
415163953Srrs		return (error);
416164039Srwatson
417163953Srrs	error = SYSCTL_IN(req, addrs, sizeof(addrs));
418163953Srrs	if (error)
419163953Srrs		return (error);
420163953Srrs
421163953Srrs	stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
422163953Srrs	    sintosa(&addrs[1]),
423167598Srrs	    &inp, &net, 1, vrf_id);
424163953Srrs	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
425163953Srrs		if ((inp != NULL) && (stcb == NULL)) {
426163953Srrs			/* reduce ref-count */
427163953Srrs			SCTP_INP_WLOCK(inp);
428163953Srrs			SCTP_INP_DECR_REF(inp);
429164085Srrs			goto cred_can_cont;
430163953Srrs		}
431163953Srrs		error = ENOENT;
432163953Srrs		goto out;
433163953Srrs	}
434163953Srrs	SCTP_TCB_UNLOCK(stcb);
435164085Srrs	/*
436164085Srrs	 * We use the write lock here, only since in the error leg we need
437164085Srrs	 * it. If we used RLOCK, then we would have to
438164085Srrs	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
439164085Srrs	 * Better to use higher wlock.
440164085Srrs	 */
441164085Srrs	SCTP_INP_WLOCK(inp);
442164085Srrscred_can_cont:
443164085Srrs	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
444164085Srrs	if (error) {
445164085Srrs		SCTP_INP_WUNLOCK(inp);
446164085Srrs		goto out;
447164085Srrs	}
448164085Srrs	cru2x(inp->sctp_socket->so_cred, &xuc);
449164085Srrs	SCTP_INP_WUNLOCK(inp);
450164085Srrs	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
451163953Srrsout:
452163953Srrs	return (error);
453163953Srrs}
454163953Srrs
455163953SrrsSYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
456163953Srrs    0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
457163953Srrs
458163953Srrs
459163953Srrsstatic void
460163953Srrssctp_abort(struct socket *so)
461163953Srrs{
462163953Srrs	struct sctp_inpcb *inp;
463163953Srrs	uint32_t flags;
464163953Srrs
465163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
466163953Srrs	if (inp == 0)
467163953Srrs		return;
468163953Srrs
469163953Srrssctp_must_try_again:
470163953Srrs	flags = inp->sctp_flags;
471163953Srrs#ifdef SCTP_LOG_CLOSING
472163953Srrs	sctp_log_closing(inp, NULL, 17);
473163953Srrs#endif
474163953Srrs	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
475163953Srrs	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
476163953Srrs#ifdef SCTP_LOG_CLOSING
477163953Srrs		sctp_log_closing(inp, NULL, 16);
478163953Srrs#endif
479169380Srrs		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
480169380Srrs		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
481163953Srrs		SOCK_LOCK(so);
482167695Srrs		SCTP_SB_CLEAR(so->so_snd);
483163953Srrs		/*
484163953Srrs		 * same for the rcv ones, they are only here for the
485163953Srrs		 * accounting/select.
486163953Srrs		 */
487167695Srrs		SCTP_SB_CLEAR(so->so_rcv);
488167695Srrs
489167695Srrs		/* Now null out the reference, we are completely detached. */
490163953Srrs		so->so_pcb = NULL;
491163953Srrs		SOCK_UNLOCK(so);
492163953Srrs	} else {
493163953Srrs		flags = inp->sctp_flags;
494163953Srrs		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
495163953Srrs			goto sctp_must_try_again;
496163953Srrs		}
497163953Srrs	}
498163953Srrs	return;
499163953Srrs}
500163953Srrs
501163953Srrsstatic int
502163953Srrssctp_attach(struct socket *so, int proto, struct thread *p)
503163953Srrs{
504163953Srrs	struct sctp_inpcb *inp;
505163953Srrs	struct inpcb *ip_inp;
506166086Srrs	int error;
507170205Srrs	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
508163953Srrs
509171167Sgnn#ifdef IPSEC
510163953Srrs	uint32_t flags;
511171440Srrs
512163953Srrs#endif
513163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
514163953Srrs	if (inp != 0) {
515163953Srrs		return EINVAL;
516163953Srrs	}
517167695Srrs	error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
518163953Srrs	if (error) {
519163953Srrs		return error;
520163953Srrs	}
521170205Srrs	error = sctp_inpcb_alloc(so, vrf_id);
522163953Srrs	if (error) {
523163953Srrs		return error;
524163953Srrs	}
525163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
526163953Srrs	SCTP_INP_WLOCK(inp);
527163953Srrs
528163953Srrs	inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;	/* I'm not v6! */
529163953Srrs	ip_inp = &inp->ip_inp.inp;
530163953Srrs	ip_inp->inp_vflag |= INP_IPV4;
531163953Srrs	ip_inp->inp_ip_ttl = ip_defttl;
532163953Srrs
533171167Sgnn#ifdef IPSEC
534171133Sgnn	error = ipsec_init_policy(so, &ip_inp->inp_sp);
535163953Srrs#ifdef SCTP_LOG_CLOSING
536163953Srrs	sctp_log_closing(inp, NULL, 17);
537163953Srrs#endif
538163953Srrs	if (error != 0) {
539163953Srrs		flags = inp->sctp_flags;
540163953Srrs		if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
541163953Srrs		    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
542163953Srrs#ifdef SCTP_LOG_CLOSING
543163953Srrs			sctp_log_closing(inp, NULL, 15);
544163953Srrs#endif
545169352Srrs			SCTP_INP_WUNLOCK(inp);
546169380Srrs			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
547169380Srrs			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
548169254Srrs		} else {
549169352Srrs			SCTP_INP_WUNLOCK(inp);
550163953Srrs		}
551163953Srrs		return error;
552163953Srrs	}
553171167Sgnn#endif				/* IPSEC */
554163953Srrs	SCTP_INP_WUNLOCK(inp);
555163953Srrs	return 0;
556163953Srrs}
557163953Srrs
558163953Srrsstatic int
559163953Srrssctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
560163953Srrs{
561163953Srrs	struct sctp_inpcb *inp;
562166086Srrs	int error;
563163953Srrs
564163953Srrs#ifdef INET6
565163953Srrs	if (addr && addr->sa_family != AF_INET)
566163953Srrs		/* must be a v4 address! */
567163953Srrs		return EINVAL;
568163953Srrs#endif				/* INET6 */
569170056Srrs	if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
570170056Srrs		return EINVAL;
571170056Srrs	}
572163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
573163953Srrs	if (inp == 0)
574163953Srrs		return EINVAL;
575163953Srrs
576170744Srrs	error = sctp_inpcb_bind(so, addr, NULL, p);
577163953Srrs	return error;
578163953Srrs}
579163953Srrs
580163953Srrsstatic void
581163953Srrssctp_close(struct socket *so)
582163953Srrs{
583163953Srrs	struct sctp_inpcb *inp;
584163953Srrs	uint32_t flags;
585163953Srrs
586163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
587163953Srrs	if (inp == 0)
588163953Srrs		return;
589163953Srrs
590163953Srrs	/*
591163953Srrs	 * Inform all the lower layer assoc that we are done.
592163953Srrs	 */
593163953Srrssctp_must_try_again:
594163953Srrs	flags = inp->sctp_flags;
595163953Srrs#ifdef SCTP_LOG_CLOSING
596163953Srrs	sctp_log_closing(inp, NULL, 17);
597163953Srrs#endif
598163953Srrs	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
599163953Srrs	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
600163953Srrs		if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
601163953Srrs		    (so->so_rcv.sb_cc > 0)) {
602163953Srrs#ifdef SCTP_LOG_CLOSING
603163953Srrs			sctp_log_closing(inp, NULL, 13);
604163953Srrs#endif
605169380Srrs			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
606169380Srrs			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
607163953Srrs		} else {
608163953Srrs#ifdef SCTP_LOG_CLOSING
609163953Srrs			sctp_log_closing(inp, NULL, 14);
610163953Srrs#endif
611169380Srrs			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
612169380Srrs			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
613163953Srrs		}
614163953Srrs		/*
615163953Srrs		 * The socket is now detached, no matter what the state of
616163953Srrs		 * the SCTP association.
617163953Srrs		 */
618163953Srrs		SOCK_LOCK(so);
619167695Srrs		SCTP_SB_CLEAR(so->so_snd);
620163953Srrs		/*
621163953Srrs		 * same for the rcv ones, they are only here for the
622163953Srrs		 * accounting/select.
623163953Srrs		 */
624167695Srrs		SCTP_SB_CLEAR(so->so_rcv);
625167695Srrs
626167695Srrs		/* Now null out the reference, we are completely detached. */
627163953Srrs		so->so_pcb = NULL;
628163953Srrs		SOCK_UNLOCK(so);
629163953Srrs	} else {
630163953Srrs		flags = inp->sctp_flags;
631163953Srrs		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
632163953Srrs			goto sctp_must_try_again;
633163953Srrs		}
634163953Srrs	}
635163953Srrs	return;
636163953Srrs}
637163953Srrs
638163953Srrs
639163953Srrsint
640163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
641163953Srrs    struct mbuf *control, struct thread *p);
642163953Srrs
643163953Srrs
644163953Srrsint
645163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
646163953Srrs    struct mbuf *control, struct thread *p)
647163953Srrs{
648163953Srrs	struct sctp_inpcb *inp;
649163953Srrs	int error;
650163953Srrs
651163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
652163953Srrs	if (inp == 0) {
653163953Srrs		if (control) {
654163953Srrs			sctp_m_freem(control);
655163953Srrs			control = NULL;
656163953Srrs		}
657163953Srrs		sctp_m_freem(m);
658163953Srrs		return EINVAL;
659163953Srrs	}
660163953Srrs	/* Got to have an to address if we are NOT a connected socket */
661163953Srrs	if ((addr == NULL) &&
662163953Srrs	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
663163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
664163953Srrs	    ) {
665163953Srrs		goto connected_type;
666163953Srrs	} else if (addr == NULL) {
667163953Srrs		error = EDESTADDRREQ;
668163953Srrs		sctp_m_freem(m);
669163953Srrs		if (control) {
670163953Srrs			sctp_m_freem(control);
671163953Srrs			control = NULL;
672163953Srrs		}
673163953Srrs		return (error);
674163953Srrs	}
675163953Srrs#ifdef INET6
676163953Srrs	if (addr->sa_family != AF_INET) {
677163953Srrs		/* must be a v4 address! */
678163953Srrs		sctp_m_freem(m);
679163953Srrs		if (control) {
680163953Srrs			sctp_m_freem(control);
681163953Srrs			control = NULL;
682163953Srrs		}
683163953Srrs		error = EDESTADDRREQ;
684163953Srrs		return EINVAL;
685163953Srrs	}
686163953Srrs#endif				/* INET6 */
687163953Srrsconnected_type:
688163953Srrs	/* now what about control */
689163953Srrs	if (control) {
690163953Srrs		if (inp->control) {
691169420Srrs			SCTP_PRINTF("huh? control set?\n");
692163953Srrs			sctp_m_freem(inp->control);
693163953Srrs			inp->control = NULL;
694163953Srrs		}
695163953Srrs		inp->control = control;
696163953Srrs	}
697163953Srrs	/* Place the data */
698163953Srrs	if (inp->pkt) {
699165647Srrs		SCTP_BUF_NEXT(inp->pkt_last) = m;
700163953Srrs		inp->pkt_last = m;
701163953Srrs	} else {
702163953Srrs		inp->pkt_last = inp->pkt = m;
703163953Srrs	}
704163953Srrs	if (
705163953Srrs	/* FreeBSD uses a flag passed */
706163953Srrs	    ((flags & PRUS_MORETOCOME) == 0)
707163953Srrs	    ) {
708163953Srrs		/*
709163953Srrs		 * note with the current version this code will only be used
710163953Srrs		 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
711163953Srrs		 * re-defining sosend to use the sctp_sosend. One can
712163953Srrs		 * optionally switch back to this code (by changing back the
713163953Srrs		 * definitions) but this is not advisable. This code is used
714163953Srrs		 * by FreeBSD when sending a file with sendfile() though.
715163953Srrs		 */
716163953Srrs		int ret;
717163953Srrs
718163953Srrs		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
719163953Srrs		inp->pkt = NULL;
720163953Srrs		inp->control = NULL;
721163953Srrs		return (ret);
722163953Srrs	} else {
723163953Srrs		return (0);
724163953Srrs	}
725163953Srrs}
726163953Srrs
727163953Srrsstatic int
728163953Srrssctp_disconnect(struct socket *so)
729163953Srrs{
730163953Srrs	struct sctp_inpcb *inp;
731163953Srrs
732163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
733163953Srrs	if (inp == NULL) {
734163953Srrs		return (ENOTCONN);
735163953Srrs	}
736163953Srrs	SCTP_INP_RLOCK(inp);
737163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
738166675Srrs		if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
739163953Srrs			/* No connection */
740163953Srrs			SCTP_INP_RUNLOCK(inp);
741163953Srrs			return (0);
742163953Srrs		} else {
743163953Srrs			struct sctp_association *asoc;
744163953Srrs			struct sctp_tcb *stcb;
745163953Srrs
746163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
747163953Srrs			if (stcb == NULL) {
748163953Srrs				SCTP_INP_RUNLOCK(inp);
749163953Srrs				return (EINVAL);
750163953Srrs			}
751163953Srrs			SCTP_TCB_LOCK(stcb);
752163953Srrs			asoc = &stcb->asoc;
753163953Srrs			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
754163953Srrs				/* We are about to be freed, out of here */
755163953Srrs				SCTP_TCB_UNLOCK(stcb);
756163953Srrs				SCTP_INP_RUNLOCK(inp);
757163953Srrs				return (0);
758163953Srrs			}
759163953Srrs			if (((so->so_options & SO_LINGER) &&
760163953Srrs			    (so->so_linger == 0)) ||
761163953Srrs			    (so->so_rcv.sb_cc > 0)) {
762163953Srrs				if (SCTP_GET_STATE(asoc) !=
763163953Srrs				    SCTP_STATE_COOKIE_WAIT) {
764163953Srrs					/* Left with Data unread */
765163953Srrs					struct mbuf *err;
766163953Srrs
767163953Srrs					err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
768163953Srrs					if (err) {
769163953Srrs						/*
770163953Srrs						 * Fill in the user
771163953Srrs						 * initiated abort
772163953Srrs						 */
773163953Srrs						struct sctp_paramhdr *ph;
774163953Srrs
775163953Srrs						ph = mtod(err, struct sctp_paramhdr *);
776165647Srrs						SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
777163953Srrs						ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
778165647Srrs						ph->param_length = htons(SCTP_BUF_LEN(err));
779163953Srrs					}
780163953Srrs					sctp_send_abort_tcb(stcb, err);
781163953Srrs					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
782163953Srrs				}
783163953Srrs				SCTP_INP_RUNLOCK(inp);
784163953Srrs				if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
785163953Srrs				    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
786163953Srrs					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
787163953Srrs				}
788165220Srrs				sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
789163953Srrs				/* No unlock tcb assoc is gone */
790163953Srrs				return (0);
791163953Srrs			}
792163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
793163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue) &&
794163953Srrs			    (asoc->stream_queue_cnt == 0)) {
795163953Srrs				/* there is nothing queued to send, so done */
796163953Srrs				if (asoc->locked_on_sending) {
797163953Srrs					goto abort_anyway;
798163953Srrs				}
799166675Srrs				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
800166675Srrs				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
801163953Srrs					/* only send SHUTDOWN 1st time thru */
802163953Srrs					sctp_stop_timers_for_shutdown(stcb);
803163953Srrs					sctp_send_shutdown(stcb,
804163953Srrs					    stcb->asoc.primary_destination);
805163953Srrs					sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
806166675Srrs					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
807166675Srrs					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
808166675Srrs						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
809166675Srrs					}
810163953Srrs					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
811163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
812163953Srrs					    stcb->sctp_ep, stcb,
813163953Srrs					    asoc->primary_destination);
814163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
815163953Srrs					    stcb->sctp_ep, stcb,
816163953Srrs					    asoc->primary_destination);
817163953Srrs				}
818163953Srrs			} else {
819163953Srrs				/*
820163953Srrs				 * we still got (or just got) data to send,
821163953Srrs				 * so set SHUTDOWN_PENDING
822163953Srrs				 */
823163953Srrs				/*
824163953Srrs				 * XXX sockets draft says that SCTP_EOF
825163953Srrs				 * should be sent with no data. currently,
826163953Srrs				 * we will allow user data to be sent first
827163953Srrs				 * and move to SHUTDOWN-PENDING
828163953Srrs				 */
829163953Srrs				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
830163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
831163953Srrs				    asoc->primary_destination);
832163953Srrs				if (asoc->locked_on_sending) {
833163953Srrs					/* Locked to send out the data */
834163953Srrs					struct sctp_stream_queue_pending *sp;
835163953Srrs
836163953Srrs					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
837163953Srrs					if (sp == NULL) {
838169420Srrs						SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
839163953Srrs						    asoc->locked_on_sending->stream_no);
840163953Srrs					} else {
841163953Srrs						if ((sp->length == 0) && (sp->msg_is_complete == 0))
842163953Srrs							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
843163953Srrs					}
844163953Srrs				}
845163953Srrs				if (TAILQ_EMPTY(&asoc->send_queue) &&
846163953Srrs				    TAILQ_EMPTY(&asoc->sent_queue) &&
847163953Srrs				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
848163953Srrs					struct mbuf *op_err;
849163953Srrs
850163953Srrs			abort_anyway:
851163953Srrs					op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
852163953Srrs					    0, M_DONTWAIT, 1, MT_DATA);
853163953Srrs					if (op_err) {
854163953Srrs						/*
855163953Srrs						 * Fill in the user
856163953Srrs						 * initiated abort
857163953Srrs						 */
858163953Srrs						struct sctp_paramhdr *ph;
859163953Srrs						uint32_t *ippp;
860163953Srrs
861165647Srrs						SCTP_BUF_LEN(op_err) =
862163953Srrs						    (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
863163953Srrs						ph = mtod(op_err,
864163953Srrs						    struct sctp_paramhdr *);
865163953Srrs						ph->param_type = htons(
866163953Srrs						    SCTP_CAUSE_USER_INITIATED_ABT);
867165647Srrs						ph->param_length = htons(SCTP_BUF_LEN(op_err));
868163953Srrs						ippp = (uint32_t *) (ph + 1);
869165220Srrs						*ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
870163953Srrs					}
871165220Srrs					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
872163953Srrs					sctp_send_abort_tcb(stcb, op_err);
873163953Srrs					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
874163953Srrs					if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
875163953Srrs					    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
876163953Srrs						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
877163953Srrs					}
878163953Srrs					SCTP_INP_RUNLOCK(inp);
879165220Srrs					sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
880163953Srrs					return (0);
881163953Srrs				}
882163953Srrs			}
883163953Srrs			SCTP_TCB_UNLOCK(stcb);
884163953Srrs			SCTP_INP_RUNLOCK(inp);
885163953Srrs			return (0);
886163953Srrs		}
887163953Srrs		/* not reached */
888163953Srrs	} else {
889163953Srrs		/* UDP model does not support this */
890163953Srrs		SCTP_INP_RUNLOCK(inp);
891163953Srrs		return EOPNOTSUPP;
892163953Srrs	}
893163953Srrs}
894163953Srrs
895163953Srrsint
896163953Srrssctp_shutdown(struct socket *so)
897163953Srrs{
898163953Srrs	struct sctp_inpcb *inp;
899163953Srrs
900163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
901163953Srrs	if (inp == 0) {
902163953Srrs		return EINVAL;
903163953Srrs	}
904163953Srrs	SCTP_INP_RLOCK(inp);
905163953Srrs	/* For UDP model this is a invalid call */
906163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
907163953Srrs		/* Restore the flags that the soshutdown took away. */
908163953Srrs		so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
909163953Srrs		/* This proc will wakeup for read and do nothing (I hope) */
910163953Srrs		SCTP_INP_RUNLOCK(inp);
911163953Srrs		return (EOPNOTSUPP);
912163953Srrs	}
913163953Srrs	/*
914163953Srrs	 * Ok if we reach here its the TCP model and it is either a SHUT_WR
915163953Srrs	 * or SHUT_RDWR. This means we put the shutdown flag against it.
916163953Srrs	 */
917163953Srrs	{
918163953Srrs		struct sctp_tcb *stcb;
919163953Srrs		struct sctp_association *asoc;
920163953Srrs
921163953Srrs		socantsendmore(so);
922163953Srrs
923163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
924163953Srrs		if (stcb == NULL) {
925163953Srrs			/*
926163953Srrs			 * Ok we hit the case that the shutdown call was
927163953Srrs			 * made after an abort or something. Nothing to do
928163953Srrs			 * now.
929163953Srrs			 */
930168299Srrs			SCTP_INP_RUNLOCK(inp);
931163953Srrs			return (0);
932163953Srrs		}
933163953Srrs		SCTP_TCB_LOCK(stcb);
934163953Srrs		asoc = &stcb->asoc;
935163953Srrs		if (TAILQ_EMPTY(&asoc->send_queue) &&
936163953Srrs		    TAILQ_EMPTY(&asoc->sent_queue) &&
937163953Srrs		    (asoc->stream_queue_cnt == 0)) {
938163953Srrs			if (asoc->locked_on_sending) {
939163953Srrs				goto abort_anyway;
940163953Srrs			}
941163953Srrs			/* there is nothing queued to send, so I'm done... */
942163953Srrs			if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
943163953Srrs				/* only send SHUTDOWN the first time through */
944163953Srrs				sctp_stop_timers_for_shutdown(stcb);
945163953Srrs				sctp_send_shutdown(stcb,
946163953Srrs				    stcb->asoc.primary_destination);
947163953Srrs				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
948166675Srrs				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
949166675Srrs				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
950166675Srrs					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
951166675Srrs				}
952163953Srrs				asoc->state = SCTP_STATE_SHUTDOWN_SENT;
953163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
954163953Srrs				    stcb->sctp_ep, stcb,
955163953Srrs				    asoc->primary_destination);
956163953Srrs				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
957163953Srrs				    stcb->sctp_ep, stcb,
958163953Srrs				    asoc->primary_destination);
959163953Srrs			}
960163953Srrs		} else {
961163953Srrs			/*
962163953Srrs			 * we still got (or just got) data to send, so set
963163953Srrs			 * SHUTDOWN_PENDING
964163953Srrs			 */
965163953Srrs			asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
966163953Srrs			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
967163953Srrs			    asoc->primary_destination);
968163953Srrs
969163953Srrs			if (asoc->locked_on_sending) {
970163953Srrs				/* Locked to send out the data */
971163953Srrs				struct sctp_stream_queue_pending *sp;
972163953Srrs
973163953Srrs				sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
974163953Srrs				if (sp == NULL) {
975169420Srrs					SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
976163953Srrs					    asoc->locked_on_sending->stream_no);
977163953Srrs				} else {
978163953Srrs					if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
979163953Srrs						asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
980163953Srrs					}
981163953Srrs				}
982163953Srrs			}
983163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
984163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue) &&
985163953Srrs			    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
986163953Srrs				struct mbuf *op_err;
987163953Srrs
988163953Srrs		abort_anyway:
989163953Srrs				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
990163953Srrs				    0, M_DONTWAIT, 1, MT_DATA);
991163953Srrs				if (op_err) {
992163953Srrs					/* Fill in the user initiated abort */
993163953Srrs					struct sctp_paramhdr *ph;
994163953Srrs					uint32_t *ippp;
995163953Srrs
996165647Srrs					SCTP_BUF_LEN(op_err) =
997163953Srrs					    sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
998163953Srrs					ph = mtod(op_err,
999163953Srrs					    struct sctp_paramhdr *);
1000163953Srrs					ph->param_type = htons(
1001163953Srrs					    SCTP_CAUSE_USER_INITIATED_ABT);
1002165647Srrs					ph->param_length = htons(SCTP_BUF_LEN(op_err));
1003163953Srrs					ippp = (uint32_t *) (ph + 1);
1004165220Srrs					*ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1005163953Srrs				}
1006165220Srrs				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1007163953Srrs				sctp_abort_an_association(stcb->sctp_ep, stcb,
1008163953Srrs				    SCTP_RESPONSE_TO_USER_REQ,
1009163953Srrs				    op_err);
1010163953Srrs				goto skip_unlock;
1011163953Srrs			}
1012163953Srrs		}
1013163953Srrs		SCTP_TCB_UNLOCK(stcb);
1014163953Srrs	}
1015163953Srrsskip_unlock:
1016163953Srrs	SCTP_INP_RUNLOCK(inp);
1017163953Srrs	return 0;
1018163953Srrs}
1019163953Srrs
1020163953Srrs/*
1021163953Srrs * copies a "user" presentable address and removes embedded scope, etc.
1022163953Srrs * returns 0 on success, 1 on error
1023163953Srrs */
1024163953Srrsstatic uint32_t
1025163953Srrssctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1026163953Srrs{
1027163953Srrs	struct sockaddr_in6 lsa6;
1028163953Srrs
1029163953Srrs	sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1030163953Srrs	    &lsa6);
1031163953Srrs	memcpy(ss, sa, sa->sa_len);
1032163953Srrs	return (0);
1033163953Srrs}
1034163953Srrs
1035163953Srrs
1036163953Srrs
1037166675Srrsstatic size_t
1038168124Srrssctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1039163953Srrs    struct sctp_tcb *stcb,
1040166675Srrs    size_t limit,
1041167598Srrs    struct sockaddr_storage *sas,
1042167598Srrs    uint32_t vrf_id)
1043163953Srrs{
1044167598Srrs	struct sctp_ifn *sctp_ifn;
1045167598Srrs	struct sctp_ifa *sctp_ifa;
1046166675Srrs	int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1047166675Srrs	size_t actual;
1048163953Srrs	int ipv4_addr_legal, ipv6_addr_legal;
1049167598Srrs	struct sctp_vrf *vrf;
1050163953Srrs
1051163953Srrs	actual = 0;
1052163953Srrs	if (limit <= 0)
1053163953Srrs		return (actual);
1054163953Srrs
1055163953Srrs	if (stcb) {
1056163953Srrs		/* Turn on all the appropriate scope */
1057163953Srrs		loopback_scope = stcb->asoc.loopback_scope;
1058163953Srrs		ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1059163953Srrs		local_scope = stcb->asoc.local_scope;
1060163953Srrs		site_scope = stcb->asoc.site_scope;
1061163953Srrs	} else {
1062163953Srrs		/* Turn on ALL scope, since we look at the EP */
1063163953Srrs		loopback_scope = ipv4_local_scope = local_scope =
1064163953Srrs		    site_scope = 1;
1065163953Srrs	}
1066163953Srrs	ipv4_addr_legal = ipv6_addr_legal = 0;
1067163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1068163953Srrs		ipv6_addr_legal = 1;
1069166023Srrs		if (SCTP_IPV6_V6ONLY(inp) == 0) {
1070163953Srrs			ipv4_addr_legal = 1;
1071163953Srrs		}
1072163953Srrs	} else {
1073163953Srrs		ipv4_addr_legal = 1;
1074163953Srrs	}
1075167598Srrs	vrf = sctp_find_vrf(vrf_id);
1076167598Srrs	if (vrf == NULL) {
1077167598Srrs		return (0);
1078167598Srrs	}
1079163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1080167598Srrs		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1081163953Srrs			if ((loopback_scope == 0) &&
1082167598Srrs			    SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1083163953Srrs				/* Skip loopback if loopback_scope not set */
1084163953Srrs				continue;
1085163953Srrs			}
1086167598Srrs			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1087163953Srrs				if (stcb) {
1088163953Srrs					/*
1089163953Srrs					 * For the BOUND-ALL case, the list
1090163953Srrs					 * associated with a TCB is Always
1091163953Srrs					 * considered a reverse list.. i.e.
1092163953Srrs					 * it lists addresses that are NOT
1093163953Srrs					 * part of the association. If this
1094163953Srrs					 * is one of those we must skip it.
1095163953Srrs					 */
1096163953Srrs					if (sctp_is_addr_restricted(stcb,
1097167598Srrs					    sctp_ifa)) {
1098163953Srrs						continue;
1099163953Srrs					}
1100163953Srrs				}
1101167598Srrs				if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
1102163953Srrs				    (ipv4_addr_legal)) {
1103163953Srrs					struct sockaddr_in *sin;
1104163953Srrs
1105167598Srrs					sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1106163953Srrs					if (sin->sin_addr.s_addr == 0) {
1107163953Srrs						/*
1108163953Srrs						 * we skip unspecifed
1109163953Srrs						 * addresses
1110163953Srrs						 */
1111163953Srrs						continue;
1112163953Srrs					}
1113163953Srrs					if ((ipv4_local_scope == 0) &&
1114163953Srrs					    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1115163953Srrs						continue;
1116163953Srrs					}
1117163953Srrs					if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
1118163953Srrs						in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1119163953Srrs						((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1120163953Srrs						sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1121163953Srrs						actual += sizeof(sizeof(struct sockaddr_in6));
1122163953Srrs					} else {
1123163953Srrs						memcpy(sas, sin, sizeof(*sin));
1124163953Srrs						((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1125163953Srrs						sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1126163953Srrs						actual += sizeof(*sin);
1127163953Srrs					}
1128163953Srrs					if (actual >= limit) {
1129163953Srrs						return (actual);
1130163953Srrs					}
1131167598Srrs				} else if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
1132163953Srrs				    (ipv6_addr_legal)) {
1133163953Srrs					struct sockaddr_in6 *sin6;
1134163953Srrs
1135167598Srrs					sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1136163953Srrs					if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1137163953Srrs						/*
1138163953Srrs						 * we skip unspecifed
1139163953Srrs						 * addresses
1140163953Srrs						 */
1141163953Srrs						continue;
1142163953Srrs					}
1143163953Srrs					if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1144163953Srrs						if (local_scope == 0)
1145163953Srrs							continue;
1146163953Srrs						if (sin6->sin6_scope_id == 0) {
1147163953Srrs							if (sa6_recoverscope(sin6) != 0)
1148163953Srrs								/*
1149163953Srrs								 * bad link
1150163953Srrs								 * local
1151163953Srrs								 * address
1152163953Srrs								 */
1153163953Srrs								continue;
1154163953Srrs						}
1155163953Srrs					}
1156163953Srrs					if ((site_scope == 0) &&
1157163953Srrs					    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1158163953Srrs						continue;
1159163953Srrs					}
1160163953Srrs					memcpy(sas, sin6, sizeof(*sin6));
1161163953Srrs					((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1162163953Srrs					sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1163163953Srrs					actual += sizeof(*sin6);
1164163953Srrs					if (actual >= limit) {
1165163953Srrs						return (actual);
1166163953Srrs					}
1167163953Srrs				}
1168163953Srrs			}
1169163953Srrs		}
1170163953Srrs	} else {
1171163953Srrs		struct sctp_laddr *laddr;
1172163953Srrs
1173167598Srrs		/* The list is a NEGATIVE list */
1174167598Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1175167598Srrs			if (stcb) {
1176167598Srrs				if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1177163953Srrs					continue;
1178163953Srrs				}
1179163953Srrs			}
1180167598Srrs			if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1181167598Srrs				continue;
1182167598Srrs
1183167598Srrs			((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1184167598Srrs			sas = (struct sockaddr_storage *)((caddr_t)sas +
1185167598Srrs			    laddr->ifa->address.sa.sa_len);
1186167598Srrs			actual += laddr->ifa->address.sa.sa_len;
1187167598Srrs			if (actual >= limit) {
1188167598Srrs				return (actual);
1189163953Srrs			}
1190163953Srrs		}
1191163953Srrs	}
1192163953Srrs	return (actual);
1193163953Srrs}
1194163953Srrs
1195168124Srrsstatic size_t
1196168124Srrssctp_fill_up_addresses(struct sctp_inpcb *inp,
1197168124Srrs    struct sctp_tcb *stcb,
1198168124Srrs    size_t limit,
1199168124Srrs    struct sockaddr_storage *sas)
1200168124Srrs{
1201168124Srrs	size_t size = 0;
1202168124Srrs
1203168124Srrs	/* fill up addresses for the endpoint's default vrf */
1204168124Srrs	size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1205168124Srrs	    inp->def_vrf_id);
1206168124Srrs	return (size);
1207168124Srrs}
1208168124Srrs
1209163953Srrsstatic int
1210168124Srrssctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1211163953Srrs{
1212163953Srrs	int cnt = 0;
1213167598Srrs	struct sctp_vrf *vrf = NULL;
1214163953Srrs
1215163953Srrs	/*
1216163953Srrs	 * In both sub-set bound an bound_all cases we return the MAXIMUM
1217163953Srrs	 * number of addresses that you COULD get. In reality the sub-set
1218163953Srrs	 * bound may have an exclusion list for a given TCB OR in the
1219163953Srrs	 * bound-all case a TCB may NOT include the loopback or other
1220163953Srrs	 * addresses as well.
1221163953Srrs	 */
1222167598Srrs	vrf = sctp_find_vrf(vrf_id);
1223167598Srrs	if (vrf == NULL) {
1224167598Srrs		return (0);
1225167598Srrs	}
1226163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1227167598Srrs		struct sctp_ifn *sctp_ifn;
1228167598Srrs		struct sctp_ifa *sctp_ifa;
1229163953Srrs
1230167598Srrs		LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1231167598Srrs			LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1232163953Srrs				/* Count them if they are the right type */
1233167598Srrs				if (sctp_ifa->address.sa.sa_family == AF_INET) {
1234163953Srrs					if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1235163953Srrs						cnt += sizeof(struct sockaddr_in6);
1236163953Srrs					else
1237163953Srrs						cnt += sizeof(struct sockaddr_in);
1238163953Srrs
1239167598Srrs				} else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1240163953Srrs					cnt += sizeof(struct sockaddr_in6);
1241163953Srrs			}
1242163953Srrs		}
1243163953Srrs	} else {
1244163953Srrs		struct sctp_laddr *laddr;
1245163953Srrs
1246163953Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1247167598Srrs			if (laddr->ifa->address.sa.sa_family == AF_INET) {
1248163953Srrs				if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1249163953Srrs					cnt += sizeof(struct sockaddr_in6);
1250163953Srrs				else
1251163953Srrs					cnt += sizeof(struct sockaddr_in);
1252163953Srrs
1253167598Srrs			} else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1254163953Srrs				cnt += sizeof(struct sockaddr_in6);
1255163953Srrs		}
1256163953Srrs	}
1257163953Srrs	return (cnt);
1258163953Srrs}
1259163953Srrs
1260168124Srrsstatic int
1261168124Srrssctp_count_max_addresses(struct sctp_inpcb *inp)
1262168124Srrs{
1263168124Srrs	int cnt = 0;
1264166675Srrs
1265168124Srrs	/* count addresses for the endpoint's default VRF */
1266168124Srrs	cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1267168124Srrs	return (cnt);
1268168124Srrs}
1269168124Srrs
1270163953Srrsstatic int
1271166675Srrssctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1272166675Srrs    size_t optsize, void *p, int delay)
1273163953Srrs{
1274163953Srrs	int error = 0;
1275163953Srrs	int creat_lock_on = 0;
1276163953Srrs	struct sctp_tcb *stcb = NULL;
1277163953Srrs	struct sockaddr *sa;
1278169352Srrs	int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1279169352Srrs	int added = 0;
1280167598Srrs	uint32_t vrf_id;
1281170056Srrs	int bad_addresses = 0;
1282167598Srrs	sctp_assoc_t *a_id;
1283163953Srrs
1284169420Srrs	SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1285163953Srrs
1286163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1287163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1288163953Srrs		/* We are already connected AND the TCP model */
1289163953Srrs		return (EADDRINUSE);
1290163953Srrs	}
1291163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1292163953Srrs		return (EINVAL);
1293163953Srrs	}
1294163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1295163953Srrs		SCTP_INP_RLOCK(inp);
1296163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1297163953Srrs		SCTP_INP_RUNLOCK(inp);
1298163953Srrs	}
1299163953Srrs	if (stcb) {
1300163953Srrs		return (EALREADY);
1301163953Srrs	}
1302163953Srrs	SCTP_INP_INCR_REF(inp);
1303163953Srrs	SCTP_ASOC_CREATE_LOCK(inp);
1304163953Srrs	creat_lock_on = 1;
1305163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1306163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1307163953Srrs		error = EFAULT;
1308163953Srrs		goto out_now;
1309163953Srrs	}
1310166675Srrs	totaddrp = (int *)optval;
1311163953Srrs	totaddr = *totaddrp;
1312163953Srrs	sa = (struct sockaddr *)(totaddrp + 1);
1313170056Srrs	stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1314170056Srrs	if ((stcb != NULL) || bad_addresses) {
1315169352Srrs		/* Already have or am bring up an association */
1316169352Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
1317169352Srrs		creat_lock_on = 0;
1318170931Srrs		if (stcb)
1319170931Srrs			SCTP_TCB_UNLOCK(stcb);
1320170056Srrs		if (bad_addresses == 0)
1321170056Srrs			error = EALREADY;
1322169352Srrs		goto out_now;
1323163953Srrs	}
1324163953Srrs#ifdef INET6
1325163953Srrs	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1326163953Srrs	    (num_v6 > 0)) {
1327163953Srrs		error = EINVAL;
1328163953Srrs		goto out_now;
1329163953Srrs	}
1330163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1331163953Srrs	    (num_v4 > 0)) {
1332163953Srrs		struct in6pcb *inp6;
1333163953Srrs
1334163953Srrs		inp6 = (struct in6pcb *)inp;
1335166023Srrs		if (SCTP_IPV6_V6ONLY(inp6)) {
1336163953Srrs			/*
1337163953Srrs			 * if IPV6_V6ONLY flag, ignore connections destined
1338163953Srrs			 * to a v4 addr or v4-mapped addr
1339163953Srrs			 */
1340163953Srrs			error = EINVAL;
1341163953Srrs			goto out_now;
1342163953Srrs		}
1343163953Srrs	}
1344163953Srrs#endif				/* INET6 */
1345163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1346163953Srrs	    SCTP_PCB_FLAGS_UNBOUND) {
1347163953Srrs		/* Bind a ephemeral port */
1348170744Srrs		error = sctp_inpcb_bind(so, NULL, NULL, p);
1349163953Srrs		if (error) {
1350163953Srrs			goto out_now;
1351163953Srrs		}
1352163953Srrs	}
1353167695Srrs	/* FIX ME: do we want to pass in a vrf on the connect call? */
1354167695Srrs	vrf_id = inp->def_vrf_id;
1355167695Srrs
1356163953Srrs	/* We are GOOD to go */
1357167598Srrs	stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id);
1358163953Srrs	if (stcb == NULL) {
1359163953Srrs		/* Gak! no memory */
1360163953Srrs		goto out_now;
1361163953Srrs	}
1362169352Srrs	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1363163953Srrs	/* move to second address */
1364163953Srrs	if (sa->sa_family == AF_INET)
1365163953Srrs		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1366163953Srrs	else
1367163953Srrs		sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1368163953Srrs
1369170056Srrs	error = 0;
1370169352Srrs	added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1371167598Srrs	/* Fill in the return id */
1372170056Srrs	if (error) {
1373170056Srrs		sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
1374170056Srrs		goto out_now;
1375170056Srrs	}
1376167598Srrs	a_id = (sctp_assoc_t *) optval;
1377167598Srrs	*a_id = sctp_get_associd(stcb);
1378163953Srrs
1379163953Srrs	/* initialize authentication parameters for the assoc */
1380163953Srrs	sctp_initialize_auth_params(inp, stcb);
1381163953Srrs
1382163953Srrs	if (delay) {
1383163953Srrs		/* doing delayed connection */
1384163953Srrs		stcb->asoc.delayed_connection = 1;
1385163953Srrs		sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1386163953Srrs	} else {
1387169378Srrs		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1388163953Srrs		sctp_send_initiate(inp, stcb);
1389163953Srrs	}
1390163953Srrs	SCTP_TCB_UNLOCK(stcb);
1391163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1392163953Srrs		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1393163953Srrs		/* Set the connected flag so we can queue data */
1394163953Srrs		soisconnecting(so);
1395163953Srrs	}
1396163953Srrsout_now:
1397169655Srrs	if (creat_lock_on) {
1398163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
1399169655Srrs	}
1400163953Srrs	SCTP_INP_DECR_REF(inp);
1401163953Srrs	return error;
1402163953Srrs}
1403163953Srrs
1404169420Srrs#define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1405169655Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1406169655Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1407166675Srrs		SCTP_INP_RLOCK(inp); \
1408166675Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1409169655Srrs		if (stcb) { \
1410166675Srrs			SCTP_TCB_LOCK(stcb); \
1411169655Srrs                } \
1412166675Srrs		SCTP_INP_RUNLOCK(inp); \
1413166675Srrs	} else if (assoc_id != 0) { \
1414166675Srrs		stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1415166675Srrs		if (stcb == NULL) { \
1416166675Srrs			error = ENOENT; \
1417166675Srrs			break; \
1418166675Srrs		} \
1419166675Srrs	} else { \
1420166675Srrs		stcb = NULL; \
1421169420Srrs        } \
1422169420Srrs  }
1423163953Srrs
1424169420Srrs
1425169420Srrs#define SCTP_CHECK_AND_CAST(destp, srcp, type, size)  {\
1426166675Srrs	if (size < sizeof(type)) { \
1427166675Srrs		error = EINVAL; \
1428166675Srrs		break; \
1429166675Srrs	} else { \
1430166675Srrs		destp = (type *)srcp; \
1431169420Srrs	} \
1432169420Srrs      }
1433163953Srrs
1434163953Srrsstatic int
1435166675Srrssctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1436166675Srrs    void *p)
1437163953Srrs{
1438163953Srrs	struct sctp_inpcb *inp;
1439166675Srrs	int error, val = 0;
1440163953Srrs	struct sctp_tcb *stcb = NULL;
1441163953Srrs
1442166675Srrs	if (optval == NULL) {
1443166675Srrs		return (EINVAL);
1444166675Srrs	}
1445163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1446163953Srrs	if (inp == 0)
1447163953Srrs		return EINVAL;
1448163953Srrs	error = 0;
1449163953Srrs
1450166675Srrs	switch (optname) {
1451163953Srrs	case SCTP_NODELAY:
1452163953Srrs	case SCTP_AUTOCLOSE:
1453163953Srrs	case SCTP_EXPLICIT_EOR:
1454163953Srrs	case SCTP_AUTO_ASCONF:
1455163953Srrs	case SCTP_DISABLE_FRAGMENTS:
1456163953Srrs	case SCTP_I_WANT_MAPPED_V4_ADDR:
1457163953Srrs	case SCTP_USE_EXT_RCVINFO:
1458163953Srrs		SCTP_INP_RLOCK(inp);
1459166675Srrs		switch (optname) {
1460163953Srrs		case SCTP_DISABLE_FRAGMENTS:
1461166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1462163953Srrs			break;
1463163953Srrs		case SCTP_I_WANT_MAPPED_V4_ADDR:
1464166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1465163953Srrs			break;
1466163953Srrs		case SCTP_AUTO_ASCONF:
1467166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1468163953Srrs			break;
1469163953Srrs		case SCTP_EXPLICIT_EOR:
1470166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1471163953Srrs			break;
1472163953Srrs		case SCTP_NODELAY:
1473166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1474163953Srrs			break;
1475163953Srrs		case SCTP_USE_EXT_RCVINFO:
1476166675Srrs			val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1477163953Srrs			break;
1478163953Srrs		case SCTP_AUTOCLOSE:
1479163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1480166675Srrs				val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1481163953Srrs			else
1482166675Srrs				val = 0;
1483163953Srrs			break;
1484163953Srrs
1485163953Srrs		default:
1486163953Srrs			error = ENOPROTOOPT;
1487163953Srrs		}		/* end switch (sopt->sopt_name) */
1488166675Srrs		if (optname != SCTP_AUTOCLOSE) {
1489163953Srrs			/* make it an "on/off" value */
1490166675Srrs			val = (val != 0);
1491163953Srrs		}
1492166675Srrs		if (*optsize < sizeof(val)) {
1493163953Srrs			error = EINVAL;
1494163953Srrs		}
1495163953Srrs		SCTP_INP_RUNLOCK(inp);
1496163953Srrs		if (error == 0) {
1497163953Srrs			/* return the option value */
1498166675Srrs			*(int *)optval = val;
1499166675Srrs			*optsize = sizeof(val);
1500163953Srrs		}
1501163953Srrs		break;
1502170091Srrs	case SCTP_GET_PACKET_LOG:
1503170091Srrs		{
1504170091Srrs#ifdef  SCTP_PACKET_LOGGING
1505170091Srrs			uint8_t *target;
1506170091Srrs			int ret;
1507167598Srrs
1508170091Srrs			SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1509170091Srrs			ret = sctp_copy_out_packet_log(target, (int)*optsize);
1510170091Srrs			*optsize = ret;
1511170091Srrs#else
1512170091Srrs			error = EOPNOTSUPP;
1513170091Srrs#endif
1514170091Srrs			break;
1515170091Srrs		}
1516163953Srrs	case SCTP_PARTIAL_DELIVERY_POINT:
1517163953Srrs		{
1518166675Srrs			uint32_t *value;
1519166675Srrs
1520166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1521166675Srrs			*value = inp->partial_delivery_point;
1522166675Srrs			*optsize = sizeof(uint32_t);
1523163953Srrs		}
1524163953Srrs		break;
1525163953Srrs	case SCTP_FRAGMENT_INTERLEAVE:
1526163953Srrs		{
1527166675Srrs			uint32_t *value;
1528166675Srrs
1529166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1530168943Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1531168943Srrs				if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1532168943Srrs					*value = SCTP_FRAG_LEVEL_2;
1533168943Srrs				} else {
1534168943Srrs					*value = SCTP_FRAG_LEVEL_1;
1535168943Srrs				}
1536168943Srrs			} else {
1537168943Srrs				*value = SCTP_FRAG_LEVEL_0;
1538168943Srrs			}
1539166675Srrs			*optsize = sizeof(uint32_t);
1540163953Srrs		}
1541163953Srrs		break;
1542163953Srrs	case SCTP_CMT_ON_OFF:
1543163953Srrs		{
1544166675Srrs			struct sctp_assoc_value *av;
1545166675Srrs
1546166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1547166675Srrs			if (sctp_cmt_on_off) {
1548166675Srrs				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1549166675Srrs				if (stcb) {
1550166675Srrs					av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1551166675Srrs					SCTP_TCB_UNLOCK(stcb);
1552166675Srrs
1553166675Srrs				} else {
1554166675Srrs					error = ENOTCONN;
1555166675Srrs				}
1556166675Srrs			} else {
1557166675Srrs				error = ENOPROTOOPT;
1558163953Srrs			}
1559166675Srrs			*optsize = sizeof(*av);
1560163953Srrs		}
1561163953Srrs		break;
1562171440Srrs		/* JRS - Get socket option for pluggable congestion control */
1563171440Srrs	case SCTP_PLUGGABLE_CC:
1564171440Srrs		{
1565171440Srrs			struct sctp_assoc_value *av;
1566171440Srrs
1567171440Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1568171440Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1569171440Srrs			if (stcb) {
1570171440Srrs				av->assoc_value = stcb->asoc.congestion_control_module;
1571171440Srrs				SCTP_TCB_UNLOCK(stcb);
1572171440Srrs			} else {
1573171440Srrs				av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1574171440Srrs			}
1575171440Srrs			*optsize = sizeof(*av);
1576171440Srrs		}
1577171440Srrs		break;
1578163953Srrs	case SCTP_GET_ADDR_LEN:
1579163953Srrs		{
1580163953Srrs			struct sctp_assoc_value *av;
1581163953Srrs
1582166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1583163953Srrs			error = EINVAL;
1584167598Srrs#ifdef INET
1585163953Srrs			if (av->assoc_value == AF_INET) {
1586163953Srrs				av->assoc_value = sizeof(struct sockaddr_in);
1587163953Srrs				error = 0;
1588163953Srrs			}
1589163953Srrs#endif
1590167598Srrs#ifdef INET6
1591163953Srrs			if (av->assoc_value == AF_INET6) {
1592163953Srrs				av->assoc_value = sizeof(struct sockaddr_in6);
1593163953Srrs				error = 0;
1594163953Srrs			}
1595163953Srrs#endif
1596166675Srrs			*optsize = sizeof(*av);
1597163953Srrs		}
1598163953Srrs		break;
1599169655Srrs	case SCTP_GET_ASSOC_NUMBER:
1600163953Srrs		{
1601169655Srrs			uint32_t *value, cnt;
1602163953Srrs
1603169655Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1604163953Srrs			cnt = 0;
1605163953Srrs			SCTP_INP_RLOCK(inp);
1606169655Srrs			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1607169655Srrs				cnt++;
1608163953Srrs			}
1609169655Srrs			SCTP_INP_RUNLOCK(inp);
1610169655Srrs			*value = cnt;
1611169655Srrs			*optsize = sizeof(uint32_t);
1612169655Srrs		}
1613169655Srrs		break;
1614163953Srrs
1615169655Srrs	case SCTP_GET_ASSOC_ID_LIST:
1616169655Srrs		{
1617169655Srrs			struct sctp_assoc_ids *ids;
1618169655Srrs			unsigned int at, limit;
1619169655Srrs
1620169655Srrs			SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1621163953Srrs			at = 0;
1622169655Srrs			limit = *optsize / sizeof(sctp_assoc_t);
1623169655Srrs			SCTP_INP_RLOCK(inp);
1624169655Srrs			LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1625169655Srrs				if (at < limit) {
1626169655Srrs					ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1627169655Srrs				} else {
1628169655Srrs					error = EINVAL;
1629163953Srrs					break;
1630163953Srrs				}
1631163953Srrs			}
1632163953Srrs			SCTP_INP_RUNLOCK(inp);
1633169655Srrs			*optsize = at * sizeof(sctp_assoc_t);
1634163953Srrs		}
1635163953Srrs		break;
1636163953Srrs	case SCTP_CONTEXT:
1637163953Srrs		{
1638163953Srrs			struct sctp_assoc_value *av;
1639163953Srrs
1640166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1641166675Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1642166675Srrs
1643166675Srrs			if (stcb) {
1644166675Srrs				av->assoc_value = stcb->asoc.context;
1645166675Srrs				SCTP_TCB_UNLOCK(stcb);
1646163953Srrs			} else {
1647166675Srrs				SCTP_INP_RLOCK(inp);
1648163953Srrs				av->assoc_value = inp->sctp_context;
1649166675Srrs				SCTP_INP_RUNLOCK(inp);
1650163953Srrs			}
1651166675Srrs			*optsize = sizeof(*av);
1652163953Srrs		}
1653163953Srrs		break;
1654167598Srrs	case SCTP_VRF_ID:
1655167598Srrs		{
1656170056Srrs			uint32_t *default_vrfid;
1657167598Srrs
1658170056Srrs			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1659170056Srrs			*default_vrfid = inp->def_vrf_id;
1660167598Srrs			break;
1661167598Srrs		}
1662167598Srrs	case SCTP_GET_ASOC_VRF:
1663167598Srrs		{
1664167598Srrs			struct sctp_assoc_value *id;
1665167598Srrs
1666167598Srrs			SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1667167598Srrs			SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1668167598Srrs			if (stcb == NULL) {
1669167598Srrs				error = EINVAL;
1670167598Srrs				break;
1671167598Srrs			}
1672167598Srrs			id->assoc_value = stcb->asoc.vrf_id;
1673167598Srrs			break;
1674167598Srrs		}
1675167598Srrs	case SCTP_GET_VRF_IDS:
1676167598Srrs		{
1677167598Srrs			error = EOPNOTSUPP;
1678167598Srrs			break;
1679167598Srrs		}
1680163953Srrs	case SCTP_GET_NONCE_VALUES:
1681163953Srrs		{
1682163953Srrs			struct sctp_get_nonce_values *gnv;
1683163953Srrs
1684166675Srrs			SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1685166675Srrs			SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1686166675Srrs
1687166675Srrs			if (stcb) {
1688163953Srrs				gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1689163953Srrs				gnv->gn_local_tag = stcb->asoc.my_vtag;
1690163953Srrs				SCTP_TCB_UNLOCK(stcb);
1691166675Srrs			} else {
1692166675Srrs				error = ENOTCONN;
1693163953Srrs			}
1694166675Srrs			*optsize = sizeof(*gnv);
1695163953Srrs		}
1696163953Srrs		break;
1697170056Srrs	case SCTP_DELAYED_SACK:
1698163953Srrs		{
1699170056Srrs			struct sctp_sack_info *sack;
1700163953Srrs
1701170056Srrs			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1702170056Srrs			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1703166675Srrs			if (stcb) {
1704170056Srrs				sack->sack_delay = stcb->asoc.delayed_ack;
1705170056Srrs				sack->sack_freq = stcb->asoc.sack_freq;
1706166675Srrs				SCTP_TCB_UNLOCK(stcb);
1707166675Srrs			} else {
1708163953Srrs				SCTP_INP_RLOCK(inp);
1709170056Srrs				sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1710170056Srrs				sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1711163953Srrs				SCTP_INP_RUNLOCK(inp);
1712163953Srrs			}
1713170056Srrs			*optsize = sizeof(*sack);
1714163953Srrs		}
1715163953Srrs		break;
1716163953Srrs
1717163953Srrs	case SCTP_GET_SNDBUF_USE:
1718166675Srrs		{
1719163953Srrs			struct sctp_sockstat *ss;
1720163953Srrs
1721166675Srrs			SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1722166675Srrs			SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1723166675Srrs
1724166675Srrs			if (stcb) {
1725166675Srrs				ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1726166675Srrs				ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1727166675Srrs				    stcb->asoc.size_on_all_streams);
1728166675Srrs				SCTP_TCB_UNLOCK(stcb);
1729166675Srrs			} else {
1730163953Srrs				error = ENOTCONN;
1731163953Srrs			}
1732166675Srrs			*optsize = sizeof(struct sctp_sockstat);
1733163953Srrs		}
1734163953Srrs		break;
1735170056Srrs	case SCTP_MAX_BURST:
1736163953Srrs		{
1737166675Srrs			uint8_t *value;
1738163953Srrs
1739166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1740166675Srrs
1741163953Srrs			SCTP_INP_RLOCK(inp);
1742166675Srrs			*value = inp->sctp_ep.max_burst;
1743163953Srrs			SCTP_INP_RUNLOCK(inp);
1744166675Srrs			*optsize = sizeof(uint8_t);
1745163953Srrs		}
1746163953Srrs		break;
1747163953Srrs	case SCTP_MAXSEG:
1748163953Srrs		{
1749167598Srrs			struct sctp_assoc_value *av;
1750163953Srrs			int ovh;
1751163953Srrs
1752167598Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1753170056Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1754163953Srrs
1755167598Srrs			if (stcb) {
1756167598Srrs				av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1757167598Srrs				SCTP_TCB_UNLOCK(stcb);
1758163953Srrs			} else {
1759167598Srrs				SCTP_INP_RLOCK(inp);
1760167598Srrs				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1761167598Srrs					ovh = SCTP_MED_OVERHEAD;
1762167598Srrs				} else {
1763167598Srrs					ovh = SCTP_MED_V4_OVERHEAD;
1764167598Srrs				}
1765170056Srrs				if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
1766170056Srrs					av->assoc_value = 0;
1767170056Srrs				else
1768170056Srrs					av->assoc_value = inp->sctp_frag_point - ovh;
1769167598Srrs				SCTP_INP_RUNLOCK(inp);
1770163953Srrs			}
1771167598Srrs			*optsize = sizeof(struct sctp_assoc_value);
1772163953Srrs		}
1773163953Srrs		break;
1774163953Srrs	case SCTP_GET_STAT_LOG:
1775167598Srrs		error = sctp_fill_stat_log(optval, optsize);
1776163953Srrs		break;
1777163953Srrs	case SCTP_EVENTS:
1778163953Srrs		{
1779163953Srrs			struct sctp_event_subscribe *events;
1780163953Srrs
1781166675Srrs			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1782163953Srrs			memset(events, 0, sizeof(*events));
1783163953Srrs			SCTP_INP_RLOCK(inp);
1784163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1785163953Srrs				events->sctp_data_io_event = 1;
1786163953Srrs
1787163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1788163953Srrs				events->sctp_association_event = 1;
1789163953Srrs
1790163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1791163953Srrs				events->sctp_address_event = 1;
1792163953Srrs
1793163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1794163953Srrs				events->sctp_send_failure_event = 1;
1795163953Srrs
1796163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1797163953Srrs				events->sctp_peer_error_event = 1;
1798163953Srrs
1799163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1800163953Srrs				events->sctp_shutdown_event = 1;
1801163953Srrs
1802163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1803163953Srrs				events->sctp_partial_delivery_event = 1;
1804163953Srrs
1805163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1806163953Srrs				events->sctp_adaptation_layer_event = 1;
1807163953Srrs
1808163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
1809163953Srrs				events->sctp_authentication_event = 1;
1810163953Srrs
1811163953Srrs			if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
1812163953Srrs				events->sctp_stream_reset_events = 1;
1813163953Srrs			SCTP_INP_RUNLOCK(inp);
1814166675Srrs			*optsize = sizeof(struct sctp_event_subscribe);
1815163953Srrs		}
1816163953Srrs		break;
1817163953Srrs
1818163953Srrs	case SCTP_ADAPTATION_LAYER:
1819166675Srrs		{
1820166675Srrs			uint32_t *value;
1821166675Srrs
1822166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1823166675Srrs
1824166675Srrs			SCTP_INP_RLOCK(inp);
1825166675Srrs			*value = inp->sctp_ep.adaptation_layer_indicator;
1826166675Srrs			SCTP_INP_RUNLOCK(inp);
1827166675Srrs			*optsize = sizeof(uint32_t);
1828163953Srrs		}
1829163953Srrs		break;
1830163953Srrs	case SCTP_SET_INITIAL_DBG_SEQ:
1831166675Srrs		{
1832166675Srrs			uint32_t *value;
1833166675Srrs
1834166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1835166675Srrs			SCTP_INP_RLOCK(inp);
1836166675Srrs			*value = inp->sctp_ep.initial_sequence_debug;
1837166675Srrs			SCTP_INP_RUNLOCK(inp);
1838166675Srrs			*optsize = sizeof(uint32_t);
1839163953Srrs		}
1840163953Srrs		break;
1841163953Srrs	case SCTP_GET_LOCAL_ADDR_SIZE:
1842166675Srrs		{
1843166675Srrs			uint32_t *value;
1844166675Srrs
1845166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1846166675Srrs			SCTP_INP_RLOCK(inp);
1847168124Srrs			*value = sctp_count_max_addresses(inp);
1848166675Srrs			SCTP_INP_RUNLOCK(inp);
1849166675Srrs			*optsize = sizeof(uint32_t);
1850163953Srrs		}
1851163953Srrs		break;
1852163953Srrs	case SCTP_GET_REMOTE_ADDR_SIZE:
1853163953Srrs		{
1854166675Srrs			uint32_t *value;
1855166675Srrs			size_t size;
1856163953Srrs			struct sctp_nets *net;
1857163953Srrs
1858166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1859166675Srrs			/* FIXME MT: change to sctp_assoc_value? */
1860166675Srrs			SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
1861166675Srrs
1862166675Srrs			if (stcb) {
1863166675Srrs				size = 0;
1864166675Srrs				/* Count the sizes */
1865166675Srrs				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1866166675Srrs					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1867166675Srrs					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1868166675Srrs						size += sizeof(struct sockaddr_in6);
1869166675Srrs					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1870166675Srrs						size += sizeof(struct sockaddr_in);
1871166675Srrs					} else {
1872166675Srrs						/* huh */
1873166675Srrs						break;
1874166675Srrs					}
1875163953Srrs				}
1876166675Srrs				SCTP_TCB_UNLOCK(stcb);
1877166675Srrs				*value = (uint32_t) size;
1878166675Srrs			} else {
1879166675Srrs				error = ENOTCONN;
1880163953Srrs			}
1881166675Srrs			*optsize = sizeof(uint32_t);
1882163953Srrs		}
1883163953Srrs		break;
1884163953Srrs	case SCTP_GET_PEER_ADDRESSES:
1885163953Srrs		/*
1886163953Srrs		 * Get the address information, an array is passed in to
1887163953Srrs		 * fill up we pack it.
1888163953Srrs		 */
1889163953Srrs		{
1890166675Srrs			size_t cpsz, left;
1891163953Srrs			struct sockaddr_storage *sas;
1892163953Srrs			struct sctp_nets *net;
1893163953Srrs			struct sctp_getaddresses *saddr;
1894163953Srrs
1895166675Srrs			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1896166675Srrs			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1897163953Srrs
1898166675Srrs			if (stcb) {
1899166675Srrs				left = (*optsize) - sizeof(struct sctp_getaddresses);
1900166675Srrs				*optsize = sizeof(struct sctp_getaddresses);
1901166675Srrs				sas = (struct sockaddr_storage *)&saddr->addr[0];
1902166675Srrs
1903166675Srrs				TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1904166675Srrs					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1905166675Srrs					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1906166675Srrs						cpsz = sizeof(struct sockaddr_in6);
1907166675Srrs					} else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1908166675Srrs						cpsz = sizeof(struct sockaddr_in);
1909166675Srrs					} else {
1910166675Srrs						/* huh */
1911166675Srrs						break;
1912166675Srrs					}
1913166675Srrs					if (left < cpsz) {
1914166675Srrs						/* not enough room. */
1915166675Srrs						break;
1916166675Srrs					}
1917166675Srrs					if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
1918166675Srrs					    (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
1919166675Srrs						/* Must map the address */
1920166675Srrs						in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
1921166675Srrs						    (struct sockaddr_in6 *)sas);
1922166675Srrs					} else {
1923166675Srrs						memcpy(sas, &net->ro._l_addr, cpsz);
1924166675Srrs					}
1925166675Srrs					((struct sockaddr_in *)sas)->sin_port = stcb->rport;
1926166675Srrs
1927166675Srrs					sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
1928166675Srrs					left -= cpsz;
1929166675Srrs					*optsize += cpsz;
1930163953Srrs				}
1931166675Srrs				SCTP_TCB_UNLOCK(stcb);
1932166675Srrs			} else {
1933166675Srrs				error = ENOENT;
1934163953Srrs			}
1935163953Srrs		}
1936163953Srrs		break;
1937163953Srrs	case SCTP_GET_LOCAL_ADDRESSES:
1938163953Srrs		{
1939166675Srrs			size_t limit, actual;
1940163953Srrs			struct sockaddr_storage *sas;
1941163953Srrs			struct sctp_getaddresses *saddr;
1942163953Srrs
1943166675Srrs			SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1944166675Srrs			SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1945163953Srrs
1946163953Srrs			sas = (struct sockaddr_storage *)&saddr->addr[0];
1947166675Srrs			limit = *optsize - sizeof(sctp_assoc_t);
1948168124Srrs			actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
1949169655Srrs			if (stcb) {
1950163953Srrs				SCTP_TCB_UNLOCK(stcb);
1951169655Srrs			}
1952166675Srrs			*optsize = sizeof(struct sockaddr_storage) + actual;
1953163953Srrs		}
1954163953Srrs		break;
1955163953Srrs	case SCTP_PEER_ADDR_PARAMS:
1956163953Srrs		{
1957163953Srrs			struct sctp_paddrparams *paddrp;
1958163953Srrs			struct sctp_nets *net;
1959163953Srrs
1960166675Srrs			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
1961166675Srrs			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
1962163953Srrs
1963163953Srrs			net = NULL;
1964166675Srrs			if (stcb) {
1965166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
1966166675Srrs			} else {
1967166675Srrs				/*
1968166675Srrs				 * We increment here since
1969166675Srrs				 * sctp_findassociation_ep_addr() wil do a
1970166675Srrs				 * decrement if it finds the stcb as long as
1971166675Srrs				 * the locked tcb (last argument) is NOT a
1972166675Srrs				 * TCB.. aka NULL.
1973166675Srrs				 */
1974166675Srrs				SCTP_INP_INCR_REF(inp);
1975166675Srrs				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
1976163953Srrs				if (stcb == NULL) {
1977166675Srrs					SCTP_INP_DECR_REF(inp);
1978163953Srrs				}
1979163953Srrs			}
1980163953Srrs
1981163953Srrs			if (stcb) {
1982163953Srrs				/* Applys to the specific association */
1983163953Srrs				paddrp->spp_flags = 0;
1984163953Srrs				if (net) {
1985170056Srrs					int ovh;
1986170056Srrs
1987170056Srrs					if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1988170056Srrs						ovh = SCTP_MED_OVERHEAD;
1989170056Srrs					} else {
1990170056Srrs						ovh = SCTP_MED_V4_OVERHEAD;
1991170056Srrs					}
1992170056Srrs
1993170056Srrs
1994163953Srrs					paddrp->spp_pathmaxrxt = net->failure_threshold;
1995170056Srrs					paddrp->spp_pathmtu = net->mtu - ovh;
1996163953Srrs					/* get flags for HB */
1997163953Srrs					if (net->dest_state & SCTP_ADDR_NOHB)
1998163953Srrs						paddrp->spp_flags |= SPP_HB_DISABLE;
1999163953Srrs					else
2000163953Srrs						paddrp->spp_flags |= SPP_HB_ENABLE;
2001163953Srrs					/* get flags for PMTU */
2002165647Srrs					if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2003163953Srrs						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2004163953Srrs					} else {
2005163953Srrs						paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2006163953Srrs					}
2007167598Srrs#ifdef INET
2008163953Srrs					if (net->ro._l_addr.sin.sin_family == AF_INET) {
2009163953Srrs						paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2010163953Srrs						paddrp->spp_flags |= SPP_IPV4_TOS;
2011163953Srrs					}
2012163953Srrs#endif
2013167598Srrs#ifdef INET6
2014163953Srrs					if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2015163953Srrs						paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2016163953Srrs						paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2017163953Srrs					}
2018163953Srrs#endif
2019163953Srrs				} else {
2020163953Srrs					/*
2021163953Srrs					 * No destination so return default
2022163953Srrs					 * value
2023163953Srrs					 */
2024170056Srrs					int cnt = 0;
2025170056Srrs
2026163953Srrs					paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2027163953Srrs					paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2028167598Srrs#ifdef INET
2029163953Srrs					paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2030163953Srrs					paddrp->spp_flags |= SPP_IPV4_TOS;
2031163953Srrs#endif
2032167598Srrs#ifdef INET6
2033163953Srrs					paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2034163953Srrs					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2035163953Srrs#endif
2036163953Srrs					/* default settings should be these */
2037170056Srrs					if (stcb->asoc.hb_is_disabled == 0) {
2038163953Srrs						paddrp->spp_flags |= SPP_HB_ENABLE;
2039170056Srrs					} else {
2040170056Srrs						paddrp->spp_flags |= SPP_HB_DISABLE;
2041163953Srrs					}
2042170056Srrs					TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2043170056Srrs						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2044170056Srrs							cnt++;
2045170056Srrs						}
2046170056Srrs					}
2047170056Srrs					if (cnt) {
2048170056Srrs						paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2049170056Srrs					}
2050163953Srrs				}
2051163953Srrs				paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2052163953Srrs				paddrp->spp_assoc_id = sctp_get_associd(stcb);
2053163953Srrs				SCTP_TCB_UNLOCK(stcb);
2054163953Srrs			} else {
2055163953Srrs				/* Use endpoint defaults */
2056163953Srrs				SCTP_INP_RLOCK(inp);
2057163953Srrs				paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2058163953Srrs				paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2059163953Srrs				paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2060163953Srrs				/* get inp's default */
2061167598Srrs#ifdef INET
2062163953Srrs				paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2063163953Srrs				paddrp->spp_flags |= SPP_IPV4_TOS;
2064163953Srrs#endif
2065167598Srrs#ifdef INET6
2066163953Srrs				if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2067163953Srrs					paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2068163953Srrs					paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2069163953Srrs				}
2070163953Srrs#endif
2071163953Srrs				/* can't return this */
2072163953Srrs				paddrp->spp_pathmtu = 0;
2073170056Srrs
2074163953Srrs				/* default behavior, no stcb */
2075170056Srrs				paddrp->spp_flags = SPP_PMTUD_ENABLE;
2076163953Srrs
2077170056Srrs				if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2078170056Srrs					paddrp->spp_flags |= SPP_HB_ENABLE;
2079170056Srrs				} else {
2080170056Srrs					paddrp->spp_flags |= SPP_HB_DISABLE;
2081170056Srrs				}
2082163953Srrs				SCTP_INP_RUNLOCK(inp);
2083163953Srrs			}
2084166675Srrs			*optsize = sizeof(struct sctp_paddrparams);
2085163953Srrs		}
2086163953Srrs		break;
2087163953Srrs	case SCTP_GET_PEER_ADDR_INFO:
2088163953Srrs		{
2089163953Srrs			struct sctp_paddrinfo *paddri;
2090163953Srrs			struct sctp_nets *net;
2091163953Srrs
2092166675Srrs			SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2093166675Srrs			SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2094166675Srrs
2095163953Srrs			net = NULL;
2096166675Srrs			if (stcb) {
2097166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2098166675Srrs			} else {
2099166675Srrs				/*
2100166675Srrs				 * We increment here since
2101166675Srrs				 * sctp_findassociation_ep_addr() wil do a
2102166675Srrs				 * decrement if it finds the stcb as long as
2103166675Srrs				 * the locked tcb (last argument) is NOT a
2104166675Srrs				 * TCB.. aka NULL.
2105166675Srrs				 */
2106166675Srrs				SCTP_INP_INCR_REF(inp);
2107166675Srrs				stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2108166675Srrs				if (stcb == NULL) {
2109166675Srrs					SCTP_INP_DECR_REF(inp);
2110163953Srrs				}
2111166675Srrs			}
2112163953Srrs
2113166675Srrs			if ((stcb) && (net)) {
2114166675Srrs				paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2115166675Srrs				paddri->spinfo_cwnd = net->cwnd;
2116166675Srrs				paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2117166675Srrs				paddri->spinfo_rto = net->RTO;
2118166675Srrs				paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2119166675Srrs				SCTP_TCB_UNLOCK(stcb);
2120163953Srrs			} else {
2121163953Srrs				if (stcb) {
2122163953Srrs					SCTP_TCB_UNLOCK(stcb);
2123163953Srrs				}
2124163953Srrs				error = ENOENT;
2125163953Srrs			}
2126166675Srrs			*optsize = sizeof(struct sctp_paddrinfo);
2127163953Srrs		}
2128163953Srrs		break;
2129163953Srrs	case SCTP_PCB_STATUS:
2130163953Srrs		{
2131163953Srrs			struct sctp_pcbinfo *spcb;
2132163953Srrs
2133166675Srrs			SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2134163953Srrs			sctp_fill_pcbinfo(spcb);
2135166675Srrs			*optsize = sizeof(struct sctp_pcbinfo);
2136163953Srrs		}
2137163953Srrs		break;
2138167598Srrs
2139163953Srrs	case SCTP_STATUS:
2140163953Srrs		{
2141163953Srrs			struct sctp_nets *net;
2142163953Srrs			struct sctp_status *sstat;
2143163953Srrs
2144166675Srrs			SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2145166675Srrs			SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2146163953Srrs
2147163953Srrs			if (stcb == NULL) {
2148163953Srrs				error = EINVAL;
2149163953Srrs				break;
2150163953Srrs			}
2151163953Srrs			/*
2152163953Srrs			 * I think passing the state is fine since
2153163953Srrs			 * sctp_constants.h will be available to the user
2154163953Srrs			 * land.
2155163953Srrs			 */
2156163953Srrs			sstat->sstat_state = stcb->asoc.state;
2157163953Srrs			sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2158163953Srrs			sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2159163953Srrs			/*
2160163953Srrs			 * We can't include chunks that have been passed to
2161163953Srrs			 * the socket layer. Only things in queue.
2162163953Srrs			 */
2163163953Srrs			sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2164163953Srrs			    stcb->asoc.cnt_on_all_streams);
2165163953Srrs
2166163953Srrs
2167163953Srrs			sstat->sstat_instrms = stcb->asoc.streamincnt;
2168163953Srrs			sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2169163953Srrs			sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2170163953Srrs			memcpy(&sstat->sstat_primary.spinfo_address,
2171163953Srrs			    &stcb->asoc.primary_destination->ro._l_addr,
2172163953Srrs			    ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2173163953Srrs			net = stcb->asoc.primary_destination;
2174163953Srrs			((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2175163953Srrs			/*
2176163953Srrs			 * Again the user can get info from sctp_constants.h
2177163953Srrs			 * for what the state of the network is.
2178163953Srrs			 */
2179163953Srrs			sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2180163953Srrs			sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2181163953Srrs			sstat->sstat_primary.spinfo_srtt = net->lastsa;
2182163953Srrs			sstat->sstat_primary.spinfo_rto = net->RTO;
2183163953Srrs			sstat->sstat_primary.spinfo_mtu = net->mtu;
2184163953Srrs			sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2185163953Srrs			SCTP_TCB_UNLOCK(stcb);
2186166675Srrs			*optsize = sizeof(*sstat);
2187163953Srrs		}
2188163953Srrs		break;
2189163953Srrs	case SCTP_RTOINFO:
2190163953Srrs		{
2191163953Srrs			struct sctp_rtoinfo *srto;
2192163953Srrs
2193166675Srrs			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2194166675Srrs			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2195166675Srrs
2196166675Srrs			if (stcb) {
2197166675Srrs				srto->srto_initial = stcb->asoc.initial_rto;
2198166675Srrs				srto->srto_max = stcb->asoc.maxrto;
2199166675Srrs				srto->srto_min = stcb->asoc.minrto;
2200166675Srrs				SCTP_TCB_UNLOCK(stcb);
2201166675Srrs			} else {
2202163953Srrs				SCTP_INP_RLOCK(inp);
2203163953Srrs				srto->srto_initial = inp->sctp_ep.initial_rto;
2204163953Srrs				srto->srto_max = inp->sctp_ep.sctp_maxrto;
2205163953Srrs				srto->srto_min = inp->sctp_ep.sctp_minrto;
2206163953Srrs				SCTP_INP_RUNLOCK(inp);
2207163953Srrs			}
2208166675Srrs			*optsize = sizeof(*srto);
2209163953Srrs		}
2210163953Srrs		break;
2211163953Srrs	case SCTP_ASSOCINFO:
2212163953Srrs		{
2213163953Srrs			struct sctp_assocparams *sasoc;
2214163953Srrs
2215166675Srrs			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2216166675Srrs			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2217163953Srrs
2218163953Srrs			if (stcb) {
2219163953Srrs				sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2220163953Srrs				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2221163953Srrs				sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2222163953Srrs				sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2223169655Srrs				sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2224163953Srrs				SCTP_TCB_UNLOCK(stcb);
2225163953Srrs			} else {
2226163953Srrs				SCTP_INP_RLOCK(inp);
2227163953Srrs				sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2228163953Srrs				sasoc->sasoc_number_peer_destinations = 0;
2229163953Srrs				sasoc->sasoc_peer_rwnd = 0;
2230163953Srrs				sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2231169655Srrs				sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2232163953Srrs				SCTP_INP_RUNLOCK(inp);
2233163953Srrs			}
2234166675Srrs			*optsize = sizeof(*sasoc);
2235163953Srrs		}
2236163953Srrs		break;
2237163953Srrs	case SCTP_DEFAULT_SEND_PARAM:
2238163953Srrs		{
2239163953Srrs			struct sctp_sndrcvinfo *s_info;
2240163953Srrs
2241166675Srrs			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2242166675Srrs			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2243166675Srrs
2244166675Srrs			if (stcb) {
2245170056Srrs				memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2246166675Srrs				SCTP_TCB_UNLOCK(stcb);
2247166675Srrs			} else {
2248163953Srrs				SCTP_INP_RLOCK(inp);
2249170056Srrs				memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2250163953Srrs				SCTP_INP_RUNLOCK(inp);
2251163953Srrs			}
2252166675Srrs			*optsize = sizeof(*s_info);
2253163953Srrs		}
2254163953Srrs		break;
2255163953Srrs	case SCTP_INITMSG:
2256163953Srrs		{
2257163953Srrs			struct sctp_initmsg *sinit;
2258163953Srrs
2259166675Srrs			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2260163953Srrs			SCTP_INP_RLOCK(inp);
2261163953Srrs			sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2262163953Srrs			sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2263163953Srrs			sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2264163953Srrs			sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2265163953Srrs			SCTP_INP_RUNLOCK(inp);
2266166675Srrs			*optsize = sizeof(*sinit);
2267163953Srrs		}
2268163953Srrs		break;
2269163953Srrs	case SCTP_PRIMARY_ADDR:
2270163953Srrs		/* we allow a "get" operation on this */
2271163953Srrs		{
2272163953Srrs			struct sctp_setprim *ssp;
2273163953Srrs
2274166675Srrs			SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2275166675Srrs			SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2276166675Srrs
2277166675Srrs			if (stcb) {
2278166675Srrs				/* simply copy out the sockaddr_storage... */
2279170056Srrs				int len;
2280170056Srrs
2281170056Srrs				len = *optsize;
2282170056Srrs				if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2283170056Srrs					len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2284170056Srrs
2285170056Srrs				memcpy(&ssp->ssp_addr,
2286170056Srrs				    &stcb->asoc.primary_destination->ro._l_addr,
2287170056Srrs				    len);
2288166675Srrs				SCTP_TCB_UNLOCK(stcb);
2289166675Srrs			} else {
2290163953Srrs				error = EINVAL;
2291163953Srrs			}
2292166675Srrs			*optsize = sizeof(*ssp);
2293163953Srrs		}
2294163953Srrs		break;
2295163953Srrs
2296163953Srrs	case SCTP_HMAC_IDENT:
2297163953Srrs		{
2298163953Srrs			struct sctp_hmacalgo *shmac;
2299163953Srrs			sctp_hmaclist_t *hmaclist;
2300163953Srrs			uint32_t size;
2301163953Srrs			int i;
2302163953Srrs
2303166675Srrs			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2304166675Srrs
2305163953Srrs			SCTP_INP_RLOCK(inp);
2306163953Srrs			hmaclist = inp->sctp_ep.local_hmacs;
2307163953Srrs			if (hmaclist == NULL) {
2308163953Srrs				/* no HMACs to return */
2309166675Srrs				*optsize = sizeof(*shmac);
2310168299Srrs				SCTP_INP_RUNLOCK(inp);
2311163953Srrs				break;
2312163953Srrs			}
2313163953Srrs			/* is there room for all of the hmac ids? */
2314163953Srrs			size = sizeof(*shmac) + (hmaclist->num_algo *
2315163953Srrs			    sizeof(shmac->shmac_idents[0]));
2316166675Srrs			if ((size_t)(*optsize) < size) {
2317163953Srrs				error = EINVAL;
2318163953Srrs				SCTP_INP_RUNLOCK(inp);
2319163953Srrs				break;
2320163953Srrs			}
2321163953Srrs			/* copy in the list */
2322163953Srrs			for (i = 0; i < hmaclist->num_algo; i++)
2323163953Srrs				shmac->shmac_idents[i] = hmaclist->hmac[i];
2324163953Srrs			SCTP_INP_RUNLOCK(inp);
2325166675Srrs			*optsize = size;
2326163953Srrs			break;
2327163953Srrs		}
2328163953Srrs	case SCTP_AUTH_ACTIVE_KEY:
2329163953Srrs		{
2330163953Srrs			struct sctp_authkeyid *scact;
2331163953Srrs
2332166675Srrs			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2333166675Srrs			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2334166675Srrs
2335166675Srrs			if (stcb) {
2336163953Srrs				/* get the active key on the assoc */
2337163953Srrs				scact->scact_keynumber = stcb->asoc.authinfo.assoc_keyid;
2338163953Srrs				SCTP_TCB_UNLOCK(stcb);
2339163953Srrs			} else {
2340163953Srrs				/* get the endpoint active key */
2341163953Srrs				SCTP_INP_RLOCK(inp);
2342163953Srrs				scact->scact_keynumber = inp->sctp_ep.default_keyid;
2343163953Srrs				SCTP_INP_RUNLOCK(inp);
2344163953Srrs			}
2345166675Srrs			*optsize = sizeof(*scact);
2346163953Srrs			break;
2347163953Srrs		}
2348163953Srrs	case SCTP_LOCAL_AUTH_CHUNKS:
2349163953Srrs		{
2350163953Srrs			struct sctp_authchunks *sac;
2351163953Srrs			sctp_auth_chklist_t *chklist = NULL;
2352166675Srrs			size_t size = 0;
2353163953Srrs
2354166675Srrs			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2355166675Srrs			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2356166675Srrs
2357166675Srrs			if (stcb) {
2358163953Srrs				/* get off the assoc */
2359163953Srrs				chklist = stcb->asoc.local_auth_chunks;
2360163953Srrs				/* is there enough space? */
2361163953Srrs				size = sctp_auth_get_chklist_size(chklist);
2362166675Srrs				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2363163953Srrs					error = EINVAL;
2364166675Srrs				} else {
2365166675Srrs					/* copy in the chunks */
2366169420Srrs					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2367163953Srrs				}
2368163953Srrs				SCTP_TCB_UNLOCK(stcb);
2369163953Srrs			} else {
2370163953Srrs				/* get off the endpoint */
2371163953Srrs				SCTP_INP_RLOCK(inp);
2372163953Srrs				chklist = inp->sctp_ep.local_auth_chunks;
2373163953Srrs				/* is there enough space? */
2374163953Srrs				size = sctp_auth_get_chklist_size(chklist);
2375166675Srrs				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2376163953Srrs					error = EINVAL;
2377166675Srrs				} else {
2378166675Srrs					/* copy in the chunks */
2379169420Srrs					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2380163953Srrs				}
2381163953Srrs				SCTP_INP_RUNLOCK(inp);
2382163953Srrs			}
2383166675Srrs			*optsize = sizeof(struct sctp_authchunks) + size;
2384163953Srrs			break;
2385163953Srrs		}
2386163953Srrs	case SCTP_PEER_AUTH_CHUNKS:
2387163953Srrs		{
2388163953Srrs			struct sctp_authchunks *sac;
2389163953Srrs			sctp_auth_chklist_t *chklist = NULL;
2390166675Srrs			size_t size = 0;
2391163953Srrs
2392166675Srrs			SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2393166675Srrs			SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2394166675Srrs
2395166675Srrs			if (stcb) {
2396166675Srrs				/* get off the assoc */
2397166675Srrs				chklist = stcb->asoc.peer_auth_chunks;
2398166675Srrs				/* is there enough space? */
2399166675Srrs				size = sctp_auth_get_chklist_size(chklist);
2400166675Srrs				if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2401166675Srrs					error = EINVAL;
2402166675Srrs				} else {
2403166675Srrs					/* copy in the chunks */
2404169420Srrs					(void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2405166675Srrs				}
2406166675Srrs				SCTP_TCB_UNLOCK(stcb);
2407166675Srrs			} else {
2408163953Srrs				error = ENOENT;
2409163953Srrs			}
2410166675Srrs			*optsize = sizeof(struct sctp_authchunks) + size;
2411163953Srrs			break;
2412163953Srrs		}
2413163953Srrs
2414163953Srrs
2415163953Srrs	default:
2416163953Srrs		error = ENOPROTOOPT;
2417166675Srrs		*optsize = 0;
2418163953Srrs		break;
2419163953Srrs	}			/* end switch (sopt->sopt_name) */
2420163953Srrs	return (error);
2421163953Srrs}
2422163953Srrs
2423163953Srrsstatic int
2424166675Srrssctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2425166675Srrs    void *p)
2426163953Srrs{
2427166675Srrs	int error, set_opt;
2428166675Srrs	uint32_t *mopt;
2429163953Srrs	struct sctp_tcb *stcb = NULL;
2430163953Srrs	struct sctp_inpcb *inp;
2431167598Srrs	uint32_t vrf_id;
2432163953Srrs
2433166675Srrs	if (optval == NULL) {
2434169420Srrs		SCTP_PRINTF("optval is NULL\n");
2435163953Srrs		return (EINVAL);
2436163953Srrs	}
2437163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
2438167598Srrs	if (inp == 0) {
2439169420Srrs		SCTP_PRINTF("inp is NULL?\n");
2440163953Srrs		return EINVAL;
2441167598Srrs	}
2442168299Srrs	vrf_id = inp->def_vrf_id;
2443163953Srrs
2444163953Srrs	error = 0;
2445166675Srrs	switch (optname) {
2446163953Srrs	case SCTP_NODELAY:
2447163953Srrs	case SCTP_AUTOCLOSE:
2448163953Srrs	case SCTP_AUTO_ASCONF:
2449163953Srrs	case SCTP_EXPLICIT_EOR:
2450163953Srrs	case SCTP_DISABLE_FRAGMENTS:
2451163953Srrs	case SCTP_USE_EXT_RCVINFO:
2452163953Srrs	case SCTP_I_WANT_MAPPED_V4_ADDR:
2453163953Srrs		/* copy in the option value */
2454166675Srrs		SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2455163953Srrs		set_opt = 0;
2456163953Srrs		if (error)
2457163953Srrs			break;
2458166675Srrs		switch (optname) {
2459163953Srrs		case SCTP_DISABLE_FRAGMENTS:
2460163953Srrs			set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2461163953Srrs			break;
2462163953Srrs		case SCTP_AUTO_ASCONF:
2463163953Srrs			set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2464163953Srrs			break;
2465163953Srrs		case SCTP_EXPLICIT_EOR:
2466163953Srrs			set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2467163953Srrs			break;
2468163953Srrs		case SCTP_USE_EXT_RCVINFO:
2469163953Srrs			set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2470163953Srrs			break;
2471163953Srrs		case SCTP_I_WANT_MAPPED_V4_ADDR:
2472163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2473163953Srrs				set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2474163953Srrs			} else {
2475163953Srrs				return (EINVAL);
2476163953Srrs			}
2477163953Srrs			break;
2478163953Srrs		case SCTP_NODELAY:
2479163953Srrs			set_opt = SCTP_PCB_FLAGS_NODELAY;
2480163953Srrs			break;
2481163953Srrs		case SCTP_AUTOCLOSE:
2482170056Srrs			if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2483170056Srrs			    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2484170056Srrs				return (EINVAL);
2485170056Srrs			}
2486163953Srrs			set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2487163953Srrs			/*
2488163953Srrs			 * The value is in ticks. Note this does not effect
2489163953Srrs			 * old associations, only new ones.
2490163953Srrs			 */
2491163953Srrs			inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2492163953Srrs			break;
2493163953Srrs		}
2494163953Srrs		SCTP_INP_WLOCK(inp);
2495163953Srrs		if (*mopt != 0) {
2496163953Srrs			sctp_feature_on(inp, set_opt);
2497163953Srrs		} else {
2498163953Srrs			sctp_feature_off(inp, set_opt);
2499163953Srrs		}
2500163953Srrs		SCTP_INP_WUNLOCK(inp);
2501163953Srrs		break;
2502163953Srrs	case SCTP_PARTIAL_DELIVERY_POINT:
2503163953Srrs		{
2504166675Srrs			uint32_t *value;
2505166675Srrs
2506166675Srrs			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2507167736Srrs			if (*value > SCTP_SB_LIMIT_RCV(so)) {
2508167736Srrs				error = EINVAL;
2509167736Srrs				break;
2510167736Srrs			}
2511166675Srrs			inp->partial_delivery_point = *value;
2512163953Srrs		}
2513163953Srrs		break;
2514163953Srrs	case SCTP_FRAGMENT_INTERLEAVE:
2515163953Srrs		/* not yet until we re-write sctp_recvmsg() */
2516163953Srrs		{
2517168943Srrs			uint32_t *level;
2518163953Srrs
2519168943Srrs			SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2520168943Srrs			if (*level == SCTP_FRAG_LEVEL_2) {
2521163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2522168943Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2523168943Srrs			} else if (*level == SCTP_FRAG_LEVEL_1) {
2524168943Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2525168943Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2526168943Srrs			} else if (*level == SCTP_FRAG_LEVEL_0) {
2527170056Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2528168943Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2529168943Srrs
2530163953Srrs			} else {
2531168943Srrs				error = EINVAL;
2532163953Srrs			}
2533163953Srrs		}
2534163953Srrs		break;
2535163953Srrs	case SCTP_CMT_ON_OFF:
2536163953Srrs		{
2537163953Srrs			struct sctp_assoc_value *av;
2538163953Srrs
2539166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2540166675Srrs			if (sctp_cmt_on_off) {
2541166675Srrs				SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2542166675Srrs				if (stcb) {
2543163953Srrs					stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2544166675Srrs					SCTP_TCB_UNLOCK(stcb);
2545163953Srrs				} else {
2546166675Srrs					error = ENOTCONN;
2547163953Srrs				}
2548166675Srrs			} else {
2549166675Srrs				error = ENOPROTOOPT;
2550163953Srrs			}
2551163953Srrs		}
2552163953Srrs		break;
2553171440Srrs		/* JRS - Set socket option for pluggable congestion control */
2554171440Srrs	case SCTP_PLUGGABLE_CC:
2555171440Srrs		{
2556171440Srrs			struct sctp_assoc_value *av;
2557171440Srrs
2558171440Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2559171440Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2560171440Srrs			if (stcb) {
2561171440Srrs				switch (av->assoc_value) {
2562171440Srrs					/*
2563171440Srrs					 * JRS - Standard TCP congestion
2564171440Srrs					 * control
2565171440Srrs					 */
2566171440Srrs				case SCTP_CC_RFC2581:
2567171440Srrs					{
2568171440Srrs						stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
2569171440Srrs						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2570171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
2571171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
2572171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2573171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2574171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2575171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2576171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2577171440Srrs						SCTP_TCB_UNLOCK(stcb);
2578171440Srrs						break;
2579171440Srrs					}
2580171440Srrs					/*
2581171440Srrs					 * JRS - High Speed TCP congestion
2582171440Srrs					 * control (Floyd)
2583171440Srrs					 */
2584171440Srrs				case SCTP_CC_HSTCP:
2585171440Srrs					{
2586171440Srrs						stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
2587171440Srrs						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2588171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
2589171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
2590171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2591171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2592171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2593171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2594171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2595171440Srrs						SCTP_TCB_UNLOCK(stcb);
2596171440Srrs						break;
2597171440Srrs					}
2598171440Srrs					/* JRS - HTCP congestion control */
2599171440Srrs				case SCTP_CC_HTCP:
2600171440Srrs					{
2601171440Srrs						stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
2602171440Srrs						stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
2603171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
2604171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
2605171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
2606171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
2607171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2608171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2609171440Srrs						stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
2610171440Srrs						SCTP_TCB_UNLOCK(stcb);
2611171440Srrs						break;
2612171440Srrs					}
2613171440Srrs					/*
2614171440Srrs					 * JRS - All other values are
2615171440Srrs					 * invalid
2616171440Srrs					 */
2617171440Srrs				default:
2618171440Srrs					{
2619171440Srrs						error = EINVAL;
2620171440Srrs						SCTP_TCB_UNLOCK(stcb);
2621171440Srrs						break;
2622171440Srrs					}
2623171440Srrs				}
2624171440Srrs			} else {
2625171440Srrs				switch (av->assoc_value) {
2626171440Srrs				case SCTP_CC_RFC2581:
2627171440Srrs				case SCTP_CC_HSTCP:
2628171440Srrs				case SCTP_CC_HTCP:
2629171440Srrs					inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2630171440Srrs					break;
2631171440Srrs				default:
2632171440Srrs					error = EINVAL;
2633171440Srrs					break;
2634171440Srrs				};
2635171440Srrs			}
2636171440Srrs		}
2637171440Srrs		break;
2638163953Srrs	case SCTP_CLR_STAT_LOG:
2639163953Srrs		error = EOPNOTSUPP;
2640163953Srrs		break;
2641163953Srrs	case SCTP_CONTEXT:
2642163953Srrs		{
2643163953Srrs			struct sctp_assoc_value *av;
2644163953Srrs
2645166675Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2646166675Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2647166675Srrs
2648166675Srrs			if (stcb) {
2649166675Srrs				stcb->asoc.context = av->assoc_value;
2650166675Srrs				SCTP_TCB_UNLOCK(stcb);
2651163953Srrs			} else {
2652166675Srrs				SCTP_INP_WLOCK(inp);
2653163953Srrs				inp->sctp_context = av->assoc_value;
2654166675Srrs				SCTP_INP_WUNLOCK(inp);
2655163953Srrs			}
2656163953Srrs		}
2657163953Srrs		break;
2658167598Srrs	case SCTP_VRF_ID:
2659167598Srrs		{
2660170056Srrs			uint32_t *default_vrfid;
2661167598Srrs
2662170056Srrs			SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2663170056Srrs			if (*default_vrfid > SCTP_MAX_VRF_ID) {
2664167598Srrs				error = EINVAL;
2665167598Srrs				break;
2666167598Srrs			}
2667170056Srrs			inp->def_vrf_id = *default_vrfid;
2668167598Srrs			break;
2669167598Srrs		}
2670167598Srrs	case SCTP_DEL_VRF_ID:
2671167598Srrs		{
2672167598Srrs			error = EOPNOTSUPP;
2673167598Srrs			break;
2674167598Srrs		}
2675167598Srrs	case SCTP_ADD_VRF_ID:
2676167598Srrs		{
2677167598Srrs			error = EOPNOTSUPP;
2678167598Srrs			break;
2679167598Srrs		}
2680170056Srrs	case SCTP_DELAYED_SACK:
2681163953Srrs		{
2682170056Srrs			struct sctp_sack_info *sack;
2683163953Srrs
2684170056Srrs			SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
2685170056Srrs			SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2686166675Srrs			if (stcb) {
2687170056Srrs				if (sack->sack_delay) {
2688170056Srrs					if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2689170056Srrs						sack->sack_delay = TICKS_TO_MSEC(1);
2690170056Srrs					}
2691170056Srrs					stcb->asoc.delayed_ack = sack->sack_delay;
2692170056Srrs				}
2693170056Srrs				if (sack->sack_freq) {
2694170056Srrs					stcb->asoc.sack_freq = sack->sack_freq;
2695170056Srrs				}
2696166675Srrs				SCTP_TCB_UNLOCK(stcb);
2697166675Srrs			} else {
2698163953Srrs				SCTP_INP_WLOCK(inp);
2699170056Srrs				if (sack->sack_delay) {
2700170056Srrs					if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2701170056Srrs						sack->sack_delay = TICKS_TO_MSEC(1);
2702170056Srrs					}
2703170056Srrs					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
2704170056Srrs				}
2705170056Srrs				if (sack->sack_freq) {
2706170056Srrs					inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
2707170056Srrs				}
2708163953Srrs				SCTP_INP_WUNLOCK(inp);
2709163953Srrs			}
2710166675Srrs			break;
2711163953Srrs		}
2712163953Srrs	case SCTP_AUTH_CHUNK:
2713163953Srrs		{
2714163953Srrs			struct sctp_authchunk *sauth;
2715163953Srrs
2716166675Srrs			SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
2717166675Srrs
2718166675Srrs			SCTP_INP_WLOCK(inp);
2719166675Srrs			if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks))
2720163953Srrs				error = EINVAL;
2721166675Srrs			SCTP_INP_WUNLOCK(inp);
2722163953Srrs			break;
2723163953Srrs		}
2724163953Srrs	case SCTP_AUTH_KEY:
2725163953Srrs		{
2726163953Srrs			struct sctp_authkey *sca;
2727163953Srrs			struct sctp_keyhead *shared_keys;
2728163953Srrs			sctp_sharedkey_t *shared_key;
2729163953Srrs			sctp_key_t *key = NULL;
2730166675Srrs			size_t size;
2731163953Srrs
2732166675Srrs			SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
2733169420Srrs			SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
2734169420Srrs			size = optsize - sizeof(*sca);
2735166675Srrs
2736166675Srrs			if (stcb) {
2737163953Srrs				/* set it on the assoc */
2738163953Srrs				shared_keys = &stcb->asoc.shared_keys;
2739163953Srrs				/* clear the cached keys for this key id */
2740163953Srrs				sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
2741163953Srrs				/*
2742163953Srrs				 * create the new shared key and
2743163953Srrs				 * insert/replace it
2744163953Srrs				 */
2745163953Srrs				if (size > 0) {
2746163953Srrs					key = sctp_set_key(sca->sca_key, (uint32_t) size);
2747163953Srrs					if (key == NULL) {
2748163953Srrs						error = ENOMEM;
2749163953Srrs						SCTP_TCB_UNLOCK(stcb);
2750163953Srrs						break;
2751163953Srrs					}
2752163953Srrs				}
2753163953Srrs				shared_key = sctp_alloc_sharedkey();
2754163953Srrs				if (shared_key == NULL) {
2755163953Srrs					sctp_free_key(key);
2756163953Srrs					error = ENOMEM;
2757163953Srrs					SCTP_TCB_UNLOCK(stcb);
2758163953Srrs					break;
2759163953Srrs				}
2760163953Srrs				shared_key->key = key;
2761163953Srrs				shared_key->keyid = sca->sca_keynumber;
2762163953Srrs				sctp_insert_sharedkey(shared_keys, shared_key);
2763163953Srrs				SCTP_TCB_UNLOCK(stcb);
2764163953Srrs			} else {
2765166675Srrs				/* set it on the endpoint */
2766163953Srrs				SCTP_INP_WLOCK(inp);
2767163953Srrs				shared_keys = &inp->sctp_ep.shared_keys;
2768163953Srrs				/*
2769163953Srrs				 * clear the cached keys on all assocs for
2770163953Srrs				 * this key id
2771163953Srrs				 */
2772163953Srrs				sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
2773163953Srrs				/*
2774163953Srrs				 * create the new shared key and
2775163953Srrs				 * insert/replace it
2776163953Srrs				 */
2777163953Srrs				if (size > 0) {
2778163953Srrs					key = sctp_set_key(sca->sca_key, (uint32_t) size);
2779163953Srrs					if (key == NULL) {
2780163953Srrs						error = ENOMEM;
2781163953Srrs						SCTP_INP_WUNLOCK(inp);
2782163953Srrs						break;
2783163953Srrs					}
2784163953Srrs				}
2785163953Srrs				shared_key = sctp_alloc_sharedkey();
2786163953Srrs				if (shared_key == NULL) {
2787163953Srrs					sctp_free_key(key);
2788163953Srrs					error = ENOMEM;
2789163953Srrs					SCTP_INP_WUNLOCK(inp);
2790163953Srrs					break;
2791163953Srrs				}
2792163953Srrs				shared_key->key = key;
2793163953Srrs				shared_key->keyid = sca->sca_keynumber;
2794163953Srrs				sctp_insert_sharedkey(shared_keys, shared_key);
2795163953Srrs				SCTP_INP_WUNLOCK(inp);
2796163953Srrs			}
2797163953Srrs			break;
2798163953Srrs		}
2799163953Srrs	case SCTP_HMAC_IDENT:
2800163953Srrs		{
2801163953Srrs			struct sctp_hmacalgo *shmac;
2802163953Srrs			sctp_hmaclist_t *hmaclist;
2803163953Srrs			uint32_t hmacid;
2804170056Srrs			size_t size, i, found;
2805163953Srrs
2806166675Srrs			SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
2807166675Srrs			size = (optsize - sizeof(*shmac)) / sizeof(shmac->shmac_idents[0]);
2808163953Srrs			hmaclist = sctp_alloc_hmaclist(size);
2809163953Srrs			if (hmaclist == NULL) {
2810163953Srrs				error = ENOMEM;
2811163953Srrs				break;
2812163953Srrs			}
2813163953Srrs			for (i = 0; i < size; i++) {
2814163953Srrs				hmacid = shmac->shmac_idents[i];
2815163953Srrs				if (sctp_auth_add_hmacid(hmaclist, (uint16_t) hmacid)) {
2816163953Srrs					 /* invalid HMACs were found */ ;
2817163953Srrs					error = EINVAL;
2818164085Srrs					sctp_free_hmaclist(hmaclist);
2819163953Srrs					goto sctp_set_hmac_done;
2820163953Srrs				}
2821163953Srrs			}
2822170056Srrs			found = 0;
2823170056Srrs			for (i = 0; i < hmaclist->num_algo; i++) {
2824170056Srrs				if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
2825170056Srrs					/* already in list */
2826170056Srrs					found = 1;
2827170056Srrs				}
2828170056Srrs			}
2829170056Srrs			if (!found) {
2830170056Srrs				sctp_free_hmaclist(hmaclist);
2831170056Srrs				error = EINVAL;
2832170056Srrs				break;
2833170056Srrs			}
2834163953Srrs			/* set it on the endpoint */
2835163953Srrs			SCTP_INP_WLOCK(inp);
2836163953Srrs			if (inp->sctp_ep.local_hmacs)
2837163953Srrs				sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2838163953Srrs			inp->sctp_ep.local_hmacs = hmaclist;
2839163953Srrs			SCTP_INP_WUNLOCK(inp);
2840163953Srrs	sctp_set_hmac_done:
2841163953Srrs			break;
2842163953Srrs		}
2843163953Srrs	case SCTP_AUTH_ACTIVE_KEY:
2844163953Srrs		{
2845163953Srrs			struct sctp_authkeyid *scact;
2846163953Srrs
2847166675Srrs			SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
2848166675Srrs			SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2849166675Srrs
2850163953Srrs			/* set the active key on the right place */
2851166675Srrs			if (stcb) {
2852163953Srrs				/* set the active key on the assoc */
2853163953Srrs				if (sctp_auth_setactivekey(stcb, scact->scact_keynumber))
2854163953Srrs					error = EINVAL;
2855163953Srrs				SCTP_TCB_UNLOCK(stcb);
2856163953Srrs			} else {
2857163953Srrs				/* set the active key on the endpoint */
2858163953Srrs				SCTP_INP_WLOCK(inp);
2859163953Srrs				if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber))
2860163953Srrs					error = EINVAL;
2861163953Srrs				SCTP_INP_WUNLOCK(inp);
2862163953Srrs			}
2863163953Srrs			break;
2864163953Srrs		}
2865163953Srrs	case SCTP_AUTH_DELETE_KEY:
2866163953Srrs		{
2867163953Srrs			struct sctp_authkeyid *scdel;
2868163953Srrs
2869166675Srrs			SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
2870166675Srrs			SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
2871166675Srrs
2872163953Srrs			/* delete the key from the right place */
2873166675Srrs			if (stcb) {
2874163953Srrs				if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber))
2875163953Srrs					error = EINVAL;
2876163953Srrs				SCTP_TCB_UNLOCK(stcb);
2877163953Srrs			} else {
2878163953Srrs				SCTP_INP_WLOCK(inp);
2879163953Srrs				if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber))
2880163953Srrs					error = EINVAL;
2881163953Srrs				SCTP_INP_WUNLOCK(inp);
2882163953Srrs			}
2883163953Srrs			break;
2884163953Srrs		}
2885163953Srrs
2886163953Srrs	case SCTP_RESET_STREAMS:
2887163953Srrs		{
2888163953Srrs			struct sctp_stream_reset *strrst;
2889163953Srrs			uint8_t send_in = 0, send_tsn = 0, send_out = 0;
2890163953Srrs			int i;
2891163953Srrs
2892166675Srrs			SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
2893166675Srrs			SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
2894163953Srrs
2895163953Srrs			if (stcb == NULL) {
2896163953Srrs				error = ENOENT;
2897163953Srrs				break;
2898163953Srrs			}
2899163953Srrs			if (stcb->asoc.peer_supports_strreset == 0) {
2900163953Srrs				/*
2901163953Srrs				 * Peer does not support it, we return
2902163953Srrs				 * protocol not supported since this is true
2903163953Srrs				 * for this feature and this peer, not the
2904163953Srrs				 * socket request in general.
2905163953Srrs				 */
2906163953Srrs				error = EPROTONOSUPPORT;
2907163953Srrs				SCTP_TCB_UNLOCK(stcb);
2908163953Srrs				break;
2909163953Srrs			}
2910163953Srrs			if (stcb->asoc.stream_reset_outstanding) {
2911163953Srrs				error = EALREADY;
2912163953Srrs				SCTP_TCB_UNLOCK(stcb);
2913163953Srrs				break;
2914163953Srrs			}
2915163953Srrs			if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2916163953Srrs				send_in = 1;
2917163953Srrs			} else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2918163953Srrs				send_out = 1;
2919163953Srrs			} else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2920163953Srrs				send_in = 1;
2921163953Srrs				send_out = 1;
2922163953Srrs			} else if (strrst->strrst_flags == SCTP_RESET_TSN) {
2923163953Srrs				send_tsn = 1;
2924163953Srrs			} else {
2925163953Srrs				error = EINVAL;
2926163953Srrs				SCTP_TCB_UNLOCK(stcb);
2927163953Srrs				break;
2928163953Srrs			}
2929163953Srrs			for (i = 0; i < strrst->strrst_num_streams; i++) {
2930163953Srrs				if ((send_in) &&
2931163953Srrs
2932163953Srrs				    (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
2933163953Srrs					error = EINVAL;
2934163953Srrs					goto get_out;
2935163953Srrs				}
2936163953Srrs				if ((send_out) &&
2937163953Srrs				    (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
2938163953Srrs					error = EINVAL;
2939163953Srrs					goto get_out;
2940163953Srrs				}
2941163953Srrs			}
2942163953Srrs			if (error) {
2943163953Srrs		get_out:
2944163953Srrs				SCTP_TCB_UNLOCK(stcb);
2945163953Srrs				break;
2946163953Srrs			}
2947163953Srrs			error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2948163953Srrs			    strrst->strrst_list,
2949163953Srrs			    send_out, (stcb->asoc.str_reset_seq_in - 3),
2950163953Srrs			    send_in, send_tsn);
2951163953Srrs
2952163953Srrs			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ);
2953163953Srrs			SCTP_TCB_UNLOCK(stcb);
2954163953Srrs		}
2955163953Srrs		break;
2956166675Srrs
2957163953Srrs	case SCTP_CONNECT_X:
2958166675Srrs		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2959163953Srrs			error = EINVAL;
2960163953Srrs			break;
2961163953Srrs		}
2962166675Srrs		error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
2963163953Srrs		break;
2964163953Srrs
2965163953Srrs	case SCTP_CONNECT_X_DELAYED:
2966166675Srrs		if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2967163953Srrs			error = EINVAL;
2968163953Srrs			break;
2969163953Srrs		}
2970166675Srrs		error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
2971163953Srrs		break;
2972163953Srrs
2973163953Srrs	case SCTP_CONNECT_X_COMPLETE:
2974163953Srrs		{
2975163953Srrs			struct sockaddr *sa;
2976163953Srrs			struct sctp_nets *net;
2977163953Srrs
2978166675Srrs			/* FIXME MT: check correct? */
2979166675Srrs			SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
2980166675Srrs
2981163953Srrs			/* find tcb */
2982163953Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2983163953Srrs				SCTP_INP_RLOCK(inp);
2984163953Srrs				stcb = LIST_FIRST(&inp->sctp_asoc_list);
2985163953Srrs				if (stcb) {
2986163953Srrs					SCTP_TCB_LOCK(stcb);
2987163953Srrs					net = sctp_findnet(stcb, sa);
2988163953Srrs				}
2989163953Srrs				SCTP_INP_RUNLOCK(inp);
2990163953Srrs			} else {
2991166675Srrs				/*
2992166675Srrs				 * We increment here since
2993166675Srrs				 * sctp_findassociation_ep_addr() wil do a
2994166675Srrs				 * decrement if it finds the stcb as long as
2995166675Srrs				 * the locked tcb (last argument) is NOT a
2996166675Srrs				 * TCB.. aka NULL.
2997166675Srrs				 */
2998163953Srrs				SCTP_INP_INCR_REF(inp);
2999163953Srrs				stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3000163953Srrs				if (stcb == NULL) {
3001163953Srrs					SCTP_INP_DECR_REF(inp);
3002163953Srrs				}
3003163953Srrs			}
3004163953Srrs
3005163953Srrs			if (stcb == NULL) {
3006163953Srrs				error = ENOENT;
3007163953Srrs				break;
3008163953Srrs			}
3009163953Srrs			if (stcb->asoc.delayed_connection == 1) {
3010163953Srrs				stcb->asoc.delayed_connection = 0;
3011169378Srrs				(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3012165220Srrs				sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3013165220Srrs				    stcb->asoc.primary_destination,
3014165220Srrs				    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3015163953Srrs				sctp_send_initiate(inp, stcb);
3016163953Srrs			} else {
3017163953Srrs				/*
3018163953Srrs				 * already expired or did not use delayed
3019163953Srrs				 * connectx
3020163953Srrs				 */
3021163953Srrs				error = EALREADY;
3022163953Srrs			}
3023163953Srrs			SCTP_TCB_UNLOCK(stcb);
3024163953Srrs		}
3025163953Srrs		break;
3026170056Srrs	case SCTP_MAX_BURST:
3027163953Srrs		{
3028163953Srrs			uint8_t *burst;
3029163953Srrs
3030166675Srrs			SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3031166675Srrs
3032163953Srrs			SCTP_INP_WLOCK(inp);
3033163953Srrs			if (*burst) {
3034163953Srrs				inp->sctp_ep.max_burst = *burst;
3035163953Srrs			}
3036163953Srrs			SCTP_INP_WUNLOCK(inp);
3037163953Srrs		}
3038163953Srrs		break;
3039163953Srrs	case SCTP_MAXSEG:
3040163953Srrs		{
3041167598Srrs			struct sctp_assoc_value *av;
3042163953Srrs			int ovh;
3043163953Srrs
3044167598Srrs			SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3045167598Srrs			SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3046166675Srrs
3047170056Srrs			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3048170056Srrs				ovh = SCTP_MED_OVERHEAD;
3049170056Srrs			} else {
3050170056Srrs				ovh = SCTP_MED_V4_OVERHEAD;
3051170056Srrs			}
3052167598Srrs			if (stcb) {
3053170056Srrs				if (av->assoc_value) {
3054170056Srrs					stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3055170056Srrs				} else {
3056170056Srrs					stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3057170056Srrs				}
3058167598Srrs				SCTP_TCB_UNLOCK(stcb);
3059163953Srrs			} else {
3060167598Srrs				SCTP_INP_WLOCK(inp);
3061167598Srrs				/*
3062167598Srrs				 * FIXME MT: I think this is not in tune
3063167598Srrs				 * with the API ID
3064167598Srrs				 */
3065167598Srrs				if (av->assoc_value) {
3066167598Srrs					inp->sctp_frag_point = (av->assoc_value + ovh);
3067167598Srrs				} else {
3068170056Srrs					inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3069167598Srrs				}
3070167598Srrs				SCTP_INP_WUNLOCK(inp);
3071163953Srrs			}
3072163953Srrs		}
3073163953Srrs		break;
3074163953Srrs	case SCTP_EVENTS:
3075163953Srrs		{
3076163953Srrs			struct sctp_event_subscribe *events;
3077163953Srrs
3078166675Srrs			SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3079166675Srrs
3080163953Srrs			SCTP_INP_WLOCK(inp);
3081163953Srrs			if (events->sctp_data_io_event) {
3082163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3083163953Srrs			} else {
3084163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3085163953Srrs			}
3086163953Srrs
3087163953Srrs			if (events->sctp_association_event) {
3088163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3089163953Srrs			} else {
3090163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3091163953Srrs			}
3092163953Srrs
3093163953Srrs			if (events->sctp_address_event) {
3094163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3095163953Srrs			} else {
3096163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3097163953Srrs			}
3098163953Srrs
3099163953Srrs			if (events->sctp_send_failure_event) {
3100163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3101163953Srrs			} else {
3102163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3103163953Srrs			}
3104163953Srrs
3105163953Srrs			if (events->sctp_peer_error_event) {
3106163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3107163953Srrs			} else {
3108163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3109163953Srrs			}
3110163953Srrs
3111163953Srrs			if (events->sctp_shutdown_event) {
3112163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3113163953Srrs			} else {
3114163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3115163953Srrs			}
3116163953Srrs
3117163953Srrs			if (events->sctp_partial_delivery_event) {
3118163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3119163953Srrs			} else {
3120163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3121163953Srrs			}
3122163953Srrs
3123163953Srrs			if (events->sctp_adaptation_layer_event) {
3124163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3125163953Srrs			} else {
3126163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3127163953Srrs			}
3128163953Srrs
3129163953Srrs			if (events->sctp_authentication_event) {
3130163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3131163953Srrs			} else {
3132163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3133163953Srrs			}
3134163953Srrs
3135163953Srrs			if (events->sctp_stream_reset_events) {
3136163953Srrs				sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3137163953Srrs			} else {
3138163953Srrs				sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3139163953Srrs			}
3140163953Srrs			SCTP_INP_WUNLOCK(inp);
3141163953Srrs		}
3142163953Srrs		break;
3143163953Srrs
3144163953Srrs	case SCTP_ADAPTATION_LAYER:
3145163953Srrs		{
3146163953Srrs			struct sctp_setadaptation *adap_bits;
3147163953Srrs
3148166675Srrs			SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3149163953Srrs			SCTP_INP_WLOCK(inp);
3150163953Srrs			inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3151163953Srrs			SCTP_INP_WUNLOCK(inp);
3152163953Srrs		}
3153163953Srrs		break;
3154166675Srrs#ifdef SCTP_DEBUG
3155163953Srrs	case SCTP_SET_INITIAL_DBG_SEQ:
3156163953Srrs		{
3157163953Srrs			uint32_t *vvv;
3158163953Srrs
3159166675Srrs			SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3160163953Srrs			SCTP_INP_WLOCK(inp);
3161163953Srrs			inp->sctp_ep.initial_sequence_debug = *vvv;
3162163953Srrs			SCTP_INP_WUNLOCK(inp);
3163163953Srrs		}
3164163953Srrs		break;
3165166675Srrs#endif
3166163953Srrs	case SCTP_DEFAULT_SEND_PARAM:
3167163953Srrs		{
3168163953Srrs			struct sctp_sndrcvinfo *s_info;
3169163953Srrs
3170166675Srrs			SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3171166675Srrs			SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3172163953Srrs
3173166675Srrs			if (stcb) {
3174166675Srrs				if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3175170056Srrs					memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3176163953Srrs				} else {
3177166675Srrs					error = EINVAL;
3178163953Srrs				}
3179166675Srrs				SCTP_TCB_UNLOCK(stcb);
3180166675Srrs			} else {
3181166675Srrs				SCTP_INP_WLOCK(inp);
3182170056Srrs				memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3183166675Srrs				SCTP_INP_WUNLOCK(inp);
3184163953Srrs			}
3185163953Srrs		}
3186163953Srrs		break;
3187163953Srrs	case SCTP_PEER_ADDR_PARAMS:
3188163953Srrs		/* Applys to the specific association */
3189163953Srrs		{
3190163953Srrs			struct sctp_paddrparams *paddrp;
3191163953Srrs			struct sctp_nets *net;
3192163953Srrs
3193166675Srrs			SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3194166675Srrs			SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3195163953Srrs			net = NULL;
3196166675Srrs			if (stcb) {
3197166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3198166675Srrs			} else {
3199166675Srrs				/*
3200166675Srrs				 * We increment here since
3201166675Srrs				 * sctp_findassociation_ep_addr() wil do a
3202166675Srrs				 * decrement if it finds the stcb as long as
3203166675Srrs				 * the locked tcb (last argument) is NOT a
3204166675Srrs				 * TCB.. aka NULL.
3205166675Srrs				 */
3206166675Srrs				SCTP_INP_INCR_REF(inp);
3207166675Srrs				stcb = sctp_findassociation_ep_addr(&inp,
3208166675Srrs				    (struct sockaddr *)&paddrp->spp_address,
3209166675Srrs				    &net, NULL, NULL);
3210163953Srrs				if (stcb == NULL) {
3211166675Srrs					SCTP_INP_DECR_REF(inp);
3212163953Srrs				}
3213163953Srrs			}
3214170056Srrs			/* sanity checks */
3215170056Srrs			if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3216170056Srrs				if (stcb)
3217170056Srrs					SCTP_TCB_UNLOCK(stcb);
3218170056Srrs				return (EINVAL);
3219170056Srrs			}
3220170056Srrs			if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3221170056Srrs				if (stcb)
3222170056Srrs					SCTP_TCB_UNLOCK(stcb);
3223170056Srrs				return (EINVAL);
3224170056Srrs			}
3225163953Srrs			if (stcb) {
3226163953Srrs				/************************TCB SPECIFIC SET ******************/
3227163953Srrs				/*
3228163953Srrs				 * do we change the timer for HB, we run
3229163953Srrs				 * only one?
3230163953Srrs				 */
3231170056Srrs				int ovh = 0;
3232170056Srrs
3233170056Srrs				if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3234170056Srrs					ovh = SCTP_MED_OVERHEAD;
3235170056Srrs				} else {
3236170056Srrs					ovh = SCTP_MED_V4_OVERHEAD;
3237170056Srrs				}
3238170056Srrs
3239163953Srrs				if (paddrp->spp_hbinterval)
3240163953Srrs					stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3241163953Srrs				else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3242163953Srrs					stcb->asoc.heart_beat_delay = 0;
3243163953Srrs
3244163953Srrs				/* network sets ? */
3245163953Srrs				if (net) {
3246163953Srrs					/************************NET SPECIFIC SET ******************/
3247163953Srrs					if (paddrp->spp_flags & SPP_HB_DEMAND) {
3248163953Srrs						/* on demand HB */
3249171440Srrs						if (sctp_send_hb(stcb, 1, net) < 0) {
3250171440Srrs							/* asoc destroyed */
3251171440Srrs							error = EINVAL;
3252171440Srrs							break;
3253171440Srrs						}
3254163953Srrs					}
3255163953Srrs					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3256163953Srrs						net->dest_state |= SCTP_ADDR_NOHB;
3257163953Srrs					}
3258163953Srrs					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3259163953Srrs						net->dest_state &= ~SCTP_ADDR_NOHB;
3260163953Srrs					}
3261170056Srrs					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3262165647Srrs						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3263165220Srrs							sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3264165220Srrs							    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3265163953Srrs						}
3266163953Srrs						if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3267170056Srrs							net->mtu = paddrp->spp_pathmtu + ovh;
3268169352Srrs							if (net->mtu < stcb->asoc.smallest_mtu) {
3269169352Srrs#ifdef SCTP_PRINT_FOR_B_AND_M
3270169420Srrs								SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3271169352Srrs								    net->mtu);
3272169352Srrs#endif
3273167695Srrs								sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3274169352Srrs							}
3275163953Srrs						}
3276163953Srrs					}
3277163953Srrs					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3278165647Srrs						if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3279163953Srrs							sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3280163953Srrs						}
3281163953Srrs					}
3282163953Srrs					if (paddrp->spp_pathmaxrxt)
3283163953Srrs						net->failure_threshold = paddrp->spp_pathmaxrxt;
3284167598Srrs#ifdef INET
3285163953Srrs					if (paddrp->spp_flags & SPP_IPV4_TOS) {
3286163953Srrs						if (net->ro._l_addr.sin.sin_family == AF_INET) {
3287163953Srrs							net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3288163953Srrs						}
3289163953Srrs					}
3290163953Srrs#endif
3291167598Srrs#ifdef INET6
3292163953Srrs					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3293163953Srrs						if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3294163953Srrs							net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3295163953Srrs						}
3296163953Srrs					}
3297163953Srrs#endif
3298163953Srrs				} else {
3299163953Srrs					/************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3300163953Srrs					if (paddrp->spp_pathmaxrxt)
3301163953Srrs						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3302163953Srrs
3303163953Srrs					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3304163953Srrs						/* Turn back on the timer */
3305163953Srrs						stcb->asoc.hb_is_disabled = 0;
3306163953Srrs						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3307163953Srrs					}
3308170056Srrs					if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3309170056Srrs						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3310170056Srrs							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3311170056Srrs								sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3312170056Srrs								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3313170056Srrs							}
3314170056Srrs							if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3315170056Srrs								net->mtu = paddrp->spp_pathmtu + ovh;
3316170056Srrs								if (net->mtu < stcb->asoc.smallest_mtu) {
3317170056Srrs#ifdef SCTP_PRINT_FOR_B_AND_M
3318170056Srrs									SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3319170056Srrs									    net->mtu);
3320170056Srrs#endif
3321170056Srrs									sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3322170056Srrs								}
3323170056Srrs							}
3324170056Srrs						}
3325170056Srrs					}
3326170056Srrs					if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3327170056Srrs						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3328170056Srrs							if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3329170056Srrs								sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3330170056Srrs							}
3331170056Srrs						}
3332170056Srrs					}
3333163953Srrs					if (paddrp->spp_flags & SPP_HB_DISABLE) {
3334163953Srrs						int cnt_of_unconf = 0;
3335163953Srrs						struct sctp_nets *lnet;
3336163953Srrs
3337163953Srrs						stcb->asoc.hb_is_disabled = 1;
3338163953Srrs						TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3339163953Srrs							if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3340163953Srrs								cnt_of_unconf++;
3341163953Srrs							}
3342163953Srrs						}
3343163953Srrs						/*
3344163953Srrs						 * stop the timer ONLY if we
3345163953Srrs						 * have no unconfirmed
3346163953Srrs						 * addresses
3347163953Srrs						 */
3348163953Srrs						if (cnt_of_unconf == 0) {
3349170056Srrs							TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3350170056Srrs								sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3351170056Srrs								    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3352170056Srrs							}
3353163953Srrs						}
3354163953Srrs					}
3355163953Srrs					if (paddrp->spp_flags & SPP_HB_ENABLE) {
3356163953Srrs						/* start up the timer. */
3357170056Srrs						TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3358170056Srrs							sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3359170056Srrs						}
3360163953Srrs					}
3361167598Srrs#ifdef INET
3362163953Srrs					if (paddrp->spp_flags & SPP_IPV4_TOS)
3363163953Srrs						stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3364163953Srrs#endif
3365167598Srrs#ifdef INET6
3366163953Srrs					if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3367163953Srrs						stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3368163953Srrs#endif
3369163953Srrs
3370163953Srrs				}
3371163953Srrs				SCTP_TCB_UNLOCK(stcb);
3372163953Srrs			} else {
3373163953Srrs				/************************NO TCB, SET TO default stuff ******************/
3374163953Srrs				SCTP_INP_WLOCK(inp);
3375163953Srrs				/*
3376163953Srrs				 * For the TOS/FLOWLABEL stuff you set it
3377163953Srrs				 * with the options on the socket
3378163953Srrs				 */
3379163953Srrs				if (paddrp->spp_pathmaxrxt) {
3380163953Srrs					inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3381163953Srrs				}
3382170056Srrs				if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3383170056Srrs					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3384170056Srrs				else if (paddrp->spp_hbinterval)
3385170056Srrs					inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3386170056Srrs
3387163953Srrs				if (paddrp->spp_flags & SPP_HB_ENABLE) {
3388163953Srrs					sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3389170056Srrs
3390163953Srrs				} else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3391163953Srrs					sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3392163953Srrs				}
3393163953Srrs				SCTP_INP_WUNLOCK(inp);
3394163953Srrs			}
3395163953Srrs		}
3396163953Srrs		break;
3397163953Srrs	case SCTP_RTOINFO:
3398163953Srrs		{
3399163953Srrs			struct sctp_rtoinfo *srto;
3400169655Srrs			uint32_t new_init, new_min, new_max;
3401163953Srrs
3402166675Srrs			SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3403166675Srrs			SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3404166675Srrs
3405166675Srrs			if (stcb) {
3406167598Srrs				if (srto->srto_initial)
3407169655Srrs					new_init = srto->srto_initial;
3408169655Srrs				else
3409169655Srrs					new_init = stcb->asoc.initial_rto;
3410167598Srrs				if (srto->srto_max)
3411169655Srrs					new_max = srto->srto_max;
3412169655Srrs				else
3413169655Srrs					new_max = stcb->asoc.maxrto;
3414167598Srrs				if (srto->srto_min)
3415169655Srrs					new_min = srto->srto_min;
3416169655Srrs				else
3417169655Srrs					new_min = stcb->asoc.minrto;
3418169655Srrs				if ((new_min <= new_init) && (new_init <= new_max)) {
3419169655Srrs					stcb->asoc.initial_rto = new_init;
3420169655Srrs					stcb->asoc.maxrto = new_max;
3421169655Srrs					stcb->asoc.minrto = new_min;
3422169655Srrs				} else {
3423169655Srrs					error = EDOM;
3424169655Srrs				}
3425166675Srrs				SCTP_TCB_UNLOCK(stcb);
3426166675Srrs			} else {
3427163953Srrs				SCTP_INP_WLOCK(inp);
3428167598Srrs				if (srto->srto_initial)
3429169655Srrs					new_init = srto->srto_initial;
3430169655Srrs				else
3431169655Srrs					new_init = inp->sctp_ep.initial_rto;
3432167598Srrs				if (srto->srto_max)
3433169655Srrs					new_max = srto->srto_max;
3434169655Srrs				else
3435169655Srrs					new_max = inp->sctp_ep.sctp_maxrto;
3436167598Srrs				if (srto->srto_min)
3437169655Srrs					new_min = srto->srto_min;
3438169655Srrs				else
3439169655Srrs					new_min = inp->sctp_ep.sctp_minrto;
3440169655Srrs				if ((new_min <= new_init) && (new_init <= new_max)) {
3441169655Srrs					inp->sctp_ep.initial_rto = new_init;
3442169655Srrs					inp->sctp_ep.sctp_maxrto = new_max;
3443169655Srrs					inp->sctp_ep.sctp_minrto = new_min;
3444169655Srrs				} else {
3445169655Srrs					error = EDOM;
3446169655Srrs				}
3447163953Srrs				SCTP_INP_WUNLOCK(inp);
3448163953Srrs			}
3449163953Srrs		}
3450163953Srrs		break;
3451163953Srrs	case SCTP_ASSOCINFO:
3452163953Srrs		{
3453163953Srrs			struct sctp_assocparams *sasoc;
3454163953Srrs
3455166675Srrs			SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3456166675Srrs			SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3457166675Srrs
3458163953Srrs			if (stcb) {
3459163953Srrs				if (sasoc->sasoc_asocmaxrxt)
3460163953Srrs					stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3461163953Srrs				sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3462163953Srrs				sasoc->sasoc_peer_rwnd = 0;
3463163953Srrs				sasoc->sasoc_local_rwnd = 0;
3464170056Srrs				if (sasoc->sasoc_cookie_life) {
3465170056Srrs					if (sasoc->sasoc_cookie_life < 1000)
3466170056Srrs						sasoc->sasoc_cookie_life = 1000;
3467169655Srrs					stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3468167598Srrs				}
3469163953Srrs				SCTP_TCB_UNLOCK(stcb);
3470163953Srrs			} else {
3471163953Srrs				SCTP_INP_WLOCK(inp);
3472163953Srrs				if (sasoc->sasoc_asocmaxrxt)
3473163953Srrs					inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3474163953Srrs				sasoc->sasoc_number_peer_destinations = 0;
3475163953Srrs				sasoc->sasoc_peer_rwnd = 0;
3476163953Srrs				sasoc->sasoc_local_rwnd = 0;
3477170056Srrs				if (sasoc->sasoc_cookie_life) {
3478170056Srrs					if (sasoc->sasoc_cookie_life < 1000)
3479170056Srrs						sasoc->sasoc_cookie_life = 1000;
3480169655Srrs					inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3481167598Srrs				}
3482163953Srrs				SCTP_INP_WUNLOCK(inp);
3483163953Srrs			}
3484163953Srrs		}
3485163953Srrs		break;
3486163953Srrs	case SCTP_INITMSG:
3487163953Srrs		{
3488163953Srrs			struct sctp_initmsg *sinit;
3489163953Srrs
3490166675Srrs			SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
3491163953Srrs			SCTP_INP_WLOCK(inp);
3492163953Srrs			if (sinit->sinit_num_ostreams)
3493163953Srrs				inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3494163953Srrs
3495163953Srrs			if (sinit->sinit_max_instreams)
3496163953Srrs				inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3497163953Srrs
3498163953Srrs			if (sinit->sinit_max_attempts)
3499163953Srrs				inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3500163953Srrs
3501167598Srrs			if (sinit->sinit_max_init_timeo)
3502163953Srrs				inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3503163953Srrs			SCTP_INP_WUNLOCK(inp);
3504163953Srrs		}
3505163953Srrs		break;
3506163953Srrs	case SCTP_PRIMARY_ADDR:
3507163953Srrs		{
3508163953Srrs			struct sctp_setprim *spa;
3509163953Srrs			struct sctp_nets *net, *lnet;
3510163953Srrs
3511166675Srrs			SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
3512166675Srrs			SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
3513163953Srrs
3514166675Srrs			net = NULL;
3515166675Srrs			if (stcb) {
3516166675Srrs				net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3517166675Srrs			} else {
3518166675Srrs				/*
3519166675Srrs				 * We increment here since
3520166675Srrs				 * sctp_findassociation_ep_addr() wil do a
3521166675Srrs				 * decrement if it finds the stcb as long as
3522166675Srrs				 * the locked tcb (last argument) is NOT a
3523166675Srrs				 * TCB.. aka NULL.
3524166675Srrs				 */
3525163953Srrs				SCTP_INP_INCR_REF(inp);
3526163953Srrs				stcb = sctp_findassociation_ep_addr(&inp,
3527163953Srrs				    (struct sockaddr *)&spa->ssp_addr,
3528163953Srrs				    &net, NULL, NULL);
3529163953Srrs				if (stcb == NULL) {
3530163953Srrs					SCTP_INP_DECR_REF(inp);
3531163953Srrs				}
3532163953Srrs			}
3533166675Srrs
3534166675Srrs			if ((stcb) && (net)) {
3535166675Srrs				if ((net != stcb->asoc.primary_destination) &&
3536166675Srrs				    (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3537166675Srrs					/* Ok we need to set it */
3538166675Srrs					lnet = stcb->asoc.primary_destination;
3539166675Srrs					if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
3540166675Srrs						if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3541166675Srrs							net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3542166675Srrs						}
3543166675Srrs						net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3544163953Srrs					}
3545163953Srrs				}
3546166675Srrs			} else {
3547166675Srrs				error = EINVAL;
3548163953Srrs			}
3549166675Srrs			if (stcb) {
3550166675Srrs				SCTP_TCB_UNLOCK(stcb);
3551166675Srrs			}
3552163953Srrs		}
3553163953Srrs		break;
3554167598Srrs	case SCTP_SET_DYNAMIC_PRIMARY:
3555167598Srrs		{
3556167598Srrs			union sctp_sockstore *ss;
3557163953Srrs
3558170587Srwatson			error = priv_check(curthread,
3559170587Srwatson			    PRIV_NETINET_RESERVEDPORT);
3560167598Srrs			if (error)
3561167598Srrs				break;
3562167598Srrs
3563167598Srrs			SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
3564167598Srrs			/* SUPER USER CHECK? */
3565167598Srrs			error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
3566167598Srrs		}
3567167598Srrs		break;
3568163953Srrs	case SCTP_SET_PEER_PRIMARY_ADDR:
3569163953Srrs		{
3570163953Srrs			struct sctp_setpeerprim *sspp;
3571163953Srrs
3572166675Srrs			SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
3573166675Srrs			SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
3574169208Srrs			if (stcb != NULL) {
3575170056Srrs				struct sctp_ifa *ifa;
3576170056Srrs
3577170056Srrs				ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
3578170056Srrs				    stcb->asoc.vrf_id, 0);
3579170056Srrs				if (ifa == NULL) {
3580166675Srrs					error = EINVAL;
3581170056Srrs					goto out_of_it;
3582166675Srrs				}
3583170056Srrs				if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3584170056Srrs					/*
3585170056Srrs					 * Must validate the ifa found is in
3586170056Srrs					 * our ep
3587170056Srrs					 */
3588170056Srrs					struct sctp_laddr *laddr;
3589170056Srrs					int found = 0;
3590170056Srrs
3591170056Srrs					LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3592170056Srrs						if (laddr->ifa == NULL) {
3593170056Srrs							SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
3594170056Srrs							    __FUNCTION__);
3595170056Srrs							continue;
3596170056Srrs						}
3597170056Srrs						if (laddr->ifa == ifa) {
3598170056Srrs							found = 1;
3599170056Srrs							break;
3600170056Srrs						}
3601170056Srrs					}
3602170056Srrs					if (!found) {
3603170056Srrs						error = EINVAL;
3604170056Srrs						goto out_of_it;
3605170056Srrs					}
3606170056Srrs				}
3607170056Srrs				if (sctp_set_primary_ip_address_sa(stcb,
3608170056Srrs				    (struct sockaddr *)&sspp->sspp_addr) != 0) {
3609170056Srrs					error = EINVAL;
3610170056Srrs				}
3611170056Srrs		out_of_it:
3612169208Srrs				SCTP_TCB_UNLOCK(stcb);
3613166675Srrs			} else {
3614163953Srrs				error = EINVAL;
3615163953Srrs			}
3616169208Srrs
3617163953Srrs		}
3618163953Srrs		break;
3619163953Srrs	case SCTP_BINDX_ADD_ADDR:
3620163953Srrs		{
3621163953Srrs			struct sctp_getaddresses *addrs;
3622163953Srrs
3623170606Srrs			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
3624170606Srrs			    optsize);
3625170606Srrs			sctp_bindx_add_address(so, inp, addrs->addr,
3626170606Srrs			    addrs->sget_assoc_id, vrf_id,
3627170606Srrs			    &error, p);
3628163953Srrs		}
3629163953Srrs		break;
3630163953Srrs	case SCTP_BINDX_REM_ADDR:
3631163953Srrs		{
3632163953Srrs			struct sctp_getaddresses *addrs;
3633163953Srrs
3634166675Srrs			SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3635170606Srrs			sctp_bindx_delete_address(so, inp, addrs->addr,
3636170606Srrs			    addrs->sget_assoc_id, vrf_id,
3637170606Srrs			    &error);
3638163953Srrs		}
3639163953Srrs		break;
3640163953Srrs	default:
3641163953Srrs		error = ENOPROTOOPT;
3642163953Srrs		break;
3643163953Srrs	}			/* end switch (opt) */
3644163953Srrs	return (error);
3645163953Srrs}
3646163953Srrs
3647163953Srrs
3648163953Srrsint
3649163953Srrssctp_ctloutput(struct socket *so, struct sockopt *sopt)
3650163953Srrs{
3651166675Srrs	void *optval = NULL;
3652166675Srrs	size_t optsize = 0;
3653163953Srrs	struct sctp_inpcb *inp;
3654166675Srrs	void *p;
3655166675Srrs	int error = 0;
3656163953Srrs
3657163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3658163953Srrs	if (inp == 0) {
3659163953Srrs		/* I made the same as TCP since we are not setup? */
3660163953Srrs		return (ECONNRESET);
3661163953Srrs	}
3662163953Srrs	if (sopt->sopt_level != IPPROTO_SCTP) {
3663163953Srrs		/* wrong proto level... send back up to IP */
3664163953Srrs#ifdef INET6
3665163953Srrs		if (INP_CHECK_SOCKAF(so, AF_INET6))
3666163953Srrs			error = ip6_ctloutput(so, sopt);
3667163953Srrs		else
3668163953Srrs#endif				/* INET6 */
3669163953Srrs			error = ip_ctloutput(so, sopt);
3670163953Srrs		return (error);
3671163953Srrs	}
3672166675Srrs	optsize = sopt->sopt_valsize;
3673166675Srrs	if (optsize) {
3674170091Srrs		SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
3675166675Srrs		if (optval == NULL) {
3676163953Srrs			return (ENOBUFS);
3677163953Srrs		}
3678166675Srrs		error = sooptcopyin(sopt, optval, optsize, optsize);
3679163953Srrs		if (error) {
3680170091Srrs			SCTP_FREE(optval, SCTP_M_SOCKOPT);
3681163953Srrs			goto out;
3682163953Srrs		}
3683163953Srrs	}
3684166675Srrs	p = (void *)sopt->sopt_td;
3685163953Srrs	if (sopt->sopt_dir == SOPT_SET) {
3686166675Srrs		error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
3687163953Srrs	} else if (sopt->sopt_dir == SOPT_GET) {
3688166675Srrs		error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
3689163953Srrs	} else {
3690163953Srrs		error = EINVAL;
3691163953Srrs	}
3692166675Srrs	if ((error == 0) && (optval != NULL)) {
3693166675Srrs		error = sooptcopyout(sopt, optval, optsize);
3694170091Srrs		SCTP_FREE(optval, SCTP_M_SOCKOPT);
3695166675Srrs	} else if (optval != NULL) {
3696170091Srrs		SCTP_FREE(optval, SCTP_M_SOCKOPT);
3697163953Srrs	}
3698163953Srrsout:
3699163953Srrs	return (error);
3700163953Srrs}
3701163953Srrs
3702163953Srrs
3703163953Srrsstatic int
3704163953Srrssctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
3705163953Srrs{
3706163953Srrs	int error = 0;
3707163953Srrs	int create_lock_on = 0;
3708167598Srrs	uint32_t vrf_id;
3709163953Srrs	struct sctp_inpcb *inp;
3710163953Srrs	struct sctp_tcb *stcb = NULL;
3711163953Srrs
3712163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3713163953Srrs	if (inp == 0) {
3714163953Srrs		/* I made the same as TCP since we are not setup? */
3715163953Srrs		return (ECONNRESET);
3716163953Srrs	}
3717170056Srrs	if (addr == NULL)
3718170056Srrs		return EINVAL;
3719170056Srrs
3720170056Srrs	if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
3721170056Srrs		return (EINVAL);
3722170056Srrs	}
3723170056Srrs	if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
3724170056Srrs		return (EINVAL);
3725170056Srrs	}
3726163953Srrs	SCTP_ASOC_CREATE_LOCK(inp);
3727163953Srrs	create_lock_on = 1;
3728163953Srrs
3729163953Srrs	SCTP_INP_INCR_REF(inp);
3730163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3731163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3732163953Srrs		/* Should I really unlock ? */
3733163953Srrs		error = EFAULT;
3734163953Srrs		goto out_now;
3735163953Srrs	}
3736163953Srrs#ifdef INET6
3737163953Srrs	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
3738163953Srrs	    (addr->sa_family == AF_INET6)) {
3739163953Srrs		error = EINVAL;
3740163953Srrs		goto out_now;
3741163953Srrs	}
3742163953Srrs#endif				/* INET6 */
3743163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
3744163953Srrs	    SCTP_PCB_FLAGS_UNBOUND) {
3745163953Srrs		/* Bind a ephemeral port */
3746170744Srrs		error = sctp_inpcb_bind(so, NULL, NULL, p);
3747163953Srrs		if (error) {
3748163953Srrs			goto out_now;
3749163953Srrs		}
3750163953Srrs	}
3751163953Srrs	/* Now do we connect? */
3752163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
3753163953Srrs		error = EINVAL;
3754163953Srrs		goto out_now;
3755163953Srrs	}
3756163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3757163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3758163953Srrs		/* We are already connected AND the TCP model */
3759163953Srrs		error = EADDRINUSE;
3760163953Srrs		goto out_now;
3761163953Srrs	}
3762163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3763163953Srrs		SCTP_INP_RLOCK(inp);
3764163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
3765163953Srrs		SCTP_INP_RUNLOCK(inp);
3766163953Srrs	} else {
3767163953Srrs		/*
3768166675Srrs		 * We increment here since sctp_findassociation_ep_addr()
3769166675Srrs		 * wil do a decrement if it finds the stcb as long as the
3770166675Srrs		 * locked tcb (last argument) is NOT a TCB.. aka NULL.
3771163953Srrs		 */
3772163953Srrs		SCTP_INP_INCR_REF(inp);
3773163953Srrs		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
3774163953Srrs		if (stcb == NULL) {
3775163953Srrs			SCTP_INP_DECR_REF(inp);
3776168299Srrs		} else {
3777168299Srrs			SCTP_TCB_LOCK(stcb);
3778163953Srrs		}
3779163953Srrs	}
3780163953Srrs	if (stcb != NULL) {
3781163953Srrs		/* Already have or am bring up an association */
3782163953Srrs		error = EALREADY;
3783163953Srrs		goto out_now;
3784163953Srrs	}
3785168299Srrs	vrf_id = inp->def_vrf_id;
3786163953Srrs	/* We are GOOD to go */
3787167598Srrs	stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id);
3788163953Srrs	if (stcb == NULL) {
3789163953Srrs		/* Gak! no memory */
3790167598Srrs		goto out_now;
3791163953Srrs	}
3792163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3793163953Srrs		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3794163953Srrs		/* Set the connected flag so we can queue data */
3795163953Srrs		soisconnecting(so);
3796163953Srrs	}
3797163953Srrs	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
3798169378Srrs	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3799163953Srrs
3800163953Srrs	/* initialize authentication parameters for the assoc */
3801163953Srrs	sctp_initialize_auth_params(inp, stcb);
3802163953Srrs
3803163953Srrs	sctp_send_initiate(inp, stcb);
3804168299Srrs	SCTP_TCB_UNLOCK(stcb);
3805163953Srrsout_now:
3806169420Srrs	if (create_lock_on) {
3807163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
3808169420Srrs	}
3809163953Srrs	SCTP_INP_DECR_REF(inp);
3810163953Srrs	return error;
3811163953Srrs}
3812163953Srrs
3813163953Srrsint
3814163953Srrssctp_listen(struct socket *so, int backlog, struct thread *p)
3815163953Srrs{
3816163953Srrs	/*
3817163953Srrs	 * Note this module depends on the protocol processing being called
3818163953Srrs	 * AFTER any socket level flags and backlog are applied to the
3819163953Srrs	 * socket. The traditional way that the socket flags are applied is
3820163953Srrs	 * AFTER protocol processing. We have made a change to the
3821163953Srrs	 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
3822163953Srrs	 * place if the socket API for SCTP is to work properly.
3823163953Srrs	 */
3824163953Srrs
3825163953Srrs	int error = 0;
3826163953Srrs	struct sctp_inpcb *inp;
3827163953Srrs
3828163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3829163953Srrs	if (inp == 0) {
3830163953Srrs		/* I made the same as TCP since we are not setup? */
3831163953Srrs		return (ECONNRESET);
3832163953Srrs	}
3833163953Srrs	SCTP_INP_RLOCK(inp);
3834163953Srrs#ifdef SCTP_LOCK_LOGGING
3835170744Srrs	if (sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE) {
3836170744Srrs		sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
3837170744Srrs	}
3838163953Srrs#endif
3839163953Srrs	SOCK_LOCK(so);
3840163953Srrs	error = solisten_proto_check(so);
3841163953Srrs	if (error) {
3842163953Srrs		SOCK_UNLOCK(so);
3843169208Srrs		SCTP_INP_RUNLOCK(inp);
3844163953Srrs		return (error);
3845163953Srrs	}
3846163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3847163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3848163953Srrs		/* We are already connected AND the TCP model */
3849163953Srrs		SCTP_INP_RUNLOCK(inp);
3850163953Srrs		SOCK_UNLOCK(so);
3851163953Srrs		return (EADDRINUSE);
3852163953Srrs	}
3853163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3854163953Srrs		/* We must do a bind. */
3855166675Srrs		SOCK_UNLOCK(so);
3856163953Srrs		SCTP_INP_RUNLOCK(inp);
3857170744Srrs		if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
3858163953Srrs			/* bind error, probably perm */
3859163953Srrs			return (error);
3860163953Srrs		}
3861166675Srrs		SOCK_LOCK(so);
3862163953Srrs	} else {
3863163953Srrs		SCTP_INP_RUNLOCK(inp);
3864163953Srrs	}
3865163953Srrs	/* It appears for 7.0 and on, we must always call this. */
3866163953Srrs	solisten_proto(so, backlog);
3867163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3868163953Srrs		/* remove the ACCEPTCONN flag for one-to-many sockets */
3869163953Srrs		so->so_options &= ~SO_ACCEPTCONN;
3870163953Srrs	}
3871163953Srrs	if (backlog == 0) {
3872163953Srrs		/* turning off listen */
3873163953Srrs		so->so_options &= ~SO_ACCEPTCONN;
3874163953Srrs	}
3875163953Srrs	SOCK_UNLOCK(so);
3876163953Srrs	return (error);
3877163953Srrs}
3878163953Srrs
3879163953Srrsstatic int sctp_defered_wakeup_cnt = 0;
3880163953Srrs
3881163953Srrsint
3882163953Srrssctp_accept(struct socket *so, struct sockaddr **addr)
3883163953Srrs{
3884163953Srrs	struct sctp_tcb *stcb;
3885163953Srrs	struct sctp_inpcb *inp;
3886163953Srrs	union sctp_sockstore store;
3887163953Srrs
3888163953Srrs	int error;
3889163953Srrs
3890163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3891163953Srrs
3892163953Srrs	if (inp == 0) {
3893163953Srrs		return (ECONNRESET);
3894163953Srrs	}
3895163953Srrs	SCTP_INP_RLOCK(inp);
3896163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3897168299Srrs		SCTP_INP_RUNLOCK(inp);
3898163953Srrs		return (ENOTSUP);
3899163953Srrs	}
3900163953Srrs	if (so->so_state & SS_ISDISCONNECTED) {
3901163953Srrs		SCTP_INP_RUNLOCK(inp);
3902163953Srrs		return (ECONNABORTED);
3903163953Srrs	}
3904163953Srrs	stcb = LIST_FIRST(&inp->sctp_asoc_list);
3905163953Srrs	if (stcb == NULL) {
3906163953Srrs		SCTP_INP_RUNLOCK(inp);
3907163953Srrs		return (ECONNRESET);
3908163953Srrs	}
3909163953Srrs	SCTP_TCB_LOCK(stcb);
3910163953Srrs	SCTP_INP_RUNLOCK(inp);
3911163953Srrs	store = stcb->asoc.primary_destination->ro._l_addr;
3912163953Srrs	SCTP_TCB_UNLOCK(stcb);
3913163953Srrs	if (store.sa.sa_family == AF_INET) {
3914163953Srrs		struct sockaddr_in *sin;
3915163953Srrs
3916163953Srrs		SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3917163953Srrs		sin->sin_family = AF_INET;
3918163953Srrs		sin->sin_len = sizeof(*sin);
3919163953Srrs		sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
3920163953Srrs		sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
3921163953Srrs		*addr = (struct sockaddr *)sin;
3922163953Srrs	} else {
3923163953Srrs		struct sockaddr_in6 *sin6;
3924163953Srrs
3925163953Srrs		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
3926163953Srrs		sin6->sin6_family = AF_INET6;
3927163953Srrs		sin6->sin6_len = sizeof(*sin6);
3928163953Srrs		sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
3929163953Srrs
3930163953Srrs		sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
3931164085Srrs		if ((error = sa6_recoverscope(sin6)) != 0) {
3932164085Srrs			SCTP_FREE_SONAME(sin6);
3933163953Srrs			return (error);
3934164085Srrs		}
3935163953Srrs		*addr = (struct sockaddr *)sin6;
3936163953Srrs	}
3937163953Srrs	/* Wake any delayed sleep action */
3938163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
3939166086Srrs		SCTP_INP_WLOCK(inp);
3940163953Srrs		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
3941163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
3942163953Srrs			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3943166086Srrs			SCTP_INP_WUNLOCK(inp);
3944163953Srrs			SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
3945163953Srrs			if (sowriteable(inp->sctp_socket)) {
3946163953Srrs				sowwakeup_locked(inp->sctp_socket);
3947163953Srrs			} else {
3948163953Srrs				SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
3949163953Srrs			}
3950166086Srrs			SCTP_INP_WLOCK(inp);
3951163953Srrs		}
3952163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
3953163953Srrs			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3954166086Srrs			SCTP_INP_WUNLOCK(inp);
3955163953Srrs			SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
3956163953Srrs			if (soreadable(inp->sctp_socket)) {
3957163953Srrs				sctp_defered_wakeup_cnt++;
3958163953Srrs				sorwakeup_locked(inp->sctp_socket);
3959163953Srrs			} else {
3960163953Srrs				SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
3961163953Srrs			}
3962166086Srrs			SCTP_INP_WLOCK(inp);
3963163953Srrs		}
3964166086Srrs		SCTP_INP_WUNLOCK(inp);
3965163953Srrs	}
3966163953Srrs	return (0);
3967163953Srrs}
3968163953Srrs
3969163953Srrsint
3970163953Srrssctp_ingetaddr(struct socket *so, struct sockaddr **addr)
3971163953Srrs{
3972163953Srrs	struct sockaddr_in *sin;
3973167598Srrs	uint32_t vrf_id;
3974163953Srrs	struct sctp_inpcb *inp;
3975167695Srrs	struct sctp_ifa *sctp_ifa;
3976163953Srrs
3977163953Srrs	/*
3978163953Srrs	 * Do the malloc first in case it blocks.
3979163953Srrs	 */
3980163953Srrs	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3981163953Srrs	sin->sin_family = AF_INET;
3982163953Srrs	sin->sin_len = sizeof(*sin);
3983163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
3984163953Srrs	if (!inp) {
3985163953Srrs		SCTP_FREE_SONAME(sin);
3986163953Srrs		return ECONNRESET;
3987163953Srrs	}
3988163953Srrs	SCTP_INP_RLOCK(inp);
3989163953Srrs	sin->sin_port = inp->sctp_lport;
3990163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3991163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3992163953Srrs			struct sctp_tcb *stcb;
3993163953Srrs			struct sockaddr_in *sin_a;
3994163953Srrs			struct sctp_nets *net;
3995163953Srrs			int fnd;
3996163953Srrs
3997163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
3998163953Srrs			if (stcb == NULL) {
3999163953Srrs				goto notConn;
4000163953Srrs			}
4001163953Srrs			fnd = 0;
4002163953Srrs			sin_a = NULL;
4003163953Srrs			SCTP_TCB_LOCK(stcb);
4004163953Srrs			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4005163953Srrs				sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4006164085Srrs				if (sin_a == NULL)
4007164085Srrs					/* this will make coverity happy */
4008164085Srrs					continue;
4009164085Srrs
4010163953Srrs				if (sin_a->sin_family == AF_INET) {
4011163953Srrs					fnd = 1;
4012163953Srrs					break;
4013163953Srrs				}
4014163953Srrs			}
4015163953Srrs			if ((!fnd) || (sin_a == NULL)) {
4016163953Srrs				/* punt */
4017163953Srrs				SCTP_TCB_UNLOCK(stcb);
4018163953Srrs				goto notConn;
4019163953Srrs			}
4020168299Srrs			vrf_id = inp->def_vrf_id;
4021167598Srrs			sctp_ifa = sctp_source_address_selection(inp,
4022167598Srrs			    stcb,
4023168299Srrs			    (sctp_route_t *) & net->ro,
4024167598Srrs			    net, 0, vrf_id);
4025167598Srrs			if (sctp_ifa) {
4026167598Srrs				sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4027167598Srrs				sctp_free_ifa(sctp_ifa);
4028167598Srrs			}
4029163953Srrs			SCTP_TCB_UNLOCK(stcb);
4030163953Srrs		} else {
4031163953Srrs			/* For the bound all case you get back 0 */
4032163953Srrs	notConn:
4033163953Srrs			sin->sin_addr.s_addr = 0;
4034163953Srrs		}
4035163953Srrs
4036163953Srrs	} else {
4037163953Srrs		/* Take the first IPv4 address in the list */
4038163953Srrs		struct sctp_laddr *laddr;
4039163953Srrs		int fnd = 0;
4040163953Srrs
4041163953Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4042167598Srrs			if (laddr->ifa->address.sa.sa_family == AF_INET) {
4043163953Srrs				struct sockaddr_in *sin_a;
4044163953Srrs
4045167598Srrs				sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4046163953Srrs				sin->sin_addr = sin_a->sin_addr;
4047163953Srrs				fnd = 1;
4048163953Srrs				break;
4049163953Srrs			}
4050163953Srrs		}
4051163953Srrs		if (!fnd) {
4052163953Srrs			SCTP_FREE_SONAME(sin);
4053163953Srrs			SCTP_INP_RUNLOCK(inp);
4054163953Srrs			return ENOENT;
4055163953Srrs		}
4056163953Srrs	}
4057163953Srrs	SCTP_INP_RUNLOCK(inp);
4058163953Srrs	(*addr) = (struct sockaddr *)sin;
4059163953Srrs	return (0);
4060163953Srrs}
4061163953Srrs
4062163953Srrsint
4063163953Srrssctp_peeraddr(struct socket *so, struct sockaddr **addr)
4064163953Srrs{
4065163953Srrs	struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4066166086Srrs	int fnd;
4067163953Srrs	struct sockaddr_in *sin_a;
4068163953Srrs	struct sctp_inpcb *inp;
4069163953Srrs	struct sctp_tcb *stcb;
4070163953Srrs	struct sctp_nets *net;
4071163953Srrs
4072163953Srrs	/* Do the malloc first in case it blocks. */
4073163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
4074163953Srrs	if ((inp == NULL) ||
4075163953Srrs	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4076163953Srrs		/* UDP type and listeners will drop out here */
4077163953Srrs		return (ENOTCONN);
4078163953Srrs	}
4079163953Srrs	SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4080163953Srrs	sin->sin_family = AF_INET;
4081163953Srrs	sin->sin_len = sizeof(*sin);
4082163953Srrs
4083163953Srrs	/* We must recapture incase we blocked */
4084163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
4085163953Srrs	if (!inp) {
4086163953Srrs		SCTP_FREE_SONAME(sin);
4087163953Srrs		return ECONNRESET;
4088163953Srrs	}
4089163953Srrs	SCTP_INP_RLOCK(inp);
4090163953Srrs	stcb = LIST_FIRST(&inp->sctp_asoc_list);
4091169420Srrs	if (stcb) {
4092163953Srrs		SCTP_TCB_LOCK(stcb);
4093169420Srrs	}
4094163953Srrs	SCTP_INP_RUNLOCK(inp);
4095163953Srrs	if (stcb == NULL) {
4096163953Srrs		SCTP_FREE_SONAME(sin);
4097163953Srrs		return ECONNRESET;
4098163953Srrs	}
4099163953Srrs	fnd = 0;
4100163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4101163953Srrs		sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4102163953Srrs		if (sin_a->sin_family == AF_INET) {
4103163953Srrs			fnd = 1;
4104163953Srrs			sin->sin_port = stcb->rport;
4105163953Srrs			sin->sin_addr = sin_a->sin_addr;
4106163953Srrs			break;
4107163953Srrs		}
4108163953Srrs	}
4109163953Srrs	SCTP_TCB_UNLOCK(stcb);
4110163953Srrs	if (!fnd) {
4111163953Srrs		/* No IPv4 address */
4112163953Srrs		SCTP_FREE_SONAME(sin);
4113163953Srrs		return ENOENT;
4114163953Srrs	}
4115163953Srrs	(*addr) = (struct sockaddr *)sin;
4116163953Srrs	return (0);
4117163953Srrs}
4118163953Srrs
4119163953Srrsstruct pr_usrreqs sctp_usrreqs = {
4120163953Srrs	.pru_abort = sctp_abort,
4121163953Srrs	.pru_accept = sctp_accept,
4122163953Srrs	.pru_attach = sctp_attach,
4123163953Srrs	.pru_bind = sctp_bind,
4124163953Srrs	.pru_connect = sctp_connect,
4125163953Srrs	.pru_control = in_control,
4126163953Srrs	.pru_close = sctp_close,
4127163953Srrs	.pru_detach = sctp_close,
4128163953Srrs	.pru_sopoll = sopoll_generic,
4129163953Srrs	.pru_disconnect = sctp_disconnect,
4130163953Srrs	.pru_listen = sctp_listen,
4131163953Srrs	.pru_peeraddr = sctp_peeraddr,
4132163953Srrs	.pru_send = sctp_sendm,
4133163953Srrs	.pru_shutdown = sctp_shutdown,
4134163953Srrs	.pru_sockaddr = sctp_ingetaddr,
4135163953Srrs	.pru_sosend = sctp_sosend,
4136163953Srrs	.pru_soreceive = sctp_soreceive
4137163953Srrs};
4138