Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ defmt = ["dep:defmt", "heapless/defmt"]
"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 @@ -103,7 +104,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-eth", "socket-raw", "socket-icmp", "socket-udp", "socket-tcp", "socket-dhcpv4", "socket-dns", "socket-mdns", "socket-eth",
"packetmeta-id", "async", "multicast"
]

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Enable `smoltcp::phy::RawSocket` and `smoltcp::phy::TunTapInterface`, respective

These features are enabled by default.

### Features `socket-raw`, `socket-udp`, `socket-tcp`, `socket-icmp`, `socket-dhcpv4`, `socket-dns`
### Features `socket-eth`, `socket-raw`, `socket-udp`, `socket-tcp`, `socket-icmp`, `socket-dhcpv4`, `socket-dns`

Enable the corresponding socket type.

Expand Down
11 changes: 6 additions & 5 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,21 @@ FEATURES_TEST=(
"std,medium-ieee802154,proto-sixlowpan,proto-sixlowpan-fragmentation,socket-udp"
"std,medium-ieee802154,proto-rpl,proto-sixlowpan,proto-sixlowpan-fragmentation,socket-udp"
"std,medium-ip,proto-ipv4,proto-ipv6,socket-tcp,socket-udp"
"std,medium-ethernet,medium-ip,medium-ieee802154,proto-ipv4,proto-ipv6,multicast,proto-rpl,socket-raw,socket-udp,socket-tcp,socket-icmp,socket-dns,async"
"std,medium-ethernet,medium-ip,medium-ieee802154,proto-ipv4,proto-ipv6,multicast,proto-rpl,socket-eth,socket-raw,socket-udp,socket-tcp,socket-icmp,socket-dns,async"
"std,medium-ip,proto-ipv4,proto-ipv6,multicast,socket-raw,socket-udp,socket-tcp,socket-icmp,socket-dns,async"
"std,medium-ieee802154,medium-ip,proto-ipv4,socket-raw"
"std,medium-ethernet,proto-ipv4,proto-ipsec,socket-raw"
"std,medium-ethernet,proto-ipv4,socket-eth,packetmeta-id,async"
)

FEATURES_TEST_NIGHTLY=(
"alloc,medium-ethernet,proto-ipv4,proto-ipv6,socket-raw,socket-udp,socket-tcp,socket-icmp"
"alloc,medium-ethernet,proto-ipv4,proto-ipv6,socket-raw,socket-eth,socket-udp,socket-tcp,socket-icmp"
)

FEATURES_CHECK=(
"medium-ip,medium-ethernet,medium-ieee802154,proto-ipv6,proto-ipv6,multicast,proto-dhcpv4,proto-ipsec,socket-raw,socket-udp,socket-tcp,socket-icmp,socket-dns,async"
"defmt,medium-ip,medium-ethernet,proto-ipv6,proto-ipv6,multicast,proto-dhcpv4,socket-raw,socket-udp,socket-tcp,socket-icmp,socket-dns,async"
"defmt,alloc,medium-ip,medium-ethernet,proto-ipv6,proto-ipv6,multicast,proto-dhcpv4,socket-raw,socket-udp,socket-tcp,socket-icmp,socket-dns,async"
"medium-ip,medium-ethernet,medium-ieee802154,proto-ipv6,proto-ipv6,multicast,proto-dhcpv4,proto-ipsec,socket-eth,socket-raw,socket-udp,socket-tcp,socket-icmp,socket-dns,async"
"defmt,medium-ip,medium-ethernet,proto-ipv6,proto-ipv6,multicast,proto-dhcpv4,socket-eth,socket-raw,socket-udp,socket-tcp,socket-icmp,socket-dns,async"
"defmt,alloc,medium-ip,medium-ethernet,proto-ipv6,proto-ipv6,multicast,proto-dhcpv4,socket-eth,socket-raw,socket-udp,socket-tcp,socket-icmp,socket-dns,async"
"medium-ieee802154,proto-sixlowpan,socket-dns"
)

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

#[cfg(feature = "socket-eth")]
let _ = self.eth_socket_filter(
sockets,
meta,
&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 +53,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 +67,6 @@ impl InterfaceInner {
frame.set_src_addr(src_addr);

f(frame);

Ok(())
})
}
}
66 changes: 54 additions & 12 deletions src/iface/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,10 @@ impl Interface {
}

let mut neighbor_addr = None;
let mut respond = |inner: &mut InterfaceInner, meta: PacketMeta, response: Packet| {
let mut respond = |inner: &mut InterfaceInner,
meta: PacketMeta,
response: Packet|
-> Result<(), EgressError> {
neighbor_addr = Some(response.ip_repr().dst_addr());
let t = device.transmit(inner.now).ok_or_else(|| {
net_debug!("failed to transmit IP: device exhausted");
Expand Down Expand Up @@ -750,6 +753,26 @@ impl Interface {
Packet::new(ip, IpPayload::Udp(udp, dns)),
)
}),
#[cfg(feature = "socket-eth")]
Socket::Eth(socket) => {
socket.dispatch(&mut self.inner, |inner, meta, (eth_repr, payload)| {
let mut token = device.transmit(inner.now).ok_or_else(|| {
net_debug!("failed to transmit raw ETH: device exhausted");
EgressError::Exhausted
})?;
token.set_meta(meta);
inner.dispatch_ethernet(token, payload.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 @@ -904,6 +927,29 @@ impl InterfaceInner {
handled_by_raw_socket
}

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

// Pass every packet to all eth 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, meta, 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 @@ -941,7 +987,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 @@ -1074,17 +1121,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
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ compile_error!(
#[cfg(all(
feature = "socket",
not(any(
feature = "socket-eth",
feature = "socket-raw",
feature = "socket-udp",
feature = "socket-tcp",
Expand All @@ -112,7 +113,7 @@ compile_error!(
))
))]
compile_error!(
"If you enable the socket feature, you must enable at least one of the following features: socket-raw, socket-udp, socket-tcp, socket-icmp, socket-dhcpv4, socket-dns"
"If you enable the socket feature, you must enable at least one of the following features: socket-eth, socket-raw, socket-udp, socket-tcp, socket-icmp, socket-dhcpv4, socket-dns"
);

#[cfg(all(
Expand Down
Loading