tun_common.go 4.5 KB

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