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