@ -10,6 +10,7 @@ import (
"strings"
"strings"
"github.com/coreos/go-iptables/iptables"
"github.com/coreos/go-iptables/iptables"
"github.com/go-multierror/multierror"
"github.com/tailscale/wireguard-go/device"
"github.com/tailscale/wireguard-go/device"
"github.com/tailscale/wireguard-go/tun"
"github.com/tailscale/wireguard-go/tun"
"inet.af/netaddr"
"inet.af/netaddr"
@ -145,7 +146,7 @@ func (r *linuxRouter) Up() error {
func ( r * linuxRouter ) Close ( ) error {
func ( r * linuxRouter ) Close ( ) error {
if err := r . dns . Down ( ) ; err != nil {
if err := r . dns . Down ( ) ; err != nil {
return fmt . Errorf ( "dns down: % v ", err )
return fmt . Errorf ( "dns down: % w ", err )
}
}
if err := r . downInterface ( ) ; err != nil {
if err := r . downInterface ( ) ; err != nil {
return err
return err
@ -165,23 +166,28 @@ func (r *linuxRouter) Close() error {
// Set implements the Router interface.
// Set implements the Router interface.
func ( r * linuxRouter ) Set ( cfg * Config ) error {
func ( r * linuxRouter ) Set ( cfg * Config ) error {
var errs [ ] error
if cfg == nil {
if cfg == nil {
cfg = & shutdownConfig
cfg = & shutdownConfig
}
}
if err := r . dns . Set ( cfg . DNS ) ; err != nil {
errs = append ( errs , fmt . Errorf ( "dns set: %w" , err ) )
}
if err := r . setNetfilterMode ( cfg . NetfilterMode ) ; err != nil {
if err := r . setNetfilterMode ( cfg . NetfilterMode ) ; err != nil {
return err
errs = append ( errs , err )
}
}
newRoutes , err := cidrDiff ( "route" , r . routes , cfg . Routes , r . addRoute , r . delRoute , r . logf )
newRoutes , err := cidrDiff ( "route" , r . routes , cfg . Routes , r . addRoute , r . delRoute , r . logf )
if err != nil {
if err != nil {
return err
errs = append ( errs , err )
}
}
r . routes = newRoutes
r . routes = newRoutes
newAddrs , err := cidrDiff ( "addr" , r . addrs , cfg . LocalAddrs , r . addAddress , r . delAddress , r . logf )
newAddrs , err := cidrDiff ( "addr" , r . addrs , cfg . LocalAddrs , r . addAddress , r . delAddress , r . logf )
if err != nil {
if err != nil {
return err
errs = append ( errs , err )
}
}
r . addrs = newAddrs
r . addrs = newAddrs
@ -190,20 +196,16 @@ func (r *linuxRouter) Set(cfg *Config) error {
// state already correct, nothing to do.
// state already correct, nothing to do.
case cfg . SNATSubnetRoutes :
case cfg . SNATSubnetRoutes :
if err := r . addSNATRule ( ) ; err != nil {
if err := r . addSNATRule ( ) ; err != nil {
return err
errs = append ( errs , err )
}
}
default :
default :
if err := r . delSNATRule ( ) ; err != nil {
if err := r . delSNATRule ( ) ; err != nil {
return err
errs = append ( errs , err )
}
}
}
}
r . snatSubnetRoutes = cfg . SNATSubnetRoutes
r . snatSubnetRoutes = cfg . SNATSubnetRoutes
if err := r . dns . Set ( cfg . DNS ) ; err != nil {
return multierror . New ( errs )
return fmt . Errorf ( "dns set: %v" , err )
}
return nil
}
}
// setNetfilterMode switches the router to the given netfilter
// setNetfilterMode switches the router to the given netfilter