linuxfw.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package linuxfw returns the kind of firewall being used by the kernel.
  4. //go:build linux
  5. package linuxfw
  6. import (
  7. "bytes"
  8. "errors"
  9. "fmt"
  10. "os"
  11. "os/exec"
  12. "strconv"
  13. "strings"
  14. "github.com/tailscale/netlink"
  15. "tailscale.com/types/logger"
  16. )
  17. // The following bits are added to packet marks for Tailscale use.
  18. //
  19. // We tried to pick bits sufficiently out of the way that it's
  20. // unlikely to collide with existing uses. We have 4 bytes of mark
  21. // bits to play with. We leave the lower byte alone on the assumption
  22. // that sysadmins would use those. Kubernetes uses a few bits in the
  23. // second byte, so we steer clear of that too.
  24. //
  25. // Empirically, most of the documentation on packet marks on the
  26. // internet gives the impression that the marks are 16 bits
  27. // wide. Based on this, we theorize that the upper two bytes are
  28. // relatively unused in the wild, and so we consume bits 16:23 (the
  29. // third byte).
  30. //
  31. // The constants are in the iptables/iproute2 string format for
  32. // matching and setting the bits, so they can be directly embedded in
  33. // commands.
  34. const (
  35. // The mask for reading/writing the 'firewall mask' bits on a packet.
  36. // See the comment on the const block on why we only use the third byte.
  37. //
  38. // We claim bits 16:23 entirely. For now we only use the lower four
  39. // bits, leaving the higher 4 bits for future use.
  40. TailscaleFwmarkMask = "0xff0000"
  41. TailscaleFwmarkMaskNeg = "0xff00ffff"
  42. TailscaleFwmarkMaskNum = 0xff0000
  43. // Packet is from Tailscale and to a subnet route destination, so
  44. // is allowed to be routed through this machine.
  45. TailscaleSubnetRouteMark = "0x40000"
  46. TailscaleSubnetRouteMarkNum = 0x40000
  47. // This one is same value but padded to even number of digit, so
  48. // hex decoding can work correctly.
  49. TailscaleSubnetRouteMarkHexStr = "0x040000"
  50. // Packet was originated by tailscaled itself, and must not be
  51. // routed over the Tailscale network.
  52. TailscaleBypassMark = "0x80000"
  53. TailscaleBypassMarkNum = 0x80000
  54. )
  55. // errCode extracts and returns the process exit code from err, or
  56. // zero if err is nil.
  57. func errCode(err error) int {
  58. if err == nil {
  59. return 0
  60. }
  61. var e *exec.ExitError
  62. if ok := errors.As(err, &e); ok {
  63. return e.ExitCode()
  64. }
  65. s := err.Error()
  66. if strings.HasPrefix(s, "exitcode:") {
  67. code, err := strconv.Atoi(s[9:])
  68. if err == nil {
  69. return code
  70. }
  71. }
  72. return -42
  73. }
  74. // checkIPv6 checks whether the system appears to have a working IPv6
  75. // network stack. It returns an error explaining what looks wrong or
  76. // missing. It does not check that IPv6 is currently functional or
  77. // that there's a global address, just that the system would support
  78. // IPv6 if it were on an IPv6 network.
  79. func checkIPv6(logf logger.Logf) error {
  80. _, err := os.Stat("/proc/sys/net/ipv6")
  81. if os.IsNotExist(err) {
  82. return err
  83. }
  84. bs, err := os.ReadFile("/proc/sys/net/ipv6/conf/all/disable_ipv6")
  85. if err != nil {
  86. // Be conservative if we can't find the IPv6 configuration knob.
  87. return err
  88. }
  89. disabled, err := strconv.ParseBool(strings.TrimSpace(string(bs)))
  90. if err != nil {
  91. return errors.New("disable_ipv6 has invalid bool")
  92. }
  93. if disabled {
  94. return errors.New("disable_ipv6 is set")
  95. }
  96. // Older kernels don't support IPv6 policy routing. Some kernels
  97. // support policy routing but don't have this knob, so absence of
  98. // the knob is not fatal.
  99. bs, err = os.ReadFile("/proc/sys/net/ipv6/conf/all/disable_policy")
  100. if err == nil {
  101. disabled, err = strconv.ParseBool(strings.TrimSpace(string(bs)))
  102. if err != nil {
  103. return errors.New("disable_policy has invalid bool")
  104. }
  105. if disabled {
  106. return errors.New("disable_policy is set")
  107. }
  108. }
  109. if err := CheckIPRuleSupportsV6(logf); err != nil {
  110. return fmt.Errorf("kernel doesn't support IPv6 policy routing: %w", err)
  111. }
  112. // Some distros ship ip6tables separately from iptables.
  113. if _, err := exec.LookPath("ip6tables"); err != nil {
  114. return err
  115. }
  116. return nil
  117. }
  118. // checkSupportsV6NAT returns whether the system has a "nat" table in the
  119. // IPv6 netfilter stack.
  120. //
  121. // The nat table was added after the initial release of ipv6
  122. // netfilter, so some older distros ship a kernel that can't NAT IPv6
  123. // traffic.
  124. func checkSupportsV6NAT() bool {
  125. bs, err := os.ReadFile("/proc/net/ip6_tables_names")
  126. if err != nil {
  127. // Can't read the file. Assume SNAT works.
  128. return true
  129. }
  130. if bytes.Contains(bs, []byte("nat\n")) {
  131. return true
  132. }
  133. // In nftables mode, that proc file will be empty. Try another thing:
  134. if exec.Command("modprobe", "ip6table_nat").Run() == nil {
  135. return true
  136. }
  137. return false
  138. }
  139. func CheckIPRuleSupportsV6(logf logger.Logf) error {
  140. // First try just a read-only operation to ideally avoid
  141. // having to modify any state.
  142. if rules, err := netlink.RuleList(netlink.FAMILY_V6); err != nil {
  143. return fmt.Errorf("querying IPv6 policy routing rules: %w", err)
  144. } else {
  145. if len(rules) > 0 {
  146. logf("[v1] kernel supports IPv6 policy routing (found %d rules)", len(rules))
  147. return nil
  148. }
  149. }
  150. // Try to actually create & delete one as a test.
  151. rule := netlink.NewRule()
  152. rule.Priority = 1234
  153. rule.Mark = TailscaleBypassMarkNum
  154. rule.Table = 52
  155. rule.Family = netlink.FAMILY_V6
  156. // First delete the rule unconditionally, and don't check for
  157. // errors. This is just cleaning up anything that might be already
  158. // there.
  159. netlink.RuleDel(rule)
  160. // And clean up on exit.
  161. defer netlink.RuleDel(rule)
  162. return netlink.RuleAdd(rule)
  163. }