summaryrefslogtreecommitdiffstats
path: root/net/shunt/files/shunt.rpcd
blob: c4d51179243ad52c92469ac3a336d336c288e1f0 (plain)
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
// shunt - rpcd backend for the LuCI frontend
//
// Stateless: renders the configuration through the same modules the daemon
// uses and reads the rest from the kernel. The daemon is asked only for the
// facts nothing outside its process can see.
//
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2026 Dirk Brenken <dev@brenken.org>

import { popen, readfile } from 'fs';

const ubus = require('ubus');
const rtnl = require('rtnl');

// The rtnl constants hang off a `const` sub-object, not off the module - the
// example in lib/rtnl.c's own header says otherwise and yields null.
const RT = rtnl.const;

import { load as config_load, parse as config_parse } from 'shunt.config';
import { resolve as netifd_resolve } from 'shunt.netifd';
import { compile as match_compile } from 'shunt.match';
import { compile as nft_compile } from 'shunt.nft';
import { compile as route_compile } from 'shunt.route';
import { names as poll_names } from 'shunt.poll';

const TABLE_FAMILY = 'inet';
const TABLE_NAME = 'shunt';

function daemon_status() {
	let conn = ubus.connect();

	if (!conn)
		return null;

	let r = conn.call('shunt', 'status');

	return r ?? null;
}

// The same sequence as the daemon's build_state(), minus the logging: two
// readings of one config file are how a status view starts to lie.
function render() {
	let sections = config_load();

	if (sections == null)
		return null;

	let cfg = config_parse(sections);

	if (!cfg)
		return null;

	let dump = null;
	let conn = ubus.connect();

	if (conn)
		dump = conn.call('network.interface', 'dump');

	cfg.policies = netifd_resolve(cfg.policies, dump);

	let m = match_compile(cfg.policies);
	let n = nft_compile(cfg.policies);
	let r = route_compile(cfg.policies, n.marks);

	return { cfg, matcher: m, nft: n, route: r };
}

// Policy devices whose marked traffic the kernel would drop: max(all, <dev>),
// blocked only when all is strict (1) and the device is not loose itself. Same
// logic as the daemon. When rp_filter_manage is on the daemon has already set
// these to 2, so this reads back empty on its own.
function rp_filter_blocked(policies) {
	let rp = (k) => trim(readfile(`/proc/sys/net/ipv4/conf/${k}/rp_filter`) ?? '');

	if (rp('all') != '1')
		return [];

	let seen = {}, blocked = [];

	for (let p in (policies ?? [])) {
		let dev = p.interface;

		if (!length(dev ?? '') || seen[dev])
			continue;

		seen[dev] = true;

		let v = rp(dev);

		// Absent device: no traffic, nothing dropped - not blocked. The
		// daemon re-checks on ifup when it appears.
		if (v == '')
			continue;

		if (v != '2')
			push(blocked, dev);
	}

	return blocked;
}

// Same check the daemon logs, surfaced for the UI: which of all/default carry
// strict reverse path filtering, which drops shunt's asymmetric traffic.
function rp_filter_strict() {
	let strict = [];

	for (let key in [ 'all', 'default' ])
		if (trim(readfile(`/proc/sys/net/ipv4/conf/${key}/rp_filter`) ?? '') == '1')
			push(strict, key);

	return strict;
}

function nft_table() {
	let fh = popen(sprintf('nft -j list table %s %s 2>/dev/null',
		TABLE_FAMILY, TABLE_NAME), 'r');

	if (!fh)
		return null;

	let out = fh.read('all');

	fh.close();

	if (!length(out ?? ''))
		return null;

	let j = json(out);

	return j?.nftables ? j : null;
}

function nft_sets(table) {
	let out = {};

	for (let item in (table?.nftables ?? [])) {
		let s = item?.set;

		if (!s?.name)
			continue;

		let elems = [];

		for (let e in (s.elem ?? [])) {
			let v = e?.elem ?? e;
			let val = v?.val ?? v;

			push(elems, {
				addr: (type(val) == 'object') ? (val.prefix ? sprintf('%s/%d', val.prefix.addr, val.prefix.len) : null) : val,
				expires: v?.expires,
				packets: v?.counter?.packets,
				bytes: v?.counter?.bytes
			});
		}

		out[s.name] = elems;
	}

	return out;
}

function kernel_rules(marks) {
	let want = {};

	for (let m in (marks ?? []))
		want[sprintf('%d', m.mark)] = m.name;

	let res = rtnl.request(RT.RTM_GETRULE, RT.NLM_F_DUMP,
		{ family: RT.AF_UNSPEC });

	if (res == null)
		return null;

	let out = {};

	for (let r in res) {
		if (r?.fwmark == null)
			continue;

		let name = want[sprintf('%d', r.fwmark)];

		if (!name)
			continue;

		if (!out[name])
			out[name] = [];

		push(out[name], {
			family: r.family,
			priority: r.priority,
			table: r.table,
			fwmark: r.fwmark,
			fwmask: r.fwmask
		});
	}

	return out;
}

function kernel_routes(marks) {
	let out = {};

	for (let m in (marks ?? [])) {
		let n = 0;

		for (let fam in [ RT.AF_INET, RT.AF_INET6 ]) {
			let res = rtnl.request(RT.RTM_GETROUTE, RT.NLM_F_DUMP,
				{ family: fam, table: m.rt_table });

			if (res == null) {
				n = null;
				break;
			}

			for (let r in res)
				if (r?.table == m.rt_table)
					n++;
		}

		out[m.name] = n;
	}

	return out;
}

return {
	'luci.shunt': {

		status: {
			args: {},
			call: function(req) {
				let st = render();

				if (!st)
					return { error: 'cannot read /etc/config/shunt' };

				let svc = daemon_status();
				let table = nft_table();
				let rules = kernel_rules(st.nft.marks);
				let routes = kernel_routes(st.nft.marks);
				let policies = [];

				for (let m in st.nft.marks) {
					let p = null;

					for (let c in st.cfg.policies)
						if (c.name == m.name)
							p = c;

					push(policies, {
						name: m.name,
						mark: m.mark,
						rt_table: m.rt_table,
						rt_prio: m.rt_prio,
						interface: p?.interface,
						fallback: p?.fallback,
						domains: length(p?.domains ?? []),
						rules: rules ? length(rules[m.name] ?? []) : null,
						routes: routes[m.name]
					});
				}

				return {
					running: (svc != null),
					applied: (table != null),
					service: svc,
					global: st.cfg.global,
					policies,
					poll_names: length(poll_names(st.cfg.policies)),
					rp_filter_blocked: rp_filter_blocked(st.cfg.policies),
					rp_filter_strict: rp_filter_strict(),
					issues: [
						...st.cfg.issues,
						...st.matcher.issues,
						...st.nft.issues,
						...st.route.issues
					]
				};
			}
		},

		sets: {
			args: { policy: '' },
			call: function(req) {
				let table = nft_table();

				if (!table)
					return { sets: {} };

				let all = nft_sets(table);
				let want = req.args?.policy;

				if (!length(want ?? ''))
					return { sets: all };

				let out = {};

				for (let name in all) {
					let at = (substr(name, 0, 1) == 'm') ? 2 : 3;

					if (substr(name, at) == want)
						out[name] = all[name];
				}

				return { sets: out };
			}
		}
	}
};