tun.go 3.4 KB

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