Skip to contentSkip to conversation

rtnetlink: dump link-layer multicast addresses

PATCH Superseded Yuyang Huang 2 participants 13 messages Sep 24 – Oct 6

Subsystems (5)

v7 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v7 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 3 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. Patch 2 adds a generation counter for dev->mc changes so a multi-part dump reports NLM_F_DUMP_INTR like the IPv4 and IPv6 ones. Patches 1 and 4 update the rt-addr spec and patch 5 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 v7:
- Add a per netns generation counter bumped on dev->mc changes and fold it into cb->seq, dev_base_seq alone did not cover the dumped entries
- Sample cb->seq under the address lock of each device and again when a dump round ends, so the NLMSG_DONE check sees a change too
- Drop the Reviewed-by tag of patch 3 after these changes

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 (5):
netlink: specs: rt-addr: fix the type of target-netnsid
net: add a generation counter for dev->mc changes
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                |   4 +
 include/net/net_namespace.h              |   1 +
 include/uapi/linux/if_addr.h             |   1 +
 net/core/dev_addr_lists.c                | 216 +++++++++++++++++++++++
 net/core/rtnetlink.c                     |   2 +
 tools/testing/selftests/net/rtnetlink.py |  73 +++++++-
 7 files changed, 311 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v7 1/5 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
index 0ecbd24c890c..17ead2203451 100644
@@ -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
#2 lore raw

PATCH v7 2/5 net: add a generation counter for dev->mc changes

In reply to Yuyang Huang

A multi-part RTM_GETMULTICAST dump of dev->mc resumes by position, so entries added or removed between two dump rounds can be skipped or repeated. The IPv4 and IPv6 dumps report that with NLM_F_DUMP_INTR by stamping cb->seq from a per netns generation counter combined with dev_base_seq, see inet_base_seq().

Add the equivalent for the device multicast lists: a per netns counter bumped whenever an entry is added to or removed from any dev->mc. The list helpers do not know which device a list belongs to, so give netdev_hw_addr_list an owner set for dev->mc only and bump the counter of dev_net(owner) where entries are created and freed. That covers the dev_mc_* helpers, both lists of a sync, the hardware sync helpers drivers call from their rx mode callbacks or their own workers and the reconciliation after an asynchronous rx mode update, while snapshot and other lists have no owner and are not tracked. It is atomic since the writers only hold the address lock of their own device.

Used by the following patch for the AF_PACKET multicast dump.

---
 include/linux/netdevice.h   |  3 +++
 include/net/net_namespace.h |  1 +
 net/core/dev_addr_lists.c   | 21 +++++++++++++++++++++
 3 files changed, 25 insertions(+)
include/linux/netdevice.h +3 -0
index 5d16737167ee..be3804cdbb2d 100644
@@ -256,6 +256,9 @@ struct netdev_hw_addr_list {
 
 	/* Auxiliary tree for faster lookup on addition and deletion */
 	struct rb_root		tree;
+
+	/* Set for dev->mc, the owning device of a tracked list */
+	struct net_device	*owner;
 };
 
 #define netdev_hw_addr_list_count(l) ((l)->count)
