1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * a) Redistributions of source code must retain the above copyright notice,
12 *    this list of conditions and the following disclaimer.
13 *
14 * b) Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in
16 *    the documentation and/or other materials provided with the distribution.
17 *
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 *    contributors may be used to endorse or promote products derived
20 *    from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD$");
37
38#include <netinet/sctp_os.h>
39#ifdef INET6
40#include <sys/proc.h>
41#include <netinet/sctp_pcb.h>
42#include <netinet/sctp_header.h>
43#include <netinet/sctp_var.h>
44#include <netinet6/sctp6_var.h>
45#include <netinet/sctp_sysctl.h>
46#include <netinet/sctp_output.h>
47#include <netinet/sctp_uio.h>
48#include <netinet/sctp_asconf.h>
49#include <netinet/sctputil.h>
50#include <netinet/sctp_indata.h>
51#include <netinet/sctp_timer.h>
52#include <netinet/sctp_auth.h>
53#include <netinet/sctp_input.h>
54#include <netinet/sctp_output.h>
55#include <netinet/sctp_bsd_addr.h>
56#include <netinet/sctp_crc32.h>
57#include <netinet/icmp6.h>
58#include <netinet/udp.h>
59
60int
61sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
62{
63	struct mbuf *m;
64	int iphlen;
65	uint32_t vrf_id;
66	uint8_t ecn_bits;
67	struct sockaddr_in6 src, dst;
68	struct ip6_hdr *ip6;
69	struct sctphdr *sh;
70	struct sctp_chunkhdr *ch;
71	int length, offset;
72	uint8_t compute_crc;
73	uint32_t mflowid;
74	uint8_t mflowtype;
75	uint16_t fibnum;
76
77	iphlen = *offp;
78	if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
79		SCTP_RELEASE_PKT(*i_pak);
80		return (IPPROTO_DONE);
81	}
82	m = SCTP_HEADER_TO_CHAIN(*i_pak);
83#ifdef SCTP_MBUF_LOGGING
84	/* Log in any input mbufs */
85	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
86		sctp_log_mbc(m, SCTP_MBUF_INPUT);
87	}
88#endif
89#ifdef SCTP_PACKET_LOGGING
90	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
91		sctp_packet_log(m);
92	}
93#endif
94	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
95	    "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
96	    m->m_pkthdr.len,
97	    if_name(m->m_pkthdr.rcvif),
98	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
99	mflowid = m->m_pkthdr.flowid;
100	mflowtype = M_HASHTYPE_GET(m);
101	fibnum = M_GETFIB(m);
102	SCTP_STAT_INCR(sctps_recvpackets);
103	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
104	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
105	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
106	if (m->m_len < offset) {
107		m = m_pullup(m, offset);
108		if (m == NULL) {
109			SCTP_STAT_INCR(sctps_hdrops);
110			return (IPPROTO_DONE);
111		}
112	}
113	ip6 = mtod(m, struct ip6_hdr *);
114	sh = (struct sctphdr *)(mtod(m, caddr_t)+iphlen);
115	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
116	offset -= sizeof(struct sctp_chunkhdr);
117	memset(&src, 0, sizeof(struct sockaddr_in6));
118	src.sin6_family = AF_INET6;
119	src.sin6_len = sizeof(struct sockaddr_in6);
120	src.sin6_port = sh->src_port;
121	src.sin6_addr = ip6->ip6_src;
122	if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
123		goto out;
124	}
125	memset(&dst, 0, sizeof(struct sockaddr_in6));
126	dst.sin6_family = AF_INET6;
127	dst.sin6_len = sizeof(struct sockaddr_in6);
128	dst.sin6_port = sh->dest_port;
129	dst.sin6_addr = ip6->ip6_dst;
130	if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
131		goto out;
132	}
133	length = ntohs(ip6->ip6_plen) + iphlen;
134	/* Validate mbuf chain length with IP payload length. */
135	if (SCTP_HEADER_LEN(m) != length) {
136		SCTPDBG(SCTP_DEBUG_INPUT1,
137		    "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
138		SCTP_STAT_INCR(sctps_hdrops);
139		goto out;
140	}
141	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
142		goto out;
143	}
144	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
145	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
146		SCTP_STAT_INCR(sctps_recvhwcrc);
147		compute_crc = 0;
148	} else {
149		SCTP_STAT_INCR(sctps_recvswcrc);
150		compute_crc = 1;
151	}
152	sctp_common_input_processing(&m, iphlen, offset, length,
153	    (struct sockaddr *)&src,
154	    (struct sockaddr *)&dst,
155	    sh, ch,
156	    compute_crc,
157	    ecn_bits,
158	    mflowtype, mflowid, fibnum,
159	    vrf_id, port);
160out:
161	if (m) {
162		sctp_m_freem(m);
163	}
164	return (IPPROTO_DONE);
165}
166
167
168int
169sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
170{
171	return (sctp6_input_with_port(i_pak, offp, 0));
172}
173
174void
175sctp6_notify(struct sctp_inpcb *inp,
176    struct sctp_tcb *stcb,
177    struct sctp_nets *net,
178    uint8_t icmp6_type,
179    uint8_t icmp6_code,
180    uint32_t next_mtu)
181{
182#if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
183	struct socket *so;
184#endif
185	int timer_stopped;
186
187	switch (icmp6_type) {
188	case ICMP6_DST_UNREACH:
189		if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) ||
190		    (icmp6_code == ICMP6_DST_UNREACH_ADMIN) ||
191		    (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) ||
192		    (icmp6_code == ICMP6_DST_UNREACH_ADDR)) {
193			/* Mark the net unreachable. */
194			if (net->dest_state & SCTP_ADDR_REACHABLE) {
195				/* Ok that destination is not reachable */
196				net->dest_state &= ~SCTP_ADDR_REACHABLE;
197				net->dest_state &= ~SCTP_ADDR_PF;
198				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
199				    stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
200			}
201		}
202		SCTP_TCB_UNLOCK(stcb);
203		break;
204	case ICMP6_PARAM_PROB:
205		/* Treat it like an ABORT. */
206		if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) {
207			sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
208#if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
209			so = SCTP_INP_SO(inp);
210			atomic_add_int(&stcb->asoc.refcnt, 1);
211			SCTP_TCB_UNLOCK(stcb);
212			SCTP_SOCKET_LOCK(so, 1);
213			SCTP_TCB_LOCK(stcb);
214			atomic_subtract_int(&stcb->asoc.refcnt, 1);
215#endif
216			(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
217			    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
218#if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
219			SCTP_SOCKET_UNLOCK(so, 1);
220#endif
221		} else {
222			SCTP_TCB_UNLOCK(stcb);
223		}
224		break;
225	case ICMP6_PACKET_TOO_BIG:
226		if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
227			SCTP_TCB_UNLOCK(stcb);
228			break;
229		}
230		if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
231			timer_stopped = 1;
232			sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
233			    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
234		} else {
235			timer_stopped = 0;
236		}
237		/* Update the path MTU. */
238		if (net->port) {
239			next_mtu -= sizeof(struct udphdr);
240		}
241		if (net->mtu > next_mtu) {
242			net->mtu = next_mtu;
243			if (net->port) {
244				sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu + sizeof(struct udphdr));
245			} else {
246				sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu);
247			}
248		}
249		/* Update the association MTU */
250		if (stcb->asoc.smallest_mtu > next_mtu) {
251			sctp_pathmtu_adjustment(stcb, next_mtu);
252		}
253		/* Finally, start the PMTU timer if it was running before. */
254		if (timer_stopped) {
255			sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
256		}
257		SCTP_TCB_UNLOCK(stcb);
258		break;
259	default:
260		SCTP_TCB_UNLOCK(stcb);
261		break;
262	}
263}
264
265void
266sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
267{
268	struct ip6ctlparam *ip6cp;
269	struct sctp_inpcb *inp;
270	struct sctp_tcb *stcb;
271	struct sctp_nets *net;
272	struct sctphdr sh;
273	struct sockaddr_in6 src, dst;
274
275	if (pktdst->sa_family != AF_INET6 ||
276	    pktdst->sa_len != sizeof(struct sockaddr_in6)) {
277		return;
278	}
279
280	if ((unsigned)cmd >= PRC_NCMDS) {
281		return;
282	}
283	if (PRC_IS_REDIRECT(cmd)) {
284		d = NULL;
285	} else if (inet6ctlerrmap[cmd] == 0) {
286		return;
287	}
288	/* If the parameter is from icmp6, decode it. */
289	if (d != NULL) {
290		ip6cp = (struct ip6ctlparam *)d;
291	} else {
292		ip6cp = (struct ip6ctlparam *)NULL;
293	}
294
295	if (ip6cp != NULL) {
296		/*
297		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
298		 * valid.
299		 */
300		if (ip6cp->ip6c_m == NULL) {
301			return;
302		}
303
304		/*
305		 * Check if we can safely examine the ports and the
306		 * verification tag of the SCTP common header.
307		 */
308		if (ip6cp->ip6c_m->m_pkthdr.len <
309		    (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
310			return;
311		}
312
313		/* Copy out the port numbers and the verification tag. */
314		memset(&sh, 0, sizeof(sh));
315		m_copydata(ip6cp->ip6c_m,
316		    ip6cp->ip6c_off,
317		    sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
318		    (caddr_t)&sh);
319		memset(&src, 0, sizeof(struct sockaddr_in6));
320		src.sin6_family = AF_INET6;
321		src.sin6_len = sizeof(struct sockaddr_in6);
322		src.sin6_port = sh.src_port;
323		src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
324		if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
325			return;
326		}
327		memset(&dst, 0, sizeof(struct sockaddr_in6));
328		dst.sin6_family = AF_INET6;
329		dst.sin6_len = sizeof(struct sockaddr_in6);
330		dst.sin6_port = sh.dest_port;
331		dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
332		if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
333			return;
334		}
335		inp = NULL;
336		net = NULL;
337		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
338		    (struct sockaddr *)&src,
339		    &inp, &net, 1, SCTP_DEFAULT_VRFID);
340		if ((stcb != NULL) &&
341		    (net != NULL) &&
342		    (inp != NULL)) {
343			/* Check the verification tag */
344			if (ntohl(sh.v_tag) != 0) {
345				/*
346				 * This must be the verification tag used
347				 * for sending out packets. We don't
348				 * consider packets reflecting the
349				 * verification tag.
350				 */
351				if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
352					SCTP_TCB_UNLOCK(stcb);
353					return;
354				}
355			} else {
356				if (ip6cp->ip6c_m->m_pkthdr.len >=
357				    ip6cp->ip6c_off + sizeof(struct sctphdr) +
358				    sizeof(struct sctp_chunkhdr) +
359				    offsetof(struct sctp_init, a_rwnd)) {
360					/*
361					 * In this case we can check if we
362					 * got an INIT chunk and if the
363					 * initiate tag matches.
364					 */
365					uint32_t initiate_tag;
366					uint8_t chunk_type;
367
368					m_copydata(ip6cp->ip6c_m,
369					    ip6cp->ip6c_off +
370					    sizeof(struct sctphdr),
371					    sizeof(uint8_t),
372					    (caddr_t)&chunk_type);
373					m_copydata(ip6cp->ip6c_m,
374					    ip6cp->ip6c_off +
375					    sizeof(struct sctphdr) +
376					    sizeof(struct sctp_chunkhdr),
377					    sizeof(uint32_t),
378					    (caddr_t)&initiate_tag);
379					if ((chunk_type != SCTP_INITIATION) ||
380					    (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
381						SCTP_TCB_UNLOCK(stcb);
382						return;
383					}
384				} else {
385					SCTP_TCB_UNLOCK(stcb);
386					return;
387				}
388			}
389			sctp6_notify(inp, stcb, net,
390			    ip6cp->ip6c_icmp6->icmp6_type,
391			    ip6cp->ip6c_icmp6->icmp6_code,
392			    ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
393		} else {
394			if ((stcb == NULL) && (inp != NULL)) {
395				/* reduce inp's ref-count */
396				SCTP_INP_WLOCK(inp);
397				SCTP_INP_DECR_REF(inp);
398				SCTP_INP_WUNLOCK(inp);
399			}
400			if (stcb) {
401				SCTP_TCB_UNLOCK(stcb);
402			}
403		}
404	}
405}
406
407/*
408 * this routine can probably be collasped into the one in sctp_userreq.c
409 * since they do the same thing and now we lookup with a sockaddr
410 */
411static int
412sctp6_getcred(SYSCTL_HANDLER_ARGS)
413{
414	struct xucred xuc;
415	struct sockaddr_in6 addrs[2];
416	struct sctp_inpcb *inp;
417	struct sctp_nets *net;
418	struct sctp_tcb *stcb;
419	int error;
420	uint32_t vrf_id;
421
422	vrf_id = SCTP_DEFAULT_VRFID;
423
424	error = priv_check(req->td, PRIV_NETINET_GETCRED);
425	if (error)
426		return (error);
427
428	if (req->newlen != sizeof(addrs)) {
429		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
430		return (EINVAL);
431	}
432	if (req->oldlen != sizeof(struct ucred)) {
433		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
434		return (EINVAL);
435	}
436	error = SYSCTL_IN(req, addrs, sizeof(addrs));
437	if (error)
438		return (error);
439
440	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
441	    sin6tosa(&addrs[0]),
442	    &inp, &net, 1, vrf_id);
443	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
444		if ((inp != NULL) && (stcb == NULL)) {
445			/* reduce ref-count */
446			SCTP_INP_WLOCK(inp);
447			SCTP_INP_DECR_REF(inp);
448			goto cred_can_cont;
449		}
450		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
451		error = ENOENT;
452		goto out;
453	}
454	SCTP_TCB_UNLOCK(stcb);
455	/*
456	 * We use the write lock here, only since in the error leg we need
457	 * it. If we used RLOCK, then we would have to
458	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
459	 * Better to use higher wlock.
460	 */
461	SCTP_INP_WLOCK(inp);
462cred_can_cont:
463	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
464	if (error) {
465		SCTP_INP_WUNLOCK(inp);
466		goto out;
467	}
468	cru2x(inp->sctp_socket->so_cred, &xuc);
469	SCTP_INP_WUNLOCK(inp);
470	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
471out:
472	return (error);
473}
474
475SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred,
476    CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
477    0, 0, sctp6_getcred, "S,ucred",
478    "Get the ucred of a SCTP6 connection");
479
480
481/* This is the same as the sctp_abort() could be made common */
482static void
483sctp6_abort(struct socket *so)
484{
485	struct epoch_tracker et;
486	struct sctp_inpcb *inp;
487	uint32_t flags;
488
489	inp = (struct sctp_inpcb *)so->so_pcb;
490	if (inp == NULL) {
491		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
492		return;
493	}
494	NET_EPOCH_ENTER(et);
495sctp_must_try_again:
496	flags = inp->sctp_flags;
497#ifdef SCTP_LOG_CLOSING
498	sctp_log_closing(inp, NULL, 17);
499#endif
500	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
501	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
502#ifdef SCTP_LOG_CLOSING
503		sctp_log_closing(inp, NULL, 16);
504#endif
505		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
506		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
507		SOCK_LOCK(so);
508		SCTP_SB_CLEAR(so->so_snd);
509		/*
510		 * same for the rcv ones, they are only here for the
511		 * accounting/select.
512		 */
513		SCTP_SB_CLEAR(so->so_rcv);
514		/* Now null out the reference, we are completely detached. */
515		so->so_pcb = NULL;
516		SOCK_UNLOCK(so);
517	} else {
518		flags = inp->sctp_flags;
519		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
520			goto sctp_must_try_again;
521		}
522	}
523	NET_EPOCH_EXIT(et);
524	return;
525}
526
527static int
528sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
529{
530	int error;
531	struct sctp_inpcb *inp;
532	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
533
534	inp = (struct sctp_inpcb *)so->so_pcb;
535	if (inp != NULL) {
536		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
537		return (EINVAL);
538	}
539
540	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
541		error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
542		if (error)
543			return (error);
544	}
545	error = sctp_inpcb_alloc(so, vrf_id);
546	if (error)
547		return (error);
548	inp = (struct sctp_inpcb *)so->so_pcb;
549	SCTP_INP_WLOCK(inp);
550	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
551
552	inp->ip_inp.inp.inp_vflag |= INP_IPV6;
553	inp->ip_inp.inp.in6p_hops = -1;	/* use kernel default */
554	inp->ip_inp.inp.in6p_cksum = -1;	/* just to be sure */
555#ifdef INET
556	/*
557	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
558	 * socket as well, because the socket may be bound to an IPv6
559	 * wildcard address, which may match an IPv4-mapped IPv6 address.
560	 */
561	inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
562#endif
563	SCTP_INP_WUNLOCK(inp);
564	return (0);
565}
566
567static int
568sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
569{
570	struct sctp_inpcb *inp;
571	int error;
572	u_char vflagsav;
573
574	inp = (struct sctp_inpcb *)so->so_pcb;
575	if (inp == NULL) {
576		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
577		return (EINVAL);
578	}
579
580	if (addr) {
581		switch (addr->sa_family) {
582#ifdef INET
583		case AF_INET:
584			if (addr->sa_len != sizeof(struct sockaddr_in)) {
585				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
586				return (EINVAL);
587			}
588			break;
589#endif
590#ifdef INET6
591		case AF_INET6:
592			if (addr->sa_len != sizeof(struct sockaddr_in6)) {
593				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
594				return (EINVAL);
595			}
596			break;
597#endif
598		default:
599			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
600			return (EINVAL);
601		}
602	}
603	vflagsav = inp->ip_inp.inp.inp_vflag;
604	inp->ip_inp.inp.inp_vflag &= ~INP_IPV4;
605	inp->ip_inp.inp.inp_vflag |= INP_IPV6;
606	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp) == 0)) {
607		switch (addr->sa_family) {
608#ifdef INET
609		case AF_INET:
610			/* binding v4 addr to v6 socket, so reset flags */
611			inp->ip_inp.inp.inp_vflag |= INP_IPV4;
612			inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
613			break;
614#endif
615#ifdef INET6
616		case AF_INET6:
617			{
618				struct sockaddr_in6 *sin6_p;
619
620				sin6_p = (struct sockaddr_in6 *)addr;
621
622				if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
623					inp->ip_inp.inp.inp_vflag |= INP_IPV4;
624				}
625#ifdef INET
626				if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
627					struct sockaddr_in sin;
628
629					in6_sin6_2_sin(&sin, sin6_p);
630					inp->ip_inp.inp.inp_vflag |= INP_IPV4;
631					inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
632					error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
633					goto out;
634				}
635#endif
636				break;
637			}
638#endif
639		default:
640			break;
641		}
642	} else if (addr != NULL) {
643		struct sockaddr_in6 *sin6_p;
644
645		/* IPV6_V6ONLY socket */
646#ifdef INET
647		if (addr->sa_family == AF_INET) {
648			/* can't bind v4 addr to v6 only socket! */
649			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
650			error = EINVAL;
651			goto out;
652		}
653#endif
654		sin6_p = (struct sockaddr_in6 *)addr;
655
656		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
657			/* can't bind v4-mapped addrs either! */
658			/* NOTE: we don't support SIIT */
659			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
660			error = EINVAL;
661			goto out;
662		}
663	}
664	error = sctp_inpcb_bind(so, addr, NULL, p);
665out:
666	if (error != 0)
667		inp->ip_inp.inp.inp_vflag = vflagsav;
668	return (error);
669}
670
671
672static void
673sctp6_close(struct socket *so)
674{
675	sctp_close(so);
676}
677
678/* This could be made common with sctp_detach() since they are identical */
679
680static
681int
682sctp6_disconnect(struct socket *so)
683{
684	return (sctp_disconnect(so));
685}
686
687
688int
689sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
690    struct mbuf *control, struct thread *p);
691
692
693static int
694sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
695    struct mbuf *control, struct thread *p)
696{
697	struct sctp_inpcb *inp;
698
699#ifdef INET
700	struct sockaddr_in6 *sin6;
701#endif				/* INET */
702	/* No SPL needed since sctp_output does this */
703
704	inp = (struct sctp_inpcb *)so->so_pcb;
705	if (inp == NULL) {
706		if (control) {
707			SCTP_RELEASE_PKT(control);
708			control = NULL;
709		}
710		SCTP_RELEASE_PKT(m);
711		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
712		return (EINVAL);
713	}
714	/*
715	 * For the TCP model we may get a NULL addr, if we are a connected
716	 * socket thats ok.
717	 */
718	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
719	    (addr == NULL)) {
720		goto connected_type;
721	}
722	if (addr == NULL) {
723		SCTP_RELEASE_PKT(m);
724		if (control) {
725			SCTP_RELEASE_PKT(control);
726			control = NULL;
727		}
728		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
729		return (EDESTADDRREQ);
730	}
731#ifdef INET
732	sin6 = (struct sockaddr_in6 *)addr;
733	if (SCTP_IPV6_V6ONLY(inp)) {
734		/*
735		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
736		 * v4 addr or v4-mapped addr
737		 */
738		if (addr->sa_family == AF_INET) {
739			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
740			return (EINVAL);
741		}
742		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
743			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
744			return (EINVAL);
745		}
746	}
747	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
748		struct sockaddr_in sin;
749
750		/* convert v4-mapped into v4 addr and send */
751		in6_sin6_2_sin(&sin, sin6);
752		return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
753	}
754#endif				/* INET */
755connected_type:
756	/* now what about control */
757	if (control) {
758		if (inp->control) {
759			SCTP_PRINTF("huh? control set?\n");
760			SCTP_RELEASE_PKT(inp->control);
761			inp->control = NULL;
762		}
763		inp->control = control;
764	}
765	/* Place the data */
766	if (inp->pkt) {
767		SCTP_BUF_NEXT(inp->pkt_last) = m;
768		inp->pkt_last = m;
769	} else {
770		inp->pkt_last = inp->pkt = m;
771	}
772	if (
773	/* FreeBSD and MacOSX uses a flag passed */
774	    ((flags & PRUS_MORETOCOME) == 0)
775	    ) {
776		/*
777		 * note with the current version this code will only be used
778		 * by OpenBSD, NetBSD and FreeBSD have methods for
779		 * re-defining sosend() to use sctp_sosend().  One can
780		 * optionaly switch back to this code (by changing back the
781		 * defininitions but this is not advisable.
782		 */
783		struct epoch_tracker et;
784		int ret;
785
786		NET_EPOCH_ENTER(et);
787		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
788		NET_EPOCH_EXIT(et);
789		inp->pkt = NULL;
790		inp->control = NULL;
791		return (ret);
792	} else {
793		return (0);
794	}
795}
796
797static int
798sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
799{
800	struct epoch_tracker et;
801	uint32_t vrf_id;
802	int error = 0;
803	struct sctp_inpcb *inp;
804	struct sctp_tcb *stcb;
805#ifdef INET
806	struct sockaddr_in6 *sin6;
807	union sctp_sockstore store;
808#endif
809
810	inp = (struct sctp_inpcb *)so->so_pcb;
811	if (inp == NULL) {
812		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
813		return (ECONNRESET);	/* I made the same as TCP since we are
814					 * not setup? */
815	}
816	if (addr == NULL) {
817		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
818		return (EINVAL);
819	}
820	switch (addr->sa_family) {
821#ifdef INET
822	case AF_INET:
823		if (addr->sa_len != sizeof(struct sockaddr_in)) {
824			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
825			return (EINVAL);
826		}
827		break;
828#endif
829#ifdef INET6
830	case AF_INET6:
831		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
832			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
833			return (EINVAL);
834		}
835		break;
836#endif
837	default:
838		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
839		return (EINVAL);
840	}
841
842	vrf_id = inp->def_vrf_id;
843	SCTP_ASOC_CREATE_LOCK(inp);
844	SCTP_INP_RLOCK(inp);
845	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
846	    SCTP_PCB_FLAGS_UNBOUND) {
847		/* Bind a ephemeral port */
848		SCTP_INP_RUNLOCK(inp);
849		error = sctp6_bind(so, NULL, p);
850		if (error) {
851			SCTP_ASOC_CREATE_UNLOCK(inp);
852
853			return (error);
854		}
855		SCTP_INP_RLOCK(inp);
856	}
857	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
858	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
859		/* We are already connected AND the TCP model */
860		SCTP_INP_RUNLOCK(inp);
861		SCTP_ASOC_CREATE_UNLOCK(inp);
862		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
863		return (EADDRINUSE);
864	}
865#ifdef INET
866	sin6 = (struct sockaddr_in6 *)addr;
867	if (SCTP_IPV6_V6ONLY(inp)) {
868		/*
869		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
870		 * addr or v4-mapped addr
871		 */
872		if (addr->sa_family == AF_INET) {
873			SCTP_INP_RUNLOCK(inp);
874			SCTP_ASOC_CREATE_UNLOCK(inp);
875			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
876			return (EINVAL);
877		}
878		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
879			SCTP_INP_RUNLOCK(inp);
880			SCTP_ASOC_CREATE_UNLOCK(inp);
881			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
882			return (EINVAL);
883		}
884	}
885	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
886		/* convert v4-mapped into v4 addr */
887		in6_sin6_2_sin(&store.sin, sin6);
888		addr = &store.sa;
889	}
890#endif				/* INET */
891	/* Now do we connect? */
892	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
893		stcb = LIST_FIRST(&inp->sctp_asoc_list);
894		if (stcb) {
895			SCTP_TCB_LOCK(stcb);
896		}
897		SCTP_INP_RUNLOCK(inp);
898	} else {
899		SCTP_INP_RUNLOCK(inp);
900		SCTP_INP_WLOCK(inp);
901		SCTP_INP_INCR_REF(inp);
902		SCTP_INP_WUNLOCK(inp);
903		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
904		if (stcb == NULL) {
905			SCTP_INP_WLOCK(inp);
906			SCTP_INP_DECR_REF(inp);
907			SCTP_INP_WUNLOCK(inp);
908		}
909	}
910
911	if (stcb != NULL) {
912		/* Already have or am bring up an association */
913		SCTP_ASOC_CREATE_UNLOCK(inp);
914		SCTP_TCB_UNLOCK(stcb);
915		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
916		return (EALREADY);
917	}
918	/* We are GOOD to go */
919	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
920	    inp->sctp_ep.pre_open_stream_count,
921	    inp->sctp_ep.port, p,
922	    SCTP_INITIALIZE_AUTH_PARAMS);
923	SCTP_ASOC_CREATE_UNLOCK(inp);
924	if (stcb == NULL) {
925		/* Gak! no memory */
926		return (error);
927	}
928	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
929		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
930		/* Set the connected flag so we can queue data */
931		soisconnecting(so);
932	}
933	SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
934	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
935	NET_EPOCH_ENTER(et);
936	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
937	SCTP_TCB_UNLOCK(stcb);
938	NET_EPOCH_EXIT(et);
939	return (error);
940}
941
942static int
943sctp6_getaddr(struct socket *so, struct sockaddr **addr)
944{
945	struct sockaddr_in6 *sin6;
946	struct sctp_inpcb *inp;
947	uint32_t vrf_id;
948	struct sctp_ifa *sctp_ifa;
949
950	int error;
951
952	/*
953	 * Do the malloc first in case it blocks.
954	 */
955	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
956	if (sin6 == NULL)
957		return (ENOMEM);
958	sin6->sin6_family = AF_INET6;
959	sin6->sin6_len = sizeof(*sin6);
960
961	inp = (struct sctp_inpcb *)so->so_pcb;
962	if (inp == NULL) {
963		SCTP_FREE_SONAME(sin6);
964		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
965		return (ECONNRESET);
966	}
967	SCTP_INP_RLOCK(inp);
968	sin6->sin6_port = inp->sctp_lport;
969	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
970		/* For the bound all case you get back 0 */
971		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
972			struct sctp_tcb *stcb;
973			struct sockaddr_in6 *sin_a6;
974			struct sctp_nets *net;
975			int fnd;
976
977			stcb = LIST_FIRST(&inp->sctp_asoc_list);
978			if (stcb == NULL) {
979				SCTP_INP_RUNLOCK(inp);
980				SCTP_FREE_SONAME(sin6);
981				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
982				return (ENOENT);
983			}
984			fnd = 0;
985			sin_a6 = NULL;
986			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
987				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
988				if (sin_a6 == NULL)
989					/* this will make coverity happy */
990					continue;
991
992				if (sin_a6->sin6_family == AF_INET6) {
993					fnd = 1;
994					break;
995				}
996			}
997			if ((!fnd) || (sin_a6 == NULL)) {
998				/* punt */
999				SCTP_INP_RUNLOCK(inp);
1000				SCTP_FREE_SONAME(sin6);
1001				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1002				return (ENOENT);
1003			}
1004			vrf_id = inp->def_vrf_id;
1005			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
1006			if (sctp_ifa) {
1007				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1008			}
1009		} else {
1010			/* For the bound all case you get back 0 */
1011			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1012		}
1013	} else {
1014		/* Take the first IPv6 address in the list */
1015		struct sctp_laddr *laddr;
1016		int fnd = 0;
1017
1018		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1019			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1020				struct sockaddr_in6 *sin_a;
1021
1022				sin_a = &laddr->ifa->address.sin6;
1023				sin6->sin6_addr = sin_a->sin6_addr;
1024				fnd = 1;
1025				break;
1026			}
1027		}
1028		if (!fnd) {
1029			SCTP_FREE_SONAME(sin6);
1030			SCTP_INP_RUNLOCK(inp);
1031			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1032			return (ENOENT);
1033		}
1034	}
1035	SCTP_INP_RUNLOCK(inp);
1036	/* Scoping things for v6 */
1037	if ((error = sa6_recoverscope(sin6)) != 0) {
1038		SCTP_FREE_SONAME(sin6);
1039		return (error);
1040	}
1041	(*addr) = (struct sockaddr *)sin6;
1042	return (0);
1043}
1044
1045static int
1046sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1047{
1048	struct sockaddr_in6 *sin6;
1049	int fnd;
1050	struct sockaddr_in6 *sin_a6;
1051	struct sctp_inpcb *inp;
1052	struct sctp_tcb *stcb;
1053	struct sctp_nets *net;
1054	int error;
1055
1056	/* Do the malloc first in case it blocks. */
1057	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1058	if (sin6 == NULL)
1059		return (ENOMEM);
1060	sin6->sin6_family = AF_INET6;
1061	sin6->sin6_len = sizeof(*sin6);
1062
1063	inp = (struct sctp_inpcb *)so->so_pcb;
1064	if ((inp == NULL) ||
1065	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1066		/* UDP type and listeners will drop out here */
1067		SCTP_FREE_SONAME(sin6);
1068		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1069		return (ENOTCONN);
1070	}
1071	SCTP_INP_RLOCK(inp);
1072	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1073	if (stcb) {
1074		SCTP_TCB_LOCK(stcb);
1075	}
1076	SCTP_INP_RUNLOCK(inp);
1077	if (stcb == NULL) {
1078		SCTP_FREE_SONAME(sin6);
1079		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1080		return (ECONNRESET);
1081	}
1082	fnd = 0;
1083	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1084		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1085		if (sin_a6->sin6_family == AF_INET6) {
1086			fnd = 1;
1087			sin6->sin6_port = stcb->rport;
1088			sin6->sin6_addr = sin_a6->sin6_addr;
1089			break;
1090		}
1091	}
1092	SCTP_TCB_UNLOCK(stcb);
1093	if (!fnd) {
1094		/* No IPv4 address */
1095		SCTP_FREE_SONAME(sin6);
1096		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1097		return (ENOENT);
1098	}
1099	if ((error = sa6_recoverscope(sin6)) != 0) {
1100		SCTP_FREE_SONAME(sin6);
1101		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1102		return (error);
1103	}
1104	*addr = (struct sockaddr *)sin6;
1105	return (0);
1106}
1107
1108static int
1109sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1110{
1111	struct inpcb *inp = sotoinpcb(so);
1112	int error;
1113
1114	if (inp == NULL) {
1115		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1116		return (EINVAL);
1117	}
1118
1119	/* allow v6 addresses precedence */
1120	error = sctp6_getaddr(so, nam);
1121#ifdef INET
1122	if (error) {
1123		struct sockaddr_in6 *sin6;
1124
1125		/* try v4 next if v6 failed */
1126		error = sctp_ingetaddr(so, nam);
1127		if (error) {
1128			return (error);
1129		}
1130		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1131		if (sin6 == NULL) {
1132			SCTP_FREE_SONAME(*nam);
1133			return (ENOMEM);
1134		}
1135		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1136		SCTP_FREE_SONAME(*nam);
1137		*nam = (struct sockaddr *)sin6;
1138	}
1139#endif
1140	return (error);
1141}
1142
1143
1144static int
1145sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1146{
1147	struct inpcb *inp = sotoinpcb(so);
1148	int error;
1149
1150	if (inp == NULL) {
1151		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1152		return (EINVAL);
1153	}
1154
1155	/* allow v6 addresses precedence */
1156	error = sctp6_peeraddr(so, nam);
1157#ifdef INET
1158	if (error) {
1159		struct sockaddr_in6 *sin6;
1160
1161		/* try v4 next if v6 failed */
1162		error = sctp_peeraddr(so, nam);
1163		if (error) {
1164			return (error);
1165		}
1166		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1167		if (sin6 == NULL) {
1168			SCTP_FREE_SONAME(*nam);
1169			return (ENOMEM);
1170		}
1171		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1172		SCTP_FREE_SONAME(*nam);
1173		*nam = (struct sockaddr *)sin6;
1174	}
1175#endif
1176	return (error);
1177}
1178
1179struct pr_usrreqs sctp6_usrreqs = {
1180	.pru_abort = sctp6_abort,
1181	.pru_accept = sctp_accept,
1182	.pru_attach = sctp6_attach,
1183	.pru_bind = sctp6_bind,
1184	.pru_connect = sctp6_connect,
1185	.pru_control = in6_control,
1186	.pru_close = sctp6_close,
1187	.pru_detach = sctp6_close,
1188	.pru_sopoll = sopoll_generic,
1189	.pru_flush = sctp_flush,
1190	.pru_disconnect = sctp6_disconnect,
1191	.pru_listen = sctp_listen,
1192	.pru_peeraddr = sctp6_getpeeraddr,
1193	.pru_send = sctp6_send,
1194	.pru_shutdown = sctp_shutdown,
1195	.pru_sockaddr = sctp6_in6getaddr,
1196	.pru_sosend = sctp_sosend,
1197	.pru_soreceive = sctp_soreceive
1198};
1199
1200#endif
1201