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