FreeBSD/Linux Kernel Cross Reference
sys/netinet/in_gif.c
1 /* $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $ */
2
3 /*-
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: releng/10.0/sys/netinet/in_gif.c 243882 2012-12-05 08:04:20Z glebius $");
34
35 #include "opt_mrouting.h"
36 #include "opt_inet.h"
37 #include "opt_inet6.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/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/protosw.h>
48 #include <sys/malloc.h>
49
50 #include <net/if.h>
51 #include <net/route.h>
52 #include <net/vnet.h>
53
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/ip_var.h>
58 #include <netinet/in_gif.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip_encap.h>
61 #include <netinet/ip_ecn.h>
62
63 #ifdef INET6
64 #include <netinet/ip6.h>
65 #endif
66
67 #ifdef MROUTING
68 #include <netinet/ip_mroute.h>
69 #endif /* MROUTING */
70
71 #include <net/if_gif.h>
72
73 static int gif_validate4(const struct ip *, struct gif_softc *,
74 struct ifnet *);
75
76 extern struct domain inetdomain;
77 struct protosw in_gif_protosw = {
78 .pr_type = SOCK_RAW,
79 .pr_domain = &inetdomain,
80 .pr_protocol = 0/* IPPROTO_IPV[46] */,
81 .pr_flags = PR_ATOMIC|PR_ADDR,
82 .pr_input = in_gif_input,
83 .pr_output = (pr_output_t*)rip_output,
84 .pr_ctloutput = rip_ctloutput,
85 .pr_usrreqs = &rip_usrreqs
86 };
87
88 VNET_DEFINE(int, ip_gif_ttl) = GIF_TTL;
89 #define V_ip_gif_ttl VNET(ip_gif_ttl)
90 SYSCTL_VNET_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_RW,
91 &VNET_NAME(ip_gif_ttl), 0, "");
92
93 int
94 in_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
95 {
96 struct gif_softc *sc = ifp->if_softc;
97 struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
98 struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
99 struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
100 struct ip iphdr; /* capsule IP header, host byte ordered */
101 struct etherip_header eiphdr;
102 int error, len, proto;
103 u_int8_t tos;
104
105 GIF_LOCK_ASSERT(sc);
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 case AF_LINK:
147 proto = IPPROTO_ETHERIP;
148
149 /*
150 * GIF_SEND_REVETHIP (disabled by default) intentionally
151 * sends an EtherIP packet with revered version field in
152 * the header. This is a knob for backward compatibility
153 * with FreeBSD 7.2R or prior.
154 */
155 if ((sc->gif_options & GIF_SEND_REVETHIP)) {
156 eiphdr.eip_ver = 0;
157 eiphdr.eip_resvl = ETHERIP_VERSION;
158 eiphdr.eip_resvh = 0;
159 } else {
160 eiphdr.eip_ver = ETHERIP_VERSION;
161 eiphdr.eip_resvl = 0;
162 eiphdr.eip_resvh = 0;
163 }
164 /* prepend Ethernet-in-IP header */
165 M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT);
166 if (m && m->m_len < sizeof(struct etherip_header))
167 m = m_pullup(m, sizeof(struct etherip_header));
168 if (m == NULL)
169 return ENOBUFS;
170 bcopy(&eiphdr, mtod(m, struct etherip_header *),
171 sizeof(struct etherip_header));
172 break;
173
174 default:
175 #ifdef DEBUG
176 printf("in_gif_output: warning: unknown family %d passed\n",
177 family);
178 #endif
179 m_freem(m);
180 return EAFNOSUPPORT;
181 }
182
183 bzero(&iphdr, sizeof(iphdr));
184 iphdr.ip_src = sin_src->sin_addr;
185 /* bidirectional configured tunnel mode */
186 if (sin_dst->sin_addr.s_addr != INADDR_ANY)
187 iphdr.ip_dst = sin_dst->sin_addr;
188 else {
189 m_freem(m);
190 return ENETUNREACH;
191 }
192 iphdr.ip_p = proto;
193 /* version will be set in ip_output() */
194 iphdr.ip_ttl = V_ip_gif_ttl;
195 iphdr.ip_len = htons(m->m_pkthdr.len + sizeof(struct ip));
196 ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
197 &iphdr.ip_tos, &tos);
198
199 /* prepend new IP header */
200 len = sizeof(struct ip);
201 #ifndef __NO_STRICT_ALIGNMENT
202 if (family == AF_LINK)
203 len += ETHERIP_ALIGN;
204 #endif
205 M_PREPEND(m, len, M_NOWAIT);
206 if (m != NULL && m->m_len < len)
207 m = m_pullup(m, len);
208 if (m == NULL) {
209 printf("ENOBUFS in in_gif_output %d\n", __LINE__);
210 return ENOBUFS;
211 }
212 #ifndef __NO_STRICT_ALIGNMENT
213 if (family == AF_LINK) {
214 len = mtod(m, vm_offset_t) & 3;
215 KASSERT(len == 0 || len == ETHERIP_ALIGN,
216 ("in_gif_output: unexpected misalignment"));
217 m->m_data += len;
218 m->m_len -= ETHERIP_ALIGN;
219 }
220 #endif
221 bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
222
223 M_SETFIB(m, sc->gif_fibnum);
224
225 if (dst->sin_family != sin_dst->sin_family ||
226 dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) {
227 /* cache route doesn't match */
228 bzero(dst, sizeof(*dst));
229 dst->sin_family = sin_dst->sin_family;
230 dst->sin_len = sizeof(struct sockaddr_in);
231 dst->sin_addr = sin_dst->sin_addr;
232 if (sc->gif_ro.ro_rt) {
233 RTFREE(sc->gif_ro.ro_rt);
234 sc->gif_ro.ro_rt = NULL;
235 }
236 #if 0
237 GIF2IFP(sc)->if_mtu = GIF_MTU;
238 #endif
239 }
240
241 if (sc->gif_ro.ro_rt == NULL) {
242 in_rtalloc_ign(&sc->gif_ro, 0, sc->gif_fibnum);
243 if (sc->gif_ro.ro_rt == NULL) {
244 m_freem(m);
245 return ENETUNREACH;
246 }
247
248 /* if it constitutes infinite encapsulation, punt. */
249 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
250 m_freem(m);
251 return ENETUNREACH; /* XXX */
252 }
253 #if 0
254 ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
255 - sizeof(struct ip);
256 #endif
257 }
258
259 error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
260
261 if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
262 sc->gif_ro.ro_rt != NULL) {
263 RTFREE(sc->gif_ro.ro_rt);
264 sc->gif_ro.ro_rt = NULL;
265 }
266
267 return (error);
268 }
269
270 void
271 in_gif_input(struct mbuf *m, int off)
272 {
273 struct ifnet *gifp = NULL;
274 struct gif_softc *sc;
275 struct ip *ip;
276 int af;
277 u_int8_t otos;
278 int proto;
279
280 ip = mtod(m, struct ip *);
281 proto = ip->ip_p;
282
283 sc = (struct gif_softc *)encap_getarg(m);
284 if (sc == NULL) {
285 m_freem(m);
286 KMOD_IPSTAT_INC(ips_nogif);
287 return;
288 }
289
290 gifp = GIF2IFP(sc);
291 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
292 m_freem(m);
293 KMOD_IPSTAT_INC(ips_nogif);
294 return;
295 }
296
297 otos = ip->ip_tos;
298 m_adj(m, off);
299
300 switch (proto) {
301 #ifdef INET
302 case IPPROTO_IPV4:
303 {
304 struct ip *ip;
305 af = AF_INET;
306 if (m->m_len < sizeof(*ip)) {
307 m = m_pullup(m, sizeof(*ip));
308 if (!m)
309 return;
310 }
311 ip = mtod(m, struct ip *);
312 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
313 ECN_ALLOWED : ECN_NOCARE,
314 &otos, &ip->ip_tos) == 0) {
315 m_freem(m);
316 return;
317 }
318 break;
319 }
320 #endif
321 #ifdef INET6
322 case IPPROTO_IPV6:
323 {
324 struct ip6_hdr *ip6;
325 u_int8_t itos, oitos;
326
327 af = AF_INET6;
328 if (m->m_len < sizeof(*ip6)) {
329 m = m_pullup(m, sizeof(*ip6));
330 if (!m)
331 return;
332 }
333 ip6 = mtod(m, struct ip6_hdr *);
334 itos = oitos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
335 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
336 ECN_ALLOWED : ECN_NOCARE,
337 &otos, &itos) == 0) {
338 m_freem(m);
339 return;
340 }
341 if (itos != oitos) {
342 ip6->ip6_flow &= ~htonl(0xff << 20);
343 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
344 }
345 break;
346 }
347 #endif /* INET6 */
348 case IPPROTO_ETHERIP:
349 af = AF_LINK;
350 break;
351
352 default:
353 KMOD_IPSTAT_INC(ips_nogif);
354 m_freem(m);
355 return;
356 }
357 gif_input(m, af, gifp);
358 return;
359 }
360
361 /*
362 * validate outer address.
363 */
364 static int
365 gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
366 {
367 struct sockaddr_in *src, *dst;
368 struct in_ifaddr *ia4;
369
370 src = (struct sockaddr_in *)sc->gif_psrc;
371 dst = (struct sockaddr_in *)sc->gif_pdst;
372
373 /* check for address match */
374 if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
375 dst->sin_addr.s_addr != ip->ip_src.s_addr)
376 return 0;
377
378 /* martian filters on outer source - NOT done in ip_input! */
379 if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)))
380 return 0;
381 switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
382 case 0: case 127: case 255:
383 return 0;
384 }
385
386 /* reject packets with broadcast on source */
387 /* XXXRW: should use hash lists? */
388 IN_IFADDR_RLOCK();
389 TAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) {
390 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
391 continue;
392 if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
393 IN_IFADDR_RUNLOCK();
394 return 0;
395 }
396 }
397 IN_IFADDR_RUNLOCK();
398
399 /* ingress filters on outer source */
400 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
401 struct sockaddr_in sin;
402 struct rtentry *rt;
403
404 bzero(&sin, sizeof(sin));
405 sin.sin_family = AF_INET;
406 sin.sin_len = sizeof(struct sockaddr_in);
407 sin.sin_addr = ip->ip_src;
408 /* XXX MRT check for the interface we would use on output */
409 rt = in_rtalloc1((struct sockaddr *)&sin, 0,
410 0UL, sc->gif_fibnum);
411 if (!rt || rt->rt_ifp != ifp) {
412 #if 0
413 log(LOG_WARNING, "%s: packet from 0x%x dropped "
414 "due to ingress filter\n", if_name(GIF2IFP(sc)),
415 (u_int32_t)ntohl(sin.sin_addr.s_addr));
416 #endif
417 if (rt)
418 RTFREE_LOCKED(rt);
419 return 0;
420 }
421 RTFREE_LOCKED(rt);
422 }
423
424 return 32 * 2;
425 }
426
427 /*
428 * we know that we are in IFF_UP, outer address available, and outer family
429 * matched the physical addr family. see gif_encapcheck().
430 */
431 int
432 gif_encapcheck4(const struct mbuf *m, int off, int proto, void *arg)
433 {
434 struct ip ip;
435 struct gif_softc *sc;
436 struct ifnet *ifp;
437
438 /* sanity check done in caller */
439 sc = (struct gif_softc *)arg;
440
441 /* LINTED const cast */
442 m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
443 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
444
445 return gif_validate4(&ip, sc, ifp);
446 }
447
448 int
449 in_gif_attach(struct gif_softc *sc)
450 {
451 sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
452 &in_gif_protosw, sc);
453 if (sc->encap_cookie4 == NULL)
454 return EEXIST;
455 return 0;
456 }
457
458 int
459 in_gif_detach(struct gif_softc *sc)
460 {
461 int error;
462
463 error = encap_detach(sc->encap_cookie4);
464 if (error == 0)
465 sc->encap_cookie4 = NULL;
466 return error;
467 }
Cache object: fc82b7b2c29ce646d52901248309955a
|