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