2016-12-12 15:19:53 +08:00
|
|
|
use Error;
|
2016-12-12 10:39:46 +08:00
|
|
|
use phy::Device;
|
2016-12-12 15:19:53 +08:00
|
|
|
use wire::{EthernetAddress, EthernetProtocolType, EthernetFrame};
|
|
|
|
use wire::{ArpPacket, ArpRepr, ArpOperation};
|
2016-12-12 10:39:46 +08:00
|
|
|
use super::{ProtocolAddress, ArpCache};
|
|
|
|
|
|
|
|
/// An Ethernet network interface.
|
|
|
|
#[derive(Debug)]
|
2016-12-12 15:19:53 +08:00
|
|
|
pub struct Interface<'a, DeviceT: Device, ArpCacheT: ArpCache> {
|
|
|
|
device: DeviceT,
|
|
|
|
arp_cache: ArpCacheT,
|
|
|
|
hardware_addr: EthernetAddress,
|
|
|
|
protocol_addrs: &'a [ProtocolAddress]
|
2016-12-12 10:39:46 +08:00
|
|
|
}
|
|
|
|
|
2016-12-12 15:19:53 +08:00
|
|
|
impl<'a, DeviceT: Device, ArpCacheT: ArpCache> Interface<'a, DeviceT, ArpCacheT> {
|
2016-12-12 10:39:46 +08:00
|
|
|
/// Create a network interface using the provided network device.
|
|
|
|
///
|
|
|
|
/// The newly created interface uses hardware address `00-00-00-00-00-00` and
|
|
|
|
/// has no assigned protocol addresses.
|
2016-12-12 15:19:53 +08:00
|
|
|
pub fn new(device: DeviceT, arp_cache: ArpCacheT) -> Interface<'a, DeviceT, ArpCacheT> {
|
2016-12-12 10:39:46 +08:00
|
|
|
Interface {
|
2016-12-12 15:19:53 +08:00
|
|
|
device: device,
|
|
|
|
arp_cache: arp_cache,
|
|
|
|
hardware_addr: EthernetAddress([0x00; 6]),
|
|
|
|
protocol_addrs: &[]
|
2016-12-12 10:39:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the hardware address of the interface.
|
|
|
|
pub fn hardware_addr(&self) -> EthernetAddress {
|
|
|
|
self.hardware_addr
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the hardware address of the interface.
|
|
|
|
///
|
|
|
|
/// # Panics
|
2016-12-12 15:19:53 +08:00
|
|
|
/// This function panics if the address is not unicast.
|
2016-12-12 10:39:46 +08:00
|
|
|
pub fn set_hardware_addr(&mut self, addr: EthernetAddress) {
|
2016-12-12 15:19:53 +08:00
|
|
|
if addr.is_multicast() {
|
|
|
|
panic!("hardware address {} is not unicast", addr)
|
|
|
|
}
|
|
|
|
|
2016-12-12 10:39:46 +08:00
|
|
|
self.hardware_addr = addr
|
|
|
|
}
|
2016-12-12 15:19:53 +08:00
|
|
|
|
|
|
|
/// Get the protocol addresses of the interface.
|
|
|
|
pub fn protocol_addrs(&self) -> &'a [ProtocolAddress] {
|
|
|
|
self.protocol_addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the protocol addresses of the interface.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
/// This function panics if any of the addresses is not unicast.
|
|
|
|
pub fn set_protocol_addrs(&mut self, addrs: &'a [ProtocolAddress]) {
|
|
|
|
for addr in addrs {
|
|
|
|
if !addr.is_unicast() {
|
|
|
|
panic!("protocol address {} is not unicast", addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.protocol_addrs = addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks whether the interface has the given protocol address assigned.
|
|
|
|
pub fn has_protocol_addr<T: Into<ProtocolAddress>>(&self, addr: T) -> bool {
|
|
|
|
let addr = addr.into();
|
|
|
|
self.protocol_addrs.iter().any(|&probe| probe == addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive and process a packet, if available.
|
|
|
|
pub fn poll(&mut self) -> Result<(), Error> {
|
|
|
|
enum Response {
|
|
|
|
Nop,
|
|
|
|
Arp(ArpRepr)
|
|
|
|
}
|
2016-12-12 20:30:35 +08:00
|
|
|
let mut response = Response::Nop;
|
2016-12-12 15:19:53 +08:00
|
|
|
|
2016-12-12 20:30:35 +08:00
|
|
|
let rx_buffer = try!(self.device.receive());
|
|
|
|
let frame = try!(EthernetFrame::new(rx_buffer));
|
|
|
|
match frame.ethertype() {
|
|
|
|
EthernetProtocolType::Arp => {
|
|
|
|
let packet = try!(ArpPacket::new(frame.payload()));
|
|
|
|
let repr = try!(ArpRepr::parse(&packet));
|
|
|
|
match repr {
|
2016-12-13 06:41:34 +08:00
|
|
|
// Respond to ARP requests aimed at us, and fill the ARP cache
|
|
|
|
// from all ARP requests, including gratuitous.
|
2016-12-12 20:30:35 +08:00
|
|
|
ArpRepr::EthernetIpv4 {
|
|
|
|
operation: ArpOperation::Request,
|
|
|
|
source_hardware_addr, source_protocol_addr,
|
|
|
|
target_protocol_addr, ..
|
|
|
|
} => {
|
2016-12-13 06:41:34 +08:00
|
|
|
self.arp_cache.fill(source_protocol_addr.into(), source_hardware_addr);
|
|
|
|
|
2016-12-12 20:30:35 +08:00
|
|
|
if self.has_protocol_addr(target_protocol_addr) {
|
|
|
|
response = Response::Arp(ArpRepr::EthernetIpv4 {
|
|
|
|
operation: ArpOperation::Reply,
|
|
|
|
source_hardware_addr: self.hardware_addr,
|
|
|
|
source_protocol_addr: target_protocol_addr,
|
|
|
|
target_hardware_addr: source_hardware_addr,
|
|
|
|
target_protocol_addr: source_protocol_addr
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
2016-12-13 06:41:34 +08:00
|
|
|
|
|
|
|
// Fill the ARP cache from gratuitous ARP replies.
|
|
|
|
ArpRepr::EthernetIpv4 {
|
|
|
|
operation: ArpOperation::Reply,
|
|
|
|
source_hardware_addr, source_protocol_addr, ..
|
|
|
|
} => {
|
|
|
|
self.arp_cache.fill(source_protocol_addr.into(), source_hardware_addr)
|
|
|
|
},
|
|
|
|
|
2016-12-12 20:30:35 +08:00
|
|
|
_ => return Err(Error::Unrecognized)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => return Err(Error::Unrecognized)
|
|
|
|
}
|
2016-12-12 15:19:53 +08:00
|
|
|
|
|
|
|
match response {
|
|
|
|
Response::Nop => Ok(()),
|
2016-12-13 06:41:34 +08:00
|
|
|
|
2016-12-12 15:19:53 +08:00
|
|
|
Response::Arp(repr) => {
|
2016-12-12 20:30:35 +08:00
|
|
|
let tx_size = self.device.mtu();
|
|
|
|
let tx_buffer = try!(self.device.transmit(tx_size));
|
|
|
|
let mut frame = try!(EthernetFrame::new(tx_buffer));
|
2016-12-13 01:26:06 +08:00
|
|
|
frame.set_src_addr(self.hardware_addr);
|
|
|
|
frame.set_dst_addr(match repr {
|
2016-12-12 20:30:35 +08:00
|
|
|
ArpRepr::EthernetIpv4 { target_hardware_addr, .. } => target_hardware_addr,
|
|
|
|
_ => unreachable!()
|
|
|
|
});
|
|
|
|
frame.set_ethertype(EthernetProtocolType::Arp);
|
2016-12-12 15:19:53 +08:00
|
|
|
|
2016-12-12 20:30:35 +08:00
|
|
|
let mut packet = try!(ArpPacket::new(frame.payload_mut()));
|
|
|
|
repr.emit(&mut packet);
|
2016-12-12 15:19:53 +08:00
|
|
|
|
2016-12-12 20:30:35 +08:00
|
|
|
Ok(())
|
2016-12-12 15:19:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-12 10:39:46 +08:00
|
|
|
}
|