netlink: support dumping IPv4 multicast addresses
Subsystems (5)
PATCH v5 1/1 netlink: support dumping IPv4 multicast addresses
Extended RTM_GETMULTICAST to support dumping joined IPv4 multicast addresses, in addition to the existing IPv6 functionality. This allows userspace applications to retrieve both IPv4 and IPv6 multicast addresses through similar netlink command and then monitor future changes by registering to RTNLGRP_IPV4_MCADDR and RTNLGRP_IPV6_MCADDR.
This change includes a new ynl based selftest case. To run the test, execute the following command:
$ vng -v --user root --cpus 16 -- \
make -C tools/testing/selftests TARGETS=net \
TEST_PROGS=rtnetlink.py TEST_GEN_PROGS="" run_tests---
Changelog since v4:
- Fixes a bug where fillargs->event was not initialized in the code path for dumping ifaddr.
- Fixes a bug where reply messages contain the wrong sequence number.
- Minor style fixes.
- Adds a ynl selftest.
Changelog since v3:
- Refactor in_dev_dump_addr() to break down the logic into two separate functions to simplify the logic.
Changelog since v2:
- Fix checkpatch.pl warnings.
- Remove one redundant EXPORT_SYMBOL().
Changelog since v1:
- Minor style fixes.
- Use for_each_pmc_rcu() instead of for_each_pmc_rtnl().
Documentation/netlink/specs/rt_link.yaml | 70 ++++++++++++++++++++ include/linux/igmp.h | 14 ++++ net/ipv4/devinet.c | 82 ++++++++++++++++++++---- net/ipv4/igmp.c | 13 ++-- tools/testing/selftests/net/Makefile | 1 + tools/testing/selftests/net/rtnetlink.py | 30 +++++++++ 6 files changed, 192 insertions(+), 18 deletions(-) create mode 100755 tools/testing/selftests/net/rtnetlink.py
Documentation/netlink/specs/rt_link.yaml +70 -0
@@ -92,6 +92,41 @@ definitions: - name: ifi-change type: u32 + - + name: ifaddrmsg + type: struct + members: + - + name: ifa-family + type: u8 + - + name: ifa-prefixlen + type: u8 + - + name: ifa-flags + type: u8 + - + name: ifa-scope + type: u8 + - + name: ifa-index + type: u32 + - + name: ifacacheinfo + type: struct + members: + - + name: ifa-prefered + type: u32 + - + name: ifa-valid + type: u32 + - + name: cstamp + type: u32 + - + name: tstamp + type: u32 - name: ifla-bridge-id type: struct
@@ -2253,6 +2288,18 @@ attribute-sets: - name: tailroom type: u16 + - + name: ifmcaddr-attrs + attributes: + - + name: addr + type: binary + value: 7 + - + name: cacheinfo + type: binary + struct: ifacacheinfo + value: 6 sub-messages: -
@@ -2493,6 +2540,29 @@ operations: reply: value: 92 attributes: *link-stats-attrs + - + name: getmaddrs + doc: Get / dump IPv4/IPv6 multicast addresses. + attribute-set: ifmcaddr-attrs + fixed-header: ifaddrmsg + do: + request: + value: 58 + attributes: + - ifa-family + - ifa-index + reply: + value: 58 + attributes: &mcaddr-attrs + - addr + - cacheinfo + dump: + request: + value: 58 + - ifa-family + reply: + value: 58 + attributes: *mcaddr-attrs mcast-groups: list:
include/linux/igmp.h +14 -0
@@ -16,6 +16,7 @@#include <linux/ip.h> #include <linux/refcount.h> #include <linux/sockptr.h> +#include <net/addrconf.h> #include <uapi/linux/igmp.h> static inline struct igmphdr *igmp_hdr (const struct sk_buff *skb)
@@ -92,6 +93,16 @@ struct ip_mc_list {struct rcu_head rcu; }; +struct inet_fill_args { +u32 portid; +u32 seq; +int event; +unsigned int flags; +int netnsid; +int ifindex; +enum addr_type_t type; +}; +/* V3 exponential field decoding */ #define IGMPV3_MASK (value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))#define IGMPV3_EXP (thresh, nbmant, nbexp, value) \
@@ -142,4 +153,7 @@ extern void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr,extern void ip_mc_inc_group (struct in_device *in_dev,__be32 addr);int ip_mc_check_igmp (struct sk_buff *skb); +int inet_fill_ifmcaddr (struct sk_buff *skb,struct net_device *dev, +const struct ip_mc_list *im, +struct inet_fill_args *args);#endif
net/ipv4/devinet.c +68 -14
125 lines not on this page · raw message
net/ipv4/igmp.c +9 -4
@@ -1432,14 +1432,16 @@ static void ip_mc_hash_remove(struct in_device *in_dev, *mc_hash = im->next_hash; } -static int inet_fill_ifmcaddr (struct sk_buff *skb,struct net_device *dev, -const struct ip_mc_list *im,int event) +int inet_fill_ifmcaddr (struct sk_buff *skb,struct net_device *dev, +const struct ip_mc_list *im, +struct inet_fill_args *args) {struct ifa_cacheinfo ci;struct ifaddrmsg *ifm;struct nlmsghdr *nlh; - nlh =nlmsg_put (skb,0 ,0 , event,sizeof (struct ifaddrmsg ),0 ); + nlh =nlmsg_put (skb, args->portid, args->seq, args->event, +sizeof (struct ifaddrmsg ), args->flags);if (!nlh)return -EMSGSIZE ;
@@ -1468,6 +1470,9 @@ static int inet_fill_ifmcaddr(struct sk_buff *skb, struct net_device *dev,static void inet_ifmcaddr_notify (struct net_device *dev,const struct ip_mc_list *im,int event) { +struct inet_fill_args fillargs = { + .event = event, + };struct net *net =dev_net (dev);struct sk_buff *skb;int err = -ENOMEM ;
@@ -1479,7 +1484,7 @@ static void inet_ifmcaddr_notify(struct net_device *dev,if (!skb) goto error; - err =inet_fill_ifmcaddr (skb, dev, im, event); + err =inet_fill_ifmcaddr (skb, dev, im, &fillargs);if (err <0 ) {WARN_ON_ONCE (err == -EMSGSIZE );nlmsg_free (skb);
tools/testing/selftests/net/Makefile +1 -0
@@ -36,6 +36,7 @@ TEST_PROGS += cmsg_so_priority.sh TEST_PROGS += cmsg_time.sh cmsg_ipv6.sh TEST_PROGS += netns-name.sh TEST_PROGS += nl_netdev.py +TEST_PROGS += rtnetlink.py TEST_PROGS += srv6_end_dt46_l3vpn_test.sh TEST_PROGS += srv6_end_dt4_l3vpn_test.sh TEST_PROGS += srv6_end_dt6_l3vpn_test.sh
tools/testing/selftests/net/rtnetlink.py new file +30 -0
@@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +from lib.py import ksft_exit, ksft_run, ksft_ge, RtnlFamily +import socket + +IPV4_ALL_HOSTS_MULTICAST = b'\xe0\x00\x00\x01' + +def dump_mcaddr_check(rtnl: RtnlFamily) -> None: + """ + Verify that at least one interface has the IPv4 all-hosts multicast address. + At least the loopback interface should have this address. + """ + + addresses = rtnl.getmaddrs({"ifa-family": socket.AF_INET}, dump=True) + + all_host_multicasts = [ + addr for addr in addresses if addr['addr'] == IPV4_ALL_HOSTS_MULTICAST + ] + + ksft_ge(len(all_host_multicasts), 1, + "No interface found with the IPv4 all-hosts multicast address") + +def main() -> None: + rtnl = RtnlFamily() + ksft_run([dump_mcaddr_check], args=(rtnl, )) + ksft_exit() + +if __name__ == "__main__": + main()
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
Paolo Abeni
maintainerQuoted 6 lines · Yuyang Huang
Extended
RTM_GETMULTICASTto support dumping joined IPv4 multicast addresses, in addition to the existing IPv6 functionality. This allows userspace applications to retrieve both IPv4 and IPv6 multicast addresses through similar netlink command and then monitor future changes by registering toRTNLGRP_IPV4_MCADDRandRTNLGRP_IPV6_MCADDR.
Quoted 6 lines
This change includes a new ynl based selftest case. To run the test, execute the following command:
$ vng -v --user root --cpus 16 -- \ make -C tools/testing/selftests TARGETS=net \ TEST_PROGS=rtnetlink.py TEST_GEN_PROGS="" run_tests
Thanks for including the test!
Quoted 85 lines
Documentation/netlink/specs/rt_link.yaml +63
-0@@ -92,6 +92,41 @@ definitions: - name: ifi-change type: u32 + - + name: ifaddrmsg + type: struct + members: + - + name: ifa-family + type: u8 + - + name: ifa-prefixlen + type: u8 + - + name: ifa-flags + type: u8 + - + name: ifa-scope + type: u8 + - + name: ifa-index + type: u32 + - + name: ifacacheinfo + type: struct + members: + - + name: ifa-prefered + type: u32 + - + name: ifa-valid + type: u32 + - + name: cstamp + type: u32 + - + name: tstamp + type: u32 - name: ifla-bridge-id type: struct@@ -2253,6 +2288,18 @@ attribute-sets: - name: tailroom type: u16 + - + name: ifmcaddr-attrs + attributes: + - + name: addr + type: binary + value: 7 + - + name: cacheinfo + type: binary + struct: ifacacheinfo + value: 6 sub-messages: -@@ -2493,6 +2540,29 @@ operations: reply: value: 92 attributes: *link-stats-attrs + - + name: getmaddrs + doc: Get / dump IPv4/IPv6 multicast addresses. + attribute-set: ifmcaddr-attrs + fixed-header: ifaddrmsg + do: + request: + value: 58 + attributes: + - ifa-family + - ifa-index + reply: + value: 58 + attributes: &mcaddr-attrs + - addr + - cacheinfo
Documentation/netlink/specs/rt_link.yaml operations: · line 2568
Quoted 10 lines
Documentation/netlink/specs/rt_link.yaml +7
-0+ dump: + request: + value: 58 + - ifa-family + reply: + value: 58 + attributes: *mcaddr-attrs mcast-groups: list:
IMHO the test case itself should land to a separate patch.
Quoted 15 lines
include/linux/igmp.h +1
-0@@ -16,6 +16,7 @@#include <linux/ip.h> #include <linux/refcount.h> #include <linux/sockptr.h> +#include <net/addrconf.h> #include <uapi/linux/igmp.h> static inline struct igmphdr *igmp_hdr (const struct sk_buff *skb)@@ -92,6 +93,16 @@ struct ip_mc_list {struct rcu_head rcu; };
include/linux/igmp.h struct ip_mc_list { · line 104
Quoted 10 lines
include/linux/igmp.h +9
-0+struct inet_fill_args { +u32 portid; +u32 seq; +int event; +unsigned int flags; +int netnsid; +int ifindex; +enum addr_type_t type; +};
Why moving the struct definition here? IMHO addrconf.h is better suited and will avoid additional headers dep.
include/linux/igmp.h static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb, · line 1903
Quoted 11 lines
include/linux/igmp.h +7
-0@@ -1874,6 +1894,23 @@ static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb,return err; } +static int in_dev_dump_addr (struct in_device *in_dev,struct sk_buff *skb, +struct netlink_callback *cb,int *s_ip_idx, +struct inet_fill_args *fillargs) +{ +switch (fillargs->type) { +case UNICAST_ADDR : + fillargs->event =RTM_NEWADDR ;
I think that adding an additional argument for 'event' to inet_dump_addr() would simplify the code a bit.
include/linux/igmp.h static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb, · line 1910
Quoted 7 lines
include/linux/igmp.h +7
-0+return in_dev_dump_ifaddr (in_dev, skb, cb, s_ip_idx, fillargs); +case MULTICAST_ADDR : + fillargs->event =RTM_GETMULTICAST ; +return in_dev_dump_ifmcaddr (in_dev, skb, cb, s_ip_idx, + fillargs); +default : +return 0 ;
Why not erroring out on unknown types? should never happen, right?
include/linux/igmp.h static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) · line 1994
Quoted 9 lines
include/linux/igmp.h +5
-0@@ -1949,6 +1987,20 @@ static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)return err; } +static int inet_dump_ifaddr (struct sk_buff *skb,struct netlink_callback *cb) +{ +enum addr_type_t type =UNICAST_ADDR ; + +return inet_dump_addr (skb, cb, type);
You can avoid the local variable usage.
include/linux/igmp.h static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) · line 2001
Quoted 7 lines
include/linux/igmp.h +7
-0+} + +static int inet_dump_ifmcaddr (struct sk_buff *skb,struct netlink_callback *cb) +{ +enum addr_type_t type =MULTICAST_ADDR ; + +return inet_dump_addr (skb, cb, type);
Same here.
Thanks!
Paolo
Hi Paolo
Thanks for the review feedback, I will adjust them in the patch v6.
Quoted 2 lines
Why moving the struct definition here? IMHO
addrconf.his better suited and will avoid additional headers dep.
The struct inet_fill_args is moved from devinet.c to igmp.h to make it usable in both devinet.c and igmp.c.
I feel it is incorrect to move struct inet_fill_args to addrconf.h because that file contains IPv6 specific definition and it also contains struct inet6_fill_args. After refactoring, I will remove the usage of enum addr_type_t type, so we don't need to import addrconf.h anymore.
Thanks,
Yuyang
Quoted 195 lines · Paolo Abeni
Quoted 6 lines · Yuyang Huang
Extended
RTM_GETMULTICASTto support dumping joined IPv4 multicast addresses, in addition to the existing IPv6 functionality. This allows userspace applications to retrieve both IPv4 and IPv6 multicast addresses through similar netlink command and then monitor future changes by registering toRTNLGRP_IPV4_MCADDRandRTNLGRP_IPV6_MCADDR.Quoted 6 lines
This change includes a new ynl based selftest case. To run the test, execute the following command:
$ vng -v --user root --cpus 16 -- \ make -C tools/testing/selftests TARGETS=net \ TEST_PROGS=rtnetlink.py TEST_GEN_PROGS="" run_testsThanks for including the test!
Quoted 85 lines
Documentation/netlink/specs/rt_link.yaml +63
-0@@ -92,6 +92,41 @@ definitions: - name: ifi-change type: u32 + - + name: ifaddrmsg + type: struct + members: + - + name: ifa-family + type: u8 + - + name: ifa-prefixlen + type: u8 + - + name: ifa-flags + type: u8 + - + name: ifa-scope + type: u8 + - + name: ifa-index + type: u32 + - + name: ifacacheinfo + type: struct + members: + - + name: ifa-prefered + type: u32 + - + name: ifa-valid + type: u32 + - + name: cstamp + type: u32 + - + name: tstamp + type: u32 - name: ifla-bridge-id type: struct@@ -2253,6 +2288,18 @@ attribute-sets: - name: tailroom type: u16 + - + name: ifmcaddr-attrs + attributes: + - + name: addr + type: binary + value: 7 + - + name: cacheinfo + type: binary + struct: ifacacheinfo + value: 6 sub-messages: -@@ -2493,6 +2540,29 @@ operations: reply: value: 92 attributes: *link-stats-attrs + - + name: getmaddrs + doc: Get / dump IPv4/IPv6 multicast addresses. + attribute-set: ifmcaddr-attrs + fixed-header: ifaddrmsg + do: + request: + value: 58 + attributes: + - ifa-family + - ifa-index + reply: + value: 58 + attributes: &mcaddr-attrs + - addr + - cacheinfoDocumentation/netlink/specs/rt_link.yaml operations: · line 2568
Quoted 10 lines
Documentation/netlink/specs/rt_link.yaml +7
-0+ dump: + request: + value: 58 + - ifa-family + reply: + value: 58 + attributes: *mcaddr-attrs mcast-groups: list:IMHO the test case itself should land to a separate patch.
Quoted 15 lines
include/linux/igmp.h +1
-0@@ -16,6 +16,7 @@#include <linux/ip.h> #include <linux/refcount.h> #include <linux/sockptr.h> +#include <net/addrconf.h> #include <uapi/linux/igmp.h> static inline struct igmphdr *igmp_hdr (const struct sk_buff *skb)@@ -92,6 +93,16 @@ struct ip_mc_list {struct rcu_head rcu; };include/linux/igmp.h struct ip_mc_list { · line 104
Quoted 10 lines
include/linux/igmp.h +9
-0+struct inet_fill_args { +u32 portid; +u32 seq; +int event; +unsigned int flags; +int netnsid; +int ifindex; +enum addr_type_t type; +};Why moving the struct definition here? IMHO
addrconf.his better suited and will avoid additional headers dep.include/linux/igmp.h static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb, · line 1903
Quoted 11 lines
include/linux/igmp.h +7
-0@@ -1874,6 +1894,23 @@ static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb,return err; } +static int in_dev_dump_addr (struct in_device *in_dev,struct sk_buff *skb, +struct netlink_callback *cb,int *s_ip_idx, +struct inet_fill_args *fillargs) +{ +switch (fillargs->type) { +case UNICAST_ADDR : + fillargs->event =RTM_NEWADDR ;I think that adding an additional argument for 'event' to
inet_dump_addr()would simplify the code a bit.include/linux/igmp.h static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb, · line 1910
Quoted 7 lines
include/linux/igmp.h +7
-0+return in_dev_dump_ifaddr (in_dev, skb, cb, s_ip_idx, fillargs); +case MULTICAST_ADDR : + fillargs->event =RTM_GETMULTICAST ; +return in_dev_dump_ifmcaddr (in_dev, skb, cb, s_ip_idx, + fillargs); +default : +return 0 ;Why not erroring out on unknown types? should never happen, right?
include/linux/igmp.h static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) · line 1994
Quoted 9 lines
include/linux/igmp.h +5
-0@@ -1949,6 +1987,20 @@ static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)return err; } +static int inet_dump_ifaddr (struct sk_buff *skb,struct netlink_callback *cb) +{ +enum addr_type_t type =UNICAST_ADDR ; + +return inet_dump_addr (skb, cb, type);You can avoid the local variable usage.
include/linux/igmp.h static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) · line 2001
Quoted 7 lines
include/linux/igmp.h +7
-0+} + +static int inet_dump_ifmcaddr (struct sk_buff *skb,struct netlink_callback *cb) +{ +enum addr_type_t type =MULTICAST_ADDR ; + +return inet_dump_addr (skb, cb, type);Same here.
Thanks!
Paolo
Paolo Abeni
maintainerQuoted 7 lines · Yuyang Huang
Hi Paolo
Thanks for the review feedback, I will adjust them in the patch v6.
Quoted 2 lines
Why moving the struct definition here? IMHO
addrconf.his better suited and will avoid additional headers dep.
Quoted 7 lines
The
struct inet_fill_argsis moved fromdevinet.ctoigmp.hto make it usable in bothdevinet.candigmp.c.
I feel it is incorrect to movestruct inet_fill_argstoaddrconf.hbecause that file contains IPv6 specific definition and it also containsstruct inet6_fill_args. After refactoring, I will remove the usage of enumaddr_type_ttype, so we don't need to importaddrconf.hanymore.
Do you mean that in the next iteration you are going to remove the 'enum addr_type_t type' filed from struct inet_fill_args? That would be good.
As per the header file, I tend to think that the better choice in this case would probably be creating a new one.
/P
Quoted 2 lines
Do you mean that in the next iteration you are going to remove the 'enum
addr_type_ttype' filed from structinet_fill_args? That would be good.
Sorry, I already sent out the v6 patch. I should wait for more time. PTAL at the v6 patch thread and we can continue the discussion there.
Quoted 2 lines
As per the header file, I tend to think that the better choice in this case would probably be creating a new one.
Having the struct inet_fill_args in igmp.h will make the IPv4 logic consistent with IPv6 logic.
IPv4: struct inet_fill_args in igmp.h and use it in igmp.c/devinet.c
IPv6: ``struct inet6_fill_args` in addrconf.h and use it in mld.c/addrconf.c
Thanks,
Yuyang
Quoted 25 lines · Paolo Abeni
Quoted 7 lines · Yuyang Huang
Hi Paolo
Thanks for the review feedback, I will adjust them in the patch v6.
Quoted 2 lines
Why moving the struct definition here? IMHO
addrconf.his better suited and will avoid additional headers dep.Quoted 7 lines
The
struct inet_fill_argsis moved fromdevinet.ctoigmp.hto make it usable in bothdevinet.candigmp.c.
I feel it is incorrect to movestruct inet_fill_argstoaddrconf.hbecause that file contains IPv6 specific definition and it also containsstruct inet6_fill_args. After refactoring, I will remove the usage of enumaddr_type_ttype, so we don't need to importaddrconf.hanymore.Do you mean that in the next iteration you are going to remove the 'enum
addr_type_ttype' filed from structinet_fill_args? That would be good.As per the header file, I tend to think that the better choice in this case would probably be creating a new one.
/P