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