6 'require firewall as fwmodel';
7 'require tools.firewall as fwtool';
8 'require tools.widgets as widgets';
10 function rule_proto_txt(s, ctHelpers) {
11 var f = (uci.get('firewall', s, 'family') || '').toLowerCase().replace(/^(?:any|\*)$/, '');
13 var proto = L.toArray(uci.get('firewall', s, 'proto')).filter(function(p) {
14 return (p != '*' && p != 'any' && p != 'all');
16 var pr = fwtool.lookupProto(p);
20 types: (pr[0] == 1 || pr[0] == 58) ? L.toArray(uci.get('firewall', s, 'icmp_type')) : null
24 m = String(uci.get('firewall', s, 'helper') || '').match(/^(!\s*)?(\S+)$/);
26 val: m[0].toUpperCase(),
28 name: (ctHelpers.filter(function(ctH) { return ctH.name.toLowerCase() == m[2].toLowerCase() })[0] || {}).description
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);
33 val: m[0].toUpperCase().replace(/X/g, 'x'),
35 num: '0x%02X'.format(+m[2]),
36 mask: m[3] ? '0x%02X'.format(+m[3]) : null
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}))$/);
44 num: m[3] ? '0x%02X'.format(+m[3]) : null
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 "%{helper.name}"">%{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'),
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;
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')),
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;
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')),
84 function rule_limit_txt(s) {
85 var m = String(uci.get('firewall', s, 'limit')).match(/^(\d+)\/([smhd])\w*$/i),
88 unit: ({ s: _('second'), m: _('minute'), h: _('hour'), d: _('day') })[m[2]],
89 burst: uci.get('firewall', s, 'limit_burst')
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 });
98 function rule_target_txt(s, ctHelpers) {
99 var t = uci.get('firewall', s, 'target'),
100 h = (uci.get('firewall', s, 'set_helper') || '').toUpperCase(),
103 src: uci.get('firewall', s, 'src'),
104 dest: uci.get('firewall', s, 'dest'),
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
114 return fwtool.fmt(_('<var data-tooltip="DROP">Drop</var> %{src?%{dest?forward:input}:output}'), s);
117 return fwtool.fmt(_('<var data-tooltip="ACCEPT">Accept</var> %{src?%{dest?forward:input}:output}'), s);
120 return fwtool.fmt(_('<var data-tooltip="REJECT">Reject</var> %{src?%{dest?forward:input}:output}'), s);
123 return fwtool.fmt(_('<var data-tooltip="NOTRACK">Do not track</var> %{src?%{dest?forward:input}:output}'), s);
126 return fwtool.fmt(_('<var data-tooltip="HELPER">Assign conntrack</var> helper <var%{helper_name? data-tooltip="%{helper_name}"}>%{set_helper}</var>'), s);
129 return fwtool.fmt(_('<var data-tooltip="MARK">%{set_mark?Assign:XOR}</var> firewall mark <var>%{set_mark?:%{set_xmark}}</var>'), s);
132 return fwtool.fmt(_('<var data-tooltip="DSCP">Assign DSCP</var> classification <var>%{set_dscp}</var>'), s);
139 return L.view.extend({
140 callHostHints: rpc.declare({
142 method: 'getHostHints',
146 callConntrackHelpers: rpc.declare({
148 method: 'getConntrackHelpers',
149 expect: { result: [] }
154 this.callHostHints(),
155 this.callConntrackHelpers(),
160 render: function(data) {
161 if (fwtool.checkLegacySNAT())
162 return fwtool.renderMigration();
164 return this.renderRules(data);
167 renderRules: function(data) {
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.'));
175 s = m.section(form.GridSection, 'rule', _('Traffic Rules'));
180 s.tab('general', _('General Settings'));
181 s.tab('advanced', _('Advanced Settings'));
182 s.tab('timed', _('Time Restrictions'));
184 s.filter = function(section_id) {
185 return (uci.get('firewall', section_id, 'target') != 'SNAT');
188 s.sectiontitle = function(section_id) {
189 return uci.get('firewall', section_id, 'name') || _('Unnamed rule');
192 s.handleAdd = function(ev) {
193 var config_name = this.uciconfig || this.map.config,
194 section_id = uci.add(config_name, this.sectiontype),
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];
203 opt1.default = 'wan';
204 opt2.default = 'lan';
206 this.addedSection = section_id;
207 this.renderMoreOptionsModal(section_id);
213 o = s.taboption('general', form.Value, 'name', _('Name'));
214 o.placeholder = _('Unnamed rule');
217 o = s.option(form.DummyValue, '_match', _('Match'));
219 o.textvalue = function(s) {
221 rule_proto_txt(s, ctHelpers), E('br'),
222 rule_src_txt(s, hosts), E('br'),
223 rule_dest_txt(s), E('br'),
228 o = s.option(form.ListValue, '_target', _('Action'));
230 o.textvalue = function(s) {
231 return rule_target_txt(s, ctHelpers);
234 o = s.option(form.Flag, 'enabled', _('Enable'));
236 o.default = o.enabled;
240 o = s.taboption('advanced', form.ListValue, 'direction', _('Match device'));
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');
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.'));
265 o.depends('direction', 'in');
266 o.depends('direction', 'out');
268 o = s.taboption('advanced', form.ListValue, 'family', _('Restrict to address family'));
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);
280 o = s.taboption('general', fwtool.CBIProtocolSelect, 'proto', _('Protocol'));
282 o.default = 'tcp udp';
284 o = s.taboption('advanced', form.MultiValue, 'icmp_type', _('Match ICMP type'));
289 o.placeholder = _('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');
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 });
332 o = s.taboption('general', widgets.ZoneSelect, 'src', _('Source zone'));
336 o.allowlocal = 'src';
338 fwtool.addMACOption(s, 'advanced', 'src_mac', _('Source MAC address'), null, hosts);
339 fwtool.addIPOption(s, 'general', 'src_ip', _('Source address'), null, '', hosts, true);
341 o = s.taboption('general', form.Value, 'src_port', _('Source port'));
343 o.datatype = 'list(neg(portrange))';
344 o.placeholder = _('any');
345 o.depends({ proto: 'tcp', '!contains': true });
346 o.depends({ proto: 'udp', '!contains': true });
348 o = s.taboption('general', widgets.ZoneSelect, 'dest', _('Destination zone'));
354 fwtool.addIPOption(s, 'general', 'dest_ip', _('Destination address'), null, '', hosts, true);
356 o = s.taboption('general', form.Value, 'dest_port', _('Destination port'));
358 o.datatype = 'list(neg(portrange))';
359 o.placeholder = _('any');
360 o.depends({ proto: 'tcp', '!contains': true });
361 o.depends({ proto: 'udp', '!contains': true });
363 o = s.taboption('general', form.ListValue, 'target', _('Action'));
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');
379 return m ? 'MARK_SET' : 'MARK_XOR';
383 o.write = function(section_id, value) {
384 return this.super('write', [section_id, (value == 'MARK_SET' || value == 'MARK_XOR') ? 'MARK' : value]);
387 fwtool.addMarkOption(s, 1);
388 fwtool.addMarkOption(s, 2);
389 fwtool.addDSCPOption(s, true);
391 o = s.taboption('general', form.ListValue, 'set_helper', _('Tracking helper'), _('Assign the specified connection tracking helper to matched traffic.'));
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()));
398 o = s.taboption('advanced', form.Value, 'helper', _('Match helper'), _('Match traffic using the specified connection tracking helper.'));
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)
407 value = value.replace(/^!\s*/, '');
409 for (var i = 0; i < ctHelpers.length; i++)
410 if (value == ctHelpers[i].name)
413 return _('Unknown or not installed conntrack helper "%s"').format(value);
416 fwtool.addMarkOption(s, false);
417 fwtool.addDSCPOption(s, false);
418 fwtool.addLimitOption(s);
419 fwtool.addLimitBurstOption(s);
421 o = s.taboption('advanced', form.Value, 'extra', _('Extra arguments'),
422 _('Passes additional arguments to iptables. Use with care!'));
425 o = s.taboption('timed', form.MultiValue, 'weekdays', _('Week Days'));
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(' ') ]);
441 o = s.taboption('timed', form.MultiValue, 'monthdays', _('Month Days'));
445 o.placeholder = _('Any day');
446 o.write = function(section_id, value) {
447 return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
449 for (var i = 1; i <= 31; i++)
452 o = s.taboption('timed', form.Value, 'start_time', _('Start Time (hh.mm.ss)'));
454 o.datatype = 'timehhmmss';
456 o = s.taboption('timed', form.Value, 'stop_time', _('Stop Time (hh.mm.ss)'));
458 o.datatype = 'timehhmmss';
460 o = s.taboption('timed', form.Value, 'start_date', _('Start Date (yyyy-mm-dd)'));
462 o.datatype = 'dateyyyymmdd';
464 o = s.taboption('timed', form.Value, 'stop_date', _('Stop Date (yyyy-mm-dd)'));
466 o.datatype = 'dateyyyymmdd';
468 o = s.taboption('timed', form.Flag, 'utc_time', _('Time in UTC'));
470 o.default = o.disabled;