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