xref: /freebsd/sys/netinet6/sctp6_usrreq.c (revision 1b649582bb0e487bed04b4580c56d739e051eb35)
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 
443f8829a4aSRandall Stewart 	if (req->newlen != sizeof(addrs))
444f8829a4aSRandall Stewart 		return (EINVAL);
445f8829a4aSRandall Stewart 	if (req->oldlen != sizeof(struct ucred))
446f8829a4aSRandall Stewart 		return (EINVAL);
447f8829a4aSRandall Stewart 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
448f8829a4aSRandall Stewart 	if (error)
449f8829a4aSRandall Stewart 		return (error);
450f8829a4aSRandall Stewart 
451f8829a4aSRandall Stewart 	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
452f8829a4aSRandall Stewart 	    sin6tosa(&addrs[1]),
45342551e99SRandall Stewart 	    &inp, &net, 1, vrf_id);
454f8829a4aSRandall Stewart 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
45503b0b021SRandall Stewart 		if ((inp != NULL) && (stcb == NULL)) {
45603b0b021SRandall Stewart 			/* reduce ref-count */
457f8829a4aSRandall Stewart 			SCTP_INP_WLOCK(inp);
458f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
45903b0b021SRandall Stewart 			goto cred_can_cont;
460f8829a4aSRandall Stewart 		}
46103b0b021SRandall Stewart 		error = ENOENT;
462f8829a4aSRandall Stewart 		goto out;
463f8829a4aSRandall Stewart 	}
464f8829a4aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
46503b0b021SRandall Stewart 	/*
46603b0b021SRandall Stewart 	 * We use the write lock here, only since in the error leg we need
46703b0b021SRandall Stewart 	 * it. If we used RLOCK, then we would have to
46803b0b021SRandall Stewart 	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
46903b0b021SRandall Stewart 	 * Better to use higher wlock.
47003b0b021SRandall Stewart 	 */
47103b0b021SRandall Stewart 	SCTP_INP_WLOCK(inp);
47203b0b021SRandall Stewart cred_can_cont:
47303b0b021SRandall Stewart 	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
47403b0b021SRandall Stewart 	if (error) {
47503b0b021SRandall Stewart 		SCTP_INP_WUNLOCK(inp);
47603b0b021SRandall Stewart 		goto out;
47703b0b021SRandall Stewart 	}
47803b0b021SRandall Stewart 	cru2x(inp->sctp_socket->so_cred, &xuc);
47903b0b021SRandall Stewart 	SCTP_INP_WUNLOCK(inp);
48003b0b021SRandall Stewart 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
481f8829a4aSRandall Stewart out:
482f8829a4aSRandall Stewart 	return (error);
483f8829a4aSRandall Stewart }
484f8829a4aSRandall Stewart 
485f8829a4aSRandall Stewart SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
486f8829a4aSRandall Stewart     0, 0,
487f8829a4aSRandall Stewart     sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
488f8829a4aSRandall Stewart 
489f8829a4aSRandall Stewart 
490f8829a4aSRandall Stewart /* This is the same as the sctp_abort() could be made common */
491f8829a4aSRandall Stewart static void
492f8829a4aSRandall Stewart sctp6_abort(struct socket *so)
493f8829a4aSRandall Stewart {
494f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
495f8829a4aSRandall Stewart 	uint32_t flags;
496f8829a4aSRandall Stewart 
497f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
498f8829a4aSRandall Stewart 	if (inp == 0)
499f8829a4aSRandall Stewart 		return;
500f8829a4aSRandall Stewart sctp_must_try_again:
501f8829a4aSRandall Stewart 	flags = inp->sctp_flags;
502f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
503f8829a4aSRandall Stewart 	sctp_log_closing(inp, NULL, 17);
504f8829a4aSRandall Stewart #endif
505f8829a4aSRandall Stewart 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
506f8829a4aSRandall Stewart 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
507f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
508f8829a4aSRandall Stewart 		sctp_log_closing(inp, NULL, 16);
509f8829a4aSRandall Stewart #endif
510b0552ae2SRandall Stewart 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
511b0552ae2SRandall Stewart 		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
512f8829a4aSRandall Stewart 		SOCK_LOCK(so);
513132dea7dSRandall Stewart 		SCTP_SB_CLEAR(so->so_snd);
514f8829a4aSRandall Stewart 		/*
515f8829a4aSRandall Stewart 		 * same for the rcv ones, they are only here for the
516f8829a4aSRandall Stewart 		 * accounting/select.
517f8829a4aSRandall Stewart 		 */
518132dea7dSRandall Stewart 		SCTP_SB_CLEAR(so->so_rcv);
519132dea7dSRandall Stewart 		/* Now null out the reference, we are completely detached. */
520f8829a4aSRandall Stewart 		so->so_pcb = NULL;
521f8829a4aSRandall Stewart 		SOCK_UNLOCK(so);
522f8829a4aSRandall Stewart 	} else {
523f8829a4aSRandall Stewart 		flags = inp->sctp_flags;
524f8829a4aSRandall Stewart 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
525f8829a4aSRandall Stewart 			goto sctp_must_try_again;
526f8829a4aSRandall Stewart 		}
527f8829a4aSRandall Stewart 	}
528f8829a4aSRandall Stewart 	return;
529f8829a4aSRandall Stewart }
530f8829a4aSRandall Stewart 
531f8829a4aSRandall Stewart static int
532f8829a4aSRandall Stewart sctp6_attach(struct socket *so, int proto, struct thread *p)
533f8829a4aSRandall Stewart {
534f8829a4aSRandall Stewart 	struct in6pcb *inp6;
53593164cf9SRandall Stewart 	int error;
536f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
537f4c93d24SRandall Stewart 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
538f8829a4aSRandall Stewart 
539f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
540f8829a4aSRandall Stewart 	if (inp != NULL)
541f8829a4aSRandall Stewart 		return EINVAL;
542f8829a4aSRandall Stewart 
543f8829a4aSRandall Stewart 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
544132dea7dSRandall Stewart 		error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
545f8829a4aSRandall Stewart 		if (error)
546f8829a4aSRandall Stewart 			return error;
547f8829a4aSRandall Stewart 	}
548f4c93d24SRandall Stewart 	error = sctp_inpcb_alloc(so, vrf_id);
549f8829a4aSRandall Stewart 	if (error)
550f8829a4aSRandall Stewart 		return error;
551f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
552f4c93d24SRandall Stewart 	SCTP_INP_WLOCK(inp);
553f8829a4aSRandall Stewart 	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
554f8829a4aSRandall Stewart 	inp6 = (struct in6pcb *)inp;
555f8829a4aSRandall Stewart 
556f8829a4aSRandall Stewart 	inp6->inp_vflag |= INP_IPV6;
557f8829a4aSRandall Stewart 	inp6->in6p_hops = -1;	/* use kernel default */
558f8829a4aSRandall Stewart 	inp6->in6p_cksum = -1;	/* just to be sure */
559f8829a4aSRandall Stewart #ifdef INET
560f8829a4aSRandall Stewart 	/*
561f8829a4aSRandall Stewart 	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
562f8829a4aSRandall Stewart 	 * socket as well, because the socket may be bound to an IPv6
563f8829a4aSRandall Stewart 	 * wildcard address, which may match an IPv4-mapped IPv6 address.
564f8829a4aSRandall Stewart 	 */
565f8829a4aSRandall Stewart 	inp6->inp_ip_ttl = ip_defttl;
566f8829a4aSRandall Stewart #endif
567f8829a4aSRandall Stewart 	/*
568f8829a4aSRandall Stewart 	 * Hmm what about the IPSEC stuff that is missing here but in
569f8829a4aSRandall Stewart 	 * sctp_attach()?
570f8829a4aSRandall Stewart 	 */
571f4c93d24SRandall Stewart 	SCTP_INP_WUNLOCK(inp);
572f8829a4aSRandall Stewart 	return 0;
573f8829a4aSRandall Stewart }
574f8829a4aSRandall Stewart 
575f8829a4aSRandall Stewart static int
576f8829a4aSRandall Stewart sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
577f8829a4aSRandall Stewart {
578f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
579f8829a4aSRandall Stewart 	struct in6pcb *inp6;
58093164cf9SRandall Stewart 	int error;
581f8829a4aSRandall Stewart 
582f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
583f8829a4aSRandall Stewart 	if (inp == 0)
584f8829a4aSRandall Stewart 		return EINVAL;
585f8829a4aSRandall Stewart 
586d61a0ae0SRandall Stewart 	if (addr) {
587d61a0ae0SRandall Stewart 		if ((addr->sa_family == AF_INET6) &&
588d61a0ae0SRandall Stewart 		    (addr->sa_len != sizeof(struct sockaddr_in6))) {
589d61a0ae0SRandall Stewart 			return EINVAL;
590d61a0ae0SRandall Stewart 		}
591d61a0ae0SRandall Stewart 		if ((addr->sa_family == AF_INET) &&
592d61a0ae0SRandall Stewart 		    (addr->sa_len != sizeof(struct sockaddr_in))) {
593d61a0ae0SRandall Stewart 			return EINVAL;
594d61a0ae0SRandall Stewart 		}
595d61a0ae0SRandall Stewart 	}
596f8829a4aSRandall Stewart 	inp6 = (struct in6pcb *)inp;
597f8829a4aSRandall Stewart 	inp6->inp_vflag &= ~INP_IPV4;
598f8829a4aSRandall Stewart 	inp6->inp_vflag |= INP_IPV6;
59944b7479bSRandall Stewart 	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
600f8829a4aSRandall Stewart 		if (addr->sa_family == AF_INET) {
601f8829a4aSRandall Stewart 			/* binding v4 addr to v6 socket, so reset flags */
602f8829a4aSRandall Stewart 			inp6->inp_vflag |= INP_IPV4;
603f8829a4aSRandall Stewart 			inp6->inp_vflag &= ~INP_IPV6;
604f8829a4aSRandall Stewart 		} else {
605f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6_p;
606f8829a4aSRandall Stewart 
607f8829a4aSRandall Stewart 			sin6_p = (struct sockaddr_in6 *)addr;
608f8829a4aSRandall Stewart 
609f8829a4aSRandall Stewart 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
610f8829a4aSRandall Stewart 				inp6->inp_vflag |= INP_IPV4;
611f8829a4aSRandall Stewart 			} else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
612f8829a4aSRandall Stewart 				struct sockaddr_in sin;
613f8829a4aSRandall Stewart 
614f8829a4aSRandall Stewart 				in6_sin6_2_sin(&sin, sin6_p);
615f8829a4aSRandall Stewart 				inp6->inp_vflag |= INP_IPV4;
616f8829a4aSRandall Stewart 				inp6->inp_vflag &= ~INP_IPV6;
6171b649582SRandall Stewart 				error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
618f8829a4aSRandall Stewart 				return error;
619f8829a4aSRandall Stewart 			}
620f8829a4aSRandall Stewart 		}
621f8829a4aSRandall Stewart 	} else if (addr != NULL) {
622f8829a4aSRandall Stewart 		/* IPV6_V6ONLY socket */
623f8829a4aSRandall Stewart 		if (addr->sa_family == AF_INET) {
624f8829a4aSRandall Stewart 			/* can't bind v4 addr to v6 only socket! */
625f8829a4aSRandall Stewart 			return EINVAL;
626f8829a4aSRandall Stewart 		} else {
627f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin6_p;
628f8829a4aSRandall Stewart 
629f8829a4aSRandall Stewart 			sin6_p = (struct sockaddr_in6 *)addr;
630f8829a4aSRandall Stewart 
631f8829a4aSRandall Stewart 			if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
632f8829a4aSRandall Stewart 				/* can't bind v4-mapped addrs either! */
633f8829a4aSRandall Stewart 				/* NOTE: we don't support SIIT */
634f8829a4aSRandall Stewart 				return EINVAL;
635f8829a4aSRandall Stewart 		}
636f8829a4aSRandall Stewart 	}
6371b649582SRandall Stewart 	error = sctp_inpcb_bind(so, addr, NULL, p);
638f8829a4aSRandall Stewart 	return error;
639f8829a4aSRandall Stewart }
640f8829a4aSRandall Stewart 
641f8829a4aSRandall Stewart 
642f8829a4aSRandall Stewart static void
643f8829a4aSRandall Stewart sctp6_close(struct socket *so)
644f8829a4aSRandall Stewart {
645f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
646f8829a4aSRandall Stewart 	uint32_t flags;
647f8829a4aSRandall Stewart 
648f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
649f8829a4aSRandall Stewart 	if (inp == 0)
650f8829a4aSRandall Stewart 		return;
651f8829a4aSRandall Stewart 
652f8829a4aSRandall Stewart 	/*
653f8829a4aSRandall Stewart 	 * Inform all the lower layer assoc that we are done.
654f8829a4aSRandall Stewart 	 */
655f8829a4aSRandall Stewart sctp_must_try_again:
656f8829a4aSRandall Stewart 	flags = inp->sctp_flags;
657f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
658f8829a4aSRandall Stewart 	sctp_log_closing(inp, NULL, 17);
659f8829a4aSRandall Stewart #endif
660f8829a4aSRandall Stewart 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
661f8829a4aSRandall Stewart 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
662f8829a4aSRandall Stewart 		if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
663f8829a4aSRandall Stewart 		    (so->so_rcv.sb_cc > 0)) {
664f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
665f8829a4aSRandall Stewart 			sctp_log_closing(inp, NULL, 13);
666f8829a4aSRandall Stewart #endif
667b0552ae2SRandall Stewart 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT
668b0552ae2SRandall Stewart 			    ,SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
669f8829a4aSRandall Stewart 		} else {
670f8829a4aSRandall Stewart #ifdef SCTP_LOG_CLOSING
671f8829a4aSRandall Stewart 			sctp_log_closing(inp, NULL, 14);
672f8829a4aSRandall Stewart #endif
673b0552ae2SRandall Stewart 			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
674b0552ae2SRandall Stewart 			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
675f8829a4aSRandall Stewart 		}
676f8829a4aSRandall Stewart 		/*
677f8829a4aSRandall Stewart 		 * The socket is now detached, no matter what the state of
678f8829a4aSRandall Stewart 		 * the SCTP association.
679f8829a4aSRandall Stewart 		 */
680f8829a4aSRandall Stewart 		SOCK_LOCK(so);
681132dea7dSRandall Stewart 		SCTP_SB_CLEAR(so->so_snd);
682f8829a4aSRandall Stewart 		/*
683f8829a4aSRandall Stewart 		 * same for the rcv ones, they are only here for the
684f8829a4aSRandall Stewart 		 * accounting/select.
685f8829a4aSRandall Stewart 		 */
686132dea7dSRandall Stewart 		SCTP_SB_CLEAR(so->so_rcv);
687132dea7dSRandall Stewart 		/* Now null out the reference, we are completely detached. */
688f8829a4aSRandall Stewart 		so->so_pcb = NULL;
689f8829a4aSRandall Stewart 		SOCK_UNLOCK(so);
690f8829a4aSRandall Stewart 	} else {
691f8829a4aSRandall Stewart 		flags = inp->sctp_flags;
692f8829a4aSRandall Stewart 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
693f8829a4aSRandall Stewart 			goto sctp_must_try_again;
694f8829a4aSRandall Stewart 		}
695f8829a4aSRandall Stewart 	}
696f8829a4aSRandall Stewart 	return;
697f8829a4aSRandall Stewart 
698f8829a4aSRandall Stewart }
699f8829a4aSRandall Stewart 
70042551e99SRandall Stewart /* This could be made common with sctp_detach() since they are identical */
701f8829a4aSRandall Stewart 
702c105859eSRandall Stewart static
703c105859eSRandall Stewart int
704f8829a4aSRandall Stewart sctp6_disconnect(struct socket *so)
705f8829a4aSRandall Stewart {
706f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
707f8829a4aSRandall Stewart 
708f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
709f8829a4aSRandall Stewart 	if (inp == NULL) {
710f8829a4aSRandall Stewart 		return (ENOTCONN);
711f8829a4aSRandall Stewart 	}
712f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(inp);
713f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
714f42a358aSRandall Stewart 		if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
715f8829a4aSRandall Stewart 			/* No connection */
716f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
717f8829a4aSRandall Stewart 			return (ENOTCONN);
718f8829a4aSRandall Stewart 		} else {
719f8829a4aSRandall Stewart 			int some_on_streamwheel = 0;
720f8829a4aSRandall Stewart 			struct sctp_association *asoc;
721f8829a4aSRandall Stewart 			struct sctp_tcb *stcb;
722f8829a4aSRandall Stewart 
723f8829a4aSRandall Stewart 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
724f8829a4aSRandall Stewart 			if (stcb == NULL) {
725f8829a4aSRandall Stewart 				SCTP_INP_RUNLOCK(inp);
726f8829a4aSRandall Stewart 				return (EINVAL);
727f8829a4aSRandall Stewart 			}
728f8829a4aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
729f8829a4aSRandall Stewart 			asoc = &stcb->asoc;
730f8829a4aSRandall Stewart 			if (((so->so_options & SO_LINGER) &&
731f8829a4aSRandall Stewart 			    (so->so_linger == 0)) ||
732f8829a4aSRandall Stewart 			    (so->so_rcv.sb_cc > 0)) {
733f8829a4aSRandall Stewart 				if (SCTP_GET_STATE(asoc) !=
734f8829a4aSRandall Stewart 				    SCTP_STATE_COOKIE_WAIT) {
735f8829a4aSRandall Stewart 					/* Left with Data unread */
736c105859eSRandall Stewart 					struct mbuf *op_err;
737f8829a4aSRandall Stewart 
738c105859eSRandall Stewart 					op_err = sctp_generate_invmanparam(SCTP_CAUSE_USER_INITIATED_ABT);
739c105859eSRandall Stewart 					sctp_send_abort_tcb(stcb, op_err);
740f8829a4aSRandall Stewart 					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
741f8829a4aSRandall Stewart 				}
742f8829a4aSRandall Stewart 				SCTP_INP_RUNLOCK(inp);
743f8829a4aSRandall Stewart 				if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
744f8829a4aSRandall Stewart 				    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
745f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
746f8829a4aSRandall Stewart 				}
747a5d547adSRandall Stewart 				sctp_free_assoc(inp, stcb, SCTP_DONOT_SETSCOPE,
748a5d547adSRandall Stewart 				    SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_2);
749f8829a4aSRandall Stewart 				/* No unlock tcb assoc is gone */
750f8829a4aSRandall Stewart 				return (0);
751f8829a4aSRandall Stewart 			}
752f8829a4aSRandall Stewart 			if (!TAILQ_EMPTY(&asoc->out_wheel)) {
753f8829a4aSRandall Stewart 				/* Check to see if some data queued */
754f8829a4aSRandall Stewart 				struct sctp_stream_out *outs;
755f8829a4aSRandall Stewart 
756f8829a4aSRandall Stewart 				TAILQ_FOREACH(outs, &asoc->out_wheel,
757f8829a4aSRandall Stewart 				    next_spoke) {
758f8829a4aSRandall Stewart 					if (!TAILQ_EMPTY(&outs->outqueue)) {
759f8829a4aSRandall Stewart 						some_on_streamwheel = 1;
760f8829a4aSRandall Stewart 						break;
761f8829a4aSRandall Stewart 					}
762f8829a4aSRandall Stewart 				}
763f8829a4aSRandall Stewart 			}
764f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue) &&
765f8829a4aSRandall Stewart 			    TAILQ_EMPTY(&asoc->sent_queue) &&
766f8829a4aSRandall Stewart 			    (some_on_streamwheel == 0)) {
767f8829a4aSRandall Stewart 				/* nothing queued to send, so I'm done... */
768f8829a4aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) !=
769f8829a4aSRandall Stewart 				    SCTP_STATE_SHUTDOWN_SENT) &&
770f8829a4aSRandall Stewart 				    (SCTP_GET_STATE(asoc) !=
771f8829a4aSRandall Stewart 				    SCTP_STATE_SHUTDOWN_ACK_SENT)) {
772f8829a4aSRandall Stewart 					/* only send SHUTDOWN the first time */
773f8829a4aSRandall Stewart 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
774f8829a4aSRandall Stewart 					sctp_chunk_output(stcb->sctp_ep, stcb, 1);
775f42a358aSRandall Stewart 					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
776f42a358aSRandall Stewart 					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
777f8829a4aSRandall Stewart 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
778f42a358aSRandall Stewart 					}
779f42a358aSRandall Stewart 					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
780f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
781f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb,
782f8829a4aSRandall Stewart 					    asoc->primary_destination);
783f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
784f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb,
785f8829a4aSRandall Stewart 					    asoc->primary_destination);
786f8829a4aSRandall Stewart 				}
787f8829a4aSRandall Stewart 			} else {
788f8829a4aSRandall Stewart 				/*
789f8829a4aSRandall Stewart 				 * we still got (or just got) data to send,
790f8829a4aSRandall Stewart 				 * so set SHUTDOWN_PENDING
791f8829a4aSRandall Stewart 				 */
792f8829a4aSRandall Stewart 				/*
793f8829a4aSRandall Stewart 				 * XXX sockets draft says that MSG_EOF
794f8829a4aSRandall Stewart 				 * should be sent with no data.  currently,
795f8829a4aSRandall Stewart 				 * we will allow user data to be sent first
796f8829a4aSRandall Stewart 				 * and move to SHUTDOWN-PENDING
797f8829a4aSRandall Stewart 				 */
798f8829a4aSRandall Stewart 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
799f8829a4aSRandall Stewart 			}
800f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
801f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
802f8829a4aSRandall Stewart 			return (0);
803f8829a4aSRandall Stewart 		}
804f8829a4aSRandall Stewart 	} else {
805f8829a4aSRandall Stewart 		/* UDP model does not support this */
806f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
807f8829a4aSRandall Stewart 		return EOPNOTSUPP;
808f8829a4aSRandall Stewart 	}
809f8829a4aSRandall Stewart }
810f8829a4aSRandall Stewart 
811c105859eSRandall Stewart 
812f8829a4aSRandall Stewart int
813f8829a4aSRandall Stewart sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
814f8829a4aSRandall Stewart     struct mbuf *control, struct thread *p);
815f8829a4aSRandall Stewart 
816f8829a4aSRandall Stewart 
817f8829a4aSRandall Stewart static int
818f8829a4aSRandall Stewart sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
819f8829a4aSRandall Stewart     struct mbuf *control, struct thread *p)
820f8829a4aSRandall Stewart {
821f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
822f8829a4aSRandall Stewart 	struct inpcb *in_inp;
823f8829a4aSRandall Stewart 	struct in6pcb *inp6;
824f8829a4aSRandall Stewart 
825f8829a4aSRandall Stewart #ifdef INET
826f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6;
827f8829a4aSRandall Stewart 
828f8829a4aSRandall Stewart #endif				/* INET */
829f8829a4aSRandall Stewart 	/* No SPL needed since sctp_output does this */
830f8829a4aSRandall Stewart 
831f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
832f8829a4aSRandall Stewart 	if (inp == NULL) {
833f8829a4aSRandall Stewart 		if (control) {
83417205eccSRandall Stewart 			SCTP_RELEASE_PKT(control);
835f8829a4aSRandall Stewart 			control = NULL;
836f8829a4aSRandall Stewart 		}
83717205eccSRandall Stewart 		SCTP_RELEASE_PKT(m);
838f8829a4aSRandall Stewart 		return EINVAL;
839f8829a4aSRandall Stewart 	}
840f8829a4aSRandall Stewart 	in_inp = (struct inpcb *)inp;
841f8829a4aSRandall Stewart 	inp6 = (struct in6pcb *)inp;
842f8829a4aSRandall Stewart 	/*
843f8829a4aSRandall Stewart 	 * For the TCP model we may get a NULL addr, if we are a connected
844f8829a4aSRandall Stewart 	 * socket thats ok.
845f8829a4aSRandall Stewart 	 */
846f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
847f8829a4aSRandall Stewart 	    (addr == NULL)) {
848f8829a4aSRandall Stewart 		goto connected_type;
849f8829a4aSRandall Stewart 	}
850f8829a4aSRandall Stewart 	if (addr == NULL) {
85117205eccSRandall Stewart 		SCTP_RELEASE_PKT(m);
852f8829a4aSRandall Stewart 		if (control) {
85317205eccSRandall Stewart 			SCTP_RELEASE_PKT(control);
854f8829a4aSRandall Stewart 			control = NULL;
855f8829a4aSRandall Stewart 		}
856f8829a4aSRandall Stewart 		return (EDESTADDRREQ);
857f8829a4aSRandall Stewart 	}
858f8829a4aSRandall Stewart #ifdef INET
859f8829a4aSRandall Stewart 	sin6 = (struct sockaddr_in6 *)addr;
86044b7479bSRandall Stewart 	if (SCTP_IPV6_V6ONLY(inp6)) {
861f8829a4aSRandall Stewart 		/*
862f8829a4aSRandall Stewart 		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
863f8829a4aSRandall Stewart 		 * v4 addr or v4-mapped addr
864f8829a4aSRandall Stewart 		 */
865f8829a4aSRandall Stewart 		if (addr->sa_family == AF_INET) {
866f8829a4aSRandall Stewart 			return EINVAL;
867f8829a4aSRandall Stewart 		}
868f8829a4aSRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
869f8829a4aSRandall Stewart 			return EINVAL;
870f8829a4aSRandall Stewart 		}
871f8829a4aSRandall Stewart 	}
872f8829a4aSRandall Stewart 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
873f8829a4aSRandall Stewart 		if (!ip6_v6only) {
874f8829a4aSRandall Stewart 			struct sockaddr_in sin;
875f8829a4aSRandall Stewart 
876f8829a4aSRandall Stewart 			/* convert v4-mapped into v4 addr and send */
877f8829a4aSRandall Stewart 			in6_sin6_2_sin(&sin, sin6);
878f8829a4aSRandall Stewart 			return sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
879f8829a4aSRandall Stewart 			    control, p);
880f8829a4aSRandall Stewart 		} else {
881f8829a4aSRandall Stewart 			/* mapped addresses aren't enabled */
882f8829a4aSRandall Stewart 			return EINVAL;
883f8829a4aSRandall Stewart 		}
884f8829a4aSRandall Stewart 	}
885f8829a4aSRandall Stewart #endif				/* INET */
886f8829a4aSRandall Stewart connected_type:
887f8829a4aSRandall Stewart 	/* now what about control */
888f8829a4aSRandall Stewart 	if (control) {
889f8829a4aSRandall Stewart 		if (inp->control) {
890ad81507eSRandall Stewart 			SCTP_PRINTF("huh? control set?\n");
89117205eccSRandall Stewart 			SCTP_RELEASE_PKT(inp->control);
892f8829a4aSRandall Stewart 			inp->control = NULL;
893f8829a4aSRandall Stewart 		}
894f8829a4aSRandall Stewart 		inp->control = control;
895f8829a4aSRandall Stewart 	}
896f8829a4aSRandall Stewart 	/* Place the data */
897f8829a4aSRandall Stewart 	if (inp->pkt) {
898139bc87fSRandall Stewart 		SCTP_BUF_NEXT(inp->pkt_last) = m;
899f8829a4aSRandall Stewart 		inp->pkt_last = m;
900f8829a4aSRandall Stewart 	} else {
901f8829a4aSRandall Stewart 		inp->pkt_last = inp->pkt = m;
902f8829a4aSRandall Stewart 	}
903f8829a4aSRandall Stewart 	if (
904f8829a4aSRandall Stewart 	/* FreeBSD and MacOSX uses a flag passed */
905f8829a4aSRandall Stewart 	    ((flags & PRUS_MORETOCOME) == 0)
906f8829a4aSRandall Stewart 	    ) {
907f8829a4aSRandall Stewart 		/*
908f8829a4aSRandall Stewart 		 * note with the current version this code will only be used
909f8829a4aSRandall Stewart 		 * by OpenBSD, NetBSD and FreeBSD have methods for
910f8829a4aSRandall Stewart 		 * re-defining sosend() to use sctp_sosend().  One can
911f8829a4aSRandall Stewart 		 * optionaly switch back to this code (by changing back the
912f8829a4aSRandall Stewart 		 * defininitions but this is not advisable.
913f8829a4aSRandall Stewart 		 */
914f8829a4aSRandall Stewart 		int ret;
915f8829a4aSRandall Stewart 
916f8829a4aSRandall Stewart 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
917f8829a4aSRandall Stewart 		inp->pkt = NULL;
918f8829a4aSRandall Stewart 		inp->control = NULL;
919f8829a4aSRandall Stewart 		return (ret);
920f8829a4aSRandall Stewart 	} else {
921f8829a4aSRandall Stewart 		return (0);
922f8829a4aSRandall Stewart 	}
923f8829a4aSRandall Stewart }
924f8829a4aSRandall Stewart 
925f8829a4aSRandall Stewart static int
926f8829a4aSRandall Stewart sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
927f8829a4aSRandall Stewart {
92842551e99SRandall Stewart 	uint32_t vrf_id;
929f8829a4aSRandall Stewart 	int error = 0;
930f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
931f8829a4aSRandall Stewart 	struct in6pcb *inp6;
932f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
933f8829a4aSRandall Stewart 
934f8829a4aSRandall Stewart #ifdef INET
935f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6;
936f8829a4aSRandall Stewart 	struct sockaddr_storage ss;
937f8829a4aSRandall Stewart 
938f8829a4aSRandall Stewart #endif				/* INET */
939f8829a4aSRandall Stewart 
940f8829a4aSRandall Stewart 	inp6 = (struct in6pcb *)so->so_pcb;
941f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
942f8829a4aSRandall Stewart 	if (inp == 0) {
943f8829a4aSRandall Stewart 		return (ECONNRESET);	/* I made the same as TCP since we are
944f8829a4aSRandall Stewart 					 * not setup? */
945f8829a4aSRandall Stewart 	}
946d61a0ae0SRandall Stewart 	if (addr == NULL) {
947d61a0ae0SRandall Stewart 		return (EINVAL);
948d61a0ae0SRandall Stewart 	}
949d61a0ae0SRandall Stewart 	if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
950d61a0ae0SRandall Stewart 		return (EINVAL);
951d61a0ae0SRandall Stewart 	}
952d61a0ae0SRandall Stewart 	if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
953d61a0ae0SRandall Stewart 		return (EINVAL);
954d61a0ae0SRandall Stewart 	}
955bff64a4dSRandall Stewart 	vrf_id = inp->def_vrf_id;
956f8829a4aSRandall Stewart 	SCTP_ASOC_CREATE_LOCK(inp);
957f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(inp);
958f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
959f8829a4aSRandall Stewart 	    SCTP_PCB_FLAGS_UNBOUND) {
960f8829a4aSRandall Stewart 		/* Bind a ephemeral port */
961f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
962f8829a4aSRandall Stewart 		error = sctp6_bind(so, NULL, p);
963f8829a4aSRandall Stewart 		if (error) {
964f8829a4aSRandall Stewart 			SCTP_ASOC_CREATE_UNLOCK(inp);
965f8829a4aSRandall Stewart 
966f8829a4aSRandall Stewart 			return (error);
967f8829a4aSRandall Stewart 		}
968f8829a4aSRandall Stewart 		SCTP_INP_RLOCK(inp);
969f8829a4aSRandall Stewart 	}
970f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
971f8829a4aSRandall Stewart 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
972f8829a4aSRandall Stewart 		/* We are already connected AND the TCP model */
973f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
974f8829a4aSRandall Stewart 		SCTP_ASOC_CREATE_UNLOCK(inp);
975f8829a4aSRandall Stewart 		return (EADDRINUSE);
976f8829a4aSRandall Stewart 	}
977f8829a4aSRandall Stewart #ifdef INET
978f8829a4aSRandall Stewart 	sin6 = (struct sockaddr_in6 *)addr;
97944b7479bSRandall Stewart 	if (SCTP_IPV6_V6ONLY(inp6)) {
980f8829a4aSRandall Stewart 		/*
981f8829a4aSRandall Stewart 		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
982f8829a4aSRandall Stewart 		 * addr or v4-mapped addr
983f8829a4aSRandall Stewart 		 */
984f8829a4aSRandall Stewart 		if (addr->sa_family == AF_INET) {
985f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
986f8829a4aSRandall Stewart 			SCTP_ASOC_CREATE_UNLOCK(inp);
987f8829a4aSRandall Stewart 			return EINVAL;
988f8829a4aSRandall Stewart 		}
989f8829a4aSRandall Stewart 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
990f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
991f8829a4aSRandall Stewart 			SCTP_ASOC_CREATE_UNLOCK(inp);
992f8829a4aSRandall Stewart 			return EINVAL;
993f8829a4aSRandall Stewart 		}
994f8829a4aSRandall Stewart 	}
995f8829a4aSRandall Stewart 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
996f8829a4aSRandall Stewart 		if (!ip6_v6only) {
997f8829a4aSRandall Stewart 			/* convert v4-mapped into v4 addr */
998f8829a4aSRandall Stewart 			in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
999f8829a4aSRandall Stewart 			addr = (struct sockaddr *)&ss;
1000f8829a4aSRandall Stewart 		} else {
1001f8829a4aSRandall Stewart 			/* mapped addresses aren't enabled */
1002f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
1003f8829a4aSRandall Stewart 			SCTP_ASOC_CREATE_UNLOCK(inp);
1004f8829a4aSRandall Stewart 			return EINVAL;
1005f8829a4aSRandall Stewart 		}
1006f8829a4aSRandall Stewart 	} else
1007f8829a4aSRandall Stewart #endif				/* INET */
1008f8829a4aSRandall Stewart 		addr = addr;	/* for true v6 address case */
1009f8829a4aSRandall Stewart 
1010f8829a4aSRandall Stewart 	/* Now do we connect? */
1011f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1012f8829a4aSRandall Stewart 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1013ad81507eSRandall Stewart 		if (stcb) {
1014f8829a4aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1015ad81507eSRandall Stewart 		}
1016f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
1017f8829a4aSRandall Stewart 	} else {
1018f8829a4aSRandall Stewart 		SCTP_INP_RUNLOCK(inp);
1019f8829a4aSRandall Stewart 		SCTP_INP_WLOCK(inp);
1020f8829a4aSRandall Stewart 		SCTP_INP_INCR_REF(inp);
1021f8829a4aSRandall Stewart 		SCTP_INP_WUNLOCK(inp);
1022f8829a4aSRandall Stewart 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1023f8829a4aSRandall Stewart 		if (stcb == NULL) {
1024f8829a4aSRandall Stewart 			SCTP_INP_WLOCK(inp);
1025f8829a4aSRandall Stewart 			SCTP_INP_DECR_REF(inp);
1026f8829a4aSRandall Stewart 			SCTP_INP_WUNLOCK(inp);
1027f8829a4aSRandall Stewart 		}
1028f8829a4aSRandall Stewart 	}
1029f8829a4aSRandall Stewart 
1030f8829a4aSRandall Stewart 	if (stcb != NULL) {
1031f8829a4aSRandall Stewart 		/* Already have or am bring up an association */
1032f8829a4aSRandall Stewart 		SCTP_ASOC_CREATE_UNLOCK(inp);
1033f8829a4aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
1034f8829a4aSRandall Stewart 		return (EALREADY);
1035f8829a4aSRandall Stewart 	}
1036f8829a4aSRandall Stewart 	/* We are GOOD to go */
103752be287eSRandall Stewart 	stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
1038f8829a4aSRandall Stewart 	SCTP_ASOC_CREATE_UNLOCK(inp);
1039f8829a4aSRandall Stewart 	if (stcb == NULL) {
1040f8829a4aSRandall Stewart 		/* Gak! no memory */
1041f8829a4aSRandall Stewart 		return (error);
1042f8829a4aSRandall Stewart 	}
1043f8829a4aSRandall Stewart 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1044f8829a4aSRandall Stewart 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1045f8829a4aSRandall Stewart 		/* Set the connected flag so we can queue data */
1046f8829a4aSRandall Stewart 		soisconnecting(so);
1047f8829a4aSRandall Stewart 	}
1048f8829a4aSRandall Stewart 	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1049ad81507eSRandall Stewart 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1050f8829a4aSRandall Stewart 
1051f8829a4aSRandall Stewart 	/* initialize authentication parameters for the assoc */
1052f8829a4aSRandall Stewart 	sctp_initialize_auth_params(inp, stcb);
1053f8829a4aSRandall Stewart 
1054f8829a4aSRandall Stewart 	sctp_send_initiate(inp, stcb);
1055f8829a4aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
1056f8829a4aSRandall Stewart 	return error;
1057f8829a4aSRandall Stewart }
1058f8829a4aSRandall Stewart 
1059f8829a4aSRandall Stewart static int
1060f8829a4aSRandall Stewart sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1061f8829a4aSRandall Stewart {
1062f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6;
1063f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
106442551e99SRandall Stewart 	uint32_t vrf_id;
106542551e99SRandall Stewart 	struct sctp_ifa *sctp_ifa;
1066f8829a4aSRandall Stewart 
1067f8829a4aSRandall Stewart 	int error;
1068f8829a4aSRandall Stewart 
1069f8829a4aSRandall Stewart 	/*
1070f8829a4aSRandall Stewart 	 * Do the malloc first in case it blocks.
1071f8829a4aSRandall Stewart 	 */
1072f8829a4aSRandall Stewart 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1073f8829a4aSRandall Stewart 	sin6->sin6_family = AF_INET6;
1074f8829a4aSRandall Stewart 	sin6->sin6_len = sizeof(*sin6);
1075f8829a4aSRandall Stewart 
1076f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
1077f8829a4aSRandall Stewart 	if (inp == NULL) {
1078f8829a4aSRandall Stewart 		SCTP_FREE_SONAME(sin6);
1079f8829a4aSRandall Stewart 		return ECONNRESET;
1080f8829a4aSRandall Stewart 	}
1081f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(inp);
1082f8829a4aSRandall Stewart 	sin6->sin6_port = inp->sctp_lport;
1083f8829a4aSRandall Stewart 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1084f8829a4aSRandall Stewart 		/* For the bound all case you get back 0 */
1085f8829a4aSRandall Stewart 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1086f8829a4aSRandall Stewart 			struct sctp_tcb *stcb;
1087f8829a4aSRandall Stewart 			struct sockaddr_in6 *sin_a6;
1088f8829a4aSRandall Stewart 			struct sctp_nets *net;
1089f8829a4aSRandall Stewart 			int fnd;
1090f8829a4aSRandall Stewart 
1091f8829a4aSRandall Stewart 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1092f8829a4aSRandall Stewart 			if (stcb == NULL) {
1093f8829a4aSRandall Stewart 				goto notConn6;
1094f8829a4aSRandall Stewart 			}
1095f8829a4aSRandall Stewart 			fnd = 0;
1096f8829a4aSRandall Stewart 			sin_a6 = NULL;
1097f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1098f8829a4aSRandall Stewart 				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
109903b0b021SRandall Stewart 				if (sin_a6 == NULL)
110003b0b021SRandall Stewart 					/* this will make coverity happy */
110103b0b021SRandall Stewart 					continue;
110203b0b021SRandall Stewart 
1103f8829a4aSRandall Stewart 				if (sin_a6->sin6_family == AF_INET6) {
1104f8829a4aSRandall Stewart 					fnd = 1;
1105f8829a4aSRandall Stewart 					break;
1106f8829a4aSRandall Stewart 				}
1107f8829a4aSRandall Stewart 			}
1108f8829a4aSRandall Stewart 			if ((!fnd) || (sin_a6 == NULL)) {
1109f8829a4aSRandall Stewart 				/* punt */
1110f8829a4aSRandall Stewart 				goto notConn6;
1111f8829a4aSRandall Stewart 			}
1112bff64a4dSRandall Stewart 			vrf_id = inp->def_vrf_id;
1113bff64a4dSRandall Stewart 			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
111442551e99SRandall Stewart 			if (sctp_ifa) {
111542551e99SRandall Stewart 				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
111642551e99SRandall Stewart 			}
1117f8829a4aSRandall Stewart 		} else {
1118f8829a4aSRandall Stewart 			/* For the bound all case you get back 0 */
1119f8829a4aSRandall Stewart 	notConn6:
1120f8829a4aSRandall Stewart 			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1121f8829a4aSRandall Stewart 		}
1122f8829a4aSRandall Stewart 	} else {
1123f8829a4aSRandall Stewart 		/* Take the first IPv6 address in the list */
1124f8829a4aSRandall Stewart 		struct sctp_laddr *laddr;
1125f8829a4aSRandall Stewart 		int fnd = 0;
1126f8829a4aSRandall Stewart 
1127f8829a4aSRandall Stewart 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
112842551e99SRandall Stewart 			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1129f8829a4aSRandall Stewart 				struct sockaddr_in6 *sin_a;
1130f8829a4aSRandall Stewart 
113142551e99SRandall Stewart 				sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1132f8829a4aSRandall Stewart 				sin6->sin6_addr = sin_a->sin6_addr;
1133f8829a4aSRandall Stewart 				fnd = 1;
1134f8829a4aSRandall Stewart 				break;
1135f8829a4aSRandall Stewart 			}
1136f8829a4aSRandall Stewart 		}
1137f8829a4aSRandall Stewart 		if (!fnd) {
1138f8829a4aSRandall Stewart 			SCTP_FREE_SONAME(sin6);
1139f8829a4aSRandall Stewart 			SCTP_INP_RUNLOCK(inp);
1140f8829a4aSRandall Stewart 			return ENOENT;
1141f8829a4aSRandall Stewart 		}
1142f8829a4aSRandall Stewart 	}
1143f8829a4aSRandall Stewart 	SCTP_INP_RUNLOCK(inp);
1144f8829a4aSRandall Stewart 	/* Scoping things for v6 */
114503b0b021SRandall Stewart 	if ((error = sa6_recoverscope(sin6)) != 0) {
114603b0b021SRandall Stewart 		SCTP_FREE_SONAME(sin6);
1147f8829a4aSRandall Stewart 		return (error);
114803b0b021SRandall Stewart 	}
1149f8829a4aSRandall Stewart 	(*addr) = (struct sockaddr *)sin6;
1150f8829a4aSRandall Stewart 	return (0);
1151f8829a4aSRandall Stewart }
1152f8829a4aSRandall Stewart 
1153f8829a4aSRandall Stewart static int
1154f8829a4aSRandall Stewart sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1155f8829a4aSRandall Stewart {
1156f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1157f8829a4aSRandall Stewart 	int fnd;
1158f8829a4aSRandall Stewart 	struct sockaddr_in6 *sin_a6;
1159f8829a4aSRandall Stewart 	struct sctp_inpcb *inp;
1160f8829a4aSRandall Stewart 	struct sctp_tcb *stcb;
1161f8829a4aSRandall Stewart 	struct sctp_nets *net;
1162f8829a4aSRandall Stewart 
1163f8829a4aSRandall Stewart 	int error;
1164f8829a4aSRandall Stewart 
1165f8829a4aSRandall Stewart 	/*
1166f8829a4aSRandall Stewart 	 * Do the malloc first in case it blocks.
1167f8829a4aSRandall Stewart 	 */
1168f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
1169f8829a4aSRandall Stewart 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1170f8829a4aSRandall Stewart 		/* UDP type and listeners will drop out here */
1171f8829a4aSRandall Stewart 		return (ENOTCONN);
1172f8829a4aSRandall Stewart 	}
1173f8829a4aSRandall Stewart 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1174f8829a4aSRandall Stewart 	sin6->sin6_family = AF_INET6;
1175f8829a4aSRandall Stewart 	sin6->sin6_len = sizeof(*sin6);
1176f8829a4aSRandall Stewart 
1177f8829a4aSRandall Stewart 	/* We must recapture incase we blocked */
1178f8829a4aSRandall Stewart 	inp = (struct sctp_inpcb *)so->so_pcb;
1179f8829a4aSRandall Stewart 	if (inp == NULL) {
1180f8829a4aSRandall Stewart 		SCTP_FREE_SONAME(sin6);
1181f8829a4aSRandall Stewart 		return ECONNRESET;
1182f8829a4aSRandall Stewart 	}
1183f8829a4aSRandall Stewart 	SCTP_INP_RLOCK(inp);
1184f8829a4aSRandall Stewart 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1185ad81507eSRandall Stewart 	if (stcb) {
1186f8829a4aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
1187ad81507eSRandall Stewart 	}
1188f8829a4aSRandall Stewart 	SCTP_INP_RUNLOCK(inp);
1189f8829a4aSRandall Stewart 	if (stcb == NULL) {
1190f8829a4aSRandall Stewart 		SCTP_FREE_SONAME(sin6);
1191f8829a4aSRandall Stewart 		return ECONNRESET;
1192f8829a4aSRandall Stewart 	}
1193f8829a4aSRandall Stewart 	fnd = 0;
1194f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1195f8829a4aSRandall Stewart 		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1196f8829a4aSRandall Stewart 		if (sin_a6->sin6_family == AF_INET6) {
1197f8829a4aSRandall Stewart 			fnd = 1;
1198f8829a4aSRandall Stewart 			sin6->sin6_port = stcb->rport;
1199f8829a4aSRandall Stewart 			sin6->sin6_addr = sin_a6->sin6_addr;
1200f8829a4aSRandall Stewart 			break;
1201f8829a4aSRandall Stewart 		}
1202f8829a4aSRandall Stewart 	}
1203f8829a4aSRandall Stewart 	SCTP_TCB_UNLOCK(stcb);
1204f8829a4aSRandall Stewart 	if (!fnd) {
1205f8829a4aSRandall Stewart 		/* No IPv4 address */
1206f8829a4aSRandall Stewart 		SCTP_FREE_SONAME(sin6);
1207f8829a4aSRandall Stewart 		return ENOENT;
1208f8829a4aSRandall Stewart 	}
1209f8829a4aSRandall Stewart 	if ((error = sa6_recoverscope(sin6)) != 0)
1210f8829a4aSRandall Stewart 		return (error);
1211f8829a4aSRandall Stewart 	*addr = (struct sockaddr *)sin6;
1212f8829a4aSRandall Stewart 	return (0);
1213f8829a4aSRandall Stewart }
1214f8829a4aSRandall Stewart 
1215f8829a4aSRandall Stewart static int
1216f8829a4aSRandall Stewart sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1217f8829a4aSRandall Stewart {
1218f8829a4aSRandall Stewart 	struct sockaddr *addr;
1219f8829a4aSRandall Stewart 	struct in6pcb *inp6 = sotoin6pcb(so);
122093164cf9SRandall Stewart 	int error;
1221f8829a4aSRandall Stewart 
1222f8829a4aSRandall Stewart 	if (inp6 == NULL)
1223f8829a4aSRandall Stewart 		return EINVAL;
1224f8829a4aSRandall Stewart 
1225f8829a4aSRandall Stewart 	/* allow v6 addresses precedence */
1226f8829a4aSRandall Stewart 	error = sctp6_getaddr(so, nam);
1227f8829a4aSRandall Stewart 	if (error) {
1228f8829a4aSRandall Stewart 		/* try v4 next if v6 failed */
1229f8829a4aSRandall Stewart 		error = sctp_ingetaddr(so, nam);
1230f8829a4aSRandall Stewart 		if (error) {
1231f8829a4aSRandall Stewart 			return (error);
1232f8829a4aSRandall Stewart 		}
1233f8829a4aSRandall Stewart 		addr = *nam;
1234f8829a4aSRandall Stewart 		/* if I'm V6ONLY, convert it to v4-mapped */
123544b7479bSRandall Stewart 		if (SCTP_IPV6_V6ONLY(inp6)) {
1236f8829a4aSRandall Stewart 			struct sockaddr_in6 sin6;
1237f8829a4aSRandall Stewart 
1238f8829a4aSRandall Stewart 			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1239f8829a4aSRandall Stewart 			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1240c105859eSRandall Stewart 
1241f8829a4aSRandall Stewart 		}
1242f8829a4aSRandall Stewart 	}
1243f8829a4aSRandall Stewart 	return (error);
1244f8829a4aSRandall Stewart }
1245f8829a4aSRandall Stewart 
1246f8829a4aSRandall Stewart 
1247f8829a4aSRandall Stewart static int
1248f8829a4aSRandall Stewart sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1249f8829a4aSRandall Stewart {
1250f8829a4aSRandall Stewart 	struct sockaddr *addr = *nam;
1251f8829a4aSRandall Stewart 	struct in6pcb *inp6 = sotoin6pcb(so);
125293164cf9SRandall Stewart 	int error;
1253f8829a4aSRandall Stewart 
1254f8829a4aSRandall Stewart 	if (inp6 == NULL)
1255f8829a4aSRandall Stewart 		return EINVAL;
1256f8829a4aSRandall Stewart 
1257f8829a4aSRandall Stewart 	/* allow v6 addresses precedence */
1258f8829a4aSRandall Stewart 	error = sctp6_peeraddr(so, nam);
1259f8829a4aSRandall Stewart 	if (error) {
1260f8829a4aSRandall Stewart 		/* try v4 next if v6 failed */
1261f8829a4aSRandall Stewart 		error = sctp_peeraddr(so, nam);
1262f8829a4aSRandall Stewart 		if (error) {
1263f8829a4aSRandall Stewart 			return (error);
1264f8829a4aSRandall Stewart 		}
1265f8829a4aSRandall Stewart 		/* if I'm V6ONLY, convert it to v4-mapped */
126644b7479bSRandall Stewart 		if (SCTP_IPV6_V6ONLY(inp6)) {
1267f8829a4aSRandall Stewart 			struct sockaddr_in6 sin6;
1268f8829a4aSRandall Stewart 
1269f8829a4aSRandall Stewart 			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1270f8829a4aSRandall Stewart 			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1271f8829a4aSRandall Stewart 		}
1272f8829a4aSRandall Stewart 	}
1273f8829a4aSRandall Stewart 	return error;
1274f8829a4aSRandall Stewart }
1275f8829a4aSRandall Stewart 
1276f8829a4aSRandall Stewart struct pr_usrreqs sctp6_usrreqs = {
1277f8829a4aSRandall Stewart 	.pru_abort = sctp6_abort,
1278f8829a4aSRandall Stewart 	.pru_accept = sctp_accept,
1279f8829a4aSRandall Stewart 	.pru_attach = sctp6_attach,
1280f8829a4aSRandall Stewart 	.pru_bind = sctp6_bind,
1281f8829a4aSRandall Stewart 	.pru_connect = sctp6_connect,
1282f8829a4aSRandall Stewart 	.pru_control = in6_control,
1283f8829a4aSRandall Stewart 	.pru_close = sctp6_close,
1284f8829a4aSRandall Stewart 	.pru_detach = sctp6_close,
1285f8829a4aSRandall Stewart 	.pru_sopoll = sopoll_generic,
1286f8829a4aSRandall Stewart 	.pru_disconnect = sctp6_disconnect,
1287f8829a4aSRandall Stewart 	.pru_listen = sctp_listen,
1288f8829a4aSRandall Stewart 	.pru_peeraddr = sctp6_getpeeraddr,
1289f8829a4aSRandall Stewart 	.pru_send = sctp6_send,
1290f8829a4aSRandall Stewart 	.pru_shutdown = sctp_shutdown,
1291f8829a4aSRandall Stewart 	.pru_sockaddr = sctp6_in6getaddr,
1292f8829a4aSRandall Stewart 	.pru_sosend = sctp_sosend,
1293f8829a4aSRandall Stewart 	.pru_soreceive = sctp_soreceive
1294f8829a4aSRandall Stewart };
1295