1 # This test verifies that we can request and assign IPv6 prefixes from upstream
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.
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
10 import ./make-test-python.nix ({ pkgs, lib, ... }: {
11 name = "systemd-networkd-ipv6-prefix-delegation";
12 meta = with lib.maintainers; {
13 maintainers = [ andir hexa ];
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.
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 Kea for that task.
26 isp = { lib, pkgs, ... }: {
27 virtualisation.vlans = [ 1 ];
30 firewall.enable = false;
31 interfaces.eth1 = lib.mkForce {}; # Don't use scripted networking
39 matchConfig.Name = "eth1";
43 networkConfig.IPForward = true;
48 # Since we want to program the routes that we delegate to the "customer"
49 # into our routing table we must provide kea with the required capability.
50 systemd.services.kea-dhcp6-server.serviceConfig = {
51 AmbientCapabilities = [ "CAP_NET_ADMIN" ];
52 CapabilityBoundingSet = [ "CAP_NET_ADMIN" ];
56 # Configure the DHCPv6 server to hand out both IA_NA and IA_PD.
58 # We will hand out /48 prefixes from the subnet 2001:DB8:F000::/36.
59 # That gives us ~8k prefixes. That should be enough for this test.
61 # Since (usually) you will not receive a prefix with the router
62 # advertisements we also hand out /128 leases from the range
63 # 2001:DB8:0000:0000:FFFF::/112.
67 interfaces-config.interfaces = [ "eth1" ];
70 subnet = "2001:DB8:F::/36";
72 prefix = "2001:DB8:F::";
77 pool = "2001:DB8:0000:0000:FFFF::-2001:DB8:0000:0000:FFFF::FFFF";
81 # This is the glue between Kea and the Kernel FIB. DHCPv6
82 # rightfully has no concept of setting up a route in your
83 # FIB. This step really depends on your setup.
85 # In a production environment your DHCPv6 server is likely
86 # not the router. You might want to consider BGP, NETCONF
87 # calls, … in those cases.
89 # In this example we use the run script hook, that lets use
90 # execute anything and passes information via the environment.
91 # https://kea.readthedocs.io/en/kea-2.2.0/arm/hooks.html#run-script-run-script-support-for-external-hook-scripts
93 library = "${pkgs.kea}/lib/kea/hooks/libdhcp_run_script.so";
95 name = pkgs.writeShellScript "kea-run-hooks" ''
96 export PATH="${lib.makeBinPath (with pkgs; [ coreutils iproute2 ])}"
100 leases6_committed() {
101 for i in $(seq $LEASES6_SIZE); do
103 prefix_var="LEASES6_AT''${idx}_ADDRESS"
104 plen_var="LEASES6_AT''${idx}_PREFIX_LEN"
106 ip -6 route replace ''${!prefix_var}/''${!plen_var} via $QUERY6_REMOTE_ADDR dev $QUERY6_IFACE_NAME
111 echo "Unhandled function call ''${*}"
120 unknown_handler "''${@}"
130 # Finally we have to set up the router advertisements. While we could be
131 # using networkd or bird for this task `radvd` is probably the most
132 # venerable of them all. It was made explicitly for this purpose and
133 # the configuration is much more straightforward than what networkd
135 # As outlined above we will have to set the `Managed` flag as otherwise
136 # the clients will not know if they should do DHCPv6. (Some do
144 AdvOtherConfigFlag off; # we don't really have DNS or NTP or anything like that to distribute
156 # This will be our (residential) router that receives the IPv6 prefix (IA_PD)
157 # and /128 (IA_NA) allocation.
159 # Here we will actually start using networkd.
161 virtualisation.vlans = [ 1 2 ];
162 systemd.services.systemd-networkd.environment.SYSTEMD_LOG_LEVEL = "debug";
164 boot.kernel.sysctl = {
165 # we want to forward packets from the ISP to the client and back.
166 "net.ipv6.conf.all.forwarding" = 1;
172 # Consider enabling this in production and generating firewall rules
173 # for fowarding/input from the configured interfaces so you do not have
174 # to manage multiple places
175 firewall.enable = false;
180 # systemd-networkd will load the first network unit file
181 # that matches, ordered lexiographically by filename.
182 # /etc/systemd/network/{40-eth1,99-main}.network already
183 # exists. This network unit must be loaded for the test,
184 # however, hence why this network is named such.
186 # Configuration of the interface to the ISP.
187 # We must request accept RAs and request the PD prefix.
191 Description = "ISP interface";
193 #DHCP = false; # no need for legacy IP
196 # We care about this interface when talking about being "online".
197 # If this interface is in the `routable` state we can reach
198 # others and they should be able to reach us.
199 RequiredForOnline = "routable";
201 # This configures the DHCPv6 client part towards the ISPs DHCPv6 server.
203 # We have to include a request for a prefix in our DHCPv6 client
205 # Otherwise the upstream DHCPv6 server wouldn't know if we want a
206 # prefix or not. Note: On some installation it makes sense to
207 # always force that option on the DHPCv6 server since there are
208 # certain CPEs that are just not setting this field but happily
209 # accept the delegated prefix.
210 PrefixDelegationHint = "::/48";
213 # Let networkd know that we would very much like to use DHCPv6
214 # to obtain the "managed" information. Not sure why they can't
215 # just take that from the upstream RAs.
220 # Interface to the client. Here we should redistribute a /64 from
221 # the prefix we received from the ISP.
225 Description = "Client interface";
226 # The client shouldn't be allowed to send us RAs, that would be weird.
227 IPv6AcceptRA = false;
229 # Delegate prefixes from the DHCPv6 PD pool.
230 DHCPPrefixDelegation = true;
234 # In a production environment you should consider setting these as well:
235 # ipv6SendRAConfig = {
238 #DNS= = "fe80::1"; # or whatever "well known" IP your router will have on the inside.
241 # This adds a "random" ULA prefix to the interface that is being
242 # advertised to the clients.
243 # Not used in this test.
246 # ipv6PrefixConfig = {
247 # AddressAutoconfiguration = true;
248 # PreferredLifetimeSec = 1800;
249 # ValidLifetimeSec = 1800;
255 # finally we are going to add a static IPv6 unique local address to
256 # the "lo" interface. This will serve as ICMPv6 echo target to
257 # verify connectivity from the client to the router.
261 { addressConfig.Address = "FD42::1/128"; }
267 # make the network-online target a requirement, we wait for it in our test script
268 systemd.targets.network-online.wantedBy = [ "multi-user.target" ];
271 # This is the client behind the router. We should be receving router
272 # advertisements for both the ULA and the delegated prefix.
273 # All we have to do is boot with the default (networkd) configuration.
275 virtualisation.vlans = [ 2 ];
276 systemd.services.systemd-networkd.environment.SYSTEMD_LOG_LEVEL = "debug";
282 # make the network-online target a requirement, we wait for it in our test script
283 systemd.targets.network-online.wantedBy = [ "multi-user.target" ];
288 # First start the router and wait for it it reach a state where we are
289 # certain networkd is up and it is able to send out RAs
291 router.wait_for_unit("systemd-networkd.service")
293 # After that we can boot the client and wait for the network online target.
294 # Since we only care about IPv6 that should not involve waiting for legacy
297 client.wait_for_unit("network-online.target")
299 # the static address on the router should not be reachable
300 client.wait_until_succeeds("ping -6 -c 1 FD42::1")
302 # the global IP of the ISP router should still not be a reachable
303 router.fail("ping -6 -c 1 2001:DB8::1")
305 # Once we have internal connectivity boot up the ISP
308 # Since for the ISP "being online" should have no real meaning we just
309 # wait for the target where all the units have been started.
310 # It probably still takes a few more seconds for all the RA timers to be
312 isp.wait_for_unit("multi-user.target")
314 # wait until the uplink interface has a good status
315 router.wait_for_unit("network-online.target")
316 router.wait_until_succeeds("ping -6 -c1 2001:DB8::1")
318 # shortly after that the client should have received it's global IPv6
319 # address and thus be able to ping the ISP
320 client.wait_until_succeeds("ping -6 -c1 2001:DB8::1")
322 # verify that we got a globally scoped address in eth1 from the
323 # documentation prefix
324 ip_output = client.succeed("ip --json -6 address show dev eth1")
328 ip_json = json.loads(ip_output)[0]
330 addr["local"].upper().startswith("2001:DB8:")
331 for addr in ip_json["addr_info"]
332 if addr["scope"] == "global"