diff --git a/package/luci-app-dawnline/Makefile b/package/luci-app-dawnline/Makefile index 5b208c4..935562d 100644 --- a/package/luci-app-dawnline/Makefile +++ b/package/luci-app-dawnline/Makefile @@ -6,7 +6,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=luci-app-dawnline -PKG_VERSION:=0.4.0 +PKG_VERSION:=0.4.1 PKG_RELEASE:=1 PKG_LICENSE:=MIT diff --git a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/fw.uc b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/fw.uc index 809bc7f..9233a7b 100644 --- a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/fw.uc +++ b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/fw.uc @@ -1,23 +1,30 @@ // Dawnline — fw4 and routing around it: each tunnel's zone, its forwardings // to home and WAN, the WAN rules for the tunnels and the portal (named -// sections dawnline_*), what the watchdog checks about them, and the reply -// routes that keep Dawnline's own answers on the main table. Only sections +// sections dawnline_*), what the watchdog checks about them, and the nft +// table that keeps Dawnline's own answers out of proxies. Only sections // named dawnline_* are ever changed. +import { readfile, unlink } from 'fs'; +import { q, sh, sh_out, uniq, uci_put, uci_section, ack_config, write_file, RUN_DIR } from 'dawnline.util'; import { cursor } from 'uci'; -import { sh, sh_out, uniq, uci_put, uci_section, ack_config } from 'dawnline.util'; import * as core from 'dawnline.core'; import * as net from 'dawnline.net'; +import * as nft from 'dawnline.nft'; const LOOP_ZONE = 'dl_loop'; /* * Our own answers leave by the main table: the portal's (TCP) and each - * tunnel's (UDP). Proxies on the router (tproxy with an fwmark rule) may mark - * the router's own traffic to internet addresses and divert it locally, which - * swallows replies to outside clients. These rules sit before such fwmark - * rules and only match the router's own traffic (iif lo) from our listening - * ports: forwarded traffic keeps whatever routing the router has for it. + * tunnel's (UDP). Proxies on the router (tproxy with an fwmark rule to a + * `local default` table) mark the router's own traffic to internet addresses + * in an output route chain, which diverts replies to outside clients into lo. + * A policy rule on the source port can't exempt them: the reroute after the + * mark is set looks up without ports. So a later output route chain of our + * own clears exactly those proxy marks on packets from our listening ports, + * and the kernel routes them again by main. Forwarded traffic is not touched. */ +const REPLY_TABLE = 'dawnline_reply'; +const REPLY_SIG = `${RUN_DIR}/reply.nft`; +/** Older versions kept answers out of proxies with `ip rule` at this priority; it never matched. */ const PREF_REPLY = 90; /** With `dry`, only says whether the firewall config differs from what Dawnline wants. */ @@ -140,38 +147,115 @@ export function drift(m) { return null; }; -/** Our reply rules of one family as `ip rule show` prints them: [{ proto, port, lo }]. */ -function reply_rules(fam) { - let out = []; - // "90: from all iif lo ipproto udp sport 51820 lookup main" (older versions: without "iif lo"). - for (let line in split(sh_out(`ip ${fam} rule show priority ${PREF_REPLY} 2>/dev/null`) ?? '', '\n')) { - let mm = match(line, /ipproto (tcp|udp) sport ([0-9]+) lookup main/); - if (mm) - push(out, { proto: mm[1], port: mm[2], lo: index(line, ' iif lo ') >= 0 }); - } - return out; +const MARK_ALL = 0xffffffff; + +function hx(n) { + return sprintf('0x%x', n); } -/** One `iif lo ipproto

