home *** CD-ROM | disk | FTP | other *** search
/ ftp.sustworks.com 2018 / ftp.sustworks.com.zip / ftp.sustworks.com / open_source_IPNetMonitor_TNKE.dmg / ipkTypes.h < prev    next >
Text File  |  2005-08-22  |  12KB  |  351 lines

  1. //
  2. // ipkTypes.h
  3. // IPNetSentryX
  4. //
  5. // Created by Peter Sichel on Thu Nov 14 2002.
  6. // Copyright (c) 2002 Sustainable Softworks, Inc. All rights reserved.
  7. //
  8. // IPNetSentry_NKE and IPNetRouter_NKE shared types
  9. // This module is designed to be tested as client code and then incorporated
  10. // as part of our NKE
  11.  
  12. #ifndef _H_ipkTypes
  13. #define _H_ipkTypes
  14. #pragma once
  15.  
  16. #include <sys/types.h>
  17. #include <sys/time.h>
  18. #include <libkern/OSTypes.h>
  19. #include <netinet/kpi_ipfilter.h>
  20.  
  21. #define kBSDNameLength    16
  22. #define kServiceIDNameLength 48
  23.  
  24. // ---------------------------------------------------------------------------
  25. // InterfaceEntry
  26. // ---------------------------------------------------------------------------
  27. // define interfaceEntry to represent interface attach parameters
  28. typedef struct netNumber {
  29.     u_int32_t    address;
  30.     u_int32_t    mask;
  31. } netNumber_t;
  32.  
  33. typedef struct    KFT_interfaceEntry {
  34.     char         bsdName[kBSDNameLength];    // corresponding interface name (CString)
  35.     char        serviceID[kServiceIDNameLength];
  36.     netNumber_t ifNet;
  37.     netNumber_t natNet;
  38.     netNumber_t singleNet;
  39.     netNumber_t excludeNet;
  40.     u_int32_t    exposedHost;
  41.     u_int8_t    exposedHostSelection;
  42.     u_int8_t    filterOn;            // IP filter on this interface
  43.     u_int8_t    externalOn;
  44.     u_int8_t    natOn;                // NAT on this interface
  45. } KFT_interfaceEntry_t;
  46.  
  47. // ---------------------------------------------------------------------------
  48. // Control and Attach instance
  49. // ---------------------------------------------------------------------------
  50. // maximum number of controllers and DLIL attachments use index values 1..n
  51. // 0 is reserved for "not found"
  52. #define kMaxControl    8
  53. #define kMaxAttach 8
  54.  
  55. // controller instance
  56. typedef struct controlE {
  57.     socket_t ctl;                // Non-null if controlled
  58.     int monitorOn;                // master on/off for this controller
  59.     int nkeSends;                // count packets sent upstream since last request
  60.                                 // so we don't flood input queue when no one is listening
  61.     u_int8_t attachMap[kMaxAttach+1]; // map of corresponding DLIL attachments if any
  62.                                 // map[i]>0 if attached
  63. } control_t;
  64.  
  65. // DLIL attach instance
  66. typedef struct attachE {
  67.     ipfilter_t ipFilterRef;
  68.     interface_filter_t ifFilterRef;
  69.     KFT_interfaceEntry_t kftInterfaceEntry;
  70.     // monitor tool
  71.     int32_t sendCount;        // traffic stats for instance
  72.     int32_t receiveCount;
  73.     int32_t sendStamp;        // capture and hold previous counts for reporting
  74.     int32_t receiveStamp;
  75.     // ref
  76.     ifnet_t ifnet_ref;        // the ifnet we attached to
  77.     u_int8_t attachIndex;   // remember our own attach index for convenience
  78.     u_int8_t pad1;
  79.     u_int8_t pad2;
  80.     u_int8_t pad3;
  81. } attach_t;
  82.  
  83. typedef struct KFT_stat64 {
  84.     int64_t count;
  85.     int64_t previous;
  86.     int64_t delta;
  87. } KFT_stat64_t;
  88.  
  89. typedef struct KFT_stat {
  90.     int32_t count;
  91.     int32_t previous;
  92.     int32_t delta;
  93.     int32_t pad;
  94. } KFT_stat_t;
  95.  
  96. // ---------------------------------------------------------------------------
  97. // Filter Entry
  98. // ---------------------------------------------------------------------------
  99. #define kPropertySize 128
  100. #define kPropertyReserve 32
  101. // define filter table entry
  102. typedef struct KFT_filterEntry {
  103.     u_int16_t nodeCount;
  104.     u_int8_t pad0;
  105.     u_int8_t enabled;
  106.     u_int8_t nodeNumber[16];
  107.     u_int8_t nodeName[32];
  108.     u_int8_t property;
  109.     u_int8_t relation;
  110.     u_int8_t filterAction;
  111.     u_int8_t expandedState;
  112.     int32_t lastTime;
  113.     KFT_stat64_t match;
  114.     KFT_stat64_t byte;
  115.     int32_t rateLimit;        // rate limit in bytes/sec
  116.     int32_t intervalBytes;
  117.     int16_t activeInCount;
  118.     int16_t activeOutCount;
  119.     u_int8_t propertyValue[kPropertySize];    // |propertyValue ->  <- parameterValue|
  120.     u_int8_t pad1;
  121.     u_int8_t propertyEnd;
  122.     u_int8_t parameterStart;
  123. } KFT_filterEntry_t;
  124.  
  125. // ---------------------------------------------------------------------------
  126. // Trigger Entry
  127. // ---------------------------------------------------------------------------
  128. // define trigger table entry used to maintain triggered addresses
  129. typedef struct KFT_triggerEntry {
  130.     u_int32_t address;  // address and type are used together as an 8-byte key
  131.     u_int32_t type;
  132.     u_int32_t lastTime;
  133.     u_int32_t durationSeconds;
  134.     u_int8_t duration;    
  135.     u_int8_t flags;
  136.     u_int8_t pad0;
  137.     u_int8_t pad1;
  138.     KFT_stat_t match;
  139.     u_int8_t nodeNumber[16];
  140. } KFT_triggerEntry_t;
  141. #define kTriggerTypeTrigger 0
  142. #define kTriggerTypeAddress 1
  143. #define kTriggerTypeAuthorize 2
  144. #define kTriggerFlagDelete 1
  145. #define kTriggerFlagUpdate 2
  146. #define kTriggerFlagRemoveAll 4
  147.  
  148. typedef struct KFT_triggerKey {
  149.     u_int32_t address;  // address and type are used together as an 8-byte key
  150.     u_int32_t type;
  151. } KFT_triggerKey_t;
  152.  
  153. // ---------------------------------------------------------------------------
  154. // Connection Endpoint
  155. // ---------------------------------------------------------------------------
  156. // define Connection endpoint, MSB->LSB: protocol, port, address
  157. // so that adjacent entries will be in protocol, port order.
  158. typedef struct KFT_connectionEndpoint {
  159.     u_int16_t port;
  160.     u_int8_t  pad;
  161.     u_int8_t  protocol;
  162.     u_int32_t address;
  163. } KFT_connectionEndpoint_t;
  164.  
  165. // ---------------------------------------------------------------------------
  166. // NAT Entry
  167. // ---------------------------------------------------------------------------
  168. // define NAT table
  169. typedef struct KFT_natEntry {
  170.     KFT_connectionEndpoint_t apparent;
  171.     KFT_connectionEndpoint_t actual;
  172.     KFT_connectionEndpoint_t remote;
  173.     u_int32_t    lastTime;            // NSTimeInterval since 1970
  174.     u_int32_t    flags;
  175.     char         bsdName[kBSDNameLength];    // corresponding interface name (CString)
  176.     // NAPT
  177.     u_int16_t    portRange;            // offset to last port in port range
  178.     u_int32_t   seqFINLocal;        // seq# to check for last ACK
  179.     u_int32_t   seqFINPeer;
  180. //    u_int16_t    identification;        // from IP header
  181. //    u_int16_t    fragmentOffset;
  182. //    u_int32_t    seqInitial;            // used to offset seq and ack #'s
  183. //    int16_t        seqOffset;            //   for content masquerading
  184. //    u_int32_t    seqInitial2;        // used to offset seq and ack #'s
  185. //    int16_t        seqOffset2;            //   for content masquerading
  186. //    int16_t        seqOffsetPrev;
  187. } KFT_natEntry_t;
  188.  
  189. // Values for NAT entry Flags
  190. #define kNatFlagFINLocal        1        // Seen TCP FIN from local host
  191. #define kNatFlagFINPeer            2        // Seen TCP FIN from peer
  192. #define kNatFlagFINAckLocal        4        // Seen TCP FIN Ack from local host
  193. #define kNatFlagFINAckPeer        8        // Seen TCP FIN Ack from peer
  194. #define kNatFlagNonSyn            0x80    // Sent more than a Syn
  195. #define kNatFlagDelete            0x0100
  196. #define kNatFlagUpdate             0x0200
  197. #define kNatFlagRemoveAll        0x0400
  198.  
  199. // ---------------------------------------------------------------------------
  200. // Fragment Entry
  201. // ---------------------------------------------------------------------------
  202. // define IP Fragment table
  203. typedef struct KFT_fragmentId {
  204.     u_int16_t pad;
  205.     u_int16_t identification;
  206.     u_int32_t srcAddress;
  207. } KFT_fragmentId_t;
  208.  
  209. typedef struct KFT_fragmentEntry {
  210.     KFT_fragmentId_t fragment;
  211.     u_int32_t    lastTime;            // NSTimeInterval since 1970 (in seconds)
  212.     u_int16_t    srcPort;            // source and dest ports needed to lookup connection entry
  213.     u_int16_t    dstPort;            // for subsequent fragments
  214. } KFT_fragmentEntry_t;
  215.  
  216.  
  217. // ---------------------------------------------------------------------------
  218. // Connection Entry
  219. // ---------------------------------------------------------------------------
  220. // define connection table entry used to maintain connection state
  221.  
  222. // track sequence numbers as a linked list off the connection entry so we can manipulate out of order
  223. // keep separate lists for connection packets recieved (r) and sent (s)
  224. typedef struct KFT_seqEntry {
  225.     void* next;            // next seqEntry if any
  226.     void* prev;        // prev seqEntry if any
  227.     u_int32_t seqNext;  // sent+size+SYN+FIN, next byte in sequence, or ack number for this segment
  228.     u_int32_t ackWin;   // advertized window sent with ack of this segment (used for retransmission and removal)
  229.     u_int16_t dup;        // count duplicates sent
  230.     u_int16_t flags;
  231.     double sentTime;
  232.     double ackTime;
  233.     double deliverTime;
  234. } KFT_seqEntry_t;
  235. #define kSeqFlag_sent   1
  236. #define kSeqFlag_ack   2
  237. #define kSeqFlag_deliver  4
  238.  
  239. typedef struct KFT_seqList {
  240.     KFT_seqEntry_t* head;
  241.     KFT_seqEntry_t* tail;
  242.     u_int32_t count;
  243. } KFT_seqList_t;
  244.  
  245. typedef struct KFT_connectionInfo {        // one for each direction received (r) and sent (s)
  246.     double rtt;                // smoothed rtt estimate for my packets
  247.     u_int32_t intervalBytes; // number of bytes sent so far this interval
  248.     u_int32_t targetBytes;  // target bytes to send this interval if rate limited
  249.     u_int32_t seqNext;        // seq of last byte sent plus 1 or ack number expected for last segment
  250.     u_int32_t ackReceived;  // highest ack delivered from peer or local ISN
  251.     int32_t rwin;            // receive window from peer (for ackReceived)
  252.     int32_t rwinUsed;        // highest seq# sent - ackReceived
  253.     int32_t rwinRemain;   // rwin - rwinUsed
  254.     u_int16_t scale;        // window scale factor
  255.     u_int16_t mss;            // mss option sent to peer
  256.     u_int16_t rateLimitRule; // index of rule in filter table (for rate limit bandwidth bytes/sec)
  257.     u_int16_t pad;
  258. } KFT_connectionInfo_t;
  259.  
  260. typedef struct KFT_connectionEntry {
  261.     KFT_connectionEndpoint_t remote;
  262.     KFT_connectionEndpoint_t local;
  263.     KFT_stat_t    dataIn;
  264.     KFT_stat_t    dataOut;
  265.     u_int32_t    lastTime;
  266.     u_int32_t    firstTime;
  267.     u_int32_t    lastReport;
  268.     u_int32_t    flags;
  269.     u_int16_t    dropCount;
  270.     // connection state
  271.     u_int32_t   seqFINLocal;        // seq# to check for last ACK
  272.     u_int32_t   seqFINPeer;
  273.     // bandwidth management
  274.     double intervalStart;
  275.     KFT_connectionInfo_t rInfo;
  276.     KFT_connectionInfo_t sInfo;
  277.     KFT_seqList_t rSeqList;
  278.     KFT_seqList_t sSeqList;
  279.     // soure aware routing
  280.     u_long filterID;        // interface where packet arrived from
  281.     u_int8_t remoteHW[6];
  282.     u_int8_t localHW[6];
  283.     //char bsdName[kBSDNameLength];    // corresponding interface name (CString)
  284. } KFT_connectionEntry_t;
  285.  
  286. // Values for Connection entry Flags
  287. #define kConnectionFlagFINLocal            1        // Seen TCP FIN from local host
  288. #define kConnectionFlagFINPeer            2        // Seen TCP FIN from peer
  289. #define kConnectionFlagFINAckLocal        4        // Seen TCP FIN Ack from local host
  290. #define kConnectionFlagFINAckPeer        8        // Seen TCP FIN Ack from peer
  291. #define kConnectionFlagClosed            0x40    // Closed by firewall
  292. #define kConnectionFlagNonSyn            0x80    // Sent more than a Syn
  293. #define kConnectionFlagDelete            0x0100
  294. #define kConnectionFlagUpdate             0x0200
  295.  
  296. // Ethernet Address
  297. typedef struct {
  298.     u_int8_t octet[6];
  299. } EthernetAddress_t;
  300.  
  301. // ---------------------------------------------------------------------------
  302. // Bridge Entry
  303. // ---------------------------------------------------------------------------
  304. // define bridge table entry used to do Ethernet bridging (16 bytes/entry)
  305. typedef struct KFT_bridgeEntry {
  306.     u_long filterID;            // data link (port) packet arrived on
  307.     u_int32_t lastTime;            // last time we saw or used this address (tv_sec)
  308.     EthernetAddress_t ea;        // hardware MAC address
  309.     u_int8_t portInfo;            // count port conflicts
  310.     u_int8_t flags;
  311. } KFT_bridgeEntry_t;
  312. #define kBridgeFlagOutbound            0x01    // direction of packet seen
  313. #define kBridgeFlagDelete            0x40
  314. #define kBridgeFlagUpdate             0x80
  315. #define kMaskConflictCount            0x0F
  316. #define kMaskAttachIndex            0xF0
  317.  
  318.  
  319. // ---------------------------------------------------------------------------
  320. // packet data
  321. // ---------------------------------------------------------------------------
  322. // structure used to pass around packets
  323. typedef struct KFT_packetData {
  324.     struct ifnet **ifnet_ptr;
  325.     struct mbuf    **mbuf_ptr;    // pointer to mbuf chain
  326.     char    **frame_ptr;        // frame pointer
  327.     attach_t* myAttach;    // pointer to interface attach instance for this datagram
  328.     KFT_filterEntry_t* kftEntry;    // matching filter entry
  329.     KFT_triggerEntry_t* triggerEntry;    // matching trigger entry
  330.     KFT_connectionEntry_t* connectionEntry;    // matching connection state entry
  331.     KFT_natEntry_t* natEntry;    // matching nat entry if any
  332.     u_int8_t* datagram;    // start of datagram in first mbuf
  333.     int segmentLen;        // length of data segment
  334.     u_int16_t   rateLimitInRule;
  335.     u_int16_t   rateLimitOutRule;
  336.     u_int16_t    ipOffset;    // integer offset to start of IP datagram within mbuf data
  337.     u_int16_t    matchOffset;    // base for relative data content matching
  338.     u_int16_t    textOffset;        // display what we found
  339.     u_int16_t    textLength;
  340.     u_int8_t    direction;    // 0=output, 1=input (from which intercept was called)
  341.     u_int8_t ifType;        // from ifnet 
  342.     u_int8_t ifHeaderLen;    // length of frame header
  343.     u_int8_t ipHeaderLen;
  344.     u_int8_t transportHeaderLen;
  345.     u_int8_t leafAction;    // remember leaf action for children if any
  346.     u_int8_t dontLog;        // mark as not to be logged (1 = dont log)
  347.     u_int8_t bridgeNonIP;            // Ethernet and not IP
  348. } KFT_packetData_t;
  349.  
  350. #endif
  351.