1 # This test verifies that we can request and assign IPv6 prefixes from upstream
3 # The setup consists 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.IPv4Forwarding = true;
44 networkConfig.IPv6Forwarding = true;
49 # Since we want to program the routes that we delegate to the "customer"
50 # into our routing table we must provide kea with the required capability.
51 systemd.services.kea-dhcp6-server.serviceConfig = {
52 AmbientCapabilities = [ "CAP_NET_ADMIN" ];
53 CapabilityBoundingSet = [ "CAP_NET_ADMIN" ];
57 # Configure the DHCPv6 server to hand out both IA_NA and IA_PD.
59 # We will hand out /48 prefixes from the subnet 2001:DB8:F000::/36.
60 # That gives us ~8k prefixes. That should be enough for this test.
62 # Since (usually) you will not receive a prefix with the router
63 # advertisements we also hand out /128 leases from the range
64 # 2001:DB8:0000:0000:FFFF::/112.
68 interfaces-config.interfaces = [ "eth1" ];
72 subnet = "2001:DB8::/32";
74 prefix = "2001:DB8:1000::";
79 pool = "2001:DB8:0000:0000::-2001:DB8:0FFF:FFFF::FFFF";
83 # This is the glue between Kea and the Kernel FIB. DHCPv6
84 # rightfully has no concept of setting up a route in your
85 # FIB. This step really depends on your setup.
87 # In a production environment your DHCPv6 server is likely
88 # not the router. You might want to consider BGP, NETCONF
89 # calls, … in those cases.
91 # In this example we use the run script hook, that lets use
92 # execute anything and passes information via the environment.
93 # https://kea.readthedocs.io/en/kea-2.2.0/arm/hooks.html#run-script-run-script-support-for-external-hook-scripts
95 library = "${pkgs.kea}/lib/kea/hooks/libdhcp_run_script.so";
97 name = pkgs.writeShellScript "kea-run-hooks" ''
98 export PATH="${lib.makeBinPath (with pkgs; [ coreutils iproute2 ])}"
102 leases6_committed() {
103 for i in $(seq $LEASES6_SIZE); do
105 prefix_var="LEASES6_AT''${idx}_ADDRESS"
106 plen_var="LEASES6_AT''${idx}_PREFIX_LEN"
108 ip -6 route replace ''${!prefix_var}/''${!plen_var} via $QUERY6_REMOTE_ADDR dev $QUERY6_IFACE_NAME
113 echo "Unhandled function call ''${*}"
122 unknown_handler "''${@}"
132 # Finally we have to set up the router advertisements. While we could be
133 # using networkd or bird for this task `radvd` is probably the most
134 # venerable of them all. It was made explicitly for this purpose and
135 # the configuration is much more straightforward than what networkd
137 # As outlined above we will have to set the `Managed` flag as otherwise
138 # the clients will not know if they should do DHCPv6. (Some do
146 AdvOtherConfigFlag off; # we don't really have DNS or NTP or anything like that to distribute
158 # This will be our (residential) router that receives the IPv6 prefix (IA_PD)
159 # and /128 (IA_NA) allocation.
161 # Here we will actually start using networkd.
163 virtualisation.vlans = [ 1 2 ];
164 systemd.services.systemd-networkd.environment.SYSTEMD_LOG_LEVEL = "debug";
166 boot.kernel.sysctl = {
167 # we want to forward packets from the ISP to the client and back.
168 "net.ipv6.conf.all.forwarding" = 1;
174 # Consider enabling this in production and generating firewall rules
175 # for fowarding/input from the configured interfaces so you do not have
176 # to manage multiple places
177 firewall.enable = false;
178 interfaces.eth1.ipv6.addresses = lib.mkForce [ ];
183 # systemd-networkd will load the first network unit file
184 # that matches, ordered lexiographically by filename.
185 # /etc/systemd/network/{40-eth1,99-main}.network already
186 # exists. This network unit must be loaded for the test,
187 # however, hence why this network is named such.
189 # Configuration of the interface to the ISP.
190 # We must request accept RAs and request the PD prefix.
194 Description = "ISP interface";
196 #DHCP = false; # no need for legacy IP
199 # We care about this interface when talking about being "online".
200 # If this interface is in the `routable` state we can reach
201 # others and they should be able to reach us.
202 RequiredForOnline = "routable";
204 # This configures the DHCPv6 client part towards the ISPs DHCPv6 server.
206 # We have to include a request for a prefix in our DHCPv6 client
208 # Otherwise the upstream DHCPv6 server wouldn't know if we want a
209 # prefix or not. Note: On some installation it makes sense to
210 # always force that option on the DHPCv6 server since there are
211 # certain CPEs that are just not setting this field but happily
212 # accept the delegated prefix.
213 PrefixDelegationHint = "::/48";
216 # Let networkd know that we would very much like to use DHCPv6
217 # to obtain the "managed" information. Not sure why they can't
218 # just take that from the upstream RAs.
223 # Interface to the client. Here we should redistribute a /64 from
224 # the prefix we received from the ISP.
228 Description = "Client interface";
229 # The client shouldn't be allowed to send us RAs, that would be weird.
230 IPv6AcceptRA = false;
232 # Delegate prefixes from the DHCPv6 PD pool.
233 DHCPPrefixDelegation = true;
237 # In a production environment you should consider setting these as well:
238 # ipv6SendRAConfig = {
241 #DNS= = "fe80::1"; # or whatever "well known" IP your router will have on the inside.
244 # This adds a "random" ULA prefix to the interface that is being
245 # advertised to the clients.
246 # Not used in this test.
249 # ipv6PrefixConfig = {
250 # AddressAutoconfiguration = true;
251 # PreferredLifetimeSec = 1800;
252 # ValidLifetimeSec = 1800;
258 # finally we are going to add a static IPv6 unique local address to
259 # the "lo" interface. This will serve as ICMPv6 echo target to
260 # verify connectivity from the client to the router.
264 { Address = "FD42::1/128"; }
271 # This is the client behind the router. We should be receiving 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";
280 interfaces.eth1.ipv6.addresses = lib.mkForce [ ];
286 # First start the router and wait for it it reach a state where we are
287 # certain networkd is up and it is able to send out RAs
289 router.wait_for_unit("systemd-networkd.service")
291 # After that we can boot the client and wait for the network online target.
292 # Since we only care about IPv6 that should not involve waiting for legacy
295 client.systemctl("start network-online.target")
296 client.wait_for_unit("network-online.target")
298 # the static address on the router should not be reachable
299 client.wait_until_succeeds("ping -6 -c 1 FD42::1")
301 # the global IP of the ISP router should still not be a reachable
302 router.fail("ping -6 -c 1 2001:DB8::1")
304 # Once we have internal connectivity boot up the ISP
307 # Since for the ISP "being online" should have no real meaning we just
308 # wait for the target where all the units have been started.
309 # It probably still takes a few more seconds for all the RA timers to be
311 isp.wait_for_unit("multi-user.target")
313 # wait until the uplink interface has a good status
314 router.systemctl("start network-online.target")
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"