xref: /freebsd/sys/netinet/in_gif.c (revision 7850fa71f55a16f414bb21163d80a03a5ab34522)
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 #include <sys/vimage.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 #include <netinet/ip.h>
57 #include <netinet/ip_var.h>
58 #include <netinet/in_gif.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip_encap.h>
61 #include <netinet/ip_ecn.h>
62 #include <netinet/vinet.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 #ifdef VIMAGE_GLOBALS
90 extern int ip_gif_ttl;
91 #endif
92 SYSCTL_V_INT(V_NET, vnet_gif, _net_inet_ip, IPCTL_GIF_TTL, gifttl,
93 	CTLFLAG_RW, ip_gif_ttl,	0, "");
94 
95 int
96 in_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
97 {
98 	INIT_VNET_GIF(ifp->if_vnet);
99 	struct gif_softc *sc = ifp->if_softc;
100 	struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
101 	struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
102 	struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
103 	struct ip iphdr;	/* capsule IP header, host byte ordered */
104 	struct etherip_header eiphdr;
105 	int error, len, proto;
106 	u_int8_t tos;
107 
108 	GIF_LOCK_ASSERT(sc);
109 
110 	if (sin_src == NULL || sin_dst == NULL ||
111 	    sin_src->sin_family != AF_INET ||
112 	    sin_dst->sin_family != AF_INET) {
113 		m_freem(m);
114 		return EAFNOSUPPORT;
115 	}
116 
117 	switch (family) {
118 #ifdef INET
119 	case AF_INET:
120 	    {
121 		struct ip *ip;
122 
123 		proto = IPPROTO_IPV4;
124 		if (m->m_len < sizeof(*ip)) {
125 			m = m_pullup(m, sizeof(*ip));
126 			if (!m)
127 				return ENOBUFS;
128 		}
129 		ip = mtod(m, struct ip *);
130 		tos = ip->ip_tos;
131 		break;
132 	    }
133 #endif /* INET */
134 #ifdef INET6
135 	case AF_INET6:
136 	    {
137 		struct ip6_hdr *ip6;
138 		proto = IPPROTO_IPV6;
139 		if (m->m_len < sizeof(*ip6)) {
140 			m = m_pullup(m, sizeof(*ip6));
141 			if (!m)
142 				return ENOBUFS;
143 		}
144 		ip6 = mtod(m, struct ip6_hdr *);
145 		tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
146 		break;
147 	    }
148 #endif /* INET6 */
149 	case AF_LINK:
150  		proto = IPPROTO_ETHERIP;
151 
152 		/*
153 		 * GIF_SEND_REVETHIP (disabled by default) intentionally
154 		 * sends an EtherIP packet with revered version field in
155 		 * the header.  This is a knob for backward compatibility
156 		 * with FreeBSD 7.2R or prior.
157 		 */
158 		if ((sc->gif_options & GIF_SEND_REVETHIP)) {
159  			eiphdr.eip_ver = 0;
160  			eiphdr.eip_resvl = ETHERIP_VERSION;
161  			eiphdr.eip_resvh = 0;
162 		} else {
163  			eiphdr.eip_ver = ETHERIP_VERSION;
164  			eiphdr.eip_resvl = 0;
165  			eiphdr.eip_resvh = 0;
166 		}
167  		/* prepend Ethernet-in-IP header */
168  		M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
169  		if (m && m->m_len < sizeof(struct etherip_header))
170  			m = m_pullup(m, sizeof(struct etherip_header));
171  		if (m == NULL)
172  			return ENOBUFS;
173  		bcopy(&eiphdr, mtod(m, struct etherip_header *),
174 		    sizeof(struct etherip_header));
175 		break;
176 
177 	default:
178 #ifdef DEBUG
179 		printf("in_gif_output: warning: unknown family %d passed\n",
180 			family);
181 #endif
182 		m_freem(m);
183 		return EAFNOSUPPORT;
184 	}
185 
186 	bzero(&iphdr, sizeof(iphdr));
187 	iphdr.ip_src = sin_src->sin_addr;
188 	/* bidirectional configured tunnel mode */
189 	if (sin_dst->sin_addr.s_addr != INADDR_ANY)
190 		iphdr.ip_dst = sin_dst->sin_addr;
191 	else {
192 		m_freem(m);
193 		return ENETUNREACH;
194 	}
195 	iphdr.ip_p = proto;
196 	/* version will be set in ip_output() */
197 	iphdr.ip_ttl = V_ip_gif_ttl;
198 	iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip);
199 	ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
200 		       &iphdr.ip_tos, &tos);
201 
202 	/* prepend new IP header */
203 	len = sizeof(struct ip);
204 #ifndef __NO_STRICT_ALIGNMENT
205 	if (family == AF_LINK)
206 		len += ETHERIP_ALIGN;
207 #endif
208 	M_PREPEND(m, len, M_DONTWAIT);
209 	if (m != NULL && m->m_len < len)
210 		m = m_pullup(m, len);
211 	if (m == NULL) {
212 		printf("ENOBUFS in in_gif_output %d\n", __LINE__);
213 		return ENOBUFS;
214 	}
215 #ifndef __NO_STRICT_ALIGNMENT
216 	if (family == AF_LINK) {
217 		len = mtod(m, vm_offset_t) & 3;
218 		KASSERT(len == 0 || len == ETHERIP_ALIGN,
219 		    ("in_gif_output: unexpected misalignment"));
220 		m->m_data += len;
221 		m->m_len -= ETHERIP_ALIGN;
222 	}
223 #endif
224 	bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
225 
226 	M_SETFIB(m, sc->gif_fibnum);
227 
228 	if (dst->sin_family != sin_dst->sin_family ||
229 	    dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) {
230 		/* cache route doesn't match */
231 		bzero(dst, sizeof(*dst));
232 		dst->sin_family = sin_dst->sin_family;
233 		dst->sin_len = sizeof(struct sockaddr_in);
234 		dst->sin_addr = sin_dst->sin_addr;
235 		if (sc->gif_ro.ro_rt) {
236 			RTFREE(sc->gif_ro.ro_rt);
237 			sc->gif_ro.ro_rt = NULL;
238 		}
239 #if 0
240 		GIF2IFP(sc)->if_mtu = GIF_MTU;
241 #endif
242 	}
243 
244 	if (sc->gif_ro.ro_rt == NULL) {
245 		in_rtalloc_ign(&sc->gif_ro, 0, sc->gif_fibnum);
246 		if (sc->gif_ro.ro_rt == NULL) {
247 			m_freem(m);
248 			return ENETUNREACH;
249 		}
250 
251 		/* if it constitutes infinite encapsulation, punt. */
252 		if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
253 			m_freem(m);
254 			return ENETUNREACH;	/* XXX */
255 		}
256 #if 0
257 		ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
258 			- sizeof(struct ip);
259 #endif
260 	}
261 
262 	error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
263 
264 	if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
265 	    sc->gif_ro.ro_rt != NULL) {
266 		RTFREE(sc->gif_ro.ro_rt);
267 		sc->gif_ro.ro_rt = NULL;
268 	}
269 
270 	return (error);
271 }
272 
273 void
274 in_gif_input(struct mbuf *m, int off)
275 {
276 	INIT_VNET_INET(curvnet);
277 	struct ifnet *gifp = NULL;
278 	struct gif_softc *sc;
279 	struct ip *ip;
280 	int af;
281 	u_int8_t otos;
282 	int proto;
283 
284 	ip = mtod(m, struct ip *);
285 	proto = ip->ip_p;
286 
287 	sc = (struct gif_softc *)encap_getarg(m);
288 	if (sc == NULL) {
289 		m_freem(m);
290 		IPSTAT_INC(ips_nogif);
291 		return;
292 	}
293 
294 	gifp = GIF2IFP(sc);
295 	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
296 		m_freem(m);
297 		IPSTAT_INC(ips_nogif);
298 		return;
299 	}
300 
301 	otos = ip->ip_tos;
302 	m_adj(m, off);
303 
304 	switch (proto) {
305 #ifdef INET
306 	case IPPROTO_IPV4:
307 	    {
308 		struct ip *ip;
309 		af = AF_INET;
310 		if (m->m_len < sizeof(*ip)) {
311 			m = m_pullup(m, sizeof(*ip));
312 			if (!m)
313 				return;
314 		}
315 		ip = mtod(m, struct ip *);
316 		if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
317 				  ECN_ALLOWED : ECN_NOCARE,
318 				  &otos, &ip->ip_tos) == 0) {
319 			m_freem(m);
320 			return;
321 		}
322 		break;
323 	    }
324 #endif
325 #ifdef INET6
326 	case IPPROTO_IPV6:
327 	    {
328 		struct ip6_hdr *ip6;
329 		u_int8_t itos, oitos;
330 
331 		af = AF_INET6;
332 		if (m->m_len < sizeof(*ip6)) {
333 			m = m_pullup(m, sizeof(*ip6));
334 			if (!m)
335 				return;
336 		}
337 		ip6 = mtod(m, struct ip6_hdr *);
338 		itos = oitos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
339 		if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
340 				  ECN_ALLOWED : ECN_NOCARE,
341 				  &otos, &itos) == 0) {
342 			m_freem(m);
343 			return;
344 		}
345 		if (itos != oitos) {
346 			ip6->ip6_flow &= ~htonl(0xff << 20);
347 			ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
348 		}
349 		break;
350 	    }
351 #endif /* INET6 */
352  	case IPPROTO_ETHERIP:
353  		af = AF_LINK;
354  		break;
355 
356 	default:
357 		IPSTAT_INC(ips_nogif);
358 		m_freem(m);
359 		return;
360 	}
361 	gif_input(m, af, gifp);
362 	return;
363 }
364 
365 /*
366  * validate outer address.
367  */
368 static int
369 gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
370 {
371 	INIT_VNET_INET(curvnet);
372 	struct sockaddr_in *src, *dst;
373 	struct in_ifaddr *ia4;
374 
375 	src = (struct sockaddr_in *)sc->gif_psrc;
376 	dst = (struct sockaddr_in *)sc->gif_pdst;
377 
378 	/* check for address match */
379 	if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
380 	    dst->sin_addr.s_addr != ip->ip_src.s_addr)
381 		return 0;
382 
383 	/* martian filters on outer source - NOT done in ip_input! */
384 	if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)))
385 		return 0;
386 	switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
387 	case 0: case 127: case 255:
388 		return 0;
389 	}
390 
391 	/* reject packets with broadcast on source */
392 	/* XXXRW: should use hash lists? */
393 	IN_IFADDR_RLOCK();
394 	TAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) {
395 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
396 			continue;
397 		if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
398 			IN_IFADDR_RUNLOCK();
399 			return 0;
400 		}
401 	}
402 	IN_IFADDR_RUNLOCK();
403 
404 	/* ingress filters on outer source */
405 	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
406 		struct sockaddr_in sin;
407 		struct rtentry *rt;
408 
409 		bzero(&sin, sizeof(sin));
410 		sin.sin_family = AF_INET;
411 		sin.sin_len = sizeof(struct sockaddr_in);
412 		sin.sin_addr = ip->ip_src;
413 		/* XXX MRT  check for the interface we would use on output */
414 		rt = in_rtalloc1((struct sockaddr *)&sin, 0,
415 		    0UL, sc->gif_fibnum);
416 		if (!rt || rt->rt_ifp != ifp) {
417 #if 0
418 			log(LOG_WARNING, "%s: packet from 0x%x dropped "
419 			    "due to ingress filter\n", if_name(GIF2IFP(sc)),
420 			    (u_int32_t)ntohl(sin.sin_addr.s_addr));
421 #endif
422 			if (rt)
423 				RTFREE_LOCKED(rt);
424 			return 0;
425 		}
426 		RTFREE_LOCKED(rt);
427 	}
428 
429 	return 32 * 2;
430 }
431 
432 /*
433  * we know that we are in IFF_UP, outer address available, and outer family
434  * matched the physical addr family.  see gif_encapcheck().
435  */
436 int
437 gif_encapcheck4(const struct mbuf *m, int off, int proto, void *arg)
438 {
439 	struct ip ip;
440 	struct gif_softc *sc;
441 	struct ifnet *ifp;
442 
443 	/* sanity check done in caller */
444 	sc = (struct gif_softc *)arg;
445 
446 	/* LINTED const cast */
447 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
448 	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
449 
450 	return gif_validate4(&ip, sc, ifp);
451 }
452 
453 int
454 in_gif_attach(struct gif_softc *sc)
455 {
456 	sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
457 	    &in_gif_protosw, sc);
458 	if (sc->encap_cookie4 == NULL)
459 		return EEXIST;
460 	return 0;
461 }
462 
463 int
464 in_gif_detach(struct gif_softc *sc)
465 {
466 	int error;
467 
468 	error = encap_detach(sc->encap_cookie4);
469 	if (error == 0)
470 		sc->encap_cookie4 = NULL;
471 	return error;
472 }
473