in_gif.c revision 1.48 1 1.48 dyoung /* $NetBSD: in_gif.c,v 1.48 2006/07/28 16:30:55 dyoung 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.48 dyoung __KERNEL_RCSID(0, "$NetBSD: in_gif.c,v 1.48 2006/07/28 16:30:55 dyoung 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.37 kim #include "bridge.h"
70 1.37 kim #include <net/if_ether.h>
71 1.2 itojun
72 1.2 itojun #include <machine/stdarg.h>
73 1.7 itojun
74 1.7 itojun #include <net/net_osdep.h>
75 1.2 itojun
76 1.39 perry static int gif_validate4(const struct ip *, struct gif_softc *,
77 1.39 perry struct ifnet *);
78 1.22 itojun
79 1.2 itojun #if NGIF > 0
80 1.4 itojun int ip_gif_ttl = GIF_TTL;
81 1.2 itojun #else
82 1.4 itojun int ip_gif_ttl = 0;
83 1.7 itojun #endif
84 1.2 itojun
85 1.35 matt const struct protosw in_gif_protosw =
86 1.26 itojun { SOCK_RAW, &inetdomain, 0/* IPPROTO_IPV[46] */, PR_ATOMIC|PR_ADDR,
87 1.26 itojun in_gif_input, rip_output, 0, rip_ctloutput,
88 1.26 itojun rip_usrreq,
89 1.26 itojun 0, 0, 0, 0,
90 1.26 itojun };
91 1.22 itojun
92 1.2 itojun int
93 1.40 perry in_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
94 1.2 itojun {
95 1.16 itojun struct gif_softc *sc = (struct gif_softc*)ifp;
96 1.2 itojun struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
97 1.2 itojun struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
98 1.2 itojun struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
99 1.2 itojun struct ip iphdr; /* capsule IP header, host byte ordered */
100 1.38 he #if NBRIDGE > 0
101 1.37 kim struct etherip_header eiphdr;
102 1.38 he #endif
103 1.2 itojun int proto, error;
104 1.2 itojun u_int8_t tos;
105 1.2 itojun
106 1.2 itojun if (sin_src == NULL || sin_dst == NULL ||
107 1.2 itojun sin_src->sin_family != AF_INET ||
108 1.2 itojun sin_dst->sin_family != AF_INET) {
109 1.2 itojun m_freem(m);
110 1.2 itojun return EAFNOSUPPORT;
111 1.2 itojun }
112 1.2 itojun
113 1.2 itojun switch (family) {
114 1.2 itojun #ifdef INET
115 1.2 itojun case AF_INET:
116 1.2 itojun {
117 1.48 dyoung const struct ip *ip;
118 1.2 itojun
119 1.2 itojun proto = IPPROTO_IPV4;
120 1.2 itojun if (m->m_len < sizeof(*ip)) {
121 1.2 itojun m = m_pullup(m, sizeof(*ip));
122 1.2 itojun if (!m)
123 1.2 itojun return ENOBUFS;
124 1.2 itojun }
125 1.48 dyoung ip = mtod(m, const struct ip *);
126 1.2 itojun tos = ip->ip_tos;
127 1.2 itojun break;
128 1.2 itojun }
129 1.26 itojun #endif /* INET */
130 1.2 itojun #ifdef INET6
131 1.2 itojun case AF_INET6:
132 1.2 itojun {
133 1.48 dyoung const struct ip6_hdr *ip6;
134 1.2 itojun proto = IPPROTO_IPV6;
135 1.2 itojun if (m->m_len < sizeof(*ip6)) {
136 1.2 itojun m = m_pullup(m, sizeof(*ip6));
137 1.2 itojun if (!m)
138 1.2 itojun return ENOBUFS;
139 1.2 itojun }
140 1.48 dyoung ip6 = mtod(m, const struct ip6_hdr *);
141 1.2 itojun tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
142 1.2 itojun break;
143 1.2 itojun }
144 1.26 itojun #endif /* INET6 */
145 1.19 itojun #ifdef ISO
146 1.19 itojun case AF_ISO:
147 1.19 itojun proto = IPPROTO_EON;
148 1.19 itojun tos = 0;
149 1.19 itojun break;
150 1.19 itojun #endif
151 1.37 kim #if NBRIDGE > 0
152 1.37 kim case AF_LINK:
153 1.37 kim proto = IPPROTO_ETHERIP;
154 1.37 kim eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK;
155 1.37 kim eiphdr.eip_pad = 0;
156 1.37 kim /* prepend Ethernet-in-IP header */
157 1.37 kim M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
158 1.37 kim if (m && m->m_len < sizeof(struct etherip_header))
159 1.37 kim m = m_pullup(m, sizeof(struct etherip_header));
160 1.37 kim if (m == NULL)
161 1.37 kim return ENOBUFS;
162 1.37 kim bcopy(&eiphdr, mtod(m, struct etherip_header *), sizeof(struct etherip_header));
163 1.37 kim break;
164 1.37 kim #endif
165 1.2 itojun default:
166 1.9 itojun #ifdef DEBUG
167 1.2 itojun printf("in_gif_output: warning: unknown family %d passed\n",
168 1.2 itojun family);
169 1.2 itojun #endif
170 1.2 itojun m_freem(m);
171 1.2 itojun return EAFNOSUPPORT;
172 1.2 itojun }
173 1.2 itojun
174 1.16 itojun bzero(&iphdr, sizeof(iphdr));
175 1.2 itojun iphdr.ip_src = sin_src->sin_addr;
176 1.21 itojun /* bidirectional configured tunnel mode */
177 1.21 itojun if (sin_dst->sin_addr.s_addr != INADDR_ANY)
178 1.21 itojun iphdr.ip_dst = sin_dst->sin_addr;
179 1.21 itojun else {
180 1.21 itojun m_freem(m);
181 1.21 itojun return ENETUNREACH;
182 1.2 itojun }
183 1.2 itojun iphdr.ip_p = proto;
184 1.2 itojun /* version will be set in ip_output() */
185 1.4 itojun iphdr.ip_ttl = ip_gif_ttl;
186 1.29 itojun iphdr.ip_len = htons(m->m_pkthdr.len + sizeof(struct ip));
187 1.2 itojun if (ifp->if_flags & IFF_LINK1)
188 1.2 itojun ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos);
189 1.20 itojun else
190 1.20 itojun ip_ecn_ingress(ECN_NOCARE, &iphdr.ip_tos, &tos);
191 1.2 itojun
192 1.2 itojun /* prepend new IP header */
193 1.2 itojun M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
194 1.48 dyoung /* XXX Is m_pullup really necessary after M_PREPEND? */
195 1.2 itojun if (m && m->m_len < sizeof(struct ip))
196 1.2 itojun m = m_pullup(m, sizeof(struct ip));
197 1.22 itojun if (m == NULL)
198 1.2 itojun return ENOBUFS;
199 1.12 itojun bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
200 1.2 itojun
201 1.47 kardel if (sc->gif_route_expire - time_second <= 0 ||
202 1.45 mlelstv dst->sin_family != sin_dst->sin_family ||
203 1.45 mlelstv !in_hosteq(dst->sin_addr, sin_dst->sin_addr)) {
204 1.2 itojun /* cache route doesn't match */
205 1.28 itojun bzero(dst, sizeof(*dst));
206 1.2 itojun dst->sin_family = sin_dst->sin_family;
207 1.2 itojun dst->sin_len = sizeof(struct sockaddr_in);
208 1.2 itojun dst->sin_addr = sin_dst->sin_addr;
209 1.2 itojun if (sc->gif_ro.ro_rt) {
210 1.2 itojun RTFREE(sc->gif_ro.ro_rt);
211 1.2 itojun sc->gif_ro.ro_rt = NULL;
212 1.2 itojun }
213 1.2 itojun }
214 1.2 itojun
215 1.2 itojun if (sc->gif_ro.ro_rt == NULL) {
216 1.2 itojun rtalloc(&sc->gif_ro);
217 1.2 itojun if (sc->gif_ro.ro_rt == NULL) {
218 1.2 itojun m_freem(m);
219 1.2 itojun return ENETUNREACH;
220 1.2 itojun }
221 1.12 itojun
222 1.12 itojun /* if it constitutes infinite encapsulation, punt. */
223 1.12 itojun if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
224 1.12 itojun m_freem(m);
225 1.12 itojun return ENETUNREACH; /*XXX*/
226 1.12 itojun }
227 1.45 mlelstv
228 1.47 kardel sc->gif_route_expire = time_second + GIF_ROUTE_TTL;
229 1.2 itojun }
230 1.12 itojun
231 1.33 itojun error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
232 1.30 itojun return (error);
233 1.2 itojun }
234 1.2 itojun
235 1.2 itojun void
236 1.2 itojun in_gif_input(struct mbuf *m, ...)
237 1.2 itojun {
238 1.2 itojun int off, proto;
239 1.2 itojun struct ifnet *gifp = NULL;
240 1.2 itojun struct ip *ip;
241 1.18 itojun va_list ap;
242 1.12 itojun int af;
243 1.2 itojun u_int8_t otos;
244 1.2 itojun
245 1.2 itojun va_start(ap, m);
246 1.2 itojun off = va_arg(ap, int);
247 1.2 itojun proto = va_arg(ap, int);
248 1.2 itojun va_end(ap);
249 1.2 itojun
250 1.2 itojun ip = mtod(m, struct ip *);
251 1.2 itojun
252 1.12 itojun gifp = (struct ifnet *)encap_getarg(m);
253 1.2 itojun
254 1.12 itojun if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
255 1.2 itojun m_freem(m);
256 1.2 itojun ipstat.ips_nogif++;
257 1.2 itojun return;
258 1.2 itojun }
259 1.32 itojun #ifndef GIF_ENCAPCHECK
260 1.22 itojun if (!gif_validate4(ip, (struct gif_softc *)gifp, m->m_pkthdr.rcvif)) {
261 1.22 itojun m_freem(m);
262 1.22 itojun ipstat.ips_nogif++;
263 1.22 itojun return;
264 1.22 itojun }
265 1.22 itojun #endif
266 1.2 itojun
267 1.2 itojun otos = ip->ip_tos;
268 1.2 itojun m_adj(m, off);
269 1.2 itojun
270 1.2 itojun switch (proto) {
271 1.2 itojun #ifdef INET
272 1.2 itojun case IPPROTO_IPV4:
273 1.2 itojun {
274 1.42 christos struct ip *xip;
275 1.2 itojun af = AF_INET;
276 1.42 christos if (m->m_len < sizeof(*xip)) {
277 1.42 christos m = m_pullup(m, sizeof(*xip));
278 1.2 itojun if (!m)
279 1.2 itojun return;
280 1.2 itojun }
281 1.42 christos xip = mtod(m, struct ip *);
282 1.2 itojun if (gifp->if_flags & IFF_LINK1)
283 1.42 christos ip_ecn_egress(ECN_ALLOWED, &otos, &xip->ip_tos);
284 1.20 itojun else
285 1.42 christos ip_ecn_egress(ECN_NOCARE, &otos, &xip->ip_tos);
286 1.2 itojun break;
287 1.2 itojun }
288 1.2 itojun #endif
289 1.2 itojun #ifdef INET6
290 1.2 itojun case IPPROTO_IPV6:
291 1.2 itojun {
292 1.2 itojun struct ip6_hdr *ip6;
293 1.2 itojun u_int8_t itos;
294 1.2 itojun af = AF_INET6;
295 1.2 itojun if (m->m_len < sizeof(*ip6)) {
296 1.2 itojun m = m_pullup(m, sizeof(*ip6));
297 1.2 itojun if (!m)
298 1.2 itojun return;
299 1.2 itojun }
300 1.2 itojun ip6 = mtod(m, struct ip6_hdr *);
301 1.2 itojun itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
302 1.2 itojun if (gifp->if_flags & IFF_LINK1)
303 1.2 itojun ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
304 1.20 itojun else
305 1.20 itojun ip_ecn_egress(ECN_NOCARE, &otos, &itos);
306 1.2 itojun ip6->ip6_flow &= ~htonl(0xff << 20);
307 1.2 itojun ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
308 1.2 itojun break;
309 1.2 itojun }
310 1.2 itojun #endif /* INET6 */
311 1.18 itojun #ifdef ISO
312 1.18 itojun case IPPROTO_EON:
313 1.18 itojun af = AF_ISO;
314 1.18 itojun break;
315 1.18 itojun #endif
316 1.37 kim #if NBRIDGE > 0
317 1.37 kim case IPPROTO_ETHERIP:
318 1.37 kim af = AF_LINK;
319 1.41 perry break;
320 1.37 kim #endif
321 1.2 itojun default:
322 1.2 itojun ipstat.ips_nogif++;
323 1.2 itojun m_freem(m);
324 1.2 itojun return;
325 1.2 itojun }
326 1.2 itojun gif_input(m, af, gifp);
327 1.2 itojun return;
328 1.12 itojun }
329 1.12 itojun
330 1.12 itojun /*
331 1.22 itojun * validate outer address.
332 1.12 itojun */
333 1.22 itojun static int
334 1.40 perry gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
335 1.12 itojun {
336 1.12 itojun struct sockaddr_in *src, *dst;
337 1.12 itojun struct in_ifaddr *ia4;
338 1.12 itojun
339 1.12 itojun src = (struct sockaddr_in *)sc->gif_psrc;
340 1.12 itojun dst = (struct sockaddr_in *)sc->gif_pdst;
341 1.12 itojun
342 1.12 itojun /* check for address match */
343 1.22 itojun if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
344 1.22 itojun dst->sin_addr.s_addr != ip->ip_src.s_addr)
345 1.12 itojun return 0;
346 1.12 itojun
347 1.12 itojun /* martian filters on outer source - NOT done in ip_input! */
348 1.22 itojun if (IN_MULTICAST(ip->ip_src.s_addr))
349 1.12 itojun return 0;
350 1.22 itojun switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
351 1.12 itojun case 0: case 127: case 255:
352 1.12 itojun return 0;
353 1.12 itojun }
354 1.12 itojun /* reject packets with broadcast on source */
355 1.34 jonathan TAILQ_FOREACH(ia4, &in_ifaddrhead, ia_list) {
356 1.12 itojun if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
357 1.12 itojun continue;
358 1.22 itojun if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
359 1.12 itojun return 0;
360 1.12 itojun }
361 1.12 itojun
362 1.12 itojun /* ingress filters on outer source */
363 1.22 itojun if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
364 1.12 itojun struct sockaddr_in sin;
365 1.12 itojun struct rtentry *rt;
366 1.12 itojun
367 1.12 itojun bzero(&sin, sizeof(sin));
368 1.12 itojun sin.sin_family = AF_INET;
369 1.12 itojun sin.sin_len = sizeof(struct sockaddr_in);
370 1.22 itojun sin.sin_addr = ip->ip_src;
371 1.12 itojun rt = rtalloc1((struct sockaddr *)&sin, 0);
372 1.22 itojun if (!rt || rt->rt_ifp != ifp) {
373 1.16 itojun #if 0
374 1.16 itojun log(LOG_WARNING, "%s: packet from 0x%x dropped "
375 1.16 itojun "due to ingress filter\n", if_name(&sc->gif_if),
376 1.16 itojun (u_int32_t)ntohl(sin.sin_addr.s_addr));
377 1.16 itojun #endif
378 1.16 itojun if (rt)
379 1.16 itojun rtfree(rt);
380 1.12 itojun return 0;
381 1.12 itojun }
382 1.12 itojun rtfree(rt);
383 1.12 itojun }
384 1.12 itojun
385 1.21 itojun return 32 * 2;
386 1.22 itojun }
387 1.22 itojun
388 1.32 itojun #ifdef GIF_ENCAPCHECK
389 1.22 itojun /*
390 1.22 itojun * we know that we are in IFF_UP, outer address available, and outer family
391 1.22 itojun * matched the physical addr family. see gif_encapcheck().
392 1.22 itojun */
393 1.22 itojun int
394 1.44 tron gif_encapcheck4(struct mbuf *m, int off, int proto, void *arg)
395 1.22 itojun {
396 1.22 itojun struct ip ip;
397 1.22 itojun struct gif_softc *sc;
398 1.22 itojun struct ifnet *ifp;
399 1.22 itojun
400 1.22 itojun /* sanity check done in caller */
401 1.22 itojun sc = (struct gif_softc *)arg;
402 1.22 itojun
403 1.43 tron m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
404 1.22 itojun ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
405 1.22 itojun
406 1.22 itojun return gif_validate4(&ip, sc, ifp);
407 1.22 itojun }
408 1.32 itojun #endif
409 1.22 itojun
410 1.22 itojun int
411 1.40 perry in_gif_attach(struct gif_softc *sc)
412 1.22 itojun {
413 1.32 itojun #ifndef GIF_ENCAPCHECK
414 1.22 itojun struct sockaddr_in mask4;
415 1.22 itojun
416 1.22 itojun bzero(&mask4, sizeof(mask4));
417 1.22 itojun mask4.sin_len = sizeof(struct sockaddr_in);
418 1.22 itojun mask4.sin_addr.s_addr = ~0;
419 1.22 itojun
420 1.22 itojun if (!sc->gif_psrc || !sc->gif_pdst)
421 1.22 itojun return EINVAL;
422 1.22 itojun sc->encap_cookie4 = encap_attach(AF_INET, -1, sc->gif_psrc,
423 1.22 itojun (struct sockaddr *)&mask4, sc->gif_pdst, (struct sockaddr *)&mask4,
424 1.42 christos (const struct protosw *)&in_gif_protosw, sc);
425 1.22 itojun #else
426 1.22 itojun sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
427 1.22 itojun &in_gif_protosw, sc);
428 1.22 itojun #endif
429 1.22 itojun if (sc->encap_cookie4 == NULL)
430 1.22 itojun return EEXIST;
431 1.22 itojun return 0;
432 1.22 itojun }
433 1.22 itojun
434 1.22 itojun int
435 1.40 perry in_gif_detach(struct gif_softc *sc)
436 1.22 itojun {
437 1.22 itojun int error;
438 1.22 itojun
439 1.22 itojun error = encap_detach(sc->encap_cookie4);
440 1.22 itojun if (error == 0)
441 1.22 itojun sc->encap_cookie4 = NULL;
442 1.45 mlelstv
443 1.45 mlelstv if (sc->gif_ro.ro_rt) {
444 1.45 mlelstv RTFREE(sc->gif_ro.ro_rt);
445 1.45 mlelstv sc->gif_ro.ro_rt = NULL;
446 1.45 mlelstv }
447 1.45 mlelstv
448 1.22 itojun return error;
449 1.2 itojun }
450