6 'require firewall as fwmodel';
7 'require tools.firewall as fwtool';
8 'require tools.widgets as widgets';
10 function rule_proto_txt(s
) {
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
);
21 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);
23 val
: m
[0].toUpperCase().replace(/X
/g
, 'x'),
25 num
: '0x%02X'.format(+m
[2]),
26 mask
: m
[3] ? '0x%02X'.format(+m
[3]) : null
29 return fwtool
.fmt(_('Forwarded IPv4%{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 function rule_src_txt(s
, hosts
) {
36 var z
= uci
.get('firewall', s
, 'src');
38 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>}}'), {
39 src
: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel
.getColorForName(null) }, [E('em', _('any zone'))]),
40 src_ip
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_ip'), 'toLowerCase'),
41 src_port
: fwtool
.map_invert(uci
.get('firewall', s
, 'src_port'))
45 function rule_dest_txt(s
) {
46 var z
= uci
.get('firewall', s
, 'src');
48 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>}}'), {
49 dest
: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel
.getColorForName((z
&& z
!= '*') ? z
: null) }, [(z
== '*') ? E('em', _('any zone')) : (z
|| E('em', _('this device')))]),
50 dest_ip
: fwtool
.map_invert(uci
.get('firewall', s
, 'dest_ip'), 'toLowerCase'),
51 dest_port
: fwtool
.map_invert(uci
.get('firewall', s
, 'dest_port')),
52 dest_device
: uci
.get('firewall', s
, 'device')
56 function rule_limit_txt(s
) {
57 var m
= String(uci
.get('firewall', s
, 'limit')).match(/^(\d+)\/([smhd])\w*$/i),
60 unit
: ({ s
: _('second'), m
: _('minute'), h
: _('hour'), d
: _('day') })[m
[2]],
61 burst
: uci
.get('firewall', s
, 'limit_burst')
67 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
});
70 function rule_target_txt(s
) {
71 var t
= uci
.get('firewall', s
, 'target'),
74 snat_ip
: uci
.get('firewall', s
, 'snat_ip'),
75 snat_port
: uci
.get('firewall', s
, 'snat_port')
80 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
);
83 return fwtool
.fmt(_('<var data-tooltip="MASQUERADE">Automatically rewrite</var> source IP'));
86 return fwtool
.fmt(_('<var data-tooltip="ACCEPT">Prevent source rewrite</var>'));
93 return L
.view
.extend({
94 callHostHints
: rpc
.declare({
96 method
: 'getHostHints',
100 callNetworkDevices
: rpc
.declare({
102 method
: 'getNetworkDevices',
108 this.callHostHints(),
109 this.callNetworkDevices()
113 render: function(data
) {
118 m
= new form
.Map('firewall', _('Firewall - NAT Rules'),
119 _('NAT rules allow fine grained control over the source IP to use for outbound or forwarded traffic.'));
121 s
= m
.section(form
.GridSection
, 'nat', _('NAT Rules'));
126 s
.tab('general', _('General Settings'));
127 s
.tab('advanced', _('Advanced Settings'));
128 s
.tab('timed', _('Time Restrictions'));
130 s
.sectiontitle = function(section_id
) {
131 return uci
.get('firewall', section_id
, 'name') || _('Unnamed NAT');
134 o
= s
.taboption('general', form
.Value
, 'name', _('Name'));
135 o
.placeholder
= _('Unnamed NAT');
138 o
= s
.option(form
.DummyValue
, '_match', _('Match'));
140 o
.textvalue = function(s
) {
142 rule_proto_txt(s
), E('br'),
143 rule_src_txt(s
, hosts
), E('br'),
144 rule_dest_txt(s
), E('br'),
149 o
= s
.option(form
.ListValue
, '_target', _('Action'));
151 o
.textvalue = function(s
) {
152 return rule_target_txt(s
);
155 o
= s
.option(form
.Flag
, 'enabled', _('Enable'));
157 o
.default = o
.enabled
;
160 o
= s
.taboption('general', fwtool
.CBIProtocolSelect
, 'proto', _('Protocol'));
164 o
= s
.taboption('general', widgets
.ZoneSelect
, 'src', _('Outbound zone'));
171 o
= fwtool
.addIPOption(s
, 'general', 'src_ip', _('Source address'),
172 _('Match forwarded traffic from this IP or range.'), 'ipv4', hosts
);
174 o
.datatype
= 'neg(ipmask4)';
176 o
= s
.taboption('general', form
.Value
, 'src_port', _('Source port'),
177 _('Match forwarded traffic originating from the given source port or port range.'));
180 o
.datatype
= 'neg(portrange)';
181 o
.placeholder
= _('any');
182 o
.depends({ proto
: 'tcp', '!contains': true });
183 o
.depends({ proto
: 'udp', '!contains': true });
185 o
= fwtool
.addIPOption(s
, 'general', 'dest_ip', _('Destination address'),
186 _('Match forwarded traffic directed at the given IP address.'), 'ipv4', hosts
);
188 o
.datatype
= 'neg(ipmask4)';
190 o
= s
.taboption('general', form
.Value
, 'dest_port', _('Destination port'),
191 _('Match forwarded traffic directed at the given destination port or port range.'));
194 o
.placeholder
= _('any');
195 o
.datatype
= 'neg(portrange)';
196 o
.depends({ proto
: 'tcp', '!contains': true });
197 o
.depends({ proto
: 'udp', '!contains': true });
199 o
= s
.taboption('general', form
.ListValue
, 'target', _('Action'));
202 o
.value('SNAT', _('SNAT - Rewrite to specific source IP or port'));
203 o
.value('MASQUERADE', _('MASQUERADE - Automatically rewrite to outbound interface IP'));
204 o
.value('ACCEPT', _('ACCEPT - Disable address rewriting'));
206 o
= fwtool
.addLocalIPOption(s
, 'general', 'snat_ip', _('Rewrite IP address'),
207 _('Rewrite matched traffic to the specified source IP address.'), devs
);
208 o
.placeholder
= null;
209 o
.depends('target', 'SNAT');
210 o
.validate = function(section_id
, value
) {
211 var port
= this.map
.lookupOption('snat_port', section_id
),
212 a
= this.formvalue(section_id
),
213 p
= port
? port
[0].formvalue(section_id
) : null;
215 if ((a
== null || a
== '') && (p
== null || p
== ''))
216 return _('A rewrite IP must be specified!');
221 o
= s
.taboption('general', form
.Value
, 'snat_port', _('Rewrite port'),
222 _('Rewrite matched traffic to the specified source port or port range.'));
225 o
.placeholder
= _('do not rewrite');
226 o
.datatype
= 'portrange';
227 o
.depends({ proto
: 'tcp', '!contains': true });
228 o
.depends({ proto
: 'udp', '!contains': true });
230 o
= s
.taboption('advanced', widgets
.DeviceSelect
, 'device', _('Outbound device'),
231 _('Matches forwarded traffic using the specified outbound network device.'));
236 fwtool
.addMarkOption(s
, false);
237 fwtool
.addLimitOption(s
);
238 fwtool
.addLimitBurstOption(s
);
240 o
= s
.taboption('advanced', form
.Value
, 'extra', _('Extra arguments'),
241 _('Passes additional arguments to iptables. Use with care!'));
245 o
= s
.taboption('timed', form
.MultiValue
, 'weekdays', _('Week Days'));
249 o
.placeholder
= _('Any day');
250 o
.value('Sun', _('Sunday'));
251 o
.value('Mon', _('Monday'));
252 o
.value('Tue', _('Tuesday'));
253 o
.value('Wed', _('Wednesday'));
254 o
.value('Thu', _('Thursday'));
255 o
.value('Fri', _('Friday'));
256 o
.value('Sat', _('Saturday'));
257 o
.write = function(section_id
, value
) {
258 return this.super('write', [ section_id
, L
.toArray(value
).join(' ') ]);
261 o
= s
.taboption('timed', form
.MultiValue
, 'monthdays', _('Month Days'));
265 o
.placeholder
= _('Any day');
266 o
.write = function(section_id
, value
) {
267 return this.super('write', [ section_id
, L
.toArray(value
).join(' ') ]);
269 for (var i
= 1; i
<= 31; i
++)
272 o
= s
.taboption('timed', form
.Value
, 'start_time', _('Start Time (hh.mm.ss)'));
274 o
.datatype
= 'timehhmmss';
276 o
= s
.taboption('timed', form
.Value
, 'stop_time', _('Stop Time (hh.mm.ss)'));
278 o
.datatype
= 'timehhmmss';
280 o
= s
.taboption('timed', form
.Value
, 'start_date', _('Start Date (yyyy-mm-dd)'));
282 o
.datatype
= 'dateyyyymmdd';
284 o
= s
.taboption('timed', form
.Value
, 'stop_date', _('Stop Date (yyyy-mm-dd)'));
286 o
.datatype
= 'dateyyyymmdd';
288 o
= s
.taboption('timed', form
.Flag
, 'utc_time', _('Time in UTC'));
290 o
.default = o
.disabled
;