in_gif.c revision 1.16 1 1.16 itojun /* $NetBSD: in_gif.c,v 1.16 2001/01/22 07:51:01 itojun Exp $ */
2 1.16 itojun /* $KAME: in_gif.c,v 1.50 2001/01/22 07:27:16 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.2 itojun
33 1.2 itojun /*
34 1.2 itojun * in_gif.c
35 1.2 itojun */
36 1.2 itojun
37 1.12 itojun #ifdef __FreeBSD__
38 1.12 itojun #include "opt_mrouting.h"
39 1.12 itojun #endif
40 1.12 itojun #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
41 1.2 itojun #include "opt_inet.h"
42 1.12 itojun #endif
43 1.2 itojun
44 1.2 itojun #include <sys/param.h>
45 1.2 itojun #include <sys/systm.h>
46 1.2 itojun #include <sys/socket.h>
47 1.2 itojun #include <sys/sockio.h>
48 1.2 itojun #include <sys/mbuf.h>
49 1.2 itojun #include <sys/errno.h>
50 1.12 itojun #ifdef __FreeBSD__
51 1.12 itojun #include <sys/kernel.h>
52 1.12 itojun #include <sys/sysctl.h>
53 1.12 itojun #endif
54 1.12 itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
55 1.2 itojun #include <sys/ioctl.h>
56 1.12 itojun #endif
57 1.16 itojun #include <sys/syslog.h>
58 1.12 itojun
59 1.12 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
60 1.12 itojun #include <sys/malloc.h>
61 1.12 itojun #endif
62 1.2 itojun
63 1.2 itojun #include <net/if.h>
64 1.2 itojun #include <net/route.h>
65 1.2 itojun
66 1.2 itojun #include <netinet/in.h>
67 1.2 itojun #include <netinet/in_systm.h>
68 1.2 itojun #include <netinet/ip.h>
69 1.2 itojun #include <netinet/ip_var.h>
70 1.2 itojun #include <netinet/in_gif.h>
71 1.12 itojun #include <netinet/in_var.h>
72 1.12 itojun #include <netinet/ip_encap.h>
73 1.2 itojun #include <netinet/ip_ecn.h>
74 1.12 itojun #ifdef __OpenBSD__
75 1.12 itojun #include <netinet/ip_ipsp.h>
76 1.12 itojun #endif
77 1.2 itojun
78 1.2 itojun #ifdef INET6
79 1.2 itojun #include <netinet/ip6.h>
80 1.2 itojun #endif
81 1.2 itojun
82 1.2 itojun #ifdef MROUTING
83 1.2 itojun #include <netinet/ip_mroute.h>
84 1.2 itojun #endif /* MROUTING */
85 1.2 itojun
86 1.2 itojun #include <net/if_gif.h>
87 1.2 itojun
88 1.2 itojun #include "gif.h"
89 1.2 itojun
90 1.2 itojun #include <machine/stdarg.h>
91 1.7 itojun
92 1.7 itojun #include <net/net_osdep.h>
93 1.2 itojun
94 1.2 itojun #if NGIF > 0
95 1.4 itojun int ip_gif_ttl = GIF_TTL;
96 1.2 itojun #else
97 1.4 itojun int ip_gif_ttl = 0;
98 1.7 itojun #endif
99 1.12 itojun #ifdef __FreeBSD__
100 1.12 itojun SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_RW,
101 1.12 itojun &ip_gif_ttl, 0, "");
102 1.12 itojun #endif
103 1.2 itojun
104 1.2 itojun int
105 1.2 itojun in_gif_output(ifp, family, m, rt)
106 1.2 itojun struct ifnet *ifp;
107 1.2 itojun int family;
108 1.2 itojun struct mbuf *m;
109 1.2 itojun struct rtentry *rt;
110 1.2 itojun {
111 1.16 itojun struct gif_softc *sc = (struct gif_softc*)ifp;
112 1.2 itojun struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
113 1.2 itojun struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
114 1.2 itojun struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
115 1.2 itojun struct ip iphdr; /* capsule IP header, host byte ordered */
116 1.2 itojun int proto, error;
117 1.2 itojun u_int8_t tos;
118 1.2 itojun
119 1.2 itojun if (sin_src == NULL || sin_dst == NULL ||
120 1.2 itojun sin_src->sin_family != AF_INET ||
121 1.2 itojun sin_dst->sin_family != AF_INET) {
122 1.2 itojun m_freem(m);
123 1.2 itojun return EAFNOSUPPORT;
124 1.2 itojun }
125 1.2 itojun
126 1.2 itojun switch (family) {
127 1.2 itojun #ifdef INET
128 1.2 itojun case AF_INET:
129 1.2 itojun {
130 1.2 itojun struct ip *ip;
131 1.2 itojun
132 1.2 itojun proto = IPPROTO_IPV4;
133 1.2 itojun if (m->m_len < sizeof(*ip)) {
134 1.2 itojun m = m_pullup(m, sizeof(*ip));
135 1.2 itojun if (!m)
136 1.2 itojun return ENOBUFS;
137 1.2 itojun }
138 1.2 itojun ip = mtod(m, struct ip *);
139 1.2 itojun tos = ip->ip_tos;
140 1.15 thorpej
141 1.15 thorpej /* RFCs 1853, 2003, 2401 -- copy the DF bit. */
142 1.15 thorpej iphdr.ip_off |= (ntohs(ip->ip_off) & IP_DF);
143 1.2 itojun break;
144 1.2 itojun }
145 1.2 itojun #endif /*INET*/
146 1.2 itojun #ifdef INET6
147 1.2 itojun case AF_INET6:
148 1.2 itojun {
149 1.2 itojun struct ip6_hdr *ip6;
150 1.2 itojun proto = IPPROTO_IPV6;
151 1.2 itojun if (m->m_len < sizeof(*ip6)) {
152 1.2 itojun m = m_pullup(m, sizeof(*ip6));
153 1.2 itojun if (!m)
154 1.2 itojun return ENOBUFS;
155 1.2 itojun }
156 1.2 itojun ip6 = mtod(m, struct ip6_hdr *);
157 1.2 itojun tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
158 1.2 itojun break;
159 1.2 itojun }
160 1.2 itojun #endif /*INET6*/
161 1.2 itojun default:
162 1.9 itojun #ifdef DEBUG
163 1.2 itojun printf("in_gif_output: warning: unknown family %d passed\n",
164 1.2 itojun family);
165 1.2 itojun #endif
166 1.2 itojun m_freem(m);
167 1.2 itojun return EAFNOSUPPORT;
168 1.2 itojun }
169 1.2 itojun
170 1.16 itojun bzero(&iphdr, sizeof(iphdr));
171 1.2 itojun iphdr.ip_src = sin_src->sin_addr;
172 1.2 itojun if (ifp->if_flags & IFF_LINK0) {
173 1.2 itojun /* multi-destination mode */
174 1.2 itojun if (sin_dst->sin_addr.s_addr != INADDR_ANY)
175 1.2 itojun iphdr.ip_dst = sin_dst->sin_addr;
176 1.2 itojun else if (rt) {
177 1.7 itojun if (family != AF_INET) {
178 1.7 itojun m_freem(m);
179 1.7 itojun return EINVAL; /*XXX*/
180 1.7 itojun }
181 1.2 itojun iphdr.ip_dst = ((struct sockaddr_in *)
182 1.2 itojun (rt->rt_gateway))->sin_addr;
183 1.2 itojun } else {
184 1.2 itojun m_freem(m);
185 1.2 itojun return ENETUNREACH;
186 1.2 itojun }
187 1.2 itojun } else {
188 1.2 itojun /* bidirectional configured tunnel mode */
189 1.2 itojun if (sin_dst->sin_addr.s_addr != INADDR_ANY)
190 1.2 itojun iphdr.ip_dst = sin_dst->sin_addr;
191 1.2 itojun else {
192 1.2 itojun m_freem(m);
193 1.2 itojun return ENETUNREACH;
194 1.2 itojun }
195 1.2 itojun }
196 1.2 itojun iphdr.ip_p = proto;
197 1.2 itojun /* version will be set in ip_output() */
198 1.4 itojun iphdr.ip_ttl = ip_gif_ttl;
199 1.2 itojun iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip);
200 1.2 itojun if (ifp->if_flags & IFF_LINK1)
201 1.2 itojun ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos);
202 1.2 itojun
203 1.2 itojun /* prepend new IP header */
204 1.2 itojun M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
205 1.2 itojun if (m && m->m_len < sizeof(struct ip))
206 1.2 itojun m = m_pullup(m, sizeof(struct ip));
207 1.2 itojun if (m == NULL) {
208 1.2 itojun printf("ENOBUFS in in_gif_output %d\n", __LINE__);
209 1.2 itojun return ENOBUFS;
210 1.2 itojun }
211 1.12 itojun bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
212 1.2 itojun
213 1.2 itojun if (dst->sin_family != sin_dst->sin_family ||
214 1.2 itojun dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) {
215 1.2 itojun /* cache route doesn't match */
216 1.2 itojun dst->sin_family = sin_dst->sin_family;
217 1.2 itojun dst->sin_len = sizeof(struct sockaddr_in);
218 1.2 itojun dst->sin_addr = sin_dst->sin_addr;
219 1.2 itojun if (sc->gif_ro.ro_rt) {
220 1.2 itojun RTFREE(sc->gif_ro.ro_rt);
221 1.2 itojun sc->gif_ro.ro_rt = NULL;
222 1.2 itojun }
223 1.2 itojun #if 0
224 1.2 itojun sc->gif_if.if_mtu = GIF_MTU;
225 1.2 itojun #endif
226 1.2 itojun }
227 1.2 itojun
228 1.2 itojun if (sc->gif_ro.ro_rt == NULL) {
229 1.2 itojun rtalloc(&sc->gif_ro);
230 1.2 itojun if (sc->gif_ro.ro_rt == NULL) {
231 1.2 itojun m_freem(m);
232 1.2 itojun return ENETUNREACH;
233 1.2 itojun }
234 1.12 itojun
235 1.12 itojun /* if it constitutes infinite encapsulation, punt. */
236 1.12 itojun if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
237 1.12 itojun m_freem(m);
238 1.12 itojun return ENETUNREACH; /*XXX*/
239 1.12 itojun }
240 1.2 itojun #if 0
241 1.2 itojun ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
242 1.2 itojun - sizeof(struct ip);
243 1.12 itojun #endif
244 1.2 itojun }
245 1.12 itojun
246 1.7 itojun #ifndef __OpenBSD__
247 1.7 itojun error = ip_output(m, NULL, &sc->gif_ro, 0, NULL);
248 1.7 itojun #else
249 1.7 itojun error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
250 1.7 itojun #endif
251 1.2 itojun return(error);
252 1.2 itojun }
253 1.2 itojun
254 1.2 itojun void
255 1.2 itojun #if __STDC__
256 1.2 itojun in_gif_input(struct mbuf *m, ...)
257 1.2 itojun #else
258 1.2 itojun in_gif_input(m, va_alist)
259 1.2 itojun struct mbuf *m;
260 1.2 itojun va_dcl
261 1.2 itojun #endif
262 1.2 itojun {
263 1.2 itojun int off, proto;
264 1.2 itojun struct ifnet *gifp = NULL;
265 1.2 itojun struct ip *ip;
266 1.12 itojun int af;
267 1.2 itojun va_list ap;
268 1.2 itojun u_int8_t otos;
269 1.2 itojun
270 1.2 itojun va_start(ap, m);
271 1.2 itojun off = va_arg(ap, int);
272 1.12 itojun #ifndef __OpenBSD__
273 1.2 itojun proto = va_arg(ap, int);
274 1.12 itojun #endif
275 1.2 itojun va_end(ap);
276 1.2 itojun
277 1.2 itojun ip = mtod(m, struct ip *);
278 1.12 itojun #ifdef __OpenBSD__
279 1.12 itojun proto = ip->ip_p;
280 1.12 itojun #endif
281 1.2 itojun
282 1.12 itojun gifp = (struct ifnet *)encap_getarg(m);
283 1.2 itojun
284 1.12 itojun if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
285 1.2 itojun m_freem(m);
286 1.2 itojun ipstat.ips_nogif++;
287 1.2 itojun return;
288 1.2 itojun }
289 1.2 itojun
290 1.2 itojun otos = ip->ip_tos;
291 1.2 itojun m_adj(m, off);
292 1.2 itojun
293 1.2 itojun switch (proto) {
294 1.2 itojun #ifdef INET
295 1.2 itojun case IPPROTO_IPV4:
296 1.2 itojun {
297 1.2 itojun struct ip *ip;
298 1.2 itojun af = AF_INET;
299 1.2 itojun if (m->m_len < sizeof(*ip)) {
300 1.2 itojun m = m_pullup(m, sizeof(*ip));
301 1.2 itojun if (!m)
302 1.2 itojun return;
303 1.2 itojun }
304 1.2 itojun ip = mtod(m, struct ip *);
305 1.2 itojun if (gifp->if_flags & IFF_LINK1)
306 1.2 itojun ip_ecn_egress(ECN_ALLOWED, &otos, &ip->ip_tos);
307 1.2 itojun break;
308 1.2 itojun }
309 1.2 itojun #endif
310 1.2 itojun #ifdef INET6
311 1.2 itojun case IPPROTO_IPV6:
312 1.2 itojun {
313 1.2 itojun struct ip6_hdr *ip6;
314 1.2 itojun u_int8_t itos;
315 1.2 itojun af = AF_INET6;
316 1.2 itojun if (m->m_len < sizeof(*ip6)) {
317 1.2 itojun m = m_pullup(m, sizeof(*ip6));
318 1.2 itojun if (!m)
319 1.2 itojun return;
320 1.2 itojun }
321 1.2 itojun ip6 = mtod(m, struct ip6_hdr *);
322 1.2 itojun itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
323 1.2 itojun if (gifp->if_flags & IFF_LINK1)
324 1.2 itojun ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
325 1.2 itojun ip6->ip6_flow &= ~htonl(0xff << 20);
326 1.2 itojun ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
327 1.2 itojun break;
328 1.2 itojun }
329 1.2 itojun #endif /* INET6 */
330 1.2 itojun default:
331 1.2 itojun ipstat.ips_nogif++;
332 1.2 itojun m_freem(m);
333 1.2 itojun return;
334 1.2 itojun }
335 1.2 itojun gif_input(m, af, gifp);
336 1.2 itojun return;
337 1.12 itojun }
338 1.12 itojun
339 1.12 itojun /*
340 1.12 itojun * we know that we are in IFF_UP, outer address available, and outer family
341 1.12 itojun * matched the physical addr family. see gif_encapcheck().
342 1.12 itojun */
343 1.12 itojun int
344 1.12 itojun gif_encapcheck4(m, off, proto, arg)
345 1.12 itojun const struct mbuf *m;
346 1.12 itojun int off;
347 1.12 itojun int proto;
348 1.12 itojun void *arg;
349 1.12 itojun {
350 1.12 itojun struct ip ip;
351 1.12 itojun struct gif_softc *sc;
352 1.12 itojun struct sockaddr_in *src, *dst;
353 1.12 itojun int addrmatch;
354 1.12 itojun struct in_ifaddr *ia4;
355 1.12 itojun
356 1.12 itojun /* sanity check done in caller */
357 1.12 itojun sc = (struct gif_softc *)arg;
358 1.12 itojun src = (struct sockaddr_in *)sc->gif_psrc;
359 1.12 itojun dst = (struct sockaddr_in *)sc->gif_pdst;
360 1.12 itojun
361 1.12 itojun /* LINTED const cast */
362 1.12 itojun m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
363 1.12 itojun
364 1.12 itojun /* check for address match */
365 1.12 itojun addrmatch = 0;
366 1.12 itojun if (src->sin_addr.s_addr == ip.ip_dst.s_addr)
367 1.12 itojun addrmatch |= 1;
368 1.12 itojun if (dst->sin_addr.s_addr == ip.ip_src.s_addr)
369 1.12 itojun addrmatch |= 2;
370 1.12 itojun else if ((sc->gif_if.if_flags & IFF_LINK0) != 0 &&
371 1.12 itojun dst->sin_addr.s_addr == INADDR_ANY) {
372 1.12 itojun addrmatch |= 2; /* we accept any source */
373 1.12 itojun }
374 1.12 itojun if (addrmatch != 3)
375 1.12 itojun return 0;
376 1.12 itojun
377 1.12 itojun /* martian filters on outer source - NOT done in ip_input! */
378 1.13 enami if (IN_MULTICAST(ip.ip_src.s_addr))
379 1.12 itojun return 0;
380 1.12 itojun switch ((ntohl(ip.ip_src.s_addr) & 0xff000000) >> 24) {
381 1.12 itojun case 0: case 127: case 255:
382 1.12 itojun return 0;
383 1.12 itojun }
384 1.12 itojun /* reject packets with broadcast on source */
385 1.12 itojun #if defined(__OpenBSD__) || defined(__NetBSD__)
386 1.12 itojun for (ia4 = in_ifaddr.tqh_first; ia4; ia4 = ia4->ia_list.tqe_next)
387 1.14 itojun #elif (defined(__FreeBSD__) && __FreeBSD__ >= 3)
388 1.14 itojun for (ia4 = TAILQ_FIRST(&in_ifaddrhead); ia4;
389 1.14 itojun ia4 = TAILQ_NEXT(ia4, ia_link))
390 1.12 itojun #else
391 1.12 itojun for (ia4 = in_ifaddr; ia4 != NULL; ia4 = ia4->ia_next)
392 1.12 itojun #endif
393 1.12 itojun {
394 1.12 itojun if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
395 1.12 itojun continue;
396 1.12 itojun if (ip.ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
397 1.12 itojun return 0;
398 1.12 itojun }
399 1.12 itojun
400 1.12 itojun /* ingress filters on outer source */
401 1.16 itojun if ((sc->gif_if.if_flags & IFF_LINK2) == 0 &&
402 1.16 itojun (m->m_flags & M_PKTHDR) != 0 && m->m_pkthdr.rcvif) {
403 1.12 itojun struct sockaddr_in sin;
404 1.12 itojun struct rtentry *rt;
405 1.12 itojun
406 1.12 itojun bzero(&sin, sizeof(sin));
407 1.12 itojun sin.sin_family = AF_INET;
408 1.12 itojun sin.sin_len = sizeof(struct sockaddr_in);
409 1.12 itojun sin.sin_addr = ip.ip_src;
410 1.12 itojun #ifdef __FreeBSD__
411 1.12 itojun rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
412 1.12 itojun #else
413 1.12 itojun rt = rtalloc1((struct sockaddr *)&sin, 0);
414 1.12 itojun #endif
415 1.16 itojun if (!rt || rt->rt_ifp != m->m_pkthdr.rcvif) {
416 1.16 itojun #if 0
417 1.16 itojun log(LOG_WARNING, "%s: packet from 0x%x dropped "
418 1.16 itojun "due to ingress filter\n", if_name(&sc->gif_if),
419 1.16 itojun (u_int32_t)ntohl(sin.sin_addr.s_addr));
420 1.16 itojun #endif
421 1.16 itojun if (rt)
422 1.16 itojun rtfree(rt);
423 1.12 itojun return 0;
424 1.12 itojun }
425 1.12 itojun rtfree(rt);
426 1.12 itojun }
427 1.12 itojun
428 1.12 itojun /* prioritize: IFF_LINK0 mode is less preferred */
429 1.12 itojun return (sc->gif_if.if_flags & IFF_LINK0) ? 32 : 32 * 2;
430 1.2 itojun }
431