Skip to content

all: implement peer config change detection#66

Open
systemcrash wants to merge 1 commit intoopenwrt:masterfrom
systemcrash:detect_peer_changes
Open

all: implement peer config change detection#66
systemcrash wants to merge 1 commit intoopenwrt:masterfrom
systemcrash:detect_peer_changes

Conversation

@systemcrash
Copy link
Contributor

@systemcrash systemcrash commented Jan 30, 2026

all: implement peer config change detection

netifd detects when an interface config changes by
declaring various parameters the interface has. But if
an interface has separate config sections for peer configs,
netifd is blind to those changes. This requires a manual
call to renew, whereby proto_x_renew() accounts for
peer changes.

This change allows netifd to see when peer entries change.
The proto script shall define:

peer_detect=1

For example, defining this for WireGuard:

proto_wireguard_init_config() {
	...
	peer_detect=1

	...
}

is enough for netifd to detect when any wireguard_wgX peer
entries change, avoid disruptive network restarts, and simply
renew.

netifd now hashes interface peer entries prefixed
<proto>_<interface>, and if its config entries change, netifd
issues a renew or restart. Example: for WireGuard interface
foo, WireGuard entries config wireguard_foo are hashed.
For Yggdrasil interface test, Yggdrasil entries
config yggdrasil_test_peer are hashed. Each interface
requires an additional 8 bytes for the 64 bit hash.

The hash is not chosen for security or integrity,
but to detect change. It's low complexity, quick and requires
no extra .h or lib.

https://datatracker.ietf.org/doc/draft-eastlake-fnv/

We add also the peer_detect property to get_proto_handlers:

"wireguard": {
	"validate": {
		"private_key": "string",
		"listen_port": "uinteger",
		"mtu": "uinteger",
		"fwmark": "string",
		"addresses": "string"
	},
	"immediate": false,
	"no_device": false,
	"init_available": true,
	"renew_available": true,
	"peer_detect": true,
	"force_link_default": false,
	"last_error": false,
	"teardown_on_l3_link_down": false,
	"no_task": true
},...

ping @nbd168 who is familiar with netifd for an opinion.

Perhaps also @jow- and @hauke

systemcrash added a commit to systemcrash/openwrt that referenced this pull request Jan 31, 2026
- add a renew handler
- add a peer detect handler

( benefits from openwrt/netifd#66 )

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
@systemcrash systemcrash force-pushed the detect_peer_changes branch 2 times, most recently from 7ef2079 to 2eceb4e Compare January 31, 2026 16:08
robimarko pushed a commit to systemcrash/openwrt that referenced this pull request Feb 2, 2026
- add a renew handler
- add a peer detect handler

( benefits from openwrt/netifd#66 )

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
systemcrash added a commit to systemcrash/openwrt that referenced this pull request Feb 2, 2026
Proto handler now also detects changes to
- addresses

Tighten also assign address portion

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit d59b360)

wireguard-tools: add protocol renew handler

- add a renew handler
- add a peer detect handler

( benefits from openwrt/netifd#66 )

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 400742a)

wireguard-tools: avoid temp file for key gen

- no longer write any temporary file for key gen

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 9778987)

wireguard-tools: avoid temp file for peer generation

- no longer write any temporary file for peer gen
- use wg syncconf to update active interfaces (not setconf)

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 1482077)

wireguard-tools: enable reload without teardown

- preserve (active) interface (at reload)

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 0a576de)
systemcrash added a commit to systemcrash/openwrt that referenced this pull request Feb 2, 2026
Proto handler now also detects changes to
- addresses

Tighten also assign address portion

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit d59b360)

wireguard-tools: add protocol renew handler

- add a renew handler
- add a peer detect handler

( benefits from openwrt/netifd#66 )

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 400742a)

wireguard-tools: avoid temp file for key gen

- no longer write any temporary file for key gen

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 9778987)

wireguard-tools: avoid temp file for peer generation

- no longer write any temporary file for peer gen
- use wg syncconf to update active interfaces (not setconf)

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 1482077)

wireguard-tools: enable reload without teardown

- preserve (active) interface (at reload)

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 0a576de)
systemcrash added a commit to systemcrash/openwrt that referenced this pull request Feb 3, 2026
Proto handler now also detects changes to
- addresses

