xref: /freebsd/sys/netinet6/udp6_usrreq.c (revision 313376588638950ba1e93c403dd8c97bc52fd3a2)
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * Copyright (c) 2010-2011 Juniper Networks, Inc.
4  * Copyright (c) 2014 Kevin Lo
5  * All rights reserved.
6  *
7  * Portions of this software were developed by Robert N. M. Watson under
8  * contract to Juniper Networks, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the project nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	$KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
35  *	$KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
36  */
37 
38 /*-
39  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
40  *	The Regents of the University of California.
41  * All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 4. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  *
67  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
68  */
69 
70 #include <sys/cdefs.h>
71 __FBSDID("$FreeBSD$");
72 
73 #include "opt_inet.h"
74 #include "opt_inet6.h"
75 #include "opt_ipfw.h"
76 #include "opt_ipsec.h"
77 #include "opt_rss.h"
78 
79 #include <sys/param.h>
80 #include <sys/jail.h>
81 #include <sys/kernel.h>
82 #include <sys/lock.h>
83 #include <sys/mbuf.h>
84 #include <sys/priv.h>
85 #include <sys/proc.h>
86 #include <sys/protosw.h>
87 #include <sys/sdt.h>
88 #include <sys/signalvar.h>
89 #include <sys/socket.h>
90 #include <sys/socketvar.h>
91 #include <sys/sx.h>
92 #include <sys/sysctl.h>
93 #include <sys/syslog.h>
94 #include <sys/systm.h>
95 
96 #include <net/if.h>
97 #include <net/if_var.h>
98 #include <net/if_types.h>
99 #include <net/route.h>
100 
101 #include <netinet/in.h>
102 #include <netinet/in_kdtrace.h>
103 #include <netinet/in_pcb.h>
104 #include <netinet/in_systm.h>
105 #include <netinet/in_var.h>
106 #include <netinet/ip.h>
107 #include <netinet/ip_icmp.h>
108 #include <netinet/ip6.h>
109 #include <netinet/icmp_var.h>
110 #include <netinet/icmp6.h>
111 #include <netinet/ip_var.h>
112 #include <netinet/udp.h>
113 #include <netinet/udp_var.h>
114 #include <netinet/udplite.h>
115 #include <netinet/in_rss.h>
116 
117 #include <netinet6/ip6protosw.h>
118 #include <netinet6/ip6_var.h>
119 #include <netinet6/in6_pcb.h>
120 #include <netinet6/udp6_var.h>
121 #include <netinet6/scope6_var.h>
122 
123 #ifdef IPSEC
124 #include <netipsec/ipsec.h>
125 #include <netipsec/ipsec6.h>
126 #endif /* IPSEC */
127 
128 #include <security/mac/mac_framework.h>
129 
130 /*
131  * UDP protocol implementation.
132  * Per RFC 768, August, 1980.
133  */
134 
135 extern struct protosw	inetsw[];
136 static void		udp6_detach(struct socket *so);
137 
138 static void
139 udp6_append(struct inpcb *inp, struct mbuf *n, int off,
140     struct sockaddr_in6 *fromsa)
141 {
142 	struct socket *so;
143 	struct mbuf *opts;
144 	struct udpcb *up;
145 
146 	INP_LOCK_ASSERT(inp);
147 
148 	/*
149 	 * Engage the tunneling protocol.
150 	 */
151 	up = intoudpcb(inp);
152 	if (up->u_tun_func != NULL) {
153 		(*up->u_tun_func)(n, off, inp, (struct sockaddr *)fromsa,
154 		    up->u_tun_ctx);
155 		return;
156 	}
157 #ifdef IPSEC
158 	/* Check AH/ESP integrity. */
159 	if (ipsec6_in_reject(n, inp)) {
160 		m_freem(n);
161 		IPSEC6STAT_INC(ips_in_polvio);
162 		return;
163 	}
164 #endif /* IPSEC */
165 #ifdef MAC
166 	if (mac_inpcb_check_deliver(inp, n) != 0) {
167 		m_freem(n);
168 		return;
169 	}
170 #endif
171 	opts = NULL;
172 	if (inp->inp_flags & INP_CONTROLOPTS ||
173 	    inp->inp_socket->so_options & SO_TIMESTAMP)
174 		ip6_savecontrol(inp, n, &opts);
175 	m_adj(n, off + sizeof(struct udphdr));
176 
177 	so = inp->inp_socket;
178 	SOCKBUF_LOCK(&so->so_rcv);
179 	if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
180 	    opts) == 0) {
181 		SOCKBUF_UNLOCK(&so->so_rcv);
182 		m_freem(n);
183 		if (opts)
184 			m_freem(opts);
185 		UDPSTAT_INC(udps_fullsock);
186 	} else
187 		sorwakeup_locked(so);
188 }
189 
190 int
191 udp6_input(struct mbuf **mp, int *offp, int proto)
192 {
193 	struct mbuf *m = *mp;
194 	struct ifnet *ifp;
195 	struct ip6_hdr *ip6;
196 	struct udphdr *uh;
197 	struct inpcb *inp;
198 	struct inpcbinfo *pcbinfo;
199 	struct udpcb *up;
200 	int off = *offp;
201 	int cscov_partial;
202 	int plen, ulen;
203 	struct sockaddr_in6 fromsa;
204 	struct m_tag *fwd_tag;
205 	uint16_t uh_sum;
206 	uint8_t nxt;
207 
208 	ifp = m->m_pkthdr.rcvif;
209 	ip6 = mtod(m, struct ip6_hdr *);
210 
211 	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
212 		/* XXX send icmp6 host/port unreach? */
213 		m_freem(m);
214 		return (IPPROTO_DONE);
215 	}
216 
217 #ifndef PULLDOWN_TEST
218 	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
219 	ip6 = mtod(m, struct ip6_hdr *);
220 	uh = (struct udphdr *)((caddr_t)ip6 + off);
221 #else
222 	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
223 	if (!uh)
224 		return (IPPROTO_DONE);
225 #endif
226 
227 	UDPSTAT_INC(udps_ipackets);
228 
229 	/*
230 	 * Destination port of 0 is illegal, based on RFC768.
231 	 */
232 	if (uh->uh_dport == 0)
233 		goto badunlocked;
234 
235 	plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
236 	ulen = ntohs((u_short)uh->uh_ulen);
237 
238 	nxt = ip6->ip6_nxt;
239 	cscov_partial = (nxt == IPPROTO_UDPLITE) ? 1 : 0;
240 	if (nxt == IPPROTO_UDPLITE) {
241 		/* Zero means checksum over the complete packet. */
242 		if (ulen == 0)
243 			ulen = plen;
244 		if (ulen == plen)
245 			cscov_partial = 0;
246 		if ((ulen < sizeof(struct udphdr)) || (ulen > plen)) {
247 			/* XXX: What is the right UDPLite MIB counter? */
248 			goto badunlocked;
249 		}
250 		if (uh->uh_sum == 0) {
251 			/* XXX: What is the right UDPLite MIB counter? */
252 			goto badunlocked;
253 		}
254 	} else {
255 		if ((ulen < sizeof(struct udphdr)) || (plen != ulen)) {
256 			UDPSTAT_INC(udps_badlen);
257 			goto badunlocked;
258 		}
259 		if (uh->uh_sum == 0) {
260 			UDPSTAT_INC(udps_nosum);
261 			goto badunlocked;
262 		}
263 	}
264 
265 	if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) &&
266 	    !cscov_partial) {
267 		if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
268 			uh_sum = m->m_pkthdr.csum_data;
269 		else
270 			uh_sum = in6_cksum_pseudo(ip6, ulen, nxt,
271 			    m->m_pkthdr.csum_data);
272 		uh_sum ^= 0xffff;
273 	} else
274 		uh_sum = in6_cksum_partial(m, nxt, off, plen, ulen);
275 
276 	if (uh_sum != 0) {
277 		UDPSTAT_INC(udps_badsum);
278 		goto badunlocked;
279 	}
280 
281 	/*
282 	 * Construct sockaddr format source address.
283 	 */
284 	init_sin6(&fromsa, m);
285 	fromsa.sin6_port = uh->uh_sport;
286 
287 	pcbinfo = get_inpcbinfo(nxt);
288 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
289 		struct inpcb *last;
290 		struct inpcbhead *pcblist;
291 		struct ip6_moptions *imo;
292 
293 		INP_INFO_RLOCK(pcbinfo);
294 		/*
295 		 * In the event that laddr should be set to the link-local
296 		 * address (this happens in RIPng), the multicast address
297 		 * specified in the received packet will not match laddr.  To
298 		 * handle this situation, matching is relaxed if the
299 		 * receiving interface is the same as one specified in the
300 		 * socket and if the destination multicast address matches
301 		 * one of the multicast groups specified in the socket.
302 		 */
303 
304 		/*
305 		 * KAME note: traditionally we dropped udpiphdr from mbuf
306 		 * here.  We need udphdr for IPsec processing so we do that
307 		 * later.
308 		 */
309 		pcblist = get_pcblist(nxt);
310 		last = NULL;
311 		LIST_FOREACH(inp, pcblist, inp_list) {
312 			if ((inp->inp_vflag & INP_IPV6) == 0)
313 				continue;
314 			if (inp->inp_lport != uh->uh_dport)
315 				continue;
316 			if (inp->inp_fport != 0 &&
317 			    inp->inp_fport != uh->uh_sport)
318 				continue;
319 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
320 				if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
321 							&ip6->ip6_dst))
322 					continue;
323 			}
324 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
325 				if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
326 							&ip6->ip6_src) ||
327 				    inp->inp_fport != uh->uh_sport)
328 					continue;
329 			}
330 
331 			/*
332 			 * XXXRW: Because we weren't holding either the inpcb
333 			 * or the hash lock when we checked for a match
334 			 * before, we should probably recheck now that the
335 			 * inpcb lock is (supposed to be) held.
336 			 */
337 
338 			/*
339 			 * Handle socket delivery policy for any-source
340 			 * and source-specific multicast. [RFC3678]
341 			 */
342 			imo = inp->in6p_moptions;
343 			if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
344 				struct sockaddr_in6	 mcaddr;
345 				int			 blocked;
346 
347 				INP_RLOCK(inp);
348 
349 				bzero(&mcaddr, sizeof(struct sockaddr_in6));
350 				mcaddr.sin6_len = sizeof(struct sockaddr_in6);
351 				mcaddr.sin6_family = AF_INET6;
352 				mcaddr.sin6_addr = ip6->ip6_dst;
353 
354 				blocked = im6o_mc_filter(imo, ifp,
355 					(struct sockaddr *)&mcaddr,
356 					(struct sockaddr *)&fromsa);
357 				if (blocked != MCAST_PASS) {
358 					if (blocked == MCAST_NOTGMEMBER)
359 						IP6STAT_INC(ip6s_notmember);
360 					if (blocked == MCAST_NOTSMEMBER ||
361 					    blocked == MCAST_MUTED)
362 						UDPSTAT_INC(udps_filtermcast);
363 					INP_RUNLOCK(inp); /* XXX */
364 					continue;
365 				}
366 
367 				INP_RUNLOCK(inp);
368 			}
369 			if (last != NULL) {
370 				struct mbuf *n;
371 
372 				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
373 					INP_RLOCK(last);
374 					UDP_PROBE(receive, NULL, last, ip6,
375 					    last, uh);
376 					udp6_append(last, n, off, &fromsa);
377 					INP_RUNLOCK(last);
378 				}
379 			}
380 			last = inp;
381 			/*
382 			 * Don't look for additional matches if this one does
383 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
384 			 * socket options set.  This heuristic avoids
385 			 * searching through all pcbs in the common case of a
386 			 * non-shared port.  It assumes that an application
387 			 * will never clear these options after setting them.
388 			 */
389 			if ((last->inp_socket->so_options &
390 			     (SO_REUSEPORT|SO_REUSEADDR)) == 0)
391 				break;
392 		}
393 
394 		if (last == NULL) {
395 			/*
396 			 * No matching pcb found; discard datagram.  (No need
397 			 * to send an ICMP Port Unreachable for a broadcast
398 			 * or multicast datgram.)
399 			 */
400 			UDPSTAT_INC(udps_noport);
401 			UDPSTAT_INC(udps_noportmcast);
402 			goto badheadlocked;
403 		}
404 		INP_RLOCK(last);
405 		INP_INFO_RUNLOCK(pcbinfo);
406 		UDP_PROBE(receive, NULL, last, ip6, last, uh);
407 		udp6_append(last, m, off, &fromsa);
408 		INP_RUNLOCK(last);
409 		return (IPPROTO_DONE);
410 	}
411 	/*
412 	 * Locate pcb for datagram.
413 	 */
414 
415 	/*
416 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
417 	 */
418 	if ((m->m_flags & M_IP6_NEXTHOP) &&
419 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
420 		struct sockaddr_in6 *next_hop6;
421 
422 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
423 
424 		/*
425 		 * Transparently forwarded. Pretend to be the destination.
426 		 * Already got one like this?
427 		 */
428 		inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
429 		    uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
430 		    INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif, m);
431 		if (!inp) {
432 			/*
433 			 * It's new.  Try to find the ambushing socket.
434 			 * Because we've rewritten the destination address,
435 			 * any hardware-generated hash is ignored.
436 			 */
437 			inp = in6_pcblookup(pcbinfo, &ip6->ip6_src,
438 			    uh->uh_sport, &next_hop6->sin6_addr,
439 			    next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
440 			    uh->uh_dport, INPLOOKUP_WILDCARD |
441 			    INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif);
442 		}
443 		/* Remove the tag from the packet. We don't need it anymore. */
444 		m_tag_delete(m, fwd_tag);
445 		m->m_flags &= ~M_IP6_NEXTHOP;
446 	} else
447 		inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
448 		    uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
449 		    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
450 		    m->m_pkthdr.rcvif, m);
451 	if (inp == NULL) {
452 		if (udp_log_in_vain) {
453 			char ip6bufs[INET6_ADDRSTRLEN];
454 			char ip6bufd[INET6_ADDRSTRLEN];
455 
456 			log(LOG_INFO,
457 			    "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
458 			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
459 			    ntohs(uh->uh_dport),
460 			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
461 			    ntohs(uh->uh_sport));
462 		}
463 		UDPSTAT_INC(udps_noport);
464 		if (m->m_flags & M_MCAST) {
465 			printf("UDP6: M_MCAST is set in a unicast packet.\n");
466 			UDPSTAT_INC(udps_noportmcast);
467 			goto badunlocked;
468 		}
469 		if (V_udp_blackhole)
470 			goto badunlocked;
471 		if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
472 			goto badunlocked;
473 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
474 		return (IPPROTO_DONE);
475 	}
476 	INP_RLOCK_ASSERT(inp);
477 	up = intoudpcb(inp);
478 	if (cscov_partial) {
479 		if (up->u_rxcslen == 0 || up->u_rxcslen > ulen) {
480 			INP_RUNLOCK(inp);
481 			m_freem(m);
482 			return (IPPROTO_DONE);
483 		}
484 	}
485 	UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
486 	udp6_append(inp, m, off, &fromsa);
487 	INP_RUNLOCK(inp);
488 	return (IPPROTO_DONE);
489 
490 badheadlocked:
491 	INP_INFO_RUNLOCK(pcbinfo);
492 badunlocked:
493 	if (m)
494 		m_freem(m);
495 	return (IPPROTO_DONE);
496 }
497 
498 static void
499 udp6_common_ctlinput(int cmd, struct sockaddr *sa, void *d,
500     struct inpcbinfo *pcbinfo)
501 {
502 	struct udphdr uh;
503 	struct ip6_hdr *ip6;
504 	struct mbuf *m;
505 	int off = 0;
506 	struct ip6ctlparam *ip6cp = NULL;
507 	const struct sockaddr_in6 *sa6_src = NULL;
508 	void *cmdarg;
509 	struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
510 	struct udp_portonly {
511 		u_int16_t uh_sport;
512 		u_int16_t uh_dport;
513 	} *uhp;
514 
515 	if (sa->sa_family != AF_INET6 ||
516 	    sa->sa_len != sizeof(struct sockaddr_in6))
517 		return;
518 
519 	if ((unsigned)cmd >= PRC_NCMDS)
520 		return;
521 	if (PRC_IS_REDIRECT(cmd))
522 		notify = in6_rtchange, d = NULL;
523 	else if (cmd == PRC_HOSTDEAD)
524 		d = NULL;
525 	else if (inet6ctlerrmap[cmd] == 0)
526 		return;
527 
528 	/* if the parameter is from icmp6, decode it. */
529 	if (d != NULL) {
530 		ip6cp = (struct ip6ctlparam *)d;
531 		m = ip6cp->ip6c_m;
532 		ip6 = ip6cp->ip6c_ip6;
533 		off = ip6cp->ip6c_off;
534 		cmdarg = ip6cp->ip6c_cmdarg;
535 		sa6_src = ip6cp->ip6c_src;
536 	} else {
537 		m = NULL;
538 		ip6 = NULL;
539 		cmdarg = NULL;
540 		sa6_src = &sa6_any;
541 	}
542 
543 	if (ip6) {
544 		/*
545 		 * XXX: We assume that when IPV6 is non NULL,
546 		 * M and OFF are valid.
547 		 */
548 
549 		/* Check if we can safely examine src and dst ports. */
550 		if (m->m_pkthdr.len < off + sizeof(*uhp))
551 			return;
552 
553 		bzero(&uh, sizeof(uh));
554 		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
555 
556 		(void)in6_pcbnotify(pcbinfo, sa, uh.uh_dport,
557 		    (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
558 		    cmdarg, notify);
559 	} else
560 		(void)in6_pcbnotify(pcbinfo, sa, 0,
561 		    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
562 }
563 
564 void
565 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
566 {
567 
568 	return (udp6_common_ctlinput(cmd, sa, d, &V_udbinfo));
569 }
570 
571 void
572 udplite6_ctlinput(int cmd, struct sockaddr *sa, void *d)
573 {
574 
575 	return (udp6_common_ctlinput(cmd, sa, d, &V_ulitecbinfo));
576 }
577 
578 static int
579 udp6_getcred(SYSCTL_HANDLER_ARGS)
580 {
581 	struct xucred xuc;
582 	struct sockaddr_in6 addrs[2];
583 	struct inpcb *inp;
584 	int error;
585 
586 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
587 	if (error)
588 		return (error);
589 
590 	if (req->newlen != sizeof(addrs))
591 		return (EINVAL);
592 	if (req->oldlen != sizeof(struct xucred))
593 		return (EINVAL);
594 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
595 	if (error)
596 		return (error);
597 	if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
598 	    (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
599 		return (error);
600 	}
601 	inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
602 	    addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
603 	    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
604 	if (inp != NULL) {
605 		INP_RLOCK_ASSERT(inp);
606 		if (inp->inp_socket == NULL)
607 			error = ENOENT;
608 		if (error == 0)
609 			error = cr_canseesocket(req->td->td_ucred,
610 			    inp->inp_socket);
611 		if (error == 0)
612 			cru2x(inp->inp_cred, &xuc);
613 		INP_RUNLOCK(inp);
614 	} else
615 		error = ENOENT;
616 	if (error == 0)
617 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
618 	return (error);
619 }
620 
621 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
622     0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
623 
624 static int
625 udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
626     struct mbuf *control, struct thread *td)
627 {
628 	u_int32_t ulen = m->m_pkthdr.len;
629 	u_int32_t plen = sizeof(struct udphdr) + ulen;
630 	struct ip6_hdr *ip6;
631 	struct udphdr *udp6;
632 	struct in6_addr *laddr, *faddr, in6a;
633 	struct sockaddr_in6 *sin6 = NULL;
634 	struct ifnet *oifp = NULL;
635 	int cscov_partial = 0;
636 	int scope_ambiguous = 0;
637 	u_short fport;
638 	int error = 0;
639 	uint8_t nxt;
640 	uint16_t cscov = 0;
641 	struct ip6_pktopts *optp, opt;
642 	int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
643 	int flags;
644 	struct sockaddr_in6 tmp;
645 
646 	INP_WLOCK_ASSERT(inp);
647 	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
648 
649 	if (addr6) {
650 		/* addr6 has been validated in udp6_send(). */
651 		sin6 = (struct sockaddr_in6 *)addr6;
652 
653 		/* protect *sin6 from overwrites */
654 		tmp = *sin6;
655 		sin6 = &tmp;
656 
657 		/*
658 		 * Application should provide a proper zone ID or the use of
659 		 * default zone IDs should be enabled.  Unfortunately, some
660 		 * applications do not behave as it should, so we need a
661 		 * workaround.  Even if an appropriate ID is not determined,
662 		 * we'll see if we can determine the outgoing interface.  If we
663 		 * can, determine the zone ID based on the interface below.
664 		 */
665 		if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
666 			scope_ambiguous = 1;
667 		if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
668 			return (error);
669 	}
670 
671 	if (control) {
672 		if ((error = ip6_setpktopts(control, &opt,
673 		    inp->in6p_outputopts, td->td_ucred, IPPROTO_UDP)) != 0)
674 			goto release;
675 		optp = &opt;
676 	} else
677 		optp = inp->in6p_outputopts;
678 
679 	if (sin6) {
680 		faddr = &sin6->sin6_addr;
681 
682 		/*
683 		 * Since we saw no essential reason for calling in_pcbconnect,
684 		 * we get rid of such kind of logic, and call in6_selectsrc
685 		 * and in6_pcbsetport in order to fill in the local address
686 		 * and the local port.
687 		 */
688 		if (sin6->sin6_port == 0) {
689 			error = EADDRNOTAVAIL;
690 			goto release;
691 		}
692 
693 		if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
694 			/* how about ::ffff:0.0.0.0 case? */
695 			error = EISCONN;
696 			goto release;
697 		}
698 
699 		fport = sin6->sin6_port; /* allow 0 port */
700 
701 		if (IN6_IS_ADDR_V4MAPPED(faddr)) {
702 			if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
703 				/*
704 				 * I believe we should explicitly discard the
705 				 * packet when mapped addresses are disabled,
706 				 * rather than send the packet as an IPv6 one.
707 				 * If we chose the latter approach, the packet
708 				 * might be sent out on the wire based on the
709 				 * default route, the situation which we'd
710 				 * probably want to avoid.
711 				 * (20010421 jinmei@kame.net)
712 				 */
713 				error = EINVAL;
714 				goto release;
715 			}
716 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
717 			    !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
718 				/*
719 				 * when remote addr is an IPv4-mapped address,
720 				 * local addr should not be an IPv6 address,
721 				 * since you cannot determine how to map IPv6
722 				 * source address to IPv4.
723 				 */
724 				error = EINVAL;
725 				goto release;
726 			}
727 
728 			af = AF_INET;
729 		}
730 
731 		if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
732 			error = in6_selectsrc(sin6, optp, inp, NULL,
733 			    td->td_ucred, &oifp, &in6a);
734 			if (error)
735 				goto release;
736 			if (oifp && scope_ambiguous &&
737 			    (error = in6_setscope(&sin6->sin6_addr,
738 			    oifp, NULL))) {
739 				goto release;
740 			}
741 			laddr = &in6a;
742 		} else
743 			laddr = &inp->in6p_laddr;	/* XXX */
744 		if (laddr == NULL) {
745 			if (error == 0)
746 				error = EADDRNOTAVAIL;
747 			goto release;
748 		}
749 		if (inp->inp_lport == 0 &&
750 		    (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0) {
751 			/* Undo an address bind that may have occurred. */
752 			inp->in6p_laddr = in6addr_any;
753 			goto release;
754 		}
755 	} else {
756 		if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
757 			error = ENOTCONN;
758 			goto release;
759 		}
760 		if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
761 			if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
762 				/*
763 				 * XXX: this case would happen when the
764 				 * application sets the V6ONLY flag after
765 				 * connecting the foreign address.
766 				 * Such applications should be fixed,
767 				 * so we bark here.
768 				 */
769 				log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
770 				    "option was set for a connected socket\n");
771 				error = EINVAL;
772 				goto release;
773 			} else
774 				af = AF_INET;
775 		}
776 		laddr = &inp->in6p_laddr;
777 		faddr = &inp->in6p_faddr;
778 		fport = inp->inp_fport;
779 	}
780 
781 	if (af == AF_INET)
782 		hlen = sizeof(struct ip);
783 
784 	/*
785 	 * Calculate data length and get a mbuf
786 	 * for UDP and IP6 headers.
787 	 */
788 	M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT);
789 	if (m == 0) {
790 		error = ENOBUFS;
791 		goto release;
792 	}
793 
794 	/*
795 	 * Stuff checksum and output datagram.
796 	 */
797 	nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
798 	    IPPROTO_UDP : IPPROTO_UDPLITE;
799 	udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
800 	udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
801 	udp6->uh_dport = fport;
802 	if (nxt == IPPROTO_UDPLITE) {
803 		struct udpcb *up;
804 
805 		up = intoudpcb(inp);
806 		cscov = up->u_txcslen;
807 		if (cscov >= plen)
808 			cscov = 0;
809 		udp6->uh_ulen = htons(cscov);
810 		/*
811 		 * For UDP-Lite, checksum coverage length of zero means
812 		 * the entire UDPLite packet is covered by the checksum.
813 		 */
814 		cscov_partial = (cscov == 0) ? 0 : 1;
815 	} else if (plen <= 0xffff)
816 		udp6->uh_ulen = htons((u_short)plen);
817 	else
818 		udp6->uh_ulen = 0;
819 	udp6->uh_sum = 0;
820 
821 	switch (af) {
822 	case AF_INET6:
823 		ip6 = mtod(m, struct ip6_hdr *);
824 		ip6->ip6_flow	= inp->inp_flow & IPV6_FLOWINFO_MASK;
825 		ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
826 		ip6->ip6_vfc	|= IPV6_VERSION;
827 		ip6->ip6_plen	= htons((u_short)plen);
828 		ip6->ip6_nxt	= nxt;
829 		ip6->ip6_hlim	= in6_selecthlim(inp, NULL);
830 		ip6->ip6_src	= *laddr;
831 		ip6->ip6_dst	= *faddr;
832 
833 		if (cscov_partial) {
834 			if ((udp6->uh_sum = in6_cksum_partial(m, nxt,
835 			    sizeof(struct ip6_hdr), plen, cscov)) == 0)
836 				udp6->uh_sum = 0xffff;
837 		} else {
838 			udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
839 			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
840 			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
841 		}
842 
843 		/*
844 		 * XXX for now assume UDP is 2-tuple.
845 		 * Later on this may become configurable as 4-tuple;
846 		 * we should support that.
847 		 *
848 		 * XXX .. and we should likely cache this in the inpcb.
849 		 */
850 #ifdef	RSS
851 		m->m_pkthdr.flowid = rss_hash_ip6_2tuple(*faddr, *laddr);
852 		m->m_flags |= M_FLOWID;
853 		M_HASHTYPE_SET(m, M_HASHTYPE_RSS_IPV6);
854 #endif
855 		flags = 0;
856 
857 #ifdef	RSS
858 		/*
859 		 * Don't override with the inp cached flowid.
860 		 *
861 		 * Until the whole UDP path is vetted, it may actually
862 		 * be incorrect.
863 		 */
864 		flags |= IP_NODEFAULTFLOWID;
865 #endif
866 
867 		UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
868 		UDPSTAT_INC(udps_opackets);
869 		error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
870 		    NULL, inp);
871 		break;
872 	case AF_INET:
873 		error = EAFNOSUPPORT;
874 		goto release;
875 	}
876 	goto releaseopt;
877 
878 release:
879 	m_freem(m);
880 
881 releaseopt:
882 	if (control) {
883 		ip6_clearpktopts(&opt, -1);
884 		m_freem(control);
885 	}
886 	return (error);
887 }
888 
889 static void
890 udp6_abort(struct socket *so)
891 {
892 	struct inpcb *inp;
893 	struct inpcbinfo *pcbinfo;
894 
895 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
896 	inp = sotoinpcb(so);
897 	KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
898 
899 #ifdef INET
900 	if (inp->inp_vflag & INP_IPV4) {
901 		struct pr_usrreqs *pru;
902 
903 		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
904 		(*pru->pru_abort)(so);
905 		return;
906 	}
907 #endif
908 
909 	INP_WLOCK(inp);
910 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
911 		INP_HASH_WLOCK(pcbinfo);
912 		in6_pcbdisconnect(inp);
913 		inp->in6p_laddr = in6addr_any;
914 		INP_HASH_WUNLOCK(pcbinfo);
915 		soisdisconnected(so);
916 	}
917 	INP_WUNLOCK(inp);
918 }
919 
920 static int
921 udp6_attach(struct socket *so, int proto, struct thread *td)
922 {
923 	struct inpcb *inp;
924 	struct inpcbinfo *pcbinfo;
925 	int error;
926 
927 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
928 	inp = sotoinpcb(so);
929 	KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
930 
931 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
932 		error = soreserve(so, udp_sendspace, udp_recvspace);
933 		if (error)
934 			return (error);
935 	}
936 	INP_INFO_WLOCK(pcbinfo);
937 	error = in_pcballoc(so, pcbinfo);
938 	if (error) {
939 		INP_INFO_WUNLOCK(pcbinfo);
940 		return (error);
941 	}
942 	inp = (struct inpcb *)so->so_pcb;
943 	inp->inp_vflag |= INP_IPV6;
944 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
945 		inp->inp_vflag |= INP_IPV4;
946 	inp->in6p_hops = -1;	/* use kernel default */
947 	inp->in6p_cksum = -1;	/* just to be sure */
948 	/*
949 	 * XXX: ugly!!
950 	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
951 	 * because the socket may be bound to an IPv6 wildcard address,
952 	 * which may match an IPv4-mapped IPv6 address.
953 	 */
954 	inp->inp_ip_ttl = V_ip_defttl;
955 
956 	error = udp_newudpcb(inp);
957 	if (error) {
958 		in_pcbdetach(inp);
959 		in_pcbfree(inp);
960 		INP_INFO_WUNLOCK(pcbinfo);
961 		return (error);
962 	}
963 	INP_WUNLOCK(inp);
964 	INP_INFO_WUNLOCK(pcbinfo);
965 	return (0);
966 }
967 
968 static int
969 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
970 {
971 	struct inpcb *inp;
972 	struct inpcbinfo *pcbinfo;
973 	int error;
974 
975 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
976 	inp = sotoinpcb(so);
977 	KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
978 
979 	INP_WLOCK(inp);
980 	INP_HASH_WLOCK(pcbinfo);
981 	inp->inp_vflag &= ~INP_IPV4;
982 	inp->inp_vflag |= INP_IPV6;
983 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
984 		struct sockaddr_in6 *sin6_p;
985 
986 		sin6_p = (struct sockaddr_in6 *)nam;
987 
988 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
989 			inp->inp_vflag |= INP_IPV4;
990 #ifdef INET
991 		else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
992 			struct sockaddr_in sin;
993 
994 			in6_sin6_2_sin(&sin, sin6_p);
995 			inp->inp_vflag |= INP_IPV4;
996 			inp->inp_vflag &= ~INP_IPV6;
997 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
998 			    td->td_ucred);
999 			goto out;
1000 		}
1001 #endif
1002 	}
1003 
1004 	error = in6_pcbbind(inp, nam, td->td_ucred);
1005 #ifdef INET
1006 out:
1007 #endif
1008 	INP_HASH_WUNLOCK(pcbinfo);
1009 	INP_WUNLOCK(inp);
1010 	return (error);
1011 }
1012 
1013 static void
1014 udp6_close(struct socket *so)
1015 {
1016 	struct inpcb *inp;
1017 	struct inpcbinfo *pcbinfo;
1018 
1019 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1020 	inp = sotoinpcb(so);
1021 	KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
1022 
1023 #ifdef INET
1024 	if (inp->inp_vflag & INP_IPV4) {
1025 		struct pr_usrreqs *pru;
1026 
1027 		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1028 		(*pru->pru_disconnect)(so);
1029 		return;
1030 	}
1031 #endif
1032 	INP_WLOCK(inp);
1033 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1034 		INP_HASH_WLOCK(pcbinfo);
1035 		in6_pcbdisconnect(inp);
1036 		inp->in6p_laddr = in6addr_any;
1037 		INP_HASH_WUNLOCK(pcbinfo);
1038 		soisdisconnected(so);
1039 	}
1040 	INP_WUNLOCK(inp);
1041 }
1042 
1043 static int
1044 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1045 {
1046 	struct inpcb *inp;
1047 	struct inpcbinfo *pcbinfo;
1048 	struct sockaddr_in6 *sin6;
1049 	int error;
1050 
1051 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1052 	inp = sotoinpcb(so);
1053 	sin6 = (struct sockaddr_in6 *)nam;
1054 	KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
1055 
1056 	/*
1057 	 * XXXRW: Need to clarify locking of v4/v6 flags.
1058 	 */
1059 	INP_WLOCK(inp);
1060 #ifdef INET
1061 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1062 		struct sockaddr_in sin;
1063 
1064 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1065 			error = EINVAL;
1066 			goto out;
1067 		}
1068 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1069 			error = EISCONN;
1070 			goto out;
1071 		}
1072 		in6_sin6_2_sin(&sin, sin6);
1073 		inp->inp_vflag |= INP_IPV4;
1074 		inp->inp_vflag &= ~INP_IPV6;
1075 		error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
1076 		if (error != 0)
1077 			goto out;
1078 		INP_HASH_WLOCK(pcbinfo);
1079 		error = in_pcbconnect(inp, (struct sockaddr *)&sin,
1080 		    td->td_ucred);
1081 		INP_HASH_WUNLOCK(pcbinfo);
1082 		if (error == 0)
1083 			soisconnected(so);
1084 		goto out;
1085 	}
1086 #endif
1087 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1088 		error = EISCONN;
1089 		goto out;
1090 	}
1091 	inp->inp_vflag &= ~INP_IPV4;
1092 	inp->inp_vflag |= INP_IPV6;
1093 	error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1094 	if (error != 0)
1095 		goto out;
1096 	INP_HASH_WLOCK(pcbinfo);
1097 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1098 	INP_HASH_WUNLOCK(pcbinfo);
1099 	if (error == 0)
1100 		soisconnected(so);
1101 out:
1102 	INP_WUNLOCK(inp);
1103 	return (error);
1104 }
1105 
1106 static void
1107 udp6_detach(struct socket *so)
1108 {
1109 	struct inpcb *inp;
1110 	struct inpcbinfo *pcbinfo;
1111 	struct udpcb *up;
1112 
1113 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1114 	inp = sotoinpcb(so);
1115 	KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1116 
1117 	INP_INFO_WLOCK(pcbinfo);
1118 	INP_WLOCK(inp);
1119 	up = intoudpcb(inp);
1120 	KASSERT(up != NULL, ("%s: up == NULL", __func__));
1121 	in_pcbdetach(inp);
1122 	in_pcbfree(inp);
1123 	INP_INFO_WUNLOCK(pcbinfo);
1124 	udp_discardcb(up);
1125 }
1126 
1127 static int
1128 udp6_disconnect(struct socket *so)
1129 {
1130 	struct inpcb *inp;
1131 	struct inpcbinfo *pcbinfo;
1132 	int error;
1133 
1134 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1135 	inp = sotoinpcb(so);
1136 	KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1137 
1138 #ifdef INET
1139 	if (inp->inp_vflag & INP_IPV4) {
1140 		struct pr_usrreqs *pru;
1141 
1142 		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1143 		(void)(*pru->pru_disconnect)(so);
1144 		return (0);
1145 	}
1146 #endif
1147 
1148 	INP_WLOCK(inp);
1149 
1150 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1151 		error = ENOTCONN;
1152 		goto out;
1153 	}
1154 
1155 	INP_HASH_WLOCK(pcbinfo);
1156 	in6_pcbdisconnect(inp);
1157 	inp->in6p_laddr = in6addr_any;
1158 	INP_HASH_WUNLOCK(pcbinfo);
1159 	SOCK_LOCK(so);
1160 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1161 	SOCK_UNLOCK(so);
1162 out:
1163 	INP_WUNLOCK(inp);
1164 	return (0);
1165 }
1166 
1167 static int
1168 udp6_send(struct socket *so, int flags, struct mbuf *m,
1169     struct sockaddr *addr, struct mbuf *control, struct thread *td)
1170 {
1171 	struct inpcb *inp;
1172 	struct inpcbinfo *pcbinfo;
1173 	int error = 0;
1174 
1175 	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1176 	inp = sotoinpcb(so);
1177 	KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
1178 
1179 	INP_WLOCK(inp);
1180 	if (addr) {
1181 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1182 			error = EINVAL;
1183 			goto bad;
1184 		}
1185 		if (addr->sa_family != AF_INET6) {
1186 			error = EAFNOSUPPORT;
1187 			goto bad;
1188 		}
1189 	}
1190 
1191 #ifdef INET
1192 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1193 		int hasv4addr;
1194 		struct sockaddr_in6 *sin6 = 0;
1195 
1196 		if (addr == 0)
1197 			hasv4addr = (inp->inp_vflag & INP_IPV4);
1198 		else {
1199 			sin6 = (struct sockaddr_in6 *)addr;
1200 			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
1201 			    ? 1 : 0;
1202 		}
1203 		if (hasv4addr) {
1204 			struct pr_usrreqs *pru;
1205 
1206 			/*
1207 			 * XXXRW: We release UDP-layer locks before calling
1208 			 * udp_send() in order to avoid recursion.  However,
1209 			 * this does mean there is a short window where inp's
1210 			 * fields are unstable.  Could this lead to a
1211 			 * potential race in which the factors causing us to
1212 			 * select the UDPv4 output routine are invalidated?
1213 			 */
1214 			INP_WUNLOCK(inp);
1215 			if (sin6)
1216 				in6_sin6_2_sin_in_sock(addr);
1217 			pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1218 			/* addr will just be freed in sendit(). */
1219 			return ((*pru->pru_send)(so, flags, m, addr, control,
1220 			    td));
1221 		}
1222 	}
1223 #endif
1224 #ifdef MAC
1225 	mac_inpcb_create_mbuf(inp, m);
1226 #endif
1227 	INP_HASH_WLOCK(pcbinfo);
1228 	error = udp6_output(inp, m, addr, control, td);
1229 	INP_HASH_WUNLOCK(pcbinfo);
1230 #ifdef INET
1231 #endif
1232 	INP_WUNLOCK(inp);
1233 	return (error);
1234 
1235 bad:
1236 	INP_WUNLOCK(inp);
1237 	m_freem(m);
1238 	return (error);
1239 }
1240 
1241 struct pr_usrreqs udp6_usrreqs = {
1242 	.pru_abort =		udp6_abort,
1243 	.pru_attach =		udp6_attach,
1244 	.pru_bind =		udp6_bind,
1245 	.pru_connect =		udp6_connect,
1246 	.pru_control =		in6_control,
1247 	.pru_detach =		udp6_detach,
1248 	.pru_disconnect =	udp6_disconnect,
1249 	.pru_peeraddr =		in6_mapped_peeraddr,
1250 	.pru_send =		udp6_send,
1251 	.pru_shutdown =		udp_shutdown,
1252 	.pru_sockaddr =		in6_mapped_sockaddr,
1253 	.pru_soreceive =	soreceive_dgram,
1254 	.pru_sosend =		sosend_dgram,
1255 	.pru_sosetlabel =	in_pcbsosetlabel,
1256 	.pru_close =		udp6_close
1257 };
1258