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
|
'use strict';
'require fs';
'require rpc';
'require tools.network as tn';
'require ui';
'require validation';
'require view';
const callNetworkInterfaceDump = rpc.declare({
object: 'network.interface',
method: 'dump',
expect: { interface: [] }
});
const callUfpList = rpc.declare({
object: 'fingerprint',
method: 'fingerprint',
expect: { '': {} }
});
function applyMask(addr, mask, v6) {
const words = v6 ? validation.parseIPv6(addr) : validation.parseIPv4(addr);
const bword = v6 ? 0xffff : 0xff;
const bwlen = v6 ? 16 : 8;
if (!words || mask < 0 || mask > (v6 ? 128 : 32))
return null;
for (let i = 0; i < words.length; i++) {
const b = Math.min(mask, bwlen);
words[i] &= (bword << (bwlen - b)) & bword;
mask -= b;
}
return String.prototype.format.apply(
v6 ? '%x:%x:%x:%x:%x:%x:%x:%x' : '%d.%d.%d.%d', words);
}
return view.extend({
load() {
return Promise.all([
callNetworkInterfaceDump(),
L.resolveDefault(fs.exec('/sbin/ip', [ '-4', '-j', 'neigh', 'show' ]), {}),
L.resolveDefault(fs.exec('/sbin/ip', [ '-4', '-j', 'route', 'show', 'table', 'all' ]), {}),
L.resolveDefault(fs.exec('/sbin/ip', [ '-4', '-j', 'rule', 'show' ]), {}),
L.resolveDefault(fs.exec('/sbin/ip', [ '-6', '-j', 'neigh', 'show' ]), {}),
L.resolveDefault(fs.exec('/sbin/ip', [ '-6', '-j', 'route', 'show', 'table', 'all' ]), {}),
L.resolveDefault(fs.exec('/sbin/ip', [ '-6', '-j', 'rule', 'show' ]), {}),
L.hasSystemFeature('ufpd') ? callUfpList() : null,
]);
},
getNetworkByDevice(networks, dev, addr, mask, v6) {
const addr_arrays = [ 'ipv4-address', 'ipv6-address', 'ipv6-prefix', 'ipv6-prefix-assignment', 'route' ];
let matching_iface = null;
let matching_prefix = -1;
for (const net of networks) {
if (!L.isObject(net) || (net.l3_device !== dev && net.device !== dev))
continue;
for (const key of addr_arrays) {
const list = net[key];
if (!Array.isArray(list)) continue;
for (const { address, target, mask: cmp_mask } of list) {
const cmp_addr = address || target;
if (!cmp_addr) continue;
if (applyMask(cmp_addr, cmp_mask, v6) !== applyMask(addr, cmp_mask, v6) || mask < cmp_mask)
continue;
if (cmp_mask > matching_prefix) {
matching_iface = net.interface;
matching_prefix = cmp_mask;
}
}
}
}
return matching_iface;
},
parseJSON(string) {
try {
return JSON.parse(string);
} catch (e) {
return [];
}
},
parseNeighbs(nbs, macs, networks, v6) {
const res = [];
for (const n of this.parseJSON(nbs)) {
let vendor;
if (n.dst.match(/^fe[89a-f][0-9a-f]:/))
continue;
if (n.state.find(f => {return f == 'FAILED'}))
continue;
for (let mac in macs) {
if (n?.lladdr === mac)
vendor = macs[mac].vendor;
}
const net = this.getNetworkByDevice(networks, n?.dev, n?.dst, v6 ? 128 : 32, v6);
res.push([
E('div', { 'data-tooltip': JSON.stringify(n) }, [
'#',
n?.nud ? `; ${_('NUD')}: ${n?.nud}` : '',
n?.proxy === null ? `; ${_('Proxy')}: ✅` : '',
n?.nomaster === null ? `; ${_('No master')} : ✅` : '',
n?.vrf ? `; ${_('VRF')}: ${n?.vrf}` : '',
]),
n?.dst,
n?.lladdr?.toUpperCase() + (vendor ? ` (${vendor})` : ''),
E('span', { 'class': 'ifacebadge' }, [ net ? net : '(%s)'.format(n?.dev) ]),
]);
}
return res;
},
parseRoutes(routes, networks, v6) {
const res = [];
for (const rt of this.parseJSON(routes)) {
const dest = rt.dst == 'default' ? (v6 ? '::/0' : '0.0.0.0/0') : rt.dst;
if (dest == 'fe80::/64' || dest == 'ff00::/8')
continue;
const [addr, bits = (v6 ? 128 : 32)] = dest.split('/');
const net = this.getNetworkByDevice(networks, rt.dev, addr, bits, v6);
res.push([
E('span', { 'class': 'ifacebadge' }, [ net ? net : '(%s)'.format(rt.dev) ]),
dest,
rt?.gateway || '-',
rt?.prefsrc || rt?.from || '-',
String(rt?.metric || '-'),
rt?.table || 'main',
rt?.protocol,
]);
}
return res;
},
parseRules(rules) {
const r = [];
for (const rl of this.parseJSON(rules)) {
r.push([
E('div', { 'data-tooltip': JSON.stringify(rl) }, [
'#',
rl?.not === null ? `; ${_('Not')}: ✅` : '',
rl?.nop === null ? `; ${_('No-op')}: ✅` : '',
rl?.l3mdev === null ? `; ${_('L3Mdev')}: ✅` : '',
rl?.fwmark ? `; ${_('Fwmark')}:${rl?.fwmark}` : '',
rl?.from ? `; ${_('From')}:${rl?.from}` : '',
rl?.to ? `; ${_('To')}:${rl?.to}` : '',
rl?.tos ? `; ${_('ToS')}:${rl?.tos}` : '',
rl?.dsfield ? `; ${_('DSCP')}:${rl?.dsfield}` : '',
rl?.uidrange ? `; ${_('UID-range')}:${rl?.uidrange}` : '',
rl?.goto ? `; ${_('goto')}:${rl?.goto}` : '',
rl?.nat ? `; ${_('NAT')}:${rl?.nat}` : '',
]),
rl?.priority,
rl?.iif ? E('span', { 'class': 'ifacebadge' }, [ rl?.iif ]) : '-',
rl?.src ? (rl?.srclen ? rl?.src + '/' + rl?.srclen : rl?.src) : _('any'),
rl?.sport || '-',
rl?.action || '-',
tn.protocols.find(f => {return f.i == rl?.ipproto?.split?.('-')[1] })?.d || '-',
rl?.oif ? E('span', { 'class': 'ifacebadge' }, [ rl?.oif ]) : '-',
rl?.dst ? (rl?.dstlen ? rl?.dst + '/' + rl?.dstlen : rl?.dst) : _('any'),
rl?.dport || '-',
rl?.table || '-',
]);
}
return r;
},
render([
networks,
{ stdout: ip4neighbs = '' } = {},
{ stdout: ip4routes = '' } = {},
{ stdout: ip4rules = '' } = {},
{ stdout: ip6neighbs = '' } = {},
{ stdout: ip6routes = '' } = {},
{ stdout: ip6rules = '' } = {},
macdata,
]) {
const device_title = _('Which is used to access this %s').format(_('Target'));
const target_title = _('Network and its mask that define the size of the destination');
const gateway_title = _('The address through which this %s is reachable').format(_('Target'));
const metric_title = _('Quantifies the cost or distance to a destination in a way that allows routers to make informed decisions about the optimal path to forward data packets');
const table_title = _('Common name or numeric ID of the %s in which this route is found').format(_('Table'));
const proto_title = _('The routing protocol identifier of this route');
const source_title = _('Network and its mask that define which source addresses use this route');
const neigh4tbl = E('table', { 'class': 'table' }, [
E('tr', { 'class': 'tr table-titles' }, [
E('th', { 'class': 'th' }, [ _('Entry') ]),
E('th', { 'class': 'th' }, [ _('IP address') ]),
E('th', { 'class': 'th' }, [ _('MAC address') ]),
E('th', { 'class': 'th' }, [ _('Interface') ]),
])
]);
const route4tbl = E('table', { 'class': 'table' }, [
E('tr', { 'class': 'tr table-titles' }, [
E('th', { 'class': 'th', 'title': device_title }, [ _('Device') ]),
E('th', { 'class': 'th', 'title': target_title }, [ _('Target') ]),
E('th', { 'class': 'th', 'title': gateway_title }, [ _('Gateway') ]),
E('th', { 'class': 'th', 'title': source_title }, [ _('Source') ]),
E('th', { 'class': 'th', 'title': metric_title }, [ _('Metric') ]),
E('th', { 'class': 'th', 'title': table_title }, [ _('Table') ]),
E('th', { 'class': 'th', 'title': proto_title }, [ _('Protocol') ]),
])
]);
const rule4tbl = E('table', { 'class': 'table' }, [
E('tr', { 'class': 'tr table-titles' }, [
E('th', { 'class': 'th' }, [ _('Rule') ]),
E('th', { 'class': 'th' }, [ _('Priority') ]),
E('th', { 'class': 'th' }, [ _('Ingress') ]),
E('th', { 'class': 'th' }, [ _('Source') ]),
E('th', { 'class': 'th' }, [ _('Src Port') ]),
E('th', { 'class': 'th' }, [ _('Action') ]),
E('th', { 'class': 'th' }, [ _('IP Protocol') ]),
E('th', { 'class': 'th' }, [ _('Egress') ]),
E('th', { 'class': 'th' }, [ _('Destination') ]),
E('th', { 'class': 'th' }, [ _('Dest Port') ]),
E('th', { 'class': 'th' }, [ _('Table') ]),
])
]);
const neigh6tbl = E('table', { 'class': 'table' }, [
E('tr', { 'class': 'tr table-titles' }, [
E('th', { 'class': 'th' }, [ _('Entry') ]),
E('th', { 'class': 'th' }, [ _('IP address') ]),
E('th', { 'class': 'th' }, [ _('MAC address') ]),
E('th', { 'class': 'th' }, [ _('Interface') ]),
])
]);
const route6tbl = E('table', { 'class': 'table' }, [
E('tr', { 'class': 'tr table-titles' }, [
E('th', { 'class': 'th', 'title': device_title }, [ _('Device') ]),
E('th', { 'class': 'th', 'title': target_title }, [ _('Target') ]),
E('th', { 'class': 'th', 'title': gateway_title }, [ _('Gateway') ]),
E('th', { 'class': 'th', 'title': source_title }, [ _('Source') ]),
E('th', { 'class': 'th', 'title': metric_title }, [ _('Metric') ]),
E('th', { 'class': 'th', 'title': table_title }, [ _('Table') ]),
E('th', { 'class': 'th', 'title': proto_title }, [ _('Protocol') ]),
])
]);
const rule6tbl = E('table', { 'class': 'table' }, [
E('tr', { 'class': 'tr table-titles' }, [
E('th', { 'class': 'th' }, [ _('Rule') ]),
E('th', { 'class': 'th' }, [ _('Priority') ]),
E('th', { 'class': 'th' }, [ _('Ingress') ]),
E('th', { 'class': 'th' }, [ _('Source') ]),
E('th', { 'class': 'th' }, [ _('Src Port') ]),
E('th', { 'class': 'th' }, [ _('Action') ]),
E('th', { 'class': 'th' }, [ _('IP Protocol') ]),
E('th', { 'class': 'th' }, [ _('Egress') ]),
E('th', { 'class': 'th' }, [ _('Destination') ]),
E('th', { 'class': 'th' }, [ _('Dest Port') ]),
E('th', { 'class': 'th' }, [ _('Table') ]),
])
]);
cbi_update_table(neigh4tbl, this.parseNeighbs(ip4neighbs, macdata, networks, false),
E('em', _('No entries available'))
);
cbi_update_table(route4tbl, this.parseRoutes(ip4routes, networks, false),
E('em', _('No entries available'))
);
cbi_update_table(rule4tbl, this.parseRules(ip4rules),
E('em', _('No entries available'))
);
cbi_update_table(neigh6tbl, this.parseNeighbs(ip6neighbs, macdata, networks, true),
E('em', _('No entries available'))
);
cbi_update_table(route6tbl, this.parseRoutes(ip6routes, networks, true),
E('em', _('No entries available'))
);
cbi_update_table(rule6tbl, this.parseRules(ip6rules),
E('em', _('No entries available'))
);
const view = E([], [
E('h2', {}, [ _('Routing') ]),
E('p', {}, [ _('The following rules are currently active on this system.') ]),
E('div', {}, [
E('div', { 'class': 'cbi-section', 'data-tab': 'ipv4routing', 'data-tab-title': _('IPv4 Routing') }, [
E('h3', {}, [ _('IPv4 Neighbours') ]),
neigh4tbl,
E('h3', {}, [ _('Active IPv4 Routes') ]),
route4tbl,
E('h3', {}, [ _('Active IPv4 Rules') ]),
rule4tbl,
]),
E('div', { 'class': 'cbi-section', 'data-tab': 'ipv6routing', 'data-tab-title': _('IPv6 Routing') }, [
E('h3', {}, [ _('IPv6 Neighbours') ]),
neigh6tbl,
E('h3', {}, [ _('Active IPv6 Routes') ]),
route6tbl,
E('h3', {}, [ _('Active IPv6 Rules') ]),
rule6tbl,
])
])
]);
ui.tabs.initTabGroup(view.lastElementChild.childNodes);
return view;
},
handleSaveApply: null,
handleSave: null,
handleReset: null
});
|