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