From d9a7205be5470f6ea03944c76e2ee2b5a1906041 Mon Sep 17 00:00:00 2001 From: Dmytro Shynkevych Date: Tue, 22 Feb 2022 02:33:23 -0500 Subject: [PATCH] net/tstun: set link speed to SPEED_UNKNOWN Fixes #3933. Signed-off-by: Dmytro Shynkevych --- cmd/tailscaled/depaware.txt | 1 + go.mod | 3 +- go.sum | 5 ++- net/tstun/linkattrs_linux.go | 64 +++++++++++++++++++++++++++++++++ net/tstun/linkattrs_notlinux.go | 14 ++++++++ net/tstun/tun.go | 3 ++ 6 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 net/tstun/linkattrs_linux.go create mode 100644 net/tstun/linkattrs_notlinux.go diff --git a/cmd/tailscaled/depaware.txt b/cmd/tailscaled/depaware.txt index cd2eef9d1..002760d31 100644 --- a/cmd/tailscaled/depaware.txt +++ b/cmd/tailscaled/depaware.txt @@ -83,6 +83,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de github.com/klauspost/compress/internal/snapref from github.com/klauspost/compress/zstd github.com/klauspost/compress/zstd from tailscale.com/smallzstd github.com/klauspost/compress/zstd/internal/xxhash from github.com/klauspost/compress/zstd + L github.com/mdlayher/genetlink from tailscale.com/net/tstun L 💣 github.com/mdlayher/netlink from github.com/jsimonetti/rtnetlink+ L 💣 github.com/mdlayher/netlink/nlenc from github.com/jsimonetti/rtnetlink+ L github.com/mdlayher/sdnotify from tailscale.com/util/systemd diff --git a/go.mod b/go.mod index 3c4fe1284..33a3a4dbc 100644 --- a/go.mod +++ b/go.mod @@ -28,6 +28,7 @@ require ( github.com/jsimonetti/rtnetlink v0.0.0-20211203074127-fd9a11f42291 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/klauspost/compress v1.13.6 + github.com/mdlayher/genetlink v1.2.0 github.com/mdlayher/netlink v1.6.0 github.com/mdlayher/sdnotify v0.0.0-20210228150836-ea3ec207d697 github.com/miekg/dns v1.1.43 @@ -50,7 +51,7 @@ require ( golang.org/x/crypto v0.0.0-20211202192323-5770296d904e golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd golang.org/x/sync v0.0.0-20210220032951-036812b2e83c - golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 + golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 golang.org/x/tools v0.1.8 diff --git a/go.sum b/go.sum index 928eae061..049630618 100644 --- a/go.sum +++ b/go.sum @@ -839,6 +839,8 @@ github.com/mdlayher/ethernet v0.0.0-20190606142754-0394541c37b7/go.mod h1:U6ZQob github.com/mdlayher/ethtool v0.0.0-20210210192532-2b88debcdd43/go.mod h1:+t7E0lkKfbBsebllff1xdTmyJt8lH37niI6kwFk9OTo= github.com/mdlayher/ethtool v0.0.0-20211028163843-288d040e9d60/go.mod h1:aYbhishWc4Ai3I2U4Gaa2n3kHWSwzme6EsG/46HRQbE= github.com/mdlayher/genetlink v1.0.0/go.mod h1:0rJ0h4itni50A86M2kHcgS85ttZazNt7a8H2a2cw0Gc= +github.com/mdlayher/genetlink v1.2.0 h1:4yrIkRV5Wfk1WfpWTcoOlGmsWgQj3OtQN9ZsbrE+XtU= +github.com/mdlayher/genetlink v1.2.0/go.mod h1:ra5LDov2KrUCZJiAtEvXXZBxGMInICMXIwshlJ+qRxQ= github.com/mdlayher/netlink v0.0.0-20190409211403-11939a169225/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA= github.com/mdlayher/netlink v1.0.0/go.mod h1:KxeJAFOFLG6AjpyDkQ/iIhxygIUKD+vcwqcnu43w/+M= github.com/mdlayher/netlink v1.1.0/go.mod h1:H4WCitaheIsdF9yOYu8CFmCgQthAPIWZmcKp9uZHgmY= @@ -1611,8 +1613,9 @@ golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211105183446-c75c47738b0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 h1:XDXtA5hveEEV8JB2l7nhMTp3t3cHp9ZpwcdjqyEWLlo= golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1ii9sTLUeBQwQQfUHtrs= +golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= diff --git a/net/tstun/linkattrs_linux.go b/net/tstun/linkattrs_linux.go new file mode 100644 index 000000000..57aa406c4 --- /dev/null +++ b/net/tstun/linkattrs_linux.go @@ -0,0 +1,64 @@ +// Copyright (c) 2022 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 tstun + +import ( + "github.com/mdlayher/genetlink" + "github.com/mdlayher/netlink" + "golang.org/x/sys/unix" + "golang.zx2c4.com/wireguard/tun" +) + +// setLinkSpeed sets the advertised link speed of the TUN interface. +func setLinkSpeed(iface tun.Device, mbps int) error { + name, err := iface.Name() + if err != nil { + return err + } + + conn, err := genetlink.Dial(&netlink.Config{Strict: true}) + if err != nil { + return err + } + + defer conn.Close() + + f, err := conn.GetFamily(unix.ETHTOOL_GENL_NAME) + if err != nil { + return err + } + + ae := netlink.NewAttributeEncoder() + ae.Nested(unix.ETHTOOL_A_LINKMODES_HEADER, func(nae *netlink.AttributeEncoder) error { + nae.String(unix.ETHTOOL_A_HEADER_DEV_NAME, name) + return nil + }) + ae.Uint32(unix.ETHTOOL_A_LINKMODES_SPEED, uint32(mbps)) + + b, err := ae.Encode() + if err != nil { + return err + } + + _, err = conn.Execute( + genetlink.Message{ + Header: genetlink.Header{ + Command: unix.ETHTOOL_MSG_LINKMODES_SET, + Version: unix.ETHTOOL_GENL_VERSION, + }, + Data: b, + }, + f.ID, + netlink.Request|netlink.Acknowledge, + ) + return err +} + +// setLinkAttrs sets up link attributes that can be queried by external tools. +// Its failure is non-fatal to interface bringup. +func setLinkAttrs(iface tun.Device) error { + // By default the link speed is 10Mbps, which is easily exceeded and causes monitoring tools to complain (#3933). + return setLinkSpeed(iface, unix.SPEED_UNKNOWN) +} diff --git a/net/tstun/linkattrs_notlinux.go b/net/tstun/linkattrs_notlinux.go new file mode 100644 index 000000000..5acf9e019 --- /dev/null +++ b/net/tstun/linkattrs_notlinux.go @@ -0,0 +1,14 @@ +// Copyright (c) 2022 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. + +//go:build !linux +// +build !linux + +package tstun + +import "golang.zx2c4.com/wireguard/tun" + +func setLinkAttrs(iface tun.Device) error { + return nil +} diff --git a/net/tstun/tun.go b/net/tstun/tun.go index 09386bd51..286c5f754 100644 --- a/net/tstun/tun.go +++ b/net/tstun/tun.go @@ -69,6 +69,9 @@ func New(logf logger.Logf, tunName string) (tun.Device, string, error) { dev.Close() return nil, "", err } + if err := setLinkAttrs(dev); err != nil { + logf("setting link attributes: %v", err) + } name, err := interfaceName(dev) if err != nil { dev.Close()