xref: /netbsd-src/sys/netinet/in_gif.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: in_gif.c,v 1.63 2013/03/01 18:25:57 joerg 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.63 2013/03/01 18:25:57 joerg Exp $");
35 
36 #include "opt_inet.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/ioctl.h>
45 #include <sys/syslog.h>
46 #include <sys/protosw.h>
47 #include <sys/kernel.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 <net/net_osdep.h>
70 
71 static int gif_validate4(const struct ip *, struct gif_softc *,
72 	struct ifnet *);
73 
74 #if NGIF > 0
75 int ip_gif_ttl = GIF_TTL;
76 #else
77 int ip_gif_ttl = 0;
78 #endif
79 
80 const struct protosw in_gif_protosw =
81 { SOCK_RAW,	&inetdomain,	0/* IPPROTO_IPV[46] */,	PR_ATOMIC|PR_ADDR,
82   in_gif_input, rip_output,	0,		rip_ctloutput,
83   rip_usrreq,
84   0,            0,              0,              0,
85 };
86 
87 int
88 in_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
89 {
90 	struct rtentry *rt;
91 	struct gif_softc *sc = ifp->if_softc;
92 	struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
93 	struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
94 	struct ip iphdr;	/* capsule IP header, host byte ordered */
95 	int proto, error;
96 	u_int8_t tos;
97 	union {
98 		struct sockaddr		dst;
99 		struct sockaddr_in	dst4;
100 	} u;
101 
102 	if (sin_src == NULL || sin_dst == NULL ||
103 	    sin_src->sin_family != AF_INET ||
104 	    sin_dst->sin_family != AF_INET) {
105 		m_freem(m);
106 		return EAFNOSUPPORT;
107 	}
108 
109 	switch (family) {
110 #ifdef INET
111 	case AF_INET:
112 	    {
113 		const struct ip *ip;
114 
115 		proto = IPPROTO_IPV4;
116 		if (m->m_len < sizeof(*ip)) {
117 			m = m_pullup(m, sizeof(*ip));
118 			if (m == NULL)
119 				return ENOBUFS;
120 		}
121 		ip = mtod(m, const struct ip *);
122 		tos = ip->ip_tos;
123 		break;
124 	    }
125 #endif /* INET */
126 #ifdef INET6
127 	case AF_INET6:
128 	    {
129 		const struct ip6_hdr *ip6;
130 		proto = IPPROTO_IPV6;
131 		if (m->m_len < sizeof(*ip6)) {
132 			m = m_pullup(m, sizeof(*ip6));
133 			if (m == NULL)
134 				return ENOBUFS;
135 		}
136 		ip6 = mtod(m, const struct ip6_hdr *);
137 		tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
138 		break;
139 	    }
140 #endif /* INET6 */
141 	default:
142 #ifdef DEBUG
143 		printf("in_gif_output: warning: unknown family %d passed\n",
144 			family);
145 #endif
146 		m_freem(m);
147 		return EAFNOSUPPORT;
148 	}
149 
150 	memset(&iphdr, 0, sizeof(iphdr));
151 	iphdr.ip_src = sin_src->sin_addr;
152 	/* bidirectional configured tunnel mode */
153 	if (sin_dst->sin_addr.s_addr != INADDR_ANY)
154 		iphdr.ip_dst = sin_dst->sin_addr;
155 	else {
156 		m_freem(m);
157 		return ENETUNREACH;
158 	}
159 	iphdr.ip_p = proto;
160 	/* version will be set in ip_output() */
161 	iphdr.ip_ttl = ip_gif_ttl;
162 	iphdr.ip_len = htons(m->m_pkthdr.len + sizeof(struct ip));
163 	if (ifp->if_flags & IFF_LINK1)
164 		ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos);
165 	else
166 		ip_ecn_ingress(ECN_NOCARE, &iphdr.ip_tos, &tos);
167 
168 	/* prepend new IP header */
169 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
170 	/* XXX Is m_pullup really necessary after M_PREPEND? */
171 	if (m != NULL && M_UNWRITABLE(m, sizeof(struct ip)))
172 		m = m_pullup(m, sizeof(struct ip));
173 	if (m == NULL)
174 		return ENOBUFS;
175 	bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
176 
177 	sockaddr_in_init(&u.dst4, &sin_dst->sin_addr, 0);
178 	if ((rt = rtcache_lookup(&sc->gif_ro, &u.dst)) == NULL) {
179 		m_freem(m);
180 		return ENETUNREACH;
181 	}
182 
183 	/* If the route constitutes infinite encapsulation, punt. */
184 	if (rt->rt_ifp == ifp) {
185 		rtcache_free(&sc->gif_ro);
186 		m_freem(m);
187 		return ENETUNREACH;	/*XXX*/
188 	}
189 
190 	error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
191 	return (error);
192 }
193 
194 void
195 in_gif_input(struct mbuf *m, ...)
196 {
197 	int off, proto;
198 	struct ifnet *gifp = NULL;
199 	const struct ip *ip;
200 	va_list ap;
201 	int af;
202 	u_int8_t otos;
203 
204 	va_start(ap, m);
205 	off = va_arg(ap, int);
206 	proto = va_arg(ap, int);
207 	va_end(ap);
208 
209 	ip = mtod(m, const struct ip *);
210 
211 	gifp = (struct ifnet *)encap_getarg(m);
212 
213 	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
214 		m_freem(m);
215 		ip_statinc(IP_STAT_NOGIF);
216 		return;
217 	}
218 #ifndef GIF_ENCAPCHECK
219 	if (!gif_validate4(ip, gifp->if_softc, m->m_pkthdr.rcvif)) {
220 		m_freem(m);
221 		ip_statinc(IP_STAT_NOGIF);
222 		return;
223 	}
224 #endif
225 
226 	otos = ip->ip_tos;
227 	m_adj(m, off);
228 
229 	switch (proto) {
230 #ifdef INET
231 	case IPPROTO_IPV4:
232 	    {
233 		struct ip *xip;
234 		af = AF_INET;
235 		if (M_UNWRITABLE(m, sizeof(*xip))) {
236 			if ((m = m_pullup(m, sizeof(*xip))) == NULL)
237 				return;
238 		}
239 		xip = mtod(m, struct ip *);
240 		if (gifp->if_flags & IFF_LINK1)
241 			ip_ecn_egress(ECN_ALLOWED, &otos, &xip->ip_tos);
242 		else
243 			ip_ecn_egress(ECN_NOCARE, &otos, &xip->ip_tos);
244 		break;
245 	    }
246 #endif
247 #ifdef INET6
248 	case IPPROTO_IPV6:
249 	    {
250 		struct ip6_hdr *ip6;
251 		u_int8_t itos;
252 		af = AF_INET6;
253 		if (M_UNWRITABLE(m, sizeof(*ip6))) {
254 			if ((m = m_pullup(m, sizeof(*ip6))) == NULL)
255 				return;
256 		}
257 		ip6 = mtod(m, struct ip6_hdr *);
258 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
259 		if (gifp->if_flags & IFF_LINK1)
260 			ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
261 		else
262 			ip_ecn_egress(ECN_NOCARE, &otos, &itos);
263 		ip6->ip6_flow &= ~htonl(0xff << 20);
264 		ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
265 		break;
266 	    }
267 #endif /* INET6 */
268 	default:
269 		ip_statinc(IP_STAT_NOGIF);
270 		m_freem(m);
271 		return;
272 	}
273 	gif_input(m, af, gifp);
274 	return;
275 }
276 
277 /*
278  * validate outer address.
279  */
280 static int
281 gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
282 {
283 	struct sockaddr_in *src, *dst;
284 	struct in_ifaddr *ia4;
285 
286 	src = (struct sockaddr_in *)sc->gif_psrc;
287 	dst = (struct sockaddr_in *)sc->gif_pdst;
288 
289 	/* check for address match */
290 	if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
291 	    dst->sin_addr.s_addr != ip->ip_src.s_addr)
292 		return 0;
293 
294 	/* martian filters on outer source - NOT done in ip_input! */
295 	if (IN_MULTICAST(ip->ip_src.s_addr))
296 		return 0;
297 	switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
298 	case 0: case 127: case 255:
299 		return 0;
300 	}
301 	/* reject packets with broadcast on source */
302 	TAILQ_FOREACH(ia4, &in_ifaddrhead, ia_list) {
303 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
304 			continue;
305 		if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
306 			return 0;
307 	}
308 
309 	/* ingress filters on outer source */
310 	if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
311 		union {
312 			struct sockaddr sa;
313 			struct sockaddr_in sin;
314 		} u;
315 		struct rtentry *rt;
316 
317 		sockaddr_in_init(&u.sin, &ip->ip_src, 0);
318 		rt = rtalloc1(&u.sa, 0);
319 		if (rt == NULL || rt->rt_ifp != ifp) {
320 #if 0
321 			log(LOG_WARNING, "%s: packet from 0x%x dropped "
322 			    "due to ingress filter\n", if_name(&sc->gif_if),
323 			    (u_int32_t)ntohl(u.sin.sin_addr.s_addr));
324 #endif
325 			if (rt != NULL)
326 				rtfree(rt);
327 			return 0;
328 		}
329 		rtfree(rt);
330 	}
331 
332 	return 32 * 2;
333 }
334 
335 #ifdef GIF_ENCAPCHECK
336 /*
337  * we know that we are in IFF_UP, outer address available, and outer family
338  * matched the physical addr family.  see gif_encapcheck().
339  */
340 int
341 gif_encapcheck4(struct mbuf *m, int off, int proto, void *arg)
342 {
343 	struct ip ip;
344 	struct gif_softc *sc;
345 	struct ifnet *ifp;
346 
347 	/* sanity check done in caller */
348 	sc = arg;
349 
350 	m_copydata(m, 0, sizeof(ip), &ip);
351 	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
352 
353 	return gif_validate4(&ip, sc, ifp);
354 }
355 #endif
356 
357 int
358 in_gif_attach(struct gif_softc *sc)
359 {
360 #ifndef GIF_ENCAPCHECK
361 	struct sockaddr_in mask4;
362 
363 	memset(&mask4, 0, sizeof(mask4));
364 	mask4.sin_len = sizeof(struct sockaddr_in);
365 	mask4.sin_addr.s_addr = ~0;
366 
367 	if (!sc->gif_psrc || !sc->gif_pdst)
368 		return EINVAL;
369 	sc->encap_cookie4 = encap_attach(AF_INET, -1, sc->gif_psrc,
370 	    (struct sockaddr *)&mask4, sc->gif_pdst, (struct sockaddr *)&mask4,
371 	    (const struct protosw *)&in_gif_protosw, sc);
372 #else
373 	sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
374 	    &in_gif_protosw, sc);
375 #endif
376 	if (sc->encap_cookie4 == NULL)
377 		return EEXIST;
378 	return 0;
379 }
380 
381 int
382 in_gif_detach(struct gif_softc *sc)
383 {
384 	int error;
385 
386 	error = encap_detach(sc->encap_cookie4);
387 	if (error == 0)
388 		sc->encap_cookie4 = NULL;
389 
390 	rtcache_free(&sc->gif_ro);
391 
392 	return error;
393 }
394