Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add new socket type for raw ethernet protocols #1013

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ defmt = ["dep:defmt", "heapless/defmt-03"]
"socket-dhcpv4" = ["socket", "medium-ethernet", "proto-dhcpv4"]
"socket-dns" = ["socket", "proto-dns"]
"socket-mdns" = ["socket-dns"]
"socket-eth" = ["socket", "medium-ethernet"]

# Enable Cubic TCP congestion control algorithm, and it is used as a default congestion controller.
#
Expand All @@ -99,7 +100,7 @@ default = [
"phy-raw_socket", "phy-tuntap_interface",
"proto-ipv4", "proto-dhcpv4", "proto-ipv6", "proto-dns",
"proto-ipv4-fragmentation", "proto-sixlowpan-fragmentation",
"socket-raw", "socket-icmp", "socket-udp", "socket-tcp", "socket-dhcpv4", "socket-dns", "socket-mdns",
"socket-raw", "socket-icmp", "socket-udp", "socket-tcp", "socket-dhcpv4", "socket-dns", "socket-mdns", "socket-eth",
"packetmeta-id", "async", "multicast"
]

Expand Down
16 changes: 8 additions & 8 deletions src/iface/interface/ethernet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ impl InterfaceInner {
return None;
}

#[cfg(feature = "socket-eth")]
let _ = self.eth_socket_filter(
sockets,
&EthernetRepr::parse(&eth_frame).unwrap(),
eth_frame.payload(),
);

match eth_frame.ethertype() {
#[cfg(feature = "proto-ipv4")]
EthernetProtocol::Arp => self.process_arp(self.now, &eth_frame),
Expand Down Expand Up @@ -45,12 +52,7 @@ impl InterfaceInner {
}
}

pub(super) fn dispatch_ethernet<Tx, F>(
&mut self,
tx_token: Tx,
buffer_len: usize,
f: F,
) -> Result<(), DispatchError>
pub(super) fn dispatch_ethernet<Tx, F>(&mut self, tx_token: Tx, buffer_len: usize, f: F)
where
Tx: TxToken,
F: FnOnce(EthernetFrame<&mut [u8]>),
Expand All @@ -64,8 +66,6 @@ impl InterfaceInner {
frame.set_src_addr(src_addr);

f(frame);

Ok(())
})
}
}
60 changes: 49 additions & 11 deletions src/iface/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,26 @@ impl Interface {
Packet::new(ip, IpPayload::Udp(udp, dns)),
)
}),
#[cfg(feature = "socket-eth")]
Socket::Eth(socket) => {
socket.dispatch(&mut self.inner, |inner, (eth_repr, payload)| {
let token = device.transmit(inner.now).ok_or_else(|| {
net_debug!("failed to transmit raw ETH: device exhausted");
EgressError::Exhausted
})?;
let frame_len = eth_repr.buffer_len() + payload.len();
inner.dispatch_ethernet(token, frame_len, |mut frame| {
frame.set_dst_addr(eth_repr.dst_addr);
frame.set_src_addr(eth_repr.src_addr);
frame.set_ethertype(eth_repr.ethertype);
frame.payload_mut().copy_from_slice(payload);
});

result = PollResult::SocketStateChanged;

Ok(())
})
}
};

match result {
Expand Down Expand Up @@ -893,6 +913,28 @@ impl InterfaceInner {
handled_by_raw_socket
}

#[cfg(feature = "socket-eth")]
fn eth_socket_filter(
&mut self,
sockets: &mut SocketSet,
eth_repr: &EthernetRepr,
eth_payload: &[u8],
) -> bool {
let mut handled_by_eth_socket = false;

// Pass every IP packet to all raw sockets we have registered.
for eth_socket in sockets
.items_mut()
.filter_map(|i| eth::Socket::downcast_mut(&mut i.socket))
{
if eth_socket.accepts(eth_repr) {
eth_socket.process(self, eth_repr, eth_payload);
handled_by_eth_socket = true;
}
}
handled_by_eth_socket
}

/// Checks if an address is broadcast, taking into account ipv4 subnet-local
/// broadcast addresses.
pub(crate) fn is_broadcast(&self, address: &IpAddress) -> bool {
Expand Down Expand Up @@ -930,7 +972,8 @@ impl InterfaceInner {

let mut packet = ArpPacket::new_unchecked(frame.payload_mut());
arp_repr.emit(&mut packet);
})
});
Ok(())
}
EthernetPacket::Ip(packet) => {
self.dispatch_ip(tx_token, PacketMeta::default(), packet, frag)
Expand Down Expand Up @@ -1063,17 +1106,12 @@ impl InterfaceInner {
target_protocol_addr: dst_addr,
};

if let Err(e) =
self.dispatch_ethernet(tx_token, arp_repr.buffer_len(), |mut frame| {
frame.set_dst_addr(EthernetAddress::BROADCAST);
frame.set_ethertype(EthernetProtocol::Arp);
self.dispatch_ethernet(tx_token, arp_repr.buffer_len(), |mut frame| {
frame.set_dst_addr(EthernetAddress::BROADCAST);
frame.set_ethertype(EthernetProtocol::Arp);

arp_repr.emit(&mut ArpPacket::new_unchecked(frame.payload_mut()))
})
{
net_debug!("Failed to dispatch ARP request: {:?}", e);
return Err(DispatchError::NeighborPending);
}
arp_repr.emit(&mut ArpPacket::new_unchecked(frame.payload_mut()))
});
}

#[cfg(feature = "proto-ipv6")]
Expand Down
Loading
Loading