2007-03-04 08:59:00 +03:00
|
|
|
/* $NetBSD: in_gif.c,v 1.55 2007/03/04 06:03:20 christos Exp $ */
|
2001-07-29 09:08:32 +04:00
|
|
|
/* $KAME: in_gif.c,v 1.66 2001/07/29 04:46:09 itojun Exp $ */
|
1999-07-04 01:24:45 +04:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
2000-04-19 10:30:51 +04:00
|
|
|
*
|
1999-06-28 10:36:47 +04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2000-04-19 10:30:51 +04:00
|
|
|
*
|
1999-06-28 10:36:47 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-11-13 03:32:34 +03:00
|
|
|
#include <sys/cdefs.h>
|
2007-03-04 08:59:00 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: in_gif.c,v 1.55 2007/03/04 06:03:20 christos Exp $");
|
2001-11-13 03:32:34 +03:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
#include "opt_inet.h"
|
2001-02-20 11:49:15 +03:00
|
|
|
#include "opt_iso.h"
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
2001-01-22 10:51:01 +03:00
|
|
|
#include <sys/syslog.h>
|
2001-12-21 06:21:50 +03:00
|
|
|
#include <sys/protosw.h>
|
2005-06-26 14:39:21 +04:00
|
|
|
#include <sys/kernel.h>
|
2000-04-19 10:30:51 +04:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
#include <netinet/in_gif.h>
|
2000-04-19 10:30:51 +04:00
|
|
|
#include <netinet/in_var.h>
|
|
|
|
#include <netinet/ip_encap.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <netinet/ip_ecn.h>
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#endif
|
|
|
|
|
2002-06-09 20:33:36 +04:00
|
|
|
#include <net/if_gif.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
#include "gif.h"
|
|
|
|
|
|
|
|
#include <machine/stdarg.h>
|
1999-12-13 18:17:17 +03:00
|
|
|
|
|
|
|
#include <net/net_osdep.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2005-02-03 00:41:55 +03:00
|
|
|
static int gif_validate4(const struct ip *, struct gif_softc *,
|
|
|
|
struct ifnet *);
|
2001-07-29 09:08:32 +04:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
#if NGIF > 0
|
1999-07-06 16:23:19 +04:00
|
|
|
int ip_gif_ttl = GIF_TTL;
|
1999-06-28 10:36:47 +04:00
|
|
|
#else
|
1999-07-06 16:23:19 +04:00
|
|
|
int ip_gif_ttl = 0;
|
1999-06-28 10:36:47 +04:00
|
|
|
#endif
|
|
|
|
|
2004-04-22 05:01:40 +04:00
|
|
|
const struct protosw in_gif_protosw =
|
2001-12-21 06:21:50 +03:00
|
|
|
{ SOCK_RAW, &inetdomain, 0/* IPPROTO_IPV[46] */, PR_ATOMIC|PR_ADDR,
|
|
|
|
in_gif_input, rip_output, 0, rip_ctloutput,
|
|
|
|
rip_usrreq,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
};
|
2001-07-29 09:08:32 +04:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
int
|
2005-02-03 06:49:01 +03:00
|
|
|
in_gif_output(struct ifnet *ifp, int family, struct mbuf *m)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2001-01-22 10:51:01 +03:00
|
|
|
struct gif_softc *sc = (struct gif_softc*)ifp;
|
1999-06-28 10:36:47 +04:00
|
|
|
struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
|
|
|
|
struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
|
|
|
|
struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
|
|
|
|
struct ip iphdr; /* capsule IP header, host byte ordered */
|
|
|
|
int proto, error;
|
|
|
|
u_int8_t tos;
|
|
|
|
|
|
|
|
if (sin_src == NULL || sin_dst == NULL ||
|
|
|
|
sin_src->sin_family != AF_INET ||
|
|
|
|
sin_dst->sin_family != AF_INET) {
|
|
|
|
m_freem(m);
|
|
|
|
return EAFNOSUPPORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (family) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
|
|
|
{
|
2006-07-28 20:30:55 +04:00
|
|
|
const struct ip *ip;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
proto = IPPROTO_IPV4;
|
|
|
|
if (m->m_len < sizeof(*ip)) {
|
|
|
|
m = m_pullup(m, sizeof(*ip));
|
2006-07-28 21:04:27 +04:00
|
|
|
if (m == NULL)
|
1999-06-28 10:36:47 +04:00
|
|
|
return ENOBUFS;
|
|
|
|
}
|
2006-07-28 20:30:55 +04:00
|
|
|
ip = mtod(m, const struct ip *);
|
1999-06-28 10:36:47 +04:00
|
|
|
tos = ip->ip_tos;
|
|
|
|
break;
|
|
|
|
}
|
2001-12-21 06:21:50 +03:00
|
|
|
#endif /* INET */
|
1999-06-28 10:36:47 +04:00
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
{
|
2006-07-28 20:30:55 +04:00
|
|
|
const struct ip6_hdr *ip6;
|
1999-06-28 10:36:47 +04:00
|
|
|
proto = IPPROTO_IPV6;
|
|
|
|
if (m->m_len < sizeof(*ip6)) {
|
|
|
|
m = m_pullup(m, sizeof(*ip6));
|
|
|
|
if (!m)
|
|
|
|
return ENOBUFS;
|
|
|
|
}
|
2006-07-28 20:30:55 +04:00
|
|
|
ip6 = mtod(m, const struct ip6_hdr *);
|
1999-06-28 10:36:47 +04:00
|
|
|
tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
|
|
|
|
break;
|
|
|
|
}
|
2001-12-21 06:21:50 +03:00
|
|
|
#endif /* INET6 */
|
2001-02-20 13:41:47 +03:00
|
|
|
#ifdef ISO
|
|
|
|
case AF_ISO:
|
|
|
|
proto = IPPROTO_EON;
|
|
|
|
tos = 0;
|
|
|
|
break;
|
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
default:
|
2000-02-07 09:15:16 +03:00
|
|
|
#ifdef DEBUG
|
1999-06-28 10:36:47 +04:00
|
|
|
printf("in_gif_output: warning: unknown family %d passed\n",
|
|
|
|
family);
|
|
|
|
#endif
|
|
|
|
m_freem(m);
|
|
|
|
return EAFNOSUPPORT;
|
|
|
|
}
|
|
|
|
|
2001-01-22 10:51:01 +03:00
|
|
|
bzero(&iphdr, sizeof(iphdr));
|
1999-06-28 10:36:47 +04:00
|
|
|
iphdr.ip_src = sin_src->sin_addr;
|
2001-05-14 17:35:20 +04:00
|
|
|
/* bidirectional configured tunnel mode */
|
|
|
|
if (sin_dst->sin_addr.s_addr != INADDR_ANY)
|
|
|
|
iphdr.ip_dst = sin_dst->sin_addr;
|
|
|
|
else {
|
|
|
|
m_freem(m);
|
|
|
|
return ENETUNREACH;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
iphdr.ip_p = proto;
|
|
|
|
/* version will be set in ip_output() */
|
1999-07-06 16:23:19 +04:00
|
|
|
iphdr.ip_ttl = ip_gif_ttl;
|
2002-08-14 04:23:27 +04:00
|
|
|
iphdr.ip_len = htons(m->m_pkthdr.len + sizeof(struct ip));
|
1999-06-28 10:36:47 +04:00
|
|
|
if (ifp->if_flags & IFF_LINK1)
|
|
|
|
ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos);
|
2001-05-10 05:37:42 +04:00
|
|
|
else
|
|
|
|
ip_ecn_ingress(ECN_NOCARE, &iphdr.ip_tos, &tos);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* prepend new IP header */
|
|
|
|
M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
|
2006-07-28 20:30:55 +04:00
|
|
|
/* XXX Is m_pullup really necessary after M_PREPEND? */
|
2006-07-28 21:34:13 +04:00
|
|
|
if (m != NULL && M_UNWRITABLE(m, sizeof(struct ip)))
|
1999-06-28 10:36:47 +04:00
|
|
|
m = m_pullup(m, sizeof(struct ip));
|
2001-07-29 09:08:32 +04:00
|
|
|
if (m == NULL)
|
1999-06-28 10:36:47 +04:00
|
|
|
return ENOBUFS;
|
2000-04-19 10:30:51 +04:00
|
|
|
bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
|
1999-06-28 10:36:47 +04:00
|
|
|
|
Here are various changes designed to protect against bad IPv4
routing caused by stale route caches (struct route). Route caches
are sprinkled throughout PCBs, the IP fast-forwarding table, and
IP tunnel interfaces (gre, gif, stf).
Stale IPv6 and ISO route caches will be treated by separate patches.
Thank you to Christoph Badura for suggesting the general approach
to invalidating route caches that I take here.
Here are the details:
Add hooks to struct domain for tracking and for invalidating each
domain's route caches: dom_rtcache, dom_rtflush, and dom_rtflushall.
Introduce helper subroutines, rtflush(ro) for invalidating a route
cache, rtflushall(family) for invalidating all route caches in a
routing domain, and rtcache(ro) for notifying the domain of a new
cached route.
Chain together all IPv4 route caches where ro_rt != NULL. Provide
in_rtcache() for adding a route to the chain. Provide in_rtflush()
and in_rtflushall() for invalidating IPv4 route caches. In
in_rtflush(), set ro_rt to NULL, and remove the route from the
chain. In in_rtflushall(), walk the chain and remove every route
cache.
In rtrequest1(), call rtflushall() to invalidate route caches when
a route is added.
In gif(4), discard the workaround for stale caches that involves
expiring them every so often.
Replace the pattern 'RTFREE(ro->ro_rt); ro->ro_rt = NULL;' with a
call to rtflush(ro).
Update ipflow_fastforward() and all other users of route caches so
that they expect a cached route, ro->ro_rt, to turn to NULL.
Take care when moving a 'struct route' to rtflush() the source and
to rtcache() the destination.
In domain initializers, use .dom_xxx tags.
KNF here and there.
2006-12-09 08:33:04 +03:00
|
|
|
if (dst->sin_family != sin_dst->sin_family ||
|
2006-12-16 00:18:52 +03:00
|
|
|
!in_hosteq(dst->sin_addr, sin_dst->sin_addr))
|
|
|
|
rtcache_free(&sc->gif_ro);
|
|
|
|
else
|
|
|
|
rtcache_check(&sc->gif_ro);
|
|
|
|
if (sc->gif_ro.ro_rt == NULL) {
|
2007-02-17 08:31:39 +03:00
|
|
|
memset(dst, 0, sizeof(*dst));
|
1999-06-28 10:36:47 +04:00
|
|
|
dst->sin_family = sin_dst->sin_family;
|
|
|
|
dst->sin_len = sizeof(struct sockaddr_in);
|
|
|
|
dst->sin_addr = sin_dst->sin_addr;
|
2006-12-16 00:18:52 +03:00
|
|
|
rtcache_init(&sc->gif_ro);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (sc->gif_ro.ro_rt == NULL) {
|
|
|
|
m_freem(m);
|
|
|
|
return ENETUNREACH;
|
|
|
|
}
|
2006-12-16 00:18:52 +03:00
|
|
|
}
|
2000-04-19 10:30:51 +04:00
|
|
|
|
2006-12-16 00:18:52 +03:00
|
|
|
/* If the route constitutes infinite encapsulation, punt. */
|
|
|
|
if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
|
|
|
|
rtcache_free(&sc->gif_ro);
|
|
|
|
m_freem(m);
|
|
|
|
return ENETUNREACH; /*XXX*/
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-04-19 10:30:51 +04:00
|
|
|
|
2003-08-23 01:53:01 +04:00
|
|
|
error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
|
2002-09-11 06:46:42 +04:00
|
|
|
return (error);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
in_gif_input(struct mbuf *m, ...)
|
|
|
|
{
|
|
|
|
int off, proto;
|
|
|
|
struct ifnet *gifp = NULL;
|
2006-07-28 21:04:27 +04:00
|
|
|
const struct ip *ip;
|
1999-06-28 10:36:47 +04:00
|
|
|
va_list ap;
|
2001-02-20 11:49:15 +03:00
|
|
|
int af;
|
1999-06-28 10:36:47 +04:00
|
|
|
u_int8_t otos;
|
|
|
|
|
|
|
|
va_start(ap, m);
|
|
|
|
off = va_arg(ap, int);
|
|
|
|
proto = va_arg(ap, int);
|
|
|
|
va_end(ap);
|
|
|
|
|
2006-07-28 21:04:27 +04:00
|
|
|
ip = mtod(m, const struct ip *);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-04-19 10:30:51 +04:00
|
|
|
gifp = (struct ifnet *)encap_getarg(m);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-04-19 10:30:51 +04:00
|
|
|
if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
|
1999-06-28 10:36:47 +04:00
|
|
|
m_freem(m);
|
|
|
|
ipstat.ips_nogif++;
|
|
|
|
return;
|
|
|
|
}
|
2002-11-11 21:35:27 +03:00
|
|
|
#ifndef GIF_ENCAPCHECK
|
2001-07-29 09:08:32 +04:00
|
|
|
if (!gif_validate4(ip, (struct gif_softc *)gifp, m->m_pkthdr.rcvif)) {
|
|
|
|
m_freem(m);
|
|
|
|
ipstat.ips_nogif++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
otos = ip->ip_tos;
|
|
|
|
m_adj(m, off);
|
|
|
|
|
|
|
|
switch (proto) {
|
|
|
|
#ifdef INET
|
|
|
|
case IPPROTO_IPV4:
|
|
|
|
{
|
2005-05-30 01:41:23 +04:00
|
|
|
struct ip *xip;
|
1999-06-28 10:36:47 +04:00
|
|
|
af = AF_INET;
|
2006-07-28 21:34:13 +04:00
|
|
|
if (M_UNWRITABLE(m, sizeof(*xip))) {
|
2006-07-28 21:04:27 +04:00
|
|
|
if ((m = m_pullup(m, sizeof(*xip))) == NULL)
|
1999-06-28 10:36:47 +04:00
|
|
|
return;
|
|
|
|
}
|
2005-05-30 01:41:23 +04:00
|
|
|
xip = mtod(m, struct ip *);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (gifp->if_flags & IFF_LINK1)
|
2005-05-30 01:41:23 +04:00
|
|
|
ip_ecn_egress(ECN_ALLOWED, &otos, &xip->ip_tos);
|
2001-05-10 05:37:42 +04:00
|
|
|
else
|
2005-05-30 01:41:23 +04:00
|
|
|
ip_ecn_egress(ECN_NOCARE, &otos, &xip->ip_tos);
|
1999-06-28 10:36:47 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
case IPPROTO_IPV6:
|
|
|
|
{
|
|
|
|
struct ip6_hdr *ip6;
|
|
|
|
u_int8_t itos;
|
|
|
|
af = AF_INET6;
|
2006-07-28 21:34:13 +04:00
|
|
|
if (M_UNWRITABLE(m, sizeof(*ip6))) {
|
2006-07-28 21:04:27 +04:00
|
|
|
if ((m = m_pullup(m, sizeof(*ip6))) == NULL)
|
1999-06-28 10:36:47 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ip6 = mtod(m, struct ip6_hdr *);
|
|
|
|
itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
|
|
|
|
if (gifp->if_flags & IFF_LINK1)
|
|
|
|
ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
|
2001-05-10 05:37:42 +04:00
|
|
|
else
|
|
|
|
ip_ecn_egress(ECN_NOCARE, &otos, &itos);
|
1999-06-28 10:36:47 +04:00
|
|
|
ip6->ip6_flow &= ~htonl(0xff << 20);
|
|
|
|
ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* INET6 */
|
2001-02-20 11:49:15 +03:00
|
|
|
#ifdef ISO
|
|
|
|
case IPPROTO_EON:
|
|
|
|
af = AF_ISO;
|
|
|
|
break;
|
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
default:
|
|
|
|
ipstat.ips_nogif++;
|
|
|
|
m_freem(m);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gif_input(m, af, gifp);
|
|
|
|
return;
|
|
|
|
}
|
2000-04-19 10:30:51 +04:00
|
|
|
|
|
|
|
/*
|
2001-07-29 09:08:32 +04:00
|
|
|
* validate outer address.
|
2000-04-19 10:30:51 +04:00
|
|
|
*/
|
2001-07-29 09:08:32 +04:00
|
|
|
static int
|
2005-02-03 06:49:01 +03:00
|
|
|
gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
|
2001-07-29 09:08:32 +04:00
|
|
|
{
|
2000-04-19 10:30:51 +04:00
|
|
|
struct sockaddr_in *src, *dst;
|
|
|
|
struct in_ifaddr *ia4;
|
|
|
|
|
|
|
|
src = (struct sockaddr_in *)sc->gif_psrc;
|
|
|
|
dst = (struct sockaddr_in *)sc->gif_pdst;
|
|
|
|
|
|
|
|
/* check for address match */
|
2001-07-29 09:08:32 +04:00
|
|
|
if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
|
|
|
|
dst->sin_addr.s_addr != ip->ip_src.s_addr)
|
2000-04-19 10:30:51 +04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* martian filters on outer source - NOT done in ip_input! */
|
2001-07-29 09:08:32 +04:00
|
|
|
if (IN_MULTICAST(ip->ip_src.s_addr))
|
2000-04-19 10:30:51 +04:00
|
|
|
return 0;
|
2001-07-29 09:08:32 +04:00
|
|
|
switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
|
2000-04-19 10:30:51 +04:00
|
|
|
case 0: case 127: case 255:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* reject packets with broadcast on source */
|
2003-11-11 23:25:26 +03:00
|
|
|
TAILQ_FOREACH(ia4, &in_ifaddrhead, ia_list) {
|
2000-04-19 10:30:51 +04:00
|
|
|
if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
|
|
|
|
continue;
|
2001-07-29 09:08:32 +04:00
|
|
|
if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
|
2000-04-19 10:30:51 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ingress filters on outer source */
|
2001-07-29 09:08:32 +04:00
|
|
|
if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
|
2000-04-19 10:30:51 +04:00
|
|
|
struct sockaddr_in sin;
|
|
|
|
struct rtentry *rt;
|
|
|
|
|
|
|
|
bzero(&sin, sizeof(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_len = sizeof(struct sockaddr_in);
|
2001-07-29 09:08:32 +04:00
|
|
|
sin.sin_addr = ip->ip_src;
|
2000-04-19 10:30:51 +04:00
|
|
|
rt = rtalloc1((struct sockaddr *)&sin, 0);
|
2001-07-29 09:08:32 +04:00
|
|
|
if (!rt || rt->rt_ifp != ifp) {
|
2001-01-22 10:51:01 +03:00
|
|
|
#if 0
|
|
|
|
log(LOG_WARNING, "%s: packet from 0x%x dropped "
|
|
|
|
"due to ingress filter\n", if_name(&sc->gif_if),
|
|
|
|
(u_int32_t)ntohl(sin.sin_addr.s_addr));
|
|
|
|
#endif
|
|
|
|
if (rt)
|
|
|
|
rtfree(rt);
|
2000-04-19 10:30:51 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rtfree(rt);
|
|
|
|
}
|
|
|
|
|
2001-05-14 17:35:20 +04:00
|
|
|
return 32 * 2;
|
2000-04-19 10:30:51 +04:00
|
|
|
}
|
2001-07-29 09:08:32 +04:00
|
|
|
|
2002-11-11 21:35:27 +03:00
|
|
|
#ifdef GIF_ENCAPCHECK
|
2001-07-29 09:08:32 +04:00
|
|
|
/*
|
|
|
|
* we know that we are in IFF_UP, outer address available, and outer family
|
|
|
|
* matched the physical addr family. see gif_encapcheck().
|
|
|
|
*/
|
|
|
|
int
|
2005-06-02 19:21:35 +04:00
|
|
|
gif_encapcheck4(struct mbuf *m, int off, int proto, void *arg)
|
2001-07-29 09:08:32 +04:00
|
|
|
{
|
|
|
|
struct ip ip;
|
|
|
|
struct gif_softc *sc;
|
|
|
|
struct ifnet *ifp;
|
|
|
|
|
|
|
|
/* sanity check done in caller */
|
|
|
|
sc = (struct gif_softc *)arg;
|
|
|
|
|
2007-03-04 08:59:00 +03:00
|
|
|
m_copydata(m, 0, sizeof(ip), (void *)&ip);
|
2001-07-29 09:08:32 +04:00
|
|
|
ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
|
|
|
|
|
|
|
|
return gif_validate4(&ip, sc, ifp);
|
|
|
|
}
|
2002-11-11 21:35:27 +03:00
|
|
|
#endif
|
2001-07-29 09:08:32 +04:00
|
|
|
|
|
|
|
int
|
2005-02-03 06:49:01 +03:00
|
|
|
in_gif_attach(struct gif_softc *sc)
|
2001-07-29 09:08:32 +04:00
|
|
|
{
|
2002-11-11 21:35:27 +03:00
|
|
|
#ifndef GIF_ENCAPCHECK
|
2001-07-29 09:08:32 +04:00
|
|
|
struct sockaddr_in mask4;
|
|
|
|
|
|
|
|
bzero(&mask4, sizeof(mask4));
|
|
|
|
mask4.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
mask4.sin_addr.s_addr = ~0;
|
|
|
|
|
|
|
|
if (!sc->gif_psrc || !sc->gif_pdst)
|
|
|
|
return EINVAL;
|
|
|
|
sc->encap_cookie4 = encap_attach(AF_INET, -1, sc->gif_psrc,
|
|
|
|
(struct sockaddr *)&mask4, sc->gif_pdst, (struct sockaddr *)&mask4,
|
2005-05-30 01:41:23 +04:00
|
|
|
(const struct protosw *)&in_gif_protosw, sc);
|
2001-07-29 09:08:32 +04:00
|
|
|
#else
|
|
|
|
sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
|
|
|
|
&in_gif_protosw, sc);
|
|
|
|
#endif
|
|
|
|
if (sc->encap_cookie4 == NULL)
|
|
|
|
return EEXIST;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-02-03 06:49:01 +03:00
|
|
|
in_gif_detach(struct gif_softc *sc)
|
2001-07-29 09:08:32 +04:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = encap_detach(sc->encap_cookie4);
|
|
|
|
if (error == 0)
|
|
|
|
sc->encap_cookie4 = NULL;
|
2005-06-26 14:39:21 +04:00
|
|
|
|
2006-12-16 00:18:52 +03:00
|
|
|
rtcache_free(&sc->gif_ro);
|
2005-06-26 14:39:21 +04:00
|
|
|
|
2001-07-29 09:08:32 +04:00
|
|
|
return error;
|
|
|
|
}
|