2021-03-01 04:52:50 +00:00
|
|
|
// Copyright (c) 2021 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 monitor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-03-16 19:06:53 +00:00
|
|
|
"strings"
|
2021-03-02 05:34:41 +00:00
|
|
|
"sync"
|
2021-03-01 04:52:50 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/route"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/types/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
const debugRouteMessages = false
|
|
|
|
|
|
|
|
// 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 }
|
|
|
|
|
2021-03-03 05:45:40 +00:00
|
|
|
func newOSMon(logf logger.Logf, _ *Mon) (osMon, error) {
|
2021-03-01 04:52:50 +00:00
|
|
|
fd, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &darwinRouteMon{
|
|
|
|
logf: logf,
|
2021-03-02 05:34:41 +00:00
|
|
|
fd: fd,
|
2021-03-01 04:52:50 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type darwinRouteMon struct {
|
2021-03-02 05:34:41 +00:00
|
|
|
logf logger.Logf
|
|
|
|
fd int // AF_ROUTE socket
|
|
|
|
buf [2 << 10]byte
|
|
|
|
closeOnce sync.Once
|
2021-03-01 04:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *darwinRouteMon) Close() error {
|
2021-03-02 05:34:41 +00:00
|
|
|
var err error
|
|
|
|
m.closeOnce.Do(func() {
|
|
|
|
err = unix.Close(m.fd)
|
|
|
|
})
|
|
|
|
return err
|
2021-03-01 04:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *darwinRouteMon) Receive() (message, error) {
|
2021-03-02 18:29:49 +00:00
|
|
|
for {
|
|
|
|
n, err := unix.Read(m.fd, m.buf[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
msgs, err := route.ParseRIB(route.RIBTypeRoute, m.buf[:n])
|
|
|
|
if err != nil {
|
|
|
|
if debugRouteMessages {
|
|
|
|
m.logf("read %d bytes (% 02x), failed to parse RIB: %v", n, m.buf[:n], err)
|
|
|
|
}
|
|
|
|
return unspecifiedMessage{}, nil
|
|
|
|
}
|
|
|
|
if len(msgs) == 0 {
|
|
|
|
if debugRouteMessages {
|
|
|
|
m.logf("read %d bytes with no messages (% 02x)", n, m.buf[:n])
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nSkip := 0
|
|
|
|
for _, msg := range msgs {
|
|
|
|
if m.skipMessage(msg) {
|
|
|
|
nSkip++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if debugRouteMessages {
|
|
|
|
m.logf("read %d bytes, %d messages (%d skipped)", n, len(msgs), nSkip)
|
2021-03-16 19:06:53 +00:00
|
|
|
if nSkip < len(msgs) {
|
|
|
|
m.logMessages(msgs)
|
|
|
|
}
|
2021-03-02 18:29:49 +00:00
|
|
|
}
|
|
|
|
if nSkip == len(msgs) {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-02 16:15:57 +00:00
|
|
|
return unspecifiedMessage{}, nil
|
2021-03-01 04:52:50 +00:00
|
|
|
}
|
2021-03-02 18:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *darwinRouteMon) skipMessage(msg route.Message) bool {
|
2021-03-16 19:06:53 +00:00
|
|
|
switch msg := msg.(type) {
|
2021-03-02 18:29:49 +00:00
|
|
|
case *route.InterfaceMulticastAddrMessage:
|
|
|
|
return true
|
2021-03-16 19:06:53 +00:00
|
|
|
case *route.InterfaceAddrMessage:
|
|
|
|
return m.skipInterfaceAddrMessage(msg)
|
|
|
|
case *route.RouteMessage:
|
|
|
|
return m.skipRouteMessage(msg)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// addrType returns addrs[rtaxType], if that (the route address type) exists,
|
|
|
|
// else it returns nil.
|
|
|
|
//
|
|
|
|
// The RTAX_* constants at https://github.com/apple/darwin-xnu/blob/main/bsd/net/route.h
|
|
|
|
// for what each address index represents.
|
|
|
|
func addrType(addrs []route.Addr, rtaxType int) route.Addr {
|
|
|
|
if len(addrs) > rtaxType {
|
|
|
|
return addrs[rtaxType]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *darwinRouteMon) skipInterfaceAddrMessage(msg *route.InterfaceAddrMessage) bool {
|
|
|
|
if la, ok := addrType(msg.Addrs, unix.RTAX_IFP).(*route.LinkAddr); ok {
|
|
|
|
baseName := strings.TrimRight(la.Name, "0123456789")
|
|
|
|
switch baseName {
|
2021-09-28 04:12:29 +01:00
|
|
|
case "llw", "awdl", "pdp_ip", "ipsec":
|
2021-03-16 19:06:53 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *darwinRouteMon) skipRouteMessage(msg *route.RouteMessage) bool {
|
|
|
|
if ip := ipOfAddr(addrType(msg.Addrs, unix.RTAX_DST)); ip.IsLinkLocalUnicast() {
|
|
|
|
// Skip those like:
|
|
|
|
// dst = fe80::b476:66ff:fe30:c8f6%15
|
|
|
|
return true
|
2021-03-01 04:52:50 +00:00
|
|
|
}
|
2021-03-02 18:29:49 +00:00
|
|
|
return false
|
2021-03-01 04:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *darwinRouteMon) logMessages(msgs []route.Message) {
|
|
|
|
for i, msg := range msgs {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
default:
|
|
|
|
m.logf(" [%d] %T", i, msg)
|
2021-03-16 19:06:53 +00:00
|
|
|
case *route.InterfaceAddrMessage:
|
|
|
|
m.logf(" [%d] InterfaceAddrMessage: ver=%d, type=%v, flags=0x%x, idx=%v",
|
|
|
|
i, msg.Version, msg.Type, msg.Flags, msg.Index)
|
|
|
|
m.logAddrs(msg.Addrs)
|
2021-03-01 04:52:50 +00:00
|
|
|
case *route.InterfaceMulticastAddrMessage:
|
|
|
|
m.logf(" [%d] InterfaceMulticastAddrMessage: ver=%d, type=%v, flags=0x%x, idx=%v",
|
|
|
|
i, msg.Version, msg.Type, msg.Flags, msg.Index)
|
|
|
|
m.logAddrs(msg.Addrs)
|
|
|
|
case *route.RouteMessage:
|
2021-03-16 19:06:53 +00:00
|
|
|
m.logf(" [%d] RouteMessage: ver=%d, type=%v, flags=0x%x, idx=%v, id=%v, seq=%v, err=%v",
|
2021-03-01 04:52:50 +00:00
|
|
|
i, msg.Version, msg.Type, msg.Flags, msg.Index, msg.ID, msg.Seq, msg.Err)
|
|
|
|
m.logAddrs(msg.Addrs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *darwinRouteMon) logAddrs(addrs []route.Addr) {
|
|
|
|
for i, a := range addrs {
|
|
|
|
if a == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m.logf(" %v = %v", rtaxName(i), fmtAddr(a))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:06:53 +00:00
|
|
|
// ipOfAddr returns the route.Addr (possibly nil) as a netaddr.IP
|
|
|
|
// (possibly zero).
|
|
|
|
func ipOfAddr(a route.Addr) netaddr.IP {
|
2021-03-01 04:52:50 +00:00
|
|
|
switch a := a.(type) {
|
|
|
|
case *route.Inet4Addr:
|
|
|
|
return netaddr.IPv4(a.IP[0], a.IP[1], a.IP[2], a.IP[3])
|
|
|
|
case *route.Inet6Addr:
|
|
|
|
ip := netaddr.IPv6Raw(a.IP)
|
|
|
|
if a.ZoneID != 0 {
|
|
|
|
ip = ip.WithZone(fmt.Sprint(a.ZoneID)) // TODO: look up net.InterfaceByIndex? but it might be changing?
|
|
|
|
}
|
|
|
|
return ip
|
2021-03-16 19:06:53 +00:00
|
|
|
}
|
|
|
|
return netaddr.IP{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fmtAddr(a route.Addr) interface{} {
|
|
|
|
if a == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if ip := ipOfAddr(a); !ip.IsZero() {
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
switch a := a.(type) {
|
2021-03-01 04:52:50 +00:00
|
|
|
case *route.LinkAddr:
|
|
|
|
return fmt.Sprintf("[LinkAddr idx=%v name=%q addr=%x]", a.Index, a.Name, a.Addr)
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%T: %+v", a, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:06:53 +00:00
|
|
|
// See https://github.com/apple/darwin-xnu/blob/main/bsd/net/route.h
|
2021-03-01 04:52:50 +00:00
|
|
|
func rtaxName(i int) string {
|
|
|
|
switch i {
|
|
|
|
case unix.RTAX_DST:
|
|
|
|
return "dst"
|
|
|
|
case unix.RTAX_GATEWAY:
|
|
|
|
return "gateway"
|
|
|
|
case unix.RTAX_NETMASK:
|
|
|
|
return "netmask"
|
|
|
|
case unix.RTAX_GENMASK:
|
|
|
|
return "genmask"
|
2021-03-16 19:06:53 +00:00
|
|
|
case unix.RTAX_IFP: // "interface name sockaddr present"
|
2021-03-01 04:52:50 +00:00
|
|
|
return "IFP"
|
2021-03-16 19:06:53 +00:00
|
|
|
case unix.RTAX_IFA: // "interface addr sockaddr present"
|
2021-03-01 04:52:50 +00:00
|
|
|
return "IFA"
|
|
|
|
case unix.RTAX_AUTHOR:
|
|
|
|
return "author"
|
|
|
|
case unix.RTAX_BRD:
|
|
|
|
return "BRD"
|
|
|
|
}
|
|
|
|
return fmt.Sprint(i)
|
|
|
|
}
|