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 family
= (uci
.get('firewall', s
, 'family') || '').toLowerCase().replace(/^(?:any|\*)$/, '');
13 var dip
= uci
.get('firewall', s
, 'dest_ip') || ''
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 return fwtool
.fmt(_('Incoming %{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>}%{helper?, helper %{helper.inv?<var data-tooltip="Match any helper except "%{helper.name}"">%{helper.val}</var>:<var data-tooltip="%{helper.name}">%{helper.val}</var>}}'), {
41 ipv4
: ((!family
&& (dip
.indexOf(':') == -1)) || family
== 'ipv4'),
42 ipv6
: ((!family
&& (dip
.indexOf(':') != -1)) || family
== 'ipv6'),
49 function rule_src_txt(s
, hosts
) {
50 var z
= uci
.get('firewall', s
, 'src');
52 return fwtool
.fmt(_('From %{src}%{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>}}'), {
53 src
: E('span', { 'class': 'zonebadge', 'style': fwmodel
.getZoneColorStyle(z
) }, [(z
== '*') ? E('em', _('any zone')) : (z
? E('strong', z
) : E('em', _('this device')))]),
54 src_ip
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_ip'), 'toLowerCase'),
55 src_mac
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_mac'), 'toUpperCase').map(function(v
) { return Object
.assign(v
, { hint
: hosts
[v
.val
] }) }),
56 src_port
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_port'))
60 function rule_dest_txt(s
) {
61 return fwtool
.fmt(_('To %{dest}%{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>}}'), {
62 dest
: E('span', { 'class': 'zonebadge', 'style': fwmodel
.getZoneColorStyle(null) }, [E('em', _('this device'))]),
63 dest_ip
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_dip'), 'toLowerCase'),
64 dest_port
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_dport'))
68 function rule_limit_txt(s
) {
69 var m
= String(uci
.get('firewall', s
, 'limit')).match(/^(\d+)\/([smhd])\w*$/i),
72 unit
: ({ s
: _('second'), m
: _('minute'), h
: _('hour'), d
: _('day') })[m
[2]],
73 burst
: uci
.get('firewall', s
, 'limit_burst')
79 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
});
82 function rule_target_txt(s
) {
83 var z
= uci
.get('firewall', s
, 'dest');
85 return fwtool
.fmt(_('<var data-tooltip="DNAT">Forward</var> to %{dest}%{dest_ip? IP <var>%{dest_ip}</var>}%{dest_port? port <var>%{dest_port}</var>}'), {
86 dest
: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel
.getColorForName((z
&& z
!= '*') ? z
: null) }, [(z
== '*') ? E('em', _('any zone')) : (z
? E('strong', z
) : E('em', _('this device')))]),
87 dest_ip
: (uci
.get('firewall', s
, 'dest_ip') || '').toLowerCase(),
88 dest_port
: uci
.get('firewall', s
, 'dest_port')
92 function validate_opt_family(m
, section_id
, opt
) {
93 var dopt
= m
.section
.getOption('dest_ip'),
94 fmopt
= m
.section
.getOption('family');
96 if (!dopt
.isValid(section_id
) && opt
!= 'dest_ip')
98 if (!fmopt
.isValid(section_id
) && opt
!= 'family')
101 var dip
= dopt
.formvalue(section_id
) || '',
102 fm
= fmopt
.formvalue(section_id
) || '';
104 if (fm
== '' || (fm
== 'ipv6' && (dip
.indexOf(':') != -1 || dip
== '')) || (fm
== 'ipv4' && dip
.indexOf(':') == -1))
107 return _('Address family, Internal IP address must match');
111 callHostHints
: rpc
.declare({
113 method
: 'getHostHints',
117 callConntrackHelpers
: rpc
.declare({
119 method
: 'getConntrackHelpers',
120 expect
: { result
: [] }
123 callNetworkDevices
: rpc
.declare({
125 method
: 'getNetworkDevices',
131 this.callHostHints(),
132 this.callConntrackHelpers(),
133 this.callNetworkDevices(),
138 render: function(data
) {
139 if (fwtool
.checkLegacySNAT())
140 return fwtool
.renderMigration();
142 return this.renderForwards(data
);
145 renderForwards: function(data
) {
150 var fw4
= L
.hasSystemFeature('firewall4');
152 m
= new form
.Map('firewall', _('Firewall - Port Forwards'),
153 _('Port forwarding allows remote computers on the Internet to connect to a specific computer or service within the private LAN.'));
155 s
= m
.section(form
.GridSection
, 'redirect', _('Port Forwards'));
160 s
.tab('general', _('General Settings'));
161 s
.tab('advanced', _('Advanced Settings'));
163 s
.filter = function(section_id
) {
164 return (uci
.get('firewall', section_id
, 'target') != 'SNAT');
167 s
.sectiontitle = function(section_id
) {
168 return uci
.get('firewall', section_id
, 'name') || _('Unnamed forward');
171 s
.handleAdd = function(ev
) {
172 var config_name
= this.uciconfig
|| this.map
.config
,
173 section_id
= uci
.add(config_name
, this.sectiontype
);
175 uci
.set(config_name
, section_id
, 'dest', 'lan');
176 uci
.set(config_name
, section_id
, 'target', 'DNAT');
178 m
.addedSection
= section_id
;
179 this.renderMoreOptionsModal(section_id
);
182 o
= s
.taboption('general', form
.Value
, 'name', _('Name'));
183 o
.placeholder
= _('Unnamed forward');
187 o
= s
.taboption('general', form
.ListValue
, 'family', _('Restrict to address family'));
190 o
.value('ipv4', _('IPv4 only'));
191 o
.value('ipv6', _('IPv6 only'));
192 o
.value('', _('automatic')); // infer from zone or used IP addresses
193 o
.cfgvalue = function(section_id
) {
194 var val
= this.map
.data
.get(this.map
.config
, section_id
, 'family');
196 if (!val
|| val
== 'any' || val
== 'all' || val
== '*')
198 else if (val
== 'inet' || String(val
).indexOf('4') != -1)
200 else if (String(val
).indexOf('6') != -1)
203 o
.validate = function(section_id
, value
) {
204 fwtool
.updateHostHints(this.map
, section_id
, 'dest_ip', value
, hosts
);
205 return !fw4
?true:validate_opt_family(this, section_id
, 'family');
209 o
= s
.option(form
.DummyValue
, '_match', _('Match'));
211 o
.textvalue = function(s
) {
213 rule_proto_txt(s
, ctHelpers
), E('br'),
214 rule_src_txt(s
, hosts
), E('br'),
215 rule_dest_txt(s
), E('br'),
220 o
= s
.option(form
.ListValue
, '_dest', _('Action'));
222 o
.textvalue = function(s
) {
228 o
= s
.option(form
.Flag
, 'enabled', _('Enable'));
230 o
.default = o
.enabled
;
233 o
= s
.taboption('general', fwtool
.CBIProtocolSelect
, 'proto', _('Protocol'));
235 o
.default = 'tcp udp';
237 o
= s
.taboption('general', widgets
.ZoneSelect
, 'src', _('Source zone'));
243 o
= s
.taboption('advanced', form
.Value
, 'ipset', _('Use ipset'));
244 uci
.sections('firewall', 'ipset', function(s
) {
245 if (typeof(s
.name
) == 'string')
246 o
.value(s
.name
, s
.comment
? '%s (%s)'.format(s
.name
, s
.comment
) : s
.name
);
251 o
= fwtool
.addMACOption(s
, 'advanced', 'src_mac', _('Source MAC address'),
252 _('Only match incoming traffic from these MACs.'), hosts
);
254 o
.datatype
= 'list(neg(macaddr))';
256 o
= fwtool
.addIPOption(s
, 'advanced', 'src_ip', _('Source IP address'),
257 _('Only match incoming traffic from this IP or range.'), !fw4
?'ipv4':'', hosts
);
259 o
.datatype
= !fw4
?'neg(ipmask4("true"))':'neg(ipmask("true"))';
261 o
= s
.taboption('advanced', form
.Value
, 'src_port', _('Source port'),
262 _('Only match incoming traffic originating from the given source port or port range on the client host'));
265 o
.datatype
= 'neg(portrange)';
266 o
.placeholder
= _('any');
267 o
.depends({ proto
: 'tcp', '!contains': true });
268 o
.depends({ proto
: 'udp', '!contains': true });
270 o
= fwtool
.addLocalIPOption(s
, 'advanced', 'src_dip', _('External IP address'),
271 _('Only match incoming traffic directed at the given IP address.'), devs
);
272 o
.datatype
= !fw4
?'neg(ipmask4("true"))':'neg(ipmask("true"))';
275 o
= s
.taboption('general', form
.Value
, 'src_dport', _('External port'),
276 _('Match incoming traffic directed at the given destination port or port range on this host'));
279 o
.datatype
= 'neg(portrange)';
280 o
.depends({ proto
: 'tcp', '!contains': true });
281 o
.depends({ proto
: 'udp', '!contains': true });
283 o
= s
.taboption('general', widgets
.ZoneSelect
, 'dest', _('Internal zone'));
288 o
= fwtool
.addIPOption(s
, 'general', 'dest_ip', _('Internal IP address'),
289 _('Redirect matched incoming traffic to the specified internal host'), !fw4
?'ipv4':'', hosts
);
291 o
.datatype
= !fw4
?'ipmask4':'ipmask';
293 o
= s
.taboption('general', form
.Value
, 'dest_port', _('Internal port'),
294 _('Redirect matched incoming traffic to the given port on the internal host'));
297 o
.placeholder
= _('any');
298 o
.datatype
= 'portrange';
299 o
.depends({ proto
: 'tcp', '!contains': true });
300 o
.depends({ proto
: 'udp', '!contains': true });
302 o
= s
.taboption('advanced', form
.Flag
, 'reflection', _('Enable NAT Loopback'));
305 o
.default = o
.enabled
;
307 o
= s
.taboption('advanced', form
.ListValue
, 'reflection_src', _('Loopback source IP'), _('Specifies whether to use the external or the internal IP address for reflected traffic.'));
309 o
.depends('reflection', '1');
310 o
.value('internal', _('Use internal IP address'));
311 o
.value('external', _('Use external IP address'));
312 o
.write = function(section_id
, value
) {
313 uci
.set('firewall', section_id
, 'reflection_src', (value
!= 'internal') ? value
: null);
316 o
= s
.taboption('advanced', form
.Value
, 'helper', _('Match helper'), _('Match traffic using the specified connection tracking helper.'));
318 o
.placeholder
= _('any');
319 for (var i
= 0; i
< ctHelpers
.length
; i
++)
320 o
.value(ctHelpers
[i
].name
, '%s (%s)'.format(ctHelpers
[i
].description
, ctHelpers
[i
].name
.toUpperCase()));
321 o
.validate = function(section_id
, value
) {
322 if (value
== '' || value
== null)
325 value
= value
.replace(/^!\s*/, '');
327 for (var i
= 0; i
< ctHelpers
.length
; i
++)
328 if (value
== ctHelpers
[i
].name
)
331 return _('Unknown or not installed conntrack helper "%s"').format(value
);
334 fwtool
.addMarkOption(s
, false);
335 fwtool
.addLimitOption(s
);
336 fwtool
.addLimitBurstOption(s
);
338 if (!L
.hasSystemFeature('firewall4')) {
339 o
= s
.taboption('advanced', form
.Value
, 'extra', _('Extra arguments'),
340 _('Passes additional arguments to iptables. Use with care!'));