2015-07-02 01:00:48 -04:00
|
|
|
package osl
|
2015-06-04 23:21:23 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2015-06-04 23:21:23 -04:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (n *networkNamespace) Gateway() net.IP {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
return n.gw
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) GatewayIPv6() net.IP {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
return n.gwv6
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) StaticRoutes() []*types.StaticRoute {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
routes := make([]*types.StaticRoute, len(n.staticRoutes))
|
|
|
|
for i, route := range n.staticRoutes {
|
|
|
|
r := route.GetCopy()
|
|
|
|
routes[i] = r
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) setGateway(gw net.IP) {
|
|
|
|
n.Lock()
|
|
|
|
n.gw = gw
|
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) setGatewayIPv6(gwv6 net.IP) {
|
|
|
|
n.Lock()
|
|
|
|
n.gwv6 = gwv6
|
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) SetGateway(gw net.IP) error {
|
|
|
|
// Silently return if the gateway is empty
|
|
|
|
if len(gw) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
err := n.programGateway(gw, true)
|
2015-06-04 23:21:23 -04:00
|
|
|
if err == nil {
|
|
|
|
n.setGateway(gw)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) UnsetGateway() error {
|
|
|
|
gw := n.Gateway()
|
|
|
|
|
|
|
|
// Silently return if the gateway is empty
|
|
|
|
if len(gw) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
err := n.programGateway(gw, false)
|
2015-06-04 23:21:23 -04:00
|
|
|
if err == nil {
|
|
|
|
n.setGateway(net.IP{})
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
func (n *networkNamespace) programGateway(gw net.IP, isAdd bool) error {
|
|
|
|
gwRoutes, err := n.nlHandle.RouteGet(gw)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("route for the gateway %s could not be found: %v", gw, err)
|
|
|
|
}
|
2015-06-04 23:21:23 -04:00
|
|
|
|
2016-06-23 16:21:58 -04:00
|
|
|
var linkIndex int
|
|
|
|
for _, gwRoute := range gwRoutes {
|
|
|
|
if gwRoute.Gw == nil {
|
|
|
|
linkIndex = gwRoute.LinkIndex
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if linkIndex == 0 {
|
|
|
|
return fmt.Errorf("Direct route for the gateway %s could not be found", gw)
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
if isAdd {
|
|
|
|
return n.nlHandle.RouteAdd(&netlink.Route{
|
2015-06-04 23:21:23 -04:00
|
|
|
Scope: netlink.SCOPE_UNIVERSE,
|
2016-06-23 16:21:58 -04:00
|
|
|
LinkIndex: linkIndex,
|
2015-06-04 23:21:23 -04:00
|
|
|
Gw: gw,
|
|
|
|
})
|
2016-05-16 14:51:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return n.nlHandle.RouteDel(&netlink.Route{
|
|
|
|
Scope: netlink.SCOPE_UNIVERSE,
|
2016-06-23 16:21:58 -04:00
|
|
|
LinkIndex: linkIndex,
|
2016-05-16 14:51:40 -04:00
|
|
|
Gw: gw,
|
2015-06-04 23:21:23 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Program a route in to the namespace routing table.
|
2016-05-16 14:51:40 -04:00
|
|
|
func (n *networkNamespace) programRoute(path string, dest *net.IPNet, nh net.IP) error {
|
|
|
|
gwRoutes, err := n.nlHandle.RouteGet(nh)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("route for the next hop %s could not be found: %v", nh, err)
|
|
|
|
}
|
2015-06-04 23:21:23 -04:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
return n.nlHandle.RouteAdd(&netlink.Route{
|
|
|
|
Scope: netlink.SCOPE_UNIVERSE,
|
|
|
|
LinkIndex: gwRoutes[0].LinkIndex,
|
|
|
|
Gw: nh,
|
|
|
|
Dst: dest,
|
2015-06-04 23:21:23 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete a route from the namespace routing table.
|
2016-05-16 14:51:40 -04:00
|
|
|
func (n *networkNamespace) removeRoute(path string, dest *net.IPNet, nh net.IP) error {
|
|
|
|
gwRoutes, err := n.nlHandle.RouteGet(nh)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("route for the next hop could not be found: %v", err)
|
|
|
|
}
|
2015-06-04 23:21:23 -04:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
return n.nlHandle.RouteDel(&netlink.Route{
|
|
|
|
Scope: netlink.SCOPE_UNIVERSE,
|
|
|
|
LinkIndex: gwRoutes[0].LinkIndex,
|
|
|
|
Gw: nh,
|
|
|
|
Dst: dest,
|
2015-06-04 23:21:23 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) SetGatewayIPv6(gwv6 net.IP) error {
|
|
|
|
// Silently return if the gateway is empty
|
|
|
|
if len(gwv6) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
err := n.programGateway(gwv6, true)
|
2015-06-04 23:21:23 -04:00
|
|
|
if err == nil {
|
2016-04-28 15:49:40 -04:00
|
|
|
n.setGatewayIPv6(gwv6)
|
2015-06-04 23:21:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) UnsetGatewayIPv6() error {
|
|
|
|
gwv6 := n.GatewayIPv6()
|
|
|
|
|
|
|
|
// Silently return if the gateway is empty
|
|
|
|
if len(gwv6) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
err := n.programGateway(gwv6, false)
|
2015-06-04 23:21:23 -04:00
|
|
|
if err == nil {
|
|
|
|
n.Lock()
|
|
|
|
n.gwv6 = net.IP{}
|
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) AddStaticRoute(r *types.StaticRoute) error {
|
2016-05-16 14:51:40 -04:00
|
|
|
err := n.programRoute(n.nsPath(), r.Destination, r.NextHop)
|
2015-06-04 23:21:23 -04:00
|
|
|
if err == nil {
|
|
|
|
n.Lock()
|
|
|
|
n.staticRoutes = append(n.staticRoutes, r)
|
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) RemoveStaticRoute(r *types.StaticRoute) error {
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
err := n.removeRoute(n.nsPath(), r.Destination, r.NextHop)
|
2015-06-04 23:21:23 -04:00
|
|
|
if err == nil {
|
|
|
|
n.Lock()
|
|
|
|
lastIndex := len(n.staticRoutes) - 1
|
|
|
|
for i, v := range n.staticRoutes {
|
|
|
|
if v == r {
|
|
|
|
// Overwrite the route we're removing with the last element
|
|
|
|
n.staticRoutes[i] = n.staticRoutes[lastIndex]
|
|
|
|
// Shorten the slice to trim the extra element
|
|
|
|
n.staticRoutes = n.staticRoutes[:lastIndex]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|