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 proto
= L
.toArray(uci
.get('firewall', s
, 'proto')).filter(function(p
) {
12 return (p
!= '*' && p
!= 'any' && p
!= 'all');
14 var pr
= fwtool
.lookupProto(p
);
18 types
: (pr
[0] == 1 || pr
[0] == 58) ? L
.toArray(uci
.get('firewall', s
, 'icmp_type')) : null
22 m
= String(uci
.get('firewall', s
, 'helper') || '').match(/^(!\s*)?(\S+)$/);
24 val
: m
[0].toUpperCase(),
26 name
: (ctHelpers
.filter(function(ctH
) { return ctH
.name
.toLowerCase() == m
[2].toLowerCase() })[0] || {}).description
29 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);
31 val
: m
[0].toUpperCase().replace(/X
/g
, 'x'),
33 num
: '0x%02X'.format(+m
[2]),
34 mask
: m
[3] ? '0x%02X'.format(+m
[3]) : null
37 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>}}'), {
44 function rule_src_txt(s
, hosts
) {
45 var z
= uci
.get('firewall', s
, 'src');
47 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>}}'), {
48 src
: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel
.getColorForName((z
&& z
!= '*') ? z
: null) }, [(z
== '*') ? E('em', _('any zone')) : (z
|| E('em', _('this device')))]),
49 src_ip
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_ip'), 'toLowerCase'),
50 src_mac
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_mac'), 'toUpperCase').map(function(v
) { return Object
.assign(v
, { hint
: hosts
[v
.val
] }) }),
51 src_port
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_port'))
55 function rule_dest_txt(s
) {
56 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>}}'), {
57 dest
: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel
.getColorForName(null) }, [E('em', _('this device'))]),
58 dest_ip
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_dip'), 'toLowerCase'),
59 dest_port
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_dport'))
63 function rule_limit_txt(s
) {
64 var m
= String(uci
.get('firewall', s
, 'limit')).match(/^(\d+)\/([smhd])\w*$/i),
67 unit
: ({ s
: _('second'), m
: _('minute'), h
: _('hour'), d
: _('day') })[m
[2]],
68 burst
: uci
.get('firewall', s
, 'limit_burst')
74 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
});
77 function rule_target_txt(s
) {
78 var z
= uci
.get('firewall', s
, 'dest');
80 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>}'), {
81 dest
: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel
.getColorForName((z
&& z
!= '*') ? z
: null) }, [(z
== '*') ? E('em', _('any zone')) : (z
|| E('em', _('this device')))]),
82 dest_ip
: (uci
.get('firewall', s
, 'dest_ip') || '').toLowerCase(),
83 dest_port
: uci
.get('firewall', s
, 'dest_port')
87 return L
.view
.extend({
88 callHostHints
: rpc
.declare({
90 method
: 'getHostHints',
94 callConntrackHelpers
: rpc
.declare({
96 method
: 'getConntrackHelpers',
97 expect
: { result
: [] }
100 callNetworkDevices
: rpc
.declare({
102 method
: 'getNetworkDevices',
108 this.callHostHints(),
109 this.callConntrackHelpers(),
110 this.callNetworkDevices()
114 render: function(data
) {
120 m
= new form
.Map('firewall', _('Firewall - Port Forwards'),
121 _('Port forwarding allows remote computers on the Internet to connect to a specific computer or service within the private LAN.'));
123 s
= m
.section(form
.GridSection
, 'redirect', _('Port Forwards'));
128 s
.tab('general', _('General Settings'));
129 s
.tab('advanced', _('Advanced Settings'));
131 s
.filter = function(section_id
) {
132 return (uci
.get('firewall', section_id
, 'target') != 'SNAT');
135 s
.sectiontitle = function(section_id
) {
136 return uci
.get('firewall', section_id
, 'name') || _('Unnamed forward');
139 s
.handleAdd = function(ev
) {
140 var config_name
= this.uciconfig
|| this.map
.config
,
141 section_id
= uci
.add(config_name
, this.sectiontype
);
143 uci
.set(config_name
, section_id
, 'target', 'DNAT');
145 this.addedSection
= section_id
;
146 this.renderMoreOptionsModal(section_id
);
149 o
= s
.taboption('general', form
.Value
, 'name', _('Name'));
150 o
.placeholder
= _('Unnamed forward');
153 o
= s
.option(form
.DummyValue
, '_match', _('Match'));
155 o
.textvalue = function(s
) {
157 rule_proto_txt(s
, ctHelpers
), E('br'),
158 rule_src_txt(s
, hosts
), E('br'),
159 rule_dest_txt(s
), E('br'),
164 o
= s
.option(form
.ListValue
, '_dest', _('Action'));
166 o
.textvalue = function(s
) {
172 o
= s
.option(form
.Flag
, 'enabled', _('Enable'));
174 o
.default = o
.enabled
;
177 o
= s
.taboption('general', fwtool
.CBIProtocolSelect
, 'proto', _('Protocol'));
179 o
.default = 'tcp udp';
181 o
= s
.taboption('general', widgets
.ZoneSelect
, 'src', _('Source zone'));
187 o
= fwtool
.addMACOption(s
, 'advanced', 'src_mac', _('Source MAC address'),
188 _('Only match incoming traffic from these MACs.'), hosts
);
190 o
.datatype
= 'list(neg(macaddr))';
192 o
= fwtool
.addIPOption(s
, 'advanced', 'src_ip', _('Source IP address'),
193 _('Only match incoming traffic from this IP or range.'), 'ipv4', hosts
);
195 o
.datatype
= 'neg(ipmask4)';
197 o
= s
.taboption('advanced', form
.Value
, 'src_port', _('Source port'),
198 _('Only match incoming traffic originating from the given source port or port range on the client host'));
201 o
.datatype
= 'neg(portrange)';
202 o
.placeholder
= _('any');
203 o
.depends({ proto
: 'tcp', '!contains': true });
204 o
.depends({ proto
: 'udp', '!contains': true });
206 o
= fwtool
.addLocalIPOption(s
, 'advanced', 'src_dip', _('External IP address'),
207 _('Only match incoming traffic directed at the given IP address.'), devs
);
208 o
.datatype
= 'neg(ipmask4)';
211 o
= s
.taboption('general', form
.Value
, 'src_dport', _('External port'),
212 _('Match incoming traffic directed at the given destination port or port range on this host'));
215 o
.datatype
= 'neg(portrange)';
216 o
.depends({ proto
: 'tcp', '!contains': true });
217 o
.depends({ proto
: 'udp', '!contains': true });
219 o
= s
.taboption('general', widgets
.ZoneSelect
, 'dest', _('Internal zone'));
225 o
= fwtool
.addIPOption(s
, 'general', 'dest_ip', _('Internal IP address'),
226 _('Redirect matched incoming traffic to the specified internal host'), 'ipv4', hosts
);
228 o
.datatype
= 'ipmask4';
230 o
= s
.taboption('general', form
.Value
, 'dest_port', _('Internal port'),
231 _('Redirect matched incoming traffic to the given port on the internal host'));
234 o
.placeholder
= _('any');
235 o
.datatype
= 'portrange';
236 o
.depends({ proto
: 'tcp', '!contains': true });
237 o
.depends({ proto
: 'udp', '!contains': true });
239 o
= s
.taboption('advanced', form
.Flag
, 'reflection', _('Enable NAT Loopback'));
242 o
.default = o
.enabled
;
244 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.'));
246 o
.depends('reflection', '1');
247 o
.value('internal', _('Use internal IP address'));
248 o
.value('external', _('Use external IP address'));
249 o
.write = function(section_id
, value
) {
250 uci
.set('firewall', section_id
, 'reflection_src', (value
!= 'internal') ? value
: null);
253 o
= s
.taboption('advanced', form
.Value
, 'helper', _('Match helper'), _('Match traffic using the specified connection tracking helper.'));
255 o
.placeholder
= _('any');
256 for (var i
= 0; i
< ctHelpers
.length
; i
++)
257 o
.value(ctHelpers
[i
].name
, '%s (%s)'.format(ctHelpers
[i
].description
, ctHelpers
[i
].name
.toUpperCase()));
258 o
.validate = function(section_id
, value
) {
259 if (value
== '' || value
== null)
262 value
= value
.replace(/^!\s*/, '');
264 for (var i
= 0; i
< ctHelpers
.length
; i
++)
265 if (value
== ctHelpers
[i
].name
)
268 return _('Unknown or not installed conntrack helper "%s"').format(value
);
271 fwtool
.addMarkOption(s
, false);
272 fwtool
.addLimitOption(s
);
273 fwtool
.addLimitBurstOption(s
);
275 o
= s
.taboption('advanced', form
.Value
, 'extra', _('Extra arguments'),
276 _('Passes additional arguments to iptables. Use with care!'));