route.go 5.3 KB

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