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