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