sctp6_usrreq.c revision 171943
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 171943 2007-08-24 00:53:53Z 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;
127163953Srrs	if ((sctp_no_csum_on_loopback == 0) ||
128165647Srrs	    (!SCTP_IS_IT_LOOPBACK(m))) {
129163953Srrs		/*
130163953Srrs		 * we do NOT validate things from the loopback if the sysctl
131163953Srrs		 * is set to 1.
132163953Srrs		 */
133163953Srrs		check = sh->checksum;	/* save incoming checksum */
134163953Srrs		if ((check == 0) && (sctp_no_csum_on_loopback)) {
135163953Srrs			/*
136163953Srrs			 * special hook for where we got a local address
137163953Srrs			 * somehow routed across a non IFT_LOOP type
138163953Srrs			 * interface
139163953Srrs			 */
140163953Srrs			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))
141163953Srrs				goto sctp_skip_csum;
142163953Srrs		}
143163953Srrs		sh->checksum = 0;	/* prepare for calc */
144163953Srrs		calc_check = sctp_calculate_sum(m, &mlen, iphlen);
145163953Srrs		if (calc_check != check) {
146169420Srrs			SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
147169420Srrs			    calc_check, check, m, mlen, iphlen);
148163953Srrs			stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
149168299Srrs			    sh, ch, &in6p, &net, vrf_id);
150163953Srrs			/* in6p's ref-count increased && stcb locked */
151163953Srrs			if ((in6p) && (stcb)) {
152163953Srrs				sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
153163953Srrs				sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, 2);
154163953Srrs			} else if ((in6p != NULL) && (stcb == NULL)) {
155163953Srrs				refcount_up = 1;
156163953Srrs			}
157163953Srrs			SCTP_STAT_INCR(sctps_badsum);
158163953Srrs			SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
159163953Srrs			goto bad;
160163953Srrs		}
161163953Srrs		sh->checksum = calc_check;
162165647Srrs	}
163163953Srrssctp_skip_csum:
164163953Srrs	net = NULL;
165163953Srrs	/*
166163953Srrs	 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
167163953Srrs	 * IP/SCTP/first chunk header...
168163953Srrs	 */
169163953Srrs	stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
170168299Srrs	    sh, ch, &in6p, &net, vrf_id);
171163953Srrs	/* in6p's ref-count increased */
172163953Srrs	if (in6p == NULL) {
173163953Srrs		struct sctp_init_chunk *init_chk, chunk_buf;
174163953Srrs
175163953Srrs		SCTP_STAT_INCR(sctps_noport);
176163953Srrs		if (ch->chunk_type == SCTP_INITIATION) {
177163953Srrs			/*
178163953Srrs			 * we do a trick here to get the INIT tag, dig in
179163953Srrs			 * and get the tag from the INIT and put it in the
180163953Srrs			 * common header.
181163953Srrs			 */
182163953Srrs			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
183163953Srrs			    iphlen + sizeof(*sh), sizeof(*init_chk),
184168299Srrs			    (uint8_t *) & chunk_buf);
185169420Srrs			if (init_chk)
186169420Srrs				sh->v_tag = init_chk->init.initiate_tag;
187169420Srrs			else
188169420Srrs				sh->v_tag = 0;
189163953Srrs		}
190165220Srrs		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
191170181Srrs			sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id);
192165220Srrs			goto bad;
193165220Srrs		}
194165220Srrs		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
195165220Srrs			goto bad;
196165220Srrs		}
197165220Srrs		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
198170181Srrs			sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id);
199163953Srrs		goto bad;
200163953Srrs	} else if (stcb == NULL) {
201163953Srrs		refcount_up = 1;
202163953Srrs	}
203163953Srrs	in6p_ip = (struct inpcb *)in6p;
204171167Sgnn#ifdef IPSEC
205163953Srrs	/*
206163953Srrs	 * Check AH/ESP integrity.
207163953Srrs	 */
208163996Srrs	if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) {
209163953Srrs/* XXX */
210163953Srrs		ipsec6stat.in_polvio++;
211163953Srrs		goto bad;
212163996Srrs	}
213171440Srrs#endif				/* IPSEC */
214163953Srrs
215163953Srrs	/*
216163953Srrs	 * CONTROL chunk processing
217163953Srrs	 */
218163953Srrs	offset -= sizeof(*ch);
219163953Srrs	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
220165647Srrs
221165647Srrs	/* Length now holds the total packet length payload + iphlen */
222165647Srrs	length = ntohs(ip6->ip6_plen) + iphlen;
223165647Srrs
224169655Srrs	/* sa_ignore NO_NULL_CHK */
225169378Srrs	sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
226170181Srrs	    in6p, stcb, net, ecn_bits, vrf_id);
227163953Srrs	/* inp's ref-count reduced && stcb unlocked */
228163953Srrs	/* XXX this stuff below gets moved to appropriate parts later... */
229163953Srrs	if (m)
230169352Srrs		sctp_m_freem(m);
231163953Srrs	if ((in6p) && refcount_up) {
232163953Srrs		/* reduce ref-count */
233163953Srrs		SCTP_INP_WLOCK(in6p);
234163953Srrs		SCTP_INP_DECR_REF(in6p);
235163953Srrs		SCTP_INP_WUNLOCK(in6p);
236163953Srrs	}
237163953Srrs	return IPPROTO_DONE;
238163953Srrs
239163953Srrsbad:
240169420Srrs	if (stcb) {
241163953Srrs		SCTP_TCB_UNLOCK(stcb);
242169420Srrs	}
243163953Srrs	if ((in6p) && refcount_up) {
244163953Srrs		/* reduce ref-count */
245163953Srrs		SCTP_INP_WLOCK(in6p);
246163953Srrs		SCTP_INP_DECR_REF(in6p);
247163953Srrs		SCTP_INP_WUNLOCK(in6p);
248163953Srrs	}
249163953Srrs	if (m)
250169352Srrs		sctp_m_freem(m);
251163953Srrs	return IPPROTO_DONE;
252163953Srrs}
253163953Srrs
254163953Srrs
255163953Srrsstatic void
256171259Sdelphijsctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
257171259Sdelphij    struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
258163953Srrs{
259168299Srrs	uint32_t nxtsz;
260163953Srrs
261163953Srrs	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
262163953Srrs	    (icmp6 == NULL) || (sh == NULL)) {
263163953Srrs		goto out;
264163953Srrs	}
265163953Srrs	/* First do we even look at it? */
266163953Srrs	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
267163953Srrs		goto out;
268163953Srrs
269163953Srrs	if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
270163953Srrs		/* not PACKET TO BIG */
271163953Srrs		goto out;
272163953Srrs	}
273163953Srrs	/*
274163953Srrs	 * ok we need to look closely. We could even get smarter and look at
275163953Srrs	 * anyone that we sent to in case we get a different ICMP that tells
276163953Srrs	 * us there is no way to reach a host, but for this impl, all we
277163953Srrs	 * care about is MTU discovery.
278163953Srrs	 */
279163953Srrs	nxtsz = ntohl(icmp6->icmp6_mtu);
280163953Srrs	/* Stop any PMTU timer */
281165220Srrs	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
282163953Srrs
283163953Srrs	/* Adjust destination size limit */
284163953Srrs	if (net->mtu > nxtsz) {
285163953Srrs		net->mtu = nxtsz;
286163953Srrs	}
287163953Srrs	/* now what about the ep? */
288163953Srrs	if (stcb->asoc.smallest_mtu > nxtsz) {
289163953Srrs		struct sctp_tmit_chunk *chk;
290163953Srrs
291163953Srrs		/* Adjust that too */
292163953Srrs		stcb->asoc.smallest_mtu = nxtsz;
293163953Srrs		/* now off to subtract IP_DF flag if needed */
294163953Srrs
295163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
296168299Srrs			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
297163953Srrs				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
298163953Srrs			}
299163953Srrs		}
300163953Srrs		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
301168299Srrs			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
302163953Srrs				/*
303163953Srrs				 * For this guy we also mark for immediate
304163953Srrs				 * resend since we sent to big of chunk
305163953Srrs				 */
306163953Srrs				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
307163953Srrs				if (chk->sent != SCTP_DATAGRAM_RESEND)
308163953Srrs					stcb->asoc.sent_queue_retran_cnt++;
309163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
310163953Srrs				chk->rec.data.doing_fast_retransmit = 0;
311163953Srrs
312163953Srrs				chk->sent = SCTP_DATAGRAM_RESEND;
313163953Srrs				/* Clear any time so NO RTT is being done */
314163953Srrs				chk->sent_rcv_time.tv_sec = 0;
315163953Srrs				chk->sent_rcv_time.tv_usec = 0;
316163953Srrs				stcb->asoc.total_flight -= chk->send_size;
317163953Srrs				net->flight_size -= chk->send_size;
318163953Srrs			}
319163953Srrs		}
320163953Srrs	}
321163953Srrs	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
322163953Srrsout:
323169420Srrs	if (stcb) {
324163953Srrs		SCTP_TCB_UNLOCK(stcb);
325169420Srrs	}
326163953Srrs}
327163953Srrs
328163953Srrs
329163953Srrsvoid
330171259Sdelphijsctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
331163953Srrs{
332163953Srrs	struct sctphdr sh;
333163953Srrs	struct ip6ctlparam *ip6cp = NULL;
334167598Srrs	uint32_t vrf_id;
335166086Srrs	int cm;
336163953Srrs
337167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
338167598Srrs
339163953Srrs	if (pktdst->sa_family != AF_INET6 ||
340163953Srrs	    pktdst->sa_len != sizeof(struct sockaddr_in6))
341163953Srrs		return;
342163953Srrs
343163953Srrs	if ((unsigned)cmd >= PRC_NCMDS)
344163953Srrs		return;
345163953Srrs	if (PRC_IS_REDIRECT(cmd)) {
346163953Srrs		d = NULL;
347163953Srrs	} else if (inet6ctlerrmap[cmd] == 0) {
348163953Srrs		return;
349163953Srrs	}
350163953Srrs	/* if the parameter is from icmp6, decode it. */
351163953Srrs	if (d != NULL) {
352163953Srrs		ip6cp = (struct ip6ctlparam *)d;
353163953Srrs	} else {
354163953Srrs		ip6cp = (struct ip6ctlparam *)NULL;
355163953Srrs	}
356163953Srrs
357163953Srrs	if (ip6cp) {
358163953Srrs		/*
359163953Srrs		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
360163953Srrs		 * valid.
361163953Srrs		 */
362163953Srrs		/* check if we can safely examine src and dst ports */
363163953Srrs		struct sctp_inpcb *inp = NULL;
364163953Srrs		struct sctp_tcb *stcb = NULL;
365163953Srrs		struct sctp_nets *net = NULL;
366163953Srrs		struct sockaddr_in6 final;
367163953Srrs
368165647Srrs		if (ip6cp->ip6c_m == NULL)
369163953Srrs			return;
370163953Srrs
371163953Srrs		bzero(&sh, sizeof(sh));
372163953Srrs		bzero(&final, sizeof(final));
373163953Srrs		inp = NULL;
374163953Srrs		net = NULL;
375163953Srrs		m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
376163953Srrs		    (caddr_t)&sh);
377163953Srrs		ip6cp->ip6c_src->sin6_port = sh.src_port;
378163953Srrs		final.sin6_len = sizeof(final);
379163953Srrs		final.sin6_family = AF_INET6;
380163953Srrs		final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
381163953Srrs		final.sin6_port = sh.dest_port;
382163953Srrs		stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
383163953Srrs		    (struct sockaddr *)&final,
384167598Srrs		    &inp, &net, 1, vrf_id);
385163953Srrs		/* inp's ref-count increased && stcb locked */
386163953Srrs		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
387163953Srrs			if (cmd == PRC_MSGSIZE) {
388163953Srrs				sctp6_notify_mbuf(inp,
389163953Srrs				    ip6cp->ip6c_icmp6,
390163953Srrs				    &sh,
391163953Srrs				    stcb,
392163953Srrs				    net);
393163953Srrs				/* inp's ref-count reduced && stcb unlocked */
394163953Srrs			} else {
395163953Srrs				if (cmd == PRC_HOSTDEAD) {
396163953Srrs					cm = EHOSTUNREACH;
397163953Srrs				} else {
398163953Srrs					cm = inet6ctlerrmap[cmd];
399163953Srrs				}
400163953Srrs				sctp_notify(inp, cm, &sh,
401163953Srrs				    (struct sockaddr *)&final,
402163953Srrs				    stcb, net);
403163953Srrs				/* inp's ref-count reduced && stcb unlocked */
404163953Srrs			}
405163953Srrs		} else {
406163953Srrs			if (PRC_IS_REDIRECT(cmd) && inp) {
407163953Srrs				in6_rtchange((struct in6pcb *)inp,
408163953Srrs				    inet6ctlerrmap[cmd]);
409163953Srrs			}
410163953Srrs			if (inp) {
411163953Srrs				/* reduce inp's ref-count */
412163953Srrs				SCTP_INP_WLOCK(inp);
413163953Srrs				SCTP_INP_DECR_REF(inp);
414163953Srrs				SCTP_INP_WUNLOCK(inp);
415163953Srrs			}
416163953Srrs			if (stcb)
417163953Srrs				SCTP_TCB_UNLOCK(stcb);
418163953Srrs		}
419163953Srrs	}
420163953Srrs}
421163953Srrs
422163953Srrs/*
423163953Srrs * this routine can probably be collasped into the one in sctp_userreq.c
424163953Srrs * since they do the same thing and now we lookup with a sockaddr
425163953Srrs */
426163953Srrsstatic int
427163953Srrssctp6_getcred(SYSCTL_HANDLER_ARGS)
428163953Srrs{
429164085Srrs	struct xucred xuc;
430163953Srrs	struct sockaddr_in6 addrs[2];
431163953Srrs	struct sctp_inpcb *inp;
432163953Srrs	struct sctp_nets *net;
433163953Srrs	struct sctp_tcb *stcb;
434164085Srrs	int error;
435167598Srrs	uint32_t vrf_id;
436163953Srrs
437167598Srrs	vrf_id = SCTP_DEFAULT_VRFID;
438167598Srrs
439170587Srwatson	error = priv_check(req->td, PRIV_NETINET_GETCRED);
440163953Srrs	if (error)
441163953Srrs		return (error);
442163953Srrs
443171943Srrs	if (req->newlen != sizeof(addrs)) {
444171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
445163953Srrs		return (EINVAL);
446171943Srrs	}
447171943Srrs	if (req->oldlen != sizeof(struct ucred)) {
448171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
449163953Srrs		return (EINVAL);
450171943Srrs	}
451163953Srrs	error = SYSCTL_IN(req, addrs, sizeof(addrs));
452163953Srrs	if (error)
453163953Srrs		return (error);
454163953Srrs
455163953Srrs	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
456163953Srrs	    sin6tosa(&addrs[1]),
457167598Srrs	    &inp, &net, 1, vrf_id);
458163953Srrs	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
459164085Srrs		if ((inp != NULL) && (stcb == NULL)) {
460164085Srrs			/* reduce ref-count */
461163953Srrs			SCTP_INP_WLOCK(inp);
462163953Srrs			SCTP_INP_DECR_REF(inp);
463164085Srrs			goto cred_can_cont;
464163953Srrs		}
465171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
466164085Srrs		error = ENOENT;
467163953Srrs		goto out;
468163953Srrs	}
469163953Srrs	SCTP_TCB_UNLOCK(stcb);
470164085Srrs	/*
471164085Srrs	 * We use the write lock here, only since in the error leg we need
472164085Srrs	 * it. If we used RLOCK, then we would have to
473164085Srrs	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
474164085Srrs	 * Better to use higher wlock.
475164085Srrs	 */
476164085Srrs	SCTP_INP_WLOCK(inp);
477164085Srrscred_can_cont:
478164085Srrs	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
479164085Srrs	if (error) {
480164085Srrs		SCTP_INP_WUNLOCK(inp);
481164085Srrs		goto out;
482164085Srrs	}
483164085Srrs	cru2x(inp->sctp_socket->so_cred, &xuc);
484164085Srrs	SCTP_INP_WUNLOCK(inp);
485164085Srrs	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
486163953Srrsout:
487163953Srrs	return (error);
488163953Srrs}
489163953Srrs
490163953SrrsSYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
491163953Srrs    0, 0,
492163953Srrs    sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
493163953Srrs
494163953Srrs
495163953Srrs/* This is the same as the sctp_abort() could be made common */
496163953Srrsstatic void
497163953Srrssctp6_abort(struct socket *so)
498163953Srrs{
499163953Srrs	struct sctp_inpcb *inp;
500163953Srrs	uint32_t flags;
501163953Srrs
502163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
503163953Srrs	if (inp == 0)
504163953Srrs		return;
505163953Srrssctp_must_try_again:
506163953Srrs	flags = inp->sctp_flags;
507163953Srrs#ifdef SCTP_LOG_CLOSING
508163953Srrs	sctp_log_closing(inp, NULL, 17);
509163953Srrs#endif
510163953Srrs	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
511163953Srrs	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
512163953Srrs#ifdef SCTP_LOG_CLOSING
513163953Srrs		sctp_log_closing(inp, NULL, 16);
514163953Srrs#endif
515169380Srrs		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
516169380Srrs		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
517163953Srrs		SOCK_LOCK(so);
518167695Srrs		SCTP_SB_CLEAR(so->so_snd);
519163953Srrs		/*
520163953Srrs		 * same for the rcv ones, they are only here for the
521163953Srrs		 * accounting/select.
522163953Srrs		 */
523167695Srrs		SCTP_SB_CLEAR(so->so_rcv);
524167695Srrs		/* Now null out the reference, we are completely detached. */
525163953Srrs		so->so_pcb = NULL;
526163953Srrs		SOCK_UNLOCK(so);
527163953Srrs	} else {
528163953Srrs		flags = inp->sctp_flags;
529163953Srrs		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
530163953Srrs			goto sctp_must_try_again;
531163953Srrs		}
532163953Srrs	}
533163953Srrs	return;
534163953Srrs}
535163953Srrs
536163953Srrsstatic int
537163953Srrssctp6_attach(struct socket *so, int proto, struct thread *p)
538163953Srrs{
539163953Srrs	struct in6pcb *inp6;
540166086Srrs	int error;
541163953Srrs	struct sctp_inpcb *inp;
542170205Srrs	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
543163953Srrs
544163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
545171943Srrs	if (inp != NULL) {
546171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
547163953Srrs		return EINVAL;
548171943Srrs	}
549163953Srrs	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
550167695Srrs		error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
551163953Srrs		if (error)
552163953Srrs			return error;
553163953Srrs	}
554170205Srrs	error = sctp_inpcb_alloc(so, vrf_id);
555163953Srrs	if (error)
556163953Srrs		return error;
557163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
558170205Srrs	SCTP_INP_WLOCK(inp);
559163953Srrs	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
560163953Srrs	inp6 = (struct in6pcb *)inp;
561163953Srrs
562163953Srrs	inp6->inp_vflag |= INP_IPV6;
563163953Srrs	inp6->in6p_hops = -1;	/* use kernel default */
564163953Srrs	inp6->in6p_cksum = -1;	/* just to be sure */
565163953Srrs#ifdef INET
566163953Srrs	/*
567163953Srrs	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
568163953Srrs	 * socket as well, because the socket may be bound to an IPv6
569163953Srrs	 * wildcard address, which may match an IPv4-mapped IPv6 address.
570163953Srrs	 */
571163953Srrs	inp6->inp_ip_ttl = ip_defttl;
572163953Srrs#endif
573163953Srrs	/*
574163953Srrs	 * Hmm what about the IPSEC stuff that is missing here but in
575163953Srrs	 * sctp_attach()?
576163953Srrs	 */
577170205Srrs	SCTP_INP_WUNLOCK(inp);
578163953Srrs	return 0;
579163953Srrs}
580163953Srrs
581163953Srrsstatic int
582163953Srrssctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
583163953Srrs{
584163953Srrs	struct sctp_inpcb *inp;
585163953Srrs	struct in6pcb *inp6;
586166086Srrs	int error;
587163953Srrs
588163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
589171943Srrs	if (inp == 0) {
590171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
591163953Srrs		return EINVAL;
592171943Srrs	}
593170056Srrs	if (addr) {
594170056Srrs		if ((addr->sa_family == AF_INET6) &&
595170056Srrs		    (addr->sa_len != sizeof(struct sockaddr_in6))) {
596171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
597170056Srrs			return EINVAL;
598170056Srrs		}
599170056Srrs		if ((addr->sa_family == AF_INET) &&
600170056Srrs		    (addr->sa_len != sizeof(struct sockaddr_in))) {
601171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
602170056Srrs			return EINVAL;
603170056Srrs		}
604170056Srrs	}
605163953Srrs	inp6 = (struct in6pcb *)inp;
606163953Srrs	inp6->inp_vflag &= ~INP_IPV4;
607163953Srrs	inp6->inp_vflag |= INP_IPV6;
608166023Srrs	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
609163953Srrs		if (addr->sa_family == AF_INET) {
610163953Srrs			/* binding v4 addr to v6 socket, so reset flags */
611163953Srrs			inp6->inp_vflag |= INP_IPV4;
612163953Srrs			inp6->inp_vflag &= ~INP_IPV6;
613163953Srrs		} else {
614163953Srrs			struct sockaddr_in6 *sin6_p;
615163953Srrs
616163953Srrs			sin6_p = (struct sockaddr_in6 *)addr;
617163953Srrs
618163953Srrs			if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
619163953Srrs				inp6->inp_vflag |= INP_IPV4;
620163953Srrs			} else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
621163953Srrs				struct sockaddr_in sin;
622163953Srrs
623163953Srrs				in6_sin6_2_sin(&sin, sin6_p);
624163953Srrs				inp6->inp_vflag |= INP_IPV4;
625163953Srrs				inp6->inp_vflag &= ~INP_IPV6;
626171572Srrs				error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
627163953Srrs				return error;
628163953Srrs			}
629163953Srrs		}
630163953Srrs	} else if (addr != NULL) {
631163953Srrs		/* IPV6_V6ONLY socket */
632163953Srrs		if (addr->sa_family == AF_INET) {
633163953Srrs			/* can't bind v4 addr to v6 only socket! */
634171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
635163953Srrs			return EINVAL;
636163953Srrs		} else {
637163953Srrs			struct sockaddr_in6 *sin6_p;
638163953Srrs
639163953Srrs			sin6_p = (struct sockaddr_in6 *)addr;
640163953Srrs
641163953Srrs			if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
642163953Srrs				/* can't bind v4-mapped addrs either! */
643163953Srrs				/* NOTE: we don't support SIIT */
644171943Srrs				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
645171943Srrs			return EINVAL;
646163953Srrs		}
647163953Srrs	}
648171572Srrs	error = sctp_inpcb_bind(so, addr, NULL, p);
649163953Srrs	return error;
650163953Srrs}
651163953Srrs
652163953Srrs
653163953Srrsstatic void
654163953Srrssctp6_close(struct socket *so)
655163953Srrs{
656163953Srrs	struct sctp_inpcb *inp;
657163953Srrs	uint32_t flags;
658163953Srrs
659163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
660163953Srrs	if (inp == 0)
661163953Srrs		return;
662163953Srrs
663163953Srrs	/*
664163953Srrs	 * Inform all the lower layer assoc that we are done.
665163953Srrs	 */
666163953Srrssctp_must_try_again:
667163953Srrs	flags = inp->sctp_flags;
668163953Srrs#ifdef SCTP_LOG_CLOSING
669163953Srrs	sctp_log_closing(inp, NULL, 17);
670163953Srrs#endif
671163953Srrs	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
672163953Srrs	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
673163953Srrs		if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
674163953Srrs		    (so->so_rcv.sb_cc > 0)) {
675163953Srrs#ifdef SCTP_LOG_CLOSING
676163953Srrs			sctp_log_closing(inp, NULL, 13);
677163953Srrs#endif
678169380Srrs			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT
679169380Srrs			    ,SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
680163953Srrs		} else {
681163953Srrs#ifdef SCTP_LOG_CLOSING
682163953Srrs			sctp_log_closing(inp, NULL, 14);
683163953Srrs#endif
684169380Srrs			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
685169380Srrs			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
686163953Srrs		}
687163953Srrs		/*
688163953Srrs		 * The socket is now detached, no matter what the state of
689163953Srrs		 * the SCTP association.
690163953Srrs		 */
691163953Srrs		SOCK_LOCK(so);
692167695Srrs		SCTP_SB_CLEAR(so->so_snd);
693163953Srrs		/*
694163953Srrs		 * same for the rcv ones, they are only here for the
695163953Srrs		 * accounting/select.
696163953Srrs		 */
697167695Srrs		SCTP_SB_CLEAR(so->so_rcv);
698167695Srrs		/* Now null out the reference, we are completely detached. */
699163953Srrs		so->so_pcb = NULL;
700163953Srrs		SOCK_UNLOCK(so);
701163953Srrs	} else {
702163953Srrs		flags = inp->sctp_flags;
703163953Srrs		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
704163953Srrs			goto sctp_must_try_again;
705163953Srrs		}
706163953Srrs	}
707163953Srrs	return;
708163953Srrs
709163953Srrs}
710163953Srrs
711167598Srrs/* This could be made common with sctp_detach() since they are identical */
712163953Srrs
713168709Srrsstatic
714168709Srrsint
715163953Srrssctp6_disconnect(struct socket *so)
716163953Srrs{
717163953Srrs	struct sctp_inpcb *inp;
718163953Srrs
719163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
720163953Srrs	if (inp == NULL) {
721171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
722163953Srrs		return (ENOTCONN);
723163953Srrs	}
724163953Srrs	SCTP_INP_RLOCK(inp);
725163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
726166675Srrs		if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
727163953Srrs			/* No connection */
728163953Srrs			SCTP_INP_RUNLOCK(inp);
729171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
730163953Srrs			return (ENOTCONN);
731163953Srrs		} else {
732163953Srrs			int some_on_streamwheel = 0;
733163953Srrs			struct sctp_association *asoc;
734163953Srrs			struct sctp_tcb *stcb;
735163953Srrs
736163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
737163953Srrs			if (stcb == NULL) {
738163953Srrs				SCTP_INP_RUNLOCK(inp);
739171943Srrs				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
740163953Srrs				return (EINVAL);
741163953Srrs			}
742163953Srrs			SCTP_TCB_LOCK(stcb);
743163953Srrs			asoc = &stcb->asoc;
744163953Srrs			if (((so->so_options & SO_LINGER) &&
745163953Srrs			    (so->so_linger == 0)) ||
746163953Srrs			    (so->so_rcv.sb_cc > 0)) {
747163953Srrs				if (SCTP_GET_STATE(asoc) !=
748163953Srrs				    SCTP_STATE_COOKIE_WAIT) {
749163953Srrs					/* Left with Data unread */
750168709Srrs					struct mbuf *op_err;
751163953Srrs
752168709Srrs					op_err = sctp_generate_invmanparam(SCTP_CAUSE_USER_INITIATED_ABT);
753168709Srrs					sctp_send_abort_tcb(stcb, op_err);
754163953Srrs					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
755163953Srrs				}
756163953Srrs				SCTP_INP_RUNLOCK(inp);
757163953Srrs				if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
758163953Srrs				    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
759163953Srrs					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
760163953Srrs				}
761171943Srrs				if (sctp_free_assoc(inp, stcb, SCTP_DONOT_SETSCOPE,
762171943Srrs				    SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_2) == 0) {
763171943Srrs					SCTP_TCB_UNLOCK(stcb);
764171943Srrs				}
765163953Srrs				/* No unlock tcb assoc is gone */
766163953Srrs				return (0);
767163953Srrs			}
768163953Srrs			if (!TAILQ_EMPTY(&asoc->out_wheel)) {
769163953Srrs				/* Check to see if some data queued */
770163953Srrs				struct sctp_stream_out *outs;
771163953Srrs
772163953Srrs				TAILQ_FOREACH(outs, &asoc->out_wheel,
773163953Srrs				    next_spoke) {
774163953Srrs					if (!TAILQ_EMPTY(&outs->outqueue)) {
775163953Srrs						some_on_streamwheel = 1;
776163953Srrs						break;
777163953Srrs					}
778163953Srrs				}
779163953Srrs			}
780163953Srrs			if (TAILQ_EMPTY(&asoc->send_queue) &&
781163953Srrs			    TAILQ_EMPTY(&asoc->sent_queue) &&
782163953Srrs			    (some_on_streamwheel == 0)) {
783163953Srrs				/* nothing queued to send, so I'm done... */
784163953Srrs				if ((SCTP_GET_STATE(asoc) !=
785163953Srrs				    SCTP_STATE_SHUTDOWN_SENT) &&
786163953Srrs				    (SCTP_GET_STATE(asoc) !=
787163953Srrs				    SCTP_STATE_SHUTDOWN_ACK_SENT)) {
788163953Srrs					/* only send SHUTDOWN the first time */
789163953Srrs					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
790163953Srrs					sctp_chunk_output(stcb->sctp_ep, stcb, 1);
791166675Srrs					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
792166675Srrs					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
793166675Srrs						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
794166675Srrs					}
795171943Srrs					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
796163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
797163953Srrs					    stcb->sctp_ep, stcb,
798163953Srrs					    asoc->primary_destination);
799163953Srrs					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
800163953Srrs					    stcb->sctp_ep, stcb,
801163953Srrs					    asoc->primary_destination);
802163953Srrs				}
803163953Srrs			} else {
804163953Srrs				/*
805163953Srrs				 * we still got (or just got) data to send,
806163953Srrs				 * so set SHUTDOWN_PENDING
807163953Srrs				 */
808163953Srrs				/*
809163953Srrs				 * XXX sockets draft says that MSG_EOF
810163953Srrs				 * should be sent with no data.  currently,
811163953Srrs				 * we will allow user data to be sent first
812163953Srrs				 * and move to SHUTDOWN-PENDING
813163953Srrs				 */
814163953Srrs				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
815163953Srrs			}
816163953Srrs			SCTP_TCB_UNLOCK(stcb);
817163953Srrs			SCTP_INP_RUNLOCK(inp);
818163953Srrs			return (0);
819163953Srrs		}
820163953Srrs	} else {
821163953Srrs		/* UDP model does not support this */
822163953Srrs		SCTP_INP_RUNLOCK(inp);
823171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EOPNOTSUPP);
824163953Srrs		return EOPNOTSUPP;
825163953Srrs	}
826163953Srrs}
827163953Srrs
828168709Srrs
829163953Srrsint
830163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
831163953Srrs    struct mbuf *control, struct thread *p);
832163953Srrs
833163953Srrs
834163953Srrsstatic int
835163953Srrssctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
836163953Srrs    struct mbuf *control, struct thread *p)
837163953Srrs{
838163953Srrs	struct sctp_inpcb *inp;
839163953Srrs	struct inpcb *in_inp;
840163953Srrs	struct in6pcb *inp6;
841163953Srrs
842163953Srrs#ifdef INET
843163953Srrs	struct sockaddr_in6 *sin6;
844163953Srrs
845163953Srrs#endif				/* INET */
846163953Srrs	/* No SPL needed since sctp_output does this */
847163953Srrs
848163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
849163953Srrs	if (inp == NULL) {
850163953Srrs		if (control) {
851169352Srrs			SCTP_RELEASE_PKT(control);
852163953Srrs			control = NULL;
853163953Srrs		}
854169352Srrs		SCTP_RELEASE_PKT(m);
855171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
856163953Srrs		return EINVAL;
857163953Srrs	}
858163953Srrs	in_inp = (struct inpcb *)inp;
859163953Srrs	inp6 = (struct in6pcb *)inp;
860163953Srrs	/*
861163953Srrs	 * For the TCP model we may get a NULL addr, if we are a connected
862163953Srrs	 * socket thats ok.
863163953Srrs	 */
864163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
865163953Srrs	    (addr == NULL)) {
866163953Srrs		goto connected_type;
867163953Srrs	}
868163953Srrs	if (addr == NULL) {
869169352Srrs		SCTP_RELEASE_PKT(m);
870163953Srrs		if (control) {
871169352Srrs			SCTP_RELEASE_PKT(control);
872163953Srrs			control = NULL;
873163953Srrs		}
874171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
875163953Srrs		return (EDESTADDRREQ);
876163953Srrs	}
877163953Srrs#ifdef INET
878163953Srrs	sin6 = (struct sockaddr_in6 *)addr;
879166023Srrs	if (SCTP_IPV6_V6ONLY(inp6)) {
880163953Srrs		/*
881163953Srrs		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
882163953Srrs		 * v4 addr or v4-mapped addr
883163953Srrs		 */
884163953Srrs		if (addr->sa_family == AF_INET) {
885171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
886163953Srrs			return EINVAL;
887163953Srrs		}
888163953Srrs		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
889171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
890163953Srrs			return EINVAL;
891163953Srrs		}
892163953Srrs	}
893163953Srrs	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
894163953Srrs		if (!ip6_v6only) {
895163953Srrs			struct sockaddr_in sin;
896163953Srrs
897163953Srrs			/* convert v4-mapped into v4 addr and send */
898163953Srrs			in6_sin6_2_sin(&sin, sin6);
899163953Srrs			return sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
900163953Srrs			    control, p);
901163953Srrs		} else {
902163953Srrs			/* mapped addresses aren't enabled */
903171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
904163953Srrs			return EINVAL;
905163953Srrs		}
906163953Srrs	}
907163953Srrs#endif				/* INET */
908163953Srrsconnected_type:
909163953Srrs	/* now what about control */
910163953Srrs	if (control) {
911163953Srrs		if (inp->control) {
912169420Srrs			SCTP_PRINTF("huh? control set?\n");
913169352Srrs			SCTP_RELEASE_PKT(inp->control);
914163953Srrs			inp->control = NULL;
915163953Srrs		}
916163953Srrs		inp->control = control;
917163953Srrs	}
918163953Srrs	/* Place the data */
919163953Srrs	if (inp->pkt) {
920165647Srrs		SCTP_BUF_NEXT(inp->pkt_last) = m;
921163953Srrs		inp->pkt_last = m;
922163953Srrs	} else {
923163953Srrs		inp->pkt_last = inp->pkt = m;
924163953Srrs	}
925163953Srrs	if (
926163953Srrs	/* FreeBSD and MacOSX uses a flag passed */
927163953Srrs	    ((flags & PRUS_MORETOCOME) == 0)
928163953Srrs	    ) {
929163953Srrs		/*
930163953Srrs		 * note with the current version this code will only be used
931163953Srrs		 * by OpenBSD, NetBSD and FreeBSD have methods for
932163953Srrs		 * re-defining sosend() to use sctp_sosend().  One can
933163953Srrs		 * optionaly switch back to this code (by changing back the
934163953Srrs		 * defininitions but this is not advisable.
935163953Srrs		 */
936163953Srrs		int ret;
937163953Srrs
938163953Srrs		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
939163953Srrs		inp->pkt = NULL;
940163953Srrs		inp->control = NULL;
941163953Srrs		return (ret);
942163953Srrs	} else {
943163953Srrs		return (0);
944163953Srrs	}
945163953Srrs}
946163953Srrs
947163953Srrsstatic int
948163953Srrssctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
949163953Srrs{
950167598Srrs	uint32_t vrf_id;
951163953Srrs	int error = 0;
952163953Srrs	struct sctp_inpcb *inp;
953163953Srrs	struct in6pcb *inp6;
954163953Srrs	struct sctp_tcb *stcb;
955163953Srrs
956163953Srrs#ifdef INET
957163953Srrs	struct sockaddr_in6 *sin6;
958163953Srrs	struct sockaddr_storage ss;
959163953Srrs
960163953Srrs#endif				/* INET */
961163953Srrs
962163953Srrs	inp6 = (struct in6pcb *)so->so_pcb;
963163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
964163953Srrs	if (inp == 0) {
965171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
966163953Srrs		return (ECONNRESET);	/* I made the same as TCP since we are
967163953Srrs					 * not setup? */
968163953Srrs	}
969170056Srrs	if (addr == NULL) {
970171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
971170056Srrs		return (EINVAL);
972170056Srrs	}
973170056Srrs	if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
974171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
975170056Srrs		return (EINVAL);
976170056Srrs	}
977170056Srrs	if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
978171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
979170056Srrs		return (EINVAL);
980170056Srrs	}
981168299Srrs	vrf_id = inp->def_vrf_id;
982163953Srrs	SCTP_ASOC_CREATE_LOCK(inp);
983163953Srrs	SCTP_INP_RLOCK(inp);
984163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
985163953Srrs	    SCTP_PCB_FLAGS_UNBOUND) {
986163953Srrs		/* Bind a ephemeral port */
987163953Srrs		SCTP_INP_RUNLOCK(inp);
988163953Srrs		error = sctp6_bind(so, NULL, p);
989163953Srrs		if (error) {
990163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
991163953Srrs
992163953Srrs			return (error);
993163953Srrs		}
994163953Srrs		SCTP_INP_RLOCK(inp);
995163953Srrs	}
996163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
997163953Srrs	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
998163953Srrs		/* We are already connected AND the TCP model */
999163953Srrs		SCTP_INP_RUNLOCK(inp);
1000163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
1001171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
1002163953Srrs		return (EADDRINUSE);
1003163953Srrs	}
1004163953Srrs#ifdef INET
1005163953Srrs	sin6 = (struct sockaddr_in6 *)addr;
1006166023Srrs	if (SCTP_IPV6_V6ONLY(inp6)) {
1007163953Srrs		/*
1008163953Srrs		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
1009163953Srrs		 * addr or v4-mapped addr
1010163953Srrs		 */
1011163953Srrs		if (addr->sa_family == AF_INET) {
1012163953Srrs			SCTP_INP_RUNLOCK(inp);
1013163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
1014171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1015163953Srrs			return EINVAL;
1016163953Srrs		}
1017163953Srrs		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1018163953Srrs			SCTP_INP_RUNLOCK(inp);
1019163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
1020171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1021163953Srrs			return EINVAL;
1022163953Srrs		}
1023163953Srrs	}
1024163953Srrs	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1025163953Srrs		if (!ip6_v6only) {
1026163953Srrs			/* convert v4-mapped into v4 addr */
1027163953Srrs			in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1028163953Srrs			addr = (struct sockaddr *)&ss;
1029163953Srrs		} else {
1030163953Srrs			/* mapped addresses aren't enabled */
1031163953Srrs			SCTP_INP_RUNLOCK(inp);
1032163953Srrs			SCTP_ASOC_CREATE_UNLOCK(inp);
1033171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1034163953Srrs			return EINVAL;
1035163953Srrs		}
1036163953Srrs	} else
1037163953Srrs#endif				/* INET */
1038163953Srrs		addr = addr;	/* for true v6 address case */
1039163953Srrs
1040163953Srrs	/* Now do we connect? */
1041163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1042163953Srrs		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1043169420Srrs		if (stcb) {
1044163953Srrs			SCTP_TCB_UNLOCK(stcb);
1045169420Srrs		}
1046163953Srrs		SCTP_INP_RUNLOCK(inp);
1047163953Srrs	} else {
1048163953Srrs		SCTP_INP_RUNLOCK(inp);
1049163953Srrs		SCTP_INP_WLOCK(inp);
1050163953Srrs		SCTP_INP_INCR_REF(inp);
1051163953Srrs		SCTP_INP_WUNLOCK(inp);
1052163953Srrs		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1053163953Srrs		if (stcb == NULL) {
1054163953Srrs			SCTP_INP_WLOCK(inp);
1055163953Srrs			SCTP_INP_DECR_REF(inp);
1056163953Srrs			SCTP_INP_WUNLOCK(inp);
1057163953Srrs		}
1058163953Srrs	}
1059163953Srrs
1060163953Srrs	if (stcb != NULL) {
1061163953Srrs		/* Already have or am bring up an association */
1062163953Srrs		SCTP_ASOC_CREATE_UNLOCK(inp);
1063163953Srrs		SCTP_TCB_UNLOCK(stcb);
1064171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
1065163953Srrs		return (EALREADY);
1066163953Srrs	}
1067163953Srrs	/* We are GOOD to go */
1068171531Srrs	stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
1069163953Srrs	SCTP_ASOC_CREATE_UNLOCK(inp);
1070163953Srrs	if (stcb == NULL) {
1071163953Srrs		/* Gak! no memory */
1072163953Srrs		return (error);
1073163953Srrs	}
1074163953Srrs	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1075163953Srrs		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1076163953Srrs		/* Set the connected flag so we can queue data */
1077163953Srrs		soisconnecting(so);
1078163953Srrs	}
1079163953Srrs	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1080169420Srrs	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1081163953Srrs
1082163953Srrs	/* initialize authentication parameters for the assoc */
1083163953Srrs	sctp_initialize_auth_params(inp, stcb);
1084163953Srrs
1085163953Srrs	sctp_send_initiate(inp, stcb);
1086163953Srrs	SCTP_TCB_UNLOCK(stcb);
1087163953Srrs	return error;
1088163953Srrs}
1089163953Srrs
1090163953Srrsstatic int
1091163953Srrssctp6_getaddr(struct socket *so, struct sockaddr **addr)
1092163953Srrs{
1093163953Srrs	struct sockaddr_in6 *sin6;
1094163953Srrs	struct sctp_inpcb *inp;
1095167598Srrs	uint32_t vrf_id;
1096167598Srrs	struct sctp_ifa *sctp_ifa;
1097163953Srrs
1098163953Srrs	int error;
1099163953Srrs
1100163953Srrs	/*
1101163953Srrs	 * Do the malloc first in case it blocks.
1102163953Srrs	 */
1103163953Srrs	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1104163953Srrs	sin6->sin6_family = AF_INET6;
1105163953Srrs	sin6->sin6_len = sizeof(*sin6);
1106163953Srrs
1107163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1108163953Srrs	if (inp == NULL) {
1109163953Srrs		SCTP_FREE_SONAME(sin6);
1110171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1111163953Srrs		return ECONNRESET;
1112163953Srrs	}
1113163953Srrs	SCTP_INP_RLOCK(inp);
1114163953Srrs	sin6->sin6_port = inp->sctp_lport;
1115163953Srrs	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1116163953Srrs		/* For the bound all case you get back 0 */
1117163953Srrs		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1118163953Srrs			struct sctp_tcb *stcb;
1119163953Srrs			struct sockaddr_in6 *sin_a6;
1120163953Srrs			struct sctp_nets *net;
1121163953Srrs			int fnd;
1122163953Srrs
1123163953Srrs			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1124163953Srrs			if (stcb == NULL) {
1125163953Srrs				goto notConn6;
1126163953Srrs			}
1127163953Srrs			fnd = 0;
1128163953Srrs			sin_a6 = NULL;
1129163953Srrs			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1130163953Srrs				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1131164085Srrs				if (sin_a6 == NULL)
1132164085Srrs					/* this will make coverity happy */
1133164085Srrs					continue;
1134164085Srrs
1135163953Srrs				if (sin_a6->sin6_family == AF_INET6) {
1136163953Srrs					fnd = 1;
1137163953Srrs					break;
1138163953Srrs				}
1139163953Srrs			}
1140163953Srrs			if ((!fnd) || (sin_a6 == NULL)) {
1141163953Srrs				/* punt */
1142163953Srrs				goto notConn6;
1143163953Srrs			}
1144168299Srrs			vrf_id = inp->def_vrf_id;
1145168299Srrs			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1146167598Srrs			if (sctp_ifa) {
1147167598Srrs				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1148167598Srrs			}
1149163953Srrs		} else {
1150163953Srrs			/* For the bound all case you get back 0 */
1151163953Srrs	notConn6:
1152163953Srrs			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1153163953Srrs		}
1154163953Srrs	} else {
1155163953Srrs		/* Take the first IPv6 address in the list */
1156163953Srrs		struct sctp_laddr *laddr;
1157163953Srrs		int fnd = 0;
1158163953Srrs
1159163953Srrs		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1160167598Srrs			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1161163953Srrs				struct sockaddr_in6 *sin_a;
1162163953Srrs
1163167598Srrs				sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1164163953Srrs				sin6->sin6_addr = sin_a->sin6_addr;
1165163953Srrs				fnd = 1;
1166163953Srrs				break;
1167163953Srrs			}
1168163953Srrs		}
1169163953Srrs		if (!fnd) {
1170163953Srrs			SCTP_FREE_SONAME(sin6);
1171163953Srrs			SCTP_INP_RUNLOCK(inp);
1172171943Srrs			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1173163953Srrs			return ENOENT;
1174163953Srrs		}
1175163953Srrs	}
1176163953Srrs	SCTP_INP_RUNLOCK(inp);
1177163953Srrs	/* Scoping things for v6 */
1178164085Srrs	if ((error = sa6_recoverscope(sin6)) != 0) {
1179164085Srrs		SCTP_FREE_SONAME(sin6);
1180163953Srrs		return (error);
1181164085Srrs	}
1182163953Srrs	(*addr) = (struct sockaddr *)sin6;
1183163953Srrs	return (0);
1184163953Srrs}
1185163953Srrs
1186163953Srrsstatic int
1187163953Srrssctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1188163953Srrs{
1189163953Srrs	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1190163953Srrs	int fnd;
1191163953Srrs	struct sockaddr_in6 *sin_a6;
1192163953Srrs	struct sctp_inpcb *inp;
1193163953Srrs	struct sctp_tcb *stcb;
1194163953Srrs	struct sctp_nets *net;
1195163953Srrs
1196163953Srrs	int error;
1197163953Srrs
1198163953Srrs	/*
1199163953Srrs	 * Do the malloc first in case it blocks.
1200163953Srrs	 */
1201163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1202163953Srrs	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1203163953Srrs		/* UDP type and listeners will drop out here */
1204171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1205163953Srrs		return (ENOTCONN);
1206163953Srrs	}
1207163953Srrs	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1208163953Srrs	sin6->sin6_family = AF_INET6;
1209163953Srrs	sin6->sin6_len = sizeof(*sin6);
1210163953Srrs
1211163953Srrs	/* We must recapture incase we blocked */
1212163953Srrs	inp = (struct sctp_inpcb *)so->so_pcb;
1213163953Srrs	if (inp == NULL) {
1214163953Srrs		SCTP_FREE_SONAME(sin6);
1215171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1216163953Srrs		return ECONNRESET;
1217163953Srrs	}
1218163953Srrs	SCTP_INP_RLOCK(inp);
1219163953Srrs	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1220169420Srrs	if (stcb) {
1221163953Srrs		SCTP_TCB_LOCK(stcb);
1222169420Srrs	}
1223163953Srrs	SCTP_INP_RUNLOCK(inp);
1224163953Srrs	if (stcb == NULL) {
1225163953Srrs		SCTP_FREE_SONAME(sin6);
1226171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1227163953Srrs		return ECONNRESET;
1228163953Srrs	}
1229163953Srrs	fnd = 0;
1230163953Srrs	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1231163953Srrs		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1232163953Srrs		if (sin_a6->sin6_family == AF_INET6) {
1233163953Srrs			fnd = 1;
1234163953Srrs			sin6->sin6_port = stcb->rport;
1235163953Srrs			sin6->sin6_addr = sin_a6->sin6_addr;
1236163953Srrs			break;
1237163953Srrs		}
1238163953Srrs	}
1239163953Srrs	SCTP_TCB_UNLOCK(stcb);
1240163953Srrs	if (!fnd) {
1241163953Srrs		/* No IPv4 address */
1242163953Srrs		SCTP_FREE_SONAME(sin6);
1243171943Srrs		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1244163953Srrs		return ENOENT;
1245163953Srrs	}
1246163953Srrs	if ((error = sa6_recoverscope(sin6)) != 0)
1247163953Srrs		return (error);
1248163953Srrs	*addr = (struct sockaddr *)sin6;
1249163953Srrs	return (0);
1250163953Srrs}
1251163953Srrs
1252163953Srrsstatic int
1253163953Srrssctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1254163953Srrs{
1255163953Srrs	struct sockaddr *addr;
1256163953Srrs	struct in6pcb *inp6 = sotoin6pcb(so);
1257166086Srrs	int error;
1258163953Srrs
1259171943Srrs	if (inp6 == NULL) {
1260171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1261163953Srrs		return EINVAL;
1262171943Srrs	}
1263163953Srrs	/* allow v6 addresses precedence */
1264163953Srrs	error = sctp6_getaddr(so, nam);
1265163953Srrs	if (error) {
1266163953Srrs		/* try v4 next if v6 failed */
1267163953Srrs		error = sctp_ingetaddr(so, nam);
1268163953Srrs		if (error) {
1269163953Srrs			return (error);
1270163953Srrs		}
1271163953Srrs		addr = *nam;
1272163953Srrs		/* if I'm V6ONLY, convert it to v4-mapped */
1273166023Srrs		if (SCTP_IPV6_V6ONLY(inp6)) {
1274163953Srrs			struct sockaddr_in6 sin6;
1275163953Srrs
1276163953Srrs			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1277163953Srrs			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1278168709Srrs
1279163953Srrs		}
1280163953Srrs	}
1281163953Srrs	return (error);
1282163953Srrs}
1283163953Srrs
1284163953Srrs
1285163953Srrsstatic int
1286163953Srrssctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1287163953Srrs{
1288163953Srrs	struct sockaddr *addr = *nam;
1289163953Srrs	struct in6pcb *inp6 = sotoin6pcb(so);
1290166086Srrs	int error;
1291163953Srrs
1292171943Srrs	if (inp6 == NULL) {
1293171943Srrs		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1294163953Srrs		return EINVAL;
1295171943Srrs	}
1296163953Srrs	/* allow v6 addresses precedence */
1297163953Srrs	error = sctp6_peeraddr(so, nam);
1298163953Srrs	if (error) {
1299163953Srrs		/* try v4 next if v6 failed */
1300163953Srrs		error = sctp_peeraddr(so, nam);
1301163953Srrs		if (error) {
1302163953Srrs			return (error);
1303163953Srrs		}
1304163953Srrs		/* if I'm V6ONLY, convert it to v4-mapped */
1305166023Srrs		if (SCTP_IPV6_V6ONLY(inp6)) {
1306163953Srrs			struct sockaddr_in6 sin6;
1307163953Srrs
1308163953Srrs			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1309163953Srrs			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1310163953Srrs		}
1311163953Srrs	}
1312163953Srrs	return error;
1313163953Srrs}
1314163953Srrs
1315163953Srrsstruct pr_usrreqs sctp6_usrreqs = {
1316163953Srrs	.pru_abort = sctp6_abort,
1317163953Srrs	.pru_accept = sctp_accept,
1318163953Srrs	.pru_attach = sctp6_attach,
1319163953Srrs	.pru_bind = sctp6_bind,
1320163953Srrs	.pru_connect = sctp6_connect,
1321163953Srrs	.pru_control = in6_control,
1322163953Srrs	.pru_close = sctp6_close,
1323163953Srrs	.pru_detach = sctp6_close,
1324163953Srrs	.pru_sopoll = sopoll_generic,
1325163953Srrs	.pru_disconnect = sctp6_disconnect,
1326163953Srrs	.pru_listen = sctp_listen,
1327163953Srrs	.pru_peeraddr = sctp6_getpeeraddr,
1328163953Srrs	.pru_send = sctp6_send,
1329163953Srrs	.pru_shutdown = sctp_shutdown,
1330163953Srrs	.pru_sockaddr = sctp6_in6getaddr,
1331163953Srrs	.pru_sosend = sctp_sosend,
1332163953Srrs	.pru_soreceive = sctp_soreceive
1333163953Srrs};
1334