1 const fs = require("fs");
2 const uci = require("uci");
3 const ubus = require("ubus");
5 const STATEFILE = "/var/run/fw4.state";
7 const PARSE_LIST = 0x01;
8 const FLATTEN_LIST = 0x02;
9 const NO_INVERT = 0x04;
10 const UNSUPPORTED = 0x08;
11 const REQUIRED = 0x10;
12 const DEPRECATED = 0x20;
14 const ipv4_icmptypes = {
15 "any": [ 0xFF, 0, 0xFF ],
16 "echo-reply": [ 0, 0, 0xFF ],
17 "pong": [ 0, 0, 0xFF ], /* Alias */
19 "destination-unreachable": [ 3, 0, 0xFF ],
20 "network-unreachable": [ 3, 0, 0 ],
21 "host-unreachable": [ 3, 1, 1 ],
22 "protocol-unreachable": [ 3, 2, 2 ],
23 "port-unreachable": [ 3, 3, 3 ],
24 "fragmentation-needed": [ 3, 4, 4 ],
25 "source-route-failed": [ 3, 5, 5 ],
26 "network-unknown": [ 3, 6, 6 ],
27 "host-unknown": [ 3, 7, 7 ],
28 "network-prohibited": [ 3, 9, 9 ],
29 "host-prohibited": [ 3, 10, 10 ],
30 "TOS-network-unreachable": [ 3, 11, 11 ],
31 "TOS-host-unreachable": [ 3, 12, 12 ],
32 "communication-prohibited": [ 3, 13, 13 ],
33 "host-precedence-violation": [ 3, 14, 14 ],
34 "precedence-cutoff": [ 3, 15, 15 ],
36 "source-quench": [ 4, 0, 0xFF ],
38 "redirect": [ 5, 0, 0xFF ],
39 "network-redirect": [ 5, 0, 0 ],
40 "host-redirect": [ 5, 1, 1 ],
41 "TOS-network-redirect": [ 5, 2, 2 ],
42 "TOS-host-redirect": [ 5, 3, 3 ],
44 "echo-request": [ 8, 0, 0xFF ],
45 "ping": [ 8, 0, 0xFF ], /* Alias */
47 "router-advertisement": [ 9, 0, 0xFF ],
49 "router-solicitation": [ 10, 0, 0xFF ],
51 "time-exceeded": [ 11, 0, 0xFF ],
52 "ttl-exceeded": [ 11, 0, 0xFF ], /* Alias */
53 "ttl-zero-during-transit": [ 11, 0, 0 ],
54 "ttl-zero-during-reassembly": [ 11, 1, 1 ],
56 "parameter-problem": [ 12, 0, 0xFF ],
57 "ip-header-bad": [ 12, 0, 0 ],
58 "required-option-missing": [ 12, 1, 1 ],
60 "timestamp-request": [ 13, 0, 0xFF ],
62 "timestamp-reply": [ 14, 0, 0xFF ],
64 "address-mask-request": [ 17, 0, 0xFF ],
66 "address-mask-reply": [ 18, 0, 0xFF ]
69 const ipv6_icmptypes = {
70 "destination-unreachable": [ 1, 0, 0xFF ],
71 "no-route": [ 1, 0, 0 ],
72 "communication-prohibited": [ 1, 1, 1 ],
73 "address-unreachable": [ 1, 3, 3 ],
74 "port-unreachable": [ 1, 4, 4 ],
76 "packet-too-big": [ 2, 0, 0xFF ],
78 "time-exceeded": [ 3, 0, 0xFF ],
79 "ttl-exceeded": [ 3, 0, 0xFF ], /* Alias */
80 "ttl-zero-during-transit": [ 3, 0, 0 ],
81 "ttl-zero-during-reassembly": [ 3, 1, 1 ],
83 "parameter-problem": [ 4, 0, 0xFF ],
84 "bad-header": [ 4, 0, 0 ],
85 "unknown-header-type": [ 4, 1, 1 ],
86 "unknown-option": [ 4, 2, 2 ],
88 "echo-request": [ 128, 0, 0xFF ],
89 "ping": [ 128, 0, 0xFF ], /* Alias */
91 "echo-reply": [ 129, 0, 0xFF ],
92 "pong": [ 129, 0, 0xFF ], /* Alias */
94 "router-solicitation": [ 133, 0, 0xFF ],
96 "router-advertisement": [ 134, 0, 0xFF ],
98 "neighbour-solicitation": [ 135, 0, 0xFF ],
99 "neighbor-solicitation": [ 135, 0, 0xFF ], /* Alias */
101 "neighbour-advertisement": [ 136, 0, 0xFF ],
102 "neighbor-advertisement": [ 136, 0, 0xFF ], /* Alias */
104 "redirect": [ 137, 0, 0xFF ]
107 const dscp_classes = {
133 function to_mask(bits, v6) {
134 let m = [], n = false;
141 if (bits > (v6 ? 128 : 32))
144 for (let i = 0; i < (v6 ? 16 : 4); i++) {
145 let b = (bits < 8) ? bits : 8;
146 m[i] = (n ? ~(0xff << (8 - b)) : (0xff << (8 - b))) & 0xff;
153 function to_bits(mask) {
154 let a = iptoarr(mask);
161 for (let i = 0, z = false; i < length(a); i++) {
164 while (!z && (a[i] & 0x80)) {
165 a[i] = (a[i] << 1) & 0xff;
176 function apply_mask(addr, mask) {
177 let a = iptoarr(addr);
182 if (type(mask) == "int") {
183 for (let i = 0; i < length(a); i++) {
184 let b = (mask < 8) ? mask : 8;
185 a[i] &= (0xff << (8 - b)) & 0xff;
190 let m = iptoarr(mask);
192 if (!m || length(a) != length(m))
195 for (let i = 0; i < length(a); i++)
202 function to_array(x) {
203 if (type(x) == "array")
209 if (type(x) == "object")
214 return (x == "") ? [] : split(x, /[ \t]+/);
217 function filter_pos(x) {
218 let rv = filter(x, e => !e.invert);
219 return length(rv) ? rv : null;
222 function filter_neg(x) {
223 let rv = filter(x, e => e.invert);
224 return length(rv) ? rv : null;
227 function null_if_empty(x) {
228 return length(x) ? x : null;
231 function subnets_split_af(x) {
234 for (let ag in to_array(x)) {
235 for (let a in filter(ag.addrs, a => (a.family == 4)))
236 push(rv[0] ||= [], { ...a, invert: ag.invert });
238 for (let a in filter(ag.addrs, a => (a.family == 6)))
239 push(rv[1] ||= [], { ...a, invert: ag.invert });
243 rv.family = (!rv[0] ^ !rv[1]) ? (rv[0] ? 4 : 6) : 0;
248 function subnets_group_by_masking(x) {
249 let groups = [], plain = [], nc = [], invert_plain = [], invert_masked = [];
251 for (let a in to_array(x)) {
252 if (a.bits == -1 && !a.invert)
256 else if (a.bits == -1)
257 push(invert_masked, a);
259 push(invert_plain, a);
263 push(groups, [ null, null_if_empty(invert_plain), [ a, ...invert_masked ] ]);
268 null_if_empty(invert_plain),
269 null_if_empty(invert_masked)
272 else if (!length(groups)) {
275 null_if_empty(invert_plain),
276 null_if_empty(invert_masked)
283 function ensure_tcpudp(x) {
284 if (length(filter(x, p => (p.name == "tcp" || p.name == "udp"))))
287 let rest = filter(x, p => !p.any),
288 any = filter(x, p => p.any);
290 if (length(any) && !length(rest)) {
292 push(x, { name: "tcp" }, { name: "udp" });
299 let is_family = (x, v) => (!x.family || x.family == v);
300 let family_is_ipv4 = (x) => (!x.family || x.family == 4);
301 let family_is_ipv6 = (x) => (!x.family || x.family == 6);
303 function infer_family(f, objects) {
307 for (let i = 0; i < length(objects); i += 2) {
308 let objs = to_array(objects[i]),
309 desc = objects[i + 1];
311 for (let obj in objs) {
312 if (!obj || !obj.family || obj.family == res)
322 ? `references IPv${obj.family} only ${desc} but is restricted to IPv${res} by ${by}`
323 : `is restricted to IPv${res} but referenced ${desc} is IPv${obj.family} only`;
330 function map_setmatch(set, match, proto) {
331 if (!set || (('inet_service' in set.types) && proto != 'tcp' && proto != 'udp'))
336 for (let i, t in set.types) {
337 let dir = ((match.dir?.[i] || set.directions[i] || 'src') == 'src' ? 's' : 'd');
341 fields[i] = `ip ${dir}addr`;
345 fields[i] = `ip6 ${dir}addr`;
352 fields[i] = 'ether saddr';
356 fields[i] = `${proto} ${dir}port`;
364 function resolve_lower_devices(devstatus, devname, require_hwoffload) {
365 let dir = fs.opendir(`/sys/class/net/${devname}`);
369 switch (devstatus[devname]?.devtype) {
374 while ((e = dir.read()) != null)
375 if (index(e, "lower_") === 0)
376 push(devs, ...resolve_lower_devices(devstatus, substr(e, 6), require_hwoffload));
381 if (!require_hwoffload || devstatus[devname]?.["hw-tc-offload"])
393 function nft_json_command(...args) {
394 let cmd = [ "/usr/sbin/nft", "--terse", "--json", ...args ];
395 let nft = fs.popen(join(" ", cmd), "r");
400 info = filter(json(nft.read("all"))?.nftables,
401 item => (type(item) == "object" && !item.metainfo));
404 warn(`Unable to parse nftables JSON output: ${e}\n`);
410 warn(`Unable to popen() ${cmd}: ${fs.error()}\n`);
416 function nft_try_hw_offload(devices) {
418 add table inet fw4-hw-offload-test;
419 add flowtable inet fw4-hw-offload-test ft {
420 hook ingress priority 0;
421 devices = { "${join('", "', devices)}" };
426 let rc = system(`/usr/sbin/nft -c '${replace(nft_test, "'", "'\\''")}' 2>/dev/null`);
433 read_kernel_version: function() {
434 let fd = fs.open("/proc/version", "r"),
438 let m = match(fd.read("line"), /^Linux version ([0-9]+)\.([0-9]+)\.([0-9]+)/);
440 v = m ? (+m[1] << 24) | (+m[2] << 16) | (+m[3] << 8) : 0;
447 resolve_offload_devices: function() {
448 if (!this.default_option("flow_offloading"))
451 let devstatus = null;
453 let bus = ubus.connect();
456 devstatus = bus.call("network.device", "status") || {};
460 if (this.default_option("flow_offloading_hw")) {
461 for (let zone in this.zones())
462 for (let device in zone.related_physdevs)
463 push(devices, ...resolve_lower_devices(devstatus, device, true));
465 devices = sort(uniq(devices));
467 if (length(devices) && nft_try_hw_offload(devices))
470 this.warn('Hardware flow offloading unavailable, falling back to software offloading');
471 this.state.defaults.flow_offloading_hw = false;
476 for (let zone in this.zones())
477 for (let device in zone.related_physdevs)
478 push(devices, ...resolve_lower_devices(devstatus, device, false));
480 return sort(uniq(devices));
483 check_set_types: function() {
486 for (let item in nft_json_command("list", "sets", "inet"))
487 if (item.set?.table == "fw4")
488 sets[item.set.name] = (type(item.set.type) == "array") ? item.set.type : [ item.set.type ];
493 check_flowtable: function() {
494 for (let item in nft_json_command("list", "flowtables", "inet"))
495 if (item.flowtable?.table == "fw4" && item.flowtable?.name == "ft")
501 read_state: function() {
502 let fd = fs.open(STATEFILE, "r");
507 state = json(fd.read("all"));
510 warn(`Unable to parse '${STATEFILE}': ${e}\n`);
519 read_ubus: function() {
522 rules = [], networks = {},
523 bus = ubus.connect();
526 ifaces = bus.call("network.interface", "dump");
527 services = bus.call("service", "get_data", { "type": "firewall" });
532 warn(`Unable to connect to ubus: ${ubus.error()}\n`);
537 // Gather logical network information from ubus
540 if (type(ifaces?.interface) == "array") {
541 for (let ifc in ifaces.interface) {
544 device: ifc.l3_device,
549 if (type(ifc["ipv4-address"]) == "array") {
550 for (let addr in ifc["ipv4-address"]) {
551 push(net.ipaddrs ||= [], {
554 mask: to_mask(addr.mask, false),
560 if (type(ifc["ipv6-address"]) == "array") {
561 for (let addr in ifc["ipv6-address"]) {
562 push(net.ipaddrs ||= [], {
565 mask: to_mask(addr.mask, true),
571 if (type(ifc["ipv6-prefix-assignment"]) == "array") {
572 for (let addr in ifc["ipv6-prefix-assignment"]) {
573 if (addr["local-address"]) {
574 push(net.ipaddrs ||= [], {
576 addr: addr["local-address"].address,
577 mask: to_mask(addr["local-address"].mask, true),
578 bits: addr["local-address"].mask
584 if (type(ifc.data?.firewall) == "array") {
587 for (let rulespec in ifc.data.firewall) {
591 name: (rulespec.type != 'ipset') ? `ubus:${ifc.interface}[${ifc.proto}] ${rulespec.type || 'rule'} ${n}` : rulespec.name,
592 device: rulespec.device || ifc.l3_device
599 networks[ifc.interface] = net;
605 // Gather firewall rule definitions from ubus services
608 if (type(services) == "object") {
609 for (let svcname, service in services) {
610 if (type(service?.firewall) == "array") {
613 for (let rulespec in services[svcname].firewall) {
617 name: (rulespec.type != 'ipset') ? `ubus:${svcname} ${rulespec.type || 'rule'} ${n}` : rulespec.name
624 for (let svcinst, instance in service) {
625 if (type(instance?.firewall) == "array") {
628 for (let rulespec in instance.firewall) {
632 name: (rulespec.type != 'ipset') ? `ubus:${svcname}[${svcinst}] ${rulespec.type || 'rule'} ${n}` : rulespec.name
648 load: function(use_statefile) {
651 this.state = use_statefile ? this.read_state() : null;
653 this.cursor = uci.cursor();
654 this.cursor.load("firewall");
655 this.cursor.load("/usr/share/firewall4/helpers");
658 this.state = this.read_ubus();
660 this.kernel = this.read_kernel_version();
664 // Read helper mapping
667 this.cursor.foreach("helpers", "helper", h => self.parse_helper(h));
671 // Read default policies
674 this.cursor.foreach("firewall", "defaults", d => self.parse_defaults(d));
676 if (!this.state.defaults)
677 this.parse_defaults({});
681 // Build list of ipsets
684 if (!this.state.ipsets) {
685 map(filter(this.state.ubus_rules, n => (n.type == "ipset")), s => self.parse_ipset(s));
686 this.cursor.foreach("firewall", "ipset", s => self.parse_ipset(s));
691 // Build list of logical zones
694 if (!this.state.zones)
695 this.cursor.foreach("firewall", "zone", z => self.parse_zone(z));
699 // Build list of rules
702 map(filter(this.state.ubus_rules, r => (r.type == "rule")), r => self.parse_rule(r));
703 this.cursor.foreach("firewall", "rule", r => self.parse_rule(r));
707 // Build list of forwardings
710 this.cursor.foreach("firewall", "forwarding", f => self.parse_forwarding(f));
714 // Build list of redirects
717 map(filter(this.state.ubus_rules, r => (r.type == "redirect")), r => self.parse_redirect(r));
718 this.cursor.foreach("firewall", "redirect", r => self.parse_redirect(r));
722 // Build list of snats
725 map(filter(this.state.ubus_rules, n => (n.type == "nat")), n => self.parse_nat(n));
726 this.cursor.foreach("firewall", "nat", n => self.parse_nat(n));
730 let fd = fs.open(STATEFILE, "w");
734 zones: this.state.zones,
735 ipsets: this.state.ipsets,
736 networks: this.state.networks,
737 ubus_rules: this.state.ubus_rules
743 warn(`Unable to write '${STATEFILE}': ${fs.error()}\n`);
748 warn: function(fmt, ...args) {
752 let msg = sprintf(fmt, ...args);
755 warn(`\033[33m${msg}\033[m\n`);
757 warn(`[!] ${msg}\n`);
760 get: function(sid, opt) {
761 return this.cursor.get("firewall", sid, opt);
764 get_all: function(sid) {
765 return this.cursor.get_all("firewall", sid);
768 parse_options: function(s, spec) {
771 for (let key, val in spec) {
772 let datatype = `parse_${val[0]}`,
775 parsefn = (flags & PARSE_LIST) ? "parse_list" : "parse_opt";
777 let res = this[parsefn](s, key, datatype, defval, flags);
782 if (type(res) == "object" && res.invert && (flags & NO_INVERT)) {
783 this.warn_section(s, `option '${key}' must not be negated`);
788 if (flags & DEPRECATED)
789 this.warn_section(s, `option '${key}' is deprecated by fw4`);
790 else if (flags & UNSUPPORTED)
791 this.warn_section(s, `option '${key}' is not supported by fw4`);
798 if (index(opt, '.') != 0 && opt != 'type' && !exists(spec, opt)) {
799 this.warn_section(s, `specifies unknown option '${opt}'`);
806 parse_subnet: function(subnet) {
807 let parts = split(subnet, "/");
810 switch (length(parts)) {
812 a = iptoarr(parts[0]);
813 m = iptoarr(parts[1]);
819 if (length(a) != length(m))
822 b = to_bits(parts[1]);
824 /* allow non-contiguous masks such as `::ffff:ffff:ffff:ffff` */
837 if (type(b) != "int")
840 m = to_mask(b, length(a) == 16);
845 family: (length(a) == 16) ? 6 : 4,
852 parts = split(parts[0], "-");
854 switch (length(parts)) {
856 a = iptoarr(parts[0]);
857 b = iptoarr(parts[1]);
859 if (a && b && length(a) == length(b)) {
861 family: (length(a) == 16) ? 6 : 4,
871 a = iptoarr(parts[0]);
875 family: (length(a) == 16) ? 6 : 4,
877 mask: to_mask(length(a) * 8, length(a) == 16),
882 n = this.state.networks[parts[0]];
885 return [ ...(n.ipaddrs || []) ];
892 parse_enum: function(val, choices) {
893 if (type(val) == "string") {
896 for (let i = 0; i < length(choices); i++)
897 if (lc(substr(choices[i], 0, length(val))) == val)
904 section_id: function(sid) {
905 let s = this.get_all(sid);
910 if (s[".anonymous"]) {
913 this.cursor.foreach("firewall", s[".type"], function(ss) {
914 if (ss[".name"] == s[".name"])
920 return `@${s['.type']}[${c}]`;
926 warn_section: function(s, msg) {
929 this.warn("Section %s (%s) %s", this.section_id(s[".name"]), s.name, msg);
931 this.warn("Section %s %s", this.section_id(s[".name"]), msg);
935 this.warn("ubus %s (%s) %s", s.type || "rule", s.name, msg);
937 this.warn("ubus %s %s", s.type || "rule", msg);
941 parse_policy: function(val) {
942 return this.parse_enum(val, [
949 parse_bool: function(val) {
950 if (val == "1" || val == "on" || val == "true" || val == "yes")
952 else if (val == "0" || val == "off" || val == "false" || val == "no")
958 parse_family: function(val) {
959 if (val == 'any' || val == 'all' || val == '*')
961 else if (val == 'inet' || index(val, '4') > -1)
963 else if (index(val, '6') > -1)
969 parse_zone_ref: function(val) {
974 return { any: true };
976 for (let zone in this.state.zones) {
977 if (zone.name == val) {
988 parse_device: function(val) {
989 let rv = this.parse_invert(val);
1002 parse_direction: function(val) {
1003 if (val == 'in' || val == 'ingress')
1005 else if (val == 'out' || val == 'egress')
1011 parse_setmatch: function(val) {
1012 let rv = this.parse_invert(val);
1017 rv.val = trim(replace(rv.val, /^[^ \t]+/, function(m) {
1022 let dir = split(rv.val, /[ \t,]/);
1024 for (let i = 0; i < 3 && i < length(dir); i++) {
1025 if (dir[i] == "dst" || dir[i] == "dest")
1026 (rv.dir ||= [])[i] = "dst";
1027 else if (dir[i] == "src")
1028 (rv.dir ||= [])[i] = "src";
1031 return length(rv.name) ? rv : null;
1034 parse_cthelper: function(val) {
1035 let rv = this.parse_invert(val);
1040 let helper = filter(this.state.helpers, h => (h.name == rv.val))?.[0];
1042 return helper ? { ...rv, ...helper } : null;
1045 parse_protocol: function(val) {
1046 let p = this.parse_invert(val);
1068 p.name = 'ipv6-icmp';
1073 { invert: p.invert, name: 'tcp' },
1074 { invert: p.invert, name: 'udp' }
1089 return (p.any || length(p.name)) ? p : null;
1092 parse_mac: function(val) {
1093 let mac = this.parse_invert(val);
1094 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;
1099 mac.mac = sprintf('%02x:%02x:%02x:%02x:%02x:%02x',
1100 hex(m[1]), hex(m[2]), hex(m[3]),
1101 hex(m[4]), hex(m[5]), hex(m[6]));
1106 parse_port: function(val) {
1107 let port = this.parse_invert(val);
1108 let m = port ? match(port.val, /^([0-9]{1,5})([-:]([0-9]{1,5}))?$/i) : null;
1114 let min_port = +m[1];
1115 let max_port = +m[3];
1117 if (min_port > max_port ||
1118 min_port < 0 || max_port < 0 ||
1119 min_port > 65535 || max_port > 65535)
1122 port.min = min_port;
1123 port.max = max_port;
1128 if (pn != pn || pn < 0 || pn > 65535)
1138 parse_network: function(val) {
1139 let rv = this.parse_invert(val);
1144 let nets = this.parse_subnet(rv.val);
1150 rv.addrs = [ ...nets ];
1155 parse_icmptype: function(val) {
1158 if (exists(ipv4_icmptypes, val)) {
1161 rv.type = ipv4_icmptypes[val][0];
1162 rv.code_min = ipv4_icmptypes[val][1];
1163 rv.code_max = ipv4_icmptypes[val][2];
1166 if (exists(ipv6_icmptypes, val)) {
1167 rv.family = rv.family ? 0 : 6;
1169 rv.type6 = ipv6_icmptypes[val][0];
1170 rv.code6_min = ipv6_icmptypes[val][1];
1171 rv.code6_max = ipv6_icmptypes[val][2];
1174 if (!exists(rv, "family")) {
1175 let m = match(val, /^([0-9]+)(\/([0-9]+))?$/);
1182 rv.code_min = +m[3];
1183 rv.code_max = rv.code_min;
1191 if (rv.type > 0xFF || rv.code_min > 0xFF || rv.code_max > 0xFF)
1197 rv.code6_min = rv.code_min;
1198 rv.code6_max = rv.code_max;
1204 parse_invert: function(val) {
1208 let rv = { invert: false };
1210 rv.val = trim(replace(val, /^[ \t]*!/, () => (rv.invert = true, '')));
1212 return length(rv.val) ? rv : null;
1215 parse_limit: function(val) {
1216 let rv = this.parse_invert(val);
1217 let m = rv ? match(rv.val, /^([0-9]+)(\/([a-z]+))?$/) : null;
1223 let u = m[3] ? this.parse_enum(m[3], [ "second", "minute", "hour", "day" ]) : "second";
1234 parse_int: function(val) {
1237 return (n == n) ? n : null;
1240 parse_date: function(val) {
1241 let d = match(val, /^([0-9]{4})(-([0-9]{1,2})(-([0-9]{1,2})(T([0-9:]+))?)?)?$/);
1243 if (d == null || d[1] < 1970 || d[1] > 2038 || d[3] > 12 || d[5] > 31)
1246 let t = this.parse_time(d[7] ?? "0");
1259 parse_time: function(val) {
1260 let t = match(val, /^([0-9]{1,2})(:([0-9]{1,2})(:([0-9]{1,2}))?)?$/);
1262 if (t == null || t[1] > 23 || t[3] > 59 || t[5] > 59)
1272 parse_weekdays: function(val) {
1273 let rv = this.parse_invert(val);
1278 for (let day in to_array(rv.val)) {
1279 day = this.parse_enum(day, [
1292 (rv.days ||= {})[day] = true;
1295 rv.days = keys(rv.days);
1297 return rv.days ? rv : null;
1300 parse_monthdays: function(val) {
1301 let rv = this.parse_invert(val);
1306 for (let day in to_array(rv.val)) {
1309 if (day < 1 || day > 31)
1312 (rv.days ||= [])[day] = true;
1315 return rv.days ? rv : null;
1318 parse_mark: function(val) {
1319 let rv = this.parse_invert(val);
1320 let m = rv ? match(rv.val, /^(0?x?[0-9a-f]+)(\/(0?x?[0-9a-f]+))?$/i) : null;
1327 if (n != n || n > 0xFFFFFFFF)
1331 rv.mask = 0xFFFFFFFF;
1336 if (n != n || n > 0xFFFFFFFF)
1345 parse_dscp: function(val) {
1346 let rv = this.parse_invert(val);
1351 rv.val = uc(rv.val);
1353 if (exists(dscp_classes, rv.val)) {
1354 rv.dscp = dscp_classes[rv.val];
1359 if (n != n || n < 0 || n > 0x3F)
1368 parse_target: function(val) {
1369 return this.parse_enum(val, [
1386 parse_reject_code: function(val) {
1387 return this.parse_enum(val, [
1396 parse_reflection_source: function(val) {
1397 return this.parse_enum(val, [
1403 parse_ipsettype: function(val) {
1404 let m = match(val, /^(src|dst|dest)_(.+)$/);
1405 let t = this.parse_enum(m ? m[2] : val, [
1413 return t ? [ (!m || m[1] == 'src') ? 'src' : 'dst', t ] : null;
1416 parse_ipsetentry: function(val, set) {
1417 let values = split(val, /[ \t]+/);
1419 if (length(values) != length(set.types))
1425 for (let i, t in set.types) {
1428 ip = filter(this.parse_subnet(values[i]), a => (a.family == 4));
1430 switch (length(ip) ?? 0) {
1431 case 0: return null;
1433 default: this.warn("Set entry '%s' resolves to multiple addresses, using first one", values[i]);
1436 rv[i] = ("net" in set.fw4types) ? `${ip[0].addr}/${ip[0].bits}` : ip[0].addr;
1440 ip = filter(this.parse_subnet(values[i]), a => (a.family == 6));
1442 switch (length(ip) ?? 0) {
1443 case 0: return null;
1445 case 2: this.warn("Set entry '%s' resolves to multiple addresses, using first one", values[i]);
1448 rv[i] = ("net" in set.fw4types) ? `${ip[0].addr}/${ip[0].bits}` : ip[0].addr;
1453 mac = this.parse_mac(values[i]);
1455 if (!mac || mac.invert)
1461 case 'inet_service':
1462 port = this.parse_port(values[i]);
1464 if (!port || port.invert || port.min != port.max)
1475 return length(rv) ? rv : null;
1478 parse_string: function(val) {
1482 parse_opt: function(s, opt, fn, defval, flags) {
1486 if (flags & REQUIRED) {
1487 this.warn_section(s, `option '${opt}' is mandatory but not set`);
1494 if (type(val) == "array") {
1495 this.warn_section(s, `option '${opt}' must not be a list`);
1498 else if (val == null) {
1502 let res = this[fn](val);
1505 this.warn_section(s, `option '${opt}' specifies invalid value '${val}'`);
1512 parse_list: function(s, opt, fn, defval, flags) {
1517 if (flags & REQUIRED) {
1518 this.warn_section(s, `option '${opt}' is mandatory but not set`);
1525 for (val in to_array(val)) {
1526 let res = this[fn](val);
1529 this.warn_section(s, `option '${opt}' specifies invalid value '${val}'`);
1533 if (flags & FLATTEN_LIST)
1534 push(rv, ...to_array(res));
1539 return length(rv) ? rv : null;
1542 quote: function(s, force) {
1543 if (force === true || !match(s, /^([0-9A-Fa-f:.\/-]+)( \. [0-9A-Fa-f:.\/-]+)*$/))
1544 return `"${replace(s + "", /(["\\])/g, '\\$1')}"`;
1551 return `${a.addr}-${a.addr2}`;
1553 if ((a.family == 4 && a.bits == 32) ||
1554 (a.family == 6 && a.bits == 128))
1558 return `${apply_mask(a.addr, a.bits)}/${a.bits}`;
1560 return `${a.addr}/${a.mask}`;
1563 host: function(a, v6brackets) {
1565 ? `${a.addr}-${a.addr2}`
1566 : (a.family == 6 && v6brackets)
1567 ? `[${apply_mask(a.addr, a.bits)}]` : apply_mask(a.addr, a.bits);
1574 return `${p.min}-${p.max}`;
1577 set: function(v, force) {
1580 v = filter(to_array(v), item => !seen[item]++);
1582 if (force || length(v) != 1)
1583 return `{ ${join(', ', map(v, this.quote))} }`;
1585 return this.quote(v[0]);
1588 concat: function(v) {
1589 return join(' . ', to_array(v));
1592 ipproto: function(family) {
1602 nfproto: function(family, human_readable) {
1605 return human_readable ? "IPv4" : "ipv4";
1608 return human_readable ? "IPv6" : "ipv6";
1611 return human_readable ? "IPv4/IPv6" : null;
1615 l4proto: function(family, proto) {
1616 switch (proto.name) {
1618 switch (family ?? 0) {
1620 return this.set(['icmp', 'ipv6-icmp']);
1631 datetime: function(stamp) {
1632 return sprintf('"%04d-%02d-%02d %02d:%02d:%02d"',
1633 stamp.year, stamp.month, stamp.day,
1634 stamp.hour, stamp.min, stamp.sec);
1637 date: function(stamp) {
1638 return sprintf('"%04d-%02d-%02d"', stamp.year, stamp.month, stamp.day);
1641 datestamp: function(stamp) {
1642 return exists(stamp, 'hour') ? this.datetime(stamp) : this.date(stamp);
1645 time: function(stamp) {
1646 return sprintf('"%02d:%02d:%02d"', stamp.hour, stamp.min, stamp.sec);
1650 return sprintf('0x%x', n);
1653 is_loopback_dev: function(dev) {
1654 return !!(+fs.readfile(`/sys/class/net/${dev}/flags`) & 0x8);
1657 is_loopback_addr: function(addr) {
1658 return (index(addr, "127.") == 0 || addr == "::1" || addr == "::1/128");
1661 filter_loopback_devs: function(devs, invert) {
1662 return null_if_empty(filter(devs, d => (this.is_loopback_dev(d) == invert)));
1665 filter_loopback_addrs: function(addrs, invert) {
1666 return null_if_empty(filter(addrs, a => (this.is_loopback_addr(a) == invert)));
1670 input_policy: function(reject_as_drop) {
1671 return (!reject_as_drop || this.state.defaults.input != 'reject') ? this.state.defaults.input : 'drop';
1674 output_policy: function(reject_as_drop) {
1675 return (!reject_as_drop || this.state.defaults.output != 'reject') ? this.state.defaults.output : 'drop';
1678 forward_policy: function(reject_as_drop) {
1679 return (!reject_as_drop || this.state.defaults.forward != 'reject') ? this.state.defaults.forward : 'drop';
1682 default_option: function(flag) {
1683 return this.state.defaults[flag];
1686 helpers: function() {
1687 return this.state.helpers;
1691 return this.state.zones;
1694 rules: function(chain) {
1695 return filter(this.state.rules, r => (r.chain == chain));
1698 redirects: function(chain) {
1699 return filter(this.state.redirects, r => (r.chain == chain));
1702 ipsets: function() {
1703 return this.state.ipsets;
1706 parse_setfile: function(set, cb) {
1707 let fd = fs.open(set.loadfile, "r");
1710 warn(`Unable to load file '${set.loadfile}' for set '${set.name}': ${fs.error()}\n`);
1714 let line = null, count = 0;
1716 while ((line = fd.read("line")) !== "") {
1719 if (length(line) == 0 || ord(line) == 35)
1722 let v = this.parse_ipsetentry(line, set);
1725 this.warn(`Skipping invalid entry '${line}' in file '${set.loadfile}' for set '${set.name}'`);
1739 print_setentries: function(set) {
1741 let printer = (entry) => {
1743 print("\t\telements = {\n");
1747 print("\t\t\t", join(" . ", entry), ",\n");
1750 map(set.entries, printer);
1753 this.parse_setfile(set, printer);
1759 parse_helper: function(data) {
1760 let helper = this.parse_options(data, {
1761 name: [ "string", null, REQUIRED ],
1762 description: [ "string" ],
1763 module: [ "string" ],
1764 family: [ "family" ],
1765 proto: [ "protocol", null, PARSE_LIST | FLATTEN_LIST | NO_INVERT ],
1766 port: [ "port", null, NO_INVERT ]
1769 if (helper === false) {
1770 this.warn("Helper definition '%s' skipped due to invalid options", data.name || data['.name']);
1773 else if (helper.proto.any) {
1774 this.warn("Helper definition '%s' must not specify wildcard protocol", data.name || data['.name']);
1777 else if (length(helper.proto) > 1) {
1778 this.warn("Helper definition '%s' must not specify multiple protocols", data.name || data['.name']);
1782 helper.available = (fs.stat(`/sys/module/${helper.module}`)?.type == "directory");
1784 push(this.state.helpers ||= [], helper);
1787 parse_defaults: function(data) {
1788 if (this.state.defaults) {
1789 this.warn_section(data, ": ignoring duplicate defaults section");
1793 let defs = this.parse_options(data, {
1794 input: [ "policy", "drop" ],
1795 output: [ "policy", "drop" ],
1796 forward: [ "policy", "drop" ],
1798 drop_invalid: [ "bool" ],
1799 tcp_reject_code: [ "reject_code", "tcp-reset" ],
1800 any_reject_code: [ "reject_code", "port-unreachable" ],
1802 syn_flood: [ "bool" ],
1803 synflood_protect: [ "bool" ],
1804 synflood_rate: [ "limit", "25/second" ],
1805 synflood_burst: [ "int", "50" ],
1807 tcp_syncookies: [ "bool", "1" ],
1809 tcp_window_scaling: [ "bool", "1" ],
1811 accept_redirects: [ "bool" ],
1812 accept_source_route: [ "bool" ],
1814 auto_helper: [ "bool", "1" ],
1815 custom_chains: [ "bool", null, UNSUPPORTED ],
1816 disable_ipv6: [ "bool", null, UNSUPPORTED ],
1817 flow_offloading: [ "bool", "0" ],
1818 flow_offloading_hw: [ "bool", "0" ]
1821 if (defs.synflood_protect === null)
1822 defs.synflood_protect = defs.syn_flood;
1824 delete defs.syn_flood;
1826 this.state.defaults = defs;
1829 parse_zone: function(data) {
1830 let zone = this.parse_options(data, {
1831 enabled: [ "bool", "1" ],
1833 name: [ "string", null, REQUIRED ],
1834 family: [ "family" ],
1836 network: [ "device", null, PARSE_LIST ],
1837 device: [ "device", null, PARSE_LIST ],
1838 subnet: [ "network", null, PARSE_LIST ],
1840 input: [ "policy", this.state.defaults ? this.state.defaults.input : "drop" ],
1841 output: [ "policy", this.state.defaults ? this.state.defaults.output : "drop" ],
1842 forward: [ "policy", this.state.defaults ? this.state.defaults.forward : "drop" ],
1845 masq_allow_invalid: [ "bool" ],
1846 masq_src: [ "network", null, PARSE_LIST ],
1847 masq_dest: [ "network", null, PARSE_LIST ],
1851 extra: [ "string", null, UNSUPPORTED ],
1852 extra_src: [ "string", null, UNSUPPORTED ],
1853 extra_dest: [ "string", null, UNSUPPORTED ],
1855 mtu_fix: [ "bool" ],
1856 custom_chains: [ "bool", null, UNSUPPORTED ],
1859 log_limit: [ "limit", null, UNSUPPORTED ],
1861 auto_helper: [ "bool", "1" ],
1862 helper: [ "cthelper", null, PARSE_LIST ],
1864 counter: [ "bool", "1" ]
1867 if (zone === false) {
1868 this.warn_section(data, "skipped due to invalid options");
1871 else if (!zone.enabled) {
1872 this.warn_section(data, "is disabled, ignoring section");
1876 for (let helper in zone.helper) {
1877 if (!helper.available) {
1878 this.warn_section(data, `uses unavailable ct helper '${zone.helper.name}'`);
1882 if (zone.mtu_fix && this.kernel < 0x040a0000) {
1883 this.warn_section(data, "option 'mtu_fix' requires kernel 4.10 or later");
1887 if (this.state.defaults?.auto_helper === false)
1888 zone.auto_helper = false;
1890 let match_devices = [];
1891 let related_physdevs = [];
1892 let related_subnets = [];
1893 let related_ubus_networks = [];
1894 let match_subnets, masq_src_subnets, masq_dest_subnets;
1896 for (let name, net in this.state.networks) {
1897 if (net.zone === zone.name)
1898 push(related_ubus_networks, { invert: false, device: name });
1901 zone.network = [ ...to_array(zone.network), ...related_ubus_networks ];
1903 for (let e in zone.network) {
1904 if (exists(this.state.networks, e.device)) {
1905 let net = this.state.networks[e.device];
1908 push(match_devices, {
1914 if (net.physdev && !e.invert)
1915 push(related_physdevs, net.physdev);
1917 push(related_subnets, ...(net.ipaddrs || []));
1921 push(match_devices, ...to_array(zone.device));
1923 match_subnets = subnets_split_af(zone.subnet);
1924 masq_src_subnets = subnets_split_af(zone.masq_src);
1925 masq_dest_subnets = subnets_split_af(zone.masq_dest);
1927 push(related_subnets, ...(match_subnets[0] || []), ...(match_subnets[1] || []));
1929 let match_rules = [];
1931 let add_rule = (family, devices, subnets, zone) => {
1936 r.devices_pos = null_if_empty(devices[0]);
1937 r.devices_neg = null_if_empty(devices[1]);
1938 r.devices_neg_wildcard = null_if_empty(devices[2]);
1940 r.subnets_pos = map(subnets[0], this.cidr);
1941 r.subnets_neg = map(subnets[1], this.cidr);
1942 r.subnets_masked = subnets[2];
1944 push(match_rules, r);
1947 let family = infer_family(zone.family, [
1948 zone.helper, "ct helper",
1949 match_subnets, "subnet list"
1952 if (type(family) == "string") {
1953 this.warn_section(data, `${family}, skipping`);
1957 // group non-inverted device matches into wildcard and non-wildcard ones
1958 let devices = [], plain_devices = [], plain_invert_devices = [], wildcard_invert_devices = [];
1960 for (let device in match_devices) {
1961 let m = match(device.device, /^([^+]*)(\+)?$/);
1964 this.warn_section(data, `skipping invalid wildcard pattern '${device.device}'`);
1968 // filter `+` (match any device) since nftables does not support
1969 // wildcard only matches
1970 if (!device.invert && m[0] == '+')
1973 // replace inverted `+` (match no device) with invalid pattern
1974 if (device.invert && m[0] == '+') {
1975 device.device = '/never/';
1976 device.invert = false;
1979 // replace "name+" matches with "name*"
1980 else if (m[2] == '+')
1981 device.device = m[1] + '*';
1983 device.wildcard = !!m[2];
1985 if (!device.invert && device.wildcard)
1986 push(devices, [ [ device.device ], plain_invert_devices, wildcard_invert_devices ]);
1987 else if (!device.invert)
1988 push(plain_devices, device.device);
1989 else if (device.wildcard)
1990 push(wildcard_invert_devices, device.device);
1992 push(plain_invert_devices, device.device);
1995 if (length(plain_devices))
1998 plain_invert_devices,
1999 wildcard_invert_devices
2001 else if (!length(devices))
2004 plain_invert_devices,
2005 wildcard_invert_devices
2008 // emit zone jump rules for each device group
2009 if (length(match_devices) || length(match_subnets[0]) || length(match_subnets[1])) {
2010 for (let devgroup in devices) {
2011 // check if there's no AF specific bits, in this case we can do AF agnostic matching
2012 if (!family && !length(match_subnets[0]) && !length(match_subnets[1])) {
2013 add_rule(0, devgroup, [], zone);
2016 // we need to emit one or two AF specific rules
2018 if (!family || family == 4)
2019 for (let subnets in subnets_group_by_masking(match_subnets[0]))
2020 add_rule(4, devgroup, subnets, zone);
2022 if (!family || family == 6)
2023 for (let subnets in subnets_group_by_masking(match_subnets[1]))
2024 add_rule(6, devgroup, subnets, zone);
2029 zone.family = family;
2031 zone.match_rules = match_rules;
2033 zone.masq4_src_subnets = subnets_group_by_masking(masq_src_subnets[0]);
2034 zone.masq4_dest_subnets = subnets_group_by_masking(masq_dest_subnets[0]);
2036 zone.masq6_src_subnets = subnets_group_by_masking(masq_src_subnets[1]);
2037 zone.masq6_dest_subnets = subnets_group_by_masking(masq_dest_subnets[1]);
2040 zone.sflags[zone.input] = true;
2043 zone.dflags[zone.output] = true;
2044 zone.dflags[zone.forward] = true;
2046 zone.match_devices = map(filter(match_devices, d => !d.invert), d => d.device);
2047 zone.match_subnets = map(filter(related_subnets, s => !s.invert && s.bits != -1), this.cidr);
2049 zone.related_subnets = related_subnets;
2050 zone.related_physdevs = related_physdevs;
2052 if (zone.masq || zone.masq6)
2053 zone.dflags.snat = true;
2055 if ((zone.auto_helper && !(zone.masq || zone.masq6)) || length(zone.helper)) {
2056 zone.dflags.helper = true;
2058 for (let helper in (length(zone.helper) ? zone.helper : this.state.helpers)) {
2059 if (!helper.available)
2062 for (let proto in helper.proto) {
2063 push(this.state.rules ||= [], {
2064 chain: `helper_${zone.name}`,
2065 family: helper.family,
2066 name: helper.description || helper.name,
2069 dports_pos: [ this.port(helper.port) ],
2077 push(this.state.zones ||= [], zone);
2080 parse_forwarding: function(data) {
2081 let fwd = this.parse_options(data, {
2082 enabled: [ "bool", "1" ],
2085 family: [ "family" ],
2087 src: [ "zone_ref", null, REQUIRED ],
2088 dest: [ "zone_ref", null, REQUIRED ]
2091 if (fwd === false) {
2092 this.warn_section(data, "skipped due to invalid options");
2095 else if (!fwd.enabled) {
2096 this.warn_section(data, "is disabled, ignoring section");
2100 let add_rule = (family, fwd) => {
2105 proto: { any: true }
2108 f.name ||= `Accept ${fwd.src.any ? "any" : fwd.src.zone.name} to ${fwd.dest.any ? "any" : fwd.dest.zone.name} ${family ? `${this.nfproto(family, true)} ` : ''}forwarding`;
2109 f.chain = fwd.src.any ? "forward" : `forward_${fwd.src.zone.name}`;
2112 f.target = "accept";
2114 f.jump_chain = `accept_to_${fwd.dest.zone.name}`;
2116 push(this.state.rules ||= [], f);
2120 /* inherit family restrictions from related zones */
2121 let family = infer_family(fwd.family, [
2122 fwd.src?.zone, "source zone",
2123 fwd.dest?.zone, "destination zone"
2126 if (type(family) == "string") {
2127 this.warn_section(data, `${family}, skipping`);
2131 add_rule(family, fwd);
2134 fwd.dest.zone.dflags.accept = true;
2137 parse_rule: function(data) {
2138 let rule = this.parse_options(data, {
2139 enabled: [ "bool", "1" ],
2141 name: [ "string", this.section_id(data[".name"]) ],
2142 _name: [ "string", null, DEPRECATED ],
2143 family: [ "family" ],
2145 src: [ "zone_ref" ],
2146 dest: [ "zone_ref" ],
2148 device: [ "device", null, NO_INVERT ],
2149 direction: [ "direction" ],
2151 ipset: [ "setmatch" ],
2152 helper: [ "cthelper" ],
2153 set_helper: [ "cthelper", null, NO_INVERT ],
2155 proto: [ "protocol", "tcpudp", PARSE_LIST | FLATTEN_LIST ],
2157 src_ip: [ "network", null, PARSE_LIST ],
2158 src_mac: [ "mac", null, PARSE_LIST ],
2159 src_port: [ "port", null, PARSE_LIST ],
2161 dest_ip: [ "network", null, PARSE_LIST ],
2162 dest_port: [ "port", null, PARSE_LIST ],
2164 icmp_type: [ "icmptype", null, PARSE_LIST ],
2165 extra: [ "string", null, UNSUPPORTED ],
2168 limit_burst: [ "int" ],
2170 utc_time: [ "bool" ],
2171 start_date: [ "date" ],
2172 stop_date: [ "date" ],
2173 start_time: [ "time" ],
2174 stop_time: [ "time" ],
2175 weekdays: [ "weekdays" ],
2176 monthdays: [ "monthdays", null, UNSUPPORTED ],
2179 set_mark: [ "mark", null, NO_INVERT ],
2180 set_xmark: [ "mark", null, NO_INVERT ],
2183 set_dscp: [ "dscp", null, NO_INVERT ],
2185 counter: [ "bool", "1" ],
2187 target: [ "target" ]
2190 if (rule === false) {
2191 this.warn_section(data, "skipped due to invalid options");
2194 else if (!rule.enabled) {
2195 this.warn_section(data, "is disabled, ignoring section");
2199 if (rule.target in ["helper", "notrack"] && (!rule.src || !rule.src.zone)) {
2200 this.warn_section(data, `must specify a source zone for target '${rule.target}'`);
2203 else if (rule.target == "dscp" && !rule.set_dscp) {
2204 this.warn_section(data, "must specify option 'set_dscp' for target 'dscp'");
2207 else if (rule.target == "mark" && !rule.set_mark && !rule.set_xmark) {
2208 this.warn_section(data, "must specify option 'set_mark' or 'set_xmark' for target 'mark'");
2211 else if (rule.target == "helper" && !rule.set_helper) {
2212 this.warn_section(data, "must specify option 'set_helper' for target 'helper'");
2215 else if (rule.device?.any) {
2216 this.warn_section(data, "must not specify '*' as device");
2223 ipset = filter(this.state.ipsets, s => (s.name == rule.ipset.name))[0];
2226 this.warn_section(data, `references unknown set '${rule.ipset.name}'`);
2230 if (('inet_service' in ipset.types) && !ensure_tcpudp(rule.proto)) {
2231 this.warn_section(data, "references named set with port match but no UDP/TCP protocol, ignoring section");
2236 let need_src_action_chain = (rule) => (rule.src?.zone?.log && rule.target != "accept");
2238 let add_rule = (family, proto, saddrs, daddrs, sports, dports, icmptypes, icmpcodes, ipset, rule) => {
2244 has_addrs: !!(saddrs[0] || saddrs[1] || saddrs[2] || daddrs[0] || daddrs[1] || daddrs[2]),
2245 has_ports: !!(length(sports) || length(dports)),
2246 saddrs_pos: map(saddrs[0], this.cidr),
2247 saddrs_neg: map(saddrs[1], this.cidr),
2248 saddrs_masked: saddrs[2],
2249 daddrs_pos: map(daddrs[0], this.cidr),
2250 daddrs_neg: map(daddrs[1], this.cidr),
2251 daddrs_masked: daddrs[2],
2252 sports_pos: map(filter_pos(sports), this.port),
2253 sports_neg: map(filter_neg(sports), this.port),
2254 dports_pos: map(filter_pos(dports), this.port),
2255 dports_neg: map(filter_neg(dports), this.port),
2256 smacs_pos: map(filter_pos(rule.src_mac), m => m.mac),
2257 smacs_neg: map(filter_neg(rule.src_mac), m => m.mac),
2258 icmp_types: map(icmptypes, i => (family == 4 ? i.type : i.type6)),
2259 icmp_codes: map(icmpcodes, ic => `${(family == 4) ? ic.type : ic.type6} . ${(family == 4) ? ic.code_min : ic.code6_min}`)
2262 if (!length(r.icmp_types))
2263 delete r.icmp_types;
2265 if (!length(r.icmp_codes))
2266 delete r.icmp_codes;
2270 invert: r.set_mark.invert,
2271 mark: r.set_mark.mark,
2272 mask: r.set_mark.mark | r.set_mark.mask
2278 let set_types = map_setmatch(ipset, rule.ipset, proto.name);
2280 if (set_types !== set_types) {
2281 this.warn_section(data, "destination MAC address matching not supported");
2283 } else if (set_types) {
2284 r.ipset = { ...r.ipset, fields: set_types };
2287 if (r.target == "notrack") {
2288 r.chain = `notrack_${r.src.zone.name}`;
2289 r.src.zone.dflags.notrack = true;
2291 else if (r.target == "helper") {
2292 r.chain = `helper_${r.src.zone.name}`;
2293 r.src.zone.dflags.helper = true;
2295 else if (r.target == "mark" || r.target == "dscp") {
2296 if ((r.src?.any && r.dest?.any) || (r.src?.zone && r.dest?.zone))
2297 r.chain = "mangle_forward";
2298 else if (r.src?.any && r.dest?.zone)
2299 r.chain = "mangle_postrouting";
2300 else if (r.src?.zone && r.dest?.any)
2301 r.chain = "mangle_prerouting";
2302 else if (r.src && !r.dest)
2303 r.chain = "mangle_input";
2305 r.chain = "mangle_output";
2308 r.src.zone.dflags[r.target] = true;
2309 r.iifnames = null_if_empty(r.src.zone.match_devices);
2313 r.dest.zone.dflags[r.target] = true;
2314 r.oifnames = null_if_empty(r.dest.zone.match_devices);
2322 r.chain = `${r.dest ? "forward" : "input"}_${r.src.zone.name}`;
2324 r.chain = r.dest ? "forward" : "input";
2327 if (r.dest && !r.src) {
2329 r.chain = sprintf("output_%s", r.dest.zone.name);
2334 if (r.dest && !r.dest.any) {
2335 r.jump_chain = `${r.target}_to_${r.dest.zone.name}`;
2336 r.dest.zone.dflags[r.target] = true;
2338 else if (need_src_action_chain(r)) {
2339 r.jump_chain = `${r.target}_from_${r.src.zone.name}`;
2340 r.src.zone.sflags[r.target] = true;
2342 else if (r.target == "reject")
2343 r.jump_chain = "handle_reject";
2347 r[r.direction ? "oifnames" : "iifnames"] = [ r.device.device ];
2349 push(this.state.rules ||= [], r);
2352 for (let proto in rule.proto) {
2353 let sip, dip, sports, dports, itypes4, itypes6;
2354 let family = rule.family;
2356 switch (proto.name) {
2358 itypes4 = filter(rule.icmp_type || [], family_is_ipv4);
2359 itypes6 = filter(rule.icmp_type || [], family_is_ipv6);
2364 itypes6 = filter(rule.icmp_type || [], family_is_ipv6);
2369 sports = rule.src_port;
2370 dports = rule.dest_port;
2374 sip = subnets_split_af(rule.src_ip);
2375 dip = subnets_split_af(rule.dest_ip);
2377 family = infer_family(family, [
2380 dip, "destination IP",
2381 rule.src?.zone, "source zone",
2382 rule.dest?.zone, "destination zone",
2383 rule.helper, "helper match",
2384 rule.set_helper, "helper to set"
2387 if (type(family) == "string") {
2388 this.warn_section(data, `${family}, skipping`);
2392 let has_ipv4_specifics = (length(sip[0]) || length(dip[0]) || length(itypes4) || rule.dscp !== null);
2393 let has_ipv6_specifics = (length(sip[1]) || length(dip[1]) || length(itypes6) || rule.dscp !== null);
2395 /* if no family was configured, infer target family from IP addresses */
2396 if (family === null) {
2397 if (has_ipv4_specifics && !has_ipv6_specifics)
2399 else if (has_ipv6_specifics && !has_ipv4_specifics)
2405 /* check if there's no AF specific bits, in this case we can do an AF agnostic rule */
2406 if (!family && rule.target != "dscp" && !has_ipv4_specifics && !has_ipv6_specifics) {
2407 add_rule(0, proto, [], [], sports, dports, null, null, null, rule);
2410 /* we need to emit one or two AF specific rules */
2412 if (family == 0 || family == 4) {
2413 let icmp_types = filter(itypes4, i => (i.code_min == 0 && i.code_max == 0xFF));
2414 let icmp_codes = filter(itypes4, i => (i.code_min != 0 || i.code_max != 0xFF));
2416 for (let saddrs in subnets_group_by_masking(sip[0])) {
2417 for (let daddrs in subnets_group_by_masking(dip[0])) {
2418 if (length(icmp_types) || (!length(icmp_types) && !length(icmp_codes)))
2419 add_rule(4, proto, saddrs, daddrs, sports, dports, icmp_types, null, ipset, rule);
2421 if (length(icmp_codes))
2422 add_rule(4, proto, saddrs, daddrs, sports, dports, null, icmp_codes, ipset, rule);
2427 if (family == 0 || family == 6) {
2428 let icmp_types = filter(itypes6, i => (i.code_min == 0 && i.code_max == 0xFF));
2429 let icmp_codes = filter(itypes6, i => (i.code_min != 0 || i.code_max != 0xFF));
2431 for (let saddrs in subnets_group_by_masking(sip[1])) {
2432 for (let daddrs in subnets_group_by_masking(dip[1])) {
2433 if (length(icmp_types) || (!length(icmp_types) && !length(icmp_codes)))
2434 add_rule(6, proto, saddrs, daddrs, sports, dports, icmp_types, null, ipset, rule);
2436 if (length(icmp_codes))
2437 add_rule(6, proto, saddrs, daddrs, sports, dports, null, icmp_codes, ipset, rule);
2445 parse_redirect: function(data) {
2446 let redir = this.parse_options(data, {
2447 enabled: [ "bool", "1" ],
2449 name: [ "string", this.section_id(data[".name"]) ],
2450 _name: [ "string", null, DEPRECATED ],
2451 family: [ "family" ],
2453 src: [ "zone_ref" ],
2454 dest: [ "zone_ref" ],
2456 ipset: [ "setmatch" ],
2457 helper: [ "cthelper", null, NO_INVERT ],
2459 proto: [ "protocol", "tcpudp", PARSE_LIST | FLATTEN_LIST ],
2461 src_ip: [ "network" ],
2462 src_mac: [ "mac", null, PARSE_LIST ],
2463 src_port: [ "port" ],
2465 src_dip: [ "network" ],
2466 src_dport: [ "port" ],
2468 dest_ip: [ "network" ],
2469 dest_port: [ "port" ],
2471 extra: [ "string", null, UNSUPPORTED ],
2474 limit_burst: [ "int" ],
2476 utc_time: [ "bool" ],
2477 start_date: [ "date" ],
2478 stop_date: [ "date" ],
2479 start_time: [ "time" ],
2480 stop_time: [ "time" ],
2481 weekdays: [ "weekdays" ],
2482 monthdays: [ "monthdays", null, UNSUPPORTED ],
2486 reflection: [ "bool", "1" ],
2487 reflection_src: [ "reflection_source", "internal" ],
2489 reflection_zone: [ "zone_ref", null, PARSE_LIST ],
2491 counter: [ "bool", "1" ],
2493 target: [ "target", "dnat" ]
2496 if (redir === false) {
2497 this.warn_section(data, "skipped due to invalid options");
2500 else if (!redir.enabled) {
2501 this.warn_section(data, "is disabled, ignoring section");
2505 if (!(redir.target in ["dnat", "snat"])) {
2506 this.warn_section(data, "has invalid target specified, defaulting to dnat");
2507 redir.target = "dnat";
2513 ipset = filter(this.state.ipsets, s => (s.name == redir.ipset.name))[0];
2516 this.warn_section(data, `references unknown set '${redir.ipset.name}'`);
2520 if (('inet_service' in ipset.types) && !ensure_tcpudp(redir.proto)) {
2521 this.warn_section(data, "references named set with port match but no UDP/TCP protocol, ignoring section");
2526 let resolve_dest = (redir) => {
2527 for (let zone in this.state.zones) {
2528 for (let zone_addr in zone.related_subnets) {
2529 for (let dest_addr in redir.dest_ip.addrs) {
2530 if (dest_addr.family != zone_addr.family)
2533 let a = apply_mask(dest_addr.addr, zone_addr.mask);
2534 let b = apply_mask(zone_addr.addr, zone_addr.mask);
2552 if (redir.target == "dnat") {
2554 return this.warn_section(data, "has no source specified");
2555 else if (redir.src.any)
2556 return this.warn_section(data, "must not have source '*' for dnat target");
2557 else if (redir.dest_ip && redir.dest_ip.invert)
2558 return this.warn_section(data, "must not specify a negated 'dest_ip' value");
2559 else if (redir.dest_ip && length(filter(redir.dest_ip.addrs, a => a.bits == -1)))
2560 return this.warn_section(data, "must not use non-contiguous masks in 'dest_ip'");
2562 if (!redir.dest && redir.dest_ip && resolve_dest(redir))
2563 this.warn_section(data, `does not specify a destination, assuming '${redir.dest.zone.name}'`);
2565 if (!redir.dest_port)
2566 redir.dest_port = redir.src_dport;
2568 if (redir.reflection && redir.dest?.zone && redir.src.zone.masq) {
2569 redir.dest.zone.dflags.accept = true;
2570 redir.dest.zone.dflags.dnat = true;
2571 redir.dest.zone.dflags.snat = true;
2575 redir.src.zone.dflags.helper = true;
2577 redir.src.zone.dflags[redir.target] = true;
2581 return this.warn_section(data, "has no destination specified");
2582 else if (redir.dest.any)
2583 return this.warn_section(data, "must not have destination '*' for snat target");
2584 else if (!redir.src_dip)
2585 return this.warn_section(data, "has no 'src_dip' option specified");
2586 else if (redir.src_dip.invert)
2587 return this.warn_section(data, "must not specify a negated 'src_dip' value");
2588 else if (length(filter(redir.src_dip.addrs, a => a.bits == -1)))
2589 return this.warn_section(data, "must not use non-contiguous masks in 'src_dip'");
2590 else if (redir.src_mac)
2591 return this.warn_section(data, "must not use 'src_mac' option for snat target");
2592 else if (redir.helper)
2593 return this.warn_section(data, "must not use 'helper' option for snat target");
2595 redir.dest.zone.dflags[redir.target] = true;
2599 let add_rule = (family, proto, saddrs, daddrs, raddrs, sport, dport, rport, ipset, redir) => {
2605 has_addrs: !!(saddrs[0] || saddrs[1] || saddrs[2] || daddrs[0] || daddrs[1] || daddrs[2]),
2606 has_ports: !!(sport || dport || rport),
2607 saddrs_pos: map(saddrs[0], this.cidr),
2608 saddrs_neg: map(saddrs[1], this.cidr),
2609 saddrs_masked: saddrs[2],
2610 daddrs_pos: map(daddrs[0], this.cidr),
2611 daddrs_neg: map(daddrs[1], this.cidr),
2612 daddrs_masked: daddrs[2],
2613 sports_pos: map(filter_pos(to_array(sport)), this.port),
2614 sports_neg: map(filter_neg(to_array(sport)), this.port),
2615 dports_pos: map(filter_pos(to_array(dport)), this.port),
2616 dports_neg: map(filter_neg(to_array(dport)), this.port),
2617 smacs_pos: map(filter_pos(redir.src_mac), m => m.mac),
2618 smacs_neg: map(filter_neg(redir.src_mac), m => m.mac),
2620 raddr: raddrs ? raddrs[0] : null,
2624 let set_types = map_setmatch(ipset, redir.ipset, proto.name);
2626 if (set_types !== set_types) {
2627 this.warn_section(data, "destination MAC address matching not supported");
2629 } else if (set_types) {
2630 r.ipset = { ...r.ipset, fields: set_types };
2635 r.chain = `dstnat_${r.src.zone.name}`;
2636 r.src.zone.dflags.dnat = true;
2639 r.target = "redirect";
2644 r.chain = `srcnat_${r.dest.zone.name}`;
2645 r.dest.zone.dflags.snat = true;
2649 push(this.state.redirects ||= [], r);
2652 let to_hostaddr = (a) => {
2653 let bits = (a.family == 4) ? 32 : 128;
2657 addr: apply_mask(a.addr, bits),
2662 for (let proto in redir.proto) {
2663 let sip, dip, rip, iip, eip, refip, sport, dport, rport;
2664 let family = redir.family;
2666 if (proto.name == "ipv6-icmp")
2669 switch (redir.target) {
2671 sip = subnets_split_af(redir.src_ip);
2672 dip = subnets_split_af(redir.src_dip);
2673 rip = subnets_split_af(redir.dest_ip);
2675 switch (proto.name) {
2678 sport = redir.src_port;
2679 dport = redir.src_dport;
2680 rport = redir.dest_port;
2687 sip = subnets_split_af(redir.src_ip);
2688 dip = subnets_split_af(redir.dest_ip);
2689 rip = subnets_split_af(redir.src_dip);
2691 switch (proto.name) {
2694 sport = redir.src_port;
2695 dport = redir.dest_port;
2696 rport = redir.src_dport;
2703 family = infer_family(family, [
2706 dip, "destination IP",
2708 redir.src?.zone, "source zone",
2709 redir.dest?.zone, "destination zone",
2710 redir.helper, "helper match"
2713 if (type(family) == "string") {
2714 this.warn_section(data, `${family}, skipping`);
2718 /* build reflection rules */
2719 if (redir.target == "dnat" && redir.reflection &&
2720 (length(rip[0]) || length(rip[1])) && redir.src?.zone && redir.dest?.zone) {
2722 name: `${redir.name} (reflection)`,
2724 helper: redir.helper,
2726 // XXX: this likely makes no sense for reflection rules
2727 //src_mac: redir.src_mac,
2730 limit_burst: redir.limit_burst,
2732 start_date: redir.start_date,
2733 stop_date: redir.stop_date,
2734 start_time: redir.start_time,
2735 stop_time: redir.stop_time,
2736 weekdays: redir.weekdays,
2741 let eaddrs = length(dip) ? dip : subnets_split_af({ addrs: map(redir.src.zone.related_subnets, to_hostaddr) });
2742 let rzones = length(redir.reflection_zone) ? redir.reflection_zone : [ redir.dest ];
2744 for (let rzone in rzones) {
2745 if (!is_family(rzone, family)) {
2746 this.warn_section(data,
2747 `is restricted to IPv${family} but referenced reflection zone is IPv${rzone.family} only, skipping`);
2751 let iaddrs = subnets_split_af({ addrs: rzone.zone.related_subnets });
2752 let refaddrs = (redir.reflection_src == "internal") ? iaddrs : eaddrs;
2754 for (let i = 0; i <= 1; i++) {
2755 if (redir.src.zone[i ? "masq6" : "masq"] && length(rip[i])) {
2756 let snat_addr = refaddrs[i]?.[0];
2758 /* For internal reflection sources try to find a suitable candiate IP
2759 * among the reflection zone subnets which is within the same subnet
2760 * as the original DNAT destination. If we can't find any matching
2761 * one then simply take the first candidate. */
2762 if (redir.reflection_src == "internal") {
2763 for (let zone_addr in rzone.zone.related_subnets) {
2764 if (zone_addr.family != rip[i][0].family)
2767 let r = apply_mask(rip[i][0].addr, zone_addr.mask);
2768 let a = apply_mask(zone_addr.addr, zone_addr.mask);
2773 snat_addr = zone_addr;
2779 this.warn_section(data, `${redir.reflection_src || "external"} rewrite IP cannot be determined, disabling reflection`);
2781 else if (!length(iaddrs[i])) {
2782 this.warn_section(data, "internal address range cannot be determined, disabling reflection");
2784 else if (!length(eaddrs[i])) {
2785 this.warn_section(data, "external address range cannot be determined, disabling reflection");
2788 refredir.src = rzone;
2789 refredir.dest = null;
2790 refredir.target = "dnat";
2792 for (let saddrs in subnets_group_by_masking(iaddrs[i]))
2793 for (let daddrs in subnets_group_by_masking(eaddrs[i]))
2794 add_rule(i ? 6 : 4, proto, saddrs, daddrs, rip[i], sport, dport, rport, null, refredir);
2796 refredir.src = null;
2797 refredir.dest = rzone;
2798 refredir.target = "snat";
2800 for (let daddrs in subnets_group_by_masking(rip[i]))
2801 for (let saddrs in subnets_group_by_masking(iaddrs[i]))
2802 add_rule(i ? 6 : 4, proto, saddrs, daddrs, [ to_hostaddr(snat_addr) ], null, rport, null, null, refredir);
2809 if (length(rip[0]) > 1 || length(rip[1]) > 1)
2810 this.warn_section(data, "specifies multiple rewrite addresses, using only first one");
2812 let has_ip4_addr = length(sip[0]) || length(dip[0]) || length(rip[0]),
2813 has_ip6_addr = length(sip[1]) || length(dip[1]) || length(rip[1]),
2814 has_any_addr = has_ip4_addr || has_ip6_addr;
2816 /* check if there's no AF specific bits, in this case we can do an AF agnostic rule */
2817 if (!family && !has_any_addr) {
2818 /* for backwards compatibility, treat unspecified family as IPv4 unless user explicitly requested any (0) */
2822 add_rule(family, proto, [], [], null, sport, dport, rport, null, redir);
2825 /* we need to emit one or two AF specific rules */
2827 if ((!family || family == 4) && (!has_any_addr || has_ip4_addr)) {
2828 for (let saddrs in subnets_group_by_masking(sip[0]))
2829 for (let daddrs in subnets_group_by_masking(dip[0]))
2830 add_rule(4, proto, saddrs, daddrs, rip[0], sport, dport, rport, ipset, redir);
2833 if ((!family || family == 6) && (!has_any_addr || has_ip6_addr)) {
2834 for (let saddrs in subnets_group_by_masking(sip[1]))
2835 for (let daddrs in subnets_group_by_masking(dip[1]))
2836 add_rule(6, proto, saddrs, daddrs, rip[1], sport, dport, rport, ipset, redir);
2842 parse_nat: function(data) {
2843 let snat = this.parse_options(data, {
2844 enabled: [ "bool", "1" ],
2846 name: [ "string", this.section_id(data[".name"]) ],
2847 family: [ "family" ],
2849 src: [ "zone_ref" ],
2850 device: [ "string" ],
2852 ipset: [ "setmatch", null, UNSUPPORTED ],
2854 proto: [ "protocol", "all", PARSE_LIST | FLATTEN_LIST ],
2856 src_ip: [ "network" ],
2857 src_port: [ "port" ],
2859 snat_ip: [ "network", null, NO_INVERT ],
2860 snat_port: [ "port", null, NO_INVERT ],
2862 dest_ip: [ "network" ],
2863 dest_port: [ "port" ],
2865 extra: [ "string", null, UNSUPPORTED ],
2868 limit_burst: [ "int" ],
2870 connlimit_ports: [ "bool" ],
2872 utc_time: [ "bool" ],
2873 start_date: [ "date" ],
2874 stop_date: [ "date" ],
2875 start_time: [ "time" ],
2876 stop_time: [ "time" ],
2877 weekdays: [ "weekdays" ],
2878 monthdays: [ "monthdays", null, UNSUPPORTED ],
2882 target: [ "target", "masquerade" ]
2885 if (snat === false) {
2886 this.warn_section(data, "skipped due to invalid options");
2889 else if (!snat.enabled) {
2890 this.warn_section(data, "is disabled, ignoring section");
2894 if (!(snat.target in ["accept", "snat", "masquerade"])) {
2895 this.warn_section(data, "has invalid target specified, defaulting to masquerade");
2896 snat.target = "masquerade";
2899 if (snat.target == "snat" && !snat.snat_ip && !snat.snat_port) {
2900 this.warn_section(data, "needs either 'snat_ip' or 'snat_port' for target snat, ignoring section");
2903 else if (snat.target != "snat" && snat.snat_ip) {
2904 this.warn_section(data, "must not use 'snat_ip' for non-snat target, ignoring section");
2907 else if (snat.target != "snat" && snat.snat_port) {
2908 this.warn_section(data, "must not use 'snat_port' for non-snat target, ignoring section");
2912 if ((snat.snat_port || snat.src_port || snat.dest_port) && !ensure_tcpudp(snat.proto)) {
2913 this.warn_section(data, "specifies ports but no UDP/TCP protocol, ignoring section");
2917 if (snat.snat_ip && length(filter(snat.snat_ip.addrs, a => a.bits == -1 || a.invert))) {
2918 this.warn_section(data, "must not use inversion or non-contiguous masks in 'snat_ip', ignoring section");
2922 let add_rule = (family, proto, saddrs, daddrs, raddrs, sport, dport, rport, snat) => {
2928 has_addrs: !!(saddrs[0] || saddrs[1] || saddrs[2] || daddrs[0] || daddrs[1] || daddrs[2]),
2929 has_ports: !!(sport || dport),
2930 saddrs_pos: map(saddrs[0], this.cidr),
2931 saddrs_neg: map(saddrs[1], this.cidr),
2932 saddrs_masked: saddrs[2],
2933 daddrs_pos: map(daddrs[0], this.cidr),
2934 daddrs_neg: map(daddrs[1], this.cidr),
2935 daddrs_masked: daddrs[2],
2936 sports_pos: map(filter_pos(to_array(sport)), this.port),
2937 sports_neg: map(filter_neg(to_array(sport)), this.port),
2938 dports_pos: map(filter_pos(to_array(dport)), this.port),
2939 dports_neg: map(filter_neg(to_array(dport)), this.port),
2941 raddr: raddrs ? raddrs[0] : null,
2944 chain: snat.src?.zone ? `srcnat_${snat.src.zone.name}` : "srcnat"
2947 push(this.state.redirects ||= [], n);
2950 for (let proto in snat.proto) {
2951 let sip, dip, rip, sport, dport, rport;
2952 let family = snat.family;
2954 sip = subnets_split_af(snat.src_ip);
2955 dip = subnets_split_af(snat.dest_ip);
2956 rip = subnets_split_af(snat.snat_ip);
2958 switch (proto.name) {
2961 sport = snat.src_port;
2962 dport = snat.dest_port;
2963 rport = snat.snat_port;
2967 if (length(rip[0]) > 1 || length(rip[1]) > 1)
2968 this.warn_section(data, "specifies multiple rewrite addresses, using only first one");
2970 family = infer_family(family, [
2972 dip, "destination IP",
2974 snat.src?.zone, "source zone"
2977 if (type(family) == "string") {
2978 this.warn_section(data, `${family}, skipping`);
2983 snat.src.zone.dflags.snat = true;
2985 /* if no family was configured, infer target family from IP addresses */
2986 if (family === null) {
2987 if ((length(sip[0]) || length(dip[0]) || length(rip[0])) && !length(sip[1]) && !length(dip[1]) && !length(rip[1]))
2989 else if ((length(sip[1]) || length(dip[1]) || length(rip[1])) && !length(sip[0]) && !length(dip[0]) && !length(rip[0]))
2992 family = 4; /* default to IPv4 only for backwards compatibility, unless an explict family any was configured */
2995 /* check if there's no AF specific bits, in this case we can do an AF agnostic rule */
2996 if (!family && !length(sip[0]) && !length(sip[1]) && !length(dip[0]) && !length(dip[1]) && !length(rip[0]) && !length(rip[1])) {
2997 add_rule(0, proto, [], [], null, sport, dport, rport, snat);
3000 /* we need to emit one or two AF specific rules */
3002 if (family == 0 || family == 4)
3003 for (let saddr in subnets_group_by_masking(sip[0]))
3004 for (let daddr in subnets_group_by_masking(dip[0]))
3005 add_rule(4, proto, saddr, daddr, rip[0], sport, dport, rport, snat);
3007 if (family == 0 || family == 6)
3008 for (let saddr in subnets_group_by_masking(sip[1]))
3009 for (let daddr in subnets_group_by_masking(dip[1]))
3010 add_rule(6, proto, saddr, daddr, rip[1], sport, dport, rport, snat);
3015 parse_ipset: function(data) {
3016 let ipset = this.parse_options(data, {
3017 enabled: [ "bool", "1" ],
3018 reload_set: [ "bool" ],
3019 counters: [ "bool" ],
3020 comment: [ "bool" ],
3022 name: [ "string", null, REQUIRED ],
3023 family: [ "family", "4" ],
3025 storage: [ "string", null, UNSUPPORTED ],
3026 match: [ "ipsettype", null, PARSE_LIST ],
3028 iprange: [ "string", null, UNSUPPORTED ],
3029 portrange: [ "string", null, UNSUPPORTED ],
3031 netmask: [ "int", null, UNSUPPORTED ],
3033 hashsize: [ "int", null, UNSUPPORTED ],
3034 timeout: [ "int", "-1" ],
3036 external: [ "string", null, UNSUPPORTED ],
3038 entry: [ "string", null, PARSE_LIST ],
3039 loadfile: [ "string" ]
3042 if (ipset === false) {
3043 this.warn_section(data, "skipped due to invalid options");
3046 else if (!ipset.enabled) {
3047 this.warn_section(data, "is disabled, ignoring section");
3051 if (ipset.family == 0) {
3052 this.warn_section(data, "must not specify family 'any'");
3055 else if (!length(ipset.match)) {
3056 this.warn_section(data, "has no datatypes assigned");
3060 let dirs = map(ipset.match, m => m[0]),
3061 types = map(ipset.match, m => m[1]),
3064 if ("set" in types) {
3065 this.warn_section(data, "match type 'set' is not supported");
3069 if ("net" in types) {
3070 if (this.kernel < 0x05060000) {
3071 this.warn_section(data, "match type 'net' requires kernel 5.6 or later");
3083 types: map(types, (t) => {
3087 return (ipset.family == 4) ? 'ipv4_addr' : 'ipv6_addr';
3090 return 'ether_addr';
3093 return 'inet_service';
3101 s.entries = filter(map(ipset.entry, (e) => {
3102 let v = this.parse_ipsetentry(e, s);
3105 this.warn_section(data, `ignoring invalid ipset entry '${e}'`);
3108 }), (e) => (e != null));
3110 push(this.state.ipsets ||= [], s);