Tighten also assign address portion

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit d59b360)

wireguard-tools: add protocol renew handler

- add a renew handler
- add a peer detect handler

( benefits from openwrt/netifd#66 )

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 400742a)

wireguard-tools: avoid temp file for key gen

- no longer write any temporary file for key gen

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 9778987)

wireguard-tools: avoid temp file for peer generation

- no longer write any temporary file for peer gen
- use wg syncconf to update active interfaces (not setconf)

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 1482077)

wireguard-tools: enable reload without teardown

- preserve (active) interface (at reload)

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 0a576de)

wireguard-tools: fix script errors

follow-up to 1482077

Schoolboy error on the peer_psk value.

Also fix an issue when joining peer IPv4 and IPv6 AllowedIPs
(${peer_a_ips/ /, } replaces only the first space, while
${peer_a_ips// /, } replaces all the spaces).

Closes: openwrt#21847
Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21851
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 3f0de6a)
systemcrash added a commit to systemcrash/openwrt that referenced this pull request Feb 3, 2026
Proto handler now also detects changes to
- addresses

Tighten also assign address portion

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit d59b360)

wireguard-tools: add protocol renew handler

- add a renew handler
- add a peer detect handler

( benefits from openwrt/netifd#66 )

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 400742a)

wireguard-tools: avoid temp file for key gen

- no longer write any temporary file for key gen

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 9778987)

wireguard-tools: avoid temp file for peer generation

- no longer write any temporary file for peer gen
- use wg syncconf to update active interfaces (not setconf)

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 1482077)

wireguard-tools: enable reload without teardown

- preserve (active) interface (at reload)

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 0a576de)

wireguard-tools: fix script errors

follow-up to 1482077

Schoolboy error on the peer_psk value.

