Skip to content

Commit 525f783

Browse files
committed
Add EXP_ExternalConfiguration for Tailscale
1 parent a5db80d commit 525f783

File tree

4 files changed

+66
-19
lines changed

4 files changed

+66
-19
lines changed

tun.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ type Options struct {
109109
_TXChecksumOffload bool
110110

111111
// For library usages.
112-
EXP_DisableDNSHijack bool
112+
EXP_DisableDNSHijack bool
113+
EXP_ExternalConfiguration bool
113114

114115
// For gvisor stack, it should be enabled when MTU is less than 32768; otherwise it should be less than or equal to 8192.
115116
// The above condition is just an estimate and not exact, calculated on M4 pro.

tun_darwin.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,17 @@ func New(options Options) (Tun, error) {
146146
}
147147

148148
func (t *NativeTun) Start() error {
149+
if t.options.EXP_ExternalConfiguration {
150+
return nil
151+
}
149152
t.options.InterfaceMonitor.RegisterMyInterface(t.options.Name)
150153
return t.setRoutes()
151154
}
152155

153156
func (t *NativeTun) Close() error {
157+
if t.options.EXP_ExternalConfiguration {
158+
return t.tunFile.Close()
159+
}
154160
defer flushDNSCache()
155161
return E.Errors(t.unsetRoutes(), t.tunFile.Close())
156162
}
@@ -232,6 +238,9 @@ func create(tunFd int, ifIndex int, name string, options Options) error {
232238
if err != nil {
233239
return os.NewSyscallError("IoctlSetIfreqMTU", err)
234240
}
241+
if options.EXP_ExternalConfiguration {
242+
return nil
243+
}
235244
if len(options.Inet4Address) > 0 {
236245
for _, address := range options.Inet4Address {
237246
ifReq := ifAliasReq{
@@ -396,11 +405,14 @@ func (t *NativeTun) TXChecksumOffload() bool {
396405
}
397406

398407
func (t *NativeTun) UpdateRouteOptions(tunOptions Options) error {
408+
t.options = tunOptions
409+
if t.options.EXP_ExternalConfiguration {
410+
return nil
411+
}
399412
err := t.unsetRoutes()
400413
if err != nil {
401414
return err
402415
}
403-
t.options = tunOptions
404416
return t.setRoutes()
405417
}
406418

tun_linux.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,23 @@ func (t *NativeTun) configure(tunLink netlink.Link) error {
125125
} else if err != nil {
126126
return err
127127
}
128-
129-
if len(t.options.Inet4Address) > 0 {
130-
for _, address := range t.options.Inet4Address {
131-
addr4, _ := netlink.ParseAddr(address.String())
132-
err = netlink.AddrAdd(tunLink, addr4)
133-
if err != nil {
134-
return err
128+
if !t.options.EXP_ExternalConfiguration {
129+
if len(t.options.Inet4Address) > 0 {
130+
for _, address := range t.options.Inet4Address {
131+
addr4, _ := netlink.ParseAddr(address.String())
132+
err = netlink.AddrAdd(tunLink, addr4)
133+
if err != nil {
134+
return err
135+
}
135136
}
136137
}
137-
}
138-
if len(t.options.Inet6Address) > 0 {
139-
for _, address := range t.options.Inet6Address {
140-
addr6, _ := netlink.ParseAddr(address.String())
141-
err = netlink.AddrAdd(tunLink, addr6)
142-
if err != nil {
143-
return err
138+
if len(t.options.Inet6Address) > 0 {
139+
for _, address := range t.options.Inet6Address {
140+
addr6, _ := netlink.ParseAddr(address.String())
141+
err = netlink.AddrAdd(tunLink, addr6)
142+
if err != nil {
143+
return err
144+
}
144145
}
145146
}
146147
}
@@ -257,7 +258,9 @@ func (t *NativeTun) Start() error {
257258
if t.options.FileDescriptor != 0 {
258259
return nil
259260
}
260-
t.options.InterfaceMonitor.RegisterMyInterface(t.options.Name)
261+
if !t.options.EXP_ExternalConfiguration {
262+
t.options.InterfaceMonitor.RegisterMyInterface(t.options.Name)
263+
}
261264
tunLink, err := netlink.LinkByName(t.options.Name)
262265
if err != nil {
263266
return err
@@ -277,6 +280,10 @@ func (t *NativeTun) Start() error {
277280
}
278281
}
279282

283+
if t.options.EXP_ExternalConfiguration {
284+
return nil
285+
}
286+
280287
if t.options.IPRoute2TableIndex == 0 {
281288
for {
282289
t.options.IPRoute2TableIndex = int(rand.Uint32())
@@ -315,6 +322,9 @@ func (t *NativeTun) Close() error {
315322
if t.interfaceCallback != nil {
316323
t.options.InterfaceMonitor.UnregisterCallback(t.interfaceCallback)
317324
}
325+
if t.options.EXP_ExternalConfiguration {
326+
return common.Close(common.PtrOrNil(t.tunFile))
327+
}
318328
return E.Errors(t.unsetRoute(), t.unsetRules(), common.Close(common.PtrOrNil(t.tunFile)))
319329
}
320330

@@ -476,6 +486,9 @@ func prefixToIPNet(prefix netip.Prefix) *net.IPNet {
476486
func (t *NativeTun) UpdateRouteOptions(tunOptions Options) error {
477487
if t.options.FileDescriptor > 0 {
478488
return nil
489+
} else if t.options.EXP_ExternalConfiguration {
490+
t.options = tunOptions
491+
return nil
479492
} else if !t.options.AutoRoute {
480493
t.options = tunOptions
481494
return nil

tun_windows.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ func New(options Options) (WinTun, error) {
6565
}
6666

6767
func (t *NativeTun) configure() error {
68+
if t.options.EXP_ExternalConfiguration {
69+
return nil
70+
}
6871
luid := winipcfg.LUID(t.adapter.LUID())
6972
if len(t.options.Inet4Address) > 0 {
7073
err := luid.SetIPAddressesForFamily(winipcfg.AddressFamily(windows.AF_INET), t.options.Inet4Address)
@@ -162,10 +165,10 @@ func (t *NativeTun) Name() (string, error) {
162165
}
163166

164167
func (t *NativeTun) Start() error {
165-
t.options.InterfaceMonitor.RegisterMyInterface(t.options.Name)
166-
if !t.options.AutoRoute {
168+
if t.options.EXP_ExternalConfiguration || !t.options.AutoRoute {
167169
return nil
168170
}
171+
t.options.InterfaceMonitor.RegisterMyInterface(t.options.Name)
169172
luid := winipcfg.LUID(t.adapter.LUID())
170173
gateway4, gateway6 := t.options.Inet4GatewayAddr(), t.options.Inet6GatewayAddr()
171174
routeRanges, err := t.options.BuildAutoRouteRanges(false)
@@ -393,6 +396,21 @@ retry:
393396
}
394397
}
395398

399+
func (t *NativeTun) MTU() (int, error) {
400+
return int(t.options.MTU), nil
401+
}
402+
403+
func (t *NativeTun) ForceMTU(mtu int) {
404+
if mtu <= 0 {
405+
return
406+
}
407+
t.options.MTU = uint32(mtu)
408+
}
409+
410+
func (t *NativeTun) LUID() uint64 {
411+
return t.adapter.LUID()
412+
}
413+
396414
func (t *NativeTun) ReadPacket() ([]byte, func(), error) {
397415
t.running.Add(1)
398416
retry:
@@ -543,6 +561,9 @@ func (t *NativeTun) Close() error {
543561

544562
func (t *NativeTun) UpdateRouteOptions(tunOptions Options) error {
545563
t.options = tunOptions
564+
if t.options.EXP_ExternalConfiguration {
565+
return nil
566+
}
546567
if !t.options.AutoRoute {
547568
return nil
548569
}

0 commit comments

Comments
 (0)