xref: /netbsd-src/sys/netinet6/in6_gif.c (revision 7e30e94394d0994ab9534f68a8f91665045c91ce)
1 /*	$NetBSD: in6_gif.c,v 1.85 2017/01/16 15:44:47 christos Exp $	*/
2 /*	$KAME: in6_gif.c,v 1.62 2001/07/29 04:27:25 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: in6_gif.c,v 1.85 2017/01/16 15:44:47 christos Exp $");
35 
36 #ifdef _KERNEL_OPT
37 #include "opt_inet.h"
38 #endif
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/mbuf.h>
45 #include <sys/errno.h>
46 #include <sys/ioctl.h>
47 #include <sys/queue.h>
48 #include <sys/syslog.h>
49 #include <sys/kernel.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/ip6_private.h>
64 #include <netinet6/in6_gif.h>
65 #include <netinet6/in6_var.h>
66 #endif
67 #include <netinet6/ip6protosw.h> /* for struct ip6ctlparam */
68 #include <netinet/ip_ecn.h>
69 
70 #include <net/if_gif.h>
71 
72 #include <net/net_osdep.h>
73 
74 static int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
75 	struct ifnet *);
76 
77 int	ip6_gif_hlim = GIF_HLIM;
78 
79 static const struct encapsw in6_gif_encapsw;
80 
81 /*
82  * family - family of the packet to be encapsulate.
83  */
84 
85 int
86 in6_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
87 {
88 	struct rtentry *rt;
89 	struct route *ro;
90 	struct gif_softc *sc = ifp->if_softc;
91 	struct sockaddr_in6 *sin6_src = satosin6(sc->gif_psrc);
92 	struct sockaddr_in6 *sin6_dst = satosin6(sc->gif_pdst);
93 	struct ip6_hdr *ip6;
94 	int proto, error;
95 	u_int8_t itos, otos;
96 
97 	if (sin6_src == NULL || sin6_dst == NULL ||
98 	    sin6_src->sin6_family != AF_INET6 ||
99 	    sin6_dst->sin6_family != AF_INET6) {
100 		m_freem(m);
101 		return EAFNOSUPPORT;
102 	}
103 
104 	switch (family) {
105 #ifdef INET
106 	case AF_INET:
107 	    {
108 		struct ip *ip;
109 
110 		proto = IPPROTO_IPV4;
111 		if (m->m_len < sizeof(*ip)) {
112 			m = m_pullup(m, sizeof(*ip));
113 			if (!m)
114 				return ENOBUFS;
115 		}
116 		ip = mtod(m, struct ip *);
117 		itos = ip->ip_tos;
118 		break;
119 	    }
120 #endif
121 #ifdef INET6
122 	case AF_INET6:
123 	    {
124 		proto = IPPROTO_IPV6;
125 		if (m->m_len < sizeof(*ip6)) {
126 			m = m_pullup(m, sizeof(*ip6));
127 			if (!m)
128 				return ENOBUFS;
129 		}
130 		ip6 = mtod(m, struct ip6_hdr *);
131 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
132 		break;
133 	    }
134 #endif
135 	default:
136 #ifdef DEBUG
137 		printf("in6_gif_output: warning: unknown family %d passed\n",
138 			family);
139 #endif
140 		m_freem(m);
141 		return EAFNOSUPPORT;
142 	}
143 
144 	/* prepend new IP header */
145 	M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
146 	if (m && m->m_len < sizeof(struct ip6_hdr))
147 		m = m_pullup(m, sizeof(struct ip6_hdr));
148 	if (m == NULL)
149 		return ENOBUFS;
150 
151 	ip6 = mtod(m, struct ip6_hdr *);
152 	ip6->ip6_flow	= 0;
153 	ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
154 	ip6->ip6_vfc	|= IPV6_VERSION;
155 #if 0	/* ip6->ip6_plen will be filled by ip6_output */
156 	ip6->ip6_plen	= htons((u_int16_t)m->m_pkthdr.len);
157 #endif
158 	ip6->ip6_nxt	= proto;
159 	ip6->ip6_hlim	= ip6_gif_hlim;
160 	ip6->ip6_src	= sin6_src->sin6_addr;
161 	/* bidirectional configured tunnel mode */
162 	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
163 		ip6->ip6_dst = sin6_dst->sin6_addr;
164 	else  {
165 		m_freem(m);
166 		return ENETUNREACH;
167 	}
168 	if (ifp->if_flags & IFF_LINK1)
169 		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
170 	else
171 		ip_ecn_ingress(ECN_NOCARE, &otos, &itos);
172 	ip6->ip6_flow &= ~ntohl(0xff00000);
173 	ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
174 
175 	ro = percpu_getref(sc->gif_ro_percpu);
176 	rt = rtcache_lookup(ro, sc->gif_pdst);
177 	if (rt == NULL) {
178 		percpu_putref(sc->gif_ro_percpu);
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_unref(rt, ro);
186 		rtcache_free(ro);
187 		percpu_putref(sc->gif_ro_percpu);
188 		m_freem(m);
189 		return ENETUNREACH;	/* XXX */
190 	}
191 	rtcache_unref(rt, ro);
192 
193 #ifdef IPV6_MINMTU
194 	/*
195 	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
196 	 * it is too painful to ask for resend of inner packet, to achieve
197 	 * path MTU discovery for encapsulated packets.
198 	 */
199 	error = ip6_output(m, 0, ro, IPV6_MINMTU, NULL, NULL, NULL);
200 #else
201 	error = ip6_output(m, 0, ro, 0, NULL, NULL, NULL);
202 #endif
203 	percpu_putref(sc->gif_ro_percpu);
204 	return (error);
205 }
206 
207 int
208 in6_gif_input(struct mbuf **mp, int *offp, int proto)
209 {
210 	struct mbuf *m = *mp;
211 	struct ifnet *gifp = NULL;
212 	struct ip6_hdr *ip6;
213 	int af = 0;
214 	u_int32_t otos;
215 
216 	ip6 = mtod(m, struct ip6_hdr *);
217 
218 	gifp = (struct ifnet *)encap_getarg(m);
219 
220 	if (gifp == NULL || (gifp->if_flags & (IFF_UP|IFF_RUNNING))
221 		!= (IFF_UP|IFF_RUNNING)) {
222 		m_freem(m);
223 		IP6_STATINC(IP6_STAT_NOGIF);
224 		return IPPROTO_DONE;
225 	}
226 #ifndef GIF_ENCAPCHECK
227 	struct gif_softc *sc = (struct gif_softc *)gifp->if_softc;
228 	/* other CPU do delete_tunnel */
229 	if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
230 		m_freem(m);
231 		IP6_STATINC(IP6_STAT_NOGIF);
232 		return IPPROTO_DONE;
233 	}
234 
235 	struct psref psref;
236 	struct ifnet *rcvif = m_get_rcvif_psref(m, &psref);
237 	if (rcvif == NULL || !gif_validate6(ip6, sc, rcvif)) {
238 		m_put_rcvif_psref(rcvif, &psref);
239 		m_freem(m);
240 		IP6_STATINC(IP6_STAT_NOGIF);
241 		return IPPROTO_DONE;
242 	}
243 	m_put_rcvif_psref(rcvif, &psref);
244 #endif
245 
246 	otos = ip6->ip6_flow;
247 	m_adj(m, *offp);
248 
249 	switch (proto) {
250 #ifdef INET
251 	case IPPROTO_IPV4:
252 	    {
253 		struct ip *ip;
254 		u_int8_t otos8;
255 		af = AF_INET;
256 		otos8 = (ntohl(otos) >> 20) & 0xff;
257 		if (m->m_len < sizeof(*ip)) {
258 			m = m_pullup(m, sizeof(*ip));
259 			if (!m)
260 				return IPPROTO_DONE;
261 		}
262 		ip = mtod(m, struct ip *);
263 		if (gifp->if_flags & IFF_LINK1)
264 			ip_ecn_egress(ECN_ALLOWED, &otos8, &ip->ip_tos);
265 		else
266 			ip_ecn_egress(ECN_NOCARE, &otos8, &ip->ip_tos);
267 		break;
268 	    }
269 #endif /* INET */
270 #ifdef INET6
271 	case IPPROTO_IPV6:
272 	    {
273 		struct ip6_hdr *ip6x;
274 		af = AF_INET6;
275 		if (m->m_len < sizeof(*ip6x)) {
276 			m = m_pullup(m, sizeof(*ip6x));
277 			if (!m)
278 				return IPPROTO_DONE;
279 		}
280 		ip6x = mtod(m, struct ip6_hdr *);
281 		if (gifp->if_flags & IFF_LINK1)
282 			ip6_ecn_egress(ECN_ALLOWED, &otos, &ip6x->ip6_flow);
283 		else
284 			ip6_ecn_egress(ECN_NOCARE, &otos, &ip6x->ip6_flow);
285 		break;
286 	    }
287 #endif
288 	default:
289 		IP6_STATINC(IP6_STAT_NOGIF);
290 		m_freem(m);
291 		return IPPROTO_DONE;
292 	}
293 
294 	gif_input(m, af, gifp);
295 	return IPPROTO_DONE;
296 }
297 
298 /*
299  * validate outer address.
300  */
301 static int
302 gif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
303 	struct ifnet *ifp)
304 {
305 	const struct sockaddr_in6 *src, *dst;
306 
307 	src = satosin6(sc->gif_psrc);
308 	dst = satosin6(sc->gif_pdst);
309 
310 	/* check for address match */
311 	if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
312 	    !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
313 		return 0;
314 
315 	/* martian filters on outer source - done in ip6_input */
316 
317 	/* ingress filters on outer source */
318 	if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
319 		union {
320 			struct sockaddr sa;
321 			struct sockaddr_in6 sin6;
322 		} u;
323 		struct rtentry *rt;
324 
325 		/* XXX scopeid */
326 		sockaddr_in6_init(&u.sin6, &ip6->ip6_src, 0, 0, 0);
327 		rt = rtalloc1(&u.sa, 0);
328 		if (rt == NULL || rt->rt_ifp != ifp) {
329 #if 0
330 			char ip6buf[INET6_ADDRSTRLEN];
331 			log(LOG_WARNING, "%s: packet from %s dropped "
332 			    "due to ingress filter\n", if_name(&sc->gif_if),
333 			    IN6_PRINT(ip6buf, &u.sin6.sin6_addr));
334 #endif
335 			if (rt != NULL)
336 				rt_unref(rt);
337 			return 0;
338 		}
339 		rt_unref(rt);
340 	}
341 
342 	return 128 * 2;
343 }
344 
345 #ifdef GIF_ENCAPCHECK
346 /*
347  * we know that we are in IFF_UP, outer address available, and outer family
348  * matched the physical addr family.  see gif_encapcheck().
349  */
350 int
351 gif_encapcheck6(struct mbuf *m, int off, int proto, void *arg)
352 {
353 	struct ip6_hdr ip6;
354 	struct gif_softc *sc;
355 	struct ifnet *ifp = NULL;
356 	int r;
357 	struct psref psref;
358 
359 	/* sanity check done in caller */
360 	sc = arg;
361 
362 	m_copydata(m, 0, sizeof(ip6), (void *)&ip6);
363 	if ((m->m_flags & M_PKTHDR) != 0)
364 		ifp = m_get_rcvif_psref(m, &psref);
365 
366 	r = gif_validate6(&ip6, sc, ifp);
367 
368 	m_put_rcvif_psref(ifp, &psref);
369 	return r;
370 }
371 #endif
372 
373 int
374 in6_gif_attach(struct gif_softc *sc)
375 {
376 #ifndef GIF_ENCAPCHECK
377 	struct sockaddr_in6 mask6;
378 
379 	memset(&mask6, 0, sizeof(mask6));
380 	mask6.sin6_len = sizeof(struct sockaddr_in6);
381 	mask6.sin6_addr.s6_addr32[0] = mask6.sin6_addr.s6_addr32[1] =
382 	    mask6.sin6_addr.s6_addr32[2] = mask6.sin6_addr.s6_addr32[3] = ~0;
383 
384 	if (!sc->gif_psrc || !sc->gif_pdst)
385 		return EINVAL;
386 	sc->encap_cookie6 = encap_attach(AF_INET6, -1, sc->gif_psrc,
387 	    sin6tosa(&mask6), sc->gif_pdst, sin6tosa(&mask6),
388 	    (const void *)&in6_gif_encapsw, sc);
389 #else
390 	sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
391 	    &in6_gif_encapsw, sc);
392 #endif
393 	if (sc->encap_cookie6 == NULL)
394 		return EEXIST;
395 	return 0;
396 }
397 
398 int
399 in6_gif_detach(struct gif_softc *sc)
400 {
401 	int error;
402 
403 	error = in6_gif_pause(sc);
404 
405 	percpu_foreach(sc->gif_ro_percpu, gif_rtcache_free_pc, NULL);
406 
407 	return error;
408 }
409 
410 int
411 in6_gif_pause(struct gif_softc *sc)
412 {
413 	int error;
414 
415 	error = encap_detach(sc->encap_cookie6);
416 	if (error == 0)
417 		sc->encap_cookie6 = NULL;
418 
419 	return error;
420 }
421 
422 void *
423 in6_gif_ctlinput(int cmd, const struct sockaddr *sa, void *d, void *eparg)
424 {
425 	struct gif_softc *sc = eparg;
426 	struct ip6ctlparam *ip6cp = NULL;
427 	struct ip6_hdr *ip6;
428 	const struct sockaddr_in6 *dst6;
429 	struct route *ro;
430 
431 	if (sa->sa_family != AF_INET6 ||
432 	    sa->sa_len != sizeof(struct sockaddr_in6))
433 		return NULL;
434 
435 	if ((unsigned)cmd >= PRC_NCMDS)
436 		return NULL;
437 	if (cmd == PRC_HOSTDEAD)
438 		d = NULL;
439 	else if (inet6ctlerrmap[cmd] == 0)
440 		return NULL;
441 
442 	/* if the parameter is from icmp6, decode it. */
443 	if (d != NULL) {
444 		ip6cp = (struct ip6ctlparam *)d;
445 		ip6 = ip6cp->ip6c_ip6;
446 	} else {
447 		ip6 = NULL;
448 	}
449 
450 	if (!ip6)
451 		return NULL;
452 
453 	if ((sc->gif_if.if_flags & IFF_RUNNING) == 0)
454 		return NULL;
455 	if (sc->gif_psrc->sa_family != AF_INET6)
456 		return NULL;
457 
458 	ro = percpu_getref(sc->gif_ro_percpu);
459 	dst6 = satocsin6(rtcache_getdst(ro));
460 	/* XXX scope */
461 	if (dst6 == NULL)
462 		;
463 	else if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst6->sin6_addr))
464 		rtcache_free(ro);
465 
466 	percpu_putref(sc->gif_ro_percpu);
467 	return NULL;
468 }
469 
470 ENCAP_PR_WRAP_CTLINPUT(in6_gif_ctlinput)
471 #define	in6_gif_ctlinput	in6_gif_ctlinput_wrapper
472 
473 static const struct encapsw in6_gif_encapsw = {
474 	.encapsw6 = {
475 		.pr_input	= in6_gif_input,
476 		.pr_ctlinput	= in6_gif_ctlinput,
477 	}
478 };
479