xref: /freebsd/sys/netinet6/sctp6_usrreq.c (revision c4739e2f478736687f85dfb28db95587d3e8d7a8)
1f8829a4aSRandall Stewart /*-
2b1006367SRandall Stewart  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3f8829a4aSRandall Stewart  *
4f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
5f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
6f8829a4aSRandall Stewart  *
7f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
8f8829a4aSRandall Stewart  *   this list of conditions and the following disclaimer.
9f8829a4aSRandall Stewart  *
10f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
11f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
12f8829a4aSRandall Stewart  *   the documentation and/or other materials provided with the distribution.
13f8829a4aSRandall Stewart  *
14f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
16f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
17f8829a4aSRandall Stewart  *
18f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
29f8829a4aSRandall Stewart  */
30f8829a4aSRandall Stewart /*	$KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $	*/
3173932c69SRandall Stewart #include <sys/cdefs.h>
32f8829a4aSRandall Stewart __FBSDID("$FreeBSD$");
33f8829a4aSRandall Stewart 
34c105859eSRandall Stewart 
35f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
3693164cf9SRandall Stewart #include <sys/proc.h>
37f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
38f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
39f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
40c105859eSRandall Stewart #if defined(INET6)
41c105859eSRandall Stewart #include <netinet6/sctp6_var.h>
42c105859eSRandall Stewart #endif
4342551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
44f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
4593164cf9SRandall Stewart #include <netinet/sctp_uio.h>
4693164cf9SRandall Stewart #include <netinet/sctp_asconf.h>
4793164cf9SRandall Stewart #include <netinet/sctputil.h>
4893164cf9SRandall Stewart #include <netinet/sctp_indata.h>
4993164cf9SRandall Stewart #include <netinet/sctp_timer.h>
5093164cf9SRandall Stewart #include <netinet/sctp_auth.h>
51c105859eSRandall Stewart #include <netinet/sctp_input.h>
52c105859eSRandall Stewart #include <netinet/sctp_output.h>
53207304d4SRandall Stewart #include <netinet/sctp_bsd_addr.h>
54f8829a4aSRandall Stewart 
55b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
562cb64cb2SGeorge V. Neville-Neil #include <netipsec/ipsec.h>
572cb64cb2SGeorge V. Neville-Neil #if defined(INET6)
582cb64cb2SGeorge V. Neville-Neil #include <netipsec/ipsec6.h>
592cb64cb2SGeorge V. Neville-Neil #endif				/* INET6 */
60b2630c29SGeorge V. Neville-Neil #endif				/* IPSEC */
61f8829a4aSRandall Stewart 
6293164cf9SRandall Stewart extern struct protosw inetsw[];
63f8829a4aSRandall Stewart 
64f8829a4aSRandall Stewart int
651272577eSXin LI sctp6_input(struct mbuf **i_pak, int *offp, int proto)
66f8829a4aSRandall Stewart {
67139bc87fSRandall Stewart 	struct mbuf *m;
68f8829a4aSRandall Stewart 	struct ip6_hdr *ip6;
69f8829a4aSRandall Stewart 	struct sctphdr *sh;
70f8829a4aSRandall Stewart 	struct sctp_inpcb *in6p = NULL;
71f8829a4aSRandall Stewart 	struct sctp_nets *net;
72f8829a4aSRandall Stewart 	int refcount_up = 0;
7317205eccSRandall Stewart 	uint32_t check, calc_check;
74ad21a364SRandall Stewart 	uint32_t vrf_id = 0;
75f8829a4aSRandall Stewart 	struct inpcb *in6p_ip;
76f8829a4aSRandall Stewart 	struct sctp_chunkhdr *ch;
77f8829a4aSRandall Stewart 	int length, mlen, offset, iphlen;
78bff64a4dSRandall Stewart 	uint8_t ecn_bits;
79f8829a4aSRandall Stewart 	struct sctp_tcb *stcb = NULL;
80d61a0ae0SRandall Stewart 	int pkt_len = 0;
81f8829a4aSRandall Stewart 	int off = *offp;
82f8829a4aSRandall Stewart 
8317205eccSRandall Stewart 	/* get the VRF and table id's */
8417205eccSRandall Stewart 	if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
8517205eccSRandall Stewart 		SCTP_RELEASE_PKT(*i_pak);
8617205eccSRandall Stewart 		return (-1);
8717205eccSRandall Stewart 	}
88c105859eSRandall Stewart 	m = SCTP_HEADER_TO_CHAIN(*i_pak);
89d61a0ae0SRandall Stewart 	pkt_len = SCTP_HEADER_LEN((*i_pak));
90c105859eSRandall Stewart 
91207304d4SRandall Stewart #ifdef  SCTP_PACKET_LOGGING
92207304d4SRandall Stewart 	sctp_packet_log(m, pkt_len);
93207304d4SRandall Stewart #endif
94f8829a4aSRandall Stewart 	ip6 = mtod(m, struct ip6_hdr *);
95f8829a4aSRandall Stewart 	/* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
96d61a0ae0SRandall Stewart 	IP6_EXTHDR_GET(sh, struct sctphdr *, m, off,
97d61a0ae0SRandall Stewart 	    (int)(sizeof(*sh) + sizeof(*ch)));
98f8829a4aSRandall Stewart 	if (sh == NULL) {
99f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_hdrops);
100f8829a4aSRandall Stewart 		return IPPROTO_DONE;
101f8829a4aSRandall Stewart 	}
102f8829a4aSRandall Stewart 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
103f8829a4aSRandall Stewart 	iphlen = off;
104f8829a4aSRandall Stewart 	offset = iphlen + sizeof(*sh) + sizeof(*ch);
105e42a0f5eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1,
106e42a0f5eSRandall Stewart 	    "sctp6_input() length:%d iphlen:%d\n", pkt_len, iphlen);
107e42a0f5eSRandall Stewart 
108f8829a4aSRandall Stewart 
109f8829a4aSRandall Stewart #if defined(NFAITH) && NFAITH > 0
110f8829a4aSRandall Stewart 
111f8829a4aSRandall Stewart 	if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) {
112f8829a4aSRandall Stewart 		/* XXX send icmp6 host/port unreach? */
113f8829a4aSRandall Stewart 		goto bad;
114f8829a4aSRandall Stewart 	}
115f8829a4aSRandall Stewart #endif				/* NFAITH defined and > 0 */
116f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvpackets);
117f8829a4aSRandall Stewart 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
118ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n",
119d61a0ae0SRandall Stewart 	    iphlen, pkt_len);
120f8829a4aSRandall Stewart 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
121f8829a4aSRandall Stewart 		/* No multi-cast support in SCTP */
122f8829a4aSRandall Stewart 		goto bad;
123f8829a4aSRandall Stewart 	}
124f8829a4aSRandall Stewart 	/* destination port of 0 is illegal, based on RFC2960. */
125f8829a4aSRandall Stewart 	if (sh->dest_port == 0)
126f8829a4aSRandall Stewart 		goto bad;
127f8829a4aSRandall Stewart 	if ((sctp_no_csum_on_loopback == 0) ||
128139bc87fSRandall Stewart 	    (!SCTP_IS_IT_LOOPBACK(m))) {
129f8829a4aSRandall Stewart 		/*
130f8829a4aSRandall Stewart 		 * we do NOT validate things from the loopback if the sysctl
131f8829a4aSRandall Stewart 		 * is set to 1.
132f8829a4aSRandall Stewart 		 */
133f8829a4aSRandall Stewart 		check = sh->checksum;	/* save incoming checksum */
134f8829a4aSRandall Stewart 		if ((check == 0) && (sctp_no_csum_on_loopback)) {
135f8829a4aSRandall Stewart 			/*
136f8829a4aSRandall Stewart 			 * special hook for where we got a local address
137f8829a4aSRandall Stewart 			 * somehow routed across a non IFT_LOOP type
138f8829a4aSRandall Stewart 			 * interface
139f8829a4aSRandall Stewart 			 */
140f8829a4aSRandall Stewart 			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))
141f8829a4aSRandall Stewart 				goto sctp_skip_csum;
142f8829a4aSRandall Stewart 		}
143f8829a4aSRandall Stewart 		sh->checksum = 0;	/* prepare for calc */
144f8829a4aSRandall Stewart 		calc_check = sctp_calculate_sum(m, &mlen, iphlen);
145f8829a4aSRandall Stewart 		if (calc_check != check) {
146ad81507eSRandall Stewart 			SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
147ad81507eSRandall Stewart 			    calc_check, check, m, mlen, iphlen);
148f8829a4aSRandall Stewart 			stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
149bff64a4dSRandall Stewart 			    sh, ch, &in6p, &net, vrf_id);
150f8829a4aSRandall Stewart 			/* in6p's ref-count increased && stcb locked */
151f8829a4aSRandall Stewart 			if ((in6p) && (stcb)) {
152f8829a4aSRandall Stewart 				sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
153f8829a4aSRandall Stewart 				sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, 2);
154f8829a4aSRandall Stewart 			} else if ((in6p != NULL) && (stcb == NULL)) {
155f8829a4aSRandall Stewart 				refcount_up = 1;
156f8829a4aSRandall Stewart 			}
157f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_badsum);
158f8829a4aSRandall Stewart 			SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
159f8829a4aSRandall Stewart 			goto bad;
160f8829a4aSRandall Stewart 		}
161f8829a4aSRandall Stewart 		sh->checksum = calc_check;
162f8829a4aSRandall Stewart 	}
163139bc87fSRandall Stewart sctp_skip_csum:
164f8829a4aSRandall Stewart 	net = NULL;
165f8829a4aSRandall Stewart 	/*
166f8829a4aSRandall Stewart 	 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
167f8829a4aSRandall Stewart 	 * IP/SCTP/first chunk header...
168f8829a4aSRandall Stewart 	 */
169f8829a4aSRandall Stewart 	stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
170bff64a4dSRandall Stewart 	    sh, ch, &in6p, &net, vrf_id);
171f8829a4aSRandall Stewart 	/* in6p's ref-count increased */
172f8829a4aSRandall Stewart 	if (in6p == NULL) {
173f8829a4aSRandall Stewart 		struct sctp_init_chunk *init_chk, chunk_buf;
174f8829a4aSRandall Stewart 
175f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_noport);
176f8829a4aSRandall Stewart 		if (ch->chunk_type == SCTP_INITIATION) {
177f8829a4aSRandall Stewart 			/*
178f8829a4aSRandall Stewart 			 * we do a trick here to get the INIT tag, dig in
179f8829a4aSRandall Stewart 			 * and get the tag from the INIT and put it in the
180f8829a4aSRandall Stewart 			 * common header.
181f8829a4aSRandall Stewart 			 */
182f8829a4aSRandall Stewart 			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
183f8829a4aSRandall Stewart 			    iphlen + sizeof(*sh), sizeof(*init_chk),
184bff64a4dSRandall Stewart 			    (uint8_t *) & chunk_buf);
185ad81507eSRandall Stewart 			if (init_chk)
186f8829a4aSRandall Stewart 				sh->v_tag = init_chk->init.initiate_tag;
187ad81507eSRandall Stewart 			else
188ad81507eSRandall Stewart 				sh->v_tag = 0;
189f8829a4aSRandall Stewart 		}
190a5d547adSRandall Stewart 		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
191ad21a364SRandall Stewart 			sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id);
192a5d547adSRandall Stewart 			goto bad;
193a5d547adSRandall Stewart 		}
194a5d547adSRandall Stewart 		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
195a5d547adSRandall Stewart 			goto bad;
196a5d547adSRandall Stewart 		}
197a5d547adSRandall Stewart 		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
198ad21a364SRandall Stewart 			sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id);
199f8829a4aSRandall Stewart 		goto bad;
200f8829a4aSRandall Stewart 	} else if (stcb == NULL) {
201f8829a4aSRandall Stewart 		refcount_up = 1;
202f8829a4aSRandall Stewart 	}
203f8829a4aSRandall Stewart 	in6p_ip = (struct inpcb *)in6p;
204b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
205f8829a4aSRandall Stewart 	/*
206f8829a4aSRandall Stewart 	 * Check AH/ESP integrity.
207f8829a4aSRandall Stewart 	 */
20850cec919SRandall Stewart 	if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) {
209f8829a4aSRandall Stewart /* XXX */
210f8829a4aSRandall Stewart 		ipsec6stat.in_polvio++;
211f8829a4aSRandall Stewart 		goto bad;
212f8829a4aSRandall Stewart 	}
213b2630c29SGeorge V. Neville-Neil #endif				/* IPSEC */
214f8829a4aSRandall Stewart 
215f8829a4aSRandall Stewart 	/*
216f8829a4aSRandall Stewart 	 * CONTROL chunk processing
217f8829a4aSRandall Stewart 	 */
218f8829a4aSRandall Stewart 	offset -= sizeof(*ch);
219f8829a4aSRandall Stewart 	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
220139bc87fSRandall Stewart 
221139bc87fSRandall Stewart 	/* Length now holds the total packet length payload + iphlen */
222139bc87fSRandall Stewart 	length = ntohs(ip6->ip6_plen) + iphlen;
223139bc87fSRandall Stewart 
2243c503c28SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
2256e55db54SRandall Stewart 	sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
226ad21a364SRandall Stewart 	    in6p, stcb, net, ecn_bits, vrf_id);
227f8829a4aSRandall Stewart 	/* inp's ref-count reduced && stcb unlocked */
228f8829a4aSRandall Stewart 	/* XXX this stuff below gets moved to appropriate parts later... */
229f8829a4aSRandall Stewart 	if (m)
23017205eccSRandall Stewart 		sctp_m_freem(m);
231f8829a4aSRandall Stewart 	if ((in6p) && refcount_up) {
232f8829a4aSRandall Stewart 		/* reduce ref-count */
233f8829a4aSRandall Stewart 		SCTP_INP_WLOCK(in6p);
234f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(in6p);
235f8829a4aSRandall Stewart 		SCTP_INP_WUNLOCK(in6p);
236f8829a4aSRandall Stewart 	}
237f8829a4aSRandall Stewart 	return IPPROTO_DONE;
238f8829a4aSRandall Stewart 
239f8829a4aSRandall Stewart bad:
240ad81507eSRandall Stewart 	if (stcb) {
241f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
242ad81507eSRandall Stewart 	}
243f8829a4aSRandall Stewart 	if ((in6p) && refcount_up) {
244f8829a4aSRandall Stewart 		/* reduce ref-count */
245f8829a4aSRandall Stewart 		SCTP_INP_WLOCK(in6p);
246f8829a4aSRandall Stewart 		SCTP_INP_DECR_REF(in6p);
247f8829a4aSRandall Stewart 		SCTP_INP_WUNLOCK(in6p);
248f8829a4aSRandall Stewart 	}
249f8829a4aSRandall Stewart 	if (m)
25017205eccSRandall Stewart 		sctp_m_freem(m);
251f8829a4aSRandall Stewart 	return IPPROTO_DONE;
252f8829a4aSRandall Stewart }
253f8829a4aSRandall Stewart 
254f8829a4aSRandall Stewart 
255f8829a4aSRandall Stewart static void
2561272577eSXin LI sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
2571272577eSXin LI     struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
258f8829a4aSRandall Stewart {
259bff64a4dSRandall Stewart 	uint32_t nxtsz;
260f8829a4aSRandall Stewart 
261f8829a4aSRandall Stewart 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
262f8829a4aSRandall Stewart 	    (icmp6 == NULL) || (sh == NULL)) {
263f8829a4aSRandall Stewart 		goto out;
264f8829a4aSRandall Stewart 	}
265f8829a4aSRandall Stewart 	/* First do we even look at it? */
266f8829a4aSRandall Stewart 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
267f8829a4aSRandall Stewart 		goto out;
268f8829a4aSRandall Stewart 
269f8829a4aSRandall Stewart 	if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
270f8829a4aSRandall Stewart 		/* not PACKET TO BIG */
271f8829a4aSRandall Stewart 		goto out;
272f8829a4aSRandall Stewart 	}
273f8829a4aSRandall Stewart 	/*
274f8829a4aSRandall Stewart 	 * ok we need to look closely. We could even get smarter and look at
275f8829a4aSRandall Stewart 	 * anyone that we sent to in case we get a different ICMP that tells
276f8829a4aSRandall Stewart 	 * us there is no way to reach a host, but for this impl, all we
277f8829a4aSRandall Stewart 	 * care about is MTU discovery.
278f8829a4aSRandall Stewart 	 */
279f8829a4aSRandall Stewart 	nxtsz = ntohl(icmp6->icmp6_mtu);
280f8829a4aSRandall Stewart 	/* Stop any PMTU timer */
281a5d547adSRandall Stewart 	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
282f8829a4aSRandall Stewart 
283f8829a4aSRandall Stewart 	/* Adjust destination size limit */
284f8829a4aSRandall Stewart 	if (net->mtu > nxtsz) {
285f8829a4aSRandall Stewart 		net->mtu = nxtsz;
286f8829a4aSRandall Stewart 	}
287f8829a4aSRandall Stewart 	/* now what about the ep? */
288f8829a4aSRandall Stewart 	if (stcb->asoc.smallest_mtu > nxtsz) {
289f8829a4aSRandall Stewart 		struct sctp_tmit_chunk *chk;
290f8829a4aSRandall Stewart 
291f8829a4aSRandall Stewart 		/* Adjust that too */
292f8829a4aSRandall Stewart 		stcb->asoc.smallest_mtu = nxtsz;
293f8829a4aSRandall Stewart 		/* now off to subtract IP_DF flag if needed */
294f8829a4aSRandall Stewart 
295f8829a4aSRandall Stewart 		TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
296bff64a4dSRandall Stewart 			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
297f8829a4aSRandall Stewart 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
298f8829a4aSRandall Stewart 			}
299f8829a4aSRandall Stewart 		}
300f8829a4aSRandall Stewart 		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
301bff64a4dSRandall Stewart 			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
302f8829a4aSRandall Stewart 				/*
303f8829a4aSRandall Stewart 				 * For this guy we also mark for immediate
304f8829a4aSRandall Stewart 				 * resend since we sent to big of chunk
305f8829a4aSRandall Stewart 				 */
306f8829a4aSRandall Stewart 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
307f8829a4aSRandall Stewart 				if (chk->sent != SCTP_DATAGRAM_RESEND)
308f8829a4aSRandall Stewart 					stcb->asoc.sent_queue_retran_cnt++;
309f8829a4aSRandall Stewart 				chk->sent = SCTP_DATAGRAM_RESEND;
310f8829a4aSRandall Stewart 				chk->rec.data.doing_fast_retransmit = 0;
311f8829a4aSRandall Stewart 
312f8829a4aSRandall Stewart 				chk->sent = SCTP_DATAGRAM_RESEND;
313f8829a4aSRandall Stewart 				/* Clear any time so NO RTT is being done */
314f8829a4aSRandall Stewart 				chk->sent_rcv_time.tv_sec = 0;
315f8829a4aSRandall Stewart 				chk->sent_rcv_time.tv_usec = 0;
316f8829a4aSRandall Stewart 				stcb->asoc.total_flight -= chk->send_size;
317f8829a4aSRandall Stewart 				net->flight_size -= chk->send_size;
318f8829a4aSRandall Stewart 			}
319f8829a4aSRandall Stewart 		}
320f8829a4aSRandall Stewart 	}
321f8829a4aSRandall Stewart 	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
322f8829a4aSRandall Stewart out:
323ad81507eSRandall Stewart 	if (stcb) {
324f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
325f8829a4aSRandall Stewart 	}
326ad81507eSRandall Stewart }
327f8829a4aSRandall Stewart 
328f8829a4aSRandall Stewart 
329f8829a4aSRandall Stewart void
3301272577eSXin LI sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
331f8829a4aSRandall Stewart {
332f8829a4aSRandall Stewart 	struct sctphdr sh;
333f8829a4aSRandall Stewart 	struct ip6ctlparam *ip6cp = NULL;
33442551e99SRandall Stewart 	uint32_t vrf_id;
33593164cf9SRandall Stewart 	int cm;
336f8829a4aSRandall Stewart 
33742551e99SRandall Stewart 	vrf_id = SCTP_DEFAULT_VRFID;
33842551e99SRandall Stewart 
339f8829a4aSRandall Stewart 	if (pktdst->sa_family != AF_INET6 ||
340f8829a4aSRandall Stewart 	    pktdst->sa_len != sizeof(struct sockaddr_in6))
341f8829a4aSRandall Stewart 		return;
342f8829a4aSRandall Stewart 
343f8829a4aSRandall Stewart 	if ((unsigned)cmd >= PRC_NCMDS)
344f8829a4aSRandall Stewart 		return;
345f8829a4aSRandall Stewart 	if (PRC_IS_REDIRECT(cmd)) {
346f8829a4aSRandall Stewart 		d = NULL;
347f8829a4aSRandall Stewart 	} else if (inet6ctlerrmap[cmd] == 0) {
348f8829a4aSRandall Stewart 		return;
349f8829a4aSRandall Stewart 	}
350f8829a4aSRandall Stewart 	/* if the parameter is from icmp6, decode it. */
351f8829a4aSRandall Stewart 	if (d != NULL) {
352f8829a4aSRandall Stewart 		ip6cp = (struct ip6ctlparam *)d;
353f8829a4aSRandall Stewart 	} else {
354f8829a4aSRandall Stewart 		ip6cp = (struct ip6ctlparam *)NULL;
355f8829a4aSRandall Stewart 	}
356f8829a4aSRandall Stewart 
357f8829a4aSRandall Stewart 	if (ip6cp) {
358f8829a4aSRandall Stewart 		/*
359f8829a4aSRandall Stewart 		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
360f8829a4aSRandall Stewart 		 * valid.
361f8829a4aSRandall Stewart 		 */
362f8829a4aSRandall Stewart 		/* check if we can safely examine src and dst ports */
363f8829a4aSRandall Stewart 		struct sctp_inpcb *inp = NULL;
364f8829a4aSRandall Stewart 		struct sctp_tcb *stcb = NULL;
365f8829a4aSRandall Stewart 		struct sctp_nets *net = NULL;
366f8829a4aSRandall Stewart 		struct sockaddr_in6 final;
367f8829a4aSRandall Stewart 
368139bc87fSRandall Stewart 		if (ip6cp->ip6c_m == NULL)
369f8829a4aSRandall Stewart 			return;
370f8829a4aSRandall Stewart 
371f8829a4aSRandall Stewart 		bzero(&sh, sizeof(sh));
372f8829a4aSRandall Stewart 		bzero(&final, sizeof(final));
373f8829a4aSRandall Stewart 		inp = NULL;
374f8829a4aSRandall Stewart 		net = NULL;
375f8829a4aSRandall Stewart 		m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
376f8829a4aSRandall Stewart 		    (caddr_t)&sh);
377f8829a4aSRandall Stewart 		ip6cp->ip6c_src->sin6_port = sh.src_port;
378f8829a4aSRandall Stewart 		final.sin6_len = sizeof(final);
379f8829a4aSRandall Stewart 		final.sin6_family = AF_INET6;
380f8829a4aSRandall Stewart 		final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
381f8829a4aSRandall Stewart 		final.sin6_port = sh.dest_port;
382f8829a4aSRandall Stewart 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
383f8829a4aSRandall Stewart 		    (struct sockaddr *)&final,
38442551e99SRandall Stewart 		    &inp, &net, 1, vrf_id);
385f8829a4aSRandall Stewart 		/* inp's ref-count increased && stcb locked */
386f8829a4aSRandall Stewart 		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
387f8829a4aSRandall Stewart 			if (cmd == PRC_MSGSIZE) {
388f8829a4aSRandall Stewart 				sctp6_notify_mbuf(inp,
389f8829a4aSRandall Stewart 				    ip6cp->ip6c_icmp6,
390f8829a4aSRandall Stewart 				    &sh,
391f8829a4aSRandall Stewart 				    stcb,
392f8829a4aSRandall Stewart 				    net);
393f8829a4aSRandall Stewart 				/* inp's ref-count reduced && stcb unlocked */
394f8829a4aSRandall Stewart 			} else {
395f8829a4aSRandall Stewart 				if (cmd == PRC_HOSTDEAD) {
396f8829a4aSRandall Stewart 					cm = EHOSTUNREACH;
397f8829a4aSRandall Stewart 				} else {
398f8829a4aSRandall Stewart 					cm = inet6ctlerrmap[cmd];
399f8829a4aSRandall Stewart 				}
400f8829a4aSRandall Stewart 				sctp_notify(inp, cm, &sh,
401f8829a4aSRandall Stewart 				    (struct sockaddr *)&final,
402f8829a4aSRandall Stewart 				    stcb, net);
403f8829a4aSRandall Stewart 				/* inp's ref-count reduced && stcb unlocked */
404f8829a4aSRandall Stewart 			}
405f8829a4aSRandall Stewart 		} else {
406f8829a4aSRandall Stewart 			if (PRC_IS_REDIRECT(cmd) && inp) {
407f8829a4aSRandall Stewart 				in6_rtchange((struct in6pcb *)inp,
408f8829a4aSRandall Stewart 				    inet6ctlerrmap[cmd]);
409f8829a4aSRandall Stewart 			}
410f8829a4aSRandall Stewart 			if (inp) {
411f8829a4aSRandall Stewart 				/* reduce inp's ref-count */
412f8829a4aSRandall Stewart 				SCTP_INP_WLOCK(inp);
413f8829a4aSRandall Stewart 				SCTP_INP_DECR_REF(inp);
414f8829a4aSRandall Stewart 				SCTP_INP_WUNLOCK(inp);
415f8829a4aSRandall Stewart 			}
416f8829a4aSRandall Stewart 			if (stcb)
417f8829a4aSRandall Stewart 				SCTP_TCB_UNLOCK(stcb);
418f8829a4aSRandall Stewart 		}
419f8829a4aSRandall Stewart 	}
420f8829a4aSRandall Stewart }
421f8829a4aSRandall Stewart 
422f8829a4aSRandall Stewart /*
423f8829a4aSRandall Stewart  * this routine can probably be collasped into the one in sctp_userreq.c
424f8829a4aSRandall Stewart  * since they do the same thing and now we lookup with a sockaddr
425f8829a4aSRandall Stewart  */
426f8829a4aSRandall Stewart static int
427f8829a4aSRandall Stewart sctp6_getcred(SYSCTL_HANDLER_ARGS)
428f8829a4aSRandall Stewart {
42903b0b021SRandall Stewart 	struct xucred xuc;
430f8829a4aSRandall Stewart 	struct sockaddr_in6 addrs[2];
431f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
432f8829a4aSRandall Stewart 	struct sctp_nets *net;
433f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
43403b0b021SRandall Stewart 	int error;
43542551e99SRandall Stewart 	uint32_t vrf_id;
43642551e99SRandall Stewart 
43742551e99SRandall Stewart 	vrf_id = SCTP_DEFAULT_VRFID;
438f8829a4aSRandall Stewart 
43932f9753cSRobert Watson 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
440f8829a4aSRandall Stewart 	if (error)
441f8829a4aSRandall Stewart 		return (error);
442f8829a4aSRandall Stewart 
443c4739e2fSRandall Stewart 	if (req->newlen != sizeof(addrs)) {
444c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
445f8829a4aSRandall Stewart 		return (EINVAL);
446c4739e2fSRandall Stewart 	}
447c4739e2fSRandall Stewart 	if (req->oldlen != sizeof(struct ucred)) {
448c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
449f8829a4aSRandall Stewart 		return (EINVAL);
450c4739e2fSRandall Stewart 	}
451f8829a4aSRandall Stewart 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
452f8829a4aSRandall Stewart 	if (error)
453f8829a4aSRandall Stewart 		return (error);
454f8829a4aSRandall Stewart 
455f8829a4aSRandall Stewart 	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
456f8829a4aSRandall Stewart 	    sin6tosa(&addrs[1]),
45742551e99SRandall Stewart 	    &inp, &net, 1, vrf_id);
458f8829a4aSRandall Stewart 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
45903b0b021SRandall Stewart 		if ((inp != NULL) && (stcb == NULL)) {
46003b0b021SRandall Stewart 			/* reduce ref-count */
461f8829a4aSRandall Stewart 			SCTP_INP_WLOCK(inp);
462f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
46303b0b021SRandall Stewart 			goto cred_can_cont;
464f8829a4aSRandall Stewart 		}
465c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
46603b0b021SRandall Stewart 		error = ENOENT;
467f8829a4aSRandall Stewart 		goto out;
468f8829a4aSRandall Stewart 	}
469f8829a4aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
47003b0b021SRandall Stewart 	/*
47103b0b021SRandall Stewart 	 * We use the write lock here, only since in the error leg we need
47203b0b021SRandall Stewart 	 * it. If we used RLOCK, then we would have to
47303b0b021SRandall Stewart 	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
47403b0b021SRandall Stewart 	 * Better to use higher wlock.
47503b0b021SRandall Stewart 	 */
47603b0b021SRandall Stewart 	SCTP_INP_WLOCK(inp);
47703b0b021SRandall Stewart cred_can_cont:
47803b0b021SRandall Stewart 	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
47903b0b021SRandall Stewart 	if (error) {
48003b0b021SRandall Stewart 		SCTP_INP_WUNLOCK(inp);
48103b0b021SRandall Stewart 		goto out;
48203b0b021SRandall Stewart 	}
48303b0b021SRandall Stewart 	cru2x(inp->sctp_socket->so_cred, &xuc);
48403b0b021SRandall Stewart 	SCTP_INP_WUNLOCK(inp);
48503b0b021SRandall Stewart 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
486f8829a4aSRandall Stewart out:
487f8829a4aSRandall Stewart 	return (error);
488f8829a4aSRandall Stewart }
489f8829a4aSRandall Stewart 
490f8829a4aSRandall Stewart SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
491f8829a4aSRandall Stewart     0, 0,
492f8829a4aSRandall Stewart     sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
493f8829a4aSRandall Stewart 
494f8829a4aSRandall Stewart 
495f8829a4aSRandall Stewart /* This is the same as the sctp_abort() could be made common */
496f8829a4aSRandall Stewart static void
497f8829a4aSRandall Stewart sctp6_abort(struct socket *so)
498f8829a4aSRandall Stewart {
499f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
500f8829a4aSRandall Stewart 	uint32_t flags;
501f8829a4aSRandall Stewart 
502f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
503f8829a4aSRandall Stewart 	if (inp == 0)
504f8829a4aSRandall Stewart 		return;
505f8829a4aSRandall Stewart sctp_must_try_again:
506f8829a4aSRandall Stewart 	flags = inp->sctp_flags;
507f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
508f8829a4aSRandall Stewart 	sctp_log_closing(inp, NULL, 17);
509f8829a4aSRandall Stewart #endif
510f8829a4aSRandall Stewart 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
511f8829a4aSRandall Stewart 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
512f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
513f8829a4aSRandall Stewart 		sctp_log_closing(inp, NULL, 16);
514f8829a4aSRandall Stewart #endif
515b0552ae2SRandall Stewart 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
516b0552ae2SRandall Stewart 		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
517f8829a4aSRandall Stewart 		SOCK_LOCK(so);
518132dea7dSRandall Stewart 		SCTP_SB_CLEAR(so->so_snd);
519f8829a4aSRandall Stewart 		/*
520f8829a4aSRandall Stewart 		 * same for the rcv ones, they are only here for the
521f8829a4aSRandall Stewart 		 * accounting/select.
522f8829a4aSRandall Stewart 		 */
523132dea7dSRandall Stewart 		SCTP_SB_CLEAR(so->so_rcv);
524132dea7dSRandall Stewart 		/* Now null out the reference, we are completely detached. */
525f8829a4aSRandall Stewart 		so->so_pcb = NULL;
526f8829a4aSRandall Stewart 		SOCK_UNLOCK(so);
527f8829a4aSRandall Stewart 	} else {
528f8829a4aSRandall Stewart 		flags = inp->sctp_flags;
529f8829a4aSRandall Stewart 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
530f8829a4aSRandall Stewart 			goto sctp_must_try_again;
531f8829a4aSRandall Stewart 		}
532f8829a4aSRandall Stewart 	}
533f8829a4aSRandall Stewart 	return;
534f8829a4aSRandall Stewart }
535f8829a4aSRandall Stewart 
536f8829a4aSRandall Stewart static int
537f8829a4aSRandall Stewart sctp6_attach(struct socket *so, int proto, struct thread *p)
538f8829a4aSRandall Stewart {
539f8829a4aSRandall Stewart 	struct in6pcb *inp6;
54093164cf9SRandall Stewart 	int error;
541f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
542f4c93d24SRandall Stewart 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
543f8829a4aSRandall Stewart 
544f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
545c4739e2fSRandall Stewart 	if (inp != NULL) {
546c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
547f8829a4aSRandall Stewart 		return EINVAL;
548c4739e2fSRandall Stewart 	}
549f8829a4aSRandall Stewart 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
550132dea7dSRandall Stewart 		error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
551f8829a4aSRandall Stewart 		if (error)
552f8829a4aSRandall Stewart 			return error;
553f8829a4aSRandall Stewart 	}
554f4c93d24SRandall Stewart 	error = sctp_inpcb_alloc(so, vrf_id);
555f8829a4aSRandall Stewart 	if (error)
556f8829a4aSRandall Stewart 		return error;
557f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
558f4c93d24SRandall Stewart 	SCTP_INP_WLOCK(inp);
559f8829a4aSRandall Stewart 	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
560f8829a4aSRandall Stewart 	inp6 = (struct in6pcb *)inp;
561f8829a4aSRandall Stewart 
562f8829a4aSRandall Stewart 	inp6->inp_vflag |= INP_IPV6;
563f8829a4aSRandall Stewart 	inp6->in6p_hops = -1;	/* use kernel default */
564f8829a4aSRandall Stewart 	inp6->in6p_cksum = -1;	/* just to be sure */
565f8829a4aSRandall Stewart #ifdef INET
566f8829a4aSRandall Stewart 	/*
567f8829a4aSRandall Stewart 	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
568f8829a4aSRandall Stewart 	 * socket as well, because the socket may be bound to an IPv6
569f8829a4aSRandall Stewart 	 * wildcard address, which may match an IPv4-mapped IPv6 address.
570f8829a4aSRandall Stewart 	 */
571f8829a4aSRandall Stewart 	inp6->inp_ip_ttl = ip_defttl;
572f8829a4aSRandall Stewart #endif
573f8829a4aSRandall Stewart 	/*
574f8829a4aSRandall Stewart 	 * Hmm what about the IPSEC stuff that is missing here but in
575f8829a4aSRandall Stewart 	 * sctp_attach()?
576f8829a4aSRandall Stewart 	 */
577f4c93d24SRandall Stewart 	SCTP_INP_WUNLOCK(inp);
578f8829a4aSRandall Stewart 	return 0;
579f8829a4aSRandall Stewart }
580f8829a4aSRandall Stewart 
581f8829a4aSRandall Stewart static int
582f8829a4aSRandall Stewart sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
583f8829a4aSRandall Stewart {
584f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
585f8829a4aSRandall Stewart 	struct in6pcb *inp6;
58693164cf9SRandall Stewart 	int error;
587f8829a4aSRandall Stewart 
588f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
589c4739e2fSRandall Stewart 	if (inp == 0) {
590c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
591f8829a4aSRandall Stewart 		return EINVAL;
592c4739e2fSRandall Stewart 	}
593d61a0ae0SRandall Stewart 	if (addr) {
594d61a0ae0SRandall Stewart 		if ((addr->sa_family == AF_INET6) &&
595d61a0ae0SRandall Stewart 		    (addr->sa_len != sizeof(struct sockaddr_in6))) {
596c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
597d61a0ae0SRandall Stewart 			return EINVAL;
598d61a0ae0SRandall Stewart 		}
599d61a0ae0SRandall Stewart 		if ((addr->sa_family == AF_INET) &&
600d61a0ae0SRandall Stewart 		    (addr->sa_len != sizeof(struct sockaddr_in))) {
601c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
602d61a0ae0SRandall Stewart 			return EINVAL;
603d61a0ae0SRandall Stewart 		}
604d61a0ae0SRandall Stewart 	}
605f8829a4aSRandall Stewart 	inp6 = (struct in6pcb *)inp;
606f8829a4aSRandall Stewart 	inp6->inp_vflag &= ~INP_IPV4;
607f8829a4aSRandall Stewart 	inp6->inp_vflag |= INP_IPV6;
60844b7479bSRandall Stewart 	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
609f8829a4aSRandall Stewart 		if (addr->sa_family == AF_INET) {
610f8829a4aSRandall Stewart 			/* binding v4 addr to v6 socket, so reset flags */
611f8829a4aSRandall Stewart 			inp6->inp_vflag |= INP_IPV4;
612f8829a4aSRandall Stewart 			inp6->inp_vflag &= ~INP_IPV6;
613f8829a4aSRandall Stewart 		} else {
614f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6_p;
615f8829a4aSRandall Stewart 
616f8829a4aSRandall Stewart 			sin6_p = (struct sockaddr_in6 *)addr;
617f8829a4aSRandall Stewart 
618f8829a4aSRandall Stewart 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
619f8829a4aSRandall Stewart 				inp6->inp_vflag |= INP_IPV4;
620f8829a4aSRandall Stewart 			} else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
621f8829a4aSRandall Stewart 				struct sockaddr_in sin;
622f8829a4aSRandall Stewart 
623f8829a4aSRandall Stewart 				in6_sin6_2_sin(&sin, sin6_p);
624f8829a4aSRandall Stewart 				inp6->inp_vflag |= INP_IPV4;
625f8829a4aSRandall Stewart 				inp6->inp_vflag &= ~INP_IPV6;
6261b649582SRandall Stewart 				error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
627f8829a4aSRandall Stewart 				return error;
628f8829a4aSRandall Stewart 			}
629f8829a4aSRandall Stewart 		}
630f8829a4aSRandall Stewart 	} else if (addr != NULL) {
631f8829a4aSRandall Stewart 		/* IPV6_V6ONLY socket */
632f8829a4aSRandall Stewart 		if (addr->sa_family == AF_INET) {
633f8829a4aSRandall Stewart 			/* can't bind v4 addr to v6 only socket! */
634c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
635f8829a4aSRandall Stewart 			return EINVAL;
636f8829a4aSRandall Stewart 		} else {
637f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6_p;
638f8829a4aSRandall Stewart 
639f8829a4aSRandall Stewart 			sin6_p = (struct sockaddr_in6 *)addr;
640f8829a4aSRandall Stewart 
641f8829a4aSRandall Stewart 			if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
642f8829a4aSRandall Stewart 				/* can't bind v4-mapped addrs either! */
643f8829a4aSRandall Stewart 				/* NOTE: we don't support SIIT */
644c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
645f8829a4aSRandall Stewart 			return EINVAL;
646f8829a4aSRandall Stewart 		}
647f8829a4aSRandall Stewart 	}
6481b649582SRandall Stewart 	error = sctp_inpcb_bind(so, addr, NULL, p);
649f8829a4aSRandall Stewart 	return error;
650f8829a4aSRandall Stewart }
651f8829a4aSRandall Stewart 
652f8829a4aSRandall Stewart 
653f8829a4aSRandall Stewart static void
654f8829a4aSRandall Stewart sctp6_close(struct socket *so)
655f8829a4aSRandall Stewart {
656f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
657f8829a4aSRandall Stewart 	uint32_t flags;
658f8829a4aSRandall Stewart 
659f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
660f8829a4aSRandall Stewart 	if (inp == 0)
661f8829a4aSRandall Stewart 		return;
662f8829a4aSRandall Stewart 
663f8829a4aSRandall Stewart 	/*
664f8829a4aSRandall Stewart 	 * Inform all the lower layer assoc that we are done.
665f8829a4aSRandall Stewart 	 */
666f8829a4aSRandall Stewart sctp_must_try_again:
667f8829a4aSRandall Stewart 	flags = inp->sctp_flags;
668f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
669f8829a4aSRandall Stewart 	sctp_log_closing(inp, NULL, 17);
670f8829a4aSRandall Stewart #endif
671f8829a4aSRandall Stewart 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
672f8829a4aSRandall Stewart 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
673f8829a4aSRandall Stewart 		if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
674f8829a4aSRandall Stewart 		    (so->so_rcv.sb_cc > 0)) {
675f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
676f8829a4aSRandall Stewart 			sctp_log_closing(inp, NULL, 13);
677f8829a4aSRandall Stewart #endif
678b0552ae2SRandall Stewart 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT
679b0552ae2SRandall Stewart 			    ,SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
680f8829a4aSRandall Stewart 		} else {
681f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
682f8829a4aSRandall Stewart 			sctp_log_closing(inp, NULL, 14);
683f8829a4aSRandall Stewart #endif
684b0552ae2SRandall Stewart 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
685b0552ae2SRandall Stewart 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
686f8829a4aSRandall Stewart 		}
687f8829a4aSRandall Stewart 		/*
688f8829a4aSRandall Stewart 		 * The socket is now detached, no matter what the state of
689f8829a4aSRandall Stewart 		 * the SCTP association.
690f8829a4aSRandall Stewart 		 */
691f8829a4aSRandall Stewart 		SOCK_LOCK(so);
692132dea7dSRandall Stewart 		SCTP_SB_CLEAR(so->so_snd);
693f8829a4aSRandall Stewart 		/*
694f8829a4aSRandall Stewart 		 * same for the rcv ones, they are only here for the
695f8829a4aSRandall Stewart 		 * accounting/select.
696f8829a4aSRandall Stewart 		 */
697132dea7dSRandall Stewart 		SCTP_SB_CLEAR(so->so_rcv);
698132dea7dSRandall Stewart 		/* Now null out the reference, we are completely detached. */
699f8829a4aSRandall Stewart 		so->so_pcb = NULL;
700f8829a4aSRandall Stewart 		SOCK_UNLOCK(so);
701f8829a4aSRandall Stewart 	} else {
702f8829a4aSRandall Stewart 		flags = inp->sctp_flags;
703f8829a4aSRandall Stewart 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
704f8829a4aSRandall Stewart 			goto sctp_must_try_again;
705f8829a4aSRandall Stewart 		}
706f8829a4aSRandall Stewart 	}
707f8829a4aSRandall Stewart 	return;
708f8829a4aSRandall Stewart 
709f8829a4aSRandall Stewart }
710f8829a4aSRandall Stewart 
71142551e99SRandall Stewart /* This could be made common with sctp_detach() since they are identical */
712f8829a4aSRandall Stewart 
713c105859eSRandall Stewart static
714c105859eSRandall Stewart int
715f8829a4aSRandall Stewart sctp6_disconnect(struct socket *so)
716f8829a4aSRandall Stewart {
717f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
718f8829a4aSRandall Stewart 
719f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
720f8829a4aSRandall Stewart 	if (inp == NULL) {
721c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
722f8829a4aSRandall Stewart 		return (ENOTCONN);
723f8829a4aSRandall Stewart 	}
724f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(inp);
725f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
726f42a358aSRandall Stewart 		if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
727f8829a4aSRandall Stewart 			/* No connection */
728f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
729c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
730f8829a4aSRandall Stewart 			return (ENOTCONN);
731f8829a4aSRandall Stewart 		} else {
732f8829a4aSRandall Stewart 			int some_on_streamwheel = 0;
733f8829a4aSRandall Stewart 			struct sctp_association *asoc;
734f8829a4aSRandall Stewart 			struct sctp_tcb *stcb;
735f8829a4aSRandall Stewart 
736f8829a4aSRandall Stewart 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
737f8829a4aSRandall Stewart 			if (stcb == NULL) {
738f8829a4aSRandall Stewart 				SCTP_INP_RUNLOCK(inp);
739c4739e2fSRandall Stewart 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
740f8829a4aSRandall Stewart 				return (EINVAL);
741f8829a4aSRandall Stewart 			}
742f8829a4aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
743f8829a4aSRandall Stewart 			asoc = &stcb->asoc;
744f8829a4aSRandall Stewart 			if (((so->so_options & SO_LINGER) &&
745f8829a4aSRandall Stewart 			    (so->so_linger == 0)) ||
746f8829a4aSRandall Stewart 			    (so->so_rcv.sb_cc > 0)) {
747f8829a4aSRandall Stewart 				if (SCTP_GET_STATE(asoc) !=
748f8829a4aSRandall Stewart 				    SCTP_STATE_COOKIE_WAIT) {
749f8829a4aSRandall Stewart 					/* Left with Data unread */
750c105859eSRandall Stewart 					struct mbuf *op_err;
751f8829a4aSRandall Stewart 
752c105859eSRandall Stewart 					op_err = sctp_generate_invmanparam(SCTP_CAUSE_USER_INITIATED_ABT);
753c105859eSRandall Stewart 					sctp_send_abort_tcb(stcb, op_err);
754f8829a4aSRandall Stewart 					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
755f8829a4aSRandall Stewart 				}
756f8829a4aSRandall Stewart 				SCTP_INP_RUNLOCK(inp);
757f8829a4aSRandall Stewart 				if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
758f8829a4aSRandall Stewart 				    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
759f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
760f8829a4aSRandall Stewart 				}
761c4739e2fSRandall Stewart 				if (sctp_free_assoc(inp, stcb, SCTP_DONOT_SETSCOPE,
762c4739e2fSRandall Stewart 				    SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_2) == 0) {
763c4739e2fSRandall Stewart 					SCTP_TCB_UNLOCK(stcb);
764c4739e2fSRandall Stewart 				}
765f8829a4aSRandall Stewart 				/* No unlock tcb assoc is gone */
766f8829a4aSRandall Stewart 				return (0);
767f8829a4aSRandall Stewart 			}
768f8829a4aSRandall Stewart 			if (!TAILQ_EMPTY(&asoc->out_wheel)) {
769f8829a4aSRandall Stewart 				/* Check to see if some data queued */
770f8829a4aSRandall Stewart 				struct sctp_stream_out *outs;
771f8829a4aSRandall Stewart 
772f8829a4aSRandall Stewart 				TAILQ_FOREACH(outs, &asoc->out_wheel,
773f8829a4aSRandall Stewart 				    next_spoke) {
774f8829a4aSRandall Stewart 					if (!TAILQ_EMPTY(&outs->outqueue)) {
775f8829a4aSRandall Stewart 						some_on_streamwheel = 1;
776f8829a4aSRandall Stewart 						break;
777f8829a4aSRandall Stewart 					}
778f8829a4aSRandall Stewart 				}
779f8829a4aSRandall Stewart 			}
780f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue) &&
781f8829a4aSRandall Stewart 			    TAILQ_EMPTY(&asoc->sent_queue) &&
782f8829a4aSRandall Stewart 			    (some_on_streamwheel == 0)) {
783f8829a4aSRandall Stewart 				/* nothing queued to send, so I'm done... */
784f8829a4aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) !=
785f8829a4aSRandall Stewart 				    SCTP_STATE_SHUTDOWN_SENT) &&
786f8829a4aSRandall Stewart 				    (SCTP_GET_STATE(asoc) !=
787f8829a4aSRandall Stewart 				    SCTP_STATE_SHUTDOWN_ACK_SENT)) {
788f8829a4aSRandall Stewart 					/* only send SHUTDOWN the first time */
789f8829a4aSRandall Stewart 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
790f8829a4aSRandall Stewart 					sctp_chunk_output(stcb->sctp_ep, stcb, 1);
791f42a358aSRandall Stewart 					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
792f42a358aSRandall Stewart 					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
793f8829a4aSRandall Stewart 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
794f42a358aSRandall Stewart 					}
795c4739e2fSRandall Stewart 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
796f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
797f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb,
798f8829a4aSRandall Stewart 					    asoc->primary_destination);
799f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
800f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb,
801f8829a4aSRandall Stewart 					    asoc->primary_destination);
802f8829a4aSRandall Stewart 				}
803f8829a4aSRandall Stewart 			} else {
804f8829a4aSRandall Stewart 				/*
805f8829a4aSRandall Stewart 				 * we still got (or just got) data to send,
806f8829a4aSRandall Stewart 				 * so set SHUTDOWN_PENDING
807f8829a4aSRandall Stewart 				 */
808f8829a4aSRandall Stewart 				/*
809f8829a4aSRandall Stewart 				 * XXX sockets draft says that MSG_EOF
810f8829a4aSRandall Stewart 				 * should be sent with no data.  currently,
811f8829a4aSRandall Stewart 				 * we will allow user data to be sent first
812f8829a4aSRandall Stewart 				 * and move to SHUTDOWN-PENDING
813f8829a4aSRandall Stewart 				 */
814f8829a4aSRandall Stewart 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
815f8829a4aSRandall Stewart 			}
816f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
817f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
818f8829a4aSRandall Stewart 			return (0);
819f8829a4aSRandall Stewart 		}
820f8829a4aSRandall Stewart 	} else {
821f8829a4aSRandall Stewart 		/* UDP model does not support this */
822f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
823c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EOPNOTSUPP);
824f8829a4aSRandall Stewart 		return EOPNOTSUPP;
825f8829a4aSRandall Stewart 	}
826f8829a4aSRandall Stewart }
827f8829a4aSRandall Stewart 
828c105859eSRandall Stewart 
829f8829a4aSRandall Stewart int
830f8829a4aSRandall Stewart sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
831f8829a4aSRandall Stewart     struct mbuf *control, struct thread *p);
832f8829a4aSRandall Stewart 
833f8829a4aSRandall Stewart 
834f8829a4aSRandall Stewart static int
835f8829a4aSRandall Stewart sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
836f8829a4aSRandall Stewart     struct mbuf *control, struct thread *p)
837f8829a4aSRandall Stewart {
838f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
839f8829a4aSRandall Stewart 	struct inpcb *in_inp;
840f8829a4aSRandall Stewart 	struct in6pcb *inp6;
841f8829a4aSRandall Stewart 
842f8829a4aSRandall Stewart #ifdef INET
843f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6;
844f8829a4aSRandall Stewart 
845f8829a4aSRandall Stewart #endif				/* INET */
846f8829a4aSRandall Stewart 	/* No SPL needed since sctp_output does this */
847f8829a4aSRandall Stewart 
848f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
849f8829a4aSRandall Stewart 	if (inp == NULL) {
850f8829a4aSRandall Stewart 		if (control) {
85117205eccSRandall Stewart 			SCTP_RELEASE_PKT(control);
852f8829a4aSRandall Stewart 			control = NULL;
853f8829a4aSRandall Stewart 		}
85417205eccSRandall Stewart 		SCTP_RELEASE_PKT(m);
855c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
856f8829a4aSRandall Stewart 		return EINVAL;
857f8829a4aSRandall Stewart 	}
858f8829a4aSRandall Stewart 	in_inp = (struct inpcb *)inp;
859f8829a4aSRandall Stewart 	inp6 = (struct in6pcb *)inp;
860f8829a4aSRandall Stewart 	/*
861f8829a4aSRandall Stewart 	 * For the TCP model we may get a NULL addr, if we are a connected
862f8829a4aSRandall Stewart 	 * socket thats ok.
863f8829a4aSRandall Stewart 	 */
864f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
865f8829a4aSRandall Stewart 	    (addr == NULL)) {
866f8829a4aSRandall Stewart 		goto connected_type;
867f8829a4aSRandall Stewart 	}
868f8829a4aSRandall Stewart 	if (addr == NULL) {
86917205eccSRandall Stewart 		SCTP_RELEASE_PKT(m);
870f8829a4aSRandall Stewart 		if (control) {
87117205eccSRandall Stewart 			SCTP_RELEASE_PKT(control);
872f8829a4aSRandall Stewart 			control = NULL;
873f8829a4aSRandall Stewart 		}
874c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
875f8829a4aSRandall Stewart 		return (EDESTADDRREQ);
876f8829a4aSRandall Stewart 	}
877f8829a4aSRandall Stewart #ifdef INET
878f8829a4aSRandall Stewart 	sin6 = (struct sockaddr_in6 *)addr;
87944b7479bSRandall Stewart 	if (SCTP_IPV6_V6ONLY(inp6)) {
880f8829a4aSRandall Stewart 		/*
881f8829a4aSRandall Stewart 		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
882f8829a4aSRandall Stewart 		 * v4 addr or v4-mapped addr
883f8829a4aSRandall Stewart 		 */
884f8829a4aSRandall Stewart 		if (addr->sa_family == AF_INET) {
885c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
886f8829a4aSRandall Stewart 			return EINVAL;
887f8829a4aSRandall Stewart 		}
888f8829a4aSRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
889c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
890f8829a4aSRandall Stewart 			return EINVAL;
891f8829a4aSRandall Stewart 		}
892f8829a4aSRandall Stewart 	}
893f8829a4aSRandall Stewart 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
894f8829a4aSRandall Stewart 		if (!ip6_v6only) {
895f8829a4aSRandall Stewart 			struct sockaddr_in sin;
896f8829a4aSRandall Stewart 
897f8829a4aSRandall Stewart 			/* convert v4-mapped into v4 addr and send */
898f8829a4aSRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
899f8829a4aSRandall Stewart 			return sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
900f8829a4aSRandall Stewart 			    control, p);
901f8829a4aSRandall Stewart 		} else {
902f8829a4aSRandall Stewart 			/* mapped addresses aren't enabled */
903c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
904f8829a4aSRandall Stewart 			return EINVAL;
905f8829a4aSRandall Stewart 		}
906f8829a4aSRandall Stewart 	}
907f8829a4aSRandall Stewart #endif				/* INET */
908f8829a4aSRandall Stewart connected_type:
909f8829a4aSRandall Stewart 	/* now what about control */
910f8829a4aSRandall Stewart 	if (control) {
911f8829a4aSRandall Stewart 		if (inp->control) {
912ad81507eSRandall Stewart 			SCTP_PRINTF("huh? control set?\n");
91317205eccSRandall Stewart 			SCTP_RELEASE_PKT(inp->control);
914f8829a4aSRandall Stewart 			inp->control = NULL;
915f8829a4aSRandall Stewart 		}
916f8829a4aSRandall Stewart 		inp->control = control;
917f8829a4aSRandall Stewart 	}
918f8829a4aSRandall Stewart 	/* Place the data */
919f8829a4aSRandall Stewart 	if (inp->pkt) {
920139bc87fSRandall Stewart 		SCTP_BUF_NEXT(inp->pkt_last) = m;
921f8829a4aSRandall Stewart 		inp->pkt_last = m;
922f8829a4aSRandall Stewart 	} else {
923f8829a4aSRandall Stewart 		inp->pkt_last = inp->pkt = m;
924f8829a4aSRandall Stewart 	}
925f8829a4aSRandall Stewart 	if (
926f8829a4aSRandall Stewart 	/* FreeBSD and MacOSX uses a flag passed */
927f8829a4aSRandall Stewart 	    ((flags & PRUS_MORETOCOME) == 0)
928f8829a4aSRandall Stewart 	    ) {
929f8829a4aSRandall Stewart 		/*
930f8829a4aSRandall Stewart 		 * note with the current version this code will only be used
931f8829a4aSRandall Stewart 		 * by OpenBSD, NetBSD and FreeBSD have methods for
932f8829a4aSRandall Stewart 		 * re-defining sosend() to use sctp_sosend().  One can
933f8829a4aSRandall Stewart 		 * optionaly switch back to this code (by changing back the
934f8829a4aSRandall Stewart 		 * defininitions but this is not advisable.
935f8829a4aSRandall Stewart 		 */
936f8829a4aSRandall Stewart 		int ret;
937f8829a4aSRandall Stewart 
938f8829a4aSRandall Stewart 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
939f8829a4aSRandall Stewart 		inp->pkt = NULL;
940f8829a4aSRandall Stewart 		inp->control = NULL;
941f8829a4aSRandall Stewart 		return (ret);
942f8829a4aSRandall Stewart 	} else {
943f8829a4aSRandall Stewart 		return (0);
944f8829a4aSRandall Stewart 	}
945f8829a4aSRandall Stewart }
946f8829a4aSRandall Stewart 
947f8829a4aSRandall Stewart static int
948f8829a4aSRandall Stewart sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
949f8829a4aSRandall Stewart {
95042551e99SRandall Stewart 	uint32_t vrf_id;
951f8829a4aSRandall Stewart 	int error = 0;
952f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
953f8829a4aSRandall Stewart 	struct in6pcb *inp6;
954f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
955f8829a4aSRandall Stewart 
956f8829a4aSRandall Stewart #ifdef INET
957f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6;
958f8829a4aSRandall Stewart 	struct sockaddr_storage ss;
959f8829a4aSRandall Stewart 
960f8829a4aSRandall Stewart #endif				/* INET */
961f8829a4aSRandall Stewart 
962f8829a4aSRandall Stewart 	inp6 = (struct in6pcb *)so->so_pcb;
963f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
964f8829a4aSRandall Stewart 	if (inp == 0) {
965c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
966f8829a4aSRandall Stewart 		return (ECONNRESET);	/* I made the same as TCP since we are
967f8829a4aSRandall Stewart 					 * not setup? */
968f8829a4aSRandall Stewart 	}
969d61a0ae0SRandall Stewart 	if (addr == NULL) {
970c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
971d61a0ae0SRandall Stewart 		return (EINVAL);
972d61a0ae0SRandall Stewart 	}
973d61a0ae0SRandall Stewart 	if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
974c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
975d61a0ae0SRandall Stewart 		return (EINVAL);
976d61a0ae0SRandall Stewart 	}
977d61a0ae0SRandall Stewart 	if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
978c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
979d61a0ae0SRandall Stewart 		return (EINVAL);
980d61a0ae0SRandall Stewart 	}
981bff64a4dSRandall Stewart 	vrf_id = inp->def_vrf_id;
982f8829a4aSRandall Stewart 	SCTP_ASOC_CREATE_LOCK(inp);
983f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(inp);
984f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
985f8829a4aSRandall Stewart 	    SCTP_PCB_FLAGS_UNBOUND) {
986f8829a4aSRandall Stewart 		/* Bind a ephemeral port */
987f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
988f8829a4aSRandall Stewart 		error = sctp6_bind(so, NULL, p);
989f8829a4aSRandall Stewart 		if (error) {
990f8829a4aSRandall Stewart 			SCTP_ASOC_CREATE_UNLOCK(inp);
991f8829a4aSRandall Stewart 
992f8829a4aSRandall Stewart 			return (error);
993f8829a4aSRandall Stewart 		}
994f8829a4aSRandall Stewart 		SCTP_INP_RLOCK(inp);
995f8829a4aSRandall Stewart 	}
996f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
997f8829a4aSRandall Stewart 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
998f8829a4aSRandall Stewart 		/* We are already connected AND the TCP model */
999f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
1000f8829a4aSRandall Stewart 		SCTP_ASOC_CREATE_UNLOCK(inp);
1001c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
1002f8829a4aSRandall Stewart 		return (EADDRINUSE);
1003f8829a4aSRandall Stewart 	}
1004f8829a4aSRandall Stewart #ifdef INET
1005f8829a4aSRandall Stewart 	sin6 = (struct sockaddr_in6 *)addr;
100644b7479bSRandall Stewart 	if (SCTP_IPV6_V6ONLY(inp6)) {
1007f8829a4aSRandall Stewart 		/*
1008f8829a4aSRandall Stewart 		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
1009f8829a4aSRandall Stewart 		 * addr or v4-mapped addr
1010f8829a4aSRandall Stewart 		 */
1011f8829a4aSRandall Stewart 		if (addr->sa_family == AF_INET) {
1012f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
1013f8829a4aSRandall Stewart 			SCTP_ASOC_CREATE_UNLOCK(inp);
1014c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1015f8829a4aSRandall Stewart 			return EINVAL;
1016f8829a4aSRandall Stewart 		}
1017f8829a4aSRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1018f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
1019f8829a4aSRandall Stewart 			SCTP_ASOC_CREATE_UNLOCK(inp);
1020c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1021f8829a4aSRandall Stewart 			return EINVAL;
1022f8829a4aSRandall Stewart 		}
1023f8829a4aSRandall Stewart 	}
1024f8829a4aSRandall Stewart 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1025f8829a4aSRandall Stewart 		if (!ip6_v6only) {
1026f8829a4aSRandall Stewart 			/* convert v4-mapped into v4 addr */
1027f8829a4aSRandall Stewart 			in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1028f8829a4aSRandall Stewart 			addr = (struct sockaddr *)&ss;
1029f8829a4aSRandall Stewart 		} else {
1030f8829a4aSRandall Stewart 			/* mapped addresses aren't enabled */
1031f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
1032f8829a4aSRandall Stewart 			SCTP_ASOC_CREATE_UNLOCK(inp);
1033c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1034f8829a4aSRandall Stewart 			return EINVAL;
1035f8829a4aSRandall Stewart 		}
1036f8829a4aSRandall Stewart 	} else
1037f8829a4aSRandall Stewart #endif				/* INET */
1038f8829a4aSRandall Stewart 		addr = addr;	/* for true v6 address case */
1039f8829a4aSRandall Stewart 
1040f8829a4aSRandall Stewart 	/* Now do we connect? */
1041f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1042f8829a4aSRandall Stewart 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1043ad81507eSRandall Stewart 		if (stcb) {
1044f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1045ad81507eSRandall Stewart 		}
1046f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
1047f8829a4aSRandall Stewart 	} else {
1048f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
1049f8829a4aSRandall Stewart 		SCTP_INP_WLOCK(inp);
1050f8829a4aSRandall Stewart 		SCTP_INP_INCR_REF(inp);
1051f8829a4aSRandall Stewart 		SCTP_INP_WUNLOCK(inp);
1052f8829a4aSRandall Stewart 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1053f8829a4aSRandall Stewart 		if (stcb == NULL) {
1054f8829a4aSRandall Stewart 			SCTP_INP_WLOCK(inp);
1055f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
1056f8829a4aSRandall Stewart 			SCTP_INP_WUNLOCK(inp);
1057f8829a4aSRandall Stewart 		}
1058f8829a4aSRandall Stewart 	}
1059f8829a4aSRandall Stewart 
1060f8829a4aSRandall Stewart 	if (stcb != NULL) {
1061f8829a4aSRandall Stewart 		/* Already have or am bring up an association */
1062f8829a4aSRandall Stewart 		SCTP_ASOC_CREATE_UNLOCK(inp);
1063f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1064c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
1065f8829a4aSRandall Stewart 		return (EALREADY);
1066f8829a4aSRandall Stewart 	}
1067f8829a4aSRandall Stewart 	/* We are GOOD to go */
106852be287eSRandall Stewart 	stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
1069f8829a4aSRandall Stewart 	SCTP_ASOC_CREATE_UNLOCK(inp);
1070f8829a4aSRandall Stewart 	if (stcb == NULL) {
1071f8829a4aSRandall Stewart 		/* Gak! no memory */
1072f8829a4aSRandall Stewart 		return (error);
1073f8829a4aSRandall Stewart 	}
1074f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1075f8829a4aSRandall Stewart 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1076f8829a4aSRandall Stewart 		/* Set the connected flag so we can queue data */
1077f8829a4aSRandall Stewart 		soisconnecting(so);
1078f8829a4aSRandall Stewart 	}
1079f8829a4aSRandall Stewart 	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1080ad81507eSRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1081f8829a4aSRandall Stewart 
1082f8829a4aSRandall Stewart 	/* initialize authentication parameters for the assoc */
1083f8829a4aSRandall Stewart 	sctp_initialize_auth_params(inp, stcb);
1084f8829a4aSRandall Stewart 
1085f8829a4aSRandall Stewart 	sctp_send_initiate(inp, stcb);
1086f8829a4aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
1087f8829a4aSRandall Stewart 	return error;
1088f8829a4aSRandall Stewart }
1089f8829a4aSRandall Stewart 
1090f8829a4aSRandall Stewart static int
1091f8829a4aSRandall Stewart sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1092f8829a4aSRandall Stewart {
1093f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6;
1094f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
109542551e99SRandall Stewart 	uint32_t vrf_id;
109642551e99SRandall Stewart 	struct sctp_ifa *sctp_ifa;
1097f8829a4aSRandall Stewart 
1098f8829a4aSRandall Stewart 	int error;
1099f8829a4aSRandall Stewart 
1100f8829a4aSRandall Stewart 	/*
1101f8829a4aSRandall Stewart 	 * Do the malloc first in case it blocks.
1102f8829a4aSRandall Stewart 	 */
1103f8829a4aSRandall Stewart 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1104f8829a4aSRandall Stewart 	sin6->sin6_family = AF_INET6;
1105f8829a4aSRandall Stewart 	sin6->sin6_len = sizeof(*sin6);
1106f8829a4aSRandall Stewart 
1107f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
1108f8829a4aSRandall Stewart 	if (inp == NULL) {
1109f8829a4aSRandall Stewart 		SCTP_FREE_SONAME(sin6);
1110c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1111f8829a4aSRandall Stewart 		return ECONNRESET;
1112f8829a4aSRandall Stewart 	}
1113f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(inp);
1114f8829a4aSRandall Stewart 	sin6->sin6_port = inp->sctp_lport;
1115f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1116f8829a4aSRandall Stewart 		/* For the bound all case you get back 0 */
1117f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1118f8829a4aSRandall Stewart 			struct sctp_tcb *stcb;
1119f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin_a6;
1120f8829a4aSRandall Stewart 			struct sctp_nets *net;
1121f8829a4aSRandall Stewart 			int fnd;
1122f8829a4aSRandall Stewart 
1123f8829a4aSRandall Stewart 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1124f8829a4aSRandall Stewart 			if (stcb == NULL) {
1125f8829a4aSRandall Stewart 				goto notConn6;
1126f8829a4aSRandall Stewart 			}
1127f8829a4aSRandall Stewart 			fnd = 0;
1128f8829a4aSRandall Stewart 			sin_a6 = NULL;
1129f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1130f8829a4aSRandall Stewart 				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
113103b0b021SRandall Stewart 				if (sin_a6 == NULL)
113203b0b021SRandall Stewart 					/* this will make coverity happy */
113303b0b021SRandall Stewart 					continue;
113403b0b021SRandall Stewart 
1135f8829a4aSRandall Stewart 				if (sin_a6->sin6_family == AF_INET6) {
1136f8829a4aSRandall Stewart 					fnd = 1;
1137f8829a4aSRandall Stewart 					break;
1138f8829a4aSRandall Stewart 				}
1139f8829a4aSRandall Stewart 			}
1140f8829a4aSRandall Stewart 			if ((!fnd) || (sin_a6 == NULL)) {
1141f8829a4aSRandall Stewart 				/* punt */
1142f8829a4aSRandall Stewart 				goto notConn6;
1143f8829a4aSRandall Stewart 			}
1144bff64a4dSRandall Stewart 			vrf_id = inp->def_vrf_id;
1145bff64a4dSRandall Stewart 			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
114642551e99SRandall Stewart 			if (sctp_ifa) {
114742551e99SRandall Stewart 				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
114842551e99SRandall Stewart 			}
1149f8829a4aSRandall Stewart 		} else {
1150f8829a4aSRandall Stewart 			/* For the bound all case you get back 0 */
1151f8829a4aSRandall Stewart 	notConn6:
1152f8829a4aSRandall Stewart 			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1153f8829a4aSRandall Stewart 		}
1154f8829a4aSRandall Stewart 	} else {
1155f8829a4aSRandall Stewart 		/* Take the first IPv6 address in the list */
1156f8829a4aSRandall Stewart 		struct sctp_laddr *laddr;
1157f8829a4aSRandall Stewart 		int fnd = 0;
1158f8829a4aSRandall Stewart 
1159f8829a4aSRandall Stewart 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
116042551e99SRandall Stewart 			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1161f8829a4aSRandall Stewart 				struct sockaddr_in6 *sin_a;
1162f8829a4aSRandall Stewart 
116342551e99SRandall Stewart 				sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1164f8829a4aSRandall Stewart 				sin6->sin6_addr = sin_a->sin6_addr;
1165f8829a4aSRandall Stewart 				fnd = 1;
1166f8829a4aSRandall Stewart 				break;
1167f8829a4aSRandall Stewart 			}
1168f8829a4aSRandall Stewart 		}
1169f8829a4aSRandall Stewart 		if (!fnd) {
1170f8829a4aSRandall Stewart 			SCTP_FREE_SONAME(sin6);
1171f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
1172c4739e2fSRandall Stewart 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1173f8829a4aSRandall Stewart 			return ENOENT;
1174f8829a4aSRandall Stewart 		}
1175f8829a4aSRandall Stewart 	}
1176f8829a4aSRandall Stewart 	SCTP_INP_RUNLOCK(inp);
1177f8829a4aSRandall Stewart 	/* Scoping things for v6 */
117803b0b021SRandall Stewart 	if ((error = sa6_recoverscope(sin6)) != 0) {
117903b0b021SRandall Stewart 		SCTP_FREE_SONAME(sin6);
1180f8829a4aSRandall Stewart 		return (error);
118103b0b021SRandall Stewart 	}
1182f8829a4aSRandall Stewart 	(*addr) = (struct sockaddr *)sin6;
1183f8829a4aSRandall Stewart 	return (0);
1184f8829a4aSRandall Stewart }
1185f8829a4aSRandall Stewart 
1186f8829a4aSRandall Stewart static int
1187f8829a4aSRandall Stewart sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1188f8829a4aSRandall Stewart {
1189f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1190f8829a4aSRandall Stewart 	int fnd;
1191f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin_a6;
1192f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
1193f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1194f8829a4aSRandall Stewart 	struct sctp_nets *net;
1195f8829a4aSRandall Stewart 
1196f8829a4aSRandall Stewart 	int error;
1197f8829a4aSRandall Stewart 
1198f8829a4aSRandall Stewart 	/*
1199f8829a4aSRandall Stewart 	 * Do the malloc first in case it blocks.
1200f8829a4aSRandall Stewart 	 */
1201f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
1202f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1203f8829a4aSRandall Stewart 		/* UDP type and listeners will drop out here */
1204c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1205f8829a4aSRandall Stewart 		return (ENOTCONN);
1206f8829a4aSRandall Stewart 	}
1207f8829a4aSRandall Stewart 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1208f8829a4aSRandall Stewart 	sin6->sin6_family = AF_INET6;
1209f8829a4aSRandall Stewart 	sin6->sin6_len = sizeof(*sin6);
1210f8829a4aSRandall Stewart 
1211f8829a4aSRandall Stewart 	/* We must recapture incase we blocked */
1212f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
1213f8829a4aSRandall Stewart 	if (inp == NULL) {
1214f8829a4aSRandall Stewart 		SCTP_FREE_SONAME(sin6);
1215c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1216f8829a4aSRandall Stewart 		return ECONNRESET;
1217f8829a4aSRandall Stewart 	}
1218f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(inp);
1219f8829a4aSRandall Stewart 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1220ad81507eSRandall Stewart 	if (stcb) {
1221f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
1222ad81507eSRandall Stewart 	}
1223f8829a4aSRandall Stewart 	SCTP_INP_RUNLOCK(inp);
1224f8829a4aSRandall Stewart 	if (stcb == NULL) {
1225f8829a4aSRandall Stewart 		SCTP_FREE_SONAME(sin6);
1226c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1227f8829a4aSRandall Stewart 		return ECONNRESET;
1228f8829a4aSRandall Stewart 	}
1229f8829a4aSRandall Stewart 	fnd = 0;
1230f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1231f8829a4aSRandall Stewart 		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1232f8829a4aSRandall Stewart 		if (sin_a6->sin6_family == AF_INET6) {
1233f8829a4aSRandall Stewart 			fnd = 1;
1234f8829a4aSRandall Stewart 			sin6->sin6_port = stcb->rport;
1235f8829a4aSRandall Stewart 			sin6->sin6_addr = sin_a6->sin6_addr;
1236f8829a4aSRandall Stewart 			break;
1237f8829a4aSRandall Stewart 		}
1238f8829a4aSRandall Stewart 	}
1239f8829a4aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
1240f8829a4aSRandall Stewart 	if (!fnd) {
1241f8829a4aSRandall Stewart 		/* No IPv4 address */
1242f8829a4aSRandall Stewart 		SCTP_FREE_SONAME(sin6);
1243c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1244f8829a4aSRandall Stewart 		return ENOENT;
1245f8829a4aSRandall Stewart 	}
1246f8829a4aSRandall Stewart 	if ((error = sa6_recoverscope(sin6)) != 0)
1247f8829a4aSRandall Stewart 		return (error);
1248f8829a4aSRandall Stewart 	*addr = (struct sockaddr *)sin6;
1249f8829a4aSRandall Stewart 	return (0);
1250f8829a4aSRandall Stewart }
1251f8829a4aSRandall Stewart 
1252f8829a4aSRandall Stewart static int
1253f8829a4aSRandall Stewart sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1254f8829a4aSRandall Stewart {
1255f8829a4aSRandall Stewart 	struct sockaddr *addr;
1256f8829a4aSRandall Stewart 	struct in6pcb *inp6 = sotoin6pcb(so);
125793164cf9SRandall Stewart 	int error;
1258f8829a4aSRandall Stewart 
1259c4739e2fSRandall Stewart 	if (inp6 == NULL) {
1260c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1261f8829a4aSRandall Stewart 		return EINVAL;
1262c4739e2fSRandall Stewart 	}
1263f8829a4aSRandall Stewart 	/* allow v6 addresses precedence */
1264f8829a4aSRandall Stewart 	error = sctp6_getaddr(so, nam);
1265f8829a4aSRandall Stewart 	if (error) {
1266f8829a4aSRandall Stewart 		/* try v4 next if v6 failed */
1267f8829a4aSRandall Stewart 		error = sctp_ingetaddr(so, nam);
1268f8829a4aSRandall Stewart 		if (error) {
1269f8829a4aSRandall Stewart 			return (error);
1270f8829a4aSRandall Stewart 		}
1271f8829a4aSRandall Stewart 		addr = *nam;
1272f8829a4aSRandall Stewart 		/* if I'm V6ONLY, convert it to v4-mapped */
127344b7479bSRandall Stewart 		if (SCTP_IPV6_V6ONLY(inp6)) {
1274f8829a4aSRandall Stewart 			struct sockaddr_in6 sin6;
1275f8829a4aSRandall Stewart 
1276f8829a4aSRandall Stewart 			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1277f8829a4aSRandall Stewart 			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1278c105859eSRandall Stewart 
1279f8829a4aSRandall Stewart 		}
1280f8829a4aSRandall Stewart 	}
1281f8829a4aSRandall Stewart 	return (error);
1282f8829a4aSRandall Stewart }
1283f8829a4aSRandall Stewart 
1284f8829a4aSRandall Stewart 
1285f8829a4aSRandall Stewart static int
1286f8829a4aSRandall Stewart sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1287f8829a4aSRandall Stewart {
1288f8829a4aSRandall Stewart 	struct sockaddr *addr = *nam;
1289f8829a4aSRandall Stewart 	struct in6pcb *inp6 = sotoin6pcb(so);
129093164cf9SRandall Stewart 	int error;
1291f8829a4aSRandall Stewart 
1292c4739e2fSRandall Stewart 	if (inp6 == NULL) {
1293c4739e2fSRandall Stewart 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1294f8829a4aSRandall Stewart 		return EINVAL;
1295c4739e2fSRandall Stewart 	}
1296f8829a4aSRandall Stewart 	/* allow v6 addresses precedence */
1297f8829a4aSRandall Stewart 	error = sctp6_peeraddr(so, nam);
1298f8829a4aSRandall Stewart 	if (error) {
1299f8829a4aSRandall Stewart 		/* try v4 next if v6 failed */
1300f8829a4aSRandall Stewart 		error = sctp_peeraddr(so, nam);
1301f8829a4aSRandall Stewart 		if (error) {
1302f8829a4aSRandall Stewart 			return (error);
1303f8829a4aSRandall Stewart 		}
1304f8829a4aSRandall Stewart 		/* if I'm V6ONLY, convert it to v4-mapped */
130544b7479bSRandall Stewart 		if (SCTP_IPV6_V6ONLY(inp6)) {
1306f8829a4aSRandall Stewart 			struct sockaddr_in6 sin6;
1307f8829a4aSRandall Stewart 
1308f8829a4aSRandall Stewart 			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1309f8829a4aSRandall Stewart 			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1310f8829a4aSRandall Stewart 		}
1311f8829a4aSRandall Stewart 	}
1312f8829a4aSRandall Stewart 	return error;
1313f8829a4aSRandall Stewart }
1314f8829a4aSRandall Stewart 
1315f8829a4aSRandall Stewart struct pr_usrreqs sctp6_usrreqs = {
1316f8829a4aSRandall Stewart 	.pru_abort = sctp6_abort,
1317f8829a4aSRandall Stewart 	.pru_accept = sctp_accept,
1318f8829a4aSRandall Stewart 	.pru_attach = sctp6_attach,
1319f8829a4aSRandall Stewart 	.pru_bind = sctp6_bind,
1320f8829a4aSRandall Stewart 	.pru_connect = sctp6_connect,
1321f8829a4aSRandall Stewart 	.pru_control = in6_control,
1322f8829a4aSRandall Stewart 	.pru_close = sctp6_close,
1323f8829a4aSRandall Stewart 	.pru_detach = sctp6_close,
1324f8829a4aSRandall Stewart 	.pru_sopoll = sopoll_generic,
1325f8829a4aSRandall Stewart 	.pru_disconnect = sctp6_disconnect,
1326f8829a4aSRandall Stewart 	.pru_listen = sctp_listen,
1327f8829a4aSRandall Stewart 	.pru_peeraddr = sctp6_getpeeraddr,
1328f8829a4aSRandall Stewart 	.pru_send = sctp6_send,
1329f8829a4aSRandall Stewart 	.pru_shutdown = sctp_shutdown,
1330f8829a4aSRandall Stewart 	.pru_sockaddr = sctp6_in6getaddr,
1331f8829a4aSRandall Stewart 	.pru_sosend = sctp_sosend,
1332f8829a4aSRandall Stewart 	.pru_soreceive = sctp_soreceive
1333f8829a4aSRandall Stewart };
1334