IPStack - Fiddling with firewall code
[tpg/acess2.git] / Modules / IPStack / firewall.c
1 /*
2  * Acess2 IP Stack
3  * - Firewall Rules
4  */
5 #include "ipstack.h"
6 #include "firewall.h"
7
8 // === IMPORTS ===
9
10 // === TYPES ===
11 typedef struct sFirewallMod     tFirewallMod;
12 typedef struct sModuleRule      tModuleRule;
13 typedef struct sRule    tRule;
14 typedef struct sChain   tChain;
15
16 struct sModuleRule
17 {
18         tModuleRule     *Next;
19         
20         tFirewallMod    *Mod;
21         
22         char    Data[];
23 };
24
25 struct sRule
26 {
27         tRule   *Next;
28         
29          int    PacketCount;    // Number of packets seen
30          int    ByteCount;              // Number of bytes seen (IP Payload bytes)
31         
32          int    bInvertSource;
33         void    *Source;
34          int    SourceMask;
35          
36          int    bInvertDest;
37         void    *Dest;
38          int    DestMask;
39         
40         tModuleRule     *Modules;
41         
42         char    Action[];       // Target rule name
43 };
44
45 struct sChain
46 {
47         tChain  *Next;
48         
49         tRule   *FirstRule;
50         tRule   *LastRule;
51         
52         char    Name[];
53 };
54
55 // === PROTOTYPES ===
56  int    IPTables_TestChain(
57         const char *RuleName,
58         const int AddressType,
59         const void *Src, const void *Dest,
60         Uint8 Type, Uint32 Flags,
61         size_t Length, const void *Data
62         );
63
64 // === GLOBALS ===
65 tChain  *gapFirewall_Chains[10];
66 tChain  gFirewall_DROP = {.Name="DROP"};
67 tChain  gFirewall_ACCEPT = {.Name="ACCEPT"};
68 tChain  gFirewall_RETURN = {.Name="RETURN"};
69
70 // === CODE ===
71 int IPTables_DoRule(
72         tRule *Rule, int AddrType,
73         const void *Src, const void *Dest,
74         Uint8 Type, Uint32 Flags,
75         size_t Length, const void *Data)
76 {
77         return 0;
78 }
79
80 /**
81  * \brief Tests an IPv4 chain on a packet
82  * \return Boolean Disallow (0: Packet Allowed, 1: Drop, 2: Reject, 3: Continue)
83  */
84 int IPTables_TestChain(
85         const char *RuleName,
86         const int AddressType,
87         const void *Src, const void *Dest,
88         Uint8 Type, Uint32 Flags,
89         size_t Length, const void *Data
90         )
91 {
92          int    rv;
93         tChain  *chain;
94         tRule   *rule;
95         
96         for( chain = gapFirewall_Chains[AddressType]; chain; chain = chain->Next )
97         {
98                 if( strcmp(chain->Name, RuleName) == 0 )
99                         break;
100         }
101         if( !chain )    return -1;      // Bad rule name
102         
103         // Check the rules
104         for( rule = chain->FirstRule; rule; rule = rule->Next )
105         {
106                 rv = IPTables_DoRule(rule, AddressType, Src, Dest, Type, Flags, Length, Data);
107                 if( rv == -1 )
108                         continue ;
109                 
110                 return rv;
111         }
112         
113         
114         return 0;       // Accept all for now
115 }

UCC git Repository :: git.ucc.asn.au