Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Plugin package that fixes and enhances the Tailscale integration on GL.iNet rout
or not Tailscale or the kill switch is on, and is kept current as internet connections change — on
every connection event, and re-checked by the watchdog about every 30 seconds. Applied
automatically — no user action required beyond GL's own setting.
- **IoT Network Access** — bidirectional firewall forwardings between IoT network (br-iot) and Tailscale interface (tailscale0), IoT subnet route advertisement, and policy route fixup that ensure IoT clients can use exit nodes and are covered by the kill switch. Requires the IoT Network feature, which the firmware creates since 4.9.0; the toggle is hidden on older firmware, which has no iot network. The kill switch has covered iot since v1.0.22; this adds the remote-access side (route advertisement and the forwardings), which v1.0.22 does not do.
- **Tailscale SSH** — GUI toggle for `tailscale set --ssh`, which enables Tailscale's ACL-based SSH authentication. Most users don't need this — SSH to the router's tailscale IP already works via the normal SSH daemon (Dropbear) without any extra setup. Enable this only if you specifically want identity-based access controlled by a Tailscale SSH ACL rule (Access Controls → Tailscale SSH tab). While enabled, `tailscaled` takes over port 22 for tailnet-origin traffic, which breaks SSH from LAN clients that reach the router via Tailscale subnet routing; in that case, run Dropbear on an alternate port (System → Administration → SSH Access) to keep a path open for both Tailscale and LAN clients.
- **Hide Tailscale Search Domain** (v1.0.22+, optional) — off by default. GL writes your tailnet's MagicDNS suffix (`x.ts.net`) into dnsmasq's advertised domain, so every LAN client receives it over DHCP as its domain/search suffix. On firmware 4.9 GL *replaces* your local domain rather than appending to it, which also breaks `.lan` name resolution. When enabled, the plugin keeps your own local domain as the advertised value and re-asserts it whenever GL writes the suffix back — including against the background re-assert loop 4.9 runs for up to a minute after an interface comes up. Tailnet name resolution is deliberately left intact: the router's split-DNS entry for `ts.net` is untouched, so an explicit `host.x.ts.net` still resolves and only the broadcast hint goes away. This removes one passive indicator that a device sits behind a Tailscale node — it does not hide Tailscale generally, since the router still holds a tailnet address and still answers `ts.net` queries. Turning it off restores GL's behavior at the next network event rather than instantly.
- **Tailscale Version Manager** — installed vs latest version display, one-click update using space-optimized combined binaries, factory restore. Since v1.0.22, versions are compared as full build strings, so an upstream *rebuild* — same Tailscale version re-released with a new build suffix, as happened with the 2026-06-04 `ipnbus` fix — is detected and offered as "(rebuild)" instead of reporting "already at latest" forever. Downloads are bounded by connect and overall timeouts with a guaranteed terminal status, so a dead or crawling network path produces a clean error instead of a stuck "Downloading..." spinner.
Expand Down
4 changes: 2 additions & 2 deletions pkg/control
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Priority: optional
Description: Tailscale enhancements for GL.iNet routers
Adds features that complement GL's native Tailscale integration:
a kernel-level policy-routing kill switch (IPv4 and IPv6) that
survives tailscaled crashes, guest network routing through the
Tailscale exit node, optional Tailscale SSH toggle for ACL-based
survives tailscaled crashes, guest and IoT network routing through
the Tailscale exit node, optional Tailscale SSH toggle for ACL-based
authentication, and a Tailscale binary version manager. On
firmware 4.9+, the plugin auto-adapts to coexist with GL's native
Tailscale features.
Expand Down
17 changes: 15 additions & 2 deletions pkg/prerm
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,25 @@ else
logger -t ts-fix "prerm: guest_net helper not found in /usr/bin/ts-fix-reapply - guest route fixup (to <guest net> table main) not removed"
fi

# Remove guest routing firewall forwardings, the kill switch's lan -> tailscale0 tunnel path,
# Remove IoT subnet policy route fixup. The IoT network comes from reapply's own tested helper
# (iot_net: ipcalc.sh, right for any prefix length — zeroing the last octet is right only for a
# /24 and misses the rule on any other IoT network). It is loaded from the installed reapply,
# which opkg deletes only after this script, rather than copied here. The eval runs nothing but
# the marked helper block of our own root-owned package file.
eval "$(awk '/^# ---8<--- iot_net/,/^# ---8<--- end iot_net/' /usr/bin/ts-fix-reapply 2>/dev/null)"
if command -v iot_net >/dev/null 2>&1; then
_iot=$(iot_net)
[ -n "$_iot" ] && ip rule del to "$_iot" table main 2>/dev/null
else
logger -t ts-fix "prerm: iot_net helper not found in /usr/bin/ts-fix-reapply - IoT route fixup (to <iot net> table main) not removed"
fi

