summaryrefslogtreecommitdiff
path: root/net/wireguard-tools/files/patch-wg-quick_freebsd.bash
blob: 83254d7abac391b012ce8e2a3f4e41a0d50d9fe2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
--- wg-quick/freebsd.bash.orig	2021-09-13 22:43:31 UTC
+++ wg-quick/freebsd.bash
@@ -27,6 +27,7 @@ SAVE_CONFIG=0
 CONFIG_FILE=""
 PROGRAM="${0##*/}"
 ARGS=( "$@" )
+FREEBSD_MAJOR_VERSION=$(freebsd-version | cut -d. -f1)
 
 cmd() {
 	echo "[#] $*" >&3
@@ -283,20 +284,28 @@ monitor_daemon() {
 	echo "[+] Backgrounding route monitor" >&2
 	(make_temp
 	trap 'del_routes; clean_temp; exit 0' INT TERM EXIT
+	local event grep_for pid
+	if [[ "$FREEBSD_MAJOR_VERSION" -lt 14 ]]; then
+		grep_for="RTM_"
+	elif [[ $AUTO_ROUTE4 -eq 1 || $AUTO_ROUTE6 -eq 1 ]]; then
+		grep_for="iface" # needs refinement
+	else
+		grep_for="(add/repl|delete) iface iface#[0-9]{1,4} $INTERFACE "
+	fi
 	exec >/dev/null 2>&1
-	exec 19< <(exec route -n monitor)
-	local event pid=$!
+	exec 19< <(exec sh -c "route -n monitor | grep -E --line-buffered '$grep_for'")
+	monitor_ppid=$!
 	# 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
 		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
+	pkill -P "$monitor_ppid" route || true
+	) & disown
 }
 
 HAVE_SET_DNS=0