rules.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. 'use strict';
  2. 'require view';
  3. 'require ui';
  4. 'require rpc';
  5. 'require uci';
  6. 'require form';
  7. 'require firewall as fwmodel';
  8. 'require tools.firewall as fwtool';
  9. 'require tools.widgets as widgets';
  10. function rule_proto_txt(s, ctHelpers) {
  11. var f = (uci.get('firewall', s, 'family') || '').toLowerCase().replace(/^(?:any|\*)$/, '');
  12. var proto = L.toArray(uci.get('firewall', s, 'proto')).filter(function(p) {
  13. return (p != '*' && p != 'any' && p != 'all');
  14. }).map(function(p) {
  15. var pr = fwtool.lookupProto(p);
  16. return {
  17. num: pr[0],
  18. name: pr[1],
  19. types: (pr[0] == 1 || pr[0] == 58) ? L.toArray(uci.get('firewall', s, 'icmp_type')) : null
  20. };
  21. });
  22. m = String(uci.get('firewall', s, 'helper') || '').match(/^(!\s*)?(\S+)$/);
  23. var h = m ? {
  24. val: m[0].toUpperCase(),
  25. inv: m[1],
  26. name: (ctHelpers.filter(function(ctH) { return ctH.name.toLowerCase() == m[2].toLowerCase() })[0] || {}).description
  27. } : null;
  28. 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);
  29. var w = m ? {
  30. val: m[0].toUpperCase().replace(/X/g, 'x'),
  31. inv: m[1],
  32. num: '0x%02X'.format(+m[2]),
  33. mask: m[3] ? '0x%02X'.format(+m[3]) : null
  34. } : null;
  35. m = String(uci.get('firewall', s, 'dscp')).match(/^(!\s*)?(?:(CS[0-7]|BE|AF[1234][123]|EF)|(0x[0-9a-f]{1,2}|[0-9]{1,2}))$/);
  36. var d = m ? {
  37. val: m[0],
  38. inv: m[1],
  39. name: m[2],
  40. num: m[3] ? '0x%02X'.format(+m[3]) : null
  41. } : null;
  42. return fwtool.fmt(_('%{src?%{dest?Forwarded:Incoming}:Outgoing} %{ipv6?%{ipv4?<var>IPv4</var> and <var>IPv6</var>:<var>IPv6</var>}:<var>IPv4</var>}%{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>}%{dscp?, DSCP %{dscp.inv?<var data-tooltip="Match DSCP classifications except %{dscp.num?:%{dscp.name}}">%{dscp.val}</var>:<var>%{dscp.val}</var>}}%{helper?, helper %{helper.inv?<var data-tooltip="Match any helper except &quot;%{helper.name}&quot;">%{helper.val}</var>:<var data-tooltip="%{helper.name}">%{helper.val}</var>}}'), {
  43. ipv4: (!f || f == 'ipv4'),
  44. ipv6: (!f || f == 'ipv6'),
  45. src: uci.get('firewall', s, 'src'),
  46. dest: uci.get('firewall', s, 'dest'),
  47. proto: proto,
  48. helper: h,
  49. mark: w,
  50. dscp: d
  51. });
  52. }
  53. function rule_src_txt(s, hosts) {
  54. var z = uci.get('firewall', s, 'src'),
  55. d = (uci.get('firewall', s, 'direction') == 'in') ? uci.get('firewall', s, 'device') : null;
  56. 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>}}%{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>}}'), {
  57. src: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel.getColorForName((z && z != '*') ? z : null) }, [(z == '*') ? E('em', _('any zone')) : (z || E('em', _('this device')))]),
  58. src_ip: fwtool.map_invert(uci.get('firewall', s, 'src_ip'), 'toLowerCase'),
  59. src_mac: fwtool.map_invert(uci.get('firewall', s, 'src_mac'), 'toUpperCase').map(function(v) { return Object.assign(v, { hint: hosts[v.val] }) }),
  60. src_port: fwtool.map_invert(uci.get('firewall', s, 'src_port')),
  61. src_device: d
  62. });
  63. }
  64. function rule_dest_txt(s) {
  65. var z = uci.get('firewall', s, 'dest'),
  66. d = (uci.get('firewall', s, 'direction') == 'out') ? uci.get('firewall', s, 'device') : null;
  67. return fwtool.fmt(_('To %{dest}%{dest_device?, 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>}}'), {
  68. dest: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel.getColorForName((z && z != '*') ? z : null) }, [(z == '*') ? E('em', _('any zone')) : (z || E('em', _('this device')))]),
  69. dest_ip: fwtool.map_invert(uci.get('firewall', s, 'dest_ip'), 'toLowerCase'),
  70. dest_port: fwtool.map_invert(uci.get('firewall', s, 'dest_port')),
  71. dest_device: d
  72. });
  73. }
  74. function rule_limit_txt(s) {
  75. var m = String(uci.get('firewall', s, 'limit')).match(/^(\d+)\/([smhd])\w*$/i),
  76. l = m ? {
  77. num: +m[1],
  78. unit: ({ s: _('second'), m: _('minute'), h: _('hour'), d: _('day') })[m[2]],
  79. burst: uci.get('firewall', s, 'limit_burst')
  80. } : null;
  81. if (!l)
  82. return '';
  83. 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 });
  84. }
  85. function rule_target_txt(s, ctHelpers) {
  86. var t = uci.get('firewall', s, 'target'),
  87. h = (uci.get('firewall', s, 'set_helper') || '').toUpperCase(),
  88. s = {
  89. target: t,
  90. src: uci.get('firewall', s, 'src'),
  91. dest: uci.get('firewall', s, 'dest'),
  92. set_helper: h,
  93. set_mark: uci.get('firewall', s, 'set_mark'),
  94. set_xmark: uci.get('firewall', s, 'set_xmark'),
  95. set_dscp: uci.get('firewall', s, 'set_dscp'),
  96. helper_name: (ctHelpers.filter(function(ctH) { return ctH.name.toUpperCase() == h })[0] || {}).description
  97. };
  98. switch (t) {
  99. case 'DROP':
  100. return fwtool.fmt(_('<var data-tooltip="DROP">Drop</var> %{src?%{dest?forward:input}:output}'), s);
  101. case 'ACCEPT':
  102. return fwtool.fmt(_('<var data-tooltip="ACCEPT">Accept</var> %{src?%{dest?forward:input}:output}'), s);
  103. case 'REJECT':
  104. return fwtool.fmt(_('<var data-tooltip="REJECT">Reject</var> %{src?%{dest?forward:input}:output}'), s);
  105. case 'NOTRACK':
  106. return fwtool.fmt(_('<var data-tooltip="NOTRACK">Do not track</var> %{src?%{dest?forward:input}:output}'), s);
  107. case 'HELPER':
  108. return fwtool.fmt(_('<var data-tooltip="HELPER">Assign conntrack</var> helper <var%{helper_name? data-tooltip="%{helper_name}"}>%{set_helper}</var>'), s);
  109. case 'MARK':
  110. return fwtool.fmt(_('<var data-tooltip="MARK">%{set_mark?Assign:XOR}</var> firewall mark <var>%{set_mark?:%{set_xmark}}</var>'), s);
  111. case 'DSCP':
  112. return fwtool.fmt(_('<var data-tooltip="DSCP">Assign DSCP</var> classification <var>%{set_dscp}</var>'), s);
  113. default:
  114. return t;
  115. }
  116. }
  117. return view.extend({
  118. callHostHints: rpc.declare({
  119. object: 'luci-rpc',
  120. method: 'getHostHints',
  121. expect: { '': {} }
  122. }),
  123. callConntrackHelpers: rpc.declare({
  124. object: 'luci',
  125. method: 'getConntrackHelpers',
  126. expect: { result: [] }
  127. }),
  128. load: function() {
  129. return Promise.all([
  130. this.callHostHints(),
  131. this.callConntrackHelpers(),
  132. uci.load('firewall')
  133. ]);
  134. },
  135. render: function(data) {
  136. if (fwtool.checkLegacySNAT())
  137. return fwtool.renderMigration();
  138. else
  139. return this.renderRules(data);
  140. },
  141. renderRules: function(data) {
  142. var hosts = data[0],
  143. ctHelpers = data[1],
  144. m, s, o;
  145. m = new form.Map('firewall', _('Firewall - Traffic Rules'),
  146. _('Traffic rules define policies for packets traveling between different zones, for example to reject traffic between certain hosts or to open WAN ports on the router.'));
  147. s = m.section(form.GridSection, 'rule', _('Traffic Rules'));
  148. s.addremove = true;
  149. s.anonymous = true;
  150. s.sortable = true;
  151. s.tab('general', _('General Settings'));
  152. s.tab('advanced', _('Advanced Settings'));
  153. s.tab('timed', _('Time Restrictions'));
  154. s.filter = function(section_id) {
  155. return (uci.get('firewall', section_id, 'target') != 'SNAT');
  156. };
  157. s.sectiontitle = function(section_id) {
  158. return uci.get('firewall', section_id, 'name') || _('Unnamed rule');
  159. };
  160. s.handleAdd = function(ev) {
  161. var config_name = this.uciconfig || this.map.config,
  162. section_id = uci.add(config_name, this.sectiontype),
  163. opt1 = this.getOption('src'),
  164. opt2 = this.getOption('dest');
  165. opt1.default = 'wan';
  166. opt2.default = 'lan';
  167. this.addedSection = section_id;
  168. this.renderMoreOptionsModal(section_id);
  169. delete opt1.default;
  170. delete opt2.default;
  171. };
  172. o = s.taboption('general', form.Value, 'name', _('Name'));
  173. o.placeholder = _('Unnamed rule');
  174. o.modalonly = true;
  175. o = s.option(form.DummyValue, '_match', _('Match'));
  176. o.modalonly = false;
  177. o.textvalue = function(s) {
  178. return E('small', [
  179. rule_proto_txt(s, ctHelpers), E('br'),
  180. rule_src_txt(s, hosts), E('br'),
  181. rule_dest_txt(s), E('br'),
  182. rule_limit_txt(s)
  183. ]);
  184. };
  185. o = s.option(form.ListValue, '_target', _('Action'));
  186. o.modalonly = false;
  187. o.textvalue = function(s) {
  188. return rule_target_txt(s, ctHelpers);
  189. };
  190. o = s.option(form.Flag, 'enabled', _('Enable'));
  191. o.modalonly = false;
  192. o.default = o.enabled;
  193. o.editable = true;
  194. o.tooltip = function(section_id) {
  195. var weekdays = uci.get('firewall', section_id, 'weekdays');
  196. var monthdays = uci.get('firewall', section_id, 'monthdays');
  197. var start_time = uci.get('firewall', section_id, 'start_time');
  198. var stop_time = uci.get('firewall', section_id, 'stop_time');
  199. var start_date = uci.get('firewall', section_id, 'start_date');
  200. var stop_date = uci.get('firewall', section_id, 'stop_date');
  201. if (weekdays || monthdays || start_time || stop_time || start_date || stop_date )
  202. return _('Time restritions are enabled for this rule');
  203. return null;
  204. };
  205. o = s.taboption('advanced', form.ListValue, 'direction', _('Match device'));
  206. o.modalonly = true;
  207. o.value('', _('unspecified'));
  208. o.value('in', _('Inbound device'));
  209. o.value('out', _('Outbound device'));
  210. o.cfgvalue = function(section_id) {
  211. var val = uci.get('firewall', section_id, 'direction');
  212. switch (val) {
  213. case 'in':
  214. case 'ingress':
  215. return 'in';
  216. case 'out':
  217. case 'egress':
  218. return 'out';
  219. }
  220. return null;
  221. };
  222. o = s.taboption('advanced', widgets.DeviceSelect, 'device', _('Device name'),
  223. _('Specifies whether to tie this traffic rule to a specific inbound or outbound network device.'));
  224. o.modalonly = true;
  225. o.noaliases = true;
  226. o.rmempty = false;
  227. o.depends('direction', 'in');
  228. o.depends('direction', 'out');
  229. o = s.taboption('advanced', form.ListValue, 'family', _('Restrict to address family'));
  230. o.modalonly = true;
  231. o.rmempty = true;
  232. o.value('', _('IPv4 and IPv6'));
  233. o.value('ipv4', _('IPv4 only'));
  234. o.value('ipv6', _('IPv6 only'));
  235. o.validate = function(section_id, value) {
  236. fwtool.updateHostHints(this.map, section_id, 'src_ip', value, hosts);
  237. fwtool.updateHostHints(this.map, section_id, 'dest_ip', value, hosts);
  238. return true;
  239. };
  240. o = s.taboption('general', fwtool.CBIProtocolSelect, 'proto', _('Protocol'));
  241. o.modalonly = true;
  242. o.default = 'tcp udp';
  243. o = s.taboption('advanced', form.MultiValue, 'icmp_type', _('Match ICMP type'));
  244. o.modalonly = true;
  245. o.multiple = true;
  246. o.custom = true;
  247. o.cast = 'table';
  248. o.placeholder = _('any');
  249. o.value('', 'any');
  250. o.value('address-mask-reply');
  251. o.value('address-mask-request');
  252. o.value('communication-prohibited');
  253. o.value('destination-unreachable');
  254. o.value('echo-reply');
  255. o.value('echo-request');
  256. o.value('fragmentation-needed');
  257. o.value('host-precedence-violation');
  258. o.value('host-prohibited');
  259. o.value('host-redirect');
  260. o.value('host-unknown');
  261. o.value('host-unreachable');
  262. o.value('ip-header-bad');
  263. o.value('neighbour-advertisement');
  264. o.value('neighbour-solicitation');
  265. o.value('network-prohibited');
  266. o.value('network-redirect');
  267. o.value('network-unknown');
  268. o.value('network-unreachable');
  269. o.value('packet-too-big');
  270. o.value('parameter-problem');
  271. o.value('port-unreachable');
  272. o.value('precedence-cutoff');
  273. o.value('protocol-unreachable');
  274. o.value('redirect');
  275. o.value('required-option-missing');
  276. o.value('router-advertisement');
  277. o.value('router-solicitation');
  278. o.value('source-quench');
  279. o.value('source-route-failed');
  280. o.value('time-exceeded');
  281. o.value('timestamp-reply');
  282. o.value('timestamp-request');
  283. o.value('TOS-host-redirect');
  284. o.value('TOS-host-unreachable');
  285. o.value('TOS-network-redirect');
  286. o.value('TOS-network-unreachable');
  287. o.value('ttl-zero-during-reassembly');
  288. o.value('ttl-zero-during-transit');
  289. o.depends({ proto: 'icmp', '!contains': true });
  290. o.depends({ proto: 'icmpv6', '!contains': true });
  291. o = s.taboption('general', widgets.ZoneSelect, 'src', _('Source zone'));
  292. o.modalonly = true;
  293. o.nocreate = true;
  294. o.allowany = true;
  295. o.allowlocal = 'src';
  296. fwtool.addMACOption(s, 'advanced', 'src_mac', _('Source MAC address'), null, hosts);
  297. fwtool.addIPOption(s, 'general', 'src_ip', _('Source address'), null, '', hosts, true);
  298. o = s.taboption('general', form.Value, 'src_port', _('Source port'));
  299. o.modalonly = true;
  300. o.datatype = 'list(neg(portrange))';
  301. o.placeholder = _('any');
  302. o.depends({ proto: 'tcp', '!contains': true });
  303. o.depends({ proto: 'udp', '!contains': true });
  304. o = s.taboption('general', widgets.ZoneSelect, 'dest', _('Destination zone'));
  305. o.modalonly = true;
  306. o.nocreate = true;
  307. o.allowany = true;
  308. o.allowlocal = true;
  309. fwtool.addIPOption(s, 'general', 'dest_ip', _('Destination address'), null, '', hosts, true);
  310. o = s.taboption('general', form.Value, 'dest_port', _('Destination port'));
  311. o.modalonly = true;
  312. o.datatype = 'list(neg(portrange))';
  313. o.placeholder = _('any');
  314. o.depends({ proto: 'tcp', '!contains': true });
  315. o.depends({ proto: 'udp', '!contains': true });
  316. o = s.taboption('general', form.ListValue, 'target', _('Action'));
  317. o.modalonly = true;
  318. o.default = 'ACCEPT';
  319. o.value('DROP', _('drop'));
  320. o.value('ACCEPT', _('accept'));
  321. o.value('REJECT', _('reject'));
  322. o.value('NOTRACK', _("don't track"));
  323. o.value('HELPER', _('assign conntrack helper'));
  324. o.value('MARK_SET', _('apply firewall mark'));
  325. o.value('MARK_XOR', _('XOR firewall mark'));
  326. o.value('DSCP', _('DSCP classification'));
  327. o.cfgvalue = function(section_id) {
  328. var t = uci.get('firewall', section_id, 'target'),
  329. m = uci.get('firewall', section_id, 'set_mark');
  330. if (t == 'MARK')
  331. return m ? 'MARK_SET' : 'MARK_XOR';
  332. return t;
  333. };
  334. o.write = function(section_id, value) {
  335. return this.super('write', [section_id, (value == 'MARK_SET' || value == 'MARK_XOR') ? 'MARK' : value]);
  336. };
  337. fwtool.addMarkOption(s, 1);
  338. fwtool.addMarkOption(s, 2);
  339. fwtool.addDSCPOption(s, true);
  340. o = s.taboption('general', form.ListValue, 'set_helper', _('Tracking helper'), _('Assign the specified connection tracking helper to matched traffic.'));
  341. o.modalonly = true;
  342. o.placeholder = _('any');
  343. o.depends('target', 'HELPER');
  344. for (var i = 0; i < ctHelpers.length; i++)
  345. o.value(ctHelpers[i].name, '%s (%s)'.format(ctHelpers[i].description, ctHelpers[i].name.toUpperCase()));
  346. o = s.taboption('advanced', form.Value, 'helper', _('Match helper'), _('Match traffic using the specified connection tracking helper.'));
  347. o.modalonly = true;
  348. o.placeholder = _('any');
  349. for (var i = 0; i < ctHelpers.length; i++)
  350. o.value(ctHelpers[i].name, '%s (%s)'.format(ctHelpers[i].description, ctHelpers[i].name.toUpperCase()));
  351. o.validate = function(section_id, value) {
  352. if (value == '' || value == null)
  353. return true;
  354. value = value.replace(/^!\s*/, '');
  355. for (var i = 0; i < ctHelpers.length; i++)
  356. if (value == ctHelpers[i].name)
  357. return true;
  358. return _('Unknown or not installed conntrack helper "%s"').format(value);
  359. };
  360. fwtool.addMarkOption(s, false);
  361. fwtool.addDSCPOption(s, false);
  362. fwtool.addLimitOption(s);
  363. fwtool.addLimitBurstOption(s);
  364. o = s.taboption('advanced', form.Value, 'extra', _('Extra arguments'),
  365. _('Passes additional arguments to iptables. Use with care!'));
  366. o.modalonly = true;
  367. o = s.taboption('timed', form.MultiValue, 'weekdays', _('Week Days'));
  368. o.modalonly = true;
  369. o.multiple = true;
  370. o.display = 5;
  371. o.placeholder = _('Any day');
  372. o.value('Sun', _('Sunday'));
  373. o.value('Mon', _('Monday'));
  374. o.value('Tue', _('Tuesday'));
  375. o.value('Wed', _('Wednesday'));
  376. o.value('Thu', _('Thursday'));
  377. o.value('Fri', _('Friday'));
  378. o.value('Sat', _('Saturday'));
  379. o.write = function(section_id, value) {
  380. return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
  381. };
  382. o = s.taboption('timed', form.MultiValue, 'monthdays', _('Month Days'));
  383. o.modalonly = true;
  384. o.multiple = true;
  385. o.display_size = 15;
  386. o.placeholder = _('Any day');
  387. o.write = function(section_id, value) {
  388. return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
  389. };
  390. for (var i = 1; i <= 31; i++)
  391. o.value(i);
  392. o = s.taboption('timed', form.Value, 'start_time', _('Start Time (hh.mm.ss)'));
  393. o.modalonly = true;
  394. o.datatype = 'timehhmmss';
  395. o = s.taboption('timed', form.Value, 'stop_time', _('Stop Time (hh.mm.ss)'));
  396. o.modalonly = true;
  397. o.datatype = 'timehhmmss';
  398. o = s.taboption('timed', form.Value, 'start_date', _('Start Date (yyyy-mm-dd)'));
  399. o.modalonly = true;
  400. o.datatype = 'dateyyyymmdd';
  401. o = s.taboption('timed', form.Value, 'stop_date', _('Stop Date (yyyy-mm-dd)'));
  402. o.modalonly = true;
  403. o.datatype = 'dateyyyymmdd';
  404. o = s.taboption('timed', form.Flag, 'utc_time', _('Time in UTC'));
  405. o.modalonly = true;
  406. o.default = o.disabled;
  407. return m.render();
  408. }
  409. });