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