summaryrefslogtreecommitdiffstats
path: root/test/test_nat64.py
blob: a28d7257aa0b0b49b40776f29377b9b3ec64ba3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
#!/usr/bin/env python3

import ipaddress
import random
import socket
import struct
import unittest
from io import BytesIO

import scapy.compat
from config import config
from framework import VppTestCase
from asfframework import (
    tag_fixme_vpp_workers,
    tag_fixme_ubuntu2204,
    is_distro_ubuntu2204,
    VppTestRunner,
)
from ipfix import IPFIX, Set, Template, Data, IPFIXDecoder
from scapy.data import IP_PROTOS
from scapy.layers.inet import IP, TCP, UDP, ICMP
from scapy.layers.inet import IPerror, TCPerror, UDPerror, ICMPerror
from scapy.layers.inet6 import ICMPv6DestUnreach, IPerror6, IPv6ExtHdrFragment
from scapy.layers.inet6 import (
    IPv6,
    ICMPv6EchoRequest,
    ICMPv6EchoReply,
    ICMPv6ND_NS,
    ICMPv6ND_NA,
    ICMPv6NDOptDstLLAddr,
    fragment6,
)
from scapy.layers.l2 import Ether, GRE
from scapy.packet import Raw
from syslog_rfc5424_parser import SyslogMessage, ParseError
from syslog_rfc5424_parser.constants import SyslogSeverity
from util import ppc, ppp
from vpp_papi import VppEnum
from config import config


