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