tun.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package overlay
  2. import (
  3. "fmt"
  4. "log/slog"
  5. "net"
  6. "net/netip"
  7. "github.com/slackhq/nebula/config"
  8. "github.com/slackhq/nebula/header"
  9. "github.com/slackhq/nebula/udp"
  10. "github.com/slackhq/nebula/util"
  11. )
  12. const DefaultMTU = 1300
  13. // MaxMTU is the largest tun.mtu, or route mtu, the underlay can carry.
  14. // Every underlay buffer is udp.MTU bytes, and a relayed packet adds header.MaxOverhead to its plaintext.
  15. const MaxMTU = udp.MTU - header.MaxOverhead
  16. // getMTU reads tun.mtu, capping a value too big for the underlay to carry.
  17. func getMTU(l *slog.Logger, c *config.C) int {
  18. mtu := c.GetInt("tun.mtu", DefaultMTU)
  19. if mtu > MaxMTU {
  20. l.Warn("tun.mtu is too big for the underlay to carry, capping it", "mtu", mtu, "maxMTU", MaxMTU)
  21. return MaxMTU
  22. }
  23. return mtu
  24. }
  25. type NameError struct {
  26. Name string
  27. Underlying error
  28. }
  29. func (e *NameError) Error() string {
  30. return fmt.Sprintf("could not set tun device name: %s because %s", e.Name, e.Underlying)
  31. }
  32. // TODO: We may be able to remove routines
  33. type DeviceFactory func(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, routines int) (Device, error)
  34. func NewDeviceFromConfig(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, routines int) (Device, error) {
  35. c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
  36. r.RestartRequiredIfChanged("tun.disabled")
  37. })
  38. switch {
  39. case c.GetBool("tun.disabled", false):
  40. tun := newDisabledTun(vpnNetworks, c.GetInt("tun.tx_queue", 5000), c.GetBool("stats.message_metrics", false), l)
  41. c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
  42. r.RestartRequiredIfChanged("tun.tx_queue", "stats.message_metrics")
  43. })
  44. return tun, nil
  45. default:
  46. return newTun(c, l, vpnNetworks, routines > 1)
  47. }
  48. }
  49. func NewFdDeviceFromConfig(fd *int) DeviceFactory {
  50. return func(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, routines int) (Device, error) {
  51. return newTunFromFd(c, l, *fd, vpnNetworks)
  52. }
  53. }
  54. func getAllRoutesFromConfig(l *slog.Logger, c *config.C, vpnNetworks []netip.Prefix, initial bool) (bool, []Route, error) {
  55. if !initial && !c.HasChanged("tun.routes") && !c.HasChanged("tun.unsafe_routes") {
  56. return false, nil, nil
  57. }
  58. routes, err := parseRoutes(c, vpnNetworks)
  59. if err != nil {
  60. return true, nil, util.NewContextualError("Could not parse tun.routes", nil, err)
  61. }
  62. unsafeRoutes, err := parseUnsafeRoutes(c, vpnNetworks)
  63. if err != nil {
  64. return true, nil, util.NewContextualError("Could not parse tun.unsafe_routes", nil, err)
  65. }
  66. routes = append(routes, unsafeRoutes...)
  67. for i, r := range routes {
  68. if r.MTU > MaxMTU {
  69. l.Warn("route mtu is too big for the underlay to carry, capping it", "route", r.Cidr, "mtu", r.MTU, "maxMTU", MaxMTU)
  70. routes[i].MTU = MaxMTU
  71. }
  72. }
  73. return true, routes, nil
  74. }
  75. // findRemovedRoutes will return all routes that are not present in the newRoutes list and would affect the system route table.
  76. // Via is not used to evaluate since it does not affect the system route table.
  77. func findRemovedRoutes(newRoutes, oldRoutes []Route) []Route {
  78. var removed []Route
  79. has := func(entry Route) bool {
  80. for _, check := range newRoutes {
  81. if check.Equal(entry) {
  82. return true
  83. }
  84. }
  85. return false
  86. }
  87. for _, oldEntry := range oldRoutes {
  88. if !has(oldEntry) {
  89. removed = append(removed, oldEntry)
  90. }
  91. }
  92. return removed
  93. }
  94. func prefixToMask(prefix netip.Prefix) netip.Addr {
  95. pLen := 128
  96. if prefix.Addr().Is4() {
  97. pLen = 32
  98. }
  99. addr, _ := netip.AddrFromSlice(net.CIDRMask(prefix.Bits(), pLen))
  100. return addr
  101. }
  102. func flipBytes(b []byte) []byte {
  103. for i := range b {
  104. b[i] ^= 0xFF
  105. }
  106. return b
  107. }
  108. func orBytes(a []byte, b []byte) []byte {
  109. ret := make([]byte, len(a))
  110. for i := range a {
  111. ret[i] = a[i] | b[i]
  112. }
  113. return ret
  114. }
  115. func getBroadcast(cidr netip.Prefix) netip.Addr {
  116. broadcast, _ := netip.AddrFromSlice(
  117. orBytes(
  118. cidr.Addr().AsSlice(),
  119. flipBytes(prefixToMask(cidr).AsSlice()),
  120. ),
  121. )
  122. return broadcast
  123. }
  124. func selectGateway(dest netip.Prefix, gateways []netip.Prefix) (netip.Prefix, error) {
  125. for _, gateway := range gateways {
  126. if dest.Addr().Is4() && gateway.Addr().Is4() {
  127. return gateway, nil
  128. }
  129. if dest.Addr().Is6() && gateway.Addr().Is6() {
  130. return gateway, nil
  131. }
  132. }
  133. return netip.Prefix{}, fmt.Errorf("no gateway found for %v in the list of vpn networks", dest)
  134. }