2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-03-01 04:52:50 +00:00
|
|
|
|
2023-04-18 21:26:58 +00:00
|
|
|
package netmon
|
2021-03-01 04:52:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
"net/netip"
|
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"
|
2022-07-25 03:08:42 +00:00
|
|
|
"tailscale.com/net/netaddr"
|
2021-03-01 04:52:50 +00:00
|
|
|
"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 }
|
|
|
|
|
2023-04-18 21:26:58 +00:00
|
|
|
func newOSMon(logf logger.Logf, _ *Monitor) (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
|
|
|
|
}
|
2024-11-22 22:26:42 +00:00
|
|
|
msgs, err := func() (msgs []route.Message, err error) {
|
|
|
|
defer func() {
|
|
|
|
if recover() != nil {
|
|
|
|
msgs = nil
|
|
|
|
err = fmt.Errorf("panic parsing route message")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return route.ParseRIB(route.RIBTypeRoute, m.buf[:n])
|
|
|
|
}()
|
2021-03-02 18:29:49 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-20 19:03:50 +00:00
|
|
|
func (m *darwinRouteMon) IsInterestingInterface(iface string) bool {
|
|
|
|
baseName := strings.TrimRight(iface, "0123456789")
|
|
|
|
switch baseName {
|
2022-04-25 19:10:31 +00:00
|
|
|
// TODO(maisem): figure out what this list should actually be.
|
|
|
|
case "llw", "awdl", "ipsec":
|
2022-04-20 19:03:50 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:06:53 +00:00
|
|
|
func (m *darwinRouteMon) skipInterfaceAddrMessage(msg *route.InterfaceAddrMessage) bool {
|
|
|
|
if la, ok := addrType(msg.Addrs, unix.RTAX_IFP).(*route.LinkAddr); ok {
|
2022-04-20 19:03:50 +00:00
|
|
|
if !m.IsInterestingInterface(la.Name) {
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
// ipOfAddr returns the route.Addr (possibly nil) as a netip.Addr
|
2021-03-16 19:06:53 +00:00
|
|
|
// (possibly zero).
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func ipOfAddr(a route.Addr) netip.Addr {
|
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:
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
ip := netip.AddrFrom16(a.IP)
|
2021-03-01 04:52:50 +00:00
|
|
|
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
|
|
|
}
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
return netip.Addr{}
|
2021-03-16 19:06:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 23:27:57 +00:00
|
|
|
func fmtAddr(a route.Addr) any {
|
2021-03-16 19:06:53 +00:00
|
|
|
if a == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
if ip := ipOfAddr(a); ip.IsValid() {
|
2021-03-16 19:06:53 +00:00
|
|
|
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)
|
|
|
|
}
|