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