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