tun.go 3.4 KB

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