in6_gif.c revision 1.55
1/*	$NetBSD: in6_gif.c,v 1.55 2008/04/15 03:57:04 thorpej Exp $	*/
2/*	$KAME: in6_gif.c,v 1.62 2001/07/29 04:27:25 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 <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: in6_gif.c,v 1.55 2008/04/15 03:57:04 thorpej Exp $");
35
36#include "opt_inet.h"
37#include "opt_iso.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/socket.h>
42#include <sys/sockio.h>
43#include <sys/mbuf.h>
44#include <sys/errno.h>
45#include <sys/ioctl.h>
46#include <sys/queue.h>
47#include <sys/syslog.h>
48#include <sys/protosw.h>
49#include <sys/kernel.h>
50
51#include <net/if.h>
52#include <net/route.h>
53
54#include <netinet/in.h>
55#include <netinet/in_systm.h>
56#ifdef INET
57#include <netinet/ip.h>
58#endif
59#include <netinet/ip_encap.h>
60#ifdef INET6
61#include <netinet/ip6.h>
62#include <netinet6/ip6_var.h>
63#include <netinet6/ip6_private.h>
64#include <netinet6/in6_gif.h>
65#include <netinet6/in6_var.h>
66#endif
67#include <netinet6/ip6protosw.h>
68#include <netinet/ip_ecn.h>
69
70#include <net/if_gif.h>
71
72#include <net/net_osdep.h>
73
74static int gif_validate6 __P((const struct ip6_hdr *, struct gif_softc *,
75	struct ifnet *));
76
77int	ip6_gif_hlim = GIF_HLIM;
78
79extern struct domain inet6domain;
80const struct ip6protosw in6_gif_protosw =
81{ SOCK_RAW,	&inet6domain,	0/* IPPROTO_IPV[46] */,	PR_ATOMIC|PR_ADDR,
82  in6_gif_input, rip6_output,	in6_gif_ctlinput, rip6_ctloutput,
83  rip6_usrreq,
84  0,            0,              0,              0,
85};
86
87extern LIST_HEAD(, gif_softc) gif_softc_list;
88
89/*
90 * family - family of the packet to be encapsulate.
91 */
92
93int
94in6_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
95{
96	struct rtentry *rt;
97	struct gif_softc *sc = (struct gif_softc*)ifp;
98	struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
99	struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
100	struct ip6_hdr *ip6;
101	int proto, error;
102	u_int8_t itos, otos;
103	union {
104		struct sockaddr		dst;
105		struct sockaddr_in6	dst6;
106	} u;
107
108	if (sin6_src == NULL || sin6_dst == NULL ||
109	    sin6_src->sin6_family != AF_INET6 ||
110	    sin6_dst->sin6_family != AF_INET6) {
111		m_freem(m);
112		return EAFNOSUPPORT;
113	}
114
115	switch (family) {
116#ifdef INET
117	case AF_INET:
118	    {
119		struct ip *ip;
120
121		proto = IPPROTO_IPV4;
122		if (m->m_len < sizeof(*ip)) {
123			m = m_pullup(m, sizeof(*ip));
124			if (!m)
125				return ENOBUFS;
126		}
127		ip = mtod(m, struct ip *);
128		itos = ip->ip_tos;
129		break;
130	    }
131#endif
132#ifdef INET6
133	case AF_INET6:
134	    {
135		proto = IPPROTO_IPV6;
136		if (m->m_len < sizeof(*ip6)) {
137			m = m_pullup(m, sizeof(*ip6));
138			if (!m)
139				return ENOBUFS;
140		}
141		ip6 = mtod(m, struct ip6_hdr *);
142		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
143		break;
144	    }
145#endif
146#ifdef ISO
147	case AF_ISO:
148		proto = IPPROTO_EON;
149		itos = 0;
150		break;
151#endif
152	default:
153#ifdef DEBUG
154		printf("in6_gif_output: warning: unknown family %d passed\n",
155			family);
156#endif
157		m_freem(m);
158		return EAFNOSUPPORT;
159	}
160
161	/* prepend new IP header */
162	M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
163	if (m && m->m_len < sizeof(struct ip6_hdr))
164		m = m_pullup(m, sizeof(struct ip6_hdr));
165	if (m == NULL)
166		return ENOBUFS;
167
168	ip6 = mtod(m, struct ip6_hdr *);
169	ip6->ip6_flow	= 0;
170	ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
171	ip6->ip6_vfc	|= IPV6_VERSION;
172#if 0	/* ip6->ip6_plen will be filled by ip6_output */
173	ip6->ip6_plen	= htons((u_int16_t)m->m_pkthdr.len);
174#endif
175	ip6->ip6_nxt	= proto;
176	ip6->ip6_hlim	= ip6_gif_hlim;
177	ip6->ip6_src	= sin6_src->sin6_addr;
178	/* bidirectional configured tunnel mode */
179	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
180		ip6->ip6_dst = sin6_dst->sin6_addr;
181	else  {
182		m_freem(m);
183		return ENETUNREACH;
184	}
185	if (ifp->if_flags & IFF_LINK1)
186		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
187	else
188		ip_ecn_ingress(ECN_NOCARE, &otos, &itos);
189	ip6->ip6_flow &= ~ntohl(0xff00000);
190	ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
191
192	sockaddr_in6_init(&u.dst6, &sin6_dst->sin6_addr, 0, 0, 0);
193	if ((rt = rtcache_lookup(&sc->gif_ro, &u.dst)) == NULL) {
194		m_freem(m);
195		return ENETUNREACH;
196	}
197
198	/* If the route constitutes infinite encapsulation, punt. */
199	if (rt->rt_ifp == ifp) {
200		m_freem(m);
201		return ENETUNREACH;	/* XXX */
202	}
203
204#ifdef IPV6_MINMTU
205	/*
206	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
207	 * it is too painful to ask for resend of inner packet, to achieve
208	 * path MTU discovery for encapsulated packets.
209	 */
210	error = ip6_output(m, 0, &sc->gif_ro, IPV6_MINMTU,
211		    (struct ip6_moptions *)NULL, (struct socket *)NULL, NULL);
212#else
213	error = ip6_output(m, 0, &sc->gif_ro, 0,
214		    (struct ip6_moptions *)NULL, (struct socket *)NULL, NULL);
215#endif
216
217	return (error);
218}
219
220int in6_gif_input(struct mbuf **mp, int *offp, int proto)
221{
222	struct mbuf *m = *mp;
223	struct ifnet *gifp = NULL;
224	struct ip6_hdr *ip6;
225	int af = 0;
226	u_int32_t otos;
227
228	ip6 = mtod(m, struct ip6_hdr *);
229
230	gifp = (struct ifnet *)encap_getarg(m);
231
232	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
233		m_freem(m);
234		IP6_STATINC(IP6_STAT_NOGIF);
235		return IPPROTO_DONE;
236	}
237#ifndef GIF_ENCAPCHECK
238	if (!gif_validate6(ip6, (struct gif_softc *)gifp, m->m_pkthdr.rcvif)) {
239		m_freem(m);
240		IP6_STATINC(IP6_STAT_NOGIF);
241		return IPPROTO_DONE;
242	}
243#endif
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 *ip6x;
273		af = AF_INET6;
274		if (m->m_len < sizeof(*ip6x)) {
275			m = m_pullup(m, sizeof(*ip6x));
276			if (!m)
277				return IPPROTO_DONE;
278		}
279		ip6x = mtod(m, struct ip6_hdr *);
280		if (gifp->if_flags & IFF_LINK1)
281			ip6_ecn_egress(ECN_ALLOWED, &otos, &ip6x->ip6_flow);
282		else
283			ip6_ecn_egress(ECN_NOCARE, &otos, &ip6x->ip6_flow);
284		break;
285	    }
286#endif
287#ifdef ISO
288	case IPPROTO_EON:
289		af = AF_ISO;
290		break;
291#endif
292	default:
293		IP6_STATINC(IP6_STAT_NOGIF);
294		m_freem(m);
295		return IPPROTO_DONE;
296	}
297
298	gif_input(m, af, gifp);
299	return IPPROTO_DONE;
300}
301
302/*
303 * validate outer address.
304 */
305static int
306gif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
307	struct ifnet *ifp)
308{
309	const struct sockaddr_in6 *src, *dst;
310
311	src = (struct sockaddr_in6 *)sc->gif_psrc;
312	dst = (struct sockaddr_in6 *)sc->gif_pdst;
313
314	/* check for address match */
315	if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
316	    !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
317		return 0;
318
319	/* martian filters on outer source - done in ip6_input */
320
321	/* ingress filters on outer source */
322	if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
323		struct sockaddr_in6 sin6;
324		struct rtentry *rt;
325
326		memset(&sin6, 0, sizeof(sin6));
327		sin6.sin6_family = AF_INET6;
328		sin6.sin6_len = sizeof(struct sockaddr_in6);
329		sin6.sin6_addr = ip6->ip6_src;
330		/* XXX scopeid */
331		rt = rtalloc1((struct sockaddr *)&sin6, 0);
332		if (!rt || rt->rt_ifp != ifp) {
333#if 0
334			log(LOG_WARNING, "%s: packet from %s dropped "
335			    "due to ingress filter\n", if_name(&sc->gif_if),
336			    ip6_sprintf(&sin6.sin6_addr));
337#endif
338			if (rt)
339				rtfree(rt);
340			return 0;
341		}
342		rtfree(rt);
343	}
344
345	return 128 * 2;
346}
347
348#ifdef GIF_ENCAPCHECK
349/*
350 * we know that we are in IFF_UP, outer address available, and outer family
351 * matched the physical addr family.  see gif_encapcheck().
352 */
353int
354gif_encapcheck6(struct mbuf *m, int off, int proto, void *arg)
355{
356	struct ip6_hdr ip6;
357	struct gif_softc *sc;
358	struct ifnet *ifp;
359
360	/* sanity check done in caller */
361	sc = (struct gif_softc *)arg;
362
363	m_copydata(m, 0, sizeof(ip6), (void *)&ip6);
364	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
365
366	return gif_validate6(&ip6, sc, ifp);
367}
368#endif
369
370int
371in6_gif_attach(struct gif_softc *sc)
372{
373#ifndef GIF_ENCAPCHECK
374	struct sockaddr_in6 mask6;
375
376	bzero(&mask6, sizeof(mask6));
377	mask6.sin6_len = sizeof(struct sockaddr_in6);
378	mask6.sin6_addr.s6_addr32[0] = mask6.sin6_addr.s6_addr32[1] =
379	    mask6.sin6_addr.s6_addr32[2] = mask6.sin6_addr.s6_addr32[3] = ~0;
380
381	if (!sc->gif_psrc || !sc->gif_pdst)
382		return EINVAL;
383	sc->encap_cookie6 = encap_attach(AF_INET6, -1, sc->gif_psrc,
384	    (struct sockaddr *)&mask6, sc->gif_pdst, (struct sockaddr *)&mask6,
385	    (const void *)&in6_gif_protosw, sc);
386#else
387	sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
388	    (struct protosw *)&in6_gif_protosw, sc);
389#endif
390	if (sc->encap_cookie6 == NULL)
391		return EEXIST;
392	return 0;
393}
394
395int
396in6_gif_detach(struct gif_softc *sc)
397{
398	int error;
399
400	error = encap_detach(sc->encap_cookie6);
401	if (error == 0)
402		sc->encap_cookie6 = NULL;
403
404	rtcache_free(&sc->gif_ro);
405
406	return error;
407}
408
409void
410in6_gif_ctlinput(int cmd, const struct sockaddr *sa, void *d)
411{
412	struct gif_softc *sc;
413	struct ip6ctlparam *ip6cp = NULL;
414	struct ip6_hdr *ip6;
415	const struct sockaddr_in6 *dst6;
416
417	if (sa->sa_family != AF_INET6 ||
418	    sa->sa_len != sizeof(struct sockaddr_in6))
419		return;
420
421	if ((unsigned)cmd >= PRC_NCMDS)
422		return;
423	if (cmd == PRC_HOSTDEAD)
424		d = NULL;
425	else if (inet6ctlerrmap[cmd] == 0)
426		return;
427
428	/* if the parameter is from icmp6, decode it. */
429	if (d != NULL) {
430		ip6cp = (struct ip6ctlparam *)d;
431		ip6 = ip6cp->ip6c_ip6;
432	} else {
433		ip6 = NULL;
434	}
435
436	if (!ip6)
437		return;
438
439	/*
440	 * for now we don't care which type it was, just flush the route cache.
441	 * XXX slow.  sc (or sc->encap_cookie6) should be passed from
442	 * ip_encap.c.
443	 */
444	LIST_FOREACH(sc, &gif_softc_list, gif_list) {
445		if ((sc->gif_if.if_flags & IFF_RUNNING) == 0)
446			continue;
447		if (sc->gif_psrc->sa_family != AF_INET6)
448			continue;
449
450		dst6 = satocsin6(rtcache_getdst(&sc->gif_ro));
451		/* XXX scope */
452		if (dst6 == NULL)
453			;
454		else if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst6->sin6_addr))
455			rtcache_free(&sc->gif_ro);
456	}
457}
458