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