1 let fs = require("fs");
2 let uci = require("uci");
3 let ubus = require("ubus");
5 let STATEFILE = "/var/run/fw4.state";
8 let FLATTEN_LIST = 0x02;
10 let UNSUPPORTED = 0x08;
12 let DEPRECATED = 0x20;
14 let 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 let 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 ]
133 function to_mask(bits, v6) {
136 if (bits < 0 || bits > (v6 ? 128 : 32))
139 for (let i = 0; i < (v6 ? 16 : 4); i++) {
140 let b = (bits < 8) ? bits : 8;
141 m[i] = (0xff << (8 - b)) & 0xff;
148 function to_bits(mask) {
149 let a = iptoarr(mask);
156 for (let i = 0, z = false; i < length(a); i++) {
159 while (!z && (a[i] & 0x80)) {
160 a[i] = (a[i] << 1) & 0xff;
171 function apply_mask(addr, mask) {
172 let a = iptoarr(addr);
177 if (type(mask) == "int") {
178 for (let i = 0; i < length(a); i++) {
179 let b = (mask < 8) ? mask : 8;
180 a[i] &= (0xff << (8 - b)) & 0xff;
185 let m = iptoarr(mask);
187 if (!m || length(a) != length(m))
190 for (let i = 0; i < length(a); i++)
197 function to_array(x) {
198 if (type(x) == "array")
204 if (type(x) == "object")
209 return (x == "") ? [] : split(x, /[ \t]+/);
212 function filter_pos(x) {
213 let rv = filter(x, e => !e.invert);
214 return length(rv) ? rv : null;
217 function filter_neg(x) {
218 let rv = filter(x, e => e.invert);
219 return length(rv) ? rv : null;
222 function null_if_empty(x) {
223 return length(x) ? x : null;
226 function subnets_split_af(x) {
229 for (let ag in to_array(x)) {
230 for (let a in filter(ag.addrs, a => (a.family == 4))) {
232 push(rv[0], { ...a, invert: ag.invert });
235 for (let a in filter(ag.addrs, a => (a.family == 6))) {
237 push(rv[1], { ...a, invert: ag.invert });
242 rv.family = (!rv[0] ^ !rv[1]) ? (rv[0] ? 4 : 6) : 0;
247 function subnets_group_by_masking(x) {
248 let groups = [], plain = [], nc = [], invert_plain = [], invert_masked = [];
250 for (let a in to_array(x)) {
251 if (a.bits == -1 && !a.invert)
255 else if (a.bits == -1)
256 push(invert_masked, a);
258 push(invert_plain, a);
262 push(groups, [ null, null_if_empty(invert_plain), [ a, ...invert_masked ] ]);
267 null_if_empty(invert_plain),
268 null_if_empty(invert_masked)
271 else if (!length(groups)) {
274 null_if_empty(invert_plain),
275 null_if_empty(invert_masked)
282 function ensure_tcpudp(x) {
283 if (length(filter(x, p => (p.name == "tcp" || p.name == "udp"))))
286 let rest = filter(x, p => !p.any),
287 any = filter(x, p => p.any);
289 if (length(any) && !length(rest)) {
291 push(x, { name: "tcp" }, { name: "udp" });
298 let is_family = (x, v) => (!x.family || x.family == v);
299 let family_is_ipv4 = (x) => (!x.family || x.family == 4);
300 let family_is_ipv6 = (x) => (!x.family || x.family == 6);
302 function infer_family(f, objects) {
306 for (let i = 0; i < length(objects); i += 2) {
307 let objs = to_array(objects[i]),
308 desc = objects[i + 1];
310 for (let obj in objs) {
311 if (!obj || !obj.family || obj.family == res)
321 ? sprintf('references IPv%d only %s but is restricted to IPv%d by %s', obj.family, desc, res, by)
322 : sprintf('is restricted to IPv%d but referenced %s is IPv%d only', res, desc, obj.family);
329 function map_setmatch(set, match, proto) {
330 if (!set || (('inet_service' in set.types) && proto != 'tcp' && proto != 'udp'))
335 for (let i, t in set.types) {
336 let dir = (((match.dir && match.dir[i]) || set.directions[i] || 'src') == 'src' ? 's' : 'd');
340 fields[i] = sprintf('ip %saddr', dir);
344 fields[i] = sprintf('ip6 %saddr', dir);
351 fields[i] = 'ether saddr';
355 fields[i] = sprintf('%s %sport', proto, dir);
363 function resolve_lower_devices(devstatus, devname) {
364 let dir = fs.opendir("/sys/class/net/" + devname);
368 if (!devstatus || devstatus[devname]?.["hw-tc-offload"]) {
374 while ((e = dir.read()) != null)
375 if (index(e, "lower_") === 0)
376 push(devs, ...resolve_lower_devices(devstatus, substr(e, 6)));
385 function nft_json_command(...args) {
386 let cmd = [ "/usr/sbin/nft", "--terse", "--json", ...args ];
387 let nft = fs.popen(join(" ", cmd), "r");
392 info = filter(json(nft.read("all"))?.nftables,
393 item => (type(item) == "object" && !item.metainfo));
396 warn(sprintf("Unable to parse nftables JSON output: %s\n", e));
402 warn(sprintf("Unable to popen() %s: %s\n", cmd, fs.error()));
408 function nft_try_hw_offload(devices) {
410 'add table inet fw4-hw-offload-test; ' +
411 'add flowtable inet fw4-hw-offload-test ft { ' +
412 'hook ingress priority 0; ' +
413 'devices = { "' + join('", "', devices) + '" }; ' +
417 let rc = system(sprintf("/usr/sbin/nft -c '%s' 2>/dev/null", replace(nft_test, "'", "'\\''")));
424 read_kernel_version: function() {
425 let fd = fs.open("/proc/version", "r"),
429 let m = match(fd.read("line"), /^Linux version ([0-9]+)\.([0-9]+)\.([0-9]+)/);
431 v = m ? (+m[1] << 24) | (+m[2] << 16) | (+m[3] << 8) : 0;
438 resolve_offload_devices: function() {
439 if (!this.default_option("flow_offloading"))
442 let devstatus = null;
445 if (this.default_option("flow_offloading_hw")) {
446 let bus = ubus.connect();
449 devstatus = bus.call("network.device", "status") || {};
453 for (let zone in this.zones())
454 for (let device in zone.related_physdevs)
455 push(devices, ...resolve_lower_devices(devstatus, device));
457 devices = uniq(devices);
459 if (nft_try_hw_offload(devices))
462 this.warn('Hardware flow offloading unavailable, falling back to software offloading');
463 this.state.defaults.flow_offloading_hw = false;
468 for (let zone in this.zones())
469 for (let device in zone.match_devices)
470 push(devices, ...resolve_lower_devices(null, device));
472 return uniq(devices);
475 check_set_types: function() {
478 for (let item in nft_json_command("list", "sets", "inet"))
479 if (item.set?.table == "fw4")
480 sets[item.set.name] = (type(item.set.type) == "array") ? item.set.type : [ item.set.type ];
485 check_flowtable: function() {
486 for (let item in nft_json_command("list", "flowtables", "inet"))
487 if (item.flowtable?.table == "fw4" && item.flowtable?.name == "ft")
493 read_state: function() {
494 let fd = fs.open(STATEFILE, "r");
499 state = json(fd.read("all"));
502 warn(sprintf("Unable to parse '%s': %s\n", STATEFILE, e));
511 read_ubus: function() {
514 rules = [], networks = {},
515 bus = ubus.connect();
518 ifaces = bus.call("network.interface", "dump");
519 services = bus.call("service", "get_data", { "type": "firewall" });
524 warn(sprintf("Unable to connect to ubus: %s\n", ubus.error()));
529 // Gather logical network information from ubus
532 if (type(ifaces) == "object" && type(ifaces.interface) == "array") {
533 for (let ifc in ifaces.interface) {
536 device: ifc.l3_device,
541 if (type(ifc["ipv4-address"]) == "array") {
542 for (let addr in ifc["ipv4-address"]) {
543 net.ipaddrs = net.ipaddrs || [];
547 mask: to_mask(addr.mask, false),
553 if (type(ifc["ipv6-address"]) == "array") {
554 for (let addr in ifc["ipv6-address"]) {
555 net.ipaddrs = net.ipaddrs || [];
559 mask: to_mask(addr.mask, true),
565 if (type(ifc["ipv6-prefix-assignment"]) == "array") {
566 for (let addr in ifc["ipv6-prefix-assignment"]) {
567 if (addr["local-address"]) {
568 net.ipaddrs = net.ipaddrs || [];
571 addr: addr["local-address"].address,
572 mask: to_mask(addr["local-address"].mask, true),
573 bits: addr["local-address"].mask
579 if (type(ifc.data) == "object" && type(ifc.data.firewall) == "array") {
582 for (let rulespec in ifc.data.firewall) {
586 name: (rulespec.type != 'ipset') ? sprintf('ubus:%s[%s] %s %d', ifc.interface, ifc.proto, rulespec.type || 'rule', n) : rulespec.name,
587 device: rulespec.device || ifc.l3_device
594 networks[ifc.interface] = net;
600 // Gather firewall rule definitions from ubus services
603 if (type(services) == "object") {
604 for (let svcname, service in services) {
605 if (type(service) == "object" && type(service.firewall) == "array") {
608 for (let rulespec in services[svcname].firewall) {
612 name: (rulespec.type != 'ipset') ? sprintf('ubus:%s %s %d', svcname, rulespec.type || 'rule', n) : rulespec.name
619 for (let svcinst, instance in service) {
620 if (type(instance) == "object" && type(instance.firewall) == "array") {
623 for (let rulespec in instance.firewall) {
627 name: (rulespec.type != 'ipset') ? sprintf('ubus:%s[%s] %s %d', svcname, svcinst, rulespec.type || 'rule', n) : rulespec.name
643 load: function(use_statefile) {
646 this.state = use_statefile ? this.read_state() : null;
648 this.cursor = uci.cursor();
649 this.cursor.load("firewall");
650 this.cursor.load("/usr/share/firewall4/helpers");
653 this.state = this.read_ubus();
655 this.kernel = this.read_kernel_version();
659 // Read helper mapping
662 this.cursor.foreach("helpers", "helper", h => self.parse_helper(h));
666 // Read default policies
669 this.cursor.foreach("firewall", "defaults", d => self.parse_defaults(d));
671 if (!this.state.defaults)
672 this.parse_defaults({});
676 // Build list of ipsets
679 if (!this.state.ipsets) {
680 map(filter(this.state.ubus_rules, n => (n.type == "ipset")), s => self.parse_ipset(s));
681 this.cursor.foreach("firewall", "ipset", s => self.parse_ipset(s));
686 // Build list of logical zones
689 if (!this.state.zones)
690 this.cursor.foreach("firewall", "zone", z => self.parse_zone(z));
694 // Build list of rules
697 map(filter(this.state.ubus_rules, r => (r.type == "rule")), r => self.parse_rule(r));
698 this.cursor.foreach("firewall", "rule", r => self.parse_rule(r));
702 // Build list of forwardings
705 this.cursor.foreach("firewall", "forwarding", f => self.parse_forwarding(f));
709 // Build list of redirects
712 map(filter(this.state.ubus_rules, r => (r.type == "redirect")), r => self.parse_redirect(r));
713 this.cursor.foreach("firewall", "redirect", r => self.parse_redirect(r));
717 // Build list of snats
720 map(filter(this.state.ubus_rules, n => (n.type == "nat")), n => self.parse_nat(n));
721 this.cursor.foreach("firewall", "nat", n => self.parse_nat(n));
725 let fd = fs.open(STATEFILE, "w");
729 zones: this.state.zones,
730 ipsets: this.state.ipsets,
731 networks: this.state.networks,
732 ubus_rules: this.state.ubus_rules
738 warn("Unable to write '%s': %s\n", STATEFILE, fs.error());
743 warn: function(fmt, ...args) {
747 let msg = sprintf(fmt, ...args);
750 warn("\033[33m", msg, "\033[m\n");
752 warn("[!] ", msg, "\n");
755 get: function(sid, opt) {
756 return this.cursor.get("firewall", sid, opt);
759 get_all: function(sid) {
760 return this.cursor.get_all("firewall", sid);
763 parse_options: function(s, spec) {
766 for (let key, val in spec) {
767 let datatype = "parse_" + val[0],
770 parsefn = (flags & PARSE_LIST) ? "parse_list" : "parse_opt";
772 let res = this[parsefn](s, key, datatype, defval, flags);
777 if (type(res) == "object" && res.invert && (flags & NO_INVERT)) {
778 this.warn_section(s, "option '" + key + '" must not be negated');
783 if (flags & DEPRECATED)
784 this.warn_section(s, "option '" + key + "' is deprecated by fw4");
785 else if (flags & UNSUPPORTED)
786 this.warn_section(s, "option '" + key + "' is not supported by fw4");
793 if (index(opt, '.') != 0 && opt != 'type' && !exists(spec, opt)) {
794 this.warn_section(s, "specifies unknown option '" + opt + "'");
801 parse_subnet: function(subnet) {
802 let parts = split(subnet, "/");
805 switch (length(parts)) {
807 a = iptoarr(parts[0]);
808 m = iptoarr(parts[1]);
814 if (length(a) != length(m))
817 b = to_bits(parts[1]);
819 /* allow non-contiguous masks such as `::ffff:ffff:ffff:ffff` */
832 if (type(b) != "int")
835 m = to_mask(b, length(a) == 16);
839 family: (length(a) == 16) ? 6 : 4,
846 parts = split(parts[0], "-");
848 switch (length(parts)) {
850 a = iptoarr(parts[0]);
851 b = iptoarr(parts[1]);
853 if (a && b && length(a) == length(b)) {
855 family: (length(a) == 16) ? 6 : 4,
865 a = iptoarr(parts[0]);
869 family: (length(a) == 16) ? 6 : 4,
871 mask: to_mask(length(a) * 8, length(a) == 16),
876 n = this.state.networks[parts[0]];
879 return [ ...(n.ipaddrs || []) ];
886 parse_enum: function(val, choices) {
887 if (type(val) == "string") {
890 for (let i = 0; i < length(choices); i++)
891 if (lc(substr(choices[i], 0, length(val))) == val)
898 section_id: function(sid) {
899 let s = this.get_all(sid);
904 if (s[".anonymous"]) {
907 this.cursor.foreach("firewall", s[".type"], function(ss) {
908 if (ss[".name"] == s[".name"])
914 return sprintf("@%s[%d]", s[".type"], c);
920 warn_section: function(s, msg) {
923 this.warn("Section %s (%s) %s", this.section_id(s[".name"]), s.name, msg);
925 this.warn("Section %s %s", this.section_id(s[".name"]), msg);
929 this.warn("ubus %s (%s) %s", s.type || "rule", s.name, msg);
931 this.warn("ubus %s %s", s.type || "rule", msg);
935 parse_policy: function(val) {
936 return this.parse_enum(val, [
943 parse_bool: function(val) {
944 if (val == "1" || val == "on" || val == "true" || val == "yes")
946 else if (val == "0" || val == "off" || val == "false" || val == "no")
952 parse_family: function(val) {
953 if (val == 'any' || val == 'all' || val == '*')
955 else if (val == 'inet' || index(val, '4') > -1)
957 else if (index(val, '6') > -1)
963 parse_zone_ref: function(val) {
968 return { any: true };
970 for (let zone in this.state.zones) {
971 if (zone.name == val) {
982 parse_device: function(val) {
983 let rv = this.parse_invert(val);
996 parse_direction: function(val) {
997 if (val == 'in' || val == 'ingress')
999 else if (val == 'out' || val == 'egress')
1005 parse_setmatch: function(val) {
1006 let rv = this.parse_invert(val);
1011 rv.val = trim(replace(rv.val, /^[^ \t]+/, function(m) {
1016 let dir = split(rv.val, /[ \t,]/);
1018 for (let i = 0; i < 3 && i < length(dir); i++) {
1019 if (dir[i] == "dst" || dir[i] == "dest") {
1020 rv.dir = rv.dir || [];
1023 else if (dir[i] == "src") {
1024 rv.dir = rv.dir || [];
1029 return length(rv.name) ? rv : null;
1032 parse_cthelper: function(val) {
1033 let rv = this.parse_invert(val);
1038 let helper = filter(this.state.helpers, h => (h.name == rv.val))[0];
1040 return helper ? { ...rv, ...helper } : null;
1043 parse_protocol: function(val) {
1044 let p = this.parse_invert(val);
1066 p.name = 'ipv6-icmp';
1071 { invert: p.invert, name: 'tcp' },
1072 { invert: p.invert, name: 'udp' }
1087 return (p.any || length(p.name)) ? p : null;
1090 parse_mac: function(val) {
1091 let mac = this.parse_invert(val);
1092 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;
1097 mac.mac = sprintf('%02x:%02x:%02x:%02x:%02x:%02x',
1098 hex(m[1]), hex(m[2]), hex(m[3]),
1099 hex(m[4]), hex(m[5]), hex(m[6]));
1104 parse_port: function(val) {
1105 let port = this.parse_invert(val);
1106 let m = port ? match(port.val, /^([0-9]{1,5})([-:]([0-9]{1,5}))?$/i) : null;
1112 let min_port = +m[1];
1113 let max_port = +m[3];
1115 if (min_port > max_port ||
1116 min_port < 0 || max_port < 0 ||
1117 min_port > 65535 || max_port > 65535)
1120 port.min = min_port;
1121 port.max = max_port;
1126 if (pn != pn || pn < 0 || pn > 65535)
1136 parse_network: function(val) {
1137 let rv = this.parse_invert(val);
1142 let nets = this.parse_subnet(rv.val);
1148 rv.addrs = [ ...nets ];
1153 parse_icmptype: function(val) {
1156 if (exists(ipv4_icmptypes, val)) {
1159 rv.type = ipv4_icmptypes[val][0];
1160 rv.code_min = ipv4_icmptypes[val][1];
1161 rv.code_max = ipv4_icmptypes[val][2];
1164 if (exists(ipv6_icmptypes, val)) {
1165 rv.family = rv.family ? 0 : 6;
1167 rv.type6 = ipv6_icmptypes[val][0];
1168 rv.code6_min = ipv6_icmptypes[val][1];
1169 rv.code6_max = ipv6_icmptypes[val][2];
1172 if (!exists(rv, "family")) {
1173 let m = match(val, /^([0-9]+)(\/([0-9]+))?$/);
1180 rv.code_min = +m[3];
1181 rv.code_max = rv.code_min;
1189 if (rv.type > 0xFF || rv.code_min > 0xFF || rv.code_max > 0xFF)
1195 rv.code6_min = rv.code_min;
1196 rv.code6_max = rv.code_max;
1202 parse_invert: function(val) {
1206 let rv = { invert: false };
1208 rv.val = trim(replace(val, /^[ \t]*!/, () => (rv.invert = true, '')));
1210 return length(rv.val) ? rv : null;
1213 parse_limit: function(val) {
1214 let rv = this.parse_invert(val);
1215 let m = rv ? match(rv.val, /^([0-9]+)(\/([a-z]+))?$/) : null;
1221 let u = m[3] ? this.parse_enum(m[3], [ "second", "minute", "hour", "day" ]) : "second";
1232 parse_int: function(val) {
1235 return (n == n) ? n : null;
1238 parse_date: function(val) {
1239 let m = match(val, /^([0-9-]+)T([0-9:]+)$/);
1240 let d = m ? match(m[1], /^([0-9]{1,4})(-([0-9]{1,2})(-([0-9]{1,2}))?)?$/) : null;
1241 let t = this.parse_time(m[2]);
1246 if (d == null || d[1] < 1970 || d[1] > 2038 || d[3] < 1 || d[3] > 12 || d[5] < 1 || d[5] > 31)
1256 hour: t ? +t[1] : 0,
1262 parse_time: function(val) {
1263 let t = match(val, /^([0-9]{1,2})(:([0-9]{1,2})(:([0-9]{1,2}))?)?$/);
1265 if (t == null || t[1] > 23 || t[3] > 59 || t[5] > 59)
1275 parse_weekdays: function(val) {
1276 let rv = this.parse_invert(val);
1281 for (let day in to_array(rv.val)) {
1282 day = this.parse_enum(day, [
1295 rv.days = rv.days || {};
1296 rv.days[day] = true;
1299 rv.days = keys(rv.days);
1301 return rv.days ? rv : null;
1304 parse_monthdays: function(val) {
1305 let rv = this.parse_invert(val);
1310 for (let day in to_array(rv.val)) {
1313 if (day < 1 || day > 31)
1316 rv.days = rv.days || [];
1317 rv.days[day] = true;
1320 return rv.days ? rv : null;
1323 parse_mark: function(val) {
1324 let rv = this.parse_invert(val);
1325 let m = rv ? match(rv.val, /^(0?x?[0-9a-f]+)(\/(0?x?[0-9a-f]+))?$/i) : null;
1332 if (n != n || n > 0xFFFFFFFF)
1336 rv.mask = 0xFFFFFFFF;
1341 if (n != n || n > 0xFFFFFFFF)
1350 parse_dscp: function(val) {
1351 let rv = this.parse_invert(val);
1356 rv.val = uc(rv.val);
1358 if (exists(dscp_classes, rv.val)) {
1359 rv.dscp = dscp_classes[rv.val];
1364 if (n != n || n < 0 || n > 0x3F)
1373 parse_target: function(val) {
1374 return this.parse_enum(val, [
1391 parse_reject_code: function(val) {
1392 return this.parse_enum(val, [
1401 parse_reflection_source: function(val) {
1402 return this.parse_enum(val, [
1408 parse_ipsettype: function(val) {
1409 let m = match(val, /^(src|dst|dest)_(.+)$/);
1410 let t = this.parse_enum(m ? m[2] : val, [
1418 return t ? [ (!m || m[1] == 'src') ? 'src' : 'dst', t ] : null;
1421 parse_ipsetentry: function(val, set) {
1422 let values = split(val, /[ \t]+/);
1424 if (length(values) != length(set.types))
1430 for (let i, t in set.types) {
1433 ip = filter(this.parse_subnet(values[i]), a => (a.family == 4));
1435 switch (length(ip)) {
1436 case 0: return null;
1438 default: this.warn("Set entry '%s' resolves to multiple addresses, using first one", values[i]);
1441 rv[i] = ("net" in set.fw4types) ? ip[0].addr + "/" + ip[0].bits : ip[0].addr;
1445 ip = filter(this.parse_subnet(values[i]), a => (a.family == 6));
1447 switch(length(ip)) {
1448 case 0: return null;
1450 case 2: this.warn("Set entry '%s' resolves to multiple addresses, using first one", values[i]);
1453 rv[i] = ("net" in set.fw4types) ? ip[0].addr + "/" + ip[0].bits : ip[0].addr;
1458 mac = this.parse_mac(values[i]);
1460 if (!mac || mac.invert)
1466 case 'inet_service':
1467 port = this.parse_port(values[i]);
1469 if (!port || port.invert || port.min != port.max)
1480 return length(rv) ? rv : null;
1483 parse_string: function(val) {
1487 parse_opt: function(s, opt, fn, defval, flags) {
1491 if (flags & REQUIRED) {
1492 this.warn_section(s, "option '" + opt + "' is mandatory but not set");
1499 if (type(val) == "array") {
1500 this.warn_section(s, "option '" + opt + "' must not be a list");
1503 else if (val == null) {
1507 let res = this[fn](val);
1510 this.warn_section(s, "option '" + opt + "' specifies invalid value '" + val + "'");
1517 parse_list: function(s, opt, fn, defval, flags) {
1522 if (flags & REQUIRED) {
1523 this.warn_section(s, "option '" + opt + "' is mandatory but not set");
1530 for (val in to_array(val)) {
1531 let res = this[fn](val);
1534 this.warn_section(s, "option '" + opt + "' specifies invalid value '" + val + "'");
1538 if (flags & FLATTEN_LIST)
1539 push(rv, ...to_array(res));
1544 return length(rv) ? rv : null;
1547 quote: function(s, force) {
1548 if (force === true || !match(s, /^([0-9A-Fa-f:.\/-]+)( \. [0-9A-Fa-f:.\/-]+)*$/))
1549 return sprintf('"%s"', replace(s + "", /(["\\])/g, '\\$1'));
1556 return sprintf("%s-%s", a.addr, a.addr2);
1558 if ((a.family == 4 && a.bits == 32) ||
1559 (a.family == 6 && a.bits == 128))
1563 return sprintf("%s/%d", apply_mask(a.addr, a.bits), a.bits);
1565 return sprintf("%s/%s", a.addr, a.mask);
1568 host: function(a, v6brackets) {
1570 ? sprintf("%s-%s", a.addr, a.addr2)
1571 : sprintf((a.family == 6 && v6brackets) ? "[%s]" : "%s", apply_mask(a.addr, a.bits));
1576 return sprintf('%d', p.min);
1578 return sprintf('%d-%d', p.min, p.max);
1581 set: function(v, force) {
1584 v = filter(to_array(v), item => !seen[item]++);
1586 if (force || length(v) != 1)
1587 return sprintf('{ %s }', join(', ', map(v, this.quote)));
1589 return this.quote(v[0]);
1592 concat: function(v) {
1593 return join(' . ', to_array(v));
1596 ipproto: function(family) {
1606 nfproto: function(family, human_readable) {
1609 return human_readable ? "IPv4" : "ipv4";
1612 return human_readable ? "IPv6" : "ipv6";
1615 return human_readable ? "IPv4/IPv6" : null;
1619 datetime: function(stamp) {
1620 return sprintf('"%04d-%02d-%02d %02d:%02d:%02d"',
1621 stamp.year, stamp.month, stamp.day,
1622 stamp.hour, stamp.min, stamp.sec);
1625 date: function(stamp) {
1626 return sprintf('"%04d-%02d-%02d"', stamp.year, stamp.month, stamp.day);
1629 time: function(stamp) {
1630 return sprintf('"%02d:%02d:%02d"', stamp.hour, stamp.min, stamp.sec);
1634 return sprintf('0x%x', n);
1637 is_loopback_dev: function(dev) {
1638 let fd = fs.open(sprintf("/sys/class/net/%s/flags", dev), "r");
1643 let flags = +fd.read("line");
1647 return !!(flags & 0x8);
1650 is_loopback_addr: function(addr) {
1651 return (index(addr, "127.") == 0 || addr == "::1" || addr == "::1/128");
1654 filter_loopback_devs: function(devs, invert) {
1655 return null_if_empty(filter(devs, d => (this.is_loopback_dev(d) == invert)));
1658 filter_loopback_addrs: function(addrs, invert) {
1659 return null_if_empty(filter(addrs, a => (this.is_loopback_addr(a) == invert)));
1663 input_policy: function(reject_as_drop) {
1664 return (!reject_as_drop || this.state.defaults.input != 'reject') ? this.state.defaults.input : 'drop';
1667 output_policy: function(reject_as_drop) {
1668 return (!reject_as_drop || this.state.defaults.output != 'reject') ? this.state.defaults.output : 'drop';
1671 forward_policy: function(reject_as_drop) {
1672 return (!reject_as_drop || this.state.defaults.forward != 'reject') ? this.state.defaults.forward : 'drop';
1675 default_option: function(flag) {
1676 return this.state.defaults[flag];
1679 helpers: function() {
1680 return this.state.helpers;
1684 return this.state.zones;
1687 rules: function(chain) {
1688 return filter(this.state.rules, r => (r.chain == chain));
1691 redirects: function(chain) {
1692 return filter(this.state.redirects, r => (r.chain == chain));
1695 ipsets: function() {
1696 return this.state.ipsets;
1699 parse_setfile: function(set, cb) {
1700 let fd = fs.open(set.loadfile, "r");
1703 warn(sprintf("Unable to load file '%s' for set '%s': %s\n",
1704 set.loadfile, set.name, fs.error()));
1708 let line = null, count = 0;
1710 while ((line = fd.read("line")) !== "") {
1713 if (length(line) == 0 || ord(line) == 35)
1716 let v = this.parse_ipsetentry(line, set);
1719 this.warn("Skipping invalid entry '%s' in file '%s' for set '%s'",
1720 line, set.loadfile, set.name);
1734 print_setentries: function(set) {
1736 let printer = (entry) => {
1738 print("\t\telements = {\n");
1742 print("\t\t\t", join(" . ", entry), ",\n");
1745 map(set.entries, printer);
1748 this.parse_setfile(set, printer);
1754 parse_helper: function(data) {
1755 let helper = this.parse_options(data, {
1756 name: [ "string", null, REQUIRED ],
1757 description: [ "string" ],
1758 module: [ "string" ],
1759 family: [ "family" ],
1760 proto: [ "protocol", null, PARSE_LIST | FLATTEN_LIST | NO_INVERT ],
1761 port: [ "port", null, NO_INVERT ]
1764 if (helper === false) {
1765 this.warn("Helper definition '%s' skipped due to invalid options", data.name || data['.name']);
1768 else if (helper.proto.any) {
1769 this.warn("Helper definition '%s' must not specify wildcard protocol", data.name || data['.name']);
1772 else if (length(helper.proto) > 1) {
1773 this.warn("Helper definition '%s' must not specify multiple protocols", data.name || data['.name']);
1777 helper.available = ((fs.stat("/sys/module/" + helper.module) || {}).type == "directory");
1779 this.state.helpers = this.state.helpers || [];
1780 push(this.state.helpers, helper);
1783 parse_defaults: function(data) {
1784 if (this.state.defaults) {
1785 this.warn_section(data, ": ignoring duplicate defaults section");
1789 let defs = this.parse_options(data, {
1790 input: [ "policy", "drop" ],
1791 output: [ "policy", "drop" ],
1792 forward: [ "policy", "drop" ],
1794 drop_invalid: [ "bool" ],
1795 tcp_reject_code: [ "reject_code", "tcp-reset" ],
1796 any_reject_code: [ "reject_code", "port-unreachable" ],
1798 syn_flood: [ "bool" ],
1799 synflood_protect: [ "bool" ],
1800 synflood_rate: [ "limit", "25/second" ],
1801 synflood_burst: [ "int", "50" ],
1803 tcp_syncookies: [ "bool", "1" ],
1805 tcp_window_scaling: [ "bool", "1" ],
1807 accept_redirects: [ "bool" ],
1808 accept_source_route: [ "bool" ],
1810 auto_helper: [ "bool", "1" ],
1811 custom_chains: [ "bool", null, UNSUPPORTED ],
1812 disable_ipv6: [ "bool", null, UNSUPPORTED ],
1813 flow_offloading: [ "bool", "0" ],
1814 flow_offloading_hw: [ "bool", "0" ]
1817 if (defs.synflood_protect === null)
1818 defs.synflood_protect = defs.syn_flood;
1820 delete defs.syn_flood;
1822 this.state.defaults = defs;
1825 parse_zone: function(data) {
1826 let zone = this.parse_options(data, {
1827 enabled: [ "bool", "1" ],
1829 name: [ "string", null, REQUIRED ],
1830 family: [ "family" ],
1832 network: [ "device", null, PARSE_LIST ],
1833 device: [ "device", null, PARSE_LIST ],
1834 subnet: [ "network", null, PARSE_LIST ],
1836 input: [ "policy", this.state.defaults ? this.state.defaults.input : "drop" ],
1837 output: [ "policy", this.state.defaults ? this.state.defaults.output : "drop" ],
1838 forward: [ "policy", this.state.defaults ? this.state.defaults.forward : "drop" ],
1841 masq_allow_invalid: [ "bool" ],
1842 masq_src: [ "network", null, PARSE_LIST ],
1843 masq_dest: [ "network", null, PARSE_LIST ],
1847 extra: [ "string", null, UNSUPPORTED ],
1848 extra_src: [ "string", null, UNSUPPORTED ],
1849 extra_dest: [ "string", null, UNSUPPORTED ],
1851 mtu_fix: [ "bool" ],
1852 custom_chains: [ "bool", null, UNSUPPORTED ],
1855 log_limit: [ "limit", null, UNSUPPORTED ],
1857 auto_helper: [ "bool", "1" ],
1858 helper: [ "cthelper", null, PARSE_LIST ],
1860 counter: [ "bool", "1" ]
1863 if (zone === false) {
1864 this.warn_section(data, "skipped due to invalid options");
1867 else if (!zone.enabled) {
1868 this.warn_section(data, "is disabled, ignoring section");
1871 else if (zone.helper && !zone.helper.available) {
1872 this.warn_section(data, "uses unavailable ct helper '" + zone.helper.name + "', ignoring section");
1876 if (zone.mtu_fix && this.kernel < 0x040a0000) {
1877 this.warn_section(data, "option 'mtu_fix' requires kernel 4.10 or later");
1881 if (this.state.defaults && this.state.defaults.auto_helper === false)
1882 zone.auto_helper = false;
1884 let match_devices = [];
1885 let related_physdevs = [];
1886 let related_subnets = [];
1887 let related_ubus_networks = [];
1888 let match_subnets, masq_src_subnets, masq_dest_subnets;
1890 for (let name, net in this.state.networks) {
1891 if (net.zone === zone.name)
1892 push(related_ubus_networks, { invert: false, device: name });
1895 for (let e in [ ...to_array(zone.network), ...related_ubus_networks ]) {
1896 if (exists(this.state.networks, e.device)) {
1897 let net = this.state.networks[e.device];
1900 push(match_devices, {
1906 if (net.physdev && !e.invert)
1907 push(related_physdevs, net.physdev);
1909 push(related_subnets, ...(net.ipaddrs || []));
1913 push(match_devices, ...to_array(zone.device));
1915 match_subnets = subnets_split_af(zone.subnet);
1916 masq_src_subnets = subnets_split_af(zone.masq_src);
1917 masq_dest_subnets = subnets_split_af(zone.masq_dest);
1919 push(related_subnets, ...(match_subnets[0] || []), ...(match_subnets[1] || []));
1921 let match_rules = [];
1923 let add_rule = (family, devices, subnets, zone) => {
1928 r.devices_pos = null_if_empty(devices[0]);
1929 r.devices_neg = null_if_empty(devices[1]);
1930 r.devices_neg_wildcard = null_if_empty(devices[2]);
1932 r.subnets_pos = map(subnets[0], this.cidr);
1933 r.subnets_neg = map(subnets[1], this.cidr);
1934 r.subnets_masked = subnets[2];
1936 push(match_rules, r);
1939 let family = infer_family(zone.family, [
1940 zone.helper, "ct helper",
1941 match_subnets, "subnet list"
1944 if (type(family) == "string") {
1945 this.warn_section(data, family + ", skipping");
1949 // group non-inverted device matches into wildcard and non-wildcard ones
1950 let devices = [], plain_devices = [], plain_invert_devices = [], wildcard_invert_devices = [];
1952 for (let device in match_devices) {
1953 let m = match(device.device, /^([^+]*)(\+)?$/);
1956 this.warn_section(data, "skipping invalid wildcard pattern '" + device.device + '"');
1960 // filter `+` (match any device) since nftables does not support
1961 // wildcard only matches
1962 if (!device.invert && m[0] == '+')
1965 // replace inverted `+` (match no device) with invalid pattern
1966 if (device.invert && m[0] == '+') {
1967 device.device = '/never/';
1968 device.invert = false;
1971 // replace "name+" matches with "name*"
1972 else if (m[2] == '+')
1973 device.device = m[1] + '*';
1975 device.wildcard = !!m[2];
1977 if (!device.invert && device.wildcard)
1978 push(devices, [ [ device.device ], plain_invert_devices, wildcard_invert_devices ]);
1979 else if (!device.invert)
1980 push(plain_devices, device.device);
1981 else if (device.wildcard)
1982 push(wildcard_invert_devices, device.device);
1984 push(plain_invert_devices, device.device);
1987 if (length(plain_devices))
1990 plain_invert_devices,
1991 wildcard_invert_devices
1993 else if (!length(devices))
1996 plain_invert_devices,
1997 wildcard_invert_devices
2000 // emit zone jump rules for each device group
2001 if (length(match_devices) || length(match_subnets[0]) || length(match_subnets[1])) {
2002 for (let devgroup in devices) {
2003 // check if there's no AF specific bits, in this case we can do AF agnostic matching
2004 if (!family && !length(match_subnets[0]) && !length(match_subnets[1])) {
2005 add_rule(0, devgroup, [], zone);
2008 // we need to emit one or two AF specific rules
2010 if (family_is_ipv4(zone) && length(match_subnets[0]))
2011 for (let subnets in subnets_group_by_masking(match_subnets[0]))
2012 add_rule(4, devgroup, subnets, zone);
2014 if (family_is_ipv6(zone) && length(match_subnets[1]))
2015 for (let subnets in subnets_group_by_masking(match_subnets[1]))
2016 add_rule(6, devgroup, subnets, zone);
2021 zone.family = family;
2023 zone.match_rules = match_rules;
2025 zone.masq4_src_subnets = subnets_group_by_masking(masq_src_subnets[0]);
2026 zone.masq4_dest_subnets = subnets_group_by_masking(masq_dest_subnets[0]);
2028 zone.masq6_src_subnets = subnets_group_by_masking(masq_src_subnets[1]);
2029 zone.masq6_dest_subnets = subnets_group_by_masking(masq_dest_subnets[1]);
2032 zone.sflags[zone.input] = true;
2035 zone.dflags[zone.output] = true;
2036 zone.dflags[zone.forward] = true;
2038 zone.match_devices = map(filter(match_devices, d => !d.invert), d => d.device);
2039 zone.match_subnets = map(filter(related_subnets, s => !s.invert && s.bits != -1), this.cidr);
2041 zone.related_subnets = related_subnets;
2042 zone.related_physdevs = related_physdevs;
2044 if (zone.masq || zone.masq6)
2045 zone.dflags.snat = true;
2047 if ((zone.auto_helper && !(zone.masq || zone.masq6)) || length(zone.helper)) {
2048 zone.dflags.helper = true;
2050 for (let helper in (length(zone.helper) ? zone.helper : this.state.helpers)) {
2051 if (!helper.available)
2054 for (let proto in helper.proto) {
2055 this.state.rules = this.state.rules || [];
2056 push(this.state.rules, {
2057 chain: "helper_" + zone.name,
2058 family: helper.family,
2059 name: helper.description || helper.name,
2062 dports_pos: [ this.port(helper.port) ],
2070 this.state.zones = this.state.zones || [];
2071 push(this.state.zones, zone);
2074 parse_forwarding: function(data) {
2075 let fwd = this.parse_options(data, {
2076 enabled: [ "bool", "1" ],
2079 family: [ "family" ],
2081 src: [ "zone_ref", null, REQUIRED ],
2082 dest: [ "zone_ref", null, REQUIRED ]
2085 if (fwd === false) {
2086 this.warn_section(data, "skipped due to invalid options");
2089 else if (!fwd.enabled) {
2090 this.warn_section(data, "is disabled, ignoring section");
2094 let add_rule = (family, fwd) => {
2099 proto: { any: true }
2102 f.name = fwd.name || sprintf("Accept %s to %s forwarding",
2103 fwd.src.any ? "any" : fwd.src.zone.name,
2104 fwd.dest.any ? "any" : fwd.dest.zone.name);
2106 f.chain = fwd.src.any ? "forward" : sprintf("forward_%s", fwd.src.zone.name);
2109 f.target = "accept";
2111 f.jump_chain = sprintf("accept_to_%s", fwd.dest.zone.name);
2113 this.state.rules = this.state.rules || [];
2114 push(this.state.rules, f);
2118 let family = fwd.family;
2120 /* inherit family restrictions from related zones */
2121 if (family === 0 || family === null) {
2122 let f1 = fwd.src.zone ? fwd.src.zone.family : 0;
2123 let f2 = fwd.dest.zone ? fwd.dest.zone.family : 0;
2125 if (f1 && f2 && f1 != f2) {
2126 this.warn_section(data,
2127 sprintf("references src %s restricted to %s and dest %s restricted to %s, ignoring forwarding",
2128 fwd.src.zone.name, this.nfproto(f1, true),
2129 fwd.dest.zone.name, this.nfproto(f2, true)));
2134 this.warn_section(data,
2135 sprintf("inheriting %s restriction from src %s",
2136 this.nfproto(f1, true), fwd.src.zone.name));
2141 this.warn_section(data,
2142 sprintf("inheriting %s restriction from dest %s",
2143 this.nfproto(f2, true), fwd.dest.zone.name));
2149 add_rule(family, fwd);
2152 fwd.dest.zone.dflags.accept = true;
2155 parse_rule: function(data) {
2156 let rule = this.parse_options(data, {
2157 enabled: [ "bool", "1" ],
2159 name: [ "string", this.section_id(data[".name"]) ],
2160 _name: [ "string", null, DEPRECATED ],
2161 family: [ "family" ],
2163 src: [ "zone_ref" ],
2164 dest: [ "zone_ref" ],
2166 device: [ "device", null, NO_INVERT ],
2167 direction: [ "direction" ],
2169 ipset: [ "setmatch" ],
2170 helper: [ "cthelper" ],
2171 set_helper: [ "cthelper", null, NO_INVERT ],
2173 proto: [ "protocol", "tcpudp", PARSE_LIST | FLATTEN_LIST ],
2175 src_ip: [ "network", null, PARSE_LIST ],
2176 src_mac: [ "mac", null, PARSE_LIST ],
2177 src_port: [ "port", null, PARSE_LIST ],
2179 dest_ip: [ "network", null, PARSE_LIST ],
2180 dest_port: [ "port", null, PARSE_LIST ],
2182 icmp_type: [ "icmptype", null, PARSE_LIST ],
2183 extra: [ "string", null, UNSUPPORTED ],
2186 limit_burst: [ "int" ],
2188 utc_time: [ "bool" ],
2189 start_date: [ "date" ],
2190 stop_date: [ "date" ],
2191 start_time: [ "time" ],
2192 stop_time: [ "time" ],
2193 weekdays: [ "weekdays" ],
2194 monthdays: [ "monthdays", null, UNSUPPORTED ],
2197 set_mark: [ "mark", null, NO_INVERT ],
2198 set_xmark: [ "mark", null, NO_INVERT ],
2201 set_dscp: [ "dscp", null, NO_INVERT ],
2203 counter: [ "bool", "1" ],
2205 target: [ "target" ]
2208 if (rule === false) {
2209 this.warn_section(data, "skipped due to invalid options");
2212 else if (!rule.enabled) {
2213 this.warn_section(data, "is disabled, ignoring section");
2217 if (rule.target in ["helper", "notrack"] && (!rule.src || !rule.src.zone)) {
2218 this.warn_section(data, "must specify a source zone for target '" + rule.target + "'");
2221 else if (rule.target == "dscp" && !rule.set_dscp) {
2222 this.warn_section(data, "must specify option 'set_dscp' for target 'dscp'");
2225 else if (rule.target == "mark" && !rule.set_mark && !rule.set_xmark) {
2226 this.warn_section(data, "must specify option 'set_mark' or 'set_xmark' for target 'mark'");
2229 else if (rule.target == "helper" && !rule.set_helper) {
2230 this.warn_section(data, "must specify option 'set_helper' for target 'helper'");
2233 else if (rule.device?.any) {
2234 this.warn_section(data, "must not specify '*' as device");
2241 ipset = filter(this.state.ipsets, s => (s.name == rule.ipset.name))[0];
2244 this.warn_section(data, "references unknown set '" + rule.ipset.name + "'");
2248 if (('inet_service' in ipset.types) && !ensure_tcpudp(rule.proto)) {
2249 this.warn_section(data, "references named set with port match but no UDP/TCP protocol, ignoring section");
2254 let need_src_action_chain = (rule) => (rule.src && rule.src.zone && rule.src.zone.log && rule.target != "accept");
2256 let add_rule = (family, proto, saddrs, daddrs, sports, dports, icmptypes, icmpcodes, ipset, rule) => {
2262 has_addrs: !!(saddrs[0] || saddrs[1] || saddrs[2] || daddrs[0] || daddrs[1] || daddrs[2]),
2263 has_ports: !!(length(sports) || length(dports)),
2264 saddrs_pos: map(saddrs[0], this.cidr),
2265 saddrs_neg: map(saddrs[1], this.cidr),
2266 saddrs_masked: saddrs[2],
2267 daddrs_pos: map(daddrs[0], this.cidr),
2268 daddrs_neg: map(daddrs[1], this.cidr),
2269 daddrs_masked: daddrs[2],
2270 sports_pos: map(filter_pos(sports), this.port),
2271 sports_neg: map(filter_neg(sports), this.port),
2272 dports_pos: map(filter_pos(dports), this.port),
2273 dports_neg: map(filter_neg(dports), this.port),
2274 smacs_pos: map(filter_pos(rule.src_mac), m => m.mac),
2275 smacs_neg: map(filter_neg(rule.src_mac), m => m.mac),
2276 icmp_types: map(icmptypes, i => (family == 4 ? i.type : i.type6)),
2277 icmp_codes: map(icmpcodes, ic => sprintf('%d . %d', (family == 4) ? ic.type : ic.type6, (family == 4) ? ic.code_min : ic.code6_min))
2280 if (!length(r.icmp_types))
2281 delete r.icmp_types;
2283 if (!length(r.icmp_codes))
2284 delete r.icmp_codes;
2288 invert: r.set_mark.invert,
2289 mark: r.set_mark.mark,
2290 mask: r.set_mark.mark | r.set_mark.mask
2296 let set_types = map_setmatch(ipset, rule.ipset, proto.name);
2298 if (set_types !== set_types) {
2299 this.warn_section(data, "destination MAC address matching not supported");
2301 } else if (set_types) {
2302 r.ipset = { ...r.ipset, fields: set_types };
2305 if (r.target == "notrack") {
2306 r.chain = sprintf("notrack_%s", r.src.zone.name);
2307 r.src.zone.dflags.notrack = true;
2309 else if (r.target == "helper") {
2310 r.chain = sprintf("helper_%s", r.src.zone.name);
2311 r.src.zone.dflags.helper = true;
2313 else if (r.target == "mark" || r.target == "dscp") {
2314 if ((r.src?.any && r.dest?.any) || (r.src?.zone && r.dest?.zone))
2315 r.chain = "mangle_forward";
2316 else if (r.src?.any && r.dest?.zone)
2317 r.chain = "mangle_postrouting";
2318 else if (r.src?.zone && r.dest?.any)
2319 r.chain = "mangle_prerouting";
2320 else if (r.src && !r.dest)
2321 r.chain = "mangle_input";
2323 r.chain = "mangle_output";
2326 r.src.zone.dflags[r.target] = true;
2327 r.iifnames = null_if_empty(r.src.zone.match_devices);
2331 r.dest.zone.dflags[r.target] = true;
2332 r.oifnames = null_if_empty(r.dest.zone.match_devices);
2340 r.chain = sprintf("%s_%s", r.dest ? "forward" : "input", r.src.zone.name);
2342 r.chain = r.dest ? "forward" : "input";
2345 if (r.dest && !r.src) {
2347 r.chain = sprintf("output_%s", r.dest.zone.name);
2352 if (r.dest && !r.dest.any) {
2353 r.jump_chain = sprintf("%s_to_%s", r.target, r.dest.zone.name);
2354 r.dest.zone.dflags[r.target] = true;
2356 else if (need_src_action_chain(r)) {
2357 r.jump_chain = sprintf("%s_from_%s", r.target, r.src.zone.name);
2358 r.src.zone.dflags[r.target] = true;
2360 else if (r.target == "reject")
2361 r.jump_chain = "handle_reject";
2365 r[r.direction ? "oifnames" : "iifnames"] = [ r.device.device ];
2367 this.state.rules = this.state.rules || [];
2368 push(this.state.rules, r);
2371 for (let proto in rule.proto) {
2372 let sip, dip, sports, dports, itypes4, itypes6;
2373 let family = rule.family;
2375 switch (proto.name) {
2377 itypes4 = filter(rule.icmp_type || [], family_is_ipv4);
2378 itypes6 = filter(rule.icmp_type || [], family_is_ipv6);
2383 itypes6 = filter(rule.icmp_type || [], family_is_ipv6);
2388 sports = rule.src_port;
2389 dports = rule.dest_port;
2393 sip = subnets_split_af(rule.src_ip);
2394 dip = subnets_split_af(rule.dest_ip);
2396 family = infer_family(family, [
2399 dip, "destination IP",
2400 rule.src?.zone, "source zone",
2401 rule.dest?.zone, "destination zone",
2402 rule.helper, "helper match",
2403 rule.set_helper, "helper to set"
2406 if (type(family) == "string") {
2407 this.warn_section(data, family + ", skipping");
2411 let has_ipv4_specifics = (length(sip[0]) || length(dip[0]) || length(itypes4) || rule.dscp !== null);
2412 let has_ipv6_specifics = (length(sip[1]) || length(dip[1]) || length(itypes6) || rule.dscp !== null);
2414 /* if no family was configured, infer target family from IP addresses */
2415 if (family === null) {
2416 if (has_ipv4_specifics && !has_ipv6_specifics)
2418 else if (has_ipv6_specifics && !has_ipv4_specifics)
2424 /* check if there's no AF specific bits, in this case we can do an AF agnostic rule */
2425 if (!family && rule.target != "dscp" && !has_ipv4_specifics && !has_ipv6_specifics) {
2426 add_rule(0, proto, [], [], sports, dports, null, null, null, rule);
2429 /* we need to emit one or two AF specific rules */
2431 if (family == 0 || family == 4) {
2432 let icmp_types = filter(itypes4, i => (i.code_min == 0 && i.code_max == 0xFF));
2433 let icmp_codes = filter(itypes4, i => (i.code_min != 0 || i.code_max != 0xFF));
2435 for (let saddrs in subnets_group_by_masking(sip[0])) {
2436 for (let daddrs in subnets_group_by_masking(dip[0])) {
2437 if (length(icmp_types) || (!length(icmp_types) && !length(icmp_codes)))
2438 add_rule(4, proto, saddrs, daddrs, sports, dports, icmp_types, null, ipset, rule);
2440 if (length(icmp_codes))
2441 add_rule(4, proto, saddrs, daddrs, sports, dports, null, icmp_codes, ipset, rule);
2446 if (family == 0 || family == 6) {
2447 let icmp_types = filter(itypes6, i => (i.code_min == 0 && i.code_max == 0xFF));
2448 let icmp_codes = filter(itypes6, i => (i.code_min != 0 || i.code_max != 0xFF));
2450 for (let saddrs in subnets_group_by_masking(sip[1])) {
2451 for (let daddrs in subnets_group_by_masking(dip[1])) {
2452 if (length(icmp_types) || (!length(icmp_types) && !length(icmp_codes)))
2453 add_rule(6, proto, saddrs, daddrs, sports, dports, icmp_types, null, ipset, rule);
2455 if (length(icmp_codes))
2456 add_rule(6, proto, saddrs, daddrs, sports, dports, null, icmp_codes, ipset, rule);
2464 parse_redirect: function(data) {
2465 let redir = this.parse_options(data, {
2466 enabled: [ "bool", "1" ],
2468 name: [ "string", this.section_id(data[".name"]) ],
2469 _name: [ "string", null, DEPRECATED ],
2470 family: [ "family" ],
2472 src: [ "zone_ref" ],
2473 dest: [ "zone_ref" ],
2475 ipset: [ "setmatch" ],
2476 helper: [ "cthelper", null, NO_INVERT ],
2478 proto: [ "protocol", "tcpudp", PARSE_LIST | FLATTEN_LIST ],
2480 src_ip: [ "network" ],
2481 src_mac: [ "mac", null, PARSE_LIST ],
2482 src_port: [ "port" ],
2484 src_dip: [ "network" ],
2485 src_dport: [ "port" ],
2487 dest_ip: [ "network" ],
2488 dest_port: [ "port" ],
2490 extra: [ "string", null, UNSUPPORTED ],
2493 limit_burst: [ "int" ],
2495 utc_time: [ "bool" ],
2496 start_date: [ "date" ],
2497 stop_date: [ "date" ],
2498 start_time: [ "time" ],
2499 stop_time: [ "time" ],
2500 weekdays: [ "weekdays" ],
2501 monthdays: [ "monthdays", null, UNSUPPORTED ],
2505 reflection: [ "bool", "1" ],
2506 reflection_src: [ "reflection_source", "internal" ],
2508 reflection_zone: [ "zone_ref", null, PARSE_LIST ],
2510 counter: [ "bool", "1" ],
2512 target: [ "target", "dnat" ]
2515 if (redir === false) {
2516 this.warn_section(data, "skipped due to invalid options");
2519 else if (!redir.enabled) {
2520 this.warn_section(data, "is disabled, ignoring section");
2524 if (!(redir.target in ["dnat", "snat"])) {
2525 this.warn_section(data, "has invalid target specified, defaulting to dnat");
2526 redir.target = "dnat";
2532 ipset = filter(this.state.ipsets, s => (s.name == redir.ipset.name))[0];
2535 this.warn_section(data, "references unknown set '" + redir.ipset.name + "'");
2539 if (('inet_service' in ipset.types) && !ensure_tcpudp(redir.proto)) {
2540 this.warn_section(data, "references named set with port match but no UDP/TCP protocol, ignoring section");
2545 let resolve_dest = (redir) => {
2546 for (let zone in this.state.zones) {
2547 for (let zone_addr in zone.related_subnets) {
2548 for (let dest_addr in redir.dest_ip.addrs) {
2549 if (dest_addr.family != zone_addr.family)
2552 let a = apply_mask(dest_addr.addr, zone_addr.mask);
2553 let b = apply_mask(zone_addr.addr, zone_addr.mask);
2571 if (redir.target == "dnat") {
2573 return this.warn_section(data, "has no source specified");
2574 else if (redir.src.any)
2575 return this.warn_section(data, "must not have source '*' for dnat target");
2576 else if (redir.dest_ip && redir.dest_ip.invert)
2577 return this.warn_section(data, "must not specify a negated 'dest_ip' value");
2578 else if (redir.dest_ip && length(filter(redir.dest_ip.addrs, a => a.bits == -1)))
2579 return this.warn_section(data, "must not use non-contiguous masks in 'dest_ip'");
2581 if (!redir.dest && redir.dest_ip && resolve_dest(redir))
2582 this.warn_section(data, "does not specify a destination, assuming '" + redir.dest.zone.name + "'");
2584 if (!redir.dest_port)
2585 redir.dest_port = redir.src_dport;
2587 if (redir.reflection && redir.dest && redir.dest.zone && redir.src.zone.masq) {
2588 redir.dest.zone.dflags.accept = true;
2589 redir.dest.zone.dflags.dnat = true;
2590 redir.dest.zone.dflags.snat = true;
2594 redir.src.zone.dflags.helper = true;
2596 redir.src.zone.dflags[redir.target] = true;
2600 return this.warn_section(data, "has no destination specified");
2601 else if (redir.dest.any)
2602 return this.warn_section(data, "must not have destination '*' for snat target");
2603 else if (!redir.src_dip)
2604 return this.warn_section(data, "has no 'src_dip' option specified");
2605 else if (redir.src_dip.invert)
2606 return this.warn_section(data, "must not specify a negated 'src_dip' value");
2607 else if (length(filter(redir.src_dip.addrs, a => a.bits == -1)))
2608 return this.warn_section(data, "must not use non-contiguous masks in 'src_dip'");
2609 else if (redir.src_mac)
2610 return this.warn_section(data, "must not use 'src_mac' option for snat target");
2611 else if (redir.helper)
2612 return this.warn_section(data, "must not use 'helper' option for snat target");
2614 redir.dest.zone.dflags[redir.target] = true;
2618 let add_rule = (family, proto, saddrs, daddrs, raddrs, sport, dport, rport, ipset, redir) => {
2624 has_addrs: !!(saddrs[0] || saddrs[1] || saddrs[2] || daddrs[0] || daddrs[1] || daddrs[2]),
2625 has_ports: !!(sport || dport || rport),
2626 saddrs_pos: map(saddrs[0], this.cidr),
2627 saddrs_neg: map(saddrs[1], this.cidr),
2628 saddrs_masked: saddrs[2],
2629 daddrs_pos: map(daddrs[0], this.cidr),
2630 daddrs_neg: map(daddrs[1], this.cidr),
2631 daddrs_masked: daddrs[2],
2632 sports_pos: map(filter_pos(to_array(sport)), this.port),
2633 sports_neg: map(filter_neg(to_array(sport)), this.port),
2634 dports_pos: map(filter_pos(to_array(dport)), this.port),
2635 dports_neg: map(filter_neg(to_array(dport)), this.port),
2636 smacs_pos: map(filter_pos(redir.src_mac), m => m.mac),
2637 smacs_neg: map(filter_neg(redir.src_mac), m => m.mac),
2639 raddr: raddrs ? raddrs[0] : null,
2643 let set_types = map_setmatch(ipset, redir.ipset, proto.name);
2645 if (set_types !== set_types) {
2646 this.warn_section(data, "destination MAC address matching not supported");
2648 } else if (set_types) {
2649 r.ipset = { ...r.ipset, fields: set_types };
2654 r.chain = sprintf("dstnat_%s", r.src.zone.name);
2655 r.src.zone.dflags.dnat = true;
2658 r.target = "redirect";
2663 r.chain = sprintf("srcnat_%s", r.dest.zone.name);
2664 r.dest.zone.dflags.snat = true;
2668 this.state.redirects = this.state.redirects || [];
2669 push(this.state.redirects, r);
2672 let to_hostaddr = (a) => {
2673 let bits = (a.family == 4) ? 32 : 128;
2677 addr: apply_mask(a.addr, bits),
2682 for (let proto in redir.proto) {
2683 let sip, dip, rip, iip, eip, refip, sport, dport, rport;
2684 let family = redir.family;
2686 if (proto.name == "ipv6-icmp")
2689 switch (redir.target) {
2691 sip = subnets_split_af(redir.src_ip);
2692 dip = subnets_split_af(redir.src_dip);
2693 rip = subnets_split_af(redir.dest_ip);
2695 switch (proto.name) {
2698 sport = redir.src_port;
2699 dport = redir.src_dport;
2700 rport = redir.dest_port;
2707 sip = subnets_split_af(redir.src_ip);
2708 dip = subnets_split_af(redir.dest_ip);
2709 rip = subnets_split_af(redir.src_dip);
2711 switch (proto.name) {
2714 sport = redir.src_port;
2715 dport = redir.dest_port;
2716 rport = redir.src_dport;
2723 family = infer_family(family, [
2726 dip, "destination IP",
2728 redir.src?.zone, "source zone",
2729 redir.dest?.zone, "destination zone",
2730 redir.helper, "helper match"
2733 if (type(family) == "string") {
2734 this.warn_section(data, family + ", skipping");
2738 /* build reflection rules */
2739 if (redir.target == "dnat" && redir.reflection &&
2740 (length(rip[0]) || length(rip[1])) && redir.src?.zone && redir.dest?.zone) {
2742 name: redir.name + " (reflection)",
2744 helper: redir.helper,
2746 // XXX: this likely makes no sense for reflection rules
2747 //src_mac: redir.src_mac,
2750 limit_burst: redir.limit_burst,
2752 start_date: redir.start_date,
2753 stop_date: redir.stop_date,
2754 start_time: redir.start_time,
2755 stop_time: redir.stop_time,
2756 weekdays: redir.weekdays,
2761 let eaddrs = length(dip) ? dip : subnets_split_af({ addrs: map(redir.src.zone.related_subnets, to_hostaddr) });
2762 let rzones = length(redir.reflection_zone) ? redir.reflection_zone : [ redir.dest ];
2764 for (let rzone in rzones) {
2765 if (!is_family(rzone, family)) {
2766 this.warn_section(data,
2767 sprintf("is restricted to IPv%d but referenced reflection zone is IPv%d only, skipping",
2768 family, rzone.family));
2772 let iaddrs = subnets_split_af({ addrs: rzone.zone.related_subnets });
2773 let refaddrs = (redir.reflection_src == "internal") ? iaddrs : eaddrs;
2775 for (let i = 0; i <= 1; i++) {
2776 if (redir.src.zone[i ? "masq6" : "masq"] && length(rip[i])) {
2777 let snat_addr = refaddrs[i]?.[0];
2779 /* For internal reflection sources try to find a suitable candiate IP
2780 * among the reflection zone subnets which is within the same subnet
2781 * as the original DNAT destination. If we can't find any matching
2782 * one then simply take the first candidate. */
2783 if (redir.reflection_src == "internal") {
2784 for (let zone_addr in rzone.zone.related_subnets) {
2785 if (zone_addr.family != rip[i][0].family)
2788 let r = apply_mask(rip[i][0].addr, zone_addr.mask);
2789 let a = apply_mask(zone_addr.addr, zone_addr.mask);
2794 snat_addr = zone_addr;
2800 this.warn_section(data, (redir.reflection_src || "external") + " rewrite IP cannot be determined, disabling reflection");
2802 else if (!length(iaddrs[i])) {
2803 this.warn_section(data, "internal address range cannot be determined, disabling reflection");
2805 else if (!length(eaddrs[i])) {
2806 this.warn_section(data, "external address range cannot be determined, disabling reflection");
2809 refredir.src = rzone;
2810 refredir.dest = null;
2811 refredir.target = "dnat";
2813 for (let saddrs in subnets_group_by_masking(iaddrs[i]))
2814 for (let daddrs in subnets_group_by_masking(eaddrs[i]))
2815 add_rule(i ? 6 : 4, proto, saddrs, daddrs, rip[i], sport, dport, rport, null, refredir);
2817 refredir.src = null;
2818 refredir.dest = rzone;
2819 refredir.target = "snat";
2821 for (let daddrs in subnets_group_by_masking(rip[i]))
2822 for (let saddrs in subnets_group_by_masking(iaddrs[i]))
2823 add_rule(i ? 6 : 4, proto, saddrs, daddrs, [ to_hostaddr(snat_addr) ], null, rport, null, null, refredir);
2830 if (length(rip[0]) > 1 || length(rip[1]) > 1)
2831 this.warn_section(data, "specifies multiple rewrite addresses, using only first one");
2833 let has_ip4_addr = length(sip[0]) || length(dip[0]) || length(rip[0]),
2834 has_ip6_addr = length(sip[1]) || length(dip[1]) || length(rip[1]),
2835 has_any_addr = has_ip4_addr || has_ip6_addr;
2837 /* check if there's no AF specific bits, in this case we can do an AF agnostic rule */
2838 if (!family && !has_any_addr) {
2839 /* for backwards compatibility, treat unspecified family as IPv4 unless user explicitly requested any (0) */
2843 add_rule(family, proto, [], [], null, sport, dport, rport, null, redir);
2846 /* we need to emit one or two AF specific rules */
2848 if ((!family || family == 4) && (!has_any_addr || has_ip4_addr)) {
2849 for (let saddrs in subnets_group_by_masking(sip[0]))
2850 for (let daddrs in subnets_group_by_masking(dip[0]))
2851 add_rule(4, proto, saddrs, daddrs, rip[0], sport, dport, rport, ipset, redir);
2854 if ((!family || family == 6) && (!has_any_addr || has_ip6_addr)) {
2855 for (let saddrs in subnets_group_by_masking(sip[1]))
2856 for (let daddrs in subnets_group_by_masking(dip[1]))
2857 add_rule(6, proto, saddrs, daddrs, rip[1], sport, dport, rport, ipset, redir);
2863 parse_nat: function(data) {
2864 let snat = this.parse_options(data, {
2865 enabled: [ "bool", "1" ],
2867 name: [ "string", this.section_id(data[".name"]) ],
2868 family: [ "family", "4" ],
2870 src: [ "zone_ref" ],
2871 device: [ "string" ],
2873 ipset: [ "setmatch", null, UNSUPPORTED ],
2875 proto: [ "protocol", "all", PARSE_LIST | FLATTEN_LIST ],
2877 src_ip: [ "network" ],
2878 src_port: [ "port" ],
2880 snat_ip: [ "network", null, NO_INVERT ],
2881 snat_port: [ "port", null, NO_INVERT ],
2883 dest_ip: [ "network" ],
2884 dest_port: [ "port" ],
2886 extra: [ "string", null, UNSUPPORTED ],
2889 limit_burst: [ "int" ],
2891 connlimit_ports: [ "bool" ],
2893 utc_time: [ "bool" ],
2894 start_date: [ "date" ],
2895 stop_date: [ "date" ],
2896 start_time: [ "time" ],
2897 stop_time: [ "time" ],
2898 weekdays: [ "weekdays" ],
2899 monthdays: [ "monthdays", null, UNSUPPORTED ],
2903 target: [ "target", "masquerade" ]
2906 if (snat === false) {
2907 this.warn_section(data, "skipped due to invalid options");
2910 else if (!snat.enabled) {
2911 this.warn_section(data, "is disabled, ignoring section");
2915 if (!(snat.target in ["accept", "snat", "masquerade"])) {
2916 this.warn_section(data, "has invalid target specified, defaulting to masquerade");
2917 snat.target = "masquerade";
2920 if (snat.target == "snat" && !snat.snat_ip && !snat.snat_port) {
2921 this.warn_section(data, "needs either 'snat_ip' or 'snat_port' for target snat, ignoring section");
2924 else if (snat.target != "snat" && snat.snat_ip) {
2925 this.warn_section(data, "must not use 'snat_ip' for non-snat target, ignoring section");
2928 else if (snat.target != "snat" && snat.snat_port) {
2929 this.warn_section(data, "must not use 'snat_port' for non-snat target, ignoring section");
2933 if ((snat.snat_port || snat.src_port || snat.dest_port) && !ensure_tcpudp(snat.proto)) {
2934 this.warn_section(data, "specifies ports but no UDP/TCP protocol, ignoring section");
2938 if (snat.snat_ip && length(filter(snat.snat_ip.addrs, a => a.bits == -1 || a.invert))) {
2939 this.warn_section(data, "must not use inversion or non-contiguous masks in 'snat_ip', ignoring section");
2943 if (snat.src && snat.src.zone)
2944 snat.src.zone.dflags.snat = true;
2946 let add_rule = (family, proto, saddrs, daddrs, raddrs, sport, dport, rport, snat) => {
2952 has_addrs: !!(saddrs[0] || saddrs[1] || saddrs[2] || daddrs[0] || daddrs[1] || daddrs[2]),
2953 has_ports: !!(sport || dport),
2954 saddrs_pos: map(saddrs[0], this.cidr),
2955 saddrs_neg: map(saddrs[1], this.cidr),
2956 saddrs_masked: saddrs[2],
2957 daddrs_pos: map(daddrs[0], this.cidr),
2958 daddrs_neg: map(daddrs[1], this.cidr),
2959 daddrs_masked: daddrs[2],
2960 sports_pos: map(filter_pos(to_array(sport)), this.port),
2961 sports_neg: map(filter_neg(to_array(sport)), this.port),
2962 dports_pos: map(filter_pos(to_array(dport)), this.port),
2963 dports_neg: map(filter_neg(to_array(dport)), this.port),
2965 raddr: raddrs ? raddrs[0] : null,
2968 chain: (snat.src && snat.src.zone) ? sprintf("srcnat_%s", snat.src.zone.name) : "srcnat"
2971 this.state.redirects = this.state.redirects || [];
2972 push(this.state.redirects, n);
2975 for (let proto in snat.proto) {
2976 let sip, dip, rip, sport, dport, rport;
2977 let family = snat.family;
2979 sip = subnets_split_af(snat.src_ip);
2980 dip = subnets_split_af(snat.dest_ip);
2981 rip = subnets_split_af(snat.snat_ip);
2983 switch (proto.name) {
2986 sport = snat.src_port;
2987 dport = snat.dest_port;
2988 rport = snat.snat_port;
2992 if (length(rip[0]) > 1 || length(rip[1]) > 1)
2993 this.warn_section(data, "specifies multiple rewrite addresses, using only first one");
2995 /* inherit family restrictions from related zones */
2996 if (family === 0 || family === null) {
2997 let f = (rule.src && rule.src.zone) ? rule.src.zone.family : 0;
3000 this.warn_section(r,
3001 sprintf("inheriting %s restriction from src %s",
3002 this.nfproto(f1, true), rule.src.zone.name));
3008 /* if no family was configured, infer target family from IP addresses */
3009 if (family === null) {
3010 if ((length(sip[0]) || length(dip[0]) || length(rip[0])) && !length(sip[1]) && !length(dip[1]) && !length(rip[1]))
3012 else if ((length(sip[1]) || length(dip[1]) || length(rip[1])) && !length(sip[0]) && !length(dip[0]) && !length(rip[0]))
3018 /* check if there's no AF specific bits, in this case we can do an AF agnostic rule */
3019 if (!family && !length(sip[0]) && !length(sip[1]) && !length(dip[0]) && !length(dip[1]) && !length(rip[0]) && !length(rip[1])) {
3020 add_rule(0, proto, [], [], null, sport, dport, rport, snat);
3023 /* we need to emit one or two AF specific rules */
3025 if (family == 0 || family == 4)
3026 for (let saddr in subnets_group_by_masking(sip[0]))
3027 for (let daddr in subnets_group_by_masking(dip[0]))
3028 add_rule(4, proto, saddr, daddr, rip[0], sport, dport, rport, snat);
3030 if (family == 0 || family == 6)
3031 for (let saddr in subnets_group_by_masking(sip[1]))
3032 for (let daddr in subnets_group_by_masking(dip[1]))
3033 add_rule(6, proto, saddr, daddr, rip[1], sport, dport, rport, snat);
3038 parse_ipset: function(data) {
3039 let ipset = this.parse_options(data, {
3040 enabled: [ "bool", "1" ],
3041 reload_set: [ "bool" ],
3042 counters: [ "bool" ],
3043 comment: [ "bool" ],
3045 name: [ "string", null, REQUIRED ],
3046 family: [ "family", "4" ],
3048 storage: [ "string", null, UNSUPPORTED ],
3049 match: [ "ipsettype", null, PARSE_LIST ],
3051 iprange: [ "string", null, UNSUPPORTED ],
3052 portrange: [ "string", null, UNSUPPORTED ],
3054 netmask: [ "int", null, UNSUPPORTED ],
3056 hashsize: [ "int", null, UNSUPPORTED ],
3057 timeout: [ "int", "-1" ],
3059 external: [ "string", null, UNSUPPORTED ],
3061 entry: [ "string", null, PARSE_LIST ],
3062 loadfile: [ "string" ]
3065 if (ipset === false) {
3066 this.warn_section(data, "skipped due to invalid options");
3069 else if (!ipset.enabled) {
3070 this.warn_section(data, "is disabled, ignoring section");
3074 if (ipset.family == 0) {
3075 this.warn_section(data, "must not specify family 'any'");
3078 else if (!length(ipset.match)) {
3079 this.warn_section(data, "has no datatypes assigned");
3083 let dirs = map(ipset.match, m => m[0]),
3084 types = map(ipset.match, m => m[1]),
3087 if ("set" in types) {
3088 this.warn_section(data, "match type 'set' is not supported");
3092 if ("net" in types) {
3093 if (this.kernel < 0x05060000) {
3094 this.warn_section(data, "match type 'net' requires kernel 5.6 or later");
3106 types: map(types, (t) => {
3110 return (ipset.family == 4) ? 'ipv4_addr' : 'ipv6_addr';
3113 return 'ether_addr';
3116 return 'inet_service';
3125 s.entries = filter(map(ipset.entry, (e) => {
3126 let v = self.parse_ipsetentry(e, s);
3129 self.warn_section(data, "ignoring invalid ipset entry '" + e + "'");
3132 }), (e) => (e != null));
3134 this.state.ipsets = this.state.ipsets || [];
3135 push(this.state.ipsets, s);