Branch data Line data Source code
1 : : // $Id:$
2 : : //
3 : : // The central management unit for dynamic analyzer selection.
4 : :
5 : : #ifndef DPM_H
6 : : #define DPM_H
7 : :
8 : : #include <queue>
9 : :
10 : : #include "Analyzer.h"
11 : : #include "Dict.h"
12 : : #include "net_util.h"
13 : :
14 : : // DPM debug logging, which includes the connection id into the message.
15 : : #ifdef DEBUG
16 : : # define DBG_DPD(conn, txt) \
17 : : DBG_LOG(DBG_DPD, "%s " txt, \
18 : : fmt_conn_id(conn->OrigAddr(), ntohs(conn->OrigPort()), \
19 : : conn->RespAddr(), ntohs(conn->RespPort())));
20 : : # define DBG_DPD_ARGS(conn, fmt, args...) \
21 : : DBG_LOG(DBG_DPD, "%s " fmt, \
22 : : fmt_conn_id(conn->OrigAddr(), ntohs(conn->OrigPort()), \
23 : : conn->RespAddr(), ntohs(conn->RespPort())), ##args);
24 : : #else
25 : : # define DBG_DPD(conn, txt)
26 : : # define DBG_DPD_ARGS(conn, fmt, args...)
27 : : #endif
28 : :
29 : : // Map to assign expected connections to analyzers.
30 : : class ExpectedConn {
31 : : public:
32 : : // This form can be used for IPv6 as well as IPv4.
33 : : ExpectedConn(const uint32* _orig, const uint32* _resp,
34 : : uint16 _resp_p, uint16 _proto);
35 : :
36 : : // This form only works for expecting an IPv4 connection. Note
37 : : // that we do the right thing whether we're built IPv4-only or
38 : : // BROv6.
39 : : ExpectedConn(uint32 _orig, uint32 _resp, uint16 _resp_p, uint16 _proto);
40 : :
41 : : ExpectedConn(const ExpectedConn& c);
42 : :
43 : : uint32 orig[NUM_ADDR_WORDS];
44 : : uint32 resp[NUM_ADDR_WORDS];
45 : : uint16 resp_p;
46 : : uint16 proto;
47 : : };
48 : :
49 : : // Associates an analyzer for an expected future connection.
50 : : class AssignedAnalyzer {
51 : : public:
52 : 0 : AssignedAnalyzer(const ExpectedConn& c)
53 : 0 : : conn(c)
54 : : {
55 : 0 : }
56 : :
57 : : ExpectedConn conn;
58 : : AnalyzerTag::Tag analyzer;
59 : : double timeout;
60 : : void* cookie;
61 : : bool deleted;
62 : :
63 : 0 : static bool compare(const AssignedAnalyzer* a1, const AssignedAnalyzer* a2)
64 : 0 : { return a1->timeout > a2->timeout; }
65 : : };
66 : :
67 [ - + ][ # # ]: 4 : declare(PDict, AssignedAnalyzer);
68 : :
69 : : class DPM {
70 : : public:
71 : : DPM();
72 : : ~DPM();
73 : :
74 : : // Setup analyzer config.
75 : : void PreScriptInit(); // To be called before scripts are parsed ...
76 : : void PostScriptInit(); // ... and after.
77 : :
78 : : // Given info about the first packet, build initial analyzer tree.
79 : : //
80 : : // It would be more flexible if we simply pass in the IP header
81 : : // and then extract the information we need. However, when this
82 : : // method is called from the session management, protocol and ports
83 : : // have already been extracted there and it would be a waste to do
84 : : // it again.
85 : : //
86 : : // Returns 0 if we can't build a tree (e.g., because the necessary
87 : : // analyzers have not been converted to the DPM framework yet...)
88 : :
89 : : bool BuildInitialAnalyzerTree(TransportProto proto, Connection* conn,
90 : : const u_char* data);
91 : :
92 : : // Schedules a particular analyzer for an upcoming connection.
93 : : // 0 acts as a wildcard for orig. (Cookie is currently unused.
94 : : // Eventually, we may pass it on to the analyzer).
95 : : void ExpectConnection(addr_type orig, addr_type resp, uint16 resp_p,
96 : : TransportProto proto, AnalyzerTag::Tag analyzer,
97 : : double timeout, void* cookie);
98 : :
99 : : // Activates signature matching for protocol detection. (Called when an
100 : : // DPM signatures is found.)
101 : 11 : void ActivateSigs() { sigs_activated = true; }
102 : : bool SigsActivated() const { return sigs_activated; }
103 : :
104 : : void Done();
105 : :
106 : : private:
107 : : // Convert script-level config into internal data structures.
108 : : void AddConfig(const Analyzer::Config& tag);
109 : :
110 : : // Return analyzer if any has been scheduled with ExpectConnection()
111 : : // AnalyzerTag::::Error if none.
112 : : AnalyzerTag::Tag GetExpected(int proto, const Connection* conn);
113 : :
114 : : // Mappings of destination port to analyzer.
115 : : typedef list<AnalyzerTag::Tag> tag_list;
116 : : typedef map<uint32, tag_list*> analyzer_map;
117 : : analyzer_map tcp_ports;
118 : : analyzer_map udp_ports;
119 : :
120 : : // Array of bools indicating whether an analyzer is activated,
121 : : // indexed by AnalyzerTag::Tag.
122 : : bool* active_analyzers;
123 : :
124 : : // True if signature-matching has been activated.
125 : : bool sigs_activated;
126 : :
127 : : PDict(AssignedAnalyzer) expected_conns;
128 : :
129 : : typedef priority_queue<
130 : : AssignedAnalyzer*,
131 : : vector<AssignedAnalyzer*>,
132 : : bool (*)(const AssignedAnalyzer*,
133 : : const AssignedAnalyzer*)> conn_queue;
134 : : conn_queue expected_conns_queue;
135 : : };
136 : :
137 : : extern DPM* dpm;
138 : :
139 : : #endif
|