xref: /freebsd/sys/netinet6/sctp6_usrreq.c (revision caeae63f97ec54833d17a36032db1590796913a8)
1f8829a4aSRandall Stewart /*-
2b1006367SRandall Stewart  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5f8829a4aSRandall Stewart  *
6f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
7f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
8f8829a4aSRandall Stewart  *
9f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
10f8829a4aSRandall Stewart  *    this list of conditions and the following disclaimer.
11f8829a4aSRandall Stewart  *
12f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
13f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
14f8829a4aSRandall Stewart  *    the documentation and/or other materials provided with the distribution.
15f8829a4aSRandall Stewart  *
16f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
18f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
19f8829a4aSRandall Stewart  *
20f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
31f8829a4aSRandall Stewart  */
32b48287a3SDavid E. O'Brien 
3373932c69SRandall Stewart #include <sys/cdefs.h>
34f8829a4aSRandall Stewart __FBSDID("$FreeBSD$");
35f8829a4aSRandall Stewart 
36f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
375e20b91dSMichael Tuexen #ifdef INET6
3893164cf9SRandall Stewart #include <sys/proc.h>
39f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
40f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
41f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
42e0e00a4dSMichael Tuexen #ifdef INET6
43c105859eSRandall Stewart #include <netinet6/sctp6_var.h>
44c105859eSRandall Stewart #endif
4542551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
46f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
4793164cf9SRandall Stewart #include <netinet/sctp_uio.h>
4893164cf9SRandall Stewart #include <netinet/sctp_asconf.h>
4993164cf9SRandall Stewart #include <netinet/sctputil.h>
5093164cf9SRandall Stewart #include <netinet/sctp_indata.h>
5193164cf9SRandall Stewart #include <netinet/sctp_timer.h>
5293164cf9SRandall Stewart #include <netinet/sctp_auth.h>
53c105859eSRandall Stewart #include <netinet/sctp_input.h>
54c105859eSRandall Stewart #include <netinet/sctp_output.h>
55207304d4SRandall Stewart #include <netinet/sctp_bsd_addr.h>
56a99b6783SRandall Stewart #include <netinet/sctp_crc32.h>
57b3f1ea41SRandall Stewart #include <netinet/udp.h>
58f8829a4aSRandall Stewart 
59b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
602cb64cb2SGeorge V. Neville-Neil #include <netipsec/ipsec.h>
61e0e00a4dSMichael Tuexen #ifdef INET6
622cb64cb2SGeorge V. Neville-Neil #include <netipsec/ipsec6.h>
632cb64cb2SGeorge V. Neville-Neil #endif				/* INET6 */
64b2630c29SGeorge V. Neville-Neil #endif				/* IPSEC */
65f8829a4aSRandall Stewart 
6693164cf9SRandall Stewart extern struct protosw inetsw[];
67f8829a4aSRandall Stewart 
68f8829a4aSRandall Stewart int
693a51a264SMichael Tuexen sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
70f8829a4aSRandall Stewart {
71139bc87fSRandall Stewart 	struct mbuf *m;
726dc5aabcSMichael Tuexen 	int iphlen;
73a8775ad9SMichael Tuexen 	uint32_t vrf_id;
746dc5aabcSMichael Tuexen 	uint8_t ecn_bits;
75b1754ad1SMichael Tuexen 	struct sockaddr_in6 src, dst;
76f8829a4aSRandall Stewart 	struct ip6_hdr *ip6;
77f8829a4aSRandall Stewart 	struct sctphdr *sh;
78f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
796dc5aabcSMichael Tuexen 	int length, offset;
809c7635e1SMichael Tuexen 
819c7635e1SMichael Tuexen #if !defined(SCTP_WITH_NO_CSUM)
82a8775ad9SMichael Tuexen 	uint8_t compute_crc;
839c7635e1SMichael Tuexen 
849c7635e1SMichael Tuexen #endif
85a8775ad9SMichael Tuexen 	uint32_t mflowid;
86457b4b88SMichael Tuexen 	uint8_t mflowtype;
87f8829a4aSRandall Stewart 
886dc5aabcSMichael Tuexen 	iphlen = *offp;
8917205eccSRandall Stewart 	if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
9017205eccSRandall Stewart 		SCTP_RELEASE_PKT(*i_pak);
916dc5aabcSMichael Tuexen 		return (IPPROTO_DONE);
9217205eccSRandall Stewart 	}
93c105859eSRandall Stewart 	m = SCTP_HEADER_TO_CHAIN(*i_pak);
946dc5aabcSMichael Tuexen #ifdef SCTP_MBUF_LOGGING
956dc5aabcSMichael Tuexen 	/* Log in any input mbufs */
966dc5aabcSMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
976dc5aabcSMichael Tuexen 		struct mbuf *mat;
98c105859eSRandall Stewart 
996dc5aabcSMichael Tuexen 		for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
1006dc5aabcSMichael Tuexen 			if (SCTP_BUF_IS_EXTENDED(mat)) {
1016dc5aabcSMichael Tuexen 				sctp_log_mb(mat, SCTP_MBUF_INPUT);
1026dc5aabcSMichael Tuexen 			}
1036dc5aabcSMichael Tuexen 		}
1046dc5aabcSMichael Tuexen 	}
1056dc5aabcSMichael Tuexen #endif
106207304d4SRandall Stewart #ifdef SCTP_PACKET_LOGGING
107f9384252SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
108f9384252SMichael Tuexen 		sctp_packet_log(m);
109f9384252SMichael Tuexen 	}
110207304d4SRandall Stewart #endif
111a8775ad9SMichael Tuexen 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
1121a94cdbeSMichael Tuexen 	    "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
113a8775ad9SMichael Tuexen 	    m->m_pkthdr.len,
114a8775ad9SMichael Tuexen 	    if_name(m->m_pkthdr.rcvif),
1151a94cdbeSMichael Tuexen 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
116f30ac432SMichael Tuexen 	mflowid = m->m_pkthdr.flowid;
117457b4b88SMichael Tuexen 	mflowtype = M_HASHTYPE_GET(m);
1186dc5aabcSMichael Tuexen 	SCTP_STAT_INCR(sctps_recvpackets);
1196dc5aabcSMichael Tuexen 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
1206dc5aabcSMichael Tuexen 	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
1216dc5aabcSMichael Tuexen 	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
122a8775ad9SMichael Tuexen 	ip6 = mtod(m, struct ip6_hdr *);
1236dc5aabcSMichael Tuexen 	IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
1246dc5aabcSMichael Tuexen 	    (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
125f8829a4aSRandall Stewart 	if (sh == NULL) {
126f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_hdrops);
12760990c0cSMichael Tuexen 		return (IPPROTO_DONE);
128f8829a4aSRandall Stewart 	}
129f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
1306dc5aabcSMichael Tuexen 	offset -= sizeof(struct sctp_chunkhdr);
131b1754ad1SMichael Tuexen 	memset(&src, 0, sizeof(struct sockaddr_in6));
132b1754ad1SMichael Tuexen 	src.sin6_family = AF_INET6;
133b1754ad1SMichael Tuexen 	src.sin6_len = sizeof(struct sockaddr_in6);
134b1754ad1SMichael Tuexen 	src.sin6_port = sh->src_port;
135b1754ad1SMichael Tuexen 	src.sin6_addr = ip6->ip6_src;
136b1754ad1SMichael Tuexen 	if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
137a8775ad9SMichael Tuexen 		goto out;
138b1754ad1SMichael Tuexen 	}
139b1754ad1SMichael Tuexen 	memset(&dst, 0, sizeof(struct sockaddr_in6));
140b1754ad1SMichael Tuexen 	dst.sin6_family = AF_INET6;
141b1754ad1SMichael Tuexen 	dst.sin6_len = sizeof(struct sockaddr_in6);
142b1754ad1SMichael Tuexen 	dst.sin6_port = sh->dest_port;
143b1754ad1SMichael Tuexen 	dst.sin6_addr = ip6->ip6_dst;
144b1754ad1SMichael Tuexen 	if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
145a8775ad9SMichael Tuexen 		goto out;
146b1754ad1SMichael Tuexen 	}
1476dc5aabcSMichael Tuexen 	length = ntohs(ip6->ip6_plen) + iphlen;
1486dc5aabcSMichael Tuexen 	/* Validate mbuf chain length with IP payload length. */
149a8775ad9SMichael Tuexen 	if (SCTP_HEADER_LEN(m) != length) {
1506dc5aabcSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INPUT1,
151a8775ad9SMichael Tuexen 		    "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
1526dc5aabcSMichael Tuexen 		SCTP_STAT_INCR(sctps_hdrops);
153a8775ad9SMichael Tuexen 		goto out;
154f8829a4aSRandall Stewart 	}
1556dc5aabcSMichael Tuexen 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
156a8775ad9SMichael Tuexen 		goto out;
1576dc5aabcSMichael Tuexen 	}
158a8775ad9SMichael Tuexen 	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
1599c7635e1SMichael Tuexen #if defined(SCTP_WITH_NO_CSUM)
1609c7635e1SMichael Tuexen 	SCTP_STAT_INCR(sctps_recvnocrc);
1619c7635e1SMichael Tuexen #else
162a99b6783SRandall Stewart 	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
163a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvhwcrc);
164a8775ad9SMichael Tuexen 		compute_crc = 0;
165a8775ad9SMichael Tuexen 	} else {
166a99b6783SRandall Stewart 		SCTP_STAT_INCR(sctps_recvswcrc);
167a8775ad9SMichael Tuexen 		compute_crc = 1;
168f8829a4aSRandall Stewart 	}
1696dc5aabcSMichael Tuexen #endif
170b1754ad1SMichael Tuexen 	sctp_common_input_processing(&m, iphlen, offset, length,
171b1754ad1SMichael Tuexen 	    (struct sockaddr *)&src,
172b1754ad1SMichael Tuexen 	    (struct sockaddr *)&dst,
173a8775ad9SMichael Tuexen 	    sh, ch,
174a8775ad9SMichael Tuexen #if !defined(SCTP_WITH_NO_CSUM)
175a8775ad9SMichael Tuexen 	    compute_crc,
176a8775ad9SMichael Tuexen #endif
177a8775ad9SMichael Tuexen 	    ecn_bits,
178457b4b88SMichael Tuexen 	    mflowtype, mflowid,
179f30ac432SMichael Tuexen 	    vrf_id, port);
180a8775ad9SMichael Tuexen out:
1816dc5aabcSMichael Tuexen 	if (m) {
18217205eccSRandall Stewart 		sctp_m_freem(m);
1836dc5aabcSMichael Tuexen 	}
18460990c0cSMichael Tuexen 	return (IPPROTO_DONE);
185f8829a4aSRandall Stewart }
186f8829a4aSRandall Stewart 
187f8829a4aSRandall Stewart 
1883a51a264SMichael Tuexen int
1893a51a264SMichael Tuexen sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
1903a51a264SMichael Tuexen {
1913a51a264SMichael Tuexen 	return (sctp6_input_with_port(i_pak, offp, 0));
1923a51a264SMichael Tuexen }
1933a51a264SMichael Tuexen 
194f8829a4aSRandall Stewart static void
1951272577eSXin LI sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
1961272577eSXin LI     struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
197f8829a4aSRandall Stewart {
198bff64a4dSRandall Stewart 	uint32_t nxtsz;
199f8829a4aSRandall Stewart 
200f8829a4aSRandall Stewart 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
201f8829a4aSRandall Stewart 	    (icmp6 == NULL) || (sh == NULL)) {
202f8829a4aSRandall Stewart 		goto out;
203f8829a4aSRandall Stewart 	}
204f8829a4aSRandall Stewart 	/* First do we even look at it? */
205f8829a4aSRandall Stewart 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
206f8829a4aSRandall Stewart 		goto out;
207f8829a4aSRandall Stewart 
208f8829a4aSRandall Stewart 	if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
209f8829a4aSRandall Stewart 		/* not PACKET TO BIG */
210f8829a4aSRandall Stewart 		goto out;
211f8829a4aSRandall Stewart 	}
212f8829a4aSRandall Stewart 	/*
213f8829a4aSRandall Stewart 	 * ok we need to look closely. We could even get smarter and look at
214f8829a4aSRandall Stewart 	 * anyone that we sent to in case we get a different ICMP that tells
215f8829a4aSRandall Stewart 	 * us there is no way to reach a host, but for this impl, all we
216f8829a4aSRandall Stewart 	 * care about is MTU discovery.
217f8829a4aSRandall Stewart 	 */
218f8829a4aSRandall Stewart 	nxtsz = ntohl(icmp6->icmp6_mtu);
219f8829a4aSRandall Stewart 	/* Stop any PMTU timer */
220a5d547adSRandall Stewart 	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
221f8829a4aSRandall Stewart 
222f8829a4aSRandall Stewart 	/* Adjust destination size limit */
223f8829a4aSRandall Stewart 	if (net->mtu > nxtsz) {
224f8829a4aSRandall Stewart 		net->mtu = nxtsz;
225830d754dSRandall Stewart 		if (net->port) {
226830d754dSRandall Stewart 			net->mtu -= sizeof(struct udphdr);
227830d754dSRandall Stewart 		}
228f8829a4aSRandall Stewart 	}
229f8829a4aSRandall Stewart 	/* now what about the ep? */
230f8829a4aSRandall Stewart 	if (stcb->asoc.smallest_mtu > nxtsz) {
231f8829a4aSRandall Stewart 		struct sctp_tmit_chunk *chk;
232f8829a4aSRandall Stewart 
233f8829a4aSRandall Stewart 		/* Adjust that too */
234f8829a4aSRandall Stewart 		stcb->asoc.smallest_mtu = nxtsz;
235f8829a4aSRandall Stewart 		/* now off to subtract IP_DF flag if needed */
236f8829a4aSRandall Stewart 
237f8829a4aSRandall Stewart 		TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
238bff64a4dSRandall Stewart 			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
239f8829a4aSRandall Stewart 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
240f8829a4aSRandall Stewart 			}
241f8829a4aSRandall Stewart 		}
242f8829a4aSRandall Stewart 		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
243bff64a4dSRandall Stewart 			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
244f8829a4aSRandall Stewart 				/*
245f8829a4aSRandall Stewart 				 * For this guy we also mark for immediate
246f8829a4aSRandall Stewart 				 * resend since we sent to big of chunk
247f8829a4aSRandall Stewart 				 */
248f8829a4aSRandall Stewart 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
249f8829a4aSRandall Stewart 				if (chk->sent != SCTP_DATAGRAM_RESEND)
250f8829a4aSRandall Stewart 					stcb->asoc.sent_queue_retran_cnt++;
251f8829a4aSRandall Stewart 				chk->sent = SCTP_DATAGRAM_RESEND;
252f8829a4aSRandall Stewart 				chk->rec.data.doing_fast_retransmit = 0;
253f8829a4aSRandall Stewart 
254f8829a4aSRandall Stewart 				chk->sent = SCTP_DATAGRAM_RESEND;
255f8829a4aSRandall Stewart 				/* Clear any time so NO RTT is being done */
256f8829a4aSRandall Stewart 				chk->sent_rcv_time.tv_sec = 0;
257f8829a4aSRandall Stewart 				chk->sent_rcv_time.tv_usec = 0;
258f8829a4aSRandall Stewart 				stcb->asoc.total_flight -= chk->send_size;
259f8829a4aSRandall Stewart 				net->flight_size -= chk->send_size;
260f8829a4aSRandall Stewart 			}
261f8829a4aSRandall Stewart 		}
262f8829a4aSRandall Stewart 	}
263f8829a4aSRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
264f8829a4aSRandall Stewart out:
265ad81507eSRandall Stewart 	if (stcb) {
266f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
267f8829a4aSRandall Stewart 	}
268ad81507eSRandall Stewart }
269f8829a4aSRandall Stewart 
270f8829a4aSRandall Stewart 
27104ee05e8SRandall Stewart void
272851b7298SRandall Stewart sctp6_notify(struct sctp_inpcb *inp,
273851b7298SRandall Stewart     struct icmp6_hdr *icmph,
274851b7298SRandall Stewart     struct sctphdr *sh,
275851b7298SRandall Stewart     struct sockaddr *to,
276851b7298SRandall Stewart     struct sctp_tcb *stcb,
277851b7298SRandall Stewart     struct sctp_nets *net)
278851b7298SRandall Stewart {
279851b7298SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
280851b7298SRandall Stewart 	struct socket *so;
281851b7298SRandall Stewart 
282851b7298SRandall Stewart #endif
283a2b42326SMichael Tuexen 
284851b7298SRandall Stewart 	/* protection */
285851b7298SRandall Stewart 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
286851b7298SRandall Stewart 	    (sh == NULL) || (to == NULL)) {
287851b7298SRandall Stewart 		if (stcb)
288851b7298SRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
289851b7298SRandall Stewart 		return;
290851b7298SRandall Stewart 	}
291851b7298SRandall Stewart 	/* First job is to verify the vtag matches what I would send */
292851b7298SRandall Stewart 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
293851b7298SRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
294851b7298SRandall Stewart 		return;
295851b7298SRandall Stewart 	}
296851b7298SRandall Stewart 	if (icmph->icmp6_type != ICMP_UNREACH) {
297851b7298SRandall Stewart 		/* We only care about unreachable */
298851b7298SRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
299851b7298SRandall Stewart 		return;
300851b7298SRandall Stewart 	}
301851b7298SRandall Stewart 	if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
302851b7298SRandall Stewart 	    (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
303851b7298SRandall Stewart 	    (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
304851b7298SRandall Stewart 	    (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
305851b7298SRandall Stewart 	    (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
306851b7298SRandall Stewart 	    (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
307851b7298SRandall Stewart 	    (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
308851b7298SRandall Stewart 	    (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
309851b7298SRandall Stewart 
310851b7298SRandall Stewart 		/*
311851b7298SRandall Stewart 		 * Hmm reachablity problems we must examine closely. If its
312851b7298SRandall Stewart 		 * not reachable, we may have lost a network. Or if there is
313851b7298SRandall Stewart 		 * NO protocol at the other end named SCTP. well we consider
314851b7298SRandall Stewart 		 * it a OOTB abort.
315851b7298SRandall Stewart 		 */
316851b7298SRandall Stewart 		if (net->dest_state & SCTP_ADDR_REACHABLE) {
317851b7298SRandall Stewart 			/* Ok that destination is NOT reachable */
318851b7298SRandall Stewart 			net->dest_state &= ~SCTP_ADDR_REACHABLE;
319851b7298SRandall Stewart 			net->dest_state &= ~SCTP_ADDR_PF;
320851b7298SRandall Stewart 			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3211a5b7901SMichael Tuexen 			    stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
322851b7298SRandall Stewart 		}
323851b7298SRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
324851b7298SRandall Stewart 	} else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
325851b7298SRandall Stewart 	    (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
326851b7298SRandall Stewart 		/*
327851b7298SRandall Stewart 		 * Here the peer is either playing tricks on us, including
328851b7298SRandall Stewart 		 * an address that belongs to someone who does not support
329851b7298SRandall Stewart 		 * SCTP OR was a userland implementation that shutdown and
330851b7298SRandall Stewart 		 * now is dead. In either case treat it like a OOTB abort
331851b7298SRandall Stewart 		 * with no TCB
332851b7298SRandall Stewart 		 */
333410a3b1eSMichael Tuexen 		sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
334851b7298SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
335851b7298SRandall Stewart 		so = SCTP_INP_SO(inp);
336851b7298SRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
337851b7298SRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
338851b7298SRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
339851b7298SRandall Stewart 		SCTP_TCB_LOCK(stcb);
340851b7298SRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
341851b7298SRandall Stewart #endif
342851b7298SRandall Stewart 		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
343851b7298SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
344851b7298SRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
345851b7298SRandall Stewart 		/* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
346851b7298SRandall Stewart #endif
347851b7298SRandall Stewart 		/* no need to unlock here, since the TCB is gone */
348851b7298SRandall Stewart 	} else {
349851b7298SRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
350851b7298SRandall Stewart 	}
351851b7298SRandall Stewart }
352851b7298SRandall Stewart 
353851b7298SRandall Stewart 
354851b7298SRandall Stewart 
355f8829a4aSRandall Stewart void
3561272577eSXin LI sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
357f8829a4aSRandall Stewart {
358f8829a4aSRandall Stewart 	struct sctphdr sh;
359f8829a4aSRandall Stewart 	struct ip6ctlparam *ip6cp = NULL;
36042551e99SRandall Stewart 	uint32_t vrf_id;
361f8829a4aSRandall Stewart 
36242551e99SRandall Stewart 	vrf_id = SCTP_DEFAULT_VRFID;
36342551e99SRandall Stewart 
364f8829a4aSRandall Stewart 	if (pktdst->sa_family != AF_INET6 ||
365f8829a4aSRandall Stewart 	    pktdst->sa_len != sizeof(struct sockaddr_in6))
366f8829a4aSRandall Stewart 		return;
367f8829a4aSRandall Stewart 
368f8829a4aSRandall Stewart 	if ((unsigned)cmd >= PRC_NCMDS)
369f8829a4aSRandall Stewart 		return;
370f8829a4aSRandall Stewart 	if (PRC_IS_REDIRECT(cmd)) {
371f8829a4aSRandall Stewart 		d = NULL;
372f8829a4aSRandall Stewart 	} else if (inet6ctlerrmap[cmd] == 0) {
373f8829a4aSRandall Stewart 		return;
374f8829a4aSRandall Stewart 	}
375f8829a4aSRandall Stewart 	/* if the parameter is from icmp6, decode it. */
376f8829a4aSRandall Stewart 	if (d != NULL) {
377f8829a4aSRandall Stewart 		ip6cp = (struct ip6ctlparam *)d;
378f8829a4aSRandall Stewart 	} else {
379f8829a4aSRandall Stewart 		ip6cp = (struct ip6ctlparam *)NULL;
380f8829a4aSRandall Stewart 	}
381f8829a4aSRandall Stewart 
382f8829a4aSRandall Stewart 	if (ip6cp) {
383f8829a4aSRandall Stewart 		/*
384f8829a4aSRandall Stewart 		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
385f8829a4aSRandall Stewart 		 * valid.
386f8829a4aSRandall Stewart 		 */
387f8829a4aSRandall Stewart 		/* check if we can safely examine src and dst ports */
388f8829a4aSRandall Stewart 		struct sctp_inpcb *inp = NULL;
389f8829a4aSRandall Stewart 		struct sctp_tcb *stcb = NULL;
390f8829a4aSRandall Stewart 		struct sctp_nets *net = NULL;
391f8829a4aSRandall Stewart 		struct sockaddr_in6 final;
392f8829a4aSRandall Stewart 
393139bc87fSRandall Stewart 		if (ip6cp->ip6c_m == NULL)
394f8829a4aSRandall Stewart 			return;
395f8829a4aSRandall Stewart 
396f8829a4aSRandall Stewart 		bzero(&sh, sizeof(sh));
397f8829a4aSRandall Stewart 		bzero(&final, sizeof(final));
398f8829a4aSRandall Stewart 		inp = NULL;
399f8829a4aSRandall Stewart 		net = NULL;
400f8829a4aSRandall Stewart 		m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
401f8829a4aSRandall Stewart 		    (caddr_t)&sh);
402f8829a4aSRandall Stewart 		ip6cp->ip6c_src->sin6_port = sh.src_port;
403f8829a4aSRandall Stewart 		final.sin6_len = sizeof(final);
404f8829a4aSRandall Stewart 		final.sin6_family = AF_INET6;
405f8829a4aSRandall Stewart 		final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
406f8829a4aSRandall Stewart 		final.sin6_port = sh.dest_port;
407b1754ad1SMichael Tuexen 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&final,
408b1754ad1SMichael Tuexen 		    (struct sockaddr *)ip6cp->ip6c_src,
40942551e99SRandall Stewart 		    &inp, &net, 1, vrf_id);
410f8829a4aSRandall Stewart 		/* inp's ref-count increased && stcb locked */
411f8829a4aSRandall Stewart 		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
412f8829a4aSRandall Stewart 			if (cmd == PRC_MSGSIZE) {
413f8829a4aSRandall Stewart 				sctp6_notify_mbuf(inp,
414f8829a4aSRandall Stewart 				    ip6cp->ip6c_icmp6,
415f8829a4aSRandall Stewart 				    &sh,
416f8829a4aSRandall Stewart 				    stcb,
417f8829a4aSRandall Stewart 				    net);
418f8829a4aSRandall Stewart 				/* inp's ref-count reduced && stcb unlocked */
419f8829a4aSRandall Stewart 			} else {
420851b7298SRandall Stewart 				sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
421f8829a4aSRandall Stewart 				    (struct sockaddr *)&final,
422f8829a4aSRandall Stewart 				    stcb, net);
423f8829a4aSRandall Stewart 				/* inp's ref-count reduced && stcb unlocked */
424f8829a4aSRandall Stewart 			}
425f8829a4aSRandall Stewart 		} else {
426f8829a4aSRandall Stewart 			if (PRC_IS_REDIRECT(cmd) && inp) {
427f8829a4aSRandall Stewart 				in6_rtchange((struct in6pcb *)inp,
428f8829a4aSRandall Stewart 				    inet6ctlerrmap[cmd]);
429f8829a4aSRandall Stewart 			}
430f8829a4aSRandall Stewart 			if (inp) {
431f8829a4aSRandall Stewart 				/* reduce inp's ref-count */
432f8829a4aSRandall Stewart 				SCTP_INP_WLOCK(inp);
433f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
434f8829a4aSRandall Stewart 				SCTP_INP_WUNLOCK(inp);
435f8829a4aSRandall Stewart 			}
436f8829a4aSRandall Stewart 			if (stcb)
437f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
438f8829a4aSRandall Stewart 		}
439f8829a4aSRandall Stewart 	}
440f8829a4aSRandall Stewart }
441f8829a4aSRandall Stewart 
442f8829a4aSRandall Stewart /*
443f8829a4aSRandall Stewart  * this routine can probably be collasped into the one in sctp_userreq.c
444f8829a4aSRandall Stewart  * since they do the same thing and now we lookup with a sockaddr
445f8829a4aSRandall Stewart  */
446f8829a4aSRandall Stewart static int
447f8829a4aSRandall Stewart sctp6_getcred(SYSCTL_HANDLER_ARGS)
448f8829a4aSRandall Stewart {
44903b0b021SRandall Stewart 	struct xucred xuc;
450f8829a4aSRandall Stewart 	struct sockaddr_in6 addrs[2];
451f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
452f8829a4aSRandall Stewart 	struct sctp_nets *net;
453f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
45403b0b021SRandall Stewart 	int error;
45542551e99SRandall Stewart 	uint32_t vrf_id;
45642551e99SRandall Stewart 
45742551e99SRandall Stewart 	vrf_id = SCTP_DEFAULT_VRFID;
458f8829a4aSRandall Stewart 
45932f9753cSRobert Watson 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
460f8829a4aSRandall Stewart 	if (error)
461f8829a4aSRandall Stewart 		return (error);
462f8829a4aSRandall Stewart 
463c4739e2fSRandall Stewart 	if (req->newlen != sizeof(addrs)) {
464c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
465f8829a4aSRandall Stewart 		return (EINVAL);
466c4739e2fSRandall Stewart 	}
467c4739e2fSRandall Stewart 	if (req->oldlen != sizeof(struct ucred)) {
468c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
469f8829a4aSRandall Stewart 		return (EINVAL);
470c4739e2fSRandall Stewart 	}
471f8829a4aSRandall Stewart 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
472f8829a4aSRandall Stewart 	if (error)
473f8829a4aSRandall Stewart 		return (error);
474f8829a4aSRandall Stewart 
475b1754ad1SMichael Tuexen 	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
476b1754ad1SMichael Tuexen 	    sin6tosa(&addrs[0]),
47742551e99SRandall Stewart 	    &inp, &net, 1, vrf_id);
478f8829a4aSRandall Stewart 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
47903b0b021SRandall Stewart 		if ((inp != NULL) && (stcb == NULL)) {
48003b0b021SRandall Stewart 			/* reduce ref-count */
481f8829a4aSRandall Stewart 			SCTP_INP_WLOCK(inp);
482f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
48303b0b021SRandall Stewart 			goto cred_can_cont;
484f8829a4aSRandall Stewart 		}
485c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
48603b0b021SRandall Stewart 		error = ENOENT;
487f8829a4aSRandall Stewart 		goto out;
488f8829a4aSRandall Stewart 	}
489f8829a4aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
49003b0b021SRandall Stewart 	/*
49103b0b021SRandall Stewart 	 * We use the write lock here, only since in the error leg we need
49203b0b021SRandall Stewart 	 * it. If we used RLOCK, then we would have to
49303b0b021SRandall Stewart 	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
49403b0b021SRandall Stewart 	 * Better to use higher wlock.
49503b0b021SRandall Stewart 	 */
49603b0b021SRandall Stewart 	SCTP_INP_WLOCK(inp);
49703b0b021SRandall Stewart cred_can_cont:
49803b0b021SRandall Stewart 	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
49903b0b021SRandall Stewart 	if (error) {
50003b0b021SRandall Stewart 		SCTP_INP_WUNLOCK(inp);
50103b0b021SRandall Stewart 		goto out;
50203b0b021SRandall Stewart 	}
50303b0b021SRandall Stewart 	cru2x(inp->sctp_socket->so_cred, &xuc);
50403b0b021SRandall Stewart 	SCTP_INP_WUNLOCK(inp);
50503b0b021SRandall Stewart 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
506f8829a4aSRandall Stewart out:
507f8829a4aSRandall Stewart 	return (error);
508f8829a4aSRandall Stewart }
509f8829a4aSRandall Stewart 
510f8829a4aSRandall Stewart SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
511f8829a4aSRandall Stewart     0, 0,
512f8829a4aSRandall Stewart     sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
513f8829a4aSRandall Stewart 
514f8829a4aSRandall Stewart 
515f8829a4aSRandall Stewart /* This is the same as the sctp_abort() could be made common */
516f8829a4aSRandall Stewart static void
517f8829a4aSRandall Stewart sctp6_abort(struct socket *so)
518f8829a4aSRandall Stewart {
519f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
520f8829a4aSRandall Stewart 	uint32_t flags;
521f8829a4aSRandall Stewart 
522f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
523dea47f39SMichael Tuexen 	if (inp == NULL) {
524851b7298SRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
525f8829a4aSRandall Stewart 		return;
526851b7298SRandall Stewart 	}
527f8829a4aSRandall Stewart sctp_must_try_again:
528f8829a4aSRandall Stewart 	flags = inp->sctp_flags;
529f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
530f8829a4aSRandall Stewart 	sctp_log_closing(inp, NULL, 17);
531f8829a4aSRandall Stewart #endif
532f8829a4aSRandall Stewart 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
533f8829a4aSRandall Stewart 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
534f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
535f8829a4aSRandall Stewart 		sctp_log_closing(inp, NULL, 16);
536f8829a4aSRandall Stewart #endif
537b0552ae2SRandall Stewart 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
538b0552ae2SRandall Stewart 		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
539f8829a4aSRandall Stewart 		SOCK_LOCK(so);
540132dea7dSRandall Stewart 		SCTP_SB_CLEAR(so->so_snd);
541f8829a4aSRandall Stewart 		/*
542f8829a4aSRandall Stewart 		 * same for the rcv ones, they are only here for the
543f8829a4aSRandall Stewart 		 * accounting/select.
544f8829a4aSRandall Stewart 		 */
545132dea7dSRandall Stewart 		SCTP_SB_CLEAR(so->so_rcv);
546132dea7dSRandall Stewart 		/* Now null out the reference, we are completely detached. */
547f8829a4aSRandall Stewart 		so->so_pcb = NULL;
548f8829a4aSRandall Stewart 		SOCK_UNLOCK(so);
549f8829a4aSRandall Stewart 	} else {
550f8829a4aSRandall Stewart 		flags = inp->sctp_flags;
551f8829a4aSRandall Stewart 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
552f8829a4aSRandall Stewart 			goto sctp_must_try_again;
553f8829a4aSRandall Stewart 		}
554f8829a4aSRandall Stewart 	}
555f8829a4aSRandall Stewart 	return;
556f8829a4aSRandall Stewart }
557f8829a4aSRandall Stewart 
558f8829a4aSRandall Stewart static int
5597215cc1bSMichael Tuexen sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
560f8829a4aSRandall Stewart {
561f8829a4aSRandall Stewart 	struct in6pcb *inp6;
56293164cf9SRandall Stewart 	int error;
563f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
564f4c93d24SRandall Stewart 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
565f8829a4aSRandall Stewart 
566f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
567c4739e2fSRandall Stewart 	if (inp != NULL) {
568c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
56960990c0cSMichael Tuexen 		return (EINVAL);
570c4739e2fSRandall Stewart 	}
571f8829a4aSRandall Stewart 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
572b3f1ea41SRandall Stewart 		error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
573f8829a4aSRandall Stewart 		if (error)
57460990c0cSMichael Tuexen 			return (error);
575f8829a4aSRandall Stewart 	}
576f4c93d24SRandall Stewart 	error = sctp_inpcb_alloc(so, vrf_id);
577f8829a4aSRandall Stewart 	if (error)
57860990c0cSMichael Tuexen 		return (error);
579f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
580f4c93d24SRandall Stewart 	SCTP_INP_WLOCK(inp);
581f8829a4aSRandall Stewart 	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
582f8829a4aSRandall Stewart 	inp6 = (struct in6pcb *)inp;
583f8829a4aSRandall Stewart 
584f8829a4aSRandall Stewart 	inp6->inp_vflag |= INP_IPV6;
585f8829a4aSRandall Stewart 	inp6->in6p_hops = -1;	/* use kernel default */
586f8829a4aSRandall Stewart 	inp6->in6p_cksum = -1;	/* just to be sure */
587f8829a4aSRandall Stewart #ifdef INET
588f8829a4aSRandall Stewart 	/*
589f8829a4aSRandall Stewart 	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
590f8829a4aSRandall Stewart 	 * socket as well, because the socket may be bound to an IPv6
591f8829a4aSRandall Stewart 	 * wildcard address, which may match an IPv4-mapped IPv6 address.
592f8829a4aSRandall Stewart 	 */
593482444b4SRandall Stewart 	inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
594f8829a4aSRandall Stewart #endif
595f8829a4aSRandall Stewart 	/*
596f8829a4aSRandall Stewart 	 * Hmm what about the IPSEC stuff that is missing here but in
597f8829a4aSRandall Stewart 	 * sctp_attach()?
598f8829a4aSRandall Stewart 	 */
599f4c93d24SRandall Stewart 	SCTP_INP_WUNLOCK(inp);
60060990c0cSMichael Tuexen 	return (0);
601f8829a4aSRandall Stewart }
602f8829a4aSRandall Stewart 
603f8829a4aSRandall Stewart static int
604f8829a4aSRandall Stewart sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
605f8829a4aSRandall Stewart {
606f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
607f8829a4aSRandall Stewart 	struct in6pcb *inp6;
60893164cf9SRandall Stewart 	int error;
609f8829a4aSRandall Stewart 
610f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
611dea47f39SMichael Tuexen 	if (inp == NULL) {
612c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
61360990c0cSMichael Tuexen 		return (EINVAL);
614c4739e2fSRandall Stewart 	}
615d61a0ae0SRandall Stewart 	if (addr) {
616e6194c2eSMichael Tuexen 		switch (addr->sa_family) {
617e6194c2eSMichael Tuexen #ifdef INET
618e6194c2eSMichael Tuexen 		case AF_INET:
619e6194c2eSMichael Tuexen 			if (addr->sa_len != sizeof(struct sockaddr_in)) {
620c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
62160990c0cSMichael Tuexen 				return (EINVAL);
622d61a0ae0SRandall Stewart 			}
623e6194c2eSMichael Tuexen 			break;
624e6194c2eSMichael Tuexen #endif
625e6194c2eSMichael Tuexen #ifdef INET6
626e6194c2eSMichael Tuexen 		case AF_INET6:
627e6194c2eSMichael Tuexen 			if (addr->sa_len != sizeof(struct sockaddr_in6)) {
628e6194c2eSMichael Tuexen 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
62960990c0cSMichael Tuexen 				return (EINVAL);
630e6194c2eSMichael Tuexen 			}
631e6194c2eSMichael Tuexen 			break;
632e6194c2eSMichael Tuexen #endif
633e6194c2eSMichael Tuexen 		default:
634c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
63560990c0cSMichael Tuexen 			return (EINVAL);
636d61a0ae0SRandall Stewart 		}
637d61a0ae0SRandall Stewart 	}
638f8829a4aSRandall Stewart 	inp6 = (struct in6pcb *)inp;
639f8829a4aSRandall Stewart 	inp6->inp_vflag &= ~INP_IPV4;
640f8829a4aSRandall Stewart 	inp6->inp_vflag |= INP_IPV6;
64144b7479bSRandall Stewart 	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
642e6194c2eSMichael Tuexen 		switch (addr->sa_family) {
643e6194c2eSMichael Tuexen #ifdef INET
644e6194c2eSMichael Tuexen 		case AF_INET:
645f8829a4aSRandall Stewart 			/* binding v4 addr to v6 socket, so reset flags */
646f8829a4aSRandall Stewart 			inp6->inp_vflag |= INP_IPV4;
647f8829a4aSRandall Stewart 			inp6->inp_vflag &= ~INP_IPV6;
648e6194c2eSMichael Tuexen 			break;
649e6194c2eSMichael Tuexen #endif
650e6194c2eSMichael Tuexen #ifdef INET6
651e6194c2eSMichael Tuexen 		case AF_INET6:
652e6194c2eSMichael Tuexen 			{
653f8829a4aSRandall Stewart 				struct sockaddr_in6 *sin6_p;
654f8829a4aSRandall Stewart 
655f8829a4aSRandall Stewart 				sin6_p = (struct sockaddr_in6 *)addr;
656f8829a4aSRandall Stewart 
657f8829a4aSRandall Stewart 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
658f8829a4aSRandall Stewart 					inp6->inp_vflag |= INP_IPV4;
659e6194c2eSMichael Tuexen 				}
660e6194c2eSMichael Tuexen #ifdef INET
661e6194c2eSMichael Tuexen 				if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
662f8829a4aSRandall Stewart 					struct sockaddr_in sin;
663f8829a4aSRandall Stewart 
664f8829a4aSRandall Stewart 					in6_sin6_2_sin(&sin, sin6_p);
665f8829a4aSRandall Stewart 					inp6->inp_vflag |= INP_IPV4;
666f8829a4aSRandall Stewart 					inp6->inp_vflag &= ~INP_IPV6;
6671b649582SRandall Stewart 					error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
66860990c0cSMichael Tuexen 					return (error);
669f8829a4aSRandall Stewart 				}
670e6194c2eSMichael Tuexen #endif
671e6194c2eSMichael Tuexen 				break;
672e6194c2eSMichael Tuexen 			}
673e6194c2eSMichael Tuexen #endif
674e6194c2eSMichael Tuexen 		default:
675e6194c2eSMichael Tuexen 			break;
676f8829a4aSRandall Stewart 		}
677f8829a4aSRandall Stewart 	} else if (addr != NULL) {
678e6194c2eSMichael Tuexen 		struct sockaddr_in6 *sin6_p;
679e6194c2eSMichael Tuexen 
680f8829a4aSRandall Stewart 		/* IPV6_V6ONLY socket */
681e6194c2eSMichael Tuexen #ifdef INET
682f8829a4aSRandall Stewart 		if (addr->sa_family == AF_INET) {
683f8829a4aSRandall Stewart 			/* can't bind v4 addr to v6 only socket! */
684c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
68560990c0cSMichael Tuexen 			return (EINVAL);
686e6194c2eSMichael Tuexen 		}
687e6194c2eSMichael Tuexen #endif
688f8829a4aSRandall Stewart 		sin6_p = (struct sockaddr_in6 *)addr;
689f8829a4aSRandall Stewart 
690851b7298SRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
691f8829a4aSRandall Stewart 			/* can't bind v4-mapped addrs either! */
692f8829a4aSRandall Stewart 			/* NOTE: we don't support SIIT */
693c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
69460990c0cSMichael Tuexen 			return (EINVAL);
695f8829a4aSRandall Stewart 		}
696f8829a4aSRandall Stewart 	}
6971b649582SRandall Stewart 	error = sctp_inpcb_bind(so, addr, NULL, p);
69860990c0cSMichael Tuexen 	return (error);
699f8829a4aSRandall Stewart }
700f8829a4aSRandall Stewart 
701f8829a4aSRandall Stewart 
702f8829a4aSRandall Stewart static void
703f8829a4aSRandall Stewart sctp6_close(struct socket *so)
704f8829a4aSRandall Stewart {
7052afb3e84SRandall Stewart 	sctp_close(so);
706f8829a4aSRandall Stewart }
707f8829a4aSRandall Stewart 
70842551e99SRandall Stewart /* This could be made common with sctp_detach() since they are identical */
709f8829a4aSRandall Stewart 
710c105859eSRandall Stewart static
711c105859eSRandall Stewart int
712f8829a4aSRandall Stewart sctp6_disconnect(struct socket *so)
713f8829a4aSRandall Stewart {
7142afb3e84SRandall Stewart 	return (sctp_disconnect(so));
715f8829a4aSRandall Stewart }
716f8829a4aSRandall Stewart 
717c105859eSRandall Stewart 
718f8829a4aSRandall Stewart int
719f8829a4aSRandall Stewart sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
720f8829a4aSRandall Stewart     struct mbuf *control, struct thread *p);
721f8829a4aSRandall Stewart 
722f8829a4aSRandall Stewart 
723f8829a4aSRandall Stewart static int
724f8829a4aSRandall Stewart sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
725f8829a4aSRandall Stewart     struct mbuf *control, struct thread *p)
726f8829a4aSRandall Stewart {
727f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
728f8829a4aSRandall Stewart 	struct in6pcb *inp6;
729f8829a4aSRandall Stewart 
730f8829a4aSRandall Stewart #ifdef INET
731f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6;
732f8829a4aSRandall Stewart 
733f8829a4aSRandall Stewart #endif				/* INET */
734f8829a4aSRandall Stewart 	/* No SPL needed since sctp_output does this */
735f8829a4aSRandall Stewart 
736f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
737f8829a4aSRandall Stewart 	if (inp == NULL) {
738f8829a4aSRandall Stewart 		if (control) {
73917205eccSRandall Stewart 			SCTP_RELEASE_PKT(control);
740f8829a4aSRandall Stewart 			control = NULL;
741f8829a4aSRandall Stewart 		}
74217205eccSRandall Stewart 		SCTP_RELEASE_PKT(m);
743c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
74460990c0cSMichael Tuexen 		return (EINVAL);
745f8829a4aSRandall Stewart 	}
746f8829a4aSRandall Stewart 	inp6 = (struct in6pcb *)inp;
747f8829a4aSRandall Stewart 	/*
748f8829a4aSRandall Stewart 	 * For the TCP model we may get a NULL addr, if we are a connected
749f8829a4aSRandall Stewart 	 * socket thats ok.
750f8829a4aSRandall Stewart 	 */
751f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
752f8829a4aSRandall Stewart 	    (addr == NULL)) {
753f8829a4aSRandall Stewart 		goto connected_type;
754f8829a4aSRandall Stewart 	}
755f8829a4aSRandall Stewart 	if (addr == NULL) {
75617205eccSRandall Stewart 		SCTP_RELEASE_PKT(m);
757f8829a4aSRandall Stewart 		if (control) {
75817205eccSRandall Stewart 			SCTP_RELEASE_PKT(control);
759f8829a4aSRandall Stewart 			control = NULL;
760f8829a4aSRandall Stewart 		}
761c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
762f8829a4aSRandall Stewart 		return (EDESTADDRREQ);
763f8829a4aSRandall Stewart 	}
764f8829a4aSRandall Stewart #ifdef INET
765f8829a4aSRandall Stewart 	sin6 = (struct sockaddr_in6 *)addr;
76644b7479bSRandall Stewart 	if (SCTP_IPV6_V6ONLY(inp6)) {
767f8829a4aSRandall Stewart 		/*
768f8829a4aSRandall Stewart 		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
769f8829a4aSRandall Stewart 		 * v4 addr or v4-mapped addr
770f8829a4aSRandall Stewart 		 */
771f8829a4aSRandall Stewart 		if (addr->sa_family == AF_INET) {
772c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
77360990c0cSMichael Tuexen 			return (EINVAL);
774f8829a4aSRandall Stewart 		}
775f8829a4aSRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
776c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
77760990c0cSMichael Tuexen 			return (EINVAL);
778f8829a4aSRandall Stewart 		}
779f8829a4aSRandall Stewart 	}
780f8829a4aSRandall Stewart 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
781f8829a4aSRandall Stewart 		struct sockaddr_in sin;
782f8829a4aSRandall Stewart 
783f8829a4aSRandall Stewart 		/* convert v4-mapped into v4 addr and send */
784f8829a4aSRandall Stewart 		in6_sin6_2_sin(&sin, sin6);
7853457ccdaSMichael Tuexen 		return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
786f8829a4aSRandall Stewart 	}
787f8829a4aSRandall Stewart #endif				/* INET */
788f8829a4aSRandall Stewart connected_type:
789f8829a4aSRandall Stewart 	/* now what about control */
790f8829a4aSRandall Stewart 	if (control) {
791f8829a4aSRandall Stewart 		if (inp->control) {
792ad81507eSRandall Stewart 			SCTP_PRINTF("huh? control set?\n");
79317205eccSRandall Stewart 			SCTP_RELEASE_PKT(inp->control);
794f8829a4aSRandall Stewart 			inp->control = NULL;
795f8829a4aSRandall Stewart 		}
796f8829a4aSRandall Stewart 		inp->control = control;
797f8829a4aSRandall Stewart 	}
798f8829a4aSRandall Stewart 	/* Place the data */
799f8829a4aSRandall Stewart 	if (inp->pkt) {
800139bc87fSRandall Stewart 		SCTP_BUF_NEXT(inp->pkt_last) = m;
801f8829a4aSRandall Stewart 		inp->pkt_last = m;
802f8829a4aSRandall Stewart 	} else {
803f8829a4aSRandall Stewart 		inp->pkt_last = inp->pkt = m;
804f8829a4aSRandall Stewart 	}
805f8829a4aSRandall Stewart 	if (
806f8829a4aSRandall Stewart 	/* FreeBSD and MacOSX uses a flag passed */
807f8829a4aSRandall Stewart 	    ((flags & PRUS_MORETOCOME) == 0)
808f8829a4aSRandall Stewart 	    ) {
809f8829a4aSRandall Stewart 		/*
810f8829a4aSRandall Stewart 		 * note with the current version this code will only be used
811f8829a4aSRandall Stewart 		 * by OpenBSD, NetBSD and FreeBSD have methods for
812f8829a4aSRandall Stewart 		 * re-defining sosend() to use sctp_sosend().  One can
813f8829a4aSRandall Stewart 		 * optionaly switch back to this code (by changing back the
814f8829a4aSRandall Stewart 		 * defininitions but this is not advisable.
815f8829a4aSRandall Stewart 		 */
816f8829a4aSRandall Stewart 		int ret;
817f8829a4aSRandall Stewart 
818f8829a4aSRandall Stewart 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
819f8829a4aSRandall Stewart 		inp->pkt = NULL;
820f8829a4aSRandall Stewart 		inp->control = NULL;
821f8829a4aSRandall Stewart 		return (ret);
822f8829a4aSRandall Stewart 	} else {
823f8829a4aSRandall Stewart 		return (0);
824f8829a4aSRandall Stewart 	}
825f8829a4aSRandall Stewart }
826f8829a4aSRandall Stewart 
827f8829a4aSRandall Stewart static int
828f8829a4aSRandall Stewart sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
829f8829a4aSRandall Stewart {
83042551e99SRandall Stewart 	uint32_t vrf_id;
831f8829a4aSRandall Stewart 	int error = 0;
832f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
833f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
834f8829a4aSRandall Stewart 
835f8829a4aSRandall Stewart #ifdef INET
836b54ddf22SMichael Tuexen 	struct in6pcb *inp6;
837f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6;
83824aaac8dSMichael Tuexen 	union sctp_sockstore store;
839f8829a4aSRandall Stewart 
840e6194c2eSMichael Tuexen #endif
841f8829a4aSRandall Stewart 
842b54ddf22SMichael Tuexen #ifdef INET
843f8829a4aSRandall Stewart 	inp6 = (struct in6pcb *)so->so_pcb;
844b54ddf22SMichael Tuexen #endif
845f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
846dea47f39SMichael Tuexen 	if (inp == NULL) {
847c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
848f8829a4aSRandall Stewart 		return (ECONNRESET);	/* I made the same as TCP since we are
849f8829a4aSRandall Stewart 					 * not setup? */
850f8829a4aSRandall Stewart 	}
851d61a0ae0SRandall Stewart 	if (addr == NULL) {
852c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
853d61a0ae0SRandall Stewart 		return (EINVAL);
854d61a0ae0SRandall Stewart 	}
855e6194c2eSMichael Tuexen 	switch (addr->sa_family) {
856e6194c2eSMichael Tuexen #ifdef INET
857e6194c2eSMichael Tuexen 	case AF_INET:
858e6194c2eSMichael Tuexen 		if (addr->sa_len != sizeof(struct sockaddr_in)) {
859c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
860d61a0ae0SRandall Stewart 			return (EINVAL);
861d61a0ae0SRandall Stewart 		}
862e6194c2eSMichael Tuexen 		break;
863e6194c2eSMichael Tuexen #endif
864e6194c2eSMichael Tuexen #ifdef INET6
865e6194c2eSMichael Tuexen 	case AF_INET6:
866e6194c2eSMichael Tuexen 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
867c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
868d61a0ae0SRandall Stewart 			return (EINVAL);
869d61a0ae0SRandall Stewart 		}
870e6194c2eSMichael Tuexen 		break;
871e6194c2eSMichael Tuexen #endif
872e6194c2eSMichael Tuexen 	default:
873e6194c2eSMichael Tuexen 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
874e6194c2eSMichael Tuexen 		return (EINVAL);
875e6194c2eSMichael Tuexen 	}
876e6194c2eSMichael Tuexen 
877bff64a4dSRandall Stewart 	vrf_id = inp->def_vrf_id;
878f8829a4aSRandall Stewart 	SCTP_ASOC_CREATE_LOCK(inp);
879f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(inp);
880f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
881f8829a4aSRandall Stewart 	    SCTP_PCB_FLAGS_UNBOUND) {
882f8829a4aSRandall Stewart 		/* Bind a ephemeral port */
883f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
884f8829a4aSRandall Stewart 		error = sctp6_bind(so, NULL, p);
885f8829a4aSRandall Stewart 		if (error) {
886f8829a4aSRandall Stewart 			SCTP_ASOC_CREATE_UNLOCK(inp);
887f8829a4aSRandall Stewart 
888f8829a4aSRandall Stewart 			return (error);
889f8829a4aSRandall Stewart 		}
890f8829a4aSRandall Stewart 		SCTP_INP_RLOCK(inp);
891f8829a4aSRandall Stewart 	}
892f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
893f8829a4aSRandall Stewart 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
894f8829a4aSRandall Stewart 		/* We are already connected AND the TCP model */
895f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
896f8829a4aSRandall Stewart 		SCTP_ASOC_CREATE_UNLOCK(inp);
897c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
898f8829a4aSRandall Stewart 		return (EADDRINUSE);
899f8829a4aSRandall Stewart 	}
900f8829a4aSRandall Stewart #ifdef INET
901f8829a4aSRandall Stewart 	sin6 = (struct sockaddr_in6 *)addr;
90244b7479bSRandall Stewart 	if (SCTP_IPV6_V6ONLY(inp6)) {
903f8829a4aSRandall Stewart 		/*
904f8829a4aSRandall Stewart 		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
905f8829a4aSRandall Stewart 		 * addr or v4-mapped addr
906f8829a4aSRandall Stewart 		 */
907f8829a4aSRandall Stewart 		if (addr->sa_family == AF_INET) {
908f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
909f8829a4aSRandall Stewart 			SCTP_ASOC_CREATE_UNLOCK(inp);
910c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
91160990c0cSMichael Tuexen 			return (EINVAL);
912f8829a4aSRandall Stewart 		}
913f8829a4aSRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
914f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
915f8829a4aSRandall Stewart 			SCTP_ASOC_CREATE_UNLOCK(inp);
916c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
91760990c0cSMichael Tuexen 			return (EINVAL);
918f8829a4aSRandall Stewart 		}
919f8829a4aSRandall Stewart 	}
920f8829a4aSRandall Stewart 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
921f8829a4aSRandall Stewart 		/* convert v4-mapped into v4 addr */
92224aaac8dSMichael Tuexen 		in6_sin6_2_sin(&store.sin, sin6);
92324aaac8dSMichael Tuexen 		addr = &store.sa;
924274b0bd5SMichael Tuexen 	}
925f8829a4aSRandall Stewart #endif				/* INET */
926f8829a4aSRandall Stewart 	/* Now do we connect? */
927f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
928f8829a4aSRandall Stewart 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
929ad81507eSRandall Stewart 		if (stcb) {
930f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
931ad81507eSRandall Stewart 		}
932f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
933f8829a4aSRandall Stewart 	} else {
934f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
935f8829a4aSRandall Stewart 		SCTP_INP_WLOCK(inp);
936f8829a4aSRandall Stewart 		SCTP_INP_INCR_REF(inp);
937f8829a4aSRandall Stewart 		SCTP_INP_WUNLOCK(inp);
938f8829a4aSRandall Stewart 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
939f8829a4aSRandall Stewart 		if (stcb == NULL) {
940f8829a4aSRandall Stewart 			SCTP_INP_WLOCK(inp);
941f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
942f8829a4aSRandall Stewart 			SCTP_INP_WUNLOCK(inp);
943f8829a4aSRandall Stewart 		}
944f8829a4aSRandall Stewart 	}
945f8829a4aSRandall Stewart 
946f8829a4aSRandall Stewart 	if (stcb != NULL) {
947f8829a4aSRandall Stewart 		/* Already have or am bring up an association */
948f8829a4aSRandall Stewart 		SCTP_ASOC_CREATE_UNLOCK(inp);
949f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
950c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
951f8829a4aSRandall Stewart 		return (EALREADY);
952f8829a4aSRandall Stewart 	}
953f8829a4aSRandall Stewart 	/* We are GOOD to go */
954b5c16493SMichael Tuexen 	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
955f8829a4aSRandall Stewart 	SCTP_ASOC_CREATE_UNLOCK(inp);
956f8829a4aSRandall Stewart 	if (stcb == NULL) {
957f8829a4aSRandall Stewart 		/* Gak! no memory */
958f8829a4aSRandall Stewart 		return (error);
959f8829a4aSRandall Stewart 	}
960f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
961f8829a4aSRandall Stewart 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
962f8829a4aSRandall Stewart 		/* Set the connected flag so we can queue data */
963f8829a4aSRandall Stewart 		soisconnecting(so);
964f8829a4aSRandall Stewart 	}
965f8829a4aSRandall Stewart 	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
966ad81507eSRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
967f8829a4aSRandall Stewart 
968f8829a4aSRandall Stewart 	/* initialize authentication parameters for the assoc */
969f8829a4aSRandall Stewart 	sctp_initialize_auth_params(inp, stcb);
970f8829a4aSRandall Stewart 
971ceaad40aSRandall Stewart 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
972f8829a4aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
97360990c0cSMichael Tuexen 	return (error);
974f8829a4aSRandall Stewart }
975f8829a4aSRandall Stewart 
976f8829a4aSRandall Stewart static int
977f8829a4aSRandall Stewart sctp6_getaddr(struct socket *so, struct sockaddr **addr)
978f8829a4aSRandall Stewart {
979f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6;
980f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
98142551e99SRandall Stewart 	uint32_t vrf_id;
98242551e99SRandall Stewart 	struct sctp_ifa *sctp_ifa;
983f8829a4aSRandall Stewart 
984f8829a4aSRandall Stewart 	int error;
985f8829a4aSRandall Stewart 
986f8829a4aSRandall Stewart 	/*
987f8829a4aSRandall Stewart 	 * Do the malloc first in case it blocks.
988f8829a4aSRandall Stewart 	 */
989e6194c2eSMichael Tuexen 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
99020083c2eSMichael Tuexen 	if (sin6 == NULL)
99160990c0cSMichael Tuexen 		return (ENOMEM);
992f8829a4aSRandall Stewart 	sin6->sin6_family = AF_INET6;
993f8829a4aSRandall Stewart 	sin6->sin6_len = sizeof(*sin6);
994f8829a4aSRandall Stewart 
995f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
996f8829a4aSRandall Stewart 	if (inp == NULL) {
997f8829a4aSRandall Stewart 		SCTP_FREE_SONAME(sin6);
998c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
99960990c0cSMichael Tuexen 		return (ECONNRESET);
1000f8829a4aSRandall Stewart 	}
1001f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(inp);
1002f8829a4aSRandall Stewart 	sin6->sin6_port = inp->sctp_lport;
1003f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1004f8829a4aSRandall Stewart 		/* For the bound all case you get back 0 */
1005f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1006f8829a4aSRandall Stewart 			struct sctp_tcb *stcb;
1007f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin_a6;
1008f8829a4aSRandall Stewart 			struct sctp_nets *net;
1009f8829a4aSRandall Stewart 			int fnd;
1010f8829a4aSRandall Stewart 
1011f8829a4aSRandall Stewart 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1012f8829a4aSRandall Stewart 			if (stcb == NULL) {
1013f8829a4aSRandall Stewart 				goto notConn6;
1014f8829a4aSRandall Stewart 			}
1015f8829a4aSRandall Stewart 			fnd = 0;
1016f8829a4aSRandall Stewart 			sin_a6 = NULL;
1017f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1018f8829a4aSRandall Stewart 				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
101903b0b021SRandall Stewart 				if (sin_a6 == NULL)
102003b0b021SRandall Stewart 					/* this will make coverity happy */
102103b0b021SRandall Stewart 					continue;
102203b0b021SRandall Stewart 
1023f8829a4aSRandall Stewart 				if (sin_a6->sin6_family == AF_INET6) {
1024f8829a4aSRandall Stewart 					fnd = 1;
1025f8829a4aSRandall Stewart 					break;
1026f8829a4aSRandall Stewart 				}
1027f8829a4aSRandall Stewart 			}
1028f8829a4aSRandall Stewart 			if ((!fnd) || (sin_a6 == NULL)) {
1029f8829a4aSRandall Stewart 				/* punt */
1030f8829a4aSRandall Stewart 				goto notConn6;
1031f8829a4aSRandall Stewart 			}
1032bff64a4dSRandall Stewart 			vrf_id = inp->def_vrf_id;
1033bff64a4dSRandall Stewart 			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
103442551e99SRandall Stewart 			if (sctp_ifa) {
103542551e99SRandall Stewart 				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
103642551e99SRandall Stewart 			}
1037f8829a4aSRandall Stewart 		} else {
1038f8829a4aSRandall Stewart 			/* For the bound all case you get back 0 */
1039f8829a4aSRandall Stewart 	notConn6:
1040f8829a4aSRandall Stewart 			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1041f8829a4aSRandall Stewart 		}
1042f8829a4aSRandall Stewart 	} else {
1043f8829a4aSRandall Stewart 		/* Take the first IPv6 address in the list */
1044f8829a4aSRandall Stewart 		struct sctp_laddr *laddr;
1045f8829a4aSRandall Stewart 		int fnd = 0;
1046f8829a4aSRandall Stewart 
1047f8829a4aSRandall Stewart 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
104842551e99SRandall Stewart 			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1049f8829a4aSRandall Stewart 				struct sockaddr_in6 *sin_a;
1050f8829a4aSRandall Stewart 
105124aaac8dSMichael Tuexen 				sin_a = &laddr->ifa->address.sin6;
1052f8829a4aSRandall Stewart 				sin6->sin6_addr = sin_a->sin6_addr;
1053f8829a4aSRandall Stewart 				fnd = 1;
1054f8829a4aSRandall Stewart 				break;
1055f8829a4aSRandall Stewart 			}
1056f8829a4aSRandall Stewart 		}
1057f8829a4aSRandall Stewart 		if (!fnd) {
1058f8829a4aSRandall Stewart 			SCTP_FREE_SONAME(sin6);
1059f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
1060c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
106160990c0cSMichael Tuexen 			return (ENOENT);
1062f8829a4aSRandall Stewart 		}
1063f8829a4aSRandall Stewart 	}
1064f8829a4aSRandall Stewart 	SCTP_INP_RUNLOCK(inp);
1065f8829a4aSRandall Stewart 	/* Scoping things for v6 */
106603b0b021SRandall Stewart 	if ((error = sa6_recoverscope(sin6)) != 0) {
106703b0b021SRandall Stewart 		SCTP_FREE_SONAME(sin6);
1068f8829a4aSRandall Stewart 		return (error);
106903b0b021SRandall Stewart 	}
1070f8829a4aSRandall Stewart 	(*addr) = (struct sockaddr *)sin6;
1071f8829a4aSRandall Stewart 	return (0);
1072f8829a4aSRandall Stewart }
1073f8829a4aSRandall Stewart 
1074f8829a4aSRandall Stewart static int
1075f8829a4aSRandall Stewart sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1076f8829a4aSRandall Stewart {
1077310a0277SMichael Tuexen 	struct sockaddr_in6 *sin6;
1078f8829a4aSRandall Stewart 	int fnd;
1079f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin_a6;
1080f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
1081f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1082f8829a4aSRandall Stewart 	struct sctp_nets *net;
1083f8829a4aSRandall Stewart 	int error;
1084f8829a4aSRandall Stewart 
108560990c0cSMichael Tuexen 	/* Do the malloc first in case it blocks. */
1086f8829a4aSRandall Stewart 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
108720083c2eSMichael Tuexen 	if (sin6 == NULL)
108820083c2eSMichael Tuexen 		return (ENOMEM);
1089f8829a4aSRandall Stewart 	sin6->sin6_family = AF_INET6;
1090f8829a4aSRandall Stewart 	sin6->sin6_len = sizeof(*sin6);
1091f8829a4aSRandall Stewart 
1092f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
109360990c0cSMichael Tuexen 	if ((inp == NULL) ||
109460990c0cSMichael Tuexen 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
109560990c0cSMichael Tuexen 		/* UDP type and listeners will drop out here */
1096f8829a4aSRandall Stewart 		SCTP_FREE_SONAME(sin6);
109760990c0cSMichael Tuexen 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
109860990c0cSMichael Tuexen 		return (ENOTCONN);
1099f8829a4aSRandall Stewart 	}
1100f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(inp);
1101f8829a4aSRandall Stewart 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1102ad81507eSRandall Stewart 	if (stcb) {
1103f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
1104ad81507eSRandall Stewart 	}
1105f8829a4aSRandall Stewart 	SCTP_INP_RUNLOCK(inp);
1106f8829a4aSRandall Stewart 	if (stcb == NULL) {
1107f8829a4aSRandall Stewart 		SCTP_FREE_SONAME(sin6);
1108c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
110960990c0cSMichael Tuexen 		return (ECONNRESET);
1110f8829a4aSRandall Stewart 	}
1111f8829a4aSRandall Stewart 	fnd = 0;
1112f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1113f8829a4aSRandall Stewart 		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1114f8829a4aSRandall Stewart 		if (sin_a6->sin6_family == AF_INET6) {
1115f8829a4aSRandall Stewart 			fnd = 1;
1116f8829a4aSRandall Stewart 			sin6->sin6_port = stcb->rport;
1117f8829a4aSRandall Stewart 			sin6->sin6_addr = sin_a6->sin6_addr;
1118f8829a4aSRandall Stewart 			break;
1119f8829a4aSRandall Stewart 		}
1120f8829a4aSRandall Stewart 	}
1121f8829a4aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
1122f8829a4aSRandall Stewart 	if (!fnd) {
1123f8829a4aSRandall Stewart 		/* No IPv4 address */
1124f8829a4aSRandall Stewart 		SCTP_FREE_SONAME(sin6);
1125c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
112660990c0cSMichael Tuexen 		return (ENOENT);
1127f8829a4aSRandall Stewart 	}
1128*caeae63fSMichael Tuexen 	if ((error = sa6_recoverscope(sin6)) != 0) {
1129*caeae63fSMichael Tuexen 		SCTP_FREE_SONAME(sin6);
1130*caeae63fSMichael Tuexen 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1131f8829a4aSRandall Stewart 		return (error);
1132*caeae63fSMichael Tuexen 	}
1133f8829a4aSRandall Stewart 	*addr = (struct sockaddr *)sin6;
1134f8829a4aSRandall Stewart 	return (0);
1135f8829a4aSRandall Stewart }
1136f8829a4aSRandall Stewart 
1137f8829a4aSRandall Stewart static int
1138f8829a4aSRandall Stewart sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1139f8829a4aSRandall Stewart {
1140e6194c2eSMichael Tuexen #ifdef INET
1141f8829a4aSRandall Stewart 	struct sockaddr *addr;
1142e6194c2eSMichael Tuexen 
1143e6194c2eSMichael Tuexen #endif
1144f8829a4aSRandall Stewart 	struct in6pcb *inp6 = sotoin6pcb(so);
114593164cf9SRandall Stewart 	int error;
1146f8829a4aSRandall Stewart 
1147c4739e2fSRandall Stewart 	if (inp6 == NULL) {
1148c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
114960990c0cSMichael Tuexen 		return (EINVAL);
1150c4739e2fSRandall Stewart 	}
1151f8829a4aSRandall Stewart 	/* allow v6 addresses precedence */
1152f8829a4aSRandall Stewart 	error = sctp6_getaddr(so, nam);
1153e6194c2eSMichael Tuexen #ifdef INET
1154f8829a4aSRandall Stewart 	if (error) {
1155f8829a4aSRandall Stewart 		/* try v4 next if v6 failed */
1156f8829a4aSRandall Stewart 		error = sctp_ingetaddr(so, nam);
1157f8829a4aSRandall Stewart 		if (error) {
1158f8829a4aSRandall Stewart 			return (error);
1159f8829a4aSRandall Stewart 		}
1160f8829a4aSRandall Stewart 		addr = *nam;
1161f8829a4aSRandall Stewart 		/* if I'm V6ONLY, convert it to v4-mapped */
116244b7479bSRandall Stewart 		if (SCTP_IPV6_V6ONLY(inp6)) {
1163f8829a4aSRandall Stewart 			struct sockaddr_in6 sin6;
1164f8829a4aSRandall Stewart 
1165f8829a4aSRandall Stewart 			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1166f8829a4aSRandall Stewart 			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1167f8829a4aSRandall Stewart 		}
1168f8829a4aSRandall Stewart 	}
1169e6194c2eSMichael Tuexen #endif
1170f8829a4aSRandall Stewart 	return (error);
1171f8829a4aSRandall Stewart }
1172f8829a4aSRandall Stewart 
1173f8829a4aSRandall Stewart 
1174f8829a4aSRandall Stewart static int
1175f8829a4aSRandall Stewart sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1176f8829a4aSRandall Stewart {
1177e6194c2eSMichael Tuexen #ifdef INET
1178e6194c2eSMichael Tuexen 	struct sockaddr *addr;
1179e6194c2eSMichael Tuexen 
1180e6194c2eSMichael Tuexen #endif
1181f8829a4aSRandall Stewart 	struct in6pcb *inp6 = sotoin6pcb(so);
118293164cf9SRandall Stewart 	int error;
1183f8829a4aSRandall Stewart 
1184c4739e2fSRandall Stewart 	if (inp6 == NULL) {
1185c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
118660990c0cSMichael Tuexen 		return (EINVAL);
1187c4739e2fSRandall Stewart 	}
1188f8829a4aSRandall Stewart 	/* allow v6 addresses precedence */
1189f8829a4aSRandall Stewart 	error = sctp6_peeraddr(so, nam);
1190e6194c2eSMichael Tuexen #ifdef INET
1191f8829a4aSRandall Stewart 	if (error) {
1192f8829a4aSRandall Stewart 		/* try v4 next if v6 failed */
1193f8829a4aSRandall Stewart 		error = sctp_peeraddr(so, nam);
1194f8829a4aSRandall Stewart 		if (error) {
1195f8829a4aSRandall Stewart 			return (error);
1196f8829a4aSRandall Stewart 		}
1197e6194c2eSMichael Tuexen 		addr = *nam;
1198f8829a4aSRandall Stewart 		/* if I'm V6ONLY, convert it to v4-mapped */
119944b7479bSRandall Stewart 		if (SCTP_IPV6_V6ONLY(inp6)) {
1200f8829a4aSRandall Stewart 			struct sockaddr_in6 sin6;
1201f8829a4aSRandall Stewart 
1202f8829a4aSRandall Stewart 			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1203f8829a4aSRandall Stewart 			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1204f8829a4aSRandall Stewart 		}
1205f8829a4aSRandall Stewart 	}
1206e6194c2eSMichael Tuexen #endif
120760990c0cSMichael Tuexen 	return (error);
1208f8829a4aSRandall Stewart }
1209f8829a4aSRandall Stewart 
1210f8829a4aSRandall Stewart struct pr_usrreqs sctp6_usrreqs = {
1211f8829a4aSRandall Stewart 	.pru_abort = sctp6_abort,
1212f8829a4aSRandall Stewart 	.pru_accept = sctp_accept,
1213f8829a4aSRandall Stewart 	.pru_attach = sctp6_attach,
1214f8829a4aSRandall Stewart 	.pru_bind = sctp6_bind,
1215f8829a4aSRandall Stewart 	.pru_connect = sctp6_connect,
1216f8829a4aSRandall Stewart 	.pru_control = in6_control,
1217f8829a4aSRandall Stewart 	.pru_close = sctp6_close,
1218f8829a4aSRandall Stewart 	.pru_detach = sctp6_close,
1219f8829a4aSRandall Stewart 	.pru_sopoll = sopoll_generic,
1220276ca501SRandall Stewart 	.pru_flush = sctp_flush,
1221f8829a4aSRandall Stewart 	.pru_disconnect = sctp6_disconnect,
1222f8829a4aSRandall Stewart 	.pru_listen = sctp_listen,
1223f8829a4aSRandall Stewart 	.pru_peeraddr = sctp6_getpeeraddr,
1224f8829a4aSRandall Stewart 	.pru_send = sctp6_send,
1225f8829a4aSRandall Stewart 	.pru_shutdown = sctp_shutdown,
1226f8829a4aSRandall Stewart 	.pru_sockaddr = sctp6_in6getaddr,
1227f8829a4aSRandall Stewart 	.pru_sosend = sctp_sosend,
1228f8829a4aSRandall Stewart 	.pru_soreceive = sctp_soreceive
1229f8829a4aSRandall Stewart };
12305e20b91dSMichael Tuexen 
12315e20b91dSMichael Tuexen #endif
1232