in_gif.c revision 1.55 1 1.55 christos /* $NetBSD: in_gif.c,v 1.55 2007/03/04 06:03:20 christos Exp $ */
2 1.22 itojun /* $KAME: in_gif.c,v 1.66 2001/07/29 04:46:09 itojun Exp $ */
3 1.3 thorpej
4 1.2 itojun /*
5 1.2 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 1.2 itojun * All rights reserved.
7 1.12 itojun *
8 1.2 itojun * Redistribution and use in source and binary forms, with or without
9 1.2 itojun * modification, are permitted provided that the following conditions
10 1.2 itojun * are met:
11 1.2 itojun * 1. Redistributions of source code must retain the above copyright
12 1.2 itojun * notice, this list of conditions and the following disclaimer.
13 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.2 itojun * notice, this list of conditions and the following disclaimer in the
15 1.2 itojun * documentation and/or other materials provided with the distribution.
16 1.2 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.2 itojun * may be used to endorse or promote products derived from this software
18 1.2 itojun * without specific prior written permission.
19 1.12 itojun *
20 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.2 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.2 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.2 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.2 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.2 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.2 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.2 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.2 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.2 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.2 itojun * SUCH DAMAGE.
31 1.2 itojun */
32 1.25 lukem
33 1.25 lukem #include <sys/cdefs.h>
34 1.55 christos __KERNEL_RCSID(0, "$NetBSD: in_gif.c,v 1.55 2007/03/04 06:03:20 christos Exp $");
35 1.2 itojun
36 1.2 itojun #include "opt_inet.h"
37 1.18 itojun #include "opt_iso.h"
38 1.2 itojun
39 1.2 itojun #include <sys/param.h>
40 1.2 itojun #include <sys/systm.h>
41 1.2 itojun #include <sys/socket.h>
42 1.2 itojun #include <sys/sockio.h>
43 1.2 itojun #include <sys/mbuf.h>
44 1.2 itojun #include <sys/errno.h>
45 1.2 itojun #include <sys/ioctl.h>
46 1.16 itojun #include <sys/syslog.h>
47 1.26 itojun #include <sys/protosw.h>
48 1.45 mlelstv #include <sys/kernel.h>
49 1.12 itojun
50 1.2 itojun #include <net/if.h>
51 1.2 itojun #include <net/route.h>
52 1.2 itojun
53 1.2 itojun #include <netinet/in.h>
54 1.2 itojun #include <netinet/in_systm.h>
55 1.2 itojun #include <netinet/ip.h>
56 1.2 itojun #include <netinet/ip_var.h>
57 1.2 itojun #include <netinet/in_gif.h>
58 1.12 itojun #include <netinet/in_var.h>
59 1.12 itojun #include <netinet/ip_encap.h>
60 1.2 itojun #include <netinet/ip_ecn.h>
61 1.2 itojun
62 1.2 itojun #ifdef INET6
63 1.2 itojun #include <netinet/ip6.h>
64 1.2 itojun #endif
65 1.2 itojun
66 1.27 itojun #include <net/if_gif.h>
67 1.2 itojun
68 1.2 itojun #include "gif.h"
69 1.2 itojun
70 1.2 itojun #include <machine/stdarg.h>
71 1.7 itojun
72 1.7 itojun #include <net/net_osdep.h>
73 1.2 itojun
74 1.39 perry static int gif_validate4(const struct ip *, struct gif_softc *,
75 1.39 perry struct ifnet *);
76 1.22 itojun
77 1.2 itojun #if NGIF > 0
78 1.4 itojun int ip_gif_ttl = GIF_TTL;
79 1.2 itojun #else
80 1.4 itojun int ip_gif_ttl = 0;
81 1.7 itojun #endif
82 1.2 itojun
83 1.35 matt const struct protosw in_gif_protosw =
84 1.26 itojun { SOCK_RAW, &inetdomain, 0/* IPPROTO_IPV[46] */, PR_ATOMIC|PR_ADDR,
85 1.26 itojun in_gif_input, rip_output, 0, rip_ctloutput,
86 1.26 itojun rip_usrreq,
87 1.26 itojun 0, 0, 0, 0,
88 1.26 itojun };
89 1.22 itojun
90 1.2 itojun int
91 1.40 perry in_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
92 1.2 itojun {
93 1.16 itojun struct gif_softc *sc = (struct gif_softc*)ifp;
94 1.2 itojun struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
95 1.2 itojun struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
96 1.2 itojun struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
97 1.2 itojun struct ip iphdr; /* capsule IP header, host byte ordered */
98 1.2 itojun int proto, error;
99 1.2 itojun u_int8_t tos;
100 1.2 itojun
101 1.2 itojun if (sin_src == NULL || sin_dst == NULL ||
102 1.2 itojun sin_src->sin_family != AF_INET ||
103 1.2 itojun sin_dst->sin_family != AF_INET) {
104 1.2 itojun m_freem(m);
105 1.2 itojun return EAFNOSUPPORT;
106 1.2 itojun }
107 1.2 itojun
108 1.2 itojun switch (family) {
109 1.2 itojun #ifdef INET
110 1.2 itojun case AF_INET:
111 1.2 itojun {
112 1.48 dyoung const struct ip *ip;
113 1.2 itojun
114 1.2 itojun proto = IPPROTO_IPV4;
115 1.2 itojun if (m->m_len < sizeof(*ip)) {
116 1.2 itojun m = m_pullup(m, sizeof(*ip));
117 1.49 dyoung if (m == NULL)
118 1.2 itojun return ENOBUFS;
119 1.2 itojun }
120 1.48 dyoung ip = mtod(m, const struct ip *);
121 1.2 itojun tos = ip->ip_tos;
122 1.2 itojun break;
123 1.2 itojun }
124 1.26 itojun #endif /* INET */
125 1.2 itojun #ifdef INET6
126 1.2 itojun case AF_INET6:
127 1.2 itojun {
128 1.48 dyoung const struct ip6_hdr *ip6;
129 1.2 itojun proto = IPPROTO_IPV6;
130 1.2 itojun if (m->m_len < sizeof(*ip6)) {
131 1.2 itojun m = m_pullup(m, sizeof(*ip6));
132 1.2 itojun if (!m)
133 1.2 itojun return ENOBUFS;
134 1.2 itojun }
135 1.48 dyoung ip6 = mtod(m, const struct ip6_hdr *);
136 1.2 itojun tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
137 1.2 itojun break;
138 1.2 itojun }
139 1.26 itojun #endif /* INET6 */
140 1.19 itojun #ifdef ISO
141 1.19 itojun case AF_ISO:
142 1.19 itojun proto = IPPROTO_EON;
143 1.19 itojun tos = 0;
144 1.19 itojun break;
145 1.19 itojun #endif
146 1.2 itojun default:
147 1.9 itojun #ifdef DEBUG
148 1.2 itojun printf("in_gif_output: warning: unknown family %d passed\n",
149 1.2 itojun family);
150 1.2 itojun #endif
151 1.2 itojun m_freem(m);
152 1.2 itojun return EAFNOSUPPORT;
153 1.2 itojun }
154 1.2 itojun
155 1.16 itojun bzero(&iphdr, sizeof(iphdr));
156 1.2 itojun iphdr.ip_src = sin_src->sin_addr;
157 1.21 itojun /* bidirectional configured tunnel mode */
158 1.21 itojun if (sin_dst->sin_addr.s_addr != INADDR_ANY)
159 1.21 itojun iphdr.ip_dst = sin_dst->sin_addr;
160 1.21 itojun else {
161 1.21 itojun m_freem(m);
162 1.21 itojun return ENETUNREACH;
163 1.2 itojun }
164 1.2 itojun iphdr.ip_p = proto;
165 1.2 itojun /* version will be set in ip_output() */
166 1.4 itojun iphdr.ip_ttl = ip_gif_ttl;
167 1.29 itojun iphdr.ip_len = htons(m->m_pkthdr.len + sizeof(struct ip));
168 1.2 itojun if (ifp->if_flags & IFF_LINK1)
169 1.2 itojun ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos);
170 1.20 itojun else
171 1.20 itojun ip_ecn_ingress(ECN_NOCARE, &iphdr.ip_tos, &tos);
172 1.2 itojun
173 1.2 itojun /* prepend new IP header */
174 1.2 itojun M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
175 1.48 dyoung /* XXX Is m_pullup really necessary after M_PREPEND? */
176 1.50 dyoung if (m != NULL && M_UNWRITABLE(m, sizeof(struct ip)))
177 1.2 itojun m = m_pullup(m, sizeof(struct ip));
178 1.22 itojun if (m == NULL)
179 1.2 itojun return ENOBUFS;
180 1.12 itojun bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
181 1.2 itojun
182 1.52 dyoung if (dst->sin_family != sin_dst->sin_family ||
183 1.53 joerg !in_hosteq(dst->sin_addr, sin_dst->sin_addr))
184 1.53 joerg rtcache_free(&sc->gif_ro);
185 1.53 joerg else
186 1.53 joerg rtcache_check(&sc->gif_ro);
187 1.53 joerg if (sc->gif_ro.ro_rt == NULL) {
188 1.54 dyoung memset(dst, 0, sizeof(*dst));
189 1.2 itojun dst->sin_family = sin_dst->sin_family;
190 1.2 itojun dst->sin_len = sizeof(struct sockaddr_in);
191 1.2 itojun dst->sin_addr = sin_dst->sin_addr;
192 1.53 joerg rtcache_init(&sc->gif_ro);
193 1.2 itojun if (sc->gif_ro.ro_rt == NULL) {
194 1.2 itojun m_freem(m);
195 1.2 itojun return ENETUNREACH;
196 1.2 itojun }
197 1.53 joerg }
198 1.12 itojun
199 1.53 joerg /* If the route constitutes infinite encapsulation, punt. */
200 1.53 joerg if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
201 1.53 joerg rtcache_free(&sc->gif_ro);
202 1.53 joerg m_freem(m);
203 1.53 joerg return ENETUNREACH; /*XXX*/
204 1.2 itojun }
205 1.12 itojun
206 1.33 itojun error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
207 1.30 itojun return (error);
208 1.2 itojun }
209 1.2 itojun
210 1.2 itojun void
211 1.2 itojun in_gif_input(struct mbuf *m, ...)
212 1.2 itojun {
213 1.2 itojun int off, proto;
214 1.2 itojun struct ifnet *gifp = NULL;
215 1.49 dyoung const struct ip *ip;
216 1.18 itojun va_list ap;
217 1.12 itojun int af;
218 1.2 itojun u_int8_t otos;
219 1.2 itojun
220 1.2 itojun va_start(ap, m);
221 1.2 itojun off = va_arg(ap, int);
222 1.2 itojun proto = va_arg(ap, int);
223 1.2 itojun va_end(ap);
224 1.2 itojun
225 1.49 dyoung ip = mtod(m, const struct ip *);
226 1.2 itojun
227 1.12 itojun gifp = (struct ifnet *)encap_getarg(m);
228 1.2 itojun
229 1.12 itojun if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
230 1.2 itojun m_freem(m);
231 1.2 itojun ipstat.ips_nogif++;
232 1.2 itojun return;
233 1.2 itojun }
234 1.32 itojun #ifndef GIF_ENCAPCHECK
235 1.22 itojun if (!gif_validate4(ip, (struct gif_softc *)gifp, m->m_pkthdr.rcvif)) {
236 1.22 itojun m_freem(m);
237 1.22 itojun ipstat.ips_nogif++;
238 1.22 itojun return;
239 1.22 itojun }
240 1.22 itojun #endif
241 1.2 itojun
242 1.2 itojun otos = ip->ip_tos;
243 1.2 itojun m_adj(m, off);
244 1.2 itojun
245 1.2 itojun switch (proto) {
246 1.2 itojun #ifdef INET
247 1.2 itojun case IPPROTO_IPV4:
248 1.2 itojun {
249 1.42 christos struct ip *xip;
250 1.2 itojun af = AF_INET;
251 1.50 dyoung if (M_UNWRITABLE(m, sizeof(*xip))) {
252 1.49 dyoung if ((m = m_pullup(m, sizeof(*xip))) == NULL)
253 1.2 itojun return;
254 1.2 itojun }
255 1.42 christos xip = mtod(m, struct ip *);
256 1.2 itojun if (gifp->if_flags & IFF_LINK1)
257 1.42 christos ip_ecn_egress(ECN_ALLOWED, &otos, &xip->ip_tos);
258 1.20 itojun else
259 1.42 christos ip_ecn_egress(ECN_NOCARE, &otos, &xip->ip_tos);
260 1.2 itojun break;
261 1.2 itojun }
262 1.2 itojun #endif
263 1.2 itojun #ifdef INET6
264 1.2 itojun case IPPROTO_IPV6:
265 1.2 itojun {
266 1.2 itojun struct ip6_hdr *ip6;
267 1.2 itojun u_int8_t itos;
268 1.2 itojun af = AF_INET6;
269 1.50 dyoung if (M_UNWRITABLE(m, sizeof(*ip6))) {
270 1.49 dyoung if ((m = m_pullup(m, sizeof(*ip6))) == NULL)
271 1.2 itojun return;
272 1.2 itojun }
273 1.2 itojun ip6 = mtod(m, struct ip6_hdr *);
274 1.2 itojun itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
275 1.2 itojun if (gifp->if_flags & IFF_LINK1)
276 1.2 itojun ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
277 1.20 itojun else
278 1.20 itojun ip_ecn_egress(ECN_NOCARE, &otos, &itos);
279 1.2 itojun ip6->ip6_flow &= ~htonl(0xff << 20);
280 1.2 itojun ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
281 1.2 itojun break;
282 1.2 itojun }
283 1.2 itojun #endif /* INET6 */
284 1.18 itojun #ifdef ISO
285 1.18 itojun case IPPROTO_EON:
286 1.18 itojun af = AF_ISO;
287 1.18 itojun break;
288 1.18 itojun #endif
289 1.2 itojun default:
290 1.2 itojun ipstat.ips_nogif++;
291 1.2 itojun m_freem(m);
292 1.2 itojun return;
293 1.2 itojun }
294 1.2 itojun gif_input(m, af, gifp);
295 1.2 itojun return;
296 1.12 itojun }
297 1.12 itojun
298 1.12 itojun /*
299 1.22 itojun * validate outer address.
300 1.12 itojun */
301 1.22 itojun static int
302 1.40 perry gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
303 1.12 itojun {
304 1.12 itojun struct sockaddr_in *src, *dst;
305 1.12 itojun struct in_ifaddr *ia4;
306 1.12 itojun
307 1.12 itojun src = (struct sockaddr_in *)sc->gif_psrc;
308 1.12 itojun dst = (struct sockaddr_in *)sc->gif_pdst;
309 1.12 itojun
310 1.12 itojun /* check for address match */
311 1.22 itojun if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
312 1.22 itojun dst->sin_addr.s_addr != ip->ip_src.s_addr)
313 1.12 itojun return 0;
314 1.12 itojun
315 1.12 itojun /* martian filters on outer source - NOT done in ip_input! */
316 1.22 itojun if (IN_MULTICAST(ip->ip_src.s_addr))
317 1.12 itojun return 0;
318 1.22 itojun switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
319 1.12 itojun case 0: case 127: case 255:
320 1.12 itojun return 0;
321 1.12 itojun }
322 1.12 itojun /* reject packets with broadcast on source */
323 1.34 jonathan TAILQ_FOREACH(ia4, &in_ifaddrhead, ia_list) {
324 1.12 itojun if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
325 1.12 itojun continue;
326 1.22 itojun if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
327 1.12 itojun return 0;
328 1.12 itojun }
329 1.12 itojun
330 1.12 itojun /* ingress filters on outer source */
331 1.22 itojun if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
332 1.12 itojun struct sockaddr_in sin;
333 1.12 itojun struct rtentry *rt;
334 1.12 itojun
335 1.12 itojun bzero(&sin, sizeof(sin));
336 1.12 itojun sin.sin_family = AF_INET;
337 1.12 itojun sin.sin_len = sizeof(struct sockaddr_in);
338 1.22 itojun sin.sin_addr = ip->ip_src;
339 1.12 itojun rt = rtalloc1((struct sockaddr *)&sin, 0);
340 1.22 itojun if (!rt || rt->rt_ifp != ifp) {
341 1.16 itojun #if 0
342 1.16 itojun log(LOG_WARNING, "%s: packet from 0x%x dropped "
343 1.16 itojun "due to ingress filter\n", if_name(&sc->gif_if),
344 1.16 itojun (u_int32_t)ntohl(sin.sin_addr.s_addr));
345 1.16 itojun #endif
346 1.16 itojun if (rt)
347 1.16 itojun rtfree(rt);
348 1.12 itojun return 0;
349 1.12 itojun }
350 1.12 itojun rtfree(rt);
351 1.12 itojun }
352 1.12 itojun
353 1.21 itojun return 32 * 2;
354 1.22 itojun }
355 1.22 itojun
356 1.32 itojun #ifdef GIF_ENCAPCHECK
357 1.22 itojun /*
358 1.22 itojun * we know that we are in IFF_UP, outer address available, and outer family
359 1.22 itojun * matched the physical addr family. see gif_encapcheck().
360 1.22 itojun */
361 1.22 itojun int
362 1.44 tron gif_encapcheck4(struct mbuf *m, int off, int proto, void *arg)
363 1.22 itojun {
364 1.22 itojun struct ip ip;
365 1.22 itojun struct gif_softc *sc;
366 1.22 itojun struct ifnet *ifp;
367 1.22 itojun
368 1.22 itojun /* sanity check done in caller */
369 1.22 itojun sc = (struct gif_softc *)arg;
370 1.22 itojun
371 1.55 christos m_copydata(m, 0, sizeof(ip), (void *)&ip);
372 1.22 itojun ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
373 1.22 itojun
374 1.22 itojun return gif_validate4(&ip, sc, ifp);
375 1.22 itojun }
376 1.32 itojun #endif
377 1.22 itojun
378 1.22 itojun int
379 1.40 perry in_gif_attach(struct gif_softc *sc)
380 1.22 itojun {
381 1.32 itojun #ifndef GIF_ENCAPCHECK
382 1.22 itojun struct sockaddr_in mask4;
383 1.22 itojun
384 1.22 itojun bzero(&mask4, sizeof(mask4));
385 1.22 itojun mask4.sin_len = sizeof(struct sockaddr_in);
386 1.22 itojun mask4.sin_addr.s_addr = ~0;
387 1.22 itojun
388 1.22 itojun if (!sc->gif_psrc || !sc->gif_pdst)
389 1.22 itojun return EINVAL;
390 1.22 itojun sc->encap_cookie4 = encap_attach(AF_INET, -1, sc->gif_psrc,
391 1.22 itojun (struct sockaddr *)&mask4, sc->gif_pdst, (struct sockaddr *)&mask4,
392 1.42 christos (const struct protosw *)&in_gif_protosw, sc);
393 1.22 itojun #else
394 1.22 itojun sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
395 1.22 itojun &in_gif_protosw, sc);
396 1.22 itojun #endif
397 1.22 itojun if (sc->encap_cookie4 == NULL)
398 1.22 itojun return EEXIST;
399 1.22 itojun return 0;
400 1.22 itojun }
401 1.22 itojun
402 1.22 itojun int
403 1.40 perry in_gif_detach(struct gif_softc *sc)
404 1.22 itojun {
405 1.22 itojun int error;
406 1.22 itojun
407 1.22 itojun error = encap_detach(sc->encap_cookie4);
408 1.22 itojun if (error == 0)
409 1.22 itojun sc->encap_cookie4 = NULL;
410 1.45 mlelstv
411 1.53 joerg rtcache_free(&sc->gif_ro);
412 1.45 mlelstv
413 1.22 itojun return error;
414 1.2 itojun }
415