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