#include <sys/param.h>
#include <sys/systm.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/mbuf.h>
#include <sys/mcache.h>
#include <sys/errno.h>
#include <sys/domain.h>
#include <sys/protosw.h>
#include <sys/queue.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/ip_encap.h>
#if INET6
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet6/ip6protosw.h>
#endif
#include <net/net_osdep.h>
#ifndef __APPLE__
#include <sys/kernel.h>
#include <sys/malloc.h>
MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
#endif
static void encap_init(struct protosw *, struct domain *);
static void encap_add(struct encaptab *);
static int mask_match(const struct encaptab *, const struct sockaddr *,
const struct sockaddr *);
static void encap_fillarg(struct mbuf *, const struct encaptab *);
#ifndef LIST_HEAD_INITIALIZER
LIST_HEAD(, encaptab) encaptab;
#else
LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(&encaptab);
#endif
static void
encap_init(struct protosw *pp, struct domain *dp)
{
#pragma unused(dp)
static int encap_initialized = 0;
VERIFY((pp->pr_flags & (PR_INITIALIZED|PR_ATTACHED)) == PR_ATTACHED);
if (encap_initialized)
return;
encap_initialized = 1;
#if 0
LIST_INIT(&encaptab);
#endif
}
void
encap4_init(struct protosw *pp, struct domain *dp)
{
encap_init(pp, dp);
}
void
encap6_init(struct ip6protosw *pp, struct domain *dp)
{
encap_init((struct protosw *)pp, dp);
}
#if INET
void
encap4_input(m, off)
struct mbuf *m;
int off;
{
int proto;
struct ip *ip;
struct sockaddr_in s, d;
const struct protosw *psw;
struct encaptab *ep, *match;
int prio, matchprio;
#ifndef __APPLE__
va_start(ap, m);
off = va_arg(ap, int);
proto = va_arg(ap, int);
va_end(ap);
#endif
MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
ip = mtod(m, struct ip *);
#ifdef __APPLE__
proto = ip->ip_p;
#endif
bzero(&s, sizeof(s));
s.sin_family = AF_INET;
s.sin_len = sizeof(struct sockaddr_in);
s.sin_addr = ip->ip_src;
bzero(&d, sizeof(d));
d.sin_family = AF_INET;
d.sin_len = sizeof(struct sockaddr_in);
d.sin_addr = ip->ip_dst;
match = NULL;
matchprio = 0;
for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
if (ep->af != AF_INET)
continue;
if (ep->proto >= 0 && ep->proto != proto)
continue;
if (ep->func)
prio = (*ep->func)(m, off, proto, ep->arg);
else {
prio = mask_match(ep, (struct sockaddr *)&d,
(struct sockaddr *)&s);
}
if (prio <= 0)
continue;
if (prio > matchprio) {
matchprio = prio;
match = ep;
}
}
if (match) {
psw = (const struct protosw *)match->psw;
if (psw && psw->pr_input) {
encap_fillarg(m, match);
(*psw->pr_input)(m, off);
} else
m_freem(m);
return;
}
rip_input(m, off);
}
#endif
#if INET6
int
encap6_input(struct mbuf **mp, int *offp, int proto)
{
struct mbuf *m = *mp;
struct ip6_hdr *ip6;
struct sockaddr_in6 s, d;
const struct ip6protosw *psw;
struct encaptab *ep, *match;
int prio, matchprio;
MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
ip6 = mtod(m, struct ip6_hdr *);
bzero(&s, sizeof(s));
s.sin6_family = AF_INET6;
s.sin6_len = sizeof(struct sockaddr_in6);
s.sin6_addr = ip6->ip6_src;
bzero(&d, sizeof(d));
d.sin6_family = AF_INET6;
d.sin6_len = sizeof(struct sockaddr_in6);
d.sin6_addr = ip6->ip6_dst;
match = NULL;
matchprio = 0;
for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
if (ep->af != AF_INET6)
continue;
if (ep->proto >= 0 && ep->proto != proto)
continue;
if (ep->func)
prio = (*ep->func)(m, *offp, proto, ep->arg);
else {
prio = mask_match(ep, (struct sockaddr *)&d,
(struct sockaddr *)&s);
}
if (prio <= 0)
continue;
if (prio > matchprio) {
matchprio = prio;
match = ep;
}
}
if (match) {
psw = (const struct ip6protosw *)match->psw;
if (psw && psw->pr_input) {
encap_fillarg(m, match);
return (*psw->pr_input)(mp, offp, proto);
} else {
m_freem(m);
return IPPROTO_DONE;
}
}
return rip6_input(mp, offp, proto);
}
#endif
static void
encap_add(ep)
struct encaptab *ep;
{
LIST_INSERT_HEAD(&encaptab, ep, chain);
}
const struct encaptab *
encap_attach(af, proto, sp, sm, dp, dm, psw, arg)
int af;
int proto;
const struct sockaddr *sp, *sm;
const struct sockaddr *dp, *dm;
const struct protosw *psw;
void *arg;
{
struct encaptab *ep;
int error;
if (sp->sa_len > sizeof(ep->src) || dp->sa_len > sizeof(ep->dst)) {
error = EINVAL;
goto fail;
}
if (sp->sa_len != dp->sa_len) {
error = EINVAL;
goto fail;
}
if (af != sp->sa_family || af != dp->sa_family) {
error = EINVAL;
goto fail;
}
for (ep = LIST_FIRST(&encaptab); ep; ep = LIST_NEXT(ep, chain)) {
if (ep->af != af)
continue;
if (ep->proto != proto)
continue;
if (ep->src.ss_len != sp->sa_len ||
bcmp(&ep->src, sp, sp->sa_len) != 0 ||
bcmp(&ep->srcmask, sm, sp->sa_len) != 0)
continue;
if (ep->dst.ss_len != dp->sa_len ||
bcmp(&ep->dst, dp, dp->sa_len) != 0 ||
bcmp(&ep->dstmask, dm, dp->sa_len) != 0)
continue;
error = EEXIST;
goto fail;
}
ep = _MALLOC(sizeof(*ep), M_NETADDR, M_WAITOK | M_ZERO);
if (ep == NULL) {
error = ENOBUFS;
goto fail;
}
ep->af = af;
ep->proto = proto;
bcopy(sp, &ep->src, sp->sa_len);
bcopy(sm, &ep->srcmask, sp->sa_len);
bcopy(dp, &ep->dst, dp->sa_len);
bcopy(dm, &ep->dstmask, dp->sa_len);
ep->psw = psw;
ep->arg = arg;
encap_add(ep);
error = 0;
return ep;
fail:
return NULL;
}
const struct encaptab *
encap_attach_func(af, proto, func, psw, arg)
int af;
int proto;
int (*func)(const struct mbuf *, int, int, void *);
const struct protosw *psw;
void *arg;
{
struct encaptab *ep;
int error;
if (!func) {
error = EINVAL;
goto fail;
}
ep = _MALLOC(sizeof(*ep), M_NETADDR, M_WAITOK | M_ZERO);
if (ep == NULL) {
error = ENOBUFS;
goto fail;
}
ep->af = af;
ep->proto = proto;
ep->func = func;
ep->psw = psw;
ep->arg = arg;
encap_add(ep);
error = 0;
return ep;
fail:
return NULL;
}
int
encap_detach(cookie)
const struct encaptab *cookie;
{
const struct encaptab *ep = cookie;
struct encaptab *p;
for (p = LIST_FIRST(&encaptab); p; p = LIST_NEXT(p, chain)) {
if (p == ep) {
LIST_REMOVE(p, chain);
_FREE(p, M_NETADDR);
return 0;
}
}
return EINVAL;
}
static int
mask_match(ep, sp, dp)
const struct encaptab *ep;
const struct sockaddr *sp;
const struct sockaddr *dp;
{
struct sockaddr_storage s;
struct sockaddr_storage d;
int i;
const u_int8_t *p, *q;
u_int8_t *r;
int matchlen;
if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d))
return 0;
if (sp->sa_family != ep->af || dp->sa_family != ep->af)
return 0;
if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len)
return 0;
matchlen = 0;
p = (const u_int8_t *)sp;
q = (const u_int8_t *)&ep->srcmask;
r = (u_int8_t *)&s;
for (i = 0 ; i < sp->sa_len; i++) {
r[i] = p[i] & q[i];
matchlen += (q[i] ? 8 : 0);
}
p = (const u_int8_t *)dp;
q = (const u_int8_t *)&ep->dstmask;
r = (u_int8_t *)&d;
for (i = 0 ; i < dp->sa_len; i++) {
r[i] = p[i] & q[i];
matchlen += (q[i] ? 8 : 0);
}
s.ss_len = sp->sa_len;
s.ss_family = sp->sa_family;
d.ss_len = dp->sa_len;
d.ss_family = dp->sa_family;
if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 &&
bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) {
return matchlen;
} else
return 0;
}
struct encaptabtag {
void* *arg;
};
static void
encap_fillarg(
struct mbuf *m,
const struct encaptab *ep)
{
struct m_tag *tag;
struct encaptabtag *et;
tag = m_tag_create(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_ENCAP,
sizeof(struct encaptabtag), M_WAITOK, m);
if (tag != NULL) {
et = (struct encaptabtag*)(tag + 1);
et->arg = ep->arg;
m_tag_prepend(m, tag);
}
}
void *
encap_getarg(m)
struct mbuf *m;
{
struct m_tag *tag;
struct encaptabtag *et;
void *p = NULL;
tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_ENCAP, NULL);
if (tag) {
et = (struct encaptabtag*)(tag + 1);
p = et->arg;
m_tag_delete(m, tag);
}
return p;
}