Skip to content

feat(adblock): implement device-based enforcement and migration scripts - #1875

Open
m-dilorenzi wants to merge 1 commit into
mainfrom
fix_dns_adblock
Open

feat(adblock): implement device-based enforcement and migration scripts#1875
m-dilorenzi wants to merge 1 commit into
mainfrom
fix_dns_adblock

Conversation

@m-dilorenzi

Copy link
Copy Markdown
Collaborator

This PR fixes the issue where DNS queries are no longer redirected to dnsmasq after the adblock upgrade to 4.5.5.

Up to adblock 4.1.5 the local DNS enforcement was configured with firewall zone names, and adblock turned them into UCI redirect sections letting fw4 resolve each zone into its devices. Since 4.5.5 adblock writes nftables rules directly and expects the adb_nftdevforce option to contain device names, because its values are used as-is in the iifname match. Zone names kept being stored there, so the resulting rules never matched any packet and DNS traffic was not intercepted.

The fix makes adb_nftdevforce a derived value: the zones selected in the UI are stored in a dedicated ns_tsdns_zones option and expanded into the devices of those zones. The UI is not affected, it keeps working with zones.

Changes

  • packages/ns-api/files/ns.threatshield: dns_edit_settings now saves the selected zones in ns_tsdns_zones and writes the devices they contain into adb_nftdevforce; dns_list_settings reads the zones back from the new option, with a fallback that keeps the settings page working until the migration has run.
  • packages/ns-threat_shield/files/configure-adblock-devices.py (new): pre-commit hook that recomputes adb_nftdevforce whenever adblock, firewall or network change, so that adding an interface to an enforced zone does not leave its DNS traffic unfiltered.
  • packages/ns-threat_shield/files/adblock-devices-migrate.py (new): rebuilds the configuration on systems already upgraded, which would otherwise stay broken until someone re-saves the Threat Shield DNS settings, and restarts adblock.
  • packages/ns-threat_shield/files/36_ns-threat_shield (new): uci-defaults running the migration above, following the same pattern used for the wireguard migration.
  • packages/ns-threat_shield/Makefile: installs the new hook, migration and uci-defaults, and raises the ns-api requirement so the migration cannot be undone by an older API still writing zone names.

Closes: #1871

@m-dilorenzi m-dilorenzi self-assigned this Aug 7, 2026
Comment on lines +752 to +757
devices = []
for zone in zones:
for device in utils.get_all_devices_by_zone(e_uci, zone, exclude_aliases=True):
if device not in devices:
devices.append(device)
return sorted(devices)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Next time you can use a set, so you don't need to check for presence in the list, for the number of occurrences, do not bother changing it

Comment on lines +739 to +747
def dns_get_enforced_zones(e_uci):
# the user selection is stored in ns_tsdns_zones: adb_nftdevforce holds the
# devices derived from it and can't be mapped back to zones without ambiguity
zones = list(e_uci.get('adblock', 'global', 'ns_tsdns_zones', list=True, default=[]))
if zones:
return zones
# before the switch to device-based enforcement the selection was stored inside
# adb_nftdevforce: keep reading it until the migration has run
return list(e_uci.get('adblock', 'global', 'adb_nftdevforce', list=True, default=['lan']))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

After the uci default, this is not needed, right?
We can safely remove it and put this logic underneath

$(INSTALL_BIN) ./files/adjust-banip.py $(1)/usr/libexec/ns-api/post-commit/
$(INSTALL_BIN) ./files/configure-banip-wans.py $(1)/usr/libexec/ns-api/pre-commit/
$(INSTALL_BIN) ./files/configure-adblock-devices.py $(1)/usr/libexec/ns-api/pre-commit/
$(INSTALL_DIR) $(1)/usr/libexec

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Move it at the top where all install dir resides, this missing could've cause issues.

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.

LAN DNS traffic is no longer redirected to dnsmasq

3 participants