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