2014-09-01 03:27:34 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"syscall"
|
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
"github.com/vishvananda/netlink/nl"
|
|
|
|
)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
|
|
|
// RtAttr is shared so it is in netlink_linux.go
|
|
|
|
|
|
|
|
// RouteAdd will add a route to the system.
|
|
|
|
// Equivalent to: `ip route add $route`
|
|
|
|
func RouteAdd(route *Route) error {
|
2014-09-19 02:04:48 +00:00
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_NEWROUTE, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
|
2015-07-28 02:29:45 +00:00
|
|
|
return routeHandle(route, req, nl.NewRtMsg())
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RouteAdd will delete a route from the system.
|
|
|
|
// Equivalent to: `ip route del $route`
|
|
|
|
func RouteDel(route *Route) error {
|
2014-09-19 02:04:48 +00:00
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_DELROUTE, syscall.NLM_F_ACK)
|
2015-07-28 02:29:45 +00:00
|
|
|
return routeHandle(route, req, nl.NewRtDelMsg())
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 02:29:45 +00:00
|
|
|
func routeHandle(route *Route, req *nl.NetlinkRequest, msg *nl.RtMsg) error {
|
2015-02-24 18:19:01 +00:00
|
|
|
if (route.Dst == nil || route.Dst.IP == nil) && route.Src == nil && route.Gw == nil {
|
2014-09-01 03:27:34 +00:00
|
|
|
return fmt.Errorf("one of Dst.IP, Src, or Gw must not be nil")
|
|
|
|
}
|
|
|
|
|
2014-09-15 00:44:56 +00:00
|
|
|
msg.Scope = uint8(route.Scope)
|
2014-09-01 03:27:34 +00:00
|
|
|
family := -1
|
2014-09-19 02:04:48 +00:00
|
|
|
var rtAttrs []*nl.RtAttr
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2015-02-24 18:19:01 +00:00
|
|
|
if route.Dst != nil && route.Dst.IP != nil {
|
2014-09-01 03:27:34 +00:00
|
|
|
dstLen, _ := route.Dst.Mask.Size()
|
|
|
|
msg.Dst_len = uint8(dstLen)
|
2014-09-19 02:04:48 +00:00
|
|
|
dstFamily := nl.GetIPFamily(route.Dst.IP)
|
2014-09-01 03:27:34 +00:00
|
|
|
family = dstFamily
|
|
|
|
var dstData []byte
|
2014-09-17 16:33:53 +00:00
|
|
|
if dstFamily == FAMILY_V4 {
|
2014-09-01 03:27:34 +00:00
|
|
|
dstData = route.Dst.IP.To4()
|
|
|
|
} else {
|
|
|
|
dstData = route.Dst.IP.To16()
|
|
|
|
}
|
2014-09-19 02:04:48 +00:00
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_DST, dstData))
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if route.Src != nil {
|
2014-09-19 02:04:48 +00:00
|
|
|
srcFamily := nl.GetIPFamily(route.Src)
|
2014-09-01 03:27:34 +00:00
|
|
|
if family != -1 && family != srcFamily {
|
|
|
|
return fmt.Errorf("source and destination ip are not the same IP family")
|
|
|
|
}
|
|
|
|
family = srcFamily
|
|
|
|
var srcData []byte
|
2014-09-17 16:33:53 +00:00
|
|
|
if srcFamily == FAMILY_V4 {
|
2014-09-01 03:27:34 +00:00
|
|
|
srcData = route.Src.To4()
|
|
|
|
} else {
|
|
|
|
srcData = route.Src.To16()
|
|
|
|
}
|
|
|
|
// The commonly used src ip for routes is actually PREFSRC
|
2014-09-19 02:04:48 +00:00
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_PREFSRC, srcData))
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if route.Gw != nil {
|
2014-09-19 02:04:48 +00:00
|
|
|
gwFamily := nl.GetIPFamily(route.Gw)
|
2014-09-01 03:27:34 +00:00
|
|
|
if family != -1 && family != gwFamily {
|
|
|
|
return fmt.Errorf("gateway, source, and destination ip are not the same IP family")
|
|
|
|
}
|
|
|
|
family = gwFamily
|
|
|
|
var gwData []byte
|
2014-09-17 16:33:53 +00:00
|
|
|
if gwFamily == FAMILY_V4 {
|
2014-09-01 03:27:34 +00:00
|
|
|
gwData = route.Gw.To4()
|
|
|
|
} else {
|
|
|
|
gwData = route.Gw.To16()
|
|
|
|
}
|
2014-09-19 02:04:48 +00:00
|
|
|
rtAttrs = append(rtAttrs, nl.NewRtAttr(syscall.RTA_GATEWAY, gwData))
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg.Family = uint8(family)
|
|
|
|
|
|
|
|
req.AddData(msg)
|
|
|
|
for _, attr := range rtAttrs {
|
|
|
|
req.AddData(attr)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
b = make([]byte, 4)
|
2014-09-19 02:04:48 +00:00
|
|
|
native = nl.NativeEndian()
|
2014-09-01 03:27:34 +00:00
|
|
|
)
|
2014-10-13 23:09:09 +00:00
|
|
|
native.PutUint32(b, uint32(route.LinkIndex))
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
req.AddData(nl.NewRtAttr(syscall.RTA_OIF, b))
|
2014-09-01 03:27:34 +00:00
|
|
|
|
|
|
|
_, err := req.Execute(syscall.NETLINK_ROUTE, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RouteList gets a list of routes in the system.
|
|
|
|
// Equivalent to: `ip route show`.
|
|
|
|
// The list can be filtered by link and ip family.
|
2014-09-24 05:21:11 +00:00
|
|
|
func RouteList(link Link, family int) ([]Route, error) {
|
2014-09-19 02:04:48 +00:00
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_GETROUTE, syscall.NLM_F_DUMP)
|
|
|
|
msg := nl.NewIfInfomsg(family)
|
2014-09-01 03:27:34 +00:00
|
|
|
req.AddData(msg)
|
|
|
|
|
|
|
|
msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWROUTE)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:21:11 +00:00
|
|
|
index := 0
|
|
|
|
if link != nil {
|
|
|
|
base := link.Attrs()
|
|
|
|
ensureIndex(base)
|
|
|
|
index = base.Index
|
|
|
|
}
|
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
native := nl.NativeEndian()
|
2015-06-19 00:41:46 +00:00
|
|
|
var res []Route
|
2015-08-18 00:37:46 +00:00
|
|
|
MsgLoop:
|
2014-09-01 03:27:34 +00:00
|
|
|
for _, m := range msgs {
|
2014-09-19 02:04:48 +00:00
|
|
|
msg := nl.DeserializeRtMsg(m)
|
2014-09-01 03:27:34 +00:00
|
|
|
|
|
|
|
if msg.Flags&syscall.RTM_F_CLONED != 0 {
|
|
|
|
// Ignore cloned routes
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Table != syscall.RT_TABLE_MAIN {
|
|
|
|
// Ignore non-main tables
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-09-19 02:04:48 +00:00
|
|
|
attrs, err := nl.ParseRouteAttr(m[msg.Len():])
|
2014-09-01 03:27:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-09-15 00:44:56 +00:00
|
|
|
route := Route{Scope: Scope(msg.Scope)}
|
2014-09-01 03:27:34 +00:00
|
|
|
for _, attr := range attrs {
|
|
|
|
switch attr.Attr.Type {
|
|
|
|
case syscall.RTA_GATEWAY:
|
|
|
|
route.Gw = net.IP(attr.Value)
|
|
|
|
case syscall.RTA_PREFSRC:
|
|
|
|
route.Src = net.IP(attr.Value)
|
|
|
|
case syscall.RTA_DST:
|
2014-09-07 18:19:59 +00:00
|
|
|
route.Dst = &net.IPNet{
|
2014-09-01 03:27:34 +00:00
|
|
|
IP: attr.Value,
|
|
|
|
Mask: net.CIDRMask(int(msg.Dst_len), 8*len(attr.Value)),
|
|
|
|
}
|
|
|
|
case syscall.RTA_OIF:
|
2014-09-24 05:21:11 +00:00
|
|
|
routeIndex := int(native.Uint32(attr.Value[0:4]))
|
|
|
|
if link != nil && routeIndex != index {
|
2014-09-01 03:27:34 +00:00
|
|
|
// Ignore routes from other interfaces
|
2015-08-18 00:37:46 +00:00
|
|
|
continue MsgLoop
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
2014-10-13 23:09:09 +00:00
|
|
|
route.LinkIndex = routeIndex
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
res = append(res, route)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2015-01-11 18:04:13 +00:00
|
|
|
|
|
|
|
// RouteGet gets a route to a specific destination from the host system.
|
|
|
|
// Equivalent to: 'ip route get'.
|
|
|
|
func RouteGet(destination net.IP) ([]Route, error) {
|
|
|
|
req := nl.NewNetlinkRequest(syscall.RTM_GETROUTE, syscall.NLM_F_REQUEST)
|
|
|
|
family := nl.GetIPFamily(destination)
|
|
|
|
var destinationData []byte
|
|
|
|
var bitlen uint8
|
|
|
|
if family == FAMILY_V4 {
|
|
|
|
destinationData = destination.To4()
|
|
|
|
bitlen = 32
|
|
|
|
} else {
|
|
|
|
destinationData = destination.To16()
|
|
|
|
bitlen = 128
|
|
|
|
}
|
|
|
|
msg := &nl.RtMsg{}
|
|
|
|
msg.Family = uint8(family)
|
|
|
|
msg.Dst_len = bitlen
|
|
|
|
req.AddData(msg)
|
|
|
|
|
|
|
|
rtaDst := nl.NewRtAttr(syscall.RTA_DST, destinationData)
|
|
|
|
req.AddData(rtaDst)
|
|
|
|
|
|
|
|
msgs, err := req.Execute(syscall.NETLINK_ROUTE, syscall.RTM_NEWROUTE)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
native := nl.NativeEndian()
|
2015-06-19 00:41:46 +00:00
|
|
|
var res []Route
|
2015-01-11 18:04:13 +00:00
|
|
|
for _, m := range msgs {
|
|
|
|
msg := nl.DeserializeRtMsg(m)
|
|
|
|
attrs, err := nl.ParseRouteAttr(m[msg.Len():])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
route := Route{}
|
|
|
|
for _, attr := range attrs {
|
|
|
|
switch attr.Attr.Type {
|
|
|
|
case syscall.RTA_GATEWAY:
|
|
|
|
route.Gw = net.IP(attr.Value)
|
|
|
|
case syscall.RTA_PREFSRC:
|
|
|
|
route.Src = net.IP(attr.Value)
|
|
|
|
case syscall.RTA_DST:
|
|
|
|
route.Dst = &net.IPNet{
|
|
|
|
IP: attr.Value,
|
|
|
|
Mask: net.CIDRMask(int(msg.Dst_len), 8*len(attr.Value)),
|
|
|
|
}
|
|
|
|
case syscall.RTA_OIF:
|
|
|
|
routeIndex := int(native.Uint32(attr.Value[0:4]))
|
|
|
|
route.LinkIndex = routeIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = append(res, route)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
|
|
|
|
}
|