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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
|
#!/usr/bin/ucode
// shunt - policy based routing daemon
//
// Reads the config, renders the ruleset and the routes, applies them, then
// keeps the learned sets fed from a poll cycle and a passive DNS observer.
// All decisions live in the modules; this file is wiring.
//
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2026 Dirk Brenken <dev@brenken.org>
import { popen, writefile, readfile, unlink, mkdir, lstat, error as fs_error } from 'fs';
import { openlog, syslog, LOG_PID, LOG_DAEMON, LOG_ERR, LOG_WARNING,
LOG_NOTICE, LOG_INFO, LOG_DEBUG } from 'log';
import { load as cfg_load, parse as cfg_parse } from 'shunt.config';
import { compile as match_compile } from 'shunt.match';
import { compile as nft_compile, refresh, teardown } from 'shunt.nft';
import { compile as route_compile } from 'shunt.route';
import { open as snoop_open, observe, RECV_LEN } from 'shunt.snoop';
import { names as poll_names, plan as poll_plan,
addresses as poll_addresses,
index_results as poll_index } from 'shunt.poll';
import { resolve as netifd_resolve } from 'shunt.netifd';
import { create as dedupe_create } from 'shunt.dedupe';
const RT_TABLES = '/etc/iproute2/rt_tables.d/shunt.conf';
const TAG = 'shunt';
let verbose = false;
let dbg = false;
// syslog(3) through the binding, not a `logger` process per line. '%s' as the
// format because syslog() runs sprintf over its arguments, and an ip error
// text can contain a percent sign.
const PRIO = { err: LOG_ERR, warn: LOG_WARNING, notice: LOG_NOTICE,
info: LOG_INFO, debug: LOG_DEBUG };
openlog(TAG, LOG_PID, LOG_DAEMON);
function log(prio, msg) {
syslog(PRIO[prio] ?? LOG_NOTICE, '%s', msg);
if (verbose)
warn(sprintf('[%s] %s\n', prio, msg));
}
function debug(msg) {
if (dbg)
log('debug', msg);
}
let ubus_conn = null;
function ubus() {
if (ubus_conn != null)
return ubus_conn;
try {
ubus_conn = require('ubus').connect();
}
catch (e) {
ubus_conn = false;
}
if (!ubus_conn)
log('warn', 'ubus unavailable - gateway discovery and interface events disabled');
return ubus_conn;
}
function netifd_dump() {
let c = ubus();
return c ? c.call('network.interface', 'dump') : null;
}
function load_config() {
let sections = cfg_load();
if (sections == null) {
log('err', 'ucode-mod-uci missing');
return null;
}
return cfg_parse(sections);
}
function report(kind, issues) {
for (let i in issues)
log('warn', sprintf('%s: %J', kind, i));
}
const RUN_DIR = '/tmp/.shunt';
const RUN_ERR = RUN_DIR + '/cmd.err';
function capture_ok() {
mkdir(RUN_DIR, 0o700);
let st = lstat(RUN_DIR);
return st != null && st.type == 'directory' && st.uid == 0 &&
!st.perm.group_write && !st.perm.other_write &&
!st.perm.group_read && !st.perm.other_read;
}
function loud(argv) {
if (!capture_ok())
return { rc: quiet(argv), err: '' };
let rc = system([ '/bin/sh', '-c',
sprintf('exec "$0" "$@" 2>%s', RUN_ERR), ...argv ]);
let err = '';
if (rc != 0)
err = replace(trim(readfile(RUN_ERR) ?? ''), /\s*\n\s*/g, '; ');
unlink(RUN_ERR);
return { rc, err };
}
// quiet() drops the child's stderr, loud() keeps it for the warning. Neither
// may be called `run` - that name is the daemon's own entry point.
function quiet(argv) {
return system([ '/bin/sh', '-c', 'exec "$0" "$@" 2>/dev/null', ...argv ]);
}
function nft_pipe(batch, what) {
let fh = popen('nft -f -', 'w');
if (!fh) {
log('err', sprintf('%s: cannot spawn nft: %s', what, fs_error()));
return false;
}
fh.write(batch);
let rc = fh.close();
if (rc != 0) {
log('err', sprintf('%s: nft exited %d', what, rc));
return false;
}
return true;
}
function apply(state) {
if (!nft_pipe(state.nft.setup, 'setup'))
return false;
if (length(state.route.rt_tables)) {
mkdir('/etc/iproute2/rt_tables.d', 0o755);
if (!writefile(RT_TABLES, state.route.rt_tables))
log('warn', sprintf('cannot write %s: %s', RT_TABLES, fs_error()));
}
for (let argv in state.route.del)
quiet(argv);
let failed = 0;
let reasons = {};
for (let argv in state.route.add) {
let r = loud(argv);
if (r.rc != 0) {
let why = length(r.err) ? r.err : sprintf('exit %d', r.rc);
failed++;
reasons[why] = (reasons[why] ?? 0) + 1;
debug(sprintf('not applied: %s - %s', join(' ', argv), why));
}
}
for (let why in reasons)
log('warn', sprintf('%d of %d route/rule command(s) not applied - %s',
reasons[why], length(state.route.add), why));
if (failed)
log('warn', 'policy not applied yet - traffic falls through to main; restart once the interface is up');
return true;
}
function flush(state) {
if (state)
for (let argv in state.route.del)
quiet(argv);
nft_pipe(teardown(), 'teardown');
if (readfile(RT_TABLES) != null)
unlink(RT_TABLES);
}
function rp_read(k) {
return trim(readfile(`/proc/sys/net/ipv4/conf/${k}/rp_filter`) ?? '');
}
// Distinct, existing policy devices. Deduped so a device shared by several
// policies is set or reported once.
function policy_devices(policies) {
let seen = {}, out = [];
for (let p in (policies ?? [])) {
let dev = p.interface;
if (length(dev ?? '') && !seen[dev]) {
seen[dev] = true;
push(out, dev);
}
}
return out;
}
// Which policy devices the kernel would drop marked traffic on. rp_filter
// takes max(conf.all, conf.<dev>), so a device is blocked only when all is
// strict (1 - 0 is off, 2 is loose) AND the device is not loosened itself. A
// device with no /proc entry does not exist yet and inherits default.
function rp_filter_blocked(policies) {
if (rp_read('all') != '1')
return [];
let blocked = [];
for (let dev in policy_devices(policies)) {
let v = rp_read(dev);
if (v == '')
continue;
if (v != '2')
push(blocked, dev);
}
return blocked;
}
// With rp_filter_manage set, shunt loosens rp_filter on its own policy devices
// - the per-interface fix the README documents, done automatically. Bounded to
// exactly the devices shunt routes into, never all/default, and only on a
// device that exists. Off by default: changing a security setting is opt-in.
function rp_filter_apply(policies) {
for (let dev in policy_devices(policies))
if (rp_read(dev) != '' && rp_read(dev) != '2')
loud([ 'sysctl', '-w', sprintf('net.ipv4.conf.%s.rp_filter=2', dev) ]);
}
// Reads the live /proc value, so when rp_filter_apply has done its job the
// list is empty on its own - no need to consult the switch a second time.
function check_rp_filter(policies) {
for (let dev in rp_filter_blocked(policies))
log('warn', sprintf('rp_filter is strict on %s - shunt\'s marked traffic will be dropped there; set net.ipv4.conf.%s.rp_filter=2 or enable rp_filter_manage, see the README',
dev, dev));
}
// silent: build only what a teardown consumes and say nothing about the
// configuration - flush() needs route.del and nothing else.
function build_state(silent) {
let cfg = load_config();
if (!cfg)
return null;
if (!silent)
report('config', cfg.issues);
cfg.policies = netifd_resolve(cfg.policies, netifd_dump());
let matcher = null;
if (!silent) {
matcher = match_compile(cfg.policies);
report('domain', matcher.issues);
}
let n = nft_compile(cfg.policies);
if (!silent)
report('nft', n.issues);
let r = route_compile(cfg.policies, n.marks);
if (!silent)
report('route', r.issues);
return { cfg, matcher, nft: n, route: r };
}
// nft -f reads the entire ruleset before resolving a single name, so on a box
// with large sets from another tool one add element costs seconds of CPU.
// Writes are collected and applied by a timer whose interval follows the
// measured cost: an ordinary box stays at the floor, an expensive one backs
// off.
const WRITE_MIN = 2;
const WRITE_MAX = 60;
const WRITE_FACTOR = 3;
function queue_writes(st, writes, now) {
for (let w in writes)
if (st.state.nft.learn[w.set] && st.cache.due(w.set, w.addr, now))
st.pending[`${w.set}/${w.addr}`] = w;
}
function drain_writes(st) {
let due = values(st.pending);
st.pending = {};
if (!length(due))
return;
let r = refresh(due, st.state.cfg.global.entry_ttl);
report('refresh', r.issues);
if (!length(r.batch))
return;
let t0 = time();
let ok = nft_pipe(r.batch, 'refresh');
let cost = time() - t0;
if (ok)
debug(sprintf('%d element(s) written in %ds', length(due), cost));
let want = cost * WRITE_FACTOR;
if (want < WRITE_MIN)
want = WRITE_MIN;
if (want > WRITE_MAX)
want = WRITE_MAX;
if (want != st.interval) {
debug(sprintf('write interval %ds -> %ds (last write %ds)',
st.interval, want, cost));
st.interval = want;
st.timer.set(want * 1000);
}
}
function run() {
let uloop, resolv;
try {
uloop = require('uloop');
}
catch (e) {
log('err', 'ucode-mod-uloop missing');
return 1;
}
let state = build_state();
if (!state)
return 2;
dbg = verbose || state.cfg.global.debug;
if (!state.cfg.global.enabled) {
log('notice', 'disabled in config - running idle; enable and reload to start');
state.cfg.policies = [];
state.matcher = match_compile([]);
state.nft = nft_compile([]);
state.route = route_compile([], state.nft.marks);
}
if (!length(state.nft.marks))
log('warn', 'no usable policy - running idle; configure a policy and reload, `shunt check` shows the reasons');
if (state.cfg.global.rp_filter_manage)
rp_filter_apply(state.cfg.policies);
check_rp_filter(state.cfg.policies);
if (!apply(state)) {
flush(state);
return 1;
}
let cache = dedupe_create(state.cfg.global.entry_ttl);
let targets = poll_names(state.cfg.policies);
let stats = { started: time(), resolv: false, snoop: [],
matched: 0, drops: {} };
try {
resolv = require('resolv');
}
catch (e) {
resolv = null;
if (length(targets))
log('warn', 'ucode-mod-resolv missing - poll disabled, snoop only');
}
stats.resolv = (resolv != null);
let unresolved = {};
let wq = { state, cache, pending: {}, interval: WRITE_MIN, timer: null };
function poll_cycle() {
if (!resolv || !length(targets))
return;
let res = resolv.query(targets, { type: [ 'A', 'AAAA' ],
timeout: 2000, retries: 1 });
if (!res) {
log('warn', 'poll: query failed');
return;
}
let by = poll_index(res);
for (let name in targets) {
let got = poll_addresses(by, name);
let n = length(got.a) + length(got.aaaa);
if (n && unresolved[name]) {
unresolved[name] = false;
log('info', sprintf('poll: %s resolves again', name));
}
else if (!n && !unresolved[name]) {
let owners = state.matcher.test(name);
unresolved[name] = true;
log('warn', sprintf('poll: %s%s has no address - the policy entry has no effect until it resolves',
name, owners != null
? sprintf(' (policy %s)', join(', ', owners)) : ''));
}
}
queue_writes(wq, poll_plan(res, state.matcher, targets), time());
}
// The cache is pruned here and not at the tail of poll_cycle():
// poll_cycle() returns early without resolv, without pollable names
// and on a failed query, while snoop keeps feeding queue_writes() in
// all three cases.
function tick() {
for (let argv in state.route.add)
quiet(argv);
poll_cycle();
cache.prune(time());
}
wq.timer = uloop.interval(WRITE_MIN * 1000, () => drain_writes(wq));
poll_cycle();
uloop.interval(state.cfg.global.poll_interval * 1000, tick);
if (resolv && length(targets))
log('info', sprintf('poll: %d name(s) every %ds', length(targets),
state.cfg.global.poll_interval));
let c = ubus();
if (c) {
let pending = null;
function rebuild_routes() {
pending = null;
let resolved = netifd_resolve(state.cfg.policies, netifd_dump());
let r = route_compile(resolved, state.nft.marks);
report('route', r.issues);
// A policy device may have just appeared - the boot-time case a
// static sysctl.d file misses, since /proc/<dev> did not exist
// yet. Re-apply so it is loose from the moment it comes up, then
// re-check: with manage on the check reads the value just set
// and stays silent, without it this is the moment to warn.
if (state.cfg.global.rp_filter_manage)
rp_filter_apply(resolved);
check_rp_filter(resolved);
for (let argv in state.route.del)
quiet(argv);
for (let argv in r.add)
quiet(argv);
state.route = r;
}
c.listener('network.interface', (type, msg) => {
if (msg?.action != 'ifup' && msg?.action != 'ifdown')
return;
log('info', sprintf('%s %s - rebuilding routes',
msg.action, msg.interface ?? '?'));
if (pending)
pending.set(500);
else
pending = uloop.timer(500, rebuild_routes);
});
// Must be total: an exception in a ubus handler halts uloop and takes
// snoop, poll and the keeper down with the reply.
function status_reply() {
let names = [];
for (let m in state.nft.marks)
push(names, m.name);
return {
started: stats.started,
policies: names,
poll: {
resolv: stats.resolv,
names: length(targets),
interval: state.cfg.global.poll_interval
},
snoop: {
devices: stats.snoop,
matched: stats.matched,
drops: stats.drops
},
dedupe: cache.size()
};
}
let obj = c.publish('shunt', { status: { call: () => status_reply() } });
if (!obj)
log('warn', sprintf('cannot publish ubus object: %s',
require('ubus').error() ?? 'unknown'));
}
let socks = [];
if (state.cfg.global.snoop && length(state.nft.marks)) {
let socket = null;
for (let dev in state.cfg.global.snoop_devices) {
let s = snoop_open(dev);
if (!s.ok) {
log('err', sprintf('snoop %s: %s', dev, s.err));
continue;
}
if (socket == null)
socket = require('socket');
let sock = s.sock;
push(socks, sock);
push(stats.snoop, dev);
// Each handler closes over its own socket; binding the loop
// variable would leave them all reading the last one opened.
uloop.handle(sock, () => {
let frame;
while ((frame = sock.recv(RECV_LEN, socket.MSG_DONTWAIT)) != null) {
let v = observe(frame, state.matcher);
if (v.drop != null) {
stats.drops[v.drop] = (stats.drops[v.drop] ?? 0) + 1;
continue;
}
stats.matched++;
let writes = [];
for (let policy in v.policies) {
for (let a in v.a)
push(writes, { set: `d4_${policy}`, addr: a });
for (let a in v.aaaa)
push(writes, { set: `d6_${policy}`, addr: a });
}
debug(sprintf('snoop: %s -> %s (%d addr)',
v.qname, join(', ', v.policies), length(writes)));
queue_writes(wq, writes, time());
}
}, uloop.ULOOP_READ);
}
if (length(socks))
log('info', sprintf('snoop: listening on %s',
join(', ', stats.snoop)));
else if (!resolv || !length(targets)) {
log('err', 'neither snoop nor poll available - nothing to do');
flush(state);
return 1;
}
}
log('notice', sprintf('started: %d polic%s, mask 0x%08x',
length(state.nft.marks), length(state.nft.marks) == 1 ? 'y' : 'ies',
0xff000000));
uloop.run();
log('notice', 'stopping');
for (let sock in socks)
sock.close();
flush(state);
return 0;
}
function check() {
verbose = true;
let state = build_state();
if (!state)
return 2;
printf('global: %.2J\n', state.cfg.global);
printf('policies: %d accepted, %d mark(s)\n',
length(state.cfg.policies), length(state.nft.marks));
for (let m in state.nft.marks)
printf(' %-16s mark 0x%08x table %d pref %d\n',
m.name, m.mark, m.rt_table, m.rt_prio);
let total = length(state.matcher.issues) + length(state.nft.issues) +
length(state.route.issues);
printf('issues: %d (see above)\n', total);
printf('poll: %d name(s)\n', length(poll_names(state.cfg.policies)));
return length(state.nft.marks) ? 0 : 2;
}
let cmd = null;
for (let a in ARGV) {
if (a == '-v')
verbose = dbg = true;
else if (cmd == null)
cmd = a;
}
switch (cmd) {
case 'run':
exit(run());
case 'check':
exit(check());
case 'flush':
flush(build_state(true));
exit(0);
default:
warn('usage: shunt [-v] run|check|flush\n');
exit(2);
}
|