@@ -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 at which to listen the wireguard traffic. iptables rules are added to accept the UDP
171
+ // packets destined at the wireguard port when wireguard is used as the encryption mode.
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,22 @@ 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
+ addFilterRulesToChain (iptablesFilterRulesByChainV4 , & c .nodeNetworkPolicyIPTablesIPv4 )
737
+ addFilterRulesToChain (iptablesFilterRulesByChainV4 , & c .wireguardIPTablesIPv4 )
738
+
739
+ iptablesFilterRulesByChainV6 := make (map [string ][]string )
740
+ addFilterRulesToChain (iptablesFilterRulesByChainV6 , & c .nodeNetworkPolicyIPTablesIPv6 )
741
+ addFilterRulesToChain (iptablesFilterRulesByChainV6 , & c .wireguardIPTablesIPv6 )
728
742
729
743
// Use iptables-restore to configure IPv4 settings.
730
744
if c .networkConfig .IPv4Enabled {
@@ -737,7 +751,7 @@ func (c *Client) syncIPTables() error {
737
751
config .VirtualNodePortDNATIPv4 ,
738
752
config .VirtualServiceIPv4 ,
739
753
snatMarkToIPv4 ,
740
- nodeNetworkPolicyIPTablesIPv4 ,
754
+ iptablesFilterRulesByChainV4 ,
741
755
false )
742
756
743
757
// Setting --noflush to keep the previous contents (i.e. non antrea managed chains) of the tables.
@@ -757,7 +771,7 @@ func (c *Client) syncIPTables() error {
757
771
config .VirtualNodePortDNATIPv6 ,
758
772
config .VirtualServiceIPv6 ,
759
773
snatMarkToIPv6 ,
760
- nodeNetworkPolicyIPTablesIPv6 ,
774
+ iptablesFilterRulesByChainV6 ,
761
775
true )
762
776
// Setting --noflush to keep the previous contents (i.e. non antrea managed chains) of the tables.
763
777
if err := c .iptables .Restore (iptablesData .String (), false , true ); err != nil {
@@ -777,7 +791,7 @@ func (c *Client) restoreIptablesData(podCIDR *net.IPNet,
777
791
nodePortDNATVirtualIP ,
778
792
serviceVirtualIP net.IP ,
779
793
snatMarkToIP map [uint32 ]net.IP ,
780
- nodeNetWorkPolicyIPTables map [string ][]string ,
794
+ iptablesFiltersRulesByChain map [string ][]string ,
781
795
isIPv6 bool ) * bytes.Buffer {
782
796
// Create required rules in the antrea chains.
783
797
// Use iptables-restore as it flushes the involved chains and creates the desired rules
@@ -897,7 +911,7 @@ func (c *Client) restoreIptablesData(podCIDR *net.IPNet,
897
911
writeLine (iptablesData , iptables .MakeChainLine (antreaForwardChain ))
898
912
899
913
var nodeNetworkPolicyIPTablesChains []string
900
- for chain := range nodeNetWorkPolicyIPTables {
914
+ for chain := range iptablesFiltersRulesByChain {
901
915
nodeNetworkPolicyIPTablesChains = append (nodeNetworkPolicyIPTablesChains , chain )
902
916
}
903
917
if c .deterministic {
@@ -937,7 +951,7 @@ func (c *Client) restoreIptablesData(podCIDR *net.IPNet,
937
951
}... )
938
952
}
939
953
for _ , chain := range nodeNetworkPolicyIPTablesChains {
940
- for _ , rule := range nodeNetWorkPolicyIPTables [chain ] {
954
+ for _ , rule := range iptablesFiltersRulesByChain [chain ] {
941
955
writeLine (iptablesData , rule )
942
956
}
943
957
}
@@ -1198,6 +1212,37 @@ func (c *Client) initNodeNetworkPolicy() {
1198
1212
}
1199
1213
}
1200
1214
1215
+ func (c * Client ) initWireguard () {
1216
+ wireguardPort := intstr .FromInt (c .wireguardPort )
1217
+ antreaInputChainRules := []string {
1218
+ iptables .NewRuleBuilder (antreaInputChain ).
1219
+ SetComment ("Antrea: allow input wireguard packets" ).
1220
+ MatchTransProtocol ("udp" ).
1221
+ MatchPortDst (& wireguardPort , nil ).
1222
+ SetTarget (iptables .AcceptTarget ).
1223
+ Done ().
1224
+ GetRule (),
1225
+ }
1226
+ antreaOutputChainRules := []string {
1227
+ iptables .NewRuleBuilder (antreaOutputChain ).
1228
+ SetComment ("Antrea: allow output wireguard packets" ).
1229
+ MatchTransProtocol ("udp" ).
1230
+ MatchPortDst (& wireguardPort , nil ).
1231
+ SetTarget (iptables .AcceptTarget ).
1232
+ Done ().
1233
+ GetRule (),
1234
+ }
1235
+
1236
+ if c .networkConfig .IPv6Enabled {
1237
+ c .wireguardIPTablesIPv6 .Store (antreaInputChain , antreaInputChainRules )
1238
+ c .wireguardIPTablesIPv6 .Store (antreaOutputChain , antreaOutputChainRules )
1239
+ }
1240
+ if c .networkConfig .IPv4Enabled {
1241
+ c .wireguardIPTablesIPv4 .Store (antreaInputChain , antreaInputChainRules )
1242
+ c .wireguardIPTablesIPv4 .Store (antreaOutputChain , antreaOutputChainRules )
1243
+ }
1244
+ }
1245
+
1201
1246
// Reconcile removes orphaned podCIDRs from ipset and removes routes to orphaned podCIDRs
1202
1247
// based on the desired podCIDRs.
1203
1248
func (c * Client ) Reconcile (podCIDRs []string ) error {
0 commit comments