5135ef1e990cf315e31ab51926f0a5c39e2038bb
[project/firewall4.git] / root / usr / share / ucode / fw4.uc
1 {%
2
3 let fs = require("fs");
4 let uci = require("uci");
5 let ubus = require("ubus");
6
7 let STATEFILE = "/var/run/fw4.state";
8
9 let PARSE_LIST = 0x01;
10 let FLATTEN_LIST = 0x02;
11 let NO_INVERT = 0x04;
12 let UNSUPPORTED = 0x08;
13 let REQUIRED = 0x10;
14 let DEPRECATED = 0x20;
15
16 let ipv4_icmptypes = {
17 "any": [ 0xFF, 0, 0xFF ],
18 "echo-reply": [ 0, 0, 0xFF ],
19 "pong": [ 0, 0, 0xFF ], /* Alias */
20
21 "destination-unreachable": [ 3, 0, 0xFF ],
22 "network-unreachable": [ 3, 0, 0 ],
23 "host-unreachable": [ 3, 1, 1 ],
24 "protocol-unreachable": [ 3, 2, 2 ],
25 "port-unreachable": [ 3, 3, 3 ],
26 "fragmentation-needed": [ 3, 4, 4 ],
27 "source-route-failed": [ 3, 5, 5 ],
28 "network-unknown": [ 3, 6, 6 ],
29 "host-unknown": [ 3, 7, 7 ],
30 "network-prohibited": [ 3, 9, 9 ],
31 "host-prohibited": [ 3, 10, 10 ],
32 "TOS-network-unreachable": [ 3, 11, 11 ],
33 "TOS-host-unreachable": [ 3, 12, 12 ],
34 "communication-prohibited": [ 3, 13, 13 ],
35 "host-precedence-violation": [ 3, 14, 14 ],
36 "precedence-cutoff": [ 3, 15, 15 ],
37
38 "source-quench": [ 4, 0, 0xFF ],
39
40 "redirect": [ 5, 0, 0xFF ],
41 "network-redirect": [ 5, 0, 0 ],
42 "host-redirect": [ 5, 1, 1 ],
43 "TOS-network-redirect": [ 5, 2, 2 ],
44 "TOS-host-redirect": [ 5, 3, 3 ],
45
46 "echo-request": [ 8, 0, 0xFF ],
47 "ping": [ 8, 0, 0xFF ], /* Alias */
48
49 "router-advertisement": [ 9, 0, 0xFF ],
50
51 "router-solicitation": [ 10, 0, 0xFF ],
52
53 "time-exceeded": [ 11, 0, 0xFF ],
54 "ttl-exceeded": [ 11, 0, 0xFF ], /* Alias */
55 "ttl-zero-during-transit": [ 11, 0, 0 ],
56 "ttl-zero-during-reassembly": [ 11, 1, 1 ],
57
58 "parameter-problem": [ 12, 0, 0xFF ],
59 "ip-header-bad": [ 12, 0, 0 ],
60 "required-option-missing": [ 12, 1, 1 ],
61
62 "timestamp-request": [ 13, 0, 0xFF ],
63
64 "timestamp-reply": [ 14, 0, 0xFF ],
65
66 "address-mask-request": [ 17, 0, 0xFF ],
67
68 "address-mask-reply": [ 18, 0, 0xFF ]
69 };
70
71 let ipv6_icmptypes = {
72 "destination-unreachable": [ 1, 0, 0xFF ],
73 "no-route": [ 1, 0, 0 ],
74 "communication-prohibited": [ 1, 1, 1 ],
75 "address-unreachable": [ 1, 3, 3 ],
76 "port-unreachable": [ 1, 4, 4 ],
77
78 "packet-too-big": [ 2, 0, 0xFF ],
79
80 "time-exceeded": [ 3, 0, 0xFF ],
81 "ttl-exceeded": [ 3, 0, 0xFF ], /* Alias */
82 "ttl-zero-during-transit": [ 3, 0, 0 ],
83 "ttl-zero-during-reassembly": [ 3, 1, 1 ],
84
85 "parameter-problem": [ 4, 0, 0xFF ],
86 "bad-header": [ 4, 0, 0 ],
87 "unknown-header-type": [ 4, 1, 1 ],
88 "unknown-option": [ 4, 2, 2 ],
89
90 "echo-request": [ 128, 0, 0xFF ],
91 "ping": [ 128, 0, 0xFF ], /* Alias */
92
93 "echo-reply": [ 129, 0, 0xFF ],
94 "pong": [ 129, 0, 0xFF ], /* Alias */
95
96 "router-solicitation": [ 133, 0, 0xFF ],
97
98 "router-advertisement": [ 134, 0, 0xFF ],
99
100 "neighbour-solicitation": [ 135, 0, 0xFF ],
101 "neighbor-solicitation": [ 135, 0, 0xFF ], /* Alias */
102
103 "neighbour-advertisement": [ 136, 0, 0xFF ],
104 "neighbor-advertisement": [ 136, 0, 0xFF ], /* Alias */
105
106 "redirect": [ 137, 0, 0xFF ]
107 };
108
109 let dscp_classes = {
110 "CS0": 0x00,
111 "CS1": 0x08,
112 "CS2": 0x10,
113 "CS3": 0x18,
114 "CS4": 0x20,
115 "CS5": 0x28,
116 "CS6": 0x30,
117 "CS7": 0x38,
118 "BE": 0x00,
119 "LE": 0x01,
120 "AF11": 0x0a,
121 "AF12": 0x0c,
122 "AF13": 0x0e,
123 "AF21": 0x12,
124 "AF22": 0x14,
125 "AF23": 0x16,
126 "AF31": 0x1a,
127 "AF32": 0x1c,
128 "AF33": 0x1e,
129 "AF41": 0x22,
130 "AF42": 0x24,
131 "AF43": 0x26,
132 "EF": 0x2e
133 };
134
135 function to_mask(bits, v6) {
136 let m = [];
137
138 if (bits < 0 || bits > (v6 ? 128 : 32))
139 return null;
140
141 for (let i = 0; i < (v6 ? 16 : 4); i++) {
142 let b = (bits < 8) ? bits : 8;
143 m[i] = (0xff << (8 - b)) & 0xff;
144 bits -= b;
145 }
146
147 return arrtoip(m);
148 }
149
150 function to_bits(mask) {
151 let a = iptoarr(mask);
152
153 if (!a)
154 return null;
155
156 let bits = 0;
157
158 for (let i = 0, z = false; i < length(a); i++) {
159 z = z || !a[i];
160
161 while (!z && (a[i] & 0x80)) {
162 a[i] = (a[i] << 1) & 0xff;
163 bits++;
164 }
165
166 if (a[i])
167 return null;
168 }
169
170 return bits;
171 }
172
173 function apply_mask(addr, mask) {
174 let a = iptoarr(addr);
175
176 if (!a)
177 return null;
178
179 if (type(mask) == "int") {
180 for (let i = 0; i < length(a); i++) {
181 let b = (mask < 8) ? mask : 8;
182 a[i] &= (0xff << (8 - b)) & 0xff;
183 mask -= b;
184 }
185 }
186 else {
187 let m = iptoarr(mask);
188
189 if (!m || length(a) != length(m))
190 return null;
191
192 for (let i = 0; i < length(a); i++)
193 a[i] &= m[i];
194 }
195
196 return arrtoip(a);
197 }
198
199 function to_array(x) {
200 if (type(x) == "array")
201 return x;
202
203 if (x == null)
204 return [];
205
206 if (type(x) == "object")
207 return [ x ];
208
209 x = trim("" + x);
210
211 return (x == "") ? [] : split(x, /[ \t]+/);
212 }
213
214 function filter_pos(x) {
215 let rv = filter(x, e => !e.invert);
216 return length(rv) ? rv : null;
217 }
218
219 function filter_neg(x) {
220 let rv = filter(x, e => e.invert);
221 return length(rv) ? rv : null;
222 }
223
224 function null_if_empty(x) {
225 return length(x) ? x : null;
226 }
227
228 function subnets_split_af(x) {
229 let rv = [];
230
231 for (let ag in to_array(x)) {
232 for (let a in filter(ag.addrs, a => (a.family == 4))) {
233 rv[0] = rv[0] || [];
234 push(rv[0], { ...a, invert: ag.invert });
235 }
236
237 for (let a in filter(ag.addrs, a => (a.family == 6))) {
238 rv[1] = rv[1] || [];
239 push(rv[1], { ...a, invert: ag.invert });
240 }
241 }
242
243 return rv;
244 }
245
246 function subnets_group_by_masking(x) {
247 let groups = [], plain = [], nc = [], invert_plain = [], invert_masked = [];
248
249 for (let a in to_array(x)) {
250 if (a.bits == -1 && !a.invert)
251 push(nc, a);
252 else if (!a.invert)
253 push(plain, a);
254 else if (a.bits == -1)
255 push(invert_masked, a);
256 else
257 push(invert_plain, a);
258 }
259
260 for (let a in nc)
261 push(groups, [ null, null_if_empty(invert_plain), [ a, ...invert_masked ] ]);
262
263 if (length(plain)) {
264 push(groups, [
265 plain,
266 null_if_empty(invert_plain),
267 null_if_empty(invert_masked)
268 ]);
269 }
270 else if (!length(groups)) {
271 push(groups, [
272 null,
273 null_if_empty(invert_plain),
274 null_if_empty(invert_masked)
275 ]);
276 }
277
278 return groups;
279 }
280
281 function ensure_tcpudp(x) {
282 if (length(filter(x, p => (p.name == "tcp" || p.name == "udp"))))
283 return true;
284
285 let rest = filter(x, p => !p.any),
286 any = filter(x, p => p.any);
287
288 if (length(any) && !length(rest)) {
289 splice(x, 0);
290 push(x, { name: "tcp" }, { name: "udp" });
291 return true;
292 }
293
294 return false;
295 }
296
297 let is_family = (x, v) => (!x.family || x.family == v);
298 let family_is_ipv4 = (x) => (!x.family || x.family == 4);
299 let family_is_ipv6 = (x) => (!x.family || x.family == 6);
300
301 function infer_family(f, objects) {
302 let res = f;
303 let by = null;
304
305 for (let i = 0; i < length(objects); i += 2) {
306 let objs = to_array(objects[i]),
307 desc = objects[i + 1];
308
309 for (let obj in objs) {
310 if (!obj || !obj.family || obj.family == res)
311 continue;
312
313 if (res == 0) {
314 res = obj.family;
315 by = obj.desc;
316 continue;
317 }
318
319 return by
320 ? sprintf('references IPv%d only %s but is restricted to IPv%d by %s', obj.family, desc, res, by)
321 : sprintf('is restricted to IPv%d but referenced %s is IPv%d only', res, desc, obj.family);
322 }
323 }
324
325 return res;
326 }
327
328 function map_setmatch(set, match, proto) {
329 if (!set || (('inet_service' in set.types) && proto != 'tcp' && proto != 'udp'))
330 return null;
331
332 let fields = [];
333
334 for (let i, t in set.types) {
335 let dir = (((match.dir && match.dir[i]) || set.directions[i] || 'src') == 'src' ? 's' : 'd');
336
337 switch (t) {
338 case 'ipv4_addr':
339 fields[i] = sprintf('ip %saddr', dir);
340 break;
341
342 case 'ipv6_addr':
343 fields[i] = sprintf('ip6 %saddr', dir);
344 break;
345
346 case 'ether_addr':
347 if (dir != 's')
348 return NaN;
349
350 fields[i] = 'ether saddr';
351 break;
352
353 case 'inet_service':
354 fields[i] = sprintf('%s %sport', proto, dir);
355 break;
356 }
357 }
358
359 return fields;
360 }
361
362 function resolve_lower_devices(devstatus, devname) {
363 let dir = fs.opendir("/sys/class/net/" + devname);
364 let devs = [];
365
366 if (dir) {
367 if (!devstatus || devstatus[devname]?.["hw-tc-offload"]) {
368 push(devs, devname);
369 }
370 else {
371 let e;
372
373 while ((e = dir.read()) != null)
374 if (index(e, "lower_") === 0)
375 push(devs, ...resolve_lower_devices(devstatus, substr(e, 6)));
376 }
377
378 dir.close();
379 }
380
381 return devs;
382 }
383
384 function nft_json_command(...args) {
385 let cmd = [ "/usr/sbin/nft", "--terse", "--json", ...args ];
386 let nft = fs.popen(join(" ", cmd), "r");
387 let info;
388
389 if (nft) {
390 try {
391 info = filter(json(nft.read("all"))?.nftables,
392 item => (type(item) == "object" && !item.metainfo));
393 }
394 catch (e) {
395 warn(sprintf("Unable to parse nftables JSON output: %s\n", e));
396 }
397
398 nft.close();
399 }
400 else {
401 warn(sprintf("Unable to popen() %s: %s\n", cmd, fs.error()));
402 }
403
404 return info || [];
405 }
406
407 function nft_try_hw_offload(devices) {
408 let nft_test =
409 'add table inet fw4-hw-offload-test; ' +
410 'add flowtable inet fw4-hw-offload-test ft { ' +
411 'hook ingress priority 0; ' +
412 'devices = { "' + join('", "', devices) + '" }; ' +
413 'flags offload; ' +
414 '}';
415
416 let rc = system(sprintf("/usr/sbin/nft -c '%s' 2>/dev/null", replace(nft_test, "'", "'\\''")));
417
418 return (rc == 0);
419 }
420
421
422 return {
423 read_kernel_version: function() {
424 let fd = fs.open("/proc/version", "r"),
425 v = 0;
426
427 if (fd) {
428 let m = match(fd.read("line"), /^Linux version ([0-9]+)\.([0-9]+)\.([0-9]+)/);
429
430 v = m ? (+m[1] << 24) | (+m[2] << 16) | (+m[3] << 8) : 0;
431 fd.close();
432 }
433
434 return v;
435 },
436
437 resolve_offload_devices: function() {
438 if (!this.default_option("flow_offloading"))
439 return [];
440
441 let devstatus = null;
442 let devices = [];
443
444 if (this.default_option("flow_offloading_hw")) {
445 let bus = ubus.connect();
446
447 if (bus) {
448 devstatus = bus.call("network.device", "status") || {};
449 bus.disconnect();
450 }
451
452 for (let zone in this.zones())
453 for (let device in zone.match_devices)
454 push(devices, ...resolve_lower_devices(devstatus, device));
455
456 devices = uniq(devices);
457
458 if (nft_try_hw_offload(devices))
459 return devices;
460
461 this.warn('Hardware flow offloading unavailable, falling back to software offloading');
462 this.state.defaults.flow_offloading_hw = false;
463 }
464
465 devices = [];
466
467 for (let zone in this.zones())
468 for (let device in zone.match_devices)
469 push(devices, ...resolve_lower_devices(null, device));
470
471 return uniq(devices);
472 },
473
474 check_set_types: function() {
475 let sets = {};
476
477 for (let item in nft_json_command("list", "sets", "inet"))
478 if (item.set?.table == "fw4")
479 sets[item.set.name] = (type(item.set.type) == "array") ? item.set.type : [ item.set.type ];
480
481 return sets;
482 },
483
484 check_flowtable: function() {
485 for (let item in nft_json_command("list", "flowtables", "inet"))
486 if (item.flowtable?.table == "fw4" && item.flowtable?.name == "ft")
487 return true;
488
489 return false;
490 },
491
492 read_state: function() {
493 let fd = fs.open(STATEFILE, "r");
494 let state = null;
495
496 if (fd) {
497 try {
498 state = json(fd.read("all"));
499 }
500 catch (e) {
501 warn(sprintf("Unable to parse '%s': %s\n", STATEFILE, e));
502 }
503
504 fd.close();
505 }
506
507 return state;
508 },
509
510 read_ubus: function() {
511 let self = this,
512 ifaces, services,
513 rules = [], networks = {},
514 bus = ubus.connect();
515
516 if (bus) {
517 ifaces = bus.call("network.interface", "dump");
518 services = bus.call("service", "get_data", { "type": "firewall" });
519
520 bus.disconnect();
521 }
522 else {
523 warn(sprintf("Unable to connect to ubus: %s\n", ubus.error()));
524 }
525
526
527 //
528 // Gather logical network information from ubus
529 //
530
531 if (type(ifaces) == "object" && type(ifaces.interface) == "array") {
532 for (let ifc in ifaces.interface) {
533 let net = {
534 up: ifc.up,
535 device: ifc.l3_device,
536 zone: ifc.data?.zone
537 };
538
539 if (type(ifc["ipv4-address"]) == "array") {
540 for (let addr in ifc["ipv4-address"]) {
541 net.ipaddrs = net.ipaddrs || [];
542 push(net.ipaddrs, {
543 family: 4,
544 addr: addr.address,
545 mask: to_mask(addr.mask, false),
546 bits: addr.mask
547 });
548 }
549 }
550
551 if (type(ifc["ipv6-address"]) == "array") {
552 for (let addr in ifc["ipv6-address"]) {
553 net.ipaddrs = net.ipaddrs || [];
554 push(net.ipaddrs, {
555 family: 6,
556 addr: addr.address,
557 mask: to_mask(addr.mask, true),
558 bits: addr.mask
559 });
560 }
561 }
562
563 if (type(ifc["ipv6-prefix-assignment"]) == "array") {
564 for (let addr in ifc["ipv6-prefix-assignment"]) {
565 if (addr["local-address"]) {
566 net.ipaddrs = net.ipaddrs || [];
567 push(net.ipaddrs, {
568 family: 6,
569 addr: addr["local-address"].address,
570 mask: to_mask(addr["local-address"].mask, true),
571 bits: addr["local-address"].mask
572 });
573 }
574 }
575 }
576
577 if (type(ifc.data) == "object" && type(ifc.data.firewall) == "array") {
578 let n = 0;
579
580 for (let rulespec in ifc.data.firewall) {
581 push(rules, {
582 ...rulespec,
583
584 name: (rulespec.type != 'ipset') ? sprintf('ubus:%s[%s] %s %d', ifc.interface, ifc.proto, rulespec.type || 'rule', n) : rulespec.name,
585 device: rulespec.device || ifc.l3_device
586 });
587
588 n++;
589 }
590 }
591
592 networks[ifc.interface] = net;
593 }
594 }
595
596
597 //
598 // Gather firewall rule definitions from ubus services
599 //
600
601 if (type(services) == "object") {
602 for (let svcname, service in services) {
603 if (type(service) == "object" && type(service.firewall) == "array") {
604 let n = 0;
605
606 for (let rulespec in services[svcname].firewall) {
607 push(rules, {
608 ...rulespec,
609
610 name: (rulespec.type != 'ipset') ? sprintf('ubus:%s %s %d', svcname, rulespec.type || 'rule', n) : rulespec.name
611 });
612
613 n++;
614 }
615 }
616
617 for (let svcinst, instance in service) {
618 if (type(instance) == "object" && type(instance.firewall) == "array") {
619 let n = 0;
620
621 for (let rulespec in instance.firewall) {
622 push(rules, {
623 ...rulespec,
624
625 name: (rulespec.type != 'ipset') ? sprintf('ubus:%s[%s] %s %d', svcname, svcinst, rulespec.type || 'rule', n) : rulespec.name
626 });
627
628 n++;
629 }
630 }
631 }
632 }
633 }
634
635 return {
636 networks: networks,
637 ubus_rules: rules
638 };
639 },
640
641 load: function(use_statefile) {
642 let self = this;
643
644 this.state = use_statefile ? this.read_state() : null;
645
646 this.cursor = uci.cursor();
647 this.cursor.load("firewall");
648 this.cursor.load("/usr/share/firewall4/helpers");
649
650 if (!this.state)
651 this.state = this.read_ubus();
652
653 this.kernel = this.read_kernel_version();
654
655
656 //
657 // Read helper mapping
658 //
659
660 this.cursor.foreach("helpers", "helper", h => self.parse_helper(h));
661
662
663 //
664 // Read default policies
665 //
666
667 this.cursor.foreach("firewall", "defaults", d => self.parse_defaults(d));
668
669 if (!this.state.defaults)
670 this.parse_defaults({});
671
672
673 //
674 // Build list of ipsets
675 //
676
677 if (!this.state.ipsets) {
678 map(filter(this.state.ubus_rules, n => (n.type == "ipset")), s => self.parse_ipset(s));
679 this.cursor.foreach("firewall", "ipset", s => self.parse_ipset(s));
680 }
681
682
683 //
684 // Build list of logical zones
685 //
686
687 if (!this.state.zones)
688 this.cursor.foreach("firewall", "zone", z => self.parse_zone(z));
689
690
691 //
692 // Build list of rules
693 //
694
695 map(filter(this.state.ubus_rules, r => (r.type == "rule")), r => self.parse_rule(r));
696 this.cursor.foreach("firewall", "rule", r => self.parse_rule(r));
697
698
699 //
700 // Build list of forwardings
701 //
702
703 this.cursor.foreach("firewall", "forwarding", f => self.parse_forwarding(f));
704
705
706 //
707 // Build list of redirects
708 //
709
710 map(filter(this.state.ubus_rules, r => (r.type == "redirect")), r => self.parse_redirect(r));
711 this.cursor.foreach("firewall", "redirect", r => self.parse_redirect(r));
712
713
714 //
715 // Build list of snats
716 //
717
718 map(filter(this.state.ubus_rules, n => (n.type == "nat")), n => self.parse_nat(n));
719 this.cursor.foreach("firewall", "nat", n => self.parse_nat(n));
720
721
722 if (use_statefile) {
723 let fd = fs.open(STATEFILE, "w");
724
725 if (fd) {
726 fd.write({
727 zones: this.state.zones,
728 ipsets: this.state.ipsets,
729 networks: this.state.networks,
730 ubus_rules: this.state.ubus_rules
731 });
732
733 fd.close();
734 }
735 else {
736 warn("Unable to write '%s': %s\n", STATEFILE, fs.error());
737 }
738 }
739 },
740
741 warn: function(fmt, ...args) {
742 if (getenv("QUIET"))
743 return;
744
745 let msg = sprintf(fmt, ...args);
746
747 if (getenv("TTY"))
748 warn("\033[33m", msg, "\033[m\n");
749 else
750 warn("[!] ", msg, "\n");
751 },
752
753 get: function(sid, opt) {
754 return this.cursor.get("firewall", sid, opt);
755 },
756
757 get_all: function(sid) {
758 return this.cursor.get_all("firewall", sid);
759 },
760
761 parse_options: function(s, spec) {
762 let rv = {};
763
764 for (let key, val in spec) {
765 let datatype = "parse_" + val[0],
766 defval = val[1],
767 flags = val[2] || 0,
768 parsefn = (flags & PARSE_LIST) ? "parse_list" : "parse_opt";
769
770 let res = this[parsefn](s, key, datatype, defval, flags);
771
772 if (res !== res)
773 return false;
774
775 if (type(res) == "object" && res.invert && (flags & NO_INVERT)) {
776 this.warn_section(s, "option '" + key + '" must not be negated');
777 return false;
778 }
779
780 if (res != null) {
781 if (flags & DEPRECATED)
782 this.warn_section(s, "option '" + key + "' is deprecated by fw4");
783 else if (flags & UNSUPPORTED)
784 this.warn_section(s, "option '" + key + "' is not supported by fw4");
785 else
786 rv[key] = res;
787 }
788 }
789
790 for (let opt in s) {
791 if (index(opt, '.') != 0 && opt != 'type' && !exists(spec, opt)) {
792 this.warn_section(s, "specifies unknown option '" + opt + "'");
793 }
794 }
795
796 return rv;
797 },
798
799 parse_subnet: function(subnet) {
800 let parts = split(subnet, "/");
801 let a, b, m, n;
802
803 switch (length(parts)) {
804 case 2:
805 a = iptoarr(parts[0]);
806 m = iptoarr(parts[1]);
807
808 if (!a)
809 return null;
810
811 if (m) {
812 if (length(a) != length(m))
813 return null;
814
815 b = to_bits(parts[1]);
816
817 /* allow non-contiguous masks such as `::ffff:ffff:ffff:ffff` */
818 if (b == null) {
819 b = -1;
820
821 for (let i, x in m)
822 a[i] &= x;
823 }
824
825 m = arrtoip(m);
826 }
827 else {
828 b = +parts[1];
829
830 if (type(b) != "int")
831 return null;
832
833 m = to_mask(b, length(a) == 16);
834 }
835
836 return [{
837 family: (length(a) == 16) ? 6 : 4,
838 addr: arrtoip(a),
839 mask: m,
840 bits: b
841 }];
842
843 case 1:
844 parts = split(parts[0], "-");
845
846 switch (length(parts)) {
847 case 2:
848 a = iptoarr(parts[0]);
849 b = iptoarr(parts[1]);
850
851 if (a && b && length(a) == length(b)) {
852 return [{
853 family: (length(a) == 16) ? 6 : 4,
854 addr: arrtoip(a),
855 addr2: arrtoip(b),
856 range: true
857 }];
858 }
859
860 break;
861
862 case 1:
863 a = iptoarr(parts[0]);
864
865 if (a) {
866 return [{
867 family: (length(a) == 16) ? 6 : 4,
868 addr: arrtoip(a),
869 mask: to_mask(length(a) * 8, length(a) == 16),
870 bits: length(a) * 8
871 }];
872 }
873
874 n = this.state.networks[parts[0]];
875
876 if (n)
877 return [ ...(n.ipaddrs || []) ];
878 }
879 }
880
881 return null;
882 },
883
884 parse_enum: function(val, choices) {
885 if (type(val) == "string") {
886 val = lc(val);
887
888 for (let i = 0; i < length(choices); i++)
889 if (substr(choices[i], 0, length(val)) == val)
890 return choices[i];
891 }
892
893 return null;
894 },
895
896 section_id: function(sid) {
897 let s = this.get_all(sid);
898
899 if (!s)
900 return null;
901
902 if (s[".anonymous"]) {
903 let c = 0;
904
905 this.cursor.foreach("firewall", s[".type"], function(ss) {
906 if (ss[".name"] == s[".name"])
907 return false;
908
909 c++;
910 });
911
912 return sprintf("@%s[%d]", s[".type"], c);
913 }
914
915 return s[".name"];
916 },
917
918 warn_section: function(s, msg) {
919 if (s[".name"]) {
920 if (s.name)
921 this.warn("Section %s (%s) %s", this.section_id(s[".name"]), s.name, msg);
922 else
923 this.warn("Section %s %s", this.section_id(s[".name"]), msg);
924 }
925 else {
926 if (s.name)
927 this.warn("ubus %s (%s) %s", s.type || "rule", s.name, msg);
928 else
929 this.warn("ubus %s %s", s.type || "rule", msg);
930 }
931 },
932
933 parse_policy: function(val) {
934 return this.parse_enum(val, [
935 "accept",
936 "reject",
937 "drop"
938 ]);
939 },
940
941 parse_bool: function(val) {
942 if (val == "1" || val == "on" || val == "true" || val == "yes")
943 return true;
944 else if (val == "0" || val == "off" || val == "false" || val == "no")
945 return false;
946 else
947 return null;
948 },
949
950 parse_family: function(val) {
951 if (val == 'any' || val == 'all' || val == '*')
952 return 0;
953 else if (val == 'inet' || index(val, '4') > -1)
954 return 4;
955 else if (index(val, '6') > -1)
956 return 6;
957
958 return null;
959 },
960
961 parse_zone_ref: function(val) {
962 if (val == null)
963 return null;
964
965 if (val == '*')
966 return { any: true };
967
968 for (let zone in this.state.zones) {
969 if (zone.name == val) {
970 return {
971 any: false,
972 zone: zone
973 };
974 }
975 }
976
977 return null;
978 },
979
980 parse_device: function(val) {
981 let rv = this.parse_invert(val);
982
983 if (!rv)
984 return null;
985
986 if (rv.val == '*')
987 rv.any = true;
988 else
989 rv.device = rv.val;
990
991 return rv;
992 },
993
994 parse_direction: function(val) {
995 if (val == 'in' || val == 'ingress')
996 return false;
997 else if (val == 'out' || val == 'egress')
998 return true;
999
1000 return null;
1001 },
1002
1003 parse_setmatch: function(val) {
1004 let rv = this.parse_invert(val);
1005
1006 if (!rv)
1007 return null;
1008
1009 rv.val = trim(replace(rv.val, /^[^ \t]+/, function(m) {
1010 rv.name = m;
1011 return '';
1012 }));
1013
1014 let dir = split(rv.val, /[ \t,]/);
1015
1016 for (let i = 0; i < 3 && i < length(dir); i++) {
1017 if (dir[i] == "dst" || dir[i] == "dest") {
1018 rv.dir = rv.dir || [];
1019 rv.dir[i] = "dst";
1020 }
1021 else if (dir[i] == "src") {
1022 rv.dir = rv.dir || [];
1023 rv.dir[i] = "src";
1024 }
1025 }
1026
1027 return length(rv.name) ? rv : null;
1028 },
1029
1030 parse_cthelper: function(val) {
1031 let rv = this.parse_invert(val);
1032
1033 if (!rv)
1034 return null;
1035
1036 let helper = filter(this.state.helpers, h => (h.name == rv.val))[0];
1037
1038 return helper ? { ...rv, ...helper } : null;
1039 },
1040
1041 parse_protocol: function(val) {
1042 let p = this.parse_invert(val);
1043
1044 if (!p)
1045 return null;
1046
1047 p.val = lc(p.val);
1048
1049 switch (p.val) {
1050 case 'all':
1051 case 'any':
1052 case '*':
1053 p.any = true;
1054 break;
1055
1056 case '1':
1057 case 'icmp':
1058 p.name = 'icmp';
1059 break;
1060
1061 case '58':
1062 case 'icmpv6':
1063 case 'ipv6-icmp':
1064 p.name = 'ipv6-icmp';
1065 break;
1066
1067 case 'tcpudp':
1068 return [
1069 { invert: p.invert, name: 'tcp' },
1070 { invert: p.invert, name: 'udp' }
1071 ];
1072
1073 case '6':
1074 p.name = 'tcp';
1075 break;
1076
1077 case '17':
1078 p.name = 'udp';
1079 break;
1080
1081 default:
1082 p.name = p.val;
1083 }
1084
1085 return (p.any || length(p.name)) ? p : null;
1086 },
1087
1088 parse_mac: function(val) {
1089 let mac = this.parse_invert(val);
1090 let m = mac ? match(mac.val, /^([0-9a-f]{1,2})[:-]([0-9a-f]{1,2})[:-]([0-9a-f]{1,2})[:-]([0-9a-f]{1,2})[:-]([0-9a-f]{1,2})[:-]([0-9a-f]{1,2})$/i) : null;
1091
1092 if (!m)
1093 return null;
1094
1095 mac.mac = sprintf('%02x:%02x:%02x:%02x:%02x:%02x',
1096 hex(m[1]), hex(m[2]), hex(m[3]),
1097 hex(m[4]), hex(m[5]), hex(m[6]));
1098
1099 return mac;
1100 },
1101
1102 parse_port: function(val) {
1103 let port = this.parse_invert(val);
1104 let m = port ? match(port.val, /^([0-9]{1,5})([-:]([0-9]{1,5}))?$/i) : null;
1105
1106 if (!m)
1107 return null;
1108
1109 if (m[3]) {
1110 let min_port = +m[1];
1111 let max_port = +m[3];
1112
1113 if (min_port > max_port ||
1114 min_port < 0 || max_port < 0 ||
1115 min_port > 65535 || max_port > 65535)
1116 return null;
1117
1118 port.min = min_port;
1119 port.max = max_port;
1120 }
1121 else {
1122 let pn = +m[1];
1123
1124 if (pn != pn || pn < 0 || pn > 65535)
1125 return null;
1126
1127 port.min = pn;
1128 port.max = pn;
1129 }
1130
1131 return port;
1132 },
1133
1134 parse_network: function(val) {
1135 let rv = this.parse_invert(val);
1136
1137 if (!rv)
1138 return null;
1139
1140 let nets = this.parse_subnet(rv.val);
1141
1142 if (nets === null)
1143 return null;
1144
1145 if (length(nets))
1146 rv.addrs = [ ...nets ];
1147
1148 return rv;
1149 },
1150
1151 parse_icmptype: function(val) {
1152 let rv = {};
1153
1154 if (exists(ipv4_icmptypes, val)) {
1155 rv.family = 4;
1156
1157 rv.type = ipv4_icmptypes[val][0];
1158 rv.code_min = ipv4_icmptypes[val][1];
1159 rv.code_max = ipv4_icmptypes[val][2];
1160 }
1161
1162 if (exists(ipv6_icmptypes, val)) {
1163 rv.family = rv.family ? 0 : 6;
1164
1165 rv.type6 = ipv6_icmptypes[val][0];
1166 rv.code6_min = ipv6_icmptypes[val][1];
1167 rv.code6_max = ipv6_icmptypes[val][2];
1168 }
1169
1170 if (!exists(rv, "family")) {
1171 let m = match(val, /^([0-9]+)(\/([0-9]+))?$/);
1172
1173 if (!m)
1174 return null;
1175
1176 if (m[3]) {
1177 rv.type = +m[1];
1178 rv.code_min = +m[3];
1179 rv.code_max = rv.code_min;
1180 }
1181 else {
1182 rv.type = +m[1];
1183 rv.code_min = 0;
1184 rv.code_max = 0xFF;
1185 }
1186
1187 if (rv.type > 0xFF || rv.code_min > 0xFF || rv.code_max > 0xFF)
1188 return null;
1189
1190 rv.family = 0;
1191
1192 rv.type6 = rv.type;
1193 rv.code6_min = rv.code_min;
1194 rv.code6_max = rv.code_max;
1195 }
1196
1197 return rv;
1198 },
1199
1200 parse_invert: function(val) {
1201 if (val == null)
1202 return null;
1203
1204 let rv = { invert: false };
1205
1206 rv.val = trim(replace(val, /^[ \t]*!/, () => (rv.invert = true, '')));
1207
1208 return length(rv.val) ? rv : null;
1209 },
1210
1211 parse_limit: function(val) {
1212 let rv = this.parse_invert(val);
1213 let m = rv ? match(rv.val, /^([0-9]+)(\/([a-z]+))?$/) : null;
1214
1215 if (!m)
1216 return null;
1217
1218 let n = +m[1];
1219 let u = m[3] ? this.parse_enum(m[3], [ "second", "minute", "hour", "day" ]) : "second";
1220
1221 if (!u)
1222 return null;
1223
1224 rv.rate = n;
1225 rv.unit = u;
1226
1227 return rv;
1228 },
1229
1230 parse_int: function(val) {
1231 let n = +val;
1232
1233 return (n == n) ? n : null;
1234 },
1235
1236 parse_date: function(val) {
1237 let m = match(val, /^([0-9-]+)T([0-9:]+)$/);
1238 let d = m ? match(m[1], /^([0-9]{1,4})(-([0-9]{1,2})(-([0-9]{1,2}))?)?$/) : null;
1239 let t = this.parse_time(m[2]);
1240
1241 d[3] = d[3] || 1;
1242 d[5] = d[5] || 1;
1243
1244 if (d == null || d[1] < 1970 || d[1] > 2038 || d[3] < 1 || d[3] > 12 || d[5] < 1 || d[5] > 31)
1245 return null;
1246
1247 if (m[2] && !t)
1248 return null;
1249
1250 return {
1251 year: +d[1],
1252 month: +d[3],
1253 day: +d[5],
1254 hour: t ? +t[1] : 0,
1255 min: t ? +t[3] : 0,
1256 sec: t ? +t[5] : 0
1257 };
1258 },
1259
1260 parse_time: function(val) {
1261 let t = match(val, /^([0-9]{1,2})(:([0-9]{1,2})(:([0-9]{1,2}))?)?$/);
1262
1263 if (t == null || t[1] > 23 || t[3] > 59 || t[5] > 59)
1264 return null;
1265
1266 return {
1267 hour: +t[1],
1268 min: +t[3],
1269 sec: +t[5]
1270 };
1271 },
1272
1273 parse_weekdays: function(val) {
1274 let rv = this.parse_invert(val);
1275
1276 if (!rv)
1277 return null;
1278
1279 for (let day in to_array(rv.val)) {
1280 day = this.parse_enum(day, [
1281 "monday",
1282 "tuesday",
1283 "wednesday",
1284 "thursday",
1285 "friday",
1286 "saturday",
1287 "sunday"
1288 ]);
1289
1290 if (!day)
1291 return null;
1292
1293 rv.days = rv.days || {};
1294 rv.days[day] = true;
1295 }
1296
1297 rv.days = keys(rv.days);
1298
1299 return rv.days ? rv : null;
1300 },
1301
1302 parse_monthdays: function(val) {
1303 let rv = this.parse_invert(val);
1304
1305 if (!rv)
1306 return null;
1307
1308 for (let day in to_array(rv.val)) {
1309 day = +day;
1310
1311 if (day < 1 || day > 31)
1312 return null;
1313
1314 rv.days = rv.days || [];
1315 rv.days[day] = true;
1316 }
1317
1318 return rv.days ? rv : null;
1319 },
1320
1321 parse_mark: function(val) {
1322 let rv = this.parse_invert(val);
1323 let m = rv ? match(rv.val, /^(0?x?[0-9a-f]+)(\/(0?x?[0-9a-f]+))?$/i) : null;
1324
1325 if (!m)
1326 return null;
1327
1328 let n = +m[1];
1329
1330 if (n != n || n > 0xFFFFFFFF)
1331 return null;
1332
1333 rv.mark = n;
1334 rv.mask = 0xFFFFFFFF;
1335
1336 if (m[3]) {
1337 n = +m[3];
1338
1339 if (n != n || n > 0xFFFFFFFF)
1340 return null;
1341
1342 rv.mask = n;
1343 }
1344
1345 return rv;
1346 },
1347
1348 parse_dscp: function(val) {
1349 let rv = this.parse_invert(val);
1350
1351 if (!rv)
1352 return null;
1353
1354 rv.val = uc(rv.val);
1355
1356 if (exists(dscp_classes, rv.val)) {
1357 rv.dscp = dscp_classes[rv.val];
1358 }
1359 else {
1360 let n = +rv.val;
1361
1362 if (n != n || n < 0 || n > 0x3F)
1363 return null;
1364
1365 rv.dscp = n;
1366 }
1367
1368 return rv;
1369 },
1370
1371 parse_target: function(val) {
1372 return this.parse_enum(val, [
1373 "accept",
1374 "reject",
1375 "drop",
1376 "notrack",
1377 "helper",
1378 "mark",
1379 "dscp",
1380 "dnat",
1381 "snat",
1382 "masquerade",
1383 "accept",
1384 "reject",
1385 "drop"
1386 ]);
1387 },
1388
1389 parse_reject_code: function(val) {
1390 return this.parse_enum(val, [
1391 "tcp-reset",
1392 "port-unreachable",
1393 "admin-prohibited",
1394 "host-unreachable",
1395 "no-route"
1396 ]);
1397 },
1398
1399 parse_reflection_source: function(val) {
1400 return this.parse_enum(val, [
1401 "internal",
1402 "external"
1403 ]);
1404 },
1405
1406 parse_ipsettype: function(val) {
1407 let m = match(val, /^(src|dst|dest)_(.+)$/);
1408 let t = this.parse_enum(m ? m[2] : val, [
1409 "ip",
1410 "port",
1411 "mac",
1412 "net",
1413 "set"
1414 ]);
1415
1416 return t ? [ (!m || m[1] == 'src') ? 'src' : 'dst', t ] : null;
1417 },
1418
1419 parse_ipsetentry: function(val, set) {
1420 let values = split(val, /[ \t]+/);
1421
1422 if (length(values) != length(set.types))
1423 return null;
1424
1425 let rv = [];
1426 let ip, mac, port;
1427
1428 for (let i, t in set.types) {
1429 switch (t) {
1430 case 'ipv4_addr':
1431 ip = filter(this.parse_subnet(values[i]), a => (a.family == 4));
1432
1433 switch (length(ip)) {
1434 case 0: return null;
1435 case 1: break;
1436 default: this.warn("Set entry '%s' resolves to multiple addresses, using first one", values[i]);
1437 }
1438
1439 rv[i] = ("net" in set.fw4types) ? ip[0].addr + "/" + ip[0].bits : ip[0].addr;
1440 break;
1441
1442 case 'ipv6_addr':
1443 ip = filter(this.parse_subnet(values[i]), a => (a.family == 6));
1444
1445 switch(length(ip)) {
1446 case 0: return null;
1447 case 1: break;
1448 case 2: this.warn("Set entry '%s' resolves to multiple addresses, using first one", values[i]);
1449 }
1450
1451 rv[i] = ("net" in set.fw4types) ? ip[0].addr + "/" + ip[0].bits : ip[0].addr;
1452
1453 break;
1454
1455 case 'ether_addr':
1456 mac = this.parse_mac(values[i]);
1457
1458 if (!mac || mac.invert)
1459 return null;
1460
1461 rv[i] = mac.mac;
1462 break;
1463
1464 case 'inet_service':
1465 port = this.parse_port(values[i]);
1466
1467 if (!port || port.invert || port.min != port.max)
1468 return null;
1469
1470 rv[i] = port.min;
1471 break;
1472
1473 default:
1474 rv[i] = values[i];
1475 }
1476 }
1477
1478 return length(rv) ? rv : null;
1479 },
1480
1481 parse_string: function(val) {
1482 return "" + val;
1483 },
1484
1485 parse_opt: function(s, opt, fn, defval, flags) {
1486 let val = s[opt];
1487
1488 if (val === null) {
1489 if (flags & REQUIRED) {
1490 this.warn_section(s, "option '" + opt + "' is mandatory but not set");
1491 return NaN;
1492 }
1493
1494 val = defval;
1495 }
1496
1497 if (type(val) == "array") {
1498 this.warn_section(s, "option '" + opt + "' must not be a list");
1499 return NaN;
1500 }
1501 else if (val == null) {
1502 return null;
1503 }
1504
1505 let res = this[fn](val);
1506
1507 if (res === null) {
1508 this.warn_section(s, "option '" + opt + "' specifies invalid value '" + val + "'");
1509 return NaN;
1510 }
1511
1512 return res;
1513 },
1514
1515 parse_list: function(s, opt, fn, defval, flags) {
1516 let val = s[opt];
1517 let rv = [];
1518
1519 if (val == null) {
1520 if (flags & REQUIRED) {
1521 this.warn_section(s, "option '" + opt + "' is mandatory but not set");
1522 return NaN;
1523 }
1524
1525 val = defval;
1526 }
1527
1528 for (val in to_array(val)) {
1529 let res = this[fn](val);
1530
1531 if (res === null) {
1532 this.warn_section(s, "option '" + opt + "' specifies invalid value '" + val + "'");
1533 return NaN;
1534 }
1535
1536 if (flags & FLATTEN_LIST)
1537 push(rv, ...to_array(res));
1538 else
1539 push(rv, res);
1540 }
1541
1542 return length(rv) ? rv : null;
1543 },
1544
1545 quote: function(s, force) {
1546 if (force === true || !match(s, /^([0-9A-Fa-f:.\/-]+)( \. [0-9A-Fa-f:.\/-]+)*$/))
1547 return sprintf('"%s"', replace(s + "", /(["\\])/g, '\\$1'));
1548
1549 return s;
1550 },
1551
1552 cidr: function(a) {
1553 if (a.range)
1554 return sprintf("%s-%s", a.addr, a.addr2);
1555
1556 if ((a.family == 4 && a.bits == 32) ||
1557 (a.family == 6 && a.bits == 128))
1558 return a.addr;
1559
1560 if (a.bits >= 0)
1561 return sprintf("%s/%d", apply_mask(a.addr, a.bits), a.bits);
1562
1563 return sprintf("%s/%s", a.addr, a.mask);
1564 },
1565
1566 host: function(a) {
1567 return a.range
1568 ? sprintf("%s-%s", a.addr, a.addr2)
1569 : apply_mask(a.addr, a.bits);
1570 },
1571
1572 port: function(p) {
1573 if (p.min == p.max)
1574 return sprintf('%d', p.min);
1575
1576 return sprintf('%d-%d', p.min, p.max);
1577 },
1578
1579 set: function(v, force) {
1580 let seen = {};
1581
1582 v = filter(to_array(v), item => !seen[item]++);
1583
1584 if (force || length(v) != 1)
1585 return sprintf('{ %s }', join(', ', map(v, this.quote)));
1586
1587 return this.quote(v[0]);
1588 },
1589
1590 concat: function(v) {
1591 return join(' . ', to_array(v));
1592 },
1593
1594 ipproto: function(family) {
1595 switch (family) {
1596 case 4:
1597 return "ip";
1598
1599 case 6:
1600 return "ip6";
1601 }
1602 },
1603
1604 nfproto: function(family, human_readable) {
1605 switch (family) {
1606 case 4:
1607 return human_readable ? "IPv4" : "ipv4";
1608
1609 case 6:
1610 return human_readable ? "IPv6" : "ipv6";
1611
1612 default:
1613 return human_readable ? "IPv4/IPv6" : null;
1614 }
1615 },
1616
1617 datetime: function(stamp) {
1618 return sprintf('"%04d-%02d-%02d %02d:%02d:%02d"',
1619 stamp.year, stamp.month, stamp.day,
1620 stamp.hour, stamp.min, stamp.sec);
1621 },
1622
1623 date: function(stamp) {
1624 return sprintf('"%04d-%02d-%02d"', stamp.year, stamp.month, stamp.day);
1625 },
1626
1627 time: function(stamp) {
1628 return sprintf('"%02d:%02d:%02d"', stamp.hour, stamp.min, stamp.sec);
1629 },
1630
1631 hex: function(n) {
1632 return sprintf('0x%x', n);
1633 },
1634
1635 is_loopback_dev: function(dev) {
1636 let fd = fs.open(sprintf("/sys/class/net/%s/flags", dev), "r");
1637
1638 if (!fd)
1639 return false;
1640
1641 let flags = +fd.read("line");
1642
1643 fd.close();
1644
1645 return !!(flags & 0x8);
1646 },
1647
1648 is_loopback_addr: function(addr) {
1649 return (index(addr, "127.") == 0 || addr == "::1" || addr == "::1/128");
1650 },
1651
1652 filter_loopback_devs: function(devs, invert) {
1653 return null_if_empty(filter(devs, d => (this.is_loopback_dev(d) == invert)));
1654 },
1655
1656 filter_loopback_addrs: function(addrs, invert) {
1657 return null_if_empty(filter(addrs, a => (this.is_loopback_addr(a) == invert)));
1658 },
1659
1660
1661 input_policy: function(reject_as_drop) {
1662 return (!reject_as_drop || this.state.defaults.input != 'reject') ? this.state.defaults.input : 'drop';
1663 },
1664
1665 output_policy: function(reject_as_drop) {
1666 return (!reject_as_drop || this.state.defaults.output != 'reject') ? this.state.defaults.output : 'drop';
1667 },
1668
1669 forward_policy: function(reject_as_drop) {
1670 return (!reject_as_drop || this.state.defaults.forward != 'reject') ? this.state.defaults.forward : 'drop';
1671 },
1672
1673 default_option: function(flag) {
1674 return this.state.defaults[flag];
1675 },
1676
1677 helpers: function() {
1678 return this.state.helpers;
1679 },
1680
1681 zones: function() {
1682 return this.state.zones;
1683 },
1684
1685 rules: function(chain) {
1686 return filter(this.state.rules, r => (r.chain == chain));
1687 },
1688
1689 redirects: function(chain) {
1690 return filter(this.state.redirects, r => (r.chain == chain));
1691 },
1692
1693 ipsets: function() {
1694 return this.state.ipsets;
1695 },
1696
1697 parse_setfile: function(set, cb) {
1698 let fd = fs.open(set.loadfile, "r");
1699
1700 if (!fd) {
1701 warn(sprintf("Unable to load file '%s' for set '%s': %s\n",
1702 set.loadfile, set.name, fs.error()));
1703 return;
1704 }
1705
1706 let line = null, count = 0;
1707
1708 while ((line = fd.read("line")) !== "") {
1709 line = trim(line);
1710
1711 if (length(line) == 0 || ord(line) == 35)
1712 continue;
1713
1714 let v = this.parse_ipsetentry(line, set);
1715
1716 if (!v) {
1717 this.warn("Skipping invalid entry '%s' in file '%s' for set '%s'",
1718 line, set.loadfile, set.name);
1719 continue;
1720 }
1721
1722 cb(v);
1723
1724 count++;
1725 }
1726
1727 fd.close();
1728
1729 return count;
1730 },
1731
1732 print_setentries: function(set) {
1733 let first = true;
1734 let printer = (entry) => {
1735 if (first) {
1736 print("\t\telements = {\n");
1737 first = false;
1738 }
1739
1740 print("\t\t\t", join(" . ", entry), ",\n");
1741 };
1742
1743 map(set.entries, printer);
1744
1745 if (set.loadfile)
1746 this.parse_setfile(set, printer);
1747
1748 if (!first)
1749 print("\t\t}\n");
1750 },
1751
1752 parse_helper: function(data) {
1753 let helper = this.parse_options(data, {
1754 name: [ "string", null, REQUIRED ],
1755 description: [ "string" ],
1756 module: [ "string" ],
1757 family: [ "family" ],
1758 proto: [ "protocol", null, PARSE_LIST | FLATTEN_LIST | NO_INVERT ],
1759 port: [ "port", null, NO_INVERT ]
1760 });
1761
1762 if (helper === false) {
1763 this.warn("Helper definition '%s' skipped due to invalid options", data.name || data['.name']);
1764 return;
1765 }
1766 else if (helper.proto.any) {
1767 this.warn("Helper definition '%s' must not specify wildcard protocol", data.name || data['.name']);
1768 return;
1769 }
1770 else if (length(helper.proto) > 1) {
1771 this.warn("Helper definition '%s' must not specify multiple protocols", data.name || data['.name']);
1772 return;
1773 }
1774
1775 helper.available = ((fs.stat("/sys/module/" + helper.module) || {}).type == "directory");
1776
1777 this.state.helpers = this.state.helpers || [];
1778 push(this.state.helpers, helper);
1779 },
1780
1781 parse_defaults: function(data) {
1782 if (this.state.defaults) {
1783 this.warn_section(data, ": ignoring duplicate defaults section");
1784 return;
1785 }
1786
1787 let defs = this.parse_options(data, {
1788 input: [ "policy", "drop" ],
1789 output: [ "policy", "drop" ],
1790 forward: [ "policy", "drop" ],
1791
1792 drop_invalid: [ "bool" ],
1793 tcp_reject_code: [ "reject_code", "tcp-reset" ],
1794 any_reject_code: [ "reject_code", "port-unreachable" ],
1795
1796 syn_flood: [ "bool" ],
1797 synflood_protect: [ "bool" ],
1798 synflood_rate: [ "limit", "25/second" ],
1799 synflood_burst: [ "int", "50" ],
1800
1801 tcp_syncookies: [ "bool", "1" ],
1802 tcp_ecn: [ "int" ],
1803 tcp_window_scaling: [ "bool", "1" ],
1804
1805 accept_redirects: [ "bool" ],
1806 accept_source_route: [ "bool" ],
1807
1808 auto_helper: [ "bool", "1" ],
1809 custom_chains: [ "bool", null, UNSUPPORTED ],
1810 disable_ipv6: [ "bool", null, UNSUPPORTED ],
1811 flow_offloading: [ "bool", "0" ],
1812 flow_offloading_hw: [ "bool", "0" ]
1813 });
1814
1815 if (defs.synflood_protect === null)
1816 defs.synflood_protect = defs.syn_flood;
1817
1818 delete defs.syn_flood;
1819
1820 this.state.defaults = defs;
1821 },
1822
1823 parse_zone: function(data) {
1824 let zone = this.parse_options(data, {
1825 enabled: [ "bool", "1" ],
1826
1827 name: [ "string", null, REQUIRED ],
1828 family: [ "family" ],
1829
1830 network: [ "device", null, PARSE_LIST ],
1831 device: [ "device", null, PARSE_LIST ],
1832 subnet: [ "network", null, PARSE_LIST ],
1833
1834 input: [ "policy", this.state.defaults ? this.state.defaults.input : "drop" ],
1835 output: [ "policy", this.state.defaults ? this.state.defaults.output : "drop" ],
1836 forward: [ "policy", this.state.defaults ? this.state.defaults.forward : "drop" ],
1837
1838 masq: [ "bool" ],
1839 masq_allow_invalid: [ "bool" ],
1840 masq_src: [ "network", null, PARSE_LIST ],
1841 masq_dest: [ "network", null, PARSE_LIST ],
1842
1843 masq6: [ "bool" ],
1844
1845 extra: [ "string", null, UNSUPPORTED ],
1846 extra_src: [ "string", null, UNSUPPORTED ],
1847 extra_dest: [ "string", null, UNSUPPORTED ],
1848
1849 mtu_fix: [ "bool" ],
1850 custom_chains: [ "bool", null, UNSUPPORTED ],
1851
1852 log: [ "int" ],
1853 log_limit: [ "limit", null, UNSUPPORTED ],
1854
1855 auto_helper: [ "bool", "1" ],
1856 helper: [ "cthelper", null, PARSE_LIST ],
1857
1858 counter: [ "bool", "1" ]
1859 });
1860
1861 if (zone === false) {
1862 this.warn_section(data, "skipped due to invalid options");
1863 return;
1864 }
1865 else if (!zone.enabled) {
1866 this.warn_section(data, "is disabled, ignoring section");
1867 return;
1868 }
1869 else if (zone.helper && !zone.helper.available) {
1870 this.warn_section(data, "uses unavailable ct helper '" + zone.helper.name + "', ignoring section");
1871 return;
1872 }
1873
1874 if (zone.mtu_fix && this.kernel < 0x040a0000) {
1875 this.warn_section(data, "option 'mtu_fix' requires kernel 4.10 or later");
1876 return;
1877 }
1878
1879 if (this.state.defaults && this.state.defaults.auto_helper === false)
1880 zone.auto_helper = false;
1881
1882 let match_devices = [];
1883 let related_subnets = [];
1884 let related_ubus_networks = [];
1885 let match_subnets, masq_src_subnets, masq_dest_subnets;
1886
1887 for (let name, net in this.state.networks) {
1888 if (net.zone === zone.name)
1889 push(related_ubus_networks, { invert: false, device: name });
1890 }
1891
1892 for (let e in [ ...to_array(zone.network), ...related_ubus_networks ]) {
1893 if (exists(this.state.networks, e.device)) {
1894 let net = this.state.networks[e.device];
1895
1896 if (net.device) {
1897 push(match_devices, {
1898 invert: e.invert,
1899 device: net.device
1900 });
1901 }
1902
1903 push(related_subnets, ...(net.ipaddrs || []));
1904 }
1905 }
1906
1907 push(match_devices, ...to_array(zone.device));
1908
1909 match_subnets = subnets_split_af(zone.subnet);
1910 masq_src_subnets = subnets_split_af(zone.masq_src);
1911 masq_dest_subnets = subnets_split_af(zone.masq_dest);
1912
1913 push(related_subnets, ...(match_subnets[0] || []), ...(match_subnets[1] || []));
1914
1915 let match_rules = [];
1916
1917 let add_rule = (family, devices, subnets, zone) => {
1918 let r = {};
1919
1920 r.family = family;
1921
1922 r.devices_pos = null_if_empty(devices[0]);
1923 r.devices_neg = null_if_empty(devices[1]);
1924 r.devices_neg_wildcard = null_if_empty(devices[2]);
1925
1926 r.subnets_pos = map(subnets[0], this.cidr);
1927 r.subnets_neg = map(subnets[1], this.cidr);
1928 r.subnets_masked = subnets[2];
1929
1930 push(match_rules, r);
1931 };
1932
1933 let family = infer_family(zone.family, [
1934 zone.helper, "ct helper"
1935 ]);
1936
1937 // group non-inverted device matches into wildcard and non-wildcard ones
1938 let devices = [], plain_devices = [], plain_invert_devices = [], wildcard_invert_devices = [];
1939
1940 for (let device in match_devices) {
1941 let m = match(device.device, /^([^+]*)(\+)?$/);
1942
1943 if (!m) {
1944 this.warn_section(data, "skipping invalid wildcard pattern '" + device.device + '"');
1945 continue;
1946 }
1947
1948 // filter `+` (match any device) since nftables does not support
1949 // wildcard only matches
1950 if (!device.invert && m[0] == '+')
1951 continue;
1952
1953 // replace inverted `+` (match no device) with invalid pattern
1954 if (device.invert && m[0] == '+') {
1955 device.device = '/never/';
1956 device.invert = false;
1957 }
1958
1959 // replace "name+" matches with "name*"
1960 else if (m[2] == '+')
1961 device.device = m[1] + '*';
1962
1963 device.wildcard = !!m[2];
1964
1965 if (!device.invert && device.wildcard)
1966 push(devices, [ [ device.device ], plain_invert_devices, wildcard_invert_devices ]);
1967 else if (!device.invert)
1968 push(plain_devices, device.device);
1969 else if (device.wildcard)
1970 push(wildcard_invert_devices, device.device);
1971 else
1972 push(plain_invert_devices, device.device);
1973 }
1974
1975 if (length(plain_devices))
1976 push(devices, [
1977 plain_devices,
1978 plain_invert_devices,
1979 wildcard_invert_devices
1980 ]);
1981 else if (!length(devices))
1982 push(devices, [
1983 null,
1984 plain_invert_devices,
1985 wildcard_invert_devices
1986 ]);
1987
1988 // emit zone jump rules for each device group
1989 if (length(match_devices) || length(match_subnets[0]) || length(match_subnets[1])) {
1990 for (let devgroup in devices) {
1991 // check if there's no AF specific bits, in this case we can do AF agnostic matching
1992 if (!family && !length(match_subnets[0]) && !length(match_subnets[1])) {
1993 add_rule(0, devgroup, [], zone);
1994 }
1995
1996 // we need to emit one or two AF specific rules
1997 else {
1998 if (family_is_ipv4(zone) && length(match_subnets[0]))
1999 for (let subnets in subnets_group_by_masking(match_subnets[0]))
2000 add_rule(4, devgroup, subnets, zone);
2001
2002 if (family_is_ipv6(zone) && length(match_subnets[1]))
2003 for (let subnets in subnets_group_by_masking(match_subnets[1]))
2004 add_rule(6, devgroup, subnets, zone);
2005 }
2006 }
2007 }
2008
2009 zone.match_rules = match_rules;
2010
2011 zone.masq4_src_subnets = subnets_group_by_masking(masq_src_subnets[0]);
2012 zone.masq4_dest_subnets = subnets_group_by_masking(masq_dest_subnets[0]);
2013
2014 zone.masq6_src_subnets = subnets_group_by_masking(masq_src_subnets[1]);
2015 zone.masq6_dest_subnets = subnets_group_by_masking(masq_dest_subnets[1]);
2016
2017 zone.sflags = {};
2018 zone.sflags[zone.input] = true;
2019
2020 zone.dflags = {};
2021 zone.dflags[zone.output] = true;
2022 zone.dflags[zone.forward] = true;
2023
2024 zone.match_devices = map(filter(match_devices, d => !d.invert), d => d.device);
2025 zone.match_subnets = map(filter(related_subnets, s => !s.invert && s.bits != -1), this.cidr);
2026
2027 zone.related_subnets = related_subnets;
2028
2029 if (zone.masq || zone.masq6)
2030 zone.dflags.snat = true;
2031
2032 if ((zone.auto_helper && !(zone.masq || zone.masq6)) || length(zone.helper)) {
2033 zone.dflags.helper = true;
2034
2035 for (let helper in (length(zone.helper) ? zone.helper : this.state.helpers)) {
2036 if (!helper.available)
2037 continue;
2038
2039 for (let proto in helper.proto) {
2040 this.state.rules = this.state.rules || [];
2041 push(this.state.rules, {
2042 chain: "helper_" + zone.name,
2043 family: helper.family,
2044 name: helper.description || helper.name,
2045 proto: proto,
2046 src: zone,
2047 dports_pos: [ this.port(helper.port) ],
2048 target: "helper",
2049 set_helper: helper
2050 });
2051 }
2052 }
2053 }
2054
2055 this.state.zones = this.state.zones || [];
2056 push(this.state.zones, zone);
2057 },
2058
2059 parse_forwarding: function(data) {
2060 let fwd = this.parse_options(data, {
2061 enabled: [ "bool", "1" ],
2062
2063 name: [ "string" ],
2064 family: [ "family" ],
2065
2066 src: [ "zone_ref", null, REQUIRED ],
2067 dest: [ "zone_ref", null, REQUIRED ]
2068 });
2069
2070 if (fwd === false) {
2071 this.warn_section(data, "skipped due to invalid options");
2072 return;
2073 }
2074 else if (!fwd.enabled) {
2075 this.warn_section(data, "is disabled, ignoring section");
2076 return;
2077 }
2078
2079 let add_rule = (family, fwd) => {
2080 let f = {
2081 ...fwd,
2082
2083 family: family,
2084 proto: { any: true }
2085 };
2086
2087 f.name = fwd.name || sprintf("Accept %s to %s forwarding",
2088 fwd.src.any ? "any" : fwd.src.zone.name,
2089 fwd.dest.any ? "any" : fwd.dest.zone.name);
2090
2091 f.chain = fwd.src.any ? "forward" : sprintf("forward_%s", fwd.src.zone.name);
2092
2093 if (fwd.dest.any)
2094 f.target = "accept";
2095 else
2096 f.jump_chain = sprintf("accept_to_%s", fwd.dest.zone.name);
2097
2098 this.state.rules = this.state.rules || [];
2099 push(this.state.rules, f);
2100 };
2101
2102
2103 let family = fwd.family;
2104
2105 /* inherit family restrictions from related zones */
2106 if (family === 0 || family === null) {
2107 let f1 = fwd.src.zone ? fwd.src.zone.family : 0;
2108 let f2 = fwd.dest.zone ? fwd.dest.zone.family : 0;
2109
2110 if (f1 && f2 && f1 != f2) {
2111 this.warn_section(data,
2112 sprintf("references src %s restricted to %s and dest %s restricted to %s, ignoring forwarding",
2113 fwd.src.zone.name, this.nfproto(f1, true),
2114 fwd.dest.zone.name, this.nfproto(f2, true)));
2115
2116 return;
2117 }
2118 else if (f1) {
2119 this.warn_section(data,
2120 sprintf("inheriting %s restriction from src %s",
2121 this.nfproto(f1, true), fwd.src.zone.name));
2122
2123 family = f1;
2124 }
2125 else if (f2) {
2126 this.warn_section(data,
2127 sprintf("inheriting %s restriction from dest %s",
2128 this.nfproto(f2, true), fwd.dest.zone.name));
2129
2130 family = f2;
2131 }
2132 }
2133
2134 add_rule(family, fwd);
2135
2136 if (fwd.dest.zone)
2137 fwd.dest.zone.dflags.accept = true;
2138 },
2139
2140 parse_rule: function(data) {
2141 let rule = this.parse_options(data, {
2142 enabled: [ "bool", "1" ],
2143
2144 name: [ "string", this.section_id(data[".name"]) ],
2145 _name: [ "string", null, DEPRECATED ],
2146 family: [ "family" ],
2147
2148 src: [ "zone_ref" ],
2149 dest: [ "zone_ref" ],
2150
2151 device: [ "device", null, NO_INVERT ],
2152 direction: [ "direction" ],
2153
2154 ipset: [ "setmatch" ],
2155 helper: [ "cthelper" ],
2156 set_helper: [ "cthelper", null, NO_INVERT ],
2157
2158 proto: [ "protocol", "tcpudp", PARSE_LIST | FLATTEN_LIST ],
2159
2160 src_ip: [ "network", null, PARSE_LIST ],
2161 src_mac: [ "mac", null, PARSE_LIST ],
2162 src_port: [ "port", null, PARSE_LIST ],
2163
2164 dest_ip: [ "network", null, PARSE_LIST ],
2165 dest_port: [ "port", null, PARSE_LIST ],
2166
2167 icmp_type: [ "icmptype", null, PARSE_LIST ],
2168 extra: [ "string", null, UNSUPPORTED ],
2169
2170 limit: [ "limit" ],
2171 limit_burst: [ "int" ],
2172
2173 utc_time: [ "bool" ],
2174 start_date: [ "date" ],
2175 stop_date: [ "date" ],
2176 start_time: [ "time" ],
2177 stop_time: [ "time" ],
2178 weekdays: [ "weekdays" ],
2179 monthdays: [ "monthdays", null, UNSUPPORTED ],
2180
2181 mark: [ "mark" ],
2182 set_mark: [ "mark", null, NO_INVERT ],
2183 set_xmark: [ "mark", null, NO_INVERT ],
2184
2185 dscp: [ "dscp" ],
2186 set_dscp: [ "dscp", null, NO_INVERT ],
2187
2188 counter: [ "bool", "1" ],
2189
2190 target: [ "target" ]
2191 });
2192
2193 if (rule === false) {
2194 this.warn_section(data, "skipped due to invalid options");
2195 return;
2196 }
2197 else if (!rule.enabled) {
2198 this.warn_section(data, "is disabled, ignoring section");
2199 return;
2200 }
2201
2202 if (rule.target in ["helper", "notrack"] && (!rule.src || !rule.src.zone)) {
2203 this.warn_section(data, "must specify a source zone for target '" + rule.target + "'");
2204 return;
2205 }
2206 else if (rule.target == "dscp" && !rule.set_dscp) {
2207 this.warn_section(data, "must specify option 'set_dscp' for target 'dscp'");
2208 return;
2209 }
2210 else if (rule.target == "mark" && !rule.set_mark && !rule.set_xmark) {
2211 this.warn_section(data, "must specify option 'set_mark' or 'set_xmark' for target 'mark'");
2212 return;
2213 }
2214 else if (rule.target == "helper" && !rule.set_helper) {
2215 this.warn_section(data, "must specify option 'set_helper' for target 'helper'");
2216 return;
2217 }
2218 else if (rule.device?.any) {
2219 this.warn_section(data, "must not specify '*' as device");
2220 return;
2221 }
2222
2223 let ipset;
2224
2225 if (rule.ipset) {
2226 ipset = filter(this.state.ipsets, s => (s.name == rule.ipset.name))[0];
2227
2228 if (!ipset) {
2229 this.warn_section(data, "references unknown set '" + rule.ipset.name + "'");
2230 return;
2231 }
2232
2233 if (('inet_service' in ipset.types) && !ensure_tcpudp(rule.proto)) {
2234 this.warn_section(data, "references named set with port match but no UDP/TCP protocol, ignoring section");
2235 return;
2236 }
2237 }
2238
2239 let need_src_action_chain = (rule) => (rule.src && rule.src.zone && rule.src.zone.log && rule.target != "accept");
2240
2241 let add_rule = (family, proto, saddrs, daddrs, sports, dports, icmptypes, icmpcodes, ipset, rule) => {
2242 let r = {
2243 ...rule,
2244
2245 family: family,
2246 proto: proto,
2247 has_addrs: !!(saddrs[0] || saddrs[1] || saddrs[2] || daddrs[0] || daddrs[1] || daddrs[2]),
2248 has_ports: !!(length(sports) || length(dports)),
2249 saddrs_pos: map(saddrs[0], this.cidr),
2250 saddrs_neg: map(saddrs[1], this.cidr),
2251 saddrs_masked: saddrs[2],
2252 daddrs_pos: map(daddrs[0], this.cidr),
2253 daddrs_neg: map(daddrs[1], this.cidr),
2254 daddrs_masked: daddrs[2],
2255 sports_pos: map(filter_pos(sports), this.port),
2256 sports_neg: map(filter_neg(sports), this.port),
2257 dports_pos: map(filter_pos(dports), this.port),
2258 dports_neg: map(filter_neg(dports), this.port),
2259 smacs_pos: map(filter_pos(rule.src_mac), m => m.mac),
2260 smacs_neg: map(filter_neg(rule.src_mac), m => m.mac),
2261 icmp_types: map(icmptypes, i => (family == 4 ? i.type : i.type6)),
2262 icmp_codes: map(icmpcodes, ic => sprintf('%d . %d', (family == 4) ? ic.type : ic.type6, (family == 4) ? ic.code_min : ic.code6_min))
2263 };
2264
2265 if (!length(r.icmp_types))
2266 delete r.icmp_types;
2267
2268 if (!length(r.icmp_codes))
2269 delete r.icmp_codes;
2270
2271 if (r.set_mark) {
2272 r.set_xmark = {
2273 invert: r.set_mark.invert,
2274 mark: r.set_mark.mark,
2275 mask: r.set_mark.mark | r.set_mark.mask
2276 };
2277
2278 delete r.set_mark;
2279 }
2280
2281 let set_types = map_setmatch(ipset, rule.ipset, proto.name);
2282
2283 if (set_types !== set_types) {
2284 this.warn_section(data, "destination MAC address matching not supported");
2285 return;
2286 } else if (set_types) {
2287 r.ipset = { ...r.ipset, fields: set_types };
2288 }
2289
2290 if (r.target == "notrack") {
2291 r.chain = sprintf("notrack_%s", r.src.zone.name);
2292 r.src.zone.dflags.notrack = true;
2293 }
2294 else if (r.target == "helper") {
2295 r.chain = sprintf("helper_%s", r.src.zone.name);
2296 r.src.zone.dflags.helper = true;
2297 }
2298 else if (r.target == "mark" || r.target == "dscp") {
2299 if ((r.src?.any && r.dest?.any) || (r.src?.zone && r.dest?.zone))
2300 r.chain = "mangle_forward";
2301 else if (r.src?.any && r.dest?.zone)
2302 r.chain = "mangle_postrouting";
2303 else if (r.src?.zone && r.dest?.any)
2304 r.chain = "mangle_prerouting";
2305 else if (r.src && !r.dest)
2306 r.chain = "mangle_input";
2307 else
2308 r.chain = "mangle_output";
2309
2310 if (r.src?.zone) {
2311 r.src.zone.dflags[r.target] = true;
2312 r.iifnames = null_if_empty(r.src.zone.match_devices);
2313 }
2314
2315 if (r.dest?.zone) {
2316 r.dest.zone.dflags[r.target] = true;
2317 r.oifnames = null_if_empty(r.dest.zone.match_devices);
2318 }
2319 }
2320 else {
2321 r.chain = "output";
2322
2323 if (r.src) {
2324 if (!r.src.any)
2325 r.chain = sprintf("%s_%s", r.dest ? "forward" : "input", r.src.zone.name);
2326 else
2327 r.chain = r.dest ? "forward" : "input";
2328 }
2329
2330 if (r.dest && !r.src) {
2331 if (!r.dest.any)
2332 r.chain = sprintf("output_%s", r.dest.zone.name);
2333 else
2334 r.chain = "output";
2335 }
2336
2337 if (r.dest && !r.dest.any) {
2338 r.jump_chain = sprintf("%s_to_%s", r.target, r.dest.zone.name);
2339 r.dest.zone.dflags[r.target] = true;
2340 }
2341 else if (need_src_action_chain(r)) {
2342 r.jump_chain = sprintf("%s_from_%s", r.target, r.src.zone.name);
2343 r.src.zone.dflags[r.target] = true;
2344 }
2345 else if (r.target == "reject")
2346 r.jump_chain = "handle_reject";
2347 }
2348
2349 if (r.device)
2350 r[r.direction ? "oifnames" : "iifnames"] = [ r.device.device ];
2351
2352 this.state.rules = this.state.rules || [];
2353 push(this.state.rules, r);
2354 };
2355
2356 for (let proto in rule.proto) {
2357 let sip, dip, sports, dports, itypes4, itypes6;
2358 let family = rule.family;
2359
2360 switch (proto.name) {
2361 case "icmp":
2362 itypes4 = filter(rule.icmp_type || [], family_is_ipv4);
2363 itypes6 = filter(rule.icmp_type || [], family_is_ipv6);
2364 break;
2365
2366 case "ipv6-icmp":
2367 family = 6;
2368 itypes6 = filter(rule.icmp_type || [], family_is_ipv6);
2369 break;
2370
2371 case "tcp":
2372 case "udp":
2373 sports = rule.src_port;
2374 dports = rule.dest_port;
2375 break;
2376 }
2377
2378 family = infer_family(family, [
2379 ipset, "set match",
2380 rule.src, "source zone",
2381 rule.dest, "destination zone",
2382 rule.helper, "helper match",
2383 rule.set_helper, "helper to set"
2384 ]);
2385
2386 if (type(family) == "string") {
2387 this.warn_section(data, family + ", skipping");
2388 continue;
2389 }
2390
2391 sip = subnets_split_af(rule.src_ip);
2392 dip = subnets_split_af(rule.dest_ip);
2393
2394 let has_ipv4_specifics = (length(sip[0]) || length(dip[0]) || length(itypes4) || rule.dscp !== null);
2395 let has_ipv6_specifics = (length(sip[1]) || length(dip[1]) || length(itypes6) || rule.dscp !== null);
2396
2397 /* if no family was configured, infer target family from IP addresses */
2398 if (family === null) {
2399 if (has_ipv4_specifics && !has_ipv6_specifics)
2400 family = 4;
2401 else if (has_ipv6_specifics && !has_ipv4_specifics)
2402 family = 6;
2403 else
2404 family = 0;
2405 }
2406
2407 /* check if there's no AF specific bits, in this case we can do an AF agnostic rule */
2408 if (!family && rule.target != "dscp" && !has_ipv4_specifics && !has_ipv6_specifics) {
2409 add_rule(0, proto, [], [], sports, dports, null, null, null, rule);
2410 }
2411
2412 /* we need to emit one or two AF specific rules */
2413 else {
2414 if (family == 0 || family == 4) {
2415 let icmp_types = filter(itypes4, i => (i.code_min == 0 && i.code_max == 0xFF));
2416 let icmp_codes = filter(itypes4, i => (i.code_min != 0 || i.code_max != 0xFF));
2417
2418 for (let saddrs in subnets_group_by_masking(sip[0])) {
2419 for (let daddrs in subnets_group_by_masking(dip[0])) {
2420 if (length(icmp_types) || (!length(icmp_types) && !length(icmp_codes)))
2421 add_rule(4, proto, saddrs, daddrs, sports, dports, icmp_types, null, ipset, rule);
2422
2423 if (length(icmp_codes))
2424 add_rule(4, proto, saddrs, daddrs, sports, dports, null, icmp_codes, ipset, rule);
2425 }
2426 }
2427 }
2428
2429 if (family == 0 || family == 6) {
2430 let icmp_types = filter(itypes6, i => (i.code_min == 0 && i.code_max == 0xFF));
2431 let icmp_codes = filter(itypes6, i => (i.code_min != 0 || i.code_max != 0xFF));
2432
2433 for (let saddrs in subnets_group_by_masking(sip[1])) {
2434 for (let daddrs in subnets_group_by_masking(dip[1])) {
2435 if (length(icmp_types) || (!length(icmp_types) && !length(icmp_codes)))
2436 add_rule(6, proto, saddrs, daddrs, sports, dports, icmp_types, null, ipset, rule);
2437
2438 if (length(icmp_codes))
2439 add_rule(6, proto, saddrs, daddrs, sports, dports, null, icmp_codes, ipset, rule);
2440 }
2441 }
2442 }
2443 }
2444 }
2445 },
2446
2447 parse_redirect: function(data) {
2448 let redir = this.parse_options(data, {
2449 enabled: [ "bool", "1" ],
2450
2451 name: [ "string", this.section_id(data[".name"]) ],
2452 _name: [ "string", null, DEPRECATED ],
2453 family: [ "family" ],
2454
2455 src: [ "zone_ref" ],
2456 dest: [ "zone_ref" ],
2457
2458 ipset: [ "setmatch" ],
2459 helper: [ "cthelper", null, NO_INVERT ],
2460
2461 proto: [ "protocol", "tcpudp", PARSE_LIST | FLATTEN_LIST ],
2462
2463 src_ip: [ "network" ],
2464 src_mac: [ "mac", null, PARSE_LIST ],
2465 src_port: [ "port" ],
2466
2467 src_dip: [ "network" ],
2468 src_dport: [ "port" ],
2469
2470 dest_ip: [ "network" ],
2471 dest_port: [ "port" ],
2472
2473 extra: [ "string", null, UNSUPPORTED ],
2474
2475 limit: [ "limit" ],
2476 limit_burst: [ "int" ],
2477
2478 utc_time: [ "bool" ],
2479 start_date: [ "date" ],
2480 stop_date: [ "date" ],
2481 start_time: [ "time" ],
2482 stop_time: [ "time" ],
2483 weekdays: [ "weekdays" ],
2484 monthdays: [ "monthdays", null, UNSUPPORTED ],
2485
2486 mark: [ "mark" ],
2487
2488 reflection: [ "bool", "1" ],
2489 reflection_src: [ "reflection_source", "internal" ],
2490
2491 reflection_zone: [ "zone_ref", null, PARSE_LIST ],
2492
2493 counter: [ "bool", "1" ],
2494
2495 target: [ "target", "dnat" ]
2496 });
2497
2498 if (redir === false) {
2499 this.warn_section(data, "skipped due to invalid options");
2500 return;
2501 }
2502 else if (!redir.enabled) {
2503 this.warn_section(data, "is disabled, ignoring section");
2504 return;
2505 }
2506
2507 if (!(redir.target in ["dnat", "snat"])) {
2508 this.warn_section(data, "has invalid target specified, defaulting to dnat");
2509 redir.target = "dnat";
2510 }
2511
2512 let ipset;
2513
2514 if (redir.ipset) {
2515 ipset = filter(this.state.ipsets, s => (s.name == redir.ipset.name))[0];
2516
2517 if (!ipset) {
2518 this.warn_section(data, "references unknown set '" + redir.ipset.name + "'");
2519 return;
2520 }
2521
2522 if (('inet_service' in ipset.types) && !ensure_tcpudp(redir.proto)) {
2523 this.warn_section(data, "references named set with port match but no UDP/TCP protocol, ignoring section");
2524 return;
2525 }
2526 }
2527
2528 let resolve_dest = (redir) => {
2529 for (let zone in this.state.zones) {
2530 for (let zone_addr in zone.related_subnets) {
2531 for (let dest_addr in redir.dest_ip.addrs) {
2532 if (dest_addr.family != zone_addr.family)
2533 continue;
2534
2535 let a = apply_mask(dest_addr.addr, zone_addr.mask);
2536 let b = apply_mask(zone_addr.addr, zone_addr.mask);
2537
2538 if (a != b)
2539 continue;
2540
2541 redir.dest = {
2542 any: false,
2543 zone: zone
2544 };
2545
2546 return true;
2547 }
2548 }
2549 }
2550
2551 return false;
2552 };
2553
2554 if (redir.target == "dnat") {
2555 if (!redir.src)
2556 return this.warn_section(data, "has no source specified");
2557 else if (redir.src.any)
2558 return this.warn_section(data, "must not have source '*' for dnat target");
2559 else if (redir.dest_ip && redir.dest_ip.invert)
2560 return this.warn_section(data, "must not specify a negated 'dest_ip' value");
2561 else if (redir.dest_ip && length(filter(redir.dest_ip.addrs, a => a.bits == -1)))
2562 return this.warn_section(data, "must not use non-contiguous masks in 'dest_ip'");
2563
2564 if (!redir.dest && redir.dest_ip && resolve_dest(redir))
2565 this.warn_section(data, "does not specify a destination, assuming '" + redir.dest.zone.name + "'");
2566
2567 if (!redir.dest_port)
2568 redir.dest_port = redir.src_dport;
2569
2570 if (redir.reflection && redir.dest && redir.dest.zone && redir.src.zone.masq) {
2571 redir.dest.zone.dflags.accept = true;
2572 redir.dest.zone.dflags.dnat = true;
2573 redir.dest.zone.dflags.snat = true;
2574 }
2575
2576 if (redir.helper)
2577 redir.src.zone.dflags.helper = true;
2578
2579 redir.src.zone.dflags[redir.target] = true;
2580 }
2581 else {
2582 if (!redir.dest)
2583 return this.warn_section(data, "has no destination specified");
2584 else if (redir.dest.any)
2585 return this.warn_section(data, "must not have destination '*' for snat target");
2586 else if (!redir.src_dip)
2587 return this.warn_section(data, "has no 'src_dip' option specified");
2588 else if (redir.src_dip.invert)
2589 return this.warn_section(data, "must not specify a negated 'src_dip' value");
2590 else if (length(filter(redir.src_dip.addrs, a => a.bits == -1)))
2591 return this.warn_section(data, "must not use non-contiguous masks in 'src_dip'");
2592 else if (redir.src_mac)
2593 return this.warn_section(data, "must not use 'src_mac' option for snat target");
2594 else if (redir.helper)
2595 return this.warn_section(data, "must not use 'helper' option for snat target");
2596
2597 redir.dest.zone.dflags[redir.target] = true;
2598 }
2599
2600
2601 let add_rule = (family, proto, saddrs, daddrs, raddrs, sport, dport, rport, ipset, redir) => {
2602 let r = {
2603 ...redir,
2604
2605 family: family,
2606 proto: proto,
2607 has_addrs: !!(saddrs[0] || saddrs[1] || saddrs[2] || daddrs[0] || daddrs[1] || daddrs[2]),
2608 has_ports: !!(sport || dport || rport),
2609 saddrs_pos: map(saddrs[0], this.cidr),
2610 saddrs_neg: map(saddrs[1], this.cidr),
2611 saddrs_masked: saddrs[2],
2612 daddrs_pos: map(daddrs[0], this.cidr),
2613 daddrs_neg: map(daddrs[1], this.cidr),
2614 daddrs_masked: daddrs[2],
2615 sports_pos: map(filter_pos(to_array(sport)), this.port),
2616 sports_neg: map(filter_neg(to_array(sport)), this.port),
2617 dports_pos: map(filter_pos(to_array(dport)), this.port),
2618 dports_neg: map(filter_neg(to_array(dport)), this.port),
2619 smacs_pos: map(filter_pos(redir.src_mac), m => m.mac),
2620 smacs_neg: map(filter_neg(redir.src_mac), m => m.mac),
2621
2622 raddr: raddrs ? raddrs[0] : null,
2623 rport: rport
2624 };
2625
2626 let set_types = map_setmatch(ipset, redir.ipset, proto.name);
2627
2628 if (set_types !== set_types) {
2629 this.warn_section(data, "destination MAC address matching not supported");
2630 return;
2631 } else if (set_types) {
2632 r.ipset = { ...r.ipset, fields: set_types };
2633 }
2634
2635 switch (r.target) {
2636 case "dnat":
2637 r.chain = sprintf("dstnat_%s", r.src.zone.name);
2638 r.src.zone.dflags.dnat = true;
2639
2640 if (!r.raddr)
2641 r.target = "redirect";
2642
2643 break;
2644
2645 case "snat":
2646 r.chain = sprintf("srcnat_%s", r.dest.zone.name);
2647 r.dest.zone.dflags.snat = true;
2648 break;
2649 }
2650
2651 this.state.redirects = this.state.redirects || [];
2652 push(this.state.redirects, r);
2653 };
2654
2655 let to_hostaddr = (a) => {
2656 let bits = (a.family == 4) ? 32 : 128;
2657
2658 return {
2659 family: a.family,
2660 addr: apply_mask(a.addr, bits),
2661 bits: bits
2662 };
2663 };
2664
2665 for (let proto in redir.proto) {
2666 let sip, dip, rip, iip, eip, refip, sport, dport, rport;
2667 let family = redir.family;
2668
2669 if (proto.name == "ipv6-icmp")
2670 family = 6;
2671
2672 family = infer_family(family, [
2673 ipset, "set match",
2674 redir.src, "source zone",
2675 redir.dest, "destination zone",
2676 redir.helper, "helper match"
2677 ]);
2678
2679 if (type(family) == "string") {
2680 this.warn_section(data, family + ", skipping");
2681 continue;
2682 }
2683
2684 switch (redir.target) {
2685 case "dnat":
2686 sip = subnets_split_af(redir.src_ip);
2687 dip = subnets_split_af(redir.src_dip);
2688 rip = subnets_split_af(redir.dest_ip);
2689
2690 switch (proto.name) {
2691 case "tcp":
2692 case "udp":
2693 sport = redir.src_port;
2694 dport = redir.src_dport;
2695 rport = redir.dest_port;
2696 break;
2697 }
2698
2699 /* build reflection rules */
2700 if (redir.reflection && (length(rip[0]) || length(rip[1])) && redir.src?.zone && redir.dest?.zone) {
2701 let refredir = {
2702 name: redir.name + " (reflection)",
2703
2704 helper: redir.helper,
2705
2706 // XXX: this likely makes no sense for reflection rules
2707 //src_mac: redir.src_mac,
2708
2709 limit: redir.limit,
2710 limit_burst: redir.limit_burst,
2711
2712 start_date: redir.start_date,
2713 stop_date: redir.stop_date,
2714 start_time: redir.start_time,
2715 stop_time: redir.stop_time,
2716 weekdays: redir.weekdays,
2717
2718 mark: redir.mark
2719 };
2720
2721 let eaddrs = length(dip) ? dip : subnets_split_af({ addrs: map(redir.src.zone.related_subnets, to_hostaddr) });
2722 let rzones = length(redir.reflection_zone) ? redir.reflection_zone : [ redir.dest ];
2723
2724 for (let rzone in rzones) {
2725 if (!is_family(rzone, family)) {
2726 this.warn_section(data,
2727 sprintf("is restricted to IPv%d but referenced reflection zone is IPv%d only, skipping",
2728 family, rzone.family));
2729 continue;
2730 }
2731
2732 let iaddrs = subnets_split_af({ addrs: rzone.zone.related_subnets });
2733 let refaddrs = (redir.reflection_src == "internal") ? iaddrs : eaddrs;
2734
2735 for (let i = 0; i <= 1; i++) {
2736 if (redir.src.zone[i ? "masq6" : "masq"] && length(rip[i])) {
2737 let snat_addr = refaddrs[i]?.[0];
2738
2739 /* For internal reflection sources try to find a suitable candiate IP
2740 * among the reflection zone subnets which is within the same subnet
2741 * as the original DNAT destination. If we can't find any matching
2742 * one then simply take the first candidate. */
2743 if (redir.reflection_src == "internal") {
2744 for (let zone_addr in rzone.zone.related_subnets) {
2745 if (zone_addr.family != rip[i][0].family)
2746 continue;
2747
2748 let r = apply_mask(rip[i][0].addr, zone_addr.mask);
2749 let a = apply_mask(zone_addr.addr, zone_addr.mask);
2750
2751 if (r != a)
2752 continue;
2753
2754 snat_addr = zone_addr;
2755 break;
2756 }
2757 }
2758
2759 if (snat_addr) {
2760 refredir.src = rzone;
2761 refredir.dest = null;
2762 refredir.target = "dnat";
2763
2764 for (let saddrs in subnets_group_by_masking(iaddrs[i]))
2765 for (let daddrs in subnets_group_by_masking(eaddrs[i]))
2766 add_rule(i ? 6 : 4, proto, saddrs, daddrs, rip[i], sport, dport, rport, null, refredir);
2767
2768 refredir.src = null;
2769 refredir.dest = rzone;
2770 refredir.target = "snat";
2771
2772 for (let daddrs in subnets_group_by_masking(rip[i]))
2773 for (let saddrs in subnets_group_by_masking(iaddrs[i]))
2774 add_rule(i ? 6 : 4, proto, saddrs, daddrs, [ to_hostaddr(snat_addr) ], null, rport, null, null, refredir);
2775 }
2776 }
2777 }
2778 }
2779 }
2780
2781
2782 break;
2783
2784 case "snat":
2785 sip = subnets_split_af(redir.src_ip);
2786 dip = subnets_split_af(redir.dest_ip);
2787 rip = subnets_split_af(redir.src_dip);
2788
2789 switch (proto.name) {
2790 case "tcp":
2791 case "udp":
2792 sport = redir.src_port;
2793 dport = redir.dest_port;
2794 rport = redir.src_dport;
2795 break;
2796 }
2797
2798 break;
2799 }
2800
2801 if (length(rip[0]) > 1 || length(rip[1]) > 1)
2802 this.warn_section(data, "specifies multiple rewrite addresses, using only first one");
2803
2804 /* check if there's no AF specific bits, in this case we can do an AF agnostic rule */
2805 if (!family && !length(sip[0]) && !length(sip[1]) && !length(dip[0]) && !length(dip[1]) && !length(rip[0]) && !length(rip[1])) {
2806 /* for backwards compatibility, treat unspecified family as IPv4 unless user explicitly requested any (0) */
2807 if (family == null)
2808 family = 4;
2809
2810 add_rule(family, proto, [], [], null, sport, dport, rport, null, redir);
2811 }
2812
2813 /* we need to emit one or two AF specific rules */
2814 else {
2815 if ((!family || family == 4) && (length(sip[0]) || length(dip[0]) || length(rip[0]))) {
2816 for (let saddrs in subnets_group_by_masking(sip[0]))
2817 for (let daddrs in subnets_group_by_masking(dip[0]))
2818 add_rule(4, proto, saddrs, daddrs, rip[0], sport, dport, rport, ipset, redir);
2819 }
2820
2821 if ((!family || family == 6) && (length(sip[1]) || length(dip[1]) || length(rip[1]))) {
2822 for (let saddrs in subnets_group_by_masking(sip[1]))
2823 for (let daddrs in subnets_group_by_masking(dip[1]))
2824 add_rule(6, proto, saddrs, daddrs, rip[1], sport, dport, rport, ipset, redir);
2825 }
2826 }
2827 }
2828 },
2829
2830 parse_nat: function(data) {
2831 let snat = this.parse_options(data, {
2832 enabled: [ "bool", "1" ],
2833
2834 name: [ "string", this.section_id(data[".name"]) ],
2835 family: [ "family", "4" ],
2836
2837 src: [ "zone_ref" ],
2838 device: [ "string" ],
2839
2840 ipset: [ "setmatch", null, UNSUPPORTED ],
2841
2842 proto: [ "protocol", "all", PARSE_LIST | FLATTEN_LIST ],
2843
2844 src_ip: [ "network" ],
2845 src_port: [ "port" ],
2846
2847 snat_ip: [ "network", null, NO_INVERT ],
2848 snat_port: [ "port", null, NO_INVERT ],
2849
2850 dest_ip: [ "network" ],
2851 dest_port: [ "port" ],
2852
2853 extra: [ "string", null, UNSUPPORTED ],
2854
2855 limit: [ "limit" ],
2856 limit_burst: [ "int" ],
2857
2858 connlimit_ports: [ "bool" ],
2859
2860 utc_time: [ "bool" ],
2861 start_date: [ "date" ],
2862 stop_date: [ "date" ],
2863 start_time: [ "time" ],
2864 stop_time: [ "time" ],
2865 weekdays: [ "weekdays" ],
2866 monthdays: [ "monthdays", null, UNSUPPORTED ],
2867
2868 mark: [ "mark" ],
2869
2870 target: [ "target", "masquerade" ]
2871 });
2872
2873 if (snat === false) {
2874 this.warn_section(data, "skipped due to invalid options");
2875 return;
2876 }
2877 else if (!snat.enabled) {
2878 this.warn_section(data, "is disabled, ignoring section");
2879 return;
2880 }
2881
2882 if (!(snat.target in ["accept", "snat", "masquerade"])) {
2883 this.warn_section(data, "has invalid target specified, defaulting to masquerade");
2884 snat.target = "masquerade";
2885 }
2886
2887 if (snat.target == "snat" && !snat.snat_ip && !snat.snat_port) {
2888 this.warn_section(data, "needs either 'snat_ip' or 'snat_port' for target snat, ignoring section");
2889 return;
2890 }
2891 else if (snat.target != "snat" && snat.snat_ip) {
2892 this.warn_section(data, "must not use 'snat_ip' for non-snat target, ignoring section");
2893 return;
2894 }
2895 else if (snat.target != "snat" && snat.snat_port) {
2896 this.warn_section(data, "must not use 'snat_port' for non-snat target, ignoring section");
2897 return;
2898 }
2899
2900 if ((snat.snat_port || snat.src_port || snat.dest_port) && !ensure_tcpudp(snat.proto)) {
2901 this.warn_section(data, "specifies ports but no UDP/TCP protocol, ignoring section");
2902 return;
2903 }
2904
2905 if (snat.snat_ip && length(filter(snat.snat_ip.addrs, a => a.bits == -1 || a.invert))) {
2906 this.warn_section(data, "must not use inversion or non-contiguous masks in 'snat_ip', ignoring section");
2907 return;
2908 }
2909
2910 if (snat.src && snat.src.zone)
2911 snat.src.zone.dflags.snat = true;
2912
2913 let add_rule = (family, proto, saddrs, daddrs, raddrs, sport, dport, rport, snat) => {
2914 let n = {
2915 ...snat,
2916
2917 family: family,
2918 proto: proto,
2919 has_addrs: !!(saddrs[0] || saddrs[1] || saddrs[2] || daddrs[0] || daddrs[1] || daddrs[2]),
2920 has_ports: !!(sport || dport),
2921 saddrs_pos: map(saddrs[0], this.cidr),
2922 saddrs_neg: map(saddrs[1], this.cidr),
2923 saddrs_masked: saddrs[2],
2924 daddrs_pos: map(daddrs[0], this.cidr),
2925 daddrs_neg: map(daddrs[1], this.cidr),
2926 daddrs_masked: daddrs[2],
2927 sports_pos: map(filter_pos(to_array(sport)), this.port),
2928 sports_neg: map(filter_neg(to_array(sport)), this.port),
2929 dports_pos: map(filter_pos(to_array(dport)), this.port),
2930 dports_neg: map(filter_neg(to_array(dport)), this.port),
2931
2932 raddr: raddrs ? raddrs[0] : null,
2933 rport: rport,
2934
2935 chain: (snat.src && snat.src.zone) ? sprintf("srcnat_%s", snat.src.zone.name) : "srcnat"
2936 };
2937
2938 this.state.redirects = this.state.redirects || [];
2939 push(this.state.redirects, n);
2940 };
2941
2942 for (let proto in snat.proto) {
2943 let sip, dip, rip, sport, dport, rport;
2944 let family = snat.family;
2945
2946 sip = subnets_split_af(snat.src_ip);
2947 dip = subnets_split_af(snat.dest_ip);
2948 rip = subnets_split_af(snat.snat_ip);
2949
2950 switch (proto.name) {
2951 case "tcp":
2952 case "udp":
2953 sport = snat.src_port;
2954 dport = snat.dest_port;
2955 rport = snat.snat_port;
2956 break;
2957 }
2958
2959 if (length(rip[0]) > 1 || length(rip[1]) > 1)
2960 this.warn_section(data, "specifies multiple rewrite addresses, using only first one");
2961
2962 /* inherit family restrictions from related zones */
2963 if (family === 0 || family === null) {
2964 let f = (rule.src && rule.src.zone) ? rule.src.zone.family : 0;
2965
2966 if (f) {
2967 this.warn_section(r,
2968 sprintf("inheriting %s restriction from src %s",
2969 this.nfproto(f1, true), rule.src.zone.name));
2970
2971 family = f;
2972 }
2973 }
2974
2975 /* if no family was configured, infer target family from IP addresses */
2976 if (family === null) {
2977 if ((length(sip[0]) || length(dip[0]) || length(rip[0])) && !length(sip[1]) && !length(dip[1]) && !length(rip[1]))
2978 family = 4;
2979 else if ((length(sip[1]) || length(dip[1]) || length(rip[1])) && !length(sip[0]) && !length(dip[0]) && !length(rip[0]))
2980 family = 6;
2981 else
2982 family = 0;
2983 }
2984
2985 /* check if there's no AF specific bits, in this case we can do an AF agnostic rule */
2986 if (!family && !length(sip[0]) && !length(sip[1]) && !length(dip[0]) && !length(dip[1]) && !length(rip[0]) && !length(rip[1])) {
2987 add_rule(0, proto, [], [], null, sport, dport, rport, snat);
2988 }
2989
2990 /* we need to emit one or two AF specific rules */
2991 else {
2992 if (family == 0 || family == 4)
2993 for (let saddr in subnets_group_by_masking(sip[0]))
2994 for (let daddr in subnets_group_by_masking(dip[0]))
2995 add_rule(4, proto, saddr, daddr, rip[0], sport, dport, rport, snat);
2996
2997 if (family == 0 || family == 6)
2998 for (let saddr in subnets_group_by_masking(sip[1]))
2999 for (let daddr in subnets_group_by_masking(dip[1]))
3000 add_rule(6, proto, saddr, daddr, rip[1], sport, dport, rport, snat);
3001 }
3002 }
3003 },
3004
3005 parse_ipset: function(data) {
3006 let ipset = this.parse_options(data, {
3007 enabled: [ "bool", "1" ],
3008 reload_set: [ "bool" ],
3009 counters: [ "bool" ],
3010 comment: [ "bool" ],
3011
3012 name: [ "string", null, REQUIRED ],
3013 family: [ "family", "4" ],
3014
3015 storage: [ "string", null, UNSUPPORTED ],
3016 match: [ "ipsettype", null, PARSE_LIST ],
3017
3018 iprange: [ "string", null, UNSUPPORTED ],
3019 portrange: [ "string", null, UNSUPPORTED ],
3020
3021 netmask: [ "int", null, UNSUPPORTED ],
3022 maxelem: [ "int" ],
3023 hashsize: [ "int", null, UNSUPPORTED ],
3024 timeout: [ "int", "-1" ],
3025
3026 external: [ "string", null, UNSUPPORTED ],
3027
3028 entry: [ "string", null, PARSE_LIST ],
3029 loadfile: [ "string" ]
3030 });
3031
3032 if (ipset === false) {
3033 this.warn_section(data, "skipped due to invalid options");
3034 return;
3035 }
3036 else if (!ipset.enabled) {
3037 this.warn_section(data, "is disabled, ignoring section");
3038 return;
3039 }
3040
3041 if (ipset.family == 0) {
3042 this.warn_section(data, "must not specify family 'any'");
3043 return;
3044 }
3045 else if (!length(ipset.match)) {
3046 this.warn_section(data, "has no datatypes assigned");
3047 return;
3048 }
3049
3050 let dirs = map(ipset.match, m => m[0]),
3051 types = map(ipset.match, m => m[1]),
3052 interval = false;
3053
3054 if ("set" in types) {
3055 this.warn_section(data, "match type 'set' is not supported");
3056 return;
3057 }
3058
3059 if ("net" in types) {
3060 if (this.kernel < 0x05060000) {
3061 this.warn_section(data, "match type 'net' requires kernel 5.6 or later");
3062 return;
3063 }
3064
3065 interval = true;
3066 }
3067
3068 let s = {
3069 ...ipset,
3070
3071 fw4types: types,
3072
3073 types: map(types, (t) => {
3074 switch (t) {
3075 case 'ip':
3076 case 'net':
3077 return (ipset.family == 4) ? 'ipv4_addr' : 'ipv6_addr';
3078
3079 case 'mac':
3080 return 'ether_addr';
3081
3082 case 'port':
3083 return 'inet_service';
3084 }
3085 }),
3086
3087 directions: dirs,
3088 interval: interval
3089 };
3090
3091 let self = this;
3092 s.entries = filter(map(ipset.entry, (e) => {
3093 let v = self.parse_ipsetentry(e, s);
3094
3095 if (!v)
3096 self.warn_section(data, "ignoring invalid ipset entry '" + e + "'");
3097
3098 return v;
3099 }), (e) => (e != null));
3100
3101 this.state.ipsets = this.state.ipsets || [];
3102 push(this.state.ipsets, s);
3103 }
3104 };