at 21.11-pre 12 kB view raw
1# This test verifies that we can request and assign IPv6 prefixes from upstream 2# (e.g. ISP) routers. 3# The setup consits of three VMs. One for the ISP, as your residential router 4# and the third as a client machine in the residential network. 5# 6# There are two VLANs in this test: 7# - VLAN 1 is the connection between the ISP and the router 8# - VLAN 2 is the connection between the router and the client 9 10import ./make-test-python.nix ({pkgs, ...}: { 11 name = "systemd-networkd-ipv6-prefix-delegation"; 12 meta = with pkgs.lib.maintainers; { 13 maintainers = [ andir ]; 14 }; 15 nodes = { 16 17 # The ISP's routers job is to delegate IPv6 prefixes via DHCPv6. Like with 18 # regular IPv6 auto-configuration it will also emit IPv6 router 19 # advertisements (RAs). Those RA's will not carry a prefix but in contrast 20 # just set the "Other" flag to indicate to the receiving nodes that they 21 # should attempt DHCPv6. 22 # 23 # Note: On the ISPs device we don't really care if we are using networkd in 24 # this example. That being said we can't use it (yet) as networkd doesn't 25 # implement the serving side of DHCPv6. We will use ISC's well aged dhcpd6 26 # for that task. 27 isp = { lib, pkgs, ... }: { 28 virtualisation.vlans = [ 1 ]; 29 networking = { 30 useDHCP = false; 31 firewall.enable = false; 32 interfaces.eth1.ipv4.addresses = lib.mkForce []; # no need for legacy IP 33 interfaces.eth1.ipv6.addresses = lib.mkForce [ 34 { address = "2001:DB8::1"; prefixLength = 64; } 35 ]; 36 }; 37 38 # Since we want to program the routes that we delegate to the "customer" 39 # into our routing table we must have a way to gain the required privs. 40 # This security wrapper will do in our test setup. 41 # 42 # DO NOT COPY THIS TO PRODUCTION AS IS. Think about it at least twice. 43 # Everyone on the "isp" machine will be able to add routes to the kernel. 44 security.wrappers.add-dhcpd-lease = { 45 source = pkgs.writeShellScript "add-dhcpd-lease" '' 46 exec ${pkgs.iproute2}/bin/ip -6 route replace "$1" via "$2" 47 ''; 48 capabilities = "cap_net_admin+ep"; 49 }; 50 services = { 51 # Configure the DHCPv6 server 52 # 53 # We will hand out /48 prefixes from the subnet 2001:DB8:F000::/36. 54 # That gives us ~8k prefixes. That should be enough for this test. 55 # 56 # Since (usually) you will not receive a prefix with the router 57 # advertisements we also hand out /128 leases from the range 58 # 2001:DB8:0000:0000:FFFF::/112. 59 dhcpd6 = { 60 enable = true; 61 interfaces = [ "eth1" ]; 62 extraConfig = '' 63 subnet6 2001:DB8::/36 { 64 range6 2001:DB8:0000:0000:FFFF:: 2001:DB8:0000:0000:FFFF::FFFF; 65 prefix6 2001:DB8:F000:: 2001:DB8:FFFF:: /48; 66 } 67 68 # This is the secret sauce. We have to extract the prefix and the 69 # next hop when commiting the lease to the database. dhcpd6 70 # (rightfully) has not concept of adding routes to the systems 71 # routing table. It really depends on the setup. 72 # 73 # In a production environment your DHCPv6 server is likely not the 74 # router. You might want to consider BGP, custom NetConf calls, 75 # in those cases. 76 on commit { 77 set IP = pick-first-value(binary-to-ascii(16, 16, ":", substring(option dhcp6.ia-na, 16, 16)), "n/a"); 78 set Prefix = pick-first-value(binary-to-ascii(16, 16, ":", suffix(option dhcp6.ia-pd, 16)), "n/a"); 79 set PrefixLength = pick-first-value(binary-to-ascii(10, 8, ":", substring(suffix(option dhcp6.ia-pd, 17), 0, 1)), "n/a"); 80 log(concat(IP, " ", Prefix, " ", PrefixLength)); 81 execute("/run/wrappers/bin/add-dhcpd-lease", concat(Prefix,"/",PrefixLength), IP); 82 } 83 ''; 84 }; 85 86 # Finally we have to set up the router advertisements. While we could be 87 # using networkd or bird for this task `radvd` is probably the most 88 # venerable of them all. It was made explicitly for this purpose and 89 # the configuration is much more straightforward than what networkd 90 # requires. 91 # As outlined above we will have to set the `Managed` flag as otherwise 92 # the clients will not know if they should do DHCPv6. (Some do 93 # anyway/always) 94 radvd = { 95 enable = true; 96 config = '' 97 interface eth1 { 98 AdvSendAdvert on; 99 AdvManagedFlag on; 100 AdvOtherConfigFlag off; # we don't really have DNS or NTP or anything like that to distribute 101 prefix ::/64 { 102 AdvOnLink on; 103 AdvAutonomous on; 104 }; 105 }; 106 ''; 107 }; 108 109 }; 110 }; 111 112 # This will be our (residential) router that receives the IPv6 prefix (IA_PD) 113 # and /128 (IA_NA) allocation. 114 # 115 # Here we will actually start using networkd. 116 router = { 117 virtualisation.vlans = [ 1 2 ]; 118 systemd.services.systemd-networkd.environment.SYSTEMD_LOG_LEVEL = "debug"; 119 120 boot.kernel.sysctl = { 121 # we want to forward packets from the ISP to the client and back. 122 "net.ipv6.conf.all.forwarding" = 1; 123 }; 124 125 networking = { 126 useNetworkd = true; 127 useDHCP = false; 128 # Consider enabling this in production and generating firewall rules 129 # for fowarding/input from the configured interfaces so you do not have 130 # to manage multiple places 131 firewall.enable = false; 132 }; 133 134 systemd.network = { 135 networks = { 136 # systemd-networkd will load the first network unit file 137 # that matches, ordered lexiographically by filename. 138 # /etc/systemd/network/{40-eth1,99-main}.network already 139 # exists. This network unit must be loaded for the test, 140 # however, hence why this network is named such. 141 142 # Configuration of the interface to the ISP. 143 # We must request accept RAs and request the PD prefix. 144 "01-eth1" = { 145 name = "eth1"; 146 networkConfig = { 147 Description = "ISP interface"; 148 IPv6AcceptRA = true; 149 #DHCP = false; # no need for legacy IP 150 }; 151 linkConfig = { 152 # We care about this interface when talking about being "online". 153 # If this interface is in the `routable` state we can reach 154 # others and they should be able to reach us. 155 RequiredForOnline = "routable"; 156 }; 157 # This configures the DHCPv6 client part towards the ISPs DHCPv6 server. 158 dhcpV6Config = { 159 # We have to include a request for a prefix in our DHCPv6 client 160 # request packets. 161 # Otherwise the upstream DHCPv6 server wouldn't know if we want a 162 # prefix or not. Note: On some installation it makes sense to 163 # always force that option on the DHPCv6 server since there are 164 # certain CPEs that are just not setting this field but happily 165 # accept the delegated prefix. 166 PrefixDelegationHint = "::/48"; 167 }; 168 ipv6SendRAConfig = { 169 # Let networkd know that we would very much like to use DHCPv6 170 # to obtain the "managed" information. Not sure why they can't 171 # just take that from the upstream RAs. 172 Managed = true; 173 }; 174 }; 175 176 # Interface to the client. Here we should redistribute a /64 from 177 # the prefix we received from the ISP. 178 "01-eth2" = { 179 name = "eth2"; 180 networkConfig = { 181 Description = "Client interface"; 182 # The client shouldn't be allowed to send us RAs, that would be weird. 183 IPv6AcceptRA = false; 184 185 # Delegate prefixes from the DHCPv6 PD pool. 186 DHCPv6PrefixDelegation = true; 187 IPv6SendRA = true; 188 }; 189 190 # In a production environment you should consider setting these as well: 191 # ipv6SendRAConfig = { 192 #EmitDNS = true; 193 #EmitDomains = true; 194 #DNS= = "fe80::1"; # or whatever "well known" IP your router will have on the inside. 195 # }; 196 197 # This adds a "random" ULA prefix to the interface that is being 198 # advertised to the clients. 199 # Not used in this test. 200 # ipv6Prefixes = [ 201 # { 202 # ipv6PrefixConfig = { 203 # AddressAutoconfiguration = true; 204 # PreferredLifetimeSec = 1800; 205 # ValidLifetimeSec = 1800; 206 # }; 207 # } 208 # ]; 209 }; 210 211 # finally we are going to add a static IPv6 unique local address to 212 # the "lo" interface. This will serve as ICMPv6 echo target to 213 # verify connectivity from the client to the router. 214 "01-lo" = { 215 name = "lo"; 216 addresses = [ 217 { addressConfig.Address = "FD42::1/128"; } 218 ]; 219 }; 220 }; 221 }; 222 223 # make the network-online target a requirement, we wait for it in our test script 224 systemd.targets.network-online.wantedBy = [ "multi-user.target" ]; 225 }; 226 227 # This is the client behind the router. We should be receving router 228 # advertisements for both the ULA and the delegated prefix. 229 # All we have to do is boot with the default (networkd) configuration. 230 client = { 231 virtualisation.vlans = [ 2 ]; 232 systemd.services.systemd-networkd.environment.SYSTEMD_LOG_LEVEL = "debug"; 233 networking = { 234 useNetworkd = true; 235 useDHCP = false; 236 }; 237 238 # make the network-online target a requirement, we wait for it in our test script 239 systemd.targets.network-online.wantedBy = [ "multi-user.target" ]; 240 }; 241 }; 242 243 testScript = '' 244 # First start the router and wait for it it reach a state where we are 245 # certain networkd is up and it is able to send out RAs 246 router.start() 247 router.wait_for_unit("systemd-networkd.service") 248 249 # After that we can boot the client and wait for the network online target. 250 # Since we only care about IPv6 that should not involve waiting for legacy 251 # IP leases. 252 client.start() 253 client.wait_for_unit("network-online.target") 254 255 # the static address on the router should not be reachable 256 client.wait_until_succeeds("ping -6 -c 1 FD42::1") 257 258 # the global IP of the ISP router should still not be a reachable 259 router.fail("ping -6 -c 1 2001:DB8::1") 260 261 # Once we have internal connectivity boot up the ISP 262 isp.start() 263 264 # Since for the ISP "being online" should have no real meaning we just 265 # wait for the target where all the units have been started. 266 # It probably still takes a few more seconds for all the RA timers to be 267 # fired etc.. 268 isp.wait_for_unit("multi-user.target") 269 270 # wait until the uplink interface has a good status 271 router.wait_for_unit("network-online.target") 272 router.wait_until_succeeds("ping -6 -c1 2001:DB8::1") 273 274 # shortly after that the client should have received it's global IPv6 275 # address and thus be able to ping the ISP 276 client.wait_until_succeeds("ping -6 -c1 2001:DB8::1") 277 278 # verify that we got a globally scoped address in eth1 from the 279 # documentation prefix 280 ip_output = client.succeed("ip --json -6 address show dev eth1") 281 282 import json 283 284 ip_json = json.loads(ip_output)[0] 285 assert any( 286 addr["local"].upper().startswith("2001:DB8:") 287 for addr in ip_json["addr_info"] 288 if addr["scope"] == "global" 289 ) 290 ''; 291})