2017-06-29 10:13:41 +03:00
|
|
|
/* $NetBSD: xform_ipip.c,v 1.50 2017/06/29 07:13:41 ozaki-r Exp $ */
|
2003-08-14 00:06:49 +04:00
|
|
|
/* $FreeBSD: src/sys/netipsec/xform_ipip.c,v 1.3.2.1 2003/01/24 05:11:36 sam Exp $ */
|
|
|
|
/* $OpenBSD: ip_ipip.c,v 1.25 2002/06/10 18:04:55 itojun Exp $ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2017-06-29 10:13:41 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: xform_ipip.c,v 1.50 2017/06/29 07:13:41 ozaki-r Exp $");
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* IP-inside-IP processing
|
|
|
|
*/
|
2017-04-06 12:20:07 +03:00
|
|
|
#if defined(_KERNEL_OPT)
|
2003-08-14 00:06:49 +04:00
|
|
|
#include "opt_inet.h"
|
2017-04-06 12:20:07 +03:00
|
|
|
#endif
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/kernel.h>
|
2016-01-23 02:27:12 +03:00
|
|
|
#include <sys/protosw.h>
|
2003-08-14 00:06:49 +04:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/netisr.h>
|
|
|
|
|
|
|
|
#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 <netinet/ip_encap.h>
|
|
|
|
|
|
|
|
#include <netipsec/ipsec.h>
|
2008-04-23 10:09:04 +04:00
|
|
|
#include <netipsec/ipsec_private.h>
|
2003-08-14 00:06:49 +04:00
|
|
|
#include <netipsec/xform.h>
|
|
|
|
|
|
|
|
#include <netipsec/ipip_var.h>
|
|
|
|
|
|
|
|
#ifdef MROUTING
|
|
|
|
#include <netinet/ip_mroute.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#include <netipsec/ipsec6.h>
|
|
|
|
#include <netinet6/in6_var.h>
|
2016-01-23 02:27:12 +03:00
|
|
|
#include <netinet6/ip6protosw.h>
|
2003-08-14 00:06:49 +04:00
|
|
|
#endif
|
|
|
|
|
2003-10-07 02:05:15 +04:00
|
|
|
#include <netipsec/key.h>
|
|
|
|
#include <netipsec/key_debug.h>
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
typedef void pr_in_input_t (struct mbuf *m, ...);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We can control the acceptance of IP4 packets by altering the sysctl
|
|
|
|
* net.inet.ipip.allow value. Zero means drop them, all else is acceptance.
|
|
|
|
*/
|
|
|
|
int ipip_allow = 0;
|
2008-04-23 10:09:04 +04:00
|
|
|
|
|
|
|
percpu_t *ipipstat_percpu;
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
#ifdef SYSCTL_DECL
|
|
|
|
SYSCTL_DECL(_net_inet_ipip);
|
|
|
|
|
|
|
|
SYSCTL_INT(_net_inet_ipip, OID_AUTO,
|
|
|
|
ipip_allow, CTLFLAG_RW, &ipip_allow, 0, "");
|
|
|
|
SYSCTL_STRUCT(_net_inet_ipip, IPSECCTL_STATS,
|
|
|
|
stats, CTLFLAG_RD, &ipipstat, ipipstat, "");
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void ipe4_attach(void);
|
|
|
|
|
|
|
|
|
|
|
|
/* XXX IPCOMP */
|
|
|
|
#define M_IPSEC (M_AUTHIPHDR|M_AUTHIPDGM|M_DECRYPTED)
|
|
|
|
|
|
|
|
static void _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp);
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
/*
|
|
|
|
* Really only a wrapper for ipip_input(), for use with IPv6.
|
|
|
|
*/
|
|
|
|
int
|
2006-11-16 04:32:37 +03:00
|
|
|
ip4_input6(struct mbuf **m, int *offp, int proto)
|
2003-08-14 00:06:49 +04:00
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
/* If we do not accept IP-in-IP explicitly, drop. */
|
|
|
|
if (!ipip_allow && ((*m)->m_flags & M_IPSEC) == 0) {
|
2017-04-16 01:01:57 +03:00
|
|
|
DPRINTF(("%s: dropped due to policy\n", __func__));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_PDROPS);
|
2003-08-14 00:06:49 +04:00
|
|
|
m_freem(*m);
|
|
|
|
return IPPROTO_DONE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
_ipip_input(*m, *offp, NULL);
|
|
|
|
return IPPROTO_DONE;
|
|
|
|
}
|
|
|
|
#endif /* INET6 */
|
|
|
|
|
|
|
|
#ifdef INET
|
|
|
|
/*
|
|
|
|
* Really only a wrapper for ipip_input(), for use with IPv4.
|
|
|
|
*/
|
|
|
|
void
|
2016-01-26 09:00:10 +03:00
|
|
|
ip4_input(struct mbuf *m, int off, int proto)
|
2003-08-14 00:06:49 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* If we do not accept IP-in-IP explicitly, drop. */
|
|
|
|
if (!ipip_allow && (m->m_flags & M_IPSEC) == 0) {
|
2017-04-16 01:01:57 +03:00
|
|
|
DPRINTF(("%s: dropped due to policy\n", __func__));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_PDROPS);
|
2003-08-14 00:06:49 +04:00
|
|
|
m_freem(m);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-01-26 09:00:10 +03:00
|
|
|
_ipip_input(m, off, NULL);
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
|
|
|
#endif /* INET */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ipip_input gets called when we receive an IP{46} encapsulated packet,
|
|
|
|
* either because we got it at a real interface, or because AH or ESP
|
|
|
|
* were being used in tunnel mode (in which case the rcvif element will
|
|
|
|
* contain the address of the encX interface associated with the tunnel.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2006-11-16 04:32:37 +03:00
|
|
|
_ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp)
|
2003-08-14 00:06:49 +04:00
|
|
|
{
|
|
|
|
register struct sockaddr_in *sin;
|
|
|
|
register struct ifnet *ifp;
|
|
|
|
register struct ifaddr *ifa;
|
2014-06-06 03:48:16 +04:00
|
|
|
pktqueue_t *pktq = NULL;
|
2003-08-14 00:06:49 +04:00
|
|
|
struct ip *ipo;
|
|
|
|
#ifdef INET6
|
|
|
|
register struct sockaddr_in6 *sin6;
|
|
|
|
struct ip6_hdr *ip6 = NULL;
|
2017-04-16 01:01:57 +03:00
|
|
|
uint8_t itos;
|
2003-08-14 00:06:49 +04:00
|
|
|
#endif
|
2017-04-16 01:01:57 +03:00
|
|
|
uint8_t otos;
|
|
|
|
uint8_t v;
|
2003-08-14 00:06:49 +04:00
|
|
|
int hlen;
|
|
|
|
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_IPACKETS);
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
m_copydata(m, 0, 1, &v);
|
|
|
|
|
|
|
|
switch (v >> 4) {
|
|
|
|
#ifdef INET
|
|
|
|
case 4:
|
|
|
|
hlen = sizeof(struct ip);
|
|
|
|
break;
|
|
|
|
#endif /* INET */
|
|
|
|
#ifdef INET6
|
|
|
|
case 6:
|
|
|
|
hlen = sizeof(struct ip6_hdr);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2017-04-16 01:01:57 +03:00
|
|
|
DPRINTF(("%s: bad protocol version 0x%x (%u) "
|
|
|
|
"for outer header\n", __func__, v, v>>4));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_FAMILY);
|
2003-08-14 00:06:49 +04:00
|
|
|
m_freem(m);
|
|
|
|
return /* EAFNOSUPPORT */;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bring the IP header in the first mbuf, if not there already */
|
|
|
|
if (m->m_len < hlen) {
|
|
|
|
if ((m = m_pullup(m, hlen)) == NULL) {
|
2017-04-16 01:01:57 +03:00
|
|
|
DPRINTF(("%s: m_pullup (1) failed\n", __func__));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_HDROPS);
|
2003-08-14 00:06:49 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ipo = mtod(m, struct ip *);
|
|
|
|
|
|
|
|
#ifdef MROUTING
|
|
|
|
if (ipo->ip_v == IPVERSION && ipo->ip_p == IPPROTO_IPV4) {
|
|
|
|
if (IN_MULTICAST(((struct ip *)((char *) ipo + iphlen))->ip_dst.s_addr)) {
|
|
|
|
ipip_mroute_input (m, iphlen);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MROUTING */
|
|
|
|
|
|
|
|
/* Keep outer ecn field. */
|
|
|
|
switch (v >> 4) {
|
|
|
|
#ifdef INET
|
|
|
|
case 4:
|
|
|
|
otos = ipo->ip_tos;
|
|
|
|
break;
|
|
|
|
#endif /* INET */
|
|
|
|
#ifdef INET6
|
|
|
|
case 6:
|
|
|
|
otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2017-04-16 01:01:57 +03:00
|
|
|
panic("%s: unknown ip version %u (outer)", __func__, v >> 4);
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove outer IP header */
|
|
|
|
m_adj(m, iphlen);
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
if (m->m_pkthdr.len < sizeof(struct ip)) {
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_HDROPS);
|
2003-08-14 00:06:49 +04:00
|
|
|
m_freem(m);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_copydata(m, 0, 1, &v);
|
|
|
|
|
|
|
|
switch (v >> 4) {
|
|
|
|
#ifdef INET
|
|
|
|
case 4:
|
|
|
|
hlen = sizeof(struct ip);
|
|
|
|
break;
|
|
|
|
#endif /* INET */
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
case 6:
|
|
|
|
hlen = sizeof(struct ip6_hdr);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2017-04-16 01:01:57 +03:00
|
|
|
DPRINTF(("%s: bad protocol version %#x (%u) "
|
|
|
|
"for inner header\n", __func__, v, v >> 4));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_FAMILY);
|
2003-08-14 00:06:49 +04:00
|
|
|
m_freem(m);
|
|
|
|
return; /* EAFNOSUPPORT */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bring the inner IP header in the first mbuf, if not there already.
|
|
|
|
*/
|
|
|
|
if (m->m_len < hlen) {
|
|
|
|
if ((m = m_pullup(m, hlen)) == NULL) {
|
2017-04-16 01:01:57 +03:00
|
|
|
DPRINTF(("%s: m_pullup (2) failed\n", __func__));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_HDROPS);
|
2003-08-14 00:06:49 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RFC 1853 specifies that the inner TTL should not be touched on
|
|
|
|
* decapsulation. There's no reason this comment should be here, but
|
|
|
|
* this is as good as any a position.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Some sanity checks in the inner IP header */
|
|
|
|
switch (v >> 4) {
|
|
|
|
#ifdef INET
|
|
|
|
case 4:
|
|
|
|
ipo = mtod(m, struct ip *);
|
|
|
|
ip_ecn_egress(ip4_ipsec_ecn, &otos, &ipo->ip_tos);
|
|
|
|
break;
|
|
|
|
#endif /* INET */
|
|
|
|
#ifdef INET6
|
|
|
|
case 6:
|
|
|
|
ip6 = (struct ip6_hdr *) ipo;
|
|
|
|
itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
|
|
|
|
ip_ecn_egress(ip6_ipsec_ecn, &otos, &itos);
|
|
|
|
ip6->ip6_flow &= ~htonl(0xff << 20);
|
2017-04-16 01:01:57 +03:00
|
|
|
ip6->ip6_flow |= htonl((uint32_t) itos << 20);
|
2003-08-14 00:06:49 +04:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2017-04-16 01:01:57 +03:00
|
|
|
panic("%s: unknown ip version %u (inner)", __func__, v>>4);
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for local address spoofing. */
|
2016-06-10 16:31:43 +03:00
|
|
|
if ((m_get_rcvif_NOMPSAFE(m) == NULL ||
|
|
|
|
!(m_get_rcvif_NOMPSAFE(m)->if_flags & IFF_LOOPBACK)) &&
|
2003-08-14 00:06:49 +04:00
|
|
|
ipip_allow != 2) {
|
2016-05-12 05:24:16 +03:00
|
|
|
int s = pserialize_read_enter();
|
|
|
|
IFNET_READER_FOREACH(ifp) {
|
2016-07-07 12:32:01 +03:00
|
|
|
IFADDR_READER_FOREACH(ifa, ifp) {
|
2003-08-14 00:06:49 +04:00
|
|
|
#ifdef INET
|
|
|
|
if (ipo) {
|
|
|
|
if (ifa->ifa_addr->sa_family !=
|
|
|
|
AF_INET)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sin = (struct sockaddr_in *) ifa->ifa_addr;
|
|
|
|
|
|
|
|
if (sin->sin_addr.s_addr ==
|
|
|
|
ipo->ip_src.s_addr) {
|
2016-05-12 05:24:16 +03:00
|
|
|
pserialize_read_exit(s);
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_SPOOF);
|
2003-08-14 00:06:49 +04:00
|
|
|
m_freem(m);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* INET */
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
if (ip6) {
|
|
|
|
if (ifa->ifa_addr->sa_family !=
|
|
|
|
AF_INET6)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
|
|
|
|
|
|
|
|
if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
|
2016-05-12 05:24:16 +03:00
|
|
|
pserialize_read_exit(s);
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_SPOOF);
|
2003-08-14 00:06:49 +04:00
|
|
|
m_freem(m);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif /* INET6 */
|
|
|
|
}
|
|
|
|
}
|
2016-05-12 05:24:16 +03:00
|
|
|
pserialize_read_exit(s);
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Statistics */
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATADD(IPIP_STAT_IBYTES, m->m_pkthdr.len - iphlen);
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Interface pointer stays the same; if no IPsec processing has
|
|
|
|
* been done (or will be done), this will point to a normal
|
|
|
|
* interface. Otherwise, it'll point to an enc interface, which
|
|
|
|
* will allow a packet filter to distinguish between secure and
|
|
|
|
* untrusted packets.
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (v >> 4) {
|
|
|
|
#ifdef INET
|
|
|
|
case 4:
|
2014-06-06 03:48:16 +04:00
|
|
|
pktq = ip_pktq;
|
2003-08-14 00:06:49 +04:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
case 6:
|
2014-06-06 03:48:16 +04:00
|
|
|
pktq = ip6_pktq;
|
2003-08-14 00:06:49 +04:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2017-04-16 01:01:57 +03:00
|
|
|
panic("%s: should never reach here", __func__);
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
|
|
|
|
2014-06-06 03:48:16 +04:00
|
|
|
int s = splnet();
|
|
|
|
if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_QFULL);
|
2014-06-06 03:48:16 +04:00
|
|
|
m_freem(m);
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
2014-06-06 03:48:16 +04:00
|
|
|
splx(s);
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ipip_output(
|
2006-10-14 00:53:59 +04:00
|
|
|
struct mbuf *m,
|
|
|
|
struct ipsecrequest *isr,
|
|
|
|
struct mbuf **mp,
|
2006-11-16 04:32:37 +03:00
|
|
|
int skip,
|
|
|
|
int protoff
|
2003-08-14 00:06:49 +04:00
|
|
|
)
|
|
|
|
{
|
2017-05-11 08:55:14 +03:00
|
|
|
char buf[IPSEC_ADDRSTRLEN];
|
2011-02-18 22:06:45 +03:00
|
|
|
const struct secasvar *sav;
|
2017-04-16 01:01:57 +03:00
|
|
|
uint8_t tp, otos;
|
2003-08-14 00:06:49 +04:00
|
|
|
struct secasindex *saidx;
|
|
|
|
int error;
|
|
|
|
#ifdef INET
|
2017-04-16 01:01:57 +03:00
|
|
|
uint8_t itos;
|
2003-08-14 00:06:49 +04:00
|
|
|
struct ip *ipo;
|
|
|
|
#endif /* INET */
|
|
|
|
#ifdef INET6
|
|
|
|
struct ip6_hdr *ip6, *ip6o;
|
|
|
|
#endif /* INET6 */
|
|
|
|
|
2017-04-16 01:01:57 +03:00
|
|
|
IPSEC_SPLASSERT_SOFTNET(__func__);
|
2003-08-14 00:06:49 +04:00
|
|
|
|
2017-04-18 08:26:41 +03:00
|
|
|
KASSERT(isr->sav != NULL);
|
2003-08-14 00:06:49 +04:00
|
|
|
sav = isr->sav;
|
2017-04-18 08:26:41 +03:00
|
|
|
KASSERT(sav->sah != NULL);
|
2003-08-14 00:06:49 +04: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) {
|
2017-04-16 01:01:57 +03:00
|
|
|
DPRINTF(("%s: unspecified tunnel endpoint "
|
|
|
|
"address in SA %s/%08lx\n", __func__,
|
2017-05-11 08:55:14 +03:00
|
|
|
ipsec_address(&saidx->dst, buf, sizeof(buf)),
|
2003-08-14 00:06:49 +04:00
|
|
|
(u_long) ntohl(sav->spi)));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_UNSPEC);
|
2003-08-14 00:06:49 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
|
|
|
|
if (m == 0) {
|
2017-04-16 01:01:57 +03:00
|
|
|
DPRINTF(("%s: M_PREPEND failed\n", __func__));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_HDROPS);
|
2003-08-14 00:06:49 +04: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);
|
|
|
|
ipo->ip_ttl = ip_defttl;
|
|
|
|
ipo->ip_sum = 0;
|
|
|
|
ipo->ip_src = saidx->src.sin.sin_addr;
|
|
|
|
ipo->ip_dst = saidx->dst.sin.sin_addr;
|
2008-02-11 00:42:20 +03:00
|
|
|
ipo->ip_id = ip_newid(NULL);
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
/* If the inner protocol is IP... */
|
|
|
|
if (tp == IPVERSION) {
|
|
|
|
/* Save ECN notification */
|
|
|
|
m_copydata(m, sizeof(struct ip) +
|
|
|
|
offsetof(struct ip, ip_tos),
|
2017-04-16 01:01:57 +03:00
|
|
|
sizeof(uint8_t), &itos);
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
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),
|
2017-04-16 01:01:57 +03:00
|
|
|
sizeof(uint16_t), &ipo->ip_off);
|
2017-04-19 06:39:14 +03:00
|
|
|
ipo->ip_off &= ~ htons(IP_DF | IP_MF | IP_OFFMASK);
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
|
|
|
#ifdef INET6
|
|
|
|
else if (tp == (IPV6_VERSION >> 4)) {
|
2017-04-16 01:01:57 +03:00
|
|
|
uint32_t itos32;
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
/* Save ECN notification. */
|
|
|
|
m_copydata(m, sizeof(struct ip) +
|
|
|
|
offsetof(struct ip6_hdr, ip6_flow),
|
2017-04-16 01:01:57 +03:00
|
|
|
sizeof(uint32_t), &itos32);
|
2003-08-14 00:06:49 +04:00
|
|
|
itos = ntohl(itos32) >> 20;
|
|
|
|
ipo->ip_p = IPPROTO_IPV6;
|
|
|
|
ipo->ip_off = 0;
|
|
|
|
}
|
|
|
|
#endif /* INET6 */
|
|
|
|
else {
|
|
|
|
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)) {
|
2017-04-16 01:01:57 +03:00
|
|
|
DPRINTF(("%s: unspecified tunnel endpoint "
|
|
|
|
"address in SA %s/%08lx\n", __func__,
|
2017-05-11 08:55:14 +03:00
|
|
|
ipsec_address(&saidx->dst, buf, sizeof(buf)),
|
2003-08-14 00:06:49 +04:00
|
|
|
(u_long) ntohl(sav->spi)));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_UNSPEC);
|
2003-08-14 00:06:49 +04:00
|
|
|
error = ENOBUFS;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2011-06-06 20:48:35 +04:00
|
|
|
if (tp == (IPV6_VERSION >> 4)) {
|
|
|
|
/* scoped address handling */
|
|
|
|
ip6 = mtod(m, struct ip6_hdr *);
|
|
|
|
if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
|
|
|
|
ip6->ip6_src.s6_addr16[1] = 0;
|
|
|
|
if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
|
|
|
|
ip6->ip6_dst.s6_addr16[1] = 0;
|
|
|
|
}
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
|
|
|
|
if (m == 0) {
|
2017-04-16 01:01:57 +03:00
|
|
|
DPRINTF(("%s: M_PREPEND failed\n", __func__));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_HDROPS);
|
2003-08-14 00:06:49 +04: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;
|
2017-04-15 01:35:05 +03:00
|
|
|
ip6o->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6o));
|
2003-08-14 00:06:49 +04:00
|
|
|
ip6o->ip6_hlim = ip_defttl;
|
|
|
|
ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
|
|
|
|
ip6o->ip6_src = saidx->src.sin6.sin6_addr;
|
2011-06-06 20:48:35 +04:00
|
|
|
if (IN6_IS_SCOPE_LINKLOCAL(&ip6o->ip6_dst))
|
|
|
|
ip6o->ip6_dst.s6_addr16[1] = htons(saidx->dst.sin6.sin6_scope_id);
|
|
|
|
if (IN6_IS_SCOPE_LINKLOCAL(&ip6o->ip6_src))
|
|
|
|
ip6o->ip6_src.s6_addr16[1] = htons(saidx->src.sin6.sin6_scope_id);
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
#ifdef INET
|
|
|
|
if (tp == IPVERSION) {
|
|
|
|
/* Save ECN notification */
|
|
|
|
m_copydata(m, sizeof(struct ip6_hdr) +
|
2017-04-16 01:01:57 +03:00
|
|
|
offsetof(struct ip, ip_tos), sizeof(uint8_t),
|
2007-03-05 00:17:54 +03:00
|
|
|
&itos);
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
/* This is really IPVERSION. */
|
|
|
|
ip6o->ip6_nxt = IPPROTO_IPIP;
|
|
|
|
} else
|
|
|
|
#endif /* INET */
|
|
|
|
if (tp == (IPV6_VERSION >> 4)) {
|
2017-04-16 01:01:57 +03:00
|
|
|
uint32_t itos32;
|
2003-08-14 00:06:49 +04:00
|
|
|
|
|
|
|
/* Save ECN notification. */
|
|
|
|
m_copydata(m, sizeof(struct ip6_hdr) +
|
|
|
|
offsetof(struct ip6_hdr, ip6_flow),
|
2017-04-16 01:01:57 +03:00
|
|
|
sizeof(uint32_t), &itos32);
|
2003-08-14 00:06:49 +04:00
|
|
|
itos = ntohl(itos32) >> 20;
|
|
|
|
|
|
|
|
ip6o->ip6_nxt = IPPROTO_IPV6;
|
|
|
|
} else {
|
|
|
|
goto nofamily;
|
|
|
|
}
|
|
|
|
|
|
|
|
otos = 0;
|
|
|
|
ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
|
2017-04-16 01:01:57 +03:00
|
|
|
ip6o->ip6_flow |= htonl((uint32_t) otos << 20);
|
2003-08-14 00:06:49 +04:00
|
|
|
break;
|
|
|
|
#endif /* INET6 */
|
|
|
|
|
|
|
|
default:
|
|
|
|
nofamily:
|
2017-04-16 01:01:57 +03:00
|
|
|
DPRINTF(("%s: unsupported protocol family %u\n", __func__,
|
2003-08-14 00:06:49 +04:00
|
|
|
saidx->dst.sa.sa_family));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_FAMILY);
|
2003-08-14 00:06:49 +04:00
|
|
|
error = EAFNOSUPPORT; /* XXX diffs from openbsd */
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATINC(IPIP_STAT_OPACKETS);
|
2003-08-14 00:06:49 +04:00
|
|
|
*mp = m;
|
|
|
|
|
|
|
|
#ifdef INET
|
|
|
|
if (saidx->dst.sa.sa_family == AF_INET) {
|
|
|
|
#if 0
|
|
|
|
if (sav->tdb_xform->xf_type == XF_IP4)
|
|
|
|
tdb->tdb_cur_bytes +=
|
|
|
|
m->m_pkthdr.len - sizeof(struct ip);
|
|
|
|
#endif
|
2008-04-23 10:09:04 +04:00
|
|
|
IPIP_STATADD(IPIP_STAT_OBYTES,
|
|
|
|
m->m_pkthdr.len - sizeof(struct ip));
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
|
|
|
#endif /* INET */
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
if (saidx->dst.sa.sa_family == AF_INET6) {
|
|
|
|
#if 0
|
|
|
|
if (sav->tdb_xform->xf_type == XF_IP4)
|
|
|
|
tdb->tdb_cur_bytes +=
|
|
|
|
m->m_pkthdr.len - sizeof(struct ip6_hdr);
|
|
|
|
#endif
|
2011-06-06 20:48:35 +04:00
|
|
|
IPIP_STATADD(IPIP_STAT_OBYTES,
|
2008-04-23 10:09:04 +04:00
|
|
|
m->m_pkthdr.len - sizeof(struct ip6_hdr));
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|
|
|
|
#endif /* INET6 */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
bad:
|
|
|
|
if (m)
|
2004-01-16 14:06:27 +03:00
|
|
|
m_freem(m);
|
|
|
|
*mp = NULL;
|
2003-08-14 00:06:49 +04:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-02-18 22:56:01 +03:00
|
|
|
ipe4_init(struct secasvar *sav, const struct xformsw *xsp)
|
2003-08-14 00:06:49 +04:00
|
|
|
{
|
|
|
|
sav->tdb_xform = xsp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ipe4_zeroize(struct secasvar *sav)
|
|
|
|
{
|
|
|
|
sav->tdb_xform = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-10-14 00:53:59 +04:00
|
|
|
ipe4_input(
|
|
|
|
struct mbuf *m,
|
2011-02-18 22:06:45 +03:00
|
|
|
const struct secasvar *sav,
|
2006-11-16 04:32:37 +03:00
|
|
|
int skip,
|
|
|
|
int protoff
|
2006-10-14 00:53:59 +04:00
|
|
|
)
|
2003-08-14 00:06:49 +04:00
|
|
|
{
|
|
|
|
/* This is a rather serious mistake, so no conditional printing. */
|
2017-04-16 01:01:57 +03:00
|
|
|
printf("%s: should never be called\n", __func__);
|
2003-08-14 00:06:49 +04:00
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct xformsw ipe4_xformsw = {
|
2017-06-29 10:13:41 +03:00
|
|
|
.xf_type = XF_IP4,
|
|
|
|
.xf_flags = 0,
|
|
|
|
.xf_name = "IPv4 Simple Encapsulation",
|
|
|
|
.xf_init = ipe4_init,
|
|
|
|
.xf_zeroize = ipe4_zeroize,
|
|
|
|
.xf_input = ipe4_input,
|
|
|
|
.xf_output = ipip_output,
|
|
|
|
.xf_next = NULL,
|
2003-08-14 00:06:49 +04:00
|
|
|
};
|
|
|
|
|
2007-02-10 12:43:05 +03:00
|
|
|
#ifdef INET
|
2016-01-26 08:58:05 +03:00
|
|
|
static struct encapsw ipe4_encapsw = {
|
|
|
|
.encapsw4 = {
|
|
|
|
.pr_input = ip4_input,
|
|
|
|
.pr_ctlinput = NULL,
|
|
|
|
}
|
2007-02-10 12:43:05 +03:00
|
|
|
};
|
2003-08-14 00:06:49 +04:00
|
|
|
#endif
|
2007-02-10 12:43:05 +03:00
|
|
|
#ifdef INET6
|
2016-01-26 08:58:05 +03:00
|
|
|
static struct encapsw ipe4_encapsw6 = {
|
|
|
|
.encapsw6 = {
|
|
|
|
.pr_input = ip4_input6,
|
|
|
|
.pr_ctlinput = NULL,
|
|
|
|
}
|
2003-08-14 00:06:49 +04:00
|
|
|
};
|
2007-02-10 12:43:05 +03:00
|
|
|
#endif
|
|
|
|
|
2003-08-14 00:06:49 +04:00
|
|
|
/*
|
|
|
|
* Check the encapsulated packet to see if we want it
|
|
|
|
*/
|
|
|
|
static int
|
2006-10-14 00:53:59 +04:00
|
|
|
ipe4_encapcheck(struct mbuf *m,
|
2006-11-16 04:32:37 +03:00
|
|
|
int off,
|
|
|
|
int proto,
|
|
|
|
void *arg
|
2006-10-14 00:53:59 +04:00
|
|
|
)
|
2003-08-14 00:06:49 +04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Only take packets coming from IPSEC tunnels; the rest
|
|
|
|
* must be handled by the gif tunnel code. Note that we
|
|
|
|
* also return a minimum priority when we want the packet
|
|
|
|
* so any explicit gif tunnels take precedence.
|
|
|
|
*/
|
|
|
|
return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2017-04-19 06:39:14 +03:00
|
|
|
void
|
2003-08-14 00:06:49 +04:00
|
|
|
ipe4_attach(void)
|
|
|
|
{
|
2008-04-23 10:09:04 +04:00
|
|
|
|
|
|
|
ipipstat_percpu = percpu_alloc(sizeof(uint64_t) * IPIP_NSTATS);
|
|
|
|
|
2003-08-14 00:06:49 +04:00
|
|
|
xform_register(&ipe4_xformsw);
|
|
|
|
/* attach to encapsulation framework */
|
|
|
|
/* XXX save return cookie for detach on module remove */
|
2016-07-04 07:35:09 +03:00
|
|
|
|
|
|
|
encapinit();
|
|
|
|
/* This function is called before ifinit(). Who else gets lock? */
|
|
|
|
(void)encap_lock_enter();
|
2016-07-04 07:17:25 +03:00
|
|
|
/* ipe4_encapsw and ipe4_encapsw must be added atomically */
|
2007-02-10 12:43:05 +03:00
|
|
|
#ifdef INET
|
2003-08-14 00:06:49 +04:00
|
|
|
(void) encap_attach_func(AF_INET, -1,
|
2016-01-26 08:58:05 +03:00
|
|
|
ipe4_encapcheck, &ipe4_encapsw, NULL);
|
2007-02-10 12:43:05 +03:00
|
|
|
#endif
|
2003-08-14 00:06:49 +04:00
|
|
|
#ifdef INET6
|
|
|
|
(void) encap_attach_func(AF_INET6, -1,
|
2016-01-26 08:58:05 +03:00
|
|
|
ipe4_encapcheck, &ipe4_encapsw6, NULL);
|
2003-08-14 00:06:49 +04:00
|
|
|
#endif
|
2016-07-04 07:17:25 +03:00
|
|
|
encap_lock_exit();
|
2003-08-14 00:06:49 +04:00
|
|
|
}
|