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 proto
= L
.toArray(uci
.get('firewall', s
, 'proto')).filter(function(p
) {
13 return (p
!= '*' && p
!= 'any' && p
!= 'all');
15 var pr
= fwtool
.lookupProto(p
);
19 types
: (pr
[0] == 1 || pr
[0] == 58) ? L
.toArray(uci
.get('firewall', s
, 'icmp_type')) : null
23 m
= String(uci
.get('firewall', s
, 'helper') || '').match(/^(!\s*)?(\S+)$/);
25 val
: m
[0].toUpperCase(),
27 name
: (ctHelpers
.filter(function(ctH
) { return ctH
.name
.toLowerCase() == m
[2].toLowerCase() })[0] || {}).description
30 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 val
: m
[0].toUpperCase().replace(/X
/g
, 'x'),
34 num
: '0x%02X'.format(+m
[2]),
35 mask
: m
[3] ? '0x%02X'.format(+m
[3]) : null
38 return fwtool
.fmt(_('Incoming IPv4%{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>}}'), {
45 function rule_src_txt(s
, hosts
) {
46 var z
= uci
.get('firewall', s
, 'src');
48 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>}}'), {
49 src
: E('span', { 'class': 'zonebadge', 'style': fwmodel
.getZoneColorStyle(z
) }, [(z
== '*') ? E('em', _('any zone')) : (z
? E('strong', z
) : E('em', _('this device')))]),
50 src_ip
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_ip'), 'toLowerCase'),
51 src_mac
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_mac'), 'toUpperCase').map(function(v
) { return Object
.assign(v
, { hint
: hosts
[v
.val
] }) }),
52 src_port
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_port'))
56 function rule_dest_txt(s
) {
57 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>}}'), {
58 dest
: E('span', { 'class': 'zonebadge', 'style': fwmodel
.getZoneColorStyle(null) }, [E('em', _('this device'))]),
59 dest_ip
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_dip'), 'toLowerCase'),
60 dest_port
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_dport'))
64 function rule_limit_txt(s
) {
65 var m
= String(uci
.get('firewall', s
, 'limit')).match(/^(\d+)\/([smhd])\w*$/i),
68 unit
: ({ s
: _('second'), m
: _('minute'), h
: _('hour'), d
: _('day') })[m
[2]],
69 burst
: uci
.get('firewall', s
, 'limit_burst')
75 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
});
78 function rule_target_txt(s
) {
79 var z
= uci
.get('firewall', s
, 'dest');
81 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>}'), {
82 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')))]),
83 dest_ip
: (uci
.get('firewall', s
, 'dest_ip') || '').toLowerCase(),
84 dest_port
: uci
.get('firewall', s
, 'dest_port')
89 callHostHints
: rpc
.declare({
91 method
: 'getHostHints',
95 callConntrackHelpers
: rpc
.declare({
97 method
: 'getConntrackHelpers',
98 expect
: { result
: [] }
101 callNetworkDevices
: rpc
.declare({
103 method
: 'getNetworkDevices',
109 this.callHostHints(),
110 this.callConntrackHelpers(),
111 this.callNetworkDevices(),
116 render: function(data
) {
117 if (fwtool
.checkLegacySNAT())
118 return fwtool
.renderMigration();
120 return this.renderForwards(data
);
123 renderForwards: function(data
) {
129 m
= new form
.Map('firewall', _('Firewall - Port Forwards'),
130 _('Port forwarding allows remote computers on the Internet to connect to a specific computer or service within the private LAN.'));
132 s
= m
.section(form
.GridSection
, 'redirect', _('Port Forwards'));
137 s
.tab('general', _('General Settings'));
138 s
.tab('advanced', _('Advanced Settings'));
140 s
.filter = function(section_id
) {
141 return (uci
.get('firewall', section_id
, 'target') != 'SNAT');
144 s
.sectiontitle = function(section_id
) {
145 return uci
.get('firewall', section_id
, 'name') || _('Unnamed forward');
148 s
.handleAdd = function(ev
) {
149 var config_name
= this.uciconfig
|| this.map
.config
,
150 section_id
= uci
.add(config_name
, this.sectiontype
);
152 uci
.set(config_name
, section_id
, 'target', 'DNAT');
154 this.addedSection
= section_id
;
155 this.renderMoreOptionsModal(section_id
);
158 o
= s
.taboption('general', form
.Value
, 'name', _('Name'));
159 o
.placeholder
= _('Unnamed forward');
162 o
= s
.option(form
.DummyValue
, '_match', _('Match'));
164 o
.textvalue = function(s
) {
166 rule_proto_txt(s
, ctHelpers
), E('br'),
167 rule_src_txt(s
, hosts
), E('br'),
168 rule_dest_txt(s
), E('br'),
173 o
= s
.option(form
.ListValue
, '_dest', _('Action'));
175 o
.textvalue = function(s
) {
181 o
= s
.option(form
.Flag
, 'enabled', _('Enable'));
183 o
.default = o
.enabled
;
186 o
= s
.taboption('general', fwtool
.CBIProtocolSelect
, 'proto', _('Protocol'));
188 o
.default = 'tcp udp';
190 o
= s
.taboption('general', widgets
.ZoneSelect
, 'src', _('Source zone'));
196 o
= fwtool
.addMACOption(s
, 'advanced', 'src_mac', _('Source MAC address'),
197 _('Only match incoming traffic from these MACs.'), hosts
);
199 o
.datatype
= 'list(neg(macaddr))';
201 o
= fwtool
.addIPOption(s
, 'advanced', 'src_ip', _('Source IP address'),
202 _('Only match incoming traffic from this IP or range.'), 'ipv4', hosts
);
204 o
.datatype
= 'neg(ipmask4("true"))';
206 o
= s
.taboption('advanced', form
.Value
, 'src_port', _('Source port'),
207 _('Only match incoming traffic originating from the given source port or port range on the client host'));
210 o
.datatype
= 'neg(portrange)';
211 o
.placeholder
= _('any');
212 o
.depends({ proto
: 'tcp', '!contains': true });
213 o
.depends({ proto
: 'udp', '!contains': true });
215 o
= fwtool
.addLocalIPOption(s
, 'advanced', 'src_dip', _('External IP address'),
216 _('Only match incoming traffic directed at the given IP address.'), devs
);
217 o
.datatype
= 'neg(ipmask4("true"))';
220 o
= s
.taboption('general', form
.Value
, 'src_dport', _('External port'),
221 _('Match incoming traffic directed at the given destination port or port range on this host'));
224 o
.datatype
= 'neg(portrange)';
225 o
.depends({ proto
: 'tcp', '!contains': true });
226 o
.depends({ proto
: 'udp', '!contains': true });
228 o
= s
.taboption('general', widgets
.ZoneSelect
, 'dest', _('Internal zone'));
234 o
= fwtool
.addIPOption(s
, 'general', 'dest_ip', _('Internal IP address'),
235 _('Redirect matched incoming traffic to the specified internal host'), 'ipv4', hosts
);
237 o
.datatype
= 'ipmask4';
239 o
= s
.taboption('general', form
.Value
, 'dest_port', _('Internal port'),
240 _('Redirect matched incoming traffic to the given port on the internal host'));
243 o
.placeholder
= _('any');
244 o
.datatype
= 'portrange';
245 o
.depends({ proto
: 'tcp', '!contains': true });
246 o
.depends({ proto
: 'udp', '!contains': true });
248 o
= s
.taboption('advanced', form
.Flag
, 'reflection', _('Enable NAT Loopback'));
251 o
.default = o
.enabled
;
253 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.'));
255 o
.depends('reflection', '1');
256 o
.value('internal', _('Use internal IP address'));
257 o
.value('external', _('Use external IP address'));
258 o
.write = function(section_id
, value
) {
259 uci
.set('firewall', section_id
, 'reflection_src', (value
!= 'internal') ? value
: null);
262 o
= s
.taboption('advanced', form
.Value
, 'helper', _('Match helper'), _('Match traffic using the specified connection tracking helper.'));
264 o
.placeholder
= _('any');
265 for (var i
= 0; i
< ctHelpers
.length
; i
++)
266 o
.value(ctHelpers
[i
].name
, '%s (%s)'.format(ctHelpers
[i
].description
, ctHelpers
[i
].name
.toUpperCase()));
267 o
.validate = function(section_id
, value
) {
268 if (value
== '' || value
== null)
271 value
= value
.replace(/^!\s*/, '');
273 for (var i
= 0; i
< ctHelpers
.length
; i
++)
274 if (value
== ctHelpers
[i
].name
)
277 return _('Unknown or not installed conntrack helper "%s"').format(value
);
280 fwtool
.addMarkOption(s
, false);
281 fwtool
.addLimitOption(s
);
282 fwtool
.addLimitBurstOption(s
);
284 if (!L
.hasSystemFeature('firewall4')) {
285 o
= s
.taboption('advanced', form
.Value
, 'extra', _('Extra arguments'),
286 _('Passes additional arguments to iptables. Use with care!'));