11use core:: task:: Waker ;
22
3- use smoltcp:: { storage:: PacketBuffer , time:: Instant , wire:: IpAddress } ;
3+ use bitflags:: bitflags;
4+ use num_enum:: TryFromPrimitive ;
5+ use smoltcp:: {
6+ storage:: PacketBuffer ,
7+ time:: Instant ,
8+ wire:: { IpAddress , Ipv4Address } ,
9+ } ;
410
511mod ethernet;
612mod loopback;
@@ -15,6 +21,16 @@ pub use vsock::*;
1521pub trait Device : Send + Sync {
1622 fn name ( & self ) -> & str ;
1723
24+ fn get_type ( & self ) -> DeviceType ;
25+
26+ fn get_flags ( & self ) -> DeviceFlags ;
27+
28+ fn get_index ( & self ) -> u32 ;
29+
30+ fn ipv4_addr ( & self ) -> Option < Ipv4Address > ;
31+
32+ fn prefix_len ( & self ) -> Option < u8 > ;
33+
1834 fn recv ( & mut self , buffer : & mut PacketBuffer < ( ) > , timestamp : Instant ) -> bool ;
1935 /// Sends a packet to the next hop.
2036 ///
@@ -25,3 +41,81 @@ pub trait Device: Send + Sync {
2541
2642 fn register_waker ( & self , waker : & Waker ) ;
2743}
44+
45+ /// Device type.
46+ ///
47+ /// Reference: <https://elixir.bootlin.com/linux/v6.0.18/source/include/uapi/linux/if_arp.h#L30>
48+ #[ repr( u16 ) ]
49+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , TryFromPrimitive ) ]
50+ #[ allow( dead_code) ]
51+ pub enum DeviceType {
52+ // Arp protocol hardware identifiers
53+ /// from KA9Q: NET/ROM pseudo
54+ NETROM = 0 ,
55+ /// Ethernet 10Mbps
56+ ETHER = 1 ,
57+ /// Experimental Ethernet
58+ EETHER = 2 ,
59+
60+ // Dummy types for non ARP hardware
61+ /// IPIP tunnel
62+ TUNNEL = 768 ,
63+ /// IP6IP6 tunnel
64+ TUNNEL6 = 769 ,
65+ /// Frame Relay Access Device
66+ FRAD = 770 ,
67+ /// SKIP vif
68+ SKIP = 771 ,
69+ /// Loopback device
70+ LOOPBACK = 772 ,
71+ /// Localtalk device
72+ LOCALTALK = 773 ,
73+ // TODO: This enum is not exhaustive
74+ }
75+
76+ bitflags ! {
77+ /// Device flags.
78+ ///
79+ /// Reference: <https://elixir.bootlin.com/linux/v6.0.18/source/include/uapi/linux/if.h#L82>
80+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
81+ pub struct DeviceFlags : u32 {
82+ /// Device is up
83+ const UP = 1 <<0 ;
84+ /// Broadcast address valid
85+ const BROADCAST = 1 <<1 ;
86+ /// Turn on debugging
87+ const DEBUG = 1 <<2 ;
88+ /// Loopback net
89+ const LOOPBACK = 1 <<3 ;
90+ /// Device is has p-p link
91+ const POINTOPOINT = 1 <<4 ;
92+ /// Avoid use of trailers
93+ const NOTRAILERS = 1 <<5 ;
94+ /// Device RFC2863 OPER_UP
95+ const RUNNING = 1 <<6 ;
96+ /// No ARP protocol
97+ const NOARP = 1 <<7 ;
98+ /// Receive all packets
99+ const PROMISC = 1 <<8 ;
100+ /// Receive all multicast packets
101+ const ALLMULTI = 1 <<9 ;
102+ /// Master of a load balancer
103+ const MASTER = 1 <<10 ;
104+ /// Slave of a load balancer
105+ const SLAVE = 1 <<11 ;
106+ /// Supports multicast
107+ const MULTICAST = 1 <<12 ;
108+ /// Can set media type
109+ const PORTSEL = 1 <<13 ;
110+ /// Auto media select active
111+ const AUTOMEDIA = 1 <<14 ;
112+ /// Dialup device with changing addresses
113+ const DYNAMIC = 1 <<15 ;
114+ /// Driver signals L1 up
115+ const LOWER_UP = 1 <<16 ;
116+ /// Driver signals dormant
117+ const DORMANT = 1 <<17 ;
118+ /// Echo sent packets
119+ const ECHO = 1 <<18 ;
120+ }
121+ }
0 commit comments