nat: fix counters increment for output feature 18/33018/8
authorAlexander Chernavin <achernavin@netgate.com>
Tue, 6 Jul 2021 10:08:26 +0000 (06:08 -0400)
committerMatthew Smith <mgsmith@netgate.com>
Tue, 17 Aug 2021 21:32:14 +0000 (21:32 +0000)
commit4de12b9c62757f7297c6ac5c7eedff17082f4ee8
tree9435894aa5f601220efde2d9a414586cd9a79cad
parentb93782b36643714a8dcb53e2e396da642b1b0053
nat: fix counters increment for output feature

Type: fix

The NAT plugin stores packet counters and a counter represents a vector
indexed by interface index. When an interface is assigned a NAT role,
the counters are validated to be long enough for the given interface
index.

When a packet traverses NAT in2out and output feature is disabled, the
appropriate counters are updated by the RX interface index. In this
case, translation happens on the inside interface and its index was
ensured to be valid in all of the counters during NAT role assignment.

When a packet traverses NAT in2out and output feature is enabled, the
appropriate counters are updated by the RX interface index too. In this
case, translation happens on the outside interface and the packet could
be received on any interface, even with no NAT role assigned. If that's
the case and its index is greater than the greatest index validated in
the counters, a new counter value will be written to memory that does
not belong to the counter. As a result, a crash will occur at some
point.

With this change, use TX interface index to update the counters when
output feature is enabled. TX interface is an actual interface where
translation happens and its index is always valid in the counters.

Signed-off-by: Alexander Chernavin <achernavin@netgate.com>
Change-Id: I53a52af949fe96419e1b5fef4134ab4062198f51
src/plugins/nat/nat44-ed/nat44_ed_in2out.c
src/plugins/nat/nat44-ei/nat44_ei_hairpinning.c
src/plugins/nat/nat44-ei/nat44_ei_in2out.c
test/test_nat44_ed.py