Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import unittest |
| 4 | import ipaddress |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 5 | from framework import VppTestCase |
| 6 | from asfframework import VppTestRunner |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 7 | from config import config |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 8 | |
Ole Troan | ff344a9 | 2023-10-12 18:54:55 +0200 | [diff] [blame] | 9 | from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest, ICMPv6DestUnreach |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 10 | from scapy.layers.l2 import Ether |
| 11 | from scapy.packet import Raw |
| 12 | |
| 13 | |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 14 | @unittest.skipIf("npt66" in config.excluded_plugins, "Exclude NPTv6 plugin tests") |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 15 | class TestNPT66(VppTestCase): |
| 16 | """NPTv6 Test Case""" |
| 17 | |
Ole Troan | 9587d39 | 2023-09-01 14:18:23 +0200 | [diff] [blame] | 18 | extra_vpp_plugin_config = [ |
| 19 | "plugin npt66_plugin.so {enable}", |
| 20 | ] |
| 21 | |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 22 | def setUp(self): |
| 23 | super(TestNPT66, self).setUp() |
| 24 | |
| 25 | # create 2 pg interfaces |
| 26 | self.create_pg_interfaces(range(2)) |
| 27 | |
| 28 | for i in self.pg_interfaces: |
| 29 | i.admin_up() |
| 30 | i.config_ip6() |
| 31 | i.resolve_ndp() |
| 32 | |
| 33 | def tearDown(self): |
| 34 | for i in self.pg_interfaces: |
| 35 | i.unconfig_ip6() |
| 36 | i.admin_down() |
| 37 | super(TestNPT66, self).tearDown() |
| 38 | |
Ole Troan | ff344a9 | 2023-10-12 18:54:55 +0200 | [diff] [blame] | 39 | def send_and_verify(self, internal, reply_icmp_error=False): |
Ole Troan | 34850e0 | 2023-09-01 14:15:39 +0200 | [diff] [blame] | 40 | sendif = self.pg0 |
| 41 | recvif = self.pg1 |
| 42 | local_mac = self.pg0.local_mac |
| 43 | remote_mac = self.pg0.remote_mac |
| 44 | src = ipaddress.ip_interface(internal).ip + 1 |
| 45 | dst = self.pg1.remote_ip6 |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 46 | |
| 47 | p = ( |
| 48 | Ether(dst=local_mac, src=remote_mac) |
| 49 | / IPv6(src=src, dst=dst) |
| 50 | / ICMPv6EchoRequest() |
Ole Troan | 34850e0 | 2023-09-01 14:15:39 +0200 | [diff] [blame] | 51 | / Raw(b"Request") |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 52 | ) |
Ole Troan | ff344a9 | 2023-10-12 18:54:55 +0200 | [diff] [blame] | 53 | # print('Sending packet') |
| 54 | # p.show2() |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 55 | rxs = self.send_and_expect(sendif, p, recvif) |
| 56 | for rx in rxs: |
Ole Troan | ff344a9 | 2023-10-12 18:54:55 +0200 | [diff] [blame] | 57 | # print('Received packet') |
| 58 | # rx.show2() |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 59 | original_cksum = rx[ICMPv6EchoRequest].cksum |
| 60 | del rx[ICMPv6EchoRequest].cksum |
| 61 | rx = rx.__class__(bytes(rx)) |
| 62 | self.assertEqual(original_cksum, rx[ICMPv6EchoRequest].cksum) |
| 63 | |
Ole Troan | 34850e0 | 2023-09-01 14:15:39 +0200 | [diff] [blame] | 64 | # Generate a replies |
Ole Troan | ff344a9 | 2023-10-12 18:54:55 +0200 | [diff] [blame] | 65 | if reply_icmp_error: |
| 66 | # print('Generating an ICMP error message') |
| 67 | reply = ( |
| 68 | Ether(dst=rx[Ether].src, src=local_mac) |
| 69 | / IPv6(src=rx[IPv6].dst, dst=rx[IPv6].src) |
| 70 | / ICMPv6DestUnreach() |
| 71 | / rx[IPv6] |
| 72 | ) |
| 73 | # print('Sending ICMP error message reply') |
| 74 | # reply.show2() |
| 75 | replies = self.send_and_expect(recvif, reply, sendif) |
| 76 | for r in replies: |
| 77 | # print('Received ICMP error message reply on the other side') |
| 78 | # r.show2() |
| 79 | self.assertEqual(str(p[IPv6].src), r[IPv6].dst) |
| 80 | original_cksum = r[ICMPv6EchoRequest].cksum |
| 81 | del r[ICMPv6EchoRequest].cksum |
| 82 | r = r.__class__(bytes(r)) |
| 83 | self.assertEqual(original_cksum, r[ICMPv6EchoRequest].cksum) |
Ole Troan | 34850e0 | 2023-09-01 14:15:39 +0200 | [diff] [blame] | 84 | |
Ole Troan | ff344a9 | 2023-10-12 18:54:55 +0200 | [diff] [blame] | 85 | else: |
| 86 | reply = ( |
| 87 | Ether(dst=rx[Ether].src, src=local_mac) |
| 88 | / IPv6(src=rx[IPv6].dst, dst=rx[IPv6].src) |
| 89 | / ICMPv6EchoRequest() |
| 90 | / Raw(b"Reply") |
| 91 | ) |
Ole Troan | 34850e0 | 2023-09-01 14:15:39 +0200 | [diff] [blame] | 92 | |
Ole Troan | ff344a9 | 2023-10-12 18:54:55 +0200 | [diff] [blame] | 93 | replies = self.send_and_expect(recvif, reply, sendif) |
| 94 | for r in replies: |
Dave Wallace | e3f59e2 | 2023-10-30 17:05:23 -0400 | [diff] [blame] | 95 | # r.show2() |
Ole Troan | ff344a9 | 2023-10-12 18:54:55 +0200 | [diff] [blame] | 96 | self.assertEqual(str(p[IPv6].src), r[IPv6].dst) |
| 97 | original_cksum = r[ICMPv6EchoRequest].cksum |
| 98 | del r[ICMPv6EchoRequest].cksum |
| 99 | r = r.__class__(bytes(r)) |
| 100 | self.assertEqual(original_cksum, r[ICMPv6EchoRequest].cksum) |
| 101 | |
| 102 | def do_test(self, internal, external, reply_icmp_error=False): |
| 103 | """Add NPT66 binding and send packet""" |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 104 | self.vapi.npt66_binding_add_del( |
| 105 | sw_if_index=self.pg1.sw_if_index, |
| 106 | internal=internal, |
| 107 | external=external, |
| 108 | is_add=True, |
| 109 | ) |
Ole Troan | 34850e0 | 2023-09-01 14:15:39 +0200 | [diff] [blame] | 110 | ## TODO use route api |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 111 | self.vapi.cli(f"ip route add {internal} via {self.pg0.remote_ip6}") |
| 112 | |
Ole Troan | ff344a9 | 2023-10-12 18:54:55 +0200 | [diff] [blame] | 113 | self.send_and_verify(internal, reply_icmp_error=reply_icmp_error) |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 114 | |
| 115 | self.vapi.npt66_binding_add_del( |
| 116 | sw_if_index=self.pg1.sw_if_index, |
| 117 | internal=internal, |
| 118 | external=external, |
| 119 | is_add=False, |
| 120 | ) |
| 121 | |
| 122 | def test_npt66_simple(self): |
| 123 | """Send and receive a packet through NPT66""" |
| 124 | |
Ole Troan | 34850e0 | 2023-09-01 14:15:39 +0200 | [diff] [blame] | 125 | self.do_test("fd00:0000:0000::/48", "2001:4650:c3ed::/48") |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 126 | self.do_test("fc00:1::/48", "2001:db8:1::/48") |
| 127 | self.do_test("fc00:1234::/32", "2001:db8:1::/32") |
| 128 | self.do_test("fc00:1234::/63", "2001:db8:1::/56") |
| 129 | |
Ole Troan | ff344a9 | 2023-10-12 18:54:55 +0200 | [diff] [blame] | 130 | def test_npt66_icmp6(self): |
| 131 | """Send and receive a packet through NPT66""" |
| 132 | |
| 133 | # Test ICMP6 error packets |
| 134 | self.do_test( |
| 135 | "fd00:0000:0000::/48", "2001:4650:c3ed::/48", reply_icmp_error=True |
| 136 | ) |
| 137 | self.do_test("fc00:1::/48", "2001:db8:1::/48", reply_icmp_error=True) |
| 138 | self.do_test("fc00:1234::/32", "2001:db8:1::/32", reply_icmp_error=True) |
| 139 | self.do_test("fc00:1234::/63", "2001:db8:1::/56", reply_icmp_error=True) |
| 140 | |
Ole Troan | 6ee3aa4 | 2023-08-17 13:36:08 +0200 | [diff] [blame] | 141 | |
| 142 | if __name__ == "__main__": |
| 143 | unittest.main(testRunner=VppTestRunner) |