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