|
|
@ -132,6 +132,7 @@ type TailscaledEnv struct {
|
|
|
|
func (c *Config) AddNetwork(opts ...any) *Network {
|
|
|
|
func (c *Config) AddNetwork(opts ...any) *Network {
|
|
|
|
num := len(c.networks)
|
|
|
|
num := len(c.networks)
|
|
|
|
n := &Network{
|
|
|
|
n := &Network{
|
|
|
|
|
|
|
|
num: num + 1,
|
|
|
|
mac: MAC{0x52, 0xee, 0xee, 0xee, 0xee, byte(num) + 1}, // 52=TS then 0xee for 'etwork
|
|
|
|
mac: MAC{0x52, 0xee, 0xee, 0xee, 0xee, byte(num) + 1}, // 52=TS then 0xee for 'etwork
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.networks = append(c.networks, n)
|
|
|
|
c.networks = append(c.networks, n)
|
|
|
@ -139,9 +140,15 @@ func (c *Config) AddNetwork(opts ...any) *Network {
|
|
|
|
switch o := o.(type) {
|
|
|
|
switch o := o.(type) {
|
|
|
|
case string:
|
|
|
|
case string:
|
|
|
|
if ip, err := netip.ParseAddr(o); err == nil {
|
|
|
|
if ip, err := netip.ParseAddr(o); err == nil {
|
|
|
|
n.wanIP = ip
|
|
|
|
n.wanIP4 = ip
|
|
|
|
} else if ip, err := netip.ParsePrefix(o); err == nil {
|
|
|
|
} else if ip, err := netip.ParsePrefix(o); err == nil {
|
|
|
|
n.lanIP = ip
|
|
|
|
// If the prefix is IPv4, treat it as the router's internal IPv4 address + CIDR.
|
|
|
|
|
|
|
|
// If the prefix is IPv6, treat it as the router's WAN IPv6 + CIDR (typically a /64).
|
|
|
|
|
|
|
|
if ip.Addr().Is4() {
|
|
|
|
|
|
|
|
n.lanIP4 = ip
|
|
|
|
|
|
|
|
} else if ip.Addr().Is6() {
|
|
|
|
|
|
|
|
n.wanIP6 = ip
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if n.err == nil {
|
|
|
|
if n.err == nil {
|
|
|
|
n.err = fmt.Errorf("unknown string option %q", o)
|
|
|
|
n.err = fmt.Errorf("unknown string option %q", o)
|
|
|
@ -208,6 +215,21 @@ func (n *Node) SetVerboseSyslog(v bool) {
|
|
|
|
n.verboseSyslog = v
|
|
|
|
n.verboseSyslog = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// IsV6Only reports whether this node is only connected to IPv6 networks.
|
|
|
|
|
|
|
|
func (n *Node) IsV6Only() bool {
|
|
|
|
|
|
|
|
for _, net := range n.nets {
|
|
|
|
|
|
|
|
if net.CanV4() {
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, net := range n.nets {
|
|
|
|
|
|
|
|
if net.CanV6() {
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Network returns the first network this node is connected to,
|
|
|
|
// Network returns the first network this node is connected to,
|
|
|
|
// or nil if none.
|
|
|
|
// or nil if none.
|
|
|
|
func (n *Node) Network() *Network {
|
|
|
|
func (n *Node) Network() *Network {
|
|
|
@ -219,12 +241,15 @@ func (n *Node) Network() *Network {
|
|
|
|
|
|
|
|
|
|
|
|
// Network is the configuration of a network in the virtual network.
|
|
|
|
// Network is the configuration of a network in the virtual network.
|
|
|
|
type Network struct {
|
|
|
|
type Network struct {
|
|
|
|
|
|
|
|
num int // 1-based
|
|
|
|
mac MAC // MAC address of the router/gateway
|
|
|
|
mac MAC // MAC address of the router/gateway
|
|
|
|
natType NAT
|
|
|
|
natType NAT
|
|
|
|
|
|
|
|
|
|
|
|
wanIP netip.Addr
|
|
|
|
wanIP6 netip.Prefix // global unicast router in host bits; CIDR is /64 delegated to LAN
|
|
|
|
lanIP netip.Prefix
|
|
|
|
|
|
|
|
nodes []*Node
|
|
|
|
wanIP4 netip.Addr // IPv4 WAN IP, if any
|
|
|
|
|
|
|
|
lanIP4 netip.Prefix
|
|
|
|
|
|
|
|
nodes []*Node
|
|
|
|
|
|
|
|
|
|
|
|
svcs set.Set[NetworkService]
|
|
|
|
svcs set.Set[NetworkService]
|
|
|
|
|
|
|
|
|
|
|
@ -232,6 +257,14 @@ type Network struct {
|
|
|
|
err error // carried error
|
|
|
|
err error // carried error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (n *Network) CanV4() bool {
|
|
|
|
|
|
|
|
return n.lanIP4.IsValid() || n.wanIP4.IsValid()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (n *Network) CanV6() bool {
|
|
|
|
|
|
|
|
return n.wanIP6.IsValid()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (n *Network) CanTakeMoreNodes() bool {
|
|
|
|
func (n *Network) CanTakeMoreNodes() bool {
|
|
|
|
if n.natType == One2OneNAT {
|
|
|
|
if n.natType == One2OneNAT {
|
|
|
|
return len(n.nodes) == 0
|
|
|
|
return len(n.nodes) == 0
|
|
|
@ -282,24 +315,43 @@ func (s *Server) initFromConfig(c *Config) error {
|
|
|
|
if conf.err != nil {
|
|
|
|
if conf.err != nil {
|
|
|
|
return conf.err
|
|
|
|
return conf.err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !conf.lanIP.IsValid() {
|
|
|
|
if !conf.lanIP4.IsValid() && !conf.wanIP6.IsValid() {
|
|
|
|
conf.lanIP = netip.MustParsePrefix("192.168.0.0/24")
|
|
|
|
conf.lanIP4 = netip.MustParsePrefix("192.168.0.0/24")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n := &network{
|
|
|
|
n := &network{
|
|
|
|
s: s,
|
|
|
|
num: conf.num,
|
|
|
|
mac: conf.mac,
|
|
|
|
s: s,
|
|
|
|
portmap: conf.svcs.Contains(NATPMP), // TODO: expand network.portmap
|
|
|
|
mac: conf.mac,
|
|
|
|
wanIP: conf.wanIP,
|
|
|
|
portmap: conf.svcs.Contains(NATPMP), // TODO: expand network.portmap
|
|
|
|
lanIP: conf.lanIP,
|
|
|
|
wanIP6: conf.wanIP6,
|
|
|
|
nodesByIP: map[netip.Addr]*node{},
|
|
|
|
v4: conf.lanIP4.IsValid(),
|
|
|
|
logf: logger.WithPrefix(log.Printf, fmt.Sprintf("[net-%v] ", conf.mac)),
|
|
|
|
v6: conf.wanIP6.IsValid(),
|
|
|
|
|
|
|
|
wanIP4: conf.wanIP4,
|
|
|
|
|
|
|
|
lanIP4: conf.lanIP4,
|
|
|
|
|
|
|
|
nodesByIP: map[netip.Addr]*node{},
|
|
|
|
|
|
|
|
nodesByMAC: map[MAC]*node{},
|
|
|
|
|
|
|
|
logf: logger.WithPrefix(log.Printf, fmt.Sprintf("[net-%v] ", conf.mac)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
netOfConf[conf] = n
|
|
|
|
netOfConf[conf] = n
|
|
|
|
s.networks.Add(n)
|
|
|
|
s.networks.Add(n)
|
|
|
|
if _, ok := s.networkByWAN[conf.wanIP]; ok {
|
|
|
|
if conf.wanIP4.IsValid() {
|
|
|
|
return fmt.Errorf("two networks have the same WAN IP %v; Anycast not (yet?) supported", conf.wanIP)
|
|
|
|
if conf.wanIP4.Is6() {
|
|
|
|
|
|
|
|
return fmt.Errorf("invalid IPv6 address in wanIP")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := s.networkByWAN.Lookup(conf.wanIP4); ok {
|
|
|
|
|
|
|
|
return fmt.Errorf("two networks have the same WAN IP %v; Anycast not (yet?) supported", conf.wanIP4)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
s.networkByWAN.Insert(netip.PrefixFrom(conf.wanIP4, 32), n)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.wanIP6.IsValid() {
|
|
|
|
|
|
|
|
if conf.wanIP6.Addr().Is4() {
|
|
|
|
|
|
|
|
return fmt.Errorf("invalid IPv4 address in wanIP6")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := s.networkByWAN.LookupPrefix(conf.wanIP6); ok {
|
|
|
|
|
|
|
|
return fmt.Errorf("two networks have the same WAN IPv6 %v; Anycast not (yet?) supported", conf.wanIP6)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
s.networkByWAN.Insert(conf.wanIP6, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.networkByWAN[conf.wanIP] = n
|
|
|
|
|
|
|
|
n.lanInterfaceID = must.Get(s.pcapWriter.AddInterface(pcapgo.NgInterface{
|
|
|
|
n.lanInterfaceID = must.Get(s.pcapWriter.AddInterface(pcapgo.NgInterface{
|
|
|
|
Name: fmt.Sprintf("network%d-lan", i+1),
|
|
|
|
Name: fmt.Sprintf("network%d-lan", i+1),
|
|
|
|
LinkType: layers.LinkTypeIPv4,
|
|
|
|
LinkType: layers.LinkTypeIPv4,
|
|
|
@ -330,13 +382,16 @@ func (s *Server) initFromConfig(c *Config) error {
|
|
|
|
s.nodes = append(s.nodes, n)
|
|
|
|
s.nodes = append(s.nodes, n)
|
|
|
|
s.nodeByMAC[n.mac] = n
|
|
|
|
s.nodeByMAC[n.mac] = n
|
|
|
|
|
|
|
|
|
|
|
|
// Allocate a lanIP for the node. Use the network's CIDR and use final
|
|
|
|
if n.net.v4 {
|
|
|
|
// octet 101 (for first node), 102, etc. The node number comes from the
|
|
|
|
// Allocate a lanIP for the node. Use the network's CIDR and use final
|
|
|
|
// last octent of the MAC address (0-based)
|
|
|
|
// octet 101 (for first node), 102, etc. The node number comes from the
|
|
|
|
ip4 := n.net.lanIP.Addr().As4()
|
|
|
|
// last octent of the MAC address (0-based)
|
|
|
|
ip4[3] = 100 + n.mac[5]
|
|
|
|
ip4 := n.net.lanIP4.Addr().As4()
|
|
|
|
n.lanIP = netip.AddrFrom4(ip4)
|
|
|
|
ip4[3] = 100 + n.mac[5]
|
|
|
|
n.net.nodesByIP[n.lanIP] = n
|
|
|
|
n.lanIP = netip.AddrFrom4(ip4)
|
|
|
|
|
|
|
|
n.net.nodesByIP[n.lanIP] = n
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
n.net.nodesByMAC[n.mac] = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now that nodes are populated, set up NAT:
|
|
|
|
// Now that nodes are populated, set up NAT:
|
|
|
|