759c23e0d9224ff43ca70e54e6aacce4d2f07ea4
7 'require firewall as fwmodel';
8 'require tools.firewall as fwtool';
9 'require tools.widgets as widgets';
11 function rule_proto_txt(s
) {
12 var family
= (uci
.get('firewall', s
, 'family') || '').toLowerCase().replace(/^(?:any|\*)$/, '');
13 var sip
= uci
.get('firewall', s
, 'src_ip') || '';
14 var dip
= uci
.get('firewall', s
, 'dest_ip') || '';
15 var rwip
= uci
.get('firewall', s
, 'snat_ip') || '';
16 var proto
= L
.toArray(uci
.get('firewall', s
, 'proto')).filter(function(p
) {
17 return (p
!= '*' && p
!= 'any' && p
!= 'all');
19 var pr
= fwtool
.lookupProto(p
);
26 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);
28 val
: m
[0].toUpperCase().replace(/X
/g
, 'x'),
30 num
: '0x%02X'.format(+m
[2]),
31 mask
: m
[3] ? '0x%02X'.format(+m
[3]) : null
34 return fwtool
.fmt(_('Forwarded %{ipv6?%{ipv4?<var>IPv4</var> and <var>IPv6</var>:<var>IPv6</var>}:<var>IPv4</var>}%{proto?, protocol %{proto#%{next?, }<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>}'), {
35 ipv4
: (family
== 'ipv4' || (!family
&& sip
.indexOf(':') == -1 && dip
.indexOf(':') == -1 && rwip
.indexOf(':') == -1)),
36 ipv6
: (family
== 'ipv6' || (!family
&& (!sip
|| !dip
|| !rwip
)) || (!family
&& (sip
.indexOf(':') != -1 || dip
.indexOf(':') != -1 || rwip
.indexOf(':') != -1))),
42 function rule_src_txt(s
, hosts
) {
43 var z
= uci
.get('firewall', s
, 'src');
45 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>}}'), {
46 src
: E('span', { 'class': 'zonebadge', 'style': fwmodel
.getZoneColorStyle(null) }, [E('em', _('any zone'))]),
47 src_ip
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_ip'), 'toLowerCase'),
48 src_port
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_port'))
52 function rule_dest_txt(s
) {
53 var z
= uci
.get('firewall', s
, 'src');
55 return fwtool
.fmt(_('To %{dest}%{dest_device?, via 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>}}'), {
56 dest
: E('span', { 'class': 'zonebadge', 'style': fwmodel
.getZoneColorStyle(z
) }, [(z
== '*') ? E('em', _('any zone')) : (z
? E('strong', z
) : E('em', _('this device')))]),
57 dest_ip
: fwtool
.map_invert(uci
.get('firewall', s
, 'dest_ip'), 'toLowerCase'),
58 dest_port
: fwtool
.map_invert(uci
.get('firewall', s
, 'dest_port')),
59 dest_device
: uci
.get('firewall', s
, 'device')
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 t
= uci
.get('firewall', s
, 'target'),
81 snat_ip
: uci
.get('firewall', s
, 'snat_ip'),
82 snat_port
: uci
.get('firewall', s
, 'snat_port')
87 return fwtool
.fmt(_('<var data-tooltip="SNAT">Statically rewrite</var> to source %{snat_ip?IP <var>%{snat_ip}</var>} %{snat_port?port <var>%{snat_port}</var>}'), s
);
90 return fwtool
.fmt(_('<var data-tooltip="MASQUERADE">Automatically rewrite</var> source IP'));
93 return fwtool
.fmt(_('<var data-tooltip="ACCEPT">Prevent source rewrite</var>'));
100 function validate_opt_family(m
, section_id
, opt
) {
101 var sopt
= m
.section
.getOption('src_ip'),
102 dopt
= m
.section
.getOption('dest_ip'),
103 rwopt
= m
.section
.getOption('snat_ip'),
104 fmopt
= m
.section
.getOption('family'),
105 tgopt
= m
.section
.getOption('target');
107 if (!sopt
.isValid(section_id
) && opt
!= 'src_ip')
109 if (!dopt
.isValid(section_id
) && opt
!= 'dest_ip')
111 if (!rwopt
.isValid(section_id
) && opt
!= 'snat_ip')
113 if (!fmopt
.isValid(section_id
) && opt
!= 'family')
115 if (!tgopt
.isValid(section_id
) && opt
!= 'target')
118 var sip
= sopt
.formvalue(section_id
) || '',
119 dip
= dopt
.formvalue(section_id
) || '',
120 rwip
= rwopt
.formvalue(section_id
) || '',
121 fm
= fmopt
.formvalue(section_id
) || '',
122 tg
= tgopt
.formvalue(section_id
);
124 if (fm
== 'ipv6' && (sip
.indexOf(':') != -1 || sip
== '') && (dip
.indexOf(':') != -1 || dip
== '') && ((rwip
.indexOf(':') != -1 && tg
== 'SNAT') || rwip
== ''))
126 if (fm
== 'ipv4' && (sip
.indexOf(':') == -1) && (dip
.indexOf(':') == -1) && ((rwip
.indexOf(':') == -1 && tg
== 'SNAT') || rwip
== ''))
129 if ((sip
.indexOf(':') != -1 || sip
== '') && (dip
.indexOf(':') != -1 || dip
== '') && ((rwip
.indexOf(':') != -1 && tg
== 'SNAT') || rwip
== ''))
131 if ((sip
.indexOf(':') == -1) && (dip
.indexOf(':') == -1) && ((rwip
.indexOf(':') == -1 && tg
== 'SNAT') || rwip
== ''))
135 return _('Address family, source address, destination address, rewrite IP address must match');
139 callHostHints
: rpc
.declare({
141 method
: 'getHostHints',
145 callNetworkDevices
: rpc
.declare({
147 method
: 'getNetworkDevices',
153 this.callHostHints(),
154 this.callNetworkDevices(),
159 render: function(data
) {
160 if (fwtool
.checkLegacySNAT())
161 return fwtool
.renderMigration();
163 return this.renderNats(data
);
166 renderNats: function(data
) {
170 var fw4
= L
.hasSystemFeature('firewall4');
172 m
= new form
.Map('firewall', _('Firewall - NAT Rules'),
173 _('NAT rules allow fine grained control over the source IP to use for outbound or forwarded traffic.'));
175 s
= m
.section(form
.GridSection
, 'nat', _('NAT Rules'));
180 s
.tab('general', _('General Settings'));
181 s
.tab('advanced', _('Advanced Settings'));
182 s
.tab('timed', _('Time Restrictions'));
184 s
.sectiontitle = function(section_id
) {
185 return uci
.get('firewall', section_id
, 'name') || _('Unnamed NAT');
188 o
= s
.taboption('general', form
.Value
, 'name', _('Name'));
189 o
.placeholder
= _('Unnamed NAT');
192 o
= s
.option(form
.DummyValue
, '_match', _('Match'));
194 o
.textvalue = function(s
) {
196 rule_proto_txt(s
), E('br'),
197 rule_src_txt(s
, hosts
), E('br'),
198 rule_dest_txt(s
), E('br'),
203 o
= s
.option(form
.ListValue
, '_target', _('Action'));
205 o
.textvalue = function(s
) {
206 return rule_target_txt(s
);
209 o
= s
.option(form
.Flag
, 'enabled', _('Enable'));
211 o
.default = o
.enabled
;
215 o
= s
.taboption('general', form
.ListValue
, 'family', _('Restrict to address family'));
218 o
.value('ipv4', _('IPv4 only'));
219 o
.value('ipv6', _('IPv6 only'));
220 o
.value('', _('automatic')); // infer from zone or used IP addresses
221 o
.cfgvalue = function(section_id
) {
222 var val
= this.map
.data
.get(this.map
.config
, section_id
, 'family');
224 if (!val
|| val
== 'any' || val
== 'all' || val
== '*')
226 else if (val
== 'inet' || String(val
).indexOf('4') != -1)
228 else if (String(val
).indexOf('6') != -1)
231 o
.validate = function(section_id
, value
) {
232 fwtool
.updateHostHints(this.map
, section_id
, 'src_ip', value
, hosts
);
233 fwtool
.updateHostHints(this.map
, section_id
, 'dest_ip', value
, hosts
);
234 return !fw4
?true:validate_opt_family(this, section_id
, 'family');
238 o
= s
.taboption('general', fwtool
.CBIProtocolSelect
, 'proto', _('Protocol'));
242 o
= s
.taboption('general', widgets
.ZoneSelect
, 'src', _('Outbound zone'));
249 o
= fwtool
.addIPOption(s
, 'general', 'src_ip', _('Source address'),
250 _('Match forwarded traffic from this IP or range.'), !fw4
?'ipv4':'', hosts
);
252 o
.datatype
= !fw4
?'neg(ipmask4("true"))':'neg(ipmask("true"))';
253 o
.validate = function(section_id
, value
) {
254 return !fw4
?true:validate_opt_family(this, section_id
, 'src_ip');
257 o
= s
.taboption('general', form
.Value
, 'src_port', _('Source port'),
258 _('Match forwarded traffic originating from the given source port or port range.'));
261 o
.datatype
= 'neg(portrange)';
262 o
.placeholder
= _('any');
263 o
.depends({ proto
: 'tcp', '!contains': true });
264 o
.depends({ proto
: 'udp', '!contains': true });
266 o
= fwtool
.addIPOption(s
, 'general', 'dest_ip', _('Destination address'),
267 _('Match forwarded traffic directed at the given IP address.'), !fw4
?'ipv4':'', hosts
);
269 o
.datatype
= !fw4
?'neg(ipmask4("true"))':'neg(ipmask("true"))';
270 o
.validate = function(section_id
, value
) {
271 return !fw4
?true:validate_opt_family(this, section_id
, 'dest_ip');
274 o
= s
.taboption('general', form
.Value
, 'dest_port', _('Destination port'),
275 _('Match forwarded traffic directed at the given destination port or port range.'));
278 o
.placeholder
= _('any');
279 o
.datatype
= 'neg(portrange)';
280 o
.depends({ proto
: 'tcp', '!contains': true });
281 o
.depends({ proto
: 'udp', '!contains': true });
283 o
= s
.taboption('general', form
.ListValue
, 'target', _('Action'));
286 o
.value('SNAT', _('SNAT - Rewrite to specific source IP or port'));
287 o
.value('MASQUERADE', _('MASQUERADE - Automatically rewrite to outbound interface IP'));
288 o
.value('ACCEPT', _('ACCEPT - Disable address rewriting'));
289 o
.validate = function(section_id
, value
) {
290 return !fw4
?true:validate_opt_family(this, section_id
, 'target');
293 o
= fwtool
.addLocalIPOption(s
, 'general', 'snat_ip', _('Rewrite IP address'),
294 _('Rewrite matched traffic to the specified source IP address.'), devs
);
295 o
.placeholder
= null;
296 o
.depends('target', 'SNAT');
297 o
.validate = function(section_id
, value
) {
298 var a
= this.formvalue(section_id
),
299 p
= this.section
.formvalue(section_id
, 'snat_port');
301 if ((a
== null || a
== '') && (p
== null || p
== '') && value
== '')
302 return _('A rewrite IP must be specified!');
304 return !fw4
?true:validate_opt_family(this, section_id
, 'snat_ip');
307 o
= s
.taboption('general', form
.Value
, 'snat_port', _('Rewrite port'),
308 _('Rewrite matched traffic to the specified source port or port range.'));
311 o
.placeholder
= _('do not rewrite');
312 o
.datatype
= 'portrange';
313 o
.depends({ proto
: 'tcp', '!contains': true });
314 o
.depends({ proto
: 'udp', '!contains': true });
316 var have_fw4
= L
.hasSystemFeature('firewall4')
318 o
= s
.taboption('advanced', form
.Value
, 'ipset', _('Use ipset'));
319 uci
.sections('firewall', 'ipset', function(s
) {
320 if (typeof(s
.name
) == 'string')
321 o
.value(s
.name
, s
.comment
? '%s (%s)'.format(s
.name
, s
.comment
) : s
.name
);
327 o
= s
.taboption('advanced', widgets
.DeviceSelect
, 'device', _('Outbound device'),
328 _('Matches forwarded traffic using the specified outbound network device.'));
333 fwtool
.addMarkOption(s
, false);
334 fwtool
.addLimitOption(s
);
335 fwtool
.addLimitBurstOption(s
);
338 o
= s
.taboption('advanced', form
.Value
, 'extra', _('Extra arguments'),
339 _('Passes additional arguments to iptables. Use with care!'));
344 o
= s
.taboption('timed', form
.MultiValue
, 'weekdays', _('Week Days'));
348 o
.placeholder
= _('Any day');
349 o
.value('Sun', _('Sunday'));
350 o
.value('Mon', _('Monday'));
351 o
.value('Tue', _('Tuesday'));
352 o
.value('Wed', _('Wednesday'));
353 o
.value('Thu', _('Thursday'));
354 o
.value('Fri', _('Friday'));
355 o
.value('Sat', _('Saturday'));
356 o
.write = function(section_id
, value
) {
357 return this.super('write', [ section_id
, L
.toArray(value
).join(' ') ]);
360 o
= s
.taboption('timed', form
.MultiValue
, 'monthdays', _('Month Days'));
364 o
.placeholder
= _('Any day');
365 o
.write = function(section_id
, value
) {
366 return this.super('write', [ section_id
, L
.toArray(value
).join(' ') ]);
368 for (var i
= 1; i
<= 31; i
++)
371 o
= s
.taboption('timed', form
.Value
, 'start_time', _('Start Time (hh:mm:ss)'));
373 o
.datatype
= 'timehhmmss';
375 o
= s
.taboption('timed', form
.Value
, 'stop_time', _('Stop Time (hh:mm:ss)'));
377 o
.datatype
= 'timehhmmss';
379 o
= s
.taboption('timed', form
.Value
, 'start_date', _('Start Date (yyyy-mm-dd)'));
381 o
.datatype
= 'dateyyyymmdd';
383 o
= s
.taboption('timed', form
.Value
, 'stop_date', _('Stop Date (yyyy-mm-dd)'));
385 o
.datatype
= 'dateyyyymmdd';
387 o
= s
.taboption('timed', form
.Flag
, 'utc_time', _('Time in UTC'));
389 o
.default = o
.disabled
;