From patchwork Tue Dec 12 10:17:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13488879 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="xCrqYk7f" Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4381F92 for ; Tue, 12 Dec 2023 02:17:51 -0800 (PST) Received: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-9fa2714e828so716895766b.1 for ; Tue, 12 Dec 2023 02:17:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1702376269; x=1702981069; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Gvq7X9IR33uLz+NOli+ik7mMtRRti1WP//ME+EWKFLc=; b=xCrqYk7fLNRCFvRTY1mn9WUWK9a8uLzwvbG9WGs4snOfgF+2KZb/A+RgIbuY5trNR5 qUOPAgCzuMMHZ9koW7MmtVskY7NS7HebFCREx/BMS0IBdfM5mFZdgcGRkl8PSijf/b1N W9yNFr1Et8bUyqdiRyXOAkCR8XXHyOLkSguJ67rZs5Asd1ytE8a3yePaL/IhDuYtI0mg gUjmJRw/QsQ6xRN3Ccp2awZsUCy/XCYe1ZnJjBWNAmFKn5R4X6yTsZpd9bXYgJ+a/Fgm Q51m3R/ZD1fUdkMnk0OlzWejaGC8+3y1Na6aolnjqKWK/Zf5fKzKw2h+LVDBQxPrfOQD ZvPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702376269; x=1702981069; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Gvq7X9IR33uLz+NOli+ik7mMtRRti1WP//ME+EWKFLc=; b=AQXw+u6eVnMYbQaGCDub+ebeuWZQ0U063bmE2KWZCpub4unJ4yRodImLJER7qkoKT3 M1ce9BHMbAAXDJlDOkkGmDETjl+vBUHO411Cirwy7dsdSw3Xnk6KQ0DUW899+uxWcPkE Ognc5t0XdnmAoPUHcizzcYJhnI0j6YkuLAZJ+0qsQ1I7Tg3AiI5F9ejs6/kQgF7atsmQ j4mcD+hws6XU4Unewe6QcXQJq27xyGp8k6ADdebZcFMAJUvM0cdrzHxlToBfDjSBtMvQ cDesjg/E7ZJMp0PIdpSu4kF4XIxGHRBYDmwOTQamEO5q5KNIVqzK2nbj3aZgenwbcenT miAA== X-Gm-Message-State: AOJu0YzjVaieTgG8a60HdKxQPriVRz4eiYZePILMOjg7wk9MBJL7IcS1 YG5MM3kMFMZLYGOL98XYVKueG9mqmWv/z/jU70U= X-Google-Smtp-Source: AGHT+IFLXzl33h/5IQ56s7iw09G7bVbx8QsiX4NhwJcfOKMBMwuvG7N4KyH0ECzwgIv1y7rgbfAKvA== X-Received: by 2002:a17:906:184:b0:a19:a19b:55ce with SMTP id 4-20020a170906018400b00a19a19b55cemr2655303ejb.94.1702376269749; Tue, 12 Dec 2023 02:17:49 -0800 (PST) Received: from localhost (host-213-179-129-39.customer.m-online.net. [213.179.129.39]) by smtp.gmail.com with ESMTPSA id ll9-20020a170907190900b00a1da2f7c1d8sm6026586ejc.77.2023.12.12.02.17.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 02:17:49 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, jhs@mojatatu.com, johannes@sipsolutions.net, andriy.shevchenko@linux.intel.com, amritha.nambiar@intel.com, sdf@google.com, horms@kernel.org, przemyslaw.kitszel@intel.com Subject: [patch net-next v6 7/9] genetlink: introduce helpers to do filtered multicast Date: Tue, 12 Dec 2023 11:17:34 +0100 Message-ID: <20231212101736.1112671-8-jiri@resnulli.us> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231212101736.1112671-1-jiri@resnulli.us> References: <20231212101736.1112671-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jiri Pirko Currently it is possible for netlink kernel user to pass custom filter function to broadcast send function netlink_broadcast_filtered(). However, this is not exposed to multicast send and to generic netlink users. Extend the api and introduce a netlink helper nlmsg_multicast_filtered() and a generic netlink helper genlmsg_multicast_netns_filtered() to allow generic netlink families to specify filter function while sending multicast messages. Signed-off-by: Jiri Pirko --- v1->v2: - used netlink_filter_fn introduce by the previous patch - added return comments to silence scripts/kernel-doc warnings --- include/net/genetlink.h | 35 +++++++++++++++++++++++++++++++---- include/net/netlink.h | 31 +++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/include/net/genetlink.h b/include/net/genetlink.h index 7df3ca11070a..3dedb6268d5e 100644 --- a/include/net/genetlink.h +++ b/include/net/genetlink.h @@ -453,6 +453,35 @@ static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr) nlmsg_cancel(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN); } +/** + * genlmsg_multicast_netns_filtered - multicast a netlink message + * to a specific netns with filter + * function + * @family: the generic netlink family + * @net: the net namespace + * @skb: netlink message as socket buffer + * @portid: own netlink portid to avoid sending to yourself + * @group: offset of multicast group in groups array + * @flags: allocation flags + * @filter: filter function + * @filter_data: filter function private data + * + * Return: 0 on success, negative error code for failure. + */ +static inline int +genlmsg_multicast_netns_filtered(const struct genl_family *family, + struct net *net, struct sk_buff *skb, + u32 portid, unsigned int group, gfp_t flags, + netlink_filter_fn filter, + void *filter_data) +{ + if (WARN_ON_ONCE(group >= family->n_mcgrps)) + return -EINVAL; + group = family->mcgrp_offset + group; + return nlmsg_multicast_filtered(net->genl_sock, skb, portid, group, + flags, filter, filter_data); +} + /** * genlmsg_multicast_netns - multicast a netlink message to a specific netns * @family: the generic netlink family @@ -466,10 +495,8 @@ static inline int genlmsg_multicast_netns(const struct genl_family *family, struct net *net, struct sk_buff *skb, u32 portid, unsigned int group, gfp_t flags) { - if (WARN_ON_ONCE(group >= family->n_mcgrps)) - return -EINVAL; - group = family->mcgrp_offset + group; - return nlmsg_multicast(net->genl_sock, skb, portid, group, flags); + return genlmsg_multicast_netns_filtered(family, net, skb, portid, + group, flags, NULL, NULL); } /** diff --git a/include/net/netlink.h b/include/net/netlink.h index 28039e57070a..c19ff921b661 100644 --- a/include/net/netlink.h +++ b/include/net/netlink.h @@ -1087,27 +1087,50 @@ static inline void nlmsg_free(struct sk_buff *skb) } /** - * nlmsg_multicast - multicast a netlink message + * nlmsg_multicast_filtered - multicast a netlink message with filter function * @sk: netlink socket to spread messages to * @skb: netlink message as socket buffer * @portid: own netlink portid to avoid sending to yourself * @group: multicast group id * @flags: allocation flags + * @filter: filter function + * @filter_data: filter function private data + * + * Return: 0 on success, negative error code for failure. */ -static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, - u32 portid, unsigned int group, gfp_t flags) +static inline int nlmsg_multicast_filtered(struct sock *sk, struct sk_buff *skb, + u32 portid, unsigned int group, + gfp_t flags, + netlink_filter_fn filter, + void *filter_data) { int err; NETLINK_CB(skb).dst_group = group; - err = netlink_broadcast(sk, skb, portid, group, flags); + err = netlink_broadcast_filtered(sk, skb, portid, group, flags, + filter, filter_data); if (err > 0) err = 0; return err; } +/** + * nlmsg_multicast - multicast a netlink message + * @sk: netlink socket to spread messages to + * @skb: netlink message as socket buffer + * @portid: own netlink portid to avoid sending to yourself + * @group: multicast group id + * @flags: allocation flags + */ +static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb, + u32 portid, unsigned int group, gfp_t flags) +{ + return nlmsg_multicast_filtered(sk, skb, portid, group, flags, + NULL, NULL); +} + /** * nlmsg_unicast - unicast a netlink message * @sk: netlink socket to spread message to