1
+ // SPDX-License-Identifier: GPL-2.0
2
+ use alloc:: vec:: Vec ;
3
+ use core:: ops:: { Deref , DerefMut } ;
4
+
5
+ use crate :: { bindings, linked_list:: Wrapper } ;
6
+ use crate :: { c_types, CStr } ;
7
+ use crate :: error:: { Error , KernelResult } ;
8
+ use crate :: sync:: Lock ;
9
+ use crate :: user_ptr:: { UserSlicePtr , UserSlicePtrReader , UserSlicePtrWriter } ;
10
+
11
+ use super :: device:: { NetDeviceAdapter , NetDevice } ;
12
+
13
+
14
+ /*#[repr(C)]
15
+ pub struct NlAttr {
16
+ nla_len: u16,
17
+ nla_type: u16,
18
+ }*/
19
+ pub struct NlAttr ( * const bindings:: nlattr ) ;
20
+
21
+ impl NlAttr {
22
+ pub fn is_null ( & self ) -> bool {
23
+ self . 0 . is_null ( )
24
+ }
25
+
26
+ pub fn nla_len ( & self ) -> u16 {
27
+ if self . is_null ( ) {
28
+ return 0 ;
29
+ }
30
+
31
+ // NO-PANIC: self is valid and not null
32
+ let nlattr = self . 0 . as_ref ( ) . unwrap ( ) ;
33
+ nlattr. nla_len - bindings:: NLA_HDRLEN
34
+ }
35
+
36
+ /// Constructs a new [`struct nlattr`] wrapper.
37
+ ///
38
+ /// # Safety
39
+ ///
40
+ /// The pointer `ptr` must be non-null and valid for the lifetime of the object.
41
+ pub unsafe fn from_ptr ( ptr : * const bindings:: nlattr ) -> Self {
42
+ Self ( ptr)
43
+ }
44
+ /*pub unsafe fn from_ptr(ptr: *const bindings::nlattr) -> &'static mut Self {
45
+ (ptr as *mut NlAttr).as_mut().unwrap()
46
+ }*/
47
+ }
48
+
49
+ pub struct NlExtAck ( * const bindings:: netlink_ext_ack ) ;
50
+
51
+ impl NlExtAck {
52
+ /// Constructs a new [`struct netlink_ext_ack`] wrapper.
53
+ ///
54
+ /// # Safety
55
+ ///
56
+ /// The pointer `ptr` must be non-null and valid for the lifetime of the object.
57
+ pub unsafe fn from_ptr ( ptr : * const bindings:: netlink_ext_ack ) -> Self {
58
+ Self ( ptr)
59
+ }
60
+ }
61
+
62
+ pub struct NlAttrVec < ' a > ( & ' a mut [ NlAttr ] ) ;
63
+
64
+ impl < ' a > NlAttrVec < ' a > {
65
+ pub fn get ( & self , offset : u32 ) -> Option < NlAttr > {
66
+ if offset > bindings:: __IFLA_MAX {
67
+ return None ;
68
+ }
69
+
70
+ let nlattr = self . 0 [ offset as usize ] ;
71
+ if nlattr. is_null ( ) {
72
+ None
73
+ } else {
74
+ Some ( nlattr)
75
+ }
76
+ }
77
+
78
+ /// Constructs a new [`struct nlattr[]`] wrapper.
79
+ ///
80
+ /// # Safety
81
+ ///
82
+ /// The pointer `ptr` must be non-null and valid for the lifetime of the object.
83
+ /// The pointer `ptr` must be valid for the size of `__IFLA_MAX` * `mem::size_of<NlAttr>`
84
+ pub unsafe fn from_ptr ( ptr : * const bindings:: nlattr ) -> Self {
85
+ // TODO: is this correct?
86
+ Self ( core:: slice:: from_raw_parts_mut ( ptr as * mut NlAttr , bindings:: __IFLA_MAX as usize ) )
87
+ }
88
+ }
0 commit comments