#include <sys/param.h>
#include <sys/alq.h>
#include <sys/errno.h>
#include <sys/eventhandler.h>
#include <sys/hash.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/lock.h>
#include <sys/mbuf.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/pcpu.h>
#include <sys/proc.h>
#include <sys/reboot.h>
#include <sys/sbuf.h>
#include <sys/sdt.h>
#include <sys/smp.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sysctl.h>
#include <sys/unistd.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/pfil.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_kdtrace.h>
#include <netinet/in_fib.h>
#include <netinet/in_pcb.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/tcp_var.h>
#ifdef SIFTR_IPV6
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet6/in6_fib.h>
#include <netinet6/in6_pcb.h>
#endif
#include <machine/in_cksum.h>
#define V_MAJOR 1
#define V_BACKBREAK 3
#define V_BACKCOMPAT 0
#define MODVERSION __CONCAT(V_MAJOR, __CONCAT(V_BACKBREAK, V_BACKCOMPAT))
#define MODVERSION_STR __XSTRING(V_MAJOR) "." __XSTRING(V_BACKBREAK) "." \
__XSTRING(V_BACKCOMPAT)
#define HOOK 0
#define UNHOOK 1
#define SIFTR_EXPECTED_MAX_TCP_FLOWS 65536
#define SYS_NAME "FreeBSD"
#define PACKET_TAG_SIFTR 100
#define PACKET_COOKIE_SIFTR 21749576
#define SIFTR_LOG_FILE_MODE 0644
#define SIFTR_DISABLE 0
#define SIFTR_ENABLE 1
#define MAX_LOG_MSG_LEN 300
#define MAX_LOG_BATCH_SIZE 3
#define SIFTR_ALQ_BUFLEN (1000*MAX_LOG_MSG_LEN)
#ifdef SIFTR_IPV6
#define SIFTR_IPMODE 6
#else
#define SIFTR_IPMODE 4
#endif
static MALLOC_DEFINE(M_SIFTR, "siftr", "dynamic memory used by SIFTR");
static MALLOC_DEFINE(M_SIFTR_PKTNODE, "siftr_pktnode",
"SIFTR pkt_node struct");
static MALLOC_DEFINE(M_SIFTR_HASHNODE, "siftr_hashnode",
"SIFTR flow_hash_node struct");
struct pkt_node {
struct timeval tval;
enum {
DIR_IN = 0,
DIR_OUT = 1,
} direction;
uint8_t ipver;
uint16_t lport;
uint16_t fport;
union in_dependaddr laddr;
union in_dependaddr faddr;
uint32_t snd_cwnd;
uint32_t snd_wnd;
uint32_t rcv_wnd;
uint32_t t_flags2;
uint32_t snd_ssthresh;
int conn_state;
uint32_t mss;
uint32_t srtt;
u_char sack_enabled;
u_char snd_scale;
u_char rcv_scale;
u_int t_flags;
uint32_t rto;
u_int snd_buf_hiwater;
u_int snd_buf_cc;
u_int rcv_buf_hiwater;
u_int rcv_buf_cc;
u_int sent_inflight_bytes;
int t_segqlen;
u_int flowid;
u_int flowtype;
STAILQ_ENTRY(pkt_node) nodes;
};
struct flow_info
{
#ifdef SIFTR_IPV6
char laddr[INET6_ADDRSTRLEN];
char faddr[INET6_ADDRSTRLEN];
#else
char laddr[INET_ADDRSTRLEN];
char faddr[INET_ADDRSTRLEN];
#endif
uint16_t lport;
uint16_t fport;
uint32_t key;
};
struct flow_hash_node
{
uint16_t counter;
struct flow_info const_info;
LIST_ENTRY(flow_hash_node) nodes;
};
struct siftr_stats
{
uint64_t n_in;
uint64_t n_out;
uint32_t nskip_in_malloc;
uint32_t nskip_out_malloc;
uint32_t nskip_in_inpcb;
uint32_t nskip_out_inpcb;
uint32_t nskip_in_tcpcb;
uint32_t nskip_out_tcpcb;
uint32_t nskip_in_dejavu;
uint32_t nskip_out_dejavu;
};
DPCPU_DEFINE_STATIC(struct siftr_stats, ss);
static volatile unsigned int siftr_exit_pkt_manager_thread = 0;
static unsigned int siftr_enabled = 0;
static unsigned int siftr_pkts_per_log = 1;
static uint16_t siftr_port_filter = 0;
static char siftr_logfile[PATH_MAX] = "/var/log/siftr.log";
static char siftr_logfile_shadow[PATH_MAX] = "/var/log/siftr.log";
static u_long siftr_hashmask;
STAILQ_HEAD(pkthead, pkt_node) pkt_queue = STAILQ_HEAD_INITIALIZER(pkt_queue);
LIST_HEAD(listhead, flow_hash_node) *counter_hash;
static int wait_for_pkt;
static struct alq *siftr_alq = NULL;
static struct mtx siftr_pkt_queue_mtx;
static struct mtx siftr_pkt_mgr_mtx;
static struct thread *siftr_pkt_manager_thr = NULL;
static char direction[2] = {'i','o'};
static eventhandler_tag siftr_shutdown_tag;
static int siftr_sysctl_enabled_handler(SYSCTL_HANDLER_ARGS);
static int siftr_sysctl_logfile_name_handler(SYSCTL_HANDLER_ARGS);
SYSCTL_DECL(_net_inet_siftr);
SYSCTL_NODE(_net_inet, OID_AUTO, siftr, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
"siftr related settings");
SYSCTL_PROC(_net_inet_siftr, OID_AUTO, enabled,
CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
&siftr_enabled, 0, &siftr_sysctl_enabled_handler, "IU",
"switch siftr module operations on/off");
SYSCTL_PROC(_net_inet_siftr, OID_AUTO, logfile,
CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &siftr_logfile_shadow,
sizeof(siftr_logfile_shadow), &siftr_sysctl_logfile_name_handler, "A",
"file to save siftr log messages to");
SYSCTL_UINT(_net_inet_siftr, OID_AUTO, ppl, CTLFLAG_RW,
&siftr_pkts_per_log, 1,
"number of packets between generating a log message");
SYSCTL_U16(_net_inet_siftr, OID_AUTO, port_filter, CTLFLAG_RW,
&siftr_port_filter, 0,
"enable packet filter on a TCP port");
static inline struct flow_hash_node *
siftr_find_flow(struct listhead *counter_list, uint32_t id)
{
struct flow_hash_node *hash_node;
if (LIST_FIRST(counter_list) != NULL) {
LIST_FOREACH(hash_node, counter_list, nodes) {
if (hash_node->const_info.key == id) {
return hash_node;
}
}
}
return NULL;
}
static inline struct flow_hash_node *
siftr_new_hash_node(struct flow_info info, int dir,
struct siftr_stats *ss)
{
struct flow_hash_node *hash_node;
struct listhead *counter_list;
counter_list = counter_hash + (info.key & siftr_hashmask);
hash_node = malloc(sizeof(struct flow_hash_node), M_SIFTR_HASHNODE,
M_NOWAIT|M_ZERO);
if (hash_node != NULL) {
hash_node->counter = 0;
hash_node->const_info = info;
LIST_INSERT_HEAD(counter_list, hash_node, nodes);
return hash_node;
} else {
if (dir == DIR_IN)
ss->nskip_in_malloc++;
else
ss->nskip_out_malloc++;
return NULL;
}
}
static int
siftr_process_pkt(struct pkt_node * pkt_node, char *buf)
{
struct flow_hash_node *hash_node;
struct listhead *counter_list;
int ret_sz;
if (pkt_node->flowid == 0) {
panic("%s: flowid not available", __func__);
}
counter_list = counter_hash + (pkt_node->flowid & siftr_hashmask);
hash_node = siftr_find_flow(counter_list, pkt_node->flowid);
if (hash_node == NULL) {
return 0;
} else if (siftr_pkts_per_log > 1) {
hash_node->counter = (hash_node->counter + 1) %
siftr_pkts_per_log;
if (hash_node->counter > 0)
return 0;
}
ret_sz = snprintf(buf, MAX_LOG_MSG_LEN,
"%c,%jd.%06ld,%s,%hu,%s,%hu,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,"
"%u,%u,%u,%u,%u,%u,%u,%u\n",
direction[pkt_node->direction],
(intmax_t)pkt_node->tval.tv_sec,
pkt_node->tval.tv_usec,
hash_node->const_info.laddr,
hash_node->const_info.lport,
hash_node->const_info.faddr,
hash_node->const_info.fport,
pkt_node->snd_ssthresh,
pkt_node->snd_cwnd,
pkt_node->t_flags2,
pkt_node->snd_wnd,
pkt_node->rcv_wnd,
pkt_node->snd_scale,
pkt_node->rcv_scale,
pkt_node->conn_state,
pkt_node->mss,
pkt_node->srtt,
pkt_node->sack_enabled,
pkt_node->t_flags,
pkt_node->rto,
pkt_node->snd_buf_hiwater,
pkt_node->snd_buf_cc,
pkt_node->rcv_buf_hiwater,
pkt_node->rcv_buf_cc,
pkt_node->sent_inflight_bytes,
pkt_node->t_segqlen,
pkt_node->flowid,
pkt_node->flowtype);
return ret_sz;
}
static void
siftr_pkt_manager_thread(void *arg)
{
STAILQ_HEAD(pkthead, pkt_node) tmp_pkt_queue =
STAILQ_HEAD_INITIALIZER(tmp_pkt_queue);
struct pkt_node *pkt_node;
uint8_t draining;
struct ale *log_buf;
int ret_sz, cnt = 0;
char *bufp;
draining = 2;
mtx_lock(&siftr_pkt_mgr_mtx);
while (draining) {
mtx_sleep(&wait_for_pkt, &siftr_pkt_mgr_mtx, PWAIT, "pktwait",
1);
mtx_lock(&siftr_pkt_queue_mtx);
STAILQ_CONCAT(&tmp_pkt_queue, &pkt_queue);
mtx_unlock(&siftr_pkt_queue_mtx);
mtx_unlock(&siftr_pkt_mgr_mtx);
while ((pkt_node = STAILQ_FIRST(&tmp_pkt_queue)) != NULL) {
log_buf = alq_getn(siftr_alq, MAX_LOG_MSG_LEN *
((STAILQ_NEXT(pkt_node, nodes) != NULL) ?
MAX_LOG_BATCH_SIZE : 1),
ALQ_WAITOK);
if (log_buf != NULL) {
log_buf->ae_bytesused = 0;
bufp = log_buf->ae_data;
} else {
bufp = NULL;
}
STAILQ_FOREACH(pkt_node, &tmp_pkt_queue, nodes) {
if (log_buf != NULL) {
ret_sz = siftr_process_pkt(pkt_node,
bufp);
bufp += ret_sz;
log_buf->ae_bytesused += ret_sz;
}
if (++cnt >= MAX_LOG_BATCH_SIZE)
break;
}
if (log_buf != NULL) {
alq_post_flags(siftr_alq, log_buf, 0);
}
for (; cnt > 0; cnt--) {
pkt_node = STAILQ_FIRST(&tmp_pkt_queue);
STAILQ_REMOVE_HEAD(&tmp_pkt_queue, nodes);
free(pkt_node, M_SIFTR_PKTNODE);
}
}
KASSERT(STAILQ_EMPTY(&tmp_pkt_queue),
("SIFTR tmp_pkt_queue not empty after flush"));
mtx_lock(&siftr_pkt_mgr_mtx);
if (siftr_exit_pkt_manager_thread)
draining--;
}
mtx_unlock(&siftr_pkt_mgr_mtx);
kthread_exit();
}
static inline int
siftr_chkreinject(struct mbuf *m, int dir, struct siftr_stats *ss)
{
if (m_tag_locate(m, PACKET_COOKIE_SIFTR, PACKET_TAG_SIFTR, NULL)
!= NULL) {
if (dir == PFIL_IN)
ss->nskip_in_dejavu++;
else
ss->nskip_out_dejavu++;
return (1);
} else {
struct m_tag *tag = m_tag_alloc(PACKET_COOKIE_SIFTR,
PACKET_TAG_SIFTR, 0, M_NOWAIT);
if (tag == NULL) {
if (dir == PFIL_IN)
ss->nskip_in_malloc++;
else
ss->nskip_out_malloc++;
return (1);
}
m_tag_prepend(m, tag);
}
return (0);
}
static inline struct inpcb *
siftr_findinpcb(int ipver, struct ip *ip, struct mbuf *m, uint16_t sport,
uint16_t dport, int dir, struct siftr_stats *ss)
{
struct inpcb *inp;
if (dir == PFIL_IN)
inp = (ipver == INP_IPV4 ?
in_pcblookup(&V_tcbinfo, ip->ip_src, sport, ip->ip_dst,
dport, INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif)
:
#ifdef SIFTR_IPV6
in6_pcblookup(&V_tcbinfo,
&((struct ip6_hdr *)ip)->ip6_src, sport,
&((struct ip6_hdr *)ip)->ip6_dst, dport, INPLOOKUP_RLOCKPCB,
m->m_pkthdr.rcvif)
#else
NULL
#endif
);
else
inp = (ipver == INP_IPV4 ?
in_pcblookup(&V_tcbinfo, ip->ip_dst, dport, ip->ip_src,
sport, INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif)
:
#ifdef SIFTR_IPV6
in6_pcblookup(&V_tcbinfo,
&((struct ip6_hdr *)ip)->ip6_dst, dport,
&((struct ip6_hdr *)ip)->ip6_src, sport, INPLOOKUP_RLOCKPCB,
m->m_pkthdr.rcvif)
#else
NULL
#endif
);
if (inp == NULL) {
if (dir == PFIL_IN)
ss->nskip_in_inpcb++;
else
ss->nskip_out_inpcb++;
}
return (inp);
}
static inline uint32_t
siftr_get_flowid(struct inpcb *inp, int ipver, uint32_t *phashtype)
{
if (inp->inp_flowid == 0) {
#ifdef SIFTR_IPV6
if (ipver == INP_IPV6) {
return fib6_calc_packet_hash(&inp->in6p_laddr,
&inp->in6p_faddr,
inp->inp_lport,
inp->inp_fport,
IPPROTO_TCP,
phashtype);
} else
#endif
{
return fib4_calc_packet_hash(inp->inp_laddr,
inp->inp_faddr,
inp->inp_lport,
inp->inp_fport,
IPPROTO_TCP,
phashtype);
}
} else {
*phashtype = inp->inp_flowtype;
return inp->inp_flowid;
}
}
static inline void
siftr_siftdata(struct pkt_node *pn, struct inpcb *inp, struct tcpcb *tp,
int ipver, int dir, int inp_locally_locked)
{
pn->ipver = ipver;
pn->lport = inp->inp_lport;
pn->fport = inp->inp_fport;
pn->laddr = inp->inp_inc.inc_ie.ie_dependladdr;
pn->faddr = inp->inp_inc.inc_ie.ie_dependfaddr;
pn->snd_cwnd = tp->snd_cwnd;
pn->snd_wnd = tp->snd_wnd;
pn->rcv_wnd = tp->rcv_wnd;
pn->t_flags2 = tp->t_flags2;
pn->snd_ssthresh = tp->snd_ssthresh;
pn->snd_scale = tp->snd_scale;
pn->rcv_scale = tp->rcv_scale;
pn->conn_state = tp->t_state;
pn->mss = tp->t_maxseg;
pn->srtt = ((uint64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
pn->sack_enabled = (tp->t_flags & TF_SACK_PERMIT) != 0;
pn->t_flags = tp->t_flags;
pn->rto = tp->t_rxtcur * tick;
pn->snd_buf_hiwater = inp->inp_socket->so_snd.sb_hiwat;
pn->snd_buf_cc = sbused(&inp->inp_socket->so_snd);
pn->rcv_buf_hiwater = inp->inp_socket->so_rcv.sb_hiwat;
pn->rcv_buf_cc = sbused(&inp->inp_socket->so_rcv);
pn->sent_inflight_bytes = tp->snd_max - tp->snd_una;
pn->t_segqlen = tp->t_segqlen;
if (inp_locally_locked)
INP_RUNLOCK(inp);
pn->direction = (dir == PFIL_IN ? DIR_IN : DIR_OUT);
microtime(&pn->tval);
TCP_PROBE1(siftr, pn);
}
static pfil_return_t
siftr_chkpkt(struct mbuf **m, struct ifnet *ifp, int flags,
void *ruleset __unused, struct inpcb *inp)
{
struct pkt_node *pn;
struct ip *ip;
struct tcphdr *th;
struct tcpcb *tp;
struct siftr_stats *ss;
unsigned int ip_hl;
int inp_locally_locked, dir;
uint32_t hash_id, hash_type;
struct listhead *counter_list;
struct flow_hash_node *hash_node;
inp_locally_locked = 0;
dir = PFIL_DIR(flags);
ss = DPCPU_PTR(ss);
ip = mtod(*m, struct ip *);
if (ip->ip_p != IPPROTO_TCP)
goto ret;
ip_hl = (ip->ip_hl << 2);
th = (struct tcphdr *)((caddr_t)ip + ip_hl);
if ((siftr_port_filter != 0) &&
(siftr_port_filter != ntohs(th->th_sport)) &&
(siftr_port_filter != ntohs(th->th_dport))) {
goto ret;
}
if (siftr_chkreinject(*m, dir, ss))
goto ret;
if (dir == PFIL_IN)
ss->n_in++;
else
ss->n_out++;
if (!inp) {
inp = siftr_findinpcb(INP_IPV4, ip, *m, th->th_sport,
th->th_dport, dir, ss);
if (inp == NULL)
goto ret;
else
inp_locally_locked = 1;
}
INP_LOCK_ASSERT(inp);
tp = intotcpcb(inp);
if (tp == NULL || tp->t_state < TCPS_ESTABLISHED) {
if (dir == PFIL_IN)
ss->nskip_in_tcpcb++;
else
ss->nskip_out_tcpcb++;
goto inp_unlock;
}
hash_id = siftr_get_flowid(inp, INP_IPV4, &hash_type);
counter_list = counter_hash + (hash_id & siftr_hashmask);
hash_node = siftr_find_flow(counter_list, hash_id);
if (hash_node == NULL) {
struct flow_info info;
inet_ntoa_r(inp->inp_laddr, info.laddr);
inet_ntoa_r(inp->inp_faddr, info.faddr);
info.lport = ntohs(inp->inp_lport);
info.fport = ntohs(inp->inp_fport);
info.key = hash_id;
hash_node = siftr_new_hash_node(info, dir, ss);
}
if (hash_node == NULL) {
goto inp_unlock;
}
pn = malloc(sizeof(struct pkt_node), M_SIFTR_PKTNODE, M_NOWAIT|M_ZERO);
if (pn == NULL) {
if (dir == PFIL_IN)
ss->nskip_in_malloc++;
else
ss->nskip_out_malloc++;
goto inp_unlock;
}
pn->flowid = hash_id;
pn->flowtype = hash_type;
siftr_siftdata(pn, inp, tp, INP_IPV4, dir, inp_locally_locked);
mtx_lock(&siftr_pkt_queue_mtx);
STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes);
mtx_unlock(&siftr_pkt_queue_mtx);
goto ret;
inp_unlock:
if (inp_locally_locked)
INP_RUNLOCK(inp);
ret:
return (PFIL_PASS);
}
#ifdef SIFTR_IPV6
static pfil_return_t
siftr_chkpkt6(struct mbuf **m, struct ifnet *ifp, int flags,
void *ruleset __unused, struct inpcb *inp)
{
struct pkt_node *pn;
struct ip6_hdr *ip6;
struct tcphdr *th;
struct tcpcb *tp;
struct siftr_stats *ss;
unsigned int ip6_hl;
int inp_locally_locked, dir;
uint32_t hash_id, hash_type;
struct listhead *counter_list;
struct flow_hash_node *hash_node;
inp_locally_locked = 0;
dir = PFIL_DIR(flags);
ss = DPCPU_PTR(ss);
ip6 = mtod(*m, struct ip6_hdr *);
if (ip6->ip6_nxt != IPPROTO_TCP)
goto ret6;
ip6_hl = sizeof(struct ip6_hdr);
th = (struct tcphdr *)((caddr_t)ip6 + ip6_hl);
if ((siftr_port_filter != 0) &&
(siftr_port_filter != ntohs(th->th_sport)) &&
(siftr_port_filter != ntohs(th->th_dport))) {
goto ret6;
}
if (siftr_chkreinject(*m, dir, ss))
goto ret6;
if (dir == PFIL_IN)
ss->n_in++;
else
ss->n_out++;
if (!inp) {
inp = siftr_findinpcb(INP_IPV6, (struct ip *)ip6, *m,
th->th_sport, th->th_dport, dir, ss);
if (inp == NULL)
goto ret6;
else
inp_locally_locked = 1;
}
tp = intotcpcb(inp);
if (tp == NULL || tp->t_state < TCPS_ESTABLISHED) {
if (dir == PFIL_IN)
ss->nskip_in_tcpcb++;
else
ss->nskip_out_tcpcb++;
goto inp_unlock6;
}
hash_id = siftr_get_flowid(inp, INP_IPV6, &hash_type);
counter_list = counter_hash + (hash_id & siftr_hashmask);
hash_node = siftr_find_flow(counter_list, hash_id);
if (!hash_node) {
struct flow_info info;
ip6_sprintf(info.laddr, &inp->in6p_laddr);
ip6_sprintf(info.faddr, &inp->in6p_faddr);
info.lport = ntohs(inp->inp_lport);
info.fport = ntohs(inp->inp_fport);
info.key = hash_id;
hash_node = siftr_new_hash_node(info, dir, ss);
}
if (!hash_node) {
goto inp_unlock6;
}
pn = malloc(sizeof(struct pkt_node), M_SIFTR_PKTNODE, M_NOWAIT|M_ZERO);
if (pn == NULL) {
if (dir == PFIL_IN)
ss->nskip_in_malloc++;
else
ss->nskip_out_malloc++;
goto inp_unlock6;
}
pn->flowid = hash_id;
pn->flowtype = hash_type;
siftr_siftdata(pn, inp, tp, INP_IPV6, dir, inp_locally_locked);
mtx_lock(&siftr_pkt_queue_mtx);
STAILQ_INSERT_TAIL(&pkt_queue, pn, nodes);
mtx_unlock(&siftr_pkt_queue_mtx);
goto ret6;
inp_unlock6:
if (inp_locally_locked)
INP_RUNLOCK(inp);
ret6:
return (PFIL_PASS);
}
#endif
VNET_DEFINE_STATIC(pfil_hook_t, siftr_inet_hook);
#define V_siftr_inet_hook VNET(siftr_inet_hook)
#ifdef SIFTR_IPV6
VNET_DEFINE_STATIC(pfil_hook_t, siftr_inet6_hook);
#define V_siftr_inet6_hook VNET(siftr_inet6_hook)
#endif
static int
siftr_pfil(int action)
{
struct pfil_hook_args pha = {
.pa_version = PFIL_VERSION,
.pa_flags = PFIL_IN | PFIL_OUT,
.pa_modname = "siftr",
.pa_rulname = "default",
};
struct pfil_link_args pla = {
.pa_version = PFIL_VERSION,
.pa_flags = PFIL_IN | PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR,
};
VNET_ITERATOR_DECL(vnet_iter);
VNET_LIST_RLOCK();
VNET_FOREACH(vnet_iter) {
CURVNET_SET(vnet_iter);
if (action == HOOK) {
pha.pa_mbuf_chk = siftr_chkpkt;
pha.pa_type = PFIL_TYPE_IP4;
V_siftr_inet_hook = pfil_add_hook(&pha);
pla.pa_hook = V_siftr_inet_hook;
pla.pa_head = V_inet_pfil_head;
(void)pfil_link(&pla);
#ifdef SIFTR_IPV6
pha.pa_mbuf_chk = siftr_chkpkt6;
pha.pa_type = PFIL_TYPE_IP6;
V_siftr_inet6_hook = pfil_add_hook(&pha);
pla.pa_hook = V_siftr_inet6_hook;
pla.pa_head = V_inet6_pfil_head;
(void)pfil_link(&pla);
#endif
} else if (action == UNHOOK) {
pfil_remove_hook(V_siftr_inet_hook);
#ifdef SIFTR_IPV6
pfil_remove_hook(V_siftr_inet6_hook);
#endif
}
CURVNET_RESTORE();
}
VNET_LIST_RUNLOCK();
return (0);
}
static int
siftr_sysctl_logfile_name_handler(SYSCTL_HANDLER_ARGS)
{
struct alq *new_alq;
int error;
error = sysctl_handle_string(oidp, arg1, arg2, req);
if (error != 0 || req->newptr == NULL ||
strncmp(siftr_logfile, arg1, arg2) == 0)
goto done;
error = alq_open(&new_alq, arg1, curthread->td_ucred,
SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0);
if (error != 0)
goto done;
if (siftr_alq == NULL) {
alq_close(new_alq);
} else {
alq_close(siftr_alq);
siftr_alq = new_alq;
}
strlcpy(siftr_logfile, arg1, arg2);
done:
return (error);
}
static int
siftr_manage_ops(uint8_t action)
{
struct siftr_stats totalss;
struct timeval tval;
struct flow_hash_node *counter, *tmp_counter;
struct sbuf *s;
int i, error;
uint32_t bytes_to_write, total_skipped_pkts;
error = 0;
total_skipped_pkts = 0;
if ((s = sbuf_new(NULL, NULL, 200, SBUF_AUTOEXTEND)) == NULL)
return (-1);
if (action == SIFTR_ENABLE && siftr_pkt_manager_thr == NULL) {
alq_open(&siftr_alq, siftr_logfile, curthread->td_ucred,
SIFTR_LOG_FILE_MODE, SIFTR_ALQ_BUFLEN, 0);
STAILQ_INIT(&pkt_queue);
DPCPU_ZERO(ss);
siftr_exit_pkt_manager_thread = 0;
kthread_add(&siftr_pkt_manager_thread, NULL, NULL,
&siftr_pkt_manager_thr, RFNOWAIT, 0,
"siftr_pkt_manager_thr");
siftr_pfil(HOOK);
microtime(&tval);
sbuf_printf(s,
"enable_time_secs=%jd\tenable_time_usecs=%06ld\t"
"siftrver=%s\tsysname=%s\tsysver=%u\tipmode=%u\n",
(intmax_t)tval.tv_sec, tval.tv_usec, MODVERSION_STR,
SYS_NAME, __FreeBSD_version, SIFTR_IPMODE);
sbuf_finish(s);
alq_writen(siftr_alq, sbuf_data(s), sbuf_len(s), ALQ_WAITOK);
} else if (action == SIFTR_DISABLE && siftr_pkt_manager_thr != NULL) {
siftr_pfil(UNHOOK);
mtx_lock(&siftr_pkt_mgr_mtx);
siftr_exit_pkt_manager_thread = 1;
wakeup(&wait_for_pkt);
mtx_sleep(siftr_pkt_manager_thr, &siftr_pkt_mgr_mtx, PWAIT,
"thrwait", 0);
siftr_pkt_manager_thr = NULL;
mtx_unlock(&siftr_pkt_mgr_mtx);
totalss.n_in = DPCPU_VARSUM(ss, n_in);
totalss.n_out = DPCPU_VARSUM(ss, n_out);
totalss.nskip_in_malloc = DPCPU_VARSUM(ss, nskip_in_malloc);
totalss.nskip_out_malloc = DPCPU_VARSUM(ss, nskip_out_malloc);
totalss.nskip_in_tcpcb = DPCPU_VARSUM(ss, nskip_in_tcpcb);
totalss.nskip_out_tcpcb = DPCPU_VARSUM(ss, nskip_out_tcpcb);
totalss.nskip_in_inpcb = DPCPU_VARSUM(ss, nskip_in_inpcb);
totalss.nskip_out_inpcb = DPCPU_VARSUM(ss, nskip_out_inpcb);
total_skipped_pkts = totalss.nskip_in_malloc +
totalss.nskip_out_malloc + totalss.nskip_in_tcpcb +
totalss.nskip_out_tcpcb + totalss.nskip_in_inpcb +
totalss.nskip_out_inpcb;
microtime(&tval);
sbuf_printf(s,
"disable_time_secs=%jd\tdisable_time_usecs=%06ld\t"
"num_inbound_tcp_pkts=%ju\tnum_outbound_tcp_pkts=%ju\t"
"total_tcp_pkts=%ju\tnum_inbound_skipped_pkts_malloc=%u\t"
"num_outbound_skipped_pkts_malloc=%u\t"
"num_inbound_skipped_pkts_tcpcb=%u\t"
"num_outbound_skipped_pkts_tcpcb=%u\t"
"num_inbound_skipped_pkts_inpcb=%u\t"
"num_outbound_skipped_pkts_inpcb=%u\t"
"total_skipped_tcp_pkts=%u\tflow_list=",
(intmax_t)tval.tv_sec,
tval.tv_usec,
(uintmax_t)totalss.n_in,
(uintmax_t)totalss.n_out,
(uintmax_t)(totalss.n_in + totalss.n_out),
totalss.nskip_in_malloc,
totalss.nskip_out_malloc,
totalss.nskip_in_tcpcb,
totalss.nskip_out_tcpcb,
totalss.nskip_in_inpcb,
totalss.nskip_out_inpcb,
total_skipped_pkts);
for (i = 0; i <= siftr_hashmask; i++) {
LIST_FOREACH_SAFE(counter, counter_hash + i, nodes,
tmp_counter) {
sbuf_printf(s, "%s;%hu-%s;%hu,",
counter->const_info.laddr,
counter->const_info.lport,
counter->const_info.faddr,
counter->const_info.fport);
free(counter, M_SIFTR_HASHNODE);
}
LIST_INIT(counter_hash + i);
}
sbuf_printf(s, "\n");
sbuf_finish(s);
i = 0;
do {
bytes_to_write = min(SIFTR_ALQ_BUFLEN, sbuf_len(s)-i);
alq_writen(siftr_alq, sbuf_data(s)+i, bytes_to_write, ALQ_WAITOK);
i += bytes_to_write;
} while (i < sbuf_len(s));
alq_close(siftr_alq);
siftr_alq = NULL;
} else
error = EINVAL;
sbuf_delete(s);
return (error);
}
static int
siftr_sysctl_enabled_handler(SYSCTL_HANDLER_ARGS)
{
int error;
uint32_t new;
new = siftr_enabled;
error = sysctl_handle_int(oidp, &new, 0, req);
if (error == 0 && req->newptr != NULL) {
if (new > 1)
return (EINVAL);
else if (new != siftr_enabled) {
if ((error = siftr_manage_ops(new)) == 0) {
siftr_enabled = new;
} else {
siftr_manage_ops(SIFTR_DISABLE);
}
}
}
return (error);
}
static void
siftr_shutdown_handler(void *arg, int howto)
{
if ((howto & RB_NOSYNC) != 0 || SCHEDULER_STOPPED())
return;
if (siftr_enabled == 1) {
siftr_manage_ops(SIFTR_DISABLE);
}
}
static int
deinit_siftr(void)
{
EVENTHANDLER_DEREGISTER(shutdown_pre_sync, siftr_shutdown_tag);
siftr_manage_ops(SIFTR_DISABLE);
hashdestroy(counter_hash, M_SIFTR, siftr_hashmask);
mtx_destroy(&siftr_pkt_queue_mtx);
mtx_destroy(&siftr_pkt_mgr_mtx);
return (0);
}
static int
init_siftr(void)
{
siftr_shutdown_tag = EVENTHANDLER_REGISTER(shutdown_pre_sync,
siftr_shutdown_handler, NULL, SHUTDOWN_PRI_FIRST);
counter_hash = hashinit(SIFTR_EXPECTED_MAX_TCP_FLOWS, M_SIFTR,
&siftr_hashmask);
mtx_init(&siftr_pkt_queue_mtx, "siftr_pkt_queue_mtx", NULL, MTX_DEF);
mtx_init(&siftr_pkt_mgr_mtx, "siftr_pkt_mgr_mtx", NULL, MTX_DEF);
uprintf("\nStatistical Information For TCP Research (SIFTR) %s\n"
" http://caia.swin.edu.au/urp/newtcp\n\n",
MODVERSION_STR);
return (0);
}
static int
siftr_load_handler(module_t mod, int what, void *arg)
{
int ret;
switch (what) {
case MOD_LOAD:
ret = init_siftr();
break;
case MOD_QUIESCE:
case MOD_SHUTDOWN:
ret = deinit_siftr();
break;
case MOD_UNLOAD:
ret = 0;
break;
default:
ret = EINVAL;
break;
}
return (ret);
}
static moduledata_t siftr_mod = {
.name = "siftr",
.evhand = siftr_load_handler,
};
DECLARE_MODULE(siftr, siftr_mod, SI_SUB_LAST, SI_ORDER_ANY);
MODULE_DEPEND(siftr, alq, 1, 1, 1);
MODULE_VERSION(siftr, MODVERSION);