xref: /dragonfly/sys/netinet6/in6_gif.c (revision b40e316c)
1 /*	$FreeBSD: src/sys/netinet6/in6_gif.c,v 1.2.2.7 2003/01/23 21:06:47 sam Exp $	*/
2 /*	$DragonFly: src/sys/netinet6/in6_gif.c,v 1.11 2005/01/06 17:59:32 hsu Exp $	*/
3 /*	$KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $	*/
4 
5 /*
6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/socket.h>
40 #include <sys/sockio.h>
41 #include <sys/mbuf.h>
42 #include <sys/errno.h>
43 #include <sys/queue.h>
44 #include <sys/syslog.h>
45 #include <sys/protosw.h>
46 
47 #include <sys/malloc.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 #ifdef INET
55 #include <netinet/ip.h>
56 #endif
57 #include <netinet/ip_encap.h>
58 #ifdef INET6
59 #include <netinet/ip6.h>
60 #include <netinet6/ip6_var.h>
61 #include <netinet6/in6_gif.h>
62 #include <netinet6/in6_var.h>
63 #endif
64 #include <netinet6/ip6protosw.h>
65 #include <netinet/ip_ecn.h>
66 #ifdef INET6
67 #include <netinet6/ip6_ecn.h>
68 #endif
69 
70 #include <net/gif/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 extern  struct domain inet6domain;
78 struct ip6protosw in6_gif_protosw =
79 { SOCK_RAW,	&inet6domain,	0/*IPPROTO_IPV[46]*/,	PR_ATOMIC|PR_ADDR,
80   in6_gif_input, rip6_output,	0,		rip6_ctloutput,
81   cpu0_soport,
82   0,		0,		0,		0,
83   &rip6_usrreqs
84 };
85 
86 int
87 in6_gif_output(struct ifnet *ifp,
88 	       int family,	/* family of the packet to be encapsulate. */
89 	       struct mbuf *m)
90 {
91 	struct gif_softc *sc = (struct gif_softc*)ifp;
92 	struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
93 	struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
94 	struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
95 	struct ip6_hdr *ip6;
96 	int proto;
97 	u_int8_t itos, otos;
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 	default:
139 #ifdef DEBUG
140 		printf("in6_gif_output: warning: unknown family %d passed\n",
141 			family);
142 #endif
143 		m_freem(m);
144 		return EAFNOSUPPORT;
145 	}
146 
147 	/* prepend new IP header */
148 	M_PREPEND(m, sizeof(struct ip6_hdr), MB_DONTWAIT);
149 	if (m && m->m_len < sizeof(struct ip6_hdr))
150 		m = m_pullup(m, sizeof(struct ip6_hdr));
151 	if (m == NULL) {
152 		printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
153 		return ENOBUFS;
154 	}
155 
156 	ip6 = mtod(m, struct ip6_hdr *);
157 	ip6->ip6_flow	= 0;
158 	ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
159 	ip6->ip6_vfc	|= IPV6_VERSION;
160 	ip6->ip6_plen	= htons((u_short)m->m_pkthdr.len);
161 	ip6->ip6_nxt	= proto;
162 	ip6->ip6_hlim	= ip6_gif_hlim;
163 	ip6->ip6_src	= sin6_src->sin6_addr;
164 	/* bidirectional configured tunnel mode */
165 	if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
166 		ip6->ip6_dst = sin6_dst->sin6_addr;
167 	else  {
168 		m_freem(m);
169 		return ENETUNREACH;
170 	}
171 	if (ifp->if_flags & IFF_LINK1)
172 		ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
173 	else
174 		ip_ecn_ingress(ECN_NOCARE, &otos, &itos);
175 	ip6->ip6_flow &= ~ntohl(0xff00000);
176 	ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
177 
178 	if (dst->sin6_family != sin6_dst->sin6_family ||
179 	     !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
180 		/* cache route doesn't match */
181 		bzero(dst, sizeof(*dst));
182 		dst->sin6_family = sin6_dst->sin6_family;
183 		dst->sin6_len = sizeof(struct sockaddr_in6);
184 		dst->sin6_addr = sin6_dst->sin6_addr;
185 		if (sc->gif_ro6.ro_rt) {
186 			RTFREE(sc->gif_ro6.ro_rt);
187 			sc->gif_ro6.ro_rt = NULL;
188 		}
189 #if 0
190 		sc->gif_if.if_mtu = GIF_MTU;
191 #endif
192 	}
193 
194 	if (sc->gif_ro6.ro_rt == NULL) {
195 		rtalloc((struct route *)&sc->gif_ro6);
196 		if (sc->gif_ro6.ro_rt == NULL) {
197 			m_freem(m);
198 			return ENETUNREACH;
199 		}
200 
201 		/* if it constitutes infinite encapsulation, punt. */
202 		if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
203 			m_freem(m);
204 			return ENETUNREACH;	/*XXX*/
205 		}
206 #if 0
207 		ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
208 			- sizeof(struct ip6_hdr);
209 #endif
210 	}
211 
212 #ifdef IPV6_MINMTU
213 	/*
214 	 * force fragmentation to minimum MTU, to avoid path MTU discovery.
215 	 * it is too painful to ask for resend of inner packet, to achieve
216 	 * path MTU discovery for encapsulated packets.
217 	 */
218 	return(ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL));
219 #else
220 	return(ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL));
221 #endif
222 }
223 
224 int in6_gif_input(struct mbuf **mp, int *offp, int proto)
225 {
226 	struct mbuf *m = *mp;
227 	struct ifnet *gifp = NULL;
228 	struct ip6_hdr *ip6;
229 	int af = 0;
230 	u_int32_t otos;
231 
232 	ip6 = mtod(m, struct ip6_hdr *);
233 
234 	gifp = (struct ifnet *)encap_getarg(m);
235 
236 	if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
237 		m_freem(m);
238 		ip6stat.ip6s_nogif++;
239 		return IPPROTO_DONE;
240 	}
241 
242 	otos = ip6->ip6_flow;
243 	m_adj(m, *offp);
244 
245 	switch (proto) {
246 #ifdef INET
247 	case IPPROTO_IPV4:
248 	    {
249 		struct ip *ip;
250 		u_int8_t otos8;
251 		af = AF_INET;
252 		otos8 = (ntohl(otos) >> 20) & 0xff;
253 		if (m->m_len < sizeof(*ip)) {
254 			m = m_pullup(m, sizeof(*ip));
255 			if (!m)
256 				return IPPROTO_DONE;
257 		}
258 		ip = mtod(m, struct ip *);
259 		if (gifp->if_flags & IFF_LINK1)
260 			ip_ecn_egress(ECN_ALLOWED, &otos8, &ip->ip_tos);
261 		else
262 			ip_ecn_egress(ECN_NOCARE, &otos8, &ip->ip_tos);
263 		break;
264 	    }
265 #endif /* INET */
266 #ifdef INET6
267 	case IPPROTO_IPV6:
268 	    {
269 		struct ip6_hdr *ip6;
270 		af = AF_INET6;
271 		if (m->m_len < sizeof(*ip6)) {
272 			m = m_pullup(m, sizeof(*ip6));
273 			if (!m)
274 				return IPPROTO_DONE;
275 		}
276 		ip6 = mtod(m, struct ip6_hdr *);
277 		if (gifp->if_flags & IFF_LINK1)
278 			ip6_ecn_egress(ECN_ALLOWED, &otos, &ip6->ip6_flow);
279 		else
280 			ip6_ecn_egress(ECN_NOCARE, &otos, &ip6->ip6_flow);
281 		break;
282 	    }
283 #endif
284 	default:
285 		ip6stat.ip6s_nogif++;
286 		m_freem(m);
287 		return IPPROTO_DONE;
288 	}
289 
290 	gif_input(m, af, gifp);
291 	return IPPROTO_DONE;
292 }
293 
294 /*
295  * validate outer address.
296  */
297 static int
298 gif_validate6(const struct ip6_hdr *ip6, struct gif_softc *sc,
299 	      struct ifnet *ifp)
300 {
301 	struct sockaddr_in6 *src, *dst;
302 
303 	src = (struct sockaddr_in6 *)sc->gif_psrc;
304 	dst = (struct sockaddr_in6 *)sc->gif_pdst;
305 
306 	/*
307 	 * Check for address match.  Note that the check is for an incoming
308 	 * packet.  We should compare the *source* address in our configuration
309 	 * and the *destination* address of the packet, and vice versa.
310 	 */
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 		struct sockaddr_in6 sin6;
320 		struct rtentry *rt;
321 
322 		bzero(&sin6, sizeof(sin6));
323 		sin6.sin6_family = AF_INET6;
324 		sin6.sin6_len = sizeof(struct sockaddr_in6);
325 		sin6.sin6_addr = ip6->ip6_src;
326 #ifndef SCOPEDROUTING
327 		sin6.sin6_scope_id = 0; /* XXX */
328 #endif
329 
330 		rt = rtpurelookup((struct sockaddr *)&sin6);
331 		if (rt == NULL || rt->rt_ifp != ifp) {
332 #if 0
333 			log(LOG_WARNING, "%s: packet from %s dropped "
334 			    "due to ingress filter\n", if_name(&sc->gif_if),
335 			    ip6_sprintf(&sin6.sin6_addr));
336 #endif
337 			if (rt != NULL)
338 				--rt->rt_refcnt;
339 			return 0;
340 		}
341 		--rt->rt_refcnt;
342 	}
343 
344 	return 128 * 2;
345 }
346 
347 /*
348  * we know that we are in IFF_UP, outer address available, and outer family
349  * matched the physical addr family.  see gif_encapcheck().
350  * sanity check for arg should have been done in the caller.
351  */
352 int
353 gif_encapcheck6(const struct mbuf *m, int off, int proto, void *arg)
354 {
355 	struct ip6_hdr ip6;
356 	struct gif_softc *sc;
357 	struct ifnet *ifp;
358 
359 	/* sanity check done in caller */
360 	sc = (struct gif_softc *)arg;
361 
362 	m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
363 	ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
364 
365 	return gif_validate6(&ip6, sc, ifp);
366 }
367 
368 int
369 in6_gif_attach(struct gif_softc *sc)
370 {
371 	sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
372 	    (struct protosw *)&in6_gif_protosw, sc);
373 	if (sc->encap_cookie6 == NULL)
374 		return EEXIST;
375 	return 0;
376 }
377 
378 int
379 in6_gif_detach(struct gif_softc *sc)
380 {
381 	int error;
382 
383 	error = encap_detach(sc->encap_cookie6);
384 	if (error == 0)
385 		sc->encap_cookie6 = NULL;
386 	return error;
387 }
388