3 let fs = require("fs");
4 let uci = require("uci");
5 let ubus = require("ubus");
7 let STATEFILE = "/var/run/fw4.state";
10 let FLATTEN_LIST = 0x02;
12 let UNSUPPORTED = 0x08;
14 let DEPRECATED = 0x20;
16 let ipv4_icmptypes = {
17 "any": [ 0xFF, 0, 0xFF ],
18 "echo-reply": [ 0, 0, 0xFF ],
19 "pong": [ 0, 0, 0xFF ], /* Alias */
21 "destination-unreachable": [ 3, 0, 0xFF ],
22 "network-unreachable": [ 3, 0, 0 ],
23 "host-unreachable": [ 3, 1, 1 ],
24 "protocol-unreachable": [ 3, 2, 2 ],
25 "port-unreachable": [ 3, 3, 3 ],
26 "fragmentation-needed": [ 3, 4, 4 ],
27 "source-route-failed": [ 3, 5, 5 ],
28 "network-unknown": [ 3, 6, 6 ],
29 "host-unknown": [ 3, 7, 7 ],
30 "network-prohibited": [ 3, 9, 9 ],
31 "host-prohibited": [ 3, 10, 10 ],
32 "TOS-network-unreachable": [ 3, 11, 11 ],
33 "TOS-host-unreachable": [ 3, 12, 12 ],
34 "communication-prohibited": [ 3, 13, 13 ],
35 "host-precedence-violation": [ 3, 14, 14 ],
36 "precedence-cutoff": [ 3, 15, 15 ],
38 "source-quench": [ 4, 0, 0xFF ],
40 "redirect": [ 5, 0, 0xFF ],
41 "network-redirect": [ 5, 0, 0 ],
42 "host-redirect": [ 5, 1, 1 ],
43 "TOS-network-redirect": [ 5, 2, 2 ],
44 "TOS-host-redirect": [ 5, 3, 3 ],
46 "echo-request": [ 8, 0, 0xFF ],
47 "ping": [ 8, 0, 0xFF ], /* Alias */
49 "router-advertisement": [ 9, 0, 0xFF ],
51 "router-solicitation": [ 10, 0, 0xFF ],
53 "time-exceeded": [ 11, 0, 0xFF ],
54 "ttl-exceeded": [ 11, 0, 0xFF ], /* Alias */
55 "ttl-zero-during-transit": [ 11, 0, 0 ],
56 "ttl-zero-during-reassembly": [ 11, 1, 1 ],
58 "parameter-problem": [ 12, 0, 0xFF ],
59 "ip-header-bad": [ 12, 0, 0 ],
60 "required-option-missing": [ 12, 1, 1 ],
62 "timestamp-request": [ 13, 0, 0xFF ],
64 "timestamp-reply": [ 14, 0, 0xFF ],
66 "address-mask-request": [ 17, 0, 0xFF ],
68 "address-mask-reply": [ 18, 0, 0xFF ]
71 let ipv6_icmptypes = {
72 "destination-unreachable": [ 1, 0, 0xFF ],
73 "no-route": [ 1, 0, 0 ],
74 "communication-prohibited": [ 1, 1, 1 ],
75 "address-unreachable": [ 1, 3, 3 ],
76 "port-unreachable": [ 1, 4, 4 ],
78 "packet-too-big": [ 2, 0, 0xFF ],
80 "time-exceeded": [ 3, 0, 0xFF ],
81 "ttl-exceeded": [ 3, 0, 0xFF ], /* Alias */
82 "ttl-zero-during-transit": [ 3, 0, 0 ],
83 "ttl-zero-during-reassembly": [ 3, 1, 1 ],
85 "parameter-problem": [ 4, 0, 0xFF ],
86 "bad-header": [ 4, 0, 0 ],
87 "unknown-header-type": [ 4, 1, 1 ],
88 "unknown-option": [ 4, 2, 2 ],
90 "echo-request": [ 128, 0, 0xFF ],
91 "ping": [ 128, 0, 0xFF ], /* Alias */
93 "echo-reply": [ 129, 0, 0xFF ],
94 "pong": [ 129, 0, 0xFF ], /* Alias */
96 "router-solicitation": [ 133, 0, 0xFF ],
98 "router-advertisement": [ 134, 0, 0xFF ],
100 "neighbour-solicitation": [ 135, 0, 0xFF ],
101 "neighbor-solicitation": [ 135, 0, 0xFF ], /* Alias */
103 "neighbour-advertisement": [ 136, 0, 0xFF ],
104 "neighbor-advertisement": [ 136, 0, 0xFF ], /* Alias */
106 "redirect": [ 137, 0, 0xFF ]
135 function to_mask(bits, v6) {
138 if (bits < 0 || bits > (v6 ? 128 : 32))
141 for (let i = 0; i < (v6 ? 16 : 4); i++) {
142 let b = (bits < 8) ? bits : 8;
143 m[i] = (0xff << (8 - b)) & 0xff;
150 function to_bits(mask) {
151 let a = iptoarr(mask);
158 for (let i = 0, z = false; i < length(a); i++) {
161 while (!z && (a[i] & 0x80)) {
162 a[i] = (a[i] << 1) & 0xff;
173 function apply_mask(addr, mask) {
174 let a = iptoarr(addr);
179 if (type(mask) == "int") {
180 for (let i = 0; i < length(a); i++) {
181 let b = (mask < 8) ? mask : 8;
182 a[i] &= (0xff << (8 - b)) & 0xff;
187 let m = iptoarr(mask);
189 if (!m || length(a) != length(m))
192 for (let i = 0; i < length(a); i++)
199 function to_array(x) {
200 if (type(x) == "array")
206 if (type(x) == "object")
211 return (x == "") ? [] : split(x, /[ \t]+/);
214 function filter_pos(x) {
215 let rv = filter(x, e => !e.invert);
216 return length(rv) ? rv : null;
219 function filter_neg(x) {
220 let rv = filter(x, e => e.invert);
221 return length(rv) ? rv : null;
224 function null_if_empty(x) {
225 return length(x) ? x : null;
228 function subnets_split_af(x) {
231 for (let ag in to_array(x)) {
232 for (let a in filter(ag.addrs, a => (a.family == 4))) {
234 push(rv[0], { ...a, invert: ag.invert });
237 for (let a in filter(ag.addrs, a => (a.family == 6))) {
239 push(rv[1], { ...a, invert: ag.invert });
244 rv.family = (!rv[0] ^ !rv[1]) ? (rv[0] ? 4 : 6) : 0;
249 function subnets_group_by_masking(x) {
250 let groups = [], plain = [], nc = [], invert_plain = [], invert_masked = [];
252 for (let a in to_array(x)) {
253 if (a.bits == -1 && !a.invert)
257 else if (a.bits == -1)
258 push(invert_masked, a);
260 push(invert_plain, a);
264 push(groups, [ null, null_if_empty(invert_plain), [ a, ...invert_masked ] ]);
269 null_if_empty(invert_plain),
270 null_if_empty(invert_masked)
273 else if (!length(groups)) {
276 null_if_empty(invert_plain),
277 null_if_empty(invert_masked)
284 function ensure_tcpudp(x) {
285 if (length(filter(x, p => (p.name == "tcp" || p.name == "udp"))))
288 let rest = filter(x, p => !p.any),
289 any = filter(x, p => p.any);
291 if (length(any) && !length(rest)) {
293 push(x, { name: "tcp" }, { name: "udp" });
300 let is_family = (x, v) => (!x.family || x.family == v);
301 let family_is_ipv4 = (x) => (!x.family || x.family == 4);
302 let family_is_ipv6 = (x) => (!x.family || x.family == 6);
304 function infer_family(f, objects) {
308 for (let i = 0; i < length(objects); i += 2) {
309 let objs = to_array(objects[i]),
310 desc = objects[i + 1];
312 for (let obj in objs) {
313 if (!obj || !obj.family || obj.family == res)
323 ? sprintf('references IPv%d only %s but is restricted to IPv%d by %s', obj.family, desc, res, by)
324 : sprintf('is restricted to IPv%d but referenced %s is IPv%d only', res, desc, obj.family);
331 function map_setmatch(set, match, proto) {
332 if (!set || (('inet_service' in set.types) && proto != 'tcp' && proto != 'udp'))
337 for (let i, t in set.types) {
338 let dir = (((match.dir && match.dir[i]) || set.directions[i] || 'src') == 'src' ? 's' : 'd');
342 fields[i] = sprintf('ip %saddr', dir);
346 fields[i] = sprintf('ip6 %saddr', dir);
353 fields[i] = 'ether saddr';
357 fields[i] = sprintf('%s %sport', proto, dir);
365 function resolve_lower_devices(devstatus, devname) {
366 let dir = fs.opendir("/sys/class/net/" + devname);
370 if (!devstatus || devstatus[devname]?.["hw-tc-offload"]) {
376 while ((e = dir.read()) != null)
377 if (index(e, "lower_") === 0)
378 push(devs, ...resolve_lower_devices(devstatus, substr(e, 6)));
387 function nft_json_command(...args) {
388 let cmd = [ "/usr/sbin/nft", "--terse", "--json", ...args ];
389 let nft = fs.popen(join(" ", cmd), "r");
394 info = filter(json(nft.read("all"))?.nftables,
395 item => (type(item) == "object" && !item.metainfo));
398 warn(sprintf("Unable to parse nftables JSON output: %s\n", e));
404 warn(sprintf("Unable to popen() %s: %s\n", cmd, fs.error()));
410 function nft_try_hw_offload(devices) {
412 'add table inet fw4-hw-offload-test; ' +
413 'add flowtable inet fw4-hw-offload-test ft { ' +
414 'hook ingress priority 0; ' +
415 'devices = { "' + join('", "', devices) + '" }; ' +
419 let rc = system(sprintf("/usr/sbin/nft -c '%s' 2>/dev/null", replace(nft_test, "'", "'\\''")));
426 read_kernel_version: function() {
427 let fd = fs.open("/proc/version", "r"),
431 let m = match(fd.read("line"), /^Linux version ([0-9]+)\.([0-9]+)\.([0-9]+)/);
433 v = m ? (+m[1] << 24) | (+m[2] << 16) | (+m[3] << 8) : 0;
440 resolve_offload_devices: function() {
441 if (!this.default_option("flow_offloading"))
444 let devstatus = null;
447 if (this.default_option("flow_offloading_hw")) {
448 let bus = ubus.connect();
451 devstatus = bus.call("network.device", "status") || {};
455 for (let zone in this.zones())
456 for (let device in zone.related_physdevs)
457 push(devices, ...resolve_lower_devices(devstatus, device));
459 devices = uniq(devices);
461 if (nft_try_hw_offload(devices))
464 this.warn('Hardware flow offloading unavailable, falling back to software offloading');
465 this.state.defaults.flow_offloading_hw = false;
470 for (let zone in this.zones())
471 for (let device in zone.match_devices)
472 push(devices, ...resolve_lower_devices(null, device));
474 return uniq(devices);
477 check_set_types: function() {
480 for (let item in nft_json_command("list", "sets", "inet"))
481 if (item.set?.table == "fw4")
482 sets[item.set.name] = (type(item.set.type) == "array") ? item.set.type : [ item.set.type ];
487 check_flowtable: function() {
488 for (let item in nft_json_command("list", "flowtables", "inet"))
489 if (item.flowtable?.table == "fw4" && item.flowtable?.name == "ft")
495 read_state: function() {
496 let fd = fs.open(STATEFILE, "r");
501 state = json(fd.read("all"));
504 warn(sprintf("Unable to parse '%s': %s\n", STATEFILE, e));
513 read_ubus: function() {
516 rules = [], networks = {},
517 bus = ubus.connect();
520 ifaces = bus.call("network.interface", "dump");
521 services = bus.call("service", "get_data", { "type": "firewall" });
526 warn(sprintf("Unable to connect to ubus: %s\n", ubus.error()));
531 // Gather logical network information from ubus
534 if (type(ifaces) == "object" && type(ifaces.interface) == "array") {
535 for (let ifc in ifaces.interface) {
538 device: ifc.l3_device,
543 if (type(ifc["ipv4-address"]) == "array") {
544 for (let addr in ifc["ipv4-address"]) {
545 net.ipaddrs = net.ipaddrs || [];
549 mask: to_mask(addr.mask, false),
555 if (type(ifc["ipv6-address"]) == "array") {
556 for (let addr in ifc["ipv6-address"]) {
557 net.ipaddrs = net.ipaddrs || [];
561 mask: to_mask(addr.mask, true),
567 if (type(ifc["ipv6-prefix-assignment"]) == "array") {
568 for (let addr in ifc["ipv6-prefix-assignment"]) {
569 if (addr["local-address"]) {
570 net.ipaddrs = net.ipaddrs || [];
573 addr: addr["local-address"].address,
574 mask: to_mask(addr["local-address"].mask, true),
575 bits: addr["local-address"].mask
581 if (type(ifc.data) == "object" && type(ifc.data.firewall) == "array") {
584 for (let rulespec in ifc.data.firewall) {
588 name: (rulespec.type != 'ipset') ? sprintf('ubus:%s[%s] %s %d', ifc.interface, ifc.proto, rulespec.type || 'rule', n) : rulespec.name,
589 device: rulespec.device || ifc.l3_device
596 networks[ifc.interface] = net;
602 // Gather firewall rule definitions from ubus services
605 if (type(services) == "object") {
606 for (let svcname, service in services) {
607 if (type(service) == "object" && type(service.firewall) == "array") {
610 for (let rulespec in services[svcname].firewall) {
614 name: (rulespec.type != 'ipset') ? sprintf('ubus:%s %s %d', svcname, rulespec.type || 'rule', n) : rulespec.name
621 for (let svcinst, instance in service) {
622 if (type(instance) == "object" && type(instance.firewall) == "array") {
625 for (let rulespec in instance.firewall) {
629 name: (rulespec.type != 'ipset') ? sprintf('ubus:%s[%s] %s %d', svcname, svcinst, rulespec.type || 'rule', n) : rulespec.name
645 load: function(use_statefile) {
648 this.state = use_statefile ? this.read_state() : null;
650 this.cursor = uci.cursor();
651 this.cursor.load("firewall");
652 this.cursor.load("/usr/share/firewall4/helpers");
655 this.state = this.read_ubus();
657 this.kernel = this.read_kernel_version();
661 // Read helper mapping
664 this.cursor.foreach("helpers", "helper", h => self.parse_helper(h));
668 // Read default policies
671 this.cursor.foreach("firewall", "defaults", d => self.parse_defaults(d));
673 if (!this.state.defaults)
674 this.parse_defaults({});
678 // Build list of ipsets
681 if (!this.state.ipsets) {
682 map(filter(this.state.ubus_rules, n => (n.type == "ipset")), s => self.parse_ipset(s));
683 this.cursor.foreach("firewall", "ipset", s => self.parse_ipset(s));
688 // Build list of logical zones
691 if (!this.state.zones)
692 this.cursor.foreach("firewall", "zone", z => self.parse_zone(z));
696 // Build list of rules
699 map(filter(this.state.ubus_rules, r => (r.type == "rule")), r => self.parse_rule(r));
700 this.cursor.foreach("firewall", "rule", r => self.parse_rule(r));
704 // Build list of forwardings
707 this.cursor.foreach("firewall", "forwarding", f => self.parse_forwarding(f));
711 // Build list of redirects
714 map(filter(this.state.ubus_rules, r => (r.type == "redirect")), r => self.parse_redirect(r));
715 this.cursor.foreach("firewall", "redirect", r => self.parse_redirect(r));
719 // Build list of snats
722 map(filter(this.state.ubus_rules, n => (n.type == "nat")), n => self.parse_nat(n));
723 this.cursor.foreach("firewall", "nat", n => self.parse_nat(n));
727 let fd = fs.open(STATEFILE, "w");
731 zones: this.state.zones,
732 ipsets: this.state.ipsets,
733 networks: this.state.networks,
734 ubus_rules: this.state.ubus_rules
740 warn("Unable to write '%s': %s\n", STATEFILE, fs.error());
745 warn: function(fmt, ...args) {
749 let msg = sprintf(fmt, ...args);
752 warn("\033[33m", msg, "\033[m\n");
754 warn("[!] ", msg, "\n");
757 get: function(sid, opt) {
758 return this.cursor.get("firewall", sid, opt);
761 get_all: function(sid) {
762 return this.cursor.get_all("firewall", sid);
765 parse_options: function(s, spec) {
768 for (let key, val in spec) {
769 let datatype = "parse_" + val[0],
772 parsefn = (flags & PARSE_LIST) ? "parse_list" : "parse_opt";
774 let res = this[parsefn](s, key, datatype, defval, flags);
779 if (type(res) == "object" && res.invert && (flags & NO_INVERT)) {
780 this.warn_section(s, "option '" + key + '" must not be negated');
785 if (flags & DEPRECATED)
786 this.warn_section(s, "option '" + key + "' is deprecated by fw4");
787 else if (flags & UNSUPPORTED)
788 this.warn_section(s, "option '" + key + "' is not supported by fw4");
795 if (index(opt, '.') != 0 && opt != 'type' && !exists(spec, opt)) {
796 this.warn_section(s, "specifies unknown option '" + opt + "'");
803 parse_subnet: function(subnet) {
804 let parts = split(subnet, "/");
807 switch (length(parts)) {
809 a = iptoarr(parts[0]);
810 m = iptoarr(parts[1]);
816 if (length(a) != length(m))
819 b = to_bits(parts[1]);
821 /* allow non-contiguous masks such as `::ffff:ffff:ffff:ffff` */
834 if (type(b) != "int")
837 m = to_mask(b, length(a) == 16);
841 family: (length(a) == 16) ? 6 : 4,
848 parts = split(parts[0], "-");
850 switch (length(parts)) {
852 a = iptoarr(parts[0]);
853 b = iptoarr(parts[1]);
855 if (a && b && length(a) == length(b)) {
857 family: (length(a) == 16) ? 6 : 4,
867 a = iptoarr(parts[0]);
871 family: (length(a) == 16) ? 6 : 4,
873 mask: to_mask(length(a) * 8, length(a) == 16),
878 n = this.state.networks[parts[0]];
881 return [ ...(n.ipaddrs || []) ];
888 parse_enum: function(val, choices) {
889 if (type(val) == "string") {
892 for (let i = 0; i < length(choices); i++)
893 if (substr(choices[i], 0, length(val)) == val)
900 section_id: function(sid) {
901 let s = this.get_all(sid);
906 if (s[".anonymous"]) {
909 this.cursor.foreach("firewall", s[".type"], function(ss) {
910 if (ss[".name"] == s[".name"])
916 return sprintf("@%s[%d]", s[".type"], c);
922 warn_section: function(s, msg) {
925 this.warn("Section %s (%s) %s", this.section_id(s[".name"]), s.name, msg);
927 this.warn("Section %s %s", this.section_id(s[".name"]), msg);
931 this.warn("ubus %s (%s) %s", s.type || "rule", s.name, msg);
933 this.warn("ubus %s %s", s.type || "rule", msg);
937 parse_policy: function(val) {
938 return this.parse_enum(val, [
945 parse_bool: function(val) {
946 if (val == "1" || val == "on" || val == "true" || val == "yes")
948 else if (val == "0" || val == "off" || val == "false" || val == "no")
954 parse_family: function(val) {
955 if (val == 'any' || val == 'all' || val == '*')
957 else if (val == 'inet' || index(val, '4') > -1)
959 else if (index(val, '6') > -1)
965 parse_zone_ref: function(val) {
970 return { any: true };
972 for (let zone in this.state.zones) {
973 if (zone.name == val) {
984 parse_device: function(val) {
985 let rv = this.parse_invert(val);
998 parse_direction: function(val) {
999 if (val == 'in' || val == 'ingress')
1001 else if (val == 'out' || val == 'egress')
1007 parse_setmatch: function(val) {
1008 let rv = this.parse_invert(val);
1013 rv.val = trim(replace(rv.val, /^[^ \t]+/, function(m) {
1018 let dir = split(rv.val, /[ \t,]/);
1020 for (let i = 0; i < 3 && i < length(dir); i++) {
1021 if (dir[i] == "dst" || dir[i] == "dest") {
1022 rv.dir = rv.dir || [];
1025 else if (dir[i] == "src") {
1026 rv.dir = rv.dir || [];
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 m = match(val, /^([0-9-]+)T([0-9:]+)$/);
1242 let d = m ? match(m[1], /^([0-9]{1,4})(-([0-9]{1,2})(-([0-9]{1,2}))?)?$/) : null;
1243 let t = this.parse_time(m[2]);
1248 if (d == null || d[1] < 1970 || d[1] > 2038 || d[3] < 1 || d[3] > 12 || d[5] < 1 || d[5] > 31)
1258 hour: t ? +t[1] : 0,
1264 parse_time: function(val) {
1265 let t = match(val, /^([0-9]{1,2})(:([0-9]{1,2})(:([0-9]{1,2}))?)?$/);
1267 if (t == null || t[1] > 23 || t[3] > 59 || t[5] > 59)
1277 parse_weekdays: function(val) {
1278 let rv = this.parse_invert(val);
1283 for (let day in to_array(rv.val)) {
1284 day = this.parse_enum(day, [
1297 rv.days = rv.days || {};
1298 rv.days[day] = true;
1301 rv.days = keys(rv.days);
1303 return rv.days ? rv : null;
1306 parse_monthdays: function(val) {
1307 let rv = this.parse_invert(val);
1312 for (let day in to_array(rv.val)) {
1315 if (day < 1 || day > 31)
1318 rv.days = rv.days || [];
1319 rv.days[day] = true;
1322 return rv.days ? rv : null;
1325 parse_mark: function(val) {
1326 let rv = this.parse_invert(val);
1327 let m = rv ? match(rv.val, /^(0?x?[0-9a-f]+)(\/(0?x?[0-9a-f]+))?$/i) : null;
1334 if (n != n || n > 0xFFFFFFFF)
1338 rv.mask = 0xFFFFFFFF;
1343 if (n != n || n > 0xFFFFFFFF)
1352 parse_dscp: function(val) {
1353 let rv = this.parse_invert(val);
1358 rv.val = uc(rv.val);
1360 if (exists(dscp_classes, rv.val)) {
1361 rv.dscp = dscp_classes[rv.val];
1366 if (n != n || n < 0 || n > 0x3F)
1375 parse_target: function(val) {
1376 return this.parse_enum(val, [
1393 parse_reject_code: function(val) {
1394 return this.parse_enum(val, [
1403 parse_reflection_source: function(val) {
1404 return this.parse_enum(val, [
1410 parse_ipsettype: function(val) {
1411 let m = match(val, /^(src|dst|dest)_(.+)$/);
1412 let t = this.parse_enum(m ? m[2] : val, [
1420 return t ? [ (!m || m[1] == 'src') ? 'src' : 'dst', t ] : null;
1423 parse_ipsetentry: function(val, set) {
1424 let values = split(val, /[ \t]+/);
1426 if (length(values) != length(set.types))
1432 for (let i, t in set.types) {
1435 ip = filter(this.parse_subnet(values[i]), a => (a.family == 4));
1437 switch (length(ip)) {
1438 case 0: return null;
1440 default: this.warn("Set entry '%s' resolves to multiple addresses, using first one", values[i]);
1443 rv[i] = ("net" in set.fw4types) ? ip[0].addr + "/" + ip[0].bits : ip[0].addr;
1447 ip = filter(this.parse_subnet(values[i]), a => (a.family == 6));
1449 switch(length(ip)) {
1450 case 0: return null;
1452 case 2: this.warn("Set entry '%s' resolves to multiple addresses, using first one", values[i]);
1455 rv[i] = ("net" in set.fw4types) ? ip[0].addr + "/" + ip[0].bits : ip[0].addr;
1460 mac = this.parse_mac(values[i]);
1462 if (!mac || mac.invert)
1468 case 'inet_service':
1469 port = this.parse_port(values[i]);
1471 if (!port || port.invert || port.min != port.max)
1482 return length(rv) ? rv : null;
1485 parse_string: function(val) {
1489 parse_opt: function(s, opt, fn, defval, flags) {
1493 if (flags & REQUIRED) {
1494 this.warn_section(s, "option '" + opt + "' is mandatory but not set");
1501 if (type(val) == "array") {
1502 this.warn_section(s, "option '" + opt + "' must not be a list");
1505 else if (val == null) {
1509 let res = this[fn](val);
1512 this.warn_section(s, "option '" + opt + "' specifies invalid value '" + val + "'");
1519 parse_list: function(s, opt, fn, defval, flags) {
1524 if (flags & REQUIRED) {
1525 this.warn_section(s, "option '" + opt + "' is mandatory but not set");
1532 for (val in to_array(val)) {
1533 let res = this[fn](val);
1536 this.warn_section(s, "option '" + opt + "' specifies invalid value '" + val + "'");
1540 if (flags & FLATTEN_LIST)
1541 push(rv, ...to_array(res));
1546 return length(rv) ? rv : null;
1549 quote: function(s, force) {
1550 if (force === true || !match(s, /^([0-9A-Fa-f:.\/-]+)( \. [0-9A-Fa-f:.\/-]+)*$/))
1551 return sprintf('"%s"', replace(s + "", /(["\\])/g, '\\$1'));
1558 return sprintf("%s-%s", a.addr, a.addr2);
1560 if ((a.family == 4 && a.bits == 32) ||
1561 (a.family == 6 && a.bits == 128))
1565 return sprintf("%s/%d", apply_mask(a.addr, a.bits), a.bits);
1567 return sprintf("%s/%s", a.addr, a.mask);
1572 ? sprintf("%s-%s", a.addr, a.addr2)
1573 : apply_mask(a.addr, a.bits);
1578 return sprintf('%d', p.min);
1580 return sprintf('%d-%d', p.min, p.max);
1583 set: function(v, force) {
1586 v = filter(to_array(v), item => !seen[item]++);
1588 if (force || length(v) != 1)
1589 return sprintf('{ %s }', join(', ', map(v, this.quote)));
1591 return this.quote(v[0]);
1594 concat: function(v) {
1595 return join(' . ', to_array(v));
1598 ipproto: function(family) {
1608 nfproto: function(family, human_readable) {
1611 return human_readable ? "IPv4" : "ipv4";
1614 return human_readable ? "IPv6" : "ipv6";
1617 return human_readable ? "IPv4/IPv6" : null;
1621 datetime: function(stamp) {
1622 return sprintf('"%04d-%02d-%02d %02d:%02d:%02d"',
1623 stamp.year, stamp.month, stamp.day,
1624 stamp.hour, stamp.min, stamp.sec);
1627 date: function(stamp) {
1628 return sprintf('"%04d-%02d-%02d"', stamp.year, stamp.month, stamp.day);
1631 time: function(stamp) {
1632 return sprintf('"%02d:%02d:%02d"', stamp.hour, stamp.min, stamp.sec);
1636 return sprintf('0x%x', n);
1639 is_loopback_dev: function(dev) {
1640 let fd = fs.open(sprintf("/sys/class/net/%s/flags", dev), "r");
1645 let flags = +fd.read("line");
1649 return !!(flags & 0x8);
1652 is_loopback_addr: function(addr) {
1653 return (index(addr, "127.") == 0 || addr == "::1" || addr == "::1/128");
1656 filter_loopback_devs: function(devs, invert) {
1657 return null_if_empty(filter(devs, d => (this.is_loopback_dev(d) == invert)));
1660 filter_loopback_addrs: function(addrs, invert) {
1661 return null_if_empty(filter(addrs, a => (this.is_loopback_addr(a) == invert)));
1665 input_policy: function(reject_as_drop) {
1666 return (!reject_as_drop || this.state.defaults.input != 'reject') ? this.state.defaults.input : 'drop';
1669 output_policy: function(reject_as_drop) {
1670 return (!reject_as_drop || this.state.defaults.output != 'reject') ? this.state.defaults.output : 'drop';
1673 forward_policy: function(reject_as_drop) {
1674 return (!reject_as_drop || this.state.defaults.forward != 'reject') ? this.state.defaults.forward : 'drop';
1677 default_option: function(flag) {
1678 return this.state.defaults[flag];
1681 helpers: function() {
1682 return this.state.helpers;
1686 return this.state.zones;
1689 rules: function(chain) {
1690 return filter(this.state.rules, r => (r.chain == chain));
1693 redirects: function(chain) {
1694 return filter(this.state.redirects, r => (r.chain == chain));
1697 ipsets: function() {
1698 return this.state.ipsets;
1701 parse_setfile: function(set, cb) {
1702 let fd = fs.open(set.loadfile, "r");
1705 warn(sprintf("Unable to load file '%s' for set '%s': %s\n",
1706 set.loadfile, set.name, fs.error()));
1710 let line = null, count = 0;
1712 while ((line = fd.read("line")) !== "") {
1715 if (length(line) == 0 || ord(line) == 35)
1718 let v = this.parse_ipsetentry(line, set);
1721 this.warn("Skipping invalid entry '%s' in file '%s' for set '%s'",
1722 line, set.loadfile, set.name);
1736 print_setentries: function(set) {
1738 let printer = (entry) => {
1740 print("\t\telements = {\n");
1744 print("\t\t\t", join(" . ", entry), ",\n");
1747 map(set.entries, printer);
1750 this.parse_setfile(set, printer);
1756 parse_helper: function(data) {
1757 let helper = this.parse_options(data, {
1758 name: [ "string", null, REQUIRED ],
1759 description: [ "string" ],
1760 module: [ "string" ],
1761 family: [ "family" ],
1762 proto: [ "protocol", null, PARSE_LIST | FLATTEN_LIST | NO_INVERT ],
1763 port: [ "port", null, NO_INVERT ]
1766 if (helper === false) {
1767 this.warn("Helper definition '%s' skipped due to invalid options", data.name || data['.name']);
1770 else if (helper.proto.any) {
1771 this.warn("Helper definition '%s' must not specify wildcard protocol", data.name || data['.name']);
1774 else if (length(helper.proto) > 1) {
1775 this.warn("Helper definition '%s' must not specify multiple protocols", data.name || data['.name']);
1779 helper.available = ((fs.stat("/sys/module/" + helper.module) || {}).type == "directory");
1781 this.state.helpers = this.state.helpers || [];
1782 push(this.state.helpers, helper);
1785 parse_defaults: function(data) {
1786 if (this.state.defaults) {
1787 this.warn_section(data, ": ignoring duplicate defaults section");
1791 let defs = this.parse_options(data, {
1792 input: [ "policy", "drop" ],
1793 output: [ "policy", "drop" ],
1794 forward: [ "policy", "drop" ],
1796 drop_invalid: [ "bool" ],
1797 tcp_reject_code: [ "reject_code", "tcp-reset" ],
1798 any_reject_code: [ "reject_code", "port-unreachable" ],
1800 syn_flood: [ "bool" ],
1801 synflood_protect: [ "bool" ],
1802 synflood_rate: [ "limit", "25/second" ],
1803 synflood_burst: [ "int", "50" ],
1805 tcp_syncookies: [ "bool", "1" ],
1807 tcp_window_scaling: [ "bool", "1" ],
1809 accept_redirects: [ "bool" ],
1810 accept_source_route: [ "bool" ],
1812 auto_helper: [ "bool", "1" ],
1813 custom_chains: [ "bool", null, UNSUPPORTED ],
1814 disable_ipv6: [ "bool", null, UNSUPPORTED ],
1815 flow_offloading: [ "bool", "0" ],
1816 flow_offloading_hw: [ "bool", "0" ]
1819 if (defs.synflood_protect === null)
1820 defs.synflood_protect = defs.syn_flood;
1822 delete defs.syn_flood;
1824 this.state.defaults = defs;
1827 parse_zone: function(data) {
1828 let zone = this.parse_options(data, {
1829 enabled: [ "bool", "1" ],
1831 name: [ "string", null, REQUIRED ],
1832 family: [ "family" ],
1834 network: [ "device", null, PARSE_LIST ],
1835 device: [ "device", null, PARSE_LIST ],
1836 subnet: [ "network", null, PARSE_LIST ],
1838 input: [ "policy", this.state.defaults ? this.state.defaults.input : "drop" ],
1839 output: [ "policy", this.state.defaults ? this.state.defaults.output : "drop" ],
1840 forward: [ "policy", this.state.defaults ? this.state.defaults.forward : "drop" ],
1843 masq_allow_invalid: [ "bool" ],
1844 masq_src: [ "network", null, PARSE_LIST ],
1845 masq_dest: [ "network", null, PARSE_LIST ],
1849 extra: [ "string", null, UNSUPPORTED ],
1850 extra_src: [ "string", null, UNSUPPORTED ],
1851 extra_dest: [ "string", null, UNSUPPORTED ],
1853 mtu_fix: [ "bool" ],
1854 custom_chains: [ "bool", null, UNSUPPORTED ],
1857 log_limit: [ "limit", null, UNSUPPORTED ],
1859 auto_helper: [ "bool", "1" ],
1860 helper: [ "cthelper", null, PARSE_LIST ],
1862 counter: [ "bool", "1" ]
1865 if (zone === false) {
1866 this.warn_section(data, "skipped due to invalid options");
1869 else if (!zone.enabled) {
1870 this.warn_section(data, "is disabled, ignoring section");
1873 else if (zone.helper && !zone.helper.available) {
1874 this.warn_section(data, "uses unavailable ct helper '" + zone.helper.name + "', ignoring section");
1878 if (zone.mtu_fix && this.kernel < 0x040a0000) {
1879 this.warn_section(data, "option 'mtu_fix' requires kernel 4.10 or later");
1883 if (this.state.defaults && this.state.defaults.auto_helper === false)
1884 zone.auto_helper = false;
1886 let match_devices = [];
1887 let related_physdevs = [];
1888 let related_subnets = [];
1889 let related_ubus_networks = [];
1890 let match_subnets, masq_src_subnets, masq_dest_subnets;
1892 for (let name, net in this.state.networks) {
1893 if (net.zone === zone.name)
1894 push(related_ubus_networks, { invert: false, device: name });
1897 for (let e in [ ...to_array(zone.network), ...related_ubus_networks ]) {
1898 if (exists(this.state.networks, e.device)) {
1899 let net = this.state.networks[e.device];
1902 push(match_devices, {
1908 if (net.physdev && !e.invert)
1909 push(related_physdevs, net.physdev);
1911 push(related_subnets, ...(net.ipaddrs || []));
1915 push(match_devices, ...to_array(zone.device));
1917 match_subnets = subnets_split_af(zone.subnet);
1918 masq_src_subnets = subnets_split_af(zone.masq_src);
1919 masq_dest_subnets = subnets_split_af(zone.masq_dest);
1921 push(related_subnets, ...(match_subnets[0] || []), ...(match_subnets[1] || []));
1923 let match_rules = [];
1925 let add_rule = (family, devices, subnets, zone) => {
1930 r.devices_pos = null_if_empty(devices[0]);
1931 r.devices_neg = null_if_empty(devices[1]);
1932 r.devices_neg_wildcard = null_if_empty(devices[2]);
1934 r.subnets_pos = map(subnets[0], this.cidr);
1935 r.subnets_neg = map(subnets[1], this.cidr);
1936 r.subnets_masked = subnets[2];
1938 push(match_rules, r);
1941 let family = infer_family(zone.family, [
1942 zone.helper, "ct helper",
1943 match_subnets, "subnet list"
1946 if (type(family) == "string") {
1947 this.warn_section(data, family + ", skipping");
1951 // group non-inverted device matches into wildcard and non-wildcard ones
1952 let devices = [], plain_devices = [], plain_invert_devices = [], wildcard_invert_devices = [];
1954 for (let device in match_devices) {
1955 let m = match(device.device, /^([^+]*)(\+)?$/);
1958 this.warn_section(data, "skipping invalid wildcard pattern '" + device.device + '"');
1962 // filter `+` (match any device) since nftables does not support
1963 // wildcard only matches
1964 if (!device.invert && m[0] == '+')
1967 // replace inverted `+` (match no device) with invalid pattern
1968 if (device.invert && m[0] == '+') {
1969 device.device = '/never/';
1970 device.invert = false;
1973 // replace "name+" matches with "name*"
1974 else if (m[2] == '+')
1975 device.device = m[1] + '*';
1977 device.wildcard = !!m[2];
1979 if (!device.invert && device.wildcard)
1980 push(devices, [ [ device.device ], plain_invert_devices, wildcard_invert_devices ]);
1981 else if (!device.invert)
1982 push(plain_devices, device.device);
1983 else if (device.wildcard)
1984 push(wildcard_invert_devices, device.device);
1986 push(plain_invert_devices, device.device);
1989 if (length(plain_devices))
1992 plain_invert_devices,
1993 wildcard_invert_devices
1995 else if (!length(devices))
1998 plain_invert_devices,
1999 wildcard_invert_devices
2002 // emit zone jump rules for each device group
2003 if (length(match_devices) || length(match_subnets[0]) || length(match_subnets[1])) {
2004 for (let devgroup in devices) {
2005 // check if there's no AF specific bits, in this case we can do AF agnostic matching
2006 if (!family && !length(match_subnets[0]) && !length(match_subnets[1])) {
2007 add_rule(0, devgroup, [], zone);
2010 // we need to emit one or two AF specific rules
2012 if (family_is_ipv4(zone) && length(match_subnets[0]))
2013 for (let subnets in subnets_group_by_masking(match_subnets[0]))
2014 add_rule(4, devgroup, subnets, zone);
2016 if (family_is_ipv6(zone) && length(match_subnets[1]))
2017 for (let subnets in subnets_group_by_masking(match_subnets[1]))
2018 add_rule(6, devgroup, subnets, zone);
2023 zone.family = family;
2025 zone.match_rules = match_rules;
2027 zone.masq4_src_subnets = subnets_group_by_masking(masq_src_subnets[0]);
2028 zone.masq4_dest_subnets = subnets_group_by_masking(masq_dest_subnets[0]);
2030 zone.masq6_src_subnets = subnets_group_by_masking(masq_src_subnets[1]);
2031 zone.masq6_dest_subnets = subnets_group_by_masking(masq_dest_subnets[1]);
2034 zone.sflags[zone.input] = true;
2037 zone.dflags[zone.output] = true;
2038 zone.dflags[zone.forward] = true;
2040 zone.match_devices = map(filter(match_devices, d => !d.invert), d => d.device);
2041 zone.match_subnets = map(filter(related_subnets, s => !s.invert && s.bits != -1), this.cidr);
2043 zone.related_subnets = related_subnets;
2044 zone.related_physdevs = related_physdevs;
2046 if (zone.masq || zone.masq6)
2047 zone.dflags.snat = true;
2049 if ((zone.auto_helper && !(zone.masq || zone.masq6)) || length(zone.helper)) {
2050 zone.dflags.helper = true;
2052 for (let helper in (length(zone.helper) ? zone.helper : this.state.helpers)) {
2053 if (!helper.available)
2056 for (let proto in helper.proto) {
2057 this.state.rules = this.state.rules || [];
2058 push(this.state.rules, {
2059 chain: "helper_" + zone.name,
2060 family: helper.family,
2061 name: helper.description || helper.name,
2064 dports_pos: [ this.port(helper.port) ],
2072 this.state.zones = this.state.zones || [];
2073 push(this.state.zones, zone);
2076 parse_forwarding: function(data) {
2077 let fwd = this.parse_options(data, {
2078 enabled: [ "bool", "1" ],
2081 family: [ "family" ],
2083 src: [ "zone_ref", null, REQUIRED ],
2084 dest: [ "zone_ref", null, REQUIRED ]
2087 if (fwd === false) {
2088 this.warn_section(data, "skipped due to invalid options");
2091 else if (!fwd.enabled) {
2092 this.warn_section(data, "is disabled, ignoring section");
2096 let add_rule = (family, fwd) => {
2101 proto: { any: true }
2104 f.name = fwd.name || sprintf("Accept %s to %s forwarding",
2105 fwd.src.any ? "any" : fwd.src.zone.name,
2106 fwd.dest.any ? "any" : fwd.dest.zone.name);
2108 f.chain = fwd.src.any ? "forward" : sprintf("forward_%s", fwd.src.zone.name);
2111 f.target = "accept";
2113 f.jump_chain = sprintf("accept_to_%s", fwd.dest.zone.name);
2115 this.state.rules = this.state.rules || [];
2116 push(this.state.rules, f);
2120 let family = fwd.family;
2122 /* inherit family restrictions from related zones */
2123 if (family === 0 || family === null) {
2124 let f1 = fwd.src.zone ? fwd.src.zone.family : 0;
2125 let f2 = fwd.dest.zone ? fwd.dest.zone.family : 0;
2127 if (f1 && f2 && f1 != f2) {
2128 this.warn_section(data,
2129 sprintf("references src %s restricted to %s and dest %s restricted to %s, ignoring forwarding",
2130 fwd.src.zone.name, this.nfproto(f1, true),
2131 fwd.dest.zone.name, this.nfproto(f2, true)));
2136 this.warn_section(data,
2137 sprintf("inheriting %s restriction from src %s",
2138 this.nfproto(f1, true), fwd.src.zone.name));
2143 this.warn_section(data,
2144 sprintf("inheriting %s restriction from dest %s",
2145 this.nfproto(f2, true), fwd.dest.zone.name));
2151 add_rule(family, fwd);
2154 fwd.dest.zone.dflags.accept = true;
2157 parse_rule: function(data) {
2158 let rule = this.parse_options(data, {
2159 enabled: [ "bool", "1" ],
2161 name: [ "string", this.section_id(data[".name"]) ],
2162 _name: [ "string", null, DEPRECATED ],
2163 family: [ "family" ],
2165 src: [ "zone_ref" ],
2166 dest: [ "zone_ref" ],
2168 device: [ "device", null, NO_INVERT ],
2169 direction: [ "direction" ],
2171 ipset: [ "setmatch" ],
2172 helper: [ "cthelper" ],
2173 set_helper: [ "cthelper", null, NO_INVERT ],
2175 proto: [ "protocol", "tcpudp", PARSE_LIST | FLATTEN_LIST ],
2177 src_ip: [ "network", null, PARSE_LIST ],
2178 src_mac: [ "mac", null, PARSE_LIST ],
2179 src_port: [ "port", null, PARSE_LIST ],
2181 dest_ip: [ "network", null, PARSE_LIST ],
2182 dest_port: [ "port", null, PARSE_LIST ],
2184 icmp_type: [ "icmptype", null, PARSE_LIST ],
2185 extra: [ "string", null, UNSUPPORTED ],
2188 limit_burst: [ "int" ],
2190 utc_time: [ "bool" ],
2191 start_date: [ "date" ],
2192 stop_date: [ "date" ],
2193 start_time: [ "time" ],
2194 stop_time: [ "time" ],
2195 weekdays: [ "weekdays" ],
2196 monthdays: [ "monthdays", null, UNSUPPORTED ],
2199 set_mark: [ "mark", null, NO_INVERT ],
2200 set_xmark: [ "mark", null, NO_INVERT ],
2203 set_dscp: [ "dscp", null, NO_INVERT ],
2205 counter: [ "bool", "1" ],
2207 target: [ "target" ]
2210 if (rule === false) {
2211 this.warn_section(data, "skipped due to invalid options");
2214 else if (!rule.enabled) {
2215 this.warn_section(data, "is disabled, ignoring section");
2219 if (rule.target in ["helper", "notrack"] && (!rule.src || !rule.src.zone)) {
2220 this.warn_section(data, "must specify a source zone for target '" + rule.target + "'");
2223 else if (rule.target == "dscp" && !rule.set_dscp) {
2224 this.warn_section(data, "must specify option 'set_dscp' for target 'dscp'");
2227 else if (rule.target == "mark" && !rule.set_mark && !rule.set_xmark) {
2228 this.warn_section(data, "must specify option 'set_mark' or 'set_xmark' for target 'mark'");
2231 else if (rule.target == "helper" && !rule.set_helper) {
2232 this.warn_section(data, "must specify option 'set_helper' for target 'helper'");
2235 else if (rule.device?.any) {
2236 this.warn_section(data, "must not specify '*' as device");
2243 ipset = filter(this.state.ipsets, s => (s.name == rule.ipset.name))[0];
2246 this.warn_section(data, "references unknown set '" + rule.ipset.name + "'");
2250 if (('inet_service' in ipset.types) && !ensure_tcpudp(rule.proto)) {
2251 this.warn_section(data, "references named set with port match but no UDP/TCP protocol, ignoring section");
2256 let need_src_action_chain = (rule) => (rule.src && rule.src.zone && rule.src.zone.log && rule.target != "accept");
2258 let add_rule = (family, proto, saddrs, daddrs, sports, dports, icmptypes, icmpcodes, ipset, rule) => {
2264 has_addrs: !!(saddrs[0] || saddrs[1] || saddrs[2] || daddrs[0] || daddrs[1] || daddrs[2]),
2265 has_ports: !!(length(sports) || length(dports)),
2266 saddrs_pos: map(saddrs[0], this.cidr),
2267 saddrs_neg: map(saddrs[1], this.cidr),
2268 saddrs_masked: saddrs[2],
2269 daddrs_pos: map(daddrs[0], this.cidr),
2270 daddrs_neg: map(daddrs[1], this.cidr),
2271 daddrs_masked: daddrs[2],
2272 sports_pos: map(filter_pos(sports), this.port),
2273 sports_neg: map(filter_neg(sports), this.port),
2274 dports_pos: map(filter_pos(dports), this.port),
2275 dports_neg: map(filter_neg(dports), this.port),
2276 smacs_pos: map(filter_pos(rule.src_mac), m => m.mac),
2277 smacs_neg: map(filter_neg(rule.src_mac), m => m.mac),
2278 icmp_types: map(icmptypes, i => (family == 4 ? i.type : i.type6)),
2279 icmp_codes: map(icmpcodes, ic => sprintf('%d . %d', (family == 4) ? ic.type : ic.type6, (family == 4) ? ic.code_min : ic.code6_min))
2282 if (!length(r.icmp_types))
2283 delete r.icmp_types;
2285 if (!length(r.icmp_codes))
2286 delete r.icmp_codes;
2290 invert: r.set_mark.invert,
2291 mark: r.set_mark.mark,
2292 mask: r.set_mark.mark | r.set_mark.mask
2298 let set_types = map_setmatch(ipset, rule.ipset, proto.name);
2300 if (set_types !== set_types) {
2301 this.warn_section(data, "destination MAC address matching not supported");
2303 } else if (set_types) {
2304 r.ipset = { ...r.ipset, fields: set_types };
2307 if (r.target == "notrack") {
2308 r.chain = sprintf("notrack_%s", r.src.zone.name);
2309 r.src.zone.dflags.notrack = true;
2311 else if (r.target == "helper") {
2312 r.chain = sprintf("helper_%s", r.src.zone.name);
2313 r.src.zone.dflags.helper = true;
2315 else if (r.target == "mark" || r.target == "dscp") {
2316 if ((r.src?.any && r.dest?.any) || (r.src?.zone && r.dest?.zone))
2317 r.chain = "mangle_forward";
2318 else if (r.src?.any && r.dest?.zone)
2319 r.chain = "mangle_postrouting";
2320 else if (r.src?.zone && r.dest?.any)
2321 r.chain = "mangle_prerouting";
2322 else if (r.src && !r.dest)
2323 r.chain = "mangle_input";
2325 r.chain = "mangle_output";
2328 r.src.zone.dflags[r.target] = true;
2329 r.iifnames = null_if_empty(r.src.zone.match_devices);
2333 r.dest.zone.dflags[r.target] = true;
2334 r.oifnames = null_if_empty(r.dest.zone.match_devices);
2342 r.chain = sprintf("%s_%s", r.dest ? "forward" : "input", r.src.zone.name);
2344 r.chain = r.dest ? "forward" : "input";
2347 if (r.dest && !r.src) {
2349 r.chain = sprintf("output_%s", r.dest.zone.name);
2354 if (r.dest && !r.dest.any) {
2355 r.jump_chain = sprintf("%s_to_%s", r.target, r.dest.zone.name);
2356 r.dest.zone.dflags[r.target] = true;
2358 else if (need_src_action_chain(r)) {
2359 r.jump_chain = sprintf("%s_from_%s", r.target, r.src.zone.name);
2360 r.src.zone.dflags[r.target] = true;
2362 else if (r.target == "reject")
2363 r.jump_chain = "handle_reject";
2367 r[r.direction ? "oifnames" : "iifnames"] = [ r.device.device ];
2369 this.state.rules = this.state.rules || [];
2370 push(this.state.rules, r);
2373 for (let proto in rule.proto) {
2374 let sip, dip, sports, dports, itypes4, itypes6;
2375 let family = rule.family;
2377 switch (proto.name) {
2379 itypes4 = filter(rule.icmp_type || [], family_is_ipv4);
2380 itypes6 = filter(rule.icmp_type || [], family_is_ipv6);
2385 itypes6 = filter(rule.icmp_type || [], family_is_ipv6);
2390 sports = rule.src_port;
2391 dports = rule.dest_port;
2395 sip = subnets_split_af(rule.src_ip);
2396 dip = subnets_split_af(rule.dest_ip);
2398 family = infer_family(family, [
2401 dip, "destination IP",
2402 rule.src?.zone, "source zone",
2403 rule.dest?.zone, "destination zone",
2404 rule.helper, "helper match",
2405 rule.set_helper, "helper to set"
2408 if (type(family) == "string") {
2409 this.warn_section(data, family + ", skipping");
2413 let has_ipv4_specifics = (length(sip[0]) || length(dip[0]) || length(itypes4) || rule.dscp !== null);
2414 let has_ipv6_specifics = (length(sip[1]) || length(dip[1]) || length(itypes6) || rule.dscp !== null);
2416 /* if no family was configured, infer target family from IP addresses */
2417 if (family === null) {
2418 if (has_ipv4_specifics && !has_ipv6_specifics)
2420 else if (has_ipv6_specifics && !has_ipv4_specifics)
2426 /* check if there's no AF specific bits, in this case we can do an AF agnostic rule */
2427 if (!family && rule.target != "dscp" && !has_ipv4_specifics && !has_ipv6_specifics) {
2428 add_rule(0, proto, [], [], sports, dports, null, null, null, rule);
2431 /* we need to emit one or two AF specific rules */
2433 if (family == 0 || family == 4) {
2434 let icmp_types = filter(itypes4, i => (i.code_min == 0 && i.code_max == 0xFF));
2435 let icmp_codes = filter(itypes4, i => (i.code_min != 0 || i.code_max != 0xFF));
2437 for (let saddrs in subnets_group_by_masking(sip[0])) {
2438 for (let daddrs in subnets_group_by_masking(dip[0])) {
2439 if (length(icmp_types) || (!length(icmp_types) && !length(icmp_codes)))
2440 add_rule(4, proto, saddrs, daddrs, sports, dports, icmp_types, null, ipset, rule);
2442 if (length(icmp_codes))
2443 add_rule(4, proto, saddrs, daddrs, sports, dports, null, icmp_codes, ipset, rule);
2448 if (family == 0 || family == 6) {
2449 let icmp_types = filter(itypes6, i => (i.code_min == 0 && i.code_max == 0xFF));
2450 let icmp_codes = filter(itypes6, i => (i.code_min != 0 || i.code_max != 0xFF));
2452 for (let saddrs in subnets_group_by_masking(sip[1])) {
2453 for (let daddrs in subnets_group_by_masking(dip[1])) {
2454 if (length(icmp_types) || (!length(icmp_types) && !length(icmp_codes)))
2455 add_rule(6, proto, saddrs, daddrs, sports, dports, icmp_types, null, ipset, rule);
2457 if (length(icmp_codes))
2458 add_rule(6, proto, saddrs, daddrs, sports, dports, null, icmp_codes, ipset, rule);
2466 parse_redirect: function(data) {
2467 let redir = this.parse_options(data, {
2468 enabled: [ "bool", "1" ],
2470 name: [ "string", this.section_id(data[".name"]) ],
2471 _name: [ "string", null, DEPRECATED ],
2472 family: [ "family" ],
2474 src: [ "zone_ref" ],
2475 dest: [ "zone_ref" ],
2477 ipset: [ "setmatch" ],
2478 helper: [ "cthelper", null, NO_INVERT ],
2480 proto: [ "protocol", "tcpudp", PARSE_LIST | FLATTEN_LIST ],
2482 src_ip: [ "network" ],
2483 src_mac: [ "mac", null, PARSE_LIST ],
2484 src_port: [ "port" ],
2486 src_dip: [ "network" ],
2487 src_dport: [ "port" ],
2489 dest_ip: [ "network" ],
2490 dest_port: [ "port" ],
2492 extra: [ "string", null, UNSUPPORTED ],
2495 limit_burst: [ "int" ],
2497 utc_time: [ "bool" ],
2498 start_date: [ "date" ],
2499 stop_date: [ "date" ],
2500 start_time: [ "time" ],
2501 stop_time: [ "time" ],
2502 weekdays: [ "weekdays" ],
2503 monthdays: [ "monthdays", null, UNSUPPORTED ],
2507 reflection: [ "bool", "1" ],
2508 reflection_src: [ "reflection_source", "internal" ],
2510 reflection_zone: [ "zone_ref", null, PARSE_LIST ],
2512 counter: [ "bool", "1" ],
2514 target: [ "target", "dnat" ]
2517 if (redir === false) {
2518 this.warn_section(data, "skipped due to invalid options");
2521 else if (!redir.enabled) {
2522 this.warn_section(data, "is disabled, ignoring section");
2526 if (!(redir.target in ["dnat", "snat"])) {
2527 this.warn_section(data, "has invalid target specified, defaulting to dnat");
2528 redir.target = "dnat";
2534 ipset = filter(this.state.ipsets, s => (s.name == redir.ipset.name))[0];
2537 this.warn_section(data, "references unknown set '" + redir.ipset.name + "'");
2541 if (('inet_service' in ipset.types) && !ensure_tcpudp(redir.proto)) {
2542 this.warn_section(data, "references named set with port match but no UDP/TCP protocol, ignoring section");
2547 let resolve_dest = (redir) => {
2548 for (let zone in this.state.zones) {
2549 for (let zone_addr in zone.related_subnets) {
2550 for (let dest_addr in redir.dest_ip.addrs) {
2551 if (dest_addr.family != zone_addr.family)
2554 let a = apply_mask(dest_addr.addr, zone_addr.mask);
2555 let b = apply_mask(zone_addr.addr, zone_addr.mask);
2573 if (redir.target == "dnat") {
2575 return this.warn_section(data, "has no source specified");
2576 else if (redir.src.any)
2577 return this.warn_section(data, "must not have source '*' for dnat target");
2578 else if (redir.dest_ip && redir.dest_ip.invert)
2579 return this.warn_section(data, "must not specify a negated 'dest_ip' value");
2580 else if (redir.dest_ip && length(filter(redir.dest_ip.addrs, a => a.bits == -1)))
2581 return this.warn_section(data, "must not use non-contiguous masks in 'dest_ip'");
2583 if (!redir.dest && redir.dest_ip && resolve_dest(redir))
2584 this.warn_section(data, "does not specify a destination, assuming '" + redir.dest.zone.name + "'");
2586 if (!redir.dest_port)
2587 redir.dest_port = redir.src_dport;
2589 if (redir.reflection && redir.dest && redir.dest.zone && redir.src.zone.masq) {
2590 redir.dest.zone.dflags.accept = true;
2591 redir.dest.zone.dflags.dnat = true;
2592 redir.dest.zone.dflags.snat = true;
2596 redir.src.zone.dflags.helper = true;
2598 redir.src.zone.dflags[redir.target] = true;
2602 return this.warn_section(data, "has no destination specified");
2603 else if (redir.dest.any)
2604 return this.warn_section(data, "must not have destination '*' for snat target");
2605 else if (!redir.src_dip)
2606 return this.warn_section(data, "has no 'src_dip' option specified");
2607 else if (redir.src_dip.invert)
2608 return this.warn_section(data, "must not specify a negated 'src_dip' value");
2609 else if (length(filter(redir.src_dip.addrs, a => a.bits == -1)))
2610 return this.warn_section(data, "must not use non-contiguous masks in 'src_dip'");
2611 else if (redir.src_mac)
2612 return this.warn_section(data, "must not use 'src_mac' option for snat target");
2613 else if (redir.helper)
2614 return this.warn_section(data, "must not use 'helper' option for snat target");
2616 redir.dest.zone.dflags[redir.target] = true;
2620 let add_rule = (family, proto, saddrs, daddrs, raddrs, sport, dport, rport, ipset, redir) => {
2626 has_addrs: !!(saddrs[0] || saddrs[1] || saddrs[2] || daddrs[0] || daddrs[1] || daddrs[2]),
2627 has_ports: !!(sport || dport || rport),
2628 saddrs_pos: map(saddrs[0], this.cidr),
2629 saddrs_neg: map(saddrs[1], this.cidr),
2630 saddrs_masked: saddrs[2],
2631 daddrs_pos: map(daddrs[0], this.cidr),
2632 daddrs_neg: map(daddrs[1], this.cidr),
2633 daddrs_masked: daddrs[2],
2634 sports_pos: map(filter_pos(to_array(sport)), this.port),
2635 sports_neg: map(filter_neg(to_array(sport)), this.port),
2636 dports_pos: map(filter_pos(to_array(dport)), this.port),
2637 dports_neg: map(filter_neg(to_array(dport)), this.port),
2638 smacs_pos: map(filter_pos(redir.src_mac), m => m.mac),
2639 smacs_neg: map(filter_neg(redir.src_mac), m => m.mac),
2641 raddr: raddrs ? raddrs[0] : null,
2645 let set_types = map_setmatch(ipset, redir.ipset, proto.name);
2647 if (set_types !== set_types) {
2648 this.warn_section(data, "destination MAC address matching not supported");
2650 } else if (set_types) {
2651 r.ipset = { ...r.ipset, fields: set_types };
2656 r.chain = sprintf("dstnat_%s", r.src.zone.name);
2657 r.src.zone.dflags.dnat = true;
2660 r.target = "redirect";
2665 r.chain = sprintf("srcnat_%s", r.dest.zone.name);
2666 r.dest.zone.dflags.snat = true;
2670 this.state.redirects = this.state.redirects || [];
2671 push(this.state.redirects, r);
2674 let to_hostaddr = (a) => {
2675 let bits = (a.family == 4) ? 32 : 128;
2679 addr: apply_mask(a.addr, bits),
2684 for (let proto in redir.proto) {
2685 let sip, dip, rip, iip, eip, refip, sport, dport, rport;
2686 let family = redir.family;
2688 if (proto.name == "ipv6-icmp")
2691 switch (redir.target) {
2693 sip = subnets_split_af(redir.src_ip);
2694 dip = subnets_split_af(redir.src_dip);
2695 rip = subnets_split_af(redir.dest_ip);
2697 switch (proto.name) {
2700 sport = redir.src_port;
2701 dport = redir.src_dport;
2702 rport = redir.dest_port;
2709 sip = subnets_split_af(redir.src_ip);
2710 dip = subnets_split_af(redir.dest_ip);
2711 rip = subnets_split_af(redir.src_dip);
2713 switch (proto.name) {
2716 sport = redir.src_port;
2717 dport = redir.dest_port;
2718 rport = redir.src_dport;
2725 family = infer_family(family, [
2728 dip, "destination IP",
2730 redir.src?.zone, "source zone",
2731 redir.dest?.zone, "destination zone",
2732 redir.helper, "helper match"
2735 if (type(family) == "string") {
2736 this.warn_section(data, family + ", skipping");
2740 /* build reflection rules */
2741 if (redir.target == "dnat" && redir.reflection &&
2742 (length(rip[0]) || length(rip[1])) && redir.src?.zone && redir.dest?.zone) {
2744 name: redir.name + " (reflection)",
2746 helper: redir.helper,
2748 // XXX: this likely makes no sense for reflection rules
2749 //src_mac: redir.src_mac,
2752 limit_burst: redir.limit_burst,
2754 start_date: redir.start_date,
2755 stop_date: redir.stop_date,
2756 start_time: redir.start_time,
2757 stop_time: redir.stop_time,
2758 weekdays: redir.weekdays,
2763 let eaddrs = length(dip) ? dip : subnets_split_af({ addrs: map(redir.src.zone.related_subnets, to_hostaddr) });
2764 let rzones = length(redir.reflection_zone) ? redir.reflection_zone : [ redir.dest ];
2766 for (let rzone in rzones) {
2767 if (!is_family(rzone, family)) {
2768 this.warn_section(data,
2769 sprintf("is restricted to IPv%d but referenced reflection zone is IPv%d only, skipping",
2770 family, rzone.family));
2774 let iaddrs = subnets_split_af({ addrs: rzone.zone.related_subnets });
2775 let refaddrs = (redir.reflection_src == "internal") ? iaddrs : eaddrs;
2777 for (let i = 0; i <= 1; i++) {
2778 if (redir.src.zone[i ? "masq6" : "masq"] && length(rip[i])) {
2779 let snat_addr = refaddrs[i]?.[0];
2781 /* For internal reflection sources try to find a suitable candiate IP
2782 * among the reflection zone subnets which is within the same subnet
2783 * as the original DNAT destination. If we can't find any matching
2784 * one then simply take the first candidate. */
2785 if (redir.reflection_src == "internal") {
2786 for (let zone_addr in rzone.zone.related_subnets) {
2787 if (zone_addr.family != rip[i][0].family)
2790 let r = apply_mask(rip[i][0].addr, zone_addr.mask);
2791 let a = apply_mask(zone_addr.addr, zone_addr.mask);
2796 snat_addr = zone_addr;
2802 this.warn_section(data, (redir.reflection_src || "external") + " rewrite IP cannot be determined, disabling reflection");
2804 else if (!length(iaddrs[i])) {
2805 this.warn_section(data, "internal address range cannot be determined, disabling reflection");
2807 else if (!length(eaddrs[i])) {
2808 this.warn_section(data, "external address range cannot be determined, disabling reflection");
2811 refredir.src = rzone;
2812 refredir.dest = null;
2813 refredir.target = "dnat";
2815 for (let saddrs in subnets_group_by_masking(iaddrs[i]))
2816 for (let daddrs in subnets_group_by_masking(eaddrs[i]))
2817 add_rule(i ? 6 : 4, proto, saddrs, daddrs, rip[i], sport, dport, rport, null, refredir);
2819 refredir.src = null;
2820 refredir.dest = rzone;
2821 refredir.target = "snat";
2823 for (let daddrs in subnets_group_by_masking(rip[i]))
2824 for (let saddrs in subnets_group_by_masking(iaddrs[i]))
2825 add_rule(i ? 6 : 4, proto, saddrs, daddrs, [ to_hostaddr(snat_addr) ], null, rport, null, null, refredir);
2832 if (length(rip[0]) > 1 || length(rip[1]) > 1)
2833 this.warn_section(data, "specifies multiple rewrite addresses, using only first one");
2835 /* check if there's no AF specific bits, in this case we can do an AF agnostic rule */
2836 if (!family && !length(sip[0]) && !length(sip[1]) && !length(dip[0]) && !length(dip[1]) && !length(rip[0]) && !length(rip[1])) {
2837 /* for backwards compatibility, treat unspecified family as IPv4 unless user explicitly requested any (0) */
2841 add_rule(family, proto, [], [], null, sport, dport, rport, null, redir);
2844 /* we need to emit one or two AF specific rules */
2846 if ((!family || family == 4) && (length(sip[0]) || length(dip[0]) || length(rip[0]))) {
2847 for (let saddrs in subnets_group_by_masking(sip[0]))
2848 for (let daddrs in subnets_group_by_masking(dip[0]))
2849 add_rule(4, proto, saddrs, daddrs, rip[0], sport, dport, rport, ipset, redir);
2852 if ((!family || family == 6) && (length(sip[1]) || length(dip[1]) || length(rip[1]))) {
2853 for (let saddrs in subnets_group_by_masking(sip[1]))
2854 for (let daddrs in subnets_group_by_masking(dip[1]))
2855 add_rule(6, proto, saddrs, daddrs, rip[1], sport, dport, rport, ipset, redir);
2861 parse_nat: function(data) {
2862 let snat = this.parse_options(data, {
2863 enabled: [ "bool", "1" ],
2865 name: [ "string", this.section_id(data[".name"]) ],
2866 family: [ "family", "4" ],
2868 src: [ "zone_ref" ],
2869 device: [ "string" ],
2871 ipset: [ "setmatch", null, UNSUPPORTED ],
2873 proto: [ "protocol", "all", PARSE_LIST | FLATTEN_LIST ],
2875 src_ip: [ "network" ],
2876 src_port: [ "port" ],
2878 snat_ip: [ "network", null, NO_INVERT ],
2879 snat_port: [ "port", null, NO_INVERT ],
2881 dest_ip: [ "network" ],
2882 dest_port: [ "port" ],
2884 extra: [ "string", null, UNSUPPORTED ],
2887 limit_burst: [ "int" ],
2889 connlimit_ports: [ "bool" ],
2891 utc_time: [ "bool" ],
2892 start_date: [ "date" ],
2893 stop_date: [ "date" ],
2894 start_time: [ "time" ],
2895 stop_time: [ "time" ],
2896 weekdays: [ "weekdays" ],
2897 monthdays: [ "monthdays", null, UNSUPPORTED ],
2901 target: [ "target", "masquerade" ]
2904 if (snat === false) {
2905 this.warn_section(data, "skipped due to invalid options");
2908 else if (!snat.enabled) {
2909 this.warn_section(data, "is disabled, ignoring section");
2913 if (!(snat.target in ["accept", "snat", "masquerade"])) {
2914 this.warn_section(data, "has invalid target specified, defaulting to masquerade");
2915 snat.target = "masquerade";
2918 if (snat.target == "snat" && !snat.snat_ip && !snat.snat_port) {
2919 this.warn_section(data, "needs either 'snat_ip' or 'snat_port' for target snat, ignoring section");
2922 else if (snat.target != "snat" && snat.snat_ip) {
2923 this.warn_section(data, "must not use 'snat_ip' for non-snat target, ignoring section");
2926 else if (snat.target != "snat" && snat.snat_port) {
2927 this.warn_section(data, "must not use 'snat_port' for non-snat target, ignoring section");
2931 if ((snat.snat_port || snat.src_port || snat.dest_port) && !ensure_tcpudp(snat.proto)) {
2932 this.warn_section(data, "specifies ports but no UDP/TCP protocol, ignoring section");
2936 if (snat.snat_ip && length(filter(snat.snat_ip.addrs, a => a.bits == -1 || a.invert))) {
2937 this.warn_section(data, "must not use inversion or non-contiguous masks in 'snat_ip', ignoring section");
2941 if (snat.src && snat.src.zone)
2942 snat.src.zone.dflags.snat = true;
2944 let add_rule = (family, proto, saddrs, daddrs, raddrs, sport, dport, rport, snat) => {
2950 has_addrs: !!(saddrs[0] || saddrs[1] || saddrs[2] || daddrs[0] || daddrs[1] || daddrs[2]),
2951 has_ports: !!(sport || dport),
2952 saddrs_pos: map(saddrs[0], this.cidr),
2953 saddrs_neg: map(saddrs[1], this.cidr),
2954 saddrs_masked: saddrs[2],
2955 daddrs_pos: map(daddrs[0], this.cidr),
2956 daddrs_neg: map(daddrs[1], this.cidr),
2957 daddrs_masked: daddrs[2],
2958 sports_pos: map(filter_pos(to_array(sport)), this.port),
2959 sports_neg: map(filter_neg(to_array(sport)), this.port),
2960 dports_pos: map(filter_pos(to_array(dport)), this.port),
2961 dports_neg: map(filter_neg(to_array(dport)), this.port),
2963 raddr: raddrs ? raddrs[0] : null,
2966 chain: (snat.src && snat.src.zone) ? sprintf("srcnat_%s", snat.src.zone.name) : "srcnat"
2969 this.state.redirects = this.state.redirects || [];
2970 push(this.state.redirects, n);
2973 for (let proto in snat.proto) {
2974 let sip, dip, rip, sport, dport, rport;
2975 let family = snat.family;
2977 sip = subnets_split_af(snat.src_ip);
2978 dip = subnets_split_af(snat.dest_ip);
2979 rip = subnets_split_af(snat.snat_ip);
2981 switch (proto.name) {
2984 sport = snat.src_port;
2985 dport = snat.dest_port;
2986 rport = snat.snat_port;
2990 if (length(rip[0]) > 1 || length(rip[1]) > 1)
2991 this.warn_section(data, "specifies multiple rewrite addresses, using only first one");
2993 /* inherit family restrictions from related zones */
2994 if (family === 0 || family === null) {
2995 let f = (rule.src && rule.src.zone) ? rule.src.zone.family : 0;
2998 this.warn_section(r,
2999 sprintf("inheriting %s restriction from src %s",
3000 this.nfproto(f1, true), rule.src.zone.name));
3006 /* if no family was configured, infer target family from IP addresses */
3007 if (family === null) {
3008 if ((length(sip[0]) || length(dip[0]) || length(rip[0])) && !length(sip[1]) && !length(dip[1]) && !length(rip[1]))
3010 else if ((length(sip[1]) || length(dip[1]) || length(rip[1])) && !length(sip[0]) && !length(dip[0]) && !length(rip[0]))
3016 /* check if there's no AF specific bits, in this case we can do an AF agnostic rule */
3017 if (!family && !length(sip[0]) && !length(sip[1]) && !length(dip[0]) && !length(dip[1]) && !length(rip[0]) && !length(rip[1])) {
3018 add_rule(0, proto, [], [], null, sport, dport, rport, snat);
3021 /* we need to emit one or two AF specific rules */
3023 if (family == 0 || family == 4)
3024 for (let saddr in subnets_group_by_masking(sip[0]))
3025 for (let daddr in subnets_group_by_masking(dip[0]))
3026 add_rule(4, proto, saddr, daddr, rip[0], sport, dport, rport, snat);
3028 if (family == 0 || family == 6)
3029 for (let saddr in subnets_group_by_masking(sip[1]))
3030 for (let daddr in subnets_group_by_masking(dip[1]))
3031 add_rule(6, proto, saddr, daddr, rip[1], sport, dport, rport, snat);
3036 parse_ipset: function(data) {
3037 let ipset = this.parse_options(data, {
3038 enabled: [ "bool", "1" ],
3039 reload_set: [ "bool" ],
3040 counters: [ "bool" ],
3041 comment: [ "bool" ],
3043 name: [ "string", null, REQUIRED ],
3044 family: [ "family", "4" ],
3046 storage: [ "string", null, UNSUPPORTED ],
3047 match: [ "ipsettype", null, PARSE_LIST ],
3049 iprange: [ "string", null, UNSUPPORTED ],
3050 portrange: [ "string", null, UNSUPPORTED ],
3052 netmask: [ "int", null, UNSUPPORTED ],
3054 hashsize: [ "int", null, UNSUPPORTED ],
3055 timeout: [ "int", "-1" ],
3057 external: [ "string", null, UNSUPPORTED ],
3059 entry: [ "string", null, PARSE_LIST ],
3060 loadfile: [ "string" ]
3063 if (ipset === false) {
3064 this.warn_section(data, "skipped due to invalid options");
3067 else if (!ipset.enabled) {
3068 this.warn_section(data, "is disabled, ignoring section");
3072 if (ipset.family == 0) {
3073 this.warn_section(data, "must not specify family 'any'");
3076 else if (!length(ipset.match)) {
3077 this.warn_section(data, "has no datatypes assigned");
3081 let dirs = map(ipset.match, m => m[0]),
3082 types = map(ipset.match, m => m[1]),
3085 if ("set" in types) {
3086 this.warn_section(data, "match type 'set' is not supported");
3090 if ("net" in types) {
3091 if (this.kernel < 0x05060000) {
3092 this.warn_section(data, "match type 'net' requires kernel 5.6 or later");
3104 types: map(types, (t) => {
3108 return (ipset.family == 4) ? 'ipv4_addr' : 'ipv6_addr';
3111 return 'ether_addr';
3114 return 'inet_service';
3123 s.entries = filter(map(ipset.entry, (e) => {
3124 let v = self.parse_ipsetentry(e, s);
3127 self.warn_section(data, "ignoring invalid ipset entry '" + e + "'");
3130 }), (e) => (e != null));
3132 this.state.ipsets = this.state.ipsets || [];
3133 push(this.state.ipsets, s);