2020-02-05 22:16:58 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package wgengine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tailscale/wireguard-go/wgcfg"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/wgengine/filter"
|
|
|
|
)
|
|
|
|
|
2020-02-11 23:21:24 +00:00
|
|
|
// ByteCount is the number of bytes that have been sent or received.
|
|
|
|
//
|
|
|
|
// TODO: why is this a type? remove?
|
|
|
|
// TODO: document whether it's payload bytes only or if it includes framing overhead.
|
2020-02-05 22:16:58 +00:00
|
|
|
type ByteCount int64
|
|
|
|
|
|
|
|
type PeerStatus struct {
|
|
|
|
TxBytes, RxBytes ByteCount
|
|
|
|
LastHandshake time.Time
|
|
|
|
NodeKey tailcfg.NodeKey
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:21:24 +00:00
|
|
|
// Status is the Engine status.
|
2020-02-05 22:16:58 +00:00
|
|
|
type Status struct {
|
|
|
|
Peers []PeerStatus
|
|
|
|
LocalAddrs []string // TODO(crawshaw): []wgcfg.Endpoint?
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:21:24 +00:00
|
|
|
// StatusCallback is the type of status callbacks used by
|
|
|
|
// Engine.SetStatusCallback.
|
|
|
|
//
|
|
|
|
// Exactly one of Status or error is non-nil.
|
|
|
|
type StatusCallback func(*Status, error)
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2020-02-11 23:21:24 +00:00
|
|
|
// RouteSettings is the full WireGuard config data (set of peers keys,
|
|
|
|
// IP, etc in wgcfg.Config) plus the things that WireGuard doesn't do
|
|
|
|
// itself, like DNS stuff.
|
2020-02-05 22:16:58 +00:00
|
|
|
type RouteSettings struct {
|
2020-02-11 23:21:24 +00:00
|
|
|
LocalAddr wgcfg.CIDR // TODO: why is this here? how does it differ from wgcfg.Config's info?
|
2020-02-05 22:16:58 +00:00
|
|
|
DNS []net.IP
|
|
|
|
DNSDomains []string
|
2020-02-11 23:21:24 +00:00
|
|
|
Cfg wgcfg.Config // TODO: value type here, but pointer below?
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 23:21:24 +00:00
|
|
|
// OnlyRelevantParts returns a string minimally describing the route settings.
|
2020-02-05 22:16:58 +00:00
|
|
|
func (rs *RouteSettings) OnlyRelevantParts() string {
|
|
|
|
var peers [][]wgcfg.CIDR
|
|
|
|
for _, p := range rs.Cfg.Peers {
|
|
|
|
peers = append(peers, p.AllowedIPs)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v %v %v %v",
|
|
|
|
rs.LocalAddr, rs.DNS, rs.DNSDomains, peers)
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:21:24 +00:00
|
|
|
// Router is the TODO.
|
2020-02-05 22:16:58 +00:00
|
|
|
type Router interface {
|
2020-02-11 23:21:24 +00:00
|
|
|
// Up brings the router up.
|
|
|
|
// TODO: more than once? after Close?
|
2020-02-05 22:16:58 +00:00
|
|
|
Up() error
|
2020-02-11 23:21:24 +00:00
|
|
|
// SetRoutes sets the routes.
|
|
|
|
// TODO: while running?
|
|
|
|
SetRoutes(RouteSettings) error
|
|
|
|
// Close closes the router.
|
|
|
|
// TODO: return an error? does this block?
|
2020-02-05 22:16:58 +00:00
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:21:24 +00:00
|
|
|
// Engine is the Tailscale WireGuard engine interface.
|
2020-02-05 22:16:58 +00:00
|
|
|
type Engine interface {
|
2020-02-11 23:21:24 +00:00
|
|
|
// Reconfig reconfigures WireGuard and makes sure it's running.
|
2020-02-05 22:16:58 +00:00
|
|
|
// This also handles setting up any kernel routes.
|
2020-02-11 23:21:24 +00:00
|
|
|
//
|
|
|
|
// The provided DNS domains are not part of wgcfg.Config, as
|
|
|
|
// WireGuard itself doesn't care about such things.
|
|
|
|
//
|
|
|
|
// This is called whenever the tailcontrol (control plane)
|
|
|
|
// sends an updated network map.
|
2020-02-05 22:16:58 +00:00
|
|
|
Reconfig(cfg *wgcfg.Config, dnsDomains []string) error
|
2020-02-11 23:21:24 +00:00
|
|
|
|
|
|
|
// SetFilter updates the packet filter.
|
|
|
|
SetFilter(*filter.Filter)
|
|
|
|
|
|
|
|
// SetStatusCallback sets the function to call when the
|
|
|
|
// WireGuard status changes.
|
|
|
|
SetStatusCallback(StatusCallback)
|
|
|
|
|
|
|
|
// RequestStatus requests a WireGuard status update right
|
|
|
|
// away, sent to the callback registered via SetStatusCallback.
|
2020-02-05 22:16:58 +00:00
|
|
|
RequestStatus()
|
2020-02-11 23:21:24 +00:00
|
|
|
|
|
|
|
// Close shuts down this wireguard instance, remove any routes
|
|
|
|
// it added, etc. To bring it up again later, you'll need a
|
|
|
|
// new Engine.
|
2020-02-05 22:16:58 +00:00
|
|
|
Close()
|
2020-02-11 23:21:24 +00:00
|
|
|
|
|
|
|
// Wait waits until the Engine's Close method is called or the
|
|
|
|
// engine aborts with an error. You don't have to call this.
|
|
|
|
// TODO: return an error?
|
2020-02-05 22:16:58 +00:00
|
|
|
Wait()
|
2020-02-11 23:21:24 +00:00
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
// LinkChange informs the engine that the system network
|
|
|
|
// link has changed. The isExpensive parameter is set on links
|
2020-02-11 23:21:24 +00:00
|
|
|
// where sending packets uses substantial power or money,
|
|
|
|
// such as mobile data on a phone.
|
2020-02-05 22:16:58 +00:00
|
|
|
LinkChange(isExpensive bool)
|
|
|
|
}
|