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