xref: /freebsd/sys/netinet6/in6_gif.c (revision b28624fde638caadd4a89f50c9b7e7da0f98c4d2)
1 /*	$FreeBSD$	*/
2 /*	$KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 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 "opt_inet.h"
34 #include "opt_inet6.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/socket.h>
39 #include <sys/sockio.h>
40 #include <sys/mbuf.h>
41 #include <sys/errno.h>
42 #include <sys/queue.h>
43 #include <sys/syslog.h>
44 #include <sys/protosw.h>
45 
46 #include <sys/malloc.h>
47 
48 #include <net/if.h>
49 #include <net/route.h>
50 
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #ifdef INET
54 #include <netinet/ip.h>
55 #endif
56 #include <netinet/ip_encap.h>
57 #ifdef INET6
58 #include <netinet/ip6.h>
59 #include <netinet6/ip6_var.h>
60 #include <netinet6/in6_gif.h>
61 #include <netinet6/in6_var.h>
62 #endif
63 #include <netinet6/ip6protosw.h>
64 #include <netinet/ip_ecn.h>
65 #ifdef INET6
66 #include <netinet6/ip6_ecn.h>
67 #endif
68 
69 #include <net/if_gif.h>
70 
71 static int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
72 			 struct ifnet *);
73 
74 extern  struct domain inet6domain;
75 struct ip6protosw in6_gif_protosw =
76 { SOCK_RAW,	&inet6domain,	0/* IPPROTO_IPV[46] */,	PR_ATOMIC|PR_ADDR,
77   in6_gif_input, rip6_output,	0,		rip6_ctloutput,
78   0,
79   0,		0,		0,		0,
80   &rip6_usrreqs
81 };
82 
83 int
84 in6_gif_output(struct ifnet *ifp,
85     int family,			/* family of the packet to be encapsulate */
86     struct mbuf *m)
87 {
88 	struct gif_softc *sc = ifp->if_softc;
89 	struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
90 	struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
91 	struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
92 	struct ip6_hdr *ip6;
93 	struct etherip_header eiphdr;
94 	int proto, error;
95 	u_int8_t itos, otos;
96 
97 	GIF_LOCK_ASSERT(sc);
98 
99 	if (sin6_src == NULL || sin6_dst == NULL ||
100 	    sin6_src->sin6_family != AF_INET6 ||
101 	    sin6_dst->sin6_family != AF_INET6) {
102 		m_freem(m);
103 		return EAFNOSUPPORT;
104 	}
105 
106 	switch (family) {
107 #ifdef INET
108 	case AF_INET:
109 	    {
110 		struct ip *ip;
111 
112 		proto = IPPROTO_IPV4;
113 		if (m->m_len < sizeof(*ip)) {
114 			m = m_pullup(m, sizeof(*ip));
115 			if (!m)
116 				return ENOBUFS;
117 		}
118 		ip = mtod(m, struct ip *);
119 		itos = ip->ip_tos;
120 		break;
121 	    }
122 #endif
123 #ifdef INET6
124 	case AF_INET6:
125 	    {
126 		struct ip6_hdr *ip6;
127 		proto = IPPROTO_IPV6;
128 		if (m->m_len < sizeof(*ip6)) {
129 			m = m_pullup(m, sizeof(*ip6));
130 			if (!m)
131 				return ENOBUFS;
132 		}
133 		ip6 = mtod(m, struct ip6_hdr *);
134 		itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
135 		break;
136 	    }
137 #endif
138 	case AF_LINK:
139 		proto = IPPROTO_ETHERIP;
140 		eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK;
141 		eiphdr.eip_pad = 0;
142 		/* prepend Ethernet-in-IP header */
143 		M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
144 		if (m && m->m_len < sizeof(struct etherip_header))
145 			m = m_pullup(m, sizeof(struct etherip_header));
146 		if (m == NULL)
147 			return ENOBUFS;
148 		bcopy(&eiphdr, mtod(m, struct etherip_header *),
149 		    sizeof(struct etherip_header));
150 		break;
151 
152 	default:
153 #ifdef DEBUG
154 		printf("in6_gif_output: warning: unknown family %d passed\n",
155 			family);
156 #endif
157 		m_freem(m);
158 		return EAFNOSUPPORT;
159 	}
160 
161 	/* prepend new IP header */
162 	M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
163 	if (m && m->m_len < sizeof(struct ip6_hdr))
164 		m = m_pullup(m, sizeof(struct ip6_hdr));
165 	if (m == NULL) {
166 		printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
167 		return ENOBUFS;
168 	}
169 
170 	ip6 = mtod(m, struct ip6_hdr *);
171 	ip6->ip6_flow	= 0;
172 	ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
173 	ip6->ip6_vfc	|= IPV6_VERSION;
174 	ip6->ip6_plen	= htons((u_short)m->m_pkthdr.len);
175 	ip6->ip6_nxt	= proto;
176 	ip6->ip6_hlim	= ip6_gif_hlim;
177 	ip6->ip6_src	= sin6_src->sin6_addr;
178 	/* bidirectional configured tunnel mode */
179 	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
180 		ip6->ip6_dst = sin6_dst->sin6_addr;
181 	else  {
182 		m_freem(m);
183 		return ENETUNREACH;
184 	}
185 	ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
186 		       &otos, &itos);
187 	ip6->ip6_flow &= ~htonl(0xff << 20);
188 	ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
189 
190 	if (dst->sin6_family != sin6_dst->sin6_family ||
191 	     !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
192 		/* cache route doesn't match */
193 		bzero(dst, sizeof(*dst));
194 		dst->sin6_family = sin6_dst->sin6_family;
195 		dst->sin6_len = sizeof(struct sockaddr_in6);
196 		dst->sin6_addr = sin6_dst->sin6_addr;
197 		if (sc->gif_ro6.ro_rt) {
198 			RTFREE(sc->gif_ro6.ro_rt);
199 			sc->gif_ro6.ro_rt = NULL;
200 		}
201 #if 0
202 		GIF2IFP(sc)->if_mtu = GIF_MTU;
203 #endif
204 	}
205 
206 	if (sc->gif_ro6.ro_rt == NULL) {
207 		rtalloc((struct route *)&sc->gif_ro6);
208 		if (sc->gif_ro6.ro_rt == NULL) {
209 			m_freem(m);
210 			return ENETUNREACH;
211 		}
212 
213 		/* if it constitutes infinite encapsulation, punt. */
214 		if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
215 			m_freem(m);
216 			return ENETUNREACH;	/*XXX*/
217 		}
218 #if 0
219 		ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
220 			- sizeof(struct ip6_hdr);
221 #endif
222 	}
223 
224 #ifdef IPV6_MINMTU
225 	/*
226 	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
227 	 * it is too painful to ask for resend of inner packet, to achieve
228 	 * path MTU discovery for encapsulated packets.
229 	 */
230 	error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL);
231 #else
232 	error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL);
233 #endif
234 
235 	if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
236 	    sc->gif_ro6.ro_rt != NULL) {
237 		RTFREE(sc->gif_ro6.ro_rt);
238 		sc->gif_ro6.ro_rt = NULL;
239 	}
240 
241 	return (error);
242 }
243 
244 int
245 in6_gif_input(struct mbuf **mp, int *offp, int proto)
246 {
247 	struct mbuf *m = *mp;
248 	struct ifnet *gifp = NULL;
249 	struct gif_softc *sc;
250 	struct ip6_hdr *ip6;
251 	int af = 0;
252 	u_int32_t otos;
253 
254 	ip6 = mtod(m, struct ip6_hdr *);
255 
256 	sc = (struct gif_softc *)encap_getarg(m);
257 	if (sc == NULL) {
258 		m_freem(m);
259 		ip6stat.ip6s_nogif++;
260 		return IPPROTO_DONE;
261 	}
262 
263 	gifp = GIF2IFP(sc);
264 	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
265 		m_freem(m);
266 		ip6stat.ip6s_nogif++;
267 		return IPPROTO_DONE;
268 	}
269 
270 	otos = ip6->ip6_flow;
271 	m_adj(m, *offp);
272 
273 	switch (proto) {
274 #ifdef INET
275 	case IPPROTO_IPV4:
276 	    {
277 		struct ip *ip;
278 		u_int8_t otos8;
279 		af = AF_INET;
280 		otos8 = (ntohl(otos) >> 20) & 0xff;
281 		if (m->m_len < sizeof(*ip)) {
282 			m = m_pullup(m, sizeof(*ip));
283 			if (!m)
284 				return IPPROTO_DONE;
285 		}
286 		ip = mtod(m, struct ip *);
287 		if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
288 				  ECN_ALLOWED : ECN_NOCARE,
289 				  &otos8, &ip->ip_tos) == 0) {
290 			m_freem(m);
291 			return IPPROTO_DONE;
292 		}
293 		break;
294 	    }
295 #endif /* INET */
296 #ifdef INET6
297 	case IPPROTO_IPV6:
298 	    {
299 		struct ip6_hdr *ip6;
300 		af = AF_INET6;
301 		if (m->m_len < sizeof(*ip6)) {
302 			m = m_pullup(m, sizeof(*ip6));
303 			if (!m)
304 				return IPPROTO_DONE;
305 		}
306 		ip6 = mtod(m, struct ip6_hdr *);
307 		if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ?
308 				   ECN_ALLOWED : ECN_NOCARE,
309 				   &otos, &ip6->ip6_flow) == 0) {
310 			m_freem(m);
311 			return IPPROTO_DONE;
312 		}
313 		break;
314 	    }
315 #endif
316 	case IPPROTO_ETHERIP:
317 		af = AF_LINK;
318 		break;
319 
320 	default:
321 		ip6stat.ip6s_nogif++;
322 		m_freem(m);
323 		return IPPROTO_DONE;
324 	}
325 
326 	gif_input(m, af, gifp);
327 	return IPPROTO_DONE;
328 }
329 
330 /*
331  * validate outer address.
332  */
333 static int
334 gif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
335     struct ifnet *ifp)
336 {
337 	struct sockaddr_in6 *src, *dst;
338 
339 	src = (struct sockaddr_in6 *)sc->gif_psrc;
340 	dst = (struct sockaddr_in6 *)sc->gif_pdst;
341 
342 	/*
343 	 * Check for address match.  Note that the check is for an incoming
344 	 * packet.  We should compare the *source* address in our configuration
345 	 * and the *destination* address of the packet, and vice versa.
346 	 */
347 	if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
348 	    !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
349 		return 0;
350 
351 	/* martian filters on outer source - done in ip6_input */
352 
353 	/* ingress filters on outer source */
354 	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
355 		struct sockaddr_in6 sin6;
356 		struct rtentry *rt;
357 
358 		bzero(&sin6, sizeof(sin6));
359 		sin6.sin6_family = AF_INET6;
360 		sin6.sin6_len = sizeof(struct sockaddr_in6);
361 		sin6.sin6_addr = ip6->ip6_src;
362 		sin6.sin6_scope_id = 0; /* XXX */
363 
364 		rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
365 		if (!rt || rt->rt_ifp != ifp) {
366 #if 0
367 			char ip6buf[INET6_ADDRSTRLEN];
368 			log(LOG_WARNING, "%s: packet from %s dropped "
369 			    "due to ingress filter\n", if_name(GIF2IFP(sc)),
370 			    ip6_sprintf(ip6buf, &sin6.sin6_addr));
371 #endif
372 			if (rt)
373 				rtfree(rt);
374 			return 0;
375 		}
376 		rtfree(rt);
377 	}
378 
379 	return 128 * 2;
380 }
381 
382 /*
383  * we know that we are in IFF_UP, outer address available, and outer family
384  * matched the physical addr family.  see gif_encapcheck().
385  * sanity check for arg should have been done in the caller.
386  */
387 int
388 gif_encapcheck6(const struct mbuf *m, int off, int proto, void *arg)
389 {
390 	struct ip6_hdr ip6;
391 	struct gif_softc *sc;
392 	struct ifnet *ifp;
393 
394 	/* sanity check done in caller */
395 	sc = (struct gif_softc *)arg;
396 
397 	/* LINTED const cast */
398 	m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
399 	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
400 
401 	return gif_validate6(&ip6, sc, ifp);
402 }
403 
404 int
405 in6_gif_attach(struct gif_softc *sc)
406 {
407 	sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
408 	    (void *)&in6_gif_protosw, sc);
409 	if (sc->encap_cookie6 == NULL)
410 		return EEXIST;
411 	return 0;
412 }
413 
414 int
415 in6_gif_detach(struct gif_softc *sc)
416 {
417 	int error;
418 
419 	error = encap_detach(sc->encap_cookie6);
420 	if (error == 0)
421 		sc->encap_cookie6 = NULL;
422 	return error;
423 }
424