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