mirror of https://github.com/tailscale/tailscale/
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
291 lines
8.4 KiB
Go
291 lines
8.4 KiB
Go
![]()
2 years ago
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||
![]()
5 years ago
|
|
||
![]()
3 years ago
|
//go:build !android
|
||
![]()
4 years ago
|
|
||
![]()
1 year ago
|
package netmon
|
||
![]()
5 years ago
|
|
||
|
import (
|
||
![]()
4 years ago
|
"net"
|
||
![]()
2 years ago
|
"net/netip"
|
||
![]()
5 years ago
|
"time"
|
||
![]()
5 years ago
|
|
||
![]()
4 years ago
|
"github.com/jsimonetti/rtnetlink"
|
||
![]()
5 years ago
|
"github.com/mdlayher/netlink"
|
||
|
"golang.org/x/sys/unix"
|
||
![]()
2 years ago
|
"tailscale.com/envknob"
|
||
![]()
4 years ago
|
"tailscale.com/net/tsaddr"
|
||
|
"tailscale.com/types/logger"
|
||
![]()
5 years ago
|
)
|
||
|
|
||
![]()
2 years ago
|
var debugNetlinkMessages = envknob.RegisterBool("TS_DEBUG_NETLINK")
|
||
![]()
2 years ago
|
|
||
![]()
4 years ago
|
// unspecifiedMessage is a minimal message implementation that should not
|
||
|
// be ignored. In general, OS-specific implementations should use better
|
||
|
// types and avoid this if they can.
|
||
|
type unspecifiedMessage struct{}
|
||
|
|
||
|
func (unspecifiedMessage) ignore() bool { return false }
|
||
|
|
||
![]()
5 years ago
|
// nlConn wraps a *netlink.Conn and returns a monitor.Message
|
||
|
// instead of a netlink.Message. Currently, messages are discarded,
|
||
|
// but down the line, when messages trigger different logic depending
|
||
|
// on the type of event, this provides the capability of handling
|
||
|
// each architecture-specific message in a generic fashion.
|
||
|
type nlConn struct {
|
||
![]()
4 years ago
|
logf logger.Logf
|
||
|
conn *netlink.Conn
|
||
|
buffered []netlink.Message
|
||
![]()
2 years ago
|
|
||
|
// addrCache maps interface indices to a set of addresses, and is
|
||
|
// used to suppress duplicate RTM_NEWADDR messages. It is populated
|
||
|
// by RTM_NEWADDR messages and de-populated by RTM_DELADDR. See
|
||
|
// issue #4282.
|
||
![]()
2 years ago
|
addrCache map[uint32]map[netip.Addr]bool
|
||
![]()
5 years ago
|
}
|
||
|
|
||
![]()
1 year ago
|
func newOSMon(logf logger.Logf, m *Monitor) (osMon, error) {
|
||
![]()
5 years ago
|
conn, err := netlink.Dial(unix.NETLINK_ROUTE, &netlink.Config{
|
||
![]()
4 years ago
|
// Routes get us most of the events of interest, but we need
|
||
|
// address as well to cover things like DHCP deciding to give
|
||
|
// us a new address upon renewal - routing wouldn't change,
|
||
|
// but all reachability would.
|
||
![]()
3 years ago
|
Groups: unix.RTMGRP_IPV4_IFADDR | unix.RTMGRP_IPV6_IFADDR |
|
||
|
unix.RTMGRP_IPV4_ROUTE | unix.RTMGRP_IPV6_ROUTE |
|
||
|
unix.RTMGRP_IPV4_RULE, // no IPV6_RULE in x/sys/unix
|
||
![]()
5 years ago
|
})
|
||
|
if err != nil {
|
||
![]()
3 years ago
|
// Google Cloud Run does not implement NETLINK_ROUTE RTMGRP support
|
||
|
logf("monitor_linux: AF_NETLINK RTMGRP failed, falling back to polling")
|
||
|
return newPollingMon(logf, m)
|
||
![]()
5 years ago
|
}
|
||
![]()
2 years ago
|
return &nlConn{logf: logf, conn: conn, addrCache: make(map[uint32]map[netip.Addr]bool)}, nil
|
||
![]()
5 years ago
|
}
|
||
|
|
||
![]()
2 years ago
|
func (c *nlConn) IsInterestingInterface(iface string) bool { return true }
|
||
|
|
||
![]()
4 years ago
|
func (c *nlConn) Close() error { return c.conn.Close() }
|
||
![]()
5 years ago
|
|
||
![]()
5 years ago
|
func (c *nlConn) Receive() (message, error) {
|
||
![]()
4 years ago
|
if len(c.buffered) == 0 {
|
||
|
var err error
|
||
|
c.buffered, err = c.conn.Receive()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if len(c.buffered) == 0 {
|
||
|
// Unexpected. Not seen in wild, but sleep defensively.
|
||
|
time.Sleep(time.Second)
|
||
![]()
4 years ago
|
return ignoreMessage{}, nil
|
||
![]()
4 years ago
|
}
|
||
|
}
|
||
|
msg := c.buffered[0]
|
||
|
c.buffered = c.buffered[1:]
|
||
|
|
||
|
// See https://github.com/torvalds/linux/blob/master/include/uapi/linux/rtnetlink.h
|
||
|
// And https://man7.org/linux/man-pages/man7/rtnetlink.7.html
|
||
|
switch msg.Header.Type {
|
||
![]()
4 years ago
|
case unix.RTM_NEWADDR, unix.RTM_DELADDR:
|
||
![]()
4 years ago
|
var rmsg rtnetlink.AddressMessage
|
||
|
if err := rmsg.UnmarshalBinary(msg.Data); err != nil {
|
||
![]()
4 years ago
|
c.logf("failed to parse type %v: %v", msg.Header.Type, err)
|
||
![]()
4 years ago
|
return unspecifiedMessage{}, nil
|
||
![]()
4 years ago
|
}
|
||
![]()
2 years ago
|
|
||
![]()
2 years ago
|
nip := netaddrIP(rmsg.Attributes.Address)
|
||
|
|
||
![]()
2 years ago
|
if debugNetlinkMessages() {
|
||
![]()
2 years ago
|
typ := "RTM_NEWADDR"
|
||
|
if msg.Header.Type == unix.RTM_DELADDR {
|
||
|
typ = "RTM_DELADDR"
|
||
|
}
|
||
|
|
||
|
// label attributes are seemingly only populated for ipv4 addresses in the wild.
|
||
|
label := rmsg.Attributes.Label
|
||
|
if label == "" {
|
||
|
itf, err := net.InterfaceByIndex(int(rmsg.Index))
|
||
|
if err == nil {
|
||
|
label = itf.Name
|
||
|
}
|
||
|
}
|
||
|
|
||
|
c.logf("%s: %s(%d) %s / %s", typ, label, rmsg.Index, rmsg.Attributes.Address, rmsg.Attributes.Local)
|
||
|
}
|
||
|
|
||
|
addrs := c.addrCache[rmsg.Index]
|
||
|
|
||
|
// Ignore duplicate RTM_NEWADDR messages using c.addrCache to
|
||
|
// detect them. See nlConn.addrcache and issue #4282.
|
||
|
if msg.Header.Type == unix.RTM_NEWADDR {
|
||
|
if addrs == nil {
|
||
![]()
2 years ago
|
addrs = make(map[netip.Addr]bool)
|
||
![]()
2 years ago
|
c.addrCache[rmsg.Index] = addrs
|
||
|
}
|
||
|
|
||
|
if addrs[nip] {
|
||
![]()
2 years ago
|
if debugNetlinkMessages() {
|
||
![]()
2 years ago
|
c.logf("ignored duplicate RTM_NEWADDR for %s", nip)
|
||
|
}
|
||
|
return ignoreMessage{}, nil
|
||
|
}
|
||
|
|
||
|
addrs[nip] = true
|
||
|
} else { // msg.Header.Type == unix.RTM_DELADDR
|
||
|
if addrs != nil {
|
||
|
delete(addrs, nip)
|
||
|
}
|
||
|
|
||
|
if len(addrs) == 0 {
|
||
|
delete(c.addrCache, rmsg.Index)
|
||
|
}
|
||
|
}
|
||
|
|
||
![]()
2 years ago
|
nam := &newAddrMessage{
|
||
![]()
2 years ago
|
IfIndex: rmsg.Index,
|
||
|
Addr: nip,
|
||
|
Delete: msg.Header.Type == unix.RTM_DELADDR,
|
||
![]()
2 years ago
|
}
|
||
![]()
2 years ago
|
if debugNetlinkMessages() {
|
||
![]()
2 years ago
|
c.logf("%+v", nam)
|
||
|
}
|
||
|
return nam, nil
|
||
![]()
4 years ago
|
case unix.RTM_NEWROUTE, unix.RTM_DELROUTE:
|
||
|
typeStr := "RTM_NEWROUTE"
|
||
|
if msg.Header.Type == unix.RTM_DELROUTE {
|
||
|
typeStr = "RTM_DELROUTE"
|
||
|
}
|
||
![]()
4 years ago
|
var rmsg rtnetlink.RouteMessage
|
||
|
if err := rmsg.UnmarshalBinary(msg.Data); err != nil {
|
||
![]()
4 years ago
|
c.logf("%s: failed to parse: %v", typeStr, err)
|
||
|
return unspecifiedMessage{}, nil
|
||
|
}
|
||
|
src := netaddrIPPrefix(rmsg.Attributes.Src, rmsg.SrcLength)
|
||
|
dst := netaddrIPPrefix(rmsg.Attributes.Dst, rmsg.DstLength)
|
||
|
gw := netaddrIP(rmsg.Attributes.Gateway)
|
||
|
|
||
![]()
3 years ago
|
if msg.Header.Type == unix.RTM_NEWROUTE &&
|
||
|
(rmsg.Attributes.Table == 255 || rmsg.Attributes.Table == 254) &&
|
||
![]()
2 years ago
|
(dst.Addr().IsMulticast() || dst.Addr().IsLinkLocalUnicast()) {
|
||
![]()
2 years ago
|
|
||
![]()
2 years ago
|
if debugNetlinkMessages() {
|
||
![]()
2 years ago
|
c.logf("%s ignored", typeStr)
|
||
|
}
|
||
|
|
||
![]()
3 years ago
|
// Normal Linux route changes on new interface coming up; don't log or react.
|
||
|
return ignoreMessage{}, nil
|
||
|
}
|
||
|
|
||
![]()
3 years ago
|
if rmsg.Table == tsTable && dst.IsSingleIP() {
|
||
![]()
4 years ago
|
// Don't log. Spammy and normal to see a bunch of these on start-up,
|
||
|
// which we make ourselves.
|
||
![]()
2 years ago
|
} else if tsaddr.IsTailscaleIP(dst.Addr()) {
|
||
![]()
3 years ago
|
// Verbose only.
|
||
|
c.logf("%s: [v1] src=%v, dst=%v, gw=%v, outif=%v, table=%v", typeStr,
|
||
|
condNetAddrPrefix(src), condNetAddrPrefix(dst), condNetAddrIP(gw),
|
||
|
rmsg.Attributes.OutIface, rmsg.Attributes.Table)
|
||
![]()
4 years ago
|
} else {
|
||
|
c.logf("%s: src=%v, dst=%v, gw=%v, outif=%v, table=%v", typeStr,
|
||
|
condNetAddrPrefix(src), condNetAddrPrefix(dst), condNetAddrIP(gw),
|
||
|
rmsg.Attributes.OutIface, rmsg.Attributes.Table)
|
||
|
}
|
||
|
if msg.Header.Type == unix.RTM_DELROUTE {
|
||
|
// Just logging it for now.
|
||
|
// (Debugging https://github.com/tailscale/tailscale/issues/643)
|
||
![]()
4 years ago
|
return unspecifiedMessage{}, nil
|
||
![]()
4 years ago
|
}
|
||
![]()
2 years ago
|
|
||
|
nrm := &newRouteMessage{
|
||
![]()
4 years ago
|
Table: rmsg.Table,
|
||
![]()
4 years ago
|
Src: src,
|
||
|
Dst: dst,
|
||
|
Gateway: gw,
|
||
![]()
2 years ago
|
}
|
||
![]()
2 years ago
|
if debugNetlinkMessages() {
|
||
![]()
2 years ago
|
c.logf("%+v", nrm)
|
||
|
}
|
||
|
return nrm, nil
|
||
![]()
3 years ago
|
case unix.RTM_NEWRULE:
|
||
|
// Probably ourselves adding it.
|
||
|
return ignoreMessage{}, nil
|
||
![]()
3 years ago
|
case unix.RTM_DELRULE:
|
||
|
// For https://github.com/tailscale/tailscale/issues/1591 where
|
||
|
// systemd-networkd deletes our rules.
|
||
|
var rmsg rtnetlink.RouteMessage
|
||
|
err := rmsg.UnmarshalBinary(msg.Data)
|
||
|
if err != nil {
|
||
|
c.logf("ip rule deleted; failed to parse netlink message: %v", err)
|
||
|
} else {
|
||
|
c.logf("ip rule deleted: %+v", rmsg)
|
||
|
// On `ip -4 rule del pref 5210 table main`, logs:
|
||
|
// monitor: ip rule deleted: {Family:2 DstLength:0 SrcLength:0 Tos:0 Table:254 Protocol:0 Scope:0 Type:1 Flags:0 Attributes:{Dst:<nil> Src:<nil> Gateway:<nil> OutIface:0 Priority:5210 Table:254 Mark:4294967295 Expires:<nil> Metrics:<nil> Multipath:[]}}
|
||
|
}
|
||
![]()
2 years ago
|
rdm := ipRuleDeletedMessage{
|
||
![]()
3 years ago
|
table: rmsg.Table,
|
||
|
priority: rmsg.Attributes.Priority,
|
||
![]()
2 years ago
|
}
|
||
![]()
2 years ago
|
if debugNetlinkMessages() {
|
||
![]()
2 years ago
|
c.logf("%+v", rdm)
|
||
|
}
|
||
|
return rdm, nil
|
||
![]()
2 years ago
|
case unix.RTM_NEWLINK, unix.RTM_DELLINK:
|
||
|
// This is an unhandled message, but don't print an error.
|
||
|
// See https://github.com/tailscale/tailscale/issues/6806
|
||
|
return unspecifiedMessage{}, nil
|
||
![]()
4 years ago
|
default:
|
||
![]()
4 years ago
|
c.logf("unhandled netlink msg type %+v, %q", msg.Header, msg.Data)
|
||
![]()
4 years ago
|
return unspecifiedMessage{}, nil
|
||
![]()
5 years ago
|
}
|
||
![]()
4 years ago
|
}
|
||
|
|
||
![]()
2 years ago
|
func netaddrIP(std net.IP) netip.Addr {
|
||
![]()
2 years ago
|
ip, _ := netip.AddrFromSlice(std)
|
||
|
return ip.Unmap()
|
||
![]()
4 years ago
|
}
|
||
|
|
||
![]()
2 years ago
|
func netaddrIPPrefix(std net.IP, bits uint8) netip.Prefix {
|
||
![]()
2 years ago
|
ip, _ := netip.AddrFromSlice(std)
|
||
|
return netip.PrefixFrom(ip.Unmap(), int(bits))
|
||
![]()
4 years ago
|
}
|
||
|
|
||
![]()
2 years ago
|
func condNetAddrPrefix(ipp netip.Prefix) string {
|
||
![]()
2 years ago
|
if !ipp.Addr().IsValid() {
|
||
![]()
4 years ago
|
return ""
|
||
|
}
|
||
|
return ipp.String()
|
||
|
}
|
||
|
|
||
![]()
2 years ago
|
func condNetAddrIP(ip netip.Addr) string {
|
||
![]()
2 years ago
|
if !ip.IsValid() {
|
||
![]()
4 years ago
|
return ""
|
||
|
}
|
||
|
return ip.String()
|
||
|
}
|
||
|
|
||
![]()
4 years ago
|
// newRouteMessage is a message for a new route being added.
|
||
|
type newRouteMessage struct {
|
||
![]()
2 years ago
|
Src, Dst netip.Prefix
|
||
|
Gateway netip.Addr
|
||
![]()
4 years ago
|
Table uint8
|
||
![]()
4 years ago
|
}
|
||
|
|
||
![]()
4 years ago
|
const tsTable = 52
|
||
|
|
||
![]()
4 years ago
|
func (m *newRouteMessage) ignore() bool {
|
||
![]()
2 years ago
|
return m.Table == tsTable || tsaddr.IsTailscaleIP(m.Dst.Addr())
|
||
![]()
4 years ago
|
}
|
||
|
|
||
|
// newAddrMessage is a message for a new address being added.
|
||
|
type newAddrMessage struct {
|
||
![]()
2 years ago
|
Delete bool
|
||
![]()
2 years ago
|
Addr netip.Addr
|
||
![]()
2 years ago
|
IfIndex uint32 // interface index
|
||
![]()
4 years ago
|
}
|
||
|
|
||
|
func (m *newAddrMessage) ignore() bool {
|
||
|
return tsaddr.IsTailscaleIP(m.Addr)
|
||
![]()
5 years ago
|
}
|
||
![]()
4 years ago
|
|
||
|
type ignoreMessage struct{}
|
||
|
|
||
|
func (ignoreMessage) ignore() bool { return true }
|