#include <sys/types.h>
#include <sys/queue.h>
#include <sys/malloc.h>
#include <sys/param.h>
#include <sys/mbuf.h>
#include <sys/domain.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <machine/spl.h>
#include "kext_net.h"
struct nf_list nf_list;
int
register_sockfilter(struct NFDescriptor *nfp, struct NFDescriptor *nfp1,
struct protosw *pr, int flags)
{ int s;
static int NF_initted = 0;
if (nfp == NULL)
return(EINVAL);
s = splhigh();
if (!NF_initted)
{ NF_initted = 1;
TAILQ_INIT(&nf_list);
}
TAILQ_INSERT_TAIL(&nf_list, nfp, nf_list);
if (nfp->nf_flags & NFD_GLOBAL)
{ if (flags & NFF_BEFORE)
{ if (nfp1 == NULL)
{ TAILQ_INSERT_HEAD(&pr->pr_sfilter,
nfp, nf_next);
} else
TAILQ_INSERT_BEFORE(nfp1, nfp, nf_next);
} else
{ if (nfp1 == NULL)
{ TAILQ_INSERT_TAIL(&pr->pr_sfilter,
nfp, nf_next);
} else
TAILQ_INSERT_AFTER(&pr->pr_sfilter, nfp1,
nfp, nf_next);
}
}
splx(s);
return(0);
}
unregister_sockfilter(struct NFDescriptor *nfp, struct protosw *pr, int flags)
{ int s;
s = splhigh();
TAILQ_REMOVE(&nf_list, nfp, nf_list);
if (nfp->nf_flags & NFD_GLOBAL)
TAILQ_REMOVE(&pr->pr_sfilter, nfp, nf_next);
splx(s);
return(0);
}
struct NFDescriptor *
find_nke(unsigned int handle)
{ struct NFDescriptor *nfp;
nfp = nf_list.tqh_first;
while (nfp)
{ if (nfp->nf_handle == handle)
return(nfp);
nfp = nfp->nf_list.tqe_next;
}
return(NULL);
}
int
nke_insert(struct socket *so, struct so_nke *np)
{ int s, error;
struct kextcb *kp, *kp1;
struct NFDescriptor *nf1, *nf2 = NULL;
if (np->nke_where != NULL)
{ if ((nf2 = find_nke(np->nke_where)) == NULL)
{
return(ENXIO);
}
}
if ((nf1 = find_nke(np->nke_handle)) == NULL)
{
return(ENXIO);
}
kp = so->so_ext;
kp1 = NULL;
if (np->nke_flags & NFF_BEFORE)
{ if (nf2)
{ while (kp)
{ if (kp->e_nfd == nf2)
break;
kp1 = kp;
kp = kp->e_next;
}
if (kp == NULL)
return(ENXIO);
}
} else
{ if (nf2)
{ while (kp)
{ if (kp->e_nfd == nf2)
break;
kp1 = kp;
kp = kp->e_next;
}
if (kp == NULL)
return(ENXIO);
}
kp1 = kp;
}
MALLOC(kp, struct kextcb *, sizeof(*kp), M_TEMP, M_WAITOK);
if (kp == NULL)
return(ENOBUFS);
bzero(kp, sizeof (*kp));
if (kp1 == NULL)
{ kp->e_next = so->so_ext;
so->so_ext = kp;
} else
{ kp->e_next = kp1->e_next;
kp1->e_next = kp;
}
kp->e_fcb = NULL;
kp->e_nfd = nf1;
kp->e_soif = nf1->nf_soif;
kp->e_sout = nf1->nf_soutil;
if (kp->e_soif && kp->e_soif->sf_socreate)
(*kp->e_soif->sf_socreate)(so, so->so_proto, kp);
return(0);
}