HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

test/test-network/systemd-networkd-tests.py

468,283 bytesGit mode 100755Download original fileLGPL-2.1-or-later

Git blob: aaf8830b28df24259c9ca79d57929ab5449e46af

Lines 9001–10000 of 11,559

        print(f'DHCPv4 client state = {state}')
        self.assertEqual(state, 'stopped')
 
        # restart dnsmasq to clear log
        stop_dnsmasq()
        start_dnsmasq('--dhcp-option=108,00:00:02:00')
 
        # Test renew command
        # See https://github.com/systemd/systemd/pull/29472#issuecomment-1759092138
        networkctl('renew', 'veth99')
 
        for _ in range(100):
            state = get_dhcp4_client_state('veth99')
            if state == 'stopped':
                break
            time.sleep(0.2)
 
        print(f'DHCPv4 client state = {state}')
        self.assertEqual(state, 'stopped')
 
        print('## dnsmasq log')
        output = read_dnsmasq_log_file()
        print(output)
        self.assertIn('DHCPDISCOVER(veth-peer) 12:34:56:78:9a:bc', output)
        self.assertIn('DHCPOFFER(veth-peer)', output)
        self.assertNotIn('DHCPREQUEST(veth-peer)', output)
        self.assertNotIn('DHCPACK(veth-peer)', output)
 
    def test_dhcp_client_ipv6_only_with_custom_client_identifier(self):
        copy_network_unit(
            '25-veth.netdev',
            '25-dhcp-server-veth-peer.network',
            '25-dhcp-client-ipv6-only-custom-client-identifier.network',
        )
 
        start_networkd()
        self.wait_online('veth-peer:carrier')
        start_dnsmasq()
        self.wait_online('veth99:routable', 'veth-peer:routable')
 
        # checking address
        output = check_output('ip address show dev veth99 scope global')
        print(output)
        self.assertRegex(output, r'inet6 2600::[0-9a-f:]*/128 scope global dynamic noprefixroute')
        self.assertNotIn('192.168.5', output)
 
        print('## dnsmasq log')
        output = read_dnsmasq_log_file()
        print(output)
        self.assertIn('DHCPSOLICIT(veth-peer) 00:42:00:00:ab:11:f9:2a:c2:77:29:f9:5c:00', output)
        self.assertNotIn('DHCPADVERTISE(veth-peer)', output)
        self.assertNotIn('DHCPREQUEST(veth-peer)', output)
        self.assertIn('DHCPREPLY(veth-peer)', output)
        self.assertIn('sent size:  0 option: 14 rapid-commit', output)
 
    def test_dhcp_client_ipv4_only(self):
        copy_network_unit(
            '25-veth.netdev',
            '25-dhcp-server-veth-peer.network',
            '25-dhcp-client-ipv4-only.network',
            '25-sit-dhcp4.netdev',
            '25-sit-dhcp4.network',
        )
 
        self.setup_nftset('addr4', 'ipv4_addr')
        self.setup_nftset('network4', 'ipv4_addr', 'flags interval;')
        self.setup_nftset('ifindex', 'iface_index')
 
        start_networkd()
        self.wait_online('veth-peer:carrier')
        start_dnsmasq(
            '--dhcp-option=option:dns-server,192.168.5.6,192.168.5.7',
            '--dhcp-option=option:sip-server,192.168.5.21,192.168.5.22',
            '--dhcp-option=option:domain-search,example.com',
            '--dhcp-alternate-port=67,5555',
            ipv4_range='192.168.5.110,192.168.5.119',
        )
        self.wait_online('veth99:routable', 'veth-peer:routable', 'sit-dhcp4:carrier')
        self.wait_address('veth99', r'inet 192.168.5.11[0-9]*/24', ipv='-4')
 
        print('## ip address show dev veth99 scope global')
        output = check_output('ip address show dev veth99 scope global')
        print(output)
        self.assertIn('mtu 1492', output)
        self.assertIn('inet 192.168.5.250/24 brd 192.168.5.255 scope global veth99', output)
        self.assertRegex(output, r'inet 192.168.5.11[0-9]/24 metric 24 brd 192.168.5.255 scope global secondary dynamic noprefixroute test-label')  # fmt: skip
        self.assertNotIn('2600::', output)
 
        output = check_output('ip -4 --json address show dev veth99')
        for i in json.loads(output)[0]['addr_info']:
            if i['label'] == 'test-label':
                address1 = i['local']
                break
        else:
            self.assertFalse(True)
 
        self.assertRegex(address1, r'^192.168.5.11[0-9]$')
 
        print('## ip route show table main dev veth99')
        output = check_output('ip route show table main dev veth99')
        print(output)
        # no DHCP routes assigned to the main table
        self.assertNotIn('proto dhcp', output)
        # static routes
        self.assertIn('192.168.5.0/24 proto kernel scope link src 192.168.5.250', output)
        self.assertIn('192.168.5.0/24 proto static scope link', output)
        self.assertIn('192.168.6.0/24 proto static scope link', output)
        self.assertIn('192.168.7.0/24 proto static scope link', output)
 
        print('## ip route show table 211 dev veth99')
        output = check_output('ip route show table 211 dev veth99')
        print(output)
        self.assertRegex(output, f'default via 192.168.5.1 proto dhcp src {address1} metric 24')
        self.assertRegex(output, f'192.168.5.0/24 proto dhcp scope link src {address1} metric 24')
        self.assertRegex(output, f'192.168.5.1 proto dhcp scope link src {address1} metric 24')
        self.assertRegex(output, f'192.168.5.6 proto dhcp scope link src {address1} metric 24')
        self.assertRegex(output, f'192.168.5.7 proto dhcp scope link src {address1} metric 24')
        self.assertRegex(output, f'192.0.2.0/24 via 192.168.5.1 proto dhcp src {address1}')
 
        print('## ip route show table 212 dev veth99')
        output = check_output('ip route show table 212 dev veth99')
        print(output)
        self.assertRegex(output, f'192.168.5.0/24 proto dhcp scope link src {address1} metric 24')
        self.assertRegex(output, f'192.168.5.1 proto dhcp scope link src {address1} metric 24')
        self.assertRegex(output, f'198.51.100.0/24 via 192.168.5.1 proto dhcp src {address1}')
 
        print('## link state file')
        output = read_link_state_file('veth99')
        print(output)
        # checking DNS server, SIP server, and Domains
        self.assertIn('DNS=192.168.5.6 192.168.5.7', output)
        self.assertIn('SIP=192.168.5.21 192.168.5.22', output)
        self.assertIn('DOMAINS=example.com', output)
 
        print('## json')
        j = json.loads(networkctl_json('veth99'))
 
        self.assertEqual(len(j['DNS']), 2)
        for i in j['DNS']:
            print(i)
            self.assertEqual(i['Family'], 2)
            a = socket.inet_ntop(socket.AF_INET, bytearray(i['Address']))
            self.assertRegex(a, '^192.168.5.[67]$')
            self.assertEqual(i['ConfigSource'], 'DHCPv4')
            a = socket.inet_ntop(socket.AF_INET, bytearray(i['ConfigProvider']))
            self.assertEqual('192.168.5.1', a)
 
        self.assertEqual(len(j['SIP']), 2)
        for i in j['SIP']:
            print(i)
            self.assertEqual(i['Family'], 2)
            a = socket.inet_ntop(socket.AF_INET, bytearray(i['Address']))
            self.assertRegex(a, '^192.168.5.2[12]$')
            self.assertEqual(i['ConfigSource'], 'DHCPv4')
            a = socket.inet_ntop(socket.AF_INET, bytearray(i['ConfigProvider']))
            self.assertEqual('192.168.5.1', a)
 
        print('## tunnel')
        output = check_output('ip -d link show sit-dhcp4')
        print(output)
        self.assertRegex(output, rf'sit (ip6ip )?remote any local {address1} dev veth99')
 
        print('## dnsmasq log')
        output = read_dnsmasq_log_file()
        print(output)
        self.assertIn('vendor class: FooBarVendorTest', output)
        self.assertIn('DHCPDISCOVER(veth-peer) 192.168.5.110 12:34:56:78:9a:bc', output)
        self.assertIn('client provides name: test-hostname', output)
        self.assertIn('26:mtu', output)
 
        print('### networkctl dhcp-lease')
        output = networkctl_dhcp_lease('veth99')
        print(output)
        # header
        self.assertIn('Hardware Type: ETHER', output)
        self.assertIn('Hardware Address: 12:34:56:78:9a:bc', output)
        self.assertIn(f'Client Address: {address1}', output)
        self.assertIn('Server Address: 192.168.5.1', output)
        # options
        self.assertRegex(output, r'1 subnet mask *255\.255\.255\.0')
        self.assertRegex(output, r'3 router *192\.168\.5\.1')
        self.assertRegex(output, r'6 domain name server *192\.168\.5\.6\n *192\.168\.5\.7')
        self.assertRegex(output, r'26 MTU size *1492')
        self.assertRegex(output, r'28 broadcast address *192\.168\.5\.255')
        self.assertRegex(output, r'51 lease time *2min')
        self.assertRegex(output, r'53 message type *5')
        self.assertRegex(output, r'54 server identifier *192\.168\.5\.1')
        self.assertRegex(output, r'119 domain search *example\.com')
        self.assertRegex(output, r'120 SIP server *192\.168\.5\.21\n *192\.168\.5\.22')
        # extra arguments
        output = networkctl_dhcp_lease('veth99', '1')
        print(output)
        self.assertRegex(output, r'1 subnet mask *255\.255\.255\.0')
        output = networkctl_dhcp_lease('veth99', '1:auto')
        print(output)
        self.assertRegex(output, r'1 subnet mask *255\.255\.255\.0')
        output = networkctl_dhcp_lease('veth99', '1:hex')
        print(output)
        self.assertRegex(output, 'CODE *NAME *DATA')
        self.assertRegex(output, r'1 subnet mask *ff:ff:ff:00')
        output = networkctl_dhcp_lease('veth99', '1:hex', '--no-legend')
        print(output)
        self.assertNotIn('CODE', output)
        self.assertNotIn('NAME', output)
        self.assertNotIn('DATA', output)
        self.assertRegex(output, r'1 subnet mask *ff:ff:ff:00')
 
        # change address range, DNS servers, and Domains
        stop_dnsmasq()
        start_dnsmasq(
            '--dhcp-option=option:dns-server,192.168.5.1,192.168.5.7,192.168.5.8',
            '--dhcp-option=option:sip-server,192.168.5.23,192.168.5.24',
            '--dhcp-option=option:domain-search,foo.example.com',
            '--dhcp-alternate-port=67,5555',
            ipv4_range='192.168.5.120,192.168.5.129',
        )
 
        # Sleep for 120 sec as the dnsmasq minimum lease time can only be set to 120
        print('Wait for the DHCP lease to be expired')
        self.wait_address_dropped('veth99', f'inet {address1}/24', ipv='-4', timeout_sec=120)
        self.wait_address('veth99', r'inet 192.168.5.12[0-9]*/24', ipv='-4')
 
        self.wait_online('veth99:routable', 'veth-peer:routable')
 
        print('## ip address show dev veth99 scope global')
        output = check_output('ip address show dev veth99 scope global')
        print(output)
        self.assertIn('mtu 1492', output)
        self.assertIn('inet 192.168.5.250/24 brd 192.168.5.255 scope global veth99', output)
        self.assertNotIn(f'{address1}', output)
        self.assertRegex(output, r'inet 192.168.5.12[0-9]/24 metric 24 brd 192.168.5.255 scope global secondary dynamic noprefixroute test-label')  # fmt: skip
        self.assertNotIn('2600::', output)
 
        output = check_output('ip -4 --json address show dev veth99')
        for i in json.loads(output)[0]['addr_info']:
            if i['label'] == 'test-label':
                address2 = i['local']
                break
        else:
            self.assertFalse(True)
 
        self.assertRegex(address2, r'^192.168.5.12[0-9]$')
 
        print('## ip route show table main dev veth99')
        output = check_output('ip route show table main dev veth99')
        print(output)
        # no DHCP routes assigned to the main table
        self.assertNotIn('proto dhcp', output)
        # static routes
        self.assertIn('192.168.5.0/24 proto kernel scope link src 192.168.5.250', output)
        self.assertIn('192.168.5.0/24 proto static scope link', output)
        self.assertIn('192.168.6.0/24 proto static scope link', output)
        self.assertIn('192.168.7.0/24 proto static scope link', output)
 
        print('## ip route show table 211 dev veth99')
        output = check_output('ip route show table 211 dev veth99')
        print(output)
        self.assertRegex(output, f'default via 192.168.5.1 proto dhcp src {address2} metric 24')
        self.assertRegex(output, f'192.168.5.0/24 proto dhcp scope link src {address2} metric 24')
        self.assertRegex(output, f'192.168.5.1 proto dhcp scope link src {address2} metric 24')
        self.assertNotIn('192.168.5.6', output)
        self.assertRegex(output, f'192.168.5.7 proto dhcp scope link src {address2} metric 24')
        self.assertRegex(output, f'192.168.5.8 proto dhcp scope link src {address2} metric 24')
        self.assertRegex(output, f'192.0.2.0/24 via 192.168.5.1 proto dhcp src {address2}')
 
        print('## ip route show table 212 dev veth99')
        output = check_output('ip route show table 212 dev veth99')
        print(output)
        self.assertRegex(output, f'192.168.5.0/24 proto dhcp scope link src {address2} metric 24')
        self.assertRegex(output, f'192.168.5.1 proto dhcp scope link src {address2} metric 24')
        self.assertRegex(output, f'198.51.100.0/24 via 192.168.5.1 proto dhcp src {address2}')
 
        print('## link state file')
        output = read_link_state_file('veth99')
        print(output)
        # checking DNS server, SIP server, and Domains
        self.assertIn('DNS=192.168.5.1 192.168.5.7 192.168.5.8', output)
        self.assertIn('SIP=192.168.5.23 192.168.5.24', output)
        self.assertIn('DOMAINS=foo.example.com', output)
 
        print('## json')
        j = json.loads(networkctl_json('veth99'))
 
        self.assertEqual(len(j['DNS']), 3)
        for i in j['DNS']:
            print(i)
            self.assertEqual(i['Family'], 2)
            a = socket.inet_ntop(socket.AF_INET, bytearray(i['Address']))
            self.assertRegex(a, '^192.168.5.[178]$')
            self.assertEqual(i['ConfigSource'], 'DHCPv4')
            a = socket.inet_ntop(socket.AF_INET, bytearray(i['ConfigProvider']))
            self.assertEqual('192.168.5.1', a)
 
        self.assertEqual(len(j['SIP']), 2)
        for i in j['SIP']:
            print(i)
            self.assertEqual(i['Family'], 2)
            a = socket.inet_ntop(socket.AF_INET, bytearray(i['Address']))
            self.assertRegex(a, '^192.168.5.2[34]$')
            self.assertEqual(i['ConfigSource'], 'DHCPv4')
            a = socket.inet_ntop(socket.AF_INET, bytearray(i['ConfigProvider']))
            self.assertEqual('192.168.5.1', a)
 
        print('## tunnel')
        output = check_output('ip -d link show sit-dhcp4')
        print(output)
        self.assertRegex(output, rf'sit (ip6ip )?remote any local {address2} dev veth99')
 
        print('## dnsmasq log')
        output = read_dnsmasq_log_file()
        print(output)
        self.assertIn('vendor class: FooBarVendorTest', output)
        self.assertIn(f'DHCPDISCOVER(veth-peer) {address1} 12:34:56:78:9a:bc', output)
        self.assertIn('client provides name: test-hostname', output)
        self.assertIn('26:mtu', output)
 
        print('### networkctl dhcp-lease')
        output = networkctl_dhcp_lease('veth99')
        print(output)
        # header
        self.assertIn('Hardware Type: ETHER', output)
        self.assertIn('Hardware Address: 12:34:56:78:9a:bc', output)
        self.assertIn(f'Client Address: {address2}', output)
        self.assertIn('Server Address: 192.168.5.1', output)
        # options
        self.assertRegex(output, r'1 subnet mask *255\.255\.255\.0')
        self.assertRegex(output, r'3 router *192\.168\.5\.1')
        self.assertRegex(output, r'6 domain name server *192\.168\.5\.1\n *192\.168\.5\.7\n *192\.168\.5\.8')
        self.assertRegex(output, r'26 MTU size *1492')
        self.assertRegex(output, r'28 broadcast address *192\.168\.5\.255')
        self.assertRegex(output, r'51 lease time *2min')
        self.assertRegex(output, r'53 message type *5')
        self.assertRegex(output, r'54 server identifier *192\.168\.5\.1')
        self.assertRegex(output, r'119 domain search *foo\.example\.com')
        self.assertRegex(output, r'120 SIP server *192\.168\.5\.23\n *192\.168\.5\.24')
 
        self.check_netlabel('veth99', r'192\.168\.5\.0/24')
 
        self.check_nftset('addr4', r'192\.168\.5\.1')
        self.check_nftset('network4', r'192\.168\.5\.0/24')
        self.check_nftset('ifindex', 'veth99')
 
        # Check if DHCPv4 address and routes are removed on stop. For issue #34837.
        stop_networkd(show_logs=False)
        self.wait_address_dropped('veth99', f'inet {address2}/24', ipv='-4', timeout_sec=120)
 
        print('## ip address show dev veth99 scope global')
        output = check_output('ip address show dev veth99 scope global')
        print(output)
        self.assertNotIn(f'{address2}', output)
 
        print('## ip route show table main dev veth99')
        output = check_output('ip route show table main dev veth99')
        print(output)
        self.assertNotIn(f'{address2}', output)
 
        print('## ip route show table 211 dev veth99')
        output = check_output('ip route show table 211 dev veth99')
        print(output)
        self.assertNotIn(f'{address2}', output)
 
        print('## ip route show table 212 dev veth99')
        output = check_output('ip route show table 212 dev veth99')
        print(output)
        self.assertNotIn(f'{address2}', output)
 
        self.teardown_nftset('addr4', 'network4', 'ifindex')
 
    def _test_dhcp_client_send_release_one(self, stop=True) -> bool:
        start_dnsmasq(
            namespace='ns-server',
            interface='server',
            ipv4_range='192.0.2.100,192.0.2.109',
            ipv4_router='192.0.2.1',
        )
 
        start_networkd()
        self.wait_online('client:routable')
 
        print('## ip -4 address show dev client scope global')
        output = check_output('ip -4 address show dev client scope global')
        print(output)
        self.assertRegex(output, r'192.0.2.10[0-9]/24')
 
        print('## ip -4 route show dev client')
        output = check_output('ip -4 route show dev client')
        print(output)
        self.assertRegex(output, r'default via 192.0.2.1 proto dhcp src 192.0.2.10[0-9]')
        self.assertRegex(output, r'192.0.2.0/24 proto kernel scope link src 192.0.2.10[0-9]')
        self.assertRegex(output, r'192.0.2.1 proto dhcp scope link src 192.0.2.10[0-9]')
 
        if stop:
            stop_networkd()
        else:
            networkctl('down', 'client')
 
        success = False
        for _ in range(20):
            time.sleep(0.5)
            try:
                output = read_dnsmasq_log_file()
            except FileNotFoundError:
                output = ''
            if 'DHCPRELEASE' in output:
                success = True
                break
 
        print('## dnsmasq log')
        print(output)
 
        return success
 
    def test_dhcp_client_send_release(self):
        check_output('ip netns add ns-bridge')
        check_output('ip netns exec ns-bridge ip link add bridge99 type bridge')
        check_output('ip netns exec ns-bridge ip link set bridge99 address 12:34:56:78:90:ab')
        check_output('ip netns exec ns-bridge ip link set bridge99 up')
 
        check_output('ip link add client type veth peer clientp')
        check_output('ip link set clientp netns ns-bridge')
        check_output('ip netns exec ns-bridge ip link set clientp master bridge99')
        check_output('ip netns exec ns-bridge ip link set clientp up')
 
        check_output('ip link add server type veth peer serverp')
        check_output('ip link set serverp netns ns-bridge')
        check_output('ip netns exec ns-bridge ip link set serverp master bridge99')
        check_output('ip netns exec ns-bridge ip link set serverp up')
 
        check_output('ip netns add ns-server')
        check_output('ip link set server netns ns-server')
        check_output('ip netns exec ns-server ip link set server up')
        check_output('ip netns exec ns-server ip address add 192.0.2.1/24 dev server')
 
        copy_network_unit('25-dhcp-client-simple.network')
 
        '''
        Sending DHCPRELEASE is best-effort. Even if send() succeeds, the packet may be dropped later in the
        networking stack (e.g. due to unresolved neighbor state or interface teardown). Userspace cannot
        reliably determine whether the packet was eventually transmitted or dropped.
 
        Hence, the test below may be flaky. In most cases, neighbor resolution completes quickly enough and
        the packet is transmitted before the interface is brought down. Running the test multiple times
        should make it sufficiently reliable.
        '''
 
        first = True
        for _ in range(5):
            if not first:
                stop_dnsmasq()
                stop_networkd(show_logs=False)
 
            first = False
 
            if self._test_dhcp_client_send_release_one():
                break
        else:
            self.fail('Timed out waiting for DHCPRELEASE in dnsmasq log (on stopping networkd)')
 
        for _ in range(5):
            stop_dnsmasq()
            stop_networkd(show_logs=False)
 
            if self._test_dhcp_client_send_release_one(stop=False):
                break
        else:
            self.fail('Timed out waiting for DHCPRELEASE in dnsmasq log (on bringing down interface)')
 
    def test_dhcp_client_ipv4_dbus_status(self):
        copy_network_unit(
            '25-veth.netdev',
            '25-dhcp-server-veth-peer.network',
            '25-dhcp-client-ipv4-only.network',
        )
        start_networkd()
        self.wait_online('veth-peer:carrier')
 
        state = get_dhcp4_client_state('veth99')
        print(f'State = {state}')
        self.assertEqual(state, 'rebooting')
 
        start_dnsmasq(
            '--dhcp-option=option:dns-server,192.168.5.6,192.168.5.7',
            '--dhcp-option=option:domain-search,example.com',
            '--dhcp-alternate-port=67,5555',
            ipv4_range='192.168.5.110,192.168.5.119',
        )
        self.wait_online('veth99:routable', 'veth-peer:routable')
        self.wait_address('veth99', r'inet 192.168.5.11[0-9]*/24', ipv='-4')
 
        state = get_dhcp4_client_state('veth99')
        print(f'State = {state}')
        self.assertEqual(state, 'bound')
 
    def test_dhcp_client_allow_list(self):
        copy_network_unit(
            '25-veth.netdev',
            '25-dhcp-server-veth-peer.network',
            '25-dhcp-client-allow-list.network',
            copy_dropins=False,
        )
 
        start_networkd()
        self.wait_online('veth-peer:carrier')
        since = datetime.datetime.now()
        start_dnsmasq()
 
        self.check_networkd_log(
            'veth99: DHCPv4 server IP address 192.168.5.1 not found in allow-list, ignoring offer.',
            since=since,
        )
 
        copy_network_unit('25-dhcp-client-allow-list.network.d/00-allow-list.conf')
        since = datetime.datetime.now()
        networkctl_reload()
 
        self.check_networkd_log(
            'veth99: DHCPv4 server IP address 192.168.5.1 not found in allow-list, ignoring offer.',
            since=since,
        )
 
        copy_network_unit('25-dhcp-client-allow-list.network.d/10-deny-list.conf')
        since = datetime.datetime.now()
        networkctl_reload()
 
        self.check_networkd_log(
            'veth99: DHCPv4 server IP address 192.168.5.1 found in deny-list, ignoring offer.',
            since=since,
        )
 
    @unittest.skipUnless(
        '--dhcp-rapid-commit' in run('dnsmasq --help').stdout,
        reason='dnsmasq is missing dhcp-rapid-commit support',
    )
    def test_dhcp_client_rapid_commit(self):
        copy_network_unit('25-veth.netdev', '25-dhcp-server-veth-peer.network', '25-dhcp-client.network')
        start_networkd()
        self.wait_online('veth-peer:carrier')
 
        start_dnsmasq('--dhcp-rapid-commit')
        self.wait_online('veth99:routable', 'veth-peer:routable')
        self.wait_address('veth99', r'inet 192.168.5.[0-9]*/24', ipv='-4')
 
        state = get_dhcp4_client_state('veth99')
        print(f'DHCPv4 client state = {state}')
        self.assertEqual(state, 'bound')
 
        output = read_dnsmasq_log_file()
        self.assertIn('DHCPDISCOVER(veth-peer)', output)
        self.assertNotIn('DHCPOFFER(veth-peer)', output)
        self.assertNotIn('DHCPREQUEST(veth-peer)', output)
        self.assertIn('DHCPACK(veth-peer)', output)
 
    def check_bootp_client(self, check_log):
        self.wait_online('veth99:routable', 'veth-peer:routable')
        output = check_output('ip -4 address show dev veth99')
        print(output)
        self.assertRegex(output, r'inet 192.168.5.[0-9]*/24')
 
        state = get_dhcp4_client_state('veth99')
        print(f'DHCPv4 client state = {state}')
        self.assertEqual(state, 'bound')
 
        if check_log:
            output = read_dnsmasq_log_file()
            print(output)
            self.assertIn('BOOTP(veth-peer)', output)
            self.assertNotIn('DHCPDISCOVER(veth-peer)', output)
            self.assertNotIn('DHCPOFFER(veth-peer)', output)
            self.assertNotIn('DHCPREQUEST(veth-peer)', output)
            self.assertNotIn('DHCPACK(veth-peer)', output)
 
    def test_bootp_client(self):
        copy_network_unit('25-veth.netdev', '25-dhcp-server-veth-peer.network', '25-bootp-client.network')
        start_networkd()
        self.wait_online('veth-peer:carrier')
        start_dnsmasq('--dhcp-host=12:34:56:78:9a:bc,192.168.5.42,trixie-mule')
        self.check_bootp_client(check_log=True)
 
        touch_network_unit('25-bootp-client.network')
        networkctl_reload()
        self.check_bootp_client(check_log=True)
 
        with open(
            os.path.join(network_unit_dir, '25-bootp-client.network'), mode='a', encoding='utf-8'
        ) as f:
            f.write('[DHCPv4]\nBOOTP=no\n')
 
        networkctl_reload()
        self.check_bootp_client(check_log=False)
 
        output = read_dnsmasq_log_file()
        print(output)
        # Note, on reload, the DHCP client will be started from INIT-REBOOT state,
        # hence DISCOVER and OFFER message will not be sent/received.
        self.assertNotIn('DHCPDISCOVER(veth-peer)', output)
        self.assertNotIn('DHCPOFFER(veth-peer)', output)
        self.assertIn('DHCPREQUEST(veth-peer)', output)
        self.assertIn('DHCPACK(veth-peer)', output)
 
        with open(
            os.path.join(network_unit_dir, '25-bootp-client.network'), mode='a', encoding='utf-8'
        ) as f:
            f.write('[DHCPv4]\nBOOTP=yes\n')
 
        since = datetime.datetime.now()
 
        networkctl_reload()
        self.check_bootp_client(check_log=False)
 
        # Check if the client send RELEASE message of the previous lease
        self.check_networkd_log('veth99: DHCPv4 client: RELEASE', since=since)
 
    def test_dhcp_client_ipv6_only_mode_without_ipv6_connectivity(self):
        copy_network_unit(
            '25-veth.netdev',
            '25-dhcp-server-ipv6-only-mode.network',
            '25-dhcp-client-ipv6-only-mode.network',
        )
        start_networkd()
        self.wait_online('veth99:routable', 'veth-peer:routable', timeout='40s')
        self.wait_address('veth99', r'inet 192.168.5.[0-9]*/24', ipv='-4')
 
        state = get_dhcp4_client_state('veth99')
        print(f'State = {state}')
        self.assertEqual(state, 'bound')
 
    def test_dhcp_client_ipv4_use_routes_gateway(self):
        first = True
        for routes, gateway, dns_and_ntp_routes, classless in itertools.product([True, False], repeat=4):
            if first:
                first = False
            else:
                self.tearDown()
 
            # fmt: off
            print(f'### test_dhcp_client_ipv4_use_routes_gateway(routes={routes}, gateway={gateway}, dns_and_ntp_routes={dns_and_ntp_routes}, classless={classless})')
            with self.subTest(routes=routes, gateway=gateway, dns_and_ntp_routes=dns_and_ntp_routes, classless=classless):
                self._test_dhcp_client_ipv4_use_routes_gateway(routes, gateway, dns_and_ntp_routes, classless)
            # fmt: on
 
    def _test_dhcp_client_ipv4_use_routes_gateway(
        self, use_routes, use_gateway, dns_and_ntp_routes, classless
    ):
        testunit = '25-dhcp-client-ipv4-use-routes-use-gateway.network'
        testunits = ['25-veth.netdev', '25-dhcp-server-veth-peer.network', testunit]
        testunits.append(f'{testunit}.d/use-routes-{use_routes}.conf')
        testunits.append(f'{testunit}.d/use-gateway-{use_gateway}.conf')
        testunits.append(f'{testunit}.d/use-dns-and-ntp-routes-{dns_and_ntp_routes}.conf')
        copy_network_unit(*testunits, copy_dropins=False)
 
        start_networkd()
        self.wait_online('veth-peer:carrier')
        additional_options = [
            '--dhcp-option=option:dns-server,192.168.5.10,8.8.8.8',
            '--dhcp-option=option:ntp-server,192.168.5.11,9.9.9.9',
            '--dhcp-option=option:static-route,192.168.6.100,192.168.5.2,8.8.8.8,192.168.5.3',
        ]
        if classless:
            additional_options += [
                '--dhcp-option=option:classless-static-route,0.0.0.0/0,192.168.5.4,8.0.0.0/8,192.168.5.5,192.168.5.64/26,192.168.5.5'
            ]
        start_dnsmasq(*additional_options)
        self.wait_online('veth99:routable', 'veth-peer:routable')
 
        output = check_output('ip -4 route show dev veth99')
        print(output)
 
        # Check UseRoutes=
        # fmt: off
        if use_routes:
            if classless:
                self.assertRegex(output, r'default via 192.168.5.4 proto dhcp src 192.168.5.[0-9]* metric 1024')
                self.assertRegex(output, r'8.0.0.0/8 via 192.168.5.5 proto dhcp src 192.168.5.[0-9]* metric 1024')
                self.assertRegex(output, r'192.168.5.64/26 via 192.168.5.5 proto dhcp src 192.168.5.[0-9]* metric 1024')
                self.assertRegex(output, r'192.168.5.4 proto dhcp scope link src 192.168.5.[0-9]* metric 1024')
                self.assertRegex(output, r'192.168.5.5 proto dhcp scope link src 192.168.5.[0-9]* metric 1024')
            else:
                self.assertRegex(output, r'192.168.6.0/24 via 192.168.5.2 proto dhcp src 192.168.5.[0-9]* metric 1024')
                self.assertRegex(output, r'8.0.0.0/8 via 192.168.5.3 proto dhcp src 192.168.5.[0-9]* metric 1024')
                self.assertRegex(output, r'192.168.5.2 proto dhcp scope link src 192.168.5.[0-9]* metric 1024')
                self.assertRegex(output, r'192.168.5.3 proto dhcp scope link src 192.168.5.[0-9]* metric 1024')
        else:
            self.assertNotRegex(output, r'default via 192.168.5.4 proto dhcp src 192.168.5.[0-9]* metric 1024')
            self.assertNotRegex(output, r'8.0.0.0/8 via 192.168.5.5 proto dhcp src 192.168.5.[0-9]* metric 1024')
            self.assertNotRegex(output, r'192.168.5.4 proto dhcp scope link src 192.168.5.[0-9]* metric 1024')
            self.assertNotRegex(output, r'192.168.5.5 proto dhcp scope link src 192.168.5.[0-9]* metric 1024')
            self.assertNotRegex(output, r'192.168.6.0/24 via 192.168.5.2 proto dhcp src 192.168.5.[0-9]* metric 1024')
            self.assertNotRegex(output, r'8.0.0.0/8 via 192.168.5.3 proto dhcp src 192.168.5.[0-9]* metric 1024')
            self.assertNotRegex(output, r'192.168.5.2 proto dhcp scope link src 192.168.5.[0-9]* metric 1024')
            self.assertNotRegex(output, r'192.168.5.3 proto dhcp scope link src 192.168.5.[0-9]* metric 1024')
        # fmt: on
 
        # Check UseGateway=
        if use_gateway and (not classless or not use_routes):
            self.assertRegex(output, r'default via 192.168.5.1 proto dhcp src 192.168.5.[0-9]* metric 1024')
        else:
            self.assertNotRegex(output, r'default via 192.168.5.1 proto dhcp src 192.168.5.[0-9]* metric 1024')  # fmt: skip
 
        # Check route to gateway
        if (use_gateway or dns_and_ntp_routes) and (not classless or not use_routes):
            self.assertRegex(output, r'192.168.5.1 proto dhcp scope link src 192.168.5.[0-9]* metric 1024')
        else:
            self.assertNotRegex(output, r'192.168.5.1 proto dhcp scope link src 192.168.5.[0-9]* metric 1024')  # fmt: skip
 
        # Check RoutesToDNS= and RoutesToNTP=
        # fmt: off
        if dns_and_ntp_routes:
            self.assertRegex(output, r'192.168.5.10 proto dhcp scope link src 192.168.5.[0-9]* metric 1024')
            self.assertRegex(output, r'192.168.5.11 proto dhcp scope link src 192.168.5.[0-9]* metric 1024')
            if use_routes:
                if classless:
                    self.assertRegex(output, r'8.8.8.8 via 192.168.5.5 proto dhcp src 192.168.5.[0-9]* metric 1024')
                    self.assertRegex(output, r'9.9.9.9 via 192.168.5.4 proto dhcp src 192.168.5.[0-9]* metric 1024')
                else:
                    self.assertRegex(output, r'8.8.8.8 via 192.168.5.3 proto dhcp src 192.168.5.[0-9]* metric 1024')
                    self.assertRegex(output, r'9.9.9.9 via 192.168.5.1 proto dhcp src 192.168.5.[0-9]* metric 1024')
            else:
                self.assertRegex(output, r'8.8.8.8 via 192.168.5.1 proto dhcp src 192.168.5.[0-9]* metric 1024')
                self.assertRegex(output, r'9.9.9.9 via 192.168.5.1 proto dhcp src 192.168.5.[0-9]* metric 1024')
        else:
            self.assertNotRegex(output, r'192.168.5.10 proto dhcp scope link src 192.168.5.[0-9]* metric 1024')
            self.assertNotRegex(output, r'192.168.5.11 proto dhcp scope link src 192.168.5.[0-9]* metric 1024')
            self.assertNotRegex(output, r'8.8.8.8 via 192.168.5.[0-9]* proto dhcp src 192.168.5.[0-9]* metric 1024')
            self.assertNotRegex(output, r'9.9.9.9 via 192.168.5.[0-9]* proto dhcp src 192.168.5.[0-9]* metric 1024')
        # fmt: on
 
        check_json(networkctl_json())
 
    def test_dhcp_client_ipv4_rfc3021(self):
        copy_network_unit(
            '25-veth.netdev',
            '25-dhcp-server-veth-peer.network',
            '25-dhcp-client-ipv4-use-routes-use-gateway.network',
            copy_dropins=False,
        )
 
        start_networkd()
        self.wait_online('veth-peer:carrier')
        # The lower address of a /31 network is a valid host address, see RFC 3021. Announce a classless
        # static route for a larger network whose base address is the same as the acquired address.
        start_dnsmasq(
            '--dhcp-host=12:34:56:78:9a:bc,192.168.5.0',
            '--dhcp-option=option:classless-static-route,192.168.5.0/25,192.168.5.1',
            ipv4_range='192.168.5.0,192.168.5.1,255.255.255.254',
        )
        self.wait_online('veth99:routable', 'veth-peer:routable')
 
        output = check_output('ip -4 address show dev veth99')
        print(output)
        self.assertIn('inet 192.168.5.0/31', output)
 
        output = check_output('ip -4 route show dev veth99')
        print(output)
        # The acquired address only shadows a host route to itself, hence the gateway must be honored.
        self.assertIn('192.168.5.0/25 via 192.168.5.1 proto dhcp src 192.168.5.0 metric 1024', output)
        self.assertIn('192.168.5.1 proto dhcp scope link src 192.168.5.0 metric 1024', output)
        self.assertNotIn('192.168.5.0/25 proto dhcp scope host', output)
 
        check_json(networkctl_json())
 
    def test_dhcp_client_settings_anonymize(self):
        copy_network_unit(
            '25-veth.netdev',
            '25-dhcp-server-veth-peer.network',
            '25-dhcp-client-anonymize.network',
        )
        start_networkd()
        self.wait_online('veth-peer:carrier')
        start_dnsmasq()
        self.wait_online('veth99:routable', 'veth-peer:routable')
 
        print('## dnsmasq log')
        output = read_dnsmasq_log_file()
        print(output)
        self.assertNotIn('VendorClassIdentifier=SusantVendorTest', output)
        self.assertNotIn('test-hostname', output)
        self.assertNotIn('26:mtu', output)
 
    def test_dhcp_keep_configuration_dynamic(self):
        copy_network_unit(
            '25-veth.netdev',
            '25-dhcp-server-veth-peer.network',
            '25-dhcp-client-keep-configuration-dynamic.network',
        )
        start_networkd()
        self.wait_online('veth-peer:carrier')
        start_dnsmasq()
        self.wait_online('veth99:routable', 'veth-peer:routable')
 
        output = check_output('ip address show dev veth99 scope global')
        print(output)
        self.assertRegex(output, r'inet 192.168.5.[0-9]*/24 metric 1024 brd 192.168.5.255 scope global veth99\n *valid_lft forever preferred_lft forever')  # fmt: skip
 
        # Stopping dnsmasq as networkd won't be allowed to renew the DHCP lease.
        stop_dnsmasq()
 
        # Sleep for 120 sec as the dnsmasq minimum lease time can only be set to 120
        print('Wait for the DHCP lease to be expired')
        time.sleep(120)
 
        # The lease address should be kept after the lease expired
        output = check_output('ip address show dev veth99 scope global')
        print(output)
        self.assertRegex(output, r'inet 192.168.5.[0-9]*/24 metric 1024 brd 192.168.5.255 scope global veth99\n *valid_lft forever preferred_lft forever')  # fmt: skip
 
        stop_networkd()
 
        # The lease address should be kept after networkd stopped
        output = check_output('ip address show dev veth99 scope global')
        print(output)
        self.assertRegex(output, r'inet 192.168.5.[0-9]*/24 metric 1024 brd 192.168.5.255 scope global veth99\n *valid_lft forever preferred_lft forever')  # fmt: skip
 
        with open(
            os.path.join(network_unit_dir, '25-dhcp-client-keep-configuration-dynamic.network'),
            mode='a',
            encoding='utf-8',
        ) as f:
            f.write('[Network]\nDHCP=no\n')
 
        start_networkd()
        self.wait_online('veth99:routable', 'veth-peer:routable')
 
        # Still the lease address should be kept after networkd restarted
        output = check_output('ip address show dev veth99 scope global')
        print(output)
        self.assertRegex(output, r'inet 192.168.5.[0-9]*/24 metric 1024 brd 192.168.5.255 scope global veth99\n *valid_lft forever preferred_lft forever')  # fmt: skip
 
    def test_dhcp_keep_configuration_dynamic_on_stop(self):
        copy_network_unit(
            '25-veth.netdev',
            '25-dhcp-server-veth-peer.network',
            '25-dhcp-client-keep-configuration-dynamic-on-stop.network',
        )
        start_networkd()
        self.wait_online('veth-peer:carrier')
        start_dnsmasq()
        self.wait_online('veth99:routable', 'veth-peer:routable')
 
        output = check_output('ip address show dev veth99 scope global')
        print(output)
        self.assertRegex(output, r'inet 192.168.5.[0-9]*/24 metric 1024 brd 192.168.5.255 scope global dynamic veth99')  # fmt: skip
 
        stop_dnsmasq()
        stop_networkd()
 
        output = check_output('ip address show dev veth99 scope global')
        print(output)
        self.assertRegex(output, r'inet 192.168.5.[0-9]*/24 metric 1024 brd 192.168.5.255 scope global dynamic veth99')  # fmt: skip
 
        start_networkd()
        self.wait_online('veth-peer:routable')
 
        output = check_output('ip address show dev veth99 scope global')
        print(output)
        self.assertNotIn('192.168.5.', output)
 
    def test_dhcp_client_reuse_address_as_static(self):
        copy_network_unit('25-veth.netdev', '25-dhcp-server-veth-peer.network', '25-dhcp-client.network')
        start_networkd()
        self.wait_online('veth-peer:carrier')
        start_dnsmasq()
        self.wait_online('veth99:routable', 'veth-peer:routable')
 
        # link become 'routable' when at least one protocol provide an valid address.
        self.wait_address(
            'veth99',
            r'inet 192.168.5.[0-9]*/24 metric 1024 brd 192.168.5.255 scope global dynamic',
            ipv='-4',
        )
        self.wait_address(
            'veth99',
            r'inet6 2600::[0-9a-f]*/128 scope global (dynamic noprefixroute|noprefixroute dynamic)',
            ipv='-6',
        )
 
        output = check_output('ip address show dev veth99 scope global')
        ipv4_address = re.search(r'192.168.5.[0-9]*/24', output).group()
        ipv6_address = re.search(r'2600::[0-9a-f:]*/128', output).group()
        static_network = f'''
[Match]
Name=veth99
[Network]
IPv6AcceptRA=no
Address={ipv4_address}
Address={ipv6_address}
'''
        print(static_network)
 
        remove_network_unit('25-dhcp-client.network')
 
        with open(os.path.join(network_unit_dir, '25-static.network'), mode='w', encoding='utf-8') as f:
            f.write(static_network)
 
        restart_networkd()
        self.wait_online('veth99:routable')
 
        output = check_output('ip -4 address show dev veth99 scope global')
        print(output)
        self.assertRegex(output, f'inet {ipv4_address} brd 192.168.5.255 scope global veth99\n *valid_lft forever preferred_lft forever')  # fmt: skip
 
        output = check_output('ip -6 address show dev veth99 scope global')
        print(output)
        self.assertRegex(output, f'inet6 {ipv6_address} scope global *\n *valid_lft forever preferred_lft forever')  # fmt: skip
 
    @expectedFailureIfModuleIsNotAvailable('vrf')
    def test_dhcp_client_vrf(self):
        copy_network_unit(
            '25-veth.netdev',
            '25-dhcp-server-veth-peer.network',
            '25-dhcp-client-vrf.network',
            '25-vrf.netdev',
            '25-vrf.network',
        )
        start_networkd()
        self.wait_online('veth-peer:carrier')
        start_dnsmasq()
        self.wait_online('veth99:routable', 'veth-peer:routable', 'vrf99:carrier')
 
        # link become 'routable' when at least one protocol provide an valid address.
        self.wait_address(
            'veth99',
            r'inet 192.168.5.[0-9]*/24 metric 1024 brd 192.168.5.255 scope global dynamic',
            ipv='-4',
        )
        self.wait_address(
            'veth99',
            r'inet6 2600::[0-9a-f]*/128 scope global (dynamic noprefixroute|noprefixroute dynamic)',
            ipv='-6',
        )
 
        print('## ip -d link show dev vrf99')
        output = check_output('ip -d link show dev vrf99')
        print(output)
        self.assertRegex(output, 'vrf table 42')
 
        print('## ip address show vrf vrf99')
        output = check_output('ip address show vrf vrf99')
        print(output)
        # fmt: off
        self.assertRegex(output, 'inet 192.168.5.[0-9]*/24 metric 1024 brd 192.168.5.255 scope global dynamic veth99')
        self.assertRegex(output, 'inet6 2600::[0-9a-f]*/128 scope global (dynamic noprefixroute|noprefixroute dynamic)')
        self.assertRegex(output, 'inet6 .* scope link')
        # fmt: on
 
        print('## ip address show dev veth99')
        output = check_output('ip address show dev veth99')
        print(output)
        # fmt: off
        self.assertRegex(output, 'inet 192.168.5.[0-9]*/24 metric 1024 brd 192.168.5.255 scope global dynamic veth99')
        self.assertRegex(output, 'inet6 2600::[0-9a-f]*/128 scope global (dynamic noprefixroute|noprefixroute dynamic)')
        self.assertRegex(output, 'inet6 .* scope link')
        # fmt: on
 
        print('## ip route show vrf vrf99')
        output = check_output('ip route show vrf vrf99')
        print(output)
        self.assertRegex(output, 'default via 192.168.5.1 dev veth99 proto dhcp src 192.168.5.')
        self.assertRegex(output, '192.168.5.0/24 dev veth99 proto kernel scope link src 192.168.5')
        self.assertRegex(output, '192.168.5.1 dev veth99 proto dhcp scope link src 192.168.5')
 
        print('## ip route show table main dev veth99')
        output = check_output('ip route show table main dev veth99')
        print(output)
        self.assertEqual(output, '')
 
    def test_dhcp_client_gateway_onlink_implicit(self):
        copy_network_unit(
            '25-veth.netdev',
            '25-dhcp-server-veth-peer.network',
            '25-dhcp-client-gateway-onlink-implicit.network',
        )
        start_networkd()
        self.wait_online('veth-peer:carrier')
        start_dnsmasq()
        self.wait_online('veth99:routable', 'veth-peer:routable')
 
        output = networkctl_status('veth99')
        print(output)
        self.assertRegex(output, '192.168.5')
 
        output = check_output('ip route list dev veth99 10.0.0.0/8')
        print(output)
        self.assertRegex(output, 'onlink')
        output = check_output('ip route list dev veth99 192.168.100.0/24')
        print(output)
        self.assertRegex(output, 'onlink')
 
    def test_dhcp_client_with_ipv4ll(self):
        copy_network_unit(
            '25-veth.netdev',
            '25-dhcp-server-veth-peer.network',
            '25-dhcp-client-with-ipv4ll.network',
        )
        start_networkd()
        # we need to increase timeout above default, as this will need to wait for
        # systemd-networkd to get the dhcpv4 transient failure event
        self.wait_online('veth99:degraded', 'veth-peer:routable', timeout='60s')
 
        output = check_output('ip -4 address show dev veth99')
        print(output)