FreeBSD kernel IPv4 code
siftr.c File Reference
#include <sys/cdefs.h>
#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/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 <netinet/in.h>
#include <netinet/in_kdtrace.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>
#include <machine/in_cksum.h>
Include dependency graph for siftr.c:

Go to the source code of this file.

Data Structures

struct  pkt_node
 
struct  flow_hash_node
 
struct  siftr_stats
 

Macros

#define V_MAJOR   1
 
#define V_BACKBREAK   2
 
#define V_BACKCOMPAT   4
 
#define MODVERSION   __CONCAT(V_MAJOR, __CONCAT(V_BACKBREAK, V_BACKCOMPAT))
 
#define MODVERSION_STR
 
#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   200
 
#define SIFTR_ALQ_BUFLEN   (1000*MAX_LOG_MSG_LEN)
 
#define FLOW_KEY_LEN   13
 
#define SIFTR_IPMODE   4
 
#define UPPER_SHORT(X)   (((X) & 0xFFFF0000) >> 16)
 
#define LOWER_SHORT(X)   ((X) & 0x0000FFFF)
 
#define FIRST_OCTET(X)   (((X) & 0xFF000000) >> 24)
 
#define SECOND_OCTET(X)   (((X) & 0x00FF0000) >> 16)
 
#define THIRD_OCTET(X)   (((X) & 0x0000FF00) >> 8)
 
#define FOURTH_OCTET(X)   ((X) & 0x000000FF)
 
#define V_siftr_inet_hook   VNET(siftr_inet_hook)
 

Functions

 __FBSDID ("$FreeBSD$")
 
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")
 
 DPCPU_DEFINE_STATIC (struct siftr_stats, ss)
 
 STAILQ_HEAD (pkthead, pkt_node)
 
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_UINT (_net_inet_siftr, OID_AUTO, genhashes, CTLFLAG_RW, &siftr_generate_hashes, 0, "enable packet hash generation")
 
 SYSCTL_U16 (_net_inet_siftr, OID_AUTO, port_filter, CTLFLAG_RW, &siftr_port_filter, 0, "enable packet filter on a TCP port")
 
static void siftr_process_pkt (struct pkt_node *pkt_node)
 
static void siftr_pkt_manager_thread (void *arg)
 
static uint32_t hash_pkt (struct mbuf *m, uint32_t offset)
 
static int siftr_chkreinject (struct mbuf *m, int dir, struct siftr_stats *ss)
 
static struct inpcbsiftr_findinpcb (int ipver, struct ip *ip, struct mbuf *m, uint16_t sport, uint16_t dport, int dir, struct siftr_stats *ss)
 
static void siftr_siftdata (struct pkt_node *pn, struct inpcb *inp, struct tcpcb *tp, int ipver, int dir, int inp_locally_locked)
 
static pfil_return_t siftr_chkpkt (struct mbuf **m, struct ifnet *ifp, int flags, void *ruleset __unused, struct inpcb *inp)
 
 VNET_DEFINE_STATIC (pfil_hook_t, siftr_inet_hook)
 
static int siftr_pfil (int action)
 
static int siftr_manage_ops (uint8_t action)
 
static void siftr_shutdown_handler (void *arg)
 
static int deinit_siftr (void)
 
static int init_siftr (void)
 
static int siftr_load_handler (module_t mod, int what, void *arg)
 
 DECLARE_MODULE (siftr, siftr_mod, SI_SUB_LAST, SI_ORDER_ANY)
 
 MODULE_DEPEND (siftr, alq, 1, 1, 1)
 
 MODULE_VERSION (siftr, MODVERSION)
 

Variables

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 unsigned int siftr_generate_hashes = 0
 
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
 
static moduledata_t siftr_mod
 

Macro Definition Documentation

◆ FIRST_OCTET

#define FIRST_OCTET (   X)    (((X) & 0xFF000000) >> 24)

Definition at line 160 of file siftr.c.

◆ FLOW_KEY_LEN

#define FLOW_KEY_LEN   13

Definition at line 147 of file siftr.c.

◆ FOURTH_OCTET

#define FOURTH_OCTET (   X)    ((X) & 0x000000FF)

Definition at line 163 of file siftr.c.

