2
0

tun_common.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package nebula
  2. import (
  3. "fmt"
  4. "net"
  5. "strconv"
  6. )
  7. const DEFAULT_MTU = 1300
  8. type route struct {
  9. mtu int
  10. route *net.IPNet
  11. via *net.IP
  12. }
  13. func parseRoutes(config *Config, network *net.IPNet) ([]route, error) {
  14. var err error
  15. r := config.Get("tun.routes")
  16. if r == nil {
  17. return []route{}, nil
  18. }
  19. rawRoutes, ok := r.([]interface{})
  20. if !ok {
  21. return nil, fmt.Errorf("tun.routes is not an array")
  22. }
  23. if len(rawRoutes) < 1 {
  24. return []route{}, nil
  25. }
  26. routes := make([]route, len(rawRoutes))
  27. for i, r := range rawRoutes {
  28. m, ok := r.(map[interface{}]interface{})
  29. if !ok {
  30. return nil, fmt.Errorf("entry %v in tun.routes is invalid", i+1)
  31. }
  32. rMtu, ok := m["mtu"]
  33. if !ok {
  34. return nil, fmt.Errorf("entry %v.mtu in tun.routes is not present", i+1)
  35. }
  36. mtu, ok := rMtu.(int)
  37. if !ok {
  38. mtu, err = strconv.Atoi(rMtu.(string))
  39. if err != nil {
  40. return nil, fmt.Errorf("entry %v.mtu in tun.routes is not an integer: %v", i+1, err)
  41. }
  42. }
  43. if mtu < 500 {
  44. return nil, fmt.Errorf("entry %v.mtu in tun.routes is below 500: %v", i+1, mtu)
  45. }
  46. rRoute, ok := m["route"]
  47. if !ok {
  48. return nil, fmt.Errorf("entry %v.route in tun.routes is not present", i+1)
  49. }
  50. r := route{
  51. mtu: mtu,
  52. }
  53. _, r.route, err = net.ParseCIDR(fmt.Sprintf("%v", rRoute))
  54. if err != nil {
  55. return nil, fmt.Errorf("entry %v.route in tun.routes failed to parse: %v", i+1, err)
  56. }
  57. if !ipWithin(network, r.route) {
  58. return nil, fmt.Errorf(
  59. "entry %v.route in tun.routes is not contained within the network attached to the certificate; route: %v, network: %v",
  60. i+1,
  61. r.route.String(),
  62. network.String(),
  63. )
  64. }
  65. routes[i] = r
  66. }
  67. return routes, nil
  68. }
  69. func parseUnsafeRoutes(config *Config, network *net.IPNet) ([]route, error) {
  70. var err error
  71. r := config.Get("tun.unsafe_routes")
  72. if r == nil {
  73. return []route{}, nil
  74. }
  75. rawRoutes, ok := r.([]interface{})
  76. if !ok {
  77. return nil, fmt.Errorf("tun.unsafe_routes is not an array")
  78. }
  79. if len(rawRoutes) < 1 {
  80. return []route{}, nil
  81. }
  82. routes := make([]route, len(rawRoutes))
  83. for i, r := range rawRoutes {
  84. m, ok := r.(map[interface{}]interface{})
  85. if !ok {
  86. return nil, fmt.Errorf("entry %v in tun.unsafe_routes is invalid", i+1)
  87. }
  88. rMtu, ok := m["mtu"]
  89. if !ok {
  90. rMtu = config.GetInt("tun.mtu", DEFAULT_MTU)
  91. }
  92. mtu, ok := rMtu.(int)
  93. if !ok {
  94. mtu, err = strconv.Atoi(rMtu.(string))
  95. if err != nil {
  96. return nil, fmt.Errorf("entry %v.mtu in tun.unsafe_routes is not an integer: %v", i+1, err)
  97. }
  98. }
  99. if mtu < 500 {
  100. return nil, fmt.Errorf("entry %v.mtu in tun.unsafe_routes is below 500: %v", i+1, mtu)
  101. }
  102. rVia, ok := m["via"]
  103. if !ok {
  104. return nil, fmt.Errorf("entry %v.via in tun.unsafe_routes is not present", i+1)
  105. }
  106. via, ok := rVia.(string)
  107. if !ok {
  108. return nil, fmt.Errorf("entry %v.via in tun.unsafe_routes is not a string: found %T", i+1, rVia)
  109. }
  110. nVia := net.ParseIP(via)
  111. if nVia == nil {
  112. return nil, fmt.Errorf("entry %v.via in tun.unsafe_routes failed to parse address: %v", i+1, via)
  113. }
  114. rRoute, ok := m["route"]
  115. if !ok {
  116. return nil, fmt.Errorf("entry %v.route in tun.unsafe_routes is not present", i+1)
  117. }
  118. r := route{
  119. via: &nVia,
  120. mtu: mtu,
  121. }
  122. _, r.route, err = net.ParseCIDR(fmt.Sprintf("%v", rRoute))
  123. if err != nil {
  124. return nil, fmt.Errorf("entry %v.route in tun.unsafe_routes failed to parse: %v", i+1, err)
  125. }
  126. if ipWithin(network, r.route) {
  127. return nil, fmt.Errorf(
  128. "entry %v.route in tun.unsafe_routes is contained within the network attached to the certificate; route: %v, network: %v",
  129. i+1,
  130. r.route.String(),
  131. network.String(),
  132. )
  133. }
  134. routes[i] = r
  135. }
  136. return routes, nil
  137. }
  138. func ipWithin(o *net.IPNet, i *net.IPNet) bool {
  139. // Make sure o contains the lowest form of i
  140. if !o.Contains(i.IP.Mask(i.Mask)) {
  141. return false
  142. }
  143. // Find the max ip in i
  144. ip4 := i.IP.To4()
  145. if ip4 == nil {
  146. return false
  147. }
  148. last := make(net.IP, len(ip4))
  149. copy(last, ip4)
  150. for x := range ip4 {
  151. last[x] |= ^i.Mask[x]
  152. }
  153. // Make sure o contains the max
  154. if !o.Contains(last) {
  155. return false
  156. }
  157. return true
  158. }