55 Commits

Author SHA1 Message Date
Thibaut Vandervelden
d26a313d9d fix(ipv6): don't panic if no suitable src_addr
Prevent panic when no suitable source address is found. If no suitable
address is found, the loopback address is used instead. The function can
still panic when the destination address is unspecified.

More tests are added:
- Tests when the interface has no addresses. The loopback address is
  used as source address.
- Tests when the interface only has a link-local address. The link-local
  address is used as source address, unless the destination address is
  the loopback address. In this case, the loopback address is used as
  source address.
2024-01-11 14:24:46 +01:00
Thibaut Vandervelden
951b59ba1c change(socket/icmp): split ICMPv4/v6 processing
Splitting the accept and process functions into separate functions for
IPv4 and IPv6 allows us to avoid the `IpRepr` enum and instead use the
`Ipv4Repr` and `Ipv6Repr` structs directly. This reduces the binary size
by 1.5 KiB.
2024-01-05 14:23:26 +01:00
Thibaut Vandervelden
dd9eff7cf9 ipv6: use RFC6724 for selecting IPv6 src address
RFC6724 defines how the source address should be selected when given a
destination address. Instead of selecting the first address in the list
of interface addresses, the source address is selected following the
standard.
2023-12-01 11:18:47 +01:00
Thibaut Vandervelden
533f103a95
pass the now time when creating the iface
Signed-off-by: Thibaut Vandervelden <thvdveld@vub.be>
2023-06-12 16:31:38 +02:00
Thibaut Vandervelden
099f5f7c2a Don't use Option for neighbor cache anymore 2023-04-04 18:54:33 +02:00
Dario Nieuwenhuis
5740b76574 iface: remove builder. 2023-01-19 14:43:15 +01:00
Dario Nieuwenhuis
050731b519 iface: remove all uses of crate::{Error, Result}; 2023-01-14 21:57:36 +01:00
Dario Nieuwenhuis
3a080d818b Clippy fixes 2023-01-02 00:20:05 +01:00
Dario Nieuwenhuis
4eb49b808a iface: use heapless LinearMap for neighbor cache. 2022-12-23 18:13:03 +01:00
Dario Nieuwenhuis
9beb57a992 iface: use heapless Vec for routes.
Lookup is O(n) now. However, it previously did 32 (or 128 for ipv6!)
map lookups. Since the route table typically doesn't have that many
routes, the new code is likely faster even if it's O(n).
2022-12-23 18:13:03 +01:00
Davide Della Giustina
4ddb96b1b2
Increased max number of IPs from 4 to 5 because some test use 5 2022-12-20 14:04:24 +00:00
Davide Della Giustina
aec718a08f
Fix clippy and rustfmt errors 2022-12-20 13:21:28 +00:00
Davide Della Giustina
8cf7625cc2
ip_addrs field of an interface is now a heapless::Vec (one test still failing) 2022-12-20 12:02:10 +00:00
Dario Nieuwenhuis
9e18ca127e socket_set: add get_mut, make get immutable. 2022-06-06 02:26:45 +02:00
Dario Nieuwenhuis
d703a66d1d iface: borrow the device instead of owning it. 2022-06-06 02:26:45 +02:00
Dario Nieuwenhuis
b71ffcb6cc iface: borrow the SocketSet instead of owning. 2022-06-06 01:14:53 +02:00
Dario Nieuwenhuis
72a9ee46ce socket: reorganize module structure, with one module per protocol. 2022-05-20 02:18:00 +02:00
Dario Nieuwenhuis
eb41d077e0 Remove IpAddress::Unspecified, assign src addr in sockets.
This continues work started in #579, with the goal of "remove unspecified variants from wire".

"unspecified" variants are bad, they've been a source of bugs in the past. The issue with them is that
depending on the context it may or may not make sense for the value to be unspecified.
It's better to not have them, and then use Option only where the value can really be unspecified.

