renet/src/iface/ethernet.rs

426 lines
18 KiB
Rust
Raw Normal View History

use managed::{Managed, ManagedSlice};
2016-12-12 15:19:53 +08:00
use Error;
2016-12-12 10:39:46 +08:00
use phy::Device;
use wire::{EthernetAddress, EthernetProtocol, EthernetFrame};
2016-12-12 15:19:53 +08:00
use wire::{ArpPacket, ArpRepr, ArpOperation};
use wire::{Ipv4Packet, Ipv4Repr};
use wire::{Icmpv4Packet, Icmpv4Repr, Icmpv4DstUnreachable};
use wire::{IpAddress, IpProtocol, IpRepr};
use wire::{TcpPacket, TcpRepr, TcpControl};
2017-01-11 13:25:54 +08:00
use socket::SocketSet;
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.
2017-01-11 13:25:54 +08:00
pub struct Interface<'a, 'b, 'c, DeviceT: Device + 'a> {
device: Managed<'a, DeviceT>,
2017-01-11 13:25:54 +08:00
arp_cache: Managed<'b, ArpCache>,
2016-12-12 15:19:53 +08:00
hardware_addr: EthernetAddress,
2017-01-11 13:25:54 +08:00
protocol_addrs: ManagedSlice<'c, IpAddress>,
2016-12-12 10:39:46 +08:00
}
2017-01-11 13:25:54 +08:00
impl<'a, 'b, 'c, DeviceT: Device + 'a> Interface<'a, 'b, 'c, DeviceT> {
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.
2017-01-11 13:25:54 +08:00
pub fn new<DeviceMT, ArpCacheMT, ProtocolAddrsMT>
(device: DeviceMT, arp_cache: ArpCacheMT,
hardware_addr: EthernetAddress, protocol_addrs: ProtocolAddrsMT) ->
Interface<'a, 'b, 'c, DeviceT>
where DeviceMT: Into<Managed<'a, DeviceT>>,
2017-01-11 13:25:54 +08:00
ArpCacheMT: Into<Managed<'b, ArpCache>>,
ProtocolAddrsMT: Into<ManagedSlice<'c, IpAddress>>, {
let device = device.into();
let arp_cache = arp_cache.into();
2017-01-11 13:25:54 +08:00
let protocol_addrs = protocol_addrs.into();
Self::check_hardware_addr(&hardware_addr);
Self::check_protocol_addrs(&protocol_addrs);
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,
}
}
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: &[IpAddress]) {
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) -> &[IpAddress] {
self.protocol_addrs.as_ref()
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.
2017-01-11 13:25:54 +08:00
pub fn update_protocol_addrs<F: FnOnce(&mut ManagedSlice<'c, IpAddress>)>(&mut self, f: F) {
f(&mut self.protocol_addrs);
Self::check_protocol_addrs(&self.protocol_addrs)
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<IpAddress>>(&self, addr: T) -> bool {
2016-12-12 15:19:53 +08:00
let addr = addr.into();
self.protocol_addrs.iter().any(|&probe| probe == addr)
2016-12-12 15:19:53 +08:00
}
2017-01-11 13:25:54 +08:00
/// Receive and process a packet, if available, and then transmit a packet, if necessary,
/// handling the given set of sockets.
2016-12-31 16:35:07 +08:00
///
/// The timestamp is a monotonically increasing number of milliseconds.
2017-01-11 13:25:54 +08:00
pub fn poll(&mut self, sockets: &mut SocketSet, timestamp: u64) -> 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>),
Tcpv4(Ipv4Repr, TcpRepr<'a>)
2016-12-12 15:19:53 +08:00
}
// First, transmit any outgoing packets.
loop {
2017-01-11 13:25:54 +08:00
if try!(self.emit(sockets, timestamp)) { break }
}
// Now, receive any incoming packets.
2016-12-12 20:30:35 +08:00
let rx_buffer = try!(self.device.receive());
let eth_frame = try!(EthernetFrame::new(&rx_buffer));
if eth_frame.dst_addr() != self.hardware_addr { return Ok(()) }
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.
EthernetProtocol::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, ..
} => {
self.arp_cache.fill(&source_protocol_addr.into(), &source_hardware_addr);
2016-12-13 06:41:34 +08:00
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-13 06:41:34 +08:00
},
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.
EthernetProtocol::Ipv4 => {
let ipv4_packet = try!(Ipv4Packet::new(eth_frame.payload()));
let ipv4_repr = try!(Ipv4Repr::parse(&ipv4_packet));
// Fill the ARP cache from IP header.
self.arp_cache.fill(&IpAddress::Ipv4(ipv4_repr.src_addr), &eth_frame.src_addr());
match ipv4_repr {
2016-12-13 07:22:59 +08:00
// Ignore IP packets not directed at us.
Ipv4Repr { dst_addr, .. } if !self.has_protocol_addr(dst_addr) => (),
// Respond to ICMP packets.
Ipv4Repr { protocol: IpProtocol::Icmp, src_addr, dst_addr, .. } => {
let icmp_packet = try!(Icmpv4Packet::new(ipv4_packet.payload()));
2016-12-13 07:22:59 +08:00
let icmp_repr = try!(Icmpv4Repr::parse(&icmp_packet));
match icmp_repr {
// Respond to echo requests.
Icmpv4Repr::EchoRequest {
ident, seq_no, data
} => {
let icmp_reply_repr = Icmpv4Repr::EchoReply {
ident: ident,
seq_no: seq_no,
data: data
2016-12-13 07:22:59 +08:00
};
let ipv4_reply_repr = Ipv4Repr {
src_addr: dst_addr,
dst_addr: src_addr,
protocol: IpProtocol::Icmp,
payload_len: icmp_reply_repr.buffer_len()
};
response = Response::Icmpv4(ipv4_reply_repr, icmp_reply_repr)
2016-12-13 07:22:59 +08:00
}
// 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, .. } => {
let mut handled = false;
2017-01-11 13:25:54 +08:00
for socket in sockets.iter_mut() {
let ip_repr = IpRepr::Ipv4(ipv4_repr);
2016-12-31 16:35:07 +08:00
match socket.process(timestamp, &ip_repr, ipv4_packet.payload()) {
2016-12-25 17:22:49 +08:00
Ok(()) => {
// The packet was valid and handled by socket.
handled = true;
break
}
Err(Error::Rejected) => {
// The packet wasn't addressed to the socket.
// For TCP, send RST only if no other socket accepts
// the packet.
continue
}
Err(Error::Malformed) => {
// The packet was addressed to the socket but is malformed.
// For TCP, send RST immediately.
break
}
Err(e) => return Err(e)
}
}
2016-12-15 01:39:44 +08:00
if !handled && protocol == IpProtocol::Tcp {
let tcp_packet = try!(TcpPacket::new(ipv4_packet.payload()));
if !tcp_packet.rst() {
let tcp_reply_repr = TcpRepr {
src_port: tcp_packet.dst_port(),
dst_port: tcp_packet.src_port(),
control: TcpControl::Rst,
seq_number: tcp_packet.ack_number(),
ack_number: Some(tcp_packet.seq_number() +
tcp_packet.segment_len()),
window_len: 0,
max_seg_size: None,
payload: &[]
};
let ipv4_reply_repr = Ipv4Repr {
src_addr: dst_addr,
dst_addr: src_addr,
protocol: IpProtocol::Tcp,
payload_len: tcp_reply_repr.buffer_len()
};
response = Response::Tcpv4(ipv4_reply_repr, tcp_reply_repr);
}
} else if !handled {
let reason;
if protocol == IpProtocol::Udp {
reason = Icmpv4DstUnreachable::PortUnreachable
} else {
reason = Icmpv4DstUnreachable::ProtoUnreachable
}
let mut data = [0; 8];
data.copy_from_slice(&ipv4_packet.payload()[0..8]);
let icmp_reply_repr = Icmpv4Repr::DstUnreachable {
reason: reason,
header: ipv4_repr,
data: data
};
let ipv4_reply_repr = Ipv4Repr {
src_addr: dst_addr,
dst_addr: src_addr,
protocol: IpProtocol::Icmp,
payload_len: icmp_reply_repr.buffer_len()
};
response = Response::Icmpv4(ipv4_reply_repr, icmp_reply_repr)
}
},
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
macro_rules! ip_response {
($tx_buffer:ident, $frame:ident, $ip_repr:ident) => ({
let dst_hardware_addr =
match self.arp_cache.lookup(&$ip_repr.dst_addr.into()) {
None => return Err(Error::Unaddressable),
Some(hardware_addr) => hardware_addr
};
let frame_len = EthernetFrame::<&[u8]>::buffer_len($ip_repr.buffer_len() +
$ip_repr.payload_len);
$tx_buffer = try!(self.device.transmit(frame_len));
$frame = try!(EthernetFrame::new(&mut $tx_buffer));
$frame.set_src_addr(self.hardware_addr);
$frame.set_dst_addr(dst_hardware_addr);
$frame.set_ethertype(EthernetProtocol::Ipv4);
let mut ip_packet = try!(Ipv4Packet::new($frame.payload_mut()));
$ip_repr.emit(&mut ip_packet);
ip_packet
})
}
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(EthernetProtocol::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);
Ok(())
2016-12-13 07:22:59 +08:00
},
Response::Icmpv4(ip_repr, icmp_repr) => {
let mut tx_buffer;
let mut frame;
let mut ip_packet = ip_response!(tx_buffer, frame, ip_repr);
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);
Ok(())
}
Response::Tcpv4(ip_repr, tcp_repr) => {
let mut tx_buffer;
let mut frame;
let mut ip_packet = ip_response!(tx_buffer, frame, ip_repr);
let mut tcp_packet = try!(TcpPacket::new(ip_packet.payload_mut()));
tcp_repr.emit(&mut tcp_packet,
&IpAddress::Ipv4(ip_repr.src_addr),
&IpAddress::Ipv4(ip_repr.dst_addr));
Ok(())
2016-12-12 15:19:53 +08:00
}
2016-12-13 07:22:59 +08:00
Response::Nop => {
Ok(())
}
}
}
2017-01-11 13:25:54 +08:00
fn emit(&mut self, sockets: &mut SocketSet, timestamp: u64) -> Result<bool, Error> {
// Borrow checker is being overly careful around closures, so we have
// to hack around that.
let src_hardware_addr = self.hardware_addr;
let src_protocol_addrs = self.protocol_addrs.as_ref();
let arp_cache = &mut self.arp_cache;
let device = &mut self.device;
2017-01-27 10:49:06 +08:00
let mtu = device.mtu() - EthernetFrame::<&[u8]>::header_len();
let mut nothing_to_transmit = true;
2017-01-11 13:25:54 +08:00
for socket in sockets.iter_mut() {
2017-01-27 10:49:06 +08:00
let result = socket.dispatch(timestamp, mtu, &mut |repr, payload| {
let repr = try!(repr.lower(src_protocol_addrs));
match arp_cache.lookup(&repr.dst_addr()) {
Some(dst_hardware_addr) => {
let tx_len = EthernetFrame::<&[u8]>::buffer_len(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(EthernetProtocol::Ipv4);
repr.emit(frame.payload_mut());
let mut ip_packet = try!(Ipv4Packet::new(frame.payload_mut()));
payload.emit(&repr, ip_packet.payload_mut());
}
None => {
let (src_addr, dst_addr) =
match (repr.src_addr(), repr.dst_addr()) {
(IpAddress::Ipv4(src_addr), IpAddress::Ipv4(dst_addr)) =>
(src_addr, dst_addr),
_ => unimplemented!()
};
let payload = ArpRepr::EthernetIpv4 {
operation: ArpOperation::Request,
source_hardware_addr: src_hardware_addr,
source_protocol_addr: src_addr,
target_hardware_addr: EthernetAddress::default(),
target_protocol_addr: dst_addr,
};
let tx_len = EthernetFrame::<&[u8]>::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(EthernetAddress([0xff; 6]));
frame.set_ethertype(EthernetProtocol::Arp);
let mut arp_packet = try!(ArpPacket::new(frame.payload_mut()));
payload.emit(&mut arp_packet);
}
}
Ok(())
});
match result {
Ok(()) => {
nothing_to_transmit = false;
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(nothing_to_transmit)
2016-12-12 15:19:53 +08:00
}
2016-12-12 10:39:46 +08:00
}