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