@@ -34,9 +34,7 @@ namespace net {
3434 packets_rx_ {Statman::get ().create (Stat::UINT64, inet.ifname () + " .ip4.packets_rx" ).get_uint64 ()},
3535 packets_tx_ {Statman::get ().create (Stat::UINT64, inet.ifname () + " .ip4.packets_tx" ).get_uint64 ()},
3636 packets_dropped_ {Statman::get ().create (Stat::UINT32, inet.ifname () + " .ip4.packets_dropped" ).get_uint32 ()},
37- stack_ {inet},
38- upstream_filter_ {this , &IP4::filter_upstream},
39- downstream_filter_{this , &IP4::filter_downstream}
37+ stack_ {inet}
4038 {}
4139
4240
@@ -50,7 +48,7 @@ namespace net {
5048 }
5149
5250
53- IP4::IP_packet_ptr IP4::filter_upstream (IP4::IP_packet_ptr packet)
51+ IP4::IP_packet_ptr IP4::drop_invalid_in (IP4::IP_packet_ptr packet)
5452 {
5553 IP4::Direction up = IP4::Direction::Upstream;
5654
@@ -73,7 +71,7 @@ namespace net {
7371 }
7472
7573
76- IP4::IP_packet_ptr IP4::filter_downstream (IP4::IP_packet_ptr packet)
74+ IP4::IP_packet_ptr IP4::drop_invalid_out (IP4::IP_packet_ptr packet)
7775 {
7876 // RFC-1122 3.2.1.7, MUST NOT send packet with TTL of 0
7977 if (packet->ip_ttl () == 0 )
@@ -102,7 +100,12 @@ namespace net {
102100 // Stat increment packets received
103101 packets_rx_++;
104102
105- packet = upstream_filter_ (std::move (packet));
103+
104+ packet = drop_invalid_in (std::move (packet));
105+ if (UNLIKELY (packet == nullptr )) return ;
106+
107+ // Enter prerouting chain
108+ packet = stack_.prerouting_chain ()(std::move (packet), stack_);
106109 if (UNLIKELY (packet == nullptr )) return ;
107110
108111 // Drop / forward if my ip address doesn't match dest. or broadcast
@@ -122,6 +125,9 @@ namespace net {
122125 return ;
123126 }
124127
128+ packet = stack_.input_chain ()(std::move (packet), stack_);
129+ if (UNLIKELY (packet == nullptr )) return ;
130+
125131 // Pass packet to it's respective protocol controller
126132 switch (packet->ip_protocol ()) {
127133 case Protocol::ICMPv4:
@@ -152,56 +158,62 @@ namespace net {
152158 void IP4::transmit (Packet_ptr pckt) {
153159 assert ((size_t )pckt->size () > sizeof (header));
154160
155- auto ip4_pckt = static_unique_ptr_cast<PacketIP4>(std::move (pckt));
161+ auto packet = static_unique_ptr_cast<PacketIP4>(std::move (pckt));
162+
163+ packet->make_flight_ready ();
156164
157- ip4_pckt->make_flight_ready ();
165+ packet = stack_.output_chain ()(std::move (packet), stack_);
166+ if (UNLIKELY (packet == nullptr )) return ;
158167
159- ship (std::move (ip4_pckt ));
168+ ship (std::move (packet ));
160169 }
161170
162- void IP4::ship (Packet_ptr pckt)
171+ void IP4::ship (Packet_ptr pckt, addr next_hop )
163172 {
164- auto ip4_pckt = static_unique_ptr_cast<PacketIP4>(std::move (pckt));
173+ auto packet = static_unique_ptr_cast<PacketIP4>(std::move (pckt));
165174
166175 // Send loopback packets right back
167- if (UNLIKELY (stack_.is_loopback (ip4_pckt ->ip_dst ()))) {
176+ if (UNLIKELY (stack_.is_loopback (packet ->ip_dst ()))) {
168177 debug (" <IP4> Destination address is loopback \n " );
169- IP4::receive (std::move (ip4_pckt ));
178+ IP4::receive (std::move (packet ));
170179 return ;
171180 }
172181
173- addr next_hop;
182+ // Filter illegal egress packets
183+ packet = drop_invalid_out (std::move (packet));
184+ if (packet == nullptr ) return ;
174185
175- if (ip4_pckt->ip_dst () != IP4::ADDR_BCAST)
176- {
177- // Create local and target subnets
178- addr target = ip4_pckt->ip_dst () & stack_.netmask ();
179- addr local = stack_.ip_addr () & stack_.netmask ();
186+ packet = stack_.postrouting_chain ()(std::move (packet), stack_);
187+ if (UNLIKELY (packet == nullptr )) return ;
180188
181- // Compare subnets to know where to send packet
182- next_hop = target == local ? ip4_pckt->ip_dst () : stack_.gateway ();
183189
184- debug (" <IP4 TOP> Next hop for %s, (netmask %s, local IP: %s, gateway: %s) == %s\n " ,
185- ip4_pckt->ip_dst ().str ().c_str (),
186- stack_.netmask ().str ().c_str (),
187- stack_.ip_addr ().str ().c_str (),
188- stack_.gateway ().str ().c_str (),
189- next_hop.str ().c_str ());
190+ if (next_hop == 0 ) {
191+ if (UNLIKELY (packet->ip_dst () == IP4::ADDR_BCAST)) {
192+ next_hop = IP4::ADDR_BCAST;
193+ } else {
194+ // Create local and target subnets
195+ addr target = packet->ip_dst () & stack_.netmask ();
196+ addr local = stack_.ip_addr () & stack_.netmask ();
190197
191- } else {
192- next_hop = IP4::ADDR_BCAST;
193- }
198+ // Compare subnets to know where to send packet
199+ next_hop = target == local ? packet->ip_dst () : stack_.gateway ();
194200
195- // Filter illegal egress packets
196- ip4_pckt = upstream_filter_ (std::move (ip4_pckt));
197- if (ip4_pckt == nullptr ) return ;
201+ debug (" <IP4 TOP> Next hop for %s, (netmask %s, local IP: %s, gateway: %s) == %s\n " ,
202+ packet->ip_dst ().str ().c_str (),
203+ stack_.netmask ().str ().c_str (),
204+ stack_.ip_addr ().str ().c_str (),
205+ stack_.gateway ().str ().c_str (),
206+ next_hop.str ().c_str ());
207+
208+ }
209+ }
198210
199211 // Stat increment packets transmitted
200212 packets_tx_++;
201213
202- debug (" <IP4> Transmitting packet, layer begin: buf + %li\n " , ip4_pckt ->layer_begin () - ip4_pckt ->buf ());
214+ debug (" <IP4> Transmitting packet, layer begin: buf + %li\n " , packet ->layer_begin () - packet ->buf ());
203215
204- linklayer_out_ (std::move (ip4_pckt ), next_hop);
216+ linklayer_out_ (std::move (packet ), next_hop);
205217 }
206218
207219} // < namespace net
0 commit comments