◆ HOOK

#define HOOK   0

Definition at line 121 of file siftr.c.

◆ LOWER_SHORT

#define LOWER_SHORT (   X)    ((X) & 0x0000FFFF)

Definition at line 158 of file siftr.c.

◆ MAX_LOG_MSG_LEN

#define MAX_LOG_MSG_LEN   200

Definition at line 135 of file siftr.c.

◆ MODVERSION

#define MODVERSION   __CONCAT(V_MAJOR, __CONCAT(V_BACKBREAK, V_BACKCOMPAT))

Definition at line 117 of file siftr.c.

◆ MODVERSION_STR

#define MODVERSION_STR
Value:
__XSTRING(V_MAJOR) "." __XSTRING(V_BACKBREAK) "." \
__XSTRING(V_BACKCOMPAT)
#define V_BACKCOMPAT
Definition: siftr.c:116
#define V_MAJOR
Definition: siftr.c:114
#define V_BACKBREAK
Definition: siftr.c:115

Definition at line 118 of file siftr.c.

◆ PACKET_COOKIE_SIFTR

#define PACKET_COOKIE_SIFTR   21749576

Definition at line 126 of file siftr.c.

◆ PACKET_TAG_SIFTR

#define PACKET_TAG_SIFTR   100

Definition at line 125 of file siftr.c.

◆ SECOND_OCTET

#define SECOND_OCTET (   X)    (((X) & 0x00FF0000) >> 16)

Definition at line 161 of file siftr.c.

◆ SIFTR_ALQ_BUFLEN

#define SIFTR_ALQ_BUFLEN   (1000*MAX_LOG_MSG_LEN)

Definition at line 137 of file siftr.c.

◆ SIFTR_DISABLE

#define SIFTR_DISABLE   0

Definition at line 128 of file siftr.c.

◆ SIFTR_ENABLE

#define SIFTR_ENABLE   1

Definition at line 129 of file siftr.c.

◆ SIFTR_EXPECTED_MAX_TCP_FLOWS

#define SIFTR_EXPECTED_MAX_TCP_FLOWS   65536

Definition at line 123 of file siftr.c.

◆ SIFTR_IPMODE

#define SIFTR_IPMODE   4

Definition at line 153 of file siftr.c.

◆ SIFTR_LOG_FILE_MODE

#define SIFTR_LOG_FILE_MODE   0644

Definition at line 127 of file siftr.c.

◆ SYS_NAME

#define SYS_NAME   "FreeBSD"

Definition at line 124 of file siftr.c.

◆ THIRD_OCTET

#define THIRD_OCTET (   X)    (((X) & 0x0000FF00) >> 8)

Definition at line 162 of file siftr.c.

◆ UNHOOK

#define UNHOOK   1

Definition at line 122 of file siftr.c.

◆ UPPER_SHORT

#define UPPER_SHORT (   X)    (((X) & 0xFFFF0000) >> 16)

Definition at line 157 of file siftr.c.

◆ V_BACKBREAK

#define V_BACKBREAK   2

Definition at line 115 of file siftr.c.

◆ V_BACKCOMPAT

#define V_BACKCOMPAT   4

Definition at line 116 of file siftr.c.

◆ V_MAJOR

#define V_MAJOR   1

Definition at line 114 of file siftr.c.

◆ V_siftr_inet_hook

#define V_siftr_inet_hook   VNET(siftr_inet_hook)

Definition at line 1137 of file siftr.c.

Function Documentation

◆ __FBSDID()

__FBSDID ( "$FreeBSD$"  )

◆ DECLARE_MODULE()

DECLARE_MODULE ( siftr  ,
siftr_mod  ,
SI_SUB_LAST  ,
SI_ORDER_ANY   
)

◆ deinit_siftr()

static int deinit_siftr ( void  )
static

Definition at line 1510 of file siftr.c.

References SIFTR_DISABLE, siftr_hashmask, and siftr_manage_ops().

Referenced by siftr_load_handler().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ DPCPU_DEFINE_STATIC()

DPCPU_DEFINE_STATIC ( struct siftr_stats  ,
ss   
)

◆ hash_pkt()

static uint32_t hash_pkt ( struct mbuf *  m,
uint32_t  offset 
)
static

