2021-05-26 13:02:35 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""NAT44 ED output-feature tests"""
|
|
|
|
|
|
|
|
import random
|
|
|
|
import unittest
|
2024-07-23 01:28:19 -04:00
|
|
|
import struct
|
|
|
|
import socket
|
2023-08-31 00:47:44 -04:00
|
|
|
from scapy.layers.inet import Ether, IP, TCP
|
2021-05-26 13:02:35 +02:00
|
|
|
from scapy.packet import Raw
|
|
|
|
from scapy.data import IP_PROTOS
|
2023-08-31 00:47:44 -04:00
|
|
|
from framework import VppTestCase
|
|
|
|
from asfframework import VppTestRunner
|
2021-05-26 13:02:35 +02:00
|
|
|
from vpp_papi import VppEnum
|
2024-03-11 10:38:46 +00:00
|
|
|
from config import config
|
2021-05-26 13:02:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_nat44_ed_in2out_worker_index(ip, vpp_worker_count):
|
|
|
|
if 0 == vpp_worker_count:
|
|
|
|
return 0
|
|
|
|
numeric = socket.inet_aton(ip)
|
|
|
|
numeric = struct.unpack("!L", numeric)[0]
|
|
|
|
numeric = socket.htonl(numeric)
|
|
|
|
h = numeric + (numeric >> 8) + (numeric >> 16) + (numeric >> 24)
|
|
|
|
return 1 + h % vpp_worker_count
|
|
|
|
|
|
|
|
|
2024-03-11 10:38:46 +00:00
|
|
|
@unittest.skipIf("nat" in config.excluded_plugins, "Exclude NAT plugin tests")
|
2021-05-26 13:02:35 +02:00
|
|
|
class TestNAT44EDOutput(VppTestCase):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""NAT44 ED output feature Test Case"""
|
|
|
|
|
2021-05-26 13:02:35 +02:00
|
|
|
max_sessions = 1024
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super().setUpClass()
|
|
|
|
cls.create_pg_interfaces(range(2))
|
|
|
|
cls.interfaces = list(cls.pg_interfaces)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
super().tearDownClass()
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
|
|
|
for i in self.interfaces:
|
|
|
|
i.admin_up()
|
|
|
|
i.config_ip4()
|
|
|
|
i.resolve_arp()
|
2022-04-26 19:02:15 +02:00
|
|
|
self.vapi.nat44_ed_plugin_enable_disable(sessions=self.max_sessions, enable=1)
|
2021-05-26 13:02:35 +02:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
if not self.vpp_dead:
|
|
|
|
self.logger.debug(self.vapi.cli("show nat44 sessions"))
|
|
|
|
super().tearDown()
|
|
|
|
if not self.vpp_dead:
|
|
|
|
for i in self.pg_interfaces:
|
|
|
|
i.unconfig_ip4()
|
|
|
|
i.admin_down()
|
|
|
|
self.vapi.nat44_ed_plugin_enable_disable(enable=0)
|
|
|
|
|
|
|
|
def test_static_dynamic(self):
|
2022-04-26 19:02:15 +02:00
|
|
|
"""Create static mapping which matches existing dynamic mapping"""
|
2021-05-26 13:02:35 +02:00
|
|
|
|
2022-02-15 11:56:07 -08:00
|
|
|
config = self.vapi.nat44_show_running_config()
|
|
|
|
old_timeouts = config.timeouts
|
2021-05-26 13:02:35 +02:00
|
|
|
new_transitory = 2
|
|
|
|
self.vapi.nat_set_timeouts(
|
|
|
|
udp=old_timeouts.udp,
|
|
|
|
tcp_established=old_timeouts.tcp_established,
|
|
|
|
icmp=old_timeouts.icmp,
|
2022-04-26 19:02:15 +02:00
|
|
|
tcp_transitory=new_transitory,
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
|
|
|
|
local_host = self.pg0.remote_ip4
|
|
|
|
remote_host = self.pg1.remote_ip4
|
|
|
|
nat_intf = self.pg1
|
|
|
|
outside_addr = nat_intf.local_ip4
|
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
self.vapi.nat44_add_del_address_range(
|
|
|
|
first_ip_address=outside_addr,
|
|
|
|
last_ip_address=outside_addr,
|
|
|
|
vrf_id=0xFFFFFFFF,
|
|
|
|
is_add=1,
|
|
|
|
flags=0,
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
self.vapi.nat44_interface_add_del_feature(
|
2022-04-26 19:02:15 +02:00
|
|
|
sw_if_index=self.pg0.sw_if_index, is_add=1
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
self.vapi.nat44_interface_add_del_feature(
|
|
|
|
sw_if_index=self.pg0.sw_if_index,
|
2022-04-26 19:02:15 +02:00
|
|
|
flags=VppEnum.vl_api_nat_config_flags_t.NAT_IS_INSIDE,
|
|
|
|
is_add=1,
|
|
|
|
)
|
2022-02-15 11:56:07 -08:00
|
|
|
self.vapi.nat44_ed_add_del_output_interface(
|
2022-04-26 19:02:15 +02:00
|
|
|
sw_if_index=self.pg1.sw_if_index, is_add=1
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
|
|
|
|
thread_index = get_nat44_ed_in2out_worker_index(
|
2022-04-26 19:02:15 +02:00
|
|
|
local_host, self.vpp_worker_count
|
|
|
|
)
|
|
|
|
port_per_thread = int((0xFFFF - 1024) / max(1, self.vpp_worker_count))
|
2021-05-26 13:02:35 +02:00
|
|
|
local_sport = 1024 + random.randint(1, port_per_thread)
|
|
|
|
if self.vpp_worker_count > 0:
|
|
|
|
local_sport += port_per_thread * (thread_index - 1)
|
|
|
|
|
|
|
|
remote_dport = 10000
|
|
|
|
|
|
|
|
pg0 = self.pg0
|
|
|
|
pg1 = self.pg1
|
|
|
|
|
|
|
|
# first setup a dynamic TCP session
|
|
|
|
|
|
|
|
# SYN packet in->out
|
2022-04-26 19:02:15 +02:00
|
|
|
p = (
|
|
|
|
Ether(src=pg0.remote_mac, dst=pg0.local_mac)
|
|
|
|
/ IP(src=local_host, dst=remote_host)
|
|
|
|
/ TCP(sport=local_sport, dport=remote_dport, flags="S")
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
p = self.send_and_expect(pg0, [p], pg1)[0]
|
|
|
|
|
|
|
|
self.assertEqual(p[IP].src, outside_addr)
|
|
|
|
self.assertEqual(p[TCP].sport, local_sport)
|
|
|
|
outside_port = p[TCP].sport
|
|
|
|
|
|
|
|
# SYN+ACK packet out->in
|
2022-04-26 19:02:15 +02:00
|
|
|
p = (
|
|
|
|
Ether(src=pg1.remote_mac, dst=pg1.local_mac)
|
|
|
|
/ IP(src=remote_host, dst=outside_addr)
|
|
|
|
/ TCP(sport=remote_dport, dport=outside_port, flags="SA")
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
self.send_and_expect(pg1, [p], pg0)
|
|
|
|
|
|
|
|
# ACK packet in->out
|
2022-04-26 19:02:15 +02:00
|
|
|
p = (
|
|
|
|
Ether(src=pg0.remote_mac, dst=pg0.local_mac)
|
|
|
|
/ IP(src=local_host, dst=remote_host)
|
|
|
|
/ TCP(sport=local_sport, dport=remote_dport, flags="A")
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
self.send_and_expect(pg0, [p], pg1)
|
|
|
|
|
|
|
|
# now we have a session up, create a conflicting static mapping
|
|
|
|
self.vapi.nat44_add_del_static_mapping(
|
|
|
|
is_add=1,
|
|
|
|
local_ip_address=local_host,
|
|
|
|
external_ip_address=outside_addr,
|
2022-04-26 19:02:15 +02:00
|
|
|
external_sw_if_index=0xFFFFFFFF,
|
2021-05-26 13:02:35 +02:00
|
|
|
local_port=local_sport,
|
|
|
|
external_port=outside_port,
|
|
|
|
protocol=IP_PROTOS.tcp,
|
2022-04-26 19:02:15 +02:00
|
|
|
flags=VppEnum.vl_api_nat_config_flags_t.NAT_IS_OUT2IN_ONLY,
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
|
|
|
|
sessions = self.vapi.nat44_user_session_dump(local_host, 0)
|
|
|
|
self.assertEqual(1, len(sessions))
|
|
|
|
|
|
|
|
# now send some more data over existing session - it should pass
|
|
|
|
|
|
|
|
# in->out
|
2022-04-26 19:02:15 +02:00
|
|
|
p = (
|
|
|
|
Ether(src=pg0.remote_mac, dst=pg0.local_mac)
|
|
|
|
/ IP(src=local_host, dst=remote_host)
|
|
|
|
/ TCP(sport=local_sport, dport=remote_dport)
|
|
|
|
/ Raw("zippity zap")
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
self.send_and_expect(pg0, [p], pg1)
|
|
|
|
|
|
|
|
# out->in
|
2022-04-26 19:02:15 +02:00
|
|
|
p = (
|
|
|
|
Ether(src=pg1.remote_mac, dst=pg1.local_mac)
|
|
|
|
/ IP(src=remote_host, dst=outside_addr)
|
|
|
|
/ TCP(sport=remote_dport, dport=outside_port)
|
|
|
|
/ Raw("flippity flop")
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
self.send_and_expect(pg1, [p], pg0)
|
|
|
|
|
|
|
|
# now close the session
|
|
|
|
|
|
|
|
# FIN packet in -> out
|
2022-04-26 19:02:15 +02:00
|
|
|
p = (
|
|
|
|
Ether(src=pg0.remote_mac, dst=pg0.local_mac)
|
|
|
|
/ IP(src=local_host, dst=remote_host)
|
|
|
|
/ TCP(sport=local_sport, dport=remote_dport, flags="FA", seq=100, ack=300)
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
self.send_and_expect(pg0, [p], pg1)
|
|
|
|
|
|
|
|
# FIN+ACK packet out -> in
|
2022-04-26 19:02:15 +02:00
|
|
|
p = (
|
|
|
|
Ether(src=pg1.remote_mac, dst=pg1.local_mac)
|
|
|
|
/ IP(src=remote_host, dst=outside_addr)
|
|
|
|
/ TCP(sport=remote_dport, dport=outside_port, flags="FA", seq=300, ack=101)
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
self.send_and_expect(pg1, [p], pg0)
|
|
|
|
|
|
|
|
# ACK packet in -> out
|
2022-04-26 19:02:15 +02:00
|
|
|
p = (
|
|
|
|
Ether(src=pg0.remote_mac, dst=pg0.local_mac)
|
|
|
|
/ IP(src=local_host, dst=remote_host)
|
|
|
|
/ TCP(sport=local_sport, dport=remote_dport, flags="A", seq=101, ack=301)
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
self.send_and_expect(pg0, [p], pg1)
|
|
|
|
|
|
|
|
# session now in transitory timeout
|
|
|
|
# try SYN packet in->out - should be dropped
|
2022-04-26 19:02:15 +02:00
|
|
|
p = (
|
|
|
|
Ether(src=pg0.remote_mac, dst=pg0.local_mac)
|
|
|
|
/ IP(src=local_host, dst=remote_host)
|
|
|
|
/ TCP(sport=local_sport, dport=remote_dport, flags="S")
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
pg0.add_stream(p)
|
|
|
|
self.pg_enable_capture()
|
|
|
|
self.pg_start()
|
|
|
|
|
|
|
|
self.sleep(new_transitory, "wait for transitory timeout")
|
|
|
|
pg0.assert_nothing_captured(0)
|
|
|
|
|
|
|
|
# session should still exist
|
|
|
|
sessions = self.vapi.nat44_user_session_dump(pg0.remote_ip4, 0)
|
|
|
|
self.assertEqual(1, len(sessions))
|
|
|
|
|
|
|
|
# send FIN+ACK packet in->out - will cause session to be wiped
|
|
|
|
# but won't create a new session
|
2022-04-26 19:02:15 +02:00
|
|
|
p = (
|
ethernet: check destination mac for L3 in ethernet-input node
When the NIC does not support mac filter, we rely on ethernet-input
node to do the destination mac check, ie, when the interface is in L3,
the mac address for the packet must be the mac address of the
interface where the packet arrives. This works fine in ethernet-input
node when all packets in the frame might have different interfaces, ie,
ETH_INPUT_FRAME_F_SINGLE_SW_IF_ID is not set in the frame. However,
when all packets are having the same interface,
ETH_INPUT_FRAME_F_SINGLE_SW_IF_ID is set, ethernet-input node goes
through the optimized routine eth_input_single_int -> eth_input_process_frame.
That is where dmac check has a bug when all packets in the frame are
either, ip4, ip6, or mpls without vlan tags. Because without vlan tags,
the code handles all packets in fast path and ignores dmac check.
With vlan tags, the code goes to slow path where dmac check is handled
properly.
The fix is to check if we have a bad dmac in the fast path and force the
code to go to slow path which will handle dmac check properly.
Also do a wholesale correction on all the testcases which do not use
the proper dmac when sending L3 packets.
Type: fix
Change-Id: I73153a805cecdc24c4eefcc781676de04737ae2c
Signed-off-by: Steven Luong <sluong@cisco.com>
2024-04-19 09:49:20 -07:00
|
|
|
Ether(src=pg1.remote_mac, dst=pg1.local_mac)
|
2022-04-26 19:02:15 +02:00
|
|
|
/ IP(src=local_host, dst=remote_host)
|
|
|
|
/ TCP(sport=local_sport, dport=remote_dport, flags="FA", seq=300, ack=101)
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
pg1.add_stream(p)
|
|
|
|
self.pg_enable_capture()
|
|
|
|
self.pg_start()
|
|
|
|
pg0.assert_nothing_captured(0)
|
|
|
|
|
|
|
|
sessions = self.vapi.nat44_user_session_dump(pg0.remote_ip4, 0)
|
|
|
|
self.assertEqual(0, len(sessions))
|
|
|
|
|
|
|
|
# create a new session and make sure the outside port is remapped
|
|
|
|
# SYN packet in->out
|
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
p = (
|
|
|
|
Ether(src=pg0.remote_mac, dst=pg0.local_mac)
|
|
|
|
/ IP(src=local_host, dst=remote_host)
|
|
|
|
/ TCP(sport=local_sport, dport=remote_dport, flags="S")
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
p = self.send_and_expect(pg0, [p], pg1)[0]
|
|
|
|
|
|
|
|
self.assertEqual(p[IP].src, outside_addr)
|
|
|
|
self.assertNotEqual(p[TCP].sport, local_sport)
|
|
|
|
|
|
|
|
# make sure static mapping works and creates a new session
|
|
|
|
# SYN packet out->in
|
2022-04-26 19:02:15 +02:00
|
|
|
p = (
|
|
|
|
Ether(src=pg1.remote_mac, dst=pg1.local_mac)
|
|
|
|
/ IP(src=remote_host, dst=outside_addr)
|
|
|
|
/ TCP(sport=remote_dport, dport=outside_port, flags="S")
|
|
|
|
)
|
2021-05-26 13:02:35 +02:00
|
|
|
self.send_and_expect(pg1, [p], pg0)
|
|
|
|
|
|
|
|
sessions = self.vapi.nat44_user_session_dump(pg0.remote_ip4, 0)
|
|
|
|
self.assertEqual(2, len(sessions))
|
|
|
|
|
|
|
|
|
2022-04-26 19:02:15 +02:00
|
|
|
if __name__ == "__main__":
|
2021-05-26 13:02:35 +02:00
|
|
|
unittest.main(testRunner=VppTestRunner)
|