1use kernel::{
12 alloc::{self, AllocError},
13 error::to_result,
14 prelude::*,
15 types::Opaque,
16 ThisModule,
17};
18
19use core::{
20 mem::ManuallyDrop,
21 ptr::NonNull, };
23
24use zerocopy::{
25 Immutable,
26 IntoBytes, };
28
29pub const GENLMSG_DEFAULT_SIZE: usize = bindings::GENLMSG_DEFAULT_SIZE;
31
32pub struct NetlinkSkBuff {
41 skb: NonNull<kernel::bindings::sk_buff>,
42}
43
44impl NetlinkSkBuff {
45 pub fn new(size: usize, flags: alloc::Flags) -> Result<NetlinkSkBuff, AllocError> {
47 let skb = unsafe { bindings::genlmsg_new(size, flags.as_raw()) };
49 let skb = NonNull::new(skb).ok_or(AllocError)?;
50 Ok(NetlinkSkBuff { skb })
51 }
52
53 pub fn genlmsg_put(
55 self,
56 portid: u32,
57 seq: u32,
58 family: &'static Family,
59 cmd: u8,
60 ) -> Result<GenlMsg, AllocError> {
61 let skb = self.skb.as_ptr();
62 let hdr = unsafe { bindings::genlmsg_put(skb, portid, seq, family.as_raw(), 0, cmd) };
64 let hdr = NonNull::new(hdr).ok_or(AllocError)?;
65 Ok(GenlMsg { skb: self, hdr })
66 }
67}
68
69impl Drop for NetlinkSkBuff {
70 fn drop(&mut self) {
71 unsafe { bindings::nlmsg_free(self.skb.as_ptr()) }
73 }
74}
75
76pub struct GenlMsg {
82 skb: NetlinkSkBuff,
83 hdr: NonNull<c_void>,
84}
85
86impl GenlMsg {
87 #[inline]
89 fn put<T>(&mut self, attrtype: c_int, value: &T) -> Result
90 where
91 T: ?Sized + IntoBytes + Immutable,
92 {
93 let skb = self.skb.skb.as_ptr();
94 let len = size_of_val(value);
95 let ptr = core::ptr::from_ref(value).cast::<c_void>();
96 to_result(unsafe { bindings::nla_put(skb, attrtype, len as c_int, ptr) })
99 }
100
101 #[inline]
103 pub fn put_u32(&mut self, attrtype: c_int, value: u32) -> Result {
104 self.put(attrtype, &value)
105 }
106
107 #[inline]
109 pub fn put_string(&mut self, attrtype: c_int, value: &CStr) -> Result {
110 self.put(attrtype, value.to_bytes_with_nul())
111 }
112
113 #[inline]
115 pub fn put_flag(&mut self, attrtype: c_int) -> Result {
116 let skb = self.skb.skb.as_ptr();
117 to_result(unsafe { bindings::nla_put(skb, attrtype, 0, core::ptr::null()) })
120 }
121
122 #[inline]
124 pub fn multicast(
125 self,
126 family: &'static Family,
127 portid: u32,
128 group: u32,
129 flags: alloc::Flags,
130 ) -> Result {
131 let me = ManuallyDrop::new(self);
132 unsafe {
135 bindings::genlmsg_end(me.skb.skb.as_ptr(), me.hdr.as_ptr());
136 to_result(bindings::genlmsg_multicast(
137 family.as_raw(),
138 me.skb.skb.as_ptr(),
139 portid,
140 group,
141 flags.as_raw(),
142 ))
143 }
144 }
145}
146impl Drop for GenlMsg {
147 fn drop(&mut self) {
148 unsafe { bindings::genlmsg_cancel(self.skb.skb.as_ptr(), self.hdr.as_ptr()) };
150 }
151}
152
153struct FamilyFlags {
155 netnsok: bool,
157 parallel_ops: bool,
159}
160
161impl FamilyFlags {
162 const fn into_bitfield(self) -> bindings::__BindgenBitfieldUnit<[u8; 1]> {
164 let mut bits = 0;
170 if self.netnsok {
171 bits |= 1 << 0;
172 }
173 if self.parallel_ops {
174 bits |= 1 << 1;
175 }
176 bits = u8::from_le(bits);
178 unsafe { core::mem::transmute::<u8, bindings::__BindgenBitfieldUnit<[u8; 1]>>(bits) }
180 }
181}
182
183#[repr(transparent)]
185pub struct Family {
186 inner: Opaque<bindings::genl_family>,
187}
188
189unsafe impl Sync for Family {}
191
192impl Family {
193 pub const fn const_new(
200 module: &ThisModule,
201 name: &[u8],
202 version: u32,
203 mcgrps: &'static [MulticastGroup],
204 ) -> Family {
205 let n_mcgrps = mcgrps.len() as u8;
206 if n_mcgrps as usize != mcgrps.len() {
207 panic!("too many mcgrps");
208 }
209 let mut genl_family = bindings::genl_family {
210 version,
211 _bitfield_1: FamilyFlags {
212 netnsok: true,
213 parallel_ops: true,
214 }
215 .into_bitfield(),
216 module: module.as_ptr(),
217 mcgrps: mcgrps.as_ptr().cast(),
218 n_mcgrps,
219 ..pin_init::zeroed()
220 };
221 if CStr::from_bytes_with_nul(name).is_err() {
222 panic!("genl_family name not nul-terminated");
223 }
224 if genl_family.name.len() < name.len() {
225 panic!("genl_family name too long");
226 }
227 let mut i = 0;
228 while i < name.len() {
229 genl_family.name[i] = name[i];
230 i += 1;
231 }
232 Family {
233 inner: Opaque::new(genl_family),
234 }
235 }
236
237 pub fn has_listeners(&self, group: u32) -> bool {
239 unsafe {
241 bindings::genl_has_listeners(self.as_raw(), &raw mut bindings::init_net, group) != 0
242 }
243 }
244
245 pub fn as_raw(&self) -> *mut bindings::genl_family {
247 self.inner.get()
248 }
249}
250
251#[repr(transparent)]
253pub struct MulticastGroup {
254 group: bindings::genl_multicast_group,
256}
257
258unsafe impl Sync for MulticastGroup {}
260
261impl MulticastGroup {
262 pub const fn const_new(name: &CStr) -> MulticastGroup {
266 let mut group: bindings::genl_multicast_group = pin_init::zeroed();
267
268 let name = name.to_bytes_with_nul();
269 if group.name.len() < name.len() {
270 panic!("genl_multicast_group name too long");
271 }
272 let mut i = 0;
273 while i < name.len() {
274 group.name[i] = name[i];
275 i += 1;
276 }
277
278 MulticastGroup { group }
279 }
280}
281
282pub struct Registration {
291 family: &'static Family,
292}
293
294impl Family {
295 pub fn register(&'static self) -> Result<Registration> {
297 to_result(unsafe { bindings::genl_register_family(self.as_raw()) })?;
300 Ok(Registration { family: self })
301 }
302}
303
304impl Drop for Registration {
305 fn drop(&mut self) {
306 unsafe { bindings::genl_unregister_family(self.family.as_raw()) };
310 }
311}
312
313#[macros::kunit_tests(rust_netlink)]
314mod tests {
315 use super::*;
316
317 #[test]
318 fn test_family_flags_bitfield() {
319 for netnsok in [false, true] {
320 for parallel_ops in [false, true] {
321 let mut b_fam = bindings::genl_family {
322 ..Default::default()
323 };
324 b_fam.set_netnsok(if netnsok { 1 } else { 0 });
325 b_fam.set_parallel_ops(if parallel_ops { 1 } else { 0 });
326
327 let c_bitfield = FamilyFlags {
328 netnsok,
329 parallel_ops,
330 }
331 .into_bitfield();
332
333 let b_val: u8 = unsafe { core::mem::transmute(b_fam._bitfield_1) };
335 let c_val: u8 = unsafe { core::mem::transmute(c_bitfield) };
337 assert_eq!(b_val, c_val);
338 }
339 }
340 }
341}