Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 1 | import unittest |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 2 | import socket |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 3 | |
| 4 | from scapy.layers.inet import IP, ICMP, TCP |
| 5 | from scapy.layers.ipsec import SecurityAssociation |
| 6 | from scapy.layers.l2 import Ether, Raw |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 7 | from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 8 | |
| 9 | from framework import VppTestCase, VppTestRunner |
| 10 | from util import ppp |
| 11 | |
| 12 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 13 | class IPsecIPv4Params(object): |
| 14 | addr_type = socket.AF_INET |
| 15 | addr_any = "0.0.0.0" |
| 16 | addr_bcast = "255.255.255.255" |
| 17 | addr_len = 32 |
| 18 | is_ipv6 = 0 |
| 19 | remote_tun_if_host = '1.1.1.1' |
| 20 | |
| 21 | scapy_tun_sa_id = 10 |
| 22 | scapy_tun_spi = 1001 |
| 23 | vpp_tun_sa_id = 20 |
| 24 | vpp_tun_spi = 1000 |
| 25 | |
| 26 | scapy_tra_sa_id = 30 |
| 27 | scapy_tra_spi = 2001 |
| 28 | vpp_tra_sa_id = 40 |
| 29 | vpp_tra_spi = 2000 |
| 30 | |
| 31 | auth_algo_vpp_id = 2 # internal VPP enum value for SHA1_96 |
| 32 | auth_algo = 'HMAC-SHA1-96' # scapy name |
| 33 | auth_key = 'C91KUR9GYMm5GfkEvNjX' |
| 34 | |
| 35 | crypt_algo_vpp_id = 1 # internal VPP enum value for AES_CBC_128 |
| 36 | crypt_algo = 'AES-CBC' # scapy name |
| 37 | crypt_key = 'JPjyOWBeVEQiMe7h' |
| 38 | |
| 39 | |
| 40 | class IPsecIPv6Params(object): |
| 41 | addr_type = socket.AF_INET6 |
| 42 | addr_any = "0::0" |
| 43 | addr_bcast = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" |
| 44 | addr_len = 128 |
| 45 | is_ipv6 = 1 |
| 46 | remote_tun_if_host = '1111:1111:1111:1111:1111:1111:1111:1111' |
| 47 | |
| 48 | scapy_tun_sa_id = 50 |
| 49 | scapy_tun_spi = 3001 |
| 50 | vpp_tun_sa_id = 60 |
| 51 | vpp_tun_spi = 3000 |
| 52 | |
| 53 | scapy_tra_sa_id = 70 |
| 54 | scapy_tra_spi = 4001 |
| 55 | vpp_tra_sa_id = 80 |
| 56 | vpp_tra_spi = 4000 |
| 57 | |
| 58 | auth_algo_vpp_id = 4 # internal VPP enum value for SHA_256_128 |
| 59 | auth_algo = 'SHA2-256-128' # scapy name |
| 60 | auth_key = 'C91KUR9GYMm5GfkEvNjX' |
| 61 | |
| 62 | crypt_algo_vpp_id = 3 # internal VPP enum value for AES_CBC_256 |
| 63 | crypt_algo = 'AES-CBC' # scapy name |
| 64 | crypt_key = 'JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h' |
| 65 | |
| 66 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 67 | class TemplateIpsec(VppTestCase): |
| 68 | """ |
| 69 | TRANSPORT MODE: |
| 70 | |
| 71 | ------ encrypt --- |
| 72 | |tra_if| <-------> |VPP| |
| 73 | ------ decrypt --- |
| 74 | |
| 75 | TUNNEL MODE: |
| 76 | |
| 77 | ------ encrypt --- plain --- |
| 78 | |tun_if| <------- |VPP| <------ |pg1| |
| 79 | ------ --- --- |
| 80 | |
| 81 | ------ decrypt --- plain --- |
| 82 | |tun_if| -------> |VPP| ------> |pg1| |
| 83 | ------ --- --- |
| 84 | """ |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 85 | ipv4_params = IPsecIPv4Params() |
| 86 | ipv6_params = IPsecIPv6Params() |
| 87 | params = {ipv4_params.addr_type: ipv4_params, |
| 88 | ipv6_params.addr_type: ipv6_params} |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 89 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 90 | payload = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
| 91 | |
| 92 | tun_spd_id = 1 |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 93 | tra_spd_id = 2 |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 94 | |
| 95 | vpp_esp_protocol = 1 |
| 96 | vpp_ah_protocol = 0 |
| 97 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 98 | @classmethod |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 99 | def ipsec_select_backend(cls): |
| 100 | """ empty method to be overloaded when necessary """ |
| 101 | pass |
| 102 | |
| 103 | @classmethod |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 104 | def setUpClass(cls): |
| 105 | super(TemplateIpsec, cls).setUpClass() |
| 106 | cls.create_pg_interfaces(range(3)) |
| 107 | cls.interfaces = list(cls.pg_interfaces) |
| 108 | for i in cls.interfaces: |
| 109 | i.admin_up() |
| 110 | i.config_ip4() |
| 111 | i.resolve_arp() |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 112 | i.config_ip6() |
| 113 | i.resolve_ndp() |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 114 | cls.ipsec_select_backend() |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 115 | |
| 116 | def tearDown(self): |
| 117 | super(TemplateIpsec, self).tearDown() |
| 118 | if not self.vpp_dead: |
| 119 | self.vapi.cli("show hardware") |
| 120 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 121 | def gen_encrypt_pkts(self, sa, sw_intf, src, dst, count=1): |
| 122 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
| 123 | sa.encrypt(IP(src=src, dst=dst) / ICMP() / self.payload) |
| 124 | for i in range(count)] |
| 125 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 126 | def gen_encrypt_pkts6(self, sa, sw_intf, src, dst, count=1): |
| 127 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
| 128 | sa.encrypt(IPv6(src=src, dst=dst) / |
| 129 | ICMPv6EchoRequest(id=0, seq=1, data=self.payload)) |
| 130 | for i in range(count)] |
| 131 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 132 | def gen_pkts(self, sw_intf, src, dst, count=1): |
| 133 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
| 134 | IP(src=src, dst=dst) / ICMP() / self.payload |
| 135 | for i in range(count)] |
| 136 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 137 | def gen_pkts6(self, sw_intf, src, dst, count=1): |
| 138 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
| 139 | IPv6(src=src, dst=dst) / |
| 140 | ICMPv6EchoRequest(id=0, seq=1, data=self.payload) |
| 141 | for i in range(count)] |
| 142 | |
| 143 | def configure_sa_tun(self, params): |
| 144 | ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6} |
| 145 | scapy_tun_sa = SecurityAssociation( |
| 146 | self.encryption_type, spi=params.vpp_tun_spi, |
| 147 | crypt_algo=params.crypt_algo, crypt_key=params.crypt_key, |
| 148 | auth_algo=params.auth_algo, auth_key=params.auth_key, |
| 149 | tunnel_header=ip_class_by_addr_type[params.addr_type]( |
| 150 | src=self.tun_if.remote_addr[params.addr_type], |
| 151 | dst=self.tun_if.local_addr[params.addr_type])) |
| 152 | vpp_tun_sa = SecurityAssociation( |
| 153 | self.encryption_type, spi=params.scapy_tun_spi, |
| 154 | crypt_algo=params.crypt_algo, crypt_key=params.crypt_key, |
| 155 | auth_algo=params.auth_algo, auth_key=params.auth_key, |
| 156 | tunnel_header=ip_class_by_addr_type[params.addr_type]( |
| 157 | dst=self.tun_if.remote_addr[params.addr_type], |
| 158 | src=self.tun_if.local_addr[params.addr_type])) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 159 | return vpp_tun_sa, scapy_tun_sa |
| 160 | |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame^] | 161 | @classmethod |
| 162 | def configure_sa_tra(cls, params): |
| 163 | params.scapy_tra_sa = SecurityAssociation(cls.encryption_type, |
| 164 | spi=params.vpp_tra_spi, |
| 165 | crypt_algo=params.crypt_algo, |
| 166 | crypt_key=params.crypt_key, |
| 167 | auth_algo=params.auth_algo, |
| 168 | auth_key=params.auth_key) |
| 169 | params.vpp_tra_sa = SecurityAssociation(cls.encryption_type, |
| 170 | spi=params.scapy_tra_spi, |
| 171 | crypt_algo=params.crypt_algo, |
| 172 | crypt_key=params.crypt_key, |
| 173 | auth_algo=params.auth_algo, |
| 174 | auth_key=params.auth_key) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 175 | |
| 176 | |
| 177 | class IpsecTcpTests(object): |
| 178 | def test_tcp_checksum(self): |
| 179 | """ verify checksum correctness for vpp generated packets """ |
| 180 | self.vapi.cli("test http server") |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 181 | p = self.params[socket.AF_INET] |
| 182 | vpp_tun_sa, scapy_tun_sa = self.configure_sa_tun(p) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 183 | send = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) / |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 184 | scapy_tun_sa.encrypt(IP(src=p.remote_tun_if_host, |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 185 | dst=self.tun_if.local_ip4) / |
| 186 | TCP(flags='S', dport=80))) |
| 187 | self.logger.debug(ppp("Sending packet:", send)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 188 | recv = self.send_and_expect(self.tun_if, [send], self.tun_if) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 189 | recv = recv[0] |
| 190 | decrypted = vpp_tun_sa.decrypt(recv[IP]) |
| 191 | self.assert_packet_checksums_valid(decrypted) |
| 192 | |
| 193 | |
| 194 | class IpsecTraTests(object): |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame^] | 195 | def test_tra_anti_replay(self, count=1): |
| 196 | """ ipsec v4 transport anti-reply test """ |
| 197 | p = self.params[socket.AF_INET] |
| 198 | |
| 199 | # fire in a packet with seq number 1 |
| 200 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 201 | dst=self.tra_if.local_mac) / |
| 202 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 203 | dst=self.tra_if.local_ip4) / |
| 204 | ICMP(), |
| 205 | seq_num=1)) |
| 206 | recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 207 | |
| 208 | # now move the window over to 235 |
| 209 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 210 | dst=self.tra_if.local_mac) / |
| 211 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 212 | dst=self.tra_if.local_ip4) / |
| 213 | ICMP(), |
| 214 | seq_num=235)) |
| 215 | recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 216 | |
| 217 | # the window size is 64 packets |
| 218 | # in window are still accepted |
| 219 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 220 | dst=self.tra_if.local_mac) / |
| 221 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 222 | dst=self.tra_if.local_ip4) / |
| 223 | ICMP(), |
| 224 | seq_num=172)) |
| 225 | recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 226 | |
| 227 | # out of window are dropped |
| 228 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 229 | dst=self.tra_if.local_mac) / |
| 230 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 231 | dst=self.tra_if.local_ip4) / |
| 232 | ICMP(), |
| 233 | seq_num=17)) |
| 234 | self.send_and_assert_no_replies(self.tra_if, pkt * 17) |
| 235 | |
| 236 | err = self.statistics.get_counter( |
| 237 | '/err/%s/SA replayed packet' % self.tra4_decrypt_node_name) |
| 238 | self.assertEqual(err, 17) |
| 239 | |
| 240 | # a packet that does not decrypt does not move the window forward |
| 241 | bogus_sa = SecurityAssociation(self.encryption_type, |
| 242 | p.vpp_tra_spi) |
| 243 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 244 | dst=self.tra_if.local_mac) / |
| 245 | bogus_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 246 | dst=self.tra_if.local_ip4) / |
| 247 | ICMP(), |
| 248 | seq_num=350)) |
| 249 | self.send_and_assert_no_replies(self.tra_if, pkt * 17) |
| 250 | |
| 251 | err = self.statistics.get_counter( |
| 252 | '/err/%s/Integrity check failed' % self.tra4_decrypt_node_name) |
| 253 | self.assertEqual(err, 17) |
| 254 | |
| 255 | # which we can determine since this packet is still in the window |
| 256 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 257 | dst=self.tra_if.local_mac) / |
| 258 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 259 | dst=self.tra_if.local_ip4) / |
| 260 | ICMP(), |
| 261 | seq_num=234)) |
| 262 | recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 263 | |
| 264 | # move the security-associations seq number on to the last we used |
| 265 | p.scapy_tra_sa.seq_num = 351 |
| 266 | p.vpp_tra_sa.seq_num = 351 |
| 267 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 268 | def test_tra_basic(self, count=1): |
| 269 | """ ipsec v4 transport basic test """ |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 270 | self.vapi.cli("clear errors") |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 271 | try: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 272 | p = self.params[socket.AF_INET] |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame^] | 273 | send_pkts = self.gen_encrypt_pkts(p.scapy_tra_sa, self.tra_if, |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 274 | src=self.tra_if.remote_ip4, |
| 275 | dst=self.tra_if.local_ip4, |
| 276 | count=count) |
| 277 | recv_pkts = self.send_and_expect(self.tra_if, send_pkts, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 278 | self.tra_if) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame^] | 279 | for rx in recv_pkts: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 280 | try: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame^] | 281 | decrypted = p.vpp_tra_sa.decrypt(rx[IP]) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 282 | self.assert_packet_checksums_valid(decrypted) |
| 283 | except: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame^] | 284 | self.logger.debug(ppp("Unexpected packet:", rx)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 285 | raise |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 286 | finally: |
| 287 | self.logger.info(self.vapi.ppcli("show error")) |
| 288 | self.logger.info(self.vapi.ppcli("show ipsec")) |
| 289 | |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 290 | self.assert_packet_counter_equal(self.tra4_encrypt_node_name, count) |
| 291 | self.assert_packet_counter_equal(self.tra4_decrypt_node_name, count) |
| 292 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 293 | def test_tra_burst(self): |
| 294 | """ ipsec v4 transport burst test """ |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 295 | self.test_tra_basic(count=257) |
| 296 | |
| 297 | def test_tra_basic6(self, count=1): |
| 298 | """ ipsec v6 transport basic test """ |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 299 | self.vapi.cli("clear errors") |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 300 | try: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 301 | p = self.params[socket.AF_INET6] |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame^] | 302 | send_pkts = self.gen_encrypt_pkts6(p.scapy_tra_sa, self.tra_if, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 303 | src=self.tra_if.remote_ip6, |
| 304 | dst=self.tra_if.local_ip6, |
| 305 | count=count) |
| 306 | recv_pkts = self.send_and_expect(self.tra_if, send_pkts, |
| 307 | self.tra_if) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame^] | 308 | for rx in recv_pkts: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 309 | try: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame^] | 310 | decrypted = p.vpp_tra_sa.decrypt(rx[IPv6]) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 311 | self.assert_packet_checksums_valid(decrypted) |
| 312 | except: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame^] | 313 | self.logger.debug(ppp("Unexpected packet:", rx)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 314 | raise |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 315 | finally: |
| 316 | self.logger.info(self.vapi.ppcli("show error")) |
| 317 | self.logger.info(self.vapi.ppcli("show ipsec")) |
| 318 | |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 319 | self.assert_packet_counter_equal(self.tra6_encrypt_node_name, count) |
| 320 | self.assert_packet_counter_equal(self.tra6_decrypt_node_name, count) |
| 321 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 322 | def test_tra_burst6(self): |
| 323 | """ ipsec v6 transport burst test """ |
| 324 | self.test_tra_basic6(count=257) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 325 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 326 | |
| 327 | class IpsecTun4Tests(object): |
| 328 | def test_tun_basic44(self, count=1): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 329 | """ ipsec 4o4 tunnel basic test """ |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 330 | self.vapi.cli("clear errors") |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 331 | try: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 332 | p = self.params[socket.AF_INET] |
| 333 | vpp_tun_sa, scapy_tun_sa = self.configure_sa_tun(p) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 334 | send_pkts = self.gen_encrypt_pkts(scapy_tun_sa, self.tun_if, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 335 | src=p.remote_tun_if_host, |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 336 | dst=self.pg1.remote_ip4, |
| 337 | count=count) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 338 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 339 | for recv_pkt in recv_pkts: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 340 | self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 341 | self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4) |
| 342 | self.assert_packet_checksums_valid(recv_pkt) |
| 343 | send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 344 | dst=p.remote_tun_if_host, count=count) |
| 345 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 346 | for recv_pkt in recv_pkts: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 347 | try: |
| 348 | decrypt_pkt = vpp_tun_sa.decrypt(recv_pkt[IP]) |
| 349 | if not decrypt_pkt.haslayer(IP): |
| 350 | decrypt_pkt = IP(decrypt_pkt[Raw].load) |
| 351 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4) |
| 352 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host) |
| 353 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 354 | except: |
| 355 | self.logger.debug(ppp("Unexpected packet:", recv_pkt)) |
| 356 | try: |
| 357 | self.logger.debug( |
| 358 | ppp("Decrypted packet:", decrypt_pkt)) |
| 359 | except: |
| 360 | pass |
| 361 | raise |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 362 | finally: |
| 363 | self.logger.info(self.vapi.ppcli("show error")) |
| 364 | self.logger.info(self.vapi.ppcli("show ipsec")) |
| 365 | |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 366 | self.assert_packet_counter_equal(self.tun4_encrypt_node_name, count) |
| 367 | self.assert_packet_counter_equal(self.tun4_decrypt_node_name, count) |
| 368 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 369 | def test_tun_burst44(self): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 370 | """ ipsec 4o4 tunnel burst test """ |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 371 | self.test_tun_basic44(count=257) |
| 372 | |
| 373 | |
| 374 | class IpsecTun6Tests(object): |
| 375 | def test_tun_basic66(self, count=1): |
| 376 | """ ipsec 6o6 tunnel basic test """ |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 377 | self.vapi.cli("clear errors") |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 378 | try: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 379 | p = self.params[socket.AF_INET6] |
| 380 | vpp_tun_sa, scapy_tun_sa = self.configure_sa_tun(p) |
| 381 | send_pkts = self.gen_encrypt_pkts6(scapy_tun_sa, self.tun_if, |
| 382 | src=p.remote_tun_if_host, |
| 383 | dst=self.pg1.remote_ip6, |
| 384 | count=count) |
| 385 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
| 386 | for recv_pkt in recv_pkts: |
| 387 | self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host) |
| 388 | self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6) |
| 389 | self.assert_packet_checksums_valid(recv_pkt) |
| 390 | send_pkts = self.gen_pkts6(self.pg1, src=self.pg1.remote_ip6, |
| 391 | dst=p.remote_tun_if_host, |
| 392 | count=count) |
| 393 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if) |
| 394 | for recv_pkt in recv_pkts: |
| 395 | try: |
| 396 | decrypt_pkt = vpp_tun_sa.decrypt(recv_pkt[IPv6]) |
| 397 | if not decrypt_pkt.haslayer(IPv6): |
| 398 | decrypt_pkt = IPv6(decrypt_pkt[Raw].load) |
| 399 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6) |
| 400 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host) |
| 401 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 402 | except: |
| 403 | self.logger.debug(ppp("Unexpected packet:", recv_pkt)) |
| 404 | try: |
| 405 | self.logger.debug( |
| 406 | ppp("Decrypted packet:", decrypt_pkt)) |
| 407 | except: |
| 408 | pass |
| 409 | raise |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 410 | finally: |
| 411 | self.logger.info(self.vapi.ppcli("show error")) |
| 412 | self.logger.info(self.vapi.ppcli("show ipsec")) |
| 413 | |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 414 | self.assert_packet_counter_equal(self.tun6_encrypt_node_name, count) |
| 415 | self.assert_packet_counter_equal(self.tun6_decrypt_node_name, count) |
| 416 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 417 | def test_tun_burst66(self): |
| 418 | """ ipsec 6o6 tunnel burst test """ |
| 419 | self.test_tun_basic66(count=257) |
| 420 | |
| 421 | |
| 422 | class IpsecTunTests(IpsecTun4Tests, IpsecTun6Tests): |
| 423 | pass |
| 424 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 425 | |
| 426 | if __name__ == '__main__': |
| 427 | unittest.main(testRunner=VppTestRunner) |