sctp6_usrreq.c revision 293897
155714Skris/*-
255714Skris * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
355714Skris * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
455714Skris * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
555714Skris *
655714Skris * Redistribution and use in source and binary forms, with or without
755714Skris * modification, are permitted provided that the following conditions are met:
8280304Sjkim *
955714Skris * a) Redistributions of source code must retain the above copyright notice,
1055714Skris *    this list of conditions and the following disclaimer.
1155714Skris *
1255714Skris * b) Redistributions in binary form must reproduce the above copyright
1355714Skris *    notice, this list of conditions and the following disclaimer in
1455714Skris *    the documentation and/or other materials provided with the distribution.
15280304Sjkim *
1655714Skris * c) Neither the name of Cisco Systems, Inc. nor the names of its
1755714Skris *    contributors may be used to endorse or promote products derived
1855714Skris *    from this software without specific prior written permission.
1955714Skris *
2055714Skris * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2155714Skris * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22280304Sjkim * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2355714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2455714Skris * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2555714Skris * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2655714Skris * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2755714Skris * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2855714Skris * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2955714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3055714Skris * THE POSSIBILITY OF SUCH DAMAGE.
3155714Skris */
3255714Skris
3355714Skris#include <sys/cdefs.h>
3455714Skris__FBSDID("$FreeBSD: stable/10/sys/netinet6/sctp6_usrreq.c 293897 2016-01-14 09:11:42Z glebius $");
3555714Skris
3655714Skris#include <netinet/sctp_os.h>
37280304Sjkim#ifdef INET6
3855714Skris#include <sys/proc.h>
3955714Skris#include <netinet/sctp_pcb.h>
40280304Sjkim#include <netinet/sctp_header.h>
4155714Skris#include <netinet/sctp_var.h>
4255714Skris#ifdef INET6
4355714Skris#include <netinet6/sctp6_var.h>
4455714Skris#endif
4555714Skris#include <netinet/sctp_sysctl.h>
4655714Skris#include <netinet/sctp_output.h>
4755714Skris#include <netinet/sctp_uio.h>
4855714Skris#include <netinet/sctp_asconf.h>
4955714Skris#include <netinet/sctputil.h>
5055714Skris#include <netinet/sctp_indata.h>
5155714Skris#include <netinet/sctp_timer.h>
52280304Sjkim#include <netinet/sctp_auth.h>
5355714Skris#include <netinet/sctp_input.h>
5455714Skris#include <netinet/sctp_output.h>
5555714Skris#include <netinet/sctp_bsd_addr.h>
5655714Skris#include <netinet/sctp_crc32.h>
5755714Skris#include <netinet/udp.h>
5855714Skris
5955714Skris#ifdef IPSEC
6055714Skris#include <netipsec/ipsec.h>
6155714Skris#ifdef INET6
6255714Skris#include <netipsec/ipsec6.h>
6355714Skris#endif				/* INET6 */
6455714Skris#endif				/* IPSEC */
6568651Skris
6668651Skrisextern struct protosw inetsw[];
67215697Ssimon
68280304Sjkimint
69280304Sjkimsctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
70280304Sjkim{
7168651Skris	struct mbuf *m;
7255714Skris	int iphlen;
7355714Skris	uint32_t vrf_id;
74280304Sjkim	uint8_t ecn_bits;
75280304Sjkim	struct sockaddr_in6 src, dst;
76280304Sjkim	struct ip6_hdr *ip6;
77280304Sjkim	struct sctphdr *sh;
78280304Sjkim	struct sctp_chunkhdr *ch;
79280304Sjkim	int length, offset;
8055714Skris
81280304Sjkim#if !defined(SCTP_WITH_NO_CSUM)
82280304Sjkim	uint8_t compute_crc;
83280304Sjkim
84280304Sjkim#endif
85280304Sjkim	uint32_t mflowid;
86280304Sjkim	uint8_t mflowtype;
87280304Sjkim	uint16_t fibnum;
88280304Sjkim
89280304Sjkim	iphlen = *offp;
90280304Sjkim	if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
91280304Sjkim		SCTP_RELEASE_PKT(*i_pak);
92280304Sjkim		return (IPPROTO_DONE);
93280304Sjkim	}
94280304Sjkim	m = SCTP_HEADER_TO_CHAIN(*i_pak);
95280304Sjkim#ifdef SCTP_MBUF_LOGGING
9655714Skris	/* Log in any input mbufs */
97280304Sjkim	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
98280304Sjkim		sctp_log_mbc(m, SCTP_MBUF_INPUT);
99280304Sjkim	}
100280304Sjkim#endif
101280304Sjkim#ifdef SCTP_PACKET_LOGGING
102280304Sjkim	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
103280304Sjkim		sctp_packet_log(m);
104280304Sjkim	}
105280304Sjkim#endif
106280304Sjkim	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
107280304Sjkim	    "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
10855714Skris	    m->m_pkthdr.len,
10955714Skris	    if_name(m->m_pkthdr.rcvif),
110280304Sjkim	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
111280304Sjkim	mflowid = m->m_pkthdr.flowid;
112280304Sjkim	mflowtype = M_HASHTYPE_GET(m);
11355714Skris	fibnum = M_GETFIB(m);
11455714Skris	SCTP_STAT_INCR(sctps_recvpackets);
115280304Sjkim	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
116280304Sjkim	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
11755714Skris	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
118280304Sjkim	ip6 = mtod(m, struct ip6_hdr *);
119280304Sjkim	IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
120280304Sjkim	    (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
12155714Skris	if (sh == NULL) {
122280304Sjkim		SCTP_STAT_INCR(sctps_hdrops);
123280304Sjkim		return (IPPROTO_DONE);
124280304Sjkim	}
125280304Sjkim	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
126280304Sjkim	offset -= sizeof(struct sctp_chunkhdr);
127280304Sjkim	memset(&src, 0, sizeof(struct sockaddr_in6));
128280304Sjkim	src.sin6_family = AF_INET6;
12955714Skris	src.sin6_len = sizeof(struct sockaddr_in6);
130280304Sjkim	src.sin6_port = sh->src_port;
131280304Sjkim	src.sin6_addr = ip6->ip6_src;
132280304Sjkim	if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
133280304Sjkim		goto out;
134280304Sjkim	}
135280304Sjkim	memset(&dst, 0, sizeof(struct sockaddr_in6));
13655714Skris	dst.sin6_family = AF_INET6;
13755714Skris	dst.sin6_len = sizeof(struct sockaddr_in6);
138280304Sjkim	dst.sin6_port = sh->dest_port;
139280304Sjkim	dst.sin6_addr = ip6->ip6_dst;
140280304Sjkim	if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
141280304Sjkim		goto out;
142280304Sjkim	}
143280304Sjkim	if (faithprefix_p != NULL && (*faithprefix_p) (&dst.sin6_addr)) {
144280304Sjkim		/* XXX send icmp6 host/port unreach? */
145280304Sjkim		goto out;
146280304Sjkim	}
147280304Sjkim	length = ntohs(ip6->ip6_plen) + iphlen;
14855714Skris	/* Validate mbuf chain length with IP payload length. */
149280304Sjkim	if (SCTP_HEADER_LEN(m) != length) {
150280304Sjkim		SCTPDBG(SCTP_DEBUG_INPUT1,
151280304Sjkim		    "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
152280304Sjkim		SCTP_STAT_INCR(sctps_hdrops);
15355714Skris		goto out;
154280304Sjkim	}
155280304Sjkim	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
156280304Sjkim		goto out;
15755714Skris	}
158280304Sjkim	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
159280304Sjkim#if defined(SCTP_WITH_NO_CSUM)
16055714Skris	SCTP_STAT_INCR(sctps_recvnocrc);
161280304Sjkim#else
162215697Ssimon	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
163280304Sjkim		SCTP_STAT_INCR(sctps_recvhwcrc);
164280304Sjkim		compute_crc = 0;
165280304Sjkim	} else {
166280304Sjkim		SCTP_STAT_INCR(sctps_recvswcrc);
167280304Sjkim		compute_crc = 1;
168280304Sjkim	}
169280304Sjkim#endif
17055714Skris	sctp_common_input_processing(&m, iphlen, offset, length,
171280304Sjkim	    (struct sockaddr *)&src,
172280304Sjkim	    (struct sockaddr *)&dst,
173280304Sjkim	    sh, ch,
174280304Sjkim#if !defined(SCTP_WITH_NO_CSUM)
175280304Sjkim	    compute_crc,
176280304Sjkim#endif
177280304Sjkim	    ecn_bits,
178280304Sjkim	    mflowtype, mflowid, fibnum,
179280304Sjkim	    vrf_id, port);
180280304Sjkimout:
181280304Sjkim	if (m) {
182280304Sjkim		sctp_m_freem(m);
183280304Sjkim	}
184280304Sjkim	return (IPPROTO_DONE);
185280304Sjkim}
186280304Sjkim
187280304Sjkim
18855714Skrisint
189280304Sjkimsctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
190280304Sjkim{
191280304Sjkim	return (sctp6_input_with_port(i_pak, offp, 0));
192280304Sjkim}
19355714Skris
194280304Sjkimstatic void
195280304Sjkimsctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
196280304Sjkim    struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
197280304Sjkim{
198120631Snectar	uint32_t nxtsz;
199280304Sjkim
200280304Sjkim	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
20155714Skris	    (icmp6 == NULL) || (sh == NULL)) {
202280304Sjkim		goto out;
203280304Sjkim	}
20455714Skris	/* First do we even look at it? */
205280304Sjkim	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
206280304Sjkim		goto out;
207280304Sjkim
208280304Sjkim	if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
209280304Sjkim		/* not PACKET TO BIG */
210280304Sjkim		goto out;
211280304Sjkim	}
212280304Sjkim	/*
213280304Sjkim	 * ok we need to look closely. We could even get smarter and look at
214280304Sjkim	 * anyone that we sent to in case we get a different ICMP that tells
215280304Sjkim	 * us there is no way to reach a host, but for this impl, all we
216280304Sjkim	 * care about is MTU discovery.
217280304Sjkim	 */
218280304Sjkim	nxtsz = ntohl(icmp6->icmp6_mtu);
219280304Sjkim	/* Stop any PMTU timer */
220280304Sjkim	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL,
22155714Skris	    SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
222280304Sjkim
223280304Sjkim	/* Adjust destination size limit */
224280304Sjkim	if (net->mtu > nxtsz) {
225280304Sjkim		net->mtu = nxtsz;
226280304Sjkim		if (net->port) {
227280304Sjkim			net->mtu -= sizeof(struct udphdr);
228280304Sjkim		}
229280304Sjkim	}
230280304Sjkim	/* now what about the ep? */
231280304Sjkim	if (stcb->asoc.smallest_mtu > nxtsz) {
232280304Sjkim		struct sctp_tmit_chunk *chk;
233280304Sjkim
234280304Sjkim		/* Adjust that too */
23555714Skris		stcb->asoc.smallest_mtu = nxtsz;
236280304Sjkim		/* now off to subtract IP_DF flag if needed */
237280304Sjkim
238280304Sjkim		TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
239280304Sjkim			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
240280304Sjkim				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
241280304Sjkim			}
242280304Sjkim		}
243280304Sjkim		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
244280304Sjkim			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
245280304Sjkim				/*
24655714Skris				 * For this guy we also mark for immediate
247280304Sjkim				 * resend since we sent to big of chunk
248280304Sjkim				 */
249280304Sjkim				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
250280304Sjkim				if (chk->sent != SCTP_DATAGRAM_RESEND)
251280304Sjkim					stcb->asoc.sent_queue_retran_cnt++;
252280304Sjkim				chk->sent = SCTP_DATAGRAM_RESEND;
253280304Sjkim				chk->rec.data.doing_fast_retransmit = 0;
254280304Sjkim
255280304Sjkim				chk->sent = SCTP_DATAGRAM_RESEND;
256280304Sjkim				/* Clear any time so NO RTT is being done */
257280304Sjkim				chk->sent_rcv_time.tv_sec = 0;
258280304Sjkim				chk->sent_rcv_time.tv_usec = 0;
259280304Sjkim				stcb->asoc.total_flight -= chk->send_size;
260280304Sjkim				net->flight_size -= chk->send_size;
261280304Sjkim			}
262280304Sjkim		}
263280304Sjkim	}
264280304Sjkim	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
265280304Sjkimout:
26655714Skris	if (stcb) {
267280304Sjkim		SCTP_TCB_UNLOCK(stcb);
268280304Sjkim	}
269280304Sjkim}
270280304Sjkim
271280304Sjkim
272280304Sjkimvoid
273280304Sjkimsctp6_notify(struct sctp_inpcb *inp,
274280304Sjkim    struct icmp6_hdr *icmph,
275280304Sjkim    struct sctphdr *sh,
276280304Sjkim    struct sockaddr *to,
277280304Sjkim    struct sctp_tcb *stcb,
278280304Sjkim    struct sctp_nets *net)
279280304Sjkim{
280280304Sjkim#if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
281280304Sjkim	struct socket *so;
282280304Sjkim
283280304Sjkim#endif
284280304Sjkim
285280304Sjkim	/* protection */
286280304Sjkim	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
287280304Sjkim	    (sh == NULL) || (to == NULL)) {
288280304Sjkim		if (stcb)
289280304Sjkim			SCTP_TCB_UNLOCK(stcb);
290280304Sjkim		return;
291280304Sjkim	}
292280304Sjkim	/* First job is to verify the vtag matches what I would send */
293280304Sjkim	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
294280304Sjkim		SCTP_TCB_UNLOCK(stcb);
295280304Sjkim		return;
296280304Sjkim	}
29755714Skris	if (icmph->icmp6_type != ICMP_UNREACH) {
298280304Sjkim		/* We only care about unreachable */
299280304Sjkim		SCTP_TCB_UNLOCK(stcb);
30055714Skris		return;
301215697Ssimon	}
302280304Sjkim	if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
303215697Ssimon	    (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
304280304Sjkim	    (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
305215697Ssimon	    (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
306280304Sjkim	    (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
307280304Sjkim	    (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
308280304Sjkim	    (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
309280304Sjkim	    (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
310280304Sjkim
311280304Sjkim		/*
312280304Sjkim		 * Hmm reachablity problems we must examine closely. If its
313280304Sjkim		 * not reachable, we may have lost a network. Or if there is
314280304Sjkim		 * NO protocol at the other end named SCTP. well we consider
315280304Sjkim		 * it a OOTB abort.
316280304Sjkim		 */
317280304Sjkim		if (net->dest_state & SCTP_ADDR_REACHABLE) {
318280304Sjkim			/* Ok that destination is NOT reachable */
319280304Sjkim			net->dest_state &= ~SCTP_ADDR_REACHABLE;
320280304Sjkim			net->dest_state &= ~SCTP_ADDR_PF;
321280304Sjkim			sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
322280304Sjkim			    stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
323280304Sjkim		}
324280304Sjkim		SCTP_TCB_UNLOCK(stcb);
325280304Sjkim	} else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
326280304Sjkim	    (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
327280304Sjkim		/*
328280304Sjkim		 * Here the peer is either playing tricks on us, including
329280304Sjkim		 * an address that belongs to someone who does not support
330280304Sjkim		 * SCTP OR was a userland implementation that shutdown and
331280304Sjkim		 * now is dead. In either case treat it like a OOTB abort
332280304Sjkim		 * with no TCB
333280304Sjkim		 */
334280304Sjkim		sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
335280304Sjkim#if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
336280304Sjkim		so = SCTP_INP_SO(inp);
337280304Sjkim		atomic_add_int(&stcb->asoc.refcnt, 1);
338280304Sjkim		SCTP_TCB_UNLOCK(stcb);
33955714Skris		SCTP_SOCKET_LOCK(so, 1);
340280304Sjkim		SCTP_TCB_LOCK(stcb);
341280304Sjkim		atomic_subtract_int(&stcb->asoc.refcnt, 1);
342280304Sjkim#endif
343280304Sjkim		(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
34455714Skris		    SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_2);
345280304Sjkim#if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
346280304Sjkim		SCTP_SOCKET_UNLOCK(so, 1);
347280304Sjkim		/* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
348280304Sjkim#endif
349280304Sjkim		/* no need to unlock here, since the TCB is gone */
350280304Sjkim	} else {
351280304Sjkim		SCTP_TCB_UNLOCK(stcb);
352280304Sjkim	}
353280304Sjkim}
354280304Sjkim
355280304Sjkim
356280304Sjkim
357280304Sjkimvoid
358280304Sjkimsctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
35955714Skris{
36068651Skris	struct sctphdr sh;
361280304Sjkim	struct ip6ctlparam *ip6cp = NULL;
362280304Sjkim	uint32_t vrf_id;
363280304Sjkim
364280304Sjkim	vrf_id = SCTP_DEFAULT_VRFID;
365280304Sjkim
36655714Skris	if (pktdst->sa_family != AF_INET6 ||
367280304Sjkim	    pktdst->sa_len != sizeof(struct sockaddr_in6))
368280304Sjkim		return;
36955714Skris
370280304Sjkim	if ((unsigned)cmd >= PRC_NCMDS)
371280304Sjkim		return;
372280304Sjkim	if (PRC_IS_REDIRECT(cmd)) {
373280304Sjkim		d = NULL;
374280304Sjkim	} else if (inet6ctlerrmap[cmd] == 0) {
375280304Sjkim		return;
376280304Sjkim	}
37755714Skris	/* if the parameter is from icmp6, decode it. */
378280304Sjkim	if (d != NULL) {
379280304Sjkim		ip6cp = (struct ip6ctlparam *)d;
380280304Sjkim	} else {
381280304Sjkim		ip6cp = (struct ip6ctlparam *)NULL;
382280304Sjkim	}
383280304Sjkim
384280304Sjkim	if (ip6cp) {
385280304Sjkim		/*
386280304Sjkim		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
387280304Sjkim		 * valid.
388280304Sjkim		 */
389280304Sjkim		struct sctp_inpcb *inp = NULL;
390280304Sjkim		struct sctp_tcb *stcb = NULL;
391280304Sjkim		struct sctp_nets *net = NULL;
392280304Sjkim		struct sockaddr_in6 final;
393280304Sjkim
394280304Sjkim		if (ip6cp->ip6c_m == NULL)
395280304Sjkim			return;
396280304Sjkim
39755714Skris		/* Check if we can safely examine the SCTP header. */
398280304Sjkim		if (ip6cp->ip6c_m->m_pkthdr.len < ip6cp->ip6c_off + sizeof(sh))
399280304Sjkim			return;
40055714Skris
401280304Sjkim		bzero(&sh, sizeof(sh));
402280304Sjkim		bzero(&final, sizeof(final));
40355714Skris		inp = NULL;
404280304Sjkim		net = NULL;
405280304Sjkim		m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
406280304Sjkim		    (caddr_t)&sh);
407280304Sjkim		ip6cp->ip6c_src->sin6_port = sh.src_port;
408280304Sjkim		final.sin6_len = sizeof(final);
409280304Sjkim		final.sin6_family = AF_INET6;
410280304Sjkim		final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
411280304Sjkim		final.sin6_port = sh.dest_port;
412280304Sjkim		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&final,
413280304Sjkim		    (struct sockaddr *)ip6cp->ip6c_src,
414280304Sjkim		    &inp, &net, 1, vrf_id);
415280304Sjkim		/* inp's ref-count increased && stcb locked */
416280304Sjkim		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
417280304Sjkim			if (cmd == PRC_MSGSIZE) {
418280304Sjkim				sctp6_notify_mbuf(inp,
419280304Sjkim				    ip6cp->ip6c_icmp6,
420280304Sjkim				    &sh,
421280304Sjkim				    stcb,
422280304Sjkim				    net);
423280304Sjkim				/* inp's ref-count reduced && stcb unlocked */
424280304Sjkim			} else {
425280304Sjkim				sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
426280304Sjkim				    (struct sockaddr *)&final,
427280304Sjkim				    stcb, net);
428280304Sjkim				/* inp's ref-count reduced && stcb unlocked */
429280304Sjkim			}
430280304Sjkim		} else {
431280304Sjkim			if (PRC_IS_REDIRECT(cmd) && inp) {
432280304Sjkim				in6_rtchange((struct in6pcb *)inp,
433280304Sjkim				    inet6ctlerrmap[cmd]);
434280304Sjkim			}
435280304Sjkim			if (inp) {
436280304Sjkim				/* reduce inp's ref-count */
437280304Sjkim				SCTP_INP_WLOCK(inp);
438280304Sjkim				SCTP_INP_DECR_REF(inp);
439280304Sjkim				SCTP_INP_WUNLOCK(inp);
440280304Sjkim			}
441280304Sjkim			if (stcb)
442280304Sjkim				SCTP_TCB_UNLOCK(stcb);
443280304Sjkim		}
444280304Sjkim	}
445280304Sjkim}
446280304Sjkim
447280304Sjkim/*
448280304Sjkim * this routine can probably be collasped into the one in sctp_userreq.c
449280304Sjkim * since they do the same thing and now we lookup with a sockaddr
450280304Sjkim */
451280304Sjkimstatic int
452280304Sjkimsctp6_getcred(SYSCTL_HANDLER_ARGS)
45355714Skris{
454280304Sjkim	struct xucred xuc;
455280304Sjkim	struct sockaddr_in6 addrs[2];
456280304Sjkim	struct sctp_inpcb *inp;
457280304Sjkim	struct sctp_nets *net;
458280304Sjkim	struct sctp_tcb *stcb;
459280304Sjkim	int error;
460280304Sjkim	uint32_t vrf_id;
461280304Sjkim
462280304Sjkim	vrf_id = SCTP_DEFAULT_VRFID;
463280304Sjkim
464280304Sjkim	error = priv_check(req->td, PRIV_NETINET_GETCRED);
465280304Sjkim	if (error)
466280304Sjkim		return (error);
467280304Sjkim
468280304Sjkim	if (req->newlen != sizeof(addrs)) {
469280304Sjkim		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
470280304Sjkim		return (EINVAL);
471280304Sjkim	}
472280304Sjkim	if (req->oldlen != sizeof(struct ucred)) {
47355714Skris		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
47468651Skris		return (EINVAL);
475280304Sjkim	}
476280304Sjkim	error = SYSCTL_IN(req, addrs, sizeof(addrs));
477280304Sjkim	if (error)
478280304Sjkim		return (error);
47955714Skris
480280304Sjkim	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
48155714Skris	    sin6tosa(&addrs[0]),
482280304Sjkim	    &inp, &net, 1, vrf_id);
483280304Sjkim	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
484280304Sjkim		if ((inp != NULL) && (stcb == NULL)) {
485280304Sjkim			/* reduce ref-count */
486280304Sjkim			SCTP_INP_WLOCK(inp);
487280304Sjkim			SCTP_INP_DECR_REF(inp);
488280304Sjkim			goto cred_can_cont;
489280304Sjkim		}
490280304Sjkim		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
491280304Sjkim		error = ENOENT;
492280304Sjkim		goto out;
493280304Sjkim	}
494280304Sjkim	SCTP_TCB_UNLOCK(stcb);
495280304Sjkim	/*
496280304Sjkim	 * We use the write lock here, only since in the error leg we need
497280304Sjkim	 * it. If we used RLOCK, then we would have to
498280304Sjkim	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
499280304Sjkim	 * Better to use higher wlock.
500280304Sjkim	 */
501280304Sjkim	SCTP_INP_WLOCK(inp);
502280304Sjkimcred_can_cont:
503280304Sjkim	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
504280304Sjkim	if (error) {
505280304Sjkim		SCTP_INP_WUNLOCK(inp);
506280304Sjkim		goto out;
507280304Sjkim	}
508280304Sjkim	cru2x(inp->sctp_socket->so_cred, &xuc);
509280304Sjkim	SCTP_INP_WUNLOCK(inp);
510280304Sjkim	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
511280304Sjkimout:
512280304Sjkim	return (error);
513280304Sjkim}
514280304Sjkim
515280304SjkimSYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
516280304Sjkim    0, 0,
517280304Sjkim    sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
518280304Sjkim
519280304Sjkim
520280304Sjkim/* This is the same as the sctp_abort() could be made common */
521280304Sjkimstatic void
522280304Sjkimsctp6_abort(struct socket *so)
523280304Sjkim{
524280304Sjkim	struct sctp_inpcb *inp;
525280304Sjkim	uint32_t flags;
526280304Sjkim
527280304Sjkim	inp = (struct sctp_inpcb *)so->so_pcb;
528280304Sjkim	if (inp == NULL) {
529280304Sjkim		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
530280304Sjkim		return;
531280304Sjkim	}
532280304Sjkimsctp_must_try_again:
533280304Sjkim	flags = inp->sctp_flags;
534280304Sjkim#ifdef SCTP_LOG_CLOSING
535280304Sjkim	sctp_log_closing(inp, NULL, 17);
536280304Sjkim#endif
53755714Skris	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
538280304Sjkim	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
539280304Sjkim#ifdef SCTP_LOG_CLOSING
540280304Sjkim		sctp_log_closing(inp, NULL, 16);
541280304Sjkim#endif
542280304Sjkim		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
54355714Skris		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
544280304Sjkim		SOCK_LOCK(so);
545280304Sjkim		SCTP_SB_CLEAR(so->so_snd);
546280304Sjkim		/*
547280304Sjkim		 * same for the rcv ones, they are only here for the
548280304Sjkim		 * accounting/select.
549280304Sjkim		 */
550280304Sjkim		SCTP_SB_CLEAR(so->so_rcv);
551280304Sjkim		/* Now null out the reference, we are completely detached. */
552280304Sjkim		so->so_pcb = NULL;
553280304Sjkim		SOCK_UNLOCK(so);
554280304Sjkim	} else {
55555714Skris		flags = inp->sctp_flags;
55668651Skris		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
557280304Sjkim			goto sctp_must_try_again;
558280304Sjkim		}
55959191Skris	}
560280304Sjkim	return;
561280304Sjkim}
562280304Sjkim
563280304Sjkimstatic int
564280304Sjkimsctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
565280304Sjkim{
566280304Sjkim	struct in6pcb *inp6;
567280304Sjkim	int error;
568280304Sjkim	struct sctp_inpcb *inp;
56959191Skris	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
570215697Ssimon
571280304Sjkim	inp = (struct sctp_inpcb *)so->so_pcb;
572280304Sjkim	if (inp != NULL) {
573280304Sjkim		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
574		return (EINVAL);
575	}
576	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
577		error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
578		if (error)
579			return (error);
580	}
581	error = sctp_inpcb_alloc(so, vrf_id);
582	if (error)
583		return (error);
584	inp = (struct sctp_inpcb *)so->so_pcb;
585	SCTP_INP_WLOCK(inp);
586	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
587	inp6 = (struct in6pcb *)inp;
588
589	inp6->inp_vflag |= INP_IPV6;
590	inp6->in6p_hops = -1;	/* use kernel default */
591	inp6->in6p_cksum = -1;	/* just to be sure */
592#ifdef INET
593	/*
594	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
595	 * socket as well, because the socket may be bound to an IPv6
596	 * wildcard address, which may match an IPv4-mapped IPv6 address.
597	 */
598	inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
599#endif
600	/*
601	 * Hmm what about the IPSEC stuff that is missing here but in
602	 * sctp_attach()?
603	 */
604	SCTP_INP_WUNLOCK(inp);
605	return (0);
606}
607
608static int
609sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
610{
611	struct sctp_inpcb *inp;
612	struct in6pcb *inp6;
613	int error;
614
615	inp = (struct sctp_inpcb *)so->so_pcb;
616	if (inp == NULL) {
617		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
618		return (EINVAL);
619	}
620	if (addr) {
621		switch (addr->sa_family) {
622#ifdef INET
623		case AF_INET:
624			if (addr->sa_len != sizeof(struct sockaddr_in)) {
625				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
626				return (EINVAL);
627			}
628			break;
629#endif
630#ifdef INET6
631		case AF_INET6:
632			if (addr->sa_len != sizeof(struct sockaddr_in6)) {
633				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
634				return (EINVAL);
635			}
636			break;
637#endif
638		default:
639			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
640			return (EINVAL);
641		}
642	}
643	inp6 = (struct in6pcb *)inp;
644	inp6->inp_vflag &= ~INP_IPV4;
645	inp6->inp_vflag |= INP_IPV6;
646	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
647		switch (addr->sa_family) {
648#ifdef INET
649		case AF_INET:
650			/* binding v4 addr to v6 socket, so reset flags */
651			inp6->inp_vflag |= INP_IPV4;
652			inp6->inp_vflag &= ~INP_IPV6;
653			break;
654#endif
655#ifdef INET6
656		case AF_INET6:
657			{
658				struct sockaddr_in6 *sin6_p;
659
660				sin6_p = (struct sockaddr_in6 *)addr;
661
662				if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
663					inp6->inp_vflag |= INP_IPV4;
664				}
665#ifdef INET
666				if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
667					struct sockaddr_in sin;
668
669					in6_sin6_2_sin(&sin, sin6_p);
670					inp6->inp_vflag |= INP_IPV4;
671					inp6->inp_vflag &= ~INP_IPV6;
672					error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
673					return (error);
674				}
675#endif
676				break;
677			}
678#endif
679		default:
680			break;
681		}
682	} else if (addr != NULL) {
683		struct sockaddr_in6 *sin6_p;
684
685		/* IPV6_V6ONLY socket */
686#ifdef INET
687		if (addr->sa_family == AF_INET) {
688			/* can't bind v4 addr to v6 only socket! */
689			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
690			return (EINVAL);
691		}
692#endif
693		sin6_p = (struct sockaddr_in6 *)addr;
694
695		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
696			/* can't bind v4-mapped addrs either! */
697			/* NOTE: we don't support SIIT */
698			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
699			return (EINVAL);
700		}
701	}
702	error = sctp_inpcb_bind(so, addr, NULL, p);
703	return (error);
704}
705
706
707static void
708sctp6_close(struct socket *so)
709{
710	sctp_close(so);
711}
712
713/* This could be made common with sctp_detach() since they are identical */
714
715static
716int
717sctp6_disconnect(struct socket *so)
718{
719	return (sctp_disconnect(so));
720}
721
722
723int
724sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
725    struct mbuf *control, struct thread *p);
726
727
728static int
729sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
730    struct mbuf *control, struct thread *p)
731{
732	struct sctp_inpcb *inp;
733	struct in6pcb *inp6;
734
735#ifdef INET
736	struct sockaddr_in6 *sin6;
737
738#endif				/* INET */
739	/* No SPL needed since sctp_output does this */
740
741	inp = (struct sctp_inpcb *)so->so_pcb;
742	if (inp == NULL) {
743		if (control) {
744			SCTP_RELEASE_PKT(control);
745			control = NULL;
746		}
747		SCTP_RELEASE_PKT(m);
748		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
749		return (EINVAL);
750	}
751	inp6 = (struct in6pcb *)inp;
752	/*
753	 * For the TCP model we may get a NULL addr, if we are a connected
754	 * socket thats ok.
755	 */
756	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
757	    (addr == NULL)) {
758		goto connected_type;
759	}
760	if (addr == NULL) {
761		SCTP_RELEASE_PKT(m);
762		if (control) {
763			SCTP_RELEASE_PKT(control);
764			control = NULL;
765		}
766		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
767		return (EDESTADDRREQ);
768	}
769#ifdef INET
770	sin6 = (struct sockaddr_in6 *)addr;
771	if (SCTP_IPV6_V6ONLY(inp6)) {
772		/*
773		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
774		 * v4 addr or v4-mapped addr
775		 */
776		if (addr->sa_family == AF_INET) {
777			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
778			return (EINVAL);
779		}
780		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
781			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
782			return (EINVAL);
783		}
784	}
785	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
786		struct sockaddr_in sin;
787
788		/* convert v4-mapped into v4 addr and send */
789		in6_sin6_2_sin(&sin, sin6);
790		return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
791	}
792#endif				/* INET */
793connected_type:
794	/* now what about control */
795	if (control) {
796		if (inp->control) {
797			SCTP_PRINTF("huh? control set?\n");
798			SCTP_RELEASE_PKT(inp->control);
799			inp->control = NULL;
800		}
801		inp->control = control;
802	}
803	/* Place the data */
804	if (inp->pkt) {
805		SCTP_BUF_NEXT(inp->pkt_last) = m;
806		inp->pkt_last = m;
807	} else {
808		inp->pkt_last = inp->pkt = m;
809	}
810	if (
811	/* FreeBSD and MacOSX uses a flag passed */
812	    ((flags & PRUS_MORETOCOME) == 0)
813	    ) {
814		/*
815		 * note with the current version this code will only be used
816		 * by OpenBSD, NetBSD and FreeBSD have methods for
817		 * re-defining sosend() to use sctp_sosend().  One can
818		 * optionaly switch back to this code (by changing back the
819		 * defininitions but this is not advisable.
820		 */
821		int ret;
822
823		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
824		inp->pkt = NULL;
825		inp->control = NULL;
826		return (ret);
827	} else {
828		return (0);
829	}
830}
831
832static int
833sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
834{
835	uint32_t vrf_id;
836	int error = 0;
837	struct sctp_inpcb *inp;
838	struct sctp_tcb *stcb;
839
840#ifdef INET
841	struct in6pcb *inp6;
842	struct sockaddr_in6 *sin6;
843	union sctp_sockstore store;
844
845#endif
846
847#ifdef INET
848	inp6 = (struct in6pcb *)so->so_pcb;
849#endif
850	inp = (struct sctp_inpcb *)so->so_pcb;
851	if (inp == NULL) {
852		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
853		return (ECONNRESET);	/* I made the same as TCP since we are
854					 * not setup? */
855	}
856	if (addr == NULL) {
857		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
858		return (EINVAL);
859	}
860	switch (addr->sa_family) {
861#ifdef INET
862	case AF_INET:
863		if (addr->sa_len != sizeof(struct sockaddr_in)) {
864			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
865			return (EINVAL);
866		}
867		break;
868#endif
869#ifdef INET6
870	case AF_INET6:
871		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
872			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
873			return (EINVAL);
874		}
875		break;
876#endif
877	default:
878		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
879		return (EINVAL);
880	}
881
882	vrf_id = inp->def_vrf_id;
883	SCTP_ASOC_CREATE_LOCK(inp);
884	SCTP_INP_RLOCK(inp);
885	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
886	    SCTP_PCB_FLAGS_UNBOUND) {
887		/* Bind a ephemeral port */
888		SCTP_INP_RUNLOCK(inp);
889		error = sctp6_bind(so, NULL, p);
890		if (error) {
891			SCTP_ASOC_CREATE_UNLOCK(inp);
892
893			return (error);
894		}
895		SCTP_INP_RLOCK(inp);
896	}
897	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
898	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
899		/* We are already connected AND the TCP model */
900		SCTP_INP_RUNLOCK(inp);
901		SCTP_ASOC_CREATE_UNLOCK(inp);
902		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
903		return (EADDRINUSE);
904	}
905#ifdef INET
906	sin6 = (struct sockaddr_in6 *)addr;
907	if (SCTP_IPV6_V6ONLY(inp6)) {
908		/*
909		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
910		 * addr or v4-mapped addr
911		 */
912		if (addr->sa_family == AF_INET) {
913			SCTP_INP_RUNLOCK(inp);
914			SCTP_ASOC_CREATE_UNLOCK(inp);
915			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
916			return (EINVAL);
917		}
918		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
919			SCTP_INP_RUNLOCK(inp);
920			SCTP_ASOC_CREATE_UNLOCK(inp);
921			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
922			return (EINVAL);
923		}
924	}
925	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
926		/* convert v4-mapped into v4 addr */
927		in6_sin6_2_sin(&store.sin, sin6);
928		addr = &store.sa;
929	}
930#endif				/* INET */
931	/* Now do we connect? */
932	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
933		stcb = LIST_FIRST(&inp->sctp_asoc_list);
934		if (stcb) {
935			SCTP_TCB_UNLOCK(stcb);
936		}
937		SCTP_INP_RUNLOCK(inp);
938	} else {
939		SCTP_INP_RUNLOCK(inp);
940		SCTP_INP_WLOCK(inp);
941		SCTP_INP_INCR_REF(inp);
942		SCTP_INP_WUNLOCK(inp);
943		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
944		if (stcb == NULL) {
945			SCTP_INP_WLOCK(inp);
946			SCTP_INP_DECR_REF(inp);
947			SCTP_INP_WUNLOCK(inp);
948		}
949	}
950
951	if (stcb != NULL) {
952		/* Already have or am bring up an association */
953		SCTP_ASOC_CREATE_UNLOCK(inp);
954		SCTP_TCB_UNLOCK(stcb);
955		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
956		return (EALREADY);
957	}
958	/* We are GOOD to go */
959	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
960	SCTP_ASOC_CREATE_UNLOCK(inp);
961	if (stcb == NULL) {
962		/* Gak! no memory */
963		return (error);
964	}
965	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
966		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
967		/* Set the connected flag so we can queue data */
968		soisconnecting(so);
969	}
970	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
971	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
972
973	/* initialize authentication parameters for the assoc */
974	sctp_initialize_auth_params(inp, stcb);
975
976	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
977	SCTP_TCB_UNLOCK(stcb);
978	return (error);
979}
980
981static int
982sctp6_getaddr(struct socket *so, struct sockaddr **addr)
983{
984	struct sockaddr_in6 *sin6;
985	struct sctp_inpcb *inp;
986	uint32_t vrf_id;
987	struct sctp_ifa *sctp_ifa;
988
989	int error;
990
991	/*
992	 * Do the malloc first in case it blocks.
993	 */
994	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
995	if (sin6 == NULL)
996		return (ENOMEM);
997	sin6->sin6_family = AF_INET6;
998	sin6->sin6_len = sizeof(*sin6);
999
1000	inp = (struct sctp_inpcb *)so->so_pcb;
1001	if (inp == NULL) {
1002		SCTP_FREE_SONAME(sin6);
1003		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1004		return (ECONNRESET);
1005	}
1006	SCTP_INP_RLOCK(inp);
1007	sin6->sin6_port = inp->sctp_lport;
1008	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1009		/* For the bound all case you get back 0 */
1010		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1011			struct sctp_tcb *stcb;
1012			struct sockaddr_in6 *sin_a6;
1013			struct sctp_nets *net;
1014			int fnd;
1015
1016			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1017			if (stcb == NULL) {
1018				goto notConn6;
1019			}
1020			fnd = 0;
1021			sin_a6 = NULL;
1022			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1023				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1024				if (sin_a6 == NULL)
1025					/* this will make coverity happy */
1026					continue;
1027
1028				if (sin_a6->sin6_family == AF_INET6) {
1029					fnd = 1;
1030					break;
1031				}
1032			}
1033			if ((!fnd) || (sin_a6 == NULL)) {
1034				/* punt */
1035				goto notConn6;
1036			}
1037			vrf_id = inp->def_vrf_id;
1038			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1039			if (sctp_ifa) {
1040				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1041			}
1042		} else {
1043			/* For the bound all case you get back 0 */
1044	notConn6:
1045			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1046		}
1047	} else {
1048		/* Take the first IPv6 address in the list */
1049		struct sctp_laddr *laddr;
1050		int fnd = 0;
1051
1052		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1053			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1054				struct sockaddr_in6 *sin_a;
1055
1056				sin_a = &laddr->ifa->address.sin6;
1057				sin6->sin6_addr = sin_a->sin6_addr;
1058				fnd = 1;
1059				break;
1060			}
1061		}
1062		if (!fnd) {
1063			SCTP_FREE_SONAME(sin6);
1064			SCTP_INP_RUNLOCK(inp);
1065			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1066			return (ENOENT);
1067		}
1068	}
1069	SCTP_INP_RUNLOCK(inp);
1070	/* Scoping things for v6 */
1071	if ((error = sa6_recoverscope(sin6)) != 0) {
1072		SCTP_FREE_SONAME(sin6);
1073		return (error);
1074	}
1075	(*addr) = (struct sockaddr *)sin6;
1076	return (0);
1077}
1078
1079static int
1080sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1081{
1082	struct sockaddr_in6 *sin6;
1083	int fnd;
1084	struct sockaddr_in6 *sin_a6;
1085	struct sctp_inpcb *inp;
1086	struct sctp_tcb *stcb;
1087	struct sctp_nets *net;
1088	int error;
1089
1090	/* Do the malloc first in case it blocks. */
1091	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1092	if (sin6 == NULL)
1093		return (ENOMEM);
1094	sin6->sin6_family = AF_INET6;
1095	sin6->sin6_len = sizeof(*sin6);
1096
1097	inp = (struct sctp_inpcb *)so->so_pcb;
1098	if ((inp == NULL) ||
1099	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1100		/* UDP type and listeners will drop out here */
1101		SCTP_FREE_SONAME(sin6);
1102		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1103		return (ENOTCONN);
1104	}
1105	SCTP_INP_RLOCK(inp);
1106	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1107	if (stcb) {
1108		SCTP_TCB_LOCK(stcb);
1109	}
1110	SCTP_INP_RUNLOCK(inp);
1111	if (stcb == NULL) {
1112		SCTP_FREE_SONAME(sin6);
1113		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1114		return (ECONNRESET);
1115	}
1116	fnd = 0;
1117	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1118		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1119		if (sin_a6->sin6_family == AF_INET6) {
1120			fnd = 1;
1121			sin6->sin6_port = stcb->rport;
1122			sin6->sin6_addr = sin_a6->sin6_addr;
1123			break;
1124		}
1125	}
1126	SCTP_TCB_UNLOCK(stcb);
1127	if (!fnd) {
1128		/* No IPv4 address */
1129		SCTP_FREE_SONAME(sin6);
1130		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1131		return (ENOENT);
1132	}
1133	if ((error = sa6_recoverscope(sin6)) != 0) {
1134		SCTP_FREE_SONAME(sin6);
1135		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1136		return (error);
1137	}
1138	*addr = (struct sockaddr *)sin6;
1139	return (0);
1140}
1141
1142static int
1143sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1144{
1145#ifdef INET
1146	struct sockaddr *addr;
1147
1148#endif
1149	struct in6pcb *inp6 = sotoin6pcb(so);
1150	int error;
1151
1152	if (inp6 == NULL) {
1153		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1154		return (EINVAL);
1155	}
1156	/* allow v6 addresses precedence */
1157	error = sctp6_getaddr(so, nam);
1158#ifdef INET
1159	if (error) {
1160		/* try v4 next if v6 failed */
1161		error = sctp_ingetaddr(so, nam);
1162		if (error) {
1163			return (error);
1164		}
1165		addr = *nam;
1166		/* if I'm V6ONLY, convert it to v4-mapped */
1167		if (SCTP_IPV6_V6ONLY(inp6)) {
1168			struct sockaddr_in6 sin6;
1169
1170			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1171			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1172		}
1173	}
1174#endif
1175	return (error);
1176}
1177
1178
1179static int
1180sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1181{
1182#ifdef INET
1183	struct sockaddr *addr;
1184
1185#endif
1186	struct in6pcb *inp6 = sotoin6pcb(so);
1187	int error;
1188
1189	if (inp6 == NULL) {
1190		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1191		return (EINVAL);
1192	}
1193	/* allow v6 addresses precedence */
1194	error = sctp6_peeraddr(so, nam);
1195#ifdef INET
1196	if (error) {
1197		/* try v4 next if v6 failed */
1198		error = sctp_peeraddr(so, nam);
1199		if (error) {
1200			return (error);
1201		}
1202		addr = *nam;
1203		/* if I'm V6ONLY, convert it to v4-mapped */
1204		if (SCTP_IPV6_V6ONLY(inp6)) {
1205			struct sockaddr_in6 sin6;
1206
1207			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1208			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1209		}
1210	}
1211#endif
1212	return (error);
1213}
1214
1215struct pr_usrreqs sctp6_usrreqs = {
1216	.pru_abort = sctp6_abort,
1217	.pru_accept = sctp_accept,
1218	.pru_attach = sctp6_attach,
1219	.pru_bind = sctp6_bind,
1220	.pru_connect = sctp6_connect,
1221	.pru_control = in6_control,
1222	.pru_close = sctp6_close,
1223	.pru_detach = sctp6_close,
1224	.pru_sopoll = sopoll_generic,
1225	.pru_flush = sctp_flush,
1226	.pru_disconnect = sctp6_disconnect,
1227	.pru_listen = sctp_listen,
1228	.pru_peeraddr = sctp6_getpeeraddr,
1229	.pru_send = sctp6_send,
1230	.pru_shutdown = sctp_shutdown,
1231	.pru_sockaddr = sctp6_in6getaddr,
1232	.pru_sosend = sctp_sosend,
1233	.pru_soreceive = sctp_soreceive
1234};
1235
1236#endif
1237