This removes lots of `Address::Unspecified => unreachable!()` and similar match arms, which shows the
unreachable variant was actively unwanted in many places.

This fixes the "unspecified src addr" panic:

- Picking src addr is now the resposibility of the sockets, not the interface. All sockets now emit IpReprs with properly assigned src addr.
- Removed the `assert!(!ip_repr.src_addr().is_unspecified());`. This assert is WRONG even if
  now sockets pick the source address, because there ARE cases where we indeed want to send a
  packet with zero src addr, for example in DHCP.
2022-05-19 04:38:55 +02:00
Dario Nieuwenhuis
9a2093e39e Remove braces that were previously needed to workaround SocketRef borrow issues. 2021-11-03 23:32:32 +01:00
Dario Nieuwenhuis
ef213fa772 socket: remove SocketRef.
The intent was to run custom code after the user is done modifying the socket,
for example to update a (not yet existing) port->socket map in SocketSet. However
this wouldn't work, since the SocketRef would have to borrow the SocketSet at
the same time as the Socket to be able to notify the SocketSet.

I believe such indexing can be achieved by setting a "dirty" bit *before* giving
the socket to the user, then on poll() reindexing all dirty sockets. This could
even be faster: if user gets a socket multiple times between polls, it'd be reindexed
only once.
2021-11-03 23:32:32 +01:00
Dario Nieuwenhuis
bde881d2f9 iface: own the SocketSet instead of borrowing it 2021-11-03 23:32:32 +01:00
Dario Nieuwenhuis
68e25a29c3 Add RawHardwareAddress, use it in wire ndisc.
This avoids wire needing to know what medium we're on.
2021-10-21 01:25:13 +02:00
Thibaut Vandervelden
3d782f19cd Add support for 802.15.4 and 6LoWPAN 2021-10-21 01:25:12 +02:00
Dario Nieuwenhuis
2cb407d62a rustfmt 2021-06-27 09:31:59 +02:00
Dario Nieuwenhuis
b869449b31 Add support for TUN interfaces. 2021-03-31 17:05:09 +02:00
Dario Nieuwenhuis
d352e151f6 Add support for IP mediums.
- Add `medium` in `DeviceCapabilities`.
- Rename EthernetInterface to Interface.
- Add support to Interface for both Ethernet and IP mediums. The medium to use is detected from `device.capabilities().medium`.
- Ethernet-only features are gated behind the "ethernet" feature, as before.
- IP features are always enabled for now.
2021-03-31 17:05:09 +02:00
Alex Crawford
1cbc38d0ea Clean up examples
These were flagged by `cargo clippy`:

    warning: you seem to be trying to use match for destructuring a
             single pattern. Consider using `if let`
    warning: called `.nth(0)` on a `std::iter::Iterator`, when `.next()`
             is equivalent
    warning: using `write!()` with a format string that ends in a single
             newline
    warning: useless conversion to the same type:
             `smoltcp::wire::Ipv4Address`
    warning: called `map(f)` on an `Option` value where `f` is a closure
             that returns the unit type `()`
    warning: returning the result of a `let` binding from a block
    warning: use of `unwrap_or` followed by a function call
2021-01-04 10:39:27 -08:00
Dario Nieuwenhuis
1e40b934bf Update to Rust 2018. Fixes #271 2020-12-27 17:54:56 +01:00
whitequark
a4d0a7e39c Fix unused mut in examples/ping.rs. NFC. 2019-06-22 08:20:41 +00:00
whitequark
185a0ca781 examples: remove the remaining panics on poll error. 2018-07-31 13:58:00 +00:00
whitequark
4a253fecdf Rename new method on Packet types to new_checked.
Fixes #195.

