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