rtnetlink: dump link-layer multicast addresses
Subsystems (5)
v6 cover rtnetlink: dump link-layer multicast addresses
"ip maddr show" prints link-layer, IPv4 and IPv6 entries. The IPv4 and IPv6 ones come over netlink today, with IFA_MC_USERS for the user count. The link-layer list, dev->mc, is only exported via /proc/net/dev_mcast, so iproute2 and other users still parse procfs for it.
This series adds the AF_PACKET family to RTM_GETMULTICAST so that the link-layer multicast addresses are reported the same way. Patch 2 dumps dev->mc in the ifaddrmsg format of the IPv4 and IPv6 dumps: IFA_MULTICAST carries the address, IFA_MC_USERS the reference count and a new IFA_F_GLOBAL flag marks entries added explicitly, the "static" column of /proc/net/dev_mcast. ifa_index limits the dump to one device and IFA_TARGET_NETNSID selects another netns. Patches 1 and 3 update the rt-addr spec and patch 4 adds a selftest.
AF_PACKET dumps returned -EOPNOTSUPP before, so iproute2 can keep the procfs fallback for older kernels. The iproute2 side is ready and will be posted once this is in.
Changes in v6:
- Move the dump to net/core/dev_addr_lists.c next to the dev->mc helpers
- Stamp cb->seq from dev_base_seq and check dump consistency
- Let an unexpected dump error propagate instead of skipping
- Check for an address only present in the peer netns in the target-netnsid selftest, the peer and local ifindex can be equal
- Document the ifa-index filter and the zero header fields in the spec
Changes in v5:
- Filter the target-netnsid selftest dump by ifa-index, a new netns also contains the fallback tunnel devices
Changes in v4:
- Reset the resume offset when the device the dump stopped at is gone
- Use a tracked netns reference (put_net_track)
- Say ifa-family must be set in the spec doc, drop the AF_UNSPEC remark
- Close the netlink socket and guard the checks in the selftest
- Drop the Fixes tag
Changes in v3:
- Report the static bit as a new IFA_F_GLOBAL flag in IFA_FLAGS instead of IFA_F_PERMANENT
- Support IFA_TARGET_NETNSID and test it
- Describe global_use accurately, it is also set by dev_mc_add_excl()
- Fix the target-netnsid type in the rt-addr spec, as its own patch
Changes in v2:
- Always validate the request header, not only with strict checking
- Use a single "with" statement in the selftest (ruff)
Yuyang Huang (4):
netlink: specs: rt-addr: fix the type of target-netnsid
net: add AF_PACKET multicast dumps
netlink: specs: rt-addr: document AF_PACKET multicast dumps
selftests: net: test AF_PACKET multicast dumps
Documentation/netlink/specs/rt-addr.yaml | 19 ++- include/linux/netdevice.h | 1 + include/uapi/linux/if_addr.h | 1 + net/core/dev_addr_lists.c | 177 +++++++++++++++++++++++ net/core/rtnetlink.c | 2 + tools/testing/selftests/net/rtnetlink.py | 73 +++++++++- 6 files changed, 268 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.0
PATCH v6 1/4 netlink: specs: rt-addr: fix the type of target-netnsid
The kernel parses IFA_TARGET_NETNSID as NLA_S32 and rt-link.yaml declares its target-netnsid as s32, but rt-addr.yaml has it as binary.
--- Documentation/netlink/specs/rt-addr.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/netlink/specs/rt-addr.yaml +1 -1
@@ -119,7 +119,7 @@ attribute-sets: type: u32 - name: target-netnsid - type: binary + type: s32 - name: proto type: u8
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH net-next v6 1/4] netlink: specs: rt-addr: fix the type of target-netnsid
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The patch fixes a real, user-visible defect in a shipped netlink spec — Documentation/netlink/specs/rt-addr.yaml declared `addr-…
--
netlink: specs: rt-addr: fix the type of target-netnsid
This changes the target-netnsid attribute in the addr-attrs set of rt-addr.yaml from binary to s32, matching what the kernel policy declares. No Link: tags are present in the commit header.
Documentation/netlink/specs/rt-addr.yaml attribute-sets: · line 122
Quoted 10 lines
Documentation/netlink/specs/rt-addr.yaml +1
-1@@ -119,7 +119,7 @@ attribute-sets: type: u32 - name: target-netnsid - type: binary + type: s32
[Severity: Low]
This isn't a bug in the change itself, but should this carry a Fixes: tag?
The spec has been shipped with the wrong type, and the generated ynl code and the Python decoder have been treating the netns ID as an opaque byte blob, with the request path rejecting an integer for a binary attribute. The kernel side has always declared it signed 32-bit:
net/ipv4/devinet.c:ifa_ipv4_policy:
[IFA_TARGET_NETNSID] = { .type = NLA_S32 },with nla_put_s32() on the emit side and nla_get_s32() on the parse side in net/ipv6/addrconf.c.
git blame on the pre-patch tree attributes the type: binary line in Documentation/netlink/specs/rt-addr.yaml to:
dfb0f7d9d97956 ("doc/netlink: Add spec for rt addr messages")
so would adding
help backport tooling and users identify which releases carry the wrong type?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260921235951.3214-1-sigefriedhyy%40gmail.com
Re: [PATCH net-next v6 1/4] netlink: specs: rt-addr: fix the type of target-netnsid
Quoted 21 lines · On Wed, Sep 23, 2026 at 9:19 AM <[email protected]> wrote:
[Severity: Low]
This isn't a bug in the change itself, but should this carry a Fixes: tag?The spec has been shipped with the wrong type, and the generated ynl code and the Python decoder have been treating the netns ID as an opaque byte blob, with the request path rejecting an integer for a binary attribute. The kernel side has always declared it signed 32-bit:
net/ipv4/devinet.c:ifa_ipv4_policy:
[IFA_TARGET_NETNSID] = { .type = NLA_S32 },with
nla_put_s32()on the emit side andnla_get_s32()on the parse side innet/ipv6/addrconf.c.git blame on the pre-patch tree attributes the type: binary line in
Documentation/netlink/specs/rt-addr.yamlto:
dfb0f7d9d97956("doc/netlink: Add spec for rt addr messages")so would adding
Quoted 7 lines
Fixes dfb0f7d9d979 ("doc/netlink: Add spec for rt addr messages")help backport tooling and users identify which releases carry the wrong type?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260921235951.3214-1-sigefriedhyy%40gmail.com
This issue has been discussed in a previous review comment before, we want to keep this patch target net-next without the Fixes tag.
PATCH v6 2/4 net: add AF_PACKET multicast dumps
In reply to Yuyang HuangRTM_GETMULTICAST dumps IPv4 and IPv6 multicast group memberships, but the device multicast list (dev->mc) is only available through /proc/net/dev_mcast, so "ip maddr show" still has to parse procfs for its link-layer entries.
Handle RTM_GETMULTICAST dumps with ifa_family set to AF_PACKET next to the dev->mc helpers in dev_addr_lists.c and report every entry of dev->mc in the existing ifaddrmsg format:
- IFA_MULTICAST carries the raw link-layer address
- IFA_MC_USERS carries the entry reference count
- IFA_F_GLOBAL in IFA_FLAGS reports netdev_hw_addr::global_use, set by dev_mc_add_global() (SIOCADDMULTI) and dev_mc_add_excl() ("bridge fdb add ... self"), i.e. entries added explicitly rather than by a protocol join. This is the static column of /proc/net/dev_mcast
- ifa_scope is RT_SCOPE_LINK
This covers every column of /proc/net/dev_mcast. AF_PACKET is the family iproute2 already uses for link-layer addresses ("ip -0").
The default FDB dump also walks dev->mc, but only for Ethernet devices without an ndo_fdb_dump of their own, so bridge, vxlan or macvlan devices never show their multicast filter there, and it has no users count or global_use bit. Extending it would change "bridge fdb show" output and add NDA_* attributes.
Requests are always validated, there are no legacy users: prefixlen, flags and scope must be zero, ifa_index selects one device and IFA_TARGET_NETNSID is the only attribute accepted. The dump runs under RCU and netif_addr_lock_bh() without RTNL, and stamps cb->seq from dev_base_seq so a device added or removed between dump rounds sets NLM_F_DUMP_INTR.
--- include/linux/netdevice.h | 1 + include/uapi/linux/if_addr.h | 1 + net/core/dev_addr_lists.c | 177 +++++++++++++++++++++++++++++++++++ net/core/rtnetlink.c | 2 + 4 files changed, 181 insertions(+)
include/linux/netdevice.h +1 -0
@@ -5169,6 +5169,7 @@ int dev_mc_sync_multiple(struct net_device *to, struct net_device *from);void dev_mc_unsync (struct net_device *to,struct net_device *from);void dev_mc_flush (struct net_device *dev);void dev_mc_init (struct net_device *dev); +int dev_mc_dump (struct sk_buff *skb,struct netlink_callback *cb);/** * __dev_mc_sync - Synchronize device's multicast list
include/uapi/linux/if_addr.h +1 -0
@@ -57,6 +57,7 @@ enum {#define IFA_F_NOPREFIXROUTE 0x200#define IFA_F_MCAUTOJOIN 0x400#define IFA_F_STABLE_PRIVACY 0x800 +#define IFA_F_GLOBAL 0x1000struct ifa_cacheinfo {__u32 ifa_prefered;
net/core/dev_addr_lists.c +177 -0
191 lines not on this page · raw message
net/core/rtnetlink.c +2 -0
@@ -7278,6 +7278,8 @@ static const struct rtnl_msg_handler rtnetlink_rtnl_msg_handlers[] __initconst = {.msgtype = RTM_SETSTATS, .doit = rtnl_stats_set}, {.msgtype = RTM_NEWLINKPROP, .doit = rtnl_newlinkprop}, {.msgtype = RTM_DELLINKPROP, .doit = rtnl_dellinkprop}, + {.protocol = PF_PACKET, .msgtype = RTM_GETMULTICAST, + .dumpit = dev_mc_dump, .flags = RTNL_FLAG_DUMP_UNLOCKED}, {.protocol = PF_BRIDGE, .msgtype = RTM_GETLINK, .dumpit = rtnl_bridge_getlink}, {.protocol = PF_BRIDGE, .msgtype = RTM_DELLINK,
Signature · 1 line
2.43.0
Re: [PATCH net-next v6 2/4] net: add AF_PACKET multicast dumps
Le 22/09/2026 à 01:59, Yuyang Huang a écrit :
Quoted 37 lines
RTM_GETMULTICASTdumps IPv4 and IPv6 multicast group memberships, but the device multicast list (dev->mc) is only available through/proc/net/dev_mcast, so "ip maddr show" still has to parse procfs for its link-layer entries.Handle
RTM_GETMULTICASTdumps withifa_familyset toAF_PACKETnext to thedev->mchelpers indev_addr_lists.cand report every entry ofdev->mcin the existing ifaddrmsg format:-
IFA_MULTICASTcarries the raw link-layer address
-IFA_MC_USERScarries the entry reference count
-IFA_F_GLOBALinIFA_FLAGSreportsnetdev_hw_addr::global_use, set bydev_mc_add_global()(SIOCADDMULTI) anddev_mc_add_excl()("bridge fdb add ... self"), i.e. entries added explicitly rather than by a protocol join. This is the static column of/proc/net/dev_mcast
-ifa_scopeisRT_SCOPE_LINKThis covers every column of
/proc/net/dev_mcast.AF_PACKETis the family iproute2 already uses for link-layer addresses ("ip -0").The default FDB dump also walks
dev->mc, but only for Ethernet devices without anndo_fdb_dumpof their own, so bridge, vxlan or macvlan devices never show their multicast filter there, and it has no users count orglobal_usebit. Extending it would change "bridge fdb show" output and add NDA_* attributes.Requests are always validated, there are no legacy users: prefixlen, flags and scope must be zero,
ifa_indexselects one device andIFA_TARGET_NETNSIDis the only attribute accepted. The dump runs under RCU andnetif_addr_lock_bh()without RTNL, and stampscb->seqfromdev_base_seqso a device added or removed between dump rounds setsNLM_F_DUMP_INTR.Signed-off-by Yuyang Huang <[email protected]>Reviewed-by Nicolas Dichtel <[email protected]>---
[snip]
Quoted 27 lines
net/core/dev_addr_lists.c +27
-0+static int dev_mc_dump_dev (struct net_device *dev,struct sk_buff *skb, +struct netlink_callback *cb,int *s_addr_idx, +unsigned int flags,int netnsid) +{ +struct netdev_hw_addr *ha; +int addr_idx =0 ; +int err =0 ; + +netif_addr_lock_bh (dev); +netdev_for_each_mc_addr (ha, dev) { +if (addr_idx < *s_addr_idx) { + addr_idx++; +continue ; + } + err =dev_mc_fill_addr (skb, dev, ha,NETLINK_CB (cb->skb).portid, + cb->nlh->nlmsg_seq, flags, netnsid); +if (err <0 ) +break ; +nl_dump_check_consistent (cb,nlmsg_hdr (skb)); + addr_idx++; + } +netif_addr_unlock_bh (dev); + + *s_addr_idx = err <0 ? addr_idx :0 ; + +return err; +}
[snip]
Quoted 10 lines
net/core/dev_addr_lists.c +10
-0+int dev_mc_dump (struct sk_buff *skb,struct netlink_callback *cb) +{ +struct dev_mc_dump_filter filter = { + .tgt_net =sock_net (skb->sk), + .netnsid =-1 , + }; +unsigned int flags =NLM_F_MULTI ; +struct { +unsigned long ifindex; +int addr_idx;
net/core/dev_addr_lists.c void dev_mc_init(struct net_device *dev) · line 1324
Quoted 10 lines
net/core/dev_addr_lists.c +10
-0+ } *ctx = (void *)cb->ctx; +unsigned long s_ifindex; +struct net_device *dev; +int err; + + err =dev_mc_valid_dump_req (cb->nlh, skb->sk, &filter, cb->extack); +if (err <0 ) +return err; + + cb->seq =READ_ONCE (filter.tgt_net->dev_base_seq);
dev_base_seq only indicates whether the netdevice list has changed, not whether the addr list has changed. It's only a partial consistency check. See inet_base_seq()/inet6_base_seq().
I don't see an equivalent of dev_addr_genid for the L2 mcaddr list; it should probably be added.
Re: [PATCH net-next v6 2/4] net: add AF_PACKET multicast dumps
Quoted 6 lines · Nicolas Dichtel
dev_base_seqonly indicates whether the netdevice list has changed, not whether the addr list has changed. It's only a partial consistency check. See inet_base_seq()/inet6_base_seq().I don't see an equivalent of
dev_addr_genidfor the L2 mcaddr list; it should probably be added.
Thanks for the suggestion, I will fix this issue in v7: I think I need to add a per netns counter bumped on dev->mc changes and combine it with dev_base_seq like inet_base_seq() does.
Thanks,
Yuyang
[email protected]
AI reviewRe: [PATCH net-next v6 2/4] net: add AF_PACKET multicast dumps
In reply to Yuyang HuangThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] dev_mc_dump()/dev_mc_dump_dev() resume a paused dump with a bare positional index into dev->mc (ctx->addr_idx / *s_addr_idx) and…
- [Low] uAPI vs netlink YAML spec out of sync: the patch adds IFA_F_GLOBAL (0x1000), emits IFA_FLAGS/IFA_TARGET_NETNSID in…
--
net: add AF_PACKET multicast dumps
This adds an RTM_GETMULTICAST dump handler for ifa_family AF_PACKET that reports every entry of dev->mc in the ifaddrmsg format, with the raw link-layer address in IFA_MULTICAST, the reference count in IFA_MC_USERS and a new IFA_F_GLOBAL bit mirroring netdev_hw_addr::global_use. The dump runs under RCU and netif_addr_lock_bh() without RTNL.
include/uapi/linux/if_addr.h enum { · line 60
Quoted 9 lines
include/uapi/linux/if_addr.h +1
-0@@ -57,6 +57,7 @@ enum {#define IFA_F_NOPREFIXROUTE 0x200#define IFA_F_MCAUTOJOIN 0x400#define IFA_F_STABLE_PRIVACY 0x800 +#define IFA_F_GLOBAL 0x1000
[Severity: Low]
This isn't a bug, just an observation about the intermediate state of the
series: this commit adds the new bit 12 and starts emitting IFA_FLAGS and IFA_TARGET_NETNSID for AF_PACKET RTM_GETMULTICAST, but Documentation/netlink/specs/rt-addr.yaml is not touched here. At this commit the ifa-flags enum still ends at stable-privacy, the getmulticast dump request lists no attributes, the reply set is {multicast, mc-users, cacheinfo} and the op doc mentions IPv4/IPv6 only, so a spec driven decoder such as ynl would see bit 12 as unknown and would not describe target-netnsid or flags.
The next patch in the series, "netlink: specs: rt-addr: document AF_PACKET multicast dumps", adds "- name: global" to ifa-flags, adds target-netnsid to the dump request, adds flags and target-netnsid to the shared mcaddr-attrs reply set and updates the doc, so the gap is closed within the series. Would it be worth folding the spec update into this commit so that no bisect point emits attributes the spec does not describe?
Quoted 4 lines
net/core/dev_addr_lists.c +0
-0
[ ... ]
Quoted 1 line
net/core/dev_addr_lists.c +0
-0@@ -1180,6 +1184,179 @@ void dev_mc_init(struct net_device *dev)
[ ... ]
Quoted 17 lines
net/core/dev_addr_lists.c +17
-0+static int dev_mc_dump_dev (struct net_device *dev,struct sk_buff *skb, +struct netlink_callback *cb,int *s_addr_idx, +unsigned int flags,int netnsid) +{ +struct netdev_hw_addr *ha; +int addr_idx =0 ; +int err =0 ; + +netif_addr_lock_bh (dev); +netdev_for_each_mc_addr (ha, dev) { +if (addr_idx < *s_addr_idx) { + addr_idx++; +continue ; + } + err =dev_mc_fill_addr (skb, dev, ha,NETLINK_CB (cb->skb).portid, + cb->nlh->nlmsg_seq, flags, netnsid); +if (err <0 )
net/core/dev_addr_lists.c void dev_mc_init(struct net_device *dev) · line 1247
Quoted 10 lines
net/core/dev_addr_lists.c +10
-0+break ; +nl_dump_check_consistent (cb,nlmsg_hdr (skb)); + addr_idx++; + } +netif_addr_unlock_bh (dev); + + *s_addr_idx = err <0 ? addr_idx :0 ; + +return err; +}
[Severity: Medium]
Can this dump silently skip or repeat a dev->mc entry without setting NLM_F_DUMP_INTR?
The resume cursor here is a bare ordinal into dev->mc, and netif_addr_lock_bh(dev) is dropped before returning, so the lock is not held across a dump round boundary:
*s_addr_idx = err < 0 ? addr_idx : 0;
If round N stops at ordinal k+1 and an entry before k is removed while the dump is paused, every later entry shifts down by one and round N+1 starts at k+1, so the entry that moved from k+1 to k is never reported. An insertion before the cursor reports one entry twice.
The only consistency stamp is dev_base_seq:
net/core/dev_addr_lists.c void dev_mc_init(struct net_device *dev) · line 1324
Quoted 1 line
net/core/dev_addr_lists.c +1
-0+ cb->seq =READ_ONCE (filter.tgt_net->dev_base_seq);
and dev_base_seq_inc() is called only from list_netdevice() and unlist_netdevice() in net/core/dev.c, so it tracks device register and unregister, not dev->mc mutations. nl_dump_check_consistent() therefore cannot raise NLM_F_DUMP_INTR for the mutation that breaks the resume index, and user space sees what looks like a complete, consistent dump.
The writers are reachable without any privilege, for instanceIP_DROP_MEMBERSHIP: __ip_mc_dec_group()->ip_mc_filter_del()->dev_mc_del(), or IP_ADD_MEMBERSHIP: ip_mc_inc_group()->dev_mc_add(), and multiple dump rounds happen as soon as the reply does not fit in one skb.
net/core/rtnetlink.c static const struct rtnl_msg_handler rtnetlink_rtnl_msg_handlers[] __initconst = · line 7282
Quoted 10 lines
net/core/rtnetlink.c +2
-0@@ -7278,6 +7278,8 @@ static const struct rtnl_msg_handler rtnetlink_rtnl_msg_handlers[] __initconst = {.msgtype = RTM_SETSTATS, .doit = rtnl_stats_set}, {.msgtype = RTM_NEWLINKPROP, .doit = rtnl_newlinkprop}, {.msgtype = RTM_DELLINKPROP, .doit = rtnl_dellinkprop}, + {.protocol = PF_PACKET, .msgtype = RTM_GETMULTICAST, + .dumpit = dev_mc_dump, .flags = RTNL_FLAG_DUMP_UNLOCKED},
Since RTNL_FLAG_DUMP_UNLOCKED is used, even the dev->mc writers that do hold RTNL (SIOCADDMULTI via dev_mc_add_global(), "bridge fdb add ... self" via dev_mc_add_excl(), dev_mc_flush()) are free to run between two rounds of this dump. Would a generation counter bumped on dev->mc changes, and folded into cb->seq, be needed here, or could the resume point be keyed on the address itself rather than on its position in the list?
The commit message says:
stamps cb->seq from dev_base_seq so a device added or removed between dump rounds sets NLM_F_DUMP_INTR
That statement is accurate for device add and remove, but should it also note that changes to the dumped objects themselves, the dev->mc entries, are not covered by that counter?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260921235951.3214-1-sigefriedhyy%40gmail.com
Re: [PATCH net-next v6 2/4] net: add AF_PACKET multicast dumps
Quoted 2 lines
- [Medium] dev_mc_dump()/dev_mc_dump_dev() resume a paused dump with a bare positional index into
dev->mc(ctx->addr_idx/*s_addr_idx) and…
Same point Nicolas raised. I will fix it in patchset v7.
Quoted 2 lines
- [Low] uAPI vs netlink YAML spec out of sync: the patch adds
IFA_F_GLOBAL(0x1000), emits IFA_FLAGS/IFA_TARGET_NETNSID in…
The spec update is the next patch of the series.
PATCH v6 3/4 netlink: specs: rt-addr: document AF_PACKET multicast dumps
In reply to Yuyang HuangAdd the global flag, list the attributes the AF_PACKET dump uses and describe how ifa-family selects IPv4, IPv6 or link-layer output for RTM_GETMULTICAST.
--- Documentation/netlink/specs/rt-addr.yaml | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
Documentation/netlink/specs/rt-addr.yaml +15 -2
@@ -77,6 +77,8 @@ definitions: name: mcautojoin - name: stable-privacy + - + name: global attribute-sets: -
@@ -168,7 +170,15 @@ operations: attributes: *ifaddr-all - name: getmulticast - doc: Get / dump IPv4/IPv6 multicast addresses. + doc: | + Get / dump multicast addresses. ifa-family must select the address + family: AF_INET or AF_INET6 for the IP multicast groups joined on + a device, AF_PACKET for the link-layer multicast addresses in the + device filter. Link-layer entries added explicitly, e.g. with + SIOCADDMULTI or "bridge fdb add ... self", rather than by a + protocol join are reported with the global flag set. A non-zero + ifa-index restricts the dump to that device, ifa-prefixlen, + ifa-flags and ifa-scope must be zero. attribute-set: addr-attrs fixed-header: ifaddrmsg do:
@@ -181,10 +191,13 @@ operations: - multicast - mc-users - cacheinfo + - flags + - target-netnsid dump: request: value: 58 - attributes: [] + attributes: + - target-netnsid reply: value: 58 attributes: *mcaddr-attrs
Signature · 1 line
2.43.0
PATCH v6 4/4 selftests: net: test AF_PACKET multicast dumps
In reply to Yuyang HuangDump the link-layer multicast addresses of a dummy device and verify that ifa_index restricts the dump to that device, that the all-hosts address joined on link up is listed without IFA_F_GLOBAL, that an address added with SIOCADDMULTI is listed with IFA_F_GLOBAL and IFA_MC_USERS, and that IFA_TARGET_NETNSID dumps another netns.
--- tools/testing/selftests/net/rtnetlink.py | 73 +++++++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-)
tools/testing/selftests/net/rtnetlink.py +71 -2
@@ -5,13 +5,18 @@ import socket import struct import time from lib.py import bkg, ip, ksft_exit, ksft_run, ksft_eq, ksft_ge, ksft_true, KsftSkipEx -from lib.py import ksft_not_in, ksft_not_none +from lib.py import ksft_in, ksft_not_in, ksft_not_none from lib.py import CmdExitFailure, NetNS, NetNSEnter, RtnlAddrFamily, RtnlRouteFamily from lib.py import defer IPV4_ALL_HOSTS_MULTICAST = b'\xe0\x00\x00\x01' IPV4_TEST_MULTICAST = b'\xef\x01\x01\x01' IPV6_TEST_MULTICAST = bytes.fromhex('ff020000000000000000000000000123') +ETH_ALL_HOSTS_MULTICAST = bytes.fromhex('01005e000001') +ETH_TEST_MULTICAST_STR = '01:00:5e:01:01:01' +ETH_TEST_MULTICAST = bytes.fromhex(ETH_TEST_MULTICAST_STR.replace(':', '')) +ETH_PEER_MULTICAST_STR = '01:00:5e:02:02:02' +ETH_PEER_MULTICAST = bytes.fromhex(ETH_PEER_MULTICAST_STR.replace(':', '')) def _users_for(rtnl: RtnlAddrFamily, family: int, grp: bytes, ifindex: int):
@@ -105,6 +110,69 @@ def dump_mcaddr6_check() -> None: s2.close() +def dump_mcaddr_l2_check() -> None: + """ + Verify link-layer multicast addresses in an AF_PACKET RTM_GETMULTICAST + dump: the ifa-index filter, mc-users, the global flag and + target-netnsid. + """ + + with NetNS() as ns, NetNSEnter(str(ns)): + for ifname in ("dummy1", "dummy2"): + ip(f"link add name {ifname} type dummy") + ip(f"link set {ifname} up") + dev_idx = socket.if_nametoindex("dummy1") + ip(f"maddr add {ETH_TEST_MULTICAST_STR} dev dummy1") + + rtnl = RtnlAddrFamily() + defer(rtnl.close) + addresses = rtnl.getmulticast( + {"ifa-family": socket.AF_PACKET, "ifa-index": dev_idx}, + dump=True) + + # dummy2 has entries as well, only dummy1 may be listed + ksft_eq({addr['ifa-index'] for addr in addresses}, {dev_idx}, + "AF_PACKET multicast dump ignored ifa-index filter") + + entries = {addr['multicast']: addr for addr in addresses} + + # Bringing an Ethernet device up joins 224.0.0.1, which maps + # to 01:00:5e:00:00:01 in the device multicast list. + all_hosts = entries.get(ETH_ALL_HOSTS_MULTICAST) + ksft_not_none(all_hosts, + "dummy1 does not have the all-hosts link-layer address") + if all_hosts is not None: + ksft_not_in('global', all_hosts['flags'], + "protocol entry is global") + + static = entries.get(ETH_TEST_MULTICAST) + ksft_not_none(static, "dummy1 does not have the SIOCADDMULTI address") + if static is not None: + ksft_eq(static['mc-users'], 1, + "unexpected mc-users for the SIOCADDMULTI address") + ksft_in('global', static['flags'], + "SIOCADDMULTI entry is not global") + + # target-netnsid dumps another netns, ifa-index is relative to it + with NetNS() as peer: + ip(f"netns set {peer} 5") + ip("link add name dummy3 type dummy", ns=peer) + ip("link set dummy3 up", ns=peer) + ip(f"maddr add {ETH_PEER_MULTICAST_STR} dev dummy3", ns=peer) + peer_idx = ip("link show dummy3", json=True, ns=peer)[0]['ifindex'] + + addresses = rtnl.getmulticast( + {"ifa-family": socket.AF_PACKET, "target-netnsid": 5, + "ifa-index": peer_idx}, dump=True) + ksft_eq({(addr['ifa-index'], addr['target-netnsid']) + for addr in addresses}, {(peer_idx, 5)}, + "target-netnsid did not dump the peer netns") + # dummy1 in this netns can have the same ifindex as dummy3 + ksft_in(ETH_PEER_MULTICAST, + {addr['multicast'] for addr in addresses}, + "target-netnsid did not dump the peer device") + + def ipv4_devconf_notify() -> None: """ Configure an interface and set ipv4-devconf values through netlink
@@ -424,7 +492,8 @@ def ipv6_verify_inter_scope_addr_order() -> None: def main() -> None: - ksft_run([dump_mcaddr_check, dump_mcaddr6_check, ipv4_devconf_notify, + ksft_run([dump_mcaddr_check, dump_mcaddr6_check, dump_mcaddr_l2_check, + ipv4_devconf_notify, ipv6_route_del_reason_expired, ipv6_route_del_reason_ra_withdrawn, ipv6_route_del_reason_absent,
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH net-next v6 4/4] selftests: net: test AF_PACKET multicast dumps
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] dev_mc_fill_addr() (net/core/dev_addr_lists.c:1210) emits IFA_MULTICAST with `nla_put(skb, IFA_MULTICAST, dev->addr_len,…
- [Medium] dump_mcaddr_l2_check() (tools/testing/selftests/net/rtnetlink.py:113) issues the new…
--
selftests: net: test AF_PACKET multicast dumps
Adds dump_mcaddr_l2_check() to tools/testing/selftests/net/rtnetlink.py, which dumps the link-layer multicast addresses of dummy devices over AF_PACKET RTM_GETMULTICAST and checks the ifa-index filter, the global flag, mc-users and target-netnsid handling.
Quoted 4 lines
tools/testing/selftests/net/rtnetlink.py +0
-0
[ ... ]
Quoted 13 lines
tools/testing/selftests/net/rtnetlink.py +9
-0@@ -105,6 +110,69 @@ def dump_mcaddr6_check() -> None: s2.close() +def dump_mcaddr_l2_check() -> None: + """ + Verify link-layer multicast addresses in an AF_PACKET RTM_GETMULTICAST + dump: the ifa-index filter, mc-users, the global flag and + target-netnsid. + """ + + with NetNS() as ns, NetNSEnter(str(ns)): + for ifname in ("dummy1", "dummy2"):
tools/testing/selftests/net/rtnetlink.py def dump_mcaddr6_check() -> None: · line 131
Quoted 10 lines
tools/testing/selftests/net/rtnetlink.py +10
-0+ ip(f"link add name {ifname} type dummy") + ip(f"link set {ifname} up") + dev_idx = socket.if_nametoindex("dummy1") + ip(f"maddr add {ETH_TEST_MULTICAST_STR} dev dummy1") + + rtnl = RtnlAddrFamily() + defer(rtnl.close) + addresses = rtnl.getmulticast( + {"ifa-family": socket.AF_PACKET, "ifa-index": dev_idx}, + dump=True)
[Severity: Medium]
Should this dump be guarded so that an older kernel produces a skip instead of a failure?
On a kernel without the PF_PACKET RTM_GETMULTICAST registration, rtnetlink_rcv_msg() looks up the handler for the requested family, then retries with PF_UNSPEC:
net/core/rtnetlink.c:rtnetlink_rcv_msg() {
...
link = rtnl_get_link(family, type);
if (!link || !link->dumpit) {
family = PF_UNSPEC;
link = rtnl_get_link(family, type);
if (!link || !link->dumpit)
goto err_unlock;
...
}There is no PF_UNSPEC RTM_GETMULTICAST dumpit (only PF_INET in net/ipv4/devinet.c and PF_INET6 in net/ipv6/addrconf.c, plus the PF_PACKET entry added by this series), so err stays at its -EOPNOTSUPP initialisation and an error ack comes back. ynl then turns that into an exception:
tools/net/ynl/pyynl/lib/ynl.py:YnlFamily._op() {
...
if nl_msg.error:
raise NlError(nl_msg)
...
}which ksft_run() reports as "not ok" plus a traceback.
The two neighbouring tests in this same file take the other route:_users_for() returns None when mc-users is absent, and dump_mcaddr_check()/dump_mcaddr6_check() convert that into raise KsftSkipEx("kernel does not expose IFA_MC_USERS"). Would wrapping the first getmulticast() call and raising KsftSkipEx on NlError keep this new case consistent with the rest of the file when the selftest tree is newer than the kernel under test?
tools/testing/selftests/net/rtnetlink.py def dump_mcaddr6_check() -> None: · line 137
Quoted 6 lines
tools/testing/selftests/net/rtnetlink.py +6
-0+ + # dummy2 has entries as well, only dummy1 may be listed + ksft_eq({addr['ifa-index'] for addr in addresses}, {dev_idx}, + "AF_PACKET multicast dump ignored ifa-index filter") + + entries = {addr['multicast']: addr for addr in addresses}
[Severity: High]
The IFA_MULTICAST payload consumed here is emitted by dev_mc_fill_addr() in net/core/dev_addr_lists.c, added by this series, using the device's current addr_len for every entry of dev->mc:
net/core/dev_addr_lists.c:dev_mc_fill_addr() {
...
nla_put(skb, IFA_MULTICAST, dev->addr_len, ha->addr) ||
...
}Each entry was created with an unzeroed allocation where only addr_len bytes were written:
net/core/dev_addr_lists.c:__hw_addr_create() {
...
ha = kmalloc(alloc_size, GFP_ATOMIC);
if (!ha)
return NULL;
memcpy(ha->addr, addr, addr_len);
...
}ha->addr is MAX_ADDR_LEN (32) bytes, so bytes addr_len..31 hold stale slab contents. Can this dump then copy uninitialised heap bytes to user space if dev->addr_len grows after the entry was inserted?
The sequence I end up with is:
1. create a tap device (ARPHRD_ETHER, addr_len 6; tun_net_mclist is provided as .ndo_set_rx_mode, which SIOCADDMULTI requires per net/core/dev_ioctl.c)
2. ip maddr add 01:00:5e:01:01:01 dev tap0, so __hw_addr_create() copies 6 bytes
3. with the device down, TUNSETLINK to ARPHRD_TUNNEL6:
drivers/net/tun.c:__tun_chr_ioctl() {
...
tun->dev->type = (int) arg;
tun->dev->addr_len = tun_get_addr_len(tun->dev->type);
...
} addr_len becomes sizeof(struct in6_addr), and dev->mc is not flushed; the NETDEV_PRE_TYPE_CHANGE handlers only drop the protocol-joined mappings via ip_mc_unmap()/ipv6_mc_unmap(), not global_use entries
4. an AF_PACKET RTM_GETMULTICAST dump (RTNL_KIND_GET, no capability required) now returns 16 bytes for that entry, 10 of which were never initialised
The pre-existing dev->mc dump path does not reach this because it is gated on the device type:
net/core/rtnetlink.c:ndo_dflt_fdb_dump() {
...
if (dev->type != ARPHRD_ETHER)
return -EINVAL;
...
}The new dump has no such restriction. Would bounding the copy by what was actually initialised - recording or clamping a per-entry length, or zeroing ha->addr in __hw_addr_create() - be preferable to trusting dev->addr_len here?
For the record, the bonding path is not a trigger: bond_enslave() calls dev_uc_flush()/dev_mc_flush() immediately before bond_setup_by_slave() changes addr_len, so only the tun/tap TUNSETLINK path above applies.
Quoted 17 lines
tools/testing/selftests/net/rtnetlink.py +17
-0+ + # Bringing an Ethernet device up joins 224.0.0.1, which maps + # to 01:00:5e:00:00:01 in the device multicast list. + all_hosts = entries.get(ETH_ALL_HOSTS_MULTICAST) + ksft_not_none(all_hosts, + "dummy1 does not have the all-hosts link-layer address") + if all_hosts is not None: + ksft_not_in('global', all_hosts['flags'], + "protocol entry is global") + + static = entries.get(ETH_TEST_MULTICAST) + ksft_not_none(static, "dummy1 does not have the SIOCADDMULTI address") + if static is not None: + ksft_eq(static['mc-users'], 1, + "unexpected mc-users for the SIOCADDMULTI address") + ksft_in('global', static['flags'], + "SIOCADDMULTI entry is not global")
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260921235951.3214-1-sigefriedhyy%40gmail.com
Re: [PATCH net-next v6 4/4] selftests: net: test AF_PACKET multicast dumps
Quoted 2 lines
- [High]
dev_mc_fill_addr()(net/core/dev_addr_lists.c:1210) emitsIFA_MULTICASTwith `nla_put(skb,IFA_MULTICAST,dev->addr_len
Existing issue, /proc/net/dev_mcast prints the same bytes. Should be addressed separately.
Quoted 2 lines
- [Medium]
dump_mcaddr_l2_check()(tools/testing/selftests/net/rtnetlink.py:113) issues the new
This is WAI.