Skip to content

Fix DHCP NTP handoff from NetworkManager to timesyncd - #4351

Open
kevinwieland wants to merge 2 commits into
openWB:trixie-nextfrom
kevinwieland:ntp-fix
Open

kevinwieland wants to merge 2 commits into
openWB:trixie-nextfrom
kevinwieland:ntp-fix

Conversation

@kevinwieland

@kevinwieland kevinwieland commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

NetworkManager receives NTP servers via DHCPv4 option 42 but does not automatically pass them to systemd-timesyncd. This change installs the missing integration during openWB startup.

  • Run a dedicated timesync setup step before the existing network/internet check, without blocking startup on failure.
  • Install a root-owned NetworkManager dispatcher and immediately import existing DHCP leases without reconnecting the network.
  • Refresh NTP servers on connection up/down and DHCPv4 lease changes, combining connected interfaces and validating/deduplicating IPv4 addresses.
  • Maintain only /run/systemd/timesyncd.conf.d/60-openwb-dhcp.conf, leaving existing static NTP settings untouched.
  • Restart timesyncd only when configuration changes; retry failed restarts on the next relevant event and serialize concurrent updates.

The integration is only installed when both NetworkManager and systemd-timesyncd are already active. No packages are installed or services enabled. DHCPv6 is outside this change's scope.

Validation

  • Bash syntax checks passed for all three scripts.
  • git diff --check passed.
  • 18 isolated dispatcher tests passed during local preparation; test files and additional documentation are not included in this PR.
  • Not yet integration-tested on an openWB device.

Custom startup scripts that bypass runs/atreboot.sh must invoke runs/setup_timesync.sh separately; external startup scripts and systemd drop-ins are not modified.

Summary by Sourcery

Integrate NetworkManager DHCP NTP servers with systemd-timesyncd during openWB startup without changing systems that do not use both services.

New Features:

  • Add DHCP-provided NTP synchronization between NetworkManager and systemd-timesyncd.
  • Refresh timesyncd configuration from active DHCP connections and existing leases while preserving static NTP settings.

Bug Fixes:

  • Ensure DHCP NTP servers are handed off during startup even when internet connectivity is unavailable.

Enhancements:

  • Install and maintain a root-owned NetworkManager dispatcher that updates timesyncd only when needed and tolerates setup failures without blocking startup.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 4 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="runs/setup_timesync.sh" line_range="22" />
<code_context>
+
+install -d -o root -g root -m 0755 /etc/NetworkManager/dispatcher.d
+if ! cmp -s "$source_file" "$dispatcher"; then
+	install -o root -g root -m 0755 "$source_file" "$dispatcher"
+	echo "installed NetworkManager DHCP NTP dispatcher"
+fi
</code_context>
<issue_to_address>
**Rogue DHCP can control system time**

When a rogue DHCP server supplies option 42 on a connected interface, the dispatcher accepts any syntactically valid DHCP NTP address, writes it as the active timesyncd server, and restarts timesyncd; the server can then skew the host clock and disrupt time-dependent security checks.

Restrict importing DHCP NTP servers to explicitly trusted NetworkManager connections, or otherwise require a trusted/authenticated time source.
</issue_to_address>

### Comment 2
<location path="runs/setup_timesync.sh" line_range="22" />
<code_context>
+
+install -d -o root -g root -m 0755 /etc/NetworkManager/dispatcher.d
+if ! cmp -s "$source_file" "$dispatcher"; then
+	install -o root -g root -m 0755 "$source_file" "$dispatcher"
+	echo "installed NetworkManager DHCP NTP dispatcher"
+fi
</code_context>
<issue_to_address>
**DHCP servers are discarded**

When a connected interface has an NTP server in its DHCPv4 lease, the dispatcher’s `awk` parser uses `.` as a regular-expression separator, so it splits each IPv4 address at every character instead of at literal dots. No address passes the four-octet check, leaving timesyncd without the DHCP-provided servers.

Use a literal-dot separator such as `[.]` when splitting the address into octets.
</issue_to_address>

### Comment 3
<location path="runs/setup_timesync.sh" line_range="11-12" />
<code_context>
+fi
+
+# Do not change systems using dhcpcd, networkd or another time service.
+if ! command -v nmcli >/dev/null || ! systemctl is-active --quiet NetworkManager.service ||
+	! systemctl is-active --quiet systemd-timesyncd.service; then
+	echo "NetworkManager/timesyncd not active, skipping DHCP NTP integration"
+	exit 0
</code_context>
<issue_to_address>
**DHCP NTP handoff stays absent**

When openWB startup runs before NetworkManager or systemd-timesyncd becomes active, `setup_timesync.sh` exits successfully without installing the dispatcher, and startup does not retry after the services become active, so DHCP NTP servers are not handed to timesyncd for the rest of that boot.

Arrange for setup to run after both services are active or retry it when they become active.

Also at `runs/atreboot.sh:100-101`.
</issue_to_address>

### Comment 4
<location path="runs/setup_timesync.sh" line_range="30" />
<code_context>
+chmod 0755 "$dispatcher"
+
+# The connection may already be up before the dispatcher is installed.
+"$dispatcher" none refresh
</code_context>
<issue_to_address>
**Some DHCP time servers are discarded**

When a DHCP NTP option contains multiple addresses rendered with comma separators, `awk` splits the option value on whitespace and passes each token directly to `split(..., ".")`; when NetworkManager renders multiple servers comma-separated, the comma makes the preceding address fail validation, so that server is omitted from timesyncd’s configuration.

Normalize the NetworkManager list separators before validating each address.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread runs/setup_timesync.sh Resolved
Comment thread runs/setup_timesync.sh Resolved
Comment thread runs/setup_timesync.sh Resolved
Comment thread runs/setup_timesync.sh Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant