tun.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. switch {
  36. case c.GetBool("tun.disabled", false):
  37. tun := newDisabledTun(vpnNetworks, c.GetInt("tun.tx_queue", 5000), c.GetBool("stats.message_metrics", false), l)
  38. return tun, nil
  39. default:
  40. return newTun(c, l, vpnNetworks, routines > 1)
  41. }
  42. }
  43. func NewFdDeviceFromConfig(fd *int) DeviceFactory {
  44. return func(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, routines int) (Device, error) {
  45. return newTunFromFd(c, l, *fd, vpnNetworks)
  46. }
  47. }
  48. func getAllRoutesFromConfig(l *slog.Logger, c *config.C, vpnNetworks []netip.Prefix, initial bool) (bool, []Route, error) {
  49. if !initial && !c.HasChanged("tun.routes") && !c.HasChanged("tun.unsafe_routes") {
  50. return false, nil, nil
  51. }
  52. routes, err := parseRoutes(c, vpnNetworks)
  53. if err != nil {
  54. return true, nil, util.NewContextualError("Could not parse tun.routes", nil, err)
  55. }
  56. unsafeRoutes, err := parseUnsafeRoutes(c, vpnNetworks)
  57. if err != nil {
  58. return true, nil, util.NewContextualError("Could not parse tun.unsafe_routes", nil, err)
  59. }
  60. routes = append(routes, unsafeRoutes...)
  61. for i, r := range routes {
  62. if r.MTU > MaxMTU {
  63. l.Warn("route mtu is too big for the underlay to carry, capping it", "route", r.Cidr, "mtu", r.MTU, "maxMTU", MaxMTU)
  64. routes[i].MTU = MaxMTU
  65. }
  66. }
  67. return true, routes, nil
  68. }
  69. // findRemovedRoutes will return all routes that are not present in the newRoutes list and would affect the system route table.
  70. // Via is not used to evaluate since it does not affect the system route table.
  71. func findRemovedRoutes(newRoutes, oldRoutes []Route) []Route {
  72. var removed []Route
  73. has := func(entry Route) bool {
  74. for _, check := range newRoutes {
  75. if check.Equal(entry) {
  76. return true
  77. }
  78. }
  79. return false
  80. }
  81. for _, oldEntry := range oldRoutes {
  82. if !has(oldEntry) {
  83. removed = append(removed, oldEntry)
  84. }
  85. }
  86. return removed
  87. }
  88. func prefixToMask(prefix netip.Prefix) netip.Addr {
  89. pLen := 128
  90. if prefix.Addr().Is4() {
  91. pLen = 32
  92. }
  93. addr, _ := netip.AddrFromSlice(net.CIDRMask(prefix.Bits(), pLen))
  94. return addr
  95. }
  96. func flipBytes(b []byte) []byte {
  97. for i := range b {
  98. b[i] ^= 0xFF
  99. }
  100. return b
  101. }
  102. func orBytes(a []byte, b []byte) []byte {
  103. ret := make([]byte, len(a))
  104. for i := range a {
  105. ret[i] = a[i] | b[i]
  106. }
  107. return ret
  108. }
  109. func getBroadcast(cidr netip.Prefix) netip.Addr {
  110. broadcast, _ := netip.AddrFromSlice(
  111. orBytes(
  112. cidr.Addr().AsSlice(),
  113. flipBytes(prefixToMask(cidr).AsSlice()),
  114. ),
  115. )
  116. return broadcast
  117. }
  118. func selectGateway(dest netip.Prefix, gateways []netip.Prefix) (netip.Prefix, error) {
  119. for _, gateway := range gateways {
  120. if dest.Addr().Is4() && gateway.Addr().Is4() {
  121. return gateway, nil
  122. }
  123. if dest.Addr().Is6() && gateway.Addr().Is6() {
  124. return gateway, nil
  125. }
  126. }
  127. return netip.Prefix{}, fmt.Errorf("no gateway found for %v in the list of vpn networks", dest)
  128. }