Also fix an issue when joining peer IPv4 and IPv6 AllowedIPs
(${peer_a_ips/ /, } replaces only the first space, while
${peer_a_ips// /, } replaces all the spaces).

Closes: openwrt#21847
Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21851
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 3f0de6a)
@systemcrash
Copy link
Contributor Author

@efahl - don't know whether you run WireGuard anywhere, but could I trouble you for a SoB line with this if you do? Or even if you don't that you've run it in place and nothing blew up :D

MattzFiber pushed a commit to MattzFiber/openwrt that referenced this pull request Feb 3, 2026
- add a renew handler
- add a peer detect handler

( benefits from openwrt/netifd#66 )

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
systemcrash added a commit to systemcrash/openwrt that referenced this pull request Feb 4, 2026
- add a renew handler
- add a peer detect handler

( benefits from openwrt/netifd#66 )

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 400742a)
systemcrash added a commit to systemcrash/openwrt that referenced this pull request Feb 4, 2026
- add a renew handler
- add a peer detect handler

( benefits from openwrt/netifd#66 )

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 400742a)
pesa1234 pushed a commit to pesa1234/openwrt that referenced this pull request Feb 5, 2026
- add a renew handler
- add a peer detect handler

( benefits from openwrt/netifd#66 )

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
hauke pushed a commit to systemcrash/openwrt that referenced this pull request Feb 8, 2026
- add a renew handler
- add a peer detect handler

( benefits from openwrt/netifd#66 )

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
(cherry picked from commit 400742a)
Link: openwrt#21840
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
stavultras pushed a commit to stavultras/openwrt that referenced this pull request Feb 9, 2026
- add a renew handler
- add a peer detect handler

( benefits from openwrt/netifd#66 )

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: openwrt#21784
Signed-off-by: Robert Marko <robimarko@gmail.com>
netifd detects when an interface config changes by
declaring various parameters the interface has. But if
an interface has separate config sections for peer configs,
netifd is blind to those changes. This requires a manual
call to renew, whereby proto_x_renew() accounts for
peer changes.

This change allows netifd to see when peer entries change.
The proto script shall define:

peer_detect=1

For example, defining this for WireGuard:

proto_wireguard_init_config() {
	...
	peer_detect=1

	...
}

is enough for netifd to detect when any wireguard_wgX peer
entries change, avoid disruptive network restarts, and simply
renew.

netifd now hashes interface peer entries prefixed
<proto>_<interface>, and if its config entries change, netifd
issues a renew or restart. Example: for WireGuard interface
'foo', WireGuard entries 'config wireguard_foo' are hashed.
For Yggdrasil interface 'test', Yggdrasil entries
'config yggdrasil_test_peer' are hashed.  Each interface
requires an additional 8 bytes for the 64 bit hash.

The hash is not chosen for security or integrity,
but to detect change. It's low complexity, quick and requires
no extra .h or lib.

https://datatracker.ietf.org/doc/draft-eastlake-fnv/

We add also the peer_detect property to get_proto_handlers:

"wireguard": {
	"validate": {
		"private_key": "string",
		"listen_port": "uinteger",
		"mtu": "uinteger",
		"fwmark": "string",
		"addresses": "string"
	},
	"immediate": false,
	"no_device": false,
	"init_available": true,
	"renew_available": true,
	"peer_detect": true,
	"force_link_default": false,
	"last_error": false,
	"teardown_on_l3_link_down": false,
	"no_task": true
},...

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
@efahl
Copy link

efahl commented Feb 9, 2026

Oh, sorry, I missed this last week. No, I don't have WG on anything, but from a superficial scan of the code it looks reasonable...

@nbd168
Copy link
Member

nbd168 commented Feb 9, 2026

I'm planning on adding netifd ucode proto handlers soon, which will have more flexibility in parsing the configuration. I expect that this will make the extra C code you're adding obsolete.

@systemcrash
Copy link
Contributor Author

Oh that would be amazing. I was looking at the same thing wondering how I can make the openvpn proto conversion that I made also work in ucode. ( netifd only globs for *.sh ).

I hope you're right - even with the scripts of today, netifd is blind to peer entries.

@nbd168
Copy link
Member

nbd168 commented Feb 10, 2026

I'm planning on giving ucode proto handlers potential access to the full uci config (or even files if needed), while still leaving in enough of a framework that they don't have to worry about things like config change detection.

@nbd168
Copy link
Member

nbd168 commented Feb 15, 2026

Please test the netifd update and wireguard-tools change from https://git.openwrt.org/?p=openwrt/staging/nbd.git;a=summary

@systemcrash
Copy link
Contributor Author

systemcrash commented Feb 16, 2026

Built a fresh rc4 VM, apk add, network restart. Crash loop. Tried removing old files, netifd_loglevel 7 no help. ONLY when I added

procd_set_param stderr 1 to /etc/init.d/network did I start to see something. We should fix that.

When I added the updated libblobmsg that it was complaining about to /dev/null, did it start to work ;)

You can add my SoB if you want. Excellent work. Smash it in and pick it to 25.12.

Edit: didn't think to try the ubus debug stuff, but it's kinda tough without network :)

Edit 2:

I think we should still use

	$(INSTALL_BIN) ./files/wireguard.uc $(1)/lib/netifd/proto/

since we want to ensure +x permissions on the 'bin' - then it can be called directly....

@nbd168
Copy link
Member

nbd168 commented Feb 16, 2026

Thanks for testing. By the way the +x is completely unnecessary, because the script cannot be run directly - it can only be loaded by netifd. I will remove the #!/usr/bin/env ucode instead to make that clear.

@systemcrash
Copy link
Contributor Author

Well, at least for testing, it has utility. Calling the script directly, or ucode script.uc to validate it has no problems is useful.

The examples/proto.uc could do with getting some more "meat".

@nbd168
Copy link
Member

nbd168 commented Feb 16, 2026

It cannot be run directly, since it depends on API provided by netifd (and the handler stub). netifd should emit log messages when the script doesn't compile, so it's not like there is no way of checking it.

@systemcrash
Copy link
Contributor Author

systemcrash commented Feb 16, 2026

It cannot be run directly, since it depends on API provided by netifd (and the handler stub). netifd should emit log messages when the script doesn't compile, so it's not like there is no way of checking it.

Yes, you're correct there. I'm wondering about process management. Do we need to check return values of proto.run_command(cmd);?

Would be happy if you could check my usage in openwrt/packages#28533

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.

3 participants

Comments