sctp6_usrreq.c revision 168299
1163953Srrs/*-
2166675Srrs * Copyright (c) 2001-2007, Cisco Systems, Inc. All rights reserved.
3163953Srrs *
4163953Srrs * Redistribution and use in source and binary forms, with or without
5163953Srrs * modification, are permitted provided that the following conditions are met:
6163953Srrs *
7163953Srrs * a) Redistributions of source code must retain the above copyright notice,
8163953Srrs *   this list of conditions and the following disclaimer.
9163953Srrs *
10163953Srrs * b) Redistributions in binary form must reproduce the above copyright
11163953Srrs *    notice, this list of conditions and the following disclaimer in
12163953Srrs *   the documentation and/or other materials provided with the distribution.
13163953Srrs *
14163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
15163953Srrs *    contributors may be used to endorse or promote products derived
16163953Srrs *    from this software without specific prior written permission.
17163953Srrs *
18163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
29163953Srrs */
30163953Srrs/*	$KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $	*/
31163954Srrs#include <sys/cdefs.h>
32163953Srrs__FBSDID("$FreeBSD: head/sys/netinet6/sctp6_usrreq.c 168299 2007-04-03 11:15:32Z rrs $");
33163953Srrs
34166086Srrs#include <netinet/sctp_os.h>
35163953Srrs#include <sys/proc.h>
36163953Srrs#include <netinet/sctp_pcb.h>
37163953Srrs#include <netinet/sctp_header.h>
38163953Srrs#include <netinet/sctp_var.h>
39167598Srrs#include <netinet/sctp_sysctl.h>
40163953Srrs#include <netinet/sctp_output.h>
41166086Srrs#include <netinet/sctp_input.h>
42163953Srrs#include <netinet/sctp_bsd_addr.h>
43166086Srrs#include <netinet/sctp_uio.h>
44163953Srrs#include <netinet/sctp_asconf.h>
45166086Srrs#include <netinet/sctputil.h>
46166086Srrs#include <netinet/sctp_indata.h>
47166086Srrs#include <netinet/sctp_asconf.h>
48166086Srrs#include <netinet/sctp_timer.h>
49166086Srrs#include <netinet/sctp_auth.h>
50163953Srrs#include <netinet6/sctp6_var.h>
51163953Srrs
52163953Srrs
53163953Srrs
54163953Srrsextern struct protosw inetsw[];
55163953Srrs
56163953Srrs
57163953Srrs
58163953Srrsint
59163953Srrssctp6_input(mp, offp, proto)
60163953Srrs	struct mbuf **mp;
61163953Srrs	int *offp;
62163953Srrs	int proto;
63163953Srrs{
64165647Srrs	struct mbuf *m;
65163953Srrs	struct ip6_hdr *ip6;
66163953Srrs	struct sctphdr *sh;
67163953Srrs	struct sctp_inpcb *in6p = NULL;
68163953Srrs	struct sctp_nets *net;
69163953Srrs	int refcount_up = 0;
70168299Srrs	uint32_t check, calc_check, vrf_id;
71163953Srrs	struct inpcb *in6p_ip;
72163953Srrs	struct sctp_chunkhdr *ch;
73163953Srrs	int length, mlen, offset, iphlen;
74168299Srrs	uint8_t ecn_bits;
75163953Srrs	struct sctp_tcb *stcb = NULL;
76163953Srrs	int off = *offp;
77163953Srrs
78168299Srrs	vrf_id = SCTP_DEFAULT_VRFID;
79165647Srrs	m = SCTP_HEADER_TO_CHAIN(*mp);
80165647Srrs
81163953Srrs	ip6 = mtod(m, struct ip6_hdr *);
82163953Srrs	/* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
83163953Srrs	IP6_EXTHDR_GET(sh, struct sctphdr *, m, off, sizeof(*sh) + sizeof(*ch));
84163953Srrs	if (sh == NULL) {
85163953Srrs		SCTP_STAT_INCR(sctps_hdrops);
86163953Srrs		return IPPROTO_DONE;
87163953Srrs	}
88163953Srrs	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
89163953Srrs	iphlen = off;
90163953Srrs	offset = iphlen + sizeof(*sh) + sizeof(*ch);
91163953Srrs
92163953Srrs#if defined(NFAITH) && NFAITH > 0
93163953Srrs
94163953Srrs	if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) {
95163953Srrs		/* XXX send icmp6 host/port unreach? */
96163953Srrs		goto bad;
97163953Srrs	}
98163953Srrs#endif				/* NFAITH defined and > 0 */
99163953Srrs	SCTP_STAT_INCR(sctps_recvpackets);
100163953Srrs	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
101163953Srrs#ifdef SCTP_DEBUG
102163953Srrs	if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
103165647Srrs		printf("V6 input gets a packet iphlen:%d pktlen:%d\n", iphlen, SCTP_HEADER_LEN((*mp)));
104163953Srrs	}
105163953Srrs#endif
106163953Srrs	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
107163953Srrs		/* No multi-cast support in SCTP */
108163953Srrs		goto bad;
109163953Srrs	}
110163953Srrs	/* destination port of 0 is illegal, based on RFC2960. */
111163953Srrs	if (sh->dest_port == 0)
112163953Srrs		goto bad;
113163953Srrs	if ((sctp_no_csum_on_loopback == 0) ||
114165647Srrs	    (!SCTP_IS_IT_LOOPBACK(m))) {
115163953Srrs		/*
116163953Srrs		 * we do NOT validate things from the loopback if the sysctl
117163953Srrs		 * is set to 1.
118163953Srrs		 */
119163953Srrs		check = sh->checksum;	/* save incoming checksum */
120163953Srrs		if ((check == 0) && (sctp_no_csum_on_loopback)) {
121163953Srrs			/*
122163953Srrs			 * special hook for where we got a local address
123163953Srrs			 * somehow routed across a non IFT_LOOP type
124163953Srrs			 * interface
125163953Srrs			 */
126163953Srrs			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))
127163953Srrs				goto sctp_skip_csum;
128163953Srrs		}
129163953Srrs		sh->checksum = 0;	/* prepare for calc */
130163953Srrs		calc_check = sctp_calculate_sum(m, &mlen, iphlen);
131163953Srrs		if (calc_check != check) {
132163953Srrs#ifdef SCTP_DEBUG
133163953Srrs			if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
134163996Srrs				printf("Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
135163996Srrs				    calc_check, check, m,
136163953Srrs				    mlen, iphlen);
137163953Srrs			}
138163953Srrs#endif
139163953Srrs			stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
140168299Srrs			    sh, ch, &in6p, &net, vrf_id);
141163953Srrs			/* in6p's ref-count increased && stcb locked */
142163953Srrs			if ((in6p) && (stcb)) {
143163953Srrs				sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
144163953Srrs				sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, 2);
145163953Srrs			} else if ((in6p != NULL) && (stcb == NULL)) {
146163953Srrs				refcount_up = 1;
147163953Srrs			}
148163953Srrs			SCTP_STAT_INCR(sctps_badsum);
149163953Srrs			SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
150163953Srrs			goto bad;
151163953Srrs		}
152163953Srrs		sh->checksum = calc_check;
153165647Srrs	}
154163953Srrssctp_skip_csum:
155163953Srrs	net = NULL;
156163953Srrs	/*
157163953Srrs	 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
158163953Srrs	 * IP/SCTP/first chunk header...
159163953Srrs	 */
160163953Srrs	stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
161168299Srrs	    sh, ch, &in6p, &net, vrf_id);
162163953Srrs	/* in6p's ref-count increased */
163163953Srrs	if (in6p == NULL) {
164163953Srrs		struct sctp_init_chunk *init_chk, chunk_buf;
165163953Srrs
166163953Srrs		SCTP_STAT_INCR(sctps_noport);
167163953Srrs		if (ch->chunk_type == SCTP_INITIATION) {
168163953Srrs			/*
169163953Srrs			 * we do a trick here to get the INIT tag, dig in
170163953Srrs			 * and get the tag from the INIT and put it in the
171163953Srrs			 * common header.
172163953Srrs			 */
173163953Srrs			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
174163953Srrs			    iphlen + sizeof(*sh), sizeof(*init_chk),
175168299Srrs			    (uint8_t *) & chunk_buf);
176163953Srrs			sh->v_tag = init_chk->init.initiate_tag;
177163953Srrs		}
178165220Srrs		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
179165220Srrs			sctp_send_shutdown_complete2(m, iphlen, sh);
180165220Srrs			goto bad;
181165220Srrs		}
182165220Srrs		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
183165220Srrs			goto bad;
184165220Srrs		}
185165220Srrs		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
186165220Srrs			sctp_send_abort(m, iphlen, sh, 0, NULL);
187163953Srrs		goto bad;
188163953Srrs	} else if (stcb == NULL) {
189163953Srrs		refcount_up = 1;
190163953Srrs	}
191163953Srrs	in6p_ip = (struct inpcb *)in6p;
192163953Srrs#ifdef IPSEC
193163953Srrs	/*
194163953Srrs	 * Check AH/ESP integrity.
195163953Srrs	 */
196163996Srrs	if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) {
197163953Srrs/* XXX */
198163953Srrs		ipsec6stat.in_polvio++;
199163953Srrs		goto bad;
200163996Srrs	}
201163953Srrs#endif				/* IPSEC */
202163953Srrs
203163953Srrs
204163953Srrs	/*
205163953Srrs	 * CONTROL chunk processing
206163953Srrs	 */
207163953Srrs	offset -= sizeof(*ch);
208163953Srrs	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
209165647Srrs
210165647Srrs	/* Length now holds the total packet length payload + iphlen */
211165647Srrs	length = ntohs(ip6->ip6_plen) + iphlen;
212165647Srrs
213163953Srrs	(void)sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
214163953Srrs	    in6p, stcb, net, ecn_bits);
215163953Srrs	/* inp's ref-count reduced && stcb unlocked */
216163953Srrs	/* XXX this stuff below gets moved to appropriate parts later... */
217163953Srrs	if (m)
218163953Srrs		m_freem(m);
219163953Srrs	if ((in6p) && refcount_up) {
220163953Srrs		/* reduce ref-count */
221163953Srrs		SCTP_INP_WLOCK(in6p);
222163953Srrs		SCTP_INP_DECR_REF(in6p);
223163953Srrs		SCTP_INP_WUNLOCK(in6p);
224163953Srrs	}
225163953Srrs	return IPPROTO_DONE;
226163953Srrs
227163953Srrsbad:
228163953Srrs	if (stcb)
229163953Srrs		SCTP_TCB_UNLOCK(stcb);
230163953Srrs
231163953Srrs	if ((in6p) && refcount_up) {
232163953Srrs		/* reduce ref-count */
233163953Srrs		SCTP_INP_WLOCK(in6p);
234163953Srrs		SCTP_INP_DECR_REF(in6p);
235163953Srrs		SCTP_INP_WUNLOCK(in6p);
236163953Srrs	}
237163953Srrs	if (m)
238163953Srrs		m_freem(m);
239163953Srrs	return IPPROTO_DONE;
240163953Srrs}
241163953Srrs
242163953Srrs
243163953Srrsstatic void
244163953Srrssctp6_notify_mbuf(struct sctp_inpcb *inp,
245163953Srrs    struct icmp6_hdr *icmp6,
246163953Srrs    struct sctphdr *sh,
247163953Srrs    struct sctp_tcb *stcb,
248163953Srrs    struct sctp_nets *net)
249163953Srrs{
250168299Srrs	uint32_t nxtsz;
251163953Srrs
252163953Srrs	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
253163953Srrs	    (icmp6 == NULL) || (sh == NULL)) {
254163953Srrs		goto out;
255163953Srrs	}
256163953Srrs	/* First do we even look at it? */
257163953Srrs	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
258163953Srrs		goto out;
259163953Srrs
260163953Srrs	if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
261163953Srrs		/* not PACKET TO BIG */
262163953Srrs		goto out;
263163953Srrs	}
264163953Srrs	/*
265163953Srrs	 * ok we need to look closely. We could even get smarter and look at
266163953Srrs	 * anyone that we sent to in case we get a different ICMP that tells
267163953Srrs	 * us there is no way to reach a host, but for this impl, all we
268163953Srrs	 * care about is MTU discovery.
269163953Srrs	 */
270163953Srrs	nxtsz = ntohl(icmp6->icmp6_mtu);
271163953Srrs	/* Stop any PMTU timer */
272165220Srrs	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
273163953Srrs
274163953Srrs	/* Adjust destination size limit */
275163953Srrs	if (net->mtu > nxtsz) {
276163953Srrs		net->mtu = nxtsz;
277163953Srrs	}
278163953Srrs	/* now what about the ep? */
279163953Srrs	if (stcb->asoc.smallest_mtu > nxtsz) {
280163953Srrs		struct sctp_tmit_chunk *chk;
281163953Srrs
282163953Srrs		/* Adjust that too */
283163953Srrs		stcb->asoc.smallest_mtu = nxtsz;
284163953Srrs		/* now off to subtract IP_DF flag if needed */
285163953Srrs
286163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
287168299Srrs			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
288163953Srrs				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
289163953Srrs			}
290163953Srrs		}
291163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
292168299Srrs			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
293163953Srrs				/*
294163953Srrs				 * For this guy we also mark for immediate
295163953Srrs				 * resend since we sent to big of chunk
296163953Srrs				 */
297163953Srrs				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
298163953Srrs				if (chk->sent != SCTP_DATAGRAM_RESEND)
299163953Srrs					stcb->asoc.sent_queue_retran_cnt++;
300163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
301163953Srrs				chk->rec.data.doing_fast_retransmit = 0;
302163953Srrs
303163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
304163953Srrs				/* Clear any time so NO RTT is being done */
305163953Srrs				chk->sent_rcv_time.tv_sec = 0;
306163953Srrs				chk->sent_rcv_time.tv_usec = 0;
307163953Srrs				stcb->asoc.total_flight -= chk->send_size;
308163953Srrs				net->flight_size -= chk->send_size;
309163953Srrs			}
310163953Srrs		}
311163953Srrs	}
312163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
313163953Srrsout:
314163953Srrs	if (stcb)
315163953Srrs		SCTP_TCB_UNLOCK(stcb);
316163953Srrs}
317163953Srrs
318163953Srrs
319163953Srrsvoid
320163953Srrssctp6_ctlinput(cmd, pktdst, d)
321163953Srrs	int cmd;
322163953Srrs	struct sockaddr *pktdst;
323163953Srrs	void *d;
324163953Srrs{
325163953Srrs	struct sctphdr sh;
326163953Srrs	struct ip6ctlparam *ip6cp = NULL;
327167598Srrs	uint32_t vrf_id;
328166086Srrs	int cm;
329163953Srrs
330167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
331167598Srrs
332163953Srrs	if (pktdst->sa_family != AF_INET6 ||
333163953Srrs	    pktdst->sa_len != sizeof(struct sockaddr_in6))
334163953Srrs		return;
335163953Srrs
336163953Srrs	if ((unsigned)cmd >= PRC_NCMDS)
337163953Srrs		return;
338163953Srrs	if (PRC_IS_REDIRECT(cmd)) {
339163953Srrs		d = NULL;
340163953Srrs	} else if (inet6ctlerrmap[cmd] == 0) {
341163953Srrs		return;
342163953Srrs	}
343163953Srrs	/* if the parameter is from icmp6, decode it. */
344163953Srrs	if (d != NULL) {
345163953Srrs		ip6cp = (struct ip6ctlparam *)d;
346163953Srrs	} else {
347163953Srrs		ip6cp = (struct ip6ctlparam *)NULL;
348163953Srrs	}
349163953Srrs
350163953Srrs	if (ip6cp) {
351163953Srrs		/*
352163953Srrs		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
353163953Srrs		 * valid.
354163953Srrs		 */
355163953Srrs		/* check if we can safely examine src and dst ports */
356163953Srrs		struct sctp_inpcb *inp = NULL;
357163953Srrs		struct sctp_tcb *stcb = NULL;
358163953Srrs		struct sctp_nets *net = NULL;
359163953Srrs		struct sockaddr_in6 final;
360163953Srrs
361165647Srrs		if (ip6cp->ip6c_m == NULL)
362163953Srrs			return;
363163953Srrs
364163953Srrs		bzero(&sh, sizeof(sh));
365163953Srrs		bzero(&final, sizeof(final));
366163953Srrs		inp = NULL;
367163953Srrs		net = NULL;
368163953Srrs		m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
369163953Srrs		    (caddr_t)&sh);
370163953Srrs		ip6cp->ip6c_src->sin6_port = sh.src_port;
371163953Srrs		final.sin6_len = sizeof(final);
372163953Srrs		final.sin6_family = AF_INET6;
373163953Srrs		final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
374163953Srrs		final.sin6_port = sh.dest_port;
375163953Srrs		stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
376163953Srrs		    (struct sockaddr *)&final,
377167598Srrs		    &inp, &net, 1, vrf_id);
378163953Srrs		/* inp's ref-count increased && stcb locked */
379163953Srrs		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
380163953Srrs			if (cmd == PRC_MSGSIZE) {
381163953Srrs				sctp6_notify_mbuf(inp,
382163953Srrs				    ip6cp->ip6c_icmp6,
383163953Srrs				    &sh,
384163953Srrs				    stcb,
385163953Srrs				    net);
386163953Srrs				/* inp's ref-count reduced && stcb unlocked */
387163953Srrs			} else {
388163953Srrs				if (cmd == PRC_HOSTDEAD) {
389163953Srrs					cm = EHOSTUNREACH;
390163953Srrs				} else {
391163953Srrs					cm = inet6ctlerrmap[cmd];
392163953Srrs				}
393163953Srrs				sctp_notify(inp, cm, &sh,
394163953Srrs				    (struct sockaddr *)&final,
395163953Srrs				    stcb, net);
396163953Srrs				/* inp's ref-count reduced && stcb unlocked */
397163953Srrs			}
398163953Srrs		} else {
399163953Srrs			if (PRC_IS_REDIRECT(cmd) && inp) {
400163953Srrs				in6_rtchange((struct in6pcb *)inp,
401163953Srrs				    inet6ctlerrmap[cmd]);
402163953Srrs			}
403163953Srrs			if (inp) {
404163953Srrs				/* reduce inp's ref-count */
405163953Srrs				SCTP_INP_WLOCK(inp);
406163953Srrs				SCTP_INP_DECR_REF(inp);
407163953Srrs				SCTP_INP_WUNLOCK(inp);
408163953Srrs			}
409163953Srrs			if (stcb)
410163953Srrs				SCTP_TCB_UNLOCK(stcb);
411163953Srrs		}
412163953Srrs	}
413163953Srrs}
414163953Srrs
415163953Srrs/*
416163953Srrs * this routine can probably be collasped into the one in sctp_userreq.c
417163953Srrs * since they do the same thing and now we lookup with a sockaddr
418163953Srrs */
419163953Srrsstatic int
420163953Srrssctp6_getcred(SYSCTL_HANDLER_ARGS)
421163953Srrs{
422164085Srrs	struct xucred xuc;
423163953Srrs	struct sockaddr_in6 addrs[2];
424163953Srrs	struct sctp_inpcb *inp;
425163953Srrs	struct sctp_nets *net;
426163953Srrs	struct sctp_tcb *stcb;
427164085Srrs	int error;
428167598Srrs	uint32_t vrf_id;
429163953Srrs
430167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
431167598Srrs
432164039Srwatson	/*
433164039Srwatson	 * XXXRW: Other instances of getcred use SUSER_ALLOWJAIL, as socket
434164039Srwatson	 * visibility is scoped using cr_canseesocket(), which it is not
435164039Srwatson	 * here.
436164039Srwatson	 */
437164039Srwatson	error = priv_check_cred(req->td->td_ucred, PRIV_NETINET_RESERVEDPORT,
438164039Srwatson	    0);
439163953Srrs	if (error)
440163953Srrs		return (error);
441163953Srrs
442163953Srrs	if (req->newlen != sizeof(addrs))
443163953Srrs		return (EINVAL);
444163953Srrs	if (req->oldlen != sizeof(struct ucred))
445163953Srrs		return (EINVAL);
446163953Srrs	error = SYSCTL_IN(req, addrs, sizeof(addrs));
447163953Srrs	if (error)
448163953Srrs		return (error);
449163953Srrs
450163953Srrs	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
451163953Srrs	    sin6tosa(&addrs[1]),
452167598Srrs	    &inp, &net, 1, vrf_id);
453163953Srrs	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
454164085Srrs		if ((inp != NULL) && (stcb == NULL)) {
455164085Srrs			/* reduce ref-count */
456163953Srrs			SCTP_INP_WLOCK(inp);
457163953Srrs			SCTP_INP_DECR_REF(inp);
458164085Srrs			goto cred_can_cont;
459163953Srrs		}
460164085Srrs		error = ENOENT;
461163953Srrs		goto out;
462163953Srrs	}
463163953Srrs	SCTP_TCB_UNLOCK(stcb);
464164085Srrs	/*
465164085Srrs	 * We use the write lock here, only since in the error leg we need
466164085Srrs	 * it. If we used RLOCK, then we would have to
467164085Srrs	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
468164085Srrs	 * Better to use higher wlock.
469164085Srrs	 */
470164085Srrs	SCTP_INP_WLOCK(inp);
471164085Srrscred_can_cont:
472164085Srrs	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
473164085Srrs	if (error) {
474164085Srrs		SCTP_INP_WUNLOCK(inp);
475164085Srrs		goto out;
476164085Srrs	}
477164085Srrs	cru2x(inp->sctp_socket->so_cred, &xuc);
478164085Srrs	SCTP_INP_WUNLOCK(inp);
479164085Srrs	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
480163953Srrsout:
481163953Srrs	return (error);
482163953Srrs}
483163953Srrs
484163953SrrsSYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
485163953Srrs    0, 0,
486163953Srrs    sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
487163953Srrs
488163953Srrs
489163953Srrs/* This is the same as the sctp_abort() could be made common */
490163953Srrsstatic void
491163953Srrssctp6_abort(struct socket *so)
492163953Srrs{
493163953Srrs	struct sctp_inpcb *inp;
494163953Srrs	uint32_t flags;
495163953Srrs
496163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
497163953Srrs	if (inp == 0)
498163953Srrs		return;
499163953Srrssctp_must_try_again:
500163953Srrs	flags = inp->sctp_flags;
501163953Srrs#ifdef SCTP_LOG_CLOSING
502163953Srrs	sctp_log_closing(inp, NULL, 17);
503163953Srrs#endif
504163953Srrs	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
505163953Srrs	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
506163953Srrs#ifdef SCTP_LOG_CLOSING
507163953Srrs		sctp_log_closing(inp, NULL, 16);
508163953Srrs#endif
509163953Srrs		sctp_inpcb_free(inp, 1, 0);
510163953Srrs		SOCK_LOCK(so);
511167695Srrs		SCTP_SB_CLEAR(so->so_snd);
512163953Srrs		/*
513163953Srrs		 * same for the rcv ones, they are only here for the
514163953Srrs		 * accounting/select.
515163953Srrs		 */
516167695Srrs		SCTP_SB_CLEAR(so->so_rcv);
517167695Srrs		/* Now null out the reference, we are completely detached. */
518163953Srrs		so->so_pcb = NULL;
519163953Srrs		SOCK_UNLOCK(so);
520163953Srrs	} else {
521163953Srrs		flags = inp->sctp_flags;
522163953Srrs		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
523163953Srrs			goto sctp_must_try_again;
524163953Srrs		}
525163953Srrs	}
526163953Srrs	return;
527163953Srrs}
528163953Srrs
529163953Srrsstatic int
530163953Srrssctp6_attach(struct socket *so, int proto, struct thread *p)
531163953Srrs{
532163953Srrs	struct in6pcb *inp6;
533166086Srrs	int error;
534163953Srrs	struct sctp_inpcb *inp;
535163953Srrs
536163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
537163953Srrs	if (inp != NULL)
538163953Srrs		return EINVAL;
539163953Srrs
540163953Srrs	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
541167695Srrs		error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
542163953Srrs		if (error)
543163953Srrs			return error;
544163953Srrs	}
545163953Srrs	error = sctp_inpcb_alloc(so);
546163953Srrs	if (error)
547163953Srrs		return error;
548163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
549163953Srrs	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
550163953Srrs	inp6 = (struct in6pcb *)inp;
551163953Srrs
552163953Srrs	inp6->inp_vflag |= INP_IPV6;
553163953Srrs	inp6->in6p_hops = -1;	/* use kernel default */
554163953Srrs	inp6->in6p_cksum = -1;	/* just to be sure */
555163953Srrs#ifdef INET
556163953Srrs	/*
557163953Srrs	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
558163953Srrs	 * socket as well, because the socket may be bound to an IPv6
559163953Srrs	 * wildcard address, which may match an IPv4-mapped IPv6 address.
560163953Srrs	 */
561163953Srrs	inp6->inp_ip_ttl = ip_defttl;
562163953Srrs#endif
563163953Srrs	/*
564163953Srrs	 * Hmm what about the IPSEC stuff that is missing here but in
565163953Srrs	 * sctp_attach()?
566163953Srrs	 */
567163953Srrs	return 0;
568163953Srrs}
569163953Srrs
570163953Srrsstatic int
571163953Srrssctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
572163953Srrs{
573163953Srrs	struct sctp_inpcb *inp;
574163953Srrs	struct in6pcb *inp6;
575166086Srrs	int error;
576163953Srrs
577163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
578163953Srrs	if (inp == 0)
579163953Srrs		return EINVAL;
580163953Srrs
581163953Srrs	inp6 = (struct in6pcb *)inp;
582163953Srrs	inp6->inp_vflag &= ~INP_IPV4;
583163953Srrs	inp6->inp_vflag |= INP_IPV6;
584166023Srrs	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
585163953Srrs		if (addr->sa_family == AF_INET) {
586163953Srrs			/* binding v4 addr to v6 socket, so reset flags */
587163953Srrs			inp6->inp_vflag |= INP_IPV4;
588163953Srrs			inp6->inp_vflag &= ~INP_IPV6;
589163953Srrs		} else {
590163953Srrs			struct sockaddr_in6 *sin6_p;
591163953Srrs
592163953Srrs			sin6_p = (struct sockaddr_in6 *)addr;
593163953Srrs
594163953Srrs			if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
595163953Srrs				inp6->inp_vflag |= INP_IPV4;
596163953Srrs			} else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
597163953Srrs				struct sockaddr_in sin;
598163953Srrs
599163953Srrs				in6_sin6_2_sin(&sin, sin6_p);
600163953Srrs				inp6->inp_vflag |= INP_IPV4;
601163953Srrs				inp6->inp_vflag &= ~INP_IPV6;
602163953Srrs				error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, p);
603163953Srrs				return error;
604163953Srrs			}
605163953Srrs		}
606163953Srrs	} else if (addr != NULL) {
607163953Srrs		/* IPV6_V6ONLY socket */
608163953Srrs		if (addr->sa_family == AF_INET) {
609163953Srrs			/* can't bind v4 addr to v6 only socket! */
610163953Srrs			return EINVAL;
611163953Srrs		} else {
612163953Srrs			struct sockaddr_in6 *sin6_p;
613163953Srrs
614163953Srrs			sin6_p = (struct sockaddr_in6 *)addr;
615163953Srrs
616163953Srrs			if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
617163953Srrs				/* can't bind v4-mapped addrs either! */
618163953Srrs				/* NOTE: we don't support SIIT */
619163953Srrs				return EINVAL;
620163953Srrs		}
621163953Srrs	}
622163953Srrs	error = sctp_inpcb_bind(so, addr, p);
623163953Srrs	return error;
624163953Srrs}
625163953Srrs
626163953Srrs
627163953Srrsstatic void
628163953Srrssctp6_close(struct socket *so)
629163953Srrs{
630163953Srrs	struct sctp_inpcb *inp;
631163953Srrs	uint32_t flags;
632163953Srrs
633163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
634163953Srrs	if (inp == 0)
635163953Srrs		return;
636163953Srrs
637163953Srrs	/*
638163953Srrs	 * Inform all the lower layer assoc that we are done.
639163953Srrs	 */
640163953Srrssctp_must_try_again:
641163953Srrs	flags = inp->sctp_flags;
642163953Srrs#ifdef SCTP_LOG_CLOSING
643163953Srrs	sctp_log_closing(inp, NULL, 17);
644163953Srrs#endif
645163953Srrs	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
646163953Srrs	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
647163953Srrs		if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
648163953Srrs		    (so->so_rcv.sb_cc > 0)) {
649163953Srrs#ifdef SCTP_LOG_CLOSING
650163953Srrs			sctp_log_closing(inp, NULL, 13);
651163953Srrs#endif
652163953Srrs			sctp_inpcb_free(inp, 1, 1);
653163953Srrs		} else {
654163953Srrs#ifdef SCTP_LOG_CLOSING
655163953Srrs			sctp_log_closing(inp, NULL, 14);
656163953Srrs#endif
657163953Srrs			sctp_inpcb_free(inp, 0, 1);
658163953Srrs		}
659163953Srrs		/*
660163953Srrs		 * The socket is now detached, no matter what the state of
661163953Srrs		 * the SCTP association.
662163953Srrs		 */
663163953Srrs		SOCK_LOCK(so);
664167695Srrs		SCTP_SB_CLEAR(so->so_snd);
665163953Srrs		/*
666163953Srrs		 * same for the rcv ones, they are only here for the
667163953Srrs		 * accounting/select.
668163953Srrs		 */
669167695Srrs		SCTP_SB_CLEAR(so->so_rcv);
670167695Srrs		/* Now null out the reference, we are completely detached. */
671163953Srrs		so->so_pcb = NULL;
672163953Srrs		SOCK_UNLOCK(so);
673163953Srrs	} else {
674163953Srrs		flags = inp->sctp_flags;
675163953Srrs		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
676163953Srrs			goto sctp_must_try_again;
677163953Srrs		}
678163953Srrs	}
679163953Srrs	return;
680163953Srrs
681163953Srrs}
682163953Srrs
683167598Srrs/* This could be made common with sctp_detach() since they are identical */
684163953Srrs
685163953Srrsstatic int
686163953Srrssctp6_disconnect(struct socket *so)
687163953Srrs{
688163953Srrs	struct sctp_inpcb *inp;
689163953Srrs
690163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
691163953Srrs	if (inp == NULL) {
692163953Srrs		return (ENOTCONN);
693163953Srrs	}
694163953Srrs	SCTP_INP_RLOCK(inp);
695163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
696166675Srrs		if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
697163953Srrs			/* No connection */
698163953Srrs			SCTP_INP_RUNLOCK(inp);
699163953Srrs			return (ENOTCONN);
700163953Srrs		} else {
701163953Srrs			int some_on_streamwheel = 0;
702163953Srrs			struct sctp_association *asoc;
703163953Srrs			struct sctp_tcb *stcb;
704163953Srrs
705163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
706163953Srrs			if (stcb == NULL) {
707163953Srrs				SCTP_INP_RUNLOCK(inp);
708163953Srrs				return (EINVAL);
709163953Srrs			}
710163953Srrs			SCTP_TCB_LOCK(stcb);
711163953Srrs			asoc = &stcb->asoc;
712163953Srrs			if (((so->so_options & SO_LINGER) &&
713163953Srrs			    (so->so_linger == 0)) ||
714163953Srrs			    (so->so_rcv.sb_cc > 0)) {
715163953Srrs				if (SCTP_GET_STATE(asoc) !=
716163953Srrs				    SCTP_STATE_COOKIE_WAIT) {
717163953Srrs					/* Left with Data unread */
718163953Srrs					struct mbuf *err;
719163953Srrs
720163953Srrs					err = NULL;
721163953Srrs					MGET(err, M_DONTWAIT, MT_DATA);
722163953Srrs					if (err) {
723163953Srrs						/*
724163953Srrs						 * Fill in the user
725163953Srrs						 * initiated abort
726163953Srrs						 */
727163953Srrs						struct sctp_paramhdr *ph;
728163953Srrs
729163953Srrs						ph = mtod(err, struct sctp_paramhdr *);
730165647Srrs						SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
731163953Srrs						ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
732165647Srrs						ph->param_length = htons(SCTP_BUF_LEN(err));
733163953Srrs					}
734163953Srrs					sctp_send_abort_tcb(stcb, err);
735163953Srrs					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
736163953Srrs				}
737163953Srrs				SCTP_INP_RUNLOCK(inp);
738163953Srrs				if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
739163953Srrs				    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
740163953Srrs					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
741163953Srrs				}
742165220Srrs				sctp_free_assoc(inp, stcb, SCTP_DONOT_SETSCOPE,
743165220Srrs				    SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_2);
744163953Srrs				/* No unlock tcb assoc is gone */
745163953Srrs				return (0);
746163953Srrs			}
747163953Srrs			if (!TAILQ_EMPTY(&asoc->out_wheel)) {
748163953Srrs				/* Check to see if some data queued */
749163953Srrs				struct sctp_stream_out *outs;
750163953Srrs
751163953Srrs				TAILQ_FOREACH(outs, &asoc->out_wheel,
752163953Srrs				    next_spoke) {
753163953Srrs					if (!TAILQ_EMPTY(&outs->outqueue)) {
754163953Srrs						some_on_streamwheel = 1;
755163953Srrs						break;
756163953Srrs					}
757163953Srrs				}
758163953Srrs			}
759163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
760163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue) &&
761163953Srrs			    (some_on_streamwheel == 0)) {
762163953Srrs				/* nothing queued to send, so I'm done... */
763163953Srrs				if ((SCTP_GET_STATE(asoc) !=
764163953Srrs				    SCTP_STATE_SHUTDOWN_SENT) &&
765163953Srrs				    (SCTP_GET_STATE(asoc) !=
766163953Srrs				    SCTP_STATE_SHUTDOWN_ACK_SENT)) {
767163953Srrs					/* only send SHUTDOWN the first time */
768163953Srrs					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
769163953Srrs					sctp_chunk_output(stcb->sctp_ep, stcb, 1);
770166675Srrs					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
771166675Srrs					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
772166675Srrs						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
773166675Srrs					}
774163953Srrs					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
775163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
776163953Srrs					    stcb->sctp_ep, stcb,
777163953Srrs					    asoc->primary_destination);
778163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
779163953Srrs					    stcb->sctp_ep, stcb,
780163953Srrs					    asoc->primary_destination);
781163953Srrs				}
782163953Srrs			} else {
783163953Srrs				/*
784163953Srrs				 * we still got (or just got) data to send,
785163953Srrs				 * so set SHUTDOWN_PENDING
786163953Srrs				 */
787163953Srrs				/*
788163953Srrs				 * XXX sockets draft says that MSG_EOF
789163953Srrs				 * should be sent with no data.  currently,
790163953Srrs				 * we will allow user data to be sent first
791163953Srrs				 * and move to SHUTDOWN-PENDING
792163953Srrs				 */
793163953Srrs				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
794163953Srrs			}
795163953Srrs			SCTP_TCB_UNLOCK(stcb);
796163953Srrs			SCTP_INP_RUNLOCK(inp);
797163953Srrs			return (0);
798163953Srrs		}
799163953Srrs	} else {
800163953Srrs		/* UDP model does not support this */
801163953Srrs		SCTP_INP_RUNLOCK(inp);
802163953Srrs		return EOPNOTSUPP;
803163953Srrs	}
804163953Srrs}
805163953Srrs
806163953Srrsint
807163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
808163953Srrs    struct mbuf *control, struct thread *p);
809163953Srrs
810163953Srrs
811163953Srrs
812163953Srrsstatic int
813163953Srrssctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
814163953Srrs    struct mbuf *control, struct thread *p)
815163953Srrs{
816163953Srrs	struct sctp_inpcb *inp;
817163953Srrs	struct inpcb *in_inp;
818163953Srrs	struct in6pcb *inp6;
819163953Srrs
820163953Srrs#ifdef INET
821163953Srrs	struct sockaddr_in6 *sin6;
822163953Srrs
823163953Srrs#endif				/* INET */
824163953Srrs	/* No SPL needed since sctp_output does this */
825163953Srrs
826163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
827163953Srrs	if (inp == NULL) {
828163953Srrs		if (control) {
829163953Srrs			m_freem(control);
830163953Srrs			control = NULL;
831163953Srrs		}
832163953Srrs		m_freem(m);
833163953Srrs		return EINVAL;
834163953Srrs	}
835163953Srrs	in_inp = (struct inpcb *)inp;
836163953Srrs	inp6 = (struct in6pcb *)inp;
837163953Srrs	/*
838163953Srrs	 * For the TCP model we may get a NULL addr, if we are a connected
839163953Srrs	 * socket thats ok.
840163953Srrs	 */
841163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
842163953Srrs	    (addr == NULL)) {
843163953Srrs		goto connected_type;
844163953Srrs	}
845163953Srrs	if (addr == NULL) {
846163953Srrs		m_freem(m);
847163953Srrs		if (control) {
848163953Srrs			m_freem(control);
849163953Srrs			control = NULL;
850163953Srrs		}
851163953Srrs		return (EDESTADDRREQ);
852163953Srrs	}
853163953Srrs#ifdef INET
854163953Srrs	sin6 = (struct sockaddr_in6 *)addr;
855166023Srrs	if (SCTP_IPV6_V6ONLY(inp6)) {
856163953Srrs		/*
857163953Srrs		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
858163953Srrs		 * v4 addr or v4-mapped addr
859163953Srrs		 */
860163953Srrs		if (addr->sa_family == AF_INET) {
861163953Srrs			return EINVAL;
862163953Srrs		}
863163953Srrs		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
864163953Srrs			return EINVAL;
865163953Srrs		}
866163953Srrs	}
867163953Srrs	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
868163953Srrs		if (!ip6_v6only) {
869163953Srrs			struct sockaddr_in sin;
870163953Srrs
871163953Srrs			/* convert v4-mapped into v4 addr and send */
872163953Srrs			in6_sin6_2_sin(&sin, sin6);
873163953Srrs			return sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
874163953Srrs			    control, p);
875163953Srrs		} else {
876163953Srrs			/* mapped addresses aren't enabled */
877163953Srrs			return EINVAL;
878163953Srrs		}
879163953Srrs	}
880163953Srrs#endif				/* INET */
881163953Srrsconnected_type:
882163953Srrs	/* now what about control */
883163953Srrs	if (control) {
884163953Srrs		if (inp->control) {
885163953Srrs			printf("huh? control set?\n");
886163953Srrs			m_freem(inp->control);
887163953Srrs			inp->control = NULL;
888163953Srrs		}
889163953Srrs		inp->control = control;
890163953Srrs	}
891163953Srrs	/* Place the data */
892163953Srrs	if (inp->pkt) {
893165647Srrs		SCTP_BUF_NEXT(inp->pkt_last) = m;
894163953Srrs		inp->pkt_last = m;
895163953Srrs	} else {
896163953Srrs		inp->pkt_last = inp->pkt = m;
897163953Srrs	}
898163953Srrs	if (
899163953Srrs	/* FreeBSD and MacOSX uses a flag passed */
900163953Srrs	    ((flags & PRUS_MORETOCOME) == 0)
901163953Srrs	    ) {
902163953Srrs		/*
903163953Srrs		 * note with the current version this code will only be used
904163953Srrs		 * by OpenBSD, NetBSD and FreeBSD have methods for
905163953Srrs		 * re-defining sosend() to use sctp_sosend().  One can
906163953Srrs		 * optionaly switch back to this code (by changing back the
907163953Srrs		 * defininitions but this is not advisable.
908163953Srrs		 */
909163953Srrs		int ret;
910163953Srrs
911163953Srrs		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
912163953Srrs		inp->pkt = NULL;
913163953Srrs		inp->control = NULL;
914163953Srrs		return (ret);
915163953Srrs	} else {
916163953Srrs		return (0);
917163953Srrs	}
918163953Srrs}
919163953Srrs
920163953Srrsstatic int
921163953Srrssctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
922163953Srrs{
923167598Srrs	uint32_t vrf_id;
924163953Srrs	int error = 0;
925163953Srrs	struct sctp_inpcb *inp;
926163953Srrs	struct in6pcb *inp6;
927163953Srrs	struct sctp_tcb *stcb;
928163953Srrs
929163953Srrs#ifdef INET
930163953Srrs	struct sockaddr_in6 *sin6;
931163953Srrs	struct sockaddr_storage ss;
932163953Srrs
933163953Srrs#endif				/* INET */
934163953Srrs
935163953Srrs	inp6 = (struct in6pcb *)so->so_pcb;
936163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
937163953Srrs	if (inp == 0) {
938163953Srrs		return (ECONNRESET);	/* I made the same as TCP since we are
939163953Srrs					 * not setup? */
940163953Srrs	}
941168299Srrs	vrf_id = inp->def_vrf_id;
942163953Srrs	SCTP_ASOC_CREATE_LOCK(inp);
943163953Srrs	SCTP_INP_RLOCK(inp);
944163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
945163953Srrs	    SCTP_PCB_FLAGS_UNBOUND) {
946163953Srrs		/* Bind a ephemeral port */
947163953Srrs		SCTP_INP_RUNLOCK(inp);
948163953Srrs		error = sctp6_bind(so, NULL, p);
949163953Srrs		if (error) {
950163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
951163953Srrs
952163953Srrs			return (error);
953163953Srrs		}
954163953Srrs		SCTP_INP_RLOCK(inp);
955163953Srrs	}
956163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
957163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
958163953Srrs		/* We are already connected AND the TCP model */
959163953Srrs		SCTP_INP_RUNLOCK(inp);
960163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
961163953Srrs		return (EADDRINUSE);
962163953Srrs	}
963163953Srrs#ifdef INET
964163953Srrs	sin6 = (struct sockaddr_in6 *)addr;
965166023Srrs	if (SCTP_IPV6_V6ONLY(inp6)) {
966163953Srrs		/*
967163953Srrs		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
968163953Srrs		 * addr or v4-mapped addr
969163953Srrs		 */
970163953Srrs		if (addr->sa_family == AF_INET) {
971163953Srrs			SCTP_INP_RUNLOCK(inp);
972163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
973163953Srrs			return EINVAL;
974163953Srrs		}
975163953Srrs		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
976163953Srrs			SCTP_INP_RUNLOCK(inp);
977163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
978163953Srrs			return EINVAL;
979163953Srrs		}
980163953Srrs	}
981163953Srrs	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
982163953Srrs		if (!ip6_v6only) {
983163953Srrs			/* convert v4-mapped into v4 addr */
984163953Srrs			in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
985163953Srrs			addr = (struct sockaddr *)&ss;
986163953Srrs		} else {
987163953Srrs			/* mapped addresses aren't enabled */
988163953Srrs			SCTP_INP_RUNLOCK(inp);
989163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
990163953Srrs			return EINVAL;
991163953Srrs		}
992163953Srrs	} else
993163953Srrs#endif				/* INET */
994163953Srrs		addr = addr;	/* for true v6 address case */
995163953Srrs
996163953Srrs	/* Now do we connect? */
997163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
998163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
999163953Srrs		if (stcb)
1000163953Srrs			SCTP_TCB_UNLOCK(stcb);
1001163953Srrs		SCTP_INP_RUNLOCK(inp);
1002163953Srrs	} else {
1003163953Srrs		SCTP_INP_RUNLOCK(inp);
1004163953Srrs		SCTP_INP_WLOCK(inp);
1005163953Srrs		SCTP_INP_INCR_REF(inp);
1006163953Srrs		SCTP_INP_WUNLOCK(inp);
1007163953Srrs		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1008163953Srrs		if (stcb == NULL) {
1009163953Srrs			SCTP_INP_WLOCK(inp);
1010163953Srrs			SCTP_INP_DECR_REF(inp);
1011163953Srrs			SCTP_INP_WUNLOCK(inp);
1012163953Srrs		}
1013163953Srrs	}
1014163953Srrs
1015163953Srrs	if (stcb != NULL) {
1016163953Srrs		/* Already have or am bring up an association */
1017163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
1018163953Srrs		SCTP_TCB_UNLOCK(stcb);
1019163953Srrs		return (EALREADY);
1020163953Srrs	}
1021163953Srrs	/* We are GOOD to go */
1022167598Srrs	stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id);
1023163953Srrs	SCTP_ASOC_CREATE_UNLOCK(inp);
1024163953Srrs	if (stcb == NULL) {
1025163953Srrs		/* Gak! no memory */
1026163953Srrs		return (error);
1027163953Srrs	}
1028163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1029163953Srrs		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1030163953Srrs		/* Set the connected flag so we can queue data */
1031163953Srrs		soisconnecting(so);
1032163953Srrs	}
1033163953Srrs	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1034163953Srrs	SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1035163953Srrs
1036163953Srrs	/* initialize authentication parameters for the assoc */
1037163953Srrs	sctp_initialize_auth_params(inp, stcb);
1038163953Srrs
1039163953Srrs	sctp_send_initiate(inp, stcb);
1040163953Srrs	SCTP_TCB_UNLOCK(stcb);
1041163953Srrs	return error;
1042163953Srrs}
1043163953Srrs
1044163953Srrsstatic int
1045163953Srrssctp6_getaddr(struct socket *so, struct sockaddr **addr)
1046163953Srrs{
1047163953Srrs	struct sockaddr_in6 *sin6;
1048163953Srrs	struct sctp_inpcb *inp;
1049167598Srrs	uint32_t vrf_id;
1050167598Srrs	struct sctp_ifa *sctp_ifa;
1051163953Srrs
1052163953Srrs	int error;
1053163953Srrs
1054163953Srrs	/*
1055163953Srrs	 * Do the malloc first in case it blocks.
1056163953Srrs	 */
1057163953Srrs	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1058163953Srrs	sin6->sin6_family = AF_INET6;
1059163953Srrs	sin6->sin6_len = sizeof(*sin6);
1060163953Srrs
1061163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1062163953Srrs	if (inp == NULL) {
1063163953Srrs		SCTP_FREE_SONAME(sin6);
1064163953Srrs		return ECONNRESET;
1065163953Srrs	}
1066163953Srrs	SCTP_INP_RLOCK(inp);
1067163953Srrs	sin6->sin6_port = inp->sctp_lport;
1068163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1069163953Srrs		/* For the bound all case you get back 0 */
1070163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1071163953Srrs			struct sctp_tcb *stcb;
1072163953Srrs			struct sockaddr_in6 *sin_a6;
1073163953Srrs			struct sctp_nets *net;
1074163953Srrs			int fnd;
1075163953Srrs
1076163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1077163953Srrs			if (stcb == NULL) {
1078163953Srrs				goto notConn6;
1079163953Srrs			}
1080163953Srrs			fnd = 0;
1081163953Srrs			sin_a6 = NULL;
1082163953Srrs			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1083163953Srrs				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1084164085Srrs				if (sin_a6 == NULL)
1085164085Srrs					/* this will make coverity happy */
1086164085Srrs					continue;
1087164085Srrs
1088163953Srrs				if (sin_a6->sin6_family == AF_INET6) {
1089163953Srrs					fnd = 1;
1090163953Srrs					break;
1091163953Srrs				}
1092163953Srrs			}
1093163953Srrs			if ((!fnd) || (sin_a6 == NULL)) {
1094163953Srrs				/* punt */
1095163953Srrs				goto notConn6;
1096163953Srrs			}
1097168299Srrs			vrf_id = inp->def_vrf_id;
1098168299Srrs			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1099167598Srrs			if (sctp_ifa) {
1100167598Srrs				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1101167598Srrs			}
1102163953Srrs		} else {
1103163953Srrs			/* For the bound all case you get back 0 */
1104163953Srrs	notConn6:
1105163953Srrs			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1106163953Srrs		}
1107163953Srrs	} else {
1108163953Srrs		/* Take the first IPv6 address in the list */
1109163953Srrs		struct sctp_laddr *laddr;
1110163953Srrs		int fnd = 0;
1111163953Srrs
1112163953Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1113167598Srrs			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1114163953Srrs				struct sockaddr_in6 *sin_a;
1115163953Srrs
1116167598Srrs				sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1117163953Srrs				sin6->sin6_addr = sin_a->sin6_addr;
1118163953Srrs				fnd = 1;
1119163953Srrs				break;
1120163953Srrs			}
1121163953Srrs		}
1122163953Srrs		if (!fnd) {
1123163953Srrs			SCTP_FREE_SONAME(sin6);
1124163953Srrs			SCTP_INP_RUNLOCK(inp);
1125163953Srrs			return ENOENT;
1126163953Srrs		}
1127163953Srrs	}
1128163953Srrs	SCTP_INP_RUNLOCK(inp);
1129163953Srrs	/* Scoping things for v6 */
1130164085Srrs	if ((error = sa6_recoverscope(sin6)) != 0) {
1131164085Srrs		SCTP_FREE_SONAME(sin6);
1132163953Srrs		return (error);
1133164085Srrs	}
1134163953Srrs	(*addr) = (struct sockaddr *)sin6;
1135163953Srrs	return (0);
1136163953Srrs}
1137163953Srrs
1138163953Srrsstatic int
1139163953Srrssctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1140163953Srrs{
1141163953Srrs	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1142163953Srrs	int fnd;
1143163953Srrs	struct sockaddr_in6 *sin_a6;
1144163953Srrs	struct sctp_inpcb *inp;
1145163953Srrs	struct sctp_tcb *stcb;
1146163953Srrs	struct sctp_nets *net;
1147163953Srrs
1148163953Srrs	int error;
1149163953Srrs
1150163953Srrs	/*
1151163953Srrs	 * Do the malloc first in case it blocks.
1152163953Srrs	 */
1153163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1154163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1155163953Srrs		/* UDP type and listeners will drop out here */
1156163953Srrs		return (ENOTCONN);
1157163953Srrs	}
1158163953Srrs	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1159163953Srrs	sin6->sin6_family = AF_INET6;
1160163953Srrs	sin6->sin6_len = sizeof(*sin6);
1161163953Srrs
1162163953Srrs	/* We must recapture incase we blocked */
1163163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1164163953Srrs	if (inp == NULL) {
1165163953Srrs		SCTP_FREE_SONAME(sin6);
1166163953Srrs		return ECONNRESET;
1167163953Srrs	}
1168163953Srrs	SCTP_INP_RLOCK(inp);
1169163953Srrs	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1170163953Srrs	if (stcb)
1171163953Srrs		SCTP_TCB_LOCK(stcb);
1172163953Srrs	SCTP_INP_RUNLOCK(inp);
1173163953Srrs	if (stcb == NULL) {
1174163953Srrs		SCTP_FREE_SONAME(sin6);
1175163953Srrs		return ECONNRESET;
1176163953Srrs	}
1177163953Srrs	fnd = 0;
1178163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1179163953Srrs		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1180163953Srrs		if (sin_a6->sin6_family == AF_INET6) {
1181163953Srrs			fnd = 1;
1182163953Srrs			sin6->sin6_port = stcb->rport;
1183163953Srrs			sin6->sin6_addr = sin_a6->sin6_addr;
1184163953Srrs			break;
1185163953Srrs		}
1186163953Srrs	}
1187163953Srrs	SCTP_TCB_UNLOCK(stcb);
1188163953Srrs	if (!fnd) {
1189163953Srrs		/* No IPv4 address */
1190163953Srrs		SCTP_FREE_SONAME(sin6);
1191163953Srrs		return ENOENT;
1192163953Srrs	}
1193163953Srrs	if ((error = sa6_recoverscope(sin6)) != 0)
1194163953Srrs		return (error);
1195163953Srrs	*addr = (struct sockaddr *)sin6;
1196163953Srrs	return (0);
1197163953Srrs}
1198163953Srrs
1199163953Srrsstatic int
1200163953Srrssctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1201163953Srrs{
1202163953Srrs	struct sockaddr *addr;
1203163953Srrs	struct in6pcb *inp6 = sotoin6pcb(so);
1204166086Srrs	int error;
1205163953Srrs
1206163953Srrs	if (inp6 == NULL)
1207163953Srrs		return EINVAL;
1208163953Srrs
1209163953Srrs	/* allow v6 addresses precedence */
1210163953Srrs	error = sctp6_getaddr(so, nam);
1211163953Srrs	if (error) {
1212163953Srrs		/* try v4 next if v6 failed */
1213163953Srrs		error = sctp_ingetaddr(so, nam);
1214163953Srrs		if (error) {
1215163953Srrs			return (error);
1216163953Srrs		}
1217163953Srrs		addr = *nam;
1218163953Srrs		/* if I'm V6ONLY, convert it to v4-mapped */
1219166023Srrs		if (SCTP_IPV6_V6ONLY(inp6)) {
1220163953Srrs			struct sockaddr_in6 sin6;
1221163953Srrs
1222163953Srrs			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1223163953Srrs			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1224163953Srrs		}
1225163953Srrs	}
1226163953Srrs	return (error);
1227163953Srrs}
1228163953Srrs
1229163953Srrs
1230163953Srrsstatic int
1231163953Srrssctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1232163953Srrs{
1233163953Srrs	struct sockaddr *addr = *nam;
1234163953Srrs	struct in6pcb *inp6 = sotoin6pcb(so);
1235166086Srrs	int error;
1236163953Srrs
1237163953Srrs	if (inp6 == NULL)
1238163953Srrs		return EINVAL;
1239163953Srrs
1240163953Srrs	/* allow v6 addresses precedence */
1241163953Srrs	error = sctp6_peeraddr(so, nam);
1242163953Srrs	if (error) {
1243163953Srrs		/* try v4 next if v6 failed */
1244163953Srrs		error = sctp_peeraddr(so, nam);
1245163953Srrs		if (error) {
1246163953Srrs			return (error);
1247163953Srrs		}
1248163953Srrs		/* if I'm V6ONLY, convert it to v4-mapped */
1249166023Srrs		if (SCTP_IPV6_V6ONLY(inp6)) {
1250163953Srrs			struct sockaddr_in6 sin6;
1251163953Srrs
1252163953Srrs			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1253163953Srrs			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1254163953Srrs		}
1255163953Srrs	}
1256163953Srrs	return error;
1257163953Srrs}
1258163953Srrs
1259163953Srrsstruct pr_usrreqs sctp6_usrreqs = {
1260163953Srrs	.pru_abort = sctp6_abort,
1261163953Srrs	.pru_accept = sctp_accept,
1262163953Srrs	.pru_attach = sctp6_attach,
1263163953Srrs	.pru_bind = sctp6_bind,
1264163953Srrs	.pru_connect = sctp6_connect,
1265163953Srrs	.pru_control = in6_control,
1266163953Srrs	.pru_close = sctp6_close,
1267163953Srrs	.pru_detach = sctp6_close,
1268163953Srrs	.pru_sopoll = sopoll_generic,
1269163953Srrs	.pru_disconnect = sctp6_disconnect,
1270163953Srrs	.pru_listen = sctp_listen,
1271163953Srrs	.pru_peeraddr = sctp6_getpeeraddr,
1272163953Srrs	.pru_send = sctp6_send,
1273163953Srrs	.pru_shutdown = sctp_shutdown,
1274163953Srrs	.pru_sockaddr = sctp6_in6getaddr,
1275163953Srrs	.pru_sosend = sctp_sosend,
1276163953Srrs	.pru_soreceive = sctp_soreceive
1277163953Srrs};
1278