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