2020-02-12 20:53:55 +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.
|
|
|
|
|
2020-04-24 14:23:32 +01:00
|
|
|
// +build !android
|
|
|
|
|
2020-02-12 20:53:55 +00:00
|
|
|
package monitor
|
|
|
|
|
|
|
|
import (
|
2020-07-07 00:36:57 +01:00
|
|
|
"net"
|
2020-02-17 19:28:07 +00:00
|
|
|
"time"
|
2020-02-12 20:53:55 +00:00
|
|
|
|
2020-07-07 00:36:57 +01:00
|
|
|
"github.com/jsimonetti/rtnetlink"
|
2020-02-12 20:53:55 +00:00
|
|
|
"github.com/mdlayher/netlink"
|
|
|
|
"golang.org/x/sys/unix"
|
2020-07-07 00:36:57 +01:00
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/net/tsaddr"
|
|
|
|
"tailscale.com/types/logger"
|
2020-02-12 20:53:55 +00:00
|
|
|
)
|
|
|
|
|
2020-07-15 00:28:02 +01:00
|
|
|
// 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 }
|
|
|
|
|
2020-02-12 20:53:55 +00:00
|
|
|
// 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 {
|
2020-07-07 00:36:57 +01:00
|
|
|
logf logger.Logf
|
|
|
|
conn *netlink.Conn
|
|
|
|
buffered []netlink.Message
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 00:26:11 +01:00
|
|
|
func newOSMon(logf logger.Logf, m *Mon) (osMon, error) {
|
2020-02-12 20:53:55 +00:00
|
|
|
conn, err := netlink.Dial(unix.NETLINK_ROUTE, &netlink.Config{
|
2021-02-23 03:38:07 +00:00
|
|
|
// 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.
|
2021-07-18 22:50:47 +01:00
|
|
|
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
|
2020-02-12 20:53:55 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-04-01 00:26:11 +01:00
|
|
|
// 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)
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
2020-07-07 00:36:57 +01:00
|
|
|
return &nlConn{logf: logf, conn: conn}, nil
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 17:54:22 +00:00
|
|
|
func (c *nlConn) Close() error { return c.conn.Close() }
|
2020-02-12 20:53:55 +00:00
|
|
|
|
2020-02-17 17:00:38 +00:00
|
|
|
func (c *nlConn) Receive() (message, error) {
|
2020-07-07 00:36:57 +01:00
|
|
|
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)
|
2020-07-07 19:07:12 +01:00
|
|
|
return ignoreMessage{}, nil
|
2020-07-07 00:36:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2020-07-07 19:07:12 +01:00
|
|
|
case unix.RTM_NEWADDR, unix.RTM_DELADDR:
|
2020-07-07 00:36:57 +01:00
|
|
|
var rmsg rtnetlink.AddressMessage
|
|
|
|
if err := rmsg.UnmarshalBinary(msg.Data); err != nil {
|
2020-08-06 04:44:05 +01:00
|
|
|
c.logf("failed to parse type %v: %v", msg.Header.Type, err)
|
2020-07-07 19:07:12 +01:00
|
|
|
return unspecifiedMessage{}, nil
|
2020-07-07 00:36:57 +01:00
|
|
|
}
|
|
|
|
return &newAddrMessage{
|
2020-07-07 19:07:12 +01:00
|
|
|
Label: rmsg.Attributes.Label,
|
|
|
|
Addr: netaddrIP(rmsg.Attributes.Local),
|
|
|
|
Delete: msg.Header.Type == unix.RTM_DELADDR,
|
2020-07-07 00:36:57 +01:00
|
|
|
}, nil
|
2020-08-12 21:12:56 +01:00
|
|
|
case unix.RTM_NEWROUTE, unix.RTM_DELROUTE:
|
|
|
|
typeStr := "RTM_NEWROUTE"
|
|
|
|
if msg.Header.Type == unix.RTM_DELROUTE {
|
|
|
|
typeStr = "RTM_DELROUTE"
|
|
|
|
}
|
2020-07-07 00:36:57 +01:00
|
|
|
var rmsg rtnetlink.RouteMessage
|
|
|
|
if err := rmsg.UnmarshalBinary(msg.Data); err != nil {
|
2020-08-12 21:12:56 +01:00
|
|
|
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)
|
|
|
|
|
2021-04-12 18:27:10 +01:00
|
|
|
if rmsg.Table == tsTable && dst.IsSingleIP() {
|
2020-08-12 21:12:56 +01:00
|
|
|
// Don't log. Spammy and normal to see a bunch of these on start-up,
|
|
|
|
// which we make ourselves.
|
|
|
|
} 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)
|
2020-07-07 19:07:12 +01:00
|
|
|
return unspecifiedMessage{}, nil
|
2020-07-07 00:36:57 +01:00
|
|
|
}
|
|
|
|
return &newRouteMessage{
|
|
|
|
Table: rmsg.Table,
|
2020-08-12 21:12:56 +01:00
|
|
|
Src: src,
|
|
|
|
Dst: dst,
|
|
|
|
Gateway: gw,
|
2020-07-07 00:36:57 +01:00
|
|
|
}, nil
|
2021-07-19 22:30:15 +01:00
|
|
|
case unix.RTM_NEWRULE:
|
|
|
|
// Probably ourselves adding it.
|
|
|
|
return ignoreMessage{}, nil
|
2021-07-18 22:50:47 +01:00
|
|
|
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:[]}}
|
|
|
|
}
|
|
|
|
return ipRuleDeletedMessage{}, nil
|
2020-07-07 00:36:57 +01:00
|
|
|
default:
|
2020-08-06 04:44:05 +01:00
|
|
|
c.logf("unhandled netlink msg type %+v, %q", msg.Header, msg.Data)
|
2020-07-07 18:44:54 +01:00
|
|
|
return unspecifiedMessage{}, nil
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
2020-07-07 00:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func netaddrIP(std net.IP) netaddr.IP {
|
|
|
|
ip, _ := netaddr.FromStdIP(std)
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
|
2020-08-12 21:12:56 +01:00
|
|
|
func netaddrIPPrefix(std net.IP, bits uint8) netaddr.IPPrefix {
|
|
|
|
ip, _ := netaddr.FromStdIP(std)
|
2021-05-15 02:07:28 +01:00
|
|
|
return netaddr.IPPrefixFrom(ip, bits)
|
2020-08-12 21:12:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func condNetAddrPrefix(ipp netaddr.IPPrefix) string {
|
2021-05-15 02:07:28 +01:00
|
|
|
if ipp.IP().IsZero() {
|
2020-08-12 21:12:56 +01:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ipp.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func condNetAddrIP(ip netaddr.IP) string {
|
|
|
|
if ip.IsZero() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ip.String()
|
|
|
|
}
|
|
|
|
|
2020-07-07 00:36:57 +01:00
|
|
|
// newRouteMessage is a message for a new route being added.
|
|
|
|
type newRouteMessage struct {
|
2020-08-12 21:12:56 +01:00
|
|
|
Src, Dst netaddr.IPPrefix
|
|
|
|
Gateway netaddr.IP
|
|
|
|
Table uint8
|
2020-07-07 00:36:57 +01:00
|
|
|
}
|
|
|
|
|
2020-08-12 21:12:56 +01:00
|
|
|
const tsTable = 52
|
|
|
|
|
2020-07-07 00:36:57 +01:00
|
|
|
func (m *newRouteMessage) ignore() bool {
|
2021-05-15 02:07:28 +01:00
|
|
|
return m.Table == tsTable || tsaddr.IsTailscaleIP(m.Dst.IP())
|
2020-07-07 00:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// newAddrMessage is a message for a new address being added.
|
|
|
|
type newAddrMessage struct {
|
2020-07-07 19:07:12 +01:00
|
|
|
Delete bool
|
|
|
|
Addr netaddr.IP
|
|
|
|
Label string // netlink Label attribute (e.g. "tailscale0")
|
2020-07-07 00:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *newAddrMessage) ignore() bool {
|
|
|
|
return tsaddr.IsTailscaleIP(m.Addr)
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
2020-07-07 19:07:12 +01:00
|
|
|
|
|
|
|
type ignoreMessage struct{}
|
|
|
|
|
|
|
|
func (ignoreMessage) ignore() bool { return true }
|
2021-07-18 22:50:47 +01:00
|
|
|
|
|
|
|
type ipRuleDeletedMessage struct{}
|
|
|
|
|
|
|
|
func (ipRuleDeletedMessage) ignore() bool { return false }
|