2005-01-07 01:45:51 +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.
|
2014-10-14 13:31:47 +00:00
|
|
|
*
|
|
|
|
* $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $
|
1999-12-07 17:39:16 +00:00
|
|
|
*/
|
|
|
|
|
2007-10-07 20:44:24 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2000-07-04 16:35:15 +00:00
|
|
|
#include "opt_inet.h"
|
1999-12-07 17:39:16 +00:00
|
|
|
#include "opt_inet6.h"
|
|
|
|
|
|
|
|
#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>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/sysctl.h>
|
2002-10-16 19:49:37 +00:00
|
|
|
#include <sys/protosw.h>
|
2000-07-04 16:35:15 +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>
|
2009-08-01 19:26:27 +00:00
|
|
|
#include <net/vnet.h>
|
1999-12-07 17:39:16 +00:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_var.h>
|
2000-07-04 16:35:15 +00:00
|
|
|
#include <netinet/in_var.h>
|
|
|
|
#include <netinet/ip_encap.h>
|
1999-12-22 19:13:38 +00:00
|
|
|
#include <netinet/ip_ecn.h>
|
Merge helper fib* functions used for basic lookups.
Vast majority of rtalloc(9) users require only basic info from
route table (e.g. "does the rtentry interface match with the interface
I have?". "what is the MTU?", "Give me the IPv4 source address to use",
etc..).
Instead of hand-rolling lookups, checking if rtentry is up, valid,
dealing with IPv6 mtu, finding "address" ifp (almost never done right),
provide easy-to-use API hiding all the complexity and returning the
needed info into small on-stack structure.
This change also helps hiding route subsystem internals (locking, direct
rtentry accesses).
Additionaly, using this API improves lookup performance since rtentry is not
locked.
(This is safe, since all the rtentry changes happens under both radix WLOCK
and rtentry WLOCK).
Sponsored by: Yandex LLC
2015-12-08 10:50:03 +00:00
|
|
|
#include <netinet/in_fib.h>
|
2000-07-04 16:35:15 +00:00
|
|
|
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
2000-07-04 16:35:15 +00:00
|
|
|
#include <netinet/ip6.h>
|
1999-12-07 17:39:16 +00:00
|
|
|
#endif
|
|
|
|
|
2014-10-14 13:31:47 +00:00
|
|
|
#include <net/if_gif.h>
|
1999-12-07 17:39:16 +00:00
|
|
|
|
2014-12-23 16:17:37 +00:00
|
|
|
static int in_gif_input(struct mbuf **, int *, int);
|
2002-10-16 19:49:37 +00:00
|
|
|
|
|
|
|
extern struct domain inetdomain;
|
2014-12-23 16:17:37 +00:00
|
|
|
static struct protosw in_gif_protosw = {
|
2005-11-09 13:29:16 +00:00
|
|
|
.pr_type = SOCK_RAW,
|
|
|
|
.pr_domain = &inetdomain,
|
|
|
|
.pr_protocol = 0/* IPPROTO_IPV[46] */,
|
|
|
|
.pr_flags = PR_ATOMIC|PR_ADDR,
|
|
|
|
.pr_input = in_gif_input,
|
2014-08-15 02:43:02 +00:00
|
|
|
.pr_output = rip_output,
|
2005-11-09 13:29:16 +00:00
|
|
|
.pr_ctloutput = rip_ctloutput,
|
|
|
|
.pr_usrreqs = &rip_usrreqs
|
2002-10-16 19:49:37 +00:00
|
|
|
};
|
|
|
|
|
2014-12-23 16:17:37 +00:00
|
|
|
#define GIF_TTL 30
|
|
|
|
static VNET_DEFINE(int, ip_gif_ttl) = GIF_TTL;
|
2010-04-29 11:52:42 +00:00
|
|
|
#define V_ip_gif_ttl VNET(ip_gif_ttl)
|
2014-11-07 09:39:05 +00:00
|
|
|
SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_VNET | CTLFLAG_RW,
|
Build on Jeff Roberson's linker-set based dynamic per-CPU allocator
(DPCPU), as suggested by Peter Wemm, and implement a new per-virtual
network stack memory allocator. Modify vnet to use the allocator
instead of monolithic global container structures (vinet, ...). This
change solves many binary compatibility problems associated with
VIMAGE, and restores ELF symbols for virtualized global variables.
Each virtualized global variable exists as a "reference copy", and also
once per virtual network stack. Virtualized global variables are
tagged at compile-time, placing the in a special linker set, which is
loaded into a contiguous region of kernel memory. Virtualized global
variables in the base kernel are linked as normal, but those in modules
are copied and relocated to a reserved portion of the kernel's vnet
region with the help of a the kernel linker.
Virtualized global variables exist in per-vnet memory set up when the
network stack instance is created, and are initialized statically from
the reference copy. Run-time access occurs via an accessor macro, which
converts from the current vnet and requested symbol to a per-vnet
address. When "options VIMAGE" is not compiled into the kernel, normal
global ELF symbols will be used instead and indirection is avoided.
This change restores static initialization for network stack global
variables, restores support for non-global symbols and types, eliminates
the need for many subsystem constructors, eliminates large per-subsystem
structures that caused many binary compatibility issues both for
monitoring applications (netstat) and kernel modules, removes the
per-function INIT_VNET_*() macros throughout the stack, eliminates the
need for vnet_symmap ksym(2) munging, and eliminates duplicate
definitions of virtualized globals under VIMAGE_GLOBALS.
Bump __FreeBSD_version and update UPDATING.
Portions submitted by: bz
Reviewed by: bz, zec
Discussed with: gnn, jamie, jeff, jhb, julian, sam
Suggested by: peter
Approved by: re (kensmith)
2009-07-14 22:48:30 +00:00
|
|
|
&VNET_NAME(ip_gif_ttl), 0, "");
|
1999-12-07 17:39:16 +00:00
|
|
|
|
|
|
|
int
|
2014-10-14 13:31:47 +00:00
|
|
|
in_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;
|
2014-10-14 13:31:47 +00:00
|
|
|
struct ip *ip;
|
|
|
|
int len;
|
1999-12-07 17:39:16 +00:00
|
|
|
|
|
|
|
/* prepend new IP header */
|
2009-03-07 19:08:58 +00:00
|
|
|
len = sizeof(struct ip);
|
|
|
|
#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,
|
|
|
|
("in_gif_output: unexpected misalignment"));
|
|
|
|
m->m_data += len;
|
|
|
|
m->m_len -= ETHERIP_ALIGN;
|
|
|
|
}
|
|
|
|
#endif
|
2014-10-14 13:31:47 +00:00
|
|
|
ip = mtod(m, struct ip *);
|
|
|
|
GIF_RLOCK(sc);
|
|
|
|
if (sc->gif_family != AF_INET) {
|
|
|
|
m_freem(m);
|
|
|
|
GIF_RUNLOCK(sc);
|
|
|
|
return (ENETDOWN);
|
1999-12-07 17:39:16 +00:00
|
|
|
}
|
2014-10-14 13:31:47 +00:00
|
|
|
bcopy(sc->gif_iphdr, ip, sizeof(struct ip));
|
|
|
|
GIF_RUNLOCK(sc);
|
1999-12-07 17:39:16 +00:00
|
|
|
|
2014-10-14 13:31:47 +00:00
|
|
|
ip->ip_p = proto;
|
|
|
|
/* version will be set in ip_output() */
|
|
|
|
ip->ip_ttl = V_ip_gif_ttl;
|
|
|
|
ip->ip_len = htons(m->m_pkthdr.len);
|
|
|
|
ip->ip_tos = ecn;
|
2004-12-06 19:02:43 +00:00
|
|
|
|
2014-10-14 13:31:47 +00:00
|
|
|
return (ip_output(m, NULL, NULL, 0, NULL, NULL));
|
1999-12-07 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 16:17:37 +00:00
|
|
|
static int
|
2014-08-08 01:57:15 +00:00
|
|
|
in_gif_input(struct mbuf **mp, int *offp, int proto)
|
1999-12-07 17:39:16 +00:00
|
|
|
{
|
2014-10-14 13:31:47 +00:00
|
|
|
struct mbuf *m = *mp;
|
2005-06-20 08:39:30 +00:00
|
|
|
struct gif_softc *sc;
|
2014-10-14 13:31:47 +00:00
|
|
|
struct ifnet *gifp;
|
1999-12-07 17:39:16 +00:00
|
|
|
struct ip *ip;
|
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 08:39:30 +00:00
|
|
|
if (sc == NULL) {
|
|
|
|
m_freem(m);
|
2009-08-02 19:43:32 +00:00
|
|
|
KMOD_IPSTAT_INC(ips_nogif);
|
2014-08-08 01:57:15 +00:00
|
|
|
return (IPPROTO_DONE);
|
2005-06-20 08:39:30 +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
|
|
|
ip = mtod(m, struct ip *);
|
2014-10-14 13:31:47 +00:00
|
|
|
ecn = ip->ip_tos;
|
|
|
|
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
|
|
|
KMOD_IPSTAT_INC(ips_nogif);
|
1999-12-07 17:39:16 +00:00
|
|
|
}
|
2014-08-08 01:57:15 +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
|
|
|
|
in_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 ip *ip;
|
|
|
|
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);
|
2000-07-04 16:35:15 +00:00
|
|
|
|
|
|
|
/* check for address match */
|
2015-07-29 14:07:43 +00:00
|
|
|
ip = mtod(m, const struct ip *);
|
2015-05-15 12:19:45 +00:00
|
|
|
if (sc->gif_iphdr->ip_src.s_addr != ip->ip_dst.s_addr)
|
2014-10-14 13:31:47 +00:00
|
|
|
return (0);
|
2015-05-15 12:19:45 +00:00
|
|
|
ret = 32;
|
|
|
|
if (sc->gif_iphdr->ip_dst.s_addr != ip->ip_src.s_addr) {
|
|
|
|
if ((sc->gif_options & GIF_IGNORE_SOURCE) == 0)
|
|
|
|
return (0);
|
|
|
|
} else
|
|
|
|
ret += 32;
|
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) {
|
Merge helper fib* functions used for basic lookups.
Vast majority of rtalloc(9) users require only basic info from
route table (e.g. "does the rtentry interface match with the interface
I have?". "what is the MTU?", "Give me the IPv4 source address to use",
etc..).
Instead of hand-rolling lookups, checking if rtentry is up, valid,
dealing with IPv6 mtu, finding "address" ifp (almost never done right),
provide easy-to-use API hiding all the complexity and returning the
needed info into small on-stack structure.
This change also helps hiding route subsystem internals (locking, direct
rtentry accesses).
Additionaly, using this API improves lookup performance since rtentry is not
locked.
(This is safe, since all the rtentry changes happens under both radix WLOCK
and rtentry WLOCK).
Sponsored by: Yandex LLC
2015-12-08 10:50:03 +00:00
|
|
|
struct nhop4_basic nh4;
|
|
|
|
struct in_addr dst;
|
|
|
|
|
|
|
|
dst = ip->ip_src;
|
|
|
|
|
|
|
|
if (fib4_lookup_nh_basic(sc->gif_fibnum, dst, 0, 0, &nh4) != 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (nh4.nh_ifp != m->m_pkthdr.rcvif)
|
2014-10-14 13:31:47 +00:00
|
|
|
return (0);
|
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-05-10 15:58:48 +00:00
|
|
|
in_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_INET, -1, gif_encapcheck,
|
|
|
|
&in_gif_protosw, sc);
|
|
|
|
if (sc->gif_ecookie == NULL)
|
|
|
|
return (EEXIST);
|
|
|
|
return (0);
|
2002-10-16 19:49:37 +00:00
|
|
|
}
|