xref: /netbsd/sys/netinet6/in6_gif.c (revision c4a72b64)
1 /*	$NetBSD: in6_gif.c,v 1.33 2002/11/25 02:04:23 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.33 2002/11/25 02:04:23 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 
50 #include <net/if.h>
51 #include <net/route.h>
52 
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #ifdef INET
56 #include <netinet/ip.h>
57 #endif
58 #include <netinet/ip_encap.h>
59 #ifdef INET6
60 #include <netinet/ip6.h>
61 #include <netinet6/ip6_var.h>
62 #include <netinet6/in6_gif.h>
63 #include <netinet6/in6_var.h>
64 #endif
65 #include <netinet6/ip6protosw.h>
66 #include <netinet/ip_ecn.h>
67 
68 #include <net/if_gif.h>
69 
70 #include <net/net_osdep.h>
71 
72 static int gif_validate6 __P((const struct ip6_hdr *, struct gif_softc *,
73 	struct ifnet *));
74 
75 int	ip6_gif_hlim = GIF_HLIM;
76 
77 extern struct domain inet6domain;
78 struct ip6protosw in6_gif_protosw =
79 { SOCK_RAW,	&inet6domain,	0/* IPPROTO_IPV[46] */,	PR_ATOMIC|PR_ADDR,
80   in6_gif_input, rip6_output,	in6_gif_ctlinput, rip6_ctloutput,
81   rip6_usrreq,
82   0,            0,              0,              0,
83 };
84 
85 extern LIST_HEAD(, gif_softc) gif_softc_list;
86 
87 int
88 in6_gif_output(ifp, family, m)
89 	struct ifnet *ifp;
90 	int family; /* family of the packet to be encapsulate. */
91 	struct mbuf *m;
92 {
93 	struct gif_softc *sc = (struct gif_softc*)ifp;
94 	struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
95 	struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
96 	struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
97 	struct ip6_hdr *ip6;
98 	int proto, error;
99 	u_int8_t itos, otos;
100 
101 	if (sin6_src == NULL || sin6_dst == NULL ||
102 	    sin6_src->sin6_family != AF_INET6 ||
103 	    sin6_dst->sin6_family != AF_INET6) {
104 		m_freem(m);
105 		return EAFNOSUPPORT;
106 	}
107 
108 	switch (family) {
109 #ifdef INET
110 	case AF_INET:
111 	    {
112 		struct ip *ip;
113 
114 		proto = IPPROTO_IPV4;
115 		if (m->m_len < sizeof(*ip)) {
116 			m = m_pullup(m, sizeof(*ip));
117 			if (!m)
118 				return ENOBUFS;
119 		}
120 		ip = mtod(m, struct ip *);
121 		itos = ip->ip_tos;
122 		break;
123 	    }
124 #endif
125 #ifdef INET6
126 	case AF_INET6:
127 	    {
128 		struct ip6_hdr *ip6;
129 		proto = IPPROTO_IPV6;
130 		if (m->m_len < sizeof(*ip6)) {
131 			m = m_pullup(m, sizeof(*ip6));
132 			if (!m)
133 				return ENOBUFS;
134 		}
135 		ip6 = mtod(m, struct ip6_hdr *);
136 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
137 		break;
138 	    }
139 #endif
140 #ifdef ISO
141 	case AF_ISO:
142 		proto = IPPROTO_EON;
143 		itos = 0;
144 		break;
145 #endif
146 	default:
147 #ifdef DEBUG
148 		printf("in6_gif_output: warning: unknown family %d passed\n",
149 			family);
150 #endif
151 		m_freem(m);
152 		return EAFNOSUPPORT;
153 	}
154 
155 	/* prepend new IP header */
156 	M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
157 	if (m && m->m_len < sizeof(struct ip6_hdr))
158 		m = m_pullup(m, sizeof(struct ip6_hdr));
159 	if (m == NULL)
160 		return ENOBUFS;
161 
162 	ip6 = mtod(m, struct ip6_hdr *);
163 	ip6->ip6_flow	= 0;
164 	ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
165 	ip6->ip6_vfc	|= IPV6_VERSION;
166 	ip6->ip6_plen	= htons((u_short)m->m_pkthdr.len);
167 	ip6->ip6_nxt	= proto;
168 	ip6->ip6_hlim	= ip6_gif_hlim;
169 	ip6->ip6_src	= sin6_src->sin6_addr;
170 	/* bidirectional configured tunnel mode */
171 	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
172 		ip6->ip6_dst = sin6_dst->sin6_addr;
173 	else  {
174 		m_freem(m);
175 		return ENETUNREACH;
176 	}
177 	if (ifp->if_flags & IFF_LINK1)
178 		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
179 	else
180 		ip_ecn_ingress(ECN_NOCARE, &otos, &itos);
181 	ip6->ip6_flow &= ~ntohl(0xff00000);
182 	ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
183 
184 	if (dst->sin6_family != sin6_dst->sin6_family ||
185 	     !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
186 		/* cache route doesn't match */
187 		bzero(dst, sizeof(*dst));
188 		dst->sin6_family = sin6_dst->sin6_family;
189 		dst->sin6_len = sizeof(struct sockaddr_in6);
190 		dst->sin6_addr = sin6_dst->sin6_addr;
191 		if (sc->gif_ro6.ro_rt) {
192 			RTFREE(sc->gif_ro6.ro_rt);
193 			sc->gif_ro6.ro_rt = NULL;
194 		}
195 	}
196 
197 	if (sc->gif_ro6.ro_rt == NULL) {
198 		rtalloc((struct route *)&sc->gif_ro6);
199 		if (sc->gif_ro6.ro_rt == NULL) {
200 			m_freem(m);
201 			return ENETUNREACH;
202 		}
203 
204 		/* if it constitutes infinite encapsulation, punt. */
205 		if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
206 			m_freem(m);
207 			return ENETUNREACH;	/* XXX */
208 		}
209 	}
210 
211 #ifdef IPV6_MINMTU
212 	/*
213 	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
214 	 * it is too painful to ask for resend of inner packet, to achieve
215 	 * path MTU discovery for encapsulated packets.
216 	 */
217 	error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL);
218 #else
219 	error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL);
220 #endif
221 
222 	return (error);
223 }
224 
225 int 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 #ifndef GIF_ENCAPCHECK
245 	if (!gif_validate6(ip6, (struct gif_softc *)gifp, m->m_pkthdr.rcvif)) {
246 		m_freem(m);
247 		ip6stat.ip6s_nogif++;
248 		return IPPROTO_DONE;
249 	}
250 #endif
251 
252 	otos = ip6->ip6_flow;
253 	m_adj(m, *offp);
254 
255 	switch (proto) {
256 #ifdef INET
257 	case IPPROTO_IPV4:
258 	    {
259 		struct ip *ip;
260 		u_int8_t otos8;
261 		af = AF_INET;
262 		otos8 = (ntohl(otos) >> 20) & 0xff;
263 		if (m->m_len < sizeof(*ip)) {
264 			m = m_pullup(m, sizeof(*ip));
265 			if (!m)
266 				return IPPROTO_DONE;
267 		}
268 		ip = mtod(m, struct ip *);
269 		if (gifp->if_flags & IFF_LINK1)
270 			ip_ecn_egress(ECN_ALLOWED, &otos8, &ip->ip_tos);
271 		else
272 			ip_ecn_egress(ECN_NOCARE, &otos8, &ip->ip_tos);
273 		break;
274 	    }
275 #endif /* INET */
276 #ifdef INET6
277 	case IPPROTO_IPV6:
278 	    {
279 		struct ip6_hdr *ip6;
280 		af = AF_INET6;
281 		if (m->m_len < sizeof(*ip6)) {
282 			m = m_pullup(m, sizeof(*ip6));
283 			if (!m)
284 				return IPPROTO_DONE;
285 		}
286 		ip6 = mtod(m, struct ip6_hdr *);
287 		if (gifp->if_flags & IFF_LINK1)
288 			ip6_ecn_egress(ECN_ALLOWED, &otos, &ip6->ip6_flow);
289 		else
290 			ip6_ecn_egress(ECN_NOCARE, &otos, &ip6->ip6_flow);
291 		break;
292 	    }
293 #endif
294 #ifdef ISO
295 	case IPPROTO_EON:
296 		af = AF_ISO;
297 		break;
298 #endif
299 	default:
300 		ip6stat.ip6s_nogif++;
301 		m_freem(m);
302 		return IPPROTO_DONE;
303 	}
304 
305 	gif_input(m, af, gifp);
306 	return IPPROTO_DONE;
307 }
308 
309 /*
310  * validate outer address.
311  */
312 static int
313 gif_validate6(ip6, sc, ifp)
314 	const struct ip6_hdr *ip6;
315 	struct gif_softc *sc;
316 	struct ifnet *ifp;
317 {
318 	struct sockaddr_in6 *src, *dst;
319 
320 	src = (struct sockaddr_in6 *)sc->gif_psrc;
321 	dst = (struct sockaddr_in6 *)sc->gif_pdst;
322 
323 	/* check for address match */
324 	if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
325 	    !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
326 		return 0;
327 
328 	/* martian filters on outer source - done in ip6_input */
329 
330 	/* ingress filters on outer source */
331 	if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
332 		struct sockaddr_in6 sin6;
333 		struct rtentry *rt;
334 
335 		bzero(&sin6, sizeof(sin6));
336 		sin6.sin6_family = AF_INET6;
337 		sin6.sin6_len = sizeof(struct sockaddr_in6);
338 		sin6.sin6_addr = ip6->ip6_src;
339 		/* XXX scopeid */
340 		rt = rtalloc1((struct sockaddr *)&sin6, 0);
341 		if (!rt || rt->rt_ifp != ifp) {
342 #if 0
343 			log(LOG_WARNING, "%s: packet from %s dropped "
344 			    "due to ingress filter\n", if_name(&sc->gif_if),
345 			    ip6_sprintf(&sin6.sin6_addr));
346 #endif
347 			if (rt)
348 				rtfree(rt);
349 			return 0;
350 		}
351 		rtfree(rt);
352 	}
353 
354 	return 128 * 2;
355 }
356 
357 #ifdef GIF_ENCAPCHECK
358 /*
359  * we know that we are in IFF_UP, outer address available, and outer family
360  * matched the physical addr family.  see gif_encapcheck().
361  */
362 int
363 gif_encapcheck6(m, off, proto, arg)
364 	const struct mbuf *m;
365 	int off;
366 	int proto;
367 	void *arg;
368 {
369 	struct ip6_hdr ip6;
370 	struct gif_softc *sc;
371 	struct ifnet *ifp;
372 
373 	/* sanity check done in caller */
374 	sc = (struct gif_softc *)arg;
375 
376 	/* LINTED const cast */
377 	m_copydata((struct mbuf *)m, 0, sizeof(ip6), (caddr_t)&ip6);
378 	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
379 
380 	return gif_validate6(&ip6, sc, ifp);
381 }
382 #endif
383 
384 int
385 in6_gif_attach(sc)
386 	struct gif_softc *sc;
387 {
388 #ifndef GIF_ENCAPCHECK
389 	struct sockaddr_in6 mask6;
390 
391 	bzero(&mask6, sizeof(mask6));
392 	mask6.sin6_len = sizeof(struct sockaddr_in6);
393 	mask6.sin6_addr.s6_addr32[0] = mask6.sin6_addr.s6_addr32[1] =
394 	    mask6.sin6_addr.s6_addr32[2] = mask6.sin6_addr.s6_addr32[3] = ~0;
395 
396 	if (!sc->gif_psrc || !sc->gif_pdst)
397 		return EINVAL;
398 	sc->encap_cookie6 = encap_attach(AF_INET6, -1, sc->gif_psrc,
399 	    (struct sockaddr *)&mask6, sc->gif_pdst, (struct sockaddr *)&mask6,
400 	    (void *)&in6_gif_protosw, sc);
401 #else
402 	sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
403 	    (struct protosw *)&in6_gif_protosw, sc);
404 #endif
405 	if (sc->encap_cookie6 == NULL)
406 		return EEXIST;
407 	return 0;
408 }
409 
410 int
411 in6_gif_detach(sc)
412 	struct gif_softc *sc;
413 {
414 	int error;
415 
416 	error = encap_detach(sc->encap_cookie6);
417 	if (error == 0)
418 		sc->encap_cookie6 = NULL;
419 	return error;
420 }
421 
422 void
423 in6_gif_ctlinput(cmd, sa, d)
424 	int cmd;
425 	struct sockaddr *sa;
426 	void *d;
427 {
428 	struct gif_softc *sc;
429 	struct ip6ctlparam *ip6cp = NULL;
430 	struct mbuf *m;
431 	struct ip6_hdr *ip6;
432 	int off;
433 	void *cmdarg;
434 	const struct sockaddr_in6 *sa6_src = NULL;
435 	struct sockaddr_in6 *dst6;
436 
437 	if (sa->sa_family != AF_INET6 ||
438 	    sa->sa_len != sizeof(struct sockaddr_in6))
439 		return;
440 
441 	if ((unsigned)cmd >= PRC_NCMDS)
442 		return;
443 	if (cmd == PRC_HOSTDEAD)
444 		d = NULL;
445 	else if (inet6ctlerrmap[cmd] == 0)
446 		return;
447 
448 	/* if the parameter is from icmp6, decode it. */
449 	if (d != NULL) {
450 		ip6cp = (struct ip6ctlparam *)d;
451 		m = ip6cp->ip6c_m;
452 		ip6 = ip6cp->ip6c_ip6;
453 		off = ip6cp->ip6c_off;
454 		cmdarg = ip6cp->ip6c_cmdarg;
455 		sa6_src = ip6cp->ip6c_src;
456 	} else {
457 		m = NULL;
458 		ip6 = NULL;
459 		cmdarg = NULL;
460 		sa6_src = &sa6_any;
461 	}
462 
463 	if (!ip6)
464 		return;
465 
466 	/*
467 	 * for now we don't care which type it was, just flush the route cache.
468 	 * XXX slow.  sc (or sc->encap_cookie6) should be passed from
469 	 * ip_encap.c.
470 	 */
471 	for (sc = LIST_FIRST(&gif_softc_list); sc;
472 	     sc = LIST_NEXT(sc, gif_list)) {
473 		if ((sc->gif_if.if_flags & IFF_RUNNING) == 0)
474 			continue;
475 		if (sc->gif_psrc->sa_family != AF_INET6)
476 			continue;
477 		if (!sc->gif_ro6.ro_rt)
478 			continue;
479 
480 		dst6 = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
481 		/* XXX scope */
482 		if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst6->sin6_addr)) {
483 			/* flush route cache */
484 			RTFREE(sc->gif_ro6.ro_rt);
485 			sc->gif_ro6.ro_rt = NULL;
486 		}
487 	}
488 }
489