44 "context"
55 "fmt"
66 "net/netip"
7+ "slices"
78 "strings"
89 "time"
910
@@ -40,6 +41,8 @@ type tunnelUpPMTUDData struct {
4041 // network is used to find the network level header overhead.
4142 // It can be [constants.UDP] or [constants.TCP].
4243 network string
44+ // ipv6 is true if the VPN connection supports IPv6.
45+ ipv6 bool
4346 // icmpAddrs is the list of addresses to use for ICMP path MTU discovery.
4447 // Each address should handle ICMP packets for PMTUD to work.
4548 icmpAddrs []netip.Addr
@@ -69,7 +72,7 @@ func (l *Loop) onTunnelUp(ctx, loopCtx context.Context, data tunnelUpData) {
6972 if data .pmtud .enabled {
7073 mtuLogger := l .logger .New (log .SetComponent ("MTU discovery" ))
7174 err := updateToMaxMTU (ctx , data .vpnIntf , data .pmtud .vpnType ,
72- data .pmtud .network , data .pmtud .icmpAddrs , data .pmtud .tcpAddrs ,
75+ data .pmtud .network , data .pmtud .ipv6 , data . pmtud . icmpAddrs , data .pmtud .tcpAddrs ,
7376 l .netLinker , l .routing , l .fw , mtuLogger )
7477 if err != nil {
7578 mtuLogger .Error (err .Error ())
@@ -173,16 +176,11 @@ func (l *Loop) restartVPN(ctx context.Context, healthErr error) {
173176}
174177
175178func updateToMaxMTU (ctx context.Context , vpnInterface string ,
176- vpnType , network string , icmpAddrs []netip.Addr , tcpAddrs []netip.AddrPort ,
179+ vpnType , network string , ipv6 bool , icmpAddrs []netip.Addr , tcpAddrs []netip.AddrPort ,
177180 netlinker NetLinker , routing Routing , firewall tcp.Firewall , logger * log.Logger ,
178181) error {
179182 logger .Info ("finding maximum MTU, this can take up to 6 seconds" )
180183
181- vpnGatewayIP , err := routing .VPNLocalGatewayIP (vpnInterface )
182- if err != nil {
183- return fmt .Errorf ("getting VPN gateway IP address: %w" , err )
184- }
185-
186184 vpnRoutes , err := routing .VPNRoutes (vpnInterface )
187185 if err != nil {
188186 return fmt .Errorf ("getting VPN routes: %w" , err )
@@ -195,7 +193,7 @@ func updateToMaxMTU(ctx context.Context, vpnInterface string,
195193
196194 originalMTU := link .MTU
197195
198- vpnLinkMTU := pmtud .MaxTheoreticalVPNMTU (vpnType , network , vpnGatewayIP )
196+ vpnLinkMTU := pmtud .MaxTheoreticalVPNMTU (vpnType , network , ipv6 )
199197
200198 // Setting the VPN link MTU to 1500 might interrupt the connection until
201199 // the new MTU is set again, but this is necessary to find the highest valid MTU.
@@ -206,6 +204,15 @@ func updateToMaxMTU(ctx context.Context, vpnInterface string,
206204 return fmt .Errorf ("setting VPN interface %s MTU to %d: %w" , vpnInterface , vpnLinkMTU , err )
207205 }
208206
207+ if ! ipv6 {
208+ icmpAddrs = slices .DeleteFunc (icmpAddrs , func (addr netip.Addr ) bool {
209+ return addr .Is6 ()
210+ })
211+ tcpAddrs = slices .DeleteFunc (tcpAddrs , func (addr netip.AddrPort ) bool {
212+ return addr .Addr ().Is6 ()
213+ })
214+ }
215+
209216 const pingTimeout = time .Second
210217 vpnLinkMTU , err = pmtud .PathMTUDiscover (ctx , icmpAddrs , tcpAddrs ,
211218 vpnLinkMTU , pingTimeout , firewall , logger )
0 commit comments