udp6_usrreq.c revision 121901
1/*	$FreeBSD: head/sys/netinet6/udp6_usrreq.c 121901 2003-11-02 19:09:29Z ume $	*/
2/*	$KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $	*/
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * 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
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * Copyright (c) 1982, 1986, 1989, 1993
35 *	The Regents of the University of California.  All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 *    notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 *    notice, this list of conditions and the following disclaimer in the
44 *    documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 *    must display the following acknowledgement:
47 *	This product includes software developed by the University of
48 *	California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 *    may be used to endorse or promote products derived from this software
51 *    without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 *	@(#)udp_var.h	8.1 (Berkeley) 6/10/93
66 */
67
68#include "opt_inet.h"
69#include "opt_inet6.h"
70#include "opt_ipsec.h"
71
72#include <sys/param.h>
73#include <sys/errno.h>
74#include <sys/kernel.h>
75#include <sys/lock.h>
76#include <sys/mbuf.h>
77#include <sys/proc.h>
78#include <sys/protosw.h>
79#include <sys/signalvar.h>
80#include <sys/socket.h>
81#include <sys/socketvar.h>
82#include <sys/stat.h>
83#include <sys/sx.h>
84#include <sys/sysctl.h>
85#include <sys/syslog.h>
86#include <sys/systm.h>
87
88#include <net/if.h>
89#include <net/if_types.h>
90#include <net/route.h>
91
92#include <netinet/in.h>
93#include <netinet/in_pcb.h>
94#include <netinet/in_systm.h>
95#include <netinet/in_var.h>
96#include <netinet/ip.h>
97#include <netinet/ip6.h>
98#include <netinet/icmp6.h>
99#include <netinet/ip_var.h>
100#include <netinet/udp.h>
101#include <netinet/udp_var.h>
102#include <netinet6/ip6protosw.h>
103#include <netinet6/ip6_var.h>
104#include <netinet6/in6_pcb.h>
105#include <netinet6/udp6_var.h>
106
107#ifdef IPSEC
108#include <netinet6/ipsec.h>
109#include <netinet6/ipsec6.h>
110#endif /* IPSEC */
111
112#ifdef FAST_IPSEC
113#include <netipsec/ipsec.h>
114#include <netipsec/ipsec6.h>
115#endif /* FAST_IPSEC */
116
117/*
118 * UDP protocol inplementation.
119 * Per RFC 768, August, 1980.
120 */
121
122extern	struct protosw inetsw[];
123static	int in6_mcmatch __P((struct inpcb *, struct in6_addr *, struct ifnet *));
124static	int udp6_detach __P((struct socket *so));
125
126static int
127in6_mcmatch(in6p, ia6, ifp)
128	struct inpcb *in6p;
129	register struct in6_addr *ia6;
130	struct ifnet *ifp;
131{
132	struct ip6_moptions *im6o = in6p->in6p_moptions;
133	struct in6_multi_mship *imm;
134
135	if (im6o == NULL)
136		return 0;
137
138	for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
139	     imm = imm->i6mm_chain.le_next) {
140		if ((ifp == NULL ||
141		     imm->i6mm_maddr->in6m_ifp == ifp) &&
142		    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
143				       ia6))
144			return 1;
145	}
146	return 0;
147}
148
149int
150udp6_input(mp, offp, proto)
151	struct mbuf **mp;
152	int *offp, proto;
153{
154	struct mbuf *m = *mp;
155	register struct ip6_hdr *ip6;
156	register struct udphdr *uh;
157	register struct inpcb *in6p;
158	struct  mbuf *opts = NULL;
159	int off = *offp;
160	int plen, ulen;
161	struct sockaddr_in6 fromsa;
162
163	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
164
165	ip6 = mtod(m, struct ip6_hdr *);
166
167	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
168		/* XXX send icmp6 host/port unreach? */
169		m_freem(m);
170		return IPPROTO_DONE;
171	}
172
173	udpstat.udps_ipackets++;
174
175	plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
176	uh = (struct udphdr *)((caddr_t)ip6 + off);
177	ulen = ntohs((u_short)uh->uh_ulen);
178
179	if (plen != ulen) {
180		udpstat.udps_badlen++;
181		goto bad;
182	}
183
184	/*
185	 * Checksum extended UDP header and data.
186	 */
187	if (uh->uh_sum == 0)
188		udpstat.udps_nosum++;
189	else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
190		udpstat.udps_badsum++;
191		goto bad;
192	}
193
194	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
195		struct	inpcb *last;
196
197		/*
198		 * Deliver a multicast datagram to all sockets
199		 * for which the local and remote addresses and ports match
200		 * those of the incoming datagram.  This allows more than
201		 * one process to receive multicasts on the same port.
202		 * (This really ought to be done for unicast datagrams as
203		 * well, but that would cause problems with existing
204		 * applications that open both address-specific sockets and
205		 * a wildcard socket listening to the same port -- they would
206		 * end up receiving duplicates of every unicast datagram.
207		 * Those applications open the multiple sockets to overcome an
208		 * inadequacy of the UDP socket interface, but for backwards
209		 * compatibility we avoid the problem here rather than
210		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
211		 */
212
213		/*
214		 * In a case that laddr should be set to the link-local
215		 * address (this happens in RIPng), the multicast address
216		 * specified in the received packet does not match with
217		 * laddr. To cure this situation, the matching is relaxed
218		 * if the receiving interface is the same as one specified
219		 * in the socket and if the destination multicast address
220		 * matches one of the multicast groups specified in the socket.
221		 */
222
223		/*
224		 * Construct sockaddr format source address.
225		 */
226		init_sin6(&fromsa, m); /* general init */
227		fromsa.sin6_port = uh->uh_sport;
228		/*
229		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
230		 * We need udphdr for IPsec processing so we do that later.
231		 */
232
233		/*
234		 * Locate pcb(s) for datagram.
235		 * (Algorithm copied from raw_intr().)
236		 */
237		last = NULL;
238		LIST_FOREACH(in6p, &udb, inp_list) {
239			if ((in6p->inp_vflag & INP_IPV6) == 0)
240				continue;
241			if (in6p->in6p_lport != uh->uh_dport)
242				continue;
243			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
244				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
245							&ip6->ip6_dst) &&
246				    !in6_mcmatch(in6p, &ip6->ip6_dst,
247						 m->m_pkthdr.rcvif))
248					continue;
249			}
250			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
251				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
252							&ip6->ip6_src) ||
253				   in6p->in6p_fport != uh->uh_sport)
254					continue;
255			}
256
257			if (last != NULL) {
258				struct	mbuf *n;
259
260#ifdef IPSEC
261				/*
262				 * Check AH/ESP integrity.
263				 */
264				if (ipsec6_in_reject(m, last))
265					ipsec6stat.in_polvio++;
266					/* do not inject data into pcb */
267				else
268#endif /* IPSEC */
269#ifdef FAST_IPSEC
270				/*
271				 * Check AH/ESP integrity.
272				 */
273				if (ipsec6_in_reject(m, last))
274					;
275				else
276#endif /* FAST_IPSEC */
277				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
278					/*
279					 * KAME NOTE: do not
280					 * m_copy(m, offset, ...) above.
281					 * sbappendaddr() expects M_PKTHDR,
282					 * and m_copy() will copy M_PKTHDR
283					 * only if offset is 0.
284					 */
285					if (last->in6p_flags & IN6P_CONTROLOPTS
286					    || last->in6p_socket->so_options & SO_TIMESTAMP)
287						ip6_savecontrol(last, n, &opts);
288
289					m_adj(n, off + sizeof(struct udphdr));
290					if (sbappendaddr(&last->in6p_socket->so_rcv,
291							 (struct sockaddr *)&fromsa,
292							n, opts) == 0) {
293						m_freem(n);
294						if (opts)
295							m_freem(opts);
296						udpstat.udps_fullsock++;
297					} else
298						sorwakeup(last->in6p_socket);
299					opts = NULL;
300				}
301			}
302			last = in6p;
303			/*
304			 * Don't look for additional matches if this one does
305			 * not have either the SO_REUSEPORT or SO_REUSEADDR
306			 * socket options set.  This heuristic avoids searching
307			 * through all pcbs in the common case of a non-shared
308			 * port.  It assumes that an application will never
309			 * clear these options after setting them.
310			 */
311			if ((last->in6p_socket->so_options &
312			     (SO_REUSEPORT|SO_REUSEADDR)) == 0)
313				break;
314		}
315
316		if (last == NULL) {
317			/*
318			 * No matching pcb found; discard datagram.
319			 * (No need to send an ICMP Port Unreachable
320			 * for a broadcast or multicast datgram.)
321			 */
322			udpstat.udps_noport++;
323			udpstat.udps_noportmcast++;
324			goto bad;
325		}
326#ifdef IPSEC
327		/*
328		 * Check AH/ESP integrity.
329		 */
330		if (ipsec6_in_reject(m, last)) {
331			ipsec6stat.in_polvio++;
332			goto bad;
333		}
334#endif /* IPSEC */
335#ifdef FAST_IPSEC
336		/*
337		 * Check AH/ESP integrity.
338		 */
339		if (ipsec6_in_reject(m, last)) {
340			goto bad;
341		}
342#endif /* FAST_IPSEC */
343		if (last->in6p_flags & IN6P_CONTROLOPTS
344		    || last->in6p_socket->so_options & SO_TIMESTAMP)
345			ip6_savecontrol(last, m, &opts);
346
347		m_adj(m, off + sizeof(struct udphdr));
348		if (sbappendaddr(&last->in6p_socket->so_rcv,
349				(struct sockaddr *)&fromsa,
350				m, opts) == 0) {
351			udpstat.udps_fullsock++;
352			goto bad;
353		}
354		sorwakeup(last->in6p_socket);
355		return IPPROTO_DONE;
356	}
357	/*
358	 * Locate pcb for datagram.
359	 */
360	in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
361				  &ip6->ip6_dst, uh->uh_dport, 1,
362				  m->m_pkthdr.rcvif);
363	if (in6p == 0) {
364		if (log_in_vain) {
365			char buf[INET6_ADDRSTRLEN];
366
367			strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
368			log(LOG_INFO,
369			    "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
370			    buf, ntohs(uh->uh_dport),
371			    ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
372		}
373		udpstat.udps_noport++;
374		if (m->m_flags & M_MCAST) {
375			printf("UDP6: M_MCAST is set in a unicast packet.\n");
376			udpstat.udps_noportmcast++;
377			goto bad;
378		}
379		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
380		return IPPROTO_DONE;
381	}
382#ifdef IPSEC
383	/*
384	 * Check AH/ESP integrity.
385	 */
386	if (ipsec6_in_reject(m, in6p)) {
387		ipsec6stat.in_polvio++;
388		goto bad;
389	}
390#endif /* IPSEC */
391#ifdef FAST_IPSEC
392	/*
393	 * Check AH/ESP integrity.
394	 */
395	if (ipsec6_in_reject(m, in6p)) {
396		goto bad;
397	}
398#endif /* FAST_IPSEC */
399
400	/*
401	 * Construct sockaddr format source address.
402	 * Stuff source address and datagram in user buffer.
403	 */
404	init_sin6(&fromsa, m); /* general init */
405	fromsa.sin6_port = uh->uh_sport;
406	if (in6p->in6p_flags & IN6P_CONTROLOPTS
407	    || in6p->in6p_socket->so_options & SO_TIMESTAMP)
408		ip6_savecontrol(in6p, m, &opts);
409	m_adj(m, off + sizeof(struct udphdr));
410	if (sbappendaddr(&in6p->in6p_socket->so_rcv,
411			(struct sockaddr *)&fromsa,
412			m, opts) == 0) {
413		udpstat.udps_fullsock++;
414		goto bad;
415	}
416	sorwakeup(in6p->in6p_socket);
417	return IPPROTO_DONE;
418bad:
419	if (m)
420		m_freem(m);
421	if (opts)
422		m_freem(opts);
423	return IPPROTO_DONE;
424}
425
426void
427udp6_ctlinput(cmd, sa, d)
428	int cmd;
429	struct sockaddr *sa;
430	void *d;
431{
432	struct udphdr uh;
433	struct ip6_hdr *ip6;
434	struct mbuf *m;
435	int off = 0;
436	struct ip6ctlparam *ip6cp = NULL;
437	const struct sockaddr_in6 *sa6_src = NULL;
438	struct inpcb *(*notify) __P((struct inpcb *, int)) = udp_notify;
439	struct udp_portonly {
440		u_int16_t uh_sport;
441		u_int16_t uh_dport;
442	} *uhp;
443
444	if (sa->sa_family != AF_INET6 ||
445	    sa->sa_len != sizeof(struct sockaddr_in6))
446		return;
447
448	if ((unsigned)cmd >= PRC_NCMDS)
449		return;
450	if (PRC_IS_REDIRECT(cmd))
451		notify = in6_rtchange, d = NULL;
452	else if (cmd == PRC_HOSTDEAD)
453		d = NULL;
454	else if (inet6ctlerrmap[cmd] == 0)
455		return;
456
457	/* if the parameter is from icmp6, decode it. */
458	if (d != NULL) {
459		ip6cp = (struct ip6ctlparam *)d;
460		m = ip6cp->ip6c_m;
461		ip6 = ip6cp->ip6c_ip6;
462		off = ip6cp->ip6c_off;
463		sa6_src = ip6cp->ip6c_src;
464	} else {
465		m = NULL;
466		ip6 = NULL;
467		sa6_src = &sa6_any;
468	}
469
470	if (ip6) {
471		/*
472		 * XXX: We assume that when IPV6 is non NULL,
473		 * M and OFF are valid.
474		 */
475
476		/* check if we can safely examine src and dst ports */
477		if (m->m_pkthdr.len < off + sizeof(*uhp))
478			return;
479
480		bzero(&uh, sizeof(uh));
481		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
482
483		(void) in6_pcbnotify(&udb, sa, uh.uh_dport,
484				     (struct sockaddr *)ip6cp->ip6c_src,
485				     uh.uh_sport, cmd, notify);
486	} else
487		(void) in6_pcbnotify(&udb, sa, 0,
488				     (const struct sockaddr *)sa6_src,
489				     0, cmd, notify);
490}
491
492static int
493udp6_getcred(SYSCTL_HANDLER_ARGS)
494{
495	struct xucred xuc;
496	struct sockaddr_in6 addrs[2];
497	struct inpcb *inp;
498	int error, s;
499
500	error = suser(req->td);
501	if (error)
502		return (error);
503
504	if (req->newlen != sizeof(addrs))
505		return (EINVAL);
506	if (req->oldlen != sizeof(struct xucred))
507		return (EINVAL);
508	error = SYSCTL_IN(req, addrs, sizeof(addrs));
509	if (error)
510		return (error);
511	s = splnet();
512	inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
513				 addrs[1].sin6_port,
514				 &addrs[0].sin6_addr, addrs[0].sin6_port,
515				 1, NULL);
516	if (!inp || !inp->inp_socket) {
517		error = ENOENT;
518		goto out;
519	}
520	cru2x(inp->inp_socket->so_cred, &xuc);
521	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
522out:
523	splx(s);
524	return (error);
525}
526
527SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
528	    0, 0,
529	    udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
530
531static int
532udp6_abort(struct socket *so)
533{
534	struct inpcb *inp;
535	int s;
536
537	inp = sotoinpcb(so);
538	if (inp == 0)
539		return EINVAL;	/* ??? possible? panic instead? */
540	soisdisconnected(so);
541	s = splnet();
542	in6_pcbdetach(inp);
543	splx(s);
544	return 0;
545}
546
547static int
548udp6_attach(struct socket *so, int proto, struct thread *td)
549{
550	struct inpcb *inp;
551	int s, error;
552
553	inp = sotoinpcb(so);
554	if (inp != 0)
555		return EINVAL;
556
557	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
558		error = soreserve(so, udp_sendspace, udp_recvspace);
559		if (error)
560			return error;
561	}
562	s = splnet();
563	error = in_pcballoc(so, &udbinfo, td);
564	splx(s);
565	if (error)
566		return error;
567	inp = (struct inpcb *)so->so_pcb;
568	inp->inp_vflag |= INP_IPV6;
569	if (!ip6_v6only)
570		inp->inp_vflag |= INP_IPV4;
571	inp->in6p_hops = -1;	/* use kernel default */
572	inp->in6p_cksum = -1;	/* just to be sure */
573	/*
574	 * XXX: ugly!!
575	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
576	 * because the socket may be bound to an IPv6 wildcard address,
577	 * which may match an IPv4-mapped IPv6 address.
578	 */
579	inp->inp_ip_ttl = ip_defttl;
580	return 0;
581}
582
583static int
584udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
585{
586	struct inpcb *inp;
587	int s, error;
588
589	inp = sotoinpcb(so);
590	if (inp == 0)
591		return EINVAL;
592
593	inp->inp_vflag &= ~INP_IPV4;
594	inp->inp_vflag |= INP_IPV6;
595	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
596		struct sockaddr_in6 *sin6_p;
597
598		sin6_p = (struct sockaddr_in6 *)nam;
599
600		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
601			inp->inp_vflag |= INP_IPV4;
602		else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
603			struct sockaddr_in sin;
604
605			in6_sin6_2_sin(&sin, sin6_p);
606			inp->inp_vflag |= INP_IPV4;
607			inp->inp_vflag &= ~INP_IPV6;
608			s = splnet();
609			error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
610			splx(s);
611			return error;
612		}
613	}
614
615	s = splnet();
616	error = in6_pcbbind(inp, nam, td);
617	splx(s);
618	return error;
619}
620
621static int
622udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
623{
624	struct inpcb *inp;
625	int s, error;
626
627	inp = sotoinpcb(so);
628	if (inp == 0)
629		return EINVAL;
630
631	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
632		struct sockaddr_in6 *sin6_p;
633
634		sin6_p = (struct sockaddr_in6 *)nam;
635		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
636			struct sockaddr_in sin;
637
638			if (inp->inp_faddr.s_addr != INADDR_ANY)
639				return EISCONN;
640			in6_sin6_2_sin(&sin, sin6_p);
641			s = splnet();
642			error = in_pcbconnect(inp, (struct sockaddr *)&sin, td);
643			splx(s);
644			if (error == 0) {
645				inp->inp_vflag |= INP_IPV4;
646				inp->inp_vflag &= ~INP_IPV6;
647				soisconnected(so);
648			}
649			return error;
650		}
651	}
652	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
653		return EISCONN;
654	s = splnet();
655	error = in6_pcbconnect(inp, nam, td);
656	splx(s);
657	if (error == 0) {
658		if (!ip6_v6only) { /* should be non mapped addr */
659			inp->inp_vflag &= ~INP_IPV4;
660			inp->inp_vflag |= INP_IPV6;
661		}
662		soisconnected(so);
663	}
664	return error;
665}
666
667static int
668udp6_detach(struct socket *so)
669{
670	struct inpcb *inp;
671	int s;
672
673	inp = sotoinpcb(so);
674	if (inp == 0)
675		return EINVAL;
676	s = splnet();
677	in6_pcbdetach(inp);
678	splx(s);
679	return 0;
680}
681
682static int
683udp6_disconnect(struct socket *so)
684{
685	struct inpcb *inp;
686	int s;
687
688	inp = sotoinpcb(so);
689	if (inp == 0)
690		return EINVAL;
691
692	if (inp->inp_vflag & INP_IPV4) {
693		struct pr_usrreqs *pru;
694
695		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
696		return ((*pru->pru_disconnect)(so));
697	}
698
699	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
700		return ENOTCONN;
701
702	s = splnet();
703	in6_pcbdisconnect(inp);
704	inp->in6p_laddr = in6addr_any;
705	splx(s);
706	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
707	return 0;
708}
709
710static int
711udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
712	  struct mbuf *control, struct thread *td)
713{
714	struct inpcb *inp;
715	int error = 0;
716
717	inp = sotoinpcb(so);
718	if (inp == 0) {
719		error = EINVAL;
720		goto bad;
721	}
722
723	if (addr) {
724		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
725			error = EINVAL;
726			goto bad;
727		}
728		if (addr->sa_family != AF_INET6) {
729			error = EAFNOSUPPORT;
730			goto bad;
731		}
732	}
733
734	if (!ip6_v6only) {
735		int hasv4addr;
736		struct sockaddr_in6 *sin6 = 0;
737
738		if (addr == 0)
739			hasv4addr = (inp->inp_vflag & INP_IPV4);
740		else {
741			sin6 = (struct sockaddr_in6 *)addr;
742			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
743				? 1 : 0;
744		}
745		if (hasv4addr) {
746			struct pr_usrreqs *pru;
747
748			if (sin6)
749				in6_sin6_2_sin_in_sock(addr);
750			pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
751			error = ((*pru->pru_send)(so, flags, m, addr, control,
752						  td));
753			/* addr will just be freed in sendit(). */
754			return error;
755		}
756	}
757
758	return udp6_output(inp, m, addr, control, td);
759
760  bad:
761	m_freem(m);
762	return (error);
763}
764
765struct pr_usrreqs udp6_usrreqs = {
766	udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect,
767	pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect,
768	pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp,
769	pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown,
770	in6_mapped_sockaddr, sosend, soreceive, sopoll
771};
772