linuxfw.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright (c) Tailscale Inc & contributors
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build linux
  4. // Package linuxfw returns the kind of firewall being used by the kernel.
  5. package linuxfw
  6. import (
  7. "errors"
  8. "fmt"
  9. "os"
  10. "strconv"
  11. "strings"
  12. "github.com/tailscale/netlink"
  13. "tailscale.com/feature"
  14. "tailscale.com/tsconst"
  15. "tailscale.com/types/logger"
  16. )
  17. // MatchDecision is the decision made by the firewall for a packet matched by a rule.
  18. // It is used to decide whether to accept or masquerade a packet in addMatchSubnetRouteMarkRule.
  19. type MatchDecision int
  20. const (
  21. Accept MatchDecision = iota
  22. Masq
  23. )
  24. type FWModeNotSupportedError struct {
  25. Mode FirewallMode
  26. Err error
  27. }
  28. func (e FWModeNotSupportedError) Error() string {
  29. return fmt.Sprintf("firewall mode %q not supported: %v", e.Mode, e.Err)
  30. }
  31. func (e FWModeNotSupportedError) Is(target error) bool {
  32. _, ok := target.(FWModeNotSupportedError)
  33. return ok
  34. }
  35. func (e FWModeNotSupportedError) Unwrap() error {
  36. return e.Err
  37. }
  38. type FirewallMode string
  39. const (
  40. FirewallModeIPTables FirewallMode = "iptables"
  41. FirewallModeNfTables FirewallMode = "nftables"
  42. )
  43. // The following bits are added to packet marks for Tailscale use.
  44. //
  45. // We tried to pick bits sufficiently out of the way that it's
  46. // unlikely to collide with existing uses. We have 4 bytes of mark
  47. // bits to play with. We leave the lower byte alone on the assumption
  48. // that sysadmins would use those. Kubernetes uses a few bits in the
  49. // second byte, so we steer clear of that too.
  50. //
  51. // Empirically, most of the documentation on packet marks on the
  52. // internet gives the impression that the marks are 16 bits
  53. // wide. Based on this, we theorize that the upper two bytes are
  54. // relatively unused in the wild, and so we consume bits 16:23 (the
  55. // third byte).
  56. //
  57. // The constants are in the iptables/iproute2 string format for
  58. // matching and setting the bits, so they can be directly embedded in
  59. // commands.
  60. const (
  61. fwmarkMask = tsconst.LinuxFwmarkMask
  62. fwmarkMaskNum = tsconst.LinuxFwmarkMaskNum
  63. subnetRouteMark = tsconst.LinuxSubnetRouteMark
  64. subnetRouteMarkNum = tsconst.LinuxSubnetRouteMarkNum
  65. bypassMark = tsconst.LinuxBypassMark
  66. bypassMarkNum = tsconst.LinuxBypassMarkNum
  67. )
  68. // getTailscaleFwmarkMaskNeg returns the negation of TailscaleFwmarkMask in bytes.
  69. func getTailscaleFwmarkMaskNeg() []byte {
  70. return []byte{0xff, 0x00, 0xff, 0xff}
  71. }
  72. // getTailscaleFwmarkMask returns the TailscaleFwmarkMask in bytes.
  73. func getTailscaleFwmarkMask() []byte {
  74. return []byte{0x00, 0xff, 0x00, 0x00}
  75. }
  76. // getTailscaleSubnetRouteMark returns the TailscaleSubnetRouteMark in bytes.
  77. func getTailscaleSubnetRouteMark() []byte {
  78. return []byte{0x00, 0x04, 0x00, 0x00}
  79. }
  80. // checkIPv6ForTest can be set in tests.
  81. var checkIPv6ForTest func(logger.Logf) error
  82. // checkIPv6 checks whether the system appears to have a working IPv6
  83. // network stack. It returns an error explaining what looks wrong or
  84. // missing. It does not check that IPv6 is currently functional or
  85. // that there's a global address, just that the system would support
  86. // IPv6 if it were on an IPv6 network.
  87. func CheckIPv6(logf logger.Logf) error {
  88. if f := checkIPv6ForTest; f != nil {
  89. return f(logf)
  90. }
  91. _, err := os.Stat("/proc/sys/net/ipv6")
  92. if os.IsNotExist(err) {
  93. return err
  94. }
  95. bs, err := os.ReadFile("/proc/sys/net/ipv6/conf/all/disable_ipv6")
  96. if err != nil {
  97. // Be conservative if we can't find the IPv6 configuration knob.
  98. return err
  99. }
  100. disabled, err := strconv.ParseBool(strings.TrimSpace(string(bs)))
  101. if err != nil {
  102. return errors.New("disable_ipv6 has invalid bool")
  103. }
  104. if disabled {
  105. return errors.New("disable_ipv6 is set")
  106. }
  107. // Older kernels don't support IPv6 policy routing. Some kernels
  108. // support policy routing but don't have this knob, so absence of
  109. // the knob is not fatal.
  110. bs, err = os.ReadFile("/proc/sys/net/ipv6/conf/all/disable_policy")
  111. if err == nil {
  112. disabled, err = strconv.ParseBool(strings.TrimSpace(string(bs)))
  113. if err != nil {
  114. return errors.New("disable_policy has invalid bool")
  115. }
  116. if disabled {
  117. return errors.New("disable_policy is set")
  118. }
  119. }
  120. if err := CheckIPRuleSupportsV6(logf); err != nil {
  121. return fmt.Errorf("kernel doesn't support IPv6 policy routing: %w", err)
  122. }
  123. return nil
  124. }
  125. func CheckIPRuleSupportsV6(logf logger.Logf) error {
  126. // First try just a read-only operation to ideally avoid
  127. // having to modify any state.
  128. if rules, err := netlink.RuleList(netlink.FAMILY_V6); err != nil {
  129. return fmt.Errorf("querying IPv6 policy routing rules: %w", err)
  130. } else {
  131. if len(rules) > 0 {
  132. logf("[v1] kernel supports IPv6 policy routing (found %d rules)", len(rules))
  133. return nil
  134. }
  135. }
  136. // Try to actually create & delete one as a test.
  137. rule := netlink.NewRule()
  138. rule.Priority = 1234
  139. rule.Mark = bypassMarkNum
  140. rule.Table = 52
  141. rule.Family = netlink.FAMILY_V6
  142. // First delete the rule unconditionally, and don't check for
  143. // errors. This is just cleaning up anything that might be already
  144. // there.
  145. netlink.RuleDel(rule)
  146. // And clean up on exit.
  147. defer netlink.RuleDel(rule)
  148. return netlink.RuleAdd(rule)
  149. }
  150. var hookIPTablesCleanup feature.Hook[func(logger.Logf)]
  151. // IPTablesCleanUp removes all Tailscale added iptables rules.
  152. // Any errors that occur are logged to the provided logf.
  153. func IPTablesCleanUp(logf logger.Logf) {
  154. if f, ok := hookIPTablesCleanup.GetOk(); ok {
  155. f(logf)
  156. }
  157. }