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
|
'use strict';
'require ui';
'require rpc';
'require uci';
'require form';
'require tools.firewall as fwtool';
'require tools.widgets as widgets';
function fmt(fmt /*, ...*/) {
var repl = [], wrap = false;
for (var i = 1; i < arguments.length; i++) {
if (L.dom.elem(arguments[i])) {
switch (arguments[i].nodeType) {
case 1:
repl.push(arguments[i].outerHTML);
wrap = true;
break;
case 3:
repl.push(arguments[i].data);
break;
case 11:
var span = E('span');
span.appendChild(arguments[i]);
repl.push(span.innerHTML);
wrap = true;
break;
default:
repl.push('');
}
}
else {
repl.push(arguments[i]);
}
}
var rv = fmt.format.apply(fmt, repl);
return wrap ? E('span', rv) : rv;
}
function forward_proto_txt(s) {
return fmt('%s-%s',
fwtool.fmt_family(uci.get('firewall', s, 'family')),
fwtool.fmt_proto(uci.get('firewall', s, 'proto'),
uci.get('firewall', s, 'icmp_type')) || 'TCP+UDP');
}
function rule_src_txt(s) {
var z = fwtool.fmt_zone(uci.get('firewall', s, 'src')),
p = fwtool.fmt_port(uci.get('firewall', s, 'src_port')),
m = fwtool.fmt_mac(uci.get('firewall', s, 'src_mac'));
// Forward/Input
if (z) {
var a = fwtool.fmt_ip(uci.get('firewall', s, 'src_ip'), _('any host'));
if (p && m)
return fmt(_('From %s in %s with source %s and %s'), a, z, p, m);
else if (p || m)
return fmt(_('From %s in %s with source %s'), a, z, p || m);
else
return fmt(_('From %s in %s'), a, z);
}
// Output
else {
var a = fwtool.fmt_ip(uci.get('firewall', s, 'src_ip'), _('any router IP'));
if (p && m)
return fmt(_('From %s on <var>this device</var> with source %s and %s'), a, p, m);
else if (p || m)
return fmt(_('From %s on <var>this device</var> with source %s'), a, p || m);
else
return fmt(_('From %s on <var>this device</var>'), a);
}
}
function rule_dest_txt(s) {
var z = fwtool.fmt_zone(uci.get('firewall', s, 'dest')),
p = fwtool.fmt_port(uci.get('firewall', s, 'dest_port'));
// Forward
if (z) {
var a = fwtool.fmt_ip(uci.get('firewall', s, 'dest_ip'), _('any host'));
if (p)
return fmt(_('To %s, %s in %s'), a, p, z);
else
return fmt(_('To %s in %s'), a, z);
}
// Input
else {
var a = fwtool.fmt_ip(uci.get('firewall', s, 'dest_ip'), _('any router IP'));
if (p)
return fmt(_('To %s at %s on <var>this device</var>'), a, p);
else
return fmt(_('To %s on <var>this device</var>'), a);
}
}
function rule_target_txt(s) {
var t = fwtool.fmt_target(uci.get('firewall', s, 'target'), uci.get('firewall', s, 'src'), uci.get('firewall', s, 'dest')),
l = fwtool.fmt_limit(uci.get('firewall', s, 'limit'), uci.get('firewall', s, 'limit_burst'));
if (l)
return fmt(_('<var>%s</var> and limit to %s'), t, l);
else
return fmt('<var>%s</var>', t);
}
function update_ip_hints(map, section_id, family, hosts) {
var elem_src_ip = map.lookupOption('src_ip', section_id)[0].getUIElement(section_id),
elem_dst_ip = map.lookupOption('dest_ip', section_id)[0].getUIElement(section_id),
choice_values = [], choice_labels = {};
elem_src_ip.clearChoices();
elem_dst_ip.clearChoices();
if (!family || family == 'ipv4') {
L.sortedKeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
var val = hosts[mac].ipv4,
txt = '%s (<strong>%s</strong>)'.format(val, hosts[mac].name || mac);
choice_values.push(val);
choice_labels[val] = txt;
});
}
if (!family || family == 'ipv6') {
L.sortedKeys(hosts, 'ipv6', 'addr').forEach(function(mac) {
var val = hosts[mac].ipv6,
txt = '%s (<strong>%s</strong>)'.format(val, hosts[mac].name || mac);
choice_values.push(val);
choice_labels[val] = txt;
});
}
elem_src_ip.addChoices(choice_values, choice_labels);
elem_dst_ip.addChoices(choice_values, choice_labels);
}
return L.view.extend({
callHostHints: rpc.declare({
object: 'luci-rpc',
method: 'getHostHints',
expect: { '': {} }
}),
load: function() {
return this.callHostHints().catch(function(e) {
console.debug('load fail', e);
});
},
render: function(hosts) {
var m, s, o;
m = new form.Map('firewall', _('Firewall - Traffic Rules'),
_('Traffic rules define policies for packets traveling between different zones, for example to reject traffic between certain hosts or to open WAN ports on the router.'));
s = m.section(form.GridSection, 'rule', _('Traffic Rules'));
s.addremove = true;
s.anonymous = true;
s.sortable = true;
s.tab('general', _('General Settings'));
s.tab('advanced', _('Advanced Settings'));
s.tab('timed', _('Time Restrictions'));
s.filter = function(section_id) {
return (uci.get('firewall', section_id, 'target') != 'SNAT');
};
s.sectiontitle = function(section_id) {
return uci.get('firewall', section_id, 'name') || _('Unnamed rule');
};
s.handleAdd = function(ev) {
var config_name = this.uciconfig || this.map.config,
section_id = uci.add(config_name, this.sectiontype),
opt1, opt2;
for (var i = 0; i < this.children.length; i++)
if (this.children[i].option == 'src')
opt1 = this.children[i];
else if (this.children[i].option == 'dest')
opt2 = this.children[i];
opt1.default = 'wan';
opt2.default = 'lan';
this.addedSection = section_id;
this.renderMoreOptionsModal(section_id);
delete opt1.default;
delete opt2.default;
};
o = s.taboption('general', form.Value, 'name', _('Name'));
o.placeholder = _('Unnamed rule');
o.modalonly = true;
o = s.option(form.DummyValue, '_match', _('Match'));
o.modalonly = false;
o.textvalue = function(s) {
return E('small', [
forward_proto_txt(s), E('br'),
rule_src_txt(s), E('br'),
rule_dest_txt(s)
]);
};
o = s.option(form.ListValue, '_target', _('Action'));
o.modalonly = false;
o.textvalue = function(s) {
return rule_target_txt(s);
};
o = s.option(form.Flag, 'enabled', _('Enable'));
o.modalonly = false;
o.default = o.enabled;
o.editable = true;
//ft.opt_enabled(s, Button);
//ft.opt_name(s, Value, _('Name'));
o = s.taboption('advanced', form.ListValue, 'family', _('Restrict to address family'));
o.modalonly = true;
o.rmempty = true;
o.value('', _('IPv4 and IPv6'));
o.value('ipv4', _('IPv4 only'));
o.value('ipv6', _('IPv6 only'));
o.validate = function(section_id, value) {
update_ip_hints(this.map, section_id, value, hosts);
return true;
};
o = s.taboption('general', form.Value, 'proto', _('Protocol'));
o.modalonly = true;
o.default = 'tcp udp';
o.value('all', _('Any'));
o.value('tcp udp', 'TCP+UDP');
o.value('tcp', 'TCP');
o.value('udp', 'UDP');
o.value('icmp', 'ICMP');
o.cfgvalue = function(/* ... */) {
var v = this.super('cfgvalue', arguments);
return (v == 'tcpudp') ? 'tcp udp' : v;
};
o = s.taboption('advanced', form.MultiValue, 'icmp_type', _('Match ICMP type'));
o.modalonly = true;
o.multiple = true;
o.custom = true;
o.cast = 'table';
o.placeholder = _('any');
o.value('', 'any');
o.value('address-mask-reply');
o.value('address-mask-request');
o.value('communication-prohibited');
o.value('destination-unreachable');
o.value('echo-reply');
o.value('echo-request');
o.value('fragmentation-needed');
o.value('host-precedence-violation');
o.value('host-prohibited');
o.value('host-redirect');
o.value('host-unknown');
o.value('host-unreachable');
o.value('ip-header-bad');
o.value('neighbour-advertisement');
o.value('neighbour-solicitation');
o.value('network-prohibited');
o.value('network-redirect');
o.value('network-unknown');
o.value('network-unreachable');
o.value('parameter-problem');
o.value('port-unreachable');
o.value('precedence-cutoff');
o.value('protocol-unreachable');
o.value('redirect');
o.value('required-option-missing');
o.value('router-advertisement');
o.value('router-solicitation');
o.value('source-quench');
o.value('source-route-failed');
o.value('time-exceeded');
o.value('timestamp-reply');
o.value('timestamp-request');
o.value('TOS-host-redirect');
o.value('TOS-host-unreachable');
o.value('TOS-network-redirect');
o.value('TOS-network-unreachable');
o.value('ttl-zero-during-reassembly');
o.value('ttl-zero-during-transit');
o.depends('proto', 'icmp');
o = s.taboption('general', widgets.ZoneSelect, 'src', _('Source zone'));
o.modalonly = true;
o.nocreate = true;
o.allowany = true;
o.allowlocal = 'src';
o = s.taboption('advanced', form.Value, 'src_mac', _('Source MAC address'));
o.modalonly = true;
o.datatype = 'list(macaddr)';
o.placeholder = _('any');
L.sortedKeys(hosts).forEach(function(mac) {
o.value(mac, '%s (%s)'.format(
mac,
hosts[mac].name || hosts[mac].ipv4 || hosts[mac].ipv6 || '?'
));
});
o = s.taboption('general', form.Value, 'src_ip', _('Source address'));
o.modalonly = true;
o.datatype = 'list(neg(ipmask))';
o.placeholder = _('any');
o.transformChoices = function() { return {} }; /* force combobox rendering */
o = s.taboption('general', form.Value, 'src_port', _('Source port'));
o.modalonly = true;
o.datatype = 'list(neg(portrange))';
o.placeholder = _('any');
o.depends('proto', 'tcp');
o.depends('proto', 'udp');
o.depends('proto', 'tcp udp');
o.depends('proto', 'tcpudp');
o = s.taboption('general', widgets.ZoneSelect, 'dest', _('Destination zone'));
o.modalonly = true;
o.nocreate = true;
o.allowany = true;
o.allowlocal = true;
o = s.taboption('general', form.Value, 'dest_ip', _('Destination address'));
o.modalonly = true;
o.datatype = 'list(neg(ipmask))';
o.placeholder = _('any');
o.transformChoices = function() { return {} }; /* force combobox rendering */
o = s.taboption('general', form.Value, 'dest_port', _('Destination port'));
o.modalonly = true;
o.datatype = 'list(neg(portrange))';
o.placeholder = _('any');
o.depends('proto', 'tcp');
o.depends('proto', 'udp');
o.depends('proto', 'tcp udp');
o.depends('proto', 'tcpudp');
o = s.taboption('general', form.ListValue, 'target', _('Action'));
o.modalonly = true;
o.default = 'ACCEPT';
o.value('DROP', _('drop'));
o.value('ACCEPT', _('accept'));
o.value('REJECT', _('reject'));
o.value('NOTRACK', _("don't track"));
o = s.taboption('advanced', form.Value, 'extra', _('Extra arguments'),
_('Passes additional arguments to iptables. Use with care!'));
o.modalonly = true;
o = s.taboption('timed', form.MultiValue, 'weekdays', _('Week Days'));
o.modalonly = true;
o.multiple = true;
o.display = 5;
o.placeholder = _('Any day');
o.value('Sun', _('Sunday'));
o.value('Mon', _('Monday'));
o.value('Tue', _('Tuesday'));
o.value('Wed', _('Wednesday'));
o.value('Thu', _('Thursday'));
o.value('Fri', _('Friday'));
o.value('Sat', _('Saturday'));
o.write = function(section_id, value) {
return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
};
o = s.taboption('timed', form.MultiValue, 'monthdays', _('Month Days'));
o.modalonly = true;
o.multiple = true;
o.display_size = 15;
o.placeholder = _('Any day');
o.write = function(section_id, value) {
return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
};
for (var i = 1; i <= 31; i++)
o.value(i);
o = s.taboption('timed', form.Value, 'start_time', _('Start Time (hh.mm.ss)'));
o.modalonly = true;
o.datatype = 'timehhmmss';
o = s.taboption('timed', form.Value, 'stop_time', _('Stop Time (hh.mm.ss)'));
o.modalonly = true;
o.datatype = 'timehhmmss';
o = s.taboption('timed', form.Value, 'start_date', _('Start Date (yyyy-mm-dd)'));
o.modalonly = true;
o.datatype = 'dateyyyymmdd';
o = s.taboption('timed', form.Value, 'stop_date', _('Stop Date (yyyy-mm-dd)'));
o.modalonly = true;
o.datatype = 'dateyyyymmdd';
o = s.taboption('timed', form.Flag, 'utc_time', _('Time in UTC'));
o.modalonly = true;
o.default = o.disabled;
return m.render();
}
});
|