in_gif.c revision 1.29 1 /* $NetBSD: in_gif.c,v 1.29 2002/08/14 00:23:29 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.29 2002/08/14 00:23:29 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 = htons(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 bzero(dst, sizeof(*dst));
191 dst->sin_family = sin_dst->sin_family;
192 dst->sin_len = sizeof(struct sockaddr_in);
193 dst->sin_addr = sin_dst->sin_addr;
194 if (sc->gif_ro.ro_rt) {
195 RTFREE(sc->gif_ro.ro_rt);
196 sc->gif_ro.ro_rt = NULL;
197 }
198 }
199
200 if (sc->gif_ro.ro_rt == NULL) {
201 rtalloc(&sc->gif_ro);
202 if (sc->gif_ro.ro_rt == NULL) {
203 m_freem(m);
204 return ENETUNREACH;
205 }
206
207 /* if it constitutes infinite encapsulation, punt. */
208 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
209 m_freem(m);
210 return ENETUNREACH; /*XXX*/
211 }
212 }
213
214 error = ip_output(m, NULL, &sc->gif_ro, 0, NULL);
215 return(error);
216 }
217
218 void
219 #if __STDC__
220 in_gif_input(struct mbuf *m, ...)
221 #else
222 in_gif_input(m, va_alist)
223 struct mbuf *m;
224 va_dcl
225 #endif
226 {
227 int off, proto;
228 struct ifnet *gifp = NULL;
229 struct ip *ip;
230 va_list ap;
231 int af;
232 u_int8_t otos;
233
234 va_start(ap, m);
235 off = va_arg(ap, int);
236 proto = va_arg(ap, int);
237 va_end(ap);
238
239 ip = mtod(m, struct ip *);
240
241 gifp = (struct ifnet *)encap_getarg(m);
242
243 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
244 m_freem(m);
245 ipstat.ips_nogif++;
246 return;
247 }
248 #ifndef USE_ENCAPCHECK
249 if (!gif_validate4(ip, (struct gif_softc *)gifp, m->m_pkthdr.rcvif)) {
250 m_freem(m);
251 ipstat.ips_nogif++;
252 return;
253 }
254 #endif
255
256 otos = ip->ip_tos;
257 m_adj(m, off);
258
259 switch (proto) {
260 #ifdef INET
261 case IPPROTO_IPV4:
262 {
263 struct ip *ip;
264 af = AF_INET;
265 if (m->m_len < sizeof(*ip)) {
266 m = m_pullup(m, sizeof(*ip));
267 if (!m)
268 return;
269 }
270 ip = mtod(m, struct ip *);
271 if (gifp->if_flags & IFF_LINK1)
272 ip_ecn_egress(ECN_ALLOWED, &otos, &ip->ip_tos);
273 else
274 ip_ecn_egress(ECN_NOCARE, &otos, &ip->ip_tos);
275 break;
276 }
277 #endif
278 #ifdef INET6
279 case IPPROTO_IPV6:
280 {
281 struct ip6_hdr *ip6;
282 u_int8_t itos;
283 af = AF_INET6;
284 if (m->m_len < sizeof(*ip6)) {
285 m = m_pullup(m, sizeof(*ip6));
286 if (!m)
287 return;
288 }
289 ip6 = mtod(m, struct ip6_hdr *);
290 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
291 if (gifp->if_flags & IFF_LINK1)
292 ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
293 else
294 ip_ecn_egress(ECN_NOCARE, &otos, &itos);
295 ip6->ip6_flow &= ~htonl(0xff << 20);
296 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
297 break;
298 }
299 #endif /* INET6 */
300 #ifdef ISO
301 case IPPROTO_EON:
302 af = AF_ISO;
303 break;
304 #endif
305 default:
306 ipstat.ips_nogif++;
307 m_freem(m);
308 return;
309 }
310 gif_input(m, af, gifp);
311 return;
312 }
313
314 /*
315 * validate outer address.
316 */
317 static int
318 gif_validate4(ip, sc, ifp)
319 const struct ip *ip;
320 struct gif_softc *sc;
321 struct ifnet *ifp;
322 {
323 struct sockaddr_in *src, *dst;
324 struct in_ifaddr *ia4;
325
326 src = (struct sockaddr_in *)sc->gif_psrc;
327 dst = (struct sockaddr_in *)sc->gif_pdst;
328
329 /* check for address match */
330 if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
331 dst->sin_addr.s_addr != ip->ip_src.s_addr)
332 return 0;
333
334 /* martian filters on outer source - NOT done in ip_input! */
335 if (IN_MULTICAST(ip->ip_src.s_addr))
336 return 0;
337 switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
338 case 0: case 127: case 255:
339 return 0;
340 }
341 /* reject packets with broadcast on source */
342 TAILQ_FOREACH(ia4, &in_ifaddr, ia_list) {
343 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
344 continue;
345 if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
346 return 0;
347 }
348
349 /* ingress filters on outer source */
350 if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
351 struct sockaddr_in sin;
352 struct rtentry *rt;
353
354 bzero(&sin, sizeof(sin));
355 sin.sin_family = AF_INET;
356 sin.sin_len = sizeof(struct sockaddr_in);
357 sin.sin_addr = ip->ip_src;
358 rt = rtalloc1((struct sockaddr *)&sin, 0);
359 if (!rt || rt->rt_ifp != ifp) {
360 #if 0
361 log(LOG_WARNING, "%s: packet from 0x%x dropped "
362 "due to ingress filter\n", if_name(&sc->gif_if),
363 (u_int32_t)ntohl(sin.sin_addr.s_addr));
364 #endif
365 if (rt)
366 rtfree(rt);
367 return 0;
368 }
369 rtfree(rt);
370 }
371
372 return 32 * 2;
373 }
374
375 /*
376 * we know that we are in IFF_UP, outer address available, and outer family
377 * matched the physical addr family. see gif_encapcheck().
378 */
379 int
380 gif_encapcheck4(m, off, proto, arg)
381 const struct mbuf *m;
382 int off;
383 int proto;
384 void *arg;
385 {
386 struct ip ip;
387 struct gif_softc *sc;
388 struct ifnet *ifp;
389
390 /* sanity check done in caller */
391 sc = (struct gif_softc *)arg;
392
393 /* LINTED const cast */
394 m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
395 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
396
397 return gif_validate4(&ip, sc, ifp);
398 }
399
400 int
401 in_gif_attach(sc)
402 struct gif_softc *sc;
403 {
404 #ifndef USE_ENCAPCHECK
405 struct sockaddr_in mask4;
406
407 bzero(&mask4, sizeof(mask4));
408 mask4.sin_len = sizeof(struct sockaddr_in);
409 mask4.sin_addr.s_addr = ~0;
410
411 if (!sc->gif_psrc || !sc->gif_pdst)
412 return EINVAL;
413 sc->encap_cookie4 = encap_attach(AF_INET, -1, sc->gif_psrc,
414 (struct sockaddr *)&mask4, sc->gif_pdst, (struct sockaddr *)&mask4,
415 (struct protosw *)&in_gif_protosw, sc);
416 #else
417 sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
418 &in_gif_protosw, sc);
419 #endif
420 if (sc->encap_cookie4 == NULL)
421 return EEXIST;
422 return 0;
423 }
424
425 int
426 in_gif_detach(sc)
427 struct gif_softc *sc;
428 {
429 int error;
430
431 error = encap_detach(sc->encap_cookie4);
432 if (error == 0)
433 sc->encap_cookie4 = NULL;
434 return error;
435 }
436