tun/platform/linux/mod.rs
1// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2// Version 2, December 2004
3//
4// Copyleft (ↄ) meh. <meh@schizofreni.co> | http://meh.schizofreni.co
5//
6// Everyone is permitted to copy and distribute verbatim or modified
7// copies of this license document, and changing it is allowed as long
8// as the name is changed.
9//
10// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12//
13// 0. You just DO WHAT THE FUCK YOU WANT TO.
14
15//! Linux specific functionality.
16
17mod sys;
18
19mod device;
20pub use self::device::Device;
21
22use crate::configuration::Configuration;
23use crate::error::Result;
24
25/// Linux-only interface configuration.
26#[derive(Copy, Clone, Debug)]
27pub struct PlatformConfig {
28 /// switch of Enable/Disable packet information for network driver
29 pub(crate) packet_information: bool,
30 /// root privileges required or not
31 pub(crate) ensure_root_privileges: bool,
32
33 /// Enable IFF_NAPI
34 pub(crate) napi: bool,
35
36 /// Enable IFF_VNET_HDR
37 pub(crate) vnet_hdr: bool,
38}
39
40/// `packet_information` is default to be `false` and `ensure_root_privileges` is default to be `true`.
41impl Default for PlatformConfig {
42 fn default() -> Self {
43 PlatformConfig {
44 packet_information: false,
45 ensure_root_privileges: true,
46 napi: false,
47 vnet_hdr: false,
48 }
49 }
50}
51
52impl PlatformConfig {
53 /// Enable or disable packet information, the first 4 bytes of
54 /// each packet delivered from/to Linux underlying API is a header with flags and protocol type when enabled.
55 ///
56 /// [Note: This configuration just applies to the Linux underlying API and is a no-op on `tun` crate
57 /// (i.e. the packets delivered from/to `tun` crate must always NOT contain packet information) -- end note].
58 #[deprecated(
59 since = "0.7.0",
60 note = "No effect applies to the packets delivered from/to tun since the packets always contain no header on all platforms."
61 )]
62 pub fn packet_information(&mut self, value: bool) -> &mut Self {
63 self.packet_information = value;
64 self
65 }
66
67 /// Indicated whether tun running in root privilege,
68 /// since some operations need it such as assigning IP/netmask/destination etc.
69 pub fn ensure_root_privileges(&mut self, value: bool) -> &mut Self {
70 self.ensure_root_privileges = value;
71 self
72 }
73
74 /// Enable / Disable IFF_NAPI flag.
75 pub fn napi(&mut self, value: bool) -> &mut Self {
76 self.napi = value;
77 self
78 }
79
80 /// Enable / Disable IFF_VNET_HDR flag.
81 pub fn vnet_hdr(&mut self, value: bool) -> &mut Self {
82 self.vnet_hdr = value;
83 self
84 }
85}
86
87/// Create a TUN device with the given name.
88pub fn create(configuration: &Configuration) -> Result<Device> {
89 Device::new(configuration)
90}