include/net/net_namespace.h +1 -0
index 46b4c67e2966..d8c681ab5c74 100644
@@ -71,6 +71,7 @@ struct net {
 	spinlock_t		rules_mod_lock;
 
 	unsigned int		dev_base_seq;	/* protected by rtnl_mutex */
+	atomic_t		dev_mc_genid;	/* bumped on dev->mc changes */
 	u32			ifindex;
 
 	spinlock_t		nsid_lock;
net/core/dev_addr_lists.c +21 -0
index 08528ca0a8b3..18acd874fdbd 100644
@@ -16,6 +16,19 @@
 
 #include "dev.h"
 
+/**
+ * __hw_addr_changed - account a change of a tracked address list
+ * @list: the address list an entry was added to or removed from
+ *
+ * Bumps the netns generation counter RTM_GETMULTICAST dumps use to detect
+ * changes of dev->mc between dump rounds. Untracked lists have no owner.
+ */
+static void __hw_addr_changed(struct netdev_hw_addr_list *list)
+{
+	if (list->owner)
+		atomic_inc(&dev_net(list->owner)->dev_mc_genid);
+}
+
 /*
  * General list handling functions
  */
@@ -126,6 +139,7 @@ static int __hw_addr_add_ex(struct netdev_hw_addr_list *list,
 
 	list_add_tail_rcu(&ha->list, &list->list);
 	list->count++;
+	__hw_addr_changed(list);
 
 	return 0;
 }
@@ -162,6 +176,7 @@ static int __hw_addr_del_entry(struct netdev_hw_addr_list *list,
 	list_del_rcu(&ha->list);
 	kfree_rcu(ha, rcu_head);
 	list->count--;
+	__hw_addr_changed(list);
 	return 0;
 }
 
@@ -487,6 +502,8 @@ void __hw_addr_flush(struct netdev_hw_addr_list *list)
 {
 	struct netdev_hw_addr *ha, *tmp;
 
+	if (list->count)
+		__hw_addr_changed(list);
 	list->tree = RB_ROOT;
 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
 		list_del_rcu(&ha->list);
@@ -501,6 +518,7 @@ void __hw_addr_init(struct netdev_hw_addr_list *list)
 	INIT_LIST_HEAD(&list->list);
 	list->count = 0;
 	list->tree = RB_ROOT;
+	list->owner = NULL;
 }
 EXPORT_SYMBOL(__hw_addr_init);
 
@@ -612,6 +630,7 @@ void __hw_addr_list_reconcile(struct netdev_hw_addr_list *real_list,
 				__hw_addr_insert(real_list, ref_ha,
 						 addr_len);
 				real_list->count++;
+				__hw_addr_changed(real_list);
 			}
 			continue;
 		}
@@ -623,6 +642,7 @@ void __hw_addr_list_reconcile(struct netdev_hw_addr_list *real_list,
 			list_del_rcu(&real_ha->list);
 			kfree_rcu(real_ha, rcu_head);
 			real_list->count--;
+			__hw_addr_changed(real_list);
 		}
 	}
 
@@ -1177,6 +1197,7 @@ EXPORT_SYMBOL(dev_mc_flush);
 void dev_mc_init(struct net_device *dev)
 {
 	__hw_addr_init(&dev->mc);
+	dev->mc.owner = dev;
 }
 EXPORT_SYMBOL(dev_mc_init);
 
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH net-next v7 2/5] net: add a generation counter for dev->mc changes

Le 24/09/2026 à 03:15, Yuyang Huang a écrit :

Quoted 6 lines

A multi-part RTM_GETMULTICAST dump of dev->mc resumes by position, so entries added or removed between two dump rounds can be skipped or repeated. The IPv4 and IPv6 dumps report that with NLM_F_DUMP_INTR by stamping cb->seq from a per netns generation counter combined with dev_base_seq, see inet_base_seq().

Quoted 10 lines

Add the equivalent for the device multicast lists: a per netns counter bumped whenever an entry is added to or removed from any dev->mc. The list helpers do not know which device a list belongs to, so give netdev_hw_addr_list an owner set for dev->mc only and bump the counter of dev_net(owner) where entries are created and freed. That covers the dev_mc_* helpers, both lists of a sync, the hardware sync helpers drivers call from their rx mode callbacks or their own workers and the reconciliation after an asynchronous rx mode update, while snapshot and other lists have no owner and are not tracked. It is atomic since the writers only hold the address lock of their own device.

For IPv4 and IPv6, the generation counter was initially used to invalidate some caches; that's why it is global. Here, the goal is only to check the consistency of the netlink dump. I wonder if maintaining a counter per list would not be more straightforward.

A helper could then manage list->count and list->genid, both evolving together.

#4 lore raw

Re: [PATCH net-next v7 2/5] net: add a generation counter for dev->mc changes

