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