Merge #469
469: Add IEEE 802.15.4/6LoWPAN support r=Dirbaio a=thibautvdv This adds the IEEE 802.15.4 frame representation. Still a work in progress and interested to know what you think about this. I really would like to add 6LowPAN as well, however I'm not sure where to put this in smoltcp, since 6LowPAN is kind of weird. Co-authored-by: Thibaut Vandervelden <thvdveld@vub.be> Co-authored-by: Dario Nieuwenhuis <dirbaio@dirbaio.net>master
commit
221eaf90f1
|
@ -73,7 +73,7 @@ jobs:
|
|||
|
||||
features:
|
||||
# These feature sets cannot run tests, so we only check they build.
|
||||
- rand-custom-impl medium-ip medium-ethernet proto-ipv6 proto-ipv6 proto-igmp proto-dhcpv4 socket-raw socket-udp socket-tcp socket-icmp async
|
||||
- rand-custom-impl medium-ip medium-ethernet medium-ieee802154 proto-ipv6 proto-ipv6 proto-igmp proto-dhcpv4 socket-raw socket-udp socket-tcp socket-icmp async
|
||||
- rand-custom-impl defmt defmt-trace medium-ip medium-ethernet proto-ipv6 proto-ipv6 proto-igmp proto-dhcpv4 socket-raw socket-udp socket-tcp socket-icmp async
|
||||
|
||||
steps:
|
||||
|
|
16
Cargo.toml
16
Cargo.toml
|
@ -37,18 +37,24 @@ verbose = []
|
|||
rand-custom-impl = []
|
||||
"medium-ethernet" = ["socket"]
|
||||
"medium-ip" = ["socket"]
|
||||
"phy-raw_socket" = ["std", "libc", "medium-ethernet"]
|
||||
"medium-ieee802154" = ["socket", "proto-sixlowpan"]
|
||||
|
||||
"phy-raw_socket" = ["std", "libc"]
|
||||
"phy-tuntap_interface" = ["std", "libc", "medium-ethernet"]
|
||||
|
||||
"proto-ipv4" = []
|
||||
"proto-igmp" = ["proto-ipv4"]
|
||||
"proto-dhcpv4" = ["proto-ipv4"]
|
||||
"proto-ipv6" = []
|
||||
"proto-sixlowpan" = ["proto-ipv6"]
|
||||
|
||||
"socket" = []
|
||||
"socket-raw" = ["socket"]
|
||||
"socket-udp" = ["socket"]
|
||||
"socket-tcp" = ["socket"]
|
||||
"socket-icmp" = ["socket"]
|
||||
"socket-dhcpv4" = ["socket", "medium-ethernet", "proto-dhcpv4"]
|
||||
|
||||
"async" = []
|
||||
|
||||
defmt-trace = []
|
||||
|
@ -59,9 +65,9 @@ defmt-error = []
|
|||
|
||||
default = [
|
||||
"std", "log", # needed for `cargo test --no-default-features --features default` :/
|
||||
"medium-ethernet", "medium-ip",
|
||||
"medium-ethernet", "medium-ip", "medium-ieee802154",
|
||||
"phy-raw_socket", "phy-tuntap_interface",
|
||||
"proto-ipv4", "proto-igmp", "proto-dhcpv4", "proto-ipv6",
|
||||
"proto-ipv4", "proto-igmp", "proto-dhcpv4", "proto-ipv6", "proto-sixlowpan",
|
||||
"socket-raw", "socket-icmp", "socket-udp", "socket-tcp", "socket-dhcpv4",
|
||||
"async"
|
||||
]
|
||||
|
@ -110,5 +116,9 @@ required-features = ["std", "medium-ethernet", "medium-ip", "phy-tuntap_interfac
|
|||
name = "dhcp_client"
|
||||
required-features = ["std", "medium-ethernet", "medium-ip", "phy-tuntap_interface", "proto-ipv4", "proto-dhcpv4", "socket-raw"]
|
||||
|
||||
[[example]]
|
||||
name = "sixlowpan"
|
||||
required-features = ["std", "medium-ieee802154", "phy-raw_socket", "proto-sixlowpan", "socket-udp"]
|
||||
|
||||
[profile.release]
|
||||
debug = 2
|
||||
|
|
|
@ -100,7 +100,7 @@ fn main() {
|
|||
let mut builder = InterfaceBuilder::new(device).ip_addrs(ip_addrs);
|
||||
if medium == Medium::Ethernet {
|
||||
builder = builder
|
||||
.ethernet_addr(ethernet_addr)
|
||||
.hardware_addr(ethernet_addr.into())
|
||||
.neighbor_cache(neighbor_cache);
|
||||
}
|
||||
let mut iface = builder.finalize();
|
||||
|
|
|
@ -47,7 +47,7 @@ fn main() {
|
|||
.routes(routes);
|
||||
if medium == Medium::Ethernet {
|
||||
builder = builder
|
||||
.ethernet_addr(ethernet_addr)
|
||||
.hardware_addr(ethernet_addr.into())
|
||||
.neighbor_cache(neighbor_cache);
|
||||
}
|
||||
let mut iface = builder.finalize();
|
||||
|
|
|
@ -39,7 +39,7 @@ fn main() {
|
|||
.routes(routes);
|
||||
if medium == Medium::Ethernet {
|
||||
builder = builder
|
||||
.ethernet_addr(ethernet_addr)
|
||||
.hardware_addr(ethernet_addr.into())
|
||||
.neighbor_cache(neighbor_cache);
|
||||
}
|
||||
let mut iface = builder.finalize();
|
||||
|
|
|
@ -53,7 +53,7 @@ fn main() {
|
|||
.routes(routes);
|
||||
if medium == Medium::Ethernet {
|
||||
builder = builder
|
||||
.ethernet_addr(ethernet_addr)
|
||||
.hardware_addr(ethernet_addr.into())
|
||||
.neighbor_cache(neighbor_cache);
|
||||
}
|
||||
let mut iface = builder.finalize();
|
||||
|
|
|
@ -95,7 +95,7 @@ fn main() {
|
|||
|
||||
let mut ip_addrs = [IpCidr::new(IpAddress::v4(127, 0, 0, 1), 8)];
|
||||
let mut iface = InterfaceBuilder::new(device)
|
||||
.ethernet_addr(EthernetAddress::default())
|
||||
.hardware_addr(EthernetAddress::default().into())
|
||||
.neighbor_cache(neighbor_cache)
|
||||
.ip_addrs(ip_addrs)
|
||||
.finalize();
|
||||
|
|
|
@ -38,7 +38,7 @@ fn main() {
|
|||
let ip_addr = IpCidr::new(IpAddress::from(local_addr), 24);
|
||||
let mut ipv4_multicast_storage = [None; 1];
|
||||
let mut iface = InterfaceBuilder::new(device)
|
||||
.ethernet_addr(ethernet_addr)
|
||||
.hardware_addr(ethernet_addr.into())
|
||||
.neighbor_cache(neighbor_cache)
|
||||
.ip_addrs([ip_addr])
|
||||
.ipv4_multicast_groups(&mut ipv4_multicast_storage[..])
|
||||
|
|
|
@ -132,7 +132,7 @@ fn main() {
|
|||
.routes(routes);
|
||||
if medium == Medium::Ethernet {
|
||||
builder = builder
|
||||
.ethernet_addr(ethernet_addr)
|
||||
.hardware_addr(ethernet_addr.into())
|
||||
.neighbor_cache(neighbor_cache);
|
||||
}
|
||||
let mut iface = builder.finalize();
|
||||
|
|
|
@ -59,7 +59,7 @@ fn main() {
|
|||
let mut builder = InterfaceBuilder::new(device).ip_addrs(ip_addrs);
|
||||
if medium == Medium::Ethernet {
|
||||
builder = builder
|
||||
.ethernet_addr(ethernet_addr)
|
||||
.hardware_addr(ethernet_addr.into())
|
||||
.neighbor_cache(neighbor_cache);
|
||||
}
|
||||
let mut iface = builder.finalize();
|
||||
|
|
|
@ -0,0 +1,134 @@
|
|||
//! 6lowpan exmaple
|
||||
//!
|
||||
//! This example is designed to run using the Linux ieee802154/6lowpan support,
|
||||
//! using mac802154_hwsim.
|
||||
//!
|
||||
//! mac802154_hwsim allows you to create multiple "virtual" radios and specify
|
||||
//! which is in range with which. This is very useful for testing without
|
||||
//! needing real hardware. By default it creates two interfaces `wpan0` and
|
||||
//! `wpan1` that are in range with each other. You can customize this with
|
||||
//! the `wpan-hwsim` tool.
|
||||
//!
|
||||
//! We'll configure Linux to speak 6lowpan on `wpan0`, and leave `wpan1`
|
||||
//! unconfigured so smoltcp can use it with a raw socket.
|
||||
//!
|
||||
//! # Setup
|
||||
//!
|
||||
//! modprobe mac802154_hwsim
|
||||
//!
|
||||
//! ip link set wpan0 down
|
||||
//! ip link set wpan1 down
|
||||
//! iwpan dev wpan0 set pan_id 0xbeef
|
||||
//! iwpan dev wpan1 set pan_id 0xbeef
|
||||
//! ip link add link wpan0 name lowpan0 type lowpan
|
||||
//! ip link set wpan0 up
|
||||
//! ip link set wpan1 up
|
||||
//! ip link set lowpan0 up
|
||||
//!
|
||||
//! # Running
|
||||
//!
|
||||
//! Run it with `sudo ./target/debug/examples/sixlowpan`.
|
||||
//!
|
||||
//! You can set wireshark to sniff on interface `wpan0` to see the packets.
|
||||
//!
|
||||
//! Ping it with `ping fe80::180b:4242:4242:4242%lowpan0`.
|
||||
//!
|
||||
//! Speak UDP with `nc -uv fe80::180b:4242:4242:4242%lowpan0 6969`.
|
||||
//!
|
||||
//! # Teardown
|
||||
//!
|
||||
//! rmmod mac802154_hwsim
|
||||
//!
|
||||
|
||||
mod utils;
|
||||
|
||||
use log::debug;
|
||||
use std::collections::BTreeMap;
|
||||
use std::os::unix::io::AsRawFd;
|
||||
use std::str;
|
||||
|
||||
use smoltcp::iface::{InterfaceBuilder, NeighborCache};
|
||||
use smoltcp::phy::{wait as phy_wait, Medium, RawSocket};
|
||||
use smoltcp::socket::SocketSet;
|
||||
use smoltcp::socket::{UdpPacketMetadata, UdpSocket, UdpSocketBuffer};
|
||||
use smoltcp::time::Instant;
|
||||
use smoltcp::wire::{Ieee802154Pan, IpAddress, IpCidr};
|
||||
|
||||
fn main() {
|
||||
utils::setup_logging("");
|
||||
|
||||
let (mut opts, mut free) = utils::create_options();
|
||||
utils::add_middleware_options(&mut opts, &mut free);
|
||||
|
||||
let mut matches = utils::parse_options(&opts, free);
|
||||
|
||||
let device = RawSocket::new("wpan1", Medium::Ieee802154).unwrap();
|
||||
|
||||
let fd = device.as_raw_fd();
|
||||
let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);
|
||||
|
||||
let neighbor_cache = NeighborCache::new(BTreeMap::new());
|
||||
|
||||
let udp_rx_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY], vec![0; 64]);
|
||||
let udp_tx_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY], vec![0; 128]);
|
||||
let udp_socket = UdpSocket::new(udp_rx_buffer, udp_tx_buffer);
|
||||
|
||||
let ieee802154_addr = smoltcp::wire::Ieee802154Address::Extended([
|
||||
0x1a, 0x0b, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
|
||||
]);
|
||||
let ip_addrs = [IpCidr::new(
|
||||
IpAddress::v6(0xfe80, 0, 0, 0, 0x180b, 0x4242, 0x4242, 0x4242),
|
||||
64,
|
||||
)];
|
||||
|
||||
let mut builder = InterfaceBuilder::new(device)
|
||||
.ip_addrs(ip_addrs)
|
||||
.pan_id(Ieee802154Pan(0xbeef));
|
||||
builder = builder
|
||||
.hardware_addr(ieee802154_addr.into())
|
||||
.neighbor_cache(neighbor_cache);
|
||||
let mut iface = builder.finalize();
|
||||
|
||||
let mut sockets = SocketSet::new(vec![]);
|
||||
let udp_handle = sockets.add(udp_socket);
|
||||
|
||||
loop {
|
||||
let timestamp = Instant::now();
|
||||
match iface.poll(&mut sockets, timestamp) {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
debug!("poll error: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// udp:6969: respond "hello"
|
||||
{
|
||||
let mut socket = sockets.get::<UdpSocket>(udp_handle);
|
||||
if !socket.is_open() {
|
||||
socket.bind(6969).unwrap()
|
||||
}
|
||||
|
||||
let client = match socket.recv() {
|
||||
Ok((data, endpoint)) => {
|
||||
debug!(
|
||||
"udp:6969 recv data: {:?} from {}",
|
||||
str::from_utf8(data).unwrap(),
|
||||
endpoint
|
||||
);
|
||||
Some(endpoint)
|
||||
}
|
||||
Err(_) => None,
|
||||
};
|
||||
if let Some(endpoint) = client {
|
||||
let data = b"hello\n";
|
||||
debug!(
|
||||
"udp:6969 send data: {:?}",
|
||||
str::from_utf8(data.as_ref()).unwrap()
|
||||
);
|
||||
socket.send_slice(data, endpoint).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
phy_wait(fd, iface.poll_delay(&sockets, timestamp)).expect("wait error");
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ use std::os::unix::io::AsRawFd;
|
|||
|
||||
fn main() {
|
||||
let ifname = env::args().nth(1).unwrap();
|
||||
let mut socket = RawSocket::new(ifname.as_ref()).unwrap();
|
||||
let mut socket = RawSocket::new(ifname.as_ref(), smoltcp::phy::Medium::Ethernet).unwrap();
|
||||
loop {
|
||||
phy_wait(socket.as_raw_fd(), None).unwrap();
|
||||
let (rx_token, _) = socket.receive().unwrap();
|
||||
|
|
|
@ -12,7 +12,6 @@ use std::process;
|
|||
use std::str::{self, FromStr};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use smoltcp::phy::RawSocket;
|
||||
#[cfg(feature = "phy-tuntap_interface")]
|
||||
use smoltcp::phy::TunTapInterface;
|
||||
use smoltcp::phy::{Device, FaultInjector, Medium, Tracer};
|
||||
|
@ -112,11 +111,6 @@ pub fn parse_tuntap_options(matches: &mut Matches) -> TunTapInterface {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn parse_raw_socket_options(matches: &mut Matches) -> RawSocket {
|
||||
let interface = matches.free.remove(0);
|
||||
RawSocket::new(&interface).unwrap()
|
||||
}
|
||||
|
||||
pub fn add_middleware_options(opts: &mut Options, _free: &mut Vec<&str>) {
|
||||
opts.optopt("", "pcap", "Write a packet capture file", "FILE");
|
||||
opts.optopt(
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -4,19 +4,27 @@ The `iface` module deals with the *network interfaces*. It filters incoming fram
|
|||
provides lookup and caching of hardware addresses, and handles management packets.
|
||||
*/
|
||||
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ip"))]
|
||||
#[cfg(any(
|
||||
feature = "medium-ethernet",
|
||||
feature = "medium-ip",
|
||||
feature = "medium-ieee802154"
|
||||
))]
|
||||
mod interface;
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
mod neighbor;
|
||||
mod route;
|
||||
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
pub(crate) use self::neighbor::Answer as NeighborAnswer;
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
pub use self::neighbor::Cache as NeighborCache;
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
pub use self::neighbor::Neighbor;
|
||||
pub use self::route::{Route, Routes};
|
||||
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ip"))]
|
||||
#[cfg(any(
|
||||
feature = "medium-ethernet",
|
||||
feature = "medium-ip",
|
||||
feature = "medium-ieee802154"
|
||||
))]
|
||||
pub use self::interface::{Interface, InterfaceBuilder};
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
use managed::ManagedMap;
|
||||
|
||||
use crate::time::{Duration, Instant};
|
||||
use crate::wire::{EthernetAddress, IpAddress};
|
||||
use crate::wire::{HardwareAddress, IpAddress};
|
||||
|
||||
/// A cached neighbor.
|
||||
///
|
||||
|
@ -13,7 +13,7 @@ use crate::wire::{EthernetAddress, IpAddress};
|
|||
#[derive(Debug, Clone, Copy)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
pub struct Neighbor {
|
||||
hardware_addr: EthernetAddress,
|
||||
hardware_addr: HardwareAddress,
|
||||
expires_at: Instant,
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@ pub struct Neighbor {
|
|||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
pub(crate) enum Answer {
|
||||
/// The neighbor address is in the cache and not expired.
|
||||
Found(EthernetAddress),
|
||||
Found(HardwareAddress),
|
||||
/// The neighbor address is not in the cache, or has expired.
|
||||
NotFound,
|
||||
/// The neighbor address is not in the cache, or has expired,
|
||||
|
@ -115,7 +115,7 @@ impl<'a> Cache<'a> {
|
|||
pub fn fill(
|
||||
&mut self,
|
||||
protocol_addr: IpAddress,
|
||||
hardware_addr: EthernetAddress,
|
||||
hardware_addr: HardwareAddress,
|
||||
timestamp: Instant,
|
||||
) {
|
||||
debug_assert!(protocol_addr.is_unicast());
|
||||
|
@ -196,9 +196,7 @@ impl<'a> Cache<'a> {
|
|||
}
|
||||
|
||||
pub(crate) fn lookup(&self, protocol_addr: &IpAddress, timestamp: Instant) -> Answer {
|
||||
if protocol_addr.is_broadcast() {
|
||||
return Answer::Found(EthernetAddress::BROADCAST);
|
||||
}
|
||||
assert!(protocol_addr.is_unicast());
|
||||
|
||||
if let Some(&Neighbor {
|
||||
expires_at,
|
||||
|
@ -228,10 +226,12 @@ mod test {
|
|||
use crate::wire::ip::test::{MOCK_IP_ADDR_1, MOCK_IP_ADDR_2, MOCK_IP_ADDR_3, MOCK_IP_ADDR_4};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
const HADDR_A: EthernetAddress = EthernetAddress([0, 0, 0, 0, 0, 1]);
|
||||
const HADDR_B: EthernetAddress = EthernetAddress([0, 0, 0, 0, 0, 2]);
|
||||
const HADDR_C: EthernetAddress = EthernetAddress([0, 0, 0, 0, 0, 3]);
|
||||
const HADDR_D: EthernetAddress = EthernetAddress([0, 0, 0, 0, 0, 4]);
|
||||
use crate::wire::EthernetAddress;
|
||||
|
||||
const HADDR_A: HardwareAddress = HardwareAddress::Ethernet(EthernetAddress([0, 0, 0, 0, 0, 1]));
|
||||
const HADDR_B: HardwareAddress = HardwareAddress::Ethernet(EthernetAddress([0, 0, 0, 0, 0, 2]));
|
||||
const HADDR_C: HardwareAddress = HardwareAddress::Ethernet(EthernetAddress([0, 0, 0, 0, 0, 3]));
|
||||
const HADDR_D: HardwareAddress = HardwareAddress::Ethernet(EthernetAddress([0, 0, 0, 0, 0, 4]));
|
||||
|
||||
#[test]
|
||||
fn test_fill() {
|
||||
|
|
21
src/lib.rs
21
src/lib.rs
|
@ -97,8 +97,12 @@ compile_error!("at least one socket needs to be enabled"); */
|
|||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
extern crate alloc;
|
||||
|
||||
#[cfg(not(any(feature = "proto-ipv4", feature = "proto-ipv6")))]
|
||||
compile_error!("You must enable at least one of the following features: proto-ipv4, proto-ipv6");
|
||||
#[cfg(not(any(
|
||||
feature = "proto-ipv4",
|
||||
feature = "proto-ipv6",
|
||||
feature = "proto-sixlowpan"
|
||||
)))]
|
||||
compile_error!("You must enable at least one of the following features: proto-ipv4, proto-ipv6, proto-sixlowpan");
|
||||
|
||||
#[cfg(all(
|
||||
feature = "socket",
|
||||
|
@ -113,9 +117,13 @@ compile_error!("If you enable the socket feature, you must enable at least one o
|
|||
|
||||
#[cfg(all(
|
||||
feature = "socket",
|
||||
not(any(feature = "medium-ethernet", feature = "medium-ip",))
|
||||
not(any(
|
||||
feature = "medium-ethernet",
|
||||
feature = "medium-ip",
|
||||
feature = "medium-ieee802154",
|
||||
))
|
||||
))]
|
||||
compile_error!("If you enable the socket feature, you must enable at least one of the following features: medium-ip, medium-ethernet");
|
||||
compile_error!("If you enable the socket feature, you must enable at least one of the following features: medium-ip, medium-ethernet, medium-ieee802154");
|
||||
|
||||
#[cfg(all(feature = "defmt", feature = "log"))]
|
||||
compile_error!("You must enable at most one of the following features: defmt, log");
|
||||
|
@ -174,6 +182,10 @@ pub enum Error {
|
|||
/// An incoming packet was recognized but contradicted internal state.
|
||||
/// E.g. a TCP packet addressed to a socket that doesn't exist.
|
||||
Dropped,
|
||||
|
||||
/// An incoming packet was recognized but some parts are not supported by smoltcp.
|
||||
/// E.g. some bit configuration in a packet header is not supported, but is defined in an RFC.
|
||||
NotSupported,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
|
@ -195,6 +207,7 @@ impl fmt::Display for Error {
|
|||
Error::Fragmented => write!(f, "fragmented packet"),
|
||||
Error::Malformed => write!(f, "malformed packet"),
|
||||
Error::Dropped => write!(f, "dropped by socket"),
|
||||
Error::NotSupported => write!(f, "not supported by smoltcp"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -254,6 +254,8 @@ impl DeviceCapabilities {
|
|||
}
|
||||
#[cfg(feature = "medium-ip")]
|
||||
Medium::Ip => self.max_transmission_unit,
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
Medium::Ieee802154 => self.max_transmission_unit, // TODO(thvdveld): what is the MTU for Medium::IEEE802
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -275,15 +277,32 @@ pub enum Medium {
|
|||
/// Examples of devices of this type are the Linux `tun`, PPP interfaces, VPNs in tun (layer 3) mode.
|
||||
#[cfg(feature = "medium-ip")]
|
||||
Ip,
|
||||
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
Ieee802154,
|
||||
}
|
||||
|
||||
impl Default for Medium {
|
||||
fn default() -> Medium {
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
return Medium::Ethernet;
|
||||
#[cfg(all(feature = "medium-ip", not(feature = "medium-ethernet")))]
|
||||
#[cfg(all(
|
||||
feature = "medium-ip",
|
||||
not(feature = "medium-ethernet"),
|
||||
not(feature = "medium-ieee802154")
|
||||
))]
|
||||
return Medium::Ip;
|
||||
#[cfg(all(not(feature = "medium-ip"), not(feature = "medium-ethernet")))]
|
||||
#[cfg(all(
|
||||
feature = "medium-ieee802154",
|
||||
not(feature = "medium-ip"),
|
||||
not(feature = "medium-ethernet")
|
||||
))]
|
||||
return Medium::Ieee802154;
|
||||
#[cfg(all(
|
||||
not(feature = "medium-ip"),
|
||||
not(feature = "medium-ethernet"),
|
||||
not(feature = "medium-ieee802154")
|
||||
))]
|
||||
panic!("No medium enabled");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,8 @@ enum_with_unknown! {
|
|||
Ethernet = 1,
|
||||
/// IPv4 or IPv6 packets (depending on the version field)
|
||||
Ip = 101,
|
||||
/// IEEE 802.15.4 packets with FCS included.
|
||||
Ieee802154WithFcs = 195,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -133,6 +135,8 @@ impl<D: for<'a> Device<'a>, S: PcapSink> PcapWriter<D, S> {
|
|||
Medium::Ip => PcapLinkType::Ip,
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
Medium::Ethernet => PcapLinkType::Ethernet,
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
Medium::Ieee802154 => PcapLinkType::Ieee802154WithFcs,
|
||||
};
|
||||
sink.global_header(link_type);
|
||||
PcapWriter {
|
||||
|
|
|
@ -11,6 +11,7 @@ use crate::Result;
|
|||
/// A socket that captures or transmits the complete frame.
|
||||
#[derive(Debug)]
|
||||
pub struct RawSocket {
|
||||
medium: Medium,
|
||||
lower: Rc<RefCell<sys::RawSocketDesc>>,
|
||||
mtu: usize,
|
||||
}
|
||||
|
@ -26,11 +27,21 @@ impl RawSocket {
|
|||
///
|
||||
/// This requires superuser privileges or a corresponding capability bit
|
||||
/// set on the executable.
|
||||
pub fn new(name: &str) -> io::Result<RawSocket> {
|
||||
let mut lower = sys::RawSocketDesc::new(name)?;
|
||||
pub fn new(name: &str, medium: Medium) -> io::Result<RawSocket> {
|
||||
let mut lower = sys::RawSocketDesc::new(name, medium)?;
|
||||
lower.bind_interface()?;
|
||||
let mtu = lower.interface_mtu()?;
|
||||
|
||||
let mut mtu = lower.interface_mtu()?;
|
||||
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
if medium == Medium::Ethernet {
|
||||
// SIOCGIFMTU returns the IP MTU (typically 1500 bytes.)
|
||||
// smoltcp counts the entire Ethernet packet in the MTU, so add the Ethernet header size to it.
|
||||
mtu += crate::wire::EthernetFrame::<&[u8]>::header_len()
|
||||
}
|
||||
|
||||
Ok(RawSocket {
|
||||
medium,
|
||||
lower: Rc::new(RefCell::new(lower)),
|
||||
mtu: mtu,
|
||||
})
|
||||
|
@ -44,7 +55,7 @@ impl<'a> Device<'a> for RawSocket {
|
|||
fn capabilities(&self) -> DeviceCapabilities {
|
||||
DeviceCapabilities {
|
||||
max_transmission_unit: self.mtu,
|
||||
medium: Medium::Ethernet,
|
||||
medium: self.medium,
|
||||
..DeviceCapabilities::default()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ use std::os::unix::io::{AsRawFd, RawFd};
|
|||
use libc;
|
||||
|
||||
use super::{ifreq, ifreq_for};
|
||||
use crate::phy::Medium;
|
||||
use crate::wire::ETHERNET_HEADER_LEN;
|
||||
|
||||
/// set interface
|
||||
|
@ -67,7 +68,7 @@ fn open_device() -> io::Result<libc::c_int> {
|
|||
}
|
||||
|
||||
impl BpfDevice {
|
||||
pub fn new(name: &str) -> io::Result<BpfDevice> {
|
||||
pub fn new(name: &str, _medium: Medium) -> io::Result<BpfDevice> {
|
||||
Ok(BpfDevice {
|
||||
fd: open_device()?,
|
||||
ifreq: ifreq_for(name),
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
pub const SIOCGIFMTU: libc::c_ulong = 0x8921;
|
||||
pub const SIOCGIFINDEX: libc::c_ulong = 0x8933;
|
||||
pub const ETH_P_ALL: libc::c_short = 0x0003;
|
||||
pub const ETH_P_IEEE802154: libc::c_short = 0x00F6;
|
||||
|
||||
pub const TUNSETIFF: libc::c_ulong = 0x400454CA;
|
||||
pub const IFF_TUN: libc::c_int = 0x0001;
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
use super::*;
|
||||
use crate::phy::Medium;
|
||||
use crate::wire::EthernetFrame;
|
||||
use std::os::unix::io::{AsRawFd, RawFd};
|
||||
use std::{io, mem};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RawSocketDesc {
|
||||
protocol: libc::c_short,
|
||||
lower: libc::c_int,
|
||||
ifreq: ifreq,
|
||||
}
|
||||
|
@ -16,12 +18,21 @@ impl AsRawFd for RawSocketDesc {
|
|||
}
|
||||
|
||||
impl RawSocketDesc {
|
||||
pub fn new(name: &str) -> io::Result<RawSocketDesc> {
|
||||
pub fn new(name: &str, medium: Medium) -> io::Result<RawSocketDesc> {
|
||||
let protocol = match medium {
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
Medium::Ethernet => imp::ETH_P_ALL,
|
||||
#[cfg(feature = "medium-ip")]
|
||||
Medium::Ip => imp::ETH_P_ALL,
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
Medium::Ieee802154 => imp::ETH_P_IEEE802154,
|
||||
};
|
||||
|
||||
let lower = unsafe {
|
||||
let lower = libc::socket(
|
||||
libc::AF_PACKET,
|
||||
libc::SOCK_RAW | libc::SOCK_NONBLOCK,
|
||||
imp::ETH_P_ALL.to_be() as i32,
|
||||
protocol.to_be() as i32,
|
||||
);
|
||||
if lower == -1 {
|
||||
return Err(io::Error::last_os_error());
|
||||
|
@ -30,7 +41,8 @@ impl RawSocketDesc {
|
|||
};
|
||||
|
||||
Ok(RawSocketDesc {
|
||||
lower: lower,
|
||||
protocol,
|
||||
lower,
|
||||
ifreq: ifreq_for(name),
|
||||
})
|
||||
}
|
||||
|
@ -46,7 +58,7 @@ impl RawSocketDesc {
|
|||
pub fn bind_interface(&mut self) -> io::Result<()> {
|
||||
let sockaddr = libc::sockaddr_ll {
|
||||
sll_family: libc::AF_PACKET as u16,
|
||||
sll_protocol: imp::ETH_P_ALL.to_be() as u16,
|
||||
sll_protocol: self.protocol.to_be() as u16,
|
||||
sll_ifindex: ifreq_ioctl(self.lower, &mut self.ifreq, imp::SIOCGIFINDEX)?,
|
||||
sll_hatype: 1,
|
||||
sll_pkttype: 0,
|
||||
|
|
|
@ -42,6 +42,8 @@ impl TunTapInterfaceDesc {
|
|||
Medium::Ip => imp::IFF_TUN,
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
Medium::Ethernet => imp::IFF_TAP,
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
Medium::Ieee802154 => todo!(),
|
||||
};
|
||||
self.ifreq.ifr_data = mode | imp::IFF_NO_PI;
|
||||
ifreq_ioctl(self.lower, &mut self.ifreq, imp::TUNSETIFF).map(|_| ())
|
||||
|
@ -72,6 +74,8 @@ impl TunTapInterfaceDesc {
|
|||
Medium::Ip => ip_mtu,
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
Medium::Ethernet => ip_mtu + EthernetFrame::<&[u8]>::header_len(),
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
Medium::Ieee802154 => todo!(),
|
||||
};
|
||||
|
||||
Ok(mtu)
|
||||
|
|
|
@ -190,6 +190,8 @@ impl<'a> fmt::Display for Packet<'a> {
|
|||
}
|
||||
_ => f.write_str("unrecognized IP version"),
|
||||
},
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
Medium::Ieee802154 => Ok(()), // XXX
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ use crate::socket::SocketHandle;
|
|||
use crate::socket::{Context, SocketMeta};
|
||||
use crate::time::{Duration, Instant};
|
||||
use crate::wire::dhcpv4::field as dhcpv4_field;
|
||||
use crate::wire::HardwareAddress;
|
||||
use crate::wire::{
|
||||
DhcpMessageType, DhcpPacket, DhcpRepr, IpAddress, IpProtocol, Ipv4Address, Ipv4Cidr, Ipv4Repr,
|
||||
UdpRepr, DHCP_CLIENT_PORT, DHCP_MAX_DNS_SERVER_COUNT, DHCP_SERVER_PORT, UDP_HEADER_LEN,
|
||||
|
@ -218,7 +219,13 @@ impl Dhcpv4Socket {
|
|||
return Ok(());
|
||||
}
|
||||
};
|
||||
if dhcp_repr.client_hardware_address != cx.ethernet_address.unwrap() {
|
||||
let hardware_addr = if let Some(HardwareAddress::Ethernet(addr)) = cx.hardware_addr {
|
||||
addr
|
||||
} else {
|
||||
return Err(Error::Malformed);
|
||||
};
|
||||
|
||||
if dhcp_repr.client_hardware_address != hardware_addr {
|
||||
return Ok(());
|
||||
}
|
||||
if dhcp_repr.transaction_id != self.transaction_id {
|
||||
|
@ -381,7 +388,11 @@ impl Dhcpv4Socket {
|
|||
{
|
||||
// note: Dhcpv4Socket is only usable in ethernet mediums, so the
|
||||
// unwrap can never fail.
|
||||
let ethernet_addr = cx.ethernet_address.unwrap();
|
||||
let ethernet_addr = if let Some(HardwareAddress::Ethernet(addr)) = cx.hardware_addr {
|
||||
addr
|
||||
} else {
|
||||
return Err(Error::Malformed);
|
||||
};
|
||||
|
||||
// Worst case biggest IPv4 header length.
|
||||
// 0x0f * 4 = 60 bytes.
|
||||
|
|
|
@ -187,8 +187,13 @@ from_socket!(Dhcpv4Socket, Dhcpv4);
|
|||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct Context {
|
||||
pub now: Instant,
|
||||
#[cfg(all(feature = "medium-ethernet", feature = "socket-dhcpv4"))]
|
||||
pub ethernet_address: Option<crate::wire::EthernetAddress>,
|
||||
#[cfg(all(
|
||||
any(feature = "medium-ethernet", feature = "medium-ieee802154"),
|
||||
feature = "socket-dhcpv4"
|
||||
))]
|
||||
pub hardware_addr: Option<crate::wire::HardwareAddress>,
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
pub pan_id: Option<crate::wire::Ieee802154Pan>,
|
||||
pub caps: DeviceCapabilities,
|
||||
}
|
||||
|
||||
|
@ -215,10 +220,16 @@ impl Context {
|
|||
#[cfg(not(feature = "medium-ethernet"))]
|
||||
max_transmission_unit: 1500,
|
||||
},
|
||||
#[cfg(all(feature = "medium-ethernet", feature = "socket-dhcpv4"))]
|
||||
ethernet_address: Some(crate::wire::EthernetAddress([
|
||||
0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
|
||||
])),
|
||||
#[cfg(all(
|
||||
any(feature = "medium-ethernet", feature = "medium-ieee802154"),
|
||||
feature = "socket-dhcpv4"
|
||||
))]
|
||||
hardware_addr: Some(crate::wire::HardwareAddress::Ethernet(
|
||||
crate::wire::EthernetAddress([0x02, 0x02, 0x02, 0x02, 0x02, 0x02]),
|
||||
)),
|
||||
now: Instant::from_millis_const(0),
|
||||
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
pan_id: Some(crate::wire::Ieee802154Pan(0xabcd)),
|
||||
};
|
||||
}
|
||||
|
|
|
@ -69,9 +69,7 @@ impl<'a> Set<'a> {
|
|||
}
|
||||
|
||||
match self.sockets {
|
||||
ManagedSlice::Borrowed(_) => {
|
||||
panic!("adding a socket to a full SocketSet")
|
||||
}
|
||||
ManagedSlice::Borrowed(_) => panic!("adding a socket to a full SocketSet"),
|
||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||
ManagedSlice::Owned(ref mut sockets) => {
|
||||
sockets.push(None);
|
||||
|
|
|
@ -4,7 +4,7 @@ use core::{cmp, fmt};
|
|||
use crate::phy::ChecksumCapabilities;
|
||||
use crate::wire::ip::checksum;
|
||||
use crate::wire::MldRepr;
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
use crate::wire::NdiscRepr;
|
||||
use crate::wire::{IpAddress, IpProtocol, Ipv6Packet, Ipv6Repr};
|
||||
use crate::{Error, Result};
|
||||
|
@ -532,7 +532,7 @@ pub enum Repr<'a> {
|
|||
seq_no: u16,
|
||||
data: &'a [u8],
|
||||
},
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
Ndisc(NdiscRepr<'a>),
|
||||
Mld(MldRepr<'a>),
|
||||
}
|
||||
|
@ -617,7 +617,7 @@ impl<'a> Repr<'a> {
|
|||
seq_no: packet.echo_seq_no(),
|
||||
data: packet.payload(),
|
||||
}),
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
(msg_type, 0) if msg_type.is_ndisc() => NdiscRepr::parse(packet).map(Repr::Ndisc),
|
||||
(msg_type, 0) if msg_type.is_mld() => MldRepr::parse(packet).map(Repr::Mld),
|
||||
_ => Err(Error::Unrecognized),
|
||||
|
@ -636,7 +636,7 @@ impl<'a> Repr<'a> {
|
|||
&Repr::EchoRequest { data, .. } | &Repr::EchoReply { data, .. } => {
|
||||
field::ECHO_SEQNO.end + data.len()
|
||||
}
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
&Repr::Ndisc(ndisc) => ndisc.buffer_len(),
|
||||
&Repr::Mld(mld) => mld.buffer_len(),
|
||||
}
|
||||
|
@ -730,7 +730,7 @@ impl<'a> Repr<'a> {
|
|||
packet.payload_mut()[..data_len].copy_from_slice(&data[..data_len])
|
||||
}
|
||||
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
Repr::Ndisc(ndisc) => ndisc.emit(packet),
|
||||
|
||||
Repr::Mld(mld) => mld.emit(packet),
|
||||
|
|
|
@ -0,0 +1,897 @@
|
|||
use core::fmt;
|
||||
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
|
||||
use crate::wire::ipv6::Address as Ipv6Address;
|
||||
use crate::Error;
|
||||
use crate::Result;
|
||||
|
||||
const CRC_TABLE: [u16; 256] = [
|
||||
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3,
|
||||
0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
|
||||
0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 0x2102, 0x308b, 0x0210, 0x1399,
|
||||
0x6726, 0x76af, 0x4434, 0x55bd, 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
|
||||
0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 0xbdcb, 0xac42, 0x9ed9, 0x8f50,
|
||||
0xfbef, 0xea66, 0xd8fd, 0xc974, 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
|
||||
0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 0x5285, 0x430c, 0x7197, 0x601e,
|
||||
0x14a1, 0x0528, 0x37b3, 0x263a, 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
|
||||
0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 0xef4e, 0xfec7, 0xcc5c, 0xddd5,
|
||||
0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
|
||||
0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 0x8408, 0x9581, 0xa71a, 0xb693,
|
||||
0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
|
||||
0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 0x18c1, 0x0948, 0x3bd3, 0x2a5a,
|
||||
0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
|
||||
0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 0xb58b, 0xa402, 0x9699, 0x8710,
|
||||
0xf3af, 0xe226, 0xd0bd, 0xc134, 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
|
||||
0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 0x4a44, 0x5bcd, 0x6956, 0x78df,
|
||||
0x0c60, 0x1de9, 0x2f72, 0x3efb, 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
|
||||
0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 0xe70e, 0xf687, 0xc41c, 0xd595,
|
||||
0xa12a, 0xb0a3, 0x8238, 0x93b1, 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
|
||||
0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 0x7bc7, 0x6a4e, 0x58d5, 0x495c,
|
||||
0x3de3, 0x2c6a, 0x1ef1, 0x0f78,
|
||||
];
|
||||
|
||||
pub fn calculate_crc(buffer: &[u8]) -> u16 {
|
||||
fn crc_byte(crc: u16, c: u8) -> u16 {
|
||||
(crc >> 8) ^ CRC_TABLE[((crc ^ (c as u16)) & 0xff) as usize]
|
||||
}
|
||||
|
||||
let mut crc = 0;
|
||||
|
||||
for b in buffer {
|
||||
crc = crc_byte(crc, *b);
|
||||
}
|
||||
|
||||
crc
|
||||
}
|
||||
|
||||
enum_with_unknown! {
|
||||
/// IEEE 802.15.4 frame type.
|
||||
pub enum FrameType(u8) {
|
||||
Beacon = 0b000,
|
||||
Data = 0b001,
|
||||
Acknowledgement = 0b010,
|
||||
MacCommand = 0b011,
|
||||
Multipurpose = 0b101,
|
||||
FragmentOrFrak = 0b110,
|
||||
Extended = 0b111,
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for FrameType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
FrameType::Beacon => write!(f, "Beacon"),
|
||||
FrameType::Data => write!(f, "Data"),
|
||||
FrameType::Acknowledgement => write!(f, "Ack"),
|
||||
FrameType::MacCommand => write!(f, "MAC command"),
|
||||
FrameType::Multipurpose => write!(f, "Multipurpose"),
|
||||
FrameType::FragmentOrFrak => write!(f, "FragmentOrFrak"),
|
||||
FrameType::Extended => write!(f, "Extended"),
|
||||
FrameType::Unknown(id) => write!(f, "0b{:04b}", id),
|
||||
}
|
||||
}
|
||||
}
|
||||
enum_with_unknown! {
|
||||
/// IEEE 802.15.4 addressing mode for destination and source addresses.
|
||||
pub enum AddressingMode(u8) {
|
||||
Absent = 0b00,
|
||||
Short = 0b10,
|
||||
Extended = 0b11,
|
||||
}
|
||||
}
|
||||
|
||||
impl AddressingMode {
|
||||
/// Return the size in octets of the address.
|
||||
fn size(&self) -> usize {
|
||||
match self {
|
||||
AddressingMode::Absent => 0,
|
||||
AddressingMode::Short => 2,
|
||||
AddressingMode::Extended => 8,
|
||||
AddressingMode::Unknown(_) => 0, // TODO(thvdveld): what do we need to here?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for AddressingMode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
AddressingMode::Absent => write!(f, "Absent"),
|
||||
AddressingMode::Short => write!(f, "Short"),
|
||||
AddressingMode::Extended => write!(f, "Extended"),
|
||||
AddressingMode::Unknown(id) => write!(f, "0b{:04b}", id),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A IEEE 802.15.4 PAN.
|
||||
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
|
||||
pub struct Pan(pub u16);
|
||||
|
||||
impl Pan {
|
||||
pub const BROADCAST: Self = Self(0xffff);
|
||||
|
||||
/// Return the PAN ID as bytes.
|
||||
pub fn as_bytes(&self) -> [u8; 2] {
|
||||
let mut pan = [0u8; 2];
|
||||
LittleEndian::write_u16(&mut pan, self.0);
|
||||
pan
|
||||
}
|
||||
}
|
||||
|
||||
/// A IEEE 802.15.4 address.
|
||||
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
|
||||
pub enum Address {
|
||||
Absent,
|
||||
Short([u8; 2]),
|
||||
Extended([u8; 8]),
|
||||
}
|
||||
|
||||
impl Address {
|
||||
/// The broadcast address.
|
||||
pub const BROADCAST: Address = Address::Short([0xff; 2]);
|
||||
|
||||
/// Query whether the address is an unicast address.
|
||||
pub fn is_unicast(&self) -> bool {
|
||||
!self.is_broadcast()
|
||||
}
|
||||
|
||||
/// Query whether this address is the broadcast address.
|
||||
pub fn is_broadcast(&self) -> bool {
|
||||
*self == Self::BROADCAST
|
||||
}
|
||||
|
||||
fn short_from_bytes(a: [u8; 2]) -> Self {
|
||||
Self::Short(a)
|
||||
}
|
||||
|
||||
fn extended_from_bytes(a: [u8; 8]) -> Self {
|
||||
Self::Extended(a)
|
||||
}
|
||||
|
||||
pub fn from_bytes(a: &[u8]) -> Self {
|
||||
if a.len() == 2 {
|
||||
let mut b = [0u8; 2];
|
||||
b.copy_from_slice(a);
|
||||
Address::Short(b)
|
||||
} else if a.len() == 8 {
|
||||
let mut b = [0u8; 8];
|
||||
b.copy_from_slice(a);
|
||||
Address::Extended(b)
|
||||
} else {
|
||||
panic!("Not an IEEE802.15.4 address");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_bytes(&self) -> &[u8] {
|
||||
match self {
|
||||
Address::Absent => &[],
|
||||
Address::Short(value) => value,
|
||||
Address::Extended(value) => value,
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert the extended address to an Extended Unique Identifier (EUI-64)
|
||||
pub fn as_eui_64(&self) -> Option<[u8; 8]> {
|
||||
match self {
|
||||
Address::Absent | Address::Short(_) => None,
|
||||
Address::Extended(value) => {
|
||||
let mut bytes = [0; 8];
|
||||
bytes.copy_from_slice(&value[..]);
|
||||
|
||||
bytes[0] ^= 1 << 1;
|
||||
|
||||
Some(bytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert an extended address to a link-local IPv6 address using the EUI-64 format from
|
||||
/// RFC2464.
|
||||
pub fn as_link_local_address(&self) -> Option<Ipv6Address> {
|
||||
let mut bytes = [0; 16];
|
||||
bytes[0] = 0xfe;
|
||||
bytes[1] = 0x80;
|
||||
bytes[8..].copy_from_slice(&self.as_eui_64()?);
|
||||
|
||||
Some(Ipv6Address::from_bytes(&bytes))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Address {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Absent => write!(f, "not-present"),
|
||||
Self::Short(bytes) => write!(f, "{:02x}-{:02x}", bytes[0], bytes[1]),
|
||||
Self::Extended(bytes) => write!(
|
||||
f,
|
||||
"{:02x}-{:02x}-{:02x}-{:02x}-{:02x}-{:02x}-{:02x}-{:02x}",
|
||||
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum_with_unknown! {
|
||||
/// IEEE 802.15.4 addressing mode for destination and source addresses.
|
||||
pub enum FrameVersion(u8) {
|
||||
Ieee802154_2003 = 0b00,
|
||||
Ieee802154_2006 = 0b01,
|
||||
Ieee802154 = 0b10,
|
||||
}
|
||||
}
|
||||
|
||||
/// A read/write wrapper around an IEEE 802.15.4 frame buffer.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Frame<T: AsRef<[u8]>> {
|
||||
buffer: T,
|
||||
}
|
||||
|
||||
mod field {
|
||||
use crate::wire::field::*;
|
||||
|
||||
pub const FRAMECONTROL: Field = 0..2;
|
||||
pub const SEQUENCE_NUMBER: usize = 2;
|
||||
pub const ADDRESSING: Rest = 3..;
|
||||
}
|
||||
|
||||
macro_rules! fc_bit_field {
|
||||
($field:ident, $bit:literal) => {
|
||||
#[inline]
|
||||
pub fn $field(&self) -> bool {
|
||||
let data = self.buffer.as_ref();
|
||||
let raw = LittleEndian::read_u16(&data[field::FRAMECONTROL]);
|
||||
|
||||
((raw >> $bit) & 0b1) == 0b1
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! set_fc_bit_field {
|
||||
($field:ident, $bit:literal) => {
|
||||
#[inline]
|
||||
pub fn $field(&mut self, val: bool) {
|
||||
let data = &mut self.buffer.as_mut()[field::FRAMECONTROL];
|
||||
let mut raw = LittleEndian::read_u16(data);
|
||||
raw |= ((val as u16) << $bit);
|
||||
|
||||
data.copy_from_slice(&raw.to_le_bytes());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl<T: AsRef<[u8]>> Frame<T> {
|
||||
/// Input a raw octet buffer with Ethernet frame structure.
|
||||
pub fn new_unchecked(buffer: T) -> Frame<T> {
|
||||
Frame { buffer }
|
||||
}
|
||||
|
||||
/// Shorthand for a combination of [new_unchecked] and [check_len].
|
||||
///
|
||||
/// [new_unchecked]: #method.new_unchecked
|
||||
/// [check_len]: #method.check_len
|
||||
pub fn new_checked(buffer: T) -> Result<Frame<T>> {
|
||||
let packet = Self::new_unchecked(buffer);
|
||||
packet.check_len()?;
|
||||
|
||||
if matches!(packet.dst_addressing_mode(), AddressingMode::Unknown(_)) {
|
||||
return Err(Error::Malformed);
|
||||
}
|
||||
|
||||
if matches!(packet.src_addressing_mode(), AddressingMode::Unknown(_)) {
|
||||
return Err(Error::Malformed);
|
||||
}
|
||||
|
||||
Ok(packet)
|
||||
}
|
||||
|
||||
/// Ensure that no accessor method will panic if called.
|
||||
/// Returns `Err(Error::Truncated)` if the buffer is too short.
|
||||
pub fn check_len(&self) -> Result<()> {
|
||||
if self.buffer.as_ref().is_empty() {
|
||||
Err(Error::Truncated)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Consumes the frame, returning the underlying buffer.
|
||||
pub fn into_inner(self) -> T {
|
||||
self.buffer
|
||||
}
|
||||
|
||||
/// Return the FrameType field.
|
||||
#[inline]
|
||||
pub fn frame_type(&self) -> FrameType {
|
||||
let data = self.buffer.as_ref();
|
||||
let raw = LittleEndian::read_u16(&data[field::FRAMECONTROL]);
|
||||
let ft = (raw & 0b11) as u8;
|
||||
FrameType::from(ft)
|
||||
}
|
||||
|
||||
fc_bit_field!(security_enabled, 3);
|
||||
fc_bit_field!(frame_pending, 4);
|
||||
fc_bit_field!(ack_request, 5);
|
||||
fc_bit_field!(pan_id_compression, 6);
|
||||
|
||||
fc_bit_field!(sequence_number_suppression, 8);
|
||||
fc_bit_field!(ie_present, 9);
|
||||
|
||||
/// Return the destination addressing mode.
|
||||
#[inline]
|
||||
pub fn dst_addressing_mode(&self) -> AddressingMode {
|
||||
let data = self.buffer.as_ref();
|
||||
let raw = LittleEndian::read_u16(&data[field::FRAMECONTROL]);
|
||||
let am = ((raw >> 10) & 0b11) as u8;
|
||||
AddressingMode::from(am)
|
||||
}
|
||||
|
||||
/// Return the frame version.
|
||||
#[inline]
|
||||
pub fn frame_version(&self) -> FrameVersion {
|
||||
let data = self.buffer.as_ref();
|
||||
let raw = LittleEndian::read_u16(&data[field::FRAMECONTROL]);
|
||||
let fv = ((raw >> 12) & 0b11) as u8;
|
||||
FrameVersion::from(fv)
|
||||
}
|
||||
|
||||
/// Return the source addressing mode.
|
||||
#[inline]
|
||||
pub fn src_addressing_mode(&self) -> AddressingMode {
|
||||
let data = self.buffer.as_ref();
|
||||
let raw = LittleEndian::read_u16(&data[field::FRAMECONTROL]);
|
||||
let am = ((raw >> 14) & 0b11) as u8;
|
||||
AddressingMode::from(am)
|
||||
}
|
||||
|
||||
/// Return the sequence number of the frame.
|
||||
#[inline]
|
||||
pub fn sequence_number(&self) -> Option<u8> {
|
||||
match self.frame_type() {
|
||||
FrameType::Beacon
|
||||
| FrameType::Data
|
||||
| FrameType::Acknowledgement
|
||||
| FrameType::MacCommand
|
||||
| FrameType::Multipurpose => {
|
||||
let data = self.buffer.as_ref();
|
||||
let raw = data[field::SEQUENCE_NUMBER];
|
||||
Some(raw)
|
||||
}
|
||||
FrameType::Extended | FrameType::FragmentOrFrak | FrameType::Unknown(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the addressing fields.
|
||||
#[inline]
|
||||
fn addressing_fields(&self) -> Option<&[u8]> {
|
||||
match self.frame_type() {
|
||||
FrameType::Beacon
|
||||
| FrameType::Data
|
||||
| FrameType::MacCommand
|
||||
| FrameType::Multipurpose => (),
|
||||
FrameType::Acknowledgement if self.frame_version() == FrameVersion::Ieee802154 => (),
|
||||
FrameType::Acknowledgement
|
||||
| FrameType::Extended
|
||||
| FrameType::FragmentOrFrak
|
||||
| FrameType::Unknown(_) => return None,
|
||||
}
|
||||
|
||||
let mut offset = 2;
|
||||
|
||||
// Calculate the size of the addressing field.
|
||||
offset += self.dst_addressing_mode().size();
|
||||
offset += self.src_addressing_mode().size();
|
||||
|
||||
if !self.pan_id_compression() {
|
||||
offset += 2;
|
||||
}
|
||||
|
||||
Some(&self.buffer.as_ref()[field::ADDRESSING][..offset])
|
||||
}
|
||||
|
||||
/// Return the destination PAN field.
|
||||
#[inline]
|
||||
pub fn dst_pan_id(&self) -> Option<Pan> {
|
||||
let addressing_fields = self.addressing_fields()?;
|
||||
match self.dst_addressing_mode() {
|
||||
AddressingMode::Absent => None,
|
||||
AddressingMode::Short | AddressingMode::Extended => {
|
||||
Some(Pan(LittleEndian::read_u16(&addressing_fields[0..2])))
|
||||
}
|
||||
AddressingMode::Unknown(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the destination address field.
|
||||
#[inline]
|
||||
pub fn dst_addr(&self) -> Option<Address> {
|
||||
let addressing_fields = self.addressing_fields()?;
|
||||
match self.dst_addressing_mode() {
|
||||
AddressingMode::Absent => Some(Address::Absent),
|
||||
AddressingMode::Short => {
|
||||
let mut raw = [0u8; 2];
|
||||
raw.clone_from_slice(&addressing_fields[2..4]);
|
||||
raw.reverse();
|
||||
Some(Address::short_from_bytes(raw))
|
||||
}
|
||||
AddressingMode::Extended => {
|
||||
let mut raw = [0u8; 8];
|
||||
raw.clone_from_slice(&addressing_fields[2..10]);
|
||||
raw.reverse();
|
||||
Some(Address::extended_from_bytes(raw))
|
||||
}
|
||||
AddressingMode::Unknown(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the destination PAN field.
|
||||
#[inline]
|
||||
pub fn src_pan_id(&self) -> Option<Pan> {
|
||||
if self.pan_id_compression() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let addressing_fields = self.addressing_fields()?;
|
||||
let offset = self.dst_addressing_mode().size() + 2;
|
||||
|
||||
match self.src_addressing_mode() {
|
||||
AddressingMode::Absent => None,
|
||||
AddressingMode::Short | AddressingMode::Extended => Some(Pan(LittleEndian::read_u16(
|
||||
&addressing_fields[offset..offset + 2],
|
||||
))),
|
||||
AddressingMode::Unknown(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the source address field.
|
||||
#[inline]
|
||||
pub fn src_addr(&self) -> Option<Address> {
|
||||
let addressing_fields = self.addressing_fields()?;
|
||||
let mut offset = match self.dst_addressing_mode() {
|
||||
AddressingMode::Absent => 0,
|
||||
AddressingMode::Short => 2,
|
||||
AddressingMode::Extended => 8,
|
||||
_ => return None, // TODO(thvdveld): what do we do here?
|
||||
} + 2;
|
||||
|
||||
if !self.pan_id_compression() {
|
||||
offset += 2;
|
||||
}
|
||||
|
||||
match self.src_addressing_mode() {
|
||||
AddressingMode::Absent => Some(Address::Absent),
|
||||
AddressingMode::Short => {
|
||||
let mut raw = [0u8; 2];
|
||||
raw.clone_from_slice(&addressing_fields[offset..offset + 2]);
|
||||
raw.reverse();
|
||||
Some(Address::short_from_bytes(raw))
|
||||
}
|
||||
AddressingMode::Extended => {
|
||||
let mut raw = [0u8; 8];
|
||||
raw.clone_from_slice(&addressing_fields[offset..offset + 8]);
|
||||
raw.reverse();
|
||||
Some(Address::extended_from_bytes(raw))
|
||||
}
|
||||
AddressingMode::Unknown(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the Auxilliary Security Header Field
|
||||
#[inline]
|
||||
pub fn aux_security_header(&self) -> Option<&[u8]> {
|
||||
match self.frame_type() {
|
||||
FrameType::Beacon
|
||||
| FrameType::Data
|
||||
| FrameType::MacCommand
|
||||
| FrameType::Multipurpose => (),
|
||||
FrameType::Acknowledgement if self.frame_version() == FrameVersion::Ieee802154 => (),
|
||||
FrameType::Acknowledgement
|
||||
| FrameType::Extended
|
||||
| FrameType::FragmentOrFrak
|
||||
| FrameType::Unknown(_) => return None,
|
||||
}
|
||||
|
||||
if !self.security_enabled() {
|
||||
return None;
|
||||
}
|
||||
|
||||
net_debug!("Auxilliary security header is currently not supported.");
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: AsRef<[u8]> + ?Sized> Frame<&'a T> {
|
||||
/// Return a pointer to the payload.
|
||||
#[inline]
|
||||
pub fn payload(&self) -> Option<&'a [u8]> {
|
||||
match self.frame_type() {
|
||||
FrameType::Data => {
|
||||
let data = &self.buffer.as_ref()[field::ADDRESSING];
|
||||
let offset = self.addressing_fields().unwrap().len();
|
||||
|
||||
Some(&data[offset..])
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AsRef<[u8]> + AsMut<[u8]>> Frame<T> {
|
||||
/// Set the frame type.
|
||||
#[inline]
|
||||
pub fn set_frame_type(&mut self, frame_type: FrameType) {
|
||||
let data = &mut self.buffer.as_mut()[field::FRAMECONTROL];
|
||||
let mut raw = LittleEndian::read_u16(data);
|
||||
|
||||
raw = (raw & !(0b111)) | (u8::from(frame_type) as u16 & 0b111);
|
||||
data.copy_from_slice(&raw.to_le_bytes());
|
||||
}
|
||||
|
||||
set_fc_bit_field!(set_security_enabled, 3);
|
||||
set_fc_bit_field!(set_frame_pending, 4);
|
||||
set_fc_bit_field!(set_ack_request, 5);
|
||||
set_fc_bit_field!(set_pan_id_compression, 6);
|
||||
|
||||
/// Set the frame version.
|
||||
#[inline]
|
||||
pub fn set_frame_version(&mut self, version: FrameVersion) {
|
||||
let data = &mut self.buffer.as_mut()[field::FRAMECONTROL];
|
||||
let mut raw = LittleEndian::read_u16(data);
|
||||
|
||||
raw = (raw & !(0b11 << 12)) | ((u8::from(version) as u16 & 0b11) << 12);
|
||||
data.copy_from_slice(&raw.to_le_bytes());
|
||||
}
|
||||
|
||||
/// Set the frame sequence number.
|
||||
#[inline]
|
||||
pub fn set_sequence_number(&mut self, value: u8) {
|
||||
let data = self.buffer.as_mut();
|
||||
data[field::SEQUENCE_NUMBER] = value;
|
||||
}
|
||||
|
||||
/// Set the destination PAN ID.
|
||||
#[inline]
|
||||
pub fn set_dst_pan_id(&mut self, value: Pan) {
|
||||
// NOTE the destination addressing mode must be different than Absent.
|
||||
// This is the reason why we set it to Extended.
|
||||
self.set_dst_addressing_mode(AddressingMode::Extended);
|
||||
|
||||
let data = self.buffer.as_mut();
|
||||
data[field::ADDRESSING][..2].copy_from_slice(&value.as_bytes());
|
||||
}
|
||||
|
||||
/// Set the destination address.
|
||||
#[inline]
|
||||
pub fn set_dst_addr(&mut self, mut value: Address) {
|
||||
match value {
|
||||
Address::Absent => self.set_dst_addressing_mode(AddressingMode::Absent),
|
||||
Address::Short(ref mut value) => {
|
||||
value.reverse();
|
||||
self.set_dst_addressing_mode(AddressingMode::Short);
|
||||
let data = self.buffer.as_mut();
|
||||
data[field::ADDRESSING][2..2 + 2].copy_from_slice(value);
|
||||
value.reverse();
|
||||
}
|
||||
Address::Extended(ref mut value) => {
|
||||
value.reverse();
|
||||
self.set_dst_addressing_mode(AddressingMode::Extended);
|
||||
let data = &mut self.buffer.as_mut()[field::ADDRESSING];
|
||||
data[2..2 + 8].copy_from_slice(value);
|
||||
value.reverse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the destination addressing mode.
|
||||
#[inline]
|
||||
fn set_dst_addressing_mode(&mut self, value: AddressingMode) {
|
||||
let data = &mut self.buffer.as_mut()[field::FRAMECONTROL];
|
||||
let mut raw = LittleEndian::read_u16(data);
|
||||
|
||||
raw = (raw & !(0b11 << 10)) | ((u8::from(value) as u16 & 0b11) << 10);
|
||||
data.copy_from_slice(&raw.to_le_bytes());
|
||||
}
|
||||
|
||||
/// Set the source PAN ID.
|
||||
#[inline]
|
||||
pub fn set_src_pan_id(&mut self, value: Pan) {
|
||||
let offset = match self.dst_addressing_mode() {
|
||||
AddressingMode::Absent => 0,
|
||||
AddressingMode::Short => 2,
|
||||
AddressingMode::Extended => 8,
|
||||
_ => unreachable!(),
|
||||
} + 2;
|
||||
|
||||
let data = &mut self.buffer.as_mut()[field::ADDRESSING];
|
||||
data[offset..offset + 2].copy_from_slice(&value.as_bytes());
|
||||
}
|
||||
|
||||
/// Set the source address.
|
||||
#[inline]
|
||||
pub fn set_src_addr(&mut self, mut value: Address) {
|
||||
let offset = match self.dst_addressing_mode() {
|
||||
AddressingMode::Absent => 0,
|
||||
AddressingMode::Short => 2,
|
||||
AddressingMode::Extended => 8,
|
||||
_ => unreachable!(),
|
||||
} + 2;
|
||||
|
||||
let offset = offset + if self.pan_id_compression() { 0 } else { 2 };
|
||||
|
||||
match value {
|
||||
Address::Absent => self.set_src_addressing_mode(AddressingMode::Absent),
|
||||
Address::Short(ref mut value) => {
|
||||
value.reverse();
|
||||
self.set_src_addressing_mode(AddressingMode::Short);
|
||||
let data = &mut self.buffer.as_mut()[field::ADDRESSING];
|
||||
data[offset..offset + 2].copy_from_slice(value);
|
||||
value.reverse();
|
||||
}
|
||||
Address::Extended(ref mut value) => {
|
||||
value.reverse();
|
||||
self.set_src_addressing_mode(AddressingMode::Extended);
|
||||
let data = &mut self.buffer.as_mut()[field::ADDRESSING];
|
||||
data[offset..offset + 8].copy_from_slice(value);
|
||||
value.reverse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the source addressing mode.
|
||||
#[inline]
|
||||
fn set_src_addressing_mode(&mut self, value: AddressingMode) {
|
||||
let data = &mut self.buffer.as_mut()[field::FRAMECONTROL];
|
||||
let mut raw = LittleEndian::read_u16(data);
|
||||
|
||||
raw = (raw & !(0b11 << 14)) | ((u8::from(value) as u16 & 0b11) << 14);
|
||||
data.copy_from_slice(&raw.to_le_bytes());
|
||||
}
|
||||
|
||||
/// Return a mutable pointer to the payload.
|
||||
#[inline]
|
||||
pub fn payload_mut(&mut self) -> Option<&mut [u8]> {
|
||||
match self.frame_type() {
|
||||
FrameType::Data => {
|
||||
let mut start_offset = 3;
|
||||
start_offset += self.addressing_fields().unwrap().len();
|
||||
|
||||
let data = self.buffer.as_mut();
|
||||
let end_offset = start_offset + data.len() - 2;
|
||||
Some(&mut data[start_offset..end_offset])
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AsRef<[u8]>> fmt::Display for Frame<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"IEEE802.15.4 frame type={} seq={:2x?} dst_pan={:x?} dest={:x?} src_pan={:?} src={:x?}",
|
||||
self.frame_type(),
|
||||
self.sequence_number(),
|
||||
self.dst_pan_id(),
|
||||
self.dst_addr(),
|
||||
self.src_pan_id(),
|
||||
self.src_addr(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// A high-level representation of an IEEE802.15.4 frame.
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub struct Repr {
|
||||
pub frame_type: FrameType,
|
||||
pub security_enabled: bool,
|
||||
pub frame_pending: bool,
|
||||
pub ack_request: bool,
|
||||
pub sequence_number: Option<u8>,
|
||||
pub pan_id_compression: bool,
|
||||
pub frame_version: FrameVersion,
|
||||
pub dst_pan_id: Option<Pan>,
|
||||
pub dst_addr: Option<Address>,
|
||||
pub src_pan_id: Option<Pan>,
|
||||
pub src_addr: Option<Address>,
|
||||
}
|
||||
|
||||
impl Repr {
|
||||
/// Parse an IEEE 802.15.4 frame and return a high-level representation.
|
||||
pub fn parse<T: AsRef<[u8]> + ?Sized>(packet: &Frame<&T>) -> Result<Repr> {
|
||||
// Ensure the basic accessors will work.
|
||||
packet.check_len()?;
|
||||
|
||||
Ok(Repr {
|
||||
frame_type: packet.frame_type(),
|
||||
security_enabled: packet.security_enabled(),
|
||||
frame_pending: packet.frame_pending(),
|
||||
ack_request: packet.ack_request(),
|
||||
sequence_number: packet.sequence_number(),
|
||||
pan_id_compression: packet.pan_id_compression(),
|
||||
frame_version: packet.frame_version(),
|
||||
dst_pan_id: packet.dst_pan_id(),
|
||||
dst_addr: packet.dst_addr(),
|
||||
src_pan_id: packet.src_pan_id(),
|
||||
src_addr: packet.src_addr(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Return the length of a buffer required to hold a packet with the payload of a given length.
|
||||
#[inline]
|
||||
pub fn buffer_len(&self) -> usize {
|
||||
3 + 2
|
||||
+ match self.dst_addr {
|
||||
Some(Address::Absent) | None => 0,
|
||||
Some(Address::Short(_)) => 2,
|
||||
Some(Address::Extended(_)) => 8,
|
||||
}
|
||||
+ if !self.pan_id_compression { 2 } else { 0 }
|
||||
+ match self.src_addr {
|
||||
Some(Address::Absent) | None => 0,
|
||||
Some(Address::Short(_)) => 2,
|
||||
Some(Address::Extended(_)) => 8,
|
||||
}
|
||||
}
|
||||
|
||||
/// Emit a high-level representation into an IEEE802.15.4 frame.
|
||||
pub fn emit<T: AsRef<[u8]> + AsMut<[u8]>>(&self, frame: &mut Frame<T>) {
|
||||
frame.set_frame_type(self.frame_type);
|
||||
frame.set_security_enabled(self.security_enabled);
|
||||
frame.set_frame_pending(self.frame_pending);
|
||||
frame.set_ack_request(self.ack_request);
|
||||
frame.set_pan_id_compression(self.pan_id_compression);
|
||||
frame.set_frame_version(self.frame_version);
|
||||
|
||||
if let Some(sequence_number) = self.sequence_number {
|
||||
frame.set_sequence_number(sequence_number);
|
||||
}
|
||||
|
||||
if let Some(dst_pan_id) = self.dst_pan_id {
|
||||
frame.set_dst_pan_id(dst_pan_id);
|
||||
}
|
||||
if let Some(dst_addr) = self.dst_addr {
|
||||
frame.set_dst_addr(dst_addr);
|
||||
}
|
||||
|
||||
if !self.pan_id_compression && self.src_pan_id.is_some() {
|
||||
frame.set_src_pan_id(self.src_pan_id.unwrap());
|
||||
}
|
||||
|
||||
if let Some(src_addr) = self.src_addr {
|
||||
frame.set_src_addr(src_addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::Result;
|
||||
|
||||
#[test]
|
||||
fn test_broadcast() {
|
||||
assert!(Address::BROADCAST.is_broadcast());
|
||||
assert!(!Address::BROADCAST.is_unicast());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prepare_frame() {
|
||||
let mut buffer = [0u8; 128];
|
||||
|
||||
let repr = Repr {
|
||||
frame_type: FrameType::Data,
|
||||
security_enabled: false,
|
||||
frame_pending: false,
|
||||
ack_request: true,
|
||||
pan_id_compression: true,
|
||||
frame_version: FrameVersion::Ieee802154,
|
||||
sequence_number: Some(1),
|
||||
dst_pan_id: Some(Pan(0xabcd)),
|
||||
dst_addr: Some(Address::BROADCAST),
|
||||
src_pan_id: None,
|
||||
src_addr: Some(Address::Extended([
|
||||
0xc7, 0xd9, 0xb5, 0x14, 0x00, 0x4b, 0x12, 0x00,
|
||||
])),
|
||||
};
|
||||
|
||||
let buffer_len = repr.buffer_len();
|
||||
|
||||
let mut frame = Frame::new_unchecked(&mut buffer[..buffer_len]);
|
||||
repr.emit(&mut frame);
|
||||
|
||||
println!("{:2x?}", frame);
|
||||
|
||||
assert_eq!(frame.frame_type(), FrameType::Data);
|
||||
assert!(!frame.security_enabled());
|
||||
assert!(!frame.frame_pending());
|
||||
assert!(frame.ack_request());
|
||||
assert!(frame.pan_id_compression());
|
||||
assert_eq!(frame.frame_version(), FrameVersion::Ieee802154);
|
||||
assert_eq!(frame.sequence_number(), Some(1));
|
||||
assert_eq!(frame.dst_pan_id(), Some(Pan(0xabcd)));
|
||||
assert_eq!(frame.dst_addr(), Some(Address::BROADCAST));
|
||||
assert_eq!(frame.src_pan_id(), None);
|
||||
assert_eq!(
|
||||
frame.src_addr(),
|
||||
Some(Address::Extended([
|
||||
0xc7, 0xd9, 0xb5, 0x14, 0x00, 0x4b, 0x12, 0x00
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
macro_rules! vector_test {
|
||||
($name:ident $bytes:expr ; $($test_method:ident -> $expected:expr,)*) => {
|
||||
#[test]
|
||||
#[allow(clippy::bool_assert_comparison)]
|
||||
fn $name() -> Result<()> {
|
||||
let frame = &$bytes;
|
||||
let frame = Frame::new_checked(frame)?;
|
||||
|
||||
$(
|
||||
assert_eq!(frame.$test_method(), $expected, stringify!($test_method));
|
||||
)*
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector_test! {
|
||||
extended_addr
|
||||
[
|
||||
0b0000_0001, 0b1100_1100, // frame control
|
||||
0b0, // seq
|
||||
0xcd, 0xab, // pan id
|
||||
0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, // dst addr
|
||||
0x03, 0x04, // pan id
|
||||
0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, // src addr
|
||||
];
|
||||
frame_type -> FrameType::Data,
|
||||
dst_addr -> Some(Address::Extended([0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00])),
|
||||
src_addr -> Some(Address::Extended([0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00])),
|
||||
dst_pan_id -> Some(Pan(0xabcd)),
|
||||
}
|
||||
|
||||
vector_test! {
|
||||
short_addr
|
||||
[
|
||||
0x01, 0x98, // frame control
|
||||
0x00, // sequence number
|
||||
0x34, 0x12, 0x78, 0x56, // PAN identifier and address of destination
|
||||
0x34, 0x12, 0xbc, 0x9a, // PAN identifier and address of source
|
||||
];
|
||||
frame_type -> FrameType::Data,
|
||||
security_enabled -> false,
|
||||
frame_pending -> false,
|
||||
ack_request -> false,
|
||||
pan_id_compression -> false,
|
||||
dst_addressing_mode -> AddressingMode::Short,
|
||||
frame_version -> FrameVersion::Ieee802154_2006,
|
||||
src_addressing_mode -> AddressingMode::Short,
|
||||
dst_pan_id -> Some(Pan(0x1234)),
|
||||
dst_addr -> Some(Address::Short([0x56, 0x78])),
|
||||
src_pan_id -> Some(Pan(0x1234)),
|
||||
src_addr -> Some(Address::Short([0x9a, 0xbc])),
|
||||
}
|
||||
|
||||
vector_test! {
|
||||
zolertia_remote
|
||||
[
|
||||
0x41, 0xd8, // frame control
|
||||
0x01, // sequence number
|
||||
0xcd, 0xab, // Destination PAN id
|
||||
0xff, 0xff, // Short destination address
|
||||
0xc7, 0xd9, 0xb5, 0x14, 0x00, 0x4b, 0x12, 0x00, // Extended source address
|
||||
0x2b, 0x00, 0x00, 0x00, // payload
|
||||
];
|
||||
frame_type -> FrameType::Data,
|
||||
security_enabled -> false,
|
||||
frame_pending -> false,
|
||||
ack_request -> false,
|
||||
pan_id_compression -> true,
|
||||
dst_addressing_mode -> AddressingMode::Short,
|
||||
frame_version -> FrameVersion::Ieee802154_2006,
|
||||
src_addressing_mode -> AddressingMode::Extended,
|
||||
//payload -> Some(&[0x2b, 0x00, 0x00, 0x00]),
|
||||
}
|
||||
}
|
|
@ -337,23 +337,19 @@ impl<'a> fmt::Display for Repr {
|
|||
max_resp_time,
|
||||
group_addr,
|
||||
version,
|
||||
} => {
|
||||
write!(
|
||||
f,
|
||||
"IGMP membership query max_resp_time={} group_addr={} version={:?}",
|
||||
max_resp_time, group_addr, version
|
||||
)
|
||||
}
|
||||
} => write!(
|
||||
f,
|
||||
"IGMP membership query max_resp_time={} group_addr={} version={:?}",
|
||||
max_resp_time, group_addr, version
|
||||
),
|
||||
Repr::MembershipReport {
|
||||
group_addr,
|
||||
version,
|
||||
} => {
|
||||
write!(
|
||||
f,
|
||||
"IGMP membership report group_addr={} version={:?}",
|
||||
group_addr, version
|
||||
)
|
||||
}
|
||||
} => write!(
|
||||
f,
|
||||
"IGMP membership report group_addr={} version={:?}",
|
||||
group_addr, version
|
||||
),
|
||||
Repr::LeaveGroup { group_addr } => {
|
||||
write!(f, "IGMP leave group group_addr={})", group_addr)
|
||||
}
|
||||
|
|
|
@ -183,12 +183,10 @@ impl Address {
|
|||
/// unicast.
|
||||
pub fn solicited_node(&self) -> Address {
|
||||
assert!(self.is_unicast());
|
||||
let mut bytes = [
|
||||
0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00,
|
||||
];
|
||||
bytes[14..].copy_from_slice(&self.0[14..]);
|
||||
Address(bytes)
|
||||
Address([
|
||||
0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF,
|
||||
self.0[13], self.0[14], self.0[15],
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
213
src/wire/mod.rs
213
src/wire/mod.rs
|
@ -89,6 +89,8 @@ mod icmp;
|
|||
mod icmpv4;
|
||||
#[cfg(feature = "proto-ipv6")]
|
||||
mod icmpv6;
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
pub mod ieee802154;
|
||||
#[cfg(feature = "proto-igmp")]
|
||||
mod igmp;
|
||||
pub(crate) mod ip;
|
||||
|
@ -106,13 +108,23 @@ mod ipv6option;
|
|||
mod ipv6routing;
|
||||
#[cfg(feature = "proto-ipv6")]
|
||||
mod mld;
|
||||
#[cfg(all(feature = "proto-ipv6", feature = "medium-ethernet"))]
|
||||
#[cfg(all(
|
||||
feature = "proto-ipv6",
|
||||
any(feature = "medium-ethernet", feature = "medium-ieee802154")
|
||||
))]
|
||||
mod ndisc;
|
||||
#[cfg(all(feature = "proto-ipv6", feature = "medium-ethernet"))]
|
||||
#[cfg(all(
|
||||
feature = "proto-ipv6",
|
||||
any(feature = "medium-ethernet", feature = "medium-ieee802154")
|
||||
))]
|
||||
mod ndiscoption;
|
||||
#[cfg(all(feature = "proto-sixlowpan", feature = "medium-ieee802154"))]
|
||||
mod sixlowpan;
|
||||
mod tcp;
|
||||
mod udp;
|
||||
|
||||
use crate::{phy::Medium, Error};
|
||||
|
||||
pub use self::pretty_print::PrettyPrinter;
|
||||
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
|
@ -126,6 +138,24 @@ pub use self::arp::{
|
|||
Hardware as ArpHardware, Operation as ArpOperation, Packet as ArpPacket, Repr as ArpRepr,
|
||||
};
|
||||
|
||||
#[cfg(all(feature = "proto-sixlowpan", feature = "medium-ieee802154"))]
|
||||
pub use self::sixlowpan::{
|
||||
iphc::{Packet as SixlowpanIphcPacket, Repr as SixlowpanIphcRepr},
|
||||
nhc::{
|
||||
ExtensionHeaderPacket as SixlowpanExtHeaderPacket,
|
||||
ExtensionHeaderRepr as SixlowpanExtHeaderRepr, Packet as SixlowpanNhcPacket,
|
||||
UdpNhcRepr as SixlowpanUdpRepr, UdpPacket as SixlowpanUdpPacket,
|
||||
},
|
||||
NextHeader as SixlowpanNextHeader,
|
||||
};
|
||||
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
pub use self::ieee802154::{
|
||||
Address as Ieee802154Address, AddressingMode as Ieee802154AddressingMode,
|
||||
Frame as Ieee802154Frame, FrameType as Ieee802154FrameType,
|
||||
FrameVersion as Ieee802154FrameVersion, Pan as Ieee802154Pan, Repr as Ieee802154Repr,
|
||||
};
|
||||
|
||||
pub use self::ip::{
|
||||
Address as IpAddress, Cidr as IpCidr, Endpoint as IpEndpoint, Protocol as IpProtocol,
|
||||
Repr as IpRepr, Version as IpVersion,
|
||||
|
@ -177,12 +207,18 @@ pub use self::icmpv6::{
|
|||
#[cfg(any(feature = "proto-ipv4", feature = "proto-ipv6"))]
|
||||
pub use self::icmp::Repr as IcmpRepr;
|
||||
|
||||
#[cfg(all(feature = "proto-ipv6", feature = "medium-ethernet"))]
|
||||
#[cfg(all(
|
||||
feature = "proto-ipv6",
|
||||
any(feature = "medium-ethernet", feature = "medium-ieee802154")
|
||||
))]
|
||||
pub use self::ndisc::{
|
||||
NeighborFlags as NdiscNeighborFlags, Repr as NdiscRepr, RouterFlags as NdiscRouterFlags,
|
||||
};
|
||||
|
||||
#[cfg(all(feature = "proto-ipv6", feature = "medium-ethernet"))]
|
||||
#[cfg(all(
|
||||
feature = "proto-ipv6",
|
||||
any(feature = "medium-ethernet", feature = "medium-ieee802154")
|
||||
))]
|
||||
pub use self::ndiscoption::{
|
||||
NdiscOption, PrefixInfoFlags as NdiscPrefixInfoFlags,
|
||||
PrefixInformation as NdiscPrefixInformation, RedirectedHeader as NdiscRedirectedHeader,
|
||||
|
@ -205,3 +241,172 @@ pub use self::dhcpv4::{
|
|||
CLIENT_PORT as DHCP_CLIENT_PORT, MAX_DNS_SERVER_COUNT as DHCP_MAX_DNS_SERVER_COUNT,
|
||||
SERVER_PORT as DHCP_SERVER_PORT,
|
||||
};
|
||||
|
||||
/// Representation of an hardware address, such as an Ethernet address or an IEEE802.15.4 address.
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
pub enum HardwareAddress {
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
Ethernet(EthernetAddress),
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
Ieee802154(Ieee802154Address),
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
impl HardwareAddress {
|
||||
pub fn as_bytes(&self) -> &[u8] {
|
||||
match self {
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
HardwareAddress::Ethernet(addr) => addr.as_bytes(),
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
HardwareAddress::Ieee802154(addr) => addr.as_bytes(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Query wether the address is an unicast address.
|
||||
pub fn is_unicast(&self) -> bool {
|
||||
match self {
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
HardwareAddress::Ethernet(addr) => addr.is_unicast(),
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
HardwareAddress::Ieee802154(addr) => addr.is_unicast(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Query wether the address is a broadcast address.
|
||||
pub fn is_broadcast(&self) -> bool {
|
||||
match self {
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
HardwareAddress::Ethernet(addr) => addr.is_broadcast(),
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
HardwareAddress::Ieee802154(addr) => addr.is_broadcast(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
impl core::fmt::Display for HardwareAddress {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
|
||||
match self {
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
HardwareAddress::Ethernet(addr) => write!(f, "{}", addr),
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
HardwareAddress::Ieee802154(addr) => write!(f, "{}", addr),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
impl From<EthernetAddress> for HardwareAddress {
|
||||
fn from(addr: EthernetAddress) -> Self {
|
||||
HardwareAddress::Ethernet(addr)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
impl From<Ieee802154Address> for HardwareAddress {
|
||||
fn from(addr: Ieee802154Address) -> Self {
|
||||
HardwareAddress::Ieee802154(addr)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "medium-ieee802154"))]
|
||||
pub const MAX_HARDWARE_ADDRESS_LEN: usize = 6;
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
pub const MAX_HARDWARE_ADDRESS_LEN: usize = 8;
|
||||
|
||||
/// Unparsed hardware address.
|
||||
///
|
||||
/// Used to make NDISC parsing agnostic of the hardware medium in use.
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
pub struct RawHardwareAddress {
|
||||
len: u8,
|
||||
data: [u8; MAX_HARDWARE_ADDRESS_LEN],
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
impl RawHardwareAddress {
|
||||
pub fn from_bytes(addr: &[u8]) -> Self {
|
||||
let mut data = [0u8; MAX_HARDWARE_ADDRESS_LEN];
|
||||
data[..addr.len()].copy_from_slice(addr);
|
||||
|
||||
Self {
|
||||
len: addr.len() as u8,
|
||||
data,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_bytes(&self) -> &[u8] {
|
||||
&self.data[..self.len as usize]
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.len as usize
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len == 0
|
||||
}
|
||||
|
||||
pub fn parse(&self, medium: Medium) -> Result<HardwareAddress, Error> {
|
||||
match medium {
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
Medium::Ethernet => {
|
||||
if self.len() < 6 {
|
||||
return Err(Error::Malformed);
|
||||
}
|
||||
Ok(HardwareAddress::Ethernet(EthernetAddress::from_bytes(
|
||||
self.as_bytes(),
|
||||
)))
|
||||
}
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
Medium::Ieee802154 => {
|
||||
if self.len() < 8 {
|
||||
return Err(Error::Malformed);
|
||||
}
|
||||
Ok(HardwareAddress::Ieee802154(Ieee802154Address::from_bytes(
|
||||
self.as_bytes(),
|
||||
)))
|
||||
}
|
||||
#[cfg(feature = "medium-ip")]
|
||||
Medium::Ip => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
impl core::fmt::Display for RawHardwareAddress {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
|
||||
for (i, &b) in self.as_bytes().iter().enumerate() {
|
||||
if i != 0 {
|
||||
write!(f, ":")?;
|
||||
}
|
||||
write!(f, "{:02x}", b)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "medium-ethernet")]
|
||||
impl From<EthernetAddress> for RawHardwareAddress {
|
||||
fn from(addr: EthernetAddress) -> Self {
|
||||
Self::from_bytes(addr.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "medium-ieee802154")]
|
||||
impl From<Ieee802154Address> for RawHardwareAddress {
|
||||
fn from(addr: Ieee802154Address) -> Self {
|
||||
Self::from_bytes(addr.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
|
||||
impl From<HardwareAddress> for RawHardwareAddress {
|
||||
fn from(addr: HardwareAddress) -> Self {
|
||||
Self::from_bytes(addr.as_bytes())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,8 @@ use byteorder::{ByteOrder, NetworkEndian};
|
|||
use crate::time::Duration;
|
||||
use crate::wire::icmpv6::{field, Message, Packet};
|
||||
use crate::wire::Ipv6Address;
|
||||
use crate::wire::{EthernetAddress, Ipv6Packet, Ipv6Repr};
|
||||
use crate::wire::RawHardwareAddress;
|
||||
use crate::wire::{Ipv6Packet, Ipv6Repr};
|
||||
use crate::wire::{NdiscOption, NdiscOptionRepr, NdiscOptionType};
|
||||
use crate::wire::{NdiscPrefixInformation, NdiscRedirectedHeader};
|
||||
use crate::{Error, Result};
|
||||
|
@ -193,7 +194,7 @@ impl<T: AsRef<[u8]> + AsMut<[u8]>> Packet<T> {
|
|||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
pub enum Repr<'a> {
|
||||
RouterSolicit {
|
||||
lladdr: Option<EthernetAddress>,
|
||||
lladdr: Option<RawHardwareAddress>,
|
||||
},
|
||||
RouterAdvert {
|
||||
hop_limit: u8,
|
||||
|
@ -201,23 +202,23 @@ pub enum Repr<'a> {
|
|||
router_lifetime: Duration,
|
||||
reachable_time: Duration,
|
||||
retrans_time: Duration,
|
||||
lladdr: Option<EthernetAddress>,
|
||||
lladdr: Option<RawHardwareAddress>,
|
||||
mtu: Option<u32>,
|
||||
prefix_info: Option<NdiscPrefixInformation>,
|
||||
},
|
||||
NeighborSolicit {
|
||||
target_addr: Ipv6Address,
|
||||
lladdr: Option<EthernetAddress>,
|
||||
lladdr: Option<RawHardwareAddress>,
|
||||
},
|
||||
NeighborAdvert {
|
||||
flags: NeighborFlags,
|
||||
target_addr: Ipv6Address,
|
||||
lladdr: Option<EthernetAddress>,
|
||||
lladdr: Option<RawHardwareAddress>,
|
||||
},
|
||||
Redirect {
|
||||
target_addr: Ipv6Address,
|
||||
dest_addr: Ipv6Address,
|
||||
lladdr: Option<EthernetAddress>,
|
||||
lladdr: Option<RawHardwareAddress>,
|
||||
redirected_hdr: Option<NdiscRedirectedHeader<'a>>,
|
||||
},
|
||||
}
|
||||
|
@ -372,10 +373,11 @@ impl<'a> Repr<'a> {
|
|||
field::RETRANS_TM.end + offset
|
||||
}
|
||||
&Repr::NeighborSolicit { lladdr, .. } | &Repr::NeighborAdvert { lladdr, .. } => {
|
||||
match lladdr {
|
||||
Some(_) => field::TARGET_ADDR.end + 8,
|
||||
None => field::TARGET_ADDR.end,
|
||||
let mut offset = field::TARGET_ADDR.end;
|
||||
if let Some(lladdr) = lladdr {
|
||||
offset += NdiscOptionRepr::SourceLinkLayerAddr(lladdr).buffer_len();
|
||||
}
|
||||
offset
|
||||
}
|
||||
&Repr::Redirect {
|
||||
lladdr,
|
||||
|
@ -429,8 +431,9 @@ impl<'a> Repr<'a> {
|
|||
let mut offset = 0;
|
||||
if let Some(lladdr) = lladdr {
|
||||
let mut opt_pkt = NdiscOption::new_unchecked(packet.payload_mut());
|
||||
NdiscOptionRepr::SourceLinkLayerAddr(lladdr).emit(&mut opt_pkt);
|
||||
offset += 8;
|
||||
let opt = NdiscOptionRepr::SourceLinkLayerAddr(lladdr);
|
||||
opt.emit(&mut opt_pkt);
|
||||
offset += opt.buffer_len();
|
||||
}
|
||||
if let Some(mtu) = mtu {
|
||||
let mut opt_pkt =
|
||||
|
@ -509,6 +512,7 @@ mod test {
|
|||
use super::*;
|
||||
use crate::phy::ChecksumCapabilities;
|
||||
use crate::wire::ip::test::{MOCK_IP_ADDR_1, MOCK_IP_ADDR_2};
|
||||
use crate::wire::EthernetAddress;
|
||||
use crate::wire::Icmpv6Repr;
|
||||
|
||||
static ROUTER_ADVERT_BYTES: [u8; 24] = [
|
||||
|
@ -524,7 +528,7 @@ mod test {
|
|||
router_lifetime: Duration::from_secs(900),
|
||||
reachable_time: Duration::from_millis(900),
|
||||
retrans_time: Duration::from_millis(900),
|
||||
lladdr: Some(EthernetAddress([0x52, 0x54, 0x00, 0x12, 0x34, 0x56])),
|
||||
lladdr: Some(EthernetAddress([0x52, 0x54, 0x00, 0x12, 0x34, 0x56]).into()),
|
||||
mtu: None,
|
||||
prefix_info: None,
|
||||
})
|
||||
|
|
|
@ -3,9 +3,11 @@ use byteorder::{ByteOrder, NetworkEndian};
|
|||
use core::fmt;
|
||||
|
||||
use crate::time::Duration;
|
||||
use crate::wire::{EthernetAddress, Ipv6Address, Ipv6Packet, Ipv6Repr};
|
||||
use crate::wire::{Ipv6Address, Ipv6Packet, Ipv6Repr, MAX_HARDWARE_ADDRESS_LEN};
|
||||
use crate::{Error, Result};
|
||||
|
||||
use crate::wire::RawHardwareAddress;
|
||||
|
||||
enum_with_unknown! {
|
||||
/// NDISC Option Type
|
||||
pub enum Type(u8) {
|
||||
|
@ -82,9 +84,6 @@ mod field {
|
|||
// | Type | Length | Link-Layer Address ...
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
|
||||
// Link-Layer Address
|
||||
pub const LL_ADDR: Field = 2..8;
|
||||
|
||||
// Prefix Information Option fields.
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// | Type | Length | Prefix Length |L|A| Reserved1 |
|
||||
|
@ -214,9 +213,10 @@ impl<T: AsRef<[u8]>> NdiscOption<T> {
|
|||
impl<T: AsRef<[u8]>> NdiscOption<T> {
|
||||
/// Return the Source/Target Link-layer Address.
|
||||
#[inline]
|
||||
pub fn link_layer_addr(&self) -> EthernetAddress {
|
||||
pub fn link_layer_addr(&self) -> RawHardwareAddress {
|
||||
let len = MAX_HARDWARE_ADDRESS_LEN.min(self.data_len() as usize * 8 - 2);
|
||||
let data = self.buffer.as_ref();
|
||||
EthernetAddress::from_bytes(&data[field::LL_ADDR])
|
||||
RawHardwareAddress::from_bytes(&data[2..len + 2])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -297,9 +297,9 @@ impl<T: AsRef<[u8]> + AsMut<[u8]>> NdiscOption<T> {
|
|||
impl<T: AsRef<[u8]> + AsMut<[u8]>> NdiscOption<T> {
|
||||
/// Set the Source/Target Link-layer Address.
|
||||
#[inline]
|
||||
pub fn set_link_layer_addr(&mut self, addr: EthernetAddress) {
|
||||
pub fn set_link_layer_addr(&mut self, addr: RawHardwareAddress) {
|
||||
let data = self.buffer.as_mut();
|
||||
data[field::LL_ADDR].copy_from_slice(addr.as_bytes())
|
||||
data[2..2 + addr.len()].copy_from_slice(addr.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -409,8 +409,8 @@ pub struct RedirectedHeader<'a> {
|
|||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
pub enum Repr<'a> {
|
||||
SourceLinkLayerAddr(EthernetAddress),
|
||||
TargetLinkLayerAddr(EthernetAddress),
|
||||
SourceLinkLayerAddr(RawHardwareAddress),
|
||||
TargetLinkLayerAddr(RawHardwareAddress),
|
||||
PrefixInformation(PrefixInformation),
|
||||
RedirectedHeader(RedirectedHeader<'a>),
|
||||
Mtu(u32),
|
||||
|
@ -488,7 +488,11 @@ impl<'a> Repr<'a> {
|
|||
/// Return the length of a header that will be emitted from this high-level representation.
|
||||
pub fn buffer_len(&self) -> usize {
|
||||
match self {
|
||||
&Repr::SourceLinkLayerAddr(_) | &Repr::TargetLinkLayerAddr(_) => field::LL_ADDR.end,
|
||||
&Repr::SourceLinkLayerAddr(addr) | &Repr::TargetLinkLayerAddr(addr) => {
|
||||
let len = 2 + addr.len();
|
||||
// Round up to next multiple of 8
|
||||
(len + 7) / 8 * 8
|
||||
}
|
||||
&Repr::PrefixInformation(_) => field::PREFIX.end,
|
||||
&Repr::RedirectedHeader(RedirectedHeader { header, data }) => {
|
||||
field::IP_DATA + header.buffer_len() + data.len()
|
||||
|
@ -506,12 +510,14 @@ impl<'a> Repr<'a> {
|
|||
match *self {
|
||||
Repr::SourceLinkLayerAddr(addr) => {
|
||||
opt.set_option_type(Type::SourceLinkLayerAddr);
|
||||
opt.set_data_len(1);
|
||||
let opt_len = addr.len() + 2;
|
||||
opt.set_data_len(((opt_len + 7) / 8) as u8); // round to next multiple of 8.
|
||||
opt.set_link_layer_addr(addr);
|
||||
}
|
||||
Repr::TargetLinkLayerAddr(addr) => {
|
||||
opt.set_option_type(Type::TargetLinkLayerAddr);
|
||||
opt.set_data_len(1);
|
||||
let opt_len = addr.len() + 2;
|
||||
opt.set_data_len(((opt_len + 7) / 8) as u8); // round to next multiple of 8.
|
||||
opt.set_link_layer_addr(addr);
|
||||
}
|
||||
Repr::PrefixInformation(PrefixInformation {
|
||||
|
@ -668,14 +674,14 @@ mod test {
|
|||
{
|
||||
assert_eq!(
|
||||
Repr::parse(&NdiscOption::new_unchecked(&bytes)),
|
||||
Ok(Repr::SourceLinkLayerAddr(addr))
|
||||
Ok(Repr::SourceLinkLayerAddr(addr.into()))
|
||||
);
|
||||
}
|
||||
bytes[0] = 0x02;
|
||||
{
|
||||
assert_eq!(
|
||||
Repr::parse(&NdiscOption::new_unchecked(&bytes)),
|
||||
Ok(Repr::TargetLinkLayerAddr(addr))
|
||||
Ok(Repr::TargetLinkLayerAddr(addr.into()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue