| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| #include "config.h" |
|
|
| #ifdef HAVE_PFRING |
| #include <pfring.h> |
| #endif |
|
|
| |
| #define TIMEOUT 60 |
| #define BUCKET_SIZE 65537 |
| #define SNAPLENGTH 1600 |
| #define PKT_MAXPAY 255 |
| #define MAX_BYTE_CHECK 500000 |
| #define MAX_PKT_CHECK 10 |
| #define TCP_TIMEOUT 300 |
| #define UDP_TIMEOUT 60 |
| #define ICMP_TIMEOUT 60 |
| #define OTHER_TIMEOUT 300 |
|
|
| #define ETHERNET_TYPE_IP 0x0800 |
| #define ETHERNET_TYPE_IPV6 0x86dd |
|
|
| #define ETHERNET_TYPE_8021Q 0x8100 |
| #define ETHERNET_TYPE_802Q1MT 0x9100 |
| #define ETHERNET_TYPE_802Q1MT2 0x9200 |
| #define ETHERNET_TYPE_802Q1MT3 0x9300 |
| #define ETHERNET_TYPE_8021AD 0x88a8 |
|
|
| #define IP_PROTO_ICMP 1 |
| #define IP_PROTO_TCP 6 |
| #define IP_PROTO_UDP 17 |
| #define IP_PROTO_IP6 41 |
| #define IP6_PROTO_ICMP 58 |
|
|
| #define SLL_HDR_LEN 16 |
| #define LOOPBACK_HDR_LEN 4 |
| #define IP4_HEADER_LEN 20 |
| #define IP6_HEADER_LEN 40 |
| #define TCP_HEADER_LEN 20 |
| #define UDP_HEADER_LEN 8 |
| #define MAC_ADDR_LEN 6 |
| #define ETHERNET_HEADER_LEN 14 |
| #define ETHERNET_8021Q_HEADER_LEN 18 |
| #define ETHERNET_802Q1MT_HEADER_LEN 22 |
|
|
| #define IP_PROTO_TCP 6 |
| #define IP_PROTO_UDP 17 |
| #define IP_PROTO_IP6 41 |
| #define IP_PROTO_IP4 94 |
|
|
| #define TF_FIN 0x01 |
| #define TF_SYN 0x02 |
| #define TF_RST 0x04 |
| #define TF_PUSH 0x08 |
| #define TF_ACK 0x10 |
| #define TF_URG 0x20 |
| #define TF_ECE 0x40 |
| #define TF_CWR 0x80 |
|
|
| #define SUCCESS 0 |
| #define ERROR 1 |
| #define STDBUF 1024 |
|
|
| #define BPFF "port 53" |
|
|
| #if defined(__FreeBSD__) || defined(__APPLE__) |
| #define s6_addr32 __u6_addr.__u6_addr32 |
| #endif |
|
|
| #ifndef HOST_NAME_MAX |
| #if defined(__APPLE__) |
| #define HOST_NAME_MAX 255 |
| #else |
| #define HOST_NAME_MAX 64 |
| #endif |
| #endif |
|
|
| |
|
|
| |
| |
| |
|
|
| typedef struct _ether_header { |
| u_char ether_dst[6]; |
| u_char ether_src[6]; |
|
|
| union |
| { |
| struct etht |
| { |
| u_short ether_type; |
| } etht; |
|
|
| struct qt |
| { |
| u_short eth_t_8021; |
| u_short eth_t_8_vid; |
| u_short eth_t_8_type; |
| } qt; |
|
|
| struct qot |
| { |
| u_short eth_t_80212; |
| u_short eth_t_82_mvid; |
| u_short eth_t_82_8021; |
| u_short eth_t_82_vid; |
| u_short eth_t_82_type; |
| } qot; |
| } vlantag; |
|
|
| #define eth_ip_type vlantag.etht.ether_type |
|
|
| #define eth_8_type vlantag.qt.eth_t_8021 |
| #define eth_8_vid vlantag.qt.eth_t_8_vid |
| #define eth_8_ip_type vlantag.qt.eth_t_8_type |
|
|
| #define eth_82_type vlantag.qot.eth_t_80212 |
| #define eth_82_mvid vlantag.qot.eth_t_82_mvid |
| #define eth_82_8021 vlantag.qot.eth_t_82_8021 |
| #define eth_82_vid vlantag.qot.eth_t_82_vid |
| #define eth_82_ip_type vlantag.qot.eth_t_82_type |
|
|
| } ether_header; |
|
|
| typedef struct _arphdr { |
| uint16_t ar_hrd; |
| uint16_t ar_pro; |
| uint8_t ar_hln; |
| uint8_t ar_pln; |
| uint16_t ar_op; |
| } arphdr; |
|
|
| typedef struct _ether_arp { |
| arphdr ea_hdr; |
| uint8_t arp_sha[MAC_ADDR_LEN]; |
| uint8_t arp_spa[4]; |
| uint8_t arp_tha[MAC_ADDR_LEN]; |
| uint8_t arp_tpa[4]; |
| } ether_arp; |
|
|
| |
| |
| |
|
|
| typedef struct _ip4_header { |
| uint8_t ip_vhl; |
| uint8_t ip_tos; |
| uint16_t ip_len; |
| uint16_t ip_id; |
| uint16_t ip_off; |
| uint8_t ip_ttl; |
| uint8_t ip_p; |
| uint16_t ip_csum; |
| uint32_t ip_src; |
| uint32_t ip_dst; |
| } ip4_header; |
|
|
| #define IP_RF 0x8000 |
| #define IP_DF 0x4000 |
| #define IP_MF 0x2000 |
| #define IP_OFFMASK 0x1fff |
| #define IP_HL(ip4_header) (((ip4_header)->ip_vhl) & 0x0f) |
| #define IP_V(ip4_header) (((ip4_header)->ip_vhl) >> 4) |
|
|
| |
| |
| |
|
|
| typedef struct _ip6_header { |
| uint32_t vcl; |
| uint16_t len; |
| uint8_t next; |
| |
| |
| uint8_t hop_lmt; |
| struct in6_addr ip_src; |
| struct in6_addr ip_dst; |
| } ip6_header; |
|
|
| |
| |
| |
|
|
| typedef struct _tcp_header { |
| uint16_t src_port; |
| uint16_t dst_port; |
| uint32_t t_seq; |
| uint32_t t_ack; |
| uint8_t t_offx2; |
| uint8_t t_flags; |
| uint16_t t_win; |
| uint16_t t_csum; |
| uint16_t t_urgp; |
| } tcp_header; |
|
|
| #define TCP_OFFSET(tcp_header) (((tcp_header)->t_offx2 & 0xf0) >> 4) |
| #define TCP_X2(tcp_header) ((tcp_header)->t_offx2 & 0x0f) |
| #define TCP_ISFLAGSET(tcp_header, flags) (((tcp_header)->t_flags & (flags)) == (flags)) |
|
|
| |
| |
| |
|
|
| typedef struct _udp_header { |
| uint16_t src_port; |
| uint16_t dst_port; |
| uint16_t len; |
| uint16_t csum; |
| } udp_header; |
|
|
| |
| |
| |
|
|
| typedef struct _connection { |
| struct _connection *prev; |
| struct _connection *next; |
| time_t start_time; |
| time_t last_pkt_time; |
| uint64_t cxid; |
| uint8_t reversed; |
| uint32_t af; |
| uint8_t proto; |
| struct in6_addr s_ip; |
| struct in6_addr d_ip; |
| uint16_t s_port; |
| uint16_t d_port; |
| uint64_t s_total_pkts; |
| uint64_t s_total_bytes; |
| uint64_t d_total_pkts; |
| uint64_t d_total_bytes; |
| uint8_t s_tcpFlags; |
| uint8_t d_tcpFlags; |
| uint8_t check; |
| uint16_t plid; |
| } connection; |
| #define CXT_DONT_CHECK_SERVER 0x01 |
| #define CXT_DONT_CHECK_CLIENT 0x02 |
| #define CXT_SERVICE_DONT_CHECK 0x04 |
| #define CXT_CLIENT_DONT_CHECK 0x08 |
| #define CXT_SERVICE_UNKNOWN_SET 0x10 |
| #define CXT_CLIENT_UNKNOWN_SET 0x20 |
|
|
| #define ISSET_CXT_DONT_CHECK_CLIENT(pi) (pi->cxt->check & CXT_DONT_CHECK_CLIENT) |
| #define ISSET_CXT_DONT_CHECK_SERVER(pi) (pi->cxt->check & CXT_DONT_CHECK_SERVER) |
| #define ISSET_DONT_CHECK_SERVICE(pi) (pi->cxt->check & CXT_SERVICE_DONT_CHECK) |
| #define ISSET_DONT_CHECK_CLIENT(pi) (pi->cxt->check & CXT_CLIENT_DONT_CHECK) |
| #define ISSET_SERVICE_UNKNOWN(pi) (pi->cxt->check & CXT_SERVICE_UNKNOWN_SET) |
| #define ISSET_CLIENT_UNKNOWN(pi) (pi->cxt->check & CXT_CLIENT_UNKNOWN_SET) |
|
|
| #ifdef BSD_DERIVED |
| |
| #define IP4ADDR(ip) (ip)->__u6_addr.__u6_addr32[0] |
|
|
| #define CMP_ADDR6(a1,a2) \ |
| (((a1)->__u6_addr.__u6_addr32[3] == (a2)->__u6_addr.__u6_addr32[3] && \ |
| (a1)->__u6_addr.__u6_addr32[2] == (a2)->__u6_addr.__u6_addr32[2] && \ |
| (a1)->__u6_addr.__u6_addr32[1] == (a2)->__u6_addr.__u6_addr32[1] && \ |
| (a1)->__u6_addr.__u6_addr32[0] == (a2)->__u6_addr.__u6_addr32[0])) |
|
|
| |
| #define CMP_ADDR4(a1,a2) \ |
| (((a1)->__u6_addr.__u6_addr32[0] == (a2))) |
| #define CMP_ADDRA(a1,a2) \ |
| (((a1)->__u6_addr.__u6_addr32[0] == (a2)->__u6_addr.__u6_addr32[0])) |
|
|
| #define CMP_PORT(p1,p2) \ |
| ((p1 == p2)) |
| #else |
| #define IP6ADDR0(ip) ((ip)->s6_addr32[0]) |
| #define IP6ADDR1(ip) ((ip)->s6_addr32[1]) |
| #define IP6ADDR2(ip) ((ip)->s6_addr32[2]) |
| #define IP6ADDR3(ip) ((ip)->s6_addr32[3]) |
| #define IP6ADDR(ip) \ |
| IP6ADDR0(ip), IP6ADDR1(ip), IP6ADDR2(ip), IP6ADDR3(ip) |
|
|
| #define IP4ADDR(ip) ((ip)->s6_addr32[0]) |
|
|
| #define CMP_ADDR6(a1,a2) \ |
| (((a1)->s6_addr32[3] == (a2)->s6_addr32[3] && \ |
| (a1)->s6_addr32[2] == (a2)->s6_addr32[2] && \ |
| (a1)->s6_addr32[1] == (a2)->s6_addr32[1] && \ |
| (a1)->s6_addr32[0] == (a2)->s6_addr32[0])) |
|
|
| |
| |
| #define CMP_ADDR4A(a1,a2) \ |
| ((a1)->s6_addr32[0] == (a2)->s6_addr32[0]) |
| |
| #define CMP_ADDR4(apple,orange) \ |
| (((apple)->s6_addr32[0] == (orange))) |
| #define CMP_PORT(p1,p2) \ |
| ((p1 == p2)) |
| #endif |
|
|
| |
| |
| #define CMP_CXT4(cxt1, src, sp, dst, dp) \ |
| (( \ |
| CMP_PORT((cxt1)->s_port, (sp)) && \ |
| CMP_PORT((cxt1)->d_port, (dp)) && \ |
| CMP_ADDR4(&((cxt1)->s_ip), (src)) && \ |
| CMP_ADDR4(&((cxt1)->d_ip), (dst)) \ |
| )) |
|
|
| #define CMP_CXT6(cxt1, src, sp, dst, dp) \ |
| ((CMP_ADDR6(&(cxt1)->s_ip, (src)) && \ |
| CMP_ADDR6(&(cxt1)->d_ip, (dst)) && \ |
| CMP_PORT((cxt1)->s_port, (sp)) && CMP_PORT((cxt1)->d_port, (dp)))) |
|
|
| |
| #ifdef BSD_DERIVED |
| #define CLEAR_ADDR(a) { \ |
| (a)->__u6_addr.__u6_addr32[0] = 0; \ |
| (a)->__u6_addr.__u6_addr32[1] = 0; \ |
| (a)->__u6_addr.__u6_addr32[2] = 0; \ |
| (a)->__u6_addr.__u6_addr32[3] = 0; \ |
| } |
| #else |
| #define CLEAR_ADDR(a) { \ |
| (a)->s6_addr32[0] = 0; \ |
| (a)->s6_addr32[1] = 0; \ |
| (a)->s6_addr32[2] = 0; \ |
| (a)->s6_addr32[3] = 0; \ |
| } |
| #endif |
|
|
| #define CXT_HASH4(src,dst,sp,dp,pr) \ |
| ((src + dst + sp + dp + pr) % BUCKET_SIZE) |
|
|
| #ifndef BSD_DERIVED |
| #define CXT_HASH6(src,dst,sp,dp,pr) \ |
| (( \ |
| (src)->s6_addr32[0] + (src)->s6_addr32[1] + \ |
| (src)->s6_addr32[2] + (src)->s6_addr32[3] + \ |
| (dst)->s6_addr32[0] + (dst)->s6_addr32[1] + \ |
| (dst)->s6_addr32[2] + (dst)->s6_addr32[3] + \ |
| sp + dp + pr ) % BUCKET_SIZE) |
| #else |
| #define CXT_HASH6(src,dst,sp,dp,pr) \ |
| (( \ |
| (src)->__u6_addr.__u6_addr32[0] + (src)->__u6_addr.__u6_addr32[1] + \ |
| (src)->__u6_addr.__u6_addr32[2] + (src)->__u6_addr.__u6_addr32[3] + \ |
| (dst)->__u6_addr.__u6_addr32[0] + (dst)->__u6_addr.__u6_addr32[1] + \ |
| (dst)->__u6_addr.__u6_addr32[2] + (dst)->__u6_addr.__u6_addr32[3] + \ |
| sp + dp + pr ) % BUCKET_SIZE) |
| #endif |
|
|
| typedef struct _packetinfo { |
| |
| |
| |
| const struct pcap_pkthdr *pheader; |
| const uint8_t *packet; |
| |
| uint32_t eth_hlen; |
| uint16_t mvlan; |
| uint16_t vlan; |
| uint16_t eth_type; |
| uint32_t af; |
| ether_header *eth_hdr; |
| ether_arp *arph; |
| ip4_header *ip4; |
| ip6_header *ip6; |
| uint16_t packet_bytes; |
| uint16_t s_port; |
| uint16_t d_port; |
| uint8_t proto; |
| uint8_t sc; |
| tcp_header *tcph; |
| udp_header *udph; |
| uint16_t gre_hlen; |
| const uint8_t *end_ptr; |
| const uint8_t *payload; |
| uint32_t plen; |
| uint32_t our; |
| uint8_t up; |
| connection *cxt; |
| } packetinfo; |
|
|
| |
| |
| |
|
|
| #define PI_TOS(pi) ( (pi)->ip4->ip_tos ) |
| #define PI_ECN(pi) ( (pi)->tcph->t_flags & (TF_ECE|TF_CWR) ) |
|
|
| #define PI_IP4(pi) ((pi)->ip4) |
| #define PI_IP4SRC(pi) ( PI_IP4(pi)->ip_src ) |
| #define PI_IP4DST(pi) ( PI_IP4(pi)->ip_dst ) |
|
|
| #define PI_IP6(pi) ((pi)->ip6) |
| #define PI_IP6SRC(pi) (PI_IP6(pi)->ip_src) |
| #define PI_IP6DST(pi) (PI_IP6(pi)->ip_dst) |
|
|
| #define PI_TCP_SP(pi) ( ntohs((pi)->tcph->src_port)) |
| #define PI_TCP_DP(pi) ( ntohs((pi)->tcph->dst_port)) |
|
|
| #define SC_CLIENT 0x01 |
| #define SC_SERVER 0x02 |
| #define SC_UNKNOWN 0x03 |
|
|
| typedef struct _pdns_stat { |
| uint32_t got_packets; |
| uint32_t eth_recv; |
| uint32_t arp_recv; |
| uint32_t otherl_recv; |
| uint32_t vlan_recv; |
| uint32_t ip4_recv; |
| uint32_t ip6_recv; |
|
|
| uint32_t ip4_dns_udp; |
| uint32_t ip4_dec_udp_ok; |
| uint32_t ip4_dec_udp_er; |
| uint32_t ip4_dns_tcp; |
| uint32_t ip4_dec_tcp_ok; |
| uint32_t ip4_dec_tcp_er; |
| uint32_t ip4_recv_tcp; |
|
|
| uint32_t ip6_dns_udp; |
| uint32_t ip6_dec_udp_ok; |
| uint32_t ip6_dec_udp_er; |
| uint32_t ip6_dns_tcp; |
| uint32_t ip6_dec_tcp_ok; |
| uint32_t ip6_dec_tcp_er; |
| uint32_t ip6_recv_tcp; |
|
|
| uint32_t ip4ip_recv; |
| uint32_t ip6ip_recv; |
| uint32_t gre_recv; |
| uint32_t tcp_recv; |
| uint32_t udp_recv; |
| uint32_t icmp_recv; |
| uint32_t othert_recv; |
| uint32_t dns_records; |
| uint32_t dns_assets; |
| uint32_t tcp_os_assets; |
| uint32_t udp_os_assets; |
| uint32_t icmp_os_assets; |
| uint32_t dhcp_os_assets; |
| uint32_t tcp_services; |
| uint32_t tcp_clients; |
| uint32_t udp_services; |
| uint32_t udp_clients; |
| } pdns_stat; |
|
|
| #define CONFIG_VERBOSE 0x01 |
| #define CONFIG_UPDATES 0x02 |
| #define CONFIG_SYSLOG 0x04 |
| #define CONFIG_QUIET 0x08 |
| #define CONFIG_CONNECT 0x10 |
| #define CONFIG_CXWRITE 0x20 |
|
|
| #define INTERRUPT_END 0x01 |
| #define INTERRUPT_SESSION 0x02 |
| #define INTERRUPT_DNS 0x04 |
|
|
| typedef struct _globalconfig { |
| pcap_t *handle; |
| #ifdef HAVE_PFRING |
| pfring *pfhandle; |
| uint8_t use_pfring; |
| u_int cluster_id; |
| #endif |
| struct pcap_stat ps; |
| int linktype; |
| pdns_stat p_s; |
| struct bpf_program cfilter; |
| bpf_u_int32 net_mask; |
| uint8_t intr_flag; |
| uint8_t inpacket; |
|
|
| time_t dnslastchk; |
| struct timeval tstamp; |
| uint8_t cflags; |
| uint8_t verbose; |
| uint8_t print_updates; |
| uint8_t output_log; |
| uint8_t output_log_nxd; |
| uint8_t output_syslog; |
| uint8_t output_syslog_nxd; |
| #ifdef HAVE_JSON |
| uint8_t use_json; |
| uint8_t use_json_nxd; |
| #endif |
| uint8_t use_stats_file; |
| char *statsfile; |
| uint8_t setfilter; |
| uint8_t promisc; |
| uint8_t drop_privs_flag; |
| uint8_t chroot_flag; |
| uint8_t daemon_flag; |
| uint8_t logfile_all; |
| uint32_t fieldsf; |
| uint64_t dnsf; |
| uint32_t dnsfe; |
| uint32_t payload; |
| uint32_t curcxt; |
| uint32_t llcxt; |
| uint64_t mem_limit_max; |
| uint64_t mem_limit_size; |
| uint32_t dns_records; |
| uint32_t dns_assets; |
| uint64_t cxtrackerid; |
| char errbuf[PCAP_ERRBUF_SIZE]; |
| char *hostname; |
| char *bpff; |
| char *user_filter; |
| char *net_ip_string; |
| char *log_delimiter; |
| char *logfile; |
| char *logfile_nxd; |
| char *pcap_file; |
| FILE *logfile_fd; |
| FILE *logfile_nxd_fd; |
| char *dev; |
| char *dpath; |
| char *chroot_dir; |
| char *group_name; |
| char *user_name; |
| char *pidfile; |
| char *configpath; |
| uint32_t dnsprinttime; |
| uint32_t dnscachetimeout; |
| } globalconfig; |
|
|
| #define ISSET_CONFIG_VERBOSE(config) ((config).cflags & CONFIG_VERBOSE) |
| #define ISSET_CONFIG_UPDATES(config) ((config).cflags & CONFIG_UPDATES) |
| #define ISSET_CONFIG_SYSLOG(config) ((config).cflags & CONFIG_SYSLOG) |
| #define ISSET_CONFIG_QUIET(config) ((config).cflags & CONFIG_QUIET) |
|
|
| #define ISSET_INTERRUPT_END(config) ((config).intr_flag & INTERRUPT_END) |
| #define ISSET_INTERRUPT_SESSION(config) ((config).intr_flag & INTERRUPT_SESSION) |
| #define ISSET_INTERRUPT_DNS(config) ((config).intr_flag & INTERRUPT_DNS) |
|
|
| #define plog(fmt, ...) do{ fprintf(stdout, (fmt), ##__VA_ARGS__); }while(0) |
| #define flog(h, fmt, ...) \ |
| do{ \ |
| if (!(h == stdout && ISSET_CONFIG_QUIET(config))) \ |
| fprintf(h, fmt, ##__VA_ARGS__); \ |
| } \ |
| while(0) |
| #define olog(fmt, ...) do{ if(!(ISSET_CONFIG_QUIET(config))) fprintf(stdout, (fmt), ##__VA_ARGS__); }while(0) |
| |
| #ifdef DEBUG |
| #define dlog(fmt, ...) do { fprintf(stderr, ("[%s:%d(%s)] " fmt), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__);} while(0) |
| #define vlog(v, fmt, ...) do{ if(DEBUG == v) fprintf(stderr, ("[%s:%d(%s)] " fmt), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); }while(0) |
| #define elog(fmt, ...) fprintf(stderr, ("[%s:%d(%s)] " fmt), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); |
| #else |
| #define elog(fmt, ...) fprintf(stderr, (fmt), ##__VA_ARGS__); |
| #define dlog(fmt, ...) do { ; } while(0) |
| #define vlog(fmt, ...) do { ; } while(0) |
| #endif |
|
|
| int cxt_update_client(connection *cxt, packetinfo *pi); |
| int cxt_update_unknown(connection *cxt, packetinfo *pi); |
| int cxt_update_server(connection *cxt, packetinfo *pi); |
|
|
|
|