1 const fs = require("fs");
2 const uci = require("uci");
3 const ubus = require("ubus");
5 const STATEFILE = "/var/run/fw4.state";
7 const PARSE_LIST = 0x01;
8 const FLATTEN_LIST = 0x02;
9 const NO_INVERT = 0x04;
10 const UNSUPPORTED = 0x08;
11 const REQUIRED = 0x10;
12 const DEPRECATED = 0x20;
14 const ipv4_icmptypes = {
15 "any": [ 0xFF, 0, 0xFF ],
16 "echo-reply": [ 0, 0, 0xFF ],
17 "pong": [ 0, 0, 0xFF ], /* Alias */
19 "destination-unreachable": [ 3, 0, 0xFF ],
20 "network-unreachable": [ 3, 0, 0 ],
21 "host-unreachable": [ 3, 1, 1 ],
22 "protocol-unreachable": [ 3, 2, 2 ],
23 "port-unreachable": [ 3, 3, 3 ],
24 "fragmentation-needed": [ 3, 4, 4 ],
25 "source-route-failed": [ 3, 5, 5 ],
26 "network-unknown": [ 3, 6, 6 ],
27 "host-unknown": [ 3, 7, 7 ],
28 "network-prohibited": [ 3, 9, 9 ],
29 "host-prohibited": [ 3, 10, 10 ],
30 "TOS-network-unreachable": [ 3, 11, 11 ],
31 "TOS-host-unreachable": [ 3, 12, 12 ],
32 "communication-prohibited": [ 3, 13, 13 ],
33 "host-precedence-violation": [ 3, 14, 14 ],
34 "precedence-cutoff": [ 3, 15, 15 ],
36 "source-quench": [ 4, 0, 0xFF ],
38 "redirect": [ 5, 0, 0xFF ],
39 "network-redirect": [ 5, 0, 0 ],
40 "host-redirect": [ 5, 1, 1 ],
41 "TOS-network-redirect": [ 5, 2, 2 ],
42 "TOS-host-redirect": [ 5, 3, 3 ],
44 "echo-request": [ 8, 0, 0xFF ],
45 "ping": [ 8, 0, 0xFF ], /* Alias */
47 "router-advertisement": [ 9, 0, 0xFF ],
49 "router-solicitation": [ 10, 0, 0xFF ],
51 "time-exceeded": [ 11, 0, 0xFF ],
52 "ttl-exceeded": [ 11, 0, 0xFF ], /* Alias */
53 "ttl-zero-during-transit": [ 11, 0, 0 ],
54 "ttl-zero-during-reassembly": [ 11, 1, 1 ],
56 "parameter-problem": [ 12, 0, 0xFF ],
57 "ip-header-bad": [ 12, 0, 0 ],
58 "required-option-missing": [ 12, 1, 1 ],
60 "timestamp-request": [ 13, 0, 0xFF ],
62 "timestamp-reply": [ 14, 0, 0xFF ],
64 "address-mask-request": [ 17, 0, 0xFF ],
66 "address-mask-reply": [ 18, 0, 0xFF ]
69 const ipv6_icmptypes = {
70 /* "name": [ type-code, code-min, code-max ] */
71 "destination-unreachable": [ 1, 0, 0xFF ],
72 "no-route": [ 1, 0, 0 ],
73 "communication-prohibited": [ 1, 1, 1 ],
74 "address-unreachable": [ 1, 3, 3 ],
75 "port-unreachable": [ 1, 4, 4 ],
77 "packet-too-big": [ 2, 0, 0xFF ],
79 "time-exceeded": [ 3, 0, 0xFF ],
80 "ttl-exceeded": [ 3, 0, 0xFF ], /* Alias */
81 "ttl-zero-during-transit": [ 3, 0, 0 ],
82 "ttl-zero-during-reassembly": [ 3, 1, 1 ],
84 "parameter-problem": [ 4, 0, 0xFF ],
85 "bad-header": [ 4, 0, 0 ],
86 "unknown-header-type": [ 4, 1, 1 ],
87 "unknown-option": [ 4, 2, 2 ],
89 "echo-request": [ 128, 0, 0xFF ],
90 "ping": [ 128, 0, 0xFF ], /* Alias */
92 "echo-reply": [ 129, 0, 0xFF ],
93 "pong": [ 129, 0, 0xFF ], /* Alias */
95 /* only code zero (0) */
96 "multicast-listener-query": [ 130, 0, 0xFF ],
97 "multicast-listener-report": [ 131, 0, 0xFF ],
98 "multicast-listener-done": [ 132, 0, 0xFF ],
100 "router-solicitation": [ 133, 0, 0xFF ],
102 "router-advertisement": [ 134, 0, 0xFF ],
104 "neighbour-solicitation": [ 135, 0, 0xFF ],
105 "neighbor-solicitation": [ 135, 0, 0xFF ], /* Alias */
107 "neighbour-advertisement": [ 136, 0, 0xFF ],
108 "neighbor-advertisement": [ 136, 0, 0xFF ], /* Alias */
110 "redirect": [ 137, 0, 0xFF ],
112 /* codes 0, 1, 255 */
113 "router-renumbering": [ 138, 0, 0xFF ],
116 "node-info-query": [ 139, 0, 0xFF ],
118 "node-info-response": [ 140, 0, 0xFF ],
120 "inverse-neighbour-discovery-solicitation-message": [ 141, 0, 0xFF ],
121 "inverse-neighbor-discovery-solicitation-message": [ 141, 0, 0xFF ], /* Alias */
123 "inverse-neighbour-discovery-advertisement-message": [ 142, 0, 0xFF ],
124 "inverse-neighbor-discovery-advertisement-message": [ 142, 0, 0xFF ], /* Alias */
126 "v2-multicast-listener-report": [ 143, 0, 0xFF ],
128 "home-agent-address-discovery-request-message": [ 144, 0, 0xFF ],
129 "home-agent-address-discovery-reply-message": [ 145, 0, 0xFF ],
131 "mobile-prefix-solicitation": [ 146, 0, 0xFF ],
132 "mobile-prefix-advertisement": [ 147, 0, 0xFF ],
135 "certification-path-solicitation-message": [ 148, 0, 0xFF ],
136 "certification-path-advertisement-message": [ 149, 0, 0xFF ],
138 "seamoby-protocol-message": [ 150, 0, 0xFF ],
140 /* IPv6 multicast: FF02:0:0:0:0:0:0:6A, no code fields */
141 "multicast-router-advertisement": [ 151, 0, 0xFF ],
142 "multicast-router-solicitation": [ 152, 0, 0xFF ],
143 "multicast-router-termination": [ 153, 0, 0xFF ],
145 /* codes 0-5, 6-255 */
146 "fmipv6-message": [ 154, 0, 0xFF ],
148 "rpl-control-message": [ 155, 0, 0xFF ],
150 "ilnpv6-locator-update-message": [ 156, 0, 0xFF ],
152 /* codes 0-4, 5-15 */
153 "duplicate-address-request": [ 157, 0, 0xFF ],
154 /* codes 0-4, 5-15 */
155 "duplicate-address-confirmation": [ 158, 0, 0xFF ],
157 /* Multicast Protocol for Low-Power */
158 "mpl-control-message": [ 159, 0, 0xFF ],
161 "extended-echo-request": [ 160, 0, 0xFF ],
162 "extended-ping": [ 160, 0, 0xFF ], /* Alias */
164 /* codes 0-4, 5-255 */
165 "extended-echo-reply": [ 161, 0, 0xFF ],
166 "extended-pong": [ 161, 0, 0xFF ] /* Alias */
169 const dscp_classes = {
195 function to_mask(bits, v6) {
196 let m = [], n = false;
203 if (bits > (v6 ? 128 : 32))
206 for (let i = 0; i < (v6 ? 16 : 4); i++) {
207 let b = (bits < 8) ? bits : 8;
208 m[i] = (n ? ~(0xff << (8 - b)) : (0xff << (8 - b))) & 0xff;
215 function to_bits(mask) {
216 let a = iptoarr(mask);
223 for (let i = 0, z = false; i < length(a); i++) {
226 while (!z && (a[i] & 0x80)) {
227 a[i] = (a[i] << 1) & 0xff;
238 function apply_mask(addr, mask) {
239 let a = iptoarr(addr);
244 if (type(mask) == "int") {
245 for (let i = 0; i < length(a); i++) {
246 let b = (mask < 8) ? mask : 8;
247 a[i] &= (0xff << (8 - b)) & 0xff;
252 let m = iptoarr(mask);
254 if (!m || length(a) != length(m))
257 for (let i = 0; i < length(a); i++)
264 function to_array(x) {
265 if (type(x) == "array")
271 if (type(x) == "object")
276 return (x == "") ? [] : split(x, /[ \t]+/);
279 function filter_pos(x) {
280 let rv = filter(x, e => !e.invert);
281 return length(rv) ? rv : null;
284 function filter_neg(x) {
285 let rv = filter(x, e => e.invert);
286 return length(rv) ? rv : null;
289 function null_if_empty(x) {
290 return length(x) ? x : null;
293 function subnets_split_af(x) {
296 for (let ag in to_array(x)) {
297 for (let a in filter(ag.addrs, a => (a.family == 4)))
298 push(rv[0] ||= [], { ...a, invert: ag.invert });
300 for (let a in filter(ag.addrs, a => (a.family == 6)))
301 push(rv[1] ||= [], { ...a, invert: ag.invert });
305 rv.family = (!rv[0] ^ !rv[1]) ? (rv[0] ? 4 : 6) : 0;
310 function subnets_group_by_masking(x) {
311 let groups = [], plain = [], nc = [], invert_plain = [], invert_masked = [];
313 for (let a in to_array(x)) {
314 if (a.bits == -1 && !a.invert)
318 else if (a.bits == -1)
319 push(invert_masked, a);
321 push(invert_plain, a);
325 push(groups, [ null, null_if_empty(invert_plain), [ a, ...invert_masked ] ]);
330 null_if_empty(invert_plain),
331 null_if_empty(invert_masked)
334 else if (!length(groups)) {
337 null_if_empty(invert_plain),
338 null_if_empty(invert_masked)
345 function ensure_tcpudp(x) {
346 if (length(filter(x, p => (p.name == "tcp" || p.name == "udp"))))
349 let rest = filter(x, p => !p.any),
350 any = filter(x, p => p.any);
352 if (length(any) && !length(rest)) {
354 push(x, { name: "tcp" }, { name: "udp" });
361 let is_family = (x, v) => (!x.family || x.family == v);
362 let family_is_ipv4 = (x) => (!x.family || x.family == 4);
363 let family_is_ipv6 = (x) => (!x.family || x.family == 6);
365 function infer_family(f, objects) {
369 for (let i = 0; i < length(objects); i += 2) {
370 let objs = to_array(objects[i]),
371 desc = objects[i + 1];
373 for (let obj in objs) {
374 if (!obj || !obj.family || obj.family == res)
384 ? `references IPv${obj.family} only ${desc} but is restricted to IPv${res} by ${by}`
385 : `is restricted to IPv${res} but referenced ${desc} is IPv${obj.family} only`;
392 function map_setmatch(set, match, proto) {
393 if (!set || (('inet_service' in set.types) && proto != 'tcp' && proto != 'udp'))
398 for (let i, t in set.types) {
399 let dir = ((match.dir?.[i] || set.directions[i] || 'src') == 'src' ? 's' : 'd');
403 fields[i] = `ip ${dir}addr`;
407 fields[i] = `ip6 ${dir}addr`;
414 fields[i] = 'ether saddr';
418 fields[i] = `${proto} ${dir}port`;
426 function resolve_lower_devices(devstatus, devname) {
427 let dir = fs.opendir(`/sys/class/net/${devname}`);
431 switch (devstatus[devname]?.devtype) {
436 while ((e = dir.read()) != null)
437 if (index(e, "lower_") === 0)
438 push(devs, ...resolve_lower_devices(devstatus, substr(e, 6)));
454 function nft_json_command(...args) {
455 let cmd = [ "/usr/sbin/nft", "--terse", "--json", ...args ];
456 let nft = fs.popen(join(" ", cmd), "r");
461 info = filter(json(nft.read("all"))?.nftables,
462 item => (type(item) == "object" && !item.metainfo));
465 warn(`Unable to parse nftables JSON output: ${e}\n`);
471 warn(`Unable to popen() ${cmd}: ${fs.error()}\n`);
477 function nft_try_hw_offload(devices) {
479 add table inet fw4-hw-offload-test;
480 add flowtable inet fw4-hw-offload-test ft {
481 hook ingress priority 0;
482 devices = { "${join('", "', devices)}" };
487 let rc = system(`/usr/sbin/nft -c '${replace(nft_test, "'", "'\\''")}' 2>/dev/null`);
494 read_kernel_version: function() {
495 let fd = fs.open("/proc/version", "r"),
499 let m = match(fd.read("line"), /^Linux version ([0-9]+)\.([0-9]+)\.([0-9]+)/);
501 v = m ? (+m[1] << 24) | (+m[2] << 16) | (+m[3] << 8) : 0;
508 resolve_hw_offload_devices: function() {
509 if (!this.default_option("flow_offloading_hw"))
512 let devstatus = null;
514 let bus = ubus.connect();
517 devstatus = bus.call("network.device", "status") || {};
521 for (let zone in this.zones())
522 for (let device in zone.related_physdevs)
523 push(devices ||= [], ...resolve_lower_devices(devstatus, device));
528 devices = sort(uniq(devices));
530 if (!nft_try_hw_offload(devices)) {
531 this.warn('Hardware flow offloading unavailable, falling back to software offloading');
532 this.state.defaults.flow_offloading_hw = false;
540 resolve_offload_devices: function() {
541 if (!this.default_option("flow_offloading"))
544 let devices = this.resolve_hw_offload_devices();
549 for (let zone in this.zones())
550 for (let device in zone.related_physdevs)
551 if (fs.access(`/sys/class/net/${device}`))
552 push(devices, device);
554 devices = sort(uniq(devices));
560 check_set_types: function() {
563 for (let item in nft_json_command("list", "sets", "inet"))
564 if (item.set?.table == "fw4")
565 sets[item.set.name] = (type(item.set.type) == "array") ? item.set.type : [ item.set.type ];
570 check_flowtable: function() {
571 for (let item in nft_json_command("list", "flowtables", "inet"))
572 if (item.flowtable?.table == "fw4" && item.flowtable?.name == "ft")
578 read_state: function() {
579 let fd = fs.open(STATEFILE, "r");
584 state = json(fd.read("all"));
587 warn(`Unable to parse '${STATEFILE}': ${e}\n`);
596 read_ubus: function() {
599 rules = [], networks = {},
600 bus = ubus.connect();
603 ifaces = bus.call("network.interface", "dump");
604 services = bus.call("service", "get_data", { "type": "firewall" });
609 warn(`Unable to connect to ubus: ${ubus.error()}\n`);
614 // Gather logical network information from ubus
617 if (type(ifaces?.interface) == "array") {
618 for (let ifc in ifaces.interface) {
621 device: ifc.l3_device ?? ifc.device,
626 if (type(ifc["ipv4-address"]) == "array") {
627 for (let addr in ifc["ipv4-address"]) {
628 push(net.ipaddrs ||= [], {
631 mask: to_mask(addr.mask, false),
637 if (type(ifc["ipv6-address"]) == "array") {
638 for (let addr in ifc["ipv6-address"]) {
639 push(net.ipaddrs ||= [], {
642 mask: to_mask(addr.mask, true),
648 if (type(ifc["ipv6-prefix-assignment"]) == "array") {
649 for (let addr in ifc["ipv6-prefix-assignment"]) {
650 if (addr["local-address"]) {
651 push(net.ipaddrs ||= [], {
653 addr: addr["local-address"].address,
654 mask: to_mask(addr["local-address"].mask, true),
655 bits: addr["local-address"].mask
661 if (type(ifc.data?.firewall) == "array") {
664 for (let rulespec in ifc.data.firewall) {
668 name: (rulespec.type != 'ipset') ? `ubus:${ifc.interface}[${ifc.proto}] ${rulespec.type || 'rule'} ${n}` : rulespec.name,
669 device: rulespec.device ?? ifc.l3_device ?? ifc.device
676 networks[ifc.interface] = net;
682 // Gather firewall rule definitions from ubus services
685 if (type(services) == "object") {
686 for (let svcname, service in services) {
687 if (type(service?.firewall) == "array") {
690 for (let rulespec in services[svcname].firewall) {
694 name: (rulespec.type != 'ipset') ? `ubus:${svcname} ${rulespec.type || 'rule'} ${n}` : rulespec.name
701 for (let svcinst, instance in service) {
702 if (type(instance?.firewall) == "array") {
705 for (let rulespec in instance.firewall) {
709 name: (rulespec.type != 'ipset') ? `ubus:${svcname}[${svcinst}] ${rulespec.type || 'rule'} ${n}` : rulespec.name
725 load: function(use_statefile) {
728 this.state = use_statefile ? this.read_state() : null;
730 this.cursor = uci.cursor();
731 this.cursor.load("firewall");
732 this.cursor.load("/usr/share/firewall4/helpers");
735 this.state = this.read_ubus();
737 this.kernel = this.read_kernel_version();
741 // Read helper mapping
744 this.cursor.foreach("helpers", "helper", h => self.parse_helper(h));
748 // Read default policies
751 this.cursor.foreach("firewall", "defaults", d => self.parse_defaults(d));
753 if (!this.state.defaults)
754 this.parse_defaults({});
758 // Build list of ipsets
761 if (!this.state.ipsets) {
762 map(filter(this.state.ubus_rules, n => (n.type == "ipset")), s => self.parse_ipset(s));
763 this.cursor.foreach("firewall", "ipset", s => self.parse_ipset(s));
768 // Build list of logical zones
771 if (!this.state.zones)
772 this.cursor.foreach("firewall", "zone", z => self.parse_zone(z));
776 // Build list of rules
779 map(filter(this.state.ubus_rules, r => (r.type == "rule")), r => self.parse_rule(r));
780 this.cursor.foreach("firewall", "rule", r => self.parse_rule(r));
784 // Build list of forwardings
787 this.cursor.foreach("firewall", "forwarding", f => self.parse_forwarding(f));
791 // Build list of redirects
794 map(filter(this.state.ubus_rules, r => (r.type == "redirect")), r => self.parse_redirect(r));
795 this.cursor.foreach("firewall", "redirect", r => self.parse_redirect(r));
799 // Build list of snats
802 map(filter(this.state.ubus_rules, n => (n.type == "nat")), n => self.parse_nat(n));
803 this.cursor.foreach("firewall", "nat", n => self.parse_nat(n));
807 // Build list of includes
810 this.cursor.foreach("firewall", "include", i => self.parse_include(i));
814 // Discover automatic includes
817 if (this.default_option("auto_includes")) {
818 for (let position in [ 'ruleset-pre', 'ruleset-post', 'table-pre', 'table-post', 'chain-pre', 'chain-post' ])
819 for (let chain in (position in [ 'chain-pre', 'chain-post' ]) ? fs.lsdir(`/usr/share/nftables.d/${position}`) : [ null ])
820 for (let path in fs.glob(`/usr/share/nftables.d/${position}${chain ? `/${chain}` : ''}/*.nft`))
822 this.parse_include({ type: 'nftables', position, chain, path });
827 let fd = fs.open(STATEFILE, "w");
831 zones: this.state.zones,
832 ipsets: this.state.ipsets,
833 networks: this.state.networks,
834 ubus_rules: this.state.ubus_rules,
835 includes: this.state.includes
841 warn(`Unable to write '${STATEFILE}': ${fs.error()}\n`);
846 warn: function(fmt, ...args) {
850 let msg = sprintf(fmt, ...args);
853 warn(`\033[33m${msg}\033[m\n`);
855 warn(`[!] ${msg}\n`);
858 get: function(sid, opt) {
859 return this.cursor.get("firewall", sid, opt);
862 get_all: function(sid) {
863 return this.cursor.get_all("firewall", sid);
866 parse_options: function(s, spec) {
869 for (let key, val in spec) {
870 let datatype = `parse_${val[0]}`,
873 parsefn = (flags & PARSE_LIST) ? "parse_list" : "parse_opt";
875 let res = this[parsefn](s, key, datatype, defval, flags);
880 if (type(res) == "object" && res.invert && (flags & NO_INVERT)) {
881 this.warn_section(s, `option '${key}' must not be negated`);
886 if (flags & DEPRECATED)
887 this.warn_section(s, `option '${key}' is deprecated by fw4`);
888 else if (flags & UNSUPPORTED)
889 this.warn_section(s, `option '${key}' is not supported by fw4`);
896 if (index(opt, '.') != 0 && opt != 'type' && !exists(spec, opt)) {
897 this.warn_section(s, `specifies unknown option '${opt}'`);
904 parse_subnet: function(subnet) {
905 let parts = split(subnet, "/");
908 switch (length(parts)) {
910 a = iptoarr(parts[0]);
911 m = iptoarr(parts[1]);
917 if (length(a) != length(m))
920 b = to_bits(parts[1]);
922 /* allow non-contiguous masks such as `::ffff:ffff:ffff:ffff` */
935 if (type(b) != "int")
938 m = to_mask(b, length(a) == 16);
943 family: (length(a) == 16) ? 6 : 4,
950 parts = split(parts[0], "-");
952 switch (length(parts)) {
954 a = iptoarr(parts[0]);
955 b = iptoarr(parts[1]);
957 if (a && b && length(a) == length(b)) {
959 family: (length(a) == 16) ? 6 : 4,
969 a = iptoarr(parts[0]);
973 family: (length(a) == 16) ? 6 : 4,
975 mask: to_mask(length(a) * 8, length(a) == 16),
980 n = this.state.networks[parts[0]];
983 return [ ...(n.ipaddrs || []) ];
990 parse_enum: function(val, choices) {
991 if (type(val) == "string") {
994 for (let i = 0; i < length(choices); i++)
995 if (lc(substr(choices[i], 0, length(val))) == val)
1002 section_id: function(sid) {
1003 let s = this.get_all(sid);
1008 if (s[".anonymous"]) {
1011 this.cursor.foreach("firewall", s[".type"], function(ss) {
1012 if (ss[".name"] == s[".name"])
1018 return `@${s['.type']}[${c}]`;
1024 warn_section: function(s, msg) {
1027 this.warn("Section %s (%s) %s", this.section_id(s[".name"]), s.name, msg);
1029 this.warn("Section %s %s", this.section_id(s[".name"]), msg);
1033 this.warn("ubus %s (%s) %s", s.type || "rule", s.name, msg);
1035 this.warn("ubus %s %s", s.type || "rule", msg);
1039 parse_policy: function(val) {
1040 return this.parse_enum(val, [
1047 parse_bool: function(val) {
1048 if (val == "1" || val == "on" || val == "true" || val == "yes")
1050 else if (val == "0" || val == "off" || val == "false" || val == "no")
1056 parse_family: function(val) {
1057 if (val == 'any' || val == 'all' || val == '*')
1059 else if (val == 'inet' || index(val, '4') > -1)
1061 else if (index(val, '6') > -1)
1067 parse_zone_ref: function(val) {
1072 return { any: true };
1074 for (let zone in this.state.zones) {
1075 if (zone.name == val) {
1086 parse_device: function(val) {
1087 let rv = this.parse_invert(val);
1100 parse_direction: function(val) {
1101 if (val == 'in' || val == 'ingress')
1103 else if (val == 'out' || val == 'egress')
1109 parse_setmatch: function(val) {
1110 let rv = this.parse_invert(val);
1115 rv.val = trim(replace(rv.val, /^[^ \t]+/, function(m) {
1120 let dir = split(rv.val, /[ \t,]/);
1122 for (let i = 0; i < 3 && i < length(dir); i++) {
1123 if (dir[i] == "dst" || dir[i] == "dest")
1124 (rv.dir ||= [])[i] = "dst";
1125 else if (dir[i] == "src")
1126 (rv.dir ||= [])[i] = "src";
1129 return length(rv.name) ? rv : null;
1132 parse_cthelper: function(val) {
1133 let rv = this.parse_invert(val);
1138 let helper = filter(this.state.helpers, h => (h.name == rv.val))?.[0];
1140 return helper ? { ...rv, ...helper } : null;
1143 parse_protocol: function(val) {
1144 let p = this.parse_invert(val);
1166 p.name = 'ipv6-icmp';
1171 { invert: p.invert, name: 'tcp' },
1172 { invert: p.invert, name: 'udp' }
1187 return (p.any || length(p.name)) ? p : null;
1190 parse_mac: function(val) {
1191 let mac = this.parse_invert(val);
1192 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;
1197 mac.mac = sprintf('%02x:%02x:%02x:%02x:%02x:%02x',
1198 hex(m[1]), hex(m[2]), hex(m[3]),
1199 hex(m[4]), hex(m[5]), hex(m[6]));
1204 parse_port: function(val) {
1205 let port = this.parse_invert(val);
1206 let m = port ? match(port.val, /^([0-9]{1,5})([-:]([0-9]{1,5}))?$/i) : null;
1212 let min_port = +m[1];
1213 let max_port = +m[3];
1215 if (min_port > max_port ||
1216 min_port < 0 || max_port < 0 ||
1217 min_port > 65535 || max_port > 65535)
1220 port.min = min_port;
1221 port.max = max_port;
1226 if (pn != pn || pn < 0 || pn > 65535)
1236 parse_network: function(val) {
1237 let rv = this.parse_invert(val);
1242 let nets = this.parse_subnet(rv.val);
1248 rv.addrs = [ ...nets ];
1253 parse_icmptype: function(val) {
1256 if (exists(ipv4_icmptypes, val)) {
1259 rv.type = ipv4_icmptypes[val][0];
1260 rv.code_min = ipv4_icmptypes[val][1];
1261 rv.code_max = ipv4_icmptypes[val][2];
1264 if (exists(ipv6_icmptypes, val)) {
1265 rv.family = rv.family ? 0 : 6;
1267 rv.type6 = ipv6_icmptypes[val][0];
1268 rv.code6_min = ipv6_icmptypes[val][1];
1269 rv.code6_max = ipv6_icmptypes[val][2];
1272 if (!exists(rv, "family")) {
1273 let m = match(val, /^([0-9]+)(\/([0-9]+))?$/);
1280 rv.code_min = +m[3];
1281 rv.code_max = rv.code_min;
1289 if (rv.type > 0xFF || rv.code_min > 0xFF || rv.code_max > 0xFF)
1295 rv.code6_min = rv.code_min;
1296 rv.code6_max = rv.code_max;
1302 parse_invert: function(val) {
1306 let rv = { invert: false };
1308 rv.val = trim(replace(val, /^[ \t]*!/, () => (rv.invert = true, '')));
1310 return length(rv.val) ? rv : null;
1313 parse_limit: function(val) {
1314 let rv = this.parse_invert(val);
1315 let m = rv ? match(rv.val, /^([0-9]+)(\/([a-z]+))?$/) : null;
1321 let u = m[3] ? this.parse_enum(m[3], [ "second", "minute", "hour", "day" ]) : "second";
1332 parse_int: function(val) {
1335 return (n == n) ? n : null;
1338 parse_date: function(val) {
1339 let d = match(val, /^([0-9]{4})(-([0-9]{1,2})(-([0-9]{1,2})(T([0-9:]+))?)?)?$/);
1341 if (d == null || d[1] < 1970 || d[1] > 2038 || d[3] > 12 || d[5] > 31)
1344 let t = this.parse_time(d[7] ?? "0");
1357 parse_time: function(val) {
1358 let t = match(val, /^([0-9]{1,2})(:([0-9]{1,2})(:([0-9]{1,2}))?)?$/);
1360 if (t == null || t[1] > 23 || t[3] > 59 || t[5] > 59)
1370 parse_weekdays: function(val) {
1371 let rv = this.parse_invert(val);
1376 for (let day in to_array(rv.val)) {
1377 day = this.parse_enum(day, [
1390 (rv.days ||= {})[day] = true;
1393 rv.days = keys(rv.days);
1395 return rv.days ? rv : null;
1398 parse_monthdays: function(val) {
1399 let rv = this.parse_invert(val);
1404 for (let day in to_array(rv.val)) {
1407 if (day < 1 || day > 31)
1410 (rv.days ||= [])[day] = true;
1413 return rv.days ? rv : null;
1416 parse_mark: function(val) {
1417 let rv = this.parse_invert(val);
1418 let m = rv ? match(rv.val, /^(0?x?[0-9a-f]+)(\/(0?x?[0-9a-f]+))?$/i) : null;
1425 if (n != n || n > 0xFFFFFFFF)
1429 rv.mask = 0xFFFFFFFF;
1434 if (n != n || n > 0xFFFFFFFF)
1443 parse_dscp: function(val) {
1444 let rv = this.parse_invert(val);
1449 rv.val = uc(rv.val);
1451 if (exists(dscp_classes, rv.val)) {
1452 rv.dscp = dscp_classes[rv.val];
1457 if (n != n || n < 0 || n > 0x3F)
1466 parse_target: function(val) {
1467 return this.parse_enum(val, [
1484 parse_reject_code: function(val) {
1485 return this.parse_enum(val, [
1494 parse_reflection_source: function(val) {
1495 return this.parse_enum(val, [
1501 parse_ipsettype: function(val) {
1502 let m = match(val, /^(src|dst|dest)_(.+)$/);
1503 let t = this.parse_enum(m ? m[2] : val, [
1511 return t ? [ (!m || m[1] == 'src') ? 'src' : 'dst', t ] : null;
1514 parse_ipsetentry: function(val, set) {
1515 let values = split(val, /[ \t]+/);
1517 if (length(values) != length(set.types))
1523 for (let i, t in set.types) {
1526 ip = filter(this.parse_subnet(values[i]), a => (a.family == 4));
1528 switch (length(ip) ?? 0) {
1529 case 0: return null;
1531 default: this.warn("Set entry '%s' resolves to multiple addresses, using first one", values[i]);
1534 rv[i] = ("net" in set.fw4types) ? `${ip[0].addr}/${ip[0].bits}` : ip[0].addr;
1538 ip = filter(this.parse_subnet(values[i]), a => (a.family == 6));
1540 switch (length(ip) ?? 0) {
1541 case 0: return null;
1543 case 2: this.warn("Set entry '%s' resolves to multiple addresses, using first one", values[i]);
1546 rv[i] = ("net" in set.fw4types) ? `${ip[0].addr}/${ip[0].bits}` : ip[0].addr;
1551 mac = this.parse_mac(values[i]);
1553 if (!mac || mac.invert)
1559 case 'inet_service':
1560 port = this.parse_port(values[i]);
1562 if (!port || port.invert || port.min != port.max)
1573 return length(rv) ? rv : null;
1576 parse_includetype: function(val) {
1577 return this.parse_enum(val, [
1583 parse_includeposition: function(val) {
1584 return replace(this.parse_enum(val, [
1596 ]), "postpend", "append");
1599 parse_identifier: function(val) {
1600 return match(val, /^[a-zA-Z_.][a-zA-Z0-9\/_.-]*$/)?.[0];
1603 parse_string: function(val) {
1607 parse_opt: function(s, opt, fn, defval, flags) {
1611 if (flags & REQUIRED) {
1612 this.warn_section(s, `option '${opt}' is mandatory but not set`);
1619 if (type(val) == "array") {
1620 this.warn_section(s, `option '${opt}' must not be a list`);
1623 else if (val == null) {
1627 let res = this[fn](val);
1630 this.warn_section(s, `option '${opt}' specifies invalid value '${val}'`);
1637 parse_list: function(s, opt, fn, defval, flags) {
1642 if (flags & REQUIRED) {
1643 this.warn_section(s, `option '${opt}' is mandatory but not set`);
1650 for (val in to_array(val)) {
1651 let res = this[fn](val);
1654 this.warn_section(s, `option '${opt}' specifies invalid value '${val}'`);
1658 if (flags & FLATTEN_LIST)
1659 push(rv, ...to_array(res));
1664 return length(rv) ? rv : null;
1667 quote: function(s, force) {
1668 if (force === true || !match(s, /^([0-9A-Fa-f:.\/-]+)( \. [0-9A-Fa-f:.\/-]+)*$/))
1669 return `"${replace(s, '"', "'")}"`;
1676 return `${a.addr}-${a.addr2}`;
1678 if ((a.family == 4 && a.bits == 32) ||
1679 (a.family == 6 && a.bits == 128))
1683 return `${apply_mask(a.addr, a.bits)}/${a.bits}`;
1685 return `${a.addr}/${a.mask}`;
1688 host: function(a, v6brackets) {
1690 ? `${a.addr}-${a.addr2}`
1691 : (a.family == 6 && v6brackets)
1692 ? `[${apply_mask(a.addr, a.bits)}]` : apply_mask(a.addr, a.bits);
1699 return `${p.min}-${p.max}`;
1702 set: function(v, force) {
1705 v = filter(to_array(v), item => !seen[item]++);
1707 if (force || length(v) != 1)
1708 return `{ ${join(', ', map(v, this.quote))} }`;
1710 return this.quote(v[0]);
1713 concat: function(v) {
1714 return join(' . ', to_array(v));
1717 ipproto: function(family) {
1727 nfproto: function(family, human_readable) {
1730 return human_readable ? "IPv4" : "ipv4";
1733 return human_readable ? "IPv6" : "ipv6";
1736 return human_readable ? "IPv4/IPv6" : null;
1740 l4proto: function(family, proto) {
1741 switch (proto.name) {
1743 switch (family ?? 0) {
1745 return this.set(['icmp', 'ipv6-icmp']);
1756 datetime: function(stamp) {
1757 return sprintf('"%04d-%02d-%02d %02d:%02d:%02d"',
1758 stamp.year, stamp.month, stamp.day,
1759 stamp.hour, stamp.min, stamp.sec);
1762 date: function(stamp) {
1763 return sprintf('"%04d-%02d-%02d"', stamp.year, stamp.month, stamp.day);
1766 datestamp: function(stamp) {
1767 return exists(stamp, 'hour') ? this.datetime(stamp) : this.date(stamp);
1770 time: function(stamp) {
1771 return sprintf('"%02d:%02d:%02d"', stamp.hour, stamp.min, stamp.sec);
1775 return sprintf('0x%x', n);
1778 is_loopback_dev: function(dev) {
1779 return !!(+fs.readfile(`/sys/class/net/${dev}/flags`) & 0x8);
1782 is_loopback_addr: function(addr) {
1783 return (index(addr, "127.") == 0 || addr == "::1" || addr == "::1/128");
1786 filter_loopback_devs: function(devs, invert) {
1787 return null_if_empty(filter(devs, d => (this.is_loopback_dev(d) == invert)));
1790 filter_loopback_addrs: function(addrs, invert) {
1791 return null_if_empty(filter(addrs, a => (this.is_loopback_addr(a) == invert)));
1795 input_policy: function(reject_as_drop) {
1796 return (!reject_as_drop || this.state.defaults.input != 'reject') ? this.state.defaults.input : 'drop';
1799 output_policy: function(reject_as_drop) {
1800 return (!reject_as_drop || this.state.defaults.output != 'reject') ? this.state.defaults.output : 'drop';
1803 forward_policy: function(reject_as_drop) {
1804 return (!reject_as_drop || this.state.defaults.forward != 'reject') ? this.state.defaults.forward : 'drop';
1807 default_option: function(flag) {
1808 return this.state.defaults[flag];
1811 helpers: function() {
1812 return this.state.helpers;
1816 return this.state.zones;
1819 rules: function(chain) {
1820 return filter(this.state.rules, r => (r.chain == chain));
1823 redirects: function(chain) {
1824 return filter(this.state.redirects, r => (r.chain == chain));
1827 ipsets: function() {
1828 return this.state.ipsets;
1831 includes: function(position, chain) {
1837 case 'table-prepend':
1838 case 'table-append':
1843 case 'chain-prepend':
1844 case 'chain-append':
1854 for (let inc in this.state.includes)
1855 if (inc.type == 'nftables' && inc.position == position && (!chain || inc.chain == chain))
1856 push(stmts, `${pad}include "${inc.path}"\n`);
1858 print(length(stmts) > 1 ? join('', stmts) : '');
1861 parse_setfile: function(set, cb) {
1862 let fd = fs.open(set.loadfile, "r");
1865 warn(`Unable to load file '${set.loadfile}' for set '${set.name}': ${fs.error()}\n`);
1871 for (let line = fd.read("line"); length(line); line = fd.read("line")) {
1874 if (length(line) == 0 || ord(line) == 35)
1877 let v = this.parse_ipsetentry(line, set);
1880 this.warn(`Skipping invalid entry '${line}' in file '${set.loadfile}' for set '${set.name}'`);
1894 print_setentries: function(set) {
1896 let printer = (entry) => {
1898 print("\t\telements = {\n");
1902 print("\t\t\t", join(" . ", entry), ",\n");
1905 map(set.entries, printer);
1908 this.parse_setfile(set, printer);
1914 parse_helper: function(data) {
1915 let helper = this.parse_options(data, {
1916 name: [ "string", null, REQUIRED ],
1917 description: [ "string" ],
1918 module: [ "string" ],
1919 family: [ "family" ],
1920 proto: [ "protocol", null, PARSE_LIST | FLATTEN_LIST | NO_INVERT ],
1921 port: [ "port", null, NO_INVERT ]
1924 if (helper === false) {
1925 this.warn("Helper definition '%s' skipped due to invalid options", data.name || data['.name']);
1928 else if (helper.proto.any) {
1929 this.warn("Helper definition '%s' must not specify wildcard protocol", data.name || data['.name']);
1932 else if (length(helper.proto) > 1) {
1933 this.warn("Helper definition '%s' must not specify multiple protocols", data.name || data['.name']);
1937 helper.available = (fs.stat(`/sys/module/${helper.module}`)?.type == "directory");
1939 push(this.state.helpers ||= [], helper);
1942 parse_defaults: function(data) {
1943 if (this.state.defaults) {
1944 this.warn_section(data, ": ignoring duplicate defaults section");
1948 let defs = this.parse_options(data, {
1949 input: [ "policy", "drop" ],
1950 output: [ "policy", "drop" ],
1951 forward: [ "policy", "drop" ],
1953 drop_invalid: [ "bool" ],
1954 tcp_reject_code: [ "reject_code", "tcp-reset" ],
1955 any_reject_code: [ "reject_code", "port-unreachable" ],
1957 syn_flood: [ "bool" ],
1958 synflood_protect: [ "bool" ],
1959 synflood_rate: [ "limit", "25/second" ],
1960 synflood_burst: [ "int", "50" ],
1962 tcp_syncookies: [ "bool", "1" ],
1964 tcp_window_scaling: [ "bool", "1" ],
1966 accept_redirects: [ "bool" ],
1967 accept_source_route: [ "bool" ],
1969 auto_helper: [ "bool", "1" ],
1970 custom_chains: [ "bool", null, UNSUPPORTED ],
1971 disable_ipv6: [ "bool", null, UNSUPPORTED ],
1972 flow_offloading: [ "bool", "0" ],
1973 flow_offloading_hw: [ "bool", "0" ],
1975 auto_includes: [ "bool", "1" ]
1978 if (defs.synflood_protect === null)
1979 defs.synflood_protect = defs.syn_flood;
1981 delete defs.syn_flood;
1983 this.state.defaults = defs;
1986 parse_zone: function(data) {
1987 let zone = this.parse_options(data, {
1988 enabled: [ "bool", "1" ],
1990 name: [ "identifier", null, REQUIRED ],
1991 family: [ "family" ],
1993 network: [ "device", null, PARSE_LIST ],
1994 device: [ "device", null, PARSE_LIST ],
1995 subnet: [ "network", null, PARSE_LIST ],
1997 input: [ "policy", this.state.defaults ? this.state.defaults.input : "drop" ],
1998 output: [ "policy", this.state.defaults ? this.state.defaults.output : "drop" ],
1999 forward: [ "policy", this.state.defaults ? this.state.defaults.forward : "drop" ],
2002 masq_allow_invalid: [ "bool" ],
2003 masq_src: [ "network", null, PARSE_LIST ],
2004 masq_dest: [ "network", null, PARSE_LIST ],
2008 extra: [ "string", null, UNSUPPORTED ],
2009 extra_src: [ "string", null, UNSUPPORTED ],
2010 extra_dest: [ "string", null, UNSUPPORTED ],
2012 mtu_fix: [ "bool" ],
2013 custom_chains: [ "bool", null, UNSUPPORTED ],
2016 log_limit: [ "limit" ],
2018 auto_helper: [ "bool", "1" ],
2019 helper: [ "cthelper", null, PARSE_LIST ],
2021 counter: [ "bool", "1" ]
2024 if (zone === false) {
2025 this.warn_section(data, "skipped due to invalid options");
2028 else if (!zone.enabled) {
2029 this.warn_section(data, "is disabled, ignoring section");
2033 for (let helper in zone.helper) {
2034 if (!helper.available) {
2035 this.warn_section(data, `uses unavailable ct helper '${zone.helper.name}'`);
2039 if (zone.mtu_fix && this.kernel < 0x040a0000) {
2040 this.warn_section(data, "option 'mtu_fix' requires kernel 4.10 or later");
2044 if (this.state.defaults?.auto_helper === false)
2045 zone.auto_helper = false;
2047 let match_devices = [];
2048 let related_physdevs = [];
2049 let related_subnets = [];
2050 let related_ubus_networks = [];
2051 let match_subnets, masq_src_subnets, masq_dest_subnets;
2053 for (let name, net in this.state.networks) {
2054 if (net.zone === zone.name)
2055 push(related_ubus_networks, { invert: false, device: name });
2058 zone.network = [ ...to_array(zone.network), ...related_ubus_networks ];
2060 for (let e in zone.network) {
2061 if (exists(this.state.networks, e.device)) {
2062 let net = this.state.networks[e.device];
2065 push(match_devices, {
2071 if (net.physdev && !e.invert)
2072 push(related_physdevs, net.physdev);
2074 push(related_subnets, ...(net.ipaddrs || []));
2078 push(match_devices, ...to_array(zone.device));
2080 match_subnets = subnets_split_af(zone.subnet);
2081 masq_src_subnets = subnets_split_af(zone.masq_src);
2082 masq_dest_subnets = subnets_split_af(zone.masq_dest);
2084 push(related_subnets, ...(match_subnets[0] || []), ...(match_subnets[1] || []));
2086 let match_rules = [];
2088 let add_rule = (family, devices, subnets, zone) => {
2093 r.devices_pos = null_if_empty(devices[0]);
2094 r.devices_neg = null_if_empty(devices[1]);
2095 r.devices_neg_wildcard = null_if_empty(devices[2]);
2097 r.subnets_pos = map(subnets[0], this.cidr);
2098 r.subnets_neg = map(subnets[1], this.cidr);
2099 r.subnets_masked = subnets[2];
2101 push(match_rules, r);
2104 let family = infer_family(zone.family, [
2105 match_subnets, "subnet list"
2108 if (type(family) == "string") {
2109 this.warn_section(data, `${family}, skipping`);
2113 // group non-inverted device matches into wildcard and non-wildcard ones
2114 let devices = [], plain_devices = [], plain_invert_devices = [], wildcard_invert_devices = [];
2116 for (let device in match_devices) {
2117 let m = match(device.device, /^([^+]*)(\+)?$/);
2120 this.warn_section(data, `skipping invalid wildcard pattern '${device.device}'`);
2124 // filter `+` (match any device) since nftables does not support
2125 // wildcard only matches
2126 if (!device.invert && m[0] == '+')
2129 // replace inverted `+` (match no device) with invalid pattern
2130 if (device.invert && m[0] == '+') {
2131 device.device = '/never/';
2132 device.invert = false;
2135 // replace "name+" matches with "name*"
2136 else if (m[2] == '+')
2137 device.device = m[1] + '*';
2139 device.wildcard = !!m[2];
2141 if (!device.invert && device.wildcard)
2142 push(devices, [ [ device.device ], plain_invert_devices, wildcard_invert_devices ]);
2143 else if (!device.invert)
2144 push(plain_devices, device.device);
2145 else if (device.wildcard)
2146 push(wildcard_invert_devices, device.device);
2148 push(plain_invert_devices, device.device);
2151 if (length(plain_devices))
2154 plain_invert_devices,
2155 wildcard_invert_devices
2157 else if (!length(devices))
2160 plain_invert_devices,
2161 wildcard_invert_devices
2164 // emit zone jump rules for each device group
2165 if (length(match_devices) || length(match_subnets[0]) || length(match_subnets[1])) {
2166 for (let devgroup in devices) {
2167 // check if there's no AF specific bits, in this case we can do AF agnostic matching
2168 if (!family && !length(match_subnets[0]) && !length(match_subnets[1])) {
2169 add_rule(0, devgroup, [], zone);
2172 // we need to emit one or two AF specific rules
2174 if (!family || family == 4)
2175 for (let subnets in subnets_group_by_masking(match_subnets[0]))
2176 add_rule(4, devgroup, subnets, zone);
2178 if (!family || family == 6)
2179 for (let subnets in subnets_group_by_masking(match_subnets[1]))
2180 add_rule(6, devgroup, subnets, zone);
2185 zone.family = family;
2187 zone.match_rules = match_rules;
2189 zone.masq4_src_subnets = subnets_group_by_masking(masq_src_subnets[0]);
2190 zone.masq4_dest_subnets = subnets_group_by_masking(masq_dest_subnets[0]);
2192 zone.masq6_src_subnets = subnets_group_by_masking(masq_src_subnets[1]);
2193 zone.masq6_dest_subnets = subnets_group_by_masking(masq_dest_subnets[1]);
2196 zone.sflags[zone.input] = true;
2199 zone.dflags[zone.output] = true;
2200 zone.dflags[zone.forward] = true;
2202 zone.match_devices = map(filter(match_devices, d => !d.invert), d => d.device);
2203 zone.match_subnets = map(filter(related_subnets, s => !s.invert && s.bits != -1), this.cidr);
2205 zone.related_subnets = related_subnets;
2206 zone.related_physdevs = related_physdevs;
2208 if (zone.masq || zone.masq6)
2209 zone.dflags.snat = true;
2211 if ((zone.auto_helper && !(zone.masq || zone.masq6)) || length(zone.helper)) {
2212 zone.dflags.helper = true;
2214 for (let helper in (length(zone.helper) ? zone.helper : this.state.helpers)) {
2215 if (!helper.available)
2218 for (let proto in helper.proto) {
2219 push(this.state.rules ||= [], {
2220 chain: `helper_${zone.name}`,
2221 family: helper.family,
2222 name: helper.description || helper.name,
2225 dports_pos: [ this.port(helper.port) ],
2233 push(this.state.zones ||= [], zone);
2236 parse_forwarding: function(data) {
2237 let fwd = this.parse_options(data, {
2238 enabled: [ "bool", "1" ],
2241 family: [ "family" ],
2243 src: [ "zone_ref", null, REQUIRED ],
2244 dest: [ "zone_ref", null, REQUIRED ]
2247 if (fwd === false) {
2248 this.warn_section(data, "skipped due to invalid options");
2251 else if (!fwd.enabled) {
2252 this.warn_section(data, "is disabled, ignoring section");
2256 let add_rule = (family, fwd) => {
2261 proto: { any: true }
2264 f.name ||= `Accept ${fwd.src.any ? "any" : fwd.src.zone.name} to ${fwd.dest.any ? "any" : fwd.dest.zone.name} ${family ? `${this.nfproto(family, true)} ` : ''}forwarding`;
2265 f.chain = fwd.src.any ? "forward" : `forward_${fwd.src.zone.name}`;
2268 f.target = "accept";
2270 f.jump_chain = `accept_to_${fwd.dest.zone.name}`;
2272 push(this.state.rules ||= [], f);
2276 /* inherit family restrictions from related zones */
2277 let family = infer_family(fwd.family, [
2278 fwd.src?.zone, "source zone",
2279 fwd.dest?.zone, "destination zone"
2282 if (type(family) == "string") {
2283 this.warn_section(data, `${family}, skipping`);
2287 add_rule(family, fwd);
2290 fwd.dest.zone.dflags.accept = true;
2293 parse_rule: function(data) {
2294 let rule = this.parse_options(data, {
2295 enabled: [ "bool", "1" ],
2297 name: [ "string", this.section_id(data[".name"]) ],
2298 _name: [ "string", null, DEPRECATED ],
2299 family: [ "family" ],
2301 src: [ "zone_ref" ],
2302 dest: [ "zone_ref" ],
2304 device: [ "device", null, NO_INVERT ],
2305 direction: [ "direction" ],
2307 ipset: [ "setmatch" ],
2308 helper: [ "cthelper" ],
2309 set_helper: [ "cthelper", null, NO_INVERT ],
2311 proto: [ "protocol", "tcpudp", PARSE_LIST | FLATTEN_LIST ],
2313 src_ip: [ "network", null, PARSE_LIST ],
2314 src_mac: [ "mac", null, PARSE_LIST ],
2315 src_port: [ "port", null, PARSE_LIST ],
2317 dest_ip: [ "network", null, PARSE_LIST ],
2318 dest_port: [ "port", null, PARSE_LIST ],
2320 icmp_type: [ "icmptype", null, PARSE_LIST ],
2321 extra: [ "string", null, UNSUPPORTED ],
2324 limit_burst: [ "int" ],
2326 utc_time: [ "bool" ],
2327 start_date: [ "date" ],
2328 stop_date: [ "date" ],
2329 start_time: [ "time" ],
2330 stop_time: [ "time" ],
2331 weekdays: [ "weekdays" ],
2332 monthdays: [ "monthdays", null, UNSUPPORTED ],
2335 set_mark: [ "mark", null, NO_INVERT ],
2336 set_xmark: [ "mark", null, NO_INVERT ],
2339 set_dscp: [ "dscp", null, NO_INVERT ],
2341 counter: [ "bool", "1" ],
2343 log_limit: [ "limit" ],
2345 target: [ "target" ]
2348 if (rule === false) {
2349 this.warn_section(data, "skipped due to invalid options");
2352 else if (!rule.enabled) {
2353 this.warn_section(data, "is disabled, ignoring section");
2357 if (rule.target in ["helper", "notrack"] && (!rule.src || !rule.src.zone)) {
2358 this.warn_section(data, `must specify a source zone for target '${rule.target}'`);
2361 else if (rule.target == "dscp" && !rule.set_dscp) {
2362 this.warn_section(data, "must specify option 'set_dscp' for target 'dscp'");
2365 else if (rule.target == "mark" && !rule.set_mark && !rule.set_xmark) {
2366 this.warn_section(data, "must specify option 'set_mark' or 'set_xmark' for target 'mark'");
2369 else if (rule.target == "helper" && !rule.set_helper) {
2370 this.warn_section(data, "must specify option 'set_helper' for target 'helper'");
2373 else if (rule.device?.any) {
2374 this.warn_section(data, "must not specify '*' as device");
2378 switch (this.parse_bool(rule.log)) {
2380 rule.log = `${rule.name}: `;
2390 ipset = filter(this.state.ipsets, s => (s.name == rule.ipset.name))?.[0];
2393 this.warn_section(data, `references unknown set '${rule.ipset.name}'`);
2397 if (('inet_service' in ipset.types) && !ensure_tcpudp(rule.proto)) {
2398 this.warn_section(data, "references named set with port match but no UDP/TCP protocol, ignoring section");
2403 let need_src_action_chain = (rule) => (rule.src?.zone?.log && rule.target != "accept");
2405 let add_rule = (family, proto, saddrs, daddrs, sports, dports, icmptypes, icmpcodes, ipset, rule) => {
2411 has_addrs: !!(saddrs[0] || saddrs[1] || saddrs[2] || daddrs[0] || daddrs[1] || daddrs[2]),
2412 has_ports: !!(length(sports) || length(dports)),
2413 saddrs_pos: map(saddrs[0], this.cidr),
2414 saddrs_neg: map(saddrs[1], this.cidr),
2415 saddrs_masked: saddrs[2],
2416 daddrs_pos: map(daddrs[0], this.cidr),
2417 daddrs_neg: map(daddrs[1], this.cidr),
2418 daddrs_masked: daddrs[2],
2419 sports_pos: map(filter_pos(sports), this.port),
2420 sports_neg: map(filter_neg(sports), this.port),
2421 dports_pos: map(filter_pos(dports), this.port),
2422 dports_neg: map(filter_neg(dports), this.port),
2423 smacs_pos: map(filter_pos(rule.src_mac), m => m.mac),
2424 smacs_neg: map(filter_neg(rule.src_mac), m => m.mac),
2425 icmp_types: map(icmptypes, i => (family == 4 ? i.type : i.type6)),
2426 icmp_codes: map(icmpcodes, ic => `${(family == 4) ? ic.type : ic.type6} . ${(family == 4) ? ic.code_min : ic.code6_min}`)
2429 if (!length(r.icmp_types))
2430 delete r.icmp_types;
2432 if (!length(r.icmp_codes))
2433 delete r.icmp_codes;
2437 invert: r.set_mark.invert,
2438 mark: r.set_mark.mark,
2439 mask: r.set_mark.mark | r.set_mark.mask
2445 let set_types = map_setmatch(ipset, rule.ipset, proto.name);
2447 if (set_types !== set_types) {
2448 this.warn_section(data, "destination MAC address matching not supported");
2450 } else if (set_types) {
2451 r.ipset = { ...r.ipset, fields: set_types };
2454 if (r.target == "notrack") {
2455 r.chain = `notrack_${r.src.zone.name}`;
2456 r.src.zone.dflags.notrack = true;
2458 else if (r.target == "helper") {
2459 r.chain = `helper_${r.src.zone.name}`;
2460 r.src.zone.dflags.helper = true;
2462 else if (r.target == "mark" || r.target == "dscp") {
2463 if ((r.src?.any && r.dest?.any) || (r.src?.zone && r.dest?.zone))
2464 r.chain = "mangle_forward";
2465 else if (r.src?.any && r.dest?.zone)
2466 r.chain = "mangle_postrouting";
2467 else if (r.src?.zone && r.dest?.any)
2468 r.chain = "mangle_prerouting";
2469 else if (r.src && !r.dest)
2470 r.chain = "mangle_input";
2472 r.chain = "mangle_output";
2475 r.src.zone.dflags[r.target] = true;
2476 r.iifnames = null_if_empty(r.src.zone.match_devices);
2480 r.dest.zone.dflags[r.target] = true;
2481 r.oifnames = null_if_empty(r.dest.zone.match_devices);
2489 r.chain = `${r.dest ? "forward" : "input"}_${r.src.zone.name}`;
2491 r.chain = r.dest ? "forward" : "input";
2494 if (r.dest && !r.src) {
2496 r.chain = sprintf("output_%s", r.dest.zone.name);
2501 if (r.target && r.dest && !r.dest.any) {
2502 r.jump_chain = `${r.target}_to_${r.dest.zone.name}`;
2503 r.dest.zone.dflags[r.target] = true;
2505 else if (r.target && need_src_action_chain(r)) {
2506 r.jump_chain = `${r.target}_from_${r.src.zone.name}`;
2507 r.src.zone.sflags[r.target] = true;
2509 else if (r.target == "reject")
2510 r.jump_chain = "handle_reject";
2514 r[r.direction ? "oifnames" : "iifnames"] = [ r.device.device ];
2516 push(this.state.rules ||= [], r);
2519 for (let proto in rule.proto) {
2520 let sip, dip, sports, dports, itypes4, itypes6;
2521 let family = rule.family;
2523 switch (proto.name) {
2525 itypes4 = filter(rule.icmp_type || [], family_is_ipv4);
2526 itypes6 = filter(rule.icmp_type || [], family_is_ipv6);
2531 itypes6 = filter(rule.icmp_type || [], family_is_ipv6);
2536 sports = rule.src_port;
2537 dports = rule.dest_port;
2541 sip = subnets_split_af(rule.src_ip);
2542 dip = subnets_split_af(rule.dest_ip);
2544 family = infer_family(family, [
2547 dip, "destination IP",
2548 rule.src?.zone, "source zone",
2549 rule.dest?.zone, "destination zone",
2550 rule.helper, "helper match",
2551 rule.set_helper, "helper to set"
2554 if (type(family) == "string") {
2555 this.warn_section(data, `${family}, skipping`);
2559 let has_ipv4_specifics = (length(sip[0]) || length(dip[0]) || length(itypes4) || rule.dscp !== null);
2560 let has_ipv6_specifics = (length(sip[1]) || length(dip[1]) || length(itypes6) || rule.dscp !== null);
2562 /* if no family was configured, infer target family from IP addresses */
2563 if (family === null) {
2564 if (has_ipv4_specifics && !has_ipv6_specifics)
2566 else if (has_ipv6_specifics && !has_ipv4_specifics)
2572 /* check if there's no AF specific bits, in this case we can do an AF agnostic rule */
2573 if (!family && rule.target != "dscp" && !has_ipv4_specifics && !has_ipv6_specifics) {
2574 add_rule(0, proto, [], [], sports, dports, null, null, null, rule);
2577 /* we need to emit one or two AF specific rules */
2579 if (family == 0 || family == 4) {
2580 let icmp_types = filter(itypes4, i => (i.code_min == 0 && i.code_max == 0xFF));
2581 let icmp_codes = filter(itypes4, i => (i.code_min != 0 || i.code_max != 0xFF));
2583 for (let saddrs in subnets_group_by_masking(sip[0])) {
2584 for (let daddrs in subnets_group_by_masking(dip[0])) {
2585 if (length(icmp_types) || (!length(icmp_types) && !length(icmp_codes)))
2586 add_rule(4, proto, saddrs, daddrs, sports, dports, icmp_types, null, ipset, rule);
2588 if (length(icmp_codes))
2589 add_rule(4, proto, saddrs, daddrs, sports, dports, null, icmp_codes, ipset, rule);
2594 if (family == 0 || family == 6) {
2595 let icmp_types = filter(itypes6, i => (i.code_min == 0 && i.code_max == 0xFF));
2596 let icmp_codes = filter(itypes6, i => (i.code_min != 0 || i.code_max != 0xFF));
2598 for (let saddrs in subnets_group_by_masking(sip[1])) {
2599 for (let daddrs in subnets_group_by_masking(dip[1])) {
2600 if (length(icmp_types) || (!length(icmp_types) && !length(icmp_codes)))
2601 add_rule(6, proto, saddrs, daddrs, sports, dports, icmp_types, null, ipset, rule);
2603 if (length(icmp_codes))
2604 add_rule(6, proto, saddrs, daddrs, sports, dports, null, icmp_codes, ipset, rule);
2612 parse_redirect: function(data) {
2613 let redir = this.parse_options(data, {
2614 enabled: [ "bool", "1" ],
2616 name: [ "string", this.section_id(data[".name"]) ],
2617 _name: [ "string", null, DEPRECATED ],
2618 family: [ "family" ],
2620 src: [ "zone_ref" ],
2621 dest: [ "zone_ref" ],
2623 ipset: [ "setmatch" ],
2624 helper: [ "cthelper", null, NO_INVERT ],
2626 proto: [ "protocol", "tcpudp", PARSE_LIST | FLATTEN_LIST ],
2628 src_ip: [ "network" ],
2629 src_mac: [ "mac", null, PARSE_LIST ],
2630 src_port: [ "port" ],
2632 src_dip: [ "network" ],
2633 src_dport: [ "port" ],
2635 dest_ip: [ "network" ],
2636 dest_port: [ "port" ],
2638 extra: [ "string", null, UNSUPPORTED ],
2641 limit_burst: [ "int" ],
2643 utc_time: [ "bool" ],
2644 start_date: [ "date" ],
2645 stop_date: [ "date" ],
2646 start_time: [ "time" ],
2647 stop_time: [ "time" ],
2648 weekdays: [ "weekdays" ],
2649 monthdays: [ "monthdays", null, UNSUPPORTED ],
2653 reflection: [ "bool", "1" ],
2654 reflection_src: [ "reflection_source", "internal" ],
2656 reflection_zone: [ "zone_ref", null, PARSE_LIST ],
2658 counter: [ "bool", "1" ],
2660 log_limit: [ "limit" ],
2662 target: [ "target", "dnat" ]
2665 if (redir === false) {
2666 this.warn_section(data, "skipped due to invalid options");
2669 else if (!redir.enabled) {
2670 this.warn_section(data, "is disabled, ignoring section");
2674 if (!(redir.target in ["dnat", "snat"])) {
2675 this.warn_section(data, "has invalid target specified, defaulting to dnat");
2676 redir.target = "dnat";
2679 switch (this.parse_bool(redir.log)) {
2681 redir.log = `${redir.name}: `;
2691 ipset = filter(this.state.ipsets, s => (s.name == redir.ipset.name))?.[0];
2694 this.warn_section(data, `references unknown set '${redir.ipset.name}'`);
2698 if (('inet_service' in ipset.types) && !ensure_tcpudp(redir.proto)) {
2699 this.warn_section(data, "references named set with port match but no UDP/TCP protocol, ignoring section");
2704 let resolve_dest = (redir) => {
2705 for (let zone in this.state.zones) {
2706 for (let zone_addr in zone.related_subnets) {
2707 for (let dest_addr in redir.dest_ip.addrs) {
2708 if (dest_addr.family != zone_addr.family)
2711 let a = apply_mask(dest_addr.addr, zone_addr.mask);
2712 let b = apply_mask(zone_addr.addr, zone_addr.mask);
2730 if (redir.target == "dnat") {
2732 return this.warn_section(data, "has no source specified");
2733 else if (redir.src.any)
2734 return this.warn_section(data, "must not have source '*' for dnat target");
2735 else if (redir.dest_ip && redir.dest_ip.invert)
2736 return this.warn_section(data, "must not specify a negated 'dest_ip' value");
2737 else if (redir.dest_ip && length(filter(redir.dest_ip.addrs, a => a.bits == -1)))
2738 return this.warn_section(data, "must not use non-contiguous masks in 'dest_ip'");
2740 if (!redir.dest && redir.dest_ip && resolve_dest(redir))
2741 this.warn_section(data, `does not specify a destination, assuming '${redir.dest.zone.name}'`);
2743 if (!redir.dest_port)
2744 redir.dest_port = redir.src_dport;
2746 if (redir.reflection && redir.dest?.zone && redir.src.zone.masq) {
2747 redir.dest.zone.dflags.accept = true;
2748 redir.dest.zone.dflags.dnat = true;
2749 redir.dest.zone.dflags.snat = true;
2753 redir.src.zone.dflags.helper = true;
2755 redir.src.zone.dflags[redir.target] = true;
2759 return this.warn_section(data, "has no destination specified");
2760 else if (redir.dest.any)
2761 return this.warn_section(data, "must not have destination '*' for snat target");
2762 else if (!redir.src_dip)
2763 return this.warn_section(data, "has no 'src_dip' option specified");
2764 else if (redir.src_dip.invert)
2765 return this.warn_section(data, "must not specify a negated 'src_dip' value");
2766 else if (length(filter(redir.src_dip.addrs, a => a.bits == -1)))
2767 return this.warn_section(data, "must not use non-contiguous masks in 'src_dip'");
2768 else if (redir.src_mac)
2769 return this.warn_section(data, "must not use 'src_mac' option for snat target");
2770 else if (redir.helper)
2771 return this.warn_section(data, "must not use 'helper' option for snat target");
2773 redir.dest.zone.dflags[redir.target] = true;
2776 let add_rule = (family, proto, saddrs, daddrs, raddrs, sport, dport, rport, ipset, redir) => {
2782 has_addrs: !!(saddrs[0] || saddrs[1] || saddrs[2] || daddrs[0] || daddrs[1] || daddrs[2]),
2783 has_ports: !!(sport || dport || rport),
2784 saddrs_pos: map(saddrs[0], this.cidr),
2785 saddrs_neg: map(saddrs[1], this.cidr),
2786 saddrs_masked: saddrs[2],
2787 daddrs_pos: map(daddrs[0], this.cidr),
2788 daddrs_neg: map(daddrs[1], this.cidr),
2789 daddrs_masked: daddrs[2],
2790 sports_pos: map(filter_pos(to_array(sport)), this.port),
2791 sports_neg: map(filter_neg(to_array(sport)), this.port),
2792 dports_pos: map(filter_pos(to_array(dport)), this.port),
2793 dports_neg: map(filter_neg(to_array(dport)), this.port),
2794 smacs_pos: map(filter_pos(redir.src_mac), m => m.mac),
2795 smacs_neg: map(filter_neg(redir.src_mac), m => m.mac),
2797 raddr: raddrs ? raddrs[0] : null,
2801 let set_types = map_setmatch(ipset, redir.ipset, proto.name);
2803 if (set_types !== set_types) {
2804 this.warn_section(data, "destination MAC address matching not supported");
2806 } else if (set_types) {
2807 r.ipset = { ...r.ipset, fields: set_types };
2812 r.chain = `dstnat_${r.src.zone.name}`;
2813 r.src.zone.dflags.dnat = true;
2816 r.target = "redirect";
2821 r.chain = `srcnat_${r.dest.zone.name}`;
2822 r.dest.zone.dflags.snat = true;
2826 push(this.state.redirects ||= [], r);
2829 let to_hostaddr = (a) => {
2830 let bits = (a.family == 4) ? 32 : 128;
2834 addr: apply_mask(a.addr, bits),
2839 for (let proto in redir.proto) {
2840 let sip, dip, rip, iip, eip, refip, sport, dport, rport;
2841 let family = redir.family;
2843 if (proto.name == "ipv6-icmp")
2846 switch (redir.target) {
2848 sip = subnets_split_af(redir.src_ip);
2849 dip = subnets_split_af(redir.src_dip);
2850 rip = subnets_split_af(redir.dest_ip);
2852 switch (proto.name) {
2855 sport = redir.src_port;
2856 dport = redir.src_dport;
2857 rport = redir.dest_port;
2864 sip = subnets_split_af(redir.src_ip);
2865 dip = subnets_split_af(redir.dest_ip);
2866 rip = subnets_split_af(redir.src_dip);
2868 switch (proto.name) {
2871 sport = redir.src_port;
2872 dport = redir.dest_port;
2873 rport = redir.src_dport;
2880 family = infer_family(family, [
2883 dip, "destination IP",
2885 redir.src?.zone, "source zone",
2886 redir.dest?.zone, "destination zone",
2887 redir.helper, "helper match"
2890 if (type(family) == "string") {
2891 this.warn_section(data, `${family}, skipping`);
2895 /* build reflection rules */
2896 if (redir.target == "dnat" && redir.reflection &&
2897 (length(rip[0]) || length(rip[1])) && redir.src?.zone && redir.dest?.zone) {
2899 name: `${redir.name} (reflection)`,
2901 helper: redir.helper,
2903 // XXX: this likely makes no sense for reflection rules
2904 //src_mac: redir.src_mac,
2907 limit_burst: redir.limit_burst,
2909 start_date: redir.start_date,
2910 stop_date: redir.stop_date,
2911 start_time: redir.start_time,
2912 stop_time: redir.stop_time,
2913 weekdays: redir.weekdays,
2918 let eaddrs = length(dip) ? dip : subnets_split_af({ addrs: map(redir.src.zone.related_subnets, to_hostaddr) });
2919 let rzones = length(redir.reflection_zone) ? redir.reflection_zone : [ redir.dest ];
2921 for (let rzone in rzones) {
2922 if (!is_family(rzone, family)) {
2923 this.warn_section(data,
2924 `is restricted to IPv${family} but referenced reflection zone is IPv${rzone.family} only, skipping`);
2928 let iaddrs = subnets_split_af({ addrs: rzone.zone.related_subnets });
2929 let refaddrs = (redir.reflection_src == "internal") ? iaddrs : eaddrs;
2931 for (let i = 0; i <= 1; i++) {
2932 if (redir.src.zone[i ? "masq6" : "masq"] && length(rip[i])) {
2933 let snat_addr = refaddrs[i]?.[0];
2935 /* For internal reflection sources try to find a suitable candiate IP
2936 * among the reflection zone subnets which is within the same subnet
2937 * as the original DNAT destination. If we can't find any matching
2938 * one then simply take the first candidate. */
2939 if (redir.reflection_src == "internal") {
2940 for (let zone_addr in rzone.zone.related_subnets) {
2941 if (zone_addr.family != rip[i][0].family)
2944 let r = apply_mask(rip[i][0].addr, zone_addr.mask);
2945 let a = apply_mask(zone_addr.addr, zone_addr.mask);
2950 snat_addr = zone_addr;
2956 this.warn_section(data, `${redir.reflection_src || "external"} rewrite IP cannot be determined, disabling reflection`);
2958 else if (!length(iaddrs[i])) {
2959 this.warn_section(data, "internal address range cannot be determined, disabling reflection");
2961 else if (!length(eaddrs[i])) {
2962 this.warn_section(data, "external address range cannot be determined, disabling reflection");
2965 refredir.src = rzone;
2966 refredir.dest = null;
2967 refredir.target = "dnat";
2969 for (let saddrs in subnets_group_by_masking(iaddrs[i]))
2970 for (let daddrs in subnets_group_by_masking(eaddrs[i]))
2971 add_rule(i ? 6 : 4, proto, saddrs, daddrs, rip[i], sport, dport, rport, null, refredir);
2973 refredir.src = null;
2974 refredir.dest = rzone;
2975 refredir.target = "snat";
2977 for (let daddrs in subnets_group_by_masking(rip[i]))
2978 for (let saddrs in subnets_group_by_masking(iaddrs[i]))
2979 add_rule(i ? 6 : 4, proto, saddrs, daddrs, [ to_hostaddr(snat_addr) ], null, rport, null, null, refredir);
2986 if (length(rip[0]) > 1 || length(rip[1]) > 1)
2987 this.warn_section(data, "specifies multiple rewrite addresses, using only first one");
2989 let has_ip4_addr = length(sip[0]) || length(dip[0]) || length(rip[0]),
2990 has_ip6_addr = length(sip[1]) || length(dip[1]) || length(rip[1]),
2991 has_any_addr = has_ip4_addr || has_ip6_addr;
2993 /* check if there's no AF specific bits, in this case we can do an AF agnostic rule */
2994 if (!family && !has_any_addr) {
2995 /* for backwards compatibility, treat unspecified family as IPv4 unless user explicitly requested any (0) */
2999 add_rule(family, proto, [], [], null, sport, dport, rport, null, redir);
3002 /* we need to emit one or two AF specific rules */
3004 if ((!family || family == 4) && (!has_any_addr || has_ip4_addr)) {
3005 for (let saddrs in subnets_group_by_masking(sip[0]))
3006 for (let daddrs in subnets_group_by_masking(dip[0]))
3007 add_rule(4, proto, saddrs, daddrs, rip[0], sport, dport, rport, ipset, redir);
3010 if ((!family || family == 6) && (!has_any_addr || has_ip6_addr)) {
3011 for (let saddrs in subnets_group_by_masking(sip[1]))
3012 for (let daddrs in subnets_group_by_masking(dip[1]))
3013 add_rule(6, proto, saddrs, daddrs, rip[1], sport, dport, rport, ipset, redir);
3019 parse_nat: function(data) {
3020 let snat = this.parse_options(data, {
3021 enabled: [ "bool", "1" ],
3023 name: [ "string", this.section_id(data[".name"]) ],
3024 family: [ "family" ],
3026 src: [ "zone_ref" ],
3027 device: [ "string" ],
3029 ipset: [ "setmatch", null, UNSUPPORTED ],
3031 proto: [ "protocol", "all", PARSE_LIST | FLATTEN_LIST ],
3033 src_ip: [ "network" ],
3034 src_port: [ "port" ],
3036 snat_ip: [ "network", null, NO_INVERT ],
3037 snat_port: [ "port", null, NO_INVERT ],
3039 dest_ip: [ "network" ],
3040 dest_port: [ "port" ],
3042 extra: [ "string", null, UNSUPPORTED ],
3045 limit_burst: [ "int" ],
3047 connlimit_ports: [ "bool" ],
3049 utc_time: [ "bool" ],
3050 start_date: [ "date" ],
3051 stop_date: [ "date" ],
3052 start_time: [ "time" ],
3053 stop_time: [ "time" ],
3054 weekdays: [ "weekdays" ],
3055 monthdays: [ "monthdays", null, UNSUPPORTED ],
3059 counter: [ "bool", "1" ],
3062 target: [ "target", "masquerade" ]
3065 if (snat === false) {
3066 this.warn_section(data, "skipped due to invalid options");
3069 else if (!snat.enabled) {
3070 this.warn_section(data, "is disabled, ignoring section");
3074 if (!(snat.target in ["accept", "snat", "masquerade"])) {
3075 this.warn_section(data, "has invalid target specified, defaulting to masquerade");
3076 snat.target = "masquerade";
3079 if (snat.target == "snat" && !snat.snat_ip && !snat.snat_port) {
3080 this.warn_section(data, "needs either 'snat_ip' or 'snat_port' for target snat, ignoring section");
3083 else if (snat.target != "snat" && snat.snat_ip) {
3084 this.warn_section(data, "must not use 'snat_ip' for non-snat target, ignoring section");
3087 else if (snat.target != "snat" && snat.snat_port) {
3088 this.warn_section(data, "must not use 'snat_port' for non-snat target, ignoring section");
3092 if ((snat.snat_port || snat.src_port || snat.dest_port) && !ensure_tcpudp(snat.proto)) {
3093 this.warn_section(data, "specifies ports but no UDP/TCP protocol, ignoring section");
3097 if (snat.snat_ip && length(filter(snat.snat_ip.addrs, a => a.bits == -1 || a.invert))) {
3098 this.warn_section(data, "must not use inversion or non-contiguous masks in 'snat_ip', ignoring section");
3102 switch (this.parse_bool(snat.log)) {
3104 snat.log = `${snat.name}: `;
3111 let add_rule = (family, proto, saddrs, daddrs, raddrs, sport, dport, rport, snat) => {
3117 has_addrs: !!(saddrs[0] || saddrs[1] || saddrs[2] || daddrs[0] || daddrs[1] || daddrs[2]),
3118 has_ports: !!(sport || dport),
3119 saddrs_pos: map(saddrs[0], this.cidr),
3120 saddrs_neg: map(saddrs[1], this.cidr),
3121 saddrs_masked: saddrs[2],
3122 daddrs_pos: map(daddrs[0], this.cidr),
3123 daddrs_neg: map(daddrs[1], this.cidr),
3124 daddrs_masked: daddrs[2],
3125 sports_pos: map(filter_pos(to_array(sport)), this.port),
3126 sports_neg: map(filter_neg(to_array(sport)), this.port),
3127 dports_pos: map(filter_pos(to_array(dport)), this.port),
3128 dports_neg: map(filter_neg(to_array(dport)), this.port),
3130 raddr: raddrs ? raddrs[0] : null,
3133 chain: snat.src?.zone ? `srcnat_${snat.src.zone.name}` : "srcnat"
3136 push(this.state.redirects ||= [], n);
3139 for (let proto in snat.proto) {
3140 let sip, dip, rip, sport, dport, rport;
3141 let family = snat.family;
3143 sip = subnets_split_af(snat.src_ip);
3144 dip = subnets_split_af(snat.dest_ip);
3145 rip = subnets_split_af(snat.snat_ip);
3147 switch (proto.name) {
3150 sport = snat.src_port;
3151 dport = snat.dest_port;
3152 rport = snat.snat_port;
3156 if (length(rip[0]) > 1 || length(rip[1]) > 1)
3157 this.warn_section(data, "specifies multiple rewrite addresses, using only first one");
3159 family = infer_family(family, [
3161 dip, "destination IP",
3163 snat.src?.zone, "source zone"
3166 if (type(family) == "string") {
3167 this.warn_section(data, `${family}, skipping`);
3172 snat.src.zone.dflags.snat = true;
3174 /* if no family was configured, infer target family from IP addresses */
3175 if (family === null) {
3176 if ((length(sip[0]) || length(dip[0]) || length(rip[0])) && !length(sip[1]) && !length(dip[1]) && !length(rip[1]))
3178 else if ((length(sip[1]) || length(dip[1]) || length(rip[1])) && !length(sip[0]) && !length(dip[0]) && !length(rip[0]))
3181 family = 4; /* default to IPv4 only for backwards compatibility, unless an explict family any was configured */
3184 /* check if there's no AF specific bits, in this case we can do an AF agnostic rule */
3185 if (!family && !length(sip[0]) && !length(sip[1]) && !length(dip[0]) && !length(dip[1]) && !length(rip[0]) && !length(rip[1])) {
3186 add_rule(0, proto, [], [], null, sport, dport, rport, snat);
3189 /* we need to emit one or two AF specific rules */
3191 if (family == 0 || family == 4)
3192 for (let saddr in subnets_group_by_masking(sip[0]))
3193 for (let daddr in subnets_group_by_masking(dip[0]))
3194 add_rule(4, proto, saddr, daddr, rip[0], sport, dport, rport, snat);
3196 if (family == 0 || family == 6)
3197 for (let saddr in subnets_group_by_masking(sip[1]))
3198 for (let daddr in subnets_group_by_masking(dip[1]))
3199 add_rule(6, proto, saddr, daddr, rip[1], sport, dport, rport, snat);
3204 parse_include: function(data) {
3205 let inc = this.parse_options(data, {
3206 enabled: [ "bool", "1" ],
3208 path: [ "string", null, REQUIRED ],
3209 type: [ "includetype", "script" ],
3211 fw4_compatible: [ "bool", data.path != "/etc/firewall.user" ],
3213 family: [ "family", null, UNSUPPORTED ],
3214 reload: [ "bool", null, UNSUPPORTED ],
3216 position: [ "includeposition" ],
3217 chain: [ "identifier" ]
3221 this.warn_section(data, "is disabled, ignoring section");
3225 if (inc.type == "script" && !inc.fw4_compatible) {
3226 this.warn_section(data, "is not marked as compatible with fw4, ignoring section");
3227 this.warn_section(data, "requires 'option fw4_compatible 1' to be considered compatible");
3231 for (let opt in [ "table", "chain", "position" ]) {
3232 if (inc.type != "nftables" && inc[opt]) {
3233 this.warn_section(data, `must not specify '${opt}' for non-nftables includes, ignoring section`);
3238 switch (inc.position ??= 'table-append') {
3239 case 'ruleset-prepend':
3240 case 'ruleset-append':
3241 case 'table-prepend':
3242 case 'table-append':
3244 this.warn_section(data, `specifies 'chain' which has no effect for position ${inc.position}`);
3249 case 'chain-prepend':
3250 case 'chain-append':
3252 this.warn_section(data, `must specify 'chain' for position ${inc.position}, ignoring section`);
3259 let path = fs.readlink(inc.path) ?? inc.path;
3261 if (!fs.access(path)) {
3262 this.warn_section(data, `specifies unreachable path '${path}', ignoring section`);
3267 this.warn(`Automatically including '${path}'`);
3269 push(this.state.includes ||= [], { ...inc, path });
3272 parse_ipset: function(data) {
3273 let ipset = this.parse_options(data, {
3274 enabled: [ "bool", "1" ],
3275 reload_set: [ "bool" ],
3276 counters: [ "bool" ],
3277 comment: [ "string" ],
3279 name: [ "identifier", null, REQUIRED ],
3280 family: [ "family", "4" ],
3282 storage: [ "string", null, UNSUPPORTED ],
3283 match: [ "ipsettype", null, PARSE_LIST ],
3285 iprange: [ "string", null, UNSUPPORTED ],
3286 portrange: [ "string", null, UNSUPPORTED ],
3288 netmask: [ "int", null, UNSUPPORTED ],
3290 hashsize: [ "int", null, UNSUPPORTED ],
3291 timeout: [ "int", "-1" ],
3293 external: [ "string", null, UNSUPPORTED ],
3295 entry: [ "string", null, PARSE_LIST ],
3296 loadfile: [ "string" ]
3299 if (ipset === false) {
3300 this.warn_section(data, "skipped due to invalid options");
3303 else if (!ipset.enabled) {
3304 this.warn_section(data, "is disabled, ignoring section");
3308 if (ipset.family == 0) {
3309 this.warn_section(data, "must not specify family 'any'");
3312 else if (!length(ipset.match)) {
3313 this.warn_section(data, "has no datatypes assigned");
3317 let dirs = map(ipset.match, m => m[0]),
3318 types = map(ipset.match, m => m[1]),
3321 if ("set" in types) {
3322 this.warn_section(data, "match type 'set' is not supported");
3326 if ("net" in types) {
3327 if (this.kernel < 0x05060000) {
3328 this.warn_section(data, "match type 'net' requires kernel 5.6 or later");
3340 types: map(types, (t) => {
3344 return (ipset.family == 4) ? 'ipv4_addr' : 'ipv6_addr';
3347 return 'ether_addr';
3350 return 'inet_service';
3359 push(s.flags ??= [], 'interval');
3362 push(s.flags ??= [], 'timeout');
3364 s.entries = filter(map(ipset.entry, (e) => {
3365 let v = this.parse_ipsetentry(e, s);
3368 this.warn_section(data, `ignoring invalid ipset entry '${e}'`);
3371 }), (e) => (e != null));
3373 push(this.state.ipsets ||= [], s);