13 typedef struct sKeyValue tKeyValue;
14 typedef struct sFirewallMod tFirewallMod;
15 typedef struct sModuleRule tModuleRule;
16 typedef struct sRule tRule;
17 typedef struct sChain tChain;
30 int (*Match)(tModuleRule *Rule, int AddrType,
31 const void *Src, const void *Dest,
32 Uint8 Type, Uint32 Flags,
33 size_t Length, const void *Data);
35 tModuleRule *(*Create)(tKeyValue *Params);
51 int PacketCount; // Number of packets seen
52 int ByteCount; // Number of bytes seen (IP Payload bytes)
54 int bInvertSource; // Boolean NOT flag on source
55 void *Source; // Source address bytes
56 int SourceMask; // Source address mask bits
58 int bInvertDest; // Boolean NOT flag on destination
59 void *Dest; // Destination address bytes
60 int DestMask; // Destination address mask bits
62 tModuleRule *Modules; // Modules loaded for this rule
64 char Target[]; // Target rule name
78 int IPTables_TestChain(
80 const int AddressType,
81 const void *Src, const void *Dest,
82 Uint8 Type, Uint32 Flags,
83 size_t Length, const void *Data
87 tChain *gapFirewall_Chains[MAX_ADDRTYPE+1];
88 tChain gFirewall_DROP = {.Name="DROP"};
89 tChain gFirewall_ACCEPT = {.Name="ACCEPT"};
90 tChain gFirewall_RETURN = {.Name="RETURN"};
94 * \brief Apply a rule to a packet
95 * \return -1 for no match, -2 for RETURN, eFirewallAction otherwise
98 tRule *Rule, int AddrType,
99 const void *Src, const void *Dest,
100 Uint8 Type, Uint32 Flags,
101 size_t Length, const void *Data)
104 // Check if source doesn't match
105 if( !IPStack_CompareAddress(AddrType, Src, Rule->Source, Rule->SourceMask) == !Rule->bInvertSource )
107 // Check if destination doesn't match
108 if( !IPStack_CompareAddress(AddrType, Dest, Rule->Dest, Rule->DestMask) == !Rule->bInvertDest )
111 // TODO: Handle modules (UDP/TCP/etc)
112 tModuleRule *modrule;
113 for( modrule = Rule->Modules; modrule; modrule = modrule->Next )
115 if( !modrule->Mod->Match ) continue;
116 rv = modrule->Mod->Match(modrule, AddrType, Src, Dest, Type, Flags, Length, Data);
117 if(rv != 0) return rv; // No match / action
121 Rule->PacketCount ++;
122 Rule->ByteCount += Length;
124 return IPTables_TestChain(Rule->Target, AddrType, Src, Dest, Type, Flags, Length, Data);
128 * \brief Tests an IPv4 chain on a packet
129 * \return Boolean Disallow (0: Packet Allowed, 1: Drop, 2: Reject, 3: Continue, -1 no match)
131 int IPTables_TestChain(
132 const char *RuleName,
133 const int AddressType,
134 const void *Src, const void *Dest,
135 Uint8 Type, Uint32 Flags,
136 size_t Length, const void *Data
143 if( AddressType >= MAX_ADDRTYPE ) return -1; // Bad address type
145 // Catch builtin targets
146 if(strcmp(RuleName, "") == 0) return -1; // No action
147 if(strcmp(RuleName, "ACCEPT") == 0) return 0; // Accept packet
148 if(strcmp(RuleName, "DROP") == 0) return 1; // Drop packet
149 if(strcmp(RuleName, "RETURN") == 0) return -2; // Return from rule
152 for( chain = gapFirewall_Chains[AddressType]; chain; chain = chain->Next )
154 if( strcmp(chain->Name, RuleName) == 0 )
157 if( !chain ) return -1; // Bad rule name
160 for( rule = chain->FirstRule; rule; rule = rule->Next )
162 rv = IPTables_DoRule(rule, AddressType, Src, Dest, Type, Flags, Length, Data);
165 if( rv == -2 ) // -2 = Return from a chain/table, pretend no match
172 return 0; // Accept all for now