route_linux.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package osl
  2. import (
  3. "fmt"
  4. "net"
  5. "github.com/docker/docker/libnetwork/types"
  6. "github.com/vishvananda/netlink"
  7. )
  8. // Gateway returns the IPv4 gateway for the sandbox.
  9. func (n *Namespace) Gateway() net.IP {
  10. n.mu.Lock()
  11. defer n.mu.Unlock()
  12. return n.gw
  13. }
  14. // GatewayIPv6 returns the IPv6 gateway for the sandbox.
  15. func (n *Namespace) GatewayIPv6() net.IP {
  16. n.mu.Lock()
  17. defer n.mu.Unlock()
  18. return n.gwv6
  19. }
  20. // StaticRoutes returns additional static routes for the sandbox. Note that
  21. // directly connected routes are stored on the particular interface they
  22. // refer to.
  23. func (n *Namespace) StaticRoutes() []*types.StaticRoute {
  24. n.mu.Lock()
  25. defer n.mu.Unlock()
  26. routes := make([]*types.StaticRoute, len(n.staticRoutes))
  27. for i, route := range n.staticRoutes {
  28. r := route.GetCopy()
  29. routes[i] = r
  30. }
  31. return routes
  32. }
  33. func (n *Namespace) setGateway(gw net.IP) {
  34. n.mu.Lock()
  35. n.gw = gw
  36. n.mu.Unlock()
  37. }
  38. func (n *Namespace) setGatewayIPv6(gwv6 net.IP) {
  39. n.mu.Lock()
  40. n.gwv6 = gwv6
  41. n.mu.Unlock()
  42. }
  43. // SetGateway sets the default IPv4 gateway for the sandbox.
  44. func (n *Namespace) SetGateway(gw net.IP) error {
  45. // Silently return if the gateway is empty
  46. if len(gw) == 0 {
  47. return nil
  48. }
  49. err := n.programGateway(gw, true)
  50. if err == nil {
  51. n.setGateway(gw)
  52. }
  53. return err
  54. }
  55. // UnsetGateway the previously set default IPv4 gateway in the sandbox.
  56. func (n *Namespace) UnsetGateway() error {
  57. gw := n.Gateway()
  58. // Silently return if the gateway is empty
  59. if len(gw) == 0 {
  60. return nil
  61. }
  62. err := n.programGateway(gw, false)
  63. if err == nil {
  64. n.setGateway(net.IP{})
  65. }
  66. return err
  67. }
  68. func (n *Namespace) programGateway(gw net.IP, isAdd bool) error {
  69. gwRoutes, err := n.nlHandle.RouteGet(gw)
  70. if err != nil {
  71. return fmt.Errorf("route for the gateway %s could not be found: %v", gw, err)
  72. }
  73. var linkIndex int
  74. for _, gwRoute := range gwRoutes {
  75. if gwRoute.Gw == nil {
  76. linkIndex = gwRoute.LinkIndex
  77. break
  78. }
  79. }
  80. if linkIndex == 0 {
  81. return fmt.Errorf("Direct route for the gateway %s could not be found", gw)
  82. }
  83. if isAdd {
  84. return n.nlHandle.RouteAdd(&netlink.Route{
  85. Scope: netlink.SCOPE_UNIVERSE,
  86. LinkIndex: linkIndex,
  87. Gw: gw,
  88. })
  89. }
  90. return n.nlHandle.RouteDel(&netlink.Route{
  91. Scope: netlink.SCOPE_UNIVERSE,
  92. LinkIndex: linkIndex,
  93. Gw: gw,
  94. })
  95. }
  96. // Program a route in to the namespace routing table.
  97. func (n *Namespace) programRoute(dest *net.IPNet, nh net.IP) error {
  98. gwRoutes, err := n.nlHandle.RouteGet(nh)
  99. if err != nil {
  100. return fmt.Errorf("route for the next hop %s could not be found: %v", nh, err)
  101. }
  102. return n.nlHandle.RouteAdd(&netlink.Route{
  103. Scope: netlink.SCOPE_UNIVERSE,
  104. LinkIndex: gwRoutes[0].LinkIndex,
  105. Gw: nh,
  106. Dst: dest,
  107. })
  108. }
  109. // Delete a route from the namespace routing table.
  110. func (n *Namespace) removeRoute(dest *net.IPNet, nh net.IP) error {
  111. gwRoutes, err := n.nlHandle.RouteGet(nh)
  112. if err != nil {
  113. return fmt.Errorf("route for the next hop could not be found: %v", err)
  114. }
  115. return n.nlHandle.RouteDel(&netlink.Route{
  116. Scope: netlink.SCOPE_UNIVERSE,
  117. LinkIndex: gwRoutes[0].LinkIndex,
  118. Gw: nh,
  119. Dst: dest,
  120. })
  121. }
  122. // SetGatewayIPv6 sets the default IPv6 gateway for the sandbox.
  123. func (n *Namespace) SetGatewayIPv6(gwv6 net.IP) error {
  124. // Silently return if the gateway is empty
  125. if len(gwv6) == 0 {
  126. return nil
  127. }
  128. err := n.programGateway(gwv6, true)
  129. if err == nil {
  130. n.setGatewayIPv6(gwv6)
  131. }
  132. return err
  133. }
  134. // UnsetGatewayIPv6 unsets the previously set default IPv6 gateway in the sandbox.
  135. func (n *Namespace) UnsetGatewayIPv6() error {
  136. gwv6 := n.GatewayIPv6()
  137. // Silently return if the gateway is empty
  138. if len(gwv6) == 0 {
  139. return nil
  140. }
  141. err := n.programGateway(gwv6, false)
  142. if err == nil {
  143. n.mu.Lock()
  144. n.gwv6 = net.IP{}
  145. n.mu.Unlock()
  146. }
  147. return err
  148. }
  149. // AddStaticRoute adds a static route to the sandbox.
  150. func (n *Namespace) AddStaticRoute(r *types.StaticRoute) error {
  151. err := n.programRoute(r.Destination, r.NextHop)
  152. if err == nil {
  153. n.mu.Lock()
  154. n.staticRoutes = append(n.staticRoutes, r)
  155. n.mu.Unlock()
  156. }
  157. return err
  158. }
  159. // RemoveStaticRoute removes a static route from the sandbox.
  160. func (n *Namespace) RemoveStaticRoute(r *types.StaticRoute) error {
  161. err := n.removeRoute(r.Destination, r.NextHop)
  162. if err == nil {
  163. n.mu.Lock()
  164. lastIndex := len(n.staticRoutes) - 1
  165. for i, v := range n.staticRoutes {
  166. if v == r {
  167. // Overwrite the route we're removing with the last element
  168. n.staticRoutes[i] = n.staticRoutes[lastIndex]
  169. // Shorten the slice to trim the extra element
  170. n.staticRoutes = n.staticRoutes[:lastIndex]
  171. break
  172. }
  173. }
  174. n.mu.Unlock()
  175. }
  176. return err
  177. }