From patchwork Tue Dec 17 06:31:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yuyang Huang X-Patchwork-Id: 13911170 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5AA2A170822 for ; Tue, 17 Dec 2024 06:31:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734417091; cv=none; b=RiGAAN6qdSNiOE0Wlc05+i+7s37wSNi57PqcrummWpfYYkxBY5fXVWHM09jkBqof/LfFf2MknCiU1Y+IVHaVB8t3l75DYMmC1N+VlENIw2P1BPi/q8jOdqJ8t94kJwhrlwdnGsgk23jhD/EHrvL6/3O00Koz29EHSHUfF9nkqwo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734417091; c=relaxed/simple; bh=bAOVHJtNkWkKQOWZCiarJvDyH2qGf0PMiFP2aKG3cbo=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=cEQByq4U09dSDxaRfLyPFSIIk/YUQO7m031pLWgMtMjbTXU5fPjI6dDVwl29WywWPaqyQtNfSaaChYBOMFTTJr5es9UrUL7g540dKkHVJxRurqghRJZtMPmsVuoPocspU9+6UQOGaBRATYP5wa4AeQWsae6QIABCkmQU/VIaPHQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuyanghuang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=kTspzMsr; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yuyanghuang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kTspzMsr" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-72726ced3f3so4407214b3a.2 for ; Mon, 16 Dec 2024 22:31:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734417089; x=1735021889; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:from:to:cc:subject:date:message-id:reply-to; bh=KHYdLK7xjtNv3cBlxF/V67gsirfObyAh3u6XCjLCPR8=; b=kTspzMsrIlppEATrdJieD15WLNCLJZQ+XzpyVsxDdpp31loxEUNP9G5VNNXZ8fl/QV 9rFf52h4jGk2rbwAXpfdnjEdd05vSjPqGVxGQCYzWhtkKFQircFhG7ujPGIQZDoWGabR dJuS7szaBDH5GHCvbYBlLc7plbkUJcxCEcTcuFJxslWS877k6kPhSm23af5O6+bGdQyJ 1wQi92f/JOspDQwBXq1h0kJZjjI6ibDlyKXHkbKjUolllWPWb7sE8T+U1B0lECUzWYwi 7/TvooMlqsRh6BSgOObomqbXc/OokVYp5jI0z7piBbNw+k+TQNTJ+fJzHbj0ODWumA+8 FVGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734417089; x=1735021889; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=KHYdLK7xjtNv3cBlxF/V67gsirfObyAh3u6XCjLCPR8=; b=aQLsIrbyzMM7WThVtg/7mEyRwdT7aBWzJn81+7zJ2v0SmfTDhHQ4euHvfLdDapNxth 1ODKNFSQI3vYrdjzovoCuqZf76ou11NXPlZhsP7dmHMNFsWkt32ROWhS40jRXDD4KZrm fry0WDlRfR8sEbr5B6okNvCTJgWv3MYsMfABcAbg0DsH6ek81ZC2nLTXfwTbYfkg45kH RPt2fysnZMrzlGIUuQaJif50xw8wQgSi/582ldQDdbHzaIceGbw/IW6Q4IUgx4iwwGBy 6QIRgkMvq9HVrXk/xenVobeB0FUSkwJIa0w65JH7shKYPbv3uPLaJd2+IYU9gBkmyRIY yrmQ== X-Forwarded-Encrypted: i=1; AJvYcCXNPXSzfvLGAw5t5RR6oCOUG7xJuL4c+ZgO9BfCGiTYkrTWs8QI9axZuTRYSWvobCMSxPuqfro=@vger.kernel.org X-Gm-Message-State: AOJu0Yw8Qxra8TiQrf1LbHBhiP5LdnPVj2WDmVsIknx0PQjDz6d6NxgW xevi5wyTxUMgdtA1St0ebjM2x+72W9f06furanA+pKM3NpxFZnnKWVbZhs/IiKvPtxde/dMwJC+ AMqJBO1bGBrLCpVHLY6kV/Q== X-Google-Smtp-Source: AGHT+IGaXBkW68E9G7IgdAchOngVilP1Ei9m4pxQ1VLOYC3i/LdPdeK3TOnyAX6BQMW+IQkugoS1+MJTUppnx/lDXw== X-Received: from pfbna41.prod.google.com ([2002:a05:6a00:3e29:b0:728:958a:e45c]) (user=yuyanghuang job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:301a:b0:725:ea30:aafc with SMTP id d2e1a72fcca58-7290c0dfcaemr24248171b3a.5.1734417089560; Mon, 16 Dec 2024 22:31:29 -0800 (PST) Date: Tue, 17 Dec 2024 15:31:24 +0900 Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241217063124.3766605-1-yuyanghuang@google.com> Subject: [PATCH net-next] netlink: support dumping IPv4 multicast addresses From: Yuyang Huang To: Yuyang Huang Cc: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , David Ahern , roopa@cumulusnetworks.com, jiri@resnulli.us, stephen@networkplumber.org, jimictw@google.com, prohr@google.com, liuhangbin@gmail.com, nicolas.dichtel@6wind.com, andrew@lunn.ch, netdev@vger.kernel.org, " =?utf-8?q?Maciej_=C5=BBenczykow?= =?utf-8?q?ski?= " , Lorenzo Colitti X-Patchwork-Delegate: kuba@kernel.org 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. Cc: Maciej Żenczykowski Cc: Lorenzo Colitti Signed-off-by: Yuyang Huang --- include/linux/igmp.h | 7 +++++ net/ipv4/devinet.c | 63 +++++++++++++++++++++++++++++++++++++------- net/ipv4/igmp.c | 14 ++++------ 3 files changed, 65 insertions(+), 19 deletions(-) diff --git a/include/linux/igmp.h b/include/linux/igmp.h index 073b30a9b850..1e4164f931d6 100644 --- a/include/linux/igmp.h +++ b/include/linux/igmp.h @@ -142,4 +142,11 @@ 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); +#define for_each_pmc_rtnl(in_dev, pmc) \ + for (pmc = rtnl_dereference(in_dev->mc_list); \ + pmc != NULL; \ + pmc = rtnl_dereference(pmc->next_rcu)) + +int inet_fill_ifmcaddr(struct sk_buff *skb, struct net_device *dev, + const struct ip_mc_list *im, int event, int flags); #endif diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c index c8b3cf5fba4c..d3ddb0586074 100644 --- a/net/ipv4/devinet.c +++ b/net/ipv4/devinet.c @@ -114,6 +114,7 @@ struct inet_fill_args { unsigned int flags; int netnsid; int ifindex; + enum addr_type_t type; }; #define IN4_ADDR_HSIZE_SHIFT 8 @@ -1850,21 +1851,44 @@ 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) { + struct ip_mc_list *im; struct in_ifaddr *ifa; int ip_idx = 0; int err; - in_dev_for_each_ifa_rcu(ifa, in_dev) { - if (ip_idx < *s_ip_idx) { + switch(fillargs->type) { + case UNICAST_ADDR: + fillargs->event = RTM_NEWADDR; + in_dev_for_each_ifa_rcu(ifa, in_dev) { + if (ip_idx < *s_ip_idx) { + ip_idx++; + continue; + } + err = inet_fill_ifaddr(skb, ifa, fillargs); + if (err < 0) + goto done; + + nl_dump_check_consistent(cb, nlmsg_hdr(skb)); ip_idx++; - continue; } - err = inet_fill_ifaddr(skb, ifa, fillargs); - if (err < 0) - goto done; + break; + case MULTICAST_ADDR: + for_each_pmc_rtnl(in_dev, im) { + if (ip_idx < *s_ip_idx) { + ip_idx++; + continue; + } + err = inet_fill_ifmcaddr(skb, in_dev->dev, im, + RTM_GETMULTICAST, NLM_F_MULTI); + if (err < 0) + goto done; - nl_dump_check_consistent(cb, nlmsg_hdr(skb)); - ip_idx++; + nl_dump_check_consistent(cb, nlmsg_hdr(skb)); + ip_idx++; + } + break; + default: + break; } err = 0; ip_idx = 0; @@ -1889,15 +1913,16 @@ static u32 inet_base_seq(const struct net *net) return res; } -static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) +static int inet_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, + enum addr_type_t type) { const struct nlmsghdr *nlh = cb->nlh; struct inet_fill_args fillargs = { .portid = NETLINK_CB(cb->skb).portid, .seq = nlh->nlmsg_seq, - .event = RTM_NEWADDR, .flags = NLM_F_MULTI, .netnsid = -1, + .type = type, }; struct net *net = sock_net(skb->sk); struct net *tgt_net = net; @@ -1949,6 +1974,22 @@ 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); +} + +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); +} + static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh, u32 portid) { @@ -2845,6 +2886,8 @@ static const struct rtnl_msg_handler devinet_rtnl_msg_handlers[] __initconst = { {.protocol = PF_INET, .msgtype = RTM_GETNETCONF, .doit = inet_netconf_get_devconf, .dumpit = inet_netconf_dump_devconf, .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED}, + {.owner = THIS_MODULE, .protocol = PF_INET, .msgtype = RTM_GETMULTICAST, + .dumpit = inet_dump_ifmcaddr, .flags = RTNL_FLAG_DUMP_UNLOCKED}, }; void __init devinet_init(void) diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c index 8a370ef37d3f..7e085bdc1c40 100644 --- a/net/ipv4/igmp.c +++ b/net/ipv4/igmp.c @@ -179,11 +179,6 @@ static void ip_ma_put(struct ip_mc_list *im) pmc != NULL; \ pmc = rcu_dereference(pmc->next_rcu)) -#define for_each_pmc_rtnl(in_dev, pmc) \ - for (pmc = rtnl_dereference(in_dev->mc_list); \ - pmc != NULL; \ - pmc = rtnl_dereference(pmc->next_rcu)) - static void ip_sf_list_clear_all(struct ip_sf_list *psf) { struct ip_sf_list *next; @@ -1432,14 +1427,14 @@ 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, int event, int flags) { 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, 0, 0, event, sizeof(struct ifaddrmsg), flags); if (!nlh) return -EMSGSIZE; @@ -1464,6 +1459,7 @@ static int inet_fill_ifmcaddr(struct sk_buff *skb, struct net_device *dev, nlmsg_end(skb, nlh); return 0; } +EXPORT_SYMBOL(inet_fill_ifmcaddr); static void inet_ifmcaddr_notify(struct net_device *dev, const struct ip_mc_list *im, int event) @@ -1477,7 +1473,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, event, 0); if (err < 0) { WARN_ON_ONCE(err == -EMSGSIZE); nlmsg_free(skb);