1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/*
* Network accounting
* capture-pcap.c - capture raw packets - pcap version
* (C) 1996 Ulrich Callmeier
*/
#include "netacct.h"
#include <pcap.h>
#include <sys/ethernet.h>
#include <netinet/ip_icmp.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <string.h>
void handle_ip(unsigned char buf[], char *devname, char *user);
pcap_t *pcap;
char errbuff[PCAP_ERRBUF_SIZE];
#define PCAP_SNAPLEN 128
#define PCAP_TMOUT 1000
void handle_frame (unsigned char[], int);
void init_capture()
{
struct promisc_device *p;
pcap = NULL;
p = cfg -> promisc;
if(p!=NULL)
{
pcap = pcap_open_live(p -> name, PCAP_SNAPLEN, 1, PCAP_TMOUT, errbuff);
if(pcap == NULL)
{
syslog(LOG_ERR, "can't pcap_open_live: %s\n", errbuff);
daemon_stop(0);
}
}
DEBUG(DBG_MISC, sprintf(dbg, "%s set to promiscous mode\n", p -> name));
}
void exit_capture(void)
{
if(pcap != NULL) pcap_close (pcap);
pcap = NULL;
}
inline int onnet(unsigned long int addr, struct ipnetwork *net)
{
return ((addr & net -> netmask) == net -> netnumber);
}
int onnetlist(unsigned long int addr, struct ipnetwork *netlist)
{
while(netlist!=NULL)
{
if(onnet(addr, netlist))
{
return 1;
}
netlist = netlist -> next;
}
return 0;
}
void do_packet(u_char *usr, const struct pcap_pkthdr *h, const u_char *p)
{
handle_frame(p, h->len);
}
void packet_loop()
{
int res;
while(running)
{
res = pcap_dispatch(pcap, -1, do_packet, NULL);
if(res == -1)
{
syslog(LOG_ERR, "pcap_dispatch: %s\n", pcap_geterr(pcap));
DEBUG(DBG_ERR, sprintf(dbg,"pcap_dispatch: %s\n", pcap_geterr(pcap)));
}
}
}
FILE *acct_file = stderr;
void
handle_frame (unsigned char buf[], int length)
{
static struct ip tmp_iphdr;
unsigned short srcport, dstport;
struct tcphdr tmp_tcphdr;
struct udphdr tmp_udphdr;
struct icmp tmp_icmphdr;
int found = 0;
struct mon_host_struct *ptr;
if(buf[12] * 256 + buf[13] == htons(ETHERTYPE_IP))
{
memcpy (&tmp_iphdr, &(buf[14]), sizeof (tmp_iphdr));
found = 0;
if(cfg->hostlist) { /* we specified at least one hostlimit tag */
/* if we don't monitor this IP, bail now - mk */
for(ptr=cfg->hostlist;ptr && !found;ptr=ptr->next) {
if(ptr->ipaddr == tmp_iphdr->saddr
|| ptr->ipaddr == tmp_iphdr->daddr)
found = 1;
}
if(!found) {
packets->ignored++;
continue;
}
}
if((tmp_iphdr.ip_src.s_addr & cfg->ignoremask) ==
(tmp_iphdr.ip_dst.s_addr & cfg->ignoremask))
{
packets->local++;
return;
}
else
{
if(onnetlist(tmp_iphdr.ip_src.s_addr,cfg->ignorenet) ||
onnetlist(tmp_iphdr.ip_dst.s_addr, cfg->ignorenet))
{
if(!(onnetlist(tmp_iphdr.ip_src.s_addr,cfg->dontignore) ||
onnetlist(tmp_iphdr.ip_dst.s_addr, cfg->dontignore)))
{
if(debug_level & DBG_IGNORE)
{
char tmp[18];
strcpy(tmp, intoa(tmp_iphdr.ip_src.s_addr));
DEBUG(DBG_IGNORE, sprintf(dbg, "netignored: %s -> %s\n",
tmp,intoa(tmp_iphdr.ip_dst.s_addr)));
}
packets->netignored++;
return;
}
}
packets->ip++;
switch(tmp_iphdr.ip_p)
{
case IPPROTO_UDP:
packets->ip_udp++;
memcpy (&tmp_udphdr, &buf[14 + tmp_iphdr.ip_hl * 4], sizeof (tmp_udphdr));
srcport = tmp_udphdr.uh_sport;
dstport = tmp_udphdr.uh_dport;
break;
case IPPROTO_TCP:
packets->ip_tcp++;
memcpy (&tmp_tcphdr, &buf[14 + tmp_iphdr.ip_hl * 4], sizeof (tmp_tcphdr));
srcport = tmp_tcphdr.th_sport;
dstport = tmp_tcphdr.th_dport;
break;
case IPPROTO_ICMP:
packets->ip_icmp++;
memcpy (&tmp_icmphdr, &buf[14 + tmp_iphdr.ip_hl * 4], sizeof (tmp_icmphdr));
srcport = tmp_icmphdr.icmp_type;
dstport = tmp_icmphdr.icmp_code;
break;
default:
packets->ip_other++;
srcport = dstport = 0;
break;
}
register_packet(tmp_iphdr.ip_src.s_addr,tmp_iphdr.ip_dst.s_addr,
tmp_iphdr.ip_p, srcport, dstport,
ntohs(tmp_iphdr.ip_len), strdup(cfg->promisc->name), NULL);
}
}
else
{
/* ETH_P_ARP, ETH_P_RARP, ETH_P_IPX, etc. */
packets -> ignored ++;
}
}
|