xref: /linux/tools/testing/selftests/net/rtnetlink.py (revision b693b51e0829b96a5c43f45c3fba3d11f6f09d2f)
1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0
3
4import socket
5import time
6from lib.py import bkg, ip, ksft_exit, ksft_run, ksft_ge, ksft_true, KsftSkipEx
7from lib.py import CmdExitFailure, NetNS, NetNSEnter, RtnlAddrFamily
8
9IPV4_ALL_HOSTS_MULTICAST = b'\xe0\x00\x00\x01'
10
11def dump_mcaddr_check() -> None:
12    """
13    Verify that at least one interface has the IPv4 all-hosts multicast address.
14    At least the loopback interface should have this address.
15    """
16
17    rtnl = RtnlAddrFamily()
18    addresses = rtnl.getmulticast({"ifa-family": socket.AF_INET}, dump=True)
19
20    all_host_multicasts = [
21        addr for addr in addresses if addr['multicast'] == IPV4_ALL_HOSTS_MULTICAST
22    ]
23
24    ksft_ge(len(all_host_multicasts), 1,
25            "No interface found with the IPv4 all-hosts multicast address")
26
27def ipv4_devconf_notify() -> None:
28    """
29    Configure an interface and set ipv4-devconf values through netlink
30    to verify that the appropriate netlink notifications are being sent.
31    """
32
33    with NetNS() as ns:
34        with NetNSEnter(str(ns)):
35            ifname = "dummy1"
36            ip(f"link add name {ifname} type dummy", ns=str(ns))
37
38            with bkg("ip monitor", ns=str(ns)) as cmd_obj:
39                time.sleep(1)
40                try:
41                    ip(f"link set dev {ifname} inet forwarding on")
42                    ip(f"link set dev {ifname} inet proxy_arp on")
43                    ip(f"link set dev {ifname} inet rp_filter 1")
44                    ip(f"link set dev {ifname} inet ignore_routes_with_linkdown on")
45                except CmdExitFailure:
46                    raise KsftSkipEx("iproute2 does not support IPv4 devconf attributes")
47                time.sleep(1)
48
49    ksft_true(f"inet {ifname} ignore_routes_with_linkdown on" in cmd_obj.stdout,
50              f"No 'ignore_routes_with_linkdown on' notificiation found for interface {ifname}")
51    ksft_true(f"inet {ifname} rp_filter strict" in cmd_obj.stdout,
52              f"No 'rp_filter strict' notificiation found for interface {ifname}")
53    ksft_true(f"inet {ifname} proxy_neigh on" in cmd_obj.stdout,
54              f"No 'proxy_neigh on' notificiation found for interface {ifname}")
55    ksft_true(f"inet {ifname} forwarding on" in cmd_obj.stdout,
56              f"No 'forwarding on' notificiation found for interface {ifname}")
57
58def main() -> None:
59    ksft_run([dump_mcaddr_check, ipv4_devconf_notify])
60    ksft_exit()
61
62if __name__ == "__main__":
63    main()
64