Skip to content

Commit 1951460

Browse files
Yuyang Huangdsahern
authored andcommitted
iproute2: add 'ip monitor maddress' support
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>
1 parent 9336031 commit 1951460

3 files changed

Lines changed: 40 additions & 4 deletions

File tree

ip/ipaddress.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,10 @@ int print_addrinfo(struct nlmsghdr *n, void *arg)
15041504

15051505
SPRINT_BUF(b1);
15061506

1507-
if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
1507+
if (n->nlmsg_type != RTM_NEWADDR &&
1508+
n->nlmsg_type != RTM_DELADDR &&
1509+
n->nlmsg_type != RTM_NEWMULTICAST &&
1510+
n->nlmsg_type != RTM_DELMULTICAST)
15081511
return 0;
15091512
len -= NLMSG_LENGTH(sizeof(*ifa));
15101513
if (len < 0) {
@@ -1564,7 +1567,7 @@ int print_addrinfo(struct nlmsghdr *n, void *arg)
15641567

15651568
print_headers(fp, "[ADDR]");
15661569

1567-
if (n->nlmsg_type == RTM_DELADDR)
1570+
if (n->nlmsg_type == RTM_DELADDR || n->nlmsg_type == RTM_DELMULTICAST)
15681571
print_bool(PRINT_ANY, "deleted", "Deleted ", true);
15691572

15701573
if (!brief) {
@@ -1639,6 +1642,16 @@ int print_addrinfo(struct nlmsghdr *n, void *arg)
16391642
rta_tb[IFA_ANYCAST]));
16401643
}
16411644

1645+
if (rta_tb[IFA_MULTICAST]) {
1646+
print_string(PRINT_FP, NULL, "%s ", "mcast");
1647+
print_color_string(PRINT_ANY,
1648+
ifa_family_color(ifa->ifa_family),
1649+
"multicast",
1650+
"%s ",
1651+
format_host_rta(ifa->ifa_family,
1652+
rta_tb[IFA_MULTICAST]));
1653+
}
1654+
16421655
print_string(PRINT_ANY,
16431656
"scope",
16441657
"scope %s ",

ip/ipmonitor.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static void usage(void)
3030
fprintf(stderr,
3131
"Usage: ip monitor [ all | OBJECTS ] [ FILE ] [ label ] [ all-nsid ]\n"
3232
" [ dev DEVICE ]\n"
33-
"OBJECTS := address | link | mroute | neigh | netconf |\n"
33+
"OBJECTS := address | link | mroute | maddress | neigh | netconf |\n"
3434
" nexthop | nsid | prefix | route | rule | stats\n"
3535
"FILE := file FILENAME\n");
3636
exit(-1);
@@ -152,6 +152,11 @@ static int accept_msg(struct rtnl_ctrl_data *ctrl,
152152
ipstats_print(n, arg);
153153
return 0;
154154

155+
case RTM_DELMULTICAST:
156+
case RTM_NEWMULTICAST:
157+
print_addrinfo(n, arg);
158+
return 0;
159+
155160
case NLMSG_ERROR:
156161
case NLMSG_NOOP:
157162
case NLMSG_DONE:
@@ -178,6 +183,7 @@ static int accept_msg(struct rtnl_ctrl_data *ctrl,
178183
#define IPMON_LRULE BIT(8)
179184
#define IPMON_LNSID BIT(9)
180185
#define IPMON_LNEXTHOP BIT(10)
186+
#define IPMON_LMADDR BIT(11)
181187

182188
#define IPMON_L_ALL (~0)
183189

@@ -202,6 +208,8 @@ int do_ipmonitor(int argc, char **argv)
202208
lmask |= IPMON_LLINK;
203209
} else if (matches(*argv, "address") == 0) {
204210
lmask |= IPMON_LADDR;
211+
} else if (matches(*argv, "maddress") == 0) {
212+
lmask |= IPMON_LMADDR;
205213
} else if (matches(*argv, "route") == 0) {
206214
lmask |= IPMON_LROUTE;
207215
} else if (matches(*argv, "mroute") == 0) {
@@ -326,6 +334,21 @@ int do_ipmonitor(int argc, char **argv)
326334
exit(1);
327335
}
328336

337+
if (lmask & IPMON_LMADDR) {
338+
if ((!preferred_family || preferred_family == AF_INET) &&
339+
rtnl_add_nl_group(&rth, RTNLGRP_IPV4_MCADDR) < 0) {
340+
fprintf(stderr,
341+
"Failed to add ipv4 mcaddr group to list\n");
342+
exit(1);
343+
}
344+
if ((!preferred_family || preferred_family == AF_INET6) &&
345+
rtnl_add_nl_group(&rth, RTNLGRP_IPV6_MCADDR) < 0) {
346+
fprintf(stderr,
347+
"Failed to add ipv6 mcaddr group to list\n");
348+
exit(1);
349+
}
350+
}
351+
329352
if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0)
330353
exit(1);
331354

man/man8/ip-monitor.8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ command is the first in the command line and then the object list follows:
5454
.I OBJECT-LIST
5555
is the list of object types that we want to monitor.
5656
It may contain
57-
.BR link ", " address ", " route ", " mroute ", " prefix ", "
57+
.BR link ", " address ", " route ", " mroute ", " maddress ", " prefix ", "
5858
.BR neigh ", " netconf ", " rule ", " stats ", " nsid " and " nexthop "."
5959
If no
6060
.B file

0 commit comments

Comments
 (0)