/*
* 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.
*/
#ifndef MAX_GIF_NEST
/*
* This macro controls the upper limitation on nesting of gif tunnels.
* Since, setting a large value to this macro with a careless configuration
* may introduce system crash, we don't allow any nestings by default.
* If you need to configure nested gif tunnels, you can define this macro
* in your kernel configuration file. However, if you do so, please be
* careful to configure the tunnels so that it won't make a loop.
*/
#define MAX_GIF_NEST 1
#endif
static int max_gif_nesting = MAX_GIF_NEST;
static struct sysctllog *gif_sysctl;
#ifdef INET6
static int
sysctl_gif_pmtu_global(SYSCTLFN_ARGS)
{
int error, pmtu;
struct sysctlnode node = *rnode;
rv = sysctl_createv(clog, 0, &rnode, &cnode,
CTLFLAG_PERMANENT,
CTLTYPE_INT, "pmtu",
SYSCTL_DESCR("Path MTU setting for this gif tunnel"),
sysctl_gif_pmtu_perif, 0, (void *)sc, 0,
CTL_CREATE, CTL_EOL);
if (rv != 0)
log(LOG_WARNING, "%s: could not attach sysctl node pmtu\n", ifname);
sc->gif_pmtu = GIF_PMTU_SYSDEFAULT;
#endif
}
/* ARGSUSED */
void
gifattach(int count)
{
/*
* Nothing to do here, initialization is handled by the
* module initialization code in gifinit() below).
*/
}
var = sc->gif_var;
kmem_free(var, sizeof(*var));
kmem_free(sc, sizeof(struct gif_softc));
return 0;
}
#ifdef GIF_ENCAPCHECK
int
gif_encapcheck(struct mbuf *m, int off, int proto, void *arg)
{
struct ip ip;
struct gif_softc *sc;
struct gif_variant *var;
struct psref psref;
int ret = 0;
sc = arg;
if (sc == NULL)
return 0;
if ((sc->gif_if.if_flags & IFF_UP) == 0)
return 0;
var = gif_getref_variant(sc, &psref);
/* no physical address */
if (var->gv_psrc == NULL || var->gv_pdst == NULL)
goto out;
switch (proto) {
#ifdef INET
case IPPROTO_IPV4:
break;
#endif
#ifdef INET6
case IPPROTO_IPV6:
break;
#endif
default:
goto out;
}
/* Bail on short packets */
KASSERT(m->m_flags & M_PKTHDR);
if (m->m_pkthdr.len < sizeof(ip))
goto out;
m_copydata(m, 0, sizeof(ip), &ip);
switch (ip.ip_v) {
#ifdef INET
case 4:
if (var->gv_psrc->sa_family != AF_INET ||
var->gv_pdst->sa_family != AF_INET)
goto out;
ret = gif_encapcheck4(m, off, proto, var);
break;
#endif
#ifdef INET6
case 6:
if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
goto out;
if (var->gv_psrc->sa_family != AF_INET6 ||
var->gv_pdst->sa_family != AF_INET6)
goto out;
ret = gif_encapcheck6(m, off, proto, var);
break;
#endif
default:
goto out;
}
/*
* gif may cause infinite recursion calls when misconfigured.
* We'll prevent this by introducing upper limit.
*/
static int
gif_check_nesting(struct ifnet *ifp, struct mbuf *m)
{
return if_tunnel_check_nesting(ifp, m, max_gif_nesting);
}
var = gif_getref_variant(sc, &psref);
if (var->gv_psrc == NULL || var->gv_pdst == NULL) {
m_freem(m);
error = ENETDOWN;
goto end;
}
/* XXX should we check if our outer source is legal? */
m->m_flags &= ~(M_BCAST | M_MCAST);
/* use DLT_NULL encapsulation here to pass inner af type */
M_PREPEND(m, sizeof(int), M_DONTWAIT);
if (!m) {
error = ENOBUFS;
goto end;
}
*mtod(m, int *) = dst->sa_family;
end:
if (var != NULL)
gif_putref_variant(var, &psref);
if (error)
if_statinc(ifp, if_oerrors);
return error;
}
static void
gif_start(struct ifnet *ifp)
{
struct gif_softc *sc;
struct gif_variant *var;
struct mbuf *m;
struct psref psref;
int family;
int len;
int error;
sc = ifp->if_softc;
var = gif_getref_variant(sc, &psref);
KASSERT(var->gv_output != NULL);
/* output processing */
while (1) {
IFQ_DEQUEUE(&sc->gif_if.if_snd, m);
if (m == NULL)
break;
/* grab and chop off inner af type */
if (sizeof(int) > m->m_len) {
m = m_pullup(m, sizeof(int));
if (!m) {
if_statinc(ifp, if_oerrors);
continue;
}
}
family = *mtod(m, int *);
bpf_mtap(ifp, m, BPF_D_OUT);
m_adj(m, sizeof(int));
/* grab and chop off inner af type */
if (sizeof(int) > m->m_len) {
m = m_pullup(m, sizeof(int));
if (!m) {
if_statinc(ifp, if_oerrors);
return ENOBUFS;
}
}
family = *mtod(m, int *);
bpf_mtap(ifp, m, BPF_D_OUT);
m_adj(m, sizeof(int));
void
gif_input(struct mbuf *m, int af, struct ifnet *ifp)
{
pktqueue_t *pktq;
size_t pktlen;
if (ifp == NULL) {
/* just in case */
m_freem(m);
return;
}
m_set_rcvif(m, ifp);
pktlen = m->m_pkthdr.len;
bpf_mtap_af(ifp, af, m, BPF_D_IN);
/*
* Put the packet to the network layer input queue according to the
* specified address family. Note: we avoid direct call to the
* input function of the network layer in order to avoid recursion.
* This may be revisited in the future.
*/
switch (af) {
#ifdef INET
case AF_INET:
pktq = ip_pktq;
break;
#endif
#ifdef INET6
case AF_INET6:
pktq = ip6_pktq;
break;
#endif
default:
m_freem(m);
return;
}
const uint32_t h = pktq_rps_hash(&gif_pktq_rps_hash_p, m);
if (__predict_true(pktq_enqueue(pktq, m, h))) {
if_statadd2(ifp, if_ibytes, pktlen, if_ipackets, 1);
} else {
m_freem(m);
}
}
/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
static int
gif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
struct gif_softc *sc = ifp->if_softc;
struct ifreq *ifr = (struct ifreq*)data;
struct ifaddr *ifa = (struct ifaddr*)data;
int error = 0, size, bound;
struct sockaddr *dst, *src;
struct gif_variant *var;
struct psref psref;
if (sc2 == sc)
continue;
var2 = gif_getref_variant(sc2, &psref);
if (!var2->gv_pdst || !var2->gv_psrc) {
gif_putref_variant(var2, &psref);
continue;
}
/* can't configure same pair of address onto two gifs */
if (sockaddr_cmp(var2->gv_pdst, dst) == 0 &&
sockaddr_cmp(var2->gv_psrc, src) == 0) {
/* continue to use the old configuration. */
gif_putref_variant(var2, &psref);
mutex_exit(&gif_softcs.lock);
error = EADDRNOTAVAIL;
goto out;
}
gif_putref_variant(var2, &psref);
/* XXX both end must be valid? (I mean, not 0.0.0.0) */
}
mutex_exit(&gif_softcs.lock);