@@ -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
}
@@ -726,6 +738,34 @@ func (c *Client) syncIPTables() error {
726
738
return true
727
739
})
728
740
741
+ // Merge rules defined in wireguardIPTablesIPv4 into nodeNetworkPolicyIPTablesIPv4, since both two feature
742
+ // has added rules in antreaInputChain and antreaOutputChain.
743
+ c .wireguardIPTablesIPv4 .Range (func (key , value interface {}) bool {
744
+ chain := key .(string )
745
+ rules := value .([]string )
746
+ existingRules , found := nodeNetworkPolicyIPTablesIPv4 [chain ]
747
+ if ! found {
748
+ existingRules = make ([]string , 0 )
749
+ }
750
+ existingRules = append (existingRules , rules ... )
751
+ nodeNetworkPolicyIPTablesIPv4 [chain ] = existingRules
752
+ return true
753
+ })
754
+
755
+ // Merge rules defined in wireguardIPTablesIPv6 into nodeNetworkPolicyIPTablesIPv6, since both two feature
756
+ // has added rules in antreaInputChain and antreaOutputChain.
757
+ c .wireguardIPTablesIPv6 .Range (func (key , value interface {}) bool {
758
+ chain := key .(string )
759
+ rules := value .([]string )
760
+ existingRules , found := nodeNetworkPolicyIPTablesIPv6 [chain ]
761
+ if ! found {
762
+ existingRules = make ([]string , 0 )
763
+ }
764
+ existingRules = append (existingRules , rules ... )
765
+ nodeNetworkPolicyIPTablesIPv6 [chain ] = existingRules
766
+ return true
767
+ })
768
+
729
769
// Use iptables-restore to configure IPv4 settings.
730
770
if c .networkConfig .IPv4Enabled {
731
771
iptablesData := c .restoreIptablesData (c .nodeConfig .PodIPv4CIDR ,
@@ -1198,6 +1238,37 @@ func (c *Client) initNodeNetworkPolicy() {
1198
1238
}
1199
1239
}
1200
1240
1241
+ func (c * Client ) initWireguard () {
1242
+ wireguardPort := intstr .FromInt (c .wireguardPort )
1243
+ antreaInputChainRules := []string {
1244
+ iptables .NewRuleBuilder (antreaInputChain ).
1245
+ SetComment ("Antrea: allow input packets destined at the wireguard port" ).
1246
+ MatchTransProtocol ("udp" ).
1247
+ MatchPortDst (& wireguardPort , nil ).
1248
+ SetTarget (iptables .AcceptTarget ).
1249
+ Done ().
1250
+ GetRule (),
1251
+ }
1252
+ antreaOutputChainRules := []string {
1253
+ iptables .NewRuleBuilder (antreaOutputChain ).
1254
+ SetComment ("Antrea: allow output packets destined at the wireguard port" ).
1255
+ MatchTransProtocol ("udp" ).
1256
+ MatchPortDst (& wireguardPort , nil ).
1257
+ SetTarget (iptables .AcceptTarget ).
1258
+ Done ().
1259
+ GetRule (),
1260
+ }
1261
+
1262
+ if c .networkConfig .IPv6Enabled {
1263
+ c .wireguardIPTablesIPv6 .Store (antreaInputChain , antreaInputChainRules )
1264
+ c .wireguardIPTablesIPv6 .Store (antreaOutputChain , antreaOutputChainRules )
1265
+ }
1266
+ if c .networkConfig .IPv4Enabled {
1267
+ c .wireguardIPTablesIPv4 .Store (antreaInputChain , antreaInputChainRules )
1268
+ c .wireguardIPTablesIPv4 .Store (antreaOutputChain , antreaOutputChainRules )
1269
+ }
1270
+ }
1271
+
1201
1272
// Reconcile removes orphaned podCIDRs from ipset and removes routes to orphaned podCIDRs
1202
1273
// based on the desired podCIDRs.
1203
1274
func (c * Client ) Reconcile (podCIDRs []string ) error {
0 commit comments