Bug 275026 - net/wireguard-tools: after service wireguard stop, route monitor keeps running in the background
Summary: net/wireguard-tools: after service wireguard stop, route monitor keeps runnin...
Status: New
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: Any Any
: --- Affects Only Me
Assignee: Bernhard Froehlich
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-11-11 11:59 UTC by Vedran Miletic
Modified: 2024-04-17 01:52 UTC (History)
4 users (show)

See Also:
bugzilla: maintainer-feedback? (decke)


Attachments
script simplifcation due to 14.0-RELEASE route messages format (1000 bytes, patch)
2024-04-09 20:48 UTC, Oleg Streejak
no flags Details | Diff
another script simplification. The same effect but it's more in original author's style (1013 bytes, patch)
2024-04-09 20:51 UTC, Oleg Streejak
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Vedran Miletic 2023-11-11 11:59:50 UTC
# service wireguard start
[#] ifconfig wg create name wg0
(...)
[+] Backgrounding route monitor

# service wireguard stop
[#] ifconfig wg0 destroy

# ps | grep route
2625  4  S+   0:00.00 grep route
85368  4  I    0:00.00 route -n monitor
Comment 1 Oleg Streejak 2024-04-08 14:20:03 UTC
Hi all!

I've independently investigated this case and, at first, it seemed to me that the problem lies within wg-quick bash script, to be more specific - at monitor_daemon() function. But now it seems to me that the real cause is at route -n monitor base command. It's output changed (due to netlink transition), and now scripts have totally no input from it.

See how its output differs from 13.3 то 14.0:

under 13.3-RELEASE (GENERIC amd64)

$ bash -c 'route -n monitor | while read event; do echo "bash: $event"; done;' &
[1] 24167
$ ifconfig gif0 create && ifconfig gif0 destroy
bash: 
bash: got message of size 24 on Mon Apr  8 16:47:51 2024
bash: RTM_IFANNOUNCE: interface arrival/departure: len 24, if# 6, what: arrival
bash: 
bash: got message of size 168 on Mon Apr  8 16:47:51 2024
bash: RTM_IFINFO: iface status change: len 168, if# 6, link: down, flags:<PTP,MULTICAST>
bash: 
bash: got message of size 168 on Mon Apr  8 16:47:51 2024
bash: RTM_IFINFO: iface status change: len 168, if# 6, link: down, flags:<PTP,MULTICAST>
bash: 
bash: got message of size 24 on Mon Apr  8 16:47:51 2024
bash: RTM_IFANNOUNCE: interface arrival/departure: len 24, if# 6, what: departure
$

 
under 14.0-RELEASE-p6
$ bash -c 'route -n monitor | while read event; do echo "bash: $event"; done;' &
[4] 15153
$ ifconfig gif0 create && ifconfig gif0 destroy
$


as you may see there's not a line from monitor went to read, but route -n monitor alone (on 14.0) do print messages on console:

$ route -nt monitor &
[4] 15202
$ ifconfig gif0 create && ifconfig gif0 destroy
22:07:05.940 PID    0 add/repl iface iface#3 gif0 admin DOWN oper DOWN mtu 1280 
22:07:05.943 PID    0 add/repl iface iface#3 gif0 admin DOWN oper DOWN mtu 1280 
22:07:05.943 PID    0 add/repl iface iface#3 gif0 admin DOWN oper DOWN mtu 1280 
22:07:05.946 PID    0 add/repl iface iface#3 gif0 admin DOWN oper DOWN mtu 1280 
22:07:05.946 PID    0 delete iface iface#3 gif0 admin DOWN oper DOWN mtu 1280
Comment 2 Oleg Streejak 2024-04-09 20:48:54 UTC
Created attachment 249862 [details]
script simplifcation due to 14.0-RELEASE route messages format

the main problem was route itself, so first use my patch and recompile /sbin/route (see bug #278265). And then you can patch wg-quick
Comment 3 Oleg Streejak 2024-04-09 20:51:50 UTC
Created attachment 249863 [details]
another script simplification. The same effect but it's more in original author's style
Comment 4 Oleg Streejak 2024-04-09 20:53:41 UTC
Comment on attachment 249863 [details]
another script simplification. The same effect but it's more in original author's style

>--- /usr/local/bin/wg-quick.org	2024-04-05 18:52:38.093753000 +0300
>+++ /usr/local/bin/wg-quick	2024-04-09 19:30:08.214518000 +0300
>@@ -284,19 +284,17 @@
> 	(make_temp
> 	trap 'del_routes; clean_temp; exit 0' INT TERM EXIT
> 	exec >/dev/null 2>&1
>-	exec 19< <(exec route -n monitor)
>-	local event pid=$!
> 	# TODO: this should also check to see if the endpoint actually changes
> 	# in response to incoming packets, and then call set_endpoint_direct_route
> 	# then too. That function should be able to gracefully cleanup if the
> 	# endpoints change.
>-	while read -u 19 -r event; do
>-		[[ $event == RTM_* ]] || continue
>+	while read -r event; do
>+		# == due to 14.0 NETLINK messages == [[ $event == RTM_* ]] || continue
> 		ifconfig "$INTERFACE" >/dev/null 2>&1 || break
> 		[[ $AUTO_ROUTE4 -eq 1 || $AUTO_ROUTE6 -eq 1 ]] && set_endpoint_direct_route
> 		# TODO: set the mtu as well, but only if up
>-	done
>-	kill $pid) & disown
>+	done < <(exec route -n monitor)
>+	) & disown
> }
> 
> HAVE_SET_DNS=0