route_linux.go 4.1 KB

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