# Remove guest and IoT routing firewall forwardings, the kill switch's lan -> tailscale0 tunnel path,
# and legacy kill switch rules. ts_fix_lan2ts is normally dropped by the disarm above (only when
# the engine recorded creating it); this catches the case where disarm never ran or did not
# finish. The name is ours — GL's own lan -> tailscale0 forwarding is a different section and is
# not reachable by this delete.
for section in ts_fix_guest2ts ts_fix_ts2guest ts_fix_lan2ts ts_ks_lan2wan ts_ks_guest2wan; do
for section in ts_fix_guest2ts ts_fix_ts2guest ts_fix_iot2ts ts_fix_ts2iot ts_fix_lan2ts ts_ks_lan2wan ts_ks_guest2wan; do
uci -q delete firewall."$section" 2>/dev/null
done

Expand Down
1 change: 1 addition & 0 deletions src/config/ts-fix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ config settings 'settings'
option hide_ts_domain '0'
option user_domain ''
option route_guest '0'
option route_iot '0'
option tailscale_ssh '0'
option ks_upgrade_hint_pending '0'
option wan_masq6_set_by_plugin '0'
Expand Down
122 changes: 121 additions & 1 deletion src/rpc/ts-fix
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,25 @@ local function get_guest_subnet()
return cidr
end

-- Helper: get the IoT network as NETWORK/PREFIX for br-iot's IPv4 address, or nil. The network
-- comes from /bin/ipcalc.sh, the tool GL's own script relies on, never from zeroing the last
-- octet: that is right only for a /24 (192.168.10.1/23 is network 192.168.10.0/23), and a wrong
-- network misses GL's "from <net>" rule in fix_iot_policy_route and advertises a route that is
-- not a network address. Both values end up in "sh -c" strings: the address capture admits only
-- digits, dots and one slash, and the result must match the CIDR pattern below or nil is returned.
local function get_iot_subnet()
local data = exec("ip -4 addr show br-iot 2>/dev/null") or ""
local addr = data:match("inet (%d+%.%d+%.%d+%.%d+/%d+)")
if not addr then return nil end
local calc = exec("/bin/ipcalc.sh " .. addr .. " 2>/dev/null") or ""
local network = calc:match("NETWORK=([^\n]*)")
local prefix = calc:match("PREFIX=([^\n]*)")
if not network or not prefix then return nil end
local cidr = network .. "/" .. prefix
if not cidr:match("^%d+%.%d+%.%d+%.%d+/%d+$") then return nil end
return cidr
end

-- Helper: get currently advertised routes from tailscale prefs
local function get_current_routes()
local data = exec("/usr/sbin/tailscale debug prefs 2>/dev/null")
Expand Down Expand Up @@ -213,6 +232,34 @@ local function update_guest_route(enable)
exec("/usr/sbin/tailscale set --advertise-routes=" .. routes_str .. " 2>/dev/null")
end

-- Helper: update advertised routes, adding or removing IoT subnet
local function update_iot_route(enable)
local iot_subnet = get_iot_subnet()
if not iot_subnet then return end

local routes = get_current_routes()
local new_routes = {}
local found = false

