sctp6_usrreq.c revision 172090
1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3163953Srrs *
4163953Srrs * Redistribution and use in source and binary forms, with or without
5163953Srrs * modification, are permitted provided that the following conditions are met:
6163953Srrs *
7163953Srrs * a) Redistributions of source code must retain the above copyright notice,
8163953Srrs *   this list of conditions and the following disclaimer.
9163953Srrs *
10163953Srrs * b) Redistributions in binary form must reproduce the above copyright
11163953Srrs *    notice, this list of conditions and the following disclaimer in
12163953Srrs *   the documentation and/or other materials provided with the distribution.
13163953Srrs *
14163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
15163953Srrs *    contributors may be used to endorse or promote products derived
16163953Srrs *    from this software without specific prior written permission.
17163953Srrs *
18163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
29163953Srrs */
30163953Srrs/*	$KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $	*/
31163954Srrs#include <sys/cdefs.h>
32163953Srrs__FBSDID("$FreeBSD: head/sys/netinet6/sctp6_usrreq.c 172090 2007-09-08 11:35:11Z rrs $");
33163953Srrs
34168709Srrs
35166086Srrs#include <netinet/sctp_os.h>
36163953Srrs#include <sys/proc.h>
37163953Srrs#include <netinet/sctp_pcb.h>
38163953Srrs#include <netinet/sctp_header.h>
39163953Srrs#include <netinet/sctp_var.h>
40168709Srrs#if defined(INET6)
41168709Srrs#include <netinet6/sctp6_var.h>
42168709Srrs#endif
43167598Srrs#include <netinet/sctp_sysctl.h>
44163953Srrs#include <netinet/sctp_output.h>
45166086Srrs#include <netinet/sctp_uio.h>
46163953Srrs#include <netinet/sctp_asconf.h>
47166086Srrs#include <netinet/sctputil.h>
48166086Srrs#include <netinet/sctp_indata.h>
49166086Srrs#include <netinet/sctp_timer.h>
50166086Srrs#include <netinet/sctp_auth.h>
51168709Srrs#include <netinet/sctp_input.h>
52168709Srrs#include <netinet/sctp_output.h>
53170091Srrs#include <netinet/sctp_bsd_addr.h>
54163953Srrs
55171167Sgnn#ifdef IPSEC
56171133Sgnn#include <netipsec/ipsec.h>
57171133Sgnn#if defined(INET6)
58171133Sgnn#include <netipsec/ipsec6.h>
59171440Srrs#endif				/* INET6 */
60171440Srrs#endif				/* IPSEC */
61163953Srrs
62163953Srrsextern struct protosw inetsw[];
63163953Srrs
64163953Srrsint
65171259Sdelphijsctp6_input(struct mbuf **i_pak, int *offp, int proto)
66163953Srrs{
67165647Srrs	struct mbuf *m;
68163953Srrs	struct ip6_hdr *ip6;
69163953Srrs	struct sctphdr *sh;
70163953Srrs	struct sctp_inpcb *in6p = NULL;
71163953Srrs	struct sctp_nets *net;
72163953Srrs	int refcount_up = 0;
73169352Srrs	uint32_t check, calc_check;
74170181Srrs	uint32_t vrf_id = 0;
75163953Srrs	struct inpcb *in6p_ip;
76163953Srrs	struct sctp_chunkhdr *ch;
77163953Srrs	int length, mlen, offset, iphlen;
78168299Srrs	uint8_t ecn_bits;
79163953Srrs	struct sctp_tcb *stcb = NULL;
80170056Srrs	int pkt_len = 0;
81163953Srrs	int off = *offp;
82163953Srrs
83169352Srrs	/* get the VRF and table id's */
84169352Srrs	if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
85169352Srrs		SCTP_RELEASE_PKT(*i_pak);
86169352Srrs		return (-1);
87169352Srrs	}
88168709Srrs	m = SCTP_HEADER_TO_CHAIN(*i_pak);
89170056Srrs	pkt_len = SCTP_HEADER_LEN((*i_pak));
90165647Srrs
91170091Srrs#ifdef  SCTP_PACKET_LOGGING
92170091Srrs	sctp_packet_log(m, pkt_len);
93170091Srrs#endif
94163953Srrs	ip6 = mtod(m, struct ip6_hdr *);
95163953Srrs	/* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
96170056Srrs	IP6_EXTHDR_GET(sh, struct sctphdr *, m, off,
97170056Srrs	    (int)(sizeof(*sh) + sizeof(*ch)));
98163953Srrs	if (sh == NULL) {
99163953Srrs		SCTP_STAT_INCR(sctps_hdrops);
100163953Srrs		return IPPROTO_DONE;
101163953Srrs	}
102163953Srrs	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
103163953Srrs	iphlen = off;
104163953Srrs	offset = iphlen + sizeof(*sh) + sizeof(*ch);
105170859Srrs	SCTPDBG(SCTP_DEBUG_INPUT1,
106170859Srrs	    "sctp6_input() length:%d iphlen:%d\n", pkt_len, iphlen);
107163953Srrs
108170859Srrs
109163953Srrs#if defined(NFAITH) && NFAITH > 0
110163953Srrs
111163953Srrs	if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) {
112163953Srrs		/* XXX send icmp6 host/port unreach? */
113163953Srrs		goto bad;
114163953Srrs	}
115163953Srrs#endif				/* NFAITH defined and > 0 */
116163953Srrs	SCTP_STAT_INCR(sctps_recvpackets);
117163953Srrs	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
118169420Srrs	SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n",
119170056Srrs	    iphlen, pkt_len);
120163953Srrs	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
121163953Srrs		/* No multi-cast support in SCTP */
122163953Srrs		goto bad;
123163953Srrs	}
124163953Srrs	/* destination port of 0 is illegal, based on RFC2960. */
125163953Srrs	if (sh->dest_port == 0)
126163953Srrs		goto bad;
127171990Srrs	check = sh->checksum;	/* save incoming checksum */
128171990Srrs	if ((check == 0) && (sctp_no_csum_on_loopback) &&
129171990Srrs	    (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))) {
130171990Srrs		goto sctp_skip_csum;
131171990Srrs	}
132171990Srrs	sh->checksum = 0;	/* prepare for calc */
133171990Srrs	calc_check = sctp_calculate_sum(m, &mlen, iphlen);
134171990Srrs	if (calc_check != check) {
135171990Srrs		SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
136171990Srrs		    calc_check, check, m, mlen, iphlen);
137171990Srrs		stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
138171990Srrs		    sh, ch, &in6p, &net, vrf_id);
139171990Srrs		/* in6p's ref-count increased && stcb locked */
140171990Srrs		if ((in6p) && (stcb)) {
141171990Srrs			sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
142172090Srrs			sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
143171990Srrs		} else if ((in6p != NULL) && (stcb == NULL)) {
144171990Srrs			refcount_up = 1;
145163953Srrs		}
146171990Srrs		SCTP_STAT_INCR(sctps_badsum);
147171990Srrs		SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
148171990Srrs		goto bad;
149165647Srrs	}
150171990Srrs	sh->checksum = calc_check;
151171990Srrs
152163953Srrssctp_skip_csum:
153163953Srrs	net = NULL;
154163953Srrs	/*
155163953Srrs	 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
156163953Srrs	 * IP/SCTP/first chunk header...
157163953Srrs	 */
158163953Srrs	stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
159168299Srrs	    sh, ch, &in6p, &net, vrf_id);
160163953Srrs	/* in6p's ref-count increased */
161163953Srrs	if (in6p == NULL) {
162163953Srrs		struct sctp_init_chunk *init_chk, chunk_buf;
163163953Srrs
164163953Srrs		SCTP_STAT_INCR(sctps_noport);
165163953Srrs		if (ch->chunk_type == SCTP_INITIATION) {
166163953Srrs			/*
167163953Srrs			 * we do a trick here to get the INIT tag, dig in
168163953Srrs			 * and get the tag from the INIT and put it in the
169163953Srrs			 * common header.
170163953Srrs			 */
171163953Srrs			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
172163953Srrs			    iphlen + sizeof(*sh), sizeof(*init_chk),
173168299Srrs			    (uint8_t *) & chunk_buf);
174169420Srrs			if (init_chk)
175169420Srrs				sh->v_tag = init_chk->init.initiate_tag;
176169420Srrs			else
177169420Srrs				sh->v_tag = 0;
178163953Srrs		}
179165220Srrs		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
180170181Srrs			sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id);
181165220Srrs			goto bad;
182165220Srrs		}
183165220Srrs		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
184165220Srrs			goto bad;
185165220Srrs		}
186165220Srrs		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
187170181Srrs			sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id);
188163953Srrs		goto bad;
189163953Srrs	} else if (stcb == NULL) {
190163953Srrs		refcount_up = 1;
191163953Srrs	}
192163953Srrs	in6p_ip = (struct inpcb *)in6p;
193171167Sgnn#ifdef IPSEC
194163953Srrs	/*
195163953Srrs	 * Check AH/ESP integrity.
196163953Srrs	 */
197163996Srrs	if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) {
198163953Srrs/* XXX */
199163953Srrs		ipsec6stat.in_polvio++;
200163953Srrs		goto bad;
201163996Srrs	}
202171440Srrs#endif				/* IPSEC */
203163953Srrs
204163953Srrs	/*
205163953Srrs	 * CONTROL chunk processing
206163953Srrs	 */
207163953Srrs	offset -= sizeof(*ch);
208163953Srrs	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
209165647Srrs
210165647Srrs	/* Length now holds the total packet length payload + iphlen */
211165647Srrs	length = ntohs(ip6->ip6_plen) + iphlen;
212165647Srrs
213169655Srrs	/* sa_ignore NO_NULL_CHK */
214169378Srrs	sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
215170181Srrs	    in6p, stcb, net, ecn_bits, vrf_id);
216163953Srrs	/* inp's ref-count reduced && stcb unlocked */
217163953Srrs	/* XXX this stuff below gets moved to appropriate parts later... */
218163953Srrs	if (m)
219169352Srrs		sctp_m_freem(m);
220163953Srrs	if ((in6p) && refcount_up) {
221163953Srrs		/* reduce ref-count */
222163953Srrs		SCTP_INP_WLOCK(in6p);
223163953Srrs		SCTP_INP_DECR_REF(in6p);
224163953Srrs		SCTP_INP_WUNLOCK(in6p);
225163953Srrs	}
226163953Srrs	return IPPROTO_DONE;
227163953Srrs
228163953Srrsbad:
229169420Srrs	if (stcb) {
230163953Srrs		SCTP_TCB_UNLOCK(stcb);
231169420Srrs	}
232163953Srrs	if ((in6p) && refcount_up) {
233163953Srrs		/* reduce ref-count */
234163953Srrs		SCTP_INP_WLOCK(in6p);
235163953Srrs		SCTP_INP_DECR_REF(in6p);
236163953Srrs		SCTP_INP_WUNLOCK(in6p);
237163953Srrs	}
238163953Srrs	if (m)
239169352Srrs		sctp_m_freem(m);
240163953Srrs	return IPPROTO_DONE;
241163953Srrs}
242163953Srrs
243163953Srrs
244163953Srrsstatic void
245171259Sdelphijsctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
246171259Sdelphij    struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
247163953Srrs{
248168299Srrs	uint32_t nxtsz;
249163953Srrs
250163953Srrs	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
251163953Srrs	    (icmp6 == NULL) || (sh == NULL)) {
252163953Srrs		goto out;
253163953Srrs	}
254163953Srrs	/* First do we even look at it? */
255163953Srrs	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
256163953Srrs		goto out;
257163953Srrs
258163953Srrs	if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
259163953Srrs		/* not PACKET TO BIG */
260163953Srrs		goto out;
261163953Srrs	}
262163953Srrs	/*
263163953Srrs	 * ok we need to look closely. We could even get smarter and look at
264163953Srrs	 * anyone that we sent to in case we get a different ICMP that tells
265163953Srrs	 * us there is no way to reach a host, but for this impl, all we
266163953Srrs	 * care about is MTU discovery.
267163953Srrs	 */
268163953Srrs	nxtsz = ntohl(icmp6->icmp6_mtu);
269163953Srrs	/* Stop any PMTU timer */
270165220Srrs	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
271163953Srrs
272163953Srrs	/* Adjust destination size limit */
273163953Srrs	if (net->mtu > nxtsz) {
274163953Srrs		net->mtu = nxtsz;
275163953Srrs	}
276163953Srrs	/* now what about the ep? */
277163953Srrs	if (stcb->asoc.smallest_mtu > nxtsz) {
278163953Srrs		struct sctp_tmit_chunk *chk;
279163953Srrs
280163953Srrs		/* Adjust that too */
281163953Srrs		stcb->asoc.smallest_mtu = nxtsz;
282163953Srrs		/* now off to subtract IP_DF flag if needed */
283163953Srrs
284163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
285168299Srrs			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
286163953Srrs				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
287163953Srrs			}
288163953Srrs		}
289163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
290168299Srrs			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
291163953Srrs				/*
292163953Srrs				 * For this guy we also mark for immediate
293163953Srrs				 * resend since we sent to big of chunk
294163953Srrs				 */
295163953Srrs				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
296163953Srrs				if (chk->sent != SCTP_DATAGRAM_RESEND)
297163953Srrs					stcb->asoc.sent_queue_retran_cnt++;
298163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
299163953Srrs				chk->rec.data.doing_fast_retransmit = 0;
300163953Srrs
301163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
302163953Srrs				/* Clear any time so NO RTT is being done */
303163953Srrs				chk->sent_rcv_time.tv_sec = 0;
304163953Srrs				chk->sent_rcv_time.tv_usec = 0;
305163953Srrs				stcb->asoc.total_flight -= chk->send_size;
306163953Srrs				net->flight_size -= chk->send_size;
307163953Srrs			}
308163953Srrs		}
309163953Srrs	}
310163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
311163953Srrsout:
312169420Srrs	if (stcb) {
313163953Srrs		SCTP_TCB_UNLOCK(stcb);
314169420Srrs	}
315163953Srrs}
316163953Srrs
317163953Srrs
318163953Srrsvoid
319171259Sdelphijsctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
320163953Srrs{
321163953Srrs	struct sctphdr sh;
322163953Srrs	struct ip6ctlparam *ip6cp = NULL;
323167598Srrs	uint32_t vrf_id;
324166086Srrs	int cm;
325163953Srrs
326167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
327167598Srrs
328163953Srrs	if (pktdst->sa_family != AF_INET6 ||
329163953Srrs	    pktdst->sa_len != sizeof(struct sockaddr_in6))
330163953Srrs		return;
331163953Srrs
332163953Srrs	if ((unsigned)cmd >= PRC_NCMDS)
333163953Srrs		return;
334163953Srrs	if (PRC_IS_REDIRECT(cmd)) {
335163953Srrs		d = NULL;
336163953Srrs	} else if (inet6ctlerrmap[cmd] == 0) {
337163953Srrs		return;
338163953Srrs	}
339163953Srrs	/* if the parameter is from icmp6, decode it. */
340163953Srrs	if (d != NULL) {
341163953Srrs		ip6cp = (struct ip6ctlparam *)d;
342163953Srrs	} else {
343163953Srrs		ip6cp = (struct ip6ctlparam *)NULL;
344163953Srrs	}
345163953Srrs
346163953Srrs	if (ip6cp) {
347163953Srrs		/*
348163953Srrs		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
349163953Srrs		 * valid.
350163953Srrs		 */
351163953Srrs		/* check if we can safely examine src and dst ports */
352163953Srrs		struct sctp_inpcb *inp = NULL;
353163953Srrs		struct sctp_tcb *stcb = NULL;
354163953Srrs		struct sctp_nets *net = NULL;
355163953Srrs		struct sockaddr_in6 final;
356163953Srrs
357165647Srrs		if (ip6cp->ip6c_m == NULL)
358163953Srrs			return;
359163953Srrs
360163953Srrs		bzero(&sh, sizeof(sh));
361163953Srrs		bzero(&final, sizeof(final));
362163953Srrs		inp = NULL;
363163953Srrs		net = NULL;
364163953Srrs		m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
365163953Srrs		    (caddr_t)&sh);
366163953Srrs		ip6cp->ip6c_src->sin6_port = sh.src_port;
367163953Srrs		final.sin6_len = sizeof(final);
368163953Srrs		final.sin6_family = AF_INET6;
369163953Srrs		final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
370163953Srrs		final.sin6_port = sh.dest_port;
371163953Srrs		stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
372163953Srrs		    (struct sockaddr *)&final,
373167598Srrs		    &inp, &net, 1, vrf_id);
374163953Srrs		/* inp's ref-count increased && stcb locked */
375163953Srrs		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
376163953Srrs			if (cmd == PRC_MSGSIZE) {
377163953Srrs				sctp6_notify_mbuf(inp,
378163953Srrs				    ip6cp->ip6c_icmp6,
379163953Srrs				    &sh,
380163953Srrs				    stcb,
381163953Srrs				    net);
382163953Srrs				/* inp's ref-count reduced && stcb unlocked */
383163953Srrs			} else {
384163953Srrs				if (cmd == PRC_HOSTDEAD) {
385163953Srrs					cm = EHOSTUNREACH;
386163953Srrs				} else {
387163953Srrs					cm = inet6ctlerrmap[cmd];
388163953Srrs				}
389163953Srrs				sctp_notify(inp, cm, &sh,
390163953Srrs				    (struct sockaddr *)&final,
391163953Srrs				    stcb, net);
392163953Srrs				/* inp's ref-count reduced && stcb unlocked */
393163953Srrs			}
394163953Srrs		} else {
395163953Srrs			if (PRC_IS_REDIRECT(cmd) && inp) {
396163953Srrs				in6_rtchange((struct in6pcb *)inp,
397163953Srrs				    inet6ctlerrmap[cmd]);
398163953Srrs			}
399163953Srrs			if (inp) {
400163953Srrs				/* reduce inp's ref-count */
401163953Srrs				SCTP_INP_WLOCK(inp);
402163953Srrs				SCTP_INP_DECR_REF(inp);
403163953Srrs				SCTP_INP_WUNLOCK(inp);
404163953Srrs			}
405163953Srrs			if (stcb)
406163953Srrs				SCTP_TCB_UNLOCK(stcb);
407163953Srrs		}
408163953Srrs	}
409163953Srrs}
410163953Srrs
411163953Srrs/*
412163953Srrs * this routine can probably be collasped into the one in sctp_userreq.c
413163953Srrs * since they do the same thing and now we lookup with a sockaddr
414163953Srrs */
415163953Srrsstatic int
416163953Srrssctp6_getcred(SYSCTL_HANDLER_ARGS)
417163953Srrs{
418164085Srrs	struct xucred xuc;
419163953Srrs	struct sockaddr_in6 addrs[2];
420163953Srrs	struct sctp_inpcb *inp;
421163953Srrs	struct sctp_nets *net;
422163953Srrs	struct sctp_tcb *stcb;
423164085Srrs	int error;
424167598Srrs	uint32_t vrf_id;
425163953Srrs
426167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
427167598Srrs
428170587Srwatson	error = priv_check(req->td, PRIV_NETINET_GETCRED);
429163953Srrs	if (error)
430163953Srrs		return (error);
431163953Srrs
432171943Srrs	if (req->newlen != sizeof(addrs)) {
433171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
434163953Srrs		return (EINVAL);
435171943Srrs	}
436171943Srrs	if (req->oldlen != sizeof(struct ucred)) {
437171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
438163953Srrs		return (EINVAL);
439171943Srrs	}
440163953Srrs	error = SYSCTL_IN(req, addrs, sizeof(addrs));
441163953Srrs	if (error)
442163953Srrs		return (error);
443163953Srrs
444163953Srrs	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
445163953Srrs	    sin6tosa(&addrs[1]),
446167598Srrs	    &inp, &net, 1, vrf_id);
447163953Srrs	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
448164085Srrs		if ((inp != NULL) && (stcb == NULL)) {
449164085Srrs			/* reduce ref-count */
450163953Srrs			SCTP_INP_WLOCK(inp);
451163953Srrs			SCTP_INP_DECR_REF(inp);
452164085Srrs			goto cred_can_cont;
453163953Srrs		}
454171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
455164085Srrs		error = ENOENT;
456163953Srrs		goto out;
457163953Srrs	}
458163953Srrs	SCTP_TCB_UNLOCK(stcb);
459164085Srrs	/*
460164085Srrs	 * We use the write lock here, only since in the error leg we need
461164085Srrs	 * it. If we used RLOCK, then we would have to
462164085Srrs	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
463164085Srrs	 * Better to use higher wlock.
464164085Srrs	 */
465164085Srrs	SCTP_INP_WLOCK(inp);
466164085Srrscred_can_cont:
467164085Srrs	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
468164085Srrs	if (error) {
469164085Srrs		SCTP_INP_WUNLOCK(inp);
470164085Srrs		goto out;
471164085Srrs	}
472164085Srrs	cru2x(inp->sctp_socket->so_cred, &xuc);
473164085Srrs	SCTP_INP_WUNLOCK(inp);
474164085Srrs	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
475163953Srrsout:
476163953Srrs	return (error);
477163953Srrs}
478163953Srrs
479163953SrrsSYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
480163953Srrs    0, 0,
481163953Srrs    sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
482163953Srrs
483163953Srrs
484163953Srrs/* This is the same as the sctp_abort() could be made common */
485163953Srrsstatic void
486163953Srrssctp6_abort(struct socket *so)
487163953Srrs{
488163953Srrs	struct sctp_inpcb *inp;
489163953Srrs	uint32_t flags;
490163953Srrs
491163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
492163953Srrs	if (inp == 0)
493163953Srrs		return;
494163953Srrssctp_must_try_again:
495163953Srrs	flags = inp->sctp_flags;
496163953Srrs#ifdef SCTP_LOG_CLOSING
497163953Srrs	sctp_log_closing(inp, NULL, 17);
498163953Srrs#endif
499163953Srrs	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
500163953Srrs	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
501163953Srrs#ifdef SCTP_LOG_CLOSING
502163953Srrs		sctp_log_closing(inp, NULL, 16);
503163953Srrs#endif
504169380Srrs		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
505169380Srrs		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
506163953Srrs		SOCK_LOCK(so);
507167695Srrs		SCTP_SB_CLEAR(so->so_snd);
508163953Srrs		/*
509163953Srrs		 * same for the rcv ones, they are only here for the
510163953Srrs		 * accounting/select.
511163953Srrs		 */
512167695Srrs		SCTP_SB_CLEAR(so->so_rcv);
513167695Srrs		/* Now null out the reference, we are completely detached. */
514163953Srrs		so->so_pcb = NULL;
515163953Srrs		SOCK_UNLOCK(so);
516163953Srrs	} else {
517163953Srrs		flags = inp->sctp_flags;
518163953Srrs		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
519163953Srrs			goto sctp_must_try_again;
520163953Srrs		}
521163953Srrs	}
522163953Srrs	return;
523163953Srrs}
524163953Srrs
525163953Srrsstatic int
526163953Srrssctp6_attach(struct socket *so, int proto, struct thread *p)
527163953Srrs{
528163953Srrs	struct in6pcb *inp6;
529166086Srrs	int error;
530163953Srrs	struct sctp_inpcb *inp;
531170205Srrs	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
532163953Srrs
533163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
534171943Srrs	if (inp != NULL) {
535171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
536163953Srrs		return EINVAL;
537171943Srrs	}
538163953Srrs	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
539167695Srrs		error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
540163953Srrs		if (error)
541163953Srrs			return error;
542163953Srrs	}
543170205Srrs	error = sctp_inpcb_alloc(so, vrf_id);
544163953Srrs	if (error)
545163953Srrs		return error;
546163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
547170205Srrs	SCTP_INP_WLOCK(inp);
548163953Srrs	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
549163953Srrs	inp6 = (struct in6pcb *)inp;
550163953Srrs
551163953Srrs	inp6->inp_vflag |= INP_IPV6;
552163953Srrs	inp6->in6p_hops = -1;	/* use kernel default */
553163953Srrs	inp6->in6p_cksum = -1;	/* just to be sure */
554163953Srrs#ifdef INET
555163953Srrs	/*
556163953Srrs	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
557163953Srrs	 * socket as well, because the socket may be bound to an IPv6
558163953Srrs	 * wildcard address, which may match an IPv4-mapped IPv6 address.
559163953Srrs	 */
560163953Srrs	inp6->inp_ip_ttl = ip_defttl;
561163953Srrs#endif
562163953Srrs	/*
563163953Srrs	 * Hmm what about the IPSEC stuff that is missing here but in
564163953Srrs	 * sctp_attach()?
565163953Srrs	 */
566170205Srrs	SCTP_INP_WUNLOCK(inp);
567163953Srrs	return 0;
568163953Srrs}
569163953Srrs
570163953Srrsstatic int
571163953Srrssctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
572163953Srrs{
573163953Srrs	struct sctp_inpcb *inp;
574163953Srrs	struct in6pcb *inp6;
575166086Srrs	int error;
576163953Srrs
577163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
578171943Srrs	if (inp == 0) {
579171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
580163953Srrs		return EINVAL;
581171943Srrs	}
582170056Srrs	if (addr) {
583170056Srrs		if ((addr->sa_family == AF_INET6) &&
584170056Srrs		    (addr->sa_len != sizeof(struct sockaddr_in6))) {
585171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
586170056Srrs			return EINVAL;
587170056Srrs		}
588170056Srrs		if ((addr->sa_family == AF_INET) &&
589170056Srrs		    (addr->sa_len != sizeof(struct sockaddr_in))) {
590171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
591170056Srrs			return EINVAL;
592170056Srrs		}
593170056Srrs	}
594163953Srrs	inp6 = (struct in6pcb *)inp;
595163953Srrs	inp6->inp_vflag &= ~INP_IPV4;
596163953Srrs	inp6->inp_vflag |= INP_IPV6;
597166023Srrs	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
598163953Srrs		if (addr->sa_family == AF_INET) {
599163953Srrs			/* binding v4 addr to v6 socket, so reset flags */
600163953Srrs			inp6->inp_vflag |= INP_IPV4;
601163953Srrs			inp6->inp_vflag &= ~INP_IPV6;
602163953Srrs		} else {
603163953Srrs			struct sockaddr_in6 *sin6_p;
604163953Srrs
605163953Srrs			sin6_p = (struct sockaddr_in6 *)addr;
606163953Srrs
607163953Srrs			if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
608163953Srrs				inp6->inp_vflag |= INP_IPV4;
609163953Srrs			} else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
610163953Srrs				struct sockaddr_in sin;
611163953Srrs
612163953Srrs				in6_sin6_2_sin(&sin, sin6_p);
613163953Srrs				inp6->inp_vflag |= INP_IPV4;
614163953Srrs				inp6->inp_vflag &= ~INP_IPV6;
615171572Srrs				error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
616163953Srrs				return error;
617163953Srrs			}
618163953Srrs		}
619163953Srrs	} else if (addr != NULL) {
620163953Srrs		/* IPV6_V6ONLY socket */
621163953Srrs		if (addr->sa_family == AF_INET) {
622163953Srrs			/* can't bind v4 addr to v6 only socket! */
623171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
624163953Srrs			return EINVAL;
625163953Srrs		} else {
626163953Srrs			struct sockaddr_in6 *sin6_p;
627163953Srrs
628163953Srrs			sin6_p = (struct sockaddr_in6 *)addr;
629163953Srrs
630163953Srrs			if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
631163953Srrs				/* can't bind v4-mapped addrs either! */
632163953Srrs				/* NOTE: we don't support SIIT */
633171943Srrs				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
634171943Srrs			return EINVAL;
635163953Srrs		}
636163953Srrs	}
637171572Srrs	error = sctp_inpcb_bind(so, addr, NULL, p);
638163953Srrs	return error;
639163953Srrs}
640163953Srrs
641163953Srrs
642163953Srrsstatic void
643163953Srrssctp6_close(struct socket *so)
644163953Srrs{
645171990Srrs	sctp_close(so);
646163953Srrs}
647163953Srrs
648167598Srrs/* This could be made common with sctp_detach() since they are identical */
649163953Srrs
650168709Srrsstatic
651168709Srrsint
652163953Srrssctp6_disconnect(struct socket *so)
653163953Srrs{
654171990Srrs	return (sctp_disconnect(so));
655163953Srrs}
656163953Srrs
657168709Srrs
658163953Srrsint
659163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
660163953Srrs    struct mbuf *control, struct thread *p);
661163953Srrs
662163953Srrs
663163953Srrsstatic int
664163953Srrssctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
665163953Srrs    struct mbuf *control, struct thread *p)
666163953Srrs{
667163953Srrs	struct sctp_inpcb *inp;
668163953Srrs	struct inpcb *in_inp;
669163953Srrs	struct in6pcb *inp6;
670163953Srrs
671163953Srrs#ifdef INET
672163953Srrs	struct sockaddr_in6 *sin6;
673163953Srrs
674163953Srrs#endif				/* INET */
675163953Srrs	/* No SPL needed since sctp_output does this */
676163953Srrs
677163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
678163953Srrs	if (inp == NULL) {
679163953Srrs		if (control) {
680169352Srrs			SCTP_RELEASE_PKT(control);
681163953Srrs			control = NULL;
682163953Srrs		}
683169352Srrs		SCTP_RELEASE_PKT(m);
684171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
685163953Srrs		return EINVAL;
686163953Srrs	}
687163953Srrs	in_inp = (struct inpcb *)inp;
688163953Srrs	inp6 = (struct in6pcb *)inp;
689163953Srrs	/*
690163953Srrs	 * For the TCP model we may get a NULL addr, if we are a connected
691163953Srrs	 * socket thats ok.
692163953Srrs	 */
693163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
694163953Srrs	    (addr == NULL)) {
695163953Srrs		goto connected_type;
696163953Srrs	}
697163953Srrs	if (addr == NULL) {
698169352Srrs		SCTP_RELEASE_PKT(m);
699163953Srrs		if (control) {
700169352Srrs			SCTP_RELEASE_PKT(control);
701163953Srrs			control = NULL;
702163953Srrs		}
703171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
704163953Srrs		return (EDESTADDRREQ);
705163953Srrs	}
706163953Srrs#ifdef INET
707163953Srrs	sin6 = (struct sockaddr_in6 *)addr;
708166023Srrs	if (SCTP_IPV6_V6ONLY(inp6)) {
709163953Srrs		/*
710163953Srrs		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
711163953Srrs		 * v4 addr or v4-mapped addr
712163953Srrs		 */
713163953Srrs		if (addr->sa_family == AF_INET) {
714171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
715163953Srrs			return EINVAL;
716163953Srrs		}
717163953Srrs		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
718171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
719163953Srrs			return EINVAL;
720163953Srrs		}
721163953Srrs	}
722163953Srrs	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
723163953Srrs		if (!ip6_v6only) {
724163953Srrs			struct sockaddr_in sin;
725163953Srrs
726163953Srrs			/* convert v4-mapped into v4 addr and send */
727163953Srrs			in6_sin6_2_sin(&sin, sin6);
728163953Srrs			return sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
729163953Srrs			    control, p);
730163953Srrs		} else {
731163953Srrs			/* mapped addresses aren't enabled */
732171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
733163953Srrs			return EINVAL;
734163953Srrs		}
735163953Srrs	}
736163953Srrs#endif				/* INET */
737163953Srrsconnected_type:
738163953Srrs	/* now what about control */
739163953Srrs	if (control) {
740163953Srrs		if (inp->control) {
741169420Srrs			SCTP_PRINTF("huh? control set?\n");
742169352Srrs			SCTP_RELEASE_PKT(inp->control);
743163953Srrs			inp->control = NULL;
744163953Srrs		}
745163953Srrs		inp->control = control;
746163953Srrs	}
747163953Srrs	/* Place the data */
748163953Srrs	if (inp->pkt) {
749165647Srrs		SCTP_BUF_NEXT(inp->pkt_last) = m;
750163953Srrs		inp->pkt_last = m;
751163953Srrs	} else {
752163953Srrs		inp->pkt_last = inp->pkt = m;
753163953Srrs	}
754163953Srrs	if (
755163953Srrs	/* FreeBSD and MacOSX uses a flag passed */
756163953Srrs	    ((flags & PRUS_MORETOCOME) == 0)
757163953Srrs	    ) {
758163953Srrs		/*
759163953Srrs		 * note with the current version this code will only be used
760163953Srrs		 * by OpenBSD, NetBSD and FreeBSD have methods for
761163953Srrs		 * re-defining sosend() to use sctp_sosend().  One can
762163953Srrs		 * optionaly switch back to this code (by changing back the
763163953Srrs		 * defininitions but this is not advisable.
764163953Srrs		 */
765163953Srrs		int ret;
766163953Srrs
767163953Srrs		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
768163953Srrs		inp->pkt = NULL;
769163953Srrs		inp->control = NULL;
770163953Srrs		return (ret);
771163953Srrs	} else {
772163953Srrs		return (0);
773163953Srrs	}
774163953Srrs}
775163953Srrs
776163953Srrsstatic int
777163953Srrssctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
778163953Srrs{
779167598Srrs	uint32_t vrf_id;
780163953Srrs	int error = 0;
781163953Srrs	struct sctp_inpcb *inp;
782163953Srrs	struct in6pcb *inp6;
783163953Srrs	struct sctp_tcb *stcb;
784163953Srrs
785163953Srrs#ifdef INET
786163953Srrs	struct sockaddr_in6 *sin6;
787163953Srrs	struct sockaddr_storage ss;
788163953Srrs
789163953Srrs#endif				/* INET */
790163953Srrs
791163953Srrs	inp6 = (struct in6pcb *)so->so_pcb;
792163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
793163953Srrs	if (inp == 0) {
794171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
795163953Srrs		return (ECONNRESET);	/* I made the same as TCP since we are
796163953Srrs					 * not setup? */
797163953Srrs	}
798170056Srrs	if (addr == NULL) {
799171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
800170056Srrs		return (EINVAL);
801170056Srrs	}
802170056Srrs	if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
803171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
804170056Srrs		return (EINVAL);
805170056Srrs	}
806170056Srrs	if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
807171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
808170056Srrs		return (EINVAL);
809170056Srrs	}
810168299Srrs	vrf_id = inp->def_vrf_id;
811163953Srrs	SCTP_ASOC_CREATE_LOCK(inp);
812163953Srrs	SCTP_INP_RLOCK(inp);
813163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
814163953Srrs	    SCTP_PCB_FLAGS_UNBOUND) {
815163953Srrs		/* Bind a ephemeral port */
816163953Srrs		SCTP_INP_RUNLOCK(inp);
817163953Srrs		error = sctp6_bind(so, NULL, p);
818163953Srrs		if (error) {
819163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
820163953Srrs
821163953Srrs			return (error);
822163953Srrs		}
823163953Srrs		SCTP_INP_RLOCK(inp);
824163953Srrs	}
825163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
826163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
827163953Srrs		/* We are already connected AND the TCP model */
828163953Srrs		SCTP_INP_RUNLOCK(inp);
829163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
830171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
831163953Srrs		return (EADDRINUSE);
832163953Srrs	}
833163953Srrs#ifdef INET
834163953Srrs	sin6 = (struct sockaddr_in6 *)addr;
835166023Srrs	if (SCTP_IPV6_V6ONLY(inp6)) {
836163953Srrs		/*
837163953Srrs		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
838163953Srrs		 * addr or v4-mapped addr
839163953Srrs		 */
840163953Srrs		if (addr->sa_family == AF_INET) {
841163953Srrs			SCTP_INP_RUNLOCK(inp);
842163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
843171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
844163953Srrs			return EINVAL;
845163953Srrs		}
846163953Srrs		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
847163953Srrs			SCTP_INP_RUNLOCK(inp);
848163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
849171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
850163953Srrs			return EINVAL;
851163953Srrs		}
852163953Srrs	}
853163953Srrs	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
854163953Srrs		if (!ip6_v6only) {
855163953Srrs			/* convert v4-mapped into v4 addr */
856163953Srrs			in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
857163953Srrs			addr = (struct sockaddr *)&ss;
858163953Srrs		} else {
859163953Srrs			/* mapped addresses aren't enabled */
860163953Srrs			SCTP_INP_RUNLOCK(inp);
861163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
862171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
863163953Srrs			return EINVAL;
864163953Srrs		}
865163953Srrs	} else
866163953Srrs#endif				/* INET */
867163953Srrs		addr = addr;	/* for true v6 address case */
868163953Srrs
869163953Srrs	/* Now do we connect? */
870163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
871163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
872169420Srrs		if (stcb) {
873163953Srrs			SCTP_TCB_UNLOCK(stcb);
874169420Srrs		}
875163953Srrs		SCTP_INP_RUNLOCK(inp);
876163953Srrs	} else {
877163953Srrs		SCTP_INP_RUNLOCK(inp);
878163953Srrs		SCTP_INP_WLOCK(inp);
879163953Srrs		SCTP_INP_INCR_REF(inp);
880163953Srrs		SCTP_INP_WUNLOCK(inp);
881163953Srrs		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
882163953Srrs		if (stcb == NULL) {
883163953Srrs			SCTP_INP_WLOCK(inp);
884163953Srrs			SCTP_INP_DECR_REF(inp);
885163953Srrs			SCTP_INP_WUNLOCK(inp);
886163953Srrs		}
887163953Srrs	}
888163953Srrs
889163953Srrs	if (stcb != NULL) {
890163953Srrs		/* Already have or am bring up an association */
891163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
892163953Srrs		SCTP_TCB_UNLOCK(stcb);
893171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
894163953Srrs		return (EALREADY);
895163953Srrs	}
896163953Srrs	/* We are GOOD to go */
897171531Srrs	stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
898163953Srrs	SCTP_ASOC_CREATE_UNLOCK(inp);
899163953Srrs	if (stcb == NULL) {
900163953Srrs		/* Gak! no memory */
901163953Srrs		return (error);
902163953Srrs	}
903163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
904163953Srrs		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
905163953Srrs		/* Set the connected flag so we can queue data */
906163953Srrs		soisconnecting(so);
907163953Srrs	}
908163953Srrs	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
909169420Srrs	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
910163953Srrs
911163953Srrs	/* initialize authentication parameters for the assoc */
912163953Srrs	sctp_initialize_auth_params(inp, stcb);
913163953Srrs
914172090Srrs	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
915163953Srrs	SCTP_TCB_UNLOCK(stcb);
916163953Srrs	return error;
917163953Srrs}
918163953Srrs
919163953Srrsstatic int
920163953Srrssctp6_getaddr(struct socket *so, struct sockaddr **addr)
921163953Srrs{
922163953Srrs	struct sockaddr_in6 *sin6;
923163953Srrs	struct sctp_inpcb *inp;
924167598Srrs	uint32_t vrf_id;
925167598Srrs	struct sctp_ifa *sctp_ifa;
926163953Srrs
927163953Srrs	int error;
928163953Srrs
929163953Srrs	/*
930163953Srrs	 * Do the malloc first in case it blocks.
931163953Srrs	 */
932163953Srrs	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
933163953Srrs	sin6->sin6_family = AF_INET6;
934163953Srrs	sin6->sin6_len = sizeof(*sin6);
935163953Srrs
936163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
937163953Srrs	if (inp == NULL) {
938163953Srrs		SCTP_FREE_SONAME(sin6);
939171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
940163953Srrs		return ECONNRESET;
941163953Srrs	}
942163953Srrs	SCTP_INP_RLOCK(inp);
943163953Srrs	sin6->sin6_port = inp->sctp_lport;
944163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
945163953Srrs		/* For the bound all case you get back 0 */
946163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
947163953Srrs			struct sctp_tcb *stcb;
948163953Srrs			struct sockaddr_in6 *sin_a6;
949163953Srrs			struct sctp_nets *net;
950163953Srrs			int fnd;
951163953Srrs
952163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
953163953Srrs			if (stcb == NULL) {
954163953Srrs				goto notConn6;
955163953Srrs			}
956163953Srrs			fnd = 0;
957163953Srrs			sin_a6 = NULL;
958163953Srrs			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
959163953Srrs				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
960164085Srrs				if (sin_a6 == NULL)
961164085Srrs					/* this will make coverity happy */
962164085Srrs					continue;
963164085Srrs
964163953Srrs				if (sin_a6->sin6_family == AF_INET6) {
965163953Srrs					fnd = 1;
966163953Srrs					break;
967163953Srrs				}
968163953Srrs			}
969163953Srrs			if ((!fnd) || (sin_a6 == NULL)) {
970163953Srrs				/* punt */
971163953Srrs				goto notConn6;
972163953Srrs			}
973168299Srrs			vrf_id = inp->def_vrf_id;
974168299Srrs			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
975167598Srrs			if (sctp_ifa) {
976167598Srrs				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
977167598Srrs			}
978163953Srrs		} else {
979163953Srrs			/* For the bound all case you get back 0 */
980163953Srrs	notConn6:
981163953Srrs			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
982163953Srrs		}
983163953Srrs	} else {
984163953Srrs		/* Take the first IPv6 address in the list */
985163953Srrs		struct sctp_laddr *laddr;
986163953Srrs		int fnd = 0;
987163953Srrs
988163953Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
989167598Srrs			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
990163953Srrs				struct sockaddr_in6 *sin_a;
991163953Srrs
992167598Srrs				sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
993163953Srrs				sin6->sin6_addr = sin_a->sin6_addr;
994163953Srrs				fnd = 1;
995163953Srrs				break;
996163953Srrs			}
997163953Srrs		}
998163953Srrs		if (!fnd) {
999163953Srrs			SCTP_FREE_SONAME(sin6);
1000163953Srrs			SCTP_INP_RUNLOCK(inp);
1001171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1002163953Srrs			return ENOENT;
1003163953Srrs		}
1004163953Srrs	}
1005163953Srrs	SCTP_INP_RUNLOCK(inp);
1006163953Srrs	/* Scoping things for v6 */
1007164085Srrs	if ((error = sa6_recoverscope(sin6)) != 0) {
1008164085Srrs		SCTP_FREE_SONAME(sin6);
1009163953Srrs		return (error);
1010164085Srrs	}
1011163953Srrs	(*addr) = (struct sockaddr *)sin6;
1012163953Srrs	return (0);
1013163953Srrs}
1014163953Srrs
1015163953Srrsstatic int
1016163953Srrssctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1017163953Srrs{
1018163953Srrs	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1019163953Srrs	int fnd;
1020163953Srrs	struct sockaddr_in6 *sin_a6;
1021163953Srrs	struct sctp_inpcb *inp;
1022163953Srrs	struct sctp_tcb *stcb;
1023163953Srrs	struct sctp_nets *net;
1024163953Srrs
1025163953Srrs	int error;
1026163953Srrs
1027163953Srrs	/*
1028163953Srrs	 * Do the malloc first in case it blocks.
1029163953Srrs	 */
1030163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1031163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1032163953Srrs		/* UDP type and listeners will drop out here */
1033171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1034163953Srrs		return (ENOTCONN);
1035163953Srrs	}
1036163953Srrs	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1037163953Srrs	sin6->sin6_family = AF_INET6;
1038163953Srrs	sin6->sin6_len = sizeof(*sin6);
1039163953Srrs
1040163953Srrs	/* We must recapture incase we blocked */
1041163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1042163953Srrs	if (inp == NULL) {
1043163953Srrs		SCTP_FREE_SONAME(sin6);
1044171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1045163953Srrs		return ECONNRESET;
1046163953Srrs	}
1047163953Srrs	SCTP_INP_RLOCK(inp);
1048163953Srrs	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1049169420Srrs	if (stcb) {
1050163953Srrs		SCTP_TCB_LOCK(stcb);
1051169420Srrs	}
1052163953Srrs	SCTP_INP_RUNLOCK(inp);
1053163953Srrs	if (stcb == NULL) {
1054163953Srrs		SCTP_FREE_SONAME(sin6);
1055171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1056163953Srrs		return ECONNRESET;
1057163953Srrs	}
1058163953Srrs	fnd = 0;
1059163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1060163953Srrs		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1061163953Srrs		if (sin_a6->sin6_family == AF_INET6) {
1062163953Srrs			fnd = 1;
1063163953Srrs			sin6->sin6_port = stcb->rport;
1064163953Srrs			sin6->sin6_addr = sin_a6->sin6_addr;
1065163953Srrs			break;
1066163953Srrs		}
1067163953Srrs	}
1068163953Srrs	SCTP_TCB_UNLOCK(stcb);
1069163953Srrs	if (!fnd) {
1070163953Srrs		/* No IPv4 address */
1071163953Srrs		SCTP_FREE_SONAME(sin6);
1072171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1073163953Srrs		return ENOENT;
1074163953Srrs	}
1075163953Srrs	if ((error = sa6_recoverscope(sin6)) != 0)
1076163953Srrs		return (error);
1077163953Srrs	*addr = (struct sockaddr *)sin6;
1078163953Srrs	return (0);
1079163953Srrs}
1080163953Srrs
1081163953Srrsstatic int
1082163953Srrssctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1083163953Srrs{
1084163953Srrs	struct sockaddr *addr;
1085163953Srrs	struct in6pcb *inp6 = sotoin6pcb(so);
1086166086Srrs	int error;
1087163953Srrs
1088171943Srrs	if (inp6 == NULL) {
1089171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1090163953Srrs		return EINVAL;
1091171943Srrs	}
1092163953Srrs	/* allow v6 addresses precedence */
1093163953Srrs	error = sctp6_getaddr(so, nam);
1094163953Srrs	if (error) {
1095163953Srrs		/* try v4 next if v6 failed */
1096163953Srrs		error = sctp_ingetaddr(so, nam);
1097163953Srrs		if (error) {
1098163953Srrs			return (error);
1099163953Srrs		}
1100163953Srrs		addr = *nam;
1101163953Srrs		/* if I'm V6ONLY, convert it to v4-mapped */
1102166023Srrs		if (SCTP_IPV6_V6ONLY(inp6)) {
1103163953Srrs			struct sockaddr_in6 sin6;
1104163953Srrs
1105163953Srrs			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1106163953Srrs			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1107168709Srrs
1108163953Srrs		}
1109163953Srrs	}
1110163953Srrs	return (error);
1111163953Srrs}
1112163953Srrs
1113163953Srrs
1114163953Srrsstatic int
1115163953Srrssctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1116163953Srrs{
1117163953Srrs	struct sockaddr *addr = *nam;
1118163953Srrs	struct in6pcb *inp6 = sotoin6pcb(so);
1119166086Srrs	int error;
1120163953Srrs
1121171943Srrs	if (inp6 == NULL) {
1122171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1123163953Srrs		return EINVAL;
1124171943Srrs	}
1125163953Srrs	/* allow v6 addresses precedence */
1126163953Srrs	error = sctp6_peeraddr(so, nam);
1127163953Srrs	if (error) {
1128163953Srrs		/* try v4 next if v6 failed */
1129163953Srrs		error = sctp_peeraddr(so, nam);
1130163953Srrs		if (error) {
1131163953Srrs			return (error);
1132163953Srrs		}
1133163953Srrs		/* if I'm V6ONLY, convert it to v4-mapped */
1134166023Srrs		if (SCTP_IPV6_V6ONLY(inp6)) {
1135163953Srrs			struct sockaddr_in6 sin6;
1136163953Srrs
1137163953Srrs			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1138163953Srrs			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1139163953Srrs		}
1140163953Srrs	}
1141163953Srrs	return error;
1142163953Srrs}
1143163953Srrs
1144163953Srrsstruct pr_usrreqs sctp6_usrreqs = {
1145163953Srrs	.pru_abort = sctp6_abort,
1146163953Srrs	.pru_accept = sctp_accept,
1147163953Srrs	.pru_attach = sctp6_attach,
1148163953Srrs	.pru_bind = sctp6_bind,
1149163953Srrs	.pru_connect = sctp6_connect,
1150163953Srrs	.pru_control = in6_control,
1151163953Srrs	.pru_close = sctp6_close,
1152163953Srrs	.pru_detach = sctp6_close,
1153163953Srrs	.pru_sopoll = sopoll_generic,
1154163953Srrs	.pru_disconnect = sctp6_disconnect,
1155163953Srrs	.pru_listen = sctp_listen,
1156163953Srrs	.pru_peeraddr = sctp6_getpeeraddr,
1157163953Srrs	.pru_send = sctp6_send,
1158163953Srrs	.pru_shutdown = sctp_shutdown,
1159163953Srrs	.pru_sockaddr = sctp6_in6getaddr,
1160163953Srrs	.pru_sosend = sctp_sosend,
1161163953Srrs	.pru_soreceive = sctp_soreceive
1162163953Srrs};
1163