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