forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
118 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
use alloc::vec::Vec; | ||
use core::ops::{Deref, DerefMut}; | ||
|
||
use crate::{bindings, linked_list::Wrapper}; | ||
use crate::{c_types, CStr}; | ||
use crate::error::{Error, KernelResult}; | ||
use crate::sync::Lock; | ||
use crate::user_ptr::{UserSlicePtr, UserSlicePtrReader, UserSlicePtrWriter}; | ||
|
||
use super::device::{NetDeviceAdapter, NetDevice}; | ||
|
||
|
||
/*#[repr(C)] | ||
pub struct NlAttr { | ||
nla_len: u16, | ||
nla_type: u16, | ||
}*/ | ||
pub struct NlAttr(*const bindings::nlattr); | ||
|
||
impl NlAttr { | ||
pub fn is_null(&self) -> bool { | ||
self.0.is_null() | ||
} | ||
|
||
pub fn nla_len(&self) -> u16 { | ||
if self.is_null() { | ||
return 0; | ||
} | ||
|
||
// NO-PANIC: self is valid and not null | ||
let nlattr = self.0.as_ref().unwrap(); | ||
nlattr.nla_len - bindings::NLA_HDRLEN | ||
} | ||
|
||
/// Constructs a new [`struct nlattr`] wrapper. | ||
/// | ||
/// # Safety | ||
/// | ||
/// The pointer `ptr` must be non-null and valid for the lifetime of the object. | ||
pub unsafe fn from_ptr(ptr: *const bindings::nlattr) -> Self { | ||
Self(ptr) | ||
} | ||
/*pub unsafe fn from_ptr(ptr: *const bindings::nlattr) -> &'static mut Self { | ||
(ptr as *mut NlAttr).as_mut().unwrap() | ||
}*/ | ||
} | ||
|
||
pub struct NlExtAck(*const bindings::netlink_ext_ack); | ||
|
||
impl NlExtAck { | ||
/// Constructs a new [`struct netlink_ext_ack`] wrapper. | ||
/// | ||
/// # Safety | ||
/// | ||
/// The pointer `ptr` must be non-null and valid for the lifetime of the object. | ||
pub unsafe fn from_ptr(ptr: *const bindings::netlink_ext_ack) -> Self { | ||
Self(ptr) | ||
} | ||
} | ||
|
||
pub struct NlAttrVec<'a>(&'a mut [NlAttr]); | ||
|
||
impl<'a> NlAttrVec<'a> { | ||
pub fn get(&self, offset: u32) -> Option<NlAttr> { | ||
if offset > bindings::__IFLA_MAX { | ||
return None; | ||
} | ||
|
||
let nlattr = self.0[offset as usize]; | ||
if nlattr.is_null() { | ||
None | ||
} else { | ||
Some(nlattr) | ||
} | ||
} | ||
|
||
/// Constructs a new [`struct nlattr[]`] wrapper. | ||
/// | ||
/// # Safety | ||
/// | ||
/// The pointer `ptr` must be non-null and valid for the lifetime of the object. | ||
/// The pointer `ptr` must be valid for the size of `__IFLA_MAX` * `mem::size_of<NlAttr>` | ||
pub unsafe fn from_ptr(ptr: *const bindings::nlattr) -> Self { | ||
// TODO: is this correct? | ||
Self(core::slice::from_raw_parts_mut(ptr as *mut NlAttr, bindings::__IFLA_MAX as usize)) | ||
} | ||
} |