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