Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | """ Wg tests """ |
| 3 | |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 4 | import datetime |
| 5 | import base64 |
| 6 | |
| 7 | from hashlib import blake2s |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 8 | from scapy.packet import Packet |
| 9 | from scapy.packet import Raw |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 10 | from scapy.layers.l2 import Ether, ARP |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 11 | from scapy.layers.inet import IP, UDP |
| 12 | from scapy.contrib.wireguard import Wireguard, WireguardResponse, \ |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 13 | WireguardInitiation, WireguardTransport |
| 14 | from cryptography.hazmat.primitives.asymmetric.x25519 import \ |
| 15 | X25519PrivateKey, X25519PublicKey |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 16 | from cryptography.hazmat.primitives.serialization import Encoding, \ |
| 17 | PrivateFormat, PublicFormat, NoEncryption |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 18 | from cryptography.hazmat.primitives.hashes import BLAKE2s, Hash |
| 19 | from cryptography.hazmat.primitives.hmac import HMAC |
| 20 | from cryptography.hazmat.backends import default_backend |
| 21 | from noise.connection import NoiseConnection, Keypair |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 22 | |
| 23 | from vpp_ipip_tun_interface import VppIpIpTunInterface |
| 24 | from vpp_interface import VppInterface |
| 25 | from vpp_object import VppObject |
| 26 | from framework import VppTestCase |
| 27 | from re import compile |
| 28 | import unittest |
| 29 | |
| 30 | """ TestWg is a subclass of VPPTestCase classes. |
| 31 | |
| 32 | Wg test. |
| 33 | |
| 34 | """ |
| 35 | |
| 36 | |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 37 | def private_key_bytes(k): |
| 38 | return k.private_bytes(Encoding.Raw, |
| 39 | PrivateFormat.Raw, |
| 40 | NoEncryption()) |
| 41 | |
| 42 | |
| 43 | def public_key_bytes(k): |
| 44 | return k.public_bytes(Encoding.Raw, |
| 45 | PublicFormat.Raw) |
| 46 | |
| 47 | |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 48 | class VppWgInterface(VppInterface): |
| 49 | """ |
| 50 | VPP WireGuard interface |
| 51 | """ |
| 52 | |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 53 | def __init__(self, test, src, port): |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 54 | super(VppWgInterface, self).__init__(test) |
| 55 | |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 56 | self.port = port |
| 57 | self.src = src |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 58 | self.private_key = X25519PrivateKey.generate() |
| 59 | self.public_key = self.private_key.public_key() |
| 60 | |
| 61 | def public_key_bytes(self): |
| 62 | return public_key_bytes(self.public_key) |
| 63 | |
| 64 | def private_key_bytes(self): |
| 65 | return private_key_bytes(self.private_key) |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 66 | |
| 67 | def add_vpp_config(self): |
| 68 | r = self.test.vapi.wireguard_interface_create(interface={ |
| 69 | 'user_instance': 0xffffffff, |
| 70 | 'port': self.port, |
| 71 | 'src_ip': self.src, |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 72 | 'private_key': private_key_bytes(self.private_key), |
| 73 | 'generate_key': False |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 74 | }) |
| 75 | self.set_sw_if_index(r.sw_if_index) |
| 76 | self.test.registry.register(self, self.test.logger) |
| 77 | return self |
| 78 | |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 79 | def remove_vpp_config(self): |
| 80 | self.test.vapi.wireguard_interface_delete( |
| 81 | sw_if_index=self._sw_if_index) |
| 82 | |
| 83 | def query_vpp_config(self): |
| 84 | ts = self.test.vapi.wireguard_interface_dump(sw_if_index=0xffffffff) |
| 85 | for t in ts: |
| 86 | if t.interface.sw_if_index == self._sw_if_index and \ |
| 87 | str(t.interface.src_ip) == self.src and \ |
| 88 | t.interface.port == self.port and \ |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 89 | t.interface.private_key == private_key_bytes(self.private_key): |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 90 | return True |
| 91 | return False |
| 92 | |
| 93 | def __str__(self): |
| 94 | return self.object_id() |
| 95 | |
| 96 | def object_id(self): |
| 97 | return "wireguard-%d" % self._sw_if_index |
| 98 | |
| 99 | |
| 100 | def find_route(test, prefix, table_id=0): |
| 101 | routes = test.vapi.ip_route_dump(table_id, False) |
| 102 | |
| 103 | for e in routes: |
| 104 | if table_id == e.route.table_id \ |
| 105 | and str(e.route.prefix) == str(prefix): |
| 106 | return True |
| 107 | return False |
| 108 | |
| 109 | |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 110 | NOISE_HANDSHAKE_NAME = b"Noise_IKpsk2_25519_ChaChaPoly_BLAKE2s" |
| 111 | NOISE_IDENTIFIER_NAME = b"WireGuard v1 zx2c4 Jason@zx2c4.com" |
| 112 | |
| 113 | |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 114 | class VppWgPeer(VppObject): |
| 115 | |
| 116 | def __init__(self, |
| 117 | test, |
| 118 | itf, |
| 119 | endpoint, |
| 120 | port, |
| 121 | allowed_ips, |
| 122 | persistent_keepalive=15): |
| 123 | self._test = test |
| 124 | self.itf = itf |
| 125 | self.endpoint = endpoint |
| 126 | self.port = port |
| 127 | self.allowed_ips = allowed_ips |
| 128 | self.persistent_keepalive = persistent_keepalive |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 129 | |
| 130 | # remote peer's public |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 131 | self.private_key = X25519PrivateKey.generate() |
| 132 | self.public_key = self.private_key.public_key() |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 133 | |
| 134 | self.noise = NoiseConnection.from_name(NOISE_HANDSHAKE_NAME) |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 135 | |
| 136 | def validate_routing(self): |
| 137 | for a in self.allowed_ips: |
| 138 | self._test.assertTrue(find_route(self._test, a)) |
| 139 | |
| 140 | def validate_no_routing(self): |
| 141 | for a in self.allowed_ips: |
| 142 | self._test.assertFalse(find_route(self._test, a)) |
| 143 | |
| 144 | def add_vpp_config(self): |
| 145 | rv = self._test.vapi.wireguard_peer_add( |
| 146 | peer={ |
| 147 | 'public_key': self.public_key_bytes(), |
| 148 | 'port': self.port, |
| 149 | 'endpoint': self.endpoint, |
| 150 | 'n_allowed_ips': len(self.allowed_ips), |
| 151 | 'allowed_ips': self.allowed_ips, |
| 152 | 'sw_if_index': self.itf.sw_if_index, |
| 153 | 'persistent_keepalive': self.persistent_keepalive}) |
| 154 | self.index = rv.peer_index |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 155 | self.receiver_index = self.index + 1 |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 156 | self._test.registry.register(self, self._test.logger) |
| 157 | self.validate_routing() |
| 158 | return self |
| 159 | |
| 160 | def remove_vpp_config(self): |
| 161 | self._test.vapi.wireguard_peer_remove(peer_index=self.index) |
| 162 | self.validate_no_routing() |
| 163 | |
| 164 | def object_id(self): |
| 165 | return ("wireguard-peer-%s" % self.index) |
| 166 | |
| 167 | def public_key_bytes(self): |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 168 | return public_key_bytes(self.public_key) |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 169 | |
| 170 | def query_vpp_config(self): |
| 171 | peers = self._test.vapi.wireguard_peers_dump() |
| 172 | |
| 173 | for p in peers: |
| 174 | if p.peer.public_key == self.public_key_bytes() and \ |
| 175 | p.peer.port == self.port and \ |
| 176 | str(p.peer.endpoint) == self.endpoint and \ |
| 177 | p.peer.sw_if_index == self.itf.sw_if_index and \ |
| 178 | len(self.allowed_ips) == p.peer.n_allowed_ips: |
| 179 | self.allowed_ips.sort() |
| 180 | p.peer.allowed_ips.sort() |
| 181 | |
| 182 | for (a1, a2) in zip(self.allowed_ips, p.peer.allowed_ips): |
| 183 | if str(a1) != str(a2): |
| 184 | return False |
| 185 | return True |
| 186 | return False |
| 187 | |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 188 | def set_responder(self): |
| 189 | self.noise.set_as_responder() |
| 190 | |
| 191 | def mk_tunnel_header(self, tx_itf): |
| 192 | return (Ether(dst=tx_itf.local_mac, src=tx_itf.remote_mac) / |
| 193 | IP(src=self.endpoint, dst=self.itf.src) / |
| 194 | UDP(sport=self.port, dport=self.itf.port)) |
| 195 | |
| 196 | def noise_init(self, public_key=None): |
| 197 | self.noise.set_prologue(NOISE_IDENTIFIER_NAME) |
| 198 | self.noise.set_psks(psk=bytes(bytearray(32))) |
| 199 | |
| 200 | if not public_key: |
| 201 | public_key = self.itf.public_key |
| 202 | |
| 203 | # local/this private |
| 204 | self.noise.set_keypair_from_private_bytes( |
| 205 | Keypair.STATIC, |
| 206 | private_key_bytes(self.private_key)) |
| 207 | # remote's public |
| 208 | self.noise.set_keypair_from_public_bytes( |
| 209 | Keypair.REMOTE_STATIC, |
| 210 | public_key_bytes(public_key)) |
| 211 | |
| 212 | self.noise.start_handshake() |
| 213 | |
| 214 | def mk_handshake(self, tx_itf, public_key=None): |
| 215 | self.noise.set_as_initiator() |
| 216 | self.noise_init(public_key) |
| 217 | |
| 218 | p = (Wireguard() / WireguardInitiation()) |
| 219 | |
| 220 | p[Wireguard].message_type = 1 |
| 221 | p[Wireguard].reserved_zero = 0 |
| 222 | p[WireguardInitiation].sender_index = self.receiver_index |
| 223 | |
| 224 | # some random data for the message |
| 225 | # lifted from the noise protocol's wireguard example |
| 226 | now = datetime.datetime.now() |
| 227 | tai = struct.pack('!qi', 4611686018427387914 + int(now.timestamp()), |
| 228 | int(now.microsecond * 1e3)) |
| 229 | b = self.noise.write_message(payload=tai) |
| 230 | |
| 231 | # load noise into init message |
| 232 | p[WireguardInitiation].unencrypted_ephemeral = b[0:32] |
| 233 | p[WireguardInitiation].encrypted_static = b[32:80] |
| 234 | p[WireguardInitiation].encrypted_timestamp = b[80:108] |
| 235 | |
| 236 | # generate the mac1 hash |
| 237 | mac_key = blake2s(b'mac1----' + |
| 238 | self.itf.public_key_bytes()).digest() |
| 239 | p[WireguardInitiation].mac1 = blake2s(bytes(p)[0:116], |
| 240 | digest_size=16, |
| 241 | key=mac_key).digest() |
| 242 | p[WireguardInitiation].mac2 = bytearray(16) |
| 243 | |
| 244 | p = (self.mk_tunnel_header(tx_itf) / p) |
| 245 | |
| 246 | return p |
| 247 | |
| 248 | def verify_header(self, p): |
| 249 | self._test.assertEqual(p[IP].src, self.itf.src) |
| 250 | self._test.assertEqual(p[IP].dst, self.endpoint) |
| 251 | self._test.assertEqual(p[UDP].sport, self.itf.port) |
| 252 | self._test.assertEqual(p[UDP].dport, self.port) |
| 253 | self._test.assert_packet_checksums_valid(p) |
| 254 | |
| 255 | def consume_init(self, p, tx_itf): |
| 256 | self.noise.set_as_responder() |
| 257 | self.noise_init(self.itf.public_key) |
| 258 | self.verify_header(p) |
| 259 | |
| 260 | init = Wireguard(p[Raw]) |
| 261 | |
| 262 | self._test.assertEqual(init[Wireguard].message_type, 1) |
| 263 | self._test.assertEqual(init[Wireguard].reserved_zero, 0) |
| 264 | |
| 265 | self.sender = init[WireguardInitiation].sender_index |
| 266 | |
| 267 | # validate the hash |
| 268 | mac_key = blake2s(b'mac1----' + |
| 269 | public_key_bytes(self.public_key)).digest() |
| 270 | mac1 = blake2s(bytes(init)[0:-32], |
| 271 | digest_size=16, |
| 272 | key=mac_key).digest() |
| 273 | self._test.assertEqual(init[WireguardInitiation].mac1, mac1) |
| 274 | |
| 275 | # this passes only unencrypted_ephemeral, encrypted_static, |
| 276 | # encrypted_timestamp fields of the init |
| 277 | payload = self.noise.read_message(bytes(init)[8:-32]) |
| 278 | |
| 279 | # build the response |
| 280 | b = self.noise.write_message() |
| 281 | mac_key = blake2s(b'mac1----' + |
| 282 | public_key_bytes(self.itf.public_key)).digest() |
| 283 | resp = (Wireguard(message_type=2, reserved_zero=0) / |
| 284 | WireguardResponse(sender_index=self.receiver_index, |
| 285 | receiver_index=self.sender, |
| 286 | unencrypted_ephemeral=b[0:32], |
| 287 | encrypted_nothing=b[32:])) |
| 288 | mac1 = blake2s(bytes(resp)[:-32], |
| 289 | digest_size=16, |
| 290 | key=mac_key).digest() |
| 291 | resp[WireguardResponse].mac1 = mac1 |
| 292 | |
| 293 | resp = (self.mk_tunnel_header(tx_itf) / resp) |
| 294 | self._test.assertTrue(self.noise.handshake_finished) |
| 295 | |
| 296 | return resp |
| 297 | |
| 298 | def consume_response(self, p): |
| 299 | self.verify_header(p) |
| 300 | |
| 301 | resp = Wireguard(p[Raw]) |
| 302 | |
| 303 | self._test.assertEqual(resp[Wireguard].message_type, 2) |
| 304 | self._test.assertEqual(resp[Wireguard].reserved_zero, 0) |
| 305 | self._test.assertEqual(resp[WireguardResponse].receiver_index, |
| 306 | self.receiver_index) |
| 307 | |
| 308 | self.sender = resp[Wireguard].sender_index |
| 309 | |
| 310 | payload = self.noise.read_message(bytes(resp)[12:60]) |
| 311 | self._test.assertEqual(payload, b'') |
| 312 | self._test.assertTrue(self.noise.handshake_finished) |
| 313 | |
| 314 | def decrypt_transport(self, p): |
| 315 | self.verify_header(p) |
| 316 | |
| 317 | p = Wireguard(p[Raw]) |
| 318 | self._test.assertEqual(p[Wireguard].message_type, 4) |
| 319 | self._test.assertEqual(p[Wireguard].reserved_zero, 0) |
| 320 | self._test.assertEqual(p[WireguardTransport].receiver_index, |
| 321 | self.receiver_index) |
| 322 | |
| 323 | d = self.noise.decrypt( |
| 324 | p[WireguardTransport].encrypted_encapsulated_packet) |
| 325 | return d |
| 326 | |
| 327 | def encrypt_transport(self, p): |
| 328 | return self.noise.encrypt(bytes(p)) |
| 329 | |
Artem Glazychev | 8eb6940 | 2020-09-14 11:36:01 +0700 | [diff] [blame] | 330 | def validate_encapped(self, rxs, tx): |
| 331 | for rx in rxs: |
| 332 | rx = IP(self.decrypt_transport(rx)) |
| 333 | |
| 334 | # chech the oringial packet is present |
| 335 | self._test.assertEqual(rx[IP].dst, tx[IP].dst) |
| 336 | self._test.assertEqual(rx[IP].ttl, tx[IP].ttl-1) |
| 337 | |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 338 | |
| 339 | class TestWg(VppTestCase): |
| 340 | """ Wireguard Test Case """ |
| 341 | |
| 342 | error_str = compile(r"Error") |
| 343 | |
| 344 | @classmethod |
| 345 | def setUpClass(cls): |
| 346 | super(TestWg, cls).setUpClass() |
| 347 | try: |
| 348 | cls.create_pg_interfaces(range(3)) |
| 349 | for i in cls.pg_interfaces: |
| 350 | i.admin_up() |
| 351 | i.config_ip4() |
| 352 | i.resolve_arp() |
| 353 | |
| 354 | except Exception: |
| 355 | super(TestWg, cls).tearDownClass() |
| 356 | raise |
| 357 | |
| 358 | @classmethod |
| 359 | def tearDownClass(cls): |
| 360 | super(TestWg, cls).tearDownClass() |
| 361 | |
| 362 | def test_wg_interface(self): |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 363 | """ Simple interface creation """ |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 364 | port = 12312 |
| 365 | |
| 366 | # Create interface |
| 367 | wg0 = VppWgInterface(self, |
| 368 | self.pg1.local_ip4, |
| 369 | port).add_vpp_config() |
| 370 | |
| 371 | self.logger.info(self.vapi.cli("sh int")) |
| 372 | |
| 373 | # delete interface |
| 374 | wg0.remove_vpp_config() |
| 375 | |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 376 | def test_handshake_hash(self): |
| 377 | """ test hashing an init message """ |
| 378 | # a init packet generated by linux given the key below |
| 379 | h = "0100000098b9032b" \ |
| 380 | "55cc4b39e73c3d24" \ |
| 381 | "a2a1ab884b524a81" \ |
| 382 | "1808bb86640fb70d" \ |
| 383 | "e93154fec1879125" \ |
| 384 | "ab012624a27f0b75" \ |
| 385 | "c0a2582f438ddb5f" \ |
| 386 | "8e768af40b4ab444" \ |
| 387 | "02f9ff473e1b797e" \ |
| 388 | "80d39d93c5480c82" \ |
| 389 | "a3d4510f70396976" \ |
| 390 | "586fb67300a5167b" \ |
| 391 | "ae6ca3ff3dfd00eb" \ |
| 392 | "59be198810f5aa03" \ |
| 393 | "6abc243d2155ee4f" \ |
| 394 | "2336483900aef801" \ |
| 395 | "08752cd700000000" \ |
| 396 | "0000000000000000" \ |
| 397 | "00000000" |
| 398 | |
| 399 | b = bytearray.fromhex(h) |
| 400 | tgt = Wireguard(b) |
| 401 | |
| 402 | pubb = base64.b64decode("aRuHFTTxICIQNefp05oKWlJv3zgKxb8+WW7JJMh0jyM=") |
| 403 | pub = X25519PublicKey.from_public_bytes(pubb) |
| 404 | |
| 405 | self.assertEqual(pubb, public_key_bytes(pub)) |
| 406 | |
| 407 | # strip the macs and build a new packet |
| 408 | init = b[0:-32] |
| 409 | mac_key = blake2s(b'mac1----' + public_key_bytes(pub)).digest() |
| 410 | init += blake2s(init, |
| 411 | digest_size=16, |
| 412 | key=mac_key).digest() |
| 413 | init += b'\x00' * 16 |
| 414 | |
| 415 | act = Wireguard(init) |
| 416 | |
| 417 | self.assertEqual(tgt, act) |
| 418 | |
| 419 | def test_wg_peer_resp(self): |
| 420 | """ Send handshake response """ |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 421 | wg_output_node_name = '/err/wg-output-tun/' |
| 422 | wg_input_node_name = '/err/wg-input/' |
| 423 | |
| 424 | port = 12323 |
| 425 | |
| 426 | # Create interfaces |
| 427 | wg0 = VppWgInterface(self, |
| 428 | self.pg1.local_ip4, |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 429 | port).add_vpp_config() |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 430 | wg0.admin_up() |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 431 | wg0.config_ip4() |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 432 | |
| 433 | self.pg_enable_capture(self.pg_interfaces) |
| 434 | self.pg_start() |
| 435 | |
| 436 | peer_1 = VppWgPeer(self, |
| 437 | wg0, |
| 438 | self.pg1.remote_ip4, |
| 439 | port+1, |
| 440 | ["10.11.2.0/24", |
| 441 | "10.11.3.0/24"]).add_vpp_config() |
| 442 | self.assertEqual(len(self.vapi.wireguard_peers_dump()), 1) |
| 443 | |
| 444 | # wait for the peer to send a handshake |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 445 | rx = self.pg1.get_capture(1, timeout=2) |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 446 | |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 447 | # consume the handshake in the noise protocol and |
| 448 | # generate the response |
| 449 | resp = peer_1.consume_init(rx[0], self.pg1) |
| 450 | |
| 451 | # send the response, get keepalive |
| 452 | rxs = self.send_and_expect(self.pg1, [resp], self.pg1) |
| 453 | |
| 454 | for rx in rxs: |
| 455 | b = peer_1.decrypt_transport(rx) |
| 456 | self.assertEqual(0, len(b)) |
| 457 | |
| 458 | # send a packets that are routed into the tunnel |
| 459 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 460 | IP(src=self.pg0.remote_ip4, dst="10.11.3.2") / |
| 461 | UDP(sport=555, dport=556) / |
| 462 | Raw(b'\x00' * 80)) |
| 463 | |
| 464 | rxs = self.send_and_expect(self.pg0, p * 255, self.pg1) |
| 465 | |
Artem Glazychev | 8eb6940 | 2020-09-14 11:36:01 +0700 | [diff] [blame] | 466 | peer_1.validate_encapped(rxs, p) |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 467 | |
| 468 | # send packets into the tunnel, expect to receive them on |
| 469 | # the other side |
| 470 | p = [(peer_1.mk_tunnel_header(self.pg1) / |
| 471 | Wireguard(message_type=4, reserved_zero=0) / |
| 472 | WireguardTransport( |
| 473 | receiver_index=peer_1.sender, |
| 474 | counter=ii, |
| 475 | encrypted_encapsulated_packet=peer_1.encrypt_transport( |
| 476 | (IP(src="10.11.3.1", dst=self.pg0.remote_ip4, ttl=20) / |
| 477 | UDP(sport=222, dport=223) / |
| 478 | Raw())))) for ii in range(255)] |
| 479 | |
| 480 | rxs = self.send_and_expect(self.pg1, p, self.pg0) |
| 481 | |
| 482 | for rx in rxs: |
| 483 | self.assertEqual(rx[IP].dst, self.pg0.remote_ip4) |
| 484 | self.assertEqual(rx[IP].ttl, 19) |
| 485 | |
| 486 | def test_wg_peer_init(self): |
| 487 | """ Send handshake init """ |
| 488 | wg_output_node_name = '/err/wg-output-tun/' |
| 489 | wg_input_node_name = '/err/wg-input/' |
| 490 | |
Artem Glazychev | 124d5e0 | 2020-09-30 01:07:46 +0700 | [diff] [blame] | 491 | port = 12333 |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 492 | |
| 493 | # Create interfaces |
| 494 | wg0 = VppWgInterface(self, |
| 495 | self.pg1.local_ip4, |
| 496 | port).add_vpp_config() |
| 497 | wg0.admin_up() |
| 498 | wg0.config_ip4() |
| 499 | |
| 500 | peer_1 = VppWgPeer(self, |
| 501 | wg0, |
| 502 | self.pg1.remote_ip4, |
| 503 | port+1, |
| 504 | ["10.11.2.0/24", |
| 505 | "10.11.3.0/24"]).add_vpp_config() |
| 506 | self.assertEqual(len(self.vapi.wireguard_peers_dump()), 1) |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 507 | |
| 508 | # route a packet into the wg interface |
| 509 | # use the allowed-ip prefix |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 510 | # this is dropped because the peer is not initiated |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 511 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 512 | IP(src=self.pg0.remote_ip4, dst="10.11.3.2") / |
| 513 | UDP(sport=555, dport=556) / |
| 514 | Raw()) |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 515 | self.send_and_assert_no_replies(self.pg0, [p]) |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 516 | |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 517 | kp_error = wg_output_node_name + "Keypair error" |
| 518 | self.assertEqual(1, self.statistics.get_err_counter(kp_error)) |
| 519 | |
| 520 | # send a handsake from the peer with an invalid MAC |
| 521 | p = peer_1.mk_handshake(self.pg1) |
| 522 | p[WireguardInitiation].mac1 = b'foobar' |
| 523 | self.send_and_assert_no_replies(self.pg1, [p]) |
| 524 | self.assertEqual(1, self.statistics.get_err_counter( |
| 525 | wg_input_node_name + "Invalid MAC handshake")) |
| 526 | |
| 527 | # send a handsake from the peer but signed by the wrong key. |
| 528 | p = peer_1.mk_handshake(self.pg1, |
| 529 | X25519PrivateKey.generate().public_key()) |
| 530 | self.send_and_assert_no_replies(self.pg1, [p]) |
| 531 | self.assertEqual(1, self.statistics.get_err_counter( |
| 532 | wg_input_node_name + "Peer error")) |
| 533 | |
| 534 | # send a valid handsake init for which we expect a response |
| 535 | p = peer_1.mk_handshake(self.pg1) |
| 536 | |
| 537 | rx = self.send_and_expect(self.pg1, [p], self.pg1) |
| 538 | |
| 539 | peer_1.consume_response(rx[0]) |
| 540 | |
| 541 | # route a packet into the wg interface |
| 542 | # this is dropped because the peer is still not initiated |
| 543 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 544 | IP(src=self.pg0.remote_ip4, dst="10.11.3.2") / |
| 545 | UDP(sport=555, dport=556) / |
| 546 | Raw()) |
| 547 | self.send_and_assert_no_replies(self.pg0, [p]) |
| 548 | self.assertEqual(2, self.statistics.get_err_counter(kp_error)) |
| 549 | |
| 550 | # send a data packet from the peer through the tunnel |
| 551 | # this completes the handshake |
| 552 | p = (IP(src="10.11.3.1", dst=self.pg0.remote_ip4, ttl=20) / |
| 553 | UDP(sport=222, dport=223) / |
| 554 | Raw()) |
| 555 | d = peer_1.encrypt_transport(p) |
| 556 | p = (peer_1.mk_tunnel_header(self.pg1) / |
| 557 | (Wireguard(message_type=4, reserved_zero=0) / |
| 558 | WireguardTransport(receiver_index=peer_1.sender, |
| 559 | counter=0, |
| 560 | encrypted_encapsulated_packet=d))) |
| 561 | rxs = self.send_and_expect(self.pg1, [p], self.pg0) |
| 562 | |
| 563 | for rx in rxs: |
| 564 | self.assertEqual(rx[IP].dst, self.pg0.remote_ip4) |
| 565 | self.assertEqual(rx[IP].ttl, 19) |
| 566 | |
| 567 | # send a packets that are routed into the tunnel |
| 568 | p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 569 | IP(src=self.pg0.remote_ip4, dst="10.11.3.2") / |
| 570 | UDP(sport=555, dport=556) / |
| 571 | Raw(b'\x00' * 80)) |
| 572 | |
| 573 | rxs = self.send_and_expect(self.pg0, p * 255, self.pg1) |
| 574 | |
| 575 | for rx in rxs: |
| 576 | rx = IP(peer_1.decrypt_transport(rx)) |
| 577 | |
| 578 | # chech the oringial packet is present |
| 579 | self.assertEqual(rx[IP].dst, p[IP].dst) |
| 580 | self.assertEqual(rx[IP].ttl, p[IP].ttl-1) |
| 581 | |
| 582 | # send packets into the tunnel, expect to receive them on |
| 583 | # the other side |
| 584 | p = [(peer_1.mk_tunnel_header(self.pg1) / |
| 585 | Wireguard(message_type=4, reserved_zero=0) / |
| 586 | WireguardTransport( |
| 587 | receiver_index=peer_1.sender, |
| 588 | counter=ii+1, |
| 589 | encrypted_encapsulated_packet=peer_1.encrypt_transport( |
| 590 | (IP(src="10.11.3.1", dst=self.pg0.remote_ip4, ttl=20) / |
| 591 | UDP(sport=222, dport=223) / |
| 592 | Raw())))) for ii in range(255)] |
| 593 | |
| 594 | rxs = self.send_and_expect(self.pg1, p, self.pg0) |
| 595 | |
| 596 | for rx in rxs: |
| 597 | self.assertEqual(rx[IP].dst, self.pg0.remote_ip4) |
| 598 | self.assertEqual(rx[IP].ttl, 19) |
| 599 | |
| 600 | peer_1.remove_vpp_config() |
| 601 | wg0.remove_vpp_config() |
| 602 | |
| 603 | def test_wg_multi_peer(self): |
| 604 | """ multiple peer setup """ |
Artem Glazychev | 124d5e0 | 2020-09-30 01:07:46 +0700 | [diff] [blame] | 605 | port = 12343 |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 606 | |
| 607 | # Create interfaces |
| 608 | wg0 = VppWgInterface(self, |
| 609 | self.pg1.local_ip4, |
| 610 | port).add_vpp_config() |
| 611 | wg1 = VppWgInterface(self, |
| 612 | self.pg2.local_ip4, |
| 613 | port+1).add_vpp_config() |
| 614 | wg0.admin_up() |
| 615 | wg1.admin_up() |
| 616 | |
| 617 | # Check peer counter |
| 618 | self.assertEqual(len(self.vapi.wireguard_peers_dump()), 0) |
| 619 | |
| 620 | self.pg_enable_capture(self.pg_interfaces) |
| 621 | self.pg_start() |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 622 | |
| 623 | # Create many peers on sencond interface |
| 624 | NUM_PEERS = 16 |
| 625 | self.pg2.generate_remote_hosts(NUM_PEERS) |
| 626 | self.pg2.configure_ipv4_neighbors() |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 627 | self.pg1.generate_remote_hosts(NUM_PEERS) |
| 628 | self.pg1.configure_ipv4_neighbors() |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 629 | |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 630 | peers_1 = [] |
| 631 | peers_2 = [] |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 632 | for i in range(NUM_PEERS): |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 633 | peers_1.append(VppWgPeer(self, |
| 634 | wg0, |
| 635 | self.pg1.remote_hosts[i].ip4, |
| 636 | port+1+i, |
| 637 | ["10.0.%d.4/32" % i]).add_vpp_config()) |
| 638 | peers_2.append(VppWgPeer(self, |
| 639 | wg1, |
| 640 | self.pg2.remote_hosts[i].ip4, |
| 641 | port+100+i, |
| 642 | ["10.100.%d.4/32" % i]).add_vpp_config()) |
| 643 | |
| 644 | self.assertEqual(len(self.vapi.wireguard_peers_dump()), NUM_PEERS*2) |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 645 | |
| 646 | self.logger.info(self.vapi.cli("show wireguard peer")) |
| 647 | self.logger.info(self.vapi.cli("show wireguard interface")) |
| 648 | self.logger.info(self.vapi.cli("show adj 37")) |
| 649 | self.logger.info(self.vapi.cli("sh ip fib 172.16.3.17")) |
| 650 | self.logger.info(self.vapi.cli("sh ip fib 10.11.3.0")) |
| 651 | |
| 652 | # remove peers |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 653 | for p in peers_1: |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 654 | self.assertTrue(p.query_vpp_config()) |
| 655 | p.remove_vpp_config() |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 656 | for p in peers_2: |
| 657 | self.assertTrue(p.query_vpp_config()) |
| 658 | p.remove_vpp_config() |
Artem Glazychev | edca132 | 2020-08-31 17:12:30 +0700 | [diff] [blame] | 659 | |
| 660 | wg0.remove_vpp_config() |
Neale Ranns | d75a2d1 | 2020-09-10 08:49:10 +0000 | [diff] [blame] | 661 | wg1.remove_vpp_config() |
Artem Glazychev | 8eb6940 | 2020-09-14 11:36:01 +0700 | [diff] [blame] | 662 | |
| 663 | |
| 664 | class WireguardHandoffTests(TestWg): |
| 665 | """ Wireguard Tests in multi worker setup """ |
Klement Sekera | 8d81502 | 2021-03-15 16:58:10 +0100 | [diff] [blame] | 666 | vpp_worker_count = 2 |
Artem Glazychev | 8eb6940 | 2020-09-14 11:36:01 +0700 | [diff] [blame] | 667 | |
| 668 | def test_wg_peer_init(self): |
| 669 | """ Handoff """ |
| 670 | wg_output_node_name = '/err/wg-output-tun/' |
| 671 | wg_input_node_name = '/err/wg-input/' |
| 672 | |
Artem Glazychev | 124d5e0 | 2020-09-30 01:07:46 +0700 | [diff] [blame] | 673 | port = 12353 |
Artem Glazychev | 8eb6940 | 2020-09-14 11:36:01 +0700 | [diff] [blame] | 674 | |
| 675 | # Create interfaces |
| 676 | wg0 = VppWgInterface(self, |
| 677 | self.pg1.local_ip4, |
| 678 | port).add_vpp_config() |
| 679 | wg0.admin_up() |
| 680 | wg0.config_ip4() |
| 681 | |
| 682 | peer_1 = VppWgPeer(self, |
| 683 | wg0, |
| 684 | self.pg1.remote_ip4, |
| 685 | port+1, |
| 686 | ["10.11.2.0/24", |
| 687 | "10.11.3.0/24"]).add_vpp_config() |
| 688 | self.assertEqual(len(self.vapi.wireguard_peers_dump()), 1) |
| 689 | |
| 690 | # send a valid handsake init for which we expect a response |
| 691 | p = peer_1.mk_handshake(self.pg1) |
| 692 | |
| 693 | rx = self.send_and_expect(self.pg1, [p], self.pg1) |
| 694 | |
| 695 | peer_1.consume_response(rx[0]) |
| 696 | |
| 697 | # send a data packet from the peer through the tunnel |
| 698 | # this completes the handshake and pins the peer to worker 0 |
| 699 | p = (IP(src="10.11.3.1", dst=self.pg0.remote_ip4, ttl=20) / |
| 700 | UDP(sport=222, dport=223) / |
| 701 | Raw()) |
| 702 | d = peer_1.encrypt_transport(p) |
| 703 | p = (peer_1.mk_tunnel_header(self.pg1) / |
| 704 | (Wireguard(message_type=4, reserved_zero=0) / |
| 705 | WireguardTransport(receiver_index=peer_1.sender, |
| 706 | counter=0, |
| 707 | encrypted_encapsulated_packet=d))) |
| 708 | rxs = self.send_and_expect(self.pg1, [p], self.pg0, |
| 709 | worker=0) |
| 710 | |
| 711 | for rx in rxs: |
| 712 | self.assertEqual(rx[IP].dst, self.pg0.remote_ip4) |
| 713 | self.assertEqual(rx[IP].ttl, 19) |
| 714 | |
| 715 | # send a packets that are routed into the tunnel |
| 716 | # and pins the peer tp worker 1 |
| 717 | pe = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / |
| 718 | IP(src=self.pg0.remote_ip4, dst="10.11.3.2") / |
| 719 | UDP(sport=555, dport=556) / |
| 720 | Raw(b'\x00' * 80)) |
| 721 | rxs = self.send_and_expect(self.pg0, pe * 255, self.pg1, worker=1) |
| 722 | peer_1.validate_encapped(rxs, pe) |
| 723 | |
| 724 | # send packets into the tunnel, from the other worker |
| 725 | p = [(peer_1.mk_tunnel_header(self.pg1) / |
| 726 | Wireguard(message_type=4, reserved_zero=0) / |
| 727 | WireguardTransport( |
| 728 | receiver_index=peer_1.sender, |
| 729 | counter=ii+1, |
| 730 | encrypted_encapsulated_packet=peer_1.encrypt_transport( |
| 731 | (IP(src="10.11.3.1", dst=self.pg0.remote_ip4, ttl=20) / |
| 732 | UDP(sport=222, dport=223) / |
| 733 | Raw())))) for ii in range(255)] |
| 734 | |
| 735 | rxs = self.send_and_expect(self.pg1, p, self.pg0, worker=1) |
| 736 | |
| 737 | for rx in rxs: |
| 738 | self.assertEqual(rx[IP].dst, self.pg0.remote_ip4) |
| 739 | self.assertEqual(rx[IP].ttl, 19) |
| 740 | |
| 741 | # send a packets that are routed into the tunnel |
| 742 | # from owrker 0 |
| 743 | rxs = self.send_and_expect(self.pg0, pe * 255, self.pg1, worker=0) |
| 744 | |
| 745 | peer_1.validate_encapped(rxs, pe) |
| 746 | |
| 747 | peer_1.remove_vpp_config() |
| 748 | wg0.remove_vpp_config() |