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