Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type Options struct {
Inet6Address []netip.Prefix
MTU uint32
GSO bool
InterfaceMetric uint32
RouteMetric uint32
AutoRoute bool
InterfaceScope bool
Inet4Gateway netip.Addr
Expand Down
6 changes: 5 additions & 1 deletion tun_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,16 @@ func (t *NativeTun) routes(tunLink netlink.Link) ([]netlink.Route, error) {
} else if it.Addr().Is6() && !gateway6.IsUnspecified() {
gateway = gateway6.AsSlice()
}
return netlink.Route{
route := netlink.Route{
Dst: prefixToIPNet(it),
Gw: gateway,
LinkIndex: tunLink.Attrs().Index,
Table: t.options.IPRoute2TableIndex,
}
if t.options.RouteMetric > 0 {
route.Priority = int(t.options.RouteMetric)
}
return route
}), nil
}

Expand Down
18 changes: 12 additions & 6 deletions tun_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ func (t *NativeTun) configure() error {
inetIf.ManagedAddressConfigurationSupported = false
inetIf.OtherStatefulConfigurationSupported = false
inetIf.NLMTU = t.options.MTU
if t.options.AutoRoute {
if t.options.InterfaceMetric > 0 {
inetIf.UseAutomaticMetric = false
inetIf.Metric = t.options.InterfaceMetric
} else if t.options.AutoRoute {
inetIf.UseAutomaticMetric = false
inetIf.Metric = 0
}
Expand All @@ -148,7 +151,10 @@ func (t *NativeTun) configure() error {
inet6If.ManagedAddressConfigurationSupported = false
inet6If.OtherStatefulConfigurationSupported = false
inet6If.NLMTU = t.options.MTU
if t.options.AutoRoute {
if t.options.InterfaceMetric > 0 {
inet6If.UseAutomaticMetric = false
inet6If.Metric = t.options.InterfaceMetric
} else if t.options.AutoRoute {
inet6If.UseAutomaticMetric = false
inet6If.Metric = 0
}
Expand All @@ -175,7 +181,7 @@ func (t *NativeTun) Start() error {
if err != nil {
return err
}
err = addRouteList(luid, routeRanges, gateway4, gateway6, 0)
err = t.addRouteList(luid, routeRanges, gateway4, gateway6)
if err != nil {
return err
}
Expand Down Expand Up @@ -584,7 +590,7 @@ func (t *NativeTun) UpdateRouteOptions(tunOptions Options) error {
if err != nil {
return err
}
err = addRouteList(luid, routeRanges, gateway4, gateway6, 0)
err = t.addRouteList(luid, routeRanges, gateway4, gateway6)
if err != nil {
return err
}
Expand All @@ -595,11 +601,11 @@ func (t *NativeTun) UpdateRouteOptions(tunOptions Options) error {
return nil
}

func addRouteList(luid winipcfg.LUID, destinations []netip.Prefix, gateway4 netip.Addr, gateway6 netip.Addr, metric uint32) error {
func (t *NativeTun) addRouteList(luid winipcfg.LUID, destinations []netip.Prefix, gateway4 netip.Addr, gateway6 netip.Addr) error {
row := winipcfg.MibIPforwardRow2{}
row.Init()
row.InterfaceLUID = luid
row.Metric = metric
row.Metric = t.options.RouteMetric
nextHop4 := row.NextHop
nextHop6 := row.NextHop
if gateway4.IsValid() {
Expand Down