sctp6_usrreq.c revision 228907
1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3218319Srrs * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4218319Srrs * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
5163953Srrs *
6163953Srrs * Redistribution and use in source and binary forms, with or without
7163953Srrs * modification, are permitted provided that the following conditions are met:
8163953Srrs *
9163953Srrs * a) Redistributions of source code must retain the above copyright notice,
10228653Stuexen *    this list of conditions and the following disclaimer.
11163953Srrs *
12163953Srrs * b) Redistributions in binary form must reproduce the above copyright
13163953Srrs *    notice, this list of conditions and the following disclaimer in
14228653Stuexen *    the documentation and/or other materials provided with the distribution.
15163953Srrs *
16163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
17163953Srrs *    contributors may be used to endorse or promote products derived
18163953Srrs *    from this software without specific prior written permission.
19163953Srrs *
20163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
31163953Srrs */
32163953Srrs/*	$KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $	*/
33174510Sobrien
34163954Srrs#include <sys/cdefs.h>
35163953Srrs__FBSDID("$FreeBSD: head/sys/netinet6/sctp6_usrreq.c 228907 2011-12-27 10:16:24Z tuexen $");
36163953Srrs
37166086Srrs#include <netinet/sctp_os.h>
38163953Srrs#include <sys/proc.h>
39163953Srrs#include <netinet/sctp_pcb.h>
40163953Srrs#include <netinet/sctp_header.h>
41163953Srrs#include <netinet/sctp_var.h>
42168709Srrs#if defined(INET6)
43168709Srrs#include <netinet6/sctp6_var.h>
44168709Srrs#endif
45167598Srrs#include <netinet/sctp_sysctl.h>
46163953Srrs#include <netinet/sctp_output.h>
47166086Srrs#include <netinet/sctp_uio.h>
48163953Srrs#include <netinet/sctp_asconf.h>
49166086Srrs#include <netinet/sctputil.h>
50166086Srrs#include <netinet/sctp_indata.h>
51166086Srrs#include <netinet/sctp_timer.h>
52166086Srrs#include <netinet/sctp_auth.h>
53168709Srrs#include <netinet/sctp_input.h>
54168709Srrs#include <netinet/sctp_output.h>
55170091Srrs#include <netinet/sctp_bsd_addr.h>
56188067Srrs#include <netinet/sctp_crc32.h>
57179783Srrs#include <netinet/udp.h>
58163953Srrs
59171167Sgnn#ifdef IPSEC
60171133Sgnn#include <netipsec/ipsec.h>
61171133Sgnn#if defined(INET6)
62171133Sgnn#include <netipsec/ipsec6.h>
63171440Srrs#endif				/* INET6 */
64171440Srrs#endif				/* IPSEC */
65163953Srrs
66163953Srrsextern struct protosw inetsw[];
67163953Srrs
68163953Srrsint
69171259Sdelphijsctp6_input(struct mbuf **i_pak, int *offp, int proto)
70163953Srrs{
71165647Srrs	struct mbuf *m;
72163953Srrs	struct ip6_hdr *ip6;
73163953Srrs	struct sctphdr *sh;
74163953Srrs	struct sctp_inpcb *in6p = NULL;
75163953Srrs	struct sctp_nets *net;
76163953Srrs	int refcount_up = 0;
77170181Srrs	uint32_t vrf_id = 0;
78212699Stuexen
79212699Stuexen#ifdef IPSEC
80163953Srrs	struct inpcb *in6p_ip;
81212699Stuexen
82212699Stuexen#endif
83163953Srrs	struct sctp_chunkhdr *ch;
84188067Srrs	int length, offset, iphlen;
85168299Srrs	uint8_t ecn_bits;
86163953Srrs	struct sctp_tcb *stcb = NULL;
87170056Srrs	int pkt_len = 0;
88211969Stuexen
89211969Stuexen#if !defined(SCTP_WITH_NO_CSUM)
90211969Stuexen	uint32_t check, calc_check;
91211969Stuexen
92211969Stuexen#endif
93163953Srrs	int off = *offp;
94179783Srrs	uint16_t port = 0;
95163953Srrs
96169352Srrs	/* get the VRF and table id's */
97169352Srrs	if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
98169352Srrs		SCTP_RELEASE_PKT(*i_pak);
99169352Srrs		return (-1);
100169352Srrs	}
101168709Srrs	m = SCTP_HEADER_TO_CHAIN(*i_pak);
102170056Srrs	pkt_len = SCTP_HEADER_LEN((*i_pak));
103165647Srrs
104170091Srrs#ifdef  SCTP_PACKET_LOGGING
105170091Srrs	sctp_packet_log(m, pkt_len);
106170091Srrs#endif
107163953Srrs	ip6 = mtod(m, struct ip6_hdr *);
108163953Srrs	/* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
109170056Srrs	IP6_EXTHDR_GET(sh, struct sctphdr *, m, off,
110170056Srrs	    (int)(sizeof(*sh) + sizeof(*ch)));
111163953Srrs	if (sh == NULL) {
112163953Srrs		SCTP_STAT_INCR(sctps_hdrops);
113228907Stuexen		return (IPPROTO_DONE);
114163953Srrs	}
115163953Srrs	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
116163953Srrs	iphlen = off;
117163953Srrs	offset = iphlen + sizeof(*sh) + sizeof(*ch);
118170859Srrs	SCTPDBG(SCTP_DEBUG_INPUT1,
119170859Srrs	    "sctp6_input() length:%d iphlen:%d\n", pkt_len, iphlen);
120163953Srrs
121170859Srrs
122163953Srrs#if defined(NFAITH) && NFAITH > 0
123163953Srrs
124163953Srrs	if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) {
125163953Srrs		/* XXX send icmp6 host/port unreach? */
126163953Srrs		goto bad;
127163953Srrs	}
128163953Srrs#endif				/* NFAITH defined and > 0 */
129163953Srrs	SCTP_STAT_INCR(sctps_recvpackets);
130163953Srrs	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
131169420Srrs	SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n",
132170056Srrs	    iphlen, pkt_len);
133163953Srrs	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
134163953Srrs		/* No multi-cast support in SCTP */
135163953Srrs		goto bad;
136163953Srrs	}
137163953Srrs	/* destination port of 0 is illegal, based on RFC2960. */
138163953Srrs	if (sh->dest_port == 0)
139163953Srrs		goto bad;
140188067Srrs
141188067Srrs	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
142188067Srrs	    "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
143188067Srrs	    m->m_pkthdr.len,
144188067Srrs	    if_name(m->m_pkthdr.rcvif),
145188067Srrs	    m->m_pkthdr.csum_flags);
146211969Stuexen#if defined(SCTP_WITH_NO_CSUM)
147211969Stuexen	SCTP_STAT_INCR(sctps_recvnocrc);
148211969Stuexen#else
149188067Srrs	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
150188067Srrs		SCTP_STAT_INCR(sctps_recvhwcrc);
151188067Srrs		goto sctp_skip_csum;
152188067Srrs	}
153171990Srrs	check = sh->checksum;	/* save incoming checksum */
154179783Srrs	if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) &&
155171990Srrs	    (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))) {
156188067Srrs		SCTP_STAT_INCR(sctps_recvnocrc);
157171990Srrs		goto sctp_skip_csum;
158171990Srrs	}
159171990Srrs	sh->checksum = 0;	/* prepare for calc */
160188067Srrs	calc_check = sctp_calculate_cksum(m, iphlen);
161188067Srrs	SCTP_STAT_INCR(sctps_recvswcrc);
162171990Srrs	if (calc_check != check) {
163188067Srrs		SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p phlen:%d\n",
164188067Srrs		    calc_check, check, m, iphlen);
165228653Stuexen		stcb = sctp_findassociation_addr(m, offset - sizeof(*ch),
166171990Srrs		    sh, ch, &in6p, &net, vrf_id);
167179783Srrs		if ((net) && (port)) {
168179783Srrs			if (net->port == 0) {
169228653Stuexen				sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
170179783Srrs			}
171179783Srrs			net->port = port;
172179783Srrs		}
173218400Stuexen		if ((net != NULL) && (m->m_flags & M_FLOWID)) {
174218400Stuexen			net->flowid = m->m_pkthdr.flowid;
175218400Stuexen#ifdef INVARIANTS
176218400Stuexen			net->flowidset = 1;
177218400Stuexen#endif
178218400Stuexen		}
179171990Srrs		/* in6p's ref-count increased && stcb locked */
180171990Srrs		if ((in6p) && (stcb)) {
181171990Srrs			sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
182172090Srrs			sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
183171990Srrs		} else if ((in6p != NULL) && (stcb == NULL)) {
184171990Srrs			refcount_up = 1;
185163953Srrs		}
186171990Srrs		SCTP_STAT_INCR(sctps_badsum);
187171990Srrs		SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
188171990Srrs		goto bad;
189165647Srrs	}
190171990Srrs	sh->checksum = calc_check;
191171990Srrs
192163953Srrssctp_skip_csum:
193211969Stuexen#endif
194163953Srrs	net = NULL;
195163953Srrs	/*
196163953Srrs	 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
197163953Srrs	 * IP/SCTP/first chunk header...
198163953Srrs	 */
199228653Stuexen	stcb = sctp_findassociation_addr(m, offset - sizeof(*ch),
200168299Srrs	    sh, ch, &in6p, &net, vrf_id);
201179783Srrs	if ((net) && (port)) {
202179783Srrs		if (net->port == 0) {
203228653Stuexen			sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
204179783Srrs		}
205179783Srrs		net->port = port;
206179783Srrs	}
207218400Stuexen	if ((net != NULL) && (m->m_flags & M_FLOWID)) {
208218400Stuexen		net->flowid = m->m_pkthdr.flowid;
209218400Stuexen#ifdef INVARIANTS
210218400Stuexen		net->flowidset = 1;
211218400Stuexen#endif
212218400Stuexen	}
213163953Srrs	/* in6p's ref-count increased */
214163953Srrs	if (in6p == NULL) {
215163953Srrs		struct sctp_init_chunk *init_chk, chunk_buf;
216163953Srrs
217163953Srrs		SCTP_STAT_INCR(sctps_noport);
218163953Srrs		if (ch->chunk_type == SCTP_INITIATION) {
219163953Srrs			/*
220163953Srrs			 * we do a trick here to get the INIT tag, dig in
221163953Srrs			 * and get the tag from the INIT and put it in the
222163953Srrs			 * common header.
223163953Srrs			 */
224163953Srrs			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
225163953Srrs			    iphlen + sizeof(*sh), sizeof(*init_chk),
226168299Srrs			    (uint8_t *) & chunk_buf);
227169420Srrs			if (init_chk)
228169420Srrs				sh->v_tag = init_chk->init.initiate_tag;
229169420Srrs			else
230169420Srrs				sh->v_tag = 0;
231163953Srrs		}
232165220Srrs		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
233228653Stuexen			sctp_send_shutdown_complete2(m, sh, vrf_id, port);
234165220Srrs			goto bad;
235165220Srrs		}
236165220Srrs		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
237165220Srrs			goto bad;
238165220Srrs		}
239165220Srrs		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
240179783Srrs			sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
241163953Srrs		goto bad;
242163953Srrs	} else if (stcb == NULL) {
243163953Srrs		refcount_up = 1;
244163953Srrs	}
245171167Sgnn#ifdef IPSEC
246163953Srrs	/*
247163953Srrs	 * Check AH/ESP integrity.
248163953Srrs	 */
249212699Stuexen	in6p_ip = (struct inpcb *)in6p;
250163996Srrs	if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) {
251163953Srrs/* XXX */
252197288Srrs		MODULE_GLOBAL(ipsec6stat).in_polvio++;
253163953Srrs		goto bad;
254163996Srrs	}
255171440Srrs#endif				/* IPSEC */
256163953Srrs
257163953Srrs	/*
258163953Srrs	 * CONTROL chunk processing
259163953Srrs	 */
260163953Srrs	offset -= sizeof(*ch);
261163953Srrs	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
262165647Srrs
263165647Srrs	/* Length now holds the total packet length payload + iphlen */
264165647Srrs	length = ntohs(ip6->ip6_plen) + iphlen;
265165647Srrs
266169655Srrs	/* sa_ignore NO_NULL_CHK */
267169378Srrs	sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
268179783Srrs	    in6p, stcb, net, ecn_bits, vrf_id, port);
269163953Srrs	/* inp's ref-count reduced && stcb unlocked */
270163953Srrs	/* XXX this stuff below gets moved to appropriate parts later... */
271163953Srrs	if (m)
272169352Srrs		sctp_m_freem(m);
273163953Srrs	if ((in6p) && refcount_up) {
274163953Srrs		/* reduce ref-count */
275163953Srrs		SCTP_INP_WLOCK(in6p);
276163953Srrs		SCTP_INP_DECR_REF(in6p);
277163953Srrs		SCTP_INP_WUNLOCK(in6p);
278163953Srrs	}
279228907Stuexen	return (IPPROTO_DONE);
280163953Srrs
281163953Srrsbad:
282169420Srrs	if (stcb) {
283163953Srrs		SCTP_TCB_UNLOCK(stcb);
284169420Srrs	}
285163953Srrs	if ((in6p) && refcount_up) {
286163953Srrs		/* reduce ref-count */
287163953Srrs		SCTP_INP_WLOCK(in6p);
288163953Srrs		SCTP_INP_DECR_REF(in6p);
289163953Srrs		SCTP_INP_WUNLOCK(in6p);
290163953Srrs	}
291163953Srrs	if (m)
292169352Srrs		sctp_m_freem(m);
293228907Stuexen	return (IPPROTO_DONE);
294163953Srrs}
295163953Srrs
296163953Srrs
297163953Srrsstatic void
298171259Sdelphijsctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
299171259Sdelphij    struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
300163953Srrs{
301168299Srrs	uint32_t nxtsz;
302163953Srrs
303163953Srrs	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
304163953Srrs	    (icmp6 == NULL) || (sh == NULL)) {
305163953Srrs		goto out;
306163953Srrs	}
307163953Srrs	/* First do we even look at it? */
308163953Srrs	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
309163953Srrs		goto out;
310163953Srrs
311163953Srrs	if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
312163953Srrs		/* not PACKET TO BIG */
313163953Srrs		goto out;
314163953Srrs	}
315163953Srrs	/*
316163953Srrs	 * ok we need to look closely. We could even get smarter and look at
317163953Srrs	 * anyone that we sent to in case we get a different ICMP that tells
318163953Srrs	 * us there is no way to reach a host, but for this impl, all we
319163953Srrs	 * care about is MTU discovery.
320163953Srrs	 */
321163953Srrs	nxtsz = ntohl(icmp6->icmp6_mtu);
322163953Srrs	/* Stop any PMTU timer */
323165220Srrs	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
324163953Srrs
325163953Srrs	/* Adjust destination size limit */
326163953Srrs	if (net->mtu > nxtsz) {
327163953Srrs		net->mtu = nxtsz;
328185694Srrs		if (net->port) {
329185694Srrs			net->mtu -= sizeof(struct udphdr);
330185694Srrs		}
331163953Srrs	}
332163953Srrs	/* now what about the ep? */
333163953Srrs	if (stcb->asoc.smallest_mtu > nxtsz) {
334163953Srrs		struct sctp_tmit_chunk *chk;
335163953Srrs
336163953Srrs		/* Adjust that too */
337163953Srrs		stcb->asoc.smallest_mtu = nxtsz;
338163953Srrs		/* now off to subtract IP_DF flag if needed */
339163953Srrs
340163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
341168299Srrs			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
342163953Srrs				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
343163953Srrs			}
344163953Srrs		}
345163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
346168299Srrs			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
347163953Srrs				/*
348163953Srrs				 * For this guy we also mark for immediate
349163953Srrs				 * resend since we sent to big of chunk
350163953Srrs				 */
351163953Srrs				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
352163953Srrs				if (chk->sent != SCTP_DATAGRAM_RESEND)
353163953Srrs					stcb->asoc.sent_queue_retran_cnt++;
354163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
355163953Srrs				chk->rec.data.doing_fast_retransmit = 0;
356163953Srrs
357163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
358163953Srrs				/* Clear any time so NO RTT is being done */
359163953Srrs				chk->sent_rcv_time.tv_sec = 0;
360163953Srrs				chk->sent_rcv_time.tv_usec = 0;
361163953Srrs				stcb->asoc.total_flight -= chk->send_size;
362163953Srrs				net->flight_size -= chk->send_size;
363163953Srrs			}
364163953Srrs		}
365163953Srrs	}
366163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
367163953Srrsout:
368169420Srrs	if (stcb) {
369163953Srrs		SCTP_TCB_UNLOCK(stcb);
370169420Srrs	}
371163953Srrs}
372163953Srrs
373163953Srrs
374172156Srrsvoid
375172091Srrssctp6_notify(struct sctp_inpcb *inp,
376172091Srrs    struct icmp6_hdr *icmph,
377172091Srrs    struct sctphdr *sh,
378172091Srrs    struct sockaddr *to,
379172091Srrs    struct sctp_tcb *stcb,
380172091Srrs    struct sctp_nets *net)
381172091Srrs{
382172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
383172091Srrs	struct socket *so;
384172091Srrs
385172091Srrs#endif
386172091Srrs	/* protection */
387172091Srrs	int reason;
388172091Srrs
389172091Srrs
390172091Srrs	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
391172091Srrs	    (sh == NULL) || (to == NULL)) {
392172091Srrs		if (stcb)
393172091Srrs			SCTP_TCB_UNLOCK(stcb);
394172091Srrs		return;
395172091Srrs	}
396172091Srrs	/* First job is to verify the vtag matches what I would send */
397172091Srrs	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
398172091Srrs		SCTP_TCB_UNLOCK(stcb);
399172091Srrs		return;
400172091Srrs	}
401172091Srrs	if (icmph->icmp6_type != ICMP_UNREACH) {
402172091Srrs		/* We only care about unreachable */
403172091Srrs		SCTP_TCB_UNLOCK(stcb);
404172091Srrs		return;
405172091Srrs	}
406172091Srrs	if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
407172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
408172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
409172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
410172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
411172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
412172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
413172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
414172091Srrs
415172091Srrs		/*
416172091Srrs		 * Hmm reachablity problems we must examine closely. If its
417172091Srrs		 * not reachable, we may have lost a network. Or if there is
418172091Srrs		 * NO protocol at the other end named SCTP. well we consider
419172091Srrs		 * it a OOTB abort.
420172091Srrs		 */
421172091Srrs		if (net->dest_state & SCTP_ADDR_REACHABLE) {
422172091Srrs			/* Ok that destination is NOT reachable */
423172091Srrs			net->dest_state &= ~SCTP_ADDR_REACHABLE;
424224641Stuexen			net->dest_state &= ~SCTP_ADDR_PF;
425172091Srrs			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
426172091Srrs			    stcb, SCTP_FAILED_THRESHOLD,
427172091Srrs			    (void *)net, SCTP_SO_NOT_LOCKED);
428172091Srrs		}
429172091Srrs		SCTP_TCB_UNLOCK(stcb);
430172091Srrs	} else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
431172091Srrs	    (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
432172091Srrs		/*
433172091Srrs		 * Here the peer is either playing tricks on us, including
434172091Srrs		 * an address that belongs to someone who does not support
435172091Srrs		 * SCTP OR was a userland implementation that shutdown and
436172091Srrs		 * now is dead. In either case treat it like a OOTB abort
437172091Srrs		 * with no TCB
438172091Srrs		 */
439172091Srrs		reason = SCTP_PEER_FAULTY;
440172091Srrs		sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
441172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
442172091Srrs		so = SCTP_INP_SO(inp);
443172091Srrs		atomic_add_int(&stcb->asoc.refcnt, 1);
444172091Srrs		SCTP_TCB_UNLOCK(stcb);
445172091Srrs		SCTP_SOCKET_LOCK(so, 1);
446172091Srrs		SCTP_TCB_LOCK(stcb);
447172091Srrs		atomic_subtract_int(&stcb->asoc.refcnt, 1);
448172091Srrs#endif
449172091Srrs		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
450172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
451172091Srrs		SCTP_SOCKET_UNLOCK(so, 1);
452172091Srrs		/* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
453172091Srrs#endif
454172091Srrs		/* no need to unlock here, since the TCB is gone */
455172091Srrs	} else {
456172091Srrs		SCTP_TCB_UNLOCK(stcb);
457172091Srrs	}
458172091Srrs}
459172091Srrs
460172091Srrs
461172091Srrs
462163953Srrsvoid
463171259Sdelphijsctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
464163953Srrs{
465163953Srrs	struct sctphdr sh;
466163953Srrs	struct ip6ctlparam *ip6cp = NULL;
467167598Srrs	uint32_t vrf_id;
468163953Srrs
469167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
470167598Srrs
471163953Srrs	if (pktdst->sa_family != AF_INET6 ||
472163953Srrs	    pktdst->sa_len != sizeof(struct sockaddr_in6))
473163953Srrs		return;
474163953Srrs
475163953Srrs	if ((unsigned)cmd >= PRC_NCMDS)
476163953Srrs		return;
477163953Srrs	if (PRC_IS_REDIRECT(cmd)) {
478163953Srrs		d = NULL;
479163953Srrs	} else if (inet6ctlerrmap[cmd] == 0) {
480163953Srrs		return;
481163953Srrs	}
482163953Srrs	/* if the parameter is from icmp6, decode it. */
483163953Srrs	if (d != NULL) {
484163953Srrs		ip6cp = (struct ip6ctlparam *)d;
485163953Srrs	} else {
486163953Srrs		ip6cp = (struct ip6ctlparam *)NULL;
487163953Srrs	}
488163953Srrs
489163953Srrs	if (ip6cp) {
490163953Srrs		/*
491163953Srrs		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
492163953Srrs		 * valid.
493163953Srrs		 */
494163953Srrs		/* check if we can safely examine src and dst ports */
495163953Srrs		struct sctp_inpcb *inp = NULL;
496163953Srrs		struct sctp_tcb *stcb = NULL;
497163953Srrs		struct sctp_nets *net = NULL;
498163953Srrs		struct sockaddr_in6 final;
499163953Srrs
500165647Srrs		if (ip6cp->ip6c_m == NULL)
501163953Srrs			return;
502163953Srrs
503163953Srrs		bzero(&sh, sizeof(sh));
504163953Srrs		bzero(&final, sizeof(final));
505163953Srrs		inp = NULL;
506163953Srrs		net = NULL;
507163953Srrs		m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
508163953Srrs		    (caddr_t)&sh);
509163953Srrs		ip6cp->ip6c_src->sin6_port = sh.src_port;
510163953Srrs		final.sin6_len = sizeof(final);
511163953Srrs		final.sin6_family = AF_INET6;
512163953Srrs		final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
513163953Srrs		final.sin6_port = sh.dest_port;
514163953Srrs		stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
515163953Srrs		    (struct sockaddr *)&final,
516167598Srrs		    &inp, &net, 1, vrf_id);
517163953Srrs		/* inp's ref-count increased && stcb locked */
518163953Srrs		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
519163953Srrs			if (cmd == PRC_MSGSIZE) {
520163953Srrs				sctp6_notify_mbuf(inp,
521163953Srrs				    ip6cp->ip6c_icmp6,
522163953Srrs				    &sh,
523163953Srrs				    stcb,
524163953Srrs				    net);
525163953Srrs				/* inp's ref-count reduced && stcb unlocked */
526163953Srrs			} else {
527172091Srrs				sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
528163953Srrs				    (struct sockaddr *)&final,
529163953Srrs				    stcb, net);
530163953Srrs				/* inp's ref-count reduced && stcb unlocked */
531163953Srrs			}
532163953Srrs		} else {
533163953Srrs			if (PRC_IS_REDIRECT(cmd) && inp) {
534163953Srrs				in6_rtchange((struct in6pcb *)inp,
535163953Srrs				    inet6ctlerrmap[cmd]);
536163953Srrs			}
537163953Srrs			if (inp) {
538163953Srrs				/* reduce inp's ref-count */
539163953Srrs				SCTP_INP_WLOCK(inp);
540163953Srrs				SCTP_INP_DECR_REF(inp);
541163953Srrs				SCTP_INP_WUNLOCK(inp);
542163953Srrs			}
543163953Srrs			if (stcb)
544163953Srrs				SCTP_TCB_UNLOCK(stcb);
545163953Srrs		}
546163953Srrs	}
547163953Srrs}
548163953Srrs
549163953Srrs/*
550163953Srrs * this routine can probably be collasped into the one in sctp_userreq.c
551163953Srrs * since they do the same thing and now we lookup with a sockaddr
552163953Srrs */
553163953Srrsstatic int
554163953Srrssctp6_getcred(SYSCTL_HANDLER_ARGS)
555163953Srrs{
556164085Srrs	struct xucred xuc;
557163953Srrs	struct sockaddr_in6 addrs[2];
558163953Srrs	struct sctp_inpcb *inp;
559163953Srrs	struct sctp_nets *net;
560163953Srrs	struct sctp_tcb *stcb;
561164085Srrs	int error;
562167598Srrs	uint32_t vrf_id;
563163953Srrs
564167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
565167598Srrs
566170587Srwatson	error = priv_check(req->td, PRIV_NETINET_GETCRED);
567163953Srrs	if (error)
568163953Srrs		return (error);
569163953Srrs
570171943Srrs	if (req->newlen != sizeof(addrs)) {
571171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
572163953Srrs		return (EINVAL);
573171943Srrs	}
574171943Srrs	if (req->oldlen != sizeof(struct ucred)) {
575171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
576163953Srrs		return (EINVAL);
577171943Srrs	}
578163953Srrs	error = SYSCTL_IN(req, addrs, sizeof(addrs));
579163953Srrs	if (error)
580163953Srrs		return (error);
581163953Srrs
582163953Srrs	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
583163953Srrs	    sin6tosa(&addrs[1]),
584167598Srrs	    &inp, &net, 1, vrf_id);
585163953Srrs	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
586164085Srrs		if ((inp != NULL) && (stcb == NULL)) {
587164085Srrs			/* reduce ref-count */
588163953Srrs			SCTP_INP_WLOCK(inp);
589163953Srrs			SCTP_INP_DECR_REF(inp);
590164085Srrs			goto cred_can_cont;
591163953Srrs		}
592171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
593164085Srrs		error = ENOENT;
594163953Srrs		goto out;
595163953Srrs	}
596163953Srrs	SCTP_TCB_UNLOCK(stcb);
597164085Srrs	/*
598164085Srrs	 * We use the write lock here, only since in the error leg we need
599164085Srrs	 * it. If we used RLOCK, then we would have to
600164085Srrs	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
601164085Srrs	 * Better to use higher wlock.
602164085Srrs	 */
603164085Srrs	SCTP_INP_WLOCK(inp);
604164085Srrscred_can_cont:
605164085Srrs	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
606164085Srrs	if (error) {
607164085Srrs		SCTP_INP_WUNLOCK(inp);
608164085Srrs		goto out;
609164085Srrs	}
610164085Srrs	cru2x(inp->sctp_socket->so_cred, &xuc);
611164085Srrs	SCTP_INP_WUNLOCK(inp);
612164085Srrs	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
613163953Srrsout:
614163953Srrs	return (error);
615163953Srrs}
616163953Srrs
617163953SrrsSYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
618163953Srrs    0, 0,
619163953Srrs    sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
620163953Srrs
621163953Srrs
622163953Srrs/* This is the same as the sctp_abort() could be made common */
623163953Srrsstatic void
624163953Srrssctp6_abort(struct socket *so)
625163953Srrs{
626163953Srrs	struct sctp_inpcb *inp;
627163953Srrs	uint32_t flags;
628163953Srrs
629163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
630172091Srrs	if (inp == 0) {
631172091Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
632163953Srrs		return;
633172091Srrs	}
634163953Srrssctp_must_try_again:
635163953Srrs	flags = inp->sctp_flags;
636163953Srrs#ifdef SCTP_LOG_CLOSING
637163953Srrs	sctp_log_closing(inp, NULL, 17);
638163953Srrs#endif
639163953Srrs	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
640163953Srrs	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
641163953Srrs#ifdef SCTP_LOG_CLOSING
642163953Srrs		sctp_log_closing(inp, NULL, 16);
643163953Srrs#endif
644169380Srrs		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
645169380Srrs		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
646163953Srrs		SOCK_LOCK(so);
647167695Srrs		SCTP_SB_CLEAR(so->so_snd);
648163953Srrs		/*
649163953Srrs		 * same for the rcv ones, they are only here for the
650163953Srrs		 * accounting/select.
651163953Srrs		 */
652167695Srrs		SCTP_SB_CLEAR(so->so_rcv);
653167695Srrs		/* Now null out the reference, we are completely detached. */
654163953Srrs		so->so_pcb = NULL;
655163953Srrs		SOCK_UNLOCK(so);
656163953Srrs	} else {
657163953Srrs		flags = inp->sctp_flags;
658163953Srrs		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
659163953Srrs			goto sctp_must_try_again;
660163953Srrs		}
661163953Srrs	}
662163953Srrs	return;
663163953Srrs}
664163953Srrs
665163953Srrsstatic int
666228653Stuexensctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
667163953Srrs{
668163953Srrs	struct in6pcb *inp6;
669166086Srrs	int error;
670163953Srrs	struct sctp_inpcb *inp;
671170205Srrs	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
672163953Srrs
673163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
674171943Srrs	if (inp != NULL) {
675171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
676228907Stuexen		return (EINVAL);
677171943Srrs	}
678163953Srrs	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
679179783Srrs		error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
680163953Srrs		if (error)
681228907Stuexen			return (error);
682163953Srrs	}
683170205Srrs	error = sctp_inpcb_alloc(so, vrf_id);
684163953Srrs	if (error)
685228907Stuexen		return (error);
686163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
687170205Srrs	SCTP_INP_WLOCK(inp);
688163953Srrs	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
689163953Srrs	inp6 = (struct in6pcb *)inp;
690163953Srrs
691163953Srrs	inp6->inp_vflag |= INP_IPV6;
692163953Srrs	inp6->in6p_hops = -1;	/* use kernel default */
693163953Srrs	inp6->in6p_cksum = -1;	/* just to be sure */
694163953Srrs#ifdef INET
695163953Srrs	/*
696163953Srrs	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
697163953Srrs	 * socket as well, because the socket may be bound to an IPv6
698163953Srrs	 * wildcard address, which may match an IPv4-mapped IPv6 address.
699163953Srrs	 */
700197288Srrs	inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
701163953Srrs#endif
702163953Srrs	/*
703163953Srrs	 * Hmm what about the IPSEC stuff that is missing here but in
704163953Srrs	 * sctp_attach()?
705163953Srrs	 */
706170205Srrs	SCTP_INP_WUNLOCK(inp);
707228907Stuexen	return (0);
708163953Srrs}
709163953Srrs
710163953Srrsstatic int
711163953Srrssctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
712163953Srrs{
713163953Srrs	struct sctp_inpcb *inp;
714163953Srrs	struct in6pcb *inp6;
715166086Srrs	int error;
716163953Srrs
717163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
718171943Srrs	if (inp == 0) {
719171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
720228907Stuexen		return (EINVAL);
721171943Srrs	}
722170056Srrs	if (addr) {
723221249Stuexen		switch (addr->sa_family) {
724221249Stuexen#ifdef INET
725221249Stuexen		case AF_INET:
726221249Stuexen			if (addr->sa_len != sizeof(struct sockaddr_in)) {
727221249Stuexen				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
728228907Stuexen				return (EINVAL);
729221249Stuexen			}
730221249Stuexen			break;
731221249Stuexen#endif
732221249Stuexen#ifdef INET6
733221249Stuexen		case AF_INET6:
734221249Stuexen			if (addr->sa_len != sizeof(struct sockaddr_in6)) {
735221249Stuexen				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
736228907Stuexen				return (EINVAL);
737221249Stuexen			}
738221249Stuexen			break;
739221249Stuexen#endif
740221249Stuexen		default:
741171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
742228907Stuexen			return (EINVAL);
743170056Srrs		}
744170056Srrs	}
745163953Srrs	inp6 = (struct in6pcb *)inp;
746163953Srrs	inp6->inp_vflag &= ~INP_IPV4;
747163953Srrs	inp6->inp_vflag |= INP_IPV6;
748166023Srrs	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
749221249Stuexen		switch (addr->sa_family) {
750221249Stuexen#ifdef INET
751221249Stuexen		case AF_INET:
752163953Srrs			/* binding v4 addr to v6 socket, so reset flags */
753163953Srrs			inp6->inp_vflag |= INP_IPV4;
754163953Srrs			inp6->inp_vflag &= ~INP_IPV6;
755221249Stuexen			break;
756221249Stuexen#endif
757221249Stuexen#ifdef INET6
758221249Stuexen		case AF_INET6:
759221249Stuexen			{
760221249Stuexen				struct sockaddr_in6 *sin6_p;
761163953Srrs
762221249Stuexen				sin6_p = (struct sockaddr_in6 *)addr;
763163953Srrs
764221249Stuexen				if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
765221249Stuexen					inp6->inp_vflag |= INP_IPV4;
766221249Stuexen				}
767221249Stuexen#ifdef INET
768221249Stuexen				if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
769221249Stuexen					struct sockaddr_in sin;
770163953Srrs
771221249Stuexen					in6_sin6_2_sin(&sin, sin6_p);
772221249Stuexen					inp6->inp_vflag |= INP_IPV4;
773221249Stuexen					inp6->inp_vflag &= ~INP_IPV6;
774221249Stuexen					error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
775228907Stuexen					return (error);
776221249Stuexen				}
777221249Stuexen#endif
778221249Stuexen				break;
779163953Srrs			}
780221249Stuexen#endif
781221249Stuexen		default:
782221249Stuexen			break;
783163953Srrs		}
784163953Srrs	} else if (addr != NULL) {
785221249Stuexen		struct sockaddr_in6 *sin6_p;
786221249Stuexen
787163953Srrs		/* IPV6_V6ONLY socket */
788221249Stuexen#ifdef INET
789163953Srrs		if (addr->sa_family == AF_INET) {
790163953Srrs			/* can't bind v4 addr to v6 only socket! */
791171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
792228907Stuexen			return (EINVAL);
793221249Stuexen		}
794221249Stuexen#endif
795221249Stuexen		sin6_p = (struct sockaddr_in6 *)addr;
796163953Srrs
797221249Stuexen		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
798221249Stuexen			/* can't bind v4-mapped addrs either! */
799221249Stuexen			/* NOTE: we don't support SIIT */
800221249Stuexen			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
801228907Stuexen			return (EINVAL);
802163953Srrs		}
803163953Srrs	}
804171572Srrs	error = sctp_inpcb_bind(so, addr, NULL, p);
805228907Stuexen	return (error);
806163953Srrs}
807163953Srrs
808163953Srrs
809163953Srrsstatic void
810163953Srrssctp6_close(struct socket *so)
811163953Srrs{
812171990Srrs	sctp_close(so);
813163953Srrs}
814163953Srrs
815167598Srrs/* This could be made common with sctp_detach() since they are identical */
816163953Srrs
817168709Srrsstatic
818168709Srrsint
819163953Srrssctp6_disconnect(struct socket *so)
820163953Srrs{
821171990Srrs	return (sctp_disconnect(so));
822163953Srrs}
823163953Srrs
824168709Srrs
825163953Srrsint
826163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
827163953Srrs    struct mbuf *control, struct thread *p);
828163953Srrs
829163953Srrs
830163953Srrsstatic int
831163953Srrssctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
832163953Srrs    struct mbuf *control, struct thread *p)
833163953Srrs{
834163953Srrs	struct sctp_inpcb *inp;
835163953Srrs	struct in6pcb *inp6;
836163953Srrs
837163953Srrs#ifdef INET
838163953Srrs	struct sockaddr_in6 *sin6;
839163953Srrs
840163953Srrs#endif				/* INET */
841163953Srrs	/* No SPL needed since sctp_output does this */
842163953Srrs
843163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
844163953Srrs	if (inp == NULL) {
845163953Srrs		if (control) {
846169352Srrs			SCTP_RELEASE_PKT(control);
847163953Srrs			control = NULL;
848163953Srrs		}
849169352Srrs		SCTP_RELEASE_PKT(m);
850171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
851228907Stuexen		return (EINVAL);
852163953Srrs	}
853163953Srrs	inp6 = (struct in6pcb *)inp;
854163953Srrs	/*
855163953Srrs	 * For the TCP model we may get a NULL addr, if we are a connected
856163953Srrs	 * socket thats ok.
857163953Srrs	 */
858163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
859163953Srrs	    (addr == NULL)) {
860163953Srrs		goto connected_type;
861163953Srrs	}
862163953Srrs	if (addr == NULL) {
863169352Srrs		SCTP_RELEASE_PKT(m);
864163953Srrs		if (control) {
865169352Srrs			SCTP_RELEASE_PKT(control);
866163953Srrs			control = NULL;
867163953Srrs		}
868171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
869163953Srrs		return (EDESTADDRREQ);
870163953Srrs	}
871163953Srrs#ifdef INET
872163953Srrs	sin6 = (struct sockaddr_in6 *)addr;
873166023Srrs	if (SCTP_IPV6_V6ONLY(inp6)) {
874163953Srrs		/*
875163953Srrs		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
876163953Srrs		 * v4 addr or v4-mapped addr
877163953Srrs		 */
878163953Srrs		if (addr->sa_family == AF_INET) {
879171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
880228907Stuexen			return (EINVAL);
881163953Srrs		}
882163953Srrs		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
883171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
884228907Stuexen			return (EINVAL);
885163953Srrs		}
886163953Srrs	}
887163953Srrs	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
888197288Srrs		if (!MODULE_GLOBAL(ip6_v6only)) {
889163953Srrs			struct sockaddr_in sin;
890163953Srrs
891163953Srrs			/* convert v4-mapped into v4 addr and send */
892163953Srrs			in6_sin6_2_sin(&sin, sin6);
893228907Stuexen			return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
894228907Stuexen			    control, p));
895163953Srrs		} else {
896163953Srrs			/* mapped addresses aren't enabled */
897171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
898228907Stuexen			return (EINVAL);
899163953Srrs		}
900163953Srrs	}
901163953Srrs#endif				/* INET */
902163953Srrsconnected_type:
903163953Srrs	/* now what about control */
904163953Srrs	if (control) {
905163953Srrs		if (inp->control) {
906169420Srrs			SCTP_PRINTF("huh? control set?\n");
907169352Srrs			SCTP_RELEASE_PKT(inp->control);
908163953Srrs			inp->control = NULL;
909163953Srrs		}
910163953Srrs		inp->control = control;
911163953Srrs	}
912163953Srrs	/* Place the data */
913163953Srrs	if (inp->pkt) {
914165647Srrs		SCTP_BUF_NEXT(inp->pkt_last) = m;
915163953Srrs		inp->pkt_last = m;
916163953Srrs	} else {
917163953Srrs		inp->pkt_last = inp->pkt = m;
918163953Srrs	}
919163953Srrs	if (
920163953Srrs	/* FreeBSD and MacOSX uses a flag passed */
921163953Srrs	    ((flags & PRUS_MORETOCOME) == 0)
922163953Srrs	    ) {
923163953Srrs		/*
924163953Srrs		 * note with the current version this code will only be used
925163953Srrs		 * by OpenBSD, NetBSD and FreeBSD have methods for
926163953Srrs		 * re-defining sosend() to use sctp_sosend().  One can
927163953Srrs		 * optionaly switch back to this code (by changing back the
928163953Srrs		 * defininitions but this is not advisable.
929163953Srrs		 */
930163953Srrs		int ret;
931163953Srrs
932163953Srrs		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
933163953Srrs		inp->pkt = NULL;
934163953Srrs		inp->control = NULL;
935163953Srrs		return (ret);
936163953Srrs	} else {
937163953Srrs		return (0);
938163953Srrs	}
939163953Srrs}
940163953Srrs
941163953Srrsstatic int
942163953Srrssctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
943163953Srrs{
944167598Srrs	uint32_t vrf_id;
945163953Srrs	int error = 0;
946163953Srrs	struct sctp_inpcb *inp;
947163953Srrs	struct in6pcb *inp6;
948163953Srrs	struct sctp_tcb *stcb;
949163953Srrs
950163953Srrs#ifdef INET
951163953Srrs	struct sockaddr_in6 *sin6;
952163953Srrs	struct sockaddr_storage ss;
953163953Srrs
954221249Stuexen#endif
955163953Srrs
956163953Srrs	inp6 = (struct in6pcb *)so->so_pcb;
957163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
958163953Srrs	if (inp == 0) {
959171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
960163953Srrs		return (ECONNRESET);	/* I made the same as TCP since we are
961163953Srrs					 * not setup? */
962163953Srrs	}
963170056Srrs	if (addr == NULL) {
964171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
965170056Srrs		return (EINVAL);
966170056Srrs	}
967221249Stuexen	switch (addr->sa_family) {
968221249Stuexen#ifdef INET
969221249Stuexen	case AF_INET:
970221249Stuexen		if (addr->sa_len != sizeof(struct sockaddr_in)) {
971221249Stuexen			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
972221249Stuexen			return (EINVAL);
973221249Stuexen		}
974221249Stuexen		break;
975221249Stuexen#endif
976221249Stuexen#ifdef INET6
977221249Stuexen	case AF_INET6:
978221249Stuexen		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
979221249Stuexen			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
980221249Stuexen			return (EINVAL);
981221249Stuexen		}
982221249Stuexen		break;
983221249Stuexen#endif
984221249Stuexen	default:
985171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
986170056Srrs		return (EINVAL);
987170056Srrs	}
988221249Stuexen
989168299Srrs	vrf_id = inp->def_vrf_id;
990163953Srrs	SCTP_ASOC_CREATE_LOCK(inp);
991163953Srrs	SCTP_INP_RLOCK(inp);
992163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
993163953Srrs	    SCTP_PCB_FLAGS_UNBOUND) {
994163953Srrs		/* Bind a ephemeral port */
995163953Srrs		SCTP_INP_RUNLOCK(inp);
996163953Srrs		error = sctp6_bind(so, NULL, p);
997163953Srrs		if (error) {
998163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
999163953Srrs
1000163953Srrs			return (error);
1001163953Srrs		}
1002163953Srrs		SCTP_INP_RLOCK(inp);
1003163953Srrs	}
1004163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1005163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1006163953Srrs		/* We are already connected AND the TCP model */
1007163953Srrs		SCTP_INP_RUNLOCK(inp);
1008163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
1009171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
1010163953Srrs		return (EADDRINUSE);
1011163953Srrs	}
1012163953Srrs#ifdef INET
1013163953Srrs	sin6 = (struct sockaddr_in6 *)addr;
1014166023Srrs	if (SCTP_IPV6_V6ONLY(inp6)) {
1015163953Srrs		/*
1016163953Srrs		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
1017163953Srrs		 * addr or v4-mapped addr
1018163953Srrs		 */
1019163953Srrs		if (addr->sa_family == AF_INET) {
1020163953Srrs			SCTP_INP_RUNLOCK(inp);
1021163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
1022171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1023228907Stuexen			return (EINVAL);
1024163953Srrs		}
1025163953Srrs		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1026163953Srrs			SCTP_INP_RUNLOCK(inp);
1027163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
1028171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1029228907Stuexen			return (EINVAL);
1030163953Srrs		}
1031163953Srrs	}
1032163953Srrs	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1033197288Srrs		if (!MODULE_GLOBAL(ip6_v6only)) {
1034163953Srrs			/* convert v4-mapped into v4 addr */
1035163953Srrs			in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1036163953Srrs			addr = (struct sockaddr *)&ss;
1037163953Srrs		} else {
1038163953Srrs			/* mapped addresses aren't enabled */
1039163953Srrs			SCTP_INP_RUNLOCK(inp);
1040163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
1041171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1042228907Stuexen			return (EINVAL);
1043163953Srrs		}
1044221411Stuexen	}
1045163953Srrs#endif				/* INET */
1046163953Srrs	/* Now do we connect? */
1047163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1048163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1049169420Srrs		if (stcb) {
1050163953Srrs			SCTP_TCB_UNLOCK(stcb);
1051169420Srrs		}
1052163953Srrs		SCTP_INP_RUNLOCK(inp);
1053163953Srrs	} else {
1054163953Srrs		SCTP_INP_RUNLOCK(inp);
1055163953Srrs		SCTP_INP_WLOCK(inp);
1056163953Srrs		SCTP_INP_INCR_REF(inp);
1057163953Srrs		SCTP_INP_WUNLOCK(inp);
1058163953Srrs		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1059163953Srrs		if (stcb == NULL) {
1060163953Srrs			SCTP_INP_WLOCK(inp);
1061163953Srrs			SCTP_INP_DECR_REF(inp);
1062163953Srrs			SCTP_INP_WUNLOCK(inp);
1063163953Srrs		}
1064163953Srrs	}
1065163953Srrs
1066163953Srrs	if (stcb != NULL) {
1067163953Srrs		/* Already have or am bring up an association */
1068163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
1069163953Srrs		SCTP_TCB_UNLOCK(stcb);
1070171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
1071163953Srrs		return (EALREADY);
1072163953Srrs	}
1073163953Srrs	/* We are GOOD to go */
1074206137Stuexen	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
1075163953Srrs	SCTP_ASOC_CREATE_UNLOCK(inp);
1076163953Srrs	if (stcb == NULL) {
1077163953Srrs		/* Gak! no memory */
1078163953Srrs		return (error);
1079163953Srrs	}
1080163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1081163953Srrs		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1082163953Srrs		/* Set the connected flag so we can queue data */
1083163953Srrs		soisconnecting(so);
1084163953Srrs	}
1085163953Srrs	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1086169420Srrs	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1087163953Srrs
1088163953Srrs	/* initialize authentication parameters for the assoc */
1089163953Srrs	sctp_initialize_auth_params(inp, stcb);
1090163953Srrs
1091172090Srrs	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1092163953Srrs	SCTP_TCB_UNLOCK(stcb);
1093228907Stuexen	return (error);
1094163953Srrs}
1095163953Srrs
1096163953Srrsstatic int
1097163953Srrssctp6_getaddr(struct socket *so, struct sockaddr **addr)
1098163953Srrs{
1099163953Srrs	struct sockaddr_in6 *sin6;
1100163953Srrs	struct sctp_inpcb *inp;
1101167598Srrs	uint32_t vrf_id;
1102167598Srrs	struct sctp_ifa *sctp_ifa;
1103163953Srrs
1104163953Srrs	int error;
1105163953Srrs
1106163953Srrs	/*
1107163953Srrs	 * Do the malloc first in case it blocks.
1108163953Srrs	 */
1109221249Stuexen	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
1110211944Stuexen	if (sin6 == NULL)
1111228907Stuexen		return (ENOMEM);
1112163953Srrs	sin6->sin6_family = AF_INET6;
1113163953Srrs	sin6->sin6_len = sizeof(*sin6);
1114163953Srrs
1115163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1116163953Srrs	if (inp == NULL) {
1117163953Srrs		SCTP_FREE_SONAME(sin6);
1118171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1119228907Stuexen		return (ECONNRESET);
1120163953Srrs	}
1121163953Srrs	SCTP_INP_RLOCK(inp);
1122163953Srrs	sin6->sin6_port = inp->sctp_lport;
1123163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1124163953Srrs		/* For the bound all case you get back 0 */
1125163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1126163953Srrs			struct sctp_tcb *stcb;
1127163953Srrs			struct sockaddr_in6 *sin_a6;
1128163953Srrs			struct sctp_nets *net;
1129163953Srrs			int fnd;
1130163953Srrs
1131163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1132163953Srrs			if (stcb == NULL) {
1133163953Srrs				goto notConn6;
1134163953Srrs			}
1135163953Srrs			fnd = 0;
1136163953Srrs			sin_a6 = NULL;
1137163953Srrs			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1138163953Srrs				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1139164085Srrs				if (sin_a6 == NULL)
1140164085Srrs					/* this will make coverity happy */
1141164085Srrs					continue;
1142164085Srrs
1143163953Srrs				if (sin_a6->sin6_family == AF_INET6) {
1144163953Srrs					fnd = 1;
1145163953Srrs					break;
1146163953Srrs				}
1147163953Srrs			}
1148163953Srrs			if ((!fnd) || (sin_a6 == NULL)) {
1149163953Srrs				/* punt */
1150163953Srrs				goto notConn6;
1151163953Srrs			}
1152168299Srrs			vrf_id = inp->def_vrf_id;
1153168299Srrs			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1154167598Srrs			if (sctp_ifa) {
1155167598Srrs				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1156167598Srrs			}
1157163953Srrs		} else {
1158163953Srrs			/* For the bound all case you get back 0 */
1159163953Srrs	notConn6:
1160163953Srrs			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1161163953Srrs		}
1162163953Srrs	} else {
1163163953Srrs		/* Take the first IPv6 address in the list */
1164163953Srrs		struct sctp_laddr *laddr;
1165163953Srrs		int fnd = 0;
1166163953Srrs
1167163953Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1168167598Srrs			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1169163953Srrs				struct sockaddr_in6 *sin_a;
1170163953Srrs
1171167598Srrs				sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1172163953Srrs				sin6->sin6_addr = sin_a->sin6_addr;
1173163953Srrs				fnd = 1;
1174163953Srrs				break;
1175163953Srrs			}
1176163953Srrs		}
1177163953Srrs		if (!fnd) {
1178163953Srrs			SCTP_FREE_SONAME(sin6);
1179163953Srrs			SCTP_INP_RUNLOCK(inp);
1180171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1181228907Stuexen			return (ENOENT);
1182163953Srrs		}
1183163953Srrs	}
1184163953Srrs	SCTP_INP_RUNLOCK(inp);
1185163953Srrs	/* Scoping things for v6 */
1186164085Srrs	if ((error = sa6_recoverscope(sin6)) != 0) {
1187164085Srrs		SCTP_FREE_SONAME(sin6);
1188163953Srrs		return (error);
1189164085Srrs	}
1190163953Srrs	(*addr) = (struct sockaddr *)sin6;
1191163953Srrs	return (0);
1192163953Srrs}
1193163953Srrs
1194163953Srrsstatic int
1195163953Srrssctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1196163953Srrs{
1197163953Srrs	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1198163953Srrs	int fnd;
1199163953Srrs	struct sockaddr_in6 *sin_a6;
1200163953Srrs	struct sctp_inpcb *inp;
1201163953Srrs	struct sctp_tcb *stcb;
1202163953Srrs	struct sctp_nets *net;
1203163953Srrs	int error;
1204163953Srrs
1205228907Stuexen	/* Do the malloc first in case it blocks. */
1206163953Srrs	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1207211944Stuexen	if (sin6 == NULL)
1208211944Stuexen		return (ENOMEM);
1209163953Srrs	sin6->sin6_family = AF_INET6;
1210163953Srrs	sin6->sin6_len = sizeof(*sin6);
1211163953Srrs
1212163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1213228907Stuexen	if ((inp == NULL) ||
1214228907Stuexen	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1215228907Stuexen		/* UDP type and listeners will drop out here */
1216163953Srrs		SCTP_FREE_SONAME(sin6);
1217228907Stuexen		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1218228907Stuexen		return (ENOTCONN);
1219163953Srrs	}
1220163953Srrs	SCTP_INP_RLOCK(inp);
1221163953Srrs	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1222169420Srrs	if (stcb) {
1223163953Srrs		SCTP_TCB_LOCK(stcb);
1224169420Srrs	}
1225163953Srrs	SCTP_INP_RUNLOCK(inp);
1226163953Srrs	if (stcb == NULL) {
1227163953Srrs		SCTP_FREE_SONAME(sin6);
1228171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1229228907Stuexen		return (ECONNRESET);
1230163953Srrs	}
1231163953Srrs	fnd = 0;
1232163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1233163953Srrs		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1234163953Srrs		if (sin_a6->sin6_family == AF_INET6) {
1235163953Srrs			fnd = 1;
1236163953Srrs			sin6->sin6_port = stcb->rport;
1237163953Srrs			sin6->sin6_addr = sin_a6->sin6_addr;
1238163953Srrs			break;
1239163953Srrs		}
1240163953Srrs	}
1241163953Srrs	SCTP_TCB_UNLOCK(stcb);
1242163953Srrs	if (!fnd) {
1243163953Srrs		/* No IPv4 address */
1244163953Srrs		SCTP_FREE_SONAME(sin6);
1245171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1246228907Stuexen		return (ENOENT);
1247163953Srrs	}
1248163953Srrs	if ((error = sa6_recoverscope(sin6)) != 0)
1249163953Srrs		return (error);
1250163953Srrs	*addr = (struct sockaddr *)sin6;
1251163953Srrs	return (0);
1252163953Srrs}
1253163953Srrs
1254163953Srrsstatic int
1255163953Srrssctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1256163953Srrs{
1257221249Stuexen#ifdef INET
1258163953Srrs	struct sockaddr *addr;
1259221249Stuexen
1260221249Stuexen#endif
1261163953Srrs	struct in6pcb *inp6 = sotoin6pcb(so);
1262166086Srrs	int error;
1263163953Srrs
1264171943Srrs	if (inp6 == NULL) {
1265171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1266228907Stuexen		return (EINVAL);
1267171943Srrs	}
1268163953Srrs	/* allow v6 addresses precedence */
1269163953Srrs	error = sctp6_getaddr(so, nam);
1270221249Stuexen#ifdef INET
1271163953Srrs	if (error) {
1272163953Srrs		/* try v4 next if v6 failed */
1273163953Srrs		error = sctp_ingetaddr(so, nam);
1274163953Srrs		if (error) {
1275163953Srrs			return (error);
1276163953Srrs		}
1277163953Srrs		addr = *nam;
1278163953Srrs		/* if I'm V6ONLY, convert it to v4-mapped */
1279166023Srrs		if (SCTP_IPV6_V6ONLY(inp6)) {
1280163953Srrs			struct sockaddr_in6 sin6;
1281163953Srrs
1282163953Srrs			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1283163953Srrs			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1284163953Srrs		}
1285163953Srrs	}
1286221249Stuexen#endif
1287163953Srrs	return (error);
1288163953Srrs}
1289163953Srrs
1290163953Srrs
1291163953Srrsstatic int
1292163953Srrssctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1293163953Srrs{
1294221249Stuexen#ifdef INET
1295221249Stuexen	struct sockaddr *addr;
1296221249Stuexen
1297221249Stuexen#endif
1298163953Srrs	struct in6pcb *inp6 = sotoin6pcb(so);
1299166086Srrs	int error;
1300163953Srrs
1301171943Srrs	if (inp6 == NULL) {
1302171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1303228907Stuexen		return (EINVAL);
1304171943Srrs	}
1305163953Srrs	/* allow v6 addresses precedence */
1306163953Srrs	error = sctp6_peeraddr(so, nam);
1307221249Stuexen#ifdef INET
1308163953Srrs	if (error) {
1309163953Srrs		/* try v4 next if v6 failed */
1310163953Srrs		error = sctp_peeraddr(so, nam);
1311163953Srrs		if (error) {
1312163953Srrs			return (error);
1313163953Srrs		}
1314221249Stuexen		addr = *nam;
1315163953Srrs		/* if I'm V6ONLY, convert it to v4-mapped */
1316166023Srrs		if (SCTP_IPV6_V6ONLY(inp6)) {
1317163953Srrs			struct sockaddr_in6 sin6;
1318163953Srrs
1319163953Srrs			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1320163953Srrs			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1321163953Srrs		}
1322163953Srrs	}
1323221249Stuexen#endif
1324228907Stuexen	return (error);
1325163953Srrs}
1326163953Srrs
1327163953Srrsstruct pr_usrreqs sctp6_usrreqs = {
1328163953Srrs	.pru_abort = sctp6_abort,
1329163953Srrs	.pru_accept = sctp_accept,
1330163953Srrs	.pru_attach = sctp6_attach,
1331163953Srrs	.pru_bind = sctp6_bind,
1332163953Srrs	.pru_connect = sctp6_connect,
1333163953Srrs	.pru_control = in6_control,
1334163953Srrs	.pru_close = sctp6_close,
1335163953Srrs	.pru_detach = sctp6_close,
1336163953Srrs	.pru_sopoll = sopoll_generic,
1337178201Srrs	.pru_flush = sctp_flush,
1338163953Srrs	.pru_disconnect = sctp6_disconnect,
1339163953Srrs	.pru_listen = sctp_listen,
1340163953Srrs	.pru_peeraddr = sctp6_getpeeraddr,
1341163953Srrs	.pru_send = sctp6_send,
1342163953Srrs	.pru_shutdown = sctp_shutdown,
1343163953Srrs	.pru_sockaddr = sctp6_in6getaddr,
1344163953Srrs	.pru_sosend = sctp_sosend,
1345163953Srrs	.pru_soreceive = sctp_soreceive
1346163953Srrs};
1347