luci-app-firewall: snats.js: fix rewrite IP validation for SNAT target
[project/luci.git] / applications / luci-app-firewall / htdocs / luci-static / resources / view / firewall / snats.js
1 'use strict';
2 'require ui';
3 'require rpc';
4 'require uci';
5 'require form';
6 'require firewall as fwmodel';
7 'require tools.firewall as fwtool';
8 'require tools.widgets as widgets';
9
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');
13 }).map(function(p) {
14 var pr = fwtool.lookupProto(p);
15 return {
16 num: pr[0],
17 name: pr[1]
18 };
19 });
20
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);
22 var f = m ? {
23 val: m[0].toUpperCase().replace(/X/g, 'x'),
24 inv: m[1],
25 num: '0x%02X'.format(+m[2]),
26 mask: m[3] ? '0x%02X'.format(+m[3]) : null
27 } : null;
28
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>}'), {
30 proto: proto,
31 mark: f
32 });
33 }
34
35 function rule_src_txt(s, hosts) {
36 var z = uci.get('firewall', s, 'src');
37
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'))
42 });
43 }
44
45 function rule_dest_txt(s) {
46 var z = uci.get('firewall', s, 'src');
47
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')
53 });
54 }
55
56 function rule_limit_txt(s) {
57 var m = String(uci.get('firewall', s, 'limit')).match(/^(\d+)\/([smhd])\w*$/i),
58 l = m ? {
59 num: +m[1],
60 unit: ({ s: _('second'), m: _('minute'), h: _('hour'), d: _('day') })[m[2]],
61 burst: uci.get('firewall', s, 'limit_burst')
62 } : null;
63
64 if (!l)
65 return '';
66
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 });
68 }
69
70 function rule_target_txt(s) {
71 var t = uci.get('firewall', s, 'target'),
72 s = {
73 target: t,
74 snat_ip: uci.get('firewall', s, 'snat_ip'),
75 snat_port: uci.get('firewall', s, 'snat_port')
76 };
77
78 switch (t) {
79 case 'SNAT':
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);
81
82 case 'MASQUERADE':
83 return fwtool.fmt(_('<var data-tooltip="MASQUERADE">Automatically rewrite</var> source IP'));
84
85 case 'ACCEPT':
86 return fwtool.fmt(_('<var data-tooltip="ACCEPT">Prevent source rewrite</var>'));
87
88 default:
89 return t;
90 }
91 }
92
93 return L.view.extend({
94 callHostHints: rpc.declare({
95 object: 'luci-rpc',
96 method: 'getHostHints',
97 expect: { '': {} }
98 }),
99
100 callNetworkDevices: rpc.declare({
101 object: 'luci-rpc',
102 method: 'getNetworkDevices',
103 expect: { '': {} }
104 }),
105
106 load: function() {
107 return Promise.all([
108 this.callHostHints(),
109 this.callNetworkDevices(),
110 uci.load('firewall')
111 ]);
112 },
113
114 render: function(data) {
115 if (fwtool.checkLegacySNAT())
116 return fwtool.renderMigration();
117 else
118 return this.renderNats(data);
119 },
120
121 renderNats: function(data) {
122 var hosts = data[0],
123 devs = data[1],
124 m, s, o;
125
126 m = new form.Map('firewall', _('Firewall - NAT Rules'),
127 _('NAT rules allow fine grained control over the source IP to use for outbound or forwarded traffic.'));
128
129 s = m.section(form.GridSection, 'nat', _('NAT Rules'));
130 s.addremove = true;
131 s.anonymous = true;
132 s.sortable = true;
133
134 s.tab('general', _('General Settings'));
135 s.tab('advanced', _('Advanced Settings'));
136 s.tab('timed', _('Time Restrictions'));
137
138 s.sectiontitle = function(section_id) {
139 return uci.get('firewall', section_id, 'name') || _('Unnamed NAT');
140 };
141
142 o = s.taboption('general', form.Value, 'name', _('Name'));
143 o.placeholder = _('Unnamed NAT');
144 o.modalonly = true;
145
146 o = s.option(form.DummyValue, '_match', _('Match'));
147 o.modalonly = false;
148 o.textvalue = function(s) {
149 return E('small', [
150 rule_proto_txt(s), E('br'),
151 rule_src_txt(s, hosts), E('br'),
152 rule_dest_txt(s), E('br'),
153 rule_limit_txt(s)
154 ]);
155 };
156
157 o = s.option(form.ListValue, '_target', _('Action'));
158 o.modalonly = false;
159 o.textvalue = function(s) {
160 return rule_target_txt(s);
161 };
162
163 o = s.option(form.Flag, 'enabled', _('Enable'));
164 o.modalonly = false;
165 o.default = o.enabled;
166 o.editable = true;
167
168 o = s.taboption('general', fwtool.CBIProtocolSelect, 'proto', _('Protocol'));
169 o.modalonly = true;
170 o.default = 'all';
171
172 o = s.taboption('general', widgets.ZoneSelect, 'src', _('Outbound zone'));
173 o.modalonly = true;
174 o.rmempty = false;
175 o.nocreate = true;
176 o.allowany = true;
177 o.default = 'lan';
178
179 o = fwtool.addIPOption(s, 'general', 'src_ip', _('Source address'),
180 _('Match forwarded traffic from this IP or range.'), 'ipv4', hosts);
181 o.rmempty = true;
182 o.datatype = 'neg(ipmask4)';
183
184 o = s.taboption('general', form.Value, 'src_port', _('Source port'),
185 _('Match forwarded traffic originating from the given source port or port range.'));
186 o.modalonly = true;
187 o.rmempty = true;
188 o.datatype = 'neg(portrange)';
189 o.placeholder = _('any');
190 o.depends({ proto: 'tcp', '!contains': true });
191 o.depends({ proto: 'udp', '!contains': true });
192
193 o = fwtool.addIPOption(s, 'general', 'dest_ip', _('Destination address'),
194 _('Match forwarded traffic directed at the given IP address.'), 'ipv4', hosts);
195 o.rmempty = true;
196 o.datatype = 'neg(ipmask4)';
197
198 o = s.taboption('general', form.Value, 'dest_port', _('Destination port'),
199 _('Match forwarded traffic directed at the given destination port or port range.'));
200 o.modalonly = true;
201 o.rmempty = true;
202 o.placeholder = _('any');
203 o.datatype = 'neg(portrange)';
204 o.depends({ proto: 'tcp', '!contains': true });
205 o.depends({ proto: 'udp', '!contains': true });
206
207 o = s.taboption('general', form.ListValue, 'target', _('Action'));
208 o.modalonly = true;
209 o.default = 'SNAT';
210 o.value('SNAT', _('SNAT - Rewrite to specific source IP or port'));
211 o.value('MASQUERADE', _('MASQUERADE - Automatically rewrite to outbound interface IP'));
212 o.value('ACCEPT', _('ACCEPT - Disable address rewriting'));
213
214 o = fwtool.addLocalIPOption(s, 'general', 'snat_ip', _('Rewrite IP address'),
215 _('Rewrite matched traffic to the specified source IP address.'), devs);
216 o.placeholder = null;
217 o.depends('target', 'SNAT');
218 o.validate = function(section_id, value) {
219 var port = this.map.lookupOption('snat_port', section_id),
220 a = this.formvalue(section_id),
221 p = port ? port[0].formvalue(section_id) : null;
222
223 if ((a == null || a == '') && (p == null || p == '') && value == '')
224 return _('A rewrite IP must be specified!');
225
226 return true;
227 };
228
229 o = s.taboption('general', form.Value, 'snat_port', _('Rewrite port'),
230 _('Rewrite matched traffic to the specified source port or port range.'));
231 o.modalonly = true;
232 o.rmempty = true;
233 o.placeholder = _('do not rewrite');
234 o.datatype = 'portrange';
235 o.depends({ proto: 'tcp', '!contains': true });
236 o.depends({ proto: 'udp', '!contains': true });
237
238 o = s.taboption('advanced', widgets.DeviceSelect, 'device', _('Outbound device'),
239 _('Matches forwarded traffic using the specified outbound network device.'));
240 o.noaliases = true;
241 o.modalonly = true;
242 o.rmempty = true;
243
244 fwtool.addMarkOption(s, false);
245 fwtool.addLimitOption(s);
246 fwtool.addLimitBurstOption(s);
247
248 o = s.taboption('advanced', form.Value, 'extra', _('Extra arguments'),
249 _('Passes additional arguments to iptables. Use with care!'));
250 o.modalonly = true;
251 o.rmempty = true;
252
253 o = s.taboption('timed', form.MultiValue, 'weekdays', _('Week Days'));
254 o.modalonly = true;
255 o.multiple = true;
256 o.display = 5;
257 o.placeholder = _('Any day');
258 o.value('Sun', _('Sunday'));
259 o.value('Mon', _('Monday'));
260 o.value('Tue', _('Tuesday'));
261 o.value('Wed', _('Wednesday'));
262 o.value('Thu', _('Thursday'));
263 o.value('Fri', _('Friday'));
264 o.value('Sat', _('Saturday'));
265 o.write = function(section_id, value) {
266 return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
267 };
268
269 o = s.taboption('timed', form.MultiValue, 'monthdays', _('Month Days'));
270 o.modalonly = true;
271 o.multiple = true;
272 o.display_size = 15;
273 o.placeholder = _('Any day');
274 o.write = function(section_id, value) {
275 return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
276 };
277 for (var i = 1; i <= 31; i++)
278 o.value(i);
279
280 o = s.taboption('timed', form.Value, 'start_time', _('Start Time (hh.mm.ss)'));
281 o.modalonly = true;
282 o.datatype = 'timehhmmss';
283
284 o = s.taboption('timed', form.Value, 'stop_time', _('Stop Time (hh.mm.ss)'));
285 o.modalonly = true;
286 o.datatype = 'timehhmmss';
287
288 o = s.taboption('timed', form.Value, 'start_date', _('Start Date (yyyy-mm-dd)'));
289 o.modalonly = true;
290 o.datatype = 'dateyyyymmdd';
291
292 o = s.taboption('timed', form.Value, 'stop_date', _('Stop Date (yyyy-mm-dd)'));
293 o.modalonly = true;
294 o.datatype = 'dateyyyymmdd';
295
296 o = s.taboption('timed', form.Flag, 'utc_time', _('Time in UTC'));
297 o.modalonly = true;
298 o.default = o.disabled;
299
300 return m.render();
301 }
302 });