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 (
|
2020-04-10 15:42:34 +00:00
|
|
|
"errors"
|
2020-02-05 22:16:58 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tailscale/wireguard-go/wgcfg"
|
2020-03-26 05:57:46 +00:00
|
|
|
"tailscale.com/ipn/ipnstate"
|
2020-02-05 22:16:58 +00:00
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/wgengine/filter"
|
2020-05-11 21:02:12 +00:00
|
|
|
"tailscale.com/wgengine/router"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
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-03-26 05:57:46 +00:00
|
|
|
//
|
|
|
|
// TODO(bradfitz): remove this, subset of ipnstate? Need to migrate users.
|
2020-02-05 22:16:58 +00:00
|
|
|
type Status struct {
|
|
|
|
Peers []PeerStatus
|
|
|
|
LocalAddrs []string // TODO(crawshaw): []wgcfg.Endpoint?
|
2020-03-19 06:55:14 +00:00
|
|
|
DERPs int // number of active DERP connections
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
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-03-04 06:21:56 +00:00
|
|
|
// NetInfoCallback is the type used by Engine.SetNetInfoCallback.
|
|
|
|
type NetInfoCallback func(*tailcfg.NetInfo)
|
|
|
|
|
2020-04-10 15:42:34 +00:00
|
|
|
// ErrNoChanges is returned by Engine.Reconfig if no changes were made.
|
|
|
|
var ErrNoChanges = errors.New("no changes made to Engine config")
|
|
|
|
|
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-04-10 15:42:34 +00:00
|
|
|
//
|
|
|
|
// The returned error is ErrNoChanges if no changes were made.
|
2020-05-11 21:02:12 +00:00
|
|
|
Reconfig(cfg *wgcfg.Config, routerCfg router.Settings) error
|
2020-02-11 23:21:24 +00:00
|
|
|
|
2020-03-25 15:40:36 +00:00
|
|
|
// GetFilter returns the current packet filter, if any.
|
2020-03-25 07:47:55 +00:00
|
|
|
GetFilter() *filter.Filter
|
|
|
|
|
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-03-13 03:10:11 +00:00
|
|
|
//
|
|
|
|
// LinkChange should be called whenever something changed with
|
|
|
|
// the network, no matter how minor. The implementation should
|
|
|
|
// look at the state of the network and decide whether the
|
|
|
|
// change from before is interesting enough to warrant taking
|
|
|
|
// action on.
|
2020-02-05 22:16:58 +00:00
|
|
|
LinkChange(isExpensive bool)
|
2020-03-04 06:21:56 +00:00
|
|
|
|
2020-03-04 20:21:40 +00:00
|
|
|
// SetDERPEnabled controls whether DERP is enabled.
|
|
|
|
// It starts enabled by default.
|
|
|
|
SetDERPEnabled(bool)
|
|
|
|
|
2020-03-04 06:21:56 +00:00
|
|
|
// SetNetInfoCallback sets the function to call when a
|
|
|
|
// new NetInfo summary is available.
|
|
|
|
SetNetInfoCallback(NetInfoCallback)
|
2020-03-26 05:57:46 +00:00
|
|
|
|
|
|
|
// UpdateStatus populates the network state using the provided
|
|
|
|
// status builder.
|
|
|
|
UpdateStatus(*ipnstate.StatusBuilder)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|