Skip to contentSkip to conversation

iproute2: expose netlink constants in UAPI

PATCH Superseded Yuyang Huang 2 participants 3 messages Dec 6, 2024

Subsystems (2)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 iproute2: expose netlink constants in UAPI

This change adds the following multicast related netlink constants to the UAPI:

* RTNLGRP_IPV4_MCADDR and RTNLGRP_IPV6_MCADDR: Netlink multicast groups for IPv4 and IPv6 multicast address changes.
* RTM_NEWMULTICAST and RTM_DELMULTICAST: Netlink message types for multicast address additions and deletions.

Exposing these constants in the UAPI enables ip monitor to effectively monitor and manage multicast group memberships.

Cc Maciej Żenczykowski <[email protected]>
Cc Lorenzo Colitti <[email protected]>

---

Changelog since v2:
- Align RTM_NEWMULTICAST and RTM_GETMULTICAST enum definitions with existing code style.

 include/uapi/linux/rtnetlink.h | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
include/uapi/linux/rtnetlink.h +9 -1
index 4e6c8e14..04be20ee 100644
@@ -93,7 +93,11 @@ enum {
 	RTM_NEWPREFIX	= 52,
 #define RTM_NEWPREFIX	RTM_NEWPREFIX
 
-	RTM_GETMULTICAST = 58,
+	RTM_NEWMULTICAST = 56,
+#define RTM_NEWMULTICAST RTM_NEWMULTICAST
+	RTM_DELMULTICAST,
+#define RTM_DELMULTICAST RTM_DELMULTICAST
+	RTM_GETMULTICAST,
 #define RTM_GETMULTICAST RTM_GETMULTICAST
 
 	RTM_GETANYCAST	= 62,
@@ -772,6 +776,10 @@ enum rtnetlink_groups {
 #define RTNLGRP_TUNNEL		RTNLGRP_TUNNEL
 	RTNLGRP_STATS,
 #define RTNLGRP_STATS		RTNLGRP_STATS
+	RTNLGRP_IPV4_MCADDR,
+#define RTNLGRP_IPV4_MCADDR	RTNLGRP_IPV4_MCADDR
+	RTNLGRP_IPV6_MCADDR,
+#define RTNLGRP_IPV6_MCADDR    RTNLGRP_IPV6_MCADDR
 	__RTNLGRP_MAX
 };
 #define RTNLGRP_MAX	(__RTNLGRP_MAX - 1)
Signature · 1 line
2.47.0.338.g60cca15819-goog
#1 lore raw

PATCH v4 2/2 iproute2: add 'ip monitor maddr' 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 maddr
8: nettest123    inet6 mcast ff01::1 scope global
8: nettest123    inet6 mcast ff02::1 scope global
8: nettest123    inet mcast 224.0.0.1 scope global
8: nettest123    inet6 mcast ff02::1:ff00:7b01 scope global
Deleted 8: nettest123    inet mcast 224.0.0.1 scope global
Deleted 8: nettest123    inet6 mcast ff02::1:ff00:7b01 scope global
Deleted 8: nettest123    inet6 mcast ff02::1 scope global
Cc Maciej Żenczykowski <[email protected]>
Cc Lorenzo Colitti <[email protected]>

---

Changelog since v3:
- Update man/man8/ip-monitor.8 page.
- Use 'ip monitor maddr' for naming consistency with 'ip maddr' command.

Changelog since v1:
- Move the UAPI constants to a separate patch.
- Update the commit message.
- Fix the indentation format.

 ip/ipaddress.c        | 17 +++++++++++++++--
 ip/ipmonitor.c        | 25 ++++++++++++++++++++++++-
 man/man8/ip-monitor.8 |  2 +-
 3 files changed, 40 insertions(+), 4 deletions(-)
ip/ipaddress.c +15 -2
index d90ba94d..373f613f 100644
@@ -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 ",
ip/ipmonitor.c +24 -1
index de67f2c9..beefba4a 100644
@@ -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 | maddr | 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)
 
