Branch data Line data Source code
1 : : // $Id: PacketFilter.cc 967 2005-01-03 07:19:06Z vern $
2 : :
3 : : #include "PacketFilter.h"
4 : :
5 : 0 : void PacketFilter::AddSrc(addr_type src, uint32 tcp_flags, double probability)
6 : : {
7 : 0 : Filter* f = new Filter;
8 : 0 : f->tcp_flags = tcp_flags;
9 : 0 : f->probability = uint32(probability * RAND_MAX);
10 : 0 : src_filter.Insert(src, NUM_ADDR_WORDS * 32, f);
11 : 0 : }
12 : :
13 : 0 : void PacketFilter::AddSrc(Val* src, uint32 tcp_flags, double probability)
14 : : {
15 : 0 : Filter* f = new Filter;
16 : 0 : f->tcp_flags = tcp_flags;
17 : 0 : f->probability = uint32(probability * RAND_MAX);
18 : 0 : src_filter.Insert(src, f);
19 : 0 : }
20 : :
21 : 0 : void PacketFilter::AddDst(addr_type dst, uint32 tcp_flags, double probability)
22 : : {
23 : 0 : Filter* f = new Filter;
24 : 0 : f->tcp_flags = tcp_flags;
25 : 0 : f->probability = uint32(probability * RAND_MAX);
26 : 0 : dst_filter.Insert(dst, NUM_ADDR_WORDS * 32, f);
27 : 0 : }
28 : :
29 : 0 : void PacketFilter::AddDst(Val* dst, uint32 tcp_flags, double probability)
30 : : {
31 : 0 : Filter* f = new Filter;
32 : 0 : f->tcp_flags = tcp_flags;
33 : 0 : f->probability = uint32(probability * RAND_MAX);
34 : 0 : dst_filter.Insert(dst, f);
35 : 0 : }
36 : :
37 : 0 : bool PacketFilter::RemoveSrc(addr_type src)
38 : : {
39 : 0 : return src_filter.Remove(src, NUM_ADDR_WORDS * 32) != 0;
40 : : }
41 : :
42 : 0 : bool PacketFilter::RemoveSrc(Val* src)
43 : : {
44 : 0 : return src_filter.Remove(src) != NULL;
45 : : }
46 : :
47 : 0 : bool PacketFilter::RemoveDst(addr_type dst)
48 : : {
49 : 0 : return dst_filter.Remove(dst, NUM_ADDR_WORDS * 32) != NULL;
50 : : }
51 : :
52 : 0 : bool PacketFilter::RemoveDst(Val* dst)
53 : : {
54 : 0 : return dst_filter.Remove(dst) != NULL;
55 : : }
56 : :
57 : 0 : bool PacketFilter::Match(const IP_Hdr* ip, int len, int caplen)
58 : : {
59 : : #ifdef BROv6
60 : : Filter* f = (Filter*) src_filter.Lookup(ip->SrcAddr(),
61 : : NUM_ADDR_WORDS * 32);
62 : : #else
63 : : Filter* f = (Filter*) src_filter.Lookup(*ip->SrcAddr(),
64 : 0 : NUM_ADDR_WORDS * 32);
65 : : #endif
66 [ # # ]: 0 : if ( f )
67 : 0 : return MatchFilter(*f, *ip, len, caplen);
68 : :
69 : : #ifdef BROv6
70 : : f = (Filter*) dst_filter.Lookup(ip->DstAddr(), NUM_ADDR_WORDS * 32);
71 : : #else
72 : 0 : f = (Filter*) dst_filter.Lookup(*ip->DstAddr(), NUM_ADDR_WORDS * 32);
73 : : #endif
74 [ # # ]: 0 : if ( f )
75 : 0 : return MatchFilter(*f, *ip, len, caplen);
76 : :
77 : 0 : return default_match;
78 : : }
79 : :
80 : : bool PacketFilter::MatchFilter(const Filter& f, const IP_Hdr& ip,
81 : 0 : int len, int caplen)
82 : : {
83 [ # # ][ # # ]: 0 : if ( ip.NextProto() == IPPROTO_TCP && f.tcp_flags )
[ # # ]
84 : : {
85 : : // Caution! The packet sanity checks have not been performed yet
86 : 0 : const struct ip* ip4 = ip.IP4_Hdr();
87 : :
88 : 0 : int ip_hdr_len = ip4->ip_hl * 4;
89 : 0 : len -= ip_hdr_len; // remove IP header
90 : 0 : caplen -= ip_hdr_len;
91 : :
92 [ # # # # ]: 0 : if ( (unsigned int) len < sizeof(struct tcphdr) ||
93 : : (unsigned int) caplen < sizeof(struct tcphdr) )
94 : : // Packet too short, will be dropped anyway.
95 : 0 : return false;
96 : :
97 : : const struct tcphdr* tp =
98 : 0 : (const struct tcphdr*) ((u_char*) ip4 + ip_hdr_len);
99 : :
100 [ # # ]: 0 : if ( tp->th_flags & f.tcp_flags )
101 : : // At least one of the flags is set, so don't drop
102 : 0 : return false;
103 : : }
104 : :
105 : 0 : return uint32(random()) < f.probability;
106 [ + - ][ + - ]: 6 : }
|