in6_gif.c revision 147507
1/*	$FreeBSD: head/sys/netinet6/in6_gif.c 147507 2005-06-20 20:17:00Z ume $	*/
2/*	$KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $	*/
3
4/*-
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include "opt_inet.h"
34#include "opt_inet6.h"
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/socket.h>
39#include <sys/sockio.h>
40#include <sys/mbuf.h>
41#include <sys/errno.h>
42#include <sys/queue.h>
43#include <sys/syslog.h>
44#include <sys/protosw.h>
45
46#include <sys/malloc.h>
47
48#include <net/if.h>
49#include <net/route.h>
50
51#include <netinet/in.h>
52#include <netinet/in_systm.h>
53#ifdef INET
54#include <netinet/ip.h>
55#endif
56#include <netinet/ip_encap.h>
57#ifdef INET6
58#include <netinet/ip6.h>
59#include <netinet6/ip6_var.h>
60#include <netinet6/in6_gif.h>
61#include <netinet6/in6_var.h>
62#endif
63#include <netinet6/ip6protosw.h>
64#include <netinet/ip_ecn.h>
65#ifdef INET6
66#include <netinet6/ip6_ecn.h>
67#endif
68
69#include <net/if_gif.h>
70
71#include <net/net_osdep.h>
72
73static int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
74			 struct ifnet *);
75
76extern  struct domain inet6domain;
77struct ip6protosw in6_gif_protosw =
78{ SOCK_RAW,	&inet6domain,	0/* IPPROTO_IPV[46] */,	PR_ATOMIC|PR_ADDR,
79  in6_gif_input, rip6_output,	0,		rip6_ctloutput,
80  0,
81  0,		0,		0,		0,
82  &rip6_usrreqs
83};
84
85int
86in6_gif_output(ifp, family, m)
87	struct ifnet *ifp;
88	int family; /* family of the packet to be encapsulate. */
89	struct mbuf *m;
90{
91	struct gif_softc *sc = ifp->if_softc;
92	struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
93	struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
94	struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
95	struct ip6_hdr *ip6;
96	int proto, error;
97	u_int8_t itos, otos;
98
99	if (sin6_src == NULL || sin6_dst == NULL ||
100	    sin6_src->sin6_family != AF_INET6 ||
101	    sin6_dst->sin6_family != AF_INET6) {
102		m_freem(m);
103		return EAFNOSUPPORT;
104	}
105
106	switch (family) {
107#ifdef INET
108	case AF_INET:
109	    {
110		struct ip *ip;
111
112		proto = IPPROTO_IPV4;
113		if (m->m_len < sizeof(*ip)) {
114			m = m_pullup(m, sizeof(*ip));
115			if (!m)
116				return ENOBUFS;
117		}
118		ip = mtod(m, struct ip *);
119		itos = ip->ip_tos;
120		break;
121	    }
122#endif
123#ifdef INET6
124	case AF_INET6:
125	    {
126		struct ip6_hdr *ip6;
127		proto = IPPROTO_IPV6;
128		if (m->m_len < sizeof(*ip6)) {
129			m = m_pullup(m, sizeof(*ip6));
130			if (!m)
131				return ENOBUFS;
132		}
133		ip6 = mtod(m, struct ip6_hdr *);
134		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
135		break;
136	    }
137#endif
138	default:
139#ifdef DEBUG
140		printf("in6_gif_output: warning: unknown family %d passed\n",
141			family);
142#endif
143		m_freem(m);
144		return EAFNOSUPPORT;
145	}
146
147	/* prepend new IP header */
148	M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
149	if (m && m->m_len < sizeof(struct ip6_hdr))
150		m = m_pullup(m, sizeof(struct ip6_hdr));
151	if (m == NULL) {
152		printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
153		return ENOBUFS;
154	}
155
156	ip6 = mtod(m, struct ip6_hdr *);
157	ip6->ip6_flow	= 0;
158	ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
159	ip6->ip6_vfc	|= IPV6_VERSION;
160	ip6->ip6_plen	= htons((u_short)m->m_pkthdr.len);
161	ip6->ip6_nxt	= proto;
162	ip6->ip6_hlim	= ip6_gif_hlim;
163	ip6->ip6_src	= sin6_src->sin6_addr;
164	/* bidirectional configured tunnel mode */
165	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
166		ip6->ip6_dst = sin6_dst->sin6_addr;
167	else  {
168		m_freem(m);
169		return ENETUNREACH;
170	}
171	ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
172		       &otos, &itos);
173	ip6->ip6_flow &= ~htonl(0xff << 20);
174	ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
175
176	if (dst->sin6_family != sin6_dst->sin6_family ||
177	     !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
178		/* cache route doesn't match */
179		bzero(dst, sizeof(*dst));
180		dst->sin6_family = sin6_dst->sin6_family;
181		dst->sin6_len = sizeof(struct sockaddr_in6);
182		dst->sin6_addr = sin6_dst->sin6_addr;
183		if (sc->gif_ro6.ro_rt) {
184			RTFREE(sc->gif_ro6.ro_rt);
185			sc->gif_ro6.ro_rt = NULL;
186		}
187#if 0
188		GIF2IFP(sc)->if_mtu = GIF_MTU;
189#endif
190	}
191
192	if (sc->gif_ro6.ro_rt == NULL) {
193		rtalloc((struct route *)&sc->gif_ro6);
194		if (sc->gif_ro6.ro_rt == NULL) {
195			m_freem(m);
196			return ENETUNREACH;
197		}
198
199		/* if it constitutes infinite encapsulation, punt. */
200		if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
201			m_freem(m);
202			return ENETUNREACH;	/*XXX*/
203		}
204#if 0
205		ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
206			- sizeof(struct ip6_hdr);
207#endif
208	}
209
210#ifdef IPV6_MINMTU
211	/*
212	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
213	 * it is too painful to ask for resend of inner packet, to achieve
214	 * path MTU discovery for encapsulated packets.
215	 */
216	error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL);
217#else
218	error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL);
219#endif
220
221	if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
222	    sc->gif_ro6.ro_rt != NULL) {
223		RTFREE(sc->gif_ro6.ro_rt);
224		sc->gif_ro6.ro_rt = NULL;
225	}
226
227	return (error);
228}
229
230int
231in6_gif_input(mp, offp, proto)
232	struct mbuf **mp;
233	int *offp, proto;
234{
235	struct mbuf *m = *mp;
236	struct ifnet *gifp = NULL;
237	struct gif_softc *sc;
238	struct ip6_hdr *ip6;
239	int af = 0;
240	u_int32_t otos;
241
242	ip6 = mtod(m, struct ip6_hdr *);
243
244	sc = (struct gif_softc *)encap_getarg(m);
245	if (sc == NULL) {
246		m_freem(m);
247		ip6stat.ip6s_nogif++;
248		return IPPROTO_DONE;
249	}
250
251	gifp = GIF2IFP(sc);
252	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
253		m_freem(m);
254		ip6stat.ip6s_nogif++;
255		return IPPROTO_DONE;
256	}
257
258	otos = ip6->ip6_flow;
259	m_adj(m, *offp);
260
261	switch (proto) {
262#ifdef INET
263	case IPPROTO_IPV4:
264	    {
265		struct ip *ip;
266		u_int8_t otos8;
267		af = AF_INET;
268		otos8 = (ntohl(otos) >> 20) & 0xff;
269		if (m->m_len < sizeof(*ip)) {
270			m = m_pullup(m, sizeof(*ip));
271			if (!m)
272				return IPPROTO_DONE;
273		}
274		ip = mtod(m, struct ip *);
275		if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
276				  ECN_ALLOWED : ECN_NOCARE,
277				  &otos8, &ip->ip_tos) == 0) {
278			m_freem(m);
279			return IPPROTO_DONE;
280		}
281		break;
282	    }
283#endif /* INET */
284#ifdef INET6
285	case IPPROTO_IPV6:
286	    {
287		struct ip6_hdr *ip6;
288		af = AF_INET6;
289		if (m->m_len < sizeof(*ip6)) {
290			m = m_pullup(m, sizeof(*ip6));
291			if (!m)
292				return IPPROTO_DONE;
293		}
294		ip6 = mtod(m, struct ip6_hdr *);
295		if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ?
296				   ECN_ALLOWED : ECN_NOCARE,
297				   &otos, &ip6->ip6_flow) == 0) {
298			m_freem(m);
299			return IPPROTO_DONE;
300		}
301		break;
302	    }
303#endif
304	default:
305		ip6stat.ip6s_nogif++;
306		m_freem(m);
307		return IPPROTO_DONE;
308	}
309
310	gif_input(m, af, gifp);
311	return IPPROTO_DONE;
312}
313
314/*
315 * validate outer address.
316 */
317static int
318gif_validate6(ip6, sc, ifp)
319	const struct ip6_hdr *ip6;
320	struct gif_softc *sc;
321	struct ifnet *ifp;
322{
323	struct sockaddr_in6 *src, *dst;
324
325	src = (struct sockaddr_in6 *)sc->gif_psrc;
326	dst = (struct sockaddr_in6 *)sc->gif_pdst;
327
328	/*
329	 * Check for address match.  Note that the check is for an incoming
330	 * packet.  We should compare the *source* address in our configuration
331	 * and the *destination* address of the packet, and vice versa.
332	 */
333	if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
334	    !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
335		return 0;
336
337	/* martian filters on outer source - done in ip6_input */
338
339	/* ingress filters on outer source */
340	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
341		struct sockaddr_in6 sin6;
342		struct rtentry *rt;
343
344		bzero(&sin6, sizeof(sin6));
345		sin6.sin6_family = AF_INET6;
346		sin6.sin6_len = sizeof(struct sockaddr_in6);
347		sin6.sin6_addr = ip6->ip6_src;
348		sin6.sin6_scope_id = 0; /* XXX */
349
350		rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
351		if (!rt || rt->rt_ifp != ifp) {
352#if 0
353			log(LOG_WARNING, "%s: packet from %s dropped "
354			    "due to ingress filter\n", if_name(GIF2IFP(sc)),
355			    ip6_sprintf(&sin6.sin6_addr));
356#endif
357			if (rt)
358				rtfree(rt);
359			return 0;
360		}
361		rtfree(rt);
362	}
363
364	return 128 * 2;
365}
366
367/*
368 * we know that we are in IFF_UP, outer address available, and outer family
369 * matched the physical addr family.  see gif_encapcheck().
370 * sanity check for arg should have been done in the caller.
371 */
372int
373gif_encapcheck6(m, off, proto, arg)
374	const struct mbuf *m;
375	int off;
376	int proto;
377	void *arg;
378{
379	struct ip6_hdr ip6;
380	struct gif_softc *sc;
381	struct ifnet *ifp;
382
383	/* sanity check done in caller */
384	sc = (struct gif_softc *)arg;
385
386	/* LINTED const cast */
387	m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
388	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
389
390	return gif_validate6(&ip6, sc, ifp);
391}
392
393int
394in6_gif_attach(sc)
395	struct gif_softc *sc;
396{
397	sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
398	    (struct protosw *)&in6_gif_protosw, sc);
399	if (sc->encap_cookie6 == NULL)
400		return EEXIST;
401	return 0;
402}
403
404int
405in6_gif_detach(sc)
406	struct gif_softc *sc;
407{
408	int error;
409
410	error = encap_detach(sc->encap_cookie6);
411	if (error == 0)
412		sc->encap_cookie6 = NULL;
413	return error;
414}
415