@tag_fixme_vpp_workers
@tag_fixme_ubuntu2204
@unittest.skipIf("nat" in config.excluded_plugins, "Exclude NAT plugin tests")
class TestNAT64(VppTestCase):
    """NAT64 Test Cases"""

    @property
    def SYSLOG_SEVERITY(self):
        return VppEnum.vl_api_syslog_severity_t

    @property
    def config_flags(self):
        return VppEnum.vl_api_nat_config_flags_t

    @classmethod
    def setUpClass(cls):
        super(TestNAT64, cls).setUpClass()

        cls.tcp_port_in = 6303
        cls.tcp_port_out = 6303
        cls.udp_port_in = 6304
        cls.udp_port_out = 6304
        cls.icmp_id_in = 6305
        cls.icmp_id_out = 6305
        cls.tcp_external_port = 80
        cls.nat_addr = "10.0.0.3"
        cls.nat_addr_n = socket.inet_pton(socket.AF_INET, cls.nat_addr)
        cls.vrf1_id = 10
        cls.vrf1_nat_addr = "10.0.10.3"
        cls.ipfix_src_port = 4739
        cls.ipfix_domain_id = 1

        cls.create_pg_interfaces(range(6))
        cls.ip6_interfaces = list(cls.pg_interfaces[0:1])
        cls.ip6_interfaces.append(cls.pg_interfaces[2])
        cls.ip4_interfaces = list(cls.pg_interfaces[1:2])

        cls.vapi.ip_table_add_del_v2(
            is_add=1, table={"table_id": cls.vrf1_id, "is_ip6": 1}
        )

        cls.pg_interfaces[2].set_table_ip6(cls.vrf1_id)

        cls.pg0.generate_remote_hosts(2)

        for i in cls.ip6_interfaces:
            i.admin_up()
            i.config_ip6()
            i.configure_ipv6_neighbors()

        for i in cls.ip4_interfaces:
            i.admin_up()
            i.config_ip4()
            i.resolve_arp()

        cls.pg3.admin_up()
        cls.pg3.config_ip4()
        cls.pg3.resolve_arp()
        cls.pg3.config_ip6()
        cls.pg3.configure_ipv6_neighbors()

        cls.pg5.admin_up()
        cls.pg5.config_ip6()

    @classmethod
    def tearDownClass(cls):
        super(TestNAT64, cls).tearDownClass()

    def setUp(self):
        super(TestNAT64, self).setUp()
        self.vapi.nat64_plugin_enable_disable(enable=1, bib_buckets=128, st_buckets=256)

    def tearDown(self):
        super(TestNAT64, self).tearDown()
        if not self.vpp_dead:
            self.vapi.nat64_plugin_enable_disable(enable=0)

    def show_commands_at_teardown(self):
        self.logger.info(self.vapi.cli("show nat64 pool"))
        self.logger.info(self.vapi.cli("show nat64 interfaces"))
        self.logger.info(self.vapi.cli("show nat64 prefix"))
        self.logger.info(self.vapi.cli("show nat64 bib all"))
        self.logger.info(self.vapi.cli("show nat64 session table all"))

    def create_stream_in_ip6(self, in_if, out_if, hlim=64, pref=None, plen=0):
        """
        Create IPv6 packet stream for inside network

        :param in_if: Inside interface
        :param out_if: Outside interface
        :param ttl: Hop Limit of generated packets
        :param pref: NAT64 prefix
        :param plen: NAT64 prefix length
        """
        pkts = []
        if pref is None:
            dst = "".join(["64:ff9b::", out_if.remote_ip4])
        else:
            dst = self.compose_ip6(out_if.remote_ip4, pref, plen)

        # TCP
        p = (
            Ether(dst=in_if.local_mac, src=in_if.remote_mac)
            / IPv6(src=in_if.remote_ip6, dst=dst, hlim=hlim)
            / TCP(sport=self.tcp_port_in, dport=20)
        )
        pkts.append(p)

        # UDP
        p = (
            Ether(dst=in_if.local_mac, src=in_if.remote_mac)
            / IPv6(src=in_if.remote_ip6, dst=dst, hlim=hlim)
            / UDP(sport=self.udp_port_in, dport=20)
        )
        pkts.append(p)

        # ICMP
        p = (
            Ether(dst=in_if.local_mac, src=in_if.remote_mac)
            / IPv6(src=in_if.remote_ip6, dst=dst, hlim=hlim)
            / ICMPv6EchoRequest(id=self.icmp_id_in)
        )
        pkts.append(p)

        return pkts

    def create_stream_out(self, out_if, dst_ip=None, ttl=64, use_inside_ports=False):
        """
        Create packet stream for outside network

        :param out_if: Outside interface
        :param dst_ip: Destination IP address (Default use global NAT address)
        :param ttl: TTL of generated packets
        :param use_inside_ports: Use inside NAT ports as destination ports
               instead of outside ports
        """
        if dst_ip is None:
            dst_ip = self.nat_addr
        if not use_inside_ports:
            tcp_port = self.tcp_port_out
            udp_port = self.udp_port_out
            icmp_id = self.icmp_id_out
        else:
            tcp_port = self.tcp_port_in
            udp_port = self.udp_port_in
            icmp_id = self.icmp_id_in
        pkts = []
        # TCP
        p = (
            Ether(dst=out_if.local_mac, src=out_if.remote_mac)
            / IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl)
            / TCP(dport=tcp_port, sport=20)
        )
        pkts.extend([p, p])

        # UDP
        p = (
            Ether(dst=out_if.local_mac, src=out_if.remote_mac)
            / IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl)
            / UDP(dport=udp_port, sport=20)
        )
        pkts.append(p)

        # ICMP
        p = (
            Ether(dst=out_if.local_mac, src=out_if.remote_mac)
            / IP(src=out_if.remote_ip4, dst=dst_ip, ttl=ttl)
            / ICMP(id=icmp_id, type="echo-reply")
        )
        pkts.append(p)

        return pkts

    def verify_capture_out(
        self,
        capture,
        nat_ip=None,
        same_port=False,
        dst_ip=None,
        is_ip6=False,
        ignore_port=False,
    ):
        """
        Verify captured packets on outside network

        :param capture: Captured packets
        :param nat_ip: Translated IP address (Default use global NAT address)
        :param same_port: Source port number is not translated (Default False)
        :param dst_ip: Destination IP address (Default do not verify)
        :param is_ip6: If L3 protocol is IPv6 (Default False)
        """
        if is_ip6:
            IP46 = IPv6
            ICMP46 = ICMPv6EchoRequest
        else:
            IP46 = IP
            ICMP46 = ICMP
        if nat_ip is None:
            nat_ip = self.nat_addr
        for packet in capture:
            try:
                if not is_ip6:
                    self.assert_packet_checksums_valid(packet)
                self.assertEqual(packet[IP46].src, nat_ip)
                if dst_ip is not None:
                    self.assertEqual(packet[IP46].dst, dst_ip)
                if packet.haslayer(TCP):
                    if not ignore_port:
                        if same_port:
                            self.assertEqual(packet[TCP].sport, self.tcp_port_in)
                        else:
                            self.assertNotEqual(packet[TCP].sport, self.tcp_port_in)
                    self.tcp_port_out = packet[TCP].sport
                    self.assert_packet_checksums_valid(packet)
                elif packet.haslayer(UDP):
                    if not ignore_port:
                        if same_port:
                            self.assertEqual(packet[UDP].sport, self.udp_port_in)
                        else:
                            self.assertNotEqual(packet[UDP].sport, self.udp_port_in)
                    self.udp_port_out = packet[UDP].sport
                else:
                    if not ignore_port:
                        if same_port:
                            self.assertEqual(packet[ICMP46].id, self.icmp_id_in)
                        else:
                            self.assertNotEqual(packet[ICMP46].id, self.icmp_id_in)
                    self.icmp_id_out = packet[ICMP46].id
                    self.assert_packet_checksums_valid(packet)
            except:
                self.logger.error(
                    ppp("Unexpected or invalid packet (outside network):", packet)
                )
                raise

    def verify_capture_in_ip6(self, capture, src_ip, dst_ip):
        """
        Verify captured IPv6 packets on inside network

        :param capture: Captured packets
        :param src_ip: Source IP
        :param dst_ip: Destination IP address
        """
        for packet in capture:
            try:
                self.assertEqual(packet[IPv6].src, src_ip)
                self.assertEqual(packet[IPv6].dst, dst_ip)
                self.assert_packet_checksums_valid(packet)
                if packet.haslayer(TCP):
                    self.assertEqual(packet[TCP].dport, self.tcp_port_in)
                elif packet.haslayer(UDP):
                    self.assertEqual(packet[UDP].dport, self.udp_port_in)
                else:
                    self.assertEqual(packet[ICMPv6EchoReply].id, self.icmp_id_in)
            except:
                self.logger.error(
                    ppp("Unexpected or invalid packet (inside network):", packet)
                )
                raise

    def create_stream_frag(
        self, src_if, dst, sport, dport, data, proto=IP_PROTOS.tcp, echo_reply=False
    ):
        """
        Create fragmented packet stream

        :param src_if: Source interface
        :param dst: Destination IPv4 address
        :param sport: Source port
        :param dport: Destination port
        :param data: Payload data
        :param proto: protocol (TCP, UDP, ICMP)
        :param echo_reply: use echo_reply if protocol is ICMP
        :returns: Fragments
        """
        if proto == IP_PROTOS.tcp:
            p = (
                IP(src=src_if.remote_ip4, dst=dst)
                / TCP(sport=sport, dport=dport)
                / Raw(data)
            )
            p = p.__class__(scapy.compat.raw(p))
            chksum = p[TCP].chksum
            proto_header = TCP(sport=sport, dport=dport, chksum=chksum)
        elif proto == IP_PROTOS.udp:
            proto_header = UDP(sport=sport, dport=dport)
        elif proto == IP_PROTOS.icmp:
            if not echo_reply:
                proto_header = ICMP(id=sport, type="echo-request")
            else:
                proto_header = ICMP(id=sport, type="echo-reply")
        else:
            raise Exception("Unsupported protocol")
        id = random.randint(0, 65535)
        pkts = []
        if proto == IP_PROTOS.tcp:
            raw = Raw(data[0:4])
        else:
            raw = Raw(data[0:16])
        p = (
            Ether(src=src_if.remote_mac, dst=src_if.local_mac)
            / IP(src=src_if.remote_ip4, dst=dst, flags="MF", frag=0, id=id)
            / proto_header
            / raw
        )
        pkts.append(p)
        if proto == IP_PROTOS.tcp:
            raw = Raw(data[4:20])
        else:
            raw = Raw(data[16:32])
        p = (
            Ether(src=src_if.remote_mac, dst=src_if.local_mac)
            / IP(src=src_if.remote_ip4, dst=dst, flags="MF", frag=3, id=id, proto=proto)
            / raw
        )
        pkts.append(p)
        if proto == IP_PROTOS.tcp:
            raw = Raw(data[20:])
        else:
            raw = Raw(data[32:])
        p = (
            Ether(src=src_if.remote_mac, dst=src_if.local_mac)
            / IP(src=src_if.remote_ip4, dst=dst, frag=5, proto=proto, id=id)
            / raw
        )
        pkts.append(p)
        return pkts

    def create_stream_frag_ip6(
        self, src_if, dst, sport, dport, data, pref=None, plen=0, frag_size=128
    ):
        """
        Create fragmented packet stream

        :param src_if: Source interface
        :param dst: Destination IPv4 address
        :param sport: Source TCP port
        :param dport: Destination TCP port
        :param data: Payload data
        :param pref: NAT64 prefix
        :param plen: NAT64 prefix length
        :param fragsize: size of fragments
        :returns: Fragments
        """
        if pref is None:
            dst_ip6 = "".join(["64:ff9b::", dst])
        else:
            dst_ip6 = self.compose_ip6(dst, pref, plen)

        p = (
            Ether(dst=src_if.local_mac, src=src_if.remote_mac)
            / IPv6(src=src_if.remote_ip6, dst=dst_ip6)
            / IPv6ExtHdrFragment(id=random.randint(0, 65535))
            / TCP(sport=sport, dport=dport)
            / Raw(data)
        )

        return fragment6(p, frag_size)

    def reass_frags_and_verify(self, frags, src, dst):
        """
        Reassemble and verify fragmented packet

        :param frags: Captured fragments
        :param src: Source IPv4 address to verify
        :param dst: Destination IPv4 address to verify

        :returns: Reassembled IPv4 packet
        """
        buffer = BytesIO()
        for p in frags:
            self.assertEqual(p[IP].src, src)
            self.assertEqual(p[IP].dst, dst)
            self.assert_ip_checksum_valid(p)
            buffer.seek(p[IP].frag * 8)
            buffer.write(bytes(p[IP].payload))
        ip = IP(src=frags[0][IP].src, dst=frags[0][IP].dst, proto=frags[0][IP].proto)
        if ip.proto == IP_PROTOS.tcp:
            p = ip / TCP(buffer.getvalue())
            self.logger.debug(ppp("Reassembled:", p))
            self.assert_tcp_checksum_valid(p)
        elif ip.proto == IP_PROTOS.udp:
            p = ip / UDP(buffer.getvalue()[:8]) / Raw(buffer.getvalue()[8:])
        elif ip.proto == IP_PROTOS.icmp:
            p = ip / ICMP(buffer.getvalue())
        return p

    def reass_frags_and_verify_ip6(self, frags, src, dst):
        """
        Reassemble and verify fragmented packet

        :param frags: Captured fragments
        :param src: Source IPv6 address to verify
        :param dst: Destination IPv6 address to verify

        :returns: Reassembled IPv6 packet
        """
        buffer = BytesIO()
        for p in frags:
            self.assertEqual(p[IPv6].src, src)
            self.assertEqual(p[IPv6].dst, dst)
            buffer.seek(p[IPv6ExtHdrFragment].offset * 8)
            buffer.write(bytes(p[IPv6ExtHdrFragment].payload))
        ip = IPv6(
            src=frags[0][IPv6].src,
            dst=frags[0][IPv6].dst,
            nh=frags[0][IPv6ExtHdrFragment].nh,
        )
        if ip.nh == IP_PROTOS.tcp:
            p = ip / TCP(buffer.getvalue())
        elif ip.nh == IP_PROTOS.udp:
            p = ip / UDP(buffer.getvalue())
        self.logger.debug(ppp("Reassembled:", p))
        self.assert_packet_checksums_valid(p)
        return p

    def verify_ipfix_max_bibs(self, data, limit):
        """
        Verify IPFIX maximum BIB entries exceeded event

        :param data: Decoded IPFIX data records
        :param limit: Number of maximum BIB entries that can be created.
        """
        self.assertEqual(1, len(data))
        record = data[0]
        # natEvent
        self.assertEqual(scapy.compat.orb(record[230]), 13)
        # natQuotaExceededEvent
        self.assertEqual(struct.pack("!I", 2), record[466])
        # maxBIBEntries
        self.assertEqual(struct.pack("!I", limit), record[472])
        return len(data)

    def verify_ipfix_bib(self, data, is_create, src_addr):
        """
        Verify IPFIX NAT64 BIB create and delete events

        :param data: Decoded IPFIX data records
        :param is_create: Create event if nonzero value otherwise delete event
        :param src_addr: IPv6 source address
        """
        self.assertEqual(1, len(data))
        record = data[0]
        # natEvent
        if is_create:
            self.assertEqual(scapy.compat.orb(record[230]), 10)
        else:
            self.assertEqual(scapy.compat.orb(record[230]), 11)
        # sourceIPv6Address
        self.assertEqual(src_addr, str(ipaddress.IPv6Address(record[27])))
        # postNATSourceIPv4Address
        self.assertEqual(self.nat_addr_n, record[225])
        # protocolIdentifier
        self.assertEqual(IP_PROTOS.tcp, scapy.compat.orb(record[4]))
        # ingressVRFID
        self.assertEqual(struct.pack("!I", 0), record[234])
        # sourceTransportPort
        self.assertEqual(struct.pack("!H", self.tcp_port_in), record[7])
        # postNAPTSourceTransportPort
        self.assertEqual(struct.pack("!H", self.tcp_port_out), record[227])

    def verify_ipfix_nat64_ses(self, data, is_create, src_addr, dst_addr, dst_port):
        """
        Verify IPFIX NAT64 session create and delete events

        :param data: Decoded IPFIX data records
        :param is_create: Create event if nonzero value otherwise delete event
        :param src_addr: IPv6 source address
        :param dst_addr: IPv4 destination address
        :param dst_port: destination TCP port
        """
        self.assertEqual(1, len(data))
        record = data[0]
        # natEvent
        if is_create:
            self.assertEqual(scapy.compat.orb(record[230]), 6)
        else:
            self.assertEqual(scapy.compat.orb(record[230]), 7)
        # sourceIPv6Address
        self.assertEqual(src_addr, str(ipaddress.IPv6Address(record[27])))
        # destinationIPv6Address
        self.assertEqual(
            socket.inet_pton(
                socket.AF_INET6, self.compose_ip6(dst_addr, "64:ff9b::", 96)
            ),
            record[28],
        )
        # postNATSourceIPv4Address
        self.assertEqual(self.nat_addr_n, record[225])
        # postNATDestinationIPv4Address
        self.assertEqual(socket.inet_pton(socket.AF_INET, dst_addr), record[226])
        # protocolIdentifier
        self.assertEqual(IP_PROTOS.tcp, scapy.compat.orb(record[4]))
        # ingressVRFID
        self.assertEqual(struct.pack("!I", 0), record[234])
        # sourceTransportPort
        self.assertEqual(struct.pack("!H", self.tcp_port_in), record[7])
        # postNAPTSourceTransportPort
        self.assertEqual(struct.pack("!H", self.tcp_port_out), record[227])
        # destinationTransportPort
        self.assertEqual(struct.pack("!H", dst_port), record[11])
        # postNAPTDestinationTransportPort
        self.assertEqual(struct.pack("!H", dst_port), record[228])

    def verify_syslog_sess(self, data, is_add=True, is_ip6=False):
        message = data.decode("utf-8")
        try:
            message = SyslogMessage.parse(message)
        except ParseError as e:
            self.logger.error(e)
            raise
        else:
            self.assertEqual(message.severity, SyslogSeverity.info)
            self.assertEqual(message.appname, "NAT")
            self.assertEqual(message.msgid, "SADD" if is_add else "SDEL")
            sd_params = message.sd.get("nsess")
            self.assertTrue(sd_params is not None)
            if is_ip6:
                self.assertEqual(sd_params.get("IATYP"), "IPv6")
                self.assertEqual(sd_params.get("ISADDR"), self.pg0.remote_ip6)
            else:
                self.assertEqual(sd_params.get("IATYP"), "IPv4")
                self.assertEqual(sd_params.get("ISADDR"), self.pg0.remote_ip4)
                self.assertTrue(sd_params.get("SSUBIX") is not None)
            self.assertEqual(sd_params.get("ISPORT"), "%d" % self.tcp_port_in)
            self.assertEqual(sd_params.get("XATYP"), "IPv4")
            self.assertEqual(sd_params.get("XSADDR"), self.nat_addr)
            self.assertEqual(sd_params.get("XSPORT"), "%d" % self.tcp_port_out)
            self.assertEqual(sd_params.get("PROTO"), "%d" % IP_PROTOS.tcp)
            self.assertEqual(sd_params.get("SVLAN"), "0")
            self.assertEqual(sd_params.get("XDADDR"), self.pg1.remote_ip4)
            self.assertEqual(sd_params.get("XDPORT"), "%d" % self.tcp_external_port)

    def compose_ip6(self, ip4, pref, plen):
        """
        Compose IPv4-embedded IPv6 addresses

        :param ip4: IPv4 address
        :param pref: IPv6 prefix
        :param plen: IPv6 prefix length
        :returns: IPv4-embedded IPv6 addresses
        """
        pref_n = list(socket.inet_pton(socket.AF_INET6, pref))
        ip4_n = list(socket.inet_pton(socket.AF_INET, ip4))
        if plen == 32:
            pref_n[4] = ip4_n[0]
            pref_n[5] = ip4_n[1]
            pref_n[6] = ip4_n[2]
            pref_n[7] = ip4_n[3]
        elif plen == 40:
            pref_n[5] = ip4_n[0]
            pref_n[6] = ip4_n[1]
            pref_n[7] = ip4_n[2]
            pref_n[9] = ip4_n[3]
        elif plen == 48:
            pref_n[6] = ip4_n[0]
            pref_n[7] = ip4_n[1]
            pref_n[9] = ip4_n[2]
            pref_n[10] = ip4_n[3]
        elif plen == 56:
            pref_n[7] = ip4_n[0]
            pref_n[9] = ip4_n[1]
            pref_n[10] = ip4_n[2]
            pref_n[11] = ip4_n[3]
        elif plen == 64:
            pref_n[9] = ip4_n[0]
            pref_n[10] = ip4_n[1]
            pref_n[11] = ip4_n[2]
            pref_n[12] = ip4_n[3]
        elif plen == 96:
            pref_n[12] = ip4_n[0]
            pref_n[13] = ip4_n[1]
            pref_n[14] = ip4_n[2]
            pref_n[15] = ip4_n[3]
        packed_pref_n = b"".join([scapy.compat.chb(x) for x in pref_n])
        return socket.inet_ntop(socket.AF_INET6, packed_pref_n)

    def verify_ipfix_max_sessions(self, data, limit):
        """
        Verify IPFIX maximum session entries exceeded event

        :param data: Decoded IPFIX data records
        :param limit: Number of maximum session entries that can be created.
        """
        self.assertEqual(1, len(data))
        record = data[0]
        # natEvent
        self.assertEqual(scapy.compat.orb(record[230]), 13)
        # natQuotaExceededEvent
        self.assertEqual(struct.pack("!I", 1), record[466])
        # maxSessionEntries
        self.assertEqual(struct.pack("!I", limit), record[471])
        return len(data)

    def test_nat64_inside_interface_handles_neighbor_advertisement(self):
        """NAT64 inside interface handles Neighbor Advertisement"""

        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg5.sw_if_index
        )

        # Try to send ping
        ping = (
            Ether(dst=self.pg5.local_mac, src=self.pg5.remote_mac)
            / IPv6(src=self.pg5.remote_ip6, dst=self.pg5.local_ip6)
            / ICMPv6EchoRequest()
        )
        pkts = [ping]
        self.pg5.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()

        # Wait for Neighbor Solicitation
        capture = self.pg5.get_capture(len(pkts))
        packet = capture[0]
        try:
            self.assertEqual(packet[IPv6].src, self.pg5.local_ip6_ll)
            self.assertEqual(packet.haslayer(ICMPv6ND_NS), 1)
            tgt = packet[ICMPv6ND_NS].tgt
        except:
            self.logger.error(ppp("Unexpected or invalid packet:", packet))
            raise

        # Send Neighbor Advertisement
        p = (
            Ether(dst=self.pg5.local_mac, src=self.pg5.remote_mac)
            / IPv6(src=self.pg5.remote_ip6, dst=self.pg5.local_ip6)
            / ICMPv6ND_NA(tgt=tgt)
            / ICMPv6NDOptDstLLAddr(lladdr=self.pg5.remote_mac)
        )
        pkts = [p]
        self.pg5.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()

        # Try to send ping again
        pkts = [ping]
        self.pg5.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()

        # Wait for ping reply
        capture = self.pg5.get_capture(len(pkts))
        packet = capture[0]
        try:
            self.assertEqual(packet[IPv6].src, self.pg5.local_ip6)
            self.assertEqual(packet[IPv6].dst, self.pg5.remote_ip6)
            self.assertEqual(packet.haslayer(ICMPv6EchoReply), 1)
        except:
            self.logger.error(ppp("Unexpected or invalid packet:", packet))
            raise

    def test_pool(self):
        """Add/delete address to NAT64 pool"""
        nat_addr = "1.2.3.4"

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=nat_addr, end_addr=nat_addr, vrf_id=0xFFFFFFFF, is_add=1
        )

        addresses = self.vapi.nat64_pool_addr_dump()
        self.assertEqual(len(addresses), 1)
        self.assertEqual(str(addresses[0].address), nat_addr)

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=nat_addr, end_addr=nat_addr, vrf_id=0xFFFFFFFF, is_add=0
        )

        addresses = self.vapi.nat64_pool_addr_dump()
        self.assertEqual(len(addresses), 0)

    def test_interface(self):
        """Enable/disable NAT64 feature on the interface"""
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg1.sw_if_index
        )

        interfaces = self.vapi.nat64_interface_dump()
        self.assertEqual(len(interfaces), 2)
        pg0_found = False
        pg1_found = False
        for intf in interfaces:
            if intf.sw_if_index == self.pg0.sw_if_index:
                self.assertEqual(intf.flags, self.config_flags.NAT_IS_INSIDE)
                pg0_found = True
            elif intf.sw_if_index == self.pg1.sw_if_index:
                self.assertEqual(intf.flags, self.config_flags.NAT_IS_OUTSIDE)
                pg1_found = True
        self.assertTrue(pg0_found)
        self.assertTrue(pg1_found)

        features = self.vapi.cli("show interface features pg0")
        self.assertIn("nat64-in2out", features)
        features = self.vapi.cli("show interface features pg1")
        self.assertIn("nat64-out2in", features)

        self.vapi.nat64_add_del_interface(
            is_add=0, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=0, flags=flags, sw_if_index=self.pg1.sw_if_index
        )

        interfaces = self.vapi.nat64_interface_dump()
        self.assertEqual(len(interfaces), 0)

    def test_static_bib(self):
        """Add/delete static BIB entry"""
        in_addr = "2001:db8:85a3::8a2e:370:7334"
        out_addr = "10.1.1.3"
        in_port = 1234
        out_port = 5678
        proto = IP_PROTOS.tcp

        self.vapi.nat64_add_del_static_bib(
            i_addr=in_addr,
            o_addr=out_addr,
            i_port=in_port,
            o_port=out_port,
            proto=proto,
            vrf_id=0,
            is_add=1,
        )
        bib = self.vapi.nat64_bib_dump(proto=IP_PROTOS.tcp)
        static_bib_num = 0
        for bibe in bib:
            if bibe.flags & self.config_flags.NAT_IS_STATIC:
                static_bib_num += 1
                self.assertEqual(str(bibe.i_addr), in_addr)
                self.assertEqual(str(bibe.o_addr), out_addr)
                self.assertEqual(bibe.i_port, in_port)
                self.assertEqual(bibe.o_port, out_port)
        self.assertEqual(static_bib_num, 1)
        bibs = self.statistics.get_counter("/nat64/total-bibs")
        self.assertEqual(bibs[0][0], 1)

        self.vapi.nat64_add_del_static_bib(
            i_addr=in_addr,
            o_addr=out_addr,
            i_port=in_port,
            o_port=out_port,
            proto=proto,
            vrf_id=0,
            is_add=0,
        )
        bib = self.vapi.nat64_bib_dump(proto=IP_PROTOS.tcp)
        static_bib_num = 0
        for bibe in bib:
            if bibe.flags & self.config_flags.NAT_IS_STATIC:
                static_bib_num += 1
        self.assertEqual(static_bib_num, 0)
        bibs = self.statistics.get_counter("/nat64/total-bibs")
        self.assertEqual(bibs[0][0], 0)

    def test_set_timeouts(self):
        """Set NAT64 timeouts"""
        # verify default values
        timeouts = self.vapi.nat64_get_timeouts()
        self.assertEqual(timeouts.udp, 300)
        self.assertEqual(timeouts.icmp, 60)
        self.assertEqual(timeouts.tcp_transitory, 240)
        self.assertEqual(timeouts.tcp_established, 7440)

        # set and verify custom values
        self.vapi.nat64_set_timeouts(
            udp=200, tcp_established=7450, tcp_transitory=250, icmp=30
        )
        timeouts = self.vapi.nat64_get_timeouts()
        self.assertEqual(timeouts.udp, 200)
        self.assertEqual(timeouts.icmp, 30)
        self.assertEqual(timeouts.tcp_transitory, 250)
        self.assertEqual(timeouts.tcp_established, 7450)

    def test_dynamic(self):
        """NAT64 dynamic translation test"""
        self.tcp_port_in = 6303
        self.udp_port_in = 6304
        self.icmp_id_in = 6305

        ses_num_start = self.nat64_get_ses_num()

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg1.sw_if_index
        )

        # in2out
        tcpn = self.statistics.get_counter("/nat64/in2out/tcp")[0]
        udpn = self.statistics.get_counter("/nat64/in2out/udp")[0]
        icmpn = self.statistics.get_counter("/nat64/in2out/icmp")[0]
        drops = self.statistics.get_counter("/nat64/in2out/drops")[0]

        pkts = self.create_stream_in_ip6(self.pg0, self.pg1)
        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg1.get_capture(len(pkts))
        self.verify_capture_out(
            capture, nat_ip=self.nat_addr, dst_ip=self.pg1.remote_ip4
        )

        if_idx = self.pg0.sw_if_index
        cnt = self.statistics.get_counter("/nat64/in2out/tcp")[0]
        self.assertEqual(cnt[if_idx] - tcpn[if_idx], 1)
        cnt = self.statistics.get_counter("/nat64/in2out/udp")[0]
        self.assertEqual(cnt[if_idx] - udpn[if_idx], 1)
        cnt = self.statistics.get_counter("/nat64/in2out/icmp")[0]
        self.assertEqual(cnt[if_idx] - icmpn[if_idx], 1)
        cnt = self.statistics.get_counter("/nat64/in2out/drops")[0]
        self.assertEqual(cnt[if_idx] - drops[if_idx], 0)

        # out2in
        tcpn = self.statistics.get_counter("/nat64/out2in/tcp")[0]
        udpn = self.statistics.get_counter("/nat64/out2in/udp")[0]
        icmpn = self.statistics.get_counter("/nat64/out2in/icmp")[0]
        drops = self.statistics.get_counter("/nat64/out2in/drops")[0]

        pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr)
        self.pg1.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg0.get_capture(len(pkts))
        ip = IPv6(src="".join(["64:ff9b::", self.pg1.remote_ip4]))
        self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg0.remote_ip6)

        if_idx = self.pg1.sw_if_index
        cnt = self.statistics.get_counter("/nat64/out2in/tcp")[0]
        self.assertEqual(cnt[if_idx] - tcpn[if_idx], 2)
        cnt = self.statistics.get_counter("/nat64/out2in/udp")[0]
        self.assertEqual(cnt[if_idx] - udpn[if_idx], 1)
        cnt = self.statistics.get_counter("/nat64/out2in/icmp")[0]
        self.assertEqual(cnt[if_idx] - icmpn[if_idx], 1)
        cnt = self.statistics.get_counter("/nat64/out2in/drops")[0]
        self.assertEqual(cnt[if_idx] - drops[if_idx], 0)

        bibs = self.statistics.get_counter("/nat64/total-bibs")
        self.assertEqual(bibs[0][0], 3)
        sessions = self.statistics.get_counter("/nat64/total-sessions")
        self.assertEqual(sessions[0][0], 3)

        # in2out
        pkts = self.create_stream_in_ip6(self.pg0, self.pg1)
        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg1.get_capture(len(pkts))
        self.verify_capture_out(
            capture, nat_ip=self.nat_addr, dst_ip=self.pg1.remote_ip4
        )

        # out2in
        pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr)
        self.pg1.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg0.get_capture(len(pkts))
        self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg0.remote_ip6)

        ses_num_end = self.nat64_get_ses_num()

        self.assertEqual(ses_num_end - ses_num_start, 3)

        # tenant with specific VRF
        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.vrf1_nat_addr,
            end_addr=self.vrf1_nat_addr,
            vrf_id=self.vrf1_id,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg2.sw_if_index
        )

        pkts = self.create_stream_in_ip6(self.pg2, self.pg1)
        self.pg2.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg1.get_capture(len(pkts))
        self.verify_capture_out(
            capture, nat_ip=self.vrf1_nat_addr, dst_ip=self.pg1.remote_ip4
        )

        pkts = self.create_stream_out(self.pg1, dst_ip=self.vrf1_nat_addr)
        self.pg1.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg2.get_capture(len(pkts))
        self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg2.remote_ip6)

    def test_static(self):
        """NAT64 static translation test"""
        self.tcp_port_in = 60303
        self.udp_port_in = 60304
        self.icmp_id_in = 60305
        self.tcp_port_out = 60303
        self.udp_port_out = 60304
        self.icmp_id_out = 60305

        ses_num_start = self.nat64_get_ses_num()

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg1.sw_if_index
        )

        self.vapi.nat64_add_del_static_bib(
            i_addr=self.pg0.remote_ip6,
            o_addr=self.nat_addr,
            i_port=self.tcp_port_in,
            o_port=self.tcp_port_out,
            proto=IP_PROTOS.tcp,
            vrf_id=0,
            is_add=1,
        )
        self.vapi.nat64_add_del_static_bib(
            i_addr=self.pg0.remote_ip6,
            o_addr=self.nat_addr,
            i_port=self.udp_port_in,
            o_port=self.udp_port_out,
            proto=IP_PROTOS.udp,
            vrf_id=0,
            is_add=1,
        )
        self.vapi.nat64_add_del_static_bib(
            i_addr=self.pg0.remote_ip6,
            o_addr=self.nat_addr,
            i_port=self.icmp_id_in,
            o_port=self.icmp_id_out,
            proto=IP_PROTOS.icmp,
            vrf_id=0,
            is_add=1,
        )

        # in2out
        pkts = self.create_stream_in_ip6(self.pg0, self.pg1)
        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg1.get_capture(len(pkts))
        self.verify_capture_out(
            capture, nat_ip=self.nat_addr, dst_ip=self.pg1.remote_ip4, same_port=True
        )

        # out2in
        pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr)
        self.pg1.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg0.get_capture(len(pkts))
        ip = IPv6(src="".join(["64:ff9b::", self.pg1.remote_ip4]))
        self.verify_capture_in_ip6(capture, ip[IPv6].src, self.pg0.remote_ip6)

        ses_num_end = self.nat64_get_ses_num()

        self.assertEqual(ses_num_end - ses_num_start, 3)

    def test_session_timeout(self):
        """NAT64 session timeout"""
        self.icmp_id_in = 1234
        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg1.sw_if_index
        )
        self.vapi.nat64_set_timeouts(
            udp=300, tcp_established=5, tcp_transitory=5, icmp=5
        )

        pkts = self.create_stream_in_ip6(self.pg0, self.pg1)
        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg1.get_capture(len(pkts))

        ses_num_before_timeout = self.nat64_get_ses_num()

        self.virtual_sleep(15)

        # ICMP and TCP session after timeout
        ses_num_after_timeout = self.nat64_get_ses_num()
        self.assertEqual(ses_num_before_timeout - ses_num_after_timeout, 2)

    def test_icmp_error(self):
        """NAT64 ICMP Error message translation"""
        self.tcp_port_in = 6303
        self.udp_port_in = 6304
        self.icmp_id_in = 6305

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg1.sw_if_index
        )

        # send some packets to create sessions
        pkts = self.create_stream_in_ip6(self.pg0, self.pg1)
        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture_ip4 = self.pg1.get_capture(len(pkts))
        self.verify_capture_out(
            capture_ip4, nat_ip=self.nat_addr, dst_ip=self.pg1.remote_ip4
        )

        pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr)
        self.pg1.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture_ip6 = self.pg0.get_capture(len(pkts))
        ip = IPv6(src="".join(["64:ff9b::", self.pg1.remote_ip4]))
        self.verify_capture_in_ip6(capture_ip6, ip[IPv6].src, self.pg0.remote_ip6)

        # in2out
        pkts = [
            Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
            / IPv6(src=self.pg0.remote_ip6, dst=ip[IPv6].src)
            / ICMPv6DestUnreach(code=1)
            / packet[IPv6]
            for packet in capture_ip6
        ]
        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg1.get_capture(len(pkts))
        for packet in capture:
            try:
                self.assertEqual(packet[IP].src, self.nat_addr)
                self.assertEqual(packet[IP].dst, self.pg1.remote_ip4)
                self.assertEqual(packet[ICMP].type, 3)
                self.assertEqual(packet[ICMP].code, 13)
                inner = packet[IPerror]
                self.assertEqual(inner.src, self.pg1.remote_ip4)
                self.assertEqual(inner.dst, self.nat_addr)
                self.assert_packet_checksums_valid(packet)
                if inner.haslayer(TCPerror):
                    self.assertEqual(inner[TCPerror].dport, self.tcp_port_out)
                elif inner.haslayer(UDPerror):
                    self.assertEqual(inner[UDPerror].dport, self.udp_port_out)
                else:
                    self.assertEqual(inner[ICMPerror].id, self.icmp_id_out)
            except:
                self.logger.error(ppp("Unexpected or invalid packet:", packet))
                raise

        # out2in
        pkts = [
            Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
            / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
            / ICMP(type=3, code=13)
            / packet[IP]
            for packet in capture_ip4
        ]
        self.pg1.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg0.get_capture(len(pkts))
        for packet in capture:
            try:
                self.assertEqual(packet[IPv6].src, ip.src)
                self.assertEqual(packet[IPv6].dst, self.pg0.remote_ip6)
                icmp = packet[ICMPv6DestUnreach]
                self.assertEqual(icmp.code, 1)
                inner = icmp[IPerror6]
                self.assertEqual(inner.src, self.pg0.remote_ip6)
                self.assertEqual(inner.dst, ip.src)
                self.assert_icmpv6_checksum_valid(packet)
                if inner.haslayer(TCPerror):
                    self.assertEqual(inner[TCPerror].sport, self.tcp_port_in)
                elif inner.haslayer(UDPerror):
                    self.assertEqual(inner[UDPerror].sport, self.udp_port_in)
                else:
                    self.assertEqual(inner[ICMPv6EchoRequest].id, self.icmp_id_in)
            except:
                self.logger.error(ppp("Unexpected or invalid packet:", packet))
                raise

    def test_hairpinning(self):
        """NAT64 hairpinning"""

        client = self.pg0.remote_hosts[0]
        server = self.pg0.remote_hosts[1]
        server_tcp_in_port = 22
        server_tcp_out_port = 4022
        server_udp_in_port = 23
        server_udp_out_port = 4023
        client_tcp_in_port = 1234
        client_udp_in_port = 1235
        client_tcp_out_port = 0
        client_udp_out_port = 0
        ip = IPv6(src="".join(["64:ff9b::", self.nat_addr]))
        nat_addr_ip6 = ip.src

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg1.sw_if_index
        )

        self.vapi.nat64_add_del_static_bib(
            i_addr=server.ip6n,
            o_addr=self.nat_addr,
            i_port=server_tcp_in_port,
            o_port=server_tcp_out_port,
            proto=IP_PROTOS.tcp,
            vrf_id=0,
            is_add=1,
        )
        self.vapi.nat64_add_del_static_bib(
            i_addr=server.ip6n,
            o_addr=self.nat_addr,
            i_port=server_udp_in_port,
            o_port=server_udp_out_port,
            proto=IP_PROTOS.udp,
            vrf_id=0,
            is_add=1,
        )

        # client to server
        pkts = []
        p = (
            Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
            / IPv6(src=client.ip6, dst=nat_addr_ip6)
            / TCP(sport=client_tcp_in_port, dport=server_tcp_out_port)
        )
        pkts.append(p)
        p = (
            Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
            / IPv6(src=client.ip6, dst=nat_addr_ip6)
            / UDP(sport=client_udp_in_port, dport=server_udp_out_port)
        )
        pkts.append(p)
        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg0.get_capture(len(pkts))
        for packet in capture:
            try:
                self.assertEqual(packet[IPv6].src, nat_addr_ip6)
                self.assertEqual(packet[IPv6].dst, server.ip6)
                self.assert_packet_checksums_valid(packet)
                if packet.haslayer(TCP):
                    self.assertNotEqual(packet[TCP].sport, client_tcp_in_port)
                    self.assertEqual(packet[TCP].dport, server_tcp_in_port)
                    client_tcp_out_port = packet[TCP].sport
                else:
                    self.assertNotEqual(packet[UDP].sport, client_udp_in_port)
                    self.assertEqual(packet[UDP].dport, server_udp_in_port)
                    client_udp_out_port = packet[UDP].sport
            except:
                self.logger.error(ppp("Unexpected or invalid packet:", packet))
                raise

        # server to client
        pkts = []
        p = (
            Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
            / IPv6(src=server.ip6, dst=nat_addr_ip6)
            / TCP(sport=server_tcp_in_port, dport=client_tcp_out_port)
        )
        pkts.append(p)
        p = (
            Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
            / IPv6(src=server.ip6, dst=nat_addr_ip6)
            / UDP(sport=server_udp_in_port, dport=client_udp_out_port)
        )
        pkts.append(p)
        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg0.get_capture(len(pkts))
        for packet in capture:
            try:
                self.assertEqual(packet[IPv6].src, nat_addr_ip6)
                self.assertEqual(packet[IPv6].dst, client.ip6)
                self.assert_packet_checksums_valid(packet)
                if packet.haslayer(TCP):
                    self.assertEqual(packet[TCP].sport, server_tcp_out_port)
                    self.assertEqual(packet[TCP].dport, client_tcp_in_port)
                else:
                    self.assertEqual(packet[UDP].sport, server_udp_out_port)
                    self.assertEqual(packet[UDP].dport, client_udp_in_port)
            except:
                self.logger.error(ppp("Unexpected or invalid packet:", packet))
                raise

        # ICMP error
        pkts = []
        pkts = [
            Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
            / IPv6(src=client.ip6, dst=nat_addr_ip6)
            / ICMPv6DestUnreach(code=1)
            / packet[IPv6]
            for packet in capture
        ]
        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg0.get_capture(len(pkts))
        for packet in capture:
            try:
                self.assertEqual(packet[IPv6].src, nat_addr_ip6)
                self.assertEqual(packet[IPv6].dst, server.ip6)
                icmp = packet[ICMPv6DestUnreach]
                self.assertEqual(icmp.code, 1)
                inner = icmp[IPerror6]
                self.assertEqual(inner.src, server.ip6)
                self.assertEqual(inner.dst, nat_addr_ip6)
                self.assert_packet_checksums_valid(packet)
                if inner.haslayer(TCPerror):
                    self.assertEqual(inner[TCPerror].sport, server_tcp_in_port)
                    self.assertEqual(inner[TCPerror].dport, client_tcp_out_port)
                else:
                    self.assertEqual(inner[UDPerror].sport, server_udp_in_port)
                    self.assertEqual(inner[UDPerror].dport, client_udp_out_port)
            except:
                self.logger.error(ppp("Unexpected or invalid packet:", packet))
                raise

    def test_prefix(self):
        """NAT64 Network-Specific Prefix"""

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg1.sw_if_index
        )
        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.vrf1_nat_addr,
            end_addr=self.vrf1_nat_addr,
            vrf_id=self.vrf1_id,
            is_add=1,
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg2.sw_if_index
        )

        # Add global prefix
        global_pref64 = "2001:db8::"
        global_pref64_len = 32
        global_pref64_str = "{}/{}".format(global_pref64, global_pref64_len)
        self.vapi.nat64_add_del_prefix(prefix=global_pref64_str, vrf_id=0, is_add=1)

        prefix = self.vapi.nat64_prefix_dump()
        self.assertEqual(len(prefix), 1)
        self.assertEqual(str(prefix[0].prefix), global_pref64_str)
        self.assertEqual(prefix[0].vrf_id, 0)

        # Add tenant specific prefix
        vrf1_pref64 = "2001:db8:122:300::"
        vrf1_pref64_len = 56
        vrf1_pref64_str = "{}/{}".format(vrf1_pref64, vrf1_pref64_len)
        self.vapi.nat64_add_del_prefix(
            prefix=vrf1_pref64_str, vrf_id=self.vrf1_id, is_add=1
        )

        prefix = self.vapi.nat64_prefix_dump()
        self.assertEqual(len(prefix), 2)

        # Global prefix
        pkts = self.create_stream_in_ip6(
            self.pg0, self.pg1, pref=global_pref64, plen=global_pref64_len
        )
        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg1.get_capture(len(pkts))
        self.verify_capture_out(
            capture, nat_ip=self.nat_addr, dst_ip=self.pg1.remote_ip4
        )

        pkts = self.create_stream_out(self.pg1, dst_ip=self.nat_addr)
        self.pg1.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg0.get_capture(len(pkts))
        dst_ip = self.compose_ip6(self.pg1.remote_ip4, global_pref64, global_pref64_len)
        self.verify_capture_in_ip6(capture, dst_ip, self.pg0.remote_ip6)

        # Tenant specific prefix
        pkts = self.create_stream_in_ip6(
            self.pg2, self.pg1, pref=vrf1_pref64, plen=vrf1_pref64_len
        )
        self.pg2.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg1.get_capture(len(pkts))
        self.verify_capture_out(
            capture, nat_ip=self.vrf1_nat_addr, dst_ip=self.pg1.remote_ip4
        )

        pkts = self.create_stream_out(self.pg1, dst_ip=self.vrf1_nat_addr)
        self.pg1.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg2.get_capture(len(pkts))
        dst_ip = self.compose_ip6(self.pg1.remote_ip4, vrf1_pref64, vrf1_pref64_len)
        self.verify_capture_in_ip6(capture, dst_ip, self.pg2.remote_ip6)

    def test_unknown_proto(self):
        """NAT64 translate packet with unknown protocol"""

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg1.sw_if_index
        )
        remote_ip6 = self.compose_ip6(self.pg1.remote_ip4, "64:ff9b::", 96)

        # in2out
        p = (
            Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
            / IPv6(src=self.pg0.remote_ip6, dst=remote_ip6)
            / TCP(sport=self.tcp_port_in, dport=20)
        )
        self.pg0.add_stream(p)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        p = self.pg1.get_capture(1)

        p = (
            Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
            / IPv6(src=self.pg0.remote_ip6, dst=remote_ip6, nh=47)
            / GRE()
            / IP(src=self.pg2.local_ip4, dst=self.pg2.remote_ip4)
            / TCP(sport=1234, dport=1234)
        )
        self.pg0.add_stream(p)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        p = self.pg1.get_capture(1)
        packet = p[0]
        try:
            self.assertEqual(packet[IP].src, self.nat_addr)
            self.assertEqual(packet[IP].dst, self.pg1.remote_ip4)
            self.assertEqual(packet.haslayer(GRE), 1)
            self.assert_packet_checksums_valid(packet)
        except:
            self.logger.error(ppp("Unexpected or invalid packet:", packet))
            raise

        # out2in
        p = (
            Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
            / IP(src=self.pg1.remote_ip4, dst=self.nat_addr)
            / GRE()
            / IP(src=self.pg2.remote_ip4, dst=self.pg2.local_ip4)
            / TCP(sport=1234, dport=1234)
        )
        self.pg1.add_stream(p)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        p = self.pg0.get_capture(1)
        packet = p[0]
        try:
            self.assertEqual(packet[IPv6].src, remote_ip6)
            self.assertEqual(packet[IPv6].dst, self.pg0.remote_ip6)
            self.assertEqual(packet[IPv6].nh, 47)
        except:
            self.logger.error(ppp("Unexpected or invalid packet:", packet))
            raise

    def test_hairpinning_unknown_proto(self):
        """NAT64 translate packet with unknown protocol - hairpinning"""

        client = self.pg0.remote_hosts[0]
        server = self.pg0.remote_hosts[1]
        server_tcp_in_port = 22
        server_tcp_out_port = 4022
        client_tcp_in_port = 1234
        client_tcp_out_port = 1235
        server_nat_ip = "10.0.0.100"
        client_nat_ip = "10.0.0.110"
        server_nat_ip6 = self.compose_ip6(server_nat_ip, "64:ff9b::", 96)
        client_nat_ip6 = self.compose_ip6(client_nat_ip, "64:ff9b::", 96)

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=server_nat_ip,
            end_addr=client_nat_ip,
            vrf_id=0xFFFFFFFF,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg1.sw_if_index
        )

        self.vapi.nat64_add_del_static_bib(
            i_addr=server.ip6n,
            o_addr=server_nat_ip,
            i_port=server_tcp_in_port,
            o_port=server_tcp_out_port,
            proto=IP_PROTOS.tcp,
            vrf_id=0,
            is_add=1,
        )

        self.vapi.nat64_add_del_static_bib(
            i_addr=server.ip6n,
            o_addr=server_nat_ip,
            i_port=0,
            o_port=0,
            proto=IP_PROTOS.gre,
            vrf_id=0,
            is_add=1,
        )

        self.vapi.nat64_add_del_static_bib(
            i_addr=client.ip6n,
            o_addr=client_nat_ip,
            i_port=client_tcp_in_port,
            o_port=client_tcp_out_port,
            proto=IP_PROTOS.tcp,
            vrf_id=0,
            is_add=1,
        )

        # client to server
        p = (
            Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
            / IPv6(src=client.ip6, dst=server_nat_ip6)
            / TCP(sport=client_tcp_in_port, dport=server_tcp_out_port)
        )
        self.pg0.add_stream(p)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        p = self.pg0.get_capture(1)

        p = (
            Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
            / IPv6(src=client.ip6, dst=server_nat_ip6, nh=IP_PROTOS.gre)
            / GRE()
            / IP(src=self.pg2.local_ip4, dst=self.pg2.remote_ip4)
            / TCP(sport=1234, dport=1234)
        )
        self.pg0.add_stream(p)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        p = self.pg0.get_capture(1)
        packet = p[0]
        try:
            self.assertEqual(packet[IPv6].src, client_nat_ip6)
            self.assertEqual(packet[IPv6].dst, server.ip6)
            self.assertEqual(packet[IPv6].nh, IP_PROTOS.gre)
        except:
            self.logger.error(ppp("Unexpected or invalid packet:", packet))
            raise

        # server to client
        p = (
            Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
            / IPv6(src=server.ip6, dst=client_nat_ip6, nh=IP_PROTOS.gre)
            / GRE()
            / IP(src=self.pg2.remote_ip4, dst=self.pg2.local_ip4)
            / TCP(sport=1234, dport=1234)
        )
        self.pg0.add_stream(p)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        p = self.pg0.get_capture(1)
        packet = p[0]
        try:
            self.assertEqual(packet[IPv6].src, server_nat_ip6)
            self.assertEqual(packet[IPv6].dst, client.ip6)
            self.assertEqual(packet[IPv6].nh, IP_PROTOS.gre)
        except:
            self.logger.error(ppp("Unexpected or invalid packet:", packet))
            raise

    def test_one_armed_nat64(self):
        """One armed NAT64"""
        external_port = 0
        remote_host_ip6 = self.compose_ip6(self.pg3.remote_ip4, "64:ff9b::", 96)

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg3.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg3.sw_if_index
        )

        # in2out
        p = (
            Ether(src=self.pg3.remote_mac, dst=self.pg3.local_mac)
            / IPv6(src=self.pg3.remote_ip6, dst=remote_host_ip6)
            / TCP(sport=12345, dport=80)
        )
        self.pg3.add_stream(p)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg3.get_capture(1)
        p = capture[0]
        try:
            ip = p[IP]
            tcp = p[TCP]
            self.assertEqual(ip.src, self.nat_addr)
            self.assertEqual(ip.dst, self.pg3.remote_ip4)
            self.assertNotEqual(tcp.sport, 12345)
            external_port = tcp.sport
            self.assertEqual(tcp.dport, 80)
            self.assert_packet_checksums_valid(p)
        except:
            self.logger.error(ppp("Unexpected or invalid packet:", p))
            raise

        # out2in
        p = (
            Ether(src=self.pg3.remote_mac, dst=self.pg3.local_mac)
            / IP(src=self.pg3.remote_ip4, dst=self.nat_addr)
            / TCP(sport=80, dport=external_port)
        )
        self.pg3.add_stream(p)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg3.get_capture(1)
        p = capture[0]
        try:
            ip = p[IPv6]
            tcp = p[TCP]
            self.assertEqual(ip.src, remote_host_ip6)
            self.assertEqual(ip.dst, self.pg3.remote_ip6)
            self.assertEqual(tcp.sport, 80)
            self.assertEqual(tcp.dport, 12345)
            self.assert_packet_checksums_valid(p)
        except:
            self.logger.error(ppp("Unexpected or invalid packet:", p))
            raise

    def test_frag_in_order(self):
        """NAT64 translate fragments arriving in order"""
        self.tcp_port_in = random.randint(1025, 65535)

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg1.sw_if_index
        )

        # in2out
        data = b"a" * 200
        pkts = self.create_stream_frag_ip6(
            self.pg0, self.pg1.remote_ip4, self.tcp_port_in, 20, data
        )
        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        frags = self.pg1.get_capture(len(pkts))
        p = self.reass_frags_and_verify(frags, self.nat_addr, self.pg1.remote_ip4)
        self.assertEqual(p[TCP].dport, 20)
        self.assertNotEqual(p[TCP].sport, self.tcp_port_in)
        self.tcp_port_out = p[TCP].sport
        self.assertEqual(data, p[Raw].load)

        # out2in
        data = b"A" * 4 + b"b" * 16 + b"C" * 3
        pkts = self.create_stream_frag(
            self.pg1, self.nat_addr, 20, self.tcp_port_out, data
        )
        self.pg1.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        frags = self.pg0.get_capture(len(pkts))
        self.logger.debug(ppc("Captured:", frags))
        src = self.compose_ip6(self.pg1.remote_ip4, "64:ff9b::", 96)
        p = self.reass_frags_and_verify_ip6(frags, src, self.pg0.remote_ip6)
        self.assertEqual(p[TCP].sport, 20)
        self.assertEqual(p[TCP].dport, self.tcp_port_in)
        self.assertEqual(data, p[Raw].load)

    def test_reass_hairpinning(self):
        """NAT64 fragments hairpinning"""
        data = b"a" * 200
        server = self.pg0.remote_hosts[1]
        server_in_port = random.randint(1025, 65535)
        server_out_port = random.randint(1025, 65535)
        client_in_port = random.randint(1025, 65535)
        ip = IPv6(src="".join(["64:ff9b::", self.nat_addr]))
        nat_addr_ip6 = ip.src

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg1.sw_if_index
        )

        # add static BIB entry for server
        self.vapi.nat64_add_del_static_bib(
            i_addr=server.ip6n,
            o_addr=self.nat_addr,
            i_port=server_in_port,
            o_port=server_out_port,
            proto=IP_PROTOS.tcp,
            vrf_id=0,
            is_add=1,
        )

        # send packet from host to server
        pkts = self.create_stream_frag_ip6(
            self.pg0, self.nat_addr, client_in_port, server_out_port, data
        )
        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        frags = self.pg0.get_capture(len(pkts))
        self.logger.debug(ppc("Captured:", frags))
        p = self.reass_frags_and_verify_ip6(frags, nat_addr_ip6, server.ip6)
        self.assertNotEqual(p[TCP].sport, client_in_port)
        self.assertEqual(p[TCP].dport, server_in_port)
        self.assertEqual(data, p[Raw].load)

    def test_frag_out_of_order(self):
        """NAT64 translate fragments arriving out of order"""
        self.tcp_port_in = random.randint(1025, 65535)

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg1.sw_if_index
        )

        # in2out
        data = b"a" * 200
        pkts = self.create_stream_frag_ip6(
            self.pg0, self.pg1.remote_ip4, self.tcp_port_in, 20, data
        )
        pkts.reverse()
        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        frags = self.pg1.get_capture(len(pkts))
        p = self.reass_frags_and_verify(frags, self.nat_addr, self.pg1.remote_ip4)
        self.assertEqual(p[TCP].dport, 20)
        self.assertNotEqual(p[TCP].sport, self.tcp_port_in)
        self.tcp_port_out = p[TCP].sport
        self.assertEqual(data, p[Raw].load)

        # out2in
        data = b"A" * 4 + b"B" * 16 + b"C" * 3
        pkts = self.create_stream_frag(
            self.pg1, self.nat_addr, 20, self.tcp_port_out, data
        )
        pkts.reverse()
        self.pg1.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        frags = self.pg0.get_capture(len(pkts))
        src = self.compose_ip6(self.pg1.remote_ip4, "64:ff9b::", 96)
        p = self.reass_frags_and_verify_ip6(frags, src, self.pg0.remote_ip6)
        self.assertEqual(p[TCP].sport, 20)
        self.assertEqual(p[TCP].dport, self.tcp_port_in)
        self.assertEqual(data, p[Raw].load)

    def test_interface_addr(self):
        """Acquire NAT64 pool addresses from interface"""
        self.vapi.nat64_add_del_interface_addr(
            is_add=1, sw_if_index=self.pg4.sw_if_index
        )

        # no address in NAT64 pool
        addresses = self.vapi.nat44_address_dump()
        self.assertEqual(0, len(addresses))

        # configure interface address and check NAT64 address pool
        self.pg4.config_ip4()
        addresses = self.vapi.nat64_pool_addr_dump()
        self.assertEqual(len(addresses), 1)

        self.assertEqual(str(addresses[0].address), self.pg4.local_ip4)

        # remove interface address and check NAT64 address pool
        self.pg4.unconfig_ip4()
        addresses = self.vapi.nat64_pool_addr_dump()
        self.assertEqual(0, len(addresses))

    @unittest.skipUnless(config.extended, "part of extended tests")
    def test_ipfix_max_bibs_sessions(self):
        """IPFIX logging maximum session and BIB entries exceeded"""
        max_bibs = 1280
        max_sessions = 2560
        remote_host_ip6 = self.compose_ip6(self.pg1.remote_ip4, "64:ff9b::", 96)

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg1.sw_if_index
        )

        pkts = []
        src = ""
        for i in range(0, max_bibs):
            src = "fd01:aa::%x" % (i)
            p = (
                Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
                / IPv6(src=src, dst=remote_host_ip6)
                / TCP(sport=12345, dport=80)
            )
            pkts.append(p)
            p = (
                Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
                / IPv6(src=src, dst=remote_host_ip6)
                / TCP(sport=12345, dport=22)
            )
            pkts.append(p)
        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        self.pg1.get_capture(max_sessions)

        self.vapi.set_ipfix_exporter(
            collector_address=self.pg3.remote_ip4,
            src_address=self.pg3.local_ip4,
            path_mtu=512,
            template_interval=10,
        )
        self.vapi.nat_ipfix_enable_disable(
            domain_id=self.ipfix_domain_id, src_port=self.ipfix_src_port, enable=1
        )

        p = (
            Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
            / IPv6(src=src, dst=remote_host_ip6)
            / TCP(sport=12345, dport=25)
        ) * 3
        self.pg0.add_stream(p)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        self.pg1.assert_nothing_captured()
        self.vapi.ipfix_flush()
        capture = self.pg3.get_capture(7)
        ipfix = IPFIXDecoder()
        # first load template
        for p in capture:
            self.assertTrue(p.haslayer(IPFIX))
            self.assertEqual(p[IP].src, self.pg3.local_ip4)
            self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
            self.assertEqual(p[UDP].sport, self.ipfix_src_port)
            self.assertEqual(p[UDP].dport, 4739)
            self.assertEqual(p[IPFIX].observationDomainID, self.ipfix_domain_id)
            if p.haslayer(Template):
                ipfix.add_template(p.getlayer(Template))
        # verify events in data set
        event_count = 0
        for p in capture:
            if p.haslayer(Data):
                data = ipfix.decode_data_set(p.getlayer(Set))
                event_count += self.verify_ipfix_max_sessions(data, max_sessions)
        self.assertEqual(event_count, 1)

        p = (
            Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
            / IPv6(src=self.pg0.remote_ip6, dst=remote_host_ip6)
            / TCP(sport=12345, dport=80)
        ) * 3
        self.pg0.add_stream(p)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        self.pg1.assert_nothing_captured()
        self.vapi.ipfix_flush()
        capture = self.pg3.get_capture(1)
        # verify events in data set
        event_count = 0
        for p in capture:
            self.assertTrue(p.haslayer(IPFIX))
            self.assertEqual(p[IP].src, self.pg3.local_ip4)
            self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
            self.assertEqual(p[UDP].sport, self.ipfix_src_port)
            self.assertEqual(p[UDP].dport, 4739)
            self.assertEqual(p[IPFIX].observationDomainID, self.ipfix_domain_id)
            if p.haslayer(Data):
                data = ipfix.decode_data_set(p.getlayer(Set))
                event_count += self.verify_ipfix_max_bibs(data, max_bibs)
        self.assertEqual(event_count, 1)

    def test_ipfix_bib_ses(self):
        """IPFIX logging NAT64 BIB/session create and delete events"""
        self.tcp_port_in = random.randint(1025, 65535)
        remote_host_ip6 = self.compose_ip6(self.pg1.remote_ip4, "64:ff9b::", 96)

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg1.sw_if_index
        )
        self.vapi.set_ipfix_exporter(
            collector_address=self.pg3.remote_ip4,
            src_address=self.pg3.local_ip4,
            path_mtu=512,
            template_interval=10,
        )
        self.vapi.nat_ipfix_enable_disable(
            domain_id=self.ipfix_domain_id, src_port=self.ipfix_src_port, enable=1
        )

        # Create
        p = (
            Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
            / IPv6(src=self.pg0.remote_ip6, dst=remote_host_ip6)
            / TCP(sport=self.tcp_port_in, dport=25)
        )
        self.pg0.add_stream(p)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        p = self.pg1.get_capture(1)
        self.tcp_port_out = p[0][TCP].sport
        self.vapi.ipfix_flush()
        capture = self.pg3.get_capture(8)
        ipfix = IPFIXDecoder()
        # first load template
        for p in capture:
            self.assertTrue(p.haslayer(IPFIX))
            self.assertEqual(p[IP].src, self.pg3.local_ip4)
            self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
            self.assertEqual(p[UDP].sport, self.ipfix_src_port)
            self.assertEqual(p[UDP].dport, 4739)
            self.assertEqual(p[IPFIX].observationDomainID, self.ipfix_domain_id)
            if p.haslayer(Template):
                ipfix.add_template(p.getlayer(Template))
        # verify events in data set
        for p in capture:
            if p.haslayer(Data):
                data = ipfix.decode_data_set(p.getlayer(Set))
                if scapy.compat.orb(data[0][230]) == 10:
                    self.verify_ipfix_bib(data, 1, self.pg0.remote_ip6)
                elif scapy.compat.orb(data[0][230]) == 6:
                    self.verify_ipfix_nat64_ses(
                        data, 1, self.pg0.remote_ip6, self.pg1.remote_ip4, 25
                    )
                else:
                    self.logger.error(ppp("Unexpected or invalid packet: ", p))

        # Delete
        self.pg_enable_capture(self.pg_interfaces)
        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=0,
        )
        self.vapi.ipfix_flush()
        capture = self.pg3.get_capture(2)
        # verify events in data set
        for p in capture:
            self.assertTrue(p.haslayer(IPFIX))
            self.assertEqual(p[IP].src, self.pg3.local_ip4)
            self.assertEqual(p[IP].dst, self.pg3.remote_ip4)
            self.assertEqual(p[UDP].sport, self.ipfix_src_port)
            self.assertEqual(p[UDP].dport, 4739)
            self.assertEqual(p[IPFIX].observationDomainID, self.ipfix_domain_id)
            if p.haslayer(Data):
                data = ipfix.decode_data_set(p.getlayer(Set))
                if scapy.compat.orb(data[0][230]) == 11:
                    self.verify_ipfix_bib(data, 0, self.pg0.remote_ip6)
                elif scapy.compat.orb(data[0][230]) == 7:
                    self.verify_ipfix_nat64_ses(
                        data, 0, self.pg0.remote_ip6, self.pg1.remote_ip4, 25
                    )
                else:
                    self.logger.error(ppp("Unexpected or invalid packet: ", p))

    def test_syslog_sess(self):
        """Test syslog session creation and deletion"""
        self.tcp_port_in = random.randint(1025, 65535)
        remote_host_ip6 = self.compose_ip6(self.pg1.remote_ip4, "64:ff9b::", 96)

        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=1,
        )
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=flags, sw_if_index=self.pg0.sw_if_index
        )
        self.vapi.nat64_add_del_interface(
            is_add=1, flags=0, sw_if_index=self.pg1.sw_if_index
        )
        self.vapi.syslog_set_filter(self.SYSLOG_SEVERITY.SYSLOG_API_SEVERITY_INFO)
        self.vapi.syslog_set_sender(self.pg3.local_ip4, self.pg3.remote_ip4)

        p = (
            Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
            / IPv6(src=self.pg0.remote_ip6, dst=remote_host_ip6)
            / TCP(sport=self.tcp_port_in, dport=self.tcp_external_port)
        )
        self.pg0.add_stream(p)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        p = self.pg1.get_capture(1)
        self.tcp_port_out = p[0][TCP].sport
        capture = self.pg3.get_capture(1)
        self.verify_syslog_sess(capture[0][Raw].load, is_ip6=True)

        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        self.vapi.nat64_add_del_pool_addr_range(
            start_addr=self.nat_addr,
            end_addr=self.nat_addr,
            vrf_id=0xFFFFFFFF,
            is_add=0,
        )
        capture = self.pg3.get_capture(1)
        self.verify_syslog_sess(capture[0][Raw].load, False, True)

    def nat64_get_ses_num(self):
        """
        Return number of active NAT64 sessions.
        """
        st = self.vapi.nat64_st_dump(proto=255)
        return len(st)

    def clear_nat64(self):
        """
        Clear NAT64 configuration.
        """
        self.vapi.nat_ipfix_enable_disable(
            domain_id=self.ipfix_domain_id, src_port=self.ipfix_src_port, enable=0
        )
        self.ipfix_src_port = 4739
        self.ipfix_domain_id = 1

        self.vapi.syslog_set_filter(self.SYSLOG_SEVERITY.SYSLOG_API_SEVERITY_EMERG)

        self.vapi.nat64_set_timeouts(
            udp=300, tcp_established=7440, tcp_transitory=240, icmp=60
        )

        interfaces = self.vapi.nat64_interface_dump()
        for intf in interfaces:
            self.vapi.nat64_add_del_interface(
                is_add=0, flags=intf.flags, sw_if_index=intf.sw_if_index
            )

        bib = self.vapi.nat64_bib_dump(proto=255)
        for bibe in bib:
            if bibe.flags & self.config_flags.NAT_IS_STATIC:
                self.vapi.nat64_add_del_static_bib(
                    i_addr=bibe.i_addr,
                    o_addr=bibe.o_addr,
                    i_port=bibe.i_port,
                    o_port=bibe.o_port,
                    proto=bibe.proto,
                    vrf_id=bibe.vrf_id,
                    is_add=0,
                )

        adresses = self.vapi.nat64_pool_addr_dump()
        for addr in adresses:
            self.vapi.nat64_add_del_pool_addr_range(
                start_addr=addr.address,
                end_addr=addr.address,
                vrf_id=addr.vrf_id,
                is_add=0,
            )

        prefixes = self.vapi.nat64_prefix_dump()
        for prefix in prefixes:
            self.vapi.nat64_add_del_prefix(
                prefix=str(prefix.prefix), vrf_id=prefix.vrf_id, is_add=0
            )

        bibs = self.statistics.get_counter("/nat64/total-bibs")
        self.assertEqual(bibs[0][0], 0)
        sessions = self.statistics.get_counter("/nat64/total-sessions")
        self.assertEqual(sessions[0][0], 0)


if __name__ == "__main__":
    unittest.main(testRunner=VppTestRunner)