1
0

tun.go 3.0 KB

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