monitor_linux.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build !android
  5. package monitor
  6. import (
  7. "fmt"
  8. "net"
  9. "time"
  10. "github.com/jsimonetti/rtnetlink"
  11. "github.com/mdlayher/netlink"
  12. "golang.org/x/sys/unix"
  13. "inet.af/netaddr"
  14. "tailscale.com/net/tsaddr"
  15. "tailscale.com/types/logger"
  16. )
  17. // unspecifiedMessage is a minimal message implementation that should not
  18. // be ignored. In general, OS-specific implementations should use better
  19. // types and avoid this if they can.
  20. type unspecifiedMessage struct{}
  21. func (unspecifiedMessage) ignore() bool { return false }
  22. // nlConn wraps a *netlink.Conn and returns a monitor.Message
  23. // instead of a netlink.Message. Currently, messages are discarded,
  24. // but down the line, when messages trigger different logic depending
  25. // on the type of event, this provides the capability of handling
  26. // each architecture-specific message in a generic fashion.
  27. type nlConn struct {
  28. logf logger.Logf
  29. conn *netlink.Conn
  30. buffered []netlink.Message
  31. }
  32. func newOSMon(logf logger.Logf) (osMon, error) {
  33. conn, err := netlink.Dial(unix.NETLINK_ROUTE, &netlink.Config{
  34. // Routes get us most of the events of interest, but we need
  35. // address as well to cover things like DHCP deciding to give
  36. // us a new address upon renewal - routing wouldn't change,
  37. // but all reachability would.
  38. Groups: unix.RTMGRP_IPV4_IFADDR | unix.RTMGRP_IPV6_IFADDR | unix.RTMGRP_IPV4_ROUTE | unix.RTMGRP_IPV6_ROUTE,
  39. })
  40. if err != nil {
  41. return nil, fmt.Errorf("dialing netlink socket: %v", err)
  42. }
  43. return &nlConn{logf: logf, conn: conn}, nil
  44. }
  45. func (c *nlConn) Close() error { return c.conn.Close() }
  46. func (c *nlConn) Receive() (message, error) {
  47. if len(c.buffered) == 0 {
  48. var err error
  49. c.buffered, err = c.conn.Receive()
  50. if err != nil {
  51. return nil, err
  52. }
  53. if len(c.buffered) == 0 {
  54. // Unexpected. Not seen in wild, but sleep defensively.
  55. time.Sleep(time.Second)
  56. return ignoreMessage{}, nil
  57. }
  58. }
  59. msg := c.buffered[0]
  60. c.buffered = c.buffered[1:]
  61. // See https://github.com/torvalds/linux/blob/master/include/uapi/linux/rtnetlink.h
  62. // And https://man7.org/linux/man-pages/man7/rtnetlink.7.html
  63. switch msg.Header.Type {
  64. case unix.RTM_NEWADDR, unix.RTM_DELADDR:
  65. var rmsg rtnetlink.AddressMessage
  66. if err := rmsg.UnmarshalBinary(msg.Data); err != nil {
  67. c.logf("failed to parse type %v: %v", msg.Header.Type, err)
  68. return unspecifiedMessage{}, nil
  69. }
  70. return &newAddrMessage{
  71. Label: rmsg.Attributes.Label,
  72. Addr: netaddrIP(rmsg.Attributes.Local),
  73. Delete: msg.Header.Type == unix.RTM_DELADDR,
  74. }, nil
  75. case unix.RTM_NEWROUTE, unix.RTM_DELROUTE:
  76. typeStr := "RTM_NEWROUTE"
  77. if msg.Header.Type == unix.RTM_DELROUTE {
  78. typeStr = "RTM_DELROUTE"
  79. }
  80. var rmsg rtnetlink.RouteMessage
  81. if err := rmsg.UnmarshalBinary(msg.Data); err != nil {
  82. c.logf("%s: failed to parse: %v", typeStr, err)
  83. return unspecifiedMessage{}, nil
  84. }
  85. src := netaddrIPPrefix(rmsg.Attributes.Src, rmsg.SrcLength)
  86. dst := netaddrIPPrefix(rmsg.Attributes.Dst, rmsg.DstLength)
  87. gw := netaddrIP(rmsg.Attributes.Gateway)
  88. if msg.Header.Type == unix.RTM_NEWROUTE && rmsg.Table == tsTable && rmsg.DstLength == 32 {
  89. // Don't log. Spammy and normal to see a bunch of these on start-up,
  90. // which we make ourselves.
  91. } else {
  92. c.logf("%s: src=%v, dst=%v, gw=%v, outif=%v, table=%v", typeStr,
  93. condNetAddrPrefix(src), condNetAddrPrefix(dst), condNetAddrIP(gw),
  94. rmsg.Attributes.OutIface, rmsg.Attributes.Table)
  95. }
  96. if msg.Header.Type == unix.RTM_DELROUTE {
  97. // Just logging it for now.
  98. // (Debugging https://github.com/tailscale/tailscale/issues/643)
  99. return unspecifiedMessage{}, nil
  100. }
  101. return &newRouteMessage{
  102. Table: rmsg.Table,
  103. Src: src,
  104. Dst: dst,
  105. Gateway: gw,
  106. }, nil
  107. default:
  108. c.logf("unhandled netlink msg type %+v, %q", msg.Header, msg.Data)
  109. return unspecifiedMessage{}, nil
  110. }
  111. }
  112. func netaddrIP(std net.IP) netaddr.IP {
  113. ip, _ := netaddr.FromStdIP(std)
  114. return ip
  115. }
  116. func netaddrIPPrefix(std net.IP, bits uint8) netaddr.IPPrefix {
  117. ip, _ := netaddr.FromStdIP(std)
  118. return netaddr.IPPrefix{IP: ip, Bits: bits}
  119. }
  120. func condNetAddrPrefix(ipp netaddr.IPPrefix) string {
  121. if ipp.IP.IsZero() {
  122. return ""
  123. }
  124. return ipp.String()
  125. }
  126. func condNetAddrIP(ip netaddr.IP) string {
  127. if ip.IsZero() {
  128. return ""
  129. }
  130. return ip.String()
  131. }
  132. // newRouteMessage is a message for a new route being added.
  133. type newRouteMessage struct {
  134. Src, Dst netaddr.IPPrefix
  135. Gateway netaddr.IP
  136. Table uint8
  137. }
  138. const tsTable = 52
  139. func (m *newRouteMessage) ignore() bool {
  140. return m.Table == tsTable || tsaddr.IsTailscaleIP(m.Dst.IP)
  141. }
  142. // newAddrMessage is a message for a new address being added.
  143. type newAddrMessage struct {
  144. Delete bool
  145. Addr netaddr.IP
  146. Label string // netlink Label attribute (e.g. "tailscale0")
  147. }
  148. func (m *newAddrMessage) ignore() bool {
  149. return tsaddr.IsTailscaleIP(m.Addr)
  150. }
  151. type ignoreMessage struct{}
  152. func (ignoreMessage) ignore() bool { return true }