Definition at line 639 of file siftr.c.

References pkt_node::hash.

Referenced by siftr_chkpkt().

Here is the caller graph for this function:

◆ init_siftr()

static int init_siftr ( void  )
static

Definition at line 1525 of file siftr.c.

References MODVERSION_STR, SIFTR_EXPECTED_MAX_TCP_FLOWS, siftr_hashmask, and siftr_shutdown_handler().

Referenced by siftr_load_handler().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MALLOC_DEFINE() [1/3]

static MALLOC_DEFINE ( M_SIFTR  ,
"siftr"  ,
"dynamic memory used by SIFTR"   
)
static

◆ MALLOC_DEFINE() [2/3]

static MALLOC_DEFINE ( M_SIFTR_HASHNODE  ,
"siftr_hashnode"  ,
"SIFTR flow_hash_node struct"   
)
static

◆ MALLOC_DEFINE() [3/3]

static MALLOC_DEFINE ( M_SIFTR_PKTNODE  ,
"siftr_pktnode"  ,
"SIFTR pkt_node struct"   
)
static

◆ MODULE_DEPEND()

MODULE_DEPEND ( siftr  ,
alq  ,
,
,
 
)

◆ MODULE_VERSION()

MODULE_VERSION ( siftr  ,
MODVERSION   
)

◆ siftr_chkpkt()

static pfil_return_t siftr_chkpkt ( struct mbuf **  m,
struct ifnet *  ifp,
int  flags,
void *ruleset  __unused,
struct inpcb inp 
)
static

◆ siftr_chkreinject()

static int siftr_chkreinject ( struct mbuf *  m,
int  dir,
struct siftr_stats ss 
)
inlinestatic

Definition at line 674 of file siftr.c.

References siftr_stats::nskip_in_dejavu, siftr_stats::nskip_in_malloc, siftr_stats::nskip_out_dejavu, siftr_stats::nskip_out_malloc, PACKET_COOKIE_SIFTR, and PACKET_TAG_SIFTR.

Referenced by siftr_chkpkt().

Here is the caller graph for this function:

◆ siftr_findinpcb()

static struct inpcb * siftr_findinpcb ( int  ipver,
struct ip ip,
struct mbuf *  m,
uint16_t  sport,
uint16_t  dport,
int  dir,
struct siftr_stats ss 
)
inlinestatic

Definition at line 707 of file siftr.c.

References in_pcblookup(), INP_INFO_WUNLOCK_ASSERT, INP_IPV4, INPLOOKUP_RLOCKPCB, ip::ip_dst, siftr_stats::nskip_in_inpcb, siftr_stats::nskip_out_inpcb, and V_tcbinfo.

Referenced by siftr_chkpkt().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ siftr_load_handler()

static int siftr_load_handler ( module_t  mod,
int  what,
void *  arg 
)
static

Definition at line 1557 of file siftr.c.

References deinit_siftr(), and init_siftr().

Here is the call graph for this function:

◆ siftr_manage_ops()

◆ siftr_pfil()

static int siftr_pfil ( int  action)
static

Definition at line 1143 of file siftr.c.

References HOOK, siftr_chkpkt(), UNHOOK, V_inet_pfil_head, and V_siftr_inet_hook.

Referenced by siftr_manage_ops().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ siftr_pkt_manager_thread()

static void siftr_pkt_manager_thread ( void *  arg)
static

Definition at line 562 of file siftr.c.

References siftr_exit_pkt_manager_thread, siftr_process_pkt(), and STAILQ_HEAD().

Referenced by siftr_manage_ops().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ siftr_process_pkt()

◆ siftr_shutdown_handler()

static void siftr_shutdown_handler ( void *  arg)
static

Definition at line 1499 of file siftr.c.

References SIFTR_DISABLE, siftr_enabled, and siftr_manage_ops().

Referenced by init_siftr().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ siftr_siftdata()

◆ siftr_sysctl_enabled_handler()

static int siftr_sysctl_enabled_handler ( SYSCTL_HANDLER_ARGS  )
static

Definition at line 1476 of file siftr.c.

References SIFTR_DISABLE, siftr_enabled, and siftr_manage_ops().

Here is the call graph for this function:

