b68f428d952fc41855a72fb0d8af79788e559bae
[project/luci.git] / applications / luci-app-firewall / htdocs / luci-static / resources / view / firewall / rules.js
1 'use strict';
2 'require ui';
3 'require rpc';
4 'require uci';
5 'require form';
6 'require firewall as fwmodel';
7 'require tools.firewall as fwtool';
8 'require tools.widgets as widgets';
9
10 function rule_proto_txt(s, ctHelpers) {
11 var f = (uci.get('firewall', s, 'family') || '').toLowerCase().replace(/^(?:any|\*)$/, '');
12
13 var proto = L.toArray(uci.get('firewall', s, 'proto')).filter(function(p) {
14 return (p != '*' && p != 'any' && p != 'all');
15 }).map(function(p) {
16 var pr = fwtool.lookupProto(p);
17 return {
18 num: pr[0],
19 name: pr[1],
20 types: (pr[0] == 1 || pr[0] == 58) ? L.toArray(uci.get('firewall', s, 'icmp_type')) : null
21 };
22 });
23
24 m = String(uci.get('firewall', s, 'helper') || '').match(/^(!\s*)?(\S+)$/);
25 var h = m ? {
26 val: m[0].toUpperCase(),
27 inv: m[1],
28 name: (ctHelpers.filter(function(ctH) { return ctH.name.toLowerCase() == m[2].toLowerCase() })[0] || {}).description
29 } : null;
30
31 m = String(uci.get('firewall', s, 'mark')).match(/^(!\s*)?(0x[0-9a-f]{1,8}|[0-9]{1,10})(?:\/(0x[0-9a-f]{1,8}|[0-9]{1,10}))?$/i);
32 var f = m ? {
33 val: m[0].toUpperCase().replace(/X/g, 'x'),
34 inv: m[1],
35 num: '0x%02X'.format(+m[2]),
36 mask: m[3] ? '0x%02X'.format(+m[3]) : null
37 } : null;
38
39 m = String(uci.get('firewall', s, 'dscp')).match(/^(!\s*)?(?:(CS[0-7]|BE|AF[1234][123]|EF)|(0x[0-9a-f]{1,2}|[0-9]{1,2}))$/);
40 var d = m ? {
41 val: m[0],
42 inv: m[1],
43 name: m[2],
44 num: m[3] ? '0x%02X'.format(+m[3]) : null
45 } : null;
46
47 return fwtool.fmt(_('%{src?%{dest?Forwarded:Incoming}:Outgoing} %{ipv6?%{ipv4?<var>IPv4</var> and <var>IPv6</var>:<var>IPv6</var>}:<var>IPv4</var>}%{proto?, protocol %{proto#%{next?, }%{item.types?<var class="cbi-tooltip-container">%{item.name}<span class="cbi-tooltip">ICMP with types %{item.types#%{next?, }<var>%{item}</var>}</span></var>:<var>%{item.name}</var>}}}%{mark?, mark <var%{mark.inv? data-tooltip="Match fwmarks except %{mark.num}%{mark.mask? with mask %{mark.mask}}.":%{mark.mask? data-tooltip="Mask fwmark value with %{mark.mask} before compare."}}>%{mark.val}</var>}%{dscp?, DSCP %{dscp.inv?<var data-tooltip="Match DSCP classifications except %{dscp.num?:%{dscp.name}}">%{dscp.val}</var>:<var>%{dscp.val}</var>}}%{helper?, helper %{helper.inv?<var data-tooltip="Match any helper except &quot;%{helper.name}&quot;">%{helper.val}</var>:<var data-tooltip="%{helper.name}">%{helper.val}</var>}}'), {
48 ipv4: (!f || f == 'ipv4'),
49 ipv6: (!f || f == 'ipv6'),
50 src: uci.get('firewall', s, 'src'),
51 dest: uci.get('firewall', s, 'dest'),
52 proto: proto,
53 helper: h,
54 mark: f,
55 dscp: d
56 });
57 }
58
59 function rule_src_txt(s, hosts) {
60 var z = uci.get('firewall', s, 'src'),
61 d = (uci.get('firewall', s, 'direction') == 'in') ? uci.get('firewall', s, 'device') : null;
62
63 return fwtool.fmt(_('From %{src}%{src_device?, interface <var>%{src_device}</var>}%{src_ip?, IP %{src_ip#%{next?, }<var%{item.inv? data-tooltip="Match IP addresses except %{item.val}."}>%{item.ival}</var>}}%{src_port?, port %{src_port#%{next?, }<var%{item.inv? data-tooltip="Match ports except %{item.val}."}>%{item.ival}</var>}}%{src_mac?, MAC %{src_mac#%{next?, }<var%{item.inv? data-tooltip="Match MACs except %{item.val}%{item.hint.name? a.k.a. %{item.hint.name}}.":%{item.hint.name? data-tooltip="%{item.hint.name}"}}>%{item.ival}</var>}}'), {
64 src: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel.getColorForName((z && z != '*') ? z : null) }, [(z == '*') ? E('em', _('any zone')) : (z || E('em', _('this device')))]),
65 src_ip: fwtool.map_invert(uci.get('firewall', s, 'src_ip'), 'toLowerCase'),
66 src_mac: fwtool.map_invert(uci.get('firewall', s, 'src_mac'), 'toUpperCase').map(function(v) { return Object.assign(v, { hint: hosts[v.val] }) }),
67 src_port: fwtool.map_invert(uci.get('firewall', s, 'src_port')),
68 src_device: d
69 });
70 }
71
72 function rule_dest_txt(s) {
73 var z = uci.get('firewall', s, 'dest'),
74 d = (uci.get('firewall', s, 'direction') == 'out') ? uci.get('firewall', s, 'device') : null;
75
76 return fwtool.fmt(_('To %{dest}%{dest_device?, interface <var>%{dest_device}</var>}%{dest_ip?, IP %{dest_ip#%{next?, }<var%{item.inv? data-tooltip="Match IP addresses except %{item.val}."}>%{item.ival}</var>}}%{dest_port?, port %{dest_port#%{next?, }<var%{item.inv? data-tooltip="Match ports except %{item.val}."}>%{item.ival}</var>}}'), {
77 dest: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel.getColorForName((z && z != '*') ? z : null) }, [(z == '*') ? E('em', _('any zone')) : (z || E('em', _('this device')))]),
78 dest_ip: fwtool.map_invert(uci.get('firewall', s, 'dest_ip'), 'toLowerCase'),
79 dest_port: fwtool.map_invert(uci.get('firewall', s, 'dest_port')),
80 dest_device: d
81 });
82 }
83
84 function rule_limit_txt(s) {
85 var m = String(uci.get('firewall', s, 'limit')).match(/^(\d+)\/([smhd])\w*$/i),
86 l = m ? {
87 num: +m[1],
88 unit: ({ s: _('second'), m: _('minute'), h: _('hour'), d: _('day') })[m[2]],
89 burst: uci.get('firewall', s, 'limit_burst')
90 } : null;
91
92 if (!l)
93 return '';
94
95 return fwtool.fmt(_('Limit matching to <var>%{limit.num}</var> packets per <var>%{limit.unit}</var>%{limit.burst? burst <var>%{limit.burst}</var>}'), { limit: l });
96 }
97
98 function rule_target_txt(s, ctHelpers) {
99 var t = uci.get('firewall', s, 'target'),
100 h = (uci.get('firewall', s, 'set_helper') || '').toUpperCase(),
101 s = {
102 target: t,
103 src: uci.get('firewall', s, 'src'),
104 dest: uci.get('firewall', s, 'dest'),
105 set_helper: h,
106 set_mark: uci.get('firewall', s, 'set_mark'),
107 set_xmark: uci.get('firewall', s, 'set_xmark'),
108 set_dscp: uci.get('firewall', s, 'set_dscp'),
109 helper_name: (ctHelpers.filter(function(ctH) { return ctH.name.toUpperCase() == h })[0] || {}).description
110 };
111
112 switch (t) {
113 case 'DROP':
114 return fwtool.fmt(_('<var data-tooltip="DROP">Drop</var> %{src?%{dest?forward:input}:output}'), s);
115
116 case 'ACCEPT':
117 return fwtool.fmt(_('<var data-tooltip="ACCEPT">Accept</var> %{src?%{dest?forward:input}:output}'), s);
118
119 case 'REJECT':
120 return fwtool.fmt(_('<var data-tooltip="REJECT">Reject</var> %{src?%{dest?forward:input}:output}'), s);
121
122 case 'NOTRACK':
123 return fwtool.fmt(_('<var data-tooltip="NOTRACK">Do not track</var> %{src?%{dest?forward:input}:output}'), s);
124
125 case 'HELPER':
126 return fwtool.fmt(_('<var data-tooltip="HELPER">Assign conntrack</var> helper <var%{helper_name? data-tooltip="%{helper_name}"}>%{set_helper}</var>'), s);
127
128 case 'MARK':
129 return fwtool.fmt(_('<var data-tooltip="MARK">%{set_mark?Assign:XOR}</var> firewall mark <var>%{set_mark?:%{set_xmark}}</var>'), s);
130
131 case 'DSCP':
132 return fwtool.fmt(_('<var data-tooltip="DSCP">Assign DSCP</var> classification <var>%{set_dscp}</var>'), s);
133
134 default:
135 return t;
136 }
137 }
138
139 return L.view.extend({
140 callHostHints: rpc.declare({
141 object: 'luci-rpc',
142 method: 'getHostHints',
143 expect: { '': {} }
144 }),
145
146 callConntrackHelpers: rpc.declare({
147 object: 'luci',
148 method: 'getConntrackHelpers',
149 expect: { result: [] }
150 }),
151
152 load: function() {
153 return Promise.all([
154 this.callHostHints(),
155 this.callConntrackHelpers(),
156 uci.load('firewall')
157 ]);
158 },
159
160 render: function(data) {
161 if (fwtool.checkLegacySNAT())
162 return fwtool.renderMigration();
163 else
164 return this.renderRules(data);
165 },
166
167 renderRules: function(data) {
168 var hosts = data[0],
169 ctHelpers = data[1],
170 m, s, o;
171
172 m = new form.Map('firewall', _('Firewall - Traffic Rules'),
173 _('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.'));
174
175 s = m.section(form.GridSection, 'rule', _('Traffic Rules'));
176 s.addremove = true;
177 s.anonymous = true;
178 s.sortable = true;
179
180 s.tab('general', _('General Settings'));
181 s.tab('advanced', _('Advanced Settings'));
182 s.tab('timed', _('Time Restrictions'));
183
184 s.filter = function(section_id) {
185 return (uci.get('firewall', section_id, 'target') != 'SNAT');
186 };
187
188 s.sectiontitle = function(section_id) {
189 return uci.get('firewall', section_id, 'name') || _('Unnamed rule');
190 };
191
192 s.handleAdd = function(ev) {
193 var config_name = this.uciconfig || this.map.config,
194 section_id = uci.add(config_name, this.sectiontype),
195 opt1, opt2;
196
197 for (var i = 0; i < this.children.length; i++)
198 if (this.children[i].option == 'src')
199 opt1 = this.children[i];
200 else if (this.children[i].option == 'dest')
201 opt2 = this.children[i];
202
203 opt1.default = 'wan';
204 opt2.default = 'lan';
205
206 this.addedSection = section_id;
207 this.renderMoreOptionsModal(section_id);
208
209 delete opt1.default;
210 delete opt2.default;
211 };
212
213 o = s.taboption('general', form.Value, 'name', _('Name'));
214 o.placeholder = _('Unnamed rule');
215 o.modalonly = true;
216
217 o = s.option(form.DummyValue, '_match', _('Match'));
218 o.modalonly = false;
219 o.textvalue = function(s) {
220 return E('small', [
221 rule_proto_txt(s, ctHelpers), E('br'),
222 rule_src_txt(s, hosts), E('br'),
223 rule_dest_txt(s), E('br'),
224 rule_limit_txt(s)
225 ]);
226 };
227
228 o = s.option(form.ListValue, '_target', _('Action'));
229 o.modalonly = false;
230 o.textvalue = function(s) {
231 return rule_target_txt(s, ctHelpers);
232 };
233
234 o = s.option(form.Flag, 'enabled', _('Enable'));
235 o.modalonly = false;
236 o.default = o.enabled;
237 o.editable = true;
238
239
240 o = s.taboption('advanced', form.ListValue, 'direction', _('Match device'));
241 o.modalonly = true;
242 o.value('', _('unspecified'));
243 o.value('in', _('Inbound device'));
244 o.value('out', _('Outbound device'));
245 o.cfgvalue = function(section_id) {
246 var val = uci.get('firewall', section_id, 'direction');
247 switch (val) {
248 case 'in':
249 case 'ingress':
250 return 'in';
251
252 case 'out':
253 case 'egress':
254 return 'out';
255 }
256
257 return null;
258 };
259
260 o = s.taboption('advanced', widgets.DeviceSelect, 'device', _('Device name'),
261 _('Specifies whether to tie this traffic rule to a specific inbound or outbound network device.'));
262 o.modalonly = true;
263 o.noaliases = true;
264 o.rmempty = false;
265 o.depends('direction', 'in');
266 o.depends('direction', 'out');
267
268 o = s.taboption('advanced', form.ListValue, 'family', _('Restrict to address family'));
269 o.modalonly = true;
270 o.rmempty = true;
271 o.value('', _('IPv4 and IPv6'));
272 o.value('ipv4', _('IPv4 only'));
273 o.value('ipv6', _('IPv6 only'));
274 o.validate = function(section_id, value) {
275 fwtool.updateHostHints(this.map, section_id, 'src_ip', value, hosts);
276 fwtool.updateHostHints(this.map, section_id, 'dest_ip', value, hosts);
277 return true;
278 };
279
280 o = s.taboption('general', fwtool.CBIProtocolSelect, 'proto', _('Protocol'));
281 o.modalonly = true;
282 o.default = 'tcp udp';
283
284 o = s.taboption('advanced', form.MultiValue, 'icmp_type', _('Match ICMP type'));
285 o.modalonly = true;
286 o.multiple = true;
287 o.custom = true;
288 o.cast = 'table';
289 o.placeholder = _('any');
290 o.value('', 'any');
291 o.value('address-mask-reply');
292 o.value('address-mask-request');
293 o.value('communication-prohibited');
294 o.value('destination-unreachable');
295 o.value('echo-reply');
296 o.value('echo-request');
297 o.value('fragmentation-needed');
298 o.value('host-precedence-violation');
299 o.value('host-prohibited');
300 o.value('host-redirect');
301 o.value('host-unknown');
302 o.value('host-unreachable');
303 o.value('ip-header-bad');
304 o.value('neighbour-advertisement');
305 o.value('neighbour-solicitation');
306 o.value('network-prohibited');
307 o.value('network-redirect');
308 o.value('network-unknown');
309 o.value('network-unreachable');
310 o.value('parameter-problem');
311 o.value('port-unreachable');
312 o.value('precedence-cutoff');
313 o.value('protocol-unreachable');
314 o.value('redirect');
315 o.value('required-option-missing');
316 o.value('router-advertisement');
317 o.value('router-solicitation');
318 o.value('source-quench');
319 o.value('source-route-failed');
320 o.value('time-exceeded');
321 o.value('timestamp-reply');
322 o.value('timestamp-request');
323 o.value('TOS-host-redirect');
324 o.value('TOS-host-unreachable');
325 o.value('TOS-network-redirect');
326 o.value('TOS-network-unreachable');
327 o.value('ttl-zero-during-reassembly');
328 o.value('ttl-zero-during-transit');
329 o.depends({ proto: 'icmp', '!contains': true });
330 o.depends({ proto: 'icmpv6', '!contains': true });
331
332 o = s.taboption('general', widgets.ZoneSelect, 'src', _('Source zone'));
333 o.modalonly = true;
334 o.nocreate = true;
335 o.allowany = true;
336 o.allowlocal = 'src';
337
338 fwtool.addMACOption(s, 'advanced', 'src_mac', _('Source MAC address'), null, hosts);
339 fwtool.addIPOption(s, 'general', 'src_ip', _('Source address'), null, '', hosts, true);
340
341 o = s.taboption('general', form.Value, 'src_port', _('Source port'));
342 o.modalonly = true;
343 o.datatype = 'list(neg(portrange))';
344 o.placeholder = _('any');
345 o.depends({ proto: 'tcp', '!contains': true });
346 o.depends({ proto: 'udp', '!contains': true });
347
348 o = s.taboption('general', widgets.ZoneSelect, 'dest', _('Destination zone'));
349 o.modalonly = true;
350 o.nocreate = true;
351 o.allowany = true;
352 o.allowlocal = true;
353
354 fwtool.addIPOption(s, 'general', 'dest_ip', _('Destination address'), null, '', hosts, true);
355
356 o = s.taboption('general', form.Value, 'dest_port', _('Destination port'));
357 o.modalonly = true;
358 o.datatype = 'list(neg(portrange))';
359 o.placeholder = _('any');
360 o.depends({ proto: 'tcp', '!contains': true });
361 o.depends({ proto: 'udp', '!contains': true });
362
363 o = s.taboption('general', form.ListValue, 'target', _('Action'));
364 o.modalonly = true;
365 o.default = 'ACCEPT';
366 o.value('DROP', _('drop'));
367 o.value('ACCEPT', _('accept'));
368 o.value('REJECT', _('reject'));
369 o.value('NOTRACK', _("don't track"));
370 o.value('HELPER', _('assign conntrack helper'));
371 o.value('MARK_SET', _('apply firewall mark'));
372 o.value('MARK_XOR', _('XOR firewall mark'));
373 o.value('DSCP', _('DSCP classification'));
374 o.cfgvalue = function(section_id) {
375 var t = uci.get('firewall', section_id, 'target'),
376 m = uci.get('firewall', section_id, 'set_mark');
377
378 if (t == 'MARK')
379 return m ? 'MARK_SET' : 'MARK_XOR';
380
381 return t;
382 };
383 o.write = function(section_id, value) {
384 return this.super('write', [section_id, (value == 'MARK_SET' || value == 'MARK_XOR') ? 'MARK' : value]);
385 };
386
387 fwtool.addMarkOption(s, 1);
388 fwtool.addMarkOption(s, 2);
389 fwtool.addDSCPOption(s, true);
390
391 o = s.taboption('general', form.ListValue, 'set_helper', _('Tracking helper'), _('Assign the specified connection tracking helper to matched traffic.'));
392 o.modalonly = true;
393 o.placeholder = _('any');
394 o.depends('target', 'HELPER');
395 for (var i = 0; i < ctHelpers.length; i++)
396 o.value(ctHelpers[i].name, '%s (%s)'.format(ctHelpers[i].description, ctHelpers[i].name.toUpperCase()));
397
398 o = s.taboption('advanced', form.Value, 'helper', _('Match helper'), _('Match traffic using the specified connection tracking helper.'));
399 o.modalonly = true;
400 o.placeholder = _('any');
401 for (var i = 0; i < ctHelpers.length; i++)
402 o.value(ctHelpers[i].name, '%s (%s)'.format(ctHelpers[i].description, ctHelpers[i].name.toUpperCase()));
403 o.validate = function(section_id, value) {
404 if (value == '' || value == null)
405 return true;
406
407 value = value.replace(/^!\s*/, '');
408
409 for (var i = 0; i < ctHelpers.length; i++)
410 if (value == ctHelpers[i].name)
411 return true;
412
413 return _('Unknown or not installed conntrack helper "%s"').format(value);
414 };
415
416 fwtool.addMarkOption(s, false);
417 fwtool.addDSCPOption(s, false);
418 fwtool.addLimitOption(s);
419 fwtool.addLimitBurstOption(s);
420
421 o = s.taboption('advanced', form.Value, 'extra', _('Extra arguments'),
422 _('Passes additional arguments to iptables. Use with care!'));
423 o.modalonly = true;
424
425 o = s.taboption('timed', form.MultiValue, 'weekdays', _('Week Days'));
426 o.modalonly = true;
427 o.multiple = true;
428 o.display = 5;
429 o.placeholder = _('Any day');
430 o.value('Sun', _('Sunday'));
431 o.value('Mon', _('Monday'));
432 o.value('Tue', _('Tuesday'));
433 o.value('Wed', _('Wednesday'));
434 o.value('Thu', _('Thursday'));
435 o.value('Fri', _('Friday'));
436 o.value('Sat', _('Saturday'));
437 o.write = function(section_id, value) {
438 return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
439 };
440
441 o = s.taboption('timed', form.MultiValue, 'monthdays', _('Month Days'));
442 o.modalonly = true;
443 o.multiple = true;
444 o.display_size = 15;
445 o.placeholder = _('Any day');
446 o.write = function(section_id, value) {
447 return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
448 };
449 for (var i = 1; i <= 31; i++)
450 o.value(i);
451
452 o = s.taboption('timed', form.Value, 'start_time', _('Start Time (hh.mm.ss)'));
453 o.modalonly = true;
454 o.datatype = 'timehhmmss';
455
456 o = s.taboption('timed', form.Value, 'stop_time', _('Stop Time (hh.mm.ss)'));
457 o.modalonly = true;
458 o.datatype = 'timehhmmss';
459
460 o = s.taboption('timed', form.Value, 'start_date', _('Start Date (yyyy-mm-dd)'));
461 o.modalonly = true;
462 o.datatype = 'dateyyyymmdd';
463
464 o = s.taboption('timed', form.Value, 'stop_date', _('Stop Date (yyyy-mm-dd)'));
465 o.modalonly = true;
466 o.datatype = 'dateyyyymmdd';
467
468 o = s.taboption('timed', form.Flag, 'utc_time', _('Time in UTC'));
469 o.modalonly = true;
470 o.default = o.disabled;
471
472 return m.render();
473 }
474 });