Skip to content

Commit 468e91f

Browse files
committed
Use peer's wireguard port, not our own
Before this change, the wireguard code constructed a peer endpoint via "PeerIP + n.dev.listenPort", i.e. we used the peer's IP, but our port. This works fine if every k8s node has the same ListenPort, which is admittedly the common setup... However, some people may desire to use different ports for some cases, and in that case, we should respect that different port. I've manually tested that this works in my cluster, where 1 node has different ports for wireguard from all the others. After deploying this diff to each node, I get a working pod network.
1 parent 0d1f8e2 commit 468e91f

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

pkg/backend/wireguard/wireguard.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,17 @@ func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backen
5757
return be, nil
5858
}
5959

60-
func newSubnetAttrs(publicIP net.IP, publicIPv6 net.IP, enableIPv4, enableIPv6 bool, publicKey string) (*lease.LeaseAttrs, error) {
61-
data, err := json.Marshal(&wireguardLeaseAttrs{
60+
func newSubnetAttrs(publicIP net.IP, publicIPv6 net.IP, enableIPv4, enableIPv6 bool, publicKey string, v4Port, v6Port uint16) (*lease.LeaseAttrs, error) {
61+
v4Data, err := json.Marshal(&wireguardLeaseAttrs{
6262
PublicKey: publicKey,
63+
Port: v4Port,
64+
})
65+
if err != nil {
66+
return nil, err
67+
}
68+
v6Data, err := json.Marshal(&wireguardLeaseAttrs{
69+
PublicKey: publicKey,
70+
Port: v6Port,
6371
})
6472
if err != nil {
6573
return nil, err
@@ -74,15 +82,15 @@ func newSubnetAttrs(publicIP net.IP, publicIPv6 net.IP, enableIPv4, enableIPv6 b
7482
}
7583

7684
if enableIPv4 {
77-
leaseAttrs.BackendData = json.RawMessage(data)
85+
leaseAttrs.BackendData = json.RawMessage(v4Data)
7886
}
7987

8088
if publicIPv6 != nil {
8189
leaseAttrs.PublicIPv6 = ip.FromIP6(publicIPv6)
8290
}
8391

8492
if enableIPv6 {
85-
leaseAttrs.BackendV6Data = json.RawMessage(data)
93+
leaseAttrs.BackendV6Data = json.RawMessage(v6Data)
8694
}
8795

8896
return leaseAttrs, nil
@@ -155,7 +163,7 @@ func (be *WireguardBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGr
155163
return nil, fmt.Errorf("no valid Mode configured")
156164
}
157165

158-
subnetAttrs, err := newSubnetAttrs(be.extIface.ExtAddr, be.extIface.ExtV6Addr, config.EnableIPv4, config.EnableIPv6, publicKey)
166+
subnetAttrs, err := newSubnetAttrs(be.extIface.ExtAddr, be.extIface.ExtV6Addr, config.EnableIPv4, config.EnableIPv6, publicKey, uint16(cfg.ListenPort), uint16(cfg.ListenPortV6))
159167
if err != nil {
160168
return nil, err
161169
}

pkg/backend/wireguard/wireguard_network.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package wireguard
1818

1919
import (
20+
"cmp"
2021
"context"
2122
"encoding/json"
2223
"fmt"
@@ -101,6 +102,7 @@ func (n *network) Run(ctx context.Context) {
101102

102103
type wireguardLeaseAttrs struct {
103104
PublicKey string
105+
Port uint16
104106
}
105107

106108
// Select the endpoint address that is most likely to allow for a successful
@@ -115,24 +117,20 @@ type wireguardLeaseAttrs struct {
115117
// address will only have a small chance of succeeding (ipv6 masquarading is
116118
// very rare)
117119
// - If neither is true default to ipv4 and cross fingers.
118-
func (n *network) selectPublicEndpoint(ip4 *ip.IP4, ip6 *ip.IP6) string {
120+
func (n *network) autoMode(ip4 *ip.IP4, ip6 *ip.IP6) Mode {
119121
if ip4 != nil && ip6 == nil {
120-
return ip4.String()
122+
return Ipv4
121123
}
122-
123124
if ip4 == nil && ip6 != nil {
124-
return fmt.Sprintf("[%s]", ip6.String())
125+
return Ipv6
125126
}
126-
127127
if !ip4.IsPrivate() && n.extIface.ExtAddr != nil {
128-
return ip4.String()
128+
return Ipv4
129129
}
130-
131130
if !ip6.IsPrivate() && n.extIface.ExtV6Addr != nil && !ip.FromIP6(n.extIface.ExtV6Addr).IsPrivate() {
132-
return fmt.Sprintf("[%s]", ip6.String())
131+
return Ipv6
133132
}
134-
135-
return ip4.String()
133+
return Ipv4
136134
}
137135

138136
func (n *network) handleSubnetEvents(ctx context.Context, batch []lease.Event) {
@@ -169,12 +167,18 @@ func (n *network) handleSubnetEvents(ctx context.Context, batch []lease.Event) {
169167
subnets = append(subnets, event.Lease.IPv6Subnet.ToIPNet()) //only used if n.mode != Separate
170168
}
171169

170+
// default to the port in the attr, but use the device's listen port
171+
// if it's not set for backwards compatibility with older flannel
172+
// versions.
173+
v4Port := cmp.Or(v4wireguardAttrs.Port, uint16(n.dev.attrs.listenPort))
174+
v6Port := cmp.Or(v6wireguardAttrs.Port, uint16(n.v6Dev.attrs.listenPort))
175+
v4PeerEndpoint := fmt.Sprintf("%s:%d", event.Lease.Attrs.PublicIP.String(), v4Port)
176+
v6PeerEndpoint := fmt.Sprintf("%s:%d", event.Lease.Attrs.PublicIPv6.String(), v6Port)
172177
if n.mode == Separate {
173178
if event.Lease.EnableIPv4 {
174-
publicEndpoint := fmt.Sprintf("%s:%d", event.Lease.Attrs.PublicIP.String(), n.dev.attrs.listenPort)
175-
log.Infof("Subnet added: %v via %v", event.Lease.Subnet, publicEndpoint)
179+
log.Infof("Subnet added: %v via %v", event.Lease.Subnet, v4PeerEndpoint)
176180
if err := n.dev.addPeer(
177-
publicEndpoint,
181+
v4PeerEndpoint,
178182
v4wireguardAttrs.PublicKey,
179183
[]net.IPNet{*event.Lease.Subnet.ToIPNet()}); err != nil {
180184
log.Errorf("failed to setup ipv4 peer (%s): %v", v4wireguardAttrs.PublicKey, err)
@@ -190,10 +194,9 @@ func (n *network) handleSubnetEvents(ctx context.Context, batch []lease.Event) {
190194
}
191195

192196
if event.Lease.EnableIPv6 {
193-
publicEndpoint := fmt.Sprintf("[%s]:%d", event.Lease.Attrs.PublicIPv6.String(), n.v6Dev.attrs.listenPort)
194-
log.Infof("Subnet added: %v via %v", event.Lease.IPv6Subnet, publicEndpoint)
197+
log.Infof("Subnet added: %v via %v", event.Lease.IPv6Subnet, v6PeerEndpoint)
195198
if err := n.v6Dev.addPeer(
196-
publicEndpoint,
199+
v6PeerEndpoint,
197200
v6wireguardAttrs.PublicKey,
198201
[]net.IPNet{*event.Lease.IPv6Subnet.ToIPNet()}); err != nil {
199202
log.Errorf("failed to setup ipv6 peer (%s): %v", v6wireguardAttrs.PublicKey, err)
@@ -209,14 +212,17 @@ func (n *network) handleSubnetEvents(ctx context.Context, batch []lease.Event) {
209212
}
210213
} else {
211214
var publicEndpoint string
212-
if n.mode == Ipv4 {
213-
publicEndpoint = fmt.Sprintf("%s:%d", event.Lease.Attrs.PublicIP.String(), n.dev.attrs.listenPort)
214-
} else if n.mode == Ipv6 {
215-
publicEndpoint = fmt.Sprintf("[%s]:%d", event.Lease.Attrs.PublicIPv6.String(), n.dev.attrs.listenPort)
216-
} else { // Auto mode
217-
publicEndpoint = fmt.Sprintf("%s:%d",
218-
n.selectPublicEndpoint(&event.Lease.Attrs.PublicIP, event.Lease.Attrs.PublicIPv6),
219-
n.dev.attrs.listenPort)
215+
mode := n.mode
216+
if mode == Auto {
217+
mode = n.autoMode(&event.Lease.Attrs.PublicIP, event.Lease.Attrs.PublicIPv6)
218+
}
219+
switch mode {
220+
case Ipv4:
221+
publicEndpoint = v4PeerEndpoint
222+
case Ipv6:
223+
publicEndpoint = v6PeerEndpoint
224+
default:
225+
panic(fmt.Sprintf("inexhaustive match: %v", mode))
220226
}
221227

222228
log.Infof("Subnet(s) added: %v via %v", subnets, publicEndpoint)

0 commit comments

Comments
 (0)