in6_gif.c revision 105293
1/*	$FreeBSD: head/sys/netinet6/in6_gif.c 105293 2002-10-16 19:49:37Z 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, rt)
87	struct ifnet *ifp;
88	int family; /* family of the packet to be encapsulate. */
89	struct mbuf *m;
90	struct rtentry *rt;
91{
92	struct gif_softc *sc = (struct gif_softc*)ifp;
93	struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
94	struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
95	struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
96	struct ip6_hdr *ip6;
97	int proto;
98	u_int8_t itos, otos;
99
100	if (sin6_src == NULL || sin6_dst == NULL ||
101	    sin6_src->sin6_family != AF_INET6 ||
102	    sin6_dst->sin6_family != AF_INET6) {
103		m_freem(m);
104		return EAFNOSUPPORT;
105	}
106
107	switch (family) {
108#ifdef INET
109	case AF_INET:
110	    {
111		struct ip *ip;
112
113		proto = IPPROTO_IPV4;
114		if (m->m_len < sizeof(*ip)) {
115			m = m_pullup(m, sizeof(*ip));
116			if (!m)
117				return ENOBUFS;
118		}
119		ip = mtod(m, struct ip *);
120		itos = ip->ip_tos;
121		break;
122	    }
123#endif
124#ifdef INET6
125	case AF_INET6:
126	    {
127		struct ip6_hdr *ip6;
128		proto = IPPROTO_IPV6;
129		if (m->m_len < sizeof(*ip6)) {
130			m = m_pullup(m, sizeof(*ip6));
131			if (!m)
132				return ENOBUFS;
133		}
134		ip6 = mtod(m, struct ip6_hdr *);
135		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
136		break;
137	    }
138#endif
139	default:
140#ifdef DEBUG
141		printf("in6_gif_output: warning: unknown family %d passed\n",
142			family);
143#endif
144		m_freem(m);
145		return EAFNOSUPPORT;
146	}
147
148	/* prepend new IP header */
149	M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
150	if (m && m->m_len < sizeof(struct ip6_hdr))
151		m = m_pullup(m, sizeof(struct ip6_hdr));
152	if (m == NULL) {
153		printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
154		return ENOBUFS;
155	}
156
157	ip6 = mtod(m, struct ip6_hdr *);
158	ip6->ip6_flow	= 0;
159	ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
160	ip6->ip6_vfc	|= IPV6_VERSION;
161	ip6->ip6_plen	= htons((u_short)m->m_pkthdr.len);
162	ip6->ip6_nxt	= proto;
163	ip6->ip6_hlim	= ip6_gif_hlim;
164	ip6->ip6_src	= sin6_src->sin6_addr;
165	/* bidirectional configured tunnel mode */
166	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
167		ip6->ip6_dst = sin6_dst->sin6_addr;
168	else  {
169		m_freem(m);
170		return ENETUNREACH;
171	}
172	if (ifp->if_flags & IFF_LINK1)
173		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
174	else
175		ip_ecn_ingress(ECN_NOCARE, &otos, &itos);
176	ip6->ip6_flow &= ~ntohl(0xff00000);
177	ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
178
179	if (dst->sin6_family != sin6_dst->sin6_family ||
180	     !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
181		/* cache route doesn't match */
182		bzero(dst, sizeof(*dst));
183		dst->sin6_family = sin6_dst->sin6_family;
184		dst->sin6_len = sizeof(struct sockaddr_in6);
185		dst->sin6_addr = sin6_dst->sin6_addr;
186		if (sc->gif_ro6.ro_rt) {
187			RTFREE(sc->gif_ro6.ro_rt);
188			sc->gif_ro6.ro_rt = NULL;
189		}
190#if 0
191		sc->gif_if.if_mtu = GIF_MTU;
192#endif
193	}
194
195	if (sc->gif_ro6.ro_rt == NULL) {
196		rtalloc((struct route *)&sc->gif_ro6);
197		if (sc->gif_ro6.ro_rt == NULL) {
198			m_freem(m);
199			return ENETUNREACH;
200		}
201
202		/* if it constitutes infinite encapsulation, punt. */
203		if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
204			m_freem(m);
205			return ENETUNREACH;	/*XXX*/
206		}
207#if 0
208		ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
209			- sizeof(struct ip6_hdr);
210#endif
211	}
212
213#ifdef IPV6_MINMTU
214	/*
215	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
216	 * it is too painful to ask for resend of inner packet, to achieve
217	 * path MTU discovery for encapsulated packets.
218	 */
219	return(ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL));
220#else
221	return(ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL));
222#endif
223}
224
225int in6_gif_input(mp, offp, proto)
226	struct mbuf **mp;
227	int *offp, proto;
228{
229	struct mbuf *m = *mp;
230	struct ifnet *gifp = NULL;
231	struct ip6_hdr *ip6;
232	int af = 0;
233	u_int32_t otos;
234
235	ip6 = mtod(m, struct ip6_hdr *);
236
237	gifp = (struct ifnet *)encap_getarg(m);
238
239	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
240		m_freem(m);
241		ip6stat.ip6s_nogif++;
242		return IPPROTO_DONE;
243	}
244
245	otos = ip6->ip6_flow;
246	m_adj(m, *offp);
247
248	switch (proto) {
249#ifdef INET
250	case IPPROTO_IPV4:
251	    {
252		struct ip *ip;
253		u_int8_t otos8;
254		af = AF_INET;
255		otos8 = (ntohl(otos) >> 20) & 0xff;
256		if (m->m_len < sizeof(*ip)) {
257			m = m_pullup(m, sizeof(*ip));
258			if (!m)
259				return IPPROTO_DONE;
260		}
261		ip = mtod(m, struct ip *);
262		if (gifp->if_flags & IFF_LINK1)
263			ip_ecn_egress(ECN_ALLOWED, &otos8, &ip->ip_tos);
264		else
265			ip_ecn_egress(ECN_NOCARE, &otos8, &ip->ip_tos);
266		break;
267	    }
268#endif /* INET */
269#ifdef INET6
270	case IPPROTO_IPV6:
271	    {
272		struct ip6_hdr *ip6;
273		af = AF_INET6;
274		if (m->m_len < sizeof(*ip6)) {
275			m = m_pullup(m, sizeof(*ip6));
276			if (!m)
277				return IPPROTO_DONE;
278		}
279		ip6 = mtod(m, struct ip6_hdr *);
280		if (gifp->if_flags & IFF_LINK1)
281			ip6_ecn_egress(ECN_ALLOWED, &otos, &ip6->ip6_flow);
282		else
283			ip6_ecn_egress(ECN_NOCARE, &otos, &ip6->ip6_flow);
284		break;
285	    }
286#endif
287	default:
288		ip6stat.ip6s_nogif++;
289		m_freem(m);
290		return IPPROTO_DONE;
291	}
292
293	gif_input(m, af, gifp);
294	return IPPROTO_DONE;
295}
296
297/*
298 * validate outer address.
299 */
300static int
301gif_validate6(ip6, sc, ifp)
302	const struct ip6_hdr *ip6;
303	struct gif_softc *sc;
304	struct ifnet *ifp;
305{
306	struct sockaddr_in6 *src, *dst;
307
308	src = (struct sockaddr_in6 *)sc->gif_psrc;
309	dst = (struct sockaddr_in6 *)sc->gif_pdst;
310
311	/*
312	 * Check for address match.  Note that the check is for an incoming
313	 * packet.  We should compare the *source* address in our configuration
314	 * and the *destination* address of the packet, and vice versa.
315	 */
316	if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
317	    !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
318		return 0;
319
320	/* martian filters on outer source - done in ip6_input */
321
322	/* ingress filters on outer source */
323	if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
324		struct sockaddr_in6 sin6;
325		struct rtentry *rt;
326
327		bzero(&sin6, sizeof(sin6));
328		sin6.sin6_family = AF_INET6;
329		sin6.sin6_len = sizeof(struct sockaddr_in6);
330		sin6.sin6_addr = ip6->ip6_src;
331#ifndef SCOPEDROUTING
332		sin6.sin6_scope_id = 0; /* XXX */
333#endif
334
335		rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
336		if (!rt || rt->rt_ifp != ifp) {
337#if 0
338			log(LOG_WARNING, "%s: packet from %s dropped "
339			    "due to ingress filter\n", if_name(&sc->gif_if),
340			    ip6_sprintf(&sin6.sin6_addr));
341#endif
342			if (rt)
343				rtfree(rt);
344			return 0;
345		}
346		rtfree(rt);
347	}
348
349	return 128 * 2;
350}
351
352/*
353 * we know that we are in IFF_UP, outer address available, and outer family
354 * matched the physical addr family.  see gif_encapcheck().
355 * sanity check for arg should have been done in the caller.
356 */
357int
358gif_encapcheck6(m, off, proto, arg)
359	const struct mbuf *m;
360	int off;
361	int proto;
362	void *arg;
363{
364	struct ip6_hdr ip6;
365	struct gif_softc *sc;
366	struct ifnet *ifp;
367
368	/* sanity check done in caller */
369	sc = (struct gif_softc *)arg;
370
371	/* LINTED const cast */
372	m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
373	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
374
375	return gif_validate6(&ip6, sc, ifp);
376}
377
378int
379in6_gif_attach(sc)
380	struct gif_softc *sc;
381{
382#ifndef USE_ENCAPCHECK
383	struct sockaddr_in6 mask6;
384
385	bzero(&mask6, sizeof(mask6));
386	mask6.sin6_len = sizeof(struct sockaddr_in6);
387	mask6.sin6_addr.s6_addr32[0] = mask6.sin6_addr.s6_addr32[1] =
388	    mask6.sin6_addr.s6_addr32[2] = mask6.sin6_addr.s6_addr32[3] = ~0;
389	mask6.sin6_scope_id = ~0;
390
391	if (!sc->gif_psrc || !sc->gif_pdst)
392		return EINVAL;
393	sc->encap_cookie6 = encap_attach(AF_INET6, -1, sc->gif_psrc,
394	    (struct sockaddr *)&mask6, sc->gif_pdst, (struct sockaddr *)&mask6,
395	    (struct protosw *)&in6_gif_protosw, sc);
396#else
397	sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
398	    (struct protosw *)&in6_gif_protosw, sc);
399#endif
400	if (sc->encap_cookie6 == NULL)
401		return EEXIST;
402	return 0;
403}
404
405int
406in6_gif_detach(sc)
407	struct gif_softc *sc;
408{
409	int error;
410
411	error = encap_detach(sc->encap_cookie6);
412	if (error == 0)
413		sc->encap_cookie6 = NULL;
414	return error;
415}
416