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 |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 3 | import struct |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 4 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 5 | from scapy.layers.inet import IP, ICMP, TCP, UDP |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 6 | from scapy.layers.ipsec import SecurityAssociation, ESP |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 7 | from scapy.layers.l2 import Ether |
Paul Vinciguerra | 582eac5 | 2020-04-03 12:18:40 -0400 | [diff] [blame] | 8 | from scapy.packet import raw, Raw |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 9 | from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest, IPv6ExtHdrHopByHop, \ |
| 10 | IPv6ExtHdrFragment, IPv6ExtHdrDestOpt |
| 11 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 12 | |
| 13 | from framework import VppTestCase, VppTestRunner |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 14 | from util import ppp, reassemble4, fragment_rfc791, fragment_rfc8200 |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 15 | from vpp_papi import VppEnum |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 16 | |
| 17 | |
Paul Vinciguerra | e061dad | 2020-12-04 14:57:51 -0500 | [diff] [blame] | 18 | class IPsecIPv4Params: |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 19 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 20 | addr_type = socket.AF_INET |
| 21 | addr_any = "0.0.0.0" |
| 22 | addr_bcast = "255.255.255.255" |
| 23 | addr_len = 32 |
| 24 | is_ipv6 = 0 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 25 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 26 | def __init__(self): |
| 27 | self.remote_tun_if_host = '1.1.1.1' |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 28 | self.remote_tun_if_host6 = '1111::1' |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 29 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 30 | self.scapy_tun_sa_id = 100 |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 31 | self.scapy_tun_spi = 1000 |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 32 | self.vpp_tun_sa_id = 200 |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 33 | self.vpp_tun_spi = 2000 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 34 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 35 | self.scapy_tra_sa_id = 300 |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 36 | self.scapy_tra_spi = 3000 |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 37 | self.vpp_tra_sa_id = 400 |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 38 | self.vpp_tra_spi = 4000 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 39 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 40 | self.outer_hop_limit = 64 |
| 41 | self.inner_hop_limit = 255 |
| 42 | self.outer_flow_label = 0 |
| 43 | self.inner_flow_label = 0x12345 |
| 44 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 45 | self.auth_algo_vpp_id = (VppEnum.vl_api_ipsec_integ_alg_t. |
| 46 | IPSEC_API_INTEG_ALG_SHA1_96) |
| 47 | self.auth_algo = 'HMAC-SHA1-96' # scapy name |
Ole Troan | 64e978b | 2019-10-17 21:40:36 +0200 | [diff] [blame] | 48 | self.auth_key = b'C91KUR9GYMm5GfkEvNjX' |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 49 | |
| 50 | self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t. |
| 51 | IPSEC_API_CRYPTO_ALG_AES_CBC_128) |
| 52 | self.crypt_algo = 'AES-CBC' # scapy name |
Ole Troan | 64e978b | 2019-10-17 21:40:36 +0200 | [diff] [blame] | 53 | self.crypt_key = b'JPjyOWBeVEQiMe7h' |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 54 | self.salt = 0 |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 55 | self.flags = 0 |
| 56 | self.nat_header = None |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 57 | self.tun_flags = (VppEnum.vl_api_tunnel_encap_decap_flags_t. |
| 58 | TUNNEL_API_ENCAP_DECAP_FLAG_NONE) |
| 59 | self.dscp = 0 |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 60 | self.async_mode = False |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 61 | |
| 62 | |
Paul Vinciguerra | e061dad | 2020-12-04 14:57:51 -0500 | [diff] [blame] | 63 | class IPsecIPv6Params: |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 64 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 65 | addr_type = socket.AF_INET6 |
| 66 | addr_any = "0::0" |
| 67 | addr_bcast = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" |
| 68 | addr_len = 128 |
| 69 | is_ipv6 = 1 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 70 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 71 | def __init__(self): |
| 72 | self.remote_tun_if_host = '1111:1111:1111:1111:1111:1111:1111:1111' |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 73 | self.remote_tun_if_host4 = '1.1.1.1' |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 74 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 75 | self.scapy_tun_sa_id = 500 |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 76 | self.scapy_tun_spi = 3001 |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 77 | self.vpp_tun_sa_id = 600 |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 78 | self.vpp_tun_spi = 3000 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 79 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 80 | self.scapy_tra_sa_id = 700 |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 81 | self.scapy_tra_spi = 4001 |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 82 | self.vpp_tra_sa_id = 800 |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 83 | self.vpp_tra_spi = 4000 |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 84 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 85 | self.outer_hop_limit = 64 |
| 86 | self.inner_hop_limit = 255 |
| 87 | self.outer_flow_label = 0 |
| 88 | self.inner_flow_label = 0x12345 |
| 89 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 90 | self.auth_algo_vpp_id = (VppEnum.vl_api_ipsec_integ_alg_t. |
Neale Ranns | 1091c4a | 2019-04-08 14:48:23 +0000 | [diff] [blame] | 91 | IPSEC_API_INTEG_ALG_SHA1_96) |
| 92 | self.auth_algo = 'HMAC-SHA1-96' # scapy name |
Ole Troan | 64e978b | 2019-10-17 21:40:36 +0200 | [diff] [blame] | 93 | self.auth_key = b'C91KUR9GYMm5GfkEvNjX' |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 94 | |
| 95 | self.crypt_algo_vpp_id = (VppEnum.vl_api_ipsec_crypto_alg_t. |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 96 | IPSEC_API_CRYPTO_ALG_AES_CBC_128) |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 97 | self.crypt_algo = 'AES-CBC' # scapy name |
Ole Troan | 64e978b | 2019-10-17 21:40:36 +0200 | [diff] [blame] | 98 | self.crypt_key = b'JPjyOWBeVEQiMe7h' |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 99 | self.salt = 0 |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 100 | self.flags = 0 |
| 101 | self.nat_header = None |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 102 | self.tun_flags = (VppEnum.vl_api_tunnel_encap_decap_flags_t. |
| 103 | TUNNEL_API_ENCAP_DECAP_FLAG_NONE) |
| 104 | self.dscp = 0 |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 105 | self.async_mode = False |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 106 | |
| 107 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 108 | def mk_scapy_crypt_key(p): |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 109 | if p.crypt_algo in ("AES-GCM", "AES-CTR"): |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 110 | return p.crypt_key + struct.pack("!I", p.salt) |
| 111 | else: |
| 112 | return p.crypt_key |
| 113 | |
| 114 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 115 | def config_tun_params(p, encryption_type, tun_if): |
| 116 | ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6} |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 117 | esn_en = bool(p.flags & (VppEnum.vl_api_ipsec_sad_flags_t. |
| 118 | IPSEC_API_SAD_FLAG_USE_ESN)) |
Neale Ranns | f3a6622 | 2020-01-02 05:04:00 +0000 | [diff] [blame] | 119 | p.tun_dst = tun_if.remote_addr[p.addr_type] |
| 120 | p.tun_src = tun_if.local_addr[p.addr_type] |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 121 | crypt_key = mk_scapy_crypt_key(p) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 122 | p.scapy_tun_sa = SecurityAssociation( |
| 123 | encryption_type, spi=p.vpp_tun_spi, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 124 | crypt_algo=p.crypt_algo, |
| 125 | crypt_key=crypt_key, |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 126 | auth_algo=p.auth_algo, auth_key=p.auth_key, |
| 127 | tunnel_header=ip_class_by_addr_type[p.addr_type]( |
Neale Ranns | f3a6622 | 2020-01-02 05:04:00 +0000 | [diff] [blame] | 128 | src=p.tun_dst, |
| 129 | dst=p.tun_src), |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 130 | nat_t_header=p.nat_header, |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 131 | esn_en=esn_en) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 132 | p.vpp_tun_sa = SecurityAssociation( |
| 133 | encryption_type, spi=p.scapy_tun_spi, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 134 | crypt_algo=p.crypt_algo, |
| 135 | crypt_key=crypt_key, |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 136 | auth_algo=p.auth_algo, auth_key=p.auth_key, |
| 137 | tunnel_header=ip_class_by_addr_type[p.addr_type]( |
Neale Ranns | f3a6622 | 2020-01-02 05:04:00 +0000 | [diff] [blame] | 138 | dst=p.tun_dst, |
| 139 | src=p.tun_src), |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 140 | nat_t_header=p.nat_header, |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 141 | esn_en=esn_en) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 142 | |
| 143 | |
| 144 | def config_tra_params(p, encryption_type): |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 145 | esn_en = bool(p.flags & (VppEnum.vl_api_ipsec_sad_flags_t. |
| 146 | IPSEC_API_SAD_FLAG_USE_ESN)) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 147 | crypt_key = mk_scapy_crypt_key(p) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 148 | p.scapy_tra_sa = SecurityAssociation( |
| 149 | encryption_type, |
| 150 | spi=p.vpp_tra_spi, |
| 151 | crypt_algo=p.crypt_algo, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 152 | crypt_key=crypt_key, |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 153 | auth_algo=p.auth_algo, |
| 154 | auth_key=p.auth_key, |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 155 | nat_t_header=p.nat_header, |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 156 | esn_en=esn_en) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 157 | p.vpp_tra_sa = SecurityAssociation( |
| 158 | encryption_type, |
| 159 | spi=p.scapy_tra_spi, |
| 160 | crypt_algo=p.crypt_algo, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 161 | crypt_key=crypt_key, |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 162 | auth_algo=p.auth_algo, |
| 163 | auth_key=p.auth_key, |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 164 | nat_t_header=p.nat_header, |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 165 | esn_en=esn_en) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 166 | |
| 167 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 168 | class TemplateIpsec(VppTestCase): |
| 169 | """ |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 170 | TRANSPORT MODE:: |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 171 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 172 | ------ encrypt --- |
| 173 | |tra_if| <-------> |VPP| |
| 174 | ------ decrypt --- |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 175 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 176 | TUNNEL MODE:: |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 177 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 178 | ------ encrypt --- plain --- |
| 179 | |tun_if| <------- |VPP| <------ |pg1| |
| 180 | ------ --- --- |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 181 | |
Dave Wallace | d170681 | 2021-08-12 18:36:02 -0400 | [diff] [blame] | 182 | ------ decrypt --- plain --- |
| 183 | |tun_if| -------> |VPP| ------> |pg1| |
| 184 | ------ --- --- |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 185 | """ |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 186 | tun_spd_id = 1 |
| 187 | tra_spd_id = 2 |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 188 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 189 | def ipsec_select_backend(self): |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 190 | """ empty method to be overloaded when necessary """ |
| 191 | pass |
| 192 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 193 | @classmethod |
| 194 | def setUpClass(cls): |
| 195 | super(TemplateIpsec, cls).setUpClass() |
| 196 | |
| 197 | @classmethod |
| 198 | def tearDownClass(cls): |
| 199 | super(TemplateIpsec, cls).tearDownClass() |
| 200 | |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 201 | def setup_params(self): |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 202 | if not hasattr(self, 'ipv4_params'): |
| 203 | self.ipv4_params = IPsecIPv4Params() |
| 204 | if not hasattr(self, 'ipv6_params'): |
| 205 | self.ipv6_params = IPsecIPv6Params() |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 206 | self.params = {self.ipv4_params.addr_type: self.ipv4_params, |
| 207 | self.ipv6_params.addr_type: self.ipv6_params} |
| 208 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 209 | def config_interfaces(self): |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 210 | self.create_pg_interfaces(range(3)) |
| 211 | self.interfaces = list(self.pg_interfaces) |
| 212 | for i in self.interfaces: |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 213 | i.admin_up() |
| 214 | i.config_ip4() |
| 215 | i.resolve_arp() |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 216 | i.config_ip6() |
| 217 | i.resolve_ndp() |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 218 | |
| 219 | def setUp(self): |
| 220 | super(TemplateIpsec, self).setUp() |
| 221 | |
| 222 | self.setup_params() |
| 223 | |
| 224 | self.vpp_esp_protocol = (VppEnum.vl_api_ipsec_proto_t. |
| 225 | IPSEC_API_PROTO_ESP) |
| 226 | self.vpp_ah_protocol = (VppEnum.vl_api_ipsec_proto_t. |
| 227 | IPSEC_API_PROTO_AH) |
| 228 | |
| 229 | self.config_interfaces() |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame] | 230 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 231 | self.ipsec_select_backend() |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 232 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 233 | def unconfig_interfaces(self): |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 234 | for i in self.interfaces: |
| 235 | i.admin_down() |
| 236 | i.unconfig_ip4() |
| 237 | i.unconfig_ip6() |
| 238 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 239 | def tearDown(self): |
| 240 | super(TemplateIpsec, self).tearDown() |
| 241 | |
| 242 | self.unconfig_interfaces() |
| 243 | |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame] | 244 | def show_commands_at_teardown(self): |
| 245 | self.logger.info(self.vapi.cli("show hardware")) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 246 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 247 | def gen_encrypt_pkts(self, p, sa, sw_intf, src, dst, count=1, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 248 | payload_size=54): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 249 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 250 | sa.encrypt(IP(src=src, dst=dst) / |
Ole Troan | 8b76c23 | 2019-10-21 20:55:13 +0200 | [diff] [blame] | 251 | ICMP() / Raw(b'X' * payload_size)) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 252 | for i in range(count)] |
| 253 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 254 | def gen_encrypt_pkts6(self, p, sa, sw_intf, src, dst, count=1, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 255 | payload_size=54): |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 256 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 257 | sa.encrypt(IPv6(src=src, dst=dst, |
| 258 | hlim=p.inner_hop_limit, |
| 259 | fl=p.inner_flow_label) / |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 260 | ICMPv6EchoRequest(id=0, seq=1, |
| 261 | data='X' * payload_size)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 262 | for i in range(count)] |
| 263 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 264 | def gen_pkts(self, sw_intf, src, dst, count=1, payload_size=54): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 265 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
Ole Troan | 8b76c23 | 2019-10-21 20:55:13 +0200 | [diff] [blame] | 266 | IP(src=src, dst=dst) / ICMP() / Raw(b'X' * payload_size) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 267 | for i in range(count)] |
| 268 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 269 | def gen_pkts6(self, p, sw_intf, src, dst, count=1, payload_size=54): |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 270 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 271 | IPv6(src=src, dst=dst, |
| 272 | hlim=p.inner_hop_limit, fl=p.inner_flow_label) / |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 273 | ICMPv6EchoRequest(id=0, seq=1, data='X' * payload_size) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 274 | for i in range(count)] |
| 275 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 276 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 277 | class IpsecTcp(object): |
| 278 | def verify_tcp_checksum(self): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 279 | self.vapi.cli("test http server") |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 280 | p = self.params[socket.AF_INET] |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 281 | send = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) / |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 282 | p.scapy_tun_sa.encrypt(IP(src=p.remote_tun_if_host, |
| 283 | dst=self.tun_if.local_ip4) / |
| 284 | TCP(flags='S', dport=80))) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 285 | self.logger.debug(ppp("Sending packet:", send)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 286 | recv = self.send_and_expect(self.tun_if, [send], self.tun_if) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 287 | recv = recv[0] |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 288 | decrypted = p.vpp_tun_sa.decrypt(recv[IP]) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 289 | self.assert_packet_checksums_valid(decrypted) |
| 290 | |
| 291 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 292 | class IpsecTcpTests(IpsecTcp): |
| 293 | def test_tcp_checksum(self): |
| 294 | """ verify checksum correctness for vpp generated packets """ |
| 295 | self.verify_tcp_checksum() |
| 296 | |
| 297 | |
| 298 | class IpsecTra4(object): |
| 299 | """ verify methods for Transport v4 """ |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 300 | def get_replay_counts(self, p): |
| 301 | replay_node_name = ('/err/%s/SA replayed packet' % |
| 302 | self.tra4_decrypt_node_name[0]) |
| 303 | count = self.statistics.get_err_counter(replay_node_name) |
| 304 | |
| 305 | if p.async_mode: |
| 306 | replay_post_node_name = ('/err/%s/SA replayed packet' % |
| 307 | self.tra4_decrypt_node_name[p.async_mode]) |
| 308 | count += self.statistics.get_err_counter(replay_post_node_name) |
| 309 | |
| 310 | return count |
| 311 | |
| 312 | def get_hash_failed_counts(self, p): |
| 313 | if ESP == self.encryption_type and p.crypt_algo == "AES-GCM": |
| 314 | hash_failed_node_name = ('/err/%s/ESP decryption failed' % |
| 315 | self.tra4_decrypt_node_name[p.async_mode]) |
| 316 | else: |
| 317 | hash_failed_node_name = ('/err/%s/Integrity check failed' % |
| 318 | self.tra4_decrypt_node_name[p.async_mode]) |
| 319 | count = self.statistics.get_err_counter(hash_failed_node_name) |
| 320 | |
| 321 | if p.async_mode: |
| 322 | count += self.statistics.get_err_counter( |
| 323 | '/err/crypto-dispatch/bad-hmac') |
| 324 | |
| 325 | return count |
| 326 | |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 327 | def verify_hi_seq_num(self): |
| 328 | p = self.params[socket.AF_INET] |
| 329 | saf = VppEnum.vl_api_ipsec_sad_flags_t |
| 330 | esn_on = p.vpp_tra_sa.esn_en |
| 331 | ar_on = p.flags & saf.IPSEC_API_SAD_FLAG_USE_ANTI_REPLAY |
| 332 | |
| 333 | seq_cycle_node_name = \ |
| 334 | ('/err/%s/sequence number cycled (packet dropped)' % |
| 335 | self.tra4_encrypt_node_name) |
| 336 | replay_count = self.get_replay_counts(p) |
| 337 | hash_failed_count = self.get_hash_failed_counts(p) |
| 338 | seq_cycle_count = self.statistics.get_err_counter(seq_cycle_node_name) |
| 339 | |
| 340 | # a few packets so we get the rx seq number above the window size and |
| 341 | # thus can simulate a wrap with an out of window packet |
| 342 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 343 | dst=self.tra_if.local_mac) / |
| 344 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 345 | dst=self.tra_if.local_ip4) / |
| 346 | ICMP(), |
| 347 | seq_num=seq)) |
| 348 | for seq in range(63, 80)] |
| 349 | recv_pkts = self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 350 | |
| 351 | # these 4 packets will all choose seq-num 0 to decrpyt since none |
| 352 | # are out of window when first checked. however, once #200 has |
| 353 | # decrypted it will move the window to 200 and has #81 is out of |
| 354 | # window. this packet should be dropped. |
| 355 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 356 | dst=self.tra_if.local_mac) / |
| 357 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 358 | dst=self.tra_if.local_ip4) / |
| 359 | ICMP(), |
| 360 | seq_num=200)), |
| 361 | (Ether(src=self.tra_if.remote_mac, |
| 362 | dst=self.tra_if.local_mac) / |
| 363 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 364 | dst=self.tra_if.local_ip4) / |
| 365 | ICMP(), |
| 366 | seq_num=81)), |
| 367 | (Ether(src=self.tra_if.remote_mac, |
| 368 | dst=self.tra_if.local_mac) / |
| 369 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 370 | dst=self.tra_if.local_ip4) / |
| 371 | ICMP(), |
| 372 | seq_num=201)), |
| 373 | (Ether(src=self.tra_if.remote_mac, |
| 374 | dst=self.tra_if.local_mac) / |
| 375 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 376 | dst=self.tra_if.local_ip4) / |
| 377 | ICMP(), |
| 378 | seq_num=202))] |
| 379 | |
| 380 | # if anti-replay is off then we won't drop #81 |
| 381 | n_rx = 3 if ar_on else 4 |
| 382 | self.send_and_expect(self.tra_if, pkts, self.tra_if, n_rx=n_rx) |
| 383 | # this packet is one before the wrap |
| 384 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 385 | dst=self.tra_if.local_mac) / |
| 386 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 387 | dst=self.tra_if.local_ip4) / |
| 388 | ICMP(), |
| 389 | seq_num=203))] |
| 390 | recv_pkts = self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 391 | |
| 392 | # move the window over half way to a wrap |
| 393 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 394 | dst=self.tra_if.local_mac) / |
| 395 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 396 | dst=self.tra_if.local_ip4) / |
| 397 | ICMP(), |
| 398 | seq_num=0x80000001))] |
| 399 | recv_pkts = self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 400 | |
| 401 | # anti-replay will drop old packets, no anti-replay will not |
| 402 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 403 | dst=self.tra_if.local_mac) / |
| 404 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 405 | dst=self.tra_if.local_ip4) / |
| 406 | ICMP(), |
| 407 | seq_num=0x44000001))] |
| 408 | |
| 409 | if ar_on: |
| 410 | self.send_and_assert_no_replies(self.tra_if, pkts) |
| 411 | else: |
| 412 | recv_pkts = self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 413 | |
| 414 | if esn_on: |
| 415 | # |
| 416 | # validate wrapping the ESN |
| 417 | # |
| 418 | |
| 419 | # wrap scapy's TX SA SN |
| 420 | p.scapy_tra_sa.seq_num = 0x100000005 |
| 421 | |
| 422 | # send a packet that wraps the window for both AR and no AR |
| 423 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 424 | dst=self.tra_if.local_mac) / |
| 425 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 426 | dst=self.tra_if.local_ip4) / |
| 427 | ICMP(), |
| 428 | seq_num=0x100000005))] |
| 429 | |
| 430 | rxs = self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 431 | for rx in rxs: |
| 432 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
| 433 | |
| 434 | # move the window forward to half way to the next wrap |
| 435 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 436 | dst=self.tra_if.local_mac) / |
| 437 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 438 | dst=self.tra_if.local_ip4) / |
| 439 | ICMP(), |
| 440 | seq_num=0x180000005))] |
| 441 | |
| 442 | rxs = self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 443 | |
| 444 | # a packet less than 2^30 from the current position is: |
| 445 | # - AR: out of window and dropped |
| 446 | # - non-AR: accepted |
| 447 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 448 | dst=self.tra_if.local_mac) / |
| 449 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 450 | dst=self.tra_if.local_ip4) / |
| 451 | ICMP(), |
| 452 | seq_num=0x170000005))] |
| 453 | |
| 454 | if ar_on: |
| 455 | self.send_and_assert_no_replies(self.tra_if, pkts) |
| 456 | else: |
| 457 | self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 458 | |
| 459 | # a packet more than 2^30 from the current position is: |
| 460 | # - AR: out of window and dropped |
| 461 | # - non-AR: considered a wrap, but since it's not a wrap |
| 462 | # it won't decrpyt and so will be dropped |
| 463 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 464 | dst=self.tra_if.local_mac) / |
| 465 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 466 | dst=self.tra_if.local_ip4) / |
| 467 | ICMP(), |
| 468 | seq_num=0x130000005))] |
| 469 | |
| 470 | self.send_and_assert_no_replies(self.tra_if, pkts) |
| 471 | |
| 472 | # a packet less than 2^30 from the current position and is a |
| 473 | # wrap; (the seq is currently at 0x180000005). |
| 474 | # - AR: out of window so considered a wrap, so accepted |
| 475 | # - non-AR: not considered a wrap, so won't decrypt |
| 476 | p.scapy_tra_sa.seq_num = 0x260000005 |
| 477 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 478 | dst=self.tra_if.local_mac) / |
| 479 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 480 | dst=self.tra_if.local_ip4) / |
| 481 | ICMP(), |
| 482 | seq_num=0x260000005))] |
| 483 | if ar_on: |
| 484 | self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 485 | else: |
| 486 | self.send_and_assert_no_replies(self.tra_if, pkts) |
| 487 | |
| 488 | # |
| 489 | # window positions are different now for AR/non-AR |
| 490 | # move non-AR forward |
| 491 | # |
| 492 | if not ar_on: |
| 493 | # a packet more than 2^30 from the current position and is a |
| 494 | # wrap; (the seq is currently at 0x180000005). |
| 495 | # - AR: accepted |
| 496 | # - non-AR: not considered a wrap, so won't decrypt |
| 497 | |
| 498 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 499 | dst=self.tra_if.local_mac) / |
| 500 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 501 | dst=self.tra_if.local_ip4) / |
| 502 | ICMP(), |
| 503 | seq_num=0x200000005)), |
| 504 | (Ether(src=self.tra_if.remote_mac, |
| 505 | dst=self.tra_if.local_mac) / |
| 506 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 507 | dst=self.tra_if.local_ip4) / |
| 508 | ICMP(), |
| 509 | seq_num=0x200000006))] |
| 510 | self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 511 | |
| 512 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 513 | dst=self.tra_if.local_mac) / |
| 514 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 515 | dst=self.tra_if.local_ip4) / |
| 516 | ICMP(), |
| 517 | seq_num=0x260000005))] |
| 518 | self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 519 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 520 | def verify_tra_anti_replay(self): |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 521 | p = self.params[socket.AF_INET] |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 522 | esn_en = p.vpp_tra_sa.esn_en |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 523 | |
Ole Troan | e66443c | 2021-03-18 11:12:01 +0100 | [diff] [blame] | 524 | seq_cycle_node_name = \ |
| 525 | ('/err/%s/sequence number cycled (packet dropped)' % |
| 526 | self.tra4_encrypt_node_name) |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 527 | replay_count = self.get_replay_counts(p) |
| 528 | hash_failed_count = self.get_hash_failed_counts(p) |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 529 | seq_cycle_count = self.statistics.get_err_counter(seq_cycle_node_name) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 530 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 531 | if ESP == self.encryption_type: |
| 532 | undersize_node_name = ('/err/%s/undersized packet' % |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 533 | self.tra4_decrypt_node_name[0]) |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 534 | undersize_count = self.statistics.get_err_counter( |
| 535 | undersize_node_name) |
| 536 | |
| 537 | # |
| 538 | # send packets with seq numbers 1->34 |
| 539 | # this means the window size is still in Case B (see RFC4303 |
| 540 | # Appendix A) |
| 541 | # |
| 542 | # for reasons i haven't investigated Scapy won't create a packet with |
| 543 | # seq_num=0 |
| 544 | # |
| 545 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 546 | dst=self.tra_if.local_mac) / |
| 547 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 548 | dst=self.tra_if.local_ip4) / |
| 549 | ICMP(), |
| 550 | seq_num=seq)) |
| 551 | for seq in range(1, 34)] |
| 552 | recv_pkts = self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 553 | |
| 554 | # replayed packets are dropped |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 555 | self.send_and_assert_no_replies(self.tra_if, pkts, timeout=0.2) |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 556 | replay_count += len(pkts) |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 557 | self.assertEqual(self.get_replay_counts(p), replay_count) |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 558 | |
| 559 | # |
Neale Ranns | 3b9374f | 2019-08-01 04:45:15 -0700 | [diff] [blame] | 560 | # now send a batch of packets all with the same sequence number |
| 561 | # the first packet in the batch is legitimate, the rest bogus |
| 562 | # |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 563 | self.vapi.cli("clear error") |
| 564 | self.vapi.cli("clear node counters") |
Neale Ranns | 3b9374f | 2019-08-01 04:45:15 -0700 | [diff] [blame] | 565 | pkts = (Ether(src=self.tra_if.remote_mac, |
| 566 | dst=self.tra_if.local_mac) / |
| 567 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 568 | dst=self.tra_if.local_ip4) / |
| 569 | ICMP(), |
| 570 | seq_num=35)) |
| 571 | recv_pkts = self.send_and_expect(self.tra_if, pkts * 8, |
| 572 | self.tra_if, n_rx=1) |
| 573 | replay_count += 7 |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 574 | self.assertEqual(self.get_replay_counts(p), replay_count) |
Neale Ranns | 3b9374f | 2019-08-01 04:45:15 -0700 | [diff] [blame] | 575 | |
| 576 | # |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 577 | # now move the window over to 257 (more than one byte) and into Case A |
| 578 | # |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 579 | self.vapi.cli("clear error") |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 580 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 581 | dst=self.tra_if.local_mac) / |
| 582 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 583 | dst=self.tra_if.local_ip4) / |
| 584 | ICMP(), |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 585 | seq_num=257)) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 586 | recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 587 | |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 588 | # replayed packets are dropped |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 589 | self.send_and_assert_no_replies(self.tra_if, pkt * 3, timeout=0.2) |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 590 | replay_count += 3 |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 591 | self.assertEqual(self.get_replay_counts(p), replay_count) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 592 | |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 593 | # the window size is 64 packets |
| 594 | # in window are still accepted |
| 595 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 596 | dst=self.tra_if.local_mac) / |
| 597 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 598 | dst=self.tra_if.local_ip4) / |
| 599 | ICMP(), |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 600 | seq_num=200)) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 601 | recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 602 | |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 603 | # a packet that does not decrypt does not move the window forward |
| 604 | bogus_sa = SecurityAssociation(self.encryption_type, |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 605 | p.vpp_tra_spi, |
| 606 | crypt_algo=p.crypt_algo, |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 607 | crypt_key=mk_scapy_crypt_key(p)[::-1], |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 608 | auth_algo=p.auth_algo, |
| 609 | auth_key=p.auth_key[::-1]) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 610 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 611 | dst=self.tra_if.local_mac) / |
| 612 | bogus_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 613 | dst=self.tra_if.local_ip4) / |
| 614 | ICMP(), |
| 615 | seq_num=350)) |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 616 | self.send_and_assert_no_replies(self.tra_if, pkt * 17, timeout=0.2) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 617 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 618 | hash_failed_count += 17 |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 619 | self.assertEqual(self.get_hash_failed_counts(p), hash_failed_count) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 620 | |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 621 | # a malformed 'runt' packet |
| 622 | # created by a mis-constructed SA |
Neale Ranns | 2cdcd0c | 2019-08-27 12:26:14 +0000 | [diff] [blame] | 623 | if (ESP == self.encryption_type and p.crypt_algo != "NULL"): |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 624 | bogus_sa = SecurityAssociation(self.encryption_type, |
| 625 | p.vpp_tra_spi) |
| 626 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 627 | dst=self.tra_if.local_mac) / |
| 628 | bogus_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 629 | dst=self.tra_if.local_ip4) / |
| 630 | ICMP(), |
| 631 | seq_num=350)) |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 632 | self.send_and_assert_no_replies(self.tra_if, pkt * 17, timeout=0.2) |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 633 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 634 | undersize_count += 17 |
| 635 | self.assert_error_counter_equal(undersize_node_name, |
| 636 | undersize_count) |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 637 | |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 638 | # which we can determine since this packet is still in the window |
| 639 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 640 | dst=self.tra_if.local_mac) / |
| 641 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 642 | dst=self.tra_if.local_ip4) / |
| 643 | ICMP(), |
| 644 | seq_num=234)) |
Klement Sekera | 14d7e90 | 2018-12-10 13:46:09 +0100 | [diff] [blame] | 645 | self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 646 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 647 | # |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 648 | # out of window are dropped |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 649 | # this is Case B. So VPP will consider this to be a high seq num wrap |
| 650 | # and so the decrypt attempt will fail |
| 651 | # |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 652 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 653 | dst=self.tra_if.local_mac) / |
| 654 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 655 | dst=self.tra_if.local_ip4) / |
| 656 | ICMP(), |
| 657 | seq_num=17)) |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 658 | self.send_and_assert_no_replies(self.tra_if, pkt * 17, timeout=0.2) |
Neale Ranns | 00a4420 | 2019-03-21 16:36:28 +0000 | [diff] [blame] | 659 | |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 660 | if esn_en: |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 661 | # an out of window error with ESN looks like a high sequence |
| 662 | # wrap. but since it isn't then the verify will fail. |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 663 | hash_failed_count += 17 |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 664 | self.assertEqual(self.get_hash_failed_counts(p), hash_failed_count) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 665 | |
| 666 | else: |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 667 | replay_count += 17 |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 668 | self.assertEqual(self.get_replay_counts(p), replay_count) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 669 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 670 | # valid packet moves the window over to 258 |
Neale Ranns | 00a4420 | 2019-03-21 16:36:28 +0000 | [diff] [blame] | 671 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 672 | dst=self.tra_if.local_mac) / |
| 673 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 674 | dst=self.tra_if.local_ip4) / |
| 675 | ICMP(), |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 676 | seq_num=258)) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 677 | rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 678 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
| 679 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 680 | # |
| 681 | # move VPP's SA TX seq-num to just before the seq-number wrap. |
| 682 | # then fire in a packet that VPP should drop on TX because it |
| 683 | # causes the TX seq number to wrap; unless we're using extened sequence |
| 684 | # numbers. |
| 685 | # |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 686 | self.vapi.cli("test ipsec sa %d seq 0xffffffff" % p.scapy_tra_sa_id) |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 687 | self.logger.info(self.vapi.ppcli("show ipsec sa 0")) |
| 688 | self.logger.info(self.vapi.ppcli("show ipsec sa 1")) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 689 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 690 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 691 | dst=self.tra_if.local_mac) / |
| 692 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 693 | dst=self.tra_if.local_ip4) / |
| 694 | ICMP(), |
| 695 | seq_num=seq)) |
| 696 | for seq in range(259, 280)] |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 697 | |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 698 | if esn_en: |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 699 | rxs = self.send_and_expect(self.tra_if, pkts, self.tra_if) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 700 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 701 | # |
| 702 | # in order for scapy to decrypt its SA's high order number needs |
| 703 | # to wrap |
| 704 | # |
| 705 | p.vpp_tra_sa.seq_num = 0x100000000 |
| 706 | for rx in rxs: |
| 707 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
| 708 | |
| 709 | # |
| 710 | # wrap scapy's TX high sequence number. VPP is in case B, so it |
| 711 | # will consider this a high seq wrap also. |
| 712 | # The low seq num we set it to will place VPP's RX window in Case A |
| 713 | # |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 714 | p.scapy_tra_sa.seq_num = 0x100000005 |
| 715 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 716 | dst=self.tra_if.local_mac) / |
| 717 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 718 | dst=self.tra_if.local_ip4) / |
| 719 | ICMP(), |
| 720 | seq_num=0x100000005)) |
| 721 | rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 722 | |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 723 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 724 | |
| 725 | # |
| 726 | # A packet that has seq num between (2^32-64) and 5 is within |
| 727 | # the window |
| 728 | # |
| 729 | p.scapy_tra_sa.seq_num = 0xfffffffd |
| 730 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 731 | dst=self.tra_if.local_mac) / |
| 732 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 733 | dst=self.tra_if.local_ip4) / |
| 734 | ICMP(), |
| 735 | seq_num=0xfffffffd)) |
| 736 | rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 737 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
| 738 | |
| 739 | # |
| 740 | # While in case A we cannot wrap the high sequence number again |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 741 | # because VPP will consider this packet to be one that moves the |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 742 | # window forward |
| 743 | # |
| 744 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 745 | dst=self.tra_if.local_mac) / |
| 746 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 747 | dst=self.tra_if.local_ip4) / |
| 748 | ICMP(), |
| 749 | seq_num=0x200000999)) |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 750 | self.send_and_assert_no_replies(self.tra_if, [pkt], self.tra_if, |
| 751 | timeout=0.2) |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 752 | |
| 753 | hash_failed_count += 1 |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 754 | self.assertEqual(self.get_hash_failed_counts(p), hash_failed_count) |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 755 | |
| 756 | # |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 757 | # but if we move the window forward to case B, then we can wrap |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 758 | # again |
| 759 | # |
| 760 | p.scapy_tra_sa.seq_num = 0x100000555 |
| 761 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 762 | dst=self.tra_if.local_mac) / |
| 763 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 764 | dst=self.tra_if.local_ip4) / |
| 765 | ICMP(), |
| 766 | seq_num=0x100000555)) |
| 767 | rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 768 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
| 769 | |
| 770 | p.scapy_tra_sa.seq_num = 0x200000444 |
| 771 | pkt = (Ether(src=self.tra_if.remote_mac, |
| 772 | dst=self.tra_if.local_mac) / |
| 773 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 774 | dst=self.tra_if.local_ip4) / |
| 775 | ICMP(), |
| 776 | seq_num=0x200000444)) |
| 777 | rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if) |
| 778 | decrypted = p.vpp_tra_sa.decrypt(rx[0][IP]) |
| 779 | |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 780 | else: |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 781 | # |
| 782 | # without ESN TX sequence numbers can't wrap and packets are |
| 783 | # dropped from here on out. |
| 784 | # |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 785 | self.send_and_assert_no_replies(self.tra_if, pkts, timeout=0.2) |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 786 | seq_cycle_count += len(pkts) |
| 787 | self.assert_error_counter_equal(seq_cycle_node_name, |
| 788 | seq_cycle_count) |
Neale Ranns | 00a4420 | 2019-03-21 16:36:28 +0000 | [diff] [blame] | 789 | |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 790 | # move the security-associations seq number on to the last we used |
Neale Ranns | 00a4420 | 2019-03-21 16:36:28 +0000 | [diff] [blame] | 791 | self.vapi.cli("test ipsec sa %d seq 0x15f" % p.scapy_tra_sa_id) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 792 | p.scapy_tra_sa.seq_num = 351 |
| 793 | p.vpp_tra_sa.seq_num = 351 |
| 794 | |
Neale Ranns | e11203e | 2021-09-21 12:34:19 +0000 | [diff] [blame] | 795 | def verify_tra_lost(self): |
| 796 | p = self.params[socket.AF_INET] |
| 797 | esn_en = p.vpp_tra_sa.esn_en |
| 798 | |
| 799 | # |
| 800 | # send packets with seq numbers 1->34 |
| 801 | # this means the window size is still in Case B (see RFC4303 |
| 802 | # Appendix A) |
| 803 | # |
| 804 | # for reasons i haven't investigated Scapy won't create a packet with |
| 805 | # seq_num=0 |
| 806 | # |
| 807 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 808 | dst=self.tra_if.local_mac) / |
| 809 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 810 | dst=self.tra_if.local_ip4) / |
| 811 | ICMP(), |
| 812 | seq_num=seq)) |
| 813 | for seq in range(1, 3)] |
| 814 | self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 815 | |
| 816 | self.assertEqual(p.tra_sa_out.get_lost(), 0) |
| 817 | |
| 818 | # skip a sequence number |
| 819 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 820 | dst=self.tra_if.local_mac) / |
| 821 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 822 | dst=self.tra_if.local_ip4) / |
| 823 | ICMP(), |
| 824 | seq_num=seq)) |
| 825 | for seq in range(4, 6)] |
| 826 | self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 827 | |
| 828 | self.assertEqual(p.tra_sa_out.get_lost(), 0) |
| 829 | |
| 830 | # the lost packet are counted untill we get up past the first |
| 831 | # sizeof(replay_window) packets |
| 832 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 833 | dst=self.tra_if.local_mac) / |
| 834 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 835 | dst=self.tra_if.local_ip4) / |
| 836 | ICMP(), |
| 837 | seq_num=seq)) |
| 838 | for seq in range(6, 100)] |
| 839 | self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 840 | |
| 841 | self.assertEqual(p.tra_sa_out.get_lost(), 1) |
| 842 | |
| 843 | # lost of holes in the sequence |
| 844 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 845 | dst=self.tra_if.local_mac) / |
| 846 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 847 | dst=self.tra_if.local_ip4) / |
| 848 | ICMP(), |
| 849 | seq_num=seq)) |
| 850 | for seq in range(100, 200, 2)] |
| 851 | self.send_and_expect(self.tra_if, pkts, self.tra_if, n_rx=50) |
| 852 | |
| 853 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 854 | dst=self.tra_if.local_mac) / |
| 855 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 856 | dst=self.tra_if.local_ip4) / |
| 857 | ICMP(), |
| 858 | seq_num=seq)) |
| 859 | for seq in range(200, 300)] |
| 860 | self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 861 | |
| 862 | self.assertEqual(p.tra_sa_out.get_lost(), 51) |
| 863 | |
| 864 | # a big hole in the seq number space |
| 865 | pkts = [(Ether(src=self.tra_if.remote_mac, |
| 866 | dst=self.tra_if.local_mac) / |
| 867 | p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4, |
| 868 | dst=self.tra_if.local_ip4) / |
| 869 | ICMP(), |
| 870 | seq_num=seq)) |
| 871 | for seq in range(400, 500)] |
| 872 | self.send_and_expect(self.tra_if, pkts, self.tra_if) |
| 873 | |
| 874 | self.assertEqual(p.tra_sa_out.get_lost(), 151) |
| 875 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 876 | def verify_tra_basic4(self, count=1, payload_size=54): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 877 | """ ipsec v4 transport basic test """ |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 878 | self.vapi.cli("clear errors") |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 879 | self.vapi.cli("clear ipsec sa") |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 880 | try: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 881 | p = self.params[socket.AF_INET] |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 882 | send_pkts = self.gen_encrypt_pkts(p, p.scapy_tra_sa, self.tra_if, |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 883 | src=self.tra_if.remote_ip4, |
| 884 | dst=self.tra_if.local_ip4, |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 885 | count=count, |
| 886 | payload_size=payload_size) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 887 | recv_pkts = self.send_and_expect(self.tra_if, send_pkts, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 888 | self.tra_if) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 889 | for rx in recv_pkts: |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 890 | self.assertEqual(len(rx) - len(Ether()), rx[IP].len) |
| 891 | self.assert_packet_checksums_valid(rx) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 892 | try: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 893 | decrypted = p.vpp_tra_sa.decrypt(rx[IP]) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 894 | self.assert_packet_checksums_valid(decrypted) |
| 895 | except: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 896 | self.logger.debug(ppp("Unexpected packet:", rx)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 897 | raise |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 898 | finally: |
| 899 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 900 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 901 | |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 902 | pkts = p.tra_sa_in.get_stats()['packets'] |
| 903 | self.assertEqual(pkts, count, |
| 904 | "incorrect SA in counts: expected %d != %d" % |
| 905 | (count, pkts)) |
| 906 | pkts = p.tra_sa_out.get_stats()['packets'] |
| 907 | self.assertEqual(pkts, count, |
| 908 | "incorrect SA out counts: expected %d != %d" % |
| 909 | (count, pkts)) |
Neale Ranns | e11203e | 2021-09-21 12:34:19 +0000 | [diff] [blame] | 910 | self.assertEqual(p.tra_sa_out.get_lost(), 0) |
| 911 | self.assertEqual(p.tra_sa_in.get_lost(), 0) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 912 | |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 913 | self.assert_packet_counter_equal(self.tra4_encrypt_node_name, count) |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 914 | self.assert_packet_counter_equal(self.tra4_decrypt_node_name[0], count) |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 915 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 916 | |
| 917 | class IpsecTra4Tests(IpsecTra4): |
| 918 | """ UT test methods for Transport v4 """ |
| 919 | def test_tra_anti_replay(self): |
Paul Vinciguerra | 6e20e03 | 2019-12-27 00:19:23 -0500 | [diff] [blame] | 920 | """ ipsec v4 transport anti-replay test """ |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 921 | self.verify_tra_anti_replay() |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 922 | |
Neale Ranns | e11203e | 2021-09-21 12:34:19 +0000 | [diff] [blame] | 923 | def test_tra_lost(self): |
| 924 | """ ipsec v4 transport lost packet test """ |
| 925 | self.verify_tra_lost() |
| 926 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 927 | def test_tra_basic(self, count=1): |
| 928 | """ ipsec v4 transport basic test """ |
| 929 | self.verify_tra_basic4(count=1) |
| 930 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 931 | def test_tra_burst(self): |
| 932 | """ ipsec v4 transport burst test """ |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 933 | self.verify_tra_basic4(count=257) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 934 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 935 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 936 | class IpsecTra6(object): |
| 937 | """ verify methods for Transport v6 """ |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 938 | def verify_tra_basic6(self, count=1, payload_size=54): |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 939 | self.vapi.cli("clear errors") |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 940 | self.vapi.cli("clear ipsec sa") |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 941 | try: |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 942 | p = self.params[socket.AF_INET6] |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 943 | send_pkts = self.gen_encrypt_pkts6(p, p.scapy_tra_sa, self.tra_if, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 944 | src=self.tra_if.remote_ip6, |
| 945 | dst=self.tra_if.local_ip6, |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 946 | count=count, |
| 947 | payload_size=payload_size) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 948 | recv_pkts = self.send_and_expect(self.tra_if, send_pkts, |
| 949 | self.tra_if) |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 950 | for rx in recv_pkts: |
Neale Ranns | d207fd7 | 2019-04-18 17:18:12 -0700 | [diff] [blame] | 951 | self.assertEqual(len(rx) - len(Ether()) - len(IPv6()), |
| 952 | rx[IPv6].plen) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 953 | try: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 954 | decrypted = p.vpp_tra_sa.decrypt(rx[IPv6]) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 955 | self.assert_packet_checksums_valid(decrypted) |
| 956 | except: |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 957 | self.logger.debug(ppp("Unexpected packet:", rx)) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 958 | raise |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 959 | finally: |
| 960 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 961 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 962 | |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 963 | pkts = p.tra_sa_in.get_stats()['packets'] |
| 964 | self.assertEqual(pkts, count, |
| 965 | "incorrect SA in counts: expected %d != %d" % |
| 966 | (count, pkts)) |
| 967 | pkts = p.tra_sa_out.get_stats()['packets'] |
| 968 | self.assertEqual(pkts, count, |
| 969 | "incorrect SA out counts: expected %d != %d" % |
| 970 | (count, pkts)) |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 971 | self.assert_packet_counter_equal(self.tra6_encrypt_node_name, count) |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 972 | self.assert_packet_counter_equal(self.tra6_decrypt_node_name[0], count) |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 973 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 974 | def gen_encrypt_pkts_ext_hdrs6(self, sa, sw_intf, src, dst, count=1, |
| 975 | payload_size=54): |
| 976 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
| 977 | sa.encrypt(IPv6(src=src, dst=dst) / |
| 978 | ICMPv6EchoRequest(id=0, seq=1, |
| 979 | data='X' * payload_size)) |
| 980 | for i in range(count)] |
| 981 | |
| 982 | def gen_pkts_ext_hdrs6(self, sw_intf, src, dst, count=1, payload_size=54): |
| 983 | return [Ether(src=sw_intf.remote_mac, dst=sw_intf.local_mac) / |
| 984 | IPv6(src=src, dst=dst) / |
| 985 | IPv6ExtHdrHopByHop() / |
| 986 | IPv6ExtHdrFragment(id=2, offset=200) / |
| 987 | Raw(b'\xff' * 200) |
| 988 | for i in range(count)] |
| 989 | |
| 990 | def verify_tra_encrypted6(self, p, sa, rxs): |
| 991 | decrypted = [] |
| 992 | for rx in rxs: |
| 993 | self.assert_packet_checksums_valid(rx) |
| 994 | try: |
| 995 | decrypt_pkt = p.vpp_tra_sa.decrypt(rx[IPv6]) |
| 996 | decrypted.append(decrypt_pkt) |
| 997 | self.assert_equal(decrypt_pkt.src, self.tra_if.local_ip6) |
| 998 | self.assert_equal(decrypt_pkt.dst, self.tra_if.remote_ip6) |
| 999 | except: |
| 1000 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 1001 | try: |
| 1002 | self.logger.debug(ppp("Decrypted packet:", decrypt_pkt)) |
| 1003 | except: |
| 1004 | pass |
| 1005 | raise |
| 1006 | return decrypted |
| 1007 | |
| 1008 | def verify_tra_66_ext_hdrs(self, p): |
| 1009 | count = 63 |
| 1010 | |
| 1011 | # |
| 1012 | # check we can decrypt with options |
| 1013 | # |
| 1014 | tx = self.gen_encrypt_pkts_ext_hdrs6(p.scapy_tra_sa, self.tra_if, |
| 1015 | src=self.tra_if.remote_ip6, |
| 1016 | dst=self.tra_if.local_ip6, |
| 1017 | count=count) |
| 1018 | self.send_and_expect(self.tra_if, tx, self.tra_if) |
| 1019 | |
| 1020 | # |
| 1021 | # injecting a packet from ourselves to be routed of box is a hack |
| 1022 | # but it matches an outbout policy, alors je ne regrette rien |
| 1023 | # |
| 1024 | |
| 1025 | # one extension before ESP |
| 1026 | tx = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) / |
| 1027 | IPv6(src=self.tra_if.local_ip6, |
| 1028 | dst=self.tra_if.remote_ip6) / |
| 1029 | IPv6ExtHdrFragment(id=2, offset=200) / |
| 1030 | Raw(b'\xff' * 200)) |
| 1031 | |
| 1032 | rxs = self.send_and_expect(self.pg2, [tx], self.tra_if) |
| 1033 | dcs = self.verify_tra_encrypted6(p, p.vpp_tra_sa, rxs) |
| 1034 | |
| 1035 | for dc in dcs: |
| 1036 | # for reasons i'm not going to investigate scapy does not |
| 1037 | # created the correct headers after decrypt. but reparsing |
| 1038 | # the ipv6 packet fixes it |
| 1039 | dc = IPv6(raw(dc[IPv6])) |
| 1040 | self.assert_equal(dc[IPv6ExtHdrFragment].id, 2) |
| 1041 | |
| 1042 | # two extensions before ESP |
| 1043 | tx = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) / |
| 1044 | IPv6(src=self.tra_if.local_ip6, |
| 1045 | dst=self.tra_if.remote_ip6) / |
| 1046 | IPv6ExtHdrHopByHop() / |
| 1047 | IPv6ExtHdrFragment(id=2, offset=200) / |
| 1048 | Raw(b'\xff' * 200)) |
| 1049 | |
| 1050 | rxs = self.send_and_expect(self.pg2, [tx], self.tra_if) |
| 1051 | dcs = self.verify_tra_encrypted6(p, p.vpp_tra_sa, rxs) |
| 1052 | |
| 1053 | for dc in dcs: |
| 1054 | dc = IPv6(raw(dc[IPv6])) |
| 1055 | self.assertTrue(dc[IPv6ExtHdrHopByHop]) |
| 1056 | self.assert_equal(dc[IPv6ExtHdrFragment].id, 2) |
| 1057 | |
| 1058 | # two extensions before ESP, one after |
| 1059 | tx = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) / |
| 1060 | IPv6(src=self.tra_if.local_ip6, |
| 1061 | dst=self.tra_if.remote_ip6) / |
| 1062 | IPv6ExtHdrHopByHop() / |
| 1063 | IPv6ExtHdrFragment(id=2, offset=200) / |
| 1064 | IPv6ExtHdrDestOpt() / |
| 1065 | Raw(b'\xff' * 200)) |
| 1066 | |
| 1067 | rxs = self.send_and_expect(self.pg2, [tx], self.tra_if) |
| 1068 | dcs = self.verify_tra_encrypted6(p, p.vpp_tra_sa, rxs) |
| 1069 | |
| 1070 | for dc in dcs: |
| 1071 | dc = IPv6(raw(dc[IPv6])) |
| 1072 | self.assertTrue(dc[IPv6ExtHdrDestOpt]) |
| 1073 | self.assertTrue(dc[IPv6ExtHdrHopByHop]) |
| 1074 | self.assert_equal(dc[IPv6ExtHdrFragment].id, 2) |
| 1075 | |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 1076 | |
| 1077 | class IpsecTra6Tests(IpsecTra6): |
| 1078 | """ UT test methods for Transport v6 """ |
| 1079 | def test_tra_basic6(self): |
| 1080 | """ ipsec v6 transport basic test """ |
| 1081 | self.verify_tra_basic6(count=1) |
| 1082 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 1083 | def test_tra_burst6(self): |
| 1084 | """ ipsec v6 transport burst test """ |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 1085 | self.verify_tra_basic6(count=257) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 1086 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 1087 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1088 | class IpsecTra6ExtTests(IpsecTra6): |
| 1089 | def test_tra_ext_hdrs_66(self): |
| 1090 | """ ipsec 6o6 tra extension headers test """ |
| 1091 | self.verify_tra_66_ext_hdrs(self.params[socket.AF_INET6]) |
| 1092 | |
| 1093 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 1094 | class IpsecTra46Tests(IpsecTra4Tests, IpsecTra6Tests): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 1095 | """ UT test methods for Transport v6 and v4""" |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 1096 | pass |
| 1097 | |
| 1098 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1099 | class IpsecTun4(object): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 1100 | """ verify methods for Tunnel v4 """ |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1101 | def verify_counters4(self, p, count, n_frags=None, worker=None): |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 1102 | if not n_frags: |
| 1103 | n_frags = count |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 1104 | if (hasattr(p, "spd_policy_in_any")): |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1105 | pkts = p.spd_policy_in_any.get_stats(worker)['packets'] |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 1106 | self.assertEqual(pkts, count, |
| 1107 | "incorrect SPD any policy: expected %d != %d" % |
| 1108 | (count, pkts)) |
| 1109 | |
| 1110 | if (hasattr(p, "tun_sa_in")): |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1111 | pkts = p.tun_sa_in.get_stats(worker)['packets'] |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 1112 | self.assertEqual(pkts, count, |
| 1113 | "incorrect SA in counts: expected %d != %d" % |
| 1114 | (count, pkts)) |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1115 | pkts = p.tun_sa_out.get_stats(worker)['packets'] |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 1116 | self.assertEqual(pkts, n_frags, |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 1117 | "incorrect SA out counts: expected %d != %d" % |
| 1118 | (count, pkts)) |
| 1119 | |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 1120 | self.assert_packet_counter_equal(self.tun4_encrypt_node_name, n_frags) |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1121 | self.assert_packet_counter_equal(self.tun4_decrypt_node_name[0], count) |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 1122 | |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1123 | def verify_decrypted(self, p, rxs): |
| 1124 | for rx in rxs: |
| 1125 | self.assert_equal(rx[IP].src, p.remote_tun_if_host) |
| 1126 | self.assert_equal(rx[IP].dst, self.pg1.remote_ip4) |
| 1127 | self.assert_packet_checksums_valid(rx) |
| 1128 | |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 1129 | def verify_esp_padding(self, sa, esp_payload, decrypt_pkt): |
| 1130 | align = sa.crypt_algo.block_size |
| 1131 | if align < 4: |
| 1132 | align = 4 |
| 1133 | exp_len = (len(decrypt_pkt) + 2 + (align - 1)) & ~(align - 1) |
| 1134 | exp_len += sa.crypt_algo.iv_size |
| 1135 | exp_len += sa.crypt_algo.icv_size or sa.auth_algo.icv_size |
| 1136 | self.assertEqual(exp_len, len(esp_payload)) |
| 1137 | |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1138 | def verify_encrypted(self, p, sa, rxs): |
| 1139 | decrypt_pkts = [] |
| 1140 | for rx in rxs: |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 1141 | if p.nat_header: |
| 1142 | self.assertEqual(rx[UDP].dport, 4500) |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 1143 | self.assert_packet_checksums_valid(rx) |
| 1144 | self.assertEqual(len(rx) - len(Ether()), rx[IP].len) |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1145 | try: |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 1146 | rx_ip = rx[IP] |
| 1147 | decrypt_pkt = p.vpp_tun_sa.decrypt(rx_ip) |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1148 | if not decrypt_pkt.haslayer(IP): |
| 1149 | decrypt_pkt = IP(decrypt_pkt[Raw].load) |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 1150 | if rx_ip.proto == socket.IPPROTO_ESP: |
| 1151 | self.verify_esp_padding(sa, rx_ip[ESP].data, decrypt_pkt) |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1152 | decrypt_pkts.append(decrypt_pkt) |
| 1153 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4) |
| 1154 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host) |
| 1155 | except: |
| 1156 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 1157 | try: |
| 1158 | self.logger.debug(ppp("Decrypted packet:", decrypt_pkt)) |
| 1159 | except: |
| 1160 | pass |
| 1161 | raise |
| 1162 | pkts = reassemble4(decrypt_pkts) |
| 1163 | for pkt in pkts: |
| 1164 | self.assert_packet_checksums_valid(pkt) |
| 1165 | |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 1166 | def verify_tun_44(self, p, count=1, payload_size=64, n_rx=None): |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 1167 | self.vapi.cli("clear errors") |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1168 | self.vapi.cli("clear ipsec counters") |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 1169 | self.vapi.cli("clear ipsec sa") |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 1170 | if not n_rx: |
| 1171 | n_rx = count |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 1172 | try: |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 1173 | send_pkts = self.gen_encrypt_pkts(p, p.scapy_tun_sa, self.tun_if, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 1174 | src=p.remote_tun_if_host, |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 1175 | dst=self.pg1.remote_ip4, |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1176 | count=count, |
| 1177 | payload_size=payload_size) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 1178 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1179 | self.verify_decrypted(p, recv_pkts) |
| 1180 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 1181 | send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 1182 | dst=p.remote_tun_if_host, count=count, |
| 1183 | payload_size=payload_size) |
| 1184 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, |
| 1185 | self.tun_if, n_rx) |
Neale Ranns | f05e732 | 2019-03-29 20:23:58 +0000 | [diff] [blame] | 1186 | self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts) |
| 1187 | |
Neale Ranns | f3a6622 | 2020-01-02 05:04:00 +0000 | [diff] [blame] | 1188 | for rx in recv_pkts: |
| 1189 | self.assertEqual(rx[IP].src, p.tun_src) |
| 1190 | self.assertEqual(rx[IP].dst, p.tun_dst) |
| 1191 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 1192 | finally: |
| 1193 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 1194 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 1195 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1196 | self.logger.info(self.vapi.ppcli("show ipsec sa 0")) |
| 1197 | self.logger.info(self.vapi.ppcli("show ipsec sa 4")) |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 1198 | self.verify_counters4(p, count, n_rx) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 1199 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 1200 | def verify_tun_dropped_44(self, p, count=1, payload_size=64, n_rx=None): |
| 1201 | self.vapi.cli("clear errors") |
| 1202 | if not n_rx: |
| 1203 | n_rx = count |
| 1204 | try: |
| 1205 | send_pkts = self.gen_encrypt_pkts(p, p.scapy_tun_sa, self.tun_if, |
| 1206 | src=p.remote_tun_if_host, |
| 1207 | dst=self.pg1.remote_ip4, |
| 1208 | count=count) |
| 1209 | self.send_and_assert_no_replies(self.tun_if, send_pkts) |
| 1210 | |
| 1211 | send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, |
| 1212 | dst=p.remote_tun_if_host, count=count, |
| 1213 | payload_size=payload_size) |
| 1214 | self.send_and_assert_no_replies(self.pg1, send_pkts) |
| 1215 | |
| 1216 | finally: |
| 1217 | self.logger.info(self.vapi.ppcli("show error")) |
| 1218 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
| 1219 | |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 1220 | def verify_tun_reass_44(self, p): |
| 1221 | self.vapi.cli("clear errors") |
| 1222 | self.vapi.ip_reassembly_enable_disable( |
| 1223 | sw_if_index=self.tun_if.sw_if_index, enable_ip4=True) |
| 1224 | |
| 1225 | try: |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 1226 | send_pkts = self.gen_encrypt_pkts(p, p.scapy_tun_sa, self.tun_if, |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 1227 | src=p.remote_tun_if_host, |
| 1228 | dst=self.pg1.remote_ip4, |
| 1229 | payload_size=1900, |
| 1230 | count=1) |
| 1231 | send_pkts = fragment_rfc791(send_pkts[0], 1400) |
| 1232 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, |
| 1233 | self.pg1, n_rx=1) |
| 1234 | self.verify_decrypted(p, recv_pkts) |
| 1235 | |
| 1236 | send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, |
| 1237 | dst=p.remote_tun_if_host, count=1) |
| 1238 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, |
| 1239 | self.tun_if) |
| 1240 | self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts) |
| 1241 | |
| 1242 | finally: |
| 1243 | self.logger.info(self.vapi.ppcli("show error")) |
| 1244 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
| 1245 | |
| 1246 | self.verify_counters4(p, 1, 1) |
| 1247 | self.vapi.ip_reassembly_enable_disable( |
| 1248 | sw_if_index=self.tun_if.sw_if_index, enable_ip4=False) |
| 1249 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 1250 | def verify_tun_64(self, p, count=1): |
| 1251 | self.vapi.cli("clear errors") |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 1252 | self.vapi.cli("clear ipsec sa") |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 1253 | try: |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 1254 | send_pkts = self.gen_encrypt_pkts6(p, p.scapy_tun_sa, self.tun_if, |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 1255 | src=p.remote_tun_if_host6, |
| 1256 | dst=self.pg1.remote_ip6, |
| 1257 | count=count) |
| 1258 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
| 1259 | for recv_pkt in recv_pkts: |
| 1260 | self.assert_equal(recv_pkt[IPv6].src, p.remote_tun_if_host6) |
| 1261 | self.assert_equal(recv_pkt[IPv6].dst, self.pg1.remote_ip6) |
| 1262 | self.assert_packet_checksums_valid(recv_pkt) |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 1263 | send_pkts = self.gen_pkts6(p, self.pg1, src=self.pg1.remote_ip6, |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 1264 | dst=p.remote_tun_if_host6, count=count) |
| 1265 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if) |
| 1266 | for recv_pkt in recv_pkts: |
| 1267 | try: |
| 1268 | decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IP]) |
| 1269 | if not decrypt_pkt.haslayer(IPv6): |
| 1270 | decrypt_pkt = IPv6(decrypt_pkt[Raw].load) |
| 1271 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6) |
| 1272 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host6) |
| 1273 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 1274 | except: |
| 1275 | self.logger.error(ppp("Unexpected packet:", recv_pkt)) |
| 1276 | try: |
| 1277 | self.logger.debug( |
| 1278 | ppp("Decrypted packet:", decrypt_pkt)) |
| 1279 | except: |
| 1280 | pass |
| 1281 | raise |
| 1282 | finally: |
| 1283 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 1284 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 1285 | |
Klement Sekera | 6aa58b7 | 2019-05-16 14:34:55 +0200 | [diff] [blame] | 1286 | self.verify_counters4(p, count) |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 1287 | |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 1288 | def verify_keepalive(self, p): |
| 1289 | pkt = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) / |
| 1290 | IP(src=p.remote_tun_if_host, dst=self.tun_if.local_ip4) / |
| 1291 | UDP(sport=333, dport=4500) / |
Ole Troan | 8b76c23 | 2019-10-21 20:55:13 +0200 | [diff] [blame] | 1292 | Raw(b'\xff')) |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 1293 | self.send_and_assert_no_replies(self.tun_if, pkt*31) |
| 1294 | self.assert_error_counter_equal( |
| 1295 | '/err/%s/NAT Keepalive' % self.tun4_input_node, 31) |
| 1296 | |
| 1297 | pkt = (Ether(src=self.tun_if.remote_mac, dst=self.tun_if.local_mac) / |
| 1298 | IP(src=p.remote_tun_if_host, dst=self.tun_if.local_ip4) / |
| 1299 | UDP(sport=333, dport=4500) / |
Ole Troan | 8b76c23 | 2019-10-21 20:55:13 +0200 | [diff] [blame] | 1300 | Raw(b'\xfe')) |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 1301 | self.send_and_assert_no_replies(self.tun_if, pkt*31) |
| 1302 | self.assert_error_counter_equal( |
| 1303 | '/err/%s/Too Short' % self.tun4_input_node, 31) |
| 1304 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1305 | |
| 1306 | class IpsecTun4Tests(IpsecTun4): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 1307 | """ UT test methods for Tunnel v4 """ |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1308 | def test_tun_basic44(self): |
| 1309 | """ ipsec 4o4 tunnel basic test """ |
| 1310 | self.verify_tun_44(self.params[socket.AF_INET], count=1) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1311 | self.tun_if.admin_down() |
| 1312 | self.tun_if.resolve_arp() |
| 1313 | self.tun_if.admin_up() |
| 1314 | self.verify_tun_44(self.params[socket.AF_INET], count=1) |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1315 | |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 1316 | def test_tun_reass_basic44(self): |
| 1317 | """ ipsec 4o4 tunnel basic reassembly test """ |
| 1318 | self.verify_tun_reass_44(self.params[socket.AF_INET]) |
| 1319 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 1320 | def test_tun_burst44(self): |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 1321 | """ ipsec 4o4 tunnel burst test """ |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1322 | self.verify_tun_44(self.params[socket.AF_INET], count=127) |
| 1323 | |
| 1324 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1325 | class IpsecTun6(object): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 1326 | """ verify methods for Tunnel v6 """ |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1327 | def verify_counters6(self, p_in, p_out, count, worker=None): |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1328 | if (hasattr(p_in, "tun_sa_in")): |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1329 | pkts = p_in.tun_sa_in.get_stats(worker)['packets'] |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 1330 | self.assertEqual(pkts, count, |
| 1331 | "incorrect SA in counts: expected %d != %d" % |
| 1332 | (count, pkts)) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1333 | if (hasattr(p_out, "tun_sa_out")): |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1334 | pkts = p_out.tun_sa_out.get_stats(worker)['packets'] |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 1335 | self.assertEqual(pkts, count, |
| 1336 | "incorrect SA out counts: expected %d != %d" % |
| 1337 | (count, pkts)) |
| 1338 | self.assert_packet_counter_equal(self.tun6_encrypt_node_name, count) |
Neale Ranns | 8c609af | 2021-02-25 10:05:32 +0000 | [diff] [blame] | 1339 | self.assert_packet_counter_equal(self.tun6_decrypt_node_name[0], count) |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 1340 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1341 | def verify_decrypted6(self, p, rxs): |
| 1342 | for rx in rxs: |
| 1343 | self.assert_equal(rx[IPv6].src, p.remote_tun_if_host) |
| 1344 | self.assert_equal(rx[IPv6].dst, self.pg1.remote_ip6) |
| 1345 | self.assert_packet_checksums_valid(rx) |
| 1346 | |
| 1347 | def verify_encrypted6(self, p, sa, rxs): |
| 1348 | for rx in rxs: |
| 1349 | self.assert_packet_checksums_valid(rx) |
| 1350 | self.assertEqual(len(rx) - len(Ether()) - len(IPv6()), |
| 1351 | rx[IPv6].plen) |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 1352 | self.assert_equal(rx[IPv6].hlim, p.outer_hop_limit) |
| 1353 | if p.outer_flow_label: |
| 1354 | self.assert_equal(rx[IPv6].fl, p.outer_flow_label) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1355 | try: |
| 1356 | decrypt_pkt = p.vpp_tun_sa.decrypt(rx[IPv6]) |
| 1357 | if not decrypt_pkt.haslayer(IPv6): |
| 1358 | decrypt_pkt = IPv6(decrypt_pkt[Raw].load) |
| 1359 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 1360 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip6) |
| 1361 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host) |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 1362 | self.assert_equal(decrypt_pkt.hlim, p.inner_hop_limit - 1) |
| 1363 | self.assert_equal(decrypt_pkt.fl, p.inner_flow_label) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1364 | except: |
| 1365 | self.logger.debug(ppp("Unexpected packet:", rx)) |
| 1366 | try: |
| 1367 | self.logger.debug(ppp("Decrypted packet:", decrypt_pkt)) |
| 1368 | except: |
| 1369 | pass |
| 1370 | raise |
| 1371 | |
| 1372 | def verify_drop_tun_66(self, p_in, count=1, payload_size=64): |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 1373 | self.vapi.cli("clear errors") |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1374 | self.vapi.cli("clear ipsec sa") |
| 1375 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 1376 | send_pkts = self.gen_encrypt_pkts6(p_in, p_in.scapy_tun_sa, |
| 1377 | self.tun_if, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1378 | src=p_in.remote_tun_if_host, |
| 1379 | dst=self.pg1.remote_ip6, |
| 1380 | count=count) |
| 1381 | self.send_and_assert_no_replies(self.tun_if, send_pkts) |
| 1382 | self.logger.info(self.vapi.cli("sh punt stats")) |
| 1383 | |
| 1384 | def verify_tun_66(self, p_in, p_out=None, count=1, payload_size=64): |
| 1385 | self.vapi.cli("clear errors") |
| 1386 | self.vapi.cli("clear ipsec sa") |
| 1387 | if not p_out: |
| 1388 | p_out = p_in |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 1389 | try: |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 1390 | send_pkts = self.gen_encrypt_pkts6(p_in, p_in.scapy_tun_sa, |
| 1391 | self.tun_if, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1392 | src=p_in.remote_tun_if_host, |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 1393 | dst=self.pg1.remote_ip6, |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1394 | count=count, |
| 1395 | payload_size=payload_size) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 1396 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1397 | self.verify_decrypted6(p_in, recv_pkts) |
| 1398 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 1399 | send_pkts = self.gen_pkts6(p_in, self.pg1, src=self.pg1.remote_ip6, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1400 | dst=p_out.remote_tun_if_host, |
| 1401 | count=count, |
| 1402 | payload_size=payload_size) |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1403 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1404 | self.verify_encrypted6(p_out, p_out.vpp_tun_sa, recv_pkts) |
| 1405 | |
Neale Ranns | f3a6622 | 2020-01-02 05:04:00 +0000 | [diff] [blame] | 1406 | for rx in recv_pkts: |
| 1407 | self.assertEqual(rx[IPv6].src, p_out.tun_src) |
| 1408 | self.assertEqual(rx[IPv6].dst, p_out.tun_dst) |
| 1409 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 1410 | finally: |
| 1411 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 1412 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1413 | self.verify_counters6(p_in, p_out, count) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 1414 | |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 1415 | def verify_tun_reass_66(self, p): |
| 1416 | self.vapi.cli("clear errors") |
| 1417 | self.vapi.ip_reassembly_enable_disable( |
| 1418 | sw_if_index=self.tun_if.sw_if_index, enable_ip6=True) |
| 1419 | |
| 1420 | try: |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 1421 | send_pkts = self.gen_encrypt_pkts6(p, p.scapy_tun_sa, self.tun_if, |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 1422 | src=p.remote_tun_if_host, |
| 1423 | dst=self.pg1.remote_ip6, |
| 1424 | count=1, |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1425 | payload_size=1850) |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 1426 | send_pkts = fragment_rfc8200(send_pkts[0], 1, 1400, self.logger) |
| 1427 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, |
| 1428 | self.pg1, n_rx=1) |
| 1429 | self.verify_decrypted6(p, recv_pkts) |
| 1430 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 1431 | send_pkts = self.gen_pkts6(p, self.pg1, src=self.pg1.remote_ip6, |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 1432 | dst=p.remote_tun_if_host, |
| 1433 | count=1, |
| 1434 | payload_size=64) |
| 1435 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, |
| 1436 | self.tun_if) |
| 1437 | self.verify_encrypted6(p, p.vpp_tun_sa, recv_pkts) |
| 1438 | finally: |
| 1439 | self.logger.info(self.vapi.ppcli("show error")) |
| 1440 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
| 1441 | self.verify_counters6(p, p, 1) |
| 1442 | self.vapi.ip_reassembly_enable_disable( |
| 1443 | sw_if_index=self.tun_if.sw_if_index, enable_ip6=False) |
| 1444 | |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 1445 | def verify_tun_46(self, p, count=1): |
| 1446 | """ ipsec 4o6 tunnel basic test """ |
| 1447 | self.vapi.cli("clear errors") |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 1448 | self.vapi.cli("clear ipsec sa") |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 1449 | try: |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 1450 | send_pkts = self.gen_encrypt_pkts(p, p.scapy_tun_sa, self.tun_if, |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 1451 | src=p.remote_tun_if_host4, |
| 1452 | dst=self.pg1.remote_ip4, |
| 1453 | count=count) |
| 1454 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, self.pg1) |
| 1455 | for recv_pkt in recv_pkts: |
| 1456 | self.assert_equal(recv_pkt[IP].src, p.remote_tun_if_host4) |
| 1457 | self.assert_equal(recv_pkt[IP].dst, self.pg1.remote_ip4) |
| 1458 | self.assert_packet_checksums_valid(recv_pkt) |
| 1459 | send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, |
| 1460 | dst=p.remote_tun_if_host4, |
| 1461 | count=count) |
| 1462 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, self.tun_if) |
| 1463 | for recv_pkt in recv_pkts: |
| 1464 | try: |
| 1465 | decrypt_pkt = p.vpp_tun_sa.decrypt(recv_pkt[IPv6]) |
| 1466 | if not decrypt_pkt.haslayer(IP): |
| 1467 | decrypt_pkt = IP(decrypt_pkt[Raw].load) |
| 1468 | self.assert_equal(decrypt_pkt.src, self.pg1.remote_ip4) |
| 1469 | self.assert_equal(decrypt_pkt.dst, p.remote_tun_if_host4) |
| 1470 | self.assert_packet_checksums_valid(decrypt_pkt) |
| 1471 | except: |
| 1472 | self.logger.debug(ppp("Unexpected packet:", recv_pkt)) |
| 1473 | try: |
| 1474 | self.logger.debug(ppp("Decrypted packet:", |
| 1475 | decrypt_pkt)) |
| 1476 | except: |
| 1477 | pass |
| 1478 | raise |
| 1479 | finally: |
| 1480 | self.logger.info(self.vapi.ppcli("show error")) |
Paul Vinciguerra | 9673e3e | 2019-05-10 20:41:08 -0400 | [diff] [blame] | 1481 | self.logger.info(self.vapi.ppcli("show ipsec all")) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1482 | self.verify_counters6(p, p, count) |
Klement Sekera | 10d066e | 2018-11-13 11:12:57 +0100 | [diff] [blame] | 1483 | |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1484 | |
| 1485 | class IpsecTun6Tests(IpsecTun6): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 1486 | """ UT test methods for Tunnel v6 """ |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1487 | |
| 1488 | def test_tun_basic66(self): |
| 1489 | """ ipsec 6o6 tunnel basic test """ |
| 1490 | self.verify_tun_66(self.params[socket.AF_INET6], count=1) |
| 1491 | |
Neale Ranns | 1404698 | 2019-07-29 14:49:52 +0000 | [diff] [blame] | 1492 | def test_tun_reass_basic66(self): |
| 1493 | """ ipsec 6o6 tunnel basic reassembly test """ |
| 1494 | self.verify_tun_reass_66(self.params[socket.AF_INET6]) |
| 1495 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 1496 | def test_tun_burst66(self): |
| 1497 | """ ipsec 6o6 tunnel burst test """ |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 1498 | self.verify_tun_66(self.params[socket.AF_INET6], count=257) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 1499 | |
| 1500 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1501 | class IpsecTun6HandoffTests(IpsecTun6): |
| 1502 | """ UT test methods for Tunnel v6 with multiple workers """ |
Klement Sekera | 8d81502 | 2021-03-15 16:58:10 +0100 | [diff] [blame] | 1503 | vpp_worker_count = 2 |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1504 | |
| 1505 | def test_tun_handoff_66(self): |
| 1506 | """ ipsec 6o6 tunnel worker hand-off test """ |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 1507 | self.vapi.cli("clear errors") |
| 1508 | self.vapi.cli("clear ipsec sa") |
| 1509 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1510 | N_PKTS = 15 |
| 1511 | p = self.params[socket.AF_INET6] |
| 1512 | |
| 1513 | # inject alternately on worker 0 and 1. all counts on the SA |
| 1514 | # should be against worker 0 |
| 1515 | for worker in [0, 1, 0, 1]: |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 1516 | send_pkts = self.gen_encrypt_pkts6(p, p.scapy_tun_sa, self.tun_if, |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1517 | src=p.remote_tun_if_host, |
| 1518 | dst=self.pg1.remote_ip6, |
| 1519 | count=N_PKTS) |
| 1520 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, |
| 1521 | self.pg1, worker=worker) |
| 1522 | self.verify_decrypted6(p, recv_pkts) |
| 1523 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 1524 | send_pkts = self.gen_pkts6(p, self.pg1, src=self.pg1.remote_ip6, |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1525 | dst=p.remote_tun_if_host, |
| 1526 | count=N_PKTS) |
| 1527 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, |
| 1528 | self.tun_if, worker=worker) |
| 1529 | self.verify_encrypted6(p, p.vpp_tun_sa, recv_pkts) |
| 1530 | |
| 1531 | # all counts against the first worker that was used |
| 1532 | self.verify_counters6(p, p, 4*N_PKTS, worker=0) |
| 1533 | |
| 1534 | |
| 1535 | class IpsecTun4HandoffTests(IpsecTun4): |
| 1536 | """ UT test methods for Tunnel v4 with multiple workers """ |
Klement Sekera | 8d81502 | 2021-03-15 16:58:10 +0100 | [diff] [blame] | 1537 | vpp_worker_count = 2 |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1538 | |
| 1539 | def test_tun_handooff_44(self): |
| 1540 | """ ipsec 4o4 tunnel worker hand-off test """ |
Brian Russell | 7a29a2d | 2021-02-22 18:42:24 +0000 | [diff] [blame] | 1541 | self.vapi.cli("clear errors") |
| 1542 | self.vapi.cli("clear ipsec sa") |
| 1543 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1544 | N_PKTS = 15 |
| 1545 | p = self.params[socket.AF_INET] |
| 1546 | |
| 1547 | # inject alternately on worker 0 and 1. all counts on the SA |
| 1548 | # should be against worker 0 |
| 1549 | for worker in [0, 1, 0, 1]: |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 1550 | send_pkts = self.gen_encrypt_pkts(p, p.scapy_tun_sa, self.tun_if, |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1551 | src=p.remote_tun_if_host, |
| 1552 | dst=self.pg1.remote_ip4, |
| 1553 | count=N_PKTS) |
| 1554 | recv_pkts = self.send_and_expect(self.tun_if, send_pkts, |
| 1555 | self.pg1, worker=worker) |
| 1556 | self.verify_decrypted(p, recv_pkts) |
| 1557 | |
| 1558 | send_pkts = self.gen_pkts(self.pg1, src=self.pg1.remote_ip4, |
| 1559 | dst=p.remote_tun_if_host, |
| 1560 | count=N_PKTS) |
| 1561 | recv_pkts = self.send_and_expect(self.pg1, send_pkts, |
| 1562 | self.tun_if, worker=worker) |
| 1563 | self.verify_encrypted(p, p.vpp_tun_sa, recv_pkts) |
| 1564 | |
| 1565 | # all counts against the first worker that was used |
| 1566 | self.verify_counters4(p, 4*N_PKTS, worker=0) |
| 1567 | |
| 1568 | |
Neale Ranns | 53f526b | 2019-02-25 14:32:02 +0000 | [diff] [blame] | 1569 | class IpsecTun46Tests(IpsecTun4Tests, IpsecTun6Tests): |
Neale Ranns | 4f33c80 | 2019-04-10 12:39:10 +0000 | [diff] [blame] | 1570 | """ UT test methods for Tunnel v6 & v4 """ |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 1571 | pass |
| 1572 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 1573 | |
| 1574 | if __name__ == '__main__': |
| 1575 | unittest.main(testRunner=VppTestRunner) |