LCOV - code coverage report
Current view: top level - src - Sessions.h (source / functions) Hit Total Coverage
Test: app.info Lines: 2 14 14.3 %
Date: 2010-12-13 Functions: 6 18 33.3 %
Branches: 2 14 14.3 %

           Branch data     Line data    Source code
       1                 :            : // $Id: Sessions.h 6219 2008-10-01 05:39:07Z vern $
       2                 :            : //
       3                 :            : // See the file "COPYING" in the main distribution directory for copyright.
       4                 :            : 
       5                 :            : #ifndef sessions_h
       6                 :            : #define sessions_h
       7                 :            : 
       8                 :            : #include "Dict.h"
       9                 :            : #include "CompHash.h"
      10                 :            : #include "IP.h"
      11                 :            : #include "ARP.h"
      12                 :            : #include "Frag.h"
      13                 :            : #include "PacketFilter.h"
      14                 :            : #include "Stats.h"
      15                 :            : #include "NetVar.h"
      16                 :            : 
      17                 :            : struct pcap_pkthdr;
      18                 :            : 
      19                 :            : class Connection;
      20                 :            : class ConnID;
      21                 :            : class OSFingerprint;
      22                 :            : class ConnCompressor;
      23                 :            : 
      24 [ -  + ][ #  # ]:        698 : declare(PDict,Connection);
      25 [ -  + ][ #  # ]:          2 : declare(PDict,FragReassembler);
      26                 :            : 
      27                 :            : class Discarder;
      28                 :            : class SteppingStoneManager;
      29                 :            : class PacketFilter;
      30                 :            : 
      31                 :            : class PacketSortElement;
      32                 :            : 
      33                 :            : struct SessionStats {
      34                 :            :         int num_TCP_conns;
      35                 :            :         int num_UDP_conns;
      36                 :            :         int num_ICMP_conns;
      37                 :            :         int num_fragments;
      38                 :            :         int num_packets;
      39                 :            :         int num_timers;
      40                 :            :         int num_events_queued;
      41                 :            :         int num_events_dispatched;
      42                 :            : 
      43                 :            :         int max_TCP_conns;
      44                 :            :         int max_UDP_conns;
      45                 :            :         int max_ICMP_conns;
      46                 :            :         int max_fragments;
      47                 :            :         int max_timers;
      48                 :            : };
      49                 :            : 
      50                 :            : // Drains and deletes a timer manager if it hasn't seen any advances
      51                 :            : // for an interval timer_mgr_inactivity_timeout.
      52 [ #  # ][ #  # ]:          0 : class TimerMgrExpireTimer : public Timer {
      53                 :            : public:
      54                 :          0 :         TimerMgrExpireTimer(double t, TimerMgr* arg_mgr)
      55                 :          0 :                 : Timer(t, TIMER_TIMERMGR_EXPIRE)
      56                 :            :                 {
      57                 :          0 :                 mgr = arg_mgr;
      58                 :          0 :                 }
      59                 :            : 
      60                 :            :         virtual void Dispatch(double t, int is_expire);
      61                 :            : 
      62                 :            : protected:
      63                 :            :         double interval;
      64                 :            :         TimerMgr* mgr;
      65                 :            : };
      66                 :            : 
      67                 :            : class NetSessions {
      68                 :            : public:
      69                 :            :         NetSessions();
      70                 :            :         ~NetSessions();
      71                 :            : 
      72                 :            :         // Main entry point for packet processing. Dispatches the packet
      73                 :            :         // either through NextPacket() or NextPacketSecondary(), optionally
      74                 :            :         // employing the packet sorter first.
      75                 :            :         void DispatchPacket(double t, const struct pcap_pkthdr* hdr,
      76                 :            :                         const u_char* const pkt, int hdr_size,
      77                 :            :                         PktSrc* src_ps, PacketSortElement* pkt_elem);
      78                 :            :         
      79                 :            :         void Done();    // call to drain events before destructing
      80                 :            : 
      81                 :            :         // Returns a reassembled packet, or nil if there are still
      82                 :            :         // some missing fragments.
      83                 :            :         FragReassembler* NextFragment(double t, const IP_Hdr* ip,
      84                 :            :                                 const u_char* pkt, uint32 frag_field);
      85                 :            : 
      86                 :            :         int Get_OS_From_SYN(struct os_type* retval,
      87                 :            :                         uint16 tot, uint8 DF_flag, uint8 TTL, uint16 WSS,
      88                 :            :                         uint8 ocnt, uint8* op, uint16 MSS, uint8 win_scale,
      89                 :            :                         uint32 tstamp, /* uint8 TOS, */ uint32 quirks,
      90                 :            :                         uint8 ECN) const;
      91                 :            : 
      92                 :            :         bool CompareWithPreviousOSMatch(uint32 addr, int id) const;
      93                 :            : 
      94                 :            :         // Looks up the connection referred to by the given Val,
      95                 :            :         // which should be a conn_id record.  Returns nil if there's
      96                 :            :         // no such connection or the Val is ill-formed.
      97                 :            :         Connection* FindConnection(Val* v);
      98                 :            : 
      99                 :            :         void Remove(Connection* c);
     100                 :            :         void Remove(FragReassembler* f);
     101                 :            : 
     102                 :            :         void Insert(Connection* c);
     103                 :            : 
     104                 :            :         // Generating connection_pending events for all connections
     105                 :            :         // that are still active.
     106                 :            :         void Drain();
     107                 :            : 
     108                 :            :         // Called periodically to generate statistics reports.
     109                 :            :         void HeartBeat(double t);
     110                 :            : 
     111                 :            :         void GetStats(SessionStats& s) const;
     112                 :            : 
     113                 :            :         void Weird(const char* name,
     114                 :            :                 const struct pcap_pkthdr* hdr, const u_char* pkt);
     115                 :            :         void Weird(const char* name, const IP_Hdr* ip);
     116                 :            : 
     117                 :          0 :         PacketFilter* GetPacketFilter()
     118                 :            :                 {
     119         [ #  # ]:          0 :                 if ( ! packet_filter )
     120                 :          0 :                         packet_filter = new PacketFilter(packet_filter_default);
     121                 :          0 :                 return packet_filter;
     122                 :            :                 }
     123                 :            : 
     124                 :            :         // Looks up timer manager associated with tag.  If tag is unknown and
     125                 :            :         // "create" is true, creates new timer manager and stores it.  Returns
     126                 :            :         // global timer manager if tag is nil.
     127                 :            :         TimerMgr* LookupTimerMgr(const TimerMgr::Tag* tag, bool create = true);
     128                 :            : 
     129                 :            :         void ExpireTimerMgrs();
     130                 :            : 
     131                 :          0 :         SteppingStoneManager* GetSTPManager()   { return stp_manager; }
     132                 :            : 
     133                 :          0 :         unsigned int CurrentConnections()
     134                 :            :                 {
     135                 :            :                 return tcp_conns.Length() + udp_conns.Length() +
     136                 :          0 :                         icmp_conns.Length();
     137                 :            :                 }
     138                 :            : 
     139                 :            :         unsigned int ConnectionMemoryUsage();
     140                 :            :         unsigned int ConnectionMemoryUsageConnVals();
     141                 :            :         unsigned int MemoryAllocation();
     142                 :            :         TCPStateStats tcp_stats;        // keeps statistics on TCP states
     143                 :            : 
     144                 :            : protected:
     145                 :            :         friend class RemoteSerializer;
     146                 :            :         friend class ConnCompressor;
     147                 :            :         friend class TimerMgrExpireTimer;
     148                 :            : 
     149                 :            :         Connection* NewConn(HashKey* k, double t, const ConnID* id,
     150                 :            :                         const u_char* data, int proto);
     151                 :            : 
     152                 :            :         // Check whether the tag of the current packet is consistent with
     153                 :            :         // the given connection.  Returns:
     154                 :            :         //    -1   if current packet is to be completely ignored.
     155                 :            :         //     0   if tag is not consistent and new conn should be instantiated.
     156                 :            :         //     1   if tag is consistent, i.e., packet is part of connection.
     157                 :            :         int CheckConnectionTag(Connection* conn);
     158                 :            : 
     159                 :            :         // Returns true if the port corresonds to an application
     160                 :            :         // for which there's a Bro analyzer (even if it might not
     161                 :            :         // be used by the present policy script), or it's more
     162                 :            :         // generally a likely server port, false otherwise.
     163                 :            :         //
     164                 :            :         // Note, port is in host order.
     165                 :            :         bool IsLikelyServerPort(uint32 port,
     166                 :            :                                 TransportProto transport_proto) const;
     167                 :            : 
     168                 :            :         // Upon seeing the first packet of a connection, checks whether
     169                 :            :         // we want to analyze it (e.g., we may not want to look at partial
     170                 :            :         // connections), and, if yes, whether we should flip the roles of
     171                 :            :         // originator and responder (based on known ports or such).
     172                 :            :         // Use tcp_flags=0 for non-TCP.
     173                 :            :         bool WantConnection(uint16 src_port, uint16 dest_port,
     174                 :            :                                 TransportProto transport_proto,
     175                 :            :                                 uint8 tcp_flags, bool& flip_roles);
     176                 :            : 
     177                 :            :         void NextPacket(double t, const struct pcap_pkthdr* hdr,
     178                 :            :                         const u_char* const pkt, int hdr_size,
     179                 :            :                         PacketSortElement* pkt_elem);
     180                 :            :                 
     181                 :            :         void DoNextPacket(double t, const struct pcap_pkthdr* hdr,
     182                 :            :                         const IP_Hdr* ip_hdr, const u_char* const pkt,
     183                 :            :                         int hdr_size);
     184                 :            : 
     185                 :            :         void NextPacketSecondary(double t, const struct pcap_pkthdr* hdr,
     186                 :            :                         const u_char* const pkt, int hdr_size,
     187                 :            :                         const PktSrc* src_ps);
     188                 :            :         
     189                 :            :         // Record the given packet (if a dumper is active).  If len=0
     190                 :            :         // then the whole packet is recorded, otherwise just the first
     191                 :            :         // len bytes.
     192                 :            :         void DumpPacket(const struct pcap_pkthdr* hdr, const u_char* pkt,
     193                 :            :                         int len=0);
     194                 :            : 
     195                 :            :         void Internal(const char* msg, const struct pcap_pkthdr* hdr,
     196                 :            :                         const u_char* pkt);
     197                 :            : 
     198                 :            :         // Builds a record encapsulating a packet.  This should be more
     199                 :            :         // general, including the equivalent of a union of tcp/udp/icmp
     200                 :            :         // headers .
     201                 :            :         Val* BuildHeader(const struct ip* ip);
     202                 :            : 
     203                 :            :         CompositeHash* ch;
     204                 :            :         PDict(Connection) tcp_conns;
     205                 :            :         PDict(Connection) udp_conns;
     206                 :            :         PDict(Connection) icmp_conns;
     207                 :            :         PDict(FragReassembler) fragments;
     208                 :            : 
     209                 :            :         ARP_Analyzer* arp_analyzer;
     210                 :            : 
     211                 :            :         SteppingStoneManager* stp_manager;
     212                 :            :         Discarder* discarder;
     213                 :            :         PacketFilter* packet_filter;
     214                 :            :         OSFingerprint* SYN_OS_Fingerprinter;
     215                 :            :         int build_backdoor_analyzer;
     216                 :            :         int dump_this_packet;   // if true, current packet should be recorded
     217                 :            :         int num_packets_processed;
     218                 :            :         PacketProfiler* pkt_profiler;
     219                 :            : 
     220                 :            :         // We may use independent timer managers for different sets of related
     221                 :            :         // activity.  The managers are identified by an unique tag.
     222                 :            :         typedef std::map<TimerMgr::Tag, TimerMgr*> TimerMgrMap;
     223                 :            :         TimerMgrMap timer_mgrs;
     224                 :            : };
     225                 :            : 
     226                 :            : // Manager for the currently active sessions.
     227                 :            : extern NetSessions* sessions;
     228                 :            : 
     229                 :            : #endif

Generated by: LCOV version 1.8