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