Skip to content

Commit 75bd350

Browse files
committed
Merge remote-tracking branch 'origin/pr/531'
* origin/pr/531: network: do not restart tinyproxy if DNS hasn't changed
2 parents 112032b + 01aa298 commit 75bd350

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

network/qubes-nmhook

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
if [ "$2" = "up" ] || [ "$2" = "vpn-up" ] || [ "$2" = "vpn-down" ] || [ "$2" = "dhcp4-change" ]; then
88
/usr/lib/qubes/qubes-setup-dnat-to-ns
99

10-
# FIXME: Tinyproxy does not reload DNS servers.
11-
if under_systemd ; then
12-
systemctl --no-block try-restart qubes-updates-proxy.service
13-
else
14-
service qubes-updates-proxy try-restart
10+
# Do not restart updates proxy if DNS hasn't changed
11+
# (qubes-setup-dnat-to-ns exits with 100 in such a case)
12+
if [ $? -ne 100 ]; then
13+
# FIXME: Tinyproxy does not reload DNS servers.
14+
if under_systemd ; then
15+
systemctl --no-block try-restart qubes-updates-proxy.service
16+
else
17+
service qubes-updates-proxy try-restart
18+
fi
1519
fi
1620
fi

network/qubes-setup-dnat-to-ns

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
#
2121

2222
from __future__ import annotations
23+
24+
import subprocess
25+
import sys
26+
2327
import dbus
2428
import qubesdb
2529
from typing import List
@@ -85,7 +89,7 @@ def install_firewall_rules(dns):
8589
qubesdb_dns.append(IPv4Address(ns_maybe.decode("ascii", "strict")))
8690
except (UnicodeDecodeError, ValueError):
8791
pass
88-
res = [
92+
preamble = [
8993
'add table ip qubes',
9094
# Add the chain so that the subsequent delete will work. If the chain already
9195
# exists this is a harmless no-op.
@@ -95,6 +99,8 @@ def install_firewall_rules(dns):
9599
# atomic operation, so there is no period where neither chain is present or
96100
# where both are present.
97101
'delete chain ip qubes dnat-dns',
102+
]
103+
rules = [
98104
'table ip qubes {',
99105
'chain dnat-dns {',
100106
'type nat hook prerouting priority dstnat; policy accept;',
@@ -105,7 +111,7 @@ def install_firewall_rules(dns):
105111
# Or maybe user wants to enforce DNS-Over-HTTPS.
106112
# Drop IPv4 DNS requests to qubesdb_dns addresses.
107113
for vm_nameserver in qubesdb_dns:
108-
res += [
114+
rules += [
109115
f"ip daddr {vm_nameserver} udp dport 53 drop",
110116
f"ip daddr {vm_nameserver} tcp dport 53 drop",
111117
]
@@ -115,12 +121,25 @@ def install_firewall_rules(dns):
115121
dns_resolved = dns_resolved + dns_resolved
116122
for vm_nameserver, dest in zip(qubesdb_dns, dns_resolved):
117123
dns_ = str(dest)
118-
res += [
124+
rules += [
119125
f"ip daddr {vm_nameserver} udp dport 53 dnat to {dns_}",
120126
f"ip daddr {vm_nameserver} tcp dport 53 dnat to {dns_}",
121127
]
122-
res += ["}\n}\n"]
123-
os.execvp("nft", ("nft", "--", "\n".join(res)))
128+
rules += ["}", "}"]
129+
130+
# check if new rules are the same as the old ones - if so, don't reload
131+
# and return that info via exit code
132+
try:
133+
old_rules = subprocess.check_output(
134+
["nft", "list", "chain", "ip", "qubes", "dnat-dns"]).decode().splitlines()
135+
except subprocess.CalledProcessError:
136+
old_rules = []
137+
old_rules = [line.strip() for line in old_rules]
138+
139+
if old_rules == rules:
140+
sys.exit(100)
141+
142+
os.execvp("nft", ("nft", "--", "\n".join(preamble + rules)))
124143

125144
if __name__ == '__main__':
126145
install_firewall_rules(get_dns_resolved())

network/setup-ip

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ configure_qubes_ns() {
185185
secondary_dns=$(qubesdb-read /qubes-netvm-secondary-dns)
186186
echo "NS1=$primary_dns" > /var/run/qubes/qubes-ns
187187
echo "NS2=$secondary_dns" >> /var/run/qubes/qubes-ns
188-
/usr/lib/qubes/qubes-setup-dnat-to-ns
188+
ret=0
189+
/usr/lib/qubes/qubes-setup-dnat-to-ns || ret=$?
190+
[ "$ret" -eq 0 ] || [ "$ret" -eq 100 ] || exit "$ret"
189191
}
190192

191193
qubes_ip_change_hook() {

0 commit comments

Comments
 (0)