7 'require firewall as fwmodel';
8 'require tools.firewall as fwtool';
9 'require tools.widgets as widgets';
11 function rule_proto_txt(s
, ctHelpers
) {
12 var f
= (uci
.get('firewall', s
, 'family') || '').toLowerCase().replace(/^(?:any|\*)$/, '');
14 var proto
= L
.toArray(uci
.get('firewall', s
, 'proto')).filter(function(p
) {
15 return (p
!= '*' && p
!= 'any' && p
!= 'all');
17 var pr
= fwtool
.lookupProto(p
);
21 types
: (pr
[0] == 1 || pr
[0] == 58) ? L
.toArray(uci
.get('firewall', s
, 'icmp_type')) : null
25 m
= String(uci
.get('firewall', s
, 'helper') || '').match(/^(!\s*)?(\S+)$/);
27 val
: m
[0].toUpperCase(),
29 name
: (ctHelpers
.filter(function(ctH
) { return ctH
.name
.toLowerCase() == m
[2].toLowerCase() })[0] || {}).description
32 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);
34 val
: m
[0].toUpperCase().replace(/X
/g
, 'x'),
36 num
: '0x%02X'.format(+m
[2]),
37 mask
: m
[3] ? '0x%02X'.format(+m
[3]) : null
40 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}))$/);
45 num
: m
[3] ? '0x%02X'.format(+m
[3]) : null
48 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>}}'), {
49 ipv4
: (!f
|| f
== 'ipv4'),
50 ipv6
: (!f
|| f
== 'ipv6'),
51 src
: uci
.get('firewall', s
, 'src'),
52 dest
: uci
.get('firewall', s
, 'dest'),
60 function rule_src_txt(s
, hosts
) {
61 var z
= uci
.get('firewall', s
, 'src'),
62 d
= (uci
.get('firewall', s
, 'direction') == 'in') ? uci
.get('firewall', s
, 'device') : null;
64 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>}}'), {
65 src
: E('span', { 'class': 'zonebadge', 'style': fwmodel
.getZoneColorStyle(z
) }, [(z
== '*') ? E('em', _('any zone')) : (z
? E('strong', z
) : E('em', _('this device')))]),
66 src_ip
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_ip'), 'toLowerCase'),
67 src_mac
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_mac'), 'toUpperCase').map(function(v
) { return Object
.assign(v
, { hint
: hosts
[v
.val
] }) }),
68 src_port
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_port')),
73 function rule_dest_txt(s
) {
74 var z
= uci
.get('firewall', s
, 'dest'),
75 d
= (uci
.get('firewall', s
, 'direction') == 'out') ? uci
.get('firewall', s
, 'device') : null;
77 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>}}'), {
78 dest
: E('span', { 'class': 'zonebadge', 'style': fwmodel
.getZoneColorStyle(z
) }, [(z
== '*') ? E('em', _('any zone')) : (z
? E('strong', z
) : E('em', _('this device')))]),
79 dest_ip
: fwtool
.map_invert(uci
.get('firewall', s
, 'dest_ip'), 'toLowerCase'),
80 dest_port
: fwtool
.map_invert(uci
.get('firewall', s
, 'dest_port')),
85 function rule_limit_txt(s
) {
86 var m
= String(uci
.get('firewall', s
, 'limit')).match(/^(\d+)\/([smhd])\w*$/i),
89 unit
: ({ s
: _('second'), m
: _('minute'), h
: _('hour'), d
: _('day') })[m
[2]],
90 burst
: uci
.get('firewall', s
, 'limit_burst')
96 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
});
99 function rule_target_txt(s
, ctHelpers
) {
100 var t
= uci
.get('firewall', s
, 'target'),
101 h
= (uci
.get('firewall', s
, 'set_helper') || '').toUpperCase(),
104 src
: uci
.get('firewall', s
, 'src'),
105 dest
: uci
.get('firewall', s
, 'dest'),
107 set_mark
: uci
.get('firewall', s
, 'set_mark'),
108 set_xmark
: uci
.get('firewall', s
, 'set_xmark'),
109 set_dscp
: uci
.get('firewall', s
, 'set_dscp'),
110 helper_name
: (ctHelpers
.filter(function(ctH
) { return ctH
.name
.toUpperCase() == h
})[0] || {}).description
115 return fwtool
.fmt(_('<var data-tooltip="DROP">Drop</var> %{src?%{dest?forward:input}:output}'), s
);
118 return fwtool
.fmt(_('<var data-tooltip="ACCEPT">Accept</var> %{src?%{dest?forward:input}:output}'), s
);
121 return fwtool
.fmt(_('<var data-tooltip="REJECT">Reject</var> %{src?%{dest?forward:input}:output}'), s
);
124 return fwtool
.fmt(_('<var data-tooltip="NOTRACK">Do not track</var> %{src?%{dest?forward:input}:output}'), s
);
127 return fwtool
.fmt(_('<var data-tooltip="HELPER">Assign conntrack</var> helper <var%{helper_name? data-tooltip="%{helper_name}"}>%{set_helper}</var>'), s
);
130 return fwtool
.fmt(_('<var data-tooltip="MARK">%{set_mark?Assign:XOR}</var> firewall mark <var>%{set_mark?:%{set_xmark}}</var>'), s
);
133 return fwtool
.fmt(_('<var data-tooltip="DSCP">Assign DSCP</var> classification <var>%{set_dscp}</var>'), s
);
141 callHostHints
: rpc
.declare({
143 method
: 'getHostHints',
147 callConntrackHelpers
: rpc
.declare({
149 method
: 'getConntrackHelpers',
150 expect
: { result
: [] }
155 this.callHostHints(),
156 this.callConntrackHelpers(),
161 render: function(data
) {
162 if (fwtool
.checkLegacySNAT())
163 return fwtool
.renderMigration();
165 return this.renderRules(data
);
168 renderRules: function(data
) {
173 m
= new form
.Map('firewall', _('Firewall - Traffic Rules'),
174 _('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.'));
176 s
= m
.section(form
.GridSection
, 'rule', _('Traffic Rules'));
181 s
.tab('general', _('General Settings'));
182 s
.tab('advanced', _('Advanced Settings'));
183 s
.tab('timed', _('Time Restrictions'));
185 s
.filter = function(section_id
) {
186 return (uci
.get('firewall', section_id
, 'target') != 'SNAT');
189 s
.sectiontitle = function(section_id
) {
190 return uci
.get('firewall', section_id
, 'name') || _('Unnamed rule');
193 s
.handleAdd = function(ev
) {
194 var config_name
= this.uciconfig
|| this.map
.config
,
195 section_id
= uci
.add(config_name
, this.sectiontype
),
196 opt1
= this.getOption('src'),
197 opt2
= this.getOption('dest');
199 opt1
.default = 'wan';
200 opt2
.default = 'lan';
202 this.addedSection
= section_id
;
203 this.renderMoreOptionsModal(section_id
);
209 o
= s
.taboption('general', form
.Value
, 'name', _('Name'));
210 o
.placeholder
= _('Unnamed rule');
213 o
= s
.option(form
.DummyValue
, '_match', _('Match'));
215 o
.textvalue = function(s
) {
217 rule_proto_txt(s
, ctHelpers
), E('br'),
218 rule_src_txt(s
, hosts
), E('br'),
219 rule_dest_txt(s
), E('br'),
224 o
= s
.option(form
.ListValue
, '_target', _('Action'));
226 o
.textvalue = function(s
) {
227 return rule_target_txt(s
, ctHelpers
);
230 o
= s
.option(form
.Flag
, 'enabled', _('Enable'));
232 o
.default = o
.enabled
;
234 o
.tooltip = function(section_id
) {
235 var weekdays
= uci
.get('firewall', section_id
, 'weekdays');
236 var monthdays
= uci
.get('firewall', section_id
, 'monthdays');
237 var start_time
= uci
.get('firewall', section_id
, 'start_time');
238 var stop_time
= uci
.get('firewall', section_id
, 'stop_time');
239 var start_date
= uci
.get('firewall', section_id
, 'start_date');
240 var stop_date
= uci
.get('firewall', section_id
, 'stop_date');
242 if (weekdays
|| monthdays
|| start_time
|| stop_time
|| start_date
|| stop_date
)
243 return _('Time restritions are enabled for this rule');
248 o
= s
.taboption('advanced', form
.ListValue
, 'direction', _('Match device'));
250 o
.value('', _('unspecified'));
251 o
.value('in', _('Inbound device'));
252 o
.value('out', _('Outbound device'));
253 o
.cfgvalue = function(section_id
) {
254 var val
= uci
.get('firewall', section_id
, 'direction');
268 o
= s
.taboption('advanced', widgets
.DeviceSelect
, 'device', _('Device name'),
269 _('Specifies whether to tie this traffic rule to a specific inbound or outbound network device.'));
273 o
.depends('direction', 'in');
274 o
.depends('direction', 'out');
276 o
= s
.taboption('advanced', form
.ListValue
, 'family', _('Restrict to address family'));
279 o
.value('', _('IPv4 and IPv6'));
280 o
.value('ipv4', _('IPv4 only'));
281 o
.value('ipv6', _('IPv6 only'));
282 o
.validate = function(section_id
, value
) {
283 fwtool
.updateHostHints(this.map
, section_id
, 'src_ip', value
, hosts
);
284 fwtool
.updateHostHints(this.map
, section_id
, 'dest_ip', value
, hosts
);
288 o
= s
.taboption('general', fwtool
.CBIProtocolSelect
, 'proto', _('Protocol'));
290 o
.default = 'tcp udp';
292 o
= s
.taboption('advanced', form
.MultiValue
, 'icmp_type', _('Match ICMP type'));
297 o
.placeholder
= _('any');
299 o
.value('address-mask-reply');
300 o
.value('address-mask-request');
301 o
.value('address-unreachable'); /* ipv6 */
302 o
.value('bad-header'); /* ipv6 */
303 o
.value('communication-prohibited');
304 o
.value('destination-unreachable');
305 o
.value('echo-reply');
306 o
.value('echo-request');
307 o
.value('fragmentation-needed');
308 o
.value('host-precedence-violation');
309 o
.value('host-prohibited');
310 o
.value('host-redirect');
311 o
.value('host-unknown');
312 o
.value('host-unreachable');
313 o
.value('ip-header-bad');
314 o
.value('neighbour-advertisement');
315 o
.value('neighbour-solicitation');
316 o
.value('network-prohibited');
317 o
.value('network-redirect');
318 o
.value('network-unknown');
319 o
.value('network-unreachable');
320 o
.value('no-route'); /* ipv6 */
321 o
.value('packet-too-big');
322 o
.value('parameter-problem');
323 o
.value('port-unreachable');
324 o
.value('precedence-cutoff');
325 o
.value('protocol-unreachable');
327 o
.value('required-option-missing');
328 o
.value('router-advertisement');
329 o
.value('router-solicitation');
330 o
.value('source-quench');
331 o
.value('source-route-failed');
332 o
.value('time-exceeded');
333 o
.value('timestamp-reply');
334 o
.value('timestamp-request');
335 o
.value('TOS-host-redirect');
336 o
.value('TOS-host-unreachable');
337 o
.value('TOS-network-redirect');
338 o
.value('TOS-network-unreachable');
339 o
.value('ttl-zero-during-reassembly');
340 o
.value('ttl-zero-during-transit');
341 o
.value('unknown-header-type'); /* ipv6 */
342 o
.value('unknown-option'); /* ipv6 */
343 o
.depends({ proto
: 'icmp', '!contains': true });
344 o
.depends({ proto
: 'icmpv6', '!contains': true });
346 o
= s
.taboption('general', widgets
.ZoneSelect
, 'src', _('Source zone'));
350 o
.allowlocal
= 'src';
352 fwtool
.addMACOption(s
, 'advanced', 'src_mac', _('Source MAC address'), null, hosts
);
353 fwtool
.addIPOption(s
, 'general', 'src_ip', _('Source address'), null, '', hosts
, true);
355 o
= s
.taboption('general', form
.Value
, 'src_port', _('Source port'));
357 o
.datatype
= 'list(neg(portrange))';
358 o
.placeholder
= _('any');
359 o
.depends({ proto
: 'tcp', '!contains': true });
360 o
.depends({ proto
: 'udp', '!contains': true });
362 o
= s
.taboption('general', widgets
.ZoneSelect
, 'dest', _('Destination zone'));
368 fwtool
.addIPOption(s
, 'general', 'dest_ip', _('Destination address'), null, '', hosts
, true);
370 o
= s
.taboption('general', form
.Value
, 'dest_port', _('Destination port'));
372 o
.datatype
= 'list(neg(portrange))';
373 o
.placeholder
= _('any');
374 o
.depends({ proto
: 'tcp', '!contains': true });
375 o
.depends({ proto
: 'udp', '!contains': true });
377 o
= s
.taboption('general', form
.ListValue
, 'target', _('Action'));
379 o
.default = 'ACCEPT';
380 o
.value('DROP', _('drop'));
381 o
.value('ACCEPT', _('accept'));
382 o
.value('REJECT', _('reject'));
383 o
.value('NOTRACK', _("don't track"));
384 o
.value('HELPER', _('assign conntrack helper'));
385 o
.value('MARK_SET', _('apply firewall mark'));
386 o
.value('MARK_XOR', _('XOR firewall mark'));
387 o
.value('DSCP', _('DSCP classification'));
388 o
.cfgvalue = function(section_id
) {
389 var t
= uci
.get('firewall', section_id
, 'target'),
390 m
= uci
.get('firewall', section_id
, 'set_mark');
393 return m
? 'MARK_SET' : 'MARK_XOR';
397 o
.write = function(section_id
, value
) {
398 return this.super('write', [section_id
, (value
== 'MARK_SET' || value
== 'MARK_XOR') ? 'MARK' : value
]);
401 fwtool
.addMarkOption(s
, 1);
402 fwtool
.addMarkOption(s
, 2);
403 fwtool
.addDSCPOption(s
, true);
405 o
= s
.taboption('general', form
.ListValue
, 'set_helper', _('Tracking helper'), _('Assign the specified connection tracking helper to matched traffic.'));
407 o
.placeholder
= _('any');
408 o
.depends('target', 'HELPER');
409 for (var i
= 0; i
< ctHelpers
.length
; i
++)
410 o
.value(ctHelpers
[i
].name
, '%s (%s)'.format(ctHelpers
[i
].description
, ctHelpers
[i
].name
.toUpperCase()));
412 o
= s
.taboption('advanced', form
.Value
, 'helper', _('Match helper'), _('Match traffic using the specified connection tracking helper.'));
414 o
.placeholder
= _('any');
415 for (var i
= 0; i
< ctHelpers
.length
; i
++)
416 o
.value(ctHelpers
[i
].name
, '%s (%s)'.format(ctHelpers
[i
].description
, ctHelpers
[i
].name
.toUpperCase()));
417 o
.validate = function(section_id
, value
) {
418 if (value
== '' || value
== null)
421 value
= value
.replace(/^!\s*/, '');
423 for (var i
= 0; i
< ctHelpers
.length
; i
++)
424 if (value
== ctHelpers
[i
].name
)
427 return _('Unknown or not installed conntrack helper "%s"').format(value
);
430 fwtool
.addMarkOption(s
, false);
431 fwtool
.addDSCPOption(s
, false);
432 fwtool
.addLimitOption(s
);
433 fwtool
.addLimitBurstOption(s
);
435 if (!L
.hasSystemFeature('firewall4')) {
436 o
= s
.taboption('advanced', form
.Value
, 'extra', _('Extra arguments'),
437 _('Passes additional arguments to iptables. Use with care!'));
441 o
= s
.taboption('timed', form
.MultiValue
, 'weekdays', _('Week Days'));
445 o
.placeholder
= _('Any day');
446 o
.value('Sun', _('Sunday'));
447 o
.value('Mon', _('Monday'));
448 o
.value('Tue', _('Tuesday'));
449 o
.value('Wed', _('Wednesday'));
450 o
.value('Thu', _('Thursday'));
451 o
.value('Fri', _('Friday'));
452 o
.value('Sat', _('Saturday'));
453 o
.write = function(section_id
, value
) {
454 return this.super('write', [ section_id
, L
.toArray(value
).join(' ') ]);
457 o
= s
.taboption('timed', form
.MultiValue
, 'monthdays', _('Month Days'));
461 o
.placeholder
= _('Any day');
462 o
.write = function(section_id
, value
) {
463 return this.super('write', [ section_id
, L
.toArray(value
).join(' ') ]);
465 for (var i
= 1; i
<= 31; i
++)
468 o
= s
.taboption('timed', form
.Value
, 'start_time', _('Start Time (hh:mm:ss)'));
470 o
.datatype
= 'timehhmmss';
472 o
= s
.taboption('timed', form
.Value
, 'stop_time', _('Stop Time (hh:mm:ss)'));
474 o
.datatype
= 'timehhmmss';
476 o
= s
.taboption('timed', form
.Value
, 'start_date', _('Start Date (yyyy-mm-dd)'));
478 o
.datatype
= 'dateyyyymmdd';
480 o
= s
.taboption('timed', form
.Value
, 'stop_date', _('Stop Date (yyyy-mm-dd)'));
482 o
.datatype
= 'dateyyyymmdd';
484 o
= s
.taboption('timed', form
.Flag
, 'utc_time', _('Time in UTC'));
486 o
.default = o
.disabled
;