Le 25/09/2026 à 09:58, Nicolas Dichtel a écrit :

Quoted 18 lines

Le 24/09/2026 à 03:15, Yuyang Huang a écrit :

Quoted 6 lines

A multi-part RTM_GETMULTICAST dump of dev->mc resumes by position, so entries added or removed between two dump rounds can be skipped or repeated. The IPv4 and IPv6 dumps report that with NLM_F_DUMP_INTR by stamping cb->seq from a per netns generation counter combined with dev_base_seq, see inet_base_seq().

Quoted 10 lines

Add the equivalent for the device multicast lists: a per netns counter bumped whenever an entry is added to or removed from any dev->mc. The list helpers do not know which device a list belongs to, so give netdev_hw_addr_list an owner set for dev->mc only and bump the counter of dev_net(owner) where entries are created and freed. That covers the dev_mc_* helpers, both lists of a sync, the hardware sync helpers drivers call from their rx mode callbacks or their own workers and the reconciliation after an asynchronous rx mode update, while snapshot and other lists have no owner and are not tracked. It is atomic since the writers only hold the address lock of their own device.

Quoted 4 lines

For IPv4 and IPv6, the generation counter was initially used to invalidate some caches; that's why it is global. Here, the goal is only to check the consistency of the netlink dump. I wonder if maintaining a counter per list would not be more straightforward.

Forget, it doesn't fit in the netlink consistency framework (:

#5 lore raw

Re: [PATCH net-next v7 2/5] net: add a generation counter for dev->mc changes

In reply to Yuyang Huang

Le 24/09/2026 à 03:15, Yuyang Huang a écrit :

Quoted 31 lines

A multi-part RTM_GETMULTICAST dump of dev->mc resumes by position, so entries added or removed between two dump rounds can be skipped or repeated. The IPv4 and IPv6 dumps report that with NLM_F_DUMP_INTR by stamping cb->seq from a per netns generation counter combined with dev_base_seq, see inet_base_seq().

Add the equivalent for the device multicast lists: a per netns counter bumped whenever an entry is added to or removed from any dev->mc. The list helpers do not know which device a list belongs to, so give netdev_hw_addr_list an owner set for dev->mc only and bump the counter of dev_net(owner) where entries are created and freed. That covers the dev_mc_* helpers, both lists of a sync, the hardware sync helpers drivers call from their rx mode callbacks or their own workers and the reconciliation after an asynchronous rx mode update, while snapshot and other lists have no owner and are not tracked. It is atomic since the writers only hold the address lock of their own device.

Used by the following patch for the AF_PACKET multicast dump.

---
 include/linux/netdevice.h   |  3 +++
 include/net/net_namespace.h |  1 +
 net/core/dev_addr_lists.c   | 21 +++++++++++++++++++++
 3 files changed, 25 insertions(+)
include/linux/netdevice.h +0 -0
index 5d16737167ee..be3804cdbb2d 100644

