ipmaddr: use RTM_GETMULTICAST to list multicast addresses
Subsystems (3)
PATCH v2 1/1 ipmaddr: use RTM_GETMULTICAST to list multicast addresses
Replace /proc/net/igmp and /proc/net/igmp6 parsing in "ip maddr show" with RTM_GETMULTICAST dumps. The kernel dumps IPv6 multicast addresses via netlink since the beginning, IPv4 since v6.15 (eb4e17a1d915), and reports the group users count via IFA_MC_USERS since kernel commits 7cb8198761e6 and e1d0f3f08391.
The netlink result is only used when it carries the same information as procfs: if the dump fails (e.g. no IPv4 dump support) or any entry lacks IFA_MC_USERS, the result is discarded and the procfs parsers run as before, so output is unchanged on older kernels.
Link-layer multicast addresses are still read from /proc/net/dev_mcast as there is no netlink API for them.
When a device is given, its ifindex is passed in the dump request so strict-check kernels filter the dump server side; received entries are checked against the ifindex again for kernels that ignore the request field. An unknown device keeps printing an empty list.
---
change in v2:
- accept_maddr(): only accept RTM_NEWMULTICAST; the kernel never sends RTM_GETMULTICAST in dump replies
include/libnetlink.h | 3 ++ ip/ipmaddr.c | 109 +++++++++++++++++++++++++++++++++++++++++-- lib/libnetlink.c | 26 +++++++++++ 3 files changed, 134 insertions(+), 4 deletions(-)
include/libnetlink.h +3 -0
@@ -62,6 +62,9 @@ typedef int (*req_filter_fn_t)(struct nlmsghdr *nlh, int reqlen);int rtnl_addrdump_req (struct rtnl_handle *rth,int family,req_filter_fn_t filter_fn) __attribute__((warn_unused_result)); +int rtnl_mcaddrdump_req (struct rtnl_handle *rth,int family, +req_filter_fn_t filter_fn) + __attribute__((warn_unused_result));int rtnl_addrlbldump_req (struct rtnl_handle *rth,int family) __attribute__((warn_unused_result)); int rtnl_routedump_req(struct rtnl_handle *rth, int family,
ip/ipmaddr.c +105 -4
129 lines not on this page · raw message
lib/libnetlink.c +26 -0
@@ -336,6 +336,32 @@ int rtnl_addrdump_req(struct rtnl_handle *rth, int family,return send (rth->fd, &req,sizeof (req),0 ); } +int rtnl_mcaddrdump_req (struct rtnl_handle *rth,int family, +req_filter_fn_t filter_fn) +{ +struct { +struct nlmsghdr nlh; +struct ifaddrmsg ifm; +char buf[128 ]; + } req = { + .nlh.nlmsg_len =NLMSG_LENGTH (sizeof (struct ifaddrmsg )), + .nlh.nlmsg_type =RTM_GETMULTICAST , + .nlh.nlmsg_flags =NLM_F_DUMP |NLM_F_REQUEST , + .nlh.nlmsg_seq = rth->dump = ++rth->seq, + .ifm.ifa_family = family, + }; + +if (filter_fn) { +int err; + + err =filter_fn (&req.nlh,sizeof (req)); +if (err) +return err; + } + +return send (rth->fd, &req,sizeof (req),0 ); +} +int rtnl_addrlbldump_req (struct rtnl_handle *rth,int family) {struct {
--
2.43.0