renet/src/iface/ethernet.rs

314 lines
13 KiB
Rust
Raw Normal View History

use core::borrow::BorrowMut;
use core::marker::PhantomData;
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};
use wire::{InternetAddress, InternetProtocolType};
2016-12-13 07:22:59 +08:00
use wire::{Ipv4Packet, Ipv4Repr};
use wire::{Icmpv4Packet, Icmpv4Repr};
use socket::Socket;
use super::{ArpCache};
2016-12-12 10:39:46 +08:00
/// An Ethernet network interface.
///
/// The network interface logically owns a number of other data structures; to avoid
/// a dependency on heap allocation, it instead owns a `BorrowMut<[T]>`, which can be
/// a `&mut [T]`, or `Vec<T>` if a heap is available.
2016-12-12 10:39:46 +08:00
#[derive(Debug)]
2016-12-17 14:27:08 +08:00
pub struct Interface<'a, 'b: 'a,
DeviceT: Device,
ArpCacheT: ArpCache,
ProtocolAddrsT: BorrowMut<[InternetAddress]>,
2016-12-17 14:27:08 +08:00
SocketsT: BorrowMut<[Socket<'a, 'b>]>
> {
2016-12-12 15:19:53 +08:00
device: DeviceT,
arp_cache: ArpCacheT,
hardware_addr: EthernetAddress,
protocol_addrs: ProtocolAddrsT,
sockets: SocketsT,
2016-12-17 14:27:08 +08:00
phantom: PhantomData<Socket<'a, 'b>>
2016-12-12 10:39:46 +08:00
}
2016-12-17 14:27:08 +08:00
impl<'a, 'b: 'a,
DeviceT: Device,
ArpCacheT: ArpCache,
ProtocolAddrsT: BorrowMut<[InternetAddress]>,
2016-12-17 14:27:08 +08:00
SocketsT: BorrowMut<[Socket<'a, 'b>]>
> Interface<'a, 'b, DeviceT, ArpCacheT, ProtocolAddrsT, SocketsT> {
2016-12-12 10:39:46 +08:00
/// Create a network interface using the provided network device.
///
/// # Panics
/// See the restrictions on [set_hardware_addr](#method.set_hardware_addr)
/// and [set_protocol_addrs](#method.set_protocol_addrs) functions.
pub fn new(device: DeviceT, arp_cache: ArpCacheT, hardware_addr: EthernetAddress,
protocol_addrs: ProtocolAddrsT, sockets: SocketsT) ->
2016-12-17 14:27:08 +08:00
Interface<'a, 'b, DeviceT, ArpCacheT, ProtocolAddrsT, SocketsT> {
Self::check_hardware_addr(&hardware_addr);
Self::check_protocol_addrs(protocol_addrs.borrow());
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: hardware_addr,
protocol_addrs: protocol_addrs,
sockets: sockets,
phantom: PhantomData
}
}
fn check_hardware_addr(addr: &EthernetAddress) {
if addr.is_multicast() {
panic!("hardware address {} is not unicast", addr)
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) {
self.hardware_addr = addr;
Self::check_hardware_addr(&self.hardware_addr);
}
2016-12-12 15:19:53 +08:00
fn check_protocol_addrs(addrs: &[InternetAddress]) {
for addr in addrs {
if !addr.is_unicast() {
panic!("protocol address {} is not unicast", addr)
}
}
2016-12-12 10:39:46 +08:00
}
2016-12-12 15:19:53 +08:00
/// Get the protocol addresses of the interface.
pub fn protocol_addrs(&self) -> &[InternetAddress] {
self.protocol_addrs.borrow()
2016-12-12 15:19:53 +08:00
}
/// Update the protocol addresses of the interface.
2016-12-12 15:19:53 +08:00
///
/// # Panics
/// This function panics if any of the addresses is not unicast.
pub fn update_protocol_addrs<F: FnOnce(&mut [InternetAddress])>(&mut self, f: F) {
f(self.protocol_addrs.borrow_mut());
Self::check_protocol_addrs(self.protocol_addrs.borrow())
2016-12-12 15:19:53 +08:00
}
/// Check whether the interface has the given protocol address assigned.
pub fn has_protocol_addr<T: Into<InternetAddress>>(&self, addr: T) -> bool {
2016-12-12 15:19:53 +08:00
let addr = addr.into();
self.protocol_addrs.borrow().iter().any(|&probe| probe == addr)
2016-12-12 15:19:53 +08:00
}
/// Get the set of sockets owned by the interface.
2016-12-17 14:27:08 +08:00
pub fn sockets(&mut self) -> &mut [Socket<'a, 'b>] {
2016-12-17 13:12:45 +08:00
self.sockets.borrow_mut()
}
2016-12-12 15:19:53 +08:00
/// Receive and process a packet, if available.
pub fn poll(&mut self) -> Result<(), Error> {
2016-12-13 07:22:59 +08:00
enum Response<'a> {
2016-12-12 15:19:53 +08:00
Nop,
2016-12-13 07:22:59 +08:00
Arp(ArpRepr),
Icmpv4(Ipv4Repr, Icmpv4Repr<'a>)
2016-12-12 15:19:53 +08:00
}
2016-12-12 20:30:35 +08:00
let rx_buffer = try!(self.device.receive());
let eth_frame = try!(EthernetFrame::new(&rx_buffer));
let mut response = Response::Nop;
2016-12-13 07:22:59 +08:00
match eth_frame.ethertype() {
// Snoop all ARP traffic, and respond to ARP packets directed at us.
2016-12-12 20:30:35 +08:00
EthernetProtocolType::Arp => {
2016-12-13 07:22:59 +08:00
let arp_packet = try!(ArpPacket::new(eth_frame.payload()));
match try!(ArpRepr::parse(&arp_packet)) {
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)
}
},
2016-12-13 07:22:59 +08:00
2016-12-15 01:39:44 +08:00
// Handle IP packets directed at us.
2016-12-13 07:22:59 +08:00
EthernetProtocolType::Ipv4 => {
let ip_packet = try!(Ipv4Packet::new(eth_frame.payload()));
match try!(Ipv4Repr::parse(&ip_packet)) {
// Ignore IP packets not directed at us.
Ipv4Repr { dst_addr, .. } if !self.has_protocol_addr(dst_addr) => (),
// Respond to ICMP packets.
Ipv4Repr { protocol: InternetProtocolType::Icmp, src_addr, dst_addr } => {
let icmp_packet = try!(Icmpv4Packet::new(ip_packet.payload()));
let icmp_repr = try!(Icmpv4Repr::parse(&icmp_packet));
match icmp_repr {
// Respond to echo requests.
Icmpv4Repr::EchoRequest {
ident, seq_no, data
} => {
let ip_reply_repr = Ipv4Repr {
src_addr: dst_addr,
dst_addr: src_addr,
protocol: InternetProtocolType::Icmp
};
let icmp_reply_repr = Icmpv4Repr::EchoReply {
ident: ident,
seq_no: seq_no,
data: data
2016-12-13 07:22:59 +08:00
};
response = Response::Icmpv4(ip_reply_repr, icmp_reply_repr)
}
// Ignore any echo replies.
Icmpv4Repr::EchoReply { .. } => (),
// FIXME: do something correct here?
_ => return Err(Error::Unrecognized)
}
},
// Try dispatching a packet to a socket.
Ipv4Repr { src_addr, dst_addr, protocol } => {
for socket in self.sockets.borrow_mut() {
match socket.collect(&src_addr.into(), &dst_addr.into(),
protocol, ip_packet.payload()) {
Ok(()) => break,
Err(Error::Rejected) => continue,
Err(e) => return Err(e)
}
}
2016-12-15 01:39:44 +08:00
// FIXME: respond with ICMP destination unreachable here?
},
2016-12-13 07:22:59 +08:00
}
}
// Drop all other traffic.
2016-12-12 20:30:35 +08:00
_ => return Err(Error::Unrecognized)
}
2016-12-12 15:19:53 +08:00
2016-12-13 07:22:59 +08:00
match response {
2016-12-12 15:19:53 +08:00
Response::Arp(repr) => {
2016-12-20 07:50:04 +08:00
let tx_len = EthernetFrame::<&[u8]>::buffer_len(repr.buffer_len());
let mut tx_buffer = try!(self.device.transmit(tx_len));
let mut frame = try!(EthernetFrame::new(&mut tx_buffer));
frame.set_src_addr(self.hardware_addr);
2016-12-13 01:26:06 +08:00
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()));
2016-12-13 07:22:59 +08:00
repr.emit(&mut packet)
},
Response::Icmpv4(ip_repr, icmp_repr) => {
let dst_hardware_addr =
match self.arp_cache.lookup(ip_repr.dst_addr.into()) {
None => return Err(Error::Unaddressable),
Some(hardware_addr) => hardware_addr
};
2016-12-20 07:50:04 +08:00
let tx_len = EthernetFrame::<&[u8]>::buffer_len(ip_repr.buffer_len() +
icmp_repr.buffer_len());
let mut tx_buffer = try!(self.device.transmit(tx_len));
let mut frame = try!(EthernetFrame::new(&mut tx_buffer));
frame.set_src_addr(self.hardware_addr);
frame.set_dst_addr(dst_hardware_addr);
2016-12-13 07:22:59 +08:00
frame.set_ethertype(EthernetProtocolType::Ipv4);
let mut ip_packet = try!(Ipv4Packet::new(frame.payload_mut()));
2016-12-20 07:50:04 +08:00
ip_repr.emit(&mut ip_packet, icmp_repr.buffer_len());
2016-12-12 15:19:53 +08:00
2016-12-13 07:22:59 +08:00
let mut icmp_packet = try!(Icmpv4Packet::new(ip_packet.payload_mut()));
icmp_repr.emit(&mut icmp_packet);
2016-12-12 15:19:53 +08:00
}
2016-12-13 07:22:59 +08:00
Response::Nop => {
// Borrow checker is being overly careful around closures, so we have
// to hack around that.
let src_hardware_addr = self.hardware_addr;
let arp_cache = &mut self.arp_cache;
let device = &mut self.device;
for socket in self.sockets.borrow_mut() {
let result = socket.dispatch(&mut |src_addr, dst_addr, protocol, payload| {
let ip_repr =
match (src_addr, dst_addr) {
(&InternetAddress::Ipv4(src_addr),
&InternetAddress::Ipv4(dst_addr)) => {
Ipv4Repr {
src_addr: src_addr,
dst_addr: dst_addr,
protocol: protocol
}
},
_ => unreachable!()
};
2016-12-20 07:50:04 +08:00
let dst_hardware_addr =
match arp_cache.lookup(*dst_addr) {
None => return Err(Error::Unaddressable),
Some(hardware_addr) => hardware_addr
};
let tx_len = EthernetFrame::<&[u8]>::buffer_len(ip_repr.buffer_len() +
payload.buffer_len());
let mut tx_buffer = try!(device.transmit(tx_len));
let mut frame = try!(EthernetFrame::new(&mut tx_buffer));
frame.set_src_addr(src_hardware_addr);
frame.set_dst_addr(dst_hardware_addr);
frame.set_ethertype(EthernetProtocolType::Ipv4);
let mut ip_packet = try!(Ipv4Packet::new(frame.payload_mut()));
ip_repr.emit(&mut ip_packet, payload.buffer_len());
payload.emit(src_addr, dst_addr, ip_packet.payload_mut());
Ok(())
});
match result {
Ok(()) => break,
Err(Error::Exhausted) => continue,
Err(e) => return Err(e)
}
}
}
2016-12-12 15:19:53 +08:00
}
2016-12-13 07:22:59 +08:00
Ok(())
2016-12-12 15:19:53 +08:00
}
2016-12-12 10:39:46 +08:00
}