1
- From d4081c498ddca184578903fe5199d390bbc0707b Mon Sep 17 00:00:00 2001
1
+ From aa3b56e289fba7425e649a608c333622ffd9c367 Mon Sep 17 00:00:00 2001
2
2
From: Syrone Wong <wong.syrone@gmail.com>
3
3
Date: Sat, 9 Apr 2022 13:24:19 +0800
4
4
Subject: [PATCH] firewall4: add fullcone support
@@ -8,96 +8,82 @@ fullcone is drop-in replacement of masq for non-udp traffic
8
8
add runtime fullcone rule check, disable it globally if fullcone expr is
9
9
invalid
10
10
11
- defaults.fullcone is the global switch, while zone.fullcone4 and
12
- zone.fullcone6 are switches for IPv4 and IPv6 respectively, most
13
- IPv6 traffic do NOT need this FullCone NAT functionality.
11
+ defaults.fullcone and defaults.fullcone6 are switches for IPv4 and IPv6
12
+ respectively, most IPv6 traffic do NOT need this FullCone NAT functionality.
13
+
14
+ Renew: ZiMing Mo <msylgj@immortalwrt.org>
14
15
---
15
- root/etc/config/firewall | 3 +
16
- root/usr/share/firewall4/templates/ruleset.uc | 16 +++-
17
- .../firewall4/templates/zone-fullcone.uc | 4 +
18
- root/usr/share/ucode/fw4.uc | 76 ++++++++++++++++++-
19
- 4 files changed, 96 insertions(+), 3 deletions(-)
16
+ root/etc/config/firewall | 2 + +
17
+ root/usr/share/firewall4/templates/ruleset.uc | 16 ++++++++++++++- -
18
+ .../firewall4/templates/zone-fullcone.uc | 4 ++++
19
+ root/usr/share/ucode/fw4.uc | 69 ++++++++++++++++++-
20
+ 4 files changed, 89 insertions(+), 4 deletions(-)
20
21
create mode 100644 root/usr/share/firewall4/templates/zone-fullcone.uc
21
22
22
- diff --git a/root/etc/config/firewall b/root/etc/config/firewall
23
- index b9a4647..7187723 100644
24
23
--- a/root/etc/config/firewall
25
24
+++ b/root/etc/config/firewall
26
- @@ -5,6 +5,7 @@ config defaults
25
+ @@ -5,6 +5,10 @@ config defaults
27
26
option forward REJECT
28
27
# Uncomment this line to disable ipv6 rules
29
28
# option disable_ipv6 1
30
- + option fullcone '1'
29
+ + option flow_offloading 0
30
+ + option flow_offloading_hw 0
31
+ + option fullcone 1
32
+ + option fullcone6 0
31
33
32
34
config zone
33
35
option name lan
34
- @@ -20,6 +21,8 @@ config zone
35
- option input REJECT
36
- option output ACCEPT
37
- option forward REJECT
38
- + option fullcone4 '1'
39
- + option fullcone6 '0'
40
- option masq 1
41
- option mtu_fix 1
42
-
43
- diff --git a/root/usr/share/firewall4/templates/ruleset.uc b/root/usr/share/firewall4/templates/ruleset.uc
44
- index eaa1f04..e29eae6 100644
45
36
--- a/root/usr/share/firewall4/templates/ruleset.uc
46
37
+++ b/root/usr/share/firewall4/templates/ruleset.uc
47
- @@ -310 ,6 +310 ,12 @@ table inet fw4 {
38
+ @@ -320 ,6 +320 ,12 @@ table inet fw4 {
48
39
{% for (let redirect in fw4.redirects(`dstnat_${zone.name}`)): %}
49
40
{%+ include("redirect.uc", { fw4, redirect }) %}
50
41
{% endfor %}
51
- + {% if (zone.fullcone4 ): %}
42
+ + {% if (zone.masq && fw4.default_option("fullcone") ): %}
52
43
+ {%+ include("zone-fullcone.uc", { fw4, zone, family: 4, direction: "dstnat" }) %}
53
44
+ {% endif %}
54
- + {% if (zone.fullcone6): %}
45
+ + {% if (zone.masq6 && fw4.default_option(" fullcone6") ): %}
55
46
+ {%+ include("zone-fullcone.uc", { fw4, zone, family: 6, direction: "dstnat" }) %}
56
47
+ {% endif %}
57
48
{% fw4.includes('chain-append', `dstnat_${zone.name}`) %}
58
49
}
59
50
60
- @@ -320 ,20 +326 ,26 @@ table inet fw4 {
51
+ @@ -330 ,20 +336 ,26 @@ table inet fw4 {
61
52
{% for (let redirect in fw4.redirects(`srcnat_${zone.name}`)): %}
62
53
{%+ include("redirect.uc", { fw4, redirect }) %}
63
54
{% endfor %}
64
55
- {% if (zone.masq): %}
65
- + {% if (zone.masq && !zone.fullcone4 ): %}
56
+ + {% if (zone.masq && !fw4.default_option("fullcone") ): %}
66
57
{% for (let saddrs in zone.masq4_src_subnets): %}
67
58
{% for (let daddrs in zone.masq4_dest_subnets): %}
68
59
{%+ include("zone-masq.uc", { fw4, zone, family: 4, saddrs, daddrs }) %}
69
60
{% endfor %}
70
61
{% endfor %}
71
62
{% endif %}
72
63
- {% if (zone.masq6): %}
73
- + {% if (zone.masq6 && !zone. fullcone6): %}
64
+ + {% if (zone.masq6 && !fw4.default_option(" fullcone6") ): %}
74
65
{% for (let saddrs in zone.masq6_src_subnets): %}
75
66
{% for (let daddrs in zone.masq6_dest_subnets): %}
76
67
{%+ include("zone-masq.uc", { fw4, zone, family: 6, saddrs, daddrs }) %}
77
68
{% endfor %}
78
69
{% endfor %}
79
70
{% endif %}
80
- + {% if (zone.fullcone4 ): %}
71
+ + {% if (zone.masq && fw4.default_option("fullcone") ): %}
81
72
+ {%+ include("zone-fullcone.uc", { fw4, zone, family: 4, direction: "srcnat" }) %}
82
73
+ {% endif %}
83
- + {% if (zone.fullcone6): %}
74
+ + {% if (zone.masq6 && fw4.default_option(" fullcone6") ): %}
84
75
+ {%+ include("zone-fullcone.uc", { fw4, zone, family: 6, direction: "srcnat" }) %}
85
76
+ {% endif %}
86
77
{% fw4.includes('chain-append', `srcnat_${zone.name}`) %}
87
78
}
88
79
89
- diff --git a/root/usr/share/firewall4/templates/zone-fullcone.uc b/root/usr/share/firewall4/templates/zone-fullcone.uc
90
- new file mode 100644
91
- index 0000000..77d9806
92
80
--- /dev/null
93
81
+++ b/root/usr/share/firewall4/templates/zone-fullcone.uc
94
82
@@ -0,0 +1,4 @@
95
83
+ {# /usr/share/firewall4/templates/zone-fullcone.uc #}
96
84
+ meta nfproto {{ fw4.nfproto(family) }} fullcone comment "!fw4: Handle {{
97
85
+ zone.name
98
86
+ }} {{ fw4.nfproto(family, true) }} fullcone NAT {{ direction }} traffic"
99
- diff --git a/root/usr/share/ucode/fw4.uc b/root/usr/share/ucode/fw4.uc
100
- index 1b4764c..c5716da 100644
101
87
--- a/root/usr/share/ucode/fw4.uc
102
88
+++ b/root/usr/share/ucode/fw4.uc
103
89
@@ -1,3 +1,5 @@
@@ -106,7 +92,7 @@ index 1b4764c..c5716da 100644
106
92
const fs = require("fs");
107
93
const uci = require("uci");
108
94
const ubus = require("ubus");
109
- @@ -428 ,6 +430 ,25 @@ function nft_try_hw_offload(devices) {
95
+ @@ -490 ,6 +492 ,25 @@ function nft_try_hw_offload(devices) {
110
96
return (rc == 0);
111
97
}
112
98
@@ -132,7 +118,7 @@ index 1b4764c..c5716da 100644
132
118
133
119
return {
134
120
read_kernel_version: function() {
135
- @@ -765 ,6 +786 ,18 @@ return {
121
+ @@ -840 ,6 +861 ,18 @@ return {
136
122
warn(`[!] ${msg}\n`);
137
123
},
138
124
@@ -151,7 +137,7 @@ index 1b4764c..c5716da 100644
151
137
get: function(sid, opt) {
152
138
return this.cursor.get("firewall", sid, opt);
153
139
},
154
- @@ -946 ,6 +979 ,21 @@ return {
140
+ @@ -1021 ,6 +1054 ,21 @@ return {
155
141
}
156
142
},
157
143
@@ -173,23 +159,24 @@ index 1b4764c..c5716da 100644
173
159
parse_policy: function(val) {
174
160
return this.parse_enum(val, [
175
161
"accept",
176
- @@ -1385 ,6 +1433 ,7 @@ return {
162
+ @@ -1460 ,6 +1508 ,7 @@ return {
177
163
"dnat",
178
164
"snat",
179
165
"masquerade",
180
166
+ "fullcone",
181
167
"accept",
182
168
"reject",
183
169
"drop"
184
- @@ -1852 ,6 +1901,7 @@ return {
170
+ @@ -1927 ,6 +1976,8 @@ return {
185
171
}
186
172
187
173
let defs = this.parse_options(data, {
188
174
+ fullcone: [ "bool", "0" ],
175
+ + fullcone6: [ "bool", "0" ],
189
176
input: [ "policy", "drop" ],
190
177
output: [ "policy", "drop" ],
191
178
forward: [ "policy", "drop" ],
192
- @@ -1884 ,6 +1934 ,11 @@ return {
179
+ @@ -1961 ,6 +2012 ,11 @@ return {
193
180
194
181
delete defs.syn_flood;
195
182
@@ -201,48 +188,29 @@ index 1b4764c..c5716da 100644
201
188
this.state.defaults = defs;
202
189
},
203
190
204
- @@ -1908,6 +1963,8 @@ return {
205
- masq_dest: [ "network", null, PARSE_LIST ],
206
-
207
- masq6: [ "bool" ],
208
- + fullcone4: [ "bool", "0" ],
209
- + fullcone6: [ "bool", "0" ],
210
-
211
- extra: [ "string", null, UNSUPPORTED ],
212
- extra_src: [ "string", null, UNSUPPORTED ],
213
- @@ -1940,6 +1997,18 @@ return {
214
- }
215
- }
216
-
217
- + if (this.state.defaults && !this.state.defaults.fullcone) {
218
- + this.warn_section(data, "fullcone in defaults not enabled, ignore zone fullcone settings");
219
- + zone.fullcone4 = false;
220
- + zone.fullcone6 = false;
221
- + }
222
- + if (zone.fullcone4) {
223
- + this.myinfo_section(data, "IPv4 fullcone enabled for zone '" + zone.name + "'");
224
- + }
225
- + if (zone.fullcone6) {
226
- + this.myinfo_section(data, "IPv6 fullcone enabled for zone '" + zone.name + "'");
227
- + }
228
- +
229
- if (zone.mtu_fix && this.kernel < 0x040a0000) {
230
- this.warn_section(data, "option 'mtu_fix' requires kernel 4.10 or later");
231
- return;
232
- @@ -2110,10 +2179,15 @@ return {
191
+ @@ -2186,10 +2242,23 @@ return {
233
192
zone.related_subnets = related_subnets;
234
193
zone.related_physdevs = related_physdevs;
235
194
236
- + if (zone.fullcone4 || zone.fullcone6) {
237
- + zone.dflags.snat = true;
238
- + zone.dflags.dnat = true;
195
+ - if (zone.masq || zone.masq6)
196
+ + if (zone.masq) {
197
+ zone.dflags.snat = true;
198
+ + if (this.state.defaults.fullcone) {
199
+ + zone.dflags.dnat = true;
200
+ + this.myinfo_section(data, "IPv4 fullcone enabled for zone '" + zone.name + "'");
201
+ + }
239
202
+ }
240
203
+
241
- if (zone.masq || zone.masq6)
242
- zone.dflags.snat = true;
204
+ + if (zone.masq6) {
205
+ + zone.dflags.snat = true;
206
+ + if (this.state.defaults.fullcone6) {
207
+ + zone.dflags.dnat = true;
208
+ + this.myinfo_section(data, "IPv6 fullcone enabled for zone '" + zone.name + "'");
209
+ + }
210
+ + }
243
211
244
212
- if ((zone.auto_helper && !(zone.masq || zone.masq6)) || length(zone.helper)) {
245
- + if ((zone.auto_helper && !(zone.masq || zone.masq6 || zone.fullcone4 || zone .fullcone6)) || length(zone.helper)) {
213
+ + if ((zone.auto_helper && !(zone.masq || zone.masq6 || this.state.defaults.fullcone || this.state.defaults .fullcone6)) || length(zone.helper)) {
246
214
zone.dflags.helper = true;
247
215
248
- for (let helper in (length(zone.helper) ? zone.helper : this.state.helpers)) {
216
+ for (let helper in (length(zone.helper) ? zone.helper : this.state.helpers)) {
0 commit comments