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