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