route.go 5.9 KB

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