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