sctp6_usrreq.c revision 170181
1/*-
2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * a) Redistributions of source code must retain the above copyright notice,
8 *   this list of conditions and the following disclaimer.
9 *
10 * b) Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *   the documentation and/or other materials provided with the distribution.
13 *
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 *    contributors may be used to endorse or promote products derived
16 *    from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
29 */
30/*	$KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $	*/
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sys/netinet6/sctp6_usrreq.c 170181 2007-06-01 11:19:54Z rrs $");
33
34
35#include <netinet/sctp_os.h>
36#include <sys/proc.h>
37#include <netinet/sctp_pcb.h>
38#include <netinet/sctp_header.h>
39#include <netinet/sctp_var.h>
40#if defined(INET6)
41#include <netinet6/sctp6_var.h>
42#endif
43#include <netinet/sctp_sysctl.h>
44#include <netinet/sctp_output.h>
45#include <netinet/sctp_uio.h>
46#include <netinet/sctp_asconf.h>
47#include <netinet/sctputil.h>
48#include <netinet/sctp_indata.h>
49#include <netinet/sctp_timer.h>
50#include <netinet/sctp_auth.h>
51#include <netinet/sctp_input.h>
52#include <netinet/sctp_output.h>
53#include <netinet/sctp_bsd_addr.h>
54
55
56extern struct protosw inetsw[];
57
58
59
60
61int
62sctp6_input(i_pak, offp, proto)
63	struct mbuf **i_pak;
64	int *offp;
65	int proto;
66{
67	struct mbuf *m;
68	struct ip6_hdr *ip6;
69	struct sctphdr *sh;
70	struct sctp_inpcb *in6p = NULL;
71	struct sctp_nets *net;
72	int refcount_up = 0;
73	uint32_t check, calc_check;
74	uint32_t vrf_id = 0;
75	struct inpcb *in6p_ip;
76	struct sctp_chunkhdr *ch;
77	int length, mlen, offset, iphlen;
78	uint8_t ecn_bits;
79	struct sctp_tcb *stcb = NULL;
80	int pkt_len = 0;
81	int off = *offp;
82
83	/* get the VRF and table id's */
84	if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
85		SCTP_RELEASE_PKT(*i_pak);
86		return (-1);
87	}
88	m = SCTP_HEADER_TO_CHAIN(*i_pak);
89	pkt_len = SCTP_HEADER_LEN((*i_pak));
90
91#ifdef  SCTP_PACKET_LOGGING
92	sctp_packet_log(m, pkt_len);
93#endif
94	ip6 = mtod(m, struct ip6_hdr *);
95	/* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
96	IP6_EXTHDR_GET(sh, struct sctphdr *, m, off,
97	    (int)(sizeof(*sh) + sizeof(*ch)));
98	if (sh == NULL) {
99		SCTP_STAT_INCR(sctps_hdrops);
100		return IPPROTO_DONE;
101	}
102	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
103	iphlen = off;
104	offset = iphlen + sizeof(*sh) + sizeof(*ch);
105
106#if defined(NFAITH) && NFAITH > 0
107
108	if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) {
109		/* XXX send icmp6 host/port unreach? */
110		goto bad;
111	}
112#endif				/* NFAITH defined and > 0 */
113	SCTP_STAT_INCR(sctps_recvpackets);
114	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
115	SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n",
116	    iphlen, pkt_len);
117	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
118		/* No multi-cast support in SCTP */
119		goto bad;
120	}
121	/* destination port of 0 is illegal, based on RFC2960. */
122	if (sh->dest_port == 0)
123		goto bad;
124	if ((sctp_no_csum_on_loopback == 0) ||
125	    (!SCTP_IS_IT_LOOPBACK(m))) {
126		/*
127		 * we do NOT validate things from the loopback if the sysctl
128		 * is set to 1.
129		 */
130		check = sh->checksum;	/* save incoming checksum */
131		if ((check == 0) && (sctp_no_csum_on_loopback)) {
132			/*
133			 * special hook for where we got a local address
134			 * somehow routed across a non IFT_LOOP type
135			 * interface
136			 */
137			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))
138				goto sctp_skip_csum;
139		}
140		sh->checksum = 0;	/* prepare for calc */
141		calc_check = sctp_calculate_sum(m, &mlen, iphlen);
142		if (calc_check != check) {
143			SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
144			    calc_check, check, m, mlen, iphlen);
145			stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
146			    sh, ch, &in6p, &net, vrf_id);
147			/* in6p's ref-count increased && stcb locked */
148			if ((in6p) && (stcb)) {
149				sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
150				sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, 2);
151			} else if ((in6p != NULL) && (stcb == NULL)) {
152				refcount_up = 1;
153			}
154			SCTP_STAT_INCR(sctps_badsum);
155			SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
156			goto bad;
157		}
158		sh->checksum = calc_check;
159	}
160sctp_skip_csum:
161	net = NULL;
162	/*
163	 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
164	 * IP/SCTP/first chunk header...
165	 */
166	stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
167	    sh, ch, &in6p, &net, vrf_id);
168	/* in6p's ref-count increased */
169	if (in6p == NULL) {
170		struct sctp_init_chunk *init_chk, chunk_buf;
171
172		SCTP_STAT_INCR(sctps_noport);
173		if (ch->chunk_type == SCTP_INITIATION) {
174			/*
175			 * we do a trick here to get the INIT tag, dig in
176			 * and get the tag from the INIT and put it in the
177			 * common header.
178			 */
179			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
180			    iphlen + sizeof(*sh), sizeof(*init_chk),
181			    (uint8_t *) & chunk_buf);
182			if (init_chk)
183				sh->v_tag = init_chk->init.initiate_tag;
184			else
185				sh->v_tag = 0;
186		}
187		if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
188			sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id);
189			goto bad;
190		}
191		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
192			goto bad;
193		}
194		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
195			sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id);
196		goto bad;
197	} else if (stcb == NULL) {
198		refcount_up = 1;
199	}
200	in6p_ip = (struct inpcb *)in6p;
201#ifdef IPSEC
202	/*
203	 * Check AH/ESP integrity.
204	 */
205	if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) {
206/* XXX */
207		ipsec6stat.in_polvio++;
208		goto bad;
209	}
210#endif				/* IPSEC */
211
212	/*
213	 * CONTROL chunk processing
214	 */
215	offset -= sizeof(*ch);
216	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
217
218	/* Length now holds the total packet length payload + iphlen */
219	length = ntohs(ip6->ip6_plen) + iphlen;
220
221	/* sa_ignore NO_NULL_CHK */
222	sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
223	    in6p, stcb, net, ecn_bits, vrf_id);
224	/* inp's ref-count reduced && stcb unlocked */
225	/* XXX this stuff below gets moved to appropriate parts later... */
226	if (m)
227		sctp_m_freem(m);
228	if ((in6p) && refcount_up) {
229		/* reduce ref-count */
230		SCTP_INP_WLOCK(in6p);
231		SCTP_INP_DECR_REF(in6p);
232		SCTP_INP_WUNLOCK(in6p);
233	}
234	return IPPROTO_DONE;
235
236bad:
237	if (stcb) {
238		SCTP_TCB_UNLOCK(stcb);
239	}
240	if ((in6p) && refcount_up) {
241		/* reduce ref-count */
242		SCTP_INP_WLOCK(in6p);
243		SCTP_INP_DECR_REF(in6p);
244		SCTP_INP_WUNLOCK(in6p);
245	}
246	if (m)
247		sctp_m_freem(m);
248	return IPPROTO_DONE;
249}
250
251
252static void
253sctp6_notify_mbuf(struct sctp_inpcb *inp,
254    struct icmp6_hdr *icmp6,
255    struct sctphdr *sh,
256    struct sctp_tcb *stcb,
257    struct sctp_nets *net)
258{
259	uint32_t nxtsz;
260
261	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
262	    (icmp6 == NULL) || (sh == NULL)) {
263		goto out;
264	}
265	/* First do we even look at it? */
266	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
267		goto out;
268
269	if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
270		/* not PACKET TO BIG */
271		goto out;
272	}
273	/*
274	 * ok we need to look closely. We could even get smarter and look at
275	 * anyone that we sent to in case we get a different ICMP that tells
276	 * us there is no way to reach a host, but for this impl, all we
277	 * care about is MTU discovery.
278	 */
279	nxtsz = ntohl(icmp6->icmp6_mtu);
280	/* Stop any PMTU timer */
281	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
282
283	/* Adjust destination size limit */
284	if (net->mtu > nxtsz) {
285		net->mtu = nxtsz;
286	}
287	/* now what about the ep? */
288	if (stcb->asoc.smallest_mtu > nxtsz) {
289		struct sctp_tmit_chunk *chk;
290
291		/* Adjust that too */
292		stcb->asoc.smallest_mtu = nxtsz;
293		/* now off to subtract IP_DF flag if needed */
294
295		TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
296			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
297				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
298			}
299		}
300		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
301			if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
302				/*
303				 * For this guy we also mark for immediate
304				 * resend since we sent to big of chunk
305				 */
306				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
307				if (chk->sent != SCTP_DATAGRAM_RESEND)
308					stcb->asoc.sent_queue_retran_cnt++;
309				chk->sent = SCTP_DATAGRAM_RESEND;
310				chk->rec.data.doing_fast_retransmit = 0;
311
312				chk->sent = SCTP_DATAGRAM_RESEND;
313				/* Clear any time so NO RTT is being done */
314				chk->sent_rcv_time.tv_sec = 0;
315				chk->sent_rcv_time.tv_usec = 0;
316				stcb->asoc.total_flight -= chk->send_size;
317				net->flight_size -= chk->send_size;
318			}
319		}
320	}
321	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
322out:
323	if (stcb) {
324		SCTP_TCB_UNLOCK(stcb);
325	}
326}
327
328
329void
330sctp6_ctlinput(cmd, pktdst, d)
331	int cmd;
332	struct sockaddr *pktdst;
333	void *d;
334{
335	struct sctphdr sh;
336	struct ip6ctlparam *ip6cp = NULL;
337	uint32_t vrf_id;
338	int cm;
339
340	vrf_id = SCTP_DEFAULT_VRFID;
341
342	if (pktdst->sa_family != AF_INET6 ||
343	    pktdst->sa_len != sizeof(struct sockaddr_in6))
344		return;
345
346	if ((unsigned)cmd >= PRC_NCMDS)
347		return;
348	if (PRC_IS_REDIRECT(cmd)) {
349		d = NULL;
350	} else if (inet6ctlerrmap[cmd] == 0) {
351		return;
352	}
353	/* if the parameter is from icmp6, decode it. */
354	if (d != NULL) {
355		ip6cp = (struct ip6ctlparam *)d;
356	} else {
357		ip6cp = (struct ip6ctlparam *)NULL;
358	}
359
360	if (ip6cp) {
361		/*
362		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
363		 * valid.
364		 */
365		/* check if we can safely examine src and dst ports */
366		struct sctp_inpcb *inp = NULL;
367		struct sctp_tcb *stcb = NULL;
368		struct sctp_nets *net = NULL;
369		struct sockaddr_in6 final;
370
371		if (ip6cp->ip6c_m == NULL)
372			return;
373
374		bzero(&sh, sizeof(sh));
375		bzero(&final, sizeof(final));
376		inp = NULL;
377		net = NULL;
378		m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
379		    (caddr_t)&sh);
380		ip6cp->ip6c_src->sin6_port = sh.src_port;
381		final.sin6_len = sizeof(final);
382		final.sin6_family = AF_INET6;
383		final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
384		final.sin6_port = sh.dest_port;
385		stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
386		    (struct sockaddr *)&final,
387		    &inp, &net, 1, vrf_id);
388		/* inp's ref-count increased && stcb locked */
389		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
390			if (cmd == PRC_MSGSIZE) {
391				sctp6_notify_mbuf(inp,
392				    ip6cp->ip6c_icmp6,
393				    &sh,
394				    stcb,
395				    net);
396				/* inp's ref-count reduced && stcb unlocked */
397			} else {
398				if (cmd == PRC_HOSTDEAD) {
399					cm = EHOSTUNREACH;
400				} else {
401					cm = inet6ctlerrmap[cmd];
402				}
403				sctp_notify(inp, cm, &sh,
404				    (struct sockaddr *)&final,
405				    stcb, net);
406				/* inp's ref-count reduced && stcb unlocked */
407			}
408		} else {
409			if (PRC_IS_REDIRECT(cmd) && inp) {
410				in6_rtchange((struct in6pcb *)inp,
411				    inet6ctlerrmap[cmd]);
412			}
413			if (inp) {
414				/* reduce inp's ref-count */
415				SCTP_INP_WLOCK(inp);
416				SCTP_INP_DECR_REF(inp);
417				SCTP_INP_WUNLOCK(inp);
418			}
419			if (stcb)
420				SCTP_TCB_UNLOCK(stcb);
421		}
422	}
423}
424
425/*
426 * this routine can probably be collasped into the one in sctp_userreq.c
427 * since they do the same thing and now we lookup with a sockaddr
428 */
429static int
430sctp6_getcred(SYSCTL_HANDLER_ARGS)
431{
432	struct xucred xuc;
433	struct sockaddr_in6 addrs[2];
434	struct sctp_inpcb *inp;
435	struct sctp_nets *net;
436	struct sctp_tcb *stcb;
437	int error;
438	uint32_t vrf_id;
439
440	vrf_id = SCTP_DEFAULT_VRFID;
441
442	/*
443	 * XXXRW: Other instances of getcred use SUSER_ALLOWJAIL, as socket
444	 * visibility is scoped using cr_canseesocket(), which it is not
445	 * here.
446	 */
447	error = priv_check_cred(req->td->td_ucred, PRIV_NETINET_RESERVEDPORT,
448	    0);
449	if (error)
450		return (error);
451
452	if (req->newlen != sizeof(addrs))
453		return (EINVAL);
454	if (req->oldlen != sizeof(struct ucred))
455		return (EINVAL);
456	error = SYSCTL_IN(req, addrs, sizeof(addrs));
457	if (error)
458		return (error);
459
460	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
461	    sin6tosa(&addrs[1]),
462	    &inp, &net, 1, vrf_id);
463	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
464		if ((inp != NULL) && (stcb == NULL)) {
465			/* reduce ref-count */
466			SCTP_INP_WLOCK(inp);
467			SCTP_INP_DECR_REF(inp);
468			goto cred_can_cont;
469		}
470		error = ENOENT;
471		goto out;
472	}
473	SCTP_TCB_UNLOCK(stcb);
474	/*
475	 * We use the write lock here, only since in the error leg we need
476	 * it. If we used RLOCK, then we would have to
477	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
478	 * Better to use higher wlock.
479	 */
480	SCTP_INP_WLOCK(inp);
481cred_can_cont:
482	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
483	if (error) {
484		SCTP_INP_WUNLOCK(inp);
485		goto out;
486	}
487	cru2x(inp->sctp_socket->so_cred, &xuc);
488	SCTP_INP_WUNLOCK(inp);
489	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
490out:
491	return (error);
492}
493
494SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
495    0, 0,
496    sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
497
498
499/* This is the same as the sctp_abort() could be made common */
500static void
501sctp6_abort(struct socket *so)
502{
503	struct sctp_inpcb *inp;
504	uint32_t flags;
505
506	inp = (struct sctp_inpcb *)so->so_pcb;
507	if (inp == 0)
508		return;
509sctp_must_try_again:
510	flags = inp->sctp_flags;
511#ifdef SCTP_LOG_CLOSING
512	sctp_log_closing(inp, NULL, 17);
513#endif
514	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
515	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
516#ifdef SCTP_LOG_CLOSING
517		sctp_log_closing(inp, NULL, 16);
518#endif
519		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
520		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
521		SOCK_LOCK(so);
522		SCTP_SB_CLEAR(so->so_snd);
523		/*
524		 * same for the rcv ones, they are only here for the
525		 * accounting/select.
526		 */
527		SCTP_SB_CLEAR(so->so_rcv);
528		/* Now null out the reference, we are completely detached. */
529		so->so_pcb = NULL;
530		SOCK_UNLOCK(so);
531	} else {
532		flags = inp->sctp_flags;
533		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
534			goto sctp_must_try_again;
535		}
536	}
537	return;
538}
539
540static int
541sctp6_attach(struct socket *so, int proto, struct thread *p)
542{
543	struct in6pcb *inp6;
544	int error;
545	struct sctp_inpcb *inp;
546
547	inp = (struct sctp_inpcb *)so->so_pcb;
548	if (inp != NULL)
549		return EINVAL;
550
551	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
552		error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
553		if (error)
554			return error;
555	}
556	error = sctp_inpcb_alloc(so);
557	if (error)
558		return error;
559	inp = (struct sctp_inpcb *)so->so_pcb;
560	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
561	inp6 = (struct in6pcb *)inp;
562
563	inp6->inp_vflag |= INP_IPV6;
564	inp6->in6p_hops = -1;	/* use kernel default */
565	inp6->in6p_cksum = -1;	/* just to be sure */
566#ifdef INET
567	/*
568	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
569	 * socket as well, because the socket may be bound to an IPv6
570	 * wildcard address, which may match an IPv4-mapped IPv6 address.
571	 */
572	inp6->inp_ip_ttl = ip_defttl;
573#endif
574	/*
575	 * Hmm what about the IPSEC stuff that is missing here but in
576	 * sctp_attach()?
577	 */
578	return 0;
579}
580
581static int
582sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
583{
584	struct sctp_inpcb *inp;
585	struct in6pcb *inp6;
586	int error;
587
588	inp = (struct sctp_inpcb *)so->so_pcb;
589	if (inp == 0)
590		return EINVAL;
591
592	if (addr) {
593		if ((addr->sa_family == AF_INET6) &&
594		    (addr->sa_len != sizeof(struct sockaddr_in6))) {
595			return EINVAL;
596		}
597		if ((addr->sa_family == AF_INET) &&
598		    (addr->sa_len != sizeof(struct sockaddr_in))) {
599			return EINVAL;
600		}
601	}
602	inp6 = (struct in6pcb *)inp;
603	inp6->inp_vflag &= ~INP_IPV4;
604	inp6->inp_vflag |= INP_IPV6;
605	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
606		if (addr->sa_family == AF_INET) {
607			/* binding v4 addr to v6 socket, so reset flags */
608			inp6->inp_vflag |= INP_IPV4;
609			inp6->inp_vflag &= ~INP_IPV6;
610		} else {
611			struct sockaddr_in6 *sin6_p;
612
613			sin6_p = (struct sockaddr_in6 *)addr;
614
615			if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
616				inp6->inp_vflag |= INP_IPV4;
617			} else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
618				struct sockaddr_in sin;
619
620				in6_sin6_2_sin(&sin, sin6_p);
621				inp6->inp_vflag |= INP_IPV4;
622				inp6->inp_vflag &= ~INP_IPV6;
623				error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, p);
624				return error;
625			}
626		}
627	} else if (addr != NULL) {
628		/* IPV6_V6ONLY socket */
629		if (addr->sa_family == AF_INET) {
630			/* can't bind v4 addr to v6 only socket! */
631			return EINVAL;
632		} else {
633			struct sockaddr_in6 *sin6_p;
634
635			sin6_p = (struct sockaddr_in6 *)addr;
636
637			if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
638				/* can't bind v4-mapped addrs either! */
639				/* NOTE: we don't support SIIT */
640				return EINVAL;
641		}
642	}
643	error = sctp_inpcb_bind(so, addr, p);
644	return error;
645}
646
647
648static void
649sctp6_close(struct socket *so)
650{
651	struct sctp_inpcb *inp;
652	uint32_t flags;
653
654	inp = (struct sctp_inpcb *)so->so_pcb;
655	if (inp == 0)
656		return;
657
658	/*
659	 * Inform all the lower layer assoc that we are done.
660	 */
661sctp_must_try_again:
662	flags = inp->sctp_flags;
663#ifdef SCTP_LOG_CLOSING
664	sctp_log_closing(inp, NULL, 17);
665#endif
666	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
667	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
668		if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
669		    (so->so_rcv.sb_cc > 0)) {
670#ifdef SCTP_LOG_CLOSING
671			sctp_log_closing(inp, NULL, 13);
672#endif
673			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT
674			    ,SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
675		} else {
676#ifdef SCTP_LOG_CLOSING
677			sctp_log_closing(inp, NULL, 14);
678#endif
679			sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
680			    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
681		}
682		/*
683		 * The socket is now detached, no matter what the state of
684		 * the SCTP association.
685		 */
686		SOCK_LOCK(so);
687		SCTP_SB_CLEAR(so->so_snd);
688		/*
689		 * same for the rcv ones, they are only here for the
690		 * accounting/select.
691		 */
692		SCTP_SB_CLEAR(so->so_rcv);
693		/* Now null out the reference, we are completely detached. */
694		so->so_pcb = NULL;
695		SOCK_UNLOCK(so);
696	} else {
697		flags = inp->sctp_flags;
698		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
699			goto sctp_must_try_again;
700		}
701	}
702	return;
703
704}
705
706/* This could be made common with sctp_detach() since they are identical */
707
708static
709int
710sctp6_disconnect(struct socket *so)
711{
712	struct sctp_inpcb *inp;
713
714	inp = (struct sctp_inpcb *)so->so_pcb;
715	if (inp == NULL) {
716		return (ENOTCONN);
717	}
718	SCTP_INP_RLOCK(inp);
719	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
720		if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
721			/* No connection */
722			SCTP_INP_RUNLOCK(inp);
723			return (ENOTCONN);
724		} else {
725			int some_on_streamwheel = 0;
726			struct sctp_association *asoc;
727			struct sctp_tcb *stcb;
728
729			stcb = LIST_FIRST(&inp->sctp_asoc_list);
730			if (stcb == NULL) {
731				SCTP_INP_RUNLOCK(inp);
732				return (EINVAL);
733			}
734			SCTP_TCB_LOCK(stcb);
735			asoc = &stcb->asoc;
736			if (((so->so_options & SO_LINGER) &&
737			    (so->so_linger == 0)) ||
738			    (so->so_rcv.sb_cc > 0)) {
739				if (SCTP_GET_STATE(asoc) !=
740				    SCTP_STATE_COOKIE_WAIT) {
741					/* Left with Data unread */
742					struct mbuf *op_err;
743
744					op_err = sctp_generate_invmanparam(SCTP_CAUSE_USER_INITIATED_ABT);
745					sctp_send_abort_tcb(stcb, op_err);
746					SCTP_STAT_INCR_COUNTER32(sctps_aborted);
747				}
748				SCTP_INP_RUNLOCK(inp);
749				if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
750				    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
751					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
752				}
753				sctp_free_assoc(inp, stcb, SCTP_DONOT_SETSCOPE,
754				    SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_2);
755				/* No unlock tcb assoc is gone */
756				return (0);
757			}
758			if (!TAILQ_EMPTY(&asoc->out_wheel)) {
759				/* Check to see if some data queued */
760				struct sctp_stream_out *outs;
761
762				TAILQ_FOREACH(outs, &asoc->out_wheel,
763				    next_spoke) {
764					if (!TAILQ_EMPTY(&outs->outqueue)) {
765						some_on_streamwheel = 1;
766						break;
767					}
768				}
769			}
770			if (TAILQ_EMPTY(&asoc->send_queue) &&
771			    TAILQ_EMPTY(&asoc->sent_queue) &&
772			    (some_on_streamwheel == 0)) {
773				/* nothing queued to send, so I'm done... */
774				if ((SCTP_GET_STATE(asoc) !=
775				    SCTP_STATE_SHUTDOWN_SENT) &&
776				    (SCTP_GET_STATE(asoc) !=
777				    SCTP_STATE_SHUTDOWN_ACK_SENT)) {
778					/* only send SHUTDOWN the first time */
779					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
780					sctp_chunk_output(stcb->sctp_ep, stcb, 1);
781					if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
782					    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
783						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
784					}
785					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
786					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
787					    stcb->sctp_ep, stcb,
788					    asoc->primary_destination);
789					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
790					    stcb->sctp_ep, stcb,
791					    asoc->primary_destination);
792				}
793			} else {
794				/*
795				 * we still got (or just got) data to send,
796				 * so set SHUTDOWN_PENDING
797				 */
798				/*
799				 * XXX sockets draft says that MSG_EOF
800				 * should be sent with no data.  currently,
801				 * we will allow user data to be sent first
802				 * and move to SHUTDOWN-PENDING
803				 */
804				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
805			}
806			SCTP_TCB_UNLOCK(stcb);
807			SCTP_INP_RUNLOCK(inp);
808			return (0);
809		}
810	} else {
811		/* UDP model does not support this */
812		SCTP_INP_RUNLOCK(inp);
813		return EOPNOTSUPP;
814	}
815}
816
817
818int
819sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
820    struct mbuf *control, struct thread *p);
821
822
823static int
824sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
825    struct mbuf *control, struct thread *p)
826{
827	struct sctp_inpcb *inp;
828	struct inpcb *in_inp;
829	struct in6pcb *inp6;
830
831#ifdef INET
832	struct sockaddr_in6 *sin6;
833
834#endif				/* INET */
835	/* No SPL needed since sctp_output does this */
836
837	inp = (struct sctp_inpcb *)so->so_pcb;
838	if (inp == NULL) {
839		if (control) {
840			SCTP_RELEASE_PKT(control);
841			control = NULL;
842		}
843		SCTP_RELEASE_PKT(m);
844		return EINVAL;
845	}
846	in_inp = (struct inpcb *)inp;
847	inp6 = (struct in6pcb *)inp;
848	/*
849	 * For the TCP model we may get a NULL addr, if we are a connected
850	 * socket thats ok.
851	 */
852	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
853	    (addr == NULL)) {
854		goto connected_type;
855	}
856	if (addr == NULL) {
857		SCTP_RELEASE_PKT(m);
858		if (control) {
859			SCTP_RELEASE_PKT(control);
860			control = NULL;
861		}
862		return (EDESTADDRREQ);
863	}
864#ifdef INET
865	sin6 = (struct sockaddr_in6 *)addr;
866	if (SCTP_IPV6_V6ONLY(inp6)) {
867		/*
868		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
869		 * v4 addr or v4-mapped addr
870		 */
871		if (addr->sa_family == AF_INET) {
872			return EINVAL;
873		}
874		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
875			return EINVAL;
876		}
877	}
878	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
879		if (!ip6_v6only) {
880			struct sockaddr_in sin;
881
882			/* convert v4-mapped into v4 addr and send */
883			in6_sin6_2_sin(&sin, sin6);
884			return sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
885			    control, p);
886		} else {
887			/* mapped addresses aren't enabled */
888			return EINVAL;
889		}
890	}
891#endif				/* INET */
892connected_type:
893	/* now what about control */
894	if (control) {
895		if (inp->control) {
896			SCTP_PRINTF("huh? control set?\n");
897			SCTP_RELEASE_PKT(inp->control);
898			inp->control = NULL;
899		}
900		inp->control = control;
901	}
902	/* Place the data */
903	if (inp->pkt) {
904		SCTP_BUF_NEXT(inp->pkt_last) = m;
905		inp->pkt_last = m;
906	} else {
907		inp->pkt_last = inp->pkt = m;
908	}
909	if (
910	/* FreeBSD and MacOSX uses a flag passed */
911	    ((flags & PRUS_MORETOCOME) == 0)
912	    ) {
913		/*
914		 * note with the current version this code will only be used
915		 * by OpenBSD, NetBSD and FreeBSD have methods for
916		 * re-defining sosend() to use sctp_sosend().  One can
917		 * optionaly switch back to this code (by changing back the
918		 * defininitions but this is not advisable.
919		 */
920		int ret;
921
922		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
923		inp->pkt = NULL;
924		inp->control = NULL;
925		return (ret);
926	} else {
927		return (0);
928	}
929}
930
931static int
932sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
933{
934	uint32_t vrf_id;
935	int error = 0;
936	struct sctp_inpcb *inp;
937	struct in6pcb *inp6;
938	struct sctp_tcb *stcb;
939
940#ifdef INET
941	struct sockaddr_in6 *sin6;
942	struct sockaddr_storage ss;
943
944#endif				/* INET */
945
946	inp6 = (struct in6pcb *)so->so_pcb;
947	inp = (struct sctp_inpcb *)so->so_pcb;
948	if (inp == 0) {
949		return (ECONNRESET);	/* I made the same as TCP since we are
950					 * not setup? */
951	}
952	if (addr == NULL) {
953		return (EINVAL);
954	}
955	if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
956		return (EINVAL);
957	}
958	if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
959		return (EINVAL);
960	}
961	vrf_id = inp->def_vrf_id;
962	SCTP_ASOC_CREATE_LOCK(inp);
963	SCTP_INP_RLOCK(inp);
964	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
965	    SCTP_PCB_FLAGS_UNBOUND) {
966		/* Bind a ephemeral port */
967		SCTP_INP_RUNLOCK(inp);
968		error = sctp6_bind(so, NULL, p);
969		if (error) {
970			SCTP_ASOC_CREATE_UNLOCK(inp);
971
972			return (error);
973		}
974		SCTP_INP_RLOCK(inp);
975	}
976	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
977	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
978		/* We are already connected AND the TCP model */
979		SCTP_INP_RUNLOCK(inp);
980		SCTP_ASOC_CREATE_UNLOCK(inp);
981		return (EADDRINUSE);
982	}
983#ifdef INET
984	sin6 = (struct sockaddr_in6 *)addr;
985	if (SCTP_IPV6_V6ONLY(inp6)) {
986		/*
987		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
988		 * addr or v4-mapped addr
989		 */
990		if (addr->sa_family == AF_INET) {
991			SCTP_INP_RUNLOCK(inp);
992			SCTP_ASOC_CREATE_UNLOCK(inp);
993			return EINVAL;
994		}
995		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
996			SCTP_INP_RUNLOCK(inp);
997			SCTP_ASOC_CREATE_UNLOCK(inp);
998			return EINVAL;
999		}
1000	}
1001	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1002		if (!ip6_v6only) {
1003			/* convert v4-mapped into v4 addr */
1004			in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1005			addr = (struct sockaddr *)&ss;
1006		} else {
1007			/* mapped addresses aren't enabled */
1008			SCTP_INP_RUNLOCK(inp);
1009			SCTP_ASOC_CREATE_UNLOCK(inp);
1010			return EINVAL;
1011		}
1012	} else
1013#endif				/* INET */
1014		addr = addr;	/* for true v6 address case */
1015
1016	/* Now do we connect? */
1017	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1018		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1019		if (stcb) {
1020			SCTP_TCB_UNLOCK(stcb);
1021		}
1022		SCTP_INP_RUNLOCK(inp);
1023	} else {
1024		SCTP_INP_RUNLOCK(inp);
1025		SCTP_INP_WLOCK(inp);
1026		SCTP_INP_INCR_REF(inp);
1027		SCTP_INP_WUNLOCK(inp);
1028		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1029		if (stcb == NULL) {
1030			SCTP_INP_WLOCK(inp);
1031			SCTP_INP_DECR_REF(inp);
1032			SCTP_INP_WUNLOCK(inp);
1033		}
1034	}
1035
1036	if (stcb != NULL) {
1037		/* Already have or am bring up an association */
1038		SCTP_ASOC_CREATE_UNLOCK(inp);
1039		SCTP_TCB_UNLOCK(stcb);
1040		return (EALREADY);
1041	}
1042	/* We are GOOD to go */
1043	stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id);
1044	SCTP_ASOC_CREATE_UNLOCK(inp);
1045	if (stcb == NULL) {
1046		/* Gak! no memory */
1047		return (error);
1048	}
1049	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1050		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1051		/* Set the connected flag so we can queue data */
1052		soisconnecting(so);
1053	}
1054	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1055	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1056
1057	/* initialize authentication parameters for the assoc */
1058	sctp_initialize_auth_params(inp, stcb);
1059
1060	sctp_send_initiate(inp, stcb);
1061	SCTP_TCB_UNLOCK(stcb);
1062	return error;
1063}
1064
1065static int
1066sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1067{
1068	struct sockaddr_in6 *sin6;
1069	struct sctp_inpcb *inp;
1070	uint32_t vrf_id;
1071	struct sctp_ifa *sctp_ifa;
1072
1073	int error;
1074
1075	/*
1076	 * Do the malloc first in case it blocks.
1077	 */
1078	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1079	sin6->sin6_family = AF_INET6;
1080	sin6->sin6_len = sizeof(*sin6);
1081
1082	inp = (struct sctp_inpcb *)so->so_pcb;
1083	if (inp == NULL) {
1084		SCTP_FREE_SONAME(sin6);
1085		return ECONNRESET;
1086	}
1087	SCTP_INP_RLOCK(inp);
1088	sin6->sin6_port = inp->sctp_lport;
1089	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1090		/* For the bound all case you get back 0 */
1091		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1092			struct sctp_tcb *stcb;
1093			struct sockaddr_in6 *sin_a6;
1094			struct sctp_nets *net;
1095			int fnd;
1096
1097			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1098			if (stcb == NULL) {
1099				goto notConn6;
1100			}
1101			fnd = 0;
1102			sin_a6 = NULL;
1103			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1104				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1105				if (sin_a6 == NULL)
1106					/* this will make coverity happy */
1107					continue;
1108
1109				if (sin_a6->sin6_family == AF_INET6) {
1110					fnd = 1;
1111					break;
1112				}
1113			}
1114			if ((!fnd) || (sin_a6 == NULL)) {
1115				/* punt */
1116				goto notConn6;
1117			}
1118			vrf_id = inp->def_vrf_id;
1119			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1120			if (sctp_ifa) {
1121				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1122			}
1123		} else {
1124			/* For the bound all case you get back 0 */
1125	notConn6:
1126			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1127		}
1128	} else {
1129		/* Take the first IPv6 address in the list */
1130		struct sctp_laddr *laddr;
1131		int fnd = 0;
1132
1133		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1134			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1135				struct sockaddr_in6 *sin_a;
1136
1137				sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1138				sin6->sin6_addr = sin_a->sin6_addr;
1139				fnd = 1;
1140				break;
1141			}
1142		}
1143		if (!fnd) {
1144			SCTP_FREE_SONAME(sin6);
1145			SCTP_INP_RUNLOCK(inp);
1146			return ENOENT;
1147		}
1148	}
1149	SCTP_INP_RUNLOCK(inp);
1150	/* Scoping things for v6 */
1151	if ((error = sa6_recoverscope(sin6)) != 0) {
1152		SCTP_FREE_SONAME(sin6);
1153		return (error);
1154	}
1155	(*addr) = (struct sockaddr *)sin6;
1156	return (0);
1157}
1158
1159static int
1160sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1161{
1162	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1163	int fnd;
1164	struct sockaddr_in6 *sin_a6;
1165	struct sctp_inpcb *inp;
1166	struct sctp_tcb *stcb;
1167	struct sctp_nets *net;
1168
1169	int error;
1170
1171	/*
1172	 * Do the malloc first in case it blocks.
1173	 */
1174	inp = (struct sctp_inpcb *)so->so_pcb;
1175	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1176		/* UDP type and listeners will drop out here */
1177		return (ENOTCONN);
1178	}
1179	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1180	sin6->sin6_family = AF_INET6;
1181	sin6->sin6_len = sizeof(*sin6);
1182
1183	/* We must recapture incase we blocked */
1184	inp = (struct sctp_inpcb *)so->so_pcb;
1185	if (inp == NULL) {
1186		SCTP_FREE_SONAME(sin6);
1187		return ECONNRESET;
1188	}
1189	SCTP_INP_RLOCK(inp);
1190	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1191	if (stcb) {
1192		SCTP_TCB_LOCK(stcb);
1193	}
1194	SCTP_INP_RUNLOCK(inp);
1195	if (stcb == NULL) {
1196		SCTP_FREE_SONAME(sin6);
1197		return ECONNRESET;
1198	}
1199	fnd = 0;
1200	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1201		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1202		if (sin_a6->sin6_family == AF_INET6) {
1203			fnd = 1;
1204			sin6->sin6_port = stcb->rport;
1205			sin6->sin6_addr = sin_a6->sin6_addr;
1206			break;
1207		}
1208	}
1209	SCTP_TCB_UNLOCK(stcb);
1210	if (!fnd) {
1211		/* No IPv4 address */
1212		SCTP_FREE_SONAME(sin6);
1213		return ENOENT;
1214	}
1215	if ((error = sa6_recoverscope(sin6)) != 0)
1216		return (error);
1217	*addr = (struct sockaddr *)sin6;
1218	return (0);
1219}
1220
1221static int
1222sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1223{
1224	struct sockaddr *addr;
1225	struct in6pcb *inp6 = sotoin6pcb(so);
1226	int error;
1227
1228	if (inp6 == NULL)
1229		return EINVAL;
1230
1231	/* allow v6 addresses precedence */
1232	error = sctp6_getaddr(so, nam);
1233	if (error) {
1234		/* try v4 next if v6 failed */
1235		error = sctp_ingetaddr(so, nam);
1236		if (error) {
1237			return (error);
1238		}
1239		addr = *nam;
1240		/* if I'm V6ONLY, convert it to v4-mapped */
1241		if (SCTP_IPV6_V6ONLY(inp6)) {
1242			struct sockaddr_in6 sin6;
1243
1244			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1245			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1246
1247		}
1248	}
1249	return (error);
1250}
1251
1252
1253static int
1254sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1255{
1256	struct sockaddr *addr = *nam;
1257	struct in6pcb *inp6 = sotoin6pcb(so);
1258	int error;
1259
1260	if (inp6 == NULL)
1261		return EINVAL;
1262
1263	/* allow v6 addresses precedence */
1264	error = sctp6_peeraddr(so, nam);
1265	if (error) {
1266		/* try v4 next if v6 failed */
1267		error = sctp_peeraddr(so, nam);
1268		if (error) {
1269			return (error);
1270		}
1271		/* if I'm V6ONLY, convert it to v4-mapped */
1272		if (SCTP_IPV6_V6ONLY(inp6)) {
1273			struct sockaddr_in6 sin6;
1274
1275			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1276			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1277		}
1278	}
1279	return error;
1280}
1281
1282struct pr_usrreqs sctp6_usrreqs = {
1283	.pru_abort = sctp6_abort,
1284	.pru_accept = sctp_accept,
1285	.pru_attach = sctp6_attach,
1286	.pru_bind = sctp6_bind,
1287	.pru_connect = sctp6_connect,
1288	.pru_control = in6_control,
1289	.pru_close = sctp6_close,
1290	.pru_detach = sctp6_close,
1291	.pru_sopoll = sopoll_generic,
1292	.pru_disconnect = sctp6_disconnect,
1293	.pru_listen = sctp_listen,
1294	.pru_peeraddr = sctp6_getpeeraddr,
1295	.pru_send = sctp6_send,
1296	.pru_shutdown = sctp_shutdown,
1297	.pru_sockaddr = sctp6_in6getaddr,
1298	.pru_sosend = sctp_sosend,
1299	.pru_soreceive = sctp_soreceive
1300};
1301