include/linux/netdevice.h struct netdev_hw_addr_list { · line 261

Quoted 6 lines
include/linux/netdevice.h +3 -0
@@ -256,6 +256,9 @@ struct netdev_hw_addr_list {
 
 	/* Auxiliary tree for faster lookup on addition and deletion */
 	struct rb_root		tree;
+
+	/* Set for dev->mc, the owning device of a tracked list */
+	struct net_device	*owner;

There are several lists owned by a device (dev->dev_addrs, dev->uc, dev->mc, dev->rx_mode_addr_cache), the name 'owner' does not reflect that it is set only when the list is dev->mc.
I wonder if the name should be changed or if __hw_addr_init() should be updated to always set owner when the list is owned by a device.

Quoted 40 lines
include/linux/netdevice.h +0 -0
 };
 
 #define netdev_hw_addr_list_count(l) ((l)->count)
include/net/net_namespace.h +1 -0
index 46b4c67e2966..d8c681ab5c74 100644
@@ -71,6 +71,7 @@ struct net {
 	spinlock_t		rules_mod_lock;
 
 	unsigned int		dev_base_seq;	/* protected by rtnl_mutex */
+	atomic_t		dev_mc_genid;	/* bumped on dev->mc changes */
 	u32			ifindex;
 
 	spinlock_t		nsid_lock;
net/core/dev_addr_lists.c +13 -0
index 08528ca0a8b3..18acd874fdbd 100644
@@ -16,6 +16,19 @@
 
 #include "dev.h"
 
+/**
+ * __hw_addr_changed - account a change of a tracked address list
+ * @list: the address list an entry was added to or removed from
+ *
+ * Bumps the netns generation counter RTM_GETMULTICAST dumps use to detect
+ * changes of dev->mc between dump rounds. Untracked lists have no owner.
+ */
+static void __hw_addr_changed(struct netdev_hw_addr_list *list)
+{
+	if (list->owner)
+		atomic_inc(&dev_net(list->owner)->dev_mc_genid);
+}
+
 /*
  * General list handling functions
  */

net/core/dev_addr_lists.c static int __hw_addr_add_ex(struct netdev_hw_addr_list *list, · line 142

Quoted 4 lines
net/core/dev_addr_lists.c +1 -0
@@ -126,6 +139,7 @@ static int __hw_addr_add_ex(struct netdev_hw_addr_list *list,
 
 	list_add_tail_rcu(&ha->list, &list->list);
 	list->count++;
+	__hw_addr_changed(list);

list->count and dev_mc_genid evolves together. Perhaps some helpers would be less error-prone for future patches?

__hw_addr_count_inc()
__hw_addr_count_dec()
__hw_addr_count_reset()

?

Quoted 52 lines
net/core/dev_addr_lists.c +7 -0
 	return 0;
 }
@@ -162,6 +176,7 @@ static int __hw_addr_del_entry(struct netdev_hw_addr_list *list,
 	list_del_rcu(&ha->list);
 	kfree_rcu(ha, rcu_head);
 	list->count--;
+	__hw_addr_changed(list);
 	return 0;
 }
 
@@ -487,6 +502,8 @@ void __hw_addr_flush(struct netdev_hw_addr_list *list)
 {
 	struct netdev_hw_addr *ha, *tmp;
 
+	if (list->count)
+		__hw_addr_changed(list);
 	list->tree = RB_ROOT;
 	list_for_each_entry_safe(ha, tmp, &list->list, list) {
 		list_del_rcu(&ha->list);
@@ -501,6 +518,7 @@ void __hw_addr_init(struct netdev_hw_addr_list *list)
 	INIT_LIST_HEAD(&list->list);
 	list->count = 0;
 	list->tree = RB_ROOT;
+	list->owner = NULL;
 }
 EXPORT_SYMBOL(__hw_addr_init);
 
@@ -612,6 +630,7 @@ void __hw_addr_list_reconcile(struct netdev_hw_addr_list *real_list,
 				__hw_addr_insert(real_list, ref_ha,
 						 addr_len);
 				real_list->count++;
+				__hw_addr_changed(real_list);
 			}
 			continue;
 		}
@@ -623,6 +642,7 @@ void __hw_addr_list_reconcile(struct netdev_hw_addr_list *real_list,
 			list_del_rcu(&real_ha->list);
 			kfree_rcu(real_ha, rcu_head);
 			real_list->count--;
+			__hw_addr_changed(real_list);
 		}
 	}
 
@@ -1177,6 +1197,7 @@ EXPORT_SYMBOL(dev_mc_flush);
 void dev_mc_init(struct net_device *dev)
 {
 	__hw_addr_init(&dev->mc);
+	dev->mc.owner = dev;
 }
 EXPORT_SYMBOL(dev_mc_init);
 
#6 lore raw

Re: [PATCH net-next v7 2/5] net: add a generation counter for dev->mc changes

Quoted 11 lines

There are several lists owned by a device (dev->dev_addrs, dev->uc, dev->mc, dev->rx_mode_addr_cache), the name 'owner' does not reflect that it is set only when the list is dev->mc.
I wonder if the name should be changed or if __hw_addr_init() should be updated to always set owner when the list is owned by a device. list->count and dev_mc_genid evolves together. Perhaps some helpers would be less error-prone for future patches?

__hw_addr_count_inc()
__hw_addr_count_dec()
__hw_addr_count_reset()

Thanks for the suggestion, will update based on the suggestion in patchset v8.

Thanks,

Yuyang

#7 lore raw

Re: [PATCH net-next v7 2/5] net: add a generation counter for dev->mc changes

Quoted 3 lines
Quoted 1 line

+ dev->rx_mode_addr_cache.owner = dev;

There's no need to set this?

No, only dev->mc is looked at. It was set for all the lists of a device after Nicolas's comment on v7:

```
  There are several lists owned by a device (dev->dev_addrs, dev->uc,
  dev->mc, dev->rx_mode_addr_cache), the name 'owner' does not reflect
  that it is set only when the list is dev->mc.
  I wonder if the name should be changed or if __hw_addr_init() should
  be updated to always set owner when the list is owned by a device.
```

v10 takes the other option: the field becomes mc_dev, set for dev->mc only.

#8 lore raw

PATCH v7 3/5 net: add AF_PACKET multicast dumps

In reply to Yuyang Huang

RTM_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. cb->seq is sampled from the dev->mc generation counter and dev_base_seq under the address lock of each device and again when a round ends, so a change since the previous device or dump round sets NLM_F_DUMP_INTR, also when the last round dumps nothing because the device it stopped at is gone.

---
 include/linux/netdevice.h    |   1 +
 include/uapi/linux/if_addr.h |   1 +
 net/core/dev_addr_lists.c    | 195 +++++++++++++++++++++++++++++++++++
 net/core/rtnetlink.c         |   2 +
 4 files changed, 199 insertions(+)
include/linux/netdevice.h +1 -0
index be3804cdbb2d..204d56a19583 100644
@@ -5165,6 +5165,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
index 7fb630b7fe31..0a1ad9ebb47b 100644
@@ -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
 
 struct ifa_cacheinfo {
 	__u32	ifa_prefered;
net/core/dev_addr_lists.c +195 -0

209 lines not on this page · raw message

net/core/rtnetlink.c +2 -0
index e3444fd24061..204dc9040e3c 100644
@@ -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
Reviewed-by on v7 3/5
#9 lore raw

Re: [PATCH net-next v7 3/5] net: add AF_PACKET multicast dumps

Le 24/09/2026 à 03:15, Yuyang Huang a écrit :

Quoted 36 lines

RTM_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. cb->seq is sampled from the dev->mc generation counter and dev_base_seq under the address lock of each device and again when a round ends, so a change since the previous device or dump round sets NLM_F_DUMP_INTR, also when the last round dumps nothing because the device it stopped at is gone.

Quoted 1 line
#10 lore raw

PATCH v7 4/5 netlink: specs: rt-addr: document AF_PACKET multicast dumps

In reply to Yuyang Huang

Add 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
index 17ead2203451..dc5c0ce259ba 100644
@@ -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
#11 lore raw

PATCH v7 5/5 selftests: net: test AF_PACKET multicast dumps

In reply to Yuyang Huang

Dump 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
index dc8c77db4897..5534ade056a5 100755
@@ -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
#12 lore raw
In reply to Yuyang Huang

Sashiko's comments for v7 arrived late:
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260924011554.3494-1-sigefriedhyy%40gmail.com

I went through all of them. The only one I will take is the spec doc wording in patch 4: the ifa-index filter and the zero header fields are unconditional for AF_PACKET only, AF_INET and AF_INET6 apply them with NETLINK_GET_STRICT_CHK. I will fix that in v9.

The others are either pre-existing, already discussed in earlier versions, or not an issue.