smoltcp/socket/
dhcpv4.rs

1#[cfg(feature = "async")]
2use core::task::Waker;
3
4use crate::iface::Context;
5use crate::time::{Duration, Instant};
6use crate::wire::dhcpv4::field as dhcpv4_field;
7use crate::wire::{
8    DhcpMessageType, DhcpPacket, DhcpRepr, IpAddress, IpProtocol, Ipv4Address, Ipv4Cidr, Ipv4Repr,
9    UdpRepr, DHCP_CLIENT_PORT, DHCP_MAX_DNS_SERVER_COUNT, DHCP_SERVER_PORT, UDP_HEADER_LEN,
10};
11use crate::wire::{DhcpOption, HardwareAddress};
12use heapless::Vec;
13
14#[cfg(feature = "async")]
15use super::WakerRegistration;
16
17use super::PollAt;
18
19const DEFAULT_LEASE_DURATION: Duration = Duration::from_secs(120);
20
21const DEFAULT_PARAMETER_REQUEST_LIST: &[u8] = &[
22    dhcpv4_field::OPT_SUBNET_MASK,
23    dhcpv4_field::OPT_ROUTER,
24    dhcpv4_field::OPT_DOMAIN_NAME_SERVER,
25];
26
27/// IPv4 configuration data provided by the DHCP server.
28#[derive(Debug, Eq, PartialEq, Clone)]
29#[cfg_attr(feature = "defmt", derive(defmt::Format))]
30pub struct Config<'a> {
31    /// Information on how to reach the DHCP server that responded with DHCP
32    /// configuration.
33    pub server: ServerInfo,
34    /// IP address
35    pub address: Ipv4Cidr,
36    /// Router address, also known as default gateway. Does not necessarily
37    /// match the DHCP server's address.
38    pub router: Option<Ipv4Address>,
39    /// DNS servers
40    pub dns_servers: Vec<Ipv4Address, DHCP_MAX_DNS_SERVER_COUNT>,
41    /// Received DHCP packet
42    pub packet: Option<DhcpPacket<&'a [u8]>>,
43}
44
45/// Information on how to reach a DHCP server.
46#[derive(Debug, Clone, Copy, Eq, PartialEq)]
47#[cfg_attr(feature = "defmt", derive(defmt::Format))]
48pub struct ServerInfo {
49    /// IP address to use as destination in outgoing packets
50    pub address: Ipv4Address,
51    /// Server identifier to use in outgoing packets. Usually equal to server_address,
52    /// but may differ in some situations (eg DHCP relays)
53    pub identifier: Ipv4Address,
54}
55
56#[derive(Debug)]
57#[cfg_attr(feature = "defmt", derive(defmt::Format))]
58struct DiscoverState {
59    /// When to send next request
60    retry_at: Instant,
61}
62
63#[derive(Debug)]
64#[cfg_attr(feature = "defmt", derive(defmt::Format))]
65struct RequestState {
66    /// When to send next request
67    retry_at: Instant,
68    /// How many retries have been done
69    retry: u16,
70    /// Server we're trying to request from
71    server: ServerInfo,
72    /// IP address that we're trying to request.
73    requested_ip: Ipv4Address,
74}
75
76#[derive(Debug)]
77#[cfg_attr(feature = "defmt", derive(defmt::Format))]
78struct RenewState {
79    /// Active network config
80    config: Config<'static>,
81
82    /// Renew timer. When reached, we will start attempting
83    /// to renew this lease with the DHCP server.
84    ///
85    /// Must be less or equal than `rebind_at`.
86    renew_at: Instant,
87
88    /// Rebind timer. When reached, we will start broadcasting to renew
89    /// this lease with any DHCP server.
90    ///
91    /// Must be greater than or equal to `renew_at`, and less than or
92    /// equal to `expires_at`.
93    rebind_at: Instant,
94
95    /// Whether the T2 time has elapsed
96    rebinding: bool,
97
98    /// Expiration timer. When reached, this lease is no longer valid, so it must be
99    /// thrown away and the ethernet interface deconfigured.
100    expires_at: Instant,
101}
102
103#[derive(Debug)]
104#[cfg_attr(feature = "defmt", derive(defmt::Format))]
105enum ClientState {
106    /// Discovering the DHCP server
107    Discovering(DiscoverState),
108    /// Requesting an address
109    Requesting(RequestState),
110    /// Having an address, refresh it periodically.
111    Renewing(RenewState),
112}
113
114/// Timeout and retry configuration.
115#[derive(Debug, PartialEq, Eq, Copy, Clone)]
116#[cfg_attr(feature = "defmt", derive(defmt::Format))]
117#[non_exhaustive]
118pub struct RetryConfig {
119    pub discover_timeout: Duration,
120    /// The REQUEST timeout doubles every 2 tries.
121    pub initial_request_timeout: Duration,
122    pub request_retries: u16,
123    pub min_renew_timeout: Duration,
124    /// An upper bound on how long to wait between retrying a renew or rebind.
125    ///
126    /// Set this to [`Duration::MAX`] if you don't want to impose an upper bound.
127    pub max_renew_timeout: Duration,
128}
129
130impl Default for RetryConfig {
131    fn default() -> Self {
132        Self {
133            discover_timeout: Duration::from_secs(10),
134            initial_request_timeout: Duration::from_secs(5),
135            request_retries: 5,
136            min_renew_timeout: Duration::from_secs(60),
137            max_renew_timeout: Duration::MAX,
138        }
139    }
140}
141
142/// Return value for the `Dhcpv4Socket::poll` function
143#[derive(Debug, PartialEq, Eq)]
144#[cfg_attr(feature = "defmt", derive(defmt::Format))]
145pub enum Event<'a> {
146    /// Configuration has been lost (for example, the lease has expired)
147    Deconfigured,
148    /// Configuration has been newly acquired, or modified.
149    Configured(Config<'a>),
150}
151
152#[derive(Debug)]
153pub struct Socket<'a> {
154    /// State of the DHCP client.
155    state: ClientState,
156    /// Set to true on config/state change, cleared back to false by the `config` function.
157    config_changed: bool,
158    /// xid of the last sent message.
159    transaction_id: u32,
160
161    /// Max lease duration. If set, it sets a maximum cap to the server-provided lease duration.
162    /// Useful to react faster to IP configuration changes and to test whether renews work correctly.
163    max_lease_duration: Option<Duration>,
164
165    retry_config: RetryConfig,
166
167    /// Ignore NAKs.
168    ignore_naks: bool,
169
170    /// Server port config
171    pub(crate) server_port: u16,
172
173    /// Client port config
174    pub(crate) client_port: u16,
175
176    /// A buffer contains options additional to be added to outgoing DHCP
177    /// packets.
178    outgoing_options: &'a [DhcpOption<'a>],
179    /// A buffer containing all requested parameters.
180    parameter_request_list: Option<&'a [u8]>,
181
182    /// Incoming DHCP packets are copied into this buffer, overwriting the previous.
183    receive_packet_buffer: Option<&'a mut [u8]>,
184
185    /// Waker registration
186    #[cfg(feature = "async")]
187    waker: WakerRegistration,
188}
189
190/// DHCP client socket.
191///
192/// The socket acquires an IP address configuration through DHCP autonomously.
193/// You must query the configuration with `.poll()` after every call to `Interface::poll()`,
194/// and apply the configuration to the `Interface`.
195impl<'a> Socket<'a> {
196    /// Create a DHCPv4 socket
197    #[allow(clippy::new_without_default)]
198    pub fn new() -> Self {
199        Socket {
200            state: ClientState::Discovering(DiscoverState {
201                retry_at: Instant::from_millis(0),
202            }),
203            config_changed: true,
204            transaction_id: 1,
205            max_lease_duration: None,
206            retry_config: RetryConfig::default(),
207            ignore_naks: false,
208            outgoing_options: &[],
209            parameter_request_list: None,
210            receive_packet_buffer: None,
211            #[cfg(feature = "async")]
212            waker: WakerRegistration::new(),
213            server_port: DHCP_SERVER_PORT,
214            client_port: DHCP_CLIENT_PORT,
215        }
216    }
217
218    /// Set the retry/timeouts configuration.
219    pub fn set_retry_config(&mut self, config: RetryConfig) {
220        self.retry_config = config;
221    }
222
223    /// Gets the current retry/timeouts configuration
224    pub fn get_retry_config(&self) -> RetryConfig {
225        self.retry_config
226    }
227
228    /// Set the outgoing options.
229    pub fn set_outgoing_options(&mut self, options: &'a [DhcpOption<'a>]) {
230        self.outgoing_options = options;
231    }
232
233    /// Set the buffer into which incoming DHCP packets are copied into.
234    pub fn set_receive_packet_buffer(&mut self, buffer: &'a mut [u8]) {
235        self.receive_packet_buffer = Some(buffer);
236    }
237
238    /// Set the parameter request list.
239    ///
240    /// This should contain at least `OPT_SUBNET_MASK` (`1`), `OPT_ROUTER`
241    /// (`3`), and `OPT_DOMAIN_NAME_SERVER` (`6`).
242    pub fn set_parameter_request_list(&mut self, parameter_request_list: &'a [u8]) {
243        self.parameter_request_list = Some(parameter_request_list);
244    }
245
246    /// Get the configured max lease duration.
247    ///
248    /// See also [`Self::set_max_lease_duration()`]
249    pub fn max_lease_duration(&self) -> Option<Duration> {
250        self.max_lease_duration
251    }
252
253    /// Set the max lease duration.
254    ///
255    /// When set, the lease duration will be capped at the configured duration if the
256    /// DHCP server gives us a longer lease. This is generally not recommended, but
257    /// can be useful for debugging or reacting faster to network configuration changes.
258    ///
259    /// If None, no max is applied (the lease duration from the DHCP server is used.)
260    pub fn set_max_lease_duration(&mut self, max_lease_duration: Option<Duration>) {
261        self.max_lease_duration = max_lease_duration;
262    }
263
264    /// Get whether to ignore NAKs.
265    ///
266    /// See also [`Self::set_ignore_naks()`]
267    pub fn ignore_naks(&self) -> bool {
268        self.ignore_naks
269    }
270
271    /// Set whether to ignore NAKs.
272    ///
273    /// This is not compliant with the DHCP RFCs, since theoretically
274    /// we must stop using the assigned IP when receiving a NAK. This
275    /// can increase reliability on broken networks with buggy routers
276    /// or rogue DHCP servers, however.
277    pub fn set_ignore_naks(&mut self, ignore_naks: bool) {
278        self.ignore_naks = ignore_naks;
279    }
280
281    /// Set the server/client port
282    ///
283    /// Allows you to specify the ports used by DHCP.
284    /// This is meant to support esoteric usecases allowed by the dhclient program.
285    pub fn set_ports(&mut self, server_port: u16, client_port: u16) {
286        self.server_port = server_port;
287        self.client_port = client_port;
288    }
289
290    pub(crate) fn poll_at(&self, _cx: &mut Context) -> PollAt {
291        let t = match &self.state {
292            ClientState::Discovering(state) => state.retry_at,
293            ClientState::Requesting(state) => state.retry_at,
294            ClientState::Renewing(state) => if state.rebinding {
295                state.rebind_at
296            } else {
297                state.renew_at.min(state.rebind_at)
298            }
299            .min(state.expires_at),
300        };
301        PollAt::Time(t)
302    }
303
304    pub(crate) fn process(
305        &mut self,
306        cx: &mut Context,
307        ip_repr: &Ipv4Repr,
308        repr: &UdpRepr,
309        payload: &[u8],
310    ) {
311        let src_ip = ip_repr.src_addr;
312
313        // This is enforced in interface.rs.
314        assert!(repr.src_port == self.server_port && repr.dst_port == self.client_port);
315
316        let dhcp_packet = match DhcpPacket::new_checked(payload) {
317            Ok(dhcp_packet) => dhcp_packet,
318            Err(e) => {
319                net_debug!("DHCP invalid pkt from {}: {:?}", src_ip, e);
320                return;
321            }
322        };
323        let dhcp_repr = match DhcpRepr::parse(&dhcp_packet) {
324            Ok(dhcp_repr) => dhcp_repr,
325            Err(e) => {
326                net_debug!("DHCP error parsing pkt from {}: {:?}", src_ip, e);
327                return;
328            }
329        };
330
331        let HardwareAddress::Ethernet(ethernet_addr) = cx.hardware_addr() else {
332            panic!("using DHCPv4 socket with a non-ethernet hardware address.");
333        };
334
335        if dhcp_repr.client_hardware_address != ethernet_addr {
336            return;
337        }
338        if dhcp_repr.transaction_id != self.transaction_id {
339            return;
340        }
341        let server_identifier = match dhcp_repr.server_identifier {
342            Some(server_identifier) => server_identifier,
343            None => {
344                net_debug!(
345                    "DHCP ignoring {:?} because missing server_identifier",
346                    dhcp_repr.message_type
347                );
348                return;
349            }
350        };
351
352        net_debug!(
353            "DHCP recv {:?} from {}: {:?}",
354            dhcp_repr.message_type,
355            src_ip,
356            dhcp_repr
357        );
358
359        // Copy over the payload into the receive packet buffer.
360        if let Some(buffer) = self.receive_packet_buffer.as_mut() {
361            if let Some(buffer) = buffer.get_mut(..payload.len()) {
362                buffer.copy_from_slice(payload);
363            }
364        }
365
366        match (&mut self.state, dhcp_repr.message_type) {
367            (ClientState::Discovering(_state), DhcpMessageType::Offer) => {
368                if !dhcp_repr.your_ip.is_unicast() {
369                    net_debug!("DHCP ignoring OFFER because your_ip is not unicast");
370                    return;
371                }
372
373                self.state = ClientState::Requesting(RequestState {
374                    retry_at: cx.now(),
375                    retry: 0,
376                    server: ServerInfo {
377                        address: src_ip,
378                        identifier: server_identifier,
379                    },
380                    requested_ip: dhcp_repr.your_ip, // use the offered ip
381                });
382            }
383            (ClientState::Requesting(state), DhcpMessageType::Ack) => {
384                if let Some((config, renew_at, rebind_at, expires_at)) =
385                    Self::parse_ack(cx.now(), &dhcp_repr, self.max_lease_duration, state.server)
386                {
387                    self.state = ClientState::Renewing(RenewState {
388                        config,
389                        renew_at,
390                        rebind_at,
391                        expires_at,
392                        rebinding: false,
393                    });
394                    self.config_changed();
395                }
396            }
397            (ClientState::Requesting(_), DhcpMessageType::Nak) => {
398                if !self.ignore_naks {
399                    self.reset();
400                }
401            }
402            (ClientState::Renewing(state), DhcpMessageType::Ack) => {
403                if let Some((config, renew_at, rebind_at, expires_at)) = Self::parse_ack(
404                    cx.now(),
405                    &dhcp_repr,
406                    self.max_lease_duration,
407                    state.config.server,
408                ) {
409                    state.renew_at = renew_at;
410                    state.rebind_at = rebind_at;
411                    state.rebinding = false;
412                    state.expires_at = expires_at;
413                    // The `receive_packet_buffer` field isn't populated until
414                    // the client asks for the state, but receiving any packet
415                    // will change it, so we indicate that the config has
416                    // changed every time if the receive packet buffer is set,
417                    // but we only write changes to the rest of the config now.
418                    let config_changed =
419                        state.config != config || self.receive_packet_buffer.is_some();
420                    if state.config != config {
421                        state.config = config;
422                    }
423                    if config_changed {
424                        self.config_changed();
425                    }
426                }
427            }
428            (ClientState::Renewing(_), DhcpMessageType::Nak) => {
429                if !self.ignore_naks {
430                    self.reset();
431                }
432            }
433            _ => {
434                net_debug!(
435                    "DHCP ignoring {:?}: unexpected in current state",
436                    dhcp_repr.message_type
437                );
438            }
439        }
440    }
441
442    fn parse_ack(
443        now: Instant,
444        dhcp_repr: &DhcpRepr,
445        max_lease_duration: Option<Duration>,
446        server: ServerInfo,
447    ) -> Option<(Config<'static>, Instant, Instant, Instant)> {
448        let subnet_mask = match dhcp_repr.subnet_mask {
449            Some(subnet_mask) => subnet_mask,
450            None => {
451                net_debug!("DHCP ignoring ACK because missing subnet_mask");
452                return None;
453            }
454        };
455
456        let prefix_len = match IpAddress::Ipv4(subnet_mask).prefix_len() {
457            Some(prefix_len) => prefix_len,
458            None => {
459                net_debug!("DHCP ignoring ACK because subnet_mask is not a valid mask");
460                return None;
461            }
462        };
463
464        if !dhcp_repr.your_ip.is_unicast() {
465            net_debug!("DHCP ignoring ACK because your_ip is not unicast");
466            return None;
467        }
468
469        let mut lease_duration = dhcp_repr
470            .lease_duration
471            .map(|d| Duration::from_secs(d as _))
472            .unwrap_or(DEFAULT_LEASE_DURATION);
473        if let Some(max_lease_duration) = max_lease_duration {
474            lease_duration = lease_duration.min(max_lease_duration);
475        }
476
477        // Cleanup the DNS servers list, keeping only unicasts/
478        // TP-Link TD-W8970 sends 0.0.0.0 as second DNS server if there's only one configured :(
479        let mut dns_servers = Vec::new();
480
481        dhcp_repr
482            .dns_servers
483            .iter()
484            .flatten()
485            .filter(|s| s.is_unicast())
486            .for_each(|a| {
487                // This will never produce an error, as both the arrays and `dns_servers`
488                // have length DHCP_MAX_DNS_SERVER_COUNT
489                dns_servers.push(*a).ok();
490            });
491
492        let config = Config {
493            server,
494            address: Ipv4Cidr::new(dhcp_repr.your_ip, prefix_len),
495            router: dhcp_repr.router,
496            dns_servers,
497            packet: None,
498        };
499
500        // Set renew and rebind times as per RFC 2131:
501        // Times T1 and T2 are configurable by the server through
502        // options. T1 defaults to (0.5 * duration_of_lease). T2
503        // defaults to (0.875 * duration_of_lease).
504        let (renew_duration, rebind_duration) = match (
505            dhcp_repr
506                .renew_duration
507                .map(|d| Duration::from_secs(d as u64)),
508            dhcp_repr
509                .rebind_duration
510                .map(|d| Duration::from_secs(d as u64)),
511        ) {
512            (Some(renew_duration), Some(rebind_duration)) => (renew_duration, rebind_duration),
513            (None, None) => (lease_duration / 2, lease_duration * 7 / 8),
514            // RFC 2131 does not say what to do if only one value is
515            // provided, so:
516
517            // If only T1 is provided, set T2 to be 0.75 through the gap
518            // between T1 and the duration of the lease. If T1 is set to
519            // the default (0.5 * duration_of_lease), then T2 will also
520            // be set to the default (0.875 * duration_of_lease).
521            (Some(renew_duration), None) => (
522                renew_duration,
523                renew_duration + (lease_duration - renew_duration) * 3 / 4,
524            ),
525
526            // If only T2 is provided, then T1 will be set to be
527            // whichever is smaller of the default (0.5 *
528            // duration_of_lease) or T2.
529            (None, Some(rebind_duration)) => {
530                ((lease_duration / 2).min(rebind_duration), rebind_duration)
531            }
532        };
533        let renew_at = now + renew_duration;
534        let rebind_at = now + rebind_duration;
535        let expires_at = now + lease_duration;
536
537        Some((config, renew_at, rebind_at, expires_at))
538    }
539
540    #[cfg(not(test))]
541    fn random_transaction_id(cx: &mut Context) -> u32 {
542        cx.rand().rand_u32()
543    }
544
545    #[cfg(test)]
546    fn random_transaction_id(_cx: &mut Context) -> u32 {
547        0x12345678
548    }
549
550    pub(crate) fn dispatch<F, E>(&mut self, cx: &mut Context, emit: F) -> Result<(), E>
551    where
552        F: FnOnce(&mut Context, (Ipv4Repr, UdpRepr, DhcpRepr)) -> Result<(), E>,
553    {
554        // note: Dhcpv4Socket is only usable in ethernet mediums, so the
555        // unwrap can never fail.
556        let HardwareAddress::Ethernet(ethernet_addr) = cx.hardware_addr() else {
557            panic!("using DHCPv4 socket with a non-ethernet hardware address.");
558        };
559
560        // Worst case biggest IPv4 header length.
561        // 0x0f * 4 = 60 bytes.
562        const MAX_IPV4_HEADER_LEN: usize = 60;
563
564        // We don't directly modify self.transaction_id because sending the packet
565        // may fail. We only want to update state after successfully sending.
566        let next_transaction_id = Self::random_transaction_id(cx);
567
568        let mut dhcp_repr = DhcpRepr {
569            message_type: DhcpMessageType::Discover,
570            transaction_id: next_transaction_id,
571            secs: 0,
572            client_hardware_address: ethernet_addr,
573            client_ip: Ipv4Address::UNSPECIFIED,
574            your_ip: Ipv4Address::UNSPECIFIED,
575            server_ip: Ipv4Address::UNSPECIFIED,
576            router: None,
577            subnet_mask: None,
578            relay_agent_ip: Ipv4Address::UNSPECIFIED,
579            broadcast: false,
580            requested_ip: None,
581            client_identifier: Some(ethernet_addr),
582            server_identifier: None,
583            parameter_request_list: Some(
584                self.parameter_request_list
585                    .unwrap_or(DEFAULT_PARAMETER_REQUEST_LIST),
586            ),
587            max_size: Some((cx.ip_mtu() - MAX_IPV4_HEADER_LEN - UDP_HEADER_LEN) as u16),
588            lease_duration: None,
589            renew_duration: None,
590            rebind_duration: None,
591            dns_servers: None,
592            additional_options: self.outgoing_options,
593        };
594
595        let udp_repr = UdpRepr {
596            src_port: self.client_port,
597            dst_port: self.server_port,
598        };
599
600        let mut ipv4_repr = Ipv4Repr {
601            src_addr: Ipv4Address::UNSPECIFIED,
602            dst_addr: Ipv4Address::BROADCAST,
603            next_header: IpProtocol::Udp,
604            payload_len: 0, // filled right before emit
605            hop_limit: 64,
606        };
607
608        match &mut self.state {
609            ClientState::Discovering(state) => {
610                if cx.now() < state.retry_at {
611                    return Ok(());
612                }
613
614                // send packet
615                net_debug!(
616                    "DHCP send DISCOVER to {}: {:?}",
617                    ipv4_repr.dst_addr,
618                    dhcp_repr
619                );
620                ipv4_repr.payload_len = udp_repr.header_len() + dhcp_repr.buffer_len();
621                emit(cx, (ipv4_repr, udp_repr, dhcp_repr))?;
622
623                // Update state AFTER the packet has been successfully sent.
624                state.retry_at = cx.now() + self.retry_config.discover_timeout;
625                self.transaction_id = next_transaction_id;
626                Ok(())
627            }
628            ClientState::Requesting(state) => {
629                if cx.now() < state.retry_at {
630                    return Ok(());
631                }
632
633                if state.retry >= self.retry_config.request_retries {
634                    net_debug!("DHCP request retries exceeded, restarting discovery");
635                    self.reset();
636                    return Ok(());
637                }
638
639                dhcp_repr.message_type = DhcpMessageType::Request;
640                dhcp_repr.requested_ip = Some(state.requested_ip);
641                dhcp_repr.server_identifier = Some(state.server.identifier);
642
643                net_debug!(
644                    "DHCP send request to {}: {:?}",
645                    ipv4_repr.dst_addr,
646                    dhcp_repr
647                );
648                ipv4_repr.payload_len = udp_repr.header_len() + dhcp_repr.buffer_len();
649                emit(cx, (ipv4_repr, udp_repr, dhcp_repr))?;
650
651                // Exponential backoff: Double every 2 retries.
652                state.retry_at = cx.now()
653                    + (self.retry_config.initial_request_timeout << (state.retry as u32 / 2));
654                state.retry += 1;
655
656                self.transaction_id = next_transaction_id;
657                Ok(())
658            }
659            ClientState::Renewing(state) => {
660                let now = cx.now();
661                if state.expires_at <= now {
662                    net_debug!("DHCP lease expired");
663                    self.reset();
664                    // return Ok so we get polled again
665                    return Ok(());
666                }
667
668                if now < state.renew_at || state.rebinding && now < state.rebind_at {
669                    return Ok(());
670                }
671
672                state.rebinding |= now >= state.rebind_at;
673
674                ipv4_repr.src_addr = state.config.address.address();
675                // Renewing is unicast to the original server, rebinding is broadcast
676                if !state.rebinding {
677                    ipv4_repr.dst_addr = state.config.server.address;
678                }
679                dhcp_repr.message_type = DhcpMessageType::Request;
680                dhcp_repr.client_ip = state.config.address.address();
681
682                net_debug!("DHCP send renew to {}: {:?}", ipv4_repr.dst_addr, dhcp_repr);
683                ipv4_repr.payload_len = udp_repr.header_len() + dhcp_repr.buffer_len();
684                emit(cx, (ipv4_repr, udp_repr, dhcp_repr))?;
685
686                // In both RENEWING and REBINDING states, if the client receives no
687                // response to its DHCPREQUEST message, the client SHOULD wait one-half
688                // of the remaining time until T2 (in RENEWING state) and one-half of
689                // the remaining lease time (in REBINDING state), down to a minimum of
690                // 60 seconds, before retransmitting the DHCPREQUEST message.
691                if state.rebinding {
692                    state.rebind_at = now
693                        + self
694                            .retry_config
695                            .min_renew_timeout
696                            .max((state.expires_at - now) / 2)
697                            .min(self.retry_config.max_renew_timeout);
698                } else {
699                    state.renew_at = now
700                        + self
701                            .retry_config
702                            .min_renew_timeout
703                            .max((state.rebind_at - now) / 2)
704                            .min(state.rebind_at - now)
705                            .min(self.retry_config.max_renew_timeout);
706                }
707
708                self.transaction_id = next_transaction_id;
709                Ok(())
710            }
711        }
712    }
713
714    /// Reset state and restart discovery phase.
715    ///
716    /// Use this to speed up acquisition of an address in a new
717    /// network if a link was down and it is now back up.
718    pub fn reset(&mut self) {
719        net_trace!("DHCP reset");
720        if let ClientState::Renewing(_) = &self.state {
721            self.config_changed();
722        }
723        self.state = ClientState::Discovering(DiscoverState {
724            retry_at: Instant::from_millis(0),
725        });
726    }
727
728    /// Query the socket for configuration changes.
729    ///
730    /// The socket has an internal "configuration changed" flag. If
731    /// set, this function returns the configuration and resets the flag.
732    pub fn poll(&mut self) -> Option<Event> {
733        if !self.config_changed {
734            None
735        } else if let ClientState::Renewing(state) = &self.state {
736            self.config_changed = false;
737            Some(Event::Configured(Config {
738                server: state.config.server,
739                address: state.config.address,
740                router: state.config.router,
741                dns_servers: state.config.dns_servers.clone(),
742                packet: self
743                    .receive_packet_buffer
744                    .as_deref()
745                    .map(DhcpPacket::new_unchecked),
746            }))
747        } else {
748            self.config_changed = false;
749            Some(Event::Deconfigured)
750        }
751    }
752
753    /// This function _must_ be called when the configuration provided to the
754    /// interface, by this DHCP socket, changes. It will update the `config_changed` field
755    /// so that a subsequent call to `poll` will yield an event, and wake a possible waker.
756    pub(crate) fn config_changed(&mut self) {
757        self.config_changed = true;
758        #[cfg(feature = "async")]
759        self.waker.wake();
760    }
761
762    /// Register a waker.
763    ///
764    /// The waker is woken on state changes that might affect the return value
765    /// of `poll` method calls, which indicates a new state in the DHCP configuration
766    /// provided by this DHCP socket.
767    ///
768    /// Notes:
769    ///
770    /// - Only one waker can be registered at a time. If another waker was previously registered,
771    ///   it is overwritten and will no longer be woken.
772    /// - The Waker is woken only once. Once woken, you must register it again to receive more wakes.
773    #[cfg(feature = "async")]
774    pub fn register_waker(&mut self, waker: &Waker) {
775        self.waker.register(waker)
776    }
777}
778
779#[cfg(test)]
780mod test {
781
782    use std::ops::{Deref, DerefMut};
783
784    use super::*;
785    use crate::wire::EthernetAddress;
786
787    // =========================================================================================//
788    // Helper functions
789
790    struct TestSocket {
791        socket: Socket<'static>,
792        cx: Context,
793    }
794
795    impl Deref for TestSocket {
796        type Target = Socket<'static>;
797        fn deref(&self) -> &Self::Target {
798            &self.socket
799        }
800    }
801
802    impl DerefMut for TestSocket {
803        fn deref_mut(&mut self) -> &mut Self::Target {
804            &mut self.socket
805        }
806    }
807
808    fn send(
809        s: &mut TestSocket,
810        timestamp: Instant,
811        (ip_repr, udp_repr, dhcp_repr): (Ipv4Repr, UdpRepr, DhcpRepr),
812    ) {
813        s.cx.set_now(timestamp);
814
815        net_trace!("send: {:?}", ip_repr);
816        net_trace!("      {:?}", udp_repr);
817        net_trace!("      {:?}", dhcp_repr);
818
819        let mut payload = vec![0; dhcp_repr.buffer_len()];
820        dhcp_repr
821            .emit(&mut DhcpPacket::new_unchecked(&mut payload))
822            .unwrap();
823
824        s.socket.process(&mut s.cx, &ip_repr, &udp_repr, &payload)
825    }
826
827    fn recv(s: &mut TestSocket, timestamp: Instant, reprs: &[(Ipv4Repr, UdpRepr, DhcpRepr)]) {
828        s.cx.set_now(timestamp);
829
830        let mut i = 0;
831
832        while s.socket.poll_at(&mut s.cx) <= PollAt::Time(timestamp) {
833            let _ = s
834                .socket
835                .dispatch(&mut s.cx, |_, (mut ip_repr, udp_repr, dhcp_repr)| {
836                    assert_eq!(ip_repr.next_header, IpProtocol::Udp);
837                    assert_eq!(
838                        ip_repr.payload_len,
839                        udp_repr.header_len() + dhcp_repr.buffer_len()
840                    );
841
842                    // We validated the payload len, change it to 0 to make equality testing easier
843                    ip_repr.payload_len = 0;
844
845                    net_trace!("recv: {:?}", ip_repr);
846                    net_trace!("      {:?}", udp_repr);
847                    net_trace!("      {:?}", dhcp_repr);
848
849                    let got_repr = (ip_repr, udp_repr, dhcp_repr);
850                    match reprs.get(i) {
851                        Some(want_repr) => assert_eq!(want_repr, &got_repr),
852                        None => panic!("Too many reprs emitted"),
853                    }
854                    i += 1;
855                    Ok::<_, ()>(())
856                });
857        }
858
859        assert_eq!(i, reprs.len());
860    }
861
862    macro_rules! send {
863        ($socket:ident, $repr:expr) =>
864            (send!($socket, time 0, $repr));
865        ($socket:ident, time $time:expr, $repr:expr) =>
866            (send(&mut $socket, Instant::from_millis($time), $repr));
867    }
868
869    macro_rules! recv {
870        ($socket:ident, $reprs:expr) => ({
871            recv!($socket, time 0, $reprs);
872        });
873        ($socket:ident, time $time:expr, $reprs:expr) => ({
874            recv(&mut $socket, Instant::from_millis($time), &$reprs);
875        });
876    }
877
878    // =========================================================================================//
879    // Constants
880
881    const TXID: u32 = 0x12345678;
882
883    const MY_IP: Ipv4Address = Ipv4Address([192, 168, 1, 42]);
884    const SERVER_IP: Ipv4Address = Ipv4Address([192, 168, 1, 1]);
885    const DNS_IP_1: Ipv4Address = Ipv4Address([1, 1, 1, 1]);
886    const DNS_IP_2: Ipv4Address = Ipv4Address([1, 1, 1, 2]);
887    const DNS_IP_3: Ipv4Address = Ipv4Address([1, 1, 1, 3]);
888    const DNS_IPS: &[Ipv4Address] = &[DNS_IP_1, DNS_IP_2, DNS_IP_3];
889
890    const MASK_24: Ipv4Address = Ipv4Address([255, 255, 255, 0]);
891
892    const MY_MAC: EthernetAddress = EthernetAddress([0x02, 0x02, 0x02, 0x02, 0x02, 0x02]);
893
894    const IP_BROADCAST: Ipv4Repr = Ipv4Repr {
895        src_addr: Ipv4Address::UNSPECIFIED,
896        dst_addr: Ipv4Address::BROADCAST,
897        next_header: IpProtocol::Udp,
898        payload_len: 0,
899        hop_limit: 64,
900    };
901
902    const IP_BROADCAST_ADDRESSED: Ipv4Repr = Ipv4Repr {
903        src_addr: MY_IP,
904        dst_addr: Ipv4Address::BROADCAST,
905        next_header: IpProtocol::Udp,
906        payload_len: 0,
907        hop_limit: 64,
908    };
909
910    const IP_SERVER_BROADCAST: Ipv4Repr = Ipv4Repr {
911        src_addr: SERVER_IP,
912        dst_addr: Ipv4Address::BROADCAST,
913        next_header: IpProtocol::Udp,
914        payload_len: 0,
915        hop_limit: 64,
916    };
917
918    const IP_RECV: Ipv4Repr = Ipv4Repr {
919        src_addr: SERVER_IP,
920        dst_addr: MY_IP,
921        next_header: IpProtocol::Udp,
922        payload_len: 0,
923        hop_limit: 64,
924    };
925
926    const IP_SEND: Ipv4Repr = Ipv4Repr {
927        src_addr: MY_IP,
928        dst_addr: SERVER_IP,
929        next_header: IpProtocol::Udp,
930        payload_len: 0,
931        hop_limit: 64,
932    };
933
934    const UDP_SEND: UdpRepr = UdpRepr {
935        src_port: DHCP_CLIENT_PORT,
936        dst_port: DHCP_SERVER_PORT,
937    };
938    const UDP_RECV: UdpRepr = UdpRepr {
939        src_port: DHCP_SERVER_PORT,
940        dst_port: DHCP_CLIENT_PORT,
941    };
942
943    const DIFFERENT_CLIENT_PORT: u16 = 6800;
944    const DIFFERENT_SERVER_PORT: u16 = 6700;
945
946    const UDP_SEND_DIFFERENT_PORT: UdpRepr = UdpRepr {
947        src_port: DIFFERENT_CLIENT_PORT,
948        dst_port: DIFFERENT_SERVER_PORT,
949    };
950    const UDP_RECV_DIFFERENT_PORT: UdpRepr = UdpRepr {
951        src_port: DIFFERENT_SERVER_PORT,
952        dst_port: DIFFERENT_CLIENT_PORT,
953    };
954
955    const DHCP_DEFAULT: DhcpRepr = DhcpRepr {
956        message_type: DhcpMessageType::Unknown(99),
957        transaction_id: TXID,
958        secs: 0,
959        client_hardware_address: MY_MAC,
960        client_ip: Ipv4Address::UNSPECIFIED,
961        your_ip: Ipv4Address::UNSPECIFIED,
962        server_ip: Ipv4Address::UNSPECIFIED,
963        router: None,
964        subnet_mask: None,
965        relay_agent_ip: Ipv4Address::UNSPECIFIED,
966        broadcast: false,
967        requested_ip: None,
968        client_identifier: None,
969        server_identifier: None,
970        parameter_request_list: None,
971        dns_servers: None,
972        max_size: None,
973        renew_duration: None,
974        rebind_duration: None,
975        lease_duration: None,
976        additional_options: &[],
977    };
978
979    const DHCP_DISCOVER: DhcpRepr = DhcpRepr {
980        message_type: DhcpMessageType::Discover,
981        client_identifier: Some(MY_MAC),
982        parameter_request_list: Some(&[1, 3, 6]),
983        max_size: Some(1432),
984        ..DHCP_DEFAULT
985    };
986
987    fn dhcp_offer() -> DhcpRepr<'static> {
988        DhcpRepr {
989            message_type: DhcpMessageType::Offer,
990            server_ip: SERVER_IP,
991            server_identifier: Some(SERVER_IP),
992
993            your_ip: MY_IP,
994            router: Some(SERVER_IP),
995            subnet_mask: Some(MASK_24),
996            dns_servers: Some(Vec::from_slice(DNS_IPS).unwrap()),
997            lease_duration: Some(1000),
998
999            ..DHCP_DEFAULT
1000        }
1001    }
1002
1003    const DHCP_REQUEST: DhcpRepr = DhcpRepr {
1004        message_type: DhcpMessageType::Request,
1005        client_identifier: Some(MY_MAC),
1006        server_identifier: Some(SERVER_IP),
1007        max_size: Some(1432),
1008
1009        requested_ip: Some(MY_IP),
1010        parameter_request_list: Some(&[1, 3, 6]),
1011        ..DHCP_DEFAULT
1012    };
1013
1014    fn dhcp_ack() -> DhcpRepr<'static> {
1015        DhcpRepr {
1016            message_type: DhcpMessageType::Ack,
1017            server_ip: SERVER_IP,
1018            server_identifier: Some(SERVER_IP),
1019
1020            your_ip: MY_IP,
1021            router: Some(SERVER_IP),
1022            subnet_mask: Some(MASK_24),
1023            dns_servers: Some(Vec::from_slice(DNS_IPS).unwrap()),
1024            lease_duration: Some(1000),
1025
1026            ..DHCP_DEFAULT
1027        }
1028    }
1029
1030    const DHCP_NAK: DhcpRepr = DhcpRepr {
1031        message_type: DhcpMessageType::Nak,
1032        server_ip: SERVER_IP,
1033        server_identifier: Some(SERVER_IP),
1034        ..DHCP_DEFAULT
1035    };
1036
1037    const DHCP_RENEW: DhcpRepr = DhcpRepr {
1038        message_type: DhcpMessageType::Request,
1039        client_identifier: Some(MY_MAC),
1040        // NO server_identifier in renew requests, only in first one!
1041        client_ip: MY_IP,
1042        max_size: Some(1432),
1043
1044        requested_ip: None,
1045        parameter_request_list: Some(&[1, 3, 6]),
1046        ..DHCP_DEFAULT
1047    };
1048
1049    const DHCP_REBIND: DhcpRepr = DhcpRepr {
1050        message_type: DhcpMessageType::Request,
1051        client_identifier: Some(MY_MAC),
1052        // NO server_identifier in renew requests, only in first one!
1053        client_ip: MY_IP,
1054        max_size: Some(1432),
1055
1056        requested_ip: None,
1057        parameter_request_list: Some(&[1, 3, 6]),
1058        ..DHCP_DEFAULT
1059    };
1060
1061    // =========================================================================================//
1062    // Tests
1063
1064    use crate::phy::Medium;
1065    use crate::tests::setup;
1066    use rstest::*;
1067
1068    fn socket(medium: Medium) -> TestSocket {
1069        let (iface, _, _) = setup(medium);
1070        let mut s = Socket::new();
1071        assert_eq!(s.poll(), Some(Event::Deconfigured));
1072        TestSocket {
1073            socket: s,
1074            cx: iface.inner,
1075        }
1076    }
1077
1078    fn socket_different_port(medium: Medium) -> TestSocket {
1079        let (iface, _, _) = setup(medium);
1080        let mut s = Socket::new();
1081        s.set_ports(DIFFERENT_SERVER_PORT, DIFFERENT_CLIENT_PORT);
1082
1083        assert_eq!(s.poll(), Some(Event::Deconfigured));
1084        TestSocket {
1085            socket: s,
1086            cx: iface.inner,
1087        }
1088    }
1089
1090    fn socket_bound(medium: Medium) -> TestSocket {
1091        let mut s = socket(medium);
1092        s.state = ClientState::Renewing(RenewState {
1093            config: Config {
1094                server: ServerInfo {
1095                    address: SERVER_IP,
1096                    identifier: SERVER_IP,
1097                },
1098                address: Ipv4Cidr::new(MY_IP, 24),
1099                dns_servers: Vec::from_slice(DNS_IPS).unwrap(),
1100                router: Some(SERVER_IP),
1101                packet: None,
1102            },
1103            renew_at: Instant::from_secs(500),
1104            rebind_at: Instant::from_secs(875),
1105            rebinding: false,
1106            expires_at: Instant::from_secs(1000),
1107        });
1108
1109        s
1110    }
1111
1112    #[rstest]
1113    #[case::ip(Medium::Ethernet)]
1114    #[cfg(feature = "medium-ethernet")]
1115    fn test_bind(#[case] medium: Medium) {
1116        let mut s = socket(medium);
1117
1118        recv!(s, [(IP_BROADCAST, UDP_SEND, DHCP_DISCOVER)]);
1119        assert_eq!(s.poll(), None);
1120        send!(s, (IP_RECV, UDP_RECV, dhcp_offer()));
1121        assert_eq!(s.poll(), None);
1122        recv!(s, [(IP_BROADCAST, UDP_SEND, DHCP_REQUEST)]);
1123        assert_eq!(s.poll(), None);
1124        send!(s, (IP_RECV, UDP_RECV, dhcp_ack()));
1125
1126        assert_eq!(
1127            s.poll(),
1128            Some(Event::Configured(Config {
1129                server: ServerInfo {
1130                    address: SERVER_IP,
1131                    identifier: SERVER_IP,
1132                },
1133                address: Ipv4Cidr::new(MY_IP, 24),
1134                dns_servers: Vec::from_slice(DNS_IPS).unwrap(),
1135                router: Some(SERVER_IP),
1136                packet: None,
1137            }))
1138        );
1139
1140        match &s.state {
1141            ClientState::Renewing(r) => {
1142                assert_eq!(r.renew_at, Instant::from_secs(500));
1143                assert_eq!(r.rebind_at, Instant::from_secs(875));
1144                assert_eq!(r.expires_at, Instant::from_secs(1000));
1145            }
1146            _ => panic!("Invalid state"),
1147        }
1148    }
1149
1150    #[rstest]
1151    #[case::ip(Medium::Ethernet)]
1152    #[cfg(feature = "medium-ethernet")]
1153    fn test_bind_different_ports(#[case] medium: Medium) {
1154        let mut s = socket_different_port(medium);
1155
1156        recv!(s, [(IP_BROADCAST, UDP_SEND_DIFFERENT_PORT, DHCP_DISCOVER)]);
1157        assert_eq!(s.poll(), None);
1158        send!(s, (IP_RECV, UDP_RECV_DIFFERENT_PORT, dhcp_offer()));
1159        assert_eq!(s.poll(), None);
1160        recv!(s, [(IP_BROADCAST, UDP_SEND_DIFFERENT_PORT, DHCP_REQUEST)]);
1161        assert_eq!(s.poll(), None);
1162        send!(s, (IP_RECV, UDP_RECV_DIFFERENT_PORT, dhcp_ack()));
1163
1164        assert_eq!(
1165            s.poll(),
1166            Some(Event::Configured(Config {
1167                server: ServerInfo {
1168                    address: SERVER_IP,
1169                    identifier: SERVER_IP,
1170                },
1171                address: Ipv4Cidr::new(MY_IP, 24),
1172                dns_servers: Vec::from_slice(DNS_IPS).unwrap(),
1173                router: Some(SERVER_IP),
1174                packet: None,
1175            }))
1176        );
1177
1178        match &s.state {
1179            ClientState::Renewing(r) => {
1180                assert_eq!(r.renew_at, Instant::from_secs(500));
1181                assert_eq!(r.rebind_at, Instant::from_secs(875));
1182                assert_eq!(r.expires_at, Instant::from_secs(1000));
1183            }
1184            _ => panic!("Invalid state"),
1185        }
1186    }
1187
1188    #[rstest]
1189    #[case::ip(Medium::Ethernet)]
1190    #[cfg(feature = "medium-ethernet")]
1191    fn test_discover_retransmit(#[case] medium: Medium) {
1192        let mut s = socket(medium);
1193
1194        recv!(s, time 0, [(IP_BROADCAST, UDP_SEND, DHCP_DISCOVER)]);
1195        recv!(s, time 1_000, []);
1196        recv!(s, time 10_000, [(IP_BROADCAST, UDP_SEND, DHCP_DISCOVER)]);
1197        recv!(s, time 11_000, []);
1198        recv!(s, time 20_000, [(IP_BROADCAST, UDP_SEND, DHCP_DISCOVER)]);
1199
1200        // check after retransmits it still works
1201        send!(s, time 20_000, (IP_RECV, UDP_RECV, dhcp_offer()));
1202        recv!(s, time 20_000, [(IP_BROADCAST, UDP_SEND, DHCP_REQUEST)]);
1203    }
1204
1205    #[rstest]
1206    #[case::ip(Medium::Ethernet)]
1207    #[cfg(feature = "medium-ethernet")]
1208    fn test_request_retransmit(#[case] medium: Medium) {
1209        let mut s = socket(medium);
1210
1211        recv!(s, time 0, [(IP_BROADCAST, UDP_SEND, DHCP_DISCOVER)]);
1212        send!(s, time 0, (IP_RECV, UDP_RECV, dhcp_offer()));
1213        recv!(s, time 0, [(IP_BROADCAST, UDP_SEND, DHCP_REQUEST)]);
1214        recv!(s, time 1_000, []);
1215        recv!(s, time 5_000, [(IP_BROADCAST, UDP_SEND, DHCP_REQUEST)]);
1216        recv!(s, time 6_000, []);
1217        recv!(s, time 10_000, [(IP_BROADCAST, UDP_SEND, DHCP_REQUEST)]);
1218        recv!(s, time 15_000, []);
1219        recv!(s, time 20_000, [(IP_BROADCAST, UDP_SEND, DHCP_REQUEST)]);
1220
1221        // check after retransmits it still works
1222        send!(s, time 20_000, (IP_RECV, UDP_RECV, dhcp_ack()));
1223
1224        match &s.state {
1225            ClientState::Renewing(r) => {
1226                assert_eq!(r.renew_at, Instant::from_secs(20 + 500));
1227                assert_eq!(r.expires_at, Instant::from_secs(20 + 1000));
1228            }
1229            _ => panic!("Invalid state"),
1230        }
1231    }
1232
1233    #[rstest]
1234    #[case::ip(Medium::Ethernet)]
1235    #[cfg(feature = "medium-ethernet")]
1236    fn test_request_timeout(#[case] medium: Medium) {
1237        let mut s = socket(medium);
1238
1239        recv!(s, time 0, [(IP_BROADCAST, UDP_SEND, DHCP_DISCOVER)]);
1240        send!(s, time 0, (IP_RECV, UDP_RECV, dhcp_offer()));
1241        recv!(s, time 0, [(IP_BROADCAST, UDP_SEND, DHCP_REQUEST)]);
1242        recv!(s, time 5_000, [(IP_BROADCAST, UDP_SEND, DHCP_REQUEST)]);
1243        recv!(s, time 10_000, [(IP_BROADCAST, UDP_SEND, DHCP_REQUEST)]);
1244        recv!(s, time 20_000, [(IP_BROADCAST, UDP_SEND, DHCP_REQUEST)]);
1245        recv!(s, time 30_000, [(IP_BROADCAST, UDP_SEND, DHCP_REQUEST)]);
1246
1247        // After 5 tries and 70 seconds, it gives up.
1248        // 5 + 5 + 10 + 10 + 20 = 70
1249        recv!(s, time 70_000, [(IP_BROADCAST, UDP_SEND, DHCP_DISCOVER)]);
1250
1251        // check it still works
1252        send!(s, time 60_000, (IP_RECV, UDP_RECV, dhcp_offer()));
1253        recv!(s, time 60_000, [(IP_BROADCAST, UDP_SEND, DHCP_REQUEST)]);
1254    }
1255
1256    #[rstest]
1257    #[case::ip(Medium::Ethernet)]
1258    #[cfg(feature = "medium-ethernet")]
1259    fn test_request_nak(#[case] medium: Medium) {
1260        let mut s = socket(medium);
1261
1262        recv!(s, time 0, [(IP_BROADCAST, UDP_SEND, DHCP_DISCOVER)]);
1263        send!(s, time 0, (IP_RECV, UDP_RECV, dhcp_offer()));
1264        recv!(s, time 0, [(IP_BROADCAST, UDP_SEND, DHCP_REQUEST)]);
1265        send!(s, time 0, (IP_SERVER_BROADCAST, UDP_RECV, DHCP_NAK));
1266        recv!(s, time 0, [(IP_BROADCAST, UDP_SEND, DHCP_DISCOVER)]);
1267    }
1268
1269    #[rstest]
1270    #[case::ip(Medium::Ethernet)]
1271    #[cfg(feature = "medium-ethernet")]
1272    fn test_renew(#[case] medium: Medium) {
1273        let mut s = socket_bound(medium);
1274
1275        recv!(s, []);
1276        assert_eq!(s.poll(), None);
1277        recv!(s, time 500_000, [(IP_SEND, UDP_SEND, DHCP_RENEW)]);
1278        assert_eq!(s.poll(), None);
1279
1280        match &s.state {
1281            ClientState::Renewing(r) => {
1282                // the expiration still hasn't been bumped, because
1283                // we haven't received the ACK yet
1284                assert_eq!(r.expires_at, Instant::from_secs(1000));
1285            }
1286            _ => panic!("Invalid state"),
1287        }
1288
1289        send!(s, time 500_000, (IP_RECV, UDP_RECV, dhcp_ack()));
1290        assert_eq!(s.poll(), None);
1291
1292        match &s.state {
1293            ClientState::Renewing(r) => {
1294                // NOW the expiration gets bumped
1295                assert_eq!(r.renew_at, Instant::from_secs(500 + 500));
1296                assert_eq!(r.expires_at, Instant::from_secs(500 + 1000));
1297            }
1298            _ => panic!("Invalid state"),
1299        }
1300    }
1301
1302    #[rstest]
1303    #[case::ip(Medium::Ethernet)]
1304    #[cfg(feature = "medium-ethernet")]
1305    fn test_renew_rebind_retransmit(#[case] medium: Medium) {
1306        let mut s = socket_bound(medium);
1307
1308        recv!(s, []);
1309        // First renew attempt at T1
1310        recv!(s, time 499_000, []);
1311        recv!(s, time 500_000, [(IP_SEND, UDP_SEND, DHCP_RENEW)]);
1312        // Next renew attempt at half way to T2
1313        recv!(s, time 687_000, []);
1314        recv!(s, time 687_500, [(IP_SEND, UDP_SEND, DHCP_RENEW)]);
1315        // Next renew attempt at half way again to T2
1316        recv!(s, time 781_000, []);
1317        recv!(s, time 781_250, [(IP_SEND, UDP_SEND, DHCP_RENEW)]);
1318        // Next renew attempt 60s later (minimum interval)
1319        recv!(s, time 841_000, []);
1320        recv!(s, time 841_250, [(IP_SEND, UDP_SEND, DHCP_RENEW)]);
1321        // No more renews due to minimum interval
1322        recv!(s, time 874_000, []);
1323        // First rebind attempt
1324        recv!(s, time 875_000, [(IP_BROADCAST_ADDRESSED, UDP_SEND, DHCP_REBIND)]);
1325        // Next rebind attempt half way to expiry
1326        recv!(s, time 937_000, []);
1327        recv!(s, time 937_500, [(IP_BROADCAST_ADDRESSED, UDP_SEND, DHCP_REBIND)]);
1328        // Next rebind attempt 60s later (minimum interval)
1329        recv!(s, time 997_000, []);
1330        recv!(s, time 997_500, [(IP_BROADCAST_ADDRESSED, UDP_SEND, DHCP_REBIND)]);
1331
1332        // check it still works
1333        send!(s, time 999_000, (IP_RECV, UDP_RECV, dhcp_ack()));
1334        match &s.state {
1335            ClientState::Renewing(r) => {
1336                // NOW the expiration gets bumped
1337                assert_eq!(r.renew_at, Instant::from_secs(999 + 500));
1338                assert_eq!(r.expires_at, Instant::from_secs(999 + 1000));
1339            }
1340            _ => panic!("Invalid state"),
1341        }
1342    }
1343
1344    #[rstest]
1345    #[case::ip(Medium::Ethernet)]
1346    #[cfg(feature = "medium-ethernet")]
1347    fn test_renew_rebind_timeout(#[case] medium: Medium) {
1348        let mut s = socket_bound(medium);
1349
1350        recv!(s, []);
1351        // First renew attempt at T1
1352        recv!(s, time 500_000, [(IP_SEND, UDP_SEND, DHCP_RENEW)]);
1353        // Next renew attempt at half way to T2
1354        recv!(s, time 687_500, [(IP_SEND, UDP_SEND, DHCP_RENEW)]);
1355        // Next renew attempt at half way again to T2
1356        recv!(s, time 781_250, [(IP_SEND, UDP_SEND, DHCP_RENEW)]);
1357        // Next renew attempt 60s later (minimum interval)
1358        recv!(s, time 841_250, [(IP_SEND, UDP_SEND, DHCP_RENEW)]);
1359        // TODO uncomment below part of test
1360        // // First rebind attempt
1361        // recv!(s, time 875_000, [(IP_BROADCAST_ADDRESSED, UDP_SEND, DHCP_REBIND)]);
1362        // // Next rebind attempt half way to expiry
1363        // recv!(s, time 937_500, [(IP_BROADCAST_ADDRESSED, UDP_SEND, DHCP_REBIND)]);
1364        // // Next rebind attempt 60s later (minimum interval)
1365        // recv!(s, time 997_500, [(IP_BROADCAST_ADDRESSED, UDP_SEND, DHCP_REBIND)]);
1366        // No more rebinds due to minimum interval
1367        recv!(s, time 1_000_000, [(IP_BROADCAST, UDP_SEND, DHCP_DISCOVER)]);
1368        match &s.state {
1369            ClientState::Discovering(_) => {}
1370            _ => panic!("Invalid state"),
1371        }
1372    }
1373
1374    #[rstest]
1375    #[case::ip(Medium::Ethernet)]
1376    #[cfg(feature = "medium-ethernet")]
1377    fn test_min_max_renew_timeout(#[case] medium: Medium) {
1378        let mut s = socket_bound(medium);
1379        // Set a minimum of 45s and a maximum of 120s
1380        let config = RetryConfig {
1381            max_renew_timeout: Duration::from_secs(120),
1382            min_renew_timeout: Duration::from_secs(45),
1383            ..s.get_retry_config()
1384        };
1385        s.set_retry_config(config);
1386        recv!(s, []);
1387        // First renew attempt at T1
1388        recv!(s, time 499_999, []);
1389        recv!(s, time 500_000, [(IP_SEND, UDP_SEND, DHCP_RENEW)]);
1390        // Next renew attempt 120s after T1 because we hit the max
1391        recv!(s, time 619_999, []);
1392        recv!(s, time 620_000, [(IP_SEND, UDP_SEND, DHCP_RENEW)]);
1393        // Next renew attempt 120s after previous because we hit the max again
1394        recv!(s, time 739_999, []);
1395        recv!(s, time 740_000, [(IP_SEND, UDP_SEND, DHCP_RENEW)]);
1396        // Next renew attempt half way to T2
1397        recv!(s, time 807_499, []);
1398        recv!(s, time 807_500, [(IP_SEND, UDP_SEND, DHCP_RENEW)]);
1399        // Next renew attempt 45s after previous because we hit the min
1400        recv!(s, time 852_499, []);
1401        recv!(s, time 852_500, [(IP_SEND, UDP_SEND, DHCP_RENEW)]);
1402        // Next is a rebind, because the min puts us after T2
1403        recv!(s, time 874_999, []);
1404        recv!(s, time 875_000, [(IP_BROADCAST_ADDRESSED, UDP_SEND, DHCP_REBIND)]);
1405    }
1406
1407    #[rstest]
1408    #[case::ip(Medium::Ethernet)]
1409    #[cfg(feature = "medium-ethernet")]
1410    fn test_renew_nak(#[case] medium: Medium) {
1411        let mut s = socket_bound(medium);
1412
1413        recv!(s, time 500_000, [(IP_SEND, UDP_SEND, DHCP_RENEW)]);
1414        send!(s, time 500_000, (IP_SERVER_BROADCAST, UDP_RECV, DHCP_NAK));
1415        recv!(s, time 500_000, [(IP_BROADCAST, UDP_SEND, DHCP_DISCOVER)]);
1416    }
1417}