2002-10-16 02:10:08 +00:00
|
|
|
/* $FreeBSD$ */
|
|
|
|
/* $OpenBSD: ip_ipip.c,v 1.25 2002/06/10 18:04:55 itojun Exp $ */
|
2005-01-07 01:45:51 +00:00
|
|
|
/*-
|
2002-10-16 02:10:08 +00:00
|
|
|
* The authors of this code are John Ioannidis (ji@tla.org),
|
|
|
|
* Angelos D. Keromytis (kermit@csd.uch.gr) and
|
|
|
|
* Niels Provos (provos@physnet.uni-hamburg.de).
|
|
|
|
*
|
|
|
|
* The original version of this code was written by John Ioannidis
|
|
|
|
* for BSD/OS in Athens, Greece, in November 1995.
|
|
|
|
*
|
|
|
|
* Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
|
|
|
|
* by Angelos D. Keromytis.
|
|
|
|
*
|
|
|
|
* Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
|
|
|
|
* and Niels Provos.
|
|
|
|
*
|
|
|
|
* Additional features in 1999 by Angelos D. Keromytis.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
|
|
|
|
* Angelos D. Keromytis and Niels Provos.
|
|
|
|
* Copyright (c) 2001, Angelos D. Keromytis.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, and modify this software with or without fee
|
|
|
|
* is hereby granted, provided that this entire notice is included in
|
|
|
|
* all copies of any software which is or includes a copy or
|
|
|
|
* modification of this software.
|
|
|
|
* You may use this code under the GNU public license if you so wish. Please
|
|
|
|
* contribute changes back to the authors under this freer than GPL license
|
|
|
|
* so that we may further the use of strong encryption without limitations to
|
|
|
|
* all.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
|
|
|
|
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
|
|
|
|
* MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
|
|
|
|
* PURPOSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* IP-inside-IP processing
|
|
|
|
*/
|
|
|
|
#include "opt_inet.h"
|
|
|
|
#include "opt_inet6.h"
|
2006-06-26 22:30:08 +00:00
|
|
|
#include "opt_enc.h"
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
2013-10-26 17:58:36 +00:00
|
|
|
#include <net/if_var.h>
|
2007-07-19 09:57:54 +00:00
|
|
|
#include <net/pfil.h>
|
2002-10-16 02:10:08 +00:00
|
|
|
#include <net/netisr.h>
|
2008-12-02 21:37:28 +00:00
|
|
|
#include <net/vnet.h>
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/in_var.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_ecn.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
|
|
|
|
#include <netipsec/ipsec.h>
|
|
|
|
#include <netipsec/xform.h>
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#include <netipsec/ipsec6.h>
|
|
|
|
#include <netinet6/ip6_ecn.h>
|
|
|
|
#include <netinet6/in6_var.h>
|
2014-11-26 17:44:49 +00:00
|
|
|
#include <netinet6/scope6_var.h>
|
2002-10-16 02:10:08 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <netipsec/key.h>
|
|
|
|
#include <netipsec/key_debug.h>
|
|
|
|
|
|
|
|
int
|
2014-11-26 17:44:49 +00:00
|
|
|
ipip_output(struct mbuf *m, struct ipsecrequest *isr, struct mbuf **mp,
|
|
|
|
int skip, int protoff)
|
2002-10-16 02:10:08 +00:00
|
|
|
{
|
|
|
|
struct secasvar *sav;
|
|
|
|
u_int8_t tp, otos;
|
|
|
|
struct secasindex *saidx;
|
|
|
|
int error;
|
2011-04-27 19:28:42 +00:00
|
|
|
#if defined(INET) || defined(INET6)
|
2002-10-16 02:10:08 +00:00
|
|
|
u_int8_t itos;
|
2011-04-27 19:28:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef INET
|
2002-10-16 02:10:08 +00:00
|
|
|
struct ip *ipo;
|
|
|
|
#endif /* INET */
|
|
|
|
#ifdef INET6
|
|
|
|
struct ip6_hdr *ip6, *ip6o;
|
|
|
|
#endif /* INET6 */
|
|
|
|
|
|
|
|
sav = isr->sav;
|
2003-09-29 22:57:43 +00:00
|
|
|
IPSEC_ASSERT(sav != NULL, ("null SA"));
|
|
|
|
IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* XXX Deal with empty TDB source/destination addresses. */
|
|
|
|
|
|
|
|
m_copydata(m, 0, 1, &tp);
|
|
|
|
tp = (tp >> 4) & 0xff; /* Get the IP version number. */
|
|
|
|
|
|
|
|
saidx = &sav->sah->saidx;
|
|
|
|
switch (saidx->dst.sa.sa_family) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
|
|
|
if (saidx->src.sa.sa_family != AF_INET ||
|
|
|
|
saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
|
|
|
|
saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
|
2003-09-29 22:57:43 +00:00
|
|
|
DPRINTF(("%s: unspecified tunnel endpoint "
|
|
|
|
"address in SA %s/%08lx\n", __func__,
|
2002-10-16 02:10:08 +00:00
|
|
|
ipsec_address(&saidx->dst),
|
|
|
|
(u_long) ntohl(sav->spi)));
|
|
|
|
error = EINVAL;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2012-12-05 08:04:20 +00:00
|
|
|
M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (m == 0) {
|
2003-09-29 22:57:43 +00:00
|
|
|
DPRINTF(("%s: M_PREPEND failed\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
error = ENOBUFS;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipo = mtod(m, struct ip *);
|
|
|
|
|
|
|
|
ipo->ip_v = IPVERSION;
|
|
|
|
ipo->ip_hl = 5;
|
|
|
|
ipo->ip_len = htons(m->m_pkthdr.len);
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
ipo->ip_ttl = V_ip_defttl;
|
2002-10-16 02:10:08 +00:00
|
|
|
ipo->ip_sum = 0;
|
|
|
|
ipo->ip_src = saidx->src.sin.sin_addr;
|
|
|
|
ipo->ip_dst = saidx->dst.sin.sin_addr;
|
|
|
|
|
2004-08-14 15:32:40 +00:00
|
|
|
ipo->ip_id = ip_newid();
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
/* If the inner protocol is IP... */
|
2011-04-27 19:28:42 +00:00
|
|
|
switch (tp) {
|
|
|
|
case IPVERSION:
|
2002-10-16 02:10:08 +00:00
|
|
|
/* Save ECN notification */
|
|
|
|
m_copydata(m, sizeof(struct ip) +
|
|
|
|
offsetof(struct ip, ip_tos),
|
|
|
|
sizeof(u_int8_t), (caddr_t) &itos);
|
|
|
|
|
|
|
|
ipo->ip_p = IPPROTO_IPIP;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We should be keeping tunnel soft-state and
|
|
|
|
* send back ICMPs if needed.
|
|
|
|
*/
|
|
|
|
m_copydata(m, sizeof(struct ip) +
|
|
|
|
offsetof(struct ip, ip_off),
|
|
|
|
sizeof(u_int16_t), (caddr_t) &ipo->ip_off);
|
|
|
|
ipo->ip_off = ntohs(ipo->ip_off);
|
|
|
|
ipo->ip_off &= ~(IP_DF | IP_MF | IP_OFFMASK);
|
|
|
|
ipo->ip_off = htons(ipo->ip_off);
|
2011-04-27 19:28:42 +00:00
|
|
|
break;
|
2002-10-16 02:10:08 +00:00
|
|
|
#ifdef INET6
|
2011-04-27 19:28:42 +00:00
|
|
|
case (IPV6_VERSION >> 4):
|
|
|
|
{
|
2002-10-16 02:10:08 +00:00
|
|
|
u_int32_t itos32;
|
|
|
|
|
|
|
|
/* Save ECN notification. */
|
|
|
|
m_copydata(m, sizeof(struct ip) +
|
|
|
|
offsetof(struct ip6_hdr, ip6_flow),
|
|
|
|
sizeof(u_int32_t), (caddr_t) &itos32);
|
|
|
|
itos = ntohl(itos32) >> 20;
|
|
|
|
ipo->ip_p = IPPROTO_IPV6;
|
|
|
|
ipo->ip_off = 0;
|
2011-04-27 19:28:42 +00:00
|
|
|
break;
|
2002-10-16 02:10:08 +00:00
|
|
|
}
|
|
|
|
#endif /* INET6 */
|
2011-04-27 19:28:42 +00:00
|
|
|
default:
|
2002-10-16 02:10:08 +00:00
|
|
|
goto nofamily;
|
|
|
|
}
|
|
|
|
|
|
|
|
otos = 0;
|
|
|
|
ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
|
|
|
|
ipo->ip_tos = otos;
|
|
|
|
break;
|
|
|
|
#endif /* INET */
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
|
|
|
|
saidx->src.sa.sa_family != AF_INET6 ||
|
|
|
|
IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
|
2003-09-29 22:57:43 +00:00
|
|
|
DPRINTF(("%s: unspecified tunnel endpoint "
|
|
|
|
"address in SA %s/%08lx\n", __func__,
|
2002-10-16 02:10:08 +00:00
|
|
|
ipsec_address(&saidx->dst),
|
|
|
|
(u_long) ntohl(sav->spi)));
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* scoped address handling */
|
|
|
|
ip6 = mtod(m, struct ip6_hdr *);
|
2014-11-26 17:44:49 +00:00
|
|
|
in6_clearscope(&ip6->ip6_src);
|
|
|
|
in6_clearscope(&ip6->ip6_dst);
|
2012-12-05 08:04:20 +00:00
|
|
|
M_PREPEND(m, sizeof(struct ip6_hdr), M_NOWAIT);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (m == 0) {
|
2003-09-29 22:57:43 +00:00
|
|
|
DPRINTF(("%s: M_PREPEND failed\n", __func__));
|
2002-10-16 02:10:08 +00:00
|
|
|
error = ENOBUFS;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize IPv6 header */
|
|
|
|
ip6o = mtod(m, struct ip6_hdr *);
|
|
|
|
ip6o->ip6_flow = 0;
|
|
|
|
ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
|
|
|
|
ip6o->ip6_vfc |= IPV6_VERSION;
|
2014-05-28 12:45:27 +00:00
|
|
|
ip6o->ip6_hlim = IPV6_DEFHLIM;
|
2002-10-16 02:10:08 +00:00
|
|
|
ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
|
|
|
|
ip6o->ip6_src = saidx->src.sin6.sin6_addr;
|
2014-05-28 12:45:27 +00:00
|
|
|
ip6o->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
|
|
|
|
|
2011-04-27 19:28:42 +00:00
|
|
|
switch (tp) {
|
2002-10-16 02:10:08 +00:00
|
|
|
#ifdef INET
|
2011-04-27 19:28:42 +00:00
|
|
|
case IPVERSION:
|
2002-10-16 02:10:08 +00:00
|
|
|
/* Save ECN notification */
|
|
|
|
m_copydata(m, sizeof(struct ip6_hdr) +
|
|
|
|
offsetof(struct ip, ip_tos), sizeof(u_int8_t),
|
|
|
|
(caddr_t) &itos);
|
|
|
|
|
|
|
|
/* This is really IPVERSION. */
|
|
|
|
ip6o->ip6_nxt = IPPROTO_IPIP;
|
2011-04-27 19:28:42 +00:00
|
|
|
break;
|
2002-10-16 02:10:08 +00:00
|
|
|
#endif /* INET */
|
2011-04-27 19:28:42 +00:00
|
|
|
case (IPV6_VERSION >> 4):
|
|
|
|
{
|
|
|
|
u_int32_t itos32;
|
|
|
|
|
|
|
|
/* Save ECN notification. */
|
|
|
|
m_copydata(m, sizeof(struct ip6_hdr) +
|
|
|
|
offsetof(struct ip6_hdr, ip6_flow),
|
|
|
|
sizeof(u_int32_t), (caddr_t) &itos32);
|
|
|
|
itos = ntohl(itos32) >> 20;
|
|
|
|
|
|
|
|
ip6o->ip6_nxt = IPPROTO_IPV6;
|
2012-09-18 08:00:43 +00:00
|
|
|
break;
|
2011-04-27 19:28:42 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
goto nofamily;
|
|
|
|
}
|
2002-10-16 02:10:08 +00:00
|
|
|
|
|
|
|
otos = 0;
|
2014-05-28 12:45:27 +00:00
|
|
|
ip_ecn_ingress(V_ip6_ipsec_ecn, &otos, &itos);
|
2002-10-16 02:10:08 +00:00
|
|
|
ip6o->ip6_flow |= htonl((u_int32_t) otos << 20);
|
|
|
|
break;
|
|
|
|
#endif /* INET6 */
|
|
|
|
|
|
|
|
default:
|
|
|
|
nofamily:
|
2003-09-29 22:57:43 +00:00
|
|
|
DPRINTF(("%s: unsupported protocol family %u\n", __func__,
|
2002-10-16 02:10:08 +00:00
|
|
|
saidx->dst.sa.sa_family));
|
|
|
|
error = EAFNOSUPPORT; /* XXX diffs from openbsd */
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
*mp = m;
|
2014-11-26 17:44:49 +00:00
|
|
|
return (0);
|
2002-10-16 02:10:08 +00:00
|
|
|
bad:
|
|
|
|
if (m)
|
2004-01-20 22:45:10 +00:00
|
|
|
m_freem(m);
|
|
|
|
*mp = NULL;
|
2002-10-16 02:10:08 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ipe4_init(struct secasvar *sav, struct xformsw *xsp)
|
|
|
|
{
|
|
|
|
sav->tdb_xform = xsp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ipe4_zeroize(struct secasvar *sav)
|
|
|
|
{
|
|
|
|
sav->tdb_xform = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ipe4_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
|
|
|
|
{
|
|
|
|
/* This is a rather serious mistake, so no conditional printing. */
|
2003-09-29 22:57:43 +00:00
|
|
|
printf("%s: should never be called\n", __func__);
|
2002-10-16 02:10:08 +00:00
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct xformsw ipe4_xformsw = {
|
|
|
|
XF_IP4, 0, "IPv4 Simple Encapsulation",
|
|
|
|
ipe4_init, ipe4_zeroize, ipe4_input, ipip_output,
|
|
|
|
};
|
|
|
|
|
First pass at separating per-vnet initializer functions
from existing functions for initializing global state.
At this stage, the new per-vnet initializer functions are
directly called from the existing global initialization code,
which should in most cases result in compiler inlining those
new functions, hence yielding a near-zero functional change.
Modify the existing initializer functions which are invoked via
protosw, like ip_init() et. al., to allow them to be invoked
multiple times, i.e. per each vnet. Global state, if any,
is initialized only if such functions are called within the
context of vnet0, which will be determined via the
IS_DEFAULT_VNET(curvnet) check (currently always true).
While here, V_irtualize a few remaining global UMA zones
used by net/netinet/netipsec networking code. While it is
not yet clear to me or anybody else whether this is the right
thing to do, at this stage this makes the code more readable,
and makes it easier to track uncollected UMA-zone-backed
objects on vnet removal. In the long run, it's quite possible
that some form of shared use of UMA zone pools among multiple
vnets should be considered.
Bump __FreeBSD_version due to changes in layout of structs
vnet_ipfw, vnet_inet and vnet_net.
Approved by: julian (mentor)
2009-04-06 22:29:41 +00:00
|
|
|
static void
|
|
|
|
ipe4_attach(void)
|
|
|
|
{
|
2008-11-19 09:39:34 +00:00
|
|
|
|
2002-10-16 02:10:08 +00:00
|
|
|
xform_register(&ipe4_xformsw);
|
|
|
|
}
|
|
|
|
SYSINIT(ipe4_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipe4_attach, NULL);
|