Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/smoltcp-rs/smoltcp into fix…
Browse files Browse the repository at this point in the history
…/mismatching-src-ip
  • Loading branch information
MathiasKoch committed Oct 28, 2024
2 parents 1c89f9e + e9b66ea commit d07c192
Show file tree
Hide file tree
Showing 53 changed files with 1,095 additions and 1,454 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "smoltcp"
version = "0.11.0"
edition = "2021"
rust-version = "1.77"
rust-version = "1.80"
authors = ["whitequark <whitequark@whitequark.org>"]
description = "A TCP/IP stack designed for bare-metal, real-time systems without a heap."
documentation = "https://docs.rs/smoltcp/"
Expand All @@ -25,7 +25,7 @@ byteorder = { version = "1.0", default-features = false }
log = { version = "0.4.4", default-features = false, optional = true }
libc = { version = "0.2.18", optional = true }
bitflags = { version = "1.0", default-features = false }
defmt = { version = "0.3", optional = true }
defmt = { version = "0.3.8", optional = true, features = ["ip_in_core"] }
cfg-if = "1.0.0"
heapless = "0.8"

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ include complicated compile-time computations, such as macro or type tricks, eve
at cost of performance degradation.

_smoltcp_ does not need heap allocation *at all*, is [extensively documented][docs],
and compiles on stable Rust 1.77 and later.
and compiles on stable Rust 1.80 and later.

_smoltcp_ achieves [~Gbps of throughput](#examplesbenchmarkrs) when tested against
the Linux TCP stack in loopback mode.
Expand Down
20 changes: 8 additions & 12 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@ mod wire {
extern crate test;

#[cfg(feature = "proto-ipv6")]
const SRC_ADDR: IpAddress = IpAddress::Ipv6(Ipv6Address([
0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]));
const SRC_ADDR: IpAddress = IpAddress::Ipv6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1));
#[cfg(feature = "proto-ipv6")]
const DST_ADDR: IpAddress = IpAddress::Ipv6(Ipv6Address([
0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
]));
const DST_ADDR: IpAddress = IpAddress::Ipv6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 2));

#[cfg(all(not(feature = "proto-ipv6"), feature = "proto-ipv4"))]
const SRC_ADDR: IpAddress = IpAddress::Ipv4(Ipv4Address([192, 168, 1, 1]));
const SRC_ADDR: IpAddress = IpAddress::Ipv4(Ipv4Address::new(192, 168, 1, 1));
#[cfg(all(not(feature = "proto-ipv6"), feature = "proto-ipv4"))]
const DST_ADDR: IpAddress = IpAddress::Ipv4(Ipv4Address([192, 168, 1, 2]));
const DST_ADDR: IpAddress = IpAddress::Ipv4(Ipv4Address::new(192, 168, 1, 2));

