Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import unittest |
| 4 | import ipaddress |
| 5 | from framework import VppTestCase, VppTestRunner |
| 6 | |
| 7 | from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest |
| 8 | from scapy.layers.l2 import Ether |
| 9 | from scapy.packet import Raw |
| 10 | |
| 11 | |
| 12 | class TestNPT66(VppTestCase): |
| 13 | """NPTv6 Test Case""" |
| 14 | |
Ole Troan | 9587d39 | 2023-09-01 14:18:23 +0200 | [diff] [blame] | 15 | extra_vpp_plugin_config = [ |
| 16 | "plugin npt66_plugin.so {enable}", |
| 17 | ] |
| 18 | |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 19 | def setUp(self): |
| 20 | super(TestNPT66, self).setUp() |
| 21 | |
| 22 | # create 2 pg interfaces |
| 23 | self.create_pg_interfaces(range(2)) |
| 24 | |
| 25 | for i in self.pg_interfaces: |
| 26 | i.admin_up() |
| 27 | i.config_ip6() |
| 28 | i.resolve_ndp() |
| 29 | |
| 30 | def tearDown(self): |
| 31 | for i in self.pg_interfaces: |
| 32 | i.unconfig_ip6() |
| 33 | i.admin_down() |
| 34 | super(TestNPT66, self).tearDown() |
| 35 | |
Ole Troan | 34850e0 | 2023-09-01 14:15:39 +0200 | [diff] [blame] | 36 | def send_and_verify(self, internal): |
| 37 | sendif = self.pg0 |
| 38 | recvif = self.pg1 |
| 39 | local_mac = self.pg0.local_mac |
| 40 | remote_mac = self.pg0.remote_mac |
| 41 | src = ipaddress.ip_interface(internal).ip + 1 |
| 42 | dst = self.pg1.remote_ip6 |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 43 | |
| 44 | p = ( |
| 45 | Ether(dst=local_mac, src=remote_mac) |
| 46 | / IPv6(src=src, dst=dst) |
| 47 | / ICMPv6EchoRequest() |
Ole Troan | 34850e0 | 2023-09-01 14:15:39 +0200 | [diff] [blame] | 48 | / Raw(b"Request") |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 49 | ) |
| 50 | rxs = self.send_and_expect(sendif, p, recvif) |
| 51 | for rx in rxs: |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 52 | original_cksum = rx[ICMPv6EchoRequest].cksum |
| 53 | del rx[ICMPv6EchoRequest].cksum |
| 54 | rx = rx.__class__(bytes(rx)) |
| 55 | self.assertEqual(original_cksum, rx[ICMPv6EchoRequest].cksum) |
| 56 | |
Ole Troan | 34850e0 | 2023-09-01 14:15:39 +0200 | [diff] [blame] | 57 | # Generate a replies |
| 58 | reply = ( |
| 59 | Ether(dst=rx[Ether].src, src=local_mac) |
| 60 | / IPv6(src=rx[IPv6].dst, dst=rx[IPv6].src) |
| 61 | / ICMPv6EchoRequest() |
| 62 | / Raw(b"Reply") |
| 63 | ) |
| 64 | |
| 65 | replies = self.send_and_expect(recvif, reply, sendif) |
| 66 | for r in replies: |
| 67 | self.assertEqual(str(p[IPv6].src), r[IPv6].dst) |
| 68 | original_cksum = r[ICMPv6EchoRequest].cksum |
| 69 | del r[ICMPv6EchoRequest].cksum |
| 70 | r = r.__class__(bytes(r)) |
| 71 | self.assertEqual(original_cksum, r[ICMPv6EchoRequest].cksum) |
| 72 | |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 73 | def do_test(self, internal, external): |
| 74 | self.vapi.npt66_binding_add_del( |
| 75 | sw_if_index=self.pg1.sw_if_index, |
| 76 | internal=internal, |
| 77 | external=external, |
| 78 | is_add=True, |
| 79 | ) |
Ole Troan | 34850e0 | 2023-09-01 14:15:39 +0200 | [diff] [blame] | 80 | ## TODO use route api |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 81 | self.vapi.cli(f"ip route add {internal} via {self.pg0.remote_ip6}") |
| 82 | |
Ole Troan | 34850e0 | 2023-09-01 14:15:39 +0200 | [diff] [blame] | 83 | self.send_and_verify(internal) |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 84 | |
| 85 | self.vapi.npt66_binding_add_del( |
| 86 | sw_if_index=self.pg1.sw_if_index, |
| 87 | internal=internal, |
| 88 | external=external, |
| 89 | is_add=False, |
| 90 | ) |
| 91 | |
| 92 | def test_npt66_simple(self): |
| 93 | """Send and receive a packet through NPT66""" |
| 94 | |
Ole Troan | 34850e0 | 2023-09-01 14:15:39 +0200 | [diff] [blame] | 95 | self.do_test("fd00:0000:0000::/48", "2001:4650:c3ed::/48") |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 96 | self.do_test("fc00:1::/48", "2001:db8:1::/48") |
| 97 | self.do_test("fc00:1234::/32", "2001:db8:1::/32") |
| 98 | self.do_test("fc00:1234::/63", "2001:db8:1::/56") |
| 99 | |
| 100 | |
| 101 | if __name__ == "__main__": |
| 102 | unittest.main(testRunner=VppTestRunner) |