smoltcp-rs / smoltcp

a smol tcp/ip stack
BSD Zero Clause License
3.63k stars 402 forks source link

Bug in IPv6 source address selection (#864) prevents sending packets to localhost to localhost #894

Closed samueljero closed 5 months ago

samueljero commented 5 months ago

In particular, given an interface with only the IPv6 localhost address, it is not possible to send packets to one's self. If sending ICMP packets, smoltcp panics:

(this is version 0.11, but master has the same bug)

panicked at smoltcp-0.11.0/src/iface/interface/mod.rs:882:14:
called `Option::unwrap()` on a `None` value

This appears to result from a bug in #864, where localhost addresses are never considered for sending packets: https://github.com/smoltcp-rs/smoltcp/blob/d185a37a8e93a99aa1fe424f45b79420db8ddae5/src/iface/interface/ipv6.rs#L42-L47

Presumably this should have an exception for when the destination address is localhost.

Note that this only effects unbound sockets, as the IPv6 source address selection algorithm is not run for bound sockets.

thvdveld commented 5 months ago

Thank you for reporting this! The loopback address should be in the candidate list of source addresses. I think line 45 should be src_addr.unspecified() instead of src_addr.is_loopback(), as line 45 covers the following in RFC6724:

In any case, multicast addresses and the unspecified address MUST NOT be included in a candidate set.

I'll fix this and add more tests for the source address selection.