#[bench]
#[cfg(any(feature = "proto-ipv6", feature = "proto-ipv4"))]
Expand Down Expand Up @@ -84,8 +80,8 @@ mod wire {
#[cfg(feature = "proto-ipv4")]
fn bench_emit_ipv4(b: &mut test::Bencher) {
let repr = Ipv4Repr {
src_addr: Ipv4Address([192, 168, 1, 1]),
dst_addr: Ipv4Address([192, 168, 1, 2]),
src_addr: Ipv4Address::new(192, 168, 1, 1),
dst_addr: Ipv4Address::new(192, 168, 1, 2),
next_header: IpProtocol::Tcp,
payload_len: 100,
hop_limit: 64,
Expand All @@ -102,8 +98,8 @@ mod wire {
#[cfg(feature = "proto-ipv6")]
fn bench_emit_ipv6(b: &mut test::Bencher) {
let repr = Ipv6Repr {
src_addr: Ipv6Address([0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]),
dst_addr: Ipv6Address([0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]),
src_addr: Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1),
dst_addr: Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 2),
next_header: IpProtocol::Tcp,
payload_len: 100,
hop_limit: 64,
Expand Down
2 changes: 1 addition & 1 deletion ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -eox pipefail

export DEFMT_LOG=trace

MSRV="1.77.0"
MSRV="1.80.0"

RUSTC_VERSIONS=(
$MSRV
Expand Down
1 change: 0 additions & 1 deletion examples/loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#![allow(clippy::collapsible_if)]

#[cfg(feature = "std")]
#[allow(dead_code)]
mod utils;

use core::str;
Expand Down
6 changes: 2 additions & 4 deletions examples/multicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use smoltcp::wire::{
};

const MDNS_PORT: u16 = 5353;
const MDNS_GROUP: [u8; 4] = [224, 0, 0, 251];
const MDNS_GROUP: Ipv4Address = Ipv4Address::new(224, 0, 0, 251);

fn main() {
utils::setup_logging("warn");
Expand Down Expand Up @@ -81,9 +81,7 @@ fn main() {
let udp_handle = sockets.add(udp_socket);

// Join a multicast group to receive mDNS traffic
iface
.join_multicast_group(Ipv4Address::from_bytes(&MDNS_GROUP))
.unwrap();
iface.join_multicast_group(MDNS_GROUP).unwrap();

loop {
let timestamp = Instant::now();
Expand Down
15 changes: 6 additions & 9 deletions examples/multicast6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv6Address};
// will send packets to the multicast group we join below on tap0.

const PORT: u16 = 8123;
const GROUP: [u16; 8] = [0xff02, 0, 0, 0, 0, 0, 0, 0x1234];
const LOCAL_ADDR: [u16; 8] = [0xfe80, 0, 0, 0, 0, 0, 0, 0x101];
const ROUTER_ADDR: [u16; 8] = [0xfe80, 0, 0, 0, 0, 0, 0, 0x100];
const GROUP: Ipv6Address = Ipv6Address::new(0xff02, 0, 0, 0, 0, 0, 0, 0x1234);
const LOCAL_ADDR: Ipv6Address = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x101);
const ROUTER_ADDR: Ipv6Address = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x100);

fn main() {
utils::setup_logging("warn");
Expand All @@ -37,7 +37,6 @@ fn main() {
utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);

// Create interface
let local_addr = Ipv6Address::from_parts(&LOCAL_ADDR);
let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
let mut config = match device.capabilities().medium {
Medium::Ethernet => Config::new(ethernet_addr.into()),
Expand All @@ -49,12 +48,12 @@ fn main() {
let mut iface = Interface::new(config, &mut device, Instant::now());
iface.update_ip_addrs(|ip_addrs| {
ip_addrs
.push(IpCidr::new(IpAddress::from(local_addr), 64))
.push(IpCidr::new(IpAddress::from(LOCAL_ADDR), 64))
.unwrap();
});
iface
.routes_mut()
.add_default_ipv6_route(Ipv6Address::from_parts(&ROUTER_ADDR))
.add_default_ipv6_route(ROUTER_ADDR)
.unwrap();

// Create sockets
Expand All @@ -65,9 +64,7 @@ fn main() {
let udp_handle = sockets.add(udp_socket);

// Join a multicast group
iface
.join_multicast_group(Ipv6Address::from_parts(&GROUP))
.unwrap();
iface.join_multicast_group(GROUP).unwrap();

loop {
let timestamp = Instant::now();
Expand Down
10 changes: 5 additions & 5 deletions src/iface/fragmentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl<K> PacketAssembler<K> {
/// # Errors
///
/// - Returns [`Error::PacketAssemblerBufferTooSmall`] when trying to add data into the buffer at a non-existing
/// place.
/// place.
pub(crate) fn add(&mut self, data: &[u8], offset: usize) -> Result<(), AssemblerError> {
#[cfg(not(feature = "alloc"))]
if self.buffer.len() < offset + data.len() {
Expand Down Expand Up @@ -329,8 +329,8 @@ impl Fragmenter {
#[cfg(feature = "proto-ipv4-fragmentation")]
ipv4: Ipv4Fragmenter {
repr: Ipv4Repr {
src_addr: Ipv4Address::default(),
dst_addr: Ipv4Address::default(),
src_addr: Ipv4Address::new(0, 0, 0, 0),
dst_addr: Ipv4Address::new(0, 0, 0, 0),
next_header: IpProtocol::Unknown(0),
payload_len: 0,
hop_limit: 0,
Expand Down Expand Up @@ -373,8 +373,8 @@ impl Fragmenter {
#[cfg(feature = "proto-ipv4-fragmentation")]
{
self.ipv4.repr = Ipv4Repr {
src_addr: Ipv4Address::default(),
dst_addr: Ipv4Address::default(),
src_addr: Ipv4Address::new(0, 0, 0, 0),
dst_addr: Ipv4Address::new(0, 0, 0, 0),
next_header: IpProtocol::Unknown(0),
payload_len: 0,
hop_limit: 0,
Expand Down
30 changes: 22 additions & 8 deletions src/iface/interface/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl InterfaceInner {

/// Checks if an ipv4 address is unicast, taking into account subnet broadcast addresses
fn is_unicast_v4(&self, address: Ipv4Address) -> bool {
address.is_unicast() && !self.is_broadcast_v4(address)
address.x_is_unicast() && !self.is_broadcast_v4(address)
}

/// Get the first IPv4 address of the interface.
Expand Down Expand Up @@ -181,13 +181,27 @@ impl InterfaceInner {
{
// Ignore IP packets not directed at us, or broadcast, or any of the multicast groups.
// If AnyIP is enabled, also check if the packet is routed locally.
if !self.any_ip
|| !ipv4_repr.dst_addr.is_unicast()
|| self
.routes
.lookup(&IpAddress::Ipv4(ipv4_repr.dst_addr), self.now)
.map_or(true, |router_addr| !self.has_ip_addr(router_addr))

if !self.any_ip {
net_trace!("Rejecting IPv4 packet; any_ip=false");
return None;
}

if !ipv4_repr.dst_addr.x_is_unicast() {
net_trace!(
"Rejecting IPv4 packet; {} is not a unicast address",
ipv4_repr.dst_addr
);
return None;
}

if self
.routes
.lookup(&IpAddress::Ipv4(ipv4_repr.dst_addr), self.now)
.map_or(true, |router_addr| !self.has_ip_addr(router_addr))
{
net_trace!("Rejecting IPv4 packet; no matching routes");

return None;
}
}
Expand Down Expand Up @@ -260,7 +274,7 @@ impl InterfaceInner {
}

// Discard packets with non-unicast source addresses.
if !source_protocol_addr.is_unicast() || !source_hardware_addr.is_unicast() {
if !source_protocol_addr.x_is_unicast() || !source_hardware_addr.is_unicast() {
net_debug!("arp: non-unicast source address");
return None;
}
Expand Down
Loading

0 comments on commit d07c192

Please sign in to comment.