]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
iproute2: add 'ip monitor maddress' support
authorYuyang Huang <yuyanghuang@google.com>
Wed, 11 Dec 2024 08:24:53 +0000 (17:24 +0900)
committerDavid Ahern <dsahern@kernel.org>
Mon, 16 Dec 2024 03:04:22 +0000 (03:04 +0000)
Enhanced the 'ip monitor' command to track changes in IPv4 and IPv6
multicast addresses. This update allows the command to listen for
events related to multicast address additions and deletions by
registering to the newly introduced RTNLGRP_IPV4_MCADDR and
RTNLGRP_IPV6_MCADDR netlink groups.

This patch depends on the kernel patch that adds RTNLGRP_IPV4_MCADDR
and RTNLGRP_IPV6_MCADDR being merged first.

Here is an example usage:

root@uml-x86-64:/# ip monitor maddress
9: nettest123    inet6 mcast ff01::1 scope global
       valid_lft forever preferred_lft forever
9: nettest123    inet6 mcast ff02::1 scope global
       valid_lft forever preferred_lft forever
9: nettest123    inet mcast 224.0.0.1 scope global
       valid_lft forever preferred_lft forever
9: nettest123    inet6 mcast ff02::1:ff00:7b01 scope global
       valid_lft forever preferred_lft forever
Deleted 9: nettest123    inet mcast 224.0.0.1 scope global
       valid_lft forever preferred_lft forever
Deleted 9: nettest123    inet6 mcast ff02::1:ff00:7b01 scope global
       valid_lft forever preferred_lft forever
Deleted 9: nettest123    inet6 mcast ff02::1 scope global
       valid_lft forever preferred_lft forever

Cc: Maciej Żenczykowski <maze@google.com>
Cc: Lorenzo Colitti <lorenzo@google.com>
Signed-off-by: Yuyang Huang <yuyanghuang@google.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
ip/ipaddress.c
ip/ipmonitor.c
man/man8/ip-monitor.8

index d90ba94d11c557c7e5fbc544a4524b8f93aef920..679b4c002e127d4a4404cc8021c6c2d2c547295f 100644 (file)
@@ -1504,7 +1504,10 @@ int print_addrinfo(struct nlmsghdr *n, void *arg)
 
        SPRINT_BUF(b1);
 
-       if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
+       if (n->nlmsg_type != RTM_NEWADDR &&
+           n->nlmsg_type != RTM_DELADDR &&
+           n->nlmsg_type != RTM_NEWMULTICAST &&
+           n->nlmsg_type != RTM_DELMULTICAST)
                return 0;
        len -= NLMSG_LENGTH(sizeof(*ifa));
        if (len < 0) {
@@ -1564,7 +1567,7 @@ int print_addrinfo(struct nlmsghdr *n, void *arg)
 
        print_headers(fp, "[ADDR]");
 
-       if (n->nlmsg_type == RTM_DELADDR)
+       if (n->nlmsg_type == RTM_DELADDR || n->nlmsg_type == RTM_DELMULTICAST)
                print_bool(PRINT_ANY, "deleted", "Deleted ", true);
 
        if (!brief) {
@@ -1639,6 +1642,16 @@ int print_addrinfo(struct nlmsghdr *n, void *arg)
                                                   rta_tb[IFA_ANYCAST]));
        }
 
+       if (rta_tb[IFA_MULTICAST]) {
+               print_string(PRINT_FP, NULL, "%s ", "mcast");
+               print_color_string(PRINT_ANY,
+                                  ifa_family_color(ifa->ifa_family),
+                                  "multicast",
+                                  "%s ",
+                                  format_host_rta(ifa->ifa_family,
+                                                  rta_tb[IFA_MULTICAST]));
+       }
+
        print_string(PRINT_ANY,
                     "scope",
                     "scope %s ",
index de67f2c90cbd55097433fa1d5aced4bd49b61826..b28faa208dcb96a80f18727999706aa12b62ba15 100644 (file)
@@ -30,7 +30,7 @@ static void usage(void)
        fprintf(stderr,
                "Usage: ip monitor [ all | OBJECTS ] [ FILE ] [ label ] [ all-nsid ]\n"
                "                  [ dev DEVICE ]\n"
-               "OBJECTS :=  address | link | mroute | neigh | netconf |\n"
+               "OBJECTS :=  address | link | mroute | maddress | neigh | netconf |\n"
                "            nexthop | nsid | prefix | route | rule | stats\n"
                "FILE := file FILENAME\n");
        exit(-1);
@@ -152,6 +152,11 @@ static int accept_msg(struct rtnl_ctrl_data *ctrl,
                ipstats_print(n, arg);
                return 0;
 
+       case RTM_DELMULTICAST:
+       case RTM_NEWMULTICAST:
+               print_addrinfo(n, arg);
+               return 0;
+
        case NLMSG_ERROR:
        case NLMSG_NOOP:
        case NLMSG_DONE:
@@ -178,6 +183,7 @@ static int accept_msg(struct rtnl_ctrl_data *ctrl,
 #define IPMON_LRULE            BIT(8)
 #define IPMON_LNSID            BIT(9)
 #define IPMON_LNEXTHOP         BIT(10)
+#define IPMON_LMADDR           BIT(11)
 
 #define IPMON_L_ALL            (~0)
 
@@ -202,6 +208,8 @@ int do_ipmonitor(int argc, char **argv)
                        lmask |= IPMON_LLINK;
                } else if (matches(*argv, "address") == 0) {
                        lmask |= IPMON_LADDR;
+               } else if (matches(*argv, "maddress") == 0) {
+                       lmask |= IPMON_LMADDR;
                } else if (matches(*argv, "route") == 0) {
                        lmask |= IPMON_LROUTE;
                } else if (matches(*argv, "mroute") == 0) {
@@ -326,6 +334,21 @@ int do_ipmonitor(int argc, char **argv)
                exit(1);
        }
 
+       if (lmask & IPMON_LMADDR) {
+               if ((!preferred_family || preferred_family == AF_INET) &&
+                   rtnl_add_nl_group(&rth, RTNLGRP_IPV4_MCADDR) < 0) {
+                       fprintf(stderr,
+                               "Failed to add ipv4 mcaddr group to list\n");
+                       exit(1);
+               }
+               if ((!preferred_family || preferred_family == AF_INET6) &&
+                   rtnl_add_nl_group(&rth, RTNLGRP_IPV6_MCADDR) < 0) {
+                       fprintf(stderr,
+                               "Failed to add ipv6 mcaddr group to list\n");
+                       exit(1);
+               }
+       }
+
        if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0)
                exit(1);
 
index ec033c691d6d696c6155b86d3786f09a4bd30447..a3c099aeb2fad6a6b7ca1538648717e37598a736 100644 (file)
@@ -54,7 +54,7 @@ command is the first in the command line and then the object list follows:
 .I OBJECT-LIST
 is the list of object types that we want to monitor.
 It may contain
-.BR link ", " address ", " route ", " mroute ", " prefix ", "
+.BR link ", " address ", " route ", " mroute ", " maddress ", " prefix ", "
 .BR neigh ", " netconf ", "  rule ", " stats ", " nsid " and " nexthop "."
 If no
 .B file