Skip to content

Commit 74bd1e5

Browse files
authored
Merge pull request #1029 from thvdveld/fix-dhcpv4-panic
Fix DHCPv4 panic when T1 < T2 < lease duration is not respected
2 parents 3a30d0c + e77d02b commit 74bd1e5

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/socket/dhcpv4.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,8 @@ impl<'a> Socket<'a> {
502502
// Times T1 and T2 are configurable by the server through
503503
// options. T1 defaults to (0.5 * duration_of_lease). T2
504504
// defaults to (0.875 * duration_of_lease).
505+
// When receiving T1 and T2, they must be in the order:
506+
// T1 < T2 < lease_duration
505507
let (renew_duration, rebind_duration) = match (
506508
dhcp_repr
507509
.renew_duration
@@ -510,26 +512,36 @@ impl<'a> Socket<'a> {
510512
.rebind_duration
511513
.map(|d| Duration::from_secs(d as u64)),
512514
) {
513-
(Some(renew_duration), Some(rebind_duration)) => (renew_duration, rebind_duration),
514-
(None, None) => (lease_duration / 2, lease_duration * 7 / 8),
515+
(Some(renew_duration), Some(rebind_duration))
516+
if renew_duration < rebind_duration && rebind_duration < lease_duration =>
517+
{
518+
(renew_duration, rebind_duration)
519+
}
515520
// RFC 2131 does not say what to do if only one value is
516521
// provided, so:
517522

518523
// If only T1 is provided, set T2 to be 0.75 through the gap
519524
// between T1 and the duration of the lease. If T1 is set to
520525
// the default (0.5 * duration_of_lease), then T2 will also
521526
// be set to the default (0.875 * duration_of_lease).
522-
(Some(renew_duration), None) => (
527+
(Some(renew_duration), None) if renew_duration < lease_duration => (
523528
renew_duration,
524529
renew_duration + (lease_duration - renew_duration) * 3 / 4,
525530
),
526531

527532
// If only T2 is provided, then T1 will be set to be
528533
// whichever is smaller of the default (0.5 *
529534
// duration_of_lease) or T2.
530-
(None, Some(rebind_duration)) => {
535+
(None, Some(rebind_duration)) if rebind_duration < lease_duration => {
531536
((lease_duration / 2).min(rebind_duration), rebind_duration)
532537
}
538+
539+
// Use the defaults if the following order is not met:
540+
// T1 < T2 < lease_duration
541+
(_, _) => {
542+
net_debug!("using default T1 and T2 values since the provided values are invalid");
543+
(lease_duration / 2, lease_duration * 7 / 8)
544+
}
533545
};
534546
let renew_at = now + renew_duration;
535547
let rebind_at = now + rebind_duration;

0 commit comments

Comments
 (0)