2005-01-07 02:30:35 +00:00
|
|
|
/*-
|
1999-12-07 17:39:16 +00:00
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
2007-12-10 16:03:40 +00:00
|
|
|
*
|
|
|
|
* $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $
|
1999-12-07 17:39:16 +00:00
|
|
|
*/
|
|
|
|
|
2007-12-10 16:03:40 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1999-12-07 17:39:16 +00:00
|
|
|
#include "opt_inet.h"
|
2000-07-04 16:35:15 +00:00
|
|
|
#include "opt_inet6.h"
|
1999-12-07 17:39:16 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2014-10-14 13:31:47 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/rmlock.h>
|
1999-12-07 17:39:16 +00:00
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/errno.h>
|
2010-04-29 11:52:42 +00:00
|
|
|
#include <sys/kernel.h>
|
2001-06-11 12:39:29 +00:00
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/syslog.h>
|
2010-04-29 11:52:42 +00:00
|
|
|
#include <sys/sysctl.h>
|
2002-10-16 19:49:37 +00:00
|
|
|
#include <sys/protosw.h>
|
2001-06-11 12:39:29 +00:00
|
|
|
#include <sys/malloc.h>
|
1999-12-07 17:39:16 +00:00
|
|
|
|
|
|
|
#include <net/if.h>
|
2013-10-26 17:58:36 +00:00
|
|
|
#include <net/if_var.h>
|
1999-12-07 17:39:16 +00:00
|
|
|
#include <net/route.h>
|
2013-10-26 17:58:36 +00:00
|
|
|
#include <net/vnet.h>
|
1999-12-07 17:39:16 +00:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#ifdef INET
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#endif
|
2000-07-04 16:35:15 +00:00
|
|
|
#include <netinet/ip_encap.h>
|
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet/ip6.h>
|
1999-12-07 17:39:16 +00:00
|
|
|
#include <netinet6/ip6_var.h>
|
2000-07-04 16:35:15 +00:00
|
|
|
#include <netinet6/in6_var.h>
|
|
|
|
#endif
|
1999-12-22 19:13:38 +00:00
|
|
|
#include <netinet/ip_ecn.h>
|
2000-07-04 16:35:15 +00:00
|
|
|
#ifdef INET6
|
1999-12-22 19:13:38 +00:00
|
|
|
#include <netinet6/ip6_ecn.h>
|
2000-07-04 16:35:15 +00:00
|
|
|
#endif
|
1999-12-07 17:39:16 +00:00
|
|
|
|
|
|
|
#include <net/if_gif.h>
|
|
|
|
|
2014-12-23 16:17:37 +00:00
|
|
|
#define GIF_HLIM 30
|
|
|
|
static VNET_DEFINE(int, ip6_gif_hlim) = GIF_HLIM;
|
2010-04-29 11:52:42 +00:00
|
|
|
#define V_ip6_gif_hlim VNET(ip6_gif_hlim)
|
|
|
|
|
|
|
|
SYSCTL_DECL(_net_inet6_ip6);
|
2014-11-07 09:39:05 +00:00
|
|
|
SYSCTL_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM, gifhlim, CTLFLAG_VNET | CTLFLAG_RW,
|
2010-04-29 11:52:42 +00:00
|
|
|
&VNET_NAME(ip6_gif_hlim), 0, "");
|
|
|
|
|
2014-12-23 16:17:37 +00:00
|
|
|
static int in6_gif_input(struct mbuf **, int *, int);
|
2002-10-16 19:49:37 +00:00
|
|
|
|
|
|
|
extern struct domain inet6domain;
|
2014-12-23 16:17:37 +00:00
|
|
|
static struct protosw in6_gif_protosw = {
|
2009-01-05 20:29:01 +00:00
|
|
|
.pr_type = SOCK_RAW,
|
|
|
|
.pr_domain = &inet6domain,
|
|
|
|
.pr_protocol = 0, /* IPPROTO_IPV[46] */
|
|
|
|
.pr_flags = PR_ATOMIC|PR_ADDR,
|
|
|
|
.pr_input = in6_gif_input,
|
2014-08-15 02:43:02 +00:00
|
|
|
.pr_output = rip6_output,
|
2009-01-05 20:29:01 +00:00
|
|
|
.pr_ctloutput = rip6_ctloutput,
|
|
|
|
.pr_usrreqs = &rip6_usrreqs
|
2002-10-16 19:49:37 +00:00
|
|
|
};
|
|
|
|
|
1999-12-07 17:39:16 +00:00
|
|
|
int
|
2014-10-14 13:31:47 +00:00
|
|
|
in6_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
|
1999-12-07 17:39:16 +00:00
|
|
|
{
|
2014-10-14 13:31:47 +00:00
|
|
|
GIF_RLOCK_TRACKER;
|
2005-06-10 16:49:24 +00:00
|
|
|
struct gif_softc *sc = ifp->if_softc;
|
1999-12-07 17:39:16 +00:00
|
|
|
struct ip6_hdr *ip6;
|
2014-10-14 13:31:47 +00:00
|
|
|
int len;
|
2003-10-08 18:26:08 +00:00
|
|
|
|
1999-12-07 17:39:16 +00:00
|
|
|
/* prepend new IP header */
|
2009-03-07 19:08:58 +00:00
|
|
|
len = sizeof(struct ip6_hdr);
|
|
|
|
#ifndef __NO_STRICT_ALIGNMENT
|
2014-10-14 13:31:47 +00:00
|
|
|
if (proto == IPPROTO_ETHERIP)
|
2009-03-07 19:08:58 +00:00
|
|
|
len += ETHERIP_ALIGN;
|
|
|
|
#endif
|
2012-12-05 08:04:20 +00:00
|
|
|
M_PREPEND(m, len, M_NOWAIT);
|
2014-10-14 13:31:47 +00:00
|
|
|
if (m == NULL)
|
|
|
|
return (ENOBUFS);
|
2009-03-07 19:08:58 +00:00
|
|
|
#ifndef __NO_STRICT_ALIGNMENT
|
2014-10-14 13:31:47 +00:00
|
|
|
if (proto == IPPROTO_ETHERIP) {
|
2009-03-07 19:08:58 +00:00
|
|
|
len = mtod(m, vm_offset_t) & 3;
|
|
|
|
KASSERT(len == 0 || len == ETHERIP_ALIGN,
|
|
|
|
("in6_gif_output: unexpected misalignment"));
|
|
|
|
m->m_data += len;
|
|
|
|
m->m_len -= ETHERIP_ALIGN;
|
|
|
|
}
|
|
|
|
#endif
|
1999-12-07 17:39:16 +00:00
|
|
|
|
|
|
|
ip6 = mtod(m, struct ip6_hdr *);
|
2014-10-14 13:31:47 +00:00
|
|
|
GIF_RLOCK(sc);
|
|
|
|
if (sc->gif_family != AF_INET6) {
|
2001-06-11 12:39:29 +00:00
|
|
|
m_freem(m);
|
2014-10-14 13:31:47 +00:00
|
|
|
GIF_RUNLOCK(sc);
|
|
|
|
return (ENETDOWN);
|
1999-12-07 17:39:16 +00:00
|
|
|
}
|
2014-10-14 13:31:47 +00:00
|
|
|
bcopy(sc->gif_ip6hdr, ip6, sizeof(struct ip6_hdr));
|
|
|
|
GIF_RUNLOCK(sc);
|
1999-12-07 17:39:16 +00:00
|
|
|
|
2014-10-14 13:31:47 +00:00
|
|
|
ip6->ip6_flow |= htonl((uint32_t)ecn << 20);
|
|
|
|
ip6->ip6_nxt = proto;
|
|
|
|
ip6->ip6_hlim = V_ip6_gif_hlim;
|
2000-07-04 16:35:15 +00:00
|
|
|
/*
|
|
|
|
* force fragmentation to minimum MTU, to avoid path MTU discovery.
|
|
|
|
* it is too painful to ask for resend of inner packet, to achieve
|
|
|
|
* path MTU discovery for encapsulated packets.
|
|
|
|
*/
|
2014-10-14 13:31:47 +00:00
|
|
|
return (ip6_output(m, 0, NULL, IPV6_MINMTU, 0, NULL, NULL));
|
1999-12-07 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 16:17:37 +00:00
|
|
|
static int
|
2007-07-05 16:23:49 +00:00
|
|
|
in6_gif_input(struct mbuf **mp, int *offp, int proto)
|
1999-12-07 17:39:16 +00:00
|
|
|
{
|
|
|
|
struct mbuf *m = *mp;
|
2014-10-14 13:31:47 +00:00
|
|
|
struct ifnet *gifp;
|
2005-06-20 20:17:00 +00:00
|
|
|
struct gif_softc *sc;
|
1999-12-07 17:39:16 +00:00
|
|
|
struct ip6_hdr *ip6;
|
2014-10-14 13:31:47 +00:00
|
|
|
uint8_t ecn;
|
1999-12-07 17:39:16 +00:00
|
|
|
|
2014-10-14 13:31:47 +00:00
|
|
|
sc = encap_getarg(m);
|
2005-06-20 20:17:00 +00:00
|
|
|
if (sc == NULL) {
|
|
|
|
m_freem(m);
|
2013-04-09 07:11:22 +00:00
|
|
|
IP6STAT_INC(ip6s_nogif);
|
2014-10-14 13:31:47 +00:00
|
|
|
return (IPPROTO_DONE);
|
2005-06-20 20:17:00 +00:00
|
|
|
}
|
|
|
|
gifp = GIF2IFP(sc);
|
2014-10-14 13:31:47 +00:00
|
|
|
if ((gifp->if_flags & IFF_UP) != 0) {
|
1999-12-07 17:39:16 +00:00
|
|
|
ip6 = mtod(m, struct ip6_hdr *);
|
2014-10-14 13:31:47 +00:00
|
|
|
ecn = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
|
|
|
|
m_adj(m, *offp);
|
|
|
|
gif_input(m, gifp, proto, ecn);
|
|
|
|
} else {
|
1999-12-07 17:39:16 +00:00
|
|
|
m_freem(m);
|
2014-10-14 13:31:47 +00:00
|
|
|
IP6STAT_INC(ip6s_nogif);
|
1999-12-07 17:39:16 +00:00
|
|
|
}
|
2014-10-14 13:31:47 +00:00
|
|
|
return (IPPROTO_DONE);
|
1999-12-07 17:39:16 +00:00
|
|
|
}
|
2000-07-04 16:35:15 +00:00
|
|
|
|
|
|
|
/*
|
2015-07-29 14:07:43 +00:00
|
|
|
* we know that we are in IFF_UP, outer address available, and outer family
|
|
|
|
* matched the physical addr family. see gif_encapcheck().
|
2000-07-04 16:35:15 +00:00
|
|
|
*/
|
2015-07-29 14:07:43 +00:00
|
|
|
int
|
|
|
|
in6_gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
|
2002-10-16 19:49:37 +00:00
|
|
|
{
|
2015-07-29 14:07:43 +00:00
|
|
|
const struct ip6_hdr *ip6;
|
|
|
|
struct gif_softc *sc;
|
2015-05-15 12:19:45 +00:00
|
|
|
int ret;
|
2000-07-04 16:35:15 +00:00
|
|
|
|
2015-07-29 14:07:43 +00:00
|
|
|
/* sanity check done in caller */
|
|
|
|
sc = (struct gif_softc *)arg;
|
2014-10-14 13:31:47 +00:00
|
|
|
GIF_RLOCK_ASSERT(sc);
|
2015-07-29 14:07:43 +00:00
|
|
|
|
2002-10-16 19:49:37 +00:00
|
|
|
/*
|
|
|
|
* Check for address match. Note that the check is for an incoming
|
|
|
|
* packet. We should compare the *source* address in our configuration
|
|
|
|
* and the *destination* address of the packet, and vice versa.
|
|
|
|
*/
|
2015-07-29 14:07:43 +00:00
|
|
|
ip6 = mtod(m, const struct ip6_hdr *);
|
2015-05-15 12:19:45 +00:00
|
|
|
if (!IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src, &ip6->ip6_dst))
|
2014-10-14 13:31:47 +00:00
|
|
|
return (0);
|
2015-05-15 12:19:45 +00:00
|
|
|
ret = 128;
|
|
|
|
if (!IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst, &ip6->ip6_src)) {
|
|
|
|
if ((sc->gif_options & GIF_IGNORE_SOURCE) == 0)
|
|
|
|
return (0);
|
|
|
|
} else
|
|
|
|
ret += 128;
|
2000-07-04 16:35:15 +00:00
|
|
|
|
|
|
|
/* ingress filters on outer source */
|
2015-07-29 14:07:43 +00:00
|
|
|
if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
|
2000-07-04 16:35:15 +00:00
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
struct rtentry *rt;
|
|
|
|
|
|
|
|
bzero(&sin6, sizeof(sin6));
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
sin6.sin6_len = sizeof(struct sockaddr_in6);
|
2002-10-16 19:49:37 +00:00
|
|
|
sin6.sin6_addr = ip6->ip6_src;
|
|
|
|
sin6.sin6_scope_id = 0; /* XXX */
|
|
|
|
|
2012-02-03 13:08:44 +00:00
|
|
|
rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, 0UL,
|
|
|
|
sc->gif_fibnum);
|
2015-07-29 14:07:43 +00:00
|
|
|
if (rt == NULL || rt->rt_ifp != m->m_pkthdr.rcvif) {
|
|
|
|
if (rt != NULL)
|
2009-01-31 10:48:02 +00:00
|
|
|
RTFREE_LOCKED(rt);
|
2014-10-14 13:31:47 +00:00
|
|
|
return (0);
|
2000-07-04 16:35:15 +00:00
|
|
|
}
|
2009-01-31 10:48:02 +00:00
|
|
|
RTFREE_LOCKED(rt);
|
2000-07-04 16:35:15 +00:00
|
|
|
}
|
2015-05-15 12:19:45 +00:00
|
|
|
return (ret);
|
2000-07-04 16:35:15 +00:00
|
|
|
}
|
2002-10-16 19:49:37 +00:00
|
|
|
|
|
|
|
int
|
2007-07-05 16:23:49 +00:00
|
|
|
in6_gif_attach(struct gif_softc *sc)
|
2002-10-16 19:49:37 +00:00
|
|
|
{
|
|
|
|
|
2014-10-14 13:31:47 +00:00
|
|
|
KASSERT(sc->gif_ecookie == NULL, ("gif_ecookie isn't NULL"));
|
|
|
|
sc->gif_ecookie = encap_attach_func(AF_INET6, -1, gif_encapcheck,
|
|
|
|
(void *)&in6_gif_protosw, sc);
|
|
|
|
if (sc->gif_ecookie == NULL)
|
|
|
|
return (EEXIST);
|
|
|
|
return (0);
|
2002-10-16 19:49:37 +00:00
|
|
|
}
|