udp6_usrreq.c revision 196019
1129198Scognet/*-
2129198Scognet * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3129198Scognet * All rights reserved.
4129198Scognet *
5129198Scognet * Redistribution and use in source and binary forms, with or without
6129198Scognet * modification, are permitted provided that the following conditions
7129198Scognet * are met:
8129198Scognet * 1. Redistributions of source code must retain the above copyright
9129198Scognet *    notice, this list of conditions and the following disclaimer.
10129198Scognet * 2. Redistributions in binary form must reproduce the above copyright
11129198Scognet *    notice, this list of conditions and the following disclaimer in the
12129198Scognet *    documentation and/or other materials provided with the distribution.
13129198Scognet * 3. Neither the name of the project nor the names of its contributors
14129198Scognet *    may be used to endorse or promote products derived from this software
15129198Scognet *    without specific prior written permission.
16129198Scognet *
17129198Scognet * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18129198Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19129198Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20129198Scognet * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21129198Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22129198Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23129198Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24129198Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25129198Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26129198Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27129198Scognet * SUCH DAMAGE.
28129198Scognet *
29129198Scognet *	$KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
30129198Scognet *	$KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
31129198Scognet */
32129198Scognet
33129198Scognet/*-
34129198Scognet * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
35129198Scognet *	The Regents of the University of California.
36129198Scognet * All rights reserved.
37129198Scognet *
38129198Scognet * Redistribution and use in source and binary forms, with or without
39129198Scognet * modification, are permitted provided that the following conditions
40129198Scognet * are met:
41129198Scognet * 1. Redistributions of source code must retain the above copyright
42129198Scognet *    notice, this list of conditions and the following disclaimer.
43129198Scognet * 2. Redistributions in binary form must reproduce the above copyright
44129198Scognet *    notice, this list of conditions and the following disclaimer in the
45129198Scognet *    documentation and/or other materials provided with the distribution.
46129198Scognet * 4. Neither the name of the University nor the names of its contributors
47129198Scognet *    may be used to endorse or promote products derived from this software
48129198Scognet *    without specific prior written permission.
49129198Scognet *
50129198Scognet * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51129198Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52135652Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53135652Scognet * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54129198Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55129198Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56129198Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57129198Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58129198Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59129198Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60135652Scognet * SUCH DAMAGE.
61135652Scognet *
62129198Scognet *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
63129198Scognet */
64129198Scognet
65129198Scognet#include <sys/cdefs.h>
66129198Scognet__FBSDID("$FreeBSD: head/sys/netinet6/udp6_usrreq.c 196019 2009-08-01 19:26:27Z rwatson $");
67129198Scognet
68129198Scognet#include "opt_inet.h"
69129198Scognet#include "opt_inet6.h"
70129198Scognet#include "opt_ipsec.h"
71129198Scognet
72129198Scognet#include <sys/param.h>
73129198Scognet#include <sys/jail.h>
74129198Scognet#include <sys/kernel.h>
75129198Scognet#include <sys/lock.h>
76129198Scognet#include <sys/mbuf.h>
77129198Scognet#include <sys/priv.h>
78129198Scognet#include <sys/proc.h>
79129198Scognet#include <sys/protosw.h>
80129198Scognet#include <sys/signalvar.h>
81129198Scognet#include <sys/socket.h>
82129198Scognet#include <sys/socketvar.h>
83129198Scognet#include <sys/sx.h>
84129198Scognet#include <sys/sysctl.h>
85129198Scognet#include <sys/syslog.h>
86129198Scognet#include <sys/systm.h>
87129198Scognet
88129198Scognet#include <net/if.h>
89129198Scognet#include <net/if_types.h>
90129198Scognet#include <net/route.h>
91129198Scognet
92129198Scognet#include <netinet/in.h>
93129198Scognet#include <netinet/in_pcb.h>
94129198Scognet#include <netinet/in_systm.h>
95129198Scognet#include <netinet/in_var.h>
96129198Scognet#include <netinet/ip.h>
97129198Scognet#include <netinet/ip_icmp.h>
98129198Scognet#include <netinet/ip6.h>
99129198Scognet#include <netinet/icmp_var.h>
100129198Scognet#include <netinet/icmp6.h>
101129198Scognet#include <netinet/ip_var.h>
102129198Scognet#include <netinet/udp.h>
103129198Scognet#include <netinet/udp_var.h>
104129198Scognet
105129198Scognet#include <netinet6/ip6protosw.h>
106129198Scognet#include <netinet6/ip6_var.h>
107129198Scognet#include <netinet6/in6_pcb.h>
108129198Scognet#include <netinet6/udp6_var.h>
109129198Scognet#include <netinet6/scope6_var.h>
110129198Scognet
111129198Scognet#ifdef IPSEC
112129198Scognet#include <netipsec/ipsec.h>
113129198Scognet#include <netipsec/ipsec6.h>
114129198Scognet#endif /* IPSEC */
115129198Scognet
116129198Scognet#include <security/mac/mac_framework.h>
117129198Scognet
118129198Scognet/*
119129198Scognet * UDP protocol implementation.
120129198Scognet * Per RFC 768, August, 1980.
121129198Scognet */
122129198Scognet
123129198Scognetextern struct protosw	inetsw[];
124129198Scognetstatic void		udp6_detach(struct socket *so);
125129198Scognet
126129198Scognetstatic void
127129198Scognetudp6_append(struct inpcb *inp, struct mbuf *n, int off,
128129198Scognet    struct sockaddr_in6 *fromsa)
129129198Scognet{
130129198Scognet	struct socket *so;
131129198Scognet	struct mbuf *opts;
132129198Scognet
133129198Scognet	INP_LOCK_ASSERT(inp);
134129198Scognet
135129198Scognet#ifdef IPSEC
136129198Scognet	/* Check AH/ESP integrity. */
137129198Scognet	if (ipsec6_in_reject(n, inp)) {
138129198Scognet		m_freem(n);
139129198Scognet		V_ipsec6stat.in_polvio++;
140129198Scognet		return;
141129198Scognet	}
142129198Scognet#endif /* IPSEC */
143129198Scognet#ifdef MAC
144129198Scognet	if (mac_inpcb_check_deliver(inp, n) != 0) {
145129198Scognet		m_freem(n);
146129198Scognet		return;
147129198Scognet	}
148129198Scognet#endif
149129198Scognet	opts = NULL;
150129198Scognet	if (inp->inp_flags & INP_CONTROLOPTS ||
151129198Scognet	    inp->inp_socket->so_options & SO_TIMESTAMP)
152129198Scognet		ip6_savecontrol(inp, n, &opts);
153129198Scognet	m_adj(n, off + sizeof(struct udphdr));
154129198Scognet
155129198Scognet	so = inp->inp_socket;
156129198Scognet	SOCKBUF_LOCK(&so->so_rcv);
157129198Scognet	if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
158129198Scognet	    opts) == 0) {
159129198Scognet		SOCKBUF_UNLOCK(&so->so_rcv);
160129198Scognet		m_freem(n);
161129198Scognet		if (opts)
162129198Scognet			m_freem(opts);
163129198Scognet		UDPSTAT_INC(udps_fullsock);
164129198Scognet	} else
165129198Scognet		sorwakeup_locked(so);
166129198Scognet}
167129198Scognet
168129198Scognetint
169129198Scognetudp6_input(struct mbuf **mp, int *offp, int proto)
170129198Scognet{
171129198Scognet	struct mbuf *m = *mp;
172129198Scognet	struct ifnet *ifp;
173129198Scognet	struct ip6_hdr *ip6;
174129198Scognet	struct udphdr *uh;
175129198Scognet	struct inpcb *inp;
176129198Scognet	struct udpcb *up;
177129198Scognet	int off = *offp;
178129198Scognet	int plen, ulen;
179129198Scognet	struct sockaddr_in6 fromsa;
180129198Scognet
181129198Scognet	ifp = m->m_pkthdr.rcvif;
182129198Scognet	ip6 = mtod(m, struct ip6_hdr *);
183129198Scognet
184129198Scognet	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
185129198Scognet		/* XXX send icmp6 host/port unreach? */
186129198Scognet		m_freem(m);
187129198Scognet		return (IPPROTO_DONE);
188129198Scognet	}
189129198Scognet
190129198Scognet#ifndef PULLDOWN_TEST
191129198Scognet	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
192129198Scognet	ip6 = mtod(m, struct ip6_hdr *);
193129198Scognet	uh = (struct udphdr *)((caddr_t)ip6 + off);
194129198Scognet#else
195129198Scognet	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
196129198Scognet	if (!uh)
197129198Scognet		return (IPPROTO_DONE);
198129198Scognet#endif
199129198Scognet
200129198Scognet	UDPSTAT_INC(udps_ipackets);
201129198Scognet
202129198Scognet	/*
203129198Scognet	 * Destination port of 0 is illegal, based on RFC768.
204129198Scognet	 */
205129198Scognet	if (uh->uh_dport == 0)
206129198Scognet		goto badunlocked;
207129198Scognet
208129198Scognet	plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
209129198Scognet	ulen = ntohs((u_short)uh->uh_ulen);
210129198Scognet
211129198Scognet	if (plen != ulen) {
212129198Scognet		UDPSTAT_INC(udps_badlen);
213129198Scognet		goto badunlocked;
214129198Scognet	}
215129198Scognet
216129198Scognet	/*
217129198Scognet	 * Checksum extended UDP header and data.
218129198Scognet	 */
219129198Scognet	if (uh->uh_sum == 0) {
220129198Scognet		UDPSTAT_INC(udps_nosum);
221129198Scognet		goto badunlocked;
222129198Scognet	}
223129198Scognet	if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
224129198Scognet		UDPSTAT_INC(udps_badsum);
225129198Scognet		goto badunlocked;
226129198Scognet	}
227129198Scognet
228129198Scognet	/*
229129198Scognet	 * Construct sockaddr format source address.
230129198Scognet	 */
231129198Scognet	init_sin6(&fromsa, m);
232129198Scognet	fromsa.sin6_port = uh->uh_sport;
233129198Scognet
234129198Scognet	INP_INFO_RLOCK(&V_udbinfo);
235129198Scognet	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
236129198Scognet		struct inpcb *last;
237129198Scognet		struct ip6_moptions *imo;
238129198Scognet
239129198Scognet		/*
240129198Scognet		 * In the event that laddr should be set to the link-local
241129198Scognet		 * address (this happens in RIPng), the multicast address
242129198Scognet		 * specified in the received packet will not match laddr.  To
243129198Scognet		 * handle this situation, matching is relaxed if the
244129198Scognet		 * receiving interface is the same as one specified in the
245129198Scognet		 * socket and if the destination multicast address matches
246129198Scognet		 * one of the multicast groups specified in the socket.
247129198Scognet		 */
248129198Scognet
249129198Scognet		/*
250129198Scognet		 * KAME note: traditionally we dropped udpiphdr from mbuf
251129198Scognet		 * here.  We need udphdr for IPsec processing so we do that
252129198Scognet		 * later.
253129198Scognet		 */
254129198Scognet		last = NULL;
255129198Scognet		LIST_FOREACH(inp, &V_udb, inp_list) {
256129198Scognet			if ((inp->inp_vflag & INP_IPV6) == 0)
257129198Scognet				continue;
258129198Scognet			if (inp->inp_lport != uh->uh_dport)
259129198Scognet				continue;
260129198Scognet			if (inp->inp_fport != 0 &&
261129198Scognet			    inp->inp_fport != uh->uh_sport)
262129198Scognet				continue;
263129198Scognet			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
264129198Scognet				if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
265129198Scognet							&ip6->ip6_dst))
266129198Scognet					continue;
267129198Scognet			}
268129198Scognet			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
269129198Scognet				if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
270129198Scognet							&ip6->ip6_src) ||
271129198Scognet				    inp->inp_fport != uh->uh_sport)
272129198Scognet					continue;
273129198Scognet			}
274129198Scognet
275129198Scognet			/*
276129198Scognet			 * Handle socket delivery policy for any-source
277129198Scognet			 * and source-specific multicast. [RFC3678]
278129198Scognet			 */
279129198Scognet			imo = inp->in6p_moptions;
280129198Scognet			if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
281129198Scognet				struct sockaddr_in6	 mcaddr;
282129198Scognet				int			 blocked;
283129198Scognet
284129198Scognet				INP_RLOCK(inp);
285129198Scognet
286129198Scognet				bzero(&mcaddr, sizeof(struct sockaddr_in6));
287129198Scognet				mcaddr.sin6_len = sizeof(struct sockaddr_in6);
288129198Scognet				mcaddr.sin6_family = AF_INET6;
289129198Scognet				mcaddr.sin6_addr = ip6->ip6_dst;
290129198Scognet
291129198Scognet				blocked = im6o_mc_filter(imo, ifp,
292129198Scognet					(struct sockaddr *)&mcaddr,
293129198Scognet					(struct sockaddr *)&fromsa);
294129198Scognet				if (blocked != MCAST_PASS) {
295129198Scognet					if (blocked == MCAST_NOTGMEMBER)
296129198Scognet						IP6STAT_INC(ip6s_notmember);
297129198Scognet					if (blocked == MCAST_NOTSMEMBER ||
298129198Scognet					    blocked == MCAST_MUTED)
299129198Scognet						UDPSTAT_INC(udps_filtermcast);
300129198Scognet					INP_RUNLOCK(inp); /* XXX */
301129198Scognet					continue;
302129198Scognet				}
303129198Scognet
304129198Scognet				INP_RUNLOCK(inp);
305129198Scognet			}
306129198Scognet			if (last != NULL) {
307129198Scognet				struct mbuf *n;
308129198Scognet
309129198Scognet				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
310129198Scognet					INP_RLOCK(last);
311129198Scognet					up = intoudpcb(last);
312129198Scognet					if (up->u_tun_func == NULL) {
313129198Scognet						udp6_append(last, n, off, &fromsa);
314129198Scognet					} else {
315129198Scognet						/*
316129198Scognet						 * Engage the tunneling
317129198Scognet						 * protocol we will have to
318129198Scognet						 * leave the info_lock up,
319129198Scognet						 * since we are hunting
320129198Scognet						 * through multiple UDP's.
321129198Scognet						 *
322129198Scognet						 */
323129198Scognet						(*up->u_tun_func)(n, off, last);
324129198Scognet					}
325129198Scognet					INP_RUNLOCK(last);
326129198Scognet				}
327129198Scognet			}
328129198Scognet			last = inp;
329129198Scognet			/*
330129198Scognet			 * Don't look for additional matches if this one does
331129198Scognet			 * not have either the SO_REUSEPORT or SO_REUSEADDR
332129198Scognet			 * socket options set.  This heuristic avoids
333129198Scognet			 * searching through all pcbs in the common case of a
334129198Scognet			 * non-shared port.  It assumes that an application
335129198Scognet			 * will never clear these options after setting them.
336129198Scognet			 */
337129198Scognet			if ((last->inp_socket->so_options &
338129198Scognet			     (SO_REUSEPORT|SO_REUSEADDR)) == 0)
339129198Scognet				break;
340129198Scognet		}
341129198Scognet
342129198Scognet		if (last == NULL) {
343129198Scognet			/*
344129198Scognet			 * No matching pcb found; discard datagram.  (No need
345129198Scognet			 * to send an ICMP Port Unreachable for a broadcast
346129198Scognet			 * or multicast datgram.)
347129198Scognet			 */
348129198Scognet			UDPSTAT_INC(udps_noport);
349129198Scognet			UDPSTAT_INC(udps_noportmcast);
350129198Scognet			goto badheadlocked;
351129198Scognet		}
352129198Scognet		INP_RLOCK(last);
353129198Scognet		INP_INFO_RUNLOCK(&V_udbinfo);
354129198Scognet		up = intoudpcb(last);
355129198Scognet		if (up->u_tun_func == NULL) {
356129198Scognet			udp6_append(last, m, off, &fromsa);
357129198Scognet		} else {
358129198Scognet			/*
359129198Scognet			 * Engage the tunneling protocol.
360129198Scognet			 */
361129198Scognet			(*up->u_tun_func)(m, off, last);
362129198Scognet		}
363129198Scognet		INP_RUNLOCK(last);
364129198Scognet		return (IPPROTO_DONE);
365129198Scognet	}
366137272Scognet	/*
367137272Scognet	 * Locate pcb for datagram.
368137272Scognet	 */
369137272Scognet	inp = in6_pcblookup_hash(&V_udbinfo, &ip6->ip6_src, uh->uh_sport,
370137272Scognet	    &ip6->ip6_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
371137272Scognet	if (inp == NULL) {
372137272Scognet		if (udp_log_in_vain) {
373137272Scognet			char ip6bufs[INET6_ADDRSTRLEN];
374137272Scognet			char ip6bufd[INET6_ADDRSTRLEN];
375137272Scognet
376137272Scognet			log(LOG_INFO,
377137272Scognet			    "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
378137272Scognet			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
379137272Scognet			    ntohs(uh->uh_dport),
380137272Scognet			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
381137272Scognet			    ntohs(uh->uh_sport));
382137272Scognet		}
383137272Scognet		UDPSTAT_INC(udps_noport);
384129198Scognet		if (m->m_flags & M_MCAST) {
385129198Scognet			printf("UDP6: M_MCAST is set in a unicast packet.\n");
386129198Scognet			UDPSTAT_INC(udps_noportmcast);
387			goto badheadlocked;
388		}
389		INP_INFO_RUNLOCK(&V_udbinfo);
390		if (V_udp_blackhole)
391			goto badunlocked;
392		if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
393			goto badunlocked;
394		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
395		return (IPPROTO_DONE);
396	}
397	INP_RLOCK(inp);
398	INP_INFO_RUNLOCK(&V_udbinfo);
399	up = intoudpcb(inp);
400	if (up->u_tun_func == NULL) {
401		udp6_append(inp, m, off, &fromsa);
402	} else {
403		/*
404		 * Engage the tunneling protocol.
405		 */
406
407		(*up->u_tun_func)(m, off, inp);
408	}
409	INP_RUNLOCK(inp);
410	return (IPPROTO_DONE);
411
412badheadlocked:
413	INP_INFO_RUNLOCK(&V_udbinfo);
414badunlocked:
415	if (m)
416		m_freem(m);
417	return (IPPROTO_DONE);
418}
419
420void
421udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
422{
423	struct udphdr uh;
424	struct ip6_hdr *ip6;
425	struct mbuf *m;
426	int off = 0;
427	struct ip6ctlparam *ip6cp = NULL;
428	const struct sockaddr_in6 *sa6_src = NULL;
429	void *cmdarg;
430	struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
431	struct udp_portonly {
432		u_int16_t uh_sport;
433		u_int16_t uh_dport;
434	} *uhp;
435
436	if (sa->sa_family != AF_INET6 ||
437	    sa->sa_len != sizeof(struct sockaddr_in6))
438		return;
439
440	if ((unsigned)cmd >= PRC_NCMDS)
441		return;
442	if (PRC_IS_REDIRECT(cmd))
443		notify = in6_rtchange, d = NULL;
444	else if (cmd == PRC_HOSTDEAD)
445		d = NULL;
446	else if (inet6ctlerrmap[cmd] == 0)
447		return;
448
449	/* if the parameter is from icmp6, decode it. */
450	if (d != NULL) {
451		ip6cp = (struct ip6ctlparam *)d;
452		m = ip6cp->ip6c_m;
453		ip6 = ip6cp->ip6c_ip6;
454		off = ip6cp->ip6c_off;
455		cmdarg = ip6cp->ip6c_cmdarg;
456		sa6_src = ip6cp->ip6c_src;
457	} else {
458		m = NULL;
459		ip6 = NULL;
460		cmdarg = NULL;
461		sa6_src = &sa6_any;
462	}
463
464	if (ip6) {
465		/*
466		 * XXX: We assume that when IPV6 is non NULL,
467		 * M and OFF are valid.
468		 */
469
470		/* Check if we can safely examine src and dst ports. */
471		if (m->m_pkthdr.len < off + sizeof(*uhp))
472			return;
473
474		bzero(&uh, sizeof(uh));
475		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
476
477		(void) in6_pcbnotify(&V_udbinfo, sa, uh.uh_dport,
478		    (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
479		    cmdarg, notify);
480	} else
481		(void) in6_pcbnotify(&V_udbinfo, sa, 0,
482		    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
483}
484
485static int
486udp6_getcred(SYSCTL_HANDLER_ARGS)
487{
488	struct xucred xuc;
489	struct sockaddr_in6 addrs[2];
490	struct inpcb *inp;
491	int error;
492
493	error = priv_check(req->td, PRIV_NETINET_GETCRED);
494	if (error)
495		return (error);
496
497	if (req->newlen != sizeof(addrs))
498		return (EINVAL);
499	if (req->oldlen != sizeof(struct xucred))
500		return (EINVAL);
501	error = SYSCTL_IN(req, addrs, sizeof(addrs));
502	if (error)
503		return (error);
504	if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
505	    (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
506		return (error);
507	}
508	INP_INFO_RLOCK(&V_udbinfo);
509	inp = in6_pcblookup_hash(&V_udbinfo, &addrs[1].sin6_addr,
510	    addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port, 1,
511	    NULL);
512	if (inp != NULL) {
513		INP_RLOCK(inp);
514		INP_INFO_RUNLOCK(&V_udbinfo);
515		if (inp->inp_socket == NULL)
516			error = ENOENT;
517		if (error == 0)
518			error = cr_canseesocket(req->td->td_ucred,
519			    inp->inp_socket);
520		if (error == 0)
521			cru2x(inp->inp_cred, &xuc);
522		INP_RUNLOCK(inp);
523	} else {
524		INP_INFO_RUNLOCK(&V_udbinfo);
525		error = ENOENT;
526	}
527	if (error == 0)
528		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
529	return (error);
530}
531
532SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
533    0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
534
535static int
536udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
537    struct mbuf *control, struct thread *td)
538{
539	u_int32_t ulen = m->m_pkthdr.len;
540	u_int32_t plen = sizeof(struct udphdr) + ulen;
541	struct ip6_hdr *ip6;
542	struct udphdr *udp6;
543	struct in6_addr *laddr, *faddr, in6a;
544	struct sockaddr_in6 *sin6 = NULL;
545	struct ifnet *oifp = NULL;
546	int scope_ambiguous = 0;
547	u_short fport;
548	int error = 0;
549	struct ip6_pktopts *optp, opt;
550	int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
551	int flags;
552	struct sockaddr_in6 tmp;
553
554	INP_WLOCK_ASSERT(inp);
555
556	if (addr6) {
557		/* addr6 has been validated in udp6_send(). */
558		sin6 = (struct sockaddr_in6 *)addr6;
559
560		/* protect *sin6 from overwrites */
561		tmp = *sin6;
562		sin6 = &tmp;
563
564		/*
565		 * Application should provide a proper zone ID or the use of
566		 * default zone IDs should be enabled.  Unfortunately, some
567		 * applications do not behave as it should, so we need a
568		 * workaround.  Even if an appropriate ID is not determined,
569		 * we'll see if we can determine the outgoing interface.  If we
570		 * can, determine the zone ID based on the interface below.
571		 */
572		if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
573			scope_ambiguous = 1;
574		if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
575			return (error);
576	}
577
578	if (control) {
579		if ((error = ip6_setpktopts(control, &opt,
580		    inp->in6p_outputopts, td->td_ucred, IPPROTO_UDP)) != 0)
581			goto release;
582		optp = &opt;
583	} else
584		optp = inp->in6p_outputopts;
585
586	if (sin6) {
587		faddr = &sin6->sin6_addr;
588
589		/*
590		 * IPv4 version of udp_output calls in_pcbconnect in this case,
591		 * which needs splnet and affects performance.
592		 * Since we saw no essential reason for calling in_pcbconnect,
593		 * we get rid of such kind of logic, and call in6_selectsrc
594		 * and in6_pcbsetport in order to fill in the local address
595		 * and the local port.
596		 */
597		if (sin6->sin6_port == 0) {
598			error = EADDRNOTAVAIL;
599			goto release;
600		}
601
602		if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
603			/* how about ::ffff:0.0.0.0 case? */
604			error = EISCONN;
605			goto release;
606		}
607
608		fport = sin6->sin6_port; /* allow 0 port */
609
610		if (IN6_IS_ADDR_V4MAPPED(faddr)) {
611			if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
612				/*
613				 * I believe we should explicitly discard the
614				 * packet when mapped addresses are disabled,
615				 * rather than send the packet as an IPv6 one.
616				 * If we chose the latter approach, the packet
617				 * might be sent out on the wire based on the
618				 * default route, the situation which we'd
619				 * probably want to avoid.
620				 * (20010421 jinmei@kame.net)
621				 */
622				error = EINVAL;
623				goto release;
624			}
625			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
626			    !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
627				/*
628				 * when remote addr is an IPv4-mapped address,
629				 * local addr should not be an IPv6 address,
630				 * since you cannot determine how to map IPv6
631				 * source address to IPv4.
632				 */
633				error = EINVAL;
634				goto release;
635			}
636
637			af = AF_INET;
638		}
639
640		if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
641			error = in6_selectsrc(sin6, optp, inp, NULL,
642			    td->td_ucred, &oifp, &in6a);
643			if (error)
644				goto release;
645			if (oifp && scope_ambiguous &&
646			    (error = in6_setscope(&sin6->sin6_addr,
647			    oifp, NULL))) {
648				goto release;
649			}
650			laddr = &in6a;
651		} else
652			laddr = &inp->in6p_laddr;	/* XXX */
653		if (laddr == NULL) {
654			if (error == 0)
655				error = EADDRNOTAVAIL;
656			goto release;
657		}
658		if (inp->inp_lport == 0 &&
659		    (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0)
660			goto release;
661	} else {
662		if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
663			error = ENOTCONN;
664			goto release;
665		}
666		if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
667			if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
668				/*
669				 * XXX: this case would happen when the
670				 * application sets the V6ONLY flag after
671				 * connecting the foreign address.
672				 * Such applications should be fixed,
673				 * so we bark here.
674				 */
675				log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
676				    "option was set for a connected socket\n");
677				error = EINVAL;
678				goto release;
679			} else
680				af = AF_INET;
681		}
682		laddr = &inp->in6p_laddr;
683		faddr = &inp->in6p_faddr;
684		fport = inp->inp_fport;
685	}
686
687	if (af == AF_INET)
688		hlen = sizeof(struct ip);
689
690	/*
691	 * Calculate data length and get a mbuf
692	 * for UDP and IP6 headers.
693	 */
694	M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
695	if (m == 0) {
696		error = ENOBUFS;
697		goto release;
698	}
699
700	/*
701	 * Stuff checksum and output datagram.
702	 */
703	udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
704	udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
705	udp6->uh_dport = fport;
706	if (plen <= 0xffff)
707		udp6->uh_ulen = htons((u_short)plen);
708	else
709		udp6->uh_ulen = 0;
710	udp6->uh_sum = 0;
711
712	switch (af) {
713	case AF_INET6:
714		ip6 = mtod(m, struct ip6_hdr *);
715		ip6->ip6_flow	= inp->inp_flow & IPV6_FLOWINFO_MASK;
716		ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
717		ip6->ip6_vfc	|= IPV6_VERSION;
718#if 0				/* ip6_plen will be filled in ip6_output. */
719		ip6->ip6_plen	= htons((u_short)plen);
720#endif
721		ip6->ip6_nxt	= IPPROTO_UDP;
722		ip6->ip6_hlim	= in6_selecthlim(inp, NULL);
723		ip6->ip6_src	= *laddr;
724		ip6->ip6_dst	= *faddr;
725
726		if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
727				sizeof(struct ip6_hdr), plen)) == 0) {
728			udp6->uh_sum = 0xffff;
729		}
730
731		flags = 0;
732
733		UDPSTAT_INC(udps_opackets);
734		error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
735		    NULL, inp);
736		break;
737	case AF_INET:
738		error = EAFNOSUPPORT;
739		goto release;
740	}
741	goto releaseopt;
742
743release:
744	m_freem(m);
745
746releaseopt:
747	if (control) {
748		ip6_clearpktopts(&opt, -1);
749		m_freem(control);
750	}
751	return (error);
752}
753
754static void
755udp6_abort(struct socket *so)
756{
757	struct inpcb *inp;
758
759	inp = sotoinpcb(so);
760	KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
761
762#ifdef INET
763	if (inp->inp_vflag & INP_IPV4) {
764		struct pr_usrreqs *pru;
765
766		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
767		(*pru->pru_abort)(so);
768		return;
769	}
770#endif
771
772	INP_INFO_WLOCK(&V_udbinfo);
773	INP_WLOCK(inp);
774	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
775		in6_pcbdisconnect(inp);
776		inp->in6p_laddr = in6addr_any;
777		soisdisconnected(so);
778	}
779	INP_WUNLOCK(inp);
780	INP_INFO_WUNLOCK(&V_udbinfo);
781}
782
783static int
784udp6_attach(struct socket *so, int proto, struct thread *td)
785{
786	struct inpcb *inp;
787	int error;
788
789	inp = sotoinpcb(so);
790	KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
791
792	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
793		error = soreserve(so, udp_sendspace, udp_recvspace);
794		if (error)
795			return (error);
796	}
797	INP_INFO_WLOCK(&V_udbinfo);
798	error = in_pcballoc(so, &V_udbinfo);
799	if (error) {
800		INP_INFO_WUNLOCK(&V_udbinfo);
801		return (error);
802	}
803	inp = (struct inpcb *)so->so_pcb;
804	inp->inp_vflag |= INP_IPV6;
805	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
806		inp->inp_vflag |= INP_IPV4;
807	inp->in6p_hops = -1;	/* use kernel default */
808	inp->in6p_cksum = -1;	/* just to be sure */
809	/*
810	 * XXX: ugly!!
811	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
812	 * because the socket may be bound to an IPv6 wildcard address,
813	 * which may match an IPv4-mapped IPv6 address.
814	 */
815	inp->inp_ip_ttl = V_ip_defttl;
816
817	error = udp_newudpcb(inp);
818	if (error) {
819		in_pcbdetach(inp);
820		in_pcbfree(inp);
821		INP_INFO_WUNLOCK(&V_udbinfo);
822		return (error);
823	}
824	INP_WUNLOCK(inp);
825	INP_INFO_WUNLOCK(&V_udbinfo);
826	return (0);
827}
828
829static int
830udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
831{
832	struct inpcb *inp;
833	int error;
834
835	inp = sotoinpcb(so);
836	KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
837
838	INP_INFO_WLOCK(&V_udbinfo);
839	INP_WLOCK(inp);
840	inp->inp_vflag &= ~INP_IPV4;
841	inp->inp_vflag |= INP_IPV6;
842	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
843		struct sockaddr_in6 *sin6_p;
844
845		sin6_p = (struct sockaddr_in6 *)nam;
846
847		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
848			inp->inp_vflag |= INP_IPV4;
849		else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
850			struct sockaddr_in sin;
851
852			in6_sin6_2_sin(&sin, sin6_p);
853			inp->inp_vflag |= INP_IPV4;
854			inp->inp_vflag &= ~INP_IPV6;
855			error = in_pcbbind(inp, (struct sockaddr *)&sin,
856			    td->td_ucred);
857			goto out;
858		}
859	}
860
861	error = in6_pcbbind(inp, nam, td->td_ucred);
862out:
863	INP_WUNLOCK(inp);
864	INP_INFO_WUNLOCK(&V_udbinfo);
865	return (error);
866}
867
868static void
869udp6_close(struct socket *so)
870{
871	struct inpcb *inp;
872
873	inp = sotoinpcb(so);
874	KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
875
876#ifdef INET
877	if (inp->inp_vflag & INP_IPV4) {
878		struct pr_usrreqs *pru;
879
880		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
881		(*pru->pru_disconnect)(so);
882		return;
883	}
884#endif
885	INP_INFO_WLOCK(&V_udbinfo);
886	INP_WLOCK(inp);
887	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
888		in6_pcbdisconnect(inp);
889		inp->in6p_laddr = in6addr_any;
890		soisdisconnected(so);
891	}
892	INP_WUNLOCK(inp);
893	INP_INFO_WUNLOCK(&V_udbinfo);
894}
895
896static int
897udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
898{
899	struct inpcb *inp;
900	struct sockaddr_in6 *sin6;
901	int error;
902
903	inp = sotoinpcb(so);
904	sin6 = (struct sockaddr_in6 *)nam;
905	KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
906
907	INP_INFO_WLOCK(&V_udbinfo);
908	INP_WLOCK(inp);
909	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
910	    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
911		struct sockaddr_in sin;
912
913		if (inp->inp_faddr.s_addr != INADDR_ANY) {
914			error = EISCONN;
915			goto out;
916		}
917		in6_sin6_2_sin(&sin, sin6);
918		error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
919		if (error != 0)
920			goto out;
921		error = in_pcbconnect(inp, (struct sockaddr *)&sin,
922		    td->td_ucred);
923		if (error == 0) {
924			inp->inp_vflag |= INP_IPV4;
925			inp->inp_vflag &= ~INP_IPV6;
926			soisconnected(so);
927		}
928		goto out;
929	}
930	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
931		error = EISCONN;
932		goto out;
933	}
934	error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
935	if (error != 0)
936		goto out;
937	error = in6_pcbconnect(inp, nam, td->td_ucred);
938	if (error == 0) {
939		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
940			/* should be non mapped addr */
941			inp->inp_vflag &= ~INP_IPV4;
942			inp->inp_vflag |= INP_IPV6;
943		}
944		soisconnected(so);
945	}
946out:
947	INP_WUNLOCK(inp);
948	INP_INFO_WUNLOCK(&V_udbinfo);
949	return (error);
950}
951
952static void
953udp6_detach(struct socket *so)
954{
955	struct inpcb *inp;
956	struct udpcb *up;
957
958	inp = sotoinpcb(so);
959	KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
960
961	INP_INFO_WLOCK(&V_udbinfo);
962	INP_WLOCK(inp);
963	up = intoudpcb(inp);
964	KASSERT(up != NULL, ("%s: up == NULL", __func__));
965	in_pcbdetach(inp);
966	in_pcbfree(inp);
967	INP_INFO_WUNLOCK(&V_udbinfo);
968	udp_discardcb(up);
969}
970
971static int
972udp6_disconnect(struct socket *so)
973{
974	struct inpcb *inp;
975	int error;
976
977	inp = sotoinpcb(so);
978	KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
979
980	INP_INFO_WLOCK(&V_udbinfo);
981	INP_WLOCK(inp);
982
983#ifdef INET
984	if (inp->inp_vflag & INP_IPV4) {
985		struct pr_usrreqs *pru;
986
987		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
988		error = (*pru->pru_disconnect)(so);
989		goto out;
990	}
991#endif
992
993	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
994		error = ENOTCONN;
995		goto out;
996	}
997
998	in6_pcbdisconnect(inp);
999	inp->in6p_laddr = in6addr_any;
1000	SOCK_LOCK(so);
1001	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1002	SOCK_UNLOCK(so);
1003out:
1004	INP_WUNLOCK(inp);
1005	INP_INFO_WUNLOCK(&V_udbinfo);
1006	return (0);
1007}
1008
1009static int
1010udp6_send(struct socket *so, int flags, struct mbuf *m,
1011    struct sockaddr *addr, struct mbuf *control, struct thread *td)
1012{
1013	struct inpcb *inp;
1014	int error = 0;
1015
1016	inp = sotoinpcb(so);
1017	KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
1018
1019	INP_INFO_WLOCK(&V_udbinfo);
1020	INP_WLOCK(inp);
1021	if (addr) {
1022		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1023			error = EINVAL;
1024			goto bad;
1025		}
1026		if (addr->sa_family != AF_INET6) {
1027			error = EAFNOSUPPORT;
1028			goto bad;
1029		}
1030	}
1031
1032#ifdef INET
1033	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1034		int hasv4addr;
1035		struct sockaddr_in6 *sin6 = 0;
1036
1037		if (addr == 0)
1038			hasv4addr = (inp->inp_vflag & INP_IPV4);
1039		else {
1040			sin6 = (struct sockaddr_in6 *)addr;
1041			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
1042			    ? 1 : 0;
1043		}
1044		if (hasv4addr) {
1045			struct pr_usrreqs *pru;
1046
1047			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
1048			    !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
1049				/*
1050				 * When remote addr is IPv4-mapped address,
1051				 * local addr should not be an IPv6 address;
1052				 * since you cannot determine how to map IPv6
1053				 * source address to IPv4.
1054				 */
1055				error = EINVAL;
1056				goto out;
1057			}
1058
1059			/*
1060			 * XXXRW: We release UDP-layer locks before calling
1061			 * udp_send() in order to avoid recursion.  However,
1062			 * this does mean there is a short window where inp's
1063			 * fields are unstable.  Could this lead to a
1064			 * potential race in which the factors causing us to
1065			 * select the UDPv4 output routine are invalidated?
1066			 */
1067			INP_WUNLOCK(inp);
1068			INP_INFO_WUNLOCK(&V_udbinfo);
1069			if (sin6)
1070				in6_sin6_2_sin_in_sock(addr);
1071			pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1072			/* addr will just be freed in sendit(). */
1073			return ((*pru->pru_send)(so, flags, m, addr, control,
1074			    td));
1075		}
1076	}
1077#endif
1078#ifdef MAC
1079	mac_inpcb_create_mbuf(inp, m);
1080#endif
1081	error = udp6_output(inp, m, addr, control, td);
1082out:
1083	INP_WUNLOCK(inp);
1084	INP_INFO_WUNLOCK(&V_udbinfo);
1085	return (error);
1086
1087bad:
1088	INP_WUNLOCK(inp);
1089	INP_INFO_WUNLOCK(&V_udbinfo);
1090	m_freem(m);
1091	return (error);
1092}
1093
1094struct pr_usrreqs udp6_usrreqs = {
1095	.pru_abort =		udp6_abort,
1096	.pru_attach =		udp6_attach,
1097	.pru_bind =		udp6_bind,
1098	.pru_connect =		udp6_connect,
1099	.pru_control =		in6_control,
1100	.pru_detach =		udp6_detach,
1101	.pru_disconnect =	udp6_disconnect,
1102	.pru_peeraddr =		in6_mapped_peeraddr,
1103	.pru_send =		udp6_send,
1104	.pru_shutdown =		udp_shutdown,
1105	.pru_sockaddr =		in6_mapped_sockaddr,
1106	.pru_soreceive =	soreceive_dgram,
1107	.pru_sosend =		sosend_dgram,
1108	.pru_sosetlabel =	in_pcbsosetlabel,
1109	.pru_close =		udp6_close
1110};
1111