sctp6_usrreq.c revision 172156
1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3163953Srrs *
4163953Srrs * Redistribution and use in source and binary forms, with or without
5163953Srrs * modification, are permitted provided that the following conditions are met:
6163953Srrs *
7163953Srrs * a) Redistributions of source code must retain the above copyright notice,
8163953Srrs *   this list of conditions and the following disclaimer.
9163953Srrs *
10163953Srrs * b) Redistributions in binary form must reproduce the above copyright
11163953Srrs *    notice, this list of conditions and the following disclaimer in
12163953Srrs *   the documentation and/or other materials provided with the distribution.
13163953Srrs *
14163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
15163953Srrs *    contributors may be used to endorse or promote products derived
16163953Srrs *    from this software without specific prior written permission.
17163953Srrs *
18163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
29163953Srrs */
30163953Srrs/*	$KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $	*/
31163954Srrs#include <sys/cdefs.h>
32163953Srrs__FBSDID("$FreeBSD: head/sys/netinet6/sctp6_usrreq.c 172156 2007-09-13 10:36:43Z rrs $");
33163953Srrs
34168709Srrs
35166086Srrs#include <netinet/sctp_os.h>
36163953Srrs#include <sys/proc.h>
37163953Srrs#include <netinet/sctp_pcb.h>
38163953Srrs#include <netinet/sctp_header.h>
39163953Srrs#include <netinet/sctp_var.h>
40168709Srrs#if defined(INET6)
41168709Srrs#include <netinet6/sctp6_var.h>
42168709Srrs#endif
43167598Srrs#include <netinet/sctp_sysctl.h>
44163953Srrs#include <netinet/sctp_output.h>
45166086Srrs#include <netinet/sctp_uio.h>
46163953Srrs#include <netinet/sctp_asconf.h>
47166086Srrs#include <netinet/sctputil.h>
48166086Srrs#include <netinet/sctp_indata.h>
49166086Srrs#include <netinet/sctp_timer.h>
50166086Srrs#include <netinet/sctp_auth.h>
51168709Srrs#include <netinet/sctp_input.h>
52168709Srrs#include <netinet/sctp_output.h>
53170091Srrs#include <netinet/sctp_bsd_addr.h>
54163953Srrs
55171167Sgnn#ifdef IPSEC
56171133Sgnn#include <netipsec/ipsec.h>
57171133Sgnn#if defined(INET6)
58171133Sgnn#include <netipsec/ipsec6.h>
59171440Srrs#endif				/* INET6 */
60171440Srrs#endif				/* IPSEC */
61163953Srrs
62163953Srrsextern struct protosw inetsw[];
63163953Srrs
64163953Srrsint
65171259Sdelphijsctp6_input(struct mbuf **i_pak, int *offp, int proto)
66163953Srrs{
67165647Srrs	struct mbuf *m;
68163953Srrs	struct ip6_hdr *ip6;
69163953Srrs	struct sctphdr *sh;
70163953Srrs	struct sctp_inpcb *in6p = NULL;
71163953Srrs	struct sctp_nets *net;
72163953Srrs	int refcount_up = 0;
73169352Srrs	uint32_t check, calc_check;
74170181Srrs	uint32_t vrf_id = 0;
75163953Srrs	struct inpcb *in6p_ip;
76163953Srrs	struct sctp_chunkhdr *ch;
77163953Srrs	int length, mlen, offset, iphlen;
78168299Srrs	uint8_t ecn_bits;
79163953Srrs	struct sctp_tcb *stcb = NULL;
80170056Srrs	int pkt_len = 0;
81163953Srrs	int off = *offp;
82163953Srrs
83169352Srrs	/* get the VRF and table id's */
84169352Srrs	if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
85169352Srrs		SCTP_RELEASE_PKT(*i_pak);
86169352Srrs		return (-1);
87169352Srrs	}
88168709Srrs	m = SCTP_HEADER_TO_CHAIN(*i_pak);
89170056Srrs	pkt_len = SCTP_HEADER_LEN((*i_pak));
90165647Srrs
91170091Srrs#ifdef  SCTP_PACKET_LOGGING
92170091Srrs	sctp_packet_log(m, pkt_len);
93170091Srrs#endif
94163953Srrs	ip6 = mtod(m, struct ip6_hdr *);
95163953Srrs	/* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
96170056Srrs	IP6_EXTHDR_GET(sh, struct sctphdr *, m, off,
97170056Srrs	    (int)(sizeof(*sh) + sizeof(*ch)));
98163953Srrs	if (sh == NULL) {
99163953Srrs		SCTP_STAT_INCR(sctps_hdrops);
100163953Srrs		return IPPROTO_DONE;
101163953Srrs	}
102163953Srrs	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
103163953Srrs	iphlen = off;
104163953Srrs	offset = iphlen + sizeof(*sh) + sizeof(*ch);
105170859Srrs	SCTPDBG(SCTP_DEBUG_INPUT1,
106170859Srrs	    "sctp6_input() length:%d iphlen:%d\n", pkt_len, iphlen);
107163953Srrs
108170859Srrs
109163953Srrs#if defined(NFAITH) && NFAITH > 0
110163953Srrs
111163953Srrs	if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) {
112163953Srrs		/* XXX send icmp6 host/port unreach? */
113163953Srrs		goto bad;
114163953Srrs	}
115163953Srrs#endif				/* NFAITH defined and > 0 */
116163953Srrs	SCTP_STAT_INCR(sctps_recvpackets);
117163953Srrs	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
118169420Srrs	SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n",
119170056Srrs	    iphlen, pkt_len);
120163953Srrs	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
121163953Srrs		/* No multi-cast support in SCTP */
122163953Srrs		goto bad;
123163953Srrs	}
124163953Srrs	/* destination port of 0 is illegal, based on RFC2960. */
125163953Srrs	if (sh->dest_port == 0)
126163953Srrs		goto bad;
127171990Srrs	check = sh->checksum;	/* save incoming checksum */
128171990Srrs	if ((check == 0) && (sctp_no_csum_on_loopback) &&
129171990Srrs	    (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))) {
130171990Srrs		goto sctp_skip_csum;
131171990Srrs	}
132171990Srrs	sh->checksum = 0;	/* prepare for calc */
133171990Srrs	calc_check = sctp_calculate_sum(m, &mlen, iphlen);
134171990Srrs	if (calc_check != check) {
135171990Srrs		SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
136171990Srrs		    calc_check, check, m, mlen, iphlen);
137171990Srrs		stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
138171990Srrs		    sh, ch, &in6p, &net, vrf_id);
139171990Srrs		/* in6p's ref-count increased && stcb locked */
140171990Srrs		if ((in6p) && (stcb)) {
141171990Srrs			sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
142172090Srrs			sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
143171990Srrs		} else if ((in6p != NULL) && (stcb == NULL)) {
144171990Srrs			refcount_up = 1;
145163953Srrs		}
146171990Srrs		SCTP_STAT_INCR(sctps_badsum);
147171990Srrs		SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
148171990Srrs		goto bad;
149165647Srrs	}
150171990Srrs	sh->checksum = calc_check;
151171990Srrs
152163953Srrssctp_skip_csum:
153163953Srrs	net = NULL;
154163953Srrs	/*
155163953Srrs	 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
156163953Srrs	 * IP/SCTP/first chunk header...
157163953Srrs	 */
158163953Srrs	stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
159168299Srrs	    sh, ch, &in6p, &net, vrf_id);
160163953Srrs	/* in6p's ref-count increased */
161163953Srrs	if (in6p == NULL) {
162163953Srrs		struct sctp_init_chunk *init_chk, chunk_buf;
163163953Srrs
164163953Srrs		SCTP_STAT_INCR(sctps_noport);
165163953Srrs		if (ch->chunk_type == SCTP_INITIATION) {
166163953Srrs			/*
167163953Srrs			 * we do a trick here to get the INIT tag, dig in
168163953Srrs			 * and get the tag from the INIT and put it in the
169163953Srrs			 * common header.
170163953Srrs			 */
171163953Srrs			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
172163953Srrs			    iphlen + sizeof(*sh), sizeof(*init_chk),
173168299Srrs			    (uint8_t *) & chunk_buf);
174169420Srrs			if (init_chk)
175169420Srrs				sh->v_tag = init_chk->init.initiate_tag;
176169420Srrs			else
177169420Srrs				sh->v_tag = 0;
178163953Srrs		}
179165220Srrs		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
180170181Srrs			sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id);
181165220Srrs			goto bad;
182165220Srrs		}
183165220Srrs		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
184165220Srrs			goto bad;
185165220Srrs		}
186165220Srrs		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
187170181Srrs			sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id);
188163953Srrs		goto bad;
189163953Srrs	} else if (stcb == NULL) {
190163953Srrs		refcount_up = 1;
191163953Srrs	}
192163953Srrs	in6p_ip = (struct inpcb *)in6p;
193171167Sgnn#ifdef IPSEC
194163953Srrs	/*
195163953Srrs	 * Check AH/ESP integrity.
196163953Srrs	 */
197163996Srrs	if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) {
198163953Srrs/* XXX */
199163953Srrs		ipsec6stat.in_polvio++;
200163953Srrs		goto bad;
201163996Srrs	}
202171440Srrs#endif				/* IPSEC */
203163953Srrs
204163953Srrs	/*
205163953Srrs	 * CONTROL chunk processing
206163953Srrs	 */
207163953Srrs	offset -= sizeof(*ch);
208163953Srrs	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
209165647Srrs
210165647Srrs	/* Length now holds the total packet length payload + iphlen */
211165647Srrs	length = ntohs(ip6->ip6_plen) + iphlen;
212165647Srrs
213169655Srrs	/* sa_ignore NO_NULL_CHK */
214169378Srrs	sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
215170181Srrs	    in6p, stcb, net, ecn_bits, vrf_id);
216163953Srrs	/* inp's ref-count reduced && stcb unlocked */
217163953Srrs	/* XXX this stuff below gets moved to appropriate parts later... */
218163953Srrs	if (m)
219169352Srrs		sctp_m_freem(m);
220163953Srrs	if ((in6p) && refcount_up) {
221163953Srrs		/* reduce ref-count */
222163953Srrs		SCTP_INP_WLOCK(in6p);
223163953Srrs		SCTP_INP_DECR_REF(in6p);
224163953Srrs		SCTP_INP_WUNLOCK(in6p);
225163953Srrs	}
226163953Srrs	return IPPROTO_DONE;
227163953Srrs
228163953Srrsbad:
229169420Srrs	if (stcb) {
230163953Srrs		SCTP_TCB_UNLOCK(stcb);
231169420Srrs	}
232163953Srrs	if ((in6p) && refcount_up) {
233163953Srrs		/* reduce ref-count */
234163953Srrs		SCTP_INP_WLOCK(in6p);
235163953Srrs		SCTP_INP_DECR_REF(in6p);
236163953Srrs		SCTP_INP_WUNLOCK(in6p);
237163953Srrs	}
238163953Srrs	if (m)
239169352Srrs		sctp_m_freem(m);
240163953Srrs	return IPPROTO_DONE;
241163953Srrs}
242163953Srrs
243163953Srrs
244163953Srrsstatic void
245171259Sdelphijsctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
246171259Sdelphij    struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
247163953Srrs{
248168299Srrs	uint32_t nxtsz;
249163953Srrs
250163953Srrs	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
251163953Srrs	    (icmp6 == NULL) || (sh == NULL)) {
252163953Srrs		goto out;
253163953Srrs	}
254163953Srrs	/* First do we even look at it? */
255163953Srrs	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
256163953Srrs		goto out;
257163953Srrs
258163953Srrs	if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
259163953Srrs		/* not PACKET TO BIG */
260163953Srrs		goto out;
261163953Srrs	}
262163953Srrs	/*
263163953Srrs	 * ok we need to look closely. We could even get smarter and look at
264163953Srrs	 * anyone that we sent to in case we get a different ICMP that tells
265163953Srrs	 * us there is no way to reach a host, but for this impl, all we
266163953Srrs	 * care about is MTU discovery.
267163953Srrs	 */
268163953Srrs	nxtsz = ntohl(icmp6->icmp6_mtu);
269163953Srrs	/* Stop any PMTU timer */
270165220Srrs	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
271163953Srrs
272163953Srrs	/* Adjust destination size limit */
273163953Srrs	if (net->mtu > nxtsz) {
274163953Srrs		net->mtu = nxtsz;
275163953Srrs	}
276163953Srrs	/* now what about the ep? */
277163953Srrs	if (stcb->asoc.smallest_mtu > nxtsz) {
278163953Srrs		struct sctp_tmit_chunk *chk;
279163953Srrs
280163953Srrs		/* Adjust that too */
281163953Srrs		stcb->asoc.smallest_mtu = nxtsz;
282163953Srrs		/* now off to subtract IP_DF flag if needed */
283163953Srrs
284163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
285168299Srrs			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
286163953Srrs				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
287163953Srrs			}
288163953Srrs		}
289163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
290168299Srrs			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
291163953Srrs				/*
292163953Srrs				 * For this guy we also mark for immediate
293163953Srrs				 * resend since we sent to big of chunk
294163953Srrs				 */
295163953Srrs				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
296163953Srrs				if (chk->sent != SCTP_DATAGRAM_RESEND)
297163953Srrs					stcb->asoc.sent_queue_retran_cnt++;
298163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
299163953Srrs				chk->rec.data.doing_fast_retransmit = 0;
300163953Srrs
301163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
302163953Srrs				/* Clear any time so NO RTT is being done */
303163953Srrs				chk->sent_rcv_time.tv_sec = 0;
304163953Srrs				chk->sent_rcv_time.tv_usec = 0;
305163953Srrs				stcb->asoc.total_flight -= chk->send_size;
306163953Srrs				net->flight_size -= chk->send_size;
307163953Srrs			}
308163953Srrs		}
309163953Srrs	}
310163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
311163953Srrsout:
312169420Srrs	if (stcb) {
313163953Srrs		SCTP_TCB_UNLOCK(stcb);
314169420Srrs	}
315163953Srrs}
316163953Srrs
317163953Srrs
318172156Srrsvoid
319172091Srrssctp6_notify(struct sctp_inpcb *inp,
320172091Srrs    struct icmp6_hdr *icmph,
321172091Srrs    struct sctphdr *sh,
322172091Srrs    struct sockaddr *to,
323172091Srrs    struct sctp_tcb *stcb,
324172091Srrs    struct sctp_nets *net)
325172091Srrs{
326172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
327172091Srrs	struct socket *so;
328172091Srrs
329172091Srrs#endif
330172091Srrs	/* protection */
331172091Srrs	int reason;
332172091Srrs
333172091Srrs
334172091Srrs	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
335172091Srrs	    (sh == NULL) || (to == NULL)) {
336172091Srrs		if (stcb)
337172091Srrs			SCTP_TCB_UNLOCK(stcb);
338172091Srrs		return;
339172091Srrs	}
340172091Srrs	/* First job is to verify the vtag matches what I would send */
341172091Srrs	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
342172091Srrs		SCTP_TCB_UNLOCK(stcb);
343172091Srrs		return;
344172091Srrs	}
345172091Srrs	if (icmph->icmp6_type != ICMP_UNREACH) {
346172091Srrs		/* We only care about unreachable */
347172091Srrs		SCTP_TCB_UNLOCK(stcb);
348172091Srrs		return;
349172091Srrs	}
350172091Srrs	if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
351172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
352172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
353172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
354172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
355172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
356172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
357172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
358172091Srrs
359172091Srrs		/*
360172091Srrs		 * Hmm reachablity problems we must examine closely. If its
361172091Srrs		 * not reachable, we may have lost a network. Or if there is
362172091Srrs		 * NO protocol at the other end named SCTP. well we consider
363172091Srrs		 * it a OOTB abort.
364172091Srrs		 */
365172091Srrs		if (net->dest_state & SCTP_ADDR_REACHABLE) {
366172091Srrs			/* Ok that destination is NOT reachable */
367172091Srrs			SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
368172091Srrs			    net->error_count,
369172091Srrs			    net->failure_threshold,
370172091Srrs			    net);
371172091Srrs
372172091Srrs			net->dest_state &= ~SCTP_ADDR_REACHABLE;
373172091Srrs			net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
374172091Srrs			/*
375172091Srrs			 * JRS 5/14/07 - If a destination is unreachable,
376172091Srrs			 * the PF bit is turned off.  This allows an
377172091Srrs			 * unambiguous use of the PF bit for destinations
378172091Srrs			 * that are reachable but potentially failed. If the
379172091Srrs			 * destination is set to the unreachable state, also
380172091Srrs			 * set the destination to the PF state.
381172091Srrs			 */
382172091Srrs			/*
383172091Srrs			 * Add debug message here if destination is not in
384172091Srrs			 * PF state.
385172091Srrs			 */
386172091Srrs			/* Stop any running T3 timers here? */
387172091Srrs			if (sctp_cmt_on_off && sctp_cmt_pf) {
388172091Srrs				net->dest_state &= ~SCTP_ADDR_PF;
389172091Srrs				SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
390172091Srrs				    net);
391172091Srrs			}
392172091Srrs			net->error_count = net->failure_threshold + 1;
393172091Srrs			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
394172091Srrs			    stcb, SCTP_FAILED_THRESHOLD,
395172091Srrs			    (void *)net, SCTP_SO_NOT_LOCKED);
396172091Srrs		}
397172091Srrs		SCTP_TCB_UNLOCK(stcb);
398172091Srrs	} else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
399172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
400172091Srrs		/*
401172091Srrs		 * Here the peer is either playing tricks on us, including
402172091Srrs		 * an address that belongs to someone who does not support
403172091Srrs		 * SCTP OR was a userland implementation that shutdown and
404172091Srrs		 * now is dead. In either case treat it like a OOTB abort
405172091Srrs		 * with no TCB
406172091Srrs		 */
407172091Srrs		reason = SCTP_PEER_FAULTY;
408172091Srrs		sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
409172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
410172091Srrs		so = SCTP_INP_SO(inp);
411172091Srrs		atomic_add_int(&stcb->asoc.refcnt, 1);
412172091Srrs		SCTP_TCB_UNLOCK(stcb);
413172091Srrs		SCTP_SOCKET_LOCK(so, 1);
414172091Srrs		SCTP_TCB_LOCK(stcb);
415172091Srrs		atomic_subtract_int(&stcb->asoc.refcnt, 1);
416172091Srrs#endif
417172091Srrs		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
418172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
419172091Srrs		SCTP_SOCKET_UNLOCK(so, 1);
420172091Srrs		/* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
421172091Srrs#endif
422172091Srrs		/* no need to unlock here, since the TCB is gone */
423172091Srrs	} else {
424172091Srrs		SCTP_TCB_UNLOCK(stcb);
425172091Srrs	}
426172091Srrs}
427172091Srrs
428172091Srrs
429172091Srrs
430163953Srrsvoid
431171259Sdelphijsctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
432163953Srrs{
433163953Srrs	struct sctphdr sh;
434163953Srrs	struct ip6ctlparam *ip6cp = NULL;
435167598Srrs	uint32_t vrf_id;
436163953Srrs
437167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
438167598Srrs
439163953Srrs	if (pktdst->sa_family != AF_INET6 ||
440163953Srrs	    pktdst->sa_len != sizeof(struct sockaddr_in6))
441163953Srrs		return;
442163953Srrs
443163953Srrs	if ((unsigned)cmd >= PRC_NCMDS)
444163953Srrs		return;
445163953Srrs	if (PRC_IS_REDIRECT(cmd)) {
446163953Srrs		d = NULL;
447163953Srrs	} else if (inet6ctlerrmap[cmd] == 0) {
448163953Srrs		return;
449163953Srrs	}
450163953Srrs	/* if the parameter is from icmp6, decode it. */
451163953Srrs	if (d != NULL) {
452163953Srrs		ip6cp = (struct ip6ctlparam *)d;
453163953Srrs	} else {
454163953Srrs		ip6cp = (struct ip6ctlparam *)NULL;
455163953Srrs	}
456163953Srrs
457163953Srrs	if (ip6cp) {
458163953Srrs		/*
459163953Srrs		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
460163953Srrs		 * valid.
461163953Srrs		 */
462163953Srrs		/* check if we can safely examine src and dst ports */
463163953Srrs		struct sctp_inpcb *inp = NULL;
464163953Srrs		struct sctp_tcb *stcb = NULL;
465163953Srrs		struct sctp_nets *net = NULL;
466163953Srrs		struct sockaddr_in6 final;
467163953Srrs
468165647Srrs		if (ip6cp->ip6c_m == NULL)
469163953Srrs			return;
470163953Srrs
471163953Srrs		bzero(&sh, sizeof(sh));
472163953Srrs		bzero(&final, sizeof(final));
473163953Srrs		inp = NULL;
474163953Srrs		net = NULL;
475163953Srrs		m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
476163953Srrs		    (caddr_t)&sh);
477163953Srrs		ip6cp->ip6c_src->sin6_port = sh.src_port;
478163953Srrs		final.sin6_len = sizeof(final);
479163953Srrs		final.sin6_family = AF_INET6;
480163953Srrs		final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
481163953Srrs		final.sin6_port = sh.dest_port;
482163953Srrs		stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
483163953Srrs		    (struct sockaddr *)&final,
484167598Srrs		    &inp, &net, 1, vrf_id);
485163953Srrs		/* inp's ref-count increased && stcb locked */
486163953Srrs		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
487163953Srrs			if (cmd == PRC_MSGSIZE) {
488163953Srrs				sctp6_notify_mbuf(inp,
489163953Srrs				    ip6cp->ip6c_icmp6,
490163953Srrs				    &sh,
491163953Srrs				    stcb,
492163953Srrs				    net);
493163953Srrs				/* inp's ref-count reduced && stcb unlocked */
494163953Srrs			} else {
495172091Srrs				sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
496163953Srrs				    (struct sockaddr *)&final,
497163953Srrs				    stcb, net);
498163953Srrs				/* inp's ref-count reduced && stcb unlocked */
499163953Srrs			}
500163953Srrs		} else {
501163953Srrs			if (PRC_IS_REDIRECT(cmd) && inp) {
502163953Srrs				in6_rtchange((struct in6pcb *)inp,
503163953Srrs				    inet6ctlerrmap[cmd]);
504163953Srrs			}
505163953Srrs			if (inp) {
506163953Srrs				/* reduce inp's ref-count */
507163953Srrs				SCTP_INP_WLOCK(inp);
508163953Srrs				SCTP_INP_DECR_REF(inp);
509163953Srrs				SCTP_INP_WUNLOCK(inp);
510163953Srrs			}
511163953Srrs			if (stcb)
512163953Srrs				SCTP_TCB_UNLOCK(stcb);
513163953Srrs		}
514163953Srrs	}
515163953Srrs}
516163953Srrs
517163953Srrs/*
518163953Srrs * this routine can probably be collasped into the one in sctp_userreq.c
519163953Srrs * since they do the same thing and now we lookup with a sockaddr
520163953Srrs */
521163953Srrsstatic int
522163953Srrssctp6_getcred(SYSCTL_HANDLER_ARGS)
523163953Srrs{
524164085Srrs	struct xucred xuc;
525163953Srrs	struct sockaddr_in6 addrs[2];
526163953Srrs	struct sctp_inpcb *inp;
527163953Srrs	struct sctp_nets *net;
528163953Srrs	struct sctp_tcb *stcb;
529164085Srrs	int error;
530167598Srrs	uint32_t vrf_id;
531163953Srrs
532167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
533167598Srrs
534170587Srwatson	error = priv_check(req->td, PRIV_NETINET_GETCRED);
535163953Srrs	if (error)
536163953Srrs		return (error);
537163953Srrs
538171943Srrs	if (req->newlen != sizeof(addrs)) {
539171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
540163953Srrs		return (EINVAL);
541171943Srrs	}
542171943Srrs	if (req->oldlen != sizeof(struct ucred)) {
543171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
544163953Srrs		return (EINVAL);
545171943Srrs	}
546163953Srrs	error = SYSCTL_IN(req, addrs, sizeof(addrs));
547163953Srrs	if (error)
548163953Srrs		return (error);
549163953Srrs
550163953Srrs	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
551163953Srrs	    sin6tosa(&addrs[1]),
552167598Srrs	    &inp, &net, 1, vrf_id);
553163953Srrs	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
554164085Srrs		if ((inp != NULL) && (stcb == NULL)) {
555164085Srrs			/* reduce ref-count */
556163953Srrs			SCTP_INP_WLOCK(inp);
557163953Srrs			SCTP_INP_DECR_REF(inp);
558164085Srrs			goto cred_can_cont;
559163953Srrs		}
560171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
561164085Srrs		error = ENOENT;
562163953Srrs		goto out;
563163953Srrs	}
564163953Srrs	SCTP_TCB_UNLOCK(stcb);
565164085Srrs	/*
566164085Srrs	 * We use the write lock here, only since in the error leg we need
567164085Srrs	 * it. If we used RLOCK, then we would have to
568164085Srrs	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
569164085Srrs	 * Better to use higher wlock.
570164085Srrs	 */
571164085Srrs	SCTP_INP_WLOCK(inp);
572164085Srrscred_can_cont:
573164085Srrs	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
574164085Srrs	if (error) {
575164085Srrs		SCTP_INP_WUNLOCK(inp);
576164085Srrs		goto out;
577164085Srrs	}
578164085Srrs	cru2x(inp->sctp_socket->so_cred, &xuc);
579164085Srrs	SCTP_INP_WUNLOCK(inp);
580164085Srrs	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
581163953Srrsout:
582163953Srrs	return (error);
583163953Srrs}
584163953Srrs
585163953SrrsSYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
586163953Srrs    0, 0,
587163953Srrs    sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
588163953Srrs
589163953Srrs
590163953Srrs/* This is the same as the sctp_abort() could be made common */
591163953Srrsstatic void
592163953Srrssctp6_abort(struct socket *so)
593163953Srrs{
594163953Srrs	struct sctp_inpcb *inp;
595163953Srrs	uint32_t flags;
596163953Srrs
597163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
598172091Srrs	if (inp == 0) {
599172091Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
600163953Srrs		return;
601172091Srrs	}
602163953Srrssctp_must_try_again:
603163953Srrs	flags = inp->sctp_flags;
604163953Srrs#ifdef SCTP_LOG_CLOSING
605163953Srrs	sctp_log_closing(inp, NULL, 17);
606163953Srrs#endif
607163953Srrs	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
608163953Srrs	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
609163953Srrs#ifdef SCTP_LOG_CLOSING
610163953Srrs		sctp_log_closing(inp, NULL, 16);
611163953Srrs#endif
612169380Srrs		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
613169380Srrs		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
614163953Srrs		SOCK_LOCK(so);
615167695Srrs		SCTP_SB_CLEAR(so->so_snd);
616163953Srrs		/*
617163953Srrs		 * same for the rcv ones, they are only here for the
618163953Srrs		 * accounting/select.
619163953Srrs		 */
620167695Srrs		SCTP_SB_CLEAR(so->so_rcv);
621167695Srrs		/* Now null out the reference, we are completely detached. */
622163953Srrs		so->so_pcb = NULL;
623163953Srrs		SOCK_UNLOCK(so);
624163953Srrs	} else {
625163953Srrs		flags = inp->sctp_flags;
626163953Srrs		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
627163953Srrs			goto sctp_must_try_again;
628163953Srrs		}
629163953Srrs	}
630163953Srrs	return;
631163953Srrs}
632163953Srrs
633163953Srrsstatic int
634163953Srrssctp6_attach(struct socket *so, int proto, struct thread *p)
635163953Srrs{
636163953Srrs	struct in6pcb *inp6;
637166086Srrs	int error;
638163953Srrs	struct sctp_inpcb *inp;
639170205Srrs	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
640163953Srrs
641163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
642171943Srrs	if (inp != NULL) {
643171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
644163953Srrs		return EINVAL;
645171943Srrs	}
646163953Srrs	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
647167695Srrs		error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
648163953Srrs		if (error)
649163953Srrs			return error;
650163953Srrs	}
651170205Srrs	error = sctp_inpcb_alloc(so, vrf_id);
652163953Srrs	if (error)
653163953Srrs		return error;
654163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
655170205Srrs	SCTP_INP_WLOCK(inp);
656163953Srrs	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
657163953Srrs	inp6 = (struct in6pcb *)inp;
658163953Srrs
659163953Srrs	inp6->inp_vflag |= INP_IPV6;
660163953Srrs	inp6->in6p_hops = -1;	/* use kernel default */
661163953Srrs	inp6->in6p_cksum = -1;	/* just to be sure */
662163953Srrs#ifdef INET
663163953Srrs	/*
664163953Srrs	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
665163953Srrs	 * socket as well, because the socket may be bound to an IPv6
666163953Srrs	 * wildcard address, which may match an IPv4-mapped IPv6 address.
667163953Srrs	 */
668163953Srrs	inp6->inp_ip_ttl = ip_defttl;
669163953Srrs#endif
670163953Srrs	/*
671163953Srrs	 * Hmm what about the IPSEC stuff that is missing here but in
672163953Srrs	 * sctp_attach()?
673163953Srrs	 */
674170205Srrs	SCTP_INP_WUNLOCK(inp);
675163953Srrs	return 0;
676163953Srrs}
677163953Srrs
678163953Srrsstatic int
679163953Srrssctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
680163953Srrs{
681163953Srrs	struct sctp_inpcb *inp;
682163953Srrs	struct in6pcb *inp6;
683166086Srrs	int error;
684163953Srrs
685163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
686171943Srrs	if (inp == 0) {
687171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
688163953Srrs		return EINVAL;
689171943Srrs	}
690170056Srrs	if (addr) {
691170056Srrs		if ((addr->sa_family == AF_INET6) &&
692170056Srrs		    (addr->sa_len != sizeof(struct sockaddr_in6))) {
693171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
694170056Srrs			return EINVAL;
695170056Srrs		}
696170056Srrs		if ((addr->sa_family == AF_INET) &&
697170056Srrs		    (addr->sa_len != sizeof(struct sockaddr_in))) {
698171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
699170056Srrs			return EINVAL;
700170056Srrs		}
701170056Srrs	}
702163953Srrs	inp6 = (struct in6pcb *)inp;
703163953Srrs	inp6->inp_vflag &= ~INP_IPV4;
704163953Srrs	inp6->inp_vflag |= INP_IPV6;
705166023Srrs	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
706163953Srrs		if (addr->sa_family == AF_INET) {
707163953Srrs			/* binding v4 addr to v6 socket, so reset flags */
708163953Srrs			inp6->inp_vflag |= INP_IPV4;
709163953Srrs			inp6->inp_vflag &= ~INP_IPV6;
710163953Srrs		} else {
711163953Srrs			struct sockaddr_in6 *sin6_p;
712163953Srrs
713163953Srrs			sin6_p = (struct sockaddr_in6 *)addr;
714163953Srrs
715163953Srrs			if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
716163953Srrs				inp6->inp_vflag |= INP_IPV4;
717163953Srrs			} else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
718163953Srrs				struct sockaddr_in sin;
719163953Srrs
720163953Srrs				in6_sin6_2_sin(&sin, sin6_p);
721163953Srrs				inp6->inp_vflag |= INP_IPV4;
722163953Srrs				inp6->inp_vflag &= ~INP_IPV6;
723171572Srrs				error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
724163953Srrs				return error;
725163953Srrs			}
726163953Srrs		}
727163953Srrs	} else if (addr != NULL) {
728163953Srrs		/* IPV6_V6ONLY socket */
729163953Srrs		if (addr->sa_family == AF_INET) {
730163953Srrs			/* can't bind v4 addr to v6 only socket! */
731171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
732163953Srrs			return EINVAL;
733163953Srrs		} else {
734163953Srrs			struct sockaddr_in6 *sin6_p;
735163953Srrs
736163953Srrs			sin6_p = (struct sockaddr_in6 *)addr;
737163953Srrs
738172091Srrs			if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
739163953Srrs				/* can't bind v4-mapped addrs either! */
740163953Srrs				/* NOTE: we don't support SIIT */
741171943Srrs				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
742172091Srrs				return EINVAL;
743172091Srrs			}
744163953Srrs		}
745163953Srrs	}
746171572Srrs	error = sctp_inpcb_bind(so, addr, NULL, p);
747163953Srrs	return error;
748163953Srrs}
749163953Srrs
750163953Srrs
751163953Srrsstatic void
752163953Srrssctp6_close(struct socket *so)
753163953Srrs{
754171990Srrs	sctp_close(so);
755163953Srrs}
756163953Srrs
757167598Srrs/* This could be made common with sctp_detach() since they are identical */
758163953Srrs
759168709Srrsstatic
760168709Srrsint
761163953Srrssctp6_disconnect(struct socket *so)
762163953Srrs{
763171990Srrs	return (sctp_disconnect(so));
764163953Srrs}
765163953Srrs
766168709Srrs
767163953Srrsint
768163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
769163953Srrs    struct mbuf *control, struct thread *p);
770163953Srrs
771163953Srrs
772163953Srrsstatic int
773163953Srrssctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
774163953Srrs    struct mbuf *control, struct thread *p)
775163953Srrs{
776163953Srrs	struct sctp_inpcb *inp;
777163953Srrs	struct inpcb *in_inp;
778163953Srrs	struct in6pcb *inp6;
779163953Srrs
780163953Srrs#ifdef INET
781163953Srrs	struct sockaddr_in6 *sin6;
782163953Srrs
783163953Srrs#endif				/* INET */
784163953Srrs	/* No SPL needed since sctp_output does this */
785163953Srrs
786163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
787163953Srrs	if (inp == NULL) {
788163953Srrs		if (control) {
789169352Srrs			SCTP_RELEASE_PKT(control);
790163953Srrs			control = NULL;
791163953Srrs		}
792169352Srrs		SCTP_RELEASE_PKT(m);
793171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
794163953Srrs		return EINVAL;
795163953Srrs	}
796163953Srrs	in_inp = (struct inpcb *)inp;
797163953Srrs	inp6 = (struct in6pcb *)inp;
798163953Srrs	/*
799163953Srrs	 * For the TCP model we may get a NULL addr, if we are a connected
800163953Srrs	 * socket thats ok.
801163953Srrs	 */
802163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
803163953Srrs	    (addr == NULL)) {
804163953Srrs		goto connected_type;
805163953Srrs	}
806163953Srrs	if (addr == NULL) {
807169352Srrs		SCTP_RELEASE_PKT(m);
808163953Srrs		if (control) {
809169352Srrs			SCTP_RELEASE_PKT(control);
810163953Srrs			control = NULL;
811163953Srrs		}
812171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
813163953Srrs		return (EDESTADDRREQ);
814163953Srrs	}
815163953Srrs#ifdef INET
816163953Srrs	sin6 = (struct sockaddr_in6 *)addr;
817166023Srrs	if (SCTP_IPV6_V6ONLY(inp6)) {
818163953Srrs		/*
819163953Srrs		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
820163953Srrs		 * v4 addr or v4-mapped addr
821163953Srrs		 */
822163953Srrs		if (addr->sa_family == AF_INET) {
823171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
824163953Srrs			return EINVAL;
825163953Srrs		}
826163953Srrs		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
827171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
828163953Srrs			return EINVAL;
829163953Srrs		}
830163953Srrs	}
831163953Srrs	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
832163953Srrs		if (!ip6_v6only) {
833163953Srrs			struct sockaddr_in sin;
834163953Srrs
835163953Srrs			/* convert v4-mapped into v4 addr and send */
836163953Srrs			in6_sin6_2_sin(&sin, sin6);
837163953Srrs			return sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
838163953Srrs			    control, p);
839163953Srrs		} else {
840163953Srrs			/* mapped addresses aren't enabled */
841171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
842163953Srrs			return EINVAL;
843163953Srrs		}
844163953Srrs	}
845163953Srrs#endif				/* INET */
846163953Srrsconnected_type:
847163953Srrs	/* now what about control */
848163953Srrs	if (control) {
849163953Srrs		if (inp->control) {
850169420Srrs			SCTP_PRINTF("huh? control set?\n");
851169352Srrs			SCTP_RELEASE_PKT(inp->control);
852163953Srrs			inp->control = NULL;
853163953Srrs		}
854163953Srrs		inp->control = control;
855163953Srrs	}
856163953Srrs	/* Place the data */
857163953Srrs	if (inp->pkt) {
858165647Srrs		SCTP_BUF_NEXT(inp->pkt_last) = m;
859163953Srrs		inp->pkt_last = m;
860163953Srrs	} else {
861163953Srrs		inp->pkt_last = inp->pkt = m;
862163953Srrs	}
863163953Srrs	if (
864163953Srrs	/* FreeBSD and MacOSX uses a flag passed */
865163953Srrs	    ((flags & PRUS_MORETOCOME) == 0)
866163953Srrs	    ) {
867163953Srrs		/*
868163953Srrs		 * note with the current version this code will only be used
869163953Srrs		 * by OpenBSD, NetBSD and FreeBSD have methods for
870163953Srrs		 * re-defining sosend() to use sctp_sosend().  One can
871163953Srrs		 * optionaly switch back to this code (by changing back the
872163953Srrs		 * defininitions but this is not advisable.
873163953Srrs		 */
874163953Srrs		int ret;
875163953Srrs
876163953Srrs		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
877163953Srrs		inp->pkt = NULL;
878163953Srrs		inp->control = NULL;
879163953Srrs		return (ret);
880163953Srrs	} else {
881163953Srrs		return (0);
882163953Srrs	}
883163953Srrs}
884163953Srrs
885163953Srrsstatic int
886163953Srrssctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
887163953Srrs{
888167598Srrs	uint32_t vrf_id;
889163953Srrs	int error = 0;
890163953Srrs	struct sctp_inpcb *inp;
891163953Srrs	struct in6pcb *inp6;
892163953Srrs	struct sctp_tcb *stcb;
893163953Srrs
894163953Srrs#ifdef INET
895163953Srrs	struct sockaddr_in6 *sin6;
896163953Srrs	struct sockaddr_storage ss;
897163953Srrs
898163953Srrs#endif				/* INET */
899163953Srrs
900163953Srrs	inp6 = (struct in6pcb *)so->so_pcb;
901163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
902163953Srrs	if (inp == 0) {
903171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
904163953Srrs		return (ECONNRESET);	/* I made the same as TCP since we are
905163953Srrs					 * not setup? */
906163953Srrs	}
907170056Srrs	if (addr == NULL) {
908171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
909170056Srrs		return (EINVAL);
910170056Srrs	}
911170056Srrs	if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
912171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
913170056Srrs		return (EINVAL);
914170056Srrs	}
915170056Srrs	if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
916171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
917170056Srrs		return (EINVAL);
918170056Srrs	}
919168299Srrs	vrf_id = inp->def_vrf_id;
920163953Srrs	SCTP_ASOC_CREATE_LOCK(inp);
921163953Srrs	SCTP_INP_RLOCK(inp);
922163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
923163953Srrs	    SCTP_PCB_FLAGS_UNBOUND) {
924163953Srrs		/* Bind a ephemeral port */
925163953Srrs		SCTP_INP_RUNLOCK(inp);
926163953Srrs		error = sctp6_bind(so, NULL, p);
927163953Srrs		if (error) {
928163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
929163953Srrs
930163953Srrs			return (error);
931163953Srrs		}
932163953Srrs		SCTP_INP_RLOCK(inp);
933163953Srrs	}
934163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
935163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
936163953Srrs		/* We are already connected AND the TCP model */
937163953Srrs		SCTP_INP_RUNLOCK(inp);
938163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
939171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
940163953Srrs		return (EADDRINUSE);
941163953Srrs	}
942163953Srrs#ifdef INET
943163953Srrs	sin6 = (struct sockaddr_in6 *)addr;
944166023Srrs	if (SCTP_IPV6_V6ONLY(inp6)) {
945163953Srrs		/*
946163953Srrs		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
947163953Srrs		 * addr or v4-mapped addr
948163953Srrs		 */
949163953Srrs		if (addr->sa_family == AF_INET) {
950163953Srrs			SCTP_INP_RUNLOCK(inp);
951163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
952171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
953163953Srrs			return EINVAL;
954163953Srrs		}
955163953Srrs		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
956163953Srrs			SCTP_INP_RUNLOCK(inp);
957163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
958171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
959163953Srrs			return EINVAL;
960163953Srrs		}
961163953Srrs	}
962163953Srrs	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
963163953Srrs		if (!ip6_v6only) {
964163953Srrs			/* convert v4-mapped into v4 addr */
965163953Srrs			in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
966163953Srrs			addr = (struct sockaddr *)&ss;
967163953Srrs		} else {
968163953Srrs			/* mapped addresses aren't enabled */
969163953Srrs			SCTP_INP_RUNLOCK(inp);
970163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
971171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
972163953Srrs			return EINVAL;
973163953Srrs		}
974163953Srrs	} else
975163953Srrs#endif				/* INET */
976163953Srrs		addr = addr;	/* for true v6 address case */
977163953Srrs
978163953Srrs	/* Now do we connect? */
979163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
980163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
981169420Srrs		if (stcb) {
982163953Srrs			SCTP_TCB_UNLOCK(stcb);
983169420Srrs		}
984163953Srrs		SCTP_INP_RUNLOCK(inp);
985163953Srrs	} else {
986163953Srrs		SCTP_INP_RUNLOCK(inp);
987163953Srrs		SCTP_INP_WLOCK(inp);
988163953Srrs		SCTP_INP_INCR_REF(inp);
989163953Srrs		SCTP_INP_WUNLOCK(inp);
990163953Srrs		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
991163953Srrs		if (stcb == NULL) {
992163953Srrs			SCTP_INP_WLOCK(inp);
993163953Srrs			SCTP_INP_DECR_REF(inp);
994163953Srrs			SCTP_INP_WUNLOCK(inp);
995163953Srrs		}
996163953Srrs	}
997163953Srrs
998163953Srrs	if (stcb != NULL) {
999163953Srrs		/* Already have or am bring up an association */
1000163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
1001163953Srrs		SCTP_TCB_UNLOCK(stcb);
1002171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
1003163953Srrs		return (EALREADY);
1004163953Srrs	}
1005163953Srrs	/* We are GOOD to go */
1006171531Srrs	stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
1007163953Srrs	SCTP_ASOC_CREATE_UNLOCK(inp);
1008163953Srrs	if (stcb == NULL) {
1009163953Srrs		/* Gak! no memory */
1010163953Srrs		return (error);
1011163953Srrs	}
1012163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1013163953Srrs		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1014163953Srrs		/* Set the connected flag so we can queue data */
1015163953Srrs		soisconnecting(so);
1016163953Srrs	}
1017163953Srrs	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1018169420Srrs	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1019163953Srrs
1020163953Srrs	/* initialize authentication parameters for the assoc */
1021163953Srrs	sctp_initialize_auth_params(inp, stcb);
1022163953Srrs
1023172090Srrs	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1024163953Srrs	SCTP_TCB_UNLOCK(stcb);
1025163953Srrs	return error;
1026163953Srrs}
1027163953Srrs
1028163953Srrsstatic int
1029163953Srrssctp6_getaddr(struct socket *so, struct sockaddr **addr)
1030163953Srrs{
1031163953Srrs	struct sockaddr_in6 *sin6;
1032163953Srrs	struct sctp_inpcb *inp;
1033167598Srrs	uint32_t vrf_id;
1034167598Srrs	struct sctp_ifa *sctp_ifa;
1035163953Srrs
1036163953Srrs	int error;
1037163953Srrs
1038163953Srrs	/*
1039163953Srrs	 * Do the malloc first in case it blocks.
1040163953Srrs	 */
1041163953Srrs	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1042163953Srrs	sin6->sin6_family = AF_INET6;
1043163953Srrs	sin6->sin6_len = sizeof(*sin6);
1044163953Srrs
1045163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1046163953Srrs	if (inp == NULL) {
1047163953Srrs		SCTP_FREE_SONAME(sin6);
1048171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1049163953Srrs		return ECONNRESET;
1050163953Srrs	}
1051163953Srrs	SCTP_INP_RLOCK(inp);
1052163953Srrs	sin6->sin6_port = inp->sctp_lport;
1053163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1054163953Srrs		/* For the bound all case you get back 0 */
1055163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1056163953Srrs			struct sctp_tcb *stcb;
1057163953Srrs			struct sockaddr_in6 *sin_a6;
1058163953Srrs			struct sctp_nets *net;
1059163953Srrs			int fnd;
1060163953Srrs
1061163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1062163953Srrs			if (stcb == NULL) {
1063163953Srrs				goto notConn6;
1064163953Srrs			}
1065163953Srrs			fnd = 0;
1066163953Srrs			sin_a6 = NULL;
1067163953Srrs			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1068163953Srrs				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1069164085Srrs				if (sin_a6 == NULL)
1070164085Srrs					/* this will make coverity happy */
1071164085Srrs					continue;
1072164085Srrs
1073163953Srrs				if (sin_a6->sin6_family == AF_INET6) {
1074163953Srrs					fnd = 1;
1075163953Srrs					break;
1076163953Srrs				}
1077163953Srrs			}
1078163953Srrs			if ((!fnd) || (sin_a6 == NULL)) {
1079163953Srrs				/* punt */
1080163953Srrs				goto notConn6;
1081163953Srrs			}
1082168299Srrs			vrf_id = inp->def_vrf_id;
1083168299Srrs			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1084167598Srrs			if (sctp_ifa) {
1085167598Srrs				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1086167598Srrs			}
1087163953Srrs		} else {
1088163953Srrs			/* For the bound all case you get back 0 */
1089163953Srrs	notConn6:
1090163953Srrs			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1091163953Srrs		}
1092163953Srrs	} else {
1093163953Srrs		/* Take the first IPv6 address in the list */
1094163953Srrs		struct sctp_laddr *laddr;
1095163953Srrs		int fnd = 0;
1096163953Srrs
1097163953Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1098167598Srrs			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1099163953Srrs				struct sockaddr_in6 *sin_a;
1100163953Srrs
1101167598Srrs				sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1102163953Srrs				sin6->sin6_addr = sin_a->sin6_addr;
1103163953Srrs				fnd = 1;
1104163953Srrs				break;
1105163953Srrs			}
1106163953Srrs		}
1107163953Srrs		if (!fnd) {
1108163953Srrs			SCTP_FREE_SONAME(sin6);
1109163953Srrs			SCTP_INP_RUNLOCK(inp);
1110171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1111163953Srrs			return ENOENT;
1112163953Srrs		}
1113163953Srrs	}
1114163953Srrs	SCTP_INP_RUNLOCK(inp);
1115163953Srrs	/* Scoping things for v6 */
1116164085Srrs	if ((error = sa6_recoverscope(sin6)) != 0) {
1117164085Srrs		SCTP_FREE_SONAME(sin6);
1118163953Srrs		return (error);
1119164085Srrs	}
1120163953Srrs	(*addr) = (struct sockaddr *)sin6;
1121163953Srrs	return (0);
1122163953Srrs}
1123163953Srrs
1124163953Srrsstatic int
1125163953Srrssctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1126163953Srrs{
1127163953Srrs	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1128163953Srrs	int fnd;
1129163953Srrs	struct sockaddr_in6 *sin_a6;
1130163953Srrs	struct sctp_inpcb *inp;
1131163953Srrs	struct sctp_tcb *stcb;
1132163953Srrs	struct sctp_nets *net;
1133163953Srrs
1134163953Srrs	int error;
1135163953Srrs
1136163953Srrs	/*
1137163953Srrs	 * Do the malloc first in case it blocks.
1138163953Srrs	 */
1139163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1140163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1141163953Srrs		/* UDP type and listeners will drop out here */
1142171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1143163953Srrs		return (ENOTCONN);
1144163953Srrs	}
1145163953Srrs	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1146163953Srrs	sin6->sin6_family = AF_INET6;
1147163953Srrs	sin6->sin6_len = sizeof(*sin6);
1148163953Srrs
1149163953Srrs	/* We must recapture incase we blocked */
1150163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1151163953Srrs	if (inp == NULL) {
1152163953Srrs		SCTP_FREE_SONAME(sin6);
1153171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1154163953Srrs		return ECONNRESET;
1155163953Srrs	}
1156163953Srrs	SCTP_INP_RLOCK(inp);
1157163953Srrs	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1158169420Srrs	if (stcb) {
1159163953Srrs		SCTP_TCB_LOCK(stcb);
1160169420Srrs	}
1161163953Srrs	SCTP_INP_RUNLOCK(inp);
1162163953Srrs	if (stcb == NULL) {
1163163953Srrs		SCTP_FREE_SONAME(sin6);
1164171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1165163953Srrs		return ECONNRESET;
1166163953Srrs	}
1167163953Srrs	fnd = 0;
1168163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1169163953Srrs		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1170163953Srrs		if (sin_a6->sin6_family == AF_INET6) {
1171163953Srrs			fnd = 1;
1172163953Srrs			sin6->sin6_port = stcb->rport;
1173163953Srrs			sin6->sin6_addr = sin_a6->sin6_addr;
1174163953Srrs			break;
1175163953Srrs		}
1176163953Srrs	}
1177163953Srrs	SCTP_TCB_UNLOCK(stcb);
1178163953Srrs	if (!fnd) {
1179163953Srrs		/* No IPv4 address */
1180163953Srrs		SCTP_FREE_SONAME(sin6);
1181171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1182163953Srrs		return ENOENT;
1183163953Srrs	}
1184163953Srrs	if ((error = sa6_recoverscope(sin6)) != 0)
1185163953Srrs		return (error);
1186163953Srrs	*addr = (struct sockaddr *)sin6;
1187163953Srrs	return (0);
1188163953Srrs}
1189163953Srrs
1190163953Srrsstatic int
1191163953Srrssctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1192163953Srrs{
1193163953Srrs	struct sockaddr *addr;
1194163953Srrs	struct in6pcb *inp6 = sotoin6pcb(so);
1195166086Srrs	int error;
1196163953Srrs
1197171943Srrs	if (inp6 == NULL) {
1198171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1199163953Srrs		return EINVAL;
1200171943Srrs	}
1201163953Srrs	/* allow v6 addresses precedence */
1202163953Srrs	error = sctp6_getaddr(so, nam);
1203163953Srrs	if (error) {
1204163953Srrs		/* try v4 next if v6 failed */
1205163953Srrs		error = sctp_ingetaddr(so, nam);
1206163953Srrs		if (error) {
1207163953Srrs			return (error);
1208163953Srrs		}
1209163953Srrs		addr = *nam;
1210163953Srrs		/* if I'm V6ONLY, convert it to v4-mapped */
1211166023Srrs		if (SCTP_IPV6_V6ONLY(inp6)) {
1212163953Srrs			struct sockaddr_in6 sin6;
1213163953Srrs
1214163953Srrs			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1215163953Srrs			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1216168709Srrs
1217163953Srrs		}
1218163953Srrs	}
1219163953Srrs	return (error);
1220163953Srrs}
1221163953Srrs
1222163953Srrs
1223163953Srrsstatic int
1224163953Srrssctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1225163953Srrs{
1226163953Srrs	struct sockaddr *addr = *nam;
1227163953Srrs	struct in6pcb *inp6 = sotoin6pcb(so);
1228166086Srrs	int error;
1229163953Srrs
1230171943Srrs	if (inp6 == NULL) {
1231171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1232163953Srrs		return EINVAL;
1233171943Srrs	}
1234163953Srrs	/* allow v6 addresses precedence */
1235163953Srrs	error = sctp6_peeraddr(so, nam);
1236163953Srrs	if (error) {
1237163953Srrs		/* try v4 next if v6 failed */
1238163953Srrs		error = sctp_peeraddr(so, nam);
1239163953Srrs		if (error) {
1240163953Srrs			return (error);
1241163953Srrs		}
1242163953Srrs		/* if I'm V6ONLY, convert it to v4-mapped */
1243166023Srrs		if (SCTP_IPV6_V6ONLY(inp6)) {
1244163953Srrs			struct sockaddr_in6 sin6;
1245163953Srrs
1246163953Srrs			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1247163953Srrs			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1248163953Srrs		}
1249163953Srrs	}
1250163953Srrs	return error;
1251163953Srrs}
1252163953Srrs
1253163953Srrsstruct pr_usrreqs sctp6_usrreqs = {
1254163953Srrs	.pru_abort = sctp6_abort,
1255163953Srrs	.pru_accept = sctp_accept,
1256163953Srrs	.pru_attach = sctp6_attach,
1257163953Srrs	.pru_bind = sctp6_bind,
1258163953Srrs	.pru_connect = sctp6_connect,
1259163953Srrs	.pru_control = in6_control,
1260163953Srrs	.pru_close = sctp6_close,
1261163953Srrs	.pru_detach = sctp6_close,
1262163953Srrs	.pru_sopoll = sopoll_generic,
1263163953Srrs	.pru_disconnect = sctp6_disconnect,
1264163953Srrs	.pru_listen = sctp_listen,
1265163953Srrs	.pru_peeraddr = sctp6_getpeeraddr,
1266163953Srrs	.pru_send = sctp6_send,
1267163953Srrs	.pru_shutdown = sctp_shutdown,
1268163953Srrs	.pru_sockaddr = sctp6_in6getaddr,
1269163953Srrs	.pru_sosend = sctp_sosend,
1270163953Srrs	.pru_soreceive = sctp_soreceive
1271163953Srrs};
1272