@@ -28,6 +28,7 @@ import (
28
28
"github.com/containernetworking/plugins/pkg/ip"
29
29
"github.com/vishvananda/netlink"
30
30
"golang.org/x/sys/unix"
31
+ "k8s.io/apimachinery/pkg/util/intstr"
31
32
"k8s.io/apimachinery/pkg/util/sets"
32
33
"k8s.io/apimachinery/pkg/util/wait"
33
34
"k8s.io/klog/v2"
@@ -158,10 +159,17 @@ type Client struct {
158
159
nodeNetworkPolicyIPTablesIPv4 sync.Map
159
160
// nodeNetworkPolicyIPTablesIPv6 caches all existing IPv6 iptables chains and rules for NodeNetworkPolicy.
160
161
nodeNetworkPolicyIPTablesIPv6 sync.Map
162
+ // wireguardIPTablesIPv4 caches all existing IPv4 iptables chains and rules for WireGuard.
163
+ wireguardIPTablesIPv4 sync.Map
164
+ // wireguardIPTablesIPv6 caches all existing IPv6 iptables chains and rules for WireGuard.
165
+ wireguardIPTablesIPv6 sync.Map
161
166
// deterministic represents whether to write iptables chains and rules for NodeNetworkPolicy deterministically when
162
167
// syncIPTables is called. Enabling it may carry a performance impact. It's disabled by default and should only be
163
168
// used in testing.
164
169
deterministic bool
170
+ // wireguardPort is the port used for the WireGuard UDP tunnels. When WireGuard is enabled (used as the encryption
171
+ // mode), we add iptables rules to the filter table to accept input and output UDP traffic destined to this port.
172
+ wireguardPort int
165
173
}
166
174
167
175
// NewClient returns a route client.
@@ -173,7 +181,8 @@ func NewClient(networkConfig *config.NetworkConfig,
173
181
multicastEnabled bool ,
174
182
nodeSNATRandomFully bool ,
175
183
egressSNATRandomFully bool ,
176
- serviceCIDRProvider servicecidr.Interface ) (* Client , error ) {
184
+ serviceCIDRProvider servicecidr.Interface ,
185
+ wireguardPort int ) (* Client , error ) {
177
186
return & Client {
178
187
networkConfig : networkConfig ,
179
188
noSNAT : noSNAT ,
@@ -194,6 +203,7 @@ func NewClient(networkConfig *config.NetworkConfig,
194
203
antreaExternalIPIPSet : {},
195
204
antreaExternalIPIP6Set : {},
196
205
},
206
+ wireguardPort : wireguardPort ,
197
207
}, nil
198
208
}
199
209
@@ -265,7 +275,9 @@ func (c *Client) Initialize(nodeConfig *config.NodeConfig, done func()) error {
265
275
if c .nodeNetworkPolicyEnabled {
266
276
c .initNodeNetworkPolicy ()
267
277
}
268
-
278
+ if c .networkConfig .TrafficEncryptionMode == config .TrafficEncryptionModeWireGuard {
279
+ c .initWireguard ()
280
+ }
269
281
return nil
270
282
}
271
283
@@ -675,7 +687,7 @@ func (c *Client) syncIPTables() error {
675
687
if c .proxyAll {
676
688
jumpRules = append (jumpRules , jumpRule {iptables .NATTable , iptables .OutputChain , antreaOutputChain , "Antrea: jump to Antrea output rules" , true })
677
689
}
678
- if c .nodeNetworkPolicyEnabled {
690
+ if c .nodeNetworkPolicyEnabled || c . networkConfig . TrafficEncryptionMode == config . TrafficEncryptionModeWireGuard {
679
691
jumpRules = append (jumpRules , jumpRule {iptables .FilterTable , iptables .InputChain , antreaInputChain , "Antrea: jump to Antrea input rules" , false })
680
692
jumpRules = append (jumpRules , jumpRule {iptables .FilterTable , iptables .OutputChain , antreaOutputChain , "Antrea: jump to Antrea output rules" , false })
681
693
}
@@ -711,20 +723,24 @@ func (c *Client) syncIPTables() error {
711
723
return true
712
724
})
713
725
714
- nodeNetworkPolicyIPTablesIPv4 := map [string ][]string {}
715
- nodeNetworkPolicyIPTablesIPv6 := map [string ][]string {}
716
- c .nodeNetworkPolicyIPTablesIPv4 .Range (func (key , value interface {}) bool {
717
- chain := key .(string )
718
- rules := value .([]string )
719
- nodeNetworkPolicyIPTablesIPv4 [chain ] = rules
720
- return true
721
- })
722
- c .nodeNetworkPolicyIPTablesIPv6 .Range (func (key , value interface {}) bool {
723
- chain := key .(string )
724
- rules := value .([]string )
725
- nodeNetworkPolicyIPTablesIPv6 [chain ] = rules
726
- return true
727
- })
726
+ addFilterRulesToChain := func (iptablesRulesByChain map [string ][]string , m * sync.Map ) {
727
+ m .Range (func (key , value interface {}) bool {
728
+ chain := key .(string )
729
+ rules := value .([]string )
730
+ iptablesRulesByChain [chain ] = append (iptablesRulesByChain [chain ], rules ... )
731
+ return true
732
+ })
733
+ }
734
+
735
+ iptablesFilterRulesByChainV4 := make (map [string ][]string )
736
+ // Install the static rules (WireGuard for now) before the dynamic rules (e.g., NodeNetworkPolicy)
737
+ // for performance reasons.
738
+ addFilterRulesToChain (iptablesFilterRulesByChainV4 , & c .wireguardIPTablesIPv4 )
739
+ addFilterRulesToChain (iptablesFilterRulesByChainV4 , & c .nodeNetworkPolicyIPTablesIPv4 )
740
+
741
+ iptablesFilterRulesByChainV6 := make (map [string ][]string )
742
+ addFilterRulesToChain (iptablesFilterRulesByChainV6 , & c .wireguardIPTablesIPv6 )
743
+ addFilterRulesToChain (iptablesFilterRulesByChainV6 , & c .nodeNetworkPolicyIPTablesIPv6 )
728
744
729
745
// Use iptables-restore to configure IPv4 settings.
730
746
if c .networkConfig .IPv4Enabled {
@@ -737,7 +753,7 @@ func (c *Client) syncIPTables() error {
737
753
config .VirtualNodePortDNATIPv4 ,
738
754
config .VirtualServiceIPv4 ,
739
755
snatMarkToIPv4 ,
740
- nodeNetworkPolicyIPTablesIPv4 ,
756
+ iptablesFilterRulesByChainV4 ,
741
757
false )
742
758
743
759
// Setting --noflush to keep the previous contents (i.e. non antrea managed chains) of the tables.
@@ -757,7 +773,7 @@ func (c *Client) syncIPTables() error {
757
773
config .VirtualNodePortDNATIPv6 ,
758
774
config .VirtualServiceIPv6 ,
759
775
snatMarkToIPv6 ,
760
- nodeNetworkPolicyIPTablesIPv6 ,
776
+ iptablesFilterRulesByChainV6 ,
761
777
true )
762
778
// Setting --noflush to keep the previous contents (i.e. non antrea managed chains) of the tables.
763
779
if err := c .iptables .Restore (iptablesData .String (), false , true ); err != nil {
@@ -777,7 +793,7 @@ func (c *Client) restoreIptablesData(podCIDR *net.IPNet,
777
793
nodePortDNATVirtualIP ,
778
794
serviceVirtualIP net.IP ,
779
795
snatMarkToIP map [uint32 ]net.IP ,
780
- nodeNetWorkPolicyIPTables map [string ][]string ,
796
+ iptablesFiltersRuleByChain map [string ][]string ,
781
797
isIPv6 bool ) * bytes.Buffer {
782
798
// Create required rules in the antrea chains.
783
799
// Use iptables-restore as it flushes the involved chains and creates the desired rules
@@ -897,7 +913,7 @@ func (c *Client) restoreIptablesData(podCIDR *net.IPNet,
897
913
writeLine (iptablesData , iptables .MakeChainLine (antreaForwardChain ))
898
914
899
915
var nodeNetworkPolicyIPTablesChains []string
900
- for chain := range nodeNetWorkPolicyIPTables {
916
+ for chain := range iptablesFiltersRuleByChain {
901
917
nodeNetworkPolicyIPTablesChains = append (nodeNetworkPolicyIPTablesChains , chain )
902
918
}
903
919
if c .deterministic {
@@ -937,7 +953,7 @@ func (c *Client) restoreIptablesData(podCIDR *net.IPNet,
937
953
}... )
938
954
}
939
955
for _ , chain := range nodeNetworkPolicyIPTablesChains {
940
- for _ , rule := range nodeNetWorkPolicyIPTables [chain ] {
956
+ for _ , rule := range iptablesFiltersRuleByChain [chain ] {
941
957
writeLine (iptablesData , rule )
942
958
}
943
959
}
@@ -1198,6 +1214,37 @@ func (c *Client) initNodeNetworkPolicy() {
1198
1214
}
1199
1215
}
1200
1216
1217
+ func (c * Client ) initWireguard () {
1218
+ wireguardPort := intstr .FromInt (c .wireguardPort )
1219
+ antreaInputChainRules := []string {
1220
+ iptables .NewRuleBuilder (antreaInputChain ).
1221
+ SetComment ("Antrea: allow WireGuard input packets" ).
1222
+ MatchTransProtocol (iptables .ProtocolUDP ).
1223
+ MatchPortDst (& wireguardPort , nil ).
1224
+ SetTarget (iptables .AcceptTarget ).
1225
+ Done ().
1226
+ GetRule (),
1227
+ }
1228
+ antreaOutputChainRules := []string {
1229
+ iptables .NewRuleBuilder (antreaOutputChain ).
1230
+ SetComment ("Antrea: allow WireGuard output packets" ).
1231
+ MatchTransProtocol (iptables .ProtocolUDP ).
1232
+ MatchPortDst (& wireguardPort , nil ).
1233
+ SetTarget (iptables .AcceptTarget ).
1234
+ Done ().
1235
+ GetRule (),
1236
+ }
1237
+
1238
+ if c .networkConfig .IPv6Enabled {
1239
+ c .wireguardIPTablesIPv6 .Store (antreaInputChain , antreaInputChainRules )
1240
+ c .wireguardIPTablesIPv6 .Store (antreaOutputChain , antreaOutputChainRules )
1241
+ }
1242
+ if c .networkConfig .IPv4Enabled {
1243
+ c .wireguardIPTablesIPv4 .Store (antreaInputChain , antreaInputChainRules )
1244
+ c .wireguardIPTablesIPv4 .Store (antreaOutputChain , antreaOutputChainRules )
1245
+ }
1246
+ }
1247
+
1201
1248
// Reconcile removes orphaned podCIDRs from ipset and removes routes to orphaned podCIDRs
1202
1249
// based on the desired podCIDRs.
1203
1250
func (c * Client ) Reconcile (podCIDRs []string ) error {
0 commit comments