for _, route in ipairs(routes) do
if route == iot_subnet then
found = true
if enable then
new_routes[#new_routes + 1] = route
end
else
new_routes[#new_routes + 1] = route
end
end

if enable and not found then
new_routes[#new_routes + 1] = iot_subnet
end

local routes_str = table.concat(new_routes, ",")
exec("/usr/sbin/tailscale set --advertise-routes=" .. routes_str .. " 2>/dev/null")
end

-- Helper: set guest→tailscale firewall forwardings enabled/disabled
local function set_guest_ts_fw(enabled)
local c = uci.cursor()
Expand Down Expand Up @@ -244,6 +291,37 @@ local function set_guest_ts_fw(enabled)
return changed
end

-- Helper: set iot→tailscale firewall forwardings enabled/disabled
local function set_iot_ts_fw(enabled)
local c = uci.cursor()
local changed = false

local forwardings = {
{name = "ts_fix_iot2ts", src = "iot", dest = "tailscale0"},
{name = "ts_fix_ts2iot", src = "tailscale0", dest = "iot"},
}

for _, fwd in ipairs(forwardings) do
local exists = c:get(FW_CONFIG, fwd.name)
if exists and not enabled then
c:delete(FW_CONFIG, fwd.name)
changed = true
elseif not exists and enabled then
c:set(FW_CONFIG, fwd.name, "forwarding")
c:set(FW_CONFIG, fwd.name, "src", fwd.src)
c:set(FW_CONFIG, fwd.name, "dest", fwd.dest)
changed = true
end
end

if changed then
c:commit(FW_CONFIG)
ngx.pipe.spawn({"/etc/init.d/firewall", "reload"})
end

return changed
end

-- Helper: ensure tailscale0 zone masquerade is enabled (defense-in-depth
-- for cross-subnet routing — Tailscale's built-in SNAT can fail to
-- reinitialize after daemon restart on fw3 kernels).
Expand Down Expand Up @@ -388,6 +466,30 @@ local function fix_guest_policy_route(enabled)
end
end

-- Helper: fix IoT subnet policy route for exit node compatibility. Mirrors
-- fix_guest_policy_route above, against br-iot: GL's gl_tailscale adds
-- "from <iot_subnet> lookup main" at priority 0 whenever a Custom Exit Node is
-- set and the IoT network is enabled, which routes all IoT client traffic to
-- the main table → WAN, bypassing the exit node and the kill switch's routing
-- layer. Replaced with the destination-based "to <iot_subnet> table main".
-- Since v1.0.22 the kill switch covers iot (its engine's source-rule swap keeps
-- this same rule in place of GL's "from" rule), so the disabled path re-asserts
-- the engine's layer the same way the guest path does.
local function fix_iot_policy_route(enabled)
local iot_subnet = get_iot_subnet()
if not iot_subnet then return end

-- Always clean up both rule types to avoid duplicates
exec("ip rule del from " .. iot_subnet .. " lookup main 2>/dev/null")
exec("ip rule del to " .. iot_subnet .. " table main 2>/dev/null")

if enabled then
exec("ip rule add to " .. iot_subnet .. " table main priority 0")
else
exec("/usr/bin/ts-fix-ks rules-ensure </dev/null >/dev/null 2>&1")
end
end

-- Firmware detection — on GL 4.9+, some features overlap with GL's native
-- Tailscale enhancements. We keep the plugin fully active but hide UI for
-- features GL provides natively (Advertise Exit Node) and skip the
Expand Down Expand Up @@ -442,6 +544,7 @@ function M.get_config()
res.advertise_exit_node = c:get(TS_FIX_CONFIG, "settings", "advertise_exit_node") == "1"
res.kill_switch = c:get(TS_FIX_CONFIG, "settings", "kill_switch") == "1"
res.route_guest = c:get(TS_FIX_CONFIG, "settings", "route_guest") == "1"
res.route_iot = c:get(TS_FIX_CONFIG, "settings", "route_iot") == "1"
res.tailscale_ssh = c:get(TS_FIX_CONFIG, "settings", "tailscale_ssh") == "1"
res.ks_follow_exit_node = c:get(TS_FIX_CONFIG, "settings", "ks_follow_exit_node") == "1"
res.hide_ts_domain = c:get(TS_FIX_CONFIG, "settings", "hide_ts_domain") == "1"
Expand Down Expand Up @@ -535,6 +638,9 @@ function M.get_config()
-- Guest routing firewall forwarding state
res.route_guest_fw_active = c:get(FW_CONFIG, "ts_fix_guest2ts") ~= nil

-- IoT routing firewall forwarding state
res.route_iot_fw_active = c:get(FW_CONFIG, "ts_fix_iot2ts") ~= nil

-- Tailscale0 zone masquerade state — reported active only when BOTH
-- families' masquerade are set (mirrors kill_switch_fw_active semantics;
-- a torn state surfaces in the UI rather than silently claiming
Expand All @@ -553,6 +659,7 @@ end
@in bool ?advertise_exit_node Enable/disable exit node advertisement
@in bool ?kill_switch Enable/disable kill switch
@in bool ?route_guest Enable/disable guest network routing via Tailscale
@in bool ?route_iot Enable/disable IoT network routing via Tailscale
@in bool ?tailscale_ssh Enable/disable Tailscale SSH server
@in bool ?ks_follow_exit_node Kill switch mirrors GL's Custom Exit Node setting (watchdog-enforced)
@in bool ?hide_ts_domain Stop advertising the tailnet search domain to LAN clients
Expand Down Expand Up @@ -629,6 +736,14 @@ function M.set_config(params)
route_changed = true
end

-- Handle route_iot
if params.route_iot ~= nil then
local val = params.route_iot and "1" or "0"
c:set(TS_FIX_CONFIG, "settings", "route_iot", val)
set_iot_ts_fw(params.route_iot)
route_changed = true
end

-- Handle tailscale_ssh
if params.tailscale_ssh ~= nil then
local val = params.tailscale_ssh and "1" or "0"
Expand Down Expand Up @@ -695,14 +810,19 @@ function M.set_config(params)
end
end

-- Apply route changes (only when route_guest was actually changed)
-- Apply route changes (only when route_guest or route_iot was actually changed)
if route_changed then
local rg = c:get(TS_FIX_CONFIG, "settings", "route_guest") == "1"
update_guest_route(rg)
-- Fix policy route: swap Tailscale's "from <guest>" (blocks the exit
-- node) with "to <guest>" (allows it). Must run after
-- update_guest_route which triggers Tailscale to create its rules.
fix_guest_policy_route(rg)

local ri = c:get(TS_FIX_CONFIG, "settings", "route_iot") == "1"
update_iot_route(ri)
-- Same policy route fixup for the IoT subnet.
fix_iot_policy_route(ri)
end

-- Ensure tailscale0 zone masquerade (defense-in-depth for subnet routing +
Expand Down
Loading