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 proto = L.toArray(uci.get('firewall', s, 'proto')).filter(function(p) {
13 return (p != '*' && p != 'any' && p != 'all');
15 var pr = fwtool.lookupProto(p);
22 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);
24 val: m[0].toUpperCase().replace(/X/g, 'x'),
26 num: '0x%02X'.format(+m[2]),
27 mask: m[3] ? '0x%02X'.format(+m[3]) : null
30 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>}'), {
36 function rule_src_txt(s, hosts) {
37 var z = uci.get('firewall', s, 'src');
39 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>}}'), {
40 src: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel.getColorForName(null) }, [E('em', _('any zone'))]),
41 src_ip: fwtool.map_invert(uci.get('firewall', s, 'src_ip'), 'toLowerCase'),
42 src_port: fwtool.map_invert(uci.get('firewall', s, 'src_port'))
46 function rule_dest_txt(s) {
47 var z = uci.get('firewall', s, 'src');
49 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>}}'), {
50 dest: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel.getColorForName((z && z != '*') ? z : null) }, [(z == '*') ? E('em', _('any zone')) : (z || E('em', _('this device')))]),
51 dest_ip: fwtool.map_invert(uci.get('firewall', s, 'dest_ip'), 'toLowerCase'),
52 dest_port: fwtool.map_invert(uci.get('firewall', s, 'dest_port')),
53 dest_device: uci.get('firewall', s, 'device')
57 function rule_limit_txt(s) {
58 var m = String(uci.get('firewall', s, 'limit')).match(/^(\d+)\/([smhd])\w*$/i),
61 unit: ({ s: _('second'), m: _('minute'), h: _('hour'), d: _('day') })[m[2]],
62 burst: uci.get('firewall', s, 'limit_burst')
68 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 });
71 function rule_target_txt(s) {
72 var t = uci.get('firewall', s, 'target'),
75 snat_ip: uci.get('firewall', s, 'snat_ip'),
76 snat_port: uci.get('firewall', s, 'snat_port')
81 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);
84 return fwtool.fmt(_('<var data-tooltip="MASQUERADE">Automatically rewrite</var> source IP'));
87 return fwtool.fmt(_('<var data-tooltip="ACCEPT">Prevent source rewrite</var>'));
95 callHostHints: rpc.declare({
97 method: 'getHostHints',
101 callNetworkDevices: rpc.declare({
103 method: 'getNetworkDevices',
109 this.callHostHints(),
110 this.callNetworkDevices(),
115 render: function(data) {
116 if (fwtool.checkLegacySNAT())
117 return fwtool.renderMigration();
119 return this.renderNats(data);
122 renderNats: function(data) {
127 m = new form.Map('firewall', _('Firewall - NAT Rules'),
128 _('NAT rules allow fine grained control over the source IP to use for outbound or forwarded traffic.'));
130 s = m.section(form.GridSection, 'nat', _('NAT Rules'));
135 s.tab('general', _('General Settings'));
136 s.tab('advanced', _('Advanced Settings'));
137 s.tab('timed', _('Time Restrictions'));
139 s.sectiontitle = function(section_id) {
140 return uci.get('firewall', section_id, 'name') || _('Unnamed NAT');
143 o = s.taboption('general', form.Value, 'name', _('Name'));
144 o.placeholder = _('Unnamed NAT');
147 o = s.option(form.DummyValue, '_match', _('Match'));
149 o.textvalue = function(s) {
151 rule_proto_txt(s), E('br'),
152 rule_src_txt(s, hosts), E('br'),
153 rule_dest_txt(s), E('br'),
158 o = s.option(form.ListValue, '_target', _('Action'));
160 o.textvalue = function(s) {
161 return rule_target_txt(s);
164 o = s.option(form.Flag, 'enabled', _('Enable'));
166 o.default = o.enabled;
169 o = s.taboption('general', fwtool.CBIProtocolSelect, 'proto', _('Protocol'));
173 o = s.taboption('general', widgets.ZoneSelect, 'src', _('Outbound zone'));
180 o = fwtool.addIPOption(s, 'general', 'src_ip', _('Source address'),
181 _('Match forwarded traffic from this IP or range.'), 'ipv4', hosts);
183 o.datatype = 'neg(ipmask4)';
185 o = s.taboption('general', form.Value, 'src_port', _('Source port'),
186 _('Match forwarded traffic originating from the given source port or port range.'));
189 o.datatype = 'neg(portrange)';
190 o.placeholder = _('any');
191 o.depends({ proto: 'tcp', '!contains': true });
192 o.depends({ proto: 'udp', '!contains': true });
194 o = fwtool.addIPOption(s, 'general', 'dest_ip', _('Destination address'),
195 _('Match forwarded traffic directed at the given IP address.'), 'ipv4', hosts);
197 o.datatype = 'neg(ipmask4)';
199 o = s.taboption('general', form.Value, 'dest_port', _('Destination port'),
200 _('Match forwarded traffic directed at the given destination port or port range.'));
203 o.placeholder = _('any');
204 o.datatype = 'neg(portrange)';
205 o.depends({ proto: 'tcp', '!contains': true });
206 o.depends({ proto: 'udp', '!contains': true });
208 o = s.taboption('general', form.ListValue, 'target', _('Action'));
211 o.value('SNAT', _('SNAT - Rewrite to specific source IP or port'));
212 o.value('MASQUERADE', _('MASQUERADE - Automatically rewrite to outbound interface IP'));
213 o.value('ACCEPT', _('ACCEPT - Disable address rewriting'));
215 o = fwtool.addLocalIPOption(s, 'general', 'snat_ip', _('Rewrite IP address'),
216 _('Rewrite matched traffic to the specified source IP address.'), devs);
217 o.placeholder = null;
218 o.depends('target', 'SNAT');
219 o.validate = function(section_id, value) {
220 var port = this.map.lookupOption('snat_port', section_id),
221 a = this.formvalue(section_id),
222 p = port ? port[0].formvalue(section_id) : null;
224 if ((a == null || a == '') && (p == null || p == '') && value == '')
225 return _('A rewrite IP must be specified!');
230 o = s.taboption('general', form.Value, 'snat_port', _('Rewrite port'),
231 _('Rewrite matched traffic to the specified source port or port range.'));
234 o.placeholder = _('do not rewrite');
235 o.datatype = 'portrange';
236 o.depends({ proto: 'tcp', '!contains': true });
237 o.depends({ proto: 'udp', '!contains': true });
239 o = s.taboption('advanced', widgets.DeviceSelect, 'device', _('Outbound device'),
240 _('Matches forwarded traffic using the specified outbound network device.'));
245 fwtool.addMarkOption(s, false);
246 fwtool.addLimitOption(s);
247 fwtool.addLimitBurstOption(s);
249 o = s.taboption('advanced', form.Value, 'extra', _('Extra arguments'),
250 _('Passes additional arguments to iptables. Use with care!'));
254 o = s.taboption('timed', form.MultiValue, 'weekdays', _('Week Days'));
258 o.placeholder = _('Any day');
259 o.value('Sun', _('Sunday'));
260 o.value('Mon', _('Monday'));
261 o.value('Tue', _('Tuesday'));
262 o.value('Wed', _('Wednesday'));
263 o.value('Thu', _('Thursday'));
264 o.value('Fri', _('Friday'));
265 o.value('Sat', _('Saturday'));
266 o.write = function(section_id, value) {
267 return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
270 o = s.taboption('timed', form.MultiValue, 'monthdays', _('Month Days'));
274 o.placeholder = _('Any day');
275 o.write = function(section_id, value) {
276 return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
278 for (var i = 1; i <= 31; i++)
281 o = s.taboption('timed', form.Value, 'start_time', _('Start Time (hh.mm.ss)'));
283 o.datatype = 'timehhmmss';
285 o = s.taboption('timed', form.Value, 'stop_time', _('Stop Time (hh.mm.ss)'));
287 o.datatype = 'timehhmmss';
289 o = s.taboption('timed', form.Value, 'start_date', _('Start Date (yyyy-mm-dd)'));
291 o.datatype = 'dateyyyymmdd';
293 o = s.taboption('timed', form.Value, 'stop_date', _('Stop Date (yyyy-mm-dd)'));
295 o.datatype = 'dateyyyymmdd';
297 o = s.taboption('timed', form.Flag, 'utc_time', _('Time in UTC'));
299 o.default = o.disabled;