◆ siftr_sysctl_logfile_name_handler()

static int siftr_sysctl_logfile_name_handler ( SYSCTL_HANDLER_ARGS  )
static

Definition at line 1193 of file siftr.c.

References SIFTR_ALQ_BUFLEN, SIFTR_LOG_FILE_MODE, and siftr_logfile.

◆ STAILQ_HEAD()

STAILQ_HEAD ( pkthead  ,
pkt_node   
)

Definition at line 285 of file siftr.c.

Referenced by siftr_pkt_manager_thread().

Here is the caller graph for this function:

◆ SYSCTL_DECL()

SYSCTL_DECL ( _net_inet_siftr  )

◆ SYSCTL_NODE()

SYSCTL_NODE ( _net_inet  ,
OID_AUTO  ,
siftr  ,
CTLFLAG_RW|  CTLFLAG_MPSAFE,
NULL  ,
"siftr related settings"   
)

◆ SYSCTL_PROC() [1/2]

SYSCTL_PROC ( _net_inet_siftr  ,
OID_AUTO  ,
enabled  ,
CTLTYPE_UINT|CTLFLAG_RW|  CTLFLAG_NEEDGIANT,
siftr_enabled,
,
siftr_sysctl_enabled_handler,
"IU"  ,
"switch siftr module operations on/off"   
)

◆ SYSCTL_PROC() [2/2]

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_U16()

SYSCTL_U16 ( _net_inet_siftr  ,
OID_AUTO  ,
port_filter  ,
CTLFLAG_RW  ,
siftr_port_filter,
,
"enable packet filter on a TCP port"   
)

◆ SYSCTL_UINT() [1/2]

SYSCTL_UINT ( _net_inet_siftr  ,
OID_AUTO  ,
genhashes  ,
CTLFLAG_RW  ,
siftr_generate_hashes,
,
"enable packet hash generation"   
)

◆ SYSCTL_UINT() [2/2]

SYSCTL_UINT ( _net_inet_siftr  ,
OID_AUTO  ,
ppl  ,
CTLFLAG_RW  ,
siftr_pkts_per_log,
,
"number of packets between generating a log message"   
)

◆ VNET_DEFINE_STATIC()

VNET_DEFINE_STATIC ( pfil_hook_t  ,
siftr_inet_hook   
)

Variable Documentation

◆ siftr_enabled

unsigned int siftr_enabled = 0
static

Definition at line 277 of file siftr.c.

Referenced by siftr_shutdown_handler(), and siftr_sysctl_enabled_handler().

◆ siftr_exit_pkt_manager_thread

volatile unsigned int siftr_exit_pkt_manager_thread = 0
static

Definition at line 276 of file siftr.c.

Referenced by siftr_manage_ops(), and siftr_pkt_manager_thread().

◆ siftr_generate_hashes

unsigned int siftr_generate_hashes = 0
static

Definition at line 279 of file siftr.c.

Referenced by siftr_chkpkt().

◆ siftr_hashmask

u_long siftr_hashmask
static

Definition at line 284 of file siftr.c.

Referenced by deinit_siftr(), init_siftr(), siftr_manage_ops(), and siftr_process_pkt().

◆ siftr_logfile

char siftr_logfile[PATH_MAX] = "/var/log/siftr.log"
static

Definition at line 282 of file siftr.c.

Referenced by siftr_manage_ops(), and siftr_sysctl_logfile_name_handler().

◆ siftr_logfile_shadow

char siftr_logfile_shadow[PATH_MAX] = "/var/log/siftr.log"
static

Definition at line 283 of file siftr.c.

◆ siftr_mod

moduledata_t siftr_mod
static
Initial value:
= {
.name = "siftr",
.evhand = siftr_load_handler,
}
static int siftr_load_handler(module_t mod, int what, void *arg)
Definition: siftr.c:1557

Definition at line 1583 of file siftr.c.

◆ siftr_pkts_per_log

unsigned int siftr_pkts_per_log = 1
static

Definition at line 278 of file siftr.c.

Referenced by siftr_process_pkt().

◆ siftr_port_filter

uint16_t siftr_port_filter = 0
static

Definition at line 280 of file siftr.c.

Referenced by siftr_chkpkt().