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