xref: /freebsd/sys/netinet6/udp6_usrreq.c (revision 38a52bd3b5cac3da6f7f6eef3dd050e6aa08ebb3)
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/ip6_var.h>
118 #include <netinet6/in6_fib.h>
119 #include <netinet6/in6_pcb.h>
120 #include <netinet6/in6_rss.h>
121 #include <netinet6/udp6_var.h>
122 #include <netinet6/scope6_var.h>
123 
124 #include <netipsec/ipsec_support.h>
125 
126 #include <security/mac/mac_framework.h>
127 
128 VNET_DEFINE(int, zero_checksum_port) = 0;
129 #define	V_zero_checksum_port	VNET(zero_checksum_port)
130 SYSCTL_INT(_net_inet6_udp6, OID_AUTO, rfc6935_port, CTLFLAG_VNET | CTLFLAG_RW,
131     &VNET_NAME(zero_checksum_port), 0,
132     "Zero UDP checksum allowed for traffic to/from this port.");
133 
134 
135 /* netinet/udp_usrreqs.c */
136 pr_abort_t	udp_abort;
137 pr_disconnect_t	udp_disconnect;
138 pr_send_t	udp_send;
139 
140 /*
141  * UDP protocol implementation.
142  * Per RFC 768, August, 1980.
143  */
144 
145 static void		udp6_detach(struct socket *so);
146 
147 static int
148 udp6_append(struct inpcb *inp, struct mbuf *n, int off,
149     struct sockaddr_in6 *fromsa)
150 {
151 	struct socket *so;
152 	struct mbuf *opts = NULL, *tmp_opts;
153 	struct udpcb *up;
154 	bool filtered;
155 
156 	INP_LOCK_ASSERT(inp);
157 
158 	/*
159 	 * Engage the tunneling protocol.
160 	 */
161 	up = intoudpcb(inp);
162 	if (up->u_tun_func != NULL) {
163 		in_pcbref(inp);
164 		INP_RUNLOCK(inp);
165 		filtered = (*up->u_tun_func)(n, off, inp,
166 		    (struct sockaddr *)&fromsa[0], up->u_tun_ctx);
167 		INP_RLOCK(inp);
168 		if (filtered)
169 			return (in_pcbrele_rlocked(inp));
170 	}
171 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
172 	/* Check AH/ESP integrity. */
173 	if (IPSEC_ENABLED(ipv6)) {
174 		if (IPSEC_CHECK_POLICY(ipv6, n, inp) != 0) {
175 			m_freem(n);
176 			return (0);
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 + sizeof(struct udphdr));
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 				INP_RUNLOCK(inp);
316 				break;
317 			} else
318 				appends++;
319 		}
320 		/*
321 		 * Don't look for additional matches if this one does
322 		 * not have either the SO_REUSEPORT or SO_REUSEADDR
323 		 * socket options set.  This heuristic avoids
324 		 * searching through all pcbs in the common case of a
325 		 * non-shared port.  It assumes that an application
326 		 * will never clear these options after setting them.
327 		 */
328 		if ((inp->inp_socket->so_options &
329 		     (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0) {
330 			INP_RUNLOCK(inp);
331 			break;
332 		}
333 	}
334 	m_freem(m);
335 
336 	if (appends == 0) {
337 		/*
338 		 * No matching pcb found; discard datagram.  (No need
339 		 * to send an ICMP Port Unreachable for a broadcast
340 		 * or multicast datgram.)
341 		 */
342 		UDPSTAT_INC(udps_noport);
343 		UDPSTAT_INC(udps_noportmcast);
344 	}
345 
346 	return (IPPROTO_DONE);
347 }
348 
349 int
350 udp6_input(struct mbuf **mp, int *offp, int proto)
351 {
352 	struct mbuf *m = *mp;
353 	struct ip6_hdr *ip6;
354 	struct udphdr *uh;
355 	struct inpcb *inp;
356 	struct inpcbinfo *pcbinfo;
357 	struct udpcb *up;
358 	int off = *offp;
359 	int cscov_partial;
360 	int plen, ulen;
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 
459 	/*
460 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
461 	 */
462 	if ((m->m_flags & M_IP6_NEXTHOP) &&
463 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
464 		struct sockaddr_in6 *next_hop6;
465 
466 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
467 
468 		/*
469 		 * Transparently forwarded. Pretend to be the destination.
470 		 * Already got one like this?
471 		 */
472 		inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
473 		    uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
474 		    INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif, m);
475 		if (!inp) {
476 			/*
477 			 * It's new.  Try to find the ambushing socket.
478 			 * Because we've rewritten the destination address,
479 			 * any hardware-generated hash is ignored.
480 			 */
481 			inp = in6_pcblookup(pcbinfo, &ip6->ip6_src,
482 			    uh->uh_sport, &next_hop6->sin6_addr,
483 			    next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
484 			    uh->uh_dport, INPLOOKUP_WILDCARD |
485 			    INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif);
486 		}
487 		/* Remove the tag from the packet. We don't need it anymore. */
488 		m_tag_delete(m, fwd_tag);
489 		m->m_flags &= ~M_IP6_NEXTHOP;
490 	} else
491 		inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
492 		    uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
493 		    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
494 		    m->m_pkthdr.rcvif, m);
495 	if (inp == NULL) {
496 		if (V_udp_log_in_vain) {
497 			char ip6bufs[INET6_ADDRSTRLEN];
498 			char ip6bufd[INET6_ADDRSTRLEN];
499 
500 			log(LOG_INFO,
501 			    "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
502 			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
503 			    ntohs(uh->uh_dport),
504 			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
505 			    ntohs(uh->uh_sport));
506 		}
507 		if (nxt == IPPROTO_UDPLITE)
508 			UDPLITE_PROBE(receive, NULL, NULL, ip6, NULL, uh);
509 		else
510 			UDP_PROBE(receive, NULL, NULL, ip6, NULL, uh);
511 		UDPSTAT_INC(udps_noport);
512 		if (m->m_flags & M_MCAST) {
513 			printf("UDP6: M_MCAST is set in a unicast packet.\n");
514 			UDPSTAT_INC(udps_noportmcast);
515 			goto badunlocked;
516 		}
517 		if (V_udp_blackhole && (V_udp_blackhole_local ||
518 		    !in6_localaddr(&ip6->ip6_src)))
519 			goto badunlocked;
520 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
521 		*mp = NULL;
522 		return (IPPROTO_DONE);
523 	}
524 	INP_RLOCK_ASSERT(inp);
525 	up = intoudpcb(inp);
526 	if (cscov_partial) {
527 		if (up->u_rxcslen == 0 || up->u_rxcslen > ulen) {
528 			INP_RUNLOCK(inp);
529 			m_freem(m);
530 			*mp = NULL;
531 			return (IPPROTO_DONE);
532 		}
533 	}
534 	if (nxt == IPPROTO_UDPLITE)
535 		UDPLITE_PROBE(receive, NULL, inp, ip6, inp, uh);
536 	else
537 		UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
538 	if (udp6_append(inp, m, off, fromsa) == 0)
539 		INP_RUNLOCK(inp);
540 	*mp = NULL;
541 	return (IPPROTO_DONE);
542 
543 badunlocked:
544 	m_freem(m);
545 	*mp = NULL;
546 	return (IPPROTO_DONE);
547 }
548 
549 static void
550 udp6_common_ctlinput(struct ip6ctlparam *ip6cp, struct inpcbinfo *pcbinfo)
551 {
552 	struct udphdr uh;
553 	struct ip6_hdr *ip6;
554 	struct mbuf *m;
555 	struct inpcb *inp;
556 	int errno, off = 0;
557 	struct udp_portonly {
558 		u_int16_t uh_sport;
559 		u_int16_t uh_dport;
560 	} *uhp;
561 
562 	if ((errno = icmp6_errmap(ip6cp->ip6c_icmp6)) == 0)
563 		return;
564 
565 	m = ip6cp->ip6c_m;
566 	ip6 = ip6cp->ip6c_ip6;
567 	off = ip6cp->ip6c_off;
568 
569 	/* Check if we can safely examine src and dst ports. */
570 	if (m->m_pkthdr.len < off + sizeof(*uhp))
571 		return;
572 
573 	bzero(&uh, sizeof(uh));
574 	m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
575 
576 	/* Check to see if its tunneled */
577 	inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_dst, uh.uh_dport,
578 	    &ip6->ip6_src, uh.uh_sport, INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
579 	    m->m_pkthdr.rcvif, m);
580 	if (inp != NULL) {
581 		struct udpcb *up;
582 		udp_tun_icmp_t *func;
583 
584 		up = intoudpcb(inp);
585 		func = up->u_icmp_func;
586 		INP_RUNLOCK(inp);
587 		if (func != NULL)
588 			func(ip6cp);
589 	}
590 	in6_pcbnotify(pcbinfo, ip6cp->ip6c_finaldst, uh.uh_dport,
591 	    ip6cp->ip6c_src, uh.uh_sport, errno, ip6cp->ip6c_cmdarg,
592 	    udp_notify);
593 }
594 
595 static void
596 udp6_ctlinput(struct ip6ctlparam *ctl)
597 {
598 
599 	return (udp6_common_ctlinput(ctl, &V_udbinfo));
600 }
601 
602 static void
603 udplite6_ctlinput(struct ip6ctlparam *ctl)
604 {
605 
606 	return (udp6_common_ctlinput(ctl, &V_ulitecbinfo));
607 }
608 
609 static int
610 udp6_getcred(SYSCTL_HANDLER_ARGS)
611 {
612 	struct xucred xuc;
613 	struct sockaddr_in6 addrs[2];
614 	struct epoch_tracker et;
615 	struct inpcb *inp;
616 	int error;
617 
618 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
619 	if (error)
620 		return (error);
621 
622 	if (req->newlen != sizeof(addrs))
623 		return (EINVAL);
624 	if (req->oldlen != sizeof(struct xucred))
625 		return (EINVAL);
626 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
627 	if (error)
628 		return (error);
629 	if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
630 	    (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
631 		return (error);
632 	}
633 	NET_EPOCH_ENTER(et);
634 	inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
635 	    addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
636 	    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
637 	NET_EPOCH_EXIT(et);
638 	if (inp != NULL) {
639 		INP_RLOCK_ASSERT(inp);
640 		if (inp->inp_socket == NULL)
641 			error = ENOENT;
642 		if (error == 0)
643 			error = cr_canseesocket(req->td->td_ucred,
644 			    inp->inp_socket);
645 		if (error == 0)
646 			cru2x(inp->inp_cred, &xuc);
647 		INP_RUNLOCK(inp);
648 	} else
649 		error = ENOENT;
650 	if (error == 0)
651 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
652 	return (error);
653 }
654 
655 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred,
656     CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_MPSAFE,
657     0, 0, udp6_getcred, "S,xucred",
658     "Get the xucred of a UDP6 connection");
659 
660 static int
661 udp6_output(struct socket *so, int flags_arg, struct mbuf *m,
662     struct sockaddr *addr6, struct mbuf *control, struct thread *td)
663 {
664 	struct inpcb *inp;
665 	struct ip6_hdr *ip6;
666 	struct udphdr *udp6;
667 	struct in6_addr *laddr, *faddr, in6a;
668 	struct ip6_pktopts *optp, opt;
669 	struct sockaddr_in6 *sin6, tmp;
670 	struct epoch_tracker et;
671 	int cscov_partial, error, flags, hlen, scope_ambiguous;
672 	u_int32_t ulen, plen;
673 	uint16_t cscov;
674 	u_short fport;
675 	uint8_t nxt;
676 
677 	/* addr6 has been validated in udp6_send(). */
678 	sin6 = (struct sockaddr_in6 *)addr6;
679 
680 	/*
681 	 * In contrast to IPv4 we do not validate the max. packet length
682 	 * here due to IPv6 Jumbograms (RFC2675).
683 	 */
684 
685 	scope_ambiguous = 0;
686 	if (sin6) {
687 		/* Protect *addr6 from overwrites. */
688 		tmp = *sin6;
689 		sin6 = &tmp;
690 
691 		/*
692 		 * Application should provide a proper zone ID or the use of
693 		 * default zone IDs should be enabled.  Unfortunately, some
694 		 * applications do not behave as it should, so we need a
695 		 * workaround.  Even if an appropriate ID is not determined,
696 		 * we'll see if we can determine the outgoing interface.  If we
697 		 * can, determine the zone ID based on the interface below.
698 		 */
699 		if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
700 			scope_ambiguous = 1;
701 		if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) {
702 			if (control)
703 				m_freem(control);
704 			m_freem(m);
705 			return (error);
706 		}
707 	}
708 
709 	inp = sotoinpcb(so);
710 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
711 	/*
712 	 * In the following cases we want a write lock on the inp for either
713 	 * local operations or for possible route cache updates in the IPv6
714 	 * output path:
715 	 * - on connected sockets (sin6 is NULL) for route cache updates,
716 	 * - when we are not bound to an address and source port (it is
717 	 *   in6_pcbsetport() which will require the write lock).
718 	 *
719 	 * We check the inp fields before actually locking the inp, so
720 	 * here exists a race, and we may WLOCK the inp and end with already
721 	 * bound one by other thread. This is fine.
722 	 */
723 	if (sin6 == NULL || (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
724 	    inp->inp_lport == 0))
725 		INP_WLOCK(inp);
726 	else
727 		INP_RLOCK(inp);
728 
729 	nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
730 	    IPPROTO_UDP : IPPROTO_UDPLITE;
731 
732 #ifdef INET
733 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
734 		int hasv4addr;
735 
736 		if (sin6 == NULL)
737 			hasv4addr = (inp->inp_vflag & INP_IPV4);
738 		else
739 			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
740 			    ? 1 : 0;
741 		if (hasv4addr) {
742 			/*
743 			 * XXXRW: We release UDP-layer locks before calling
744 			 * udp_send() in order to avoid recursion.  However,
745 			 * this does mean there is a short window where inp's
746 			 * fields are unstable.  Could this lead to a
747 			 * potential race in which the factors causing us to
748 			 * select the UDPv4 output routine are invalidated?
749 			 */
750 			INP_UNLOCK(inp);
751 			if (sin6)
752 				in6_sin6_2_sin_in_sock((struct sockaddr *)sin6);
753 			/* addr will just be freed in sendit(). */
754 			return (udp_send(so, flags_arg | PRUS_IPV6, m,
755 			    (struct sockaddr *)sin6, control, td));
756 		}
757 	} else
758 #endif
759 	if (sin6 && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
760 		/*
761 		 * Given this is either an IPv6-only socket or no INET is
762 		 * supported we will fail the send if the given destination
763 		 * address is a v4mapped address.
764 		 *
765 		 * XXXGL: do we leak m and control?
766 		 */
767 		INP_UNLOCK(inp);
768 		return (EINVAL);
769 	}
770 
771 	NET_EPOCH_ENTER(et);
772 	if (control) {
773 		if ((error = ip6_setpktopts(control, &opt,
774 		    inp->in6p_outputopts, td->td_ucred, nxt)) != 0) {
775 			goto release;
776 		}
777 		optp = &opt;
778 	} else
779 		optp = inp->in6p_outputopts;
780 
781 	if (sin6) {
782 		/*
783 		 * Since we saw no essential reason for calling in_pcbconnect,
784 		 * we get rid of such kind of logic, and call in6_selectsrc
785 		 * and in6_pcbsetport in order to fill in the local address
786 		 * and the local port.
787 		 */
788 		if (sin6->sin6_port == 0) {
789 			error = EADDRNOTAVAIL;
790 			goto release;
791 		}
792 
793 		if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
794 			/* how about ::ffff:0.0.0.0 case? */
795 			error = EISCONN;
796 			goto release;
797 		}
798 
799 		/*
800 		 * Given we handle the v4mapped case in the INET block above
801 		 * assert here that it must not happen anymore.
802 		 */
803 		KASSERT(!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr),
804 		    ("%s: sin6(%p)->sin6_addr is v4mapped which we "
805 		    "should have handled.", __func__, sin6));
806 
807 		/* This only requires read-locking. */
808 		error = in6_selectsrc_socket(sin6, optp, inp,
809 		    td->td_ucred, scope_ambiguous, &in6a, NULL);
810 		if (error)
811 			goto release;
812 		laddr = &in6a;
813 
814 		if (inp->inp_lport == 0) {
815 			struct inpcbinfo *pcbinfo;
816 
817 			INP_WLOCK_ASSERT(inp);
818 
819 			pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
820 			INP_HASH_WLOCK(pcbinfo);
821 			error = in6_pcbsetport(laddr, inp, td->td_ucred);
822 			INP_HASH_WUNLOCK(pcbinfo);
823 			if (error != 0) {
824 				/* Undo an address bind that may have occurred. */
825 				inp->in6p_laddr = in6addr_any;
826 				goto release;
827 			}
828 		}
829 		faddr = &sin6->sin6_addr;
830 		fport = sin6->sin6_port; /* allow 0 port */
831 
832 	} else {
833 		if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
834 			error = ENOTCONN;
835 			goto release;
836 		}
837 		laddr = &inp->in6p_laddr;
838 		faddr = &inp->in6p_faddr;
839 		fport = inp->inp_fport;
840 	}
841 
842 	ulen = m->m_pkthdr.len;
843 	plen = sizeof(struct udphdr) + ulen;
844 	hlen = sizeof(struct ip6_hdr);
845 
846 	/*
847 	 * Calculate data length and get a mbuf
848 	 * for UDP and IP6 headers.
849 	 */
850 	M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT);
851 	if (m == NULL) {
852 		error = ENOBUFS;
853 		goto release;
854 	}
855 
856 	/*
857 	 * Stuff checksum and output datagram.
858 	 */
859 	cscov = cscov_partial = 0;
860 	udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
861 	udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
862 	udp6->uh_dport = fport;
863 	if (nxt == IPPROTO_UDPLITE) {
864 		struct udpcb *up;
865 
866 		up = intoudpcb(inp);
867 		cscov = up->u_txcslen;
868 		if (cscov >= plen)
869 			cscov = 0;
870 		udp6->uh_ulen = htons(cscov);
871 		/*
872 		 * For UDP-Lite, checksum coverage length of zero means
873 		 * the entire UDPLite packet is covered by the checksum.
874 		 */
875 		cscov_partial = (cscov == 0) ? 0 : 1;
876 	} else if (plen <= 0xffff)
877 		udp6->uh_ulen = htons((u_short)plen);
878 	else
879 		udp6->uh_ulen = 0;
880 	udp6->uh_sum = 0;
881 
882 	ip6 = mtod(m, struct ip6_hdr *);
883 	ip6->ip6_flow	= inp->inp_flow & IPV6_FLOWINFO_MASK;
884 	ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
885 	ip6->ip6_vfc	|= IPV6_VERSION;
886 	ip6->ip6_plen	= htons((u_short)plen);
887 	ip6->ip6_nxt	= nxt;
888 	ip6->ip6_hlim	= in6_selecthlim(inp, NULL);
889 	ip6->ip6_src	= *laddr;
890 	ip6->ip6_dst	= *faddr;
891 
892 #ifdef MAC
893 	mac_inpcb_create_mbuf(inp, m);
894 #endif
895 
896 	if (cscov_partial) {
897 		if ((udp6->uh_sum = in6_cksum_partial(m, nxt,
898 		    sizeof(struct ip6_hdr), plen, cscov)) == 0)
899 			udp6->uh_sum = 0xffff;
900 	} else {
901 		udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
902 		m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
903 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
904 	}
905 
906 	flags = 0;
907 #if defined(ROUTE_MPATH) || defined(RSS)
908 	if (CALC_FLOWID_OUTBOUND_SENDTO) {
909 		uint32_t hash_type, hash_val;
910 		uint8_t pr;
911 
912 		pr = inp->inp_socket->so_proto->pr_protocol;
913 
914 		hash_val = fib6_calc_packet_hash(laddr, faddr,
915 		    inp->inp_lport, fport, pr, &hash_type);
916 		m->m_pkthdr.flowid = hash_val;
917 		M_HASHTYPE_SET(m, hash_type);
918 	}
919 	/* do not use inp flowid */
920 	flags |= IP_NODEFAULTFLOWID;
921 #endif
922 
923 	UDPSTAT_INC(udps_opackets);
924 	if (nxt == IPPROTO_UDPLITE)
925 		UDPLITE_PROBE(send, NULL, inp, ip6, inp, udp6);
926 	else
927 		UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
928 	error = ip6_output(m, optp,
929 	    INP_WLOCKED(inp) ? &inp->inp_route6 : NULL, flags,
930 	    inp->in6p_moptions, NULL, inp);
931 	INP_UNLOCK(inp);
932 	NET_EPOCH_EXIT(et);
933 
934 	if (control) {
935 		ip6_clearpktopts(&opt, -1);
936 		m_freem(control);
937 	}
938 	return (error);
939 
940 release:
941 	INP_UNLOCK(inp);
942 	NET_EPOCH_EXIT(et);
943 	if (control) {
944 		ip6_clearpktopts(&opt, -1);
945 		m_freem(control);
946 	}
947 	m_freem(m);
948 
949 	return (error);
950 }
951 
952 static void
953 udp6_abort(struct socket *so)
954 {
955 	struct inpcb *inp;
956 	struct inpcbinfo *pcbinfo;
957 
958 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
959 	inp = sotoinpcb(so);
960 	KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
961 
962 	INP_WLOCK(inp);
963 #ifdef INET
964 	if (inp->inp_vflag & INP_IPV4) {
965 		INP_WUNLOCK(inp);
966 		udp_abort(so);
967 		return;
968 	}
969 #endif
970 
971 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
972 		INP_HASH_WLOCK(pcbinfo);
973 		in6_pcbdisconnect(inp);
974 		inp->in6p_laddr = in6addr_any;
975 		INP_HASH_WUNLOCK(pcbinfo);
976 		soisdisconnected(so);
977 	}
978 	INP_WUNLOCK(inp);
979 }
980 
981 static int
982 udp6_attach(struct socket *so, int proto, struct thread *td)
983 {
984 	struct inpcb *inp;
985 	struct inpcbinfo *pcbinfo;
986 	int error;
987 
988 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
989 	inp = sotoinpcb(so);
990 	KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
991 
992 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
993 		error = soreserve(so, udp_sendspace, udp_recvspace);
994 		if (error)
995 			return (error);
996 	}
997 	error = in_pcballoc(so, pcbinfo);
998 	if (error)
999 		return (error);
1000 	inp = (struct inpcb *)so->so_pcb;
1001 	inp->in6p_cksum = -1;	/* just to be sure */
1002 	/*
1003 	 * XXX: ugly!!
1004 	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
1005 	 * because the socket may be bound to an IPv6 wildcard address,
1006 	 * which may match an IPv4-mapped IPv6 address.
1007 	 */
1008 	inp->inp_ip_ttl = V_ip_defttl;
1009 
1010 	error = udp_newudpcb(inp);
1011 	if (error) {
1012 		in_pcbdetach(inp);
1013 		in_pcbfree(inp);
1014 		return (error);
1015 	}
1016 	INP_WUNLOCK(inp);
1017 	return (0);
1018 }
1019 
1020 static int
1021 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1022 {
1023 	struct inpcb *inp;
1024 	struct inpcbinfo *pcbinfo;
1025 	int error;
1026 	u_char vflagsav;
1027 
1028 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1029 	inp = sotoinpcb(so);
1030 	KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
1031 
1032 	if (nam->sa_family != AF_INET6)
1033 		return (EAFNOSUPPORT);
1034 	if (nam->sa_len != sizeof(struct sockaddr_in6))
1035 		return (EINVAL);
1036 
1037 	INP_WLOCK(inp);
1038 	INP_HASH_WLOCK(pcbinfo);
1039 	vflagsav = inp->inp_vflag;
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 	if (error != 0)
1068 		inp->inp_vflag = vflagsav;
1069 	INP_HASH_WUNLOCK(pcbinfo);
1070 	INP_WUNLOCK(inp);
1071 	return (error);
1072 }
1073 
1074 static void
1075 udp6_close(struct socket *so)
1076 {
1077 	struct inpcb *inp;
1078 	struct inpcbinfo *pcbinfo;
1079 
1080 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1081 	inp = sotoinpcb(so);
1082 	KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
1083 
1084 	INP_WLOCK(inp);
1085 #ifdef INET
1086 	if (inp->inp_vflag & INP_IPV4) {
1087 		INP_WUNLOCK(inp);
1088 		(void)udp_disconnect(so);
1089 		return;
1090 	}
1091 #endif
1092 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1093 		INP_HASH_WLOCK(pcbinfo);
1094 		in6_pcbdisconnect(inp);
1095 		inp->in6p_laddr = in6addr_any;
1096 		INP_HASH_WUNLOCK(pcbinfo);
1097 		soisdisconnected(so);
1098 	}
1099 	INP_WUNLOCK(inp);
1100 }
1101 
1102 static int
1103 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1104 {
1105 #ifdef INET
1106 	struct epoch_tracker et;
1107 #endif
1108 	struct inpcb *inp;
1109 	struct inpcbinfo *pcbinfo;
1110 	struct sockaddr_in6 *sin6;
1111 	int error;
1112 	u_char vflagsav;
1113 
1114 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1115 	inp = sotoinpcb(so);
1116 	KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
1117 
1118 	sin6 = (struct sockaddr_in6 *)nam;
1119 	if (sin6->sin6_family != AF_INET6)
1120 		return (EAFNOSUPPORT);
1121 	if (sin6->sin6_len != sizeof(*sin6))
1122 		return (EINVAL);
1123 
1124 	/*
1125 	 * XXXRW: Need to clarify locking of v4/v6 flags.
1126 	 */
1127 	INP_WLOCK(inp);
1128 #ifdef INET
1129 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1130 		struct sockaddr_in sin;
1131 
1132 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1133 			error = EINVAL;
1134 			goto out;
1135 		}
1136 		if ((inp->inp_vflag & INP_IPV4) == 0) {
1137 			error = EAFNOSUPPORT;
1138 			goto out;
1139 		}
1140 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1141 			error = EISCONN;
1142 			goto out;
1143 		}
1144 		in6_sin6_2_sin(&sin, sin6);
1145 		error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
1146 		if (error != 0)
1147 			goto out;
1148 		vflagsav = inp->inp_vflag;
1149 		inp->inp_vflag |= INP_IPV4;
1150 		inp->inp_vflag &= ~INP_IPV6;
1151 		NET_EPOCH_ENTER(et);
1152 		INP_HASH_WLOCK(pcbinfo);
1153 		error = in_pcbconnect(inp, (struct sockaddr *)&sin,
1154 		    td->td_ucred, true);
1155 		INP_HASH_WUNLOCK(pcbinfo);
1156 		NET_EPOCH_EXIT(et);
1157 		/*
1158 		 * If connect succeeds, mark socket as connected. If
1159 		 * connect fails and socket is unbound, reset inp_vflag
1160 		 * field.
1161 		 */
1162 		if (error == 0)
1163 			soisconnected(so);
1164 		else if (inp->inp_laddr.s_addr == INADDR_ANY &&
1165 		    inp->inp_lport == 0)
1166 			inp->inp_vflag = vflagsav;
1167 		goto out;
1168 	} else {
1169 		if ((inp->inp_vflag & INP_IPV6) == 0) {
1170 			error = EAFNOSUPPORT;
1171 			goto out;
1172 		}
1173 	}
1174 #endif
1175 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1176 		error = EISCONN;
1177 		goto out;
1178 	}
1179 	error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1180 	if (error != 0)
1181 		goto out;
1182 	vflagsav = inp->inp_vflag;
1183 	inp->inp_vflag &= ~INP_IPV4;
1184 	inp->inp_vflag |= INP_IPV6;
1185 	INP_HASH_WLOCK(pcbinfo);
1186 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1187 	INP_HASH_WUNLOCK(pcbinfo);
1188 	/*
1189 	 * If connect succeeds, mark socket as connected. If
1190 	 * connect fails and socket is unbound, reset inp_vflag
1191 	 * field.
1192 	 */
1193 	if (error == 0)
1194 		soisconnected(so);
1195 	else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
1196 	    inp->inp_lport == 0)
1197 		inp->inp_vflag = vflagsav;
1198 out:
1199 	INP_WUNLOCK(inp);
1200 	return (error);
1201 }
1202 
1203 static void
1204 udp6_detach(struct socket *so)
1205 {
1206 	struct inpcb *inp;
1207 	struct udpcb *up;
1208 
1209 	inp = sotoinpcb(so);
1210 	KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1211 
1212 	INP_WLOCK(inp);
1213 	up = intoudpcb(inp);
1214 	KASSERT(up != NULL, ("%s: up == NULL", __func__));
1215 	in_pcbdetach(inp);
1216 	in_pcbfree(inp);
1217 	udp_discardcb(up);
1218 }
1219 
1220 static int
1221 udp6_disconnect(struct socket *so)
1222 {
1223 	struct inpcb *inp;
1224 	struct inpcbinfo *pcbinfo;
1225 
1226 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1227 	inp = sotoinpcb(so);
1228 	KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1229 
1230 	INP_WLOCK(inp);
1231 #ifdef INET
1232 	if (inp->inp_vflag & INP_IPV4) {
1233 		INP_WUNLOCK(inp);
1234 		(void)udp_disconnect(so);
1235 		return (0);
1236 	}
1237 #endif
1238 
1239 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1240 		INP_WUNLOCK(inp);
1241 		return (ENOTCONN);
1242 	}
1243 
1244 	INP_HASH_WLOCK(pcbinfo);
1245 	in6_pcbdisconnect(inp);
1246 	inp->in6p_laddr = in6addr_any;
1247 	INP_HASH_WUNLOCK(pcbinfo);
1248 	SOCK_LOCK(so);
1249 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1250 	SOCK_UNLOCK(so);
1251 	INP_WUNLOCK(inp);
1252 	return (0);
1253 }
1254 
1255 static int
1256 udp6_send(struct socket *so, int flags, struct mbuf *m,
1257     struct sockaddr *addr, struct mbuf *control, struct thread *td)
1258 {
1259 	int error;
1260 
1261 	if (addr) {
1262 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1263 			error = EINVAL;
1264 			goto bad;
1265 		}
1266 		if (addr->sa_family != AF_INET6) {
1267 			error = EAFNOSUPPORT;
1268 			goto bad;
1269 		}
1270 	}
1271 
1272 	return (udp6_output(so, flags, m, addr, control, td));
1273 
1274 bad:
1275 	if (control)
1276 		m_freem(control);
1277 	m_freem(m);
1278 	return (error);
1279 }
1280 
1281 #define	UDP6_PROTOSW							\
1282 	.pr_type =		SOCK_DGRAM,				\
1283 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_CAPATTACH,		\
1284 	.pr_ctloutput =		ip6_ctloutput,				\
1285 	.pr_abort =		udp6_abort,				\
1286 	.pr_attach =		udp6_attach,				\
1287 	.pr_bind =		udp6_bind,				\
1288 	.pr_connect =		udp6_connect,				\
1289 	.pr_control =		in6_control,				\
1290 	.pr_detach =		udp6_detach,				\
1291 	.pr_disconnect =	udp6_disconnect,			\
1292 	.pr_peeraddr =		in6_mapped_peeraddr,			\
1293 	.pr_send =		udp6_send,				\
1294 	.pr_shutdown =		udp_shutdown,				\
1295 	.pr_sockaddr =		in6_mapped_sockaddr,			\
1296 	.pr_soreceive =		soreceive_dgram,			\
1297 	.pr_sosend =		sosend_dgram,				\
1298 	.pr_sosetlabel =	in_pcbsosetlabel,			\
1299 	.pr_close =		udp6_close
1300 
1301 struct protosw udp6_protosw = {
1302 	.pr_protocol =		IPPROTO_UDP,
1303 	UDP6_PROTOSW
1304 };
1305 
1306 struct protosw udplite6_protosw = {
1307 	.pr_protocol =		IPPROTO_UDPLITE,
1308 	UDP6_PROTOSW
1309 };
1310 
1311 static void
1312 udp6_init(void *arg __unused)
1313 {
1314 
1315 	IP6PROTO_REGISTER(IPPROTO_UDP, udp6_input, udp6_ctlinput);
1316 	IP6PROTO_REGISTER(IPPROTO_UDPLITE, udp6_input, udplite6_ctlinput);
1317 }
1318 SYSINIT(udp6_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, udp6_init, NULL);
1319