tun.go 3.1 KB

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