sport lookup main` rule per listening port and family. */ -export function sync_reply_routes(m) { - let want = []; +/** + * Policy rules that send marked traffic into a table with `local default`, + * the way transparent proxies capture the router's own traffic: + * [{ pref, mark, mask, table }], one per mark and mask. + */ +export function proxy_diverts() { + let out = [], seen = {}; + for (let fam in [ '-4', '-6' ]) + for (let line in split(sh_out(`ip ${fam} rule show 2>/dev/null`) ?? '', '\n')) { + // "1000: from all fwmark 0x1 lookup 100", or "fwmark 0x1/0xff". + let mm = match(line, /^(\d+):.*fwmark 0x([0-9a-f]+)(\/0x([0-9a-f]+))?.* lookup (\S+)/); + if (!mm || +mm[1] >= 32766) + continue; + let d = { pref: +mm[1], mark: hex(mm[2]), mask: mm[4] ? hex(mm[4]) : MARK_ALL, table: mm[5] }; + let key = `${d.mark}/${d.mask}`; + if (seen[key] || !match(sh_out(`ip ${fam} route show table ${d.table} 2>/dev/null`) ?? '', /(^|\n)local default/)) + continue; + seen[key] = true; + push(out, d); + } + return out; +}; + +/** A divert as `ip rule` shows it, for people: "pref 1000 fwmark 0x1 → table 100". */ +export function divert_str(d) { + return `pref ${d.pref} fwmark ${hx(d.mark)}${d.mask == MARK_ALL ? '' : `/${hx(d.mask)}`} → table ${d.table}`; +}; + +/** Ports Dawnline answers on from the outside: { tcp: [...], udp: [...] }. */ +function reply_ports(m) { + let p = { tcp: [], udp: [] }; + let ok = (n) => n >= 1 && n <= 65535; if (m.settings.portal_enabled) { - push(want, `tcp ${+m.settings.portal_port}`); + push(p.tcp, +m.settings.portal_port); if (core.portal_http(m)) - push(want, `tcp ${core.portal_http_port(m)}`); + push(p.tcp, +core.portal_http_port(m)); } for (let id, srv in m.servers) - if (srv.private_key && srv.enabled && +srv.port) - push(want, `udp ${+srv.port}`); - want = uniq(want); - for (let fam in [ '-4', '-6' ]) { - // Ours that are no longer wanted, and the older ones that also caught forwarded traffic. - for (let r in reply_rules(fam)) - if (!r.lo || index(want, `${r.proto} ${r.port}`) < 0) - sh(`ip ${fam} rule del priority ${PREF_REPLY}${r.lo ? ' iif lo' : ''} ipproto ${r.proto} sport ${r.port} lookup main 2>/dev/null`); - let have = map(filter(reply_rules(fam), (r) => r.lo), (r) => `${r.proto} ${r.port}`); - for (let w in want) - if (index(have, w) < 0) - sh(`ip ${fam} rule add priority ${PREF_REPLY} iif lo ipproto ${split(w, ' ')[0]} sport ${split(w, ' ')[1]} lookup main`); + if (srv.private_key && srv.enabled) + push(p.udp, +srv.port); + return { tcp: uniq(filter(p.tcp, ok)), udp: uniq(filter(p.udp, ok)) }; +} + +/** The reply table for these diverts, or '' when there is nothing to keep out of them. */ +function reply_script(m, diverts) { + let p = reply_ports(m); + if (!length(diverts) || !length(p.tcp) && !length(p.udp)) + return ''; + let t = []; + push(t, `table inet ${REPLY_TABLE} {`); + push(t, ' chain out {'); + // After the proxies' own output chains (they mark at mangle, -150). + push(t, ' type route hook output priority filter + 10; policy accept;'); + for (let proto in [ 'tcp', 'udp' ]) { + if (!length(p[proto])) + continue; + for (let d in diverts) + // Only the divert's bits: other marks on the packet (multi-WAN) stay. + push(t, ` ${proto} sport { ${join(', ', p[proto])} } ` + (d.mask == MARK_ALL + ? `meta mark ${hx(d.mark)} meta mark set 0x0` + : `meta mark & ${hx(d.mask)} == ${hx(d.mark)} meta mark set meta mark & ${hx(~d.mask & MARK_ALL)}`)); } + push(t, ' }'); + push(t, '}'); + return join('\n', t) + '\n'; +} + +function reply_loaded() { + return system(`nft list table inet ${REPLY_TABLE} >/dev/null 2>&1`) == 0; +} + +/** Are the answers from our ports kept out of these diverts right now (watchdog)? */ +export function reply_ok(m, diverts) { + let s = reply_script(m, diverts); + return s == '' || (s == readfile(REPLY_SIG) && reply_loaded()); +}; + +/** + * Keep the reply table in step with our ports and the proxies' diverts (apply + * and every agent tick: a proxy may start after Dawnline). Cheap when nothing changed. + */ +export function sync_reply_routes(m) { + // Rules of older versions ("90: from all iif lo ipproto udp sport 51820 lookup main", once without "iif lo"). + for (let fam in [ '-4', '-6' ]) + for (let line in split(sh_out(`ip ${fam} rule show priority ${PREF_REPLY} 2>/dev/null`) ?? '', '\n')) { + let mm = match(line, /(iif lo )?ipproto (tcp|udp) sport ([0-9]+) lookup main/); + if (mm) + sh(`ip ${fam} rule del priority ${PREF_REPLY}${mm[1] ? ' iif lo' : ''} ipproto ${mm[2]} sport ${mm[3]} lookup main 2>/dev/null`); + } + let s = reply_script(m, proxy_diverts()); + if (s == '') { + if (reply_loaded()) + system(`nft delete table inet ${REPLY_TABLE} 2>/dev/null`); + unlink(REPLY_SIG); + return; + } + if (s == readfile(REPLY_SIG) && reply_loaded()) + return; + let err = nft.load(`table inet ${REPLY_TABLE}\ndelete table inet ${REPLY_TABLE}\n${s}`); + if (err) { + unlink(REPLY_SIG); + system(`logger -t dawnline -p user.err ${q('reply table: ' + err)}`); + return; + } + write_file(REPLY_SIG, s, 0o644); }; diff --git a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/health.uc b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/health.uc index f982a1e..e65fae4 100644 --- a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/health.uc +++ b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/health.uc @@ -7,6 +7,7 @@ import { q, sh, sh_out, parse_cidr, cidr_overlaps, ip2int, in_cidr, file_exists, import * as core from 'dawnline.core'; import * as live from 'dawnline.live'; import * as apply from 'dawnline.apply'; +import * as fw from 'dawnline.fw'; import * as loop from 'dawnline.loop'; import * as net from 'dawnline.net'; import * as awg from 'dawnline.awg'; @@ -167,35 +168,22 @@ function check(id, title, status, detail, fix, problem) { return { id, title, status, detail: detail ?? '', fix: fix ?? null, problem: bad ? (problem ?? problem_of(title)) : null }; } -/* - * Policy rules that send the router's own (marked) traffic into a local table, - * the way transparent proxies capture it: "pref 1000 fwmark 0x1 → table 100". +/** + * Answers from our ports and the proxies that would swallow them (fw.sync_reply_routes): + * { ok, diverts: ["pref 1000 fwmark 0x1 → table 100"] }. */ -function proxy_diverts() { - let out = []; - for (let line in split(sh_out('ip -4 rule show 2>/dev/null') ?? '', '\n')) { - let mm = match(line, /^(\d+):.*fwmark (\S+).* lookup (\S+)/); - if (!mm || +mm[1] >= 32766) - continue; - if (match(sh_out(`ip -4 route show table ${mm[3]} 2>/dev/null`) ?? '', /(^|\n)local default/)) - push(out, `pref ${mm[1]} fwmark ${mm[2]} → table ${mm[3]}`); - } - return out; +function replies(m) { + let d = fw.proxy_diverts(); + return { ok: fw.reply_ok(m, d), diverts: map(d, fw.divert_str) }; } -/** Our "answers leave by main" rule for this port (see apply.sync_reply_routes). */ -function reply_rule(proto, port) { - return index(sh_out('ip -4 rule show priority 90 2>/dev/null') ?? '', `ipproto ${proto} sport ${+port} lookup main`) >= 0; -} - -function reply_check(id, title, proto, port, diverts) { - let ok = reply_rule(proto, port); - let via = length(diverts) ? ` A proxy on the router diverts its own traffic (${join(', ', diverts)}); these answers skip it.` : ''; - return check(id, title, ok ? 'ok' : (length(diverts) ? 'error' : 'warn'), - ok ? `Answers from ${uc(proto)} ${port} go straight out.${via}` - : length(diverts) ? `Answers from ${uc(proto)} ${port} can be swallowed by a proxy on the router (${join(', ', diverts)}): clients get no reply.` - : `The rule that keeps answers from ${uc(proto)} ${port} out of proxies is missing.`, - ok ? null : 'apply'); +function reply_check(id, title, proto, port, r) { + let via = join(', ', r.diverts); + return check(id, title, r.ok ? 'ok' : 'error', + !r.ok ? `Answers from ${uc(proto)} ${port} are swallowed by a proxy on the router (${via}): clients get no reply.` + : length(r.diverts) ? `Answers from ${uc(proto)} ${port} go straight out. A proxy on the router diverts its own traffic (${via}); these answers skip it.` + : `Answers from ${uc(proto)} ${port} go straight out.`, + r.ok ? null : 'apply'); } const PRIVATE = map([ '100.64.0.0/10', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16' ], parse_cidr); @@ -240,7 +228,7 @@ function awg_check() { } /** Checks of a server's AmneziaWG tunnel. */ -function awg_checks(m, id, srv, label, fwc, dmp, diverts) { +function awg_checks(m, id, srv, label, fwc, dmp, rep) { let out = []; let st = live.iface_status(srv.ifname); let up = !!st?.up; @@ -272,7 +260,7 @@ function awg_checks(m, id, srv, label, fwc, dmp, diverts) { push(out, check(`fw_${id}`, `AmneziaWG firewall${label}`, rule && zone ? 'ok' : 'error', rule && zone ? `Zone ${srv.ifname} and the WAN rule for UDP ${srv.port} exist.` : 'Firewall zone or WAN rule is missing.', rule && zone ? null : 'apply')); - push(out, reply_check(`reply_${id}`, `AmneziaWG answers reach devices${label}`, 'udp', srv.port, diverts)); + push(out, reply_check(`reply_${id}`, `AmneziaWG answers reach devices${label}`, 'udp', srv.port, rep)); // Port collisions with other tunnels. let clash = []; let nc = cursor(); @@ -296,7 +284,7 @@ function awg_checks(m, id, srv, label, fwc, dmp, diverts) { } /** The portal's checks: listening, its address, answers, plain HTTP and the certificate. */ -function portal_checks(m, need, diverts) { +function portal_checks(m, need, rep) { let out = []; let s = m.settings; if (length(need.https)) @@ -320,7 +308,7 @@ function portal_checks(m, need, diverts) { wan && index(ips, wan) < 0 ? `${s.portal_host} points at ${join(', ', ips)}, not this router (${wan}).` : `${s.portal_host} → ${join(', ', ips)}${wan ? ' (this router)' : ''}.`)); } - push(out, reply_check('portal_reply', 'Portal answers reach visitors', 'tcp', s.portal_port, diverts)); + push(out, reply_check('portal_reply', 'Portal answers reach visitors', 'tcp', s.portal_port, rep)); if (core.portal_http(m)) { let ipl = filter(core.portal_hosts(m), (h) => ip2int(h) != null); push(out, check('portal_http', 'Plain HTTP links', 'warn', @@ -347,8 +335,8 @@ function portal_checks(m, need, diverts) { * (install:, updates). */ export function health(m) { - let diverts = proxy_diverts(); m ??= core.load(); + let rep = replies(m); let out = []; let dmp = live.dump(m); let need = needs(); @@ -382,7 +370,7 @@ export function health(m) { push(out, check(`srv_${id}`, `Server${label}`, 'skip', 'Server is disabled.')); continue; } - push(out, ...awg_checks(m, id, srv, label, fwc, dmp, diverts)); + push(out, ...awg_checks(m, id, srv, label, fwc, dmp, rep)); push(out, endpoint_check(`ep_${id}`, `Public address${label}`, srv, core.endpoint_host(m, srv), `UDP ${srv.port}`)); if (srv.lan_like) { @@ -406,7 +394,7 @@ export function health(m) { } if (m.settings.portal_enabled) - push(out, ...portal_checks(m, need, diverts)); + push(out, ...portal_checks(m, need, rep)); return out; }; diff --git a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/nft.uc b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/nft.uc index 525b7ae..e20be78 100644 --- a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/nft.uc +++ b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/nft.uc @@ -181,7 +181,7 @@ function table(m, sets) { } /** Run an nft script (one transaction); null when it loaded, else what nft said. */ -function load(script) { +export function load(script) { ensure_dir(RUN_DIR); // A file of its own: two writers must never load each other's script. let f = `${RUN_DIR}/nft.${rand_hex(4)}.rules`; @@ -193,7 +193,7 @@ function load(script) { return null; // Without the random file name, so the same failure reads the same every time. return replace(trim(r.out), f, 'rules') || `nft exited with ${r.code}`; -} +}; /** Full table rebuild (structure changes: servers, home networks, DNS paths); null or the error. */ export function rebuild(m, sets) { diff --git a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/util.uc b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/util.uc index 4aab999..7d14f95 100644 --- a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/util.uc +++ b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/util.uc @@ -4,7 +4,7 @@ import { open, readfile, writefile, rename, mkdir, stat, unlink, popen, lsdir } from 'fs'; -export const VERSION = '0.4.0'; +export const VERSION = '0.4.1'; /** GitHub repository with Dawnline releases (updates and the installer). */ export const REPO = 'tickcount/luci-app-dawnline'; export const RUN_DIR = '/var/run/dawnline'; diff --git a/uninstall.sh b/uninstall.sh index 0848f03..f7c19f8 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -564,15 +564,16 @@ clean_network_leftovers() { ok "Removed leftover firewall sections: $(echo "$_fw" | tr '\n' ' ')" fi fi - if command_exists nft && nft list table inet dawnline >/dev/null 2>&1; then + for _t in dawnline dawnline_reply; do + command_exists nft && nft list table inet "$_t" >/dev/null 2>&1 || continue _found=1 if [ "$DRY_RUN" -eq 1 ]; then - dry "delete the nft table inet dawnline" + dry "delete the nft table inet $_t" else - nft delete table inet dawnline 2>/dev/null - ok "Removed the nft table inet dawnline" + nft delete table inet "$_t" 2>/dev/null + ok "Removed the nft table inet $_t" fi - fi + done if [ -e /var/run/netns/dawnline ] || ip link show dlx0 >/dev/null 2>&1 || ip link show dll0 >/dev/null 2>&1; then _found=1 if [ "$DRY_RUN" -eq 1 ]; then @@ -588,9 +589,10 @@ clean_network_leftovers() { [ "$_found" -eq 1 ] || ok "No tunnel, firewall, nft or routing leftovers" } -# Dawnline's policy rules: priority 90 sends the portal's and the tunnels' -# answers by the main table; 5200-5299 are the "like a home device" loop for a -# tunnel interface (Dawnline's, or one already gone), with tables 2077-2176. +# Dawnline's policy rules: priority 90 sent the portal's and the tunnels' +# answers by the main table (up to 0.4.0); 5200-5299 are the "like a home +# device" loop for a tunnel interface (Dawnline's, or one already gone), with +# tables 2077-2176. clean_routing_leftovers() { _ours=" $(echo "$1" | tr '\n' ' ') " _rules="" diff --git a/web/package-lock.json b/web/package-lock.json index 0f2353c..978a308 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -1,12 +1,12 @@ { "name": "dawnline-web", - "version": "0.4.0", + "version": "0.4.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "dawnline-web", - "version": "0.4.0", + "version": "0.4.1", "license": "MIT", "dependencies": { "qrcode-generator": "1.4.4" diff --git a/web/package.json b/web/package.json index eed802d..b34ee4b 100644 --- a/web/package.json +++ b/web/package.json @@ -1,6 +1,6 @@ { "name": "dawnline-web", - "version": "0.4.0", + "version": "0.4.1", "private": true, "license": "MIT", "type": "module",