1
0

tun.go 3.2 KB

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