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 (
|
|
|
|
"log"
|
|
|
|
|
2020-02-13 05:11:41 +00:00
|
|
|
winipcfg "github.com/tailscale/winipcfg-go"
|
2020-02-05 22:16:58 +00:00
|
|
|
"github.com/tailscale/wireguard-go/device"
|
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
2020-02-15 03:23:16 +00:00
|
|
|
"tailscale.com/types/logger"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
2020-03-08 05:05:34 +00:00
|
|
|
const DefaultTunName = "tailscale0"
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
type winRouter struct {
|
|
|
|
logf func(fmt string, args ...interface{})
|
|
|
|
tunname string
|
|
|
|
nativeTun *tun.NativeTun
|
2020-02-14 23:03:25 +00:00
|
|
|
wgdev *device.Device
|
2020-02-05 22:16:58 +00:00
|
|
|
routeChangeCallback *winipcfg.RouteChangeCallback
|
|
|
|
}
|
|
|
|
|
2020-02-17 17:00:38 +00:00
|
|
|
func newUserspaceRouter(logf logger.Logf, wgdev *device.Device, tundev tun.Device) (Router, error) {
|
2020-02-14 23:03:25 +00:00
|
|
|
tunname, err := tundev.Name()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &winRouter{
|
2020-02-05 22:16:58 +00:00
|
|
|
logf: logf,
|
2020-02-14 23:03:25 +00:00
|
|
|
wgdev: wgdev,
|
2020-02-05 22:16:58 +00:00
|
|
|
tunname: tunname,
|
2020-02-14 23:03:25 +00:00
|
|
|
nativeTun: tundev.(*tun.NativeTun),
|
|
|
|
}, nil
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *winRouter) Up() error {
|
|
|
|
// MonitorDefaultRoutes handles making sure our wireguard UDP
|
|
|
|
// traffic goes through the old route, not recursively through the VPN.
|
|
|
|
var err error
|
2020-04-05 15:14:55 +00:00
|
|
|
r.routeChangeCallback, err = monitorDefaultRoutes(r.wgdev, true, r.nativeTun)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("MonitorDefaultRoutes: %v\n", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *winRouter) SetRoutes(rs RouteSettings) error {
|
2020-04-05 15:14:55 +00:00
|
|
|
err := configureInterface(rs.Cfg, r.nativeTun, rs.DNS, rs.DNSDomains)
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
r.logf("ConfigureInterface: %v\n", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-13 00:16:05 +00:00
|
|
|
func (r *winRouter) Close() error {
|
2020-02-05 22:16:58 +00:00
|
|
|
if r.routeChangeCallback != nil {
|
|
|
|
r.routeChangeCallback.Unregister()
|
|
|
|
}
|
2020-02-13 00:16:05 +00:00
|
|
|
return nil
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|