2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-04-03 01:43:24 +00:00
|
|
|
|
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"tailscale.com/net/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CallbackRouter is an implementation of both Router and dns.OSConfigurator.
|
|
|
|
// When either network or DNS settings are changed, SetBoth is called with both configs.
|
|
|
|
// Mainly used as a shim for OSes that want to set both network and
|
2022-06-28 22:32:09 +00:00
|
|
|
// DNS configuration simultaneously (Mac, iOS, Android).
|
2021-04-03 01:43:24 +00:00
|
|
|
type CallbackRouter struct {
|
2021-04-05 20:05:47 +00:00
|
|
|
SetBoth func(rcfg *Config, dcfg *dns.OSConfig) error
|
|
|
|
SplitDNS bool
|
2021-04-03 01:43:24 +00:00
|
|
|
|
2021-10-09 19:17:59 +00:00
|
|
|
// GetBaseConfigFunc optionally specifies a function to return the current DNS
|
|
|
|
// config in response to GetBaseConfig.
|
|
|
|
//
|
|
|
|
// If nil, reading the current config isn't supported and GetBaseConfig()
|
|
|
|
// will return ErrGetBaseConfigNotSupported.
|
|
|
|
GetBaseConfigFunc func() (dns.OSConfig, error)
|
|
|
|
|
2023-07-18 21:51:32 +00:00
|
|
|
// InitialMTU is the MTU the tun should be initialized with.
|
|
|
|
// Zero means don't change the MTU from the default. This MTU
|
|
|
|
// is applied only once, shortly after the TUN is created, and
|
|
|
|
// ignored thereafter.
|
|
|
|
InitialMTU uint32
|
|
|
|
|
|
|
|
mu sync.Mutex // protects all the following
|
|
|
|
didSetMTU bool // if we set the MTU already
|
|
|
|
rcfg *Config // last applied router config
|
|
|
|
dcfg *dns.OSConfig // last applied DNS config
|
2021-04-03 01:43:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Up implements Router.
|
|
|
|
func (r *CallbackRouter) Up() error {
|
|
|
|
return nil // TODO: check that all callers have no need for initialization
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set implements Router.
|
|
|
|
func (r *CallbackRouter) Set(rcfg *Config) error {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
2022-06-28 22:32:09 +00:00
|
|
|
if r.rcfg.Equal(rcfg) {
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-18 21:51:32 +00:00
|
|
|
if r.didSetMTU == false {
|
|
|
|
r.didSetMTU = true
|
|
|
|
rcfg.NewMTU = int(r.InitialMTU)
|
|
|
|
}
|
2021-04-03 01:43:24 +00:00
|
|
|
r.rcfg = rcfg
|
|
|
|
return r.SetBoth(r.rcfg, r.dcfg)
|
|
|
|
}
|
|
|
|
|
2023-12-07 15:45:14 +00:00
|
|
|
// UpdateMagicsockPort implements the Router interface. This implementation
|
|
|
|
// does nothing and returns nil because this router does not currently need
|
|
|
|
// to know what the magicsock UDP port is.
|
|
|
|
func (r *CallbackRouter) UpdateMagicsockPort(_ uint16, _ string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-03 01:43:24 +00:00
|
|
|
// SetDNS implements dns.OSConfigurator.
|
|
|
|
func (r *CallbackRouter) SetDNS(dcfg dns.OSConfig) error {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
2022-06-28 22:32:09 +00:00
|
|
|
if r.dcfg != nil && r.dcfg.Equal(dcfg) {
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-03 01:43:24 +00:00
|
|
|
r.dcfg = &dcfg
|
|
|
|
return r.SetBoth(r.rcfg, r.dcfg)
|
|
|
|
}
|
|
|
|
|
2021-04-05 20:05:47 +00:00
|
|
|
// SupportsSplitDNS implements dns.OSConfigurator.
|
|
|
|
func (r *CallbackRouter) SupportsSplitDNS() bool {
|
|
|
|
return r.SplitDNS
|
2021-04-03 01:43:24 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 07:31:31 +00:00
|
|
|
func (r *CallbackRouter) GetBaseConfig() (dns.OSConfig, error) {
|
2021-10-09 19:17:59 +00:00
|
|
|
if r.GetBaseConfigFunc == nil {
|
|
|
|
return dns.OSConfig{}, dns.ErrGetBaseConfigNotSupported
|
|
|
|
}
|
|
|
|
return r.GetBaseConfigFunc()
|
2021-04-07 07:31:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 01:43:24 +00:00
|
|
|
func (r *CallbackRouter) Close() error {
|
|
|
|
return r.SetBoth(nil, nil) // TODO: check if makes sense
|
|
|
|
}
|