#include <sys/types.h>
#include <sys/kmem.h>
#include <sys/errno.h>
#include <sys/modhash.h>
#include <sys/list.h>
#include <sys/strsun.h>
#include <sys/file.h>
#include <sys/systm.h>
#include <sys/tihdr.h>
#include <sys/param.h>
#include <sys/mac_provider.h>
#include <sys/mac_ipv4.h>
#include <sys/mac_ipv6.h>
#include <sys/mac_6to4.h>
#include <sys/tsol/tnet.h>
#include <sys/sunldi.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <inet/ip.h>
#include <inet/ip_ire.h>
#include <inet/ipsec_impl.h>
#include <sys/tsol/label.h>
#include <sys/tsol/tnet.h>
#include <inet/iptun.h>
#include "iptun_impl.h"
#define IPTUN_ADDR_MATCH(iptun_type, family) \
((iptun_type == IPTUN_TYPE_IPV4 && family == AF_INET) || \
(iptun_type == IPTUN_TYPE_IPV6 && family == AF_INET6) || \
(iptun_type == IPTUN_TYPE_6TO4 && family == AF_INET))
#define IPTUN_HASH_KEY(key) ((mod_hash_key_t)(uintptr_t)(key))
#define IPTUN_MIN_IPV4_MTU 576
#define IPTUN_MIN_IPV6_MTU IPV6_MIN_MTU
#define IPTUN_MAX_IPV4_MTU (IP_MAXPACKET - sizeof (ipha_t))
#define IPTUN_MAX_IPV6_MTU (IP_MAXPACKET - sizeof (ip6_t) - \
sizeof (iptun_encaplim_t))
#define IPTUN_MIN_HOPLIMIT 1
#define IPTUN_MAX_HOPLIMIT UINT8_MAX
#define IPTUN_MIN_ENCAPLIMIT 0
#define IPTUN_MAX_ENCAPLIMIT UINT8_MAX
#define IPTUN_IPSEC_REQ_MASK (IPSEC_PREF_REQUIRED | IPSEC_PREF_NEVER)
static iptun_encaplim_t iptun_encaplim_init = {
{ IPPROTO_NONE, 0 },
IP6OPT_TUNNEL_LIMIT,
1,
IPTUN_DEFAULT_ENCAPLIMIT,
IP6OPT_PADN,
1,
0
};
static iptun_typeinfo_t iptun_type_table[] = {
{ IPTUN_TYPE_IPV4, MAC_PLUGIN_IDENT_IPV4, IPV4_VERSION,
IPTUN_MIN_IPV6_MTU, IPTUN_MAX_IPV4_MTU, B_TRUE },
{ IPTUN_TYPE_IPV6, MAC_PLUGIN_IDENT_IPV6, IPV6_VERSION,
IPTUN_MIN_IPV6_MTU, IPTUN_MAX_IPV6_MTU, B_TRUE },
{ IPTUN_TYPE_6TO4, MAC_PLUGIN_IDENT_6TO4, IPV4_VERSION,
IPTUN_MIN_IPV6_MTU, IPTUN_MAX_IPV4_MTU, B_FALSE },
{ IPTUN_TYPE_UNKNOWN, NULL, 0, 0, 0, B_FALSE }
};
mod_hash_t *iptun_hash;
static kmutex_t iptun_hash_lock;
static uint_t iptun_tunnelcount;
kmem_cache_t *iptun_cache;
ddi_taskq_t *iptun_taskq;
typedef enum {
IPTUN_TASK_MTU_UPDATE,
IPTUN_TASK_LADDR_UPDATE,
IPTUN_TASK_RADDR_UPDATE,
IPTUN_TASK_LINK_UPDATE,
IPTUN_TASK_PDATA_UPDATE
} iptun_task_t;
typedef struct iptun_task_data_s {
iptun_task_t itd_task;
datalink_id_t itd_linkid;
} iptun_task_data_t;
static void iptun_task_dispatch(iptun_t *, iptun_task_t);
static int iptun_enter(iptun_t *);
static void iptun_exit(iptun_t *);
static void iptun_headergen(iptun_t *, boolean_t);
static void iptun_drop_pkt(mblk_t *, uint64_t *);
static void iptun_input(void *, mblk_t *, void *, ip_recv_attr_t *);
static void iptun_input_icmp(void *, mblk_t *, void *, ip_recv_attr_t *);
static void iptun_output(iptun_t *, mblk_t *);
static uint32_t iptun_get_maxmtu(iptun_t *, ip_xmit_attr_t *, uint32_t);
static uint32_t iptun_update_mtu(iptun_t *, ip_xmit_attr_t *, uint32_t);
static uint32_t iptun_get_dst_pmtu(iptun_t *, ip_xmit_attr_t *);
static void iptun_update_dst_pmtu(iptun_t *, ip_xmit_attr_t *);
static int iptun_setladdr(iptun_t *, const struct sockaddr_storage *);
static void iptun_output_6to4(iptun_t *, mblk_t *);
static void iptun_output_common(iptun_t *, ip_xmit_attr_t *, mblk_t *);
static boolean_t iptun_verifyicmp(conn_t *, void *, icmph_t *, icmp6_t *,
ip_recv_attr_t *);
static void iptun_notify(void *, ip_xmit_attr_t *, ixa_notify_type_t,
ixa_notify_arg_t);
static mac_callbacks_t iptun_m_callbacks;
static int
iptun_m_getstat(void *arg, uint_t stat, uint64_t *val)
{
iptun_t *iptun = arg;
int err = 0;
switch (stat) {
case MAC_STAT_IERRORS:
*val = iptun->iptun_ierrors;
break;
case MAC_STAT_OERRORS:
*val = iptun->iptun_oerrors;
break;
case MAC_STAT_RBYTES:
*val = iptun->iptun_rbytes;
break;
case MAC_STAT_IPACKETS:
*val = iptun->iptun_ipackets;
break;
case MAC_STAT_OBYTES:
*val = iptun->iptun_obytes;
break;
case MAC_STAT_OPACKETS:
*val = iptun->iptun_opackets;
break;
case MAC_STAT_NORCVBUF:
*val = iptun->iptun_norcvbuf;
break;
case MAC_STAT_NOXMTBUF:
*val = iptun->iptun_noxmtbuf;
break;
default:
err = ENOTSUP;
}
return (err);
}
static int
iptun_m_start(void *arg)
{
iptun_t *iptun = arg;
int err;
if ((err = iptun_enter(iptun)) == 0) {
iptun->iptun_flags |= IPTUN_MAC_STARTED;
iptun_task_dispatch(iptun, IPTUN_TASK_LINK_UPDATE);
iptun_exit(iptun);
}
return (err);
}
static void
iptun_m_stop(void *arg)
{
iptun_t *iptun = arg;
if (iptun_enter(iptun) == 0) {
iptun->iptun_flags &= ~IPTUN_MAC_STARTED;
iptun_task_dispatch(iptun, IPTUN_TASK_LINK_UPDATE);
iptun_exit(iptun);
}
}
static int
iptun_m_setpromisc(void *arg, boolean_t on)
{
return (0);
}
static int
iptun_m_multicst(void *arg, boolean_t add, const uint8_t *addrp)
{
return (ENOTSUP);
}
static int
iptun_m_unicst(void *arg, const uint8_t *addrp)
{
iptun_t *iptun = arg;
int err;
struct sockaddr_storage ss;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
if ((err = iptun_enter(iptun)) == 0) {
switch (iptun->iptun_typeinfo->iti_ipvers) {
case IPV4_VERSION:
sin = (struct sockaddr_in *)&ss;
sin->sin_family = AF_INET;
bcopy(addrp, &sin->sin_addr, sizeof (in_addr_t));
break;
case IPV6_VERSION:
sin6 = (struct sockaddr_in6 *)&ss;
sin6->sin6_family = AF_INET6;
bcopy(addrp, &sin6->sin6_addr, sizeof (in6_addr_t));
break;
default:
ASSERT(0);
}
err = iptun_setladdr(iptun, &ss);
iptun_exit(iptun);
}
return (err);
}
static mblk_t *
iptun_m_tx(void *arg, mblk_t *mpchain)
{
mblk_t *mp, *nmp;
iptun_t *iptun = arg;
if (!IS_IPTUN_RUNNING(iptun)) {
iptun_drop_pkt(mpchain, &iptun->iptun_noxmtbuf);
return (NULL);
}
for (mp = mpchain; mp != NULL; mp = nmp) {
nmp = mp->b_next;
mp->b_next = NULL;
iptun_output(iptun, mp);
}
return (NULL);
}
static int
iptun_m_setprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
uint_t pr_valsize, const void *pr_val)
{
iptun_t *iptun = barg;
uint32_t value = *(uint32_t *)pr_val;
int err;
if ((err = iptun_enter(iptun)) != 0)
return (err);
switch (pr_num) {
case MAC_PROP_IPTUN_HOPLIMIT:
if (value < IPTUN_MIN_HOPLIMIT || value > IPTUN_MAX_HOPLIMIT) {
err = EINVAL;
break;
}
if (value != iptun->iptun_hoplimit) {
iptun->iptun_hoplimit = (uint8_t)value;
iptun_headergen(iptun, B_TRUE);
}
break;
case MAC_PROP_IPTUN_ENCAPLIMIT:
if (iptun->iptun_typeinfo->iti_type != IPTUN_TYPE_IPV6 ||
value > IPTUN_MAX_ENCAPLIMIT) {
err = EINVAL;
break;
}
if (value != iptun->iptun_encaplimit) {
iptun->iptun_encaplimit = (uint8_t)value;
iptun_headergen(iptun, B_TRUE);
}
break;
case MAC_PROP_MTU: {
uint32_t maxmtu = iptun_get_maxmtu(iptun, NULL, 0);
if (value < iptun->iptun_typeinfo->iti_minmtu ||
value > maxmtu) {
err = EINVAL;
break;
}
iptun->iptun_flags |= IPTUN_FIXED_MTU;
if (value != iptun->iptun_mtu) {
iptun->iptun_mtu = value;
iptun_task_dispatch(iptun, IPTUN_TASK_MTU_UPDATE);
}
break;
}
default:
err = EINVAL;
}
iptun_exit(iptun);
return (err);
}
static int
iptun_m_getprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
uint_t pr_valsize, void *pr_val)
{
iptun_t *iptun = barg;
int err;
if ((err = iptun_enter(iptun)) != 0)
return (err);
switch (pr_num) {
case MAC_PROP_IPTUN_HOPLIMIT:
ASSERT(pr_valsize >= sizeof (uint32_t));
*(uint32_t *)pr_val = iptun->iptun_hoplimit;
break;
case MAC_PROP_IPTUN_ENCAPLIMIT:
*(uint32_t *)pr_val = iptun->iptun_encaplimit;
break;
default:
err = ENOTSUP;
}
iptun_exit(iptun);
return (err);
}
static void
iptun_m_propinfo(void *barg, const char *pr_name, mac_prop_id_t pr_num,
mac_prop_info_handle_t prh)
{
iptun_t *iptun = barg;
switch (pr_num) {
case MAC_PROP_IPTUN_HOPLIMIT:
mac_prop_info_set_range_uint32(prh,
IPTUN_MIN_HOPLIMIT, IPTUN_MAX_HOPLIMIT);
mac_prop_info_set_default_uint32(prh, IPTUN_DEFAULT_HOPLIMIT);
break;
case MAC_PROP_IPTUN_ENCAPLIMIT:
if (iptun->iptun_typeinfo->iti_type != IPTUN_TYPE_IPV6)
break;
mac_prop_info_set_range_uint32(prh,
IPTUN_MIN_ENCAPLIMIT, IPTUN_MAX_ENCAPLIMIT);
mac_prop_info_set_default_uint32(prh, IPTUN_DEFAULT_ENCAPLIMIT);
break;
case MAC_PROP_MTU:
mac_prop_info_set_range_uint32(prh,
iptun->iptun_typeinfo->iti_minmtu,
iptun_get_maxmtu(iptun, NULL, 0));
break;
}
}
uint_t
iptun_count(void)
{
return (iptun_tunnelcount);
}
static int
iptun_enter(iptun_t *iptun)
{
mutex_enter(&iptun->iptun_lock);
while (iptun->iptun_flags & IPTUN_DELETE_PENDING)
cv_wait(&iptun->iptun_enter_cv, &iptun->iptun_lock);
if (iptun->iptun_flags & IPTUN_CONDEMNED) {
mutex_exit(&iptun->iptun_lock);
return (ENOENT);
}
return (0);
}
static void
iptun_exit(iptun_t *iptun)
{
mutex_exit(&iptun->iptun_lock);
}
static int
iptun_enter_by_linkid(datalink_id_t linkid, iptun_t **iptun)
{
int err;
mutex_enter(&iptun_hash_lock);
if (mod_hash_find(iptun_hash, IPTUN_HASH_KEY(linkid),
(mod_hash_val_t *)iptun) == 0)
err = iptun_enter(*iptun);
else
err = ENOENT;
if (err != 0)
*iptun = NULL;
mutex_exit(&iptun_hash_lock);
return (err);
}
static void
iptun_task_cb(void *arg)
{
iptun_task_data_t *itd = arg;
iptun_task_t task = itd->itd_task;
datalink_id_t linkid = itd->itd_linkid;
iptun_t *iptun;
uint32_t mtu;
iptun_addr_t addr;
link_state_t linkstate;
size_t header_size;
iptun_header_t header;
kmem_free(itd, sizeof (*itd));
if (iptun_enter_by_linkid(linkid, &iptun) != 0)
return;
iptun->iptun_flags |= IPTUN_UPCALL_PENDING;
switch (task) {
case IPTUN_TASK_MTU_UPDATE:
mtu = iptun->iptun_mtu;
break;
case IPTUN_TASK_LADDR_UPDATE:
addr = iptun->iptun_laddr;
break;
case IPTUN_TASK_RADDR_UPDATE:
addr = iptun->iptun_raddr;
break;
case IPTUN_TASK_LINK_UPDATE:
linkstate = IS_IPTUN_RUNNING(iptun) ?
LINK_STATE_UP : LINK_STATE_DOWN;
break;
case IPTUN_TASK_PDATA_UPDATE:
header_size = iptun->iptun_header_size;
header = iptun->iptun_header;
break;
default:
ASSERT(0);
}
iptun_exit(iptun);
switch (task) {
case IPTUN_TASK_MTU_UPDATE:
(void) mac_maxsdu_update(iptun->iptun_mh, mtu);
break;
case IPTUN_TASK_LADDR_UPDATE:
mac_unicst_update(iptun->iptun_mh, (uint8_t *)&addr.ia_addr);
break;
case IPTUN_TASK_RADDR_UPDATE:
mac_dst_update(iptun->iptun_mh, (uint8_t *)&addr.ia_addr);
break;
case IPTUN_TASK_LINK_UPDATE:
mac_link_update(iptun->iptun_mh, linkstate);
break;
case IPTUN_TASK_PDATA_UPDATE:
if (mac_pdata_update(iptun->iptun_mh,
header_size == 0 ? NULL : &header, header_size) != 0)
atomic_inc_64(&iptun->iptun_taskq_fail);
break;
}
mutex_enter(&iptun->iptun_lock);
iptun->iptun_flags &= ~IPTUN_UPCALL_PENDING;
cv_signal(&iptun->iptun_upcall_cv);
mutex_exit(&iptun->iptun_lock);
}
static void
iptun_task_dispatch(iptun_t *iptun, iptun_task_t iptun_task)
{
iptun_task_data_t *itd;
itd = kmem_alloc(sizeof (*itd), KM_NOSLEEP);
if (itd == NULL) {
atomic_inc_64(&iptun->iptun_taskq_fail);
return;
}
itd->itd_task = iptun_task;
itd->itd_linkid = iptun->iptun_linkid;
if (ddi_taskq_dispatch(iptun_taskq, iptun_task_cb, itd, DDI_NOSLEEP)) {
atomic_inc_64(&iptun->iptun_taskq_fail);
kmem_free(itd, sizeof (*itd));
}
}
static void
iptun_getaddr(iptun_addr_t *iptun_addr, struct sockaddr_storage *ss)
{
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
bzero(ss, sizeof (*ss));
switch (iptun_addr->ia_family) {
case AF_INET:
sin = (struct sockaddr_in *)ss;
sin->sin_addr.s_addr = iptun_addr->ia_addr.iau_addr4;
break;
case AF_INET6:
sin6 = (struct sockaddr_in6 *)ss;
sin6->sin6_addr = iptun_addr->ia_addr.iau_addr6;
break;
default:
ASSERT(0);
}
ss->ss_family = iptun_addr->ia_family;
}
static int
iptun_setaddr(iptun_type_t iptun_type, iptun_addr_t *iptun_addr,
const struct sockaddr_storage *ss)
{
if (!IPTUN_ADDR_MATCH(iptun_type, ss->ss_family))
return (EINVAL);
switch (ss->ss_family) {
case AF_INET: {
struct sockaddr_in *sin = (struct sockaddr_in *)ss;
if ((sin->sin_addr.s_addr == INADDR_ANY) ||
(sin->sin_addr.s_addr == INADDR_BROADCAST) ||
CLASSD(sin->sin_addr.s_addr)) {
return (EADDRNOTAVAIL);
}
iptun_addr->ia_addr.iau_addr4 = sin->sin_addr.s_addr;
break;
}
case AF_INET6: {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ss;
if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) ||
IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
return (EADDRNOTAVAIL);
}
iptun_addr->ia_addr.iau_addr6 = sin6->sin6_addr;
break;
}
default:
return (EAFNOSUPPORT);
}
iptun_addr->ia_family = ss->ss_family;
return (0);
}
static int
iptun_setladdr(iptun_t *iptun, const struct sockaddr_storage *laddr)
{
return (iptun_setaddr(iptun->iptun_typeinfo->iti_type,
&iptun->iptun_laddr, laddr));
}
static int
iptun_setraddr(iptun_t *iptun, const struct sockaddr_storage *raddr)
{
if (!(iptun->iptun_typeinfo->iti_hasraddr))
return (EINVAL);
return (iptun_setaddr(iptun->iptun_typeinfo->iti_type,
&iptun->iptun_raddr, raddr));
}
static boolean_t
iptun_canbind(iptun_t *iptun)
{
return ((iptun->iptun_flags & IPTUN_LADDR) &&
((iptun->iptun_flags & IPTUN_RADDR) ||
!(iptun->iptun_typeinfo->iti_hasraddr)));
}
static int
iptun_bind(iptun_t *iptun)
{
conn_t *connp = iptun->iptun_connp;
int error = 0;
ip_xmit_attr_t *ixa;
ip_xmit_attr_t *oldixa;
iulp_t uinfo;
ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
ixa = conn_get_ixa(connp, B_FALSE);
if (ixa == NULL)
return (ENOMEM);
ixa->ixa_flags |= IXAF_PMTU_DISCOVERY;
ASSERT(iptun_canbind(iptun));
mutex_enter(&connp->conn_lock);
connp->conn_ipversion = iptun->iptun_typeinfo->iti_ipvers;
switch (iptun->iptun_typeinfo->iti_type) {
case IPTUN_TYPE_IPV4:
IN6_IPADDR_TO_V4MAPPED(iptun->iptun_laddr4,
&connp->conn_laddr_v6);
IN6_IPADDR_TO_V4MAPPED(iptun->iptun_raddr4,
&connp->conn_faddr_v6);
ixa->ixa_flags |= IXAF_IS_IPV4;
if (ip_laddr_verify_v4(iptun->iptun_laddr4, IPCL_ZONEID(connp),
ipst, B_FALSE) != IPVL_UNICAST_UP) {
mutex_exit(&connp->conn_lock);
error = EADDRNOTAVAIL;
goto done;
}
break;
case IPTUN_TYPE_IPV6:
connp->conn_laddr_v6 = iptun->iptun_laddr6;
connp->conn_faddr_v6 = iptun->iptun_raddr6;
ixa->ixa_flags &= ~IXAF_IS_IPV4;
if (ip_laddr_verify_v6(&iptun->iptun_laddr6, IPCL_ZONEID(connp),
ipst, B_FALSE, 0) != IPVL_UNICAST_UP) {
mutex_exit(&connp->conn_lock);
error = EADDRNOTAVAIL;
goto done;
}
break;
case IPTUN_TYPE_6TO4:
IN6_IPADDR_TO_V4MAPPED(iptun->iptun_laddr4,
&connp->conn_laddr_v6);
IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &connp->conn_faddr_v6);
ixa->ixa_flags |= IXAF_IS_IPV4;
mutex_exit(&connp->conn_lock);
switch (ip_laddr_verify_v4(iptun->iptun_laddr4,
IPCL_ZONEID(connp), ipst, B_FALSE)) {
case IPVL_UNICAST_UP:
case IPVL_UNICAST_DOWN:
break;
default:
error = EADDRNOTAVAIL;
goto done;
}
goto insert;
}
ip_attr_newdst(ixa);
connp->conn_saddr_v6 = connp->conn_laddr_v6;
mutex_exit(&connp->conn_lock);
ixa->ixa_flags |= IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF;
error = ip_attr_connect(connp, ixa, &connp->conn_saddr_v6,
&connp->conn_faddr_v6, &connp->conn_faddr_v6, 0,
&connp->conn_saddr_v6, &uinfo, 0);
if (error != 0)
goto done;
ASSERT(IN6_ARE_ADDR_EQUAL(&connp->conn_laddr_v6,
&connp->conn_saddr_v6));
ixa->ixa_flags |= IXAF_VERIFY_PMTU;
ASSERT(uinfo.iulp_mtu != 0);
connp->conn_policy_cached = B_FALSE;
insert:
error = ipcl_conn_insert(connp);
if (error != 0)
goto done;
mutex_enter(&connp->conn_lock);
connp->conn_v6lastdst = connp->conn_faddr_v6;
iptun->iptun_flags |= IPTUN_BOUND;
oldixa = conn_replace_ixa(connp, ixa);
mutex_exit(&connp->conn_lock);
ixa_refrele(oldixa);
(void) iptun_update_mtu(iptun, ixa, 0);
if (IS_IPTUN_RUNNING(iptun))
iptun_task_dispatch(iptun, IPTUN_TASK_LINK_UPDATE);
done:
ixa_refrele(ixa);
return (error);
}
static void
iptun_unbind(iptun_t *iptun)
{
ASSERT(iptun->iptun_flags & IPTUN_BOUND);
ASSERT(mutex_owned(&iptun->iptun_lock) ||
(iptun->iptun_flags & IPTUN_CONDEMNED));
ip_unbind(iptun->iptun_connp);
iptun->iptun_flags &= ~IPTUN_BOUND;
if (!(iptun->iptun_flags & IPTUN_CONDEMNED))
iptun_task_dispatch(iptun, IPTUN_TASK_LINK_UPDATE);
}
static void
iptun_headergen(iptun_t *iptun, boolean_t update_mac)
{
switch (iptun->iptun_typeinfo->iti_ipvers) {
case IPV4_VERSION:
if (iptun->iptun_hoplimit == IPTUN_DEFAULT_HOPLIMIT) {
iptun->iptun_header_size = 0;
break;
}
iptun->iptun_header_size = sizeof (ipha_t);
iptun->iptun_header4.ipha_version_and_hdr_length =
IP_SIMPLE_HDR_VERSION;
iptun->iptun_header4.ipha_fragment_offset_and_flags =
htons(IPH_DF);
iptun->iptun_header4.ipha_ttl = iptun->iptun_hoplimit;
break;
case IPV6_VERSION: {
ip6_t *ip6hp = &iptun->iptun_header6.it6h_ip6h;
if (iptun->iptun_hoplimit == IPTUN_DEFAULT_HOPLIMIT &&
iptun->iptun_encaplimit == 0) {
iptun->iptun_header_size = 0;
break;
}
(void) memset(ip6hp, 0, sizeof (*ip6hp));
if (iptun->iptun_encaplimit == 0) {
iptun->iptun_header_size = sizeof (ip6_t);
ip6hp->ip6_nxt = IPPROTO_NONE;
} else {
iptun_encaplim_t *iel;
iptun->iptun_header_size = sizeof (iptun_ipv6hdrs_t);
ip6hp->ip6_plen = sizeof (*iel);
ip6hp->ip6_nxt = IPPROTO_DSTOPTS;
iel = &iptun->iptun_header6.it6h_encaplim;
*iel = iptun_encaplim_init;
iel->iel_telopt.ip6ot_encap_limit =
iptun->iptun_encaplimit;
}
ip6hp->ip6_hlim = iptun->iptun_hoplimit;
break;
}
}
if (update_mac)
iptun_task_dispatch(iptun, IPTUN_TASK_PDATA_UPDATE);
}
static boolean_t
iptun_insert_simple_policies(ipsec_policy_head_t *ph, ipsec_act_t *actp,
uint_t n, netstack_t *ns)
{
int f = IPSEC_AF_V4;
if (!ipsec_polhead_insert(ph, actp, n, f, IPSEC_TYPE_INBOUND, ns) ||
!ipsec_polhead_insert(ph, actp, n, f, IPSEC_TYPE_OUTBOUND, ns))
return (B_FALSE);
f = IPSEC_AF_V6;
return (ipsec_polhead_insert(ph, actp, n, f, IPSEC_TYPE_INBOUND, ns) &&
ipsec_polhead_insert(ph, actp, n, f, IPSEC_TYPE_OUTBOUND, ns));
}
static int
iptun_set_sec_simple(iptun_t *iptun, const ipsec_req_t *ipsr)
{
int rc = 0;
uint_t nact;
ipsec_act_t *actp = NULL;
boolean_t clear_all, old_policy = B_FALSE;
ipsec_tun_pol_t *itp;
char name[MAXLINKNAMELEN];
uint64_t gen;
netstack_t *ns = iptun->iptun_ns;
if (ipsr->ipsr_self_encap_req != 0)
return (EINVAL);
clear_all = ((ipsr->ipsr_ah_req & IPTUN_IPSEC_REQ_MASK) == 0 &&
(ipsr->ipsr_esp_req & IPTUN_IPSEC_REQ_MASK) == 0);
ASSERT(mutex_owned(&iptun->iptun_lock));
itp = iptun->iptun_itp;
if (itp == NULL) {
if (clear_all)
goto bail;
if ((rc = dls_mgmt_get_linkinfo(iptun->iptun_linkid, name, NULL,
NULL, NULL)) != 0)
goto bail;
ASSERT(name[0] != '\0');
if ((itp = create_tunnel_policy(name, &rc, &gen, ns)) == NULL)
goto bail;
iptun->iptun_itp = itp;
}
ipsec_actvec_from_req(ipsr, &actp, &nact, ns);
if (actp == NULL) {
rc = ENOMEM;
goto bail;
}
mutex_enter(&itp->itp_lock);
if (itp->itp_flags & ITPF_P_TUNNEL) {
rc = EBUSY;
goto mutex_bail;
}
old_policy = ((itp->itp_flags & ITPF_P_ACTIVE) != 0);
if (old_policy) {
ITPF_CLONE(itp->itp_flags);
rc = ipsec_copy_polhead(itp->itp_policy, itp->itp_inactive, ns);
if (rc != 0) {
itp->itp_flags &= ~ITPF_IFLAGS;
goto mutex_bail;
}
rw_enter(&itp->itp_policy->iph_lock, RW_WRITER);
ipsec_polhead_flush(itp->itp_policy, ns);
} else {
rw_enter(&itp->itp_policy->iph_lock, RW_WRITER);
}
if (clear_all) {
ASSERT(avl_numnodes(&itp->itp_policy->iph_rulebyid) == 0);
itp->itp_flags &= ~ITPF_PFLAGS;
rw_exit(&itp->itp_policy->iph_lock);
old_policy = B_FALSE;
goto recover_bail;
}
if (iptun_insert_simple_policies(itp->itp_policy, actp, nact, ns)) {
rw_exit(&itp->itp_policy->iph_lock);
itp->itp_flags = ITPF_P_ACTIVE;
(void) iptun_update_mtu(iptun, NULL, 0);
old_policy = B_FALSE;
} else {
rw_exit(&itp->itp_policy->iph_lock);
rc = ENOMEM;
}
recover_bail:
if (old_policy) {
ipsec_swap_policy(itp->itp_policy, itp->itp_inactive, ns);
ITPF_SWAP(itp->itp_flags);
}
itp->itp_flags &= ~ITPF_IFLAGS;
rw_enter(&itp->itp_inactive->iph_lock, RW_WRITER);
ipsec_polhead_flush(itp->itp_inactive, ns);
rw_exit(&itp->itp_inactive->iph_lock);
mutex_bail:
mutex_exit(&itp->itp_lock);
bail:
if (actp != NULL)
ipsec_actvec_free(actp, nact);
return (rc);
}
static iptun_typeinfo_t *
iptun_gettypeinfo(iptun_type_t type)
{
int i;
for (i = 0; iptun_type_table[i].iti_type != IPTUN_TYPE_UNKNOWN; i++) {
if (iptun_type_table[i].iti_type == type)
break;
}
return (&iptun_type_table[i]);
}
static int
iptun_setparams(iptun_t *iptun, const iptun_kparams_t *ik)
{
int err = 0;
netstack_t *ns = iptun->iptun_ns;
iptun_addr_t orig_laddr, orig_raddr;
uint_t orig_flags = iptun->iptun_flags;
if (ik->iptun_kparam_flags & IPTUN_KPARAM_LADDR) {
if (orig_flags & IPTUN_LADDR)
orig_laddr = iptun->iptun_laddr;
if ((err = iptun_setladdr(iptun, &ik->iptun_kparam_laddr)) != 0)
return (err);
iptun->iptun_flags |= IPTUN_LADDR;
}
if (ik->iptun_kparam_flags & IPTUN_KPARAM_RADDR) {
if (orig_flags & IPTUN_RADDR)
orig_raddr = iptun->iptun_raddr;
if ((err = iptun_setraddr(iptun, &ik->iptun_kparam_raddr)) != 0)
goto done;
iptun->iptun_flags |= IPTUN_RADDR;
}
if (ik->iptun_kparam_flags & IPTUN_KPARAM_SECINFO) {
if (iptun->iptun_typeinfo->iti_type == IPTUN_TYPE_6TO4) {
err = EINVAL;
goto done;
}
if (!ipsec_loaded(ns->netstack_ipsec)) {
if (ipsec_failed(ns->netstack_ipsec)) {
err = EPROTONOSUPPORT;
goto done;
}
ipsec_loader_loadnow(ns->netstack_ipsec);
err = EAGAIN;
goto done;
}
err = iptun_set_sec_simple(iptun, &ik->iptun_kparam_secinfo);
if (err == 0) {
iptun->iptun_flags |= IPTUN_SIMPLE_POLICY;
iptun->iptun_simple_policy = ik->iptun_kparam_secinfo;
}
}
done:
if (err != 0) {
if (ik->iptun_kparam_flags & IPTUN_KPARAM_LADDR &&
(orig_flags & IPTUN_LADDR))
iptun->iptun_laddr = orig_laddr;
if ((ik->iptun_kparam_flags & IPTUN_KPARAM_RADDR) &&
(orig_flags & IPTUN_RADDR))
iptun->iptun_raddr = orig_raddr;
iptun->iptun_flags = orig_flags;
}
return (err);
}
static int
iptun_register(iptun_t *iptun)
{
mac_register_t *mac;
int err;
ASSERT(!(iptun->iptun_flags & IPTUN_MAC_REGISTERED));
if ((mac = mac_alloc(MAC_VERSION)) == NULL)
return (EINVAL);
mac->m_type_ident = iptun->iptun_typeinfo->iti_ident;
mac->m_driver = iptun;
mac->m_dip = iptun_dip;
mac->m_instance = (uint_t)-1;
mac->m_src_addr = (uint8_t *)&iptun->iptun_laddr.ia_addr;
mac->m_dst_addr = iptun->iptun_typeinfo->iti_hasraddr ?
(uint8_t *)&iptun->iptun_raddr.ia_addr : NULL;
mac->m_callbacks = &iptun_m_callbacks;
mac->m_min_sdu = iptun->iptun_typeinfo->iti_minmtu;
mac->m_max_sdu = iptun->iptun_mtu;
if (iptun->iptun_header_size != 0) {
mac->m_pdata = &iptun->iptun_header;
mac->m_pdata_size = iptun->iptun_header_size;
}
if ((err = mac_register(mac, &iptun->iptun_mh)) == 0)
iptun->iptun_flags |= IPTUN_MAC_REGISTERED;
mac_free(mac);
return (err);
}
static int
iptun_unregister(iptun_t *iptun)
{
int err;
ASSERT(iptun->iptun_flags & IPTUN_MAC_REGISTERED);
if ((err = mac_unregister(iptun->iptun_mh)) == 0)
iptun->iptun_flags &= ~IPTUN_MAC_REGISTERED;
return (err);
}
static conn_t *
iptun_conn_create(iptun_t *iptun, netstack_t *ns, cred_t *credp)
{
conn_t *connp;
if ((connp = ipcl_conn_create(IPCL_IPCCONN, KM_NOSLEEP, ns)) == NULL)
return (NULL);
connp->conn_flags |= IPCL_IPTUN;
connp->conn_iptun = iptun;
connp->conn_recv = iptun_input;
connp->conn_recvicmp = iptun_input_icmp;
connp->conn_verifyicmp = iptun_verifyicmp;
connp->conn_ixa->ixa_notify = iptun_notify;
connp->conn_ixa->ixa_notify_cookie = iptun;
connp->conn_zoneid = (ns->netstack_stackid == GLOBAL_NETSTACKID) ?
crgetzoneid(credp) : GLOBAL_ZONEID;
connp->conn_cred = credp;
crhold(connp->conn_cred);
connp->conn_cpid = NOPID;
connp->conn_ixa->ixa_zoneid = connp->conn_zoneid;
ASSERT(connp->conn_ref == 1);
ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
connp->conn_ixa->ixa_cred = connp->conn_cred;
connp->conn_ixa->ixa_cpid = connp->conn_cpid;
if (is_system_labeled())
connp->conn_ixa->ixa_tsl = crgetlabel(connp->conn_cred);
connp->conn_ixa->ixa_flags |= IXAF_VERIFY_SOURCE;
switch (iptun->iptun_typeinfo->iti_ipvers) {
case IPV4_VERSION:
connp->conn_family = AF_INET6;
break;
case IPV6_VERSION:
connp->conn_family = AF_INET;
break;
}
mutex_enter(&connp->conn_lock);
connp->conn_state_flags &= ~CONN_INCIPIENT;
mutex_exit(&connp->conn_lock);
return (connp);
}
static void
iptun_conn_destroy(conn_t *connp)
{
ip_quiesce_conn(connp);
connp->conn_iptun = NULL;
ASSERT(connp->conn_ref == 1);
CONN_DEC_REF(connp);
}
static iptun_t *
iptun_alloc(void)
{
iptun_t *iptun;
if ((iptun = kmem_cache_alloc(iptun_cache, KM_NOSLEEP)) != NULL) {
bzero(iptun, sizeof (*iptun));
atomic_inc_32(&iptun_tunnelcount);
}
return (iptun);
}
static void
iptun_free(iptun_t *iptun)
{
ASSERT(iptun->iptun_flags & IPTUN_CONDEMNED);
if (iptun->iptun_flags & IPTUN_HASH_INSERTED) {
iptun_stack_t *iptuns = iptun->iptun_iptuns;
mutex_enter(&iptun_hash_lock);
VERIFY(mod_hash_remove(iptun_hash,
IPTUN_HASH_KEY(iptun->iptun_linkid),
(mod_hash_val_t *)&iptun) == 0);
mutex_exit(&iptun_hash_lock);
iptun->iptun_flags &= ~IPTUN_HASH_INSERTED;
mutex_enter(&iptuns->iptuns_lock);
list_remove(&iptuns->iptuns_iptunlist, iptun);
mutex_exit(&iptuns->iptuns_lock);
}
if (iptun->iptun_flags & IPTUN_BOUND)
iptun_unbind(iptun);
if (iptun->iptun_flags & IPTUN_MAC_REGISTERED)
VERIFY(iptun_unregister(iptun) == 0);
if (iptun->iptun_itp != NULL) {
itp_unlink(iptun->iptun_itp, iptun->iptun_ns);
ITP_REFRELE(iptun->iptun_itp, iptun->iptun_ns);
iptun->iptun_itp = NULL;
iptun->iptun_flags &= ~IPTUN_SIMPLE_POLICY;
}
if (iptun->iptun_connp != NULL) {
iptun_conn_destroy(iptun->iptun_connp);
iptun->iptun_connp = NULL;
}
netstack_rele(iptun->iptun_ns);
kmem_cache_free(iptun_cache, iptun);
atomic_dec_32(&iptun_tunnelcount);
}
int
iptun_create(iptun_kparams_t *ik, cred_t *credp)
{
iptun_t *iptun = NULL;
int err = 0, mherr;
char linkname[MAXLINKNAMELEN];
ipsec_tun_pol_t *itp;
netstack_t *ns = NULL;
iptun_stack_t *iptuns;
datalink_id_t tmpid;
zoneid_t zoneid = crgetzoneid(credp);
boolean_t link_created = B_FALSE;
if (!(ik->iptun_kparam_flags & IPTUN_KPARAM_TYPE))
return (EINVAL);
if (zone_check_datalink(&zoneid, ik->iptun_kparam_linkid) != 0) {
if (zoneid != GLOBAL_ZONEID)
return (EINVAL);
} else if (zoneid == GLOBAL_ZONEID) {
return (EINVAL);
}
if (iptun_enter_by_linkid(ik->iptun_kparam_linkid, &iptun) == 0) {
iptun_exit(iptun);
iptun = NULL;
err = EEXIST;
goto done;
}
ns = netstack_find_by_cred(credp);
iptuns = ns->netstack_iptun;
if ((iptun = iptun_alloc()) == NULL) {
err = ENOMEM;
goto done;
}
iptun->iptun_linkid = ik->iptun_kparam_linkid;
iptun->iptun_zoneid = zoneid;
iptun->iptun_ns = ns;
iptun->iptun_typeinfo = iptun_gettypeinfo(ik->iptun_kparam_type);
if (iptun->iptun_typeinfo->iti_type == IPTUN_TYPE_UNKNOWN) {
err = EINVAL;
goto done;
}
if (ik->iptun_kparam_flags & IPTUN_KPARAM_IMPLICIT)
iptun->iptun_flags |= IPTUN_IMPLICIT;
if ((err = iptun_setparams(iptun, ik)) != 0)
goto done;
iptun->iptun_hoplimit = IPTUN_DEFAULT_HOPLIMIT;
if (iptun->iptun_typeinfo->iti_type == IPTUN_TYPE_IPV6)
iptun->iptun_encaplimit = IPTUN_DEFAULT_ENCAPLIMIT;
iptun_headergen(iptun, B_FALSE);
iptun->iptun_connp = iptun_conn_create(iptun, ns, credp);
if (iptun->iptun_connp == NULL) {
err = ENOMEM;
goto done;
}
iptun->iptun_mtu = iptun->iptun_typeinfo->iti_maxmtu;
iptun->iptun_dpmtu = iptun->iptun_mtu;
if ((err = dls_mgmt_get_linkinfo(iptun->iptun_linkid, linkname, NULL,
NULL, NULL)) != 0)
goto done;
if ((itp = get_tunnel_policy(linkname, ns)) != NULL)
iptun->iptun_itp = itp;
if (iptun_canbind(iptun) && ((err = iptun_bind(iptun)) != 0))
goto done;
if ((err = iptun_register(iptun)) != 0)
goto done;
err = dls_devnet_create(iptun->iptun_mh, iptun->iptun_linkid,
iptun->iptun_zoneid);
if (err != 0)
goto done;
link_created = B_TRUE;
if ((mherr = mod_hash_insert(iptun_hash,
IPTUN_HASH_KEY(iptun->iptun_linkid), (mod_hash_val_t)iptun)) == 0) {
mutex_enter(&iptuns->iptuns_lock);
list_insert_head(&iptuns->iptuns_iptunlist, iptun);
mutex_exit(&iptuns->iptuns_lock);
iptun->iptun_flags |= IPTUN_HASH_INSERTED;
} else if (mherr == MH_ERR_NOMEM) {
err = ENOMEM;
} else if (mherr == MH_ERR_DUPLICATE) {
err = EEXIST;
} else {
err = EINVAL;
}
done:
if (iptun == NULL && ns != NULL)
netstack_rele(ns);
if (err != 0 && iptun != NULL) {
if (link_created) {
(void) dls_devnet_destroy(iptun->iptun_mh, &tmpid,
B_TRUE);
}
iptun->iptun_flags |= IPTUN_CONDEMNED;
iptun_free(iptun);
}
return (err);
}
int
iptun_delete(datalink_id_t linkid, cred_t *credp)
{
int err;
iptun_t *iptun = NULL;
if ((err = iptun_enter_by_linkid(linkid, &iptun)) != 0)
return (err);
if (iptun->iptun_zoneid != crgetzoneid(credp)) {
iptun_exit(iptun);
return (EACCES);
}
iptun->iptun_flags |= IPTUN_DELETE_PENDING;
while (iptun->iptun_flags & IPTUN_UPCALL_PENDING)
cv_wait(&iptun->iptun_upcall_cv, &iptun->iptun_lock);
iptun_exit(iptun);
if ((err = dls_devnet_destroy(iptun->iptun_mh, &linkid, B_TRUE)) == 0) {
if ((err = mac_disable(iptun->iptun_mh)) != 0) {
(void) dls_devnet_create(iptun->iptun_mh, linkid,
iptun->iptun_zoneid);
}
}
mutex_enter(&iptun->iptun_lock);
iptun->iptun_flags &= ~IPTUN_DELETE_PENDING;
if (err == 0)
iptun->iptun_flags |= IPTUN_CONDEMNED;
cv_broadcast(&iptun->iptun_enter_cv);
mutex_exit(&iptun->iptun_lock);
if (err == 0)
iptun_free(iptun);
return (err);
}
int
iptun_modify(const iptun_kparams_t *ik, cred_t *credp)
{
iptun_t *iptun;
boolean_t laddr_change = B_FALSE, raddr_change = B_FALSE;
int err;
if ((err = iptun_enter_by_linkid(ik->iptun_kparam_linkid, &iptun)) != 0)
return (err);
if (iptun->iptun_zoneid != crgetzoneid(credp)) {
err = EACCES;
goto done;
}
if (ik->iptun_kparam_flags & IPTUN_KPARAM_TYPE) {
err = EINVAL;
goto done;
}
if ((err = iptun_setparams(iptun, ik)) != 0)
goto done;
iptun_headergen(iptun, B_FALSE);
laddr_change = (ik->iptun_kparam_flags & IPTUN_KPARAM_LADDR);
raddr_change = (ik->iptun_kparam_flags & IPTUN_KPARAM_RADDR);
if (laddr_change || raddr_change) {
if (iptun->iptun_flags & IPTUN_BOUND)
iptun_unbind(iptun);
if (iptun_canbind(iptun) && (err = iptun_bind(iptun)) != 0) {
if (laddr_change)
iptun->iptun_flags &= ~IPTUN_LADDR;
if (raddr_change)
iptun->iptun_flags &= ~IPTUN_RADDR;
goto done;
}
}
if (laddr_change)
iptun_task_dispatch(iptun, IPTUN_TASK_LADDR_UPDATE);
if (raddr_change)
iptun_task_dispatch(iptun, IPTUN_TASK_RADDR_UPDATE);
done:
iptun_exit(iptun);
return (err);
}
int
iptun_info(iptun_kparams_t *ik, cred_t *credp)
{
iptun_t *iptun;
int err;
if (!dls_devnet_islinkvisible(ik->iptun_kparam_linkid,
crgetzoneid(credp)))
return (ENOENT);
if ((err = iptun_enter_by_linkid(ik->iptun_kparam_linkid, &iptun)) != 0)
return (err);
bzero(ik, sizeof (iptun_kparams_t));
ik->iptun_kparam_linkid = iptun->iptun_linkid;
ik->iptun_kparam_type = iptun->iptun_typeinfo->iti_type;
ik->iptun_kparam_flags |= IPTUN_KPARAM_TYPE;
if (iptun->iptun_flags & IPTUN_LADDR) {
iptun_getaddr(&iptun->iptun_laddr, &ik->iptun_kparam_laddr);
ik->iptun_kparam_flags |= IPTUN_KPARAM_LADDR;
}
if (iptun->iptun_flags & IPTUN_RADDR) {
iptun_getaddr(&iptun->iptun_raddr, &ik->iptun_kparam_raddr);
ik->iptun_kparam_flags |= IPTUN_KPARAM_RADDR;
}
if (iptun->iptun_flags & IPTUN_IMPLICIT)
ik->iptun_kparam_flags |= IPTUN_KPARAM_IMPLICIT;
if (iptun->iptun_itp != NULL) {
mutex_enter(&iptun->iptun_itp->itp_lock);
if (iptun->iptun_itp->itp_flags & ITPF_P_ACTIVE) {
ik->iptun_kparam_flags |= IPTUN_KPARAM_IPSECPOL;
if (iptun->iptun_flags & IPTUN_SIMPLE_POLICY) {
ik->iptun_kparam_flags |= IPTUN_KPARAM_SECINFO;
ik->iptun_kparam_secinfo =
iptun->iptun_simple_policy;
}
}
mutex_exit(&iptun->iptun_itp->itp_lock);
}
iptun_exit(iptun);
return (err);
}
int
iptun_set_6to4relay(netstack_t *ns, ipaddr_t relay_addr)
{
if (relay_addr == INADDR_BROADCAST || CLASSD(relay_addr))
return (EADDRNOTAVAIL);
ns->netstack_iptun->iptuns_relay_rtr_addr = relay_addr;
return (0);
}
void
iptun_get_6to4relay(netstack_t *ns, ipaddr_t *relay_addr)
{
*relay_addr = ns->netstack_iptun->iptuns_relay_rtr_addr;
}
void
iptun_set_policy(datalink_id_t linkid, ipsec_tun_pol_t *itp)
{
iptun_t *iptun;
if (iptun_enter_by_linkid(linkid, &iptun) != 0)
return;
if (iptun->iptun_itp != itp) {
ASSERT(iptun->iptun_itp == NULL);
ITP_REFHOLD(itp);
iptun->iptun_itp = itp;
}
(void) iptun_update_mtu(iptun, NULL, 0);
iptun_exit(iptun);
}
static uint32_t
iptun_get_dst_pmtu(iptun_t *iptun, ip_xmit_attr_t *ixa)
{
uint32_t pmtu = 0;
conn_t *connp = iptun->iptun_connp;
boolean_t need_rele = B_FALSE;
if (!(iptun->iptun_flags & IPTUN_RADDR))
return (0);
if (ixa == NULL) {
ixa = conn_get_ixa(connp, B_FALSE);
if (ixa == NULL)
return (0);
need_rele = B_TRUE;
}
if (ixa->ixa_ire != NULL) {
pmtu = ip_get_pmtu(ixa);
if (ixa->ixa_flags & IXAF_PMTU_TOO_SMALL) {
ixa->ixa_flags &= ~IXAF_DONTFRAG;
} else if (iptun->iptun_typeinfo->iti_type != IPTUN_TYPE_6TO4) {
ixa->ixa_flags |= IXAF_DONTFRAG;
} else {
ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
}
}
if (need_rele)
ixa_refrele(ixa);
return (pmtu);
}
static void
iptun_update_dst_pmtu(iptun_t *iptun, ip_xmit_attr_t *ixa)
{
uint32_t pmtu;
conn_t *connp = iptun->iptun_connp;
boolean_t need_rele = B_FALSE;
if (!(iptun->iptun_flags & IPTUN_RADDR))
return;
if (ixa == NULL) {
ixa = conn_get_ixa(connp, B_FALSE);
if (ixa == NULL)
return;
need_rele = B_TRUE;
}
if (ixa->ixa_ire != NULL) {
pmtu = ip_get_pmtu(ixa);
ixa->ixa_fragsize = ixa->ixa_pmtu = pmtu;
if (ixa->ixa_flags & IXAF_PMTU_TOO_SMALL) {
ixa->ixa_flags &= ~IXAF_DONTFRAG;
} else if (iptun->iptun_typeinfo->iti_type != IPTUN_TYPE_6TO4) {
ixa->ixa_flags |= IXAF_DONTFRAG;
} else {
ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
}
}
if (need_rele)
ixa_refrele(ixa);
}
static boolean_t
iptun_verifyicmp(conn_t *connp, void *arg2, icmph_t *icmph, icmp6_t *icmp6,
ip_recv_attr_t *ira)
{
return (B_TRUE);
}
static void
iptun_notify(void *arg, ip_xmit_attr_t *ixa, ixa_notify_type_t ntype,
ixa_notify_arg_t narg)
{
iptun_t *iptun = (iptun_t *)arg;
switch (ntype) {
case IXAN_PMTU:
(void) iptun_update_mtu(iptun, ixa, narg);
break;
}
}
static uint32_t
iptun_max_policy_overhead(ipsec_policy_t *pol, uint32_t old_ovhd)
{
uint32_t new_ovhd = old_ovhd;
while (pol != NULL) {
new_ovhd = max(new_ovhd,
ipsec_act_ovhd(&pol->ipsp_act->ipa_act));
pol = pol->ipsp_hash.hash_next;
}
return (new_ovhd);
}
static uint32_t
iptun_get_ipsec_overhead(iptun_t *iptun)
{
ipsec_policy_root_t *ipr;
ipsec_policy_head_t *iph;
ipsec_policy_t *pol;
ipsec_selector_t sel;
int i;
uint32_t ipsec_ovhd = 0;
ipsec_tun_pol_t *itp = iptun->iptun_itp;
netstack_t *ns = iptun->iptun_ns;
if (itp == NULL || !(itp->itp_flags & ITPF_P_ACTIVE)) {
if ((iptun->iptun_flags & (IPTUN_LADDR|IPTUN_RADDR)) !=
(IPTUN_LADDR|IPTUN_RADDR))
return (0);
iph = ipsec_system_policy(ns);
bzero(&sel, sizeof (sel));
sel.ips_isv4 =
(iptun->iptun_typeinfo->iti_ipvers == IPV4_VERSION);
switch (iptun->iptun_typeinfo->iti_ipvers) {
case IPV4_VERSION:
sel.ips_local_addr_v4 = iptun->iptun_laddr4;
sel.ips_remote_addr_v4 = iptun->iptun_raddr4;
break;
case IPV6_VERSION:
sel.ips_local_addr_v6 = iptun->iptun_laddr6;
sel.ips_remote_addr_v6 = iptun->iptun_raddr6;
break;
}
sel.ips_protocol = IPPROTO_ENCAP;
pol = ipsec_find_policy_head(NULL, iph, IPSEC_TYPE_OUTBOUND,
&sel);
if (pol != NULL) {
ipsec_ovhd = ipsec_act_ovhd(&pol->ipsp_act->ipa_act);
IPPOL_REFRELE(pol);
}
sel.ips_protocol = IPPROTO_IPV6;
pol = ipsec_find_policy_head(NULL, iph, IPSEC_TYPE_OUTBOUND,
&sel);
if (pol != NULL) {
ipsec_ovhd = max(ipsec_ovhd,
ipsec_act_ovhd(&pol->ipsp_act->ipa_act));
IPPOL_REFRELE(pol);
}
IPPH_REFRELE(iph, ns);
} else {
iph = itp->itp_policy;
rw_enter(&iph->iph_lock, RW_READER);
ipr = &(iph->iph_root[IPSEC_TYPE_OUTBOUND]);
ipsec_ovhd = iptun_max_policy_overhead(
ipr->ipr_nonhash[IPSEC_AF_V4], 0);
ipsec_ovhd = iptun_max_policy_overhead(
ipr->ipr_nonhash[IPSEC_AF_V6], ipsec_ovhd);
for (i = 0; i < ipr->ipr_nchains; i++) {
ipsec_ovhd = iptun_max_policy_overhead(
ipr->ipr_hash[i].hash_head, ipsec_ovhd);
}
rw_exit(&iph->iph_lock);
}
return (ipsec_ovhd);
}
static uint32_t
iptun_get_maxmtu(iptun_t *iptun, ip_xmit_attr_t *ixa, uint32_t new_pmtu)
{
size_t header_size, ipsec_overhead;
uint32_t maxmtu, pmtu;
if (new_pmtu != 0) {
if (iptun->iptun_flags & IPTUN_RADDR)
iptun->iptun_dpmtu = new_pmtu;
pmtu = new_pmtu;
} else if (iptun->iptun_flags & IPTUN_RADDR) {
if ((pmtu = iptun_get_dst_pmtu(iptun, ixa)) == 0) {
pmtu = iptun->iptun_dpmtu;
} else {
iptun->iptun_dpmtu = pmtu;
}
} else {
pmtu = iptun->iptun_typeinfo->iti_maxmtu;
}
if (iptun->iptun_header_size != 0) {
header_size = iptun->iptun_header_size;
} else {
switch (iptun->iptun_typeinfo->iti_ipvers) {
case IPV4_VERSION:
header_size = sizeof (ipha_t);
if (is_system_labeled())
header_size += IP_MAX_OPT_LENGTH;
break;
case IPV6_VERSION:
header_size = sizeof (iptun_ipv6hdrs_t);
break;
}
}
ipsec_overhead = iptun_get_ipsec_overhead(iptun);
maxmtu = pmtu - (header_size + ipsec_overhead);
return (max(maxmtu, iptun->iptun_typeinfo->iti_minmtu));
}
static uint32_t
iptun_update_mtu(iptun_t *iptun, ip_xmit_attr_t *ixa, uint32_t new_pmtu)
{
uint32_t newmtu;
iptun_update_dst_pmtu(iptun, ixa);
if (iptun->iptun_flags & IPTUN_FIXED_MTU)
return (iptun->iptun_mtu);
newmtu = iptun_get_maxmtu(iptun, ixa, new_pmtu);
if ((iptun->iptun_flags & IPTUN_RADDR) && newmtu != iptun->iptun_mtu) {
iptun->iptun_mtu = newmtu;
if (iptun->iptun_flags & IPTUN_MAC_REGISTERED)
iptun_task_dispatch(iptun, IPTUN_TASK_MTU_UPDATE);
}
return (newmtu);
}
static void
iptun_drop_pkt(mblk_t *mp, uint64_t *stat)
{
mblk_t *pktmp;
for (pktmp = mp; pktmp != NULL; pktmp = mp) {
mp = mp->b_next;
pktmp->b_next = NULL;
if (stat != NULL)
atomic_inc_64(stat);
freemsg(pktmp);
}
}
static mblk_t *
iptun_build_icmperr(size_t hdrs_size, mblk_t *orig_pkt)
{
mblk_t *icmperr_mp;
if ((icmperr_mp = allocb(hdrs_size, BPRI_MED)) != NULL) {
icmperr_mp->b_wptr += hdrs_size;
icmperr_mp->b_cont = orig_pkt;
}
return (icmperr_mp);
}
static void
iptun_sendicmp_v4(iptun_t *iptun, icmph_t *icmp, ipha_t *orig_ipha, mblk_t *mp,
ts_label_t *tsl)
{
size_t orig_pktsize, hdrs_size;
mblk_t *icmperr_mp;
ipha_t *new_ipha;
icmph_t *new_icmp;
ip_xmit_attr_t ixas;
conn_t *connp = iptun->iptun_connp;
orig_pktsize = msgdsize(mp);
hdrs_size = sizeof (ipha_t) + sizeof (icmph_t);
if ((icmperr_mp = iptun_build_icmperr(hdrs_size, mp)) == NULL) {
iptun_drop_pkt(mp, &iptun->iptun_noxmtbuf);
return;
}
new_ipha = (ipha_t *)icmperr_mp->b_rptr;
new_icmp = (icmph_t *)(new_ipha + 1);
new_ipha->ipha_version_and_hdr_length = IP_SIMPLE_HDR_VERSION;
new_ipha->ipha_type_of_service = 0;
new_ipha->ipha_ident = 0;
new_ipha->ipha_fragment_offset_and_flags = 0;
new_ipha->ipha_ttl = orig_ipha->ipha_ttl;
new_ipha->ipha_protocol = IPPROTO_ICMP;
new_ipha->ipha_src = orig_ipha->ipha_dst;
new_ipha->ipha_dst = orig_ipha->ipha_src;
new_ipha->ipha_hdr_checksum = 0;
new_ipha->ipha_length = htons(hdrs_size + orig_pktsize);
*new_icmp = *icmp;
new_icmp->icmph_checksum = 0;
new_icmp->icmph_checksum = IP_CSUM(icmperr_mp, sizeof (ipha_t), 0);
bzero(&ixas, sizeof (ixas));
ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
if (new_ipha->ipha_src == INADDR_ANY) {
ixas.ixa_flags &= ~IXAF_VERIFY_SOURCE;
ixas.ixa_flags |= IXAF_SET_SOURCE;
}
ixas.ixa_zoneid = IPCL_ZONEID(connp);
ixas.ixa_ipst = connp->conn_netstack->netstack_ip;
ixas.ixa_cred = connp->conn_cred;
ixas.ixa_cpid = NOPID;
if (is_system_labeled())
ixas.ixa_tsl = tsl;
ixas.ixa_ifindex = 0;
ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
(void) ip_output_simple(icmperr_mp, &ixas);
ixa_cleanup(&ixas);
}
static void
iptun_sendicmp_v6(iptun_t *iptun, icmp6_t *icmp6, ip6_t *orig_ip6h, mblk_t *mp,
ts_label_t *tsl)
{
size_t orig_pktsize, hdrs_size;
mblk_t *icmp6err_mp;
ip6_t *new_ip6h;
icmp6_t *new_icmp6;
ip_xmit_attr_t ixas;
conn_t *connp = iptun->iptun_connp;
orig_pktsize = msgdsize(mp);
hdrs_size = sizeof (ip6_t) + sizeof (icmp6_t);
if ((icmp6err_mp = iptun_build_icmperr(hdrs_size, mp)) == NULL) {
iptun_drop_pkt(mp, &iptun->iptun_noxmtbuf);
return;
}
new_ip6h = (ip6_t *)icmp6err_mp->b_rptr;
new_icmp6 = (icmp6_t *)(new_ip6h + 1);
new_ip6h->ip6_vcf = orig_ip6h->ip6_vcf;
new_ip6h->ip6_plen = htons(sizeof (icmp6_t) + orig_pktsize);
new_ip6h->ip6_hops = orig_ip6h->ip6_hops;
new_ip6h->ip6_nxt = IPPROTO_ICMPV6;
new_ip6h->ip6_src = orig_ip6h->ip6_dst;
new_ip6h->ip6_dst = orig_ip6h->ip6_src;
*new_icmp6 = *icmp6;
new_icmp6->icmp6_cksum = new_ip6h->ip6_plen;
bzero(&ixas, sizeof (ixas));
ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
if (IN6_IS_ADDR_UNSPECIFIED(&new_ip6h->ip6_src)) {
ixas.ixa_flags &= ~IXAF_VERIFY_SOURCE;
ixas.ixa_flags |= IXAF_SET_SOURCE;
}
ixas.ixa_zoneid = IPCL_ZONEID(connp);
ixas.ixa_ipst = connp->conn_netstack->netstack_ip;
ixas.ixa_cred = connp->conn_cred;
ixas.ixa_cpid = NOPID;
if (is_system_labeled())
ixas.ixa_tsl = tsl;
ixas.ixa_ifindex = 0;
ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
(void) ip_output_simple(icmp6err_mp, &ixas);
ixa_cleanup(&ixas);
}
static void
iptun_icmp_error_v4(iptun_t *iptun, ipha_t *orig_ipha, mblk_t *mp,
uint8_t type, uint8_t code, ts_label_t *tsl)
{
icmph_t icmp;
bzero(&icmp, sizeof (icmp));
icmp.icmph_type = type;
icmp.icmph_code = code;
iptun_sendicmp_v4(iptun, &icmp, orig_ipha, mp, tsl);
}
static void
iptun_icmp_fragneeded_v4(iptun_t *iptun, uint32_t newmtu, ipha_t *orig_ipha,
mblk_t *mp, ts_label_t *tsl)
{
icmph_t icmp;
icmp.icmph_type = ICMP_DEST_UNREACHABLE;
icmp.icmph_code = ICMP_FRAGMENTATION_NEEDED;
icmp.icmph_du_zero = 0;
icmp.icmph_du_mtu = htons(newmtu);
iptun_sendicmp_v4(iptun, &icmp, orig_ipha, mp, tsl);
}
static void
iptun_icmp_error_v6(iptun_t *iptun, ip6_t *orig_ip6h, mblk_t *mp,
uint8_t type, uint8_t code, uint32_t offset, ts_label_t *tsl)
{
icmp6_t icmp6;
bzero(&icmp6, sizeof (icmp6));
icmp6.icmp6_type = type;
icmp6.icmp6_code = code;
if (type == ICMP6_PARAM_PROB)
icmp6.icmp6_pptr = htonl(offset);
iptun_sendicmp_v6(iptun, &icmp6, orig_ip6h, mp, tsl);
}
static void
iptun_icmp_toobig_v6(iptun_t *iptun, uint32_t newmtu, ip6_t *orig_ip6h,
mblk_t *mp, ts_label_t *tsl)
{
icmp6_t icmp6;
icmp6.icmp6_type = ICMP6_PACKET_TOO_BIG;
icmp6.icmp6_code = 0;
icmp6.icmp6_mtu = htonl(newmtu);
iptun_sendicmp_v6(iptun, &icmp6, orig_ip6h, mp, tsl);
}
static boolean_t
is_icmp_error(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h)
{
uint16_t hlen;
if (ipha != NULL) {
icmph_t *icmph;
ASSERT(ip6h == NULL);
if (ipha->ipha_protocol != IPPROTO_ICMP)
return (B_FALSE);
hlen = IPH_HDR_LENGTH(ipha);
icmph = (icmph_t *)((uint8_t *)ipha + hlen);
return (ICMP_IS_ERROR(icmph->icmph_type) ||
icmph->icmph_type == ICMP_REDIRECT);
} else {
icmp6_t *icmp6;
uint8_t *nexthdrp;
ASSERT(ip6h != NULL);
if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &hlen, &nexthdrp) ||
*nexthdrp != IPPROTO_ICMPV6) {
return (B_FALSE);
}
icmp6 = (icmp6_t *)((uint8_t *)ip6h + hlen);
return (ICMP6_IS_ERROR(icmp6->icmp6_type) ||
icmp6->icmp6_type == ND_REDIRECT);
}
}
static size_t
iptun_find_headers(mblk_t *mp, size_t outer_hlen, ipha_t **outer4,
ipha_t **inner4, ip6_t **outer6, ip6_t **inner6)
{
ipha_t *ipha;
size_t first_mblkl = MBLKL(mp);
mblk_t *inner_mp;
if (first_mblkl < sizeof (ipha_t))
return (0);
ipha = (ipha_t *)(mp->b_rptr);
switch (IPH_HDR_VERSION(ipha)) {
case IPV4_VERSION:
*outer4 = ipha;
*outer6 = NULL;
if (outer_hlen == 0)
outer_hlen = IPH_HDR_LENGTH(ipha);
break;
case IPV6_VERSION:
*outer4 = NULL;
*outer6 = (ip6_t *)ipha;
if (outer_hlen == 0)
outer_hlen = ip_hdr_length_v6(mp, (ip6_t *)ipha);
break;
default:
return (0);
}
if (first_mblkl < outer_hlen ||
(first_mblkl == outer_hlen && mp->b_cont == NULL))
return (0);
if (first_mblkl == outer_hlen) {
inner_mp = mp->b_cont;
ipha = (ipha_t *)inner_mp->b_rptr;
} else {
inner_mp = mp;
ipha = (ipha_t *)(mp->b_rptr + outer_hlen);
}
switch (IPH_HDR_VERSION(ipha)) {
case IPV4_VERSION:
if (inner_mp->b_wptr - (uint8_t *)ipha < sizeof (ipha_t))
return (0);
*inner4 = ipha;
*inner6 = NULL;
break;
case IPV6_VERSION:
if (inner_mp->b_wptr - (uint8_t *)ipha < sizeof (ip6_t))
return (0);
*inner4 = NULL;
*inner6 = (ip6_t *)ipha;
break;
default:
return (0);
}
return (outer_hlen);
}
static void
iptun_input_icmp_v4(iptun_t *iptun, mblk_t *data_mp, icmph_t *icmph,
ip_recv_attr_t *ira)
{
uint8_t *orig;
ipha_t *outer4, *inner4;
ip6_t *outer6, *inner6;
int outer_hlen;
uint8_t type, code;
ASSERT(data_mp->b_cont == NULL);
orig = data_mp->b_rptr;
data_mp->b_rptr = (uint8_t *)(icmph + 1);
ASSERT(MBLKL(data_mp) >= 0);
outer_hlen = iptun_find_headers(data_mp, 0, &outer4, &inner4, &outer6,
&inner6);
ASSERT(outer6 == NULL);
data_mp->b_rptr = orig;
if (outer_hlen == 0) {
iptun_drop_pkt(data_mp, &iptun->iptun_ierrors);
return;
}
ASSERT(outer4->ipha_protocol == IPPROTO_ENCAP ||
outer4->ipha_protocol == IPPROTO_IPV6);
data_mp = ipsec_tun_inbound(ira, data_mp, iptun->iptun_itp,
inner4, inner6, outer4, outer6, -outer_hlen, iptun->iptun_ns);
if (data_mp == NULL) {
atomic_inc_64(&iptun->iptun_ierrors);
return;
}
ASSERT(data_mp->b_next == NULL);
data_mp->b_rptr = (uint8_t *)outer4 + outer_hlen;
if (is_icmp_error(data_mp, inner4, inner6)) {
iptun_drop_pkt(data_mp, &iptun->iptun_norcvbuf);
return;
}
switch (icmph->icmph_type) {
case ICMP_DEST_UNREACHABLE:
type = (inner4 != NULL ? icmph->icmph_type : ICMP6_DST_UNREACH);
switch (icmph->icmph_code) {
case ICMP_FRAGMENTATION_NEEDED: {
uint32_t newmtu;
newmtu = iptun_update_mtu(iptun, NULL,
ntohs(icmph->icmph_du_mtu));
if (inner4 != NULL) {
iptun_icmp_fragneeded_v4(iptun, newmtu, inner4,
data_mp, ira->ira_tsl);
} else {
iptun_icmp_toobig_v6(iptun, newmtu, inner6,
data_mp, ira->ira_tsl);
}
return;
}
case ICMP_DEST_NET_UNREACH_ADMIN:
case ICMP_DEST_HOST_UNREACH_ADMIN:
code = (inner4 != NULL ? ICMP_DEST_NET_UNREACH_ADMIN :
ICMP6_DST_UNREACH_ADMIN);
break;
default:
code = (inner4 != NULL ? ICMP_HOST_UNREACHABLE :
ICMP6_DST_UNREACH_ADDR);
break;
}
break;
case ICMP_TIME_EXCEEDED:
if (inner6 != NULL) {
type = ICMP6_TIME_EXCEEDED;
code = 0;
}
break;
case ICMP_PARAM_PROBLEM:
iptun_drop_pkt(data_mp, &iptun->iptun_oerrors);
return;
default:
iptun_drop_pkt(data_mp, &iptun->iptun_norcvbuf);
return;
}
if (inner4 != NULL) {
iptun_icmp_error_v4(iptun, inner4, data_mp, type, code,
ira->ira_tsl);
} else {
iptun_icmp_error_v6(iptun, inner6, data_mp, type, code, 0,
ira->ira_tsl);
}
}
static boolean_t
iptun_find_encaplimit(mblk_t *mp, ip6_t *ip6h, uint8_t **encaplim_ptr)
{
ip_pkt_t pkt;
uint8_t *endptr;
ip6_dest_t *destp;
struct ip6_opt *optp;
pkt.ipp_fields = 0;
(void) ip_find_hdr_v6(mp, ip6h, B_FALSE, &pkt, NULL);
if ((pkt.ipp_fields & IPPF_DSTOPTS) != 0) {
destp = pkt.ipp_dstopts;
} else if ((pkt.ipp_fields & IPPF_RTHDRDSTOPTS) != 0) {
destp = pkt.ipp_rthdrdstopts;
} else {
return (B_FALSE);
}
endptr = (uint8_t *)destp + 8 * (destp->ip6d_len + 1);
optp = (struct ip6_opt *)(destp + 1);
while (endptr - (uint8_t *)optp > sizeof (*optp)) {
if (optp->ip6o_type == IP6OPT_TUNNEL_LIMIT) {
if ((uint8_t *)(optp + 1) >= endptr)
return (B_FALSE);
*encaplim_ptr = (uint8_t *)&optp[1];
return (B_TRUE);
}
optp = (struct ip6_opt *)((uint8_t *)optp + optp->ip6o_len + 2);
}
return (B_FALSE);
}
static void
iptun_input_icmp_v6(iptun_t *iptun, mblk_t *data_mp, icmp6_t *icmp6h,
ip_recv_attr_t *ira)
{
uint8_t *orig;
ipha_t *outer4, *inner4;
ip6_t *outer6, *inner6;
int outer_hlen;
uint8_t type, code;
ASSERT(data_mp->b_cont == NULL);
orig = data_mp->b_rptr;
data_mp->b_rptr = (uint8_t *)(icmp6h + 1);
ASSERT(MBLKL(data_mp) >= 0);
outer_hlen = iptun_find_headers(data_mp, 0, &outer4, &inner4, &outer6,
&inner6);
ASSERT(outer4 == NULL);
data_mp->b_rptr = orig;
if (outer_hlen == 0) {
iptun_drop_pkt(data_mp, &iptun->iptun_ierrors);
return;
}
data_mp = ipsec_tun_inbound(ira, data_mp, iptun->iptun_itp,
inner4, inner6, outer4, outer6, -outer_hlen, iptun->iptun_ns);
if (data_mp == NULL) {
atomic_inc_64(&iptun->iptun_ierrors);
return;
}
ASSERT(data_mp->b_next == NULL);
data_mp->b_rptr = (uint8_t *)outer6 + outer_hlen;
if (is_icmp_error(data_mp, inner4, inner6)) {
iptun_drop_pkt(data_mp, &iptun->iptun_norcvbuf);
return;
}
switch (icmp6h->icmp6_type) {
case ICMP6_PARAM_PROB: {
uint8_t *encaplim_ptr;
if (!iptun_find_encaplimit(data_mp, outer6, &encaplim_ptr) ||
(icmp6h->icmp6_pptr !=
((ptrdiff_t)encaplim_ptr - (ptrdiff_t)outer6)) ||
*encaplim_ptr != 0) {
iptun_drop_pkt(data_mp, &iptun->iptun_oerrors);
return;
}
}
case ICMP6_TIME_EXCEEDED:
case ICMP6_DST_UNREACH:
type = (inner4 != NULL ? ICMP_DEST_UNREACHABLE :
ICMP6_DST_UNREACH);
code = (inner4 != NULL ? ICMP_HOST_UNREACHABLE :
ICMP6_DST_UNREACH_ADDR);
break;
case ICMP6_PACKET_TOO_BIG: {
uint32_t newmtu;
newmtu = iptun_update_mtu(iptun, NULL,
ntohl(icmp6h->icmp6_mtu));
if (inner4 != NULL) {
iptun_icmp_fragneeded_v4(iptun, newmtu, inner4,
data_mp, ira->ira_tsl);
} else {
iptun_icmp_toobig_v6(iptun, newmtu, inner6, data_mp,
ira->ira_tsl);
}
return;
}
default:
iptun_drop_pkt(data_mp, &iptun->iptun_norcvbuf);
return;
}
if (inner4 != NULL) {
iptun_icmp_error_v4(iptun, inner4, data_mp, type, code,
ira->ira_tsl);
} else {
iptun_icmp_error_v6(iptun, inner6, data_mp, type, code, 0,
ira->ira_tsl);
}
}
static void
iptun_input_icmp(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
{
conn_t *connp = arg;
iptun_t *iptun = connp->conn_iptun;
mblk_t *tmpmp;
size_t hlen;
ASSERT(IPCL_IS_IPTUN(connp));
if (mp->b_cont != NULL) {
if ((tmpmp = msgpullup(mp, -1)) == NULL) {
iptun_drop_pkt(mp, &iptun->iptun_norcvbuf);
return;
}
freemsg(mp);
mp = tmpmp;
}
hlen = ira->ira_ip_hdr_length;
switch (iptun->iptun_typeinfo->iti_ipvers) {
case IPV4_VERSION:
iptun_input_icmp_v4(iptun, mp, (icmph_t *)(mp->b_rptr + hlen),
ira);
break;
case IPV6_VERSION:
iptun_input_icmp_v6(iptun, mp, (icmp6_t *)(mp->b_rptr + hlen),
ira);
break;
}
}
static boolean_t
iptun_in_6to4_ok(iptun_t *iptun, ipha_t *outer4, ip6_t *inner6)
{
ipaddr_t v4addr;
if (inner6 == NULL)
return (B_FALSE);
IN6_6TO4_TO_V4ADDR(&inner6->ip6_dst, (struct in_addr *)&v4addr);
if (outer4->ipha_dst != v4addr)
return (B_FALSE);
if (IN6_IS_ADDR_6TO4(&inner6->ip6_src)) {
IN6_6TO4_TO_V4ADDR(&inner6->ip6_src, (struct in_addr *)&v4addr);
if (outer4->ipha_src != v4addr)
return (B_FALSE);
} else {
if (iptun->iptun_iptuns->iptuns_relay_rtr_addr == INADDR_ANY)
return (B_FALSE);
}
return (B_TRUE);
}
static void
iptun_input(void *arg, mblk_t *data_mp, void *arg2, ip_recv_attr_t *ira)
{
conn_t *connp = arg;
iptun_t *iptun = connp->conn_iptun;
int outer_hlen;
ipha_t *outer4, *inner4;
ip6_t *outer6, *inner6;
ASSERT(IPCL_IS_IPTUN(connp));
ASSERT(DB_TYPE(data_mp) == M_DATA);
outer_hlen = iptun_find_headers(data_mp, ira->ira_ip_hdr_length,
&outer4, &inner4, &outer6, &inner6);
if (outer_hlen == 0)
goto drop;
if (ira->ira_flags & IRAF_SYSTEM_LABELED) {
if (ira->ira_tsl == NULL)
goto drop;
if (tsol_check_dest(ira->ira_tsl, (outer4 != NULL ?
(void *)&outer4->ipha_dst : (void *)&outer6->ip6_dst),
(outer4 != NULL ? IPV4_VERSION : IPV6_VERSION),
CONN_MAC_DEFAULT, B_FALSE, NULL) != 0)
goto drop;
}
data_mp = ipsec_tun_inbound(ira, data_mp, iptun->iptun_itp,
inner4, inner6, outer4, outer6, outer_hlen, iptun->iptun_ns);
if (data_mp == NULL) {
return;
}
if (iptun->iptun_typeinfo->iti_type == IPTUN_TYPE_6TO4 &&
!iptun_in_6to4_ok(iptun, outer4, inner6))
goto drop;
do {
mblk_t *mp;
mp = data_mp->b_next;
data_mp->b_next = NULL;
atomic_inc_64(&iptun->iptun_ipackets);
atomic_add_64(&iptun->iptun_rbytes, msgdsize(data_mp));
mac_rx(iptun->iptun_mh, NULL, data_mp);
data_mp = mp;
} while (data_mp != NULL);
return;
drop:
iptun_drop_pkt(data_mp, &iptun->iptun_ierrors);
}
static boolean_t
iptun_out_process_6to4(iptun_t *iptun, ipha_t *outer4, ip6_t *inner6)
{
ipaddr_t v4addr;
if (!IN6_IS_ADDR_6TO4(&inner6->ip6_src))
return (B_FALSE);
IN6_6TO4_TO_V4ADDR(&inner6->ip6_src, (struct in_addr *)&v4addr);
if (outer4->ipha_src != v4addr)
return (B_FALSE);
if (IN6_IS_ADDR_6TO4(&inner6->ip6_dst)) {
IN6_6TO4_TO_V4ADDR(&inner6->ip6_dst,
(struct in_addr *)&outer4->ipha_dst);
if (outer4->ipha_dst == INADDR_ANY)
return (B_FALSE);
} else {
if (iptun->iptun_iptuns->iptuns_relay_rtr_addr == INADDR_ANY)
return (B_FALSE);
outer4->ipha_dst = iptun->iptun_iptuns->iptuns_relay_rtr_addr;
}
return (outer4->ipha_src != outer4->ipha_dst);
}
static mblk_t *
iptun_out_process_ipv4(iptun_t *iptun, mblk_t *mp, ipha_t *outer4,
ipha_t *inner4, ip6_t *inner6, ip_xmit_attr_t *ixa)
{
uint8_t *innerptr = (inner4 != NULL ?
(uint8_t *)inner4 : (uint8_t *)inner6);
size_t minmtu = iptun->iptun_typeinfo->iti_minmtu;
if (inner4 != NULL) {
ASSERT(outer4->ipha_protocol == IPPROTO_ENCAP);
outer4->ipha_type_of_service =
inner4->ipha_type_of_service & ~0x03;
} else {
ASSERT(outer4->ipha_protocol == IPPROTO_IPV6 &&
inner6 != NULL);
}
if (ixa->ixa_flags & IXAF_PMTU_IPV4_DF)
outer4->ipha_fragment_offset_and_flags |= IPH_DF_HTONS;
else
outer4->ipha_fragment_offset_and_flags &= ~IPH_DF_HTONS;
if (mp->b_wptr - innerptr <= minmtu) {
outer4->ipha_fragment_offset_and_flags = 0;
ixa->ixa_flags &= ~IXAF_DONTFRAG;
} else if (!(ixa->ixa_flags & IXAF_PMTU_TOO_SMALL) &&
(iptun->iptun_typeinfo->iti_type != IPTUN_TYPE_6TO4)) {
ixa->ixa_flags |= IXAF_DONTFRAG;
}
ixa->ixa_ip_hdr_length = IPH_HDR_LENGTH(outer4);
ixa->ixa_pktlen = msgdsize(mp);
ixa->ixa_protocol = outer4->ipha_protocol;
outer4->ipha_length = htons(ixa->ixa_pktlen);
return (mp);
}
static mblk_t *
iptun_insert_encaplimit(iptun_t *iptun, mblk_t *mp, ip6_t *outer6,
uint8_t limit)
{
mblk_t *newmp;
iptun_ipv6hdrs_t *newouter6;
ASSERT(outer6->ip6_nxt == IPPROTO_IPV6);
ASSERT(mp->b_cont == NULL);
mp->b_rptr += sizeof (ip6_t);
newmp = allocb(sizeof (iptun_ipv6hdrs_t) + MBLKL(mp), BPRI_MED);
if (newmp == NULL) {
iptun_drop_pkt(mp, &iptun->iptun_noxmtbuf);
return (NULL);
}
newmp->b_wptr += sizeof (iptun_ipv6hdrs_t);
bcopy(mp->b_rptr, newmp->b_wptr, MBLKL(mp));
newmp->b_wptr += MBLKL(mp);
newouter6 = (iptun_ipv6hdrs_t *)newmp->b_rptr;
bcopy(outer6, &newouter6->it6h_ip6h, sizeof (ip6_t));
newouter6->it6h_ip6h.ip6_nxt = IPPROTO_DSTOPTS;
newouter6->it6h_encaplim = iptun_encaplim_init;
newouter6->it6h_encaplim.iel_destopt.ip6d_nxt = outer6->ip6_nxt;
newouter6->it6h_encaplim.iel_telopt.ip6ot_encap_limit = limit;
freemsg(mp);
return (newmp);
}
static mblk_t *
iptun_out_process_ipv6(iptun_t *iptun, mblk_t *mp, ip6_t *outer6,
ipha_t *inner4, ip6_t *inner6, ip_xmit_attr_t *ixa)
{
uint8_t *innerptr = (inner4 != NULL ?
(uint8_t *)inner4 : (uint8_t *)inner6);
size_t minmtu = iptun->iptun_typeinfo->iti_minmtu;
uint8_t *limit, *configlimit;
uint32_t offset;
iptun_ipv6hdrs_t *v6hdrs;
if (inner6 != NULL && iptun_find_encaplimit(mp, inner6, &limit)) {
ASSERT(limit > mp->b_rptr && limit < mp->b_wptr);
if (*limit == 0) {
mp->b_rptr = (uint8_t *)inner6;
offset = limit - mp->b_rptr;
iptun_icmp_error_v6(iptun, inner6, mp, ICMP6_PARAM_PROB,
0, offset, ixa->ixa_tsl);
atomic_inc_64(&iptun->iptun_noxmtbuf);
return (NULL);
}
if (iptun->iptun_encaplimit == 0) {
if ((mp = iptun_insert_encaplimit(iptun, mp, outer6,
(*limit - 1))) == NULL)
return (NULL);
v6hdrs = (iptun_ipv6hdrs_t *)mp->b_rptr;
} else {
v6hdrs = (iptun_ipv6hdrs_t *)mp->b_rptr;
configlimit =
&v6hdrs->it6h_encaplim.iel_telopt.ip6ot_encap_limit;
if ((*limit - 1) < *configlimit)
*configlimit = (*limit - 1);
}
ixa->ixa_ip_hdr_length = sizeof (iptun_ipv6hdrs_t);
ixa->ixa_protocol = v6hdrs->it6h_encaplim.iel_destopt.ip6d_nxt;
} else {
ixa->ixa_ip_hdr_length = sizeof (ip6_t);
ixa->ixa_protocol = outer6->ip6_nxt;
}
if (mp->b_wptr - innerptr <= minmtu)
ixa->ixa_flags &= ~IXAF_DONTFRAG;
else if (!(ixa->ixa_flags & IXAF_PMTU_TOO_SMALL))
ixa->ixa_flags |= IXAF_DONTFRAG;
ixa->ixa_pktlen = msgdsize(mp);
outer6->ip6_plen = htons(ixa->ixa_pktlen - sizeof (ip6_t));
return (mp);
}
static void
iptun_output(iptun_t *iptun, mblk_t *mp)
{
conn_t *connp = iptun->iptun_connp;
mblk_t *newmp;
int error;
ip_xmit_attr_t *ixa;
ASSERT(mp->b_datap->db_type == M_DATA);
if (mp->b_cont != NULL) {
if ((newmp = msgpullup(mp, -1)) == NULL) {
iptun_drop_pkt(mp, &iptun->iptun_noxmtbuf);
return;
}
freemsg(mp);
mp = newmp;
}
if (iptun->iptun_typeinfo->iti_type == IPTUN_TYPE_6TO4) {
iptun_output_6to4(iptun, mp);
return;
}
if (is_system_labeled()) {
ixa = conn_get_ixa_exclusive(connp);
} else {
ixa = conn_get_ixa(connp, B_FALSE);
}
if (ixa == NULL) {
iptun_drop_pkt(mp, &iptun->iptun_oerrors);
return;
}
if (ixa->ixa_ire == NULL) {
error = ip_attr_connect(connp, ixa, &connp->conn_saddr_v6,
&connp->conn_faddr_v6, &connp->conn_faddr_v6, 0,
NULL, NULL, 0);
if (error != 0) {
if (ixa->ixa_ire != NULL &&
(error == EHOSTUNREACH || error == ENETUNREACH)) {
error = 0;
} else {
ixa_refrele(ixa);
iptun_drop_pkt(mp, &iptun->iptun_oerrors);
return;
}
}
}
iptun_output_common(iptun, ixa, mp);
ixa_refrele(ixa);
}
static void
iptun_output_6to4(iptun_t *iptun, mblk_t *mp)
{
conn_t *connp = iptun->iptun_connp;
ipha_t *outer4, *inner4;
ip6_t *outer6, *inner6;
ip_xmit_attr_t *ixa;
ip_xmit_attr_t *oldixa;
int error;
boolean_t need_connect;
in6_addr_t v6dst;
ASSERT(mp->b_cont == NULL);
(void) iptun_find_headers(mp, 0, &outer4, &inner4, &outer6, &inner6);
ASSERT(outer4 != NULL);
if (!iptun_out_process_6to4(iptun, outer4, inner6)) {
iptun_drop_pkt(mp, &iptun->iptun_oerrors);
return;
}
if (is_system_labeled()) {
ixa = conn_get_ixa_exclusive(connp);
} else {
ixa = conn_get_ixa(connp, B_FALSE);
}
if (ixa == NULL) {
iptun_drop_pkt(mp, &iptun->iptun_oerrors);
return;
}
mutex_enter(&connp->conn_lock);
if (connp->conn_v4lastdst == outer4->ipha_dst) {
need_connect = (ixa->ixa_ire == NULL);
} else {
ip_attr_newdst(ixa);
need_connect = B_TRUE;
}
mutex_exit(&connp->conn_lock);
if (need_connect) {
IN6_IPADDR_TO_V4MAPPED(outer4->ipha_dst, &v6dst);
error = ip_attr_connect(connp, ixa, &connp->conn_saddr_v6,
&v6dst, &v6dst, 0, NULL, NULL, 0);
if (error != 0) {
if (ixa->ixa_ire != NULL &&
(error == EHOSTUNREACH || error == ENETUNREACH)) {
error = 0;
} else {
ixa_refrele(ixa);
iptun_drop_pkt(mp, &iptun->iptun_oerrors);
return;
}
}
}
iptun_output_common(iptun, ixa, mp);
mutex_enter(&connp->conn_lock);
if (connp->conn_v4lastdst != outer4->ipha_dst) {
connp->conn_v6lastdst = v6dst;
oldixa = conn_replace_ixa(connp, ixa);
} else {
oldixa = NULL;
}
mutex_exit(&connp->conn_lock);
ixa_refrele(ixa);
if (oldixa != NULL)
ixa_refrele(oldixa);
}
static int
iptun_output_check_label(mblk_t **mpp, ip_xmit_attr_t *ixa)
{
cred_t *cr;
int adjust;
int iplen;
int err;
ts_label_t *effective_tsl = NULL;
ASSERT(is_system_labeled());
cr = msg_getcred(*mpp, NULL);
if (cr == NULL)
return (0);
ip_xmit_attr_restore_tsl(ixa, cr);
if (ixa->ixa_flags & IXAF_IS_IPV4) {
ipha_t *ipha;
ipha = (ipha_t *)(*mpp)->b_rptr;
iplen = ntohs(ipha->ipha_length);
err = tsol_check_label_v4(ixa->ixa_tsl,
ixa->ixa_zoneid, mpp, CONN_MAC_DEFAULT, B_FALSE,
ixa->ixa_ipst, &effective_tsl);
if (err != 0)
return (err);
ipha = (ipha_t *)(*mpp)->b_rptr;
adjust = (int)ntohs(ipha->ipha_length) - iplen;
} else {
ip6_t *ip6h;
ip6h = (ip6_t *)(*mpp)->b_rptr;
iplen = ntohs(ip6h->ip6_plen);
err = tsol_check_label_v6(ixa->ixa_tsl,
ixa->ixa_zoneid, mpp, CONN_MAC_DEFAULT, B_FALSE,
ixa->ixa_ipst, &effective_tsl);
if (err != 0)
return (err);
ip6h = (ip6_t *)(*mpp)->b_rptr;
adjust = (int)ntohs(ip6h->ip6_plen) - iplen;
}
if (effective_tsl != NULL) {
ip_xmit_attr_replace_tsl(ixa, effective_tsl);
}
ixa->ixa_pktlen += adjust;
ixa->ixa_ip_hdr_length += adjust;
return (0);
}
static void
iptun_output_common(iptun_t *iptun, ip_xmit_attr_t *ixa, mblk_t *mp)
{
ipsec_tun_pol_t *itp = iptun->iptun_itp;
int outer_hlen;
mblk_t *newmp;
ipha_t *outer4, *inner4;
ip6_t *outer6, *inner6;
int error;
boolean_t update_pktlen;
ASSERT(ixa->ixa_ire != NULL);
outer_hlen = iptun_find_headers(mp, 0, &outer4, &inner4, &outer6,
&inner6);
if (outer_hlen == 0) {
iptun_drop_pkt(mp, &iptun->iptun_oerrors);
return;
}
iaflags_t dontfrag = ixa->ixa_flags & IXAF_DONTFRAG;
if (outer4 != NULL) {
mp = iptun_out_process_ipv4(iptun, mp, outer4, inner4, inner6,
ixa);
} else {
mp = iptun_out_process_ipv6(iptun, mp, outer6, inner4, inner6,
ixa);
}
if (mp == NULL)
return;
if (itp != NULL && (itp->itp_flags & ITPF_P_ACTIVE)) {
mp = ipsec_tun_outbound(mp, iptun, inner4, inner6, outer4,
outer6, outer_hlen, ixa);
if (mp == NULL) {
atomic_inc_64(&iptun->iptun_oerrors);
return;
}
if (is_system_labeled()) {
error = iptun_output_check_label(&mp, ixa);
if (error != 0) {
ip2dbg(("label check failed (%d)\n", error));
iptun_drop_pkt(mp, &iptun->iptun_oerrors);
return;
}
}
update_pktlen = (mp->b_next != NULL);
for (newmp = mp; newmp != NULL; newmp = mp) {
size_t minmtu = iptun->iptun_typeinfo->iti_minmtu;
atomic_inc_64(&iptun->iptun_opackets);
atomic_add_64(&iptun->iptun_obytes, ixa->ixa_pktlen);
mp = mp->b_next;
newmp->b_next = NULL;
if (update_pktlen) {
ixa->ixa_pktlen = msgdsize(newmp);
if (ixa->ixa_pktlen <= minmtu)
ixa->ixa_flags &= ~IXAF_DONTFRAG;
}
atomic_inc_64(&iptun->iptun_opackets);
atomic_add_64(&iptun->iptun_obytes, ixa->ixa_pktlen);
error = conn_ip_output(newmp, ixa);
ixa->ixa_flags |= dontfrag;
if (error == EMSGSIZE) {
(void) iptun_update_mtu(iptun, ixa, 0);
}
}
} else {
ASSERT(ixa->ixa_ipsec_policy == NULL);
mp = ip_output_attach_policy(mp, outer4, outer6, NULL, ixa);
if (mp == NULL) {
atomic_inc_64(&iptun->iptun_oerrors);
return;
}
if (is_system_labeled()) {
error = iptun_output_check_label(&mp, ixa);
if (error != 0) {
ip2dbg(("label check failed (%d)\n", error));
iptun_drop_pkt(mp, &iptun->iptun_oerrors);
return;
}
}
atomic_inc_64(&iptun->iptun_opackets);
atomic_add_64(&iptun->iptun_obytes, ixa->ixa_pktlen);
error = conn_ip_output(mp, ixa);
if (error == EMSGSIZE) {
(void) iptun_update_mtu(iptun, ixa, 0);
}
}
if (ixa->ixa_flags & IXAF_IPSEC_SECURE)
ipsec_out_release_refs(ixa);
}
static mac_callbacks_t iptun_m_callbacks = {
.mc_callbacks = (MC_SETPROP | MC_GETPROP | MC_PROPINFO),
.mc_getstat = iptun_m_getstat,
.mc_start = iptun_m_start,
.mc_stop = iptun_m_stop,
.mc_setpromisc = iptun_m_setpromisc,
.mc_multicst = iptun_m_multicst,
.mc_unicst = iptun_m_unicst,
.mc_tx = iptun_m_tx,
.mc_reserved = NULL,
.mc_setprop = iptun_m_setprop,
.mc_getprop = iptun_m_getprop,
.mc_propinfo = iptun_m_propinfo
};