Closes: #254
Approved by: dlrobertson
2018-07-11 12:59:05 +08:00
Michal Podhradsky
4c7606d9c3 Log and print error for all examples 2018-06-22 19:30:03 +00:00
Valentin Lorentz
06d18130cd Add support for arbitrarily many routes instead of only gateways.
Closes: #219
Approved by: whitequark
2018-06-10 05:31:08 +08:00
Valentin Lorentz
4bf917ced3 Add support for IPv6 gateways.
Closes: #207
Approved by: dlrobertson
2018-05-14 09:39:39 +08:00
whitequark
cd893e6ab6 fn PacketMetadata::empty() → const PacketMetadata::EMPTY.
Fixes #181.
2018-03-24 03:23:37 +00:00
whitequark
21ca116fd7 Fix an off-by-1000 in time display in examples/ping.rs. 2018-02-22 12:25:07 +00:00
whitequark
95fd1f2568 Use storage::PacketBuffer for implementing socket::IcmpSocket.
This substantially increases its space efficiency.
2018-02-22 12:23:35 +00:00
Dan Robertson
8e74fce11d Update examples to use time types
Closes: #141
Approved by: whitequark
2018-02-16 10:44:02 +08:00
whitequark
3868dcdb14 Split poll_at/poll_delay out of poll.
The previous model was flawed. Consider the following case:
  * The main loop looks as follows (pseudocode):
      loop {
        let _ = (tcp:1234).read_all()
        wait(iface.poll())
      }
  * The remote end is continuously transmitting data and at some
    point fills the window of (tcp:1234), stopping the transmission
    afterwards.
  * The local end processes the packets and, as a part of egress
    routine, emits an ACK. That also updates the window, and
    the socket's poll_at() routine returns None, since there is
    nothing to transmit or retransmit.
  * The local end now waits indefinitely even though it can start
    processing the data in the socket buffers right now.
2017-12-22 12:59:52 +00:00
Dan Robertson
ad56bacb56 Replace EthernetInterface::new with EthernetInterfaceBuilder. 2017-12-18 13:29:29 +00:00
whitequark
34c3a8c905 Rewrite the ARP cache to allow for flood protection and expiration. 2017-11-21 10:53:55 +00:00
Dan Robertson
ef4af850e0 Add ICMP sockets
- Add support for ICMP sockets
 - Add tests for ICMP sockets
 - Rename proto-<type> features to socket-<type>
 - Update documentation
2017-11-09 00:08:24 +00:00
Philipp Oppermann
4ae84ab36a Store plain DeviceT instead of Managed<_> in EthernetInterface 2017-11-08 09:34:35 +00:00
Dan Robertson
fea462837d Implement set_ttl for Tcp and Udp sockets
- Add the ttl member to the IpRepr
 - Add the ttl member along with setters and getters to the tcp and udp
   socket types
 - Add unit tests for the new set_ttl parameter
 - Update usage of IpRepr to include the ttl value
2017-10-24 23:02:18 +00:00
Egor Karavaev
096ce02ac4 Implement a SocketRef smart pointer to detect state changes. 2017-10-05 03:44:20 +00:00
whitequark
d88ef3c8d3 Drop the pretense that anyone cares about non-IP over Ethernet.
To be precise, I'm talking about IPX, AppleTalk and DECnet here,
not things like PPPoE, ATAoE, FCoE, or PTP, which make sense
to implement on top of EthernetInterface but do not work on
the same level on top of it as IP.
2017-10-03 15:17:29 +00:00
Egor Karavaev
331dc10780 Add support for IPv4 default gateway. 2017-10-03 15:17:29 +00:00
Steffen Butzer
d5147efb82 support hardware based checksum settings in during packet send/recv
- makes sure the checksum is zeroed when not emitted by software
  (This is required by some implementations such as STM32 to work properly)
2017-10-02 21:40:08 +00:00
whitequark
16e6e70d2f Fix many warnings. 2017-09-25 00:55:54 +00:00
whitequark
cedf66a733 Enforce some lints. 2017-09-25 00:46:53 +00:00