Rust netlink support + use in Rust Binder
Subsystems (5)
Alice Ryhl
maintainerv3 cover Rust netlink support + use in Rust Binder
The C Binder driver exposes messages over netlink when transactions fail, so that a userpace daemon can respond to processes with many failing transactions.
This patch series adds netlink support from Rust, then implements an equivalent API in Rust Binder.
As Binder only uses broadcast messages, I did not add support for other kinds of messages.
Based on char-misc-next.
---
Changes in v3:
- Fix spurious 'return' statements in Rust helpers (Patch 1).
- Sashiko review:
- Fix ynl_gen to handle empty multicast groups and correct multicast indexing (Patch 2).
- Fix transaction failed reply logic to report via Netlink inside reply_inner() (Patch 4).
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Make inclusion of to_pid conditional too.
- Add note about file name in second patch.
- Make it clear that the sk_buff wrapper is netlink-specific.
- Better handle bitfield in patch 1.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Alice Ryhl (3):
rust: netlink: add raw netlink abstraction
ynl_gen: generate Rust files from yaml files
rust_binder: add generated netlink.rs file
Carlos Llamas (1):
rust_binder: report netlink transactions
drivers/android/Kconfig | 2 +- drivers/android/binder/netlink.rs | 113 ++++++++++ drivers/android/binder/rust_binder_main.rs | 8 +- drivers/android/binder/thread.rs | 10 + drivers/android/binder/transaction.rs | 40 ++++ rust/bindings/bindings_helper.h | 3 + rust/helpers/genetlink.c | 46 ++++ rust/helpers/helpers.c | 1 + rust/kernel/lib.rs | 1 + rust/kernel/netlink.rs | 329 +++++++++++++++++++++++++++++ rust/uapi/uapi_helper.h | 1 + tools/net/ynl/pyynl/ynl_gen_c.py | 139 +++++++++++- tools/net/ynl/ynl-regen.sh | 2 +- 13 files changed, 690 insertions(+), 5 deletions(-)
---
base-commit: 0990a71f678aa0f045f2c126b39b6b581844d3b0
change-id: 20260306-binder-netlink-c82110b2fb74
Best regards,
Signature · 1 line
Alice Ryhl <[email protected]>
Alice Ryhl
maintainerPATCH v3 1/4 rust: netlink: add raw netlink abstraction
This implements a safe and relatively simple API over the netlink API, that allows you to add different attributes to a netlink message and broadcast it. As the first user of this API only makes use of broadcast, only broadcast messages are supported here.
This API is intended to be safe and to be easy to use in *generated* code. This is because netlink is generally used with yaml files that describe the underlying API, and the python generator outputs C code (or, soon, Rust code) that lets you use the API more easily. So for example, if there is a string field, the code generator will output a method that internall calls put_string() with the right attr type.
--- rust/bindings/bindings_helper.h | 3 + rust/helpers/genetlink.c | 46 ++++++ rust/helpers/helpers.c | 1 + rust/kernel/lib.rs | 1 + rust/kernel/netlink.rs | 329 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 380 insertions(+)
rust/bindings/bindings_helper.h +3 -0
@@ -88,6 +88,8 @@#include <linux/wait.h> #include <linux/workqueue.h> #include <linux/xarray.h> +#include <net/genetlink.h> +#include <net/netlink.h> #include <trace/events/rust_sample.h> /*
@@ -105,6 +107,7 @@const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN =ARCH_SLAB_MINALIGN ;const size_t RUST_CONST_HELPER_ARCH_KMALLOC_MINALIGN =ARCH_KMALLOC_MINALIGN ;const size_t RUST_CONST_HELPER_PAGE_SIZE =PAGE_SIZE ; +const size_t RUST_CONST_HELPER_GENLMSG_DEFAULT_SIZE =GENLMSG_DEFAULT_SIZE ;const gfp_t RUST_CONST_HELPER_GFP_ATOMIC =GFP_ATOMIC ;const gfp_t RUST_CONST_HELPER_GFP_KERNEL =GFP_KERNEL ;const gfp_t RUST_CONST_HELPER_GFP_KERNEL_ACCOUNT =GFP_KERNEL_ACCOUNT ;
rust/helpers/genetlink.c new file +46 -0
46 lines not on this page · raw message
rust/helpers/helpers.c +1 -0
@@ -32,6 +32,7 @@#include "err.c" #include "irq.c" #include "fs.c" +#include "genetlink.c" #include "io.c" #include "jump_label.c" #include "kunit.c"
rust/kernel/lib.rs +1 -0
@@ -122,6 +122,7 @@pub mod module_param;#[cfg( CONFIG_NET )] pub mod net; +pub mod netlink;pub mod num;pub mod of;#[cfg( CONFIG_PM_OPP )]
rust/kernel/netlink.rs new file +329 -0
329 lines not on this page · raw message
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
Alice Ryhl
maintainerPATCH v3 2/4 ynl_gen: generate Rust files from yaml files
In reply to Alice RyhlTo generate netlink frames from Rust code easily, generate Rust libraries with methods for generating different netlink messages as appropriate.
The new 'rust' type corresponds to a Rust version of the C target 'kernel'. There is no Rust version of the 'uapi' target since Rust code exports its uapi via C headers - choice of language is opaque to userspace.
This logic is kept in the existing ynl_gen_c.py file to reuse CodeWriter and other shared pieces of logic in the existing python file. This has the disadvantage that the gen_c part of the name is now wrong, as it also generates Rust. One possible solution to this could be to rename the file.
--- tools/net/ynl/pyynl/ynl_gen_c.py | 139 ++++++++++++++++++++++++++++++++++++++- tools/net/ynl/ynl-regen.sh | 2 +- 2 files changed, 139 insertions(+), 2 deletions(-)
tools/net/ynl/pyynl/ynl_gen_c.py +138 -1
@@ -19,6 +19,7 @@ import pathlib import os import re import shutil +import subprocess import sys import tempfile import yaml as pyyaml
@@ -1744,6 +1745,19 @@ class CodeWriter: else: self.p('}' + line) + def array_start(self, line=''): + if line: + line = line + ' ' + self.p(line + '[') + self._ind += 1 + + def array_end(self, line=''): + if line and line[0] not in {';', ','}: + line = ' ' + line + self._ind -= 1 + self._nl = False + self.p(']' + line) + def write_doc_line(self, doc, indent=True): words = doc.split() line = ' *'
@@ -3415,10 +3429,126 @@ def find_kernel_root(full_path): return full_path, sub_path[:-1] +def render_rust(family, cw): + cw.p('#![allow(unreachable_pub, clippy::wrong_self_convention)]') + cw.p('use kernel::netlink::{Family, MulticastGroup};') + cw.p('use kernel::prelude::*;') + cw.nl() + + family_upper = c_upper(family.ident_name) + family_name = f'{family_upper}_NL_FAMILY' + mcgrps_name = f'{family_name}_MCGRPS' + + cw.p(f'pub static {family_name}: Family = Family::const_new(') + cw._ind += 1 + cw.p('&crate::THIS_MODULE,') + cw.p(f'kernel::uapi::{family.fam_key},') + cw.p(f'kernel::uapi::{family.ver_key},') + if family.mcgrps['list']: + cw.p(f'&{mcgrps_name},') + else: + cw.p('&[],') + cw._ind -= 1 + cw.p(');') + cw.nl() + + if family.mcgrps['list']: + cw.array_start(f'static {mcgrps_name}: [MulticastGroup; {len(family.mcgrps["list"])}] = ') + for grp in family.mcgrps['list']: + cw.p(f'MulticastGroup::const_new(c"{grp["name"]}"),') + cw.array_end(';') + cw.nl() + + for idx, (op_name, op) in enumerate(item for item in family.msgs.items() if 'event' in item[1]): + struct_name = op_name.capitalize() + + if 'doc' in op: + doc_lines = op['doc'].strip().split('\n') + for line in doc_lines: + cw.p(f'/// {line.strip()}') + + cw.block_start(f'pub struct {struct_name}') + cw.p('skb: kernel::netlink::GenlMsg,') + cw.block_end() + cw.nl() + + cw.block_start(f'impl {struct_name}') + cw.p('/// Create a new multicast message.') + cw.p('pub fn new(') + cw._ind += 1 + cw.p('size: usize,') + cw.p('portid: u32,') + cw.p('seq: u32,') + cw.p('flags: kernel::alloc::Flags,') + cw._ind -= 1 + cw.block_start(') -> Result<Self, kernel::alloc::AllocError>') + cw.p(f'const {op.enum_name}: u8 = kernel::uapi::{op.enum_name} as u8;') + cw.p('let skb = kernel::netlink::NetlinkSkBuff::new(size, flags)?;') + cw.p(f'let skb = skb.genlmsg_put(portid, seq, &{family_name}, {op.enum_name})?;') + cw.p('Ok(Self { skb })') + cw.block_end() + cw.nl() + + grp_idx = 0 + if 'mcgrp' in op: + grp_idx = next(i for i, grp in enumerate(family.mcgrps['list']) if grp['name'] == op['mcgrp']) + + cw.p('/// Broadcast this message.') + cw.block_start('pub fn multicast(self, portid: u32, flags: kernel::alloc::Flags) -> Result') + cw.p(f'self.skb.multicast(&{family_name}, portid, {grp_idx}, flags)') + cw.block_end() + cw.nl() + + cw.p('/// Check if this message type has listeners.') + cw.block_start('pub fn has_listeners() -> bool') + cw.p(f'{family_name}.has_listeners({grp_idx})') + cw.block_end() + + attr_set_name = op['attribute-set'] + attr_set = family.attr_sets[attr_set_name] + event_attrs = op['event']['attributes'] + + for attr_name in event_attrs: + attr = attr_set[attr_name] + method_name = attr_name.replace('-', '_') + + if attr.type == 'u32': + put_fn = 'put_u32' + arg_str = ', val' + method_args = '(&mut self, val: u32)' + elif attr.type == 'string': + put_fn = 'put_string' + arg_str = ', val' + method_args = '(&mut self, val: &CStr)' + elif attr.type == 'flag': + put_fn = 'put_flag' + arg_str = '' + method_args = '(&mut self)' + else: + put_fn = f'put_{attr.type}' + arg_str = ', val' + method_args = f'(&mut self, val: {attr.type})' + + cw.nl() + if 'doc' in attr.yaml: + doc_lines = attr.yaml['doc'].strip().split('\n') + for line in doc_lines: + cw.p(f'/// {line.strip()}') + + cw.block_start(f'pub fn {method_name}{method_args} -> Result') + cw.p(f'const {attr.enum_name}: c_int = kernel::uapi::{attr.enum_name} as c_int;') + cw.p(f'self.skb.{put_fn}({attr.enum_name}{arg_str})') + cw.block_end() + + cw.block_end() + cw.nl() + cw.p(' ') + + def main(): parser = argparse.ArgumentParser(description='Netlink simple parsing generator') parser.add_argument('--mode', dest='mode', type=str, required=True, - choices=('user', 'kernel', 'uapi')) + choices=('user', 'kernel', 'uapi', 'rust')) parser.add_argument('--spec', dest='spec', type=str, required=True) parser.add_argument('--header', dest='header', action='store_true', default=None) parser.add_argument('--source', dest='header', action='store_false')
@@ -3471,6 +3601,13 @@ def main(): render_uapi(parsed, cw) return + if args.mode == 'rust': + render_rust(parsed, cw) + cw.close_out_file() + if args.out_file: + subprocess.run(['rustfmt', '--edition', '2021', args.out_file]) + return + hdr_prot = f"_LINUX_{parsed.c_name.upper()}_GEN_H" if args.header: cw.p('#ifndef ' + hdr_prot)
tools/net/ynl/ynl-regen.sh +1 -1
@@ -17,7 +17,7 @@ done KDIR=$(dirname $(dirname $(dirname $(dirname $(realpath $0))))) pushd ${search:-$KDIR} >>/dev/null -files=$(git grep --files-with-matches '^/\* YNL-GEN \(kernel\|uapi\|user\)') +files=$(git grep --files-with-matches '^/\* YNL-GEN \(kernel\|uapi\|user\|rust\)') for f in $files; do # params: 0 1 2 3 # $YAML YNL-GEN kernel $mode
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
Alice Ryhl
maintainerPATCH v3 3/4 rust_binder: add generated netlink.rs file
In reply to Alice RyhlTo use netlink from Rust Binder, add a new generated netlink file using the new script and Documentation/netlink/specs/binder.yaml.
--- drivers/android/Kconfig | 2 +- drivers/android/binder/netlink.rs | 113 +++++++++++++++++++++++++++++ drivers/android/binder/rust_binder_main.rs | 9 ++- rust/uapi/uapi_helper.h | 1 + 4 files changed, 122 insertions(+), 3 deletions(-)
drivers/android/Kconfig +1 -1
@@ -16,7 +16,7 @@ config ANDROID_BINDER_IPC config ANDROID_BINDER_IPC_RUST bool "Rust version of Android Binder IPC Driver" - depends on RUST && MMU && !ANDROID_BINDER_IPC + depends on RUST && MMU && NET && !ANDROID_BINDER_IPC help This enables the Rust implementation of the Binder driver.
drivers/android/binder/netlink.rs new file +113 -0
113 lines not on this page · raw message
drivers/android/binder/rust_binder_main.rs +7 -2
@@ -36,6 +36,8 @@mod deferred_close;mod defs;mod error; +#[allow(dead_code)] +mod netlink;mod node;mod page_range;mod process;
@@ -286,19 +288,22 @@ fn ptr_align(value: usize) -> Option<usize> {// SAFETY: We call register in `init`. static BINDER_SHRINKER :Shrinker =unsafe {Shrinker ::new () }; -struct BinderModule {} +struct BinderModule { + _netlink: kernel::netlink::Registration , +}impl kernel::Module for BinderModule {fn init (_module: &'static kernel::ThisModule ) ->Result <Self > {// SAFETY: The module initializer never runs twice, so we only call this once. unsafe {crate ::context::CONTEXTS .init () }; +let netlink =crate ::netlink::BINDER_NL_FAMILY .register ()?;BINDER_SHRINKER .register (c"android-binder" )?;// SAFETY: The module is being loaded, so we can initialize binderfs. unsafe { kernel::error::to_result (binderfs::init_rust_binderfs ())? }; -Ok (Self {}) +Ok (Self { _netlink: netlink }) } }
rust/uapi/uapi_helper.h +1 -0
@@ -11,6 +11,7 @@#include <uapi/drm/nova_drm.h> #include <uapi/drm/panthor_drm.h> #include <uapi/linux/android/binder.h> +#include <uapi/linux/android/binder_netlink.h> #include <uapi/linux/mdio.h> #include <uapi/linux/mii.h> #include <uapi/linux/ethtool.h>
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
Alice Ryhl
maintainerPATCH v3 4/4 rust_binder: report netlink transactions
In reply to Alice RyhlFrom: Carlos Llamas <[email protected]>
The Android Binder driver supports a netlink API that reports transaction *failures* to a userapce daemon. This allows devices to monitor processes with many failed transactions so that it can e.g. kill misbehaving apps.
One very important thing that this monitors is when many oneway messages are sent to a frozen process, so there is special handling to ensure this scenario is surfaced over netlink.
--- drivers/android/binder/rust_binder_main.rs | 1 - drivers/android/binder/thread.rs | 10 ++++++++ drivers/android/binder/transaction.rs | 40 ++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-)
drivers/android/binder/rust_binder_main.rs +0 -1
@@ -36,7 +36,6 @@mod deferred_close;mod defs;mod error; -#[allow(dead_code)] mod netlink;mod node;mod page_range;
drivers/android/binder/thread.rs +10 -0
@@ -1263,6 +1263,15 @@ fn transaction(self: &Arc<Self>, cmd: u32, reader: &mut UserSliceReader) -> Resu } } +if info.oneway_spam_suspect { +// If this is both a oneway spam suspect and a failure, we report it twice. This is +// useful in case the transaction failed with BR_TRANSACTION_PENDING_FROZEN. + info.report_netlink (BR_ONEWAY_SPAM_SUSPECT , &self.process.ctx); + } +if info.reply !=0 { + info.report_netlink (info.reply, &self.process.ctx); + } +Ok (()) }
@@ -1332,6 +1341,7 @@ fn reply_inner(self: &Arc<Self>, info: &mut TransactionInfo) -> BinderResult { );let reply =Err (BR_FAILED_REPLY ); orig.from.deliver_reply (reply, &orig); + info.reply =BR_FAILED_REPLY ; err.reply =BR_TRANSACTION_COMPLETE ; err });
drivers/android/binder/transaction.rs +40 -0
@@ -3,6 +3,7 @@// Copyright (C) 2025 Google LLC. use kernel::{ + netlink::GENLMSG_DEFAULT_SIZE , prelude::*, seq_file::SeqFile , seq_print,
@@ -17,6 +18,7 @@ allocation::{Allocation, TranslatedFds}, defs::*, error::{BinderError, BinderResult}, + netlink::Report, node::{Node, NodeRef}, process::{Process, ProcessInner}, ptr_align,
@@ -49,6 +51,44 @@ impl TransactionInfo {pub (crate )fn is_oneway (&self) ->bool { self.flags &TF_ONE_WAY !=0 } + +pub (crate )fn report_netlink (&self, reply:u32 , ctx: &crate ::Context ) { +if let Err (err) = self.report_netlink_inner (reply, ctx) { +pr_warn! ( +"{}:{} netlink report failed: {err:?}\n" , + self.from_pid, + self.from_tid + ); + } + } + +fn report_netlink_inner (&self, reply:u32 , ctx: &crate ::Context ) -> kernel::error::Result { +if !Report ::has_listeners () { +return Ok (()); + } +let mut report =Report ::new (GENLMSG_DEFAULT_SIZE ,0 ,0 ,GFP_KERNEL )?; + + report.error (reply)?; + report.context (&ctx.name)?; + report.from_pid (self.from_pidas u32 )?; + report.from_tid (self.from_tidas u32 )?; +if self.to_pid !=0 { + report.to_pid (self.to_pidas u32 )?; + } +if self.to_tid !=0 { + report.to_tid (self.to_tidas u32 )?; + } + +if self.is_reply { + report.is_reply ()?; + } + report.flags (self.flags)?; + report.code (self.code)?; + report.data_size (self.data_sizeas u32 )?; + + report.multicast (0 ,GFP_KERNEL )?; +Ok (()) + } }use core::mem::offset_of;
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
Re: [PATCH v3 1/4] rust: netlink: add raw netlink abstraction
In reply to Alice RyhlQuoted 40 lines
rust/helpers/genetlink.c new file +34
-0@@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright (C) 2026 Google LLC. +*/ + +#include <net/genetlink.h> + +#ifdef CONFIG_NET + +__rust_helper struct sk_buff *rust_helper_genlmsg_new(size_t payload, gfp_t flags) +{ +return genlmsg_new (payload, flags); +} + +__rust_helper +int rust_helper_genlmsg_multicast(const struct genl_family *family, +struct sk_buff *skb,u32 portid, +unsigned int group,gfp_t flags) +{ +return genlmsg_multicast (family, skb, portid, group, flags); +} + +__rust_helper void rust_helper_genlmsg_cancel(struct sk_buff *skb, void *hdr) +{ +genlmsg_cancel (skb, hdr); +} + +__rust_helper void rust_helper_genlmsg_end(struct sk_buff *skb, void *hdr) +{ +genlmsg_end (skb, hdr); +} + +__rust_helper void rust_helper_nlmsg_free(struct sk_buff *skb)
rust/helpers/genetlink.c line 44
Quoted 10 lines
rust/helpers/genetlink.c new file +10
-0+{ +nlmsg_free (skb); +} + +__rust_helper +int rust_helper_genl_has_listeners(const struct genl_family *family, +struct net *net,unsigned int group) +{ +return genl_has_listeners (family, net, group); +}
The C part looks sensible now. For this bit only:
Andrew
Re: [PATCH v3 4/4] rust_binder: report netlink transactions
In reply to Alice RyhlQuoted 4 lines · Alice Ryhl
From: Carlos Llamas <[email protected]>
The Android Binder driver supports a netlink API that reports transaction *failures* to a userapce daemon. This allows devices to
userspace ?
Andrew
Alice Ryhl
maintainerRe: [PATCH v3 4/4] rust_binder: report netlink transactions
Quoted 7 lines · Andrew Lunn
Quoted 4 lines · Alice Ryhl
From: Carlos Llamas <[email protected]>
The Android Binder driver supports a netlink API that reports transaction *failures* to a userapce daemon. This allows devices to
userspace ?
Good catch, thanks.
Alice
Jakub Kicinski
maintainerQuoted 3 lines · On Wed, 15 Apr 2026 09:37:50 +0000 Alice Ryhl wrote:
The C Binder driver exposes messages over netlink when transactions fail, so that a userpace daemon can respond to processes with many failing transactions.
net-next is closed for the duration of the merge window, so that people can take a mental break and increasingly catch up on all the accumulated CI/AI backed work. Please come back in 2 weeks.
Re: [PATCH v3 1/4] rust: netlink: add raw netlink abstraction
In reply to Alice RyhlQuoted 245 lines · Alice Ryhl
This implements a safe and relatively simple API over the netlink API, that allows you to add different attributes to a netlink message and broadcast it. As the first user of this API only makes use of broadcast, only broadcast messages are supported here.
This API is intended to be safe and to be easy to use in *generated* code. This is because netlink is generally used with yaml files that describe the underlying API, and the python generator outputs C code (or, soon, Rust code) that lets you use the API more easily. So for example, if there is a string field, the code generator will output a method that internall calls
put_string()with the right attr type.Signed-off-by Alice Ryhl <[email protected]>--- rust/bindings/bindings_helper.h | 3 + rust/helpers/genetlink.c | 46 ++++++ rust/helpers/helpers.c | 1 + rust/kernel/lib.rs | 1 + rust/kernel/netlink.rs | 329 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 380 insertions(+)rust/bindings/bindings_helper.h +3
-0@@ -88,6 +88,8 @@#include <linux/wait.h> #include <linux/workqueue.h> #include <linux/xarray.h> +#include <net/genetlink.h> +#include <net/netlink.h> #include <trace/events/rust_sample.h> /* @@ -105,6 +107,7 @@const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN =ARCH_SLAB_MINALIGN ;const size_t RUST_CONST_HELPER_ARCH_KMALLOC_MINALIGN =ARCH_KMALLOC_MINALIGN ;const size_t RUST_CONST_HELPER_PAGE_SIZE =PAGE_SIZE ; +const size_t RUST_CONST_HELPER_GENLMSG_DEFAULT_SIZE =GENLMSG_DEFAULT_SIZE ;const gfp_t RUST_CONST_HELPER_GFP_ATOMIC =GFP_ATOMIC ;const gfp_t RUST_CONST_HELPER_GFP_KERNEL =GFP_KERNEL ;const gfp_t RUST_CONST_HELPER_GFP_KERNEL_ACCOUNT =GFP_KERNEL_ACCOUNT ;rust/helpers/genetlink.c new file +46
-0@@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright (C) 2026 Google LLC. +*/ + +#include <net/genetlink.h> + +#ifdef CONFIG_NET + +__rust_helper struct sk_buff *rust_helper_genlmsg_new(size_t payload, gfp_t flags) +{ +return genlmsg_new (payload, flags); +} + +__rust_helper +int rust_helper_genlmsg_multicast(const struct genl_family *family, +struct sk_buff *skb,u32 portid, +unsigned int group,gfp_t flags) +{ +return genlmsg_multicast (family, skb, portid, group, flags); +} + +__rust_helper void rust_helper_genlmsg_cancel(struct sk_buff *skb, void *hdr) +{ +genlmsg_cancel (skb, hdr); +} + +__rust_helper void rust_helper_genlmsg_end(struct sk_buff *skb, void *hdr) +{ +genlmsg_end (skb, hdr); +} + +__rust_helper void rust_helper_nlmsg_free(struct sk_buff *skb) +{ +nlmsg_free (skb); +} + +__rust_helper +int rust_helper_genl_has_listeners(const struct genl_family *family, +struct net *net,unsigned int group) +{ +return genl_has_listeners (family, net, group); +} + +#endif rust/helpers/helpers.c +1
-0@@ -32,6 +32,7 @@#include "err.c" #include "irq.c" #include "fs.c" +#include "genetlink.c" #include "io.c" #include "jump_label.c" #include "kunit.c" rust/kernel/lib.rs +1
-0@@ -122,6 +122,7 @@pub mod module_param;#[cfg( CONFIG_NET )] pub mod net; +pub mod netlink;pub mod num;pub mod of;#[cfg( CONFIG_PM_OPP )] rust/kernel/netlink.rs new file +120
-0@@ -0,0 +1,329 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2026 Google LLC. + +//! Rust support for generic netlink. +//! +//! Currently only supports exposing multicast groups. +//! +//! C header: [`include/net/genetlink.h`](srctree/include/net/genetlink.h) +#![cfg( CONFIG_NET )] + +use kernel::{ + alloc::{self,AllocError }, + error::to_result, + prelude::*, + transmute::AsBytes , + types::Opaque , +ThisModule , +}; + +use core::{ + mem::ManuallyDrop , + ptr::NonNull ,// +}; + +/// The default netlink message size. +pub const GENLMSG_DEFAULT_SIZE :usize = bindings::GENLMSG_DEFAULT_SIZE ; + +/// A wrapper around `struct sk_buff` for generic netlink messages. +/// +/// This type is intended to be specific for buffers used with netlink only, and other usecases for +/// `struct sk_buff` are out-of-scope for this abstraction. +/// +/// # Invariants +/// +/// The pointer has ownership over a valid `sk_buff`. +pub struct NetlinkSkBuff { + skb:NonNull <kernel::bindings::sk_buff >, +} + +impl NetlinkSkBuff { +/// Creates a new `NetlinkSkBuff` with the given size. +pub fn new (size:usize , flags: alloc::Flags ) ->Result <NetlinkSkBuff ,AllocError > { +// SAFETY: `genlmsg_new` only requires its arguments to be valid integers. +let skb =unsafe { bindings::genlmsg_new (size, flags.as_raw ()) }; +let skb =NonNull ::new (skb).ok_or (AllocError )?; +Ok (NetlinkSkBuff { skb }) + } + +/// Puts a generic netlink header into the `NetlinkSkBuff`. +pub fn genlmsg_put ( + self, + portid:u32 , + seq:u32 , + family: &'staticFamily , + cmd:u8 , + ) ->Result <GenlMsg ,AllocError > { +let skb = self.skb.as_ptr (); +// SAFETY: The skb and family pointers are valid. +let hdr =unsafe { bindings::genlmsg_put (skb, portid, seq, family.as_raw (),0 , cmd) }; +let hdr =NonNull ::new (hdr).ok_or (AllocError )?; +Ok (GenlMsg { skb: self, hdr }) + } +} + +impl Drop for NetlinkSkBuff { +fn drop (&mut self) { +// SAFETY: We have ownership over the `sk_buff`, so we may free it. +unsafe { bindings::nlmsg_free (self.skb.as_ptr ()) } + } +} + +/// A generic netlink message being constructed. +/// +/// # Invariants +/// +/// `hdr` references the header in this netlink message. +pub struct GenlMsg { + skb:NetlinkSkBuff , + hdr:NonNull <c_void >, +} + +impl GenlMsg { +/// Puts an attribute into the message. +#[inline] +fn put <T >(&mut self, attrtype:c_int , value: &T ) ->Result +where +T : ?Sized +AsBytes , + { +let skb = self.skb.skb.as_ptr (); +let len =size_of_val (value); +let ptr = core::ptr::from_ref (value).cast ::<c_void >(); +// SAFETY: `skb` is valid by `NetlinkSkBuff` type invariants, and the provided value is +// readable and initialized for its `size_of` bytes. +to_result (unsafe { bindings::nla_put (skb, attrtype, lenas c_int , ptr) }) + } + +/// Puts a `u32` attribute into the message. +#[inline] +pub fn put_u32 (&mut self, attrtype:c_int , value:u32 ) ->Result { + self.put (attrtype, &value) + } + +/// Puts a string attribute into the message. +#[inline] +pub fn put_string (&mut self, attrtype:c_int , value: &CStr ) ->Result { + self.put (attrtype, value.to_bytes_with_nul ()) + } + +/// Puts a flag attribute into the message. +#[inline] +pub fn put_flag (&mut self, attrtype:c_int ) ->Result { +let skb = self.skb.skb.as_ptr (); +// SAFETY: `skb` is valid by `NetlinkSkBuff` type invariants, and a null pointer is valid +// when the length is zero. +to_result (unsafe { bindings::nla_put (skb, attrtype,0 , core::ptr::null ()) }) + } + +/// Sends the generic netlink message as a multicast message. +#[inline]
rust/kernel/netlink.rs line 130
Quoted 10 lines
rust/kernel/netlink.rs new file +10
-0+pub fn multicast ( + self, + family: &'staticFamily , + portid:u32 , + group:u32 , + flags: alloc::Flags , + ) ->Result { +let me =ManuallyDrop ::new (self); +// SAFETY: The `skb` and `family` pointers are valid. We pass ownership of the `skb` to +// `genlmsg_multicast` by not dropping `self`.
I think if genlmsg_multicast returns an error code we may need to drop to avoid leaking. Specifically, there is at least this path:
1. Set group to a large number (that's an unconstrained public parameter)
2. We suppress drop
3. We call genlmsg_multicast
4. We call genlmsg_multicast_netns
4. We call genlmsg_multicast_netns_filtered, which does an inbounds check for the group. If it is too large, it returns EINVAL without consuming the SKB - include/net/genetlink.h:493
5. We leak the skb
However, at the same time, if we pass that check and descend into netlink_broadcast_filtered, it will unconditionally consume the SKB, and possibly return an error code in other situations.
I think this either means that we need to make the inbounds check for groups in genlmsg_multicast_netns_filtered use consume_skb(skb) before returning EINVAL, or we need to check the error code for EINVAL and manually drop if we get it. The second one seems kind of janky because genlmsg_multicast doesn't document that its free-behavior differs for different error codes.
Quoted 21 lines
rust/kernel/netlink.rs new file +21
-0+unsafe { + bindings::genlmsg_end (me.skb.skb.as_ptr (), me.hdr.as_ptr ()); +to_result (bindings::genlmsg_multicast ( + family.as_raw (), + me.skb.skb.as_ptr (), + portid, + group, + flags.as_raw (), + )) + } + } +} +impl Drop for GenlMsg { +fn drop (&mut self) { +// SAFETY: The `hdr` pointer references the header of this generic netlink message. +unsafe { bindings::genlmsg_cancel (self.skb.skb.as_ptr (), self.hdr.as_ptr ()) }; + } +} + +/// Flags for a generic netlink family. +struct FamilyFlags {
rust/kernel/netlink.rs line 161
Quoted 10 lines
rust/kernel/netlink.rs new file +10
-0+/// Whether the family supports network namespaces. + netnsok: bool, +/// Whether the family supports parallel operations. + parallel_ops: bool, +} + +impl FamilyFlags { +/// Converts the flags to the bitfield representation used by `genl_family`. +const fn into_bitfield (self) -> bindings::__BindgenBitfieldUnit <[u8 ;1 ]> { +// The below shifts are verified correct by test_family_flags_bitfield() below.
1. My understanding is that bit layout is implementation defined from C11:
"An implementation may allocate any addressable storage unit large enough to hold a bitfield." (This one gets tested statically via interaction with bindgen)
"The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined." (this one gets checked by your KUnit test)
so we can't actually know that any manual bitpack/unpack will do what we want reliably - another C compiler might do something different.
2. While this looks correct to me from an endianness perspective (in terms of what compilers actually do rather than what the spec says), have we run it on a big endian arch just to make sure?
Quoted 17 lines
rust/kernel/netlink.rs new file +17
-0+// +// Although bindgen generates helpers to change bitfields based on the C headers, these +// helpers unfortunately can't be used in const context. Since `Family` needs to be filled +// out at build-time, we use this helper instead. +let mut bits =0 ; +if self.netnsok { + bits |=1 <<0 ; + } +if self.parallel_ops { + bits |=1 <<1 ; + } +// SAFETY: This bitfield is represented as an u8. +unsafe { core::mem::transmute ::<u8 , bindings::__BindgenBitfieldUnit <[u8 ;1 ]>>(bits) } + } +} + +/// A generic netlink family.
rust/kernel/netlink.rs line 188
Quoted 10 lines
rust/kernel/netlink.rs new file +10
-0+#[repr(transparent)] +pub struct Family { + inner:Opaque <bindings::genl_family >, +} + +// SAFETY: The `Family` type is thread safe. +unsafe impl Sync for Family {} + +impl Family { +/// Creates a new `Family` instance.
Might be worth calling out that this panics on bad input rather than returning an error in docs? It might be fine because this isn't going to be called dynamically, but it doesn't match the usual behavior for other kernel functions.
rust/kernel/netlink.rs line 190
Quoted 2 lines
rust/kernel/netlink.rs new file +2
-0+pub const fn const_new ( + module: &ThisModule,
If const_new for MulticastGroup can take a &CStr, why can't we take one here?
Quoted 52 lines
rust/kernel/netlink.rs new file +52
-0+ name: &[u8], + version: u32, + mcgrps: &'static [MulticastGroup], + ) -> Family { + let n_mcgrps = mcgrps.len() as u8; +if n_mcgrpsas usize != mcgrps.len () { +panic! ("too many mcgrps" ); + } +let mut genl_family = bindings::genl_family { + version, + _bitfield_1:FamilyFlags { + netnsok:true , + parallel_ops:true , + } + .into_bitfield (), + module: module.as_ptr (), + mcgrps: mcgrps.as_ptr ().cast (), + n_mcgrps, + ..pin_init::zeroed () + }; +if CStr ::from_bytes_with_nul (name).is_err () { +panic! ("genl_family name not nul-terminated" ); + } +if genl_family.name.len () < name.len () { +panic! ("genl_family name too long" ); + } +let mut i =0 ; +while i < name.len () { + genl_family.name[i] = name[i]; + i +=1 ; + } +Family { + inner:Opaque ::new (genl_family), + } + } + +/// Checks if there are any listeners for the given multicast group. +pub fn has_listeners (&self, group:u32 ) ->bool { +// SAFETY: The family and init_net pointers are valid. +unsafe { + bindings::genl_has_listeners (self.as_raw (), &raw mut bindings::init_net, group) !=0 + } + } + +/// Returns a raw pointer to the underlying `genl_family` structure. +pub fn as_raw (&self) -> *mut bindings::genl_family { + self.inner.get () + } +} + +/// A generic netlink multicast group. +#[repr(transparent)]
rust/kernel/netlink.rs line 252
Quoted 10 lines
rust/kernel/netlink.rs new file +10
-0+pub struct MulticastGroup { +// No Opaque because fully immutable + group: bindings::genl_multicast_group , +} + +// SAFETY: Pure data so thread safe. +unsafe impl Sync for MulticastGroup {} + +impl MulticastGroup { +/// Creates a new `MulticastGroup` instance.
Same as before - should the panic be documented?
Quoted 82 lines
rust/kernel/netlink.rs new file +77
-1+pub const fn const_new (name: &CStr ) ->MulticastGroup { +let mut group: bindings::genl_multicast_group = pin_init::zeroed (); + +let name = name.to_bytes_with_nul (); +if group.name.len () < name.len () { +panic! ("genl_multicast_group name too long" ); + } +let mut i =0 ; +while i < name.len () { + group.name[i] = name[i]; + i +=1 ; + } + +MulticastGroup { group } + } +} + +/// A registration of a generic netlink family. +/// +/// This type represents the registration of a [`Family`]. When an instance of this type is +/// dropped, its respective generic netlink family will be unregistered from the system. +/// +/// # Invariants +/// +/// `self.family` always holds a valid reference to an initialized and registered [`Family`]. +pub struct Registration { + family: &'staticFamily , +} + +impl Family { +/// Registers the generic netlink family with the kernel. +pub fn register (&'static self) ->Result <Registration > { +// SAFETY: `self.as_raw()` is a valid pointer to a `genl_family` struct. +// The `genl_family` struct is static, so it will outlive the registration. +to_result (unsafe { bindings::genl_register_family (self.as_raw ()) })?; +Ok (Registration { family: self }) + } +} + +impl Drop for Registration { +fn drop (&mut self) { +// SAFETY: `self.family.as_raw()` is a valid pointer to a registered `genl_family` struct. +// The `Registration` struct ensures that `genl_unregister_family` is called exactly once +// for this family when it goes out of scope. +unsafe { bindings::genl_unregister_family (self.family.as_raw ()) }; + } +} + +#[macros::kunit_tests(rust_netlink)] +mod tests { +use super ::*; + +#[test] +fn test_family_flags_bitfield () { +for netnsokin [false ,true ] { +for parallel_opsin [false ,true ] { +let mut b_fam = bindings::genl_family { + ..Default ::default () + }; + b_fam.set_netnsok (if netnsok {1 }else {0 }); + b_fam.set_parallel_ops (if parallel_ops {1 }else {0 }); + +let c_bitfield =FamilyFlags { + netnsok, + parallel_ops, + } + .into_bitfield (); + +// SAFETY: The bit field is stored as u8. +let b_val:u8 =unsafe { core::mem::transmute (b_fam._bitfield_1) }; +// SAFETY: The bit field is stored as u8. +let c_val:u8 =unsafe { core::mem::transmute (c_bitfield) }; +assert_eq! (b_val, c_val); + } + } + } +} --2.54.0.rc0.605.g598a273b03-goog
Re: [PATCH v3 1/4] rust: netlink: add raw netlink abstraction
Quoted 4 lines · Matthew Maurer
Quoted 4 lines
rust/kernel/netlink.rs new file +4
-0+/// Sends the generic netlink message as a multicast message. +#[inline] +pub fn multicast ( + self,
Quoted 8 lines
rust/kernel/netlink.rs line 130
Quoted 8 lines
rust/kernel/netlink.rs new file +8
-0+ family: &'static Family, + portid: u32, + group: u32, + flags: alloc::Flags, + ) -> Result { +let me =ManuallyDrop ::new (self); +// SAFETY: The `skb` and `family` pointers are valid. We pass ownership of the `skb` to +// `genlmsg_multicast` by not dropping `self`.
Hi Matthew
Please trim when replying, to just the needed context.
Quoted 11 lines
I think if
genlmsg_multicastreturns an error code we may need to drop to avoid leaking. Specifically, there is at least this path:
1. Set group to a large number (that's an unconstrained public parameter)
2. We suppress drop
3. We callgenlmsg_multicast
4. We callgenlmsg_multicast_netns
4. We callgenlmsg_multicast_netns_filtered, which does an inbounds check for thegroup. If it is too large, it returns EINVAL without consuming the SKB -include/net/genetlink.h:493
5. We leak the skb
Quoted 3 lines
However, at the same time, if we pass that check and descend into
netlink_broadcast_filtered, it will unconditionally consume the SKB, and possibly return an error code in other situations.
A quick grep of the code suggests very few callers of genlmsg_multicast look at the return code.
drivers/scsi/pmcraid.c prints an error message, but does nothing with the skb.
drivers/regulator/event.c returns the error code to its caller, which discards is, and the skb is leaked.
net/ieee802154/netlink.c returns the error code up the call stack but leaks the skb.
net/nfc/netlink.c returns the error code up the call stack but leaks the skb.
So i would agree with you, freeing it on error somewhere within genlmsg_multicast() would make sense.
Andrew
Matthew Maurer
Reviewed-byRe: [PATCH v3 1/4] rust: netlink: add raw netlink abstraction
Quoted 5 lines
So i would agree with you, freeing it on error somewhere within
genlmsg_multicast()would make sense.Andrew
If this should be fixed separately, then for the rust part:
with the caveat that documentation is added to the two const_new functions to indicate that they are expected to panic on error, and should only be used at const time.
Donald Hunter
maintainerRe: [PATCH v3 2/4] ynl_gen: generate Rust files from yaml files
In reply to Alice RyhlQuoted 10 lines · Alice Ryhl
To generate netlink frames from Rust code easily, generate Rust libraries with methods for generating different netlink messages as appropriate.
The new 'rust' type corresponds to a Rust version of the C target 'kernel'. There is no Rust version of the 'uapi' target since Rust code exports its uapi via C headers - choice of language is opaque to userspace.
Quoted 5 lines
This logic is kept in the existing
ynl_gen_c.pyfile to reuse CodeWriter and other shared pieces of logic in the existing python file. This has the disadvantage that thegen_cpart of the name is now wrong, as it also generates Rust. One possible solution to this could be to rename the file.
Hi Alice,
I started a rough refactor of ynl_gen_c a while ago, so that I could reuse CodeWriter and the gentypes in a code generator for strace decoders. I just rebased the work to net-next/main here:
https://github.com/donaldh/linux/tree/ynl-gen-strace
If that looks workable for a ynl_gen_rst then I'd be happy to help tidy it up into a patch series.
Thanks,
Donald
Alice Ryhl
maintainerRe: [PATCH v3 2/4] ynl_gen: generate Rust files from yaml files
Quoted 19 lines · Donald Hunter
Quoted 10 lines · Alice Ryhl
To generate netlink frames from Rust code easily, generate Rust libraries with methods for generating different netlink messages as appropriate.
The new 'rust' type corresponds to a Rust version of the C target 'kernel'. There is no Rust version of the 'uapi' target since Rust code exports its uapi via C headers - choice of language is opaque to userspace.
Quoted 5 lines
This logic is kept in the existing
ynl_gen_c.pyfile to reuse CodeWriter and other shared pieces of logic in the existing python file. This has the disadvantage that thegen_cpart of the name is now wrong, as it also generates Rust. One possible solution to this could be to rename the file.Hi Alice,
Quoted 8 lines
I started a rough refactor of
ynl_gen_ca while ago, so that I could reuse CodeWriter and the gentypes in a code generator for strace decoders. I just rebased the work to net-next/main here:https://github.com/donaldh/linux/tree/ynl-gen-strace
If that looks workable for a
ynl_gen_rstthen I'd be happy to help tidy it up into a patch series.
That sounds convenient!
Alice