1 # This test sets up a host-to-host IPsec VPN between Alice and Bob, each on its
2 # own network and with Eve as the only route between each other. We check that
3 # Eve can eavesdrop the plaintext traffic between Alice and Bob, but once they
4 # enable the secure tunnel Eve's spying becomes ineffective.
6 import ./make-test-python.nix ({ lib, pkgs, ... }:
10 # IPsec tunnel between Alice and Bob
12 services.libreswan.enable = true;
13 services.libreswan.connections.tunnel =
22 environment.etc."ipsec.d/tunnel.secrets" =
23 { text = ''@alice @bob : PSK "j1JbIi9WY07rxwcNQ6nbyThKCf9DGxWOyokXIQcAQUnafsNTUJxfsxwk9WYK8fHj"'';
28 # Common network setup
31 extraHosts = lib.mkVMOverride ''
36 # remove all automatic addresses
38 interfaces.eth1.ipv4.addresses = lib.mkVMOverride [];
39 interfaces.eth2.ipv4.addresses = lib.mkVMOverride [];
40 # open a port for testing
41 firewall.allowedUDPPorts = [ 1234 ];
44 # Adds an address and route from a to b via Eve
46 interfaces.eth1.ipv6.addresses =
47 [ { address = a; prefixLength = 64; } ];
48 interfaces.eth1.ipv6.routes =
49 [ { address = b; prefixLength = 128; via = "fd::e"; } ];
56 meta = with lib.maintainers; {
57 maintainers = [ rnhmjoj ];
61 nodes.alice = { ... }: {
62 virtualisation.vlans = [ 1 ];
63 networking = baseNetwork // addRoute "fd::a" "fd::b";
67 nodes.bob = { ... }: {
68 virtualisation.vlans = [ 2 ];
69 networking = baseNetwork // addRoute "fd::b" "fd::a";
72 # The malicious network operator
73 nodes.eve = { ... }: {
74 virtualisation.vlans = [ 1 2 ];
75 networking = lib.mkMerge
77 { interfaces.br0.ipv6.addresses =
78 [ { address = "fd::e"; prefixLength = 64; } ];
79 bridges.br0.interfaces = [ "eth1" "eth2" ];
82 environment.systemPackages = [ pkgs.tcpdump ];
83 boot.kernel.sysctl."net.ipv6.conf.all.forwarding" = true;
88 def alice_to_bob(msg: str):
90 Sends a message as Alice to Bob
92 bob.execute("nc -lu ::0 1234 >/tmp/msg &")
94 alice.succeed(f"echo '{msg}' | nc -uw 0 bob 1234")
95 bob.succeed(f"grep '{msg}' /tmp/msg")
100 Starts eavesdropping on Alice and Bob
102 match = "src host alice and dst host bob"
103 eve.execute(f"tcpdump -i br0 -c 1 -Avv {match} >/tmp/log &")
108 with subtest("Network is up"):
109 alice.wait_until_succeeds("ping -c1 bob")
111 with subtest("Eve can eavesdrop cleartext traffic"):
113 alice_to_bob("I secretly love turnip")
115 eve.succeed("grep turnip /tmp/log")
117 with subtest("Libreswan is ready"):
118 alice.wait_for_unit("ipsec")
119 bob.wait_for_unit("ipsec")
120 alice.succeed("ipsec verify 1>&2")
122 with subtest("Alice and Bob can start the tunnel"):
123 alice.execute("ipsec auto --start tunnel >&2 &")
124 bob.succeed("ipsec auto --start tunnel")
125 # apparently this is needed to "wake" the tunnel
126 bob.execute("ping -c1 alice")
128 with subtest("Eve no longer can eavesdrop"):
130 alice_to_bob("Just kidding, I actually like rhubarb")
132 eve.fail("grep rhubarb /tmp/log")