Skip to content

Commit 253146d

Browse files
committed
Install iptables rules to allow packets to wireguard port
This change is to actively configure iptables rules on wireguard port when is used as the encription mode. This can fix traffic issues if the Node is configured with iptables default DROP policy. Signed-off-by: Wenying Dong <[email protected]>
1 parent 87def6d commit 253146d

File tree

4 files changed

+396
-5
lines changed

4 files changed

+396
-5
lines changed

cmd/antrea-agent/agent.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ func run(o *Options) error {
244244
multicastEnabled,
245245
o.config.SNATFullyRandomPorts,
246246
*o.config.Egress.SNATFullyRandomPorts,
247-
serviceCIDRProvider)
247+
serviceCIDRProvider,
248+
wireguardConfig.Port,
249+
)
248250
if err != nil {
249251
return fmt.Errorf("error creating route client: %v", err)
250252
}

pkg/agent/route/route_linux.go

+74-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/containernetworking/plugins/pkg/ip"
2929
"github.com/vishvananda/netlink"
3030
"golang.org/x/sys/unix"
31+
"k8s.io/apimachinery/pkg/util/intstr"
3132
"k8s.io/apimachinery/pkg/util/sets"
3233
"k8s.io/apimachinery/pkg/util/wait"
3334
"k8s.io/klog/v2"
@@ -158,10 +159,17 @@ type Client struct {
158159
nodeNetworkPolicyIPTablesIPv4 sync.Map
159160
// nodeNetworkPolicyIPTablesIPv6 caches all existing IPv6 iptables chains and rules for NodeNetworkPolicy.
160161
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
161166
// deterministic represents whether to write iptables chains and rules for NodeNetworkPolicy deterministically when
162167
// syncIPTables is called. Enabling it may carry a performance impact. It's disabled by default and should only be
163168
// used in testing.
164169
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
165173
}
166174

167175
// NewClient returns a route client.
@@ -173,7 +181,8 @@ func NewClient(networkConfig *config.NetworkConfig,
173181
multicastEnabled bool,
174182
nodeSNATRandomFully bool,
175183
egressSNATRandomFully bool,
176-
serviceCIDRProvider servicecidr.Interface) (*Client, error) {
184+
serviceCIDRProvider servicecidr.Interface,
185+
wireguardPort int) (*Client, error) {
177186
return &Client{
178187
networkConfig: networkConfig,
179188
noSNAT: noSNAT,
@@ -194,6 +203,7 @@ func NewClient(networkConfig *config.NetworkConfig,
194203
antreaExternalIPIPSet: {},
195204
antreaExternalIPIP6Set: {},
196205
},
206+
wireguardPort: wireguardPort,
197207
}, nil
198208
}
199209

@@ -265,7 +275,9 @@ func (c *Client) Initialize(nodeConfig *config.NodeConfig, done func()) error {
265275
if c.nodeNetworkPolicyEnabled {
266276
c.initNodeNetworkPolicy()
267277
}
268-
278+
if c.networkConfig.TrafficEncryptionMode == config.TrafficEncryptionModeWireGuard {
279+
c.initWireguard()
280+
}
269281
return nil
270282
}
271283

@@ -675,7 +687,7 @@ func (c *Client) syncIPTables() error {
675687
if c.proxyAll {
676688
jumpRules = append(jumpRules, jumpRule{iptables.NATTable, iptables.OutputChain, antreaOutputChain, "Antrea: jump to Antrea output rules", true})
677689
}
678-
if c.nodeNetworkPolicyEnabled {
690+
if c.nodeNetworkPolicyEnabled || c.networkConfig.TrafficEncryptionMode == config.TrafficEncryptionModeWireGuard {
679691
jumpRules = append(jumpRules, jumpRule{iptables.FilterTable, iptables.InputChain, antreaInputChain, "Antrea: jump to Antrea input rules", false})
680692
jumpRules = append(jumpRules, jumpRule{iptables.FilterTable, iptables.OutputChain, antreaOutputChain, "Antrea: jump to Antrea output rules", false})
681693
}
@@ -726,6 +738,34 @@ func (c *Client) syncIPTables() error {
726738
return true
727739
})
728740

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+
729769
// Use iptables-restore to configure IPv4 settings.
730770
if c.networkConfig.IPv4Enabled {
731771
iptablesData := c.restoreIptablesData(c.nodeConfig.PodIPv4CIDR,
@@ -1198,6 +1238,37 @@ func (c *Client) initNodeNetworkPolicy() {
11981238
}
11991239
}
12001240

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+
12011272
// Reconcile removes orphaned podCIDRs from ipset and removes routes to orphaned podCIDRs
12021273
// based on the desired podCIDRs.
12031274
func (c *Client) Reconcile(podCIDRs []string) error {

0 commit comments

Comments
 (0)