2020
2121#include < chrono>
2222#include < unordered_set>
23-
23+ # include < list >
2424#include < net/inet_common.hpp>
2525#include < hw/mac_addr.hpp>
2626#include < hw/nic.hpp>
@@ -48,24 +48,25 @@ namespace net {
4848 using resolve_func = delegate<void (typename IPv::addr, Error&)>;
4949 using Vip_list = std::unordered_set<typename IPV::addr>;
5050
51+
5152 // /
5253 // / NETWORK CONFIGURATION
5354 // /
5455
5556 /* * Get IP address of this interface **/
56- virtual typename IPV::addr ip_addr () = 0;
57+ virtual typename IPV::addr ip_addr () const = 0;
5758
5859 /* * Get netmask of this interface **/
59- virtual typename IPV::addr netmask () = 0;
60+ virtual typename IPV::addr netmask () const = 0;
6061
6162 /* * Get default gateway for this interface **/
62- virtual typename IPV::addr gateway () = 0;
63+ virtual typename IPV::addr gateway () const = 0;
6364
6465 /* * Get default dns for this interface **/
65- virtual typename IPV::addr dns_addr () = 0;
66+ virtual typename IPV::addr dns_addr () const = 0;
6667
6768 /* * Get broadcast address for this interface **/
68- virtual typename IPV::addr broadcast_addr () = 0;
69+ virtual typename IPV::addr broadcast_addr () const = 0;
6970
7071 /* * Set default gateway for this interface */
7172 virtual void set_gateway (typename IPV::addr server) = 0;
@@ -113,6 +114,55 @@ namespace net {
113114 virtual bool is_valid_source (typename IPV::addr) = 0;
114115
115116
117+ // /
118+ // / PACKET FILTERING
119+ // /
120+
121+ using Packetfilter = delegate<typename IPV::IP_packet_ptr (typename IPV::IP_packet_ptr, const Stack&)>;
122+
123+ struct Filter_chain {
124+ std::list<Packetfilter> chain;
125+ const char * name;
126+
127+ typename IPV::IP_packet_ptr operator ()(typename IPV::IP_packet_ptr pckt, const Stack& stack) {
128+ int i = 0 ;
129+ for (auto filter : chain) {
130+ i++;
131+ pckt = filter (std::move (pckt), stack);
132+ if (pckt == nullptr ) {
133+ debug (" Packet dropped in %s chain, filter %i \n " , name, i);
134+ // do some logging
135+ return nullptr ;
136+ }
137+ }
138+ return pckt;
139+ }
140+
141+ Filter_chain (const char * chain_name, std::initializer_list<Packetfilter> filters) :
142+ chain{filters},
143+ name{chain_name} {}
144+ };
145+
146+ /* *
147+ * Packet filtering hooks for firewall, NAT, connection tracking etc.
148+ **/
149+
150+ /* * Packets pass through prerouting chain before routing decision */
151+ virtual Filter_chain& prerouting_chain () = 0;
152+
153+ /* * Packets pass through postrouting chain after routing decision */
154+ virtual Filter_chain& postrouting_chain () = 0;
155+
156+ /* * Packets pass through forward chain by forwarder, if enabled */
157+ virtual Filter_chain& forward_chain () = 0;
158+
159+ /* * Packets pass through input chain before hitting protocol handlers */
160+ virtual Filter_chain& input_chain () = 0;
161+
162+ /* * Packets pass through output chain after exiting protocol handlers */
163+ virtual Filter_chain& output_chain () = 0;
164+
165+
116166 // /
117167 // / PROTOCOL OBJECTS
118168 // /
@@ -166,7 +216,7 @@ namespace net {
166216 virtual std::string ifname () const = 0;
167217
168218 /* * Get linklayer address for this interface **/
169- virtual MAC::Addr link_addr () = 0;
219+ virtual MAC::Addr link_addr () const = 0;
170220
171221 /* * Add cache entry to the link / IP address cache */
172222 virtual void cache_link_addr (typename IPV::addr, MAC::Addr) = 0;
@@ -182,7 +232,9 @@ namespace net {
182232 // / ROUTING
183233 // /
184234
185- /* * Set an IP forwarding delegate. E.g. used to enable routing */
235+ /* * Set an IP forwarding delegate. E.g. used to enable routing.
236+ * NOTE: The packet forwarder is expected to call the forward_chain
237+ **/
186238 virtual void set_forward_delg (Forward_delg) = 0;
187239
188240 /* * Assign boolean function to determine if we have route to a given IP */
0 commit comments