xref: /freebsd/sys/netinet6/in6_gif.c (revision 5861f9665471e98e544f6fa3ce73c4912229ff82)
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 #include <netinet6/vinet6.h>
70 #endif
71 
72 #include <net/if_gif.h>
73 
74 static int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
75 			 struct ifnet *);
76 
77 extern  struct domain inet6domain;
78 struct ip6protosw in6_gif_protosw = {
79 	.pr_type =	SOCK_RAW,
80 	.pr_domain =	&inet6domain,
81 	.pr_protocol =	0,			/* IPPROTO_IPV[46] */
82 	.pr_flags =	PR_ATOMIC|PR_ADDR,
83 	.pr_input =	in6_gif_input,
84 	.pr_output =	rip6_output,
85 	.pr_ctloutput =	rip6_ctloutput,
86 	.pr_usrreqs =	&rip6_usrreqs
87 };
88 
89 int
90 in6_gif_output(struct ifnet *ifp,
91     int family,			/* family of the packet to be encapsulate */
92     struct mbuf *m)
93 {
94 	INIT_VNET_GIF(ifp->if_vnet);
95 	struct gif_softc *sc = ifp->if_softc;
96 	struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
97 	struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
98 	struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
99 	struct ip6_hdr *ip6;
100 	struct etherip_header eiphdr;
101 	int error, len, proto;
102 	u_int8_t itos, otos;
103 
104 	GIF_LOCK_ASSERT(sc);
105 
106 	if (sin6_src == NULL || sin6_dst == NULL ||
107 	    sin6_src->sin6_family != AF_INET6 ||
108 	    sin6_dst->sin6_family != AF_INET6) {
109 		m_freem(m);
110 		return EAFNOSUPPORT;
111 	}
112 
113 	switch (family) {
114 #ifdef INET
115 	case AF_INET:
116 	    {
117 		struct ip *ip;
118 
119 		proto = IPPROTO_IPV4;
120 		if (m->m_len < sizeof(*ip)) {
121 			m = m_pullup(m, sizeof(*ip));
122 			if (!m)
123 				return ENOBUFS;
124 		}
125 		ip = mtod(m, struct ip *);
126 		itos = ip->ip_tos;
127 		break;
128 	    }
129 #endif
130 #ifdef INET6
131 	case AF_INET6:
132 	    {
133 		struct ip6_hdr *ip6;
134 		proto = IPPROTO_IPV6;
135 		if (m->m_len < sizeof(*ip6)) {
136 			m = m_pullup(m, sizeof(*ip6));
137 			if (!m)
138 				return ENOBUFS;
139 		}
140 		ip6 = mtod(m, struct ip6_hdr *);
141 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
142 		break;
143 	    }
144 #endif
145 	case AF_LINK:
146 		proto = IPPROTO_ETHERIP;
147 
148 		/*
149 		 * GIF_SEND_REVETHIP (disabled by default) intentionally
150 		 * sends an EtherIP packet with revered version field in
151 		 * the header.  This is a knob for backward compatibility
152 		 * with FreeBSD 7.2R or prior.
153 		 */
154 		if ((sc->gif_options & GIF_SEND_REVETHIP)) {
155 			eiphdr.eip_ver = 0;
156 			eiphdr.eip_resvl = ETHERIP_VERSION;
157 			eiphdr.eip_resvh = 0;
158 		} else {
159 			eiphdr.eip_ver = ETHERIP_VERSION;
160 			eiphdr.eip_resvl = 0;
161 			eiphdr.eip_resvh = 0;
162 		}
163 		/* prepend Ethernet-in-IP header */
164 		M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
165 		if (m && m->m_len < sizeof(struct etherip_header))
166 			m = m_pullup(m, sizeof(struct etherip_header));
167 		if (m == NULL)
168 			return ENOBUFS;
169 		bcopy(&eiphdr, mtod(m, struct etherip_header *),
170 		    sizeof(struct etherip_header));
171 		break;
172 
173 	default:
174 #ifdef DEBUG
175 		printf("in6_gif_output: warning: unknown family %d passed\n",
176 			family);
177 #endif
178 		m_freem(m);
179 		return EAFNOSUPPORT;
180 	}
181 
182 	/* prepend new IP header */
183 	len = sizeof(struct ip6_hdr);
184 #ifndef __NO_STRICT_ALIGNMENT
185 	if (family == AF_LINK)
186 		len += ETHERIP_ALIGN;
187 #endif
188 	M_PREPEND(m, len, M_DONTWAIT);
189 	if (m != NULL && m->m_len < len)
190 		m = m_pullup(m, len);
191 	if (m == NULL) {
192 		printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
193 		return ENOBUFS;
194 	}
195 #ifndef __NO_STRICT_ALIGNMENT
196 	if (family == AF_LINK) {
197 		len = mtod(m, vm_offset_t) & 3;
198 		KASSERT(len == 0 || len == ETHERIP_ALIGN,
199 		    ("in6_gif_output: unexpected misalignment"));
200 		m->m_data += len;
201 		m->m_len -= ETHERIP_ALIGN;
202 	}
203 #endif
204 
205 	ip6 = mtod(m, struct ip6_hdr *);
206 	ip6->ip6_flow	= 0;
207 	ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
208 	ip6->ip6_vfc	|= IPV6_VERSION;
209 	ip6->ip6_plen	= htons((u_short)m->m_pkthdr.len);
210 	ip6->ip6_nxt	= proto;
211 	ip6->ip6_hlim	= V_ip6_gif_hlim;
212 	ip6->ip6_src	= sin6_src->sin6_addr;
213 	/* bidirectional configured tunnel mode */
214 	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
215 		ip6->ip6_dst = sin6_dst->sin6_addr;
216 	else  {
217 		m_freem(m);
218 		return ENETUNREACH;
219 	}
220 	ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
221 		       &otos, &itos);
222 	ip6->ip6_flow &= ~htonl(0xff << 20);
223 	ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
224 
225 	if (dst->sin6_family != sin6_dst->sin6_family ||
226 	     !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
227 		/* cache route doesn't match */
228 		bzero(dst, sizeof(*dst));
229 		dst->sin6_family = sin6_dst->sin6_family;
230 		dst->sin6_len = sizeof(struct sockaddr_in6);
231 		dst->sin6_addr = sin6_dst->sin6_addr;
232 		if (sc->gif_ro6.ro_rt) {
233 			RTFREE(sc->gif_ro6.ro_rt);
234 			sc->gif_ro6.ro_rt = NULL;
235 		}
236 #if 0
237 		GIF2IFP(sc)->if_mtu = GIF_MTU;
238 #endif
239 	}
240 
241 	if (sc->gif_ro6.ro_rt == NULL) {
242 		rtalloc((struct route *)&sc->gif_ro6);
243 		if (sc->gif_ro6.ro_rt == NULL) {
244 			m_freem(m);
245 			return ENETUNREACH;
246 		}
247 
248 		/* if it constitutes infinite encapsulation, punt. */
249 		if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
250 			m_freem(m);
251 			return ENETUNREACH;	/*XXX*/
252 		}
253 #if 0
254 		ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
255 			- sizeof(struct ip6_hdr);
256 #endif
257 	}
258 
259 #ifdef IPV6_MINMTU
260 	/*
261 	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
262 	 * it is too painful to ask for resend of inner packet, to achieve
263 	 * path MTU discovery for encapsulated packets.
264 	 */
265 	error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL);
266 #else
267 	error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL);
268 #endif
269 
270 	if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
271 	    sc->gif_ro6.ro_rt != NULL) {
272 		RTFREE(sc->gif_ro6.ro_rt);
273 		sc->gif_ro6.ro_rt = NULL;
274 	}
275 
276 	return (error);
277 }
278 
279 int
280 in6_gif_input(struct mbuf **mp, int *offp, int proto)
281 {
282 	INIT_VNET_INET6(curvnet);
283 	struct mbuf *m = *mp;
284 	struct ifnet *gifp = NULL;
285 	struct gif_softc *sc;
286 	struct ip6_hdr *ip6;
287 	int af = 0;
288 	u_int32_t otos;
289 
290 	ip6 = mtod(m, struct ip6_hdr *);
291 
292 	sc = (struct gif_softc *)encap_getarg(m);
293 	if (sc == NULL) {
294 		m_freem(m);
295 		V_ip6stat.ip6s_nogif++;
296 		return IPPROTO_DONE;
297 	}
298 
299 	gifp = GIF2IFP(sc);
300 	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
301 		m_freem(m);
302 		V_ip6stat.ip6s_nogif++;
303 		return IPPROTO_DONE;
304 	}
305 
306 	otos = ip6->ip6_flow;
307 	m_adj(m, *offp);
308 
309 	switch (proto) {
310 #ifdef INET
311 	case IPPROTO_IPV4:
312 	    {
313 		struct ip *ip;
314 		u_int8_t otos8;
315 		af = AF_INET;
316 		otos8 = (ntohl(otos) >> 20) & 0xff;
317 		if (m->m_len < sizeof(*ip)) {
318 			m = m_pullup(m, sizeof(*ip));
319 			if (!m)
320 				return IPPROTO_DONE;
321 		}
322 		ip = mtod(m, struct ip *);
323 		if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
324 				  ECN_ALLOWED : ECN_NOCARE,
325 				  &otos8, &ip->ip_tos) == 0) {
326 			m_freem(m);
327 			return IPPROTO_DONE;
328 		}
329 		break;
330 	    }
331 #endif /* INET */
332 #ifdef INET6
333 	case IPPROTO_IPV6:
334 	    {
335 		struct ip6_hdr *ip6;
336 		af = AF_INET6;
337 		if (m->m_len < sizeof(*ip6)) {
338 			m = m_pullup(m, sizeof(*ip6));
339 			if (!m)
340 				return IPPROTO_DONE;
341 		}
342 		ip6 = mtod(m, struct ip6_hdr *);
343 		if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ?
344 				   ECN_ALLOWED : ECN_NOCARE,
345 				   &otos, &ip6->ip6_flow) == 0) {
346 			m_freem(m);
347 			return IPPROTO_DONE;
348 		}
349 		break;
350 	    }
351 #endif
352 	case IPPROTO_ETHERIP:
353 		af = AF_LINK;
354 		break;
355 
356 	default:
357 		V_ip6stat.ip6s_nogif++;
358 		m_freem(m);
359 		return IPPROTO_DONE;
360 	}
361 
362 	gif_input(m, af, gifp);
363 	return IPPROTO_DONE;
364 }
365 
366 /*
367  * validate outer address.
368  */
369 static int
370 gif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
371     struct ifnet *ifp)
372 {
373 	struct sockaddr_in6 *src, *dst;
374 
375 	src = (struct sockaddr_in6 *)sc->gif_psrc;
376 	dst = (struct sockaddr_in6 *)sc->gif_pdst;
377 
378 	/*
379 	 * Check for address match.  Note that the check is for an incoming
380 	 * packet.  We should compare the *source* address in our configuration
381 	 * and the *destination* address of the packet, and vice versa.
382 	 */
383 	if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
384 	    !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
385 		return 0;
386 
387 	/* martian filters on outer source - done in ip6_input */
388 
389 	/* ingress filters on outer source */
390 	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
391 		struct sockaddr_in6 sin6;
392 		struct rtentry *rt;
393 
394 		bzero(&sin6, sizeof(sin6));
395 		sin6.sin6_family = AF_INET6;
396 		sin6.sin6_len = sizeof(struct sockaddr_in6);
397 		sin6.sin6_addr = ip6->ip6_src;
398 		sin6.sin6_scope_id = 0; /* XXX */
399 
400 		rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
401 		if (!rt || rt->rt_ifp != ifp) {
402 #if 0
403 			char ip6buf[INET6_ADDRSTRLEN];
404 			log(LOG_WARNING, "%s: packet from %s dropped "
405 			    "due to ingress filter\n", if_name(GIF2IFP(sc)),
406 			    ip6_sprintf(ip6buf, &sin6.sin6_addr));
407 #endif
408 			if (rt)
409 				RTFREE_LOCKED(rt);
410 			return 0;
411 		}
412 		RTFREE_LOCKED(rt);
413 	}
414 
415 	return 128 * 2;
416 }
417 
418 /*
419  * we know that we are in IFF_UP, outer address available, and outer family
420  * matched the physical addr family.  see gif_encapcheck().
421  * sanity check for arg should have been done in the caller.
422  */
423 int
424 gif_encapcheck6(const struct mbuf *m, int off, int proto, void *arg)
425 {
426 	struct ip6_hdr ip6;
427 	struct gif_softc *sc;
428 	struct ifnet *ifp;
429 
430 	/* sanity check done in caller */
431 	sc = (struct gif_softc *)arg;
432 
433 	/* LINTED const cast */
434 	m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
435 	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
436 
437 	return gif_validate6(&ip6, sc, ifp);
438 }
439 
440 int
441 in6_gif_attach(struct gif_softc *sc)
442 {
443 	sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
444 	    (void *)&in6_gif_protosw, sc);
445 	if (sc->encap_cookie6 == NULL)
446 		return EEXIST;
447 	return 0;
448 }
449 
450 int
451 in6_gif_detach(struct gif_softc *sc)
452 {
453 	int error;
454 
455 	error = encap_detach(sc->encap_cookie6);
456 	if (error == 0)
457 		sc->encap_cookie6 = NULL;
458 	return error;
459 }
460