xref: /freebsd/sys/netinet/in_gif.c (revision 3a92d97ff0f22d21608e1c19b83104c4937523b6)
1 /*	$KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $	*/
2 
3 /*-
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_mrouting.h"
36 #include "opt_inet.h"
37 #include "opt_inet6.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/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/protosw.h>
48 #include <sys/malloc.h>
49 
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <net/route.h>
53 #include <net/vnet.h>
54 
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
58 #include <netinet/ip_var.h>
59 #include <netinet/in_gif.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip_encap.h>
62 #include <netinet/ip_ecn.h>
63 
64 #ifdef INET6
65 #include <netinet/ip6.h>
66 #endif
67 
68 #ifdef MROUTING
69 #include <netinet/ip_mroute.h>
70 #endif /* MROUTING */
71 
72 #include <net/if_gif.h>
73 
74 static int gif_validate4(const struct ip *, struct gif_softc *,
75 	struct ifnet *);
76 
77 extern  struct domain inetdomain;
78 struct protosw in_gif_protosw = {
79 	.pr_type =		SOCK_RAW,
80 	.pr_domain =		&inetdomain,
81 	.pr_protocol =		0/* IPPROTO_IPV[46] */,
82 	.pr_flags =		PR_ATOMIC|PR_ADDR,
83 	.pr_input =		in_gif_input,
84 	.pr_output =		(pr_output_t*)rip_output,
85 	.pr_ctloutput =		rip_ctloutput,
86 	.pr_usrreqs =		&rip_usrreqs
87 };
88 
89 VNET_DEFINE(int, ip_gif_ttl) = GIF_TTL;
90 #define	V_ip_gif_ttl		VNET(ip_gif_ttl)
91 SYSCTL_VNET_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_RW,
92 	&VNET_NAME(ip_gif_ttl), 0, "");
93 
94 int
95 in_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
96 {
97 	struct gif_softc *sc = ifp->if_softc;
98 	struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
99 	struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
100 	struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
101 	struct ip iphdr;	/* capsule IP header, host byte ordered */
102 	struct etherip_header eiphdr;
103 	int error, len, proto;
104 	u_int8_t tos;
105 
106 	GIF_LOCK_ASSERT(sc);
107 
108 	if (sin_src == NULL || sin_dst == NULL ||
109 	    sin_src->sin_family != AF_INET ||
110 	    sin_dst->sin_family != AF_INET) {
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 		tos = ip->ip_tos;
129 		break;
130 	    }
131 #endif /* INET */
132 #ifdef INET6
133 	case AF_INET6:
134 	    {
135 		struct ip6_hdr *ip6;
136 		proto = IPPROTO_IPV6;
137 		if (m->m_len < sizeof(*ip6)) {
138 			m = m_pullup(m, sizeof(*ip6));
139 			if (!m)
140 				return ENOBUFS;
141 		}
142 		ip6 = mtod(m, struct ip6_hdr *);
143 		tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
144 		break;
145 	    }
146 #endif /* INET6 */
147 	case AF_LINK:
148  		proto = IPPROTO_ETHERIP;
149 
150 		/*
151 		 * GIF_SEND_REVETHIP (disabled by default) intentionally
152 		 * sends an EtherIP packet with revered version field in
153 		 * the header.  This is a knob for backward compatibility
154 		 * with FreeBSD 7.2R or prior.
155 		 */
156 		if ((sc->gif_options & GIF_SEND_REVETHIP)) {
157  			eiphdr.eip_ver = 0;
158  			eiphdr.eip_resvl = ETHERIP_VERSION;
159  			eiphdr.eip_resvh = 0;
160 		} else {
161  			eiphdr.eip_ver = ETHERIP_VERSION;
162  			eiphdr.eip_resvl = 0;
163  			eiphdr.eip_resvh = 0;
164 		}
165  		/* prepend Ethernet-in-IP header */
166  		M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT);
167  		if (m && m->m_len < sizeof(struct etherip_header))
168  			m = m_pullup(m, sizeof(struct etherip_header));
169  		if (m == NULL)
170  			return ENOBUFS;
171  		bcopy(&eiphdr, mtod(m, struct etherip_header *),
172 		    sizeof(struct etherip_header));
173 		break;
174 
175 	default:
176 #ifdef DEBUG
177 		printf("in_gif_output: warning: unknown family %d passed\n",
178 			family);
179 #endif
180 		m_freem(m);
181 		return EAFNOSUPPORT;
182 	}
183 
184 	bzero(&iphdr, sizeof(iphdr));
185 	iphdr.ip_src = sin_src->sin_addr;
186 	/* bidirectional configured tunnel mode */
187 	if (sin_dst->sin_addr.s_addr != INADDR_ANY)
188 		iphdr.ip_dst = sin_dst->sin_addr;
189 	else {
190 		m_freem(m);
191 		return ENETUNREACH;
192 	}
193 	iphdr.ip_p = proto;
194 	/* version will be set in ip_output() */
195 	iphdr.ip_ttl = V_ip_gif_ttl;
196 	iphdr.ip_len = htons(m->m_pkthdr.len + sizeof(struct ip));
197 	ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
198 		       &iphdr.ip_tos, &tos);
199 
200 	/* prepend new IP header */
201 	len = sizeof(struct ip);
202 #ifndef __NO_STRICT_ALIGNMENT
203 	if (family == AF_LINK)
204 		len += ETHERIP_ALIGN;
205 #endif
206 	M_PREPEND(m, len, M_NOWAIT);
207 	if (m != NULL && m->m_len < len)
208 		m = m_pullup(m, len);
209 	if (m == NULL) {
210 		printf("ENOBUFS in in_gif_output %d\n", __LINE__);
211 		return ENOBUFS;
212 	}
213 #ifndef __NO_STRICT_ALIGNMENT
214 	if (family == AF_LINK) {
215 		len = mtod(m, vm_offset_t) & 3;
216 		KASSERT(len == 0 || len == ETHERIP_ALIGN,
217 		    ("in_gif_output: unexpected misalignment"));
218 		m->m_data += len;
219 		m->m_len -= ETHERIP_ALIGN;
220 	}
221 #endif
222 	bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
223 
224 	M_SETFIB(m, sc->gif_fibnum);
225 
226 	if (dst->sin_family != sin_dst->sin_family ||
227 	    dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) {
228 		/* cache route doesn't match */
229 		bzero(dst, sizeof(*dst));
230 		dst->sin_family = sin_dst->sin_family;
231 		dst->sin_len = sizeof(struct sockaddr_in);
232 		dst->sin_addr = sin_dst->sin_addr;
233 		if (sc->gif_ro.ro_rt) {
234 			RTFREE(sc->gif_ro.ro_rt);
235 			sc->gif_ro.ro_rt = NULL;
236 		}
237 #if 0
238 		GIF2IFP(sc)->if_mtu = GIF_MTU;
239 #endif
240 	}
241 
242 	if (sc->gif_ro.ro_rt == NULL) {
243 		in_rtalloc_ign(&sc->gif_ro, 0, sc->gif_fibnum);
244 		if (sc->gif_ro.ro_rt == NULL) {
245 			m_freem(m);
246 			return ENETUNREACH;
247 		}
248 
249 		/* if it constitutes infinite encapsulation, punt. */
250 		if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
251 			m_freem(m);
252 			return ENETUNREACH;	/* XXX */
253 		}
254 #if 0
255 		ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
256 			- sizeof(struct ip);
257 #endif
258 	}
259 
260 	error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
261 
262 	if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
263 	    sc->gif_ro.ro_rt != NULL) {
264 		RTFREE(sc->gif_ro.ro_rt);
265 		sc->gif_ro.ro_rt = NULL;
266 	}
267 
268 	return (error);
269 }
270 
271 void
272 in_gif_input(struct mbuf *m, int off)
273 {
274 	struct ifnet *gifp = NULL;
275 	struct gif_softc *sc;
276 	struct ip *ip;
277 	int af;
278 	u_int8_t otos;
279 	int proto;
280 
281 	ip = mtod(m, struct ip *);
282 	proto = ip->ip_p;
283 
284 	sc = (struct gif_softc *)encap_getarg(m);
285 	if (sc == NULL) {
286 		m_freem(m);
287 		KMOD_IPSTAT_INC(ips_nogif);
288 		return;
289 	}
290 
291 	gifp = GIF2IFP(sc);
292 	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
293 		m_freem(m);
294 		KMOD_IPSTAT_INC(ips_nogif);
295 		return;
296 	}
297 
298 	otos = ip->ip_tos;
299 	m_adj(m, off);
300 
301 	switch (proto) {
302 #ifdef INET
303 	case IPPROTO_IPV4:
304 	    {
305 		struct ip *ip;
306 		af = AF_INET;
307 		if (m->m_len < sizeof(*ip)) {
308 			m = m_pullup(m, sizeof(*ip));
309 			if (!m)
310 				return;
311 		}
312 		ip = mtod(m, struct ip *);
313 		if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
314 				  ECN_ALLOWED : ECN_NOCARE,
315 				  &otos, &ip->ip_tos) == 0) {
316 			m_freem(m);
317 			return;
318 		}
319 		break;
320 	    }
321 #endif
322 #ifdef INET6
323 	case IPPROTO_IPV6:
324 	    {
325 		struct ip6_hdr *ip6;
326 		u_int8_t itos, oitos;
327 
328 		af = AF_INET6;
329 		if (m->m_len < sizeof(*ip6)) {
330 			m = m_pullup(m, sizeof(*ip6));
331 			if (!m)
332 				return;
333 		}
334 		ip6 = mtod(m, struct ip6_hdr *);
335 		itos = oitos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
336 		if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
337 				  ECN_ALLOWED : ECN_NOCARE,
338 				  &otos, &itos) == 0) {
339 			m_freem(m);
340 			return;
341 		}
342 		if (itos != oitos) {
343 			ip6->ip6_flow &= ~htonl(0xff << 20);
344 			ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
345 		}
346 		break;
347 	    }
348 #endif /* INET6 */
349  	case IPPROTO_ETHERIP:
350  		af = AF_LINK;
351  		break;
352 
353 	default:
354 		KMOD_IPSTAT_INC(ips_nogif);
355 		m_freem(m);
356 		return;
357 	}
358 	gif_input(m, af, gifp);
359 	return;
360 }
361 
362 /*
363  * validate outer address.
364  */
365 static int
366 gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
367 {
368 	struct sockaddr_in *src, *dst;
369 	struct in_ifaddr *ia4;
370 
371 	src = (struct sockaddr_in *)sc->gif_psrc;
372 	dst = (struct sockaddr_in *)sc->gif_pdst;
373 
374 	/* check for address match */
375 	if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
376 	    dst->sin_addr.s_addr != ip->ip_src.s_addr)
377 		return 0;
378 
379 	/* martian filters on outer source - NOT done in ip_input! */
380 	if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)))
381 		return 0;
382 	switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
383 	case 0: case 127: case 255:
384 		return 0;
385 	}
386 
387 	/* reject packets with broadcast on source */
388 	/* XXXRW: should use hash lists? */
389 	IN_IFADDR_RLOCK();
390 	TAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) {
391 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
392 			continue;
393 		if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
394 			IN_IFADDR_RUNLOCK();
395 			return 0;
396 		}
397 	}
398 	IN_IFADDR_RUNLOCK();
399 
400 	/* ingress filters on outer source */
401 	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
402 		struct sockaddr_in sin;
403 		struct rtentry *rt;
404 
405 		bzero(&sin, sizeof(sin));
406 		sin.sin_family = AF_INET;
407 		sin.sin_len = sizeof(struct sockaddr_in);
408 		sin.sin_addr = ip->ip_src;
409 		/* XXX MRT  check for the interface we would use on output */
410 		rt = in_rtalloc1((struct sockaddr *)&sin, 0,
411 		    0UL, sc->gif_fibnum);
412 		if (!rt || rt->rt_ifp != ifp) {
413 #if 0
414 			log(LOG_WARNING, "%s: packet from 0x%x dropped "
415 			    "due to ingress filter\n", if_name(GIF2IFP(sc)),
416 			    (u_int32_t)ntohl(sin.sin_addr.s_addr));
417 #endif
418 			if (rt)
419 				RTFREE_LOCKED(rt);
420 			return 0;
421 		}
422 		RTFREE_LOCKED(rt);
423 	}
424 
425 	return 32 * 2;
426 }
427 
428 /*
429  * we know that we are in IFF_UP, outer address available, and outer family
430  * matched the physical addr family.  see gif_encapcheck().
431  */
432 int
433 gif_encapcheck4(const struct mbuf *m, int off, int proto, void *arg)
434 {
435 	struct ip ip;
436 	struct gif_softc *sc;
437 	struct ifnet *ifp;
438 
439 	/* sanity check done in caller */
440 	sc = (struct gif_softc *)arg;
441 
442 	/* LINTED const cast */
443 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
444 	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
445 
446 	return gif_validate4(&ip, sc, ifp);
447 }
448 
449 int
450 in_gif_attach(struct gif_softc *sc)
451 {
452 	sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
453 	    &in_gif_protosw, sc);
454 	if (sc->encap_cookie4 == NULL)
455 		return EEXIST;
456 	return 0;
457 }
458 
459 int
460 in_gif_detach(struct gif_softc *sc)
461 {
462 	int error;
463 
464 	error = encap_detach(sc->encap_cookie4);
465 	if (error == 0)
466 		sc->encap_cookie4 = NULL;
467 	return error;
468 }
469