2
0

route.go 5.0 KB

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