@@ -220,6 +226,8 @@ int do_ipmonitor(int argc, char **argv)
 			lmask |= IPMON_LNEXTHOP;
 		} else if (strcmp(*argv, "stats") == 0) {
 			lmask |= IPMON_LSTATS;
+		} else if (strcmp(*argv, "maddr") == 0) {
+			lmask |= IPMON_LMADDR;
 		} else if (strcmp(*argv, "all") == 0) {
 			prefix_banner = 1;
 		} else if (matches(*argv, "all-nsid") == 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);
 
man/man8/ip-monitor.8 +1 -1
index ec033c69..076e4a11 100644
@@ -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 ", " maddr ", " prefix ", "
 .BR neigh ", " netconf ", "  rule ", " stats ", " nsid " and " nexthop "."
 If no
 .B file
Signature · 1 line
2.47.0.338.g60cca15819-goog
#2 lore raw

Re: [PATCH iproute2-next, v4 2/2] iproute2: add 'ip monitor maddr' support

Le 06/12/2024 à 13:45, Yuyang Huang a écrit :

Quoted 44 lines

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 maddr
8: nettest123    inet6 mcast ff01::1 scope global
8: nettest123    inet6 mcast ff02::1 scope global
8: nettest123    inet mcast 224.0.0.1 scope global
8: nettest123    inet6 mcast ff02::1:ff00:7b01 scope global
Deleted 8: nettest123    inet mcast 224.0.0.1 scope global
Deleted 8: nettest123    inet6 mcast ff02::1:ff00:7b01 scope global
Deleted 8: nettest123    inet6 mcast ff02::1 scope global
Cc Maciej Żenczykowski <[email protected]>
Cc Lorenzo Colitti <[email protected]>

---

Changelog since v3:
- Update man/man8/ip-monitor.8 page.
- Use 'ip monitor maddr' for naming consistency with 'ip maddr' command.

Changelog since v1:
- Move the UAPI constants to a separate patch.
- Update the commit message.
- Fix the indentation format.

 ip/ipaddress.c        | 17 +++++++++++++++--
 ip/ipmonitor.c        | 25 ++++++++++++++++++++++++-
 man/man8/ip-monitor.8 |  2 +-
 3 files changed, 40 insertions(+), 4 deletions(-)
ip/ipaddress.c +0 -0
index d90ba94d..373f613f 100644

ip/ipaddress.c int print_addrinfo(struct nlmsghdr *n, void *arg) · line 1510

Quoted 8 lines
ip/ipaddress.c +4 -1
@@ -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)

We usually put the boolean operator at the end of the line.

Quoted 34 lines
ip/ipaddress.c +11 -1
 		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 ",
ip/ipmonitor.c +0 -0
index de67f2c9..beefba4a 100644

ip/ipmonitor.c static void usage(void) · line 33

Quoted 5 lines
ip/ipmonitor.c +1 -1
@@ -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 | maddr | neigh | netconf |\n"

I was probably not clear. Please use 'maddress', like the existing one.

Quoted 24 lines
ip/ipmonitor.c +6 -0
 		"            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)
 

ip/ipmonitor.c int do_ipmonitor(int argc, char **argv) · line 230

Quoted 5 lines
ip/ipmonitor.c +2 -0
@@ -220,6 +226,8 @@ int do_ipmonitor(int argc, char **argv)
 			lmask |= IPMON_LNEXTHOP;
 		} else if (strcmp(*argv, "stats") == 0) {
 			lmask |= IPMON_LSTATS;
+		} else if (strcmp(*argv, "maddr") == 0) {
+			lmask |= IPMON_LMADDR;

And here, youc an use 'matches(*argv, "maddress")' so that it will be consistent with
https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/tree/ip/ip.c?h=v6.12.0#n92

Note that it should be put after the '} else if (matches(*argv, "address") == 0) {' block also.

Regards,
Nicolas